1 /* 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. 5 * All rights reserved. 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that the following conditions 9 * are met: 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. 18 * 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 29 * SUCH DAMAGE. 30 */ 31 32 #include <sys/cdefs.h> 33 __FBSDID("$FreeBSD$"); 34 35 #include "opt_ipstealth.h" 36 37 #include <sys/param.h> 38 #include <sys/systm.h> 39 #include <sys/mbuf.h> 40 #include <sys/domain.h> 41 #include <sys/protosw.h> 42 #include <sys/socket.h> 43 #include <sys/time.h> 44 #include <sys/kernel.h> 45 #include <sys/syslog.h> 46 #include <sys/sysctl.h> 47 48 #include <net/if.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> 54 #include <net/vnet.h> 55 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> 65 66 #include <sys/socketvar.h> 67 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) 73 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) 79 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)"); 83 84 static void save_rte(struct mbuf *m, u_char *, struct in_addr); 85 86 /* 87 * Do option processing on a datagram, possibly discarding it if bad options 88 * are encountered, or forwarding it if source-routed. 89 * 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. 94 * 95 * Returns 1 if packet has been forwarded/freed, 0 if the packet should be 96 * processed further. 97 */ 98 int 99 ip_dooptions(struct mbuf *m, int pass) 100 { 101 struct ip *ip = mtod(m, struct ip *); 102 u_char *cp; 103 struct in_ifaddr *ia; 104 int opt, optlen, cnt, off, code, type = ICMP_PARAMPROB, forward = 0; 105 struct in_addr *sin, dst; 106 uint32_t ntime; 107 struct sockaddr_in ipaddr = { sizeof(ipaddr), AF_INET }; 108 109 /* Ignore or reject packets with IP options. */ 110 if (V_ip_doopts == 0) 111 return 0; 112 else if (V_ip_doopts == 2) { 113 type = ICMP_UNREACH; 114 code = ICMP_UNREACH_FILTER_PROHIB; 115 goto bad; 116 } 117 118 dst = ip->ip_dst; 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) 124 break; 125 if (opt == IPOPT_NOP) 126 optlen = 1; 127 else { 128 if (cnt < IPOPT_OLEN + sizeof(*cp)) { 129 code = &cp[IPOPT_OLEN] - (u_char *)ip; 130 goto bad; 131 } 132 optlen = cp[IPOPT_OLEN]; 133 if (optlen < IPOPT_OLEN + sizeof(*cp) || optlen > cnt) { 134 code = &cp[IPOPT_OLEN] - (u_char *)ip; 135 goto bad; 136 } 137 } 138 switch (opt) { 139 140 default: 141 break; 142 143 /* 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 149 * accessible net. 150 */ 151 case IPOPT_LSRR: 152 case IPOPT_SSRR: 153 #ifdef IPSTEALTH 154 if (V_ipstealth && pass > 0) 155 break; 156 #endif 157 if (optlen < IPOPT_OFFSET + sizeof(*cp)) { 158 code = &cp[IPOPT_OLEN] - (u_char *)ip; 159 goto bad; 160 } 161 if ((off = cp[IPOPT_OFFSET]) < IPOPT_MINOFF) { 162 code = &cp[IPOPT_OFFSET] - (u_char *)ip; 163 goto bad; 164 } 165 ipaddr.sin_addr = ip->ip_dst; 166 if (ifa_ifwithaddr_check((struct sockaddr *)&ipaddr) 167 == 0) { 168 if (opt == IPOPT_SSRR) { 169 type = ICMP_UNREACH; 170 code = ICMP_UNREACH_SRCFAIL; 171 goto bad; 172 } 173 if (!V_ip_dosourceroute) 174 goto nosourcerouting; 175 /* 176 * Loose routing, and not at next destination 177 * yet; nothing to do except forward. 178 */ 179 break; 180 } 181 off--; /* 0 origin */ 182 if (off > optlen - (int)sizeof(struct in_addr)) { 183 /* 184 * End of source route. Should be for us. 185 */ 186 if (!V_ip_acceptsourceroute) 187 goto nosourcerouting; 188 save_rte(m, cp, ip->ip_src); 189 break; 190 } 191 #ifdef IPSTEALTH 192 if (V_ipstealth) 193 goto dropit; 194 #endif 195 if (!V_ip_dosourceroute) { 196 if (V_ipforwarding) { 197 char buf[16]; /* aaa.bbb.ccc.ddd\0 */ 198 /* 199 * Acting as a router, so generate 200 * ICMP 201 */ 202 nosourcerouting: 203 strcpy(buf, inet_ntoa(ip->ip_dst)); 204 log(LOG_WARNING, 205 "attempted source route from %s to %s\n", 206 inet_ntoa(ip->ip_src), buf); 207 type = ICMP_UNREACH; 208 code = ICMP_UNREACH_SRCFAIL; 209 goto bad; 210 } else { 211 /* 212 * Not acting as a router, so 213 * silently drop. 214 */ 215 #ifdef IPSTEALTH 216 dropit: 217 #endif 218 IPSTAT_INC(ips_cantforward); 219 m_freem(m); 220 return (1); 221 } 222 } 223 224 /* 225 * locate outgoing interface 226 */ 227 (void)memcpy(&ipaddr.sin_addr, cp + off, 228 sizeof(ipaddr.sin_addr)); 229 230 if (opt == IPOPT_SSRR) { 231 #define INA struct in_ifaddr * 232 #define SA struct sockaddr * 233 ia = (INA)ifa_ifwithdstaddr((SA)&ipaddr, 234 RT_ALL_FIBS); 235 if (ia == NULL) 236 ia = (INA)ifa_ifwithnet((SA)&ipaddr, 0, 237 RT_ALL_FIBS); 238 } else 239 /* XXX MRT 0 for routing */ 240 ia = ip_rtaddr(ipaddr.sin_addr, M_GETFIB(m)); 241 if (ia == NULL) { 242 type = ICMP_UNREACH; 243 code = ICMP_UNREACH_SRCFAIL; 244 goto bad; 245 } 246 ip->ip_dst = ipaddr.sin_addr; 247 (void)memcpy(cp + off, &(IA_SIN(ia)->sin_addr), 248 sizeof(struct in_addr)); 249 ifa_free(&ia->ia_ifa); 250 cp[IPOPT_OFFSET] += sizeof(struct in_addr); 251 /* 252 * Let ip_intr's mcast routing check handle mcast pkts 253 */ 254 forward = !IN_MULTICAST(ntohl(ip->ip_dst.s_addr)); 255 break; 256 257 case IPOPT_RR: 258 #ifdef IPSTEALTH 259 if (V_ipstealth && pass == 0) 260 break; 261 #endif 262 if (optlen < IPOPT_OFFSET + sizeof(*cp)) { 263 code = &cp[IPOPT_OFFSET] - (u_char *)ip; 264 goto bad; 265 } 266 if ((off = cp[IPOPT_OFFSET]) < IPOPT_MINOFF) { 267 code = &cp[IPOPT_OFFSET] - (u_char *)ip; 268 goto bad; 269 } 270 /* 271 * If no space remains, ignore. 272 */ 273 off--; /* 0 origin */ 274 if (off > optlen - (int)sizeof(struct in_addr)) 275 break; 276 (void)memcpy(&ipaddr.sin_addr, &ip->ip_dst, 277 sizeof(ipaddr.sin_addr)); 278 /* 279 * Locate outgoing interface; if we're the 280 * destination, use the incoming interface (should be 281 * same). 282 */ 283 if ((ia = (INA)ifa_ifwithaddr((SA)&ipaddr)) == NULL && 284 (ia = ip_rtaddr(ipaddr.sin_addr, M_GETFIB(m))) == NULL) { 285 type = ICMP_UNREACH; 286 code = ICMP_UNREACH_HOST; 287 goto bad; 288 } 289 (void)memcpy(cp + off, &(IA_SIN(ia)->sin_addr), 290 sizeof(struct in_addr)); 291 ifa_free(&ia->ia_ifa); 292 cp[IPOPT_OFFSET] += sizeof(struct in_addr); 293 break; 294 295 case IPOPT_TS: 296 #ifdef IPSTEALTH 297 if (V_ipstealth && pass == 0) 298 break; 299 #endif 300 code = cp - (u_char *)ip; 301 if (optlen < 4 || optlen > 40) { 302 code = &cp[IPOPT_OLEN] - (u_char *)ip; 303 goto bad; 304 } 305 if ((off = cp[IPOPT_OFFSET]) < 5) { 306 code = &cp[IPOPT_OLEN] - (u_char *)ip; 307 goto bad; 308 } 309 if (off > optlen - (int)sizeof(int32_t)) { 310 cp[IPOPT_OFFSET + 1] += (1 << 4); 311 if ((cp[IPOPT_OFFSET + 1] & 0xf0) == 0) { 312 code = &cp[IPOPT_OFFSET] - (u_char *)ip; 313 goto bad; 314 } 315 break; 316 } 317 off--; /* 0 origin */ 318 sin = (struct in_addr *)(cp + off); 319 switch (cp[IPOPT_OFFSET + 1] & 0x0f) { 320 321 case IPOPT_TS_TSONLY: 322 break; 323 324 case IPOPT_TS_TSANDADDR: 325 if (off + sizeof(uint32_t) + 326 sizeof(struct in_addr) > optlen) { 327 code = &cp[IPOPT_OFFSET] - (u_char *)ip; 328 goto bad; 329 } 330 ipaddr.sin_addr = dst; 331 ia = (INA)ifaof_ifpforaddr((SA)&ipaddr, 332 m->m_pkthdr.rcvif); 333 if (ia == NULL) 334 continue; 335 (void)memcpy(sin, &IA_SIN(ia)->sin_addr, 336 sizeof(struct in_addr)); 337 ifa_free(&ia->ia_ifa); 338 cp[IPOPT_OFFSET] += sizeof(struct in_addr); 339 off += sizeof(struct in_addr); 340 break; 341 342 case IPOPT_TS_PRESPEC: 343 if (off + sizeof(uint32_t) + 344 sizeof(struct in_addr) > optlen) { 345 code = &cp[IPOPT_OFFSET] - (u_char *)ip; 346 goto bad; 347 } 348 (void)memcpy(&ipaddr.sin_addr, sin, 349 sizeof(struct in_addr)); 350 if (ifa_ifwithaddr_check((SA)&ipaddr) == 0) 351 continue; 352 cp[IPOPT_OFFSET] += sizeof(struct in_addr); 353 off += sizeof(struct in_addr); 354 break; 355 356 default: 357 code = &cp[IPOPT_OFFSET + 1] - (u_char *)ip; 358 goto bad; 359 } 360 ntime = iptime(); 361 (void)memcpy(cp + off, &ntime, sizeof(uint32_t)); 362 cp[IPOPT_OFFSET] += sizeof(uint32_t); 363 } 364 } 365 if (forward && V_ipforwarding) { 366 ip_forward(m, 1); 367 return (1); 368 } 369 return (0); 370 bad: 371 icmp_error(m, type, code, 0, 0); 372 IPSTAT_INC(ips_badoptions); 373 return (1); 374 } 375 376 /* 377 * Save incoming source route for use in replies, to be picked up later by 378 * ip_srcroute if the receiver is interested. 379 */ 380 static void 381 save_rte(struct mbuf *m, u_char *option, struct in_addr dst) 382 { 383 unsigned olen; 384 struct ipopt_tag *opts; 385 386 opts = (struct ipopt_tag *)m_tag_get(PACKET_TAG_IPOPTIONS, 387 sizeof(struct ipopt_tag), M_NOWAIT); 388 if (opts == NULL) 389 return; 390 391 olen = option[IPOPT_OLEN]; 392 if (olen > sizeof(opts->ip_srcrt) - (1 + sizeof(dst))) { 393 m_tag_free((struct m_tag *)opts); 394 return; 395 } 396 bcopy(option, opts->ip_srcrt.srcopt, olen); 397 opts->ip_nhops = (olen - IPOPT_OFFSET - 1) / sizeof(struct in_addr); 398 opts->ip_srcrt.dst = dst; 399 m_tag_prepend(m, (struct m_tag *)opts); 400 } 401 402 /* 403 * Retrieve incoming source route for use in replies, in the same form used 404 * by setsockopt. The first hop is placed before the options, will be 405 * removed later. 406 */ 407 struct mbuf * 408 ip_srcroute(struct mbuf *m0) 409 { 410 struct in_addr *p, *q; 411 struct mbuf *m; 412 struct ipopt_tag *opts; 413 414 opts = (struct ipopt_tag *)m_tag_find(m0, PACKET_TAG_IPOPTIONS, NULL); 415 if (opts == NULL) 416 return (NULL); 417 418 if (opts->ip_nhops == 0) 419 return (NULL); 420 m = m_get(M_NOWAIT, MT_DATA); 421 if (m == NULL) 422 return (NULL); 423 424 #define OPTSIZ (sizeof(opts->ip_srcrt.nop) + sizeof(opts->ip_srcrt.srcopt)) 425 426 /* length is (nhops+1)*sizeof(addr) + sizeof(nop + srcrt header) */ 427 m->m_len = opts->ip_nhops * sizeof(struct in_addr) + 428 sizeof(struct in_addr) + OPTSIZ; 429 430 /* 431 * First, save first hop for return route. 432 */ 433 p = &(opts->ip_srcrt.route[opts->ip_nhops - 1]); 434 *(mtod(m, struct in_addr *)) = *p--; 435 436 /* 437 * Copy option fields and padding (nop) to mbuf. 438 */ 439 opts->ip_srcrt.nop = IPOPT_NOP; 440 opts->ip_srcrt.srcopt[IPOPT_OFFSET] = IPOPT_MINOFF; 441 (void)memcpy(mtod(m, caddr_t) + sizeof(struct in_addr), 442 &(opts->ip_srcrt.nop), OPTSIZ); 443 q = (struct in_addr *)(mtod(m, caddr_t) + 444 sizeof(struct in_addr) + OPTSIZ); 445 #undef OPTSIZ 446 /* 447 * Record return path as an IP source route, reversing the path 448 * (pointers are now aligned). 449 */ 450 while (p >= opts->ip_srcrt.route) { 451 *q++ = *p--; 452 } 453 /* 454 * Last hop goes to final destination. 455 */ 456 *q = opts->ip_srcrt.dst; 457 m_tag_delete(m0, (struct m_tag *)opts); 458 return (m); 459 } 460 461 /* 462 * Strip out IP options, at higher level protocol in the kernel. 463 */ 464 void 465 ip_stripoptions(struct mbuf *m) 466 { 467 struct ip *ip = mtod(m, struct ip *); 468 int olen; 469 470 olen = (ip->ip_hl << 2) - sizeof(struct ip); 471 m->m_len -= olen; 472 if (m->m_flags & M_PKTHDR) 473 m->m_pkthdr.len -= olen; 474 ip->ip_len = htons(ntohs(ip->ip_len) - olen); 475 ip->ip_hl = sizeof(struct ip) >> 2; 476 477 bcopy((char *)ip + sizeof(struct ip) + olen, (ip + 1), 478 (size_t )(m->m_len - sizeof(struct ip))); 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 */ 488 struct mbuf * 489 ip_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 + ntohs(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 n = m_gethdr(M_NOWAIT, 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 = htons(ntohs(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 */ 538 int 539 ip_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 */ 582 int 583 ip_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 679 bad: 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 */ 700 int 701 ip_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