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