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