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