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