ip_output.c revision 55009
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 55009 1999-12-22 19:13:38Z shin $ 35 */ 36 37#define _IP_VHL 38 39#include "opt_ipfw.h" 40#include "opt_ipdn.h" 41#include "opt_ipdivert.h" 42#include "opt_ipfilter.h" 43#include "opt_ipsec.h" 44 45#include <sys/param.h> 46#include <sys/systm.h> 47#include <sys/kernel.h> 48#include <sys/malloc.h> 49#include <sys/mbuf.h> 50#include <sys/protosw.h> 51#include <sys/socket.h> 52#include <sys/socketvar.h> 53#include <sys/proc.h> 54 55#include <net/if.h> 56#include <net/route.h> 57 58#include <netinet/in.h> 59#include <netinet/in_systm.h> 60#include <netinet/ip.h> 61#include <netinet/in_pcb.h> 62#include <netinet/in_var.h> 63#include <netinet/ip_var.h> 64 65#include "faith.h" 66 67#ifdef vax 68#include <machine/mtpr.h> 69#endif 70#include <machine/in_cksum.h> 71 72static MALLOC_DEFINE(M_IPMOPTS, "ip_moptions", "internet multicast options"); 73 74#ifdef IPSEC 75#include <netinet6/ipsec.h> 76#include <netkey/key.h> 77#ifdef IPSEC_DEBUG 78#include <netkey/key_debug.h> 79#else 80#define KEYDEBUG(lev,arg) 81#endif 82#endif /*IPSEC*/ 83 84#include <netinet/ip_fw.h> 85 86#ifdef DUMMYNET 87#include <netinet/ip_dummynet.h> 88#endif 89 90#ifdef IPFIREWALL_FORWARD_DEBUG 91#define print_ip(a) printf("%ld.%ld.%ld.%ld",(ntohl(a.s_addr)>>24)&0xFF,\ 92 (ntohl(a.s_addr)>>16)&0xFF,\ 93 (ntohl(a.s_addr)>>8)&0xFF,\ 94 (ntohl(a.s_addr))&0xFF); 95#endif 96 97u_short ip_id; 98 99static struct mbuf *ip_insertoptions __P((struct mbuf *, struct mbuf *, int *)); 100static void ip_mloopback 101 __P((struct ifnet *, struct mbuf *, struct sockaddr_in *, int)); 102static int ip_getmoptions 103 __P((struct sockopt *, struct ip_moptions *)); 104static int ip_pcbopts __P((int, struct mbuf **, struct mbuf *)); 105static int ip_setmoptions 106 __P((struct sockopt *, struct ip_moptions **)); 107 108#if defined(IPFILTER_LKM) || defined(IPFILTER) 109int ip_optcopy __P((struct ip *, struct ip *)); 110extern int (*fr_checkp) __P((struct ip *, int, struct ifnet *, int, struct mbuf **)); 111#else 112static int ip_optcopy __P((struct ip *, struct ip *)); 113#endif 114 115 116extern struct protosw inetsw[]; 117 118/* 119 * IP output. The packet in mbuf chain m contains a skeletal IP 120 * header (with len, off, ttl, proto, tos, src, dst). 121 * The mbuf chain containing the packet will be freed. 122 * The mbuf opt, if present, will not be freed. 123 */ 124int 125ip_output(m0, opt, ro, flags, imo) 126 struct mbuf *m0; 127 struct mbuf *opt; 128 struct route *ro; 129 int flags; 130 struct ip_moptions *imo; 131{ 132 struct ip *ip, *mhip; 133 struct ifnet *ifp; 134 struct mbuf *m = m0; 135 int hlen = sizeof (struct ip); 136 int len, off, error = 0; 137 struct sockaddr_in *dst; 138 struct in_ifaddr *ia; 139 int isbroadcast; 140#ifdef IPSEC 141 struct route iproute; 142 struct socket *so; 143 struct secpolicy *sp = NULL; 144#endif 145 u_int16_t divert_cookie; /* firewall cookie */ 146#ifdef IPFIREWALL_FORWARD 147 int fwd_rewrite_src = 0; 148#endif 149 struct ip_fw_chain *rule = NULL; 150 151#ifdef IPDIVERT 152 /* Get and reset firewall cookie */ 153 divert_cookie = ip_divert_cookie; 154 ip_divert_cookie = 0; 155#else 156 divert_cookie = 0; 157#endif 158 159#ifdef IPSEC 160 /* 161 * NOTE: If IP_SOCKINMRCVIF flag is set, 'socket *' is kept in 162 * m->m_pkthdr.rcvif for later IPSEC check. In this case, 163 * m->m_pkthdr will be NULL cleared after the contents is saved in 164 * 'so'. 165 * NULL clearance of rcvif should be natural because the packet should 166 * have been sent from my own socket and has no rcvif in this case. 167 * It is also necessary because someone might consider it as 168 * 'ifnet *', and cause SEGV. 169 */ 170 if ((flags & IP_SOCKINMRCVIF) != 0) { 171#if defined(IPFIREWALL) && defined(DUMMYNET) 172 if (m->m_type == MT_DUMMYNET) { 173 so = (struct socket *)m->m_next->m_pkthdr.rcvif; 174 m->m_next->m_pkthdr.rcvif = NULL; 175 } else 176#endif 177 { 178 so = (struct socket *)m->m_pkthdr.rcvif; 179 m->m_pkthdr.rcvif = NULL; 180 } 181 } else 182 so = NULL; 183#endif /*IPSEC*/ 184 185#if defined(IPFIREWALL) && defined(DUMMYNET) 186 /* 187 * dummynet packet are prepended a vestigial mbuf with 188 * m_type = MT_DUMMYNET and m_data pointing to the matching 189 * rule. 190 */ 191 if (m->m_type == MT_DUMMYNET) { 192 /* 193 * the packet was already tagged, so part of the 194 * processing was already done, and we need to go down. 195 * opt, flags and imo have already been used, and now 196 * they are used to hold ifp, dst and NULL, respectively. 197 */ 198 rule = (struct ip_fw_chain *)(m->m_data) ; 199 dst = (struct sockaddr_in *)((struct dn_pkt *)m)->dn_dst; 200 m0 = m = m->m_next ; 201 ip = mtod(m, struct ip *); 202 ifp = (struct ifnet *)opt; 203 hlen = IP_VHL_HL(ip->ip_vhl) << 2 ; 204 opt = NULL ; 205 goto sendit; 206 } else 207 rule = NULL ; 208#endif 209 210#ifdef DIAGNOSTIC 211 if ((m->m_flags & M_PKTHDR) == 0) 212 panic("ip_output no HDR"); 213 if (!ro) 214 panic("ip_output no route, proto = %d", 215 mtod(m, struct ip *)->ip_p); 216#endif 217 if (opt) { 218 m = ip_insertoptions(m, opt, &len); 219 hlen = len; 220 } 221 ip = mtod(m, struct ip *); 222 /* 223 * Fill in IP header. 224 */ 225 if ((flags & (IP_FORWARDING|IP_RAWOUTPUT)) == 0) { 226 ip->ip_vhl = IP_MAKE_VHL(IPVERSION, hlen >> 2); 227 ip->ip_off &= IP_DF; 228 ip->ip_id = htons(ip_id++); 229 ipstat.ips_localout++; 230 } else { 231 hlen = IP_VHL_HL(ip->ip_vhl) << 2; 232 } 233 234 dst = (struct sockaddr_in *)&ro->ro_dst; 235 /* 236 * If there is a cached route, 237 * check that it is to the same destination 238 * and is still up. If not, free it and try again. 239 */ 240 if (ro->ro_rt && ((ro->ro_rt->rt_flags & RTF_UP) == 0 || 241 dst->sin_addr.s_addr != ip->ip_dst.s_addr)) { 242 RTFREE(ro->ro_rt); 243 ro->ro_rt = (struct rtentry *)0; 244 } 245 if (ro->ro_rt == 0) { 246 dst->sin_family = AF_INET; 247 dst->sin_len = sizeof(*dst); 248 dst->sin_addr = ip->ip_dst; 249 } 250 /* 251 * If routing to interface only, 252 * short circuit routing lookup. 253 */ 254#define ifatoia(ifa) ((struct in_ifaddr *)(ifa)) 255#define sintosa(sin) ((struct sockaddr *)(sin)) 256 if (flags & IP_ROUTETOIF) { 257 if ((ia = ifatoia(ifa_ifwithdstaddr(sintosa(dst)))) == 0 && 258 (ia = ifatoia(ifa_ifwithnet(sintosa(dst)))) == 0) { 259 ipstat.ips_noroute++; 260 error = ENETUNREACH; 261 goto bad; 262 } 263 ifp = ia->ia_ifp; 264 ip->ip_ttl = 1; 265 isbroadcast = in_broadcast(dst->sin_addr, ifp); 266 } else { 267 /* 268 * If this is the case, we probably don't want to allocate 269 * a protocol-cloned route since we didn't get one from the 270 * ULP. This lets TCP do its thing, while not burdening 271 * forwarding or ICMP with the overhead of cloning a route. 272 * Of course, we still want to do any cloning requested by 273 * the link layer, as this is probably required in all cases 274 * for correct operation (as it is for ARP). 275 */ 276 if (ro->ro_rt == 0) 277 rtalloc_ign(ro, RTF_PRCLONING); 278 if (ro->ro_rt == 0) { 279 ipstat.ips_noroute++; 280 error = EHOSTUNREACH; 281 goto bad; 282 } 283 ia = ifatoia(ro->ro_rt->rt_ifa); 284 ifp = ro->ro_rt->rt_ifp; 285 ro->ro_rt->rt_use++; 286 if (ro->ro_rt->rt_flags & RTF_GATEWAY) 287 dst = (struct sockaddr_in *)ro->ro_rt->rt_gateway; 288 if (ro->ro_rt->rt_flags & RTF_HOST) 289 isbroadcast = (ro->ro_rt->rt_flags & RTF_BROADCAST); 290 else 291 isbroadcast = in_broadcast(dst->sin_addr, ifp); 292 } 293 if (IN_MULTICAST(ntohl(ip->ip_dst.s_addr))) { 294 struct in_multi *inm; 295 296 m->m_flags |= M_MCAST; 297 /* 298 * IP destination address is multicast. Make sure "dst" 299 * still points to the address in "ro". (It may have been 300 * changed to point to a gateway address, above.) 301 */ 302 dst = (struct sockaddr_in *)&ro->ro_dst; 303 /* 304 * See if the caller provided any multicast options 305 */ 306 if (imo != NULL) { 307 ip->ip_ttl = imo->imo_multicast_ttl; 308 if (imo->imo_multicast_ifp != NULL) 309 ifp = imo->imo_multicast_ifp; 310 if (imo->imo_multicast_vif != -1) 311 ip->ip_src.s_addr = 312 ip_mcast_src(imo->imo_multicast_vif); 313 } else 314 ip->ip_ttl = IP_DEFAULT_MULTICAST_TTL; 315 /* 316 * Confirm that the outgoing interface supports multicast. 317 */ 318 if ((imo == NULL) || (imo->imo_multicast_vif == -1)) { 319 if ((ifp->if_flags & IFF_MULTICAST) == 0) { 320 ipstat.ips_noroute++; 321 error = ENETUNREACH; 322 goto bad; 323 } 324 } 325 /* 326 * If source address not specified yet, use address 327 * of outgoing interface. 328 */ 329 if (ip->ip_src.s_addr == INADDR_ANY) { 330 register struct in_ifaddr *ia1; 331 332 for (ia1 = in_ifaddrhead.tqh_first; ia1; 333 ia1 = ia1->ia_link.tqe_next) 334 if (ia1->ia_ifp == ifp) { 335 ip->ip_src = IA_SIN(ia1)->sin_addr; 336 break; 337 } 338 } 339 340 IN_LOOKUP_MULTI(ip->ip_dst, ifp, inm); 341 if (inm != NULL && 342 (imo == NULL || imo->imo_multicast_loop)) { 343 /* 344 * If we belong to the destination multicast group 345 * on the outgoing interface, and the caller did not 346 * forbid loopback, loop back a copy. 347 */ 348 ip_mloopback(ifp, m, dst, hlen); 349 } 350 else { 351 /* 352 * If we are acting as a multicast router, perform 353 * multicast forwarding as if the packet had just 354 * arrived on the interface to which we are about 355 * to send. The multicast forwarding function 356 * recursively calls this function, using the 357 * IP_FORWARDING flag to prevent infinite recursion. 358 * 359 * Multicasts that are looped back by ip_mloopback(), 360 * above, will be forwarded by the ip_input() routine, 361 * if necessary. 362 */ 363 if (ip_mrouter && (flags & IP_FORWARDING) == 0) { 364 /* 365 * Check if rsvp daemon is running. If not, don't 366 * set ip_moptions. This ensures that the packet 367 * is multicast and not just sent down one link 368 * as prescribed by rsvpd. 369 */ 370 if (!rsvp_on) 371 imo = NULL; 372 if (ip_mforward(ip, ifp, m, imo) != 0) { 373 m_freem(m); 374 goto done; 375 } 376 } 377 } 378 379 /* 380 * Multicasts with a time-to-live of zero may be looped- 381 * back, above, but must not be transmitted on a network. 382 * Also, multicasts addressed to the loopback interface 383 * are not sent -- the above call to ip_mloopback() will 384 * loop back a copy if this host actually belongs to the 385 * destination group on the loopback interface. 386 */ 387 if (ip->ip_ttl == 0 || ifp->if_flags & IFF_LOOPBACK) { 388 m_freem(m); 389 goto done; 390 } 391 392 goto sendit; 393 } 394#ifndef notdef 395 /* 396 * If source address not specified yet, use address 397 * of outgoing interface. 398 */ 399 if (ip->ip_src.s_addr == INADDR_ANY) { 400 ip->ip_src = IA_SIN(ia)->sin_addr; 401#ifdef IPFIREWALL_FORWARD 402 /* Keep note that we did this - if the firewall changes 403 * the next-hop, our interface may change, changing the 404 * default source IP. It's a shame so much effort happens 405 * twice. Oh well. 406 */ 407 fwd_rewrite_src++; 408#endif /* IPFIREWALL_FORWARD */ 409 } 410#endif /* notdef */ 411 /* 412 * Verify that we have any chance at all of being able to queue 413 * the packet or packet fragments 414 */ 415 if ((ifp->if_snd.ifq_len + ip->ip_len / ifp->if_mtu + 1) >= 416 ifp->if_snd.ifq_maxlen) { 417 error = ENOBUFS; 418 goto bad; 419 } 420 421 /* 422 * Look for broadcast address and 423 * and verify user is allowed to send 424 * such a packet. 425 */ 426 if (isbroadcast) { 427 if ((ifp->if_flags & IFF_BROADCAST) == 0) { 428 error = EADDRNOTAVAIL; 429 goto bad; 430 } 431 if ((flags & IP_ALLOWBROADCAST) == 0) { 432 error = EACCES; 433 goto bad; 434 } 435 /* don't allow broadcast messages to be fragmented */ 436 if ((u_short)ip->ip_len > ifp->if_mtu) { 437 error = EMSGSIZE; 438 goto bad; 439 } 440 m->m_flags |= M_BCAST; 441 } else { 442 m->m_flags &= ~M_BCAST; 443 } 444 445sendit: 446 /* 447 * IpHack's section. 448 * - Xlate: translate packet's addr/port (NAT). 449 * - Firewall: deny/allow/etc. 450 * - Wrap: fake packet's addr/port <unimpl.> 451 * - Encapsulate: put it in another IP and send out. <unimp.> 452 */ 453#if defined(IPFILTER) || defined(IPFILTER_LKM) 454 if (fr_checkp) { 455 struct mbuf *m1 = m; 456 457 if ((error = (*fr_checkp)(ip, hlen, ifp, 1, &m1)) || !m1) 458 goto done; 459 ip = mtod(m = m1, struct ip *); 460 } 461#endif 462 463 /* 464 * Check with the firewall... 465 */ 466 if (ip_fw_chk_ptr) { 467 struct sockaddr_in *old = dst; 468 469 off = (*ip_fw_chk_ptr)(&ip, 470 hlen, ifp, &divert_cookie, &m, &rule, &dst); 471 /* 472 * On return we must do the following: 473 * m == NULL -> drop the pkt 474 * 1<=off<= 0xffff -> DIVERT 475 * (off & 0x10000) -> send to a DUMMYNET pipe 476 * (off & 0x20000) -> TEE the packet 477 * dst != old -> IPFIREWALL_FORWARD 478 * off==0, dst==old -> accept 479 * If some of the above modules is not compiled in, then 480 * we should't have to check the corresponding condition 481 * (because the ipfw control socket should not accept 482 * unsupported rules), but better play safe and drop 483 * packets in case of doubt. 484 */ 485 if (!m) { /* firewall said to reject */ 486 error = EACCES; 487 goto done; 488 } 489 if (off == 0 && dst == old) /* common case */ 490 goto pass ; 491#ifdef DUMMYNET 492 if ((off & IP_FW_PORT_DYNT_FLAG) != 0) { 493 /* 494 * pass the pkt to dummynet. Need to include 495 * pipe number, m, ifp, ro, dst because these are 496 * not recomputed in the next pass. 497 * All other parameters have been already used and 498 * so they are not needed anymore. 499 * XXX note: if the ifp or ro entry are deleted 500 * while a pkt is in dummynet, we are in trouble! 501 */ 502 dummynet_io(off & 0xffff, DN_TO_IP_OUT, m,ifp,ro,dst,rule, 503 flags); 504 goto done; 505 } 506#endif 507#ifdef IPDIVERT 508 if (off != 0 && (off & IP_FW_PORT_DYNT_FLAG) == 0) { 509 struct mbuf *clone = NULL; 510 511 /* Clone packet if we're doing a 'tee' */ 512 if ((off & IP_FW_PORT_TEE_FLAG) != 0) 513 clone = m_dup(m, M_DONTWAIT); 514 515 /* Restore packet header fields to original values */ 516 HTONS(ip->ip_len); 517 HTONS(ip->ip_off); 518 519 /* Deliver packet to divert input routine */ 520 ip_divert_cookie = divert_cookie; 521 divert_packet(m, 0, off & 0xffff); 522 523 /* If 'tee', continue with original packet */ 524 if (clone != NULL) { 525 m = clone; 526 ip = mtod(m, struct ip *); 527 goto pass; 528 } 529 goto done; 530 } 531#endif 532 533#ifdef IPFIREWALL_FORWARD 534 /* Here we check dst to make sure it's directly reachable on the 535 * interface we previously thought it was. 536 * If it isn't (which may be likely in some situations) we have 537 * to re-route it (ie, find a route for the next-hop and the 538 * associated interface) and set them here. This is nested 539 * forwarding which in most cases is undesirable, except where 540 * such control is nigh impossible. So we do it here. 541 * And I'm babbling. 542 */ 543 if (off == 0 && old != dst) { 544 struct in_ifaddr *ia; 545 546 /* It's changed... */ 547 /* There must be a better way to do this next line... */ 548 static struct route sro_fwd, *ro_fwd = &sro_fwd; 549#ifdef IPFIREWALL_FORWARD_DEBUG 550 printf("IPFIREWALL_FORWARD: New dst ip: "); 551 print_ip(dst->sin_addr); 552 printf("\n"); 553#endif 554 /* 555 * We need to figure out if we have been forwarded 556 * to a local socket. If so then we should somehow 557 * "loop back" to ip_input, and get directed to the 558 * PCB as if we had received this packet. This is 559 * because it may be dificult to identify the packets 560 * you want to forward until they are being output 561 * and have selected an interface. (e.g. locally 562 * initiated packets) If we used the loopback inteface, 563 * we would not be able to control what happens 564 * as the packet runs through ip_input() as 565 * it is done through a ISR. 566 */ 567 for (ia = TAILQ_FIRST(&in_ifaddrhead); ia; 568 ia = TAILQ_NEXT(ia, ia_link)) { 569 /* 570 * If the addr to forward to is one 571 * of ours, we pretend to 572 * be the destination for this packet. 573 */ 574 if (IA_SIN(ia)->sin_addr.s_addr == 575 dst->sin_addr.s_addr) 576 break; 577 } 578 if (ia) { 579 /* tell ip_input "dont filter" */ 580 ip_fw_fwd_addr = dst; 581 if (m->m_pkthdr.rcvif == NULL) 582 m->m_pkthdr.rcvif = ifunit("lo0"); 583 ip->ip_len = htons((u_short)ip->ip_len); 584 ip->ip_off = htons((u_short)ip->ip_off); 585 ip->ip_sum = 0; 586 if (ip->ip_vhl == IP_VHL_BORING) { 587 ip->ip_sum = in_cksum_hdr(ip); 588 } else { 589 ip->ip_sum = in_cksum(m, hlen); 590 } 591 ip_input(m); 592 goto done; 593 } 594 /* Some of the logic for this was 595 * nicked from above. 596 * 597 * This rewrites the cached route in a local PCB. 598 * Is this what we want to do? 599 */ 600 bcopy(dst, &ro_fwd->ro_dst, sizeof(*dst)); 601 602 ro_fwd->ro_rt = 0; 603 rtalloc_ign(ro_fwd, RTF_PRCLONING); 604 605 if (ro_fwd->ro_rt == 0) { 606 ipstat.ips_noroute++; 607 error = EHOSTUNREACH; 608 goto bad; 609 } 610 611 ia = ifatoia(ro_fwd->ro_rt->rt_ifa); 612 ifp = ro_fwd->ro_rt->rt_ifp; 613 ro_fwd->ro_rt->rt_use++; 614 if (ro_fwd->ro_rt->rt_flags & RTF_GATEWAY) 615 dst = (struct sockaddr_in *)ro_fwd->ro_rt->rt_gateway; 616 if (ro_fwd->ro_rt->rt_flags & RTF_HOST) 617 isbroadcast = 618 (ro_fwd->ro_rt->rt_flags & RTF_BROADCAST); 619 else 620 isbroadcast = in_broadcast(dst->sin_addr, ifp); 621 RTFREE(ro->ro_rt); 622 ro->ro_rt = ro_fwd->ro_rt; 623 dst = (struct sockaddr_in *)&ro_fwd->ro_dst; 624 625 /* 626 * If we added a default src ip earlier, 627 * which would have been gotten from the-then 628 * interface, do it again, from the new one. 629 */ 630 if (fwd_rewrite_src) 631 ip->ip_src = IA_SIN(ia)->sin_addr; 632 goto pass ; 633 } 634#endif /* IPFIREWALL_FORWARD */ 635 /* 636 * if we get here, none of the above matches, and 637 * we have to drop the pkt 638 */ 639 m_freem(m); 640 error = EACCES; /* not sure this is the right error msg */ 641 goto done; 642 } 643 644pass: 645#ifdef IPSEC 646 /* get SP for this packet */ 647 if (so == NULL) 648 sp = ipsec4_getpolicybyaddr(m, IPSEC_DIR_OUTBOUND, flags, &error); 649 else 650 sp = ipsec4_getpolicybysock(m, IPSEC_DIR_OUTBOUND, so, &error); 651 652 if (sp == NULL) { 653 ipsecstat.out_inval++; 654 goto bad; 655 } 656 657 error = 0; 658 659 /* check policy */ 660 switch (sp->policy) { 661 case IPSEC_POLICY_DISCARD: 662 /* 663 * This packet is just discarded. 664 */ 665 ipsecstat.out_polvio++; 666 goto bad; 667 668 case IPSEC_POLICY_BYPASS: 669 case IPSEC_POLICY_NONE: 670 /* no need to do IPsec. */ 671 goto skip_ipsec; 672 673 case IPSEC_POLICY_IPSEC: 674 if (sp->req == NULL) { 675 /* XXX should be panic ? */ 676 printf("ip_output: No IPsec request specified.\n"); 677 error = EINVAL; 678 goto bad; 679 } 680 break; 681 682 case IPSEC_POLICY_ENTRUST: 683 default: 684 printf("ip_output: Invalid policy found. %d\n", sp->policy); 685 } 686 687 ip->ip_len = htons((u_short)ip->ip_len); 688 ip->ip_off = htons((u_short)ip->ip_off); 689 ip->ip_sum = 0; 690 691 { 692 struct ipsec_output_state state; 693 bzero(&state, sizeof(state)); 694 state.m = m; 695 if (flags & IP_ROUTETOIF) { 696 state.ro = &iproute; 697 bzero(&iproute, sizeof(iproute)); 698 } else 699 state.ro = ro; 700 state.dst = (struct sockaddr *)dst; 701 702 error = ipsec4_output(&state, sp, flags); 703 704 m = state.m; 705 if (flags & IP_ROUTETOIF) { 706 /* 707 * if we have tunnel mode SA, we may need to ignore 708 * IP_ROUTETOIF. 709 */ 710 if (state.ro != &iproute || state.ro->ro_rt != NULL) { 711 flags &= ~IP_ROUTETOIF; 712 ro = state.ro; 713 } 714 } else 715 ro = state.ro; 716 dst = (struct sockaddr_in *)state.dst; 717 if (error) { 718 /* mbuf is already reclaimed in ipsec4_output. */ 719 m0 = NULL; 720 switch (error) { 721 case EHOSTUNREACH: 722 case ENETUNREACH: 723 case EMSGSIZE: 724 case ENOBUFS: 725 case ENOMEM: 726 break; 727 default: 728 printf("ip4_output (ipsec): error code %d\n", error); 729 /*fall through*/ 730 case ENOENT: 731 /* don't show these error codes to the user */ 732 error = 0; 733 break; 734 } 735 goto bad; 736 } 737 } 738 739 /* be sure to update variables that are affected by ipsec4_output() */ 740 ip = mtod(m, struct ip *); 741#ifdef _IP_VHL 742 hlen = IP_VHL_HL(ip->ip_vhl) << 2; 743#else 744 hlen = ip->ip_hl << 2; 745#endif 746 if (ro->ro_rt == NULL) { 747 if ((flags & IP_ROUTETOIF) == 0) { 748 printf("ip_output: " 749 "can't update route after IPsec processing\n"); 750 error = EHOSTUNREACH; /*XXX*/ 751 goto bad; 752 } 753 } else { 754 /* nobody uses ia beyond here */ 755 ifp = ro->ro_rt->rt_ifp; 756 } 757 758 /* make it flipped, again. */ 759 ip->ip_len = ntohs((u_short)ip->ip_len); 760 ip->ip_off = ntohs((u_short)ip->ip_off); 761skip_ipsec: 762#endif /*IPSEC*/ 763 764 /* 765 * If small enough for interface, can just send directly. 766 */ 767 if ((u_short)ip->ip_len <= ifp->if_mtu) { 768 ip->ip_len = htons((u_short)ip->ip_len); 769 ip->ip_off = htons((u_short)ip->ip_off); 770 ip->ip_sum = 0; 771 if (ip->ip_vhl == IP_VHL_BORING) { 772 ip->ip_sum = in_cksum_hdr(ip); 773 } else { 774 ip->ip_sum = in_cksum(m, hlen); 775 } 776 error = (*ifp->if_output)(ifp, m, 777 (struct sockaddr *)dst, ro->ro_rt); 778 goto done; 779 } 780 /* 781 * Too large for interface; fragment if possible. 782 * Must be able to put at least 8 bytes per fragment. 783 */ 784 if (ip->ip_off & IP_DF) { 785 error = EMSGSIZE; 786 /* 787 * This case can happen if the user changed the MTU 788 * of an interface after enabling IP on it. Because 789 * most netifs don't keep track of routes pointing to 790 * them, there is no way for one to update all its 791 * routes when the MTU is changed. 792 */ 793 if ((ro->ro_rt->rt_flags & (RTF_UP | RTF_HOST)) 794 && !(ro->ro_rt->rt_rmx.rmx_locks & RTV_MTU) 795 && (ro->ro_rt->rt_rmx.rmx_mtu > ifp->if_mtu)) { 796 ro->ro_rt->rt_rmx.rmx_mtu = ifp->if_mtu; 797 } 798 ipstat.ips_cantfrag++; 799 goto bad; 800 } 801 len = (ifp->if_mtu - hlen) &~ 7; 802 if (len < 8) { 803 error = EMSGSIZE; 804 goto bad; 805 } 806 807 { 808 int mhlen, firstlen = len; 809 struct mbuf **mnext = &m->m_nextpkt; 810 811 /* 812 * Loop through length of segment after first fragment, 813 * make new header and copy data of each part and link onto chain. 814 */ 815 m0 = m; 816 mhlen = sizeof (struct ip); 817 for (off = hlen + len; off < (u_short)ip->ip_len; off += len) { 818 MGETHDR(m, M_DONTWAIT, MT_HEADER); 819 if (m == 0) { 820 error = ENOBUFS; 821 ipstat.ips_odropped++; 822 goto sendorfree; 823 } 824 m->m_flags |= (m0->m_flags & M_MCAST); 825 m->m_data += max_linkhdr; 826 mhip = mtod(m, struct ip *); 827 *mhip = *ip; 828 if (hlen > sizeof (struct ip)) { 829 mhlen = ip_optcopy(ip, mhip) + sizeof (struct ip); 830 mhip->ip_vhl = IP_MAKE_VHL(IPVERSION, mhlen >> 2); 831 } 832 m->m_len = mhlen; 833 mhip->ip_off = ((off - hlen) >> 3) + (ip->ip_off & ~IP_MF); 834 if (ip->ip_off & IP_MF) 835 mhip->ip_off |= IP_MF; 836 if (off + len >= (u_short)ip->ip_len) 837 len = (u_short)ip->ip_len - off; 838 else 839 mhip->ip_off |= IP_MF; 840 mhip->ip_len = htons((u_short)(len + mhlen)); 841 m->m_next = m_copy(m0, off, len); 842 if (m->m_next == 0) { 843 (void) m_free(m); 844 error = ENOBUFS; /* ??? */ 845 ipstat.ips_odropped++; 846 goto sendorfree; 847 } 848 m->m_pkthdr.len = mhlen + len; 849 m->m_pkthdr.rcvif = (struct ifnet *)0; 850 mhip->ip_off = htons((u_short)mhip->ip_off); 851 mhip->ip_sum = 0; 852 if (mhip->ip_vhl == IP_VHL_BORING) { 853 mhip->ip_sum = in_cksum_hdr(mhip); 854 } else { 855 mhip->ip_sum = in_cksum(m, mhlen); 856 } 857 *mnext = m; 858 mnext = &m->m_nextpkt; 859 ipstat.ips_ofragments++; 860 } 861 /* 862 * Update first fragment by trimming what's been copied out 863 * and updating header, then send each fragment (in order). 864 */ 865 m = m0; 866 m_adj(m, hlen + firstlen - (u_short)ip->ip_len); 867 m->m_pkthdr.len = hlen + firstlen; 868 ip->ip_len = htons((u_short)m->m_pkthdr.len); 869 ip->ip_off = htons((u_short)(ip->ip_off | IP_MF)); 870 ip->ip_sum = 0; 871 if (ip->ip_vhl == IP_VHL_BORING) { 872 ip->ip_sum = in_cksum_hdr(ip); 873 } else { 874 ip->ip_sum = in_cksum(m, hlen); 875 } 876sendorfree: 877 for (m = m0; m; m = m0) { 878 m0 = m->m_nextpkt; 879 m->m_nextpkt = 0; 880 if (error == 0) 881 error = (*ifp->if_output)(ifp, m, 882 (struct sockaddr *)dst, ro->ro_rt); 883 else 884 m_freem(m); 885 } 886 887 if (error == 0) 888 ipstat.ips_fragmented++; 889 } 890done: 891#ifdef IPSEC 892 if (ro == &iproute && ro->ro_rt) { 893 RTFREE(ro->ro_rt); 894 ro->ro_rt = NULL; 895 } 896 if (sp != NULL) { 897 KEYDEBUG(KEYDEBUG_IPSEC_STAMP, 898 printf("DP ip_output call free SP:%p\n", sp)); 899 key_freesp(sp); 900 } 901#endif /* IPSEC */ 902 return (error); 903bad: 904 m_freem(m0); 905 goto done; 906} 907 908/* 909 * Insert IP options into preformed packet. 910 * Adjust IP destination as required for IP source routing, 911 * as indicated by a non-zero in_addr at the start of the options. 912 * 913 * XXX This routine assumes that the packet has no options in place. 914 */ 915static struct mbuf * 916ip_insertoptions(m, opt, phlen) 917 register struct mbuf *m; 918 struct mbuf *opt; 919 int *phlen; 920{ 921 register struct ipoption *p = mtod(opt, struct ipoption *); 922 struct mbuf *n; 923 register struct ip *ip = mtod(m, struct ip *); 924 unsigned optlen; 925 926 optlen = opt->m_len - sizeof(p->ipopt_dst); 927 if (optlen + (u_short)ip->ip_len > IP_MAXPACKET) 928 return (m); /* XXX should fail */ 929 if (p->ipopt_dst.s_addr) 930 ip->ip_dst = p->ipopt_dst; 931 if (m->m_flags & M_EXT || m->m_data - optlen < m->m_pktdat) { 932 MGETHDR(n, M_DONTWAIT, MT_HEADER); 933 if (n == 0) 934 return (m); 935 n->m_pkthdr.len = m->m_pkthdr.len + optlen; 936 m->m_len -= sizeof(struct ip); 937 m->m_data += sizeof(struct ip); 938 n->m_next = m; 939 m = n; 940 m->m_len = optlen + sizeof(struct ip); 941 m->m_data += max_linkhdr; 942 (void)memcpy(mtod(m, void *), ip, sizeof(struct ip)); 943 } else { 944 m->m_data -= optlen; 945 m->m_len += optlen; 946 m->m_pkthdr.len += optlen; 947 ovbcopy((caddr_t)ip, mtod(m, caddr_t), sizeof(struct ip)); 948 } 949 ip = mtod(m, struct ip *); 950 bcopy(p->ipopt_list, ip + 1, optlen); 951 *phlen = sizeof(struct ip) + optlen; 952 ip->ip_vhl = IP_MAKE_VHL(IPVERSION, *phlen >> 2); 953 ip->ip_len += optlen; 954 return (m); 955} 956 957/* 958 * Copy options from ip to jp, 959 * omitting those not copied during fragmentation. 960 */ 961#if !defined(IPFILTER) && !defined(IPFILTER_LKM) 962static 963#endif 964int 965ip_optcopy(ip, jp) 966 struct ip *ip, *jp; 967{ 968 register u_char *cp, *dp; 969 int opt, optlen, cnt; 970 971 cp = (u_char *)(ip + 1); 972 dp = (u_char *)(jp + 1); 973 cnt = (IP_VHL_HL(ip->ip_vhl) << 2) - sizeof (struct ip); 974 for (; cnt > 0; cnt -= optlen, cp += optlen) { 975 opt = cp[0]; 976 if (opt == IPOPT_EOL) 977 break; 978 if (opt == IPOPT_NOP) { 979 /* Preserve for IP mcast tunnel's LSRR alignment. */ 980 *dp++ = IPOPT_NOP; 981 optlen = 1; 982 continue; 983 } else 984 optlen = cp[IPOPT_OLEN]; 985 /* bogus lengths should have been caught by ip_dooptions */ 986 if (optlen > cnt) 987 optlen = cnt; 988 if (IPOPT_COPIED(opt)) { 989 bcopy(cp, dp, optlen); 990 dp += optlen; 991 } 992 } 993 for (optlen = dp - (u_char *)(jp+1); optlen & 0x3; optlen++) 994 *dp++ = IPOPT_EOL; 995 return (optlen); 996} 997 998/* 999 * IP socket option processing. 1000 */ 1001int 1002ip_ctloutput(so, sopt) 1003 struct socket *so; 1004 struct sockopt *sopt; 1005{ 1006 struct inpcb *inp = sotoinpcb(so); 1007 int error, optval; 1008 1009 error = optval = 0; 1010 if (sopt->sopt_level != IPPROTO_IP) { 1011 return (EINVAL); 1012 } 1013 1014 switch (sopt->sopt_dir) { 1015 case SOPT_SET: 1016 switch (sopt->sopt_name) { 1017 case IP_OPTIONS: 1018#ifdef notyet 1019 case IP_RETOPTS: 1020#endif 1021 { 1022 struct mbuf *m; 1023 if (sopt->sopt_valsize > MLEN) { 1024 error = EMSGSIZE; 1025 break; 1026 } 1027 MGET(m, sopt->sopt_p ? M_WAIT : M_DONTWAIT, MT_HEADER); 1028 if (m == 0) { 1029 error = ENOBUFS; 1030 break; 1031 } 1032 m->m_len = sopt->sopt_valsize; 1033 error = sooptcopyin(sopt, mtod(m, char *), m->m_len, 1034 m->m_len); 1035 1036 return (ip_pcbopts(sopt->sopt_name, &inp->inp_options, 1037 m)); 1038 } 1039 1040 case IP_TOS: 1041 case IP_TTL: 1042 case IP_RECVOPTS: 1043 case IP_RECVRETOPTS: 1044 case IP_RECVDSTADDR: 1045 case IP_RECVIF: 1046#if defined(NFAITH) && NFAITH > 0 1047 case IP_FAITH: 1048#endif 1049 error = sooptcopyin(sopt, &optval, sizeof optval, 1050 sizeof optval); 1051 if (error) 1052 break; 1053 1054 switch (sopt->sopt_name) { 1055 case IP_TOS: 1056 inp->inp_ip_tos = optval; 1057 break; 1058 1059 case IP_TTL: 1060 inp->inp_ip_ttl = optval; 1061 break; 1062#define OPTSET(bit) \ 1063 if (optval) \ 1064 inp->inp_flags |= bit; \ 1065 else \ 1066 inp->inp_flags &= ~bit; 1067 1068 case IP_RECVOPTS: 1069 OPTSET(INP_RECVOPTS); 1070 break; 1071 1072 case IP_RECVRETOPTS: 1073 OPTSET(INP_RECVRETOPTS); 1074 break; 1075 1076 case IP_RECVDSTADDR: 1077 OPTSET(INP_RECVDSTADDR); 1078 break; 1079 1080 case IP_RECVIF: 1081 OPTSET(INP_RECVIF); 1082 break; 1083 1084#if defined(NFAITH) && NFAITH > 0 1085 case IP_FAITH: 1086 OPTSET(INP_FAITH); 1087 break; 1088#endif 1089 } 1090 break; 1091#undef OPTSET 1092 1093 case IP_MULTICAST_IF: 1094 case IP_MULTICAST_VIF: 1095 case IP_MULTICAST_TTL: 1096 case IP_MULTICAST_LOOP: 1097 case IP_ADD_MEMBERSHIP: 1098 case IP_DROP_MEMBERSHIP: 1099 error = ip_setmoptions(sopt, &inp->inp_moptions); 1100 break; 1101 1102 case IP_PORTRANGE: 1103 error = sooptcopyin(sopt, &optval, sizeof optval, 1104 sizeof optval); 1105 if (error) 1106 break; 1107 1108 switch (optval) { 1109 case IP_PORTRANGE_DEFAULT: 1110 inp->inp_flags &= ~(INP_LOWPORT); 1111 inp->inp_flags &= ~(INP_HIGHPORT); 1112 break; 1113 1114 case IP_PORTRANGE_HIGH: 1115 inp->inp_flags &= ~(INP_LOWPORT); 1116 inp->inp_flags |= INP_HIGHPORT; 1117 break; 1118 1119 case IP_PORTRANGE_LOW: 1120 inp->inp_flags &= ~(INP_HIGHPORT); 1121 inp->inp_flags |= INP_LOWPORT; 1122 break; 1123 1124 default: 1125 error = EINVAL; 1126 break; 1127 } 1128 break; 1129 1130#ifdef IPSEC 1131 case IP_IPSEC_POLICY: 1132 { 1133 caddr_t req; 1134 int priv; 1135 struct mbuf *m; 1136 int optname; 1137 1138 if ((error = soopt_getm(sopt, &m)) != 0) /* XXX */ 1139 break; 1140 if ((error = soopt_mcopyin(sopt, m)) != 0) /* XXX */ 1141 break; 1142 priv = (sopt->sopt_p != NULL && 1143 suser(sopt->sopt_p) != 0) ? 0 : 1; 1144 req = mtod(m, caddr_t); 1145 optname = sopt->sopt_name; 1146 error = ipsec4_set_policy(inp, optname, req, priv); 1147 m_freem(m); 1148 break; 1149 } 1150#endif /*IPSEC*/ 1151 1152 default: 1153 error = ENOPROTOOPT; 1154 break; 1155 } 1156 break; 1157 1158 case SOPT_GET: 1159 switch (sopt->sopt_name) { 1160 case IP_OPTIONS: 1161 case IP_RETOPTS: 1162 if (inp->inp_options) 1163 error = sooptcopyout(sopt, 1164 mtod(inp->inp_options, 1165 char *), 1166 inp->inp_options->m_len); 1167 else 1168 sopt->sopt_valsize = 0; 1169 break; 1170 1171 case IP_TOS: 1172 case IP_TTL: 1173 case IP_RECVOPTS: 1174 case IP_RECVRETOPTS: 1175 case IP_RECVDSTADDR: 1176 case IP_RECVIF: 1177 case IP_PORTRANGE: 1178#if defined(NFAITH) && NFAITH > 0 1179 case IP_FAITH: 1180#endif 1181 switch (sopt->sopt_name) { 1182 1183 case IP_TOS: 1184 optval = inp->inp_ip_tos; 1185 break; 1186 1187 case IP_TTL: 1188 optval = inp->inp_ip_ttl; 1189 break; 1190 1191#define OPTBIT(bit) (inp->inp_flags & bit ? 1 : 0) 1192 1193 case IP_RECVOPTS: 1194 optval = OPTBIT(INP_RECVOPTS); 1195 break; 1196 1197 case IP_RECVRETOPTS: 1198 optval = OPTBIT(INP_RECVRETOPTS); 1199 break; 1200 1201 case IP_RECVDSTADDR: 1202 optval = OPTBIT(INP_RECVDSTADDR); 1203 break; 1204 1205 case IP_RECVIF: 1206 optval = OPTBIT(INP_RECVIF); 1207 break; 1208 1209 case IP_PORTRANGE: 1210 if (inp->inp_flags & INP_HIGHPORT) 1211 optval = IP_PORTRANGE_HIGH; 1212 else if (inp->inp_flags & INP_LOWPORT) 1213 optval = IP_PORTRANGE_LOW; 1214 else 1215 optval = 0; 1216 break; 1217 1218#if defined(NFAITH) && NFAITH > 0 1219 case IP_FAITH: 1220 optval = OPTBIT(INP_FAITH); 1221 break; 1222#endif 1223 } 1224 error = sooptcopyout(sopt, &optval, sizeof optval); 1225 break; 1226 1227 case IP_MULTICAST_IF: 1228 case IP_MULTICAST_VIF: 1229 case IP_MULTICAST_TTL: 1230 case IP_MULTICAST_LOOP: 1231 case IP_ADD_MEMBERSHIP: 1232 case IP_DROP_MEMBERSHIP: 1233 error = ip_getmoptions(sopt, inp->inp_moptions); 1234 break; 1235 1236#ifdef IPSEC 1237 case IP_IPSEC_POLICY: 1238 { 1239 struct mbuf *m; 1240 caddr_t req = NULL; 1241 1242 if (m != 0) 1243 req = mtod(m, caddr_t); 1244 error = ipsec4_get_policy(sotoinpcb(so), req, &m); 1245 if (error == 0) 1246 error = soopt_mcopyout(sopt, m); /* XXX */ 1247 m_freem(m); 1248 break; 1249 } 1250#endif /*IPSEC*/ 1251 1252 default: 1253 error = ENOPROTOOPT; 1254 break; 1255 } 1256 break; 1257 } 1258 return (error); 1259} 1260 1261/* 1262 * Set up IP options in pcb for insertion in output packets. 1263 * Store in mbuf with pointer in pcbopt, adding pseudo-option 1264 * with destination address if source routed. 1265 */ 1266static int 1267ip_pcbopts(optname, pcbopt, m) 1268 int optname; 1269 struct mbuf **pcbopt; 1270 register struct mbuf *m; 1271{ 1272 register int cnt, optlen; 1273 register u_char *cp; 1274 u_char opt; 1275 1276 /* turn off any old options */ 1277 if (*pcbopt) 1278 (void)m_free(*pcbopt); 1279 *pcbopt = 0; 1280 if (m == (struct mbuf *)0 || m->m_len == 0) { 1281 /* 1282 * Only turning off any previous options. 1283 */ 1284 if (m) 1285 (void)m_free(m); 1286 return (0); 1287 } 1288 1289#ifndef vax 1290 if (m->m_len % sizeof(int32_t)) 1291 goto bad; 1292#endif 1293 /* 1294 * IP first-hop destination address will be stored before 1295 * actual options; move other options back 1296 * and clear it when none present. 1297 */ 1298 if (m->m_data + m->m_len + sizeof(struct in_addr) >= &m->m_dat[MLEN]) 1299 goto bad; 1300 cnt = m->m_len; 1301 m->m_len += sizeof(struct in_addr); 1302 cp = mtod(m, u_char *) + sizeof(struct in_addr); 1303 ovbcopy(mtod(m, caddr_t), (caddr_t)cp, (unsigned)cnt); 1304 bzero(mtod(m, caddr_t), sizeof(struct in_addr)); 1305 1306 for (; cnt > 0; cnt -= optlen, cp += optlen) { 1307 opt = cp[IPOPT_OPTVAL]; 1308 if (opt == IPOPT_EOL) 1309 break; 1310 if (opt == IPOPT_NOP) 1311 optlen = 1; 1312 else { 1313 optlen = cp[IPOPT_OLEN]; 1314 if (optlen <= IPOPT_OLEN || optlen > cnt) 1315 goto bad; 1316 } 1317 switch (opt) { 1318 1319 default: 1320 break; 1321 1322 case IPOPT_LSRR: 1323 case IPOPT_SSRR: 1324 /* 1325 * user process specifies route as: 1326 * ->A->B->C->D 1327 * D must be our final destination (but we can't 1328 * check that since we may not have connected yet). 1329 * A is first hop destination, which doesn't appear in 1330 * actual IP option, but is stored before the options. 1331 */ 1332 if (optlen < IPOPT_MINOFF - 1 + sizeof(struct in_addr)) 1333 goto bad; 1334 m->m_len -= sizeof(struct in_addr); 1335 cnt -= sizeof(struct in_addr); 1336 optlen -= sizeof(struct in_addr); 1337 cp[IPOPT_OLEN] = optlen; 1338 /* 1339 * Move first hop before start of options. 1340 */ 1341 bcopy((caddr_t)&cp[IPOPT_OFFSET+1], mtod(m, caddr_t), 1342 sizeof(struct in_addr)); 1343 /* 1344 * Then copy rest of options back 1345 * to close up the deleted entry. 1346 */ 1347 ovbcopy((caddr_t)(&cp[IPOPT_OFFSET+1] + 1348 sizeof(struct in_addr)), 1349 (caddr_t)&cp[IPOPT_OFFSET+1], 1350 (unsigned)cnt + sizeof(struct in_addr)); 1351 break; 1352 } 1353 } 1354 if (m->m_len > MAX_IPOPTLEN + sizeof(struct in_addr)) 1355 goto bad; 1356 *pcbopt = m; 1357 return (0); 1358 1359bad: 1360 (void)m_free(m); 1361 return (EINVAL); 1362} 1363 1364/* 1365 * XXX 1366 * The whole multicast option thing needs to be re-thought. 1367 * Several of these options are equally applicable to non-multicast 1368 * transmission, and one (IP_MULTICAST_TTL) totally duplicates a 1369 * standard option (IP_TTL). 1370 */ 1371/* 1372 * Set the IP multicast options in response to user setsockopt(). 1373 */ 1374static int 1375ip_setmoptions(sopt, imop) 1376 struct sockopt *sopt; 1377 struct ip_moptions **imop; 1378{ 1379 int error = 0; 1380 int i; 1381 struct in_addr addr; 1382 struct ip_mreq mreq; 1383 struct ifnet *ifp; 1384 struct ip_moptions *imo = *imop; 1385 struct route ro; 1386 struct sockaddr_in *dst; 1387 int s; 1388 1389 if (imo == NULL) { 1390 /* 1391 * No multicast option buffer attached to the pcb; 1392 * allocate one and initialize to default values. 1393 */ 1394 imo = (struct ip_moptions*)malloc(sizeof(*imo), M_IPMOPTS, 1395 M_WAITOK); 1396 1397 if (imo == NULL) 1398 return (ENOBUFS); 1399 *imop = imo; 1400 imo->imo_multicast_ifp = NULL; 1401 imo->imo_multicast_vif = -1; 1402 imo->imo_multicast_ttl = IP_DEFAULT_MULTICAST_TTL; 1403 imo->imo_multicast_loop = IP_DEFAULT_MULTICAST_LOOP; 1404 imo->imo_num_memberships = 0; 1405 } 1406 1407 switch (sopt->sopt_name) { 1408 /* store an index number for the vif you wanna use in the send */ 1409 case IP_MULTICAST_VIF: 1410 if (legal_vif_num == 0) { 1411 error = EOPNOTSUPP; 1412 break; 1413 } 1414 error = sooptcopyin(sopt, &i, sizeof i, sizeof i); 1415 if (error) 1416 break; 1417 if (!legal_vif_num(i) && (i != -1)) { 1418 error = EINVAL; 1419 break; 1420 } 1421 imo->imo_multicast_vif = i; 1422 break; 1423 1424 case IP_MULTICAST_IF: 1425 /* 1426 * Select the interface for outgoing multicast packets. 1427 */ 1428 error = sooptcopyin(sopt, &addr, sizeof addr, sizeof addr); 1429 if (error) 1430 break; 1431 /* 1432 * INADDR_ANY is used to remove a previous selection. 1433 * When no interface is selected, a default one is 1434 * chosen every time a multicast packet is sent. 1435 */ 1436 if (addr.s_addr == INADDR_ANY) { 1437 imo->imo_multicast_ifp = NULL; 1438 break; 1439 } 1440 /* 1441 * The selected interface is identified by its local 1442 * IP address. Find the interface and confirm that 1443 * it supports multicasting. 1444 */ 1445 s = splimp(); 1446 INADDR_TO_IFP(addr, ifp); 1447 if (ifp == NULL || (ifp->if_flags & IFF_MULTICAST) == 0) { 1448 splx(s); 1449 error = EADDRNOTAVAIL; 1450 break; 1451 } 1452 imo->imo_multicast_ifp = ifp; 1453 splx(s); 1454 break; 1455 1456 case IP_MULTICAST_TTL: 1457 /* 1458 * Set the IP time-to-live for outgoing multicast packets. 1459 * The original multicast API required a char argument, 1460 * which is inconsistent with the rest of the socket API. 1461 * We allow either a char or an int. 1462 */ 1463 if (sopt->sopt_valsize == 1) { 1464 u_char ttl; 1465 error = sooptcopyin(sopt, &ttl, 1, 1); 1466 if (error) 1467 break; 1468 imo->imo_multicast_ttl = ttl; 1469 } else { 1470 u_int ttl; 1471 error = sooptcopyin(sopt, &ttl, sizeof ttl, 1472 sizeof ttl); 1473 if (error) 1474 break; 1475 if (ttl > 255) 1476 error = EINVAL; 1477 else 1478 imo->imo_multicast_ttl = ttl; 1479 } 1480 break; 1481 1482 case IP_MULTICAST_LOOP: 1483 /* 1484 * Set the loopback flag for outgoing multicast packets. 1485 * Must be zero or one. The original multicast API required a 1486 * char argument, which is inconsistent with the rest 1487 * of the socket API. We allow either a char or an int. 1488 */ 1489 if (sopt->sopt_valsize == 1) { 1490 u_char loop; 1491 error = sooptcopyin(sopt, &loop, 1, 1); 1492 if (error) 1493 break; 1494 imo->imo_multicast_loop = !!loop; 1495 } else { 1496 u_int loop; 1497 error = sooptcopyin(sopt, &loop, sizeof loop, 1498 sizeof loop); 1499 if (error) 1500 break; 1501 imo->imo_multicast_loop = !!loop; 1502 } 1503 break; 1504 1505 case IP_ADD_MEMBERSHIP: 1506 /* 1507 * Add a multicast group membership. 1508 * Group must be a valid IP multicast address. 1509 */ 1510 error = sooptcopyin(sopt, &mreq, sizeof mreq, sizeof mreq); 1511 if (error) 1512 break; 1513 1514 if (!IN_MULTICAST(ntohl(mreq.imr_multiaddr.s_addr))) { 1515 error = EINVAL; 1516 break; 1517 } 1518 s = splimp(); 1519 /* 1520 * If no interface address was provided, use the interface of 1521 * the route to the given multicast address. 1522 */ 1523 if (mreq.imr_interface.s_addr == INADDR_ANY) { 1524 bzero((caddr_t)&ro, sizeof(ro)); 1525 dst = (struct sockaddr_in *)&ro.ro_dst; 1526 dst->sin_len = sizeof(*dst); 1527 dst->sin_family = AF_INET; 1528 dst->sin_addr = mreq.imr_multiaddr; 1529 rtalloc(&ro); 1530 if (ro.ro_rt == NULL) { 1531 error = EADDRNOTAVAIL; 1532 splx(s); 1533 break; 1534 } 1535 ifp = ro.ro_rt->rt_ifp; 1536 rtfree(ro.ro_rt); 1537 } 1538 else { 1539 INADDR_TO_IFP(mreq.imr_interface, ifp); 1540 } 1541 1542 /* 1543 * See if we found an interface, and confirm that it 1544 * supports multicast. 1545 */ 1546 if (ifp == NULL || (ifp->if_flags & IFF_MULTICAST) == 0) { 1547 error = EADDRNOTAVAIL; 1548 splx(s); 1549 break; 1550 } 1551 /* 1552 * See if the membership already exists or if all the 1553 * membership slots are full. 1554 */ 1555 for (i = 0; i < imo->imo_num_memberships; ++i) { 1556 if (imo->imo_membership[i]->inm_ifp == ifp && 1557 imo->imo_membership[i]->inm_addr.s_addr 1558 == mreq.imr_multiaddr.s_addr) 1559 break; 1560 } 1561 if (i < imo->imo_num_memberships) { 1562 error = EADDRINUSE; 1563 splx(s); 1564 break; 1565 } 1566 if (i == IP_MAX_MEMBERSHIPS) { 1567 error = ETOOMANYREFS; 1568 splx(s); 1569 break; 1570 } 1571 /* 1572 * Everything looks good; add a new record to the multicast 1573 * address list for the given interface. 1574 */ 1575 if ((imo->imo_membership[i] = 1576 in_addmulti(&mreq.imr_multiaddr, ifp)) == NULL) { 1577 error = ENOBUFS; 1578 splx(s); 1579 break; 1580 } 1581 ++imo->imo_num_memberships; 1582 splx(s); 1583 break; 1584 1585 case IP_DROP_MEMBERSHIP: 1586 /* 1587 * Drop a multicast group membership. 1588 * Group must be a valid IP multicast address. 1589 */ 1590 error = sooptcopyin(sopt, &mreq, sizeof mreq, sizeof mreq); 1591 if (error) 1592 break; 1593 1594 if (!IN_MULTICAST(ntohl(mreq.imr_multiaddr.s_addr))) { 1595 error = EINVAL; 1596 break; 1597 } 1598 1599 s = splimp(); 1600 /* 1601 * If an interface address was specified, get a pointer 1602 * to its ifnet structure. 1603 */ 1604 if (mreq.imr_interface.s_addr == INADDR_ANY) 1605 ifp = NULL; 1606 else { 1607 INADDR_TO_IFP(mreq.imr_interface, ifp); 1608 if (ifp == NULL) { 1609 error = EADDRNOTAVAIL; 1610 splx(s); 1611 break; 1612 } 1613 } 1614 /* 1615 * Find the membership in the membership array. 1616 */ 1617 for (i = 0; i < imo->imo_num_memberships; ++i) { 1618 if ((ifp == NULL || 1619 imo->imo_membership[i]->inm_ifp == ifp) && 1620 imo->imo_membership[i]->inm_addr.s_addr == 1621 mreq.imr_multiaddr.s_addr) 1622 break; 1623 } 1624 if (i == imo->imo_num_memberships) { 1625 error = EADDRNOTAVAIL; 1626 splx(s); 1627 break; 1628 } 1629 /* 1630 * Give up the multicast address record to which the 1631 * membership points. 1632 */ 1633 in_delmulti(imo->imo_membership[i]); 1634 /* 1635 * Remove the gap in the membership array. 1636 */ 1637 for (++i; i < imo->imo_num_memberships; ++i) 1638 imo->imo_membership[i-1] = imo->imo_membership[i]; 1639 --imo->imo_num_memberships; 1640 splx(s); 1641 break; 1642 1643 default: 1644 error = EOPNOTSUPP; 1645 break; 1646 } 1647 1648 /* 1649 * If all options have default values, no need to keep the mbuf. 1650 */ 1651 if (imo->imo_multicast_ifp == NULL && 1652 imo->imo_multicast_vif == -1 && 1653 imo->imo_multicast_ttl == IP_DEFAULT_MULTICAST_TTL && 1654 imo->imo_multicast_loop == IP_DEFAULT_MULTICAST_LOOP && 1655 imo->imo_num_memberships == 0) { 1656 free(*imop, M_IPMOPTS); 1657 *imop = NULL; 1658 } 1659 1660 return (error); 1661} 1662 1663/* 1664 * Return the IP multicast options in response to user getsockopt(). 1665 */ 1666static int 1667ip_getmoptions(sopt, imo) 1668 struct sockopt *sopt; 1669 register struct ip_moptions *imo; 1670{ 1671 struct in_addr addr; 1672 struct in_ifaddr *ia; 1673 int error, optval; 1674 u_char coptval; 1675 1676 error = 0; 1677 switch (sopt->sopt_name) { 1678 case IP_MULTICAST_VIF: 1679 if (imo != NULL) 1680 optval = imo->imo_multicast_vif; 1681 else 1682 optval = -1; 1683 error = sooptcopyout(sopt, &optval, sizeof optval); 1684 break; 1685 1686 case IP_MULTICAST_IF: 1687 if (imo == NULL || imo->imo_multicast_ifp == NULL) 1688 addr.s_addr = INADDR_ANY; 1689 else { 1690 IFP_TO_IA(imo->imo_multicast_ifp, ia); 1691 addr.s_addr = (ia == NULL) ? INADDR_ANY 1692 : IA_SIN(ia)->sin_addr.s_addr; 1693 } 1694 error = sooptcopyout(sopt, &addr, sizeof addr); 1695 break; 1696 1697 case IP_MULTICAST_TTL: 1698 if (imo == 0) 1699 optval = coptval = IP_DEFAULT_MULTICAST_TTL; 1700 else 1701 optval = coptval = imo->imo_multicast_ttl; 1702 if (sopt->sopt_valsize == 1) 1703 error = sooptcopyout(sopt, &coptval, 1); 1704 else 1705 error = sooptcopyout(sopt, &optval, sizeof optval); 1706 break; 1707 1708 case IP_MULTICAST_LOOP: 1709 if (imo == 0) 1710 optval = coptval = IP_DEFAULT_MULTICAST_LOOP; 1711 else 1712 optval = coptval = imo->imo_multicast_loop; 1713 if (sopt->sopt_valsize == 1) 1714 error = sooptcopyout(sopt, &coptval, 1); 1715 else 1716 error = sooptcopyout(sopt, &optval, sizeof optval); 1717 break; 1718 1719 default: 1720 error = ENOPROTOOPT; 1721 break; 1722 } 1723 return (error); 1724} 1725 1726/* 1727 * Discard the IP multicast options. 1728 */ 1729void 1730ip_freemoptions(imo) 1731 register struct ip_moptions *imo; 1732{ 1733 register int i; 1734 1735 if (imo != NULL) { 1736 for (i = 0; i < imo->imo_num_memberships; ++i) 1737 in_delmulti(imo->imo_membership[i]); 1738 free(imo, M_IPMOPTS); 1739 } 1740} 1741 1742/* 1743 * Routine called from ip_output() to loop back a copy of an IP multicast 1744 * packet to the input queue of a specified interface. Note that this 1745 * calls the output routine of the loopback "driver", but with an interface 1746 * pointer that might NOT be a loopback interface -- evil, but easier than 1747 * replicating that code here. 1748 */ 1749static void 1750ip_mloopback(ifp, m, dst, hlen) 1751 struct ifnet *ifp; 1752 register struct mbuf *m; 1753 register struct sockaddr_in *dst; 1754 int hlen; 1755{ 1756 register struct ip *ip; 1757 struct mbuf *copym; 1758 1759 copym = m_copy(m, 0, M_COPYALL); 1760 if (copym != NULL && (copym->m_flags & M_EXT || copym->m_len < hlen)) 1761 copym = m_pullup(copym, hlen); 1762 if (copym != NULL) { 1763 /* 1764 * We don't bother to fragment if the IP length is greater 1765 * than the interface's MTU. Can this possibly matter? 1766 */ 1767 ip = mtod(copym, struct ip *); 1768 ip->ip_len = htons((u_short)ip->ip_len); 1769 ip->ip_off = htons((u_short)ip->ip_off); 1770 ip->ip_sum = 0; 1771 if (ip->ip_vhl == IP_VHL_BORING) { 1772 ip->ip_sum = in_cksum_hdr(ip); 1773 } else { 1774 ip->ip_sum = in_cksum(copym, hlen); 1775 } 1776 /* 1777 * NB: 1778 * It's not clear whether there are any lingering 1779 * reentrancy problems in other areas which might 1780 * be exposed by using ip_input directly (in 1781 * particular, everything which modifies the packet 1782 * in-place). Yet another option is using the 1783 * protosw directly to deliver the looped back 1784 * packet. For the moment, we'll err on the side 1785 * of safety by using if_simloop(). 1786 */ 1787#if 1 /* XXX */ 1788 if (dst->sin_family != AF_INET) { 1789 printf("ip_mloopback: bad address family %d\n", 1790 dst->sin_family); 1791 dst->sin_family = AF_INET; 1792 } 1793#endif 1794 1795#ifdef notdef 1796 copym->m_pkthdr.rcvif = ifp; 1797 ip_input(copym); 1798#else 1799 if_simloop(ifp, copym, (struct sockaddr *)dst, 0); 1800#endif 1801 } 1802} 1803