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