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" 36be9347e3SAdrian Chadd #include "opt_inet.h" 376a800098SYoshinobu Inoue #include "opt_ipsec.h" 384ed84624SRobert Watson #include "opt_mac.h" 3953dcc544SMike Silbersack #include "opt_mbuf_stress_test.h" 40e440aed9SQing Li #include "opt_mpath.h" 41fbd1372aSJoerg Wunsch 42df8bae1dSRodney W. Grimes #include <sys/param.h> 4326f9a767SRodney W. Grimes #include <sys/systm.h> 44f0f6d643SLuigi Rizzo #include <sys/kernel.h> 45df8bae1dSRodney W. Grimes #include <sys/malloc.h> 46df8bae1dSRodney W. Grimes #include <sys/mbuf.h> 47acd3428bSRobert Watson #include <sys/priv.h> 48c26fe973SBjoern A. Zeeb #include <sys/proc.h> 49df8bae1dSRodney W. Grimes #include <sys/protosw.h> 50df8bae1dSRodney W. Grimes #include <sys/socket.h> 51df8bae1dSRodney W. Grimes #include <sys/socketvar.h> 529d9edc56SMike Silbersack #include <sys/sysctl.h> 53c26fe973SBjoern A. Zeeb #include <sys/ucred.h> 54603724d3SBjoern A. Zeeb #include <sys/vimage.h> 55df8bae1dSRodney W. Grimes 56df8bae1dSRodney W. Grimes #include <net/if.h> 579b932e9eSAndre Oppermann #include <net/netisr.h> 58c21fd232SAndre Oppermann #include <net/pfil.h> 59df8bae1dSRodney W. Grimes #include <net/route.h> 60e440aed9SQing Li #ifdef RADIX_MPATH 61e440aed9SQing Li #include <net/radix_mpath.h> 62e440aed9SQing Li #endif 634b79449eSBjoern A. Zeeb #include <net/vnet.h> 64df8bae1dSRodney W. Grimes 65df8bae1dSRodney W. Grimes #include <netinet/in.h> 66df8bae1dSRodney W. Grimes #include <netinet/in_systm.h> 67df8bae1dSRodney W. Grimes #include <netinet/ip.h> 68df8bae1dSRodney W. Grimes #include <netinet/in_pcb.h> 69df8bae1dSRodney W. Grimes #include <netinet/in_var.h> 70df8bae1dSRodney W. Grimes #include <netinet/ip_var.h> 71ef39adf0SAndre Oppermann #include <netinet/ip_options.h> 724b79449eSBjoern A. Zeeb #include <netinet/vinet.h> 73df8bae1dSRodney W. Grimes 74b2630c29SGeorge V. Neville-Neil #ifdef IPSEC 752cb64cb2SGeorge V. Neville-Neil #include <netinet/ip_ipsec.h> 761dfcf0d2SAndre Oppermann #include <netipsec/ipsec.h> 77b2630c29SGeorge V. Neville-Neil #endif /* IPSEC*/ 786a800098SYoshinobu Inoue 791dfcf0d2SAndre Oppermann #include <machine/in_cksum.h> 801dfcf0d2SAndre Oppermann 81aed55708SRobert Watson #include <security/mac/mac_framework.h> 82aed55708SRobert Watson 832b25acc1SLuigi Rizzo #define print_ip(x, a, y) printf("%s %d.%d.%d.%d%s",\ 842b25acc1SLuigi Rizzo x, (ntohl(a.s_addr)>>24)&0xFF,\ 85f9e354dfSJulian Elischer (ntohl(a.s_addr)>>16)&0xFF,\ 86f9e354dfSJulian Elischer (ntohl(a.s_addr)>>8)&0xFF,\ 872b25acc1SLuigi Rizzo (ntohl(a.s_addr))&0xFF, y); 88f9e354dfSJulian Elischer 8944e33a07SMarko Zec #ifdef VIMAGE_GLOBALS 90f23b4c91SGarrett Wollman u_short ip_id; 9144e33a07SMarko Zec #endif 92f23b4c91SGarrett Wollman 9353dcc544SMike Silbersack #ifdef MBUF_STRESS_TEST 949d9edc56SMike Silbersack 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 99be9347e3SAdrian Chadd #if defined(IP_NONLOCALBIND) 100be9347e3SAdrian Chadd static int ip_nonlocalok = 0; 101be9347e3SAdrian Chadd SYSCTL_INT(_net_inet_ip, OID_AUTO, nonlocalok, 102be9347e3SAdrian Chadd CTLFLAG_RW|CTLFLAG_SECURE, &ip_nonlocalok, 0, ""); 103be9347e3SAdrian Chadd #endif 104be9347e3SAdrian Chadd 105df8bae1dSRodney W. Grimes static void ip_mloopback 1064d77a549SAlfred Perlstein (struct ifnet *, struct mbuf *, struct sockaddr_in *, int); 107afed1b49SDarren Reed 108afed1b49SDarren Reed 10993e0e116SJulian Elischer extern struct protosw inetsw[]; 11093e0e116SJulian Elischer 111df8bae1dSRodney W. Grimes /* 112df8bae1dSRodney W. Grimes * IP output. The packet in mbuf chain m contains a skeletal IP 113df8bae1dSRodney W. Grimes * header (with len, off, ttl, proto, tos, src, dst). 114df8bae1dSRodney W. Grimes * The mbuf chain containing the packet will be freed. 115df8bae1dSRodney W. Grimes * The mbuf opt, if present, will not be freed. 1163de758d3SRobert Watson * In the IP forwarding case, the packet will arrive with options already 1173de758d3SRobert Watson * inserted, so must have a NULL opt pointer. 118df8bae1dSRodney W. Grimes */ 119df8bae1dSRodney W. Grimes int 120f2565d68SRobert Watson ip_output(struct mbuf *m, struct mbuf *opt, struct route *ro, int flags, 121f2565d68SRobert Watson struct ip_moptions *imo, struct inpcb *inp) 122df8bae1dSRodney W. Grimes { 1238b615593SMarko Zec INIT_VNET_NET(curvnet); 1248b615593SMarko Zec INIT_VNET_INET(curvnet); 1251e78ac21SJeffrey Hsu struct ip *ip; 1262b25acc1SLuigi Rizzo struct ifnet *ifp = NULL; /* keep compiler happy */ 127ac9d7e26SMax Laier struct mbuf *m0; 12823bf9953SPoul-Henning Kamp int hlen = sizeof (struct ip); 1293ae2ad08SJohn-Mark Gurney int mtu; 1309b932e9eSAndre Oppermann int len, error = 0; 1312b25acc1SLuigi Rizzo struct sockaddr_in *dst = NULL; /* keep compiler happy */ 1323956b023SLuigi Rizzo struct in_ifaddr *ia = NULL; 133db4f9cc7SJonathan Lemon int isbroadcast, sw_csum; 134e3f406b3SRuslan Ermilov struct route iproute; 1359b932e9eSAndre Oppermann struct in_addr odst; 1369b932e9eSAndre Oppermann #ifdef IPFIREWALL_FORWARD 1379b932e9eSAndre Oppermann struct m_tag *fwd_tag = NULL; 1389b932e9eSAndre Oppermann #endif 139ac9d7e26SMax Laier M_ASSERTPKTHDR(m); 14036e8826fSMax Laier 14102c1c707SAndre Oppermann if (ro == NULL) { 14202c1c707SAndre Oppermann ro = &iproute; 14302c1c707SAndre Oppermann bzero(ro, sizeof (*ro)); 14402c1c707SAndre Oppermann } 14502c1c707SAndre Oppermann 146bc97ba51SJulian Elischer if (inp != NULL) { 147bc97ba51SJulian Elischer M_SETFIB(m, inp->inp_inc.inc_fibnum); 148baa45840SRobert Watson INP_LOCK_ASSERT(inp); 149bc97ba51SJulian Elischer } 1502b25acc1SLuigi Rizzo 151df8bae1dSRodney W. Grimes if (opt) { 152cb7641e8SMaxim Konovalov len = 0; 153df8bae1dSRodney W. Grimes m = ip_insertoptions(m, opt, &len); 154cb7641e8SMaxim Konovalov if (len != 0) 155df8bae1dSRodney W. Grimes hlen = len; 156df8bae1dSRodney W. Grimes } 157df8bae1dSRodney W. Grimes ip = mtod(m, struct ip *); 1583efc3014SJulian Elischer 159df8bae1dSRodney W. Grimes /* 1606e49b1feSGarrett Wollman * Fill in IP header. If we are not allowing fragmentation, 161e0f630eaSAndre Oppermann * then the ip_id field is meaningless, but we don't set it 162e0f630eaSAndre Oppermann * to zero. Doing so causes various problems when devices along 163e0f630eaSAndre Oppermann * the path (routers, load balancers, firewalls, etc.) illegally 164e0f630eaSAndre Oppermann * disable DF on our packet. Note that a 16-bit counter 1656e49b1feSGarrett Wollman * will wrap around in less than 10 seconds at 100 Mbit/s on a 1666e49b1feSGarrett Wollman * medium with MTU 1500. See Steven M. Bellovin, "A Technique 1676e49b1feSGarrett Wollman * for Counting NATted Hosts", Proc. IMW'02, available at 1684d09f5a0SGleb Smirnoff * <http://www.cs.columbia.edu/~smb/papers/fnat.pdf>. 169df8bae1dSRodney W. Grimes */ 170df8bae1dSRodney W. Grimes if ((flags & (IP_FORWARDING|IP_RAWOUTPUT)) == 0) { 17153be11f6SPoul-Henning Kamp ip->ip_v = IPVERSION; 17253be11f6SPoul-Henning Kamp ip->ip_hl = hlen >> 2; 1731f44b0a1SDavid Malone ip->ip_id = ip_newid(); 174603724d3SBjoern A. Zeeb V_ipstat.ips_localout++; 175df8bae1dSRodney W. Grimes } else { 17653be11f6SPoul-Henning Kamp hlen = ip->ip_hl << 2; 177df8bae1dSRodney W. Grimes } 1789c9137eaSGarrett Wollman 179df8bae1dSRodney W. Grimes dst = (struct sockaddr_in *)&ro->ro_dst; 1809b932e9eSAndre Oppermann again: 181df8bae1dSRodney W. Grimes /* 182df8bae1dSRodney W. Grimes * If there is a cached route, 183df8bae1dSRodney W. Grimes * check that it is to the same destination 184df8bae1dSRodney W. Grimes * and is still up. If not, free it and try again. 185a4a6e773SHajimu UMEMOTO * The address family should also be checked in case of sharing the 186a4a6e773SHajimu UMEMOTO * cache with IPv6. 187df8bae1dSRodney W. Grimes */ 188df8bae1dSRodney W. Grimes if (ro->ro_rt && ((ro->ro_rt->rt_flags & RTF_UP) == 0 || 189a4a6e773SHajimu UMEMOTO dst->sin_family != AF_INET || 1909b932e9eSAndre Oppermann dst->sin_addr.s_addr != ip->ip_dst.s_addr)) { 191df8bae1dSRodney W. Grimes RTFREE(ro->ro_rt); 1923ae2ad08SJohn-Mark Gurney ro->ro_rt = (struct rtentry *)NULL; 193df8bae1dSRodney W. Grimes } 1949b932e9eSAndre Oppermann #ifdef IPFIREWALL_FORWARD 1959b932e9eSAndre Oppermann if (ro->ro_rt == NULL && fwd_tag == NULL) { 1969b932e9eSAndre Oppermann #else 1970b17fba7SAndre Oppermann if (ro->ro_rt == NULL) { 1989b932e9eSAndre Oppermann #endif 199a4a6e773SHajimu UMEMOTO bzero(dst, sizeof(*dst)); 200df8bae1dSRodney W. Grimes dst->sin_family = AF_INET; 201df8bae1dSRodney W. Grimes dst->sin_len = sizeof(*dst); 2029b932e9eSAndre Oppermann dst->sin_addr = ip->ip_dst; 203df8bae1dSRodney W. Grimes } 204df8bae1dSRodney W. Grimes /* 205a3fd02d8SBruce M Simpson * If routing to interface only, short circuit routing lookup. 206a3fd02d8SBruce M Simpson * The use of an all-ones broadcast address implies this; an 207a3fd02d8SBruce M Simpson * interface is specified by the broadcast address of an interface, 208a3fd02d8SBruce M Simpson * or the destination address of a ptp interface. 209df8bae1dSRodney W. Grimes */ 210a3fd02d8SBruce M Simpson if (flags & IP_SENDONES) { 211a3fd02d8SBruce M Simpson if ((ia = ifatoia(ifa_ifwithbroadaddr(sintosa(dst)))) == NULL && 212a3fd02d8SBruce M Simpson (ia = ifatoia(ifa_ifwithdstaddr(sintosa(dst)))) == NULL) { 213603724d3SBjoern A. Zeeb V_ipstat.ips_noroute++; 214a3fd02d8SBruce M Simpson error = ENETUNREACH; 215a3fd02d8SBruce M Simpson goto bad; 216a3fd02d8SBruce M Simpson } 217a3fd02d8SBruce M Simpson ip->ip_dst.s_addr = INADDR_BROADCAST; 218a3fd02d8SBruce M Simpson dst->sin_addr = ip->ip_dst; 219a3fd02d8SBruce M Simpson ifp = ia->ia_ifp; 220a3fd02d8SBruce M Simpson ip->ip_ttl = 1; 221a3fd02d8SBruce M Simpson isbroadcast = 1; 222a3fd02d8SBruce M Simpson } else if (flags & IP_ROUTETOIF) { 2230b17fba7SAndre Oppermann if ((ia = ifatoia(ifa_ifwithdstaddr(sintosa(dst)))) == NULL && 2240b17fba7SAndre Oppermann (ia = ifatoia(ifa_ifwithnet(sintosa(dst)))) == NULL) { 225603724d3SBjoern A. Zeeb V_ipstat.ips_noroute++; 226df8bae1dSRodney W. Grimes error = ENETUNREACH; 227df8bae1dSRodney W. Grimes goto bad; 228df8bae1dSRodney W. Grimes } 229df8bae1dSRodney W. Grimes ifp = ia->ia_ifp; 230df8bae1dSRodney W. Grimes ip->ip_ttl = 1; 2319f9b3dc4SGarrett Wollman isbroadcast = in_broadcast(dst->sin_addr, ifp); 2323afefa39SDaniel C. Sobral } else if (IN_MULTICAST(ntohl(ip->ip_dst.s_addr)) && 23338c1bc35SRuslan Ermilov imo != NULL && imo->imo_multicast_ifp != NULL) { 2343afefa39SDaniel C. Sobral /* 23538c1bc35SRuslan Ermilov * Bypass the normal routing lookup for multicast 23638c1bc35SRuslan Ermilov * packets if the interface is specified. 2373afefa39SDaniel C. Sobral */ 23838c1bc35SRuslan Ermilov ifp = imo->imo_multicast_ifp; 23938c1bc35SRuslan Ermilov IFP_TO_IA(ifp, ia); 24038c1bc35SRuslan Ermilov isbroadcast = 0; /* fool gcc */ 241df8bae1dSRodney W. Grimes } else { 2422c17fe93SGarrett Wollman /* 24397d8d152SAndre Oppermann * We want to do any cloning requested by the link layer, 24497d8d152SAndre Oppermann * as this is probably required in all cases for correct 24597d8d152SAndre Oppermann * operation (as it is for ARP). 2462c17fe93SGarrett Wollman */ 2470b17fba7SAndre Oppermann if (ro->ro_rt == NULL) 248e440aed9SQing Li #ifdef RADIX_MPATH 2498b07e49aSJulian Elischer rtalloc_mpath_fib(ro, 2508b07e49aSJulian Elischer ntohl(ip->ip_src.s_addr ^ ip->ip_dst.s_addr), 2518b07e49aSJulian Elischer inp ? inp->inp_inc.inc_fibnum : M_GETFIB(m)); 252e440aed9SQing Li #else 2538b07e49aSJulian Elischer in_rtalloc_ign(ro, 0, 2548b07e49aSJulian Elischer inp ? inp->inp_inc.inc_fibnum : M_GETFIB(m)); 255e440aed9SQing Li #endif 2560b17fba7SAndre Oppermann if (ro->ro_rt == NULL) { 257603724d3SBjoern A. Zeeb V_ipstat.ips_noroute++; 258df8bae1dSRodney W. Grimes error = EHOSTUNREACH; 259df8bae1dSRodney W. Grimes goto bad; 260df8bae1dSRodney W. Grimes } 261df8bae1dSRodney W. Grimes ia = ifatoia(ro->ro_rt->rt_ifa); 262df8bae1dSRodney W. Grimes ifp = ro->ro_rt->rt_ifp; 26397d8d152SAndre Oppermann ro->ro_rt->rt_rmx.rmx_pksent++; 264df8bae1dSRodney W. Grimes if (ro->ro_rt->rt_flags & RTF_GATEWAY) 265f2c2962eSRuslan Ermilov dst = (struct sockaddr_in *)ro->ro_rt->rt_gateway; 2669f9b3dc4SGarrett Wollman if (ro->ro_rt->rt_flags & RTF_HOST) 267f2c2962eSRuslan Ermilov isbroadcast = (ro->ro_rt->rt_flags & RTF_BROADCAST); 2689f9b3dc4SGarrett Wollman else 2699f9b3dc4SGarrett Wollman isbroadcast = in_broadcast(dst->sin_addr, ifp); 270df8bae1dSRodney W. Grimes } 2713ae2ad08SJohn-Mark Gurney /* 2723ae2ad08SJohn-Mark Gurney * Calculate MTU. If we have a route that is up, use that, 2733ae2ad08SJohn-Mark Gurney * otherwise use the interface's MTU. 2743ae2ad08SJohn-Mark Gurney */ 275384a05bfSAndre Oppermann if (ro->ro_rt != NULL && (ro->ro_rt->rt_flags & (RTF_UP|RTF_HOST))) { 2763ae2ad08SJohn-Mark Gurney /* 2773ae2ad08SJohn-Mark Gurney * This case can happen if the user changed the MTU 2783ae2ad08SJohn-Mark Gurney * of an interface after enabling IP on it. Because 2793ae2ad08SJohn-Mark Gurney * most netifs don't keep track of routes pointing to 2803ae2ad08SJohn-Mark Gurney * them, there is no way for one to update all its 2813ae2ad08SJohn-Mark Gurney * routes when the MTU is changed. 2823ae2ad08SJohn-Mark Gurney */ 2833ae2ad08SJohn-Mark Gurney if (ro->ro_rt->rt_rmx.rmx_mtu > ifp->if_mtu) 2843ae2ad08SJohn-Mark Gurney ro->ro_rt->rt_rmx.rmx_mtu = ifp->if_mtu; 2853ae2ad08SJohn-Mark Gurney mtu = ro->ro_rt->rt_rmx.rmx_mtu; 2863ae2ad08SJohn-Mark Gurney } else { 2873ae2ad08SJohn-Mark Gurney mtu = ifp->if_mtu; 2883ae2ad08SJohn-Mark Gurney } 2899b932e9eSAndre Oppermann if (IN_MULTICAST(ntohl(ip->ip_dst.s_addr))) { 290df8bae1dSRodney W. Grimes struct in_multi *inm; 291df8bae1dSRodney W. Grimes 292df8bae1dSRodney W. Grimes m->m_flags |= M_MCAST; 293df8bae1dSRodney W. Grimes /* 294df8bae1dSRodney W. Grimes * IP destination address is multicast. Make sure "dst" 295df8bae1dSRodney W. Grimes * still points to the address in "ro". (It may have been 296df8bae1dSRodney W. Grimes * changed to point to a gateway address, above.) 297df8bae1dSRodney W. Grimes */ 298df8bae1dSRodney W. Grimes dst = (struct sockaddr_in *)&ro->ro_dst; 299df8bae1dSRodney W. Grimes /* 300df8bae1dSRodney W. Grimes * See if the caller provided any multicast options 301df8bae1dSRodney W. Grimes */ 302df8bae1dSRodney W. Grimes if (imo != NULL) { 303df8bae1dSRodney W. Grimes ip->ip_ttl = imo->imo_multicast_ttl; 3041c5de19aSGarrett Wollman if (imo->imo_multicast_vif != -1) 3051c5de19aSGarrett Wollman ip->ip_src.s_addr = 306bbb4330bSLuigi Rizzo ip_mcast_src ? 307bbb4330bSLuigi Rizzo ip_mcast_src(imo->imo_multicast_vif) : 308bbb4330bSLuigi Rizzo INADDR_ANY; 309df8bae1dSRodney W. Grimes } else 310df8bae1dSRodney W. Grimes ip->ip_ttl = IP_DEFAULT_MULTICAST_TTL; 311df8bae1dSRodney W. Grimes /* 312df8bae1dSRodney W. Grimes * Confirm that the outgoing interface supports multicast. 313df8bae1dSRodney W. Grimes */ 3141c5de19aSGarrett Wollman if ((imo == NULL) || (imo->imo_multicast_vif == -1)) { 315df8bae1dSRodney W. Grimes if ((ifp->if_flags & IFF_MULTICAST) == 0) { 316603724d3SBjoern A. Zeeb V_ipstat.ips_noroute++; 317df8bae1dSRodney W. Grimes error = ENETUNREACH; 318df8bae1dSRodney W. Grimes goto bad; 319df8bae1dSRodney W. Grimes } 3201c5de19aSGarrett Wollman } 321df8bae1dSRodney W. Grimes /* 322df8bae1dSRodney W. Grimes * If source address not specified yet, use address 323df8bae1dSRodney W. Grimes * of outgoing interface. 324df8bae1dSRodney W. Grimes */ 325df8bae1dSRodney W. Grimes if (ip->ip_src.s_addr == INADDR_ANY) { 32638c1bc35SRuslan Ermilov /* Interface may have no addresses. */ 32738c1bc35SRuslan Ermilov if (ia != NULL) 32838c1bc35SRuslan Ermilov ip->ip_src = IA_SIN(ia)->sin_addr; 329df8bae1dSRodney W. Grimes } 330df8bae1dSRodney W. Grimes 331dd5a318bSRobert Watson IN_MULTI_LOCK(); 3329b932e9eSAndre Oppermann IN_LOOKUP_MULTI(ip->ip_dst, ifp, inm); 333df8bae1dSRodney W. Grimes if (inm != NULL && 334df8bae1dSRodney W. Grimes (imo == NULL || imo->imo_multicast_loop)) { 335dd5a318bSRobert Watson IN_MULTI_UNLOCK(); 336df8bae1dSRodney W. Grimes /* 337df8bae1dSRodney W. Grimes * If we belong to the destination multicast group 338df8bae1dSRodney W. Grimes * on the outgoing interface, and the caller did not 339df8bae1dSRodney W. Grimes * forbid loopback, loop back a copy. 340df8bae1dSRodney W. Grimes */ 34186b1d6d2SBill Fenner ip_mloopback(ifp, m, dst, hlen); 342df8bae1dSRodney W. Grimes } 343df8bae1dSRodney W. Grimes else { 344dd5a318bSRobert Watson IN_MULTI_UNLOCK(); 345df8bae1dSRodney W. Grimes /* 346df8bae1dSRodney W. Grimes * If we are acting as a multicast router, perform 347df8bae1dSRodney W. Grimes * multicast forwarding as if the packet had just 348df8bae1dSRodney W. Grimes * arrived on the interface to which we are about 349df8bae1dSRodney W. Grimes * to send. The multicast forwarding function 350df8bae1dSRodney W. Grimes * recursively calls this function, using the 351df8bae1dSRodney W. Grimes * IP_FORWARDING flag to prevent infinite recursion. 352df8bae1dSRodney W. Grimes * 353df8bae1dSRodney W. Grimes * Multicasts that are looped back by ip_mloopback(), 354df8bae1dSRodney W. Grimes * above, will be forwarded by the ip_input() routine, 355df8bae1dSRodney W. Grimes * if necessary. 356df8bae1dSRodney W. Grimes */ 357603724d3SBjoern A. Zeeb if (V_ip_mrouter && (flags & IP_FORWARDING) == 0) { 358f0068c4aSGarrett Wollman /* 359bbb4330bSLuigi Rizzo * If rsvp daemon is not running, do not 360f0068c4aSGarrett Wollman * set ip_moptions. This ensures that the packet 361f0068c4aSGarrett Wollman * is multicast and not just sent down one link 362f0068c4aSGarrett Wollman * as prescribed by rsvpd. 363f0068c4aSGarrett Wollman */ 364603724d3SBjoern A. Zeeb if (!V_rsvp_on) 365f0068c4aSGarrett Wollman imo = NULL; 366bbb4330bSLuigi Rizzo if (ip_mforward && 367bbb4330bSLuigi Rizzo ip_mforward(ip, ifp, m, imo) != 0) { 368df8bae1dSRodney W. Grimes m_freem(m); 369df8bae1dSRodney W. Grimes goto done; 370df8bae1dSRodney W. Grimes } 371df8bae1dSRodney W. Grimes } 372df8bae1dSRodney W. Grimes } 3735e9ae478SGarrett Wollman 374df8bae1dSRodney W. Grimes /* 375df8bae1dSRodney W. Grimes * Multicasts with a time-to-live of zero may be looped- 376df8bae1dSRodney W. Grimes * back, above, but must not be transmitted on a network. 377df8bae1dSRodney W. Grimes * Also, multicasts addressed to the loopback interface 378df8bae1dSRodney W. Grimes * are not sent -- the above call to ip_mloopback() will 379df8bae1dSRodney W. Grimes * loop back a copy if this host actually belongs to the 380df8bae1dSRodney W. Grimes * destination group on the loopback interface. 381df8bae1dSRodney W. Grimes */ 382f5fea3ddSPaul Traina if (ip->ip_ttl == 0 || ifp->if_flags & IFF_LOOPBACK) { 383df8bae1dSRodney W. Grimes m_freem(m); 384df8bae1dSRodney W. Grimes goto done; 385df8bae1dSRodney W. Grimes } 386df8bae1dSRodney W. Grimes 387df8bae1dSRodney W. Grimes goto sendit; 388df8bae1dSRodney W. Grimes } 3896a7c943cSAndre Oppermann 390df8bae1dSRodney W. Grimes /* 3912b25acc1SLuigi Rizzo * If the source address is not specified yet, use the address 392cb459254SJohn-Mark Gurney * of the outoing interface. 393df8bae1dSRodney W. Grimes */ 394f9e354dfSJulian Elischer if (ip->ip_src.s_addr == INADDR_ANY) { 39538c1bc35SRuslan Ermilov /* Interface may have no addresses. */ 39638c1bc35SRuslan Ermilov if (ia != NULL) { 397df8bae1dSRodney W. Grimes ip->ip_src = IA_SIN(ia)->sin_addr; 398f9e354dfSJulian Elischer } 39938c1bc35SRuslan Ermilov } 4006a7c943cSAndre Oppermann 401ca7a789aSMax Laier /* 402ca7a789aSMax Laier * Verify that we have any chance at all of being able to queue the 403ca7a789aSMax Laier * packet or packet fragments, unless ALTQ is enabled on the given 404ca7a789aSMax Laier * interface in which case packetdrop should be done by queueing. 405ca7a789aSMax Laier */ 40602b199f1SMax Laier #ifdef ALTQ 407ca7a789aSMax Laier if ((!ALTQ_IS_ENABLED(&ifp->if_snd)) && 4083ae2ad08SJohn-Mark Gurney ((ifp->if_snd.ifq_len + ip->ip_len / mtu + 1) >= 409ca7a789aSMax Laier ifp->if_snd.ifq_maxlen)) 410ca7a789aSMax Laier #else 4113ae2ad08SJohn-Mark Gurney if ((ifp->if_snd.ifq_len + ip->ip_len / mtu + 1) >= 412ca7a789aSMax Laier ifp->if_snd.ifq_maxlen) 413ca7a789aSMax Laier #endif /* ALTQ */ 414ca7a789aSMax Laier { 415b5390296SDavid Greenman error = ENOBUFS; 416603724d3SBjoern A. Zeeb V_ipstat.ips_odropped++; 417bbce982bSGleb Smirnoff ifp->if_snd.ifq_drops += (ip->ip_len / ifp->if_mtu + 1); 418b5390296SDavid Greenman goto bad; 419b5390296SDavid Greenman } 420b5390296SDavid Greenman 421b5390296SDavid Greenman /* 422df8bae1dSRodney W. Grimes * Look for broadcast address and 4238c3f5566SRuslan Ermilov * verify user is allowed to send 424df8bae1dSRodney W. Grimes * such a packet. 425df8bae1dSRodney W. Grimes */ 4269f9b3dc4SGarrett Wollman if (isbroadcast) { 427df8bae1dSRodney W. Grimes if ((ifp->if_flags & IFF_BROADCAST) == 0) { 428df8bae1dSRodney W. Grimes error = EADDRNOTAVAIL; 429df8bae1dSRodney W. Grimes goto bad; 430df8bae1dSRodney W. Grimes } 431df8bae1dSRodney W. Grimes if ((flags & IP_ALLOWBROADCAST) == 0) { 432df8bae1dSRodney W. Grimes error = EACCES; 433df8bae1dSRodney W. Grimes goto bad; 434df8bae1dSRodney W. Grimes } 435df8bae1dSRodney W. Grimes /* don't allow broadcast messages to be fragmented */ 4363ae2ad08SJohn-Mark Gurney if (ip->ip_len > mtu) { 437df8bae1dSRodney W. Grimes error = EMSGSIZE; 438df8bae1dSRodney W. Grimes goto bad; 439df8bae1dSRodney W. Grimes } 440df8bae1dSRodney W. Grimes m->m_flags |= M_BCAST; 4419f9b3dc4SGarrett Wollman } else { 442df8bae1dSRodney W. Grimes m->m_flags &= ~M_BCAST; 4439f9b3dc4SGarrett Wollman } 444df8bae1dSRodney W. Grimes 445f1743588SDarren Reed sendit: 446b2630c29SGeorge V. Neville-Neil #ifdef IPSEC 4471dfcf0d2SAndre Oppermann switch(ip_ipsec_output(&m, inp, &flags, &error, &ro, &iproute, &dst, &ia, &ifp)) { 4481dfcf0d2SAndre Oppermann case 1: 44933841545SHajimu UMEMOTO goto bad; 4501dfcf0d2SAndre Oppermann case -1: 4511dfcf0d2SAndre Oppermann goto done; 4521dfcf0d2SAndre Oppermann case 0: 45333841545SHajimu UMEMOTO default: 4541dfcf0d2SAndre Oppermann break; /* Continue with packet processing. */ 45533841545SHajimu UMEMOTO } 4561dfcf0d2SAndre Oppermann /* Update variables that are affected by ipsec4_output(). */ 45733841545SHajimu UMEMOTO ip = mtod(m, struct ip *); 45833841545SHajimu UMEMOTO hlen = ip->ip_hl << 2; 459b2630c29SGeorge V. Neville-Neil #endif /* IPSEC */ 46033841545SHajimu UMEMOTO 461c21fd232SAndre Oppermann /* Jump over all PFIL processing if hooks are not active. */ 462604afec4SChristian S.J. Peron if (!PFIL_HOOKED(&inet_pfil_hook)) 463c21fd232SAndre Oppermann goto passout; 464c21fd232SAndre Oppermann 465c21fd232SAndre Oppermann /* Run through list of hooks for output packets. */ 4669b932e9eSAndre Oppermann odst.s_addr = ip->ip_dst.s_addr; 467d6a8d588SMax Laier error = pfil_run_hooks(&inet_pfil_hook, &m, ifp, PFIL_OUT, inp); 468134ea224SSam Leffler if (error != 0 || m == NULL) 469c4ac87eaSDarren Reed goto done; 4709b932e9eSAndre Oppermann 471c4ac87eaSDarren Reed ip = mtod(m, struct ip *); 472fed1c7e9SSøren Schmidt 4739b932e9eSAndre Oppermann /* See if destination IP address was changed by packet filter. */ 4749b932e9eSAndre Oppermann if (odst.s_addr != ip->ip_dst.s_addr) { 4759b932e9eSAndre Oppermann m->m_flags |= M_SKIP_FIREWALL; 476f4fca2d8SAndre Oppermann /* If destination is now ourself drop to ip_input(). */ 4779b932e9eSAndre Oppermann if (in_localip(ip->ip_dst)) { 4789b932e9eSAndre Oppermann m->m_flags |= M_FASTFWD_OURS; 479f9e354dfSJulian Elischer if (m->m_pkthdr.rcvif == NULL) 480603724d3SBjoern A. Zeeb m->m_pkthdr.rcvif = V_loif; 48120c822f3SJonathan Lemon if (m->m_pkthdr.csum_flags & CSUM_DELAY_DATA) { 48220c822f3SJonathan Lemon m->m_pkthdr.csum_flags |= 48320c822f3SJonathan Lemon CSUM_DATA_VALID | CSUM_PSEUDO_HDR; 484ac9d7e26SMax Laier m->m_pkthdr.csum_data = 0xffff; 48520c822f3SJonathan Lemon } 48620c822f3SJonathan Lemon m->m_pkthdr.csum_flags |= 48720c822f3SJonathan Lemon CSUM_IP_CHECKED | CSUM_IP_VALID; 4889b932e9eSAndre Oppermann 4899b932e9eSAndre Oppermann error = netisr_queue(NETISR_IP, m); 4909b932e9eSAndre Oppermann goto done; 4919b932e9eSAndre Oppermann } else 492f4fca2d8SAndre Oppermann goto again; /* Redo the routing table lookup. */ 4939b932e9eSAndre Oppermann } 4949b932e9eSAndre Oppermann 4959b932e9eSAndre Oppermann #ifdef IPFIREWALL_FORWARD 4969b932e9eSAndre Oppermann /* See if local, if yes, send it to netisr with IP_FASTFWD_OURS. */ 4979b932e9eSAndre Oppermann if (m->m_flags & M_FASTFWD_OURS) { 4989b932e9eSAndre Oppermann if (m->m_pkthdr.rcvif == NULL) 499603724d3SBjoern A. Zeeb m->m_pkthdr.rcvif = V_loif; 5009b932e9eSAndre Oppermann if (m->m_pkthdr.csum_flags & CSUM_DELAY_DATA) { 5019b932e9eSAndre Oppermann m->m_pkthdr.csum_flags |= 5029b932e9eSAndre Oppermann CSUM_DATA_VALID | CSUM_PSEUDO_HDR; 5039b932e9eSAndre Oppermann m->m_pkthdr.csum_data = 0xffff; 5049b932e9eSAndre Oppermann } 5059b932e9eSAndre Oppermann m->m_pkthdr.csum_flags |= 5069b932e9eSAndre Oppermann CSUM_IP_CHECKED | CSUM_IP_VALID; 5079b932e9eSAndre Oppermann 5089b932e9eSAndre Oppermann error = netisr_queue(NETISR_IP, m); 509f9e354dfSJulian Elischer goto done; 510f9e354dfSJulian Elischer } 5119b932e9eSAndre Oppermann /* Or forward to some other address? */ 5129b932e9eSAndre Oppermann fwd_tag = m_tag_find(m, PACKET_TAG_IPFORWARD, NULL); 5139b932e9eSAndre Oppermann if (fwd_tag) { 5149b932e9eSAndre Oppermann dst = (struct sockaddr_in *)&ro->ro_dst; 5159b932e9eSAndre Oppermann bcopy((fwd_tag+1), dst, sizeof(struct sockaddr_in)); 5169b932e9eSAndre Oppermann m->m_flags |= M_SKIP_FIREWALL; 5179b932e9eSAndre Oppermann m_tag_delete(m, fwd_tag); 5189b932e9eSAndre Oppermann goto again; 519099dd043SAndre Oppermann } 520099dd043SAndre Oppermann #endif /* IPFIREWALL_FORWARD */ 5212b25acc1SLuigi Rizzo 522c21fd232SAndre Oppermann passout: 52351c8ec4aSRuslan Ermilov /* 127/8 must not appear on wire - RFC1122. */ 52451c8ec4aSRuslan Ermilov if ((ntohl(ip->ip_dst.s_addr) >> IN_CLASSA_NSHIFT) == IN_LOOPBACKNET || 52551c8ec4aSRuslan Ermilov (ntohl(ip->ip_src.s_addr) >> IN_CLASSA_NSHIFT) == IN_LOOPBACKNET) { 52651c8ec4aSRuslan Ermilov if ((ifp->if_flags & IFF_LOOPBACK) == 0) { 527603724d3SBjoern A. Zeeb V_ipstat.ips_badaddr++; 52851c8ec4aSRuslan Ermilov error = EADDRNOTAVAIL; 52951c8ec4aSRuslan Ermilov goto bad; 53051c8ec4aSRuslan Ermilov } 53151c8ec4aSRuslan Ermilov } 53251c8ec4aSRuslan Ermilov 533206a3274SRuslan Ermilov m->m_pkthdr.csum_flags |= CSUM_IP; 534206a3274SRuslan Ermilov sw_csum = m->m_pkthdr.csum_flags & ~ifp->if_hwassist; 535db4f9cc7SJonathan Lemon if (sw_csum & CSUM_DELAY_DATA) { 536db4f9cc7SJonathan Lemon in_delayed_cksum(m); 537db4f9cc7SJonathan Lemon sw_csum &= ~CSUM_DELAY_DATA; 538db4f9cc7SJonathan Lemon } 539206a3274SRuslan Ermilov m->m_pkthdr.csum_flags &= ifp->if_hwassist; 540db4f9cc7SJonathan Lemon 541e7319babSPoul-Henning Kamp /* 542db4f9cc7SJonathan Lemon * If small enough for interface, or the interface will take 543233dcce1SAndre Oppermann * care of the fragmentation for us, we can just send directly. 544df8bae1dSRodney W. Grimes */ 5453ae2ad08SJohn-Mark Gurney if (ip->ip_len <= mtu || 546233dcce1SAndre Oppermann (m->m_pkthdr.csum_flags & ifp->if_hwassist & CSUM_TSO) != 0 || 547233dcce1SAndre Oppermann ((ip->ip_off & IP_DF) == 0 && (ifp->if_hwassist & CSUM_FRAGMENT))) { 548fd8e4ebcSMike Barcroft ip->ip_len = htons(ip->ip_len); 549fd8e4ebcSMike Barcroft ip->ip_off = htons(ip->ip_off); 550df8bae1dSRodney W. Grimes ip->ip_sum = 0; 55153be11f6SPoul-Henning Kamp if (sw_csum & CSUM_DELAY_IP) 552df8bae1dSRodney W. Grimes ip->ip_sum = in_cksum(m, hlen); 5535da9f8faSJosef Karthauser 554233dcce1SAndre Oppermann /* 555233dcce1SAndre Oppermann * Record statistics for this interface address. 556233dcce1SAndre Oppermann * With CSUM_TSO the byte/packet count will be slightly 557233dcce1SAndre Oppermann * incorrect because we count the IP+TCP headers only 558233dcce1SAndre Oppermann * once instead of for every generated packet. 559233dcce1SAndre Oppermann */ 56038c1bc35SRuslan Ermilov if (!(flags & IP_FORWARDING) && ia) { 561233dcce1SAndre Oppermann if (m->m_pkthdr.csum_flags & CSUM_TSO) 5623dbee59bSBruce M Simpson ia->ia_ifa.if_opackets += 563233dcce1SAndre Oppermann m->m_pkthdr.len / m->m_pkthdr.tso_segsz; 564233dcce1SAndre Oppermann else 5653dbee59bSBruce M Simpson ia->ia_ifa.if_opackets++; 5663dbee59bSBruce M Simpson ia->ia_ifa.if_obytes += m->m_pkthdr.len; 5675da9f8faSJosef Karthauser } 56853dcc544SMike Silbersack #ifdef MBUF_STRESS_TEST 5693390d476SMike Silbersack if (mbuf_frag_size && m->m_pkthdr.len > mbuf_frag_size) 5703390d476SMike Silbersack m = m_fragment(m, M_DONTWAIT, mbuf_frag_size); 5719d9edc56SMike Silbersack #endif 572147f74d1SAndre Oppermann /* 573147f74d1SAndre Oppermann * Reset layer specific mbuf flags 574147f74d1SAndre Oppermann * to avoid confusing lower layers. 575147f74d1SAndre Oppermann */ 576147f74d1SAndre Oppermann m->m_flags &= ~(M_PROTOFLAGS); 577df8bae1dSRodney W. Grimes error = (*ifp->if_output)(ifp, m, 578df8bae1dSRodney W. Grimes (struct sockaddr *)dst, ro->ro_rt); 579df8bae1dSRodney W. Grimes goto done; 580df8bae1dSRodney W. Grimes } 5811e78ac21SJeffrey Hsu 582233dcce1SAndre Oppermann /* Balk when DF bit is set or the interface didn't support TSO. */ 583233dcce1SAndre Oppermann if ((ip->ip_off & IP_DF) || (m->m_pkthdr.csum_flags & CSUM_TSO)) { 584df8bae1dSRodney W. Grimes error = EMSGSIZE; 585603724d3SBjoern A. Zeeb V_ipstat.ips_cantfrag++; 586df8bae1dSRodney W. Grimes goto bad; 587df8bae1dSRodney W. Grimes } 5881e78ac21SJeffrey Hsu 5891e78ac21SJeffrey Hsu /* 5901e78ac21SJeffrey Hsu * Too large for interface; fragment if possible. If successful, 5911e78ac21SJeffrey Hsu * on return, m will point to a list of packets to be sent. 5921e78ac21SJeffrey Hsu */ 5933ae2ad08SJohn-Mark Gurney error = ip_fragment(ip, &m, mtu, ifp->if_hwassist, sw_csum); 5941e78ac21SJeffrey Hsu if (error) 595df8bae1dSRodney W. Grimes goto bad; 5961e78ac21SJeffrey Hsu for (; m; m = m0) { 597df8bae1dSRodney W. Grimes m0 = m->m_nextpkt; 598b375c9ecSLuigi Rizzo m->m_nextpkt = 0; 59907203494SDaniel C. Sobral if (error == 0) { 600fe937674SJosef Karthauser /* Record statistics for this interface address. */ 60107203494SDaniel C. Sobral if (ia != NULL) { 6023dbee59bSBruce M Simpson ia->ia_ifa.if_opackets++; 6033dbee59bSBruce M Simpson ia->ia_ifa.if_obytes += m->m_pkthdr.len; 60407203494SDaniel C. Sobral } 605147f74d1SAndre Oppermann /* 606147f74d1SAndre Oppermann * Reset layer specific mbuf flags 607147f74d1SAndre Oppermann * to avoid confusing upper layers. 608147f74d1SAndre Oppermann */ 609147f74d1SAndre Oppermann m->m_flags &= ~(M_PROTOFLAGS); 610fe937674SJosef Karthauser 611df8bae1dSRodney W. Grimes error = (*ifp->if_output)(ifp, m, 612df8bae1dSRodney W. Grimes (struct sockaddr *)dst, ro->ro_rt); 613fe937674SJosef Karthauser } else 614df8bae1dSRodney W. Grimes m_freem(m); 615df8bae1dSRodney W. Grimes } 616df8bae1dSRodney W. Grimes 617df8bae1dSRodney W. Grimes if (error == 0) 618603724d3SBjoern A. Zeeb V_ipstat.ips_fragmented++; 6191e78ac21SJeffrey Hsu 620df8bae1dSRodney W. Grimes done: 6216a800098SYoshinobu Inoue if (ro == &iproute && ro->ro_rt) { 6226a800098SYoshinobu Inoue RTFREE(ro->ro_rt); 623ac9d7e26SMax Laier } 624df8bae1dSRodney W. Grimes return (error); 625df8bae1dSRodney W. Grimes bad: 6263528d68fSBill Paul m_freem(m); 627df8bae1dSRodney W. Grimes goto done; 628df8bae1dSRodney W. Grimes } 629df8bae1dSRodney W. Grimes 6301e78ac21SJeffrey Hsu /* 6311e78ac21SJeffrey Hsu * Create a chain of fragments which fit the given mtu. m_frag points to the 6321e78ac21SJeffrey Hsu * mbuf to be fragmented; on return it points to the chain with the fragments. 6331e78ac21SJeffrey Hsu * Return 0 if no error. If error, m_frag may contain a partially built 6341e78ac21SJeffrey Hsu * chain of fragments that should be freed by the caller. 6351e78ac21SJeffrey Hsu * 6361e78ac21SJeffrey Hsu * if_hwassist_flags is the hw offload capabilities (see if_data.ifi_hwassist) 6371e78ac21SJeffrey Hsu * sw_csum contains the delayed checksums flags (e.g., CSUM_DELAY_IP). 6381e78ac21SJeffrey Hsu */ 6391e78ac21SJeffrey Hsu int 6401e78ac21SJeffrey Hsu ip_fragment(struct ip *ip, struct mbuf **m_frag, int mtu, 6411e78ac21SJeffrey Hsu u_long if_hwassist_flags, int sw_csum) 6421e78ac21SJeffrey Hsu { 6438b615593SMarko Zec INIT_VNET_INET(curvnet); 6441e78ac21SJeffrey Hsu int error = 0; 6451e78ac21SJeffrey Hsu int hlen = ip->ip_hl << 2; 6461e78ac21SJeffrey Hsu int len = (mtu - hlen) & ~7; /* size of payload in each fragment */ 6471e78ac21SJeffrey Hsu int off; 6481e78ac21SJeffrey Hsu struct mbuf *m0 = *m_frag; /* the original packet */ 6491e78ac21SJeffrey Hsu int firstlen; 6501e78ac21SJeffrey Hsu struct mbuf **mnext; 6511e78ac21SJeffrey Hsu int nfrags; 6521e78ac21SJeffrey Hsu 6531e78ac21SJeffrey Hsu if (ip->ip_off & IP_DF) { /* Fragmentation not allowed */ 654603724d3SBjoern A. Zeeb V_ipstat.ips_cantfrag++; 6551e78ac21SJeffrey Hsu return EMSGSIZE; 6561e78ac21SJeffrey Hsu } 6571e78ac21SJeffrey Hsu 6581e78ac21SJeffrey Hsu /* 6591e78ac21SJeffrey Hsu * Must be able to put at least 8 bytes per fragment. 6601e78ac21SJeffrey Hsu */ 6611e78ac21SJeffrey Hsu if (len < 8) 6621e78ac21SJeffrey Hsu return EMSGSIZE; 6631e78ac21SJeffrey Hsu 6641e78ac21SJeffrey Hsu /* 6651e78ac21SJeffrey Hsu * If the interface will not calculate checksums on 6661e78ac21SJeffrey Hsu * fragmented packets, then do it here. 6671e78ac21SJeffrey Hsu */ 6681e78ac21SJeffrey Hsu if (m0->m_pkthdr.csum_flags & CSUM_DELAY_DATA && 6691e78ac21SJeffrey Hsu (if_hwassist_flags & CSUM_IP_FRAGS) == 0) { 6701e78ac21SJeffrey Hsu in_delayed_cksum(m0); 6711e78ac21SJeffrey Hsu m0->m_pkthdr.csum_flags &= ~CSUM_DELAY_DATA; 6721e78ac21SJeffrey Hsu } 6731e78ac21SJeffrey Hsu 6741e78ac21SJeffrey Hsu if (len > PAGE_SIZE) { 6751e78ac21SJeffrey Hsu /* 6761e78ac21SJeffrey Hsu * Fragment large datagrams such that each segment 6771e78ac21SJeffrey Hsu * contains a multiple of PAGE_SIZE amount of data, 6781e78ac21SJeffrey Hsu * plus headers. This enables a receiver to perform 6791e78ac21SJeffrey Hsu * page-flipping zero-copy optimizations. 6801e78ac21SJeffrey Hsu * 6811e78ac21SJeffrey Hsu * XXX When does this help given that sender and receiver 6821e78ac21SJeffrey Hsu * could have different page sizes, and also mtu could 6831e78ac21SJeffrey Hsu * be less than the receiver's page size ? 6841e78ac21SJeffrey Hsu */ 6851e78ac21SJeffrey Hsu int newlen; 6861e78ac21SJeffrey Hsu struct mbuf *m; 6871e78ac21SJeffrey Hsu 6881e78ac21SJeffrey Hsu for (m = m0, off = 0; m && (off+m->m_len) <= mtu; m = m->m_next) 6891e78ac21SJeffrey Hsu off += m->m_len; 6901e78ac21SJeffrey Hsu 6911e78ac21SJeffrey Hsu /* 6921e78ac21SJeffrey Hsu * firstlen (off - hlen) must be aligned on an 6931e78ac21SJeffrey Hsu * 8-byte boundary 6941e78ac21SJeffrey Hsu */ 6951e78ac21SJeffrey Hsu if (off < hlen) 6961e78ac21SJeffrey Hsu goto smart_frag_failure; 6971e78ac21SJeffrey Hsu off = ((off - hlen) & ~7) + hlen; 6981e78ac21SJeffrey Hsu newlen = (~PAGE_MASK) & mtu; 6991e78ac21SJeffrey Hsu if ((newlen + sizeof (struct ip)) > mtu) { 7001e78ac21SJeffrey Hsu /* we failed, go back the default */ 7011e78ac21SJeffrey Hsu smart_frag_failure: 7021e78ac21SJeffrey Hsu newlen = len; 7031e78ac21SJeffrey Hsu off = hlen + len; 7041e78ac21SJeffrey Hsu } 7051e78ac21SJeffrey Hsu len = newlen; 7061e78ac21SJeffrey Hsu 7071e78ac21SJeffrey Hsu } else { 7081e78ac21SJeffrey Hsu off = hlen + len; 7091e78ac21SJeffrey Hsu } 7101e78ac21SJeffrey Hsu 7111e78ac21SJeffrey Hsu firstlen = off - hlen; 7121e78ac21SJeffrey Hsu mnext = &m0->m_nextpkt; /* pointer to next packet */ 7131e78ac21SJeffrey Hsu 7141e78ac21SJeffrey Hsu /* 7151e78ac21SJeffrey Hsu * Loop through length of segment after first fragment, 7161e78ac21SJeffrey Hsu * make new header and copy data of each part and link onto chain. 7171e78ac21SJeffrey Hsu * Here, m0 is the original packet, m is the fragment being created. 7181e78ac21SJeffrey Hsu * The fragments are linked off the m_nextpkt of the original 7191e78ac21SJeffrey Hsu * packet, which after processing serves as the first fragment. 7201e78ac21SJeffrey Hsu */ 7211e78ac21SJeffrey Hsu for (nfrags = 1; off < ip->ip_len; off += len, nfrags++) { 7221e78ac21SJeffrey Hsu struct ip *mhip; /* ip header on the fragment */ 7231e78ac21SJeffrey Hsu struct mbuf *m; 7241e78ac21SJeffrey Hsu int mhlen = sizeof (struct ip); 7251e78ac21SJeffrey Hsu 72634333b16SAndre Oppermann MGETHDR(m, M_DONTWAIT, MT_DATA); 7270b17fba7SAndre Oppermann if (m == NULL) { 7281e78ac21SJeffrey Hsu error = ENOBUFS; 729603724d3SBjoern A. Zeeb V_ipstat.ips_odropped++; 7301e78ac21SJeffrey Hsu goto done; 7311e78ac21SJeffrey Hsu } 7321e78ac21SJeffrey Hsu m->m_flags |= (m0->m_flags & M_MCAST) | M_FRAG; 7331e78ac21SJeffrey Hsu /* 7341e78ac21SJeffrey Hsu * In the first mbuf, leave room for the link header, then 7351e78ac21SJeffrey Hsu * copy the original IP header including options. The payload 7361e78ac21SJeffrey Hsu * goes into an additional mbuf chain returned by m_copy(). 7371e78ac21SJeffrey Hsu */ 7381e78ac21SJeffrey Hsu m->m_data += max_linkhdr; 7391e78ac21SJeffrey Hsu mhip = mtod(m, struct ip *); 7401e78ac21SJeffrey Hsu *mhip = *ip; 7411e78ac21SJeffrey Hsu if (hlen > sizeof (struct ip)) { 7421e78ac21SJeffrey Hsu mhlen = ip_optcopy(ip, mhip) + sizeof (struct ip); 7431e78ac21SJeffrey Hsu mhip->ip_v = IPVERSION; 7441e78ac21SJeffrey Hsu mhip->ip_hl = mhlen >> 2; 7451e78ac21SJeffrey Hsu } 7461e78ac21SJeffrey Hsu m->m_len = mhlen; 7471e78ac21SJeffrey Hsu /* XXX do we need to add ip->ip_off below ? */ 7481e78ac21SJeffrey Hsu mhip->ip_off = ((off - hlen) >> 3) + ip->ip_off; 7491e78ac21SJeffrey Hsu if (off + len >= ip->ip_len) { /* last fragment */ 7501e78ac21SJeffrey Hsu len = ip->ip_len - off; 7511e78ac21SJeffrey Hsu m->m_flags |= M_LASTFRAG; 7521e78ac21SJeffrey Hsu } else 7531e78ac21SJeffrey Hsu mhip->ip_off |= IP_MF; 7541e78ac21SJeffrey Hsu mhip->ip_len = htons((u_short)(len + mhlen)); 7551e78ac21SJeffrey Hsu m->m_next = m_copy(m0, off, len); 7560b17fba7SAndre Oppermann if (m->m_next == NULL) { /* copy failed */ 7571e78ac21SJeffrey Hsu m_free(m); 7581e78ac21SJeffrey Hsu error = ENOBUFS; /* ??? */ 759603724d3SBjoern A. Zeeb V_ipstat.ips_odropped++; 7601e78ac21SJeffrey Hsu goto done; 7611e78ac21SJeffrey Hsu } 7621e78ac21SJeffrey Hsu m->m_pkthdr.len = mhlen + len; 763fc74a9f9SBrooks Davis m->m_pkthdr.rcvif = NULL; 7641e78ac21SJeffrey Hsu #ifdef MAC 76530d239bcSRobert Watson mac_netinet_fragment(m0, m); 7661e78ac21SJeffrey Hsu #endif 7671e78ac21SJeffrey Hsu m->m_pkthdr.csum_flags = m0->m_pkthdr.csum_flags; 7681e78ac21SJeffrey Hsu mhip->ip_off = htons(mhip->ip_off); 7691e78ac21SJeffrey Hsu mhip->ip_sum = 0; 7701e78ac21SJeffrey Hsu if (sw_csum & CSUM_DELAY_IP) 7711e78ac21SJeffrey Hsu mhip->ip_sum = in_cksum(m, mhlen); 7721e78ac21SJeffrey Hsu *mnext = m; 7731e78ac21SJeffrey Hsu mnext = &m->m_nextpkt; 7741e78ac21SJeffrey Hsu } 775603724d3SBjoern A. Zeeb V_ipstat.ips_ofragments += nfrags; 7761e78ac21SJeffrey Hsu 7771e78ac21SJeffrey Hsu /* set first marker for fragment chain */ 7781e78ac21SJeffrey Hsu m0->m_flags |= M_FIRSTFRAG | M_FRAG; 7791e78ac21SJeffrey Hsu m0->m_pkthdr.csum_data = nfrags; 7801e78ac21SJeffrey Hsu 7811e78ac21SJeffrey Hsu /* 7821e78ac21SJeffrey Hsu * Update first fragment by trimming what's been copied out 7831e78ac21SJeffrey Hsu * and updating header. 7841e78ac21SJeffrey Hsu */ 7851e78ac21SJeffrey Hsu m_adj(m0, hlen + firstlen - ip->ip_len); 7861e78ac21SJeffrey Hsu m0->m_pkthdr.len = hlen + firstlen; 7871e78ac21SJeffrey Hsu ip->ip_len = htons((u_short)m0->m_pkthdr.len); 7881e78ac21SJeffrey Hsu ip->ip_off |= IP_MF; 7891e78ac21SJeffrey Hsu ip->ip_off = htons(ip->ip_off); 7901e78ac21SJeffrey Hsu ip->ip_sum = 0; 7911e78ac21SJeffrey Hsu if (sw_csum & CSUM_DELAY_IP) 7921e78ac21SJeffrey Hsu ip->ip_sum = in_cksum(m0, hlen); 7931e78ac21SJeffrey Hsu 7941e78ac21SJeffrey Hsu done: 7951e78ac21SJeffrey Hsu *m_frag = m0; 7961e78ac21SJeffrey Hsu return error; 7971e78ac21SJeffrey Hsu } 7981e78ac21SJeffrey Hsu 7991c238475SJonathan Lemon void 800db4f9cc7SJonathan Lemon in_delayed_cksum(struct mbuf *m) 801db4f9cc7SJonathan Lemon { 802db4f9cc7SJonathan Lemon struct ip *ip; 803db4f9cc7SJonathan Lemon u_short csum, offset; 804db4f9cc7SJonathan Lemon 805db4f9cc7SJonathan Lemon ip = mtod(m, struct ip *); 80653be11f6SPoul-Henning Kamp offset = ip->ip_hl << 2 ; 807db4f9cc7SJonathan Lemon csum = in_cksum_skip(m, ip->ip_len, offset); 808206a3274SRuslan Ermilov if (m->m_pkthdr.csum_flags & CSUM_UDP && csum == 0) 809206a3274SRuslan Ermilov csum = 0xffff; 810db4f9cc7SJonathan Lemon offset += m->m_pkthdr.csum_data; /* checksum offset */ 811db4f9cc7SJonathan Lemon 812db4f9cc7SJonathan Lemon if (offset + sizeof(u_short) > m->m_len) { 813db4f9cc7SJonathan Lemon printf("delayed m_pullup, m->len: %d off: %d p: %d\n", 814db4f9cc7SJonathan Lemon m->m_len, offset, ip->ip_p); 815db4f9cc7SJonathan Lemon /* 816db4f9cc7SJonathan Lemon * XXX 817db4f9cc7SJonathan Lemon * this shouldn't happen, but if it does, the 818db4f9cc7SJonathan Lemon * correct behavior may be to insert the checksum 8198f8d29f6SAndre Oppermann * in the appropriate next mbuf in the chain. 820db4f9cc7SJonathan Lemon */ 8218f8d29f6SAndre Oppermann return; 822db4f9cc7SJonathan Lemon } 823db4f9cc7SJonathan Lemon *(u_short *)(m->m_data + offset) = csum; 824db4f9cc7SJonathan Lemon } 825db4f9cc7SJonathan Lemon 826df8bae1dSRodney W. Grimes /* 827df8bae1dSRodney W. Grimes * IP socket option processing. 828df8bae1dSRodney W. Grimes */ 829df8bae1dSRodney W. Grimes int 830f2565d68SRobert Watson ip_ctloutput(struct socket *so, struct sockopt *sopt) 831df8bae1dSRodney W. Grimes { 832cfe8b629SGarrett Wollman struct inpcb *inp = sotoinpcb(so); 833cfe8b629SGarrett Wollman int error, optval; 834df8bae1dSRodney W. Grimes 835cfe8b629SGarrett Wollman error = optval = 0; 836cfe8b629SGarrett Wollman if (sopt->sopt_level != IPPROTO_IP) { 837bc97ba51SJulian Elischer if ((sopt->sopt_level == SOL_SOCKET) && 838bc97ba51SJulian Elischer (sopt->sopt_name == SO_SETFIB)) { 839bc97ba51SJulian Elischer inp->inp_inc.inc_fibnum = so->so_fibnum; 840bc97ba51SJulian Elischer return (0); 841bc97ba51SJulian Elischer } 842cfe8b629SGarrett Wollman return (EINVAL); 843cfe8b629SGarrett Wollman } 844df8bae1dSRodney W. Grimes 845cfe8b629SGarrett Wollman switch (sopt->sopt_dir) { 846cfe8b629SGarrett Wollman case SOPT_SET: 847cfe8b629SGarrett Wollman switch (sopt->sopt_name) { 848df8bae1dSRodney W. Grimes case IP_OPTIONS: 849df8bae1dSRodney W. Grimes #ifdef notyet 850df8bae1dSRodney W. Grimes case IP_RETOPTS: 851df8bae1dSRodney W. Grimes #endif 852cfe8b629SGarrett Wollman { 853cfe8b629SGarrett Wollman struct mbuf *m; 854cfe8b629SGarrett Wollman if (sopt->sopt_valsize > MLEN) { 855cfe8b629SGarrett Wollman error = EMSGSIZE; 856cfe8b629SGarrett Wollman break; 857cfe8b629SGarrett Wollman } 858ea26d587SRuslan Ermilov MGET(m, sopt->sopt_td ? M_WAIT : M_DONTWAIT, MT_DATA); 8590b17fba7SAndre Oppermann if (m == NULL) { 860cfe8b629SGarrett Wollman error = ENOBUFS; 861cfe8b629SGarrett Wollman break; 862cfe8b629SGarrett Wollman } 863cfe8b629SGarrett Wollman m->m_len = sopt->sopt_valsize; 864cfe8b629SGarrett Wollman error = sooptcopyin(sopt, mtod(m, char *), m->m_len, 865cfe8b629SGarrett Wollman m->m_len); 866635354c4SMaxim Konovalov if (error) { 867635354c4SMaxim Konovalov m_free(m); 868635354c4SMaxim Konovalov break; 869635354c4SMaxim Konovalov } 8708501a69cSRobert Watson INP_WLOCK(inp); 871993d9505SRobert Watson error = ip_pcbopts(inp, sopt->sopt_name, m); 8728501a69cSRobert Watson INP_WUNLOCK(inp); 873993d9505SRobert Watson return (error); 874cfe8b629SGarrett Wollman } 875df8bae1dSRodney W. Grimes 876be9347e3SAdrian Chadd #if defined(IP_NONLOCALBIND) 877be9347e3SAdrian Chadd case IP_NONLOCALOK: 878be9347e3SAdrian Chadd if (! ip_nonlocalok) { 879be9347e3SAdrian Chadd error = ENOPROTOOPT; 880be9347e3SAdrian Chadd break; 881be9347e3SAdrian Chadd } 882be9347e3SAdrian Chadd #endif 883df8bae1dSRodney W. Grimes case IP_TOS: 884df8bae1dSRodney W. Grimes case IP_TTL: 885936cd18dSAndre Oppermann case IP_MINTTL: 886df8bae1dSRodney W. Grimes case IP_RECVOPTS: 887df8bae1dSRodney W. Grimes case IP_RECVRETOPTS: 888df8bae1dSRodney W. Grimes case IP_RECVDSTADDR: 8894957466bSMatthew N. Dodd case IP_RECVTTL: 89082c23ebaSBill Fenner case IP_RECVIF: 8916a800098SYoshinobu Inoue case IP_FAITH: 8928afa2304SBruce M Simpson case IP_ONESBCAST: 893b2828ad2SAndre Oppermann case IP_DONTFRAG: 894cfe8b629SGarrett Wollman error = sooptcopyin(sopt, &optval, sizeof optval, 895cfe8b629SGarrett Wollman sizeof optval); 896cfe8b629SGarrett Wollman if (error) 897cfe8b629SGarrett Wollman break; 898df8bae1dSRodney W. Grimes 899cfe8b629SGarrett Wollman switch (sopt->sopt_name) { 900df8bae1dSRodney W. Grimes case IP_TOS: 901ca98b82cSDavid Greenman inp->inp_ip_tos = optval; 902df8bae1dSRodney W. Grimes break; 903df8bae1dSRodney W. Grimes 904df8bae1dSRodney W. Grimes case IP_TTL: 905ca98b82cSDavid Greenman inp->inp_ip_ttl = optval; 906df8bae1dSRodney W. Grimes break; 907936cd18dSAndre Oppermann 908936cd18dSAndre Oppermann case IP_MINTTL: 909a603c811SRobert Watson if (optval >= 0 && optval <= MAXTTL) 910936cd18dSAndre Oppermann inp->inp_ip_minttl = optval; 911936cd18dSAndre Oppermann else 912936cd18dSAndre Oppermann error = EINVAL; 913936cd18dSAndre Oppermann break; 914936cd18dSAndre Oppermann 915a138d217SRobert Watson #define OPTSET(bit) do { \ 9168501a69cSRobert Watson INP_WLOCK(inp); \ 917df8bae1dSRodney W. Grimes if (optval) \ 918df8bae1dSRodney W. Grimes inp->inp_flags |= bit; \ 919df8bae1dSRodney W. Grimes else \ 920a138d217SRobert Watson inp->inp_flags &= ~bit; \ 9218501a69cSRobert Watson INP_WUNLOCK(inp); \ 922a138d217SRobert Watson } while (0) 923df8bae1dSRodney W. Grimes 924df8bae1dSRodney W. Grimes case IP_RECVOPTS: 925df8bae1dSRodney W. Grimes OPTSET(INP_RECVOPTS); 926df8bae1dSRodney W. Grimes break; 927df8bae1dSRodney W. Grimes 928df8bae1dSRodney W. Grimes case IP_RECVRETOPTS: 929df8bae1dSRodney W. Grimes OPTSET(INP_RECVRETOPTS); 930df8bae1dSRodney W. Grimes break; 931df8bae1dSRodney W. Grimes 932df8bae1dSRodney W. Grimes case IP_RECVDSTADDR: 933df8bae1dSRodney W. Grimes OPTSET(INP_RECVDSTADDR); 934df8bae1dSRodney W. Grimes break; 93582c23ebaSBill Fenner 9364957466bSMatthew N. Dodd case IP_RECVTTL: 9374957466bSMatthew N. Dodd OPTSET(INP_RECVTTL); 9384957466bSMatthew N. Dodd break; 9394957466bSMatthew N. Dodd 94082c23ebaSBill Fenner case IP_RECVIF: 94182c23ebaSBill Fenner OPTSET(INP_RECVIF); 94282c23ebaSBill Fenner break; 9436a800098SYoshinobu Inoue 9446a800098SYoshinobu Inoue case IP_FAITH: 9456a800098SYoshinobu Inoue OPTSET(INP_FAITH); 9466a800098SYoshinobu Inoue break; 9478afa2304SBruce M Simpson 9488afa2304SBruce M Simpson case IP_ONESBCAST: 9498afa2304SBruce M Simpson OPTSET(INP_ONESBCAST); 9508afa2304SBruce M Simpson break; 951b2828ad2SAndre Oppermann case IP_DONTFRAG: 952b2828ad2SAndre Oppermann OPTSET(INP_DONTFRAG); 953b2828ad2SAndre Oppermann break; 954be9347e3SAdrian Chadd #if defined(IP_NONLOCALBIND) 955be9347e3SAdrian Chadd case IP_NONLOCALOK: 956be9347e3SAdrian Chadd OPTSET(INP_NONLOCALOK); 957be9347e3SAdrian Chadd break; 958be9347e3SAdrian Chadd #endif 959df8bae1dSRodney W. Grimes } 960df8bae1dSRodney W. Grimes break; 961df8bae1dSRodney W. Grimes #undef OPTSET 962df8bae1dSRodney W. Grimes 96371498f30SBruce M Simpson /* 96471498f30SBruce M Simpson * Multicast socket options are processed by the in_mcast 96571498f30SBruce M Simpson * module. 96671498f30SBruce M Simpson */ 967df8bae1dSRodney W. Grimes case IP_MULTICAST_IF: 968f0068c4aSGarrett Wollman case IP_MULTICAST_VIF: 969df8bae1dSRodney W. Grimes case IP_MULTICAST_TTL: 970df8bae1dSRodney W. Grimes case IP_MULTICAST_LOOP: 971df8bae1dSRodney W. Grimes case IP_ADD_MEMBERSHIP: 972df8bae1dSRodney W. Grimes case IP_DROP_MEMBERSHIP: 97371498f30SBruce M Simpson case IP_ADD_SOURCE_MEMBERSHIP: 97471498f30SBruce M Simpson case IP_DROP_SOURCE_MEMBERSHIP: 97571498f30SBruce M Simpson case IP_BLOCK_SOURCE: 97671498f30SBruce M Simpson case IP_UNBLOCK_SOURCE: 97771498f30SBruce M Simpson case IP_MSFILTER: 97871498f30SBruce M Simpson case MCAST_JOIN_GROUP: 97971498f30SBruce M Simpson case MCAST_LEAVE_GROUP: 98071498f30SBruce M Simpson case MCAST_JOIN_SOURCE_GROUP: 98171498f30SBruce M Simpson case MCAST_LEAVE_SOURCE_GROUP: 98271498f30SBruce M Simpson case MCAST_BLOCK_SOURCE: 98371498f30SBruce M Simpson case MCAST_UNBLOCK_SOURCE: 98471498f30SBruce M Simpson error = inp_setmoptions(inp, sopt); 985df8bae1dSRodney W. Grimes break; 986df8bae1dSRodney W. Grimes 98733b3ac06SPeter Wemm case IP_PORTRANGE: 988cfe8b629SGarrett Wollman error = sooptcopyin(sopt, &optval, sizeof optval, 989cfe8b629SGarrett Wollman sizeof optval); 990cfe8b629SGarrett Wollman if (error) 991cfe8b629SGarrett Wollman break; 99233b3ac06SPeter Wemm 9938501a69cSRobert Watson INP_WLOCK(inp); 99433b3ac06SPeter Wemm switch (optval) { 99533b3ac06SPeter Wemm case IP_PORTRANGE_DEFAULT: 99633b3ac06SPeter Wemm inp->inp_flags &= ~(INP_LOWPORT); 99733b3ac06SPeter Wemm inp->inp_flags &= ~(INP_HIGHPORT); 99833b3ac06SPeter Wemm break; 99933b3ac06SPeter Wemm 100033b3ac06SPeter Wemm case IP_PORTRANGE_HIGH: 100133b3ac06SPeter Wemm inp->inp_flags &= ~(INP_LOWPORT); 100233b3ac06SPeter Wemm inp->inp_flags |= INP_HIGHPORT; 100333b3ac06SPeter Wemm break; 100433b3ac06SPeter Wemm 100533b3ac06SPeter Wemm case IP_PORTRANGE_LOW: 100633b3ac06SPeter Wemm inp->inp_flags &= ~(INP_HIGHPORT); 100733b3ac06SPeter Wemm inp->inp_flags |= INP_LOWPORT; 100833b3ac06SPeter Wemm break; 100933b3ac06SPeter Wemm 101033b3ac06SPeter Wemm default: 101133b3ac06SPeter Wemm error = EINVAL; 101233b3ac06SPeter Wemm break; 101333b3ac06SPeter Wemm } 10148501a69cSRobert Watson INP_WUNLOCK(inp); 1015ce8c72b1SPeter Wemm break; 101633b3ac06SPeter Wemm 1017b2630c29SGeorge V. Neville-Neil #ifdef IPSEC 10186a800098SYoshinobu Inoue case IP_IPSEC_POLICY: 10196a800098SYoshinobu Inoue { 10206a800098SYoshinobu Inoue caddr_t req; 10216a800098SYoshinobu Inoue struct mbuf *m; 10226a800098SYoshinobu Inoue 10236a800098SYoshinobu Inoue if ((error = soopt_getm(sopt, &m)) != 0) /* XXX */ 10246a800098SYoshinobu Inoue break; 10256a800098SYoshinobu Inoue if ((error = soopt_mcopyin(sopt, m)) != 0) /* XXX */ 10266a800098SYoshinobu Inoue break; 10276a800098SYoshinobu Inoue req = mtod(m, caddr_t); 1028c26fe973SBjoern A. Zeeb error = ipsec4_set_policy(inp, sopt->sopt_name, req, 1029c26fe973SBjoern A. Zeeb m->m_len, (sopt->sopt_td != NULL) ? 1030c26fe973SBjoern A. Zeeb sopt->sopt_td->td_ucred : NULL); 10316a800098SYoshinobu Inoue m_freem(m); 10326a800098SYoshinobu Inoue break; 10336a800098SYoshinobu Inoue } 1034b2630c29SGeorge V. Neville-Neil #endif /* IPSEC */ 10356a800098SYoshinobu Inoue 1036df8bae1dSRodney W. Grimes default: 1037df8bae1dSRodney W. Grimes error = ENOPROTOOPT; 1038df8bae1dSRodney W. Grimes break; 1039df8bae1dSRodney W. Grimes } 1040df8bae1dSRodney W. Grimes break; 1041df8bae1dSRodney W. Grimes 1042cfe8b629SGarrett Wollman case SOPT_GET: 1043cfe8b629SGarrett Wollman switch (sopt->sopt_name) { 1044df8bae1dSRodney W. Grimes case IP_OPTIONS: 1045df8bae1dSRodney W. Grimes case IP_RETOPTS: 1046cfe8b629SGarrett Wollman if (inp->inp_options) 1047cfe8b629SGarrett Wollman error = sooptcopyout(sopt, 1048cfe8b629SGarrett Wollman mtod(inp->inp_options, 1049cfe8b629SGarrett Wollman char *), 1050cfe8b629SGarrett Wollman inp->inp_options->m_len); 1051cfe8b629SGarrett Wollman else 1052cfe8b629SGarrett Wollman sopt->sopt_valsize = 0; 1053df8bae1dSRodney W. Grimes break; 1054df8bae1dSRodney W. Grimes 1055df8bae1dSRodney W. Grimes case IP_TOS: 1056df8bae1dSRodney W. Grimes case IP_TTL: 1057936cd18dSAndre Oppermann case IP_MINTTL: 1058df8bae1dSRodney W. Grimes case IP_RECVOPTS: 1059df8bae1dSRodney W. Grimes case IP_RECVRETOPTS: 1060df8bae1dSRodney W. Grimes case IP_RECVDSTADDR: 10614957466bSMatthew N. Dodd case IP_RECVTTL: 106282c23ebaSBill Fenner case IP_RECVIF: 1063cfe8b629SGarrett Wollman case IP_PORTRANGE: 10646a800098SYoshinobu Inoue case IP_FAITH: 10658afa2304SBruce M Simpson case IP_ONESBCAST: 1066b2828ad2SAndre Oppermann case IP_DONTFRAG: 1067cfe8b629SGarrett Wollman switch (sopt->sopt_name) { 1068df8bae1dSRodney W. Grimes 1069df8bae1dSRodney W. Grimes case IP_TOS: 1070ca98b82cSDavid Greenman optval = inp->inp_ip_tos; 1071df8bae1dSRodney W. Grimes break; 1072df8bae1dSRodney W. Grimes 1073df8bae1dSRodney W. Grimes case IP_TTL: 1074ca98b82cSDavid Greenman optval = inp->inp_ip_ttl; 1075df8bae1dSRodney W. Grimes break; 1076df8bae1dSRodney W. Grimes 1077936cd18dSAndre Oppermann case IP_MINTTL: 1078936cd18dSAndre Oppermann optval = inp->inp_ip_minttl; 1079936cd18dSAndre Oppermann break; 1080936cd18dSAndre Oppermann 1081df8bae1dSRodney W. Grimes #define OPTBIT(bit) (inp->inp_flags & bit ? 1 : 0) 1082df8bae1dSRodney W. Grimes 1083df8bae1dSRodney W. Grimes case IP_RECVOPTS: 1084df8bae1dSRodney W. Grimes optval = OPTBIT(INP_RECVOPTS); 1085df8bae1dSRodney W. Grimes break; 1086df8bae1dSRodney W. Grimes 1087df8bae1dSRodney W. Grimes case IP_RECVRETOPTS: 1088df8bae1dSRodney W. Grimes optval = OPTBIT(INP_RECVRETOPTS); 1089df8bae1dSRodney W. Grimes break; 1090df8bae1dSRodney W. Grimes 1091df8bae1dSRodney W. Grimes case IP_RECVDSTADDR: 1092df8bae1dSRodney W. Grimes optval = OPTBIT(INP_RECVDSTADDR); 1093df8bae1dSRodney W. Grimes break; 109482c23ebaSBill Fenner 10954957466bSMatthew N. Dodd case IP_RECVTTL: 10964957466bSMatthew N. Dodd optval = OPTBIT(INP_RECVTTL); 10974957466bSMatthew N. Dodd break; 10984957466bSMatthew N. Dodd 109982c23ebaSBill Fenner case IP_RECVIF: 110082c23ebaSBill Fenner optval = OPTBIT(INP_RECVIF); 110182c23ebaSBill Fenner break; 1102cfe8b629SGarrett Wollman 1103cfe8b629SGarrett Wollman case IP_PORTRANGE: 1104cfe8b629SGarrett Wollman if (inp->inp_flags & INP_HIGHPORT) 1105cfe8b629SGarrett Wollman optval = IP_PORTRANGE_HIGH; 1106cfe8b629SGarrett Wollman else if (inp->inp_flags & INP_LOWPORT) 1107cfe8b629SGarrett Wollman optval = IP_PORTRANGE_LOW; 1108cfe8b629SGarrett Wollman else 1109cfe8b629SGarrett Wollman optval = 0; 1110cfe8b629SGarrett Wollman break; 11116a800098SYoshinobu Inoue 11126a800098SYoshinobu Inoue case IP_FAITH: 11136a800098SYoshinobu Inoue optval = OPTBIT(INP_FAITH); 11146a800098SYoshinobu Inoue break; 11158afa2304SBruce M Simpson 11168afa2304SBruce M Simpson case IP_ONESBCAST: 11178afa2304SBruce M Simpson optval = OPTBIT(INP_ONESBCAST); 11188afa2304SBruce M Simpson break; 1119b2828ad2SAndre Oppermann case IP_DONTFRAG: 1120b2828ad2SAndre Oppermann optval = OPTBIT(INP_DONTFRAG); 1121b2828ad2SAndre Oppermann break; 1122df8bae1dSRodney W. Grimes } 1123cfe8b629SGarrett Wollman error = sooptcopyout(sopt, &optval, sizeof optval); 1124df8bae1dSRodney W. Grimes break; 1125df8bae1dSRodney W. Grimes 112671498f30SBruce M Simpson /* 112771498f30SBruce M Simpson * Multicast socket options are processed by the in_mcast 112871498f30SBruce M Simpson * module. 112971498f30SBruce M Simpson */ 1130df8bae1dSRodney W. Grimes case IP_MULTICAST_IF: 1131f0068c4aSGarrett Wollman case IP_MULTICAST_VIF: 1132df8bae1dSRodney W. Grimes case IP_MULTICAST_TTL: 1133df8bae1dSRodney W. Grimes case IP_MULTICAST_LOOP: 113471498f30SBruce M Simpson case IP_MSFILTER: 113571498f30SBruce M Simpson error = inp_getmoptions(inp, sopt); 113633b3ac06SPeter Wemm break; 113733b3ac06SPeter Wemm 1138b2630c29SGeorge V. Neville-Neil #ifdef IPSEC 11396a800098SYoshinobu Inoue case IP_IPSEC_POLICY: 11406a800098SYoshinobu Inoue { 1141f63e7634SYoshinobu Inoue struct mbuf *m = NULL; 11426a800098SYoshinobu Inoue caddr_t req = NULL; 1143686cdd19SJun-ichiro itojun Hagino size_t len = 0; 11446a800098SYoshinobu Inoue 1145686cdd19SJun-ichiro itojun Hagino if (m != 0) { 11466a800098SYoshinobu Inoue req = mtod(m, caddr_t); 1147686cdd19SJun-ichiro itojun Hagino len = m->m_len; 1148686cdd19SJun-ichiro itojun Hagino } 1149686cdd19SJun-ichiro itojun Hagino error = ipsec4_get_policy(sotoinpcb(so), req, len, &m); 11506a800098SYoshinobu Inoue if (error == 0) 11516a800098SYoshinobu Inoue error = soopt_mcopyout(sopt, m); /* XXX */ 1152f63e7634SYoshinobu Inoue if (error == 0) 11536a800098SYoshinobu Inoue m_freem(m); 11546a800098SYoshinobu Inoue break; 11556a800098SYoshinobu Inoue } 1156b2630c29SGeorge V. Neville-Neil #endif /* IPSEC */ 11576a800098SYoshinobu Inoue 1158df8bae1dSRodney W. Grimes default: 1159df8bae1dSRodney W. Grimes error = ENOPROTOOPT; 1160df8bae1dSRodney W. Grimes break; 1161df8bae1dSRodney W. Grimes } 1162df8bae1dSRodney W. Grimes break; 1163df8bae1dSRodney W. Grimes } 1164df8bae1dSRodney W. Grimes return (error); 1165df8bae1dSRodney W. Grimes } 1166df8bae1dSRodney W. Grimes 1167df8bae1dSRodney W. Grimes /* 1168df8bae1dSRodney W. Grimes * Routine called from ip_output() to loop back a copy of an IP multicast 1169df8bae1dSRodney W. Grimes * packet to the input queue of a specified interface. Note that this 1170df8bae1dSRodney W. Grimes * calls the output routine of the loopback "driver", but with an interface 1171f5fea3ddSPaul Traina * pointer that might NOT be a loopback interface -- evil, but easier than 1172f5fea3ddSPaul Traina * replicating that code here. 1173df8bae1dSRodney W. Grimes */ 1174df8bae1dSRodney W. Grimes static void 1175f2565d68SRobert Watson ip_mloopback(struct ifnet *ifp, struct mbuf *m, struct sockaddr_in *dst, 1176f2565d68SRobert Watson int hlen) 1177df8bae1dSRodney W. Grimes { 1178b375c9ecSLuigi Rizzo register struct ip *ip; 1179df8bae1dSRodney W. Grimes struct mbuf *copym; 1180df8bae1dSRodney W. Grimes 1181e4762f75SGeorge V. Neville-Neil /* 1182e4762f75SGeorge V. Neville-Neil * Make a deep copy of the packet because we're going to 1183e4762f75SGeorge V. Neville-Neil * modify the pack in order to generate checksums. 1184e4762f75SGeorge V. Neville-Neil */ 1185e4762f75SGeorge V. Neville-Neil copym = m_dup(m, M_DONTWAIT); 118686b1d6d2SBill Fenner if (copym != NULL && (copym->m_flags & M_EXT || copym->m_len < hlen)) 118786b1d6d2SBill Fenner copym = m_pullup(copym, hlen); 1188df8bae1dSRodney W. Grimes if (copym != NULL) { 1189390cdc6aSRuslan Ermilov /* If needed, compute the checksum and mark it as valid. */ 1190390cdc6aSRuslan Ermilov if (copym->m_pkthdr.csum_flags & CSUM_DELAY_DATA) { 1191390cdc6aSRuslan Ermilov in_delayed_cksum(copym); 1192390cdc6aSRuslan Ermilov copym->m_pkthdr.csum_flags &= ~CSUM_DELAY_DATA; 1193390cdc6aSRuslan Ermilov copym->m_pkthdr.csum_flags |= 1194390cdc6aSRuslan Ermilov CSUM_DATA_VALID | CSUM_PSEUDO_HDR; 1195390cdc6aSRuslan Ermilov copym->m_pkthdr.csum_data = 0xffff; 1196390cdc6aSRuslan Ermilov } 1197df8bae1dSRodney W. Grimes /* 1198df8bae1dSRodney W. Grimes * We don't bother to fragment if the IP length is greater 1199df8bae1dSRodney W. Grimes * than the interface's MTU. Can this possibly matter? 1200df8bae1dSRodney W. Grimes */ 1201df8bae1dSRodney W. Grimes ip = mtod(copym, struct ip *); 1202fd8e4ebcSMike Barcroft ip->ip_len = htons(ip->ip_len); 1203fd8e4ebcSMike Barcroft ip->ip_off = htons(ip->ip_off); 1204df8bae1dSRodney W. Grimes ip->ip_sum = 0; 120586b1d6d2SBill Fenner ip->ip_sum = in_cksum(copym, hlen); 1206201c2527SJulian Elischer #if 1 /* XXX */ 1207201c2527SJulian Elischer if (dst->sin_family != AF_INET) { 12082b8a366cSJulian Elischer printf("ip_mloopback: bad address family %d\n", 1209201c2527SJulian Elischer dst->sin_family); 1210201c2527SJulian Elischer dst->sin_family = AF_INET; 1211201c2527SJulian Elischer } 1212201c2527SJulian Elischer #endif 121306a429a3SArchie Cobbs if_simloop(ifp, copym, dst->sin_family, 0); 1214df8bae1dSRodney W. Grimes } 1215df8bae1dSRodney W. Grimes } 1216