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