2 * SPDX-License-Identifier: BSD-3-Clause
4 * Copyright (c) 1982, 1986, 1988, 1993
5 * The Regents of the University of California.
6 * Copyright (c) 2005 Andre Oppermann, Internet Business Solutions AG.
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
17 * 3. Neither the name of the University nor the names of its contributors
18 * may be used to endorse or promote products derived from this software
19 * without specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34 #include <sys/cdefs.h>
35 __FBSDID("$FreeBSD$");
37 #include "opt_ipstealth.h"
39 #include <sys/param.h>
40 #include <sys/systm.h>
42 #include <sys/domain.h>
43 #include <sys/protosw.h>
44 #include <sys/socket.h>
46 #include <sys/kernel.h>
47 #include <sys/syslog.h>
48 #include <sys/sysctl.h>
51 #include <net/if_types.h>
52 #include <net/if_var.h>
53 #include <net/if_dl.h>
54 #include <net/route.h>
55 #include <net/netisr.h>
58 #include <netinet/in.h>
59 #include <netinet/in_fib.h>
60 #include <netinet/in_systm.h>
61 #include <netinet/in_var.h>
62 #include <netinet/ip.h>
63 #include <netinet/in_pcb.h>
64 #include <netinet/ip_var.h>
65 #include <netinet/ip_options.h>
66 #include <netinet/ip_icmp.h>
67 #include <machine/in_cksum.h>
69 #include <sys/socketvar.h>
71 VNET_DEFINE_STATIC(int, ip_dosourceroute);
72 SYSCTL_INT(_net_inet_ip, IPCTL_SOURCEROUTE, sourceroute,
73 CTLFLAG_VNET | CTLFLAG_RW, &VNET_NAME(ip_dosourceroute), 0,
74 "Enable forwarding source routed IP packets");
75 #define V_ip_dosourceroute VNET(ip_dosourceroute)
77 VNET_DEFINE_STATIC(int, ip_acceptsourceroute);
78 SYSCTL_INT(_net_inet_ip, IPCTL_ACCEPTSOURCEROUTE, accept_sourceroute,
79 CTLFLAG_VNET | CTLFLAG_RW, &VNET_NAME(ip_acceptsourceroute), 0,
80 "Enable accepting source routed IP packets");
81 #define V_ip_acceptsourceroute VNET(ip_acceptsourceroute)
83 VNET_DEFINE(int, ip_doopts) = 1; /* 0 = ignore, 1 = process, 2 = reject */
84 SYSCTL_INT(_net_inet_ip, OID_AUTO, process_options, CTLFLAG_VNET | CTLFLAG_RW,
85 &VNET_NAME(ip_doopts), 0, "Enable IP options processing ([LS]SRR, RR, TS)");
87 static void save_rte(struct mbuf *m, u_char *, struct in_addr);
90 * Do option processing on a datagram, possibly discarding it if bad options
91 * are encountered, or forwarding it if source-routed.
93 * The pass argument is used when operating in the IPSTEALTH mode to tell
94 * what options to process: [LS]SRR (pass 0) or the others (pass 1). The
95 * reason for as many as two passes is that when doing IPSTEALTH, non-routing
96 * options should be processed only if the packet is for us.
98 * Returns 1 if packet has been forwarded/freed, 0 if the packet should be
102 ip_dooptions(struct mbuf *m, int pass)
104 struct ip *ip = mtod(m, struct ip *);
106 struct in_ifaddr *ia;
107 int opt, optlen, cnt, off, code, type = ICMP_PARAMPROB, forward = 0;
108 struct in_addr *sin, dst;
110 struct nhop4_extended nh_ext;
111 struct sockaddr_in ipaddr = { sizeof(ipaddr), AF_INET };
113 /* Ignore or reject packets with IP options. */
114 if (V_ip_doopts == 0)
116 else if (V_ip_doopts == 2) {
118 code = ICMP_UNREACH_FILTER_PROHIB;
124 cp = (u_char *)(ip + 1);
125 cnt = (ip->ip_hl << 2) - sizeof (struct ip);
126 for (; cnt > 0; cnt -= optlen, cp += optlen) {
127 opt = cp[IPOPT_OPTVAL];
128 if (opt == IPOPT_EOL)
130 if (opt == IPOPT_NOP)
133 if (cnt < IPOPT_OLEN + sizeof(*cp)) {
134 code = &cp[IPOPT_OLEN] - (u_char *)ip;
137 optlen = cp[IPOPT_OLEN];
138 if (optlen < IPOPT_OLEN + sizeof(*cp) || optlen > cnt) {
139 code = &cp[IPOPT_OLEN] - (u_char *)ip;
149 * Source routing with record. Find interface with current
150 * destination address. If none on this machine then drop if
151 * strictly routed, or do nothing if loosely routed. Record
152 * interface address and bring up next address component. If
153 * strictly routed make sure next address is on directly
159 if (V_ipstealth && pass > 0)
162 if (optlen < IPOPT_OFFSET + sizeof(*cp)) {
163 code = &cp[IPOPT_OLEN] - (u_char *)ip;
166 if ((off = cp[IPOPT_OFFSET]) < IPOPT_MINOFF) {
167 code = &cp[IPOPT_OFFSET] - (u_char *)ip;
170 ipaddr.sin_addr = ip->ip_dst;
171 if (ifa_ifwithaddr_check((struct sockaddr *)&ipaddr)
173 if (opt == IPOPT_SSRR) {
175 code = ICMP_UNREACH_SRCFAIL;
178 if (!V_ip_dosourceroute)
179 goto nosourcerouting;
181 * Loose routing, and not at next destination
182 * yet; nothing to do except forward.
186 off--; /* 0 origin */
187 if (off > optlen - (int)sizeof(struct in_addr)) {
189 * End of source route. Should be for us.
191 if (!V_ip_acceptsourceroute)
192 goto nosourcerouting;
193 save_rte(m, cp, ip->ip_src);
200 if (!V_ip_dosourceroute) {
201 if (V_ipforwarding) {
202 char srcbuf[INET_ADDRSTRLEN];
203 char dstbuf[INET_ADDRSTRLEN];
206 * Acting as a router, so generate
211 "attempted source route from %s "
213 inet_ntoa_r(ip->ip_src, srcbuf),
214 inet_ntoa_r(ip->ip_dst, dstbuf));
216 code = ICMP_UNREACH_SRCFAIL;
220 * Not acting as a router, so
226 IPSTAT_INC(ips_cantforward);
234 * locate outgoing interface
236 (void)memcpy(&ipaddr.sin_addr, cp + off,
237 sizeof(ipaddr.sin_addr));
240 code = ICMP_UNREACH_SRCFAIL;
242 if (opt == IPOPT_SSRR) {
243 #define INA struct in_ifaddr *
244 #define SA struct sockaddr *
245 ia = (INA)ifa_ifwithdstaddr((SA)&ipaddr,
248 ia = (INA)ifa_ifwithnet((SA)&ipaddr, 0,
253 memcpy(cp + off, &(IA_SIN(ia)->sin_addr),
254 sizeof(struct in_addr));
256 /* XXX MRT 0 for routing */
257 if (fib4_lookup_nh_ext(M_GETFIB(m),
258 ipaddr.sin_addr, 0, 0, &nh_ext) != 0)
261 memcpy(cp + off, &nh_ext.nh_src,
262 sizeof(struct in_addr));
265 ip->ip_dst = ipaddr.sin_addr;
266 cp[IPOPT_OFFSET] += sizeof(struct in_addr);
268 * Let ip_intr's mcast routing check handle mcast pkts
270 forward = !IN_MULTICAST(ntohl(ip->ip_dst.s_addr));
275 if (V_ipstealth && pass == 0)
278 if (optlen < IPOPT_OFFSET + sizeof(*cp)) {
279 code = &cp[IPOPT_OFFSET] - (u_char *)ip;
282 if ((off = cp[IPOPT_OFFSET]) < IPOPT_MINOFF) {
283 code = &cp[IPOPT_OFFSET] - (u_char *)ip;
287 * If no space remains, ignore.
289 off--; /* 0 origin */
290 if (off > optlen - (int)sizeof(struct in_addr))
292 (void)memcpy(&ipaddr.sin_addr, &ip->ip_dst,
293 sizeof(ipaddr.sin_addr));
295 * Locate outgoing interface; if we're the
296 * destination, use the incoming interface (should be
299 if ((ia = (INA)ifa_ifwithaddr((SA)&ipaddr)) != NULL) {
300 memcpy(cp + off, &(IA_SIN(ia)->sin_addr),
301 sizeof(struct in_addr));
302 } else if (fib4_lookup_nh_ext(M_GETFIB(m),
303 ipaddr.sin_addr, 0, 0, &nh_ext) == 0) {
304 memcpy(cp + off, &nh_ext.nh_src,
305 sizeof(struct in_addr));
308 code = ICMP_UNREACH_HOST;
311 cp[IPOPT_OFFSET] += sizeof(struct in_addr);
316 if (V_ipstealth && pass == 0)
319 code = cp - (u_char *)ip;
320 if (optlen < 4 || optlen > 40) {
321 code = &cp[IPOPT_OLEN] - (u_char *)ip;
324 if ((off = cp[IPOPT_OFFSET]) < 5) {
325 code = &cp[IPOPT_OLEN] - (u_char *)ip;
328 if (off > optlen - (int)sizeof(int32_t)) {
329 cp[IPOPT_OFFSET + 1] += (1 << 4);
330 if ((cp[IPOPT_OFFSET + 1] & 0xf0) == 0) {
331 code = &cp[IPOPT_OFFSET] - (u_char *)ip;
336 off--; /* 0 origin */
337 sin = (struct in_addr *)(cp + off);
338 switch (cp[IPOPT_OFFSET + 1] & 0x0f) {
340 case IPOPT_TS_TSONLY:
343 case IPOPT_TS_TSANDADDR:
344 if (off + sizeof(uint32_t) +
345 sizeof(struct in_addr) > optlen) {
346 code = &cp[IPOPT_OFFSET] - (u_char *)ip;
349 ipaddr.sin_addr = dst;
350 ia = (INA)ifaof_ifpforaddr((SA)&ipaddr,
354 (void)memcpy(sin, &IA_SIN(ia)->sin_addr,
355 sizeof(struct in_addr));
356 cp[IPOPT_OFFSET] += sizeof(struct in_addr);
357 off += sizeof(struct in_addr);
360 case IPOPT_TS_PRESPEC:
361 if (off + sizeof(uint32_t) +
362 sizeof(struct in_addr) > optlen) {
363 code = &cp[IPOPT_OFFSET] - (u_char *)ip;
366 (void)memcpy(&ipaddr.sin_addr, sin,
367 sizeof(struct in_addr));
368 if (ifa_ifwithaddr_check((SA)&ipaddr) == 0)
370 cp[IPOPT_OFFSET] += sizeof(struct in_addr);
371 off += sizeof(struct in_addr);
375 code = &cp[IPOPT_OFFSET + 1] - (u_char *)ip;
379 (void)memcpy(cp + off, &ntime, sizeof(uint32_t));
380 cp[IPOPT_OFFSET] += sizeof(uint32_t);
384 if (forward && V_ipforwarding) {
392 icmp_error(m, type, code, 0, 0);
393 IPSTAT_INC(ips_badoptions);
398 * Save incoming source route for use in replies, to be picked up later by
399 * ip_srcroute if the receiver is interested.
402 save_rte(struct mbuf *m, u_char *option, struct in_addr dst)
405 struct ipopt_tag *opts;
407 opts = (struct ipopt_tag *)m_tag_get(PACKET_TAG_IPOPTIONS,
408 sizeof(struct ipopt_tag), M_NOWAIT);
412 olen = option[IPOPT_OLEN];
413 if (olen > sizeof(opts->ip_srcrt) - (1 + sizeof(dst))) {
414 m_tag_free((struct m_tag *)opts);
417 bcopy(option, opts->ip_srcrt.srcopt, olen);
418 opts->ip_nhops = (olen - IPOPT_OFFSET - 1) / sizeof(struct in_addr);
419 opts->ip_srcrt.dst = dst;
420 m_tag_prepend(m, (struct m_tag *)opts);
424 * Retrieve incoming source route for use in replies, in the same form used
425 * by setsockopt. The first hop is placed before the options, will be
429 ip_srcroute(struct mbuf *m0)
431 struct in_addr *p, *q;
433 struct ipopt_tag *opts;
435 opts = (struct ipopt_tag *)m_tag_find(m0, PACKET_TAG_IPOPTIONS, NULL);
439 if (opts->ip_nhops == 0)
441 m = m_get(M_NOWAIT, MT_DATA);
445 #define OPTSIZ (sizeof(opts->ip_srcrt.nop) + sizeof(opts->ip_srcrt.srcopt))
447 /* length is (nhops+1)*sizeof(addr) + sizeof(nop + srcrt header) */
448 m->m_len = opts->ip_nhops * sizeof(struct in_addr) +
449 sizeof(struct in_addr) + OPTSIZ;
452 * First, save first hop for return route.
454 p = &(opts->ip_srcrt.route[opts->ip_nhops - 1]);
455 *(mtod(m, struct in_addr *)) = *p--;
458 * Copy option fields and padding (nop) to mbuf.
460 opts->ip_srcrt.nop = IPOPT_NOP;
461 opts->ip_srcrt.srcopt[IPOPT_OFFSET] = IPOPT_MINOFF;
462 (void)memcpy(mtod(m, caddr_t) + sizeof(struct in_addr),
463 &(opts->ip_srcrt.nop), OPTSIZ);
464 q = (struct in_addr *)(mtod(m, caddr_t) +
465 sizeof(struct in_addr) + OPTSIZ);
468 * Record return path as an IP source route, reversing the path
469 * (pointers are now aligned).
471 while (p >= opts->ip_srcrt.route) {
475 * Last hop goes to final destination.
477 *q = opts->ip_srcrt.dst;
478 m_tag_delete(m0, (struct m_tag *)opts);
483 * Strip out IP options, at higher level protocol in the kernel.
486 ip_stripoptions(struct mbuf *m)
488 struct ip *ip = mtod(m, struct ip *);
491 olen = (ip->ip_hl << 2) - sizeof(struct ip);
493 if (m->m_flags & M_PKTHDR)
494 m->m_pkthdr.len -= olen;
495 ip->ip_len = htons(ntohs(ip->ip_len) - olen);
496 ip->ip_hl = sizeof(struct ip) >> 2;
498 bcopy((char *)ip + sizeof(struct ip) + olen, (ip + 1),
499 (size_t )(m->m_len - sizeof(struct ip)));
503 * Insert IP options into preformed packet. Adjust IP destination as
504 * required for IP source routing, as indicated by a non-zero in_addr at the
505 * start of the options.
507 * XXX This routine assumes that the packet has no options in place.
510 ip_insertoptions(struct mbuf *m, struct mbuf *opt, int *phlen)
512 struct ipoption *p = mtod(opt, struct ipoption *);
514 struct ip *ip = mtod(m, struct ip *);
517 optlen = opt->m_len - sizeof(p->ipopt_dst);
518 if (optlen + ntohs(ip->ip_len) > IP_MAXPACKET) {
520 return (m); /* XXX should fail */
522 if (p->ipopt_dst.s_addr)
523 ip->ip_dst = p->ipopt_dst;
524 if (!M_WRITABLE(m) || M_LEADINGSPACE(m) < optlen) {
525 n = m_gethdr(M_NOWAIT, MT_DATA);
531 n->m_pkthdr.rcvif = NULL;
532 n->m_pkthdr.len += optlen;
533 m->m_len -= sizeof(struct ip);
534 m->m_data += sizeof(struct ip);
537 m->m_len = optlen + sizeof(struct ip);
538 m->m_data += max_linkhdr;
539 bcopy(ip, mtod(m, void *), sizeof(struct ip));
543 m->m_pkthdr.len += optlen;
544 bcopy(ip, mtod(m, void *), sizeof(struct ip));
546 ip = mtod(m, struct ip *);
547 bcopy(p->ipopt_list, ip + 1, optlen);
548 *phlen = sizeof(struct ip) + optlen;
549 ip->ip_v = IPVERSION;
550 ip->ip_hl = *phlen >> 2;
551 ip->ip_len = htons(ntohs(ip->ip_len) + optlen);
556 * Copy options from ip to jp, omitting those not copied during
560 ip_optcopy(struct ip *ip, struct ip *jp)
563 int opt, optlen, cnt;
565 cp = (u_char *)(ip + 1);
566 dp = (u_char *)(jp + 1);
567 cnt = (ip->ip_hl << 2) - sizeof (struct ip);
568 for (; cnt > 0; cnt -= optlen, cp += optlen) {
570 if (opt == IPOPT_EOL)
572 if (opt == IPOPT_NOP) {
573 /* Preserve for IP mcast tunnel's LSRR alignment. */
579 KASSERT(cnt >= IPOPT_OLEN + sizeof(*cp),
580 ("ip_optcopy: malformed ipv4 option"));
581 optlen = cp[IPOPT_OLEN];
582 KASSERT(optlen >= IPOPT_OLEN + sizeof(*cp) && optlen <= cnt,
583 ("ip_optcopy: malformed ipv4 option"));
585 /* Bogus lengths should have been caught by ip_dooptions. */
588 if (IPOPT_COPIED(opt)) {
589 bcopy(cp, dp, optlen);
593 for (optlen = dp - (u_char *)(jp+1); optlen & 0x3; optlen++)
599 * Set up IP options in pcb for insertion in output packets. Store in mbuf
600 * with pointer in pcbopt, adding pseudo-option with destination address if
604 ip_pcbopts(struct inpcb *inp, int optname, struct mbuf *m)
608 struct mbuf **pcbopt;
611 INP_WLOCK_ASSERT(inp);
613 pcbopt = &inp->inp_options;
615 /* turn off any old options */
617 (void)m_free(*pcbopt);
619 if (m == NULL || m->m_len == 0) {
621 * Only turning off any previous options.
628 if (m->m_len % sizeof(int32_t))
631 * IP first-hop destination address will be stored before actual
632 * options; move other options back and clear it when none present.
634 if (m->m_data + m->m_len + sizeof(struct in_addr) >= &m->m_dat[MLEN])
637 m->m_len += sizeof(struct in_addr);
638 cp = mtod(m, u_char *) + sizeof(struct in_addr);
639 bcopy(mtod(m, void *), cp, (unsigned)cnt);
640 bzero(mtod(m, void *), sizeof(struct in_addr));
642 for (; cnt > 0; cnt -= optlen, cp += optlen) {
643 opt = cp[IPOPT_OPTVAL];
644 if (opt == IPOPT_EOL)
646 if (opt == IPOPT_NOP)
649 if (cnt < IPOPT_OLEN + sizeof(*cp))
651 optlen = cp[IPOPT_OLEN];
652 if (optlen < IPOPT_OLEN + sizeof(*cp) || optlen > cnt)
663 * User process specifies route as:
667 * D must be our final destination (but we can't
668 * check that since we may not have connected yet).
669 * A is first hop destination, which doesn't appear
670 * in actual IP option, but is stored before the
673 /* XXX-BZ PRIV_NETINET_SETHDROPTS? */
674 if (optlen < IPOPT_MINOFF - 1 + sizeof(struct in_addr))
676 m->m_len -= sizeof(struct in_addr);
677 cnt -= sizeof(struct in_addr);
678 optlen -= sizeof(struct in_addr);
679 cp[IPOPT_OLEN] = optlen;
681 * Move first hop before start of options.
683 bcopy((caddr_t)&cp[IPOPT_OFFSET+1], mtod(m, caddr_t),
684 sizeof(struct in_addr));
686 * Then copy rest of options back
687 * to close up the deleted entry.
689 bcopy((&cp[IPOPT_OFFSET+1] + sizeof(struct in_addr)),
691 (unsigned)cnt - (IPOPT_MINOFF - 1));
695 if (m->m_len > MAX_IPOPTLEN + sizeof(struct in_addr))
706 * Check for the presence of the IP Router Alert option [RFC2113]
707 * in the header of an IPv4 datagram.
709 * This call is not intended for use from the forwarding path; it is here
710 * so that protocol domains may check for the presence of the option.
711 * Given how FreeBSD's IPv4 stack is currently structured, the Router Alert
712 * option does not have much relevance to the implementation, though this
713 * may change in future.
714 * Router alert options SHOULD be passed if running in IPSTEALTH mode and
715 * we are not the endpoint.
716 * Length checks on individual options should already have been performed
717 * by ip_dooptions() therefore they are folded under INVARIANTS here.
719 * Return zero if not present or options are invalid, non-zero if present.
722 ip_checkrouteralert(struct mbuf *m)
724 struct ip *ip = mtod(m, struct ip *);
726 int opt, optlen, cnt, found_ra;
729 cp = (u_char *)(ip + 1);
730 cnt = (ip->ip_hl << 2) - sizeof (struct ip);
731 for (; cnt > 0; cnt -= optlen, cp += optlen) {
732 opt = cp[IPOPT_OPTVAL];
733 if (opt == IPOPT_EOL)
735 if (opt == IPOPT_NOP)
739 if (cnt < IPOPT_OLEN + sizeof(*cp))
742 optlen = cp[IPOPT_OLEN];
744 if (optlen < IPOPT_OLEN + sizeof(*cp) || optlen > cnt)
751 if (optlen != IPOPT_OFFSET + sizeof(uint16_t) ||
752 (*((uint16_t *)&cp[IPOPT_OFFSET]) != 0))