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