1 /* 2 * Copyright (c) 1982, 1986, 1988, 1990, 1993 3 * The Regents of the University of California. All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 8 * 1. Redistributions of source code must retain the above copyright 9 * notice, this list of conditions and the following disclaimer. 10 * 2. Redistributions in binary form must reproduce the above copyright 11 * notice, this list of conditions and the following disclaimer in the 12 * documentation and/or other materials provided with the distribution. 13 * 3. Neither the name of the University nor the names of its contributors 14 * may be used to endorse or promote products derived from this software 15 * without specific prior written permission. 16 * 17 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 20 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 27 * SUCH DAMAGE. 28 * 29 * @(#)ip_output.c 8.3 (Berkeley) 1/21/94 30 * $FreeBSD: src/sys/netinet/ip_output.c,v 1.99.2.37 2003/04/15 06:44:45 silby Exp $ 31 */ 32 33 #define _IP_VHL 34 35 #include "opt_ipdn.h" 36 #include "opt_ipdivert.h" 37 #include "opt_mbuf_stress_test.h" 38 #include "opt_mpls.h" 39 40 #include <sys/param.h> 41 #include <sys/systm.h> 42 #include <sys/kernel.h> 43 #include <sys/malloc.h> 44 #include <sys/mbuf.h> 45 #include <sys/protosw.h> 46 #include <sys/socket.h> 47 #include <sys/socketvar.h> 48 #include <sys/proc.h> 49 #include <sys/priv.h> 50 #include <sys/sysctl.h> 51 #include <sys/in_cksum.h> 52 #include <sys/lock.h> 53 54 #include <sys/thread2.h> 55 #include <sys/mplock2.h> 56 #include <sys/msgport2.h> 57 58 #include <net/if.h> 59 #include <net/netisr.h> 60 #include <net/pfil.h> 61 #include <net/route.h> 62 63 #include <netinet/in.h> 64 #include <netinet/in_systm.h> 65 #include <netinet/ip.h> 66 #include <netinet/in_pcb.h> 67 #include <netinet/in_var.h> 68 #include <netinet/ip_var.h> 69 70 #include <netproto/mpls/mpls_var.h> 71 72 static MALLOC_DEFINE(M_IPMOPTS, "ip_moptions", "internet multicast options"); 73 74 #include <net/ipfw/ip_fw.h> 75 #include <net/dummynet/ip_dummynet.h> 76 77 #define print_ip(x, a, y) kprintf("%s %d.%d.%d.%d%s",\ 78 x, (ntohl(a.s_addr)>>24)&0xFF,\ 79 (ntohl(a.s_addr)>>16)&0xFF,\ 80 (ntohl(a.s_addr)>>8)&0xFF,\ 81 (ntohl(a.s_addr))&0xFF, y); 82 83 u_short ip_id; 84 85 #ifdef MBUF_STRESS_TEST 86 int mbuf_frag_size = 0; 87 SYSCTL_INT(_net_inet_ip, OID_AUTO, mbuf_frag_size, CTLFLAG_RW, 88 &mbuf_frag_size, 0, "Fragment outgoing mbufs to this size"); 89 #endif 90 91 static int ip_do_rfc6864 = 1; 92 SYSCTL_INT(_net_inet_ip, OID_AUTO, rfc6864, CTLFLAG_RW, &ip_do_rfc6864, 0, 93 "Don't generate IP ID for DF IP datagrams"); 94 95 static struct mbuf *ip_insertoptions(struct mbuf *, struct mbuf *, int *); 96 static struct ifnet *ip_multicast_if(struct in_addr *, int *); 97 static void ip_mloopback 98 (struct ifnet *, struct mbuf *, struct sockaddr_in *, int); 99 static int ip_getmoptions 100 (struct sockopt *, struct ip_moptions *); 101 static int ip_pcbopts(int, struct mbuf **, struct mbuf *); 102 static int ip_setmoptions 103 (struct sockopt *, struct ip_moptions **); 104 105 int ip_optcopy(struct ip *, struct ip *); 106 107 extern struct protosw inetsw[]; 108 109 static int 110 ip_localforward(struct mbuf *m, const struct sockaddr_in *dst, int hlen) 111 { 112 struct in_ifaddr_container *iac; 113 114 /* 115 * We need to figure out if we have been forwarded to a local 116 * socket. If so, then we should somehow "loop back" to 117 * ip_input(), and get directed to the PCB as if we had received 118 * this packet. This is because it may be difficult to identify 119 * the packets you want to forward until they are being output 120 * and have selected an interface (e.g. locally initiated 121 * packets). If we used the loopback inteface, we would not be 122 * able to control what happens as the packet runs through 123 * ip_input() as it is done through a ISR. 124 */ 125 LIST_FOREACH(iac, INADDR_HASH(dst->sin_addr.s_addr), ia_hash) { 126 /* 127 * If the addr to forward to is one of ours, we pretend 128 * to be the destination for this packet. 129 */ 130 if (IA_SIN(iac->ia)->sin_addr.s_addr == dst->sin_addr.s_addr) 131 break; 132 } 133 if (iac != NULL) { 134 struct ip *ip; 135 136 if (m->m_pkthdr.rcvif == NULL) 137 m->m_pkthdr.rcvif = loif; 138 if (m->m_pkthdr.csum_flags & CSUM_DELAY_DATA) { 139 m->m_pkthdr.csum_flags |= CSUM_DATA_VALID | 140 CSUM_PSEUDO_HDR; 141 m->m_pkthdr.csum_data = 0xffff; 142 } 143 m->m_pkthdr.csum_flags |= CSUM_IP_CHECKED | CSUM_IP_VALID; 144 145 /* 146 * Make sure that the IP header is in one mbuf, 147 * required by ip_input 148 */ 149 if (m->m_len < hlen) { 150 m = m_pullup(m, hlen); 151 if (m == NULL) { 152 /* The packet was freed; we are done */ 153 return 1; 154 } 155 } 156 ip = mtod(m, struct ip *); 157 158 ip->ip_len = htons(ip->ip_len); 159 ip->ip_off = htons(ip->ip_off); 160 ip_input(m); 161 162 return 1; /* The packet gets forwarded locally */ 163 } 164 return 0; 165 } 166 167 /* 168 * IP output. The packet in mbuf chain m contains a skeletal IP 169 * header (with len, off, ttl, proto, tos, src, dst). 170 * The mbuf chain containing the packet will be freed. 171 * The mbuf opt, if present, will not be freed. 172 */ 173 int 174 ip_output(struct mbuf *m0, struct mbuf *opt, struct route *ro, 175 int flags, struct ip_moptions *imo, struct inpcb *inp) 176 { 177 struct ip *ip; 178 struct ifnet *ifp = NULL; /* keep compiler happy */ 179 struct mbuf *m; 180 int hlen = sizeof(struct ip); 181 int len, error = 0; 182 struct sockaddr_in *dst = NULL; /* keep compiler happy */ 183 struct in_ifaddr *ia = NULL; 184 int isbroadcast, sw_csum; 185 struct in_addr pkt_dst; 186 struct route iproute; 187 struct m_tag *mtag; 188 struct sockaddr_in *next_hop = NULL; 189 int src_was_INADDR_ANY = 0; /* as the name says... */ 190 191 ASSERT_NETISR_NCPUS(mycpuid); 192 193 m = m0; 194 M_ASSERTPKTHDR(m); 195 196 if (ro == NULL) { 197 ro = &iproute; 198 bzero(ro, sizeof *ro); 199 } else if (ro->ro_rt != NULL && ro->ro_rt->rt_cpuid != mycpuid) { 200 if (flags & IP_DEBUGROUTE) { 201 panic("ip_output: rt rt_cpuid %d accessed on cpu %d\n", 202 ro->ro_rt->rt_cpuid, mycpuid); 203 } 204 205 /* 206 * XXX 207 * If the cached rtentry's owner CPU is not the current CPU, 208 * then don't touch the cached rtentry (remote free is too 209 * expensive in this context); just relocate the route. 210 */ 211 ro = &iproute; 212 bzero(ro, sizeof *ro); 213 } 214 215 if (m->m_pkthdr.fw_flags & IPFORWARD_MBUF_TAGGED) { 216 /* Next hop */ 217 mtag = m_tag_find(m, PACKET_TAG_IPFORWARD, NULL); 218 KKASSERT(mtag != NULL); 219 next_hop = m_tag_data(mtag); 220 } 221 222 if (m->m_pkthdr.fw_flags & DUMMYNET_MBUF_TAGGED) { 223 struct dn_pkt *dn_pkt; 224 225 /* Extract info from dummynet tag */ 226 mtag = m_tag_find(m, PACKET_TAG_DUMMYNET, NULL); 227 KKASSERT(mtag != NULL); 228 dn_pkt = m_tag_data(mtag); 229 230 /* 231 * The packet was already tagged, so part of the 232 * processing was already done, and we need to go down. 233 * Get the calculated parameters from the tag. 234 */ 235 ifp = dn_pkt->ifp; 236 237 KKASSERT(ro == &iproute); 238 *ro = dn_pkt->ro; /* structure copy */ 239 KKASSERT(ro->ro_rt == NULL || ro->ro_rt->rt_cpuid == mycpuid); 240 241 dst = dn_pkt->dn_dst; 242 if (dst == (struct sockaddr_in *)&(dn_pkt->ro.ro_dst)) { 243 /* If 'dst' points into dummynet tag, adjust it */ 244 dst = (struct sockaddr_in *)&(ro->ro_dst); 245 } 246 247 ip = mtod(m, struct ip *); 248 hlen = IP_VHL_HL(ip->ip_vhl) << 2 ; 249 if (ro->ro_rt) 250 ia = ifatoia(ro->ro_rt->rt_ifa); 251 goto sendit; 252 } 253 254 if (opt) { 255 len = 0; 256 m = ip_insertoptions(m, opt, &len); 257 if (len != 0) 258 hlen = len; 259 } 260 ip = mtod(m, struct ip *); 261 262 /* 263 * Fill in IP header. 264 */ 265 if (!(flags & (IP_FORWARDING|IP_RAWOUTPUT))) { 266 ip->ip_vhl = IP_MAKE_VHL(IPVERSION, hlen >> 2); 267 ip->ip_off &= IP_DF; 268 if (ip_do_rfc6864 && (ip->ip_off & IP_DF)) 269 ip->ip_id = 0; 270 else 271 ip->ip_id = ip_newid(); 272 ipstat.ips_localout++; 273 } else { 274 hlen = IP_VHL_HL(ip->ip_vhl) << 2; 275 } 276 277 reroute: 278 pkt_dst = next_hop ? next_hop->sin_addr : ip->ip_dst; 279 280 dst = (struct sockaddr_in *)&ro->ro_dst; 281 /* 282 * If there is a cached route, 283 * check that it is to the same destination 284 * and is still up. If not, free it and try again. 285 * The address family should also be checked in case of sharing the 286 * cache with IPv6. 287 */ 288 if (ro->ro_rt && 289 (!(ro->ro_rt->rt_flags & RTF_UP) || 290 dst->sin_family != AF_INET || 291 dst->sin_addr.s_addr != pkt_dst.s_addr)) { 292 rtfree(ro->ro_rt); 293 ro->ro_rt = NULL; 294 } 295 if (ro->ro_rt == NULL) { 296 bzero(dst, sizeof *dst); 297 dst->sin_family = AF_INET; 298 dst->sin_len = sizeof *dst; 299 dst->sin_addr = pkt_dst; 300 } 301 /* 302 * If routing to interface only, 303 * short circuit routing lookup. 304 */ 305 if (flags & IP_ROUTETOIF) { 306 if ((ia = ifatoia(ifa_ifwithdstaddr(sintosa(dst)))) == NULL && 307 (ia = ifatoia(ifa_ifwithnet(sintosa(dst)))) == NULL) { 308 ipstat.ips_noroute++; 309 error = ENETUNREACH; 310 goto bad; 311 } 312 ifp = ia->ia_ifp; 313 ip->ip_ttl = 1; 314 isbroadcast = in_broadcast(dst->sin_addr, ifp); 315 } else if (IN_MULTICAST(ntohl(pkt_dst.s_addr)) && 316 imo != NULL && imo->imo_multicast_ifp != NULL) { 317 /* 318 * Bypass the normal routing lookup for multicast 319 * packets if the interface is specified. 320 */ 321 ifp = imo->imo_multicast_ifp; 322 ia = IFP_TO_IA(ifp); 323 isbroadcast = 0; /* fool gcc */ 324 } else { 325 /* 326 * If this is the case, we probably don't want to allocate 327 * a protocol-cloned route since we didn't get one from the 328 * ULP. This lets TCP do its thing, while not burdening 329 * forwarding or ICMP with the overhead of cloning a route. 330 * Of course, we still want to do any cloning requested by 331 * the link layer, as this is probably required in all cases 332 * for correct operation (as it is for ARP). 333 */ 334 if (ro->ro_rt == NULL) 335 rtalloc_ign(ro, RTF_PRCLONING); 336 if (ro->ro_rt == NULL) { 337 ipstat.ips_noroute++; 338 error = EHOSTUNREACH; 339 goto bad; 340 } 341 ia = ifatoia(ro->ro_rt->rt_ifa); 342 ifp = ro->ro_rt->rt_ifp; 343 ro->ro_rt->rt_use++; 344 if (ro->ro_rt->rt_flags & RTF_GATEWAY) 345 dst = (struct sockaddr_in *)ro->ro_rt->rt_gateway; 346 if (ro->ro_rt->rt_flags & RTF_HOST) 347 isbroadcast = (ro->ro_rt->rt_flags & RTF_BROADCAST); 348 else 349 isbroadcast = in_broadcast(dst->sin_addr, ifp); 350 } 351 if (IN_MULTICAST(ntohl(pkt_dst.s_addr))) { 352 m->m_flags |= M_MCAST; 353 /* 354 * IP destination address is multicast. Make sure "dst" 355 * still points to the address in "ro". (It may have been 356 * changed to point to a gateway address, above.) 357 */ 358 dst = (struct sockaddr_in *)&ro->ro_dst; 359 /* 360 * See if the caller provided any multicast options 361 */ 362 if (imo != NULL) { 363 ip->ip_ttl = imo->imo_multicast_ttl; 364 if (imo->imo_multicast_vif != -1) { 365 ip->ip_src.s_addr = 366 ip_mcast_src ? 367 ip_mcast_src(imo->imo_multicast_vif) : 368 INADDR_ANY; 369 } 370 } else { 371 ip->ip_ttl = IP_DEFAULT_MULTICAST_TTL; 372 } 373 /* 374 * Confirm that the outgoing interface supports multicast. 375 */ 376 if ((imo == NULL) || (imo->imo_multicast_vif == -1)) { 377 if (!(ifp->if_flags & IFF_MULTICAST)) { 378 ipstat.ips_noroute++; 379 error = ENETUNREACH; 380 goto bad; 381 } 382 } 383 /* 384 * If source address not specified yet, use address of the 385 * outgoing interface. In case, keep note we did that, so 386 * if the the firewall changes the next-hop causing the 387 * output interface to change, we can fix that. 388 */ 389 if (ip->ip_src.s_addr == INADDR_ANY || src_was_INADDR_ANY) { 390 /* Interface may have no addresses. */ 391 if (ia != NULL) { 392 ip->ip_src = IA_SIN(ia)->sin_addr; 393 src_was_INADDR_ANY = 1; 394 } 395 } 396 397 if (ip->ip_src.s_addr != INADDR_ANY) { 398 struct in_multi *inm; 399 400 inm = IN_LOOKUP_MULTI(&pkt_dst, ifp); 401 if (inm != NULL && 402 (imo == NULL || imo->imo_multicast_loop)) { 403 /* 404 * If we belong to the destination multicast 405 * group on the outgoing interface, and the 406 * caller did not forbid loopback, loop back 407 * a copy. 408 */ 409 ip_mloopback(ifp, m, dst, hlen); 410 } else { 411 /* 412 * If we are acting as a multicast router, 413 * perform multicast forwarding as if the 414 * packet had just arrived on the interface 415 * to which we are about to send. The 416 * multicast forwarding function recursively 417 * calls this function, using the IP_FORWARDING 418 * flag to prevent infinite recursion. 419 * 420 * Multicasts that are looped back by 421 * ip_mloopback(), above, will be forwarded by 422 * the ip_input() routine, if necessary. 423 */ 424 if (ip_mrouter && !(flags & IP_FORWARDING)) { 425 /* 426 * If rsvp daemon is not running, do 427 * not set ip_moptions. This ensures 428 * that the packet is multicast and 429 * not just sent down one link as 430 * prescribed by rsvpd. 431 */ 432 if (!rsvp_on) 433 imo = NULL; 434 if (ip_mforward) { 435 get_mplock(); 436 if (ip_mforward(ip, ifp, 437 m, imo) != 0) { 438 m_freem(m); 439 rel_mplock(); 440 goto done; 441 } 442 rel_mplock(); 443 } 444 } 445 } 446 } 447 448 /* 449 * Multicasts with a time-to-live of zero may be looped- 450 * back, above, but must not be transmitted on a network. 451 * Also, multicasts addressed to the loopback interface 452 * are not sent -- the above call to ip_mloopback() will 453 * loop back a copy if this host actually belongs to the 454 * destination group on the loopback interface. 455 */ 456 if (ip->ip_ttl == 0 || ifp->if_flags & IFF_LOOPBACK) { 457 m_freem(m); 458 goto done; 459 } 460 461 goto sendit; 462 } else { 463 m->m_flags &= ~M_MCAST; 464 } 465 466 /* 467 * If the source address is not specified yet, use the address 468 * of the outgoing interface. In case, keep note we did that, 469 * so if the the firewall changes the next-hop causing the output 470 * interface to change, we can fix that. 471 */ 472 if (ip->ip_src.s_addr == INADDR_ANY || src_was_INADDR_ANY) { 473 /* Interface may have no addresses. */ 474 if (ia != NULL) { 475 ip->ip_src = IA_SIN(ia)->sin_addr; 476 src_was_INADDR_ANY = 1; 477 } 478 } 479 480 /* 481 * Look for broadcast address and 482 * verify user is allowed to send 483 * such a packet. 484 */ 485 if (isbroadcast) { 486 if (!(ifp->if_flags & IFF_BROADCAST)) { 487 error = EADDRNOTAVAIL; 488 goto bad; 489 } 490 if (!(flags & IP_ALLOWBROADCAST)) { 491 error = EACCES; 492 goto bad; 493 } 494 /* don't allow broadcast messages to be fragmented */ 495 if (ip->ip_len > ifp->if_mtu) { 496 error = EMSGSIZE; 497 goto bad; 498 } 499 m->m_flags |= M_BCAST; 500 } else { 501 m->m_flags &= ~M_BCAST; 502 } 503 504 sendit: 505 506 /* We are already being fwd'd from a firewall. */ 507 if (next_hop != NULL) 508 goto pass; 509 510 /* No pfil hooks */ 511 if (!pfil_has_hooks(&inet_pfil_hook)) { 512 if (m->m_pkthdr.fw_flags & DUMMYNET_MBUF_TAGGED) { 513 /* 514 * Strip dummynet tags from stranded packets 515 */ 516 mtag = m_tag_find(m, PACKET_TAG_DUMMYNET, NULL); 517 KKASSERT(mtag != NULL); 518 m_tag_delete(m, mtag); 519 m->m_pkthdr.fw_flags &= ~DUMMYNET_MBUF_TAGGED; 520 } 521 goto pass; 522 } 523 524 /* 525 * IpHack's section. 526 * - Xlate: translate packet's addr/port (NAT). 527 * - Firewall: deny/allow/etc. 528 * - Wrap: fake packet's addr/port <unimpl.> 529 * - Encapsulate: put it in another IP and send out. <unimp.> 530 */ 531 532 /* 533 * Run through list of hooks for output packets. 534 */ 535 error = pfil_run_hooks(&inet_pfil_hook, &m, ifp, PFIL_OUT); 536 if (error != 0 || m == NULL) 537 goto done; 538 ip = mtod(m, struct ip *); 539 540 if (m->m_pkthdr.fw_flags & IPFORWARD_MBUF_TAGGED) { 541 /* 542 * Check dst to make sure it is directly reachable on the 543 * interface we previously thought it was. 544 * If it isn't (which may be likely in some situations) we have 545 * to re-route it (ie, find a route for the next-hop and the 546 * associated interface) and set them here. This is nested 547 * forwarding which in most cases is undesirable, except where 548 * such control is nigh impossible. So we do it here. 549 * And I'm babbling. 550 */ 551 mtag = m_tag_find(m, PACKET_TAG_IPFORWARD, NULL); 552 KKASSERT(mtag != NULL); 553 next_hop = m_tag_data(mtag); 554 555 /* 556 * Try local forwarding first 557 */ 558 if (ip_localforward(m, next_hop, hlen)) 559 goto done; 560 561 /* 562 * Relocate the route based on next_hop. 563 * If the current route is inp's cache, keep it untouched. 564 */ 565 if (ro == &iproute && ro->ro_rt != NULL) { 566 RTFREE(ro->ro_rt); 567 ro->ro_rt = NULL; 568 } 569 ro = &iproute; 570 bzero(ro, sizeof *ro); 571 572 /* 573 * Forwarding to broadcast address is not allowed. 574 * XXX Should we follow IP_ROUTETOIF? 575 */ 576 flags &= ~(IP_ALLOWBROADCAST | IP_ROUTETOIF); 577 578 /* We are doing forwarding now */ 579 flags |= IP_FORWARDING; 580 581 goto reroute; 582 } 583 584 if (m->m_pkthdr.fw_flags & DUMMYNET_MBUF_TAGGED) { 585 struct dn_pkt *dn_pkt; 586 587 mtag = m_tag_find(m, PACKET_TAG_DUMMYNET, NULL); 588 KKASSERT(mtag != NULL); 589 dn_pkt = m_tag_data(mtag); 590 591 /* 592 * Under certain cases it is not possible to recalculate 593 * 'ro' and 'dst', let alone 'flags', so just save them in 594 * dummynet tag and avoid the possible wrong reculcalation 595 * when we come back to ip_output() again. 596 * 597 * All other parameters have been already used and so they 598 * are not needed anymore. 599 * XXX if the ifp is deleted while a pkt is in dummynet, 600 * we are in trouble! (TODO use ifnet_detach_event) 601 * 602 * We need to copy *ro because for ICMP pkts (and maybe 603 * others) the caller passed a pointer into the stack; 604 * dst might also be a pointer into *ro so it needs to 605 * be updated. 606 */ 607 dn_pkt->ro = *ro; 608 if (ro->ro_rt) 609 ro->ro_rt->rt_refcnt++; 610 if (dst == (struct sockaddr_in *)&ro->ro_dst) { 611 /* 'dst' points into 'ro' */ 612 dst = (struct sockaddr_in *)&(dn_pkt->ro.ro_dst); 613 } 614 dn_pkt->dn_dst = dst; 615 dn_pkt->flags = flags; 616 617 ip_dn_queue(m); 618 goto done; 619 } 620 621 if (m->m_pkthdr.fw_flags & IPFW_MBUF_CONTINUE) { 622 /* ipfw was disabled/unloaded. */ 623 m_freem(m); 624 goto done; 625 } 626 pass: 627 /* 127/8 must not appear on wire - RFC1122. */ 628 if ((ntohl(ip->ip_dst.s_addr) >> IN_CLASSA_NSHIFT) == IN_LOOPBACKNET || 629 (ntohl(ip->ip_src.s_addr) >> IN_CLASSA_NSHIFT) == IN_LOOPBACKNET) { 630 if (!(ifp->if_flags & IFF_LOOPBACK)) { 631 ipstat.ips_badaddr++; 632 error = EADDRNOTAVAIL; 633 goto bad; 634 } 635 } 636 if (ip->ip_src.s_addr == INADDR_ANY || 637 IN_MULTICAST(ntohl(ip->ip_src.s_addr))) { 638 ipstat.ips_badaddr++; 639 error = EADDRNOTAVAIL; 640 goto bad; 641 } 642 643 if ((m->m_pkthdr.csum_flags & CSUM_TSO) == 0) { 644 m->m_pkthdr.csum_flags |= CSUM_IP; 645 sw_csum = m->m_pkthdr.csum_flags & ~ifp->if_hwassist; 646 if (sw_csum & CSUM_DELAY_DATA) { 647 in_delayed_cksum(m); 648 sw_csum &= ~CSUM_DELAY_DATA; 649 } 650 m->m_pkthdr.csum_flags &= ifp->if_hwassist; 651 } else { 652 sw_csum = 0; 653 } 654 m->m_pkthdr.csum_iphlen = hlen; 655 656 /* 657 * If small enough for interface, or the interface will take 658 * care of the fragmentation or segmentation for us, can just 659 * send directly. 660 */ 661 if (ip->ip_len <= ifp->if_mtu || 662 ((ifp->if_hwassist & CSUM_FRAGMENT) && !(ip->ip_off & IP_DF)) || 663 (m->m_pkthdr.csum_flags & CSUM_TSO)) { 664 ip->ip_len = htons(ip->ip_len); 665 ip->ip_off = htons(ip->ip_off); 666 ip->ip_sum = 0; 667 if (sw_csum & CSUM_DELAY_IP) { 668 if (ip->ip_vhl == IP_VHL_BORING) 669 ip->ip_sum = in_cksum_hdr(ip); 670 else 671 ip->ip_sum = in_cksum(m, hlen); 672 } 673 674 /* Record statistics for this interface address. */ 675 if (!(flags & IP_FORWARDING) && ia) { 676 IFA_STAT_INC(&ia->ia_ifa, opackets, 1); 677 IFA_STAT_INC(&ia->ia_ifa, obytes, m->m_pkthdr.len); 678 } 679 680 #ifdef MBUF_STRESS_TEST 681 if (mbuf_frag_size && m->m_pkthdr.len > mbuf_frag_size) { 682 struct mbuf *m1, *m2; 683 int length, tmp; 684 685 tmp = length = m->m_pkthdr.len; 686 687 while ((length -= mbuf_frag_size) >= 1) { 688 m1 = m_split(m, length, M_NOWAIT); 689 if (m1 == NULL) 690 break; 691 m2 = m; 692 while (m2->m_next != NULL) 693 m2 = m2->m_next; 694 m2->m_next = m1; 695 } 696 m->m_pkthdr.len = tmp; 697 } 698 #endif 699 700 #ifdef MPLS 701 if (!mpls_output_process(m, ro->ro_rt)) 702 goto done; 703 #endif 704 error = ifp->if_output(ifp, m, (struct sockaddr *)dst, 705 ro->ro_rt); 706 goto done; 707 } 708 709 if (ip->ip_off & IP_DF) { 710 error = EMSGSIZE; 711 /* 712 * This case can happen if the user changed the MTU 713 * of an interface after enabling IP on it. Because 714 * most netifs don't keep track of routes pointing to 715 * them, there is no way for one to update all its 716 * routes when the MTU is changed. 717 */ 718 if ((ro->ro_rt->rt_flags & (RTF_UP | RTF_HOST)) && 719 !(ro->ro_rt->rt_rmx.rmx_locks & RTV_MTU) && 720 (ro->ro_rt->rt_rmx.rmx_mtu > ifp->if_mtu)) { 721 ro->ro_rt->rt_rmx.rmx_mtu = ifp->if_mtu; 722 } 723 ipstat.ips_cantfrag++; 724 goto bad; 725 } 726 727 /* 728 * Too large for interface; fragment if possible. If successful, 729 * on return, m will point to a list of packets to be sent. 730 */ 731 error = ip_fragment(ip, &m, ifp->if_mtu, ifp->if_hwassist, sw_csum); 732 if (error) 733 goto bad; 734 for (; m; m = m0) { 735 m0 = m->m_nextpkt; 736 m->m_nextpkt = NULL; 737 if (error == 0) { 738 /* Record statistics for this interface address. */ 739 if (ia != NULL) { 740 IFA_STAT_INC(&ia->ia_ifa, opackets, 1); 741 IFA_STAT_INC(&ia->ia_ifa, obytes, 742 m->m_pkthdr.len); 743 } 744 #ifdef MPLS 745 if (!mpls_output_process(m, ro->ro_rt)) 746 continue; 747 #endif 748 error = ifp->if_output(ifp, m, (struct sockaddr *)dst, 749 ro->ro_rt); 750 } else { 751 m_freem(m); 752 } 753 } 754 755 if (error == 0) 756 ipstat.ips_fragmented++; 757 758 done: 759 if (ro == &iproute && ro->ro_rt != NULL) { 760 RTFREE(ro->ro_rt); 761 ro->ro_rt = NULL; 762 } 763 return (error); 764 bad: 765 m_freem(m); 766 goto done; 767 } 768 769 /* 770 * Create a chain of fragments which fit the given mtu. m_frag points to the 771 * mbuf to be fragmented; on return it points to the chain with the fragments. 772 * Return 0 if no error. If error, m_frag may contain a partially built 773 * chain of fragments that should be freed by the caller. 774 * 775 * if_hwassist_flags is the hw offload capabilities (see if_data.ifi_hwassist) 776 * sw_csum contains the delayed checksums flags (e.g., CSUM_DELAY_IP). 777 */ 778 int 779 ip_fragment(struct ip *ip, struct mbuf **m_frag, int mtu, 780 u_long if_hwassist_flags, int sw_csum) 781 { 782 int error = 0; 783 int hlen = IP_VHL_HL(ip->ip_vhl) << 2; 784 int len = (mtu - hlen) & ~7; /* size of payload in each fragment */ 785 int off; 786 struct mbuf *m0 = *m_frag; /* the original packet */ 787 int firstlen; 788 struct mbuf **mnext; 789 int nfrags; 790 791 if (ip->ip_off & IP_DF) { /* Fragmentation not allowed */ 792 ipstat.ips_cantfrag++; 793 return EMSGSIZE; 794 } 795 796 /* 797 * Must be able to put at least 8 bytes per fragment. 798 */ 799 if (len < 8) 800 return EMSGSIZE; 801 802 /* 803 * If the interface will not calculate checksums on 804 * fragmented packets, then do it here. 805 */ 806 if ((m0->m_pkthdr.csum_flags & CSUM_DELAY_DATA) && 807 !(if_hwassist_flags & CSUM_IP_FRAGS)) { 808 in_delayed_cksum(m0); 809 m0->m_pkthdr.csum_flags &= ~CSUM_DELAY_DATA; 810 } 811 812 if (len > PAGE_SIZE) { 813 /* 814 * Fragment large datagrams such that each segment 815 * contains a multiple of PAGE_SIZE amount of data, 816 * plus headers. This enables a receiver to perform 817 * page-flipping zero-copy optimizations. 818 * 819 * XXX When does this help given that sender and receiver 820 * could have different page sizes, and also mtu could 821 * be less than the receiver's page size ? 822 */ 823 int newlen; 824 struct mbuf *m; 825 826 for (m = m0, off = 0; m && (off+m->m_len) <= mtu; m = m->m_next) 827 off += m->m_len; 828 829 /* 830 * firstlen (off - hlen) must be aligned on an 831 * 8-byte boundary 832 */ 833 if (off < hlen) 834 goto smart_frag_failure; 835 off = ((off - hlen) & ~7) + hlen; 836 newlen = (~PAGE_MASK) & mtu; 837 if ((newlen + sizeof(struct ip)) > mtu) { 838 /* we failed, go back the default */ 839 smart_frag_failure: 840 newlen = len; 841 off = hlen + len; 842 } 843 len = newlen; 844 845 } else { 846 off = hlen + len; 847 } 848 849 firstlen = off - hlen; 850 mnext = &m0->m_nextpkt; /* pointer to next packet */ 851 852 /* 853 * Loop through length of segment after first fragment, 854 * make new header and copy data of each part and link onto chain. 855 * Here, m0 is the original packet, m is the fragment being created. 856 * The fragments are linked off the m_nextpkt of the original 857 * packet, which after processing serves as the first fragment. 858 */ 859 for (nfrags = 1; off < ip->ip_len; off += len, nfrags++) { 860 struct ip *mhip; /* ip header on the fragment */ 861 struct mbuf *m; 862 int mhlen = sizeof(struct ip); 863 864 MGETHDR(m, M_NOWAIT, MT_HEADER); 865 if (m == NULL) { 866 error = ENOBUFS; 867 ipstat.ips_odropped++; 868 goto done; 869 } 870 m->m_flags |= (m0->m_flags & M_MCAST) | M_FRAG; 871 /* 872 * In the first mbuf, leave room for the link header, then 873 * copy the original IP header including options. The payload 874 * goes into an additional mbuf chain returned by m_copy(). 875 */ 876 m->m_data += max_linkhdr; 877 mhip = mtod(m, struct ip *); 878 *mhip = *ip; 879 if (hlen > sizeof(struct ip)) { 880 mhlen = ip_optcopy(ip, mhip) + sizeof(struct ip); 881 mhip->ip_vhl = IP_MAKE_VHL(IPVERSION, mhlen >> 2); 882 } 883 m->m_len = mhlen; 884 /* XXX do we need to add ip->ip_off below ? */ 885 mhip->ip_off = ((off - hlen) >> 3) + ip->ip_off; 886 if (off + len >= ip->ip_len) { /* last fragment */ 887 len = ip->ip_len - off; 888 m->m_flags |= M_LASTFRAG; 889 } else 890 mhip->ip_off |= IP_MF; 891 mhip->ip_len = htons((u_short)(len + mhlen)); 892 m->m_next = m_copy(m0, off, len); 893 if (m->m_next == NULL) { /* copy failed */ 894 m_free(m); 895 error = ENOBUFS; /* ??? */ 896 ipstat.ips_odropped++; 897 goto done; 898 } 899 m->m_pkthdr.len = mhlen + len; 900 m->m_pkthdr.rcvif = NULL; 901 m->m_pkthdr.csum_flags = m0->m_pkthdr.csum_flags; 902 m->m_pkthdr.csum_iphlen = mhlen; 903 mhip->ip_off = htons(mhip->ip_off); 904 mhip->ip_sum = 0; 905 if (sw_csum & CSUM_DELAY_IP) 906 mhip->ip_sum = in_cksum(m, mhlen); 907 *mnext = m; 908 mnext = &m->m_nextpkt; 909 } 910 ipstat.ips_ofragments += nfrags; 911 912 /* set first marker for fragment chain */ 913 m0->m_flags |= M_FIRSTFRAG | M_FRAG; 914 m0->m_pkthdr.csum_data = nfrags; 915 916 /* 917 * Update first fragment by trimming what's been copied out 918 * and updating header. 919 */ 920 m_adj(m0, hlen + firstlen - ip->ip_len); 921 m0->m_pkthdr.len = hlen + firstlen; 922 ip->ip_len = htons((u_short)m0->m_pkthdr.len); 923 ip->ip_off |= IP_MF; 924 ip->ip_off = htons(ip->ip_off); 925 ip->ip_sum = 0; 926 if (sw_csum & CSUM_DELAY_IP) 927 ip->ip_sum = in_cksum(m0, hlen); 928 929 done: 930 *m_frag = m0; 931 return error; 932 } 933 934 void 935 in_delayed_cksum(struct mbuf *m) 936 { 937 struct ip *ip; 938 u_short csum, offset; 939 940 ip = mtod(m, struct ip *); 941 offset = IP_VHL_HL(ip->ip_vhl) << 2 ; 942 csum = in_cksum_skip(m, ip->ip_len, offset); 943 if (m->m_pkthdr.csum_flags & CSUM_UDP && csum == 0) 944 csum = 0xffff; 945 offset += m->m_pkthdr.csum_data; /* checksum offset */ 946 947 if (offset + sizeof(u_short) > m->m_len) { 948 kprintf("delayed m_pullup, m->len: %d off: %d p: %d\n", 949 m->m_len, offset, ip->ip_p); 950 /* 951 * XXX 952 * this shouldn't happen, but if it does, the 953 * correct behavior may be to insert the checksum 954 * in the existing chain instead of rearranging it. 955 */ 956 m = m_pullup(m, offset + sizeof(u_short)); 957 } 958 *(u_short *)(m->m_data + offset) = csum; 959 } 960 961 /* 962 * Insert IP options into preformed packet. 963 * Adjust IP destination as required for IP source routing, 964 * as indicated by a non-zero in_addr at the start of the options. 965 * 966 * XXX This routine assumes that the packet has no options in place. 967 */ 968 static struct mbuf * 969 ip_insertoptions(struct mbuf *m, struct mbuf *opt, int *phlen) 970 { 971 struct ipoption *p = mtod(opt, struct ipoption *); 972 struct mbuf *n; 973 struct ip *ip = mtod(m, struct ip *); 974 unsigned optlen; 975 976 optlen = opt->m_len - sizeof p->ipopt_dst; 977 if (optlen + (u_short)ip->ip_len > IP_MAXPACKET) { 978 *phlen = 0; 979 return (m); /* XXX should fail */ 980 } 981 if (p->ipopt_dst.s_addr) 982 ip->ip_dst = p->ipopt_dst; 983 if (m->m_flags & M_EXT || m->m_data - optlen < m->m_pktdat) { 984 MGETHDR(n, M_NOWAIT, MT_HEADER); 985 if (n == NULL) { 986 *phlen = 0; 987 return (m); 988 } 989 n->m_pkthdr.rcvif = NULL; 990 n->m_pkthdr.len = m->m_pkthdr.len + optlen; 991 m->m_len -= sizeof(struct ip); 992 m->m_data += sizeof(struct ip); 993 n->m_next = m; 994 m = n; 995 m->m_len = optlen + sizeof(struct ip); 996 m->m_data += max_linkhdr; 997 memcpy(mtod(m, void *), ip, sizeof(struct ip)); 998 } else { 999 m->m_data -= optlen; 1000 m->m_len += optlen; 1001 m->m_pkthdr.len += optlen; 1002 bcopy(ip, mtod(m, caddr_t), sizeof(struct ip)); 1003 } 1004 ip = mtod(m, struct ip *); 1005 bcopy(p->ipopt_list, ip + 1, optlen); 1006 *phlen = sizeof(struct ip) + optlen; 1007 ip->ip_vhl = IP_MAKE_VHL(IPVERSION, *phlen >> 2); 1008 ip->ip_len += optlen; 1009 return (m); 1010 } 1011 1012 /* 1013 * Copy options from ip to jp, 1014 * omitting those not copied during fragmentation. 1015 */ 1016 int 1017 ip_optcopy(struct ip *ip, struct ip *jp) 1018 { 1019 u_char *cp, *dp; 1020 int opt, optlen, cnt; 1021 1022 cp = (u_char *)(ip + 1); 1023 dp = (u_char *)(jp + 1); 1024 cnt = (IP_VHL_HL(ip->ip_vhl) << 2) - sizeof(struct ip); 1025 for (; cnt > 0; cnt -= optlen, cp += optlen) { 1026 opt = cp[0]; 1027 if (opt == IPOPT_EOL) 1028 break; 1029 if (opt == IPOPT_NOP) { 1030 /* Preserve for IP mcast tunnel's LSRR alignment. */ 1031 *dp++ = IPOPT_NOP; 1032 optlen = 1; 1033 continue; 1034 } 1035 1036 KASSERT(cnt >= IPOPT_OLEN + sizeof *cp, 1037 ("ip_optcopy: malformed ipv4 option")); 1038 optlen = cp[IPOPT_OLEN]; 1039 KASSERT(optlen >= IPOPT_OLEN + sizeof *cp && optlen <= cnt, 1040 ("ip_optcopy: malformed ipv4 option")); 1041 1042 /* bogus lengths should have been caught by ip_dooptions */ 1043 if (optlen > cnt) 1044 optlen = cnt; 1045 if (IPOPT_COPIED(opt)) { 1046 bcopy(cp, dp, optlen); 1047 dp += optlen; 1048 } 1049 } 1050 for (optlen = dp - (u_char *)(jp+1); optlen & 0x3; optlen++) 1051 *dp++ = IPOPT_EOL; 1052 return (optlen); 1053 } 1054 1055 /* 1056 * IP socket option processing. 1057 */ 1058 void 1059 ip_ctloutput(netmsg_t msg) 1060 { 1061 struct socket *so = msg->base.nm_so; 1062 struct sockopt *sopt = msg->ctloutput.nm_sopt; 1063 struct inpcb *inp = so->so_pcb; 1064 int error, optval; 1065 1066 error = optval = 0; 1067 1068 /* Get socket's owner cpuid hint */ 1069 if (sopt->sopt_level == SOL_SOCKET && 1070 sopt->sopt_dir == SOPT_GET && 1071 sopt->sopt_name == SO_CPUHINT) { 1072 optval = mycpuid; 1073 soopt_from_kbuf(sopt, &optval, sizeof(optval)); 1074 goto done; 1075 } 1076 1077 if (sopt->sopt_level != IPPROTO_IP) { 1078 error = EINVAL; 1079 goto done; 1080 } 1081 1082 switch (sopt->sopt_name) { 1083 case IP_MULTICAST_IF: 1084 case IP_MULTICAST_VIF: 1085 case IP_MULTICAST_TTL: 1086 case IP_MULTICAST_LOOP: 1087 case IP_ADD_MEMBERSHIP: 1088 case IP_DROP_MEMBERSHIP: 1089 /* 1090 * Handle multicast options in netisr0 1091 */ 1092 if (&curthread->td_msgport != netisr_cpuport(0)) { 1093 /* NOTE: so_port MUST NOT be checked in netisr0 */ 1094 msg->lmsg.ms_flags |= MSGF_IGNSOPORT; 1095 lwkt_forwardmsg(netisr_cpuport(0), &msg->lmsg); 1096 return; 1097 } 1098 break; 1099 } 1100 1101 switch (sopt->sopt_dir) { 1102 case SOPT_SET: 1103 switch (sopt->sopt_name) { 1104 case IP_OPTIONS: 1105 #ifdef notyet 1106 case IP_RETOPTS: 1107 #endif 1108 { 1109 struct mbuf *m; 1110 if (sopt->sopt_valsize > MLEN) { 1111 error = EMSGSIZE; 1112 break; 1113 } 1114 MGET(m, sopt->sopt_td ? M_WAITOK : M_NOWAIT, MT_HEADER); 1115 if (m == NULL) { 1116 error = ENOBUFS; 1117 break; 1118 } 1119 m->m_len = sopt->sopt_valsize; 1120 error = soopt_to_kbuf(sopt, mtod(m, void *), m->m_len, 1121 m->m_len); 1122 error = ip_pcbopts(sopt->sopt_name, 1123 &inp->inp_options, m); 1124 goto done; 1125 } 1126 1127 case IP_TOS: 1128 case IP_TTL: 1129 case IP_MINTTL: 1130 case IP_RECVOPTS: 1131 case IP_RECVRETOPTS: 1132 case IP_RECVDSTADDR: 1133 case IP_RECVIF: 1134 case IP_RECVTTL: 1135 error = soopt_to_kbuf(sopt, &optval, sizeof optval, 1136 sizeof optval); 1137 if (error) 1138 break; 1139 switch (sopt->sopt_name) { 1140 case IP_TOS: 1141 inp->inp_ip_tos = optval; 1142 break; 1143 1144 case IP_TTL: 1145 inp->inp_ip_ttl = optval; 1146 break; 1147 case IP_MINTTL: 1148 if (optval >= 0 && optval <= MAXTTL) 1149 inp->inp_ip_minttl = optval; 1150 else 1151 error = EINVAL; 1152 break; 1153 #define OPTSET(bit) \ 1154 if (optval) \ 1155 inp->inp_flags |= bit; \ 1156 else \ 1157 inp->inp_flags &= ~bit; 1158 1159 case IP_RECVOPTS: 1160 OPTSET(INP_RECVOPTS); 1161 break; 1162 1163 case IP_RECVRETOPTS: 1164 OPTSET(INP_RECVRETOPTS); 1165 break; 1166 1167 case IP_RECVDSTADDR: 1168 OPTSET(INP_RECVDSTADDR); 1169 break; 1170 1171 case IP_RECVIF: 1172 OPTSET(INP_RECVIF); 1173 break; 1174 1175 case IP_RECVTTL: 1176 OPTSET(INP_RECVTTL); 1177 break; 1178 } 1179 break; 1180 #undef OPTSET 1181 1182 case IP_MULTICAST_IF: 1183 case IP_MULTICAST_VIF: 1184 case IP_MULTICAST_TTL: 1185 case IP_MULTICAST_LOOP: 1186 case IP_ADD_MEMBERSHIP: 1187 case IP_DROP_MEMBERSHIP: 1188 error = ip_setmoptions(sopt, &inp->inp_moptions); 1189 break; 1190 1191 case IP_PORTRANGE: 1192 error = soopt_to_kbuf(sopt, &optval, sizeof optval, 1193 sizeof optval); 1194 if (error) 1195 break; 1196 1197 switch (optval) { 1198 case IP_PORTRANGE_DEFAULT: 1199 inp->inp_flags &= ~(INP_LOWPORT); 1200 inp->inp_flags &= ~(INP_HIGHPORT); 1201 break; 1202 1203 case IP_PORTRANGE_HIGH: 1204 inp->inp_flags &= ~(INP_LOWPORT); 1205 inp->inp_flags |= INP_HIGHPORT; 1206 break; 1207 1208 case IP_PORTRANGE_LOW: 1209 inp->inp_flags &= ~(INP_HIGHPORT); 1210 inp->inp_flags |= INP_LOWPORT; 1211 break; 1212 1213 default: 1214 error = EINVAL; 1215 break; 1216 } 1217 break; 1218 1219 1220 default: 1221 error = ENOPROTOOPT; 1222 break; 1223 } 1224 break; 1225 1226 case SOPT_GET: 1227 switch (sopt->sopt_name) { 1228 case IP_OPTIONS: 1229 case IP_RETOPTS: 1230 if (inp->inp_options) 1231 soopt_from_kbuf(sopt, mtod(inp->inp_options, 1232 char *), 1233 inp->inp_options->m_len); 1234 else 1235 sopt->sopt_valsize = 0; 1236 break; 1237 1238 case IP_TOS: 1239 case IP_TTL: 1240 case IP_MINTTL: 1241 case IP_RECVOPTS: 1242 case IP_RECVRETOPTS: 1243 case IP_RECVDSTADDR: 1244 case IP_RECVTTL: 1245 case IP_RECVIF: 1246 case IP_PORTRANGE: 1247 switch (sopt->sopt_name) { 1248 1249 case IP_TOS: 1250 optval = inp->inp_ip_tos; 1251 break; 1252 1253 case IP_TTL: 1254 optval = inp->inp_ip_ttl; 1255 break; 1256 case IP_MINTTL: 1257 optval = inp->inp_ip_minttl; 1258 break; 1259 1260 #define OPTBIT(bit) (inp->inp_flags & bit ? 1 : 0) 1261 1262 case IP_RECVOPTS: 1263 optval = OPTBIT(INP_RECVOPTS); 1264 break; 1265 1266 case IP_RECVRETOPTS: 1267 optval = OPTBIT(INP_RECVRETOPTS); 1268 break; 1269 1270 case IP_RECVDSTADDR: 1271 optval = OPTBIT(INP_RECVDSTADDR); 1272 break; 1273 1274 case IP_RECVTTL: 1275 optval = OPTBIT(INP_RECVTTL); 1276 break; 1277 1278 case IP_RECVIF: 1279 optval = OPTBIT(INP_RECVIF); 1280 break; 1281 1282 case IP_PORTRANGE: 1283 if (inp->inp_flags & INP_HIGHPORT) 1284 optval = IP_PORTRANGE_HIGH; 1285 else if (inp->inp_flags & INP_LOWPORT) 1286 optval = IP_PORTRANGE_LOW; 1287 else 1288 optval = 0; 1289 break; 1290 } 1291 soopt_from_kbuf(sopt, &optval, sizeof optval); 1292 break; 1293 1294 case IP_MULTICAST_IF: 1295 case IP_MULTICAST_VIF: 1296 case IP_MULTICAST_TTL: 1297 case IP_MULTICAST_LOOP: 1298 case IP_ADD_MEMBERSHIP: 1299 case IP_DROP_MEMBERSHIP: 1300 error = ip_getmoptions(sopt, inp->inp_moptions); 1301 break; 1302 1303 default: 1304 error = ENOPROTOOPT; 1305 break; 1306 } 1307 break; 1308 } 1309 done: 1310 lwkt_replymsg(&msg->lmsg, error); 1311 } 1312 1313 /* 1314 * Set up IP options in pcb for insertion in output packets. 1315 * Store in mbuf with pointer in pcbopt, adding pseudo-option 1316 * with destination address if source routed. 1317 */ 1318 static int 1319 ip_pcbopts(int optname, struct mbuf **pcbopt, struct mbuf *m) 1320 { 1321 int cnt, optlen; 1322 u_char *cp; 1323 u_char opt; 1324 1325 /* turn off any old options */ 1326 if (*pcbopt) 1327 m_free(*pcbopt); 1328 *pcbopt = NULL; 1329 if (m == NULL || m->m_len == 0) { 1330 /* 1331 * Only turning off any previous options. 1332 */ 1333 if (m != NULL) 1334 m_free(m); 1335 return (0); 1336 } 1337 1338 if (m->m_len % sizeof(int32_t)) 1339 goto bad; 1340 /* 1341 * IP first-hop destination address will be stored before 1342 * actual options; move other options back 1343 * and clear it when none present. 1344 */ 1345 if (m->m_data + m->m_len + sizeof(struct in_addr) >= &m->m_dat[MLEN]) 1346 goto bad; 1347 cnt = m->m_len; 1348 m->m_len += sizeof(struct in_addr); 1349 cp = mtod(m, u_char *) + sizeof(struct in_addr); 1350 bcopy(mtod(m, caddr_t), cp, cnt); 1351 bzero(mtod(m, caddr_t), sizeof(struct in_addr)); 1352 1353 for (; cnt > 0; cnt -= optlen, cp += optlen) { 1354 opt = cp[IPOPT_OPTVAL]; 1355 if (opt == IPOPT_EOL) 1356 break; 1357 if (opt == IPOPT_NOP) 1358 optlen = 1; 1359 else { 1360 if (cnt < IPOPT_OLEN + sizeof *cp) 1361 goto bad; 1362 optlen = cp[IPOPT_OLEN]; 1363 if (optlen < IPOPT_OLEN + sizeof *cp || optlen > cnt) 1364 goto bad; 1365 } 1366 switch (opt) { 1367 1368 default: 1369 break; 1370 1371 case IPOPT_LSRR: 1372 case IPOPT_SSRR: 1373 /* 1374 * user process specifies route as: 1375 * ->A->B->C->D 1376 * D must be our final destination (but we can't 1377 * check that since we may not have connected yet). 1378 * A is first hop destination, which doesn't appear in 1379 * actual IP option, but is stored before the options. 1380 */ 1381 if (optlen < IPOPT_MINOFF - 1 + sizeof(struct in_addr)) 1382 goto bad; 1383 m->m_len -= sizeof(struct in_addr); 1384 cnt -= sizeof(struct in_addr); 1385 optlen -= sizeof(struct in_addr); 1386 cp[IPOPT_OLEN] = optlen; 1387 /* 1388 * Move first hop before start of options. 1389 */ 1390 bcopy(&cp[IPOPT_OFFSET+1], mtod(m, caddr_t), 1391 sizeof(struct in_addr)); 1392 /* 1393 * Then copy rest of options back 1394 * to close up the deleted entry. 1395 */ 1396 bcopy(&cp[IPOPT_OFFSET+1] + sizeof(struct in_addr), 1397 &cp[IPOPT_OFFSET+1], 1398 cnt - (IPOPT_MINOFF - 1)); 1399 break; 1400 } 1401 } 1402 if (m->m_len > MAX_IPOPTLEN + sizeof(struct in_addr)) 1403 goto bad; 1404 *pcbopt = m; 1405 return (0); 1406 1407 bad: 1408 m_free(m); 1409 return (EINVAL); 1410 } 1411 1412 /* 1413 * XXX 1414 * The whole multicast option thing needs to be re-thought. 1415 * Several of these options are equally applicable to non-multicast 1416 * transmission, and one (IP_MULTICAST_TTL) totally duplicates a 1417 * standard option (IP_TTL). 1418 */ 1419 1420 /* 1421 * following RFC1724 section 3.3, 0.0.0.0/8 is interpreted as interface index. 1422 */ 1423 static struct ifnet * 1424 ip_multicast_if(struct in_addr *a, int *ifindexp) 1425 { 1426 int ifindex; 1427 struct ifnet *ifp; 1428 1429 if (ifindexp) 1430 *ifindexp = 0; 1431 if (ntohl(a->s_addr) >> 24 == 0) { 1432 ifindex = ntohl(a->s_addr) & 0xffffff; 1433 if (ifindex < 0 || if_index < ifindex) 1434 return NULL; 1435 ifp = ifindex2ifnet[ifindex]; 1436 if (ifindexp) 1437 *ifindexp = ifindex; 1438 } else { 1439 ifp = INADDR_TO_IFP(a); 1440 } 1441 return ifp; 1442 } 1443 1444 /* 1445 * Set the IP multicast options in response to user setsockopt(). 1446 */ 1447 static int 1448 ip_setmoptions(struct sockopt *sopt, struct ip_moptions **imop) 1449 { 1450 int error = 0; 1451 int i; 1452 struct in_addr addr; 1453 struct ip_mreq mreq; 1454 struct ifnet *ifp; 1455 struct ip_moptions *imo = *imop; 1456 int ifindex; 1457 1458 if (imo == NULL) { 1459 /* 1460 * No multicast option buffer attached to the pcb; 1461 * allocate one and initialize to default values. 1462 */ 1463 imo = kmalloc(sizeof *imo, M_IPMOPTS, M_WAITOK); 1464 1465 imo->imo_multicast_ifp = NULL; 1466 imo->imo_multicast_addr.s_addr = INADDR_ANY; 1467 imo->imo_multicast_vif = -1; 1468 imo->imo_multicast_ttl = IP_DEFAULT_MULTICAST_TTL; 1469 imo->imo_multicast_loop = IP_DEFAULT_MULTICAST_LOOP; 1470 imo->imo_num_memberships = 0; 1471 /* Assign imo to imop after all fields are setup */ 1472 cpu_sfence(); 1473 *imop = imo; 1474 } 1475 switch (sopt->sopt_name) { 1476 /* store an index number for the vif you wanna use in the send */ 1477 case IP_MULTICAST_VIF: 1478 if (legal_vif_num == 0) { 1479 error = EOPNOTSUPP; 1480 break; 1481 } 1482 error = soopt_to_kbuf(sopt, &i, sizeof i, sizeof i); 1483 if (error) 1484 break; 1485 if (!legal_vif_num(i) && (i != -1)) { 1486 error = EINVAL; 1487 break; 1488 } 1489 imo->imo_multicast_vif = i; 1490 break; 1491 1492 case IP_MULTICAST_IF: 1493 /* 1494 * Select the interface for outgoing multicast packets. 1495 */ 1496 error = soopt_to_kbuf(sopt, &addr, sizeof addr, sizeof addr); 1497 if (error) 1498 break; 1499 1500 /* 1501 * INADDR_ANY is used to remove a previous selection. 1502 * When no interface is selected, a default one is 1503 * chosen every time a multicast packet is sent. 1504 */ 1505 if (addr.s_addr == INADDR_ANY) { 1506 imo->imo_multicast_ifp = NULL; 1507 break; 1508 } 1509 /* 1510 * The selected interface is identified by its local 1511 * IP address. Find the interface and confirm that 1512 * it supports multicasting. 1513 */ 1514 crit_enter(); 1515 ifp = ip_multicast_if(&addr, &ifindex); 1516 if (ifp == NULL || !(ifp->if_flags & IFF_MULTICAST)) { 1517 crit_exit(); 1518 error = EADDRNOTAVAIL; 1519 break; 1520 } 1521 imo->imo_multicast_ifp = ifp; 1522 if (ifindex) 1523 imo->imo_multicast_addr = addr; 1524 else 1525 imo->imo_multicast_addr.s_addr = INADDR_ANY; 1526 crit_exit(); 1527 break; 1528 1529 case IP_MULTICAST_TTL: 1530 /* 1531 * Set the IP time-to-live for outgoing multicast packets. 1532 * The original multicast API required a char argument, 1533 * which is inconsistent with the rest of the socket API. 1534 * We allow either a char or an int. 1535 */ 1536 if (sopt->sopt_valsize == 1) { 1537 u_char ttl; 1538 error = soopt_to_kbuf(sopt, &ttl, 1, 1); 1539 if (error) 1540 break; 1541 imo->imo_multicast_ttl = ttl; 1542 } else { 1543 u_int ttl; 1544 error = soopt_to_kbuf(sopt, &ttl, sizeof ttl, sizeof ttl); 1545 if (error) 1546 break; 1547 if (ttl > 255) 1548 error = EINVAL; 1549 else 1550 imo->imo_multicast_ttl = ttl; 1551 } 1552 break; 1553 1554 case IP_MULTICAST_LOOP: 1555 /* 1556 * Set the loopback flag for outgoing multicast packets. 1557 * Must be zero or one. The original multicast API required a 1558 * char argument, which is inconsistent with the rest 1559 * of the socket API. We allow either a char or an int. 1560 */ 1561 if (sopt->sopt_valsize == 1) { 1562 u_char loop; 1563 1564 error = soopt_to_kbuf(sopt, &loop, 1, 1); 1565 if (error) 1566 break; 1567 imo->imo_multicast_loop = !!loop; 1568 } else { 1569 u_int loop; 1570 1571 error = soopt_to_kbuf(sopt, &loop, sizeof loop, 1572 sizeof loop); 1573 if (error) 1574 break; 1575 imo->imo_multicast_loop = !!loop; 1576 } 1577 break; 1578 1579 case IP_ADD_MEMBERSHIP: 1580 /* 1581 * Add a multicast group membership. 1582 * Group must be a valid IP multicast address. 1583 */ 1584 error = soopt_to_kbuf(sopt, &mreq, sizeof mreq, sizeof mreq); 1585 if (error) 1586 break; 1587 1588 if (!IN_MULTICAST(ntohl(mreq.imr_multiaddr.s_addr))) { 1589 error = EINVAL; 1590 break; 1591 } 1592 crit_enter(); 1593 /* 1594 * If no interface address was provided, use the interface of 1595 * the route to the given multicast address. 1596 */ 1597 if (mreq.imr_interface.s_addr == INADDR_ANY) { 1598 struct sockaddr_in dst; 1599 struct rtentry *rt; 1600 1601 bzero(&dst, sizeof(struct sockaddr_in)); 1602 dst.sin_len = sizeof(struct sockaddr_in); 1603 dst.sin_family = AF_INET; 1604 dst.sin_addr = mreq.imr_multiaddr; 1605 rt = rtlookup((struct sockaddr *)&dst); 1606 if (rt == NULL) { 1607 error = EADDRNOTAVAIL; 1608 crit_exit(); 1609 break; 1610 } 1611 --rt->rt_refcnt; 1612 ifp = rt->rt_ifp; 1613 } else { 1614 ifp = ip_multicast_if(&mreq.imr_interface, NULL); 1615 } 1616 1617 /* 1618 * See if we found an interface, and confirm that it 1619 * supports multicast. 1620 */ 1621 if (ifp == NULL || !(ifp->if_flags & IFF_MULTICAST)) { 1622 error = EADDRNOTAVAIL; 1623 crit_exit(); 1624 break; 1625 } 1626 /* 1627 * See if the membership already exists or if all the 1628 * membership slots are full. 1629 */ 1630 for (i = 0; i < imo->imo_num_memberships; ++i) { 1631 if (imo->imo_membership[i]->inm_ifp == ifp && 1632 imo->imo_membership[i]->inm_addr.s_addr 1633 == mreq.imr_multiaddr.s_addr) 1634 break; 1635 } 1636 if (i < imo->imo_num_memberships) { 1637 error = EADDRINUSE; 1638 crit_exit(); 1639 break; 1640 } 1641 if (i == IP_MAX_MEMBERSHIPS) { 1642 error = ETOOMANYREFS; 1643 crit_exit(); 1644 break; 1645 } 1646 /* 1647 * Everything looks good; add a new record to the multicast 1648 * address list for the given interface. 1649 */ 1650 if ((imo->imo_membership[i] = 1651 in_addmulti(&mreq.imr_multiaddr, ifp)) == NULL) { 1652 error = ENOBUFS; 1653 crit_exit(); 1654 break; 1655 } 1656 ++imo->imo_num_memberships; 1657 crit_exit(); 1658 break; 1659 1660 case IP_DROP_MEMBERSHIP: 1661 /* 1662 * Drop a multicast group membership. 1663 * Group must be a valid IP multicast address. 1664 */ 1665 error = soopt_to_kbuf(sopt, &mreq, sizeof mreq, sizeof mreq); 1666 if (error) 1667 break; 1668 1669 if (!IN_MULTICAST(ntohl(mreq.imr_multiaddr.s_addr))) { 1670 error = EINVAL; 1671 break; 1672 } 1673 1674 crit_enter(); 1675 /* 1676 * If an interface address was specified, get a pointer 1677 * to its ifnet structure. 1678 */ 1679 if (mreq.imr_interface.s_addr == INADDR_ANY) 1680 ifp = NULL; 1681 else { 1682 ifp = ip_multicast_if(&mreq.imr_interface, NULL); 1683 if (ifp == NULL) { 1684 error = EADDRNOTAVAIL; 1685 crit_exit(); 1686 break; 1687 } 1688 } 1689 /* 1690 * Find the membership in the membership array. 1691 */ 1692 for (i = 0; i < imo->imo_num_memberships; ++i) { 1693 if ((ifp == NULL || 1694 imo->imo_membership[i]->inm_ifp == ifp) && 1695 imo->imo_membership[i]->inm_addr.s_addr == 1696 mreq.imr_multiaddr.s_addr) 1697 break; 1698 } 1699 if (i == imo->imo_num_memberships) { 1700 error = EADDRNOTAVAIL; 1701 crit_exit(); 1702 break; 1703 } 1704 /* 1705 * Give up the multicast address record to which the 1706 * membership points. 1707 */ 1708 in_delmulti(imo->imo_membership[i]); 1709 /* 1710 * Remove the gap in the membership array. 1711 */ 1712 for (++i; i < imo->imo_num_memberships; ++i) 1713 imo->imo_membership[i-1] = imo->imo_membership[i]; 1714 --imo->imo_num_memberships; 1715 crit_exit(); 1716 break; 1717 1718 default: 1719 error = EOPNOTSUPP; 1720 break; 1721 } 1722 1723 return (error); 1724 } 1725 1726 /* 1727 * Return the IP multicast options in response to user getsockopt(). 1728 */ 1729 static int 1730 ip_getmoptions(struct sockopt *sopt, struct ip_moptions *imo) 1731 { 1732 struct in_addr addr; 1733 struct in_ifaddr *ia; 1734 int error, optval; 1735 u_char coptval; 1736 1737 error = 0; 1738 switch (sopt->sopt_name) { 1739 case IP_MULTICAST_VIF: 1740 if (imo != NULL) 1741 optval = imo->imo_multicast_vif; 1742 else 1743 optval = -1; 1744 soopt_from_kbuf(sopt, &optval, sizeof optval); 1745 break; 1746 1747 case IP_MULTICAST_IF: 1748 if (imo == NULL || imo->imo_multicast_ifp == NULL) 1749 addr.s_addr = INADDR_ANY; 1750 else if (imo->imo_multicast_addr.s_addr) { 1751 /* return the value user has set */ 1752 addr = imo->imo_multicast_addr; 1753 } else { 1754 ia = IFP_TO_IA(imo->imo_multicast_ifp); 1755 addr.s_addr = (ia == NULL) ? INADDR_ANY 1756 : IA_SIN(ia)->sin_addr.s_addr; 1757 } 1758 soopt_from_kbuf(sopt, &addr, sizeof addr); 1759 break; 1760 1761 case IP_MULTICAST_TTL: 1762 if (imo == NULL) 1763 optval = coptval = IP_DEFAULT_MULTICAST_TTL; 1764 else 1765 optval = coptval = imo->imo_multicast_ttl; 1766 if (sopt->sopt_valsize == 1) 1767 soopt_from_kbuf(sopt, &coptval, 1); 1768 else 1769 soopt_from_kbuf(sopt, &optval, sizeof optval); 1770 break; 1771 1772 case IP_MULTICAST_LOOP: 1773 if (imo == NULL) 1774 optval = coptval = IP_DEFAULT_MULTICAST_LOOP; 1775 else 1776 optval = coptval = imo->imo_multicast_loop; 1777 if (sopt->sopt_valsize == 1) 1778 soopt_from_kbuf(sopt, &coptval, 1); 1779 else 1780 soopt_from_kbuf(sopt, &optval, sizeof optval); 1781 break; 1782 1783 default: 1784 error = ENOPROTOOPT; 1785 break; 1786 } 1787 return (error); 1788 } 1789 1790 /* 1791 * Discard the IP multicast options. 1792 */ 1793 void 1794 ip_freemoptions(struct ip_moptions *imo) 1795 { 1796 int i; 1797 1798 if (imo != NULL) { 1799 for (i = 0; i < imo->imo_num_memberships; ++i) 1800 in_delmulti(imo->imo_membership[i]); 1801 kfree(imo, M_IPMOPTS); 1802 } 1803 } 1804 1805 /* 1806 * Routine called from ip_output() to loop back a copy of an IP multicast 1807 * packet to the input queue of a specified interface. Note that this 1808 * calls the output routine of the loopback "driver", but with an interface 1809 * pointer that might NOT be a loopback interface -- evil, but easier than 1810 * replicating that code here. 1811 */ 1812 static void 1813 ip_mloopback(struct ifnet *ifp, struct mbuf *m, struct sockaddr_in *dst, 1814 int hlen) 1815 { 1816 struct ip *ip; 1817 struct mbuf *copym; 1818 1819 copym = m_copypacket(m, M_NOWAIT); 1820 if (copym != NULL && (copym->m_flags & M_EXT || copym->m_len < hlen)) 1821 copym = m_pullup(copym, hlen); 1822 if (copym != NULL) { 1823 /* 1824 * if the checksum hasn't been computed, mark it as valid 1825 */ 1826 if (copym->m_pkthdr.csum_flags & CSUM_DELAY_DATA) { 1827 in_delayed_cksum(copym); 1828 copym->m_pkthdr.csum_flags &= ~CSUM_DELAY_DATA; 1829 copym->m_pkthdr.csum_flags |= 1830 CSUM_DATA_VALID | CSUM_PSEUDO_HDR; 1831 copym->m_pkthdr.csum_data = 0xffff; 1832 } 1833 /* 1834 * We don't bother to fragment if the IP length is greater 1835 * than the interface's MTU. Can this possibly matter? 1836 */ 1837 ip = mtod(copym, struct ip *); 1838 ip->ip_len = htons(ip->ip_len); 1839 ip->ip_off = htons(ip->ip_off); 1840 ip->ip_sum = 0; 1841 if (ip->ip_vhl == IP_VHL_BORING) { 1842 ip->ip_sum = in_cksum_hdr(ip); 1843 } else { 1844 ip->ip_sum = in_cksum(copym, hlen); 1845 } 1846 /* 1847 * NB: 1848 * It's not clear whether there are any lingering 1849 * reentrancy problems in other areas which might 1850 * be exposed by using ip_input directly (in 1851 * particular, everything which modifies the packet 1852 * in-place). Yet another option is using the 1853 * protosw directly to deliver the looped back 1854 * packet. For the moment, we'll err on the side 1855 * of safety by using if_simloop(). 1856 */ 1857 #if 1 /* XXX */ 1858 if (dst->sin_family != AF_INET) { 1859 kprintf("ip_mloopback: bad address family %d\n", 1860 dst->sin_family); 1861 dst->sin_family = AF_INET; 1862 } 1863 #endif 1864 if_simloop(ifp, copym, dst->sin_family, 0); 1865 } 1866 } 1867