This source file includes following definitions.
- ip6_init
- ip6_init2
- ip6intr
- ip6_input
- ip6_check_rh0hdr
- ip6_hopopts_input
- ip6_process_hopopts
- ip6_unknown_opt
- ip6_savecontrol
- ip6_pullexthdr
- ip6_get_prevhdr
- ip6_nexthdr
- ip6_lasthdr
- ip6_sysctl
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 #include "pf.h"
65 #include "carp.h"
66
67 #include <sys/param.h>
68 #include <sys/systm.h>
69 #include <sys/malloc.h>
70 #include <sys/mbuf.h>
71 #include <sys/domain.h>
72 #include <sys/protosw.h>
73 #include <sys/socket.h>
74 #include <sys/socketvar.h>
75 #include <sys/errno.h>
76 #include <sys/time.h>
77 #include <sys/kernel.h>
78 #include <sys/syslog.h>
79 #include <sys/proc.h>
80
81 #include <net/if.h>
82 #include <net/if_types.h>
83 #include <net/if_dl.h>
84 #include <net/route.h>
85 #include <net/netisr.h>
86
87 #include <netinet/in.h>
88 #include <netinet/in_systm.h>
89
90 #ifdef INET
91 #include <netinet/ip.h>
92 #include <netinet/ip_icmp.h>
93 #endif
94
95 #include <netinet/in_pcb.h>
96 #include <netinet6/in6_var.h>
97 #include <netinet/ip6.h>
98 #include <netinet6/ip6_var.h>
99 #include <netinet/icmp6.h>
100 #include <netinet6/in6_ifattach.h>
101 #include <netinet6/nd6.h>
102
103 #include <netinet6/ip6protosw.h>
104
105 #include "faith.h"
106 #include "gif.h"
107 #include "bpfilter.h"
108
109 #if NPF > 0
110 #include <net/pfvar.h>
111 #endif
112
113 #if NCARP > 0
114 #include <netinet/in_var.h>
115 #include <netinet/ip_carp.h>
116 #endif
117
118 extern struct domain inet6domain;
119 extern struct ip6protosw inet6sw[];
120
121 u_char ip6_protox[IPPROTO_MAX];
122 static int ip6qmaxlen = IFQ_MAXLEN;
123 struct in6_ifaddr *in6_ifaddr;
124 struct ifqueue ip6intrq;
125
126 int ip6_forward_srcrt;
127 int ip6_sourcecheck;
128 int ip6_sourcecheck_interval;
129
130 struct ip6stat ip6stat;
131
132 static void ip6_init2(void *);
133 int ip6_check_rh0hdr(struct mbuf *);
134
135 static int ip6_hopopts_input(u_int32_t *, u_int32_t *, struct mbuf **, int *);
136 static struct mbuf *ip6_pullexthdr(struct mbuf *, size_t, int);
137
138
139
140
141
142 void
143 ip6_init()
144 {
145 struct ip6protosw *pr;
146 int i;
147
148 pr = (struct ip6protosw *)pffindproto(PF_INET6, IPPROTO_RAW, SOCK_RAW);
149 if (pr == 0)
150 panic("ip6_init");
151 for (i = 0; i < IPPROTO_MAX; i++)
152 ip6_protox[i] = pr - inet6sw;
153 for (pr = (struct ip6protosw *)inet6domain.dom_protosw;
154 pr < (struct ip6protosw *)inet6domain.dom_protoswNPROTOSW; pr++)
155 if (pr->pr_domain->dom_family == PF_INET6 &&
156 pr->pr_protocol && pr->pr_protocol != IPPROTO_RAW)
157 ip6_protox[pr->pr_protocol] = pr - inet6sw;
158 ip6intrq.ifq_maxlen = ip6qmaxlen;
159 nd6_init();
160 frag6_init();
161 ip6_init2((void *)0);
162 }
163
164 static void
165 ip6_init2(dummy)
166 void *dummy;
167 {
168
169
170 bzero(&nd6_timer_ch, sizeof(nd6_timer_ch));
171 timeout_set(&nd6_timer_ch, nd6_timer, NULL);
172 timeout_add(&nd6_timer_ch, hz);
173 }
174
175
176
177
178 void
179 ip6intr()
180 {
181 int s;
182 struct mbuf *m;
183
184 while (ip6intrq.ifq_head) {
185 s = splnet();
186 IF_DEQUEUE(&ip6intrq, m);
187 splx(s);
188 if (m == 0)
189 return;
190 ip6_input(m);
191 }
192 }
193
194 extern struct route_in6 ip6_forward_rt;
195 extern int ip6_forward_rtableid;
196
197 void
198 ip6_input(m)
199 struct mbuf *m;
200 {
201 struct ip6_hdr *ip6;
202 int off = sizeof(struct ip6_hdr), nest;
203 u_int32_t plen;
204 u_int32_t rtalert = ~0;
205 int nxt, ours = 0;
206 struct ifnet *deliverifp = NULL;
207 #if NPF > 0
208 struct in6_addr odst;
209 #endif
210 int srcrt = 0, rtableid = 0;
211
212
213
214
215 if (m->m_flags & M_EXT) {
216 if (m->m_next)
217 ip6stat.ip6s_mext2m++;
218 else
219 ip6stat.ip6s_mext1++;
220 } else {
221 #define M2MMAX (sizeof(ip6stat.ip6s_m2m)/sizeof(ip6stat.ip6s_m2m[0]))
222 if (m->m_next) {
223 if (m->m_flags & M_LOOP) {
224 ip6stat.ip6s_m2m[lo0ifp->if_index]++;
225 } else if (m->m_pkthdr.rcvif->if_index < M2MMAX)
226 ip6stat.ip6s_m2m[m->m_pkthdr.rcvif->if_index]++;
227 else
228 ip6stat.ip6s_m2m[0]++;
229 } else
230 ip6stat.ip6s_m1++;
231 #undef M2MMAX
232 }
233
234 in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_receive);
235 ip6stat.ip6s_total++;
236
237 if (m->m_len < sizeof(struct ip6_hdr)) {
238 struct ifnet *inifp;
239 inifp = m->m_pkthdr.rcvif;
240 if ((m = m_pullup(m, sizeof(struct ip6_hdr))) == NULL) {
241 ip6stat.ip6s_toosmall++;
242 in6_ifstat_inc(inifp, ifs6_in_hdrerr);
243 return;
244 }
245 }
246
247 ip6 = mtod(m, struct ip6_hdr *);
248
249 if ((ip6->ip6_vfc & IPV6_VERSION_MASK) != IPV6_VERSION) {
250 ip6stat.ip6s_badvers++;
251 in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_hdrerr);
252 goto bad;
253 }
254
255 #if NCARP > 0
256 if (m->m_pkthdr.rcvif->if_type == IFT_CARP &&
257 m->m_pkthdr.rcvif->if_flags & IFF_LINK0 &&
258 ip6->ip6_nxt != IPPROTO_ICMPV6 &&
259 carp_lsdrop(m, AF_INET6, ip6->ip6_src.s6_addr32,
260 ip6->ip6_dst.s6_addr32))
261 goto bad;
262 #endif
263 ip6stat.ip6s_nxthist[ip6->ip6_nxt]++;
264
265
266
267
268 if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_src) ||
269 IN6_IS_ADDR_UNSPECIFIED(&ip6->ip6_dst)) {
270
271
272
273 ip6stat.ip6s_badscope++;
274 in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_addrerr);
275 goto bad;
276 }
277
278 if (IN6_IS_ADDR_MC_INTFACELOCAL(&ip6->ip6_dst) &&
279 !(m->m_flags & M_LOOP)) {
280
281
282
283
284
285
286 ip6stat.ip6s_badscope++;
287 in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_addrerr);
288 goto bad;
289 }
290
291
292
293
294
295
296
297
298
299
300
301
302
303 if (IN6_IS_ADDR_V4MAPPED(&ip6->ip6_src) ||
304 IN6_IS_ADDR_V4MAPPED(&ip6->ip6_dst)) {
305 ip6stat.ip6s_badscope++;
306 in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_addrerr);
307 goto bad;
308 }
309 #if 0
310
311
312
313
314
315
316
317 if (IN6_IS_ADDR_V4COMPAT(&ip6->ip6_src) ||
318 IN6_IS_ADDR_V4COMPAT(&ip6->ip6_dst)) {
319 ip6stat.ip6s_badscope++;
320 in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_addrerr);
321 goto bad;
322 }
323 #endif
324
325 if (ip6_check_rh0hdr(m)) {
326 ip6stat.ip6s_badoptions++;
327 in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_discard);
328 in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_hdrerr);
329 icmp6_error(m, ICMP6_PARAM_PROB, ICMP6_PARAMPROB_OPTION, 0);
330
331 return;
332 }
333
334 #if NPF > 0
335
336
337
338 odst = ip6->ip6_dst;
339 if (pf_test6(PF_IN, m->m_pkthdr.rcvif, &m, NULL) != PF_PASS)
340 goto bad;
341 if (m == NULL)
342 return;
343
344 ip6 = mtod(m, struct ip6_hdr *);
345 srcrt = !IN6_ARE_ADDR_EQUAL(&odst, &ip6->ip6_dst);
346 #endif
347
348 if (IN6_IS_ADDR_LOOPBACK(&ip6->ip6_src) ||
349 IN6_IS_ADDR_LOOPBACK(&ip6->ip6_dst)) {
350 if (m->m_pkthdr.rcvif->if_flags & IFF_LOOPBACK) {
351 ours = 1;
352 deliverifp = m->m_pkthdr.rcvif;
353 goto hbhcheck;
354 } else {
355 ip6stat.ip6s_badscope++;
356 in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_addrerr);
357 goto bad;
358 }
359 }
360
361
362 if ((m->m_pkthdr.rcvif->if_flags & IFF_LOOPBACK) == 0) {
363 if (IN6_IS_SCOPE_EMBED(&ip6->ip6_src) &&
364 ip6->ip6_src.s6_addr16[1]) {
365 ip6stat.ip6s_badscope++;
366 goto bad;
367 }
368 if (IN6_IS_SCOPE_EMBED(&ip6->ip6_dst) &&
369 ip6->ip6_dst.s6_addr16[1]) {
370 ip6stat.ip6s_badscope++;
371 goto bad;
372 }
373 }
374
375 if (IN6_IS_SCOPE_EMBED(&ip6->ip6_src))
376 ip6->ip6_src.s6_addr16[1] = htons(m->m_pkthdr.rcvif->if_index);
377 if (IN6_IS_SCOPE_EMBED(&ip6->ip6_dst))
378 ip6->ip6_dst.s6_addr16[1] = htons(m->m_pkthdr.rcvif->if_index);
379
380
381
382
383
384
385
386
387
388
389 if ((m->m_pkthdr.rcvif->if_flags & IFF_LOOPBACK) != 0 &&
390 IN6_IS_ADDR_LINKLOCAL(&ip6->ip6_dst)) {
391 if (!in6ifa_ifpwithaddr(m->m_pkthdr.rcvif, &ip6->ip6_dst)) {
392 icmp6_error(m, ICMP6_DST_UNREACH,
393 ICMP6_DST_UNREACH_ADDR, 0);
394
395 return;
396 }
397
398 ours = 1;
399 deliverifp = m->m_pkthdr.rcvif;
400 goto hbhcheck;
401 }
402
403
404
405
406 if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst)) {
407 struct in6_multi *in6m = 0;
408
409 in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_mcast);
410
411
412
413
414 IN6_LOOKUP_MULTI(ip6->ip6_dst, m->m_pkthdr.rcvif, in6m);
415 if (in6m)
416 ours = 1;
417 #ifdef MROUTING
418 else if (!ip6_mforwarding || !ip6_mrouter)
419 #else
420 else
421 #endif
422 {
423 ip6stat.ip6s_notmember++;
424 if (!IN6_IS_ADDR_MC_LINKLOCAL(&ip6->ip6_dst))
425 ip6stat.ip6s_cantforward++;
426 in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_discard);
427 goto bad;
428 }
429 deliverifp = m->m_pkthdr.rcvif;
430 goto hbhcheck;
431 }
432
433 #if NPF > 0
434 rtableid = m->m_pkthdr.pf.rtableid;
435 #endif
436
437
438
439
440 if (ip6_forward_rt.ro_rt != NULL &&
441 (ip6_forward_rt.ro_rt->rt_flags & RTF_UP) != 0 &&
442 IN6_ARE_ADDR_EQUAL(&ip6->ip6_dst,
443 &ip6_forward_rt.ro_dst.sin6_addr) &&
444 rtableid == ip6_forward_rtableid)
445 ip6stat.ip6s_forward_cachehit++;
446 else {
447 if (ip6_forward_rt.ro_rt) {
448
449 ip6stat.ip6s_forward_cachemiss++;
450 RTFREE(ip6_forward_rt.ro_rt);
451 ip6_forward_rt.ro_rt = 0;
452 }
453
454 bzero(&ip6_forward_rt.ro_dst, sizeof(struct sockaddr_in6));
455 ip6_forward_rt.ro_dst.sin6_len = sizeof(struct sockaddr_in6);
456 ip6_forward_rt.ro_dst.sin6_family = AF_INET6;
457 ip6_forward_rt.ro_dst.sin6_addr = ip6->ip6_dst;
458 ip6_forward_rtableid = rtableid;
459
460 rtalloc_mpath((struct route *)&ip6_forward_rt,
461 &ip6->ip6_src.s6_addr32[0], rtableid);
462 }
463
464 #define rt6_key(r) ((struct sockaddr_in6 *)((r)->rt_nodes->rn_key))
465
466
467
468
469
470
471
472
473
474
475 if (ip6_forward_rt.ro_rt &&
476 (ip6_forward_rt.ro_rt->rt_flags &
477 (RTF_HOST|RTF_GATEWAY)) == RTF_HOST &&
478 #if 0
479
480
481
482
483
484 IN6_ARE_ADDR_EQUAL(&ip6->ip6_dst,
485 &rt6_key(ip6_forward_rt.ro_rt)->sin6_addr) &&
486 #endif
487 ip6_forward_rt.ro_rt->rt_ifp->if_type == IFT_LOOP) {
488 struct in6_ifaddr *ia6 =
489 (struct in6_ifaddr *)ip6_forward_rt.ro_rt->rt_ifa;
490 if (ia6->ia6_flags & IN6_IFF_ANYCAST)
491 m->m_flags |= M_ANYCAST6;
492
493
494
495
496 if (!(ia6->ia6_flags & IN6_IFF_NOTREADY)) {
497
498 ours = 1;
499 deliverifp = ia6->ia_ifp;
500 goto hbhcheck;
501 } else {
502
503 nd6log((LOG_INFO,
504 "ip6_input: packet to an unready address %s->%s\n",
505 ip6_sprintf(&ip6->ip6_src),
506 ip6_sprintf(&ip6->ip6_dst)));
507
508 goto bad;
509 }
510 }
511
512
513
514
515 #if defined(NFAITH) && 0 < NFAITH
516 if (ip6_keepfaith) {
517 if (ip6_forward_rt.ro_rt && ip6_forward_rt.ro_rt->rt_ifp
518 && ip6_forward_rt.ro_rt->rt_ifp->if_type == IFT_FAITH) {
519
520 ours = 1;
521 deliverifp = ip6_forward_rt.ro_rt->rt_ifp;
522 goto hbhcheck;
523 }
524 }
525 #endif
526
527 #if 0
528 {
529
530
531
532
533
534 struct ifaddr *ifa;
535 TAILQ_FOREACH(ifa, &m->m_pkthdr.rcvif->if_addrlist, ifa_list) {
536 if (ifa->ifa_addr == NULL)
537 continue;
538 if (ifa->ifa_addr->sa_family != AF_INET6)
539 continue;
540 if (IN6_ARE_ADDR_EQUAL(IFA_IN6(ifa), &ip6->ip6_dst)) {
541 ours = 1;
542 deliverifp = ifa->ifa_ifp;
543 goto hbhcheck;
544 }
545 }
546 }
547 #endif
548
549 #if NCARP > 0
550 if (m->m_pkthdr.rcvif->if_type == IFT_CARP &&
551 m->m_pkthdr.rcvif->if_flags & IFF_LINK0 &&
552 ip6->ip6_nxt == IPPROTO_ICMPV6 &&
553 carp_lsdrop(m, AF_INET6, ip6->ip6_src.s6_addr32,
554 ip6->ip6_dst.s6_addr32))
555 goto bad;
556 #endif
557
558
559
560
561 if (!ip6_forwarding) {
562 ip6stat.ip6s_cantforward++;
563 in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_discard);
564 goto bad;
565 }
566
567 hbhcheck:
568
569
570
571
572
573 plen = (u_int32_t)ntohs(ip6->ip6_plen);
574 if (ip6->ip6_nxt == IPPROTO_HOPOPTS) {
575 struct ip6_hbh *hbh;
576
577 if (ip6_hopopts_input(&plen, &rtalert, &m, &off)) {
578 #if 0
579 in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_discard);
580 #endif
581 return;
582 }
583
584
585 ip6 = mtod(m, struct ip6_hdr *);
586
587
588
589
590
591
592 if (ip6->ip6_plen == 0 && plen == 0) {
593
594
595
596
597
598 ip6stat.ip6s_badoptions++;
599 in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_discard);
600 in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_hdrerr);
601 icmp6_error(m, ICMP6_PARAM_PROB,
602 ICMP6_PARAMPROB_HEADER,
603 (caddr_t)&ip6->ip6_plen - (caddr_t)ip6);
604 return;
605 }
606 IP6_EXTHDR_GET(hbh, struct ip6_hbh *, m, sizeof(struct ip6_hdr),
607 sizeof(struct ip6_hbh));
608 if (hbh == NULL) {
609 ip6stat.ip6s_tooshort++;
610 return;
611 }
612 nxt = hbh->ip6h_nxt;
613
614
615
616
617
618 if (rtalert != ~0 && ip6_forwarding)
619 ours = 1;
620 } else
621 nxt = ip6->ip6_nxt;
622
623
624
625
626
627
628
629 if (m->m_pkthdr.len - sizeof(struct ip6_hdr) < plen) {
630 ip6stat.ip6s_tooshort++;
631 in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_truncated);
632 goto bad;
633 }
634 if (m->m_pkthdr.len > sizeof(struct ip6_hdr) + plen) {
635 if (m->m_len == m->m_pkthdr.len) {
636 m->m_len = sizeof(struct ip6_hdr) + plen;
637 m->m_pkthdr.len = sizeof(struct ip6_hdr) + plen;
638 } else
639 m_adj(m, sizeof(struct ip6_hdr) + plen - m->m_pkthdr.len);
640 }
641
642
643
644
645 if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst)) {
646
647
648
649
650
651
652
653
654 #ifdef MROUTING
655 if (ip6_mforwarding && ip6_mrouter &&
656 ip6_mforward(ip6, m->m_pkthdr.rcvif, m)) {
657 ip6stat.ip6s_cantforward++;
658 m_freem(m);
659 return;
660 }
661 #endif
662 if (!ours) {
663 m_freem(m);
664 return;
665 }
666 } else if (!ours) {
667 ip6_forward(m, srcrt);
668 return;
669 }
670
671 ip6 = mtod(m, struct ip6_hdr *);
672
673
674
675
676
677
678
679
680
681
682 if (IN6_IS_ADDR_V4MAPPED(&ip6->ip6_src) ||
683 IN6_IS_ADDR_V4MAPPED(&ip6->ip6_dst)) {
684 ip6stat.ip6s_badscope++;
685 in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_addrerr);
686 goto bad;
687 }
688
689
690
691
692 ip6stat.ip6s_delivered++;
693 in6_ifstat_inc(deliverifp, ifs6_in_deliver);
694 nest = 0;
695
696 while (nxt != IPPROTO_DONE) {
697 if (ip6_hdrnestlimit && (++nest > ip6_hdrnestlimit)) {
698 ip6stat.ip6s_toomanyhdr++;
699 goto bad;
700 }
701
702
703
704
705
706 if (m->m_pkthdr.len < off) {
707 ip6stat.ip6s_tooshort++;
708 in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_truncated);
709 goto bad;
710 }
711
712 nxt = (*inet6sw[ip6_protox[nxt]].pr_input)(&m, &off, nxt);
713 }
714 return;
715 bad:
716 m_freem(m);
717 }
718
719
720
721 int
722 ip6_check_rh0hdr(struct mbuf *m)
723 {
724 struct ip6_hdr *ip6 = mtod(m, struct ip6_hdr *);
725 struct ip6_rthdr rthdr;
726 struct ip6_ext opt6;
727 u_int8_t proto = ip6->ip6_nxt;
728 int done = 0, lim, off, rh_cnt = 0;
729
730 off = ((caddr_t)ip6 - m->m_data) + sizeof(struct ip6_hdr);
731 lim = min(m->m_pkthdr.len, ntohs(ip6->ip6_plen) + sizeof(*ip6));
732 do {
733 switch (proto) {
734 case IPPROTO_ROUTING:
735 if (rh_cnt++) {
736
737 return (1);
738 }
739
740 if (off + sizeof(opt6) > lim) {
741
742 return (1);
743 }
744
745 m_copydata(m, off, sizeof(rthdr), (caddr_t)&rthdr);
746
747 if (rthdr.ip6r_type == IPV6_RTHDR_TYPE_0)
748 return (1);
749
750 off += (rthdr.ip6r_len + 1) * 8;
751 proto = rthdr.ip6r_nxt;
752 break;
753 case IPPROTO_AH:
754 case IPPROTO_HOPOPTS:
755 case IPPROTO_DSTOPTS:
756
757 if (off + sizeof(opt6) > lim) {
758
759
760
761
762
763 return (0);
764 }
765
766 m_copydata(m, off, sizeof(opt6), (caddr_t)&opt6);
767
768 if (proto == IPPROTO_AH)
769 off += (opt6.ip6e_len + 2) * 4;
770 else
771 off += (opt6.ip6e_len + 1) * 8;
772 proto = opt6.ip6e_nxt;
773 break;
774 case IPPROTO_FRAGMENT:
775 default:
776
777 done = 1;
778 break;
779 }
780 } while (!done);
781
782 return (0);
783 }
784
785
786
787
788
789 static int
790 ip6_hopopts_input(plenp, rtalertp, mp, offp)
791 u_int32_t *plenp;
792 u_int32_t *rtalertp;
793 struct mbuf **mp;
794 int *offp;
795 {
796 struct mbuf *m = *mp;
797 int off = *offp, hbhlen;
798 struct ip6_hbh *hbh;
799 u_int8_t *opt;
800
801
802 IP6_EXTHDR_GET(hbh, struct ip6_hbh *, m,
803 sizeof(struct ip6_hdr), sizeof(struct ip6_hbh));
804 if (hbh == NULL) {
805 ip6stat.ip6s_tooshort++;
806 return -1;
807 }
808 hbhlen = (hbh->ip6h_len + 1) << 3;
809 IP6_EXTHDR_GET(hbh, struct ip6_hbh *, m, sizeof(struct ip6_hdr),
810 hbhlen);
811 if (hbh == NULL) {
812 ip6stat.ip6s_tooshort++;
813 return -1;
814 }
815 off += hbhlen;
816 hbhlen -= sizeof(struct ip6_hbh);
817 opt = (u_int8_t *)hbh + sizeof(struct ip6_hbh);
818
819 if (ip6_process_hopopts(m, (u_int8_t *)hbh + sizeof(struct ip6_hbh),
820 hbhlen, rtalertp, plenp) < 0)
821 return (-1);
822
823 *offp = off;
824 *mp = m;
825 return (0);
826 }
827
828
829
830
831
832
833
834
835
836
837
838 int
839 ip6_process_hopopts(m, opthead, hbhlen, rtalertp, plenp)
840 struct mbuf *m;
841 u_int8_t *opthead;
842 int hbhlen;
843 u_int32_t *rtalertp;
844 u_int32_t *plenp;
845 {
846 struct ip6_hdr *ip6;
847 int optlen = 0;
848 u_int8_t *opt = opthead;
849 u_int16_t rtalert_val;
850 u_int32_t jumboplen;
851 const int erroff = sizeof(struct ip6_hdr) + sizeof(struct ip6_hbh);
852
853 for (; hbhlen > 0; hbhlen -= optlen, opt += optlen) {
854 switch (*opt) {
855 case IP6OPT_PAD1:
856 optlen = 1;
857 break;
858 case IP6OPT_PADN:
859 if (hbhlen < IP6OPT_MINLEN) {
860 ip6stat.ip6s_toosmall++;
861 goto bad;
862 }
863 optlen = *(opt + 1) + 2;
864 break;
865 case IP6OPT_ROUTER_ALERT:
866
867 if (hbhlen < IP6OPT_RTALERT_LEN) {
868 ip6stat.ip6s_toosmall++;
869 goto bad;
870 }
871 if (*(opt + 1) != IP6OPT_RTALERT_LEN - 2) {
872
873 icmp6_error(m, ICMP6_PARAM_PROB,
874 ICMP6_PARAMPROB_HEADER,
875 erroff + opt + 1 - opthead);
876 return (-1);
877 }
878 optlen = IP6OPT_RTALERT_LEN;
879 bcopy((caddr_t)(opt + 2), (caddr_t)&rtalert_val, 2);
880 *rtalertp = ntohs(rtalert_val);
881 break;
882 case IP6OPT_JUMBO:
883
884 if (hbhlen < IP6OPT_JUMBO_LEN) {
885 ip6stat.ip6s_toosmall++;
886 goto bad;
887 }
888 if (*(opt + 1) != IP6OPT_JUMBO_LEN - 2) {
889
890 icmp6_error(m, ICMP6_PARAM_PROB,
891 ICMP6_PARAMPROB_HEADER,
892 erroff + opt + 1 - opthead);
893 return (-1);
894 }
895 optlen = IP6OPT_JUMBO_LEN;
896
897
898
899
900
901 ip6 = mtod(m, struct ip6_hdr *);
902 if (ip6->ip6_plen) {
903 ip6stat.ip6s_badoptions++;
904 icmp6_error(m, ICMP6_PARAM_PROB,
905 ICMP6_PARAMPROB_HEADER,
906 erroff + opt - opthead);
907 return (-1);
908 }
909
910
911
912
913
914 bcopy(opt + 2, &jumboplen, sizeof(jumboplen));
915 jumboplen = (u_int32_t)htonl(jumboplen);
916
917 #if 1
918
919
920
921
922
923
924
925
926 if (*plenp != 0) {
927 ip6stat.ip6s_badoptions++;
928 icmp6_error(m, ICMP6_PARAM_PROB,
929 ICMP6_PARAMPROB_HEADER,
930 erroff + opt + 2 - opthead);
931 return (-1);
932 }
933 #endif
934
935
936
937
938 if (jumboplen <= IPV6_MAXPACKET) {
939 ip6stat.ip6s_badoptions++;
940 icmp6_error(m, ICMP6_PARAM_PROB,
941 ICMP6_PARAMPROB_HEADER,
942 erroff + opt + 2 - opthead);
943 return (-1);
944 }
945 *plenp = jumboplen;
946
947 break;
948 default:
949 if (hbhlen < IP6OPT_MINLEN) {
950 ip6stat.ip6s_toosmall++;
951 goto bad;
952 }
953 optlen = ip6_unknown_opt(opt, m,
954 erroff + opt - opthead);
955 if (optlen == -1)
956 return (-1);
957 optlen += 2;
958 break;
959 }
960 }
961
962 return (0);
963
964 bad:
965 m_freem(m);
966 return (-1);
967 }
968
969
970
971
972
973
974
975 int
976 ip6_unknown_opt(optp, m, off)
977 u_int8_t *optp;
978 struct mbuf *m;
979 int off;
980 {
981 struct ip6_hdr *ip6;
982
983 switch (IP6OPT_TYPE(*optp)) {
984 case IP6OPT_TYPE_SKIP:
985 return ((int)*(optp + 1));
986 case IP6OPT_TYPE_DISCARD:
987 m_freem(m);
988 return (-1);
989 case IP6OPT_TYPE_FORCEICMP:
990 ip6stat.ip6s_badoptions++;
991 icmp6_error(m, ICMP6_PARAM_PROB, ICMP6_PARAMPROB_OPTION, off);
992 return (-1);
993 case IP6OPT_TYPE_ICMP:
994 ip6stat.ip6s_badoptions++;
995 ip6 = mtod(m, struct ip6_hdr *);
996 if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst) ||
997 (m->m_flags & (M_BCAST|M_MCAST)))
998 m_freem(m);
999 else
1000 icmp6_error(m, ICMP6_PARAM_PROB,
1001 ICMP6_PARAMPROB_OPTION, off);
1002 return (-1);
1003 }
1004
1005 m_freem(m);
1006 return (-1);
1007 }
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020 void
1021 ip6_savecontrol(in6p, m, mp)
1022 struct inpcb *in6p;
1023 struct mbuf *m;
1024 struct mbuf **mp;
1025 {
1026 #define IS2292(x, y) ((in6p->in6p_flags & IN6P_RFC2292) ? (x) : (y))
1027 # define in6p_flags inp_flags
1028 struct ip6_hdr *ip6 = mtod(m, struct ip6_hdr *);
1029
1030 #ifdef SO_TIMESTAMP
1031 if (in6p->inp_socket->so_options & SO_TIMESTAMP) {
1032 struct timeval tv;
1033
1034 microtime(&tv);
1035 *mp = sbcreatecontrol((caddr_t) &tv, sizeof(tv),
1036 SCM_TIMESTAMP, SOL_SOCKET);
1037 if (*mp)
1038 mp = &(*mp)->m_next;
1039 }
1040 #endif
1041
1042
1043 if ((in6p->in6p_flags & IN6P_PKTINFO) != 0) {
1044 struct in6_pktinfo pi6;
1045 bcopy(&ip6->ip6_dst, &pi6.ipi6_addr, sizeof(struct in6_addr));
1046 if (IN6_IS_SCOPE_EMBED(&pi6.ipi6_addr))
1047 pi6.ipi6_addr.s6_addr16[1] = 0;
1048 pi6.ipi6_ifindex =
1049 (m && m->m_pkthdr.rcvif) ? m->m_pkthdr.rcvif->if_index : 0;
1050 *mp = sbcreatecontrol((caddr_t) &pi6,
1051 sizeof(struct in6_pktinfo),
1052 IS2292(IPV6_2292PKTINFO, IPV6_PKTINFO), IPPROTO_IPV6);
1053 if (*mp)
1054 mp = &(*mp)->m_next;
1055 }
1056
1057 if ((in6p->in6p_flags & IN6P_HOPLIMIT) != 0) {
1058 int hlim = ip6->ip6_hlim & 0xff;
1059 *mp = sbcreatecontrol((caddr_t) &hlim, sizeof(int),
1060 IS2292(IPV6_2292HOPLIMIT, IPV6_HOPLIMIT), IPPROTO_IPV6);
1061 if (*mp)
1062 mp = &(*mp)->m_next;
1063 }
1064
1065 if ((in6p->in6p_flags & IN6P_TCLASS) != 0) {
1066 u_int32_t flowinfo;
1067 int tclass;
1068
1069 flowinfo = (u_int32_t)ntohl(ip6->ip6_flow & IPV6_FLOWINFO_MASK);
1070 flowinfo >>= 20;
1071
1072 tclass = flowinfo & 0xff;
1073 *mp = sbcreatecontrol((caddr_t)&tclass, sizeof(tclass),
1074 IPV6_TCLASS, IPPROTO_IPV6);
1075 if (*mp)
1076 mp = &(*mp)->m_next;
1077 }
1078
1079
1080
1081
1082
1083
1084
1085
1086 if ((in6p->in6p_flags & IN6P_HOPOPTS) != 0) {
1087
1088
1089
1090
1091
1092
1093
1094 struct ip6_hdr *ip6 = mtod(m, struct ip6_hdr *);
1095 if (ip6->ip6_nxt == IPPROTO_HOPOPTS) {
1096 struct ip6_hbh *hbh;
1097 int hbhlen = 0;
1098 struct mbuf *ext;
1099
1100 ext = ip6_pullexthdr(m, sizeof(struct ip6_hdr),
1101 ip6->ip6_nxt);
1102 if (ext == NULL) {
1103 ip6stat.ip6s_tooshort++;
1104 return;
1105 }
1106 hbh = mtod(ext, struct ip6_hbh *);
1107 hbhlen = (hbh->ip6h_len + 1) << 3;
1108 if (hbhlen != ext->m_len) {
1109 m_freem(ext);
1110 ip6stat.ip6s_tooshort++;
1111 return;
1112 }
1113
1114
1115
1116
1117
1118
1119
1120
1121 *mp = sbcreatecontrol((caddr_t)hbh, hbhlen,
1122 IS2292(IPV6_2292HOPOPTS, IPV6_HOPOPTS),
1123 IPPROTO_IPV6);
1124 if (*mp)
1125 mp = &(*mp)->m_next;
1126 m_freem(ext);
1127 }
1128 }
1129
1130
1131 if ((in6p->in6p_flags & (IN6P_RTHDR | IN6P_DSTOPTS)) != 0) {
1132 struct ip6_hdr *ip6 = mtod(m, struct ip6_hdr *);
1133 int nxt = ip6->ip6_nxt, off = sizeof(struct ip6_hdr);
1134
1135
1136
1137
1138
1139
1140
1141
1142 while (1) {
1143 struct ip6_ext *ip6e = NULL;
1144 int elen;
1145 struct mbuf *ext = NULL;
1146
1147
1148
1149
1150
1151 switch (nxt) {
1152 case IPPROTO_DSTOPTS:
1153 case IPPROTO_ROUTING:
1154 case IPPROTO_HOPOPTS:
1155 case IPPROTO_AH:
1156 break;
1157 default:
1158 goto loopend;
1159 }
1160
1161 ext = ip6_pullexthdr(m, off, nxt);
1162 if (ext == NULL) {
1163 ip6stat.ip6s_tooshort++;
1164 return;
1165 }
1166 ip6e = mtod(ext, struct ip6_ext *);
1167 if (nxt == IPPROTO_AH)
1168 elen = (ip6e->ip6e_len + 2) << 2;
1169 else
1170 elen = (ip6e->ip6e_len + 1) << 3;
1171 if (elen != ext->m_len) {
1172 m_freem(ext);
1173 ip6stat.ip6s_tooshort++;
1174 return;
1175 }
1176
1177 switch (nxt) {
1178 case IPPROTO_DSTOPTS:
1179 if (!(in6p->in6p_flags & IN6P_DSTOPTS))
1180 break;
1181
1182 *mp = sbcreatecontrol((caddr_t)ip6e, elen,
1183 IS2292(IPV6_2292DSTOPTS, IPV6_DSTOPTS),
1184 IPPROTO_IPV6);
1185 if (*mp)
1186 mp = &(*mp)->m_next;
1187 break;
1188
1189 case IPPROTO_ROUTING:
1190 if (!(in6p->in6p_flags & IN6P_RTHDR))
1191 break;
1192
1193 *mp = sbcreatecontrol((caddr_t)ip6e, elen,
1194 IS2292(IPV6_2292RTHDR, IPV6_RTHDR),
1195 IPPROTO_IPV6);
1196 if (*mp)
1197 mp = &(*mp)->m_next;
1198 break;
1199
1200 case IPPROTO_HOPOPTS:
1201 case IPPROTO_AH:
1202 break;
1203
1204 default:
1205
1206
1207
1208
1209
1210
1211 m_freem(ext);
1212 goto loopend;
1213
1214 }
1215
1216
1217 off += elen;
1218 nxt = ip6e->ip6e_nxt;
1219 ip6e = NULL;
1220 m_freem(ext);
1221 ext = NULL;
1222 }
1223 loopend:
1224 ;
1225 }
1226 # undef in6p_flags
1227 }
1228
1229
1230
1231
1232
1233 static struct mbuf *
1234 ip6_pullexthdr(m, off, nxt)
1235 struct mbuf *m;
1236 size_t off;
1237 int nxt;
1238 {
1239 struct ip6_ext ip6e;
1240 size_t elen;
1241 struct mbuf *n;
1242
1243 #ifdef DIAGNOSTIC
1244 switch (nxt) {
1245 case IPPROTO_DSTOPTS:
1246 case IPPROTO_ROUTING:
1247 case IPPROTO_HOPOPTS:
1248 case IPPROTO_AH:
1249 break;
1250 default:
1251 printf("ip6_pullexthdr: invalid nxt=%d\n", nxt);
1252 }
1253 #endif
1254
1255 m_copydata(m, off, sizeof(ip6e), (caddr_t)&ip6e);
1256 if (nxt == IPPROTO_AH)
1257 elen = (ip6e.ip6e_len + 2) << 2;
1258 else
1259 elen = (ip6e.ip6e_len + 1) << 3;
1260
1261 MGET(n, M_DONTWAIT, MT_DATA);
1262 if (n && elen >= MLEN) {
1263 MCLGET(n, M_DONTWAIT);
1264 if ((n->m_flags & M_EXT) == 0) {
1265 m_free(n);
1266 n = NULL;
1267 }
1268 }
1269 if (!n)
1270 return NULL;
1271
1272 n->m_len = 0;
1273 if (elen >= M_TRAILINGSPACE(n)) {
1274 m_free(n);
1275 return NULL;
1276 }
1277
1278 m_copydata(m, off, elen, mtod(n, caddr_t));
1279 n->m_len = elen;
1280 return n;
1281 }
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295 u_int8_t *
1296 ip6_get_prevhdr(m, off)
1297 struct mbuf *m;
1298 int off;
1299 {
1300 struct ip6_hdr *ip6 = mtod(m, struct ip6_hdr *);
1301
1302 if (off == sizeof(struct ip6_hdr))
1303 return (&ip6->ip6_nxt);
1304 else {
1305 int len, nxt;
1306 struct ip6_ext *ip6e = NULL;
1307
1308 nxt = ip6->ip6_nxt;
1309 len = sizeof(struct ip6_hdr);
1310 while (len < off) {
1311 ip6e = (struct ip6_ext *)(mtod(m, caddr_t) + len);
1312
1313 switch (nxt) {
1314 case IPPROTO_FRAGMENT:
1315 len += sizeof(struct ip6_frag);
1316 break;
1317 case IPPROTO_AH:
1318 len += (ip6e->ip6e_len + 2) << 2;
1319 break;
1320 default:
1321 len += (ip6e->ip6e_len + 1) << 3;
1322 break;
1323 }
1324 nxt = ip6e->ip6e_nxt;
1325 }
1326 if (ip6e)
1327 return (&ip6e->ip6e_nxt);
1328 else
1329 return NULL;
1330 }
1331 }
1332
1333
1334
1335
1336 int
1337 ip6_nexthdr(m, off, proto, nxtp)
1338 struct mbuf *m;
1339 int off;
1340 int proto;
1341 int *nxtp;
1342 {
1343 struct ip6_hdr ip6;
1344 struct ip6_ext ip6e;
1345 struct ip6_frag fh;
1346
1347
1348 if (m == NULL)
1349 panic("ip6_nexthdr: m == NULL");
1350 if ((m->m_flags & M_PKTHDR) == 0 || m->m_pkthdr.len < off)
1351 return -1;
1352
1353 switch (proto) {
1354 case IPPROTO_IPV6:
1355 if (m->m_pkthdr.len < off + sizeof(ip6))
1356 return -1;
1357 m_copydata(m, off, sizeof(ip6), (caddr_t)&ip6);
1358 if (nxtp)
1359 *nxtp = ip6.ip6_nxt;
1360 off += sizeof(ip6);
1361 return off;
1362
1363 case IPPROTO_FRAGMENT:
1364
1365
1366
1367
1368 if (m->m_pkthdr.len < off + sizeof(fh))
1369 return -1;
1370 m_copydata(m, off, sizeof(fh), (caddr_t)&fh);
1371 if ((fh.ip6f_offlg & IP6F_OFF_MASK) != 0)
1372 return -1;
1373 if (nxtp)
1374 *nxtp = fh.ip6f_nxt;
1375 off += sizeof(struct ip6_frag);
1376 return off;
1377
1378 case IPPROTO_AH:
1379 if (m->m_pkthdr.len < off + sizeof(ip6e))
1380 return -1;
1381 m_copydata(m, off, sizeof(ip6e), (caddr_t)&ip6e);
1382 if (nxtp)
1383 *nxtp = ip6e.ip6e_nxt;
1384 off += (ip6e.ip6e_len + 2) << 2;
1385 if (m->m_pkthdr.len < off)
1386 return -1;
1387 return off;
1388
1389 case IPPROTO_HOPOPTS:
1390 case IPPROTO_ROUTING:
1391 case IPPROTO_DSTOPTS:
1392 if (m->m_pkthdr.len < off + sizeof(ip6e))
1393 return -1;
1394 m_copydata(m, off, sizeof(ip6e), (caddr_t)&ip6e);
1395 if (nxtp)
1396 *nxtp = ip6e.ip6e_nxt;
1397 off += (ip6e.ip6e_len + 1) << 3;
1398 if (m->m_pkthdr.len < off)
1399 return -1;
1400 return off;
1401
1402 case IPPROTO_NONE:
1403 case IPPROTO_ESP:
1404 case IPPROTO_IPCOMP:
1405
1406 return -1;
1407
1408 default:
1409 return -1;
1410 }
1411
1412 return -1;
1413 }
1414
1415
1416
1417
1418 int
1419 ip6_lasthdr(m, off, proto, nxtp)
1420 struct mbuf *m;
1421 int off;
1422 int proto;
1423 int *nxtp;
1424 {
1425 int newoff;
1426 int nxt;
1427
1428 if (!nxtp) {
1429 nxt = -1;
1430 nxtp = &nxt;
1431 }
1432 while (1) {
1433 newoff = ip6_nexthdr(m, off, proto, nxtp);
1434 if (newoff < 0)
1435 return off;
1436 else if (newoff < off)
1437 return -1;
1438 else if (newoff == off)
1439 return newoff;
1440
1441 off = newoff;
1442 proto = *nxtp;
1443 }
1444 }
1445
1446
1447
1448
1449
1450 u_char inet6ctlerrmap[PRC_NCMDS] = {
1451 0, 0, 0, 0,
1452 0, EMSGSIZE, EHOSTDOWN, EHOSTUNREACH,
1453 EHOSTUNREACH, EHOSTUNREACH, ECONNREFUSED, ECONNREFUSED,
1454 EMSGSIZE, EHOSTUNREACH, 0, 0,
1455 0, 0, 0, 0,
1456 ENOPROTOOPT
1457 };
1458
1459 #include <uvm/uvm_extern.h>
1460 #include <sys/sysctl.h>
1461
1462 int *ipv6ctl_vars[IPV6CTL_MAXID] = IPV6CTL_VARS;
1463
1464 int
1465 ip6_sysctl(name, namelen, oldp, oldlenp, newp, newlen)
1466 int *name;
1467 u_int namelen;
1468 void *oldp;
1469 size_t *oldlenp;
1470 void *newp;
1471 size_t newlen;
1472 {
1473
1474 if (namelen != 1)
1475 return ENOTDIR;
1476
1477 switch (name[0]) {
1478 case IPV6CTL_KAME_VERSION:
1479 return sysctl_rdstring(oldp, oldlenp, newp, __KAME_VERSION);
1480 case IPV6CTL_V6ONLY:
1481 return sysctl_rdint(oldp, oldlenp, newp, ip6_v6only);
1482 default:
1483 if (name[0] < IPV6CTL_MAXID)
1484 return (sysctl_int_arr(ipv6ctl_vars, name, namelen,
1485 oldp, oldlenp, newp, newlen));
1486 return (EOPNOTSUPP);
1487 }
1488
1489 }