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