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