ip_output.c revision 97020
1116518Sphk/* 2116518Sphk * Copyright (c) 1982, 1986, 1988, 1990, 1993 3116518Sphk * The Regents of the University of California. All rights reserved. 4116518Sphk * 5116518Sphk * Redistribution and use in source and binary forms, with or without 6116518Sphk * modification, are permitted provided that the following conditions 7116518Sphk * are met: 8116518Sphk * 1. Redistributions of source code must retain the above copyright 9116518Sphk * notice, this list of conditions and the following disclaimer. 10116518Sphk * 2. Redistributions in binary form must reproduce the above copyright 11116518Sphk * notice, this list of conditions and the following disclaimer in the 12116518Sphk * documentation and/or other materials provided with the distribution. 13116518Sphk * 3. All advertising materials mentioning features or use of this software 14116518Sphk * must display the following acknowledgement: 15116518Sphk * This product includes software developed by the University of 16116518Sphk * California, Berkeley and its contributors. 17116518Sphk * 4. Neither the name of the University nor the names of its contributors 18116518Sphk * may be used to endorse or promote products derived from this software 19116518Sphk * without specific prior written permission. 20116518Sphk * 21116518Sphk * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 22116518Sphk * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 23116518Sphk * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 24116518Sphk * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 25116518Sphk * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 26116518Sphk * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 27116518Sphk * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 28116518Sphk * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 29116518Sphk * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 30116518Sphk * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 31116518Sphk * SUCH DAMAGE. 32116518Sphk * 33116518Sphk * @(#)ip_output.c 8.3 (Berkeley) 1/21/94 34116518Sphk * $FreeBSD: head/sys/netinet/ip_output.c 97020 2002-05-20 22:05:13Z arr $ 35116518Sphk */ 36116518Sphk 37116518Sphk#define _IP_VHL 38116518Sphk 39116518Sphk#include "opt_ipfw.h" 40116518Sphk#include "opt_ipdn.h" 41116518Sphk#include "opt_ipdivert.h" 42116518Sphk#include "opt_ipfilter.h" 43116518Sphk#include "opt_ipsec.h" 44116518Sphk#include "opt_pfil_hooks.h" 45116518Sphk#include "opt_random_ip_id.h" 46116518Sphk 47116518Sphk#include <sys/param.h> 48116518Sphk#include <sys/systm.h> 49116518Sphk#include <sys/kernel.h> 50116518Sphk#include <sys/malloc.h> 51116518Sphk#include <sys/mbuf.h> 52116518Sphk#include <sys/protosw.h> 53116518Sphk#include <sys/socket.h> 54116518Sphk#include <sys/socketvar.h> 55116518Sphk 56116518Sphk#include <net/if.h> 57116518Sphk#include <net/route.h> 58116518Sphk 59116518Sphk#include <netinet/in.h> 60116518Sphk#include <netinet/in_systm.h> 61116518Sphk#include <netinet/ip.h> 62116518Sphk#include <netinet/in_pcb.h> 63116518Sphk#include <netinet/in_var.h> 64116518Sphk#include <netinet/ip_var.h> 65116518Sphk 66116518Sphk#include <machine/in_cksum.h> 67116518Sphk 68116518Sphkstatic MALLOC_DEFINE(M_IPMOPTS, "ip_moptions", "internet multicast options"); 69116518Sphk 70116518Sphk#ifdef IPSEC 71116518Sphk#include <netinet6/ipsec.h> 72116518Sphk#include <netkey/key.h> 73116518Sphk#ifdef IPSEC_DEBUG 74116518Sphk#include <netkey/key_debug.h> 75116518Sphk#else 76116518Sphk#define KEYDEBUG(lev,arg) 77116518Sphk#endif 78116518Sphk#endif /*IPSEC*/ 79116518Sphk 80116518Sphk#include <netinet/ip_fw.h> 81116518Sphk#include <netinet/ip_dummynet.h> 82116518Sphk 83116518Sphk#ifdef IPFIREWALL_FORWARD_DEBUG 84116518Sphk#define print_ip(a) printf("%ld.%ld.%ld.%ld",(ntohl(a.s_addr)>>24)&0xFF,\ 85116518Sphk (ntohl(a.s_addr)>>16)&0xFF,\ 86116518Sphk (ntohl(a.s_addr)>>8)&0xFF,\ 87116518Sphk (ntohl(a.s_addr))&0xFF); 88116518Sphk#endif 89116518Sphk 90116518Sphku_short ip_id; 91116518Sphk 92116518Sphkstatic struct mbuf *ip_insertoptions(struct mbuf *, struct mbuf *, int *); 93116518Sphkstatic struct ifnet *ip_multicast_if(struct in_addr *, int *); 94116518Sphkstatic void ip_mloopback 95116518Sphk (struct ifnet *, struct mbuf *, struct sockaddr_in *, int); 96116518Sphkstatic int ip_getmoptions 97116518Sphk (struct sockopt *, struct ip_moptions *); 98116518Sphkstatic int ip_pcbopts(int, struct mbuf **, struct mbuf *); 99116518Sphkstatic int ip_setmoptions 100116518Sphk (struct sockopt *, struct ip_moptions **); 101116518Sphk 102116518Sphkint ip_optcopy(struct ip *, struct ip *); 103116518Sphk 104116518Sphk 105116518Sphkextern struct protosw inetsw[]; 106116518Sphk 107116518Sphk/* 108116518Sphk * IP output. The packet in mbuf chain m contains a skeletal IP 109116518Sphk * header (with len, off, ttl, proto, tos, src, dst). 110116518Sphk * The mbuf chain containing the packet will be freed. 111116518Sphk * The mbuf opt, if present, will not be freed. 112116518Sphk */ 113116518Sphkint 114116518Sphkip_output(m0, opt, ro, flags, imo) 115116518Sphk struct mbuf *m0; 116116518Sphk struct mbuf *opt; 117116518Sphk struct route *ro; 118116518Sphk int flags; 119116518Sphk struct ip_moptions *imo; 120116518Sphk{ 121116518Sphk struct ip *ip, *mhip; 122116518Sphk struct ifnet *ifp; 123116518Sphk struct mbuf *m = m0; 124116518Sphk int hlen = sizeof (struct ip); 125116518Sphk int len, off, error = 0; 126116518Sphk struct sockaddr_in *dst; 127116518Sphk struct in_ifaddr *ia; 128116518Sphk int isbroadcast, sw_csum; 129116518Sphk struct in_addr pkt_dst; 130116518Sphk#ifdef IPSEC 131116518Sphk struct route iproute; 132116518Sphk struct socket *so = NULL; 133116518Sphk struct secpolicy *sp = NULL; 134116518Sphk#endif 135116518Sphk u_int16_t divert_cookie; /* firewall cookie */ 136116518Sphk#ifdef PFIL_HOOKS 137116518Sphk struct packet_filter_hook *pfh; 138116518Sphk struct mbuf *m1; 139116518Sphk int rv; 140116518Sphk#endif /* PFIL_HOOKS */ 141116518Sphk#ifdef IPFIREWALL_FORWARD 142116518Sphk int fwd_rewrite_src = 0; 143116518Sphk#endif 144116518Sphk struct ip_fw *rule = NULL; 145116518Sphk 146116518Sphk#ifdef IPDIVERT 147116518Sphk /* Get and reset firewall cookie */ 148116518Sphk divert_cookie = ip_divert_cookie; 149116518Sphk ip_divert_cookie = 0; 150116518Sphk#else 151116518Sphk divert_cookie = 0; 152116518Sphk#endif 153116518Sphk 154116518Sphk /* 155116518Sphk * dummynet packet are prepended a vestigial mbuf with 156116518Sphk * m_type = MT_DUMMYNET and m_data pointing to the matching 157116518Sphk * rule. 158116518Sphk */ 159116518Sphk if (m->m_type == MT_DUMMYNET) { 160116518Sphk /* 161116518Sphk * the packet was already tagged, so part of the 162116518Sphk * processing was already done, and we need to go down. 163116518Sphk * Get parameters from the header. 164116518Sphk */ 165116518Sphk rule = (struct ip_fw *)(m->m_data) ; 166116518Sphk opt = NULL ; 167116518Sphk ro = & ( ((struct dn_pkt *)m)->ro ) ; 168116518Sphk imo = NULL ; 169116518Sphk dst = ((struct dn_pkt *)m)->dn_dst ; 170116518Sphk ifp = ((struct dn_pkt *)m)->ifp ; 171116518Sphk flags = ((struct dn_pkt *)m)->flags ; 172116518Sphk 173116518Sphk m0 = m = m->m_next ; 174116518Sphk#ifdef IPSEC 175116518Sphk so = ipsec_getsocket(m); 176116518Sphk (void)ipsec_setsocket(m, NULL); 177116518Sphk#endif 178116518Sphk ip = mtod(m, struct ip *); 179116518Sphk hlen = IP_VHL_HL(ip->ip_vhl) << 2 ; 180116518Sphk ia = ifatoia(ro->ro_rt->rt_ifa); 181116518Sphk goto sendit; 182116518Sphk } else 183116518Sphk rule = NULL ; 184116518Sphk#ifdef IPSEC 185116518Sphk so = ipsec_getsocket(m); 186116518Sphk (void)ipsec_setsocket(m, NULL); 187116518Sphk#endif 188116518Sphk 189116518Sphk#ifdef INVARIANTS 190116518Sphk if ((m->m_flags & M_PKTHDR) == 0) 191116518Sphk panic("ip_output no HDR"); 192116518Sphk if (!ro) 193121366Sphk panic("ip_output no route, proto = %d", 194116518Sphk mtod(m, struct ip *)->ip_p); 195116518Sphk#endif 196116518Sphk if (opt) { 197116518Sphk m = ip_insertoptions(m, opt, &len); 198116518Sphk hlen = len; 199116518Sphk } 200116518Sphk ip = mtod(m, struct ip *); 201116518Sphk pkt_dst = ip_fw_fwd_addr == NULL 202116518Sphk ? ip->ip_dst : ip_fw_fwd_addr->sin_addr; 203116518Sphk 204116518Sphk /* 205116518Sphk * Fill in IP header. 206116518Sphk */ 207116518Sphk if ((flags & (IP_FORWARDING|IP_RAWOUTPUT)) == 0) { 208116518Sphk ip->ip_vhl = IP_MAKE_VHL(IPVERSION, hlen >> 2); 209116518Sphk ip->ip_off &= IP_DF; 210116518Sphk#ifdef RANDOM_IP_ID 211116518Sphk ip->ip_id = ip_randomid(); 212116518Sphk#else 213116518Sphk ip->ip_id = htons(ip_id++); 214116518Sphk#endif 215116518Sphk ipstat.ips_localout++; 216116518Sphk } else { 217116518Sphk hlen = IP_VHL_HL(ip->ip_vhl) << 2; 218116518Sphk } 219116518Sphk 220116518Sphk dst = (struct sockaddr_in *)&ro->ro_dst; 221116518Sphk /* 222116518Sphk * If there is a cached route, 223116518Sphk * check that it is to the same destination 224116518Sphk * and is still up. If not, free it and try again. 225116518Sphk * The address family should also be checked in case of sharing the 226116518Sphk * cache with IPv6. 227116518Sphk */ 228116518Sphk if (ro->ro_rt && ((ro->ro_rt->rt_flags & RTF_UP) == 0 || 229116518Sphk dst->sin_family != AF_INET || 230116518Sphk dst->sin_addr.s_addr != pkt_dst.s_addr)) { 231116518Sphk RTFREE(ro->ro_rt); 232116518Sphk ro->ro_rt = (struct rtentry *)0; 233116518Sphk } 234116518Sphk if (ro->ro_rt == 0) { 235116518Sphk bzero(dst, sizeof(*dst)); 236116518Sphk dst->sin_family = AF_INET; 237116518Sphk dst->sin_len = sizeof(*dst); 238116518Sphk dst->sin_addr = pkt_dst; 239116518Sphk } 240116518Sphk /* 241116518Sphk * If routing to interface only, 242116518Sphk * short circuit routing lookup. 243116518Sphk */ 244116518Sphk if (flags & IP_ROUTETOIF) { 245116518Sphk if ((ia = ifatoia(ifa_ifwithdstaddr(sintosa(dst)))) == 0 && 246116518Sphk (ia = ifatoia(ifa_ifwithnet(sintosa(dst)))) == 0) { 247116518Sphk ipstat.ips_noroute++; 248116518Sphk error = ENETUNREACH; 249116518Sphk goto bad; 250116518Sphk } 251116518Sphk ifp = ia->ia_ifp; 252116518Sphk ip->ip_ttl = 1; 253116518Sphk isbroadcast = in_broadcast(dst->sin_addr, ifp); 254116518Sphk } else if (IN_MULTICAST(ntohl(ip->ip_dst.s_addr)) && 255116518Sphk imo != NULL && imo->imo_multicast_ifp != NULL) { 256116518Sphk /* 257116518Sphk * Bypass the normal routing lookup for multicast 258116518Sphk * packets if the interface is specified. 259116518Sphk */ 260116518Sphk ifp = imo->imo_multicast_ifp; 261116518Sphk IFP_TO_IA(ifp, ia); 262116518Sphk isbroadcast = 0; /* fool gcc */ 263116518Sphk } else { 264116518Sphk /* 265116518Sphk * If this is the case, we probably don't want to allocate 266116518Sphk * a protocol-cloned route since we didn't get one from the 267116518Sphk * ULP. This lets TCP do its thing, while not burdening 268116518Sphk * forwarding or ICMP with the overhead of cloning a route. 269116518Sphk * Of course, we still want to do any cloning requested by 270116518Sphk * the link layer, as this is probably required in all cases 271116518Sphk * for correct operation (as it is for ARP). 272116518Sphk */ 273116518Sphk if (ro->ro_rt == 0) 274116518Sphk rtalloc_ign(ro, RTF_PRCLONING); 275116518Sphk if (ro->ro_rt == 0) { 276116518Sphk ipstat.ips_noroute++; 277116518Sphk error = EHOSTUNREACH; 278116518Sphk goto bad; 279116518Sphk } 280116518Sphk ia = ifatoia(ro->ro_rt->rt_ifa); 281116518Sphk ifp = ro->ro_rt->rt_ifp; 282116518Sphk ro->ro_rt->rt_use++; 283116518Sphk if (ro->ro_rt->rt_flags & RTF_GATEWAY) 284116518Sphk dst = (struct sockaddr_in *)ro->ro_rt->rt_gateway; 285116518Sphk if (ro->ro_rt->rt_flags & RTF_HOST) 286116518Sphk isbroadcast = (ro->ro_rt->rt_flags & RTF_BROADCAST); 287116518Sphk else 288116518Sphk isbroadcast = in_broadcast(dst->sin_addr, ifp); 289116518Sphk } 290116518Sphk if (IN_MULTICAST(ntohl(pkt_dst.s_addr))) { 291116518Sphk struct in_multi *inm; 292116518Sphk 293116518Sphk m->m_flags |= M_MCAST; 294116518Sphk /* 295116518Sphk * IP destination address is multicast. Make sure "dst" 296116518Sphk * still points to the address in "ro". (It may have been 297116518Sphk * changed to point to a gateway address, above.) 298116518Sphk */ 299116518Sphk dst = (struct sockaddr_in *)&ro->ro_dst; 300116518Sphk /* 301116518Sphk * See if the caller provided any multicast options 302116518Sphk */ 303116518Sphk if (imo != NULL) { 304116518Sphk ip->ip_ttl = imo->imo_multicast_ttl; 305116518Sphk if (imo->imo_multicast_vif != -1) 306116518Sphk ip->ip_src.s_addr = 307116518Sphk ip_mcast_src(imo->imo_multicast_vif); 308116518Sphk } else 309116518Sphk ip->ip_ttl = IP_DEFAULT_MULTICAST_TTL; 310116518Sphk /* 311116518Sphk * Confirm that the outgoing interface supports multicast. 312116518Sphk */ 313116518Sphk if ((imo == NULL) || (imo->imo_multicast_vif == -1)) { 314116518Sphk if ((ifp->if_flags & IFF_MULTICAST) == 0) { 315116518Sphk ipstat.ips_noroute++; 316116518Sphk error = ENETUNREACH; 317116518Sphk goto bad; 318116518Sphk } 319116518Sphk } 320116518Sphk /* 321116518Sphk * If source address not specified yet, use address 322116518Sphk * of outgoing interface. 323116518Sphk */ 324116518Sphk if (ip->ip_src.s_addr == INADDR_ANY) { 325116518Sphk /* Interface may have no addresses. */ 326116518Sphk if (ia != NULL) 327116518Sphk ip->ip_src = IA_SIN(ia)->sin_addr; 328116518Sphk } 329116518Sphk 330116518Sphk if (ip_mrouter && (flags & IP_FORWARDING) == 0) { 331116518Sphk /* 332116518Sphk * XXX 333116518Sphk * delayed checksums are not currently 334116518Sphk * compatible with IP multicast routing 335116518Sphk */ 336116518Sphk if (m->m_pkthdr.csum_flags & CSUM_DELAY_DATA) { 337116518Sphk in_delayed_cksum(m); 338116518Sphk m->m_pkthdr.csum_flags &= 339116518Sphk ~CSUM_DELAY_DATA; 340116518Sphk } 341116518Sphk } 342116518Sphk IN_LOOKUP_MULTI(pkt_dst, ifp, inm); 343116518Sphk if (inm != NULL && 344116518Sphk (imo == NULL || imo->imo_multicast_loop)) { 345116518Sphk /* 346116518Sphk * If we belong to the destination multicast group 347116518Sphk * on the outgoing interface, and the caller did not 348116518Sphk * forbid loopback, loop back a copy. 349116518Sphk */ 350116518Sphk ip_mloopback(ifp, m, dst, hlen); 351116518Sphk } 352116518Sphk else { 353116518Sphk /* 354116518Sphk * If we are acting as a multicast router, perform 355116518Sphk * multicast forwarding as if the packet had just 356116518Sphk * arrived on the interface to which we are about 357116518Sphk * to send. The multicast forwarding function 358116518Sphk * recursively calls this function, using the 359116518Sphk * IP_FORWARDING flag to prevent infinite recursion. 360116518Sphk * 361116518Sphk * Multicasts that are looped back by ip_mloopback(), 362116518Sphk * above, will be forwarded by the ip_input() routine, 363116518Sphk * if necessary. 364116518Sphk */ 365116518Sphk if (ip_mrouter && (flags & IP_FORWARDING) == 0) { 366116518Sphk /* 367116518Sphk * Check if rsvp daemon is running. If not, don't 368116518Sphk * set ip_moptions. This ensures that the packet 369116518Sphk * is multicast and not just sent down one link 370116518Sphk * as prescribed by rsvpd. 371116518Sphk */ 372116518Sphk if (!rsvp_on) 373116518Sphk imo = NULL; 374116518Sphk if (ip_mforward(ip, ifp, m, imo) != 0) { 375116518Sphk m_freem(m); 376116518Sphk goto done; 377116518Sphk } 378116518Sphk } 379116518Sphk } 380116518Sphk 381116518Sphk /* 382116518Sphk * Multicasts with a time-to-live of zero may be looped- 383116518Sphk * back, above, but must not be transmitted on a network. 384116518Sphk * Also, multicasts addressed to the loopback interface 385116518Sphk * are not sent -- the above call to ip_mloopback() will 386121475Sphk * loop back a copy if this host actually belongs to the 387116518Sphk * destination group on the loopback interface. 388116518Sphk */ 389116518Sphk if (ip->ip_ttl == 0 || ifp->if_flags & IFF_LOOPBACK) { 390116518Sphk m_freem(m); 391116518Sphk goto done; 392116518Sphk } 393116518Sphk 394116518Sphk goto sendit; 395116518Sphk } 396116518Sphk#ifndef notdef 397116518Sphk /* 398116518Sphk * If source address not specified yet, use address 399116518Sphk * of outgoing interface. 400116518Sphk */ 401116518Sphk if (ip->ip_src.s_addr == INADDR_ANY) { 402116518Sphk /* Interface may have no addresses. */ 403116518Sphk if (ia != NULL) { 404116518Sphk ip->ip_src = IA_SIN(ia)->sin_addr; 405116518Sphk#ifdef IPFIREWALL_FORWARD 406116518Sphk /* Keep note that we did this - if the firewall changes 407116518Sphk * the next-hop, our interface may change, changing the 408116518Sphk * default source IP. It's a shame so much effort happens 409116518Sphk * twice. Oh well. 410116518Sphk */ 411116518Sphk fwd_rewrite_src++; 412121475Sphk#endif /* IPFIREWALL_FORWARD */ 413116518Sphk } 414116518Sphk } 415116518Sphk#endif /* notdef */ 416116518Sphk /* 417116518Sphk * Verify that we have any chance at all of being able to queue 418116518Sphk * the packet or packet fragments 419116518Sphk */ 420116518Sphk if ((ifp->if_snd.ifq_len + ip->ip_len / ifp->if_mtu + 1) >= 421116518Sphk ifp->if_snd.ifq_maxlen) { 422116518Sphk error = ENOBUFS; 423116518Sphk ipstat.ips_odropped++; 424116518Sphk goto bad; 425116518Sphk } 426116518Sphk 427116518Sphk /* 428116518Sphk * Look for broadcast address and 429116518Sphk * verify user is allowed to send 430116518Sphk * such a packet. 431116518Sphk */ 432116518Sphk if (isbroadcast) { 433116518Sphk if ((ifp->if_flags & IFF_BROADCAST) == 0) { 434116518Sphk error = EADDRNOTAVAIL; 435116518Sphk goto bad; 436116518Sphk } 437116518Sphk if ((flags & IP_ALLOWBROADCAST) == 0) { 438116518Sphk error = EACCES; 439116518Sphk goto bad; 440116518Sphk } 441116518Sphk /* don't allow broadcast messages to be fragmented */ 442116518Sphk if ((u_short)ip->ip_len > ifp->if_mtu) { 443116518Sphk error = EMSGSIZE; 444116518Sphk goto bad; 445116518Sphk } 446116518Sphk m->m_flags |= M_BCAST; 447116518Sphk } else { 448116518Sphk m->m_flags &= ~M_BCAST; 449116518Sphk } 450116518Sphk 451116518Sphksendit: 452116518Sphk#ifdef IPSEC 453116518Sphk /* get SP for this packet */ 454116518Sphk if (so == NULL) 455116518Sphk sp = ipsec4_getpolicybyaddr(m, IPSEC_DIR_OUTBOUND, flags, &error); 456116518Sphk else 457125538Sle sp = ipsec4_getpolicybysock(m, IPSEC_DIR_OUTBOUND, so, &error); 458116518Sphk 459116518Sphk if (sp == NULL) { 460116518Sphk ipsecstat.out_inval++; 461116518Sphk goto bad; 462116518Sphk } 463116518Sphk 464116518Sphk error = 0; 465116518Sphk 466116518Sphk /* check policy */ 467116518Sphk switch (sp->policy) { 468116518Sphk case IPSEC_POLICY_DISCARD: 469 /* 470 * This packet is just discarded. 471 */ 472 ipsecstat.out_polvio++; 473 goto bad; 474 475 case IPSEC_POLICY_BYPASS: 476 case IPSEC_POLICY_NONE: 477 /* no need to do IPsec. */ 478 goto skip_ipsec; 479 480 case IPSEC_POLICY_IPSEC: 481 if (sp->req == NULL) { 482 /* acquire a policy */ 483 error = key_spdacquire(sp); 484 goto bad; 485 } 486 break; 487 488 case IPSEC_POLICY_ENTRUST: 489 default: 490 printf("ip_output: Invalid policy found. %d\n", sp->policy); 491 } 492 { 493 struct ipsec_output_state state; 494 bzero(&state, sizeof(state)); 495 state.m = m; 496 if (flags & IP_ROUTETOIF) { 497 state.ro = &iproute; 498 bzero(&iproute, sizeof(iproute)); 499 } else 500 state.ro = ro; 501 state.dst = (struct sockaddr *)dst; 502 503 ip->ip_sum = 0; 504 505 /* 506 * XXX 507 * delayed checksums are not currently compatible with IPsec 508 */ 509 if (m->m_pkthdr.csum_flags & CSUM_DELAY_DATA) { 510 in_delayed_cksum(m); 511 m->m_pkthdr.csum_flags &= ~CSUM_DELAY_DATA; 512 } 513 514 ip->ip_len = htons(ip->ip_len); 515 ip->ip_off = htons(ip->ip_off); 516 517 error = ipsec4_output(&state, sp, flags); 518 519 m = state.m; 520 if (flags & IP_ROUTETOIF) { 521 /* 522 * if we have tunnel mode SA, we may need to ignore 523 * IP_ROUTETOIF. 524 */ 525 if (state.ro != &iproute || state.ro->ro_rt != NULL) { 526 flags &= ~IP_ROUTETOIF; 527 ro = state.ro; 528 } 529 } else 530 ro = state.ro; 531 dst = (struct sockaddr_in *)state.dst; 532 if (error) { 533 /* mbuf is already reclaimed in ipsec4_output. */ 534 m0 = NULL; 535 switch (error) { 536 case EHOSTUNREACH: 537 case ENETUNREACH: 538 case EMSGSIZE: 539 case ENOBUFS: 540 case ENOMEM: 541 break; 542 default: 543 printf("ip4_output (ipsec): error code %d\n", error); 544 /*fall through*/ 545 case ENOENT: 546 /* don't show these error codes to the user */ 547 error = 0; 548 break; 549 } 550 goto bad; 551 } 552 } 553 554 /* be sure to update variables that are affected by ipsec4_output() */ 555 ip = mtod(m, struct ip *); 556#ifdef _IP_VHL 557 hlen = IP_VHL_HL(ip->ip_vhl) << 2; 558#else 559 hlen = ip->ip_hl << 2; 560#endif 561 if (ro->ro_rt == NULL) { 562 if ((flags & IP_ROUTETOIF) == 0) { 563 printf("ip_output: " 564 "can't update route after IPsec processing\n"); 565 error = EHOSTUNREACH; /*XXX*/ 566 goto bad; 567 } 568 } else { 569 ia = ifatoia(ro->ro_rt->rt_ifa); 570 ifp = ro->ro_rt->rt_ifp; 571 } 572 573 /* make it flipped, again. */ 574 ip->ip_len = ntohs(ip->ip_len); 575 ip->ip_off = ntohs(ip->ip_off); 576skip_ipsec: 577#endif /*IPSEC*/ 578 579 /* 580 * IpHack's section. 581 * - Xlate: translate packet's addr/port (NAT). 582 * - Firewall: deny/allow/etc. 583 * - Wrap: fake packet's addr/port <unimpl.> 584 * - Encapsulate: put it in another IP and send out. <unimp.> 585 */ 586#ifdef PFIL_HOOKS 587 /* 588 * Run through list of hooks for output packets. 589 */ 590 m1 = m; 591 pfh = pfil_hook_get(PFIL_OUT, &inetsw[ip_protox[IPPROTO_IP]].pr_pfh); 592 for (; pfh; pfh = TAILQ_NEXT(pfh, pfil_link)) 593 if (pfh->pfil_func) { 594 rv = pfh->pfil_func(ip, hlen, ifp, 1, &m1); 595 if (rv) { 596 error = EHOSTUNREACH; 597 goto done; 598 } 599 m = m1; 600 if (m == NULL) 601 goto done; 602 ip = mtod(m, struct ip *); 603 } 604#endif /* PFIL_HOOKS */ 605 606 /* 607 * Check with the firewall... 608 * but not if we are already being fwd'd from a firewall. 609 */ 610 if (fw_enable && IPFW_LOADED && !ip_fw_fwd_addr) { 611 struct sockaddr_in *old = dst; 612 613 off = ip_fw_chk_ptr(&m, ifp, &divert_cookie, &rule, &dst); 614 /* 615 * On return we must do the following: 616 * m == NULL -> drop the pkt (old interface, deprecated) 617 * (off & IP_FW_PORT_DENY_FLAG) -> drop the pkt (new interface) 618 * 1<=off<= 0xffff -> DIVERT 619 * (off & IP_FW_PORT_DYNT_FLAG) -> send to a DUMMYNET pipe 620 * (off & IP_FW_PORT_TEE_FLAG) -> TEE the packet 621 * dst != old -> IPFIREWALL_FORWARD 622 * off==0, dst==old -> accept 623 * If some of the above modules are not compiled in, then 624 * we should't have to check the corresponding condition 625 * (because the ipfw control socket should not accept 626 * unsupported rules), but better play safe and drop 627 * packets in case of doubt. 628 */ 629 if ( (off & IP_FW_PORT_DENY_FLAG) || m == NULL) { 630 if (m) 631 m_freem(m); 632 error = EACCES; 633 goto done; 634 } 635 ip = mtod(m, struct ip *); 636 if (off == 0 && dst == old) /* common case */ 637 goto pass; 638 if (DUMMYNET_LOADED && (off & IP_FW_PORT_DYNT_FLAG) != 0) { 639 /* 640 * pass the pkt to dummynet. Need to include 641 * pipe number, m, ifp, ro, dst because these are 642 * not recomputed in the next pass. 643 * All other parameters have been already used and 644 * so they are not needed anymore. 645 * XXX note: if the ifp or ro entry are deleted 646 * while a pkt is in dummynet, we are in trouble! 647 */ 648 error = ip_dn_io_ptr(off & 0xffff, DN_TO_IP_OUT, m, 649 ifp, ro, dst, rule, flags); 650 goto done; 651 } 652#ifdef IPDIVERT 653 if (off != 0 && (off & IP_FW_PORT_DYNT_FLAG) == 0) { 654 struct mbuf *clone = NULL; 655 656 /* Clone packet if we're doing a 'tee' */ 657 if ((off & IP_FW_PORT_TEE_FLAG) != 0) 658 clone = m_dup(m, M_DONTWAIT); 659 660 /* 661 * XXX 662 * delayed checksums are not currently compatible 663 * with divert sockets. 664 */ 665 if (m->m_pkthdr.csum_flags & CSUM_DELAY_DATA) { 666 in_delayed_cksum(m); 667 m->m_pkthdr.csum_flags &= ~CSUM_DELAY_DATA; 668 } 669 670 /* Restore packet header fields to original values */ 671 ip->ip_len = htons(ip->ip_len); 672 ip->ip_off = htons(ip->ip_off); 673 674 /* Deliver packet to divert input routine */ 675 ip_divert_cookie = divert_cookie; 676 divert_packet(m, 0, off & 0xffff); 677 678 /* If 'tee', continue with original packet */ 679 if (clone != NULL) { 680 m = clone; 681 ip = mtod(m, struct ip *); 682 goto pass; 683 } 684 goto done; 685 } 686#endif 687 688#ifdef IPFIREWALL_FORWARD 689 /* Here we check dst to make sure it's directly reachable on the 690 * interface we previously thought it was. 691 * If it isn't (which may be likely in some situations) we have 692 * to re-route it (ie, find a route for the next-hop and the 693 * associated interface) and set them here. This is nested 694 * forwarding which in most cases is undesirable, except where 695 * such control is nigh impossible. So we do it here. 696 * And I'm babbling. 697 */ 698 if (off == 0 && old != dst) { 699 struct in_ifaddr *ia; 700 701 /* It's changed... */ 702 /* There must be a better way to do this next line... */ 703 static struct route sro_fwd, *ro_fwd = &sro_fwd; 704#ifdef IPFIREWALL_FORWARD_DEBUG 705 printf("IPFIREWALL_FORWARD: New dst ip: "); 706 print_ip(dst->sin_addr); 707 printf("\n"); 708#endif 709 /* 710 * We need to figure out if we have been forwarded 711 * to a local socket. If so then we should somehow 712 * "loop back" to ip_input, and get directed to the 713 * PCB as if we had received this packet. This is 714 * because it may be dificult to identify the packets 715 * you want to forward until they are being output 716 * and have selected an interface. (e.g. locally 717 * initiated packets) If we used the loopback inteface, 718 * we would not be able to control what happens 719 * as the packet runs through ip_input() as 720 * it is done through a ISR. 721 */ 722 LIST_FOREACH(ia, 723 INADDR_HASH(dst->sin_addr.s_addr), ia_hash) { 724 /* 725 * If the addr to forward to is one 726 * of ours, we pretend to 727 * be the destination for this packet. 728 */ 729 if (IA_SIN(ia)->sin_addr.s_addr == 730 dst->sin_addr.s_addr) 731 break; 732 } 733 if (ia) { 734 /* tell ip_input "dont filter" */ 735 ip_fw_fwd_addr = dst; 736 if (m->m_pkthdr.rcvif == NULL) 737 m->m_pkthdr.rcvif = ifunit("lo0"); 738 if (m->m_pkthdr.csum_flags & CSUM_DELAY_DATA) { 739 m->m_pkthdr.csum_flags |= 740 CSUM_DATA_VALID | CSUM_PSEUDO_HDR; 741 m0->m_pkthdr.csum_data = 0xffff; 742 } 743 m->m_pkthdr.csum_flags |= 744 CSUM_IP_CHECKED | CSUM_IP_VALID; 745 ip->ip_len = htons(ip->ip_len); 746 ip->ip_off = htons(ip->ip_off); 747 ip_input(m); 748 goto done; 749 } 750 /* Some of the logic for this was 751 * nicked from above. 752 * 753 * This rewrites the cached route in a local PCB. 754 * Is this what we want to do? 755 */ 756 bcopy(dst, &ro_fwd->ro_dst, sizeof(*dst)); 757 758 ro_fwd->ro_rt = 0; 759 rtalloc_ign(ro_fwd, RTF_PRCLONING); 760 761 if (ro_fwd->ro_rt == 0) { 762 ipstat.ips_noroute++; 763 error = EHOSTUNREACH; 764 goto bad; 765 } 766 767 ia = ifatoia(ro_fwd->ro_rt->rt_ifa); 768 ifp = ro_fwd->ro_rt->rt_ifp; 769 ro_fwd->ro_rt->rt_use++; 770 if (ro_fwd->ro_rt->rt_flags & RTF_GATEWAY) 771 dst = (struct sockaddr_in *)ro_fwd->ro_rt->rt_gateway; 772 if (ro_fwd->ro_rt->rt_flags & RTF_HOST) 773 isbroadcast = 774 (ro_fwd->ro_rt->rt_flags & RTF_BROADCAST); 775 else 776 isbroadcast = in_broadcast(dst->sin_addr, ifp); 777 if (ro->ro_rt) 778 RTFREE(ro->ro_rt); 779 ro->ro_rt = ro_fwd->ro_rt; 780 dst = (struct sockaddr_in *)&ro_fwd->ro_dst; 781 782 /* 783 * If we added a default src ip earlier, 784 * which would have been gotten from the-then 785 * interface, do it again, from the new one. 786 */ 787 if (fwd_rewrite_src) 788 ip->ip_src = IA_SIN(ia)->sin_addr; 789 goto pass ; 790 } 791#endif /* IPFIREWALL_FORWARD */ 792 /* 793 * if we get here, none of the above matches, and 794 * we have to drop the pkt 795 */ 796 m_freem(m); 797 error = EACCES; /* not sure this is the right error msg */ 798 goto done; 799 } 800 801 ip_fw_fwd_addr = NULL; 802pass: 803 /* 127/8 must not appear on wire - RFC1122. */ 804 if ((ntohl(ip->ip_dst.s_addr) >> IN_CLASSA_NSHIFT) == IN_LOOPBACKNET || 805 (ntohl(ip->ip_src.s_addr) >> IN_CLASSA_NSHIFT) == IN_LOOPBACKNET) { 806 if ((ifp->if_flags & IFF_LOOPBACK) == 0) { 807 ipstat.ips_badaddr++; 808 error = EADDRNOTAVAIL; 809 goto bad; 810 } 811 } 812 813 m->m_pkthdr.csum_flags |= CSUM_IP; 814 sw_csum = m->m_pkthdr.csum_flags & ~ifp->if_hwassist; 815 if (sw_csum & CSUM_DELAY_DATA) { 816 in_delayed_cksum(m); 817 sw_csum &= ~CSUM_DELAY_DATA; 818 } 819 m->m_pkthdr.csum_flags &= ifp->if_hwassist; 820 821 /* 822 * If small enough for interface, or the interface will take 823 * care of the fragmentation for us, can just send directly. 824 */ 825 if ((u_short)ip->ip_len <= ifp->if_mtu || 826 ifp->if_hwassist & CSUM_FRAGMENT) { 827 ip->ip_len = htons(ip->ip_len); 828 ip->ip_off = htons(ip->ip_off); 829 ip->ip_sum = 0; 830 if (sw_csum & CSUM_DELAY_IP) { 831 if (ip->ip_vhl == IP_VHL_BORING) { 832 ip->ip_sum = in_cksum_hdr(ip); 833 } else { 834 ip->ip_sum = in_cksum(m, hlen); 835 } 836 } 837 838 /* Record statistics for this interface address. */ 839 if (!(flags & IP_FORWARDING) && ia) { 840 ia->ia_ifa.if_opackets++; 841 ia->ia_ifa.if_obytes += m->m_pkthdr.len; 842 } 843 844#ifdef IPSEC 845 /* clean ipsec history once it goes out of the node */ 846 ipsec_delaux(m); 847#endif 848 849 error = (*ifp->if_output)(ifp, m, 850 (struct sockaddr *)dst, ro->ro_rt); 851 goto done; 852 } 853 /* 854 * Too large for interface; fragment if possible. 855 * Must be able to put at least 8 bytes per fragment. 856 */ 857 if (ip->ip_off & IP_DF) { 858 error = EMSGSIZE; 859 /* 860 * This case can happen if the user changed the MTU 861 * of an interface after enabling IP on it. Because 862 * most netifs don't keep track of routes pointing to 863 * them, there is no way for one to update all its 864 * routes when the MTU is changed. 865 */ 866 if ((ro->ro_rt->rt_flags & (RTF_UP | RTF_HOST)) 867 && !(ro->ro_rt->rt_rmx.rmx_locks & RTV_MTU) 868 && (ro->ro_rt->rt_rmx.rmx_mtu > ifp->if_mtu)) { 869 ro->ro_rt->rt_rmx.rmx_mtu = ifp->if_mtu; 870 } 871 ipstat.ips_cantfrag++; 872 goto bad; 873 } 874 len = (ifp->if_mtu - hlen) &~ 7; 875 if (len < 8) { 876 error = EMSGSIZE; 877 goto bad; 878 } 879 880 /* 881 * if the interface will not calculate checksums on 882 * fragmented packets, then do it here. 883 */ 884 if (m->m_pkthdr.csum_flags & CSUM_DELAY_DATA && 885 (ifp->if_hwassist & CSUM_IP_FRAGS) == 0) { 886 in_delayed_cksum(m); 887 m->m_pkthdr.csum_flags &= ~CSUM_DELAY_DATA; 888 } 889 890 { 891 int mhlen, firstlen = len; 892 struct mbuf **mnext = &m->m_nextpkt; 893 int nfrags = 1; 894 895 /* 896 * Loop through length of segment after first fragment, 897 * make new header and copy data of each part and link onto chain. 898 */ 899 m0 = m; 900 mhlen = sizeof (struct ip); 901 for (off = hlen + len; off < (u_short)ip->ip_len; off += len) { 902 MGETHDR(m, M_DONTWAIT, MT_HEADER); 903 if (m == 0) { 904 error = ENOBUFS; 905 ipstat.ips_odropped++; 906 goto sendorfree; 907 } 908 m->m_flags |= (m0->m_flags & M_MCAST) | M_FRAG; 909 m->m_data += max_linkhdr; 910 mhip = mtod(m, struct ip *); 911 *mhip = *ip; 912 if (hlen > sizeof (struct ip)) { 913 mhlen = ip_optcopy(ip, mhip) + sizeof (struct ip); 914 mhip->ip_vhl = IP_MAKE_VHL(IPVERSION, mhlen >> 2); 915 } 916 m->m_len = mhlen; 917 mhip->ip_off = ((off - hlen) >> 3) + ip->ip_off; 918 if (off + len >= (u_short)ip->ip_len) 919 len = (u_short)ip->ip_len - off; 920 else 921 mhip->ip_off |= IP_MF; 922 mhip->ip_len = htons((u_short)(len + mhlen)); 923 m->m_next = m_copy(m0, off, len); 924 if (m->m_next == 0) { 925 (void) m_free(m); 926 error = ENOBUFS; /* ??? */ 927 ipstat.ips_odropped++; 928 goto sendorfree; 929 } 930 m->m_pkthdr.len = mhlen + len; 931 m->m_pkthdr.rcvif = (struct ifnet *)0; 932 m->m_pkthdr.csum_flags = m0->m_pkthdr.csum_flags; 933 mhip->ip_off = htons(mhip->ip_off); 934 mhip->ip_sum = 0; 935 if (sw_csum & CSUM_DELAY_IP) { 936 if (mhip->ip_vhl == IP_VHL_BORING) { 937 mhip->ip_sum = in_cksum_hdr(mhip); 938 } else { 939 mhip->ip_sum = in_cksum(m, mhlen); 940 } 941 } 942 *mnext = m; 943 mnext = &m->m_nextpkt; 944 nfrags++; 945 } 946 ipstat.ips_ofragments += nfrags; 947 948 /* set first/last markers for fragment chain */ 949 m->m_flags |= M_LASTFRAG; 950 m0->m_flags |= M_FIRSTFRAG | M_FRAG; 951 m0->m_pkthdr.csum_data = nfrags; 952 953 /* 954 * Update first fragment by trimming what's been copied out 955 * and updating header, then send each fragment (in order). 956 */ 957 m = m0; 958 m_adj(m, hlen + firstlen - (u_short)ip->ip_len); 959 m->m_pkthdr.len = hlen + firstlen; 960 ip->ip_len = htons((u_short)m->m_pkthdr.len); 961 ip->ip_off |= IP_MF; 962 ip->ip_off = htons(ip->ip_off); 963 ip->ip_sum = 0; 964 if (sw_csum & CSUM_DELAY_IP) { 965 if (ip->ip_vhl == IP_VHL_BORING) { 966 ip->ip_sum = in_cksum_hdr(ip); 967 } else { 968 ip->ip_sum = in_cksum(m, hlen); 969 } 970 } 971sendorfree: 972 for (m = m0; m; m = m0) { 973 m0 = m->m_nextpkt; 974 m->m_nextpkt = 0; 975#ifdef IPSEC 976 /* clean ipsec history once it goes out of the node */ 977 ipsec_delaux(m); 978#endif 979 if (error == 0) { 980 /* Record statistics for this interface address. */ 981 if (ia != NULL) { 982 ia->ia_ifa.if_opackets++; 983 ia->ia_ifa.if_obytes += m->m_pkthdr.len; 984 } 985 986 error = (*ifp->if_output)(ifp, m, 987 (struct sockaddr *)dst, ro->ro_rt); 988 } else 989 m_freem(m); 990 } 991 992 if (error == 0) 993 ipstat.ips_fragmented++; 994 } 995done: 996#ifdef IPSEC 997 if (ro == &iproute && ro->ro_rt) { 998 RTFREE(ro->ro_rt); 999 ro->ro_rt = NULL; 1000 } 1001 if (sp != NULL) { 1002 KEYDEBUG(KEYDEBUG_IPSEC_STAMP, 1003 printf("DP ip_output call free SP:%p\n", sp)); 1004 key_freesp(sp); 1005 } 1006#endif /* IPSEC */ 1007 return (error); 1008bad: 1009 m_freem(m); 1010 goto done; 1011} 1012 1013void 1014in_delayed_cksum(struct mbuf *m) 1015{ 1016 struct ip *ip; 1017 u_short csum, offset; 1018 1019 ip = mtod(m, struct ip *); 1020 offset = IP_VHL_HL(ip->ip_vhl) << 2 ; 1021 csum = in_cksum_skip(m, ip->ip_len, offset); 1022 if (m->m_pkthdr.csum_flags & CSUM_UDP && csum == 0) 1023 csum = 0xffff; 1024 offset += m->m_pkthdr.csum_data; /* checksum offset */ 1025 1026 if (offset + sizeof(u_short) > m->m_len) { 1027 printf("delayed m_pullup, m->len: %d off: %d p: %d\n", 1028 m->m_len, offset, ip->ip_p); 1029 /* 1030 * XXX 1031 * this shouldn't happen, but if it does, the 1032 * correct behavior may be to insert the checksum 1033 * in the existing chain instead of rearranging it. 1034 */ 1035 m = m_pullup(m, offset + sizeof(u_short)); 1036 } 1037 *(u_short *)(m->m_data + offset) = csum; 1038} 1039 1040/* 1041 * Insert IP options into preformed packet. 1042 * Adjust IP destination as required for IP source routing, 1043 * as indicated by a non-zero in_addr at the start of the options. 1044 * 1045 * XXX This routine assumes that the packet has no options in place. 1046 */ 1047static struct mbuf * 1048ip_insertoptions(m, opt, phlen) 1049 register struct mbuf *m; 1050 struct mbuf *opt; 1051 int *phlen; 1052{ 1053 register struct ipoption *p = mtod(opt, struct ipoption *); 1054 struct mbuf *n; 1055 register struct ip *ip = mtod(m, struct ip *); 1056 unsigned optlen; 1057 1058 optlen = opt->m_len - sizeof(p->ipopt_dst); 1059 if (optlen + (u_short)ip->ip_len > IP_MAXPACKET) 1060 return (m); /* XXX should fail */ 1061 if (p->ipopt_dst.s_addr) 1062 ip->ip_dst = p->ipopt_dst; 1063 if (m->m_flags & M_EXT || m->m_data - optlen < m->m_pktdat) { 1064 MGETHDR(n, M_DONTWAIT, MT_HEADER); 1065 if (n == 0) 1066 return (m); 1067 n->m_pkthdr.rcvif = (struct ifnet *)0; 1068 n->m_pkthdr.len = m->m_pkthdr.len + optlen; 1069 m->m_len -= sizeof(struct ip); 1070 m->m_data += sizeof(struct ip); 1071 n->m_next = m; 1072 m = n; 1073 m->m_len = optlen + sizeof(struct ip); 1074 m->m_data += max_linkhdr; 1075 (void)memcpy(mtod(m, void *), ip, sizeof(struct ip)); 1076 } else { 1077 m->m_data -= optlen; 1078 m->m_len += optlen; 1079 m->m_pkthdr.len += optlen; 1080 ovbcopy((caddr_t)ip, mtod(m, caddr_t), sizeof(struct ip)); 1081 } 1082 ip = mtod(m, struct ip *); 1083 bcopy(p->ipopt_list, ip + 1, optlen); 1084 *phlen = sizeof(struct ip) + optlen; 1085 ip->ip_vhl = IP_MAKE_VHL(IPVERSION, *phlen >> 2); 1086 ip->ip_len += optlen; 1087 return (m); 1088} 1089 1090/* 1091 * Copy options from ip to jp, 1092 * omitting those not copied during fragmentation. 1093 */ 1094int 1095ip_optcopy(ip, jp) 1096 struct ip *ip, *jp; 1097{ 1098 register u_char *cp, *dp; 1099 int opt, optlen, cnt; 1100 1101 cp = (u_char *)(ip + 1); 1102 dp = (u_char *)(jp + 1); 1103 cnt = (IP_VHL_HL(ip->ip_vhl) << 2) - sizeof (struct ip); 1104 for (; cnt > 0; cnt -= optlen, cp += optlen) { 1105 opt = cp[0]; 1106 if (opt == IPOPT_EOL) 1107 break; 1108 if (opt == IPOPT_NOP) { 1109 /* Preserve for IP mcast tunnel's LSRR alignment. */ 1110 *dp++ = IPOPT_NOP; 1111 optlen = 1; 1112 continue; 1113 } 1114#ifdef INVARIANTS 1115 if (cnt < IPOPT_OLEN + sizeof(*cp)) 1116 panic("malformed IPv4 option passed to ip_optcopy"); 1117#endif 1118 optlen = cp[IPOPT_OLEN]; 1119#ifdef INVARIANTS 1120 if (optlen < IPOPT_OLEN + sizeof(*cp) || optlen > cnt) 1121 panic("malformed IPv4 option passed to ip_optcopy"); 1122#endif 1123 /* bogus lengths should have been caught by ip_dooptions */ 1124 if (optlen > cnt) 1125 optlen = cnt; 1126 if (IPOPT_COPIED(opt)) { 1127 bcopy(cp, dp, optlen); 1128 dp += optlen; 1129 } 1130 } 1131 for (optlen = dp - (u_char *)(jp+1); optlen & 0x3; optlen++) 1132 *dp++ = IPOPT_EOL; 1133 return (optlen); 1134} 1135 1136/* 1137 * IP socket option processing. 1138 */ 1139int 1140ip_ctloutput(so, sopt) 1141 struct socket *so; 1142 struct sockopt *sopt; 1143{ 1144 struct inpcb *inp = sotoinpcb(so); 1145 int error, optval; 1146 1147 error = optval = 0; 1148 if (sopt->sopt_level != IPPROTO_IP) { 1149 return (EINVAL); 1150 } 1151 1152 switch (sopt->sopt_dir) { 1153 case SOPT_SET: 1154 switch (sopt->sopt_name) { 1155 case IP_OPTIONS: 1156#ifdef notyet 1157 case IP_RETOPTS: 1158#endif 1159 { 1160 struct mbuf *m; 1161 if (sopt->sopt_valsize > MLEN) { 1162 error = EMSGSIZE; 1163 break; 1164 } 1165 MGET(m, sopt->sopt_td ? M_TRYWAIT : M_DONTWAIT, MT_HEADER); 1166 if (m == 0) { 1167 error = ENOBUFS; 1168 break; 1169 } 1170 m->m_len = sopt->sopt_valsize; 1171 error = sooptcopyin(sopt, mtod(m, char *), m->m_len, 1172 m->m_len); 1173 1174 return (ip_pcbopts(sopt->sopt_name, &inp->inp_options, 1175 m)); 1176 } 1177 1178 case IP_TOS: 1179 case IP_TTL: 1180 case IP_RECVOPTS: 1181 case IP_RECVRETOPTS: 1182 case IP_RECVDSTADDR: 1183 case IP_RECVIF: 1184 case IP_FAITH: 1185 error = sooptcopyin(sopt, &optval, sizeof optval, 1186 sizeof optval); 1187 if (error) 1188 break; 1189 1190 switch (sopt->sopt_name) { 1191 case IP_TOS: 1192 inp->inp_ip_tos = optval; 1193 break; 1194 1195 case IP_TTL: 1196 inp->inp_ip_ttl = optval; 1197 break; 1198#define OPTSET(bit) \ 1199 if (optval) \ 1200 inp->inp_flags |= bit; \ 1201 else \ 1202 inp->inp_flags &= ~bit; 1203 1204 case IP_RECVOPTS: 1205 OPTSET(INP_RECVOPTS); 1206 break; 1207 1208 case IP_RECVRETOPTS: 1209 OPTSET(INP_RECVRETOPTS); 1210 break; 1211 1212 case IP_RECVDSTADDR: 1213 OPTSET(INP_RECVDSTADDR); 1214 break; 1215 1216 case IP_RECVIF: 1217 OPTSET(INP_RECVIF); 1218 break; 1219 1220 case IP_FAITH: 1221 OPTSET(INP_FAITH); 1222 break; 1223 } 1224 break; 1225#undef OPTSET 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_setmoptions(sopt, &inp->inp_moptions); 1234 break; 1235 1236 case IP_PORTRANGE: 1237 error = sooptcopyin(sopt, &optval, sizeof optval, 1238 sizeof optval); 1239 if (error) 1240 break; 1241 1242 switch (optval) { 1243 case IP_PORTRANGE_DEFAULT: 1244 inp->inp_flags &= ~(INP_LOWPORT); 1245 inp->inp_flags &= ~(INP_HIGHPORT); 1246 break; 1247 1248 case IP_PORTRANGE_HIGH: 1249 inp->inp_flags &= ~(INP_LOWPORT); 1250 inp->inp_flags |= INP_HIGHPORT; 1251 break; 1252 1253 case IP_PORTRANGE_LOW: 1254 inp->inp_flags &= ~(INP_HIGHPORT); 1255 inp->inp_flags |= INP_LOWPORT; 1256 break; 1257 1258 default: 1259 error = EINVAL; 1260 break; 1261 } 1262 break; 1263 1264#ifdef IPSEC 1265 case IP_IPSEC_POLICY: 1266 { 1267 caddr_t req; 1268 size_t len = 0; 1269 int priv; 1270 struct mbuf *m; 1271 int optname; 1272 1273 if ((error = soopt_getm(sopt, &m)) != 0) /* XXX */ 1274 break; 1275 if ((error = soopt_mcopyin(sopt, m)) != 0) /* XXX */ 1276 break; 1277 priv = (sopt->sopt_td != NULL && 1278 suser(sopt->sopt_td) != 0) ? 0 : 1; 1279 req = mtod(m, caddr_t); 1280 len = m->m_len; 1281 optname = sopt->sopt_name; 1282 error = ipsec4_set_policy(inp, optname, req, len, priv); 1283 m_freem(m); 1284 break; 1285 } 1286#endif /*IPSEC*/ 1287 1288 default: 1289 error = ENOPROTOOPT; 1290 break; 1291 } 1292 break; 1293 1294 case SOPT_GET: 1295 switch (sopt->sopt_name) { 1296 case IP_OPTIONS: 1297 case IP_RETOPTS: 1298 if (inp->inp_options) 1299 error = sooptcopyout(sopt, 1300 mtod(inp->inp_options, 1301 char *), 1302 inp->inp_options->m_len); 1303 else 1304 sopt->sopt_valsize = 0; 1305 break; 1306 1307 case IP_TOS: 1308 case IP_TTL: 1309 case IP_RECVOPTS: 1310 case IP_RECVRETOPTS: 1311 case IP_RECVDSTADDR: 1312 case IP_RECVIF: 1313 case IP_PORTRANGE: 1314 case IP_FAITH: 1315 switch (sopt->sopt_name) { 1316 1317 case IP_TOS: 1318 optval = inp->inp_ip_tos; 1319 break; 1320 1321 case IP_TTL: 1322 optval = inp->inp_ip_ttl; 1323 break; 1324 1325#define OPTBIT(bit) (inp->inp_flags & bit ? 1 : 0) 1326 1327 case IP_RECVOPTS: 1328 optval = OPTBIT(INP_RECVOPTS); 1329 break; 1330 1331 case IP_RECVRETOPTS: 1332 optval = OPTBIT(INP_RECVRETOPTS); 1333 break; 1334 1335 case IP_RECVDSTADDR: 1336 optval = OPTBIT(INP_RECVDSTADDR); 1337 break; 1338 1339 case IP_RECVIF: 1340 optval = OPTBIT(INP_RECVIF); 1341 break; 1342 1343 case IP_PORTRANGE: 1344 if (inp->inp_flags & INP_HIGHPORT) 1345 optval = IP_PORTRANGE_HIGH; 1346 else if (inp->inp_flags & INP_LOWPORT) 1347 optval = IP_PORTRANGE_LOW; 1348 else 1349 optval = 0; 1350 break; 1351 1352 case IP_FAITH: 1353 optval = OPTBIT(INP_FAITH); 1354 break; 1355 } 1356 error = sooptcopyout(sopt, &optval, sizeof optval); 1357 break; 1358 1359 case IP_MULTICAST_IF: 1360 case IP_MULTICAST_VIF: 1361 case IP_MULTICAST_TTL: 1362 case IP_MULTICAST_LOOP: 1363 case IP_ADD_MEMBERSHIP: 1364 case IP_DROP_MEMBERSHIP: 1365 error = ip_getmoptions(sopt, inp->inp_moptions); 1366 break; 1367 1368#ifdef IPSEC 1369 case IP_IPSEC_POLICY: 1370 { 1371 struct mbuf *m = NULL; 1372 caddr_t req = NULL; 1373 size_t len = 0; 1374 1375 if (m != 0) { 1376 req = mtod(m, caddr_t); 1377 len = m->m_len; 1378 } 1379 error = ipsec4_get_policy(sotoinpcb(so), req, len, &m); 1380 if (error == 0) 1381 error = soopt_mcopyout(sopt, m); /* XXX */ 1382 if (error == 0) 1383 m_freem(m); 1384 break; 1385 } 1386#endif /*IPSEC*/ 1387 1388 default: 1389 error = ENOPROTOOPT; 1390 break; 1391 } 1392 break; 1393 } 1394 return (error); 1395} 1396 1397/* 1398 * Set up IP options in pcb for insertion in output packets. 1399 * Store in mbuf with pointer in pcbopt, adding pseudo-option 1400 * with destination address if source routed. 1401 */ 1402static int 1403ip_pcbopts(optname, pcbopt, m) 1404 int optname; 1405 struct mbuf **pcbopt; 1406 register struct mbuf *m; 1407{ 1408 register int cnt, optlen; 1409 register u_char *cp; 1410 u_char opt; 1411 1412 /* turn off any old options */ 1413 if (*pcbopt) 1414 (void)m_free(*pcbopt); 1415 *pcbopt = 0; 1416 if (m == (struct mbuf *)0 || m->m_len == 0) { 1417 /* 1418 * Only turning off any previous options. 1419 */ 1420 if (m) 1421 (void)m_free(m); 1422 return (0); 1423 } 1424 1425 if (m->m_len % sizeof(int32_t)) 1426 goto bad; 1427 /* 1428 * IP first-hop destination address will be stored before 1429 * actual options; move other options back 1430 * and clear it when none present. 1431 */ 1432 if (m->m_data + m->m_len + sizeof(struct in_addr) >= &m->m_dat[MLEN]) 1433 goto bad; 1434 cnt = m->m_len; 1435 m->m_len += sizeof(struct in_addr); 1436 cp = mtod(m, u_char *) + sizeof(struct in_addr); 1437 ovbcopy(mtod(m, caddr_t), (caddr_t)cp, (unsigned)cnt); 1438 bzero(mtod(m, caddr_t), sizeof(struct in_addr)); 1439 1440 for (; cnt > 0; cnt -= optlen, cp += optlen) { 1441 opt = cp[IPOPT_OPTVAL]; 1442 if (opt == IPOPT_EOL) 1443 break; 1444 if (opt == IPOPT_NOP) 1445 optlen = 1; 1446 else { 1447 if (cnt < IPOPT_OLEN + sizeof(*cp)) 1448 goto bad; 1449 optlen = cp[IPOPT_OLEN]; 1450 if (optlen < IPOPT_OLEN + sizeof(*cp) || optlen > cnt) 1451 goto bad; 1452 } 1453 switch (opt) { 1454 1455 default: 1456 break; 1457 1458 case IPOPT_LSRR: 1459 case IPOPT_SSRR: 1460 /* 1461 * user process specifies route as: 1462 * ->A->B->C->D 1463 * D must be our final destination (but we can't 1464 * check that since we may not have connected yet). 1465 * A is first hop destination, which doesn't appear in 1466 * actual IP option, but is stored before the options. 1467 */ 1468 if (optlen < IPOPT_MINOFF - 1 + sizeof(struct in_addr)) 1469 goto bad; 1470 m->m_len -= sizeof(struct in_addr); 1471 cnt -= sizeof(struct in_addr); 1472 optlen -= sizeof(struct in_addr); 1473 cp[IPOPT_OLEN] = optlen; 1474 /* 1475 * Move first hop before start of options. 1476 */ 1477 bcopy((caddr_t)&cp[IPOPT_OFFSET+1], mtod(m, caddr_t), 1478 sizeof(struct in_addr)); 1479 /* 1480 * Then copy rest of options back 1481 * to close up the deleted entry. 1482 */ 1483 ovbcopy((caddr_t)(&cp[IPOPT_OFFSET+1] + 1484 sizeof(struct in_addr)), 1485 (caddr_t)&cp[IPOPT_OFFSET+1], 1486 (unsigned)cnt + sizeof(struct in_addr)); 1487 break; 1488 } 1489 } 1490 if (m->m_len > MAX_IPOPTLEN + sizeof(struct in_addr)) 1491 goto bad; 1492 *pcbopt = m; 1493 return (0); 1494 1495bad: 1496 (void)m_free(m); 1497 return (EINVAL); 1498} 1499 1500/* 1501 * XXX 1502 * The whole multicast option thing needs to be re-thought. 1503 * Several of these options are equally applicable to non-multicast 1504 * transmission, and one (IP_MULTICAST_TTL) totally duplicates a 1505 * standard option (IP_TTL). 1506 */ 1507 1508/* 1509 * following RFC1724 section 3.3, 0.0.0.0/8 is interpreted as interface index. 1510 */ 1511static struct ifnet * 1512ip_multicast_if(a, ifindexp) 1513 struct in_addr *a; 1514 int *ifindexp; 1515{ 1516 int ifindex; 1517 struct ifnet *ifp; 1518 1519 if (ifindexp) 1520 *ifindexp = 0; 1521 if (ntohl(a->s_addr) >> 24 == 0) { 1522 ifindex = ntohl(a->s_addr) & 0xffffff; 1523 if (ifindex < 0 || if_index < ifindex) 1524 return NULL; 1525 ifp = ifnet_byindex(ifindex); 1526 if (ifindexp) 1527 *ifindexp = ifindex; 1528 } else { 1529 INADDR_TO_IFP(*a, ifp); 1530 } 1531 return ifp; 1532} 1533 1534/* 1535 * Set the IP multicast options in response to user setsockopt(). 1536 */ 1537static int 1538ip_setmoptions(sopt, imop) 1539 struct sockopt *sopt; 1540 struct ip_moptions **imop; 1541{ 1542 int error = 0; 1543 int i; 1544 struct in_addr addr; 1545 struct ip_mreq mreq; 1546 struct ifnet *ifp; 1547 struct ip_moptions *imo = *imop; 1548 struct route ro; 1549 struct sockaddr_in *dst; 1550 int ifindex; 1551 int s; 1552 1553 if (imo == NULL) { 1554 /* 1555 * No multicast option buffer attached to the pcb; 1556 * allocate one and initialize to default values. 1557 */ 1558 imo = (struct ip_moptions*)malloc(sizeof(*imo), M_IPMOPTS, 1559 M_WAITOK); 1560 1561 if (imo == NULL) 1562 return (ENOBUFS); 1563 *imop = imo; 1564 imo->imo_multicast_ifp = NULL; 1565 imo->imo_multicast_addr.s_addr = INADDR_ANY; 1566 imo->imo_multicast_vif = -1; 1567 imo->imo_multicast_ttl = IP_DEFAULT_MULTICAST_TTL; 1568 imo->imo_multicast_loop = IP_DEFAULT_MULTICAST_LOOP; 1569 imo->imo_num_memberships = 0; 1570 } 1571 1572 switch (sopt->sopt_name) { 1573 /* store an index number for the vif you wanna use in the send */ 1574 case IP_MULTICAST_VIF: 1575 if (legal_vif_num == 0) { 1576 error = EOPNOTSUPP; 1577 break; 1578 } 1579 error = sooptcopyin(sopt, &i, sizeof i, sizeof i); 1580 if (error) 1581 break; 1582 if (!legal_vif_num(i) && (i != -1)) { 1583 error = EINVAL; 1584 break; 1585 } 1586 imo->imo_multicast_vif = i; 1587 break; 1588 1589 case IP_MULTICAST_IF: 1590 /* 1591 * Select the interface for outgoing multicast packets. 1592 */ 1593 error = sooptcopyin(sopt, &addr, sizeof addr, sizeof addr); 1594 if (error) 1595 break; 1596 /* 1597 * INADDR_ANY is used to remove a previous selection. 1598 * When no interface is selected, a default one is 1599 * chosen every time a multicast packet is sent. 1600 */ 1601 if (addr.s_addr == INADDR_ANY) { 1602 imo->imo_multicast_ifp = NULL; 1603 break; 1604 } 1605 /* 1606 * The selected interface is identified by its local 1607 * IP address. Find the interface and confirm that 1608 * it supports multicasting. 1609 */ 1610 s = splimp(); 1611 ifp = ip_multicast_if(&addr, &ifindex); 1612 if (ifp == NULL || (ifp->if_flags & IFF_MULTICAST) == 0) { 1613 splx(s); 1614 error = EADDRNOTAVAIL; 1615 break; 1616 } 1617 imo->imo_multicast_ifp = ifp; 1618 if (ifindex) 1619 imo->imo_multicast_addr = addr; 1620 else 1621 imo->imo_multicast_addr.s_addr = INADDR_ANY; 1622 splx(s); 1623 break; 1624 1625 case IP_MULTICAST_TTL: 1626 /* 1627 * Set the IP time-to-live for outgoing multicast packets. 1628 * The original multicast API required a char argument, 1629 * which is inconsistent with the rest of the socket API. 1630 * We allow either a char or an int. 1631 */ 1632 if (sopt->sopt_valsize == 1) { 1633 u_char ttl; 1634 error = sooptcopyin(sopt, &ttl, 1, 1); 1635 if (error) 1636 break; 1637 imo->imo_multicast_ttl = ttl; 1638 } else { 1639 u_int ttl; 1640 error = sooptcopyin(sopt, &ttl, sizeof ttl, 1641 sizeof ttl); 1642 if (error) 1643 break; 1644 if (ttl > 255) 1645 error = EINVAL; 1646 else 1647 imo->imo_multicast_ttl = ttl; 1648 } 1649 break; 1650 1651 case IP_MULTICAST_LOOP: 1652 /* 1653 * Set the loopback flag for outgoing multicast packets. 1654 * Must be zero or one. The original multicast API required a 1655 * char argument, which is inconsistent with the rest 1656 * of the socket API. We allow either a char or an int. 1657 */ 1658 if (sopt->sopt_valsize == 1) { 1659 u_char loop; 1660 error = sooptcopyin(sopt, &loop, 1, 1); 1661 if (error) 1662 break; 1663 imo->imo_multicast_loop = !!loop; 1664 } else { 1665 u_int loop; 1666 error = sooptcopyin(sopt, &loop, sizeof loop, 1667 sizeof loop); 1668 if (error) 1669 break; 1670 imo->imo_multicast_loop = !!loop; 1671 } 1672 break; 1673 1674 case IP_ADD_MEMBERSHIP: 1675 /* 1676 * Add a multicast group membership. 1677 * Group must be a valid IP multicast address. 1678 */ 1679 error = sooptcopyin(sopt, &mreq, sizeof mreq, sizeof mreq); 1680 if (error) 1681 break; 1682 1683 if (!IN_MULTICAST(ntohl(mreq.imr_multiaddr.s_addr))) { 1684 error = EINVAL; 1685 break; 1686 } 1687 s = splimp(); 1688 /* 1689 * If no interface address was provided, use the interface of 1690 * the route to the given multicast address. 1691 */ 1692 if (mreq.imr_interface.s_addr == INADDR_ANY) { 1693 bzero((caddr_t)&ro, sizeof(ro)); 1694 dst = (struct sockaddr_in *)&ro.ro_dst; 1695 dst->sin_len = sizeof(*dst); 1696 dst->sin_family = AF_INET; 1697 dst->sin_addr = mreq.imr_multiaddr; 1698 rtalloc(&ro); 1699 if (ro.ro_rt == NULL) { 1700 error = EADDRNOTAVAIL; 1701 splx(s); 1702 break; 1703 } 1704 ifp = ro.ro_rt->rt_ifp; 1705 rtfree(ro.ro_rt); 1706 } 1707 else { 1708 ifp = ip_multicast_if(&mreq.imr_interface, NULL); 1709 } 1710 1711 /* 1712 * See if we found an interface, and confirm that it 1713 * supports multicast. 1714 */ 1715 if (ifp == NULL || (ifp->if_flags & IFF_MULTICAST) == 0) { 1716 error = EADDRNOTAVAIL; 1717 splx(s); 1718 break; 1719 } 1720 /* 1721 * See if the membership already exists or if all the 1722 * membership slots are full. 1723 */ 1724 for (i = 0; i < imo->imo_num_memberships; ++i) { 1725 if (imo->imo_membership[i]->inm_ifp == ifp && 1726 imo->imo_membership[i]->inm_addr.s_addr 1727 == mreq.imr_multiaddr.s_addr) 1728 break; 1729 } 1730 if (i < imo->imo_num_memberships) { 1731 error = EADDRINUSE; 1732 splx(s); 1733 break; 1734 } 1735 if (i == IP_MAX_MEMBERSHIPS) { 1736 error = ETOOMANYREFS; 1737 splx(s); 1738 break; 1739 } 1740 /* 1741 * Everything looks good; add a new record to the multicast 1742 * address list for the given interface. 1743 */ 1744 if ((imo->imo_membership[i] = 1745 in_addmulti(&mreq.imr_multiaddr, ifp)) == NULL) { 1746 error = ENOBUFS; 1747 splx(s); 1748 break; 1749 } 1750 ++imo->imo_num_memberships; 1751 splx(s); 1752 break; 1753 1754 case IP_DROP_MEMBERSHIP: 1755 /* 1756 * Drop a multicast group membership. 1757 * Group must be a valid IP multicast address. 1758 */ 1759 error = sooptcopyin(sopt, &mreq, sizeof mreq, sizeof mreq); 1760 if (error) 1761 break; 1762 1763 if (!IN_MULTICAST(ntohl(mreq.imr_multiaddr.s_addr))) { 1764 error = EINVAL; 1765 break; 1766 } 1767 1768 s = splimp(); 1769 /* 1770 * If an interface address was specified, get a pointer 1771 * to its ifnet structure. 1772 */ 1773 if (mreq.imr_interface.s_addr == INADDR_ANY) 1774 ifp = NULL; 1775 else { 1776 ifp = ip_multicast_if(&mreq.imr_interface, NULL); 1777 if (ifp == NULL) { 1778 error = EADDRNOTAVAIL; 1779 splx(s); 1780 break; 1781 } 1782 } 1783 /* 1784 * Find the membership in the membership array. 1785 */ 1786 for (i = 0; i < imo->imo_num_memberships; ++i) { 1787 if ((ifp == NULL || 1788 imo->imo_membership[i]->inm_ifp == ifp) && 1789 imo->imo_membership[i]->inm_addr.s_addr == 1790 mreq.imr_multiaddr.s_addr) 1791 break; 1792 } 1793 if (i == imo->imo_num_memberships) { 1794 error = EADDRNOTAVAIL; 1795 splx(s); 1796 break; 1797 } 1798 /* 1799 * Give up the multicast address record to which the 1800 * membership points. 1801 */ 1802 in_delmulti(imo->imo_membership[i]); 1803 /* 1804 * Remove the gap in the membership array. 1805 */ 1806 for (++i; i < imo->imo_num_memberships; ++i) 1807 imo->imo_membership[i-1] = imo->imo_membership[i]; 1808 --imo->imo_num_memberships; 1809 splx(s); 1810 break; 1811 1812 default: 1813 error = EOPNOTSUPP; 1814 break; 1815 } 1816 1817 /* 1818 * If all options have default values, no need to keep the mbuf. 1819 */ 1820 if (imo->imo_multicast_ifp == NULL && 1821 imo->imo_multicast_vif == -1 && 1822 imo->imo_multicast_ttl == IP_DEFAULT_MULTICAST_TTL && 1823 imo->imo_multicast_loop == IP_DEFAULT_MULTICAST_LOOP && 1824 imo->imo_num_memberships == 0) { 1825 free(*imop, M_IPMOPTS); 1826 *imop = NULL; 1827 } 1828 1829 return (error); 1830} 1831 1832/* 1833 * Return the IP multicast options in response to user getsockopt(). 1834 */ 1835static int 1836ip_getmoptions(sopt, imo) 1837 struct sockopt *sopt; 1838 register struct ip_moptions *imo; 1839{ 1840 struct in_addr addr; 1841 struct in_ifaddr *ia; 1842 int error, optval; 1843 u_char coptval; 1844 1845 error = 0; 1846 switch (sopt->sopt_name) { 1847 case IP_MULTICAST_VIF: 1848 if (imo != NULL) 1849 optval = imo->imo_multicast_vif; 1850 else 1851 optval = -1; 1852 error = sooptcopyout(sopt, &optval, sizeof optval); 1853 break; 1854 1855 case IP_MULTICAST_IF: 1856 if (imo == NULL || imo->imo_multicast_ifp == NULL) 1857 addr.s_addr = INADDR_ANY; 1858 else if (imo->imo_multicast_addr.s_addr) { 1859 /* return the value user has set */ 1860 addr = imo->imo_multicast_addr; 1861 } else { 1862 IFP_TO_IA(imo->imo_multicast_ifp, ia); 1863 addr.s_addr = (ia == NULL) ? INADDR_ANY 1864 : IA_SIN(ia)->sin_addr.s_addr; 1865 } 1866 error = sooptcopyout(sopt, &addr, sizeof addr); 1867 break; 1868 1869 case IP_MULTICAST_TTL: 1870 if (imo == 0) 1871 optval = coptval = IP_DEFAULT_MULTICAST_TTL; 1872 else 1873 optval = coptval = imo->imo_multicast_ttl; 1874 if (sopt->sopt_valsize == 1) 1875 error = sooptcopyout(sopt, &coptval, 1); 1876 else 1877 error = sooptcopyout(sopt, &optval, sizeof optval); 1878 break; 1879 1880 case IP_MULTICAST_LOOP: 1881 if (imo == 0) 1882 optval = coptval = IP_DEFAULT_MULTICAST_LOOP; 1883 else 1884 optval = coptval = imo->imo_multicast_loop; 1885 if (sopt->sopt_valsize == 1) 1886 error = sooptcopyout(sopt, &coptval, 1); 1887 else 1888 error = sooptcopyout(sopt, &optval, sizeof optval); 1889 break; 1890 1891 default: 1892 error = ENOPROTOOPT; 1893 break; 1894 } 1895 return (error); 1896} 1897 1898/* 1899 * Discard the IP multicast options. 1900 */ 1901void 1902ip_freemoptions(imo) 1903 register struct ip_moptions *imo; 1904{ 1905 register int i; 1906 1907 if (imo != NULL) { 1908 for (i = 0; i < imo->imo_num_memberships; ++i) 1909 in_delmulti(imo->imo_membership[i]); 1910 free(imo, M_IPMOPTS); 1911 } 1912} 1913 1914/* 1915 * Routine called from ip_output() to loop back a copy of an IP multicast 1916 * packet to the input queue of a specified interface. Note that this 1917 * calls the output routine of the loopback "driver", but with an interface 1918 * pointer that might NOT be a loopback interface -- evil, but easier than 1919 * replicating that code here. 1920 */ 1921static void 1922ip_mloopback(ifp, m, dst, hlen) 1923 struct ifnet *ifp; 1924 register struct mbuf *m; 1925 register struct sockaddr_in *dst; 1926 int hlen; 1927{ 1928 register struct ip *ip; 1929 struct mbuf *copym; 1930 1931 copym = m_copy(m, 0, M_COPYALL); 1932 if (copym != NULL && (copym->m_flags & M_EXT || copym->m_len < hlen)) 1933 copym = m_pullup(copym, hlen); 1934 if (copym != NULL) { 1935 /* 1936 * We don't bother to fragment if the IP length is greater 1937 * than the interface's MTU. Can this possibly matter? 1938 */ 1939 ip = mtod(copym, struct ip *); 1940 ip->ip_len = htons(ip->ip_len); 1941 ip->ip_off = htons(ip->ip_off); 1942 ip->ip_sum = 0; 1943 if (ip->ip_vhl == IP_VHL_BORING) { 1944 ip->ip_sum = in_cksum_hdr(ip); 1945 } else { 1946 ip->ip_sum = in_cksum(copym, hlen); 1947 } 1948 /* 1949 * NB: 1950 * It's not clear whether there are any lingering 1951 * reentrancy problems in other areas which might 1952 * be exposed by using ip_input directly (in 1953 * particular, everything which modifies the packet 1954 * in-place). Yet another option is using the 1955 * protosw directly to deliver the looped back 1956 * packet. For the moment, we'll err on the side 1957 * of safety by using if_simloop(). 1958 */ 1959#if 1 /* XXX */ 1960 if (dst->sin_family != AF_INET) { 1961 printf("ip_mloopback: bad address family %d\n", 1962 dst->sin_family); 1963 dst->sin_family = AF_INET; 1964 } 1965#endif 1966 1967#ifdef notdef 1968 copym->m_pkthdr.rcvif = ifp; 1969 ip_input(copym); 1970#else 1971 /* if the checksum hasn't been computed, mark it as valid */ 1972 if (copym->m_pkthdr.csum_flags & CSUM_DELAY_DATA) { 1973 copym->m_pkthdr.csum_flags |= 1974 CSUM_DATA_VALID | CSUM_PSEUDO_HDR; 1975 copym->m_pkthdr.csum_data = 0xffff; 1976 } 1977 if_simloop(ifp, copym, dst->sin_family, 0); 1978#endif 1979 } 1980} 1981