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