1 /* 2 * SPDX-License-Identifier: BSD-3-Clause 3 * 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. 7 * All rights reserved. 8 * 9 * Redistribution and use in source and binary forms, with or without 10 * modification, are permitted provided that the following conditions 11 * are met: 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. 20 * 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 31 * SUCH DAMAGE. 32 */ 33 34 #include <sys/cdefs.h> 35 __FBSDID("$FreeBSD$"); 36 37 #include "opt_ipstealth.h" 38 39 #include <sys/param.h> 40 #include <sys/systm.h> 41 #include <sys/mbuf.h> 42 #include <sys/domain.h> 43 #include <sys/protosw.h> 44 #include <sys/socket.h> 45 #include <sys/time.h> 46 #include <sys/kernel.h> 47 #include <sys/syslog.h> 48 #include <sys/sysctl.h> 49 50 #include <net/if.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> 56 #include <net/vnet.h> 57 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> 68 69 #include <sys/socketvar.h> 70 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) 76 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) 82 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)"); 86 87 static void save_rte(struct mbuf *m, u_char *, struct in_addr); 88 89 /* 90 * Do option processing on a datagram, possibly discarding it if bad options 91 * are encountered, or forwarding it if source-routed. 92 * 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. 97 * 98 * Returns 1 if packet has been forwarded/freed, 0 if the packet should be 99 * processed further. 100 */ 101 int 102 ip_dooptions(struct mbuf *m, int pass) 103 { 104 struct ip *ip = mtod(m, struct ip *); 105 u_char *cp; 106 struct in_ifaddr *ia; 107 int opt, optlen, cnt, off, code, type = ICMP_PARAMPROB, forward = 0; 108 struct in_addr *sin, dst; 109 uint32_t ntime; 110 struct nhop4_extended nh_ext; 111 struct sockaddr_in ipaddr = { sizeof(ipaddr), AF_INET }; 112 113 NET_EPOCH_ASSERT(); 114 115 /* Ignore or reject packets with IP options. */ 116 if (V_ip_doopts == 0) 117 return 0; 118 else if (V_ip_doopts == 2) { 119 type = ICMP_UNREACH; 120 code = ICMP_UNREACH_FILTER_PROHIB; 121 goto bad; 122 } 123 124 dst = ip->ip_dst; 125 cp = (u_char *)(ip + 1); 126 cnt = (ip->ip_hl << 2) - sizeof (struct ip); 127 for (; cnt > 0; cnt -= optlen, cp += optlen) { 128 opt = cp[IPOPT_OPTVAL]; 129 if (opt == IPOPT_EOL) 130 break; 131 if (opt == IPOPT_NOP) 132 optlen = 1; 133 else { 134 if (cnt < IPOPT_OLEN + sizeof(*cp)) { 135 code = &cp[IPOPT_OLEN] - (u_char *)ip; 136 goto bad; 137 } 138 optlen = cp[IPOPT_OLEN]; 139 if (optlen < IPOPT_OLEN + sizeof(*cp) || optlen > cnt) { 140 code = &cp[IPOPT_OLEN] - (u_char *)ip; 141 goto bad; 142 } 143 } 144 switch (opt) { 145 146 default: 147 break; 148 149 /* 150 * Source routing with record. Find interface with current 151 * destination address. If none on this machine then drop if 152 * strictly routed, or do nothing if loosely routed. Record 153 * interface address and bring up next address component. If 154 * strictly routed make sure next address is on directly 155 * accessible net. 156 */ 157 case IPOPT_LSRR: 158 case IPOPT_SSRR: 159 #ifdef IPSTEALTH 160 if (V_ipstealth && pass > 0) 161 break; 162 #endif 163 if (optlen < IPOPT_OFFSET + sizeof(*cp)) { 164 code = &cp[IPOPT_OLEN] - (u_char *)ip; 165 goto bad; 166 } 167 if ((off = cp[IPOPT_OFFSET]) < IPOPT_MINOFF) { 168 code = &cp[IPOPT_OFFSET] - (u_char *)ip; 169 goto bad; 170 } 171 ipaddr.sin_addr = ip->ip_dst; 172 if (ifa_ifwithaddr_check((struct sockaddr *)&ipaddr) 173 == 0) { 174 if (opt == IPOPT_SSRR) { 175 type = ICMP_UNREACH; 176 code = ICMP_UNREACH_SRCFAIL; 177 goto bad; 178 } 179 if (!V_ip_dosourceroute) 180 goto nosourcerouting; 181 /* 182 * Loose routing, and not at next destination 183 * yet; nothing to do except forward. 184 */ 185 break; 186 } 187 off--; /* 0 origin */ 188 if (off > optlen - (int)sizeof(struct in_addr)) { 189 /* 190 * End of source route. Should be for us. 191 */ 192 if (!V_ip_acceptsourceroute) 193 goto nosourcerouting; 194 save_rte(m, cp, ip->ip_src); 195 break; 196 } 197 #ifdef IPSTEALTH 198 if (V_ipstealth) 199 goto dropit; 200 #endif 201 if (!V_ip_dosourceroute) { 202 if (V_ipforwarding) { 203 char srcbuf[INET_ADDRSTRLEN]; 204 char dstbuf[INET_ADDRSTRLEN]; 205 206 /* 207 * Acting as a router, so generate 208 * ICMP 209 */ 210 nosourcerouting: 211 log(LOG_WARNING, 212 "attempted source route from %s " 213 "to %s\n", 214 inet_ntoa_r(ip->ip_src, srcbuf), 215 inet_ntoa_r(ip->ip_dst, dstbuf)); 216 type = ICMP_UNREACH; 217 code = ICMP_UNREACH_SRCFAIL; 218 goto bad; 219 } else { 220 /* 221 * Not acting as a router, so 222 * silently drop. 223 */ 224 #ifdef IPSTEALTH 225 dropit: 226 #endif 227 IPSTAT_INC(ips_cantforward); 228 m_freem(m); 229 return (1); 230 } 231 } 232 233 /* 234 * locate outgoing interface 235 */ 236 (void)memcpy(&ipaddr.sin_addr, cp + off, 237 sizeof(ipaddr.sin_addr)); 238 239 type = ICMP_UNREACH; 240 code = ICMP_UNREACH_SRCFAIL; 241 242 if (opt == IPOPT_SSRR) { 243 #define INA struct in_ifaddr * 244 #define SA struct sockaddr * 245 ia = (INA)ifa_ifwithdstaddr((SA)&ipaddr, 246 RT_ALL_FIBS); 247 if (ia == NULL) 248 ia = (INA)ifa_ifwithnet((SA)&ipaddr, 0, 249 RT_ALL_FIBS); 250 if (ia == NULL) 251 goto bad; 252 253 memcpy(cp + off, &(IA_SIN(ia)->sin_addr), 254 sizeof(struct in_addr)); 255 } else { 256 /* XXX MRT 0 for routing */ 257 if (fib4_lookup_nh_ext(M_GETFIB(m), 258 ipaddr.sin_addr, 0, 0, &nh_ext) != 0) 259 goto bad; 260 261 memcpy(cp + off, &nh_ext.nh_src, 262 sizeof(struct in_addr)); 263 } 264 265 ip->ip_dst = ipaddr.sin_addr; 266 cp[IPOPT_OFFSET] += sizeof(struct in_addr); 267 /* 268 * Let ip_intr's mcast routing check handle mcast pkts 269 */ 270 forward = !IN_MULTICAST(ntohl(ip->ip_dst.s_addr)); 271 break; 272 273 case IPOPT_RR: 274 #ifdef IPSTEALTH 275 if (V_ipstealth && pass == 0) 276 break; 277 #endif 278 if (optlen < IPOPT_OFFSET + sizeof(*cp)) { 279 code = &cp[IPOPT_OFFSET] - (u_char *)ip; 280 goto bad; 281 } 282 if ((off = cp[IPOPT_OFFSET]) < IPOPT_MINOFF) { 283 code = &cp[IPOPT_OFFSET] - (u_char *)ip; 284 goto bad; 285 } 286 /* 287 * If no space remains, ignore. 288 */ 289 off--; /* 0 origin */ 290 if (off > optlen - (int)sizeof(struct in_addr)) 291 break; 292 (void)memcpy(&ipaddr.sin_addr, &ip->ip_dst, 293 sizeof(ipaddr.sin_addr)); 294 /* 295 * Locate outgoing interface; if we're the 296 * destination, use the incoming interface (should be 297 * same). 298 */ 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)); 306 } else { 307 type = ICMP_UNREACH; 308 code = ICMP_UNREACH_HOST; 309 goto bad; 310 } 311 cp[IPOPT_OFFSET] += sizeof(struct in_addr); 312 break; 313 314 case IPOPT_TS: 315 #ifdef IPSTEALTH 316 if (V_ipstealth && pass == 0) 317 break; 318 #endif 319 code = cp - (u_char *)ip; 320 if (optlen < 4 || optlen > 40) { 321 code = &cp[IPOPT_OLEN] - (u_char *)ip; 322 goto bad; 323 } 324 if ((off = cp[IPOPT_OFFSET]) < 5) { 325 code = &cp[IPOPT_OLEN] - (u_char *)ip; 326 goto bad; 327 } 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; 332 goto bad; 333 } 334 break; 335 } 336 off--; /* 0 origin */ 337 sin = (struct in_addr *)(cp + off); 338 switch (cp[IPOPT_OFFSET + 1] & 0x0f) { 339 340 case IPOPT_TS_TSONLY: 341 break; 342 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; 347 goto bad; 348 } 349 ipaddr.sin_addr = dst; 350 ia = (INA)ifaof_ifpforaddr((SA)&ipaddr, 351 m->m_pkthdr.rcvif); 352 if (ia == NULL) 353 continue; 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); 358 break; 359 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; 364 goto bad; 365 } 366 (void)memcpy(&ipaddr.sin_addr, sin, 367 sizeof(struct in_addr)); 368 if (ifa_ifwithaddr_check((SA)&ipaddr) == 0) 369 continue; 370 cp[IPOPT_OFFSET] += sizeof(struct in_addr); 371 off += sizeof(struct in_addr); 372 break; 373 374 default: 375 code = &cp[IPOPT_OFFSET + 1] - (u_char *)ip; 376 goto bad; 377 } 378 ntime = iptime(); 379 (void)memcpy(cp + off, &ntime, sizeof(uint32_t)); 380 cp[IPOPT_OFFSET] += sizeof(uint32_t); 381 } 382 } 383 if (forward && V_ipforwarding) { 384 ip_forward(m, 1); 385 return (1); 386 } 387 return (0); 388 bad: 389 icmp_error(m, type, code, 0, 0); 390 IPSTAT_INC(ips_badoptions); 391 return (1); 392 } 393 394 /* 395 * Save incoming source route for use in replies, to be picked up later by 396 * ip_srcroute if the receiver is interested. 397 */ 398 static void 399 save_rte(struct mbuf *m, u_char *option, struct in_addr dst) 400 { 401 unsigned olen; 402 struct ipopt_tag *opts; 403 404 opts = (struct ipopt_tag *)m_tag_get(PACKET_TAG_IPOPTIONS, 405 sizeof(struct ipopt_tag), M_NOWAIT); 406 if (opts == NULL) 407 return; 408 409 olen = option[IPOPT_OLEN]; 410 if (olen > sizeof(opts->ip_srcrt) - (1 + sizeof(dst))) { 411 m_tag_free((struct m_tag *)opts); 412 return; 413 } 414 bcopy(option, opts->ip_srcrt.srcopt, olen); 415 opts->ip_nhops = (olen - IPOPT_OFFSET - 1) / sizeof(struct in_addr); 416 opts->ip_srcrt.dst = dst; 417 m_tag_prepend(m, (struct m_tag *)opts); 418 } 419 420 /* 421 * Retrieve incoming source route for use in replies, in the same form used 422 * by setsockopt. The first hop is placed before the options, will be 423 * removed later. 424 */ 425 struct mbuf * 426 ip_srcroute(struct mbuf *m0) 427 { 428 struct in_addr *p, *q; 429 struct mbuf *m; 430 struct ipopt_tag *opts; 431 432 opts = (struct ipopt_tag *)m_tag_find(m0, PACKET_TAG_IPOPTIONS, NULL); 433 if (opts == NULL) 434 return (NULL); 435 436 if (opts->ip_nhops == 0) 437 return (NULL); 438 m = m_get(M_NOWAIT, MT_DATA); 439 if (m == NULL) 440 return (NULL); 441 442 #define OPTSIZ (sizeof(opts->ip_srcrt.nop) + sizeof(opts->ip_srcrt.srcopt)) 443 444 /* length is (nhops+1)*sizeof(addr) + sizeof(nop + srcrt header) */ 445 m->m_len = opts->ip_nhops * sizeof(struct in_addr) + 446 sizeof(struct in_addr) + OPTSIZ; 447 448 /* 449 * First, save first hop for return route. 450 */ 451 p = &(opts->ip_srcrt.route[opts->ip_nhops - 1]); 452 *(mtod(m, struct in_addr *)) = *p--; 453 454 /* 455 * Copy option fields and padding (nop) to mbuf. 456 */ 457 opts->ip_srcrt.nop = IPOPT_NOP; 458 opts->ip_srcrt.srcopt[IPOPT_OFFSET] = IPOPT_MINOFF; 459 (void)memcpy(mtod(m, caddr_t) + sizeof(struct in_addr), 460 &(opts->ip_srcrt.nop), OPTSIZ); 461 q = (struct in_addr *)(mtod(m, caddr_t) + 462 sizeof(struct in_addr) + OPTSIZ); 463 #undef OPTSIZ 464 /* 465 * Record return path as an IP source route, reversing the path 466 * (pointers are now aligned). 467 */ 468 while (p >= opts->ip_srcrt.route) { 469 *q++ = *p--; 470 } 471 /* 472 * Last hop goes to final destination. 473 */ 474 *q = opts->ip_srcrt.dst; 475 m_tag_delete(m0, (struct m_tag *)opts); 476 return (m); 477 } 478 479 /* 480 * Strip out IP options, at higher level protocol in the kernel. 481 */ 482 void 483 ip_stripoptions(struct mbuf *m) 484 { 485 struct ip *ip = mtod(m, struct ip *); 486 int olen; 487 488 olen = (ip->ip_hl << 2) - sizeof(struct ip); 489 m->m_len -= olen; 490 if (m->m_flags & M_PKTHDR) 491 m->m_pkthdr.len -= olen; 492 ip->ip_len = htons(ntohs(ip->ip_len) - olen); 493 ip->ip_hl = sizeof(struct ip) >> 2; 494 495 bcopy((char *)ip + sizeof(struct ip) + olen, (ip + 1), 496 (size_t )(m->m_len - sizeof(struct ip))); 497 } 498 499 /* 500 * Insert IP options into preformed packet. Adjust IP destination as 501 * required for IP source routing, as indicated by a non-zero in_addr at the 502 * start of the options. 503 * 504 * XXX This routine assumes that the packet has no options in place. 505 */ 506 struct mbuf * 507 ip_insertoptions(struct mbuf *m, struct mbuf *opt, int *phlen) 508 { 509 struct ipoption *p = mtod(opt, struct ipoption *); 510 struct mbuf *n; 511 struct ip *ip = mtod(m, struct ip *); 512 unsigned optlen; 513 514 optlen = opt->m_len - sizeof(p->ipopt_dst); 515 if (optlen + ntohs(ip->ip_len) > IP_MAXPACKET) { 516 *phlen = 0; 517 return (m); /* XXX should fail */ 518 } 519 if (p->ipopt_dst.s_addr) 520 ip->ip_dst = p->ipopt_dst; 521 if (!M_WRITABLE(m) || M_LEADINGSPACE(m) < optlen) { 522 n = m_gethdr(M_NOWAIT, MT_DATA); 523 if (n == NULL) { 524 *phlen = 0; 525 return (m); 526 } 527 m_move_pkthdr(n, m); 528 n->m_pkthdr.rcvif = NULL; 529 n->m_pkthdr.len += optlen; 530 m->m_len -= sizeof(struct ip); 531 m->m_data += sizeof(struct ip); 532 n->m_next = m; 533 m = n; 534 m->m_len = optlen + sizeof(struct ip); 535 m->m_data += max_linkhdr; 536 bcopy(ip, mtod(m, void *), sizeof(struct ip)); 537 } else { 538 m->m_data -= optlen; 539 m->m_len += optlen; 540 m->m_pkthdr.len += optlen; 541 bcopy(ip, mtod(m, void *), sizeof(struct ip)); 542 } 543 ip = mtod(m, struct ip *); 544 bcopy(p->ipopt_list, ip + 1, optlen); 545 *phlen = sizeof(struct ip) + optlen; 546 ip->ip_v = IPVERSION; 547 ip->ip_hl = *phlen >> 2; 548 ip->ip_len = htons(ntohs(ip->ip_len) + optlen); 549 return (m); 550 } 551 552 /* 553 * Copy options from ip to jp, omitting those not copied during 554 * fragmentation. 555 */ 556 int 557 ip_optcopy(struct ip *ip, struct ip *jp) 558 { 559 u_char *cp, *dp; 560 int opt, optlen, cnt; 561 562 cp = (u_char *)(ip + 1); 563 dp = (u_char *)(jp + 1); 564 cnt = (ip->ip_hl << 2) - sizeof (struct ip); 565 for (; cnt > 0; cnt -= optlen, cp += optlen) { 566 opt = cp[0]; 567 if (opt == IPOPT_EOL) 568 break; 569 if (opt == IPOPT_NOP) { 570 /* Preserve for IP mcast tunnel's LSRR alignment. */ 571 *dp++ = IPOPT_NOP; 572 optlen = 1; 573 continue; 574 } 575 576 KASSERT(cnt >= IPOPT_OLEN + sizeof(*cp), 577 ("ip_optcopy: malformed ipv4 option")); 578 optlen = cp[IPOPT_OLEN]; 579 KASSERT(optlen >= IPOPT_OLEN + sizeof(*cp) && optlen <= cnt, 580 ("ip_optcopy: malformed ipv4 option")); 581 582 /* Bogus lengths should have been caught by ip_dooptions. */ 583 if (optlen > cnt) 584 optlen = cnt; 585 if (IPOPT_COPIED(opt)) { 586 bcopy(cp, dp, optlen); 587 dp += optlen; 588 } 589 } 590 for (optlen = dp - (u_char *)(jp+1); optlen & 0x3; optlen++) 591 *dp++ = IPOPT_EOL; 592 return (optlen); 593 } 594 595 /* 596 * Set up IP options in pcb for insertion in output packets. Store in mbuf 597 * with pointer in pcbopt, adding pseudo-option with destination address if 598 * source routed. 599 */ 600 int 601 ip_pcbopts(struct inpcb *inp, int optname, struct mbuf *m) 602 { 603 int cnt, optlen; 604 u_char *cp; 605 struct mbuf **pcbopt; 606 u_char opt; 607 608 INP_WLOCK_ASSERT(inp); 609 610 pcbopt = &inp->inp_options; 611 612 /* turn off any old options */ 613 if (*pcbopt) 614 (void)m_free(*pcbopt); 615 *pcbopt = NULL; 616 if (m == NULL || m->m_len == 0) { 617 /* 618 * Only turning off any previous options. 619 */ 620 if (m != NULL) 621 (void)m_free(m); 622 return (0); 623 } 624 625 if (m->m_len % sizeof(int32_t)) 626 goto bad; 627 /* 628 * IP first-hop destination address will be stored before actual 629 * options; move other options back and clear it when none present. 630 */ 631 if (m->m_data + m->m_len + sizeof(struct in_addr) >= &m->m_dat[MLEN]) 632 goto bad; 633 cnt = m->m_len; 634 m->m_len += sizeof(struct in_addr); 635 cp = mtod(m, u_char *) + sizeof(struct in_addr); 636 bcopy(mtod(m, void *), cp, (unsigned)cnt); 637 bzero(mtod(m, void *), sizeof(struct in_addr)); 638 639 for (; cnt > 0; cnt -= optlen, cp += optlen) { 640 opt = cp[IPOPT_OPTVAL]; 641 if (opt == IPOPT_EOL) 642 break; 643 if (opt == IPOPT_NOP) 644 optlen = 1; 645 else { 646 if (cnt < IPOPT_OLEN + sizeof(*cp)) 647 goto bad; 648 optlen = cp[IPOPT_OLEN]; 649 if (optlen < IPOPT_OLEN + sizeof(*cp) || optlen > cnt) 650 goto bad; 651 } 652 switch (opt) { 653 654 default: 655 break; 656 657 case IPOPT_LSRR: 658 case IPOPT_SSRR: 659 /* 660 * User process specifies route as: 661 * 662 * ->A->B->C->D 663 * 664 * D must be our final destination (but we can't 665 * check that since we may not have connected yet). 666 * A is first hop destination, which doesn't appear 667 * in actual IP option, but is stored before the 668 * options. 669 */ 670 /* XXX-BZ PRIV_NETINET_SETHDROPTS? */ 671 if (optlen < IPOPT_MINOFF - 1 + sizeof(struct in_addr)) 672 goto bad; 673 m->m_len -= sizeof(struct in_addr); 674 cnt -= sizeof(struct in_addr); 675 optlen -= sizeof(struct in_addr); 676 cp[IPOPT_OLEN] = optlen; 677 /* 678 * Move first hop before start of options. 679 */ 680 bcopy((caddr_t)&cp[IPOPT_OFFSET+1], mtod(m, caddr_t), 681 sizeof(struct in_addr)); 682 /* 683 * Then copy rest of options back 684 * to close up the deleted entry. 685 */ 686 bcopy((&cp[IPOPT_OFFSET+1] + sizeof(struct in_addr)), 687 &cp[IPOPT_OFFSET+1], 688 (unsigned)cnt - (IPOPT_MINOFF - 1)); 689 break; 690 } 691 } 692 if (m->m_len > MAX_IPOPTLEN + sizeof(struct in_addr)) 693 goto bad; 694 *pcbopt = m; 695 return (0); 696 697 bad: 698 (void)m_free(m); 699 return (EINVAL); 700 } 701 702 /* 703 * Check for the presence of the IP Router Alert option [RFC2113] 704 * in the header of an IPv4 datagram. 705 * 706 * This call is not intended for use from the forwarding path; it is here 707 * so that protocol domains may check for the presence of the option. 708 * Given how FreeBSD's IPv4 stack is currently structured, the Router Alert 709 * option does not have much relevance to the implementation, though this 710 * may change in future. 711 * Router alert options SHOULD be passed if running in IPSTEALTH mode and 712 * we are not the endpoint. 713 * Length checks on individual options should already have been performed 714 * by ip_dooptions() therefore they are folded under INVARIANTS here. 715 * 716 * Return zero if not present or options are invalid, non-zero if present. 717 */ 718 int 719 ip_checkrouteralert(struct mbuf *m) 720 { 721 struct ip *ip = mtod(m, struct ip *); 722 u_char *cp; 723 int opt, optlen, cnt, found_ra; 724 725 found_ra = 0; 726 cp = (u_char *)(ip + 1); 727 cnt = (ip->ip_hl << 2) - sizeof (struct ip); 728 for (; cnt > 0; cnt -= optlen, cp += optlen) { 729 opt = cp[IPOPT_OPTVAL]; 730 if (opt == IPOPT_EOL) 731 break; 732 if (opt == IPOPT_NOP) 733 optlen = 1; 734 else { 735 #ifdef INVARIANTS 736 if (cnt < IPOPT_OLEN + sizeof(*cp)) 737 break; 738 #endif 739 optlen = cp[IPOPT_OLEN]; 740 #ifdef INVARIANTS 741 if (optlen < IPOPT_OLEN + sizeof(*cp) || optlen > cnt) 742 break; 743 #endif 744 } 745 switch (opt) { 746 case IPOPT_RA: 747 #ifdef INVARIANTS 748 if (optlen != IPOPT_OFFSET + sizeof(uint16_t) || 749 (*((uint16_t *)&cp[IPOPT_OFFSET]) != 0)) 750 break; 751 else 752 #endif 753 found_ra = 1; 754 break; 755 default: 756 break; 757 } 758 } 759 760 return (found_ra); 761 } 762