ip_options.c revision 213832
1171169Smlaier/* 2171169Smlaier * Copyright (c) 1982, 1986, 1988, 1993 3171169Smlaier * The Regents of the University of California. 4171169Smlaier * Copyright (c) 2005 Andre Oppermann, Internet Business Solutions AG. 5171169Smlaier * All rights reserved. 6171169Smlaier * 7171169Smlaier * Redistribution and use in source and binary forms, with or without 8171169Smlaier * modification, are permitted provided that the following conditions 9171169Smlaier * are met: 10171169Smlaier * 1. Redistributions of source code must retain the above copyright 11171169Smlaier * notice, this list of conditions and the following disclaimer. 12171169Smlaier * 2. Redistributions in binary form must reproduce the above copyright 13171169Smlaier * notice, this list of conditions and the following disclaimer in the 14171169Smlaier * documentation and/or other materials provided with the distribution. 15171169Smlaier * 4. Neither the name of the University nor the names of its contributors 16171169Smlaier * may be used to endorse or promote products derived from this software 17171169Smlaier * without specific prior written permission. 18171169Smlaier * 19171169Smlaier * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 20171169Smlaier * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 21171169Smlaier * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 22171169Smlaier * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 23171169Smlaier * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 24171169Smlaier * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 25171169Smlaier * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 26171169Smlaier * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 27171169Smlaier * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 28171169Smlaier * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 29171169Smlaier * SUCH DAMAGE. 30171169Smlaier */ 31171169Smlaier 32171169Smlaier#include <sys/cdefs.h> 33171169Smlaier__FBSDID("$FreeBSD: head/sys/netinet/ip_options.c 213832 2010-10-14 12:32:49Z bz $"); 34171169Smlaier 35171169Smlaier#include "opt_ipstealth.h" 36171169Smlaier 37171169Smlaier#include <sys/param.h> 38171169Smlaier#include <sys/systm.h> 39171169Smlaier#include <sys/mbuf.h> 40171169Smlaier#include <sys/domain.h> 41171169Smlaier#include <sys/protosw.h> 42171169Smlaier#include <sys/socket.h> 43171169Smlaier#include <sys/time.h> 44171169Smlaier#include <sys/kernel.h> 45171169Smlaier#include <sys/syslog.h> 46171169Smlaier#include <sys/sysctl.h> 47171169Smlaier 48171169Smlaier#include <net/if.h> 49171169Smlaier#include <net/if_types.h> 50171169Smlaier#include <net/if_var.h> 51171169Smlaier#include <net/if_dl.h> 52171169Smlaier#include <net/route.h> 53171169Smlaier#include <net/netisr.h> 54171169Smlaier#include <net/vnet.h> 55171169Smlaier 56171169Smlaier#include <netinet/in.h> 57171169Smlaier#include <netinet/in_systm.h> 58171169Smlaier#include <netinet/in_var.h> 59171169Smlaier#include <netinet/ip.h> 60171169Smlaier#include <netinet/in_pcb.h> 61171169Smlaier#include <netinet/ip_var.h> 62171169Smlaier#include <netinet/ip_options.h> 63171169Smlaier#include <netinet/ip_icmp.h> 64171169Smlaier#include <machine/in_cksum.h> 65171169Smlaier 66171169Smlaier#include <sys/socketvar.h> 67171169Smlaier 68171169Smlaierstatic int ip_dosourceroute = 0; 69171169SmlaierSYSCTL_INT(_net_inet_ip, IPCTL_SOURCEROUTE, sourceroute, CTLFLAG_RW, 70171169Smlaier &ip_dosourceroute, 0, "Enable forwarding source routed IP packets"); 71171169Smlaier 72171169Smlaierstatic int ip_acceptsourceroute = 0; 73171169SmlaierSYSCTL_INT(_net_inet_ip, IPCTL_ACCEPTSOURCEROUTE, accept_sourceroute, 74171169Smlaier CTLFLAG_RW, &ip_acceptsourceroute, 0, 75171169Smlaier "Enable accepting source routed IP packets"); 76171169Smlaier 77171169Smlaierint ip_doopts = 1; /* 0 = ignore, 1 = process, 2 = reject */ 78171169SmlaierSYSCTL_INT(_net_inet_ip, OID_AUTO, process_options, CTLFLAG_RW, 79171169Smlaier &ip_doopts, 0, "Enable IP options processing ([LS]SRR, RR, TS)"); 80171169Smlaier 81171169Smlaierstatic void save_rte(struct mbuf *m, u_char *, struct in_addr); 82171169Smlaier 83171169Smlaier/* 84171169Smlaier * Do option processing on a datagram, possibly discarding it if bad options 85171169Smlaier * are encountered, or forwarding it if source-routed. 86171169Smlaier * 87171169Smlaier * The pass argument is used when operating in the IPSTEALTH mode to tell 88171169Smlaier * what options to process: [LS]SRR (pass 0) or the others (pass 1). The 89171169Smlaier * reason for as many as two passes is that when doing IPSTEALTH, non-routing 90171169Smlaier * options should be processed only if the packet is for us. 91171169Smlaier * 92171169Smlaier * Returns 1 if packet has been forwarded/freed, 0 if the packet should be 93171169Smlaier * processed further. 94171169Smlaier */ 95171169Smlaierint 96171169Smlaierip_dooptions(struct mbuf *m, int pass) 97171169Smlaier{ 98171169Smlaier struct ip *ip = mtod(m, struct ip *); 99171169Smlaier u_char *cp; 100171169Smlaier struct in_ifaddr *ia; 101171169Smlaier int opt, optlen, cnt, off, code, type = ICMP_PARAMPROB, forward = 0; 102171169Smlaier struct in_addr *sin, dst; 103171169Smlaier uint32_t ntime; 104171169Smlaier struct sockaddr_in ipaddr = { sizeof(ipaddr), AF_INET }; 105171169Smlaier 106171169Smlaier /* Ignore or reject packets with IP options. */ 107171169Smlaier if (ip_doopts == 0) 108171169Smlaier return 0; 109171169Smlaier else if (ip_doopts == 2) { 110171169Smlaier type = ICMP_UNREACH; 111171169Smlaier code = ICMP_UNREACH_FILTER_PROHIB; 112171169Smlaier goto bad; 113171169Smlaier } 114171169Smlaier 115171169Smlaier dst = ip->ip_dst; 116171169Smlaier cp = (u_char *)(ip + 1); 117171169Smlaier cnt = (ip->ip_hl << 2) - sizeof (struct ip); 118171169Smlaier for (; cnt > 0; cnt -= optlen, cp += optlen) { 119171169Smlaier opt = cp[IPOPT_OPTVAL]; 120171169Smlaier if (opt == IPOPT_EOL) 121171169Smlaier break; 122171169Smlaier if (opt == IPOPT_NOP) 123171169Smlaier optlen = 1; 124171169Smlaier else { 125171169Smlaier if (cnt < IPOPT_OLEN + sizeof(*cp)) { 126171169Smlaier code = &cp[IPOPT_OLEN] - (u_char *)ip; 127171169Smlaier goto bad; 128171169Smlaier } 129171169Smlaier optlen = cp[IPOPT_OLEN]; 130171169Smlaier if (optlen < IPOPT_OLEN + sizeof(*cp) || optlen > cnt) { 131171169Smlaier code = &cp[IPOPT_OLEN] - (u_char *)ip; 132171169Smlaier goto bad; 133171169Smlaier } 134171169Smlaier } 135171169Smlaier switch (opt) { 136171169Smlaier 137171169Smlaier default: 138171169Smlaier break; 139171169Smlaier 140171169Smlaier /* 141171169Smlaier * Source routing with record. Find interface with current 142171169Smlaier * destination address. If none on this machine then drop if 143171169Smlaier * strictly routed, or do nothing if loosely routed. Record 144171169Smlaier * interface address and bring up next address component. If 145171169Smlaier * strictly routed make sure next address is on directly 146171169Smlaier * accessible net. 147171169Smlaier */ 148171169Smlaier case IPOPT_LSRR: 149171169Smlaier case IPOPT_SSRR: 150171169Smlaier#ifdef IPSTEALTH 151171169Smlaier if (V_ipstealth && pass > 0) 152171169Smlaier break; 153171169Smlaier#endif 154171169Smlaier if (optlen < IPOPT_OFFSET + sizeof(*cp)) { 155171169Smlaier code = &cp[IPOPT_OLEN] - (u_char *)ip; 156171169Smlaier goto bad; 157171169Smlaier } 158171169Smlaier if ((off = cp[IPOPT_OFFSET]) < IPOPT_MINOFF) { 159171169Smlaier code = &cp[IPOPT_OFFSET] - (u_char *)ip; 160171169Smlaier goto bad; 161171169Smlaier } 162171169Smlaier ipaddr.sin_addr = ip->ip_dst; 163171169Smlaier if (ifa_ifwithaddr_check((struct sockaddr *)&ipaddr) 164171169Smlaier == 0) { 165171169Smlaier if (opt == IPOPT_SSRR) { 166171169Smlaier type = ICMP_UNREACH; 167171169Smlaier code = ICMP_UNREACH_SRCFAIL; 168171169Smlaier goto bad; 169171169Smlaier } 170171169Smlaier if (!ip_dosourceroute) 171171169Smlaier goto nosourcerouting; 172171169Smlaier /* 173171169Smlaier * Loose routing, and not at next destination 174171169Smlaier * yet; nothing to do except forward. 175171169Smlaier */ 176171169Smlaier break; 177171169Smlaier } 178171169Smlaier off--; /* 0 origin */ 179171169Smlaier if (off > optlen - (int)sizeof(struct in_addr)) { 180171169Smlaier /* 181171169Smlaier * End of source route. Should be for us. 182171169Smlaier */ 183171169Smlaier if (!ip_acceptsourceroute) 184171169Smlaier goto nosourcerouting; 185171169Smlaier save_rte(m, cp, ip->ip_src); 186171169Smlaier break; 187171169Smlaier } 188171169Smlaier#ifdef IPSTEALTH 189171169Smlaier if (V_ipstealth) 190171169Smlaier goto dropit; 191171169Smlaier#endif 192171169Smlaier if (!ip_dosourceroute) { 193171169Smlaier if (V_ipforwarding) { 194171169Smlaier char buf[16]; /* aaa.bbb.ccc.ddd\0 */ 195171169Smlaier /* 196171169Smlaier * Acting as a router, so generate 197171169Smlaier * ICMP 198171169Smlaier */ 199171169Smlaiernosourcerouting: 200171169Smlaier strcpy(buf, inet_ntoa(ip->ip_dst)); 201171169Smlaier log(LOG_WARNING, 202171169Smlaier "attempted source route from %s to %s\n", 203171169Smlaier inet_ntoa(ip->ip_src), buf); 204171169Smlaier type = ICMP_UNREACH; 205171169Smlaier code = ICMP_UNREACH_SRCFAIL; 206171169Smlaier goto bad; 207171169Smlaier } else { 208171169Smlaier /* 209171169Smlaier * Not acting as a router, so 210171169Smlaier * silently drop. 211171169Smlaier */ 212171169Smlaier#ifdef IPSTEALTH 213171169Smlaierdropit: 214171169Smlaier#endif 215171169Smlaier IPSTAT_INC(ips_cantforward); 216171169Smlaier m_freem(m); 217171169Smlaier return (1); 218171169Smlaier } 219171169Smlaier } 220171169Smlaier 221171169Smlaier /* 222171169Smlaier * locate outgoing interface 223171169Smlaier */ 224171169Smlaier (void)memcpy(&ipaddr.sin_addr, cp + off, 225171169Smlaier sizeof(ipaddr.sin_addr)); 226171169Smlaier 227171169Smlaier if (opt == IPOPT_SSRR) { 228171169Smlaier#define INA struct in_ifaddr * 229171169Smlaier#define SA struct sockaddr * 230171169Smlaier if ((ia = (INA)ifa_ifwithdstaddr((SA)&ipaddr)) == NULL) 231171169Smlaier ia = (INA)ifa_ifwithnet((SA)&ipaddr, 0); 232171169Smlaier } else 233171169Smlaier/* XXX MRT 0 for routing */ 234171169Smlaier ia = ip_rtaddr(ipaddr.sin_addr, M_GETFIB(m)); 235171169Smlaier if (ia == NULL) { 236171169Smlaier type = ICMP_UNREACH; 237171169Smlaier code = ICMP_UNREACH_SRCFAIL; 238171169Smlaier goto bad; 239171169Smlaier } 240171169Smlaier ip->ip_dst = ipaddr.sin_addr; 241171169Smlaier (void)memcpy(cp + off, &(IA_SIN(ia)->sin_addr), 242171169Smlaier sizeof(struct in_addr)); 243171169Smlaier ifa_free(&ia->ia_ifa); 244171169Smlaier cp[IPOPT_OFFSET] += sizeof(struct in_addr); 245171169Smlaier /* 246171169Smlaier * Let ip_intr's mcast routing check handle mcast pkts 247171169Smlaier */ 248171169Smlaier forward = !IN_MULTICAST(ntohl(ip->ip_dst.s_addr)); 249171169Smlaier break; 250171169Smlaier 251171169Smlaier case IPOPT_RR: 252171169Smlaier#ifdef IPSTEALTH 253171169Smlaier if (V_ipstealth && pass == 0) 254171169Smlaier break; 255171169Smlaier#endif 256171169Smlaier if (optlen < IPOPT_OFFSET + sizeof(*cp)) { 257171169Smlaier code = &cp[IPOPT_OFFSET] - (u_char *)ip; 258171169Smlaier goto bad; 259171169Smlaier } 260171169Smlaier if ((off = cp[IPOPT_OFFSET]) < IPOPT_MINOFF) { 261171169Smlaier code = &cp[IPOPT_OFFSET] - (u_char *)ip; 262171169Smlaier goto bad; 263171169Smlaier } 264171169Smlaier /* 265171169Smlaier * If no space remains, ignore. 266171169Smlaier */ 267171169Smlaier off--; /* 0 origin */ 268171169Smlaier if (off > optlen - (int)sizeof(struct in_addr)) 269171169Smlaier break; 270171169Smlaier (void)memcpy(&ipaddr.sin_addr, &ip->ip_dst, 271171169Smlaier sizeof(ipaddr.sin_addr)); 272171169Smlaier /* 273171169Smlaier * Locate outgoing interface; if we're the 274171169Smlaier * destination, use the incoming interface (should be 275171169Smlaier * same). 276171169Smlaier */ 277171169Smlaier if ((ia = (INA)ifa_ifwithaddr((SA)&ipaddr)) == NULL && 278171169Smlaier (ia = ip_rtaddr(ipaddr.sin_addr, M_GETFIB(m))) == NULL) { 279171169Smlaier type = ICMP_UNREACH; 280171169Smlaier code = ICMP_UNREACH_HOST; 281171169Smlaier goto bad; 282171169Smlaier } 283171169Smlaier (void)memcpy(cp + off, &(IA_SIN(ia)->sin_addr), 284171169Smlaier sizeof(struct in_addr)); 285171169Smlaier ifa_free(&ia->ia_ifa); 286171169Smlaier cp[IPOPT_OFFSET] += sizeof(struct in_addr); 287171169Smlaier break; 288171169Smlaier 289171169Smlaier case IPOPT_TS: 290171169Smlaier#ifdef IPSTEALTH 291171169Smlaier if (V_ipstealth && pass == 0) 292171169Smlaier break; 293171169Smlaier#endif 294171169Smlaier code = cp - (u_char *)ip; 295171169Smlaier if (optlen < 4 || optlen > 40) { 296171169Smlaier code = &cp[IPOPT_OLEN] - (u_char *)ip; 297171169Smlaier goto bad; 298171169Smlaier } 299171169Smlaier if ((off = cp[IPOPT_OFFSET]) < 5) { 300171169Smlaier code = &cp[IPOPT_OLEN] - (u_char *)ip; 301171169Smlaier goto bad; 302171169Smlaier } 303171169Smlaier if (off > optlen - (int)sizeof(int32_t)) { 304171169Smlaier cp[IPOPT_OFFSET + 1] += (1 << 4); 305171169Smlaier if ((cp[IPOPT_OFFSET + 1] & 0xf0) == 0) { 306171169Smlaier code = &cp[IPOPT_OFFSET] - (u_char *)ip; 307171169Smlaier goto bad; 308171169Smlaier } 309171169Smlaier break; 310171169Smlaier } 311171169Smlaier off--; /* 0 origin */ 312171169Smlaier sin = (struct in_addr *)(cp + off); 313171169Smlaier switch (cp[IPOPT_OFFSET + 1] & 0x0f) { 314171169Smlaier 315171169Smlaier case IPOPT_TS_TSONLY: 316171169Smlaier break; 317171169Smlaier 318171169Smlaier case IPOPT_TS_TSANDADDR: 319171169Smlaier if (off + sizeof(uint32_t) + 320171169Smlaier sizeof(struct in_addr) > optlen) { 321171169Smlaier code = &cp[IPOPT_OFFSET] - (u_char *)ip; 322171169Smlaier goto bad; 323171169Smlaier } 324171169Smlaier ipaddr.sin_addr = dst; 325171169Smlaier ia = (INA)ifaof_ifpforaddr((SA)&ipaddr, 326171169Smlaier m->m_pkthdr.rcvif); 327171169Smlaier if (ia == NULL) 328171169Smlaier continue; 329171169Smlaier (void)memcpy(sin, &IA_SIN(ia)->sin_addr, 330171169Smlaier sizeof(struct in_addr)); 331171169Smlaier ifa_free(&ia->ia_ifa); 332171169Smlaier cp[IPOPT_OFFSET] += sizeof(struct in_addr); 333171169Smlaier off += sizeof(struct in_addr); 334171169Smlaier break; 335171169Smlaier 336171169Smlaier case IPOPT_TS_PRESPEC: 337171169Smlaier if (off + sizeof(uint32_t) + 338171169Smlaier sizeof(struct in_addr) > optlen) { 339171169Smlaier code = &cp[IPOPT_OFFSET] - (u_char *)ip; 340171169Smlaier goto bad; 341171169Smlaier } 342171169Smlaier (void)memcpy(&ipaddr.sin_addr, sin, 343171169Smlaier sizeof(struct in_addr)); 344171169Smlaier if (ifa_ifwithaddr_check((SA)&ipaddr) == 0) 345171169Smlaier continue; 346171169Smlaier cp[IPOPT_OFFSET] += sizeof(struct in_addr); 347171169Smlaier off += sizeof(struct in_addr); 348171169Smlaier break; 349171169Smlaier 350171169Smlaier default: 351171169Smlaier code = &cp[IPOPT_OFFSET + 1] - (u_char *)ip; 352171169Smlaier goto bad; 353171169Smlaier } 354171169Smlaier ntime = iptime(); 355171169Smlaier (void)memcpy(cp + off, &ntime, sizeof(uint32_t)); 356171169Smlaier cp[IPOPT_OFFSET] += sizeof(uint32_t); 357171169Smlaier } 358171169Smlaier } 359171169Smlaier if (forward && V_ipforwarding) { 360171169Smlaier ip_forward(m, 1); 361171169Smlaier return (1); 362171169Smlaier } 363171169Smlaier return (0); 364171169Smlaierbad: 365171169Smlaier icmp_error(m, type, code, 0, 0); 366171169Smlaier IPSTAT_INC(ips_badoptions); 367171169Smlaier return (1); 368171169Smlaier} 369171169Smlaier 370171169Smlaier/* 371171169Smlaier * Save incoming source route for use in replies, to be picked up later by 372171169Smlaier * ip_srcroute if the receiver is interested. 373171169Smlaier */ 374171169Smlaierstatic void 375171169Smlaiersave_rte(struct mbuf *m, u_char *option, struct in_addr dst) 376171169Smlaier{ 377171169Smlaier unsigned olen; 378171169Smlaier struct ipopt_tag *opts; 379171169Smlaier 380171169Smlaier opts = (struct ipopt_tag *)m_tag_get(PACKET_TAG_IPOPTIONS, 381171169Smlaier sizeof(struct ipopt_tag), M_NOWAIT); 382171169Smlaier if (opts == NULL) 383171169Smlaier return; 384171169Smlaier 385171169Smlaier olen = option[IPOPT_OLEN]; 386171169Smlaier if (olen > sizeof(opts->ip_srcrt) - (1 + sizeof(dst))) { 387171169Smlaier m_tag_free((struct m_tag *)opts); 388171169Smlaier return; 389171169Smlaier } 390171169Smlaier bcopy(option, opts->ip_srcrt.srcopt, olen); 391171169Smlaier opts->ip_nhops = (olen - IPOPT_OFFSET - 1) / sizeof(struct in_addr); 392171169Smlaier opts->ip_srcrt.dst = dst; 393171169Smlaier m_tag_prepend(m, (struct m_tag *)opts); 394171169Smlaier} 395171169Smlaier 396171169Smlaier/* 397171169Smlaier * Retrieve incoming source route for use in replies, in the same form used 398171169Smlaier * by setsockopt. The first hop is placed before the options, will be 399171169Smlaier * removed later. 400171169Smlaier */ 401171169Smlaierstruct mbuf * 402171169Smlaierip_srcroute(struct mbuf *m0) 403171169Smlaier{ 404171169Smlaier struct in_addr *p, *q; 405171169Smlaier struct mbuf *m; 406171169Smlaier struct ipopt_tag *opts; 407171169Smlaier 408171169Smlaier opts = (struct ipopt_tag *)m_tag_find(m0, PACKET_TAG_IPOPTIONS, NULL); 409171169Smlaier if (opts == NULL) 410171169Smlaier return (NULL); 411171169Smlaier 412171169Smlaier if (opts->ip_nhops == 0) 413171169Smlaier return (NULL); 414171169Smlaier m = m_get(M_DONTWAIT, MT_DATA); 415171169Smlaier if (m == NULL) 416171169Smlaier return (NULL); 417171169Smlaier 418171169Smlaier#define OPTSIZ (sizeof(opts->ip_srcrt.nop) + sizeof(opts->ip_srcrt.srcopt)) 419171169Smlaier 420171169Smlaier /* length is (nhops+1)*sizeof(addr) + sizeof(nop + srcrt header) */ 421171169Smlaier m->m_len = opts->ip_nhops * sizeof(struct in_addr) + 422171169Smlaier sizeof(struct in_addr) + OPTSIZ; 423171169Smlaier 424171169Smlaier /* 425171169Smlaier * First, save first hop for return route. 426171169Smlaier */ 427171169Smlaier p = &(opts->ip_srcrt.route[opts->ip_nhops - 1]); 428171169Smlaier *(mtod(m, struct in_addr *)) = *p--; 429171169Smlaier 430171169Smlaier /* 431171169Smlaier * Copy option fields and padding (nop) to mbuf. 432171169Smlaier */ 433171169Smlaier opts->ip_srcrt.nop = IPOPT_NOP; 434171169Smlaier opts->ip_srcrt.srcopt[IPOPT_OFFSET] = IPOPT_MINOFF; 435171169Smlaier (void)memcpy(mtod(m, caddr_t) + sizeof(struct in_addr), 436171169Smlaier &(opts->ip_srcrt.nop), OPTSIZ); 437171169Smlaier q = (struct in_addr *)(mtod(m, caddr_t) + 438171169Smlaier sizeof(struct in_addr) + OPTSIZ); 439171169Smlaier#undef OPTSIZ 440171169Smlaier /* 441171169Smlaier * Record return path as an IP source route, reversing the path 442171169Smlaier * (pointers are now aligned). 443171169Smlaier */ 444171169Smlaier while (p >= opts->ip_srcrt.route) { 445171169Smlaier *q++ = *p--; 446171169Smlaier } 447171169Smlaier /* 448171169Smlaier * Last hop goes to final destination. 449171169Smlaier */ 450171169Smlaier *q = opts->ip_srcrt.dst; 451171169Smlaier m_tag_delete(m0, (struct m_tag *)opts); 452171169Smlaier return (m); 453171169Smlaier} 454171169Smlaier 455171169Smlaier/* 456171169Smlaier * Strip out IP options, at higher level protocol in the kernel. Second 457 * argument is buffer to which options will be moved, and return value is 458 * their length. 459 * 460 * XXX should be deleted; last arg currently ignored. 461 */ 462void 463ip_stripoptions(struct mbuf *m, struct mbuf *mopt) 464{ 465 int i; 466 struct ip *ip = mtod(m, struct ip *); 467 caddr_t opts; 468 int olen; 469 470 olen = (ip->ip_hl << 2) - sizeof (struct ip); 471 opts = (caddr_t)(ip + 1); 472 i = m->m_len - (sizeof (struct ip) + olen); 473 bcopy(opts + olen, opts, (unsigned)i); 474 m->m_len -= olen; 475 if (m->m_flags & M_PKTHDR) 476 m->m_pkthdr.len -= olen; 477 ip->ip_v = IPVERSION; 478 ip->ip_hl = sizeof(struct ip) >> 2; 479} 480 481/* 482 * Insert IP options into preformed packet. Adjust IP destination as 483 * required for IP source routing, as indicated by a non-zero in_addr at the 484 * start of the options. 485 * 486 * XXX This routine assumes that the packet has no options in place. 487 */ 488struct mbuf * 489ip_insertoptions(struct mbuf *m, struct mbuf *opt, int *phlen) 490{ 491 struct ipoption *p = mtod(opt, struct ipoption *); 492 struct mbuf *n; 493 struct ip *ip = mtod(m, struct ip *); 494 unsigned optlen; 495 496 optlen = opt->m_len - sizeof(p->ipopt_dst); 497 if (optlen + ip->ip_len > IP_MAXPACKET) { 498 *phlen = 0; 499 return (m); /* XXX should fail */ 500 } 501 if (p->ipopt_dst.s_addr) 502 ip->ip_dst = p->ipopt_dst; 503 if (m->m_flags & M_EXT || m->m_data - optlen < m->m_pktdat) { 504 MGETHDR(n, M_DONTWAIT, MT_DATA); 505 if (n == NULL) { 506 *phlen = 0; 507 return (m); 508 } 509 M_MOVE_PKTHDR(n, m); 510 n->m_pkthdr.rcvif = NULL; 511 n->m_pkthdr.len += optlen; 512 m->m_len -= sizeof(struct ip); 513 m->m_data += sizeof(struct ip); 514 n->m_next = m; 515 m = n; 516 m->m_len = optlen + sizeof(struct ip); 517 m->m_data += max_linkhdr; 518 bcopy(ip, mtod(m, void *), sizeof(struct ip)); 519 } else { 520 m->m_data -= optlen; 521 m->m_len += optlen; 522 m->m_pkthdr.len += optlen; 523 bcopy(ip, mtod(m, void *), sizeof(struct ip)); 524 } 525 ip = mtod(m, struct ip *); 526 bcopy(p->ipopt_list, ip + 1, optlen); 527 *phlen = sizeof(struct ip) + optlen; 528 ip->ip_v = IPVERSION; 529 ip->ip_hl = *phlen >> 2; 530 ip->ip_len += optlen; 531 return (m); 532} 533 534/* 535 * Copy options from ip to jp, omitting those not copied during 536 * fragmentation. 537 */ 538int 539ip_optcopy(struct ip *ip, struct ip *jp) 540{ 541 u_char *cp, *dp; 542 int opt, optlen, cnt; 543 544 cp = (u_char *)(ip + 1); 545 dp = (u_char *)(jp + 1); 546 cnt = (ip->ip_hl << 2) - sizeof (struct ip); 547 for (; cnt > 0; cnt -= optlen, cp += optlen) { 548 opt = cp[0]; 549 if (opt == IPOPT_EOL) 550 break; 551 if (opt == IPOPT_NOP) { 552 /* Preserve for IP mcast tunnel's LSRR alignment. */ 553 *dp++ = IPOPT_NOP; 554 optlen = 1; 555 continue; 556 } 557 558 KASSERT(cnt >= IPOPT_OLEN + sizeof(*cp), 559 ("ip_optcopy: malformed ipv4 option")); 560 optlen = cp[IPOPT_OLEN]; 561 KASSERT(optlen >= IPOPT_OLEN + sizeof(*cp) && optlen <= cnt, 562 ("ip_optcopy: malformed ipv4 option")); 563 564 /* Bogus lengths should have been caught by ip_dooptions. */ 565 if (optlen > cnt) 566 optlen = cnt; 567 if (IPOPT_COPIED(opt)) { 568 bcopy(cp, dp, optlen); 569 dp += optlen; 570 } 571 } 572 for (optlen = dp - (u_char *)(jp+1); optlen & 0x3; optlen++) 573 *dp++ = IPOPT_EOL; 574 return (optlen); 575} 576 577/* 578 * Set up IP options in pcb for insertion in output packets. Store in mbuf 579 * with pointer in pcbopt, adding pseudo-option with destination address if 580 * source routed. 581 */ 582int 583ip_pcbopts(struct inpcb *inp, int optname, struct mbuf *m) 584{ 585 int cnt, optlen; 586 u_char *cp; 587 struct mbuf **pcbopt; 588 u_char opt; 589 590 INP_WLOCK_ASSERT(inp); 591 592 pcbopt = &inp->inp_options; 593 594 /* turn off any old options */ 595 if (*pcbopt) 596 (void)m_free(*pcbopt); 597 *pcbopt = 0; 598 if (m == NULL || m->m_len == 0) { 599 /* 600 * Only turning off any previous options. 601 */ 602 if (m != NULL) 603 (void)m_free(m); 604 return (0); 605 } 606 607 if (m->m_len % sizeof(int32_t)) 608 goto bad; 609 /* 610 * IP first-hop destination address will be stored before actual 611 * options; move other options back and clear it when none present. 612 */ 613 if (m->m_data + m->m_len + sizeof(struct in_addr) >= &m->m_dat[MLEN]) 614 goto bad; 615 cnt = m->m_len; 616 m->m_len += sizeof(struct in_addr); 617 cp = mtod(m, u_char *) + sizeof(struct in_addr); 618 bcopy(mtod(m, void *), cp, (unsigned)cnt); 619 bzero(mtod(m, void *), sizeof(struct in_addr)); 620 621 for (; cnt > 0; cnt -= optlen, cp += optlen) { 622 opt = cp[IPOPT_OPTVAL]; 623 if (opt == IPOPT_EOL) 624 break; 625 if (opt == IPOPT_NOP) 626 optlen = 1; 627 else { 628 if (cnt < IPOPT_OLEN + sizeof(*cp)) 629 goto bad; 630 optlen = cp[IPOPT_OLEN]; 631 if (optlen < IPOPT_OLEN + sizeof(*cp) || optlen > cnt) 632 goto bad; 633 } 634 switch (opt) { 635 636 default: 637 break; 638 639 case IPOPT_LSRR: 640 case IPOPT_SSRR: 641 /* 642 * User process specifies route as: 643 * 644 * ->A->B->C->D 645 * 646 * D must be our final destination (but we can't 647 * check that since we may not have connected yet). 648 * A is first hop destination, which doesn't appear 649 * in actual IP option, but is stored before the 650 * options. 651 */ 652 /* XXX-BZ PRIV_NETINET_SETHDROPTS? */ 653 if (optlen < IPOPT_MINOFF - 1 + sizeof(struct in_addr)) 654 goto bad; 655 m->m_len -= sizeof(struct in_addr); 656 cnt -= sizeof(struct in_addr); 657 optlen -= sizeof(struct in_addr); 658 cp[IPOPT_OLEN] = optlen; 659 /* 660 * Move first hop before start of options. 661 */ 662 bcopy((caddr_t)&cp[IPOPT_OFFSET+1], mtod(m, caddr_t), 663 sizeof(struct in_addr)); 664 /* 665 * Then copy rest of options back 666 * to close up the deleted entry. 667 */ 668 bcopy((&cp[IPOPT_OFFSET+1] + sizeof(struct in_addr)), 669 &cp[IPOPT_OFFSET+1], 670 (unsigned)cnt - (IPOPT_MINOFF - 1)); 671 break; 672 } 673 } 674 if (m->m_len > MAX_IPOPTLEN + sizeof(struct in_addr)) 675 goto bad; 676 *pcbopt = m; 677 return (0); 678 679bad: 680 (void)m_free(m); 681 return (EINVAL); 682} 683 684/* 685 * Check for the presence of the IP Router Alert option [RFC2113] 686 * in the header of an IPv4 datagram. 687 * 688 * This call is not intended for use from the forwarding path; it is here 689 * so that protocol domains may check for the presence of the option. 690 * Given how FreeBSD's IPv4 stack is currently structured, the Router Alert 691 * option does not have much relevance to the implementation, though this 692 * may change in future. 693 * Router alert options SHOULD be passed if running in IPSTEALTH mode and 694 * we are not the endpoint. 695 * Length checks on individual options should already have been peformed 696 * by ip_dooptions() therefore they are folded under INVARIANTS here. 697 * 698 * Return zero if not present or options are invalid, non-zero if present. 699 */ 700int 701ip_checkrouteralert(struct mbuf *m) 702{ 703 struct ip *ip = mtod(m, struct ip *); 704 u_char *cp; 705 int opt, optlen, cnt, found_ra; 706 707 found_ra = 0; 708 cp = (u_char *)(ip + 1); 709 cnt = (ip->ip_hl << 2) - sizeof (struct ip); 710 for (; cnt > 0; cnt -= optlen, cp += optlen) { 711 opt = cp[IPOPT_OPTVAL]; 712 if (opt == IPOPT_EOL) 713 break; 714 if (opt == IPOPT_NOP) 715 optlen = 1; 716 else { 717#ifdef INVARIANTS 718 if (cnt < IPOPT_OLEN + sizeof(*cp)) 719 break; 720#endif 721 optlen = cp[IPOPT_OLEN]; 722#ifdef INVARIANTS 723 if (optlen < IPOPT_OLEN + sizeof(*cp) || optlen > cnt) 724 break; 725#endif 726 } 727 switch (opt) { 728 case IPOPT_RA: 729#ifdef INVARIANTS 730 if (optlen != IPOPT_OFFSET + sizeof(uint16_t) || 731 (*((uint16_t *)&cp[IPOPT_OFFSET]) != 0)) 732 break; 733 else 734#endif 735 found_ra = 1; 736 break; 737 default: 738 break; 739 } 740 } 741 742 return (found_ra); 743} 744