]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/netinet6/ip6_output.c
MFC r342884:
[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_ipsec.h"
69 #include "opt_sctp.h"
70 #include "opt_route.h"
71 #include "opt_rss.h"
72
73 #include <sys/param.h>
74 #include <sys/kernel.h>
75 #include <sys/malloc.h>
76 #include <sys/mbuf.h>
77 #include <sys/errno.h>
78 #include <sys/priv.h>
79 #include <sys/proc.h>
80 #include <sys/protosw.h>
81 #include <sys/socket.h>
82 #include <sys/socketvar.h>
83 #include <sys/syslog.h>
84 #include <sys/ucred.h>
85
86 #include <machine/in_cksum.h>
87
88 #include <net/if.h>
89 #include <net/if_var.h>
90 #include <net/if_llatbl.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 #include <netipsec/ipsec_support.h>
111 #ifdef SCTP
112 #include <netinet/sctp.h>
113 #include <netinet/sctp_crc32.h>
114 #endif
115
116 #include <netinet6/ip6protosw.h>
117 #include <netinet6/scope6_var.h>
118
119 #ifdef FLOWTABLE
120 #include <net/flowtable.h>
121 #endif
122
123 extern int in6_mcast_loop;
124
125 struct ip6_exthdrs {
126         struct mbuf *ip6e_ip6;
127         struct mbuf *ip6e_hbh;
128         struct mbuf *ip6e_dest1;
129         struct mbuf *ip6e_rthdr;
130         struct mbuf *ip6e_dest2;
131 };
132
133 static MALLOC_DEFINE(M_IP6OPT, "ip6opt", "IPv6 options");
134
135 static int ip6_pcbopt(int, u_char *, int, struct ip6_pktopts **,
136                            struct ucred *, int);
137 static int ip6_pcbopts(struct ip6_pktopts **, struct mbuf *,
138         struct socket *, struct sockopt *);
139 static int ip6_getpcbopt(struct ip6_pktopts *, int, struct sockopt *);
140 static int ip6_setpktopt(int, u_char *, int, struct ip6_pktopts *,
141         struct ucred *, int, int, int);
142
143 static int ip6_copyexthdr(struct mbuf **, caddr_t, int);
144 static int ip6_insertfraghdr(struct mbuf *, struct mbuf *, int,
145         struct ip6_frag **);
146 static int ip6_insert_jumboopt(struct ip6_exthdrs *, u_int32_t);
147 static int ip6_splithdr(struct mbuf *, struct ip6_exthdrs *);
148 static int ip6_getpmtu(struct route_in6 *, int,
149         struct ifnet *, const struct in6_addr *, u_long *, int *, u_int,
150         u_int);
151 static int ip6_calcmtu(struct ifnet *, const struct in6_addr *, u_long,
152         u_long *, int *, u_int);
153 static int ip6_getpmtu_ctl(u_int, const struct in6_addr *, u_long *);
154 static int copypktopts(struct ip6_pktopts *, struct ip6_pktopts *, int);
155
156
157 /*
158  * Make an extension header from option data.  hp is the source, and
159  * mp is the destination.
160  */
161 #define MAKE_EXTHDR(hp, mp)                                             \
162     do {                                                                \
163         if (hp) {                                                       \
164                 struct ip6_ext *eh = (struct ip6_ext *)(hp);            \
165                 error = ip6_copyexthdr((mp), (caddr_t)(hp),             \
166                     ((eh)->ip6e_len + 1) << 3);                         \
167                 if (error)                                              \
168                         goto freehdrs;                                  \
169         }                                                               \
170     } while (/*CONSTCOND*/ 0)
171
172 /*
173  * Form a chain of extension headers.
174  * m is the extension header mbuf
175  * mp is the previous mbuf in the chain
176  * p is the next header
177  * i is the type of option.
178  */
179 #define MAKE_CHAIN(m, mp, p, i)\
180     do {\
181         if (m) {\
182                 if (!hdrsplit) \
183                         panic("assumption failed: hdr not split"); \
184                 *mtod((m), u_char *) = *(p);\
185                 *(p) = (i);\
186                 p = mtod((m), u_char *);\
187                 (m)->m_next = (mp)->m_next;\
188                 (mp)->m_next = (m);\
189                 (mp) = (m);\
190         }\
191     } while (/*CONSTCOND*/ 0)
192
193 void
194 in6_delayed_cksum(struct mbuf *m, uint32_t plen, u_short offset)
195 {
196         u_short csum;
197
198         csum = in_cksum_skip(m, offset + plen, offset);
199         if (m->m_pkthdr.csum_flags & CSUM_UDP_IPV6 && csum == 0)
200                 csum = 0xffff;
201         offset += m->m_pkthdr.csum_data;        /* checksum offset */
202
203         if (offset + sizeof(csum) > m->m_len)
204                 m_copyback(m, offset, sizeof(csum), (caddr_t)&csum);
205         else
206                 *(u_short *)mtodo(m, offset) = csum;
207 }
208
209 int
210 ip6_fragment(struct ifnet *ifp, struct mbuf *m0, int hlen, u_char nextproto,
211     int mtu, uint32_t id)
212 {
213         struct mbuf *m, **mnext, *m_frgpart;
214         struct ip6_hdr *ip6, *mhip6;
215         struct ip6_frag *ip6f;
216         int off;
217         int error;
218         int tlen = m0->m_pkthdr.len;
219
220         KASSERT(( mtu % 8 == 0), ("Fragment length must be a multiple of 8"));
221
222         m = m0;
223         ip6 = mtod(m, struct ip6_hdr *);
224         mnext = &m->m_nextpkt;
225
226         for (off = hlen; off < tlen; off += mtu) {
227                 m = m_gethdr(M_NOWAIT, MT_DATA);
228                 if (!m) {
229                         IP6STAT_INC(ip6s_odropped);
230                         return (ENOBUFS);
231                 }
232                 m->m_flags = m0->m_flags & M_COPYFLAGS;
233                 *mnext = m;
234                 mnext = &m->m_nextpkt;
235                 m->m_data += max_linkhdr;
236                 mhip6 = mtod(m, struct ip6_hdr *);
237                 *mhip6 = *ip6;
238                 m->m_len = sizeof(*mhip6);
239                 error = ip6_insertfraghdr(m0, m, hlen, &ip6f);
240                 if (error) {
241                         IP6STAT_INC(ip6s_odropped);
242                         return (error);
243                 }
244                 ip6f->ip6f_offlg = htons((u_short)((off - hlen) & ~7));
245                 if (off + mtu >= tlen)
246                         mtu = tlen - off;
247                 else
248                         ip6f->ip6f_offlg |= IP6F_MORE_FRAG;
249                 mhip6->ip6_plen = htons((u_short)(mtu + hlen +
250                     sizeof(*ip6f) - sizeof(struct ip6_hdr)));
251                 if ((m_frgpart = m_copy(m0, off, mtu)) == NULL) {
252                         IP6STAT_INC(ip6s_odropped);
253                         return (ENOBUFS);
254                 }
255                 m_cat(m, m_frgpart);
256                 m->m_pkthdr.len = mtu + hlen + sizeof(*ip6f);
257                 m->m_pkthdr.fibnum = m0->m_pkthdr.fibnum;
258                 m->m_pkthdr.rcvif = NULL;
259                 ip6f->ip6f_reserved = 0;
260                 ip6f->ip6f_ident = id;
261                 ip6f->ip6f_nxt = nextproto;
262                 IP6STAT_INC(ip6s_ofragments);
263                 in6_ifstat_inc(ifp, ifs6_out_fragcreat);
264         }
265
266         return (0);
267 }
268
269 /*
270  * IP6 output. The packet in mbuf chain m contains a skeletal IP6
271  * header (with pri, len, nxt, hlim, src, dst).
272  * This function may modify ver and hlim only.
273  * The mbuf chain containing the packet will be freed.
274  * The mbuf opt, if present, will not be freed.
275  * If route_in6 ro is present and has ro_rt initialized, route lookup would be
276  * skipped and ro->ro_rt would be used. If ro is present but ro->ro_rt is NULL,
277  * then result of route lookup is stored in ro->ro_rt.
278  *
279  * type of "mtu": rt_mtu is u_long, ifnet.ifr_mtu is int, and
280  * nd_ifinfo.linkmtu is u_int32_t.  so we use u_long to hold largest one,
281  * which is rt_mtu.
282  *
283  * ifpp - XXX: just for statistics
284  */
285 /*
286  * XXX TODO: no flowid is assigned for outbound flows?
287  */
288 int
289 ip6_output(struct mbuf *m0, struct ip6_pktopts *opt,
290     struct route_in6 *ro, int flags, struct ip6_moptions *im6o,
291     struct ifnet **ifpp, struct inpcb *inp)
292 {
293         struct ip6_hdr *ip6;
294         struct ifnet *ifp, *origifp;
295         struct mbuf *m = m0;
296         struct mbuf *mprev = NULL;
297         int hlen, tlen, len;
298         struct route_in6 ip6route;
299         struct rtentry *rt = NULL;
300         struct sockaddr_in6 *dst, src_sa, dst_sa;
301         struct in6_addr odst;
302         int error = 0;
303         struct in6_ifaddr *ia = NULL;
304         u_long mtu;
305         int alwaysfrag, dontfrag;
306         u_int32_t optlen = 0, plen = 0, unfragpartlen = 0;
307         struct ip6_exthdrs exthdrs;
308         struct in6_addr src0, dst0;
309         u_int32_t zone;
310         struct route_in6 *ro_pmtu = NULL;
311         int hdrsplit = 0;
312         int sw_csum, tso;
313         int needfiblookup;
314         uint32_t fibnum;
315         struct m_tag *fwd_tag = NULL;
316         uint32_t id;
317
318         if (inp != NULL) {
319                 INP_LOCK_ASSERT(inp);
320                 M_SETFIB(m, inp->inp_inc.inc_fibnum);
321                 if ((flags & IP_NODEFAULTFLOWID) == 0) {
322                         /* unconditionally set flowid */
323                         m->m_pkthdr.flowid = inp->inp_flowid;
324                         M_HASHTYPE_SET(m, inp->inp_flowtype);
325                 }
326         }
327
328 #if defined(IPSEC) || defined(IPSEC_SUPPORT)
329         /*
330          * IPSec checking which handles several cases.
331          * FAST IPSEC: We re-injected the packet.
332          * XXX: need scope argument.
333          */
334         if (IPSEC_ENABLED(ipv6)) {
335                 if ((error = IPSEC_OUTPUT(ipv6, m, inp)) != 0) {
336                         if (error == EINPROGRESS)
337                                 error = 0;
338                         goto done;
339                 }
340         }
341 #endif /* IPSEC */
342
343         bzero(&exthdrs, sizeof(exthdrs));
344         if (opt) {
345                 /* Hop-by-Hop options header */
346                 MAKE_EXTHDR(opt->ip6po_hbh, &exthdrs.ip6e_hbh);
347                 /* Destination options header(1st part) */
348                 if (opt->ip6po_rthdr) {
349                         /*
350                          * Destination options header(1st part)
351                          * This only makes sense with a routing header.
352                          * See Section 9.2 of RFC 3542.
353                          * Disabling this part just for MIP6 convenience is
354                          * a bad idea.  We need to think carefully about a
355                          * way to make the advanced API coexist with MIP6
356                          * options, which might automatically be inserted in
357                          * the kernel.
358                          */
359                         MAKE_EXTHDR(opt->ip6po_dest1, &exthdrs.ip6e_dest1);
360                 }
361                 /* Routing header */
362                 MAKE_EXTHDR(opt->ip6po_rthdr, &exthdrs.ip6e_rthdr);
363                 /* Destination options header(2nd part) */
364                 MAKE_EXTHDR(opt->ip6po_dest2, &exthdrs.ip6e_dest2);
365         }
366
367         /*
368          * Calculate the total length of the extension header chain.
369          * Keep the length of the unfragmentable part for fragmentation.
370          */
371         optlen = 0;
372         if (exthdrs.ip6e_hbh)
373                 optlen += exthdrs.ip6e_hbh->m_len;
374         if (exthdrs.ip6e_dest1)
375                 optlen += exthdrs.ip6e_dest1->m_len;
376         if (exthdrs.ip6e_rthdr)
377                 optlen += exthdrs.ip6e_rthdr->m_len;
378         unfragpartlen = optlen + sizeof(struct ip6_hdr);
379
380         /* NOTE: we don't add AH/ESP length here (done in ip6_ipsec_output) */
381         if (exthdrs.ip6e_dest2)
382                 optlen += exthdrs.ip6e_dest2->m_len;
383
384         /*
385          * If there is at least one extension header,
386          * separate IP6 header from the payload.
387          */
388         if (optlen && !hdrsplit) {
389                 if ((error = ip6_splithdr(m, &exthdrs)) != 0) {
390                         m = NULL;
391                         goto freehdrs;
392                 }
393                 m = exthdrs.ip6e_ip6;
394                 hdrsplit++;
395         }
396
397         ip6 = mtod(m, struct ip6_hdr *);
398
399         /* adjust mbuf packet header length */
400         m->m_pkthdr.len += optlen;
401         plen = m->m_pkthdr.len - sizeof(*ip6);
402
403         /* If this is a jumbo payload, insert a jumbo payload option. */
404         if (plen > IPV6_MAXPACKET) {
405                 if (!hdrsplit) {
406                         if ((error = ip6_splithdr(m, &exthdrs)) != 0) {
407                                 m = NULL;
408                                 goto freehdrs;
409                         }
410                         m = exthdrs.ip6e_ip6;
411                         hdrsplit++;
412                 }
413                 /* adjust pointer */
414                 ip6 = mtod(m, struct ip6_hdr *);
415                 if ((error = ip6_insert_jumboopt(&exthdrs, plen)) != 0)
416                         goto freehdrs;
417                 ip6->ip6_plen = 0;
418         } else
419                 ip6->ip6_plen = htons(plen);
420
421         /*
422          * Concatenate headers and fill in next header fields.
423          * Here we have, on "m"
424          *      IPv6 payload
425          * and we insert headers accordingly.  Finally, we should be getting:
426          *      IPv6 hbh dest1 rthdr ah* [esp* dest2 payload]
427          *
428          * during the header composing process, "m" points to IPv6 header.
429          * "mprev" points to an extension header prior to esp.
430          */
431         u_char *nexthdrp = &ip6->ip6_nxt;
432         mprev = m;
433
434         /*
435          * we treat dest2 specially.  this makes IPsec processing
436          * much easier.  the goal here is to make mprev point the
437          * mbuf prior to dest2.
438          *
439          * result: IPv6 dest2 payload
440          * m and mprev will point to IPv6 header.
441          */
442         if (exthdrs.ip6e_dest2) {
443                 if (!hdrsplit)
444                         panic("assumption failed: hdr not split");
445                 exthdrs.ip6e_dest2->m_next = m->m_next;
446                 m->m_next = exthdrs.ip6e_dest2;
447                 *mtod(exthdrs.ip6e_dest2, u_char *) = ip6->ip6_nxt;
448                 ip6->ip6_nxt = IPPROTO_DSTOPTS;
449         }
450
451         /*
452          * result: IPv6 hbh dest1 rthdr dest2 payload
453          * m will point to IPv6 header.  mprev will point to the
454          * extension header prior to dest2 (rthdr in the above case).
455          */
456         MAKE_CHAIN(exthdrs.ip6e_hbh, mprev, nexthdrp, IPPROTO_HOPOPTS);
457         MAKE_CHAIN(exthdrs.ip6e_dest1, mprev, nexthdrp,
458                    IPPROTO_DSTOPTS);
459         MAKE_CHAIN(exthdrs.ip6e_rthdr, mprev, nexthdrp,
460                    IPPROTO_ROUTING);
461
462         /*
463          * If there is a routing header, discard the packet.
464          */
465         if (exthdrs.ip6e_rthdr) {
466                  error = EINVAL;
467                  goto bad;
468         }
469
470         /* Source address validation */
471         if (IN6_IS_ADDR_UNSPECIFIED(&ip6->ip6_src) &&
472             (flags & IPV6_UNSPECSRC) == 0) {
473                 error = EOPNOTSUPP;
474                 IP6STAT_INC(ip6s_badscope);
475                 goto bad;
476         }
477         if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_src)) {
478                 error = EOPNOTSUPP;
479                 IP6STAT_INC(ip6s_badscope);
480                 goto bad;
481         }
482
483         IP6STAT_INC(ip6s_localout);
484
485         /*
486          * Route packet.
487          */
488         if (ro == NULL) {
489                 ro = &ip6route;
490                 bzero((caddr_t)ro, sizeof(*ro));
491         }
492         ro_pmtu = ro;
493         if (opt && opt->ip6po_rthdr)
494                 ro = &opt->ip6po_route;
495         dst = (struct sockaddr_in6 *)&ro->ro_dst;
496 #ifdef FLOWTABLE
497         if (ro->ro_rt == NULL)
498                 (void )flowtable_lookup(AF_INET6, m, (struct route *)ro);
499 #endif
500         fibnum = (inp != NULL) ? inp->inp_inc.inc_fibnum : M_GETFIB(m);
501 again:
502         /*
503          * if specified, try to fill in the traffic class field.
504          * do not override if a non-zero value is already set.
505          * we check the diffserv field and the ecn field separately.
506          */
507         if (opt && opt->ip6po_tclass >= 0) {
508                 int mask = 0;
509
510                 if ((ip6->ip6_flow & htonl(0xfc << 20)) == 0)
511                         mask |= 0xfc;
512                 if ((ip6->ip6_flow & htonl(0x03 << 20)) == 0)
513                         mask |= 0x03;
514                 if (mask != 0)
515                         ip6->ip6_flow |= htonl((opt->ip6po_tclass & mask) << 20);
516         }
517
518         /* fill in or override the hop limit field, if necessary. */
519         if (opt && opt->ip6po_hlim != -1)
520                 ip6->ip6_hlim = opt->ip6po_hlim & 0xff;
521         else if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst)) {
522                 if (im6o != NULL)
523                         ip6->ip6_hlim = im6o->im6o_multicast_hlim;
524                 else
525                         ip6->ip6_hlim = V_ip6_defmcasthlim;
526         }
527         /*
528          * Validate route against routing table additions;
529          * a better/more specific route might have been added.
530          * Make sure address family is set in route.
531          */
532         if (inp) {
533                 ro->ro_dst.sin6_family = AF_INET6;
534                 RT_VALIDATE((struct route *)ro, &inp->inp_rt_cookie, fibnum);
535         }
536         if (ro->ro_rt && fwd_tag == NULL && (ro->ro_rt->rt_flags & RTF_UP) &&
537             ro->ro_dst.sin6_family == AF_INET6 &&
538             IN6_ARE_ADDR_EQUAL(&ro->ro_dst.sin6_addr, &ip6->ip6_dst)) {
539                 rt = ro->ro_rt;
540                 ifp = ro->ro_rt->rt_ifp;
541         } else {
542                 if (ro->ro_lle)
543                         LLE_FREE(ro->ro_lle);   /* zeros ro_lle */
544                 ro->ro_lle = NULL;
545                 if (fwd_tag == NULL) {
546                         bzero(&dst_sa, sizeof(dst_sa));
547                         dst_sa.sin6_family = AF_INET6;
548                         dst_sa.sin6_len = sizeof(dst_sa);
549                         dst_sa.sin6_addr = ip6->ip6_dst;
550                 }
551                 error = in6_selectroute_fib(&dst_sa, opt, im6o, ro, &ifp,
552                     &rt, fibnum);
553                 if (error != 0) {
554                         if (ifp != NULL)
555                                 in6_ifstat_inc(ifp, ifs6_out_discard);
556                         goto bad;
557                 }
558         }
559         if (rt == NULL) {
560                 /*
561                  * If in6_selectroute() does not return a route entry,
562                  * dst may not have been updated.
563                  */
564                 *dst = dst_sa;  /* XXX */
565         }
566
567         /*
568          * then rt (for unicast) and ifp must be non-NULL valid values.
569          */
570         if ((flags & IPV6_FORWARDING) == 0) {
571                 /* XXX: the FORWARDING flag can be set for mrouting. */
572                 in6_ifstat_inc(ifp, ifs6_out_request);
573         }
574         if (rt != NULL) {
575                 ia = (struct in6_ifaddr *)(rt->rt_ifa);
576                 counter_u64_add(rt->rt_pksent, 1);
577         }
578
579         /* Setup data structures for scope ID checks. */
580         src0 = ip6->ip6_src;
581         bzero(&src_sa, sizeof(src_sa));
582         src_sa.sin6_family = AF_INET6;
583         src_sa.sin6_len = sizeof(src_sa);
584         src_sa.sin6_addr = ip6->ip6_src;
585
586         dst0 = ip6->ip6_dst;
587         /* re-initialize to be sure */
588         bzero(&dst_sa, sizeof(dst_sa));
589         dst_sa.sin6_family = AF_INET6;
590         dst_sa.sin6_len = sizeof(dst_sa);
591         dst_sa.sin6_addr = ip6->ip6_dst;
592
593         /* Check for valid scope ID. */
594         if (in6_setscope(&src0, ifp, &zone) == 0 &&
595             sa6_recoverscope(&src_sa) == 0 && zone == src_sa.sin6_scope_id &&
596             in6_setscope(&dst0, ifp, &zone) == 0 &&
597             sa6_recoverscope(&dst_sa) == 0 && zone == dst_sa.sin6_scope_id) {
598                 /*
599                  * The outgoing interface is in the zone of the source
600                  * and destination addresses.
601                  *
602                  * Because the loopback interface cannot receive
603                  * packets with a different scope ID than its own,
604                  * there is a trick is to pretend the outgoing packet
605                  * was received by the real network interface, by
606                  * setting "origifp" different from "ifp". This is
607                  * only allowed when "ifp" is a loopback network
608                  * interface. Refer to code in nd6_output_ifp() for
609                  * more details.
610                  */
611                 origifp = ifp;
612         
613                 /*
614                  * We should use ia_ifp to support the case of sending
615                  * packets to an address of our own.
616                  */
617                 if (ia != NULL && ia->ia_ifp)
618                         ifp = ia->ia_ifp;
619
620         } else if ((ifp->if_flags & IFF_LOOPBACK) == 0 ||
621             sa6_recoverscope(&src_sa) != 0 ||
622             sa6_recoverscope(&dst_sa) != 0 ||
623             dst_sa.sin6_scope_id == 0 ||
624             (src_sa.sin6_scope_id != 0 &&
625             src_sa.sin6_scope_id != dst_sa.sin6_scope_id) ||
626             (origifp = ifnet_byindex(dst_sa.sin6_scope_id)) == NULL) {
627                 /*
628                  * If the destination network interface is not a
629                  * loopback interface, or the destination network
630                  * address has no scope ID, or the source address has
631                  * a scope ID set which is different from the
632                  * destination address one, or there is no network
633                  * interface representing this scope ID, the address
634                  * pair is considered invalid.
635                  */
636                 IP6STAT_INC(ip6s_badscope);
637                 in6_ifstat_inc(ifp, ifs6_out_discard);
638                 if (error == 0)
639                         error = EHOSTUNREACH; /* XXX */
640                 goto bad;
641         }
642
643         /* All scope ID checks are successful. */
644
645         if (rt && !IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst)) {
646                 if (opt && opt->ip6po_nextroute.ro_rt) {
647                         /*
648                          * The nexthop is explicitly specified by the
649                          * application.  We assume the next hop is an IPv6
650                          * address.
651                          */
652                         dst = (struct sockaddr_in6 *)opt->ip6po_nexthop;
653                 }
654                 else if ((rt->rt_flags & RTF_GATEWAY))
655                         dst = (struct sockaddr_in6 *)rt->rt_gateway;
656         }
657
658         if (!IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst)) {
659                 m->m_flags &= ~(M_BCAST | M_MCAST); /* just in case */
660         } else {
661                 m->m_flags = (m->m_flags & ~M_BCAST) | M_MCAST;
662                 in6_ifstat_inc(ifp, ifs6_out_mcast);
663                 /*
664                  * Confirm that the outgoing interface supports multicast.
665                  */
666                 if (!(ifp->if_flags & IFF_MULTICAST)) {
667                         IP6STAT_INC(ip6s_noroute);
668                         in6_ifstat_inc(ifp, ifs6_out_discard);
669                         error = ENETUNREACH;
670                         goto bad;
671                 }
672                 if ((im6o == NULL && in6_mcast_loop) ||
673                     (im6o && im6o->im6o_multicast_loop)) {
674                         /*
675                          * Loop back multicast datagram if not expressly
676                          * forbidden to do so, even if we have not joined
677                          * the address; protocols will filter it later,
678                          * thus deferring a hash lookup and lock acquisition
679                          * at the expense of an m_copym().
680                          */
681                         ip6_mloopback(ifp, m);
682                 } else {
683                         /*
684                          * If we are acting as a multicast router, perform
685                          * multicast forwarding as if the packet had just
686                          * arrived on the interface to which we are about
687                          * to send.  The multicast forwarding function
688                          * recursively calls this function, using the
689                          * IPV6_FORWARDING flag to prevent infinite recursion.
690                          *
691                          * Multicasts that are looped back by ip6_mloopback(),
692                          * above, will be forwarded by the ip6_input() routine,
693                          * if necessary.
694                          */
695                         if (V_ip6_mrouter && (flags & IPV6_FORWARDING) == 0) {
696                                 /*
697                                  * XXX: ip6_mforward expects that rcvif is NULL
698                                  * when it is called from the originating path.
699                                  * However, it may not always be the case.
700                                  */
701                                 m->m_pkthdr.rcvif = NULL;
702                                 if (ip6_mforward(ip6, ifp, m) != 0) {
703                                         m_freem(m);
704                                         goto done;
705                                 }
706                         }
707                 }
708                 /*
709                  * Multicasts with a hoplimit of zero may be looped back,
710                  * above, but must not be transmitted on a network.
711                  * Also, multicasts addressed to the loopback interface
712                  * are not sent -- the above call to ip6_mloopback() will
713                  * loop back a copy if this host actually belongs to the
714                  * destination group on the loopback interface.
715                  */
716                 if (ip6->ip6_hlim == 0 || (ifp->if_flags & IFF_LOOPBACK) ||
717                     IN6_IS_ADDR_MC_INTFACELOCAL(&ip6->ip6_dst)) {
718                         m_freem(m);
719                         goto done;
720                 }
721         }
722
723         /*
724          * Fill the outgoing inteface to tell the upper layer
725          * to increment per-interface statistics.
726          */
727         if (ifpp)
728                 *ifpp = ifp;
729
730         /* Determine path MTU. */
731         if ((error = ip6_getpmtu(ro_pmtu, ro != ro_pmtu, ifp, &ip6->ip6_dst,
732                     &mtu, &alwaysfrag, fibnum, *nexthdrp)) != 0)
733                 goto bad;
734
735         /*
736          * The caller of this function may specify to use the minimum MTU
737          * in some cases.
738          * An advanced API option (IPV6_USE_MIN_MTU) can also override MTU
739          * setting.  The logic is a bit complicated; by default, unicast
740          * packets will follow path MTU while multicast packets will be sent at
741          * the minimum MTU.  If IP6PO_MINMTU_ALL is specified, all packets
742          * including unicast ones will be sent at the minimum MTU.  Multicast
743          * packets will always be sent at the minimum MTU unless
744          * IP6PO_MINMTU_DISABLE is explicitly specified.
745          * See RFC 3542 for more details.
746          */
747         if (mtu > IPV6_MMTU) {
748                 if ((flags & IPV6_MINMTU))
749                         mtu = IPV6_MMTU;
750                 else if (opt && opt->ip6po_minmtu == IP6PO_MINMTU_ALL)
751                         mtu = IPV6_MMTU;
752                 else if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst) &&
753                          (opt == NULL ||
754                           opt->ip6po_minmtu != IP6PO_MINMTU_DISABLE)) {
755                         mtu = IPV6_MMTU;
756                 }
757         }
758
759         /*
760          * clear embedded scope identifiers if necessary.
761          * in6_clearscope will touch the addresses only when necessary.
762          */
763         in6_clearscope(&ip6->ip6_src);
764         in6_clearscope(&ip6->ip6_dst);
765
766         /*
767          * If the outgoing packet contains a hop-by-hop options header,
768          * it must be examined and processed even by the source node.
769          * (RFC 2460, section 4.)
770          */
771         if (exthdrs.ip6e_hbh) {
772                 struct ip6_hbh *hbh = mtod(exthdrs.ip6e_hbh, struct ip6_hbh *);
773                 u_int32_t dummy; /* XXX unused */
774                 u_int32_t plen = 0; /* XXX: ip6_process will check the value */
775
776 #ifdef DIAGNOSTIC
777                 if ((hbh->ip6h_len + 1) << 3 > exthdrs.ip6e_hbh->m_len)
778                         panic("ip6e_hbh is not contiguous");
779 #endif
780                 /*
781                  *  XXX: if we have to send an ICMPv6 error to the sender,
782                  *       we need the M_LOOP flag since icmp6_error() expects
783                  *       the IPv6 and the hop-by-hop options header are
784                  *       contiguous unless the flag is set.
785                  */
786                 m->m_flags |= M_LOOP;
787                 m->m_pkthdr.rcvif = ifp;
788                 if (ip6_process_hopopts(m, (u_int8_t *)(hbh + 1),
789                     ((hbh->ip6h_len + 1) << 3) - sizeof(struct ip6_hbh),
790                     &dummy, &plen) < 0) {
791                         /* m was already freed at this point */
792                         error = EINVAL;/* better error? */
793                         goto done;
794                 }
795                 m->m_flags &= ~M_LOOP; /* XXX */
796                 m->m_pkthdr.rcvif = NULL;
797         }
798
799         /* Jump over all PFIL processing if hooks are not active. */
800         if (!PFIL_HOOKED(&V_inet6_pfil_hook))
801                 goto passout;
802
803         odst = ip6->ip6_dst;
804         /* Run through list of hooks for output packets. */
805         error = pfil_run_hooks(&V_inet6_pfil_hook, &m, ifp, PFIL_OUT, 0, inp);
806         if (error != 0 || m == NULL)
807                 goto done;
808         /* adjust pointer */
809         ip6 = mtod(m, struct ip6_hdr *);
810
811         needfiblookup = 0;
812         /* See if destination IP address was changed by packet filter. */
813         if (!IN6_ARE_ADDR_EQUAL(&odst, &ip6->ip6_dst)) {
814                 m->m_flags |= M_SKIP_FIREWALL;
815                 /* If destination is now ourself drop to ip6_input(). */
816                 if (in6_localip(&ip6->ip6_dst)) {
817                         m->m_flags |= M_FASTFWD_OURS;
818                         if (m->m_pkthdr.rcvif == NULL)
819                                 m->m_pkthdr.rcvif = V_loif;
820                         if (m->m_pkthdr.csum_flags & CSUM_DELAY_DATA_IPV6) {
821                                 m->m_pkthdr.csum_flags |=
822                                     CSUM_DATA_VALID_IPV6 | CSUM_PSEUDO_HDR;
823                                 m->m_pkthdr.csum_data = 0xffff;
824                         }
825 #ifdef SCTP
826                         if (m->m_pkthdr.csum_flags & CSUM_SCTP_IPV6)
827                                 m->m_pkthdr.csum_flags |= CSUM_SCTP_VALID;
828 #endif
829                         error = netisr_queue(NETISR_IPV6, m);
830                         goto done;
831                 } else {
832                         RO_RTFREE(ro);
833                         needfiblookup = 1; /* Redo the routing table lookup. */
834                         if (ro->ro_lle)
835                                 LLE_FREE(ro->ro_lle);   /* zeros ro_lle */
836                         ro->ro_lle = NULL;
837                 }
838         }
839         /* See if fib was changed by packet filter. */
840         if (fibnum != M_GETFIB(m)) {
841                 m->m_flags |= M_SKIP_FIREWALL;
842                 fibnum = M_GETFIB(m);
843                 RO_RTFREE(ro);
844                 needfiblookup = 1;
845                 if (ro->ro_lle)
846                         LLE_FREE(ro->ro_lle);   /* zeros ro_lle */
847                 ro->ro_lle = NULL;
848         }
849         if (needfiblookup)
850                 goto again;
851
852         /* See if local, if yes, send it to netisr. */
853         if (m->m_flags & M_FASTFWD_OURS) {
854                 if (m->m_pkthdr.rcvif == NULL)
855                         m->m_pkthdr.rcvif = V_loif;
856                 if (m->m_pkthdr.csum_flags & CSUM_DELAY_DATA_IPV6) {
857                         m->m_pkthdr.csum_flags |=
858                             CSUM_DATA_VALID_IPV6 | CSUM_PSEUDO_HDR;
859                         m->m_pkthdr.csum_data = 0xffff;
860                 }
861 #ifdef SCTP
862                 if (m->m_pkthdr.csum_flags & CSUM_SCTP_IPV6)
863                         m->m_pkthdr.csum_flags |= CSUM_SCTP_VALID;
864 #endif
865                 error = netisr_queue(NETISR_IPV6, m);
866                 goto done;
867         }
868         /* Or forward to some other address? */
869         if ((m->m_flags & M_IP6_NEXTHOP) &&
870             (fwd_tag = m_tag_find(m, PACKET_TAG_IPFORWARD, NULL)) != NULL) {
871                 dst = (struct sockaddr_in6 *)&ro->ro_dst;
872                 bcopy((fwd_tag+1), &dst_sa, sizeof(struct sockaddr_in6));
873                 m->m_flags |= M_SKIP_FIREWALL;
874                 m->m_flags &= ~M_IP6_NEXTHOP;
875                 m_tag_delete(m, fwd_tag);
876                 goto again;
877         }
878
879 passout:
880         /*
881          * Send the packet to the outgoing interface.
882          * If necessary, do IPv6 fragmentation before sending.
883          *
884          * the logic here is rather complex:
885          * 1: normal case (dontfrag == 0, alwaysfrag == 0)
886          * 1-a: send as is if tlen <= path mtu
887          * 1-b: fragment if tlen > path mtu
888          *
889          * 2: if user asks us not to fragment (dontfrag == 1)
890          * 2-a: send as is if tlen <= interface mtu
891          * 2-b: error if tlen > interface mtu
892          *
893          * 3: if we always need to attach fragment header (alwaysfrag == 1)
894          *      always fragment
895          *
896          * 4: if dontfrag == 1 && alwaysfrag == 1
897          *      error, as we cannot handle this conflicting request
898          */
899         sw_csum = m->m_pkthdr.csum_flags;
900         if (!hdrsplit) {
901                 tso = ((sw_csum & ifp->if_hwassist & CSUM_TSO) != 0) ? 1 : 0;
902                 sw_csum &= ~ifp->if_hwassist;
903         } else
904                 tso = 0;
905         /*
906          * If we added extension headers, we will not do TSO and calculate the
907          * checksums ourselves for now.
908          * XXX-BZ  Need a framework to know when the NIC can handle it, even
909          * with ext. hdrs.
910          */
911         if (sw_csum & CSUM_DELAY_DATA_IPV6) {
912                 sw_csum &= ~CSUM_DELAY_DATA_IPV6;
913                 in6_delayed_cksum(m, plen, sizeof(struct ip6_hdr));
914         }
915 #ifdef SCTP
916         if (sw_csum & CSUM_SCTP_IPV6) {
917                 sw_csum &= ~CSUM_SCTP_IPV6;
918                 sctp_delayed_cksum(m, sizeof(struct ip6_hdr));
919         }
920 #endif
921         m->m_pkthdr.csum_flags &= ifp->if_hwassist;
922         tlen = m->m_pkthdr.len;
923
924         if ((opt && (opt->ip6po_flags & IP6PO_DONTFRAG)) || tso)
925                 dontfrag = 1;
926         else
927                 dontfrag = 0;
928         if (dontfrag && alwaysfrag) {   /* case 4 */
929                 /* conflicting request - can't transmit */
930                 error = EMSGSIZE;
931                 goto bad;
932         }
933         if (dontfrag && tlen > IN6_LINKMTU(ifp) && !tso) {      /* case 2-b */
934                 /*
935                  * Even if the DONTFRAG option is specified, we cannot send the
936                  * packet when the data length is larger than the MTU of the
937                  * outgoing interface.
938                  * Notify the error by sending IPV6_PATHMTU ancillary data if
939                  * application wanted to know the MTU value. Also return an
940                  * error code (this is not described in the API spec).
941                  */
942                 if (inp != NULL)
943                         ip6_notify_pmtu(inp, &dst_sa, (u_int32_t)mtu);
944                 error = EMSGSIZE;
945                 goto bad;
946         }
947
948         /*
949          * transmit packet without fragmentation
950          */
951         if (dontfrag || (!alwaysfrag && tlen <= mtu)) { /* case 1-a and 2-a */
952                 struct in6_ifaddr *ia6;
953
954                 ip6 = mtod(m, struct ip6_hdr *);
955                 ia6 = in6_ifawithifp(ifp, &ip6->ip6_src);
956                 if (ia6) {
957                         /* Record statistics for this interface address. */
958                         counter_u64_add(ia6->ia_ifa.ifa_opackets, 1);
959                         counter_u64_add(ia6->ia_ifa.ifa_obytes,
960                             m->m_pkthdr.len);
961                         ifa_free(&ia6->ia_ifa);
962                 }
963                 error = nd6_output_ifp(ifp, origifp, m, dst,
964                     (struct route *)ro);
965                 goto done;
966         }
967
968         /*
969          * try to fragment the packet.  case 1-b and 3
970          */
971         if (mtu < IPV6_MMTU) {
972                 /* path MTU cannot be less than IPV6_MMTU */
973                 error = EMSGSIZE;
974                 in6_ifstat_inc(ifp, ifs6_out_fragfail);
975                 goto bad;
976         } else if (ip6->ip6_plen == 0) {
977                 /* jumbo payload cannot be fragmented */
978                 error = EMSGSIZE;
979                 in6_ifstat_inc(ifp, ifs6_out_fragfail);
980                 goto bad;
981         } else {
982                 u_char nextproto;
983
984                 /*
985                  * Too large for the destination or interface;
986                  * fragment if possible.
987                  * Must be able to put at least 8 bytes per fragment.
988                  */
989                 hlen = unfragpartlen;
990                 if (mtu > IPV6_MAXPACKET)
991                         mtu = IPV6_MAXPACKET;
992
993                 len = (mtu - hlen - sizeof(struct ip6_frag)) & ~7;
994                 if (len < 8) {
995                         error = EMSGSIZE;
996                         in6_ifstat_inc(ifp, ifs6_out_fragfail);
997                         goto bad;
998                 }
999
1000                 /*
1001                  * If the interface will not calculate checksums on
1002                  * fragmented packets, then do it here.
1003                  * XXX-BZ handle the hw offloading case.  Need flags.
1004                  */
1005                 if (m->m_pkthdr.csum_flags & CSUM_DELAY_DATA_IPV6) {
1006                         in6_delayed_cksum(m, plen, hlen);
1007                         m->m_pkthdr.csum_flags &= ~CSUM_DELAY_DATA_IPV6;
1008                 }
1009 #ifdef SCTP
1010                 if (m->m_pkthdr.csum_flags & CSUM_SCTP_IPV6) {
1011                         sctp_delayed_cksum(m, hlen);
1012                         m->m_pkthdr.csum_flags &= ~CSUM_SCTP_IPV6;
1013                 }
1014 #endif
1015                 /*
1016                  * Change the next header field of the last header in the
1017                  * unfragmentable part.
1018                  */
1019                 if (exthdrs.ip6e_rthdr) {
1020                         nextproto = *mtod(exthdrs.ip6e_rthdr, u_char *);
1021                         *mtod(exthdrs.ip6e_rthdr, u_char *) = IPPROTO_FRAGMENT;
1022                 } else if (exthdrs.ip6e_dest1) {
1023                         nextproto = *mtod(exthdrs.ip6e_dest1, u_char *);
1024                         *mtod(exthdrs.ip6e_dest1, u_char *) = IPPROTO_FRAGMENT;
1025                 } else if (exthdrs.ip6e_hbh) {
1026                         nextproto = *mtod(exthdrs.ip6e_hbh, u_char *);
1027                         *mtod(exthdrs.ip6e_hbh, u_char *) = IPPROTO_FRAGMENT;
1028                 } else {
1029                         nextproto = ip6->ip6_nxt;
1030                         ip6->ip6_nxt = IPPROTO_FRAGMENT;
1031                 }
1032
1033                 /*
1034                  * Loop through length of segment after first fragment,
1035                  * make new header and copy data of each part and link onto
1036                  * chain.
1037                  */
1038                 m0 = m;
1039                 id = htonl(ip6_randomid());
1040                 if ((error = ip6_fragment(ifp, m, hlen, nextproto, len, id)))
1041                         goto sendorfree;
1042
1043                 in6_ifstat_inc(ifp, ifs6_out_fragok);
1044         }
1045
1046         /*
1047          * Remove leading garbages.
1048          */
1049 sendorfree:
1050         m = m0->m_nextpkt;
1051         m0->m_nextpkt = 0;
1052         m_freem(m0);
1053         for (; m; m = m0) {
1054                 m0 = m->m_nextpkt;
1055                 m->m_nextpkt = 0;
1056                 if (error == 0) {
1057                         /* Record statistics for this interface address. */
1058                         if (ia) {
1059                                 counter_u64_add(ia->ia_ifa.ifa_opackets, 1);
1060                                 counter_u64_add(ia->ia_ifa.ifa_obytes,
1061                                     m->m_pkthdr.len);
1062                         }
1063                         error = nd6_output_ifp(ifp, origifp, m, dst,
1064                             (struct route *)ro);
1065                 } else
1066                         m_freem(m);
1067         }
1068
1069         if (error == 0)
1070                 IP6STAT_INC(ip6s_fragmented);
1071
1072 done:
1073         /*
1074          * Release the route if using our private route, or if
1075          * (with flowtable) we don't have our own reference.
1076          */
1077         if (ro == &ip6route ||
1078             (ro != NULL && ro->ro_flags & RT_NORTREF))
1079                 RO_RTFREE(ro);
1080         return (error);
1081
1082 freehdrs:
1083         m_freem(exthdrs.ip6e_hbh);      /* m_freem will check if mbuf is 0 */
1084         m_freem(exthdrs.ip6e_dest1);
1085         m_freem(exthdrs.ip6e_rthdr);
1086         m_freem(exthdrs.ip6e_dest2);
1087         /* FALLTHROUGH */
1088 bad:
1089         if (m)
1090                 m_freem(m);
1091         goto done;
1092 }
1093
1094 static int
1095 ip6_copyexthdr(struct mbuf **mp, caddr_t hdr, int hlen)
1096 {
1097         struct mbuf *m;
1098
1099         if (hlen > MCLBYTES)
1100                 return (ENOBUFS); /* XXX */
1101
1102         if (hlen > MLEN)
1103                 m = m_getcl(M_NOWAIT, MT_DATA, 0);
1104         else
1105                 m = m_get(M_NOWAIT, MT_DATA);
1106         if (m == NULL)
1107                 return (ENOBUFS);
1108         m->m_len = hlen;
1109         if (hdr)
1110                 bcopy(hdr, mtod(m, caddr_t), hlen);
1111
1112         *mp = m;
1113         return (0);
1114 }
1115
1116 /*
1117  * Insert jumbo payload option.
1118  */
1119 static int
1120 ip6_insert_jumboopt(struct ip6_exthdrs *exthdrs, u_int32_t plen)
1121 {
1122         struct mbuf *mopt;
1123         u_char *optbuf;
1124         u_int32_t v;
1125
1126 #define JUMBOOPTLEN     8       /* length of jumbo payload option and padding */
1127
1128         /*
1129          * If there is no hop-by-hop options header, allocate new one.
1130          * If there is one but it doesn't have enough space to store the
1131          * jumbo payload option, allocate a cluster to store the whole options.
1132          * Otherwise, use it to store the options.
1133          */
1134         if (exthdrs->ip6e_hbh == NULL) {
1135                 mopt = m_get(M_NOWAIT, MT_DATA);
1136                 if (mopt == NULL)
1137                         return (ENOBUFS);
1138                 mopt->m_len = JUMBOOPTLEN;
1139                 optbuf = mtod(mopt, u_char *);
1140                 optbuf[1] = 0;  /* = ((JUMBOOPTLEN) >> 3) - 1 */
1141                 exthdrs->ip6e_hbh = mopt;
1142         } else {
1143                 struct ip6_hbh *hbh;
1144
1145                 mopt = exthdrs->ip6e_hbh;
1146                 if (M_TRAILINGSPACE(mopt) < JUMBOOPTLEN) {
1147                         /*
1148                          * XXX assumption:
1149                          * - exthdrs->ip6e_hbh is not referenced from places
1150                          *   other than exthdrs.
1151                          * - exthdrs->ip6e_hbh is not an mbuf chain.
1152                          */
1153                         int oldoptlen = mopt->m_len;
1154                         struct mbuf *n;
1155
1156                         /*
1157                          * XXX: give up if the whole (new) hbh header does
1158                          * not fit even in an mbuf cluster.
1159                          */
1160                         if (oldoptlen + JUMBOOPTLEN > MCLBYTES)
1161                                 return (ENOBUFS);
1162
1163                         /*
1164                          * As a consequence, we must always prepare a cluster
1165                          * at this point.
1166                          */
1167                         n = m_getcl(M_NOWAIT, MT_DATA, 0);
1168                         if (n == NULL)
1169                                 return (ENOBUFS);
1170                         n->m_len = oldoptlen + JUMBOOPTLEN;
1171                         bcopy(mtod(mopt, caddr_t), mtod(n, caddr_t),
1172                             oldoptlen);
1173                         optbuf = mtod(n, caddr_t) + oldoptlen;
1174                         m_freem(mopt);
1175                         mopt = exthdrs->ip6e_hbh = n;
1176                 } else {
1177                         optbuf = mtod(mopt, u_char *) + mopt->m_len;
1178                         mopt->m_len += JUMBOOPTLEN;
1179                 }
1180                 optbuf[0] = IP6OPT_PADN;
1181                 optbuf[1] = 1;
1182
1183                 /*
1184                  * Adjust the header length according to the pad and
1185                  * the jumbo payload option.
1186                  */
1187                 hbh = mtod(mopt, struct ip6_hbh *);
1188                 hbh->ip6h_len += (JUMBOOPTLEN >> 3);
1189         }
1190
1191         /* fill in the option. */
1192         optbuf[2] = IP6OPT_JUMBO;
1193         optbuf[3] = 4;
1194         v = (u_int32_t)htonl(plen + JUMBOOPTLEN);
1195         bcopy(&v, &optbuf[4], sizeof(u_int32_t));
1196
1197         /* finally, adjust the packet header length */
1198         exthdrs->ip6e_ip6->m_pkthdr.len += JUMBOOPTLEN;
1199
1200         return (0);
1201 #undef JUMBOOPTLEN
1202 }
1203
1204 /*
1205  * Insert fragment header and copy unfragmentable header portions.
1206  */
1207 static int
1208 ip6_insertfraghdr(struct mbuf *m0, struct mbuf *m, int hlen,
1209     struct ip6_frag **frghdrp)
1210 {
1211         struct mbuf *n, *mlast;
1212
1213         if (hlen > sizeof(struct ip6_hdr)) {
1214                 n = m_copym(m0, sizeof(struct ip6_hdr),
1215                     hlen - sizeof(struct ip6_hdr), M_NOWAIT);
1216                 if (n == NULL)
1217                         return (ENOBUFS);
1218                 m->m_next = n;
1219         } else
1220                 n = m;
1221
1222         /* Search for the last mbuf of unfragmentable part. */
1223         for (mlast = n; mlast->m_next; mlast = mlast->m_next)
1224                 ;
1225
1226         if (M_WRITABLE(mlast) &&
1227             M_TRAILINGSPACE(mlast) >= sizeof(struct ip6_frag)) {
1228                 /* use the trailing space of the last mbuf for the fragment hdr */
1229                 *frghdrp = (struct ip6_frag *)(mtod(mlast, caddr_t) +
1230                     mlast->m_len);
1231                 mlast->m_len += sizeof(struct ip6_frag);
1232                 m->m_pkthdr.len += sizeof(struct ip6_frag);
1233         } else {
1234                 /* allocate a new mbuf for the fragment header */
1235                 struct mbuf *mfrg;
1236
1237                 mfrg = m_get(M_NOWAIT, MT_DATA);
1238                 if (mfrg == NULL)
1239                         return (ENOBUFS);
1240                 mfrg->m_len = sizeof(struct ip6_frag);
1241                 *frghdrp = mtod(mfrg, struct ip6_frag *);
1242                 mlast->m_next = mfrg;
1243         }
1244
1245         return (0);
1246 }
1247
1248 /*
1249  * Calculates IPv6 path mtu for destination @dst.
1250  * Resulting MTU is stored in @mtup.
1251  *
1252  * Returns 0 on success.
1253  */
1254 static int
1255 ip6_getpmtu_ctl(u_int fibnum, const struct in6_addr *dst, u_long *mtup)
1256 {
1257         struct nhop6_extended nh6;
1258         struct in6_addr kdst;
1259         uint32_t scopeid;
1260         struct ifnet *ifp;
1261         u_long mtu;
1262         int error;
1263
1264         in6_splitscope(dst, &kdst, &scopeid);
1265         if (fib6_lookup_nh_ext(fibnum, &kdst, scopeid, NHR_REF, 0, &nh6) != 0)
1266                 return (EHOSTUNREACH);
1267
1268         ifp = nh6.nh_ifp;
1269         mtu = nh6.nh_mtu;
1270
1271         error = ip6_calcmtu(ifp, dst, mtu, mtup, NULL, 0);
1272         fib6_free_nh_ext(fibnum, &nh6);
1273
1274         return (error);
1275 }
1276
1277 /*
1278  * Calculates IPv6 path MTU for @dst based on transmit @ifp,
1279  * and cached data in @ro_pmtu.
1280  * MTU from (successful) route lookup is saved (along with dst)
1281  * inside @ro_pmtu to avoid subsequent route lookups after packet
1282  * filter processing.
1283  *
1284  * Stores mtu and always-frag value into @mtup and @alwaysfragp.
1285  * Returns 0 on success.
1286  */
1287 static int
1288 ip6_getpmtu(struct route_in6 *ro_pmtu, int do_lookup,
1289     struct ifnet *ifp, const struct in6_addr *dst, u_long *mtup,
1290     int *alwaysfragp, u_int fibnum, u_int proto)
1291 {
1292         struct nhop6_basic nh6;
1293         struct in6_addr kdst;
1294         uint32_t scopeid;
1295         struct sockaddr_in6 *sa6_dst;
1296         u_long mtu;
1297
1298         mtu = 0;
1299         if (do_lookup) {
1300
1301                 /*
1302                  * Here ro_pmtu has final destination address, while
1303                  * ro might represent immediate destination.
1304                  * Use ro_pmtu destination since mtu might differ.
1305                  */
1306                 sa6_dst = (struct sockaddr_in6 *)&ro_pmtu->ro_dst;
1307                 if (!IN6_ARE_ADDR_EQUAL(&sa6_dst->sin6_addr, dst))
1308                         ro_pmtu->ro_mtu = 0;
1309
1310                 if (ro_pmtu->ro_mtu == 0) {
1311                         bzero(sa6_dst, sizeof(*sa6_dst));
1312                         sa6_dst->sin6_family = AF_INET6;
1313                         sa6_dst->sin6_len = sizeof(struct sockaddr_in6);
1314                         sa6_dst->sin6_addr = *dst;
1315
1316                         in6_splitscope(dst, &kdst, &scopeid);
1317                         if (fib6_lookup_nh_basic(fibnum, &kdst, scopeid, 0, 0,
1318                             &nh6) == 0)
1319                                 ro_pmtu->ro_mtu = nh6.nh_mtu;
1320                 }
1321
1322                 mtu = ro_pmtu->ro_mtu;
1323         }
1324
1325         if (ro_pmtu->ro_rt)
1326                 mtu = ro_pmtu->ro_rt->rt_mtu;
1327
1328         return (ip6_calcmtu(ifp, dst, mtu, mtup, alwaysfragp, proto));
1329 }
1330
1331 /*
1332  * Calculate MTU based on transmit @ifp, route mtu @rt_mtu and
1333  * hostcache data for @dst.
1334  * Stores mtu and always-frag value into @mtup and @alwaysfragp.
1335  *
1336  * Returns 0 on success.
1337  */
1338 static int
1339 ip6_calcmtu(struct ifnet *ifp, const struct in6_addr *dst, u_long rt_mtu,
1340     u_long *mtup, int *alwaysfragp, u_int proto)
1341 {
1342         u_long mtu = 0;
1343         int alwaysfrag = 0;
1344         int error = 0;
1345
1346         if (rt_mtu > 0) {
1347                 u_int32_t ifmtu;
1348                 struct in_conninfo inc;
1349
1350                 bzero(&inc, sizeof(inc));
1351                 inc.inc_flags |= INC_ISIPV6;
1352                 inc.inc6_faddr = *dst;
1353
1354                 ifmtu = IN6_LINKMTU(ifp);
1355
1356                 /* TCP is known to react to pmtu changes so skip hc */
1357                 if (proto != IPPROTO_TCP)
1358                         mtu = tcp_hc_getmtu(&inc);
1359
1360                 if (mtu)
1361                         mtu = min(mtu, rt_mtu);
1362                 else
1363                         mtu = rt_mtu;
1364                 if (mtu == 0)
1365                         mtu = ifmtu;
1366                 else if (mtu < IPV6_MMTU) {
1367                         /*
1368                          * RFC2460 section 5, last paragraph:
1369                          * if we record ICMPv6 too big message with
1370                          * mtu < IPV6_MMTU, transmit packets sized IPV6_MMTU
1371                          * or smaller, with framgent header attached.
1372                          * (fragment header is needed regardless from the
1373                          * packet size, for translators to identify packets)
1374                          */
1375                         alwaysfrag = 1;
1376                         mtu = IPV6_MMTU;
1377                 }
1378         } else if (ifp) {
1379                 mtu = IN6_LINKMTU(ifp);
1380         } else
1381                 error = EHOSTUNREACH; /* XXX */
1382
1383         *mtup = mtu;
1384         if (alwaysfragp)
1385                 *alwaysfragp = alwaysfrag;
1386         return (error);
1387 }
1388
1389 /*
1390  * IP6 socket option processing.
1391  */
1392 int
1393 ip6_ctloutput(struct socket *so, struct sockopt *sopt)
1394 {
1395         int optdatalen, uproto;
1396         void *optdata;
1397         struct inpcb *in6p = sotoinpcb(so);
1398         int error, optval;
1399         int level, op, optname;
1400         int optlen;
1401         struct thread *td;
1402 #ifdef  RSS
1403         uint32_t rss_bucket;
1404         int retval;
1405 #endif
1406
1407 /*
1408  * Don't use more than a quarter of mbuf clusters.  N.B.:
1409  * nmbclusters is an int, but nmbclusters * MCLBYTES may overflow
1410  * on LP64 architectures, so cast to u_long to avoid undefined
1411  * behavior.  ILP32 architectures cannot have nmbclusters
1412  * large enough to overflow for other reasons.
1413  */
1414 #define IPV6_PKTOPTIONS_MBUF_LIMIT      ((u_long)nmbclusters * MCLBYTES / 4)
1415
1416         level = sopt->sopt_level;
1417         op = sopt->sopt_dir;
1418         optname = sopt->sopt_name;
1419         optlen = sopt->sopt_valsize;
1420         td = sopt->sopt_td;
1421         error = 0;
1422         optval = 0;
1423         uproto = (int)so->so_proto->pr_protocol;
1424
1425         if (level != IPPROTO_IPV6) {
1426                 error = EINVAL;
1427
1428                 if (sopt->sopt_level == SOL_SOCKET &&
1429                     sopt->sopt_dir == SOPT_SET) {
1430                         switch (sopt->sopt_name) {
1431                         case SO_REUSEADDR:
1432                                 INP_WLOCK(in6p);
1433                                 if ((so->so_options & SO_REUSEADDR) != 0)
1434                                         in6p->inp_flags2 |= INP_REUSEADDR;
1435                                 else
1436                                         in6p->inp_flags2 &= ~INP_REUSEADDR;
1437                                 INP_WUNLOCK(in6p);
1438                                 error = 0;
1439                                 break;
1440                         case SO_REUSEPORT:
1441                                 INP_WLOCK(in6p);
1442                                 if ((so->so_options & SO_REUSEPORT) != 0)
1443                                         in6p->inp_flags2 |= INP_REUSEPORT;
1444                                 else
1445                                         in6p->inp_flags2 &= ~INP_REUSEPORT;
1446                                 INP_WUNLOCK(in6p);
1447                                 error = 0;
1448                                 break;
1449                         case SO_SETFIB:
1450                                 INP_WLOCK(in6p);
1451                                 in6p->inp_inc.inc_fibnum = so->so_fibnum;
1452                                 INP_WUNLOCK(in6p);
1453                                 error = 0;
1454                                 break;
1455                         default:
1456                                 break;
1457                         }
1458                 }
1459         } else {                /* level == IPPROTO_IPV6 */
1460                 switch (op) {
1461
1462                 case SOPT_SET:
1463                         switch (optname) {
1464                         case IPV6_2292PKTOPTIONS:
1465 #ifdef IPV6_PKTOPTIONS
1466                         case IPV6_PKTOPTIONS:
1467 #endif
1468                         {
1469                                 struct mbuf *m;
1470
1471                                 if (optlen > IPV6_PKTOPTIONS_MBUF_LIMIT) {
1472                                         printf("ip6_ctloutput: mbuf limit hit\n");
1473                                         error = ENOBUFS;
1474                                         break;
1475                                 }
1476
1477                                 error = soopt_getm(sopt, &m); /* XXX */
1478                                 if (error != 0)
1479                                         break;
1480                                 error = soopt_mcopyin(sopt, m); /* XXX */
1481                                 if (error != 0)
1482                                         break;
1483                                 error = ip6_pcbopts(&in6p->in6p_outputopts,
1484                                                     m, so, sopt);
1485                                 m_freem(m); /* XXX */
1486                                 break;
1487                         }
1488
1489                         /*
1490                          * Use of some Hop-by-Hop options or some
1491                          * Destination options, might require special
1492                          * privilege.  That is, normal applications
1493                          * (without special privilege) might be forbidden
1494                          * from setting certain options in outgoing packets,
1495                          * and might never see certain options in received
1496                          * packets. [RFC 2292 Section 6]
1497                          * KAME specific note:
1498                          *  KAME prevents non-privileged users from sending or
1499                          *  receiving ANY hbh/dst options in order to avoid
1500                          *  overhead of parsing options in the kernel.
1501                          */
1502                         case IPV6_RECVHOPOPTS:
1503                         case IPV6_RECVDSTOPTS:
1504                         case IPV6_RECVRTHDRDSTOPTS:
1505                                 if (td != NULL) {
1506                                         error = priv_check(td,
1507                                             PRIV_NETINET_SETHDROPTS);
1508                                         if (error)
1509                                                 break;
1510                                 }
1511                                 /* FALLTHROUGH */
1512                         case IPV6_UNICAST_HOPS:
1513                         case IPV6_HOPLIMIT:
1514
1515                         case IPV6_RECVPKTINFO:
1516                         case IPV6_RECVHOPLIMIT:
1517                         case IPV6_RECVRTHDR:
1518                         case IPV6_RECVPATHMTU:
1519                         case IPV6_RECVTCLASS:
1520                         case IPV6_RECVFLOWID:
1521 #ifdef  RSS
1522                         case IPV6_RECVRSSBUCKETID:
1523 #endif
1524                         case IPV6_V6ONLY:
1525                         case IPV6_AUTOFLOWLABEL:
1526                         case IPV6_BINDANY:
1527                         case IPV6_BINDMULTI:
1528 #ifdef  RSS
1529                         case IPV6_RSS_LISTEN_BUCKET:
1530 #endif
1531                                 if (optname == IPV6_BINDANY && td != NULL) {
1532                                         error = priv_check(td,
1533                                             PRIV_NETINET_BINDANY);
1534                                         if (error)
1535                                                 break;
1536                                 }
1537
1538                                 if (optlen != sizeof(int)) {
1539                                         error = EINVAL;
1540                                         break;
1541                                 }
1542                                 error = sooptcopyin(sopt, &optval,
1543                                         sizeof optval, sizeof optval);
1544                                 if (error)
1545                                         break;
1546                                 switch (optname) {
1547
1548                                 case IPV6_UNICAST_HOPS:
1549                                         if (optval < -1 || optval >= 256)
1550                                                 error = EINVAL;
1551                                         else {
1552                                                 /* -1 = kernel default */
1553                                                 in6p->in6p_hops = optval;
1554                                                 if ((in6p->inp_vflag &
1555                                                      INP_IPV4) != 0)
1556                                                         in6p->inp_ip_ttl = optval;
1557                                         }
1558                                         break;
1559 #define OPTSET(bit) \
1560 do { \
1561         INP_WLOCK(in6p); \
1562         if (optval) \
1563                 in6p->inp_flags |= (bit); \
1564         else \
1565                 in6p->inp_flags &= ~(bit); \
1566         INP_WUNLOCK(in6p); \
1567 } while (/*CONSTCOND*/ 0)
1568 #define OPTSET2292(bit) \
1569 do { \
1570         INP_WLOCK(in6p); \
1571         in6p->inp_flags |= IN6P_RFC2292; \
1572         if (optval) \
1573                 in6p->inp_flags |= (bit); \
1574         else \
1575                 in6p->inp_flags &= ~(bit); \
1576         INP_WUNLOCK(in6p); \
1577 } while (/*CONSTCOND*/ 0)
1578 #define OPTBIT(bit) (in6p->inp_flags & (bit) ? 1 : 0)
1579
1580 #define OPTSET2(bit, val) do {                                          \
1581         INP_WLOCK(in6p);                                                \
1582         if (val)                                                        \
1583                 in6p->inp_flags2 |= bit;                                \
1584         else                                                            \
1585                 in6p->inp_flags2 &= ~bit;                               \
1586         INP_WUNLOCK(in6p);                                              \
1587 } while (0)
1588 #define OPTBIT2(bit) (in6p->inp_flags2 & (bit) ? 1 : 0)
1589
1590                                 case IPV6_RECVPKTINFO:
1591                                         /* cannot mix with RFC2292 */
1592                                         if (OPTBIT(IN6P_RFC2292)) {
1593                                                 error = EINVAL;
1594                                                 break;
1595                                         }
1596                                         OPTSET(IN6P_PKTINFO);
1597                                         break;
1598
1599                                 case IPV6_HOPLIMIT:
1600                                 {
1601                                         struct ip6_pktopts **optp;
1602
1603                                         /* cannot mix with RFC2292 */
1604                                         if (OPTBIT(IN6P_RFC2292)) {
1605                                                 error = EINVAL;
1606                                                 break;
1607                                         }
1608                                         optp = &in6p->in6p_outputopts;
1609                                         error = ip6_pcbopt(IPV6_HOPLIMIT,
1610                                             (u_char *)&optval, sizeof(optval),
1611                                             optp, (td != NULL) ? td->td_ucred :
1612                                             NULL, uproto);
1613                                         break;
1614                                 }
1615
1616                                 case IPV6_RECVHOPLIMIT:
1617                                         /* cannot mix with RFC2292 */
1618                                         if (OPTBIT(IN6P_RFC2292)) {
1619                                                 error = EINVAL;
1620                                                 break;
1621                                         }
1622                                         OPTSET(IN6P_HOPLIMIT);
1623                                         break;
1624
1625                                 case IPV6_RECVHOPOPTS:
1626                                         /* cannot mix with RFC2292 */
1627                                         if (OPTBIT(IN6P_RFC2292)) {
1628                                                 error = EINVAL;
1629                                                 break;
1630                                         }
1631                                         OPTSET(IN6P_HOPOPTS);
1632                                         break;
1633
1634                                 case IPV6_RECVDSTOPTS:
1635                                         /* cannot mix with RFC2292 */
1636                                         if (OPTBIT(IN6P_RFC2292)) {
1637                                                 error = EINVAL;
1638                                                 break;
1639                                         }
1640                                         OPTSET(IN6P_DSTOPTS);
1641                                         break;
1642
1643                                 case IPV6_RECVRTHDRDSTOPTS:
1644                                         /* cannot mix with RFC2292 */
1645                                         if (OPTBIT(IN6P_RFC2292)) {
1646                                                 error = EINVAL;
1647                                                 break;
1648                                         }
1649                                         OPTSET(IN6P_RTHDRDSTOPTS);
1650                                         break;
1651
1652                                 case IPV6_RECVRTHDR:
1653                                         /* cannot mix with RFC2292 */
1654                                         if (OPTBIT(IN6P_RFC2292)) {
1655                                                 error = EINVAL;
1656                                                 break;
1657                                         }
1658                                         OPTSET(IN6P_RTHDR);
1659                                         break;
1660
1661                                 case IPV6_RECVPATHMTU:
1662                                         /*
1663                                          * We ignore this option for TCP
1664                                          * sockets.
1665                                          * (RFC3542 leaves this case
1666                                          * unspecified.)
1667                                          */
1668                                         if (uproto != IPPROTO_TCP)
1669                                                 OPTSET(IN6P_MTU);
1670                                         break;
1671
1672                                 case IPV6_RECVFLOWID:
1673                                         OPTSET2(INP_RECVFLOWID, optval);
1674                                         break;
1675
1676 #ifdef  RSS
1677                                 case IPV6_RECVRSSBUCKETID:
1678                                         OPTSET2(INP_RECVRSSBUCKETID, optval);
1679                                         break;
1680 #endif
1681
1682                                 case IPV6_V6ONLY:
1683                                         /*
1684                                          * make setsockopt(IPV6_V6ONLY)
1685                                          * available only prior to bind(2).
1686                                          * see ipng mailing list, Jun 22 2001.
1687                                          */
1688                                         if (in6p->inp_lport ||
1689                                             !IN6_IS_ADDR_UNSPECIFIED(&in6p->in6p_laddr)) {
1690                                                 error = EINVAL;
1691                                                 break;
1692                                         }
1693                                         OPTSET(IN6P_IPV6_V6ONLY);
1694                                         if (optval)
1695                                                 in6p->inp_vflag &= ~INP_IPV4;
1696                                         else
1697                                                 in6p->inp_vflag |= INP_IPV4;
1698                                         break;
1699                                 case IPV6_RECVTCLASS:
1700                                         /* cannot mix with RFC2292 XXX */
1701                                         if (OPTBIT(IN6P_RFC2292)) {
1702                                                 error = EINVAL;
1703                                                 break;
1704                                         }
1705                                         OPTSET(IN6P_TCLASS);
1706                                         break;
1707                                 case IPV6_AUTOFLOWLABEL:
1708                                         OPTSET(IN6P_AUTOFLOWLABEL);
1709                                         break;
1710
1711                                 case IPV6_BINDANY:
1712                                         OPTSET(INP_BINDANY);
1713                                         break;
1714
1715                                 case IPV6_BINDMULTI:
1716                                         OPTSET2(INP_BINDMULTI, optval);
1717                                         break;
1718 #ifdef  RSS
1719                                 case IPV6_RSS_LISTEN_BUCKET:
1720                                         if ((optval >= 0) &&
1721                                             (optval < rss_getnumbuckets())) {
1722                                                 in6p->inp_rss_listen_bucket = optval;
1723                                                 OPTSET2(INP_RSS_BUCKET_SET, 1);
1724                                         } else {
1725                                                 error = EINVAL;
1726                                         }
1727                                         break;
1728 #endif
1729                                 }
1730                                 break;
1731
1732                         case IPV6_TCLASS:
1733                         case IPV6_DONTFRAG:
1734                         case IPV6_USE_MIN_MTU:
1735                         case IPV6_PREFER_TEMPADDR:
1736                                 if (optlen != sizeof(optval)) {
1737                                         error = EINVAL;
1738                                         break;
1739                                 }
1740                                 error = sooptcopyin(sopt, &optval,
1741                                         sizeof optval, sizeof optval);
1742                                 if (error)
1743                                         break;
1744                                 {
1745                                         struct ip6_pktopts **optp;
1746                                         optp = &in6p->in6p_outputopts;
1747                                         error = ip6_pcbopt(optname,
1748                                             (u_char *)&optval, sizeof(optval),
1749                                             optp, (td != NULL) ? td->td_ucred :
1750                                             NULL, uproto);
1751                                         break;
1752                                 }
1753
1754                         case IPV6_2292PKTINFO:
1755                         case IPV6_2292HOPLIMIT:
1756                         case IPV6_2292HOPOPTS:
1757                         case IPV6_2292DSTOPTS:
1758                         case IPV6_2292RTHDR:
1759                                 /* RFC 2292 */
1760                                 if (optlen != sizeof(int)) {
1761                                         error = EINVAL;
1762                                         break;
1763                                 }
1764                                 error = sooptcopyin(sopt, &optval,
1765                                         sizeof optval, sizeof optval);
1766                                 if (error)
1767                                         break;
1768                                 switch (optname) {
1769                                 case IPV6_2292PKTINFO:
1770                                         OPTSET2292(IN6P_PKTINFO);
1771                                         break;
1772                                 case IPV6_2292HOPLIMIT:
1773                                         OPTSET2292(IN6P_HOPLIMIT);
1774                                         break;
1775                                 case IPV6_2292HOPOPTS:
1776                                         /*
1777                                          * Check super-user privilege.
1778                                          * See comments for IPV6_RECVHOPOPTS.
1779                                          */
1780                                         if (td != NULL) {
1781                                                 error = priv_check(td,
1782                                                     PRIV_NETINET_SETHDROPTS);
1783                                                 if (error)
1784                                                         return (error);
1785                                         }
1786                                         OPTSET2292(IN6P_HOPOPTS);
1787                                         break;
1788                                 case IPV6_2292DSTOPTS:
1789                                         if (td != NULL) {
1790                                                 error = priv_check(td,
1791                                                     PRIV_NETINET_SETHDROPTS);
1792                                                 if (error)
1793                                                         return (error);
1794                                         }
1795                                         OPTSET2292(IN6P_DSTOPTS|IN6P_RTHDRDSTOPTS); /* XXX */
1796                                         break;
1797                                 case IPV6_2292RTHDR:
1798                                         OPTSET2292(IN6P_RTHDR);
1799                                         break;
1800                                 }
1801                                 break;
1802                         case IPV6_PKTINFO:
1803                         case IPV6_HOPOPTS:
1804                         case IPV6_RTHDR:
1805                         case IPV6_DSTOPTS:
1806                         case IPV6_RTHDRDSTOPTS:
1807                         case IPV6_NEXTHOP:
1808                         {
1809                                 /* new advanced API (RFC3542) */
1810                                 u_char *optbuf;
1811                                 u_char optbuf_storage[MCLBYTES];
1812                                 int optlen;
1813                                 struct ip6_pktopts **optp;
1814
1815                                 /* cannot mix with RFC2292 */
1816                                 if (OPTBIT(IN6P_RFC2292)) {
1817                                         error = EINVAL;
1818                                         break;
1819                                 }
1820
1821                                 /*
1822                                  * We only ensure valsize is not too large
1823                                  * here.  Further validation will be done
1824                                  * later.
1825                                  */
1826                                 error = sooptcopyin(sopt, optbuf_storage,
1827                                     sizeof(optbuf_storage), 0);
1828                                 if (error)
1829                                         break;
1830                                 optlen = sopt->sopt_valsize;
1831                                 optbuf = optbuf_storage;
1832                                 optp = &in6p->in6p_outputopts;
1833                                 error = ip6_pcbopt(optname, optbuf, optlen,
1834                                     optp, (td != NULL) ? td->td_ucred : NULL,
1835                                     uproto);
1836                                 break;
1837                         }
1838 #undef OPTSET
1839
1840                         case IPV6_MULTICAST_IF:
1841                         case IPV6_MULTICAST_HOPS:
1842                         case IPV6_MULTICAST_LOOP:
1843                         case IPV6_JOIN_GROUP:
1844                         case IPV6_LEAVE_GROUP:
1845                         case IPV6_MSFILTER:
1846                         case MCAST_BLOCK_SOURCE:
1847                         case MCAST_UNBLOCK_SOURCE:
1848                         case MCAST_JOIN_GROUP:
1849                         case MCAST_LEAVE_GROUP:
1850                         case MCAST_JOIN_SOURCE_GROUP:
1851                         case MCAST_LEAVE_SOURCE_GROUP:
1852                                 error = ip6_setmoptions(in6p, sopt);
1853                                 break;
1854
1855                         case IPV6_PORTRANGE:
1856                                 error = sooptcopyin(sopt, &optval,
1857                                     sizeof optval, sizeof optval);
1858                                 if (error)
1859                                         break;
1860
1861                                 INP_WLOCK(in6p);
1862                                 switch (optval) {
1863                                 case IPV6_PORTRANGE_DEFAULT:
1864                                         in6p->inp_flags &= ~(INP_LOWPORT);
1865                                         in6p->inp_flags &= ~(INP_HIGHPORT);
1866                                         break;
1867
1868                                 case IPV6_PORTRANGE_HIGH:
1869                                         in6p->inp_flags &= ~(INP_LOWPORT);
1870                                         in6p->inp_flags |= INP_HIGHPORT;
1871                                         break;
1872
1873                                 case IPV6_PORTRANGE_LOW:
1874                                         in6p->inp_flags &= ~(INP_HIGHPORT);
1875                                         in6p->inp_flags |= INP_LOWPORT;
1876                                         break;
1877
1878                                 default:
1879                                         error = EINVAL;
1880                                         break;
1881                                 }
1882                                 INP_WUNLOCK(in6p);
1883                                 break;
1884
1885 #if defined(IPSEC) || defined(IPSEC_SUPPORT)
1886                         case IPV6_IPSEC_POLICY:
1887                                 if (IPSEC_ENABLED(ipv6)) {
1888                                         error = IPSEC_PCBCTL(ipv6, in6p, sopt);
1889                                         break;
1890                                 }
1891                                 /* FALLTHROUGH */
1892 #endif /* IPSEC */
1893
1894                         default:
1895                                 error = ENOPROTOOPT;
1896                                 break;
1897                         }
1898                         break;
1899
1900                 case SOPT_GET:
1901                         switch (optname) {
1902
1903                         case IPV6_2292PKTOPTIONS:
1904 #ifdef IPV6_PKTOPTIONS
1905                         case IPV6_PKTOPTIONS:
1906 #endif
1907                                 /*
1908                                  * RFC3542 (effectively) deprecated the
1909                                  * semantics of the 2292-style pktoptions.
1910                                  * Since it was not reliable in nature (i.e.,
1911                                  * applications had to expect the lack of some
1912                                  * information after all), it would make sense
1913                                  * to simplify this part by always returning
1914                                  * empty data.
1915                                  */
1916                                 sopt->sopt_valsize = 0;
1917                                 break;
1918
1919                         case IPV6_RECVHOPOPTS:
1920                         case IPV6_RECVDSTOPTS:
1921                         case IPV6_RECVRTHDRDSTOPTS:
1922                         case IPV6_UNICAST_HOPS:
1923                         case IPV6_RECVPKTINFO:
1924                         case IPV6_RECVHOPLIMIT:
1925                         case IPV6_RECVRTHDR:
1926                         case IPV6_RECVPATHMTU:
1927
1928                         case IPV6_V6ONLY:
1929                         case IPV6_PORTRANGE:
1930                         case IPV6_RECVTCLASS:
1931                         case IPV6_AUTOFLOWLABEL:
1932                         case IPV6_BINDANY:
1933                         case IPV6_FLOWID:
1934                         case IPV6_FLOWTYPE:
1935                         case IPV6_RECVFLOWID:
1936 #ifdef  RSS
1937                         case IPV6_RSSBUCKETID:
1938                         case IPV6_RECVRSSBUCKETID:
1939 #endif
1940                         case IPV6_BINDMULTI:
1941                                 switch (optname) {
1942
1943                                 case IPV6_RECVHOPOPTS:
1944                                         optval = OPTBIT(IN6P_HOPOPTS);
1945                                         break;
1946
1947                                 case IPV6_RECVDSTOPTS:
1948                                         optval = OPTBIT(IN6P_DSTOPTS);
1949                                         break;
1950
1951                                 case IPV6_RECVRTHDRDSTOPTS:
1952                                         optval = OPTBIT(IN6P_RTHDRDSTOPTS);
1953                                         break;
1954
1955                                 case IPV6_UNICAST_HOPS:
1956                                         optval = in6p->in6p_hops;
1957                                         break;
1958
1959                                 case IPV6_RECVPKTINFO:
1960                                         optval = OPTBIT(IN6P_PKTINFO);
1961                                         break;
1962
1963                                 case IPV6_RECVHOPLIMIT:
1964                                         optval = OPTBIT(IN6P_HOPLIMIT);
1965                                         break;
1966
1967                                 case IPV6_RECVRTHDR:
1968                                         optval = OPTBIT(IN6P_RTHDR);
1969                                         break;
1970
1971                                 case IPV6_RECVPATHMTU:
1972                                         optval = OPTBIT(IN6P_MTU);
1973                                         break;
1974
1975                                 case IPV6_V6ONLY:
1976                                         optval = OPTBIT(IN6P_IPV6_V6ONLY);
1977                                         break;
1978
1979                                 case IPV6_PORTRANGE:
1980                                     {
1981                                         int flags;
1982                                         flags = in6p->inp_flags;
1983                                         if (flags & INP_HIGHPORT)
1984                                                 optval = IPV6_PORTRANGE_HIGH;
1985                                         else if (flags & INP_LOWPORT)
1986                                                 optval = IPV6_PORTRANGE_LOW;
1987                                         else
1988                                                 optval = 0;
1989                                         break;
1990                                     }
1991                                 case IPV6_RECVTCLASS:
1992                                         optval = OPTBIT(IN6P_TCLASS);
1993                                         break;
1994
1995                                 case IPV6_AUTOFLOWLABEL:
1996                                         optval = OPTBIT(IN6P_AUTOFLOWLABEL);
1997                                         break;
1998
1999                                 case IPV6_BINDANY:
2000                                         optval = OPTBIT(INP_BINDANY);
2001                                         break;
2002
2003                                 case IPV6_FLOWID:
2004                                         optval = in6p->inp_flowid;
2005                                         break;
2006
2007                                 case IPV6_FLOWTYPE:
2008                                         optval = in6p->inp_flowtype;
2009                                         break;
2010
2011                                 case IPV6_RECVFLOWID:
2012                                         optval = OPTBIT2(INP_RECVFLOWID);
2013                                         break;
2014 #ifdef  RSS
2015                                 case IPV6_RSSBUCKETID:
2016                                         retval =
2017                                             rss_hash2bucket(in6p->inp_flowid,
2018                                             in6p->inp_flowtype,
2019                                             &rss_bucket);
2020                                         if (retval == 0)
2021                                                 optval = rss_bucket;
2022                                         else
2023                                                 error = EINVAL;
2024                                         break;
2025
2026                                 case IPV6_RECVRSSBUCKETID:
2027                                         optval = OPTBIT2(INP_RECVRSSBUCKETID);
2028                                         break;
2029 #endif
2030
2031                                 case IPV6_BINDMULTI:
2032                                         optval = OPTBIT2(INP_BINDMULTI);
2033                                         break;
2034
2035                                 }
2036                                 if (error)
2037                                         break;
2038                                 error = sooptcopyout(sopt, &optval,
2039                                         sizeof optval);
2040                                 break;
2041
2042                         case IPV6_PATHMTU:
2043                         {
2044                                 u_long pmtu = 0;
2045                                 struct ip6_mtuinfo mtuinfo;
2046
2047                                 if (!(so->so_state & SS_ISCONNECTED))
2048                                         return (ENOTCONN);
2049                                 /*
2050                                  * XXX: we dot not consider the case of source
2051                                  * routing, or optional information to specify
2052                                  * the outgoing interface.
2053                                  */
2054                                 error = ip6_getpmtu_ctl(so->so_fibnum,
2055                                     &in6p->in6p_faddr, &pmtu);
2056                                 if (error)
2057                                         break;
2058                                 if (pmtu > IPV6_MAXPACKET)
2059                                         pmtu = IPV6_MAXPACKET;
2060
2061                                 bzero(&mtuinfo, sizeof(mtuinfo));
2062                                 mtuinfo.ip6m_mtu = (u_int32_t)pmtu;
2063                                 optdata = (void *)&mtuinfo;
2064                                 optdatalen = sizeof(mtuinfo);
2065                                 error = sooptcopyout(sopt, optdata,
2066                                     optdatalen);
2067                                 break;
2068                         }
2069
2070                         case IPV6_2292PKTINFO:
2071                         case IPV6_2292HOPLIMIT:
2072                         case IPV6_2292HOPOPTS:
2073                         case IPV6_2292RTHDR:
2074                         case IPV6_2292DSTOPTS:
2075                                 switch (optname) {
2076                                 case IPV6_2292PKTINFO:
2077                                         optval = OPTBIT(IN6P_PKTINFO);
2078                                         break;
2079                                 case IPV6_2292HOPLIMIT:
2080                                         optval = OPTBIT(IN6P_HOPLIMIT);
2081                                         break;
2082                                 case IPV6_2292HOPOPTS:
2083                                         optval = OPTBIT(IN6P_HOPOPTS);
2084                                         break;
2085                                 case IPV6_2292RTHDR:
2086                                         optval = OPTBIT(IN6P_RTHDR);
2087                                         break;
2088                                 case IPV6_2292DSTOPTS:
2089                                         optval = OPTBIT(IN6P_DSTOPTS|IN6P_RTHDRDSTOPTS);
2090                                         break;
2091                                 }
2092                                 error = sooptcopyout(sopt, &optval,
2093                                     sizeof optval);
2094                                 break;
2095                         case IPV6_PKTINFO:
2096                         case IPV6_HOPOPTS:
2097                         case IPV6_RTHDR:
2098                         case IPV6_DSTOPTS:
2099                         case IPV6_RTHDRDSTOPTS:
2100                         case IPV6_NEXTHOP:
2101                         case IPV6_TCLASS:
2102                         case IPV6_DONTFRAG:
2103                         case IPV6_USE_MIN_MTU:
2104                         case IPV6_PREFER_TEMPADDR:
2105                                 error = ip6_getpcbopt(in6p->in6p_outputopts,
2106                                     optname, sopt);
2107                                 break;
2108
2109                         case IPV6_MULTICAST_IF:
2110                         case IPV6_MULTICAST_HOPS:
2111                         case IPV6_MULTICAST_LOOP:
2112                         case IPV6_MSFILTER:
2113                                 error = ip6_getmoptions(in6p, sopt);
2114                                 break;
2115
2116 #if defined(IPSEC) || defined(IPSEC_SUPPORT)
2117                         case IPV6_IPSEC_POLICY:
2118                                 if (IPSEC_ENABLED(ipv6)) {
2119                                         error = IPSEC_PCBCTL(ipv6, in6p, sopt);
2120                                         break;
2121                                 }
2122                                 /* FALLTHROUGH */
2123 #endif /* IPSEC */
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)\
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 && (ifp->if_afdata[AF_INET6] == NULL ||
2667                     (ND_IFINFO(ifp)->flags & ND6_IFF_IFDISABLED) != 0))
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 }