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