/
ip_mroute.c
3590 lines (3090 loc) · 86.8 KB
/
ip_mroute.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* $NetBSD: ip_mroute.c,v 1.100.2.1 2008/05/17 16:11:40 bouyer Exp $ */
/*
* Copyright (c) 1992, 1993
* The Regents of the University of California. All rights reserved.
*
* This code is derived from software contributed to Berkeley by
* Stephen Deering of Stanford University.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. Neither the name of the University nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*
* @(#)ip_mroute.c 8.2 (Berkeley) 11/15/93
*/
/*
* Copyright (c) 1989 Stephen Deering
*
* This code is derived from software contributed to Berkeley by
* Stephen Deering of Stanford University.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. All advertising materials mentioning features or use of this software
* must display the following acknowledgement:
* This product includes software developed by the University of
* California, Berkeley and its contributors.
* 4. Neither the name of the University nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*
* @(#)ip_mroute.c 8.2 (Berkeley) 11/15/93
*/
/*
* IP multicast forwarding procedures
*
* Written by David Waitzman, BBN Labs, August 1988.
* Modified by Steve Deering, Stanford, February 1989.
* Modified by Mark J. Steiglitz, Stanford, May, 1991
* Modified by Van Jacobson, LBL, January 1993
* Modified by Ajit Thyagarajan, PARC, August 1993
* Modified by Bill Fenner, PARC, April 1994
* Modified by Charles M. Hannum, NetBSD, May 1995.
* Modified by Ahmed Helmy, SGI, June 1996
* Modified by George Edmond Eddy (Rusty), ISI, February 1998
* Modified by Pavlin Radoslavov, USC/ISI, May 1998, August 1999, October 2000
* Modified by Hitoshi Asaeda, WIDE, August 2000
* Modified by Pavlin Radoslavov, ICSI, October 2002
*
* MROUTING Revision: 1.2
* and PIM-SMv2 and PIM-DM support, advanced API support,
* bandwidth metering and signaling
*/
#include <sys/cdefs.h>
__KERNEL_RCSID(0, "$NetBSD: ip_mroute.c,v 1.100.2.1 2008/05/17 16:11:40 bouyer Exp $");
#include "opt_inet.h"
#include "opt_ipsec.h"
#include "opt_pim.h"
#ifdef PIM
#define _PIM_VT 1
#endif
#include <sys/param.h>
#include <sys/systm.h>
#include <sys/callout.h>
#include <sys/mbuf.h>
#include <sys/socket.h>
#include <sys/socketvar.h>
#include <sys/protosw.h>
#include <sys/errno.h>
#include <sys/time.h>
#include <sys/kernel.h>
#include <sys/ioctl.h>
#include <sys/syslog.h>
#include <net/if.h>
#include <net/route.h>
#include <net/raw_cb.h>
#include <netinet/in.h>
#include <netinet/in_var.h>
#include <netinet/in_systm.h>
#include <netinet/ip.h>
#include <netinet/ip_var.h>
#include <netinet/in_pcb.h>
#include <netinet/udp.h>
#include <netinet/igmp.h>
#include <netinet/igmp_var.h>
#include <netinet/ip_mroute.h>
#ifdef PIM
#include <netinet/pim.h>
#include <netinet/pim_var.h>
#endif
#include <netinet/ip_encap.h>
#ifdef IPSEC
#include <netinet6/ipsec.h>
#include <netkey/key.h>
#endif
#ifdef FAST_IPSEC
#include <netipsec/ipsec.h>
#include <netipsec/key.h>
#endif
#include <machine/stdarg.h>
#define IP_MULTICASTOPTS 0
#define M_PULLUP(m, len) \
do { \
if ((m) && ((m)->m_flags & M_EXT || (m)->m_len < (len))) \
(m) = m_pullup((m), (len)); \
} while (/*CONSTCOND*/ 0)
/*
* Globals. All but ip_mrouter and ip_mrtproto could be static,
* except for netstat or debugging purposes.
*/
struct socket *ip_mrouter = NULL;
int ip_mrtproto = IGMP_DVMRP; /* for netstat only */
#define NO_RTE_FOUND 0x1
#define RTE_FOUND 0x2
#define MFCHASH(a, g) \
((((a).s_addr >> 20) ^ ((a).s_addr >> 10) ^ (a).s_addr ^ \
((g).s_addr >> 20) ^ ((g).s_addr >> 10) ^ (g).s_addr) & mfchash)
LIST_HEAD(mfchashhdr, mfc) *mfchashtbl;
u_long mfchash;
u_char nexpire[MFCTBLSIZ];
struct vif viftable[MAXVIFS];
struct mrtstat mrtstat;
u_int mrtdebug = 0; /* debug level */
#define DEBUG_MFC 0x02
#define DEBUG_FORWARD 0x04
#define DEBUG_EXPIRE 0x08
#define DEBUG_XMIT 0x10
#define DEBUG_PIM 0x20
#define VIFI_INVALID ((vifi_t) -1)
u_int tbfdebug = 0; /* tbf debug level */
#ifdef RSVP_ISI
u_int rsvpdebug = 0; /* rsvp debug level */
extern struct socket *ip_rsvpd;
extern int rsvp_on;
#endif /* RSVP_ISI */
/* vif attachment using sys/netinet/ip_encap.c */
static void vif_input(struct mbuf *, ...);
static int vif_encapcheck(struct mbuf *, int, int, void *);
static const struct protosw vif_protosw =
{ SOCK_RAW, &inetdomain, IPPROTO_IPV4, PR_ATOMIC|PR_ADDR,
vif_input, rip_output, 0, rip_ctloutput,
rip_usrreq,
0, 0, 0, 0,
};
#define EXPIRE_TIMEOUT (hz / 4) /* 4x / second */
#define UPCALL_EXPIRE 6 /* number of timeouts */
/*
* Define the token bucket filter structures
*/
#define TBF_REPROCESS (hz / 100) /* 100x / second */
static int get_sg_cnt(struct sioc_sg_req *);
static int get_vif_cnt(struct sioc_vif_req *);
static int ip_mrouter_init(struct socket *, struct mbuf *);
static int get_version(struct mbuf *);
static int set_assert(struct mbuf *);
static int get_assert(struct mbuf *);
static int add_vif(struct mbuf *);
static int del_vif(struct mbuf *);
static void update_mfc_params(struct mfc *, struct mfcctl2 *);
static void init_mfc_params(struct mfc *, struct mfcctl2 *);
static void expire_mfc(struct mfc *);
static int add_mfc(struct mbuf *);
#ifdef UPCALL_TIMING
static void collate(struct timeval *);
#endif
static int del_mfc(struct mbuf *);
static int set_api_config(struct mbuf *); /* chose API capabilities */
static int get_api_support(struct mbuf *);
static int get_api_config(struct mbuf *);
static int socket_send(struct socket *, struct mbuf *, struct sockaddr_in *);
static void expire_upcalls(void *);
#ifdef RSVP_ISI
static int ip_mdq(struct mbuf *, struct ifnet *, struct mfc *, vifi_t);
#else
static int ip_mdq(struct mbuf *, struct ifnet *, struct mfc *);
#endif
static void phyint_send(struct ip *, struct vif *, struct mbuf *);
static void encap_send(struct ip *, struct vif *, struct mbuf *);
static void tbf_control(struct vif *, struct mbuf *, struct ip *, u_int32_t);
static void tbf_queue(struct vif *, struct mbuf *);
static void tbf_process_q(struct vif *);
static void tbf_reprocess_q(void *);
static int tbf_dq_sel(struct vif *, struct ip *);
static void tbf_send_packet(struct vif *, struct mbuf *);
static void tbf_update_tokens(struct vif *);
static int priority(struct vif *, struct ip *);
/*
* Bandwidth monitoring
*/
static void free_bw_list(struct bw_meter *);
static int add_bw_upcall(struct mbuf *);
static int del_bw_upcall(struct mbuf *);
static void bw_meter_receive_packet(struct bw_meter *, int , struct timeval *);
static void bw_meter_prepare_upcall(struct bw_meter *, struct timeval *);
static void bw_upcalls_send(void);
static void schedule_bw_meter(struct bw_meter *, struct timeval *);
static void unschedule_bw_meter(struct bw_meter *);
static void bw_meter_process(void);
static void expire_bw_upcalls_send(void *);
static void expire_bw_meter_process(void *);
#ifdef PIM
static int pim_register_send(struct ip *, struct vif *,
struct mbuf *, struct mfc *);
static int pim_register_send_rp(struct ip *, struct vif *,
struct mbuf *, struct mfc *);
static int pim_register_send_upcall(struct ip *, struct vif *,
struct mbuf *, struct mfc *);
static struct mbuf *pim_register_prepare(struct ip *, struct mbuf *);
#endif
/*
* 'Interfaces' associated with decapsulator (so we can tell
* packets that went through it from ones that get reflected
* by a broken gateway). These interfaces are never linked into
* the system ifnet list & no routes point to them. I.e., packets
* can't be sent this way. They only exist as a placeholder for
* multicast source verification.
*/
#if 0
struct ifnet multicast_decap_if[MAXVIFS];
#endif
#define ENCAP_TTL 64
#define ENCAP_PROTO IPPROTO_IPIP /* 4 */
/* prototype IP hdr for encapsulated packets */
struct ip multicast_encap_iphdr = {
.ip_hl = sizeof(struct ip) >> 2,
.ip_v = IPVERSION,
.ip_len = sizeof(struct ip),
.ip_ttl = ENCAP_TTL,
.ip_p = ENCAP_PROTO,
};
/*
* Bandwidth meter variables and constants
*/
/*
* Pending timeouts are stored in a hash table, the key being the
* expiration time. Periodically, the entries are analysed and processed.
*/
#define BW_METER_BUCKETS 1024
static struct bw_meter *bw_meter_timers[BW_METER_BUCKETS];
struct callout bw_meter_ch;
#define BW_METER_PERIOD (hz) /* periodical handling of bw meters */
/*
* Pending upcalls are stored in a vector which is flushed when
* full, or periodically
*/
static struct bw_upcall bw_upcalls[BW_UPCALLS_MAX];
static u_int bw_upcalls_n; /* # of pending upcalls */
struct callout bw_upcalls_ch;
#define BW_UPCALLS_PERIOD (hz) /* periodical flush of bw upcalls */
#ifdef PIM
struct pimstat pimstat;
/*
* Note: the PIM Register encapsulation adds the following in front of a
* data packet:
*
* struct pim_encap_hdr {
* struct ip ip;
* struct pim_encap_pimhdr pim;
* }
*
*/
struct pim_encap_pimhdr {
struct pim pim;
uint32_t flags;
};
static struct ip pim_encap_iphdr = {
.ip_v = IPVERSION,
.ip_hl = sizeof(struct ip) >> 2,
.ip_len = sizeof(struct ip),
.ip_ttl = ENCAP_TTL,
.ip_p = IPPROTO_PIM,
};
static struct pim_encap_pimhdr pim_encap_pimhdr = {
{
PIM_MAKE_VT(PIM_VERSION, PIM_REGISTER), /* PIM vers and message type */
0, /* reserved */
0, /* checksum */
},
0 /* flags */
};
static struct ifnet multicast_register_if;
static vifi_t reg_vif_num = VIFI_INVALID;
#endif /* PIM */
/*
* Private variables.
*/
static vifi_t numvifs = 0;
static struct callout expire_upcalls_ch;
/*
* whether or not special PIM assert processing is enabled.
*/
static int pim_assert;
/*
* Rate limit for assert notification messages, in usec
*/
#define ASSERT_MSG_TIME 3000000
/*
* Kernel multicast routing API capabilities and setup.
* If more API capabilities are added to the kernel, they should be
* recorded in `mrt_api_support'.
*/
static const u_int32_t mrt_api_support = (MRT_MFC_FLAGS_DISABLE_WRONGVIF |
MRT_MFC_FLAGS_BORDER_VIF |
MRT_MFC_RP |
MRT_MFC_BW_UPCALL);
static u_int32_t mrt_api_config = 0;
/*
* Find a route for a given origin IP address and Multicast group address
* Type of service parameter to be added in the future!!!
* Statistics are updated by the caller if needed
* (mrtstat.mrts_mfc_lookups and mrtstat.mrts_mfc_misses)
*/
static struct mfc *
mfc_find(struct in_addr *o, struct in_addr *g)
{
struct mfc *rt;
LIST_FOREACH(rt, &mfchashtbl[MFCHASH(*o, *g)], mfc_hash) {
if (in_hosteq(rt->mfc_origin, *o) &&
in_hosteq(rt->mfc_mcastgrp, *g) &&
(rt->mfc_stall == NULL))
break;
}
return (rt);
}
/*
* Macros to compute elapsed time efficiently
* Borrowed from Van Jacobson's scheduling code
*/
#define TV_DELTA(a, b, delta) do { \
int xxs; \
delta = (a).tv_usec - (b).tv_usec; \
xxs = (a).tv_sec - (b).tv_sec; \
switch (xxs) { \
case 2: \
delta += 1000000; \
/* fall through */ \
case 1: \
delta += 1000000; \
/* fall through */ \
case 0: \
break; \
default: \
delta += (1000000 * xxs); \
break; \
} \
} while (/*CONSTCOND*/ 0)
#ifdef UPCALL_TIMING
u_int32_t upcall_data[51];
#endif /* UPCALL_TIMING */
/*
* Handle MRT setsockopt commands to modify the multicast routing tables.
*/
int
ip_mrouter_set(struct socket *so, int optname, struct mbuf **m)
{
int error;
if (optname != MRT_INIT && so != ip_mrouter)
error = ENOPROTOOPT;
else
switch (optname) {
case MRT_INIT:
error = ip_mrouter_init(so, *m);
break;
case MRT_DONE:
error = ip_mrouter_done();
break;
case MRT_ADD_VIF:
error = add_vif(*m);
break;
case MRT_DEL_VIF:
error = del_vif(*m);
break;
case MRT_ADD_MFC:
error = add_mfc(*m);
break;
case MRT_DEL_MFC:
error = del_mfc(*m);
break;
case MRT_ASSERT:
error = set_assert(*m);
break;
case MRT_API_CONFIG:
error = set_api_config(*m);
break;
case MRT_ADD_BW_UPCALL:
error = add_bw_upcall(*m);
break;
case MRT_DEL_BW_UPCALL:
error = del_bw_upcall(*m);
break;
default:
error = ENOPROTOOPT;
break;
}
if (*m)
m_free(*m);
return (error);
}
/*
* Handle MRT getsockopt commands
*/
int
ip_mrouter_get(struct socket *so, int optname, struct mbuf **m)
{
int error;
if (so != ip_mrouter)
error = ENOPROTOOPT;
else {
*m = m_get(M_WAIT, MT_SOOPTS);
MCLAIM(*m, so->so_mowner);
switch (optname) {
case MRT_VERSION:
error = get_version(*m);
break;
case MRT_ASSERT:
error = get_assert(*m);
break;
case MRT_API_SUPPORT:
error = get_api_support(*m);
break;
case MRT_API_CONFIG:
error = get_api_config(*m);
break;
default:
error = ENOPROTOOPT;
break;
}
if (error)
m_free(*m);
}
return (error);
}
/*
* Handle ioctl commands to obtain information from the cache
*/
int
mrt_ioctl(struct socket *so, u_long cmd, caddr_t data)
{
int error;
if (so != ip_mrouter)
error = EINVAL;
else
switch (cmd) {
case SIOCGETVIFCNT:
error = get_vif_cnt((struct sioc_vif_req *)data);
break;
case SIOCGETSGCNT:
error = get_sg_cnt((struct sioc_sg_req *)data);
break;
default:
error = EINVAL;
break;
}
return (error);
}
/*
* returns the packet, byte, rpf-failure count for the source group provided
*/
static int
get_sg_cnt(struct sioc_sg_req *req)
{
int s;
struct mfc *rt;
s = splsoftnet();
rt = mfc_find(&req->src, &req->grp);
if (rt == NULL) {
splx(s);
req->pktcnt = req->bytecnt = req->wrong_if = 0xffffffff;
return (EADDRNOTAVAIL);
}
req->pktcnt = rt->mfc_pkt_cnt;
req->bytecnt = rt->mfc_byte_cnt;
req->wrong_if = rt->mfc_wrong_if;
splx(s);
return (0);
}
/*
* returns the input and output packet and byte counts on the vif provided
*/
static int
get_vif_cnt(struct sioc_vif_req *req)
{
vifi_t vifi = req->vifi;
if (vifi >= numvifs)
return (EINVAL);
req->icount = viftable[vifi].v_pkt_in;
req->ocount = viftable[vifi].v_pkt_out;
req->ibytes = viftable[vifi].v_bytes_in;
req->obytes = viftable[vifi].v_bytes_out;
return (0);
}
/*
* Enable multicast routing
*/
static int
ip_mrouter_init(struct socket *so, struct mbuf *m)
{
int *v;
if (mrtdebug)
log(LOG_DEBUG,
"ip_mrouter_init: so_type = %d, pr_protocol = %d\n",
so->so_type, so->so_proto->pr_protocol);
if (so->so_type != SOCK_RAW ||
so->so_proto->pr_protocol != IPPROTO_IGMP)
return (EOPNOTSUPP);
if (m == NULL || m->m_len < sizeof(int))
return (EINVAL);
v = mtod(m, int *);
if (*v != 1)
return (EINVAL);
if (ip_mrouter != NULL)
return (EADDRINUSE);
ip_mrouter = so;
mfchashtbl =
hashinit(MFCTBLSIZ, HASH_LIST, M_MRTABLE, M_WAITOK, &mfchash);
bzero((caddr_t)nexpire, sizeof(nexpire));
pim_assert = 0;
callout_init(&expire_upcalls_ch);
callout_reset(&expire_upcalls_ch, EXPIRE_TIMEOUT,
expire_upcalls, NULL);
callout_init(&bw_upcalls_ch);
callout_reset(&bw_upcalls_ch, BW_UPCALLS_PERIOD,
expire_bw_upcalls_send, NULL);
callout_init(&bw_meter_ch);
callout_reset(&bw_meter_ch, BW_METER_PERIOD,
expire_bw_meter_process, NULL);
if (mrtdebug)
log(LOG_DEBUG, "ip_mrouter_init\n");
return (0);
}
/*
* Disable multicast routing
*/
int
ip_mrouter_done(void)
{
vifi_t vifi;
struct vif *vifp;
int i;
int s;
s = splsoftnet();
/* Clear out all the vifs currently in use. */
for (vifi = 0; vifi < numvifs; vifi++) {
vifp = &viftable[vifi];
if (!in_nullhost(vifp->v_lcl_addr))
reset_vif(vifp);
}
numvifs = 0;
pim_assert = 0;
mrt_api_config = 0;
callout_stop(&expire_upcalls_ch);
callout_stop(&bw_upcalls_ch);
callout_stop(&bw_meter_ch);
/*
* Free all multicast forwarding cache entries.
*/
for (i = 0; i < MFCTBLSIZ; i++) {
struct mfc *rt, *nrt;
for (rt = LIST_FIRST(&mfchashtbl[i]); rt; rt = nrt) {
nrt = LIST_NEXT(rt, mfc_hash);
expire_mfc(rt);
}
}
bzero((caddr_t)nexpire, sizeof(nexpire));
free(mfchashtbl, M_MRTABLE);
mfchashtbl = NULL;
bw_upcalls_n = 0;
bzero(bw_meter_timers, sizeof(bw_meter_timers));
/* Reset de-encapsulation cache. */
ip_mrouter = NULL;
splx(s);
if (mrtdebug)
log(LOG_DEBUG, "ip_mrouter_done\n");
return (0);
}
void
ip_mrouter_detach(struct ifnet *ifp)
{
int vifi, i;
struct vif *vifp;
struct mfc *rt;
struct rtdetq *rte;
/* XXX not sure about side effect to userland routing daemon */
for (vifi = 0; vifi < numvifs; vifi++) {
vifp = &viftable[vifi];
if (vifp->v_ifp == ifp)
reset_vif(vifp);
}
for (i = 0; i < MFCTBLSIZ; i++) {
if (nexpire[i] == 0)
continue;
LIST_FOREACH(rt, &mfchashtbl[i], mfc_hash) {
for (rte = rt->mfc_stall; rte; rte = rte->next) {
if (rte->ifp == ifp)
rte->ifp = NULL;
}
}
}
}
static int
get_version(struct mbuf *m)
{
int *v = mtod(m, int *);
*v = 0x0305; /* XXX !!!! */
m->m_len = sizeof(int);
return (0);
}
/*
* Set PIM assert processing global
*/
static int
set_assert(struct mbuf *m)
{
int *i;
if (m == NULL || m->m_len < sizeof(int))
return (EINVAL);
i = mtod(m, int *);
pim_assert = !!*i;
return (0);
}
/*
* Get PIM assert processing global
*/
static int
get_assert(struct mbuf *m)
{
int *i = mtod(m, int *);
*i = pim_assert;
m->m_len = sizeof(int);
return (0);
}
/*
* Configure API capabilities
*/
static int
set_api_config(struct mbuf *m)
{
int i;
u_int32_t *apival;
if (m == NULL || m->m_len < sizeof(u_int32_t))
return (EINVAL);
apival = mtod(m, u_int32_t *);
/*
* We can set the API capabilities only if it is the first operation
* after MRT_INIT. I.e.:
* - there are no vifs installed
* - pim_assert is not enabled
* - the MFC table is empty
*/
if (numvifs > 0) {
*apival = 0;
return (EPERM);
}
if (pim_assert) {
*apival = 0;
return (EPERM);
}
for (i = 0; i < MFCTBLSIZ; i++) {
if (LIST_FIRST(&mfchashtbl[i]) != NULL) {
*apival = 0;
return (EPERM);
}
}
mrt_api_config = *apival & mrt_api_support;
*apival = mrt_api_config;
return (0);
}
/*
* Get API capabilities
*/
static int
get_api_support(struct mbuf *m)
{
u_int32_t *apival;
if (m == NULL || m->m_len < sizeof(u_int32_t))
return (EINVAL);
apival = mtod(m, u_int32_t *);
*apival = mrt_api_support;
return (0);
}
/*
* Get API configured capabilities
*/
static int
get_api_config(struct mbuf *m)
{
u_int32_t *apival;
if (m == NULL || m->m_len < sizeof(u_int32_t))
return (EINVAL);
apival = mtod(m, u_int32_t *);
*apival = mrt_api_config;
return (0);
}
static struct sockaddr_in sin = {
.sin_len = sizeof(sin),
.sin_family = AF_INET
};
/*
* Add a vif to the vif table
*/
static int
add_vif(struct mbuf *m)
{
struct vifctl *vifcp;
struct vif *vifp;
struct ifaddr *ifa;
struct ifnet *ifp;
struct ifreq ifr;
int error, s;
if (m == NULL || m->m_len < sizeof(struct vifctl))
return (EINVAL);
vifcp = mtod(m, struct vifctl *);
if (vifcp->vifc_vifi >= MAXVIFS)
return (EINVAL);
if (in_nullhost(vifcp->vifc_lcl_addr))
return (EADDRNOTAVAIL);
vifp = &viftable[vifcp->vifc_vifi];
if (!in_nullhost(vifp->v_lcl_addr))
return (EADDRINUSE);
/* Find the interface with an address in AF_INET family. */
#ifdef PIM
if (vifcp->vifc_flags & VIFF_REGISTER) {
/*
* XXX: Because VIFF_REGISTER does not really need a valid
* local interface (e.g. it could be 127.0.0.2), we don't
* check its address.
*/
ifp = NULL;
} else
#endif
{
sin.sin_addr = vifcp->vifc_lcl_addr;
ifa = ifa_ifwithaddr(sintosa(&sin));
if (ifa == NULL)
return (EADDRNOTAVAIL);
ifp = ifa->ifa_ifp;
}
if (vifcp->vifc_flags & VIFF_TUNNEL) {
if (vifcp->vifc_flags & VIFF_SRCRT) {
log(LOG_ERR, "source routed tunnels not supported\n");
return (EOPNOTSUPP);
}
/* attach this vif to decapsulator dispatch table */
/*
* XXX Use addresses in registration so that matching
* can be done with radix tree in decapsulator. But,
* we need to check inner header for multicast, so
* this requires both radix tree lookup and then a
* function to check, and this is not supported yet.
*/
vifp->v_encap_cookie = encap_attach_func(AF_INET, IPPROTO_IPV4,
vif_encapcheck, &vif_protosw, vifp);
if (!vifp->v_encap_cookie)
return (EINVAL);
/* Create a fake encapsulation interface. */
ifp = (struct ifnet *)malloc(sizeof(*ifp), M_MRTABLE, M_WAITOK);
bzero(ifp, sizeof(*ifp));
snprintf(ifp->if_xname, sizeof(ifp->if_xname),
"mdecap%d", vifcp->vifc_vifi);
/* Prepare cached route entry. */
bzero(&vifp->v_route, sizeof(vifp->v_route));
#ifdef PIM
} else if (vifcp->vifc_flags & VIFF_REGISTER) {
ifp = &multicast_register_if;
if (mrtdebug)
log(LOG_DEBUG, "Adding a register vif, ifp: %p\n",
(void *)ifp);
if (reg_vif_num == VIFI_INVALID) {
bzero(ifp, sizeof(*ifp));
snprintf(ifp->if_xname, sizeof(ifp->if_xname),
"register_vif");
ifp->if_flags = IFF_LOOPBACK;
bzero(&vifp->v_route, sizeof(vifp->v_route));
reg_vif_num = vifcp->vifc_vifi;
}
#endif
} else {
/* Make sure the interface supports multicast. */
if ((ifp->if_flags & IFF_MULTICAST) == 0)
return (EOPNOTSUPP);
/* Enable promiscuous reception of all IP multicasts. */
satosin(&ifr.ifr_addr)->sin_len = sizeof(struct sockaddr_in);
satosin(&ifr.ifr_addr)->sin_family = AF_INET;
satosin(&ifr.ifr_addr)->sin_addr = zeroin_addr;
error = (*ifp->if_ioctl)(ifp, SIOCADDMULTI, (caddr_t)&ifr);
if (error)
return (error);
}
s = splsoftnet();
/* Define parameters for the tbf structure. */
vifp->tbf_q = NULL;
vifp->tbf_t = &vifp->tbf_q;
microtime(&vifp->tbf_last_pkt_t);
vifp->tbf_n_tok = 0;
vifp->tbf_q_len = 0;
vifp->tbf_max_q_len = MAXQSIZE;
vifp->v_flags = vifcp->vifc_flags;
vifp->v_threshold = vifcp->vifc_threshold;
/* scaling up here allows division by 1024 in critical code */
vifp->v_rate_limit = vifcp->vifc_rate_limit * 1024 / 1000;
vifp->v_lcl_addr = vifcp->vifc_lcl_addr;
vifp->v_rmt_addr = vifcp->vifc_rmt_addr;
vifp->v_ifp = ifp;
/* Initialize per vif pkt counters. */
vifp->v_pkt_in = 0;
vifp->v_pkt_out = 0;
vifp->v_bytes_in = 0;
vifp->v_bytes_out = 0;
callout_init(&vifp->v_repq_ch);
#ifdef RSVP_ISI
vifp->v_rsvp_on = 0;
vifp->v_rsvpd = NULL;
#endif /* RSVP_ISI */
splx(s);
/* Adjust numvifs up if the vifi is higher than numvifs. */
if (numvifs <= vifcp->vifc_vifi)
numvifs = vifcp->vifc_vifi + 1;
if (mrtdebug)
log(LOG_DEBUG, "add_vif #%d, lcladdr %x, %s %x, thresh %x, rate %d\n",
vifcp->vifc_vifi,
ntohl(vifcp->vifc_lcl_addr.s_addr),
(vifcp->vifc_flags & VIFF_TUNNEL) ? "rmtaddr" : "mask",
ntohl(vifcp->vifc_rmt_addr.s_addr),