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