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