/
mldv2.c
3595 lines (3302 loc) · 99.7 KB
/
mldv2.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
/* $KAME: mldv2.c,v 1.6 2004/03/10 09:13:26 suz Exp $ */
/*
* Copyright (c) 2002 INRIA. All rights reserved.
*
* Implementation of Multicast Listener Discovery, Version 2.
* Developed by Hitoshi Asaeda, INRIA, August 2002.
*
* 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 INRIA 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 INSTITUTE 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 INSTITUTE 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.
*/
/*
* Copyright (C) 1998 WIDE Project.
* All rights reserved.
*
* 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 project 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 PROJECT 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 PROJECT 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.
*/
/*
* 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.
* 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.
*
* @(#)igmp.c 8.1 (Berkeley) 7/19/93
*/
#if defined(__FreeBSD__) && __FreeBSD__ >= 3
#include "opt_inet.h"
#include "opt_inet6.h"
#endif
#ifdef __NetBSD__
#include "opt_inet.h"
#endif
#ifndef MLDV2
/*
* this file is not used at all if MLDv2 is disabled.
* separated from mld6.c (KAME Rev 1.92).
* includes part of in6.c (KAME Rev 1.360).
*/
#else
#include <sys/param.h>
#include <sys/systm.h>
#include <sys/mbuf.h>
#include <sys/socket.h>
#include <sys/sockio.h>
#include <sys/protosw.h>
#ifdef __bsdi__
#include <vm/vm.h>
#include <sys/proc.h>
#endif
#include <sys/syslog.h>
#include <sys/sysctl.h>
#if defined(__FreeBSD__) && __FreeBSD__ >= 3
#include <sys/kernel.h>
#include <sys/malloc.h>
#endif
#ifdef __OpenBSD__
#include <dev/rndvar.h>
#endif
#include <net/if.h>
#include <net/route.h>
#include <netinet/in.h>
#include <netinet/in_var.h>
#include <netinet/ip6.h>
#include <netinet6/ip6_var.h>
#include <netinet/icmp6.h>
#include <netinet6/mld6_var.h>
#include <netinet6/in6_msf.h>
#ifdef __FreeBSD__
#include <net/ethernet.h>
#endif
#include <net/if_arp.h>
#ifdef __NetBSD__
#include <net/if_ether.h>
#else
#ifdef __OpenBSD__
#include <netinet/if_ether.h>
#endif
#endif
#include <net/if_types.h>
#include <net/net_osdep.h>
#if defined(__FreeBSD__) && __FreeBSD__ >= 3
static MALLOC_DEFINE(M_MRTABLE, "mrt", "multicast routing table");
#endif
#if defined(__FreeBSD__) && __FreeBSD__ >= 3
struct in6_multihead in6_multihead; /* XXX BSS initialization */
#else
/*
* This structure is used to keep track of in6_multi chains which belong to
* deleted interface addresses.
*/
static LIST_HEAD(, multi6_kludge) in6_mk; /* XXX BSS initialization */
struct multi6_kludge {
LIST_ENTRY(multi6_kludge) mk_entry;
struct ifnet *mk_ifp;
struct in6_multihead mk_head;
};
#endif
/*
* Protocol constants
*/
/*
* time between repetitions of a node's initial report of interest in a
* multicast address(in seconds)
*/
#define MLD_UNSOLICITED_REPORT_INTERVAL 10
int mldmaxsrcfilter = IP_MAX_SOURCE_FILTER;
int mldsomaxsrc = SO_MAX_SOURCE_FILTER;
int mldalways_v2 = 0;
#ifdef MLDV2_DEBUG
int mld_debug = 1;
#else
int mld_debug = 0;
#endif
struct router6_info *Head6;
static struct ip6_pktopts ip6_opts;
static int mld_group_timers_are_running;
static int mld_interface_timers_are_running;
static int mld_state_change_timers_are_running;
static const struct sockaddr_in6 *all_nodes_linklocal;
static const struct sockaddr_in6 *all_routers_linklocal;
static const struct sockaddr_in6 *all_v2routers_linklocal;
static const int ignflags = (IN6_IFF_NOTREADY|IN6_IFF_ANYCAST) &
~IN6_IFF_TENTATIVE;
static const int qhdrlen = MLD_V2_QUERY_MINLEN; /* mldv2 query header */
static const int rhdrlen = 8; /* mldv2 report header */
static const int ghdrlen = 20; /* mld group report header */
static const int addrlen = sizeof(struct in6_addr);
#ifdef __FreeBSD__
#ifdef SYSCTL_DECL
SYSCTL_DECL(_net_inet6_icmp6);
#endif
SYSCTL_INT(_net_inet6_icmp6, ICMPV6CTL_MLD_MAXSRCFILTER, mld_maxsrcfilter, CTLFLAG_RW,
&mldmaxsrcfilter, IP_MAX_SOURCE_FILTER, "");
SYSCTL_INT(_net_inet6_icmp6, ICMPV6CTL_MLD_SOMAXSRC, mld_somaxsrc, CTLFLAG_RW,
&mldsomaxsrc, SO_MAX_SOURCE_FILTER, "");
SYSCTL_INT(_net_inet6_icmp6, ICMPV6CTL_MLD_ALWAYSV2, mld_alwaysv2, CTLFLAG_RW,
&mldalways_v2, 0, "");
#endif
#define SOURCE_RECORD_LEN(numsrc) (numsrc * addrlen)
#define GET_REPORT_SOURCE_HEAD(in6m, type, iasl) { \
if ((type) == ALLOW_NEW_SOURCES) \
(iasl) = (in6m)->in6m_source->i6ms_alw; \
else if ((type) == BLOCK_OLD_SOURCES) \
(iasl) = (in6m)->in6m_source->i6ms_blk; \
else if ((type) == CHANGE_TO_INCLUDE_MODE) \
(iasl) = (in6m)->in6m_source->i6ms_toin; \
else if ((type) == CHANGE_TO_EXCLUDE_MODE) \
(iasl) = (in6m)->in6m_source->i6ms_toex; \
else { \
if ((in6m)->in6m_state == MLD_SG_QUERY_PENDING_MEMBER) \
(iasl) = (in6m)->in6m_source->i6ms_rec; \
else \
(iasl) = (in6m)->in6m_source->i6ms_cur; \
} \
}
static void mld_sendbuf(struct mbuf *, struct ifnet *);
int mld_set_timer(struct ifnet *, struct router6_info *, struct mld_hdr *,
u_int16_t, u_int8_t);
void mld_set_hostcompat(struct ifnet *, struct router6_info *, int);
int mld_record_queried_source(struct in6_multi *, struct mld_hdr *, u_int16_t);
void mld_send_all_current_state_report(struct ifnet *);
int mld_send_current_state_report(struct mbuf **, int *, struct in6_multi *);
static void mld_sendpkt(struct in6_multi *, int, const struct in6_addr *);
static struct mld_hdr * mld_allocbuf(struct mbuf **, int, struct in6_multi *,
int);
static struct router6_info *find_rt6i(struct ifnet *);
static int mld_create_group_record(struct mbuf *, int *, struct in6_multi *,
u_int16_t, u_int16_t *, u_int8_t);
static void mld_cancel_pending_response(struct ifnet *, struct router6_info *);
void
mld_init()
{
static u_int8_t hbh_buf[8];
struct ip6_hbh *hbh = (struct ip6_hbh *)hbh_buf;
u_int16_t rtalert_code = htons((u_int16_t)IP6OPT_RTALERT_MLD);
static struct sockaddr_in6 all_nodes_linklocal0;
static struct sockaddr_in6 all_routers_linklocal0;
static struct sockaddr_in6 all_v2routers_linklocal0;
mld_group_timers_are_running = 0;
mld_interface_timers_are_running = 0;
mld_state_change_timers_are_running = 0;
/* ip6h_nxt will be fill in later */
hbh->ip6h_len = 0; /* (8 >> 3) - 1 */
/* XXX: grotty hard coding... */
hbh_buf[2] = IP6OPT_PADN; /* 2 byte padding */
hbh_buf[3] = 0;
hbh_buf[4] = IP6OPT_ROUTER_ALERT;
hbh_buf[5] = IP6OPT_RTALERT_LEN - 2;
bcopy((caddr_t)&rtalert_code, &hbh_buf[6], sizeof(u_int16_t));
all_nodes_linklocal0.sin6_family = AF_INET6;
all_nodes_linklocal0.sin6_len = sizeof(struct sockaddr_in6);
all_nodes_linklocal0.sin6_addr = in6addr_linklocal_allnodes;
all_nodes_linklocal = &all_nodes_linklocal0;
all_routers_linklocal0.sin6_family = AF_INET6;
all_routers_linklocal0.sin6_len = sizeof(struct sockaddr_in6);
all_routers_linklocal0.sin6_addr = in6addr_linklocal_allrouters;
all_routers_linklocal = &all_routers_linklocal0;
all_v2routers_linklocal0.sin6_family = AF_INET6;
all_v2routers_linklocal0.sin6_len = sizeof(struct sockaddr_in6);
all_v2routers_linklocal0.sin6_addr = in6addr_linklocal_allv2routers;
all_v2routers_linklocal = &all_v2routers_linklocal0;
ip6_initpktopts(&ip6_opts);
ip6_opts.ip6po_hbh = hbh;
Head6 = NULL;
}
struct router6_info *
rt6i_init(ifp)
struct ifnet *ifp;
{
struct router6_info *rti = NULL;
MALLOC(rti, struct router6_info *, sizeof *rti, M_MRTABLE, M_NOWAIT);
if (rti == NULL)
return NULL;
rti->rt6i_ifp = ifp;
rti->rt6i_timer1 = 0;
rti->rt6i_timer2 = 0;
rti->rt6i_qrv = MLD_DEF_RV;
rti->rt6i_qqi = MLD_DEF_QI;
rti->rt6i_qri = MLD_DEF_QRI / MLD_TIMER_SCALE;
rti->rt6i_type = MLD_V2_ROUTER;
rti->rt6i_next = Head6;
Head6 = rti;
return (rti);
}
static struct router6_info *
find_rt6i(ifp)
struct ifnet *ifp;
{
register struct router6_info *rti = Head6;
while (rti) {
if (rti->rt6i_ifp == ifp) {
return rti;
}
rti = rti->rt6i_next;
}
if ((rti = rt6i_init(ifp)) == NULL)
return NULL;
return rti;
}
void
mld_start_listening(in6m, type)
struct in6_multi *in6m;
u_int8_t type; /* State-Change report type */
{
struct mbuf *m = NULL;
int buflen = 0;
int timer_init = 1; /* indicate timer initialization */
struct sockaddr_in6 all_sa;
#if defined(__NetBSD__) || defined(__OpenBSD__)
int s = splsoftnet();
#else
int s = splnet();
#endif
/*
* This function must not be called before mld_init().
* We've once experienced the violation of the order, so we put an
* explicit assertion here.
*/
if (all_nodes_linklocal == NULL)
panic("mld_start_listening: called too early");
/*
* RFC2710 page 10:
* The node never sends a Report or Done for the link-scope all-nodes
* address.
* MLD messages are never sent for multicast addresses whose scope is 0
* (reserved) or 1 (node-local).
*/
all_sa = *all_nodes_linklocal;
if (in6_addr2zoneid(in6m->in6m_ifp, &all_sa.sin6_addr,
&all_sa.sin6_scope_id)) {
/* XXX: this should not happen! */
in6m->in6m_timer = 0;
in6m->in6m_state = MLD_OTHERLISTENER;
}
if (in6_embedscope(&all_sa.sin6_addr, &all_sa)) {
/* XXX: this should not happen! */
panic("mld_start_listening: should not happen");
}
if (IN6_ARE_ADDR_EQUAL(&in6m->in6m_addr, &all_sa.sin6_addr) ||
IPV6_ADDR_MC_SCOPE(&in6m->in6m_addr) <
IPV6_ADDR_SCOPE_LINKLOCAL) {
mldlog((LOG_DEBUG,
"mld_start_listening: not send report for %s\n",
ip6_sprintf(&in6m->in6m_addr)));
in6m->in6m_timer = 0;
in6m->in6m_state = MLD_OTHERLISTENER;
} else {
if (in6m->in6m_rti->rt6i_type == MLD_V2_ROUTER) {
mldlog((LOG_DEBUG,
"mld_start_listening: send v2 report for %s\n",
ip6_sprintf(&in6m->in6m_addr)));
mld_send_state_change_report(&m, &buflen, in6m,
type, timer_init);
} else {
mldlog((LOG_DEBUG,
"mld_start_listening: send v1 report for %s\n",
ip6_sprintf(&in6m->in6m_addr)));
mld_sendpkt(in6m, MLD_LISTENER_REPORT, NULL);
in6m->in6m_timer =
MLD_RANDOM_DELAY(MLD_UNSOLICITED_REPORT_INTERVAL *
PR_FASTHZ);
in6m->in6m_state = MLD_IREPORTEDLAST;
mld_group_timers_are_running = 1;
}
}
splx(s);
}
void
mld_stop_listening(in6m)
struct in6_multi *in6m;
{
struct in6_addr allnode, allrouter;
struct sockaddr_in6 sa6;
sa6 = *all_nodes_linklocal;
if (in6_addr2zoneid(in6m->in6m_ifp, &sa6.sin6_addr,
&sa6.sin6_scope_id) || in6_embedscope(&allrouter, &sa6)) {
/* XXX: this should not happen! */
return;
}
sa6 = *all_routers_linklocal;
if (in6_addr2zoneid(in6m->in6m_ifp, &sa6.sin6_addr,
&sa6.sin6_scope_id) || in6_embedscope(&allrouter, &sa6)) {
/* XXX impossible */
return;
}
if (in6m->in6m_state == MLD_IREPORTEDLAST &&
!IN6_ARE_ADDR_EQUAL(&in6m->in6m_addr, &allnode) &&
IPV6_ADDR_MC_SCOPE(&in6m->in6m_addr) >
IPV6_ADDR_SCOPE_INTFACELOCAL) {
mld_sendpkt(in6m, MLD_LISTENER_DONE, &allrouter);
}
}
void
mld_input(m, off)
struct mbuf *m;
int off;
{
struct ip6_hdr *ip6 = mtod(m, struct ip6_hdr *);
struct mld_hdr *mldh;
struct ifnet *ifp = m->m_pkthdr.rcvif;
struct in6_multi *in6m = NULL;
struct sockaddr_in6 all_sa, mc_sa;
#if defined(__FreeBSD__) && __FreeBSD__ >= 3
struct ifmultiaddr *ifma;
#else
struct in6_ifaddr *ia;
#endif
int timer = 0; /* timer value in the MLD query header */
struct mldv2_hdr *mldv2h;
int query_ver = 0;
int query_type;
u_int16_t mldlen;
struct router6_info *rt6i;
#ifndef PULLDOWN_TEST
IP6_EXTHDR_CHECK(m, off, sizeof(*mldh),);
mldh = (struct mld_hdr *)(mtod(m, caddr_t) + off);
#else
IP6_EXTHDR_GET(mldh, struct mld_hdr *, m, off, sizeof(*mldh));
if (mldh == NULL) {
icmp6stat.icp6s_tooshort++;
return;
}
#endif
/* source address validation */
ip6 = mtod(m, struct ip6_hdr *); /* in case mpullup */
if (!IN6_IS_ADDR_LINKLOCAL(&ip6->ip6_src)) {
/*
* RFC3590 allows the IPv6 unspecified address as the source
* address of MLD report and done messages. However, as this
* same document says, this special rule is for snooping
* switches and the RFC requires routers to discard MLD packets
* with the unspecified source address. The RFC only talks
* about hosts receiving an MLD query or report in Security
* Considerations, but this is probably the correct intention.
* RFC3590 does not talk about other cases than link-local and
* the unspecified source addresses, but we believe the same
* rule should be applied.
* As a result, we only allow link-local addresses as the
* source address; otherwise, simply discard the packet.
*/
#if 0
/*
* XXX: do not log in an input path to avoid log flooding,
* though RFC3590 says "SHOULD log" if the source of a query
* is the unspecified address.
*/
log(LOG_INFO,
"mld_input: src %s is not link-local (grp=%s)\n",
ip6_sprintf(&ip6->ip6_src), ip6_sprintf(&mldh->mld_addr));
#endif
goto end;
}
/* convert the multicast address into a full sockaddr form */
bzero(&mc_sa, sizeof(mc_sa));
mc_sa.sin6_family = AF_INET6;
mc_sa.sin6_len = sizeof(mc_sa);
mc_sa.sin6_addr = mldh->mld_addr;
if (in6_addr2zoneid(ifp, &mc_sa.sin6_addr, &mc_sa.sin6_scope_id))
goto end; /* XXX: this should not happen! */
if (in6_embedscope(&mc_sa.sin6_addr, &mc_sa))
goto end; /* XXX: this should not happen! */
rt6i = find_rt6i(ifp);
if (rt6i == NULL) {
mldlog((LOG_DEBUG,
"mld_input(): cannot find router6_info at link#%d\n",
ifp->if_index));
goto end;
}
/*
* just transit to idle state preparing for MLDv1-fallback:
* same as mld_input in mld6.c
*/
if (mldh->mld_type == MLD_LISTENER_REPORT) {
/*
* For fast leave to work, we have to know that we are the
* last person to send a report for this group. Reports
* can potentially get looped back if we are a multicast
* router, so discard reports sourced by me.
* Note that it is impossible to check IFF_LOOPBACK flag of
* ifp for this purpose, since ip6_mloopback pass the physical
* interface to looutput.
*/
if (m->m_flags & M_LOOP) /* XXX: grotty flag, but efficient */
goto end;
if (!IN6_IS_ADDR_MULTICAST(&mldh->mld_addr))
goto end;
/*
* If we belong to the group being reported, stop
* our timer for that group.
*/
IN6_LOOKUP_MULTI(mc_sa.sin6_addr, ifp, in6m);
if (in6m) {
in6m->in6m_timer = 0; /* transit to idle state */
in6m->in6m_state = MLD_OTHERLISTENER; /* clear flag */
}
goto end;
}
if (mldh->mld_type != MLD_LISTENER_QUERY)
goto end;
/* MLDv1/v2 Query */
if (ifp->if_flags & IFF_LOOPBACK)
goto end;
if (!IN6_IS_ADDR_UNSPECIFIED(&mldh->mld_addr) &&
!IN6_IS_ADDR_MULTICAST(&mldh->mld_addr))
goto end; /* print error or log stat? */
/* XXX: there should be no error here! */
all_sa = *all_nodes_linklocal;
if (in6_addr2zoneid(ifp, &all_sa.sin6_addr, &all_sa.sin6_scope_id))
goto end;
if (in6_embedscope(&all_sa.sin6_addr, &all_sa))
goto end;
/*
* MLD version and Query type check.
* MLDv1 Query: length = 24 octets AND Max-Resp-Code = 0
* MLDv2 Query: length >= 28 octets AND Max-Resp-Code != 0
* (MLDv1 implementation must accept only the first 24
* octets of the query message)
*/
mldlen = m->m_pkthdr.len - off;
if (mldlen > MLD_MINLEN && mldlen < MLD_V2_QUERY_MINLEN) {
mldlog((LOG_DEBUG, "invalid MLD packet(len=%d)\n", mldlen));
goto end;
}
if (mldlen == MLD_MINLEN) {
rt6i->rt6i_type = query_ver = MLD_V1_ROUTER;
mldlog((LOG_DEBUG, "regard it as MLDv1 Query from %s for %s\n",
ip6_sprintf(&ip6->ip6_src),
ip6_sprintf(&mldh->mld_addr)));
goto mldv1_query;
}
/* MLDv2 Query: fall back to MLDv1 Query, if necessary */
query_ver = MLD_V2_ROUTER;
/* no buffer-overrun here, since mldlen >= MLD_V2_QUERY_MINLEN */
mldv2h = (struct mldv2_hdr *) mldh;
if (query_ver != MLD_V2_ROUTER)
goto end;
/* judge query type */
if (IN6_IS_ADDR_UNSPECIFIED(&mldh->mld_addr)) {
if (mldv2h->mld_numsrc != 0) {
mldlog((LOG_DEBUG, "invalid general query(numsrc=%d)\n",
mldv2h->mld_numsrc));
goto end;
}
query_type = MLD_V2_GENERAL_QUERY;
mldlog((LOG_DEBUG, "MLDv2 general Query\n"));
goto set_timer;
}
if (IN6_IS_ADDR_MULTICAST(&mldh->mld_addr)) {
if (mldv2h->mld_numsrc == 0) {
query_type = MLD_V2_GROUP_QUERY;
mldlog((LOG_DEBUG, "MLDv2 group Query\n"));
} else {
query_type = MLD_V2_GROUP_SOURCE_QUERY;
mldlog((LOG_DEBUG, "MLDv2 source-group Query\n"));
}
goto set_timer;
}
mldlog((LOG_DEBUG, "invalid MLDv2 Query (group=%s)\n",
ip6_sprintf(&mldh->mld_addr)));
goto end;
set_timer:
if (rt6i->rt6i_type == MLD_V1_ROUTER)
goto mldv1_query;
if (mld_set_timer(ifp, rt6i, mldh, mldlen, query_type) != 0)
mldlog((LOG_DEBUG, "mld_input: receive bad query\n"));
goto end;
mldv1_query:
/* MLDv1 Query: same as mld_input in mld6.c */
timer = ntohs(mldh->mld_maxdelay) * PR_FASTHZ / MLD_TIMER_SCALE;
if (timer == 0 && mldh->mld_maxdelay)
timer = 1;
#if defined(__FreeBSD__) && __FreeBSD_version >= 500000
TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link)
#elif defined(__FreeBSD__)
for (ifma = LIST_FIRST(&ifp->if_multiaddrs);
ifma;
ifma = LIST_NEXT(ifma, ifma_link))
#else
IFP_TO_IA6(ifp, ia);
if (ia == NULL)
return; /* XXX */
for (in6m = LIST_FIRST(&ia->ia6_multiaddrs);
in6m;
in6m = LIST_NEXT(in6m, in6m_entry))
#endif
{
#ifdef __FreeBSD__
if (ifma->ifma_addr->sa_family != AF_INET6)
continue;
in6m = (struct in6_multi *)ifma->ifma_protospec;
#endif
if (IN6_ARE_ADDR_EQUAL(&in6m->in6m_addr, &all_sa.sin6_addr) ||
IPV6_ADDR_MC_SCOPE(&in6m->in6m_addr) <
IPV6_ADDR_SCOPE_LINKLOCAL)
continue;
if (!IN6_IS_ADDR_UNSPECIFIED(&mldh->mld_addr) &&
!IN6_ARE_ADDR_EQUAL(&mldh->mld_addr, &in6m->in6m_addr))
continue;
if (timer == 0) {
mldlog((LOG_DEBUG, "send an MLDv1 report now\n"));
/* send a report immediately */
mld_sendpkt(in6m, MLD_LISTENER_REPORT, NULL);
in6m->in6m_timer = 0; /* reset timer */
in6m->in6m_state = MLD_IREPORTEDLAST;
} else if (in6m->in6m_timer == 0 || /*idle state*/
in6m->in6m_timer > timer) {
mldlog((LOG_DEBUG, "invoke a MLDv1 timer\n"));
in6m->in6m_timer = MLD_RANDOM_DELAY(timer);
mld_group_timers_are_running = 1;
}
}
/*
* MLDv1 Querier Present is set to Older Version Querier Present
* Timeout seconds whenever an MLDv1 General Query is received.
*/
if (mldalways_v2 == 0 &&
IN6_ARE_ADDR_EQUAL(&mldh->mld_addr, &in6addr_any)) {
mldlog((LOG_DEBUG, "shift to MLDv1-compat mode\n"));
mld_set_hostcompat(ifp, rt6i, query_ver);
}
goto end;
end:
m_freem(m);
return;
}
void
mld_fasttimeo()
{
struct in6_multi *in6m;
struct in6_multistep step;
struct ifnet *ifp = NULL;
/*
* Both of Current-State Record timer and State-Change Record timer
* are controled.
*/
struct router6_info *rt6i;
struct mbuf *cm, *sm;
int cbuflen, sbuflen;
int s;
/*
* Quick check to see if any work needs to be done, in order
* to minimize the overhead of fasttimo processing.
*/
if (!mld_group_timers_are_running && !mld_interface_timers_are_running
&& !mld_state_change_timers_are_running)
return;
#if defined(__NetBSD__) || defined(__OpenBSD__)
s = splsoftnet();
#else
s = splnet();
#endif
if (mld_interface_timers_are_running) {
mld_interface_timers_are_running = 0;
for (rt6i = Head6; rt6i; rt6i = rt6i->rt6i_next) {
if (rt6i->rt6i_timer2 == 0)
continue; /* do nothing */
rt6i->rt6i_timer2--;
if (rt6i->rt6i_timer2 > 0) {
mld_interface_timers_are_running = 1;
continue;
}
mld_send_all_current_state_report(rt6i->rt6i_ifp);
}
}
if (!mld_group_timers_are_running &&
!mld_state_change_timers_are_running) {
splx(s);
return;
}
mld_group_timers_are_running = 0;
mld_state_change_timers_are_running = 0;
cm = sm = NULL;
cbuflen = sbuflen = 0;
IN6_FIRST_MULTI(step, in6m);
if (in6m == NULL) {
splx(s);
return;
}
ifp = in6m->in6m_ifp;
while (in6m != NULL) {
if (in6m->in6m_timer == 0)
goto next_in6m; /* do nothing */
--in6m->in6m_timer;
if (in6m->in6m_timer > 0) {
mld_group_timers_are_running = 1;
goto bypass_state_transition;
}
if (in6m->in6m_rti->rt6i_type == MLD_V1_ROUTER) {
mldlog((LOG_DEBUG, "mld_fasttimeo: v1 report\n"));
mld_sendpkt(in6m, MLD_LISTENER_REPORT, NULL);
in6m->in6m_state = MLD_IREPORTEDLAST;
} else if (in6m->in6m_state == MLD_G_QUERY_PENDING_MEMBER ||
in6m->in6m_state == MLD_SG_QUERY_PENDING_MEMBER) {
if (cm != NULL && ifp != in6m->in6m_ifp) {
mldlog((LOG_DEBUG, "mld_fasttimeo: v2 report\n"));
mld_sendbuf(cm, ifp);
cm = NULL;
}
mld_send_current_state_report(&cm, &cbuflen, in6m);
ifp = in6m->in6m_ifp;
in6m->in6m_state = MLD_OTHERLISTENER;
}
bypass_state_transition:
if (IN6_IS_LOCAL_GROUP(&in6m->in6m_addr))
goto next_in6m; /* skip */
if (in6m->in6m_source == NULL)
goto next_in6m;
if (in6m->in6m_source->i6ms_timer == 0)
goto next_in6m; /* do nothing */
--in6m->in6m_source->i6ms_timer;
if (in6m->in6m_source->i6ms_timer > 0) {
mld_state_change_timers_are_running = 1;
goto next_in6m;
}
if (sm != NULL && ifp != in6m->in6m_ifp) {
mld_sendbuf(sm, ifp);
sm = NULL;
}
/*
* Check if this report was pending Source-List-Change
* report or not. It is only the case that robvar was
* not reduced here. (XXX rarely, QRV may be changed
* in a same timing.)
*/
mldlog((LOG_DEBUG, "mld_fasttimeo: handles pending report\n"));
if (in6m->in6m_source->i6ms_robvar
== in6m->in6m_rti->rt6i_qrv) {
mld_send_state_change_report(&sm, &sbuflen, in6m, 0, 1);
sm = NULL;
} else if (in6m->in6m_source->i6ms_robvar > 0) {
mld_send_state_change_report(&sm, &sbuflen, in6m, 0, 0);
ifp = in6m->in6m_ifp;
}
if (in6m->in6m_source->i6ms_robvar != 0) {
in6m->in6m_source->i6ms_timer =
MLD_RANDOM_DELAY(MLDV2_UNSOL_INTVL * PR_FASTHZ);
mld_state_change_timers_are_running = 1;
}
next_in6m:
IN6_NEXT_MULTI(step, in6m);
}
if (cm != NULL)
mld_sendbuf(cm, ifp);
if (sm != NULL)
mld_sendbuf(sm, ifp);
splx(s);
}
void
mld_slowtimeo()
{
struct router6_info *rt6i;
int s;
#if defined(__NetBSD__) || defined(__OpenBSD__)
s = splsoftnet();
#else
s = splnet();
#endif
for (rt6i = Head6; rt6i != 0; rt6i = rt6i->rt6i_next) {
if (rt6i->rt6i_timer1 == 0) {
if (rt6i->rt6i_type != MLD_V2_ROUTER)
rt6i->rt6i_type = MLD_V2_ROUTER;
} else if (rt6i->rt6i_timer1 > 0) {
--rt6i->rt6i_timer1;
if (rt6i->rt6i_timer2 > 0)
rt6i->rt6i_timer2 = 0;
}
}
splx(s);
}
static void
mld_sendpkt(in6m, type, dst)
struct in6_multi *in6m;
int type;
const struct in6_addr *dst;
{
struct mbuf *mh;
struct mld_hdr *mldh;
struct ip6_hdr *ip6 = NULL;
struct ip6_moptions im6o;
struct ifnet *ifp = in6m->in6m_ifp;
struct in6_ifaddr *ia = NULL;
/*
* At first, find a link local address on the outgoing interface
* to use as the source address of the MLD packet.
* We do not reject tentative addresses for MLD report to deal with
* the case where we first join a link-local address.
*/
if ((ia = in6ifa_ifpforlinklocal(ifp, ignflags)) == NULL)
return;
if ((ia->ia6_flags & IN6_IFF_TENTATIVE))
ia = NULL;
/* Allocate two mbufs to store IPv6 header and MLD header */
mldh = mld_allocbuf(&mh, MLD_MINLEN, in6m, type);
if (mldh == NULL)
return;
/* fill src/dst here */
ip6 = mtod(mh, struct ip6_hdr *);
ip6->ip6_src = ia ? ia->ia_addr.sin6_addr : in6addr_any;
ip6->ip6_dst = dst ? *dst : in6m->in6m_addr;
mldh->mld_addr = in6m->in6m_addr;
in6_clearscope(&mldh->mld_addr); /* XXX */
mldh->mld_cksum = in6_cksum(mh, IPPROTO_ICMPV6, sizeof(struct ip6_hdr),
MLD_MINLEN);
/* construct multicast option */
bzero(&im6o, sizeof(im6o));
im6o.im6o_multicast_ifp = ifp;
im6o.im6o_multicast_hlim = 1;
/*
* Request loopback of the report if we are acting as a multicast
* router, so that the process-level routing daemon can hear it.
*/
im6o.im6o_multicast_loop = (ip6_mrouter != NULL);
/* increment output statictics */
icmp6stat.icp6s_outhist[type]++;
icmp6_ifstat_inc(ifp, ifs6_out_msg);
switch (type) {
case MLD_LISTENER_QUERY:
icmp6_ifstat_inc(ifp, ifs6_out_mldquery);
break;
case MLD_LISTENER_REPORT:
icmp6_ifstat_inc(ifp, ifs6_out_mldreport);
break;
case MLD_LISTENER_DONE:
icmp6_ifstat_inc(ifp, ifs6_out_mlddone);
break;
}
ip6_output(mh, &ip6_opts, NULL,
ia ? 0 : IPV6_UNSPECSRC, &im6o, NULL, NULL);
}
static struct mld_hdr *
mld_allocbuf(mh, len, in6m, type)
struct mbuf **mh;
int len;
struct in6_multi *in6m;
int type;
{
struct mbuf *md;
struct mld_hdr *mldh;
struct ip6_hdr *ip6;
/*
* Allocate mbufs to store ip6 header and MLD header.
* We allocate 2 mbufs and make chain in advance because
* it is more convenient when inserting the hop-by-hop option later.
*/
MGETHDR(*mh, M_DONTWAIT, MT_HEADER);
if (*mh == NULL)
return NULL;
MGET(md, M_DONTWAIT, MT_DATA);
/* uses cluster in case of MLDv2 */
if (md && (len > MLD_MINLEN || type == MLDV2_LISTENER_REPORT)) {
/* XXX: assumes len is less than 2K Byte */
MCLGET(md, M_DONTWAIT);
if ((md->m_flags & M_EXT) == 0) {
m_free(md);
md = NULL;
}
}
if (md == NULL) {
m_free(*mh);
*mh = NULL;
return NULL;
}
(*mh)->m_next = md;
md->m_next = NULL;
(*mh)->m_pkthdr.rcvif = NULL;
(*mh)->m_pkthdr.len = sizeof(struct ip6_hdr) + len;
(*mh)->m_len = sizeof(struct ip6_hdr);
MH_ALIGN(*mh, sizeof(struct ip6_hdr));
/* fill in the ip6 header */
ip6 = mtod(*mh, struct ip6_hdr *);
bzero(ip6, sizeof(*ip6));
ip6->ip6_flow = 0;
ip6->ip6_vfc &= ~IPV6_VERSION_MASK;
ip6->ip6_vfc |= IPV6_VERSION;
/* ip6_plen will be set later */
ip6->ip6_nxt = IPPROTO_ICMPV6;
/* ip6_hlim will be set by im6o.im6o_multicast_hlim */