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_RECVTOS: 1135 case IP_RECVTTL: 1136 error = soopt_to_kbuf(sopt, &optval, sizeof optval, 1137 sizeof optval); 1138 if (error) 1139 break; 1140 switch (sopt->sopt_name) { 1141 case IP_TOS: 1142 inp->inp_ip_tos = optval; 1143 break; 1144 1145 case IP_TTL: 1146 inp->inp_ip_ttl = optval; 1147 break; 1148 case IP_MINTTL: 1149 if (optval >= 0 && optval <= MAXTTL) 1150 inp->inp_ip_minttl = optval; 1151 else 1152 error = EINVAL; 1153 break; 1154 #define OPTSET(bit) \ 1155 if (optval) \ 1156 inp->inp_flags |= bit; \ 1157 else \ 1158 inp->inp_flags &= ~bit; 1159 1160 case IP_RECVOPTS: 1161 OPTSET(INP_RECVOPTS); 1162 break; 1163 1164 case IP_RECVRETOPTS: 1165 OPTSET(INP_RECVRETOPTS); 1166 break; 1167 1168 case IP_RECVDSTADDR: 1169 OPTSET(INP_RECVDSTADDR); 1170 break; 1171 1172 case IP_RECVIF: 1173 OPTSET(INP_RECVIF); 1174 break; 1175 1176 case IP_RECVTOS: 1177 OPTSET(INP_RECVTOS); 1178 break; 1179 1180 case IP_RECVTTL: 1181 OPTSET(INP_RECVTTL); 1182 break; 1183 } 1184 break; 1185 #undef OPTSET 1186 1187 case IP_MULTICAST_IF: 1188 case IP_MULTICAST_VIF: 1189 case IP_MULTICAST_TTL: 1190 case IP_MULTICAST_LOOP: 1191 case IP_ADD_MEMBERSHIP: 1192 case IP_DROP_MEMBERSHIP: 1193 error = ip_setmoptions(sopt, &inp->inp_moptions); 1194 break; 1195 1196 case IP_PORTRANGE: 1197 error = soopt_to_kbuf(sopt, &optval, sizeof optval, 1198 sizeof optval); 1199 if (error) 1200 break; 1201 1202 switch (optval) { 1203 case IP_PORTRANGE_DEFAULT: 1204 inp->inp_flags &= ~(INP_LOWPORT); 1205 inp->inp_flags &= ~(INP_HIGHPORT); 1206 break; 1207 1208 case IP_PORTRANGE_HIGH: 1209 inp->inp_flags &= ~(INP_LOWPORT); 1210 inp->inp_flags |= INP_HIGHPORT; 1211 break; 1212 1213 case IP_PORTRANGE_LOW: 1214 inp->inp_flags &= ~(INP_HIGHPORT); 1215 inp->inp_flags |= INP_LOWPORT; 1216 break; 1217 1218 default: 1219 error = EINVAL; 1220 break; 1221 } 1222 break; 1223 1224 1225 default: 1226 error = ENOPROTOOPT; 1227 break; 1228 } 1229 break; 1230 1231 case SOPT_GET: 1232 switch (sopt->sopt_name) { 1233 case IP_OPTIONS: 1234 case IP_RETOPTS: 1235 if (inp->inp_options) 1236 soopt_from_kbuf(sopt, mtod(inp->inp_options, 1237 char *), 1238 inp->inp_options->m_len); 1239 else 1240 sopt->sopt_valsize = 0; 1241 break; 1242 1243 case IP_TOS: 1244 case IP_TTL: 1245 case IP_MINTTL: 1246 case IP_RECVOPTS: 1247 case IP_RECVRETOPTS: 1248 case IP_RECVDSTADDR: 1249 case IP_RECVTOS: 1250 case IP_RECVTTL: 1251 case IP_RECVIF: 1252 case IP_PORTRANGE: 1253 switch (sopt->sopt_name) { 1254 1255 case IP_TOS: 1256 optval = inp->inp_ip_tos; 1257 break; 1258 1259 case IP_TTL: 1260 optval = inp->inp_ip_ttl; 1261 break; 1262 case IP_MINTTL: 1263 optval = inp->inp_ip_minttl; 1264 break; 1265 1266 #define OPTBIT(bit) (inp->inp_flags & bit ? 1 : 0) 1267 1268 case IP_RECVOPTS: 1269 optval = OPTBIT(INP_RECVOPTS); 1270 break; 1271 1272 case IP_RECVRETOPTS: 1273 optval = OPTBIT(INP_RECVRETOPTS); 1274 break; 1275 1276 case IP_RECVDSTADDR: 1277 optval = OPTBIT(INP_RECVDSTADDR); 1278 break; 1279 1280 case IP_RECVTOS: 1281 optval = OPTBIT(INP_RECVTOS); 1282 break; 1283 1284 case IP_RECVTTL: 1285 optval = OPTBIT(INP_RECVTTL); 1286 break; 1287 1288 case IP_RECVIF: 1289 optval = OPTBIT(INP_RECVIF); 1290 break; 1291 1292 case IP_PORTRANGE: 1293 if (inp->inp_flags & INP_HIGHPORT) 1294 optval = IP_PORTRANGE_HIGH; 1295 else if (inp->inp_flags & INP_LOWPORT) 1296 optval = IP_PORTRANGE_LOW; 1297 else 1298 optval = 0; 1299 break; 1300 } 1301 soopt_from_kbuf(sopt, &optval, sizeof optval); 1302 break; 1303 1304 case IP_MULTICAST_IF: 1305 case IP_MULTICAST_VIF: 1306 case IP_MULTICAST_TTL: 1307 case IP_MULTICAST_LOOP: 1308 case IP_ADD_MEMBERSHIP: 1309 case IP_DROP_MEMBERSHIP: 1310 error = ip_getmoptions(sopt, inp->inp_moptions); 1311 break; 1312 1313 default: 1314 error = ENOPROTOOPT; 1315 break; 1316 } 1317 break; 1318 } 1319 done: 1320 lwkt_replymsg(&msg->lmsg, error); 1321 } 1322 1323 /* 1324 * Set up IP options in pcb for insertion in output packets. 1325 * Store in mbuf with pointer in pcbopt, adding pseudo-option 1326 * with destination address if source routed. 1327 */ 1328 static int 1329 ip_pcbopts(int optname, struct mbuf **pcbopt, struct mbuf *m) 1330 { 1331 int cnt, optlen; 1332 u_char *cp; 1333 u_char opt; 1334 1335 /* turn off any old options */ 1336 if (*pcbopt) 1337 m_free(*pcbopt); 1338 *pcbopt = NULL; 1339 if (m == NULL || m->m_len == 0) { 1340 /* 1341 * Only turning off any previous options. 1342 */ 1343 if (m != NULL) 1344 m_free(m); 1345 return (0); 1346 } 1347 1348 if (m->m_len % sizeof(int32_t)) 1349 goto bad; 1350 /* 1351 * IP first-hop destination address will be stored before 1352 * actual options; move other options back 1353 * and clear it when none present. 1354 */ 1355 if (m->m_data + m->m_len + sizeof(struct in_addr) >= &m->m_dat[MLEN]) 1356 goto bad; 1357 cnt = m->m_len; 1358 m->m_len += sizeof(struct in_addr); 1359 cp = mtod(m, u_char *) + sizeof(struct in_addr); 1360 bcopy(mtod(m, caddr_t), cp, cnt); 1361 bzero(mtod(m, caddr_t), sizeof(struct in_addr)); 1362 1363 for (; cnt > 0; cnt -= optlen, cp += optlen) { 1364 opt = cp[IPOPT_OPTVAL]; 1365 if (opt == IPOPT_EOL) 1366 break; 1367 if (opt == IPOPT_NOP) 1368 optlen = 1; 1369 else { 1370 if (cnt < IPOPT_OLEN + sizeof *cp) 1371 goto bad; 1372 optlen = cp[IPOPT_OLEN]; 1373 if (optlen < IPOPT_OLEN + sizeof *cp || optlen > cnt) 1374 goto bad; 1375 } 1376 switch (opt) { 1377 1378 default: 1379 break; 1380 1381 case IPOPT_LSRR: 1382 case IPOPT_SSRR: 1383 /* 1384 * user process specifies route as: 1385 * ->A->B->C->D 1386 * D must be our final destination (but we can't 1387 * check that since we may not have connected yet). 1388 * A is first hop destination, which doesn't appear in 1389 * actual IP option, but is stored before the options. 1390 */ 1391 if (optlen < IPOPT_MINOFF - 1 + sizeof(struct in_addr)) 1392 goto bad; 1393 m->m_len -= sizeof(struct in_addr); 1394 cnt -= sizeof(struct in_addr); 1395 optlen -= sizeof(struct in_addr); 1396 cp[IPOPT_OLEN] = optlen; 1397 /* 1398 * Move first hop before start of options. 1399 */ 1400 bcopy(&cp[IPOPT_OFFSET+1], mtod(m, caddr_t), 1401 sizeof(struct in_addr)); 1402 /* 1403 * Then copy rest of options back 1404 * to close up the deleted entry. 1405 */ 1406 bcopy(&cp[IPOPT_OFFSET+1] + sizeof(struct in_addr), 1407 &cp[IPOPT_OFFSET+1], 1408 cnt - (IPOPT_MINOFF - 1)); 1409 break; 1410 } 1411 } 1412 if (m->m_len > MAX_IPOPTLEN + sizeof(struct in_addr)) 1413 goto bad; 1414 *pcbopt = m; 1415 return (0); 1416 1417 bad: 1418 m_free(m); 1419 return (EINVAL); 1420 } 1421 1422 /* 1423 * XXX 1424 * The whole multicast option thing needs to be re-thought. 1425 * Several of these options are equally applicable to non-multicast 1426 * transmission, and one (IP_MULTICAST_TTL) totally duplicates a 1427 * standard option (IP_TTL). 1428 */ 1429 1430 /* 1431 * following RFC1724 section 3.3, 0.0.0.0/8 is interpreted as interface index. 1432 */ 1433 static struct ifnet * 1434 ip_multicast_if(struct in_addr *a, int *ifindexp) 1435 { 1436 int ifindex; 1437 struct ifnet *ifp; 1438 1439 if (ifindexp) 1440 *ifindexp = 0; 1441 if (ntohl(a->s_addr) >> 24 == 0) { 1442 ifindex = ntohl(a->s_addr) & 0xffffff; 1443 if (ifindex < 0 || if_index < ifindex) 1444 return NULL; 1445 ifp = ifindex2ifnet[ifindex]; 1446 if (ifindexp) 1447 *ifindexp = ifindex; 1448 } else { 1449 ifp = INADDR_TO_IFP(a); 1450 } 1451 return ifp; 1452 } 1453 1454 /* 1455 * Set the IP multicast options in response to user setsockopt(). 1456 */ 1457 static int 1458 ip_setmoptions(struct sockopt *sopt, struct ip_moptions **imop) 1459 { 1460 int error = 0; 1461 int i; 1462 struct ip_mreqn mreqn; 1463 struct ifnet *ifp; 1464 struct ip_moptions *imo = *imop; 1465 int ifindex; 1466 1467 if (imo == NULL) { 1468 /* 1469 * No multicast option buffer attached to the pcb; 1470 * allocate one and initialize to default values. 1471 */ 1472 imo = kmalloc(sizeof *imo, M_IPMOPTS, M_WAITOK); 1473 1474 imo->imo_multicast_ifp = NULL; 1475 imo->imo_multicast_addr.s_addr = INADDR_ANY; 1476 imo->imo_multicast_vif = -1; 1477 imo->imo_multicast_ttl = IP_DEFAULT_MULTICAST_TTL; 1478 imo->imo_multicast_loop = IP_DEFAULT_MULTICAST_LOOP; 1479 imo->imo_num_memberships = 0; 1480 /* Assign imo to imop after all fields are setup */ 1481 cpu_sfence(); 1482 *imop = imo; 1483 } 1484 switch (sopt->sopt_name) { 1485 /* store an index number for the vif you wanna use in the send */ 1486 case IP_MULTICAST_VIF: 1487 if (legal_vif_num == 0) { 1488 error = EOPNOTSUPP; 1489 break; 1490 } 1491 error = soopt_to_kbuf(sopt, &i, sizeof i, sizeof i); 1492 if (error) 1493 break; 1494 if (!legal_vif_num(i) && (i != -1)) { 1495 error = EINVAL; 1496 break; 1497 } 1498 imo->imo_multicast_vif = i; 1499 break; 1500 1501 case IP_MULTICAST_IF: 1502 /* 1503 * Select the interface for outgoing multicast packets. 1504 */ 1505 if (sopt->sopt_valsize >= sizeof(mreqn)) { 1506 /* 1507 * Linux compat. 1508 */ 1509 error = soopt_to_kbuf(sopt, &mreqn, 1510 sizeof(mreqn), sizeof(mreqn)); 1511 if (error) 1512 break; 1513 } else if (sopt->sopt_valsize >= sizeof(struct ip_mreq)) { 1514 /* 1515 * Linux compat. 1516 */ 1517 mreqn.imr_ifindex = 0; 1518 error = soopt_to_kbuf(sopt, &mreqn, 1519 sizeof(struct ip_mreq), sizeof(struct ip_mreq)); 1520 if (error) 1521 break; 1522 } else { 1523 mreqn.imr_ifindex = 0; 1524 error = soopt_to_kbuf(sopt, &mreqn.imr_address, 1525 sizeof(struct in_addr), sizeof(struct in_addr)); 1526 if (error) 1527 break; 1528 } 1529 1530 ifindex = mreqn.imr_ifindex; 1531 if (ifindex != 0) { 1532 if (ifindex < 0 || if_index < ifindex) { 1533 error = EINVAL; 1534 break; 1535 } 1536 ifp = ifindex2ifnet[ifindex]; 1537 mreqn.imr_address.s_addr = htonl(ifindex & 0xffffff); 1538 } else { 1539 /* 1540 * INADDR_ANY is used to remove a previous selection. 1541 * When no interface is selected, a default one is 1542 * chosen every time a multicast packet is sent. 1543 */ 1544 if (mreqn.imr_address.s_addr == INADDR_ANY) { 1545 imo->imo_multicast_ifp = NULL; 1546 break; 1547 } 1548 /* 1549 * The selected interface is identified by its local 1550 * IP address. Find the interface and confirm that 1551 * it supports multicasting. 1552 */ 1553 ifp = ip_multicast_if(&mreqn.imr_address, &ifindex); 1554 } 1555 1556 if (ifp == NULL || !(ifp->if_flags & IFF_MULTICAST)) { 1557 error = EADDRNOTAVAIL; 1558 break; 1559 } 1560 imo->imo_multicast_ifp = ifp; 1561 if (ifindex) 1562 imo->imo_multicast_addr = mreqn.imr_address; 1563 else 1564 imo->imo_multicast_addr.s_addr = INADDR_ANY; 1565 break; 1566 1567 case IP_MULTICAST_TTL: 1568 /* 1569 * Set the IP time-to-live for outgoing multicast packets. 1570 * The original multicast API required a char argument, 1571 * which is inconsistent with the rest of the socket API. 1572 * We allow either a char or an int. 1573 */ 1574 if (sopt->sopt_valsize == 1) { 1575 u_char ttl; 1576 error = soopt_to_kbuf(sopt, &ttl, 1, 1); 1577 if (error) 1578 break; 1579 imo->imo_multicast_ttl = ttl; 1580 } else { 1581 u_int ttl; 1582 error = soopt_to_kbuf(sopt, &ttl, sizeof ttl, sizeof ttl); 1583 if (error) 1584 break; 1585 if (ttl > 255) 1586 error = EINVAL; 1587 else 1588 imo->imo_multicast_ttl = ttl; 1589 } 1590 break; 1591 1592 case IP_MULTICAST_LOOP: 1593 /* 1594 * Set the loopback flag for outgoing multicast packets. 1595 * Must be zero or one. The original multicast API required a 1596 * char argument, which is inconsistent with the rest 1597 * of the socket API. We allow either a char or an int. 1598 */ 1599 if (sopt->sopt_valsize == 1) { 1600 u_char loop; 1601 1602 error = soopt_to_kbuf(sopt, &loop, 1, 1); 1603 if (error) 1604 break; 1605 imo->imo_multicast_loop = !!loop; 1606 } else { 1607 u_int loop; 1608 1609 error = soopt_to_kbuf(sopt, &loop, sizeof loop, 1610 sizeof loop); 1611 if (error) 1612 break; 1613 imo->imo_multicast_loop = !!loop; 1614 } 1615 break; 1616 1617 case IP_ADD_MEMBERSHIP: 1618 /* 1619 * Add a multicast group membership. 1620 * Group must be a valid IP multicast address. 1621 */ 1622 if (sopt->sopt_valsize >= sizeof(mreqn)) { 1623 error = soopt_to_kbuf(sopt, &mreqn, 1624 sizeof(mreqn), sizeof(mreqn)); 1625 if (error) 1626 break; 1627 } else { 1628 mreqn.imr_ifindex = 0; 1629 error = soopt_to_kbuf(sopt, &mreqn, 1630 sizeof(struct ip_mreq), sizeof(struct ip_mreq)); 1631 if (error) 1632 break; 1633 } 1634 1635 if (!IN_MULTICAST(ntohl(mreqn.imr_multiaddr.s_addr))) { 1636 error = EINVAL; 1637 break; 1638 } 1639 1640 ifindex = mreqn.imr_ifindex; 1641 if (ifindex != 0) { 1642 if (ifindex < 0 || if_index < ifindex) { 1643 error = EINVAL; 1644 break; 1645 } 1646 ifp = ifindex2ifnet[ifindex]; 1647 } else if (mreqn.imr_address.s_addr == INADDR_ANY) { 1648 struct sockaddr_in dst; 1649 struct rtentry *rt; 1650 1651 /* 1652 * If no interface address or index was provided, 1653 * use the interface of the route to the given 1654 * multicast address. 1655 */ 1656 bzero(&dst, sizeof(struct sockaddr_in)); 1657 dst.sin_len = sizeof(struct sockaddr_in); 1658 dst.sin_family = AF_INET; 1659 dst.sin_addr = mreqn.imr_multiaddr; 1660 rt = rtlookup((struct sockaddr *)&dst); 1661 if (rt == NULL) { 1662 error = EADDRNOTAVAIL; 1663 break; 1664 } 1665 --rt->rt_refcnt; 1666 ifp = rt->rt_ifp; 1667 } else { 1668 ifp = ip_multicast_if(&mreqn.imr_address, NULL); 1669 } 1670 1671 /* 1672 * See if we found an interface, and confirm that it 1673 * supports multicast. 1674 */ 1675 if (ifp == NULL || !(ifp->if_flags & IFF_MULTICAST)) { 1676 error = EADDRNOTAVAIL; 1677 break; 1678 } 1679 /* 1680 * See if the membership already exists or if all the 1681 * membership slots are full. 1682 */ 1683 for (i = 0; i < imo->imo_num_memberships; ++i) { 1684 if (imo->imo_membership[i]->inm_ifp == ifp && 1685 imo->imo_membership[i]->inm_addr.s_addr 1686 == mreqn.imr_multiaddr.s_addr) 1687 break; 1688 } 1689 if (i < imo->imo_num_memberships) { 1690 error = EADDRINUSE; 1691 break; 1692 } 1693 if (i == IP_MAX_MEMBERSHIPS) { 1694 error = ETOOMANYREFS; 1695 break; 1696 } 1697 /* 1698 * Everything looks good; add a new record to the multicast 1699 * address list for the given interface. 1700 */ 1701 if ((imo->imo_membership[i] = 1702 in_addmulti(&mreqn.imr_multiaddr, ifp)) == NULL) { 1703 error = ENOBUFS; 1704 break; 1705 } 1706 ++imo->imo_num_memberships; 1707 break; 1708 1709 case IP_DROP_MEMBERSHIP: 1710 /* 1711 * Drop a multicast group membership. 1712 * Group must be a valid IP multicast address. 1713 */ 1714 if (sopt->sopt_valsize >= sizeof(mreqn)) { 1715 error = soopt_to_kbuf(sopt, &mreqn, 1716 sizeof(mreqn), sizeof(mreqn)); 1717 if (error) 1718 break; 1719 } else { 1720 mreqn.imr_ifindex = 0; 1721 error = soopt_to_kbuf(sopt, &mreqn, 1722 sizeof(struct ip_mreq), sizeof(struct ip_mreq)); 1723 if (error) 1724 break; 1725 } 1726 1727 if (!IN_MULTICAST(ntohl(mreqn.imr_multiaddr.s_addr))) { 1728 error = EINVAL; 1729 break; 1730 } 1731 1732 /* 1733 * If an interface index or address was specified, get a 1734 * pointer to its ifnet structure. 1735 */ 1736 ifindex = mreqn.imr_ifindex; 1737 if (ifindex != 0) { 1738 if (ifindex < 0 || if_index < ifindex) { 1739 error = EINVAL; 1740 break; 1741 } 1742 ifp = ifindex2ifnet[ifindex]; 1743 } else if (mreqn.imr_address.s_addr == INADDR_ANY) { 1744 ifp = NULL; 1745 } else { 1746 ifp = ip_multicast_if(&mreqn.imr_address, NULL); 1747 if (ifp == NULL) { 1748 error = EADDRNOTAVAIL; 1749 break; 1750 } 1751 } 1752 /* 1753 * Find the membership in the membership array. 1754 */ 1755 for (i = 0; i < imo->imo_num_memberships; ++i) { 1756 if ((ifp == NULL || 1757 imo->imo_membership[i]->inm_ifp == ifp) && 1758 imo->imo_membership[i]->inm_addr.s_addr == 1759 mreqn.imr_multiaddr.s_addr) 1760 break; 1761 } 1762 if (i == imo->imo_num_memberships) { 1763 error = EADDRNOTAVAIL; 1764 break; 1765 } 1766 /* 1767 * Give up the multicast address record to which the 1768 * membership points. 1769 */ 1770 in_delmulti(imo->imo_membership[i]); 1771 /* 1772 * Remove the gap in the membership array. 1773 */ 1774 for (++i; i < imo->imo_num_memberships; ++i) 1775 imo->imo_membership[i-1] = imo->imo_membership[i]; 1776 --imo->imo_num_memberships; 1777 break; 1778 1779 default: 1780 error = EOPNOTSUPP; 1781 break; 1782 } 1783 1784 return (error); 1785 } 1786 1787 /* 1788 * Return the IP multicast options in response to user getsockopt(). 1789 */ 1790 static int 1791 ip_getmoptions(struct sockopt *sopt, struct ip_moptions *imo) 1792 { 1793 struct in_addr addr; 1794 struct in_ifaddr *ia; 1795 int error, optval; 1796 u_char coptval; 1797 1798 error = 0; 1799 switch (sopt->sopt_name) { 1800 case IP_MULTICAST_VIF: 1801 if (imo != NULL) 1802 optval = imo->imo_multicast_vif; 1803 else 1804 optval = -1; 1805 soopt_from_kbuf(sopt, &optval, sizeof optval); 1806 break; 1807 1808 case IP_MULTICAST_IF: 1809 if (imo == NULL || imo->imo_multicast_ifp == NULL) 1810 addr.s_addr = INADDR_ANY; 1811 else if (imo->imo_multicast_addr.s_addr) { 1812 /* return the value user has set */ 1813 addr = imo->imo_multicast_addr; 1814 } else { 1815 ia = IFP_TO_IA(imo->imo_multicast_ifp); 1816 addr.s_addr = (ia == NULL) ? INADDR_ANY 1817 : IA_SIN(ia)->sin_addr.s_addr; 1818 } 1819 soopt_from_kbuf(sopt, &addr, sizeof addr); 1820 break; 1821 1822 case IP_MULTICAST_TTL: 1823 if (imo == NULL) 1824 optval = coptval = IP_DEFAULT_MULTICAST_TTL; 1825 else 1826 optval = coptval = imo->imo_multicast_ttl; 1827 if (sopt->sopt_valsize == 1) 1828 soopt_from_kbuf(sopt, &coptval, 1); 1829 else 1830 soopt_from_kbuf(sopt, &optval, sizeof optval); 1831 break; 1832 1833 case IP_MULTICAST_LOOP: 1834 if (imo == NULL) 1835 optval = coptval = IP_DEFAULT_MULTICAST_LOOP; 1836 else 1837 optval = coptval = imo->imo_multicast_loop; 1838 if (sopt->sopt_valsize == 1) 1839 soopt_from_kbuf(sopt, &coptval, 1); 1840 else 1841 soopt_from_kbuf(sopt, &optval, sizeof optval); 1842 break; 1843 1844 default: 1845 error = ENOPROTOOPT; 1846 break; 1847 } 1848 return (error); 1849 } 1850 1851 /* 1852 * Discard the IP multicast options. 1853 */ 1854 void 1855 ip_freemoptions(struct ip_moptions *imo) 1856 { 1857 int i; 1858 1859 if (imo != NULL) { 1860 for (i = 0; i < imo->imo_num_memberships; ++i) 1861 in_delmulti(imo->imo_membership[i]); 1862 kfree(imo, M_IPMOPTS); 1863 } 1864 } 1865 1866 /* 1867 * Routine called from ip_output() to loop back a copy of an IP multicast 1868 * packet to the input queue of a specified interface. Note that this 1869 * calls the output routine of the loopback "driver", but with an interface 1870 * pointer that might NOT be a loopback interface -- evil, but easier than 1871 * replicating that code here. 1872 */ 1873 static void 1874 ip_mloopback(struct ifnet *ifp, struct mbuf *m, struct sockaddr_in *dst, 1875 int hlen) 1876 { 1877 struct ip *ip; 1878 struct mbuf *copym; 1879 1880 copym = m_copypacket(m, M_NOWAIT); 1881 if (copym != NULL && (copym->m_flags & M_EXT || copym->m_len < hlen)) 1882 copym = m_pullup(copym, hlen); 1883 if (copym != NULL) { 1884 /* 1885 * if the checksum hasn't been computed, mark it as valid 1886 */ 1887 if (copym->m_pkthdr.csum_flags & CSUM_DELAY_DATA) { 1888 in_delayed_cksum(copym); 1889 copym->m_pkthdr.csum_flags &= ~CSUM_DELAY_DATA; 1890 copym->m_pkthdr.csum_flags |= 1891 CSUM_DATA_VALID | CSUM_PSEUDO_HDR; 1892 copym->m_pkthdr.csum_data = 0xffff; 1893 } 1894 /* 1895 * We don't bother to fragment if the IP length is greater 1896 * than the interface's MTU. Can this possibly matter? 1897 */ 1898 ip = mtod(copym, struct ip *); 1899 ip->ip_len = htons(ip->ip_len); 1900 ip->ip_off = htons(ip->ip_off); 1901 ip->ip_sum = 0; 1902 if (ip->ip_vhl == IP_VHL_BORING) { 1903 ip->ip_sum = in_cksum_hdr(ip); 1904 } else { 1905 ip->ip_sum = in_cksum(copym, hlen); 1906 } 1907 /* 1908 * NB: 1909 * It's not clear whether there are any lingering 1910 * reentrancy problems in other areas which might 1911 * be exposed by using ip_input directly (in 1912 * particular, everything which modifies the packet 1913 * in-place). Yet another option is using the 1914 * protosw directly to deliver the looped back 1915 * packet. For the moment, we'll err on the side 1916 * of safety by using if_simloop(). 1917 */ 1918 #if 1 /* XXX */ 1919 if (dst->sin_family != AF_INET) { 1920 kprintf("ip_mloopback: bad address family %d\n", 1921 dst->sin_family); 1922 dst->sin_family = AF_INET; 1923 } 1924 #endif 1925 if_simloop(ifp, copym, dst->sin_family, 0); 1926 } 1927 } 1928