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