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