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