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