Cosmetic changes.
[dragonfly.git] / sys / netinet6 / ip6_output.c
1 /*      $FreeBSD: src/sys/netinet6/ip6_output.c,v 1.13.2.18 2003/01/24 05:11:35 sam Exp $       */
2 /*      $DragonFly: src/sys/netinet6/ip6_output.c,v 1.26 2006/10/24 06:18:42 hsu Exp $  */
3 /*      $KAME: ip6_output.c,v 1.279 2002/01/26 06:12:30 jinmei Exp $    */
4
5 /*
6  * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
7  * All rights reserved.
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions
11  * are met:
12  * 1. Redistributions of source code must retain the above copyright
13  *    notice, this list of conditions and the following disclaimer.
14  * 2. Redistributions in binary form must reproduce the above copyright
15  *    notice, this list of conditions and the following disclaimer in the
16  *    documentation and/or other materials provided with the distribution.
17  * 3. Neither the name of the project nor the names of its contributors
18  *    may be used to endorse or promote products derived from this software
19  *    without specific prior written permission.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
22  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24  * ARE DISCLAIMED.  IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
25  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31  * SUCH DAMAGE.
32  */
33
34 /*
35  * Copyright (c) 1982, 1986, 1988, 1990, 1993
36  *      The Regents of the University of California.  All rights reserved.
37  *
38  * Redistribution and use in source and binary forms, with or without
39  * modification, are permitted provided that the following conditions
40  * are met:
41  * 1. Redistributions of source code must retain the above copyright
42  *    notice, this list of conditions and the following disclaimer.
43  * 2. Redistributions in binary form must reproduce the above copyright
44  *    notice, this list of conditions and the following disclaimer in the
45  *    documentation and/or other materials provided with the distribution.
46  * 3. All advertising materials mentioning features or use of this software
47  *    must display the following acknowledgement:
48  *      This product includes software developed by the University of
49  *      California, Berkeley and its contributors.
50  * 4. Neither the name of the University nor the names of its contributors
51  *    may be used to endorse or promote products derived from this software
52  *    without specific prior written permission.
53  *
54  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
55  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
56  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
57  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
58  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
59  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
60  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
61  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
62  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
63  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
64  * SUCH DAMAGE.
65  *
66  *      @(#)ip_output.c 8.3 (Berkeley) 1/21/94
67  */
68
69 #include "opt_ip6fw.h"
70 #include "opt_inet.h"
71 #include "opt_inet6.h"
72 #include "opt_ipsec.h"
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/kernel.h>
83 #include <sys/proc.h>
84
85 #include <net/if.h>
86 #include <net/route.h>
87 #include <net/pfil.h>
88
89 #include <netinet/in.h>
90 #include <netinet/in_var.h>
91 #include <netinet6/in6_var.h>
92 #include <netinet/ip6.h>
93 #include <netinet/icmp6.h>
94 #include <netinet6/ip6_var.h>
95 #include <netinet/in_pcb.h>
96 #include <netinet6/nd6.h>
97
98 #ifdef IPSEC
99 #include <netinet6/ipsec.h>
100 #ifdef INET6
101 #include <netinet6/ipsec6.h>
102 #endif
103 #include <netproto/key/key.h>
104 #endif /* IPSEC */
105
106 #ifdef FAST_IPSEC
107 #include <netproto/ipsec/ipsec.h>
108 #include <netproto/ipsec/ipsec6.h>
109 #include <netproto/ipsec/key.h>
110 #endif /* FAST_IPSEC */
111
112 #include <net/ip6fw/ip6_fw.h>
113
114 #include <net/net_osdep.h>
115
116 static MALLOC_DEFINE(M_IPMOPTS, "ip6_moptions", "internet multicast options");
117
118 struct ip6_exthdrs {
119         struct mbuf *ip6e_ip6;
120         struct mbuf *ip6e_hbh;
121         struct mbuf *ip6e_dest1;
122         struct mbuf *ip6e_rthdr;
123         struct mbuf *ip6e_dest2;
124 };
125
126 static int ip6_pcbopts (struct ip6_pktopts **, struct mbuf *,
127                             struct socket *, struct sockopt *sopt);
128 static int ip6_setmoptions (int, struct ip6_moptions **, struct mbuf *);
129 static int ip6_getmoptions (int, struct ip6_moptions *, struct mbuf **);
130 static int copyexthdr (void *, struct mbuf **);
131 static int ip6_insertfraghdr (struct mbuf *, struct mbuf *, int,
132                                   struct ip6_frag **);
133 static int ip6_insert_jumboopt (struct ip6_exthdrs *, u_int32_t);
134 static struct mbuf *ip6_splithdr (struct mbuf *);
135
136 /*
137  * IP6 output. The packet in mbuf chain m contains a skeletal IP6
138  * header (with pri, len, nxt, hlim, src, dst).
139  * This function may modify ver and hlim only.
140  * The mbuf chain containing the packet will be freed.
141  * The mbuf opt, if present, will not be freed.
142  *
143  * type of "mtu": rt_rmx.rmx_mtu is u_long, ifnet.ifr_mtu is int, and
144  * nd_ifinfo.linkmtu is u_int32_t.  so we use u_long to hold largest one,
145  * which is rt_rmx.rmx_mtu.
146  */
147 int
148 ip6_output(struct mbuf *m0, struct ip6_pktopts *opt, struct route_in6 *ro,
149            int flags, struct ip6_moptions *im6o,
150            struct ifnet **ifpp,         /* XXX: just for statistics */
151            struct inpcb *inp)
152 {
153         struct ip6_hdr *ip6, *mhip6;
154         struct ifnet *ifp, *origifp;
155         struct mbuf *m = m0;
156         int hlen, tlen, len, off;
157         struct route_in6 ip6route;
158         struct sockaddr_in6 *dst;
159         int error = 0;
160         struct in6_ifaddr *ia = NULL;
161         u_long mtu;
162         u_int32_t optlen = 0, plen = 0, unfragpartlen = 0;
163         struct ip6_exthdrs exthdrs;
164         struct in6_addr finaldst;
165         struct route_in6 *ro_pmtu = NULL;
166         boolean_t hdrsplit = FALSE;
167         int needipsec = 0;
168 #ifdef IPSEC
169         int needipsectun = 0;
170         struct secpolicy *sp = NULL;
171         struct socket *so = inp ? inp->inp_socket : NULL;
172
173         ip6 = mtod(m, struct ip6_hdr *);
174 #endif /* IPSEC */
175 #ifdef FAST_IPSEC
176         int needipsectun = 0;
177         struct secpolicy *sp = NULL;
178
179         ip6 = mtod(m, struct ip6_hdr *);
180 #endif /* FAST_IPSEC */
181
182         bzero(&exthdrs, sizeof(exthdrs));
183
184         if (opt) {
185                 if ((error = copyexthdr(opt->ip6po_hbh, &exthdrs.ip6e_hbh)))
186                         goto freehdrs;
187                 if ((error = copyexthdr(opt->ip6po_dest1, &exthdrs.ip6e_dest1)))
188                         goto freehdrs;
189                 if ((error = copyexthdr(opt->ip6po_rthdr, &exthdrs.ip6e_rthdr)))
190                         goto freehdrs;
191                 if ((error = copyexthdr(opt->ip6po_dest2, &exthdrs.ip6e_dest2)))
192                         goto freehdrs;
193         }
194
195 #ifdef IPSEC
196         /* get a security policy for this packet */
197         if (so == NULL)
198                 sp = ipsec6_getpolicybyaddr(m, IPSEC_DIR_OUTBOUND, 0, &error);
199         else
200                 sp = ipsec6_getpolicybysock(m, IPSEC_DIR_OUTBOUND, so, &error);
201
202         if (sp == NULL) {
203                 ipsec6stat.out_inval++;
204                 goto freehdrs;
205         }
206
207         error = 0;
208
209         /* check policy */
210         switch (sp->policy) {
211         case IPSEC_POLICY_DISCARD:
212                 /*
213                  * This packet is just discarded.
214                  */
215                 ipsec6stat.out_polvio++;
216                 goto freehdrs;
217
218         case IPSEC_POLICY_BYPASS:
219         case IPSEC_POLICY_NONE:
220                 /* no need to do IPsec. */
221                 needipsec = 0;
222                 break;
223
224         case IPSEC_POLICY_IPSEC:
225                 if (sp->req == NULL) {
226                         error = key_spdacquire(sp);     /* acquire a policy */
227                         goto freehdrs;
228                 }
229                 needipsec = 1;
230                 break;
231
232         case IPSEC_POLICY_ENTRUST:
233         default:
234                 printf("ip6_output: Invalid policy found. %d\n", sp->policy);
235         }
236 #endif /* IPSEC */
237 #ifdef FAST_IPSEC
238         /* get a security policy for this packet */
239         if (inp == NULL)
240                 sp = ipsec_getpolicybyaddr(m, IPSEC_DIR_OUTBOUND, 0, &error);
241         else
242                 sp = ipsec_getpolicybysock(m, IPSEC_DIR_OUTBOUND, inp, &error);
243
244         if (sp == NULL) {
245                 newipsecstat.ips_out_inval++;
246                 goto freehdrs;
247         }
248
249         error = 0;
250
251         /* check policy */
252         switch (sp->policy) {
253         case IPSEC_POLICY_DISCARD:
254                 /*
255                  * This packet is just discarded.
256                  */
257                 newipsecstat.ips_out_polvio++;
258                 goto freehdrs;
259
260         case IPSEC_POLICY_BYPASS:
261         case IPSEC_POLICY_NONE:
262                 /* no need to do IPsec. */
263                 needipsec = 0;
264                 break;
265
266         case IPSEC_POLICY_IPSEC:
267                 if (sp->req == NULL) {
268                         /* acquire a policy */
269                         error = key_spdacquire(sp);
270                         goto freehdrs;
271                 }
272                 needipsec = 1;
273                 break;
274
275         case IPSEC_POLICY_ENTRUST:
276         default:
277                 printf("ip6_output: Invalid policy found. %d\n", sp->policy);
278         }
279 #endif /* FAST_IPSEC */
280
281         /*
282          * Calculate the total length of the extension header chain.
283          * Keep the length of the unfragmentable part for fragmentation.
284          */
285         optlen = 0;
286         if (exthdrs.ip6e_hbh) optlen += exthdrs.ip6e_hbh->m_len;
287         if (exthdrs.ip6e_dest1) optlen += exthdrs.ip6e_dest1->m_len;
288         if (exthdrs.ip6e_rthdr) optlen += exthdrs.ip6e_rthdr->m_len;
289         unfragpartlen = optlen + sizeof(struct ip6_hdr);
290         /* NOTE: we don't add AH/ESP length here. do that later. */
291         if (exthdrs.ip6e_dest2) optlen += exthdrs.ip6e_dest2->m_len;
292
293         /*
294          * If we need IPsec, or there is at least one extension header,
295          * separate IP6 header from the payload.
296          */
297         if ((needipsec || optlen) && !hdrsplit) {
298                 exthdrs.ip6e_ip6 = ip6_splithdr(m);
299                 if (exthdrs.ip6e_ip6 == NULL) {
300                         error = ENOBUFS;
301                         goto freehdrs;
302                 }
303                 m = exthdrs.ip6e_ip6;
304                 hdrsplit = TRUE;
305         }
306
307         /* adjust pointer */
308         ip6 = mtod(m, struct ip6_hdr *);
309
310         /* adjust mbuf packet header length */
311         m->m_pkthdr.len += optlen;
312         plen = m->m_pkthdr.len - sizeof(*ip6);
313
314         /* If this is a jumbo payload, insert a jumbo payload option. */
315         if (plen > IPV6_MAXPACKET) {
316                 if (!hdrsplit) {
317                         exthdrs.ip6e_ip6 = ip6_splithdr(m);
318                         if (exthdrs.ip6e_ip6 == NULL) {
319                                 error = ENOBUFS;
320                                 goto freehdrs;
321                         }
322                         m = exthdrs.ip6e_ip6;
323                         hdrsplit = TRUE;
324                 }
325                 /* adjust pointer */
326                 ip6 = mtod(m, struct ip6_hdr *);
327                 if ((error = ip6_insert_jumboopt(&exthdrs, plen)) != 0)
328                         goto freehdrs;
329                 ip6->ip6_plen = 0;
330         } else
331                 ip6->ip6_plen = htons(plen);
332
333         /*
334          * Concatenate headers and fill in next header fields.
335          * Here we have, on "m"
336          *      IPv6 payload
337          * and we insert headers accordingly.  Finally, we should be getting:
338          *      IPv6 hbh dest1 rthdr ah* [esp* dest2 payload]
339          *
340          * during the header composing process, "m" points to IPv6 header.
341          * "mprev" points to an extension header prior to esp.
342          */
343         {
344                 u_char *nexthdrp = &ip6->ip6_nxt;
345                 struct mbuf *mprev = m;
346
347                 /*
348                  * we treat dest2 specially.  this makes IPsec processing
349                  * much easier.  the goal here is to make mprev point the
350                  * mbuf prior to dest2.
351                  *
352                  * result: IPv6 dest2 payload
353                  * m and mprev will point to IPv6 header.
354                  */
355                 if (exthdrs.ip6e_dest2) {
356                         if (!hdrsplit)
357                                 panic("assumption failed: hdr not split");
358                         exthdrs.ip6e_dest2->m_next = m->m_next;
359                         m->m_next = exthdrs.ip6e_dest2;
360                         *mtod(exthdrs.ip6e_dest2, u_char *) = ip6->ip6_nxt;
361                         ip6->ip6_nxt = IPPROTO_DSTOPTS;
362                 }
363
364                 if (!hdrsplit) \
365                         panic("assumption failed: hdr not split"); \
366
367 /*
368  * Place m1 after mprev.
369  */
370 #define MAKE_CHAIN(m1, mprev, nexthdrp, i)\
371     do {\
372         if (m1) {\
373                 *mtod(m1, u_char *) = *nexthdrp;\
374                 *nexthdrp = (i);\
375                 nexthdrp = mtod(m1, u_char *);\
376                 m1->m_next = mprev->m_next;\
377                 mprev->m_next = m1;\
378                 mprev = m1;\
379         }\
380     } while (0)
381                 /*
382                  * result: IPv6 hbh dest1 rthdr dest2 payload
383                  * m will point to IPv6 header.  mprev will point to the
384                  * extension header prior to dest2 (rthdr in the above case).
385                  */
386                 MAKE_CHAIN(exthdrs.ip6e_hbh, mprev, nexthdrp, IPPROTO_HOPOPTS);
387                 MAKE_CHAIN(exthdrs.ip6e_dest1, mprev, nexthdrp, IPPROTO_DSTOPTS);
388                 MAKE_CHAIN(exthdrs.ip6e_rthdr, mprev, nexthdrp, IPPROTO_ROUTING);
389
390 #if defined(IPSEC) || defined(FAST_IPSEC)
391                 if (!needipsec)
392                         goto skip_ipsec2;
393
394                 /*
395                  * pointers after IPsec headers are not valid any more.
396                  * other pointers need a great care too.
397                  * (IPsec routines should not mangle mbufs prior to AH/ESP)
398                  */
399                 exthdrs.ip6e_dest2 = NULL;
400
401             {
402                 struct ip6_rthdr *rh = NULL;
403                 int segleft_org = 0;
404                 struct ipsec_output_state state;
405
406                 if (exthdrs.ip6e_rthdr) {
407                         rh = mtod(exthdrs.ip6e_rthdr, struct ip6_rthdr *);
408                         segleft_org = rh->ip6r_segleft;
409                         rh->ip6r_segleft = 0;
410                 }
411
412                 bzero(&state, sizeof(state));
413                 state.m = m;
414                 error = ipsec6_output_trans(&state, nexthdrp, mprev, sp, flags,
415                         &needipsectun);
416                 m = state.m;
417                 if (error) {
418                         /* mbuf is already reclaimed in ipsec6_output_trans. */
419                         m = NULL;
420                         switch (error) {
421                         case EHOSTUNREACH:
422                         case ENETUNREACH:
423                         case EMSGSIZE:
424                         case ENOBUFS:
425                         case ENOMEM:
426                                 break;
427                         default:
428                                 printf("ip6_output (ipsec): error code %d\n", error);
429                                 /* fall through */
430                         case ENOENT:
431                                 /* don't show these error codes to the user */
432                                 error = 0;
433                                 break;
434                         }
435                         goto bad;
436                 }
437                 if (exthdrs.ip6e_rthdr) {
438                         /* ah6_output doesn't modify mbuf chain */
439                         rh->ip6r_segleft = segleft_org;
440                 }
441             }
442 skip_ipsec2:;
443 #endif
444         }
445
446         /*
447          * If there is a routing header, replace destination address field
448          * with the first hop of the routing header.
449          */
450         if (exthdrs.ip6e_rthdr) {
451                 struct ip6_rthdr *rh =
452                         (struct ip6_rthdr *)(mtod(exthdrs.ip6e_rthdr,
453                                                   struct ip6_rthdr *));
454                 struct ip6_rthdr0 *rh0;
455
456                 finaldst = ip6->ip6_dst;
457                 switch (rh->ip6r_type) {
458                 case IPV6_RTHDR_TYPE_0:
459                          rh0 = (struct ip6_rthdr0 *)rh;
460                          ip6->ip6_dst = rh0->ip6r0_addr[0];
461                          bcopy(&rh0->ip6r0_addr[1], &rh0->ip6r0_addr[0],
462                              sizeof(struct in6_addr)*(rh0->ip6r0_segleft - 1));
463                          rh0->ip6r0_addr[rh0->ip6r0_segleft - 1] = finaldst;
464                          break;
465                 default:        /* is it possible? */
466                          error = EINVAL;
467                          goto bad;
468                 }
469         }
470
471         /* Source address validation */
472         if (IN6_IS_ADDR_UNSPECIFIED(&ip6->ip6_src) &&
473             !(flags & IPV6_DADOUTPUT)) {
474                 error = EOPNOTSUPP;
475                 ip6stat.ip6s_badscope++;
476                 goto bad;
477         }
478         if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_src)) {
479                 error = EOPNOTSUPP;
480                 ip6stat.ip6s_badscope++;
481                 goto bad;
482         }
483
484         ip6stat.ip6s_localout++;
485
486         /*
487          * Route packet.
488          */
489         if (ro == NULL) {
490                 ro = &ip6route;
491                 bzero(ro, sizeof(*ro));
492         }
493         ro_pmtu = ro;
494         if (opt && opt->ip6po_rthdr)
495                 ro = &opt->ip6po_route;
496         dst = (struct sockaddr_in6 *)&ro->ro_dst;
497         /*
498          * If there is a cached route,
499          * check that it is to the same destination
500          * and is still up. If not, free it and try again.
501          */
502         if (ro->ro_rt != NULL &&
503             (!(ro->ro_rt->rt_flags & RTF_UP) || dst->sin6_family != AF_INET6 ||
504              !IN6_ARE_ADDR_EQUAL(&dst->sin6_addr, &ip6->ip6_dst))) {
505                 RTFREE(ro->ro_rt);
506                 ro->ro_rt = NULL;
507         }
508         if (ro->ro_rt == NULL) {
509                 bzero(dst, sizeof(*dst));
510                 dst->sin6_family = AF_INET6;
511                 dst->sin6_len = sizeof(struct sockaddr_in6);
512                 dst->sin6_addr = ip6->ip6_dst;
513         }
514 #if defined(IPSEC) || defined(FAST_IPSEC)
515         if (needipsec && needipsectun) {
516                 struct ipsec_output_state state;
517
518                 /*
519                  * All the extension headers will become inaccessible
520                  * (since they can be encrypted).
521                  * Don't panic, we need no more updates to extension headers
522                  * on inner IPv6 packet (since they are now encapsulated).
523                  *
524                  * IPv6 [ESP|AH] IPv6 [extension headers] payload
525                  */
526                 bzero(&exthdrs, sizeof(exthdrs));
527                 exthdrs.ip6e_ip6 = m;
528
529                 bzero(&state, sizeof(state));
530                 state.m = m;
531                 state.ro = (struct route *)ro;
532                 state.dst = (struct sockaddr *)dst;
533
534                 error = ipsec6_output_tunnel(&state, sp, flags);
535
536                 m = state.m;
537                 ro = (struct route_in6 *)state.ro;
538                 dst = (struct sockaddr_in6 *)state.dst;
539                 if (error) {
540                         /* mbuf is already reclaimed in ipsec6_output_tunnel. */
541                         m0 = m = NULL;
542                         m = NULL;
543                         switch (error) {
544                         case EHOSTUNREACH:
545                         case ENETUNREACH:
546                         case EMSGSIZE:
547                         case ENOBUFS:
548                         case ENOMEM:
549                                 break;
550                         default:
551                                 printf("ip6_output (ipsec): error code %d\n", error);
552                                 /* fall through */
553                         case ENOENT:
554                                 /* don't show these error codes to the user */
555                                 error = 0;
556                                 break;
557                         }
558                         goto bad;
559                 }
560
561                 exthdrs.ip6e_ip6 = m;
562         }
563 #endif /* IPSEC */
564
565         if (!IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst)) {
566                 /* Unicast */
567
568 #define ifatoia6(ifa)   ((struct in6_ifaddr *)(ifa))
569 #define sin6tosa(sin6)  ((struct sockaddr *)(sin6))
570                 /* xxx
571                  * interface selection comes here
572                  * if an interface is specified from an upper layer,
573                  * ifp must point it.
574                  */
575                 if (ro->ro_rt == NULL) {
576                         /*
577                          * non-bsdi always clone routes, if parent is
578                          * PRF_CLONING.
579                          */
580                         rtalloc((struct route *)ro);
581                 }
582                 if (ro->ro_rt == NULL) {
583                         ip6stat.ip6s_noroute++;
584                         error = EHOSTUNREACH;
585                         /* XXX in6_ifstat_inc(ifp, ifs6_out_discard); */
586                         goto bad;
587                 }
588                 ia = ifatoia6(ro->ro_rt->rt_ifa);
589                 ifp = ro->ro_rt->rt_ifp;
590                 ro->ro_rt->rt_use++;
591                 if (ro->ro_rt->rt_flags & RTF_GATEWAY)
592                         dst = (struct sockaddr_in6 *)ro->ro_rt->rt_gateway;
593                 m->m_flags &= ~(M_BCAST | M_MCAST);     /* just in case */
594
595                 in6_ifstat_inc(ifp, ifs6_out_request);
596
597                 /*
598                  * Check if the outgoing interface conflicts with
599                  * the interface specified by ifi6_ifindex (if specified).
600                  * Note that loopback interface is always okay.
601                  * (this may happen when we are sending a packet to one of
602                  *  our own addresses.)
603                  */
604                 if (opt && opt->ip6po_pktinfo
605                  && opt->ip6po_pktinfo->ipi6_ifindex) {
606                         if (!(ifp->if_flags & IFF_LOOPBACK)
607                          && ifp->if_index != opt->ip6po_pktinfo->ipi6_ifindex) {
608                                 ip6stat.ip6s_noroute++;
609                                 in6_ifstat_inc(ifp, ifs6_out_discard);
610                                 error = EHOSTUNREACH;
611                                 goto bad;
612                         }
613                 }
614
615                 if (opt && opt->ip6po_hlim != -1)
616                         ip6->ip6_hlim = opt->ip6po_hlim & 0xff;
617         } else {
618                 /* Multicast */
619                 struct  in6_multi *in6m;
620
621                 m->m_flags = (m->m_flags & ~M_BCAST) | M_MCAST;
622
623                 /*
624                  * See if the caller provided any multicast options
625                  */
626                 ifp = NULL;
627                 if (im6o != NULL) {
628                         ip6->ip6_hlim = im6o->im6o_multicast_hlim;
629                         if (im6o->im6o_multicast_ifp != NULL)
630                                 ifp = im6o->im6o_multicast_ifp;
631                 } else
632                         ip6->ip6_hlim = ip6_defmcasthlim;
633
634                 /*
635                  * See if the caller provided the outgoing interface
636                  * as an ancillary data.
637                  * Boundary check for ifindex is assumed to be already done.
638                  */
639                 if (opt && opt->ip6po_pktinfo && opt->ip6po_pktinfo->ipi6_ifindex)
640                         ifp = ifindex2ifnet[opt->ip6po_pktinfo->ipi6_ifindex];
641
642                 /*
643                  * If the destination is a node-local scope multicast,
644                  * the packet should be loop-backed only.
645                  */
646                 if (IN6_IS_ADDR_MC_NODELOCAL(&ip6->ip6_dst)) {
647                         /*
648                          * If the outgoing interface is already specified,
649                          * it should be a loopback interface.
650                          */
651                         if (ifp && !(ifp->if_flags & IFF_LOOPBACK)) {
652                                 ip6stat.ip6s_badscope++;
653                                 error = ENETUNREACH; /* XXX: better error? */
654                                 /* XXX correct ifp? */
655                                 in6_ifstat_inc(ifp, ifs6_out_discard);
656                                 goto bad;
657                         } else {
658                                 ifp = &loif[0];
659                         }
660                 }
661
662                 if (opt && opt->ip6po_hlim != -1)
663                         ip6->ip6_hlim = opt->ip6po_hlim & 0xff;
664
665                 /*
666                  * If caller did not provide an interface lookup a
667                  * default in the routing table.  This is either a
668                  * default for the speicfied group (i.e. a host
669                  * route), or a multicast default (a route for the
670                  * ``net'' ff00::/8).
671                  */
672                 if (ifp == NULL) {
673                         if (ro->ro_rt == NULL) {
674                                 ro->ro_rt =
675                                   rtpurelookup((struct sockaddr *)&ro->ro_dst);
676                         }
677                         if (ro->ro_rt == NULL) {
678                                 ip6stat.ip6s_noroute++;
679                                 error = EHOSTUNREACH;
680                                 /* XXX in6_ifstat_inc(ifp, ifs6_out_discard) */
681                                 goto bad;
682                         }
683                         ia = ifatoia6(ro->ro_rt->rt_ifa);
684                         ifp = ro->ro_rt->rt_ifp;
685                         ro->ro_rt->rt_use++;
686                 }
687
688                 if (!(flags & IPV6_FORWARDING))
689                         in6_ifstat_inc(ifp, ifs6_out_request);
690                 in6_ifstat_inc(ifp, ifs6_out_mcast);
691
692                 /*
693                  * Confirm that the outgoing interface supports multicast.
694                  */
695                 if (!(ifp->if_flags & IFF_MULTICAST)) {
696                         ip6stat.ip6s_noroute++;
697                         in6_ifstat_inc(ifp, ifs6_out_discard);
698                         error = ENETUNREACH;
699                         goto bad;
700                 }
701                 IN6_LOOKUP_MULTI(ip6->ip6_dst, ifp, in6m);
702                 if (in6m != NULL &&
703                    (im6o == NULL || im6o->im6o_multicast_loop)) {
704                         /*
705                          * If we belong to the destination multicast group
706                          * on the outgoing interface, and the caller did not
707                          * forbid loopback, loop back a copy.
708                          */
709                         ip6_mloopback(ifp, m, dst);
710                 } else {
711                         /*
712                          * If we are acting as a multicast router, perform
713                          * multicast forwarding as if the packet had just
714                          * arrived on the interface to which we are about
715                          * to send.  The multicast forwarding function
716                          * recursively calls this function, using the
717                          * IPV6_FORWARDING flag to prevent infinite recursion.
718                          *
719                          * Multicasts that are looped back by ip6_mloopback(),
720                          * above, will be forwarded by the ip6_input() routine,
721                          * if necessary.
722                          */
723                         if (ip6_mrouter && !(flags & IPV6_FORWARDING)) {
724                                 if (ip6_mforward(ip6, ifp, m) != 0) {
725                                         m_freem(m);
726                                         goto done;
727                                 }
728                         }
729                 }
730                 /*
731                  * Multicasts with a hoplimit of zero may be looped back,
732                  * above, but must not be transmitted on a network.
733                  * Also, multicasts addressed to the loopback interface
734                  * are not sent -- the above call to ip6_mloopback() will
735                  * loop back a copy if this host actually belongs to the
736                  * destination group on the loopback interface.
737                  */
738                 if (ip6->ip6_hlim == 0 || (ifp->if_flags & IFF_LOOPBACK)) {
739                         m_freem(m);
740                         goto done;
741                 }
742         }
743
744         /*
745          * Fill the outgoing inteface to tell the upper layer
746          * to increment per-interface statistics.
747          */
748         if (ifpp)
749                 *ifpp = ifp;
750
751         /*
752          * Determine path MTU.
753          */
754         if (ro_pmtu != ro) {
755                 /* The first hop and the final destination may differ. */
756                 struct sockaddr_in6 *sin6_fin =
757                         (struct sockaddr_in6 *)&ro_pmtu->ro_dst;
758
759                 if (ro_pmtu->ro_rt != NULL &&
760                     (!(ro->ro_rt->rt_flags & RTF_UP) ||
761                      !IN6_ARE_ADDR_EQUAL(&sin6_fin->sin6_addr, &finaldst))) {
762                         RTFREE(ro_pmtu->ro_rt);
763                         ro_pmtu->ro_rt = NULL;
764                 }
765                 if (ro_pmtu->ro_rt == NULL) {
766                         bzero(sin6_fin, sizeof(*sin6_fin));
767                         sin6_fin->sin6_family = AF_INET6;
768                         sin6_fin->sin6_len = sizeof(struct sockaddr_in6);
769                         sin6_fin->sin6_addr = finaldst;
770
771                         rtalloc((struct route *)ro_pmtu);
772                 }
773         }
774         if (ro_pmtu->ro_rt != NULL) {
775                 u_int32_t ifmtu = ND_IFINFO(ifp)->linkmtu;
776
777                 mtu = ro_pmtu->ro_rt->rt_rmx.rmx_mtu;
778                 if (mtu > ifmtu || mtu == 0) {
779                         /*
780                          * The MTU on the route is larger than the MTU on
781                          * the interface!  This shouldn't happen, unless the
782                          * MTU of the interface has been changed after the
783                          * interface was brought up.  Change the MTU in the
784                          * route to match the interface MTU (as long as the
785                          * field isn't locked).
786                          *
787                          * if MTU on the route is 0, we need to fix the MTU.
788                          * this case happens with path MTU discovery timeouts.
789                          */
790                          mtu = ifmtu;
791                          if (!(ro_pmtu->ro_rt->rt_rmx.rmx_locks & RTV_MTU))
792                                  ro_pmtu->ro_rt->rt_rmx.rmx_mtu = mtu; /* XXX */
793                 }
794         } else {
795                 mtu = ND_IFINFO(ifp)->linkmtu;
796         }
797
798         /*
799          * advanced API (IPV6_USE_MIN_MTU) overrides mtu setting
800          */
801         if ((flags & IPV6_MINMTU) != 0 && mtu > IPV6_MMTU)
802                 mtu = IPV6_MMTU;
803
804         /* Fake scoped addresses */
805         if ((ifp->if_flags & IFF_LOOPBACK) != 0) {
806                 /*
807                  * If source or destination address is a scoped address, and
808                  * the packet is going to be sent to a loopback interface,
809                  * we should keep the original interface.
810                  */
811
812                 /*
813                  * XXX: this is a very experimental and temporary solution.
814                  * We eventually have sockaddr_in6 and use the sin6_scope_id
815                  * field of the structure here.
816                  * We rely on the consistency between two scope zone ids
817                  * of source and destination, which should already be assured.
818                  * Larger scopes than link will be supported in the future.
819                  */
820                 origifp = NULL;
821                 if (IN6_IS_SCOPE_LINKLOCAL(&ip6->ip6_src))
822                         origifp = ifindex2ifnet[ntohs(ip6->ip6_src.s6_addr16[1])];
823                 else if (IN6_IS_SCOPE_LINKLOCAL(&ip6->ip6_dst))
824                         origifp = ifindex2ifnet[ntohs(ip6->ip6_dst.s6_addr16[1])];
825                 /*
826                  * XXX: origifp can be NULL even in those two cases above.
827                  * For example, if we remove the (only) link-local address
828                  * from the loopback interface, and try to send a link-local
829                  * address without link-id information.  Then the source
830                  * address is ::1, and the destination address is the
831                  * link-local address with its s6_addr16[1] being zero.
832                  * What is worse, if the packet goes to the loopback interface
833                  * by a default rejected route, the null pointer would be
834                  * passed to looutput, and the kernel would hang.
835                  * The following last resort would prevent such disaster.
836                  */
837                 if (origifp == NULL)
838                         origifp = ifp;
839         }
840         else
841                 origifp = ifp;
842         /*
843          * clear embedded scope identifiers if necessary.
844          * in6_clearscope will touch the addresses only when necessary.
845          */
846         in6_clearscope(&ip6->ip6_src);
847         in6_clearscope(&ip6->ip6_dst);
848
849         /*
850          * Check with the firewall...
851          */
852         if (ip6_fw_enable && ip6_fw_chk_ptr) {
853                 u_short port = 0;
854                 m->m_pkthdr.rcvif = NULL;       /* XXX */
855                 /* If ipfw says divert, we have to just drop packet */
856                 if ((*ip6_fw_chk_ptr)(&ip6, ifp, &port, &m)) {
857                         m_freem(m);
858                         goto done;
859                 }
860                 if (!m) {
861                         error = EACCES;
862                         goto done;
863                 }
864         }
865
866         /*
867          * If the outgoing packet contains a hop-by-hop options header,
868          * it must be examined and processed even by the source node.
869          * (RFC 2460, section 4.)
870          */
871         if (exthdrs.ip6e_hbh) {
872                 struct ip6_hbh *hbh = mtod(exthdrs.ip6e_hbh, struct ip6_hbh *);
873                 u_int32_t dummy1; /* XXX unused */
874                 u_int32_t dummy2; /* XXX unused */
875
876 #ifdef DIAGNOSTIC
877                 if ((hbh->ip6h_len + 1) << 3 > exthdrs.ip6e_hbh->m_len)
878                         panic("ip6e_hbh is not continuous");
879 #endif
880                 /*
881                  *  XXX: if we have to send an ICMPv6 error to the sender,
882                  *       we need the M_LOOP flag since icmp6_error() expects
883                  *       the IPv6 and the hop-by-hop options header are
884                  *       continuous unless the flag is set.
885                  */
886                 m->m_flags |= M_LOOP;
887                 m->m_pkthdr.rcvif = ifp;
888                 if (ip6_process_hopopts(m,
889                                         (u_int8_t *)(hbh + 1),
890                                         ((hbh->ip6h_len + 1) << 3) -
891                                         sizeof(struct ip6_hbh),
892                                         &dummy1, &dummy2) < 0) {
893                         /* m was already freed at this point */
894                         error = EINVAL;/* better error? */
895                         goto done;
896                 }
897                 m->m_flags &= ~M_LOOP; /* XXX */
898                 m->m_pkthdr.rcvif = NULL;
899         }
900
901         /*
902          * Run through list of hooks for output packets.
903          */
904         if (pfil_has_hooks(&inet6_pfil_hook)) {
905                 error = pfil_run_hooks(&inet6_pfil_hook, &m, ifp, PFIL_OUT);
906                 if (error != 0 || m == NULL)
907                         goto done;
908                 ip6 = mtod(m, struct ip6_hdr *);
909         }
910
911         /*
912          * Send the packet to the outgoing interface.
913          * If necessary, do IPv6 fragmentation before sending.
914          */
915         tlen = m->m_pkthdr.len;
916         if (tlen <= mtu
917 #ifdef notyet
918             /*
919              * On any link that cannot convey a 1280-octet packet in one piece,
920              * link-specific fragmentation and reassembly must be provided at
921              * a layer below IPv6. [RFC 2460, sec.5]
922              * Thus if the interface has ability of link-level fragmentation,
923              * we can just send the packet even if the packet size is
924              * larger than the link's MTU.
925              * XXX: IFF_FRAGMENTABLE (or such) flag has not been defined yet...
926              */
927
928             || ifp->if_flags & IFF_FRAGMENTABLE
929 #endif
930             )
931         {
932                 /* Record statistics for this interface address. */
933                 if (ia && !(flags & IPV6_FORWARDING)) {
934                         ia->ia_ifa.if_opackets++;
935                         ia->ia_ifa.if_obytes += m->m_pkthdr.len;
936                 }
937 #ifdef IPSEC
938                 /* clean ipsec history once it goes out of the node */
939                 ipsec_delaux(m);
940 #endif
941                 error = nd6_output(ifp, origifp, m, dst, ro->ro_rt);
942                 goto done;
943         } else if (mtu < IPV6_MMTU) {
944                 /*
945                  * note that path MTU is never less than IPV6_MMTU
946                  * (see icmp6_input).
947                  */
948                 error = EMSGSIZE;
949                 in6_ifstat_inc(ifp, ifs6_out_fragfail);
950                 goto bad;
951         } else if (ip6->ip6_plen == 0) { /* jumbo payload cannot be fragmented */
952                 error = EMSGSIZE;
953                 in6_ifstat_inc(ifp, ifs6_out_fragfail);
954                 goto bad;
955         } else {
956                 struct mbuf **mnext, *m_frgpart;
957                 struct ip6_frag *ip6f;
958                 u_int32_t id = htonl(ip6_id++);
959                 u_char nextproto;
960
961                 /*
962                  * Too large for the destination or interface;
963                  * fragment if possible.
964                  * Must be able to put at least 8 bytes per fragment.
965                  */
966                 hlen = unfragpartlen;
967                 if (mtu > IPV6_MAXPACKET)
968                         mtu = IPV6_MAXPACKET;
969
970                 len = (mtu - hlen - sizeof(struct ip6_frag)) & ~7;
971                 if (len < 8) {
972                         error = EMSGSIZE;
973                         in6_ifstat_inc(ifp, ifs6_out_fragfail);
974                         goto bad;
975                 }
976
977                 mnext = &m->m_nextpkt;
978
979                 /*
980                  * Change the next header field of the last header in the
981                  * unfragmentable part.
982                  */
983                 if (exthdrs.ip6e_rthdr) {
984                         nextproto = *mtod(exthdrs.ip6e_rthdr, u_char *);
985                         *mtod(exthdrs.ip6e_rthdr, u_char *) = IPPROTO_FRAGMENT;
986                 } else if (exthdrs.ip6e_dest1) {
987                         nextproto = *mtod(exthdrs.ip6e_dest1, u_char *);
988                         *mtod(exthdrs.ip6e_dest1, u_char *) = IPPROTO_FRAGMENT;
989                 } else if (exthdrs.ip6e_hbh) {
990                         nextproto = *mtod(exthdrs.ip6e_hbh, u_char *);
991                         *mtod(exthdrs.ip6e_hbh, u_char *) = IPPROTO_FRAGMENT;
992                 } else {
993                         nextproto = ip6->ip6_nxt;
994                         ip6->ip6_nxt = IPPROTO_FRAGMENT;
995                 }
996
997                 /*
998                  * Loop through length of segment after first fragment,
999                  * make new header and copy data of each part and link onto
1000                  * chain.
1001                  */
1002                 m0 = m;
1003                 for (off = hlen; off < tlen; off += len) {
1004                         MGETHDR(m, MB_DONTWAIT, MT_HEADER);
1005                         if (!m) {
1006                                 error = ENOBUFS;
1007                                 ip6stat.ip6s_odropped++;
1008                                 goto sendorfree;
1009                         }
1010                         m->m_pkthdr.rcvif = NULL;
1011                         m->m_flags = m0->m_flags & M_COPYFLAGS;
1012                         *mnext = m;
1013                         mnext = &m->m_nextpkt;
1014                         m->m_data += max_linkhdr;
1015                         mhip6 = mtod(m, struct ip6_hdr *);
1016                         *mhip6 = *ip6;
1017                         m->m_len = sizeof(*mhip6);
1018                         error = ip6_insertfraghdr(m0, m, hlen, &ip6f);
1019                         if (error) {
1020                                 ip6stat.ip6s_odropped++;
1021                                 goto sendorfree;
1022                         }
1023                         ip6f->ip6f_offlg = htons((u_short)((off - hlen) & ~7));
1024                         if (off + len >= tlen)
1025                                 len = tlen - off;
1026                         else
1027                                 ip6f->ip6f_offlg |= IP6F_MORE_FRAG;
1028                         mhip6->ip6_plen = htons((u_short)(len + hlen +
1029                                                           sizeof(*ip6f) -
1030                                                           sizeof(struct ip6_hdr)));
1031                         if ((m_frgpart = m_copy(m0, off, len)) == NULL) {
1032                                 error = ENOBUFS;
1033                                 ip6stat.ip6s_odropped++;
1034                                 goto sendorfree;
1035                         }
1036                         m_cat(m, m_frgpart);
1037                         m->m_pkthdr.len = len + hlen + sizeof(*ip6f);
1038                         m->m_pkthdr.rcvif = (struct ifnet *)0;
1039                         ip6f->ip6f_reserved = 0;
1040                         ip6f->ip6f_ident = id;
1041                         ip6f->ip6f_nxt = nextproto;
1042                         ip6stat.ip6s_ofragments++;
1043                         in6_ifstat_inc(ifp, ifs6_out_fragcreat);
1044                 }
1045
1046                 in6_ifstat_inc(ifp, ifs6_out_fragok);
1047         }
1048
1049         /*
1050          * Remove leading garbages.
1051          */
1052 sendorfree:
1053         m = m0->m_nextpkt;
1054         m0->m_nextpkt = 0;
1055         m_freem(m0);
1056         for (m0 = m; m; m = m0) {
1057                 m0 = m->m_nextpkt;
1058                 m->m_nextpkt = 0;
1059                 if (error == 0) {
1060                         /* Record statistics for this interface address. */
1061                         if (ia) {
1062                                 ia->ia_ifa.if_opackets++;
1063                                 ia->ia_ifa.if_obytes += m->m_pkthdr.len;
1064                         }
1065 #ifdef IPSEC
1066                         /* clean ipsec history once it goes out of the node */
1067                         ipsec_delaux(m);
1068 #endif
1069                         error = nd6_output(ifp, origifp, m, dst, ro->ro_rt);
1070                 } else
1071                         m_freem(m);
1072         }
1073
1074         if (error == 0)
1075                 ip6stat.ip6s_fragmented++;
1076
1077 done:
1078         if (ro == &ip6route && ro->ro_rt) { /* brace necessary for RTFREE */
1079                 RTFREE(ro->ro_rt);
1080         } else if (ro_pmtu == &ip6route && ro_pmtu->ro_rt) {
1081                 RTFREE(ro_pmtu->ro_rt);
1082         }
1083
1084 #ifdef IPSEC
1085         if (sp != NULL)
1086                 key_freesp(sp);
1087 #endif /* IPSEC */
1088 #ifdef FAST_IPSEC
1089         if (sp != NULL)
1090                 KEY_FREESP(&sp);
1091 #endif /* FAST_IPSEC */
1092
1093         return (error);
1094
1095 freehdrs:
1096         m_freem(exthdrs.ip6e_hbh);      /* m_freem will check if mbuf is 0 */
1097         m_freem(exthdrs.ip6e_dest1);
1098         m_freem(exthdrs.ip6e_rthdr);
1099         m_freem(exthdrs.ip6e_dest2);
1100         /* fall through */
1101 bad:
1102         m_freem(m);
1103         goto done;
1104 }
1105
1106 static int
1107 copyexthdr(void *h, struct mbuf **mp)
1108 {
1109         struct ip6_ext *hdr = h;
1110         int hlen;
1111         struct mbuf *m;
1112
1113         if (hdr == NULL)
1114                 return 0;
1115
1116         hlen = (hdr->ip6e_len + 1) * 8;
1117         if (hlen > MCLBYTES)
1118                 return ENOBUFS; /* XXX */
1119
1120         m = m_getb(hlen, MB_DONTWAIT, MT_DATA, 0);
1121         if (!m)
1122                 return ENOBUFS;
1123         m->m_len = hlen;
1124
1125         bcopy(hdr, mtod(m, caddr_t), hlen);
1126
1127         *mp = m;
1128         return 0;
1129 }
1130
1131 /*
1132  * Insert jumbo payload option.
1133  */
1134 static int
1135 ip6_insert_jumboopt(struct ip6_exthdrs *exthdrs, u_int32_t plen)
1136 {
1137         struct mbuf *mopt;
1138         u_char *optbuf;
1139         u_int32_t v;
1140
1141 #define JUMBOOPTLEN     8       /* length of jumbo payload option and padding */
1142
1143         /*
1144          * If there is no hop-by-hop options header, allocate new one.
1145          * If there is one but it doesn't have enough space to store the
1146          * jumbo payload option, allocate a cluster to store the whole options.
1147          * Otherwise, use it to store the options.
1148          */
1149         if (exthdrs->ip6e_hbh == NULL) {
1150                 MGET(mopt, MB_DONTWAIT, MT_DATA);
1151                 if (mopt == NULL)
1152                         return (ENOBUFS);
1153                 mopt->m_len = JUMBOOPTLEN;
1154                 optbuf = mtod(mopt, u_char *);
1155                 optbuf[1] = 0;  /* = ((JUMBOOPTLEN) >> 3) - 1 */
1156                 exthdrs->ip6e_hbh = mopt;
1157         } else {
1158                 struct ip6_hbh *hbh;
1159
1160                 mopt = exthdrs->ip6e_hbh;
1161                 if (M_TRAILINGSPACE(mopt) < JUMBOOPTLEN) {
1162                         /*
1163                          * XXX assumption:
1164                          * - exthdrs->ip6e_hbh is not referenced from places
1165                          *   other than exthdrs.
1166                          * - exthdrs->ip6e_hbh is not an mbuf chain.
1167                          */
1168                         int oldoptlen = mopt->m_len;
1169                         struct mbuf *n;
1170
1171                         /*
1172                          * XXX: give up if the whole (new) hbh header does
1173                          * not fit even in an mbuf cluster.
1174                          */
1175                         if (oldoptlen + JUMBOOPTLEN > MCLBYTES)
1176                                 return (ENOBUFS);
1177
1178                         /*
1179                          * As a consequence, we must always prepare a cluster
1180                          * at this point.
1181                          */
1182                         n = m_getcl(MB_DONTWAIT, MT_DATA, 0);
1183                         if (!n)
1184                                 return (ENOBUFS);
1185                         n->m_len = oldoptlen + JUMBOOPTLEN;
1186                         bcopy(mtod(mopt, caddr_t), mtod(n, caddr_t), oldoptlen);
1187                         optbuf = mtod(n, caddr_t) + oldoptlen;
1188                         m_freem(mopt);
1189                         mopt = exthdrs->ip6e_hbh = n;
1190                 } else {
1191                         optbuf = mtod(mopt, u_char *) + mopt->m_len;
1192                         mopt->m_len += JUMBOOPTLEN;
1193                 }
1194                 optbuf[0] = IP6OPT_PADN;
1195                 optbuf[1] = 1;
1196
1197                 /*
1198                  * Adjust the header length according to the pad and
1199                  * the jumbo payload option.
1200                  */
1201                 hbh = mtod(mopt, struct ip6_hbh *);
1202                 hbh->ip6h_len += (JUMBOOPTLEN >> 3);
1203         }
1204
1205         /* fill in the option. */
1206         optbuf[2] = IP6OPT_JUMBO;
1207         optbuf[3] = 4;
1208         v = (u_int32_t)htonl(plen + JUMBOOPTLEN);
1209         bcopy(&v, &optbuf[4], sizeof(u_int32_t));
1210
1211         /* finally, adjust the packet header length */
1212         exthdrs->ip6e_ip6->m_pkthdr.len += JUMBOOPTLEN;
1213
1214         return (0);
1215 #undef JUMBOOPTLEN
1216 }
1217
1218 /*
1219  * Insert fragment header and copy unfragmentable header portions.
1220  */
1221 static int
1222 ip6_insertfraghdr(struct mbuf *m0, struct mbuf *m, int hlen,
1223                   struct ip6_frag **frghdrp)
1224 {
1225         struct mbuf *n, *mlast;
1226
1227         if (hlen > sizeof(struct ip6_hdr)) {
1228                 n = m_copym(m0, sizeof(struct ip6_hdr),
1229                             hlen - sizeof(struct ip6_hdr), MB_DONTWAIT);
1230                 if (n == NULL)
1231                         return (ENOBUFS);
1232                 m->m_next = n;
1233         } else
1234                 n = m;
1235
1236         /* Search for the last mbuf of unfragmentable part. */
1237         for (mlast = n; mlast->m_next; mlast = mlast->m_next)
1238                 ;
1239
1240         if (!(mlast->m_flags & M_EXT) &&
1241             M_TRAILINGSPACE(mlast) >= sizeof(struct ip6_frag)) {
1242                 /* use the trailing space of the last mbuf for the fragment hdr */
1243                 *frghdrp =
1244                         (struct ip6_frag *)(mtod(mlast, caddr_t) + mlast->m_len);
1245                 mlast->m_len += sizeof(struct ip6_frag);
1246                 m->m_pkthdr.len += sizeof(struct ip6_frag);
1247         } else {
1248                 /* allocate a new mbuf for the fragment header */
1249                 struct mbuf *mfrg;
1250
1251                 MGET(mfrg, MB_DONTWAIT, MT_DATA);
1252                 if (mfrg == NULL)
1253                         return (ENOBUFS);
1254                 mfrg->m_len = sizeof(struct ip6_frag);
1255                 *frghdrp = mtod(mfrg, struct ip6_frag *);
1256                 mlast->m_next = mfrg;
1257         }
1258
1259         return (0);
1260 }
1261
1262 /*
1263  * IP6 socket option processing.
1264  */
1265 int
1266 ip6_ctloutput(struct socket *so, struct sockopt *sopt)
1267 {
1268         int privileged;
1269         struct inpcb *in6p = so->so_pcb;
1270         int error, optval;
1271         int level, op, optname;
1272         int optlen;
1273         struct thread *td;
1274
1275         if (sopt) {
1276                 level = sopt->sopt_level;
1277                 op = sopt->sopt_dir;
1278                 optname = sopt->sopt_name;
1279                 optlen = sopt->sopt_valsize;
1280                 td = sopt->sopt_td;
1281         } else {
1282                 panic("ip6_ctloutput: arg soopt is NULL");
1283                 /* NOT REACHED */
1284                 td = NULL;
1285         }
1286         error = optval = 0;
1287
1288         privileged = (td == NULL || suser(td)) ? 0 : 1;
1289
1290         if (level == IPPROTO_IPV6) {
1291                 switch (op) {
1292
1293                 case SOPT_SET:
1294                         switch (optname) {
1295                         case IPV6_PKTOPTIONS:
1296                         {
1297                                 struct mbuf *m;
1298
1299                                 error = soopt_getm(sopt, &m); /* XXX */
1300                                 if (error != NULL)
1301                                         break;
1302                                 error = soopt_mcopyin(sopt, m); /* XXX */
1303                                 if (error != NULL)
1304                                         break;
1305                                 error = ip6_pcbopts(&in6p->in6p_outputopts,
1306                                                     m, so, sopt);
1307                                 m_freem(m); /* XXX */
1308                                 break;
1309                         }
1310
1311                         /*
1312                          * Use of some Hop-by-Hop options or some
1313                          * Destination options, might require special
1314                          * privilege.  That is, normal applications
1315                          * (without special privilege) might be forbidden
1316                          * from setting certain options in outgoing packets,
1317                          * and might never see certain options in received
1318                          * packets. [RFC 2292 Section 6]
1319                          * KAME specific note:
1320                          *  KAME prevents non-privileged users from sending or
1321                          *  receiving ANY hbh/dst options in order to avoid
1322                          *  overhead of parsing options in the kernel.
1323                          */
1324                         case IPV6_UNICAST_HOPS:
1325                         case IPV6_CHECKSUM:
1326                         case IPV6_FAITH:
1327
1328                         case IPV6_V6ONLY:
1329                                 if (optlen != sizeof(int)) {
1330                                         error = EINVAL;
1331                                         break;
1332                                 }
1333                                 error = sooptcopyin(sopt, &optval,
1334                                         sizeof optval, sizeof optval);
1335                                 if (error)
1336                                         break;
1337                                 switch (optname) {
1338
1339                                 case IPV6_UNICAST_HOPS:
1340                                         if (optval < -1 || optval >= 256)
1341                                                 error = EINVAL;
1342                                         else {
1343                                                 /* -1 = kernel default */
1344                                                 in6p->in6p_hops = optval;
1345
1346                                                 if ((in6p->in6p_vflag &
1347                                                      INP_IPV4) != 0)
1348                                                         in6p->inp_ip_ttl = optval;
1349                                         }
1350                                         break;
1351 #define OPTSET(bit) \
1352 do { \
1353         if (optval) \
1354                 in6p->in6p_flags |= (bit); \
1355         else \
1356                 in6p->in6p_flags &= ~(bit); \
1357 } while (0)
1358 #define OPTBIT(bit) (in6p->in6p_flags & (bit) ? 1 : 0)
1359
1360                                 case IPV6_CHECKSUM:
1361                                         in6p->in6p_cksum = optval;
1362                                         break;
1363
1364                                 case IPV6_FAITH:
1365                                         OPTSET(IN6P_FAITH);
1366                                         break;
1367
1368                                 case IPV6_V6ONLY:
1369                                         /*
1370                                          * make setsockopt(IPV6_V6ONLY)
1371                                          * available only prior to bind(2).
1372                                          * see ipng mailing list, Jun 22 2001.
1373                                          */
1374                                         if (in6p->in6p_lport ||
1375                                             !IN6_IS_ADDR_UNSPECIFIED(&in6p->in6p_laddr))
1376                                         {
1377                                                 error = EINVAL;
1378                                                 break;
1379                                         }
1380                                         OPTSET(IN6P_IPV6_V6ONLY);
1381                                         if (optval)
1382                                                 in6p->in6p_vflag &= ~INP_IPV4;
1383                                         else
1384                                                 in6p->in6p_vflag |= INP_IPV4;
1385                                         break;
1386                                 }
1387                                 break;
1388
1389                         case IPV6_PKTINFO:
1390                         case IPV6_HOPLIMIT:
1391                         case IPV6_HOPOPTS:
1392                         case IPV6_DSTOPTS:
1393                         case IPV6_RTHDR:
1394                                 /* RFC 2292 */
1395                                 if (optlen != sizeof(int)) {
1396                                         error = EINVAL;
1397                                         break;
1398                                 }
1399                                 error = sooptcopyin(sopt, &optval,
1400                                         sizeof optval, sizeof optval);
1401                                 if (error)
1402                                         break;
1403                                 switch (optname) {
1404                                 case IPV6_PKTINFO:
1405                                         OPTSET(IN6P_PKTINFO);
1406                                         break;
1407                                 case IPV6_HOPLIMIT:
1408                                         OPTSET(IN6P_HOPLIMIT);
1409                                         break;
1410                                 case IPV6_HOPOPTS:
1411                                         /*
1412                                          * Check super-user privilege.
1413                                          * See comments for IPV6_RECVHOPOPTS.
1414                                          */
1415                                         if (!privileged)
1416                                                 return (EPERM);
1417                                         OPTSET(IN6P_HOPOPTS);
1418                                         break;
1419                                 case IPV6_DSTOPTS:
1420                                         if (!privileged)
1421                                                 return (EPERM);
1422                                         OPTSET(IN6P_DSTOPTS|IN6P_RTHDRDSTOPTS); /* XXX */
1423                                         break;
1424                                 case IPV6_RTHDR:
1425                                         OPTSET(IN6P_RTHDR);
1426                                         break;
1427                                 }
1428                                 break;
1429 #undef OPTSET
1430
1431                         case IPV6_MULTICAST_IF:
1432                         case IPV6_MULTICAST_HOPS:
1433                         case IPV6_MULTICAST_LOOP:
1434                         case IPV6_JOIN_GROUP:
1435                         case IPV6_LEAVE_GROUP:
1436                             {
1437                                 struct mbuf *m;
1438                                 if (sopt->sopt_valsize > MLEN) {
1439                                         error = EMSGSIZE;
1440                                         break;
1441                                 }
1442                                 /* XXX */
1443                                 MGET(m, sopt->sopt_td ? MB_WAIT : MB_DONTWAIT, MT_HEADER);
1444                                 if (m == NULL) {
1445                                         error = ENOBUFS;
1446                                         break;
1447                                 }
1448                                 m->m_len = sopt->sopt_valsize;
1449                                 error = sooptcopyin(sopt, mtod(m, char *),
1450                                                     m->m_len, m->m_len);
1451                                 error = ip6_setmoptions(sopt->sopt_name,
1452                                                         &in6p->in6p_moptions,
1453                                                         m);
1454                                 m_free(m);
1455                             }
1456                                 break;
1457
1458                         case IPV6_PORTRANGE:
1459                                 error = sooptcopyin(sopt, &optval,
1460                                     sizeof optval, sizeof optval);
1461                                 if (error)
1462                                         break;
1463
1464                                 switch (optval) {
1465                                 case IPV6_PORTRANGE_DEFAULT:
1466                                         in6p->in6p_flags &= ~(IN6P_LOWPORT);
1467                                         in6p->in6p_flags &= ~(IN6P_HIGHPORT);
1468                                         break;
1469
1470                                 case IPV6_PORTRANGE_HIGH:
1471                                         in6p->in6p_flags &= ~(IN6P_LOWPORT);
1472                                         in6p->in6p_flags |= IN6P_HIGHPORT;
1473                                         break;
1474
1475                                 case IPV6_PORTRANGE_LOW:
1476                                         in6p->in6p_flags &= ~(IN6P_HIGHPORT);
1477                                         in6p->in6p_flags |= IN6P_LOWPORT;
1478                                         break;
1479
1480                                 default:
1481                                         error = EINVAL;
1482                                         break;
1483                                 }
1484                                 break;
1485
1486 #if defined(IPSEC) || defined(FAST_IPSEC)
1487                         case IPV6_IPSEC_POLICY:
1488                             {
1489                                 caddr_t req = NULL;
1490                                 size_t len = 0;
1491                                 struct mbuf *m;
1492
1493                                 if ((error = soopt_getm(sopt, &m)) != 0) /* XXX */
1494                                         break;
1495                                 if ((error = soopt_mcopyin(sopt, m)) != 0) /* XXX */
1496                                         break;
1497                                 if (m) {
1498                                         req = mtod(m, caddr_t);
1499                                         len = m->m_len;
1500                                 }
1501                                 error = ipsec6_set_policy(in6p, optname, req,
1502                                                           len, privileged);
1503                                 m_freem(m);
1504                             }
1505                                 break;
1506 #endif /* KAME IPSEC */
1507
1508                         case IPV6_FW_ADD:
1509                         case IPV6_FW_DEL:
1510                         case IPV6_FW_FLUSH:
1511                         case IPV6_FW_ZERO:
1512                             {
1513                                 struct mbuf *m;
1514                                 struct mbuf **mp = &m;
1515
1516                                 if (ip6_fw_ctl_ptr == NULL)
1517                                         return EINVAL;
1518                                 /* XXX */
1519                                 if ((error = soopt_getm(sopt, &m)) != 0)
1520                                         break;
1521                                 /* XXX */
1522                                 if ((error = soopt_mcopyin(sopt, m)) != 0)
1523                                         break;
1524                                 error = (*ip6_fw_ctl_ptr)(optname, mp);
1525                                 m = *mp;
1526                             }
1527                                 break;
1528
1529                         default:
1530                                 error = ENOPROTOOPT;
1531                                 break;
1532                         }
1533                         break;
1534
1535                 case SOPT_GET:
1536                         switch (optname) {
1537
1538                         case IPV6_PKTOPTIONS:
1539                                 if (in6p->in6p_options) {
1540                                         struct mbuf *m;
1541                                         m = m_copym(in6p->in6p_options,
1542                                             0, M_COPYALL, MB_WAIT);
1543                                         error = soopt_mcopyout(sopt, m);
1544                                         if (error == 0)
1545                                                 m_freem(m);
1546                                 } else
1547                                         sopt->sopt_valsize = 0;
1548                                 break;
1549
1550                         case IPV6_UNICAST_HOPS:
1551                         case IPV6_CHECKSUM:
1552
1553                         case IPV6_FAITH:
1554                         case IPV6_V6ONLY:
1555                         case IPV6_PORTRANGE:
1556                                 switch (optname) {
1557
1558                                 case IPV6_UNICAST_HOPS:
1559                                         optval = in6p->in6p_hops;
1560                                         break;
1561
1562                                 case IPV6_CHECKSUM:
1563                                         optval = in6p->in6p_cksum;
1564                                         break;
1565
1566                                 case IPV6_FAITH:
1567                                         optval = OPTBIT(IN6P_FAITH);
1568                                         break;
1569
1570                                 case IPV6_V6ONLY:
1571                                         optval = OPTBIT(IN6P_IPV6_V6ONLY);
1572                                         break;
1573
1574                                 case IPV6_PORTRANGE:
1575                                     {
1576                                         int flags;
1577                                         flags = in6p->in6p_flags;
1578                                         if (flags & IN6P_HIGHPORT)
1579                                                 optval = IPV6_PORTRANGE_HIGH;
1580                                         else if (flags & IN6P_LOWPORT)
1581                                                 optval = IPV6_PORTRANGE_LOW;
1582                                         else
1583                                                 optval = 0;
1584                                         break;
1585                                     }
1586                                 }
1587                                 error = sooptcopyout(sopt, &optval,
1588                                         sizeof optval);
1589                                 break;
1590
1591                         case IPV6_PKTINFO:
1592                         case IPV6_HOPLIMIT:
1593                         case IPV6_HOPOPTS:
1594                         case IPV6_RTHDR:
1595                         case IPV6_DSTOPTS:
1596                                 if (optname == IPV6_HOPOPTS ||
1597                                     optname == IPV6_DSTOPTS ||
1598                                     !privileged)
1599                                         return (EPERM);
1600                                 switch (optname) {
1601                                 case IPV6_PKTINFO:
1602                                         optval = OPTBIT(IN6P_PKTINFO);
1603                                         break;
1604                                 case IPV6_HOPLIMIT:
1605                                         optval = OPTBIT(IN6P_HOPLIMIT);
1606                                         break;
1607                                 case IPV6_HOPOPTS:
1608                                         if (!privileged)
1609                                                 return (EPERM);
1610                                         optval = OPTBIT(IN6P_HOPOPTS);
1611                                         break;
1612                                 case IPV6_RTHDR:
1613                                         optval = OPTBIT(IN6P_RTHDR);
1614                                         break;
1615                                 case IPV6_DSTOPTS:
1616                                         if (!privileged)
1617                                                 return (EPERM);
1618                                         optval = OPTBIT(IN6P_DSTOPTS|IN6P_RTHDRDSTOPTS);
1619                                         break;
1620                                 }
1621                                 error = sooptcopyout(sopt, &optval,
1622                                         sizeof optval);
1623                                 break;
1624
1625                         case IPV6_MULTICAST_IF:
1626                         case IPV6_MULTICAST_HOPS:
1627                         case IPV6_MULTICAST_LOOP:
1628                         case IPV6_JOIN_GROUP:
1629                         case IPV6_LEAVE_GROUP:
1630                             {
1631                                 struct mbuf *m;
1632                                 error = ip6_getmoptions(sopt->sopt_name,
1633                                                 in6p->in6p_moptions, &m);
1634                                 if (error == 0)
1635                                         error = sooptcopyout(sopt,
1636                                                 mtod(m, char *), m->m_len);
1637                                 m_freem(m);
1638                             }
1639                                 break;
1640
1641 #if defined(IPSEC) || defined(FAST_IPSEC)
1642                         case IPV6_IPSEC_POLICY:
1643                           {
1644                                 caddr_t req = NULL;
1645                                 size_t len = 0;
1646                                 struct mbuf *m = NULL;
1647                                 struct mbuf **mp = &m;
1648
1649                                 error = soopt_getm(sopt, &m); /* XXX */
1650                                 if (error != NULL)
1651                                         break;
1652                                 error = soopt_mcopyin(sopt, m); /* XXX */
1653                                 if (error != NULL)
1654                                         break;
1655                                 if (m) {
1656                                         req = mtod(m, caddr_t);
1657                                         len = m->m_len;
1658                                 }
1659                                 error = ipsec6_get_policy(in6p, req, len, mp);
1660                                 if (error == 0)
1661                                         error = soopt_mcopyout(sopt, m); /*XXX*/
1662                                 if (error == 0 && m != NULL)
1663                                         m_freem(m);
1664                                 break;
1665                           }
1666 #endif /* KAME IPSEC */
1667
1668                         case IPV6_FW_GET:
1669                           {
1670                                 struct mbuf *m;
1671                                 struct mbuf **mp = &m;
1672
1673                                 if (ip6_fw_ctl_ptr == NULL)
1674                                 {
1675                                         return EINVAL;
1676                                 }
1677                                 error = (*ip6_fw_ctl_ptr)(optname, mp);
1678                                 if (error == 0)
1679                                         error = soopt_mcopyout(sopt, m); /* XXX */
1680                                 if (error == 0 && m != NULL)
1681                                         m_freem(m);
1682                           }
1683                                 break;
1684
1685                         default:
1686                                 error = ENOPROTOOPT;
1687                                 break;
1688                         }
1689                         break;
1690                 }
1691         } else {
1692                 error = EINVAL;
1693         }
1694         return (error);
1695 }
1696
1697 /*
1698  * Set up IP6 options in pcb for insertion in output packets or
1699  * specifying behavior of outgoing packets.
1700  */
1701 static int
1702 ip6_pcbopts(struct ip6_pktopts **pktopt, struct mbuf *m, struct socket *so,
1703             struct sockopt *sopt)
1704 {
1705         struct ip6_pktopts *opt = *pktopt;
1706         int error = 0;
1707         struct thread *td = sopt->sopt_td;
1708         int priv = 0;
1709
1710         /* turn off any old options. */
1711         if (opt) {
1712 #ifdef DIAGNOSTIC
1713                 if (opt->ip6po_pktinfo || opt->ip6po_nexthop ||
1714                     opt->ip6po_hbh || opt->ip6po_dest1 || opt->ip6po_dest2 ||
1715                     opt->ip6po_rhinfo.ip6po_rhi_rthdr)
1716                         printf("ip6_pcbopts: all specified options are cleared.\n");
1717 #endif
1718                 ip6_clearpktopts(opt, 1, -1);
1719         } else
1720                 opt = kmalloc(sizeof(*opt), M_IP6OPT, M_WAITOK);
1721         *pktopt = NULL;
1722
1723         if (m == NULL || m->m_len == 0) {
1724                 /*
1725                  * Only turning off any previous options, regardless of
1726                  * whether the opt is just created or given.
1727                  */
1728                 kfree(opt, M_IP6OPT);
1729                 return (0);
1730         }
1731
1732         /*  set options specified by user. */
1733         if (suser(td) == 0)
1734                 priv = 1;
1735         if ((error = ip6_setpktoptions(m, opt, priv, 1)) != 0) {
1736                 ip6_clearpktopts(opt, 1, -1); /* XXX: discard all options */
1737                 kfree(opt, M_IP6OPT);
1738                 return (error);
1739         }
1740         *pktopt = opt;
1741         return (0);
1742 }
1743
1744 /*
1745  * initialize ip6_pktopts.  beware that there are non-zero default values in
1746  * the struct.
1747  */
1748 void
1749 init_ip6pktopts(struct ip6_pktopts *opt)
1750 {
1751
1752         bzero(opt, sizeof(*opt));
1753         opt->ip6po_hlim = -1;   /* -1 means default hop limit */
1754 }
1755
1756 void
1757 ip6_clearpktopts(struct ip6_pktopts *pktopt, int needfree, int optname)
1758 {
1759         if (pktopt == NULL)
1760                 return;
1761
1762         if (optname == -1) {
1763                 if (needfree && pktopt->ip6po_pktinfo)
1764                         kfree(pktopt->ip6po_pktinfo, M_IP6OPT);
1765                 pktopt->ip6po_pktinfo = NULL;
1766         }
1767         if (optname == -1)
1768                 pktopt->ip6po_hlim = -1;
1769         if (optname == -1) {
1770                 if (needfree && pktopt->ip6po_nexthop)
1771                         kfree(pktopt->ip6po_nexthop, M_IP6OPT);
1772                 pktopt->ip6po_nexthop = NULL;
1773         }
1774         if (optname == -1) {
1775                 if (needfree && pktopt->ip6po_hbh)
1776                         kfree(pktopt->ip6po_hbh, M_IP6OPT);
1777                 pktopt->ip6po_hbh = NULL;
1778         }
1779         if (optname == -1) {
1780                 if (needfree && pktopt->ip6po_dest1)
1781                         kfree(pktopt->ip6po_dest1, M_IP6OPT);
1782                 pktopt->ip6po_dest1 = NULL;
1783         }
1784         if (optname == -1) {
1785                 if (needfree && pktopt->ip6po_rhinfo.ip6po_rhi_rthdr)
1786                         kfree(pktopt->ip6po_rhinfo.ip6po_rhi_rthdr, M_IP6OPT);
1787                 pktopt->ip6po_rhinfo.ip6po_rhi_rthdr = NULL;
1788                 if (pktopt->ip6po_route.ro_rt) {
1789                         RTFREE(pktopt->ip6po_route.ro_rt);
1790                         pktopt->ip6po_route.ro_rt = NULL;
1791                 }
1792         }
1793         if (optname == -1) {
1794                 if (needfree && pktopt->ip6po_dest2)
1795                         kfree(pktopt->ip6po_dest2, M_IP6OPT);
1796                 pktopt->ip6po_dest2 = NULL;
1797         }
1798 }
1799
1800 #define PKTOPT_EXTHDRCPY(type) \
1801 do {\
1802         if (src->type) {\
1803                 int hlen =\
1804                         (((struct ip6_ext *)src->type)->ip6e_len + 1) << 3;\
1805                 dst->type = kmalloc(hlen, M_IP6OPT, canwait);\
1806                 if (dst->type == NULL)\
1807                         goto bad;\
1808                 bcopy(src->type, dst->type, hlen);\
1809         }\
1810 } while (0)
1811
1812 struct ip6_pktopts *
1813 ip6_copypktopts(struct ip6_pktopts *src, int canwait)
1814 {
1815         struct ip6_pktopts *dst;
1816
1817         if (src == NULL) {
1818                 printf("ip6_clearpktopts: invalid argument\n");
1819                 return (NULL);
1820         }
1821
1822         dst = kmalloc(sizeof(*dst), M_IP6OPT, canwait);
1823         if (dst == NULL)
1824                 return (NULL);
1825         bzero(dst, sizeof(*dst));
1826
1827         dst->ip6po_hlim = src->ip6po_hlim;
1828         if (src->ip6po_pktinfo) {
1829                 dst->ip6po_pktinfo = kmalloc(sizeof(*dst->ip6po_pktinfo),
1830                                             M_IP6OPT, canwait);
1831                 if (dst->ip6po_pktinfo == NULL)
1832                         goto bad;
1833                 *dst->ip6po_pktinfo = *src->ip6po_pktinfo;
1834         }
1835         if (src->ip6po_nexthop) {
1836                 dst->ip6po_nexthop = kmalloc(src->ip6po_nexthop->sa_len,
1837                                             M_IP6OPT, canwait);
1838                 if (dst->ip6po_nexthop == NULL)
1839                         goto bad;
1840                 bcopy(src->ip6po_nexthop, dst->ip6po_nexthop,
1841                       src->ip6po_nexthop->sa_len);
1842         }
1843         PKTOPT_EXTHDRCPY(ip6po_hbh);
1844         PKTOPT_EXTHDRCPY(ip6po_dest1);
1845         PKTOPT_EXTHDRCPY(ip6po_dest2);
1846         PKTOPT_EXTHDRCPY(ip6po_rthdr); /* not copy the cached route */
1847         return (dst);
1848
1849 bad:
1850         if (dst->ip6po_pktinfo) kfree(dst->ip6po_pktinfo, M_IP6OPT);
1851         if (dst->ip6po_nexthop) kfree(dst->ip6po_nexthop, M_IP6OPT);
1852         if (dst->ip6po_hbh) kfree(dst->ip6po_hbh, M_IP6OPT);
1853         if (dst->ip6po_dest1) kfree(dst->ip6po_dest1, M_IP6OPT);
1854         if (dst->ip6po_dest2) kfree(dst->ip6po_dest2, M_IP6OPT);
1855         if (dst->ip6po_rthdr) kfree(dst->ip6po_rthdr, M_IP6OPT);
1856         kfree(dst, M_IP6OPT);
1857         return (NULL);
1858 }
1859 #undef PKTOPT_EXTHDRCPY
1860
1861 void
1862 ip6_freepcbopts(struct ip6_pktopts *pktopt)
1863 {
1864         if (pktopt == NULL)
1865                 return;
1866
1867         ip6_clearpktopts(pktopt, 1, -1);
1868
1869         kfree(pktopt, M_IP6OPT);
1870 }
1871
1872 /*
1873  * Set the IP6 multicast options in response to user setsockopt().
1874  */
1875 static int
1876 ip6_setmoptions(int optname, struct ip6_moptions **im6op, struct mbuf *m)
1877 {
1878         int error = 0;
1879         u_int loop, ifindex;
1880         struct ipv6_mreq *mreq;
1881         struct ifnet *ifp;
1882         struct ip6_moptions *im6o = *im6op;
1883         struct route_in6 ro;
1884         struct sockaddr_in6 *dst;
1885         struct in6_multi_mship *imm;
1886         struct thread *td = curthread;  /* XXX */
1887
1888         if (im6o == NULL) {
1889                 /*
1890                  * No multicast option buffer attached to the pcb;
1891                  * allocate one and initialize to default values.
1892                  */
1893                 im6o = (struct ip6_moptions *)
1894                         kmalloc(sizeof(*im6o), M_IPMOPTS, M_WAITOK);
1895
1896                 if (im6o == NULL)
1897                         return (ENOBUFS);
1898                 *im6op = im6o;
1899                 im6o->im6o_multicast_ifp = NULL;
1900                 im6o->im6o_multicast_hlim = ip6_defmcasthlim;
1901                 im6o->im6o_multicast_loop = IPV6_DEFAULT_MULTICAST_LOOP;
1902                 LIST_INIT(&im6o->im6o_memberships);
1903         }
1904
1905         switch (optname) {
1906
1907         case IPV6_MULTICAST_IF:
1908                 /*
1909                  * Select the interface for outgoing multicast packets.
1910                  */
1911                 if (m == NULL || m->m_len != sizeof(u_int)) {
1912                         error = EINVAL;
1913                         break;
1914                 }
1915                 bcopy(mtod(m, u_int *), &ifindex, sizeof(ifindex));
1916                 if (ifindex < 0 || if_index < ifindex) {
1917                         error = ENXIO;  /* XXX EINVAL? */
1918                         break;
1919                 }
1920                 ifp = ifindex2ifnet[ifindex];
1921                 if (ifp == NULL || !(ifp->if_flags & IFF_MULTICAST)) {
1922                         error = EADDRNOTAVAIL;
1923                         break;
1924                 }
1925                 im6o->im6o_multicast_ifp = ifp;
1926                 break;
1927
1928         case IPV6_MULTICAST_HOPS:
1929             {
1930                 /*
1931                  * Set the IP6 hoplimit for outgoing multicast packets.
1932                  */
1933                 int optval;
1934                 if (m == NULL || m->m_len != sizeof(int)) {
1935                         error = EINVAL;
1936                         break;
1937                 }
1938                 bcopy(mtod(m, u_int *), &optval, sizeof(optval));
1939                 if (optval < -1 || optval >= 256)
1940                         error = EINVAL;
1941                 else if (optval == -1)
1942                         im6o->im6o_multicast_hlim = ip6_defmcasthlim;
1943                 else
1944                         im6o->im6o_multicast_hlim = optval;
1945                 break;
1946             }
1947
1948         case IPV6_MULTICAST_LOOP:
1949                 /*
1950                  * Set the loopback flag for outgoing multicast packets.
1951                  * Must be zero or one.
1952                  */
1953                 if (m == NULL || m->m_len != sizeof(u_int)) {
1954                         error = EINVAL;
1955                         break;
1956                 }
1957                 bcopy(mtod(m, u_int *), &loop, sizeof(loop));
1958                 if (loop > 1) {
1959                         error = EINVAL;
1960                         break;
1961                 }
1962                 im6o->im6o_multicast_loop = loop;
1963                 break;
1964
1965         case IPV6_JOIN_GROUP:
1966                 /*
1967                  * Add a multicast group membership.
1968                  * Group must be a valid IP6 multicast address.
1969                  */
1970                 if (m == NULL || m->m_len != sizeof(struct ipv6_mreq)) {
1971                         error = EINVAL;
1972                         break;
1973                 }
1974                 mreq = mtod(m, struct ipv6_mreq *);
1975                 if (IN6_IS_ADDR_UNSPECIFIED(&mreq->ipv6mr_multiaddr)) {
1976                         /*
1977                          * We use the unspecified address to specify to accept
1978                          * all multicast addresses. Only super user is allowed
1979                          * to do this.
1980                          */
1981                         if (suser(td))
1982                         {
1983                                 error = EACCES;
1984                                 break;
1985                         }
1986                 } else if (!IN6_IS_ADDR_MULTICAST(&mreq->ipv6mr_multiaddr)) {
1987                         error = EINVAL;
1988                         break;
1989                 }
1990
1991                 /*
1992                  * If the interface is specified, validate it.
1993                  */
1994                 if (mreq->ipv6mr_interface < 0
1995                  || if_index < mreq->ipv6mr_interface) {
1996                         error = ENXIO;  /* XXX EINVAL? */
1997                         break;
1998                 }
1999                 /*
2000                  * If no interface was explicitly specified, choose an
2001                  * appropriate one according to the given multicast address.
2002                  */
2003                 if (mreq->ipv6mr_interface == 0) {
2004                         /*
2005                          * If the multicast address is in node-local scope,
2006                          * the interface should be a loopback interface.
2007                          * Otherwise, look up the routing table for the
2008                          * address, and choose the outgoing interface.
2009                          *   XXX: is it a good approach?
2010                          */
2011                         if (IN6_IS_ADDR_MC_NODELOCAL(&mreq->ipv6mr_multiaddr)) {
2012                                 ifp = &loif[0];
2013                         } else {
2014                                 ro.ro_rt = NULL;
2015                                 dst = (struct sockaddr_in6 *)&ro.ro_dst;
2016                                 bzero(dst, sizeof(*dst));
2017                                 dst->sin6_len = sizeof(struct sockaddr_in6);
2018                                 dst->sin6_family = AF_INET6;
2019                                 dst->sin6_addr = mreq->ipv6mr_multiaddr;
2020                                 rtalloc((struct route *)&ro);
2021                                 if (ro.ro_rt == NULL) {
2022                                         error = EADDRNOTAVAIL;
2023                                         break;
2024                                 }
2025                                 ifp = ro.ro_rt->rt_ifp;
2026                                 rtfree(ro.ro_rt);
2027                         }
2028                 } else
2029                         ifp = ifindex2ifnet[mreq->ipv6mr_interface];
2030
2031                 /*
2032                  * See if we found an interface, and confirm that it
2033                  * supports multicast
2034                  */
2035                 if (ifp == NULL || !(ifp->if_flags & IFF_MULTICAST)) {
2036                         error = EADDRNOTAVAIL;
2037                         break;
2038                 }
2039                 /*
2040                  * Put interface index into the multicast address,
2041                  * if the address has link-local scope.
2042                  */
2043                 if (IN6_IS_ADDR_MC_LINKLOCAL(&mreq->ipv6mr_multiaddr)) {
2044                         mreq->ipv6mr_multiaddr.s6_addr16[1]
2045                                 = htons(mreq->ipv6mr_interface);
2046                 }
2047                 /*
2048                  * See if the membership already exists.
2049                  */
2050                 for (imm = im6o->im6o_memberships.lh_first;
2051                      imm != NULL; imm = imm->i6mm_chain.le_next)
2052                         if (imm->i6mm_maddr->in6m_ifp == ifp &&
2053                             IN6_ARE_ADDR_EQUAL(&imm->i6mm_maddr->in6m_addr,
2054                                                &mreq->ipv6mr_multiaddr))
2055                                 break;
2056                 if (imm != NULL) {
2057                         error = EADDRINUSE;
2058                         break;
2059                 }
2060                 /*
2061                  * Everything looks good; add a new record to the multicast
2062                  * address list for the given interface.
2063                  */
2064                 imm = kmalloc(sizeof(*imm), M_IPMADDR, M_WAITOK);
2065                 if (imm == NULL) {
2066                         error = ENOBUFS;
2067                         break;
2068                 }
2069                 if ((imm->i6mm_maddr =
2070                      in6_addmulti(&mreq->ipv6mr_multiaddr, ifp, &error)) == NULL) {
2071                         kfree(imm, M_IPMADDR);
2072                         break;
2073                 }
2074                 LIST_INSERT_HEAD(&im6o->im6o_memberships, imm, i6mm_chain);
2075                 break;
2076
2077         case IPV6_LEAVE_GROUP:
2078                 /*
2079                  * Drop a multicast group membership.
2080                  * Group must be a valid IP6 multicast address.
2081                  */
2082                 if (m == NULL || m->m_len != sizeof(struct ipv6_mreq)) {
2083                         error = EINVAL;
2084                         break;
2085                 }
2086                 mreq = mtod(m, struct ipv6_mreq *);
2087                 if (IN6_IS_ADDR_UNSPECIFIED(&mreq->ipv6mr_multiaddr)) {
2088                         if (suser(td)) {
2089                                 error = EACCES;
2090                                 break;
2091                         }
2092                 } else if (!IN6_IS_ADDR_MULTICAST(&mreq->ipv6mr_multiaddr)) {
2093                         error = EINVAL;
2094                         break;
2095                 }
2096                 /*
2097                  * If an interface address was specified, get a pointer
2098                  * to its ifnet structure.
2099                  */
2100                 if (mreq->ipv6mr_interface < 0
2101                  || if_index < mreq->ipv6mr_interface) {
2102                         error = ENXIO;  /* XXX EINVAL? */
2103                         break;
2104                 }
2105                 ifp = ifindex2ifnet[mreq->ipv6mr_interface];
2106                 /*
2107                  * Put interface index into the multicast address,
2108                  * if the address has link-local scope.
2109                  */
2110                 if (IN6_IS_ADDR_MC_LINKLOCAL(&mreq->ipv6mr_multiaddr)) {
2111                         mreq->ipv6mr_multiaddr.s6_addr16[1]
2112                                 = htons(mreq->ipv6mr_interface);
2113                 }
2114                 /*
2115                  * Find the membership in the membership list.
2116                  */
2117                 for (imm = im6o->im6o_memberships.lh_first;
2118                      imm != NULL; imm = imm->i6mm_chain.le_next) {
2119                         if ((ifp == NULL ||
2120                              imm->i6mm_maddr->in6m_ifp == ifp) &&
2121                             IN6_ARE_ADDR_EQUAL(&imm->i6mm_maddr->in6m_addr,
2122                                                &mreq->ipv6mr_multiaddr))
2123                                 break;
2124                 }
2125                 if (imm == NULL) {
2126                         /* Unable to resolve interface */
2127                         error = EADDRNOTAVAIL;
2128                         break;
2129                 }
2130                 /*
2131                  * Give up the multicast address record to which the
2132                  * membership points.
2133                  */
2134                 LIST_REMOVE(imm, i6mm_chain);
2135                 in6_delmulti(imm->i6mm_maddr);
2136                 kfree(imm, M_IPMADDR);
2137                 break;
2138
2139         default:
2140                 error = EOPNOTSUPP;
2141                 break;
2142         }
2143
2144         /*
2145          * If all options have default values, no need to keep the mbuf.
2146          */
2147         if (im6o->im6o_multicast_ifp == NULL &&
2148             im6o->im6o_multicast_hlim == ip6_defmcasthlim &&
2149             im6o->im6o_multicast_loop == IPV6_DEFAULT_MULTICAST_LOOP &&
2150             im6o->im6o_memberships.lh_first == NULL) {
2151                 kfree(*im6op, M_IPMOPTS);
2152                 *im6op = NULL;
2153         }
2154
2155         return (error);
2156 }
2157
2158 /*
2159  * Return the IP6 multicast options in response to user getsockopt().
2160  */
2161 static int
2162 ip6_getmoptions(int optname, struct ip6_moptions *im6o, struct mbuf **mp)
2163 {
2164         u_int *hlim, *loop, *ifindex;
2165
2166         *mp = m_get(MB_WAIT, MT_HEADER);                /* XXX */
2167
2168         switch (optname) {
2169
2170         case IPV6_MULTICAST_IF:
2171                 ifindex = mtod(*mp, u_int *);
2172                 (*mp)->m_len = sizeof(u_int);
2173                 if (im6o == NULL || im6o->im6o_multicast_ifp == NULL)
2174                         *ifindex = 0;
2175                 else
2176                         *ifindex = im6o->im6o_multicast_ifp->if_index;
2177                 return (0);
2178
2179         case IPV6_MULTICAST_HOPS:
2180                 hlim = mtod(*mp, u_int *);
2181                 (*mp)->m_len = sizeof(u_int);
2182                 if (im6o == NULL)
2183                         *hlim = ip6_defmcasthlim;
2184                 else
2185                         *hlim = im6o->im6o_multicast_hlim;
2186                 return (0);
2187
2188         case IPV6_MULTICAST_LOOP:
2189                 loop = mtod(*mp, u_int *);
2190                 (*mp)->m_len = sizeof(u_int);
2191                 if (im6o == NULL)
2192                         *loop = ip6_defmcasthlim;
2193                 else
2194                         *loop = im6o->im6o_multicast_loop;
2195                 return (0);
2196
2197         default:
2198                 return (EOPNOTSUPP);
2199         }
2200 }
2201
2202 /*
2203  * Discard the IP6 multicast options.
2204  */
2205 void
2206 ip6_freemoptions(struct ip6_moptions *im6o)
2207 {
2208         struct in6_multi_mship *imm;
2209
2210         if (im6o == NULL)
2211                 return;
2212
2213         while ((imm = im6o->im6o_memberships.lh_first) != NULL) {
2214                 LIST_REMOVE(imm, i6mm_chain);
2215                 if (imm->i6mm_maddr)
2216                         in6_delmulti(imm->i6mm_maddr);
2217                 kfree(imm, M_IPMADDR);
2218         }
2219         kfree(im6o, M_IPMOPTS);
2220 }
2221
2222 /*
2223  * Set IPv6 outgoing packet options based on advanced API.
2224  */
2225 int
2226 ip6_setpktoptions(struct mbuf *control, struct ip6_pktopts *opt, int priv,
2227                   int needcopy)
2228 {
2229         struct cmsghdr *cm = NULL;
2230
2231         if (control == NULL || opt == NULL)
2232                 return (EINVAL);
2233
2234         init_ip6pktopts(opt);
2235
2236         /*
2237          * XXX: Currently, we assume all the optional information is stored
2238          * in a single mbuf.
2239          */
2240         if (control->m_next)
2241                 return (EINVAL);
2242
2243         for (; control->m_len; control->m_data += CMSG_ALIGN(cm->cmsg_len),
2244                      control->m_len -= CMSG_ALIGN(cm->cmsg_len)) {
2245                 cm = mtod(control, struct cmsghdr *);
2246                 if (cm->cmsg_len == 0 || cm->cmsg_len > control->m_len)
2247                         return (EINVAL);
2248                 if (cm->cmsg_level != IPPROTO_IPV6)
2249                         continue;
2250
2251                 /*
2252                  * XXX should check if RFC2292 API is mixed with 2292bis API
2253                  */
2254                 switch (cm->cmsg_type) {
2255                 case IPV6_PKTINFO:
2256                         if (cm->cmsg_len != CMSG_LEN(sizeof(struct in6_pktinfo)))
2257                                 return (EINVAL);
2258                         if (needcopy) {
2259                                 /* XXX: Is it really WAITOK? */
2260                                 opt->ip6po_pktinfo =
2261                                         kmalloc(sizeof(struct in6_pktinfo),
2262                                                M_IP6OPT, M_WAITOK);
2263                                 bcopy(CMSG_DATA(cm), opt->ip6po_pktinfo,
2264                                     sizeof(struct in6_pktinfo));
2265                         } else
2266                                 opt->ip6po_pktinfo =
2267                                         (struct in6_pktinfo *)CMSG_DATA(cm);
2268                         if (opt->ip6po_pktinfo->ipi6_ifindex &&
2269                             IN6_IS_ADDR_LINKLOCAL(&opt->ip6po_pktinfo->ipi6_addr))
2270                                 opt->ip6po_pktinfo->ipi6_addr.s6_addr16[1] =
2271                                         htons(opt->ip6po_pktinfo->ipi6_ifindex);
2272
2273                         if (opt->ip6po_pktinfo->ipi6_ifindex > if_index
2274                          || opt->ip6po_pktinfo->ipi6_ifindex < 0) {
2275                                 return (ENXIO);
2276                         }
2277
2278                         /*
2279                          * Check if the requested source address is indeed a
2280                          * unicast address assigned to the node, and can be
2281                          * used as the packet's source address.
2282                          */
2283                         if (!IN6_IS_ADDR_UNSPECIFIED(&opt->ip6po_pktinfo->ipi6_addr)) {
2284                                 struct in6_ifaddr *ia6;
2285                                 struct sockaddr_in6 sin6;
2286
2287                                 bzero(&sin6, sizeof(sin6));
2288                                 sin6.sin6_len = sizeof(sin6);
2289                                 sin6.sin6_family = AF_INET6;
2290                                 sin6.sin6_addr =
2291                                         opt->ip6po_pktinfo->ipi6_addr;
2292                                 ia6 = (struct in6_ifaddr *)ifa_ifwithaddr(sin6tosa(&sin6));
2293                                 if (ia6 == NULL ||
2294                                     (ia6->ia6_flags & (IN6_IFF_ANYCAST |
2295                                                        IN6_IFF_NOTREADY)) != 0)
2296                                         return (EADDRNOTAVAIL);
2297                         }
2298                         break;
2299
2300                 case IPV6_HOPLIMIT:
2301                         if (cm->cmsg_len != CMSG_LEN(sizeof(int)))
2302                                 return (EINVAL);
2303
2304                         opt->ip6po_hlim = *(int *)CMSG_DATA(cm);
2305                         if (opt->ip6po_hlim < -1 || opt->ip6po_hlim > 255)
2306                                 return (EINVAL);
2307                         break;
2308
2309                 case IPV6_NEXTHOP:
2310                         if (!priv)
2311                                 return (EPERM);
2312
2313                         if (cm->cmsg_len < sizeof(u_char) ||
2314                             /* check if cmsg_len is large enough for sa_len */
2315                             cm->cmsg_len < CMSG_LEN(*CMSG_DATA(cm)))
2316                                 return (EINVAL);
2317
2318                         if (needcopy) {
2319                                 opt->ip6po_nexthop =
2320                                         kmalloc(*CMSG_DATA(cm),
2321                                                M_IP6OPT, M_WAITOK);
2322                                 bcopy(CMSG_DATA(cm),
2323                                       opt->ip6po_nexthop,
2324                                       *CMSG_DATA(cm));
2325                         } else
2326                                 opt->ip6po_nexthop =
2327                                         (struct sockaddr *)CMSG_DATA(cm);
2328                         break;
2329
2330                 case IPV6_HOPOPTS:
2331                 {
2332                         struct ip6_hbh *hbh;
2333                         int hbhlen;
2334
2335                         if (cm->cmsg_len < CMSG_LEN(sizeof(struct ip6_hbh)))
2336                                 return (EINVAL);
2337                         hbh = (struct ip6_hbh *)CMSG_DATA(cm);
2338                         hbhlen = (hbh->ip6h_len + 1) << 3;
2339                         if (cm->cmsg_len != CMSG_LEN(hbhlen))
2340                                 return (EINVAL);
2341
2342                         if (needcopy) {
2343                                 opt->ip6po_hbh =
2344                                         kmalloc(hbhlen, M_IP6OPT, M_WAITOK);
2345                                 bcopy(hbh, opt->ip6po_hbh, hbhlen);
2346                         } else
2347                                 opt->ip6po_hbh = hbh;
2348                         break;
2349                 }
2350
2351                 case IPV6_DSTOPTS:
2352                 {
2353                         struct ip6_dest *dest, **newdest;
2354                         int destlen;
2355
2356                         if (cm->cmsg_len < CMSG_LEN(sizeof(struct ip6_dest)))
2357                                 return (EINVAL);
2358                         dest = (struct ip6_dest *)CMSG_DATA(cm);
2359                         destlen = (dest->ip6d_len + 1) << 3;
2360                         if (cm->cmsg_len != CMSG_LEN(destlen))
2361                                 return (EINVAL);
2362
2363                         /*
2364                          * The old advacned API is ambiguous on this
2365                          * point. Our approach is to determine the
2366                          * position based according to the existence
2367                          * of a routing header. Note, however, that
2368                          * this depends on the order of the extension
2369                          * headers in the ancillary data; the 1st part
2370                          * of the destination options header must
2371                          * appear before the routing header in the
2372                          * ancillary data, too.
2373                          * RFC2292bis solved the ambiguity by
2374                          * introducing separate cmsg types.
2375                          */
2376                         if (opt->ip6po_rthdr == NULL)
2377                                 newdest = &opt->ip6po_dest1;
2378                         else
2379                                 newdest = &opt->ip6po_dest2;
2380
2381                         if (needcopy) {
2382                                 *newdest = kmalloc(destlen, M_IP6OPT, M_WAITOK);
2383                                 bcopy(dest, *newdest, destlen);
2384                         } else
2385                                 *newdest = dest;
2386
2387                         break;
2388                 }
2389
2390                 case IPV6_RTHDR:
2391                 {
2392                         struct ip6_rthdr *rth;
2393                         int rthlen;
2394
2395                         if (cm->cmsg_len < CMSG_LEN(sizeof(struct ip6_rthdr)))
2396                                 return (EINVAL);
2397                         rth = (struct ip6_rthdr *)CMSG_DATA(cm);
2398                         rthlen = (rth->ip6r_len + 1) << 3;
2399                         if (cm->cmsg_len != CMSG_LEN(rthlen))
2400                                 return (EINVAL);
2401
2402                         switch (rth->ip6r_type) {
2403                         case IPV6_RTHDR_TYPE_0:
2404                                 /* must contain one addr */
2405                                 if (rth->ip6r_len == 0)
2406                                         return (EINVAL);
2407                                 /* length must be even */
2408                                 if (rth->ip6r_len % 2)
2409                                         return (EINVAL);
2410                                 if (rth->ip6r_len / 2 != rth->ip6r_segleft)
2411                                         return (EINVAL);
2412                                 break;
2413                         default:
2414                                 return (EINVAL);        /* not supported */
2415                         }
2416
2417                         if (needcopy) {
2418                                 opt->ip6po_rthdr = kmalloc(rthlen, M_IP6OPT,
2419                                                           M_WAITOK);
2420                                 bcopy(rth, opt->ip6po_rthdr, rthlen);
2421                         } else
2422                                 opt->ip6po_rthdr = rth;
2423
2424                         break;
2425                 }
2426
2427                 default:
2428                         return (ENOPROTOOPT);
2429                 }
2430         }
2431
2432         return (0);
2433 }
2434
2435 /*
2436  * Routine called from ip6_output() to loop back a copy of an IP6 multicast
2437  * packet to the input queue of a specified interface.  Note that this
2438  * calls the output routine of the loopback "driver", but with an interface
2439  * pointer that might NOT be &loif -- easier than replicating that code here.
2440  */
2441 void
2442 ip6_mloopback(struct ifnet *ifp, struct mbuf *m, struct sockaddr_in6 *dst)
2443 {
2444         struct mbuf *copym;
2445         struct ip6_hdr *ip6;
2446
2447         copym = m_copy(m, 0, M_COPYALL);
2448         if (copym == NULL)
2449                 return;
2450
2451         /*
2452          * Make sure to deep-copy IPv6 header portion in case the data
2453          * is in an mbuf cluster, so that we can safely override the IPv6
2454          * header portion later.
2455          */
2456         if ((copym->m_flags & M_EXT) != 0 ||
2457             copym->m_len < sizeof(struct ip6_hdr)) {
2458                 copym = m_pullup(copym, sizeof(struct ip6_hdr));
2459                 if (copym == NULL)
2460                         return;
2461         }
2462
2463 #ifdef DIAGNOSTIC
2464         if (copym->m_len < sizeof(*ip6)) {
2465                 m_freem(copym);
2466                 return;
2467         }
2468 #endif
2469
2470         ip6 = mtod(copym, struct ip6_hdr *);
2471         /*
2472          * clear embedded scope identifiers if necessary.
2473          * in6_clearscope will touch the addresses only when necessary.
2474          */
2475         in6_clearscope(&ip6->ip6_src);
2476         in6_clearscope(&ip6->ip6_dst);
2477
2478         if_simloop(ifp, copym, dst->sin6_family, NULL);
2479 }
2480
2481 /*
2482  * Separate the IPv6 header from the payload into its own mbuf.
2483  *
2484  * Returns the new mbuf chain or the original mbuf if no payload.
2485  * Returns NULL if can't allocate new mbuf for header.
2486  */
2487 static struct mbuf *
2488 ip6_splithdr(struct mbuf *m)
2489 {
2490         struct mbuf *mh;
2491
2492         if (m->m_len <= sizeof(struct ip6_hdr))         /* no payload */
2493                 return (m);
2494
2495         MGETHDR(mh, MB_DONTWAIT, MT_HEADER);
2496         if (mh == NULL)
2497                 return (NULL);
2498         mh->m_len = sizeof(struct ip6_hdr);
2499         M_MOVE_PKTHDR(mh, m);
2500         MH_ALIGN(mh, sizeof(struct ip6_hdr));
2501         bcopy(mtod(m, caddr_t), mtod(mh, caddr_t), sizeof(struct ip6_hdr));
2502         m->m_data += sizeof(struct ip6_hdr);
2503         m->m_len -= sizeof(struct ip6_hdr);
2504         mh->m_next = m;
2505         return (mh);
2506 }
2507
2508 /*
2509  * Compute IPv6 extension header length.
2510  */
2511 int
2512 ip6_optlen(struct in6pcb *in6p)
2513 {
2514         int len;
2515
2516         if (!in6p->in6p_outputopts)
2517                 return 0;
2518
2519         len = 0;
2520 #define elen(x) \
2521     (((struct ip6_ext *)(x)) ? (((struct ip6_ext *)(x))->ip6e_len + 1) << 3 : 0)
2522
2523         len += elen(in6p->in6p_outputopts->ip6po_hbh);
2524         if (in6p->in6p_outputopts->ip6po_rthdr)
2525                 /* dest1 is valid with rthdr only */
2526                 len += elen(in6p->in6p_outputopts->ip6po_dest1);
2527         len += elen(in6p->in6p_outputopts->ip6po_rthdr);
2528         len += elen(in6p->in6p_outputopts->ip6po_dest2);
2529         return len;
2530 #undef elen
2531 }