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 #include "opt_mac.h" 37 38 #include <sys/param.h> 39 #include <sys/systm.h> 40 #include <sys/mbuf.h> 41 #include <sys/domain.h> 42 #include <sys/protosw.h> 43 #include <sys/socket.h> 44 #include <sys/time.h> 45 #include <sys/kernel.h> 46 #include <sys/syslog.h> 47 #include <sys/sysctl.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 66 #include <sys/socketvar.h> 67 68 #include <security/mac/mac_framework.h> 69 70 static int ip_dosourceroute = 0; 71 SYSCTL_INT(_net_inet_ip, IPCTL_SOURCEROUTE, sourceroute, CTLFLAG_RW, 72 &ip_dosourceroute, 0, "Enable forwarding source routed IP packets"); 73 74 static int ip_acceptsourceroute = 0; 75 SYSCTL_INT(_net_inet_ip, IPCTL_ACCEPTSOURCEROUTE, accept_sourceroute, 76 CTLFLAG_RW, &ip_acceptsourceroute, 0, 77 "Enable accepting source routed IP packets"); 78 79 int ip_doopts = 1; /* 0 = ignore, 1 = process, 2 = reject */ 80 SYSCTL_INT(_net_inet_ip, OID_AUTO, process_options, CTLFLAG_RW, 81 &ip_doopts, 0, "Enable IP options processing ([LS]SRR, RR, TS)"); 82 83 static void save_rte(struct mbuf *m, u_char *, struct in_addr); 84 85 /* 86 * Do option processing on a datagram, possibly discarding it if bad options 87 * are encountered, or forwarding it if source-routed. 88 * 89 * The pass argument is used when operating in the IPSTEALTH mode to tell 90 * what options to process: [LS]SRR (pass 0) or the others (pass 1). The 91 * reason for as many as two passes is that when doing IPSTEALTH, non-routing 92 * options should be processed only if the packet is for us. 93 * 94 * Returns 1 if packet has been forwarded/freed, 0 if the packet should be 95 * processed further. 96 */ 97 int 98 ip_dooptions(struct mbuf *m, int pass) 99 { 100 struct ip *ip = mtod(m, struct ip *); 101 u_char *cp; 102 struct in_ifaddr *ia; 103 int opt, optlen, cnt, off, code, type = ICMP_PARAMPROB, forward = 0; 104 struct in_addr *sin, dst; 105 n_time ntime; 106 struct sockaddr_in ipaddr = { sizeof(ipaddr), AF_INET }; 107 108 /* Ignore or reject packets with IP options. */ 109 if (ip_doopts == 0) 110 return 0; 111 else if (ip_doopts == 2) { 112 type = ICMP_UNREACH; 113 code = ICMP_UNREACH_FILTER_PROHIB; 114 goto bad; 115 } 116 117 dst = ip->ip_dst; 118 cp = (u_char *)(ip + 1); 119 cnt = (ip->ip_hl << 2) - sizeof (struct ip); 120 for (; cnt > 0; cnt -= optlen, cp += optlen) { 121 opt = cp[IPOPT_OPTVAL]; 122 if (opt == IPOPT_EOL) 123 break; 124 if (opt == IPOPT_NOP) 125 optlen = 1; 126 else { 127 if (cnt < IPOPT_OLEN + sizeof(*cp)) { 128 code = &cp[IPOPT_OLEN] - (u_char *)ip; 129 goto bad; 130 } 131 optlen = cp[IPOPT_OLEN]; 132 if (optlen < IPOPT_OLEN + sizeof(*cp) || optlen > cnt) { 133 code = &cp[IPOPT_OLEN] - (u_char *)ip; 134 goto bad; 135 } 136 } 137 switch (opt) { 138 139 default: 140 break; 141 142 /* 143 * Source routing with record. Find interface with current 144 * destination address. If none on this machine then drop if 145 * strictly routed, or do nothing if loosely routed. Record 146 * interface address and bring up next address component. If 147 * strictly routed make sure next address is on directly 148 * accessible net. 149 */ 150 case IPOPT_LSRR: 151 case IPOPT_SSRR: 152 #ifdef IPSTEALTH 153 if (ipstealth && pass > 0) 154 break; 155 #endif 156 if (optlen < IPOPT_OFFSET + sizeof(*cp)) { 157 code = &cp[IPOPT_OLEN] - (u_char *)ip; 158 goto bad; 159 } 160 if ((off = cp[IPOPT_OFFSET]) < IPOPT_MINOFF) { 161 code = &cp[IPOPT_OFFSET] - (u_char *)ip; 162 goto bad; 163 } 164 ipaddr.sin_addr = ip->ip_dst; 165 ia = (struct in_ifaddr *) 166 ifa_ifwithaddr((struct sockaddr *)&ipaddr); 167 if (ia == NULL) { 168 if (opt == IPOPT_SSRR) { 169 type = ICMP_UNREACH; 170 code = ICMP_UNREACH_SRCFAIL; 171 goto bad; 172 } 173 if (!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 (!ip_acceptsourceroute) 187 goto nosourcerouting; 188 save_rte(m, cp, ip->ip_src); 189 break; 190 } 191 #ifdef IPSTEALTH 192 if (ipstealth) 193 goto dropit; 194 #endif 195 if (!ip_dosourceroute) { 196 if (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.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 if ((ia = (INA)ifa_ifwithdstaddr((SA)&ipaddr)) == NULL) 234 ia = (INA)ifa_ifwithnet((SA)&ipaddr); 235 } else 236 ia = ip_rtaddr(ipaddr.sin_addr); 237 if (ia == NULL) { 238 type = ICMP_UNREACH; 239 code = ICMP_UNREACH_SRCFAIL; 240 goto bad; 241 } 242 ip->ip_dst = ipaddr.sin_addr; 243 (void)memcpy(cp + off, &(IA_SIN(ia)->sin_addr), 244 sizeof(struct in_addr)); 245 cp[IPOPT_OFFSET] += sizeof(struct in_addr); 246 /* 247 * Let ip_intr's mcast routing check handle mcast pkts 248 */ 249 forward = !IN_MULTICAST(ntohl(ip->ip_dst.s_addr)); 250 break; 251 252 case IPOPT_RR: 253 #ifdef IPSTEALTH 254 if (ipstealth && pass == 0) 255 break; 256 #endif 257 if (optlen < IPOPT_OFFSET + sizeof(*cp)) { 258 code = &cp[IPOPT_OFFSET] - (u_char *)ip; 259 goto bad; 260 } 261 if ((off = cp[IPOPT_OFFSET]) < IPOPT_MINOFF) { 262 code = &cp[IPOPT_OFFSET] - (u_char *)ip; 263 goto bad; 264 } 265 /* 266 * If no space remains, ignore. 267 */ 268 off--; /* 0 origin */ 269 if (off > optlen - (int)sizeof(struct in_addr)) 270 break; 271 (void)memcpy(&ipaddr.sin_addr, &ip->ip_dst, 272 sizeof(ipaddr.sin_addr)); 273 /* 274 * Locate outgoing interface; if we're the 275 * destination, use the incoming interface (should be 276 * same). 277 */ 278 if ((ia = (INA)ifa_ifwithaddr((SA)&ipaddr)) == NULL && 279 (ia = ip_rtaddr(ipaddr.sin_addr)) == NULL) { 280 type = ICMP_UNREACH; 281 code = ICMP_UNREACH_HOST; 282 goto bad; 283 } 284 (void)memcpy(cp + off, &(IA_SIN(ia)->sin_addr), 285 sizeof(struct in_addr)); 286 cp[IPOPT_OFFSET] += sizeof(struct in_addr); 287 break; 288 289 case IPOPT_TS: 290 #ifdef IPSTEALTH 291 if (ipstealth && pass == 0) 292 break; 293 #endif 294 code = cp - (u_char *)ip; 295 if (optlen < 4 || optlen > 40) { 296 code = &cp[IPOPT_OLEN] - (u_char *)ip; 297 goto bad; 298 } 299 if ((off = cp[IPOPT_OFFSET]) < 5) { 300 code = &cp[IPOPT_OLEN] - (u_char *)ip; 301 goto bad; 302 } 303 if (off > optlen - (int)sizeof(int32_t)) { 304 cp[IPOPT_OFFSET + 1] += (1 << 4); 305 if ((cp[IPOPT_OFFSET + 1] & 0xf0) == 0) { 306 code = &cp[IPOPT_OFFSET] - (u_char *)ip; 307 goto bad; 308 } 309 break; 310 } 311 off--; /* 0 origin */ 312 sin = (struct in_addr *)(cp + off); 313 switch (cp[IPOPT_OFFSET + 1] & 0x0f) { 314 315 case IPOPT_TS_TSONLY: 316 break; 317 318 case IPOPT_TS_TSANDADDR: 319 if (off + sizeof(n_time) + 320 sizeof(struct in_addr) > optlen) { 321 code = &cp[IPOPT_OFFSET] - (u_char *)ip; 322 goto bad; 323 } 324 ipaddr.sin_addr = dst; 325 ia = (INA)ifaof_ifpforaddr((SA)&ipaddr, 326 m->m_pkthdr.rcvif); 327 if (ia == NULL) 328 continue; 329 (void)memcpy(sin, &IA_SIN(ia)->sin_addr, 330 sizeof(struct in_addr)); 331 cp[IPOPT_OFFSET] += sizeof(struct in_addr); 332 off += sizeof(struct in_addr); 333 break; 334 335 case IPOPT_TS_PRESPEC: 336 if (off + sizeof(n_time) + 337 sizeof(struct in_addr) > optlen) { 338 code = &cp[IPOPT_OFFSET] - (u_char *)ip; 339 goto bad; 340 } 341 (void)memcpy(&ipaddr.sin_addr, sin, 342 sizeof(struct in_addr)); 343 if (ifa_ifwithaddr((SA)&ipaddr) == NULL) 344 continue; 345 cp[IPOPT_OFFSET] += sizeof(struct in_addr); 346 off += sizeof(struct in_addr); 347 break; 348 349 default: 350 code = &cp[IPOPT_OFFSET + 1] - (u_char *)ip; 351 goto bad; 352 } 353 ntime = iptime(); 354 (void)memcpy(cp + off, &ntime, sizeof(n_time)); 355 cp[IPOPT_OFFSET] += sizeof(n_time); 356 } 357 } 358 if (forward && ipforwarding) { 359 ip_forward(m, 1); 360 return (1); 361 } 362 return (0); 363 bad: 364 icmp_error(m, type, code, 0, 0); 365 ipstat.ips_badoptions++; 366 return (1); 367 } 368 369 /* 370 * Save incoming source route for use in replies, to be picked up later by 371 * ip_srcroute if the receiver is interested. 372 */ 373 static void 374 save_rte(struct mbuf *m, u_char *option, struct in_addr dst) 375 { 376 unsigned olen; 377 struct ipopt_tag *opts; 378 379 opts = (struct ipopt_tag *)m_tag_get(PACKET_TAG_IPOPTIONS, 380 sizeof(struct ipopt_tag), M_NOWAIT); 381 if (opts == NULL) 382 return; 383 384 olen = option[IPOPT_OLEN]; 385 if (olen > sizeof(opts->ip_srcrt) - (1 + sizeof(dst))) { 386 m_tag_free((struct m_tag *)opts); 387 return; 388 } 389 bcopy(option, opts->ip_srcrt.srcopt, olen); 390 opts->ip_nhops = (olen - IPOPT_OFFSET - 1) / sizeof(struct in_addr); 391 opts->ip_srcrt.dst = dst; 392 m_tag_prepend(m, (struct m_tag *)opts); 393 } 394 395 /* 396 * Retrieve incoming source route for use in replies, in the same form used 397 * by setsockopt. The first hop is placed before the options, will be 398 * removed later. 399 */ 400 struct mbuf * 401 ip_srcroute(struct mbuf *m0) 402 { 403 struct in_addr *p, *q; 404 struct mbuf *m; 405 struct ipopt_tag *opts; 406 407 opts = (struct ipopt_tag *)m_tag_find(m0, PACKET_TAG_IPOPTIONS, NULL); 408 if (opts == NULL) 409 return (NULL); 410 411 if (opts->ip_nhops == 0) 412 return (NULL); 413 m = m_get(M_DONTWAIT, MT_DATA); 414 if (m == NULL) 415 return (NULL); 416 417 #define OPTSIZ (sizeof(opts->ip_srcrt.nop) + sizeof(opts->ip_srcrt.srcopt)) 418 419 /* length is (nhops+1)*sizeof(addr) + sizeof(nop + srcrt header) */ 420 m->m_len = opts->ip_nhops * sizeof(struct in_addr) + 421 sizeof(struct in_addr) + OPTSIZ; 422 423 /* 424 * First, save first hop for return route. 425 */ 426 p = &(opts->ip_srcrt.route[opts->ip_nhops - 1]); 427 *(mtod(m, struct in_addr *)) = *p--; 428 429 /* 430 * Copy option fields and padding (nop) to mbuf. 431 */ 432 opts->ip_srcrt.nop = IPOPT_NOP; 433 opts->ip_srcrt.srcopt[IPOPT_OFFSET] = IPOPT_MINOFF; 434 (void)memcpy(mtod(m, caddr_t) + sizeof(struct in_addr), 435 &(opts->ip_srcrt.nop), OPTSIZ); 436 q = (struct in_addr *)(mtod(m, caddr_t) + 437 sizeof(struct in_addr) + OPTSIZ); 438 #undef OPTSIZ 439 /* 440 * Record return path as an IP source route, reversing the path 441 * (pointers are now aligned). 442 */ 443 while (p >= opts->ip_srcrt.route) { 444 *q++ = *p--; 445 } 446 /* 447 * Last hop goes to final destination. 448 */ 449 *q = opts->ip_srcrt.dst; 450 m_tag_delete(m0, (struct m_tag *)opts); 451 return (m); 452 } 453 454 /* 455 * Strip out IP options, at higher level protocol in the kernel. Second 456 * argument is buffer to which options will be moved, and return value is 457 * their length. 458 * 459 * XXX should be deleted; last arg currently ignored. 460 */ 461 void 462 ip_stripoptions(struct mbuf *m, struct mbuf *mopt) 463 { 464 int i; 465 struct ip *ip = mtod(m, struct ip *); 466 caddr_t opts; 467 int olen; 468 469 olen = (ip->ip_hl << 2) - sizeof (struct ip); 470 opts = (caddr_t)(ip + 1); 471 i = m->m_len - (sizeof (struct ip) + olen); 472 bcopy(opts + olen, opts, (unsigned)i); 473 m->m_len -= olen; 474 if (m->m_flags & M_PKTHDR) 475 m->m_pkthdr.len -= olen; 476 ip->ip_v = IPVERSION; 477 ip->ip_hl = sizeof(struct ip) >> 2; 478 } 479 480 /* 481 * Insert IP options into preformed packet. Adjust IP destination as 482 * required for IP source routing, as indicated by a non-zero in_addr at the 483 * start of the options. 484 * 485 * XXX This routine assumes that the packet has no options in place. 486 */ 487 struct mbuf * 488 ip_insertoptions(struct mbuf *m, struct mbuf *opt, int *phlen) 489 { 490 struct ipoption *p = mtod(opt, struct ipoption *); 491 struct mbuf *n; 492 struct ip *ip = mtod(m, struct ip *); 493 unsigned optlen; 494 495 optlen = opt->m_len - sizeof(p->ipopt_dst); 496 if (optlen + ip->ip_len > IP_MAXPACKET) { 497 *phlen = 0; 498 return (m); /* XXX should fail */ 499 } 500 if (p->ipopt_dst.s_addr) 501 ip->ip_dst = p->ipopt_dst; 502 if (m->m_flags & M_EXT || m->m_data - optlen < m->m_pktdat) { 503 MGETHDR(n, M_DONTWAIT, MT_DATA); 504 if (n == NULL) { 505 *phlen = 0; 506 return (m); 507 } 508 M_MOVE_PKTHDR(n, m); 509 n->m_pkthdr.rcvif = NULL; 510 #ifdef MAC 511 mac_mbuf_copy(m, n); 512 #endif 513 n->m_pkthdr.len += optlen; 514 m->m_len -= sizeof(struct ip); 515 m->m_data += sizeof(struct ip); 516 n->m_next = m; 517 m = n; 518 m->m_len = optlen + sizeof(struct ip); 519 m->m_data += max_linkhdr; 520 bcopy(ip, mtod(m, void *), sizeof(struct ip)); 521 } else { 522 m->m_data -= optlen; 523 m->m_len += optlen; 524 m->m_pkthdr.len += optlen; 525 bcopy(ip, mtod(m, void *), sizeof(struct ip)); 526 } 527 ip = mtod(m, struct ip *); 528 bcopy(p->ipopt_list, ip + 1, optlen); 529 *phlen = sizeof(struct ip) + optlen; 530 ip->ip_v = IPVERSION; 531 ip->ip_hl = *phlen >> 2; 532 ip->ip_len += optlen; 533 return (m); 534 } 535 536 /* 537 * Copy options from ip to jp, omitting those not copied during 538 * fragmentation. 539 */ 540 int 541 ip_optcopy(struct ip *ip, struct ip *jp) 542 { 543 u_char *cp, *dp; 544 int opt, optlen, cnt; 545 546 cp = (u_char *)(ip + 1); 547 dp = (u_char *)(jp + 1); 548 cnt = (ip->ip_hl << 2) - sizeof (struct ip); 549 for (; cnt > 0; cnt -= optlen, cp += optlen) { 550 opt = cp[0]; 551 if (opt == IPOPT_EOL) 552 break; 553 if (opt == IPOPT_NOP) { 554 /* Preserve for IP mcast tunnel's LSRR alignment. */ 555 *dp++ = IPOPT_NOP; 556 optlen = 1; 557 continue; 558 } 559 560 KASSERT(cnt >= IPOPT_OLEN + sizeof(*cp), 561 ("ip_optcopy: malformed ipv4 option")); 562 optlen = cp[IPOPT_OLEN]; 563 KASSERT(optlen >= IPOPT_OLEN + sizeof(*cp) && optlen <= cnt, 564 ("ip_optcopy: malformed ipv4 option")); 565 566 /* Bogus lengths should have been caught by ip_dooptions. */ 567 if (optlen > cnt) 568 optlen = cnt; 569 if (IPOPT_COPIED(opt)) { 570 bcopy(cp, dp, optlen); 571 dp += optlen; 572 } 573 } 574 for (optlen = dp - (u_char *)(jp+1); optlen & 0x3; optlen++) 575 *dp++ = IPOPT_EOL; 576 return (optlen); 577 } 578 579 /* 580 * Set up IP options in pcb for insertion in output packets. Store in mbuf 581 * with pointer in pcbopt, adding pseudo-option with destination address if 582 * source routed. 583 */ 584 int 585 ip_pcbopts(struct inpcb *inp, int optname, struct mbuf *m) 586 { 587 int cnt, optlen; 588 u_char *cp; 589 struct mbuf **pcbopt; 590 u_char opt; 591 592 INP_LOCK_ASSERT(inp); 593 594 pcbopt = &inp->inp_options; 595 596 /* turn off any old options */ 597 if (*pcbopt) 598 (void)m_free(*pcbopt); 599 *pcbopt = 0; 600 if (m == NULL || m->m_len == 0) { 601 /* 602 * Only turning off any previous options. 603 */ 604 if (m != NULL) 605 (void)m_free(m); 606 return (0); 607 } 608 609 if (m->m_len % sizeof(int32_t)) 610 goto bad; 611 /* 612 * IP first-hop destination address will be stored before actual 613 * options; move other options back and clear it when none present. 614 */ 615 if (m->m_data + m->m_len + sizeof(struct in_addr) >= &m->m_dat[MLEN]) 616 goto bad; 617 cnt = m->m_len; 618 m->m_len += sizeof(struct in_addr); 619 cp = mtod(m, u_char *) + sizeof(struct in_addr); 620 bcopy(mtod(m, void *), cp, (unsigned)cnt); 621 bzero(mtod(m, void *), sizeof(struct in_addr)); 622 623 for (; cnt > 0; cnt -= optlen, cp += optlen) { 624 opt = cp[IPOPT_OPTVAL]; 625 if (opt == IPOPT_EOL) 626 break; 627 if (opt == IPOPT_NOP) 628 optlen = 1; 629 else { 630 if (cnt < IPOPT_OLEN + sizeof(*cp)) 631 goto bad; 632 optlen = cp[IPOPT_OLEN]; 633 if (optlen < IPOPT_OLEN + sizeof(*cp) || optlen > cnt) 634 goto bad; 635 } 636 switch (opt) { 637 638 default: 639 break; 640 641 case IPOPT_LSRR: 642 case IPOPT_SSRR: 643 /* 644 * User process specifies route as: 645 * 646 * ->A->B->C->D 647 * 648 * D must be our final destination (but we can't 649 * check that since we may not have connected yet). 650 * A is first hop destination, which doesn't appear 651 * in actual IP option, but is stored before the 652 * options. 653 */ 654 if (optlen < IPOPT_MINOFF - 1 + sizeof(struct in_addr)) 655 goto bad; 656 m->m_len -= sizeof(struct in_addr); 657 cnt -= sizeof(struct in_addr); 658 optlen -= sizeof(struct in_addr); 659 cp[IPOPT_OLEN] = optlen; 660 /* 661 * Move first hop before start of options. 662 */ 663 bcopy((caddr_t)&cp[IPOPT_OFFSET+1], mtod(m, caddr_t), 664 sizeof(struct in_addr)); 665 /* 666 * Then copy rest of options back 667 * to close up the deleted entry. 668 */ 669 bcopy((&cp[IPOPT_OFFSET+1] + sizeof(struct in_addr)), 670 &cp[IPOPT_OFFSET+1], 671 (unsigned)cnt - (IPOPT_MINOFF - 1)); 672 break; 673 } 674 } 675 if (m->m_len > MAX_IPOPTLEN + sizeof(struct in_addr)) 676 goto bad; 677 *pcbopt = m; 678 return (0); 679 680 bad: 681 (void)m_free(m); 682 return (EINVAL); 683 } 684