2 * Copyright (c) 1982, 1986, 1988, 1993
3 * The Regents of the University of California.
4 * Copyright (c) 2005 Andre Oppermann, Internet Business Solutions AG.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 4. Neither the name of the University nor the names of its contributors
16 * may be used to endorse or promote products derived from this software
17 * without specific prior written permission.
19 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 #include <sys/cdefs.h>
33 __FBSDID("$FreeBSD$");
35 #include "opt_ipstealth.h"
37 #include <sys/param.h>
38 #include <sys/systm.h>
40 #include <sys/domain.h>
41 #include <sys/protosw.h>
42 #include <sys/socket.h>
44 #include <sys/kernel.h>
45 #include <sys/syslog.h>
46 #include <sys/sysctl.h>
49 #include <net/if_types.h>
50 #include <net/if_var.h>
51 #include <net/if_dl.h>
52 #include <net/route.h>
53 #include <net/netisr.h>
56 #include <netinet/in.h>
57 #include <netinet/in_systm.h>
58 #include <netinet/in_var.h>
59 #include <netinet/ip.h>
60 #include <netinet/in_pcb.h>
61 #include <netinet/ip_var.h>
62 #include <netinet/ip_options.h>
63 #include <netinet/ip_icmp.h>
64 #include <machine/in_cksum.h>
66 #include <sys/socketvar.h>
68 static VNET_DEFINE(int, ip_dosourceroute);
69 SYSCTL_INT(_net_inet_ip, IPCTL_SOURCEROUTE, sourceroute,
70 CTLFLAG_VNET | CTLFLAG_RW, &VNET_NAME(ip_dosourceroute), 0,
71 "Enable forwarding source routed IP packets");
72 #define V_ip_dosourceroute VNET(ip_dosourceroute)
74 static VNET_DEFINE(int, ip_acceptsourceroute);
75 SYSCTL_INT(_net_inet_ip, IPCTL_ACCEPTSOURCEROUTE, accept_sourceroute,
76 CTLFLAG_VNET | CTLFLAG_RW, &VNET_NAME(ip_acceptsourceroute), 0,
77 "Enable accepting source routed IP packets");
78 #define V_ip_acceptsourceroute VNET(ip_acceptsourceroute)
80 VNET_DEFINE(int, ip_doopts) = 1; /* 0 = ignore, 1 = process, 2 = reject */
81 SYSCTL_INT(_net_inet_ip, OID_AUTO, process_options, CTLFLAG_VNET | CTLFLAG_RW,
82 &VNET_NAME(ip_doopts), 0, "Enable IP options processing ([LS]SRR, RR, TS)");
84 static void save_rte(struct mbuf *m, u_char *, struct in_addr);
87 * Do option processing on a datagram, possibly discarding it if bad options
88 * are encountered, or forwarding it if source-routed.
90 * The pass argument is used when operating in the IPSTEALTH mode to tell
91 * what options to process: [LS]SRR (pass 0) or the others (pass 1). The
92 * reason for as many as two passes is that when doing IPSTEALTH, non-routing
93 * options should be processed only if the packet is for us.
95 * Returns 1 if packet has been forwarded/freed, 0 if the packet should be
99 ip_dooptions(struct mbuf *m, int pass)
101 struct ip *ip = mtod(m, struct ip *);
103 struct in_ifaddr *ia;
104 int opt, optlen, cnt, off, code, type = ICMP_PARAMPROB, forward = 0;
105 struct in_addr *sin, dst;
107 struct sockaddr_in ipaddr = { sizeof(ipaddr), AF_INET };
109 /* Ignore or reject packets with IP options. */
110 if (V_ip_doopts == 0)
112 else if (V_ip_doopts == 2) {
114 code = ICMP_UNREACH_FILTER_PROHIB;
119 cp = (u_char *)(ip + 1);
120 cnt = (ip->ip_hl << 2) - sizeof (struct ip);
121 for (; cnt > 0; cnt -= optlen, cp += optlen) {
122 opt = cp[IPOPT_OPTVAL];
123 if (opt == IPOPT_EOL)
125 if (opt == IPOPT_NOP)
128 if (cnt < IPOPT_OLEN + sizeof(*cp)) {
129 code = &cp[IPOPT_OLEN] - (u_char *)ip;
132 optlen = cp[IPOPT_OLEN];
133 if (optlen < IPOPT_OLEN + sizeof(*cp) || optlen > cnt) {
134 code = &cp[IPOPT_OLEN] - (u_char *)ip;
144 * Source routing with record. Find interface with current
145 * destination address. If none on this machine then drop if
146 * strictly routed, or do nothing if loosely routed. Record
147 * interface address and bring up next address component. If
148 * strictly routed make sure next address is on directly
154 if (V_ipstealth && pass > 0)
157 if (optlen < IPOPT_OFFSET + sizeof(*cp)) {
158 code = &cp[IPOPT_OLEN] - (u_char *)ip;
161 if ((off = cp[IPOPT_OFFSET]) < IPOPT_MINOFF) {
162 code = &cp[IPOPT_OFFSET] - (u_char *)ip;
165 ipaddr.sin_addr = ip->ip_dst;
166 if (ifa_ifwithaddr_check((struct sockaddr *)&ipaddr)
168 if (opt == IPOPT_SSRR) {
170 code = ICMP_UNREACH_SRCFAIL;
173 if (!V_ip_dosourceroute)
174 goto nosourcerouting;
176 * Loose routing, and not at next destination
177 * yet; nothing to do except forward.
181 off--; /* 0 origin */
182 if (off > optlen - (int)sizeof(struct in_addr)) {
184 * End of source route. Should be for us.
186 if (!V_ip_acceptsourceroute)
187 goto nosourcerouting;
188 save_rte(m, cp, ip->ip_src);
195 if (!V_ip_dosourceroute) {
196 if (V_ipforwarding) {
197 char buf[16]; /* aaa.bbb.ccc.ddd\0 */
199 * Acting as a router, so generate
203 strcpy(buf, inet_ntoa(ip->ip_dst));
205 "attempted source route from %s to %s\n",
206 inet_ntoa(ip->ip_src), buf);
208 code = ICMP_UNREACH_SRCFAIL;
212 * Not acting as a router, so
218 IPSTAT_INC(ips_cantforward);
225 * locate outgoing interface
227 (void)memcpy(&ipaddr.sin_addr, cp + off,
228 sizeof(ipaddr.sin_addr));
230 if (opt == IPOPT_SSRR) {
231 #define INA struct in_ifaddr *
232 #define SA struct sockaddr *
233 if ((ia = (INA)ifa_ifwithdstaddr((SA)&ipaddr)) == NULL)
234 ia = (INA)ifa_ifwithnet((SA)&ipaddr, 0);
236 /* XXX MRT 0 for routing */
237 ia = ip_rtaddr(ipaddr.sin_addr, M_GETFIB(m));
240 code = ICMP_UNREACH_SRCFAIL;
243 ip->ip_dst = ipaddr.sin_addr;
244 (void)memcpy(cp + off, &(IA_SIN(ia)->sin_addr),
245 sizeof(struct in_addr));
246 ifa_free(&ia->ia_ifa);
247 cp[IPOPT_OFFSET] += sizeof(struct in_addr);
249 * Let ip_intr's mcast routing check handle mcast pkts
251 forward = !IN_MULTICAST(ntohl(ip->ip_dst.s_addr));
256 if (V_ipstealth && pass == 0)
259 if (optlen < IPOPT_OFFSET + sizeof(*cp)) {
260 code = &cp[IPOPT_OFFSET] - (u_char *)ip;
263 if ((off = cp[IPOPT_OFFSET]) < IPOPT_MINOFF) {
264 code = &cp[IPOPT_OFFSET] - (u_char *)ip;
268 * If no space remains, ignore.
270 off--; /* 0 origin */
271 if (off > optlen - (int)sizeof(struct in_addr))
273 (void)memcpy(&ipaddr.sin_addr, &ip->ip_dst,
274 sizeof(ipaddr.sin_addr));
276 * Locate outgoing interface; if we're the
277 * destination, use the incoming interface (should be
280 if ((ia = (INA)ifa_ifwithaddr((SA)&ipaddr)) == NULL &&
281 (ia = ip_rtaddr(ipaddr.sin_addr, M_GETFIB(m))) == NULL) {
283 code = ICMP_UNREACH_HOST;
286 (void)memcpy(cp + off, &(IA_SIN(ia)->sin_addr),
287 sizeof(struct in_addr));
288 ifa_free(&ia->ia_ifa);
289 cp[IPOPT_OFFSET] += sizeof(struct in_addr);
294 if (V_ipstealth && pass == 0)
297 code = cp - (u_char *)ip;
298 if (optlen < 4 || optlen > 40) {
299 code = &cp[IPOPT_OLEN] - (u_char *)ip;
302 if ((off = cp[IPOPT_OFFSET]) < 5) {
303 code = &cp[IPOPT_OLEN] - (u_char *)ip;
306 if (off > optlen - (int)sizeof(int32_t)) {
307 cp[IPOPT_OFFSET + 1] += (1 << 4);
308 if ((cp[IPOPT_OFFSET + 1] & 0xf0) == 0) {
309 code = &cp[IPOPT_OFFSET] - (u_char *)ip;
314 off--; /* 0 origin */
315 sin = (struct in_addr *)(cp + off);
316 switch (cp[IPOPT_OFFSET + 1] & 0x0f) {
318 case IPOPT_TS_TSONLY:
321 case IPOPT_TS_TSANDADDR:
322 if (off + sizeof(uint32_t) +
323 sizeof(struct in_addr) > optlen) {
324 code = &cp[IPOPT_OFFSET] - (u_char *)ip;
327 ipaddr.sin_addr = dst;
328 ia = (INA)ifaof_ifpforaddr((SA)&ipaddr,
332 (void)memcpy(sin, &IA_SIN(ia)->sin_addr,
333 sizeof(struct in_addr));
334 ifa_free(&ia->ia_ifa);
335 cp[IPOPT_OFFSET] += sizeof(struct in_addr);
336 off += sizeof(struct in_addr);
339 case IPOPT_TS_PRESPEC:
340 if (off + sizeof(uint32_t) +
341 sizeof(struct in_addr) > optlen) {
342 code = &cp[IPOPT_OFFSET] - (u_char *)ip;
345 (void)memcpy(&ipaddr.sin_addr, sin,
346 sizeof(struct in_addr));
347 if (ifa_ifwithaddr_check((SA)&ipaddr) == 0)
349 cp[IPOPT_OFFSET] += sizeof(struct in_addr);
350 off += sizeof(struct in_addr);
354 code = &cp[IPOPT_OFFSET + 1] - (u_char *)ip;
358 (void)memcpy(cp + off, &ntime, sizeof(uint32_t));
359 cp[IPOPT_OFFSET] += sizeof(uint32_t);
362 if (forward && V_ipforwarding) {
368 icmp_error(m, type, code, 0, 0);
369 IPSTAT_INC(ips_badoptions);
374 * Save incoming source route for use in replies, to be picked up later by
375 * ip_srcroute if the receiver is interested.
378 save_rte(struct mbuf *m, u_char *option, struct in_addr dst)
381 struct ipopt_tag *opts;
383 opts = (struct ipopt_tag *)m_tag_get(PACKET_TAG_IPOPTIONS,
384 sizeof(struct ipopt_tag), M_NOWAIT);
388 olen = option[IPOPT_OLEN];
389 if (olen > sizeof(opts->ip_srcrt) - (1 + sizeof(dst))) {
390 m_tag_free((struct m_tag *)opts);
393 bcopy(option, opts->ip_srcrt.srcopt, olen);
394 opts->ip_nhops = (olen - IPOPT_OFFSET - 1) / sizeof(struct in_addr);
395 opts->ip_srcrt.dst = dst;
396 m_tag_prepend(m, (struct m_tag *)opts);
400 * Retrieve incoming source route for use in replies, in the same form used
401 * by setsockopt. The first hop is placed before the options, will be
405 ip_srcroute(struct mbuf *m0)
407 struct in_addr *p, *q;
409 struct ipopt_tag *opts;
411 opts = (struct ipopt_tag *)m_tag_find(m0, PACKET_TAG_IPOPTIONS, NULL);
415 if (opts->ip_nhops == 0)
417 m = m_get(M_NOWAIT, MT_DATA);
421 #define OPTSIZ (sizeof(opts->ip_srcrt.nop) + sizeof(opts->ip_srcrt.srcopt))
423 /* length is (nhops+1)*sizeof(addr) + sizeof(nop + srcrt header) */
424 m->m_len = opts->ip_nhops * sizeof(struct in_addr) +
425 sizeof(struct in_addr) + OPTSIZ;
428 * First, save first hop for return route.
430 p = &(opts->ip_srcrt.route[opts->ip_nhops - 1]);
431 *(mtod(m, struct in_addr *)) = *p--;
434 * Copy option fields and padding (nop) to mbuf.
436 opts->ip_srcrt.nop = IPOPT_NOP;
437 opts->ip_srcrt.srcopt[IPOPT_OFFSET] = IPOPT_MINOFF;
438 (void)memcpy(mtod(m, caddr_t) + sizeof(struct in_addr),
439 &(opts->ip_srcrt.nop), OPTSIZ);
440 q = (struct in_addr *)(mtod(m, caddr_t) +
441 sizeof(struct in_addr) + OPTSIZ);
444 * Record return path as an IP source route, reversing the path
445 * (pointers are now aligned).
447 while (p >= opts->ip_srcrt.route) {
451 * Last hop goes to final destination.
453 *q = opts->ip_srcrt.dst;
454 m_tag_delete(m0, (struct m_tag *)opts);
459 * Strip out IP options, at higher level protocol in the kernel.
462 ip_stripoptions(struct mbuf *m)
464 struct ip *ip = mtod(m, struct ip *);
467 olen = (ip->ip_hl << 2) - sizeof(struct ip);
469 if (m->m_flags & M_PKTHDR)
470 m->m_pkthdr.len -= olen;
471 ip->ip_len = htons(ntohs(ip->ip_len) - olen);
472 ip->ip_hl = sizeof(struct ip) >> 2;
474 bcopy((char *)ip + sizeof(struct ip) + olen, (ip + 1),
475 (size_t )(m->m_len - sizeof(struct ip)));
479 * Insert IP options into preformed packet. Adjust IP destination as
480 * required for IP source routing, as indicated by a non-zero in_addr at the
481 * start of the options.
483 * XXX This routine assumes that the packet has no options in place.
486 ip_insertoptions(struct mbuf *m, struct mbuf *opt, int *phlen)
488 struct ipoption *p = mtod(opt, struct ipoption *);
490 struct ip *ip = mtod(m, struct ip *);
493 optlen = opt->m_len - sizeof(p->ipopt_dst);
494 if (optlen + ntohs(ip->ip_len) > IP_MAXPACKET) {
496 return (m); /* XXX should fail */
498 if (p->ipopt_dst.s_addr)
499 ip->ip_dst = p->ipopt_dst;
500 if (m->m_flags & M_EXT || m->m_data - optlen < m->m_pktdat) {
501 n = m_gethdr(M_NOWAIT, MT_DATA);
507 n->m_pkthdr.rcvif = NULL;
508 n->m_pkthdr.len += optlen;
509 m->m_len -= sizeof(struct ip);
510 m->m_data += sizeof(struct ip);
513 m->m_len = optlen + sizeof(struct ip);
514 m->m_data += max_linkhdr;
515 bcopy(ip, mtod(m, void *), sizeof(struct ip));
519 m->m_pkthdr.len += optlen;
520 bcopy(ip, mtod(m, void *), sizeof(struct ip));
522 ip = mtod(m, struct ip *);
523 bcopy(p->ipopt_list, ip + 1, optlen);
524 *phlen = sizeof(struct ip) + optlen;
525 ip->ip_v = IPVERSION;
526 ip->ip_hl = *phlen >> 2;
527 ip->ip_len = htons(ntohs(ip->ip_len) + optlen);
532 * Copy options from ip to jp, omitting those not copied during
536 ip_optcopy(struct ip *ip, struct ip *jp)
539 int opt, optlen, cnt;
541 cp = (u_char *)(ip + 1);
542 dp = (u_char *)(jp + 1);
543 cnt = (ip->ip_hl << 2) - sizeof (struct ip);
544 for (; cnt > 0; cnt -= optlen, cp += optlen) {
546 if (opt == IPOPT_EOL)
548 if (opt == IPOPT_NOP) {
549 /* Preserve for IP mcast tunnel's LSRR alignment. */
555 KASSERT(cnt >= IPOPT_OLEN + sizeof(*cp),
556 ("ip_optcopy: malformed ipv4 option"));
557 optlen = cp[IPOPT_OLEN];
558 KASSERT(optlen >= IPOPT_OLEN + sizeof(*cp) && optlen <= cnt,
559 ("ip_optcopy: malformed ipv4 option"));
561 /* Bogus lengths should have been caught by ip_dooptions. */
564 if (IPOPT_COPIED(opt)) {
565 bcopy(cp, dp, optlen);
569 for (optlen = dp - (u_char *)(jp+1); optlen & 0x3; optlen++)
575 * Set up IP options in pcb for insertion in output packets. Store in mbuf
576 * with pointer in pcbopt, adding pseudo-option with destination address if
580 ip_pcbopts(struct inpcb *inp, int optname, struct mbuf *m)
584 struct mbuf **pcbopt;
587 INP_WLOCK_ASSERT(inp);
589 pcbopt = &inp->inp_options;
591 /* turn off any old options */
593 (void)m_free(*pcbopt);
595 if (m == NULL || m->m_len == 0) {
597 * Only turning off any previous options.
604 if (m->m_len % sizeof(int32_t))
607 * IP first-hop destination address will be stored before actual
608 * options; move other options back and clear it when none present.
610 if (m->m_data + m->m_len + sizeof(struct in_addr) >= &m->m_dat[MLEN])
613 m->m_len += sizeof(struct in_addr);
614 cp = mtod(m, u_char *) + sizeof(struct in_addr);
615 bcopy(mtod(m, void *), cp, (unsigned)cnt);
616 bzero(mtod(m, void *), sizeof(struct in_addr));
618 for (; cnt > 0; cnt -= optlen, cp += optlen) {
619 opt = cp[IPOPT_OPTVAL];
620 if (opt == IPOPT_EOL)
622 if (opt == IPOPT_NOP)
625 if (cnt < IPOPT_OLEN + sizeof(*cp))
627 optlen = cp[IPOPT_OLEN];
628 if (optlen < IPOPT_OLEN + sizeof(*cp) || optlen > cnt)
639 * User process specifies route as:
643 * D must be our final destination (but we can't
644 * check that since we may not have connected yet).
645 * A is first hop destination, which doesn't appear
646 * in actual IP option, but is stored before the
649 /* XXX-BZ PRIV_NETINET_SETHDROPTS? */
650 if (optlen < IPOPT_MINOFF - 1 + sizeof(struct in_addr))
652 m->m_len -= sizeof(struct in_addr);
653 cnt -= sizeof(struct in_addr);
654 optlen -= sizeof(struct in_addr);
655 cp[IPOPT_OLEN] = optlen;
657 * Move first hop before start of options.
659 bcopy((caddr_t)&cp[IPOPT_OFFSET+1], mtod(m, caddr_t),
660 sizeof(struct in_addr));
662 * Then copy rest of options back
663 * to close up the deleted entry.
665 bcopy((&cp[IPOPT_OFFSET+1] + sizeof(struct in_addr)),
667 (unsigned)cnt - (IPOPT_MINOFF - 1));
671 if (m->m_len > MAX_IPOPTLEN + sizeof(struct in_addr))
682 * Check for the presence of the IP Router Alert option [RFC2113]
683 * in the header of an IPv4 datagram.
685 * This call is not intended for use from the forwarding path; it is here
686 * so that protocol domains may check for the presence of the option.
687 * Given how FreeBSD's IPv4 stack is currently structured, the Router Alert
688 * option does not have much relevance to the implementation, though this
689 * may change in future.
690 * Router alert options SHOULD be passed if running in IPSTEALTH mode and
691 * we are not the endpoint.
692 * Length checks on individual options should already have been peformed
693 * by ip_dooptions() therefore they are folded under INVARIANTS here.
695 * Return zero if not present or options are invalid, non-zero if present.
698 ip_checkrouteralert(struct mbuf *m)
700 struct ip *ip = mtod(m, struct ip *);
702 int opt, optlen, cnt, found_ra;
705 cp = (u_char *)(ip + 1);
706 cnt = (ip->ip_hl << 2) - sizeof (struct ip);
707 for (; cnt > 0; cnt -= optlen, cp += optlen) {
708 opt = cp[IPOPT_OPTVAL];
709 if (opt == IPOPT_EOL)
711 if (opt == IPOPT_NOP)
715 if (cnt < IPOPT_OLEN + sizeof(*cp))
718 optlen = cp[IPOPT_OLEN];
720 if (optlen < IPOPT_OLEN + sizeof(*cp) || optlen > cnt)
727 if (optlen != IPOPT_OFFSET + sizeof(uint16_t) ||
728 (*((uint16_t *)&cp[IPOPT_OFFSET]) != 0))