]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/netinet6/ip6_output.c
Since PFIL can change destination address, use its always actual value
[FreeBSD/FreeBSD.git] / sys / netinet6 / ip6_output.c
1 /*-
2  * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  * 3. Neither the name of the project nor the names of its contributors
14  *    may be used to endorse or promote products derived from this software
15  *    without specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
18  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20  * ARE DISCLAIMED.  IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
21  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27  * SUCH DAMAGE.
28  *
29  *      $KAME: ip6_output.c,v 1.279 2002/01/26 06:12:30 jinmei Exp $
30  */
31
32 /*-
33  * Copyright (c) 1982, 1986, 1988, 1990, 1993
34  *      The Regents of the University of California.  All rights reserved.
35  *
36  * Redistribution and use in source and binary forms, with or without
37  * modification, are permitted provided that the following conditions
38  * are met:
39  * 1. Redistributions of source code must retain the above copyright
40  *    notice, this list of conditions and the following disclaimer.
41  * 2. Redistributions in binary form must reproduce the above copyright
42  *    notice, this list of conditions and the following disclaimer in the
43  *    documentation and/or other materials provided with the distribution.
44  * 4. Neither the name of the University nor the names of its contributors
45  *    may be used to endorse or promote products derived from this software
46  *    without specific prior written permission.
47  *
48  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
49  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
50  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
51  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
52  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
53  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
54  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
55  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
56  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
57  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
58  * SUCH DAMAGE.
59  *
60  *      @(#)ip_output.c 8.3 (Berkeley) 1/21/94
61  */
62
63 #include <sys/cdefs.h>
64 __FBSDID("$FreeBSD$");
65
66 #include "opt_inet.h"
67 #include "opt_inet6.h"
68 #include "opt_ipfw.h"
69 #include "opt_ipsec.h"
70 #include "opt_sctp.h"
71 #include "opt_route.h"
72 #include "opt_rss.h"
73
74 #include <sys/param.h>
75 #include <sys/kernel.h>
76 #include <sys/malloc.h>
77 #include <sys/mbuf.h>
78 #include <sys/errno.h>
79 #include <sys/priv.h>
80 #include <sys/proc.h>
81 #include <sys/protosw.h>
82 #include <sys/socket.h>
83 #include <sys/socketvar.h>
84 #include <sys/syslog.h>
85 #include <sys/ucred.h>
86
87 #include <machine/in_cksum.h>
88
89 #include <net/if.h>
90 #include <net/if_var.h>
91 #include <net/netisr.h>
92 #include <net/route.h>
93 #include <net/pfil.h>
94 #include <net/rss_config.h>
95 #include <net/vnet.h>
96
97 #include <netinet/in.h>
98 #include <netinet/in_var.h>
99 #include <netinet/ip_var.h>
100 #include <netinet6/in6_fib.h>
101 #include <netinet6/in6_var.h>
102 #include <netinet/ip6.h>
103 #include <netinet/icmp6.h>
104 #include <netinet6/ip6_var.h>
105 #include <netinet/in_pcb.h>
106 #include <netinet/tcp_var.h>
107 #include <netinet6/nd6.h>
108 #include <netinet6/in6_rss.h>
109
110 #ifdef IPSEC
111 #include <netipsec/ipsec.h>
112 #include <netipsec/ipsec6.h>
113 #include <netipsec/key.h>
114 #include <netinet6/ip6_ipsec.h>
115 #endif /* IPSEC */
116 #ifdef SCTP
117 #include <netinet/sctp.h>
118 #include <netinet/sctp_crc32.h>
119 #endif
120
121 #include <netinet6/ip6protosw.h>
122 #include <netinet6/scope6_var.h>
123
124 #ifdef FLOWTABLE
125 #include <net/flowtable.h>
126 #endif
127
128 extern int in6_mcast_loop;
129
130 struct ip6_exthdrs {
131         struct mbuf *ip6e_ip6;
132         struct mbuf *ip6e_hbh;
133         struct mbuf *ip6e_dest1;
134         struct mbuf *ip6e_rthdr;
135         struct mbuf *ip6e_dest2;
136 };
137
138 static int ip6_pcbopt(int, u_char *, int, struct ip6_pktopts **,
139                            struct ucred *, int);
140 static int ip6_pcbopts(struct ip6_pktopts **, struct mbuf *,
141         struct socket *, struct sockopt *);
142 static int ip6_getpcbopt(struct ip6_pktopts *, int, struct sockopt *);
143 static int ip6_setpktopt(int, u_char *, int, struct ip6_pktopts *,
144         struct ucred *, int, int, int);
145
146 static int ip6_copyexthdr(struct mbuf **, caddr_t, int);
147 static int ip6_insertfraghdr(struct mbuf *, struct mbuf *, int,
148         struct ip6_frag **);
149 static int ip6_insert_jumboopt(struct ip6_exthdrs *, u_int32_t);
150 static int ip6_splithdr(struct mbuf *, struct ip6_exthdrs *);
151 static int ip6_getpmtu(struct route_in6 *, int,
152         struct ifnet *, const struct in6_addr *, u_long *, int *, u_int);
153 static int ip6_calcmtu(struct ifnet *, const struct in6_addr *, u_long,
154         u_long *, int *);
155 static int ip6_getpmtu_ctl(u_int, const struct in6_addr *, u_long *);
156 static int copypktopts(struct ip6_pktopts *, struct ip6_pktopts *, int);
157
158
159 /*
160  * Make an extension header from option data.  hp is the source, and
161  * mp is the destination.
162  */
163 #define MAKE_EXTHDR(hp, mp)                                             \
164     do {                                                                \
165         if (hp) {                                                       \
166                 struct ip6_ext *eh = (struct ip6_ext *)(hp);            \
167                 error = ip6_copyexthdr((mp), (caddr_t)(hp),             \
168                     ((eh)->ip6e_len + 1) << 3);                         \
169                 if (error)                                              \
170                         goto freehdrs;                                  \
171         }                                                               \
172     } while (/*CONSTCOND*/ 0)
173
174 /*
175  * Form a chain of extension headers.
176  * m is the extension header mbuf
177  * mp is the previous mbuf in the chain
178  * p is the next header
179  * i is the type of option.
180  */
181 #define MAKE_CHAIN(m, mp, p, i)\
182     do {\
183         if (m) {\
184                 if (!hdrsplit) \
185                         panic("assumption failed: hdr not split"); \
186                 *mtod((m), u_char *) = *(p);\
187                 *(p) = (i);\
188                 p = mtod((m), u_char *);\
189                 (m)->m_next = (mp)->m_next;\
190                 (mp)->m_next = (m);\
191                 (mp) = (m);\
192         }\
193     } while (/*CONSTCOND*/ 0)
194
195 void
196 in6_delayed_cksum(struct mbuf *m, uint32_t plen, u_short offset)
197 {
198         u_short csum;
199
200         csum = in_cksum_skip(m, offset + plen, offset);
201         if (m->m_pkthdr.csum_flags & CSUM_UDP_IPV6 && csum == 0)
202                 csum = 0xffff;
203         offset += m->m_pkthdr.csum_data;        /* checksum offset */
204
205         if (offset + sizeof(u_short) > m->m_len) {
206                 printf("%s: delayed m_pullup, m->len: %d plen %u off %u "
207                     "csum_flags=%b\n", __func__, m->m_len, plen, offset,
208                     (int)m->m_pkthdr.csum_flags, CSUM_BITS);
209                 /*
210                  * XXX this should not happen, but if it does, the correct
211                  * behavior may be to insert the checksum in the appropriate
212                  * next mbuf in the chain.
213                  */
214                 return;
215         }
216         *(u_short *)(m->m_data + offset) = csum;
217 }
218
219 int
220 ip6_fragment(struct ifnet *ifp, struct mbuf *m0, int hlen, u_char nextproto,
221     int mtu, uint32_t id)
222 {
223         struct mbuf *m, **mnext, *m_frgpart;
224         struct ip6_hdr *ip6, *mhip6;
225         struct ip6_frag *ip6f;
226         int off;
227         int error;
228         int tlen = m0->m_pkthdr.len;
229
230         m = m0;
231         ip6 = mtod(m, struct ip6_hdr *);
232         mnext = &m->m_nextpkt;
233
234         for (off = hlen; off < tlen; off += mtu) {
235                 m = m_gethdr(M_NOWAIT, MT_DATA);
236                 if (!m) {
237                         IP6STAT_INC(ip6s_odropped);
238                         return (ENOBUFS);
239                 }
240                 m->m_flags = m0->m_flags & M_COPYFLAGS;
241                 *mnext = m;
242                 mnext = &m->m_nextpkt;
243                 m->m_data += max_linkhdr;
244                 mhip6 = mtod(m, struct ip6_hdr *);
245                 *mhip6 = *ip6;
246                 m->m_len = sizeof(*mhip6);
247                 error = ip6_insertfraghdr(m0, m, hlen, &ip6f);
248                 if (error) {
249                         IP6STAT_INC(ip6s_odropped);
250                         return (error);
251                 }
252                 ip6f->ip6f_offlg = htons((u_short)((off - hlen) & ~7));
253                 if (off + mtu >= tlen)
254                         mtu = tlen - off;
255                 else
256                         ip6f->ip6f_offlg |= IP6F_MORE_FRAG;
257                 mhip6->ip6_plen = htons((u_short)(mtu + hlen +
258                     sizeof(*ip6f) - sizeof(struct ip6_hdr)));
259                 if ((m_frgpart = m_copy(m0, off, mtu)) == NULL) {
260                         IP6STAT_INC(ip6s_odropped);
261                         return (ENOBUFS);
262                 }
263                 m_cat(m, m_frgpart);
264                 m->m_pkthdr.len = mtu + hlen + sizeof(*ip6f);
265                 m->m_pkthdr.fibnum = m0->m_pkthdr.fibnum;
266                 m->m_pkthdr.rcvif = NULL;
267                 ip6f->ip6f_reserved = 0;
268                 ip6f->ip6f_ident = id;
269                 ip6f->ip6f_nxt = nextproto;
270                 IP6STAT_INC(ip6s_ofragments);
271                 in6_ifstat_inc(ifp, ifs6_out_fragcreat);
272         }
273
274         return (0);
275 }
276
277 /*
278  * IP6 output. The packet in mbuf chain m contains a skeletal IP6
279  * header (with pri, len, nxt, hlim, src, dst).
280  * This function may modify ver and hlim only.
281  * The mbuf chain containing the packet will be freed.
282  * The mbuf opt, if present, will not be freed.
283  * If route_in6 ro is present and has ro_rt initialized, route lookup would be
284  * skipped and ro->ro_rt would be used. If ro is present but ro->ro_rt is NULL,
285  * then result of route lookup is stored in ro->ro_rt.
286  *
287  * type of "mtu": rt_mtu is u_long, ifnet.ifr_mtu is int, and
288  * nd_ifinfo.linkmtu is u_int32_t.  so we use u_long to hold largest one,
289  * which is rt_mtu.
290  *
291  * ifpp - XXX: just for statistics
292  */
293 /*
294  * XXX TODO: no flowid is assigned for outbound flows?
295  */
296 int
297 ip6_output(struct mbuf *m0, struct ip6_pktopts *opt,
298     struct route_in6 *ro, int flags, struct ip6_moptions *im6o,
299     struct ifnet **ifpp, struct inpcb *inp)
300 {
301         struct ip6_hdr *ip6;
302         struct ifnet *ifp, *origifp;
303         struct mbuf *m = m0;
304         struct mbuf *mprev = NULL;
305         int hlen, tlen, len;
306         struct route_in6 ip6route;
307         struct rtentry *rt = NULL;
308         struct sockaddr_in6 *dst, src_sa, dst_sa;
309         struct in6_addr odst;
310         int error = 0;
311         struct in6_ifaddr *ia = NULL;
312         u_long mtu;
313         int alwaysfrag, dontfrag;
314         u_int32_t optlen = 0, plen = 0, unfragpartlen = 0;
315         struct ip6_exthdrs exthdrs;
316         struct in6_addr src0, dst0;
317         u_int32_t zone;
318         struct route_in6 *ro_pmtu = NULL;
319         int hdrsplit = 0;
320         int sw_csum, tso;
321         int needfiblookup;
322         uint32_t fibnum;
323         struct m_tag *fwd_tag = NULL;
324         uint32_t id;
325
326         ip6 = mtod(m, struct ip6_hdr *);
327         if (ip6 == NULL) {
328                 printf ("ip6 is NULL");
329                 goto bad;
330         }
331
332         if (inp != NULL) {
333                 M_SETFIB(m, inp->inp_inc.inc_fibnum);
334                 if ((flags & IP_NODEFAULTFLOWID) == 0) {
335                         /* unconditionally set flowid */
336                         m->m_pkthdr.flowid = inp->inp_flowid;
337                         M_HASHTYPE_SET(m, inp->inp_flowtype);
338                 }
339         }
340
341         bzero(&exthdrs, sizeof(exthdrs));
342         if (opt) {
343                 /* Hop-by-Hop options header */
344                 MAKE_EXTHDR(opt->ip6po_hbh, &exthdrs.ip6e_hbh);
345                 /* Destination options header(1st part) */
346                 if (opt->ip6po_rthdr) {
347                         /*
348                          * Destination options header(1st part)
349                          * This only makes sense with a routing header.
350                          * See Section 9.2 of RFC 3542.
351                          * Disabling this part just for MIP6 convenience is
352                          * a bad idea.  We need to think carefully about a
353                          * way to make the advanced API coexist with MIP6
354                          * options, which might automatically be inserted in
355                          * the kernel.
356                          */
357                         MAKE_EXTHDR(opt->ip6po_dest1, &exthdrs.ip6e_dest1);
358                 }
359                 /* Routing header */
360                 MAKE_EXTHDR(opt->ip6po_rthdr, &exthdrs.ip6e_rthdr);
361                 /* Destination options header(2nd part) */
362                 MAKE_EXTHDR(opt->ip6po_dest2, &exthdrs.ip6e_dest2);
363         }
364
365 #ifdef IPSEC
366         /*
367          * IPSec checking which handles several cases.
368          * FAST IPSEC: We re-injected the packet.
369          * XXX: need scope argument.
370          */
371         switch(ip6_ipsec_output(&m, inp, &error))
372         {
373         case 1:                 /* Bad packet */
374                 goto freehdrs;
375         case -1:                /* IPSec done */
376                 goto done;
377         case 0:                 /* No IPSec */
378         default:
379                 break;
380         }
381 #endif /* IPSEC */
382
383         /*
384          * Calculate the total length of the extension header chain.
385          * Keep the length of the unfragmentable part for fragmentation.
386          */
387         optlen = 0;
388         if (exthdrs.ip6e_hbh)
389                 optlen += exthdrs.ip6e_hbh->m_len;
390         if (exthdrs.ip6e_dest1)
391                 optlen += exthdrs.ip6e_dest1->m_len;
392         if (exthdrs.ip6e_rthdr)
393                 optlen += exthdrs.ip6e_rthdr->m_len;
394         unfragpartlen = optlen + sizeof(struct ip6_hdr);
395
396         /* NOTE: we don't add AH/ESP length here (done in ip6_ipsec_output) */
397         if (exthdrs.ip6e_dest2)
398                 optlen += exthdrs.ip6e_dest2->m_len;
399
400         /*
401          * If there is at least one extension header,
402          * separate IP6 header from the payload.
403          */
404         if (optlen && !hdrsplit) {
405                 if ((error = ip6_splithdr(m, &exthdrs)) != 0) {
406                         m = NULL;
407                         goto freehdrs;
408                 }
409                 m = exthdrs.ip6e_ip6;
410                 hdrsplit++;
411         }
412
413         /* adjust pointer */
414         ip6 = mtod(m, struct ip6_hdr *);
415
416         /* adjust mbuf packet header length */
417         m->m_pkthdr.len += optlen;
418         plen = m->m_pkthdr.len - sizeof(*ip6);
419
420         /* If this is a jumbo payload, insert a jumbo payload option. */
421         if (plen > IPV6_MAXPACKET) {
422                 if (!hdrsplit) {
423                         if ((error = ip6_splithdr(m, &exthdrs)) != 0) {
424                                 m = NULL;
425                                 goto freehdrs;
426                         }
427                         m = exthdrs.ip6e_ip6;
428                         hdrsplit++;
429                 }
430                 /* adjust pointer */
431                 ip6 = mtod(m, struct ip6_hdr *);
432                 if ((error = ip6_insert_jumboopt(&exthdrs, plen)) != 0)
433                         goto freehdrs;
434                 ip6->ip6_plen = 0;
435         } else
436                 ip6->ip6_plen = htons(plen);
437
438         /*
439          * Concatenate headers and fill in next header fields.
440          * Here we have, on "m"
441          *      IPv6 payload
442          * and we insert headers accordingly.  Finally, we should be getting:
443          *      IPv6 hbh dest1 rthdr ah* [esp* dest2 payload]
444          *
445          * during the header composing process, "m" points to IPv6 header.
446          * "mprev" points to an extension header prior to esp.
447          */
448         u_char *nexthdrp = &ip6->ip6_nxt;
449         mprev = m;
450
451         /*
452          * we treat dest2 specially.  this makes IPsec processing
453          * much easier.  the goal here is to make mprev point the
454          * mbuf prior to dest2.
455          *
456          * result: IPv6 dest2 payload
457          * m and mprev will point to IPv6 header.
458          */
459         if (exthdrs.ip6e_dest2) {
460                 if (!hdrsplit)
461                         panic("assumption failed: hdr not split");
462                 exthdrs.ip6e_dest2->m_next = m->m_next;
463                 m->m_next = exthdrs.ip6e_dest2;
464                 *mtod(exthdrs.ip6e_dest2, u_char *) = ip6->ip6_nxt;
465                 ip6->ip6_nxt = IPPROTO_DSTOPTS;
466         }
467
468         /*
469          * result: IPv6 hbh dest1 rthdr dest2 payload
470          * m will point to IPv6 header.  mprev will point to the
471          * extension header prior to dest2 (rthdr in the above case).
472          */
473         MAKE_CHAIN(exthdrs.ip6e_hbh, mprev, nexthdrp, IPPROTO_HOPOPTS);
474         MAKE_CHAIN(exthdrs.ip6e_dest1, mprev, nexthdrp,
475                    IPPROTO_DSTOPTS);
476         MAKE_CHAIN(exthdrs.ip6e_rthdr, mprev, nexthdrp,
477                    IPPROTO_ROUTING);
478
479         /*
480          * If there is a routing header, discard the packet.
481          */
482         if (exthdrs.ip6e_rthdr) {
483                  error = EINVAL;
484                  goto bad;
485         }
486
487         /* Source address validation */
488         if (IN6_IS_ADDR_UNSPECIFIED(&ip6->ip6_src) &&
489             (flags & IPV6_UNSPECSRC) == 0) {
490                 error = EOPNOTSUPP;
491                 IP6STAT_INC(ip6s_badscope);
492                 goto bad;
493         }
494         if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_src)) {
495                 error = EOPNOTSUPP;
496                 IP6STAT_INC(ip6s_badscope);
497                 goto bad;
498         }
499
500         IP6STAT_INC(ip6s_localout);
501
502         /*
503          * Route packet.
504          */
505         if (ro == NULL) {
506                 ro = &ip6route;
507                 bzero((caddr_t)ro, sizeof(*ro));
508         }
509         ro_pmtu = ro;
510         if (opt && opt->ip6po_rthdr)
511                 ro = &opt->ip6po_route;
512         dst = (struct sockaddr_in6 *)&ro->ro_dst;
513 #ifdef FLOWTABLE
514         if (ro->ro_rt == NULL)
515                 (void )flowtable_lookup(AF_INET6, m, (struct route *)ro);
516 #endif
517         fibnum = (inp != NULL) ? inp->inp_inc.inc_fibnum : M_GETFIB(m);
518 again:
519         /*
520          * if specified, try to fill in the traffic class field.
521          * do not override if a non-zero value is already set.
522          * we check the diffserv field and the ecn field separately.
523          */
524         if (opt && opt->ip6po_tclass >= 0) {
525                 int mask = 0;
526
527                 if ((ip6->ip6_flow & htonl(0xfc << 20)) == 0)
528                         mask |= 0xfc;
529                 if ((ip6->ip6_flow & htonl(0x03 << 20)) == 0)
530                         mask |= 0x03;
531                 if (mask != 0)
532                         ip6->ip6_flow |= htonl((opt->ip6po_tclass & mask) << 20);
533         }
534
535         /* fill in or override the hop limit field, if necessary. */
536         if (opt && opt->ip6po_hlim != -1)
537                 ip6->ip6_hlim = opt->ip6po_hlim & 0xff;
538         else if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst)) {
539                 if (im6o != NULL)
540                         ip6->ip6_hlim = im6o->im6o_multicast_hlim;
541                 else
542                         ip6->ip6_hlim = V_ip6_defmcasthlim;
543         }
544
545         /* adjust pointer */
546         ip6 = mtod(m, struct ip6_hdr *);
547
548         /*
549          * Validate route against routing table additions;
550          * a better/more specific route might have been added.
551          * Make sure address family is set in route.
552          */
553         if (inp) {
554                 ro->ro_dst.sin6_family = AF_INET6;
555                 RT_VALIDATE((struct route *)ro, &inp->inp_rt_cookie, fibnum);
556         }
557         if (ro->ro_rt && fwd_tag == NULL && (ro->ro_rt->rt_flags & RTF_UP) &&
558             ro->ro_dst.sin6_family == AF_INET6 &&
559             IN6_ARE_ADDR_EQUAL(&ro->ro_dst.sin6_addr, &ip6->ip6_dst)) {
560                 rt = ro->ro_rt;
561                 ifp = ro->ro_rt->rt_ifp;
562         } else {
563                 if (fwd_tag == NULL) {
564                         bzero(&dst_sa, sizeof(dst_sa));
565                         dst_sa.sin6_family = AF_INET6;
566                         dst_sa.sin6_len = sizeof(dst_sa);
567                         dst_sa.sin6_addr = ip6->ip6_dst;
568                 }
569                 error = in6_selectroute_fib(&dst_sa, opt, im6o, ro, &ifp,
570                     &rt, fibnum);
571                 if (error != 0) {
572                         if (ifp != NULL)
573                                 in6_ifstat_inc(ifp, ifs6_out_discard);
574                         goto bad;
575                 }
576         }
577         if (rt == NULL) {
578                 /*
579                  * If in6_selectroute() does not return a route entry,
580                  * dst may not have been updated.
581                  */
582                 *dst = dst_sa;  /* XXX */
583         }
584
585         /*
586          * then rt (for unicast) and ifp must be non-NULL valid values.
587          */
588         if ((flags & IPV6_FORWARDING) == 0) {
589                 /* XXX: the FORWARDING flag can be set for mrouting. */
590                 in6_ifstat_inc(ifp, ifs6_out_request);
591         }
592         if (rt != NULL) {
593                 ia = (struct in6_ifaddr *)(rt->rt_ifa);
594                 counter_u64_add(rt->rt_pksent, 1);
595         }
596
597
598         /*
599          * The outgoing interface must be in the zone of source and
600          * destination addresses.
601          */
602         origifp = ifp;
603
604         src0 = ip6->ip6_src;
605         if (in6_setscope(&src0, origifp, &zone))
606                 goto badscope;
607         bzero(&src_sa, sizeof(src_sa));
608         src_sa.sin6_family = AF_INET6;
609         src_sa.sin6_len = sizeof(src_sa);
610         src_sa.sin6_addr = ip6->ip6_src;
611         if (sa6_recoverscope(&src_sa) || zone != src_sa.sin6_scope_id)
612                 goto badscope;
613
614         dst0 = ip6->ip6_dst;
615         if (in6_setscope(&dst0, origifp, &zone))
616                 goto badscope;
617         /* re-initialize to be sure */
618         bzero(&dst_sa, sizeof(dst_sa));
619         dst_sa.sin6_family = AF_INET6;
620         dst_sa.sin6_len = sizeof(dst_sa);
621         dst_sa.sin6_addr = ip6->ip6_dst;
622         if (sa6_recoverscope(&dst_sa) || zone != dst_sa.sin6_scope_id) {
623                 goto badscope;
624         }
625
626         /* We should use ia_ifp to support the case of
627          * sending packets to an address of our own.
628          */
629         if (ia != NULL && ia->ia_ifp)
630                 ifp = ia->ia_ifp;
631
632         /* scope check is done. */
633         goto routefound;
634
635   badscope:
636         IP6STAT_INC(ip6s_badscope);
637         in6_ifstat_inc(origifp, ifs6_out_discard);
638         if (error == 0)
639                 error = EHOSTUNREACH; /* XXX */
640         goto bad;
641
642   routefound:
643         if (rt && !IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst)) {
644                 if (opt && opt->ip6po_nextroute.ro_rt) {
645                         /*
646                          * The nexthop is explicitly specified by the
647                          * application.  We assume the next hop is an IPv6
648                          * address.
649                          */
650                         dst = (struct sockaddr_in6 *)opt->ip6po_nexthop;
651                 }
652                 else if ((rt->rt_flags & RTF_GATEWAY))
653                         dst = (struct sockaddr_in6 *)rt->rt_gateway;
654         }
655
656         if (!IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst)) {
657                 m->m_flags &= ~(M_BCAST | M_MCAST); /* just in case */
658         } else {
659                 m->m_flags = (m->m_flags & ~M_BCAST) | M_MCAST;
660                 in6_ifstat_inc(ifp, ifs6_out_mcast);
661                 /*
662                  * Confirm that the outgoing interface supports multicast.
663                  */
664                 if (!(ifp->if_flags & IFF_MULTICAST)) {
665                         IP6STAT_INC(ip6s_noroute);
666                         in6_ifstat_inc(ifp, ifs6_out_discard);
667                         error = ENETUNREACH;
668                         goto bad;
669                 }
670                 if ((im6o == NULL && in6_mcast_loop) ||
671                     (im6o && im6o->im6o_multicast_loop)) {
672                         /*
673                          * Loop back multicast datagram if not expressly
674                          * forbidden to do so, even if we have not joined
675                          * the address; protocols will filter it later,
676                          * thus deferring a hash lookup and lock acquisition
677                          * at the expense of an m_copym().
678                          */
679                         ip6_mloopback(ifp, m);
680                 } else {
681                         /*
682                          * If we are acting as a multicast router, perform
683                          * multicast forwarding as if the packet had just
684                          * arrived on the interface to which we are about
685                          * to send.  The multicast forwarding function
686                          * recursively calls this function, using the
687                          * IPV6_FORWARDING flag to prevent infinite recursion.
688                          *
689                          * Multicasts that are looped back by ip6_mloopback(),
690                          * above, will be forwarded by the ip6_input() routine,
691                          * if necessary.
692                          */
693                         if (V_ip6_mrouter && (flags & IPV6_FORWARDING) == 0) {
694                                 /*
695                                  * XXX: ip6_mforward expects that rcvif is NULL
696                                  * when it is called from the originating path.
697                                  * However, it may not always be the case.
698                                  */
699                                 m->m_pkthdr.rcvif = NULL;
700                                 if (ip6_mforward(ip6, ifp, m) != 0) {
701                                         m_freem(m);
702                                         goto done;
703                                 }
704                         }
705                 }
706                 /*
707                  * Multicasts with a hoplimit of zero may be looped back,
708                  * above, but must not be transmitted on a network.
709                  * Also, multicasts addressed to the loopback interface
710                  * are not sent -- the above call to ip6_mloopback() will
711                  * loop back a copy if this host actually belongs to the
712                  * destination group on the loopback interface.
713                  */
714                 if (ip6->ip6_hlim == 0 || (ifp->if_flags & IFF_LOOPBACK) ||
715                     IN6_IS_ADDR_MC_INTFACELOCAL(&ip6->ip6_dst)) {
716                         m_freem(m);
717                         goto done;
718                 }
719         }
720
721         /*
722          * Fill the outgoing inteface to tell the upper layer
723          * to increment per-interface statistics.
724          */
725         if (ifpp)
726                 *ifpp = ifp;
727
728         /* Determine path MTU. */
729         if ((error = ip6_getpmtu(ro_pmtu, ro != ro_pmtu, ifp, &ip6->ip6_dst,
730             &mtu, &alwaysfrag, fibnum)) != 0)
731                 goto bad;
732
733         /*
734          * The caller of this function may specify to use the minimum MTU
735          * in some cases.
736          * An advanced API option (IPV6_USE_MIN_MTU) can also override MTU
737          * setting.  The logic is a bit complicated; by default, unicast
738          * packets will follow path MTU while multicast packets will be sent at
739          * the minimum MTU.  If IP6PO_MINMTU_ALL is specified, all packets
740          * including unicast ones will be sent at the minimum MTU.  Multicast
741          * packets will always be sent at the minimum MTU unless
742          * IP6PO_MINMTU_DISABLE is explicitly specified.
743          * See RFC 3542 for more details.
744          */
745         if (mtu > IPV6_MMTU) {
746                 if ((flags & IPV6_MINMTU))
747                         mtu = IPV6_MMTU;
748                 else if (opt && opt->ip6po_minmtu == IP6PO_MINMTU_ALL)
749                         mtu = IPV6_MMTU;
750                 else if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst) &&
751                          (opt == NULL ||
752                           opt->ip6po_minmtu != IP6PO_MINMTU_DISABLE)) {
753                         mtu = IPV6_MMTU;
754                 }
755         }
756
757         /*
758          * clear embedded scope identifiers if necessary.
759          * in6_clearscope will touch the addresses only when necessary.
760          */
761         in6_clearscope(&ip6->ip6_src);
762         in6_clearscope(&ip6->ip6_dst);
763
764         /*
765          * If the outgoing packet contains a hop-by-hop options header,
766          * it must be examined and processed even by the source node.
767          * (RFC 2460, section 4.)
768          */
769         if (exthdrs.ip6e_hbh) {
770                 struct ip6_hbh *hbh = mtod(exthdrs.ip6e_hbh, struct ip6_hbh *);
771                 u_int32_t dummy; /* XXX unused */
772                 u_int32_t plen = 0; /* XXX: ip6_process will check the value */
773
774 #ifdef DIAGNOSTIC
775                 if ((hbh->ip6h_len + 1) << 3 > exthdrs.ip6e_hbh->m_len)
776                         panic("ip6e_hbh is not contiguous");
777 #endif
778                 /*
779                  *  XXX: if we have to send an ICMPv6 error to the sender,
780                  *       we need the M_LOOP flag since icmp6_error() expects
781                  *       the IPv6 and the hop-by-hop options header are
782                  *       contiguous unless the flag is set.
783                  */
784                 m->m_flags |= M_LOOP;
785                 m->m_pkthdr.rcvif = ifp;
786                 if (ip6_process_hopopts(m, (u_int8_t *)(hbh + 1),
787                     ((hbh->ip6h_len + 1) << 3) - sizeof(struct ip6_hbh),
788                     &dummy, &plen) < 0) {
789                         /* m was already freed at this point */
790                         error = EINVAL;/* better error? */
791                         goto done;
792                 }
793                 m->m_flags &= ~M_LOOP; /* XXX */
794                 m->m_pkthdr.rcvif = NULL;
795         }
796
797         /* Jump over all PFIL processing if hooks are not active. */
798         if (!PFIL_HOOKED(&V_inet6_pfil_hook))
799                 goto passout;
800
801         odst = ip6->ip6_dst;
802         /* Run through list of hooks for output packets. */
803         error = pfil_run_hooks(&V_inet6_pfil_hook, &m, ifp, PFIL_OUT, inp);
804         if (error != 0 || m == NULL)
805                 goto done;
806         ip6 = mtod(m, struct ip6_hdr *);
807
808         needfiblookup = 0;
809         /* See if destination IP address was changed by packet filter. */
810         if (!IN6_ARE_ADDR_EQUAL(&odst, &ip6->ip6_dst)) {
811                 m->m_flags |= M_SKIP_FIREWALL;
812                 /* If destination is now ourself drop to ip6_input(). */
813                 if (in6_localip(&ip6->ip6_dst)) {
814                         m->m_flags |= M_FASTFWD_OURS;
815                         if (m->m_pkthdr.rcvif == NULL)
816                                 m->m_pkthdr.rcvif = V_loif;
817                         if (m->m_pkthdr.csum_flags & CSUM_DELAY_DATA_IPV6) {
818                                 m->m_pkthdr.csum_flags |=
819                                     CSUM_DATA_VALID_IPV6 | CSUM_PSEUDO_HDR;
820                                 m->m_pkthdr.csum_data = 0xffff;
821                         }
822 #ifdef SCTP
823                         if (m->m_pkthdr.csum_flags & CSUM_SCTP_IPV6)
824                                 m->m_pkthdr.csum_flags |= CSUM_SCTP_VALID;
825 #endif
826                         error = netisr_queue(NETISR_IPV6, m);
827                         goto done;
828                 } else {
829                         RO_RTFREE(ro);
830                         needfiblookup = 1; /* Redo the routing table lookup. */
831                 }
832         }
833         /* See if fib was changed by packet filter. */
834         if (fibnum != M_GETFIB(m)) {
835                 m->m_flags |= M_SKIP_FIREWALL;
836                 fibnum = M_GETFIB(m);
837                 RO_RTFREE(ro);
838                 needfiblookup = 1;
839         }
840         if (needfiblookup)
841                 goto again;
842
843         /* See if local, if yes, send it to netisr. */
844         if (m->m_flags & M_FASTFWD_OURS) {
845                 if (m->m_pkthdr.rcvif == NULL)
846                         m->m_pkthdr.rcvif = V_loif;
847                 if (m->m_pkthdr.csum_flags & CSUM_DELAY_DATA_IPV6) {
848                         m->m_pkthdr.csum_flags |=
849                             CSUM_DATA_VALID_IPV6 | CSUM_PSEUDO_HDR;
850                         m->m_pkthdr.csum_data = 0xffff;
851                 }
852 #ifdef SCTP
853                 if (m->m_pkthdr.csum_flags & CSUM_SCTP_IPV6)
854                         m->m_pkthdr.csum_flags |= CSUM_SCTP_VALID;
855 #endif
856                 error = netisr_queue(NETISR_IPV6, m);
857                 goto done;
858         }
859         /* Or forward to some other address? */
860         if ((m->m_flags & M_IP6_NEXTHOP) &&
861             (fwd_tag = m_tag_find(m, PACKET_TAG_IPFORWARD, NULL)) != NULL) {
862                 dst = (struct sockaddr_in6 *)&ro->ro_dst;
863                 bcopy((fwd_tag+1), &dst_sa, sizeof(struct sockaddr_in6));
864                 m->m_flags |= M_SKIP_FIREWALL;
865                 m->m_flags &= ~M_IP6_NEXTHOP;
866                 m_tag_delete(m, fwd_tag);
867                 goto again;
868         }
869
870 passout:
871         /*
872          * Send the packet to the outgoing interface.
873          * If necessary, do IPv6 fragmentation before sending.
874          *
875          * the logic here is rather complex:
876          * 1: normal case (dontfrag == 0, alwaysfrag == 0)
877          * 1-a: send as is if tlen <= path mtu
878          * 1-b: fragment if tlen > path mtu
879          *
880          * 2: if user asks us not to fragment (dontfrag == 1)
881          * 2-a: send as is if tlen <= interface mtu
882          * 2-b: error if tlen > interface mtu
883          *
884          * 3: if we always need to attach fragment header (alwaysfrag == 1)
885          *      always fragment
886          *
887          * 4: if dontfrag == 1 && alwaysfrag == 1
888          *      error, as we cannot handle this conflicting request
889          */
890         sw_csum = m->m_pkthdr.csum_flags;
891         if (!hdrsplit) {
892                 tso = ((sw_csum & ifp->if_hwassist & CSUM_TSO) != 0) ? 1 : 0;
893                 sw_csum &= ~ifp->if_hwassist;
894         } else
895                 tso = 0;
896         /*
897          * If we added extension headers, we will not do TSO and calculate the
898          * checksums ourselves for now.
899          * XXX-BZ  Need a framework to know when the NIC can handle it, even
900          * with ext. hdrs.
901          */
902         if (sw_csum & CSUM_DELAY_DATA_IPV6) {
903                 sw_csum &= ~CSUM_DELAY_DATA_IPV6;
904                 in6_delayed_cksum(m, plen, sizeof(struct ip6_hdr));
905         }
906 #ifdef SCTP
907         if (sw_csum & CSUM_SCTP_IPV6) {
908                 sw_csum &= ~CSUM_SCTP_IPV6;
909                 sctp_delayed_cksum(m, sizeof(struct ip6_hdr));
910         }
911 #endif
912         m->m_pkthdr.csum_flags &= ifp->if_hwassist;
913         tlen = m->m_pkthdr.len;
914
915         if ((opt && (opt->ip6po_flags & IP6PO_DONTFRAG)) || tso)
916                 dontfrag = 1;
917         else
918                 dontfrag = 0;
919         if (dontfrag && alwaysfrag) {   /* case 4 */
920                 /* conflicting request - can't transmit */
921                 error = EMSGSIZE;
922                 goto bad;
923         }
924         if (dontfrag && tlen > IN6_LINKMTU(ifp) && !tso) {      /* case 2-b */
925                 /*
926                  * Even if the DONTFRAG option is specified, we cannot send the
927                  * packet when the data length is larger than the MTU of the
928                  * outgoing interface.
929                  * Notify the error by sending IPV6_PATHMTU ancillary data if
930                  * application wanted to know the MTU value. Also return an
931                  * error code (this is not described in the API spec).
932                  */
933                 if (inp != NULL)
934                         ip6_notify_pmtu(inp, &dst_sa, (u_int32_t)mtu);
935                 error = EMSGSIZE;
936                 goto bad;
937         }
938
939         /*
940          * transmit packet without fragmentation
941          */
942         if (dontfrag || (!alwaysfrag && tlen <= mtu)) { /* case 1-a and 2-a */
943                 struct in6_ifaddr *ia6;
944
945                 ip6 = mtod(m, struct ip6_hdr *);
946                 ia6 = in6_ifawithifp(ifp, &ip6->ip6_src);
947                 if (ia6) {
948                         /* Record statistics for this interface address. */
949                         counter_u64_add(ia6->ia_ifa.ifa_opackets, 1);
950                         counter_u64_add(ia6->ia_ifa.ifa_obytes,
951                             m->m_pkthdr.len);
952                         ifa_free(&ia6->ia_ifa);
953                 }
954                 error = nd6_output_ifp(ifp, origifp, m, dst,
955                     (struct route *)ro);
956                 goto done;
957         }
958
959         /*
960          * try to fragment the packet.  case 1-b and 3
961          */
962         if (mtu < IPV6_MMTU) {
963                 /* path MTU cannot be less than IPV6_MMTU */
964                 error = EMSGSIZE;
965                 in6_ifstat_inc(ifp, ifs6_out_fragfail);
966                 goto bad;
967         } else if (ip6->ip6_plen == 0) {
968                 /* jumbo payload cannot be fragmented */
969                 error = EMSGSIZE;
970                 in6_ifstat_inc(ifp, ifs6_out_fragfail);
971                 goto bad;
972         } else {
973                 u_char nextproto;
974
975                 /*
976                  * Too large for the destination or interface;
977                  * fragment if possible.
978                  * Must be able to put at least 8 bytes per fragment.
979                  */
980                 hlen = unfragpartlen;
981                 if (mtu > IPV6_MAXPACKET)
982                         mtu = IPV6_MAXPACKET;
983
984                 len = (mtu - hlen - sizeof(struct ip6_frag)) & ~7;
985                 if (len < 8) {
986                         error = EMSGSIZE;
987                         in6_ifstat_inc(ifp, ifs6_out_fragfail);
988                         goto bad;
989                 }
990
991                 /*
992                  * If the interface will not calculate checksums on
993                  * fragmented packets, then do it here.
994                  * XXX-BZ handle the hw offloading case.  Need flags.
995                  */
996                 if (m->m_pkthdr.csum_flags & CSUM_DELAY_DATA_IPV6) {
997                         in6_delayed_cksum(m, plen, hlen);
998                         m->m_pkthdr.csum_flags &= ~CSUM_DELAY_DATA_IPV6;
999                 }
1000 #ifdef SCTP
1001                 if (m->m_pkthdr.csum_flags & CSUM_SCTP_IPV6) {
1002                         sctp_delayed_cksum(m, hlen);
1003                         m->m_pkthdr.csum_flags &= ~CSUM_SCTP_IPV6;
1004                 }
1005 #endif
1006                 /*
1007                  * Change the next header field of the last header in the
1008                  * unfragmentable part.
1009                  */
1010                 if (exthdrs.ip6e_rthdr) {
1011                         nextproto = *mtod(exthdrs.ip6e_rthdr, u_char *);
1012                         *mtod(exthdrs.ip6e_rthdr, u_char *) = IPPROTO_FRAGMENT;
1013                 } else if (exthdrs.ip6e_dest1) {
1014                         nextproto = *mtod(exthdrs.ip6e_dest1, u_char *);
1015                         *mtod(exthdrs.ip6e_dest1, u_char *) = IPPROTO_FRAGMENT;
1016                 } else if (exthdrs.ip6e_hbh) {
1017                         nextproto = *mtod(exthdrs.ip6e_hbh, u_char *);
1018                         *mtod(exthdrs.ip6e_hbh, u_char *) = IPPROTO_FRAGMENT;
1019                 } else {
1020                         nextproto = ip6->ip6_nxt;
1021                         ip6->ip6_nxt = IPPROTO_FRAGMENT;
1022                 }
1023
1024                 /*
1025                  * Loop through length of segment after first fragment,
1026                  * make new header and copy data of each part and link onto
1027                  * chain.
1028                  */
1029                 m0 = m;
1030                 id = htonl(ip6_randomid());
1031                 if ((error = ip6_fragment(ifp, m, hlen, nextproto, len, id)))
1032                         goto sendorfree;
1033
1034                 in6_ifstat_inc(ifp, ifs6_out_fragok);
1035         }
1036
1037         /*
1038          * Remove leading garbages.
1039          */
1040 sendorfree:
1041         m = m0->m_nextpkt;
1042         m0->m_nextpkt = 0;
1043         m_freem(m0);
1044         for (m0 = m; m; m = m0) {
1045                 m0 = m->m_nextpkt;
1046                 m->m_nextpkt = 0;
1047                 if (error == 0) {
1048                         /* Record statistics for this interface address. */
1049                         if (ia) {
1050                                 counter_u64_add(ia->ia_ifa.ifa_opackets, 1);
1051                                 counter_u64_add(ia->ia_ifa.ifa_obytes,
1052                                     m->m_pkthdr.len);
1053                         }
1054                         error = nd6_output_ifp(ifp, origifp, m, dst,
1055                             (struct route *)ro);
1056                 } else
1057                         m_freem(m);
1058         }
1059
1060         if (error == 0)
1061                 IP6STAT_INC(ip6s_fragmented);
1062
1063 done:
1064         if (ro == &ip6route)
1065                 RO_RTFREE(ro);
1066         return (error);
1067
1068 freehdrs:
1069         m_freem(exthdrs.ip6e_hbh);      /* m_freem will check if mbuf is 0 */
1070         m_freem(exthdrs.ip6e_dest1);
1071         m_freem(exthdrs.ip6e_rthdr);
1072         m_freem(exthdrs.ip6e_dest2);
1073         /* FALLTHROUGH */
1074 bad:
1075         if (m)
1076                 m_freem(m);
1077         goto done;
1078 }
1079
1080 static int
1081 ip6_copyexthdr(struct mbuf **mp, caddr_t hdr, int hlen)
1082 {
1083         struct mbuf *m;
1084
1085         if (hlen > MCLBYTES)
1086                 return (ENOBUFS); /* XXX */
1087
1088         if (hlen > MLEN)
1089                 m = m_getcl(M_NOWAIT, MT_DATA, 0);
1090         else
1091                 m = m_get(M_NOWAIT, MT_DATA);
1092         if (m == NULL)
1093                 return (ENOBUFS);
1094         m->m_len = hlen;
1095         if (hdr)
1096                 bcopy(hdr, mtod(m, caddr_t), hlen);
1097
1098         *mp = m;
1099         return (0);
1100 }
1101
1102 /*
1103  * Insert jumbo payload option.
1104  */
1105 static int
1106 ip6_insert_jumboopt(struct ip6_exthdrs *exthdrs, u_int32_t plen)
1107 {
1108         struct mbuf *mopt;
1109         u_char *optbuf;
1110         u_int32_t v;
1111
1112 #define JUMBOOPTLEN     8       /* length of jumbo payload option and padding */
1113
1114         /*
1115          * If there is no hop-by-hop options header, allocate new one.
1116          * If there is one but it doesn't have enough space to store the
1117          * jumbo payload option, allocate a cluster to store the whole options.
1118          * Otherwise, use it to store the options.
1119          */
1120         if (exthdrs->ip6e_hbh == NULL) {
1121                 mopt = m_get(M_NOWAIT, MT_DATA);
1122                 if (mopt == NULL)
1123                         return (ENOBUFS);
1124                 mopt->m_len = JUMBOOPTLEN;
1125                 optbuf = mtod(mopt, u_char *);
1126                 optbuf[1] = 0;  /* = ((JUMBOOPTLEN) >> 3) - 1 */
1127                 exthdrs->ip6e_hbh = mopt;
1128         } else {
1129                 struct ip6_hbh *hbh;
1130
1131                 mopt = exthdrs->ip6e_hbh;
1132                 if (M_TRAILINGSPACE(mopt) < JUMBOOPTLEN) {
1133                         /*
1134                          * XXX assumption:
1135                          * - exthdrs->ip6e_hbh is not referenced from places
1136                          *   other than exthdrs.
1137                          * - exthdrs->ip6e_hbh is not an mbuf chain.
1138                          */
1139                         int oldoptlen = mopt->m_len;
1140                         struct mbuf *n;
1141
1142                         /*
1143                          * XXX: give up if the whole (new) hbh header does
1144                          * not fit even in an mbuf cluster.
1145                          */
1146                         if (oldoptlen + JUMBOOPTLEN > MCLBYTES)
1147                                 return (ENOBUFS);
1148
1149                         /*
1150                          * As a consequence, we must always prepare a cluster
1151                          * at this point.
1152                          */
1153                         n = m_getcl(M_NOWAIT, MT_DATA, 0);
1154                         if (n == NULL)
1155                                 return (ENOBUFS);
1156                         n->m_len = oldoptlen + JUMBOOPTLEN;
1157                         bcopy(mtod(mopt, caddr_t), mtod(n, caddr_t),
1158                             oldoptlen);
1159                         optbuf = mtod(n, caddr_t) + oldoptlen;
1160                         m_freem(mopt);
1161                         mopt = exthdrs->ip6e_hbh = n;
1162                 } else {
1163                         optbuf = mtod(mopt, u_char *) + mopt->m_len;
1164                         mopt->m_len += JUMBOOPTLEN;
1165                 }
1166                 optbuf[0] = IP6OPT_PADN;
1167                 optbuf[1] = 1;
1168
1169                 /*
1170                  * Adjust the header length according to the pad and
1171                  * the jumbo payload option.
1172                  */
1173                 hbh = mtod(mopt, struct ip6_hbh *);
1174                 hbh->ip6h_len += (JUMBOOPTLEN >> 3);
1175         }
1176
1177         /* fill in the option. */
1178         optbuf[2] = IP6OPT_JUMBO;
1179         optbuf[3] = 4;
1180         v = (u_int32_t)htonl(plen + JUMBOOPTLEN);
1181         bcopy(&v, &optbuf[4], sizeof(u_int32_t));
1182
1183         /* finally, adjust the packet header length */
1184         exthdrs->ip6e_ip6->m_pkthdr.len += JUMBOOPTLEN;
1185
1186         return (0);
1187 #undef JUMBOOPTLEN
1188 }
1189
1190 /*
1191  * Insert fragment header and copy unfragmentable header portions.
1192  */
1193 static int
1194 ip6_insertfraghdr(struct mbuf *m0, struct mbuf *m, int hlen,
1195     struct ip6_frag **frghdrp)
1196 {
1197         struct mbuf *n, *mlast;
1198
1199         if (hlen > sizeof(struct ip6_hdr)) {
1200                 n = m_copym(m0, sizeof(struct ip6_hdr),
1201                     hlen - sizeof(struct ip6_hdr), M_NOWAIT);
1202                 if (n == NULL)
1203                         return (ENOBUFS);
1204                 m->m_next = n;
1205         } else
1206                 n = m;
1207
1208         /* Search for the last mbuf of unfragmentable part. */
1209         for (mlast = n; mlast->m_next; mlast = mlast->m_next)
1210                 ;
1211
1212         if (M_WRITABLE(mlast) &&
1213             M_TRAILINGSPACE(mlast) >= sizeof(struct ip6_frag)) {
1214                 /* use the trailing space of the last mbuf for the fragment hdr */
1215                 *frghdrp = (struct ip6_frag *)(mtod(mlast, caddr_t) +
1216                     mlast->m_len);
1217                 mlast->m_len += sizeof(struct ip6_frag);
1218                 m->m_pkthdr.len += sizeof(struct ip6_frag);
1219         } else {
1220                 /* allocate a new mbuf for the fragment header */
1221                 struct mbuf *mfrg;
1222
1223                 mfrg = m_get(M_NOWAIT, MT_DATA);
1224                 if (mfrg == NULL)
1225                         return (ENOBUFS);
1226                 mfrg->m_len = sizeof(struct ip6_frag);
1227                 *frghdrp = mtod(mfrg, struct ip6_frag *);
1228                 mlast->m_next = mfrg;
1229         }
1230
1231         return (0);
1232 }
1233
1234 /*
1235  * Calculates IPv6 path mtu for destination @dst.
1236  * Resulting MTU is stored in @mtup.
1237  *
1238  * Returns 0 on success.
1239  */
1240 static int
1241 ip6_getpmtu_ctl(u_int fibnum, const struct in6_addr *dst, u_long *mtup)
1242 {
1243         struct nhop6_extended nh6;
1244         struct in6_addr kdst;
1245         uint32_t scopeid;
1246         struct ifnet *ifp;
1247         u_long mtu;
1248         int error;
1249
1250         in6_splitscope(dst, &kdst, &scopeid);
1251         if (fib6_lookup_nh_ext(fibnum, &kdst, scopeid, NHR_REF, 0, &nh6) != 0)
1252                 return (EHOSTUNREACH);
1253
1254         ifp = nh6.nh_ifp;
1255         mtu = nh6.nh_mtu;
1256
1257         error = ip6_calcmtu(ifp, dst, mtu, mtup, NULL);
1258         fib6_free_nh_ext(fibnum, &nh6);
1259
1260         return (error);
1261 }
1262
1263 /*
1264  * Calculates IPv6 path MTU for @dst based on transmit @ifp,
1265  * and cached data in @ro_pmtu.
1266  * MTU from (successful) route lookup is saved (along with dst)
1267  * inside @ro_pmtu to avoid subsequent route lookups after packet
1268  * filter processing.
1269  *
1270  * Stores mtu and always-frag value into @mtup and @alwaysfragp.
1271  * Returns 0 on success.
1272  */
1273 static int
1274 ip6_getpmtu(struct route_in6 *ro_pmtu, int do_lookup,
1275     struct ifnet *ifp, const struct in6_addr *dst, u_long *mtup,
1276     int *alwaysfragp, u_int fibnum)
1277 {
1278         struct nhop6_basic nh6;
1279         struct in6_addr kdst;
1280         uint32_t scopeid;
1281         struct sockaddr_in6 *sa6_dst;
1282         u_long mtu;
1283
1284         mtu = 0;
1285         if (do_lookup) {
1286
1287                 /*
1288                  * Here ro_pmtu has final destination address, while
1289                  * ro might represent immediate destination.
1290                  * Use ro_pmtu destination since mtu might differ.
1291                  */
1292                 sa6_dst = (struct sockaddr_in6 *)&ro_pmtu->ro_dst;
1293                 if (!IN6_ARE_ADDR_EQUAL(&sa6_dst->sin6_addr, dst))
1294                         ro_pmtu->ro_mtu = 0;
1295
1296                 if (ro_pmtu->ro_mtu == 0) {
1297                         bzero(sa6_dst, sizeof(*sa6_dst));
1298                         sa6_dst->sin6_family = AF_INET6;
1299                         sa6_dst->sin6_len = sizeof(struct sockaddr_in6);
1300                         sa6_dst->sin6_addr = *dst;
1301
1302                         in6_splitscope(dst, &kdst, &scopeid);
1303                         if (fib6_lookup_nh_basic(fibnum, &kdst, scopeid, 0, 0,
1304                             &nh6) == 0)
1305                                 ro_pmtu->ro_mtu = nh6.nh_mtu;
1306                 }
1307
1308                 mtu = ro_pmtu->ro_mtu;
1309         }
1310
1311         if (ro_pmtu->ro_rt)
1312                 mtu = ro_pmtu->ro_rt->rt_mtu;
1313
1314         return (ip6_calcmtu(ifp, dst, mtu, mtup, alwaysfragp));
1315 }
1316
1317 /*
1318  * Calculate MTU based on transmit @ifp, route mtu @rt_mtu and
1319  * hostcache data for @dst.
1320  * Stores mtu and always-frag value into @mtup and @alwaysfragp.
1321  *
1322  * Returns 0 on success.
1323  */
1324 static int
1325 ip6_calcmtu(struct ifnet *ifp, const struct in6_addr *dst, u_long rt_mtu,
1326     u_long *mtup, int *alwaysfragp)
1327 {
1328         u_long mtu = 0;
1329         int alwaysfrag = 0;
1330         int error = 0;
1331
1332         if (rt_mtu > 0) {
1333                 u_int32_t ifmtu;
1334                 struct in_conninfo inc;
1335
1336                 bzero(&inc, sizeof(inc));
1337                 inc.inc_flags |= INC_ISIPV6;
1338                 inc.inc6_faddr = *dst;
1339
1340                 ifmtu = IN6_LINKMTU(ifp);
1341                 mtu = tcp_hc_getmtu(&inc);
1342                 if (mtu)
1343                         mtu = min(mtu, rt_mtu);
1344                 else
1345                         mtu = rt_mtu;
1346                 if (mtu == 0)
1347                         mtu = ifmtu;
1348                 else if (mtu < IPV6_MMTU) {
1349                         /*
1350                          * RFC2460 section 5, last paragraph:
1351                          * if we record ICMPv6 too big message with
1352                          * mtu < IPV6_MMTU, transmit packets sized IPV6_MMTU
1353                          * or smaller, with framgent header attached.
1354                          * (fragment header is needed regardless from the
1355                          * packet size, for translators to identify packets)
1356                          */
1357                         alwaysfrag = 1;
1358                         mtu = IPV6_MMTU;
1359                 }
1360         } else if (ifp) {
1361                 mtu = IN6_LINKMTU(ifp);
1362         } else
1363                 error = EHOSTUNREACH; /* XXX */
1364
1365         *mtup = mtu;
1366         if (alwaysfragp)
1367                 *alwaysfragp = alwaysfrag;
1368         return (error);
1369 }
1370
1371 /*
1372  * IP6 socket option processing.
1373  */
1374 int
1375 ip6_ctloutput(struct socket *so, struct sockopt *sopt)
1376 {
1377         int optdatalen, uproto;
1378         void *optdata;
1379         struct inpcb *in6p = sotoinpcb(so);
1380         int error, optval;
1381         int level, op, optname;
1382         int optlen;
1383         struct thread *td;
1384 #ifdef  RSS
1385         uint32_t rss_bucket;
1386         int retval;
1387 #endif
1388
1389         level = sopt->sopt_level;
1390         op = sopt->sopt_dir;
1391         optname = sopt->sopt_name;
1392         optlen = sopt->sopt_valsize;
1393         td = sopt->sopt_td;
1394         error = 0;
1395         optval = 0;
1396         uproto = (int)so->so_proto->pr_protocol;
1397
1398         if (level != IPPROTO_IPV6) {
1399                 error = EINVAL;
1400
1401                 if (sopt->sopt_level == SOL_SOCKET &&
1402                     sopt->sopt_dir == SOPT_SET) {
1403                         switch (sopt->sopt_name) {
1404                         case SO_REUSEADDR:
1405                                 INP_WLOCK(in6p);
1406                                 if ((so->so_options & SO_REUSEADDR) != 0)
1407                                         in6p->inp_flags2 |= INP_REUSEADDR;
1408                                 else
1409                                         in6p->inp_flags2 &= ~INP_REUSEADDR;
1410                                 INP_WUNLOCK(in6p);
1411                                 error = 0;
1412                                 break;
1413                         case SO_REUSEPORT:
1414                                 INP_WLOCK(in6p);
1415                                 if ((so->so_options & SO_REUSEPORT) != 0)
1416                                         in6p->inp_flags2 |= INP_REUSEPORT;
1417                                 else
1418                                         in6p->inp_flags2 &= ~INP_REUSEPORT;
1419                                 INP_WUNLOCK(in6p);
1420                                 error = 0;
1421                                 break;
1422                         case SO_SETFIB:
1423                                 INP_WLOCK(in6p);
1424                                 in6p->inp_inc.inc_fibnum = so->so_fibnum;
1425                                 INP_WUNLOCK(in6p);
1426                                 error = 0;
1427                                 break;
1428                         default:
1429                                 break;
1430                         }
1431                 }
1432         } else {                /* level == IPPROTO_IPV6 */
1433                 switch (op) {
1434
1435                 case SOPT_SET:
1436                         switch (optname) {
1437                         case IPV6_2292PKTOPTIONS:
1438 #ifdef IPV6_PKTOPTIONS
1439                         case IPV6_PKTOPTIONS:
1440 #endif
1441                         {
1442                                 struct mbuf *m;
1443
1444                                 error = soopt_getm(sopt, &m); /* XXX */
1445                                 if (error != 0)
1446                                         break;
1447                                 error = soopt_mcopyin(sopt, m); /* XXX */
1448                                 if (error != 0)
1449                                         break;
1450                                 error = ip6_pcbopts(&in6p->in6p_outputopts,
1451                                                     m, so, sopt);
1452                                 m_freem(m); /* XXX */
1453                                 break;
1454                         }
1455
1456                         /*
1457                          * Use of some Hop-by-Hop options or some
1458                          * Destination options, might require special
1459                          * privilege.  That is, normal applications
1460                          * (without special privilege) might be forbidden
1461                          * from setting certain options in outgoing packets,
1462                          * and might never see certain options in received
1463                          * packets. [RFC 2292 Section 6]
1464                          * KAME specific note:
1465                          *  KAME prevents non-privileged users from sending or
1466                          *  receiving ANY hbh/dst options in order to avoid
1467                          *  overhead of parsing options in the kernel.
1468                          */
1469                         case IPV6_RECVHOPOPTS:
1470                         case IPV6_RECVDSTOPTS:
1471                         case IPV6_RECVRTHDRDSTOPTS:
1472                                 if (td != NULL) {
1473                                         error = priv_check(td,
1474                                             PRIV_NETINET_SETHDROPTS);
1475                                         if (error)
1476                                                 break;
1477                                 }
1478                                 /* FALLTHROUGH */
1479                         case IPV6_UNICAST_HOPS:
1480                         case IPV6_HOPLIMIT:
1481
1482                         case IPV6_RECVPKTINFO:
1483                         case IPV6_RECVHOPLIMIT:
1484                         case IPV6_RECVRTHDR:
1485                         case IPV6_RECVPATHMTU:
1486                         case IPV6_RECVTCLASS:
1487                         case IPV6_RECVFLOWID:
1488 #ifdef  RSS
1489                         case IPV6_RECVRSSBUCKETID:
1490 #endif
1491                         case IPV6_V6ONLY:
1492                         case IPV6_AUTOFLOWLABEL:
1493                         case IPV6_BINDANY:
1494                         case IPV6_BINDMULTI:
1495 #ifdef  RSS
1496                         case IPV6_RSS_LISTEN_BUCKET:
1497 #endif
1498                                 if (optname == IPV6_BINDANY && td != NULL) {
1499                                         error = priv_check(td,
1500                                             PRIV_NETINET_BINDANY);
1501                                         if (error)
1502                                                 break;
1503                                 }
1504
1505                                 if (optlen != sizeof(int)) {
1506                                         error = EINVAL;
1507                                         break;
1508                                 }
1509                                 error = sooptcopyin(sopt, &optval,
1510                                         sizeof optval, sizeof optval);
1511                                 if (error)
1512                                         break;
1513                                 switch (optname) {
1514
1515                                 case IPV6_UNICAST_HOPS:
1516                                         if (optval < -1 || optval >= 256)
1517                                                 error = EINVAL;
1518                                         else {
1519                                                 /* -1 = kernel default */
1520                                                 in6p->in6p_hops = optval;
1521                                                 if ((in6p->inp_vflag &
1522                                                      INP_IPV4) != 0)
1523                                                         in6p->inp_ip_ttl = optval;
1524                                         }
1525                                         break;
1526 #define OPTSET(bit) \
1527 do { \
1528         INP_WLOCK(in6p); \
1529         if (optval) \
1530                 in6p->inp_flags |= (bit); \
1531         else \
1532                 in6p->inp_flags &= ~(bit); \
1533         INP_WUNLOCK(in6p); \
1534 } while (/*CONSTCOND*/ 0)
1535 #define OPTSET2292(bit) \
1536 do { \
1537         INP_WLOCK(in6p); \
1538         in6p->inp_flags |= IN6P_RFC2292; \
1539         if (optval) \
1540                 in6p->inp_flags |= (bit); \
1541         else \
1542                 in6p->inp_flags &= ~(bit); \
1543         INP_WUNLOCK(in6p); \
1544 } while (/*CONSTCOND*/ 0)
1545 #define OPTBIT(bit) (in6p->inp_flags & (bit) ? 1 : 0)
1546
1547 #define OPTSET2(bit, val) do {                                          \
1548         INP_WLOCK(in6p);                                                \
1549         if (val)                                                        \
1550                 in6p->inp_flags2 |= bit;                                \
1551         else                                                            \
1552                 in6p->inp_flags2 &= ~bit;                               \
1553         INP_WUNLOCK(in6p);                                              \
1554 } while (0)
1555 #define OPTBIT2(bit) (in6p->inp_flags2 & (bit) ? 1 : 0)
1556
1557                                 case IPV6_RECVPKTINFO:
1558                                         /* cannot mix with RFC2292 */
1559                                         if (OPTBIT(IN6P_RFC2292)) {
1560                                                 error = EINVAL;
1561                                                 break;
1562                                         }
1563                                         OPTSET(IN6P_PKTINFO);
1564                                         break;
1565
1566                                 case IPV6_HOPLIMIT:
1567                                 {
1568                                         struct ip6_pktopts **optp;
1569
1570                                         /* cannot mix with RFC2292 */
1571                                         if (OPTBIT(IN6P_RFC2292)) {
1572                                                 error = EINVAL;
1573                                                 break;
1574                                         }
1575                                         optp = &in6p->in6p_outputopts;
1576                                         error = ip6_pcbopt(IPV6_HOPLIMIT,
1577                                             (u_char *)&optval, sizeof(optval),
1578                                             optp, (td != NULL) ? td->td_ucred :
1579                                             NULL, uproto);
1580                                         break;
1581                                 }
1582
1583                                 case IPV6_RECVHOPLIMIT:
1584                                         /* cannot mix with RFC2292 */
1585                                         if (OPTBIT(IN6P_RFC2292)) {
1586                                                 error = EINVAL;
1587                                                 break;
1588                                         }
1589                                         OPTSET(IN6P_HOPLIMIT);
1590                                         break;
1591
1592                                 case IPV6_RECVHOPOPTS:
1593                                         /* cannot mix with RFC2292 */
1594                                         if (OPTBIT(IN6P_RFC2292)) {
1595                                                 error = EINVAL;
1596                                                 break;
1597                                         }
1598                                         OPTSET(IN6P_HOPOPTS);
1599                                         break;
1600
1601                                 case IPV6_RECVDSTOPTS:
1602                                         /* cannot mix with RFC2292 */
1603                                         if (OPTBIT(IN6P_RFC2292)) {
1604                                                 error = EINVAL;
1605                                                 break;
1606                                         }
1607                                         OPTSET(IN6P_DSTOPTS);
1608                                         break;
1609
1610                                 case IPV6_RECVRTHDRDSTOPTS:
1611                                         /* cannot mix with RFC2292 */
1612                                         if (OPTBIT(IN6P_RFC2292)) {
1613                                                 error = EINVAL;
1614                                                 break;
1615                                         }
1616                                         OPTSET(IN6P_RTHDRDSTOPTS);
1617                                         break;
1618
1619                                 case IPV6_RECVRTHDR:
1620                                         /* cannot mix with RFC2292 */
1621                                         if (OPTBIT(IN6P_RFC2292)) {
1622                                                 error = EINVAL;
1623                                                 break;
1624                                         }
1625                                         OPTSET(IN6P_RTHDR);
1626                                         break;
1627
1628                                 case IPV6_RECVPATHMTU:
1629                                         /*
1630                                          * We ignore this option for TCP
1631                                          * sockets.
1632                                          * (RFC3542 leaves this case
1633                                          * unspecified.)
1634                                          */
1635                                         if (uproto != IPPROTO_TCP)
1636                                                 OPTSET(IN6P_MTU);
1637                                         break;
1638
1639                                 case IPV6_RECVFLOWID:
1640                                         OPTSET2(INP_RECVFLOWID, optval);
1641                                         break;
1642
1643 #ifdef  RSS
1644                                 case IPV6_RECVRSSBUCKETID:
1645                                         OPTSET2(INP_RECVRSSBUCKETID, optval);
1646                                         break;
1647 #endif
1648
1649                                 case IPV6_V6ONLY:
1650                                         /*
1651                                          * make setsockopt(IPV6_V6ONLY)
1652                                          * available only prior to bind(2).
1653                                          * see ipng mailing list, Jun 22 2001.
1654                                          */
1655                                         if (in6p->inp_lport ||
1656                                             !IN6_IS_ADDR_UNSPECIFIED(&in6p->in6p_laddr)) {
1657                                                 error = EINVAL;
1658                                                 break;
1659                                         }
1660                                         OPTSET(IN6P_IPV6_V6ONLY);
1661                                         if (optval)
1662                                                 in6p->inp_vflag &= ~INP_IPV4;
1663                                         else
1664                                                 in6p->inp_vflag |= INP_IPV4;
1665                                         break;
1666                                 case IPV6_RECVTCLASS:
1667                                         /* cannot mix with RFC2292 XXX */
1668                                         if (OPTBIT(IN6P_RFC2292)) {
1669                                                 error = EINVAL;
1670                                                 break;
1671                                         }
1672                                         OPTSET(IN6P_TCLASS);
1673                                         break;
1674                                 case IPV6_AUTOFLOWLABEL:
1675                                         OPTSET(IN6P_AUTOFLOWLABEL);
1676                                         break;
1677
1678                                 case IPV6_BINDANY:
1679                                         OPTSET(INP_BINDANY);
1680                                         break;
1681
1682                                 case IPV6_BINDMULTI:
1683                                         OPTSET2(INP_BINDMULTI, optval);
1684                                         break;
1685 #ifdef  RSS
1686                                 case IPV6_RSS_LISTEN_BUCKET:
1687                                         if ((optval >= 0) &&
1688                                             (optval < rss_getnumbuckets())) {
1689                                                 in6p->inp_rss_listen_bucket = optval;
1690                                                 OPTSET2(INP_RSS_BUCKET_SET, 1);
1691                                         } else {
1692                                                 error = EINVAL;
1693                                         }
1694                                         break;
1695 #endif
1696                                 }
1697                                 break;
1698
1699                         case IPV6_TCLASS:
1700                         case IPV6_DONTFRAG:
1701                         case IPV6_USE_MIN_MTU:
1702                         case IPV6_PREFER_TEMPADDR:
1703                                 if (optlen != sizeof(optval)) {
1704                                         error = EINVAL;
1705                                         break;
1706                                 }
1707                                 error = sooptcopyin(sopt, &optval,
1708                                         sizeof optval, sizeof optval);
1709                                 if (error)
1710                                         break;
1711                                 {
1712                                         struct ip6_pktopts **optp;
1713                                         optp = &in6p->in6p_outputopts;
1714                                         error = ip6_pcbopt(optname,
1715                                             (u_char *)&optval, sizeof(optval),
1716                                             optp, (td != NULL) ? td->td_ucred :
1717                                             NULL, uproto);
1718                                         break;
1719                                 }
1720
1721                         case IPV6_2292PKTINFO:
1722                         case IPV6_2292HOPLIMIT:
1723                         case IPV6_2292HOPOPTS:
1724                         case IPV6_2292DSTOPTS:
1725                         case IPV6_2292RTHDR:
1726                                 /* RFC 2292 */
1727                                 if (optlen != sizeof(int)) {
1728                                         error = EINVAL;
1729                                         break;
1730                                 }
1731                                 error = sooptcopyin(sopt, &optval,
1732                                         sizeof optval, sizeof optval);
1733                                 if (error)
1734                                         break;
1735                                 switch (optname) {
1736                                 case IPV6_2292PKTINFO:
1737                                         OPTSET2292(IN6P_PKTINFO);
1738                                         break;
1739                                 case IPV6_2292HOPLIMIT:
1740                                         OPTSET2292(IN6P_HOPLIMIT);
1741                                         break;
1742                                 case IPV6_2292HOPOPTS:
1743                                         /*
1744                                          * Check super-user privilege.
1745                                          * See comments for IPV6_RECVHOPOPTS.
1746                                          */
1747                                         if (td != NULL) {
1748                                                 error = priv_check(td,
1749                                                     PRIV_NETINET_SETHDROPTS);
1750                                                 if (error)
1751                                                         return (error);
1752                                         }
1753                                         OPTSET2292(IN6P_HOPOPTS);
1754                                         break;
1755                                 case IPV6_2292DSTOPTS:
1756                                         if (td != NULL) {
1757                                                 error = priv_check(td,
1758                                                     PRIV_NETINET_SETHDROPTS);
1759                                                 if (error)
1760                                                         return (error);
1761                                         }
1762                                         OPTSET2292(IN6P_DSTOPTS|IN6P_RTHDRDSTOPTS); /* XXX */
1763                                         break;
1764                                 case IPV6_2292RTHDR:
1765                                         OPTSET2292(IN6P_RTHDR);
1766                                         break;
1767                                 }
1768                                 break;
1769                         case IPV6_PKTINFO:
1770                         case IPV6_HOPOPTS:
1771                         case IPV6_RTHDR:
1772                         case IPV6_DSTOPTS:
1773                         case IPV6_RTHDRDSTOPTS:
1774                         case IPV6_NEXTHOP:
1775                         {
1776                                 /* new advanced API (RFC3542) */
1777                                 u_char *optbuf;
1778                                 u_char optbuf_storage[MCLBYTES];
1779                                 int optlen;
1780                                 struct ip6_pktopts **optp;
1781
1782                                 /* cannot mix with RFC2292 */
1783                                 if (OPTBIT(IN6P_RFC2292)) {
1784                                         error = EINVAL;
1785                                         break;
1786                                 }
1787
1788                                 /*
1789                                  * We only ensure valsize is not too large
1790                                  * here.  Further validation will be done
1791                                  * later.
1792                                  */
1793                                 error = sooptcopyin(sopt, optbuf_storage,
1794                                     sizeof(optbuf_storage), 0);
1795                                 if (error)
1796                                         break;
1797                                 optlen = sopt->sopt_valsize;
1798                                 optbuf = optbuf_storage;
1799                                 optp = &in6p->in6p_outputopts;
1800                                 error = ip6_pcbopt(optname, optbuf, optlen,
1801                                     optp, (td != NULL) ? td->td_ucred : NULL,
1802                                     uproto);
1803                                 break;
1804                         }
1805 #undef OPTSET
1806
1807                         case IPV6_MULTICAST_IF:
1808                         case IPV6_MULTICAST_HOPS:
1809                         case IPV6_MULTICAST_LOOP:
1810                         case IPV6_JOIN_GROUP:
1811                         case IPV6_LEAVE_GROUP:
1812                         case IPV6_MSFILTER:
1813                         case MCAST_BLOCK_SOURCE:
1814                         case MCAST_UNBLOCK_SOURCE:
1815                         case MCAST_JOIN_GROUP:
1816                         case MCAST_LEAVE_GROUP:
1817                         case MCAST_JOIN_SOURCE_GROUP:
1818                         case MCAST_LEAVE_SOURCE_GROUP:
1819                                 error = ip6_setmoptions(in6p, sopt);
1820                                 break;
1821
1822                         case IPV6_PORTRANGE:
1823                                 error = sooptcopyin(sopt, &optval,
1824                                     sizeof optval, sizeof optval);
1825                                 if (error)
1826                                         break;
1827
1828                                 INP_WLOCK(in6p);
1829                                 switch (optval) {
1830                                 case IPV6_PORTRANGE_DEFAULT:
1831                                         in6p->inp_flags &= ~(INP_LOWPORT);
1832                                         in6p->inp_flags &= ~(INP_HIGHPORT);
1833                                         break;
1834
1835                                 case IPV6_PORTRANGE_HIGH:
1836                                         in6p->inp_flags &= ~(INP_LOWPORT);
1837                                         in6p->inp_flags |= INP_HIGHPORT;
1838                                         break;
1839
1840                                 case IPV6_PORTRANGE_LOW:
1841                                         in6p->inp_flags &= ~(INP_HIGHPORT);
1842                                         in6p->inp_flags |= INP_LOWPORT;
1843                                         break;
1844
1845                                 default:
1846                                         error = EINVAL;
1847                                         break;
1848                                 }
1849                                 INP_WUNLOCK(in6p);
1850                                 break;
1851
1852 #ifdef IPSEC
1853                         case IPV6_IPSEC_POLICY:
1854                         {
1855                                 caddr_t req;
1856                                 struct mbuf *m;
1857
1858                                 if ((error = soopt_getm(sopt, &m)) != 0) /* XXX */
1859                                         break;
1860                                 if ((error = soopt_mcopyin(sopt, m)) != 0) /* XXX */
1861                                         break;
1862                                 req = mtod(m, caddr_t);
1863                                 error = ipsec_set_policy(in6p, optname, req,
1864                                     m->m_len, (sopt->sopt_td != NULL) ?
1865                                     sopt->sopt_td->td_ucred : NULL);
1866                                 m_freem(m);
1867                                 break;
1868                         }
1869 #endif /* IPSEC */
1870
1871                         default:
1872                                 error = ENOPROTOOPT;
1873                                 break;
1874                         }
1875                         break;
1876
1877                 case SOPT_GET:
1878                         switch (optname) {
1879
1880                         case IPV6_2292PKTOPTIONS:
1881 #ifdef IPV6_PKTOPTIONS
1882                         case IPV6_PKTOPTIONS:
1883 #endif
1884                                 /*
1885                                  * RFC3542 (effectively) deprecated the
1886                                  * semantics of the 2292-style pktoptions.
1887                                  * Since it was not reliable in nature (i.e.,
1888                                  * applications had to expect the lack of some
1889                                  * information after all), it would make sense
1890                                  * to simplify this part by always returning
1891                                  * empty data.
1892                                  */
1893                                 sopt->sopt_valsize = 0;
1894                                 break;
1895
1896                         case IPV6_RECVHOPOPTS:
1897                         case IPV6_RECVDSTOPTS:
1898                         case IPV6_RECVRTHDRDSTOPTS:
1899                         case IPV6_UNICAST_HOPS:
1900                         case IPV6_RECVPKTINFO:
1901                         case IPV6_RECVHOPLIMIT:
1902                         case IPV6_RECVRTHDR:
1903                         case IPV6_RECVPATHMTU:
1904
1905                         case IPV6_V6ONLY:
1906                         case IPV6_PORTRANGE:
1907                         case IPV6_RECVTCLASS:
1908                         case IPV6_AUTOFLOWLABEL:
1909                         case IPV6_BINDANY:
1910                         case IPV6_FLOWID:
1911                         case IPV6_FLOWTYPE:
1912                         case IPV6_RECVFLOWID:
1913 #ifdef  RSS
1914                         case IPV6_RSSBUCKETID:
1915                         case IPV6_RECVRSSBUCKETID:
1916 #endif
1917                         case IPV6_BINDMULTI:
1918                                 switch (optname) {
1919
1920                                 case IPV6_RECVHOPOPTS:
1921                                         optval = OPTBIT(IN6P_HOPOPTS);
1922                                         break;
1923
1924                                 case IPV6_RECVDSTOPTS:
1925                                         optval = OPTBIT(IN6P_DSTOPTS);
1926                                         break;
1927
1928                                 case IPV6_RECVRTHDRDSTOPTS:
1929                                         optval = OPTBIT(IN6P_RTHDRDSTOPTS);
1930                                         break;
1931
1932                                 case IPV6_UNICAST_HOPS:
1933                                         optval = in6p->in6p_hops;
1934                                         break;
1935
1936                                 case IPV6_RECVPKTINFO:
1937                                         optval = OPTBIT(IN6P_PKTINFO);
1938                                         break;
1939
1940                                 case IPV6_RECVHOPLIMIT:
1941                                         optval = OPTBIT(IN6P_HOPLIMIT);
1942                                         break;
1943
1944                                 case IPV6_RECVRTHDR:
1945                                         optval = OPTBIT(IN6P_RTHDR);
1946                                         break;
1947
1948                                 case IPV6_RECVPATHMTU:
1949                                         optval = OPTBIT(IN6P_MTU);
1950                                         break;
1951
1952                                 case IPV6_V6ONLY:
1953                                         optval = OPTBIT(IN6P_IPV6_V6ONLY);
1954                                         break;
1955
1956                                 case IPV6_PORTRANGE:
1957                                     {
1958                                         int flags;
1959                                         flags = in6p->inp_flags;
1960                                         if (flags & INP_HIGHPORT)
1961                                                 optval = IPV6_PORTRANGE_HIGH;
1962                                         else if (flags & INP_LOWPORT)
1963                                                 optval = IPV6_PORTRANGE_LOW;
1964                                         else
1965                                                 optval = 0;
1966                                         break;
1967                                     }
1968                                 case IPV6_RECVTCLASS:
1969                                         optval = OPTBIT(IN6P_TCLASS);
1970                                         break;
1971
1972                                 case IPV6_AUTOFLOWLABEL:
1973                                         optval = OPTBIT(IN6P_AUTOFLOWLABEL);
1974                                         break;
1975
1976                                 case IPV6_BINDANY:
1977                                         optval = OPTBIT(INP_BINDANY);
1978                                         break;
1979
1980                                 case IPV6_FLOWID:
1981                                         optval = in6p->inp_flowid;
1982                                         break;
1983
1984                                 case IPV6_FLOWTYPE:
1985                                         optval = in6p->inp_flowtype;
1986                                         break;
1987
1988                                 case IPV6_RECVFLOWID:
1989                                         optval = OPTBIT2(INP_RECVFLOWID);
1990                                         break;
1991 #ifdef  RSS
1992                                 case IPV6_RSSBUCKETID:
1993                                         retval =
1994                                             rss_hash2bucket(in6p->inp_flowid,
1995                                             in6p->inp_flowtype,
1996                                             &rss_bucket);
1997                                         if (retval == 0)
1998                                                 optval = rss_bucket;
1999                                         else
2000                                                 error = EINVAL;
2001                                         break;
2002
2003                                 case IPV6_RECVRSSBUCKETID:
2004                                         optval = OPTBIT2(INP_RECVRSSBUCKETID);
2005                                         break;
2006 #endif
2007
2008                                 case IPV6_BINDMULTI:
2009                                         optval = OPTBIT2(INP_BINDMULTI);
2010                                         break;
2011
2012                                 }
2013                                 if (error)
2014                                         break;
2015                                 error = sooptcopyout(sopt, &optval,
2016                                         sizeof optval);
2017                                 break;
2018
2019                         case IPV6_PATHMTU:
2020                         {
2021                                 u_long pmtu = 0;
2022                                 struct ip6_mtuinfo mtuinfo;
2023
2024                                 if (!(so->so_state & SS_ISCONNECTED))
2025                                         return (ENOTCONN);
2026                                 /*
2027                                  * XXX: we dot not consider the case of source
2028                                  * routing, or optional information to specify
2029                                  * the outgoing interface.
2030                                  */
2031                                 error = ip6_getpmtu_ctl(so->so_fibnum,
2032                                     &in6p->in6p_faddr, &pmtu);
2033                                 if (error)
2034                                         break;
2035                                 if (pmtu > IPV6_MAXPACKET)
2036                                         pmtu = IPV6_MAXPACKET;
2037
2038                                 bzero(&mtuinfo, sizeof(mtuinfo));
2039                                 mtuinfo.ip6m_mtu = (u_int32_t)pmtu;
2040                                 optdata = (void *)&mtuinfo;
2041                                 optdatalen = sizeof(mtuinfo);
2042                                 error = sooptcopyout(sopt, optdata,
2043                                     optdatalen);
2044                                 break;
2045                         }
2046
2047                         case IPV6_2292PKTINFO:
2048                         case IPV6_2292HOPLIMIT:
2049                         case IPV6_2292HOPOPTS:
2050                         case IPV6_2292RTHDR:
2051                         case IPV6_2292DSTOPTS:
2052                                 switch (optname) {
2053                                 case IPV6_2292PKTINFO:
2054                                         optval = OPTBIT(IN6P_PKTINFO);
2055                                         break;
2056                                 case IPV6_2292HOPLIMIT:
2057                                         optval = OPTBIT(IN6P_HOPLIMIT);
2058                                         break;
2059                                 case IPV6_2292HOPOPTS:
2060                                         optval = OPTBIT(IN6P_HOPOPTS);
2061                                         break;
2062                                 case IPV6_2292RTHDR:
2063                                         optval = OPTBIT(IN6P_RTHDR);
2064                                         break;
2065                                 case IPV6_2292DSTOPTS:
2066                                         optval = OPTBIT(IN6P_DSTOPTS|IN6P_RTHDRDSTOPTS);
2067                                         break;
2068                                 }
2069                                 error = sooptcopyout(sopt, &optval,
2070                                     sizeof optval);
2071                                 break;
2072                         case IPV6_PKTINFO:
2073                         case IPV6_HOPOPTS:
2074                         case IPV6_RTHDR:
2075                         case IPV6_DSTOPTS:
2076                         case IPV6_RTHDRDSTOPTS:
2077                         case IPV6_NEXTHOP:
2078                         case IPV6_TCLASS:
2079                         case IPV6_DONTFRAG:
2080                         case IPV6_USE_MIN_MTU:
2081                         case IPV6_PREFER_TEMPADDR:
2082                                 error = ip6_getpcbopt(in6p->in6p_outputopts,
2083                                     optname, sopt);
2084                                 break;
2085
2086                         case IPV6_MULTICAST_IF:
2087                         case IPV6_MULTICAST_HOPS:
2088                         case IPV6_MULTICAST_LOOP:
2089                         case IPV6_MSFILTER:
2090                                 error = ip6_getmoptions(in6p, sopt);
2091                                 break;
2092
2093 #ifdef IPSEC
2094                         case IPV6_IPSEC_POLICY:
2095                           {
2096                                 caddr_t req = NULL;
2097                                 size_t len = 0;
2098                                 struct mbuf *m = NULL;
2099                                 struct mbuf **mp = &m;
2100                                 size_t ovalsize = sopt->sopt_valsize;
2101                                 caddr_t oval = (caddr_t)sopt->sopt_val;
2102
2103                                 error = soopt_getm(sopt, &m); /* XXX */
2104                                 if (error != 0)
2105                                         break;
2106                                 error = soopt_mcopyin(sopt, m); /* XXX */
2107                                 if (error != 0)
2108                                         break;
2109                                 sopt->sopt_valsize = ovalsize;
2110                                 sopt->sopt_val = oval;
2111                                 if (m) {
2112                                         req = mtod(m, caddr_t);
2113                                         len = m->m_len;
2114                                 }
2115                                 error = ipsec_get_policy(in6p, req, len, mp);
2116                                 if (error == 0)
2117                                         error = soopt_mcopyout(sopt, m); /* XXX */
2118                                 if (error == 0 && m)
2119                                         m_freem(m);
2120                                 break;
2121                           }
2122 #endif /* IPSEC */
2123
2124                         default:
2125                                 error = ENOPROTOOPT;
2126                                 break;
2127                         }
2128                         break;
2129                 }
2130         }
2131         return (error);
2132 }
2133
2134 int
2135 ip6_raw_ctloutput(struct socket *so, struct sockopt *sopt)
2136 {
2137         int error = 0, optval, optlen;
2138         const int icmp6off = offsetof(struct icmp6_hdr, icmp6_cksum);
2139         struct inpcb *in6p = sotoinpcb(so);
2140         int level, op, optname;
2141
2142         level = sopt->sopt_level;
2143         op = sopt->sopt_dir;
2144         optname = sopt->sopt_name;
2145         optlen = sopt->sopt_valsize;
2146
2147         if (level != IPPROTO_IPV6) {
2148                 return (EINVAL);
2149         }
2150
2151         switch (optname) {
2152         case IPV6_CHECKSUM:
2153                 /*
2154                  * For ICMPv6 sockets, no modification allowed for checksum
2155                  * offset, permit "no change" values to help existing apps.
2156                  *
2157                  * RFC3542 says: "An attempt to set IPV6_CHECKSUM
2158                  * for an ICMPv6 socket will fail."
2159                  * The current behavior does not meet RFC3542.
2160                  */
2161                 switch (op) {
2162                 case SOPT_SET:
2163                         if (optlen != sizeof(int)) {
2164                                 error = EINVAL;
2165                                 break;
2166                         }
2167                         error = sooptcopyin(sopt, &optval, sizeof(optval),
2168                                             sizeof(optval));
2169                         if (error)
2170                                 break;
2171                         if ((optval % 2) != 0) {
2172                                 /* the API assumes even offset values */
2173                                 error = EINVAL;
2174                         } else if (so->so_proto->pr_protocol ==
2175                             IPPROTO_ICMPV6) {
2176                                 if (optval != icmp6off)
2177                                         error = EINVAL;
2178                         } else
2179                                 in6p->in6p_cksum = optval;
2180                         break;
2181
2182                 case SOPT_GET:
2183                         if (so->so_proto->pr_protocol == IPPROTO_ICMPV6)
2184                                 optval = icmp6off;
2185                         else
2186                                 optval = in6p->in6p_cksum;
2187
2188                         error = sooptcopyout(sopt, &optval, sizeof(optval));
2189                         break;
2190
2191                 default:
2192                         error = EINVAL;
2193                         break;
2194                 }
2195                 break;
2196
2197         default:
2198                 error = ENOPROTOOPT;
2199                 break;
2200         }
2201
2202         return (error);
2203 }
2204
2205 /*
2206  * Set up IP6 options in pcb for insertion in output packets or
2207  * specifying behavior of outgoing packets.
2208  */
2209 static int
2210 ip6_pcbopts(struct ip6_pktopts **pktopt, struct mbuf *m,
2211     struct socket *so, struct sockopt *sopt)
2212 {
2213         struct ip6_pktopts *opt = *pktopt;
2214         int error = 0;
2215         struct thread *td = sopt->sopt_td;
2216
2217         /* turn off any old options. */
2218         if (opt) {
2219 #ifdef DIAGNOSTIC
2220                 if (opt->ip6po_pktinfo || opt->ip6po_nexthop ||
2221                     opt->ip6po_hbh || opt->ip6po_dest1 || opt->ip6po_dest2 ||
2222                     opt->ip6po_rhinfo.ip6po_rhi_rthdr)
2223                         printf("ip6_pcbopts: all specified options are cleared.\n");
2224 #endif
2225                 ip6_clearpktopts(opt, -1);
2226         } else
2227                 opt = malloc(sizeof(*opt), M_IP6OPT, M_WAITOK);
2228         *pktopt = NULL;
2229
2230         if (!m || m->m_len == 0) {
2231                 /*
2232                  * Only turning off any previous options, regardless of
2233                  * whether the opt is just created or given.
2234                  */
2235                 free(opt, M_IP6OPT);
2236                 return (0);
2237         }
2238
2239         /*  set options specified by user. */
2240         if ((error = ip6_setpktopts(m, opt, NULL, (td != NULL) ?
2241             td->td_ucred : NULL, so->so_proto->pr_protocol)) != 0) {
2242                 ip6_clearpktopts(opt, -1); /* XXX: discard all options */
2243                 free(opt, M_IP6OPT);
2244                 return (error);
2245         }
2246         *pktopt = opt;
2247         return (0);
2248 }
2249
2250 /*
2251  * initialize ip6_pktopts.  beware that there are non-zero default values in
2252  * the struct.
2253  */
2254 void
2255 ip6_initpktopts(struct ip6_pktopts *opt)
2256 {
2257
2258         bzero(opt, sizeof(*opt));
2259         opt->ip6po_hlim = -1;   /* -1 means default hop limit */
2260         opt->ip6po_tclass = -1; /* -1 means default traffic class */
2261         opt->ip6po_minmtu = IP6PO_MINMTU_MCASTONLY;
2262         opt->ip6po_prefer_tempaddr = IP6PO_TEMPADDR_SYSTEM;
2263 }
2264
2265 static int
2266 ip6_pcbopt(int optname, u_char *buf, int len, struct ip6_pktopts **pktopt,
2267     struct ucred *cred, int uproto)
2268 {
2269         struct ip6_pktopts *opt;
2270
2271         if (*pktopt == NULL) {
2272                 *pktopt = malloc(sizeof(struct ip6_pktopts), M_IP6OPT,
2273                     M_WAITOK);
2274                 ip6_initpktopts(*pktopt);
2275         }
2276         opt = *pktopt;
2277
2278         return (ip6_setpktopt(optname, buf, len, opt, cred, 1, 0, uproto));
2279 }
2280
2281 static int
2282 ip6_getpcbopt(struct ip6_pktopts *pktopt, int optname, struct sockopt *sopt)
2283 {
2284         void *optdata = NULL;
2285         int optdatalen = 0;
2286         struct ip6_ext *ip6e;
2287         int error = 0;
2288         struct in6_pktinfo null_pktinfo;
2289         int deftclass = 0, on;
2290         int defminmtu = IP6PO_MINMTU_MCASTONLY;
2291         int defpreftemp = IP6PO_TEMPADDR_SYSTEM;
2292
2293         switch (optname) {
2294         case IPV6_PKTINFO:
2295                 optdata = (void *)&null_pktinfo;
2296                 if (pktopt && pktopt->ip6po_pktinfo) {
2297                         bcopy(pktopt->ip6po_pktinfo, &null_pktinfo,
2298                             sizeof(null_pktinfo));
2299                         in6_clearscope(&null_pktinfo.ipi6_addr);
2300                 } else {
2301                         /* XXX: we don't have to do this every time... */
2302                         bzero(&null_pktinfo, sizeof(null_pktinfo));
2303                 }
2304                 optdatalen = sizeof(struct in6_pktinfo);
2305                 break;
2306         case IPV6_TCLASS:
2307                 if (pktopt && pktopt->ip6po_tclass >= 0)
2308                         optdata = (void *)&pktopt->ip6po_tclass;
2309                 else
2310                         optdata = (void *)&deftclass;
2311                 optdatalen = sizeof(int);
2312                 break;
2313         case IPV6_HOPOPTS:
2314                 if (pktopt && pktopt->ip6po_hbh) {
2315                         optdata = (void *)pktopt->ip6po_hbh;
2316                         ip6e = (struct ip6_ext *)pktopt->ip6po_hbh;
2317                         optdatalen = (ip6e->ip6e_len + 1) << 3;
2318                 }
2319                 break;
2320         case IPV6_RTHDR:
2321                 if (pktopt && pktopt->ip6po_rthdr) {
2322                         optdata = (void *)pktopt->ip6po_rthdr;
2323                         ip6e = (struct ip6_ext *)pktopt->ip6po_rthdr;
2324                         optdatalen = (ip6e->ip6e_len + 1) << 3;
2325                 }
2326                 break;
2327         case IPV6_RTHDRDSTOPTS:
2328                 if (pktopt && pktopt->ip6po_dest1) {
2329                         optdata = (void *)pktopt->ip6po_dest1;
2330                         ip6e = (struct ip6_ext *)pktopt->ip6po_dest1;
2331                         optdatalen = (ip6e->ip6e_len + 1) << 3;
2332                 }
2333                 break;
2334         case IPV6_DSTOPTS:
2335                 if (pktopt && pktopt->ip6po_dest2) {
2336                         optdata = (void *)pktopt->ip6po_dest2;
2337                         ip6e = (struct ip6_ext *)pktopt->ip6po_dest2;
2338                         optdatalen = (ip6e->ip6e_len + 1) << 3;
2339                 }
2340                 break;
2341         case IPV6_NEXTHOP:
2342                 if (pktopt && pktopt->ip6po_nexthop) {
2343                         optdata = (void *)pktopt->ip6po_nexthop;
2344                         optdatalen = pktopt->ip6po_nexthop->sa_len;
2345                 }
2346                 break;
2347         case IPV6_USE_MIN_MTU:
2348                 if (pktopt)
2349                         optdata = (void *)&pktopt->ip6po_minmtu;
2350                 else
2351                         optdata = (void *)&defminmtu;
2352                 optdatalen = sizeof(int);
2353                 break;
2354         case IPV6_DONTFRAG:
2355                 if (pktopt && ((pktopt->ip6po_flags) & IP6PO_DONTFRAG))
2356                         on = 1;
2357                 else
2358                         on = 0;
2359                 optdata = (void *)&on;
2360                 optdatalen = sizeof(on);
2361                 break;
2362         case IPV6_PREFER_TEMPADDR:
2363                 if (pktopt)
2364                         optdata = (void *)&pktopt->ip6po_prefer_tempaddr;
2365                 else
2366                         optdata = (void *)&defpreftemp;
2367                 optdatalen = sizeof(int);
2368                 break;
2369         default:                /* should not happen */
2370 #ifdef DIAGNOSTIC
2371                 panic("ip6_getpcbopt: unexpected option\n");
2372 #endif
2373                 return (ENOPROTOOPT);
2374         }
2375
2376         error = sooptcopyout(sopt, optdata, optdatalen);
2377
2378         return (error);
2379 }
2380
2381 void
2382 ip6_clearpktopts(struct ip6_pktopts *pktopt, int optname)
2383 {
2384         if (pktopt == NULL)
2385                 return;
2386
2387         if (optname == -1 || optname == IPV6_PKTINFO) {
2388                 if (pktopt->ip6po_pktinfo)
2389                         free(pktopt->ip6po_pktinfo, M_IP6OPT);
2390                 pktopt->ip6po_pktinfo = NULL;
2391         }
2392         if (optname == -1 || optname == IPV6_HOPLIMIT)
2393                 pktopt->ip6po_hlim = -1;
2394         if (optname == -1 || optname == IPV6_TCLASS)
2395                 pktopt->ip6po_tclass = -1;
2396         if (optname == -1 || optname == IPV6_NEXTHOP) {
2397                 if (pktopt->ip6po_nextroute.ro_rt) {
2398                         RTFREE(pktopt->ip6po_nextroute.ro_rt);
2399                         pktopt->ip6po_nextroute.ro_rt = NULL;
2400                 }
2401                 if (pktopt->ip6po_nexthop)
2402                         free(pktopt->ip6po_nexthop, M_IP6OPT);
2403                 pktopt->ip6po_nexthop = NULL;
2404         }
2405         if (optname == -1 || optname == IPV6_HOPOPTS) {
2406                 if (pktopt->ip6po_hbh)
2407                         free(pktopt->ip6po_hbh, M_IP6OPT);
2408                 pktopt->ip6po_hbh = NULL;
2409         }
2410         if (optname == -1 || optname == IPV6_RTHDRDSTOPTS) {
2411                 if (pktopt->ip6po_dest1)
2412                         free(pktopt->ip6po_dest1, M_IP6OPT);
2413                 pktopt->ip6po_dest1 = NULL;
2414         }
2415         if (optname == -1 || optname == IPV6_RTHDR) {
2416                 if (pktopt->ip6po_rhinfo.ip6po_rhi_rthdr)
2417                         free(pktopt->ip6po_rhinfo.ip6po_rhi_rthdr, M_IP6OPT);
2418                 pktopt->ip6po_rhinfo.ip6po_rhi_rthdr = NULL;
2419                 if (pktopt->ip6po_route.ro_rt) {
2420                         RTFREE(pktopt->ip6po_route.ro_rt);
2421                         pktopt->ip6po_route.ro_rt = NULL;
2422                 }
2423         }
2424         if (optname == -1 || optname == IPV6_DSTOPTS) {
2425                 if (pktopt->ip6po_dest2)
2426                         free(pktopt->ip6po_dest2, M_IP6OPT);
2427                 pktopt->ip6po_dest2 = NULL;
2428         }
2429 }
2430
2431 #define PKTOPT_EXTHDRCPY(type) \
2432 do {\
2433         if (src->type) {\
2434                 int hlen = (((struct ip6_ext *)src->type)->ip6e_len + 1) << 3;\
2435                 dst->type = malloc(hlen, M_IP6OPT, canwait);\
2436                 if (dst->type == NULL && canwait == M_NOWAIT)\
2437                         goto bad;\
2438                 bcopy(src->type, dst->type, hlen);\
2439         }\
2440 } while (/*CONSTCOND*/ 0)
2441
2442 static int
2443 copypktopts(struct ip6_pktopts *dst, struct ip6_pktopts *src, int canwait)
2444 {
2445         if (dst == NULL || src == NULL)  {
2446                 printf("ip6_clearpktopts: invalid argument\n");
2447                 return (EINVAL);
2448         }
2449
2450         dst->ip6po_hlim = src->ip6po_hlim;
2451         dst->ip6po_tclass = src->ip6po_tclass;
2452         dst->ip6po_flags = src->ip6po_flags;
2453         dst->ip6po_minmtu = src->ip6po_minmtu;
2454         dst->ip6po_prefer_tempaddr = src->ip6po_prefer_tempaddr;
2455         if (src->ip6po_pktinfo) {
2456                 dst->ip6po_pktinfo = malloc(sizeof(*dst->ip6po_pktinfo),
2457                     M_IP6OPT, canwait);
2458                 if (dst->ip6po_pktinfo == NULL)
2459                         goto bad;
2460                 *dst->ip6po_pktinfo = *src->ip6po_pktinfo;
2461         }
2462         if (src->ip6po_nexthop) {
2463                 dst->ip6po_nexthop = malloc(src->ip6po_nexthop->sa_len,
2464                     M_IP6OPT, canwait);
2465                 if (dst->ip6po_nexthop == NULL)
2466                         goto bad;
2467                 bcopy(src->ip6po_nexthop, dst->ip6po_nexthop,
2468                     src->ip6po_nexthop->sa_len);
2469         }
2470         PKTOPT_EXTHDRCPY(ip6po_hbh);
2471         PKTOPT_EXTHDRCPY(ip6po_dest1);
2472         PKTOPT_EXTHDRCPY(ip6po_dest2);
2473         PKTOPT_EXTHDRCPY(ip6po_rthdr); /* not copy the cached route */
2474         return (0);
2475
2476   bad:
2477         ip6_clearpktopts(dst, -1);
2478         return (ENOBUFS);
2479 }
2480 #undef PKTOPT_EXTHDRCPY
2481
2482 struct ip6_pktopts *
2483 ip6_copypktopts(struct ip6_pktopts *src, int canwait)
2484 {
2485         int error;
2486         struct ip6_pktopts *dst;
2487
2488         dst = malloc(sizeof(*dst), M_IP6OPT, canwait);
2489         if (dst == NULL)
2490                 return (NULL);
2491         ip6_initpktopts(dst);
2492
2493         if ((error = copypktopts(dst, src, canwait)) != 0) {
2494                 free(dst, M_IP6OPT);
2495                 return (NULL);
2496         }
2497
2498         return (dst);
2499 }
2500
2501 void
2502 ip6_freepcbopts(struct ip6_pktopts *pktopt)
2503 {
2504         if (pktopt == NULL)
2505                 return;
2506
2507         ip6_clearpktopts(pktopt, -1);
2508
2509         free(pktopt, M_IP6OPT);
2510 }
2511
2512 /*
2513  * Set IPv6 outgoing packet options based on advanced API.
2514  */
2515 int
2516 ip6_setpktopts(struct mbuf *control, struct ip6_pktopts *opt,
2517     struct ip6_pktopts *stickyopt, struct ucred *cred, int uproto)
2518 {
2519         struct cmsghdr *cm = NULL;
2520
2521         if (control == NULL || opt == NULL)
2522                 return (EINVAL);
2523
2524         ip6_initpktopts(opt);
2525         if (stickyopt) {
2526                 int error;
2527
2528                 /*
2529                  * If stickyopt is provided, make a local copy of the options
2530                  * for this particular packet, then override them by ancillary
2531                  * objects.
2532                  * XXX: copypktopts() does not copy the cached route to a next
2533                  * hop (if any).  This is not very good in terms of efficiency,
2534                  * but we can allow this since this option should be rarely
2535                  * used.
2536                  */
2537                 if ((error = copypktopts(opt, stickyopt, M_NOWAIT)) != 0)
2538                         return (error);
2539         }
2540
2541         /*
2542          * XXX: Currently, we assume all the optional information is stored
2543          * in a single mbuf.
2544          */
2545         if (control->m_next)
2546                 return (EINVAL);
2547
2548         for (; control->m_len > 0; control->m_data += CMSG_ALIGN(cm->cmsg_len),
2549             control->m_len -= CMSG_ALIGN(cm->cmsg_len)) {
2550                 int error;
2551
2552                 if (control->m_len < CMSG_LEN(0))
2553                         return (EINVAL);
2554
2555                 cm = mtod(control, struct cmsghdr *);
2556                 if (cm->cmsg_len == 0 || cm->cmsg_len > control->m_len)
2557                         return (EINVAL);
2558                 if (cm->cmsg_level != IPPROTO_IPV6)
2559                         continue;
2560
2561                 error = ip6_setpktopt(cm->cmsg_type, CMSG_DATA(cm),
2562                     cm->cmsg_len - CMSG_LEN(0), opt, cred, 0, 1, uproto);
2563                 if (error)
2564                         return (error);
2565         }
2566
2567         return (0);
2568 }
2569
2570 /*
2571  * Set a particular packet option, as a sticky option or an ancillary data
2572  * item.  "len" can be 0 only when it's a sticky option.
2573  * We have 4 cases of combination of "sticky" and "cmsg":
2574  * "sticky=0, cmsg=0": impossible
2575  * "sticky=0, cmsg=1": RFC2292 or RFC3542 ancillary data
2576  * "sticky=1, cmsg=0": RFC3542 socket option
2577  * "sticky=1, cmsg=1": RFC2292 socket option
2578  */
2579 static int
2580 ip6_setpktopt(int optname, u_char *buf, int len, struct ip6_pktopts *opt,
2581     struct ucred *cred, int sticky, int cmsg, int uproto)
2582 {
2583         int minmtupolicy, preftemp;
2584         int error;
2585
2586         if (!sticky && !cmsg) {
2587 #ifdef DIAGNOSTIC
2588                 printf("ip6_setpktopt: impossible case\n");
2589 #endif
2590                 return (EINVAL);
2591         }
2592
2593         /*
2594          * IPV6_2292xxx is for backward compatibility to RFC2292, and should
2595          * not be specified in the context of RFC3542.  Conversely,
2596          * RFC3542 types should not be specified in the context of RFC2292.
2597          */
2598         if (!cmsg) {
2599                 switch (optname) {
2600                 case IPV6_2292PKTINFO:
2601                 case IPV6_2292HOPLIMIT:
2602                 case IPV6_2292NEXTHOP:
2603                 case IPV6_2292HOPOPTS:
2604                 case IPV6_2292DSTOPTS:
2605                 case IPV6_2292RTHDR:
2606                 case IPV6_2292PKTOPTIONS:
2607                         return (ENOPROTOOPT);
2608                 }
2609         }
2610         if (sticky && cmsg) {
2611                 switch (optname) {
2612                 case IPV6_PKTINFO:
2613                 case IPV6_HOPLIMIT:
2614                 case IPV6_NEXTHOP:
2615                 case IPV6_HOPOPTS:
2616                 case IPV6_DSTOPTS:
2617                 case IPV6_RTHDRDSTOPTS:
2618                 case IPV6_RTHDR:
2619                 case IPV6_USE_MIN_MTU:
2620                 case IPV6_DONTFRAG:
2621                 case IPV6_TCLASS:
2622                 case IPV6_PREFER_TEMPADDR: /* XXX: not an RFC3542 option */
2623                         return (ENOPROTOOPT);
2624                 }
2625         }
2626
2627         switch (optname) {
2628         case IPV6_2292PKTINFO:
2629         case IPV6_PKTINFO:
2630         {
2631                 struct ifnet *ifp = NULL;
2632                 struct in6_pktinfo *pktinfo;
2633
2634                 if (len != sizeof(struct in6_pktinfo))
2635                         return (EINVAL);
2636
2637                 pktinfo = (struct in6_pktinfo *)buf;
2638
2639                 /*
2640                  * An application can clear any sticky IPV6_PKTINFO option by
2641                  * doing a "regular" setsockopt with ipi6_addr being
2642                  * in6addr_any and ipi6_ifindex being zero.
2643                  * [RFC 3542, Section 6]
2644                  */
2645                 if (optname == IPV6_PKTINFO && opt->ip6po_pktinfo &&
2646                     pktinfo->ipi6_ifindex == 0 &&
2647                     IN6_IS_ADDR_UNSPECIFIED(&pktinfo->ipi6_addr)) {
2648                         ip6_clearpktopts(opt, optname);
2649                         break;
2650                 }
2651
2652                 if (uproto == IPPROTO_TCP && optname == IPV6_PKTINFO &&
2653                     sticky && !IN6_IS_ADDR_UNSPECIFIED(&pktinfo->ipi6_addr)) {
2654                         return (EINVAL);
2655                 }
2656                 if (IN6_IS_ADDR_MULTICAST(&pktinfo->ipi6_addr))
2657                         return (EINVAL);
2658                 /* validate the interface index if specified. */
2659                 if (pktinfo->ipi6_ifindex > V_if_index)
2660                          return (ENXIO);
2661                 if (pktinfo->ipi6_ifindex) {
2662                         ifp = ifnet_byindex(pktinfo->ipi6_ifindex);
2663                         if (ifp == NULL)
2664                                 return (ENXIO);
2665                 }
2666                 if (ifp != NULL && (
2667                     ND_IFINFO(ifp)->flags & ND6_IFF_IFDISABLED))
2668                         return (ENETDOWN);
2669
2670                 if (ifp != NULL &&
2671                     !IN6_IS_ADDR_UNSPECIFIED(&pktinfo->ipi6_addr)) {
2672                         struct in6_ifaddr *ia;
2673
2674                         in6_setscope(&pktinfo->ipi6_addr, ifp, NULL);
2675                         ia = in6ifa_ifpwithaddr(ifp, &pktinfo->ipi6_addr);
2676                         if (ia == NULL)
2677                                 return (EADDRNOTAVAIL);
2678                         ifa_free(&ia->ia_ifa);
2679                 }
2680                 /*
2681                  * We store the address anyway, and let in6_selectsrc()
2682                  * validate the specified address.  This is because ipi6_addr
2683                  * may not have enough information about its scope zone, and
2684                  * we may need additional information (such as outgoing
2685                  * interface or the scope zone of a destination address) to
2686                  * disambiguate the scope.
2687                  * XXX: the delay of the validation may confuse the
2688                  * application when it is used as a sticky option.
2689                  */
2690                 if (opt->ip6po_pktinfo == NULL) {
2691                         opt->ip6po_pktinfo = malloc(sizeof(*pktinfo),
2692                             M_IP6OPT, M_NOWAIT);
2693                         if (opt->ip6po_pktinfo == NULL)
2694                                 return (ENOBUFS);
2695                 }
2696                 bcopy(pktinfo, opt->ip6po_pktinfo, sizeof(*pktinfo));
2697                 break;
2698         }
2699
2700         case IPV6_2292HOPLIMIT:
2701         case IPV6_HOPLIMIT:
2702         {
2703                 int *hlimp;
2704
2705                 /*
2706                  * RFC 3542 deprecated the usage of sticky IPV6_HOPLIMIT
2707                  * to simplify the ordering among hoplimit options.
2708                  */
2709                 if (optname == IPV6_HOPLIMIT && sticky)
2710                         return (ENOPROTOOPT);
2711
2712                 if (len != sizeof(int))
2713                         return (EINVAL);
2714                 hlimp = (int *)buf;
2715                 if (*hlimp < -1 || *hlimp > 255)
2716                         return (EINVAL);
2717
2718                 opt->ip6po_hlim = *hlimp;
2719                 break;
2720         }
2721
2722         case IPV6_TCLASS:
2723         {
2724                 int tclass;
2725
2726                 if (len != sizeof(int))
2727                         return (EINVAL);
2728                 tclass = *(int *)buf;
2729                 if (tclass < -1 || tclass > 255)
2730                         return (EINVAL);
2731
2732                 opt->ip6po_tclass = tclass;
2733                 break;
2734         }
2735
2736         case IPV6_2292NEXTHOP:
2737         case IPV6_NEXTHOP:
2738                 if (cred != NULL) {
2739                         error = priv_check_cred(cred,
2740                             PRIV_NETINET_SETHDROPTS, 0);
2741                         if (error)
2742                                 return (error);
2743                 }
2744
2745                 if (len == 0) { /* just remove the option */
2746                         ip6_clearpktopts(opt, IPV6_NEXTHOP);
2747                         break;
2748                 }
2749
2750                 /* check if cmsg_len is large enough for sa_len */
2751                 if (len < sizeof(struct sockaddr) || len < *buf)
2752                         return (EINVAL);
2753
2754                 switch (((struct sockaddr *)buf)->sa_family) {
2755                 case AF_INET6:
2756                 {
2757                         struct sockaddr_in6 *sa6 = (struct sockaddr_in6 *)buf;
2758                         int error;
2759
2760                         if (sa6->sin6_len != sizeof(struct sockaddr_in6))
2761                                 return (EINVAL);
2762
2763                         if (IN6_IS_ADDR_UNSPECIFIED(&sa6->sin6_addr) ||
2764                             IN6_IS_ADDR_MULTICAST(&sa6->sin6_addr)) {
2765                                 return (EINVAL);
2766                         }
2767                         if ((error = sa6_embedscope(sa6, V_ip6_use_defzone))
2768                             != 0) {
2769                                 return (error);
2770                         }
2771                         break;
2772                 }
2773                 case AF_LINK:   /* should eventually be supported */
2774                 default:
2775                         return (EAFNOSUPPORT);
2776                 }
2777
2778                 /* turn off the previous option, then set the new option. */
2779                 ip6_clearpktopts(opt, IPV6_NEXTHOP);
2780                 opt->ip6po_nexthop = malloc(*buf, M_IP6OPT, M_NOWAIT);
2781                 if (opt->ip6po_nexthop == NULL)
2782                         return (ENOBUFS);
2783                 bcopy(buf, opt->ip6po_nexthop, *buf);
2784                 break;
2785
2786         case IPV6_2292HOPOPTS:
2787         case IPV6_HOPOPTS:
2788         {
2789                 struct ip6_hbh *hbh;
2790                 int hbhlen;
2791
2792                 /*
2793                  * XXX: We don't allow a non-privileged user to set ANY HbH
2794                  * options, since per-option restriction has too much
2795                  * overhead.
2796                  */
2797                 if (cred != NULL) {
2798                         error = priv_check_cred(cred,
2799                             PRIV_NETINET_SETHDROPTS, 0);
2800                         if (error)
2801                                 return (error);
2802                 }
2803
2804                 if (len == 0) {
2805                         ip6_clearpktopts(opt, IPV6_HOPOPTS);
2806                         break;  /* just remove the option */
2807                 }
2808
2809                 /* message length validation */
2810                 if (len < sizeof(struct ip6_hbh))
2811                         return (EINVAL);
2812                 hbh = (struct ip6_hbh *)buf;
2813                 hbhlen = (hbh->ip6h_len + 1) << 3;
2814                 if (len != hbhlen)
2815                         return (EINVAL);
2816
2817                 /* turn off the previous option, then set the new option. */
2818                 ip6_clearpktopts(opt, IPV6_HOPOPTS);
2819                 opt->ip6po_hbh = malloc(hbhlen, M_IP6OPT, M_NOWAIT);
2820                 if (opt->ip6po_hbh == NULL)
2821                         return (ENOBUFS);
2822                 bcopy(hbh, opt->ip6po_hbh, hbhlen);
2823
2824                 break;
2825         }
2826
2827         case IPV6_2292DSTOPTS:
2828         case IPV6_DSTOPTS:
2829         case IPV6_RTHDRDSTOPTS:
2830         {
2831                 struct ip6_dest *dest, **newdest = NULL;
2832                 int destlen;
2833
2834                 if (cred != NULL) { /* XXX: see the comment for IPV6_HOPOPTS */
2835                         error = priv_check_cred(cred,
2836                             PRIV_NETINET_SETHDROPTS, 0);
2837                         if (error)
2838                                 return (error);
2839                 }
2840
2841                 if (len == 0) {
2842                         ip6_clearpktopts(opt, optname);
2843                         break;  /* just remove the option */
2844                 }
2845
2846                 /* message length validation */
2847                 if (len < sizeof(struct ip6_dest))
2848                         return (EINVAL);
2849                 dest = (struct ip6_dest *)buf;
2850                 destlen = (dest->ip6d_len + 1) << 3;
2851                 if (len != destlen)
2852                         return (EINVAL);
2853
2854                 /*
2855                  * Determine the position that the destination options header
2856                  * should be inserted; before or after the routing header.
2857                  */
2858                 switch (optname) {
2859                 case IPV6_2292DSTOPTS:
2860                         /*
2861                          * The old advacned API is ambiguous on this point.
2862                          * Our approach is to determine the position based
2863                          * according to the existence of a routing header.
2864                          * Note, however, that this depends on the order of the
2865                          * extension headers in the ancillary data; the 1st
2866                          * part of the destination options header must appear
2867                          * before the routing header in the ancillary data,
2868                          * too.
2869                          * RFC3542 solved the ambiguity by introducing
2870                          * separate ancillary data or option types.
2871                          */
2872                         if (opt->ip6po_rthdr == NULL)
2873                                 newdest = &opt->ip6po_dest1;
2874                         else
2875                                 newdest = &opt->ip6po_dest2;
2876                         break;
2877                 case IPV6_RTHDRDSTOPTS:
2878                         newdest = &opt->ip6po_dest1;
2879                         break;
2880                 case IPV6_DSTOPTS:
2881                         newdest = &opt->ip6po_dest2;
2882                         break;
2883                 }
2884
2885                 /* turn off the previous option, then set the new option. */
2886                 ip6_clearpktopts(opt, optname);
2887                 *newdest = malloc(destlen, M_IP6OPT, M_NOWAIT);
2888                 if (*newdest == NULL)
2889                         return (ENOBUFS);
2890                 bcopy(dest, *newdest, destlen);
2891
2892                 break;
2893         }
2894
2895         case IPV6_2292RTHDR:
2896         case IPV6_RTHDR:
2897         {
2898                 struct ip6_rthdr *rth;
2899                 int rthlen;
2900
2901                 if (len == 0) {
2902                         ip6_clearpktopts(opt, IPV6_RTHDR);
2903                         break;  /* just remove the option */
2904                 }
2905
2906                 /* message length validation */
2907                 if (len < sizeof(struct ip6_rthdr))
2908                         return (EINVAL);
2909                 rth = (struct ip6_rthdr *)buf;
2910                 rthlen = (rth->ip6r_len + 1) << 3;
2911                 if (len != rthlen)
2912                         return (EINVAL);
2913
2914                 switch (rth->ip6r_type) {
2915                 case IPV6_RTHDR_TYPE_0:
2916                         if (rth->ip6r_len == 0) /* must contain one addr */
2917                                 return (EINVAL);
2918                         if (rth->ip6r_len % 2) /* length must be even */
2919                                 return (EINVAL);
2920                         if (rth->ip6r_len / 2 != rth->ip6r_segleft)
2921                                 return (EINVAL);
2922                         break;
2923                 default:
2924                         return (EINVAL);        /* not supported */
2925                 }
2926
2927                 /* turn off the previous option */
2928                 ip6_clearpktopts(opt, IPV6_RTHDR);
2929                 opt->ip6po_rthdr = malloc(rthlen, M_IP6OPT, M_NOWAIT);
2930                 if (opt->ip6po_rthdr == NULL)
2931                         return (ENOBUFS);
2932                 bcopy(rth, opt->ip6po_rthdr, rthlen);
2933
2934                 break;
2935         }
2936
2937         case IPV6_USE_MIN_MTU:
2938                 if (len != sizeof(int))
2939                         return (EINVAL);
2940                 minmtupolicy = *(int *)buf;
2941                 if (minmtupolicy != IP6PO_MINMTU_MCASTONLY &&
2942                     minmtupolicy != IP6PO_MINMTU_DISABLE &&
2943                     minmtupolicy != IP6PO_MINMTU_ALL) {
2944                         return (EINVAL);
2945                 }
2946                 opt->ip6po_minmtu = minmtupolicy;
2947                 break;
2948
2949         case IPV6_DONTFRAG:
2950                 if (len != sizeof(int))
2951                         return (EINVAL);
2952
2953                 if (uproto == IPPROTO_TCP || *(int *)buf == 0) {
2954                         /*
2955                          * we ignore this option for TCP sockets.
2956                          * (RFC3542 leaves this case unspecified.)
2957                          */
2958                         opt->ip6po_flags &= ~IP6PO_DONTFRAG;
2959                 } else
2960                         opt->ip6po_flags |= IP6PO_DONTFRAG;
2961                 break;
2962
2963         case IPV6_PREFER_TEMPADDR:
2964                 if (len != sizeof(int))
2965                         return (EINVAL);
2966                 preftemp = *(int *)buf;
2967                 if (preftemp != IP6PO_TEMPADDR_SYSTEM &&
2968                     preftemp != IP6PO_TEMPADDR_NOTPREFER &&
2969                     preftemp != IP6PO_TEMPADDR_PREFER) {
2970                         return (EINVAL);
2971                 }
2972                 opt->ip6po_prefer_tempaddr = preftemp;
2973                 break;
2974
2975         default:
2976                 return (ENOPROTOOPT);
2977         } /* end of switch */
2978
2979         return (0);
2980 }
2981
2982 /*
2983  * Routine called from ip6_output() to loop back a copy of an IP6 multicast
2984  * packet to the input queue of a specified interface.  Note that this
2985  * calls the output routine of the loopback "driver", but with an interface
2986  * pointer that might NOT be &loif -- easier than replicating that code here.
2987  */
2988 void
2989 ip6_mloopback(struct ifnet *ifp, struct mbuf *m)
2990 {
2991         struct mbuf *copym;
2992         struct ip6_hdr *ip6;
2993
2994         copym = m_copy(m, 0, M_COPYALL);
2995         if (copym == NULL)
2996                 return;
2997
2998         /*
2999          * Make sure to deep-copy IPv6 header portion in case the data
3000          * is in an mbuf cluster, so that we can safely override the IPv6
3001          * header portion later.
3002          */
3003         if (!M_WRITABLE(copym) ||
3004             copym->m_len < sizeof(struct ip6_hdr)) {
3005                 copym = m_pullup(copym, sizeof(struct ip6_hdr));
3006                 if (copym == NULL)
3007                         return;
3008         }
3009         ip6 = mtod(copym, struct ip6_hdr *);
3010         /*
3011          * clear embedded scope identifiers if necessary.
3012          * in6_clearscope will touch the addresses only when necessary.
3013          */
3014         in6_clearscope(&ip6->ip6_src);
3015         in6_clearscope(&ip6->ip6_dst);
3016         if (copym->m_pkthdr.csum_flags & CSUM_DELAY_DATA_IPV6) {
3017                 copym->m_pkthdr.csum_flags |= CSUM_DATA_VALID_IPV6 |
3018                     CSUM_PSEUDO_HDR;
3019                 copym->m_pkthdr.csum_data = 0xffff;
3020         }
3021         if_simloop(ifp, copym, AF_INET6, 0);
3022 }
3023
3024 /*
3025  * Chop IPv6 header off from the payload.
3026  */
3027 static int
3028 ip6_splithdr(struct mbuf *m, struct ip6_exthdrs *exthdrs)
3029 {
3030         struct mbuf *mh;
3031         struct ip6_hdr *ip6;
3032
3033         ip6 = mtod(m, struct ip6_hdr *);
3034         if (m->m_len > sizeof(*ip6)) {
3035                 mh = m_gethdr(M_NOWAIT, MT_DATA);
3036                 if (mh == NULL) {
3037                         m_freem(m);
3038                         return ENOBUFS;
3039                 }
3040                 m_move_pkthdr(mh, m);
3041                 M_ALIGN(mh, sizeof(*ip6));
3042                 m->m_len -= sizeof(*ip6);
3043                 m->m_data += sizeof(*ip6);
3044                 mh->m_next = m;
3045                 m = mh;
3046                 m->m_len = sizeof(*ip6);
3047                 bcopy((caddr_t)ip6, mtod(m, caddr_t), sizeof(*ip6));
3048         }
3049         exthdrs->ip6e_ip6 = m;
3050         return 0;
3051 }
3052
3053 /*
3054  * Compute IPv6 extension header length.
3055  */
3056 int
3057 ip6_optlen(struct inpcb *in6p)
3058 {
3059         int len;
3060
3061         if (!in6p->in6p_outputopts)
3062                 return 0;
3063
3064         len = 0;
3065 #define elen(x) \
3066     (((struct ip6_ext *)(x)) ? (((struct ip6_ext *)(x))->ip6e_len + 1) << 3 : 0)
3067
3068         len += elen(in6p->in6p_outputopts->ip6po_hbh);
3069         if (in6p->in6p_outputopts->ip6po_rthdr)
3070                 /* dest1 is valid with rthdr only */
3071                 len += elen(in6p->in6p_outputopts->ip6po_dest1);
3072         len += elen(in6p->in6p_outputopts->ip6po_rthdr);
3073         len += elen(in6p->in6p_outputopts->ip6po_dest2);
3074         return len;
3075 #undef elen
3076 }