1c398230bSWarner Losh /*- 2df8bae1dSRodney W. Grimes * Copyright (c) 1982, 1986, 1988, 1990, 1993 3df8bae1dSRodney W. Grimes * The Regents of the University of California. All rights reserved. 4df8bae1dSRodney W. Grimes * 5df8bae1dSRodney W. Grimes * Redistribution and use in source and binary forms, with or without 6df8bae1dSRodney W. Grimes * modification, are permitted provided that the following conditions 7df8bae1dSRodney W. Grimes * are met: 8df8bae1dSRodney W. Grimes * 1. Redistributions of source code must retain the above copyright 9df8bae1dSRodney W. Grimes * notice, this list of conditions and the following disclaimer. 10df8bae1dSRodney W. Grimes * 2. Redistributions in binary form must reproduce the above copyright 11df8bae1dSRodney W. Grimes * notice, this list of conditions and the following disclaimer in the 12df8bae1dSRodney W. Grimes * documentation and/or other materials provided with the distribution. 13df8bae1dSRodney W. Grimes * 4. Neither the name of the University nor the names of its contributors 14df8bae1dSRodney W. Grimes * may be used to endorse or promote products derived from this software 15df8bae1dSRodney W. Grimes * without specific prior written permission. 16df8bae1dSRodney W. Grimes * 17df8bae1dSRodney W. Grimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 18df8bae1dSRodney W. Grimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 19df8bae1dSRodney W. Grimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 20df8bae1dSRodney W. Grimes * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 21df8bae1dSRodney W. Grimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 22df8bae1dSRodney W. Grimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 23df8bae1dSRodney W. Grimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 24df8bae1dSRodney W. Grimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 25df8bae1dSRodney W. Grimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 26df8bae1dSRodney W. Grimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 27df8bae1dSRodney W. Grimes * SUCH DAMAGE. 28df8bae1dSRodney W. Grimes * 29df8bae1dSRodney W. Grimes * @(#)ip_output.c 8.3 (Berkeley) 1/21/94 30df8bae1dSRodney W. Grimes */ 31df8bae1dSRodney W. Grimes 324b421e2dSMike Silbersack #include <sys/cdefs.h> 334b421e2dSMike Silbersack __FBSDID("$FreeBSD$"); 344b421e2dSMike Silbersack 351f7e052cSJulian Elischer #include "opt_ipfw.h" 366a800098SYoshinobu Inoue #include "opt_ipsec.h" 3733553d6eSBjoern A. Zeeb #include "opt_route.h" 3853dcc544SMike Silbersack #include "opt_mbuf_stress_test.h" 39e440aed9SQing Li #include "opt_mpath.h" 402f4afd21SRandall Stewart #include "opt_sctp.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> 54df8bae1dSRodney W. Grimes 55df8bae1dSRodney W. Grimes #include <net/if.h> 563ef5e21dSQing Li #include <net/if_llatbl.h> 579b932e9eSAndre Oppermann #include <net/netisr.h> 58c21fd232SAndre Oppermann #include <net/pfil.h> 59df8bae1dSRodney W. Grimes #include <net/route.h> 6065111ec7SKip Macy #include <net/flowtable.h> 61e440aed9SQing Li #ifdef RADIX_MPATH 62e440aed9SQing Li #include <net/radix_mpath.h> 63e440aed9SQing Li #endif 644b79449eSBjoern A. Zeeb #include <net/vnet.h> 65df8bae1dSRodney W. Grimes 66df8bae1dSRodney W. Grimes #include <netinet/in.h> 67df8bae1dSRodney W. Grimes #include <netinet/in_systm.h> 68df8bae1dSRodney W. Grimes #include <netinet/ip.h> 69df8bae1dSRodney W. Grimes #include <netinet/in_pcb.h> 70df8bae1dSRodney W. Grimes #include <netinet/in_var.h> 71df8bae1dSRodney W. Grimes #include <netinet/ip_var.h> 72ef39adf0SAndre Oppermann #include <netinet/ip_options.h> 732f4afd21SRandall Stewart #ifdef SCTP 742f4afd21SRandall Stewart #include <netinet/sctp.h> 752f4afd21SRandall Stewart #include <netinet/sctp_crc32.h> 762f4afd21SRandall Stewart #endif 77df8bae1dSRodney W. Grimes 78b2630c29SGeorge V. Neville-Neil #ifdef IPSEC 792cb64cb2SGeorge V. Neville-Neil #include <netinet/ip_ipsec.h> 801dfcf0d2SAndre Oppermann #include <netipsec/ipsec.h> 81b2630c29SGeorge V. Neville-Neil #endif /* IPSEC*/ 826a800098SYoshinobu Inoue 831dfcf0d2SAndre Oppermann #include <machine/in_cksum.h> 841dfcf0d2SAndre Oppermann 85aed55708SRobert Watson #include <security/mac/mac_framework.h> 86aed55708SRobert Watson 87eddfbb76SRobert Watson VNET_DEFINE(u_short, ip_id); 88f23b4c91SGarrett Wollman 8953dcc544SMike Silbersack #ifdef MBUF_STRESS_TEST 9005b9d121SBjoern A. Zeeb static int mbuf_frag_size = 0; 919d9edc56SMike Silbersack SYSCTL_INT(_net_inet_ip, OID_AUTO, mbuf_frag_size, CTLFLAG_RW, 929d9edc56SMike Silbersack &mbuf_frag_size, 0, "Fragment outgoing mbufs to this size"); 939d9edc56SMike Silbersack #endif 949d9edc56SMike Silbersack 95df8bae1dSRodney W. Grimes static void ip_mloopback 964d77a549SAlfred Perlstein (struct ifnet *, struct mbuf *, struct sockaddr_in *, int); 97afed1b49SDarren Reed 98afed1b49SDarren Reed 998b889dbbSBruce M Simpson extern int in_mcast_loop; 10093e0e116SJulian Elischer extern struct protosw inetsw[]; 10193e0e116SJulian Elischer 102df8bae1dSRodney W. Grimes /* 103df8bae1dSRodney W. Grimes * IP output. The packet in mbuf chain m contains a skeletal IP 104df8bae1dSRodney W. Grimes * header (with len, off, ttl, proto, tos, src, dst). 105df8bae1dSRodney W. Grimes * The mbuf chain containing the packet will be freed. 106df8bae1dSRodney W. Grimes * The mbuf opt, if present, will not be freed. 107bf984051SGleb Smirnoff * If route ro is present and has ro_rt initialized, route lookup would be 108bf984051SGleb Smirnoff * skipped and ro->ro_rt would be used. If ro is present but ro->ro_rt is NULL, 109bf984051SGleb Smirnoff * then result of route lookup is stored in ro->ro_rt. 110bf984051SGleb Smirnoff * 1113de758d3SRobert Watson * In the IP forwarding case, the packet will arrive with options already 1123de758d3SRobert Watson * inserted, so must have a NULL opt pointer. 113df8bae1dSRodney W. Grimes */ 114df8bae1dSRodney W. Grimes int 115f2565d68SRobert Watson ip_output(struct mbuf *m, struct mbuf *opt, struct route *ro, int flags, 116f2565d68SRobert Watson struct ip_moptions *imo, struct inpcb *inp) 117df8bae1dSRodney W. Grimes { 1181e78ac21SJeffrey Hsu struct ip *ip; 119fc74d005SBjoern A. Zeeb struct ifnet *ifp = NULL; /* keep compiler happy */ 120ac9d7e26SMax Laier struct mbuf *m0; 12123bf9953SPoul-Henning Kamp int hlen = sizeof (struct ip); 1223ae2ad08SJohn-Mark Gurney int mtu; 123ca8b83b0SLuigi Rizzo int n; /* scratchpad */ 124ca8b83b0SLuigi Rizzo int error = 0; 125ca8b83b0SLuigi Rizzo struct sockaddr_in *dst; 1263c73180fSGleb Smirnoff struct in_ifaddr *ia; 12721d172a3SGleb Smirnoff int isbroadcast; 12821d172a3SGleb Smirnoff uint16_t ip_len, ip_off, sw_csum; 129e3f406b3SRuslan Ermilov struct route iproute; 130ec396e61SLuigi Rizzo struct rtentry *rte; /* cache for ro->ro_rt */ 1319b932e9eSAndre Oppermann struct in_addr odst; 1329b932e9eSAndre Oppermann #ifdef IPFIREWALL_FORWARD 1339b932e9eSAndre Oppermann struct m_tag *fwd_tag = NULL; 1349b932e9eSAndre Oppermann #endif 1351a499816SEdward Tomasz Napierala #ifdef IPSEC 1361a499816SEdward Tomasz Napierala int no_route_but_check_spd = 0; 1371a499816SEdward Tomasz Napierala #endif 138ac9d7e26SMax Laier M_ASSERTPKTHDR(m); 13936e8826fSMax Laier 140bc97ba51SJulian Elischer if (inp != NULL) { 141baa45840SRobert Watson INP_LOCK_ASSERT(inp); 14262e1ba83SRobert Watson M_SETFIB(m, inp->inp_inc.inc_fibnum); 14380cb9f21SKip Macy if (inp->inp_flags & (INP_HW_FLOWID|INP_SW_FLOWID)) { 14480cb9f21SKip Macy m->m_pkthdr.flowid = inp->inp_flowid; 14580cb9f21SKip Macy m->m_flags |= M_FLOWID; 14680cb9f21SKip Macy } 147bc97ba51SJulian Elischer } 14862e1ba83SRobert Watson 14962e1ba83SRobert Watson if (ro == NULL) { 15062e1ba83SRobert Watson ro = &iproute; 15162e1ba83SRobert Watson bzero(ro, sizeof (*ro)); 152bf984051SGleb Smirnoff } 15362e1ba83SRobert Watson 15453be8fcaSBjoern A. Zeeb #ifdef FLOWTABLE 155bf984051SGleb Smirnoff if (ro->ro_rt == NULL) { 156d4121a02SKip Macy struct flentry *fle; 157d4121a02SKip Macy 15862e1ba83SRobert Watson /* 15962e1ba83SRobert Watson * The flow table returns route entries valid for up to 30 16062e1ba83SRobert Watson * seconds; we rely on the remainder of ip_output() taking no 16162e1ba83SRobert Watson * longer than that long for the stability of ro_rt. The 16262e1ba83SRobert Watson * flow ID assignment must have happened before this point. 16362e1ba83SRobert Watson */ 164bf984051SGleb Smirnoff fle = flowtable_lookup_mbuf(V_ip_ft, m, AF_INET); 165bf984051SGleb Smirnoff if (fle != NULL) 166d4121a02SKip Macy flow_to_route(fle, ro); 167d4121a02SKip Macy } 16853be8fcaSBjoern A. Zeeb #endif 1692b25acc1SLuigi Rizzo 170df8bae1dSRodney W. Grimes if (opt) { 171ca8b83b0SLuigi Rizzo int len = 0; 172df8bae1dSRodney W. Grimes m = ip_insertoptions(m, opt, &len); 173cb7641e8SMaxim Konovalov if (len != 0) 174ca8b83b0SLuigi Rizzo hlen = len; /* ip->ip_hl is updated above */ 175df8bae1dSRodney W. Grimes } 176df8bae1dSRodney W. Grimes ip = mtod(m, struct ip *); 177*8f134647SGleb Smirnoff ip_len = ntohs(ip->ip_len); 178*8f134647SGleb Smirnoff ip_off = ntohs(ip->ip_off); 1793efc3014SJulian Elischer 180df8bae1dSRodney W. Grimes /* 1816e49b1feSGarrett Wollman * Fill in IP header. If we are not allowing fragmentation, 182e0f630eaSAndre Oppermann * then the ip_id field is meaningless, but we don't set it 183e0f630eaSAndre Oppermann * to zero. Doing so causes various problems when devices along 184e0f630eaSAndre Oppermann * the path (routers, load balancers, firewalls, etc.) illegally 185e0f630eaSAndre Oppermann * disable DF on our packet. Note that a 16-bit counter 1866e49b1feSGarrett Wollman * will wrap around in less than 10 seconds at 100 Mbit/s on a 1876e49b1feSGarrett Wollman * medium with MTU 1500. See Steven M. Bellovin, "A Technique 1886e49b1feSGarrett Wollman * for Counting NATted Hosts", Proc. IMW'02, available at 1894d09f5a0SGleb Smirnoff * <http://www.cs.columbia.edu/~smb/papers/fnat.pdf>. 190df8bae1dSRodney W. Grimes */ 191df8bae1dSRodney W. Grimes if ((flags & (IP_FORWARDING|IP_RAWOUTPUT)) == 0) { 19253be11f6SPoul-Henning Kamp ip->ip_v = IPVERSION; 19353be11f6SPoul-Henning Kamp ip->ip_hl = hlen >> 2; 1941f44b0a1SDavid Malone ip->ip_id = ip_newid(); 19586425c62SRobert Watson IPSTAT_INC(ips_localout); 196df8bae1dSRodney W. Grimes } else { 197ca8b83b0SLuigi Rizzo /* Header already set, fetch hlen from there */ 19853be11f6SPoul-Henning Kamp hlen = ip->ip_hl << 2; 199df8bae1dSRodney W. Grimes } 2009c9137eaSGarrett Wollman 201df8bae1dSRodney W. Grimes dst = (struct sockaddr_in *)&ro->ro_dst; 2029b932e9eSAndre Oppermann again: 2033c73180fSGleb Smirnoff ia = NULL; 204df8bae1dSRodney W. Grimes /* 205df8bae1dSRodney W. Grimes * If there is a cached route, 206df8bae1dSRodney W. Grimes * check that it is to the same destination 207df8bae1dSRodney W. Grimes * and is still up. If not, free it and try again. 208a4a6e773SHajimu UMEMOTO * The address family should also be checked in case of sharing the 209a4a6e773SHajimu UMEMOTO * cache with IPv6. 210df8bae1dSRodney W. Grimes */ 211ec396e61SLuigi Rizzo rte = ro->ro_rt; 212ec396e61SLuigi Rizzo if (rte && ((rte->rt_flags & RTF_UP) == 0 || 213c7ea0aa6SQing Li rte->rt_ifp == NULL || 214c7ea0aa6SQing Li !RT_LINK_IS_UP(rte->rt_ifp) || 215a4a6e773SHajimu UMEMOTO dst->sin_family != AF_INET || 2169b932e9eSAndre Oppermann dst->sin_addr.s_addr != ip->ip_dst.s_addr)) { 217bf984051SGleb Smirnoff RO_RTFREE(ro); 218bf984051SGleb Smirnoff ro->ro_lle = NULL; 219bf984051SGleb Smirnoff rte = NULL; 220df8bae1dSRodney W. Grimes } 2219b932e9eSAndre Oppermann #ifdef IPFIREWALL_FORWARD 222ec396e61SLuigi Rizzo if (rte == NULL && fwd_tag == NULL) { 2239b932e9eSAndre Oppermann #else 224ec396e61SLuigi Rizzo if (rte == NULL) { 2259b932e9eSAndre Oppermann #endif 226a4a6e773SHajimu UMEMOTO bzero(dst, sizeof(*dst)); 227df8bae1dSRodney W. Grimes dst->sin_family = AF_INET; 228df8bae1dSRodney W. Grimes dst->sin_len = sizeof(*dst); 2299b932e9eSAndre Oppermann dst->sin_addr = ip->ip_dst; 230df8bae1dSRodney W. Grimes } 231df8bae1dSRodney W. Grimes /* 232a3fd02d8SBruce M Simpson * If routing to interface only, short circuit routing lookup. 233a3fd02d8SBruce M Simpson * The use of an all-ones broadcast address implies this; an 234a3fd02d8SBruce M Simpson * interface is specified by the broadcast address of an interface, 235a3fd02d8SBruce M Simpson * or the destination address of a ptp interface. 236df8bae1dSRodney W. Grimes */ 237a3fd02d8SBruce M Simpson if (flags & IP_SENDONES) { 238a3fd02d8SBruce M Simpson if ((ia = ifatoia(ifa_ifwithbroadaddr(sintosa(dst)))) == NULL && 239a3fd02d8SBruce M Simpson (ia = ifatoia(ifa_ifwithdstaddr(sintosa(dst)))) == NULL) { 24086425c62SRobert Watson IPSTAT_INC(ips_noroute); 241a3fd02d8SBruce M Simpson error = ENETUNREACH; 242a3fd02d8SBruce M Simpson goto bad; 243a3fd02d8SBruce M Simpson } 244a3fd02d8SBruce M Simpson ip->ip_dst.s_addr = INADDR_BROADCAST; 245a3fd02d8SBruce M Simpson dst->sin_addr = ip->ip_dst; 246a3fd02d8SBruce M Simpson ifp = ia->ia_ifp; 247a3fd02d8SBruce M Simpson ip->ip_ttl = 1; 248a3fd02d8SBruce M Simpson isbroadcast = 1; 249a3fd02d8SBruce M Simpson } else if (flags & IP_ROUTETOIF) { 2500b17fba7SAndre Oppermann if ((ia = ifatoia(ifa_ifwithdstaddr(sintosa(dst)))) == NULL && 2510ed6142bSQing Li (ia = ifatoia(ifa_ifwithnet(sintosa(dst), 0))) == NULL) { 25286425c62SRobert Watson IPSTAT_INC(ips_noroute); 253df8bae1dSRodney W. Grimes error = ENETUNREACH; 254df8bae1dSRodney W. Grimes goto bad; 255df8bae1dSRodney W. Grimes } 256df8bae1dSRodney W. Grimes ifp = ia->ia_ifp; 257df8bae1dSRodney W. Grimes ip->ip_ttl = 1; 2589f9b3dc4SGarrett Wollman isbroadcast = in_broadcast(dst->sin_addr, ifp); 2593afefa39SDaniel C. Sobral } else if (IN_MULTICAST(ntohl(ip->ip_dst.s_addr)) && 26038c1bc35SRuslan Ermilov imo != NULL && imo->imo_multicast_ifp != NULL) { 2613afefa39SDaniel C. Sobral /* 26238c1bc35SRuslan Ermilov * Bypass the normal routing lookup for multicast 26338c1bc35SRuslan Ermilov * packets if the interface is specified. 2643afefa39SDaniel C. Sobral */ 26538c1bc35SRuslan Ermilov ifp = imo->imo_multicast_ifp; 26638c1bc35SRuslan Ermilov IFP_TO_IA(ifp, ia); 26738c1bc35SRuslan Ermilov isbroadcast = 0; /* fool gcc */ 268df8bae1dSRodney W. Grimes } else { 2692c17fe93SGarrett Wollman /* 27097d8d152SAndre Oppermann * We want to do any cloning requested by the link layer, 27197d8d152SAndre Oppermann * as this is probably required in all cases for correct 27297d8d152SAndre Oppermann * operation (as it is for ARP). 2732c17fe93SGarrett Wollman */ 274ec396e61SLuigi Rizzo if (rte == NULL) { 275e440aed9SQing Li #ifdef RADIX_MPATH 2768b07e49aSJulian Elischer rtalloc_mpath_fib(ro, 2778b07e49aSJulian Elischer ntohl(ip->ip_src.s_addr ^ ip->ip_dst.s_addr), 2788b07e49aSJulian Elischer inp ? inp->inp_inc.inc_fibnum : M_GETFIB(m)); 279e440aed9SQing Li #else 2808b07e49aSJulian Elischer in_rtalloc_ign(ro, 0, 2818b07e49aSJulian Elischer inp ? inp->inp_inc.inc_fibnum : M_GETFIB(m)); 282e440aed9SQing Li #endif 283ec396e61SLuigi Rizzo rte = ro->ro_rt; 284ec396e61SLuigi Rizzo } 285c7ea0aa6SQing Li if (rte == NULL || 286c7ea0aa6SQing Li rte->rt_ifp == NULL || 287c7ea0aa6SQing Li !RT_LINK_IS_UP(rte->rt_ifp)) { 2881a499816SEdward Tomasz Napierala #ifdef IPSEC 2891a499816SEdward Tomasz Napierala /* 2901a499816SEdward Tomasz Napierala * There is no route for this packet, but it is 2911a499816SEdward Tomasz Napierala * possible that a matching SPD entry exists. 2921a499816SEdward Tomasz Napierala */ 2931a499816SEdward Tomasz Napierala no_route_but_check_spd = 1; 2941a499816SEdward Tomasz Napierala mtu = 0; /* Silence GCC warning. */ 2951a499816SEdward Tomasz Napierala goto sendit; 2961a499816SEdward Tomasz Napierala #endif 29786425c62SRobert Watson IPSTAT_INC(ips_noroute); 298df8bae1dSRodney W. Grimes error = EHOSTUNREACH; 299df8bae1dSRodney W. Grimes goto bad; 300df8bae1dSRodney W. Grimes } 301ec396e61SLuigi Rizzo ia = ifatoia(rte->rt_ifa); 3028c0fec80SRobert Watson ifa_ref(&ia->ia_ifa); 303ec396e61SLuigi Rizzo ifp = rte->rt_ifp; 304ec396e61SLuigi Rizzo rte->rt_rmx.rmx_pksent++; 305ec396e61SLuigi Rizzo if (rte->rt_flags & RTF_GATEWAY) 306ec396e61SLuigi Rizzo dst = (struct sockaddr_in *)rte->rt_gateway; 307ec396e61SLuigi Rizzo if (rte->rt_flags & RTF_HOST) 308ec396e61SLuigi Rizzo isbroadcast = (rte->rt_flags & RTF_BROADCAST); 3099f9b3dc4SGarrett Wollman else 3109f9b3dc4SGarrett Wollman isbroadcast = in_broadcast(dst->sin_addr, ifp); 311df8bae1dSRodney W. Grimes } 3123ae2ad08SJohn-Mark Gurney /* 3133ae2ad08SJohn-Mark Gurney * Calculate MTU. If we have a route that is up, use that, 3143ae2ad08SJohn-Mark Gurney * otherwise use the interface's MTU. 3153ae2ad08SJohn-Mark Gurney */ 316ec396e61SLuigi Rizzo if (rte != NULL && (rte->rt_flags & (RTF_UP|RTF_HOST))) { 3173ae2ad08SJohn-Mark Gurney /* 3183ae2ad08SJohn-Mark Gurney * This case can happen if the user changed the MTU 3193ae2ad08SJohn-Mark Gurney * of an interface after enabling IP on it. Because 3203ae2ad08SJohn-Mark Gurney * most netifs don't keep track of routes pointing to 3213ae2ad08SJohn-Mark Gurney * them, there is no way for one to update all its 3223ae2ad08SJohn-Mark Gurney * routes when the MTU is changed. 3233ae2ad08SJohn-Mark Gurney */ 324ec396e61SLuigi Rizzo if (rte->rt_rmx.rmx_mtu > ifp->if_mtu) 325ec396e61SLuigi Rizzo rte->rt_rmx.rmx_mtu = ifp->if_mtu; 326ec396e61SLuigi Rizzo mtu = rte->rt_rmx.rmx_mtu; 3273ae2ad08SJohn-Mark Gurney } else { 3283ae2ad08SJohn-Mark Gurney mtu = ifp->if_mtu; 3293ae2ad08SJohn-Mark Gurney } 330c744cde4SBjoern A. Zeeb /* Catch a possible divide by zero later. */ 331c744cde4SBjoern A. Zeeb KASSERT(mtu > 0, ("%s: mtu %d <= 0, rte=%p (rt_flags=0x%08x) ifp=%p", 332c744cde4SBjoern A. Zeeb __func__, mtu, rte, (rte != NULL) ? rte->rt_flags : 0, ifp)); 3339b932e9eSAndre Oppermann if (IN_MULTICAST(ntohl(ip->ip_dst.s_addr))) { 334df8bae1dSRodney W. Grimes m->m_flags |= M_MCAST; 335df8bae1dSRodney W. Grimes /* 336df8bae1dSRodney W. Grimes * IP destination address is multicast. Make sure "dst" 337df8bae1dSRodney W. Grimes * still points to the address in "ro". (It may have been 338df8bae1dSRodney W. Grimes * changed to point to a gateway address, above.) 339df8bae1dSRodney W. Grimes */ 340df8bae1dSRodney W. Grimes dst = (struct sockaddr_in *)&ro->ro_dst; 341df8bae1dSRodney W. Grimes /* 342df8bae1dSRodney W. Grimes * See if the caller provided any multicast options 343df8bae1dSRodney W. Grimes */ 344df8bae1dSRodney W. Grimes if (imo != NULL) { 345df8bae1dSRodney W. Grimes ip->ip_ttl = imo->imo_multicast_ttl; 3461c5de19aSGarrett Wollman if (imo->imo_multicast_vif != -1) 3471c5de19aSGarrett Wollman ip->ip_src.s_addr = 348bbb4330bSLuigi Rizzo ip_mcast_src ? 349bbb4330bSLuigi Rizzo ip_mcast_src(imo->imo_multicast_vif) : 350bbb4330bSLuigi Rizzo INADDR_ANY; 351df8bae1dSRodney W. Grimes } else 352df8bae1dSRodney W. Grimes ip->ip_ttl = IP_DEFAULT_MULTICAST_TTL; 353df8bae1dSRodney W. Grimes /* 354df8bae1dSRodney W. Grimes * Confirm that the outgoing interface supports multicast. 355df8bae1dSRodney W. Grimes */ 3561c5de19aSGarrett Wollman if ((imo == NULL) || (imo->imo_multicast_vif == -1)) { 357df8bae1dSRodney W. Grimes if ((ifp->if_flags & IFF_MULTICAST) == 0) { 35886425c62SRobert Watson IPSTAT_INC(ips_noroute); 359df8bae1dSRodney W. Grimes error = ENETUNREACH; 360df8bae1dSRodney W. Grimes goto bad; 361df8bae1dSRodney W. Grimes } 3621c5de19aSGarrett Wollman } 363df8bae1dSRodney W. Grimes /* 364df8bae1dSRodney W. Grimes * If source address not specified yet, use address 365df8bae1dSRodney W. Grimes * of outgoing interface. 366df8bae1dSRodney W. Grimes */ 367df8bae1dSRodney W. Grimes if (ip->ip_src.s_addr == INADDR_ANY) { 36838c1bc35SRuslan Ermilov /* Interface may have no addresses. */ 36938c1bc35SRuslan Ermilov if (ia != NULL) 37038c1bc35SRuslan Ermilov ip->ip_src = IA_SIN(ia)->sin_addr; 371df8bae1dSRodney W. Grimes } 372df8bae1dSRodney W. Grimes 3738b889dbbSBruce M Simpson if ((imo == NULL && in_mcast_loop) || 3748b889dbbSBruce M Simpson (imo && imo->imo_multicast_loop)) { 375df8bae1dSRodney W. Grimes /* 3768b889dbbSBruce M Simpson * Loop back multicast datagram if not expressly 3778b889dbbSBruce M Simpson * forbidden to do so, even if we are not a member 3788b889dbbSBruce M Simpson * of the group; ip_input() will filter it later, 3798b889dbbSBruce M Simpson * thus deferring a hash lookup and mutex acquisition 3808b889dbbSBruce M Simpson * at the expense of a cheap copy using m_copym(). 381df8bae1dSRodney W. Grimes */ 38286b1d6d2SBill Fenner ip_mloopback(ifp, m, dst, hlen); 3838b889dbbSBruce M Simpson } else { 384df8bae1dSRodney W. Grimes /* 385df8bae1dSRodney W. Grimes * If we are acting as a multicast router, perform 386df8bae1dSRodney W. Grimes * multicast forwarding as if the packet had just 387df8bae1dSRodney W. Grimes * arrived on the interface to which we are about 388df8bae1dSRodney W. Grimes * to send. The multicast forwarding function 389df8bae1dSRodney W. Grimes * recursively calls this function, using the 390df8bae1dSRodney W. Grimes * IP_FORWARDING flag to prevent infinite recursion. 391df8bae1dSRodney W. Grimes * 392df8bae1dSRodney W. Grimes * Multicasts that are looped back by ip_mloopback(), 393df8bae1dSRodney W. Grimes * above, will be forwarded by the ip_input() routine, 394df8bae1dSRodney W. Grimes * if necessary. 395df8bae1dSRodney W. Grimes */ 396603724d3SBjoern A. Zeeb if (V_ip_mrouter && (flags & IP_FORWARDING) == 0) { 397f0068c4aSGarrett Wollman /* 398bbb4330bSLuigi Rizzo * If rsvp daemon is not running, do not 399f0068c4aSGarrett Wollman * set ip_moptions. This ensures that the packet 400f0068c4aSGarrett Wollman * is multicast and not just sent down one link 401f0068c4aSGarrett Wollman * as prescribed by rsvpd. 402f0068c4aSGarrett Wollman */ 403603724d3SBjoern A. Zeeb if (!V_rsvp_on) 404f0068c4aSGarrett Wollman imo = NULL; 405bbb4330bSLuigi Rizzo if (ip_mforward && 406bbb4330bSLuigi Rizzo ip_mforward(ip, ifp, m, imo) != 0) { 407df8bae1dSRodney W. Grimes m_freem(m); 408df8bae1dSRodney W. Grimes goto done; 409df8bae1dSRodney W. Grimes } 410df8bae1dSRodney W. Grimes } 411df8bae1dSRodney W. Grimes } 4125e9ae478SGarrett Wollman 413df8bae1dSRodney W. Grimes /* 414df8bae1dSRodney W. Grimes * Multicasts with a time-to-live of zero may be looped- 415df8bae1dSRodney W. Grimes * back, above, but must not be transmitted on a network. 416df8bae1dSRodney W. Grimes * Also, multicasts addressed to the loopback interface 417df8bae1dSRodney W. Grimes * are not sent -- the above call to ip_mloopback() will 4188b889dbbSBruce M Simpson * loop back a copy. ip_input() will drop the copy if 4198b889dbbSBruce M Simpson * this host does not belong to the destination group on 4208b889dbbSBruce M Simpson * the loopback interface. 421df8bae1dSRodney W. Grimes */ 422f5fea3ddSPaul Traina if (ip->ip_ttl == 0 || ifp->if_flags & IFF_LOOPBACK) { 423df8bae1dSRodney W. Grimes m_freem(m); 424df8bae1dSRodney W. Grimes goto done; 425df8bae1dSRodney W. Grimes } 426df8bae1dSRodney W. Grimes 427df8bae1dSRodney W. Grimes goto sendit; 428df8bae1dSRodney W. Grimes } 4296a7c943cSAndre Oppermann 430df8bae1dSRodney W. Grimes /* 4312b25acc1SLuigi Rizzo * If the source address is not specified yet, use the address 432cb459254SJohn-Mark Gurney * of the outoing interface. 433df8bae1dSRodney W. Grimes */ 434f9e354dfSJulian Elischer if (ip->ip_src.s_addr == INADDR_ANY) { 43538c1bc35SRuslan Ermilov /* Interface may have no addresses. */ 43638c1bc35SRuslan Ermilov if (ia != NULL) { 437df8bae1dSRodney W. Grimes ip->ip_src = IA_SIN(ia)->sin_addr; 438f9e354dfSJulian Elischer } 43938c1bc35SRuslan Ermilov } 4406a7c943cSAndre Oppermann 441ca7a789aSMax Laier /* 442ca7a789aSMax Laier * Verify that we have any chance at all of being able to queue the 443ca7a789aSMax Laier * packet or packet fragments, unless ALTQ is enabled on the given 444ca7a789aSMax Laier * interface in which case packetdrop should be done by queueing. 445ca7a789aSMax Laier */ 446*8f134647SGleb Smirnoff n = ip_len / mtu + 1; /* how many fragments ? */ 447ca8b83b0SLuigi Rizzo if ( 44802b199f1SMax Laier #ifdef ALTQ 449ca8b83b0SLuigi Rizzo (!ALTQ_IS_ENABLED(&ifp->if_snd)) && 450ca7a789aSMax Laier #endif /* ALTQ */ 451ca8b83b0SLuigi Rizzo (ifp->if_snd.ifq_len + n) >= ifp->if_snd.ifq_maxlen ) { 452b5390296SDavid Greenman error = ENOBUFS; 45386425c62SRobert Watson IPSTAT_INC(ips_odropped); 454ca8b83b0SLuigi Rizzo ifp->if_snd.ifq_drops += n; 455b5390296SDavid Greenman goto bad; 456b5390296SDavid Greenman } 457b5390296SDavid Greenman 458b5390296SDavid Greenman /* 459df8bae1dSRodney W. Grimes * Look for broadcast address and 4608c3f5566SRuslan Ermilov * verify user is allowed to send 461df8bae1dSRodney W. Grimes * such a packet. 462df8bae1dSRodney W. Grimes */ 4639f9b3dc4SGarrett Wollman if (isbroadcast) { 464df8bae1dSRodney W. Grimes if ((ifp->if_flags & IFF_BROADCAST) == 0) { 465df8bae1dSRodney W. Grimes error = EADDRNOTAVAIL; 466df8bae1dSRodney W. Grimes goto bad; 467df8bae1dSRodney W. Grimes } 468df8bae1dSRodney W. Grimes if ((flags & IP_ALLOWBROADCAST) == 0) { 469df8bae1dSRodney W. Grimes error = EACCES; 470df8bae1dSRodney W. Grimes goto bad; 471df8bae1dSRodney W. Grimes } 472df8bae1dSRodney W. Grimes /* don't allow broadcast messages to be fragmented */ 473*8f134647SGleb Smirnoff if (ip_len > mtu) { 474df8bae1dSRodney W. Grimes error = EMSGSIZE; 475df8bae1dSRodney W. Grimes goto bad; 476df8bae1dSRodney W. Grimes } 477df8bae1dSRodney W. Grimes m->m_flags |= M_BCAST; 4789f9b3dc4SGarrett Wollman } else { 479df8bae1dSRodney W. Grimes m->m_flags &= ~M_BCAST; 4809f9b3dc4SGarrett Wollman } 481df8bae1dSRodney W. Grimes 482f1743588SDarren Reed sendit: 483b2630c29SGeorge V. Neville-Neil #ifdef IPSEC 4844f7418a0SEdward Tomasz Napierala switch(ip_ipsec_output(&m, inp, &flags, &error)) { 4851dfcf0d2SAndre Oppermann case 1: 48633841545SHajimu UMEMOTO goto bad; 4871dfcf0d2SAndre Oppermann case -1: 4881dfcf0d2SAndre Oppermann goto done; 4891dfcf0d2SAndre Oppermann case 0: 49033841545SHajimu UMEMOTO default: 4911dfcf0d2SAndre Oppermann break; /* Continue with packet processing. */ 49233841545SHajimu UMEMOTO } 4931a499816SEdward Tomasz Napierala /* 4941a499816SEdward Tomasz Napierala * Check if there was a route for this packet; return error if not. 4951a499816SEdward Tomasz Napierala */ 4961a499816SEdward Tomasz Napierala if (no_route_but_check_spd) { 4971a499816SEdward Tomasz Napierala IPSTAT_INC(ips_noroute); 4981a499816SEdward Tomasz Napierala error = EHOSTUNREACH; 4991a499816SEdward Tomasz Napierala goto bad; 5001a499816SEdward Tomasz Napierala } 5011dfcf0d2SAndre Oppermann /* Update variables that are affected by ipsec4_output(). */ 50233841545SHajimu UMEMOTO ip = mtod(m, struct ip *); 50333841545SHajimu UMEMOTO hlen = ip->ip_hl << 2; 504b2630c29SGeorge V. Neville-Neil #endif /* IPSEC */ 50533841545SHajimu UMEMOTO 506c21fd232SAndre Oppermann /* Jump over all PFIL processing if hooks are not active. */ 5070b4b0b0fSJulian Elischer if (!PFIL_HOOKED(&V_inet_pfil_hook)) 508c21fd232SAndre Oppermann goto passout; 509c21fd232SAndre Oppermann 510c21fd232SAndre Oppermann /* Run through list of hooks for output packets. */ 5119b932e9eSAndre Oppermann odst.s_addr = ip->ip_dst.s_addr; 5120b4b0b0fSJulian Elischer error = pfil_run_hooks(&V_inet_pfil_hook, &m, ifp, PFIL_OUT, inp); 513134ea224SSam Leffler if (error != 0 || m == NULL) 514c4ac87eaSDarren Reed goto done; 5159b932e9eSAndre Oppermann 516c4ac87eaSDarren Reed ip = mtod(m, struct ip *); 517fed1c7e9SSøren Schmidt 5189b932e9eSAndre Oppermann /* See if destination IP address was changed by packet filter. */ 5199b932e9eSAndre Oppermann if (odst.s_addr != ip->ip_dst.s_addr) { 5209b932e9eSAndre Oppermann m->m_flags |= M_SKIP_FIREWALL; 521f4fca2d8SAndre Oppermann /* If destination is now ourself drop to ip_input(). */ 5229b932e9eSAndre Oppermann if (in_localip(ip->ip_dst)) { 5239b932e9eSAndre Oppermann m->m_flags |= M_FASTFWD_OURS; 524f9e354dfSJulian Elischer if (m->m_pkthdr.rcvif == NULL) 525603724d3SBjoern A. Zeeb m->m_pkthdr.rcvif = V_loif; 52620c822f3SJonathan Lemon if (m->m_pkthdr.csum_flags & CSUM_DELAY_DATA) { 52720c822f3SJonathan Lemon m->m_pkthdr.csum_flags |= 52820c822f3SJonathan Lemon CSUM_DATA_VALID | CSUM_PSEUDO_HDR; 529ac9d7e26SMax Laier m->m_pkthdr.csum_data = 0xffff; 53020c822f3SJonathan Lemon } 53120c822f3SJonathan Lemon m->m_pkthdr.csum_flags |= 53220c822f3SJonathan Lemon CSUM_IP_CHECKED | CSUM_IP_VALID; 5332f4afd21SRandall Stewart #ifdef SCTP 5342f4afd21SRandall Stewart if (m->m_pkthdr.csum_flags & CSUM_SCTP) 5352f4afd21SRandall Stewart m->m_pkthdr.csum_flags |= CSUM_SCTP_VALID; 5362f4afd21SRandall Stewart #endif 5379b932e9eSAndre Oppermann error = netisr_queue(NETISR_IP, m); 5389b932e9eSAndre Oppermann goto done; 5393c73180fSGleb Smirnoff } else { 5403c73180fSGleb Smirnoff if (ia != NULL) 5413c73180fSGleb Smirnoff ifa_free(&ia->ia_ifa); 542f4fca2d8SAndre Oppermann goto again; /* Redo the routing table lookup. */ 5439b932e9eSAndre Oppermann } 5443c73180fSGleb Smirnoff } 5459b932e9eSAndre Oppermann 5469b932e9eSAndre Oppermann #ifdef IPFIREWALL_FORWARD 5479b932e9eSAndre Oppermann /* See if local, if yes, send it to netisr with IP_FASTFWD_OURS. */ 5489b932e9eSAndre Oppermann if (m->m_flags & M_FASTFWD_OURS) { 5499b932e9eSAndre Oppermann if (m->m_pkthdr.rcvif == NULL) 550603724d3SBjoern A. Zeeb m->m_pkthdr.rcvif = V_loif; 5519b932e9eSAndre Oppermann if (m->m_pkthdr.csum_flags & CSUM_DELAY_DATA) { 5529b932e9eSAndre Oppermann m->m_pkthdr.csum_flags |= 5539b932e9eSAndre Oppermann CSUM_DATA_VALID | CSUM_PSEUDO_HDR; 5549b932e9eSAndre Oppermann m->m_pkthdr.csum_data = 0xffff; 5559b932e9eSAndre Oppermann } 5562f4afd21SRandall Stewart #ifdef SCTP 5572f4afd21SRandall Stewart if (m->m_pkthdr.csum_flags & CSUM_SCTP) 5582f4afd21SRandall Stewart m->m_pkthdr.csum_flags |= CSUM_SCTP_VALID; 5592f4afd21SRandall Stewart #endif 5609b932e9eSAndre Oppermann m->m_pkthdr.csum_flags |= 5619b932e9eSAndre Oppermann CSUM_IP_CHECKED | CSUM_IP_VALID; 5629b932e9eSAndre Oppermann 5639b932e9eSAndre Oppermann error = netisr_queue(NETISR_IP, m); 564f9e354dfSJulian Elischer goto done; 565f9e354dfSJulian Elischer } 5669b932e9eSAndre Oppermann /* Or forward to some other address? */ 5679b932e9eSAndre Oppermann fwd_tag = m_tag_find(m, PACKET_TAG_IPFORWARD, NULL); 5689b932e9eSAndre Oppermann if (fwd_tag) { 5699b932e9eSAndre Oppermann dst = (struct sockaddr_in *)&ro->ro_dst; 5709b932e9eSAndre Oppermann bcopy((fwd_tag+1), dst, sizeof(struct sockaddr_in)); 5719b932e9eSAndre Oppermann m->m_flags |= M_SKIP_FIREWALL; 5729b932e9eSAndre Oppermann m_tag_delete(m, fwd_tag); 5733c73180fSGleb Smirnoff if (ia != NULL) 5743c73180fSGleb Smirnoff ifa_free(&ia->ia_ifa); 5759b932e9eSAndre Oppermann goto again; 576099dd043SAndre Oppermann } 577099dd043SAndre Oppermann #endif /* IPFIREWALL_FORWARD */ 5782b25acc1SLuigi Rizzo 579c21fd232SAndre Oppermann passout: 58051c8ec4aSRuslan Ermilov /* 127/8 must not appear on wire - RFC1122. */ 58151c8ec4aSRuslan Ermilov if ((ntohl(ip->ip_dst.s_addr) >> IN_CLASSA_NSHIFT) == IN_LOOPBACKNET || 58251c8ec4aSRuslan Ermilov (ntohl(ip->ip_src.s_addr) >> IN_CLASSA_NSHIFT) == IN_LOOPBACKNET) { 58351c8ec4aSRuslan Ermilov if ((ifp->if_flags & IFF_LOOPBACK) == 0) { 58486425c62SRobert Watson IPSTAT_INC(ips_badaddr); 58551c8ec4aSRuslan Ermilov error = EADDRNOTAVAIL; 58651c8ec4aSRuslan Ermilov goto bad; 58751c8ec4aSRuslan Ermilov } 58851c8ec4aSRuslan Ermilov } 58951c8ec4aSRuslan Ermilov 590206a3274SRuslan Ermilov m->m_pkthdr.csum_flags |= CSUM_IP; 591206a3274SRuslan Ermilov sw_csum = m->m_pkthdr.csum_flags & ~ifp->if_hwassist; 592db4f9cc7SJonathan Lemon if (sw_csum & CSUM_DELAY_DATA) { 593db4f9cc7SJonathan Lemon in_delayed_cksum(m); 594db4f9cc7SJonathan Lemon sw_csum &= ~CSUM_DELAY_DATA; 595db4f9cc7SJonathan Lemon } 5962f4afd21SRandall Stewart #ifdef SCTP 5972f4afd21SRandall Stewart if (sw_csum & CSUM_SCTP) { 5981966e5b5SRandall Stewart sctp_delayed_cksum(m, (uint32_t)(ip->ip_hl << 2)); 5992f4afd21SRandall Stewart sw_csum &= ~CSUM_SCTP; 6002f4afd21SRandall Stewart } 6012f4afd21SRandall Stewart #endif 602206a3274SRuslan Ermilov m->m_pkthdr.csum_flags &= ifp->if_hwassist; 603db4f9cc7SJonathan Lemon 604e7319babSPoul-Henning Kamp /* 605db4f9cc7SJonathan Lemon * If small enough for interface, or the interface will take 606233dcce1SAndre Oppermann * care of the fragmentation for us, we can just send directly. 607df8bae1dSRodney W. Grimes */ 60821d172a3SGleb Smirnoff if (ip_len <= mtu || 609233dcce1SAndre Oppermann (m->m_pkthdr.csum_flags & ifp->if_hwassist & CSUM_TSO) != 0 || 61021d172a3SGleb Smirnoff ((ip_off & IP_DF) == 0 && (ifp->if_hwassist & CSUM_FRAGMENT))) { 611df8bae1dSRodney W. Grimes ip->ip_sum = 0; 61253be11f6SPoul-Henning Kamp if (sw_csum & CSUM_DELAY_IP) 613df8bae1dSRodney W. Grimes ip->ip_sum = in_cksum(m, hlen); 6145da9f8faSJosef Karthauser 615233dcce1SAndre Oppermann /* 616233dcce1SAndre Oppermann * Record statistics for this interface address. 617233dcce1SAndre Oppermann * With CSUM_TSO the byte/packet count will be slightly 618233dcce1SAndre Oppermann * incorrect because we count the IP+TCP headers only 619233dcce1SAndre Oppermann * once instead of for every generated packet. 620233dcce1SAndre Oppermann */ 62138c1bc35SRuslan Ermilov if (!(flags & IP_FORWARDING) && ia) { 622233dcce1SAndre Oppermann if (m->m_pkthdr.csum_flags & CSUM_TSO) 6233dbee59bSBruce M Simpson ia->ia_ifa.if_opackets += 624233dcce1SAndre Oppermann m->m_pkthdr.len / m->m_pkthdr.tso_segsz; 625233dcce1SAndre Oppermann else 6263dbee59bSBruce M Simpson ia->ia_ifa.if_opackets++; 6273dbee59bSBruce M Simpson ia->ia_ifa.if_obytes += m->m_pkthdr.len; 6285da9f8faSJosef Karthauser } 62953dcc544SMike Silbersack #ifdef MBUF_STRESS_TEST 6303390d476SMike Silbersack if (mbuf_frag_size && m->m_pkthdr.len > mbuf_frag_size) 6313390d476SMike Silbersack m = m_fragment(m, M_DONTWAIT, mbuf_frag_size); 6329d9edc56SMike Silbersack #endif 633147f74d1SAndre Oppermann /* 634147f74d1SAndre Oppermann * Reset layer specific mbuf flags 635147f74d1SAndre Oppermann * to avoid confusing lower layers. 636147f74d1SAndre Oppermann */ 637147f74d1SAndre Oppermann m->m_flags &= ~(M_PROTOFLAGS); 638df8bae1dSRodney W. Grimes error = (*ifp->if_output)(ifp, m, 639279aa3d4SKip Macy (struct sockaddr *)dst, ro); 640df8bae1dSRodney W. Grimes goto done; 641df8bae1dSRodney W. Grimes } 6421e78ac21SJeffrey Hsu 643233dcce1SAndre Oppermann /* Balk when DF bit is set or the interface didn't support TSO. */ 64421d172a3SGleb Smirnoff if ((ip_off & IP_DF) || (m->m_pkthdr.csum_flags & CSUM_TSO)) { 645df8bae1dSRodney W. Grimes error = EMSGSIZE; 64686425c62SRobert Watson IPSTAT_INC(ips_cantfrag); 647df8bae1dSRodney W. Grimes goto bad; 648df8bae1dSRodney W. Grimes } 6491e78ac21SJeffrey Hsu 6501e78ac21SJeffrey Hsu /* 6511e78ac21SJeffrey Hsu * Too large for interface; fragment if possible. If successful, 6521e78ac21SJeffrey Hsu * on return, m will point to a list of packets to be sent. 6531e78ac21SJeffrey Hsu */ 6543ae2ad08SJohn-Mark Gurney error = ip_fragment(ip, &m, mtu, ifp->if_hwassist, sw_csum); 6551e78ac21SJeffrey Hsu if (error) 656df8bae1dSRodney W. Grimes goto bad; 6571e78ac21SJeffrey Hsu for (; m; m = m0) { 658df8bae1dSRodney W. Grimes m0 = m->m_nextpkt; 659b375c9ecSLuigi Rizzo m->m_nextpkt = 0; 66007203494SDaniel C. Sobral if (error == 0) { 661fe937674SJosef Karthauser /* Record statistics for this interface address. */ 66207203494SDaniel C. Sobral if (ia != NULL) { 6633dbee59bSBruce M Simpson ia->ia_ifa.if_opackets++; 6643dbee59bSBruce M Simpson ia->ia_ifa.if_obytes += m->m_pkthdr.len; 66507203494SDaniel C. Sobral } 666147f74d1SAndre Oppermann /* 667147f74d1SAndre Oppermann * Reset layer specific mbuf flags 668147f74d1SAndre Oppermann * to avoid confusing upper layers. 669147f74d1SAndre Oppermann */ 670147f74d1SAndre Oppermann m->m_flags &= ~(M_PROTOFLAGS); 671fe937674SJosef Karthauser 672df8bae1dSRodney W. Grimes error = (*ifp->if_output)(ifp, m, 673279aa3d4SKip Macy (struct sockaddr *)dst, ro); 674fe937674SJosef Karthauser } else 675df8bae1dSRodney W. Grimes m_freem(m); 676df8bae1dSRodney W. Grimes } 677df8bae1dSRodney W. Grimes 678df8bae1dSRodney W. Grimes if (error == 0) 67986425c62SRobert Watson IPSTAT_INC(ips_fragmented); 6801e78ac21SJeffrey Hsu 681df8bae1dSRodney W. Grimes done: 682bf984051SGleb Smirnoff if (ro == &iproute) 683bf984051SGleb Smirnoff RO_RTFREE(ro); 6848c0fec80SRobert Watson if (ia != NULL) 6858c0fec80SRobert Watson ifa_free(&ia->ia_ifa); 686df8bae1dSRodney W. Grimes return (error); 687df8bae1dSRodney W. Grimes bad: 6883528d68fSBill Paul m_freem(m); 689df8bae1dSRodney W. Grimes goto done; 690df8bae1dSRodney W. Grimes } 691df8bae1dSRodney W. Grimes 6921e78ac21SJeffrey Hsu /* 6931e78ac21SJeffrey Hsu * Create a chain of fragments which fit the given mtu. m_frag points to the 6941e78ac21SJeffrey Hsu * mbuf to be fragmented; on return it points to the chain with the fragments. 6951e78ac21SJeffrey Hsu * Return 0 if no error. If error, m_frag may contain a partially built 6961e78ac21SJeffrey Hsu * chain of fragments that should be freed by the caller. 6971e78ac21SJeffrey Hsu * 6981e78ac21SJeffrey Hsu * if_hwassist_flags is the hw offload capabilities (see if_data.ifi_hwassist) 6991e78ac21SJeffrey Hsu * sw_csum contains the delayed checksums flags (e.g., CSUM_DELAY_IP). 7001e78ac21SJeffrey Hsu */ 7011e78ac21SJeffrey Hsu int 7021e78ac21SJeffrey Hsu ip_fragment(struct ip *ip, struct mbuf **m_frag, int mtu, 7031e78ac21SJeffrey Hsu u_long if_hwassist_flags, int sw_csum) 7041e78ac21SJeffrey Hsu { 7051e78ac21SJeffrey Hsu int error = 0; 7061e78ac21SJeffrey Hsu int hlen = ip->ip_hl << 2; 7071e78ac21SJeffrey Hsu int len = (mtu - hlen) & ~7; /* size of payload in each fragment */ 7081e78ac21SJeffrey Hsu int off; 7091e78ac21SJeffrey Hsu struct mbuf *m0 = *m_frag; /* the original packet */ 7101e78ac21SJeffrey Hsu int firstlen; 7111e78ac21SJeffrey Hsu struct mbuf **mnext; 7121e78ac21SJeffrey Hsu int nfrags; 71321d172a3SGleb Smirnoff uint16_t ip_len, ip_off; 7141e78ac21SJeffrey Hsu 71521d172a3SGleb Smirnoff ip_len = ntohs(ip->ip_len); 71621d172a3SGleb Smirnoff ip_off = ntohs(ip->ip_off); 71721d172a3SGleb Smirnoff 71821d172a3SGleb Smirnoff if (ip_off & IP_DF) { /* Fragmentation not allowed */ 71986425c62SRobert Watson IPSTAT_INC(ips_cantfrag); 7201e78ac21SJeffrey Hsu return EMSGSIZE; 7211e78ac21SJeffrey Hsu } 7221e78ac21SJeffrey Hsu 7231e78ac21SJeffrey Hsu /* 7241e78ac21SJeffrey Hsu * Must be able to put at least 8 bytes per fragment. 7251e78ac21SJeffrey Hsu */ 7261e78ac21SJeffrey Hsu if (len < 8) 7271e78ac21SJeffrey Hsu return EMSGSIZE; 7281e78ac21SJeffrey Hsu 7291e78ac21SJeffrey Hsu /* 7301e78ac21SJeffrey Hsu * If the interface will not calculate checksums on 7311e78ac21SJeffrey Hsu * fragmented packets, then do it here. 7321e78ac21SJeffrey Hsu */ 7331e78ac21SJeffrey Hsu if (m0->m_pkthdr.csum_flags & CSUM_DELAY_DATA && 7341e78ac21SJeffrey Hsu (if_hwassist_flags & CSUM_IP_FRAGS) == 0) { 7351e78ac21SJeffrey Hsu in_delayed_cksum(m0); 7361e78ac21SJeffrey Hsu m0->m_pkthdr.csum_flags &= ~CSUM_DELAY_DATA; 7371e78ac21SJeffrey Hsu } 7382f4afd21SRandall Stewart #ifdef SCTP 7392f4afd21SRandall Stewart if (m0->m_pkthdr.csum_flags & CSUM_SCTP && 7402f4afd21SRandall Stewart (if_hwassist_flags & CSUM_IP_FRAGS) == 0) { 7411966e5b5SRandall Stewart sctp_delayed_cksum(m0, hlen); 7422f4afd21SRandall Stewart m0->m_pkthdr.csum_flags &= ~CSUM_SCTP; 7432f4afd21SRandall Stewart } 7442f4afd21SRandall Stewart #endif 7451e78ac21SJeffrey Hsu if (len > PAGE_SIZE) { 7461e78ac21SJeffrey Hsu /* 7471e78ac21SJeffrey Hsu * Fragment large datagrams such that each segment 7481e78ac21SJeffrey Hsu * contains a multiple of PAGE_SIZE amount of data, 7491e78ac21SJeffrey Hsu * plus headers. This enables a receiver to perform 7501e78ac21SJeffrey Hsu * page-flipping zero-copy optimizations. 7511e78ac21SJeffrey Hsu * 7521e78ac21SJeffrey Hsu * XXX When does this help given that sender and receiver 7531e78ac21SJeffrey Hsu * could have different page sizes, and also mtu could 7541e78ac21SJeffrey Hsu * be less than the receiver's page size ? 7551e78ac21SJeffrey Hsu */ 7561e78ac21SJeffrey Hsu int newlen; 7571e78ac21SJeffrey Hsu struct mbuf *m; 7581e78ac21SJeffrey Hsu 7591e78ac21SJeffrey Hsu for (m = m0, off = 0; m && (off+m->m_len) <= mtu; m = m->m_next) 7601e78ac21SJeffrey Hsu off += m->m_len; 7611e78ac21SJeffrey Hsu 7621e78ac21SJeffrey Hsu /* 7631e78ac21SJeffrey Hsu * firstlen (off - hlen) must be aligned on an 7641e78ac21SJeffrey Hsu * 8-byte boundary 7651e78ac21SJeffrey Hsu */ 7661e78ac21SJeffrey Hsu if (off < hlen) 7671e78ac21SJeffrey Hsu goto smart_frag_failure; 7681e78ac21SJeffrey Hsu off = ((off - hlen) & ~7) + hlen; 7691e78ac21SJeffrey Hsu newlen = (~PAGE_MASK) & mtu; 7701e78ac21SJeffrey Hsu if ((newlen + sizeof (struct ip)) > mtu) { 7711e78ac21SJeffrey Hsu /* we failed, go back the default */ 7721e78ac21SJeffrey Hsu smart_frag_failure: 7731e78ac21SJeffrey Hsu newlen = len; 7741e78ac21SJeffrey Hsu off = hlen + len; 7751e78ac21SJeffrey Hsu } 7761e78ac21SJeffrey Hsu len = newlen; 7771e78ac21SJeffrey Hsu 7781e78ac21SJeffrey Hsu } else { 7791e78ac21SJeffrey Hsu off = hlen + len; 7801e78ac21SJeffrey Hsu } 7811e78ac21SJeffrey Hsu 7821e78ac21SJeffrey Hsu firstlen = off - hlen; 7831e78ac21SJeffrey Hsu mnext = &m0->m_nextpkt; /* pointer to next packet */ 7841e78ac21SJeffrey Hsu 7851e78ac21SJeffrey Hsu /* 7861e78ac21SJeffrey Hsu * Loop through length of segment after first fragment, 7871e78ac21SJeffrey Hsu * make new header and copy data of each part and link onto chain. 7881e78ac21SJeffrey Hsu * Here, m0 is the original packet, m is the fragment being created. 7891e78ac21SJeffrey Hsu * The fragments are linked off the m_nextpkt of the original 7901e78ac21SJeffrey Hsu * packet, which after processing serves as the first fragment. 7911e78ac21SJeffrey Hsu */ 79221d172a3SGleb Smirnoff for (nfrags = 1; off < ip_len; off += len, nfrags++) { 7931e78ac21SJeffrey Hsu struct ip *mhip; /* ip header on the fragment */ 7941e78ac21SJeffrey Hsu struct mbuf *m; 7951e78ac21SJeffrey Hsu int mhlen = sizeof (struct ip); 7961e78ac21SJeffrey Hsu 79734333b16SAndre Oppermann MGETHDR(m, M_DONTWAIT, MT_DATA); 7980b17fba7SAndre Oppermann if (m == NULL) { 7991e78ac21SJeffrey Hsu error = ENOBUFS; 80086425c62SRobert Watson IPSTAT_INC(ips_odropped); 8011e78ac21SJeffrey Hsu goto done; 8021e78ac21SJeffrey Hsu } 8031e78ac21SJeffrey Hsu m->m_flags |= (m0->m_flags & M_MCAST) | M_FRAG; 8041e78ac21SJeffrey Hsu /* 8051e78ac21SJeffrey Hsu * In the first mbuf, leave room for the link header, then 8061e78ac21SJeffrey Hsu * copy the original IP header including options. The payload 8078b889dbbSBruce M Simpson * goes into an additional mbuf chain returned by m_copym(). 8081e78ac21SJeffrey Hsu */ 8091e78ac21SJeffrey Hsu m->m_data += max_linkhdr; 8101e78ac21SJeffrey Hsu mhip = mtod(m, struct ip *); 8111e78ac21SJeffrey Hsu *mhip = *ip; 8121e78ac21SJeffrey Hsu if (hlen > sizeof (struct ip)) { 8131e78ac21SJeffrey Hsu mhlen = ip_optcopy(ip, mhip) + sizeof (struct ip); 8141e78ac21SJeffrey Hsu mhip->ip_v = IPVERSION; 8151e78ac21SJeffrey Hsu mhip->ip_hl = mhlen >> 2; 8161e78ac21SJeffrey Hsu } 8171e78ac21SJeffrey Hsu m->m_len = mhlen; 81821d172a3SGleb Smirnoff /* XXX do we need to add ip_off below ? */ 81921d172a3SGleb Smirnoff mhip->ip_off = ((off - hlen) >> 3) + ip_off; 82021d172a3SGleb Smirnoff if (off + len >= ip_len) { /* last fragment */ 82121d172a3SGleb Smirnoff len = ip_len - off; 8221e78ac21SJeffrey Hsu m->m_flags |= M_LASTFRAG; 8231e78ac21SJeffrey Hsu } else 8241e78ac21SJeffrey Hsu mhip->ip_off |= IP_MF; 8251e78ac21SJeffrey Hsu mhip->ip_len = htons((u_short)(len + mhlen)); 8268b889dbbSBruce M Simpson m->m_next = m_copym(m0, off, len, M_DONTWAIT); 8270b17fba7SAndre Oppermann if (m->m_next == NULL) { /* copy failed */ 8281e78ac21SJeffrey Hsu m_free(m); 8291e78ac21SJeffrey Hsu error = ENOBUFS; /* ??? */ 83086425c62SRobert Watson IPSTAT_INC(ips_odropped); 8311e78ac21SJeffrey Hsu goto done; 8321e78ac21SJeffrey Hsu } 8331e78ac21SJeffrey Hsu m->m_pkthdr.len = mhlen + len; 834fc74a9f9SBrooks Davis m->m_pkthdr.rcvif = NULL; 8351e78ac21SJeffrey Hsu #ifdef MAC 83630d239bcSRobert Watson mac_netinet_fragment(m0, m); 8371e78ac21SJeffrey Hsu #endif 8381e78ac21SJeffrey Hsu m->m_pkthdr.csum_flags = m0->m_pkthdr.csum_flags; 8391e78ac21SJeffrey Hsu mhip->ip_off = htons(mhip->ip_off); 8401e78ac21SJeffrey Hsu mhip->ip_sum = 0; 8411e78ac21SJeffrey Hsu if (sw_csum & CSUM_DELAY_IP) 8421e78ac21SJeffrey Hsu mhip->ip_sum = in_cksum(m, mhlen); 8431e78ac21SJeffrey Hsu *mnext = m; 8441e78ac21SJeffrey Hsu mnext = &m->m_nextpkt; 8451e78ac21SJeffrey Hsu } 84686425c62SRobert Watson IPSTAT_ADD(ips_ofragments, nfrags); 8471e78ac21SJeffrey Hsu 8481e78ac21SJeffrey Hsu /* set first marker for fragment chain */ 8491e78ac21SJeffrey Hsu m0->m_flags |= M_FIRSTFRAG | M_FRAG; 8501e78ac21SJeffrey Hsu m0->m_pkthdr.csum_data = nfrags; 8511e78ac21SJeffrey Hsu 8521e78ac21SJeffrey Hsu /* 8531e78ac21SJeffrey Hsu * Update first fragment by trimming what's been copied out 8541e78ac21SJeffrey Hsu * and updating header. 8551e78ac21SJeffrey Hsu */ 85621d172a3SGleb Smirnoff m_adj(m0, hlen + firstlen - ip_len); 8571e78ac21SJeffrey Hsu m0->m_pkthdr.len = hlen + firstlen; 8581e78ac21SJeffrey Hsu ip->ip_len = htons((u_short)m0->m_pkthdr.len); 85921d172a3SGleb Smirnoff ip->ip_off = htons(ip_off | IP_MF); 8601e78ac21SJeffrey Hsu ip->ip_sum = 0; 8611e78ac21SJeffrey Hsu if (sw_csum & CSUM_DELAY_IP) 8621e78ac21SJeffrey Hsu ip->ip_sum = in_cksum(m0, hlen); 8631e78ac21SJeffrey Hsu 8641e78ac21SJeffrey Hsu done: 8651e78ac21SJeffrey Hsu *m_frag = m0; 8661e78ac21SJeffrey Hsu return error; 8671e78ac21SJeffrey Hsu } 8681e78ac21SJeffrey Hsu 8691c238475SJonathan Lemon void 870db4f9cc7SJonathan Lemon in_delayed_cksum(struct mbuf *m) 871db4f9cc7SJonathan Lemon { 872db4f9cc7SJonathan Lemon struct ip *ip; 87323e9c6dcSGleb Smirnoff uint16_t csum, offset, ip_len; 874db4f9cc7SJonathan Lemon 875db4f9cc7SJonathan Lemon ip = mtod(m, struct ip *); 87653be11f6SPoul-Henning Kamp offset = ip->ip_hl << 2 ; 87723e9c6dcSGleb Smirnoff ip_len = ntohs(ip->ip_len); 87823e9c6dcSGleb Smirnoff csum = in_cksum_skip(m, ip_len, offset); 879206a3274SRuslan Ermilov if (m->m_pkthdr.csum_flags & CSUM_UDP && csum == 0) 880206a3274SRuslan Ermilov csum = 0xffff; 881db4f9cc7SJonathan Lemon offset += m->m_pkthdr.csum_data; /* checksum offset */ 882db4f9cc7SJonathan Lemon 883db4f9cc7SJonathan Lemon if (offset + sizeof(u_short) > m->m_len) { 884db4f9cc7SJonathan Lemon printf("delayed m_pullup, m->len: %d off: %d p: %d\n", 885db4f9cc7SJonathan Lemon m->m_len, offset, ip->ip_p); 886db4f9cc7SJonathan Lemon /* 887db4f9cc7SJonathan Lemon * XXX 888db4f9cc7SJonathan Lemon * this shouldn't happen, but if it does, the 889db4f9cc7SJonathan Lemon * correct behavior may be to insert the checksum 8908f8d29f6SAndre Oppermann * in the appropriate next mbuf in the chain. 891db4f9cc7SJonathan Lemon */ 8928f8d29f6SAndre Oppermann return; 893db4f9cc7SJonathan Lemon } 894db4f9cc7SJonathan Lemon *(u_short *)(m->m_data + offset) = csum; 895db4f9cc7SJonathan Lemon } 896db4f9cc7SJonathan Lemon 897df8bae1dSRodney W. Grimes /* 898df8bae1dSRodney W. Grimes * IP socket option processing. 899df8bae1dSRodney W. Grimes */ 900df8bae1dSRodney W. Grimes int 901f2565d68SRobert Watson ip_ctloutput(struct socket *so, struct sockopt *sopt) 902df8bae1dSRodney W. Grimes { 903cfe8b629SGarrett Wollman struct inpcb *inp = sotoinpcb(so); 904cfe8b629SGarrett Wollman int error, optval; 905df8bae1dSRodney W. Grimes 906cfe8b629SGarrett Wollman error = optval = 0; 907cfe8b629SGarrett Wollman if (sopt->sopt_level != IPPROTO_IP) { 908fc06cd42SMikolaj Golub error = EINVAL; 909fc06cd42SMikolaj Golub 910fc06cd42SMikolaj Golub if (sopt->sopt_level == SOL_SOCKET && 911fc06cd42SMikolaj Golub sopt->sopt_dir == SOPT_SET) { 912fc06cd42SMikolaj Golub switch (sopt->sopt_name) { 913fc06cd42SMikolaj Golub case SO_REUSEADDR: 914fc06cd42SMikolaj Golub INP_WLOCK(inp); 915fc06cd42SMikolaj Golub if (IN_MULTICAST(ntohl(inp->inp_laddr.s_addr))) { 916fc06cd42SMikolaj Golub if ((so->so_options & 917fc06cd42SMikolaj Golub (SO_REUSEADDR | SO_REUSEPORT)) != 0) 918fc06cd42SMikolaj Golub inp->inp_flags2 |= INP_REUSEPORT; 919fc06cd42SMikolaj Golub else 920fc06cd42SMikolaj Golub inp->inp_flags2 &= ~INP_REUSEPORT; 921bc97ba51SJulian Elischer } 922fc06cd42SMikolaj Golub INP_WUNLOCK(inp); 923fc06cd42SMikolaj Golub error = 0; 924fc06cd42SMikolaj Golub break; 925fc06cd42SMikolaj Golub case SO_REUSEPORT: 926fc06cd42SMikolaj Golub INP_WLOCK(inp); 927fc06cd42SMikolaj Golub if ((so->so_options & SO_REUSEPORT) != 0) 928fc06cd42SMikolaj Golub inp->inp_flags2 |= INP_REUSEPORT; 929fc06cd42SMikolaj Golub else 930fc06cd42SMikolaj Golub inp->inp_flags2 &= ~INP_REUSEPORT; 931fc06cd42SMikolaj Golub INP_WUNLOCK(inp); 932fc06cd42SMikolaj Golub error = 0; 933fc06cd42SMikolaj Golub break; 934fc06cd42SMikolaj Golub case SO_SETFIB: 935fc06cd42SMikolaj Golub INP_WLOCK(inp); 936fc06cd42SMikolaj Golub inp->inp_inc.inc_fibnum = so->so_fibnum; 937fc06cd42SMikolaj Golub INP_WUNLOCK(inp); 938fc06cd42SMikolaj Golub error = 0; 939fc06cd42SMikolaj Golub break; 940fc06cd42SMikolaj Golub default: 941fc06cd42SMikolaj Golub break; 942fc06cd42SMikolaj Golub } 943fc06cd42SMikolaj Golub } 944fc06cd42SMikolaj Golub return (error); 945cfe8b629SGarrett Wollman } 946df8bae1dSRodney W. Grimes 947cfe8b629SGarrett Wollman switch (sopt->sopt_dir) { 948cfe8b629SGarrett Wollman case SOPT_SET: 949cfe8b629SGarrett Wollman switch (sopt->sopt_name) { 950df8bae1dSRodney W. Grimes case IP_OPTIONS: 951df8bae1dSRodney W. Grimes #ifdef notyet 952df8bae1dSRodney W. Grimes case IP_RETOPTS: 953df8bae1dSRodney W. Grimes #endif 954cfe8b629SGarrett Wollman { 955cfe8b629SGarrett Wollman struct mbuf *m; 956cfe8b629SGarrett Wollman if (sopt->sopt_valsize > MLEN) { 957cfe8b629SGarrett Wollman error = EMSGSIZE; 958cfe8b629SGarrett Wollman break; 959cfe8b629SGarrett Wollman } 960ea26d587SRuslan Ermilov MGET(m, sopt->sopt_td ? M_WAIT : M_DONTWAIT, MT_DATA); 9610b17fba7SAndre Oppermann if (m == NULL) { 962cfe8b629SGarrett Wollman error = ENOBUFS; 963cfe8b629SGarrett Wollman break; 964cfe8b629SGarrett Wollman } 965cfe8b629SGarrett Wollman m->m_len = sopt->sopt_valsize; 966cfe8b629SGarrett Wollman error = sooptcopyin(sopt, mtod(m, char *), m->m_len, 967cfe8b629SGarrett Wollman m->m_len); 968635354c4SMaxim Konovalov if (error) { 969635354c4SMaxim Konovalov m_free(m); 970635354c4SMaxim Konovalov break; 971635354c4SMaxim Konovalov } 9728501a69cSRobert Watson INP_WLOCK(inp); 973993d9505SRobert Watson error = ip_pcbopts(inp, sopt->sopt_name, m); 9748501a69cSRobert Watson INP_WUNLOCK(inp); 975993d9505SRobert Watson return (error); 976cfe8b629SGarrett Wollman } 977df8bae1dSRodney W. Grimes 978f44270e7SPawel Jakub Dawidek case IP_BINDANY: 979f44270e7SPawel Jakub Dawidek if (sopt->sopt_td != NULL) { 980f44270e7SPawel Jakub Dawidek error = priv_check(sopt->sopt_td, 981f44270e7SPawel Jakub Dawidek PRIV_NETINET_BINDANY); 982f44270e7SPawel Jakub Dawidek if (error) 983be9347e3SAdrian Chadd break; 984be9347e3SAdrian Chadd } 985cef27294SAdrian Chadd /* FALLTHROUGH */ 986df8bae1dSRodney W. Grimes case IP_TOS: 987df8bae1dSRodney W. Grimes case IP_TTL: 988936cd18dSAndre Oppermann case IP_MINTTL: 989df8bae1dSRodney W. Grimes case IP_RECVOPTS: 990df8bae1dSRodney W. Grimes case IP_RECVRETOPTS: 991df8bae1dSRodney W. Grimes case IP_RECVDSTADDR: 9924957466bSMatthew N. Dodd case IP_RECVTTL: 99382c23ebaSBill Fenner case IP_RECVIF: 9946a800098SYoshinobu Inoue case IP_FAITH: 9958afa2304SBruce M Simpson case IP_ONESBCAST: 996b2828ad2SAndre Oppermann case IP_DONTFRAG: 9973cca425bSMichael Tuexen case IP_RECVTOS: 998cfe8b629SGarrett Wollman error = sooptcopyin(sopt, &optval, sizeof optval, 999cfe8b629SGarrett Wollman sizeof optval); 1000cfe8b629SGarrett Wollman if (error) 1001cfe8b629SGarrett Wollman break; 1002df8bae1dSRodney W. Grimes 1003cfe8b629SGarrett Wollman switch (sopt->sopt_name) { 1004df8bae1dSRodney W. Grimes case IP_TOS: 1005ca98b82cSDavid Greenman inp->inp_ip_tos = optval; 1006df8bae1dSRodney W. Grimes break; 1007df8bae1dSRodney W. Grimes 1008df8bae1dSRodney W. Grimes case IP_TTL: 1009ca98b82cSDavid Greenman inp->inp_ip_ttl = optval; 1010df8bae1dSRodney W. Grimes break; 1011936cd18dSAndre Oppermann 1012936cd18dSAndre Oppermann case IP_MINTTL: 1013a603c811SRobert Watson if (optval >= 0 && optval <= MAXTTL) 1014936cd18dSAndre Oppermann inp->inp_ip_minttl = optval; 1015936cd18dSAndre Oppermann else 1016936cd18dSAndre Oppermann error = EINVAL; 1017936cd18dSAndre Oppermann break; 1018936cd18dSAndre Oppermann 1019a138d217SRobert Watson #define OPTSET(bit) do { \ 10208501a69cSRobert Watson INP_WLOCK(inp); \ 1021df8bae1dSRodney W. Grimes if (optval) \ 1022df8bae1dSRodney W. Grimes inp->inp_flags |= bit; \ 1023df8bae1dSRodney W. Grimes else \ 1024a138d217SRobert Watson inp->inp_flags &= ~bit; \ 10258501a69cSRobert Watson INP_WUNLOCK(inp); \ 1026a138d217SRobert Watson } while (0) 1027df8bae1dSRodney W. Grimes 1028df8bae1dSRodney W. Grimes case IP_RECVOPTS: 1029df8bae1dSRodney W. Grimes OPTSET(INP_RECVOPTS); 1030df8bae1dSRodney W. Grimes break; 1031df8bae1dSRodney W. Grimes 1032df8bae1dSRodney W. Grimes case IP_RECVRETOPTS: 1033df8bae1dSRodney W. Grimes OPTSET(INP_RECVRETOPTS); 1034df8bae1dSRodney W. Grimes break; 1035df8bae1dSRodney W. Grimes 1036df8bae1dSRodney W. Grimes case IP_RECVDSTADDR: 1037df8bae1dSRodney W. Grimes OPTSET(INP_RECVDSTADDR); 1038df8bae1dSRodney W. Grimes break; 103982c23ebaSBill Fenner 10404957466bSMatthew N. Dodd case IP_RECVTTL: 10414957466bSMatthew N. Dodd OPTSET(INP_RECVTTL); 10424957466bSMatthew N. Dodd break; 10434957466bSMatthew N. Dodd 104482c23ebaSBill Fenner case IP_RECVIF: 104582c23ebaSBill Fenner OPTSET(INP_RECVIF); 104682c23ebaSBill Fenner break; 10476a800098SYoshinobu Inoue 10486a800098SYoshinobu Inoue case IP_FAITH: 10496a800098SYoshinobu Inoue OPTSET(INP_FAITH); 10506a800098SYoshinobu Inoue break; 10518afa2304SBruce M Simpson 10528afa2304SBruce M Simpson case IP_ONESBCAST: 10538afa2304SBruce M Simpson OPTSET(INP_ONESBCAST); 10548afa2304SBruce M Simpson break; 1055b2828ad2SAndre Oppermann case IP_DONTFRAG: 1056b2828ad2SAndre Oppermann OPTSET(INP_DONTFRAG); 1057b2828ad2SAndre Oppermann break; 1058f44270e7SPawel Jakub Dawidek case IP_BINDANY: 1059f44270e7SPawel Jakub Dawidek OPTSET(INP_BINDANY); 1060be9347e3SAdrian Chadd break; 10613cca425bSMichael Tuexen case IP_RECVTOS: 10623cca425bSMichael Tuexen OPTSET(INP_RECVTOS); 10633cca425bSMichael Tuexen break; 1064df8bae1dSRodney W. Grimes } 1065df8bae1dSRodney W. Grimes break; 1066df8bae1dSRodney W. Grimes #undef OPTSET 1067df8bae1dSRodney W. Grimes 106871498f30SBruce M Simpson /* 106971498f30SBruce M Simpson * Multicast socket options are processed by the in_mcast 107071498f30SBruce M Simpson * module. 107171498f30SBruce M Simpson */ 1072df8bae1dSRodney W. Grimes case IP_MULTICAST_IF: 1073f0068c4aSGarrett Wollman case IP_MULTICAST_VIF: 1074df8bae1dSRodney W. Grimes case IP_MULTICAST_TTL: 1075df8bae1dSRodney W. Grimes case IP_MULTICAST_LOOP: 1076df8bae1dSRodney W. Grimes case IP_ADD_MEMBERSHIP: 1077df8bae1dSRodney W. Grimes case IP_DROP_MEMBERSHIP: 107871498f30SBruce M Simpson case IP_ADD_SOURCE_MEMBERSHIP: 107971498f30SBruce M Simpson case IP_DROP_SOURCE_MEMBERSHIP: 108071498f30SBruce M Simpson case IP_BLOCK_SOURCE: 108171498f30SBruce M Simpson case IP_UNBLOCK_SOURCE: 108271498f30SBruce M Simpson case IP_MSFILTER: 108371498f30SBruce M Simpson case MCAST_JOIN_GROUP: 108471498f30SBruce M Simpson case MCAST_LEAVE_GROUP: 108571498f30SBruce M Simpson case MCAST_JOIN_SOURCE_GROUP: 108671498f30SBruce M Simpson case MCAST_LEAVE_SOURCE_GROUP: 108771498f30SBruce M Simpson case MCAST_BLOCK_SOURCE: 108871498f30SBruce M Simpson case MCAST_UNBLOCK_SOURCE: 108971498f30SBruce M Simpson error = inp_setmoptions(inp, sopt); 1090df8bae1dSRodney W. Grimes break; 1091df8bae1dSRodney W. Grimes 109233b3ac06SPeter Wemm case IP_PORTRANGE: 1093cfe8b629SGarrett Wollman error = sooptcopyin(sopt, &optval, sizeof optval, 1094cfe8b629SGarrett Wollman sizeof optval); 1095cfe8b629SGarrett Wollman if (error) 1096cfe8b629SGarrett Wollman break; 109733b3ac06SPeter Wemm 10988501a69cSRobert Watson INP_WLOCK(inp); 109933b3ac06SPeter Wemm switch (optval) { 110033b3ac06SPeter Wemm case IP_PORTRANGE_DEFAULT: 110133b3ac06SPeter Wemm inp->inp_flags &= ~(INP_LOWPORT); 110233b3ac06SPeter Wemm inp->inp_flags &= ~(INP_HIGHPORT); 110333b3ac06SPeter Wemm break; 110433b3ac06SPeter Wemm 110533b3ac06SPeter Wemm case IP_PORTRANGE_HIGH: 110633b3ac06SPeter Wemm inp->inp_flags &= ~(INP_LOWPORT); 110733b3ac06SPeter Wemm inp->inp_flags |= INP_HIGHPORT; 110833b3ac06SPeter Wemm break; 110933b3ac06SPeter Wemm 111033b3ac06SPeter Wemm case IP_PORTRANGE_LOW: 111133b3ac06SPeter Wemm inp->inp_flags &= ~(INP_HIGHPORT); 111233b3ac06SPeter Wemm inp->inp_flags |= INP_LOWPORT; 111333b3ac06SPeter Wemm break; 111433b3ac06SPeter Wemm 111533b3ac06SPeter Wemm default: 111633b3ac06SPeter Wemm error = EINVAL; 111733b3ac06SPeter Wemm break; 111833b3ac06SPeter Wemm } 11198501a69cSRobert Watson INP_WUNLOCK(inp); 1120ce8c72b1SPeter Wemm break; 112133b3ac06SPeter Wemm 1122b2630c29SGeorge V. Neville-Neil #ifdef IPSEC 11236a800098SYoshinobu Inoue case IP_IPSEC_POLICY: 11246a800098SYoshinobu Inoue { 11256a800098SYoshinobu Inoue caddr_t req; 11266a800098SYoshinobu Inoue struct mbuf *m; 11276a800098SYoshinobu Inoue 11286a800098SYoshinobu Inoue if ((error = soopt_getm(sopt, &m)) != 0) /* XXX */ 11296a800098SYoshinobu Inoue break; 11306a800098SYoshinobu Inoue if ((error = soopt_mcopyin(sopt, m)) != 0) /* XXX */ 11316a800098SYoshinobu Inoue break; 11326a800098SYoshinobu Inoue req = mtod(m, caddr_t); 113397aa4a51SBjoern A. Zeeb error = ipsec_set_policy(inp, sopt->sopt_name, req, 1134c26fe973SBjoern A. Zeeb m->m_len, (sopt->sopt_td != NULL) ? 1135c26fe973SBjoern A. Zeeb sopt->sopt_td->td_ucred : NULL); 11366a800098SYoshinobu Inoue m_freem(m); 11376a800098SYoshinobu Inoue break; 11386a800098SYoshinobu Inoue } 1139b2630c29SGeorge V. Neville-Neil #endif /* IPSEC */ 11406a800098SYoshinobu Inoue 1141df8bae1dSRodney W. Grimes default: 1142df8bae1dSRodney W. Grimes error = ENOPROTOOPT; 1143df8bae1dSRodney W. Grimes break; 1144df8bae1dSRodney W. Grimes } 1145df8bae1dSRodney W. Grimes break; 1146df8bae1dSRodney W. Grimes 1147cfe8b629SGarrett Wollman case SOPT_GET: 1148cfe8b629SGarrett Wollman switch (sopt->sopt_name) { 1149df8bae1dSRodney W. Grimes case IP_OPTIONS: 1150df8bae1dSRodney W. Grimes case IP_RETOPTS: 1151cfe8b629SGarrett Wollman if (inp->inp_options) 1152cfe8b629SGarrett Wollman error = sooptcopyout(sopt, 1153cfe8b629SGarrett Wollman mtod(inp->inp_options, 1154cfe8b629SGarrett Wollman char *), 1155cfe8b629SGarrett Wollman inp->inp_options->m_len); 1156cfe8b629SGarrett Wollman else 1157cfe8b629SGarrett Wollman sopt->sopt_valsize = 0; 1158df8bae1dSRodney W. Grimes break; 1159df8bae1dSRodney W. Grimes 1160df8bae1dSRodney W. Grimes case IP_TOS: 1161df8bae1dSRodney W. Grimes case IP_TTL: 1162936cd18dSAndre Oppermann case IP_MINTTL: 1163df8bae1dSRodney W. Grimes case IP_RECVOPTS: 1164df8bae1dSRodney W. Grimes case IP_RECVRETOPTS: 1165df8bae1dSRodney W. Grimes case IP_RECVDSTADDR: 11664957466bSMatthew N. Dodd case IP_RECVTTL: 116782c23ebaSBill Fenner case IP_RECVIF: 1168cfe8b629SGarrett Wollman case IP_PORTRANGE: 11696a800098SYoshinobu Inoue case IP_FAITH: 11708afa2304SBruce M Simpson case IP_ONESBCAST: 1171b2828ad2SAndre Oppermann case IP_DONTFRAG: 11725f6bf451SAttilio Rao case IP_BINDANY: 11733cca425bSMichael Tuexen case IP_RECVTOS: 1174cfe8b629SGarrett Wollman switch (sopt->sopt_name) { 1175df8bae1dSRodney W. Grimes 1176df8bae1dSRodney W. Grimes case IP_TOS: 1177ca98b82cSDavid Greenman optval = inp->inp_ip_tos; 1178df8bae1dSRodney W. Grimes break; 1179df8bae1dSRodney W. Grimes 1180df8bae1dSRodney W. Grimes case IP_TTL: 1181ca98b82cSDavid Greenman optval = inp->inp_ip_ttl; 1182df8bae1dSRodney W. Grimes break; 1183df8bae1dSRodney W. Grimes 1184936cd18dSAndre Oppermann case IP_MINTTL: 1185936cd18dSAndre Oppermann optval = inp->inp_ip_minttl; 1186936cd18dSAndre Oppermann break; 1187936cd18dSAndre Oppermann 1188df8bae1dSRodney W. Grimes #define OPTBIT(bit) (inp->inp_flags & bit ? 1 : 0) 1189df8bae1dSRodney W. Grimes 1190df8bae1dSRodney W. Grimes case IP_RECVOPTS: 1191df8bae1dSRodney W. Grimes optval = OPTBIT(INP_RECVOPTS); 1192df8bae1dSRodney W. Grimes break; 1193df8bae1dSRodney W. Grimes 1194df8bae1dSRodney W. Grimes case IP_RECVRETOPTS: 1195df8bae1dSRodney W. Grimes optval = OPTBIT(INP_RECVRETOPTS); 1196df8bae1dSRodney W. Grimes break; 1197df8bae1dSRodney W. Grimes 1198df8bae1dSRodney W. Grimes case IP_RECVDSTADDR: 1199df8bae1dSRodney W. Grimes optval = OPTBIT(INP_RECVDSTADDR); 1200df8bae1dSRodney W. Grimes break; 120182c23ebaSBill Fenner 12024957466bSMatthew N. Dodd case IP_RECVTTL: 12034957466bSMatthew N. Dodd optval = OPTBIT(INP_RECVTTL); 12044957466bSMatthew N. Dodd break; 12054957466bSMatthew N. Dodd 120682c23ebaSBill Fenner case IP_RECVIF: 120782c23ebaSBill Fenner optval = OPTBIT(INP_RECVIF); 120882c23ebaSBill Fenner break; 1209cfe8b629SGarrett Wollman 1210cfe8b629SGarrett Wollman case IP_PORTRANGE: 1211cfe8b629SGarrett Wollman if (inp->inp_flags & INP_HIGHPORT) 1212cfe8b629SGarrett Wollman optval = IP_PORTRANGE_HIGH; 1213cfe8b629SGarrett Wollman else if (inp->inp_flags & INP_LOWPORT) 1214cfe8b629SGarrett Wollman optval = IP_PORTRANGE_LOW; 1215cfe8b629SGarrett Wollman else 1216cfe8b629SGarrett Wollman optval = 0; 1217cfe8b629SGarrett Wollman break; 12186a800098SYoshinobu Inoue 12196a800098SYoshinobu Inoue case IP_FAITH: 12206a800098SYoshinobu Inoue optval = OPTBIT(INP_FAITH); 12216a800098SYoshinobu Inoue break; 12228afa2304SBruce M Simpson 12238afa2304SBruce M Simpson case IP_ONESBCAST: 12248afa2304SBruce M Simpson optval = OPTBIT(INP_ONESBCAST); 12258afa2304SBruce M Simpson break; 1226b2828ad2SAndre Oppermann case IP_DONTFRAG: 1227b2828ad2SAndre Oppermann optval = OPTBIT(INP_DONTFRAG); 1228b2828ad2SAndre Oppermann break; 12295f6bf451SAttilio Rao case IP_BINDANY: 12305f6bf451SAttilio Rao optval = OPTBIT(INP_BINDANY); 12315f6bf451SAttilio Rao break; 12323cca425bSMichael Tuexen case IP_RECVTOS: 12333cca425bSMichael Tuexen optval = OPTBIT(INP_RECVTOS); 12343cca425bSMichael Tuexen break; 1235df8bae1dSRodney W. Grimes } 1236cfe8b629SGarrett Wollman error = sooptcopyout(sopt, &optval, sizeof optval); 1237df8bae1dSRodney W. Grimes break; 1238df8bae1dSRodney W. Grimes 123971498f30SBruce M Simpson /* 124071498f30SBruce M Simpson * Multicast socket options are processed by the in_mcast 124171498f30SBruce M Simpson * module. 124271498f30SBruce M Simpson */ 1243df8bae1dSRodney W. Grimes case IP_MULTICAST_IF: 1244f0068c4aSGarrett Wollman case IP_MULTICAST_VIF: 1245df8bae1dSRodney W. Grimes case IP_MULTICAST_TTL: 1246df8bae1dSRodney W. Grimes case IP_MULTICAST_LOOP: 124771498f30SBruce M Simpson case IP_MSFILTER: 124871498f30SBruce M Simpson error = inp_getmoptions(inp, sopt); 124933b3ac06SPeter Wemm break; 125033b3ac06SPeter Wemm 1251b2630c29SGeorge V. Neville-Neil #ifdef IPSEC 12526a800098SYoshinobu Inoue case IP_IPSEC_POLICY: 12536a800098SYoshinobu Inoue { 1254f63e7634SYoshinobu Inoue struct mbuf *m = NULL; 12556a800098SYoshinobu Inoue caddr_t req = NULL; 1256686cdd19SJun-ichiro itojun Hagino size_t len = 0; 12576a800098SYoshinobu Inoue 1258686cdd19SJun-ichiro itojun Hagino if (m != 0) { 12596a800098SYoshinobu Inoue req = mtod(m, caddr_t); 1260686cdd19SJun-ichiro itojun Hagino len = m->m_len; 1261686cdd19SJun-ichiro itojun Hagino } 126297aa4a51SBjoern A. Zeeb error = ipsec_get_policy(sotoinpcb(so), req, len, &m); 12636a800098SYoshinobu Inoue if (error == 0) 12646a800098SYoshinobu Inoue error = soopt_mcopyout(sopt, m); /* XXX */ 1265f63e7634SYoshinobu Inoue if (error == 0) 12666a800098SYoshinobu Inoue m_freem(m); 12676a800098SYoshinobu Inoue break; 12686a800098SYoshinobu Inoue } 1269b2630c29SGeorge V. Neville-Neil #endif /* IPSEC */ 12706a800098SYoshinobu Inoue 1271df8bae1dSRodney W. Grimes default: 1272df8bae1dSRodney W. Grimes error = ENOPROTOOPT; 1273df8bae1dSRodney W. Grimes break; 1274df8bae1dSRodney W. Grimes } 1275df8bae1dSRodney W. Grimes break; 1276df8bae1dSRodney W. Grimes } 1277df8bae1dSRodney W. Grimes return (error); 1278df8bae1dSRodney W. Grimes } 1279df8bae1dSRodney W. Grimes 1280df8bae1dSRodney W. Grimes /* 1281df8bae1dSRodney W. Grimes * Routine called from ip_output() to loop back a copy of an IP multicast 1282df8bae1dSRodney W. Grimes * packet to the input queue of a specified interface. Note that this 1283df8bae1dSRodney W. Grimes * calls the output routine of the loopback "driver", but with an interface 1284f5fea3ddSPaul Traina * pointer that might NOT be a loopback interface -- evil, but easier than 1285f5fea3ddSPaul Traina * replicating that code here. 1286df8bae1dSRodney W. Grimes */ 1287df8bae1dSRodney W. Grimes static void 1288f2565d68SRobert Watson ip_mloopback(struct ifnet *ifp, struct mbuf *m, struct sockaddr_in *dst, 1289f2565d68SRobert Watson int hlen) 1290df8bae1dSRodney W. Grimes { 1291b375c9ecSLuigi Rizzo register struct ip *ip; 1292df8bae1dSRodney W. Grimes struct mbuf *copym; 1293df8bae1dSRodney W. Grimes 1294e4762f75SGeorge V. Neville-Neil /* 1295e4762f75SGeorge V. Neville-Neil * Make a deep copy of the packet because we're going to 1296e4762f75SGeorge V. Neville-Neil * modify the pack in order to generate checksums. 1297e4762f75SGeorge V. Neville-Neil */ 1298e4762f75SGeorge V. Neville-Neil copym = m_dup(m, M_DONTWAIT); 129986b1d6d2SBill Fenner if (copym != NULL && (copym->m_flags & M_EXT || copym->m_len < hlen)) 130086b1d6d2SBill Fenner copym = m_pullup(copym, hlen); 1301df8bae1dSRodney W. Grimes if (copym != NULL) { 1302390cdc6aSRuslan Ermilov /* If needed, compute the checksum and mark it as valid. */ 1303390cdc6aSRuslan Ermilov if (copym->m_pkthdr.csum_flags & CSUM_DELAY_DATA) { 1304390cdc6aSRuslan Ermilov in_delayed_cksum(copym); 1305390cdc6aSRuslan Ermilov copym->m_pkthdr.csum_flags &= ~CSUM_DELAY_DATA; 1306390cdc6aSRuslan Ermilov copym->m_pkthdr.csum_flags |= 1307390cdc6aSRuslan Ermilov CSUM_DATA_VALID | CSUM_PSEUDO_HDR; 1308390cdc6aSRuslan Ermilov copym->m_pkthdr.csum_data = 0xffff; 1309390cdc6aSRuslan Ermilov } 1310df8bae1dSRodney W. Grimes /* 1311df8bae1dSRodney W. Grimes * We don't bother to fragment if the IP length is greater 1312df8bae1dSRodney W. Grimes * than the interface's MTU. Can this possibly matter? 1313df8bae1dSRodney W. Grimes */ 1314*8f134647SGleb Smirnoff ip = mtod(copym, struct ip *); 1315df8bae1dSRodney W. Grimes ip->ip_sum = 0; 131686b1d6d2SBill Fenner ip->ip_sum = in_cksum(copym, hlen); 1317201c2527SJulian Elischer #if 1 /* XXX */ 1318201c2527SJulian Elischer if (dst->sin_family != AF_INET) { 13192b8a366cSJulian Elischer printf("ip_mloopback: bad address family %d\n", 1320201c2527SJulian Elischer dst->sin_family); 1321201c2527SJulian Elischer dst->sin_family = AF_INET; 1322201c2527SJulian Elischer } 1323201c2527SJulian Elischer #endif 132406a429a3SArchie Cobbs if_simloop(ifp, copym, dst->sin_family, 0); 1325df8bae1dSRodney W. Grimes } 1326df8bae1dSRodney W. Grimes } 1327