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 355d6d7e75SGleb Smirnoff #include "opt_inet.h" 361f7e052cSJulian Elischer #include "opt_ipfw.h" 376a800098SYoshinobu Inoue #include "opt_ipsec.h" 3853dcc544SMike Silbersack #include "opt_mbuf_stress_test.h" 39e440aed9SQing Li #include "opt_mpath.h" 4057f60867SMark Johnston #include "opt_route.h" 412f4afd21SRandall Stewart #include "opt_sctp.h" 429c423972SAdrian Chadd #include "opt_rss.h" 43fbd1372aSJoerg Wunsch 44df8bae1dSRodney W. Grimes #include <sys/param.h> 4526f9a767SRodney W. Grimes #include <sys/systm.h> 46f0f6d643SLuigi Rizzo #include <sys/kernel.h> 47df8bae1dSRodney W. Grimes #include <sys/malloc.h> 48df8bae1dSRodney W. Grimes #include <sys/mbuf.h> 49acd3428bSRobert Watson #include <sys/priv.h> 50c26fe973SBjoern A. Zeeb #include <sys/proc.h> 51df8bae1dSRodney W. Grimes #include <sys/protosw.h> 5257f60867SMark Johnston #include <sys/sdt.h> 53df8bae1dSRodney W. Grimes #include <sys/socket.h> 54df8bae1dSRodney W. Grimes #include <sys/socketvar.h> 559d9edc56SMike Silbersack #include <sys/sysctl.h> 56c26fe973SBjoern A. Zeeb #include <sys/ucred.h> 57df8bae1dSRodney W. Grimes 58df8bae1dSRodney W. Grimes #include <net/if.h> 5976039bc8SGleb Smirnoff #include <net/if_var.h> 603ef5e21dSQing Li #include <net/if_llatbl.h> 619b932e9eSAndre Oppermann #include <net/netisr.h> 62c21fd232SAndre Oppermann #include <net/pfil.h> 63df8bae1dSRodney W. Grimes #include <net/route.h> 6465111ec7SKip Macy #include <net/flowtable.h> 65e440aed9SQing Li #ifdef RADIX_MPATH 66e440aed9SQing Li #include <net/radix_mpath.h> 67e440aed9SQing Li #endif 68b2bdc62aSAdrian Chadd #include <net/rss_config.h> 694b79449eSBjoern A. Zeeb #include <net/vnet.h> 70df8bae1dSRodney W. Grimes 71df8bae1dSRodney W. Grimes #include <netinet/in.h> 7257f60867SMark Johnston #include <netinet/in_kdtrace.h> 73df8bae1dSRodney W. Grimes #include <netinet/in_systm.h> 74df8bae1dSRodney W. Grimes #include <netinet/ip.h> 75df8bae1dSRodney W. Grimes #include <netinet/in_pcb.h> 769c423972SAdrian Chadd #include <netinet/in_rss.h> 77df8bae1dSRodney W. Grimes #include <netinet/in_var.h> 78df8bae1dSRodney W. Grimes #include <netinet/ip_var.h> 79ef39adf0SAndre Oppermann #include <netinet/ip_options.h> 802f4afd21SRandall Stewart #ifdef SCTP 812f4afd21SRandall Stewart #include <netinet/sctp.h> 822f4afd21SRandall Stewart #include <netinet/sctp_crc32.h> 832f4afd21SRandall Stewart #endif 84df8bae1dSRodney W. Grimes 85b2630c29SGeorge V. Neville-Neil #ifdef IPSEC 862cb64cb2SGeorge V. Neville-Neil #include <netinet/ip_ipsec.h> 871dfcf0d2SAndre Oppermann #include <netipsec/ipsec.h> 88b2630c29SGeorge V. Neville-Neil #endif /* IPSEC*/ 896a800098SYoshinobu Inoue 901dfcf0d2SAndre Oppermann #include <machine/in_cksum.h> 911dfcf0d2SAndre Oppermann 92aed55708SRobert Watson #include <security/mac/mac_framework.h> 93aed55708SRobert Watson 9453dcc544SMike Silbersack #ifdef MBUF_STRESS_TEST 9505b9d121SBjoern A. Zeeb static int mbuf_frag_size = 0; 969d9edc56SMike Silbersack SYSCTL_INT(_net_inet_ip, OID_AUTO, mbuf_frag_size, CTLFLAG_RW, 979d9edc56SMike Silbersack &mbuf_frag_size, 0, "Fragment outgoing mbufs to this size"); 989d9edc56SMike Silbersack #endif 999d9edc56SMike Silbersack 100df8bae1dSRodney W. Grimes static void ip_mloopback 1014d77a549SAlfred Perlstein (struct ifnet *, struct mbuf *, struct sockaddr_in *, int); 102afed1b49SDarren Reed 103afed1b49SDarren Reed 1048b889dbbSBruce M Simpson extern int in_mcast_loop; 10593e0e116SJulian Elischer extern struct protosw inetsw[]; 10693e0e116SJulian Elischer 107df8bae1dSRodney W. Grimes /* 108df8bae1dSRodney W. Grimes * IP output. The packet in mbuf chain m contains a skeletal IP 109df8bae1dSRodney W. Grimes * header (with len, off, ttl, proto, tos, src, dst). 110df8bae1dSRodney W. Grimes * The mbuf chain containing the packet will be freed. 111df8bae1dSRodney W. Grimes * The mbuf opt, if present, will not be freed. 112bf984051SGleb Smirnoff * If route ro is present and has ro_rt initialized, route lookup would be 113bf984051SGleb Smirnoff * skipped and ro->ro_rt would be used. If ro is present but ro->ro_rt is NULL, 114bf984051SGleb Smirnoff * then result of route lookup is stored in ro->ro_rt. 115bf984051SGleb Smirnoff * 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 { 1231e78ac21SJeffrey Hsu struct ip *ip; 124fc74d005SBjoern A. Zeeb struct ifnet *ifp = NULL; /* keep compiler happy */ 125ac9d7e26SMax Laier struct mbuf *m0; 12623bf9953SPoul-Henning Kamp int hlen = sizeof (struct ip); 1273ae2ad08SJohn-Mark Gurney int mtu; 128ca8b83b0SLuigi Rizzo int error = 0; 129ca8b83b0SLuigi Rizzo struct sockaddr_in *dst; 1304c7a6059SGleb Smirnoff const struct sockaddr_in *gw; 1313c73180fSGleb Smirnoff struct in_ifaddr *ia; 13221d172a3SGleb Smirnoff int isbroadcast; 133078468edSGleb Smirnoff uint16_t ip_len, ip_off; 134e3f406b3SRuslan Ermilov struct route iproute; 135ec396e61SLuigi Rizzo struct rtentry *rte; /* cache for ro->ro_rt */ 1369b932e9eSAndre Oppermann struct in_addr odst; 1379b932e9eSAndre Oppermann struct m_tag *fwd_tag = NULL; 1389c57a5b6SHiroki Sato uint32_t fibnum; 139fe82cbe8SGleb Smirnoff int have_ia_ref; 1409c57a5b6SHiroki Sato int needfiblookup; 1411a499816SEdward Tomasz Napierala #ifdef IPSEC 1421a499816SEdward Tomasz Napierala int no_route_but_check_spd = 0; 1431a499816SEdward Tomasz Napierala #endif 144ac9d7e26SMax Laier M_ASSERTPKTHDR(m); 14536e8826fSMax Laier 146bc97ba51SJulian Elischer if (inp != NULL) { 147baa45840SRobert Watson INP_LOCK_ASSERT(inp); 14862e1ba83SRobert Watson M_SETFIB(m, inp->inp_inc.inc_fibnum); 149c2529042SHans Petter Selasky if ((flags & IP_NODEFAULTFLOWID) == 0) { 15080cb9f21SKip Macy m->m_pkthdr.flowid = inp->inp_flowid; 1519c423972SAdrian Chadd M_HASHTYPE_SET(m, inp->inp_flowtype); 15280cb9f21SKip Macy } 153bc97ba51SJulian Elischer } 15462e1ba83SRobert Watson 15562e1ba83SRobert Watson if (ro == NULL) { 15662e1ba83SRobert Watson ro = &iproute; 15762e1ba83SRobert Watson bzero(ro, sizeof (*ro)); 158bf984051SGleb Smirnoff } 15962e1ba83SRobert Watson 16053be8fcaSBjoern A. Zeeb #ifdef FLOWTABLE 1610ff96b4fSGleb Smirnoff if (ro->ro_rt == NULL) 1620ff96b4fSGleb Smirnoff (void )flowtable_lookup(AF_INET, m, ro); 16353be8fcaSBjoern A. Zeeb #endif 1642b25acc1SLuigi Rizzo 165df8bae1dSRodney W. Grimes if (opt) { 166ca8b83b0SLuigi Rizzo int len = 0; 167df8bae1dSRodney W. Grimes m = ip_insertoptions(m, opt, &len); 168cb7641e8SMaxim Konovalov if (len != 0) 169ca8b83b0SLuigi Rizzo hlen = len; /* ip->ip_hl is updated above */ 170df8bae1dSRodney W. Grimes } 171df8bae1dSRodney W. Grimes ip = mtod(m, struct ip *); 1728f134647SGleb Smirnoff ip_len = ntohs(ip->ip_len); 1738f134647SGleb Smirnoff ip_off = ntohs(ip->ip_off); 1743efc3014SJulian Elischer 175df8bae1dSRodney W. Grimes if ((flags & (IP_FORWARDING|IP_RAWOUTPUT)) == 0) { 17653be11f6SPoul-Henning Kamp ip->ip_v = IPVERSION; 17753be11f6SPoul-Henning Kamp ip->ip_hl = hlen >> 2; 1786d947416SGleb Smirnoff ip_fillid(ip); 17986425c62SRobert Watson IPSTAT_INC(ips_localout); 180df8bae1dSRodney W. Grimes } else { 181ca8b83b0SLuigi Rizzo /* Header already set, fetch hlen from there */ 18253be11f6SPoul-Henning Kamp hlen = ip->ip_hl << 2; 183df8bae1dSRodney W. Grimes } 1849c9137eaSGarrett Wollman 185054692a4SAlexander V. Chernikov /* 186054692a4SAlexander V. Chernikov * dst/gw handling: 187054692a4SAlexander V. Chernikov * 1883c065f2fSGleb Smirnoff * dst can be rewritten but always points to &ro->ro_dst. 1893c065f2fSGleb Smirnoff * gw is readonly but can point either to dst OR rt_gateway, 1903c065f2fSGleb Smirnoff * therefore we need restore gw if we're redoing lookup. 191054692a4SAlexander V. Chernikov */ 1924c7a6059SGleb Smirnoff gw = dst = (struct sockaddr_in *)&ro->ro_dst; 1939c57a5b6SHiroki Sato fibnum = (inp != NULL) ? inp->inp_inc.inc_fibnum : M_GETFIB(m); 1949b932e9eSAndre Oppermann again: 1953c73180fSGleb Smirnoff ia = NULL; 196fe82cbe8SGleb Smirnoff have_ia_ref = 0; 197df8bae1dSRodney W. Grimes /* 1983c065f2fSGleb Smirnoff * If there is a cached route, check that it is to the same 1993c065f2fSGleb Smirnoff * destination and is still up. If not, free it and try again. 2003c065f2fSGleb Smirnoff * The address family should also be checked in case of sharing 2013c065f2fSGleb Smirnoff * the cache with IPv6. 202df8bae1dSRodney W. Grimes */ 203ec396e61SLuigi Rizzo rte = ro->ro_rt; 204ec396e61SLuigi Rizzo if (rte && ((rte->rt_flags & RTF_UP) == 0 || 205c7ea0aa6SQing Li rte->rt_ifp == NULL || 206c7ea0aa6SQing Li !RT_LINK_IS_UP(rte->rt_ifp) || 207a4a6e773SHajimu UMEMOTO dst->sin_family != AF_INET || 2089b932e9eSAndre Oppermann dst->sin_addr.s_addr != ip->ip_dst.s_addr)) { 209bf984051SGleb Smirnoff RO_RTFREE(ro); 210bf984051SGleb Smirnoff ro->ro_lle = NULL; 211bf984051SGleb Smirnoff rte = NULL; 212054692a4SAlexander V. Chernikov gw = dst; 213df8bae1dSRodney W. Grimes } 214ec396e61SLuigi Rizzo if (rte == NULL && fwd_tag == NULL) { 215a4a6e773SHajimu UMEMOTO bzero(dst, sizeof(*dst)); 216df8bae1dSRodney W. Grimes dst->sin_family = AF_INET; 217df8bae1dSRodney W. Grimes dst->sin_len = sizeof(*dst); 2189b932e9eSAndre Oppermann dst->sin_addr = ip->ip_dst; 219df8bae1dSRodney W. Grimes } 220df8bae1dSRodney W. Grimes /* 221a3fd02d8SBruce M Simpson * If routing to interface only, short circuit routing lookup. 222a3fd02d8SBruce M Simpson * The use of an all-ones broadcast address implies this; an 223a3fd02d8SBruce M Simpson * interface is specified by the broadcast address of an interface, 224a3fd02d8SBruce M Simpson * or the destination address of a ptp interface. 225df8bae1dSRodney W. Grimes */ 226a3fd02d8SBruce M Simpson if (flags & IP_SENDONES) { 2274f8585e0SAlan Somers if ((ia = ifatoia(ifa_ifwithbroadaddr(sintosa(dst), 22858a39d8cSAlan Somers M_GETFIB(m)))) == NULL && 2294f8585e0SAlan Somers (ia = ifatoia(ifa_ifwithdstaddr(sintosa(dst), 23058a39d8cSAlan Somers M_GETFIB(m)))) == NULL) { 23186425c62SRobert Watson IPSTAT_INC(ips_noroute); 232a3fd02d8SBruce M Simpson error = ENETUNREACH; 233a3fd02d8SBruce M Simpson goto bad; 234a3fd02d8SBruce M Simpson } 235fe82cbe8SGleb Smirnoff have_ia_ref = 1; 236a3fd02d8SBruce M Simpson ip->ip_dst.s_addr = INADDR_BROADCAST; 237a3fd02d8SBruce M Simpson dst->sin_addr = ip->ip_dst; 238a3fd02d8SBruce M Simpson ifp = ia->ia_ifp; 239a3fd02d8SBruce M Simpson ip->ip_ttl = 1; 240a3fd02d8SBruce M Simpson isbroadcast = 1; 241a3fd02d8SBruce M Simpson } else if (flags & IP_ROUTETOIF) { 2424f8585e0SAlan Somers if ((ia = ifatoia(ifa_ifwithdstaddr(sintosa(dst), 24358a39d8cSAlan Somers M_GETFIB(m)))) == NULL && 2444f8585e0SAlan Somers (ia = ifatoia(ifa_ifwithnet(sintosa(dst), 0, 24558a39d8cSAlan Somers M_GETFIB(m)))) == NULL) { 24686425c62SRobert Watson IPSTAT_INC(ips_noroute); 247df8bae1dSRodney W. Grimes error = ENETUNREACH; 248df8bae1dSRodney W. Grimes goto bad; 249df8bae1dSRodney W. Grimes } 250fe82cbe8SGleb Smirnoff have_ia_ref = 1; 251df8bae1dSRodney W. Grimes ifp = ia->ia_ifp; 252df8bae1dSRodney W. Grimes ip->ip_ttl = 1; 2539f9b3dc4SGarrett Wollman isbroadcast = in_broadcast(dst->sin_addr, ifp); 2543afefa39SDaniel C. Sobral } else if (IN_MULTICAST(ntohl(ip->ip_dst.s_addr)) && 25538c1bc35SRuslan Ermilov imo != NULL && imo->imo_multicast_ifp != NULL) { 2563afefa39SDaniel C. Sobral /* 25738c1bc35SRuslan Ermilov * Bypass the normal routing lookup for multicast 25838c1bc35SRuslan Ermilov * packets if the interface is specified. 2593afefa39SDaniel C. Sobral */ 26038c1bc35SRuslan Ermilov ifp = imo->imo_multicast_ifp; 26138c1bc35SRuslan Ermilov IFP_TO_IA(ifp, ia); 262fe82cbe8SGleb Smirnoff if (ia) 263fe82cbe8SGleb Smirnoff have_ia_ref = 1; 26438c1bc35SRuslan Ermilov isbroadcast = 0; /* fool gcc */ 265df8bae1dSRodney W. Grimes } else { 2662c17fe93SGarrett Wollman /* 26797d8d152SAndre Oppermann * We want to do any cloning requested by the link layer, 26897d8d152SAndre Oppermann * as this is probably required in all cases for correct 26997d8d152SAndre Oppermann * operation (as it is for ARP). 2702c17fe93SGarrett Wollman */ 271ec396e61SLuigi Rizzo if (rte == NULL) { 272e440aed9SQing Li #ifdef RADIX_MPATH 2738b07e49aSJulian Elischer rtalloc_mpath_fib(ro, 2748b07e49aSJulian Elischer ntohl(ip->ip_src.s_addr ^ ip->ip_dst.s_addr), 2759c57a5b6SHiroki Sato fibnum); 276e440aed9SQing Li #else 2779c57a5b6SHiroki Sato in_rtalloc_ign(ro, 0, fibnum); 278e440aed9SQing Li #endif 279ec396e61SLuigi Rizzo rte = ro->ro_rt; 280ec396e61SLuigi Rizzo } 281c7ea0aa6SQing Li if (rte == NULL || 282c7ea0aa6SQing Li rte->rt_ifp == NULL || 283c7ea0aa6SQing Li !RT_LINK_IS_UP(rte->rt_ifp)) { 2841a499816SEdward Tomasz Napierala #ifdef IPSEC 2851a499816SEdward Tomasz Napierala /* 2861a499816SEdward Tomasz Napierala * There is no route for this packet, but it is 2871a499816SEdward Tomasz Napierala * possible that a matching SPD entry exists. 2881a499816SEdward Tomasz Napierala */ 2891a499816SEdward Tomasz Napierala no_route_but_check_spd = 1; 2901a499816SEdward Tomasz Napierala mtu = 0; /* Silence GCC warning. */ 2911a499816SEdward Tomasz Napierala goto sendit; 2921a499816SEdward Tomasz Napierala #endif 29386425c62SRobert Watson IPSTAT_INC(ips_noroute); 294df8bae1dSRodney W. Grimes error = EHOSTUNREACH; 295df8bae1dSRodney W. Grimes goto bad; 296df8bae1dSRodney W. Grimes } 297ec396e61SLuigi Rizzo ia = ifatoia(rte->rt_ifa); 298ec396e61SLuigi Rizzo ifp = rte->rt_ifp; 299e3a7aa6fSGleb Smirnoff counter_u64_add(rte->rt_pksent, 1); 300ec396e61SLuigi Rizzo if (rte->rt_flags & RTF_GATEWAY) 3014c7a6059SGleb Smirnoff gw = (struct sockaddr_in *)rte->rt_gateway; 302ec396e61SLuigi Rizzo if (rte->rt_flags & RTF_HOST) 303ec396e61SLuigi Rizzo isbroadcast = (rte->rt_flags & RTF_BROADCAST); 3049f9b3dc4SGarrett Wollman else 3054c7a6059SGleb Smirnoff isbroadcast = in_broadcast(gw->sin_addr, ifp); 306df8bae1dSRodney W. Grimes } 3073ae2ad08SJohn-Mark Gurney /* 3083ae2ad08SJohn-Mark Gurney * Calculate MTU. If we have a route that is up, use that, 3093ae2ad08SJohn-Mark Gurney * otherwise use the interface's MTU. 3103ae2ad08SJohn-Mark Gurney */ 3117f948f12SAlexander V. Chernikov if (rte != NULL && (rte->rt_flags & (RTF_UP|RTF_HOST))) 312e3a7aa6fSGleb Smirnoff mtu = rte->rt_mtu; 3137f948f12SAlexander V. Chernikov else 3143ae2ad08SJohn-Mark Gurney mtu = ifp->if_mtu; 315c744cde4SBjoern A. Zeeb /* Catch a possible divide by zero later. */ 316c744cde4SBjoern A. Zeeb KASSERT(mtu > 0, ("%s: mtu %d <= 0, rte=%p (rt_flags=0x%08x) ifp=%p", 317c744cde4SBjoern A. Zeeb __func__, mtu, rte, (rte != NULL) ? rte->rt_flags : 0, ifp)); 3189b932e9eSAndre Oppermann if (IN_MULTICAST(ntohl(ip->ip_dst.s_addr))) { 319df8bae1dSRodney W. Grimes m->m_flags |= M_MCAST; 320df8bae1dSRodney W. Grimes /* 321183e1c86SGleb Smirnoff * IP destination address is multicast. Make sure "gw" 322183e1c86SGleb Smirnoff * still points to the address in "ro". (It may have been 323183e1c86SGleb Smirnoff * changed to point to a gateway address, above.) 324183e1c86SGleb Smirnoff */ 325183e1c86SGleb Smirnoff gw = dst; 326183e1c86SGleb Smirnoff /* 327df8bae1dSRodney W. Grimes * See if the caller provided any multicast options 328df8bae1dSRodney W. Grimes */ 329df8bae1dSRodney W. Grimes if (imo != NULL) { 330df8bae1dSRodney W. Grimes ip->ip_ttl = imo->imo_multicast_ttl; 3311c5de19aSGarrett Wollman if (imo->imo_multicast_vif != -1) 3321c5de19aSGarrett Wollman ip->ip_src.s_addr = 333bbb4330bSLuigi Rizzo ip_mcast_src ? 334bbb4330bSLuigi Rizzo ip_mcast_src(imo->imo_multicast_vif) : 335bbb4330bSLuigi Rizzo INADDR_ANY; 336df8bae1dSRodney W. Grimes } else 337df8bae1dSRodney W. Grimes ip->ip_ttl = IP_DEFAULT_MULTICAST_TTL; 338df8bae1dSRodney W. Grimes /* 339df8bae1dSRodney W. Grimes * Confirm that the outgoing interface supports multicast. 340df8bae1dSRodney W. Grimes */ 3411c5de19aSGarrett Wollman if ((imo == NULL) || (imo->imo_multicast_vif == -1)) { 342df8bae1dSRodney W. Grimes if ((ifp->if_flags & IFF_MULTICAST) == 0) { 34386425c62SRobert Watson IPSTAT_INC(ips_noroute); 344df8bae1dSRodney W. Grimes error = ENETUNREACH; 345df8bae1dSRodney W. Grimes goto bad; 346df8bae1dSRodney W. Grimes } 3471c5de19aSGarrett Wollman } 348df8bae1dSRodney W. Grimes /* 349df8bae1dSRodney W. Grimes * If source address not specified yet, use address 350df8bae1dSRodney W. Grimes * of outgoing interface. 351df8bae1dSRodney W. Grimes */ 352df8bae1dSRodney W. Grimes if (ip->ip_src.s_addr == INADDR_ANY) { 35338c1bc35SRuslan Ermilov /* Interface may have no addresses. */ 35438c1bc35SRuslan Ermilov if (ia != NULL) 35538c1bc35SRuslan Ermilov ip->ip_src = IA_SIN(ia)->sin_addr; 356df8bae1dSRodney W. Grimes } 357df8bae1dSRodney W. Grimes 3588b889dbbSBruce M Simpson if ((imo == NULL && in_mcast_loop) || 3598b889dbbSBruce M Simpson (imo && imo->imo_multicast_loop)) { 360df8bae1dSRodney W. Grimes /* 3618b889dbbSBruce M Simpson * Loop back multicast datagram if not expressly 3628b889dbbSBruce M Simpson * forbidden to do so, even if we are not a member 3638b889dbbSBruce M Simpson * of the group; ip_input() will filter it later, 3648b889dbbSBruce M Simpson * thus deferring a hash lookup and mutex acquisition 3658b889dbbSBruce M Simpson * at the expense of a cheap copy using m_copym(). 366df8bae1dSRodney W. Grimes */ 36786b1d6d2SBill Fenner ip_mloopback(ifp, m, dst, hlen); 3688b889dbbSBruce M Simpson } else { 369df8bae1dSRodney W. Grimes /* 370df8bae1dSRodney W. Grimes * If we are acting as a multicast router, perform 371df8bae1dSRodney W. Grimes * multicast forwarding as if the packet had just 372df8bae1dSRodney W. Grimes * arrived on the interface to which we are about 373df8bae1dSRodney W. Grimes * to send. The multicast forwarding function 374df8bae1dSRodney W. Grimes * recursively calls this function, using the 375df8bae1dSRodney W. Grimes * IP_FORWARDING flag to prevent infinite recursion. 376df8bae1dSRodney W. Grimes * 377df8bae1dSRodney W. Grimes * Multicasts that are looped back by ip_mloopback(), 378df8bae1dSRodney W. Grimes * above, will be forwarded by the ip_input() routine, 379df8bae1dSRodney W. Grimes * if necessary. 380df8bae1dSRodney W. Grimes */ 381603724d3SBjoern A. Zeeb if (V_ip_mrouter && (flags & IP_FORWARDING) == 0) { 382f0068c4aSGarrett Wollman /* 383bbb4330bSLuigi Rizzo * If rsvp daemon is not running, do not 384f0068c4aSGarrett Wollman * set ip_moptions. This ensures that the packet 385f0068c4aSGarrett Wollman * is multicast and not just sent down one link 386f0068c4aSGarrett Wollman * as prescribed by rsvpd. 387f0068c4aSGarrett Wollman */ 388603724d3SBjoern A. Zeeb if (!V_rsvp_on) 389f0068c4aSGarrett Wollman imo = NULL; 390bbb4330bSLuigi Rizzo if (ip_mforward && 391bbb4330bSLuigi Rizzo ip_mforward(ip, ifp, m, imo) != 0) { 392df8bae1dSRodney W. Grimes m_freem(m); 393df8bae1dSRodney W. Grimes goto done; 394df8bae1dSRodney W. Grimes } 395df8bae1dSRodney W. Grimes } 396df8bae1dSRodney W. Grimes } 3975e9ae478SGarrett Wollman 398df8bae1dSRodney W. Grimes /* 399df8bae1dSRodney W. Grimes * Multicasts with a time-to-live of zero may be looped- 400df8bae1dSRodney W. Grimes * back, above, but must not be transmitted on a network. 401df8bae1dSRodney W. Grimes * Also, multicasts addressed to the loopback interface 402df8bae1dSRodney W. Grimes * are not sent -- the above call to ip_mloopback() will 4038b889dbbSBruce M Simpson * loop back a copy. ip_input() will drop the copy if 4048b889dbbSBruce M Simpson * this host does not belong to the destination group on 4058b889dbbSBruce M Simpson * the loopback interface. 406df8bae1dSRodney W. Grimes */ 407f5fea3ddSPaul Traina if (ip->ip_ttl == 0 || ifp->if_flags & IFF_LOOPBACK) { 408df8bae1dSRodney W. Grimes m_freem(m); 409df8bae1dSRodney W. Grimes goto done; 410df8bae1dSRodney W. Grimes } 411df8bae1dSRodney W. Grimes 412df8bae1dSRodney W. Grimes goto sendit; 413df8bae1dSRodney W. Grimes } 4146a7c943cSAndre Oppermann 415df8bae1dSRodney W. Grimes /* 4162b25acc1SLuigi Rizzo * If the source address is not specified yet, use the address 417cb459254SJohn-Mark Gurney * of the outoing interface. 418df8bae1dSRodney W. Grimes */ 419f9e354dfSJulian Elischer if (ip->ip_src.s_addr == INADDR_ANY) { 42038c1bc35SRuslan Ermilov /* Interface may have no addresses. */ 42138c1bc35SRuslan Ermilov if (ia != NULL) { 422df8bae1dSRodney W. Grimes ip->ip_src = IA_SIN(ia)->sin_addr; 423f9e354dfSJulian Elischer } 42438c1bc35SRuslan Ermilov } 4256a7c943cSAndre Oppermann 426ca7a789aSMax Laier /* 427df8bae1dSRodney W. Grimes * Look for broadcast address and 4288c3f5566SRuslan Ermilov * verify user is allowed to send 429df8bae1dSRodney W. Grimes * such a packet. 430df8bae1dSRodney W. Grimes */ 4319f9b3dc4SGarrett Wollman if (isbroadcast) { 432df8bae1dSRodney W. Grimes if ((ifp->if_flags & IFF_BROADCAST) == 0) { 433df8bae1dSRodney W. Grimes error = EADDRNOTAVAIL; 434df8bae1dSRodney W. Grimes goto bad; 435df8bae1dSRodney W. Grimes } 436df8bae1dSRodney W. Grimes if ((flags & IP_ALLOWBROADCAST) == 0) { 437df8bae1dSRodney W. Grimes error = EACCES; 438df8bae1dSRodney W. Grimes goto bad; 439df8bae1dSRodney W. Grimes } 440df8bae1dSRodney W. Grimes /* don't allow broadcast messages to be fragmented */ 4418f134647SGleb Smirnoff if (ip_len > mtu) { 442df8bae1dSRodney W. Grimes error = EMSGSIZE; 443df8bae1dSRodney W. Grimes goto bad; 444df8bae1dSRodney W. Grimes } 445df8bae1dSRodney W. Grimes m->m_flags |= M_BCAST; 4469f9b3dc4SGarrett Wollman } else { 447df8bae1dSRodney W. Grimes m->m_flags &= ~M_BCAST; 4489f9b3dc4SGarrett Wollman } 449df8bae1dSRodney W. Grimes 450f1743588SDarren Reed sendit: 451b2630c29SGeorge V. Neville-Neil #ifdef IPSEC 4520275b2e3SAndrey V. Elsukov switch(ip_ipsec_output(&m, inp, &error)) { 4531dfcf0d2SAndre Oppermann case 1: 45433841545SHajimu UMEMOTO goto bad; 4551dfcf0d2SAndre Oppermann case -1: 4561dfcf0d2SAndre Oppermann goto done; 4571dfcf0d2SAndre Oppermann case 0: 45833841545SHajimu UMEMOTO default: 4591dfcf0d2SAndre Oppermann break; /* Continue with packet processing. */ 46033841545SHajimu UMEMOTO } 4611a499816SEdward Tomasz Napierala /* 4621a499816SEdward Tomasz Napierala * Check if there was a route for this packet; return error if not. 4631a499816SEdward Tomasz Napierala */ 4641a499816SEdward Tomasz Napierala if (no_route_but_check_spd) { 4651a499816SEdward Tomasz Napierala IPSTAT_INC(ips_noroute); 4661a499816SEdward Tomasz Napierala error = EHOSTUNREACH; 4671a499816SEdward Tomasz Napierala goto bad; 4681a499816SEdward Tomasz Napierala } 4691dfcf0d2SAndre Oppermann /* Update variables that are affected by ipsec4_output(). */ 47033841545SHajimu UMEMOTO ip = mtod(m, struct ip *); 47133841545SHajimu UMEMOTO hlen = ip->ip_hl << 2; 472b2630c29SGeorge V. Neville-Neil #endif /* IPSEC */ 47333841545SHajimu UMEMOTO 474c21fd232SAndre Oppermann /* Jump over all PFIL processing if hooks are not active. */ 4750b4b0b0fSJulian Elischer if (!PFIL_HOOKED(&V_inet_pfil_hook)) 476c21fd232SAndre Oppermann goto passout; 477c21fd232SAndre Oppermann 478c21fd232SAndre Oppermann /* Run through list of hooks for output packets. */ 4799b932e9eSAndre Oppermann odst.s_addr = ip->ip_dst.s_addr; 4800b4b0b0fSJulian Elischer error = pfil_run_hooks(&V_inet_pfil_hook, &m, ifp, PFIL_OUT, inp); 481134ea224SSam Leffler if (error != 0 || m == NULL) 482c4ac87eaSDarren Reed goto done; 4839b932e9eSAndre Oppermann 484c4ac87eaSDarren Reed ip = mtod(m, struct ip *); 4859c57a5b6SHiroki Sato needfiblookup = 0; 486fed1c7e9SSøren Schmidt 4879b932e9eSAndre Oppermann /* See if destination IP address was changed by packet filter. */ 4889b932e9eSAndre Oppermann if (odst.s_addr != ip->ip_dst.s_addr) { 4899b932e9eSAndre Oppermann m->m_flags |= M_SKIP_FIREWALL; 490f4fca2d8SAndre Oppermann /* If destination is now ourself drop to ip_input(). */ 4919b932e9eSAndre Oppermann if (in_localip(ip->ip_dst)) { 4929b932e9eSAndre Oppermann m->m_flags |= M_FASTFWD_OURS; 493f9e354dfSJulian Elischer if (m->m_pkthdr.rcvif == NULL) 494603724d3SBjoern A. Zeeb m->m_pkthdr.rcvif = V_loif; 49520c822f3SJonathan Lemon if (m->m_pkthdr.csum_flags & CSUM_DELAY_DATA) { 49620c822f3SJonathan Lemon m->m_pkthdr.csum_flags |= 49720c822f3SJonathan Lemon CSUM_DATA_VALID | CSUM_PSEUDO_HDR; 498ac9d7e26SMax Laier m->m_pkthdr.csum_data = 0xffff; 49920c822f3SJonathan Lemon } 50020c822f3SJonathan Lemon m->m_pkthdr.csum_flags |= 50120c822f3SJonathan Lemon CSUM_IP_CHECKED | CSUM_IP_VALID; 5022f4afd21SRandall Stewart #ifdef SCTP 5032f4afd21SRandall Stewart if (m->m_pkthdr.csum_flags & CSUM_SCTP) 5042f4afd21SRandall Stewart m->m_pkthdr.csum_flags |= CSUM_SCTP_VALID; 5052f4afd21SRandall Stewart #endif 5069b932e9eSAndre Oppermann error = netisr_queue(NETISR_IP, m); 5079b932e9eSAndre Oppermann goto done; 508fe82cbe8SGleb Smirnoff } else { 509fe82cbe8SGleb Smirnoff if (have_ia_ref) 510fe82cbe8SGleb Smirnoff ifa_free(&ia->ia_ifa); 5119c57a5b6SHiroki Sato needfiblookup = 1; /* Redo the routing table lookup. */ 5129b932e9eSAndre Oppermann } 513fe82cbe8SGleb Smirnoff } 5149c57a5b6SHiroki Sato /* See if fib was changed by packet filter. */ 5159c57a5b6SHiroki Sato if (fibnum != M_GETFIB(m)) { 5169c57a5b6SHiroki Sato m->m_flags |= M_SKIP_FIREWALL; 5179c57a5b6SHiroki Sato fibnum = M_GETFIB(m); 5189c57a5b6SHiroki Sato RO_RTFREE(ro); 5199c57a5b6SHiroki Sato needfiblookup = 1; 5209c57a5b6SHiroki Sato } 5219c57a5b6SHiroki Sato if (needfiblookup) 5229c57a5b6SHiroki Sato goto again; 5239b932e9eSAndre Oppermann 5249b932e9eSAndre Oppermann /* See if local, if yes, send it to netisr with IP_FASTFWD_OURS. */ 5259b932e9eSAndre Oppermann if (m->m_flags & M_FASTFWD_OURS) { 5269b932e9eSAndre Oppermann if (m->m_pkthdr.rcvif == NULL) 527603724d3SBjoern A. Zeeb m->m_pkthdr.rcvif = V_loif; 5289b932e9eSAndre Oppermann if (m->m_pkthdr.csum_flags & CSUM_DELAY_DATA) { 5299b932e9eSAndre Oppermann m->m_pkthdr.csum_flags |= 5309b932e9eSAndre Oppermann CSUM_DATA_VALID | CSUM_PSEUDO_HDR; 5319b932e9eSAndre Oppermann m->m_pkthdr.csum_data = 0xffff; 5329b932e9eSAndre Oppermann } 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 m->m_pkthdr.csum_flags |= 5389b932e9eSAndre Oppermann CSUM_IP_CHECKED | CSUM_IP_VALID; 5399b932e9eSAndre Oppermann 5409b932e9eSAndre Oppermann error = netisr_queue(NETISR_IP, m); 541f9e354dfSJulian Elischer goto done; 542f9e354dfSJulian Elischer } 5439b932e9eSAndre Oppermann /* Or forward to some other address? */ 544ffdbf9daSAndrey V. Elsukov if ((m->m_flags & M_IP_NEXTHOP) && 545ffdbf9daSAndrey V. Elsukov (fwd_tag = m_tag_find(m, PACKET_TAG_IPFORWARD, NULL)) != NULL) { 5469b932e9eSAndre Oppermann bcopy((fwd_tag+1), dst, sizeof(struct sockaddr_in)); 5479b932e9eSAndre Oppermann m->m_flags |= M_SKIP_FIREWALL; 548ffdbf9daSAndrey V. Elsukov m->m_flags &= ~M_IP_NEXTHOP; 5499b932e9eSAndre Oppermann m_tag_delete(m, fwd_tag); 550fe82cbe8SGleb Smirnoff if (have_ia_ref) 551fe82cbe8SGleb Smirnoff ifa_free(&ia->ia_ifa); 5529b932e9eSAndre Oppermann goto again; 553099dd043SAndre Oppermann } 5542b25acc1SLuigi Rizzo 555c21fd232SAndre Oppermann passout: 55651c8ec4aSRuslan Ermilov /* 127/8 must not appear on wire - RFC1122. */ 55751c8ec4aSRuslan Ermilov if ((ntohl(ip->ip_dst.s_addr) >> IN_CLASSA_NSHIFT) == IN_LOOPBACKNET || 55851c8ec4aSRuslan Ermilov (ntohl(ip->ip_src.s_addr) >> IN_CLASSA_NSHIFT) == IN_LOOPBACKNET) { 55951c8ec4aSRuslan Ermilov if ((ifp->if_flags & IFF_LOOPBACK) == 0) { 56086425c62SRobert Watson IPSTAT_INC(ips_badaddr); 56151c8ec4aSRuslan Ermilov error = EADDRNOTAVAIL; 56251c8ec4aSRuslan Ermilov goto bad; 56351c8ec4aSRuslan Ermilov } 56451c8ec4aSRuslan Ermilov } 56551c8ec4aSRuslan Ermilov 566206a3274SRuslan Ermilov m->m_pkthdr.csum_flags |= CSUM_IP; 567078468edSGleb Smirnoff if (m->m_pkthdr.csum_flags & CSUM_DELAY_DATA & ~ifp->if_hwassist) { 568db4f9cc7SJonathan Lemon in_delayed_cksum(m); 569078468edSGleb Smirnoff m->m_pkthdr.csum_flags &= ~CSUM_DELAY_DATA; 570db4f9cc7SJonathan Lemon } 5712f4afd21SRandall Stewart #ifdef SCTP 572078468edSGleb Smirnoff if (m->m_pkthdr.csum_flags & CSUM_SCTP & ~ifp->if_hwassist) { 5731966e5b5SRandall Stewart sctp_delayed_cksum(m, (uint32_t)(ip->ip_hl << 2)); 574078468edSGleb Smirnoff m->m_pkthdr.csum_flags &= ~CSUM_SCTP; 5752f4afd21SRandall Stewart } 5762f4afd21SRandall Stewart #endif 577db4f9cc7SJonathan Lemon 578e7319babSPoul-Henning Kamp /* 579db4f9cc7SJonathan Lemon * If small enough for interface, or the interface will take 580233dcce1SAndre Oppermann * care of the fragmentation for us, we can just send directly. 581df8bae1dSRodney W. Grimes */ 58221d172a3SGleb Smirnoff if (ip_len <= mtu || 583bf7dcda3SGleb Smirnoff (m->m_pkthdr.csum_flags & ifp->if_hwassist & CSUM_TSO) != 0) { 584df8bae1dSRodney W. Grimes ip->ip_sum = 0; 585078468edSGleb Smirnoff if (m->m_pkthdr.csum_flags & CSUM_IP & ~ifp->if_hwassist) { 586df8bae1dSRodney W. Grimes ip->ip_sum = in_cksum(m, hlen); 587078468edSGleb Smirnoff m->m_pkthdr.csum_flags &= ~CSUM_IP; 588078468edSGleb Smirnoff } 5895da9f8faSJosef Karthauser 590233dcce1SAndre Oppermann /* 591233dcce1SAndre Oppermann * Record statistics for this interface address. 592233dcce1SAndre Oppermann * With CSUM_TSO the byte/packet count will be slightly 593233dcce1SAndre Oppermann * incorrect because we count the IP+TCP headers only 594233dcce1SAndre Oppermann * once instead of for every generated packet. 595233dcce1SAndre Oppermann */ 59638c1bc35SRuslan Ermilov if (!(flags & IP_FORWARDING) && ia) { 597233dcce1SAndre Oppermann if (m->m_pkthdr.csum_flags & CSUM_TSO) 5987caf4ab7SGleb Smirnoff counter_u64_add(ia->ia_ifa.ifa_opackets, 5997caf4ab7SGleb Smirnoff m->m_pkthdr.len / m->m_pkthdr.tso_segsz); 600233dcce1SAndre Oppermann else 6017caf4ab7SGleb Smirnoff counter_u64_add(ia->ia_ifa.ifa_opackets, 1); 6027caf4ab7SGleb Smirnoff 6037caf4ab7SGleb Smirnoff counter_u64_add(ia->ia_ifa.ifa_obytes, m->m_pkthdr.len); 6045da9f8faSJosef Karthauser } 60553dcc544SMike Silbersack #ifdef MBUF_STRESS_TEST 6063390d476SMike Silbersack if (mbuf_frag_size && m->m_pkthdr.len > mbuf_frag_size) 607eb1b1807SGleb Smirnoff m = m_fragment(m, M_NOWAIT, mbuf_frag_size); 6089d9edc56SMike Silbersack #endif 609147f74d1SAndre Oppermann /* 610147f74d1SAndre Oppermann * Reset layer specific mbuf flags 611147f74d1SAndre Oppermann * to avoid confusing lower layers. 612147f74d1SAndre Oppermann */ 61386bd0491SAndre Oppermann m_clrprotoflags(m); 61457f60867SMark Johnston IP_PROBE(send, NULL, NULL, ip, ifp, ip, NULL); 61547e8d432SGleb Smirnoff error = (*ifp->if_output)(ifp, m, 61647e8d432SGleb Smirnoff (const struct sockaddr *)gw, ro); 617df8bae1dSRodney W. Grimes goto done; 618df8bae1dSRodney W. Grimes } 6191e78ac21SJeffrey Hsu 620233dcce1SAndre Oppermann /* Balk when DF bit is set or the interface didn't support TSO. */ 62121d172a3SGleb Smirnoff if ((ip_off & IP_DF) || (m->m_pkthdr.csum_flags & CSUM_TSO)) { 622df8bae1dSRodney W. Grimes error = EMSGSIZE; 62386425c62SRobert Watson IPSTAT_INC(ips_cantfrag); 624df8bae1dSRodney W. Grimes goto bad; 625df8bae1dSRodney W. Grimes } 6261e78ac21SJeffrey Hsu 6271e78ac21SJeffrey Hsu /* 6281e78ac21SJeffrey Hsu * Too large for interface; fragment if possible. If successful, 6291e78ac21SJeffrey Hsu * on return, m will point to a list of packets to be sent. 6301e78ac21SJeffrey Hsu */ 631078468edSGleb Smirnoff error = ip_fragment(ip, &m, mtu, ifp->if_hwassist); 6321e78ac21SJeffrey Hsu if (error) 633df8bae1dSRodney W. Grimes goto bad; 6341e78ac21SJeffrey Hsu for (; m; m = m0) { 635df8bae1dSRodney W. Grimes m0 = m->m_nextpkt; 636b375c9ecSLuigi Rizzo m->m_nextpkt = 0; 63707203494SDaniel C. Sobral if (error == 0) { 638fe937674SJosef Karthauser /* Record statistics for this interface address. */ 63907203494SDaniel C. Sobral if (ia != NULL) { 6407caf4ab7SGleb Smirnoff counter_u64_add(ia->ia_ifa.ifa_opackets, 1); 6417caf4ab7SGleb Smirnoff counter_u64_add(ia->ia_ifa.ifa_obytes, 6427caf4ab7SGleb Smirnoff m->m_pkthdr.len); 64307203494SDaniel C. Sobral } 644147f74d1SAndre Oppermann /* 645147f74d1SAndre Oppermann * Reset layer specific mbuf flags 646147f74d1SAndre Oppermann * to avoid confusing upper layers. 647147f74d1SAndre Oppermann */ 64886bd0491SAndre Oppermann m_clrprotoflags(m); 649fe937674SJosef Karthauser 65057f60867SMark Johnston IP_PROBE(send, NULL, NULL, ip, ifp, ip, NULL); 651df8bae1dSRodney W. Grimes error = (*ifp->if_output)(ifp, m, 65247e8d432SGleb Smirnoff (const struct sockaddr *)gw, ro); 653fe937674SJosef Karthauser } else 654df8bae1dSRodney W. Grimes m_freem(m); 655df8bae1dSRodney W. Grimes } 656df8bae1dSRodney W. Grimes 657df8bae1dSRodney W. Grimes if (error == 0) 65886425c62SRobert Watson IPSTAT_INC(ips_fragmented); 6591e78ac21SJeffrey Hsu 660df8bae1dSRodney W. Grimes done: 661bf984051SGleb Smirnoff if (ro == &iproute) 662bf984051SGleb Smirnoff RO_RTFREE(ro); 663fe82cbe8SGleb Smirnoff if (have_ia_ref) 664fe82cbe8SGleb Smirnoff ifa_free(&ia->ia_ifa); 665df8bae1dSRodney W. Grimes return (error); 666df8bae1dSRodney W. Grimes bad: 6673528d68fSBill Paul m_freem(m); 668df8bae1dSRodney W. Grimes goto done; 669df8bae1dSRodney W. Grimes } 670df8bae1dSRodney W. Grimes 6711e78ac21SJeffrey Hsu /* 6721e78ac21SJeffrey Hsu * Create a chain of fragments which fit the given mtu. m_frag points to the 6731e78ac21SJeffrey Hsu * mbuf to be fragmented; on return it points to the chain with the fragments. 6741e78ac21SJeffrey Hsu * Return 0 if no error. If error, m_frag may contain a partially built 6751e78ac21SJeffrey Hsu * chain of fragments that should be freed by the caller. 6761e78ac21SJeffrey Hsu * 6771e78ac21SJeffrey Hsu * if_hwassist_flags is the hw offload capabilities (see if_data.ifi_hwassist) 6781e78ac21SJeffrey Hsu */ 6791e78ac21SJeffrey Hsu int 6801e78ac21SJeffrey Hsu ip_fragment(struct ip *ip, struct mbuf **m_frag, int mtu, 681078468edSGleb Smirnoff u_long if_hwassist_flags) 6821e78ac21SJeffrey Hsu { 6831e78ac21SJeffrey Hsu int error = 0; 6841e78ac21SJeffrey Hsu int hlen = ip->ip_hl << 2; 6851e78ac21SJeffrey Hsu int len = (mtu - hlen) & ~7; /* size of payload in each fragment */ 6861e78ac21SJeffrey Hsu int off; 6871e78ac21SJeffrey Hsu struct mbuf *m0 = *m_frag; /* the original packet */ 6881e78ac21SJeffrey Hsu int firstlen; 6891e78ac21SJeffrey Hsu struct mbuf **mnext; 6901e78ac21SJeffrey Hsu int nfrags; 69121d172a3SGleb Smirnoff uint16_t ip_len, ip_off; 6921e78ac21SJeffrey Hsu 69321d172a3SGleb Smirnoff ip_len = ntohs(ip->ip_len); 69421d172a3SGleb Smirnoff ip_off = ntohs(ip->ip_off); 69521d172a3SGleb Smirnoff 69621d172a3SGleb Smirnoff if (ip_off & IP_DF) { /* Fragmentation not allowed */ 69786425c62SRobert Watson IPSTAT_INC(ips_cantfrag); 6981e78ac21SJeffrey Hsu return EMSGSIZE; 6991e78ac21SJeffrey Hsu } 7001e78ac21SJeffrey Hsu 7011e78ac21SJeffrey Hsu /* 7021e78ac21SJeffrey Hsu * Must be able to put at least 8 bytes per fragment. 7031e78ac21SJeffrey Hsu */ 7041e78ac21SJeffrey Hsu if (len < 8) 7051e78ac21SJeffrey Hsu return EMSGSIZE; 7061e78ac21SJeffrey Hsu 7071e78ac21SJeffrey Hsu /* 7081e78ac21SJeffrey Hsu * If the interface will not calculate checksums on 7091e78ac21SJeffrey Hsu * fragmented packets, then do it here. 7101e78ac21SJeffrey Hsu */ 711da2299c5SAndre Oppermann if (m0->m_pkthdr.csum_flags & CSUM_DELAY_DATA) { 7121e78ac21SJeffrey Hsu in_delayed_cksum(m0); 7131e78ac21SJeffrey Hsu m0->m_pkthdr.csum_flags &= ~CSUM_DELAY_DATA; 7141e78ac21SJeffrey Hsu } 7152f4afd21SRandall Stewart #ifdef SCTP 716da2299c5SAndre Oppermann if (m0->m_pkthdr.csum_flags & CSUM_SCTP) { 7171966e5b5SRandall Stewart sctp_delayed_cksum(m0, hlen); 7182f4afd21SRandall Stewart m0->m_pkthdr.csum_flags &= ~CSUM_SCTP; 7192f4afd21SRandall Stewart } 7202f4afd21SRandall Stewart #endif 7211e78ac21SJeffrey Hsu if (len > PAGE_SIZE) { 7221e78ac21SJeffrey Hsu /* 7231e78ac21SJeffrey Hsu * Fragment large datagrams such that each segment 7241e78ac21SJeffrey Hsu * contains a multiple of PAGE_SIZE amount of data, 7251e78ac21SJeffrey Hsu * plus headers. This enables a receiver to perform 7261e78ac21SJeffrey Hsu * page-flipping zero-copy optimizations. 7271e78ac21SJeffrey Hsu * 7281e78ac21SJeffrey Hsu * XXX When does this help given that sender and receiver 7291e78ac21SJeffrey Hsu * could have different page sizes, and also mtu could 7301e78ac21SJeffrey Hsu * be less than the receiver's page size ? 7311e78ac21SJeffrey Hsu */ 7321e78ac21SJeffrey Hsu int newlen; 7331e78ac21SJeffrey Hsu 7349c0f6aa7SHans Petter Selasky off = MIN(mtu, m0->m_pkthdr.len); 7351e78ac21SJeffrey Hsu 7361e78ac21SJeffrey Hsu /* 7371e78ac21SJeffrey Hsu * firstlen (off - hlen) must be aligned on an 7381e78ac21SJeffrey Hsu * 8-byte boundary 7391e78ac21SJeffrey Hsu */ 7401e78ac21SJeffrey Hsu if (off < hlen) 7411e78ac21SJeffrey Hsu goto smart_frag_failure; 7421e78ac21SJeffrey Hsu off = ((off - hlen) & ~7) + hlen; 7431e78ac21SJeffrey Hsu newlen = (~PAGE_MASK) & mtu; 7441e78ac21SJeffrey Hsu if ((newlen + sizeof (struct ip)) > mtu) { 7451e78ac21SJeffrey Hsu /* we failed, go back the default */ 7461e78ac21SJeffrey Hsu smart_frag_failure: 7471e78ac21SJeffrey Hsu newlen = len; 7481e78ac21SJeffrey Hsu off = hlen + len; 7491e78ac21SJeffrey Hsu } 7501e78ac21SJeffrey Hsu len = newlen; 7511e78ac21SJeffrey Hsu 7521e78ac21SJeffrey Hsu } else { 7531e78ac21SJeffrey Hsu off = hlen + len; 7541e78ac21SJeffrey Hsu } 7551e78ac21SJeffrey Hsu 7561e78ac21SJeffrey Hsu firstlen = off - hlen; 7571e78ac21SJeffrey Hsu mnext = &m0->m_nextpkt; /* pointer to next packet */ 7581e78ac21SJeffrey Hsu 7591e78ac21SJeffrey Hsu /* 7601e78ac21SJeffrey Hsu * Loop through length of segment after first fragment, 7611e78ac21SJeffrey Hsu * make new header and copy data of each part and link onto chain. 7621e78ac21SJeffrey Hsu * Here, m0 is the original packet, m is the fragment being created. 7631e78ac21SJeffrey Hsu * The fragments are linked off the m_nextpkt of the original 7641e78ac21SJeffrey Hsu * packet, which after processing serves as the first fragment. 7651e78ac21SJeffrey Hsu */ 76621d172a3SGleb Smirnoff for (nfrags = 1; off < ip_len; off += len, nfrags++) { 7671e78ac21SJeffrey Hsu struct ip *mhip; /* ip header on the fragment */ 7681e78ac21SJeffrey Hsu struct mbuf *m; 7691e78ac21SJeffrey Hsu int mhlen = sizeof (struct ip); 7701e78ac21SJeffrey Hsu 771dc4ad05eSGleb Smirnoff m = m_gethdr(M_NOWAIT, MT_DATA); 7720b17fba7SAndre Oppermann if (m == NULL) { 7731e78ac21SJeffrey Hsu error = ENOBUFS; 77486425c62SRobert Watson IPSTAT_INC(ips_odropped); 7751e78ac21SJeffrey Hsu goto done; 7761e78ac21SJeffrey Hsu } 777*c4c4346fSHans Petter Selasky /* 778*c4c4346fSHans Petter Selasky * Make sure the complete packet header gets copied 779*c4c4346fSHans Petter Selasky * from the originating mbuf to the newly created 780*c4c4346fSHans Petter Selasky * mbuf. This also ensures that existing firewall 781*c4c4346fSHans Petter Selasky * classification(s), VLAN tags and so on get copied 782*c4c4346fSHans Petter Selasky * to the resulting fragmented packet(s): 783*c4c4346fSHans Petter Selasky */ 784*c4c4346fSHans Petter Selasky if (m_dup_pkthdr(m, m0, M_NOWAIT) == 0) { 785*c4c4346fSHans Petter Selasky m_free(m); 786*c4c4346fSHans Petter Selasky error = ENOBUFS; 787*c4c4346fSHans Petter Selasky IPSTAT_INC(ips_odropped); 788*c4c4346fSHans Petter Selasky goto done; 789*c4c4346fSHans Petter Selasky } 7901e78ac21SJeffrey Hsu /* 7911e78ac21SJeffrey Hsu * In the first mbuf, leave room for the link header, then 7921e78ac21SJeffrey Hsu * copy the original IP header including options. The payload 7938b889dbbSBruce M Simpson * goes into an additional mbuf chain returned by m_copym(). 7941e78ac21SJeffrey Hsu */ 7951e78ac21SJeffrey Hsu m->m_data += max_linkhdr; 7961e78ac21SJeffrey Hsu mhip = mtod(m, struct ip *); 7971e78ac21SJeffrey Hsu *mhip = *ip; 7981e78ac21SJeffrey Hsu if (hlen > sizeof (struct ip)) { 7991e78ac21SJeffrey Hsu mhlen = ip_optcopy(ip, mhip) + sizeof (struct ip); 8001e78ac21SJeffrey Hsu mhip->ip_v = IPVERSION; 8011e78ac21SJeffrey Hsu mhip->ip_hl = mhlen >> 2; 8021e78ac21SJeffrey Hsu } 8031e78ac21SJeffrey Hsu m->m_len = mhlen; 80421d172a3SGleb Smirnoff /* XXX do we need to add ip_off below ? */ 80521d172a3SGleb Smirnoff mhip->ip_off = ((off - hlen) >> 3) + ip_off; 806fb86dfcdSAndre Oppermann if (off + len >= ip_len) 80721d172a3SGleb Smirnoff len = ip_len - off; 808fb86dfcdSAndre Oppermann else 8091e78ac21SJeffrey Hsu mhip->ip_off |= IP_MF; 8101e78ac21SJeffrey Hsu mhip->ip_len = htons((u_short)(len + mhlen)); 811eb1b1807SGleb Smirnoff m->m_next = m_copym(m0, off, len, M_NOWAIT); 8120b17fba7SAndre Oppermann if (m->m_next == NULL) { /* copy failed */ 8131e78ac21SJeffrey Hsu m_free(m); 8141e78ac21SJeffrey Hsu error = ENOBUFS; /* ??? */ 81586425c62SRobert Watson IPSTAT_INC(ips_odropped); 8161e78ac21SJeffrey Hsu goto done; 8171e78ac21SJeffrey Hsu } 8181e78ac21SJeffrey Hsu m->m_pkthdr.len = mhlen + len; 8191e78ac21SJeffrey Hsu #ifdef MAC 82030d239bcSRobert Watson mac_netinet_fragment(m0, m); 8211e78ac21SJeffrey Hsu #endif 8221e78ac21SJeffrey Hsu mhip->ip_off = htons(mhip->ip_off); 8231e78ac21SJeffrey Hsu mhip->ip_sum = 0; 824078468edSGleb Smirnoff if (m->m_pkthdr.csum_flags & CSUM_IP & ~if_hwassist_flags) { 8251e78ac21SJeffrey Hsu mhip->ip_sum = in_cksum(m, mhlen); 826078468edSGleb Smirnoff m->m_pkthdr.csum_flags &= ~CSUM_IP; 827078468edSGleb Smirnoff } 8281e78ac21SJeffrey Hsu *mnext = m; 8291e78ac21SJeffrey Hsu mnext = &m->m_nextpkt; 8301e78ac21SJeffrey Hsu } 83186425c62SRobert Watson IPSTAT_ADD(ips_ofragments, nfrags); 8321e78ac21SJeffrey Hsu 8331e78ac21SJeffrey Hsu /* 8341e78ac21SJeffrey Hsu * Update first fragment by trimming what's been copied out 8351e78ac21SJeffrey Hsu * and updating header. 8361e78ac21SJeffrey Hsu */ 83721d172a3SGleb Smirnoff m_adj(m0, hlen + firstlen - ip_len); 8381e78ac21SJeffrey Hsu m0->m_pkthdr.len = hlen + firstlen; 8391e78ac21SJeffrey Hsu ip->ip_len = htons((u_short)m0->m_pkthdr.len); 84021d172a3SGleb Smirnoff ip->ip_off = htons(ip_off | IP_MF); 8411e78ac21SJeffrey Hsu ip->ip_sum = 0; 842078468edSGleb Smirnoff if (m0->m_pkthdr.csum_flags & CSUM_IP & ~if_hwassist_flags) { 8431e78ac21SJeffrey Hsu ip->ip_sum = in_cksum(m0, hlen); 844078468edSGleb Smirnoff m0->m_pkthdr.csum_flags &= ~CSUM_IP; 845078468edSGleb Smirnoff } 8461e78ac21SJeffrey Hsu 8471e78ac21SJeffrey Hsu done: 8481e78ac21SJeffrey Hsu *m_frag = m0; 8491e78ac21SJeffrey Hsu return error; 8501e78ac21SJeffrey Hsu } 8511e78ac21SJeffrey Hsu 8521c238475SJonathan Lemon void 853db4f9cc7SJonathan Lemon in_delayed_cksum(struct mbuf *m) 854db4f9cc7SJonathan Lemon { 855db4f9cc7SJonathan Lemon struct ip *ip; 85623e9c6dcSGleb Smirnoff uint16_t csum, offset, ip_len; 857db4f9cc7SJonathan Lemon 858db4f9cc7SJonathan Lemon ip = mtod(m, struct ip *); 85953be11f6SPoul-Henning Kamp offset = ip->ip_hl << 2 ; 86023e9c6dcSGleb Smirnoff ip_len = ntohs(ip->ip_len); 86123e9c6dcSGleb Smirnoff csum = in_cksum_skip(m, ip_len, offset); 862206a3274SRuslan Ermilov if (m->m_pkthdr.csum_flags & CSUM_UDP && csum == 0) 863206a3274SRuslan Ermilov csum = 0xffff; 864db4f9cc7SJonathan Lemon offset += m->m_pkthdr.csum_data; /* checksum offset */ 865db4f9cc7SJonathan Lemon 8668e1d0a56SMichael Tuexen /* find the mbuf in the chain where the checksum starts*/ 8678e1d0a56SMichael Tuexen while ((m != NULL) && (offset >= m->m_len)) { 8688e1d0a56SMichael Tuexen offset -= m->m_len; 8698e1d0a56SMichael Tuexen m = m->m_next; 8708e1d0a56SMichael Tuexen } 87126461454SMichael Tuexen KASSERT(m != NULL, ("in_delayed_cksum: checksum outside mbuf chain.")); 87226461454SMichael Tuexen KASSERT(offset + sizeof(u_short) <= m->m_len, ("in_delayed_cksum: checksum split between mbufs.")); 873db4f9cc7SJonathan Lemon *(u_short *)(m->m_data + offset) = csum; 874db4f9cc7SJonathan Lemon } 875db4f9cc7SJonathan Lemon 876df8bae1dSRodney W. Grimes /* 877df8bae1dSRodney W. Grimes * IP socket option processing. 878df8bae1dSRodney W. Grimes */ 879df8bae1dSRodney W. Grimes int 880f2565d68SRobert Watson ip_ctloutput(struct socket *so, struct sockopt *sopt) 881df8bae1dSRodney W. Grimes { 882cfe8b629SGarrett Wollman struct inpcb *inp = sotoinpcb(so); 883cfe8b629SGarrett Wollman int error, optval; 884dc847eb6SAdrian Chadd #ifdef RSS 885dc847eb6SAdrian Chadd uint32_t rss_bucket; 886dc847eb6SAdrian Chadd int retval; 887dc847eb6SAdrian Chadd #endif 888df8bae1dSRodney W. Grimes 889cfe8b629SGarrett Wollman error = optval = 0; 890cfe8b629SGarrett Wollman if (sopt->sopt_level != IPPROTO_IP) { 891fc06cd42SMikolaj Golub error = EINVAL; 892fc06cd42SMikolaj Golub 893fc06cd42SMikolaj Golub if (sopt->sopt_level == SOL_SOCKET && 894fc06cd42SMikolaj Golub sopt->sopt_dir == SOPT_SET) { 895fc06cd42SMikolaj Golub switch (sopt->sopt_name) { 896fc06cd42SMikolaj Golub case SO_REUSEADDR: 897fc06cd42SMikolaj Golub INP_WLOCK(inp); 898efdf104bSMikolaj Golub if ((so->so_options & SO_REUSEADDR) != 0) 899efdf104bSMikolaj Golub inp->inp_flags2 |= INP_REUSEADDR; 900fc06cd42SMikolaj Golub else 901efdf104bSMikolaj Golub inp->inp_flags2 &= ~INP_REUSEADDR; 902fc06cd42SMikolaj Golub INP_WUNLOCK(inp); 903fc06cd42SMikolaj Golub error = 0; 904fc06cd42SMikolaj Golub break; 905fc06cd42SMikolaj Golub case SO_REUSEPORT: 906fc06cd42SMikolaj Golub INP_WLOCK(inp); 907fc06cd42SMikolaj Golub if ((so->so_options & SO_REUSEPORT) != 0) 908fc06cd42SMikolaj Golub inp->inp_flags2 |= INP_REUSEPORT; 909fc06cd42SMikolaj Golub else 910fc06cd42SMikolaj Golub inp->inp_flags2 &= ~INP_REUSEPORT; 911fc06cd42SMikolaj Golub INP_WUNLOCK(inp); 912fc06cd42SMikolaj Golub error = 0; 913fc06cd42SMikolaj Golub break; 914fc06cd42SMikolaj Golub case SO_SETFIB: 915fc06cd42SMikolaj Golub INP_WLOCK(inp); 916fc06cd42SMikolaj Golub inp->inp_inc.inc_fibnum = so->so_fibnum; 917fc06cd42SMikolaj Golub INP_WUNLOCK(inp); 918fc06cd42SMikolaj Golub error = 0; 919fc06cd42SMikolaj Golub break; 920fc06cd42SMikolaj Golub default: 921fc06cd42SMikolaj Golub break; 922fc06cd42SMikolaj Golub } 923fc06cd42SMikolaj Golub } 924fc06cd42SMikolaj Golub return (error); 925cfe8b629SGarrett Wollman } 926df8bae1dSRodney W. Grimes 927cfe8b629SGarrett Wollman switch (sopt->sopt_dir) { 928cfe8b629SGarrett Wollman case SOPT_SET: 929cfe8b629SGarrett Wollman switch (sopt->sopt_name) { 930df8bae1dSRodney W. Grimes case IP_OPTIONS: 931df8bae1dSRodney W. Grimes #ifdef notyet 932df8bae1dSRodney W. Grimes case IP_RETOPTS: 933df8bae1dSRodney W. Grimes #endif 934cfe8b629SGarrett Wollman { 935cfe8b629SGarrett Wollman struct mbuf *m; 936cfe8b629SGarrett Wollman if (sopt->sopt_valsize > MLEN) { 937cfe8b629SGarrett Wollman error = EMSGSIZE; 938cfe8b629SGarrett Wollman break; 939cfe8b629SGarrett Wollman } 940dc4ad05eSGleb Smirnoff m = m_get(sopt->sopt_td ? M_WAITOK : M_NOWAIT, MT_DATA); 9410b17fba7SAndre Oppermann if (m == NULL) { 942cfe8b629SGarrett Wollman error = ENOBUFS; 943cfe8b629SGarrett Wollman break; 944cfe8b629SGarrett Wollman } 945cfe8b629SGarrett Wollman m->m_len = sopt->sopt_valsize; 946cfe8b629SGarrett Wollman error = sooptcopyin(sopt, mtod(m, char *), m->m_len, 947cfe8b629SGarrett Wollman m->m_len); 948635354c4SMaxim Konovalov if (error) { 949635354c4SMaxim Konovalov m_free(m); 950635354c4SMaxim Konovalov break; 951635354c4SMaxim Konovalov } 9528501a69cSRobert Watson INP_WLOCK(inp); 953993d9505SRobert Watson error = ip_pcbopts(inp, sopt->sopt_name, m); 9548501a69cSRobert Watson INP_WUNLOCK(inp); 955993d9505SRobert Watson return (error); 956cfe8b629SGarrett Wollman } 957df8bae1dSRodney W. Grimes 958f44270e7SPawel Jakub Dawidek case IP_BINDANY: 959f44270e7SPawel Jakub Dawidek if (sopt->sopt_td != NULL) { 960f44270e7SPawel Jakub Dawidek error = priv_check(sopt->sopt_td, 961f44270e7SPawel Jakub Dawidek PRIV_NETINET_BINDANY); 962f44270e7SPawel Jakub Dawidek if (error) 963be9347e3SAdrian Chadd break; 964be9347e3SAdrian Chadd } 965cef27294SAdrian Chadd /* FALLTHROUGH */ 9660a100a6fSAdrian Chadd case IP_BINDMULTI: 9670a100a6fSAdrian Chadd #ifdef RSS 9680a100a6fSAdrian Chadd case IP_RSS_LISTEN_BUCKET: 9690a100a6fSAdrian Chadd #endif 970df8bae1dSRodney W. Grimes case IP_TOS: 971df8bae1dSRodney W. Grimes case IP_TTL: 972936cd18dSAndre Oppermann case IP_MINTTL: 973df8bae1dSRodney W. Grimes case IP_RECVOPTS: 974df8bae1dSRodney W. Grimes case IP_RECVRETOPTS: 975df8bae1dSRodney W. Grimes case IP_RECVDSTADDR: 9764957466bSMatthew N. Dodd case IP_RECVTTL: 97782c23ebaSBill Fenner case IP_RECVIF: 9788afa2304SBruce M Simpson case IP_ONESBCAST: 979b2828ad2SAndre Oppermann case IP_DONTFRAG: 9803cca425bSMichael Tuexen case IP_RECVTOS: 9819d3ddf43SAdrian Chadd case IP_RECVFLOWID: 9829d3ddf43SAdrian Chadd #ifdef RSS 9839d3ddf43SAdrian Chadd case IP_RECVRSSBUCKETID: 9849d3ddf43SAdrian Chadd #endif 985cfe8b629SGarrett Wollman error = sooptcopyin(sopt, &optval, sizeof optval, 986cfe8b629SGarrett Wollman sizeof optval); 987cfe8b629SGarrett Wollman if (error) 988cfe8b629SGarrett Wollman break; 989df8bae1dSRodney W. Grimes 990cfe8b629SGarrett Wollman switch (sopt->sopt_name) { 991df8bae1dSRodney W. Grimes case IP_TOS: 992ca98b82cSDavid Greenman inp->inp_ip_tos = optval; 993df8bae1dSRodney W. Grimes break; 994df8bae1dSRodney W. Grimes 995df8bae1dSRodney W. Grimes case IP_TTL: 996ca98b82cSDavid Greenman inp->inp_ip_ttl = optval; 997df8bae1dSRodney W. Grimes break; 998936cd18dSAndre Oppermann 999936cd18dSAndre Oppermann case IP_MINTTL: 1000a603c811SRobert Watson if (optval >= 0 && optval <= MAXTTL) 1001936cd18dSAndre Oppermann inp->inp_ip_minttl = optval; 1002936cd18dSAndre Oppermann else 1003936cd18dSAndre Oppermann error = EINVAL; 1004936cd18dSAndre Oppermann break; 1005936cd18dSAndre Oppermann 1006a138d217SRobert Watson #define OPTSET(bit) do { \ 10078501a69cSRobert Watson INP_WLOCK(inp); \ 1008df8bae1dSRodney W. Grimes if (optval) \ 1009df8bae1dSRodney W. Grimes inp->inp_flags |= bit; \ 1010df8bae1dSRodney W. Grimes else \ 1011a138d217SRobert Watson inp->inp_flags &= ~bit; \ 10128501a69cSRobert Watson INP_WUNLOCK(inp); \ 1013a138d217SRobert Watson } while (0) 1014df8bae1dSRodney W. Grimes 10150a100a6fSAdrian Chadd #define OPTSET2(bit, val) do { \ 10160a100a6fSAdrian Chadd INP_WLOCK(inp); \ 10170a100a6fSAdrian Chadd if (val) \ 10180a100a6fSAdrian Chadd inp->inp_flags2 |= bit; \ 10190a100a6fSAdrian Chadd else \ 10200a100a6fSAdrian Chadd inp->inp_flags2 &= ~bit; \ 10210a100a6fSAdrian Chadd INP_WUNLOCK(inp); \ 10220a100a6fSAdrian Chadd } while (0) 10230a100a6fSAdrian Chadd 1024df8bae1dSRodney W. Grimes case IP_RECVOPTS: 1025df8bae1dSRodney W. Grimes OPTSET(INP_RECVOPTS); 1026df8bae1dSRodney W. Grimes break; 1027df8bae1dSRodney W. Grimes 1028df8bae1dSRodney W. Grimes case IP_RECVRETOPTS: 1029df8bae1dSRodney W. Grimes OPTSET(INP_RECVRETOPTS); 1030df8bae1dSRodney W. Grimes break; 1031df8bae1dSRodney W. Grimes 1032df8bae1dSRodney W. Grimes case IP_RECVDSTADDR: 1033df8bae1dSRodney W. Grimes OPTSET(INP_RECVDSTADDR); 1034df8bae1dSRodney W. Grimes break; 103582c23ebaSBill Fenner 10364957466bSMatthew N. Dodd case IP_RECVTTL: 10374957466bSMatthew N. Dodd OPTSET(INP_RECVTTL); 10384957466bSMatthew N. Dodd break; 10394957466bSMatthew N. Dodd 104082c23ebaSBill Fenner case IP_RECVIF: 104182c23ebaSBill Fenner OPTSET(INP_RECVIF); 104282c23ebaSBill Fenner break; 10436a800098SYoshinobu Inoue 10448afa2304SBruce M Simpson case IP_ONESBCAST: 10458afa2304SBruce M Simpson OPTSET(INP_ONESBCAST); 10468afa2304SBruce M Simpson break; 1047b2828ad2SAndre Oppermann case IP_DONTFRAG: 1048b2828ad2SAndre Oppermann OPTSET(INP_DONTFRAG); 1049b2828ad2SAndre Oppermann break; 1050f44270e7SPawel Jakub Dawidek case IP_BINDANY: 1051f44270e7SPawel Jakub Dawidek OPTSET(INP_BINDANY); 1052be9347e3SAdrian Chadd break; 10533cca425bSMichael Tuexen case IP_RECVTOS: 10543cca425bSMichael Tuexen OPTSET(INP_RECVTOS); 10553cca425bSMichael Tuexen break; 10560a100a6fSAdrian Chadd case IP_BINDMULTI: 10570a100a6fSAdrian Chadd OPTSET2(INP_BINDMULTI, optval); 10580a100a6fSAdrian Chadd break; 10599d3ddf43SAdrian Chadd case IP_RECVFLOWID: 10609d3ddf43SAdrian Chadd OPTSET2(INP_RECVFLOWID, optval); 10619d3ddf43SAdrian Chadd break; 10620a100a6fSAdrian Chadd #ifdef RSS 10630a100a6fSAdrian Chadd case IP_RSS_LISTEN_BUCKET: 10640a100a6fSAdrian Chadd if ((optval >= 0) && 10650a100a6fSAdrian Chadd (optval < rss_getnumbuckets())) { 10660a100a6fSAdrian Chadd inp->inp_rss_listen_bucket = optval; 10670a100a6fSAdrian Chadd OPTSET2(INP_RSS_BUCKET_SET, 1); 10680a100a6fSAdrian Chadd } else { 10690a100a6fSAdrian Chadd error = EINVAL; 10700a100a6fSAdrian Chadd } 10710a100a6fSAdrian Chadd break; 10729d3ddf43SAdrian Chadd case IP_RECVRSSBUCKETID: 10739d3ddf43SAdrian Chadd OPTSET2(INP_RECVRSSBUCKETID, optval); 10749d3ddf43SAdrian Chadd break; 10750a100a6fSAdrian Chadd #endif 1076df8bae1dSRodney W. Grimes } 1077df8bae1dSRodney W. Grimes break; 1078df8bae1dSRodney W. Grimes #undef OPTSET 10790a100a6fSAdrian Chadd #undef OPTSET2 1080df8bae1dSRodney W. Grimes 108171498f30SBruce M Simpson /* 108271498f30SBruce M Simpson * Multicast socket options are processed by the in_mcast 108371498f30SBruce M Simpson * module. 108471498f30SBruce M Simpson */ 1085df8bae1dSRodney W. Grimes case IP_MULTICAST_IF: 1086f0068c4aSGarrett Wollman case IP_MULTICAST_VIF: 1087df8bae1dSRodney W. Grimes case IP_MULTICAST_TTL: 1088df8bae1dSRodney W. Grimes case IP_MULTICAST_LOOP: 1089df8bae1dSRodney W. Grimes case IP_ADD_MEMBERSHIP: 1090df8bae1dSRodney W. Grimes case IP_DROP_MEMBERSHIP: 109171498f30SBruce M Simpson case IP_ADD_SOURCE_MEMBERSHIP: 109271498f30SBruce M Simpson case IP_DROP_SOURCE_MEMBERSHIP: 109371498f30SBruce M Simpson case IP_BLOCK_SOURCE: 109471498f30SBruce M Simpson case IP_UNBLOCK_SOURCE: 109571498f30SBruce M Simpson case IP_MSFILTER: 109671498f30SBruce M Simpson case MCAST_JOIN_GROUP: 109771498f30SBruce M Simpson case MCAST_LEAVE_GROUP: 109871498f30SBruce M Simpson case MCAST_JOIN_SOURCE_GROUP: 109971498f30SBruce M Simpson case MCAST_LEAVE_SOURCE_GROUP: 110071498f30SBruce M Simpson case MCAST_BLOCK_SOURCE: 110171498f30SBruce M Simpson case MCAST_UNBLOCK_SOURCE: 110271498f30SBruce M Simpson error = inp_setmoptions(inp, sopt); 1103df8bae1dSRodney W. Grimes break; 1104df8bae1dSRodney W. Grimes 110533b3ac06SPeter Wemm case IP_PORTRANGE: 1106cfe8b629SGarrett Wollman error = sooptcopyin(sopt, &optval, sizeof optval, 1107cfe8b629SGarrett Wollman sizeof optval); 1108cfe8b629SGarrett Wollman if (error) 1109cfe8b629SGarrett Wollman break; 111033b3ac06SPeter Wemm 11118501a69cSRobert Watson INP_WLOCK(inp); 111233b3ac06SPeter Wemm switch (optval) { 111333b3ac06SPeter Wemm case IP_PORTRANGE_DEFAULT: 111433b3ac06SPeter Wemm inp->inp_flags &= ~(INP_LOWPORT); 111533b3ac06SPeter Wemm inp->inp_flags &= ~(INP_HIGHPORT); 111633b3ac06SPeter Wemm break; 111733b3ac06SPeter Wemm 111833b3ac06SPeter Wemm case IP_PORTRANGE_HIGH: 111933b3ac06SPeter Wemm inp->inp_flags &= ~(INP_LOWPORT); 112033b3ac06SPeter Wemm inp->inp_flags |= INP_HIGHPORT; 112133b3ac06SPeter Wemm break; 112233b3ac06SPeter Wemm 112333b3ac06SPeter Wemm case IP_PORTRANGE_LOW: 112433b3ac06SPeter Wemm inp->inp_flags &= ~(INP_HIGHPORT); 112533b3ac06SPeter Wemm inp->inp_flags |= INP_LOWPORT; 112633b3ac06SPeter Wemm break; 112733b3ac06SPeter Wemm 112833b3ac06SPeter Wemm default: 112933b3ac06SPeter Wemm error = EINVAL; 113033b3ac06SPeter Wemm break; 113133b3ac06SPeter Wemm } 11328501a69cSRobert Watson INP_WUNLOCK(inp); 1133ce8c72b1SPeter Wemm break; 113433b3ac06SPeter Wemm 1135b2630c29SGeorge V. Neville-Neil #ifdef IPSEC 11366a800098SYoshinobu Inoue case IP_IPSEC_POLICY: 11376a800098SYoshinobu Inoue { 11386a800098SYoshinobu Inoue caddr_t req; 11396a800098SYoshinobu Inoue struct mbuf *m; 11406a800098SYoshinobu Inoue 11416a800098SYoshinobu Inoue if ((error = soopt_getm(sopt, &m)) != 0) /* XXX */ 11426a800098SYoshinobu Inoue break; 11436a800098SYoshinobu Inoue if ((error = soopt_mcopyin(sopt, m)) != 0) /* XXX */ 11446a800098SYoshinobu Inoue break; 11456a800098SYoshinobu Inoue req = mtod(m, caddr_t); 114697aa4a51SBjoern A. Zeeb error = ipsec_set_policy(inp, sopt->sopt_name, req, 1147c26fe973SBjoern A. Zeeb m->m_len, (sopt->sopt_td != NULL) ? 1148c26fe973SBjoern A. Zeeb sopt->sopt_td->td_ucred : NULL); 11496a800098SYoshinobu Inoue m_freem(m); 11506a800098SYoshinobu Inoue break; 11516a800098SYoshinobu Inoue } 1152b2630c29SGeorge V. Neville-Neil #endif /* IPSEC */ 11536a800098SYoshinobu Inoue 1154df8bae1dSRodney W. Grimes default: 1155df8bae1dSRodney W. Grimes error = ENOPROTOOPT; 1156df8bae1dSRodney W. Grimes break; 1157df8bae1dSRodney W. Grimes } 1158df8bae1dSRodney W. Grimes break; 1159df8bae1dSRodney W. Grimes 1160cfe8b629SGarrett Wollman case SOPT_GET: 1161cfe8b629SGarrett Wollman switch (sopt->sopt_name) { 1162df8bae1dSRodney W. Grimes case IP_OPTIONS: 1163df8bae1dSRodney W. Grimes case IP_RETOPTS: 1164cfe8b629SGarrett Wollman if (inp->inp_options) 1165cfe8b629SGarrett Wollman error = sooptcopyout(sopt, 1166cfe8b629SGarrett Wollman mtod(inp->inp_options, 1167cfe8b629SGarrett Wollman char *), 1168cfe8b629SGarrett Wollman inp->inp_options->m_len); 1169cfe8b629SGarrett Wollman else 1170cfe8b629SGarrett Wollman sopt->sopt_valsize = 0; 1171df8bae1dSRodney W. Grimes break; 1172df8bae1dSRodney W. Grimes 1173df8bae1dSRodney W. Grimes case IP_TOS: 1174df8bae1dSRodney W. Grimes case IP_TTL: 1175936cd18dSAndre Oppermann case IP_MINTTL: 1176df8bae1dSRodney W. Grimes case IP_RECVOPTS: 1177df8bae1dSRodney W. Grimes case IP_RECVRETOPTS: 1178df8bae1dSRodney W. Grimes case IP_RECVDSTADDR: 11794957466bSMatthew N. Dodd case IP_RECVTTL: 118082c23ebaSBill Fenner case IP_RECVIF: 1181cfe8b629SGarrett Wollman case IP_PORTRANGE: 11828afa2304SBruce M Simpson case IP_ONESBCAST: 1183b2828ad2SAndre Oppermann case IP_DONTFRAG: 11845f6bf451SAttilio Rao case IP_BINDANY: 11853cca425bSMichael Tuexen case IP_RECVTOS: 11860a100a6fSAdrian Chadd case IP_BINDMULTI: 11879c423972SAdrian Chadd case IP_FLOWID: 11889c423972SAdrian Chadd case IP_FLOWTYPE: 11899d3ddf43SAdrian Chadd case IP_RECVFLOWID: 11900a100a6fSAdrian Chadd #ifdef RSS 11910a100a6fSAdrian Chadd case IP_RSSBUCKETID: 11929d3ddf43SAdrian Chadd case IP_RECVRSSBUCKETID: 11930a100a6fSAdrian Chadd #endif 1194cfe8b629SGarrett Wollman switch (sopt->sopt_name) { 1195df8bae1dSRodney W. Grimes 1196df8bae1dSRodney W. Grimes case IP_TOS: 1197ca98b82cSDavid Greenman optval = inp->inp_ip_tos; 1198df8bae1dSRodney W. Grimes break; 1199df8bae1dSRodney W. Grimes 1200df8bae1dSRodney W. Grimes case IP_TTL: 1201ca98b82cSDavid Greenman optval = inp->inp_ip_ttl; 1202df8bae1dSRodney W. Grimes break; 1203df8bae1dSRodney W. Grimes 1204936cd18dSAndre Oppermann case IP_MINTTL: 1205936cd18dSAndre Oppermann optval = inp->inp_ip_minttl; 1206936cd18dSAndre Oppermann break; 1207936cd18dSAndre Oppermann 1208df8bae1dSRodney W. Grimes #define OPTBIT(bit) (inp->inp_flags & bit ? 1 : 0) 12090a100a6fSAdrian Chadd #define OPTBIT2(bit) (inp->inp_flags2 & bit ? 1 : 0) 1210df8bae1dSRodney W. Grimes 1211df8bae1dSRodney W. Grimes case IP_RECVOPTS: 1212df8bae1dSRodney W. Grimes optval = OPTBIT(INP_RECVOPTS); 1213df8bae1dSRodney W. Grimes break; 1214df8bae1dSRodney W. Grimes 1215df8bae1dSRodney W. Grimes case IP_RECVRETOPTS: 1216df8bae1dSRodney W. Grimes optval = OPTBIT(INP_RECVRETOPTS); 1217df8bae1dSRodney W. Grimes break; 1218df8bae1dSRodney W. Grimes 1219df8bae1dSRodney W. Grimes case IP_RECVDSTADDR: 1220df8bae1dSRodney W. Grimes optval = OPTBIT(INP_RECVDSTADDR); 1221df8bae1dSRodney W. Grimes break; 122282c23ebaSBill Fenner 12234957466bSMatthew N. Dodd case IP_RECVTTL: 12244957466bSMatthew N. Dodd optval = OPTBIT(INP_RECVTTL); 12254957466bSMatthew N. Dodd break; 12264957466bSMatthew N. Dodd 122782c23ebaSBill Fenner case IP_RECVIF: 122882c23ebaSBill Fenner optval = OPTBIT(INP_RECVIF); 122982c23ebaSBill Fenner break; 1230cfe8b629SGarrett Wollman 1231cfe8b629SGarrett Wollman case IP_PORTRANGE: 1232cfe8b629SGarrett Wollman if (inp->inp_flags & INP_HIGHPORT) 1233cfe8b629SGarrett Wollman optval = IP_PORTRANGE_HIGH; 1234cfe8b629SGarrett Wollman else if (inp->inp_flags & INP_LOWPORT) 1235cfe8b629SGarrett Wollman optval = IP_PORTRANGE_LOW; 1236cfe8b629SGarrett Wollman else 1237cfe8b629SGarrett Wollman optval = 0; 1238cfe8b629SGarrett Wollman break; 12396a800098SYoshinobu Inoue 12408afa2304SBruce M Simpson case IP_ONESBCAST: 12418afa2304SBruce M Simpson optval = OPTBIT(INP_ONESBCAST); 12428afa2304SBruce M Simpson break; 1243b2828ad2SAndre Oppermann case IP_DONTFRAG: 1244b2828ad2SAndre Oppermann optval = OPTBIT(INP_DONTFRAG); 1245b2828ad2SAndre Oppermann break; 12465f6bf451SAttilio Rao case IP_BINDANY: 12475f6bf451SAttilio Rao optval = OPTBIT(INP_BINDANY); 12485f6bf451SAttilio Rao break; 12493cca425bSMichael Tuexen case IP_RECVTOS: 12503cca425bSMichael Tuexen optval = OPTBIT(INP_RECVTOS); 12513cca425bSMichael Tuexen break; 12529c423972SAdrian Chadd case IP_FLOWID: 12539c423972SAdrian Chadd optval = inp->inp_flowid; 12549c423972SAdrian Chadd break; 12559c423972SAdrian Chadd case IP_FLOWTYPE: 12569c423972SAdrian Chadd optval = inp->inp_flowtype; 12579c423972SAdrian Chadd break; 12589d3ddf43SAdrian Chadd case IP_RECVFLOWID: 12599d3ddf43SAdrian Chadd optval = OPTBIT2(INP_RECVFLOWID); 12609d3ddf43SAdrian Chadd break; 12619c423972SAdrian Chadd #ifdef RSS 12627847796aSAdrian Chadd case IP_RSSBUCKETID: 12637847796aSAdrian Chadd retval = rss_hash2bucket(inp->inp_flowid, 12647847796aSAdrian Chadd inp->inp_flowtype, 12657847796aSAdrian Chadd &rss_bucket); 12667847796aSAdrian Chadd if (retval == 0) 12677847796aSAdrian Chadd optval = rss_bucket; 12687847796aSAdrian Chadd else 12697847796aSAdrian Chadd error = EINVAL; 12709c423972SAdrian Chadd break; 12719d3ddf43SAdrian Chadd case IP_RECVRSSBUCKETID: 12729d3ddf43SAdrian Chadd optval = OPTBIT2(INP_RECVRSSBUCKETID); 12739d3ddf43SAdrian Chadd break; 12749c423972SAdrian Chadd #endif 12750a100a6fSAdrian Chadd case IP_BINDMULTI: 12760a100a6fSAdrian Chadd optval = OPTBIT2(INP_BINDMULTI); 12770a100a6fSAdrian Chadd break; 1278df8bae1dSRodney W. Grimes } 1279cfe8b629SGarrett Wollman error = sooptcopyout(sopt, &optval, sizeof optval); 1280df8bae1dSRodney W. Grimes break; 1281df8bae1dSRodney W. Grimes 128271498f30SBruce M Simpson /* 128371498f30SBruce M Simpson * Multicast socket options are processed by the in_mcast 128471498f30SBruce M Simpson * module. 128571498f30SBruce M Simpson */ 1286df8bae1dSRodney W. Grimes case IP_MULTICAST_IF: 1287f0068c4aSGarrett Wollman case IP_MULTICAST_VIF: 1288df8bae1dSRodney W. Grimes case IP_MULTICAST_TTL: 1289df8bae1dSRodney W. Grimes case IP_MULTICAST_LOOP: 129071498f30SBruce M Simpson case IP_MSFILTER: 129171498f30SBruce M Simpson error = inp_getmoptions(inp, sopt); 129233b3ac06SPeter Wemm break; 129333b3ac06SPeter Wemm 1294b2630c29SGeorge V. Neville-Neil #ifdef IPSEC 12956a800098SYoshinobu Inoue case IP_IPSEC_POLICY: 12966a800098SYoshinobu Inoue { 1297f63e7634SYoshinobu Inoue struct mbuf *m = NULL; 12986a800098SYoshinobu Inoue caddr_t req = NULL; 1299686cdd19SJun-ichiro itojun Hagino size_t len = 0; 13006a800098SYoshinobu Inoue 1301686cdd19SJun-ichiro itojun Hagino if (m != 0) { 13026a800098SYoshinobu Inoue req = mtod(m, caddr_t); 1303686cdd19SJun-ichiro itojun Hagino len = m->m_len; 1304686cdd19SJun-ichiro itojun Hagino } 130597aa4a51SBjoern A. Zeeb error = ipsec_get_policy(sotoinpcb(so), req, len, &m); 13066a800098SYoshinobu Inoue if (error == 0) 13076a800098SYoshinobu Inoue error = soopt_mcopyout(sopt, m); /* XXX */ 1308f63e7634SYoshinobu Inoue if (error == 0) 13096a800098SYoshinobu Inoue m_freem(m); 13106a800098SYoshinobu Inoue break; 13116a800098SYoshinobu Inoue } 1312b2630c29SGeorge V. Neville-Neil #endif /* IPSEC */ 13136a800098SYoshinobu Inoue 1314df8bae1dSRodney W. Grimes default: 1315df8bae1dSRodney W. Grimes error = ENOPROTOOPT; 1316df8bae1dSRodney W. Grimes break; 1317df8bae1dSRodney W. Grimes } 1318df8bae1dSRodney W. Grimes break; 1319df8bae1dSRodney W. Grimes } 1320df8bae1dSRodney W. Grimes return (error); 1321df8bae1dSRodney W. Grimes } 1322df8bae1dSRodney W. Grimes 1323df8bae1dSRodney W. Grimes /* 1324df8bae1dSRodney W. Grimes * Routine called from ip_output() to loop back a copy of an IP multicast 1325df8bae1dSRodney W. Grimes * packet to the input queue of a specified interface. Note that this 1326df8bae1dSRodney W. Grimes * calls the output routine of the loopback "driver", but with an interface 1327f5fea3ddSPaul Traina * pointer that might NOT be a loopback interface -- evil, but easier than 1328f5fea3ddSPaul Traina * replicating that code here. 1329df8bae1dSRodney W. Grimes */ 1330df8bae1dSRodney W. Grimes static void 1331f2565d68SRobert Watson ip_mloopback(struct ifnet *ifp, struct mbuf *m, struct sockaddr_in *dst, 1332f2565d68SRobert Watson int hlen) 1333df8bae1dSRodney W. Grimes { 1334b375c9ecSLuigi Rizzo register struct ip *ip; 1335df8bae1dSRodney W. Grimes struct mbuf *copym; 1336df8bae1dSRodney W. Grimes 1337e4762f75SGeorge V. Neville-Neil /* 1338e4762f75SGeorge V. Neville-Neil * Make a deep copy of the packet because we're going to 1339e4762f75SGeorge V. Neville-Neil * modify the pack in order to generate checksums. 1340e4762f75SGeorge V. Neville-Neil */ 1341eb1b1807SGleb Smirnoff copym = m_dup(m, M_NOWAIT); 1342f0cace5dSRobert Watson if (copym != NULL && (!M_WRITABLE(copym) || copym->m_len < hlen)) 134386b1d6d2SBill Fenner copym = m_pullup(copym, hlen); 1344df8bae1dSRodney W. Grimes if (copym != NULL) { 1345390cdc6aSRuslan Ermilov /* If needed, compute the checksum and mark it as valid. */ 1346390cdc6aSRuslan Ermilov if (copym->m_pkthdr.csum_flags & CSUM_DELAY_DATA) { 1347390cdc6aSRuslan Ermilov in_delayed_cksum(copym); 1348390cdc6aSRuslan Ermilov copym->m_pkthdr.csum_flags &= ~CSUM_DELAY_DATA; 1349390cdc6aSRuslan Ermilov copym->m_pkthdr.csum_flags |= 1350390cdc6aSRuslan Ermilov CSUM_DATA_VALID | CSUM_PSEUDO_HDR; 1351390cdc6aSRuslan Ermilov copym->m_pkthdr.csum_data = 0xffff; 1352390cdc6aSRuslan Ermilov } 1353df8bae1dSRodney W. Grimes /* 1354df8bae1dSRodney W. Grimes * We don't bother to fragment if the IP length is greater 1355df8bae1dSRodney W. Grimes * than the interface's MTU. Can this possibly matter? 1356df8bae1dSRodney W. Grimes */ 13578f134647SGleb Smirnoff ip = mtod(copym, struct ip *); 1358df8bae1dSRodney W. Grimes ip->ip_sum = 0; 135986b1d6d2SBill Fenner ip->ip_sum = in_cksum(copym, hlen); 1360201c2527SJulian Elischer #if 1 /* XXX */ 1361201c2527SJulian Elischer if (dst->sin_family != AF_INET) { 13622b8a366cSJulian Elischer printf("ip_mloopback: bad address family %d\n", 1363201c2527SJulian Elischer dst->sin_family); 1364201c2527SJulian Elischer dst->sin_family = AF_INET; 1365201c2527SJulian Elischer } 1366201c2527SJulian Elischer #endif 136706a429a3SArchie Cobbs if_simloop(ifp, copym, dst->sin_family, 0); 1368df8bae1dSRodney W. Grimes } 1369df8bae1dSRodney W. Grimes } 1370