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 35*5d6d7e75SGleb Smirnoff #include "opt_inet.h" 361f7e052cSJulian Elischer #include "opt_ipfw.h" 376a800098SYoshinobu Inoue #include "opt_ipsec.h" 3853dcc544SMike Silbersack #include "opt_mbuf_stress_test.h" 39e440aed9SQing Li #include "opt_mpath.h" 4057f60867SMark Johnston #include "opt_route.h" 412f4afd21SRandall Stewart #include "opt_sctp.h" 42fbd1372aSJoerg Wunsch 43df8bae1dSRodney W. Grimes #include <sys/param.h> 4426f9a767SRodney W. Grimes #include <sys/systm.h> 45f0f6d643SLuigi Rizzo #include <sys/kernel.h> 46df8bae1dSRodney W. Grimes #include <sys/malloc.h> 47df8bae1dSRodney W. Grimes #include <sys/mbuf.h> 48acd3428bSRobert Watson #include <sys/priv.h> 49c26fe973SBjoern A. Zeeb #include <sys/proc.h> 50df8bae1dSRodney W. Grimes #include <sys/protosw.h> 5157f60867SMark Johnston #include <sys/sdt.h> 52df8bae1dSRodney W. Grimes #include <sys/socket.h> 53df8bae1dSRodney W. Grimes #include <sys/socketvar.h> 549d9edc56SMike Silbersack #include <sys/sysctl.h> 55c26fe973SBjoern A. Zeeb #include <sys/ucred.h> 56df8bae1dSRodney W. Grimes 57df8bae1dSRodney W. Grimes #include <net/if.h> 5876039bc8SGleb Smirnoff #include <net/if_var.h> 593ef5e21dSQing Li #include <net/if_llatbl.h> 609b932e9eSAndre Oppermann #include <net/netisr.h> 61c21fd232SAndre Oppermann #include <net/pfil.h> 62df8bae1dSRodney W. Grimes #include <net/route.h> 6365111ec7SKip Macy #include <net/flowtable.h> 64e440aed9SQing Li #ifdef RADIX_MPATH 65e440aed9SQing Li #include <net/radix_mpath.h> 66e440aed9SQing Li #endif 674b79449eSBjoern A. Zeeb #include <net/vnet.h> 68df8bae1dSRodney W. Grimes 69df8bae1dSRodney W. Grimes #include <netinet/in.h> 7057f60867SMark Johnston #include <netinet/in_kdtrace.h> 71df8bae1dSRodney W. Grimes #include <netinet/in_systm.h> 72df8bae1dSRodney W. Grimes #include <netinet/ip.h> 73df8bae1dSRodney W. Grimes #include <netinet/in_pcb.h> 74df8bae1dSRodney W. Grimes #include <netinet/in_var.h> 75df8bae1dSRodney W. Grimes #include <netinet/ip_var.h> 76ef39adf0SAndre Oppermann #include <netinet/ip_options.h> 772f4afd21SRandall Stewart #ifdef SCTP 782f4afd21SRandall Stewart #include <netinet/sctp.h> 792f4afd21SRandall Stewart #include <netinet/sctp_crc32.h> 802f4afd21SRandall Stewart #endif 81df8bae1dSRodney W. Grimes 82b2630c29SGeorge V. Neville-Neil #ifdef IPSEC 832cb64cb2SGeorge V. Neville-Neil #include <netinet/ip_ipsec.h> 841dfcf0d2SAndre Oppermann #include <netipsec/ipsec.h> 85b2630c29SGeorge V. Neville-Neil #endif /* IPSEC*/ 866a800098SYoshinobu Inoue 871dfcf0d2SAndre Oppermann #include <machine/in_cksum.h> 881dfcf0d2SAndre Oppermann 89aed55708SRobert Watson #include <security/mac/mac_framework.h> 90aed55708SRobert Watson 91eddfbb76SRobert Watson VNET_DEFINE(u_short, ip_id); 92f23b4c91SGarrett Wollman 9353dcc544SMike Silbersack #ifdef MBUF_STRESS_TEST 9405b9d121SBjoern A. Zeeb static int mbuf_frag_size = 0; 959d9edc56SMike Silbersack SYSCTL_INT(_net_inet_ip, OID_AUTO, mbuf_frag_size, CTLFLAG_RW, 969d9edc56SMike Silbersack &mbuf_frag_size, 0, "Fragment outgoing mbufs to this size"); 979d9edc56SMike Silbersack #endif 989d9edc56SMike Silbersack 99df8bae1dSRodney W. Grimes static void ip_mloopback 1004d77a549SAlfred Perlstein (struct ifnet *, struct mbuf *, struct sockaddr_in *, int); 101afed1b49SDarren Reed 102afed1b49SDarren Reed 1038b889dbbSBruce M Simpson extern int in_mcast_loop; 10493e0e116SJulian Elischer extern struct protosw inetsw[]; 10593e0e116SJulian Elischer 106df8bae1dSRodney W. Grimes /* 107df8bae1dSRodney W. Grimes * IP output. The packet in mbuf chain m contains a skeletal IP 108df8bae1dSRodney W. Grimes * header (with len, off, ttl, proto, tos, src, dst). 109df8bae1dSRodney W. Grimes * The mbuf chain containing the packet will be freed. 110df8bae1dSRodney W. Grimes * The mbuf opt, if present, will not be freed. 111bf984051SGleb Smirnoff * If route ro is present and has ro_rt initialized, route lookup would be 112bf984051SGleb Smirnoff * skipped and ro->ro_rt would be used. If ro is present but ro->ro_rt is NULL, 113bf984051SGleb Smirnoff * then result of route lookup is stored in ro->ro_rt. 114bf984051SGleb Smirnoff * 1153de758d3SRobert Watson * In the IP forwarding case, the packet will arrive with options already 1163de758d3SRobert Watson * inserted, so must have a NULL opt pointer. 117df8bae1dSRodney W. Grimes */ 118df8bae1dSRodney W. Grimes int 119f2565d68SRobert Watson ip_output(struct mbuf *m, struct mbuf *opt, struct route *ro, int flags, 120f2565d68SRobert Watson struct ip_moptions *imo, struct inpcb *inp) 121df8bae1dSRodney W. Grimes { 1221e78ac21SJeffrey Hsu struct ip *ip; 123fc74d005SBjoern A. Zeeb struct ifnet *ifp = NULL; /* keep compiler happy */ 124ac9d7e26SMax Laier struct mbuf *m0; 12523bf9953SPoul-Henning Kamp int hlen = sizeof (struct ip); 1263ae2ad08SJohn-Mark Gurney int mtu; 127ca8b83b0SLuigi Rizzo int error = 0; 128ca8b83b0SLuigi Rizzo struct sockaddr_in *dst; 1294c7a6059SGleb Smirnoff const struct sockaddr_in *gw; 1303c73180fSGleb Smirnoff struct in_ifaddr *ia; 13121d172a3SGleb Smirnoff int isbroadcast; 132078468edSGleb Smirnoff uint16_t ip_len, ip_off; 133e3f406b3SRuslan Ermilov struct route iproute; 134ec396e61SLuigi Rizzo struct rtentry *rte; /* cache for ro->ro_rt */ 1359b932e9eSAndre Oppermann struct in_addr odst; 1369b932e9eSAndre Oppermann struct m_tag *fwd_tag = NULL; 1371a499816SEdward Tomasz Napierala #ifdef IPSEC 1381a499816SEdward Tomasz Napierala int no_route_but_check_spd = 0; 1391a499816SEdward Tomasz Napierala #endif 140ac9d7e26SMax Laier M_ASSERTPKTHDR(m); 14136e8826fSMax Laier 142bc97ba51SJulian Elischer if (inp != NULL) { 143baa45840SRobert Watson INP_LOCK_ASSERT(inp); 14462e1ba83SRobert Watson M_SETFIB(m, inp->inp_inc.inc_fibnum); 14580cb9f21SKip Macy if (inp->inp_flags & (INP_HW_FLOWID|INP_SW_FLOWID)) { 14680cb9f21SKip Macy m->m_pkthdr.flowid = inp->inp_flowid; 14780cb9f21SKip Macy m->m_flags |= M_FLOWID; 14880cb9f21SKip Macy } 149bc97ba51SJulian Elischer } 15062e1ba83SRobert Watson 15162e1ba83SRobert Watson if (ro == NULL) { 15262e1ba83SRobert Watson ro = &iproute; 15362e1ba83SRobert Watson bzero(ro, sizeof (*ro)); 154bf984051SGleb Smirnoff } 15562e1ba83SRobert Watson 15653be8fcaSBjoern A. Zeeb #ifdef FLOWTABLE 157bf984051SGleb Smirnoff if (ro->ro_rt == NULL) { 158d4121a02SKip Macy struct flentry *fle; 159d4121a02SKip Macy 16062e1ba83SRobert Watson /* 16162e1ba83SRobert Watson * The flow table returns route entries valid for up to 30 16262e1ba83SRobert Watson * seconds; we rely on the remainder of ip_output() taking no 16362e1ba83SRobert Watson * longer than that long for the stability of ro_rt. The 16462e1ba83SRobert Watson * flow ID assignment must have happened before this point. 16562e1ba83SRobert Watson */ 166*5d6d7e75SGleb Smirnoff fle = flowtable_lookup(AF_INET, m); 167bf984051SGleb Smirnoff if (fle != NULL) 168d4121a02SKip Macy flow_to_route(fle, ro); 169d4121a02SKip Macy } 17053be8fcaSBjoern A. Zeeb #endif 1712b25acc1SLuigi Rizzo 172df8bae1dSRodney W. Grimes if (opt) { 173ca8b83b0SLuigi Rizzo int len = 0; 174df8bae1dSRodney W. Grimes m = ip_insertoptions(m, opt, &len); 175cb7641e8SMaxim Konovalov if (len != 0) 176ca8b83b0SLuigi Rizzo hlen = len; /* ip->ip_hl is updated above */ 177df8bae1dSRodney W. Grimes } 178df8bae1dSRodney W. Grimes ip = mtod(m, struct ip *); 1798f134647SGleb Smirnoff ip_len = ntohs(ip->ip_len); 1808f134647SGleb Smirnoff ip_off = ntohs(ip->ip_off); 1813efc3014SJulian Elischer 182df8bae1dSRodney W. Grimes /* 1836e49b1feSGarrett Wollman * Fill in IP header. If we are not allowing fragmentation, 184e0f630eaSAndre Oppermann * then the ip_id field is meaningless, but we don't set it 185e0f630eaSAndre Oppermann * to zero. Doing so causes various problems when devices along 186e0f630eaSAndre Oppermann * the path (routers, load balancers, firewalls, etc.) illegally 187e0f630eaSAndre Oppermann * disable DF on our packet. Note that a 16-bit counter 1886e49b1feSGarrett Wollman * will wrap around in less than 10 seconds at 100 Mbit/s on a 1896e49b1feSGarrett Wollman * medium with MTU 1500. See Steven M. Bellovin, "A Technique 1906e49b1feSGarrett Wollman * for Counting NATted Hosts", Proc. IMW'02, available at 1914d09f5a0SGleb Smirnoff * <http://www.cs.columbia.edu/~smb/papers/fnat.pdf>. 192df8bae1dSRodney W. Grimes */ 193df8bae1dSRodney W. Grimes if ((flags & (IP_FORWARDING|IP_RAWOUTPUT)) == 0) { 19453be11f6SPoul-Henning Kamp ip->ip_v = IPVERSION; 19553be11f6SPoul-Henning Kamp ip->ip_hl = hlen >> 2; 1961f44b0a1SDavid Malone ip->ip_id = ip_newid(); 19786425c62SRobert Watson IPSTAT_INC(ips_localout); 198df8bae1dSRodney W. Grimes } else { 199ca8b83b0SLuigi Rizzo /* Header already set, fetch hlen from there */ 20053be11f6SPoul-Henning Kamp hlen = ip->ip_hl << 2; 201df8bae1dSRodney W. Grimes } 2029c9137eaSGarrett Wollman 203054692a4SAlexander V. Chernikov /* 204054692a4SAlexander V. Chernikov * dst/gw handling: 205054692a4SAlexander V. Chernikov * 2063c065f2fSGleb Smirnoff * dst can be rewritten but always points to &ro->ro_dst. 2073c065f2fSGleb Smirnoff * gw is readonly but can point either to dst OR rt_gateway, 2083c065f2fSGleb Smirnoff * therefore we need restore gw if we're redoing lookup. 209054692a4SAlexander V. Chernikov */ 2104c7a6059SGleb Smirnoff gw = dst = (struct sockaddr_in *)&ro->ro_dst; 2119b932e9eSAndre Oppermann again: 2123c73180fSGleb Smirnoff ia = NULL; 213df8bae1dSRodney W. Grimes /* 2143c065f2fSGleb Smirnoff * If there is a cached route, check that it is to the same 2153c065f2fSGleb Smirnoff * destination and is still up. If not, free it and try again. 2163c065f2fSGleb Smirnoff * The address family should also be checked in case of sharing 2173c065f2fSGleb Smirnoff * the cache with IPv6. 218df8bae1dSRodney W. Grimes */ 219ec396e61SLuigi Rizzo rte = ro->ro_rt; 220ec396e61SLuigi Rizzo if (rte && ((rte->rt_flags & RTF_UP) == 0 || 221c7ea0aa6SQing Li rte->rt_ifp == NULL || 222c7ea0aa6SQing Li !RT_LINK_IS_UP(rte->rt_ifp) || 223a4a6e773SHajimu UMEMOTO dst->sin_family != AF_INET || 2249b932e9eSAndre Oppermann dst->sin_addr.s_addr != ip->ip_dst.s_addr)) { 225bf984051SGleb Smirnoff RO_RTFREE(ro); 226bf984051SGleb Smirnoff ro->ro_lle = NULL; 227bf984051SGleb Smirnoff rte = NULL; 228054692a4SAlexander V. Chernikov gw = dst; 229df8bae1dSRodney W. Grimes } 230ec396e61SLuigi Rizzo if (rte == NULL && fwd_tag == NULL) { 231a4a6e773SHajimu UMEMOTO bzero(dst, sizeof(*dst)); 232df8bae1dSRodney W. Grimes dst->sin_family = AF_INET; 233df8bae1dSRodney W. Grimes dst->sin_len = sizeof(*dst); 2349b932e9eSAndre Oppermann dst->sin_addr = ip->ip_dst; 235df8bae1dSRodney W. Grimes } 236df8bae1dSRodney W. Grimes /* 237a3fd02d8SBruce M Simpson * If routing to interface only, short circuit routing lookup. 238a3fd02d8SBruce M Simpson * The use of an all-ones broadcast address implies this; an 239a3fd02d8SBruce M Simpson * interface is specified by the broadcast address of an interface, 240a3fd02d8SBruce M Simpson * or the destination address of a ptp interface. 241df8bae1dSRodney W. Grimes */ 242a3fd02d8SBruce M Simpson if (flags & IP_SENDONES) { 243a3fd02d8SBruce M Simpson if ((ia = ifatoia(ifa_ifwithbroadaddr(sintosa(dst)))) == NULL && 244a3fd02d8SBruce M Simpson (ia = ifatoia(ifa_ifwithdstaddr(sintosa(dst)))) == NULL) { 24586425c62SRobert Watson IPSTAT_INC(ips_noroute); 246a3fd02d8SBruce M Simpson error = ENETUNREACH; 247a3fd02d8SBruce M Simpson goto bad; 248a3fd02d8SBruce M Simpson } 249a3fd02d8SBruce M Simpson ip->ip_dst.s_addr = INADDR_BROADCAST; 250a3fd02d8SBruce M Simpson dst->sin_addr = ip->ip_dst; 251a3fd02d8SBruce M Simpson ifp = ia->ia_ifp; 252a3fd02d8SBruce M Simpson ip->ip_ttl = 1; 253a3fd02d8SBruce M Simpson isbroadcast = 1; 254a3fd02d8SBruce M Simpson } else if (flags & IP_ROUTETOIF) { 2550b17fba7SAndre Oppermann if ((ia = ifatoia(ifa_ifwithdstaddr(sintosa(dst)))) == NULL && 2560ed6142bSQing Li (ia = ifatoia(ifa_ifwithnet(sintosa(dst), 0))) == NULL) { 25786425c62SRobert Watson IPSTAT_INC(ips_noroute); 258df8bae1dSRodney W. Grimes error = ENETUNREACH; 259df8bae1dSRodney W. Grimes goto bad; 260df8bae1dSRodney W. Grimes } 261df8bae1dSRodney W. Grimes ifp = ia->ia_ifp; 262df8bae1dSRodney W. Grimes ip->ip_ttl = 1; 2639f9b3dc4SGarrett Wollman isbroadcast = in_broadcast(dst->sin_addr, ifp); 2643afefa39SDaniel C. Sobral } else if (IN_MULTICAST(ntohl(ip->ip_dst.s_addr)) && 26538c1bc35SRuslan Ermilov imo != NULL && imo->imo_multicast_ifp != NULL) { 2663afefa39SDaniel C. Sobral /* 26738c1bc35SRuslan Ermilov * Bypass the normal routing lookup for multicast 26838c1bc35SRuslan Ermilov * packets if the interface is specified. 2693afefa39SDaniel C. Sobral */ 27038c1bc35SRuslan Ermilov ifp = imo->imo_multicast_ifp; 27138c1bc35SRuslan Ermilov IFP_TO_IA(ifp, ia); 27238c1bc35SRuslan Ermilov isbroadcast = 0; /* fool gcc */ 273df8bae1dSRodney W. Grimes } else { 2742c17fe93SGarrett Wollman /* 27597d8d152SAndre Oppermann * We want to do any cloning requested by the link layer, 27697d8d152SAndre Oppermann * as this is probably required in all cases for correct 27797d8d152SAndre Oppermann * operation (as it is for ARP). 2782c17fe93SGarrett Wollman */ 279ec396e61SLuigi Rizzo if (rte == NULL) { 280e440aed9SQing Li #ifdef RADIX_MPATH 2818b07e49aSJulian Elischer rtalloc_mpath_fib(ro, 2828b07e49aSJulian Elischer ntohl(ip->ip_src.s_addr ^ ip->ip_dst.s_addr), 2838b07e49aSJulian Elischer inp ? inp->inp_inc.inc_fibnum : M_GETFIB(m)); 284e440aed9SQing Li #else 2858b07e49aSJulian Elischer in_rtalloc_ign(ro, 0, 2868b07e49aSJulian Elischer inp ? inp->inp_inc.inc_fibnum : M_GETFIB(m)); 287e440aed9SQing Li #endif 288ec396e61SLuigi Rizzo rte = ro->ro_rt; 289ec396e61SLuigi Rizzo } 290c7ea0aa6SQing Li if (rte == NULL || 291c7ea0aa6SQing Li rte->rt_ifp == NULL || 292c7ea0aa6SQing Li !RT_LINK_IS_UP(rte->rt_ifp)) { 2931a499816SEdward Tomasz Napierala #ifdef IPSEC 2941a499816SEdward Tomasz Napierala /* 2951a499816SEdward Tomasz Napierala * There is no route for this packet, but it is 2961a499816SEdward Tomasz Napierala * possible that a matching SPD entry exists. 2971a499816SEdward Tomasz Napierala */ 2981a499816SEdward Tomasz Napierala no_route_but_check_spd = 1; 2991a499816SEdward Tomasz Napierala mtu = 0; /* Silence GCC warning. */ 3001a499816SEdward Tomasz Napierala goto sendit; 3011a499816SEdward Tomasz Napierala #endif 30286425c62SRobert Watson IPSTAT_INC(ips_noroute); 303df8bae1dSRodney W. Grimes error = EHOSTUNREACH; 304df8bae1dSRodney W. Grimes goto bad; 305df8bae1dSRodney W. Grimes } 306ec396e61SLuigi Rizzo ia = ifatoia(rte->rt_ifa); 3078c0fec80SRobert Watson ifa_ref(&ia->ia_ifa); 308ec396e61SLuigi Rizzo ifp = rte->rt_ifp; 309ec396e61SLuigi Rizzo rte->rt_rmx.rmx_pksent++; 310ec396e61SLuigi Rizzo if (rte->rt_flags & RTF_GATEWAY) 3114c7a6059SGleb Smirnoff gw = (struct sockaddr_in *)rte->rt_gateway; 312ec396e61SLuigi Rizzo if (rte->rt_flags & RTF_HOST) 313ec396e61SLuigi Rizzo isbroadcast = (rte->rt_flags & RTF_BROADCAST); 3149f9b3dc4SGarrett Wollman else 3154c7a6059SGleb Smirnoff isbroadcast = in_broadcast(gw->sin_addr, ifp); 316df8bae1dSRodney W. Grimes } 3173ae2ad08SJohn-Mark Gurney /* 3183ae2ad08SJohn-Mark Gurney * Calculate MTU. If we have a route that is up, use that, 3193ae2ad08SJohn-Mark Gurney * otherwise use the interface's MTU. 3203ae2ad08SJohn-Mark Gurney */ 321ec396e61SLuigi Rizzo if (rte != NULL && (rte->rt_flags & (RTF_UP|RTF_HOST))) { 3223ae2ad08SJohn-Mark Gurney /* 3233ae2ad08SJohn-Mark Gurney * This case can happen if the user changed the MTU 3243ae2ad08SJohn-Mark Gurney * of an interface after enabling IP on it. Because 3253ae2ad08SJohn-Mark Gurney * most netifs don't keep track of routes pointing to 3263ae2ad08SJohn-Mark Gurney * them, there is no way for one to update all its 3273ae2ad08SJohn-Mark Gurney * routes when the MTU is changed. 3283ae2ad08SJohn-Mark Gurney */ 329ec396e61SLuigi Rizzo if (rte->rt_rmx.rmx_mtu > ifp->if_mtu) 330ec396e61SLuigi Rizzo rte->rt_rmx.rmx_mtu = ifp->if_mtu; 331ec396e61SLuigi Rizzo mtu = rte->rt_rmx.rmx_mtu; 3323ae2ad08SJohn-Mark Gurney } else { 3333ae2ad08SJohn-Mark Gurney mtu = ifp->if_mtu; 3343ae2ad08SJohn-Mark Gurney } 335c744cde4SBjoern A. Zeeb /* Catch a possible divide by zero later. */ 336c744cde4SBjoern A. Zeeb KASSERT(mtu > 0, ("%s: mtu %d <= 0, rte=%p (rt_flags=0x%08x) ifp=%p", 337c744cde4SBjoern A. Zeeb __func__, mtu, rte, (rte != NULL) ? rte->rt_flags : 0, ifp)); 3389b932e9eSAndre Oppermann if (IN_MULTICAST(ntohl(ip->ip_dst.s_addr))) { 339df8bae1dSRodney W. Grimes m->m_flags |= M_MCAST; 340df8bae1dSRodney W. Grimes /* 341183e1c86SGleb Smirnoff * IP destination address is multicast. Make sure "gw" 342183e1c86SGleb Smirnoff * still points to the address in "ro". (It may have been 343183e1c86SGleb Smirnoff * changed to point to a gateway address, above.) 344183e1c86SGleb Smirnoff */ 345183e1c86SGleb Smirnoff gw = dst; 346183e1c86SGleb Smirnoff /* 347df8bae1dSRodney W. Grimes * See if the caller provided any multicast options 348df8bae1dSRodney W. Grimes */ 349df8bae1dSRodney W. Grimes if (imo != NULL) { 350df8bae1dSRodney W. Grimes ip->ip_ttl = imo->imo_multicast_ttl; 3511c5de19aSGarrett Wollman if (imo->imo_multicast_vif != -1) 3521c5de19aSGarrett Wollman ip->ip_src.s_addr = 353bbb4330bSLuigi Rizzo ip_mcast_src ? 354bbb4330bSLuigi Rizzo ip_mcast_src(imo->imo_multicast_vif) : 355bbb4330bSLuigi Rizzo INADDR_ANY; 356df8bae1dSRodney W. Grimes } else 357df8bae1dSRodney W. Grimes ip->ip_ttl = IP_DEFAULT_MULTICAST_TTL; 358df8bae1dSRodney W. Grimes /* 359df8bae1dSRodney W. Grimes * Confirm that the outgoing interface supports multicast. 360df8bae1dSRodney W. Grimes */ 3611c5de19aSGarrett Wollman if ((imo == NULL) || (imo->imo_multicast_vif == -1)) { 362df8bae1dSRodney W. Grimes if ((ifp->if_flags & IFF_MULTICAST) == 0) { 36386425c62SRobert Watson IPSTAT_INC(ips_noroute); 364df8bae1dSRodney W. Grimes error = ENETUNREACH; 365df8bae1dSRodney W. Grimes goto bad; 366df8bae1dSRodney W. Grimes } 3671c5de19aSGarrett Wollman } 368df8bae1dSRodney W. Grimes /* 369df8bae1dSRodney W. Grimes * If source address not specified yet, use address 370df8bae1dSRodney W. Grimes * of outgoing interface. 371df8bae1dSRodney W. Grimes */ 372df8bae1dSRodney W. Grimes if (ip->ip_src.s_addr == INADDR_ANY) { 37338c1bc35SRuslan Ermilov /* Interface may have no addresses. */ 37438c1bc35SRuslan Ermilov if (ia != NULL) 37538c1bc35SRuslan Ermilov ip->ip_src = IA_SIN(ia)->sin_addr; 376df8bae1dSRodney W. Grimes } 377df8bae1dSRodney W. Grimes 3788b889dbbSBruce M Simpson if ((imo == NULL && in_mcast_loop) || 3798b889dbbSBruce M Simpson (imo && imo->imo_multicast_loop)) { 380df8bae1dSRodney W. Grimes /* 3818b889dbbSBruce M Simpson * Loop back multicast datagram if not expressly 3828b889dbbSBruce M Simpson * forbidden to do so, even if we are not a member 3838b889dbbSBruce M Simpson * of the group; ip_input() will filter it later, 3848b889dbbSBruce M Simpson * thus deferring a hash lookup and mutex acquisition 3858b889dbbSBruce M Simpson * at the expense of a cheap copy using m_copym(). 386df8bae1dSRodney W. Grimes */ 38786b1d6d2SBill Fenner ip_mloopback(ifp, m, dst, hlen); 3888b889dbbSBruce M Simpson } else { 389df8bae1dSRodney W. Grimes /* 390df8bae1dSRodney W. Grimes * If we are acting as a multicast router, perform 391df8bae1dSRodney W. Grimes * multicast forwarding as if the packet had just 392df8bae1dSRodney W. Grimes * arrived on the interface to which we are about 393df8bae1dSRodney W. Grimes * to send. The multicast forwarding function 394df8bae1dSRodney W. Grimes * recursively calls this function, using the 395df8bae1dSRodney W. Grimes * IP_FORWARDING flag to prevent infinite recursion. 396df8bae1dSRodney W. Grimes * 397df8bae1dSRodney W. Grimes * Multicasts that are looped back by ip_mloopback(), 398df8bae1dSRodney W. Grimes * above, will be forwarded by the ip_input() routine, 399df8bae1dSRodney W. Grimes * if necessary. 400df8bae1dSRodney W. Grimes */ 401603724d3SBjoern A. Zeeb if (V_ip_mrouter && (flags & IP_FORWARDING) == 0) { 402f0068c4aSGarrett Wollman /* 403bbb4330bSLuigi Rizzo * If rsvp daemon is not running, do not 404f0068c4aSGarrett Wollman * set ip_moptions. This ensures that the packet 405f0068c4aSGarrett Wollman * is multicast and not just sent down one link 406f0068c4aSGarrett Wollman * as prescribed by rsvpd. 407f0068c4aSGarrett Wollman */ 408603724d3SBjoern A. Zeeb if (!V_rsvp_on) 409f0068c4aSGarrett Wollman imo = NULL; 410bbb4330bSLuigi Rizzo if (ip_mforward && 411bbb4330bSLuigi Rizzo ip_mforward(ip, ifp, m, imo) != 0) { 412df8bae1dSRodney W. Grimes m_freem(m); 413df8bae1dSRodney W. Grimes goto done; 414df8bae1dSRodney W. Grimes } 415df8bae1dSRodney W. Grimes } 416df8bae1dSRodney W. Grimes } 4175e9ae478SGarrett Wollman 418df8bae1dSRodney W. Grimes /* 419df8bae1dSRodney W. Grimes * Multicasts with a time-to-live of zero may be looped- 420df8bae1dSRodney W. Grimes * back, above, but must not be transmitted on a network. 421df8bae1dSRodney W. Grimes * Also, multicasts addressed to the loopback interface 422df8bae1dSRodney W. Grimes * are not sent -- the above call to ip_mloopback() will 4238b889dbbSBruce M Simpson * loop back a copy. ip_input() will drop the copy if 4248b889dbbSBruce M Simpson * this host does not belong to the destination group on 4258b889dbbSBruce M Simpson * the loopback interface. 426df8bae1dSRodney W. Grimes */ 427f5fea3ddSPaul Traina if (ip->ip_ttl == 0 || ifp->if_flags & IFF_LOOPBACK) { 428df8bae1dSRodney W. Grimes m_freem(m); 429df8bae1dSRodney W. Grimes goto done; 430df8bae1dSRodney W. Grimes } 431df8bae1dSRodney W. Grimes 432df8bae1dSRodney W. Grimes goto sendit; 433df8bae1dSRodney W. Grimes } 4346a7c943cSAndre Oppermann 435df8bae1dSRodney W. Grimes /* 4362b25acc1SLuigi Rizzo * If the source address is not specified yet, use the address 437cb459254SJohn-Mark Gurney * of the outoing interface. 438df8bae1dSRodney W. Grimes */ 439f9e354dfSJulian Elischer if (ip->ip_src.s_addr == INADDR_ANY) { 44038c1bc35SRuslan Ermilov /* Interface may have no addresses. */ 44138c1bc35SRuslan Ermilov if (ia != NULL) { 442df8bae1dSRodney W. Grimes ip->ip_src = IA_SIN(ia)->sin_addr; 443f9e354dfSJulian Elischer } 44438c1bc35SRuslan Ermilov } 4456a7c943cSAndre Oppermann 446ca7a789aSMax Laier /* 447ac7e1212SAdrian Chadd * Both in the SMP world, pre-emption world if_transmit() world, 448ac7e1212SAdrian Chadd * the following code doesn't really function as intended any further. 449ac7e1212SAdrian Chadd * 450ac7e1212SAdrian Chadd * + There can and will be multiple CPUs running this code path 451ac7e1212SAdrian Chadd * in parallel, and we do no lock holding when checking the 452ac7e1212SAdrian Chadd * queue depth; 453ac7e1212SAdrian Chadd * + And since other threads can be running concurrently, even if 454ac7e1212SAdrian Chadd * we do pass this check, another thread may queue some frames 455ac7e1212SAdrian Chadd * before this thread does and it will end up partially or fully 456ac7e1212SAdrian Chadd * failing to send anyway; 457ac7e1212SAdrian Chadd * + if_transmit() based drivers don't necessarily set ifq_len 458ac7e1212SAdrian Chadd * at all. 459ac7e1212SAdrian Chadd * 460ac7e1212SAdrian Chadd * This should be replaced with a method of pushing an entire list 461ac7e1212SAdrian Chadd * of fragment frames to the driver and have the driver decide 462ac7e1212SAdrian Chadd * whether it can queue or not queue the entire set. 463ac7e1212SAdrian Chadd */ 464ac7e1212SAdrian Chadd #if 0 465ac7e1212SAdrian Chadd /* 466ca7a789aSMax Laier * Verify that we have any chance at all of being able to queue the 467ca7a789aSMax Laier * packet or packet fragments, unless ALTQ is enabled on the given 468ca7a789aSMax Laier * interface in which case packetdrop should be done by queueing. 469ca7a789aSMax Laier */ 4708f134647SGleb Smirnoff n = ip_len / mtu + 1; /* how many fragments ? */ 471ca8b83b0SLuigi Rizzo if ( 47202b199f1SMax Laier #ifdef ALTQ 473ca8b83b0SLuigi Rizzo (!ALTQ_IS_ENABLED(&ifp->if_snd)) && 474ca7a789aSMax Laier #endif /* ALTQ */ 475ca8b83b0SLuigi Rizzo (ifp->if_snd.ifq_len + n) >= ifp->if_snd.ifq_maxlen ) { 476b5390296SDavid Greenman error = ENOBUFS; 47786425c62SRobert Watson IPSTAT_INC(ips_odropped); 478ca8b83b0SLuigi Rizzo ifp->if_snd.ifq_drops += n; 479b5390296SDavid Greenman goto bad; 480b5390296SDavid Greenman } 481ac7e1212SAdrian Chadd #endif 482b5390296SDavid Greenman 483b5390296SDavid Greenman /* 484df8bae1dSRodney W. Grimes * Look for broadcast address and 4858c3f5566SRuslan Ermilov * verify user is allowed to send 486df8bae1dSRodney W. Grimes * such a packet. 487df8bae1dSRodney W. Grimes */ 4889f9b3dc4SGarrett Wollman if (isbroadcast) { 489df8bae1dSRodney W. Grimes if ((ifp->if_flags & IFF_BROADCAST) == 0) { 490df8bae1dSRodney W. Grimes error = EADDRNOTAVAIL; 491df8bae1dSRodney W. Grimes goto bad; 492df8bae1dSRodney W. Grimes } 493df8bae1dSRodney W. Grimes if ((flags & IP_ALLOWBROADCAST) == 0) { 494df8bae1dSRodney W. Grimes error = EACCES; 495df8bae1dSRodney W. Grimes goto bad; 496df8bae1dSRodney W. Grimes } 497df8bae1dSRodney W. Grimes /* don't allow broadcast messages to be fragmented */ 4988f134647SGleb Smirnoff if (ip_len > mtu) { 499df8bae1dSRodney W. Grimes error = EMSGSIZE; 500df8bae1dSRodney W. Grimes goto bad; 501df8bae1dSRodney W. Grimes } 502df8bae1dSRodney W. Grimes m->m_flags |= M_BCAST; 5039f9b3dc4SGarrett Wollman } else { 504df8bae1dSRodney W. Grimes m->m_flags &= ~M_BCAST; 5059f9b3dc4SGarrett Wollman } 506df8bae1dSRodney W. Grimes 507f1743588SDarren Reed sendit: 508b2630c29SGeorge V. Neville-Neil #ifdef IPSEC 5094f7418a0SEdward Tomasz Napierala switch(ip_ipsec_output(&m, inp, &flags, &error)) { 5101dfcf0d2SAndre Oppermann case 1: 51133841545SHajimu UMEMOTO goto bad; 5121dfcf0d2SAndre Oppermann case -1: 5131dfcf0d2SAndre Oppermann goto done; 5141dfcf0d2SAndre Oppermann case 0: 51533841545SHajimu UMEMOTO default: 5161dfcf0d2SAndre Oppermann break; /* Continue with packet processing. */ 51733841545SHajimu UMEMOTO } 5181a499816SEdward Tomasz Napierala /* 5191a499816SEdward Tomasz Napierala * Check if there was a route for this packet; return error if not. 5201a499816SEdward Tomasz Napierala */ 5211a499816SEdward Tomasz Napierala if (no_route_but_check_spd) { 5221a499816SEdward Tomasz Napierala IPSTAT_INC(ips_noroute); 5231a499816SEdward Tomasz Napierala error = EHOSTUNREACH; 5241a499816SEdward Tomasz Napierala goto bad; 5251a499816SEdward Tomasz Napierala } 5261dfcf0d2SAndre Oppermann /* Update variables that are affected by ipsec4_output(). */ 52733841545SHajimu UMEMOTO ip = mtod(m, struct ip *); 52833841545SHajimu UMEMOTO hlen = ip->ip_hl << 2; 529b2630c29SGeorge V. Neville-Neil #endif /* IPSEC */ 53033841545SHajimu UMEMOTO 531c21fd232SAndre Oppermann /* Jump over all PFIL processing if hooks are not active. */ 5320b4b0b0fSJulian Elischer if (!PFIL_HOOKED(&V_inet_pfil_hook)) 533c21fd232SAndre Oppermann goto passout; 534c21fd232SAndre Oppermann 535c21fd232SAndre Oppermann /* Run through list of hooks for output packets. */ 5369b932e9eSAndre Oppermann odst.s_addr = ip->ip_dst.s_addr; 5370b4b0b0fSJulian Elischer error = pfil_run_hooks(&V_inet_pfil_hook, &m, ifp, PFIL_OUT, inp); 538134ea224SSam Leffler if (error != 0 || m == NULL) 539c4ac87eaSDarren Reed goto done; 5409b932e9eSAndre Oppermann 541c4ac87eaSDarren Reed ip = mtod(m, struct ip *); 542fed1c7e9SSøren Schmidt 5439b932e9eSAndre Oppermann /* See if destination IP address was changed by packet filter. */ 5449b932e9eSAndre Oppermann if (odst.s_addr != ip->ip_dst.s_addr) { 5459b932e9eSAndre Oppermann m->m_flags |= M_SKIP_FIREWALL; 546f4fca2d8SAndre Oppermann /* If destination is now ourself drop to ip_input(). */ 5479b932e9eSAndre Oppermann if (in_localip(ip->ip_dst)) { 5489b932e9eSAndre Oppermann m->m_flags |= M_FASTFWD_OURS; 549f9e354dfSJulian Elischer if (m->m_pkthdr.rcvif == NULL) 550603724d3SBjoern A. Zeeb m->m_pkthdr.rcvif = V_loif; 55120c822f3SJonathan Lemon if (m->m_pkthdr.csum_flags & CSUM_DELAY_DATA) { 55220c822f3SJonathan Lemon m->m_pkthdr.csum_flags |= 55320c822f3SJonathan Lemon CSUM_DATA_VALID | CSUM_PSEUDO_HDR; 554ac9d7e26SMax Laier m->m_pkthdr.csum_data = 0xffff; 55520c822f3SJonathan Lemon } 55620c822f3SJonathan Lemon m->m_pkthdr.csum_flags |= 55720c822f3SJonathan Lemon CSUM_IP_CHECKED | CSUM_IP_VALID; 5582f4afd21SRandall Stewart #ifdef SCTP 5592f4afd21SRandall Stewart if (m->m_pkthdr.csum_flags & CSUM_SCTP) 5602f4afd21SRandall Stewart m->m_pkthdr.csum_flags |= CSUM_SCTP_VALID; 5612f4afd21SRandall Stewart #endif 5629b932e9eSAndre Oppermann error = netisr_queue(NETISR_IP, m); 5639b932e9eSAndre Oppermann goto done; 5643c73180fSGleb Smirnoff } else { 5653c73180fSGleb Smirnoff if (ia != NULL) 5663c73180fSGleb Smirnoff ifa_free(&ia->ia_ifa); 567f4fca2d8SAndre Oppermann goto again; /* Redo the routing table lookup. */ 5689b932e9eSAndre Oppermann } 5693c73180fSGleb Smirnoff } 5709b932e9eSAndre Oppermann 5719b932e9eSAndre Oppermann /* See if local, if yes, send it to netisr with IP_FASTFWD_OURS. */ 5729b932e9eSAndre Oppermann if (m->m_flags & M_FASTFWD_OURS) { 5739b932e9eSAndre Oppermann if (m->m_pkthdr.rcvif == NULL) 574603724d3SBjoern A. Zeeb m->m_pkthdr.rcvif = V_loif; 5759b932e9eSAndre Oppermann if (m->m_pkthdr.csum_flags & CSUM_DELAY_DATA) { 5769b932e9eSAndre Oppermann m->m_pkthdr.csum_flags |= 5779b932e9eSAndre Oppermann CSUM_DATA_VALID | CSUM_PSEUDO_HDR; 5789b932e9eSAndre Oppermann m->m_pkthdr.csum_data = 0xffff; 5799b932e9eSAndre Oppermann } 5802f4afd21SRandall Stewart #ifdef SCTP 5812f4afd21SRandall Stewart if (m->m_pkthdr.csum_flags & CSUM_SCTP) 5822f4afd21SRandall Stewart m->m_pkthdr.csum_flags |= CSUM_SCTP_VALID; 5832f4afd21SRandall Stewart #endif 5849b932e9eSAndre Oppermann m->m_pkthdr.csum_flags |= 5859b932e9eSAndre Oppermann CSUM_IP_CHECKED | CSUM_IP_VALID; 5869b932e9eSAndre Oppermann 5879b932e9eSAndre Oppermann error = netisr_queue(NETISR_IP, m); 588f9e354dfSJulian Elischer goto done; 589f9e354dfSJulian Elischer } 5909b932e9eSAndre Oppermann /* Or forward to some other address? */ 591ffdbf9daSAndrey V. Elsukov if ((m->m_flags & M_IP_NEXTHOP) && 592ffdbf9daSAndrey V. Elsukov (fwd_tag = m_tag_find(m, PACKET_TAG_IPFORWARD, NULL)) != NULL) { 5939b932e9eSAndre Oppermann bcopy((fwd_tag+1), dst, sizeof(struct sockaddr_in)); 5949b932e9eSAndre Oppermann m->m_flags |= M_SKIP_FIREWALL; 595ffdbf9daSAndrey V. Elsukov m->m_flags &= ~M_IP_NEXTHOP; 5969b932e9eSAndre Oppermann m_tag_delete(m, fwd_tag); 5973c73180fSGleb Smirnoff if (ia != NULL) 5983c73180fSGleb Smirnoff ifa_free(&ia->ia_ifa); 5999b932e9eSAndre Oppermann goto again; 600099dd043SAndre Oppermann } 6012b25acc1SLuigi Rizzo 602c21fd232SAndre Oppermann passout: 60351c8ec4aSRuslan Ermilov /* 127/8 must not appear on wire - RFC1122. */ 60451c8ec4aSRuslan Ermilov if ((ntohl(ip->ip_dst.s_addr) >> IN_CLASSA_NSHIFT) == IN_LOOPBACKNET || 60551c8ec4aSRuslan Ermilov (ntohl(ip->ip_src.s_addr) >> IN_CLASSA_NSHIFT) == IN_LOOPBACKNET) { 60651c8ec4aSRuslan Ermilov if ((ifp->if_flags & IFF_LOOPBACK) == 0) { 60786425c62SRobert Watson IPSTAT_INC(ips_badaddr); 60851c8ec4aSRuslan Ermilov error = EADDRNOTAVAIL; 60951c8ec4aSRuslan Ermilov goto bad; 61051c8ec4aSRuslan Ermilov } 61151c8ec4aSRuslan Ermilov } 61251c8ec4aSRuslan Ermilov 613206a3274SRuslan Ermilov m->m_pkthdr.csum_flags |= CSUM_IP; 614078468edSGleb Smirnoff if (m->m_pkthdr.csum_flags & CSUM_DELAY_DATA & ~ifp->if_hwassist) { 615db4f9cc7SJonathan Lemon in_delayed_cksum(m); 616078468edSGleb Smirnoff m->m_pkthdr.csum_flags &= ~CSUM_DELAY_DATA; 617db4f9cc7SJonathan Lemon } 6182f4afd21SRandall Stewart #ifdef SCTP 619078468edSGleb Smirnoff if (m->m_pkthdr.csum_flags & CSUM_SCTP & ~ifp->if_hwassist) { 6201966e5b5SRandall Stewart sctp_delayed_cksum(m, (uint32_t)(ip->ip_hl << 2)); 621078468edSGleb Smirnoff m->m_pkthdr.csum_flags &= ~CSUM_SCTP; 6222f4afd21SRandall Stewart } 6232f4afd21SRandall Stewart #endif 624db4f9cc7SJonathan Lemon 625e7319babSPoul-Henning Kamp /* 626db4f9cc7SJonathan Lemon * If small enough for interface, or the interface will take 627233dcce1SAndre Oppermann * care of the fragmentation for us, we can just send directly. 628df8bae1dSRodney W. Grimes */ 62921d172a3SGleb Smirnoff if (ip_len <= mtu || 630233dcce1SAndre Oppermann (m->m_pkthdr.csum_flags & ifp->if_hwassist & CSUM_TSO) != 0 || 63121d172a3SGleb Smirnoff ((ip_off & IP_DF) == 0 && (ifp->if_hwassist & CSUM_FRAGMENT))) { 632df8bae1dSRodney W. Grimes ip->ip_sum = 0; 633078468edSGleb Smirnoff if (m->m_pkthdr.csum_flags & CSUM_IP & ~ifp->if_hwassist) { 634df8bae1dSRodney W. Grimes ip->ip_sum = in_cksum(m, hlen); 635078468edSGleb Smirnoff m->m_pkthdr.csum_flags &= ~CSUM_IP; 636078468edSGleb Smirnoff } 6375da9f8faSJosef Karthauser 638233dcce1SAndre Oppermann /* 639233dcce1SAndre Oppermann * Record statistics for this interface address. 640233dcce1SAndre Oppermann * With CSUM_TSO the byte/packet count will be slightly 641233dcce1SAndre Oppermann * incorrect because we count the IP+TCP headers only 642233dcce1SAndre Oppermann * once instead of for every generated packet. 643233dcce1SAndre Oppermann */ 64438c1bc35SRuslan Ermilov if (!(flags & IP_FORWARDING) && ia) { 645233dcce1SAndre Oppermann if (m->m_pkthdr.csum_flags & CSUM_TSO) 6467caf4ab7SGleb Smirnoff counter_u64_add(ia->ia_ifa.ifa_opackets, 6477caf4ab7SGleb Smirnoff m->m_pkthdr.len / m->m_pkthdr.tso_segsz); 648233dcce1SAndre Oppermann else 6497caf4ab7SGleb Smirnoff counter_u64_add(ia->ia_ifa.ifa_opackets, 1); 6507caf4ab7SGleb Smirnoff 6517caf4ab7SGleb Smirnoff counter_u64_add(ia->ia_ifa.ifa_obytes, m->m_pkthdr.len); 6525da9f8faSJosef Karthauser } 65353dcc544SMike Silbersack #ifdef MBUF_STRESS_TEST 6543390d476SMike Silbersack if (mbuf_frag_size && m->m_pkthdr.len > mbuf_frag_size) 655eb1b1807SGleb Smirnoff m = m_fragment(m, M_NOWAIT, mbuf_frag_size); 6569d9edc56SMike Silbersack #endif 657147f74d1SAndre Oppermann /* 658147f74d1SAndre Oppermann * Reset layer specific mbuf flags 659147f74d1SAndre Oppermann * to avoid confusing lower layers. 660147f74d1SAndre Oppermann */ 66186bd0491SAndre Oppermann m_clrprotoflags(m); 66257f60867SMark Johnston IP_PROBE(send, NULL, NULL, ip, ifp, ip, NULL); 66347e8d432SGleb Smirnoff error = (*ifp->if_output)(ifp, m, 66447e8d432SGleb Smirnoff (const struct sockaddr *)gw, ro); 665df8bae1dSRodney W. Grimes goto done; 666df8bae1dSRodney W. Grimes } 6671e78ac21SJeffrey Hsu 668233dcce1SAndre Oppermann /* Balk when DF bit is set or the interface didn't support TSO. */ 66921d172a3SGleb Smirnoff if ((ip_off & IP_DF) || (m->m_pkthdr.csum_flags & CSUM_TSO)) { 670df8bae1dSRodney W. Grimes error = EMSGSIZE; 67186425c62SRobert Watson IPSTAT_INC(ips_cantfrag); 672df8bae1dSRodney W. Grimes goto bad; 673df8bae1dSRodney W. Grimes } 6741e78ac21SJeffrey Hsu 6751e78ac21SJeffrey Hsu /* 6761e78ac21SJeffrey Hsu * Too large for interface; fragment if possible. If successful, 6771e78ac21SJeffrey Hsu * on return, m will point to a list of packets to be sent. 6781e78ac21SJeffrey Hsu */ 679078468edSGleb Smirnoff error = ip_fragment(ip, &m, mtu, ifp->if_hwassist); 6801e78ac21SJeffrey Hsu if (error) 681df8bae1dSRodney W. Grimes goto bad; 6821e78ac21SJeffrey Hsu for (; m; m = m0) { 683df8bae1dSRodney W. Grimes m0 = m->m_nextpkt; 684b375c9ecSLuigi Rizzo m->m_nextpkt = 0; 68507203494SDaniel C. Sobral if (error == 0) { 686fe937674SJosef Karthauser /* Record statistics for this interface address. */ 68707203494SDaniel C. Sobral if (ia != NULL) { 6887caf4ab7SGleb Smirnoff counter_u64_add(ia->ia_ifa.ifa_opackets, 1); 6897caf4ab7SGleb Smirnoff counter_u64_add(ia->ia_ifa.ifa_obytes, 6907caf4ab7SGleb Smirnoff m->m_pkthdr.len); 69107203494SDaniel C. Sobral } 692147f74d1SAndre Oppermann /* 693147f74d1SAndre Oppermann * Reset layer specific mbuf flags 694147f74d1SAndre Oppermann * to avoid confusing upper layers. 695147f74d1SAndre Oppermann */ 69686bd0491SAndre Oppermann m_clrprotoflags(m); 697fe937674SJosef Karthauser 69857f60867SMark Johnston IP_PROBE(send, NULL, NULL, ip, ifp, ip, NULL); 699df8bae1dSRodney W. Grimes error = (*ifp->if_output)(ifp, m, 70047e8d432SGleb Smirnoff (const struct sockaddr *)gw, ro); 701fe937674SJosef Karthauser } else 702df8bae1dSRodney W. Grimes m_freem(m); 703df8bae1dSRodney W. Grimes } 704df8bae1dSRodney W. Grimes 705df8bae1dSRodney W. Grimes if (error == 0) 70686425c62SRobert Watson IPSTAT_INC(ips_fragmented); 7071e78ac21SJeffrey Hsu 708df8bae1dSRodney W. Grimes done: 709bf984051SGleb Smirnoff if (ro == &iproute) 710bf984051SGleb Smirnoff RO_RTFREE(ro); 7118c0fec80SRobert Watson if (ia != NULL) 7128c0fec80SRobert Watson ifa_free(&ia->ia_ifa); 713df8bae1dSRodney W. Grimes return (error); 714df8bae1dSRodney W. Grimes bad: 7153528d68fSBill Paul m_freem(m); 716df8bae1dSRodney W. Grimes goto done; 717df8bae1dSRodney W. Grimes } 718df8bae1dSRodney W. Grimes 7191e78ac21SJeffrey Hsu /* 7201e78ac21SJeffrey Hsu * Create a chain of fragments which fit the given mtu. m_frag points to the 7211e78ac21SJeffrey Hsu * mbuf to be fragmented; on return it points to the chain with the fragments. 7221e78ac21SJeffrey Hsu * Return 0 if no error. If error, m_frag may contain a partially built 7231e78ac21SJeffrey Hsu * chain of fragments that should be freed by the caller. 7241e78ac21SJeffrey Hsu * 7251e78ac21SJeffrey Hsu * if_hwassist_flags is the hw offload capabilities (see if_data.ifi_hwassist) 7261e78ac21SJeffrey Hsu */ 7271e78ac21SJeffrey Hsu int 7281e78ac21SJeffrey Hsu ip_fragment(struct ip *ip, struct mbuf **m_frag, int mtu, 729078468edSGleb Smirnoff u_long if_hwassist_flags) 7301e78ac21SJeffrey Hsu { 7311e78ac21SJeffrey Hsu int error = 0; 7321e78ac21SJeffrey Hsu int hlen = ip->ip_hl << 2; 7331e78ac21SJeffrey Hsu int len = (mtu - hlen) & ~7; /* size of payload in each fragment */ 7341e78ac21SJeffrey Hsu int off; 7351e78ac21SJeffrey Hsu struct mbuf *m0 = *m_frag; /* the original packet */ 7361e78ac21SJeffrey Hsu int firstlen; 7371e78ac21SJeffrey Hsu struct mbuf **mnext; 7381e78ac21SJeffrey Hsu int nfrags; 73921d172a3SGleb Smirnoff uint16_t ip_len, ip_off; 7401e78ac21SJeffrey Hsu 74121d172a3SGleb Smirnoff ip_len = ntohs(ip->ip_len); 74221d172a3SGleb Smirnoff ip_off = ntohs(ip->ip_off); 74321d172a3SGleb Smirnoff 74421d172a3SGleb Smirnoff if (ip_off & IP_DF) { /* Fragmentation not allowed */ 74586425c62SRobert Watson IPSTAT_INC(ips_cantfrag); 7461e78ac21SJeffrey Hsu return EMSGSIZE; 7471e78ac21SJeffrey Hsu } 7481e78ac21SJeffrey Hsu 7491e78ac21SJeffrey Hsu /* 7501e78ac21SJeffrey Hsu * Must be able to put at least 8 bytes per fragment. 7511e78ac21SJeffrey Hsu */ 7521e78ac21SJeffrey Hsu if (len < 8) 7531e78ac21SJeffrey Hsu return EMSGSIZE; 7541e78ac21SJeffrey Hsu 7551e78ac21SJeffrey Hsu /* 7561e78ac21SJeffrey Hsu * If the interface will not calculate checksums on 7571e78ac21SJeffrey Hsu * fragmented packets, then do it here. 7581e78ac21SJeffrey Hsu */ 759da2299c5SAndre Oppermann if (m0->m_pkthdr.csum_flags & CSUM_DELAY_DATA) { 7601e78ac21SJeffrey Hsu in_delayed_cksum(m0); 7611e78ac21SJeffrey Hsu m0->m_pkthdr.csum_flags &= ~CSUM_DELAY_DATA; 7621e78ac21SJeffrey Hsu } 7632f4afd21SRandall Stewart #ifdef SCTP 764da2299c5SAndre Oppermann if (m0->m_pkthdr.csum_flags & CSUM_SCTP) { 7651966e5b5SRandall Stewart sctp_delayed_cksum(m0, hlen); 7662f4afd21SRandall Stewart m0->m_pkthdr.csum_flags &= ~CSUM_SCTP; 7672f4afd21SRandall Stewart } 7682f4afd21SRandall Stewart #endif 7691e78ac21SJeffrey Hsu if (len > PAGE_SIZE) { 7701e78ac21SJeffrey Hsu /* 7711e78ac21SJeffrey Hsu * Fragment large datagrams such that each segment 7721e78ac21SJeffrey Hsu * contains a multiple of PAGE_SIZE amount of data, 7731e78ac21SJeffrey Hsu * plus headers. This enables a receiver to perform 7741e78ac21SJeffrey Hsu * page-flipping zero-copy optimizations. 7751e78ac21SJeffrey Hsu * 7761e78ac21SJeffrey Hsu * XXX When does this help given that sender and receiver 7771e78ac21SJeffrey Hsu * could have different page sizes, and also mtu could 7781e78ac21SJeffrey Hsu * be less than the receiver's page size ? 7791e78ac21SJeffrey Hsu */ 7801e78ac21SJeffrey Hsu int newlen; 7811e78ac21SJeffrey Hsu struct mbuf *m; 7821e78ac21SJeffrey Hsu 7831e78ac21SJeffrey Hsu for (m = m0, off = 0; m && (off+m->m_len) <= mtu; m = m->m_next) 7841e78ac21SJeffrey Hsu off += m->m_len; 7851e78ac21SJeffrey Hsu 7861e78ac21SJeffrey Hsu /* 7871e78ac21SJeffrey Hsu * firstlen (off - hlen) must be aligned on an 7881e78ac21SJeffrey Hsu * 8-byte boundary 7891e78ac21SJeffrey Hsu */ 7901e78ac21SJeffrey Hsu if (off < hlen) 7911e78ac21SJeffrey Hsu goto smart_frag_failure; 7921e78ac21SJeffrey Hsu off = ((off - hlen) & ~7) + hlen; 7931e78ac21SJeffrey Hsu newlen = (~PAGE_MASK) & mtu; 7941e78ac21SJeffrey Hsu if ((newlen + sizeof (struct ip)) > mtu) { 7951e78ac21SJeffrey Hsu /* we failed, go back the default */ 7961e78ac21SJeffrey Hsu smart_frag_failure: 7971e78ac21SJeffrey Hsu newlen = len; 7981e78ac21SJeffrey Hsu off = hlen + len; 7991e78ac21SJeffrey Hsu } 8001e78ac21SJeffrey Hsu len = newlen; 8011e78ac21SJeffrey Hsu 8021e78ac21SJeffrey Hsu } else { 8031e78ac21SJeffrey Hsu off = hlen + len; 8041e78ac21SJeffrey Hsu } 8051e78ac21SJeffrey Hsu 8061e78ac21SJeffrey Hsu firstlen = off - hlen; 8071e78ac21SJeffrey Hsu mnext = &m0->m_nextpkt; /* pointer to next packet */ 8081e78ac21SJeffrey Hsu 8091e78ac21SJeffrey Hsu /* 8101e78ac21SJeffrey Hsu * Loop through length of segment after first fragment, 8111e78ac21SJeffrey Hsu * make new header and copy data of each part and link onto chain. 8121e78ac21SJeffrey Hsu * Here, m0 is the original packet, m is the fragment being created. 8131e78ac21SJeffrey Hsu * The fragments are linked off the m_nextpkt of the original 8141e78ac21SJeffrey Hsu * packet, which after processing serves as the first fragment. 8151e78ac21SJeffrey Hsu */ 81621d172a3SGleb Smirnoff for (nfrags = 1; off < ip_len; off += len, nfrags++) { 8171e78ac21SJeffrey Hsu struct ip *mhip; /* ip header on the fragment */ 8181e78ac21SJeffrey Hsu struct mbuf *m; 8191e78ac21SJeffrey Hsu int mhlen = sizeof (struct ip); 8201e78ac21SJeffrey Hsu 821dc4ad05eSGleb Smirnoff m = m_gethdr(M_NOWAIT, MT_DATA); 8220b17fba7SAndre Oppermann if (m == NULL) { 8231e78ac21SJeffrey Hsu error = ENOBUFS; 82486425c62SRobert Watson IPSTAT_INC(ips_odropped); 8251e78ac21SJeffrey Hsu goto done; 8261e78ac21SJeffrey Hsu } 827fb86dfcdSAndre Oppermann m->m_flags |= (m0->m_flags & M_MCAST); 8281e78ac21SJeffrey Hsu /* 8291e78ac21SJeffrey Hsu * In the first mbuf, leave room for the link header, then 8301e78ac21SJeffrey Hsu * copy the original IP header including options. The payload 8318b889dbbSBruce M Simpson * goes into an additional mbuf chain returned by m_copym(). 8321e78ac21SJeffrey Hsu */ 8331e78ac21SJeffrey Hsu m->m_data += max_linkhdr; 8341e78ac21SJeffrey Hsu mhip = mtod(m, struct ip *); 8351e78ac21SJeffrey Hsu *mhip = *ip; 8361e78ac21SJeffrey Hsu if (hlen > sizeof (struct ip)) { 8371e78ac21SJeffrey Hsu mhlen = ip_optcopy(ip, mhip) + sizeof (struct ip); 8381e78ac21SJeffrey Hsu mhip->ip_v = IPVERSION; 8391e78ac21SJeffrey Hsu mhip->ip_hl = mhlen >> 2; 8401e78ac21SJeffrey Hsu } 8411e78ac21SJeffrey Hsu m->m_len = mhlen; 84221d172a3SGleb Smirnoff /* XXX do we need to add ip_off below ? */ 84321d172a3SGleb Smirnoff mhip->ip_off = ((off - hlen) >> 3) + ip_off; 844fb86dfcdSAndre Oppermann if (off + len >= ip_len) 84521d172a3SGleb Smirnoff len = ip_len - off; 846fb86dfcdSAndre Oppermann else 8471e78ac21SJeffrey Hsu mhip->ip_off |= IP_MF; 8481e78ac21SJeffrey Hsu mhip->ip_len = htons((u_short)(len + mhlen)); 849eb1b1807SGleb Smirnoff m->m_next = m_copym(m0, off, len, M_NOWAIT); 8500b17fba7SAndre Oppermann if (m->m_next == NULL) { /* copy failed */ 8511e78ac21SJeffrey Hsu m_free(m); 8521e78ac21SJeffrey Hsu error = ENOBUFS; /* ??? */ 85386425c62SRobert Watson IPSTAT_INC(ips_odropped); 8541e78ac21SJeffrey Hsu goto done; 8551e78ac21SJeffrey Hsu } 8561e78ac21SJeffrey Hsu m->m_pkthdr.len = mhlen + len; 857fc74a9f9SBrooks Davis m->m_pkthdr.rcvif = NULL; 8581e78ac21SJeffrey Hsu #ifdef MAC 85930d239bcSRobert Watson mac_netinet_fragment(m0, m); 8601e78ac21SJeffrey Hsu #endif 8611e78ac21SJeffrey Hsu m->m_pkthdr.csum_flags = m0->m_pkthdr.csum_flags; 8621e78ac21SJeffrey Hsu mhip->ip_off = htons(mhip->ip_off); 8631e78ac21SJeffrey Hsu mhip->ip_sum = 0; 864078468edSGleb Smirnoff if (m->m_pkthdr.csum_flags & CSUM_IP & ~if_hwassist_flags) { 8651e78ac21SJeffrey Hsu mhip->ip_sum = in_cksum(m, mhlen); 866078468edSGleb Smirnoff m->m_pkthdr.csum_flags &= ~CSUM_IP; 867078468edSGleb Smirnoff } 8681e78ac21SJeffrey Hsu *mnext = m; 8691e78ac21SJeffrey Hsu mnext = &m->m_nextpkt; 8701e78ac21SJeffrey Hsu } 87186425c62SRobert Watson IPSTAT_ADD(ips_ofragments, nfrags); 8721e78ac21SJeffrey Hsu 8731e78ac21SJeffrey Hsu /* 8741e78ac21SJeffrey Hsu * Update first fragment by trimming what's been copied out 8751e78ac21SJeffrey Hsu * and updating header. 8761e78ac21SJeffrey Hsu */ 87721d172a3SGleb Smirnoff m_adj(m0, hlen + firstlen - ip_len); 8781e78ac21SJeffrey Hsu m0->m_pkthdr.len = hlen + firstlen; 8791e78ac21SJeffrey Hsu ip->ip_len = htons((u_short)m0->m_pkthdr.len); 88021d172a3SGleb Smirnoff ip->ip_off = htons(ip_off | IP_MF); 8811e78ac21SJeffrey Hsu ip->ip_sum = 0; 882078468edSGleb Smirnoff if (m0->m_pkthdr.csum_flags & CSUM_IP & ~if_hwassist_flags) { 8831e78ac21SJeffrey Hsu ip->ip_sum = in_cksum(m0, hlen); 884078468edSGleb Smirnoff m0->m_pkthdr.csum_flags &= ~CSUM_IP; 885078468edSGleb Smirnoff } 8861e78ac21SJeffrey Hsu 8871e78ac21SJeffrey Hsu done: 8881e78ac21SJeffrey Hsu *m_frag = m0; 8891e78ac21SJeffrey Hsu return error; 8901e78ac21SJeffrey Hsu } 8911e78ac21SJeffrey Hsu 8921c238475SJonathan Lemon void 893db4f9cc7SJonathan Lemon in_delayed_cksum(struct mbuf *m) 894db4f9cc7SJonathan Lemon { 895db4f9cc7SJonathan Lemon struct ip *ip; 89623e9c6dcSGleb Smirnoff uint16_t csum, offset, ip_len; 897db4f9cc7SJonathan Lemon 898db4f9cc7SJonathan Lemon ip = mtod(m, struct ip *); 89953be11f6SPoul-Henning Kamp offset = ip->ip_hl << 2 ; 90023e9c6dcSGleb Smirnoff ip_len = ntohs(ip->ip_len); 90123e9c6dcSGleb Smirnoff csum = in_cksum_skip(m, ip_len, offset); 902206a3274SRuslan Ermilov if (m->m_pkthdr.csum_flags & CSUM_UDP && csum == 0) 903206a3274SRuslan Ermilov csum = 0xffff; 904db4f9cc7SJonathan Lemon offset += m->m_pkthdr.csum_data; /* checksum offset */ 905db4f9cc7SJonathan Lemon 906db4f9cc7SJonathan Lemon if (offset + sizeof(u_short) > m->m_len) { 907db4f9cc7SJonathan Lemon printf("delayed m_pullup, m->len: %d off: %d p: %d\n", 908db4f9cc7SJonathan Lemon m->m_len, offset, ip->ip_p); 909db4f9cc7SJonathan Lemon /* 910db4f9cc7SJonathan Lemon * XXX 911db4f9cc7SJonathan Lemon * this shouldn't happen, but if it does, the 912db4f9cc7SJonathan Lemon * correct behavior may be to insert the checksum 9138f8d29f6SAndre Oppermann * in the appropriate next mbuf in the chain. 914db4f9cc7SJonathan Lemon */ 9158f8d29f6SAndre Oppermann return; 916db4f9cc7SJonathan Lemon } 917db4f9cc7SJonathan Lemon *(u_short *)(m->m_data + offset) = csum; 918db4f9cc7SJonathan Lemon } 919db4f9cc7SJonathan Lemon 920df8bae1dSRodney W. Grimes /* 921df8bae1dSRodney W. Grimes * IP socket option processing. 922df8bae1dSRodney W. Grimes */ 923df8bae1dSRodney W. Grimes int 924f2565d68SRobert Watson ip_ctloutput(struct socket *so, struct sockopt *sopt) 925df8bae1dSRodney W. Grimes { 926cfe8b629SGarrett Wollman struct inpcb *inp = sotoinpcb(so); 927cfe8b629SGarrett Wollman int error, optval; 928df8bae1dSRodney W. Grimes 929cfe8b629SGarrett Wollman error = optval = 0; 930cfe8b629SGarrett Wollman if (sopt->sopt_level != IPPROTO_IP) { 931fc06cd42SMikolaj Golub error = EINVAL; 932fc06cd42SMikolaj Golub 933fc06cd42SMikolaj Golub if (sopt->sopt_level == SOL_SOCKET && 934fc06cd42SMikolaj Golub sopt->sopt_dir == SOPT_SET) { 935fc06cd42SMikolaj Golub switch (sopt->sopt_name) { 936fc06cd42SMikolaj Golub case SO_REUSEADDR: 937fc06cd42SMikolaj Golub INP_WLOCK(inp); 938efdf104bSMikolaj Golub if ((so->so_options & SO_REUSEADDR) != 0) 939efdf104bSMikolaj Golub inp->inp_flags2 |= INP_REUSEADDR; 940fc06cd42SMikolaj Golub else 941efdf104bSMikolaj Golub inp->inp_flags2 &= ~INP_REUSEADDR; 942fc06cd42SMikolaj Golub INP_WUNLOCK(inp); 943fc06cd42SMikolaj Golub error = 0; 944fc06cd42SMikolaj Golub break; 945fc06cd42SMikolaj Golub case SO_REUSEPORT: 946fc06cd42SMikolaj Golub INP_WLOCK(inp); 947fc06cd42SMikolaj Golub if ((so->so_options & SO_REUSEPORT) != 0) 948fc06cd42SMikolaj Golub inp->inp_flags2 |= INP_REUSEPORT; 949fc06cd42SMikolaj Golub else 950fc06cd42SMikolaj Golub inp->inp_flags2 &= ~INP_REUSEPORT; 951fc06cd42SMikolaj Golub INP_WUNLOCK(inp); 952fc06cd42SMikolaj Golub error = 0; 953fc06cd42SMikolaj Golub break; 954fc06cd42SMikolaj Golub case SO_SETFIB: 955fc06cd42SMikolaj Golub INP_WLOCK(inp); 956fc06cd42SMikolaj Golub inp->inp_inc.inc_fibnum = so->so_fibnum; 957fc06cd42SMikolaj Golub INP_WUNLOCK(inp); 958fc06cd42SMikolaj Golub error = 0; 959fc06cd42SMikolaj Golub break; 960fc06cd42SMikolaj Golub default: 961fc06cd42SMikolaj Golub break; 962fc06cd42SMikolaj Golub } 963fc06cd42SMikolaj Golub } 964fc06cd42SMikolaj Golub return (error); 965cfe8b629SGarrett Wollman } 966df8bae1dSRodney W. Grimes 967cfe8b629SGarrett Wollman switch (sopt->sopt_dir) { 968cfe8b629SGarrett Wollman case SOPT_SET: 969cfe8b629SGarrett Wollman switch (sopt->sopt_name) { 970df8bae1dSRodney W. Grimes case IP_OPTIONS: 971df8bae1dSRodney W. Grimes #ifdef notyet 972df8bae1dSRodney W. Grimes case IP_RETOPTS: 973df8bae1dSRodney W. Grimes #endif 974cfe8b629SGarrett Wollman { 975cfe8b629SGarrett Wollman struct mbuf *m; 976cfe8b629SGarrett Wollman if (sopt->sopt_valsize > MLEN) { 977cfe8b629SGarrett Wollman error = EMSGSIZE; 978cfe8b629SGarrett Wollman break; 979cfe8b629SGarrett Wollman } 980dc4ad05eSGleb Smirnoff m = m_get(sopt->sopt_td ? M_WAITOK : M_NOWAIT, MT_DATA); 9810b17fba7SAndre Oppermann if (m == NULL) { 982cfe8b629SGarrett Wollman error = ENOBUFS; 983cfe8b629SGarrett Wollman break; 984cfe8b629SGarrett Wollman } 985cfe8b629SGarrett Wollman m->m_len = sopt->sopt_valsize; 986cfe8b629SGarrett Wollman error = sooptcopyin(sopt, mtod(m, char *), m->m_len, 987cfe8b629SGarrett Wollman m->m_len); 988635354c4SMaxim Konovalov if (error) { 989635354c4SMaxim Konovalov m_free(m); 990635354c4SMaxim Konovalov break; 991635354c4SMaxim Konovalov } 9928501a69cSRobert Watson INP_WLOCK(inp); 993993d9505SRobert Watson error = ip_pcbopts(inp, sopt->sopt_name, m); 9948501a69cSRobert Watson INP_WUNLOCK(inp); 995993d9505SRobert Watson return (error); 996cfe8b629SGarrett Wollman } 997df8bae1dSRodney W. Grimes 998f44270e7SPawel Jakub Dawidek case IP_BINDANY: 999f44270e7SPawel Jakub Dawidek if (sopt->sopt_td != NULL) { 1000f44270e7SPawel Jakub Dawidek error = priv_check(sopt->sopt_td, 1001f44270e7SPawel Jakub Dawidek PRIV_NETINET_BINDANY); 1002f44270e7SPawel Jakub Dawidek if (error) 1003be9347e3SAdrian Chadd break; 1004be9347e3SAdrian Chadd } 1005cef27294SAdrian Chadd /* FALLTHROUGH */ 1006df8bae1dSRodney W. Grimes case IP_TOS: 1007df8bae1dSRodney W. Grimes case IP_TTL: 1008936cd18dSAndre Oppermann case IP_MINTTL: 1009df8bae1dSRodney W. Grimes case IP_RECVOPTS: 1010df8bae1dSRodney W. Grimes case IP_RECVRETOPTS: 1011df8bae1dSRodney W. Grimes case IP_RECVDSTADDR: 10124957466bSMatthew N. Dodd case IP_RECVTTL: 101382c23ebaSBill Fenner case IP_RECVIF: 10146a800098SYoshinobu Inoue case IP_FAITH: 10158afa2304SBruce M Simpson case IP_ONESBCAST: 1016b2828ad2SAndre Oppermann case IP_DONTFRAG: 10173cca425bSMichael Tuexen case IP_RECVTOS: 1018cfe8b629SGarrett Wollman error = sooptcopyin(sopt, &optval, sizeof optval, 1019cfe8b629SGarrett Wollman sizeof optval); 1020cfe8b629SGarrett Wollman if (error) 1021cfe8b629SGarrett Wollman break; 1022df8bae1dSRodney W. Grimes 1023cfe8b629SGarrett Wollman switch (sopt->sopt_name) { 1024df8bae1dSRodney W. Grimes case IP_TOS: 1025ca98b82cSDavid Greenman inp->inp_ip_tos = optval; 1026df8bae1dSRodney W. Grimes break; 1027df8bae1dSRodney W. Grimes 1028df8bae1dSRodney W. Grimes case IP_TTL: 1029ca98b82cSDavid Greenman inp->inp_ip_ttl = optval; 1030df8bae1dSRodney W. Grimes break; 1031936cd18dSAndre Oppermann 1032936cd18dSAndre Oppermann case IP_MINTTL: 1033a603c811SRobert Watson if (optval >= 0 && optval <= MAXTTL) 1034936cd18dSAndre Oppermann inp->inp_ip_minttl = optval; 1035936cd18dSAndre Oppermann else 1036936cd18dSAndre Oppermann error = EINVAL; 1037936cd18dSAndre Oppermann break; 1038936cd18dSAndre Oppermann 1039a138d217SRobert Watson #define OPTSET(bit) do { \ 10408501a69cSRobert Watson INP_WLOCK(inp); \ 1041df8bae1dSRodney W. Grimes if (optval) \ 1042df8bae1dSRodney W. Grimes inp->inp_flags |= bit; \ 1043df8bae1dSRodney W. Grimes else \ 1044a138d217SRobert Watson inp->inp_flags &= ~bit; \ 10458501a69cSRobert Watson INP_WUNLOCK(inp); \ 1046a138d217SRobert Watson } while (0) 1047df8bae1dSRodney W. Grimes 1048df8bae1dSRodney W. Grimes case IP_RECVOPTS: 1049df8bae1dSRodney W. Grimes OPTSET(INP_RECVOPTS); 1050df8bae1dSRodney W. Grimes break; 1051df8bae1dSRodney W. Grimes 1052df8bae1dSRodney W. Grimes case IP_RECVRETOPTS: 1053df8bae1dSRodney W. Grimes OPTSET(INP_RECVRETOPTS); 1054df8bae1dSRodney W. Grimes break; 1055df8bae1dSRodney W. Grimes 1056df8bae1dSRodney W. Grimes case IP_RECVDSTADDR: 1057df8bae1dSRodney W. Grimes OPTSET(INP_RECVDSTADDR); 1058df8bae1dSRodney W. Grimes break; 105982c23ebaSBill Fenner 10604957466bSMatthew N. Dodd case IP_RECVTTL: 10614957466bSMatthew N. Dodd OPTSET(INP_RECVTTL); 10624957466bSMatthew N. Dodd break; 10634957466bSMatthew N. Dodd 106482c23ebaSBill Fenner case IP_RECVIF: 106582c23ebaSBill Fenner OPTSET(INP_RECVIF); 106682c23ebaSBill Fenner break; 10676a800098SYoshinobu Inoue 10686a800098SYoshinobu Inoue case IP_FAITH: 10696a800098SYoshinobu Inoue OPTSET(INP_FAITH); 10706a800098SYoshinobu Inoue break; 10718afa2304SBruce M Simpson 10728afa2304SBruce M Simpson case IP_ONESBCAST: 10738afa2304SBruce M Simpson OPTSET(INP_ONESBCAST); 10748afa2304SBruce M Simpson break; 1075b2828ad2SAndre Oppermann case IP_DONTFRAG: 1076b2828ad2SAndre Oppermann OPTSET(INP_DONTFRAG); 1077b2828ad2SAndre Oppermann break; 1078f44270e7SPawel Jakub Dawidek case IP_BINDANY: 1079f44270e7SPawel Jakub Dawidek OPTSET(INP_BINDANY); 1080be9347e3SAdrian Chadd break; 10813cca425bSMichael Tuexen case IP_RECVTOS: 10823cca425bSMichael Tuexen OPTSET(INP_RECVTOS); 10833cca425bSMichael Tuexen break; 1084df8bae1dSRodney W. Grimes } 1085df8bae1dSRodney W. Grimes break; 1086df8bae1dSRodney W. Grimes #undef OPTSET 1087df8bae1dSRodney W. Grimes 108871498f30SBruce M Simpson /* 108971498f30SBruce M Simpson * Multicast socket options are processed by the in_mcast 109071498f30SBruce M Simpson * module. 109171498f30SBruce M Simpson */ 1092df8bae1dSRodney W. Grimes case IP_MULTICAST_IF: 1093f0068c4aSGarrett Wollman case IP_MULTICAST_VIF: 1094df8bae1dSRodney W. Grimes case IP_MULTICAST_TTL: 1095df8bae1dSRodney W. Grimes case IP_MULTICAST_LOOP: 1096df8bae1dSRodney W. Grimes case IP_ADD_MEMBERSHIP: 1097df8bae1dSRodney W. Grimes case IP_DROP_MEMBERSHIP: 109871498f30SBruce M Simpson case IP_ADD_SOURCE_MEMBERSHIP: 109971498f30SBruce M Simpson case IP_DROP_SOURCE_MEMBERSHIP: 110071498f30SBruce M Simpson case IP_BLOCK_SOURCE: 110171498f30SBruce M Simpson case IP_UNBLOCK_SOURCE: 110271498f30SBruce M Simpson case IP_MSFILTER: 110371498f30SBruce M Simpson case MCAST_JOIN_GROUP: 110471498f30SBruce M Simpson case MCAST_LEAVE_GROUP: 110571498f30SBruce M Simpson case MCAST_JOIN_SOURCE_GROUP: 110671498f30SBruce M Simpson case MCAST_LEAVE_SOURCE_GROUP: 110771498f30SBruce M Simpson case MCAST_BLOCK_SOURCE: 110871498f30SBruce M Simpson case MCAST_UNBLOCK_SOURCE: 110971498f30SBruce M Simpson error = inp_setmoptions(inp, sopt); 1110df8bae1dSRodney W. Grimes break; 1111df8bae1dSRodney W. Grimes 111233b3ac06SPeter Wemm case IP_PORTRANGE: 1113cfe8b629SGarrett Wollman error = sooptcopyin(sopt, &optval, sizeof optval, 1114cfe8b629SGarrett Wollman sizeof optval); 1115cfe8b629SGarrett Wollman if (error) 1116cfe8b629SGarrett Wollman break; 111733b3ac06SPeter Wemm 11188501a69cSRobert Watson INP_WLOCK(inp); 111933b3ac06SPeter Wemm switch (optval) { 112033b3ac06SPeter Wemm case IP_PORTRANGE_DEFAULT: 112133b3ac06SPeter Wemm inp->inp_flags &= ~(INP_LOWPORT); 112233b3ac06SPeter Wemm inp->inp_flags &= ~(INP_HIGHPORT); 112333b3ac06SPeter Wemm break; 112433b3ac06SPeter Wemm 112533b3ac06SPeter Wemm case IP_PORTRANGE_HIGH: 112633b3ac06SPeter Wemm inp->inp_flags &= ~(INP_LOWPORT); 112733b3ac06SPeter Wemm inp->inp_flags |= INP_HIGHPORT; 112833b3ac06SPeter Wemm break; 112933b3ac06SPeter Wemm 113033b3ac06SPeter Wemm case IP_PORTRANGE_LOW: 113133b3ac06SPeter Wemm inp->inp_flags &= ~(INP_HIGHPORT); 113233b3ac06SPeter Wemm inp->inp_flags |= INP_LOWPORT; 113333b3ac06SPeter Wemm break; 113433b3ac06SPeter Wemm 113533b3ac06SPeter Wemm default: 113633b3ac06SPeter Wemm error = EINVAL; 113733b3ac06SPeter Wemm break; 113833b3ac06SPeter Wemm } 11398501a69cSRobert Watson INP_WUNLOCK(inp); 1140ce8c72b1SPeter Wemm break; 114133b3ac06SPeter Wemm 1142b2630c29SGeorge V. Neville-Neil #ifdef IPSEC 11436a800098SYoshinobu Inoue case IP_IPSEC_POLICY: 11446a800098SYoshinobu Inoue { 11456a800098SYoshinobu Inoue caddr_t req; 11466a800098SYoshinobu Inoue struct mbuf *m; 11476a800098SYoshinobu Inoue 11486a800098SYoshinobu Inoue if ((error = soopt_getm(sopt, &m)) != 0) /* XXX */ 11496a800098SYoshinobu Inoue break; 11506a800098SYoshinobu Inoue if ((error = soopt_mcopyin(sopt, m)) != 0) /* XXX */ 11516a800098SYoshinobu Inoue break; 11526a800098SYoshinobu Inoue req = mtod(m, caddr_t); 115397aa4a51SBjoern A. Zeeb error = ipsec_set_policy(inp, sopt->sopt_name, req, 1154c26fe973SBjoern A. Zeeb m->m_len, (sopt->sopt_td != NULL) ? 1155c26fe973SBjoern A. Zeeb sopt->sopt_td->td_ucred : NULL); 11566a800098SYoshinobu Inoue m_freem(m); 11576a800098SYoshinobu Inoue break; 11586a800098SYoshinobu Inoue } 1159b2630c29SGeorge V. Neville-Neil #endif /* IPSEC */ 11606a800098SYoshinobu Inoue 1161df8bae1dSRodney W. Grimes default: 1162df8bae1dSRodney W. Grimes error = ENOPROTOOPT; 1163df8bae1dSRodney W. Grimes break; 1164df8bae1dSRodney W. Grimes } 1165df8bae1dSRodney W. Grimes break; 1166df8bae1dSRodney W. Grimes 1167cfe8b629SGarrett Wollman case SOPT_GET: 1168cfe8b629SGarrett Wollman switch (sopt->sopt_name) { 1169df8bae1dSRodney W. Grimes case IP_OPTIONS: 1170df8bae1dSRodney W. Grimes case IP_RETOPTS: 1171cfe8b629SGarrett Wollman if (inp->inp_options) 1172cfe8b629SGarrett Wollman error = sooptcopyout(sopt, 1173cfe8b629SGarrett Wollman mtod(inp->inp_options, 1174cfe8b629SGarrett Wollman char *), 1175cfe8b629SGarrett Wollman inp->inp_options->m_len); 1176cfe8b629SGarrett Wollman else 1177cfe8b629SGarrett Wollman sopt->sopt_valsize = 0; 1178df8bae1dSRodney W. Grimes break; 1179df8bae1dSRodney W. Grimes 1180df8bae1dSRodney W. Grimes case IP_TOS: 1181df8bae1dSRodney W. Grimes case IP_TTL: 1182936cd18dSAndre Oppermann case IP_MINTTL: 1183df8bae1dSRodney W. Grimes case IP_RECVOPTS: 1184df8bae1dSRodney W. Grimes case IP_RECVRETOPTS: 1185df8bae1dSRodney W. Grimes case IP_RECVDSTADDR: 11864957466bSMatthew N. Dodd case IP_RECVTTL: 118782c23ebaSBill Fenner case IP_RECVIF: 1188cfe8b629SGarrett Wollman case IP_PORTRANGE: 11896a800098SYoshinobu Inoue case IP_FAITH: 11908afa2304SBruce M Simpson case IP_ONESBCAST: 1191b2828ad2SAndre Oppermann case IP_DONTFRAG: 11925f6bf451SAttilio Rao case IP_BINDANY: 11933cca425bSMichael Tuexen case IP_RECVTOS: 1194cfe8b629SGarrett Wollman switch (sopt->sopt_name) { 1195df8bae1dSRodney W. Grimes 1196df8bae1dSRodney W. Grimes case IP_TOS: 1197ca98b82cSDavid Greenman optval = inp->inp_ip_tos; 1198df8bae1dSRodney W. Grimes break; 1199df8bae1dSRodney W. Grimes 1200df8bae1dSRodney W. Grimes case IP_TTL: 1201ca98b82cSDavid Greenman optval = inp->inp_ip_ttl; 1202df8bae1dSRodney W. Grimes break; 1203df8bae1dSRodney W. Grimes 1204936cd18dSAndre Oppermann case IP_MINTTL: 1205936cd18dSAndre Oppermann optval = inp->inp_ip_minttl; 1206936cd18dSAndre Oppermann break; 1207936cd18dSAndre Oppermann 1208df8bae1dSRodney W. Grimes #define OPTBIT(bit) (inp->inp_flags & bit ? 1 : 0) 1209df8bae1dSRodney W. Grimes 1210df8bae1dSRodney W. Grimes case IP_RECVOPTS: 1211df8bae1dSRodney W. Grimes optval = OPTBIT(INP_RECVOPTS); 1212df8bae1dSRodney W. Grimes break; 1213df8bae1dSRodney W. Grimes 1214df8bae1dSRodney W. Grimes case IP_RECVRETOPTS: 1215df8bae1dSRodney W. Grimes optval = OPTBIT(INP_RECVRETOPTS); 1216df8bae1dSRodney W. Grimes break; 1217df8bae1dSRodney W. Grimes 1218df8bae1dSRodney W. Grimes case IP_RECVDSTADDR: 1219df8bae1dSRodney W. Grimes optval = OPTBIT(INP_RECVDSTADDR); 1220df8bae1dSRodney W. Grimes break; 122182c23ebaSBill Fenner 12224957466bSMatthew N. Dodd case IP_RECVTTL: 12234957466bSMatthew N. Dodd optval = OPTBIT(INP_RECVTTL); 12244957466bSMatthew N. Dodd break; 12254957466bSMatthew N. Dodd 122682c23ebaSBill Fenner case IP_RECVIF: 122782c23ebaSBill Fenner optval = OPTBIT(INP_RECVIF); 122882c23ebaSBill Fenner break; 1229cfe8b629SGarrett Wollman 1230cfe8b629SGarrett Wollman case IP_PORTRANGE: 1231cfe8b629SGarrett Wollman if (inp->inp_flags & INP_HIGHPORT) 1232cfe8b629SGarrett Wollman optval = IP_PORTRANGE_HIGH; 1233cfe8b629SGarrett Wollman else if (inp->inp_flags & INP_LOWPORT) 1234cfe8b629SGarrett Wollman optval = IP_PORTRANGE_LOW; 1235cfe8b629SGarrett Wollman else 1236cfe8b629SGarrett Wollman optval = 0; 1237cfe8b629SGarrett Wollman break; 12386a800098SYoshinobu Inoue 12396a800098SYoshinobu Inoue case IP_FAITH: 12406a800098SYoshinobu Inoue optval = OPTBIT(INP_FAITH); 12416a800098SYoshinobu Inoue break; 12428afa2304SBruce M Simpson 12438afa2304SBruce M Simpson case IP_ONESBCAST: 12448afa2304SBruce M Simpson optval = OPTBIT(INP_ONESBCAST); 12458afa2304SBruce M Simpson break; 1246b2828ad2SAndre Oppermann case IP_DONTFRAG: 1247b2828ad2SAndre Oppermann optval = OPTBIT(INP_DONTFRAG); 1248b2828ad2SAndre Oppermann break; 12495f6bf451SAttilio Rao case IP_BINDANY: 12505f6bf451SAttilio Rao optval = OPTBIT(INP_BINDANY); 12515f6bf451SAttilio Rao break; 12523cca425bSMichael Tuexen case IP_RECVTOS: 12533cca425bSMichael Tuexen optval = OPTBIT(INP_RECVTOS); 12543cca425bSMichael Tuexen break; 1255df8bae1dSRodney W. Grimes } 1256cfe8b629SGarrett Wollman error = sooptcopyout(sopt, &optval, sizeof optval); 1257df8bae1dSRodney W. Grimes break; 1258df8bae1dSRodney W. Grimes 125971498f30SBruce M Simpson /* 126071498f30SBruce M Simpson * Multicast socket options are processed by the in_mcast 126171498f30SBruce M Simpson * module. 126271498f30SBruce M Simpson */ 1263df8bae1dSRodney W. Grimes case IP_MULTICAST_IF: 1264f0068c4aSGarrett Wollman case IP_MULTICAST_VIF: 1265df8bae1dSRodney W. Grimes case IP_MULTICAST_TTL: 1266df8bae1dSRodney W. Grimes case IP_MULTICAST_LOOP: 126771498f30SBruce M Simpson case IP_MSFILTER: 126871498f30SBruce M Simpson error = inp_getmoptions(inp, sopt); 126933b3ac06SPeter Wemm break; 127033b3ac06SPeter Wemm 1271b2630c29SGeorge V. Neville-Neil #ifdef IPSEC 12726a800098SYoshinobu Inoue case IP_IPSEC_POLICY: 12736a800098SYoshinobu Inoue { 1274f63e7634SYoshinobu Inoue struct mbuf *m = NULL; 12756a800098SYoshinobu Inoue caddr_t req = NULL; 1276686cdd19SJun-ichiro itojun Hagino size_t len = 0; 12776a800098SYoshinobu Inoue 1278686cdd19SJun-ichiro itojun Hagino if (m != 0) { 12796a800098SYoshinobu Inoue req = mtod(m, caddr_t); 1280686cdd19SJun-ichiro itojun Hagino len = m->m_len; 1281686cdd19SJun-ichiro itojun Hagino } 128297aa4a51SBjoern A. Zeeb error = ipsec_get_policy(sotoinpcb(so), req, len, &m); 12836a800098SYoshinobu Inoue if (error == 0) 12846a800098SYoshinobu Inoue error = soopt_mcopyout(sopt, m); /* XXX */ 1285f63e7634SYoshinobu Inoue if (error == 0) 12866a800098SYoshinobu Inoue m_freem(m); 12876a800098SYoshinobu Inoue break; 12886a800098SYoshinobu Inoue } 1289b2630c29SGeorge V. Neville-Neil #endif /* IPSEC */ 12906a800098SYoshinobu Inoue 1291df8bae1dSRodney W. Grimes default: 1292df8bae1dSRodney W. Grimes error = ENOPROTOOPT; 1293df8bae1dSRodney W. Grimes break; 1294df8bae1dSRodney W. Grimes } 1295df8bae1dSRodney W. Grimes break; 1296df8bae1dSRodney W. Grimes } 1297df8bae1dSRodney W. Grimes return (error); 1298df8bae1dSRodney W. Grimes } 1299df8bae1dSRodney W. Grimes 1300df8bae1dSRodney W. Grimes /* 1301df8bae1dSRodney W. Grimes * Routine called from ip_output() to loop back a copy of an IP multicast 1302df8bae1dSRodney W. Grimes * packet to the input queue of a specified interface. Note that this 1303df8bae1dSRodney W. Grimes * calls the output routine of the loopback "driver", but with an interface 1304f5fea3ddSPaul Traina * pointer that might NOT be a loopback interface -- evil, but easier than 1305f5fea3ddSPaul Traina * replicating that code here. 1306df8bae1dSRodney W. Grimes */ 1307df8bae1dSRodney W. Grimes static void 1308f2565d68SRobert Watson ip_mloopback(struct ifnet *ifp, struct mbuf *m, struct sockaddr_in *dst, 1309f2565d68SRobert Watson int hlen) 1310df8bae1dSRodney W. Grimes { 1311b375c9ecSLuigi Rizzo register struct ip *ip; 1312df8bae1dSRodney W. Grimes struct mbuf *copym; 1313df8bae1dSRodney W. Grimes 1314e4762f75SGeorge V. Neville-Neil /* 1315e4762f75SGeorge V. Neville-Neil * Make a deep copy of the packet because we're going to 1316e4762f75SGeorge V. Neville-Neil * modify the pack in order to generate checksums. 1317e4762f75SGeorge V. Neville-Neil */ 1318eb1b1807SGleb Smirnoff copym = m_dup(m, M_NOWAIT); 131986b1d6d2SBill Fenner if (copym != NULL && (copym->m_flags & M_EXT || copym->m_len < hlen)) 132086b1d6d2SBill Fenner copym = m_pullup(copym, hlen); 1321df8bae1dSRodney W. Grimes if (copym != NULL) { 1322390cdc6aSRuslan Ermilov /* If needed, compute the checksum and mark it as valid. */ 1323390cdc6aSRuslan Ermilov if (copym->m_pkthdr.csum_flags & CSUM_DELAY_DATA) { 1324390cdc6aSRuslan Ermilov in_delayed_cksum(copym); 1325390cdc6aSRuslan Ermilov copym->m_pkthdr.csum_flags &= ~CSUM_DELAY_DATA; 1326390cdc6aSRuslan Ermilov copym->m_pkthdr.csum_flags |= 1327390cdc6aSRuslan Ermilov CSUM_DATA_VALID | CSUM_PSEUDO_HDR; 1328390cdc6aSRuslan Ermilov copym->m_pkthdr.csum_data = 0xffff; 1329390cdc6aSRuslan Ermilov } 1330df8bae1dSRodney W. Grimes /* 1331df8bae1dSRodney W. Grimes * We don't bother to fragment if the IP length is greater 1332df8bae1dSRodney W. Grimes * than the interface's MTU. Can this possibly matter? 1333df8bae1dSRodney W. Grimes */ 13348f134647SGleb Smirnoff ip = mtod(copym, struct ip *); 1335df8bae1dSRodney W. Grimes ip->ip_sum = 0; 133686b1d6d2SBill Fenner ip->ip_sum = in_cksum(copym, hlen); 1337201c2527SJulian Elischer #if 1 /* XXX */ 1338201c2527SJulian Elischer if (dst->sin_family != AF_INET) { 13392b8a366cSJulian Elischer printf("ip_mloopback: bad address family %d\n", 1340201c2527SJulian Elischer dst->sin_family); 1341201c2527SJulian Elischer dst->sin_family = AF_INET; 1342201c2527SJulian Elischer } 1343201c2527SJulian Elischer #endif 134406a429a3SArchie Cobbs if_simloop(ifp, copym, dst->sin_family, 0); 1345df8bae1dSRodney W. Grimes } 1346df8bae1dSRodney W. Grimes } 1347