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