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