forked from leostratus/netinet
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathigmp.c
More file actions
3649 lines (3231 loc) · 96.2 KB
/
igmp.c
File metadata and controls
3649 lines (3231 loc) · 96.2 KB
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
/*-
* Copyright (c) 2007-2009 Bruce Simpson.
* Copyright (c) 1988 Stephen Deering.
* 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.
* 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.
*
* @(#)igmp.c 8.1 (Berkeley) 7/19/93
*/
/*
* Internet Group Management Protocol (IGMP) routines.
* [RFC1112, RFC2236, RFC3376]
*
* Written by Steve Deering, Stanford, May 1988.
* Modified by Rosen Sharma, Stanford, Aug 1994.
* Modified by Bill Fenner, Xerox PARC, Feb 1995.
* Modified to fully comply to IGMPv2 by Bill Fenner, Oct 1995.
* Significantly rewritten for IGMPv3, VIMAGE, and SMP by Bruce Simpson.
*
* MULTICAST Revision: 3.5.1.4
*/
#include <sys/cdefs.h>
__FBSDID("$FreeBSD$");
#include <sys/param.h>
#include <sys/systm.h>
#include <sys/module.h>
#include <sys/malloc.h>
#include <sys/mbuf.h>
#include <sys/socket.h>
#include <sys/protosw.h>
#include <sys/kernel.h>
#include <sys/sysctl.h>
#include <sys/ktr.h>
#include <sys/condvar.h>
#include <net/if.h>
#include <net/netisr.h>
#include <net/vnet.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/ip_options.h>
#include <netinet/igmp.h>
#include <netinet/igmp_var.h>
#include <machine/in_cksum.h>
#include <security/mac/mac_framework.h>
#ifndef KTR_IGMPV3
#define KTR_IGMPV3 KTR_INET
#endif
static struct igmp_ifinfo *
igi_alloc_locked(struct ifnet *);
static void igi_delete_locked(const struct ifnet *);
static void igmp_dispatch_queue(struct ifqueue *, int, const int);
static void igmp_fasttimo_vnet(void);
static void igmp_final_leave(struct in_multi *, struct igmp_ifinfo *);
static int igmp_handle_state_change(struct in_multi *,
struct igmp_ifinfo *);
static int igmp_initial_join(struct in_multi *, struct igmp_ifinfo *);
static int igmp_input_v1_query(struct ifnet *, const struct ip *,
const struct igmp *);
static int igmp_input_v2_query(struct ifnet *, const struct ip *,
const struct igmp *);
static int igmp_input_v3_query(struct ifnet *, const struct ip *,
/*const*/ struct igmpv3 *);
static int igmp_input_v3_group_query(struct in_multi *,
struct igmp_ifinfo *, int, /*const*/ struct igmpv3 *);
static int igmp_input_v1_report(struct ifnet *, /*const*/ struct ip *,
/*const*/ struct igmp *);
static int igmp_input_v2_report(struct ifnet *, /*const*/ struct ip *,
/*const*/ struct igmp *);
static void igmp_intr(struct mbuf *);
static int igmp_isgroupreported(const struct in_addr);
static struct mbuf *
igmp_ra_alloc(void);
#ifdef KTR
static char * igmp_rec_type_to_str(const int);
#endif
static void igmp_set_version(struct igmp_ifinfo *, const int);
static void igmp_slowtimo_vnet(void);
static int igmp_v1v2_queue_report(struct in_multi *, const int);
static void igmp_v1v2_process_group_timer(struct in_multi *, const int);
static void igmp_v1v2_process_querier_timers(struct igmp_ifinfo *);
static void igmp_v2_update_group(struct in_multi *, const int);
static void igmp_v3_cancel_link_timers(struct igmp_ifinfo *);
static void igmp_v3_dispatch_general_query(struct igmp_ifinfo *);
static struct mbuf *
igmp_v3_encap_report(struct ifnet *, struct mbuf *);
static int igmp_v3_enqueue_group_record(struct ifqueue *,
struct in_multi *, const int, const int, const int);
static int igmp_v3_enqueue_filter_change(struct ifqueue *,
struct in_multi *);
static void igmp_v3_process_group_timers(struct igmp_ifinfo *,
struct ifqueue *, struct ifqueue *, struct in_multi *,
const int);
static int igmp_v3_merge_state_changes(struct in_multi *,
struct ifqueue *);
static void igmp_v3_suppress_group_record(struct in_multi *);
static int sysctl_igmp_default_version(SYSCTL_HANDLER_ARGS);
static int sysctl_igmp_gsr(SYSCTL_HANDLER_ARGS);
static int sysctl_igmp_ifinfo(SYSCTL_HANDLER_ARGS);
static const struct netisr_handler igmp_nh = {
.nh_name = "igmp",
.nh_handler = igmp_intr,
.nh_proto = NETISR_IGMP,
.nh_policy = NETISR_POLICY_SOURCE,
};
/*
* System-wide globals.
*
* Unlocked access to these is OK, except for the global IGMP output
* queue. The IGMP subsystem lock ends up being system-wide for the moment,
* because all VIMAGEs have to share a global output queue, as netisrs
* themselves are not virtualized.
*
* Locking:
* * The permitted lock order is: IN_MULTI_LOCK, IGMP_LOCK, IF_ADDR_LOCK.
* Any may be taken independently; if any are held at the same
* time, the above lock order must be followed.
* * All output is delegated to the netisr.
* Now that Giant has been eliminated, the netisr may be inlined.
* * IN_MULTI_LOCK covers in_multi.
* * IGMP_LOCK covers igmp_ifinfo and any global variables in this file,
* including the output queue.
* * IF_ADDR_LOCK covers if_multiaddrs, which is used for a variety of
* per-link state iterators.
* * igmp_ifinfo is valid as long as PF_INET is attached to the interface,
* therefore it is not refcounted.
* We allow unlocked reads of igmp_ifinfo when accessed via in_multi.
*
* Reference counting
* * IGMP acquires its own reference every time an in_multi is passed to
* it and the group is being joined for the first time.
* * IGMP releases its reference(s) on in_multi in a deferred way,
* because the operations which process the release run as part of
* a loop whose control variables are directly affected by the release
* (that, and not recursing on the IF_ADDR_LOCK).
*
* VIMAGE: Each in_multi corresponds to an ifp, and each ifp corresponds
* to a vnet in ifp->if_vnet.
*
* SMPng: XXX We may potentially race operations on ifma_protospec.
* The problem is that we currently lack a clean way of taking the
* IF_ADDR_LOCK() between the ifnet and in layers w/o recursing,
* as anything which modifies ifma needs to be covered by that lock.
* So check for ifma_protospec being NULL before proceeding.
*/
struct mtx igmp_mtx;
struct mbuf *m_raopt; /* Router Alert option */
static MALLOC_DEFINE(M_IGMP, "igmp", "igmp state");
/*
* VIMAGE-wide globals.
*
* The IGMPv3 timers themselves need to run per-image, however,
* protosw timers run globally (see tcp).
* An ifnet can only be in one vimage at a time, and the loopback
* ifnet, loif, is itself virtualized.
* It would otherwise be possible to seriously hose IGMP state,
* and create inconsistencies in upstream multicast routing, if you have
* multiple VIMAGEs running on the same link joining different multicast
* groups, UNLESS the "primary IP address" is different. This is because
* IGMP for IPv4 does not force link-local addresses to be used for each
* node, unlike MLD for IPv6.
* Obviously the IGMPv3 per-interface state has per-vimage granularity
* also as a result.
*
* FUTURE: Stop using IFP_TO_IA/INADDR_ANY, and use source address selection
* policy to control the address used by IGMP on the link.
*/
static VNET_DEFINE(int, interface_timers_running); /* IGMPv3 general
* query response */
static VNET_DEFINE(int, state_change_timers_running); /* IGMPv3 state-change
* retransmit */
static VNET_DEFINE(int, current_state_timers_running); /* IGMPv1/v2 host
* report; IGMPv3 g/sg
* query response */
#define V_interface_timers_running VNET(interface_timers_running)
#define V_state_change_timers_running VNET(state_change_timers_running)
#define V_current_state_timers_running VNET(current_state_timers_running)
static VNET_DEFINE(LIST_HEAD(, igmp_ifinfo), igi_head);
static VNET_DEFINE(struct igmpstat, igmpstat) = {
.igps_version = IGPS_VERSION_3,
.igps_len = sizeof(struct igmpstat),
};
static VNET_DEFINE(struct timeval, igmp_gsrdelay) = {10, 0};
#define V_igi_head VNET(igi_head)
#define V_igmpstat VNET(igmpstat)
#define V_igmp_gsrdelay VNET(igmp_gsrdelay)
static VNET_DEFINE(int, igmp_recvifkludge) = 1;
static VNET_DEFINE(int, igmp_sendra) = 1;
static VNET_DEFINE(int, igmp_sendlocal) = 1;
static VNET_DEFINE(int, igmp_v1enable) = 1;
static VNET_DEFINE(int, igmp_v2enable) = 1;
static VNET_DEFINE(int, igmp_legacysupp);
static VNET_DEFINE(int, igmp_default_version) = IGMP_VERSION_3;
#define V_igmp_recvifkludge VNET(igmp_recvifkludge)
#define V_igmp_sendra VNET(igmp_sendra)
#define V_igmp_sendlocal VNET(igmp_sendlocal)
#define V_igmp_v1enable VNET(igmp_v1enable)
#define V_igmp_v2enable VNET(igmp_v2enable)
#define V_igmp_legacysupp VNET(igmp_legacysupp)
#define V_igmp_default_version VNET(igmp_default_version)
/*
* Virtualized sysctls.
*/
SYSCTL_VNET_STRUCT(_net_inet_igmp, IGMPCTL_STATS, stats, CTLFLAG_RW,
&VNET_NAME(igmpstat), igmpstat, "");
SYSCTL_VNET_INT(_net_inet_igmp, OID_AUTO, recvifkludge, CTLFLAG_RW,
&VNET_NAME(igmp_recvifkludge), 0,
"Rewrite IGMPv1/v2 reports from 0.0.0.0 to contain subnet address");
SYSCTL_VNET_INT(_net_inet_igmp, OID_AUTO, sendra, CTLFLAG_RW,
&VNET_NAME(igmp_sendra), 0,
"Send IP Router Alert option in IGMPv2/v3 messages");
SYSCTL_VNET_INT(_net_inet_igmp, OID_AUTO, sendlocal, CTLFLAG_RW,
&VNET_NAME(igmp_sendlocal), 0,
"Send IGMP membership reports for 224.0.0.0/24 groups");
SYSCTL_VNET_INT(_net_inet_igmp, OID_AUTO, v1enable, CTLFLAG_RW,
&VNET_NAME(igmp_v1enable), 0,
"Enable backwards compatibility with IGMPv1");
SYSCTL_VNET_INT(_net_inet_igmp, OID_AUTO, v2enable, CTLFLAG_RW,
&VNET_NAME(igmp_v2enable), 0,
"Enable backwards compatibility with IGMPv2");
SYSCTL_VNET_INT(_net_inet_igmp, OID_AUTO, legacysupp, CTLFLAG_RW,
&VNET_NAME(igmp_legacysupp), 0,
"Allow v1/v2 reports to suppress v3 group responses");
SYSCTL_VNET_PROC(_net_inet_igmp, OID_AUTO, default_version,
CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE,
&VNET_NAME(igmp_default_version), 0, sysctl_igmp_default_version, "I",
"Default version of IGMP to run on each interface");
SYSCTL_VNET_PROC(_net_inet_igmp, OID_AUTO, gsrdelay,
CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE,
&VNET_NAME(igmp_gsrdelay.tv_sec), 0, sysctl_igmp_gsr, "I",
"Rate limit for IGMPv3 Group-and-Source queries in seconds");
/*
* Non-virtualized sysctls.
*/
static SYSCTL_NODE(_net_inet_igmp, OID_AUTO, ifinfo,
CTLFLAG_RD | CTLFLAG_MPSAFE, sysctl_igmp_ifinfo,
"Per-interface IGMPv3 state");
static __inline void
igmp_save_context(struct mbuf *m, struct ifnet *ifp)
{
#ifdef VIMAGE
m->m_pkthdr.header = ifp->if_vnet;
#endif /* VIMAGE */
m->m_pkthdr.flowid = ifp->if_index;
}
static __inline void
igmp_scrub_context(struct mbuf *m)
{
m->m_pkthdr.header = NULL;
m->m_pkthdr.flowid = 0;
}
#ifdef KTR
static __inline char *
inet_ntoa_haddr(in_addr_t haddr)
{
struct in_addr ia;
ia.s_addr = htonl(haddr);
return (inet_ntoa(ia));
}
#endif
/*
* Restore context from a queued IGMP output chain.
* Return saved ifindex.
*
* VIMAGE: The assertion is there to make sure that we
* actually called CURVNET_SET() with what's in the mbuf chain.
*/
static __inline uint32_t
igmp_restore_context(struct mbuf *m)
{
#ifdef notyet
#if defined(VIMAGE) && defined(INVARIANTS)
KASSERT(curvnet == (m->m_pkthdr.header),
("%s: called when curvnet was not restored", __func__));
#endif
#endif
return (m->m_pkthdr.flowid);
}
/*
* Retrieve or set default IGMP version.
*
* VIMAGE: Assume curvnet set by caller.
* SMPng: NOTE: Serialized by IGMP lock.
*/
static int
sysctl_igmp_default_version(SYSCTL_HANDLER_ARGS)
{
int error;
int new;
error = sysctl_wire_old_buffer(req, sizeof(int));
if (error)
return (error);
IGMP_LOCK();
new = V_igmp_default_version;
error = sysctl_handle_int(oidp, &new, 0, req);
if (error || !req->newptr)
goto out_locked;
if (new < IGMP_VERSION_1 || new > IGMP_VERSION_3) {
error = EINVAL;
goto out_locked;
}
CTR2(KTR_IGMPV3, "change igmp_default_version from %d to %d",
V_igmp_default_version, new);
V_igmp_default_version = new;
out_locked:
IGMP_UNLOCK();
return (error);
}
/*
* Retrieve or set threshold between group-source queries in seconds.
*
* VIMAGE: Assume curvnet set by caller.
* SMPng: NOTE: Serialized by IGMP lock.
*/
static int
sysctl_igmp_gsr(SYSCTL_HANDLER_ARGS)
{
int error;
int i;
error = sysctl_wire_old_buffer(req, sizeof(int));
if (error)
return (error);
IGMP_LOCK();
i = V_igmp_gsrdelay.tv_sec;
error = sysctl_handle_int(oidp, &i, 0, req);
if (error || !req->newptr)
goto out_locked;
if (i < -1 || i >= 60) {
error = EINVAL;
goto out_locked;
}
CTR2(KTR_IGMPV3, "change igmp_gsrdelay from %d to %d",
V_igmp_gsrdelay.tv_sec, i);
V_igmp_gsrdelay.tv_sec = i;
out_locked:
IGMP_UNLOCK();
return (error);
}
/*
* Expose struct igmp_ifinfo to userland, keyed by ifindex.
* For use by ifmcstat(8).
*
* SMPng: NOTE: Does an unlocked ifindex space read.
* VIMAGE: Assume curvnet set by caller. The node handler itself
* is not directly virtualized.
*/
static int
sysctl_igmp_ifinfo(SYSCTL_HANDLER_ARGS)
{
int *name;
int error;
u_int namelen;
struct ifnet *ifp;
struct igmp_ifinfo *igi;
name = (int *)arg1;
namelen = arg2;
if (req->newptr != NULL)
return (EPERM);
if (namelen != 1)
return (EINVAL);
error = sysctl_wire_old_buffer(req, sizeof(struct igmp_ifinfo));
if (error)
return (error);
IN_MULTI_LOCK();
IGMP_LOCK();
if (name[0] <= 0 || name[0] > V_if_index) {
error = ENOENT;
goto out_locked;
}
error = ENOENT;
ifp = ifnet_byindex(name[0]);
if (ifp == NULL)
goto out_locked;
LIST_FOREACH(igi, &V_igi_head, igi_link) {
if (ifp == igi->igi_ifp) {
error = SYSCTL_OUT(req, igi,
sizeof(struct igmp_ifinfo));
break;
}
}
out_locked:
IGMP_UNLOCK();
IN_MULTI_UNLOCK();
return (error);
}
/*
* Dispatch an entire queue of pending packet chains
* using the netisr.
* VIMAGE: Assumes the vnet pointer has been set.
*/
static void
igmp_dispatch_queue(struct ifqueue *ifq, int limit, const int loop)
{
struct mbuf *m;
for (;;) {
_IF_DEQUEUE(ifq, m);
if (m == NULL)
break;
CTR3(KTR_IGMPV3, "%s: dispatch %p from %p", __func__, ifq, m);
if (loop)
m->m_flags |= M_IGMP_LOOP;
netisr_dispatch(NETISR_IGMP, m);
if (--limit == 0)
break;
}
}
/*
* Filter outgoing IGMP report state by group.
*
* Reports are ALWAYS suppressed for ALL-HOSTS (224.0.0.1).
* If the net.inet.igmp.sendlocal sysctl is 0, then IGMP reports are
* disabled for all groups in the 224.0.0.0/24 link-local scope. However,
* this may break certain IGMP snooping switches which rely on the old
* report behaviour.
*
* Return zero if the given group is one for which IGMP reports
* should be suppressed, or non-zero if reports should be issued.
*/
static __inline int
igmp_isgroupreported(const struct in_addr addr)
{
if (in_allhosts(addr) ||
((!V_igmp_sendlocal && IN_LOCAL_GROUP(ntohl(addr.s_addr)))))
return (0);
return (1);
}
/*
* Construct a Router Alert option to use in outgoing packets.
*/
static struct mbuf *
igmp_ra_alloc(void)
{
struct mbuf *m;
struct ipoption *p;
MGET(m, M_NOWAIT, MT_DATA);
p = mtod(m, struct ipoption *);
p->ipopt_dst.s_addr = INADDR_ANY;
p->ipopt_list[0] = IPOPT_RA; /* Router Alert Option */
p->ipopt_list[1] = 0x04; /* 4 bytes long */
p->ipopt_list[2] = IPOPT_EOL; /* End of IP option list */
p->ipopt_list[3] = 0x00; /* pad byte */
m->m_len = sizeof(p->ipopt_dst) + p->ipopt_list[1];
return (m);
}
/*
* Attach IGMP when PF_INET is attached to an interface.
*/
struct igmp_ifinfo *
igmp_domifattach(struct ifnet *ifp)
{
struct igmp_ifinfo *igi;
CTR3(KTR_IGMPV3, "%s: called for ifp %p(%s)",
__func__, ifp, ifp->if_xname);
IGMP_LOCK();
igi = igi_alloc_locked(ifp);
if (!(ifp->if_flags & IFF_MULTICAST))
igi->igi_flags |= IGIF_SILENT;
IGMP_UNLOCK();
return (igi);
}
/*
* VIMAGE: assume curvnet set by caller.
*/
static struct igmp_ifinfo *
igi_alloc_locked(/*const*/ struct ifnet *ifp)
{
struct igmp_ifinfo *igi;
IGMP_LOCK_ASSERT();
igi = malloc(sizeof(struct igmp_ifinfo), M_IGMP, M_NOWAIT|M_ZERO);
if (igi == NULL)
goto out;
igi->igi_ifp = ifp;
igi->igi_version = V_igmp_default_version;
igi->igi_flags = 0;
igi->igi_rv = IGMP_RV_INIT;
igi->igi_qi = IGMP_QI_INIT;
igi->igi_qri = IGMP_QRI_INIT;
igi->igi_uri = IGMP_URI_INIT;
SLIST_INIT(&igi->igi_relinmhead);
/*
* Responses to general queries are subject to bounds.
*/
IFQ_SET_MAXLEN(&igi->igi_gq, IGMP_MAX_RESPONSE_PACKETS);
LIST_INSERT_HEAD(&V_igi_head, igi, igi_link);
CTR2(KTR_IGMPV3, "allocate igmp_ifinfo for ifp %p(%s)",
ifp, ifp->if_xname);
out:
return (igi);
}
/*
* Hook for ifdetach.
*
* NOTE: Some finalization tasks need to run before the protocol domain
* is detached, but also before the link layer does its cleanup.
*
* SMPNG: igmp_ifdetach() needs to take IF_ADDR_LOCK().
* XXX This is also bitten by unlocked ifma_protospec access.
*/
void
igmp_ifdetach(struct ifnet *ifp)
{
struct igmp_ifinfo *igi;
struct ifmultiaddr *ifma;
struct in_multi *inm, *tinm;
CTR3(KTR_IGMPV3, "%s: called for ifp %p(%s)", __func__, ifp,
ifp->if_xname);
IGMP_LOCK();
igi = ((struct in_ifinfo *)ifp->if_afdata[AF_INET])->ii_igmp;
if (igi->igi_version == IGMP_VERSION_3) {
IF_ADDR_RLOCK(ifp);
TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) {
if (ifma->ifma_addr->sa_family != AF_INET ||
ifma->ifma_protospec == NULL)
continue;
#if 0
KASSERT(ifma->ifma_protospec != NULL,
("%s: ifma_protospec is NULL", __func__));
#endif
inm = (struct in_multi *)ifma->ifma_protospec;
if (inm->inm_state == IGMP_LEAVING_MEMBER) {
SLIST_INSERT_HEAD(&igi->igi_relinmhead,
inm, inm_nrele);
}
inm_clear_recorded(inm);
}
IF_ADDR_RUNLOCK(ifp);
/*
* Free the in_multi reference(s) for this IGMP lifecycle.
*/
SLIST_FOREACH_SAFE(inm, &igi->igi_relinmhead, inm_nrele,
tinm) {
SLIST_REMOVE_HEAD(&igi->igi_relinmhead, inm_nrele);
inm_release_locked(inm);
}
}
IGMP_UNLOCK();
}
/*
* Hook for domifdetach.
*/
void
igmp_domifdetach(struct ifnet *ifp)
{
struct igmp_ifinfo *igi;
CTR3(KTR_IGMPV3, "%s: called for ifp %p(%s)",
__func__, ifp, ifp->if_xname);
IGMP_LOCK();
igi = ((struct in_ifinfo *)ifp->if_afdata[AF_INET])->ii_igmp;
igi_delete_locked(ifp);
IGMP_UNLOCK();
}
static void
igi_delete_locked(const struct ifnet *ifp)
{
struct igmp_ifinfo *igi, *tigi;
CTR3(KTR_IGMPV3, "%s: freeing igmp_ifinfo for ifp %p(%s)",
__func__, ifp, ifp->if_xname);
IGMP_LOCK_ASSERT();
LIST_FOREACH_SAFE(igi, &V_igi_head, igi_link, tigi) {
if (igi->igi_ifp == ifp) {
/*
* Free deferred General Query responses.
*/
_IF_DRAIN(&igi->igi_gq);
LIST_REMOVE(igi, igi_link);
KASSERT(SLIST_EMPTY(&igi->igi_relinmhead),
("%s: there are dangling in_multi references",
__func__));
free(igi, M_IGMP);
return;
}
}
#ifdef INVARIANTS
panic("%s: igmp_ifinfo not found for ifp %p\n", __func__, ifp);
#endif
}
/*
* Process a received IGMPv1 query.
* Return non-zero if the message should be dropped.
*
* VIMAGE: The curvnet pointer is derived from the input ifp.
*/
static int
igmp_input_v1_query(struct ifnet *ifp, const struct ip *ip,
const struct igmp *igmp)
{
struct ifmultiaddr *ifma;
struct igmp_ifinfo *igi;
struct in_multi *inm;
/*
* IGMPv1 Host Mmembership Queries SHOULD always be addressed to
* 224.0.0.1. They are always treated as General Queries.
* igmp_group is always ignored. Do not drop it as a userland
* daemon may wish to see it.
* XXX SMPng: unlocked increments in igmpstat assumed atomic.
*/
if (!in_allhosts(ip->ip_dst) || !in_nullhost(igmp->igmp_group)) {
IGMPSTAT_INC(igps_rcv_badqueries);
return (0);
}
IGMPSTAT_INC(igps_rcv_gen_queries);
IN_MULTI_LOCK();
IGMP_LOCK();
igi = ((struct in_ifinfo *)ifp->if_afdata[AF_INET])->ii_igmp;
KASSERT(igi != NULL, ("%s: no igmp_ifinfo for ifp %p", __func__, ifp));
if (igi->igi_flags & IGIF_LOOPBACK) {
CTR2(KTR_IGMPV3, "ignore v1 query on IGIF_LOOPBACK ifp %p(%s)",
ifp, ifp->if_xname);
goto out_locked;
}
/*
* Switch to IGMPv1 host compatibility mode.
*/
igmp_set_version(igi, IGMP_VERSION_1);
CTR2(KTR_IGMPV3, "process v1 query on ifp %p(%s)", ifp, ifp->if_xname);
/*
* Start the timers in all of our group records
* for the interface on which the query arrived,
* except those which are already running.
*/
IF_ADDR_RLOCK(ifp);
TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) {
if (ifma->ifma_addr->sa_family != AF_INET ||
ifma->ifma_protospec == NULL)
continue;
inm = (struct in_multi *)ifma->ifma_protospec;
if (inm->inm_timer != 0)
continue;
switch (inm->inm_state) {
case IGMP_NOT_MEMBER:
case IGMP_SILENT_MEMBER:
break;
case IGMP_G_QUERY_PENDING_MEMBER:
case IGMP_SG_QUERY_PENDING_MEMBER:
case IGMP_REPORTING_MEMBER:
case IGMP_IDLE_MEMBER:
case IGMP_LAZY_MEMBER:
case IGMP_SLEEPING_MEMBER:
case IGMP_AWAKENING_MEMBER:
inm->inm_state = IGMP_REPORTING_MEMBER;
inm->inm_timer = IGMP_RANDOM_DELAY(
IGMP_V1V2_MAX_RI * PR_FASTHZ);
V_current_state_timers_running = 1;
break;
case IGMP_LEAVING_MEMBER:
break;
}
}
IF_ADDR_RUNLOCK(ifp);
out_locked:
IGMP_UNLOCK();
IN_MULTI_UNLOCK();
return (0);
}
/*
* Process a received IGMPv2 general or group-specific query.
*/
static int
igmp_input_v2_query(struct ifnet *ifp, const struct ip *ip,
const struct igmp *igmp)
{
struct ifmultiaddr *ifma;
struct igmp_ifinfo *igi;
struct in_multi *inm;
int is_general_query;
uint16_t timer;
is_general_query = 0;
/*
* Validate address fields upfront.
* XXX SMPng: unlocked increments in igmpstat assumed atomic.
*/
if (in_nullhost(igmp->igmp_group)) {
/*
* IGMPv2 General Query.
* If this was not sent to the all-hosts group, ignore it.
*/
if (!in_allhosts(ip->ip_dst))
return (0);
IGMPSTAT_INC(igps_rcv_gen_queries);
is_general_query = 1;
} else {
/* IGMPv2 Group-Specific Query. */
IGMPSTAT_INC(igps_rcv_group_queries);
}
IN_MULTI_LOCK();
IGMP_LOCK();
igi = ((struct in_ifinfo *)ifp->if_afdata[AF_INET])->ii_igmp;
KASSERT(igi != NULL, ("%s: no igmp_ifinfo for ifp %p", __func__, ifp));
if (igi->igi_flags & IGIF_LOOPBACK) {
CTR2(KTR_IGMPV3, "ignore v2 query on IGIF_LOOPBACK ifp %p(%s)",
ifp, ifp->if_xname);
goto out_locked;
}
/*
* Ignore v2 query if in v1 Compatibility Mode.
*/
if (igi->igi_version == IGMP_VERSION_1)
goto out_locked;
igmp_set_version(igi, IGMP_VERSION_2);
timer = igmp->igmp_code * PR_FASTHZ / IGMP_TIMER_SCALE;
if (timer == 0)
timer = 1;
if (is_general_query) {
/*
* For each reporting group joined on this
* interface, kick the report timer.
*/
CTR2(KTR_IGMPV3, "process v2 general query on ifp %p(%s)",
ifp, ifp->if_xname);
IF_ADDR_RLOCK(ifp);
TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) {
if (ifma->ifma_addr->sa_family != AF_INET ||
ifma->ifma_protospec == NULL)
continue;
inm = (struct in_multi *)ifma->ifma_protospec;
igmp_v2_update_group(inm, timer);
}
IF_ADDR_RUNLOCK(ifp);
} else {
/*
* Group-specific IGMPv2 query, we need only
* look up the single group to process it.
*/
inm = inm_lookup(ifp, igmp->igmp_group);
if (inm != NULL) {
CTR3(KTR_IGMPV3, "process v2 query %s on ifp %p(%s)",
inet_ntoa(igmp->igmp_group), ifp, ifp->if_xname);
igmp_v2_update_group(inm, timer);
}
}
out_locked:
IGMP_UNLOCK();
IN_MULTI_UNLOCK();
return (0);
}
/*
* Update the report timer on a group in response to an IGMPv2 query.
*
* If we are becoming the reporting member for this group, start the timer.
* If we already are the reporting member for this group, and timer is
* below the threshold, reset it.
*
* We may be updating the group for the first time since we switched
* to IGMPv3. If we are, then we must clear any recorded source lists,
* and transition to REPORTING state; the group timer is overloaded
* for group and group-source query responses.
*
* Unlike IGMPv3, the delay per group should be jittered
* to avoid bursts of IGMPv2 reports.
*/
static void
igmp_v2_update_group(struct in_multi *inm, const int timer)
{
CTR4(KTR_IGMPV3, "%s: %s/%s timer=%d", __func__,
inet_ntoa(inm->inm_addr), inm->inm_ifp->if_xname, timer);
IN_MULTI_LOCK_ASSERT();
switch (inm->inm_state) {
case IGMP_NOT_MEMBER:
case IGMP_SILENT_MEMBER:
break;
case IGMP_REPORTING_MEMBER:
if (inm->inm_timer != 0 &&
inm->inm_timer <= timer) {
CTR1(KTR_IGMPV3, "%s: REPORTING and timer running, "
"skipping.", __func__);
break;
}
/* FALLTHROUGH */
case IGMP_SG_QUERY_PENDING_MEMBER:
case IGMP_G_QUERY_PENDING_MEMBER:
case IGMP_IDLE_MEMBER:
case IGMP_LAZY_MEMBER:
case IGMP_AWAKENING_MEMBER:
CTR1(KTR_IGMPV3, "%s: ->REPORTING", __func__);
inm->inm_state = IGMP_REPORTING_MEMBER;
inm->inm_timer = IGMP_RANDOM_DELAY(timer);
V_current_state_timers_running = 1;
break;
case IGMP_SLEEPING_MEMBER:
CTR1(KTR_IGMPV3, "%s: ->AWAKENING", __func__);
inm->inm_state = IGMP_AWAKENING_MEMBER;
break;
case IGMP_LEAVING_MEMBER:
break;
}
}
/*
* Process a received IGMPv3 general, group-specific or
* group-and-source-specific query.
* Assumes m has already been pulled up to the full IGMP message length.
* Return 0 if successful, otherwise an appropriate error code is returned.
*/
static int
igmp_input_v3_query(struct ifnet *ifp, const struct ip *ip,
/*const*/ struct igmpv3 *igmpv3)
{
struct igmp_ifinfo *igi;
struct in_multi *inm;
int is_general_query;
uint32_t maxresp, nsrc, qqi;
uint16_t timer;
uint8_t qrv;
is_general_query = 0;
CTR2(KTR_IGMPV3, "process v3 query on ifp %p(%s)", ifp, ifp->if_xname);
maxresp = igmpv3->igmp_code; /* in 1/10ths of a second */
if (maxresp >= 128) {
maxresp = IGMP_MANT(igmpv3->igmp_code) <<
(IGMP_EXP(igmpv3->igmp_code) + 3);
}
/*
* Robustness must never be less than 2 for on-wire IGMPv3.
* FUTURE: Check if ifp has IGIF_LOOPBACK set, as we will make
* an exception for interfaces whose IGMPv3 state changes
* are redirected to loopback (e.g. MANET).
*/
qrv = IGMP_QRV(igmpv3->igmp_misc);
if (qrv < 2) {
CTR3(KTR_IGMPV3, "%s: clamping qrv %d to %d", __func__,
qrv, IGMP_RV_INIT);
qrv = IGMP_RV_INIT;
}
qqi = igmpv3->igmp_qqi;
if (qqi >= 128) {
qqi = IGMP_MANT(igmpv3->igmp_qqi) <<
(IGMP_EXP(igmpv3->igmp_qqi) + 3);
}
timer = maxresp * PR_FASTHZ / IGMP_TIMER_SCALE;
if (timer == 0)
timer = 1;
nsrc = ntohs(igmpv3->igmp_numsrc);
/*
* Validate address fields and versions upfront before
* accepting v3 query.
* XXX SMPng: Unlocked access to igmpstat counters here.
*/
if (in_nullhost(igmpv3->igmp_group)) {
/*
* IGMPv3 General Query.
*
* General Queries SHOULD be directed to 224.0.0.1.