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 30df8bae1dSRodney W. Grimes */ 31df8bae1dSRodney W. Grimes 324b421e2dSMike Silbersack #include <sys/cdefs.h> 334b421e2dSMike Silbersack __FBSDID("$FreeBSD$"); 344b421e2dSMike Silbersack 351f7e052cSJulian Elischer #include "opt_ipfw.h" 366a800098SYoshinobu Inoue #include "opt_ipsec.h" 374ed84624SRobert Watson #include "opt_mac.h" 3853dcc544SMike Silbersack #include "opt_mbuf_stress_test.h" 39fbd1372aSJoerg Wunsch 40df8bae1dSRodney W. Grimes #include <sys/param.h> 4126f9a767SRodney W. Grimes #include <sys/systm.h> 42f0f6d643SLuigi Rizzo #include <sys/kernel.h> 43df8bae1dSRodney W. Grimes #include <sys/malloc.h> 44df8bae1dSRodney W. Grimes #include <sys/mbuf.h> 45acd3428bSRobert Watson #include <sys/priv.h> 46df8bae1dSRodney W. Grimes #include <sys/protosw.h> 47df8bae1dSRodney W. Grimes #include <sys/socket.h> 48df8bae1dSRodney W. Grimes #include <sys/socketvar.h> 499d9edc56SMike Silbersack #include <sys/sysctl.h> 50df8bae1dSRodney W. Grimes 51df8bae1dSRodney W. Grimes #include <net/if.h> 529b932e9eSAndre Oppermann #include <net/netisr.h> 53c21fd232SAndre Oppermann #include <net/pfil.h> 54df8bae1dSRodney W. Grimes #include <net/route.h> 55df8bae1dSRodney W. Grimes 56df8bae1dSRodney W. Grimes #include <netinet/in.h> 57df8bae1dSRodney W. Grimes #include <netinet/in_systm.h> 58df8bae1dSRodney W. Grimes #include <netinet/ip.h> 59df8bae1dSRodney W. Grimes #include <netinet/in_pcb.h> 60df8bae1dSRodney W. Grimes #include <netinet/in_var.h> 61df8bae1dSRodney W. Grimes #include <netinet/ip_var.h> 62ef39adf0SAndre Oppermann #include <netinet/ip_options.h> 63df8bae1dSRodney W. Grimes 64b2630c29SGeorge V. Neville-Neil #ifdef IPSEC 652cb64cb2SGeorge V. Neville-Neil #include <netinet/ip_ipsec.h> 661dfcf0d2SAndre Oppermann #include <netipsec/ipsec.h> 67b2630c29SGeorge V. Neville-Neil #endif /* IPSEC*/ 686a800098SYoshinobu Inoue 691dfcf0d2SAndre Oppermann #include <machine/in_cksum.h> 701dfcf0d2SAndre Oppermann 71aed55708SRobert Watson #include <security/mac/mac_framework.h> 72aed55708SRobert Watson 732b25acc1SLuigi Rizzo #define print_ip(x, a, y) printf("%s %d.%d.%d.%d%s",\ 742b25acc1SLuigi Rizzo x, (ntohl(a.s_addr)>>24)&0xFF,\ 75f9e354dfSJulian Elischer (ntohl(a.s_addr)>>16)&0xFF,\ 76f9e354dfSJulian Elischer (ntohl(a.s_addr)>>8)&0xFF,\ 772b25acc1SLuigi Rizzo (ntohl(a.s_addr))&0xFF, y); 78f9e354dfSJulian Elischer 79f23b4c91SGarrett Wollman u_short ip_id; 80f23b4c91SGarrett Wollman 8153dcc544SMike Silbersack #ifdef MBUF_STRESS_TEST 829d9edc56SMike Silbersack int mbuf_frag_size = 0; 839d9edc56SMike Silbersack SYSCTL_INT(_net_inet_ip, OID_AUTO, mbuf_frag_size, CTLFLAG_RW, 849d9edc56SMike Silbersack &mbuf_frag_size, 0, "Fragment outgoing mbufs to this size"); 859d9edc56SMike Silbersack #endif 869d9edc56SMike Silbersack 87df8bae1dSRodney W. Grimes static void ip_mloopback 884d77a549SAlfred Perlstein (struct ifnet *, struct mbuf *, struct sockaddr_in *, int); 89afed1b49SDarren Reed 90afed1b49SDarren Reed 9193e0e116SJulian Elischer extern struct protosw inetsw[]; 9293e0e116SJulian Elischer 93df8bae1dSRodney W. Grimes /* 94df8bae1dSRodney W. Grimes * IP output. The packet in mbuf chain m contains a skeletal IP 95df8bae1dSRodney W. Grimes * header (with len, off, ttl, proto, tos, src, dst). 96df8bae1dSRodney W. Grimes * The mbuf chain containing the packet will be freed. 97df8bae1dSRodney W. Grimes * The mbuf opt, if present, will not be freed. 983de758d3SRobert Watson * In the IP forwarding case, the packet will arrive with options already 993de758d3SRobert Watson * inserted, so must have a NULL opt pointer. 100df8bae1dSRodney W. Grimes */ 101df8bae1dSRodney W. Grimes int 102f2565d68SRobert Watson ip_output(struct mbuf *m, struct mbuf *opt, struct route *ro, int flags, 103f2565d68SRobert Watson struct ip_moptions *imo, struct inpcb *inp) 104df8bae1dSRodney W. Grimes { 1051e78ac21SJeffrey Hsu struct ip *ip; 1062b25acc1SLuigi Rizzo struct ifnet *ifp = NULL; /* keep compiler happy */ 107ac9d7e26SMax Laier struct mbuf *m0; 10823bf9953SPoul-Henning Kamp int hlen = sizeof (struct ip); 1093ae2ad08SJohn-Mark Gurney int mtu; 1109b932e9eSAndre Oppermann int len, error = 0; 1112b25acc1SLuigi Rizzo struct sockaddr_in *dst = NULL; /* keep compiler happy */ 1123956b023SLuigi Rizzo struct in_ifaddr *ia = NULL; 113db4f9cc7SJonathan Lemon int isbroadcast, sw_csum; 114e3f406b3SRuslan Ermilov struct route iproute; 1159b932e9eSAndre Oppermann struct in_addr odst; 1169b932e9eSAndre Oppermann #ifdef IPFIREWALL_FORWARD 1179b932e9eSAndre Oppermann struct m_tag *fwd_tag = NULL; 1189b932e9eSAndre Oppermann #endif 119ac9d7e26SMax Laier M_ASSERTPKTHDR(m); 12036e8826fSMax Laier 12102c1c707SAndre Oppermann if (ro == NULL) { 12202c1c707SAndre Oppermann ro = &iproute; 12302c1c707SAndre Oppermann bzero(ro, sizeof (*ro)); 12402c1c707SAndre Oppermann } 12502c1c707SAndre Oppermann 12684843845SSam Leffler if (inp != NULL) 12784843845SSam Leffler INP_LOCK_ASSERT(inp); 1282b25acc1SLuigi Rizzo 129df8bae1dSRodney W. Grimes if (opt) { 130cb7641e8SMaxim Konovalov len = 0; 131df8bae1dSRodney W. Grimes m = ip_insertoptions(m, opt, &len); 132cb7641e8SMaxim Konovalov if (len != 0) 133df8bae1dSRodney W. Grimes hlen = len; 134df8bae1dSRodney W. Grimes } 135df8bae1dSRodney W. Grimes ip = mtod(m, struct ip *); 1363efc3014SJulian Elischer 137df8bae1dSRodney W. Grimes /* 1386e49b1feSGarrett Wollman * Fill in IP header. If we are not allowing fragmentation, 139e0f630eaSAndre Oppermann * then the ip_id field is meaningless, but we don't set it 140e0f630eaSAndre Oppermann * to zero. Doing so causes various problems when devices along 141e0f630eaSAndre Oppermann * the path (routers, load balancers, firewalls, etc.) illegally 142e0f630eaSAndre Oppermann * disable DF on our packet. Note that a 16-bit counter 1436e49b1feSGarrett Wollman * will wrap around in less than 10 seconds at 100 Mbit/s on a 1446e49b1feSGarrett Wollman * medium with MTU 1500. See Steven M. Bellovin, "A Technique 1456e49b1feSGarrett Wollman * for Counting NATted Hosts", Proc. IMW'02, available at 1464d09f5a0SGleb Smirnoff * <http://www.cs.columbia.edu/~smb/papers/fnat.pdf>. 147df8bae1dSRodney W. Grimes */ 148df8bae1dSRodney W. Grimes if ((flags & (IP_FORWARDING|IP_RAWOUTPUT)) == 0) { 14953be11f6SPoul-Henning Kamp ip->ip_v = IPVERSION; 15053be11f6SPoul-Henning Kamp ip->ip_hl = hlen >> 2; 1511f44b0a1SDavid Malone ip->ip_id = ip_newid(); 152df8bae1dSRodney W. Grimes ipstat.ips_localout++; 153df8bae1dSRodney W. Grimes } else { 15453be11f6SPoul-Henning Kamp hlen = ip->ip_hl << 2; 155df8bae1dSRodney W. Grimes } 1569c9137eaSGarrett Wollman 157df8bae1dSRodney W. Grimes dst = (struct sockaddr_in *)&ro->ro_dst; 1589b932e9eSAndre Oppermann again: 159df8bae1dSRodney W. Grimes /* 160df8bae1dSRodney W. Grimes * If there is a cached route, 161df8bae1dSRodney W. Grimes * check that it is to the same destination 162df8bae1dSRodney W. Grimes * and is still up. If not, free it and try again. 163a4a6e773SHajimu UMEMOTO * The address family should also be checked in case of sharing the 164a4a6e773SHajimu UMEMOTO * cache with IPv6. 165df8bae1dSRodney W. Grimes */ 166df8bae1dSRodney W. Grimes if (ro->ro_rt && ((ro->ro_rt->rt_flags & RTF_UP) == 0 || 167a4a6e773SHajimu UMEMOTO dst->sin_family != AF_INET || 1689b932e9eSAndre Oppermann dst->sin_addr.s_addr != ip->ip_dst.s_addr)) { 169df8bae1dSRodney W. Grimes RTFREE(ro->ro_rt); 1703ae2ad08SJohn-Mark Gurney ro->ro_rt = (struct rtentry *)NULL; 171df8bae1dSRodney W. Grimes } 1729b932e9eSAndre Oppermann #ifdef IPFIREWALL_FORWARD 1739b932e9eSAndre Oppermann if (ro->ro_rt == NULL && fwd_tag == NULL) { 1749b932e9eSAndre Oppermann #else 1750b17fba7SAndre Oppermann if (ro->ro_rt == NULL) { 1769b932e9eSAndre Oppermann #endif 177a4a6e773SHajimu UMEMOTO bzero(dst, sizeof(*dst)); 178df8bae1dSRodney W. Grimes dst->sin_family = AF_INET; 179df8bae1dSRodney W. Grimes dst->sin_len = sizeof(*dst); 1809b932e9eSAndre Oppermann dst->sin_addr = ip->ip_dst; 181df8bae1dSRodney W. Grimes } 182df8bae1dSRodney W. Grimes /* 183a3fd02d8SBruce M Simpson * If routing to interface only, short circuit routing lookup. 184a3fd02d8SBruce M Simpson * The use of an all-ones broadcast address implies this; an 185a3fd02d8SBruce M Simpson * interface is specified by the broadcast address of an interface, 186a3fd02d8SBruce M Simpson * or the destination address of a ptp interface. 187df8bae1dSRodney W. Grimes */ 188a3fd02d8SBruce M Simpson if (flags & IP_SENDONES) { 189a3fd02d8SBruce M Simpson if ((ia = ifatoia(ifa_ifwithbroadaddr(sintosa(dst)))) == NULL && 190a3fd02d8SBruce M Simpson (ia = ifatoia(ifa_ifwithdstaddr(sintosa(dst)))) == NULL) { 191a3fd02d8SBruce M Simpson ipstat.ips_noroute++; 192a3fd02d8SBruce M Simpson error = ENETUNREACH; 193a3fd02d8SBruce M Simpson goto bad; 194a3fd02d8SBruce M Simpson } 195a3fd02d8SBruce M Simpson ip->ip_dst.s_addr = INADDR_BROADCAST; 196a3fd02d8SBruce M Simpson dst->sin_addr = ip->ip_dst; 197a3fd02d8SBruce M Simpson ifp = ia->ia_ifp; 198a3fd02d8SBruce M Simpson ip->ip_ttl = 1; 199a3fd02d8SBruce M Simpson isbroadcast = 1; 200a3fd02d8SBruce M Simpson } else if (flags & IP_ROUTETOIF) { 2010b17fba7SAndre Oppermann if ((ia = ifatoia(ifa_ifwithdstaddr(sintosa(dst)))) == NULL && 2020b17fba7SAndre Oppermann (ia = ifatoia(ifa_ifwithnet(sintosa(dst)))) == NULL) { 203df8bae1dSRodney W. Grimes ipstat.ips_noroute++; 204df8bae1dSRodney W. Grimes error = ENETUNREACH; 205df8bae1dSRodney W. Grimes goto bad; 206df8bae1dSRodney W. Grimes } 207df8bae1dSRodney W. Grimes ifp = ia->ia_ifp; 208df8bae1dSRodney W. Grimes ip->ip_ttl = 1; 2099f9b3dc4SGarrett Wollman isbroadcast = in_broadcast(dst->sin_addr, ifp); 2103afefa39SDaniel C. Sobral } else if (IN_MULTICAST(ntohl(ip->ip_dst.s_addr)) && 21138c1bc35SRuslan Ermilov imo != NULL && imo->imo_multicast_ifp != NULL) { 2123afefa39SDaniel C. Sobral /* 21338c1bc35SRuslan Ermilov * Bypass the normal routing lookup for multicast 21438c1bc35SRuslan Ermilov * packets if the interface is specified. 2153afefa39SDaniel C. Sobral */ 21638c1bc35SRuslan Ermilov ifp = imo->imo_multicast_ifp; 21738c1bc35SRuslan Ermilov IFP_TO_IA(ifp, ia); 21838c1bc35SRuslan Ermilov isbroadcast = 0; /* fool gcc */ 219df8bae1dSRodney W. Grimes } else { 2202c17fe93SGarrett Wollman /* 22197d8d152SAndre Oppermann * We want to do any cloning requested by the link layer, 22297d8d152SAndre Oppermann * as this is probably required in all cases for correct 22397d8d152SAndre Oppermann * operation (as it is for ARP). 2242c17fe93SGarrett Wollman */ 2250b17fba7SAndre Oppermann if (ro->ro_rt == NULL) 226e6e51f05SLuigi Rizzo rtalloc_ign(ro, 0); 2270b17fba7SAndre Oppermann if (ro->ro_rt == NULL) { 228df8bae1dSRodney W. Grimes ipstat.ips_noroute++; 229df8bae1dSRodney W. Grimes error = EHOSTUNREACH; 230df8bae1dSRodney W. Grimes goto bad; 231df8bae1dSRodney W. Grimes } 232df8bae1dSRodney W. Grimes ia = ifatoia(ro->ro_rt->rt_ifa); 233df8bae1dSRodney W. Grimes ifp = ro->ro_rt->rt_ifp; 23497d8d152SAndre Oppermann ro->ro_rt->rt_rmx.rmx_pksent++; 235df8bae1dSRodney W. Grimes if (ro->ro_rt->rt_flags & RTF_GATEWAY) 236f2c2962eSRuslan Ermilov dst = (struct sockaddr_in *)ro->ro_rt->rt_gateway; 2379f9b3dc4SGarrett Wollman if (ro->ro_rt->rt_flags & RTF_HOST) 238f2c2962eSRuslan Ermilov isbroadcast = (ro->ro_rt->rt_flags & RTF_BROADCAST); 2399f9b3dc4SGarrett Wollman else 2409f9b3dc4SGarrett Wollman isbroadcast = in_broadcast(dst->sin_addr, ifp); 241df8bae1dSRodney W. Grimes } 2423ae2ad08SJohn-Mark Gurney /* 2433ae2ad08SJohn-Mark Gurney * Calculate MTU. If we have a route that is up, use that, 2443ae2ad08SJohn-Mark Gurney * otherwise use the interface's MTU. 2453ae2ad08SJohn-Mark Gurney */ 246384a05bfSAndre Oppermann if (ro->ro_rt != NULL && (ro->ro_rt->rt_flags & (RTF_UP|RTF_HOST))) { 2473ae2ad08SJohn-Mark Gurney /* 2483ae2ad08SJohn-Mark Gurney * This case can happen if the user changed the MTU 2493ae2ad08SJohn-Mark Gurney * of an interface after enabling IP on it. Because 2503ae2ad08SJohn-Mark Gurney * most netifs don't keep track of routes pointing to 2513ae2ad08SJohn-Mark Gurney * them, there is no way for one to update all its 2523ae2ad08SJohn-Mark Gurney * routes when the MTU is changed. 2533ae2ad08SJohn-Mark Gurney */ 2543ae2ad08SJohn-Mark Gurney if (ro->ro_rt->rt_rmx.rmx_mtu > ifp->if_mtu) 2553ae2ad08SJohn-Mark Gurney ro->ro_rt->rt_rmx.rmx_mtu = ifp->if_mtu; 2563ae2ad08SJohn-Mark Gurney mtu = ro->ro_rt->rt_rmx.rmx_mtu; 2573ae2ad08SJohn-Mark Gurney } else { 2583ae2ad08SJohn-Mark Gurney mtu = ifp->if_mtu; 2593ae2ad08SJohn-Mark Gurney } 2609b932e9eSAndre Oppermann if (IN_MULTICAST(ntohl(ip->ip_dst.s_addr))) { 261df8bae1dSRodney W. Grimes struct in_multi *inm; 262df8bae1dSRodney W. Grimes 263df8bae1dSRodney W. Grimes m->m_flags |= M_MCAST; 264df8bae1dSRodney W. Grimes /* 265df8bae1dSRodney W. Grimes * IP destination address is multicast. Make sure "dst" 266df8bae1dSRodney W. Grimes * still points to the address in "ro". (It may have been 267df8bae1dSRodney W. Grimes * changed to point to a gateway address, above.) 268df8bae1dSRodney W. Grimes */ 269df8bae1dSRodney W. Grimes dst = (struct sockaddr_in *)&ro->ro_dst; 270df8bae1dSRodney W. Grimes /* 271df8bae1dSRodney W. Grimes * See if the caller provided any multicast options 272df8bae1dSRodney W. Grimes */ 273df8bae1dSRodney W. Grimes if (imo != NULL) { 274df8bae1dSRodney W. Grimes ip->ip_ttl = imo->imo_multicast_ttl; 2751c5de19aSGarrett Wollman if (imo->imo_multicast_vif != -1) 2761c5de19aSGarrett Wollman ip->ip_src.s_addr = 277bbb4330bSLuigi Rizzo ip_mcast_src ? 278bbb4330bSLuigi Rizzo ip_mcast_src(imo->imo_multicast_vif) : 279bbb4330bSLuigi Rizzo INADDR_ANY; 280df8bae1dSRodney W. Grimes } else 281df8bae1dSRodney W. Grimes ip->ip_ttl = IP_DEFAULT_MULTICAST_TTL; 282df8bae1dSRodney W. Grimes /* 283df8bae1dSRodney W. Grimes * Confirm that the outgoing interface supports multicast. 284df8bae1dSRodney W. Grimes */ 2851c5de19aSGarrett Wollman if ((imo == NULL) || (imo->imo_multicast_vif == -1)) { 286df8bae1dSRodney W. Grimes if ((ifp->if_flags & IFF_MULTICAST) == 0) { 287df8bae1dSRodney W. Grimes ipstat.ips_noroute++; 288df8bae1dSRodney W. Grimes error = ENETUNREACH; 289df8bae1dSRodney W. Grimes goto bad; 290df8bae1dSRodney W. Grimes } 2911c5de19aSGarrett Wollman } 292df8bae1dSRodney W. Grimes /* 293df8bae1dSRodney W. Grimes * If source address not specified yet, use address 294df8bae1dSRodney W. Grimes * of outgoing interface. 295df8bae1dSRodney W. Grimes */ 296df8bae1dSRodney W. Grimes if (ip->ip_src.s_addr == INADDR_ANY) { 29738c1bc35SRuslan Ermilov /* Interface may have no addresses. */ 29838c1bc35SRuslan Ermilov if (ia != NULL) 29938c1bc35SRuslan Ermilov ip->ip_src = IA_SIN(ia)->sin_addr; 300df8bae1dSRodney W. Grimes } 301df8bae1dSRodney W. Grimes 302dd5a318bSRobert Watson IN_MULTI_LOCK(); 3039b932e9eSAndre Oppermann IN_LOOKUP_MULTI(ip->ip_dst, ifp, inm); 304df8bae1dSRodney W. Grimes if (inm != NULL && 305df8bae1dSRodney W. Grimes (imo == NULL || imo->imo_multicast_loop)) { 306dd5a318bSRobert Watson IN_MULTI_UNLOCK(); 307df8bae1dSRodney W. Grimes /* 308df8bae1dSRodney W. Grimes * If we belong to the destination multicast group 309df8bae1dSRodney W. Grimes * on the outgoing interface, and the caller did not 310df8bae1dSRodney W. Grimes * forbid loopback, loop back a copy. 311df8bae1dSRodney W. Grimes */ 31286b1d6d2SBill Fenner ip_mloopback(ifp, m, dst, hlen); 313df8bae1dSRodney W. Grimes } 314df8bae1dSRodney W. Grimes else { 315dd5a318bSRobert Watson IN_MULTI_UNLOCK(); 316df8bae1dSRodney W. Grimes /* 317df8bae1dSRodney W. Grimes * If we are acting as a multicast router, perform 318df8bae1dSRodney W. Grimes * multicast forwarding as if the packet had just 319df8bae1dSRodney W. Grimes * arrived on the interface to which we are about 320df8bae1dSRodney W. Grimes * to send. The multicast forwarding function 321df8bae1dSRodney W. Grimes * recursively calls this function, using the 322df8bae1dSRodney W. Grimes * IP_FORWARDING flag to prevent infinite recursion. 323df8bae1dSRodney W. Grimes * 324df8bae1dSRodney W. Grimes * Multicasts that are looped back by ip_mloopback(), 325df8bae1dSRodney W. Grimes * above, will be forwarded by the ip_input() routine, 326df8bae1dSRodney W. Grimes * if necessary. 327df8bae1dSRodney W. Grimes */ 328df8bae1dSRodney W. Grimes if (ip_mrouter && (flags & IP_FORWARDING) == 0) { 329f0068c4aSGarrett Wollman /* 330bbb4330bSLuigi Rizzo * If rsvp daemon is not running, do not 331f0068c4aSGarrett Wollman * set ip_moptions. This ensures that the packet 332f0068c4aSGarrett Wollman * is multicast and not just sent down one link 333f0068c4aSGarrett Wollman * as prescribed by rsvpd. 334f0068c4aSGarrett Wollman */ 335b124e4f2SGarrett Wollman if (!rsvp_on) 336f0068c4aSGarrett Wollman imo = NULL; 337bbb4330bSLuigi Rizzo if (ip_mforward && 338bbb4330bSLuigi Rizzo ip_mforward(ip, ifp, m, imo) != 0) { 339df8bae1dSRodney W. Grimes m_freem(m); 340df8bae1dSRodney W. Grimes goto done; 341df8bae1dSRodney W. Grimes } 342df8bae1dSRodney W. Grimes } 343df8bae1dSRodney W. Grimes } 3445e9ae478SGarrett Wollman 345df8bae1dSRodney W. Grimes /* 346df8bae1dSRodney W. Grimes * Multicasts with a time-to-live of zero may be looped- 347df8bae1dSRodney W. Grimes * back, above, but must not be transmitted on a network. 348df8bae1dSRodney W. Grimes * Also, multicasts addressed to the loopback interface 349df8bae1dSRodney W. Grimes * are not sent -- the above call to ip_mloopback() will 350df8bae1dSRodney W. Grimes * loop back a copy if this host actually belongs to the 351df8bae1dSRodney W. Grimes * destination group on the loopback interface. 352df8bae1dSRodney W. Grimes */ 353f5fea3ddSPaul Traina if (ip->ip_ttl == 0 || ifp->if_flags & IFF_LOOPBACK) { 354df8bae1dSRodney W. Grimes m_freem(m); 355df8bae1dSRodney W. Grimes goto done; 356df8bae1dSRodney W. Grimes } 357df8bae1dSRodney W. Grimes 358df8bae1dSRodney W. Grimes goto sendit; 359df8bae1dSRodney W. Grimes } 3606a7c943cSAndre Oppermann 361df8bae1dSRodney W. Grimes /* 3622b25acc1SLuigi Rizzo * If the source address is not specified yet, use the address 363cb459254SJohn-Mark Gurney * of the outoing interface. 364df8bae1dSRodney W. Grimes */ 365f9e354dfSJulian Elischer if (ip->ip_src.s_addr == INADDR_ANY) { 36638c1bc35SRuslan Ermilov /* Interface may have no addresses. */ 36738c1bc35SRuslan Ermilov if (ia != NULL) { 368df8bae1dSRodney W. Grimes ip->ip_src = IA_SIN(ia)->sin_addr; 369f9e354dfSJulian Elischer } 37038c1bc35SRuslan Ermilov } 3716a7c943cSAndre Oppermann 372ca7a789aSMax Laier /* 373ca7a789aSMax Laier * Verify that we have any chance at all of being able to queue the 374ca7a789aSMax Laier * packet or packet fragments, unless ALTQ is enabled on the given 375ca7a789aSMax Laier * interface in which case packetdrop should be done by queueing. 376ca7a789aSMax Laier */ 37702b199f1SMax Laier #ifdef ALTQ 378ca7a789aSMax Laier if ((!ALTQ_IS_ENABLED(&ifp->if_snd)) && 3793ae2ad08SJohn-Mark Gurney ((ifp->if_snd.ifq_len + ip->ip_len / mtu + 1) >= 380ca7a789aSMax Laier ifp->if_snd.ifq_maxlen)) 381ca7a789aSMax Laier #else 3823ae2ad08SJohn-Mark Gurney if ((ifp->if_snd.ifq_len + ip->ip_len / mtu + 1) >= 383ca7a789aSMax Laier ifp->if_snd.ifq_maxlen) 384ca7a789aSMax Laier #endif /* ALTQ */ 385ca7a789aSMax Laier { 386b5390296SDavid Greenman error = ENOBUFS; 38735609d45SJonathan Lemon ipstat.ips_odropped++; 388bbce982bSGleb Smirnoff ifp->if_snd.ifq_drops += (ip->ip_len / ifp->if_mtu + 1); 389b5390296SDavid Greenman goto bad; 390b5390296SDavid Greenman } 391b5390296SDavid Greenman 392b5390296SDavid Greenman /* 393df8bae1dSRodney W. Grimes * Look for broadcast address and 3948c3f5566SRuslan Ermilov * verify user is allowed to send 395df8bae1dSRodney W. Grimes * such a packet. 396df8bae1dSRodney W. Grimes */ 3979f9b3dc4SGarrett Wollman if (isbroadcast) { 398df8bae1dSRodney W. Grimes if ((ifp->if_flags & IFF_BROADCAST) == 0) { 399df8bae1dSRodney W. Grimes error = EADDRNOTAVAIL; 400df8bae1dSRodney W. Grimes goto bad; 401df8bae1dSRodney W. Grimes } 402df8bae1dSRodney W. Grimes if ((flags & IP_ALLOWBROADCAST) == 0) { 403df8bae1dSRodney W. Grimes error = EACCES; 404df8bae1dSRodney W. Grimes goto bad; 405df8bae1dSRodney W. Grimes } 406df8bae1dSRodney W. Grimes /* don't allow broadcast messages to be fragmented */ 4073ae2ad08SJohn-Mark Gurney if (ip->ip_len > mtu) { 408df8bae1dSRodney W. Grimes error = EMSGSIZE; 409df8bae1dSRodney W. Grimes goto bad; 410df8bae1dSRodney W. Grimes } 411df8bae1dSRodney W. Grimes m->m_flags |= M_BCAST; 4129f9b3dc4SGarrett Wollman } else { 413df8bae1dSRodney W. Grimes m->m_flags &= ~M_BCAST; 4149f9b3dc4SGarrett Wollman } 415df8bae1dSRodney W. Grimes 416f1743588SDarren Reed sendit: 417b2630c29SGeorge V. Neville-Neil #ifdef IPSEC 4181dfcf0d2SAndre Oppermann switch(ip_ipsec_output(&m, inp, &flags, &error, &ro, &iproute, &dst, &ia, &ifp)) { 4191dfcf0d2SAndre Oppermann case 1: 42033841545SHajimu UMEMOTO goto bad; 4211dfcf0d2SAndre Oppermann case -1: 4221dfcf0d2SAndre Oppermann goto done; 4231dfcf0d2SAndre Oppermann case 0: 42433841545SHajimu UMEMOTO default: 4251dfcf0d2SAndre Oppermann break; /* Continue with packet processing. */ 42633841545SHajimu UMEMOTO } 4271dfcf0d2SAndre Oppermann /* Update variables that are affected by ipsec4_output(). */ 42833841545SHajimu UMEMOTO ip = mtod(m, struct ip *); 42933841545SHajimu UMEMOTO hlen = ip->ip_hl << 2; 430b2630c29SGeorge V. Neville-Neil #endif /* IPSEC */ 43133841545SHajimu UMEMOTO 432c21fd232SAndre Oppermann /* Jump over all PFIL processing if hooks are not active. */ 433604afec4SChristian S.J. Peron if (!PFIL_HOOKED(&inet_pfil_hook)) 434c21fd232SAndre Oppermann goto passout; 435c21fd232SAndre Oppermann 436c21fd232SAndre Oppermann /* Run through list of hooks for output packets. */ 4379b932e9eSAndre Oppermann odst.s_addr = ip->ip_dst.s_addr; 438d6a8d588SMax Laier error = pfil_run_hooks(&inet_pfil_hook, &m, ifp, PFIL_OUT, inp); 439134ea224SSam Leffler if (error != 0 || m == NULL) 440c4ac87eaSDarren Reed goto done; 4419b932e9eSAndre Oppermann 442c4ac87eaSDarren Reed ip = mtod(m, struct ip *); 443fed1c7e9SSøren Schmidt 4449b932e9eSAndre Oppermann /* See if destination IP address was changed by packet filter. */ 4459b932e9eSAndre Oppermann if (odst.s_addr != ip->ip_dst.s_addr) { 4469b932e9eSAndre Oppermann m->m_flags |= M_SKIP_FIREWALL; 447f4fca2d8SAndre Oppermann /* If destination is now ourself drop to ip_input(). */ 4489b932e9eSAndre Oppermann if (in_localip(ip->ip_dst)) { 4499b932e9eSAndre Oppermann m->m_flags |= M_FASTFWD_OURS; 450f9e354dfSJulian Elischer if (m->m_pkthdr.rcvif == NULL) 451a9c92b54SAndre Oppermann m->m_pkthdr.rcvif = loif; 45220c822f3SJonathan Lemon if (m->m_pkthdr.csum_flags & CSUM_DELAY_DATA) { 45320c822f3SJonathan Lemon m->m_pkthdr.csum_flags |= 45420c822f3SJonathan Lemon CSUM_DATA_VALID | CSUM_PSEUDO_HDR; 455ac9d7e26SMax Laier m->m_pkthdr.csum_data = 0xffff; 45620c822f3SJonathan Lemon } 45720c822f3SJonathan Lemon m->m_pkthdr.csum_flags |= 45820c822f3SJonathan Lemon CSUM_IP_CHECKED | CSUM_IP_VALID; 4599b932e9eSAndre Oppermann 4609b932e9eSAndre Oppermann error = netisr_queue(NETISR_IP, m); 4619b932e9eSAndre Oppermann goto done; 4629b932e9eSAndre Oppermann } else 463f4fca2d8SAndre Oppermann goto again; /* Redo the routing table lookup. */ 4649b932e9eSAndre Oppermann } 4659b932e9eSAndre Oppermann 4669b932e9eSAndre Oppermann #ifdef IPFIREWALL_FORWARD 4679b932e9eSAndre Oppermann /* See if local, if yes, send it to netisr with IP_FASTFWD_OURS. */ 4689b932e9eSAndre Oppermann if (m->m_flags & M_FASTFWD_OURS) { 4699b932e9eSAndre Oppermann if (m->m_pkthdr.rcvif == NULL) 470a9c92b54SAndre Oppermann m->m_pkthdr.rcvif = loif; 4719b932e9eSAndre Oppermann if (m->m_pkthdr.csum_flags & CSUM_DELAY_DATA) { 4729b932e9eSAndre Oppermann m->m_pkthdr.csum_flags |= 4739b932e9eSAndre Oppermann CSUM_DATA_VALID | CSUM_PSEUDO_HDR; 4749b932e9eSAndre Oppermann m->m_pkthdr.csum_data = 0xffff; 4759b932e9eSAndre Oppermann } 4769b932e9eSAndre Oppermann m->m_pkthdr.csum_flags |= 4779b932e9eSAndre Oppermann CSUM_IP_CHECKED | CSUM_IP_VALID; 4789b932e9eSAndre Oppermann 4799b932e9eSAndre Oppermann error = netisr_queue(NETISR_IP, m); 480f9e354dfSJulian Elischer goto done; 481f9e354dfSJulian Elischer } 4829b932e9eSAndre Oppermann /* Or forward to some other address? */ 4839b932e9eSAndre Oppermann fwd_tag = m_tag_find(m, PACKET_TAG_IPFORWARD, NULL); 4849b932e9eSAndre Oppermann if (fwd_tag) { 4859b932e9eSAndre Oppermann dst = (struct sockaddr_in *)&ro->ro_dst; 4869b932e9eSAndre Oppermann bcopy((fwd_tag+1), dst, sizeof(struct sockaddr_in)); 4879b932e9eSAndre Oppermann m->m_flags |= M_SKIP_FIREWALL; 4889b932e9eSAndre Oppermann m_tag_delete(m, fwd_tag); 4899b932e9eSAndre Oppermann goto again; 490099dd043SAndre Oppermann } 491099dd043SAndre Oppermann #endif /* IPFIREWALL_FORWARD */ 4922b25acc1SLuigi Rizzo 493c21fd232SAndre Oppermann passout: 49451c8ec4aSRuslan Ermilov /* 127/8 must not appear on wire - RFC1122. */ 49551c8ec4aSRuslan Ermilov if ((ntohl(ip->ip_dst.s_addr) >> IN_CLASSA_NSHIFT) == IN_LOOPBACKNET || 49651c8ec4aSRuslan Ermilov (ntohl(ip->ip_src.s_addr) >> IN_CLASSA_NSHIFT) == IN_LOOPBACKNET) { 49751c8ec4aSRuslan Ermilov if ((ifp->if_flags & IFF_LOOPBACK) == 0) { 49851c8ec4aSRuslan Ermilov ipstat.ips_badaddr++; 49951c8ec4aSRuslan Ermilov error = EADDRNOTAVAIL; 50051c8ec4aSRuslan Ermilov goto bad; 50151c8ec4aSRuslan Ermilov } 50251c8ec4aSRuslan Ermilov } 50351c8ec4aSRuslan Ermilov 504206a3274SRuslan Ermilov m->m_pkthdr.csum_flags |= CSUM_IP; 505206a3274SRuslan Ermilov sw_csum = m->m_pkthdr.csum_flags & ~ifp->if_hwassist; 506db4f9cc7SJonathan Lemon if (sw_csum & CSUM_DELAY_DATA) { 507db4f9cc7SJonathan Lemon in_delayed_cksum(m); 508db4f9cc7SJonathan Lemon sw_csum &= ~CSUM_DELAY_DATA; 509db4f9cc7SJonathan Lemon } 510206a3274SRuslan Ermilov m->m_pkthdr.csum_flags &= ifp->if_hwassist; 511db4f9cc7SJonathan Lemon 512e7319babSPoul-Henning Kamp /* 513db4f9cc7SJonathan Lemon * If small enough for interface, or the interface will take 514233dcce1SAndre Oppermann * care of the fragmentation for us, we can just send directly. 515df8bae1dSRodney W. Grimes */ 5163ae2ad08SJohn-Mark Gurney if (ip->ip_len <= mtu || 517233dcce1SAndre Oppermann (m->m_pkthdr.csum_flags & ifp->if_hwassist & CSUM_TSO) != 0 || 518233dcce1SAndre Oppermann ((ip->ip_off & IP_DF) == 0 && (ifp->if_hwassist & CSUM_FRAGMENT))) { 519fd8e4ebcSMike Barcroft ip->ip_len = htons(ip->ip_len); 520fd8e4ebcSMike Barcroft ip->ip_off = htons(ip->ip_off); 521df8bae1dSRodney W. Grimes ip->ip_sum = 0; 52253be11f6SPoul-Henning Kamp if (sw_csum & CSUM_DELAY_IP) 523df8bae1dSRodney W. Grimes ip->ip_sum = in_cksum(m, hlen); 5245da9f8faSJosef Karthauser 525233dcce1SAndre Oppermann /* 526233dcce1SAndre Oppermann * Record statistics for this interface address. 527233dcce1SAndre Oppermann * With CSUM_TSO the byte/packet count will be slightly 528233dcce1SAndre Oppermann * incorrect because we count the IP+TCP headers only 529233dcce1SAndre Oppermann * once instead of for every generated packet. 530233dcce1SAndre Oppermann */ 53138c1bc35SRuslan Ermilov if (!(flags & IP_FORWARDING) && ia) { 532233dcce1SAndre Oppermann if (m->m_pkthdr.csum_flags & CSUM_TSO) 5333dbee59bSBruce M Simpson ia->ia_ifa.if_opackets += 534233dcce1SAndre Oppermann m->m_pkthdr.len / m->m_pkthdr.tso_segsz; 535233dcce1SAndre Oppermann else 5363dbee59bSBruce M Simpson ia->ia_ifa.if_opackets++; 5373dbee59bSBruce M Simpson ia->ia_ifa.if_obytes += m->m_pkthdr.len; 5385da9f8faSJosef Karthauser } 53953dcc544SMike Silbersack #ifdef MBUF_STRESS_TEST 5403390d476SMike Silbersack if (mbuf_frag_size && m->m_pkthdr.len > mbuf_frag_size) 5413390d476SMike Silbersack m = m_fragment(m, M_DONTWAIT, mbuf_frag_size); 5429d9edc56SMike Silbersack #endif 543147f74d1SAndre Oppermann /* 544147f74d1SAndre Oppermann * Reset layer specific mbuf flags 545147f74d1SAndre Oppermann * to avoid confusing lower layers. 546147f74d1SAndre Oppermann */ 547147f74d1SAndre Oppermann m->m_flags &= ~(M_PROTOFLAGS); 548147f74d1SAndre Oppermann 549df8bae1dSRodney W. Grimes error = (*ifp->if_output)(ifp, m, 550df8bae1dSRodney W. Grimes (struct sockaddr *)dst, ro->ro_rt); 551df8bae1dSRodney W. Grimes goto done; 552df8bae1dSRodney W. Grimes } 5531e78ac21SJeffrey Hsu 554233dcce1SAndre Oppermann /* Balk when DF bit is set or the interface didn't support TSO. */ 555233dcce1SAndre Oppermann if ((ip->ip_off & IP_DF) || (m->m_pkthdr.csum_flags & CSUM_TSO)) { 556df8bae1dSRodney W. Grimes error = EMSGSIZE; 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 */ 5653ae2ad08SJohn-Mark Gurney error = ip_fragment(ip, &m, 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; 57107203494SDaniel C. Sobral if (error == 0) { 572fe937674SJosef Karthauser /* Record statistics for this interface address. */ 57307203494SDaniel C. Sobral if (ia != NULL) { 5743dbee59bSBruce M Simpson ia->ia_ifa.if_opackets++; 5753dbee59bSBruce M Simpson ia->ia_ifa.if_obytes += m->m_pkthdr.len; 57607203494SDaniel C. Sobral } 577147f74d1SAndre Oppermann /* 578147f74d1SAndre Oppermann * Reset layer specific mbuf flags 579147f74d1SAndre Oppermann * to avoid confusing upper layers. 580147f74d1SAndre Oppermann */ 581147f74d1SAndre Oppermann m->m_flags &= ~(M_PROTOFLAGS); 582fe937674SJosef Karthauser 583df8bae1dSRodney W. Grimes error = (*ifp->if_output)(ifp, m, 584df8bae1dSRodney W. Grimes (struct sockaddr *)dst, ro->ro_rt); 585fe937674SJosef Karthauser } else 586df8bae1dSRodney W. Grimes m_freem(m); 587df8bae1dSRodney W. Grimes } 588df8bae1dSRodney W. Grimes 589df8bae1dSRodney W. Grimes if (error == 0) 590df8bae1dSRodney W. Grimes ipstat.ips_fragmented++; 5911e78ac21SJeffrey Hsu 592df8bae1dSRodney W. Grimes done: 5936a800098SYoshinobu Inoue if (ro == &iproute && ro->ro_rt) { 5946a800098SYoshinobu Inoue RTFREE(ro->ro_rt); 595ac9d7e26SMax Laier } 596df8bae1dSRodney W. Grimes return (error); 597df8bae1dSRodney W. Grimes bad: 5983528d68fSBill Paul m_freem(m); 599df8bae1dSRodney W. Grimes goto done; 600df8bae1dSRodney W. Grimes } 601df8bae1dSRodney W. Grimes 6021e78ac21SJeffrey Hsu /* 6031e78ac21SJeffrey Hsu * Create a chain of fragments which fit the given mtu. m_frag points to the 6041e78ac21SJeffrey Hsu * mbuf to be fragmented; on return it points to the chain with the fragments. 6051e78ac21SJeffrey Hsu * Return 0 if no error. If error, m_frag may contain a partially built 6061e78ac21SJeffrey Hsu * chain of fragments that should be freed by the caller. 6071e78ac21SJeffrey Hsu * 6081e78ac21SJeffrey Hsu * if_hwassist_flags is the hw offload capabilities (see if_data.ifi_hwassist) 6091e78ac21SJeffrey Hsu * sw_csum contains the delayed checksums flags (e.g., CSUM_DELAY_IP). 6101e78ac21SJeffrey Hsu */ 6111e78ac21SJeffrey Hsu int 6121e78ac21SJeffrey Hsu ip_fragment(struct ip *ip, struct mbuf **m_frag, int mtu, 6131e78ac21SJeffrey Hsu u_long if_hwassist_flags, int sw_csum) 6141e78ac21SJeffrey Hsu { 6151e78ac21SJeffrey Hsu int error = 0; 6161e78ac21SJeffrey Hsu int hlen = ip->ip_hl << 2; 6171e78ac21SJeffrey Hsu int len = (mtu - hlen) & ~7; /* size of payload in each fragment */ 6181e78ac21SJeffrey Hsu int off; 6191e78ac21SJeffrey Hsu struct mbuf *m0 = *m_frag; /* the original packet */ 6201e78ac21SJeffrey Hsu int firstlen; 6211e78ac21SJeffrey Hsu struct mbuf **mnext; 6221e78ac21SJeffrey Hsu int nfrags; 6231e78ac21SJeffrey Hsu 6241e78ac21SJeffrey Hsu if (ip->ip_off & IP_DF) { /* Fragmentation not allowed */ 6251e78ac21SJeffrey Hsu ipstat.ips_cantfrag++; 6261e78ac21SJeffrey Hsu return EMSGSIZE; 6271e78ac21SJeffrey Hsu } 6281e78ac21SJeffrey Hsu 6291e78ac21SJeffrey Hsu /* 6301e78ac21SJeffrey Hsu * Must be able to put at least 8 bytes per fragment. 6311e78ac21SJeffrey Hsu */ 6321e78ac21SJeffrey Hsu if (len < 8) 6331e78ac21SJeffrey Hsu return EMSGSIZE; 6341e78ac21SJeffrey Hsu 6351e78ac21SJeffrey Hsu /* 6361e78ac21SJeffrey Hsu * If the interface will not calculate checksums on 6371e78ac21SJeffrey Hsu * fragmented packets, then do it here. 6381e78ac21SJeffrey Hsu */ 6391e78ac21SJeffrey Hsu if (m0->m_pkthdr.csum_flags & CSUM_DELAY_DATA && 6401e78ac21SJeffrey Hsu (if_hwassist_flags & CSUM_IP_FRAGS) == 0) { 6411e78ac21SJeffrey Hsu in_delayed_cksum(m0); 6421e78ac21SJeffrey Hsu m0->m_pkthdr.csum_flags &= ~CSUM_DELAY_DATA; 6431e78ac21SJeffrey Hsu } 6441e78ac21SJeffrey Hsu 6451e78ac21SJeffrey Hsu if (len > PAGE_SIZE) { 6461e78ac21SJeffrey Hsu /* 6471e78ac21SJeffrey Hsu * Fragment large datagrams such that each segment 6481e78ac21SJeffrey Hsu * contains a multiple of PAGE_SIZE amount of data, 6491e78ac21SJeffrey Hsu * plus headers. This enables a receiver to perform 6501e78ac21SJeffrey Hsu * page-flipping zero-copy optimizations. 6511e78ac21SJeffrey Hsu * 6521e78ac21SJeffrey Hsu * XXX When does this help given that sender and receiver 6531e78ac21SJeffrey Hsu * could have different page sizes, and also mtu could 6541e78ac21SJeffrey Hsu * be less than the receiver's page size ? 6551e78ac21SJeffrey Hsu */ 6561e78ac21SJeffrey Hsu int newlen; 6571e78ac21SJeffrey Hsu struct mbuf *m; 6581e78ac21SJeffrey Hsu 6591e78ac21SJeffrey Hsu for (m = m0, off = 0; m && (off+m->m_len) <= mtu; m = m->m_next) 6601e78ac21SJeffrey Hsu off += m->m_len; 6611e78ac21SJeffrey Hsu 6621e78ac21SJeffrey Hsu /* 6631e78ac21SJeffrey Hsu * firstlen (off - hlen) must be aligned on an 6641e78ac21SJeffrey Hsu * 8-byte boundary 6651e78ac21SJeffrey Hsu */ 6661e78ac21SJeffrey Hsu if (off < hlen) 6671e78ac21SJeffrey Hsu goto smart_frag_failure; 6681e78ac21SJeffrey Hsu off = ((off - hlen) & ~7) + hlen; 6691e78ac21SJeffrey Hsu newlen = (~PAGE_MASK) & mtu; 6701e78ac21SJeffrey Hsu if ((newlen + sizeof (struct ip)) > mtu) { 6711e78ac21SJeffrey Hsu /* we failed, go back the default */ 6721e78ac21SJeffrey Hsu smart_frag_failure: 6731e78ac21SJeffrey Hsu newlen = len; 6741e78ac21SJeffrey Hsu off = hlen + len; 6751e78ac21SJeffrey Hsu } 6761e78ac21SJeffrey Hsu len = newlen; 6771e78ac21SJeffrey Hsu 6781e78ac21SJeffrey Hsu } else { 6791e78ac21SJeffrey Hsu off = hlen + len; 6801e78ac21SJeffrey Hsu } 6811e78ac21SJeffrey Hsu 6821e78ac21SJeffrey Hsu firstlen = off - hlen; 6831e78ac21SJeffrey Hsu mnext = &m0->m_nextpkt; /* pointer to next packet */ 6841e78ac21SJeffrey Hsu 6851e78ac21SJeffrey Hsu /* 6861e78ac21SJeffrey Hsu * Loop through length of segment after first fragment, 6871e78ac21SJeffrey Hsu * make new header and copy data of each part and link onto chain. 6881e78ac21SJeffrey Hsu * Here, m0 is the original packet, m is the fragment being created. 6891e78ac21SJeffrey Hsu * The fragments are linked off the m_nextpkt of the original 6901e78ac21SJeffrey Hsu * packet, which after processing serves as the first fragment. 6911e78ac21SJeffrey Hsu */ 6921e78ac21SJeffrey Hsu for (nfrags = 1; off < ip->ip_len; off += len, nfrags++) { 6931e78ac21SJeffrey Hsu struct ip *mhip; /* ip header on the fragment */ 6941e78ac21SJeffrey Hsu struct mbuf *m; 6951e78ac21SJeffrey Hsu int mhlen = sizeof (struct ip); 6961e78ac21SJeffrey Hsu 69734333b16SAndre Oppermann MGETHDR(m, M_DONTWAIT, MT_DATA); 6980b17fba7SAndre Oppermann if (m == NULL) { 6991e78ac21SJeffrey Hsu error = ENOBUFS; 7001e78ac21SJeffrey Hsu ipstat.ips_odropped++; 7011e78ac21SJeffrey Hsu goto done; 7021e78ac21SJeffrey Hsu } 7031e78ac21SJeffrey Hsu m->m_flags |= (m0->m_flags & M_MCAST) | M_FRAG; 7041e78ac21SJeffrey Hsu /* 7051e78ac21SJeffrey Hsu * In the first mbuf, leave room for the link header, then 7061e78ac21SJeffrey Hsu * copy the original IP header including options. The payload 7071e78ac21SJeffrey Hsu * goes into an additional mbuf chain returned by m_copy(). 7081e78ac21SJeffrey Hsu */ 7091e78ac21SJeffrey Hsu m->m_data += max_linkhdr; 7101e78ac21SJeffrey Hsu mhip = mtod(m, struct ip *); 7111e78ac21SJeffrey Hsu *mhip = *ip; 7121e78ac21SJeffrey Hsu if (hlen > sizeof (struct ip)) { 7131e78ac21SJeffrey Hsu mhlen = ip_optcopy(ip, mhip) + sizeof (struct ip); 7141e78ac21SJeffrey Hsu mhip->ip_v = IPVERSION; 7151e78ac21SJeffrey Hsu mhip->ip_hl = mhlen >> 2; 7161e78ac21SJeffrey Hsu } 7171e78ac21SJeffrey Hsu m->m_len = mhlen; 7181e78ac21SJeffrey Hsu /* XXX do we need to add ip->ip_off below ? */ 7191e78ac21SJeffrey Hsu mhip->ip_off = ((off - hlen) >> 3) + ip->ip_off; 7201e78ac21SJeffrey Hsu if (off + len >= ip->ip_len) { /* last fragment */ 7211e78ac21SJeffrey Hsu len = ip->ip_len - off; 7221e78ac21SJeffrey Hsu m->m_flags |= M_LASTFRAG; 7231e78ac21SJeffrey Hsu } else 7241e78ac21SJeffrey Hsu mhip->ip_off |= IP_MF; 7251e78ac21SJeffrey Hsu mhip->ip_len = htons((u_short)(len + mhlen)); 7261e78ac21SJeffrey Hsu m->m_next = m_copy(m0, off, len); 7270b17fba7SAndre Oppermann if (m->m_next == NULL) { /* copy failed */ 7281e78ac21SJeffrey Hsu m_free(m); 7291e78ac21SJeffrey Hsu error = ENOBUFS; /* ??? */ 7301e78ac21SJeffrey Hsu ipstat.ips_odropped++; 7311e78ac21SJeffrey Hsu goto done; 7321e78ac21SJeffrey Hsu } 7331e78ac21SJeffrey Hsu m->m_pkthdr.len = mhlen + len; 734fc74a9f9SBrooks Davis m->m_pkthdr.rcvif = NULL; 7351e78ac21SJeffrey Hsu #ifdef MAC 7361e78ac21SJeffrey Hsu mac_create_fragment(m0, m); 7371e78ac21SJeffrey Hsu #endif 7381e78ac21SJeffrey Hsu m->m_pkthdr.csum_flags = m0->m_pkthdr.csum_flags; 7391e78ac21SJeffrey Hsu mhip->ip_off = htons(mhip->ip_off); 7401e78ac21SJeffrey Hsu mhip->ip_sum = 0; 7411e78ac21SJeffrey Hsu if (sw_csum & CSUM_DELAY_IP) 7421e78ac21SJeffrey Hsu mhip->ip_sum = in_cksum(m, mhlen); 7431e78ac21SJeffrey Hsu *mnext = m; 7441e78ac21SJeffrey Hsu mnext = &m->m_nextpkt; 7451e78ac21SJeffrey Hsu } 7461e78ac21SJeffrey Hsu ipstat.ips_ofragments += nfrags; 7471e78ac21SJeffrey Hsu 7481e78ac21SJeffrey Hsu /* set first marker for fragment chain */ 7491e78ac21SJeffrey Hsu m0->m_flags |= M_FIRSTFRAG | M_FRAG; 7501e78ac21SJeffrey Hsu m0->m_pkthdr.csum_data = nfrags; 7511e78ac21SJeffrey Hsu 7521e78ac21SJeffrey Hsu /* 7531e78ac21SJeffrey Hsu * Update first fragment by trimming what's been copied out 7541e78ac21SJeffrey Hsu * and updating header. 7551e78ac21SJeffrey Hsu */ 7561e78ac21SJeffrey Hsu m_adj(m0, hlen + firstlen - ip->ip_len); 7571e78ac21SJeffrey Hsu m0->m_pkthdr.len = hlen + firstlen; 7581e78ac21SJeffrey Hsu ip->ip_len = htons((u_short)m0->m_pkthdr.len); 7591e78ac21SJeffrey Hsu ip->ip_off |= IP_MF; 7601e78ac21SJeffrey Hsu ip->ip_off = htons(ip->ip_off); 7611e78ac21SJeffrey Hsu ip->ip_sum = 0; 7621e78ac21SJeffrey Hsu if (sw_csum & CSUM_DELAY_IP) 7631e78ac21SJeffrey Hsu ip->ip_sum = in_cksum(m0, hlen); 7641e78ac21SJeffrey Hsu 7651e78ac21SJeffrey Hsu done: 7661e78ac21SJeffrey Hsu *m_frag = m0; 7671e78ac21SJeffrey Hsu return error; 7681e78ac21SJeffrey Hsu } 7691e78ac21SJeffrey Hsu 7701c238475SJonathan Lemon void 771db4f9cc7SJonathan Lemon in_delayed_cksum(struct mbuf *m) 772db4f9cc7SJonathan Lemon { 773db4f9cc7SJonathan Lemon struct ip *ip; 774db4f9cc7SJonathan Lemon u_short csum, offset; 775db4f9cc7SJonathan Lemon 776db4f9cc7SJonathan Lemon ip = mtod(m, struct ip *); 77753be11f6SPoul-Henning Kamp offset = ip->ip_hl << 2 ; 778db4f9cc7SJonathan Lemon csum = in_cksum_skip(m, ip->ip_len, offset); 779206a3274SRuslan Ermilov if (m->m_pkthdr.csum_flags & CSUM_UDP && csum == 0) 780206a3274SRuslan Ermilov csum = 0xffff; 781db4f9cc7SJonathan Lemon offset += m->m_pkthdr.csum_data; /* checksum offset */ 782db4f9cc7SJonathan Lemon 783db4f9cc7SJonathan Lemon if (offset + sizeof(u_short) > m->m_len) { 784db4f9cc7SJonathan Lemon printf("delayed m_pullup, m->len: %d off: %d p: %d\n", 785db4f9cc7SJonathan Lemon m->m_len, offset, ip->ip_p); 786db4f9cc7SJonathan Lemon /* 787db4f9cc7SJonathan Lemon * XXX 788db4f9cc7SJonathan Lemon * this shouldn't happen, but if it does, the 789db4f9cc7SJonathan Lemon * correct behavior may be to insert the checksum 7908f8d29f6SAndre Oppermann * in the appropriate next mbuf in the chain. 791db4f9cc7SJonathan Lemon */ 7928f8d29f6SAndre Oppermann return; 793db4f9cc7SJonathan Lemon } 794db4f9cc7SJonathan Lemon *(u_short *)(m->m_data + offset) = csum; 795db4f9cc7SJonathan Lemon } 796db4f9cc7SJonathan Lemon 797df8bae1dSRodney W. Grimes /* 798df8bae1dSRodney W. Grimes * IP socket option processing. 799df8bae1dSRodney W. Grimes */ 800df8bae1dSRodney W. Grimes int 801f2565d68SRobert Watson ip_ctloutput(struct socket *so, struct sockopt *sopt) 802df8bae1dSRodney W. Grimes { 803cfe8b629SGarrett Wollman struct inpcb *inp = sotoinpcb(so); 804cfe8b629SGarrett Wollman int error, optval; 805df8bae1dSRodney W. Grimes 806cfe8b629SGarrett Wollman error = optval = 0; 807cfe8b629SGarrett Wollman if (sopt->sopt_level != IPPROTO_IP) { 808cfe8b629SGarrett Wollman return (EINVAL); 809cfe8b629SGarrett Wollman } 810df8bae1dSRodney W. Grimes 811cfe8b629SGarrett Wollman switch (sopt->sopt_dir) { 812cfe8b629SGarrett Wollman case SOPT_SET: 813cfe8b629SGarrett Wollman switch (sopt->sopt_name) { 814df8bae1dSRodney W. Grimes case IP_OPTIONS: 815df8bae1dSRodney W. Grimes #ifdef notyet 816df8bae1dSRodney W. Grimes case IP_RETOPTS: 817df8bae1dSRodney W. Grimes #endif 818cfe8b629SGarrett Wollman { 819cfe8b629SGarrett Wollman struct mbuf *m; 820cfe8b629SGarrett Wollman if (sopt->sopt_valsize > MLEN) { 821cfe8b629SGarrett Wollman error = EMSGSIZE; 822cfe8b629SGarrett Wollman break; 823cfe8b629SGarrett Wollman } 824e0aec682SAndre Oppermann MGET(m, sopt->sopt_td ? M_TRYWAIT : M_DONTWAIT, MT_DATA); 8250b17fba7SAndre Oppermann if (m == NULL) { 826cfe8b629SGarrett Wollman error = ENOBUFS; 827cfe8b629SGarrett Wollman break; 828cfe8b629SGarrett Wollman } 829cfe8b629SGarrett Wollman m->m_len = sopt->sopt_valsize; 830cfe8b629SGarrett Wollman error = sooptcopyin(sopt, mtod(m, char *), m->m_len, 831cfe8b629SGarrett Wollman m->m_len); 832635354c4SMaxim Konovalov if (error) { 833635354c4SMaxim Konovalov m_free(m); 834635354c4SMaxim Konovalov break; 835635354c4SMaxim Konovalov } 836993d9505SRobert Watson INP_LOCK(inp); 837993d9505SRobert Watson error = ip_pcbopts(inp, sopt->sopt_name, m); 838993d9505SRobert Watson INP_UNLOCK(inp); 839993d9505SRobert Watson return (error); 840cfe8b629SGarrett Wollman } 841df8bae1dSRodney W. Grimes 842df8bae1dSRodney W. Grimes case IP_TOS: 843df8bae1dSRodney W. Grimes case IP_TTL: 844936cd18dSAndre Oppermann case IP_MINTTL: 845df8bae1dSRodney W. Grimes case IP_RECVOPTS: 846df8bae1dSRodney W. Grimes case IP_RECVRETOPTS: 847df8bae1dSRodney W. Grimes case IP_RECVDSTADDR: 8484957466bSMatthew N. Dodd case IP_RECVTTL: 84982c23ebaSBill Fenner case IP_RECVIF: 8506a800098SYoshinobu Inoue case IP_FAITH: 8518afa2304SBruce M Simpson case IP_ONESBCAST: 852b2828ad2SAndre Oppermann case IP_DONTFRAG: 853cfe8b629SGarrett Wollman error = sooptcopyin(sopt, &optval, sizeof optval, 854cfe8b629SGarrett Wollman sizeof optval); 855cfe8b629SGarrett Wollman if (error) 856cfe8b629SGarrett Wollman break; 857df8bae1dSRodney W. Grimes 858cfe8b629SGarrett Wollman switch (sopt->sopt_name) { 859df8bae1dSRodney W. Grimes case IP_TOS: 860ca98b82cSDavid Greenman inp->inp_ip_tos = optval; 861df8bae1dSRodney W. Grimes break; 862df8bae1dSRodney W. Grimes 863df8bae1dSRodney W. Grimes case IP_TTL: 864ca98b82cSDavid Greenman inp->inp_ip_ttl = optval; 865df8bae1dSRodney W. Grimes break; 866936cd18dSAndre Oppermann 867936cd18dSAndre Oppermann case IP_MINTTL: 868936cd18dSAndre Oppermann if (optval > 0 && optval <= MAXTTL) 869936cd18dSAndre Oppermann inp->inp_ip_minttl = optval; 870936cd18dSAndre Oppermann else 871936cd18dSAndre Oppermann error = EINVAL; 872936cd18dSAndre Oppermann break; 873936cd18dSAndre Oppermann 874a138d217SRobert Watson #define OPTSET(bit) do { \ 875a138d217SRobert Watson INP_LOCK(inp); \ 876df8bae1dSRodney W. Grimes if (optval) \ 877df8bae1dSRodney W. Grimes inp->inp_flags |= bit; \ 878df8bae1dSRodney W. Grimes else \ 879a138d217SRobert Watson inp->inp_flags &= ~bit; \ 880a138d217SRobert Watson INP_UNLOCK(inp); \ 881a138d217SRobert Watson } while (0) 882df8bae1dSRodney W. Grimes 883df8bae1dSRodney W. Grimes case IP_RECVOPTS: 884df8bae1dSRodney W. Grimes OPTSET(INP_RECVOPTS); 885df8bae1dSRodney W. Grimes break; 886df8bae1dSRodney W. Grimes 887df8bae1dSRodney W. Grimes case IP_RECVRETOPTS: 888df8bae1dSRodney W. Grimes OPTSET(INP_RECVRETOPTS); 889df8bae1dSRodney W. Grimes break; 890df8bae1dSRodney W. Grimes 891df8bae1dSRodney W. Grimes case IP_RECVDSTADDR: 892df8bae1dSRodney W. Grimes OPTSET(INP_RECVDSTADDR); 893df8bae1dSRodney W. Grimes break; 89482c23ebaSBill Fenner 8954957466bSMatthew N. Dodd case IP_RECVTTL: 8964957466bSMatthew N. Dodd OPTSET(INP_RECVTTL); 8974957466bSMatthew N. Dodd break; 8984957466bSMatthew N. Dodd 89982c23ebaSBill Fenner case IP_RECVIF: 90082c23ebaSBill Fenner OPTSET(INP_RECVIF); 90182c23ebaSBill Fenner break; 9026a800098SYoshinobu Inoue 9036a800098SYoshinobu Inoue case IP_FAITH: 9046a800098SYoshinobu Inoue OPTSET(INP_FAITH); 9056a800098SYoshinobu Inoue break; 9068afa2304SBruce M Simpson 9078afa2304SBruce M Simpson case IP_ONESBCAST: 9088afa2304SBruce M Simpson OPTSET(INP_ONESBCAST); 9098afa2304SBruce M Simpson break; 910b2828ad2SAndre Oppermann case IP_DONTFRAG: 911b2828ad2SAndre Oppermann OPTSET(INP_DONTFRAG); 912b2828ad2SAndre Oppermann break; 913df8bae1dSRodney W. Grimes } 914df8bae1dSRodney W. Grimes break; 915df8bae1dSRodney W. Grimes #undef OPTSET 916df8bae1dSRodney W. Grimes 91771498f30SBruce M Simpson /* 91871498f30SBruce M Simpson * Multicast socket options are processed by the in_mcast 91971498f30SBruce M Simpson * module. 92071498f30SBruce M Simpson */ 921df8bae1dSRodney W. Grimes case IP_MULTICAST_IF: 922f0068c4aSGarrett Wollman case IP_MULTICAST_VIF: 923df8bae1dSRodney W. Grimes case IP_MULTICAST_TTL: 924df8bae1dSRodney W. Grimes case IP_MULTICAST_LOOP: 925df8bae1dSRodney W. Grimes case IP_ADD_MEMBERSHIP: 926df8bae1dSRodney W. Grimes case IP_DROP_MEMBERSHIP: 92771498f30SBruce M Simpson case IP_ADD_SOURCE_MEMBERSHIP: 92871498f30SBruce M Simpson case IP_DROP_SOURCE_MEMBERSHIP: 92971498f30SBruce M Simpson case IP_BLOCK_SOURCE: 93071498f30SBruce M Simpson case IP_UNBLOCK_SOURCE: 93171498f30SBruce M Simpson case IP_MSFILTER: 93271498f30SBruce M Simpson case MCAST_JOIN_GROUP: 93371498f30SBruce M Simpson case MCAST_LEAVE_GROUP: 93471498f30SBruce M Simpson case MCAST_JOIN_SOURCE_GROUP: 93571498f30SBruce M Simpson case MCAST_LEAVE_SOURCE_GROUP: 93671498f30SBruce M Simpson case MCAST_BLOCK_SOURCE: 93771498f30SBruce M Simpson case MCAST_UNBLOCK_SOURCE: 93871498f30SBruce M Simpson error = inp_setmoptions(inp, sopt); 939df8bae1dSRodney W. Grimes break; 940df8bae1dSRodney W. Grimes 94133b3ac06SPeter Wemm case IP_PORTRANGE: 942cfe8b629SGarrett Wollman error = sooptcopyin(sopt, &optval, sizeof optval, 943cfe8b629SGarrett Wollman sizeof optval); 944cfe8b629SGarrett Wollman if (error) 945cfe8b629SGarrett Wollman break; 94633b3ac06SPeter Wemm 947a138d217SRobert Watson INP_LOCK(inp); 94833b3ac06SPeter Wemm switch (optval) { 94933b3ac06SPeter Wemm case IP_PORTRANGE_DEFAULT: 95033b3ac06SPeter Wemm inp->inp_flags &= ~(INP_LOWPORT); 95133b3ac06SPeter Wemm inp->inp_flags &= ~(INP_HIGHPORT); 95233b3ac06SPeter Wemm break; 95333b3ac06SPeter Wemm 95433b3ac06SPeter Wemm case IP_PORTRANGE_HIGH: 95533b3ac06SPeter Wemm inp->inp_flags &= ~(INP_LOWPORT); 95633b3ac06SPeter Wemm inp->inp_flags |= INP_HIGHPORT; 95733b3ac06SPeter Wemm break; 95833b3ac06SPeter Wemm 95933b3ac06SPeter Wemm case IP_PORTRANGE_LOW: 96033b3ac06SPeter Wemm inp->inp_flags &= ~(INP_HIGHPORT); 96133b3ac06SPeter Wemm inp->inp_flags |= INP_LOWPORT; 96233b3ac06SPeter Wemm break; 96333b3ac06SPeter Wemm 96433b3ac06SPeter Wemm default: 96533b3ac06SPeter Wemm error = EINVAL; 96633b3ac06SPeter Wemm break; 96733b3ac06SPeter Wemm } 968a138d217SRobert Watson INP_UNLOCK(inp); 969ce8c72b1SPeter Wemm break; 97033b3ac06SPeter Wemm 971b2630c29SGeorge V. Neville-Neil #ifdef IPSEC 9726a800098SYoshinobu Inoue case IP_IPSEC_POLICY: 9736a800098SYoshinobu Inoue { 9746a800098SYoshinobu Inoue caddr_t req; 975686cdd19SJun-ichiro itojun Hagino size_t len = 0; 9766a800098SYoshinobu Inoue int priv; 9776a800098SYoshinobu Inoue struct mbuf *m; 9786a800098SYoshinobu Inoue int optname; 9796a800098SYoshinobu Inoue 9806a800098SYoshinobu Inoue if ((error = soopt_getm(sopt, &m)) != 0) /* XXX */ 9816a800098SYoshinobu Inoue break; 9826a800098SYoshinobu Inoue if ((error = soopt_mcopyin(sopt, m)) != 0) /* XXX */ 9836a800098SYoshinobu Inoue break; 984acd3428bSRobert Watson if (sopt->sopt_td != NULL) { 985acd3428bSRobert Watson /* 986acd3428bSRobert Watson * XXXRW: Would be more desirable to do this 987acd3428bSRobert Watson * one layer down so that we only exercise 988acd3428bSRobert Watson * privilege if it is needed. 989acd3428bSRobert Watson */ 990acd3428bSRobert Watson error = priv_check(sopt->sopt_td, 991acd3428bSRobert Watson PRIV_NETINET_IPSEC); 992acd3428bSRobert Watson if (error) 993acd3428bSRobert Watson priv = 0; 994acd3428bSRobert Watson else 995acd3428bSRobert Watson priv = 1; 996acd3428bSRobert Watson } else 997acd3428bSRobert Watson priv = 1; 9986a800098SYoshinobu Inoue req = mtod(m, caddr_t); 999686cdd19SJun-ichiro itojun Hagino len = m->m_len; 10006a800098SYoshinobu Inoue optname = sopt->sopt_name; 1001686cdd19SJun-ichiro itojun Hagino error = ipsec4_set_policy(inp, optname, req, len, priv); 10026a800098SYoshinobu Inoue m_freem(m); 10036a800098SYoshinobu Inoue break; 10046a800098SYoshinobu Inoue } 1005b2630c29SGeorge V. Neville-Neil #endif /* IPSEC */ 10066a800098SYoshinobu Inoue 1007df8bae1dSRodney W. Grimes default: 1008df8bae1dSRodney W. Grimes error = ENOPROTOOPT; 1009df8bae1dSRodney W. Grimes break; 1010df8bae1dSRodney W. Grimes } 1011df8bae1dSRodney W. Grimes break; 1012df8bae1dSRodney W. Grimes 1013cfe8b629SGarrett Wollman case SOPT_GET: 1014cfe8b629SGarrett Wollman switch (sopt->sopt_name) { 1015df8bae1dSRodney W. Grimes case IP_OPTIONS: 1016df8bae1dSRodney W. Grimes case IP_RETOPTS: 1017cfe8b629SGarrett Wollman if (inp->inp_options) 1018cfe8b629SGarrett Wollman error = sooptcopyout(sopt, 1019cfe8b629SGarrett Wollman mtod(inp->inp_options, 1020cfe8b629SGarrett Wollman char *), 1021cfe8b629SGarrett Wollman inp->inp_options->m_len); 1022cfe8b629SGarrett Wollman else 1023cfe8b629SGarrett Wollman sopt->sopt_valsize = 0; 1024df8bae1dSRodney W. Grimes break; 1025df8bae1dSRodney W. Grimes 1026df8bae1dSRodney W. Grimes case IP_TOS: 1027df8bae1dSRodney W. Grimes case IP_TTL: 1028936cd18dSAndre Oppermann case IP_MINTTL: 1029df8bae1dSRodney W. Grimes case IP_RECVOPTS: 1030df8bae1dSRodney W. Grimes case IP_RECVRETOPTS: 1031df8bae1dSRodney W. Grimes case IP_RECVDSTADDR: 10324957466bSMatthew N. Dodd case IP_RECVTTL: 103382c23ebaSBill Fenner case IP_RECVIF: 1034cfe8b629SGarrett Wollman case IP_PORTRANGE: 10356a800098SYoshinobu Inoue case IP_FAITH: 10368afa2304SBruce M Simpson case IP_ONESBCAST: 1037b2828ad2SAndre Oppermann case IP_DONTFRAG: 1038cfe8b629SGarrett Wollman switch (sopt->sopt_name) { 1039df8bae1dSRodney W. Grimes 1040df8bae1dSRodney W. Grimes case IP_TOS: 1041ca98b82cSDavid Greenman optval = inp->inp_ip_tos; 1042df8bae1dSRodney W. Grimes break; 1043df8bae1dSRodney W. Grimes 1044df8bae1dSRodney W. Grimes case IP_TTL: 1045ca98b82cSDavid Greenman optval = inp->inp_ip_ttl; 1046df8bae1dSRodney W. Grimes break; 1047df8bae1dSRodney W. Grimes 1048936cd18dSAndre Oppermann case IP_MINTTL: 1049936cd18dSAndre Oppermann optval = inp->inp_ip_minttl; 1050936cd18dSAndre Oppermann break; 1051936cd18dSAndre Oppermann 1052df8bae1dSRodney W. Grimes #define OPTBIT(bit) (inp->inp_flags & bit ? 1 : 0) 1053df8bae1dSRodney W. Grimes 1054df8bae1dSRodney W. Grimes case IP_RECVOPTS: 1055df8bae1dSRodney W. Grimes optval = OPTBIT(INP_RECVOPTS); 1056df8bae1dSRodney W. Grimes break; 1057df8bae1dSRodney W. Grimes 1058df8bae1dSRodney W. Grimes case IP_RECVRETOPTS: 1059df8bae1dSRodney W. Grimes optval = OPTBIT(INP_RECVRETOPTS); 1060df8bae1dSRodney W. Grimes break; 1061df8bae1dSRodney W. Grimes 1062df8bae1dSRodney W. Grimes case IP_RECVDSTADDR: 1063df8bae1dSRodney W. Grimes optval = OPTBIT(INP_RECVDSTADDR); 1064df8bae1dSRodney W. Grimes break; 106582c23ebaSBill Fenner 10664957466bSMatthew N. Dodd case IP_RECVTTL: 10674957466bSMatthew N. Dodd optval = OPTBIT(INP_RECVTTL); 10684957466bSMatthew N. Dodd break; 10694957466bSMatthew N. Dodd 107082c23ebaSBill Fenner case IP_RECVIF: 107182c23ebaSBill Fenner optval = OPTBIT(INP_RECVIF); 107282c23ebaSBill Fenner break; 1073cfe8b629SGarrett Wollman 1074cfe8b629SGarrett Wollman case IP_PORTRANGE: 1075cfe8b629SGarrett Wollman if (inp->inp_flags & INP_HIGHPORT) 1076cfe8b629SGarrett Wollman optval = IP_PORTRANGE_HIGH; 1077cfe8b629SGarrett Wollman else if (inp->inp_flags & INP_LOWPORT) 1078cfe8b629SGarrett Wollman optval = IP_PORTRANGE_LOW; 1079cfe8b629SGarrett Wollman else 1080cfe8b629SGarrett Wollman optval = 0; 1081cfe8b629SGarrett Wollman break; 10826a800098SYoshinobu Inoue 10836a800098SYoshinobu Inoue case IP_FAITH: 10846a800098SYoshinobu Inoue optval = OPTBIT(INP_FAITH); 10856a800098SYoshinobu Inoue break; 10868afa2304SBruce M Simpson 10878afa2304SBruce M Simpson case IP_ONESBCAST: 10888afa2304SBruce M Simpson optval = OPTBIT(INP_ONESBCAST); 10898afa2304SBruce M Simpson break; 1090b2828ad2SAndre Oppermann case IP_DONTFRAG: 1091b2828ad2SAndre Oppermann optval = OPTBIT(INP_DONTFRAG); 1092b2828ad2SAndre Oppermann break; 1093df8bae1dSRodney W. Grimes } 1094cfe8b629SGarrett Wollman error = sooptcopyout(sopt, &optval, sizeof optval); 1095df8bae1dSRodney W. Grimes break; 1096df8bae1dSRodney W. Grimes 109771498f30SBruce M Simpson /* 109871498f30SBruce M Simpson * Multicast socket options are processed by the in_mcast 109971498f30SBruce M Simpson * module. 110071498f30SBruce M Simpson */ 1101df8bae1dSRodney W. Grimes case IP_MULTICAST_IF: 1102f0068c4aSGarrett Wollman case IP_MULTICAST_VIF: 1103df8bae1dSRodney W. Grimes case IP_MULTICAST_TTL: 1104df8bae1dSRodney W. Grimes case IP_MULTICAST_LOOP: 110571498f30SBruce M Simpson case IP_MSFILTER: 110671498f30SBruce M Simpson error = inp_getmoptions(inp, sopt); 110733b3ac06SPeter Wemm break; 110833b3ac06SPeter Wemm 1109b2630c29SGeorge V. Neville-Neil #ifdef IPSEC 11106a800098SYoshinobu Inoue case IP_IPSEC_POLICY: 11116a800098SYoshinobu Inoue { 1112f63e7634SYoshinobu Inoue struct mbuf *m = NULL; 11136a800098SYoshinobu Inoue caddr_t req = NULL; 1114686cdd19SJun-ichiro itojun Hagino size_t len = 0; 11156a800098SYoshinobu Inoue 1116686cdd19SJun-ichiro itojun Hagino if (m != 0) { 11176a800098SYoshinobu Inoue req = mtod(m, caddr_t); 1118686cdd19SJun-ichiro itojun Hagino len = m->m_len; 1119686cdd19SJun-ichiro itojun Hagino } 1120686cdd19SJun-ichiro itojun Hagino error = ipsec4_get_policy(sotoinpcb(so), req, len, &m); 11216a800098SYoshinobu Inoue if (error == 0) 11226a800098SYoshinobu Inoue error = soopt_mcopyout(sopt, m); /* XXX */ 1123f63e7634SYoshinobu Inoue if (error == 0) 11246a800098SYoshinobu Inoue m_freem(m); 11256a800098SYoshinobu Inoue break; 11266a800098SYoshinobu Inoue } 1127b2630c29SGeorge V. Neville-Neil #endif /* IPSEC */ 11286a800098SYoshinobu Inoue 1129df8bae1dSRodney W. Grimes default: 1130df8bae1dSRodney W. Grimes error = ENOPROTOOPT; 1131df8bae1dSRodney W. Grimes break; 1132df8bae1dSRodney W. Grimes } 1133df8bae1dSRodney W. Grimes break; 1134df8bae1dSRodney W. Grimes } 1135df8bae1dSRodney W. Grimes return (error); 1136df8bae1dSRodney W. Grimes } 1137df8bae1dSRodney W. Grimes 1138df8bae1dSRodney W. Grimes /* 1139df8bae1dSRodney W. Grimes * Routine called from ip_output() to loop back a copy of an IP multicast 1140df8bae1dSRodney W. Grimes * packet to the input queue of a specified interface. Note that this 1141df8bae1dSRodney W. Grimes * calls the output routine of the loopback "driver", but with an interface 1142f5fea3ddSPaul Traina * pointer that might NOT be a loopback interface -- evil, but easier than 1143f5fea3ddSPaul Traina * replicating that code here. 1144df8bae1dSRodney W. Grimes */ 1145df8bae1dSRodney W. Grimes static void 1146f2565d68SRobert Watson ip_mloopback(struct ifnet *ifp, struct mbuf *m, struct sockaddr_in *dst, 1147f2565d68SRobert Watson int hlen) 1148df8bae1dSRodney W. Grimes { 1149b375c9ecSLuigi Rizzo register struct ip *ip; 1150df8bae1dSRodney W. Grimes struct mbuf *copym; 1151df8bae1dSRodney W. Grimes 1152b375c9ecSLuigi Rizzo copym = m_copy(m, 0, M_COPYALL); 115386b1d6d2SBill Fenner if (copym != NULL && (copym->m_flags & M_EXT || copym->m_len < hlen)) 115486b1d6d2SBill Fenner copym = m_pullup(copym, hlen); 1155df8bae1dSRodney W. Grimes if (copym != NULL) { 1156390cdc6aSRuslan Ermilov /* If needed, compute the checksum and mark it as valid. */ 1157390cdc6aSRuslan Ermilov if (copym->m_pkthdr.csum_flags & CSUM_DELAY_DATA) { 1158390cdc6aSRuslan Ermilov in_delayed_cksum(copym); 1159390cdc6aSRuslan Ermilov copym->m_pkthdr.csum_flags &= ~CSUM_DELAY_DATA; 1160390cdc6aSRuslan Ermilov copym->m_pkthdr.csum_flags |= 1161390cdc6aSRuslan Ermilov CSUM_DATA_VALID | CSUM_PSEUDO_HDR; 1162390cdc6aSRuslan Ermilov copym->m_pkthdr.csum_data = 0xffff; 1163390cdc6aSRuslan Ermilov } 1164df8bae1dSRodney W. Grimes /* 1165df8bae1dSRodney W. Grimes * We don't bother to fragment if the IP length is greater 1166df8bae1dSRodney W. Grimes * than the interface's MTU. Can this possibly matter? 1167df8bae1dSRodney W. Grimes */ 1168df8bae1dSRodney W. Grimes ip = mtod(copym, struct ip *); 1169fd8e4ebcSMike Barcroft ip->ip_len = htons(ip->ip_len); 1170fd8e4ebcSMike Barcroft ip->ip_off = htons(ip->ip_off); 1171df8bae1dSRodney W. Grimes ip->ip_sum = 0; 117286b1d6d2SBill Fenner ip->ip_sum = in_cksum(copym, hlen); 11739c9137eaSGarrett Wollman /* 11749c9137eaSGarrett Wollman * NB: 1175e1596dffSBill Fenner * It's not clear whether there are any lingering 1176e1596dffSBill Fenner * reentrancy problems in other areas which might 1177e1596dffSBill Fenner * be exposed by using ip_input directly (in 1178e1596dffSBill Fenner * particular, everything which modifies the packet 1179e1596dffSBill Fenner * in-place). Yet another option is using the 1180e1596dffSBill Fenner * protosw directly to deliver the looped back 1181e1596dffSBill Fenner * packet. For the moment, we'll err on the side 1182ed7509acSJulian Elischer * of safety by using if_simloop(). 11839c9137eaSGarrett Wollman */ 1184201c2527SJulian Elischer #if 1 /* XXX */ 1185201c2527SJulian Elischer if (dst->sin_family != AF_INET) { 11862b8a366cSJulian Elischer printf("ip_mloopback: bad address family %d\n", 1187201c2527SJulian Elischer dst->sin_family); 1188201c2527SJulian Elischer dst->sin_family = AF_INET; 1189201c2527SJulian Elischer } 1190201c2527SJulian Elischer #endif 1191201c2527SJulian Elischer 11929c9137eaSGarrett Wollman #ifdef notdef 1193e1596dffSBill Fenner copym->m_pkthdr.rcvif = ifp; 1194ed7509acSJulian Elischer ip_input(copym); 11959c9137eaSGarrett Wollman #else 119606a429a3SArchie Cobbs if_simloop(ifp, copym, dst->sin_family, 0); 11979c9137eaSGarrett Wollman #endif 1198df8bae1dSRodney W. Grimes } 1199df8bae1dSRodney W. Grimes } 1200