ip_output.c revision 122330
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. All advertising materials mentioning features or use of this software 14 * must display the following acknowledgement: 15 * This product includes software developed by the University of 16 * California, Berkeley and its contributors. 17 * 4. Neither the name of the University nor the names of its contributors 18 * may be used to endorse or promote products derived from this software 19 * without specific prior written permission. 20 * 21 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 24 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 31 * SUCH DAMAGE. 32 * 33 * @(#)ip_output.c 8.3 (Berkeley) 1/21/94 34 * $FreeBSD: head/sys/netinet/ip_output.c 122330 2003-11-08 23:03:29Z sam $ 35 */ 36 37#include "opt_ipfw.h" 38#include "opt_ipdn.h" 39#include "opt_ipdivert.h" 40#include "opt_ipfilter.h" 41#include "opt_ipsec.h" 42#include "opt_mac.h" 43#include "opt_pfil_hooks.h" 44#include "opt_random_ip_id.h" 45#include "opt_mbuf_stress_test.h" 46 47#include <sys/param.h> 48#include <sys/systm.h> 49#include <sys/kernel.h> 50#include <sys/mac.h> 51#include <sys/malloc.h> 52#include <sys/mbuf.h> 53#include <sys/protosw.h> 54#include <sys/socket.h> 55#include <sys/socketvar.h> 56#include <sys/sysctl.h> 57 58#include <net/if.h> 59#include <net/route.h> 60 61#include <netinet/in.h> 62#include <netinet/in_systm.h> 63#include <netinet/ip.h> 64#include <netinet/in_pcb.h> 65#include <netinet/in_var.h> 66#include <netinet/ip_var.h> 67 68#ifdef PFIL_HOOKS 69#include <net/pfil.h> 70#endif 71 72#include <machine/in_cksum.h> 73 74static MALLOC_DEFINE(M_IPMOPTS, "ip_moptions", "internet multicast options"); 75 76#ifdef IPSEC 77#include <netinet6/ipsec.h> 78#include <netkey/key.h> 79#ifdef IPSEC_DEBUG 80#include <netkey/key_debug.h> 81#else 82#define KEYDEBUG(lev,arg) 83#endif 84#endif /*IPSEC*/ 85 86#ifdef FAST_IPSEC 87#include <netipsec/ipsec.h> 88#include <netipsec/xform.h> 89#include <netipsec/key.h> 90#endif /*FAST_IPSEC*/ 91 92#include <netinet/ip_fw.h> 93#include <netinet/ip_dummynet.h> 94 95#define print_ip(x, a, y) printf("%s %d.%d.%d.%d%s",\ 96 x, (ntohl(a.s_addr)>>24)&0xFF,\ 97 (ntohl(a.s_addr)>>16)&0xFF,\ 98 (ntohl(a.s_addr)>>8)&0xFF,\ 99 (ntohl(a.s_addr))&0xFF, y); 100 101u_short ip_id; 102 103#ifdef MBUF_STRESS_TEST 104int mbuf_frag_size = 0; 105SYSCTL_INT(_net_inet_ip, OID_AUTO, mbuf_frag_size, CTLFLAG_RW, 106 &mbuf_frag_size, 0, "Fragment outgoing mbufs to this size"); 107#endif 108 109static struct mbuf *ip_insertoptions(struct mbuf *, struct mbuf *, int *); 110static struct ifnet *ip_multicast_if(struct in_addr *, int *); 111static void ip_mloopback 112 (struct ifnet *, struct mbuf *, struct sockaddr_in *, int); 113static int ip_getmoptions 114 (struct sockopt *, struct ip_moptions *); 115static int ip_pcbopts(int, struct mbuf **, struct mbuf *); 116static int ip_setmoptions 117 (struct sockopt *, struct ip_moptions **); 118 119int ip_optcopy(struct ip *, struct ip *); 120 121 122extern struct protosw inetsw[]; 123 124/* 125 * IP output. The packet in mbuf chain m contains a skeletal IP 126 * header (with len, off, ttl, proto, tos, src, dst). 127 * The mbuf chain containing the packet will be freed. 128 * The mbuf opt, if present, will not be freed. 129 * In the IP forwarding case, the packet will arrive with options already 130 * inserted, so must have a NULL opt pointer. 131 */ 132int 133ip_output(struct mbuf *m0, struct mbuf *opt, struct route *ro, 134 int flags, struct ip_moptions *imo, struct inpcb *inp) 135{ 136 struct ip *ip; 137 struct ifnet *ifp = NULL; /* keep compiler happy */ 138 struct mbuf *m; 139 int hlen = sizeof (struct ip); 140 int len, off, error = 0; 141 struct sockaddr_in *dst = NULL; /* keep compiler happy */ 142 struct in_ifaddr *ia = NULL; 143 int isbroadcast, sw_csum; 144 struct in_addr pkt_dst; 145#ifdef IPSEC 146 struct route iproute; 147 struct socket *so; 148 struct secpolicy *sp = NULL; 149#endif 150#ifdef FAST_IPSEC 151 struct route iproute; 152 struct m_tag *mtag; 153 struct secpolicy *sp = NULL; 154 struct tdb_ident *tdbi; 155 int s; 156#endif /* FAST_IPSEC */ 157 struct ip_fw_args args; 158 int src_was_INADDR_ANY = 0; /* as the name says... */ 159 160 args.eh = NULL; 161 args.rule = NULL; 162 args.next_hop = NULL; 163 args.divert_rule = 0; /* divert cookie */ 164 165 /* Grab info from MT_TAG mbufs prepended to the chain. */ 166 for (; m0 && m0->m_type == MT_TAG; m0 = m0->m_next) { 167 switch(m0->_m_tag_id) { 168 default: 169 printf("ip_output: unrecognised MT_TAG tag %d\n", 170 m0->_m_tag_id); 171 break; 172 173 case PACKET_TAG_DUMMYNET: 174 /* 175 * the packet was already tagged, so part of the 176 * processing was already done, and we need to go down. 177 * Get parameters from the header. 178 */ 179 args.rule = ((struct dn_pkt *)m0)->rule; 180 opt = NULL ; 181 ro = & ( ((struct dn_pkt *)m0)->ro ) ; 182 imo = NULL ; 183 dst = ((struct dn_pkt *)m0)->dn_dst ; 184 ifp = ((struct dn_pkt *)m0)->ifp ; 185 flags = ((struct dn_pkt *)m0)->flags ; 186 break; 187 188 case PACKET_TAG_DIVERT: 189 args.divert_rule = (intptr_t)m0->m_data & 0xffff; 190 break; 191 192 case PACKET_TAG_IPFORWARD: 193 args.next_hop = (struct sockaddr_in *)m0->m_data; 194 break; 195 } 196 } 197 m = m0; 198 199#ifdef IPSEC 200 so = ipsec_getsocket(m); 201 (void)ipsec_setsocket(m, NULL); 202#endif /*IPSEC*/ 203 204 M_ASSERTPKTHDR(m); 205#ifndef FAST_IPSEC 206 KASSERT(ro != NULL, ("ip_output: no route, proto %d", 207 mtod(m, struct ip *)->ip_p)); 208#endif 209 if (inp != NULL) 210 INP_LOCK_ASSERT(inp); 211 212 if (args.rule != NULL) { /* dummynet already saw us */ 213 ip = mtod(m, struct ip *); 214 hlen = ip->ip_hl << 2 ; 215 if (ro->ro_rt) 216 ia = ifatoia(ro->ro_rt->rt_ifa); 217 goto sendit; 218 } 219 220 if (opt) { 221 len = 0; 222 m = ip_insertoptions(m, opt, &len); 223 if (len != 0) 224 hlen = len; 225 } 226 ip = mtod(m, struct ip *); 227 pkt_dst = args.next_hop ? args.next_hop->sin_addr : ip->ip_dst; 228 229 /* 230 * Fill in IP header. If we are not allowing fragmentation, 231 * then the ip_id field is meaningless, so send it as zero 232 * to reduce information leakage. Otherwise, if we are not 233 * randomizing ip_id, then don't bother to convert it to network 234 * byte order -- it's just a nonce. Note that a 16-bit counter 235 * will wrap around in less than 10 seconds at 100 Mbit/s on a 236 * medium with MTU 1500. See Steven M. Bellovin, "A Technique 237 * for Counting NATted Hosts", Proc. IMW'02, available at 238 * <http://www.research.att.com/~smb/papers/fnat.pdf>. 239 */ 240 if ((flags & (IP_FORWARDING|IP_RAWOUTPUT)) == 0) { 241 ip->ip_v = IPVERSION; 242 ip->ip_hl = hlen >> 2; 243 if ((ip->ip_off & IP_DF) == 0) { 244 ip->ip_off = 0; 245#ifdef RANDOM_IP_ID 246 ip->ip_id = ip_randomid(); 247#else 248 ip->ip_id = ip_id++; 249#endif 250 } else { 251 ip->ip_off = IP_DF; 252 ip->ip_id = 0; 253 } 254 ipstat.ips_localout++; 255 } else { 256 hlen = ip->ip_hl << 2; 257 } 258 259#ifdef FAST_IPSEC 260 if (ro == NULL) { 261 ro = &iproute; 262 bzero(ro, sizeof (*ro)); 263 } 264#endif /* FAST_IPSEC */ 265 dst = (struct sockaddr_in *)&ro->ro_dst; 266 /* 267 * If there is a cached route, 268 * check that it is to the same destination 269 * and is still up. If not, free it and try again. 270 * The address family should also be checked in case of sharing the 271 * cache with IPv6. 272 */ 273 if (ro->ro_rt && ((ro->ro_rt->rt_flags & RTF_UP) == 0 || 274 dst->sin_family != AF_INET || 275 dst->sin_addr.s_addr != pkt_dst.s_addr)) { 276 RTFREE(ro->ro_rt); 277 ro->ro_rt = (struct rtentry *)0; 278 } 279 if (ro->ro_rt == 0) { 280 bzero(dst, sizeof(*dst)); 281 dst->sin_family = AF_INET; 282 dst->sin_len = sizeof(*dst); 283 dst->sin_addr = pkt_dst; 284 } 285 /* 286 * If routing to interface only, 287 * short circuit routing lookup. 288 */ 289 if (flags & IP_ROUTETOIF) { 290 if ((ia = ifatoia(ifa_ifwithdstaddr(sintosa(dst)))) == 0 && 291 (ia = ifatoia(ifa_ifwithnet(sintosa(dst)))) == 0) { 292 ipstat.ips_noroute++; 293 error = ENETUNREACH; 294 goto bad; 295 } 296 ifp = ia->ia_ifp; 297 ip->ip_ttl = 1; 298 isbroadcast = in_broadcast(dst->sin_addr, ifp); 299 } else if (IN_MULTICAST(ntohl(ip->ip_dst.s_addr)) && 300 imo != NULL && imo->imo_multicast_ifp != NULL) { 301 /* 302 * Bypass the normal routing lookup for multicast 303 * packets if the interface is specified. 304 */ 305 ifp = imo->imo_multicast_ifp; 306 IFP_TO_IA(ifp, ia); 307 isbroadcast = 0; /* fool gcc */ 308 } else { 309 /* 310 * If this is the case, we probably don't want to allocate 311 * a protocol-cloned route since we didn't get one from the 312 * ULP. This lets TCP do its thing, while not burdening 313 * forwarding or ICMP with the overhead of cloning a route. 314 * Of course, we still want to do any cloning requested by 315 * the link layer, as this is probably required in all cases 316 * for correct operation (as it is for ARP). 317 */ 318 if (ro->ro_rt == 0) 319 rtalloc_ign(ro, RTF_PRCLONING); 320 if (ro->ro_rt == 0) { 321 ipstat.ips_noroute++; 322 error = EHOSTUNREACH; 323 goto bad; 324 } 325 ia = ifatoia(ro->ro_rt->rt_ifa); 326 ifp = ro->ro_rt->rt_ifp; 327 ro->ro_rt->rt_use++; 328 if (ro->ro_rt->rt_flags & RTF_GATEWAY) 329 dst = (struct sockaddr_in *)ro->ro_rt->rt_gateway; 330 if (ro->ro_rt->rt_flags & RTF_HOST) 331 isbroadcast = (ro->ro_rt->rt_flags & RTF_BROADCAST); 332 else 333 isbroadcast = in_broadcast(dst->sin_addr, ifp); 334 } 335 if (IN_MULTICAST(ntohl(pkt_dst.s_addr))) { 336 struct in_multi *inm; 337 338 m->m_flags |= M_MCAST; 339 /* 340 * IP destination address is multicast. Make sure "dst" 341 * still points to the address in "ro". (It may have been 342 * changed to point to a gateway address, above.) 343 */ 344 dst = (struct sockaddr_in *)&ro->ro_dst; 345 /* 346 * See if the caller provided any multicast options 347 */ 348 if (imo != NULL) { 349 ip->ip_ttl = imo->imo_multicast_ttl; 350 if (imo->imo_multicast_vif != -1) 351 ip->ip_src.s_addr = 352 ip_mcast_src ? 353 ip_mcast_src(imo->imo_multicast_vif) : 354 INADDR_ANY; 355 } else 356 ip->ip_ttl = IP_DEFAULT_MULTICAST_TTL; 357 /* 358 * Confirm that the outgoing interface supports multicast. 359 */ 360 if ((imo == NULL) || (imo->imo_multicast_vif == -1)) { 361 if ((ifp->if_flags & IFF_MULTICAST) == 0) { 362 ipstat.ips_noroute++; 363 error = ENETUNREACH; 364 goto bad; 365 } 366 } 367 /* 368 * If source address not specified yet, use address 369 * of outgoing interface. 370 */ 371 if (ip->ip_src.s_addr == INADDR_ANY) { 372 /* Interface may have no addresses. */ 373 if (ia != NULL) 374 ip->ip_src = IA_SIN(ia)->sin_addr; 375 } 376 377 if (ip_mrouter && (flags & IP_FORWARDING) == 0) { 378 /* 379 * XXX 380 * delayed checksums are not currently 381 * compatible with IP multicast routing 382 */ 383 if (m->m_pkthdr.csum_flags & CSUM_DELAY_DATA) { 384 in_delayed_cksum(m); 385 m->m_pkthdr.csum_flags &= 386 ~CSUM_DELAY_DATA; 387 } 388 } 389 IN_LOOKUP_MULTI(pkt_dst, ifp, inm); 390 if (inm != NULL && 391 (imo == NULL || imo->imo_multicast_loop)) { 392 /* 393 * If we belong to the destination multicast group 394 * on the outgoing interface, and the caller did not 395 * forbid loopback, loop back a copy. 396 */ 397 ip_mloopback(ifp, m, dst, hlen); 398 } 399 else { 400 /* 401 * If we are acting as a multicast router, perform 402 * multicast forwarding as if the packet had just 403 * arrived on the interface to which we are about 404 * to send. The multicast forwarding function 405 * recursively calls this function, using the 406 * IP_FORWARDING flag to prevent infinite recursion. 407 * 408 * Multicasts that are looped back by ip_mloopback(), 409 * above, will be forwarded by the ip_input() routine, 410 * if necessary. 411 */ 412 if (ip_mrouter && (flags & IP_FORWARDING) == 0) { 413 /* 414 * If rsvp daemon is not running, do not 415 * set ip_moptions. This ensures that the packet 416 * is multicast and not just sent down one link 417 * as prescribed by rsvpd. 418 */ 419 if (!rsvp_on) 420 imo = NULL; 421 if (ip_mforward && 422 ip_mforward(ip, ifp, m, imo) != 0) { 423 m_freem(m); 424 goto done; 425 } 426 } 427 } 428 429 /* 430 * Multicasts with a time-to-live of zero may be looped- 431 * back, above, but must not be transmitted on a network. 432 * Also, multicasts addressed to the loopback interface 433 * are not sent -- the above call to ip_mloopback() will 434 * loop back a copy if this host actually belongs to the 435 * destination group on the loopback interface. 436 */ 437 if (ip->ip_ttl == 0 || ifp->if_flags & IFF_LOOPBACK) { 438 m_freem(m); 439 goto done; 440 } 441 442 goto sendit; 443 } 444#ifndef notdef 445 /* 446 * If the source address is not specified yet, use the address 447 * of the outoing interface. In case, keep note we did that, so 448 * if the the firewall changes the next-hop causing the output 449 * interface to change, we can fix that. 450 */ 451 if (ip->ip_src.s_addr == INADDR_ANY) { 452 /* Interface may have no addresses. */ 453 if (ia != NULL) { 454 ip->ip_src = IA_SIN(ia)->sin_addr; 455 src_was_INADDR_ANY = 1; 456 } 457 } 458#endif /* notdef */ 459 /* 460 * Verify that we have any chance at all of being able to queue 461 * the packet or packet fragments 462 */ 463 if ((ifp->if_snd.ifq_len + ip->ip_len / ifp->if_mtu + 1) >= 464 ifp->if_snd.ifq_maxlen) { 465 error = ENOBUFS; 466 ipstat.ips_odropped++; 467 goto bad; 468 } 469 470 /* 471 * Look for broadcast address and 472 * verify user is allowed to send 473 * such a packet. 474 */ 475 if (isbroadcast) { 476 if ((ifp->if_flags & IFF_BROADCAST) == 0) { 477 error = EADDRNOTAVAIL; 478 goto bad; 479 } 480 if ((flags & IP_ALLOWBROADCAST) == 0) { 481 error = EACCES; 482 goto bad; 483 } 484 /* don't allow broadcast messages to be fragmented */ 485 if (ip->ip_len > ifp->if_mtu) { 486 error = EMSGSIZE; 487 goto bad; 488 } 489 if (flags & IP_SENDONES) 490 ip->ip_dst.s_addr = INADDR_BROADCAST; 491 m->m_flags |= M_BCAST; 492 } else { 493 m->m_flags &= ~M_BCAST; 494 } 495 496sendit: 497#ifdef IPSEC 498 /* get SP for this packet */ 499 if (so == NULL) 500 sp = ipsec4_getpolicybyaddr(m, IPSEC_DIR_OUTBOUND, 501 flags, &error); 502 else 503 sp = ipsec4_getpolicybysock(m, IPSEC_DIR_OUTBOUND, so, &error); 504 505 if (sp == NULL) { 506 ipsecstat.out_inval++; 507 goto bad; 508 } 509 510 error = 0; 511 512 /* check policy */ 513 switch (sp->policy) { 514 case IPSEC_POLICY_DISCARD: 515 /* 516 * This packet is just discarded. 517 */ 518 ipsecstat.out_polvio++; 519 goto bad; 520 521 case IPSEC_POLICY_BYPASS: 522 case IPSEC_POLICY_NONE: 523 /* no need to do IPsec. */ 524 goto skip_ipsec; 525 526 case IPSEC_POLICY_IPSEC: 527 if (sp->req == NULL) { 528 /* acquire a policy */ 529 error = key_spdacquire(sp); 530 goto bad; 531 } 532 break; 533 534 case IPSEC_POLICY_ENTRUST: 535 default: 536 printf("ip_output: Invalid policy found. %d\n", sp->policy); 537 } 538 { 539 struct ipsec_output_state state; 540 bzero(&state, sizeof(state)); 541 state.m = m; 542 if (flags & IP_ROUTETOIF) { 543 state.ro = &iproute; 544 bzero(&iproute, sizeof(iproute)); 545 } else 546 state.ro = ro; 547 state.dst = (struct sockaddr *)dst; 548 549 ip->ip_sum = 0; 550 551 /* 552 * XXX 553 * delayed checksums are not currently compatible with IPsec 554 */ 555 if (m->m_pkthdr.csum_flags & CSUM_DELAY_DATA) { 556 in_delayed_cksum(m); 557 m->m_pkthdr.csum_flags &= ~CSUM_DELAY_DATA; 558 } 559 560 ip->ip_len = htons(ip->ip_len); 561 ip->ip_off = htons(ip->ip_off); 562 563 error = ipsec4_output(&state, sp, flags); 564 565 m = state.m; 566 if (flags & IP_ROUTETOIF) { 567 /* 568 * if we have tunnel mode SA, we may need to ignore 569 * IP_ROUTETOIF. 570 */ 571 if (state.ro != &iproute || state.ro->ro_rt != NULL) { 572 flags &= ~IP_ROUTETOIF; 573 ro = state.ro; 574 } 575 } else 576 ro = state.ro; 577 dst = (struct sockaddr_in *)state.dst; 578 if (error) { 579 /* mbuf is already reclaimed in ipsec4_output. */ 580 m0 = NULL; 581 switch (error) { 582 case EHOSTUNREACH: 583 case ENETUNREACH: 584 case EMSGSIZE: 585 case ENOBUFS: 586 case ENOMEM: 587 break; 588 default: 589 printf("ip4_output (ipsec): error code %d\n", error); 590 /*fall through*/ 591 case ENOENT: 592 /* don't show these error codes to the user */ 593 error = 0; 594 break; 595 } 596 goto bad; 597 } 598 } 599 600 /* be sure to update variables that are affected by ipsec4_output() */ 601 ip = mtod(m, struct ip *); 602 hlen = ip->ip_hl << 2; 603 if (ro->ro_rt == NULL) { 604 if ((flags & IP_ROUTETOIF) == 0) { 605 printf("ip_output: " 606 "can't update route after IPsec processing\n"); 607 error = EHOSTUNREACH; /*XXX*/ 608 goto bad; 609 } 610 } else { 611 ia = ifatoia(ro->ro_rt->rt_ifa); 612 ifp = ro->ro_rt->rt_ifp; 613 } 614 615 /* make it flipped, again. */ 616 ip->ip_len = ntohs(ip->ip_len); 617 ip->ip_off = ntohs(ip->ip_off); 618skip_ipsec: 619#endif /*IPSEC*/ 620#ifdef FAST_IPSEC 621 /* 622 * Check the security policy (SP) for the packet and, if 623 * required, do IPsec-related processing. There are two 624 * cases here; the first time a packet is sent through 625 * it will be untagged and handled by ipsec4_checkpolicy. 626 * If the packet is resubmitted to ip_output (e.g. after 627 * AH, ESP, etc. processing), there will be a tag to bypass 628 * the lookup and related policy checking. 629 */ 630 mtag = m_tag_find(m, PACKET_TAG_IPSEC_PENDING_TDB, NULL); 631 s = splnet(); 632 if (mtag != NULL) { 633 tdbi = (struct tdb_ident *)(mtag + 1); 634 sp = ipsec_getpolicy(tdbi, IPSEC_DIR_OUTBOUND); 635 if (sp == NULL) 636 error = -EINVAL; /* force silent drop */ 637 m_tag_delete(m, mtag); 638 } else { 639 sp = ipsec4_checkpolicy(m, IPSEC_DIR_OUTBOUND, flags, 640 &error, inp); 641 } 642 /* 643 * There are four return cases: 644 * sp != NULL apply IPsec policy 645 * sp == NULL, error == 0 no IPsec handling needed 646 * sp == NULL, error == -EINVAL discard packet w/o error 647 * sp == NULL, error != 0 discard packet, report error 648 */ 649 if (sp != NULL) { 650 /* Loop detection, check if ipsec processing already done */ 651 KASSERT(sp->req != NULL, ("ip_output: no ipsec request")); 652 for (mtag = m_tag_first(m); mtag != NULL; 653 mtag = m_tag_next(m, mtag)) { 654 if (mtag->m_tag_cookie != MTAG_ABI_COMPAT) 655 continue; 656 if (mtag->m_tag_id != PACKET_TAG_IPSEC_OUT_DONE && 657 mtag->m_tag_id != PACKET_TAG_IPSEC_OUT_CRYPTO_NEEDED) 658 continue; 659 /* 660 * Check if policy has an SA associated with it. 661 * This can happen when an SP has yet to acquire 662 * an SA; e.g. on first reference. If it occurs, 663 * then we let ipsec4_process_packet do its thing. 664 */ 665 if (sp->req->sav == NULL) 666 break; 667 tdbi = (struct tdb_ident *)(mtag + 1); 668 if (tdbi->spi == sp->req->sav->spi && 669 tdbi->proto == sp->req->sav->sah->saidx.proto && 670 bcmp(&tdbi->dst, &sp->req->sav->sah->saidx.dst, 671 sizeof (union sockaddr_union)) == 0) { 672 /* 673 * No IPsec processing is needed, free 674 * reference to SP. 675 * 676 * NB: null pointer to avoid free at 677 * done: below. 678 */ 679 KEY_FREESP(&sp), sp = NULL; 680 splx(s); 681 goto spd_done; 682 } 683 } 684 685 /* 686 * Do delayed checksums now because we send before 687 * this is done in the normal processing path. 688 */ 689 if (m->m_pkthdr.csum_flags & CSUM_DELAY_DATA) { 690 in_delayed_cksum(m); 691 m->m_pkthdr.csum_flags &= ~CSUM_DELAY_DATA; 692 } 693 694 ip->ip_len = htons(ip->ip_len); 695 ip->ip_off = htons(ip->ip_off); 696 697 /* NB: callee frees mbuf */ 698 error = ipsec4_process_packet(m, sp->req, flags, 0); 699 /* 700 * Preserve KAME behaviour: ENOENT can be returned 701 * when an SA acquire is in progress. Don't propagate 702 * this to user-level; it confuses applications. 703 * 704 * XXX this will go away when the SADB is redone. 705 */ 706 if (error == ENOENT) 707 error = 0; 708 splx(s); 709 goto done; 710 } else { 711 splx(s); 712 713 if (error != 0) { 714 /* 715 * Hack: -EINVAL is used to signal that a packet 716 * should be silently discarded. This is typically 717 * because we asked key management for an SA and 718 * it was delayed (e.g. kicked up to IKE). 719 */ 720 if (error == -EINVAL) 721 error = 0; 722 goto bad; 723 } else { 724 /* No IPsec processing for this packet. */ 725 } 726#ifdef notyet 727 /* 728 * If deferred crypto processing is needed, check that 729 * the interface supports it. 730 */ 731 mtag = m_tag_find(m, PACKET_TAG_IPSEC_OUT_CRYPTO_NEEDED, NULL); 732 if (mtag != NULL && (ifp->if_capenable & IFCAP_IPSEC) == 0) { 733 /* notify IPsec to do its own crypto */ 734 ipsp_skipcrypto_unmark((struct tdb_ident *)(mtag + 1)); 735 error = EHOSTUNREACH; 736 goto bad; 737 } 738#endif 739 } 740spd_done: 741#endif /* FAST_IPSEC */ 742 743 /* 744 * IpHack's section. 745 * - Xlate: translate packet's addr/port (NAT). 746 * - Firewall: deny/allow/etc. 747 * - Wrap: fake packet's addr/port <unimpl.> 748 * - Encapsulate: put it in another IP and send out. <unimp.> 749 */ 750#ifdef PFIL_HOOKS 751 /* 752 * Run through list of hooks for output packets. 753 */ 754 error = pfil_run_hooks(&inet_pfil_hook, &m, ifp, PFIL_OUT); 755 if (error != 0 || m == NULL) 756 goto done; 757 ip = mtod(m, struct ip *); 758#endif /* PFIL_HOOKS */ 759 760 /* 761 * Check with the firewall... 762 * but not if we are already being fwd'd from a firewall. 763 */ 764 if (fw_enable && IPFW_LOADED && !args.next_hop) { 765 struct sockaddr_in *old = dst; 766 767 args.m = m; 768 args.next_hop = dst; 769 args.oif = ifp; 770 off = ip_fw_chk_ptr(&args); 771 m = args.m; 772 dst = args.next_hop; 773 774 /* 775 * On return we must do the following: 776 * m == NULL -> drop the pkt (old interface, deprecated) 777 * (off & IP_FW_PORT_DENY_FLAG) -> drop the pkt (new interface) 778 * 1<=off<= 0xffff -> DIVERT 779 * (off & IP_FW_PORT_DYNT_FLAG) -> send to a DUMMYNET pipe 780 * (off & IP_FW_PORT_TEE_FLAG) -> TEE the packet 781 * dst != old -> IPFIREWALL_FORWARD 782 * off==0, dst==old -> accept 783 * If some of the above modules are not compiled in, then 784 * we should't have to check the corresponding condition 785 * (because the ipfw control socket should not accept 786 * unsupported rules), but better play safe and drop 787 * packets in case of doubt. 788 */ 789 if ( (off & IP_FW_PORT_DENY_FLAG) || m == NULL) { 790 if (m) 791 m_freem(m); 792 error = EACCES; 793 goto done; 794 } 795 ip = mtod(m, struct ip *); 796 if (off == 0 && dst == old) /* common case */ 797 goto pass; 798 if (DUMMYNET_LOADED && (off & IP_FW_PORT_DYNT_FLAG) != 0) { 799 /* 800 * pass the pkt to dummynet. Need to include 801 * pipe number, m, ifp, ro, dst because these are 802 * not recomputed in the next pass. 803 * All other parameters have been already used and 804 * so they are not needed anymore. 805 * XXX note: if the ifp or ro entry are deleted 806 * while a pkt is in dummynet, we are in trouble! 807 */ 808 args.ro = ro; 809 args.dst = dst; 810 args.flags = flags; 811 812 error = ip_dn_io_ptr(m, off & 0xffff, DN_TO_IP_OUT, 813 &args); 814 goto done; 815 } 816#ifdef IPDIVERT 817 if (off != 0 && (off & IP_FW_PORT_DYNT_FLAG) == 0) { 818 struct mbuf *clone = NULL; 819 820 /* Clone packet if we're doing a 'tee' */ 821 if ((off & IP_FW_PORT_TEE_FLAG) != 0) 822 clone = m_dup(m, M_DONTWAIT); 823 824 /* 825 * XXX 826 * delayed checksums are not currently compatible 827 * with divert sockets. 828 */ 829 if (m->m_pkthdr.csum_flags & CSUM_DELAY_DATA) { 830 in_delayed_cksum(m); 831 m->m_pkthdr.csum_flags &= ~CSUM_DELAY_DATA; 832 } 833 834 /* Restore packet header fields to original values */ 835 ip->ip_len = htons(ip->ip_len); 836 ip->ip_off = htons(ip->ip_off); 837 838 /* Deliver packet to divert input routine */ 839 divert_packet(m, 0, off & 0xffff, args.divert_rule); 840 841 /* If 'tee', continue with original packet */ 842 if (clone != NULL) { 843 m = clone; 844 ip = mtod(m, struct ip *); 845 goto pass; 846 } 847 goto done; 848 } 849#endif 850 851 /* IPFIREWALL_FORWARD */ 852 /* 853 * Check dst to make sure it is directly reachable on the 854 * interface we previously thought it was. 855 * If it isn't (which may be likely in some situations) we have 856 * to re-route it (ie, find a route for the next-hop and the 857 * associated interface) and set them here. This is nested 858 * forwarding which in most cases is undesirable, except where 859 * such control is nigh impossible. So we do it here. 860 * And I'm babbling. 861 */ 862 if (off == 0 && old != dst) { /* FORWARD, dst has changed */ 863#if 0 864 /* 865 * XXX To improve readability, this block should be 866 * changed into a function call as below: 867 */ 868 error = ip_ipforward(&m, &dst, &ifp); 869 if (error) 870 goto bad; 871 if (m == NULL) /* ip_input consumed the mbuf */ 872 goto done; 873#else 874 struct in_ifaddr *ia; 875 876 /* 877 * XXX sro_fwd below is static, and a pointer 878 * to it gets passed to routines downstream. 879 * This could have surprisingly bad results in 880 * practice, because its content is overwritten 881 * by subsequent packets. 882 */ 883 /* There must be a better way to do this next line... */ 884 static struct route sro_fwd; 885 struct route *ro_fwd = &sro_fwd; 886 887#if 0 888 print_ip("IPFIREWALL_FORWARD: New dst ip: ", 889 dst->sin_addr, "\n"); 890#endif 891 892 /* 893 * We need to figure out if we have been forwarded 894 * to a local socket. If so, then we should somehow 895 * "loop back" to ip_input, and get directed to the 896 * PCB as if we had received this packet. This is 897 * because it may be dificult to identify the packets 898 * you want to forward until they are being output 899 * and have selected an interface. (e.g. locally 900 * initiated packets) If we used the loopback inteface, 901 * we would not be able to control what happens 902 * as the packet runs through ip_input() as 903 * it is done through an ISR. 904 */ 905 LIST_FOREACH(ia, 906 INADDR_HASH(dst->sin_addr.s_addr), ia_hash) { 907 /* 908 * If the addr to forward to is one 909 * of ours, we pretend to 910 * be the destination for this packet. 911 */ 912 if (IA_SIN(ia)->sin_addr.s_addr == 913 dst->sin_addr.s_addr) 914 break; 915 } 916 if (ia) { /* tell ip_input "dont filter" */ 917 struct m_hdr tag; 918 919 tag.mh_type = MT_TAG; 920 tag.mh_flags = PACKET_TAG_IPFORWARD; 921 tag.mh_data = (caddr_t)args.next_hop; 922 tag.mh_next = m; 923 924 if (m->m_pkthdr.rcvif == NULL) 925 m->m_pkthdr.rcvif = ifunit("lo0"); 926 if (m->m_pkthdr.csum_flags & CSUM_DELAY_DATA) { 927 m->m_pkthdr.csum_flags |= 928 CSUM_DATA_VALID | CSUM_PSEUDO_HDR; 929 m0->m_pkthdr.csum_data = 0xffff; 930 } 931 m->m_pkthdr.csum_flags |= 932 CSUM_IP_CHECKED | CSUM_IP_VALID; 933 ip->ip_len = htons(ip->ip_len); 934 ip->ip_off = htons(ip->ip_off); 935 ip_input((struct mbuf *)&tag); 936 goto done; 937 } 938 /* Some of the logic for this was 939 * nicked from above. 940 * 941 * This rewrites the cached route in a local PCB. 942 * Is this what we want to do? 943 */ 944 bcopy(dst, &ro_fwd->ro_dst, sizeof(*dst)); 945 946 ro_fwd->ro_rt = 0; 947 rtalloc_ign(ro_fwd, RTF_PRCLONING); 948 949 if (ro_fwd->ro_rt == 0) { 950 ipstat.ips_noroute++; 951 error = EHOSTUNREACH; 952 goto bad; 953 } 954 955 ia = ifatoia(ro_fwd->ro_rt->rt_ifa); 956 ifp = ro_fwd->ro_rt->rt_ifp; 957 ro_fwd->ro_rt->rt_use++; 958 if (ro_fwd->ro_rt->rt_flags & RTF_GATEWAY) 959 dst = (struct sockaddr_in *) 960 ro_fwd->ro_rt->rt_gateway; 961 if (ro_fwd->ro_rt->rt_flags & RTF_HOST) 962 isbroadcast = 963 (ro_fwd->ro_rt->rt_flags & RTF_BROADCAST); 964 else 965 isbroadcast = in_broadcast(dst->sin_addr, ifp); 966 if (ro->ro_rt) 967 RTFREE(ro->ro_rt); 968 ro->ro_rt = ro_fwd->ro_rt; 969 dst = (struct sockaddr_in *)&ro_fwd->ro_dst; 970 971#endif /* ... block to be put into a function */ 972 /* 973 * If we added a default src ip earlier, 974 * which would have been gotten from the-then 975 * interface, do it again, from the new one. 976 */ 977 if (src_was_INADDR_ANY) 978 ip->ip_src = IA_SIN(ia)->sin_addr; 979 goto pass ; 980 } 981 982 /* 983 * if we get here, none of the above matches, and 984 * we have to drop the pkt 985 */ 986 m_freem(m); 987 error = EACCES; /* not sure this is the right error msg */ 988 goto done; 989 } 990 991pass: 992 /* 127/8 must not appear on wire - RFC1122. */ 993 if ((ntohl(ip->ip_dst.s_addr) >> IN_CLASSA_NSHIFT) == IN_LOOPBACKNET || 994 (ntohl(ip->ip_src.s_addr) >> IN_CLASSA_NSHIFT) == IN_LOOPBACKNET) { 995 if ((ifp->if_flags & IFF_LOOPBACK) == 0) { 996 ipstat.ips_badaddr++; 997 error = EADDRNOTAVAIL; 998 goto bad; 999 } 1000 } 1001 1002 m->m_pkthdr.csum_flags |= CSUM_IP; 1003 sw_csum = m->m_pkthdr.csum_flags & ~ifp->if_hwassist; 1004 if (sw_csum & CSUM_DELAY_DATA) { 1005 in_delayed_cksum(m); 1006 sw_csum &= ~CSUM_DELAY_DATA; 1007 } 1008 m->m_pkthdr.csum_flags &= ifp->if_hwassist; 1009 1010 /* 1011 * If small enough for interface, or the interface will take 1012 * care of the fragmentation for us, can just send directly. 1013 */ 1014 if (ip->ip_len <= ifp->if_mtu || ifp->if_hwassist & CSUM_FRAGMENT) { 1015 ip->ip_len = htons(ip->ip_len); 1016 ip->ip_off = htons(ip->ip_off); 1017 ip->ip_sum = 0; 1018 if (sw_csum & CSUM_DELAY_IP) 1019 ip->ip_sum = in_cksum(m, hlen); 1020 1021 /* Record statistics for this interface address. */ 1022 if (!(flags & IP_FORWARDING) && ia) { 1023 ia->ia_ifa.if_opackets++; 1024 ia->ia_ifa.if_obytes += m->m_pkthdr.len; 1025 } 1026 1027#ifdef IPSEC 1028 /* clean ipsec history once it goes out of the node */ 1029 ipsec_delaux(m); 1030#endif 1031 1032#ifdef MBUF_STRESS_TEST 1033 if (mbuf_frag_size && m->m_pkthdr.len > mbuf_frag_size) 1034 m = m_fragment(m, M_DONTWAIT, mbuf_frag_size); 1035#endif 1036 error = (*ifp->if_output)(ifp, m, 1037 (struct sockaddr *)dst, ro->ro_rt); 1038 goto done; 1039 } 1040 1041 if (ip->ip_off & IP_DF) { 1042 error = EMSGSIZE; 1043 /* 1044 * This case can happen if the user changed the MTU 1045 * of an interface after enabling IP on it. Because 1046 * most netifs don't keep track of routes pointing to 1047 * them, there is no way for one to update all its 1048 * routes when the MTU is changed. 1049 */ 1050 if ((ro->ro_rt->rt_flags & (RTF_UP | RTF_HOST)) && 1051 !(ro->ro_rt->rt_rmx.rmx_locks & RTV_MTU) && 1052 (ro->ro_rt->rt_rmx.rmx_mtu > ifp->if_mtu)) { 1053 ro->ro_rt->rt_rmx.rmx_mtu = ifp->if_mtu; 1054 } 1055 ipstat.ips_cantfrag++; 1056 goto bad; 1057 } 1058 1059 /* 1060 * Too large for interface; fragment if possible. If successful, 1061 * on return, m will point to a list of packets to be sent. 1062 */ 1063 error = ip_fragment(ip, &m, ifp->if_mtu, ifp->if_hwassist, sw_csum); 1064 if (error) 1065 goto bad; 1066 for (; m; m = m0) { 1067 m0 = m->m_nextpkt; 1068 m->m_nextpkt = 0; 1069#ifdef IPSEC 1070 /* clean ipsec history once it goes out of the node */ 1071 ipsec_delaux(m); 1072#endif 1073 if (error == 0) { 1074 /* Record statistics for this interface address. */ 1075 if (ia != NULL) { 1076 ia->ia_ifa.if_opackets++; 1077 ia->ia_ifa.if_obytes += m->m_pkthdr.len; 1078 } 1079 1080 error = (*ifp->if_output)(ifp, m, 1081 (struct sockaddr *)dst, ro->ro_rt); 1082 } else 1083 m_freem(m); 1084 } 1085 1086 if (error == 0) 1087 ipstat.ips_fragmented++; 1088 1089done: 1090#ifdef IPSEC 1091 if (ro == &iproute && ro->ro_rt) { 1092 RTFREE(ro->ro_rt); 1093 ro->ro_rt = NULL; 1094 } 1095 if (sp != NULL) { 1096 KEYDEBUG(KEYDEBUG_IPSEC_STAMP, 1097 printf("DP ip_output call free SP:%p\n", sp)); 1098 key_freesp(sp); 1099 } 1100#endif 1101#ifdef FAST_IPSEC 1102 if (ro == &iproute && ro->ro_rt) { 1103 RTFREE(ro->ro_rt); 1104 ro->ro_rt = NULL; 1105 } 1106 if (sp != NULL) 1107 KEY_FREESP(&sp); 1108#endif 1109 return (error); 1110bad: 1111 m_freem(m); 1112 goto done; 1113} 1114 1115/* 1116 * Create a chain of fragments which fit the given mtu. m_frag points to the 1117 * mbuf to be fragmented; on return it points to the chain with the fragments. 1118 * Return 0 if no error. If error, m_frag may contain a partially built 1119 * chain of fragments that should be freed by the caller. 1120 * 1121 * if_hwassist_flags is the hw offload capabilities (see if_data.ifi_hwassist) 1122 * sw_csum contains the delayed checksums flags (e.g., CSUM_DELAY_IP). 1123 */ 1124int 1125ip_fragment(struct ip *ip, struct mbuf **m_frag, int mtu, 1126 u_long if_hwassist_flags, int sw_csum) 1127{ 1128 int error = 0; 1129 int hlen = ip->ip_hl << 2; 1130 int len = (mtu - hlen) & ~7; /* size of payload in each fragment */ 1131 int off; 1132 struct mbuf *m0 = *m_frag; /* the original packet */ 1133 int firstlen; 1134 struct mbuf **mnext; 1135 int nfrags; 1136 1137 if (ip->ip_off & IP_DF) { /* Fragmentation not allowed */ 1138 ipstat.ips_cantfrag++; 1139 return EMSGSIZE; 1140 } 1141 1142 /* 1143 * Must be able to put at least 8 bytes per fragment. 1144 */ 1145 if (len < 8) 1146 return EMSGSIZE; 1147 1148 /* 1149 * If the interface will not calculate checksums on 1150 * fragmented packets, then do it here. 1151 */ 1152 if (m0->m_pkthdr.csum_flags & CSUM_DELAY_DATA && 1153 (if_hwassist_flags & CSUM_IP_FRAGS) == 0) { 1154 in_delayed_cksum(m0); 1155 m0->m_pkthdr.csum_flags &= ~CSUM_DELAY_DATA; 1156 } 1157 1158 if (len > PAGE_SIZE) { 1159 /* 1160 * Fragment large datagrams such that each segment 1161 * contains a multiple of PAGE_SIZE amount of data, 1162 * plus headers. This enables a receiver to perform 1163 * page-flipping zero-copy optimizations. 1164 * 1165 * XXX When does this help given that sender and receiver 1166 * could have different page sizes, and also mtu could 1167 * be less than the receiver's page size ? 1168 */ 1169 int newlen; 1170 struct mbuf *m; 1171 1172 for (m = m0, off = 0; m && (off+m->m_len) <= mtu; m = m->m_next) 1173 off += m->m_len; 1174 1175 /* 1176 * firstlen (off - hlen) must be aligned on an 1177 * 8-byte boundary 1178 */ 1179 if (off < hlen) 1180 goto smart_frag_failure; 1181 off = ((off - hlen) & ~7) + hlen; 1182 newlen = (~PAGE_MASK) & mtu; 1183 if ((newlen + sizeof (struct ip)) > mtu) { 1184 /* we failed, go back the default */ 1185smart_frag_failure: 1186 newlen = len; 1187 off = hlen + len; 1188 } 1189 len = newlen; 1190 1191 } else { 1192 off = hlen + len; 1193 } 1194 1195 firstlen = off - hlen; 1196 mnext = &m0->m_nextpkt; /* pointer to next packet */ 1197 1198 /* 1199 * Loop through length of segment after first fragment, 1200 * make new header and copy data of each part and link onto chain. 1201 * Here, m0 is the original packet, m is the fragment being created. 1202 * The fragments are linked off the m_nextpkt of the original 1203 * packet, which after processing serves as the first fragment. 1204 */ 1205 for (nfrags = 1; off < ip->ip_len; off += len, nfrags++) { 1206 struct ip *mhip; /* ip header on the fragment */ 1207 struct mbuf *m; 1208 int mhlen = sizeof (struct ip); 1209 1210 MGETHDR(m, M_DONTWAIT, MT_HEADER); 1211 if (m == 0) { 1212 error = ENOBUFS; 1213 ipstat.ips_odropped++; 1214 goto done; 1215 } 1216 m->m_flags |= (m0->m_flags & M_MCAST) | M_FRAG; 1217 /* 1218 * In the first mbuf, leave room for the link header, then 1219 * copy the original IP header including options. The payload 1220 * goes into an additional mbuf chain returned by m_copy(). 1221 */ 1222 m->m_data += max_linkhdr; 1223 mhip = mtod(m, struct ip *); 1224 *mhip = *ip; 1225 if (hlen > sizeof (struct ip)) { 1226 mhlen = ip_optcopy(ip, mhip) + sizeof (struct ip); 1227 mhip->ip_v = IPVERSION; 1228 mhip->ip_hl = mhlen >> 2; 1229 } 1230 m->m_len = mhlen; 1231 /* XXX do we need to add ip->ip_off below ? */ 1232 mhip->ip_off = ((off - hlen) >> 3) + ip->ip_off; 1233 if (off + len >= ip->ip_len) { /* last fragment */ 1234 len = ip->ip_len - off; 1235 m->m_flags |= M_LASTFRAG; 1236 } else 1237 mhip->ip_off |= IP_MF; 1238 mhip->ip_len = htons((u_short)(len + mhlen)); 1239 m->m_next = m_copy(m0, off, len); 1240 if (m->m_next == 0) { /* copy failed */ 1241 m_free(m); 1242 error = ENOBUFS; /* ??? */ 1243 ipstat.ips_odropped++; 1244 goto done; 1245 } 1246 m->m_pkthdr.len = mhlen + len; 1247 m->m_pkthdr.rcvif = (struct ifnet *)0; 1248#ifdef MAC 1249 mac_create_fragment(m0, m); 1250#endif 1251 m->m_pkthdr.csum_flags = m0->m_pkthdr.csum_flags; 1252 mhip->ip_off = htons(mhip->ip_off); 1253 mhip->ip_sum = 0; 1254 if (sw_csum & CSUM_DELAY_IP) 1255 mhip->ip_sum = in_cksum(m, mhlen); 1256 *mnext = m; 1257 mnext = &m->m_nextpkt; 1258 } 1259 ipstat.ips_ofragments += nfrags; 1260 1261 /* set first marker for fragment chain */ 1262 m0->m_flags |= M_FIRSTFRAG | M_FRAG; 1263 m0->m_pkthdr.csum_data = nfrags; 1264 1265 /* 1266 * Update first fragment by trimming what's been copied out 1267 * and updating header. 1268 */ 1269 m_adj(m0, hlen + firstlen - ip->ip_len); 1270 m0->m_pkthdr.len = hlen + firstlen; 1271 ip->ip_len = htons((u_short)m0->m_pkthdr.len); 1272 ip->ip_off |= IP_MF; 1273 ip->ip_off = htons(ip->ip_off); 1274 ip->ip_sum = 0; 1275 if (sw_csum & CSUM_DELAY_IP) 1276 ip->ip_sum = in_cksum(m0, hlen); 1277 1278done: 1279 *m_frag = m0; 1280 return error; 1281} 1282 1283void 1284in_delayed_cksum(struct mbuf *m) 1285{ 1286 struct ip *ip; 1287 u_short csum, offset; 1288 1289 ip = mtod(m, struct ip *); 1290 offset = ip->ip_hl << 2 ; 1291 csum = in_cksum_skip(m, ip->ip_len, offset); 1292 if (m->m_pkthdr.csum_flags & CSUM_UDP && csum == 0) 1293 csum = 0xffff; 1294 offset += m->m_pkthdr.csum_data; /* checksum offset */ 1295 1296 if (offset + sizeof(u_short) > m->m_len) { 1297 printf("delayed m_pullup, m->len: %d off: %d p: %d\n", 1298 m->m_len, offset, ip->ip_p); 1299 /* 1300 * XXX 1301 * this shouldn't happen, but if it does, the 1302 * correct behavior may be to insert the checksum 1303 * in the existing chain instead of rearranging it. 1304 */ 1305 m = m_pullup(m, offset + sizeof(u_short)); 1306 } 1307 *(u_short *)(m->m_data + offset) = csum; 1308} 1309 1310/* 1311 * Insert IP options into preformed packet. 1312 * Adjust IP destination as required for IP source routing, 1313 * as indicated by a non-zero in_addr at the start of the options. 1314 * 1315 * XXX This routine assumes that the packet has no options in place. 1316 */ 1317static struct mbuf * 1318ip_insertoptions(m, opt, phlen) 1319 register struct mbuf *m; 1320 struct mbuf *opt; 1321 int *phlen; 1322{ 1323 register struct ipoption *p = mtod(opt, struct ipoption *); 1324 struct mbuf *n; 1325 register struct ip *ip = mtod(m, struct ip *); 1326 unsigned optlen; 1327 1328 optlen = opt->m_len - sizeof(p->ipopt_dst); 1329 if (optlen + ip->ip_len > IP_MAXPACKET) { 1330 *phlen = 0; 1331 return (m); /* XXX should fail */ 1332 } 1333 if (p->ipopt_dst.s_addr) 1334 ip->ip_dst = p->ipopt_dst; 1335 if (m->m_flags & M_EXT || m->m_data - optlen < m->m_pktdat) { 1336 MGETHDR(n, M_DONTWAIT, MT_HEADER); 1337 if (n == 0) { 1338 *phlen = 0; 1339 return (m); 1340 } 1341 n->m_pkthdr.rcvif = (struct ifnet *)0; 1342#ifdef MAC 1343 mac_create_mbuf_from_mbuf(m, n); 1344#endif 1345 n->m_pkthdr.len = m->m_pkthdr.len + optlen; 1346 m->m_len -= sizeof(struct ip); 1347 m->m_data += sizeof(struct ip); 1348 n->m_next = m; 1349 m = n; 1350 m->m_len = optlen + sizeof(struct ip); 1351 m->m_data += max_linkhdr; 1352 bcopy(ip, mtod(m, void *), sizeof(struct ip)); 1353 } else { 1354 m->m_data -= optlen; 1355 m->m_len += optlen; 1356 m->m_pkthdr.len += optlen; 1357 bcopy(ip, mtod(m, void *), sizeof(struct ip)); 1358 } 1359 ip = mtod(m, struct ip *); 1360 bcopy(p->ipopt_list, ip + 1, optlen); 1361 *phlen = sizeof(struct ip) + optlen; 1362 ip->ip_v = IPVERSION; 1363 ip->ip_hl = *phlen >> 2; 1364 ip->ip_len += optlen; 1365 return (m); 1366} 1367 1368/* 1369 * Copy options from ip to jp, 1370 * omitting those not copied during fragmentation. 1371 */ 1372int 1373ip_optcopy(ip, jp) 1374 struct ip *ip, *jp; 1375{ 1376 register u_char *cp, *dp; 1377 int opt, optlen, cnt; 1378 1379 cp = (u_char *)(ip + 1); 1380 dp = (u_char *)(jp + 1); 1381 cnt = (ip->ip_hl << 2) - sizeof (struct ip); 1382 for (; cnt > 0; cnt -= optlen, cp += optlen) { 1383 opt = cp[0]; 1384 if (opt == IPOPT_EOL) 1385 break; 1386 if (opt == IPOPT_NOP) { 1387 /* Preserve for IP mcast tunnel's LSRR alignment. */ 1388 *dp++ = IPOPT_NOP; 1389 optlen = 1; 1390 continue; 1391 } 1392 1393 KASSERT(cnt >= IPOPT_OLEN + sizeof(*cp), 1394 ("ip_optcopy: malformed ipv4 option")); 1395 optlen = cp[IPOPT_OLEN]; 1396 KASSERT(optlen >= IPOPT_OLEN + sizeof(*cp) && optlen <= cnt, 1397 ("ip_optcopy: malformed ipv4 option")); 1398 1399 /* bogus lengths should have been caught by ip_dooptions */ 1400 if (optlen > cnt) 1401 optlen = cnt; 1402 if (IPOPT_COPIED(opt)) { 1403 bcopy(cp, dp, optlen); 1404 dp += optlen; 1405 } 1406 } 1407 for (optlen = dp - (u_char *)(jp+1); optlen & 0x3; optlen++) 1408 *dp++ = IPOPT_EOL; 1409 return (optlen); 1410} 1411 1412/* 1413 * IP socket option processing. 1414 */ 1415int 1416ip_ctloutput(so, sopt) 1417 struct socket *so; 1418 struct sockopt *sopt; 1419{ 1420 struct inpcb *inp = sotoinpcb(so); 1421 int error, optval; 1422 1423 error = optval = 0; 1424 if (sopt->sopt_level != IPPROTO_IP) { 1425 return (EINVAL); 1426 } 1427 1428 switch (sopt->sopt_dir) { 1429 case SOPT_SET: 1430 switch (sopt->sopt_name) { 1431 case IP_OPTIONS: 1432#ifdef notyet 1433 case IP_RETOPTS: 1434#endif 1435 { 1436 struct mbuf *m; 1437 if (sopt->sopt_valsize > MLEN) { 1438 error = EMSGSIZE; 1439 break; 1440 } 1441 MGET(m, sopt->sopt_td ? M_TRYWAIT : M_DONTWAIT, MT_HEADER); 1442 if (m == 0) { 1443 error = ENOBUFS; 1444 break; 1445 } 1446 m->m_len = sopt->sopt_valsize; 1447 error = sooptcopyin(sopt, mtod(m, char *), m->m_len, 1448 m->m_len); 1449 1450 return (ip_pcbopts(sopt->sopt_name, &inp->inp_options, 1451 m)); 1452 } 1453 1454 case IP_TOS: 1455 case IP_TTL: 1456 case IP_RECVOPTS: 1457 case IP_RECVRETOPTS: 1458 case IP_RECVDSTADDR: 1459 case IP_RECVTTL: 1460 case IP_RECVIF: 1461 case IP_FAITH: 1462 case IP_ONESBCAST: 1463 error = sooptcopyin(sopt, &optval, sizeof optval, 1464 sizeof optval); 1465 if (error) 1466 break; 1467 1468 switch (sopt->sopt_name) { 1469 case IP_TOS: 1470 inp->inp_ip_tos = optval; 1471 break; 1472 1473 case IP_TTL: 1474 inp->inp_ip_ttl = optval; 1475 break; 1476#define OPTSET(bit) \ 1477 if (optval) \ 1478 inp->inp_flags |= bit; \ 1479 else \ 1480 inp->inp_flags &= ~bit; 1481 1482 case IP_RECVOPTS: 1483 OPTSET(INP_RECVOPTS); 1484 break; 1485 1486 case IP_RECVRETOPTS: 1487 OPTSET(INP_RECVRETOPTS); 1488 break; 1489 1490 case IP_RECVDSTADDR: 1491 OPTSET(INP_RECVDSTADDR); 1492 break; 1493 1494 case IP_RECVTTL: 1495 OPTSET(INP_RECVTTL); 1496 break; 1497 1498 case IP_RECVIF: 1499 OPTSET(INP_RECVIF); 1500 break; 1501 1502 case IP_FAITH: 1503 OPTSET(INP_FAITH); 1504 break; 1505 1506 case IP_ONESBCAST: 1507 OPTSET(INP_ONESBCAST); 1508 break; 1509 } 1510 break; 1511#undef OPTSET 1512 1513 case IP_MULTICAST_IF: 1514 case IP_MULTICAST_VIF: 1515 case IP_MULTICAST_TTL: 1516 case IP_MULTICAST_LOOP: 1517 case IP_ADD_MEMBERSHIP: 1518 case IP_DROP_MEMBERSHIP: 1519 error = ip_setmoptions(sopt, &inp->inp_moptions); 1520 break; 1521 1522 case IP_PORTRANGE: 1523 error = sooptcopyin(sopt, &optval, sizeof optval, 1524 sizeof optval); 1525 if (error) 1526 break; 1527 1528 switch (optval) { 1529 case IP_PORTRANGE_DEFAULT: 1530 inp->inp_flags &= ~(INP_LOWPORT); 1531 inp->inp_flags &= ~(INP_HIGHPORT); 1532 break; 1533 1534 case IP_PORTRANGE_HIGH: 1535 inp->inp_flags &= ~(INP_LOWPORT); 1536 inp->inp_flags |= INP_HIGHPORT; 1537 break; 1538 1539 case IP_PORTRANGE_LOW: 1540 inp->inp_flags &= ~(INP_HIGHPORT); 1541 inp->inp_flags |= INP_LOWPORT; 1542 break; 1543 1544 default: 1545 error = EINVAL; 1546 break; 1547 } 1548 break; 1549 1550#if defined(IPSEC) || defined(FAST_IPSEC) 1551 case IP_IPSEC_POLICY: 1552 { 1553 caddr_t req; 1554 size_t len = 0; 1555 int priv; 1556 struct mbuf *m; 1557 int optname; 1558 1559 if ((error = soopt_getm(sopt, &m)) != 0) /* XXX */ 1560 break; 1561 if ((error = soopt_mcopyin(sopt, m)) != 0) /* XXX */ 1562 break; 1563 priv = (sopt->sopt_td != NULL && 1564 suser(sopt->sopt_td) != 0) ? 0 : 1; 1565 req = mtod(m, caddr_t); 1566 len = m->m_len; 1567 optname = sopt->sopt_name; 1568 error = ipsec4_set_policy(inp, optname, req, len, priv); 1569 m_freem(m); 1570 break; 1571 } 1572#endif /*IPSEC*/ 1573 1574 default: 1575 error = ENOPROTOOPT; 1576 break; 1577 } 1578 break; 1579 1580 case SOPT_GET: 1581 switch (sopt->sopt_name) { 1582 case IP_OPTIONS: 1583 case IP_RETOPTS: 1584 if (inp->inp_options) 1585 error = sooptcopyout(sopt, 1586 mtod(inp->inp_options, 1587 char *), 1588 inp->inp_options->m_len); 1589 else 1590 sopt->sopt_valsize = 0; 1591 break; 1592 1593 case IP_TOS: 1594 case IP_TTL: 1595 case IP_RECVOPTS: 1596 case IP_RECVRETOPTS: 1597 case IP_RECVDSTADDR: 1598 case IP_RECVTTL: 1599 case IP_RECVIF: 1600 case IP_PORTRANGE: 1601 case IP_FAITH: 1602 case IP_ONESBCAST: 1603 switch (sopt->sopt_name) { 1604 1605 case IP_TOS: 1606 optval = inp->inp_ip_tos; 1607 break; 1608 1609 case IP_TTL: 1610 optval = inp->inp_ip_ttl; 1611 break; 1612 1613#define OPTBIT(bit) (inp->inp_flags & bit ? 1 : 0) 1614 1615 case IP_RECVOPTS: 1616 optval = OPTBIT(INP_RECVOPTS); 1617 break; 1618 1619 case IP_RECVRETOPTS: 1620 optval = OPTBIT(INP_RECVRETOPTS); 1621 break; 1622 1623 case IP_RECVDSTADDR: 1624 optval = OPTBIT(INP_RECVDSTADDR); 1625 break; 1626 1627 case IP_RECVTTL: 1628 optval = OPTBIT(INP_RECVTTL); 1629 break; 1630 1631 case IP_RECVIF: 1632 optval = OPTBIT(INP_RECVIF); 1633 break; 1634 1635 case IP_PORTRANGE: 1636 if (inp->inp_flags & INP_HIGHPORT) 1637 optval = IP_PORTRANGE_HIGH; 1638 else if (inp->inp_flags & INP_LOWPORT) 1639 optval = IP_PORTRANGE_LOW; 1640 else 1641 optval = 0; 1642 break; 1643 1644 case IP_FAITH: 1645 optval = OPTBIT(INP_FAITH); 1646 break; 1647 1648 case IP_ONESBCAST: 1649 optval = OPTBIT(INP_ONESBCAST); 1650 break; 1651 } 1652 error = sooptcopyout(sopt, &optval, sizeof optval); 1653 break; 1654 1655 case IP_MULTICAST_IF: 1656 case IP_MULTICAST_VIF: 1657 case IP_MULTICAST_TTL: 1658 case IP_MULTICAST_LOOP: 1659 case IP_ADD_MEMBERSHIP: 1660 case IP_DROP_MEMBERSHIP: 1661 error = ip_getmoptions(sopt, inp->inp_moptions); 1662 break; 1663 1664#if defined(IPSEC) || defined(FAST_IPSEC) 1665 case IP_IPSEC_POLICY: 1666 { 1667 struct mbuf *m = NULL; 1668 caddr_t req = NULL; 1669 size_t len = 0; 1670 1671 if (m != 0) { 1672 req = mtod(m, caddr_t); 1673 len = m->m_len; 1674 } 1675 error = ipsec4_get_policy(sotoinpcb(so), req, len, &m); 1676 if (error == 0) 1677 error = soopt_mcopyout(sopt, m); /* XXX */ 1678 if (error == 0) 1679 m_freem(m); 1680 break; 1681 } 1682#endif /*IPSEC*/ 1683 1684 default: 1685 error = ENOPROTOOPT; 1686 break; 1687 } 1688 break; 1689 } 1690 return (error); 1691} 1692 1693/* 1694 * Set up IP options in pcb for insertion in output packets. 1695 * Store in mbuf with pointer in pcbopt, adding pseudo-option 1696 * with destination address if source routed. 1697 */ 1698static int 1699ip_pcbopts(optname, pcbopt, m) 1700 int optname; 1701 struct mbuf **pcbopt; 1702 register struct mbuf *m; 1703{ 1704 register int cnt, optlen; 1705 register u_char *cp; 1706 u_char opt; 1707 1708 /* turn off any old options */ 1709 if (*pcbopt) 1710 (void)m_free(*pcbopt); 1711 *pcbopt = 0; 1712 if (m == (struct mbuf *)0 || m->m_len == 0) { 1713 /* 1714 * Only turning off any previous options. 1715 */ 1716 if (m) 1717 (void)m_free(m); 1718 return (0); 1719 } 1720 1721 if (m->m_len % sizeof(int32_t)) 1722 goto bad; 1723 /* 1724 * IP first-hop destination address will be stored before 1725 * actual options; move other options back 1726 * and clear it when none present. 1727 */ 1728 if (m->m_data + m->m_len + sizeof(struct in_addr) >= &m->m_dat[MLEN]) 1729 goto bad; 1730 cnt = m->m_len; 1731 m->m_len += sizeof(struct in_addr); 1732 cp = mtod(m, u_char *) + sizeof(struct in_addr); 1733 bcopy(mtod(m, void *), cp, (unsigned)cnt); 1734 bzero(mtod(m, void *), sizeof(struct in_addr)); 1735 1736 for (; cnt > 0; cnt -= optlen, cp += optlen) { 1737 opt = cp[IPOPT_OPTVAL]; 1738 if (opt == IPOPT_EOL) 1739 break; 1740 if (opt == IPOPT_NOP) 1741 optlen = 1; 1742 else { 1743 if (cnt < IPOPT_OLEN + sizeof(*cp)) 1744 goto bad; 1745 optlen = cp[IPOPT_OLEN]; 1746 if (optlen < IPOPT_OLEN + sizeof(*cp) || optlen > cnt) 1747 goto bad; 1748 } 1749 switch (opt) { 1750 1751 default: 1752 break; 1753 1754 case IPOPT_LSRR: 1755 case IPOPT_SSRR: 1756 /* 1757 * user process specifies route as: 1758 * ->A->B->C->D 1759 * D must be our final destination (but we can't 1760 * check that since we may not have connected yet). 1761 * A is first hop destination, which doesn't appear in 1762 * actual IP option, but is stored before the options. 1763 */ 1764 if (optlen < IPOPT_MINOFF - 1 + sizeof(struct in_addr)) 1765 goto bad; 1766 m->m_len -= sizeof(struct in_addr); 1767 cnt -= sizeof(struct in_addr); 1768 optlen -= sizeof(struct in_addr); 1769 cp[IPOPT_OLEN] = optlen; 1770 /* 1771 * Move first hop before start of options. 1772 */ 1773 bcopy((caddr_t)&cp[IPOPT_OFFSET+1], mtod(m, caddr_t), 1774 sizeof(struct in_addr)); 1775 /* 1776 * Then copy rest of options back 1777 * to close up the deleted entry. 1778 */ 1779 bcopy((&cp[IPOPT_OFFSET+1] + sizeof(struct in_addr)), 1780 &cp[IPOPT_OFFSET+1], 1781 (unsigned)cnt + sizeof(struct in_addr)); 1782 break; 1783 } 1784 } 1785 if (m->m_len > MAX_IPOPTLEN + sizeof(struct in_addr)) 1786 goto bad; 1787 *pcbopt = m; 1788 return (0); 1789 1790bad: 1791 (void)m_free(m); 1792 return (EINVAL); 1793} 1794 1795/* 1796 * XXX 1797 * The whole multicast option thing needs to be re-thought. 1798 * Several of these options are equally applicable to non-multicast 1799 * transmission, and one (IP_MULTICAST_TTL) totally duplicates a 1800 * standard option (IP_TTL). 1801 */ 1802 1803/* 1804 * following RFC1724 section 3.3, 0.0.0.0/8 is interpreted as interface index. 1805 */ 1806static struct ifnet * 1807ip_multicast_if(a, ifindexp) 1808 struct in_addr *a; 1809 int *ifindexp; 1810{ 1811 int ifindex; 1812 struct ifnet *ifp; 1813 1814 if (ifindexp) 1815 *ifindexp = 0; 1816 if (ntohl(a->s_addr) >> 24 == 0) { 1817 ifindex = ntohl(a->s_addr) & 0xffffff; 1818 if (ifindex < 0 || if_index < ifindex) 1819 return NULL; 1820 ifp = ifnet_byindex(ifindex); 1821 if (ifindexp) 1822 *ifindexp = ifindex; 1823 } else { 1824 INADDR_TO_IFP(*a, ifp); 1825 } 1826 return ifp; 1827} 1828 1829/* 1830 * Set the IP multicast options in response to user setsockopt(). 1831 */ 1832static int 1833ip_setmoptions(sopt, imop) 1834 struct sockopt *sopt; 1835 struct ip_moptions **imop; 1836{ 1837 int error = 0; 1838 int i; 1839 struct in_addr addr; 1840 struct ip_mreq mreq; 1841 struct ifnet *ifp; 1842 struct ip_moptions *imo = *imop; 1843 struct route ro; 1844 struct sockaddr_in *dst; 1845 int ifindex; 1846 int s; 1847 1848 if (imo == NULL) { 1849 /* 1850 * No multicast option buffer attached to the pcb; 1851 * allocate one and initialize to default values. 1852 */ 1853 imo = (struct ip_moptions*)malloc(sizeof(*imo), M_IPMOPTS, 1854 M_WAITOK); 1855 1856 if (imo == NULL) 1857 return (ENOBUFS); 1858 *imop = imo; 1859 imo->imo_multicast_ifp = NULL; 1860 imo->imo_multicast_addr.s_addr = INADDR_ANY; 1861 imo->imo_multicast_vif = -1; 1862 imo->imo_multicast_ttl = IP_DEFAULT_MULTICAST_TTL; 1863 imo->imo_multicast_loop = IP_DEFAULT_MULTICAST_LOOP; 1864 imo->imo_num_memberships = 0; 1865 } 1866 1867 switch (sopt->sopt_name) { 1868 /* store an index number for the vif you wanna use in the send */ 1869 case IP_MULTICAST_VIF: 1870 if (legal_vif_num == 0) { 1871 error = EOPNOTSUPP; 1872 break; 1873 } 1874 error = sooptcopyin(sopt, &i, sizeof i, sizeof i); 1875 if (error) 1876 break; 1877 if (!legal_vif_num(i) && (i != -1)) { 1878 error = EINVAL; 1879 break; 1880 } 1881 imo->imo_multicast_vif = i; 1882 break; 1883 1884 case IP_MULTICAST_IF: 1885 /* 1886 * Select the interface for outgoing multicast packets. 1887 */ 1888 error = sooptcopyin(sopt, &addr, sizeof addr, sizeof addr); 1889 if (error) 1890 break; 1891 /* 1892 * INADDR_ANY is used to remove a previous selection. 1893 * When no interface is selected, a default one is 1894 * chosen every time a multicast packet is sent. 1895 */ 1896 if (addr.s_addr == INADDR_ANY) { 1897 imo->imo_multicast_ifp = NULL; 1898 break; 1899 } 1900 /* 1901 * The selected interface is identified by its local 1902 * IP address. Find the interface and confirm that 1903 * it supports multicasting. 1904 */ 1905 s = splimp(); 1906 ifp = ip_multicast_if(&addr, &ifindex); 1907 if (ifp == NULL || (ifp->if_flags & IFF_MULTICAST) == 0) { 1908 splx(s); 1909 error = EADDRNOTAVAIL; 1910 break; 1911 } 1912 imo->imo_multicast_ifp = ifp; 1913 if (ifindex) 1914 imo->imo_multicast_addr = addr; 1915 else 1916 imo->imo_multicast_addr.s_addr = INADDR_ANY; 1917 splx(s); 1918 break; 1919 1920 case IP_MULTICAST_TTL: 1921 /* 1922 * Set the IP time-to-live for outgoing multicast packets. 1923 * The original multicast API required a char argument, 1924 * which is inconsistent with the rest of the socket API. 1925 * We allow either a char or an int. 1926 */ 1927 if (sopt->sopt_valsize == 1) { 1928 u_char ttl; 1929 error = sooptcopyin(sopt, &ttl, 1, 1); 1930 if (error) 1931 break; 1932 imo->imo_multicast_ttl = ttl; 1933 } else { 1934 u_int ttl; 1935 error = sooptcopyin(sopt, &ttl, sizeof ttl, 1936 sizeof ttl); 1937 if (error) 1938 break; 1939 if (ttl > 255) 1940 error = EINVAL; 1941 else 1942 imo->imo_multicast_ttl = ttl; 1943 } 1944 break; 1945 1946 case IP_MULTICAST_LOOP: 1947 /* 1948 * Set the loopback flag for outgoing multicast packets. 1949 * Must be zero or one. The original multicast API required a 1950 * char argument, which is inconsistent with the rest 1951 * of the socket API. We allow either a char or an int. 1952 */ 1953 if (sopt->sopt_valsize == 1) { 1954 u_char loop; 1955 error = sooptcopyin(sopt, &loop, 1, 1); 1956 if (error) 1957 break; 1958 imo->imo_multicast_loop = !!loop; 1959 } else { 1960 u_int loop; 1961 error = sooptcopyin(sopt, &loop, sizeof loop, 1962 sizeof loop); 1963 if (error) 1964 break; 1965 imo->imo_multicast_loop = !!loop; 1966 } 1967 break; 1968 1969 case IP_ADD_MEMBERSHIP: 1970 /* 1971 * Add a multicast group membership. 1972 * Group must be a valid IP multicast address. 1973 */ 1974 error = sooptcopyin(sopt, &mreq, sizeof mreq, sizeof mreq); 1975 if (error) 1976 break; 1977 1978 if (!IN_MULTICAST(ntohl(mreq.imr_multiaddr.s_addr))) { 1979 error = EINVAL; 1980 break; 1981 } 1982 s = splimp(); 1983 /* 1984 * If no interface address was provided, use the interface of 1985 * the route to the given multicast address. 1986 */ 1987 if (mreq.imr_interface.s_addr == INADDR_ANY) { 1988 bzero((caddr_t)&ro, sizeof(ro)); 1989 dst = (struct sockaddr_in *)&ro.ro_dst; 1990 dst->sin_len = sizeof(*dst); 1991 dst->sin_family = AF_INET; 1992 dst->sin_addr = mreq.imr_multiaddr; 1993 rtalloc(&ro); 1994 if (ro.ro_rt == NULL) { 1995 error = EADDRNOTAVAIL; 1996 splx(s); 1997 break; 1998 } 1999 ifp = ro.ro_rt->rt_ifp; 2000 RTFREE(ro.ro_rt); 2001 } 2002 else { 2003 ifp = ip_multicast_if(&mreq.imr_interface, NULL); 2004 } 2005 2006 /* 2007 * See if we found an interface, and confirm that it 2008 * supports multicast. 2009 */ 2010 if (ifp == NULL || (ifp->if_flags & IFF_MULTICAST) == 0) { 2011 error = EADDRNOTAVAIL; 2012 splx(s); 2013 break; 2014 } 2015 /* 2016 * See if the membership already exists or if all the 2017 * membership slots are full. 2018 */ 2019 for (i = 0; i < imo->imo_num_memberships; ++i) { 2020 if (imo->imo_membership[i]->inm_ifp == ifp && 2021 imo->imo_membership[i]->inm_addr.s_addr 2022 == mreq.imr_multiaddr.s_addr) 2023 break; 2024 } 2025 if (i < imo->imo_num_memberships) { 2026 error = EADDRINUSE; 2027 splx(s); 2028 break; 2029 } 2030 if (i == IP_MAX_MEMBERSHIPS) { 2031 error = ETOOMANYREFS; 2032 splx(s); 2033 break; 2034 } 2035 /* 2036 * Everything looks good; add a new record to the multicast 2037 * address list for the given interface. 2038 */ 2039 if ((imo->imo_membership[i] = 2040 in_addmulti(&mreq.imr_multiaddr, ifp)) == NULL) { 2041 error = ENOBUFS; 2042 splx(s); 2043 break; 2044 } 2045 ++imo->imo_num_memberships; 2046 splx(s); 2047 break; 2048 2049 case IP_DROP_MEMBERSHIP: 2050 /* 2051 * Drop a multicast group membership. 2052 * Group must be a valid IP multicast address. 2053 */ 2054 error = sooptcopyin(sopt, &mreq, sizeof mreq, sizeof mreq); 2055 if (error) 2056 break; 2057 2058 if (!IN_MULTICAST(ntohl(mreq.imr_multiaddr.s_addr))) { 2059 error = EINVAL; 2060 break; 2061 } 2062 2063 s = splimp(); 2064 /* 2065 * If an interface address was specified, get a pointer 2066 * to its ifnet structure. 2067 */ 2068 if (mreq.imr_interface.s_addr == INADDR_ANY) 2069 ifp = NULL; 2070 else { 2071 ifp = ip_multicast_if(&mreq.imr_interface, NULL); 2072 if (ifp == NULL) { 2073 error = EADDRNOTAVAIL; 2074 splx(s); 2075 break; 2076 } 2077 } 2078 /* 2079 * Find the membership in the membership array. 2080 */ 2081 for (i = 0; i < imo->imo_num_memberships; ++i) { 2082 if ((ifp == NULL || 2083 imo->imo_membership[i]->inm_ifp == ifp) && 2084 imo->imo_membership[i]->inm_addr.s_addr == 2085 mreq.imr_multiaddr.s_addr) 2086 break; 2087 } 2088 if (i == imo->imo_num_memberships) { 2089 error = EADDRNOTAVAIL; 2090 splx(s); 2091 break; 2092 } 2093 /* 2094 * Give up the multicast address record to which the 2095 * membership points. 2096 */ 2097 in_delmulti(imo->imo_membership[i]); 2098 /* 2099 * Remove the gap in the membership array. 2100 */ 2101 for (++i; i < imo->imo_num_memberships; ++i) 2102 imo->imo_membership[i-1] = imo->imo_membership[i]; 2103 --imo->imo_num_memberships; 2104 splx(s); 2105 break; 2106 2107 default: 2108 error = EOPNOTSUPP; 2109 break; 2110 } 2111 2112 /* 2113 * If all options have default values, no need to keep the mbuf. 2114 */ 2115 if (imo->imo_multicast_ifp == NULL && 2116 imo->imo_multicast_vif == -1 && 2117 imo->imo_multicast_ttl == IP_DEFAULT_MULTICAST_TTL && 2118 imo->imo_multicast_loop == IP_DEFAULT_MULTICAST_LOOP && 2119 imo->imo_num_memberships == 0) { 2120 free(*imop, M_IPMOPTS); 2121 *imop = NULL; 2122 } 2123 2124 return (error); 2125} 2126 2127/* 2128 * Return the IP multicast options in response to user getsockopt(). 2129 */ 2130static int 2131ip_getmoptions(sopt, imo) 2132 struct sockopt *sopt; 2133 register struct ip_moptions *imo; 2134{ 2135 struct in_addr addr; 2136 struct in_ifaddr *ia; 2137 int error, optval; 2138 u_char coptval; 2139 2140 error = 0; 2141 switch (sopt->sopt_name) { 2142 case IP_MULTICAST_VIF: 2143 if (imo != NULL) 2144 optval = imo->imo_multicast_vif; 2145 else 2146 optval = -1; 2147 error = sooptcopyout(sopt, &optval, sizeof optval); 2148 break; 2149 2150 case IP_MULTICAST_IF: 2151 if (imo == NULL || imo->imo_multicast_ifp == NULL) 2152 addr.s_addr = INADDR_ANY; 2153 else if (imo->imo_multicast_addr.s_addr) { 2154 /* return the value user has set */ 2155 addr = imo->imo_multicast_addr; 2156 } else { 2157 IFP_TO_IA(imo->imo_multicast_ifp, ia); 2158 addr.s_addr = (ia == NULL) ? INADDR_ANY 2159 : IA_SIN(ia)->sin_addr.s_addr; 2160 } 2161 error = sooptcopyout(sopt, &addr, sizeof addr); 2162 break; 2163 2164 case IP_MULTICAST_TTL: 2165 if (imo == 0) 2166 optval = coptval = IP_DEFAULT_MULTICAST_TTL; 2167 else 2168 optval = coptval = imo->imo_multicast_ttl; 2169 if (sopt->sopt_valsize == 1) 2170 error = sooptcopyout(sopt, &coptval, 1); 2171 else 2172 error = sooptcopyout(sopt, &optval, sizeof optval); 2173 break; 2174 2175 case IP_MULTICAST_LOOP: 2176 if (imo == 0) 2177 optval = coptval = IP_DEFAULT_MULTICAST_LOOP; 2178 else 2179 optval = coptval = imo->imo_multicast_loop; 2180 if (sopt->sopt_valsize == 1) 2181 error = sooptcopyout(sopt, &coptval, 1); 2182 else 2183 error = sooptcopyout(sopt, &optval, sizeof optval); 2184 break; 2185 2186 default: 2187 error = ENOPROTOOPT; 2188 break; 2189 } 2190 return (error); 2191} 2192 2193/* 2194 * Discard the IP multicast options. 2195 */ 2196void 2197ip_freemoptions(imo) 2198 register struct ip_moptions *imo; 2199{ 2200 register int i; 2201 2202 if (imo != NULL) { 2203 for (i = 0; i < imo->imo_num_memberships; ++i) 2204 in_delmulti(imo->imo_membership[i]); 2205 free(imo, M_IPMOPTS); 2206 } 2207} 2208 2209/* 2210 * Routine called from ip_output() to loop back a copy of an IP multicast 2211 * packet to the input queue of a specified interface. Note that this 2212 * calls the output routine of the loopback "driver", but with an interface 2213 * pointer that might NOT be a loopback interface -- evil, but easier than 2214 * replicating that code here. 2215 */ 2216static void 2217ip_mloopback(ifp, m, dst, hlen) 2218 struct ifnet *ifp; 2219 register struct mbuf *m; 2220 register struct sockaddr_in *dst; 2221 int hlen; 2222{ 2223 register struct ip *ip; 2224 struct mbuf *copym; 2225 2226 copym = m_copy(m, 0, M_COPYALL); 2227 if (copym != NULL && (copym->m_flags & M_EXT || copym->m_len < hlen)) 2228 copym = m_pullup(copym, hlen); 2229 if (copym != NULL) { 2230 /* 2231 * We don't bother to fragment if the IP length is greater 2232 * than the interface's MTU. Can this possibly matter? 2233 */ 2234 ip = mtod(copym, struct ip *); 2235 ip->ip_len = htons(ip->ip_len); 2236 ip->ip_off = htons(ip->ip_off); 2237 ip->ip_sum = 0; 2238 ip->ip_sum = in_cksum(copym, hlen); 2239 /* 2240 * NB: 2241 * It's not clear whether there are any lingering 2242 * reentrancy problems in other areas which might 2243 * be exposed by using ip_input directly (in 2244 * particular, everything which modifies the packet 2245 * in-place). Yet another option is using the 2246 * protosw directly to deliver the looped back 2247 * packet. For the moment, we'll err on the side 2248 * of safety by using if_simloop(). 2249 */ 2250#if 1 /* XXX */ 2251 if (dst->sin_family != AF_INET) { 2252 printf("ip_mloopback: bad address family %d\n", 2253 dst->sin_family); 2254 dst->sin_family = AF_INET; 2255 } 2256#endif 2257 2258#ifdef notdef 2259 copym->m_pkthdr.rcvif = ifp; 2260 ip_input(copym); 2261#else 2262 /* if the checksum hasn't been computed, mark it as valid */ 2263 if (copym->m_pkthdr.csum_flags & CSUM_DELAY_DATA) { 2264 copym->m_pkthdr.csum_flags |= 2265 CSUM_DATA_VALID | CSUM_PSEUDO_HDR; 2266 copym->m_pkthdr.csum_data = 0xffff; 2267 } 2268 if_simloop(ifp, copym, dst->sin_family, 0); 2269#endif 2270 } 2271} 2272