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