ip6_output.c revision 1.1.2.1 1 /*
2 * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. Neither the name of the project nor the names of its contributors
14 * may be used to endorse or promote products derived from this software
15 * without specific prior written permission.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27 * SUCH DAMAGE.
28 */
29
30 /*
31 * Copyright (c) 1982, 1986, 1988, 1990, 1993
32 * The Regents of the University of California. All rights reserved.
33 *
34 * Redistribution and use in source and binary forms, with or without
35 * modification, are permitted provided that the following conditions
36 * are met:
37 * 1. Redistributions of source code must retain the above copyright
38 * notice, this list of conditions and the following disclaimer.
39 * 2. Redistributions in binary form must reproduce the above copyright
40 * notice, this list of conditions and the following disclaimer in the
41 * documentation and/or other materials provided with the distribution.
42 * 3. All advertising materials mentioning features or use of this software
43 * must display the following acknowledgement:
44 * This product includes software developed by the University of
45 * California, Berkeley and its contributors.
46 * 4. Neither the name of the University nor the names of its contributors
47 * may be used to endorse or promote products derived from this software
48 * without specific prior written permission.
49 *
50 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
51 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
52 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
53 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
54 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
55 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
56 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
57 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
58 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
59 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
60 * SUCH DAMAGE.
61 *
62 * @(#)ip_output.c 8.3 (Berkeley) 1/21/94
63 */
64
65 #ifdef __FreeBSD__
66 #include "opt_ip6fw.h"
67 #endif
68 #if (defined(__FreeBSD__) && __FreeBSD__ >= 3) || defined(__NetBSD__)
69 #include "opt_inet.h"
70 #endif
71
72 #include <sys/param.h>
73 #include <sys/malloc.h>
74 #include <sys/mbuf.h>
75 #include <sys/errno.h>
76 #include <sys/protosw.h>
77 #include <sys/socket.h>
78 #include <sys/socketvar.h>
79 #include <sys/systm.h>
80 #include <sys/proc.h>
81
82 #include <net/if.h>
83 #include <net/route.h>
84
85 #include <netinet/in.h>
86 #include <netinet/in_var.h>
87 #include <netinet6/in6_systm.h>
88 #include <netinet6/ip6.h>
89 #include <netinet6/icmp6.h>
90 #if !defined(__FreeBSD__) || __FreeBSD__ < 3
91 #include <netinet6/in6_pcb.h>
92 #else
93 #include <netinet/in_pcb.h>
94 #endif
95 #include <netinet6/ip6_var.h>
96 #include <netinet6/nd6.h>
97
98 #ifdef IPSEC
99 #include <netinet6/ipsec.h>
100 #include <netkey/key.h>
101 #include <netkey/key_debug.h>
102 #endif /* IPSEC */
103
104 #include "loop.h"
105
106 struct ip6_exthdrs {
107 struct mbuf *ip6e_ip6;
108 struct mbuf *ip6e_hbh;
109 struct mbuf *ip6e_dest1;
110 struct mbuf *ip6e_rthdr;
111 struct mbuf *ip6e_dest2;
112 };
113
114 static int ip6_pcbopts __P((struct ip6_pktopts **, struct mbuf *,
115 struct socket *));
116 static int ip6_setmoptions __P((int, struct ip6_moptions **, struct mbuf *));
117 static int ip6_getmoptions __P((int, struct ip6_moptions *, struct mbuf **));
118 static int ip6_copyexthdr __P((struct mbuf **, caddr_t, int));
119 static int ip6_insertfraghdr __P((struct mbuf *, struct mbuf *, int,
120 struct ip6_frag **));
121 static int ip6_insert_jumboopt __P((struct ip6_exthdrs *, u_int32_t));
122 static int ip6_splithdr __P((struct mbuf *, struct ip6_exthdrs *));
123 #ifdef __bsdi__
124 extern struct ifnet loif;
125 #endif
126
127 #ifdef __NetBSD__
128 extern struct ifnet **ifindex2ifnet;
129 extern struct ifnet loif[NLOOP];
130 #endif
131
132 /*
133 * IP6 output. The packet in mbuf chain m contains a skeletal IP6
134 * header (with pri, len, nxt, hlim, src, dst).
135 * This function may modify ver and hlim only.
136 * The mbuf chain containing the packet will be freed.
137 * The mbuf opt, if present, will not be freed.
138 */
139 int
140 ip6_output(m0, opt, ro, flags, im6o)
141 struct mbuf *m0;
142 struct ip6_pktopts *opt;
143 struct route_in6 *ro;
144 int flags;
145 struct ip6_moptions *im6o;
146 {
147 struct ip6_hdr *ip6, *mhip6;
148 struct ifnet *ifp;
149 struct mbuf *m = m0;
150 int hlen, tlen, len, off;
151 struct route_in6 ip6route;
152 struct sockaddr_in6 *dst;
153 int error = 0;
154 struct in6_ifaddr *ia;
155 u_long mtu;
156 u_int32_t optlen = 0, plen = 0, unfragpartlen = 0;
157 struct ip6_exthdrs exthdrs;
158 struct in6_addr finaldst;
159 struct route_in6 *ro_pmtu = NULL;
160 int hdrsplit = 0;
161 int needipsec = 0;
162 #ifdef IPSEC
163 int needipsectun = 0;
164 struct socket *so;
165 struct secpolicy *sp = NULL;
166
167 /* for AH processing. stupid to have "socket" variable in IP layer... */
168 so = (struct socket *)m->m_pkthdr.rcvif;
169 m->m_pkthdr.rcvif = NULL;
170 ip6 = mtod(m, struct ip6_hdr *);
171 #endif /* IPSEC */
172
173 #define MAKE_EXTHDR(hp,mp) \
174 { \
175 if (hp) { \
176 struct ip6_ext *eh = (struct ip6_ext *)(hp); \
177 error = ip6_copyexthdr((mp), (caddr_t)(hp), \
178 ((eh)->ip6e_len + 1) << 3); \
179 if (error) \
180 goto freehdrs; \
181 } \
182 }
183
184 bzero(&exthdrs, sizeof(exthdrs));
185 if (opt) {
186 /* Hop-by-Hop options header */
187 MAKE_EXTHDR(opt->ip6po_hbh, &exthdrs.ip6e_hbh);
188 /* Destination options header(1st part) */
189 MAKE_EXTHDR(opt->ip6po_dest1, &exthdrs.ip6e_dest1);
190 /* Routing header */
191 MAKE_EXTHDR(opt->ip6po_rthdr, &exthdrs.ip6e_rthdr);
192 /* Destination options header(2nd part) */
193 MAKE_EXTHDR(opt->ip6po_dest2, &exthdrs.ip6e_dest2);
194 }
195
196 #ifdef IPSEC
197 /* get a security policy for this packet */
198 if (so == NULL)
199 sp = ipsec6_getpolicybyaddr(m, 0, &error);
200 else
201 sp = ipsec6_getpolicybysock(m, so, &error);
202
203 if (sp == NULL) {
204 ipsec6stat.out_inval++;
205 goto bad;
206 }
207
208 error = 0;
209
210 /* check policy */
211 switch (sp->policy) {
212 case IPSEC_POLICY_DISCARD:
213 /*
214 * This packet is just discarded.
215 */
216 ipsec6stat.out_polvio++;
217 goto bad;
218
219 case IPSEC_POLICY_BYPASS:
220 case IPSEC_POLICY_NONE:
221 /* no need to do IPsec. */
222 needipsec = 0;
223 break;
224
225 case IPSEC_POLICY_IPSEC:
226 if (sp->req == NULL) {
227 /* XXX should be panic ? */
228 printf("ip6_output: No IPsec request specified.\n");
229 error = EINVAL;
230 goto bad;
231 }
232 needipsec = 1;
233 break;
234
235 case IPSEC_POLICY_ENTRUST:
236 default:
237 printf("ip6_output: Invalid policy found. %d\n", sp->policy);
238 }
239 #endif /* IPSEC */
240
241 /*
242 * Calculate the total length of the extension header chain.
243 * Keep the length of the unfragmentable part for fragmentation.
244 */
245 if (exthdrs.ip6e_hbh) optlen += exthdrs.ip6e_hbh->m_len;
246 if (exthdrs.ip6e_dest1) optlen += exthdrs.ip6e_dest1->m_len;
247 if (exthdrs.ip6e_rthdr) optlen += exthdrs.ip6e_rthdr->m_len;
248 unfragpartlen = plen + sizeof(struct ip6_hdr);
249 /* NOTE: we don't add AH/ESP length here. do that later. */
250 if (exthdrs.ip6e_dest2) optlen += exthdrs.ip6e_dest2->m_len;
251
252 /*
253 * If we need IPsec, or there is at least one extension header,
254 * separate IP6 header from the payload.
255 */
256 if ((needipsec || optlen) && !hdrsplit) {
257 if ((error = ip6_splithdr(m, &exthdrs)) != 0) {
258 m = NULL;
259 goto freehdrs;
260 }
261 m = exthdrs.ip6e_ip6;
262 hdrsplit++;
263 }
264
265 /* adjust pointer */
266 ip6 = mtod(m, struct ip6_hdr *);
267
268 /* adjust mbuf packet header length */
269 m->m_pkthdr.len += optlen;
270 plen = m->m_pkthdr.len - sizeof(*ip6);
271
272 /* If this is a jumbo payload, insert a jumbo payload option. */
273 if (plen > IPV6_MAXPACKET) {
274 if (!hdrsplit) {
275 if ((error = ip6_splithdr(m, &exthdrs)) != 0) {
276 m = NULL;
277 goto freehdrs;
278 }
279 m = exthdrs.ip6e_ip6;
280 hdrsplit++;
281 }
282 /* adjust pointer */
283 ip6 = mtod(m, struct ip6_hdr *);
284 if ((error = ip6_insert_jumboopt(&exthdrs, plen)) != 0)
285 goto freehdrs;
286 ip6->ip6_plen = 0;
287 } else
288 ip6->ip6_plen = htons(plen);
289
290 /*
291 * Concatenate headers and fill in next header fields.
292 * Here we have, on "m"
293 * IPv6 payload or
294 * IPv6 [esp* dest2 payload]
295 * and we insert headers accordingly. Finally, we should be getting:
296 * IPv6 hbh dest1 rthdr ah* dest2 payload or
297 * IPv6 hbh dest1 rthdr ah* [esp* dest2 payload]
298 */
299 {
300 u_char *nexthdrp = &ip6->ip6_nxt;
301 struct mbuf *mprev = m;
302
303 /*
304 * we treat dest2 specially. this makes IPsec processing
305 * much easier.
306 */
307 if (exthdrs.ip6e_dest2) {
308 if (!hdrsplit)
309 panic("assumption failed: hdr not split");
310 *mtod(exthdrs.ip6e_dest2, u_char *) = ip6->ip6_nxt;
311 ip6->ip6_nxt = IPPROTO_DSTOPTS;
312 }
313
314 #define MAKE_CHAIN(m,mp,p,i)\
315 {\
316 if (m) {\
317 if (!hdrsplit) \
318 panic("assumption failed: hdr not split"); \
319 *mtod((m), u_char *) = *(p);\
320 *(p) = (i);\
321 p = mtod((m), u_char *);\
322 (m)->m_next = (mp)->m_next;\
323 (mp)->m_next = (m);\
324 (mp) = (m);\
325 }\
326 }
327 MAKE_CHAIN(exthdrs.ip6e_hbh, mprev,
328 nexthdrp, IPPROTO_HOPOPTS);
329 MAKE_CHAIN(exthdrs.ip6e_dest1, mprev,
330 nexthdrp, IPPROTO_DSTOPTS);
331 MAKE_CHAIN(exthdrs.ip6e_rthdr, mprev,
332 nexthdrp, IPPROTO_ROUTING);
333
334 #ifdef IPSEC
335 if (!needipsec)
336 goto skip_ipsec2;
337
338 /*
339 * pointers after IPsec headers are not valid any more.
340 * other pointers need a great care too.
341 * (IPsec routines should not mangle mbufs prior to AH/ESP)
342 */
343 exthdrs.ip6e_dest2 = NULL;
344
345 {
346 struct ip6_rthdr *rh = NULL;
347 int segleft_org = 0;
348 struct ipsec_output_state state;
349
350 if (exthdrs.ip6e_rthdr) {
351 rh = mtod(exthdrs.ip6e_rthdr, struct ip6_rthdr *);
352 segleft_org = rh->ip6r_segleft;
353 rh->ip6r_segleft = 0;
354 }
355
356 bzero(&state, sizeof(state));
357 state.m = m;
358 error = ipsec6_output_trans(&state, nexthdrp, mprev, sp, flags,
359 &needipsectun);
360 m = state.m;
361 if (error) {
362 /* mbuf is already reclaimed in ipsec6_output_trans. */
363 m = NULL;
364 switch (error) {
365 case EHOSTUNREACH:
366 case ENETUNREACH:
367 case EMSGSIZE:
368 case ENOBUFS:
369 case ENOMEM:
370 break;
371 default:
372 printf("ip6_output (ipsec): error code %d\n", error);
373 /*fall through*/
374 case ENOENT:
375 /* don't show these error codes to the user */
376 error = 0;
377 break;
378 }
379 goto bad;
380 }
381 if (exthdrs.ip6e_rthdr) {
382 /* ah6_output doesn't modify mbuf chain */
383 rh->ip6r_segleft = segleft_org;
384 }
385 }
386 skip_ipsec2:;
387 #endif
388 }
389
390 /*
391 * If there is a routing header, replace destination address field
392 * with the first hop of the routing header.
393 */
394 if (exthdrs.ip6e_rthdr) {
395 struct ip6_rthdr *rh =
396 (struct ip6_rthdr *)(mtod(exthdrs.ip6e_rthdr,
397 struct ip6_rthdr *));
398 struct ip6_rthdr0 *rh0;
399
400 finaldst = ip6->ip6_dst;
401 switch(rh->ip6r_type) {
402 case IPV6_RTHDR_TYPE_0:
403 rh0 = (struct ip6_rthdr0 *)rh;
404 ip6->ip6_dst = rh0->ip6r0_addr[0];
405 bcopy((caddr_t)&rh0->ip6r0_addr[1],
406 (caddr_t)&rh0->ip6r0_addr[0],
407 sizeof(struct in6_addr)*(rh0->ip6r0_segleft - 1)
408 );
409 rh0->ip6r0_addr[rh0->ip6r0_segleft - 1] = finaldst;
410 break;
411 default: /* is it possible? */
412 error = EINVAL;
413 goto bad;
414 }
415 }
416
417 /* Source address validation */
418 if (IN6_IS_ADDR_UNSPECIFIED(&ip6->ip6_src) &&
419 (flags & IPV6_DADOUTPUT) == 0) {
420 error = EOPNOTSUPP;
421 ip6stat.ip6s_badscope++;
422 goto bad;
423 }
424 if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_src)) {
425 error = EOPNOTSUPP;
426 ip6stat.ip6s_badscope++;
427 goto bad;
428 }
429
430 ip6stat.ip6s_localout++;
431
432 /*
433 * Route packet.
434 */
435 if (ro == 0) {
436 ro = &ip6route;
437 bzero((caddr_t)ro, sizeof(*ro));
438 }
439 ro_pmtu = ro;
440 if (opt && opt->ip6po_rthdr)
441 ro = &opt->ip6po_route;
442 dst = (struct sockaddr_in6 *)&ro->ro_dst;
443 /*
444 * If there is a cached route,
445 * check that it is to the same destination
446 * and is still up. If not, free it and try again.
447 */
448 if (ro->ro_rt && ((ro->ro_rt->rt_flags & RTF_UP) == 0 ||
449 !IN6_ARE_ADDR_EQUAL(&dst->sin6_addr, &ip6->ip6_dst))) {
450 RTFREE(ro->ro_rt);
451 ro->ro_rt = (struct rtentry *)0;
452 }
453 if (ro->ro_rt == 0) {
454 bzero(dst, sizeof(*dst));
455 dst->sin6_family = AF_INET6;
456 dst->sin6_len = sizeof(struct sockaddr_in6);
457 dst->sin6_addr = ip6->ip6_dst;
458 }
459 #ifdef IPSEC
460 if (needipsec && needipsectun) {
461 struct ipsec_output_state state;
462
463 /*
464 * All the extension headers will become inaccessible
465 * (since they can be encrypted).
466 * Don't panic, we need no more updates to extension headers
467 * on inner IPv6 packet (since they are now encapsulated).
468 *
469 * IPv6 [ESP|AH] IPv6 [extension headers] payload
470 */
471 bzero(&exthdrs, sizeof(exthdrs));
472 exthdrs.ip6e_ip6 = m;
473
474 bzero(&state, sizeof(state));
475 state.m = m;
476 state.ro = (struct route *)ro;
477 state.dst = (struct sockaddr *)dst;
478
479 error = ipsec6_output_tunnel(&state, sp, flags);
480
481 m = state.m;
482 ro = (struct route_in6 *)state.ro;
483 dst = (struct sockaddr_in6 *)state.dst;
484 if (error) {
485 /* mbuf is already reclaimed in ipsec6_output_tunnel. */
486 m0 = m = NULL;
487 m = NULL;
488 switch (error) {
489 case EHOSTUNREACH:
490 case ENETUNREACH:
491 case EMSGSIZE:
492 case ENOBUFS:
493 case ENOMEM:
494 break;
495 default:
496 printf("ip6_output (ipsec): error code %d\n", error);
497 /*fall through*/
498 case ENOENT:
499 /* don't show these error codes to the user */
500 error = 0;
501 break;
502 }
503 goto bad;
504 }
505
506 exthdrs.ip6e_ip6 = m;
507 }
508 #endif /*IPESC*/
509
510 if (!IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst)) {
511 /* Unicast */
512
513 #define ifatoia6(ifa) ((struct in6_ifaddr *)(ifa))
514 #define sin6tosa(sin6) ((struct sockaddr *)(sin6))
515 /* xxx
516 * interface selection comes here
517 * if an interface is specified from an upper layer,
518 * ifp must point it.
519 */
520 if (ro->ro_rt == 0) {
521 if (ro == &ip6route) /* xxx kazu */
522 rtalloc((struct route *)ro);
523 else
524 rtcalloc((struct route *)ro);
525 }
526 if (ro->ro_rt == 0) {
527 ip6stat.ip6s_noroute++;
528 error = EHOSTUNREACH;
529 goto bad;
530 }
531 ia = ifatoia6(ro->ro_rt->rt_ifa);
532 ifp = ro->ro_rt->rt_ifp;
533 ro->ro_rt->rt_use++;
534 if (ro->ro_rt->rt_flags & RTF_GATEWAY)
535 dst = (struct sockaddr_in6 *)ro->ro_rt->rt_gateway;
536 m->m_flags &= ~(M_BCAST | M_MCAST); /* just in case */
537
538 /*
539 * Check if there is the outgoing interface conflicts with
540 * the interface specified by ifi6_ifindex(if specified).
541 * Note that loopback interface is always okay.
542 * (this happens when we are sending packet toward my
543 * interface)
544 */
545 if (opt && opt->ip6po_pktinfo
546 && opt->ip6po_pktinfo->ipi6_ifindex) {
547 if (!(ifp->if_flags & IFF_LOOPBACK)
548 && ifp->if_index != opt->ip6po_pktinfo->ipi6_ifindex) {
549 ip6stat.ip6s_noroute++;
550 error = EHOSTUNREACH;
551 goto bad;
552 }
553 }
554
555 if (opt && opt->ip6po_hlim != -1)
556 ip6->ip6_hlim = opt->ip6po_hlim & 0xff;
557 } else {
558 /* Multicast */
559 struct in6_multi *in6m;
560
561 m->m_flags = (m->m_flags & ~M_BCAST) | M_MCAST;
562
563 /*
564 * See if the caller provided any multicast options
565 */
566 ifp = NULL;
567 if (im6o != NULL) {
568 ip6->ip6_hlim = im6o->im6o_multicast_hlim;
569 if (im6o->im6o_multicast_ifp != NULL)
570 ifp = im6o->im6o_multicast_ifp;
571 } else
572 ip6->ip6_hlim = ip6_defmcasthlim;
573
574 /*
575 * See if the caller provided the outgoing interface
576 * as an ancillary data.
577 * Boundary check for ifindex is assumed to be already done.
578 */
579 if (opt && opt->ip6po_pktinfo && opt->ip6po_pktinfo->ipi6_ifindex)
580 ifp = ifindex2ifnet[opt->ip6po_pktinfo->ipi6_ifindex];
581
582 /*
583 * If the destination is a node-local scope multicast,
584 * the packet should be loop-backed only.
585 */
586 if (IN6_IS_ADDR_MC_NODELOCAL(&ip6->ip6_dst)) {
587 /*
588 * If the outgoing interface is already specified,
589 * it should be a loopback interface.
590 */
591 if (ifp && (ifp->if_flags & IFF_LOOPBACK) == 0) {
592 ip6stat.ip6s_badscope++;
593 error = ENETUNREACH; /* XXX: better error? */
594 goto bad;
595 }
596 else {
597 #ifdef __bsdi__
598 ifp = &loif;
599 #else
600 ifp = &loif[0];
601 #endif
602 }
603 }
604
605 if (opt && opt->ip6po_hlim != -1)
606 ip6->ip6_hlim = opt->ip6po_hlim & 0xff;
607
608 /*
609 * If caller did not provide an interface lookup a
610 * default in the routing table. This is either a
611 * default for the speicfied group (i.e. a host
612 * route), or a multicast default (a route for the
613 * ``net'' ff00::/8).
614 */
615 if (ifp == NULL) {
616 if (ro->ro_rt == 0) {
617 #ifdef __FreeBSD__
618 ro->ro_rt = rtalloc1((struct sockaddr *)
619 &ro->ro_dst, 0, 0UL);
620 #endif /*__FreeBSD__*/
621 #if defined(__bsdi__) || defined(__NetBSD__)
622 ro->ro_rt = rtalloc1((struct sockaddr *)
623 &ro->ro_dst, 0);
624 #endif /*__bsdi__*/
625 }
626 if (ro->ro_rt == 0) {
627 ip6stat.ip6s_noroute++;
628 error = EHOSTUNREACH;
629 goto bad;
630 }
631 ia = ifatoia6(ro->ro_rt->rt_ifa);
632 ifp = ro->ro_rt->rt_ifp;
633 ro->ro_rt->rt_use++;
634 }
635 /*
636 * Confirm that the outgoing interface supports multicast.
637 */
638 if ((ifp->if_flags & IFF_MULTICAST) == 0) {
639 ip6stat.ip6s_noroute++;
640 error = ENETUNREACH;
641 goto bad;
642 }
643 IN6_LOOKUP_MULTI(ip6->ip6_dst, ifp, in6m);
644 if (in6m != NULL &&
645 (im6o == NULL || im6o->im6o_multicast_loop)) {
646 /*
647 * If we belong to the destination multicast group
648 * on the outgoing interface, and the caller did not
649 * forbid loopback, loop back a copy.
650 */
651 ip6_mloopback(ifp, m, dst);
652 } else {
653 /*
654 * If we are acting as a multicast router, perform
655 * multicast forwarding as if the packet had just
656 * arrived on the interface to which we are about
657 * to send. The multicast forwarding function
658 * recursively calls this function, using the
659 * IPV6_FORWARDING flag to prevent infinite recursion.
660 *
661 * Multicasts that are looped back by ip6_mloopback(),
662 * above, will be forwarded by the ip6_input() routine,
663 * if necessary.
664 */
665 if (ip6_mrouter && (flags & IPV6_FORWARDING) == 0) {
666 if (ip6_mforward(ip6, ifp, m) != NULL) {
667 m_freem(m);
668 goto done;
669 }
670 }
671 }
672 /*
673 * Multicasts with a hoplimit of zero may be looped back,
674 * above, but must not be transmitted on a network.
675 * Also, multicasts addressed to the loopback interface
676 * are not sent -- the above call to ip6_mloopback() will
677 * loop back a copy if this host actually belongs to the
678 * destination group on the loopback interface.
679 */
680 if (ip6->ip6_hlim == 0 || (ifp->if_flags & IFF_LOOPBACK)) {
681 m_freem(m);
682 goto done;
683 }
684 }
685
686 /*
687 * Determine path MTU.
688 */
689 if (ro_pmtu != ro) {
690 /* The first hop and the final destination may differ. */
691 struct sockaddr_in6 *sin6_fin =
692 (struct sockaddr_in6 *)&ro_pmtu->ro_dst;
693 if (ro_pmtu->ro_rt && ((ro->ro_rt->rt_flags & RTF_UP) == 0 ||
694 !IN6_ARE_ADDR_EQUAL(&sin6_fin->sin6_addr,
695 &finaldst))) {
696 RTFREE(ro_pmtu->ro_rt);
697 ro_pmtu->ro_rt = (struct rtentry *)0;
698 }
699 if (ro_pmtu->ro_rt == 0) {
700 bzero(sin6_fin, sizeof(*sin6_fin));
701 sin6_fin->sin6_family = AF_INET6;
702 sin6_fin->sin6_len = sizeof(struct sockaddr_in6);
703 sin6_fin->sin6_addr = finaldst;
704
705 #if 0
706 rtcalloc((struct route *)ro_pmtu);
707 #else
708 rtalloc((struct route *)ro_pmtu);
709 #endif
710 }
711 }
712 if (ro_pmtu->ro_rt != NULL) {
713 u_int32_t ifmtu = nd_ifinfo[ifp->if_index].linkmtu;
714
715 mtu = ro_pmtu->ro_rt->rt_rmx.rmx_mtu;
716 if (mtu > ifmtu) {
717 /*
718 * The MTU on the route is larger than the MTU on
719 * the interface! This shouldn't happen, unless the
720 * MTU of the interface has been changed after the
721 * interface was brought up. Change the MTU in the
722 * route to match the interface MTU (as long as the
723 * field isn't locked).
724 */
725 mtu = ifmtu;
726 if ((ro_pmtu->ro_rt->rt_rmx.rmx_locks & RTV_MTU) == 0)
727 ro_pmtu->ro_rt->rt_rmx.rmx_mtu = mtu; /* XXX */
728 }
729 } else {
730 mtu = nd_ifinfo[ifp->if_index].linkmtu;
731 }
732
733 /*
734 * Fake link-local scope-class addresses
735 */
736 if ((ifp->if_flags & IFF_LOOPBACK) == 0) {
737 if (IN6_IS_SCOPE_LINKLOCAL(&ip6->ip6_src))
738 ip6->ip6_src.s6_addr16[1] = 0;
739 if (IN6_IS_SCOPE_LINKLOCAL(&ip6->ip6_dst))
740 ip6->ip6_dst.s6_addr16[1] = 0;
741 }
742
743 /*
744 * If the outgoing packet contains a hop-by-hop options header,
745 * it must be examined and processed even by the source node.
746 * (RFC 2460, section 4.)
747 */
748 if (exthdrs.ip6e_hbh) {
749 struct ip6_hbh *hbh = mtod(exthdrs.ip6e_hbh,
750 struct ip6_hbh *);
751 long dummy1; /* XXX unused */
752 u_int32_t dummy2; /* XXX unused */
753
754 /*
755 * XXX: if we have to send an ICMPv6 error to the sender,
756 * we need the M_LOOP flag since icmp6_error() expects
757 * the IPv6 and the hop-by-hop options header are
758 * continuous unless the flag is set.
759 */
760 m->m_flags |= M_LOOP;
761 m->m_pkthdr.rcvif = ifp;
762 if (ip6_process_hopopts(m,
763 (u_int8_t *)(hbh + 1),
764 ((hbh->ip6h_len + 1) << 3) -
765 sizeof(struct ip6_hbh),
766 &dummy1, &dummy2) < 0) {
767 /* m was already freed at this point */
768 error = EINVAL;/* better error? */
769 goto done;
770 }
771 m->m_flags &= ~M_LOOP; /* XXX */
772 m->m_pkthdr.rcvif = NULL;
773 }
774
775 /*
776 * Send the packet to the outgoing interface.
777 * If necessary, do IPv6 fragmentation before sending.
778 */
779 tlen = m->m_pkthdr.len;
780 if (tlen <= mtu
781 #ifdef notyet
782 /*
783 * On any link that cannot convey a 1280-octet packet in one piece,
784 * link-specific fragmentation and reassembly must be provided at
785 * a layer below IPv6. [RFC 2460, sec.5]
786 * Thus if the interface has ability of link-level fragmentation,
787 * we can just send the packet even if the packet size is
788 * larger than the link's MTU.
789 * XXX: IFF_FRAGMENTABLE (or such) flag has not been defined yet...
790 */
791
792 || ifp->if_flags & IFF_FRAGMENTABLE
793 #endif
794 )
795 {
796 #ifdef NEWIP6OUTPUT
797 error = nd6_output(ifp, m, dst, ro->ro_rt);
798 #else
799 error = (*ifp->if_output)(ifp, m, (struct sockaddr *)dst,
800 ro->ro_rt);
801 #endif
802 goto done;
803 } else if (mtu < IPV6_MMTU) {
804 /*
805 * note that path MTU is never less than IPV6_MMTU
806 * (see icmp6_input).
807 */
808 error = EMSGSIZE;
809 goto bad;
810 } else if (ip6->ip6_plen == 0) { /* jumbo payload cannot be fragmented */
811 error = EMSGSIZE;
812 goto bad;
813 } else {
814 struct mbuf **mnext, *m_frgpart;
815 struct ip6_frag *ip6f;
816 u_long id = htonl(ip6_id++);
817 u_char nextproto;
818
819 /*
820 * Too large for the destination or interface;
821 * fragment if possible.
822 * Must be able to put at least 8 bytes per fragment.
823 */
824 hlen = unfragpartlen;
825 if (mtu > IPV6_MAXPACKET)
826 mtu = IPV6_MAXPACKET;
827 len = (mtu - hlen - sizeof(struct ip6_frag)) & ~7;
828 if (len < 8) {
829 error = EMSGSIZE;
830 goto bad;
831 }
832
833 mnext = &m->m_nextpkt;
834
835 /*
836 * Change the next header field of the last header in the
837 * unfragmentable part.
838 */
839 if (exthdrs.ip6e_rthdr) {
840 nextproto = *mtod(exthdrs.ip6e_rthdr, u_char *);
841 *mtod(exthdrs.ip6e_rthdr, u_char *) = IPPROTO_FRAGMENT;
842 }
843 else if (exthdrs.ip6e_dest1) {
844 nextproto = *mtod(exthdrs.ip6e_dest1, u_char *);
845 *mtod(exthdrs.ip6e_dest1, u_char *) = IPPROTO_FRAGMENT;
846 }
847 else if (exthdrs.ip6e_hbh) {
848 nextproto = *mtod(exthdrs.ip6e_hbh, u_char *);
849 *mtod(exthdrs.ip6e_hbh, u_char *) = IPPROTO_FRAGMENT;
850 }
851 else {
852 nextproto = ip6->ip6_nxt;
853 ip6->ip6_nxt = IPPROTO_FRAGMENT;
854 }
855
856 /*
857 * Loop through length of segment after first fragment,
858 * make new header and copy data of each part and link onto chain.
859 */
860 m0 = m;
861 for (off = hlen; off < tlen; off += len) {
862 MGETHDR(m, M_DONTWAIT, MT_HEADER);
863 if (!m) {
864 error = ENOBUFS;
865 ip6stat.ip6s_odropped++;
866 goto sendorfree;
867 }
868 m->m_flags = m0->m_flags & M_COPYFLAGS;
869 *mnext = m;
870 mnext = &m->m_nextpkt;
871 m->m_data += max_linkhdr;
872 mhip6 = mtod(m, struct ip6_hdr *);
873 *mhip6 = *ip6;
874 m->m_len = sizeof(*mhip6);
875 error = ip6_insertfraghdr(m0, m, hlen, &ip6f);
876 if (error) {
877 ip6stat.ip6s_odropped++;
878 goto sendorfree;
879 }
880 ip6f->ip6f_offlg = htons((u_short)((off - hlen) & ~7));
881 if (off + len >= tlen)
882 len = tlen - off;
883 else
884 ip6f->ip6f_offlg |= IP6F_MORE_FRAG;
885 mhip6->ip6_plen = htons((u_short)(len + hlen +
886 sizeof(*ip6f) -
887 sizeof(struct ip6_hdr)));
888 if ((m_frgpart = m_copy(m0, off, len)) == 0) {
889 error = ENOBUFS;
890 ip6stat.ip6s_odropped++;
891 goto sendorfree;
892 }
893 m_cat(m, m_frgpart);
894 m->m_pkthdr.len = len + hlen + sizeof(*ip6f);
895 m->m_pkthdr.rcvif = (struct ifnet *)0;
896 ip6f->ip6f_reserved = 0;
897 ip6f->ip6f_ident = id;
898 ip6f->ip6f_nxt = nextproto;
899 ip6stat.ip6s_ofragments++;
900 }
901 }
902
903 /*
904 * Remove leading garbages.
905 */
906 sendorfree:
907 m = m0->m_nextpkt;
908 m0->m_nextpkt = 0;
909 m_freem(m0);
910 for (m0 = m; m; m = m0) {
911 m0 = m->m_nextpkt;
912 m->m_nextpkt = 0;
913 if (error == 0) {
914 #ifdef NEWIP6OUTPUT
915 error = nd6_output(ifp, m, dst, ro->ro_rt);
916 #else
917 error = (*ifp->if_output)(ifp, m,
918 (struct sockaddr *)dst,
919 ro->ro_rt);
920 #endif
921 }
922 else
923 m_freem(m);
924 }
925
926 if (error == 0)
927 ip6stat.ip6s_fragmented++;
928
929 done:
930 if (ro == &ip6route && ro->ro_rt) { /* brace necessary for RTFREE */
931 RTFREE(ro->ro_rt);
932 } else if (ro_pmtu == &ip6route && ro_pmtu->ro_rt) {
933 RTFREE(ro_pmtu->ro_rt);
934 }
935
936 #ifdef IPSEC
937 if (sp != NULL)
938 key_freesp(sp);
939 #endif /* IPSEC */
940
941 return(error);
942
943 freehdrs:
944 m_freem(exthdrs.ip6e_hbh); /* m_freem will check if mbuf is 0 */
945 m_freem(exthdrs.ip6e_dest1);
946 m_freem(exthdrs.ip6e_rthdr);
947 m_freem(exthdrs.ip6e_dest2);
948 /* fall through */
949 bad:
950 m_freem(m);
951 goto done;
952 }
953
954 static int
955 ip6_copyexthdr(mp, hdr, hlen)
956 struct mbuf **mp;
957 caddr_t hdr;
958 int hlen;
959 {
960 struct mbuf *m;
961
962 if (hlen > MCLBYTES)
963 return(ENOBUFS); /* XXX */
964
965 MGET(m, M_DONTWAIT, MT_DATA);
966 if (!m)
967 return(ENOBUFS);
968
969 if (hlen > MLEN) {
970 MCLGET(m, M_DONTWAIT);
971 if ((m->m_flags & M_EXT) == 0) {
972 m_free(m);
973 return(ENOBUFS);
974 }
975 }
976 m->m_len = hlen;
977 if (hdr)
978 bcopy(hdr, mtod(m, caddr_t), hlen);
979
980 *mp = m;
981 return(0);
982 }
983
984 /*
985 * Insert jumbo payload option.
986 */
987 static int
988 ip6_insert_jumboopt(exthdrs, plen)
989 struct ip6_exthdrs *exthdrs;
990 u_int32_t plen;
991 {
992 struct mbuf *mopt;
993 u_char *optbuf;
994
995 #define JUMBOOPTLEN 8 /* length of jumbo payload option and padding */
996
997 /*
998 * If there is no hop-by-hop options header, allocate new one.
999 * If there is one but it doesn't have enough space to store the
1000 * jumbo payload option, allocate a cluster to store the whole options.
1001 * Otherwise, use it to store the options.
1002 */
1003 if (exthdrs->ip6e_hbh == 0) {
1004 MGET(mopt, M_DONTWAIT, MT_DATA);
1005 if (mopt == 0)
1006 return(ENOBUFS);
1007 mopt->m_len = JUMBOOPTLEN;
1008 optbuf = mtod(mopt, u_char *);
1009 optbuf[1] = 0; /* = ((JUMBOOPTLEN) >> 3) - 1 */
1010 exthdrs->ip6e_hbh = mopt;
1011 }
1012 else {
1013 struct ip6_hbh *hbh;
1014
1015 mopt = exthdrs->ip6e_hbh;
1016 if (M_TRAILINGSPACE(mopt) < JUMBOOPTLEN) {
1017 caddr_t oldoptp = mtod(mopt, caddr_t);
1018 int oldoptlen = mopt->m_len;
1019
1020 if (mopt->m_flags & M_EXT)
1021 return(ENOBUFS); /* XXX */
1022 MCLGET(mopt, M_DONTWAIT);
1023 if ((mopt->m_flags & M_EXT) == 0)
1024 return(ENOBUFS);
1025
1026 bcopy(oldoptp, mtod(mopt, caddr_t), oldoptlen);
1027 optbuf = mtod(mopt, caddr_t) + oldoptlen;
1028 mopt->m_len = oldoptlen + JUMBOOPTLEN;
1029 }
1030 else {
1031 optbuf = mtod(mopt, u_char *) + mopt->m_len;
1032 mopt->m_len += JUMBOOPTLEN;
1033 }
1034 optbuf[0] = IP6OPT_PADN;
1035 optbuf[1] = 1;
1036
1037 /*
1038 * Adjust the header length according to the pad and
1039 * the jumbo payload option.
1040 */
1041 hbh = mtod(mopt, struct ip6_hbh *);
1042 hbh->ip6h_len += (JUMBOOPTLEN >> 3);
1043 }
1044
1045 /* fill in the option. */
1046 optbuf[2] = IP6OPT_JUMBO;
1047 optbuf[3] = 4;
1048 *(u_int32_t *)&optbuf[4] = htonl(plen + JUMBOOPTLEN);
1049
1050 /* finally, adjust the packet header length */
1051 exthdrs->ip6e_ip6->m_pkthdr.len += JUMBOOPTLEN;
1052
1053 return(0);
1054 #undef JUMBOOPTLEN
1055 }
1056
1057 /*
1058 * Insert fragment header and copy unfragmentable header portions.
1059 */
1060 static int
1061 ip6_insertfraghdr(m0, m, hlen, frghdrp)
1062 struct mbuf *m0, *m;
1063 int hlen;
1064 struct ip6_frag **frghdrp;
1065 {
1066 struct mbuf *n, *mlast;
1067
1068 if (hlen > sizeof(struct ip6_hdr)) {
1069 n = m_copym(m0, sizeof(struct ip6_hdr),
1070 hlen - sizeof(struct ip6_hdr), M_DONTWAIT);
1071 if (n == 0)
1072 return(ENOBUFS);
1073 m->m_next = n;
1074 }
1075 else
1076 n = m;
1077
1078 /* Search for the last mbuf of unfragmentable part. */
1079 for (mlast = n; mlast->m_next; mlast = mlast->m_next)
1080 ;
1081
1082 if ((mlast->m_flags & M_EXT) == 0 &&
1083 M_TRAILINGSPACE(mlast) < sizeof(struct ip6_frag)) {
1084 /* use the trailing space of the last mbuf for the fragment hdr */
1085 *frghdrp =
1086 (struct ip6_frag *)(mtod(mlast, caddr_t) + mlast->m_len);
1087 mlast->m_len += sizeof(struct ip6_frag);
1088 m->m_pkthdr.len += sizeof(struct ip6_frag);
1089 }
1090 else {
1091 /* allocate a new mbuf for the fragment header */
1092 struct mbuf *mfrg;
1093
1094 MGET(mfrg, M_DONTWAIT, MT_DATA);
1095 if (mfrg == 0)
1096 return(ENOBUFS);
1097 mfrg->m_len = sizeof(struct ip6_frag);
1098 *frghdrp = mtod(mfrg, struct ip6_frag *);
1099 mlast->m_next = mfrg;
1100 }
1101
1102 return(0);
1103 }
1104
1105 /*
1106 * IP6 socket option processing.
1107 */
1108 int
1109 ip6_ctloutput(op, so, level, optname, mp)
1110 int op;
1111 struct socket *so;
1112 int level, optname;
1113 struct mbuf **mp;
1114 {
1115 register struct in6pcb *in6p = sotoin6pcb(so);
1116 register struct mbuf *m = *mp;
1117 register int optval = 0;
1118 int error = 0;
1119 struct proc *p = curproc; /* XXX */
1120
1121 if (level == IPPROTO_IPV6)
1122 switch (op) {
1123
1124 case PRCO_SETOPT:
1125 switch (optname) {
1126 case IPV6_PKTOPTIONS:
1127 return(ip6_pcbopts(&in6p->in6p_outputopts,
1128 m, so));
1129 case IPV6_HOPOPTS:
1130 case IPV6_DSTOPTS:
1131 if (p == 0 || suser(p->p_ucred, &p->p_acflag)) {
1132 error = EPERM;
1133 break;
1134 }
1135 /* fall through */
1136 case IPV6_UNICAST_HOPS:
1137 case IPV6_RECVOPTS:
1138 case IPV6_RECVRETOPTS:
1139 case IPV6_RECVDSTADDR:
1140 case IPV6_PKTINFO:
1141 case IPV6_HOPLIMIT:
1142 case IPV6_RTHDR:
1143 case IPV6_CHECKSUM:
1144 case IPV6_FAITH:
1145 if (!m || m->m_len != sizeof(int))
1146 error = EINVAL;
1147 else {
1148 optval = *mtod(m, int *);
1149 switch (optname) {
1150
1151 case IPV6_UNICAST_HOPS:
1152 if (optval < -1 || optval >= 256)
1153 error = EINVAL;
1154 else {
1155 /* -1 = kernel default */
1156 in6p->in6p_hops = optval;
1157 }
1158 break;
1159 #define OPTSET(bit) \
1160 if (optval) \
1161 in6p->in6p_flags |= bit; \
1162 else \
1163 in6p->in6p_flags &= ~bit;
1164
1165 case IPV6_RECVOPTS:
1166 OPTSET(IN6P_RECVOPTS);
1167 break;
1168
1169 case IPV6_RECVRETOPTS:
1170 OPTSET(IN6P_RECVRETOPTS);
1171 break;
1172
1173 case IPV6_RECVDSTADDR:
1174 OPTSET(IN6P_RECVDSTADDR);
1175 break;
1176
1177 case IPV6_PKTINFO:
1178 OPTSET(IN6P_PKTINFO);
1179 break;
1180
1181 case IPV6_HOPLIMIT:
1182 OPTSET(IN6P_HOPLIMIT);
1183 break;
1184
1185 case IPV6_HOPOPTS:
1186 OPTSET(IN6P_HOPOPTS);
1187 break;
1188
1189 case IPV6_DSTOPTS:
1190 OPTSET(IN6P_DSTOPTS);
1191 break;
1192
1193 case IPV6_RTHDR:
1194 OPTSET(IN6P_RTHDR);
1195 break;
1196
1197 case IPV6_CHECKSUM:
1198 in6p->in6p_cksum = optval;
1199 break;
1200
1201 case IPV6_FAITH:
1202 OPTSET(IN6P_FAITH);
1203 break;
1204 }
1205 }
1206 break;
1207 #undef OPTSET
1208
1209 case IPV6_MULTICAST_IF:
1210 case IPV6_MULTICAST_HOPS:
1211 case IPV6_MULTICAST_LOOP:
1212 case IPV6_JOIN_GROUP:
1213 case IPV6_LEAVE_GROUP:
1214 error = ip6_setmoptions(optname, &in6p->in6p_moptions, m);
1215 break;
1216
1217 #ifdef IPSEC
1218 case IPV6_IPSEC_POLICY:
1219 {
1220 caddr_t req = NULL;
1221 int len = 0;
1222 int priv = 0;
1223 #ifdef __NetBSD__
1224 if (p == 0 || suser(p->p_ucred, &p->p_acflag))
1225 priv = 0;
1226 else
1227 priv = 1;
1228 #else
1229 priv = (in6p->in6p_socket->so_state & SS_PRIV);
1230 #endif
1231 if (m != 0) {
1232 req = mtod(m, caddr_t);
1233 len = m->m_len;
1234 }
1235 error = ipsec_set_policy(&in6p->in6p_sp,
1236 optname, req, len,
1237 priv);
1238 }
1239 break;
1240 #endif /* IPSEC */
1241
1242 default:
1243 error = ENOPROTOOPT;
1244 break;
1245 }
1246 if (m)
1247 (void)m_free(m);
1248 break;
1249
1250 case PRCO_GETOPT:
1251 switch (optname) {
1252
1253 case IPV6_OPTIONS:
1254 case IPV6_RETOPTS:
1255 #if 0
1256 *mp = m = m_get(M_WAIT, MT_SOOPTS);
1257 if (in6p->in6p_options) {
1258 m->m_len = in6p->in6p_options->m_len;
1259 bcopy(mtod(in6p->in6p_options, caddr_t),
1260 mtod(m, caddr_t),
1261 (unsigned)m->m_len);
1262 } else
1263 m->m_len = 0;
1264 break;
1265 #else
1266 error = ENOPROTOOPT;
1267 break;
1268 #endif
1269
1270 case IPV6_PKTOPTIONS:
1271 if (in6p->in6p_options) {
1272 *mp = m_copym(in6p->in6p_options, 0,
1273 M_COPYALL, M_WAIT);
1274 } else {
1275 *mp = m_get(M_WAIT, MT_SOOPTS);
1276 (*mp)->m_len = 0;
1277 }
1278 break;
1279
1280 case IPV6_HOPOPTS:
1281 case IPV6_DSTOPTS:
1282 if (p == 0 || suser(p->p_ucred, &p->p_acflag)) {
1283 error = EPERM;
1284 break;
1285 }
1286 /* fall through */
1287 case IPV6_UNICAST_HOPS:
1288 case IPV6_RECVOPTS:
1289 case IPV6_RECVRETOPTS:
1290 case IPV6_RECVDSTADDR:
1291 case IPV6_PKTINFO:
1292 case IPV6_HOPLIMIT:
1293 case IPV6_RTHDR:
1294 case IPV6_CHECKSUM:
1295 case IPV6_FAITH:
1296 *mp = m = m_get(M_WAIT, MT_SOOPTS);
1297 m->m_len = sizeof(int);
1298 switch (optname) {
1299
1300 case IPV6_UNICAST_HOPS:
1301 optval = in6p->in6p_hops;
1302 break;
1303
1304 #define OPTBIT(bit) (in6p->in6p_flags & bit ? 1 : 0)
1305
1306 case IPV6_RECVOPTS:
1307 optval = OPTBIT(IN6P_RECVOPTS);
1308 break;
1309
1310 case IPV6_RECVRETOPTS:
1311 optval = OPTBIT(IN6P_RECVRETOPTS);
1312 break;
1313
1314 case IPV6_RECVDSTADDR:
1315 optval = OPTBIT(IN6P_RECVDSTADDR);
1316 break;
1317
1318 case IPV6_PKTINFO:
1319 optval = OPTBIT(IN6P_PKTINFO);
1320 break;
1321
1322 case IPV6_HOPLIMIT:
1323 optval = OPTBIT(IN6P_HOPLIMIT);
1324 break;
1325
1326 case IPV6_HOPOPTS:
1327 optval = OPTBIT(IN6P_HOPOPTS);
1328 break;
1329
1330 case IPV6_DSTOPTS:
1331 optval = OPTBIT(IN6P_DSTOPTS);
1332 break;
1333
1334 case IPV6_RTHDR:
1335 optval = OPTBIT(IN6P_RTHDR);
1336 break;
1337
1338 case IPV6_CHECKSUM:
1339 optval = in6p->in6p_cksum;
1340 break;
1341
1342 case IPV6_FAITH:
1343 optval = OPTBIT(IN6P_FAITH);
1344 break;
1345 }
1346 *mtod(m, int *) = optval;
1347 break;
1348
1349 case IPV6_MULTICAST_IF:
1350 case IPV6_MULTICAST_HOPS:
1351 case IPV6_MULTICAST_LOOP:
1352 case IPV6_JOIN_GROUP:
1353 case IPV6_LEAVE_GROUP:
1354 error = ip6_getmoptions(optname, in6p->in6p_moptions, mp);
1355 break;
1356
1357 #ifdef IPSEC
1358 case IPV6_IPSEC_POLICY:
1359 error = ipsec_get_policy(in6p->in6p_sp, mp);
1360 break;
1361 #endif /* IPSEC */
1362
1363 default:
1364 error = ENOPROTOOPT;
1365 break;
1366 }
1367 break;
1368 }
1369 else {
1370 error = EINVAL;
1371 if (op == PRCO_SETOPT && *mp)
1372 (void)m_free(*mp);
1373 }
1374 return(error);
1375 }
1376
1377 /*
1378 * Set up IP6 options in pcb for insertion in output packets.
1379 * Store in mbuf with pointer in pcbopt, adding pseudo-option
1380 * with destination address if source routed.
1381 */
1382 static int
1383 ip6_pcbopts(pktopt, m, so)
1384 struct ip6_pktopts **pktopt;
1385 register struct mbuf *m;
1386 struct socket *so;
1387 {
1388 register struct ip6_pktopts *opt = *pktopt;
1389 int error = 0;
1390 struct proc *p = curproc; /* XXX */
1391 int priv = 0;
1392
1393 /* turn off any old options. */
1394 if (opt) {
1395 if (opt->ip6po_m)
1396 (void)m_free(opt->ip6po_m);
1397 }
1398 else
1399 opt = malloc(sizeof(*opt), M_IP6OPT, M_WAITOK);
1400 *pktopt = 0;
1401
1402 if (!m || m->m_len == 0) {
1403 /*
1404 * Only turning off any previous options.
1405 */
1406 if (opt)
1407 free(opt, M_IP6OPT);
1408 if (m)
1409 (void)m_free(m);
1410 return(0);
1411 }
1412
1413 /* set options specified by user. */
1414 if (p && !suser(p->p_ucred, &p->p_acflag))
1415 priv = 1;
1416 if ((error = ip6_setpktoptions(m, opt, priv)) != 0) {
1417 (void)m_free(m);
1418 return(error);
1419 }
1420 *pktopt = opt;
1421 return(0);
1422 }
1423
1424 /*
1425 * Set the IP6 multicast options in response to user setsockopt().
1426 */
1427 static int
1428 ip6_setmoptions(optname, im6op, m)
1429 int optname;
1430 struct ip6_moptions **im6op;
1431 struct mbuf *m;
1432 {
1433 int error = 0;
1434 u_int loop, ifindex;
1435 struct ipv6_mreq *mreq;
1436 struct ifnet *ifp;
1437 struct ip6_moptions *im6o = *im6op;
1438 struct route_in6 ro;
1439 struct sockaddr_in6 *dst;
1440 struct in6_multi_mship *imm;
1441 struct proc *p = curproc; /* XXX */
1442
1443 if (im6o == NULL) {
1444 /*
1445 * No multicast option buffer attached to the pcb;
1446 * allocate one and initialize to default values.
1447 */
1448 im6o = (struct ip6_moptions *)
1449 malloc(sizeof(*im6o), M_IPMOPTS, M_WAITOK);
1450
1451 if (im6o == NULL)
1452 return(ENOBUFS);
1453 *im6op = im6o;
1454 im6o->im6o_multicast_ifp = NULL;
1455 im6o->im6o_multicast_hlim = ip6_defmcasthlim;
1456 im6o->im6o_multicast_loop = IPV6_DEFAULT_MULTICAST_LOOP;
1457 LIST_INIT(&im6o->im6o_memberships);
1458 }
1459
1460 switch (optname) {
1461
1462 case IPV6_MULTICAST_IF:
1463 /*
1464 * Select the interface for outgoing multicast packets.
1465 */
1466 if (m == NULL || m->m_len != sizeof(u_int)) {
1467 error = EINVAL;
1468 break;
1469 }
1470 ifindex = *(mtod(m, u_int *));
1471 if (ifindex < 0 || if_index < ifindex) {
1472 error = ENXIO; /* XXX EINVAL? */
1473 break;
1474 }
1475 ifp = ifindex2ifnet[ifindex];
1476 if (ifp == NULL || (ifp->if_flags & IFF_MULTICAST) == 0) {
1477 error = EADDRNOTAVAIL;
1478 break;
1479 }
1480 im6o->im6o_multicast_ifp = ifp;
1481 break;
1482
1483 case IPV6_MULTICAST_HOPS:
1484 {
1485 /*
1486 * Set the IP6 hoplimit for outgoing multicast packets.
1487 */
1488 int optval;
1489 if (m == NULL || m->m_len != sizeof(int)) {
1490 error = EINVAL;
1491 break;
1492 }
1493 optval = *(mtod(m, u_int *));
1494 if (optval < -1 || optval >= 256)
1495 error = EINVAL;
1496 else if (optval == -1)
1497 im6o->im6o_multicast_hlim = ip6_defmcasthlim;
1498 else
1499 im6o->im6o_multicast_hlim = optval;
1500 break;
1501 }
1502
1503 case IPV6_MULTICAST_LOOP:
1504 /*
1505 * Set the loopback flag for outgoing multicast packets.
1506 * Must be zero or one.
1507 */
1508 if (m == NULL || m->m_len != sizeof(u_int) ||
1509 (loop = *(mtod(m, u_int *))) > 1) {
1510 error = EINVAL;
1511 break;
1512 }
1513 im6o->im6o_multicast_loop = loop;
1514 break;
1515
1516 case IPV6_JOIN_GROUP:
1517 /*
1518 * Add a multicast group membership.
1519 * Group must be a valid IP6 multicast address.
1520 */
1521 if (m == NULL || m->m_len != sizeof(struct ipv6_mreq)) {
1522 error = EINVAL;
1523 break;
1524 }
1525 mreq = mtod(m, struct ipv6_mreq *);
1526 if (IN6_IS_ADDR_ANY(&mreq->ipv6mr_multiaddr)) {
1527 /*
1528 * We use the unspecified address to specify to accept
1529 * all multicast addresses. Only super user is allowed
1530 * to do this.
1531 */
1532 if (suser(p->p_ucred, &p->p_acflag)) {
1533 error = EACCES;
1534 break;
1535 }
1536 } else if (!IN6_IS_ADDR_MULTICAST(&mreq->ipv6mr_multiaddr)) {
1537 error = EINVAL;
1538 break;
1539 }
1540
1541 /*
1542 * If the interface is specified, validate it.
1543 */
1544 if (mreq->ipv6mr_interface < 0
1545 || if_index < mreq->ipv6mr_interface) {
1546 error = ENXIO; /* XXX EINVAL? */
1547 break;
1548 }
1549 /*
1550 * If no interface was explicitly specified, choose an
1551 * appropriate one according to the given multicast address.
1552 */
1553 if (mreq->ipv6mr_interface == 0) {
1554 /*
1555 * If the multicast address is in node-local scope,
1556 * the interface should be a loopback interface.
1557 * Otherwise, look up the routing table for the
1558 * address, and choose the outgoing interface.
1559 * XXX: is it a good approach?
1560 */
1561 if (IN6_IS_ADDR_MC_NODELOCAL(&mreq->ipv6mr_multiaddr)) {
1562 #ifdef __bsdi__
1563 ifp = &loif;
1564 #else
1565 ifp = &loif[0];
1566 #endif
1567 }
1568 else {
1569 ro.ro_rt = NULL;
1570 dst = (struct sockaddr_in6 *)&ro.ro_dst;
1571 bzero(dst, sizeof(*dst));
1572 dst->sin6_len = sizeof(struct sockaddr_in6);
1573 dst->sin6_family = AF_INET6;
1574 dst->sin6_addr = mreq->ipv6mr_multiaddr;
1575 rtalloc((struct route *)&ro);
1576 if (ro.ro_rt == NULL) {
1577 error = EADDRNOTAVAIL;
1578 break;
1579 }
1580 ifp = ro.ro_rt->rt_ifp;
1581 rtfree(ro.ro_rt);
1582 }
1583 } else
1584 ifp = ifindex2ifnet[mreq->ipv6mr_interface];
1585
1586 /*
1587 * See if we found an interface, and confirm that it
1588 * supports multicast
1589 */
1590 if (ifp == NULL || (ifp->if_flags & IFF_MULTICAST) == 0) {
1591 error = EADDRNOTAVAIL;
1592 break;
1593 }
1594 /*
1595 * Put interface index into the multicast address,
1596 * if the address has link-local scope.
1597 */
1598 if (IN6_IS_ADDR_MC_LINKLOCAL(&mreq->ipv6mr_multiaddr)) {
1599 mreq->ipv6mr_multiaddr.s6_addr16[1]
1600 = htons(mreq->ipv6mr_interface);
1601 }
1602 /*
1603 * See if the membership already exists.
1604 */
1605 for (imm = im6o->im6o_memberships.lh_first;
1606 imm != NULL; imm = imm->i6mm_chain.le_next)
1607 if (imm->i6mm_maddr->in6m_ifp == ifp &&
1608 IN6_ARE_ADDR_EQUAL(&imm->i6mm_maddr->in6m_addr,
1609 &mreq->ipv6mr_multiaddr))
1610 break;
1611 if (imm != NULL) {
1612 error = EADDRINUSE;
1613 break;
1614 }
1615 /*
1616 * Everything looks good; add a new record to the multicast
1617 * address list for the given interface.
1618 */
1619 imm = malloc(sizeof(*imm), M_IPMADDR, M_WAITOK);
1620 if (imm == NULL) {
1621 error = ENOBUFS;
1622 break;
1623 }
1624 if ((imm->i6mm_maddr =
1625 in6_addmulti(&mreq->ipv6mr_multiaddr, ifp, &error)) == NULL) {
1626 free(imm, M_IPMADDR);
1627 break;
1628 }
1629 LIST_INSERT_HEAD(&im6o->im6o_memberships, imm, i6mm_chain);
1630 break;
1631
1632 case IPV6_LEAVE_GROUP:
1633 /*
1634 * Drop a multicast group membership.
1635 * Group must be a valid IP6 multicast address.
1636 */
1637 if (m == NULL || m->m_len != sizeof(struct ipv6_mreq)) {
1638 error = EINVAL;
1639 break;
1640 }
1641 mreq = mtod(m, struct ipv6_mreq *);
1642 if (IN6_IS_ADDR_ANY(&mreq->ipv6mr_multiaddr)) {
1643 if (suser(p->p_ucred, &p->p_acflag)) {
1644 error = EACCES;
1645 break;
1646 }
1647 } else if (!IN6_IS_ADDR_MULTICAST(&mreq->ipv6mr_multiaddr)) {
1648 error = EINVAL;
1649 break;
1650 }
1651 /*
1652 * If an interface address was specified, get a pointer
1653 * to its ifnet structure.
1654 */
1655 if (mreq->ipv6mr_interface < 0
1656 || if_index < mreq->ipv6mr_interface) {
1657 error = ENXIO; /* XXX EINVAL? */
1658 break;
1659 }
1660 ifp = ifindex2ifnet[mreq->ipv6mr_interface];
1661 /*
1662 * Put interface index into the multicast address,
1663 * if the address has link-local scope.
1664 */
1665 if (IN6_IS_ADDR_MC_LINKLOCAL(&mreq->ipv6mr_multiaddr)) {
1666 mreq->ipv6mr_multiaddr.s6_addr16[1]
1667 = htons(mreq->ipv6mr_interface);
1668 }
1669 /*
1670 * Find the membership in the membership list.
1671 */
1672 for (imm = im6o->im6o_memberships.lh_first;
1673 imm != NULL; imm = imm->i6mm_chain.le_next) {
1674 if ((ifp == NULL ||
1675 imm->i6mm_maddr->in6m_ifp == ifp) &&
1676 IN6_ARE_ADDR_EQUAL(&imm->i6mm_maddr->in6m_addr,
1677 &mreq->ipv6mr_multiaddr))
1678 break;
1679 }
1680 if (imm == NULL) {
1681 /* Unable to resolve interface */
1682 error = EADDRNOTAVAIL;
1683 break;
1684 }
1685 /*
1686 * Give up the multicast address record to which the
1687 * membership points.
1688 */
1689 LIST_REMOVE(imm, i6mm_chain);
1690 in6_delmulti(imm->i6mm_maddr);
1691 free(imm, M_IPMADDR);
1692 break;
1693
1694 default:
1695 error = EOPNOTSUPP;
1696 break;
1697 }
1698
1699 /*
1700 * If all options have default values, no need to keep the mbuf.
1701 */
1702 if (im6o->im6o_multicast_ifp == NULL &&
1703 im6o->im6o_multicast_hlim == ip6_defmcasthlim &&
1704 im6o->im6o_multicast_loop == IPV6_DEFAULT_MULTICAST_LOOP &&
1705 im6o->im6o_memberships.lh_first == NULL) {
1706 free(*im6op, M_IPMOPTS);
1707 *im6op = NULL;
1708 }
1709
1710 return(error);
1711 }
1712
1713 /*
1714 * Return the IP6 multicast options in response to user getsockopt().
1715 */
1716 static int
1717 ip6_getmoptions(optname, im6o, mp)
1718 int optname;
1719 register struct ip6_moptions *im6o;
1720 register struct mbuf **mp;
1721 {
1722 u_int *hlim, *loop, *ifindex;
1723
1724 *mp = m_get(M_WAIT, MT_SOOPTS);
1725
1726 switch (optname) {
1727
1728 case IPV6_MULTICAST_IF:
1729 ifindex = mtod(*mp, u_int *);
1730 (*mp)->m_len = sizeof(u_int);
1731 if (im6o == NULL || im6o->im6o_multicast_ifp == NULL)
1732 *ifindex = 0;
1733 else
1734 *ifindex = im6o->im6o_multicast_ifp->if_index;
1735 return(0);
1736
1737 case IPV6_MULTICAST_HOPS:
1738 hlim = mtod(*mp, u_int *);
1739 (*mp)->m_len = sizeof(u_int);
1740 if (im6o == NULL)
1741 *hlim = ip6_defmcasthlim;
1742 else
1743 *hlim = im6o->im6o_multicast_hlim;
1744 return(0);
1745
1746 case IPV6_MULTICAST_LOOP:
1747 loop = mtod(*mp, u_int *);
1748 (*mp)->m_len = sizeof(u_int);
1749 if (im6o == NULL)
1750 *loop = ip6_defmcasthlim;
1751 else
1752 *loop = im6o->im6o_multicast_loop;
1753 return(0);
1754
1755 default:
1756 return(EOPNOTSUPP);
1757 }
1758 }
1759
1760 /*
1761 * Discard the IP6 multicast options.
1762 */
1763 void
1764 ip6_freemoptions(im6o)
1765 register struct ip6_moptions *im6o;
1766 {
1767 struct in6_multi_mship *imm;
1768
1769 if (im6o == NULL)
1770 return;
1771
1772 while ((imm = im6o->im6o_memberships.lh_first) != NULL) {
1773 LIST_REMOVE(imm, i6mm_chain);
1774 if (imm->i6mm_maddr)
1775 in6_delmulti(imm->i6mm_maddr);
1776 free(imm, M_IPMADDR);
1777 }
1778 free(im6o, M_IPMOPTS);
1779 }
1780
1781 /*
1782 * Set IPv6 outgoing packet options based on advanced API.
1783 */
1784 int
1785 ip6_setpktoptions(control, opt, priv)
1786 struct mbuf *control;
1787 struct ip6_pktopts *opt;
1788 int priv;
1789 {
1790 register struct cmsghdr *cm = 0;
1791
1792 if (control == 0 || opt == 0)
1793 return(EINVAL);
1794
1795 bzero(opt, sizeof(*opt));
1796 opt->ip6po_hlim = -1; /* -1 means to use default hop limit */
1797
1798 /*
1799 * XXX: Currently, we assume all the optional information is stored
1800 * in a single mbuf.
1801 */
1802 if (control->m_next)
1803 return(EINVAL);
1804
1805 opt->ip6po_m = control;
1806
1807 for (; control->m_len; control->m_data += CMSG_ALIGN(cm->cmsg_len),
1808 control->m_len -= CMSG_ALIGN(cm->cmsg_len)) {
1809 cm = mtod(control, struct cmsghdr *);
1810 if (cm->cmsg_len == 0 || cm->cmsg_len > control->m_len)
1811 return(EINVAL);
1812 if (cm->cmsg_level != IPPROTO_IPV6)
1813 continue;
1814
1815 switch(cm->cmsg_type) {
1816 case IPV6_PKTINFO:
1817 if (cm->cmsg_len != CMSG_LEN(sizeof(struct in6_pktinfo)))
1818 return(EINVAL);
1819 opt->ip6po_pktinfo = (struct in6_pktinfo *)CMSG_DATA(cm);
1820 if (opt->ip6po_pktinfo->ipi6_ifindex &&
1821 IN6_IS_ADDR_LINKLOCAL(&opt->ip6po_pktinfo->ipi6_addr))
1822 opt->ip6po_pktinfo->ipi6_addr.s6_addr16[1] =
1823 htons(opt->ip6po_pktinfo->ipi6_ifindex);
1824
1825 if (opt->ip6po_pktinfo->ipi6_ifindex > if_index
1826 || opt->ip6po_pktinfo->ipi6_ifindex < 0) {
1827 return(ENXIO);
1828 }
1829
1830 if (!IN6_IS_ADDR_ANY(&opt->ip6po_pktinfo->ipi6_addr)) {
1831 struct ifaddr *ia;
1832 struct sockaddr_in6 sin6;
1833
1834 bzero(&sin6, sizeof(sin6));
1835 sin6.sin6_len = sizeof(sin6);
1836 sin6.sin6_family = AF_INET6;
1837 sin6.sin6_addr =
1838 opt->ip6po_pktinfo->ipi6_addr;
1839 ia = ifa_ifwithaddr(sin6tosa(&sin6));
1840 if (ia == NULL ||
1841 (opt->ip6po_pktinfo->ipi6_ifindex &&
1842 (ia->ifa_ifp->if_index !=
1843 opt->ip6po_pktinfo->ipi6_ifindex))) {
1844 return(EADDRNOTAVAIL);
1845 }
1846 /*
1847 * Check if the requested source address is
1848 * indeed a unicast address assigned to the
1849 * node.
1850 */
1851 if (IN6_IS_ADDR_MULTICAST(&opt->ip6po_pktinfo->ipi6_addr))
1852 return(EADDRNOTAVAIL);
1853 }
1854 break;
1855
1856 case IPV6_HOPLIMIT:
1857 if (cm->cmsg_len != CMSG_LEN(sizeof(int)))
1858 return(EINVAL);
1859
1860 opt->ip6po_hlim = *(int *)CMSG_DATA(cm);
1861 if (opt->ip6po_hlim < -1 || opt->ip6po_hlim > 255)
1862 return(EINVAL);
1863 break;
1864
1865 case IPV6_NEXTHOP:
1866 if (!priv)
1867 return(EPERM);
1868 if (cm->cmsg_len < sizeof(u_char) ||
1869 cm->cmsg_len < CMSG_LEN(*CMSG_DATA(cm)))
1870 return(EINVAL);
1871
1872 opt->ip6po_nexthop = (struct sockaddr *)CMSG_DATA(cm);
1873
1874 break;
1875
1876 case IPV6_HOPOPTS:
1877 if (cm->cmsg_len < CMSG_LEN(sizeof(struct ip6_hbh)))
1878 return(EINVAL);
1879 opt->ip6po_hbh = (struct ip6_hbh *)CMSG_DATA(cm);
1880 if (cm->cmsg_len !=
1881 CMSG_LEN((opt->ip6po_hbh->ip6h_len + 1) << 3))
1882 return(EINVAL);
1883 break;
1884
1885 case IPV6_DSTOPTS:
1886 if (cm->cmsg_len < CMSG_LEN(sizeof(struct ip6_dest)))
1887 return(EINVAL);
1888
1889 /*
1890 * If there is no routing header yet, the destination
1891 * options header should be put on the 1st part.
1892 * Otherwise, the header should be on the 2nd part.
1893 * (See RFC 2460, section 4.1)
1894 */
1895 if (opt->ip6po_rthdr == NULL) {
1896 opt->ip6po_dest1 =
1897 (struct ip6_dest *)CMSG_DATA(cm);
1898 if (cm->cmsg_len !=
1899 CMSG_LEN((opt->ip6po_dest1->ip6d_len + 1)
1900 << 3))
1901 return(EINVAL);
1902 }
1903 else {
1904 opt->ip6po_dest2 =
1905 (struct ip6_dest *)CMSG_DATA(cm);
1906 if (cm->cmsg_len !=
1907 CMSG_LEN((opt->ip6po_dest2->ip6d_len + 1)
1908 << 3))
1909 return(EINVAL);
1910 }
1911 break;
1912
1913 case IPV6_RTHDR:
1914 if (cm->cmsg_len < CMSG_LEN(sizeof(struct ip6_rthdr)))
1915 return(EINVAL);
1916 opt->ip6po_rthdr = (struct ip6_rthdr *)CMSG_DATA(cm);
1917 if (cm->cmsg_len !=
1918 CMSG_LEN((opt->ip6po_rthdr->ip6r_len + 1) << 3))
1919 return(EINVAL);
1920 switch(opt->ip6po_rthdr->ip6r_type) {
1921 case IPV6_RTHDR_TYPE_0:
1922 if (opt->ip6po_rthdr->ip6r_segleft == 0)
1923 return(EINVAL);
1924 break;
1925 default:
1926 return(EINVAL);
1927 }
1928 break;
1929
1930 default:
1931 return(ENOPROTOOPT);
1932 }
1933 }
1934
1935 return(0);
1936 }
1937
1938 /*
1939 * Routine called from ip6_output() to loop back a copy of an IP6 multicast
1940 * packet to the input queue of a specified interface. Note that this
1941 * calls the output routine of the loopback "driver", but with an interface
1942 * pointer that might NOT be &loif -- easier than replicating that code here.
1943 */
1944 void
1945 ip6_mloopback(ifp, m, dst)
1946 struct ifnet *ifp;
1947 register struct mbuf *m;
1948 register struct sockaddr_in6 *dst;
1949 {
1950 struct mbuf *copym;
1951
1952 copym = m_copy(m, 0, M_COPYALL);
1953 if (copym != NULL)
1954 (void)looutput(ifp, copym, (struct sockaddr *)dst, NULL);
1955 }
1956
1957 /*
1958 * Chop IPv6 header off from the payload.
1959 */
1960 static int
1961 ip6_splithdr(m, exthdrs)
1962 struct mbuf *m;
1963 struct ip6_exthdrs *exthdrs;
1964 {
1965 struct mbuf *mh;
1966 struct ip6_hdr *ip6;
1967
1968 ip6 = mtod(m, struct ip6_hdr *);
1969 if (m->m_len > sizeof(*ip6)) {
1970 MGETHDR(mh, M_DONTWAIT, MT_HEADER);
1971 if (mh == 0) {
1972 m_freem(m);
1973 return ENOBUFS;
1974 }
1975 M_COPY_PKTHDR(mh, m);
1976 MH_ALIGN(mh, sizeof(*ip6));
1977 m->m_flags &= ~M_PKTHDR;
1978 m->m_len -= sizeof(*ip6);
1979 m->m_data += sizeof(*ip6);
1980 mh->m_next = m;
1981 m = mh;
1982 m->m_len = sizeof(*ip6);
1983 bcopy((caddr_t)ip6, mtod(m, caddr_t), sizeof(*ip6));
1984 }
1985 exthdrs->ip6e_ip6 = m;
1986 return 0;
1987 }
1988
1989 /*
1990 * Compute IPv6 extension header length.
1991 */
1992 int
1993 ip6_optlen(in6p)
1994 struct in6pcb *in6p;
1995 {
1996 int len;
1997
1998 if (!in6p->in6p_outputopts)
1999 return 0;
2000
2001 len = 0;
2002 #define elen(x) \
2003 (((struct ip6_ext *)(x)) ? (((struct ip6_ext *)(x))->ip6e_len + 1) << 3 : 0)
2004
2005 len += elen(in6p->in6p_outputopts->ip6po_hbh);
2006 len += elen(in6p->in6p_outputopts->ip6po_dest1);
2007 len += elen(in6p->in6p_outputopts->ip6po_rthdr);
2008 len += elen(in6p->in6p_outputopts->ip6po_dest2);
2009 return len;
2010 #undef elen
2011 }
2012