1c398230bSWarner Losh /*- 2df8bae1dSRodney W. Grimes * Copyright (c) 1982, 1986, 1988, 1990, 1993 3df8bae1dSRodney W. Grimes * The Regents of the University of California. All rights reserved. 4df8bae1dSRodney W. Grimes * 5df8bae1dSRodney W. Grimes * Redistribution and use in source and binary forms, with or without 6df8bae1dSRodney W. Grimes * modification, are permitted provided that the following conditions 7df8bae1dSRodney W. Grimes * are met: 8df8bae1dSRodney W. Grimes * 1. Redistributions of source code must retain the above copyright 9df8bae1dSRodney W. Grimes * notice, this list of conditions and the following disclaimer. 10df8bae1dSRodney W. Grimes * 2. Redistributions in binary form must reproduce the above copyright 11df8bae1dSRodney W. Grimes * notice, this list of conditions and the following disclaimer in the 12df8bae1dSRodney W. Grimes * documentation and/or other materials provided with the distribution. 13df8bae1dSRodney W. Grimes * 4. Neither the name of the University nor the names of its contributors 14df8bae1dSRodney W. Grimes * may be used to endorse or promote products derived from this software 15df8bae1dSRodney W. Grimes * without specific prior written permission. 16df8bae1dSRodney W. Grimes * 17df8bae1dSRodney W. Grimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 18df8bae1dSRodney W. Grimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 19df8bae1dSRodney W. Grimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 20df8bae1dSRodney W. Grimes * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 21df8bae1dSRodney W. Grimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 22df8bae1dSRodney W. Grimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 23df8bae1dSRodney W. Grimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 24df8bae1dSRodney W. Grimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 25df8bae1dSRodney W. Grimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 26df8bae1dSRodney W. Grimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 27df8bae1dSRodney W. Grimes * SUCH DAMAGE. 28df8bae1dSRodney W. Grimes * 29df8bae1dSRodney W. Grimes * @(#)ip_output.c 8.3 (Berkeley) 1/21/94 30c3aac50fSPeter Wemm * $FreeBSD$ 31df8bae1dSRodney W. Grimes */ 32df8bae1dSRodney W. Grimes 331f7e052cSJulian Elischer #include "opt_ipfw.h" 346a800098SYoshinobu Inoue #include "opt_ipsec.h" 354ed84624SRobert Watson #include "opt_mac.h" 3653dcc544SMike Silbersack #include "opt_mbuf_stress_test.h" 37fbd1372aSJoerg Wunsch 38df8bae1dSRodney W. Grimes #include <sys/param.h> 3926f9a767SRodney W. Grimes #include <sys/systm.h> 40f0f6d643SLuigi Rizzo #include <sys/kernel.h> 414ed84624SRobert Watson #include <sys/mac.h> 42df8bae1dSRodney W. Grimes #include <sys/malloc.h> 43df8bae1dSRodney W. Grimes #include <sys/mbuf.h> 44df8bae1dSRodney W. Grimes #include <sys/protosw.h> 45df8bae1dSRodney W. Grimes #include <sys/socket.h> 46df8bae1dSRodney W. Grimes #include <sys/socketvar.h> 479d9edc56SMike Silbersack #include <sys/sysctl.h> 48df8bae1dSRodney W. Grimes 49df8bae1dSRodney W. Grimes #include <net/if.h> 509b932e9eSAndre Oppermann #include <net/netisr.h> 51c21fd232SAndre Oppermann #include <net/pfil.h> 52df8bae1dSRodney W. Grimes #include <net/route.h> 53df8bae1dSRodney W. Grimes 54df8bae1dSRodney W. Grimes #include <netinet/in.h> 55df8bae1dSRodney W. Grimes #include <netinet/in_systm.h> 56df8bae1dSRodney W. Grimes #include <netinet/ip.h> 57df8bae1dSRodney W. Grimes #include <netinet/in_pcb.h> 58df8bae1dSRodney W. Grimes #include <netinet/in_var.h> 59df8bae1dSRodney W. Grimes #include <netinet/ip_var.h> 60ef39adf0SAndre Oppermann #include <netinet/ip_options.h> 61df8bae1dSRodney W. Grimes 621dfcf0d2SAndre Oppermann #if defined(IPSEC) || defined(FAST_IPSEC) 631dfcf0d2SAndre Oppermann #include <netinet/ip_ipsec.h> 646a800098SYoshinobu Inoue #ifdef IPSEC 656a800098SYoshinobu Inoue #include <netinet6/ipsec.h> 661dfcf0d2SAndre Oppermann #endif 671dfcf0d2SAndre Oppermann #ifdef FAST_IPSEC 681dfcf0d2SAndre Oppermann #include <netipsec/ipsec.h> 696a800098SYoshinobu Inoue #endif 706a800098SYoshinobu Inoue #endif /*IPSEC*/ 716a800098SYoshinobu Inoue 721dfcf0d2SAndre Oppermann #include <machine/in_cksum.h> 731dfcf0d2SAndre Oppermann 741dfcf0d2SAndre Oppermann static MALLOC_DEFINE(M_IPMOPTS, "ip_moptions", "internet multicast options"); 75b9234fafSSam Leffler 762b25acc1SLuigi Rizzo #define print_ip(x, a, y) printf("%s %d.%d.%d.%d%s",\ 772b25acc1SLuigi Rizzo x, (ntohl(a.s_addr)>>24)&0xFF,\ 78f9e354dfSJulian Elischer (ntohl(a.s_addr)>>16)&0xFF,\ 79f9e354dfSJulian Elischer (ntohl(a.s_addr)>>8)&0xFF,\ 802b25acc1SLuigi Rizzo (ntohl(a.s_addr))&0xFF, y); 81f9e354dfSJulian Elischer 82f23b4c91SGarrett Wollman u_short ip_id; 83f23b4c91SGarrett Wollman 8453dcc544SMike Silbersack #ifdef MBUF_STRESS_TEST 859d9edc56SMike Silbersack int mbuf_frag_size = 0; 869d9edc56SMike Silbersack SYSCTL_INT(_net_inet_ip, OID_AUTO, mbuf_frag_size, CTLFLAG_RW, 879d9edc56SMike Silbersack &mbuf_frag_size, 0, "Fragment outgoing mbufs to this size"); 889d9edc56SMike Silbersack #endif 899d9edc56SMike Silbersack 904d77a549SAlfred Perlstein static struct ifnet *ip_multicast_if(struct in_addr *, int *); 91df8bae1dSRodney W. Grimes static void ip_mloopback 924d77a549SAlfred Perlstein (struct ifnet *, struct mbuf *, struct sockaddr_in *, int); 9389924e58SRobert Watson static int ip_getmoptions(struct inpcb *, struct sockopt *); 945c918b56SRobert Watson static int ip_setmoptions(struct inpcb *, struct sockopt *); 95afed1b49SDarren Reed 96afed1b49SDarren Reed 9793e0e116SJulian Elischer extern struct protosw inetsw[]; 9893e0e116SJulian Elischer 99df8bae1dSRodney W. Grimes /* 100df8bae1dSRodney W. Grimes * IP output. The packet in mbuf chain m contains a skeletal IP 101df8bae1dSRodney W. Grimes * header (with len, off, ttl, proto, tos, src, dst). 102df8bae1dSRodney W. Grimes * The mbuf chain containing the packet will be freed. 103df8bae1dSRodney W. Grimes * The mbuf opt, if present, will not be freed. 1043de758d3SRobert Watson * In the IP forwarding case, the packet will arrive with options already 1053de758d3SRobert Watson * inserted, so must have a NULL opt pointer. 106df8bae1dSRodney W. Grimes */ 107df8bae1dSRodney W. Grimes int 108ac9d7e26SMax Laier ip_output(struct mbuf *m, struct mbuf *opt, struct route *ro, 1091e78ac21SJeffrey Hsu int flags, struct ip_moptions *imo, struct inpcb *inp) 110df8bae1dSRodney W. Grimes { 1111e78ac21SJeffrey Hsu struct ip *ip; 1122b25acc1SLuigi Rizzo struct ifnet *ifp = NULL; /* keep compiler happy */ 113ac9d7e26SMax Laier struct mbuf *m0; 11423bf9953SPoul-Henning Kamp int hlen = sizeof (struct ip); 1159b932e9eSAndre Oppermann int len, error = 0; 1162b25acc1SLuigi Rizzo struct sockaddr_in *dst = NULL; /* keep compiler happy */ 1173956b023SLuigi Rizzo struct in_ifaddr *ia = NULL; 118db4f9cc7SJonathan Lemon int isbroadcast, sw_csum; 119e3f406b3SRuslan Ermilov struct route iproute; 1209b932e9eSAndre Oppermann struct in_addr odst; 1219b932e9eSAndre Oppermann #ifdef IPFIREWALL_FORWARD 1229b932e9eSAndre Oppermann struct m_tag *fwd_tag = NULL; 1239b932e9eSAndre Oppermann #endif 124ac9d7e26SMax Laier M_ASSERTPKTHDR(m); 12536e8826fSMax Laier 12602c1c707SAndre Oppermann if (ro == NULL) { 12702c1c707SAndre Oppermann ro = &iproute; 12802c1c707SAndre Oppermann bzero(ro, sizeof (*ro)); 12902c1c707SAndre Oppermann } 13002c1c707SAndre Oppermann 13184843845SSam Leffler if (inp != NULL) 13284843845SSam Leffler INP_LOCK_ASSERT(inp); 1332b25acc1SLuigi Rizzo 134df8bae1dSRodney W. Grimes if (opt) { 135cb7641e8SMaxim Konovalov len = 0; 136df8bae1dSRodney W. Grimes m = ip_insertoptions(m, opt, &len); 137cb7641e8SMaxim Konovalov if (len != 0) 138df8bae1dSRodney W. Grimes hlen = len; 139df8bae1dSRodney W. Grimes } 140df8bae1dSRodney W. Grimes ip = mtod(m, struct ip *); 1413efc3014SJulian Elischer 142df8bae1dSRodney W. Grimes /* 1436e49b1feSGarrett Wollman * Fill in IP header. If we are not allowing fragmentation, 144e0f630eaSAndre Oppermann * then the ip_id field is meaningless, but we don't set it 145e0f630eaSAndre Oppermann * to zero. Doing so causes various problems when devices along 146e0f630eaSAndre Oppermann * the path (routers, load balancers, firewalls, etc.) illegally 147e0f630eaSAndre Oppermann * disable DF on our packet. Note that a 16-bit counter 1486e49b1feSGarrett Wollman * will wrap around in less than 10 seconds at 100 Mbit/s on a 1496e49b1feSGarrett Wollman * medium with MTU 1500. See Steven M. Bellovin, "A Technique 1506e49b1feSGarrett Wollman * for Counting NATted Hosts", Proc. IMW'02, available at 1514d09f5a0SGleb Smirnoff * <http://www.cs.columbia.edu/~smb/papers/fnat.pdf>. 152df8bae1dSRodney W. Grimes */ 153df8bae1dSRodney W. Grimes if ((flags & (IP_FORWARDING|IP_RAWOUTPUT)) == 0) { 15453be11f6SPoul-Henning Kamp ip->ip_v = IPVERSION; 15553be11f6SPoul-Henning Kamp ip->ip_hl = hlen >> 2; 1561f44b0a1SDavid Malone ip->ip_id = ip_newid(); 157df8bae1dSRodney W. Grimes ipstat.ips_localout++; 158df8bae1dSRodney W. Grimes } else { 15953be11f6SPoul-Henning Kamp hlen = ip->ip_hl << 2; 160df8bae1dSRodney W. Grimes } 1619c9137eaSGarrett Wollman 162df8bae1dSRodney W. Grimes dst = (struct sockaddr_in *)&ro->ro_dst; 1639b932e9eSAndre Oppermann again: 164df8bae1dSRodney W. Grimes /* 165df8bae1dSRodney W. Grimes * If there is a cached route, 166df8bae1dSRodney W. Grimes * check that it is to the same destination 167df8bae1dSRodney W. Grimes * and is still up. If not, free it and try again. 168a4a6e773SHajimu UMEMOTO * The address family should also be checked in case of sharing the 169a4a6e773SHajimu UMEMOTO * cache with IPv6. 170df8bae1dSRodney W. Grimes */ 171df8bae1dSRodney W. Grimes if (ro->ro_rt && ((ro->ro_rt->rt_flags & RTF_UP) == 0 || 172a4a6e773SHajimu UMEMOTO dst->sin_family != AF_INET || 1739b932e9eSAndre Oppermann dst->sin_addr.s_addr != ip->ip_dst.s_addr)) { 174df8bae1dSRodney W. Grimes RTFREE(ro->ro_rt); 175df8bae1dSRodney W. Grimes ro->ro_rt = (struct rtentry *)0; 176df8bae1dSRodney W. Grimes } 1779b932e9eSAndre Oppermann #ifdef IPFIREWALL_FORWARD 1789b932e9eSAndre Oppermann if (ro->ro_rt == NULL && fwd_tag == NULL) { 1799b932e9eSAndre Oppermann #else 1800b17fba7SAndre Oppermann if (ro->ro_rt == NULL) { 1819b932e9eSAndre Oppermann #endif 182a4a6e773SHajimu UMEMOTO bzero(dst, sizeof(*dst)); 183df8bae1dSRodney W. Grimes dst->sin_family = AF_INET; 184df8bae1dSRodney W. Grimes dst->sin_len = sizeof(*dst); 1859b932e9eSAndre Oppermann dst->sin_addr = ip->ip_dst; 186df8bae1dSRodney W. Grimes } 187df8bae1dSRodney W. Grimes /* 188df8bae1dSRodney W. Grimes * If routing to interface only, 189df8bae1dSRodney W. Grimes * short circuit routing lookup. 190df8bae1dSRodney W. Grimes */ 191df8bae1dSRodney W. Grimes if (flags & IP_ROUTETOIF) { 1920b17fba7SAndre Oppermann if ((ia = ifatoia(ifa_ifwithdstaddr(sintosa(dst)))) == NULL && 1930b17fba7SAndre Oppermann (ia = ifatoia(ifa_ifwithnet(sintosa(dst)))) == NULL) { 194df8bae1dSRodney W. Grimes ipstat.ips_noroute++; 195df8bae1dSRodney W. Grimes error = ENETUNREACH; 196df8bae1dSRodney W. Grimes goto bad; 197df8bae1dSRodney W. Grimes } 198df8bae1dSRodney W. Grimes ifp = ia->ia_ifp; 199df8bae1dSRodney W. Grimes ip->ip_ttl = 1; 2009f9b3dc4SGarrett Wollman isbroadcast = in_broadcast(dst->sin_addr, ifp); 201773725a2SAndre Oppermann } else if (flags & IP_SENDONES) { 202773725a2SAndre Oppermann if ((ia = ifatoia(ifa_ifwithbroadaddr(sintosa(dst)))) == NULL) { 203773725a2SAndre Oppermann ipstat.ips_noroute++; 204773725a2SAndre Oppermann error = ENETUNREACH; 205773725a2SAndre Oppermann goto bad; 206773725a2SAndre Oppermann } 207773725a2SAndre Oppermann ifp = ia->ia_ifp; 208773725a2SAndre Oppermann ip->ip_dst.s_addr = INADDR_BROADCAST; 209773725a2SAndre Oppermann dst->sin_addr = ip->ip_dst; 210773725a2SAndre Oppermann ip->ip_ttl = 1; 211773725a2SAndre Oppermann isbroadcast = 1; 2123afefa39SDaniel C. Sobral } else if (IN_MULTICAST(ntohl(ip->ip_dst.s_addr)) && 21338c1bc35SRuslan Ermilov imo != NULL && imo->imo_multicast_ifp != NULL) { 2143afefa39SDaniel C. Sobral /* 21538c1bc35SRuslan Ermilov * Bypass the normal routing lookup for multicast 21638c1bc35SRuslan Ermilov * packets if the interface is specified. 2173afefa39SDaniel C. Sobral */ 21838c1bc35SRuslan Ermilov ifp = imo->imo_multicast_ifp; 21938c1bc35SRuslan Ermilov IFP_TO_IA(ifp, ia); 22038c1bc35SRuslan Ermilov isbroadcast = 0; /* fool gcc */ 221df8bae1dSRodney W. Grimes } else { 2222c17fe93SGarrett Wollman /* 22397d8d152SAndre Oppermann * We want to do any cloning requested by the link layer, 22497d8d152SAndre Oppermann * as this is probably required in all cases for correct 22597d8d152SAndre Oppermann * operation (as it is for ARP). 2262c17fe93SGarrett Wollman */ 2270b17fba7SAndre Oppermann if (ro->ro_rt == NULL) 228e6e51f05SLuigi Rizzo rtalloc_ign(ro, 0); 2290b17fba7SAndre Oppermann if (ro->ro_rt == NULL) { 230df8bae1dSRodney W. Grimes ipstat.ips_noroute++; 231df8bae1dSRodney W. Grimes error = EHOSTUNREACH; 232df8bae1dSRodney W. Grimes goto bad; 233df8bae1dSRodney W. Grimes } 234df8bae1dSRodney W. Grimes ia = ifatoia(ro->ro_rt->rt_ifa); 235df8bae1dSRodney W. Grimes ifp = ro->ro_rt->rt_ifp; 23697d8d152SAndre Oppermann ro->ro_rt->rt_rmx.rmx_pksent++; 237df8bae1dSRodney W. Grimes if (ro->ro_rt->rt_flags & RTF_GATEWAY) 238f2c2962eSRuslan Ermilov dst = (struct sockaddr_in *)ro->ro_rt->rt_gateway; 2399f9b3dc4SGarrett Wollman if (ro->ro_rt->rt_flags & RTF_HOST) 240f2c2962eSRuslan Ermilov isbroadcast = (ro->ro_rt->rt_flags & RTF_BROADCAST); 2419f9b3dc4SGarrett Wollman else 2429f9b3dc4SGarrett Wollman isbroadcast = in_broadcast(dst->sin_addr, ifp); 243df8bae1dSRodney W. Grimes } 2449b932e9eSAndre Oppermann if (IN_MULTICAST(ntohl(ip->ip_dst.s_addr))) { 245df8bae1dSRodney W. Grimes struct in_multi *inm; 246df8bae1dSRodney W. Grimes 247df8bae1dSRodney W. Grimes m->m_flags |= M_MCAST; 248df8bae1dSRodney W. Grimes /* 249df8bae1dSRodney W. Grimes * IP destination address is multicast. Make sure "dst" 250df8bae1dSRodney W. Grimes * still points to the address in "ro". (It may have been 251df8bae1dSRodney W. Grimes * changed to point to a gateway address, above.) 252df8bae1dSRodney W. Grimes */ 253df8bae1dSRodney W. Grimes dst = (struct sockaddr_in *)&ro->ro_dst; 254df8bae1dSRodney W. Grimes /* 255df8bae1dSRodney W. Grimes * See if the caller provided any multicast options 256df8bae1dSRodney W. Grimes */ 257df8bae1dSRodney W. Grimes if (imo != NULL) { 258df8bae1dSRodney W. Grimes ip->ip_ttl = imo->imo_multicast_ttl; 2591c5de19aSGarrett Wollman if (imo->imo_multicast_vif != -1) 2601c5de19aSGarrett Wollman ip->ip_src.s_addr = 261bbb4330bSLuigi Rizzo ip_mcast_src ? 262bbb4330bSLuigi Rizzo ip_mcast_src(imo->imo_multicast_vif) : 263bbb4330bSLuigi Rizzo INADDR_ANY; 264df8bae1dSRodney W. Grimes } else 265df8bae1dSRodney W. Grimes ip->ip_ttl = IP_DEFAULT_MULTICAST_TTL; 266df8bae1dSRodney W. Grimes /* 267df8bae1dSRodney W. Grimes * Confirm that the outgoing interface supports multicast. 268df8bae1dSRodney W. Grimes */ 2691c5de19aSGarrett Wollman if ((imo == NULL) || (imo->imo_multicast_vif == -1)) { 270df8bae1dSRodney W. Grimes if ((ifp->if_flags & IFF_MULTICAST) == 0) { 271df8bae1dSRodney W. Grimes ipstat.ips_noroute++; 272df8bae1dSRodney W. Grimes error = ENETUNREACH; 273df8bae1dSRodney W. Grimes goto bad; 274df8bae1dSRodney W. Grimes } 2751c5de19aSGarrett Wollman } 276df8bae1dSRodney W. Grimes /* 277df8bae1dSRodney W. Grimes * If source address not specified yet, use address 278df8bae1dSRodney W. Grimes * of outgoing interface. 279df8bae1dSRodney W. Grimes */ 280df8bae1dSRodney W. Grimes if (ip->ip_src.s_addr == INADDR_ANY) { 28138c1bc35SRuslan Ermilov /* Interface may have no addresses. */ 28238c1bc35SRuslan Ermilov if (ia != NULL) 28338c1bc35SRuslan Ermilov ip->ip_src = IA_SIN(ia)->sin_addr; 284df8bae1dSRodney W. Grimes } 285df8bae1dSRodney W. Grimes 286dd5a318bSRobert Watson IN_MULTI_LOCK(); 2879b932e9eSAndre Oppermann IN_LOOKUP_MULTI(ip->ip_dst, ifp, inm); 288df8bae1dSRodney W. Grimes if (inm != NULL && 289df8bae1dSRodney W. Grimes (imo == NULL || imo->imo_multicast_loop)) { 290dd5a318bSRobert Watson IN_MULTI_UNLOCK(); 291df8bae1dSRodney W. Grimes /* 292df8bae1dSRodney W. Grimes * If we belong to the destination multicast group 293df8bae1dSRodney W. Grimes * on the outgoing interface, and the caller did not 294df8bae1dSRodney W. Grimes * forbid loopback, loop back a copy. 295df8bae1dSRodney W. Grimes */ 29686b1d6d2SBill Fenner ip_mloopback(ifp, m, dst, hlen); 297df8bae1dSRodney W. Grimes } 298df8bae1dSRodney W. Grimes else { 299dd5a318bSRobert Watson IN_MULTI_UNLOCK(); 300df8bae1dSRodney W. Grimes /* 301df8bae1dSRodney W. Grimes * If we are acting as a multicast router, perform 302df8bae1dSRodney W. Grimes * multicast forwarding as if the packet had just 303df8bae1dSRodney W. Grimes * arrived on the interface to which we are about 304df8bae1dSRodney W. Grimes * to send. The multicast forwarding function 305df8bae1dSRodney W. Grimes * recursively calls this function, using the 306df8bae1dSRodney W. Grimes * IP_FORWARDING flag to prevent infinite recursion. 307df8bae1dSRodney W. Grimes * 308df8bae1dSRodney W. Grimes * Multicasts that are looped back by ip_mloopback(), 309df8bae1dSRodney W. Grimes * above, will be forwarded by the ip_input() routine, 310df8bae1dSRodney W. Grimes * if necessary. 311df8bae1dSRodney W. Grimes */ 312df8bae1dSRodney W. Grimes if (ip_mrouter && (flags & IP_FORWARDING) == 0) { 313f0068c4aSGarrett Wollman /* 314bbb4330bSLuigi Rizzo * If rsvp daemon is not running, do not 315f0068c4aSGarrett Wollman * set ip_moptions. This ensures that the packet 316f0068c4aSGarrett Wollman * is multicast and not just sent down one link 317f0068c4aSGarrett Wollman * as prescribed by rsvpd. 318f0068c4aSGarrett Wollman */ 319b124e4f2SGarrett Wollman if (!rsvp_on) 320f0068c4aSGarrett Wollman imo = NULL; 321bbb4330bSLuigi Rizzo if (ip_mforward && 322bbb4330bSLuigi Rizzo ip_mforward(ip, ifp, m, imo) != 0) { 323df8bae1dSRodney W. Grimes m_freem(m); 324df8bae1dSRodney W. Grimes goto done; 325df8bae1dSRodney W. Grimes } 326df8bae1dSRodney W. Grimes } 327df8bae1dSRodney W. Grimes } 3285e9ae478SGarrett Wollman 329df8bae1dSRodney W. Grimes /* 330df8bae1dSRodney W. Grimes * Multicasts with a time-to-live of zero may be looped- 331df8bae1dSRodney W. Grimes * back, above, but must not be transmitted on a network. 332df8bae1dSRodney W. Grimes * Also, multicasts addressed to the loopback interface 333df8bae1dSRodney W. Grimes * are not sent -- the above call to ip_mloopback() will 334df8bae1dSRodney W. Grimes * loop back a copy if this host actually belongs to the 335df8bae1dSRodney W. Grimes * destination group on the loopback interface. 336df8bae1dSRodney W. Grimes */ 337f5fea3ddSPaul Traina if (ip->ip_ttl == 0 || ifp->if_flags & IFF_LOOPBACK) { 338df8bae1dSRodney W. Grimes m_freem(m); 339df8bae1dSRodney W. Grimes goto done; 340df8bae1dSRodney W. Grimes } 341df8bae1dSRodney W. Grimes 342df8bae1dSRodney W. Grimes goto sendit; 343df8bae1dSRodney W. Grimes } 344df8bae1dSRodney W. Grimes #ifndef notdef 345df8bae1dSRodney W. Grimes /* 3462b25acc1SLuigi Rizzo * If the source address is not specified yet, use the address 347cb459254SJohn-Mark Gurney * of the outoing interface. 348df8bae1dSRodney W. Grimes */ 349f9e354dfSJulian Elischer if (ip->ip_src.s_addr == INADDR_ANY) { 35038c1bc35SRuslan Ermilov /* Interface may have no addresses. */ 35138c1bc35SRuslan Ermilov if (ia != NULL) { 352df8bae1dSRodney W. Grimes ip->ip_src = IA_SIN(ia)->sin_addr; 353f9e354dfSJulian Elischer } 35438c1bc35SRuslan Ermilov } 355f9e354dfSJulian Elischer #endif /* notdef */ 356ca7a789aSMax Laier /* 357ca7a789aSMax Laier * Verify that we have any chance at all of being able to queue the 358ca7a789aSMax Laier * packet or packet fragments, unless ALTQ is enabled on the given 359ca7a789aSMax Laier * interface in which case packetdrop should be done by queueing. 360ca7a789aSMax Laier */ 36102b199f1SMax Laier #ifdef ALTQ 362ca7a789aSMax Laier if ((!ALTQ_IS_ENABLED(&ifp->if_snd)) && 363ca7a789aSMax Laier ((ifp->if_snd.ifq_len + ip->ip_len / ifp->if_mtu + 1) >= 364ca7a789aSMax Laier ifp->if_snd.ifq_maxlen)) 365ca7a789aSMax Laier #else 366b5390296SDavid Greenman if ((ifp->if_snd.ifq_len + ip->ip_len / ifp->if_mtu + 1) >= 367ca7a789aSMax Laier ifp->if_snd.ifq_maxlen) 368ca7a789aSMax Laier #endif /* ALTQ */ 369ca7a789aSMax Laier { 370b5390296SDavid Greenman error = ENOBUFS; 37135609d45SJonathan Lemon ipstat.ips_odropped++; 372bbce982bSGleb Smirnoff ifp->if_snd.ifq_drops += (ip->ip_len / ifp->if_mtu + 1); 373b5390296SDavid Greenman goto bad; 374b5390296SDavid Greenman } 375b5390296SDavid Greenman 376b5390296SDavid Greenman /* 377df8bae1dSRodney W. Grimes * Look for broadcast address and 3788c3f5566SRuslan Ermilov * verify user is allowed to send 379df8bae1dSRodney W. Grimes * such a packet. 380df8bae1dSRodney W. Grimes */ 3819f9b3dc4SGarrett Wollman if (isbroadcast) { 382df8bae1dSRodney W. Grimes if ((ifp->if_flags & IFF_BROADCAST) == 0) { 383df8bae1dSRodney W. Grimes error = EADDRNOTAVAIL; 384df8bae1dSRodney W. Grimes goto bad; 385df8bae1dSRodney W. Grimes } 386df8bae1dSRodney W. Grimes if ((flags & IP_ALLOWBROADCAST) == 0) { 387df8bae1dSRodney W. Grimes error = EACCES; 388df8bae1dSRodney W. Grimes goto bad; 389df8bae1dSRodney W. Grimes } 390df8bae1dSRodney W. Grimes /* don't allow broadcast messages to be fragmented */ 3911e78ac21SJeffrey Hsu if (ip->ip_len > ifp->if_mtu) { 392df8bae1dSRodney W. Grimes error = EMSGSIZE; 393df8bae1dSRodney W. Grimes goto bad; 394df8bae1dSRodney W. Grimes } 395df8bae1dSRodney W. Grimes m->m_flags |= M_BCAST; 3969f9b3dc4SGarrett Wollman } else { 397df8bae1dSRodney W. Grimes m->m_flags &= ~M_BCAST; 3989f9b3dc4SGarrett Wollman } 399df8bae1dSRodney W. Grimes 400f1743588SDarren Reed sendit: 4011dfcf0d2SAndre Oppermann #if defined(IPSEC) || defined(FAST_IPSEC) 4021dfcf0d2SAndre Oppermann switch(ip_ipsec_output(&m, inp, &flags, &error, &ro, &iproute, &dst, &ia, &ifp)) { 4031dfcf0d2SAndre Oppermann case 1: 40433841545SHajimu UMEMOTO goto bad; 4051dfcf0d2SAndre Oppermann case -1: 4061dfcf0d2SAndre Oppermann goto done; 4071dfcf0d2SAndre Oppermann case 0: 40833841545SHajimu UMEMOTO default: 4091dfcf0d2SAndre Oppermann break; /* Continue with packet processing. */ 41033841545SHajimu UMEMOTO } 4111dfcf0d2SAndre Oppermann /* Update variables that are affected by ipsec4_output(). */ 41233841545SHajimu UMEMOTO ip = mtod(m, struct ip *); 41333841545SHajimu UMEMOTO hlen = ip->ip_hl << 2; 41433841545SHajimu UMEMOTO #endif /* IPSEC */ 41533841545SHajimu UMEMOTO 416c21fd232SAndre Oppermann /* Jump over all PFIL processing if hooks are not active. */ 417604afec4SChristian S.J. Peron if (!PFIL_HOOKED(&inet_pfil_hook)) 418c21fd232SAndre Oppermann goto passout; 419c21fd232SAndre Oppermann 420c21fd232SAndre Oppermann /* Run through list of hooks for output packets. */ 4219b932e9eSAndre Oppermann odst.s_addr = ip->ip_dst.s_addr; 422d6a8d588SMax Laier error = pfil_run_hooks(&inet_pfil_hook, &m, ifp, PFIL_OUT, inp); 423134ea224SSam Leffler if (error != 0 || m == NULL) 424c4ac87eaSDarren Reed goto done; 4259b932e9eSAndre Oppermann 426c4ac87eaSDarren Reed ip = mtod(m, struct ip *); 427fed1c7e9SSøren Schmidt 4289b932e9eSAndre Oppermann /* See if destination IP address was changed by packet filter. */ 4299b932e9eSAndre Oppermann if (odst.s_addr != ip->ip_dst.s_addr) { 4309b932e9eSAndre Oppermann m->m_flags |= M_SKIP_FIREWALL; 431f4fca2d8SAndre Oppermann /* If destination is now ourself drop to ip_input(). */ 4329b932e9eSAndre Oppermann if (in_localip(ip->ip_dst)) { 4339b932e9eSAndre Oppermann m->m_flags |= M_FASTFWD_OURS; 434f9e354dfSJulian Elischer if (m->m_pkthdr.rcvif == NULL) 435a9c92b54SAndre Oppermann m->m_pkthdr.rcvif = loif; 43620c822f3SJonathan Lemon if (m->m_pkthdr.csum_flags & CSUM_DELAY_DATA) { 43720c822f3SJonathan Lemon m->m_pkthdr.csum_flags |= 43820c822f3SJonathan Lemon CSUM_DATA_VALID | CSUM_PSEUDO_HDR; 439ac9d7e26SMax Laier m->m_pkthdr.csum_data = 0xffff; 44020c822f3SJonathan Lemon } 44120c822f3SJonathan Lemon m->m_pkthdr.csum_flags |= 44220c822f3SJonathan Lemon CSUM_IP_CHECKED | CSUM_IP_VALID; 4439b932e9eSAndre Oppermann 4449b932e9eSAndre Oppermann error = netisr_queue(NETISR_IP, m); 4459b932e9eSAndre Oppermann goto done; 4469b932e9eSAndre Oppermann } else 447f4fca2d8SAndre Oppermann goto again; /* Redo the routing table lookup. */ 4489b932e9eSAndre Oppermann } 4499b932e9eSAndre Oppermann 4509b932e9eSAndre Oppermann #ifdef IPFIREWALL_FORWARD 4519b932e9eSAndre Oppermann /* See if local, if yes, send it to netisr with IP_FASTFWD_OURS. */ 4529b932e9eSAndre Oppermann if (m->m_flags & M_FASTFWD_OURS) { 4539b932e9eSAndre Oppermann if (m->m_pkthdr.rcvif == NULL) 454a9c92b54SAndre Oppermann m->m_pkthdr.rcvif = loif; 4559b932e9eSAndre Oppermann if (m->m_pkthdr.csum_flags & CSUM_DELAY_DATA) { 4569b932e9eSAndre Oppermann m->m_pkthdr.csum_flags |= 4579b932e9eSAndre Oppermann CSUM_DATA_VALID | CSUM_PSEUDO_HDR; 4589b932e9eSAndre Oppermann m->m_pkthdr.csum_data = 0xffff; 4599b932e9eSAndre Oppermann } 4609b932e9eSAndre Oppermann m->m_pkthdr.csum_flags |= 4619b932e9eSAndre Oppermann CSUM_IP_CHECKED | CSUM_IP_VALID; 4629b932e9eSAndre Oppermann 4639b932e9eSAndre Oppermann error = netisr_queue(NETISR_IP, m); 464f9e354dfSJulian Elischer goto done; 465f9e354dfSJulian Elischer } 4669b932e9eSAndre Oppermann /* Or forward to some other address? */ 4679b932e9eSAndre Oppermann fwd_tag = m_tag_find(m, PACKET_TAG_IPFORWARD, NULL); 4689b932e9eSAndre Oppermann if (fwd_tag) { 4699b932e9eSAndre Oppermann dst = (struct sockaddr_in *)&ro->ro_dst; 4709b932e9eSAndre Oppermann bcopy((fwd_tag+1), dst, sizeof(struct sockaddr_in)); 4719b932e9eSAndre Oppermann m->m_flags |= M_SKIP_FIREWALL; 4729b932e9eSAndre Oppermann m_tag_delete(m, fwd_tag); 4739b932e9eSAndre Oppermann goto again; 474099dd043SAndre Oppermann } 475099dd043SAndre Oppermann #endif /* IPFIREWALL_FORWARD */ 4762b25acc1SLuigi Rizzo 477c21fd232SAndre Oppermann passout: 47851c8ec4aSRuslan Ermilov /* 127/8 must not appear on wire - RFC1122. */ 47951c8ec4aSRuslan Ermilov if ((ntohl(ip->ip_dst.s_addr) >> IN_CLASSA_NSHIFT) == IN_LOOPBACKNET || 48051c8ec4aSRuslan Ermilov (ntohl(ip->ip_src.s_addr) >> IN_CLASSA_NSHIFT) == IN_LOOPBACKNET) { 48151c8ec4aSRuslan Ermilov if ((ifp->if_flags & IFF_LOOPBACK) == 0) { 48251c8ec4aSRuslan Ermilov ipstat.ips_badaddr++; 48351c8ec4aSRuslan Ermilov error = EADDRNOTAVAIL; 48451c8ec4aSRuslan Ermilov goto bad; 48551c8ec4aSRuslan Ermilov } 48651c8ec4aSRuslan Ermilov } 48751c8ec4aSRuslan Ermilov 488206a3274SRuslan Ermilov m->m_pkthdr.csum_flags |= CSUM_IP; 489206a3274SRuslan Ermilov sw_csum = m->m_pkthdr.csum_flags & ~ifp->if_hwassist; 490db4f9cc7SJonathan Lemon if (sw_csum & CSUM_DELAY_DATA) { 491db4f9cc7SJonathan Lemon in_delayed_cksum(m); 492db4f9cc7SJonathan Lemon sw_csum &= ~CSUM_DELAY_DATA; 493db4f9cc7SJonathan Lemon } 494206a3274SRuslan Ermilov m->m_pkthdr.csum_flags &= ifp->if_hwassist; 495db4f9cc7SJonathan Lemon 496e7319babSPoul-Henning Kamp /* 497db4f9cc7SJonathan Lemon * If small enough for interface, or the interface will take 498233dcce1SAndre Oppermann * care of the fragmentation for us, we can just send directly. 499df8bae1dSRodney W. Grimes */ 500233dcce1SAndre Oppermann if (ip->ip_len <= ifp->if_mtu || 501233dcce1SAndre Oppermann (m->m_pkthdr.csum_flags & ifp->if_hwassist & CSUM_TSO) != 0 || 502233dcce1SAndre Oppermann ((ip->ip_off & IP_DF) == 0 && (ifp->if_hwassist & CSUM_FRAGMENT))) { 503fd8e4ebcSMike Barcroft ip->ip_len = htons(ip->ip_len); 504fd8e4ebcSMike Barcroft ip->ip_off = htons(ip->ip_off); 505df8bae1dSRodney W. Grimes ip->ip_sum = 0; 50653be11f6SPoul-Henning Kamp if (sw_csum & CSUM_DELAY_IP) 507df8bae1dSRodney W. Grimes ip->ip_sum = in_cksum(m, hlen); 5085da9f8faSJosef Karthauser 509233dcce1SAndre Oppermann /* 510233dcce1SAndre Oppermann * Record statistics for this interface address. 511233dcce1SAndre Oppermann * With CSUM_TSO the byte/packet count will be slightly 512233dcce1SAndre Oppermann * incorrect because we count the IP+TCP headers only 513233dcce1SAndre Oppermann * once instead of for every generated packet. 514233dcce1SAndre Oppermann */ 51538c1bc35SRuslan Ermilov if (!(flags & IP_FORWARDING) && ia) { 516233dcce1SAndre Oppermann if (m->m_pkthdr.csum_flags & CSUM_TSO) 517233dcce1SAndre Oppermann ia->ia_ifa.if_opackets += 518233dcce1SAndre Oppermann m->m_pkthdr.len / m->m_pkthdr.tso_segsz; 519233dcce1SAndre Oppermann else 5205da9f8faSJosef Karthauser ia->ia_ifa.if_opackets++; 5215da9f8faSJosef Karthauser ia->ia_ifa.if_obytes += m->m_pkthdr.len; 5225da9f8faSJosef Karthauser } 52333841545SHajimu UMEMOTO #ifdef IPSEC 52433841545SHajimu UMEMOTO /* clean ipsec history once it goes out of the node */ 52533841545SHajimu UMEMOTO ipsec_delaux(m); 52633841545SHajimu UMEMOTO #endif 52753dcc544SMike Silbersack #ifdef MBUF_STRESS_TEST 5283390d476SMike Silbersack if (mbuf_frag_size && m->m_pkthdr.len > mbuf_frag_size) 5293390d476SMike Silbersack m = m_fragment(m, M_DONTWAIT, mbuf_frag_size); 5309d9edc56SMike Silbersack #endif 531147f74d1SAndre Oppermann /* 532147f74d1SAndre Oppermann * Reset layer specific mbuf flags 533147f74d1SAndre Oppermann * to avoid confusing lower layers. 534147f74d1SAndre Oppermann */ 535147f74d1SAndre Oppermann m->m_flags &= ~(M_PROTOFLAGS); 536147f74d1SAndre Oppermann 537df8bae1dSRodney W. Grimes error = (*ifp->if_output)(ifp, m, 538df8bae1dSRodney W. Grimes (struct sockaddr *)dst, ro->ro_rt); 539df8bae1dSRodney W. Grimes goto done; 540df8bae1dSRodney W. Grimes } 5411e78ac21SJeffrey Hsu 542233dcce1SAndre Oppermann /* Balk when DF bit is set or the interface didn't support TSO. */ 543233dcce1SAndre Oppermann if ((ip->ip_off & IP_DF) || (m->m_pkthdr.csum_flags & CSUM_TSO)) { 544df8bae1dSRodney W. Grimes error = EMSGSIZE; 5453d1f141bSGarrett Wollman /* 5463d1f141bSGarrett Wollman * This case can happen if the user changed the MTU 5473d1f141bSGarrett Wollman * of an interface after enabling IP on it. Because 5483d1f141bSGarrett Wollman * most netifs don't keep track of routes pointing to 5493d1f141bSGarrett Wollman * them, there is no way for one to update all its 5503d1f141bSGarrett Wollman * routes when the MTU is changed. 5513d1f141bSGarrett Wollman */ 55239550088SAndre Oppermann if (ro != NULL && 55339550088SAndre Oppermann (ro->ro_rt->rt_flags & (RTF_UP | RTF_HOST)) && 5541e78ac21SJeffrey Hsu (ro->ro_rt->rt_rmx.rmx_mtu > ifp->if_mtu)) { 5553d1f141bSGarrett Wollman ro->ro_rt->rt_rmx.rmx_mtu = ifp->if_mtu; 5563d1f141bSGarrett Wollman } 557df8bae1dSRodney W. Grimes ipstat.ips_cantfrag++; 558df8bae1dSRodney W. Grimes goto bad; 559df8bae1dSRodney W. Grimes } 5601e78ac21SJeffrey Hsu 5611e78ac21SJeffrey Hsu /* 5621e78ac21SJeffrey Hsu * Too large for interface; fragment if possible. If successful, 5631e78ac21SJeffrey Hsu * on return, m will point to a list of packets to be sent. 5641e78ac21SJeffrey Hsu */ 5651e78ac21SJeffrey Hsu error = ip_fragment(ip, &m, ifp->if_mtu, ifp->if_hwassist, sw_csum); 5661e78ac21SJeffrey Hsu if (error) 567df8bae1dSRodney W. Grimes goto bad; 5681e78ac21SJeffrey Hsu for (; m; m = m0) { 569df8bae1dSRodney W. Grimes m0 = m->m_nextpkt; 570b375c9ecSLuigi Rizzo m->m_nextpkt = 0; 57133841545SHajimu UMEMOTO #ifdef IPSEC 57233841545SHajimu UMEMOTO /* clean ipsec history once it goes out of the node */ 57333841545SHajimu UMEMOTO ipsec_delaux(m); 57433841545SHajimu UMEMOTO #endif 57507203494SDaniel C. Sobral if (error == 0) { 576fe937674SJosef Karthauser /* Record statistics for this interface address. */ 57707203494SDaniel C. Sobral if (ia != NULL) { 578fe937674SJosef Karthauser ia->ia_ifa.if_opackets++; 579fe937674SJosef Karthauser ia->ia_ifa.if_obytes += m->m_pkthdr.len; 58007203494SDaniel C. Sobral } 581147f74d1SAndre Oppermann /* 582147f74d1SAndre Oppermann * Reset layer specific mbuf flags 583147f74d1SAndre Oppermann * to avoid confusing upper layers. 584147f74d1SAndre Oppermann */ 585147f74d1SAndre Oppermann m->m_flags &= ~(M_PROTOFLAGS); 586fe937674SJosef Karthauser 587df8bae1dSRodney W. Grimes error = (*ifp->if_output)(ifp, m, 588df8bae1dSRodney W. Grimes (struct sockaddr *)dst, ro->ro_rt); 589fe937674SJosef Karthauser } else 590df8bae1dSRodney W. Grimes m_freem(m); 591df8bae1dSRodney W. Grimes } 592df8bae1dSRodney W. Grimes 593df8bae1dSRodney W. Grimes if (error == 0) 594df8bae1dSRodney W. Grimes ipstat.ips_fragmented++; 5951e78ac21SJeffrey Hsu 596df8bae1dSRodney W. Grimes done: 5976a800098SYoshinobu Inoue if (ro == &iproute && ro->ro_rt) { 5986a800098SYoshinobu Inoue RTFREE(ro->ro_rt); 599ac9d7e26SMax Laier } 600df8bae1dSRodney W. Grimes return (error); 601df8bae1dSRodney W. Grimes bad: 6023528d68fSBill Paul m_freem(m); 603df8bae1dSRodney W. Grimes goto done; 604df8bae1dSRodney W. Grimes } 605df8bae1dSRodney W. Grimes 6061e78ac21SJeffrey Hsu /* 6071e78ac21SJeffrey Hsu * Create a chain of fragments which fit the given mtu. m_frag points to the 6081e78ac21SJeffrey Hsu * mbuf to be fragmented; on return it points to the chain with the fragments. 6091e78ac21SJeffrey Hsu * Return 0 if no error. If error, m_frag may contain a partially built 6101e78ac21SJeffrey Hsu * chain of fragments that should be freed by the caller. 6111e78ac21SJeffrey Hsu * 6121e78ac21SJeffrey Hsu * if_hwassist_flags is the hw offload capabilities (see if_data.ifi_hwassist) 6131e78ac21SJeffrey Hsu * sw_csum contains the delayed checksums flags (e.g., CSUM_DELAY_IP). 6141e78ac21SJeffrey Hsu */ 6151e78ac21SJeffrey Hsu int 6161e78ac21SJeffrey Hsu ip_fragment(struct ip *ip, struct mbuf **m_frag, int mtu, 6171e78ac21SJeffrey Hsu u_long if_hwassist_flags, int sw_csum) 6181e78ac21SJeffrey Hsu { 6191e78ac21SJeffrey Hsu int error = 0; 6201e78ac21SJeffrey Hsu int hlen = ip->ip_hl << 2; 6211e78ac21SJeffrey Hsu int len = (mtu - hlen) & ~7; /* size of payload in each fragment */ 6221e78ac21SJeffrey Hsu int off; 6231e78ac21SJeffrey Hsu struct mbuf *m0 = *m_frag; /* the original packet */ 6241e78ac21SJeffrey Hsu int firstlen; 6251e78ac21SJeffrey Hsu struct mbuf **mnext; 6261e78ac21SJeffrey Hsu int nfrags; 6271e78ac21SJeffrey Hsu 6281e78ac21SJeffrey Hsu if (ip->ip_off & IP_DF) { /* Fragmentation not allowed */ 6291e78ac21SJeffrey Hsu ipstat.ips_cantfrag++; 6301e78ac21SJeffrey Hsu return EMSGSIZE; 6311e78ac21SJeffrey Hsu } 6321e78ac21SJeffrey Hsu 6331e78ac21SJeffrey Hsu /* 6341e78ac21SJeffrey Hsu * Must be able to put at least 8 bytes per fragment. 6351e78ac21SJeffrey Hsu */ 6361e78ac21SJeffrey Hsu if (len < 8) 6371e78ac21SJeffrey Hsu return EMSGSIZE; 6381e78ac21SJeffrey Hsu 6391e78ac21SJeffrey Hsu /* 6401e78ac21SJeffrey Hsu * If the interface will not calculate checksums on 6411e78ac21SJeffrey Hsu * fragmented packets, then do it here. 6421e78ac21SJeffrey Hsu */ 6431e78ac21SJeffrey Hsu if (m0->m_pkthdr.csum_flags & CSUM_DELAY_DATA && 6441e78ac21SJeffrey Hsu (if_hwassist_flags & CSUM_IP_FRAGS) == 0) { 6451e78ac21SJeffrey Hsu in_delayed_cksum(m0); 6461e78ac21SJeffrey Hsu m0->m_pkthdr.csum_flags &= ~CSUM_DELAY_DATA; 6471e78ac21SJeffrey Hsu } 6481e78ac21SJeffrey Hsu 6491e78ac21SJeffrey Hsu if (len > PAGE_SIZE) { 6501e78ac21SJeffrey Hsu /* 6511e78ac21SJeffrey Hsu * Fragment large datagrams such that each segment 6521e78ac21SJeffrey Hsu * contains a multiple of PAGE_SIZE amount of data, 6531e78ac21SJeffrey Hsu * plus headers. This enables a receiver to perform 6541e78ac21SJeffrey Hsu * page-flipping zero-copy optimizations. 6551e78ac21SJeffrey Hsu * 6561e78ac21SJeffrey Hsu * XXX When does this help given that sender and receiver 6571e78ac21SJeffrey Hsu * could have different page sizes, and also mtu could 6581e78ac21SJeffrey Hsu * be less than the receiver's page size ? 6591e78ac21SJeffrey Hsu */ 6601e78ac21SJeffrey Hsu int newlen; 6611e78ac21SJeffrey Hsu struct mbuf *m; 6621e78ac21SJeffrey Hsu 6631e78ac21SJeffrey Hsu for (m = m0, off = 0; m && (off+m->m_len) <= mtu; m = m->m_next) 6641e78ac21SJeffrey Hsu off += m->m_len; 6651e78ac21SJeffrey Hsu 6661e78ac21SJeffrey Hsu /* 6671e78ac21SJeffrey Hsu * firstlen (off - hlen) must be aligned on an 6681e78ac21SJeffrey Hsu * 8-byte boundary 6691e78ac21SJeffrey Hsu */ 6701e78ac21SJeffrey Hsu if (off < hlen) 6711e78ac21SJeffrey Hsu goto smart_frag_failure; 6721e78ac21SJeffrey Hsu off = ((off - hlen) & ~7) + hlen; 6731e78ac21SJeffrey Hsu newlen = (~PAGE_MASK) & mtu; 6741e78ac21SJeffrey Hsu if ((newlen + sizeof (struct ip)) > mtu) { 6751e78ac21SJeffrey Hsu /* we failed, go back the default */ 6761e78ac21SJeffrey Hsu smart_frag_failure: 6771e78ac21SJeffrey Hsu newlen = len; 6781e78ac21SJeffrey Hsu off = hlen + len; 6791e78ac21SJeffrey Hsu } 6801e78ac21SJeffrey Hsu len = newlen; 6811e78ac21SJeffrey Hsu 6821e78ac21SJeffrey Hsu } else { 6831e78ac21SJeffrey Hsu off = hlen + len; 6841e78ac21SJeffrey Hsu } 6851e78ac21SJeffrey Hsu 6861e78ac21SJeffrey Hsu firstlen = off - hlen; 6871e78ac21SJeffrey Hsu mnext = &m0->m_nextpkt; /* pointer to next packet */ 6881e78ac21SJeffrey Hsu 6891e78ac21SJeffrey Hsu /* 6901e78ac21SJeffrey Hsu * Loop through length of segment after first fragment, 6911e78ac21SJeffrey Hsu * make new header and copy data of each part and link onto chain. 6921e78ac21SJeffrey Hsu * Here, m0 is the original packet, m is the fragment being created. 6931e78ac21SJeffrey Hsu * The fragments are linked off the m_nextpkt of the original 6941e78ac21SJeffrey Hsu * packet, which after processing serves as the first fragment. 6951e78ac21SJeffrey Hsu */ 6961e78ac21SJeffrey Hsu for (nfrags = 1; off < ip->ip_len; off += len, nfrags++) { 6971e78ac21SJeffrey Hsu struct ip *mhip; /* ip header on the fragment */ 6981e78ac21SJeffrey Hsu struct mbuf *m; 6991e78ac21SJeffrey Hsu int mhlen = sizeof (struct ip); 7001e78ac21SJeffrey Hsu 70134333b16SAndre Oppermann MGETHDR(m, M_DONTWAIT, MT_DATA); 7020b17fba7SAndre Oppermann if (m == NULL) { 7031e78ac21SJeffrey Hsu error = ENOBUFS; 7041e78ac21SJeffrey Hsu ipstat.ips_odropped++; 7051e78ac21SJeffrey Hsu goto done; 7061e78ac21SJeffrey Hsu } 7071e78ac21SJeffrey Hsu m->m_flags |= (m0->m_flags & M_MCAST) | M_FRAG; 7081e78ac21SJeffrey Hsu /* 7091e78ac21SJeffrey Hsu * In the first mbuf, leave room for the link header, then 7101e78ac21SJeffrey Hsu * copy the original IP header including options. The payload 7111e78ac21SJeffrey Hsu * goes into an additional mbuf chain returned by m_copy(). 7121e78ac21SJeffrey Hsu */ 7131e78ac21SJeffrey Hsu m->m_data += max_linkhdr; 7141e78ac21SJeffrey Hsu mhip = mtod(m, struct ip *); 7151e78ac21SJeffrey Hsu *mhip = *ip; 7161e78ac21SJeffrey Hsu if (hlen > sizeof (struct ip)) { 7171e78ac21SJeffrey Hsu mhlen = ip_optcopy(ip, mhip) + sizeof (struct ip); 7181e78ac21SJeffrey Hsu mhip->ip_v = IPVERSION; 7191e78ac21SJeffrey Hsu mhip->ip_hl = mhlen >> 2; 7201e78ac21SJeffrey Hsu } 7211e78ac21SJeffrey Hsu m->m_len = mhlen; 7221e78ac21SJeffrey Hsu /* XXX do we need to add ip->ip_off below ? */ 7231e78ac21SJeffrey Hsu mhip->ip_off = ((off - hlen) >> 3) + ip->ip_off; 7241e78ac21SJeffrey Hsu if (off + len >= ip->ip_len) { /* last fragment */ 7251e78ac21SJeffrey Hsu len = ip->ip_len - off; 7261e78ac21SJeffrey Hsu m->m_flags |= M_LASTFRAG; 7271e78ac21SJeffrey Hsu } else 7281e78ac21SJeffrey Hsu mhip->ip_off |= IP_MF; 7291e78ac21SJeffrey Hsu mhip->ip_len = htons((u_short)(len + mhlen)); 7301e78ac21SJeffrey Hsu m->m_next = m_copy(m0, off, len); 7310b17fba7SAndre Oppermann if (m->m_next == NULL) { /* copy failed */ 7321e78ac21SJeffrey Hsu m_free(m); 7331e78ac21SJeffrey Hsu error = ENOBUFS; /* ??? */ 7341e78ac21SJeffrey Hsu ipstat.ips_odropped++; 7351e78ac21SJeffrey Hsu goto done; 7361e78ac21SJeffrey Hsu } 7371e78ac21SJeffrey Hsu m->m_pkthdr.len = mhlen + len; 738fc74a9f9SBrooks Davis m->m_pkthdr.rcvif = NULL; 7391e78ac21SJeffrey Hsu #ifdef MAC 7401e78ac21SJeffrey Hsu mac_create_fragment(m0, m); 7411e78ac21SJeffrey Hsu #endif 7421e78ac21SJeffrey Hsu m->m_pkthdr.csum_flags = m0->m_pkthdr.csum_flags; 7431e78ac21SJeffrey Hsu mhip->ip_off = htons(mhip->ip_off); 7441e78ac21SJeffrey Hsu mhip->ip_sum = 0; 7451e78ac21SJeffrey Hsu if (sw_csum & CSUM_DELAY_IP) 7461e78ac21SJeffrey Hsu mhip->ip_sum = in_cksum(m, mhlen); 7471e78ac21SJeffrey Hsu *mnext = m; 7481e78ac21SJeffrey Hsu mnext = &m->m_nextpkt; 7491e78ac21SJeffrey Hsu } 7501e78ac21SJeffrey Hsu ipstat.ips_ofragments += nfrags; 7511e78ac21SJeffrey Hsu 7521e78ac21SJeffrey Hsu /* set first marker for fragment chain */ 7531e78ac21SJeffrey Hsu m0->m_flags |= M_FIRSTFRAG | M_FRAG; 7541e78ac21SJeffrey Hsu m0->m_pkthdr.csum_data = nfrags; 7551e78ac21SJeffrey Hsu 7561e78ac21SJeffrey Hsu /* 7571e78ac21SJeffrey Hsu * Update first fragment by trimming what's been copied out 7581e78ac21SJeffrey Hsu * and updating header. 7591e78ac21SJeffrey Hsu */ 7601e78ac21SJeffrey Hsu m_adj(m0, hlen + firstlen - ip->ip_len); 7611e78ac21SJeffrey Hsu m0->m_pkthdr.len = hlen + firstlen; 7621e78ac21SJeffrey Hsu ip->ip_len = htons((u_short)m0->m_pkthdr.len); 7631e78ac21SJeffrey Hsu ip->ip_off |= IP_MF; 7641e78ac21SJeffrey Hsu ip->ip_off = htons(ip->ip_off); 7651e78ac21SJeffrey Hsu ip->ip_sum = 0; 7661e78ac21SJeffrey Hsu if (sw_csum & CSUM_DELAY_IP) 7671e78ac21SJeffrey Hsu ip->ip_sum = in_cksum(m0, hlen); 7681e78ac21SJeffrey Hsu 7691e78ac21SJeffrey Hsu done: 7701e78ac21SJeffrey Hsu *m_frag = m0; 7711e78ac21SJeffrey Hsu return error; 7721e78ac21SJeffrey Hsu } 7731e78ac21SJeffrey Hsu 7741c238475SJonathan Lemon void 775db4f9cc7SJonathan Lemon in_delayed_cksum(struct mbuf *m) 776db4f9cc7SJonathan Lemon { 777db4f9cc7SJonathan Lemon struct ip *ip; 778db4f9cc7SJonathan Lemon u_short csum, offset; 779db4f9cc7SJonathan Lemon 780db4f9cc7SJonathan Lemon ip = mtod(m, struct ip *); 78153be11f6SPoul-Henning Kamp offset = ip->ip_hl << 2 ; 782db4f9cc7SJonathan Lemon csum = in_cksum_skip(m, ip->ip_len, offset); 783206a3274SRuslan Ermilov if (m->m_pkthdr.csum_flags & CSUM_UDP && csum == 0) 784206a3274SRuslan Ermilov csum = 0xffff; 785db4f9cc7SJonathan Lemon offset += m->m_pkthdr.csum_data; /* checksum offset */ 786db4f9cc7SJonathan Lemon 787db4f9cc7SJonathan Lemon if (offset + sizeof(u_short) > m->m_len) { 788db4f9cc7SJonathan Lemon printf("delayed m_pullup, m->len: %d off: %d p: %d\n", 789db4f9cc7SJonathan Lemon m->m_len, offset, ip->ip_p); 790db4f9cc7SJonathan Lemon /* 791db4f9cc7SJonathan Lemon * XXX 792db4f9cc7SJonathan Lemon * this shouldn't happen, but if it does, the 793db4f9cc7SJonathan Lemon * correct behavior may be to insert the checksum 7948f8d29f6SAndre Oppermann * in the appropriate next mbuf in the chain. 795db4f9cc7SJonathan Lemon */ 7968f8d29f6SAndre Oppermann return; 797db4f9cc7SJonathan Lemon } 798db4f9cc7SJonathan Lemon *(u_short *)(m->m_data + offset) = csum; 799db4f9cc7SJonathan Lemon } 800db4f9cc7SJonathan Lemon 801df8bae1dSRodney W. Grimes /* 802df8bae1dSRodney W. Grimes * IP socket option processing. 803df8bae1dSRodney W. Grimes */ 804df8bae1dSRodney W. Grimes int 805b375c9ecSLuigi Rizzo ip_ctloutput(so, sopt) 806b375c9ecSLuigi Rizzo struct socket *so; 807b375c9ecSLuigi Rizzo struct sockopt *sopt; 808df8bae1dSRodney W. Grimes { 809cfe8b629SGarrett Wollman struct inpcb *inp = sotoinpcb(so); 810cfe8b629SGarrett Wollman int error, optval; 811df8bae1dSRodney W. Grimes 812cfe8b629SGarrett Wollman error = optval = 0; 813cfe8b629SGarrett Wollman if (sopt->sopt_level != IPPROTO_IP) { 814cfe8b629SGarrett Wollman return (EINVAL); 815cfe8b629SGarrett Wollman } 816df8bae1dSRodney W. Grimes 817cfe8b629SGarrett Wollman switch (sopt->sopt_dir) { 818cfe8b629SGarrett Wollman case SOPT_SET: 819cfe8b629SGarrett Wollman switch (sopt->sopt_name) { 820df8bae1dSRodney W. Grimes case IP_OPTIONS: 821df8bae1dSRodney W. Grimes #ifdef notyet 822df8bae1dSRodney W. Grimes case IP_RETOPTS: 823df8bae1dSRodney W. Grimes #endif 824cfe8b629SGarrett Wollman { 825cfe8b629SGarrett Wollman struct mbuf *m; 826cfe8b629SGarrett Wollman if (sopt->sopt_valsize > MLEN) { 827cfe8b629SGarrett Wollman error = EMSGSIZE; 828cfe8b629SGarrett Wollman break; 829cfe8b629SGarrett Wollman } 830e0aec682SAndre Oppermann MGET(m, sopt->sopt_td ? M_TRYWAIT : M_DONTWAIT, MT_DATA); 8310b17fba7SAndre Oppermann if (m == NULL) { 832cfe8b629SGarrett Wollman error = ENOBUFS; 833cfe8b629SGarrett Wollman break; 834cfe8b629SGarrett Wollman } 835cfe8b629SGarrett Wollman m->m_len = sopt->sopt_valsize; 836cfe8b629SGarrett Wollman error = sooptcopyin(sopt, mtod(m, char *), m->m_len, 837cfe8b629SGarrett Wollman m->m_len); 838635354c4SMaxim Konovalov if (error) { 839635354c4SMaxim Konovalov m_free(m); 840635354c4SMaxim Konovalov break; 841635354c4SMaxim Konovalov } 842993d9505SRobert Watson INP_LOCK(inp); 843993d9505SRobert Watson error = ip_pcbopts(inp, sopt->sopt_name, m); 844993d9505SRobert Watson INP_UNLOCK(inp); 845993d9505SRobert Watson return (error); 846cfe8b629SGarrett Wollman } 847df8bae1dSRodney W. Grimes 848df8bae1dSRodney W. Grimes case IP_TOS: 849df8bae1dSRodney W. Grimes case IP_TTL: 850936cd18dSAndre Oppermann case IP_MINTTL: 851df8bae1dSRodney W. Grimes case IP_RECVOPTS: 852df8bae1dSRodney W. Grimes case IP_RECVRETOPTS: 853df8bae1dSRodney W. Grimes case IP_RECVDSTADDR: 8544957466bSMatthew N. Dodd case IP_RECVTTL: 85582c23ebaSBill Fenner case IP_RECVIF: 8566a800098SYoshinobu Inoue case IP_FAITH: 8578afa2304SBruce M Simpson case IP_ONESBCAST: 858b2828ad2SAndre Oppermann case IP_DONTFRAG: 859cfe8b629SGarrett Wollman error = sooptcopyin(sopt, &optval, sizeof optval, 860cfe8b629SGarrett Wollman sizeof optval); 861cfe8b629SGarrett Wollman if (error) 862cfe8b629SGarrett Wollman break; 863df8bae1dSRodney W. Grimes 864cfe8b629SGarrett Wollman switch (sopt->sopt_name) { 865df8bae1dSRodney W. Grimes case IP_TOS: 866ca98b82cSDavid Greenman inp->inp_ip_tos = optval; 867df8bae1dSRodney W. Grimes break; 868df8bae1dSRodney W. Grimes 869df8bae1dSRodney W. Grimes case IP_TTL: 870ca98b82cSDavid Greenman inp->inp_ip_ttl = optval; 871df8bae1dSRodney W. Grimes break; 872936cd18dSAndre Oppermann 873936cd18dSAndre Oppermann case IP_MINTTL: 874936cd18dSAndre Oppermann if (optval > 0 && optval <= MAXTTL) 875936cd18dSAndre Oppermann inp->inp_ip_minttl = optval; 876936cd18dSAndre Oppermann else 877936cd18dSAndre Oppermann error = EINVAL; 878936cd18dSAndre Oppermann break; 879936cd18dSAndre Oppermann 880a138d217SRobert Watson #define OPTSET(bit) do { \ 881a138d217SRobert Watson INP_LOCK(inp); \ 882df8bae1dSRodney W. Grimes if (optval) \ 883df8bae1dSRodney W. Grimes inp->inp_flags |= bit; \ 884df8bae1dSRodney W. Grimes else \ 885a138d217SRobert Watson inp->inp_flags &= ~bit; \ 886a138d217SRobert Watson INP_UNLOCK(inp); \ 887a138d217SRobert Watson } while (0) 888df8bae1dSRodney W. Grimes 889df8bae1dSRodney W. Grimes case IP_RECVOPTS: 890df8bae1dSRodney W. Grimes OPTSET(INP_RECVOPTS); 891df8bae1dSRodney W. Grimes break; 892df8bae1dSRodney W. Grimes 893df8bae1dSRodney W. Grimes case IP_RECVRETOPTS: 894df8bae1dSRodney W. Grimes OPTSET(INP_RECVRETOPTS); 895df8bae1dSRodney W. Grimes break; 896df8bae1dSRodney W. Grimes 897df8bae1dSRodney W. Grimes case IP_RECVDSTADDR: 898df8bae1dSRodney W. Grimes OPTSET(INP_RECVDSTADDR); 899df8bae1dSRodney W. Grimes break; 90082c23ebaSBill Fenner 9014957466bSMatthew N. Dodd case IP_RECVTTL: 9024957466bSMatthew N. Dodd OPTSET(INP_RECVTTL); 9034957466bSMatthew N. Dodd break; 9044957466bSMatthew N. Dodd 90582c23ebaSBill Fenner case IP_RECVIF: 90682c23ebaSBill Fenner OPTSET(INP_RECVIF); 90782c23ebaSBill Fenner break; 9086a800098SYoshinobu Inoue 9096a800098SYoshinobu Inoue case IP_FAITH: 9106a800098SYoshinobu Inoue OPTSET(INP_FAITH); 9116a800098SYoshinobu Inoue break; 9128afa2304SBruce M Simpson 9138afa2304SBruce M Simpson case IP_ONESBCAST: 9148afa2304SBruce M Simpson OPTSET(INP_ONESBCAST); 9158afa2304SBruce M Simpson break; 916b2828ad2SAndre Oppermann case IP_DONTFRAG: 917b2828ad2SAndre Oppermann OPTSET(INP_DONTFRAG); 918b2828ad2SAndre Oppermann break; 919df8bae1dSRodney W. Grimes } 920df8bae1dSRodney W. Grimes break; 921df8bae1dSRodney W. Grimes #undef OPTSET 922df8bae1dSRodney W. Grimes 923df8bae1dSRodney W. Grimes case IP_MULTICAST_IF: 924f0068c4aSGarrett Wollman case IP_MULTICAST_VIF: 925df8bae1dSRodney W. Grimes case IP_MULTICAST_TTL: 926df8bae1dSRodney W. Grimes case IP_MULTICAST_LOOP: 927df8bae1dSRodney W. Grimes case IP_ADD_MEMBERSHIP: 928df8bae1dSRodney W. Grimes case IP_DROP_MEMBERSHIP: 9295c918b56SRobert Watson error = ip_setmoptions(inp, sopt); 930df8bae1dSRodney W. Grimes break; 931df8bae1dSRodney W. Grimes 93233b3ac06SPeter Wemm case IP_PORTRANGE: 933cfe8b629SGarrett Wollman error = sooptcopyin(sopt, &optval, sizeof optval, 934cfe8b629SGarrett Wollman sizeof optval); 935cfe8b629SGarrett Wollman if (error) 936cfe8b629SGarrett Wollman break; 93733b3ac06SPeter Wemm 938a138d217SRobert Watson INP_LOCK(inp); 93933b3ac06SPeter Wemm switch (optval) { 94033b3ac06SPeter Wemm case IP_PORTRANGE_DEFAULT: 94133b3ac06SPeter Wemm inp->inp_flags &= ~(INP_LOWPORT); 94233b3ac06SPeter Wemm inp->inp_flags &= ~(INP_HIGHPORT); 94333b3ac06SPeter Wemm break; 94433b3ac06SPeter Wemm 94533b3ac06SPeter Wemm case IP_PORTRANGE_HIGH: 94633b3ac06SPeter Wemm inp->inp_flags &= ~(INP_LOWPORT); 94733b3ac06SPeter Wemm inp->inp_flags |= INP_HIGHPORT; 94833b3ac06SPeter Wemm break; 94933b3ac06SPeter Wemm 95033b3ac06SPeter Wemm case IP_PORTRANGE_LOW: 95133b3ac06SPeter Wemm inp->inp_flags &= ~(INP_HIGHPORT); 95233b3ac06SPeter Wemm inp->inp_flags |= INP_LOWPORT; 95333b3ac06SPeter Wemm break; 95433b3ac06SPeter Wemm 95533b3ac06SPeter Wemm default: 95633b3ac06SPeter Wemm error = EINVAL; 95733b3ac06SPeter Wemm break; 95833b3ac06SPeter Wemm } 959a138d217SRobert Watson INP_UNLOCK(inp); 960ce8c72b1SPeter Wemm break; 96133b3ac06SPeter Wemm 962b9234fafSSam Leffler #if defined(IPSEC) || defined(FAST_IPSEC) 9636a800098SYoshinobu Inoue case IP_IPSEC_POLICY: 9646a800098SYoshinobu Inoue { 9656a800098SYoshinobu Inoue caddr_t req; 966686cdd19SJun-ichiro itojun Hagino size_t len = 0; 9676a800098SYoshinobu Inoue int priv; 9686a800098SYoshinobu Inoue struct mbuf *m; 9696a800098SYoshinobu Inoue int optname; 9706a800098SYoshinobu Inoue 9716a800098SYoshinobu Inoue if ((error = soopt_getm(sopt, &m)) != 0) /* XXX */ 9726a800098SYoshinobu Inoue break; 9736a800098SYoshinobu Inoue if ((error = soopt_mcopyin(sopt, m)) != 0) /* XXX */ 9746a800098SYoshinobu Inoue break; 975b40ce416SJulian Elischer priv = (sopt->sopt_td != NULL && 97644731cabSJohn Baldwin suser(sopt->sopt_td) != 0) ? 0 : 1; 9776a800098SYoshinobu Inoue req = mtod(m, caddr_t); 978686cdd19SJun-ichiro itojun Hagino len = m->m_len; 9796a800098SYoshinobu Inoue optname = sopt->sopt_name; 980686cdd19SJun-ichiro itojun Hagino error = ipsec4_set_policy(inp, optname, req, len, priv); 9816a800098SYoshinobu Inoue m_freem(m); 9826a800098SYoshinobu Inoue break; 9836a800098SYoshinobu Inoue } 9846a800098SYoshinobu Inoue #endif /*IPSEC*/ 9856a800098SYoshinobu Inoue 986df8bae1dSRodney W. Grimes default: 987df8bae1dSRodney W. Grimes error = ENOPROTOOPT; 988df8bae1dSRodney W. Grimes break; 989df8bae1dSRodney W. Grimes } 990df8bae1dSRodney W. Grimes break; 991df8bae1dSRodney W. Grimes 992cfe8b629SGarrett Wollman case SOPT_GET: 993cfe8b629SGarrett Wollman switch (sopt->sopt_name) { 994df8bae1dSRodney W. Grimes case IP_OPTIONS: 995df8bae1dSRodney W. Grimes case IP_RETOPTS: 996cfe8b629SGarrett Wollman if (inp->inp_options) 997cfe8b629SGarrett Wollman error = sooptcopyout(sopt, 998cfe8b629SGarrett Wollman mtod(inp->inp_options, 999cfe8b629SGarrett Wollman char *), 1000cfe8b629SGarrett Wollman inp->inp_options->m_len); 1001cfe8b629SGarrett Wollman else 1002cfe8b629SGarrett Wollman sopt->sopt_valsize = 0; 1003df8bae1dSRodney W. Grimes break; 1004df8bae1dSRodney W. Grimes 1005df8bae1dSRodney W. Grimes case IP_TOS: 1006df8bae1dSRodney W. Grimes case IP_TTL: 1007936cd18dSAndre Oppermann case IP_MINTTL: 1008df8bae1dSRodney W. Grimes case IP_RECVOPTS: 1009df8bae1dSRodney W. Grimes case IP_RECVRETOPTS: 1010df8bae1dSRodney W. Grimes case IP_RECVDSTADDR: 10114957466bSMatthew N. Dodd case IP_RECVTTL: 101282c23ebaSBill Fenner case IP_RECVIF: 1013cfe8b629SGarrett Wollman case IP_PORTRANGE: 10146a800098SYoshinobu Inoue case IP_FAITH: 10158afa2304SBruce M Simpson case IP_ONESBCAST: 1016b2828ad2SAndre Oppermann case IP_DONTFRAG: 1017cfe8b629SGarrett Wollman switch (sopt->sopt_name) { 1018df8bae1dSRodney W. Grimes 1019df8bae1dSRodney W. Grimes case IP_TOS: 1020ca98b82cSDavid Greenman optval = inp->inp_ip_tos; 1021df8bae1dSRodney W. Grimes break; 1022df8bae1dSRodney W. Grimes 1023df8bae1dSRodney W. Grimes case IP_TTL: 1024ca98b82cSDavid Greenman optval = inp->inp_ip_ttl; 1025df8bae1dSRodney W. Grimes break; 1026df8bae1dSRodney W. Grimes 1027936cd18dSAndre Oppermann case IP_MINTTL: 1028936cd18dSAndre Oppermann optval = inp->inp_ip_minttl; 1029936cd18dSAndre Oppermann break; 1030936cd18dSAndre Oppermann 1031df8bae1dSRodney W. Grimes #define OPTBIT(bit) (inp->inp_flags & bit ? 1 : 0) 1032df8bae1dSRodney W. Grimes 1033df8bae1dSRodney W. Grimes case IP_RECVOPTS: 1034df8bae1dSRodney W. Grimes optval = OPTBIT(INP_RECVOPTS); 1035df8bae1dSRodney W. Grimes break; 1036df8bae1dSRodney W. Grimes 1037df8bae1dSRodney W. Grimes case IP_RECVRETOPTS: 1038df8bae1dSRodney W. Grimes optval = OPTBIT(INP_RECVRETOPTS); 1039df8bae1dSRodney W. Grimes break; 1040df8bae1dSRodney W. Grimes 1041df8bae1dSRodney W. Grimes case IP_RECVDSTADDR: 1042df8bae1dSRodney W. Grimes optval = OPTBIT(INP_RECVDSTADDR); 1043df8bae1dSRodney W. Grimes break; 104482c23ebaSBill Fenner 10454957466bSMatthew N. Dodd case IP_RECVTTL: 10464957466bSMatthew N. Dodd optval = OPTBIT(INP_RECVTTL); 10474957466bSMatthew N. Dodd break; 10484957466bSMatthew N. Dodd 104982c23ebaSBill Fenner case IP_RECVIF: 105082c23ebaSBill Fenner optval = OPTBIT(INP_RECVIF); 105182c23ebaSBill Fenner break; 1052cfe8b629SGarrett Wollman 1053cfe8b629SGarrett Wollman case IP_PORTRANGE: 1054cfe8b629SGarrett Wollman if (inp->inp_flags & INP_HIGHPORT) 1055cfe8b629SGarrett Wollman optval = IP_PORTRANGE_HIGH; 1056cfe8b629SGarrett Wollman else if (inp->inp_flags & INP_LOWPORT) 1057cfe8b629SGarrett Wollman optval = IP_PORTRANGE_LOW; 1058cfe8b629SGarrett Wollman else 1059cfe8b629SGarrett Wollman optval = 0; 1060cfe8b629SGarrett Wollman break; 10616a800098SYoshinobu Inoue 10626a800098SYoshinobu Inoue case IP_FAITH: 10636a800098SYoshinobu Inoue optval = OPTBIT(INP_FAITH); 10646a800098SYoshinobu Inoue break; 10658afa2304SBruce M Simpson 10668afa2304SBruce M Simpson case IP_ONESBCAST: 10678afa2304SBruce M Simpson optval = OPTBIT(INP_ONESBCAST); 10688afa2304SBruce M Simpson break; 1069b2828ad2SAndre Oppermann case IP_DONTFRAG: 1070b2828ad2SAndre Oppermann optval = OPTBIT(INP_DONTFRAG); 1071b2828ad2SAndre Oppermann break; 1072df8bae1dSRodney W. Grimes } 1073cfe8b629SGarrett Wollman error = sooptcopyout(sopt, &optval, sizeof optval); 1074df8bae1dSRodney W. Grimes break; 1075df8bae1dSRodney W. Grimes 1076df8bae1dSRodney W. Grimes case IP_MULTICAST_IF: 1077f0068c4aSGarrett Wollman case IP_MULTICAST_VIF: 1078df8bae1dSRodney W. Grimes case IP_MULTICAST_TTL: 1079df8bae1dSRodney W. Grimes case IP_MULTICAST_LOOP: 1080df8bae1dSRodney W. Grimes case IP_ADD_MEMBERSHIP: 1081df8bae1dSRodney W. Grimes case IP_DROP_MEMBERSHIP: 108289924e58SRobert Watson error = ip_getmoptions(inp, sopt); 108333b3ac06SPeter Wemm break; 108433b3ac06SPeter Wemm 1085b9234fafSSam Leffler #if defined(IPSEC) || defined(FAST_IPSEC) 10866a800098SYoshinobu Inoue case IP_IPSEC_POLICY: 10876a800098SYoshinobu Inoue { 1088f63e7634SYoshinobu Inoue struct mbuf *m = NULL; 10896a800098SYoshinobu Inoue caddr_t req = NULL; 1090686cdd19SJun-ichiro itojun Hagino size_t len = 0; 10916a800098SYoshinobu Inoue 1092686cdd19SJun-ichiro itojun Hagino if (m != 0) { 10936a800098SYoshinobu Inoue req = mtod(m, caddr_t); 1094686cdd19SJun-ichiro itojun Hagino len = m->m_len; 1095686cdd19SJun-ichiro itojun Hagino } 1096686cdd19SJun-ichiro itojun Hagino error = ipsec4_get_policy(sotoinpcb(so), req, len, &m); 10976a800098SYoshinobu Inoue if (error == 0) 10986a800098SYoshinobu Inoue error = soopt_mcopyout(sopt, m); /* XXX */ 1099f63e7634SYoshinobu Inoue if (error == 0) 11006a800098SYoshinobu Inoue m_freem(m); 11016a800098SYoshinobu Inoue break; 11026a800098SYoshinobu Inoue } 11036a800098SYoshinobu Inoue #endif /*IPSEC*/ 11046a800098SYoshinobu Inoue 1105df8bae1dSRodney W. Grimes default: 1106df8bae1dSRodney W. Grimes error = ENOPROTOOPT; 1107df8bae1dSRodney W. Grimes break; 1108df8bae1dSRodney W. Grimes } 1109df8bae1dSRodney W. Grimes break; 1110df8bae1dSRodney W. Grimes } 1111df8bae1dSRodney W. Grimes return (error); 1112df8bae1dSRodney W. Grimes } 1113df8bae1dSRodney W. Grimes 1114df8bae1dSRodney W. Grimes /* 1115cfe8b629SGarrett Wollman * XXX 1116cfe8b629SGarrett Wollman * The whole multicast option thing needs to be re-thought. 1117cfe8b629SGarrett Wollman * Several of these options are equally applicable to non-multicast 1118cfe8b629SGarrett Wollman * transmission, and one (IP_MULTICAST_TTL) totally duplicates a 1119cfe8b629SGarrett Wollman * standard option (IP_TTL). 1120cfe8b629SGarrett Wollman */ 112133841545SHajimu UMEMOTO 112233841545SHajimu UMEMOTO /* 112333841545SHajimu UMEMOTO * following RFC1724 section 3.3, 0.0.0.0/8 is interpreted as interface index. 112433841545SHajimu UMEMOTO */ 112533841545SHajimu UMEMOTO static struct ifnet * 1126b375c9ecSLuigi Rizzo ip_multicast_if(a, ifindexp) 1127b375c9ecSLuigi Rizzo struct in_addr *a; 1128b375c9ecSLuigi Rizzo int *ifindexp; 112933841545SHajimu UMEMOTO { 113033841545SHajimu UMEMOTO int ifindex; 113133841545SHajimu UMEMOTO struct ifnet *ifp; 113233841545SHajimu UMEMOTO 113333841545SHajimu UMEMOTO if (ifindexp) 113433841545SHajimu UMEMOTO *ifindexp = 0; 113533841545SHajimu UMEMOTO if (ntohl(a->s_addr) >> 24 == 0) { 113633841545SHajimu UMEMOTO ifindex = ntohl(a->s_addr) & 0xffffff; 113733841545SHajimu UMEMOTO if (ifindex < 0 || if_index < ifindex) 113833841545SHajimu UMEMOTO return NULL; 1139f9132cebSJonathan Lemon ifp = ifnet_byindex(ifindex); 114033841545SHajimu UMEMOTO if (ifindexp) 114133841545SHajimu UMEMOTO *ifindexp = ifindex; 114233841545SHajimu UMEMOTO } else { 114333841545SHajimu UMEMOTO INADDR_TO_IFP(*a, ifp); 114433841545SHajimu UMEMOTO } 114533841545SHajimu UMEMOTO return ifp; 114633841545SHajimu UMEMOTO } 114733841545SHajimu UMEMOTO 1148cfe8b629SGarrett Wollman /* 1149a2dc1f50SRobert Watson * Given an inpcb, return its multicast options structure pointer. Accepts 1150a2dc1f50SRobert Watson * an unlocked inpcb pointer, but will return it locked. May sleep. 1151a2dc1f50SRobert Watson */ 1152a2dc1f50SRobert Watson static struct ip_moptions * 1153a2dc1f50SRobert Watson ip_findmoptions(struct inpcb *inp) 1154a2dc1f50SRobert Watson { 1155a2dc1f50SRobert Watson struct ip_moptions *imo; 11563548bfc9SBruce M Simpson struct in_multi **immp; 1157a2dc1f50SRobert Watson 1158a2dc1f50SRobert Watson INP_LOCK(inp); 1159a2dc1f50SRobert Watson if (inp->inp_moptions != NULL) 1160a2dc1f50SRobert Watson return (inp->inp_moptions); 1161a2dc1f50SRobert Watson 1162a2dc1f50SRobert Watson INP_UNLOCK(inp); 1163a2dc1f50SRobert Watson 1164a2dc1f50SRobert Watson imo = (struct ip_moptions*)malloc(sizeof(*imo), M_IPMOPTS, M_WAITOK); 11653548bfc9SBruce M Simpson immp = (struct in_multi **)malloc((sizeof(*immp) * IP_MIN_MEMBERSHIPS), 11663548bfc9SBruce M Simpson M_IPMOPTS, M_WAITOK); 1167a2dc1f50SRobert Watson 1168a2dc1f50SRobert Watson imo->imo_multicast_ifp = NULL; 1169a2dc1f50SRobert Watson imo->imo_multicast_addr.s_addr = INADDR_ANY; 1170a2dc1f50SRobert Watson imo->imo_multicast_vif = -1; 1171a2dc1f50SRobert Watson imo->imo_multicast_ttl = IP_DEFAULT_MULTICAST_TTL; 1172a2dc1f50SRobert Watson imo->imo_multicast_loop = IP_DEFAULT_MULTICAST_LOOP; 1173a2dc1f50SRobert Watson imo->imo_num_memberships = 0; 11743548bfc9SBruce M Simpson imo->imo_max_memberships = IP_MIN_MEMBERSHIPS; 11753548bfc9SBruce M Simpson imo->imo_membership = immp; 1176a2dc1f50SRobert Watson 1177a2dc1f50SRobert Watson INP_LOCK(inp); 1178a2dc1f50SRobert Watson if (inp->inp_moptions != NULL) { 11793548bfc9SBruce M Simpson free(immp, M_IPMOPTS); 1180a2dc1f50SRobert Watson free(imo, M_IPMOPTS); 1181a2dc1f50SRobert Watson return (inp->inp_moptions); 1182a2dc1f50SRobert Watson } 1183a2dc1f50SRobert Watson inp->inp_moptions = imo; 1184a2dc1f50SRobert Watson return (imo); 1185a2dc1f50SRobert Watson } 1186a2dc1f50SRobert Watson 1187a2dc1f50SRobert Watson /* 1188df8bae1dSRodney W. Grimes * Set the IP multicast options in response to user setsockopt(). 1189df8bae1dSRodney W. Grimes */ 11900312fbe9SPoul-Henning Kamp static int 11915c918b56SRobert Watson ip_setmoptions(struct inpcb *inp, struct sockopt *sopt) 1192df8bae1dSRodney W. Grimes { 1193cfe8b629SGarrett Wollman int error = 0; 1194cfe8b629SGarrett Wollman int i; 1195df8bae1dSRodney W. Grimes struct in_addr addr; 1196cfe8b629SGarrett Wollman struct ip_mreq mreq; 1197cfe8b629SGarrett Wollman struct ifnet *ifp; 11985c918b56SRobert Watson struct ip_moptions *imo; 1199df8bae1dSRodney W. Grimes struct route ro; 1200cfe8b629SGarrett Wollman struct sockaddr_in *dst; 120133841545SHajimu UMEMOTO int ifindex; 12029b626c29SGarrett Wollman int s; 1203df8bae1dSRodney W. Grimes 1204cfe8b629SGarrett Wollman switch (sopt->sopt_name) { 1205f0068c4aSGarrett Wollman /* store an index number for the vif you wanna use in the send */ 1206f0068c4aSGarrett Wollman case IP_MULTICAST_VIF: 1207cfe8b629SGarrett Wollman if (legal_vif_num == 0) { 12085e9ae478SGarrett Wollman error = EOPNOTSUPP; 12095e9ae478SGarrett Wollman break; 12105e9ae478SGarrett Wollman } 1211cfe8b629SGarrett Wollman error = sooptcopyin(sopt, &i, sizeof i, sizeof i); 1212cfe8b629SGarrett Wollman if (error) 1213f0068c4aSGarrett Wollman break; 12141c5de19aSGarrett Wollman if (!legal_vif_num(i) && (i != -1)) { 1215f0068c4aSGarrett Wollman error = EINVAL; 1216f0068c4aSGarrett Wollman break; 1217f0068c4aSGarrett Wollman } 1218a2dc1f50SRobert Watson imo = ip_findmoptions(inp); 1219f0068c4aSGarrett Wollman imo->imo_multicast_vif = i; 1220a2dc1f50SRobert Watson INP_UNLOCK(inp); 1221f0068c4aSGarrett Wollman break; 1222f0068c4aSGarrett Wollman 1223df8bae1dSRodney W. Grimes case IP_MULTICAST_IF: 1224df8bae1dSRodney W. Grimes /* 1225df8bae1dSRodney W. Grimes * Select the interface for outgoing multicast packets. 1226df8bae1dSRodney W. Grimes */ 1227cfe8b629SGarrett Wollman error = sooptcopyin(sopt, &addr, sizeof addr, sizeof addr); 1228cfe8b629SGarrett Wollman if (error) 1229df8bae1dSRodney W. Grimes break; 1230df8bae1dSRodney W. Grimes /* 1231df8bae1dSRodney W. Grimes * INADDR_ANY is used to remove a previous selection. 1232df8bae1dSRodney W. Grimes * When no interface is selected, a default one is 1233df8bae1dSRodney W. Grimes * chosen every time a multicast packet is sent. 1234df8bae1dSRodney W. Grimes */ 1235a2dc1f50SRobert Watson imo = ip_findmoptions(inp); 1236df8bae1dSRodney W. Grimes if (addr.s_addr == INADDR_ANY) { 1237df8bae1dSRodney W. Grimes imo->imo_multicast_ifp = NULL; 1238a2dc1f50SRobert Watson INP_UNLOCK(inp); 1239df8bae1dSRodney W. Grimes break; 1240df8bae1dSRodney W. Grimes } 1241df8bae1dSRodney W. Grimes /* 1242df8bae1dSRodney W. Grimes * The selected interface is identified by its local 1243df8bae1dSRodney W. Grimes * IP address. Find the interface and confirm that 1244df8bae1dSRodney W. Grimes * it supports multicasting. 1245df8bae1dSRodney W. Grimes */ 124620e8807cSGarrett Wollman s = splimp(); 124733841545SHajimu UMEMOTO ifp = ip_multicast_if(&addr, &ifindex); 1248df8bae1dSRodney W. Grimes if (ifp == NULL || (ifp->if_flags & IFF_MULTICAST) == 0) { 1249a2dc1f50SRobert Watson INP_UNLOCK(inp); 1250fbc6ab00SBill Fenner splx(s); 1251df8bae1dSRodney W. Grimes error = EADDRNOTAVAIL; 1252df8bae1dSRodney W. Grimes break; 1253df8bae1dSRodney W. Grimes } 1254df8bae1dSRodney W. Grimes imo->imo_multicast_ifp = ifp; 125533841545SHajimu UMEMOTO if (ifindex) 125633841545SHajimu UMEMOTO imo->imo_multicast_addr = addr; 125733841545SHajimu UMEMOTO else 125833841545SHajimu UMEMOTO imo->imo_multicast_addr.s_addr = INADDR_ANY; 1259a2dc1f50SRobert Watson INP_UNLOCK(inp); 12609b626c29SGarrett Wollman splx(s); 1261df8bae1dSRodney W. Grimes break; 1262df8bae1dSRodney W. Grimes 1263df8bae1dSRodney W. Grimes case IP_MULTICAST_TTL: 1264df8bae1dSRodney W. Grimes /* 1265df8bae1dSRodney W. Grimes * Set the IP time-to-live for outgoing multicast packets. 1266cfe8b629SGarrett Wollman * The original multicast API required a char argument, 1267cfe8b629SGarrett Wollman * which is inconsistent with the rest of the socket API. 1268cfe8b629SGarrett Wollman * We allow either a char or an int. 1269df8bae1dSRodney W. Grimes */ 1270cfe8b629SGarrett Wollman if (sopt->sopt_valsize == 1) { 1271cfe8b629SGarrett Wollman u_char ttl; 1272cfe8b629SGarrett Wollman error = sooptcopyin(sopt, &ttl, 1, 1); 1273cfe8b629SGarrett Wollman if (error) 1274df8bae1dSRodney W. Grimes break; 1275a2dc1f50SRobert Watson imo = ip_findmoptions(inp); 1276cfe8b629SGarrett Wollman imo->imo_multicast_ttl = ttl; 1277a2dc1f50SRobert Watson INP_UNLOCK(inp); 1278cfe8b629SGarrett Wollman } else { 1279cfe8b629SGarrett Wollman u_int ttl; 1280cfe8b629SGarrett Wollman error = sooptcopyin(sopt, &ttl, sizeof ttl, 1281cfe8b629SGarrett Wollman sizeof ttl); 1282cfe8b629SGarrett Wollman if (error) 1283cfe8b629SGarrett Wollman break; 1284cfe8b629SGarrett Wollman if (ttl > 255) 1285cfe8b629SGarrett Wollman error = EINVAL; 1286a2dc1f50SRobert Watson else { 1287a2dc1f50SRobert Watson imo = ip_findmoptions(inp); 1288cfe8b629SGarrett Wollman imo->imo_multicast_ttl = ttl; 1289a2dc1f50SRobert Watson INP_UNLOCK(inp); 1290a2dc1f50SRobert Watson } 1291df8bae1dSRodney W. Grimes } 1292df8bae1dSRodney W. Grimes break; 1293df8bae1dSRodney W. Grimes 1294df8bae1dSRodney W. Grimes case IP_MULTICAST_LOOP: 1295df8bae1dSRodney W. Grimes /* 1296df8bae1dSRodney W. Grimes * Set the loopback flag for outgoing multicast packets. 1297cfe8b629SGarrett Wollman * Must be zero or one. The original multicast API required a 1298cfe8b629SGarrett Wollman * char argument, which is inconsistent with the rest 1299cfe8b629SGarrett Wollman * of the socket API. We allow either a char or an int. 1300df8bae1dSRodney W. Grimes */ 1301cfe8b629SGarrett Wollman if (sopt->sopt_valsize == 1) { 1302cfe8b629SGarrett Wollman u_char loop; 1303cfe8b629SGarrett Wollman error = sooptcopyin(sopt, &loop, 1, 1); 1304cfe8b629SGarrett Wollman if (error) 1305df8bae1dSRodney W. Grimes break; 1306a2dc1f50SRobert Watson imo = ip_findmoptions(inp); 1307cfe8b629SGarrett Wollman imo->imo_multicast_loop = !!loop; 1308a2dc1f50SRobert Watson INP_UNLOCK(inp); 1309cfe8b629SGarrett Wollman } else { 1310cfe8b629SGarrett Wollman u_int loop; 1311cfe8b629SGarrett Wollman error = sooptcopyin(sopt, &loop, sizeof loop, 1312cfe8b629SGarrett Wollman sizeof loop); 1313cfe8b629SGarrett Wollman if (error) 1314cfe8b629SGarrett Wollman break; 1315a2dc1f50SRobert Watson imo = ip_findmoptions(inp); 1316cfe8b629SGarrett Wollman imo->imo_multicast_loop = !!loop; 1317a2dc1f50SRobert Watson INP_UNLOCK(inp); 1318df8bae1dSRodney W. Grimes } 1319df8bae1dSRodney W. Grimes break; 1320df8bae1dSRodney W. Grimes 1321df8bae1dSRodney W. Grimes case IP_ADD_MEMBERSHIP: 1322df8bae1dSRodney W. Grimes /* 1323df8bae1dSRodney W. Grimes * Add a multicast group membership. 1324df8bae1dSRodney W. Grimes * Group must be a valid IP multicast address. 1325df8bae1dSRodney W. Grimes */ 1326cfe8b629SGarrett Wollman error = sooptcopyin(sopt, &mreq, sizeof mreq, sizeof mreq); 1327cfe8b629SGarrett Wollman if (error) 1328df8bae1dSRodney W. Grimes break; 1329cfe8b629SGarrett Wollman 1330cfe8b629SGarrett Wollman if (!IN_MULTICAST(ntohl(mreq.imr_multiaddr.s_addr))) { 1331df8bae1dSRodney W. Grimes error = EINVAL; 1332df8bae1dSRodney W. Grimes break; 1333df8bae1dSRodney W. Grimes } 133420e8807cSGarrett Wollman s = splimp(); 1335df8bae1dSRodney W. Grimes /* 1336df8bae1dSRodney W. Grimes * If no interface address was provided, use the interface of 1337df8bae1dSRodney W. Grimes * the route to the given multicast address. 1338df8bae1dSRodney W. Grimes */ 1339cfe8b629SGarrett Wollman if (mreq.imr_interface.s_addr == INADDR_ANY) { 13401c5de19aSGarrett Wollman bzero((caddr_t)&ro, sizeof(ro)); 1341df8bae1dSRodney W. Grimes dst = (struct sockaddr_in *)&ro.ro_dst; 1342df8bae1dSRodney W. Grimes dst->sin_len = sizeof(*dst); 1343df8bae1dSRodney W. Grimes dst->sin_family = AF_INET; 1344cfe8b629SGarrett Wollman dst->sin_addr = mreq.imr_multiaddr; 134597d8d152SAndre Oppermann rtalloc_ign(&ro, RTF_CLONING); 1346df8bae1dSRodney W. Grimes if (ro.ro_rt == NULL) { 1347df8bae1dSRodney W. Grimes error = EADDRNOTAVAIL; 13489b626c29SGarrett Wollman splx(s); 1349df8bae1dSRodney W. Grimes break; 1350df8bae1dSRodney W. Grimes } 1351df8bae1dSRodney W. Grimes ifp = ro.ro_rt->rt_ifp; 1352d1dd20beSSam Leffler RTFREE(ro.ro_rt); 1353df8bae1dSRodney W. Grimes } 1354df8bae1dSRodney W. Grimes else { 135533841545SHajimu UMEMOTO ifp = ip_multicast_if(&mreq.imr_interface, NULL); 1356df8bae1dSRodney W. Grimes } 13579b626c29SGarrett Wollman 1358df8bae1dSRodney W. Grimes /* 1359df8bae1dSRodney W. Grimes * See if we found an interface, and confirm that it 1360df8bae1dSRodney W. Grimes * supports multicast. 1361df8bae1dSRodney W. Grimes */ 1362df8bae1dSRodney W. Grimes if (ifp == NULL || (ifp->if_flags & IFF_MULTICAST) == 0) { 1363df8bae1dSRodney W. Grimes error = EADDRNOTAVAIL; 13649b626c29SGarrett Wollman splx(s); 1365df8bae1dSRodney W. Grimes break; 1366df8bae1dSRodney W. Grimes } 1367df8bae1dSRodney W. Grimes /* 1368df8bae1dSRodney W. Grimes * See if the membership already exists or if all the 1369df8bae1dSRodney W. Grimes * membership slots are full. 1370df8bae1dSRodney W. Grimes */ 1371a2dc1f50SRobert Watson imo = ip_findmoptions(inp); 1372df8bae1dSRodney W. Grimes for (i = 0; i < imo->imo_num_memberships; ++i) { 1373df8bae1dSRodney W. Grimes if (imo->imo_membership[i]->inm_ifp == ifp && 1374df8bae1dSRodney W. Grimes imo->imo_membership[i]->inm_addr.s_addr 1375cfe8b629SGarrett Wollman == mreq.imr_multiaddr.s_addr) 1376df8bae1dSRodney W. Grimes break; 1377df8bae1dSRodney W. Grimes } 1378df8bae1dSRodney W. Grimes if (i < imo->imo_num_memberships) { 1379a2dc1f50SRobert Watson INP_UNLOCK(inp); 1380df8bae1dSRodney W. Grimes error = EADDRINUSE; 13819b626c29SGarrett Wollman splx(s); 1382df8bae1dSRodney W. Grimes break; 1383df8bae1dSRodney W. Grimes } 13843548bfc9SBruce M Simpson if (imo->imo_num_memberships == imo->imo_max_memberships) { 13853548bfc9SBruce M Simpson struct in_multi **nmships, **omships; 13863548bfc9SBruce M Simpson size_t newmax; 13873548bfc9SBruce M Simpson /* 13883548bfc9SBruce M Simpson * Resize the vector to next power-of-two minus 1. If the 13893548bfc9SBruce M Simpson * size would exceed the maximum then we know we've really 13903548bfc9SBruce M Simpson * run out of entries. Otherwise, we realloc() the vector 13913548bfc9SBruce M Simpson * with the INP lock held to avoid introducing a race. 13923548bfc9SBruce M Simpson */ 13933548bfc9SBruce M Simpson nmships = NULL; 13943548bfc9SBruce M Simpson omships = imo->imo_membership; 13953548bfc9SBruce M Simpson newmax = ((imo->imo_max_memberships + 1) * 2) - 1; 13963548bfc9SBruce M Simpson if (newmax <= IP_MAX_MEMBERSHIPS) { 13973548bfc9SBruce M Simpson nmships = (struct in_multi **)realloc(omships, 13983548bfc9SBruce M Simpson sizeof(*nmships) * newmax, M_IPMOPTS, M_NOWAIT); 13993548bfc9SBruce M Simpson if (nmships != NULL) { 14003548bfc9SBruce M Simpson imo->imo_membership = nmships; 14013548bfc9SBruce M Simpson imo->imo_max_memberships = newmax; 14023548bfc9SBruce M Simpson } 14033548bfc9SBruce M Simpson } 14043548bfc9SBruce M Simpson if (nmships == NULL) { 1405a2dc1f50SRobert Watson INP_UNLOCK(inp); 1406df8bae1dSRodney W. Grimes error = ETOOMANYREFS; 14079b626c29SGarrett Wollman splx(s); 1408df8bae1dSRodney W. Grimes break; 1409df8bae1dSRodney W. Grimes } 14103548bfc9SBruce M Simpson } 1411df8bae1dSRodney W. Grimes /* 1412df8bae1dSRodney W. Grimes * Everything looks good; add a new record to the multicast 1413df8bae1dSRodney W. Grimes * address list for the given interface. 1414df8bae1dSRodney W. Grimes */ 1415df8bae1dSRodney W. Grimes if ((imo->imo_membership[i] = 1416cfe8b629SGarrett Wollman in_addmulti(&mreq.imr_multiaddr, ifp)) == NULL) { 1417a2dc1f50SRobert Watson INP_UNLOCK(inp); 1418df8bae1dSRodney W. Grimes error = ENOBUFS; 14199b626c29SGarrett Wollman splx(s); 1420df8bae1dSRodney W. Grimes break; 1421df8bae1dSRodney W. Grimes } 1422df8bae1dSRodney W. Grimes ++imo->imo_num_memberships; 1423a2dc1f50SRobert Watson INP_UNLOCK(inp); 14249b626c29SGarrett Wollman splx(s); 1425df8bae1dSRodney W. Grimes break; 1426df8bae1dSRodney W. Grimes 1427df8bae1dSRodney W. Grimes case IP_DROP_MEMBERSHIP: 1428df8bae1dSRodney W. Grimes /* 1429df8bae1dSRodney W. Grimes * Drop a multicast group membership. 1430df8bae1dSRodney W. Grimes * Group must be a valid IP multicast address. 1431df8bae1dSRodney W. Grimes */ 1432cfe8b629SGarrett Wollman error = sooptcopyin(sopt, &mreq, sizeof mreq, sizeof mreq); 1433cfe8b629SGarrett Wollman if (error) 1434df8bae1dSRodney W. Grimes break; 1435cfe8b629SGarrett Wollman 1436cfe8b629SGarrett Wollman if (!IN_MULTICAST(ntohl(mreq.imr_multiaddr.s_addr))) { 1437df8bae1dSRodney W. Grimes error = EINVAL; 1438df8bae1dSRodney W. Grimes break; 1439df8bae1dSRodney W. Grimes } 14409b626c29SGarrett Wollman 144120e8807cSGarrett Wollman s = splimp(); 1442df8bae1dSRodney W. Grimes /* 1443df8bae1dSRodney W. Grimes * If an interface address was specified, get a pointer 1444df8bae1dSRodney W. Grimes * to its ifnet structure. 1445df8bae1dSRodney W. Grimes */ 1446cfe8b629SGarrett Wollman if (mreq.imr_interface.s_addr == INADDR_ANY) 1447df8bae1dSRodney W. Grimes ifp = NULL; 1448df8bae1dSRodney W. Grimes else { 144933841545SHajimu UMEMOTO ifp = ip_multicast_if(&mreq.imr_interface, NULL); 1450df8bae1dSRodney W. Grimes if (ifp == NULL) { 1451df8bae1dSRodney W. Grimes error = EADDRNOTAVAIL; 14529b626c29SGarrett Wollman splx(s); 1453df8bae1dSRodney W. Grimes break; 1454df8bae1dSRodney W. Grimes } 1455df8bae1dSRodney W. Grimes } 1456df8bae1dSRodney W. Grimes /* 1457df8bae1dSRodney W. Grimes * Find the membership in the membership array. 1458df8bae1dSRodney W. Grimes */ 1459a2dc1f50SRobert Watson imo = ip_findmoptions(inp); 1460df8bae1dSRodney W. Grimes for (i = 0; i < imo->imo_num_memberships; ++i) { 1461df8bae1dSRodney W. Grimes if ((ifp == NULL || 1462df8bae1dSRodney W. Grimes imo->imo_membership[i]->inm_ifp == ifp) && 1463df8bae1dSRodney W. Grimes imo->imo_membership[i]->inm_addr.s_addr == 1464cfe8b629SGarrett Wollman mreq.imr_multiaddr.s_addr) 1465df8bae1dSRodney W. Grimes break; 1466df8bae1dSRodney W. Grimes } 1467df8bae1dSRodney W. Grimes if (i == imo->imo_num_memberships) { 1468a2dc1f50SRobert Watson INP_UNLOCK(inp); 1469df8bae1dSRodney W. Grimes error = EADDRNOTAVAIL; 14709b626c29SGarrett Wollman splx(s); 1471df8bae1dSRodney W. Grimes break; 1472df8bae1dSRodney W. Grimes } 1473df8bae1dSRodney W. Grimes /* 1474df8bae1dSRodney W. Grimes * Give up the multicast address record to which the 1475df8bae1dSRodney W. Grimes * membership points. 1476df8bae1dSRodney W. Grimes */ 1477df8bae1dSRodney W. Grimes in_delmulti(imo->imo_membership[i]); 1478df8bae1dSRodney W. Grimes /* 1479df8bae1dSRodney W. Grimes * Remove the gap in the membership array. 1480df8bae1dSRodney W. Grimes */ 1481df8bae1dSRodney W. Grimes for (++i; i < imo->imo_num_memberships; ++i) 1482df8bae1dSRodney W. Grimes imo->imo_membership[i-1] = imo->imo_membership[i]; 1483df8bae1dSRodney W. Grimes --imo->imo_num_memberships; 1484a2dc1f50SRobert Watson INP_UNLOCK(inp); 14859b626c29SGarrett Wollman splx(s); 1486df8bae1dSRodney W. Grimes break; 1487df8bae1dSRodney W. Grimes 1488df8bae1dSRodney W. Grimes default: 1489df8bae1dSRodney W. Grimes error = EOPNOTSUPP; 1490df8bae1dSRodney W. Grimes break; 1491df8bae1dSRodney W. Grimes } 1492df8bae1dSRodney W. Grimes 1493df8bae1dSRodney W. Grimes return (error); 1494df8bae1dSRodney W. Grimes } 1495df8bae1dSRodney W. Grimes 1496df8bae1dSRodney W. Grimes /* 1497df8bae1dSRodney W. Grimes * Return the IP multicast options in response to user getsockopt(). 1498df8bae1dSRodney W. Grimes */ 14990312fbe9SPoul-Henning Kamp static int 150089924e58SRobert Watson ip_getmoptions(struct inpcb *inp, struct sockopt *sopt) 1501df8bae1dSRodney W. Grimes { 150289924e58SRobert Watson struct ip_moptions *imo; 1503cfe8b629SGarrett Wollman struct in_addr addr; 1504df8bae1dSRodney W. Grimes struct in_ifaddr *ia; 1505cfe8b629SGarrett Wollman int error, optval; 1506cfe8b629SGarrett Wollman u_char coptval; 1507df8bae1dSRodney W. Grimes 150889924e58SRobert Watson INP_LOCK(inp); 150989924e58SRobert Watson imo = inp->inp_moptions; 151089924e58SRobert Watson 1511cfe8b629SGarrett Wollman error = 0; 1512cfe8b629SGarrett Wollman switch (sopt->sopt_name) { 1513f0068c4aSGarrett Wollman case IP_MULTICAST_VIF: 1514f0068c4aSGarrett Wollman if (imo != NULL) 1515cfe8b629SGarrett Wollman optval = imo->imo_multicast_vif; 1516f0068c4aSGarrett Wollman else 1517cfe8b629SGarrett Wollman optval = -1; 151889924e58SRobert Watson INP_UNLOCK(inp); 1519cfe8b629SGarrett Wollman error = sooptcopyout(sopt, &optval, sizeof optval); 1520cfe8b629SGarrett Wollman break; 1521f0068c4aSGarrett Wollman 1522df8bae1dSRodney W. Grimes case IP_MULTICAST_IF: 1523df8bae1dSRodney W. Grimes if (imo == NULL || imo->imo_multicast_ifp == NULL) 1524cfe8b629SGarrett Wollman addr.s_addr = INADDR_ANY; 152533841545SHajimu UMEMOTO else if (imo->imo_multicast_addr.s_addr) { 152633841545SHajimu UMEMOTO /* return the value user has set */ 152733841545SHajimu UMEMOTO addr = imo->imo_multicast_addr; 152833841545SHajimu UMEMOTO } else { 1529df8bae1dSRodney W. Grimes IFP_TO_IA(imo->imo_multicast_ifp, ia); 1530cfe8b629SGarrett Wollman addr.s_addr = (ia == NULL) ? INADDR_ANY 1531df8bae1dSRodney W. Grimes : IA_SIN(ia)->sin_addr.s_addr; 1532df8bae1dSRodney W. Grimes } 153389924e58SRobert Watson INP_UNLOCK(inp); 1534cfe8b629SGarrett Wollman error = sooptcopyout(sopt, &addr, sizeof addr); 1535cfe8b629SGarrett Wollman break; 1536df8bae1dSRodney W. Grimes 1537df8bae1dSRodney W. Grimes case IP_MULTICAST_TTL: 1538cfe8b629SGarrett Wollman if (imo == 0) 1539cfe8b629SGarrett Wollman optval = coptval = IP_DEFAULT_MULTICAST_TTL; 1540cfe8b629SGarrett Wollman else 1541cfe8b629SGarrett Wollman optval = coptval = imo->imo_multicast_ttl; 154289924e58SRobert Watson INP_UNLOCK(inp); 1543cfe8b629SGarrett Wollman if (sopt->sopt_valsize == 1) 1544cfe8b629SGarrett Wollman error = sooptcopyout(sopt, &coptval, 1); 1545cfe8b629SGarrett Wollman else 1546cfe8b629SGarrett Wollman error = sooptcopyout(sopt, &optval, sizeof optval); 1547cfe8b629SGarrett Wollman break; 1548df8bae1dSRodney W. Grimes 1549df8bae1dSRodney W. Grimes case IP_MULTICAST_LOOP: 1550cfe8b629SGarrett Wollman if (imo == 0) 1551cfe8b629SGarrett Wollman optval = coptval = IP_DEFAULT_MULTICAST_LOOP; 1552cfe8b629SGarrett Wollman else 1553cfe8b629SGarrett Wollman optval = coptval = imo->imo_multicast_loop; 155489924e58SRobert Watson INP_UNLOCK(inp); 1555cfe8b629SGarrett Wollman if (sopt->sopt_valsize == 1) 1556cfe8b629SGarrett Wollman error = sooptcopyout(sopt, &coptval, 1); 1557cfe8b629SGarrett Wollman else 1558cfe8b629SGarrett Wollman error = sooptcopyout(sopt, &optval, sizeof optval); 1559cfe8b629SGarrett Wollman break; 1560df8bae1dSRodney W. Grimes 1561df8bae1dSRodney W. Grimes default: 156289924e58SRobert Watson INP_UNLOCK(inp); 1563cfe8b629SGarrett Wollman error = ENOPROTOOPT; 1564cfe8b629SGarrett Wollman break; 1565df8bae1dSRodney W. Grimes } 156689924e58SRobert Watson INP_UNLOCK_ASSERT(inp); 156789924e58SRobert Watson 1568cfe8b629SGarrett Wollman return (error); 1569df8bae1dSRodney W. Grimes } 1570df8bae1dSRodney W. Grimes 1571df8bae1dSRodney W. Grimes /* 1572df8bae1dSRodney W. Grimes * Discard the IP multicast options. 1573df8bae1dSRodney W. Grimes */ 1574df8bae1dSRodney W. Grimes void 1575b375c9ecSLuigi Rizzo ip_freemoptions(imo) 1576b375c9ecSLuigi Rizzo register struct ip_moptions *imo; 1577df8bae1dSRodney W. Grimes { 1578b375c9ecSLuigi Rizzo register int i; 1579df8bae1dSRodney W. Grimes 1580df8bae1dSRodney W. Grimes if (imo != NULL) { 1581df8bae1dSRodney W. Grimes for (i = 0; i < imo->imo_num_memberships; ++i) 1582df8bae1dSRodney W. Grimes in_delmulti(imo->imo_membership[i]); 15833548bfc9SBruce M Simpson free(imo->imo_membership, M_IPMOPTS); 1584df8bae1dSRodney W. Grimes free(imo, M_IPMOPTS); 1585df8bae1dSRodney W. Grimes } 1586df8bae1dSRodney W. Grimes } 1587df8bae1dSRodney W. Grimes 1588df8bae1dSRodney W. Grimes /* 1589df8bae1dSRodney W. Grimes * Routine called from ip_output() to loop back a copy of an IP multicast 1590df8bae1dSRodney W. Grimes * packet to the input queue of a specified interface. Note that this 1591df8bae1dSRodney W. Grimes * calls the output routine of the loopback "driver", but with an interface 1592f5fea3ddSPaul Traina * pointer that might NOT be a loopback interface -- evil, but easier than 1593f5fea3ddSPaul Traina * replicating that code here. 1594df8bae1dSRodney W. Grimes */ 1595df8bae1dSRodney W. Grimes static void 1596b375c9ecSLuigi Rizzo ip_mloopback(ifp, m, dst, hlen) 1597b375c9ecSLuigi Rizzo struct ifnet *ifp; 1598b375c9ecSLuigi Rizzo register struct mbuf *m; 1599b375c9ecSLuigi Rizzo register struct sockaddr_in *dst; 1600b375c9ecSLuigi Rizzo int hlen; 1601df8bae1dSRodney W. Grimes { 1602b375c9ecSLuigi Rizzo register struct ip *ip; 1603df8bae1dSRodney W. Grimes struct mbuf *copym; 1604df8bae1dSRodney W. Grimes 1605b375c9ecSLuigi Rizzo copym = m_copy(m, 0, M_COPYALL); 160686b1d6d2SBill Fenner if (copym != NULL && (copym->m_flags & M_EXT || copym->m_len < hlen)) 160786b1d6d2SBill Fenner copym = m_pullup(copym, hlen); 1608df8bae1dSRodney W. Grimes if (copym != NULL) { 1609390cdc6aSRuslan Ermilov /* If needed, compute the checksum and mark it as valid. */ 1610390cdc6aSRuslan Ermilov if (copym->m_pkthdr.csum_flags & CSUM_DELAY_DATA) { 1611390cdc6aSRuslan Ermilov in_delayed_cksum(copym); 1612390cdc6aSRuslan Ermilov copym->m_pkthdr.csum_flags &= ~CSUM_DELAY_DATA; 1613390cdc6aSRuslan Ermilov copym->m_pkthdr.csum_flags |= 1614390cdc6aSRuslan Ermilov CSUM_DATA_VALID | CSUM_PSEUDO_HDR; 1615390cdc6aSRuslan Ermilov copym->m_pkthdr.csum_data = 0xffff; 1616390cdc6aSRuslan Ermilov } 1617df8bae1dSRodney W. Grimes /* 1618df8bae1dSRodney W. Grimes * We don't bother to fragment if the IP length is greater 1619df8bae1dSRodney W. Grimes * than the interface's MTU. Can this possibly matter? 1620df8bae1dSRodney W. Grimes */ 1621df8bae1dSRodney W. Grimes ip = mtod(copym, struct ip *); 1622fd8e4ebcSMike Barcroft ip->ip_len = htons(ip->ip_len); 1623fd8e4ebcSMike Barcroft ip->ip_off = htons(ip->ip_off); 1624df8bae1dSRodney W. Grimes ip->ip_sum = 0; 162586b1d6d2SBill Fenner ip->ip_sum = in_cksum(copym, hlen); 16269c9137eaSGarrett Wollman /* 16279c9137eaSGarrett Wollman * NB: 1628e1596dffSBill Fenner * It's not clear whether there are any lingering 1629e1596dffSBill Fenner * reentrancy problems in other areas which might 1630e1596dffSBill Fenner * be exposed by using ip_input directly (in 1631e1596dffSBill Fenner * particular, everything which modifies the packet 1632e1596dffSBill Fenner * in-place). Yet another option is using the 1633e1596dffSBill Fenner * protosw directly to deliver the looped back 1634e1596dffSBill Fenner * packet. For the moment, we'll err on the side 1635ed7509acSJulian Elischer * of safety by using if_simloop(). 16369c9137eaSGarrett Wollman */ 1637201c2527SJulian Elischer #if 1 /* XXX */ 1638201c2527SJulian Elischer if (dst->sin_family != AF_INET) { 16392b8a366cSJulian Elischer printf("ip_mloopback: bad address family %d\n", 1640201c2527SJulian Elischer dst->sin_family); 1641201c2527SJulian Elischer dst->sin_family = AF_INET; 1642201c2527SJulian Elischer } 1643201c2527SJulian Elischer #endif 1644201c2527SJulian Elischer 16459c9137eaSGarrett Wollman #ifdef notdef 1646e1596dffSBill Fenner copym->m_pkthdr.rcvif = ifp; 1647ed7509acSJulian Elischer ip_input(copym); 16489c9137eaSGarrett Wollman #else 164906a429a3SArchie Cobbs if_simloop(ifp, copym, dst->sin_family, 0); 16509c9137eaSGarrett Wollman #endif 1651df8bae1dSRodney W. Grimes } 1652df8bae1dSRodney W. Grimes } 1653