1152592Sandre/* 2152592Sandre * Copyright (c) 1982, 1986, 1988, 1993 3169464Srwatson * The Regents of the University of California. 4169464Srwatson * Copyright (c) 2005 Andre Oppermann, Internet Business Solutions AG. 5169464Srwatson * All rights reserved. 6152592Sandre * 7152592Sandre * Redistribution and use in source and binary forms, with or without 8152592Sandre * modification, are permitted provided that the following conditions 9152592Sandre * are met: 10152592Sandre * 1. Redistributions of source code must retain the above copyright 11152592Sandre * notice, this list of conditions and the following disclaimer. 12152592Sandre * 2. Redistributions in binary form must reproduce the above copyright 13152592Sandre * notice, this list of conditions and the following disclaimer in the 14152592Sandre * documentation and/or other materials provided with the distribution. 15152592Sandre * 4. Neither the name of the University nor the names of its contributors 16152592Sandre * may be used to endorse or promote products derived from this software 17152592Sandre * without specific prior written permission. 18152592Sandre * 19152592Sandre * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 20152592Sandre * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 21152592Sandre * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 22152592Sandre * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 23152592Sandre * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 24152592Sandre * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 25152592Sandre * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 26152592Sandre * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 27152592Sandre * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 28152592Sandre * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 29152592Sandre * SUCH DAMAGE. 30152592Sandre */ 31152592Sandre 32172467Ssilby#include <sys/cdefs.h> 33172467Ssilby__FBSDID("$FreeBSD$"); 34172467Ssilby 35152592Sandre#include "opt_ipstealth.h" 36152592Sandre 37152592Sandre#include <sys/param.h> 38152592Sandre#include <sys/systm.h> 39152592Sandre#include <sys/mbuf.h> 40152592Sandre#include <sys/domain.h> 41152592Sandre#include <sys/protosw.h> 42152592Sandre#include <sys/socket.h> 43152592Sandre#include <sys/time.h> 44152592Sandre#include <sys/kernel.h> 45152592Sandre#include <sys/syslog.h> 46152592Sandre#include <sys/sysctl.h> 47152592Sandre 48152592Sandre#include <net/if.h> 49152592Sandre#include <net/if_types.h> 50152592Sandre#include <net/if_var.h> 51152592Sandre#include <net/if_dl.h> 52152592Sandre#include <net/route.h> 53152592Sandre#include <net/netisr.h> 54196019Srwatson#include <net/vnet.h> 55152592Sandre 56152592Sandre#include <netinet/in.h> 57152592Sandre#include <netinet/in_systm.h> 58152592Sandre#include <netinet/in_var.h> 59152592Sandre#include <netinet/ip.h> 60152592Sandre#include <netinet/in_pcb.h> 61152592Sandre#include <netinet/ip_var.h> 62152592Sandre#include <netinet/ip_options.h> 63152592Sandre#include <netinet/ip_icmp.h> 64152592Sandre#include <machine/in_cksum.h> 65152592Sandre 66152592Sandre#include <sys/socketvar.h> 67152592Sandre 68273188Shrsstatic VNET_DEFINE(int, ip_dosourceroute); 69273188ShrsSYSCTL_INT(_net_inet_ip, IPCTL_SOURCEROUTE, sourceroute, 70273188Shrs CTLFLAG_VNET | CTLFLAG_RW, &VNET_NAME(ip_dosourceroute), 0, 71273188Shrs "Enable forwarding source routed IP packets"); 72273188Shrs#define V_ip_dosourceroute VNET(ip_dosourceroute) 73152592Sandre 74273188Shrsstatic VNET_DEFINE(int, ip_acceptsourceroute); 75152592SandreSYSCTL_INT(_net_inet_ip, IPCTL_ACCEPTSOURCEROUTE, accept_sourceroute, 76273188Shrs CTLFLAG_VNET | CTLFLAG_RW, &VNET_NAME(ip_acceptsourceroute), 0, 77152592Sandre "Enable accepting source routed IP packets"); 78273188Shrs#define V_ip_acceptsourceroute VNET(ip_acceptsourceroute) 79152592Sandre 80273188ShrsVNET_DEFINE(int, ip_doopts) = 1; /* 0 = ignore, 1 = process, 2 = reject */ 81273188ShrsSYSCTL_INT(_net_inet_ip, OID_AUTO, process_options, CTLFLAG_VNET | CTLFLAG_RW, 82273188Shrs &VNET_NAME(ip_doopts), 0, "Enable IP options processing ([LS]SRR, RR, TS)"); 83152592Sandre 84152592Sandrestatic void save_rte(struct mbuf *m, u_char *, struct in_addr); 85152592Sandre 86152592Sandre/* 87169464Srwatson * Do option processing on a datagram, possibly discarding it if bad options 88169464Srwatson * are encountered, or forwarding it if source-routed. 89169464Srwatson * 90169464Srwatson * The pass argument is used when operating in the IPSTEALTH mode to tell 91169464Srwatson * what options to process: [LS]SRR (pass 0) or the others (pass 1). The 92169464Srwatson * reason for as many as two passes is that when doing IPSTEALTH, non-routing 93169464Srwatson * options should be processed only if the packet is for us. 94169464Srwatson * 95169464Srwatson * Returns 1 if packet has been forwarded/freed, 0 if the packet should be 96169464Srwatson * processed further. 97152592Sandre */ 98152592Sandreint 99152592Sandreip_dooptions(struct mbuf *m, int pass) 100152592Sandre{ 101152592Sandre struct ip *ip = mtod(m, struct ip *); 102152592Sandre u_char *cp; 103152592Sandre struct in_ifaddr *ia; 104152592Sandre int opt, optlen, cnt, off, code, type = ICMP_PARAMPROB, forward = 0; 105152592Sandre struct in_addr *sin, dst; 106188578Sluigi uint32_t ntime; 107152592Sandre struct sockaddr_in ipaddr = { sizeof(ipaddr), AF_INET }; 108152592Sandre 109169464Srwatson /* Ignore or reject packets with IP options. */ 110273188Shrs if (V_ip_doopts == 0) 111152592Sandre return 0; 112273188Shrs else if (V_ip_doopts == 2) { 113152592Sandre type = ICMP_UNREACH; 114152592Sandre code = ICMP_UNREACH_FILTER_PROHIB; 115152592Sandre goto bad; 116152592Sandre } 117152592Sandre 118152592Sandre dst = ip->ip_dst; 119152592Sandre cp = (u_char *)(ip + 1); 120152592Sandre cnt = (ip->ip_hl << 2) - sizeof (struct ip); 121152592Sandre for (; cnt > 0; cnt -= optlen, cp += optlen) { 122152592Sandre opt = cp[IPOPT_OPTVAL]; 123152592Sandre if (opt == IPOPT_EOL) 124152592Sandre break; 125152592Sandre if (opt == IPOPT_NOP) 126152592Sandre optlen = 1; 127152592Sandre else { 128152592Sandre if (cnt < IPOPT_OLEN + sizeof(*cp)) { 129152592Sandre code = &cp[IPOPT_OLEN] - (u_char *)ip; 130152592Sandre goto bad; 131152592Sandre } 132152592Sandre optlen = cp[IPOPT_OLEN]; 133152592Sandre if (optlen < IPOPT_OLEN + sizeof(*cp) || optlen > cnt) { 134152592Sandre code = &cp[IPOPT_OLEN] - (u_char *)ip; 135152592Sandre goto bad; 136152592Sandre } 137152592Sandre } 138152592Sandre switch (opt) { 139152592Sandre 140152592Sandre default: 141152592Sandre break; 142152592Sandre 143152592Sandre /* 144169464Srwatson * Source routing with record. Find interface with current 145169464Srwatson * destination address. If none on this machine then drop if 146169464Srwatson * strictly routed, or do nothing if loosely routed. Record 147169464Srwatson * interface address and bring up next address component. If 148169464Srwatson * strictly routed make sure next address is on directly 149169464Srwatson * accessible net. 150152592Sandre */ 151152592Sandre case IPOPT_LSRR: 152152592Sandre case IPOPT_SSRR: 153152592Sandre#ifdef IPSTEALTH 154181803Sbz if (V_ipstealth && pass > 0) 155152592Sandre break; 156152592Sandre#endif 157152592Sandre if (optlen < IPOPT_OFFSET + sizeof(*cp)) { 158152592Sandre code = &cp[IPOPT_OLEN] - (u_char *)ip; 159152592Sandre goto bad; 160152592Sandre } 161152592Sandre if ((off = cp[IPOPT_OFFSET]) < IPOPT_MINOFF) { 162152592Sandre code = &cp[IPOPT_OFFSET] - (u_char *)ip; 163152592Sandre goto bad; 164152592Sandre } 165152592Sandre ipaddr.sin_addr = ip->ip_dst; 166194760Srwatson if (ifa_ifwithaddr_check((struct sockaddr *)&ipaddr) 167194760Srwatson == 0) { 168152592Sandre if (opt == IPOPT_SSRR) { 169152592Sandre type = ICMP_UNREACH; 170152592Sandre code = ICMP_UNREACH_SRCFAIL; 171152592Sandre goto bad; 172152592Sandre } 173273188Shrs if (!V_ip_dosourceroute) 174152592Sandre goto nosourcerouting; 175152592Sandre /* 176152592Sandre * Loose routing, and not at next destination 177152592Sandre * yet; nothing to do except forward. 178152592Sandre */ 179152592Sandre break; 180152592Sandre } 181152592Sandre off--; /* 0 origin */ 182152592Sandre if (off > optlen - (int)sizeof(struct in_addr)) { 183152592Sandre /* 184152592Sandre * End of source route. Should be for us. 185152592Sandre */ 186273188Shrs if (!V_ip_acceptsourceroute) 187152592Sandre goto nosourcerouting; 188152592Sandre save_rte(m, cp, ip->ip_src); 189152592Sandre break; 190152592Sandre } 191152592Sandre#ifdef IPSTEALTH 192181803Sbz if (V_ipstealth) 193152592Sandre goto dropit; 194152592Sandre#endif 195273188Shrs if (!V_ip_dosourceroute) { 196181803Sbz if (V_ipforwarding) { 197152592Sandre char buf[16]; /* aaa.bbb.ccc.ddd\0 */ 198152592Sandre /* 199169464Srwatson * Acting as a router, so generate 200169464Srwatson * ICMP 201152592Sandre */ 202152592Sandrenosourcerouting: 203152592Sandre strcpy(buf, inet_ntoa(ip->ip_dst)); 204152592Sandre log(LOG_WARNING, 205152592Sandre "attempted source route from %s to %s\n", 206152592Sandre inet_ntoa(ip->ip_src), buf); 207152592Sandre type = ICMP_UNREACH; 208152592Sandre code = ICMP_UNREACH_SRCFAIL; 209152592Sandre goto bad; 210152592Sandre } else { 211152592Sandre /* 212169464Srwatson * Not acting as a router, so 213169464Srwatson * silently drop. 214152592Sandre */ 215152592Sandre#ifdef IPSTEALTH 216152592Sandredropit: 217152592Sandre#endif 218190951Srwatson IPSTAT_INC(ips_cantforward); 219152592Sandre m_freem(m); 220152592Sandre return (1); 221152592Sandre } 222152592Sandre } 223152592Sandre 224152592Sandre /* 225152592Sandre * locate outgoing interface 226152592Sandre */ 227152592Sandre (void)memcpy(&ipaddr.sin_addr, cp + off, 228152592Sandre sizeof(ipaddr.sin_addr)); 229152592Sandre 230152592Sandre if (opt == IPOPT_SSRR) { 231152592Sandre#define INA struct in_ifaddr * 232152592Sandre#define SA struct sockaddr * 233152592Sandre if ((ia = (INA)ifa_ifwithdstaddr((SA)&ipaddr)) == NULL) 234208553Sqingli ia = (INA)ifa_ifwithnet((SA)&ipaddr, 0); 235152592Sandre } else 236178888Sjulian/* XXX MRT 0 for routing */ 237178888Sjulian ia = ip_rtaddr(ipaddr.sin_addr, M_GETFIB(m)); 238152592Sandre if (ia == NULL) { 239152592Sandre type = ICMP_UNREACH; 240152592Sandre code = ICMP_UNREACH_SRCFAIL; 241152592Sandre goto bad; 242152592Sandre } 243152592Sandre ip->ip_dst = ipaddr.sin_addr; 244152592Sandre (void)memcpy(cp + off, &(IA_SIN(ia)->sin_addr), 245152592Sandre sizeof(struct in_addr)); 246194760Srwatson ifa_free(&ia->ia_ifa); 247152592Sandre cp[IPOPT_OFFSET] += sizeof(struct in_addr); 248152592Sandre /* 249152592Sandre * Let ip_intr's mcast routing check handle mcast pkts 250152592Sandre */ 251152592Sandre forward = !IN_MULTICAST(ntohl(ip->ip_dst.s_addr)); 252152592Sandre break; 253152592Sandre 254152592Sandre case IPOPT_RR: 255152592Sandre#ifdef IPSTEALTH 256181803Sbz if (V_ipstealth && pass == 0) 257152592Sandre break; 258152592Sandre#endif 259152592Sandre if (optlen < IPOPT_OFFSET + sizeof(*cp)) { 260152592Sandre code = &cp[IPOPT_OFFSET] - (u_char *)ip; 261152592Sandre goto bad; 262152592Sandre } 263152592Sandre if ((off = cp[IPOPT_OFFSET]) < IPOPT_MINOFF) { 264152592Sandre code = &cp[IPOPT_OFFSET] - (u_char *)ip; 265152592Sandre goto bad; 266152592Sandre } 267152592Sandre /* 268152592Sandre * If no space remains, ignore. 269152592Sandre */ 270152592Sandre off--; /* 0 origin */ 271152592Sandre if (off > optlen - (int)sizeof(struct in_addr)) 272152592Sandre break; 273152592Sandre (void)memcpy(&ipaddr.sin_addr, &ip->ip_dst, 274152592Sandre sizeof(ipaddr.sin_addr)); 275152592Sandre /* 276169464Srwatson * Locate outgoing interface; if we're the 277169464Srwatson * destination, use the incoming interface (should be 278169464Srwatson * same). 279152592Sandre */ 280152592Sandre if ((ia = (INA)ifa_ifwithaddr((SA)&ipaddr)) == NULL && 281178888Sjulian (ia = ip_rtaddr(ipaddr.sin_addr, M_GETFIB(m))) == NULL) { 282152592Sandre type = ICMP_UNREACH; 283152592Sandre code = ICMP_UNREACH_HOST; 284152592Sandre goto bad; 285152592Sandre } 286152592Sandre (void)memcpy(cp + off, &(IA_SIN(ia)->sin_addr), 287152592Sandre sizeof(struct in_addr)); 288194760Srwatson ifa_free(&ia->ia_ifa); 289152592Sandre cp[IPOPT_OFFSET] += sizeof(struct in_addr); 290152592Sandre break; 291152592Sandre 292152592Sandre case IPOPT_TS: 293152592Sandre#ifdef IPSTEALTH 294181803Sbz if (V_ipstealth && pass == 0) 295152592Sandre break; 296152592Sandre#endif 297152592Sandre code = cp - (u_char *)ip; 298152592Sandre if (optlen < 4 || optlen > 40) { 299152592Sandre code = &cp[IPOPT_OLEN] - (u_char *)ip; 300152592Sandre goto bad; 301152592Sandre } 302152592Sandre if ((off = cp[IPOPT_OFFSET]) < 5) { 303152592Sandre code = &cp[IPOPT_OLEN] - (u_char *)ip; 304152592Sandre goto bad; 305152592Sandre } 306152592Sandre if (off > optlen - (int)sizeof(int32_t)) { 307152592Sandre cp[IPOPT_OFFSET + 1] += (1 << 4); 308152592Sandre if ((cp[IPOPT_OFFSET + 1] & 0xf0) == 0) { 309152592Sandre code = &cp[IPOPT_OFFSET] - (u_char *)ip; 310152592Sandre goto bad; 311152592Sandre } 312152592Sandre break; 313152592Sandre } 314152592Sandre off--; /* 0 origin */ 315152592Sandre sin = (struct in_addr *)(cp + off); 316152592Sandre switch (cp[IPOPT_OFFSET + 1] & 0x0f) { 317152592Sandre 318152592Sandre case IPOPT_TS_TSONLY: 319152592Sandre break; 320152592Sandre 321152592Sandre case IPOPT_TS_TSANDADDR: 322188578Sluigi if (off + sizeof(uint32_t) + 323152592Sandre sizeof(struct in_addr) > optlen) { 324152592Sandre code = &cp[IPOPT_OFFSET] - (u_char *)ip; 325152592Sandre goto bad; 326152592Sandre } 327152592Sandre ipaddr.sin_addr = dst; 328152592Sandre ia = (INA)ifaof_ifpforaddr((SA)&ipaddr, 329152592Sandre m->m_pkthdr.rcvif); 330152592Sandre if (ia == NULL) 331152592Sandre continue; 332152592Sandre (void)memcpy(sin, &IA_SIN(ia)->sin_addr, 333152592Sandre sizeof(struct in_addr)); 334194760Srwatson ifa_free(&ia->ia_ifa); 335152592Sandre cp[IPOPT_OFFSET] += sizeof(struct in_addr); 336152592Sandre off += sizeof(struct in_addr); 337152592Sandre break; 338152592Sandre 339152592Sandre case IPOPT_TS_PRESPEC: 340188578Sluigi if (off + sizeof(uint32_t) + 341152592Sandre sizeof(struct in_addr) > optlen) { 342152592Sandre code = &cp[IPOPT_OFFSET] - (u_char *)ip; 343152592Sandre goto bad; 344152592Sandre } 345152592Sandre (void)memcpy(&ipaddr.sin_addr, sin, 346152592Sandre sizeof(struct in_addr)); 347213832Sbz if (ifa_ifwithaddr_check((SA)&ipaddr) == 0) 348152592Sandre continue; 349152592Sandre cp[IPOPT_OFFSET] += sizeof(struct in_addr); 350152592Sandre off += sizeof(struct in_addr); 351152592Sandre break; 352152592Sandre 353152592Sandre default: 354152592Sandre code = &cp[IPOPT_OFFSET + 1] - (u_char *)ip; 355152592Sandre goto bad; 356152592Sandre } 357152592Sandre ntime = iptime(); 358188578Sluigi (void)memcpy(cp + off, &ntime, sizeof(uint32_t)); 359188578Sluigi cp[IPOPT_OFFSET] += sizeof(uint32_t); 360152592Sandre } 361152592Sandre } 362181803Sbz if (forward && V_ipforwarding) { 363152592Sandre ip_forward(m, 1); 364152592Sandre return (1); 365152592Sandre } 366152592Sandre return (0); 367152592Sandrebad: 368152592Sandre icmp_error(m, type, code, 0, 0); 369190951Srwatson IPSTAT_INC(ips_badoptions); 370152592Sandre return (1); 371152592Sandre} 372152592Sandre 373152592Sandre/* 374169464Srwatson * Save incoming source route for use in replies, to be picked up later by 375169464Srwatson * ip_srcroute if the receiver is interested. 376152592Sandre */ 377152592Sandrestatic void 378169454Srwatsonsave_rte(struct mbuf *m, u_char *option, struct in_addr dst) 379152592Sandre{ 380152592Sandre unsigned olen; 381152592Sandre struct ipopt_tag *opts; 382152592Sandre 383152592Sandre opts = (struct ipopt_tag *)m_tag_get(PACKET_TAG_IPOPTIONS, 384169464Srwatson sizeof(struct ipopt_tag), M_NOWAIT); 385152592Sandre if (opts == NULL) 386152592Sandre return; 387152592Sandre 388152592Sandre olen = option[IPOPT_OLEN]; 389152592Sandre if (olen > sizeof(opts->ip_srcrt) - (1 + sizeof(dst))) { 390152592Sandre m_tag_free((struct m_tag *)opts); 391152592Sandre return; 392152592Sandre } 393152592Sandre bcopy(option, opts->ip_srcrt.srcopt, olen); 394152592Sandre opts->ip_nhops = (olen - IPOPT_OFFSET - 1) / sizeof(struct in_addr); 395152592Sandre opts->ip_srcrt.dst = dst; 396152592Sandre m_tag_prepend(m, (struct m_tag *)opts); 397152592Sandre} 398152592Sandre 399152592Sandre/* 400169464Srwatson * Retrieve incoming source route for use in replies, in the same form used 401169464Srwatson * by setsockopt. The first hop is placed before the options, will be 402169464Srwatson * removed later. 403152592Sandre */ 404152592Sandrestruct mbuf * 405169454Srwatsonip_srcroute(struct mbuf *m0) 406152592Sandre{ 407169464Srwatson struct in_addr *p, *q; 408169464Srwatson struct mbuf *m; 409152592Sandre struct ipopt_tag *opts; 410152592Sandre 411152592Sandre opts = (struct ipopt_tag *)m_tag_find(m0, PACKET_TAG_IPOPTIONS, NULL); 412152592Sandre if (opts == NULL) 413152592Sandre return (NULL); 414152592Sandre 415152592Sandre if (opts->ip_nhops == 0) 416152592Sandre return (NULL); 417243882Sglebius m = m_get(M_NOWAIT, MT_DATA); 418152592Sandre if (m == NULL) 419152592Sandre return (NULL); 420152592Sandre 421152592Sandre#define OPTSIZ (sizeof(opts->ip_srcrt.nop) + sizeof(opts->ip_srcrt.srcopt)) 422152592Sandre 423152592Sandre /* length is (nhops+1)*sizeof(addr) + sizeof(nop + srcrt header) */ 424152592Sandre m->m_len = opts->ip_nhops * sizeof(struct in_addr) + 425152592Sandre sizeof(struct in_addr) + OPTSIZ; 426152592Sandre 427152592Sandre /* 428169464Srwatson * First, save first hop for return route. 429152592Sandre */ 430152592Sandre p = &(opts->ip_srcrt.route[opts->ip_nhops - 1]); 431152592Sandre *(mtod(m, struct in_addr *)) = *p--; 432152592Sandre 433152592Sandre /* 434152592Sandre * Copy option fields and padding (nop) to mbuf. 435152592Sandre */ 436152592Sandre opts->ip_srcrt.nop = IPOPT_NOP; 437152592Sandre opts->ip_srcrt.srcopt[IPOPT_OFFSET] = IPOPT_MINOFF; 438152592Sandre (void)memcpy(mtod(m, caddr_t) + sizeof(struct in_addr), 439152592Sandre &(opts->ip_srcrt.nop), OPTSIZ); 440152592Sandre q = (struct in_addr *)(mtod(m, caddr_t) + 441152592Sandre sizeof(struct in_addr) + OPTSIZ); 442152592Sandre#undef OPTSIZ 443152592Sandre /* 444169464Srwatson * Record return path as an IP source route, reversing the path 445169464Srwatson * (pointers are now aligned). 446152592Sandre */ 447152592Sandre while (p >= opts->ip_srcrt.route) { 448152592Sandre *q++ = *p--; 449152592Sandre } 450152592Sandre /* 451152592Sandre * Last hop goes to final destination. 452152592Sandre */ 453152592Sandre *q = opts->ip_srcrt.dst; 454152592Sandre m_tag_delete(m0, (struct m_tag *)opts); 455152592Sandre return (m); 456152592Sandre} 457152592Sandre 458152592Sandre/* 459241480Sglebius * Strip out IP options, at higher level protocol in the kernel. 460152592Sandre */ 461152592Sandrevoid 462241480Sglebiusip_stripoptions(struct mbuf *m) 463152592Sandre{ 464152592Sandre struct ip *ip = mtod(m, struct ip *); 465152592Sandre int olen; 466152592Sandre 467241925Sglebius olen = (ip->ip_hl << 2) - sizeof(struct ip); 468152592Sandre m->m_len -= olen; 469152592Sandre if (m->m_flags & M_PKTHDR) 470152592Sandre m->m_pkthdr.len -= olen; 471241923Sglebius ip->ip_len = htons(ntohs(ip->ip_len) - olen); 472152592Sandre ip->ip_hl = sizeof(struct ip) >> 2; 473241925Sglebius 474241925Sglebius bcopy((char *)ip + sizeof(struct ip) + olen, (ip + 1), 475241925Sglebius (size_t )(m->m_len - sizeof(struct ip))); 476152592Sandre} 477152592Sandre 478152592Sandre/* 479169464Srwatson * Insert IP options into preformed packet. Adjust IP destination as 480169464Srwatson * required for IP source routing, as indicated by a non-zero in_addr at the 481169464Srwatson * start of the options. 482152592Sandre * 483152592Sandre * XXX This routine assumes that the packet has no options in place. 484152592Sandre */ 485152592Sandrestruct mbuf * 486169454Srwatsonip_insertoptions(struct mbuf *m, struct mbuf *opt, int *phlen) 487152592Sandre{ 488169464Srwatson struct ipoption *p = mtod(opt, struct ipoption *); 489152592Sandre struct mbuf *n; 490169464Srwatson struct ip *ip = mtod(m, struct ip *); 491152592Sandre unsigned optlen; 492152592Sandre 493152592Sandre optlen = opt->m_len - sizeof(p->ipopt_dst); 494241913Sglebius if (optlen + ntohs(ip->ip_len) > IP_MAXPACKET) { 495152592Sandre *phlen = 0; 496152592Sandre return (m); /* XXX should fail */ 497152592Sandre } 498152592Sandre if (p->ipopt_dst.s_addr) 499152592Sandre ip->ip_dst = p->ipopt_dst; 500152592Sandre if (m->m_flags & M_EXT || m->m_data - optlen < m->m_pktdat) { 501248324Sglebius n = m_gethdr(M_NOWAIT, MT_DATA); 502152592Sandre if (n == NULL) { 503152592Sandre *phlen = 0; 504152592Sandre return (m); 505152592Sandre } 506248373Sglebius m_move_pkthdr(n, m); 507152592Sandre n->m_pkthdr.rcvif = NULL; 508152592Sandre n->m_pkthdr.len += optlen; 509152592Sandre m->m_len -= sizeof(struct ip); 510152592Sandre m->m_data += sizeof(struct ip); 511152592Sandre n->m_next = m; 512152592Sandre m = n; 513152592Sandre m->m_len = optlen + sizeof(struct ip); 514152592Sandre m->m_data += max_linkhdr; 515152592Sandre bcopy(ip, mtod(m, void *), sizeof(struct ip)); 516152592Sandre } else { 517152592Sandre m->m_data -= optlen; 518152592Sandre m->m_len += optlen; 519152592Sandre m->m_pkthdr.len += optlen; 520152592Sandre bcopy(ip, mtod(m, void *), sizeof(struct ip)); 521152592Sandre } 522152592Sandre ip = mtod(m, struct ip *); 523152592Sandre bcopy(p->ipopt_list, ip + 1, optlen); 524152592Sandre *phlen = sizeof(struct ip) + optlen; 525152592Sandre ip->ip_v = IPVERSION; 526152592Sandre ip->ip_hl = *phlen >> 2; 527241913Sglebius ip->ip_len = htons(ntohs(ip->ip_len) + optlen); 528152592Sandre return (m); 529152592Sandre} 530152592Sandre 531152592Sandre/* 532169464Srwatson * Copy options from ip to jp, omitting those not copied during 533169464Srwatson * fragmentation. 534152592Sandre */ 535152592Sandreint 536169454Srwatsonip_optcopy(struct ip *ip, struct ip *jp) 537152592Sandre{ 538169464Srwatson u_char *cp, *dp; 539152592Sandre int opt, optlen, cnt; 540152592Sandre 541152592Sandre cp = (u_char *)(ip + 1); 542152592Sandre dp = (u_char *)(jp + 1); 543152592Sandre cnt = (ip->ip_hl << 2) - sizeof (struct ip); 544152592Sandre for (; cnt > 0; cnt -= optlen, cp += optlen) { 545152592Sandre opt = cp[0]; 546152592Sandre if (opt == IPOPT_EOL) 547152592Sandre break; 548152592Sandre if (opt == IPOPT_NOP) { 549152592Sandre /* Preserve for IP mcast tunnel's LSRR alignment. */ 550152592Sandre *dp++ = IPOPT_NOP; 551152592Sandre optlen = 1; 552152592Sandre continue; 553152592Sandre } 554152592Sandre 555152592Sandre KASSERT(cnt >= IPOPT_OLEN + sizeof(*cp), 556152592Sandre ("ip_optcopy: malformed ipv4 option")); 557152592Sandre optlen = cp[IPOPT_OLEN]; 558152592Sandre KASSERT(optlen >= IPOPT_OLEN + sizeof(*cp) && optlen <= cnt, 559152592Sandre ("ip_optcopy: malformed ipv4 option")); 560152592Sandre 561169464Srwatson /* Bogus lengths should have been caught by ip_dooptions. */ 562152592Sandre if (optlen > cnt) 563152592Sandre optlen = cnt; 564152592Sandre if (IPOPT_COPIED(opt)) { 565152592Sandre bcopy(cp, dp, optlen); 566152592Sandre dp += optlen; 567152592Sandre } 568152592Sandre } 569152592Sandre for (optlen = dp - (u_char *)(jp+1); optlen & 0x3; optlen++) 570152592Sandre *dp++ = IPOPT_EOL; 571152592Sandre return (optlen); 572152592Sandre} 573152592Sandre 574152592Sandre/* 575169464Srwatson * Set up IP options in pcb for insertion in output packets. Store in mbuf 576169464Srwatson * with pointer in pcbopt, adding pseudo-option with destination address if 577169464Srwatson * source routed. 578152592Sandre */ 579152592Sandreint 580152592Sandreip_pcbopts(struct inpcb *inp, int optname, struct mbuf *m) 581152592Sandre{ 582169464Srwatson int cnt, optlen; 583169464Srwatson u_char *cp; 584152592Sandre struct mbuf **pcbopt; 585152592Sandre u_char opt; 586152592Sandre 587178285Srwatson INP_WLOCK_ASSERT(inp); 588152592Sandre 589152592Sandre pcbopt = &inp->inp_options; 590152592Sandre 591152592Sandre /* turn off any old options */ 592152592Sandre if (*pcbopt) 593152592Sandre (void)m_free(*pcbopt); 594152592Sandre *pcbopt = 0; 595152592Sandre if (m == NULL || m->m_len == 0) { 596152592Sandre /* 597152592Sandre * Only turning off any previous options. 598152592Sandre */ 599152592Sandre if (m != NULL) 600152592Sandre (void)m_free(m); 601152592Sandre return (0); 602152592Sandre } 603152592Sandre 604152592Sandre if (m->m_len % sizeof(int32_t)) 605152592Sandre goto bad; 606152592Sandre /* 607169464Srwatson * IP first-hop destination address will be stored before actual 608169464Srwatson * options; move other options back and clear it when none present. 609152592Sandre */ 610152592Sandre if (m->m_data + m->m_len + sizeof(struct in_addr) >= &m->m_dat[MLEN]) 611152592Sandre goto bad; 612152592Sandre cnt = m->m_len; 613152592Sandre m->m_len += sizeof(struct in_addr); 614152592Sandre cp = mtod(m, u_char *) + sizeof(struct in_addr); 615152592Sandre bcopy(mtod(m, void *), cp, (unsigned)cnt); 616152592Sandre bzero(mtod(m, void *), sizeof(struct in_addr)); 617152592Sandre 618152592Sandre for (; cnt > 0; cnt -= optlen, cp += optlen) { 619152592Sandre opt = cp[IPOPT_OPTVAL]; 620152592Sandre if (opt == IPOPT_EOL) 621152592Sandre break; 622152592Sandre if (opt == IPOPT_NOP) 623152592Sandre optlen = 1; 624152592Sandre else { 625152592Sandre if (cnt < IPOPT_OLEN + sizeof(*cp)) 626152592Sandre goto bad; 627152592Sandre optlen = cp[IPOPT_OLEN]; 628152592Sandre if (optlen < IPOPT_OLEN + sizeof(*cp) || optlen > cnt) 629152592Sandre goto bad; 630152592Sandre } 631152592Sandre switch (opt) { 632152592Sandre 633152592Sandre default: 634152592Sandre break; 635152592Sandre 636152592Sandre case IPOPT_LSRR: 637152592Sandre case IPOPT_SSRR: 638152592Sandre /* 639169464Srwatson * User process specifies route as: 640169464Srwatson * 641152592Sandre * ->A->B->C->D 642169464Srwatson * 643152592Sandre * D must be our final destination (but we can't 644152592Sandre * check that since we may not have connected yet). 645169464Srwatson * A is first hop destination, which doesn't appear 646169464Srwatson * in actual IP option, but is stored before the 647169464Srwatson * options. 648152592Sandre */ 649175630Sbz /* XXX-BZ PRIV_NETINET_SETHDROPTS? */ 650152592Sandre if (optlen < IPOPT_MINOFF - 1 + sizeof(struct in_addr)) 651152592Sandre goto bad; 652152592Sandre m->m_len -= sizeof(struct in_addr); 653152592Sandre cnt -= sizeof(struct in_addr); 654152592Sandre optlen -= sizeof(struct in_addr); 655152592Sandre cp[IPOPT_OLEN] = optlen; 656152592Sandre /* 657152592Sandre * Move first hop before start of options. 658152592Sandre */ 659152592Sandre bcopy((caddr_t)&cp[IPOPT_OFFSET+1], mtod(m, caddr_t), 660152592Sandre sizeof(struct in_addr)); 661152592Sandre /* 662152592Sandre * Then copy rest of options back 663152592Sandre * to close up the deleted entry. 664152592Sandre */ 665152592Sandre bcopy((&cp[IPOPT_OFFSET+1] + sizeof(struct in_addr)), 666152592Sandre &cp[IPOPT_OFFSET+1], 667152592Sandre (unsigned)cnt - (IPOPT_MINOFF - 1)); 668152592Sandre break; 669152592Sandre } 670152592Sandre } 671152592Sandre if (m->m_len > MAX_IPOPTLEN + sizeof(struct in_addr)) 672152592Sandre goto bad; 673152592Sandre *pcbopt = m; 674152592Sandre return (0); 675152592Sandre 676152592Sandrebad: 677152592Sandre (void)m_free(m); 678152592Sandre return (EINVAL); 679152592Sandre} 680189343Sbms 681189343Sbms/* 682189343Sbms * Check for the presence of the IP Router Alert option [RFC2113] 683189343Sbms * in the header of an IPv4 datagram. 684189343Sbms * 685189343Sbms * This call is not intended for use from the forwarding path; it is here 686189343Sbms * so that protocol domains may check for the presence of the option. 687189343Sbms * Given how FreeBSD's IPv4 stack is currently structured, the Router Alert 688189343Sbms * option does not have much relevance to the implementation, though this 689189343Sbms * may change in future. 690189343Sbms * Router alert options SHOULD be passed if running in IPSTEALTH mode and 691189343Sbms * we are not the endpoint. 692189343Sbms * Length checks on individual options should already have been peformed 693189343Sbms * by ip_dooptions() therefore they are folded under INVARIANTS here. 694189343Sbms * 695189343Sbms * Return zero if not present or options are invalid, non-zero if present. 696189343Sbms */ 697189343Sbmsint 698189343Sbmsip_checkrouteralert(struct mbuf *m) 699189343Sbms{ 700189343Sbms struct ip *ip = mtod(m, struct ip *); 701189343Sbms u_char *cp; 702189343Sbms int opt, optlen, cnt, found_ra; 703189343Sbms 704189343Sbms found_ra = 0; 705189343Sbms cp = (u_char *)(ip + 1); 706189343Sbms cnt = (ip->ip_hl << 2) - sizeof (struct ip); 707189343Sbms for (; cnt > 0; cnt -= optlen, cp += optlen) { 708189343Sbms opt = cp[IPOPT_OPTVAL]; 709189343Sbms if (opt == IPOPT_EOL) 710189343Sbms break; 711189343Sbms if (opt == IPOPT_NOP) 712189343Sbms optlen = 1; 713189343Sbms else { 714189343Sbms#ifdef INVARIANTS 715189343Sbms if (cnt < IPOPT_OLEN + sizeof(*cp)) 716189343Sbms break; 717189343Sbms#endif 718189343Sbms optlen = cp[IPOPT_OLEN]; 719189343Sbms#ifdef INVARIANTS 720189343Sbms if (optlen < IPOPT_OLEN + sizeof(*cp) || optlen > cnt) 721189343Sbms break; 722189343Sbms#endif 723189343Sbms } 724189343Sbms switch (opt) { 725189343Sbms case IPOPT_RA: 726189343Sbms#ifdef INVARIANTS 727189343Sbms if (optlen != IPOPT_OFFSET + sizeof(uint16_t) || 728189343Sbms (*((uint16_t *)&cp[IPOPT_OFFSET]) != 0)) 729189343Sbms break; 730189343Sbms else 731189343Sbms#endif 732189343Sbms found_ra = 1; 733189343Sbms break; 734189343Sbms default: 735189343Sbms break; 736189343Sbms } 737189343Sbms } 738189343Sbms 739189343Sbms return (found_ra); 740189343Sbms} 741