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