1c398230bSWarner Losh /*- 2*51369649SPedro F. Giffuni * SPDX-License-Identifier: BSD-3-Clause 3*51369649SPedro F. Giffuni * 4df8bae1dSRodney W. Grimes * Copyright (c) 1982, 1986, 1988, 1990, 1993 5df8bae1dSRodney W. Grimes * The Regents of the University of California. All rights reserved. 6df8bae1dSRodney W. Grimes * 7df8bae1dSRodney W. Grimes * Redistribution and use in source and binary forms, with or without 8df8bae1dSRodney W. Grimes * modification, are permitted provided that the following conditions 9df8bae1dSRodney W. Grimes * are met: 10df8bae1dSRodney W. Grimes * 1. Redistributions of source code must retain the above copyright 11df8bae1dSRodney W. Grimes * notice, this list of conditions and the following disclaimer. 12df8bae1dSRodney W. Grimes * 2. Redistributions in binary form must reproduce the above copyright 13df8bae1dSRodney W. Grimes * notice, this list of conditions and the following disclaimer in the 14df8bae1dSRodney W. Grimes * documentation and/or other materials provided with the distribution. 15fbbd9655SWarner Losh * 3. Neither the name of the University nor the names of its contributors 16df8bae1dSRodney W. Grimes * may be used to endorse or promote products derived from this software 17df8bae1dSRodney W. Grimes * without specific prior written permission. 18df8bae1dSRodney W. Grimes * 19df8bae1dSRodney W. Grimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 20df8bae1dSRodney W. Grimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 21df8bae1dSRodney W. Grimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 22df8bae1dSRodney W. Grimes * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 23df8bae1dSRodney W. Grimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 24df8bae1dSRodney W. Grimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 25df8bae1dSRodney W. Grimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 26df8bae1dSRodney W. Grimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 27df8bae1dSRodney W. Grimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 28df8bae1dSRodney W. Grimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 29df8bae1dSRodney W. Grimes * SUCH DAMAGE. 30df8bae1dSRodney W. Grimes * 31df8bae1dSRodney W. Grimes * @(#)ip_output.c 8.3 (Berkeley) 1/21/94 32df8bae1dSRodney W. Grimes */ 33df8bae1dSRodney W. Grimes 344b421e2dSMike Silbersack #include <sys/cdefs.h> 354b421e2dSMike Silbersack __FBSDID("$FreeBSD$"); 364b421e2dSMike Silbersack 375d6d7e75SGleb Smirnoff #include "opt_inet.h" 38f3e7afe2SHans Petter Selasky #include "opt_ratelimit.h" 396a800098SYoshinobu Inoue #include "opt_ipsec.h" 4053dcc544SMike Silbersack #include "opt_mbuf_stress_test.h" 41e440aed9SQing Li #include "opt_mpath.h" 4257f60867SMark Johnston #include "opt_route.h" 432f4afd21SRandall Stewart #include "opt_sctp.h" 449c423972SAdrian Chadd #include "opt_rss.h" 45fbd1372aSJoerg Wunsch 46df8bae1dSRodney W. Grimes #include <sys/param.h> 4726f9a767SRodney W. Grimes #include <sys/systm.h> 48f0f6d643SLuigi Rizzo #include <sys/kernel.h> 49cc0a3c8cSAndrey V. Elsukov #include <sys/lock.h> 50df8bae1dSRodney W. Grimes #include <sys/malloc.h> 51df8bae1dSRodney W. Grimes #include <sys/mbuf.h> 52acd3428bSRobert Watson #include <sys/priv.h> 53c26fe973SBjoern A. Zeeb #include <sys/proc.h> 54df8bae1dSRodney W. Grimes #include <sys/protosw.h> 55cc0a3c8cSAndrey V. Elsukov #include <sys/rmlock.h> 5657f60867SMark Johnston #include <sys/sdt.h> 57df8bae1dSRodney W. Grimes #include <sys/socket.h> 58df8bae1dSRodney W. Grimes #include <sys/socketvar.h> 599d9edc56SMike Silbersack #include <sys/sysctl.h> 60c26fe973SBjoern A. Zeeb #include <sys/ucred.h> 61df8bae1dSRodney W. Grimes 62df8bae1dSRodney W. Grimes #include <net/if.h> 6376039bc8SGleb Smirnoff #include <net/if_var.h> 643ef5e21dSQing Li #include <net/if_llatbl.h> 659b932e9eSAndre Oppermann #include <net/netisr.h> 66c21fd232SAndre Oppermann #include <net/pfil.h> 67df8bae1dSRodney W. Grimes #include <net/route.h> 68e440aed9SQing Li #ifdef RADIX_MPATH 69e440aed9SQing Li #include <net/radix_mpath.h> 70e440aed9SQing Li #endif 71b2bdc62aSAdrian Chadd #include <net/rss_config.h> 724b79449eSBjoern A. Zeeb #include <net/vnet.h> 73df8bae1dSRodney W. Grimes 74df8bae1dSRodney W. Grimes #include <netinet/in.h> 7557f60867SMark Johnston #include <netinet/in_kdtrace.h> 76df8bae1dSRodney W. Grimes #include <netinet/in_systm.h> 77df8bae1dSRodney W. Grimes #include <netinet/ip.h> 78df8bae1dSRodney W. Grimes #include <netinet/in_pcb.h> 799c423972SAdrian Chadd #include <netinet/in_rss.h> 80df8bae1dSRodney W. Grimes #include <netinet/in_var.h> 81df8bae1dSRodney W. Grimes #include <netinet/ip_var.h> 82ef39adf0SAndre Oppermann #include <netinet/ip_options.h> 832f4afd21SRandall Stewart #ifdef SCTP 842f4afd21SRandall Stewart #include <netinet/sctp.h> 852f4afd21SRandall Stewart #include <netinet/sctp_crc32.h> 862f4afd21SRandall Stewart #endif 87df8bae1dSRodney W. Grimes 88fcf59617SAndrey V. Elsukov #include <netipsec/ipsec_support.h> 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 100331dff07SAlexander V. Chernikov static void ip_mloopback(struct ifnet *, const struct mbuf *, int); 101afed1b49SDarren Reed 102afed1b49SDarren Reed 1038b889dbbSBruce M Simpson extern int in_mcast_loop; 10493e0e116SJulian Elischer extern struct protosw inetsw[]; 10593e0e116SJulian Elischer 106d9f2a782SErmal Luçi static inline int 1078f980c01SMark Johnston ip_output_pfil(struct mbuf **mp, struct ifnet *ifp, struct inpcb *inp, 108d9f2a782SErmal Luçi struct sockaddr_in *dst, int *fibnum, int *error) 109d9f2a782SErmal Luçi { 110d9f2a782SErmal Luçi struct m_tag *fwd_tag = NULL; 1118f980c01SMark Johnston struct mbuf *m; 112d9f2a782SErmal Luçi struct in_addr odst; 113d9f2a782SErmal Luçi struct ip *ip; 114d9f2a782SErmal Luçi 1158f980c01SMark Johnston m = *mp; 116d9f2a782SErmal Luçi ip = mtod(m, struct ip *); 117d9f2a782SErmal Luçi 118d9f2a782SErmal Luçi /* Run through list of hooks for output packets. */ 119d9f2a782SErmal Luçi odst.s_addr = ip->ip_dst.s_addr; 1208f980c01SMark Johnston *error = pfil_run_hooks(&V_inet_pfil_hook, mp, ifp, PFIL_OUT, inp); 1218f980c01SMark Johnston m = *mp; 122d9f2a782SErmal Luçi if ((*error) != 0 || m == NULL) 123d9f2a782SErmal Luçi return 1; /* Finished */ 124d9f2a782SErmal Luçi 125d9f2a782SErmal Luçi ip = mtod(m, struct ip *); 126d9f2a782SErmal Luçi 127d9f2a782SErmal Luçi /* See if destination IP address was changed by packet filter. */ 128d9f2a782SErmal Luçi if (odst.s_addr != ip->ip_dst.s_addr) { 129d9f2a782SErmal Luçi m->m_flags |= M_SKIP_FIREWALL; 130d9f2a782SErmal Luçi /* If destination is now ourself drop to ip_input(). */ 131d9f2a782SErmal Luçi if (in_localip(ip->ip_dst)) { 132d9f2a782SErmal Luçi m->m_flags |= M_FASTFWD_OURS; 133d9f2a782SErmal Luçi if (m->m_pkthdr.rcvif == NULL) 134d9f2a782SErmal Luçi m->m_pkthdr.rcvif = V_loif; 135d9f2a782SErmal Luçi if (m->m_pkthdr.csum_flags & CSUM_DELAY_DATA) { 136d9f2a782SErmal Luçi m->m_pkthdr.csum_flags |= 137d9f2a782SErmal Luçi CSUM_DATA_VALID | CSUM_PSEUDO_HDR; 138d9f2a782SErmal Luçi m->m_pkthdr.csum_data = 0xffff; 139d9f2a782SErmal Luçi } 140d9f2a782SErmal Luçi m->m_pkthdr.csum_flags |= 141d9f2a782SErmal Luçi CSUM_IP_CHECKED | CSUM_IP_VALID; 142d9f2a782SErmal Luçi #ifdef SCTP 143d9f2a782SErmal Luçi if (m->m_pkthdr.csum_flags & CSUM_SCTP) 144d9f2a782SErmal Luçi m->m_pkthdr.csum_flags |= CSUM_SCTP_VALID; 145d9f2a782SErmal Luçi #endif 146d9f2a782SErmal Luçi *error = netisr_queue(NETISR_IP, m); 147d9f2a782SErmal Luçi return 1; /* Finished */ 148d9f2a782SErmal Luçi } 149d9f2a782SErmal Luçi 150d9f2a782SErmal Luçi bzero(dst, sizeof(*dst)); 151d9f2a782SErmal Luçi dst->sin_family = AF_INET; 152d9f2a782SErmal Luçi dst->sin_len = sizeof(*dst); 153d9f2a782SErmal Luçi dst->sin_addr = ip->ip_dst; 154d9f2a782SErmal Luçi 155d9f2a782SErmal Luçi return -1; /* Reloop */ 156d9f2a782SErmal Luçi } 157d9f2a782SErmal Luçi /* See if fib was changed by packet filter. */ 158d9f2a782SErmal Luçi if ((*fibnum) != M_GETFIB(m)) { 159d9f2a782SErmal Luçi m->m_flags |= M_SKIP_FIREWALL; 160d9f2a782SErmal Luçi *fibnum = M_GETFIB(m); 161d9f2a782SErmal Luçi return -1; /* Reloop for FIB change */ 162d9f2a782SErmal Luçi } 163d9f2a782SErmal Luçi 164d9f2a782SErmal Luçi /* See if local, if yes, send it to netisr with IP_FASTFWD_OURS. */ 165d9f2a782SErmal Luçi if (m->m_flags & M_FASTFWD_OURS) { 166d9f2a782SErmal Luçi if (m->m_pkthdr.rcvif == NULL) 167d9f2a782SErmal Luçi m->m_pkthdr.rcvif = V_loif; 168d9f2a782SErmal Luçi if (m->m_pkthdr.csum_flags & CSUM_DELAY_DATA) { 169d9f2a782SErmal Luçi m->m_pkthdr.csum_flags |= 170d9f2a782SErmal Luçi CSUM_DATA_VALID | CSUM_PSEUDO_HDR; 171d9f2a782SErmal Luçi m->m_pkthdr.csum_data = 0xffff; 172d9f2a782SErmal Luçi } 173d9f2a782SErmal Luçi #ifdef SCTP 174d9f2a782SErmal Luçi if (m->m_pkthdr.csum_flags & CSUM_SCTP) 175d9f2a782SErmal Luçi m->m_pkthdr.csum_flags |= CSUM_SCTP_VALID; 176d9f2a782SErmal Luçi #endif 177d9f2a782SErmal Luçi m->m_pkthdr.csum_flags |= 178d9f2a782SErmal Luçi CSUM_IP_CHECKED | CSUM_IP_VALID; 179d9f2a782SErmal Luçi 180d9f2a782SErmal Luçi *error = netisr_queue(NETISR_IP, m); 181d9f2a782SErmal Luçi return 1; /* Finished */ 182d9f2a782SErmal Luçi } 183d9f2a782SErmal Luçi /* Or forward to some other address? */ 184d9f2a782SErmal Luçi if ((m->m_flags & M_IP_NEXTHOP) && 185d9f2a782SErmal Luçi ((fwd_tag = m_tag_find(m, PACKET_TAG_IPFORWARD, NULL)) != NULL)) { 186d9f2a782SErmal Luçi bcopy((fwd_tag+1), dst, sizeof(struct sockaddr_in)); 187d9f2a782SErmal Luçi m->m_flags |= M_SKIP_FIREWALL; 188d9f2a782SErmal Luçi m->m_flags &= ~M_IP_NEXTHOP; 189d9f2a782SErmal Luçi m_tag_delete(m, fwd_tag); 190d9f2a782SErmal Luçi 191d9f2a782SErmal Luçi return -1; /* Reloop for CHANGE of dst */ 192d9f2a782SErmal Luçi } 193d9f2a782SErmal Luçi 194d9f2a782SErmal Luçi return 0; 195d9f2a782SErmal Luçi } 196d9f2a782SErmal Luçi 197df8bae1dSRodney W. Grimes /* 198df8bae1dSRodney W. Grimes * IP output. The packet in mbuf chain m contains a skeletal IP 199df8bae1dSRodney W. Grimes * header (with len, off, ttl, proto, tos, src, dst). 200df8bae1dSRodney W. Grimes * The mbuf chain containing the packet will be freed. 201df8bae1dSRodney W. Grimes * The mbuf opt, if present, will not be freed. 202bf984051SGleb Smirnoff * If route ro is present and has ro_rt initialized, route lookup would be 203bf984051SGleb Smirnoff * skipped and ro->ro_rt would be used. If ro is present but ro->ro_rt is NULL, 204bf984051SGleb Smirnoff * then result of route lookup is stored in ro->ro_rt. 205bf984051SGleb Smirnoff * 2063de758d3SRobert Watson * In the IP forwarding case, the packet will arrive with options already 2073de758d3SRobert Watson * inserted, so must have a NULL opt pointer. 208df8bae1dSRodney W. Grimes */ 209df8bae1dSRodney W. Grimes int 210f2565d68SRobert Watson ip_output(struct mbuf *m, struct mbuf *opt, struct route *ro, int flags, 211f2565d68SRobert Watson struct ip_moptions *imo, struct inpcb *inp) 212df8bae1dSRodney W. Grimes { 213cc0a3c8cSAndrey V. Elsukov struct rm_priotracker in_ifa_tracker; 2141e78ac21SJeffrey Hsu struct ip *ip; 215fc74d005SBjoern A. Zeeb struct ifnet *ifp = NULL; /* keep compiler happy */ 216ac9d7e26SMax Laier struct mbuf *m0; 21723bf9953SPoul-Henning Kamp int hlen = sizeof (struct ip); 2183ae2ad08SJohn-Mark Gurney int mtu; 219ca8b83b0SLuigi Rizzo int error = 0; 220ca8b83b0SLuigi Rizzo struct sockaddr_in *dst; 2214c7a6059SGleb Smirnoff const struct sockaddr_in *gw; 2223c73180fSGleb Smirnoff struct in_ifaddr *ia; 22321d172a3SGleb Smirnoff int isbroadcast; 224078468edSGleb Smirnoff uint16_t ip_len, ip_off; 225e3f406b3SRuslan Ermilov struct route iproute; 226ec396e61SLuigi Rizzo struct rtentry *rte; /* cache for ro->ro_rt */ 2279c57a5b6SHiroki Sato uint32_t fibnum; 228fe82cbe8SGleb Smirnoff int have_ia_ref; 229fcf59617SAndrey V. Elsukov #if defined(IPSEC) || defined(IPSEC_SUPPORT) 2301a499816SEdward Tomasz Napierala int no_route_but_check_spd = 0; 2311a499816SEdward Tomasz Napierala #endif 232ac9d7e26SMax Laier M_ASSERTPKTHDR(m); 23336e8826fSMax Laier 234bc97ba51SJulian Elischer if (inp != NULL) { 235baa45840SRobert Watson INP_LOCK_ASSERT(inp); 23662e1ba83SRobert Watson M_SETFIB(m, inp->inp_inc.inc_fibnum); 237c2529042SHans Petter Selasky if ((flags & IP_NODEFAULTFLOWID) == 0) { 23880cb9f21SKip Macy m->m_pkthdr.flowid = inp->inp_flowid; 2399c423972SAdrian Chadd M_HASHTYPE_SET(m, inp->inp_flowtype); 24080cb9f21SKip Macy } 241bc97ba51SJulian Elischer } 24262e1ba83SRobert Watson 24362e1ba83SRobert Watson if (ro == NULL) { 24462e1ba83SRobert Watson ro = &iproute; 24562e1ba83SRobert Watson bzero(ro, sizeof (*ro)); 2468c1960d5SMike Karels } 24762e1ba83SRobert Watson 248df8bae1dSRodney W. Grimes if (opt) { 249ca8b83b0SLuigi Rizzo int len = 0; 250df8bae1dSRodney W. Grimes m = ip_insertoptions(m, opt, &len); 251cb7641e8SMaxim Konovalov if (len != 0) 252ca8b83b0SLuigi Rizzo hlen = len; /* ip->ip_hl is updated above */ 253df8bae1dSRodney W. Grimes } 254df8bae1dSRodney W. Grimes ip = mtod(m, struct ip *); 2558f134647SGleb Smirnoff ip_len = ntohs(ip->ip_len); 2568f134647SGleb Smirnoff ip_off = ntohs(ip->ip_off); 2573efc3014SJulian Elischer 258df8bae1dSRodney W. Grimes if ((flags & (IP_FORWARDING|IP_RAWOUTPUT)) == 0) { 25953be11f6SPoul-Henning Kamp ip->ip_v = IPVERSION; 26053be11f6SPoul-Henning Kamp ip->ip_hl = hlen >> 2; 2616d947416SGleb Smirnoff ip_fillid(ip); 26286425c62SRobert Watson IPSTAT_INC(ips_localout); 263df8bae1dSRodney W. Grimes } else { 264ca8b83b0SLuigi Rizzo /* Header already set, fetch hlen from there */ 26553be11f6SPoul-Henning Kamp hlen = ip->ip_hl << 2; 266df8bae1dSRodney W. Grimes } 2679c9137eaSGarrett Wollman 268054692a4SAlexander V. Chernikov /* 269054692a4SAlexander V. Chernikov * dst/gw handling: 270054692a4SAlexander V. Chernikov * 2713c065f2fSGleb Smirnoff * dst can be rewritten but always points to &ro->ro_dst. 2723c065f2fSGleb Smirnoff * gw is readonly but can point either to dst OR rt_gateway, 2733c065f2fSGleb Smirnoff * therefore we need restore gw if we're redoing lookup. 274054692a4SAlexander V. Chernikov */ 2754c7a6059SGleb Smirnoff gw = dst = (struct sockaddr_in *)&ro->ro_dst; 2769c57a5b6SHiroki Sato fibnum = (inp != NULL) ? inp->inp_inc.inc_fibnum : M_GETFIB(m); 277d9f2a782SErmal Luçi rte = ro->ro_rt; 27884cc0778SGeorge V. Neville-Neil if (rte == NULL) { 279a4a6e773SHajimu UMEMOTO bzero(dst, sizeof(*dst)); 280df8bae1dSRodney W. Grimes dst->sin_family = AF_INET; 281df8bae1dSRodney W. Grimes dst->sin_len = sizeof(*dst); 2829b932e9eSAndre Oppermann dst->sin_addr = ip->ip_dst; 283df8bae1dSRodney W. Grimes } 284d9f2a782SErmal Luçi again: 28584cc0778SGeorge V. Neville-Neil /* 28684cc0778SGeorge V. Neville-Neil * Validate route against routing table additions; 28784cc0778SGeorge V. Neville-Neil * a better/more specific route might have been added. 28884cc0778SGeorge V. Neville-Neil */ 28984cc0778SGeorge V. Neville-Neil if (inp) 29084cc0778SGeorge V. Neville-Neil RT_VALIDATE(ro, &inp->inp_rt_cookie, fibnum); 29184cc0778SGeorge V. Neville-Neil /* 29284cc0778SGeorge V. Neville-Neil * If there is a cached route, 29384cc0778SGeorge V. Neville-Neil * check that it is to the same destination 29484cc0778SGeorge V. Neville-Neil * and is still up. If not, free it and try again. 29584cc0778SGeorge V. Neville-Neil * The address family should also be checked in case of sharing the 29684cc0778SGeorge V. Neville-Neil * cache with IPv6. 29784cc0778SGeorge V. Neville-Neil * Also check whether routing cache needs invalidation. 29884cc0778SGeorge V. Neville-Neil */ 29984cc0778SGeorge V. Neville-Neil rte = ro->ro_rt; 30084cc0778SGeorge V. Neville-Neil if (rte && ((rte->rt_flags & RTF_UP) == 0 || 30184cc0778SGeorge V. Neville-Neil rte->rt_ifp == NULL || 30284cc0778SGeorge V. Neville-Neil !RT_LINK_IS_UP(rte->rt_ifp) || 30384cc0778SGeorge V. Neville-Neil dst->sin_family != AF_INET || 30484cc0778SGeorge V. Neville-Neil dst->sin_addr.s_addr != ip->ip_dst.s_addr)) { 30584cc0778SGeorge V. Neville-Neil RTFREE(rte); 30684cc0778SGeorge V. Neville-Neil rte = ro->ro_rt = (struct rtentry *)NULL; 3076d768226SGeorge V. Neville-Neil if (ro->ro_lle) 3086d768226SGeorge V. Neville-Neil LLE_FREE(ro->ro_lle); /* zeros ro_lle */ 3096d768226SGeorge V. Neville-Neil ro->ro_lle = (struct llentry *)NULL; 31084cc0778SGeorge V. Neville-Neil } 311d9f2a782SErmal Luçi ia = NULL; 312d9f2a782SErmal Luçi have_ia_ref = 0; 313df8bae1dSRodney W. Grimes /* 314a3fd02d8SBruce M Simpson * If routing to interface only, short circuit routing lookup. 315a3fd02d8SBruce M Simpson * The use of an all-ones broadcast address implies this; an 316a3fd02d8SBruce M Simpson * interface is specified by the broadcast address of an interface, 317a3fd02d8SBruce M Simpson * or the destination address of a ptp interface. 318df8bae1dSRodney W. Grimes */ 319a3fd02d8SBruce M Simpson if (flags & IP_SENDONES) { 3204f8585e0SAlan Somers if ((ia = ifatoia(ifa_ifwithbroadaddr(sintosa(dst), 32158a39d8cSAlan Somers M_GETFIB(m)))) == NULL && 3224f8585e0SAlan Somers (ia = ifatoia(ifa_ifwithdstaddr(sintosa(dst), 32358a39d8cSAlan Somers M_GETFIB(m)))) == NULL) { 32486425c62SRobert Watson IPSTAT_INC(ips_noroute); 325a3fd02d8SBruce M Simpson error = ENETUNREACH; 326a3fd02d8SBruce M Simpson goto bad; 327a3fd02d8SBruce M Simpson } 328fe82cbe8SGleb Smirnoff have_ia_ref = 1; 329a3fd02d8SBruce M Simpson ip->ip_dst.s_addr = INADDR_BROADCAST; 330a3fd02d8SBruce M Simpson dst->sin_addr = ip->ip_dst; 331a3fd02d8SBruce M Simpson ifp = ia->ia_ifp; 332a3fd02d8SBruce M Simpson ip->ip_ttl = 1; 333a3fd02d8SBruce M Simpson isbroadcast = 1; 334a3fd02d8SBruce M Simpson } else if (flags & IP_ROUTETOIF) { 3354f8585e0SAlan Somers if ((ia = ifatoia(ifa_ifwithdstaddr(sintosa(dst), 33658a39d8cSAlan Somers M_GETFIB(m)))) == NULL && 3374f8585e0SAlan Somers (ia = ifatoia(ifa_ifwithnet(sintosa(dst), 0, 33858a39d8cSAlan Somers M_GETFIB(m)))) == NULL) { 33986425c62SRobert Watson IPSTAT_INC(ips_noroute); 340df8bae1dSRodney W. Grimes error = ENETUNREACH; 341df8bae1dSRodney W. Grimes goto bad; 342df8bae1dSRodney W. Grimes } 343fe82cbe8SGleb Smirnoff have_ia_ref = 1; 344df8bae1dSRodney W. Grimes ifp = ia->ia_ifp; 345df8bae1dSRodney W. Grimes ip->ip_ttl = 1; 3466c1bd558SRyan Stone isbroadcast = ifp->if_flags & IFF_BROADCAST ? 3476c1bd558SRyan Stone in_ifaddr_broadcast(dst->sin_addr, ia) : 0; 3483afefa39SDaniel C. Sobral } else if (IN_MULTICAST(ntohl(ip->ip_dst.s_addr)) && 34938c1bc35SRuslan Ermilov imo != NULL && imo->imo_multicast_ifp != NULL) { 3503afefa39SDaniel C. Sobral /* 35138c1bc35SRuslan Ermilov * Bypass the normal routing lookup for multicast 35238c1bc35SRuslan Ermilov * packets if the interface is specified. 3533afefa39SDaniel C. Sobral */ 35438c1bc35SRuslan Ermilov ifp = imo->imo_multicast_ifp; 355cc0a3c8cSAndrey V. Elsukov IFP_TO_IA(ifp, ia, &in_ifa_tracker); 356fe82cbe8SGleb Smirnoff if (ia) 357fe82cbe8SGleb Smirnoff have_ia_ref = 1; 35838c1bc35SRuslan Ermilov isbroadcast = 0; /* fool gcc */ 359df8bae1dSRodney W. Grimes } else { 3602c17fe93SGarrett Wollman /* 36197d8d152SAndre Oppermann * We want to do any cloning requested by the link layer, 36297d8d152SAndre Oppermann * as this is probably required in all cases for correct 36397d8d152SAndre Oppermann * operation (as it is for ARP). 3642c17fe93SGarrett Wollman */ 365ec396e61SLuigi Rizzo if (rte == NULL) { 366e440aed9SQing Li #ifdef RADIX_MPATH 3678b07e49aSJulian Elischer rtalloc_mpath_fib(ro, 3688b07e49aSJulian Elischer ntohl(ip->ip_src.s_addr ^ ip->ip_dst.s_addr), 3699c57a5b6SHiroki Sato fibnum); 370e440aed9SQing Li #else 3719c57a5b6SHiroki Sato in_rtalloc_ign(ro, 0, fibnum); 372e440aed9SQing Li #endif 373ec396e61SLuigi Rizzo rte = ro->ro_rt; 374ec396e61SLuigi Rizzo } 375c7ea0aa6SQing Li if (rte == NULL || 376d9f2a782SErmal Luçi (rte->rt_flags & RTF_UP) == 0 || 377c7ea0aa6SQing Li rte->rt_ifp == NULL || 378c7ea0aa6SQing Li !RT_LINK_IS_UP(rte->rt_ifp)) { 379fcf59617SAndrey V. Elsukov #if defined(IPSEC) || defined(IPSEC_SUPPORT) 3801a499816SEdward Tomasz Napierala /* 3811a499816SEdward Tomasz Napierala * There is no route for this packet, but it is 3821a499816SEdward Tomasz Napierala * possible that a matching SPD entry exists. 3831a499816SEdward Tomasz Napierala */ 3841a499816SEdward Tomasz Napierala no_route_but_check_spd = 1; 3851a499816SEdward Tomasz Napierala mtu = 0; /* Silence GCC warning. */ 3861a499816SEdward Tomasz Napierala goto sendit; 3871a499816SEdward Tomasz Napierala #endif 38886425c62SRobert Watson IPSTAT_INC(ips_noroute); 389df8bae1dSRodney W. Grimes error = EHOSTUNREACH; 390df8bae1dSRodney W. Grimes goto bad; 391df8bae1dSRodney W. Grimes } 392ec396e61SLuigi Rizzo ia = ifatoia(rte->rt_ifa); 393ec396e61SLuigi Rizzo ifp = rte->rt_ifp; 394e3a7aa6fSGleb Smirnoff counter_u64_add(rte->rt_pksent, 1); 39536402a68SAlexander V. Chernikov rt_update_ro_flags(ro); 396ec396e61SLuigi Rizzo if (rte->rt_flags & RTF_GATEWAY) 3974c7a6059SGleb Smirnoff gw = (struct sockaddr_in *)rte->rt_gateway; 398ec396e61SLuigi Rizzo if (rte->rt_flags & RTF_HOST) 399ec396e61SLuigi Rizzo isbroadcast = (rte->rt_flags & RTF_BROADCAST); 4006c1bd558SRyan Stone else if (ifp->if_flags & IFF_BROADCAST) 40190cc51a1SRyan Stone isbroadcast = in_ifaddr_broadcast(gw->sin_addr, ia); 4026c1bd558SRyan Stone else 4036c1bd558SRyan Stone isbroadcast = 0; 404df8bae1dSRodney W. Grimes } 405d9f2a782SErmal Luçi 4063ae2ad08SJohn-Mark Gurney /* 4073ae2ad08SJohn-Mark Gurney * Calculate MTU. If we have a route that is up, use that, 4083ae2ad08SJohn-Mark Gurney * otherwise use the interface's MTU. 4093ae2ad08SJohn-Mark Gurney */ 4107f948f12SAlexander V. Chernikov if (rte != NULL && (rte->rt_flags & (RTF_UP|RTF_HOST))) 411e3a7aa6fSGleb Smirnoff mtu = rte->rt_mtu; 4127f948f12SAlexander V. Chernikov else 4133ae2ad08SJohn-Mark Gurney mtu = ifp->if_mtu; 414c744cde4SBjoern A. Zeeb /* Catch a possible divide by zero later. */ 415c744cde4SBjoern A. Zeeb KASSERT(mtu > 0, ("%s: mtu %d <= 0, rte=%p (rt_flags=0x%08x) ifp=%p", 416c744cde4SBjoern A. Zeeb __func__, mtu, rte, (rte != NULL) ? rte->rt_flags : 0, ifp)); 417d9f2a782SErmal Luçi 4189b932e9eSAndre Oppermann if (IN_MULTICAST(ntohl(ip->ip_dst.s_addr))) { 419df8bae1dSRodney W. Grimes m->m_flags |= M_MCAST; 420df8bae1dSRodney W. Grimes /* 421183e1c86SGleb Smirnoff * IP destination address is multicast. Make sure "gw" 422183e1c86SGleb Smirnoff * still points to the address in "ro". (It may have been 423183e1c86SGleb Smirnoff * changed to point to a gateway address, above.) 424183e1c86SGleb Smirnoff */ 425183e1c86SGleb Smirnoff gw = dst; 426183e1c86SGleb Smirnoff /* 427df8bae1dSRodney W. Grimes * See if the caller provided any multicast options 428df8bae1dSRodney W. Grimes */ 429df8bae1dSRodney W. Grimes if (imo != NULL) { 430df8bae1dSRodney W. Grimes ip->ip_ttl = imo->imo_multicast_ttl; 4311c5de19aSGarrett Wollman if (imo->imo_multicast_vif != -1) 4321c5de19aSGarrett Wollman ip->ip_src.s_addr = 433bbb4330bSLuigi Rizzo ip_mcast_src ? 434bbb4330bSLuigi Rizzo ip_mcast_src(imo->imo_multicast_vif) : 435bbb4330bSLuigi Rizzo INADDR_ANY; 436df8bae1dSRodney W. Grimes } else 437df8bae1dSRodney W. Grimes ip->ip_ttl = IP_DEFAULT_MULTICAST_TTL; 438df8bae1dSRodney W. Grimes /* 439df8bae1dSRodney W. Grimes * Confirm that the outgoing interface supports multicast. 440df8bae1dSRodney W. Grimes */ 4411c5de19aSGarrett Wollman if ((imo == NULL) || (imo->imo_multicast_vif == -1)) { 442df8bae1dSRodney W. Grimes if ((ifp->if_flags & IFF_MULTICAST) == 0) { 44386425c62SRobert Watson IPSTAT_INC(ips_noroute); 444df8bae1dSRodney W. Grimes error = ENETUNREACH; 445df8bae1dSRodney W. Grimes goto bad; 446df8bae1dSRodney W. Grimes } 4471c5de19aSGarrett Wollman } 448df8bae1dSRodney W. Grimes /* 449df8bae1dSRodney W. Grimes * If source address not specified yet, use address 450df8bae1dSRodney W. Grimes * of outgoing interface. 451df8bae1dSRodney W. Grimes */ 452df8bae1dSRodney W. Grimes if (ip->ip_src.s_addr == INADDR_ANY) { 45338c1bc35SRuslan Ermilov /* Interface may have no addresses. */ 45438c1bc35SRuslan Ermilov if (ia != NULL) 45538c1bc35SRuslan Ermilov ip->ip_src = IA_SIN(ia)->sin_addr; 456df8bae1dSRodney W. Grimes } 457df8bae1dSRodney W. Grimes 4588b889dbbSBruce M Simpson if ((imo == NULL && in_mcast_loop) || 4598b889dbbSBruce M Simpson (imo && imo->imo_multicast_loop)) { 460df8bae1dSRodney W. Grimes /* 4618b889dbbSBruce M Simpson * Loop back multicast datagram if not expressly 4628b889dbbSBruce M Simpson * forbidden to do so, even if we are not a member 4638b889dbbSBruce M Simpson * of the group; ip_input() will filter it later, 4648b889dbbSBruce M Simpson * thus deferring a hash lookup and mutex acquisition 4658b889dbbSBruce M Simpson * at the expense of a cheap copy using m_copym(). 466df8bae1dSRodney W. Grimes */ 467331dff07SAlexander V. Chernikov ip_mloopback(ifp, m, hlen); 4688b889dbbSBruce M Simpson } else { 469df8bae1dSRodney W. Grimes /* 470df8bae1dSRodney W. Grimes * If we are acting as a multicast router, perform 471df8bae1dSRodney W. Grimes * multicast forwarding as if the packet had just 472df8bae1dSRodney W. Grimes * arrived on the interface to which we are about 473df8bae1dSRodney W. Grimes * to send. The multicast forwarding function 474df8bae1dSRodney W. Grimes * recursively calls this function, using the 475df8bae1dSRodney W. Grimes * IP_FORWARDING flag to prevent infinite recursion. 476df8bae1dSRodney W. Grimes * 477df8bae1dSRodney W. Grimes * Multicasts that are looped back by ip_mloopback(), 478df8bae1dSRodney W. Grimes * above, will be forwarded by the ip_input() routine, 479df8bae1dSRodney W. Grimes * if necessary. 480df8bae1dSRodney W. Grimes */ 481603724d3SBjoern A. Zeeb if (V_ip_mrouter && (flags & IP_FORWARDING) == 0) { 482f0068c4aSGarrett Wollman /* 483bbb4330bSLuigi Rizzo * If rsvp daemon is not running, do not 484f0068c4aSGarrett Wollman * set ip_moptions. This ensures that the packet 485f0068c4aSGarrett Wollman * is multicast and not just sent down one link 486f0068c4aSGarrett Wollman * as prescribed by rsvpd. 487f0068c4aSGarrett Wollman */ 488603724d3SBjoern A. Zeeb if (!V_rsvp_on) 489f0068c4aSGarrett Wollman imo = NULL; 490bbb4330bSLuigi Rizzo if (ip_mforward && 491bbb4330bSLuigi Rizzo ip_mforward(ip, ifp, m, imo) != 0) { 492df8bae1dSRodney W. Grimes m_freem(m); 493df8bae1dSRodney W. Grimes goto done; 494df8bae1dSRodney W. Grimes } 495df8bae1dSRodney W. Grimes } 496df8bae1dSRodney W. Grimes } 4975e9ae478SGarrett Wollman 498df8bae1dSRodney W. Grimes /* 499df8bae1dSRodney W. Grimes * Multicasts with a time-to-live of zero may be looped- 500df8bae1dSRodney W. Grimes * back, above, but must not be transmitted on a network. 501df8bae1dSRodney W. Grimes * Also, multicasts addressed to the loopback interface 502df8bae1dSRodney W. Grimes * are not sent -- the above call to ip_mloopback() will 5038b889dbbSBruce M Simpson * loop back a copy. ip_input() will drop the copy if 5048b889dbbSBruce M Simpson * this host does not belong to the destination group on 5058b889dbbSBruce M Simpson * the loopback interface. 506df8bae1dSRodney W. Grimes */ 507f5fea3ddSPaul Traina if (ip->ip_ttl == 0 || ifp->if_flags & IFF_LOOPBACK) { 508df8bae1dSRodney W. Grimes m_freem(m); 509df8bae1dSRodney W. Grimes goto done; 510df8bae1dSRodney W. Grimes } 511df8bae1dSRodney W. Grimes 512df8bae1dSRodney W. Grimes goto sendit; 513df8bae1dSRodney W. Grimes } 5146a7c943cSAndre Oppermann 515df8bae1dSRodney W. Grimes /* 5162b25acc1SLuigi Rizzo * If the source address is not specified yet, use the address 517cb459254SJohn-Mark Gurney * of the outoing interface. 518df8bae1dSRodney W. Grimes */ 519f9e354dfSJulian Elischer if (ip->ip_src.s_addr == INADDR_ANY) { 52038c1bc35SRuslan Ermilov /* Interface may have no addresses. */ 52138c1bc35SRuslan Ermilov if (ia != NULL) { 522df8bae1dSRodney W. Grimes ip->ip_src = IA_SIN(ia)->sin_addr; 523f9e354dfSJulian Elischer } 52438c1bc35SRuslan Ermilov } 5256a7c943cSAndre Oppermann 526ca7a789aSMax Laier /* 527df8bae1dSRodney W. Grimes * Look for broadcast address and 5288c3f5566SRuslan Ermilov * verify user is allowed to send 529df8bae1dSRodney W. Grimes * such a packet. 530df8bae1dSRodney W. Grimes */ 5319f9b3dc4SGarrett Wollman if (isbroadcast) { 532df8bae1dSRodney W. Grimes if ((ifp->if_flags & IFF_BROADCAST) == 0) { 533df8bae1dSRodney W. Grimes error = EADDRNOTAVAIL; 534df8bae1dSRodney W. Grimes goto bad; 535df8bae1dSRodney W. Grimes } 536df8bae1dSRodney W. Grimes if ((flags & IP_ALLOWBROADCAST) == 0) { 537df8bae1dSRodney W. Grimes error = EACCES; 538df8bae1dSRodney W. Grimes goto bad; 539df8bae1dSRodney W. Grimes } 540df8bae1dSRodney W. Grimes /* don't allow broadcast messages to be fragmented */ 5418f134647SGleb Smirnoff if (ip_len > mtu) { 542df8bae1dSRodney W. Grimes error = EMSGSIZE; 543df8bae1dSRodney W. Grimes goto bad; 544df8bae1dSRodney W. Grimes } 545df8bae1dSRodney W. Grimes m->m_flags |= M_BCAST; 5469f9b3dc4SGarrett Wollman } else { 547df8bae1dSRodney W. Grimes m->m_flags &= ~M_BCAST; 5489f9b3dc4SGarrett Wollman } 549df8bae1dSRodney W. Grimes 550f1743588SDarren Reed sendit: 551fcf59617SAndrey V. Elsukov #if defined(IPSEC) || defined(IPSEC_SUPPORT) 552fcf59617SAndrey V. Elsukov if (IPSEC_ENABLED(ipv4)) { 553fcf59617SAndrey V. Elsukov if ((error = IPSEC_OUTPUT(ipv4, m, inp)) != 0) { 554fcf59617SAndrey V. Elsukov if (error == EINPROGRESS) 555fcf59617SAndrey V. Elsukov error = 0; 5561dfcf0d2SAndre Oppermann goto done; 557fcf59617SAndrey V. Elsukov } 55833841545SHajimu UMEMOTO } 5591a499816SEdward Tomasz Napierala /* 5601a499816SEdward Tomasz Napierala * Check if there was a route for this packet; return error if not. 5611a499816SEdward Tomasz Napierala */ 5621a499816SEdward Tomasz Napierala if (no_route_but_check_spd) { 5631a499816SEdward Tomasz Napierala IPSTAT_INC(ips_noroute); 5641a499816SEdward Tomasz Napierala error = EHOSTUNREACH; 5651a499816SEdward Tomasz Napierala goto bad; 5661a499816SEdward Tomasz Napierala } 5671dfcf0d2SAndre Oppermann /* Update variables that are affected by ipsec4_output(). */ 56833841545SHajimu UMEMOTO ip = mtod(m, struct ip *); 56933841545SHajimu UMEMOTO hlen = ip->ip_hl << 2; 570b2630c29SGeorge V. Neville-Neil #endif /* IPSEC */ 57133841545SHajimu UMEMOTO 572c21fd232SAndre Oppermann /* Jump over all PFIL processing if hooks are not active. */ 573d9f2a782SErmal Luçi if (PFIL_HOOKED(&V_inet_pfil_hook)) { 5748f980c01SMark Johnston switch (ip_output_pfil(&m, ifp, inp, dst, &fibnum, &error)) { 575d9f2a782SErmal Luçi case 1: /* Finished */ 576c4ac87eaSDarren Reed goto done; 5779b932e9eSAndre Oppermann 578d9f2a782SErmal Luçi case 0: /* Continue normally */ 579c4ac87eaSDarren Reed ip = mtod(m, struct ip *); 580d9f2a782SErmal Luçi break; 581fed1c7e9SSøren Schmidt 582d9f2a782SErmal Luçi case -1: /* Need to try again */ 583d9f2a782SErmal Luçi /* Reset everything for a new round */ 5849c57a5b6SHiroki Sato RO_RTFREE(ro); 585fe82cbe8SGleb Smirnoff if (have_ia_ref) 586fe82cbe8SGleb Smirnoff ifa_free(&ia->ia_ifa); 5874fb3a820SAlexander V. Chernikov ro->ro_prepend = NULL; 588d9f2a782SErmal Luçi rte = NULL; 589d9f2a782SErmal Luçi gw = dst; 590d9f2a782SErmal Luçi ip = mtod(m, struct ip *); 5919b932e9eSAndre Oppermann goto again; 592d9f2a782SErmal Luçi 593d9f2a782SErmal Luçi } 594099dd043SAndre Oppermann } 5952b25acc1SLuigi Rizzo 59651c8ec4aSRuslan Ermilov /* 127/8 must not appear on wire - RFC1122. */ 59751c8ec4aSRuslan Ermilov if ((ntohl(ip->ip_dst.s_addr) >> IN_CLASSA_NSHIFT) == IN_LOOPBACKNET || 59851c8ec4aSRuslan Ermilov (ntohl(ip->ip_src.s_addr) >> IN_CLASSA_NSHIFT) == IN_LOOPBACKNET) { 59951c8ec4aSRuslan Ermilov if ((ifp->if_flags & IFF_LOOPBACK) == 0) { 60086425c62SRobert Watson IPSTAT_INC(ips_badaddr); 60151c8ec4aSRuslan Ermilov error = EADDRNOTAVAIL; 60251c8ec4aSRuslan Ermilov goto bad; 60351c8ec4aSRuslan Ermilov } 60451c8ec4aSRuslan Ermilov } 60551c8ec4aSRuslan Ermilov 606206a3274SRuslan Ermilov m->m_pkthdr.csum_flags |= CSUM_IP; 607078468edSGleb Smirnoff if (m->m_pkthdr.csum_flags & CSUM_DELAY_DATA & ~ifp->if_hwassist) { 608db4f9cc7SJonathan Lemon in_delayed_cksum(m); 609078468edSGleb Smirnoff m->m_pkthdr.csum_flags &= ~CSUM_DELAY_DATA; 610db4f9cc7SJonathan Lemon } 6112f4afd21SRandall Stewart #ifdef SCTP 612078468edSGleb Smirnoff if (m->m_pkthdr.csum_flags & CSUM_SCTP & ~ifp->if_hwassist) { 6131966e5b5SRandall Stewart sctp_delayed_cksum(m, (uint32_t)(ip->ip_hl << 2)); 614078468edSGleb Smirnoff m->m_pkthdr.csum_flags &= ~CSUM_SCTP; 6152f4afd21SRandall Stewart } 6162f4afd21SRandall Stewart #endif 617db4f9cc7SJonathan Lemon 618e7319babSPoul-Henning Kamp /* 619db4f9cc7SJonathan Lemon * If small enough for interface, or the interface will take 620233dcce1SAndre Oppermann * care of the fragmentation for us, we can just send directly. 621df8bae1dSRodney W. Grimes */ 62221d172a3SGleb Smirnoff if (ip_len <= mtu || 623bf7dcda3SGleb Smirnoff (m->m_pkthdr.csum_flags & ifp->if_hwassist & CSUM_TSO) != 0) { 624df8bae1dSRodney W. Grimes ip->ip_sum = 0; 625078468edSGleb Smirnoff if (m->m_pkthdr.csum_flags & CSUM_IP & ~ifp->if_hwassist) { 626df8bae1dSRodney W. Grimes ip->ip_sum = in_cksum(m, hlen); 627078468edSGleb Smirnoff m->m_pkthdr.csum_flags &= ~CSUM_IP; 628078468edSGleb Smirnoff } 6295da9f8faSJosef Karthauser 630233dcce1SAndre Oppermann /* 631233dcce1SAndre Oppermann * Record statistics for this interface address. 632233dcce1SAndre Oppermann * With CSUM_TSO the byte/packet count will be slightly 633233dcce1SAndre Oppermann * incorrect because we count the IP+TCP headers only 634233dcce1SAndre Oppermann * once instead of for every generated packet. 635233dcce1SAndre Oppermann */ 63638c1bc35SRuslan Ermilov if (!(flags & IP_FORWARDING) && ia) { 637233dcce1SAndre Oppermann if (m->m_pkthdr.csum_flags & CSUM_TSO) 6387caf4ab7SGleb Smirnoff counter_u64_add(ia->ia_ifa.ifa_opackets, 6397caf4ab7SGleb Smirnoff m->m_pkthdr.len / m->m_pkthdr.tso_segsz); 640233dcce1SAndre Oppermann else 6417caf4ab7SGleb Smirnoff counter_u64_add(ia->ia_ifa.ifa_opackets, 1); 6427caf4ab7SGleb Smirnoff 6437caf4ab7SGleb Smirnoff counter_u64_add(ia->ia_ifa.ifa_obytes, m->m_pkthdr.len); 6445da9f8faSJosef Karthauser } 64553dcc544SMike Silbersack #ifdef MBUF_STRESS_TEST 6463390d476SMike Silbersack if (mbuf_frag_size && m->m_pkthdr.len > mbuf_frag_size) 647eb1b1807SGleb Smirnoff m = m_fragment(m, M_NOWAIT, mbuf_frag_size); 6489d9edc56SMike Silbersack #endif 649147f74d1SAndre Oppermann /* 650147f74d1SAndre Oppermann * Reset layer specific mbuf flags 651147f74d1SAndre Oppermann * to avoid confusing lower layers. 652147f74d1SAndre Oppermann */ 65386bd0491SAndre Oppermann m_clrprotoflags(m); 65457f60867SMark Johnston IP_PROBE(send, NULL, NULL, ip, ifp, ip, NULL); 655f3e7afe2SHans Petter Selasky #ifdef RATELIMIT 656f3e7afe2SHans Petter Selasky if (inp != NULL) { 657f3e7afe2SHans Petter Selasky if (inp->inp_flags2 & INP_RATE_LIMIT_CHANGED) 658f3e7afe2SHans Petter Selasky in_pcboutput_txrtlmt(inp, ifp, m); 659f3e7afe2SHans Petter Selasky /* stamp send tag on mbuf */ 660f3e7afe2SHans Petter Selasky m->m_pkthdr.snd_tag = inp->inp_snd_tag; 661f3e7afe2SHans Petter Selasky } else { 662f3e7afe2SHans Petter Selasky m->m_pkthdr.snd_tag = NULL; 663f3e7afe2SHans Petter Selasky } 664f3e7afe2SHans Petter Selasky #endif 66547e8d432SGleb Smirnoff error = (*ifp->if_output)(ifp, m, 66647e8d432SGleb Smirnoff (const struct sockaddr *)gw, ro); 667f3e7afe2SHans Petter Selasky #ifdef RATELIMIT 668f3e7afe2SHans Petter Selasky /* check for route change */ 669f3e7afe2SHans Petter Selasky if (error == EAGAIN) 670f3e7afe2SHans Petter Selasky in_pcboutput_eagain(inp); 671f3e7afe2SHans Petter Selasky #endif 672df8bae1dSRodney W. Grimes goto done; 673df8bae1dSRodney W. Grimes } 6741e78ac21SJeffrey Hsu 675233dcce1SAndre Oppermann /* Balk when DF bit is set or the interface didn't support TSO. */ 67621d172a3SGleb Smirnoff if ((ip_off & IP_DF) || (m->m_pkthdr.csum_flags & CSUM_TSO)) { 677df8bae1dSRodney W. Grimes error = EMSGSIZE; 67886425c62SRobert Watson IPSTAT_INC(ips_cantfrag); 679df8bae1dSRodney W. Grimes goto bad; 680df8bae1dSRodney W. Grimes } 6811e78ac21SJeffrey Hsu 6821e78ac21SJeffrey Hsu /* 6831e78ac21SJeffrey Hsu * Too large for interface; fragment if possible. If successful, 6841e78ac21SJeffrey Hsu * on return, m will point to a list of packets to be sent. 6851e78ac21SJeffrey Hsu */ 686078468edSGleb Smirnoff error = ip_fragment(ip, &m, mtu, ifp->if_hwassist); 6871e78ac21SJeffrey Hsu if (error) 688df8bae1dSRodney W. Grimes goto bad; 6891e78ac21SJeffrey Hsu for (; m; m = m0) { 690df8bae1dSRodney W. Grimes m0 = m->m_nextpkt; 691b375c9ecSLuigi Rizzo m->m_nextpkt = 0; 69207203494SDaniel C. Sobral if (error == 0) { 693fe937674SJosef Karthauser /* Record statistics for this interface address. */ 69407203494SDaniel C. Sobral if (ia != NULL) { 6957caf4ab7SGleb Smirnoff counter_u64_add(ia->ia_ifa.ifa_opackets, 1); 6967caf4ab7SGleb Smirnoff counter_u64_add(ia->ia_ifa.ifa_obytes, 6977caf4ab7SGleb Smirnoff m->m_pkthdr.len); 69807203494SDaniel C. Sobral } 699147f74d1SAndre Oppermann /* 700147f74d1SAndre Oppermann * Reset layer specific mbuf flags 701147f74d1SAndre Oppermann * to avoid confusing upper layers. 702147f74d1SAndre Oppermann */ 70386bd0491SAndre Oppermann m_clrprotoflags(m); 704fe937674SJosef Karthauser 7052e77d270SAndrey V. Elsukov IP_PROBE(send, NULL, NULL, mtod(m, struct ip *), ifp, 7062e77d270SAndrey V. Elsukov mtod(m, struct ip *), NULL); 707f3e7afe2SHans Petter Selasky #ifdef RATELIMIT 708f3e7afe2SHans Petter Selasky if (inp != NULL) { 709f3e7afe2SHans Petter Selasky if (inp->inp_flags2 & INP_RATE_LIMIT_CHANGED) 710f3e7afe2SHans Petter Selasky in_pcboutput_txrtlmt(inp, ifp, m); 711f3e7afe2SHans Petter Selasky /* stamp send tag on mbuf */ 712f3e7afe2SHans Petter Selasky m->m_pkthdr.snd_tag = inp->inp_snd_tag; 713f3e7afe2SHans Petter Selasky } else { 714f3e7afe2SHans Petter Selasky m->m_pkthdr.snd_tag = NULL; 715f3e7afe2SHans Petter Selasky } 716f3e7afe2SHans Petter Selasky #endif 717df8bae1dSRodney W. Grimes error = (*ifp->if_output)(ifp, m, 71847e8d432SGleb Smirnoff (const struct sockaddr *)gw, ro); 719f3e7afe2SHans Petter Selasky #ifdef RATELIMIT 720f3e7afe2SHans Petter Selasky /* check for route change */ 721f3e7afe2SHans Petter Selasky if (error == EAGAIN) 722f3e7afe2SHans Petter Selasky in_pcboutput_eagain(inp); 723f3e7afe2SHans Petter Selasky #endif 724fe937674SJosef Karthauser } else 725df8bae1dSRodney W. Grimes m_freem(m); 726df8bae1dSRodney W. Grimes } 727df8bae1dSRodney W. Grimes 728df8bae1dSRodney W. Grimes if (error == 0) 72986425c62SRobert Watson IPSTAT_INC(ips_fragmented); 7301e78ac21SJeffrey Hsu 731df8bae1dSRodney W. Grimes done: 732cc94f0c2SGleb Smirnoff if (ro == &iproute) 733bf984051SGleb Smirnoff RO_RTFREE(ro); 7343c402323SErmal Luçi else if (rte == NULL) 7353c402323SErmal Luçi /* 7363c402323SErmal Luçi * If the caller supplied a route but somehow the reference 7373c402323SErmal Luçi * to it has been released need to prevent the caller 7383c402323SErmal Luçi * calling RTFREE on it again. 7393c402323SErmal Luçi */ 7403c402323SErmal Luçi ro->ro_rt = NULL; 741fe82cbe8SGleb Smirnoff if (have_ia_ref) 742fe82cbe8SGleb Smirnoff ifa_free(&ia->ia_ifa); 743df8bae1dSRodney W. Grimes return (error); 744df8bae1dSRodney W. Grimes bad: 7453528d68fSBill Paul m_freem(m); 746df8bae1dSRodney W. Grimes goto done; 747df8bae1dSRodney W. Grimes } 748df8bae1dSRodney W. Grimes 7491e78ac21SJeffrey Hsu /* 7501e78ac21SJeffrey Hsu * Create a chain of fragments which fit the given mtu. m_frag points to the 7511e78ac21SJeffrey Hsu * mbuf to be fragmented; on return it points to the chain with the fragments. 7521e78ac21SJeffrey Hsu * Return 0 if no error. If error, m_frag may contain a partially built 7531e78ac21SJeffrey Hsu * chain of fragments that should be freed by the caller. 7541e78ac21SJeffrey Hsu * 7551e78ac21SJeffrey Hsu * if_hwassist_flags is the hw offload capabilities (see if_data.ifi_hwassist) 7561e78ac21SJeffrey Hsu */ 7571e78ac21SJeffrey Hsu int 7581e78ac21SJeffrey Hsu ip_fragment(struct ip *ip, struct mbuf **m_frag, int mtu, 759078468edSGleb Smirnoff u_long if_hwassist_flags) 7601e78ac21SJeffrey Hsu { 7611e78ac21SJeffrey Hsu int error = 0; 7621e78ac21SJeffrey Hsu int hlen = ip->ip_hl << 2; 7631e78ac21SJeffrey Hsu int len = (mtu - hlen) & ~7; /* size of payload in each fragment */ 7641e78ac21SJeffrey Hsu int off; 7651e78ac21SJeffrey Hsu struct mbuf *m0 = *m_frag; /* the original packet */ 7661e78ac21SJeffrey Hsu int firstlen; 7671e78ac21SJeffrey Hsu struct mbuf **mnext; 7681e78ac21SJeffrey Hsu int nfrags; 76921d172a3SGleb Smirnoff uint16_t ip_len, ip_off; 7701e78ac21SJeffrey Hsu 77121d172a3SGleb Smirnoff ip_len = ntohs(ip->ip_len); 77221d172a3SGleb Smirnoff ip_off = ntohs(ip->ip_off); 77321d172a3SGleb Smirnoff 77421d172a3SGleb Smirnoff if (ip_off & IP_DF) { /* Fragmentation not allowed */ 77586425c62SRobert Watson IPSTAT_INC(ips_cantfrag); 7761e78ac21SJeffrey Hsu return EMSGSIZE; 7771e78ac21SJeffrey Hsu } 7781e78ac21SJeffrey Hsu 7791e78ac21SJeffrey Hsu /* 7801e78ac21SJeffrey Hsu * Must be able to put at least 8 bytes per fragment. 7811e78ac21SJeffrey Hsu */ 7821e78ac21SJeffrey Hsu if (len < 8) 7831e78ac21SJeffrey Hsu return EMSGSIZE; 7841e78ac21SJeffrey Hsu 7851e78ac21SJeffrey Hsu /* 7861e78ac21SJeffrey Hsu * If the interface will not calculate checksums on 7871e78ac21SJeffrey Hsu * fragmented packets, then do it here. 7881e78ac21SJeffrey Hsu */ 789da2299c5SAndre Oppermann if (m0->m_pkthdr.csum_flags & CSUM_DELAY_DATA) { 7901e78ac21SJeffrey Hsu in_delayed_cksum(m0); 7911e78ac21SJeffrey Hsu m0->m_pkthdr.csum_flags &= ~CSUM_DELAY_DATA; 7921e78ac21SJeffrey Hsu } 7932f4afd21SRandall Stewart #ifdef SCTP 794da2299c5SAndre Oppermann if (m0->m_pkthdr.csum_flags & CSUM_SCTP) { 7951966e5b5SRandall Stewart sctp_delayed_cksum(m0, hlen); 7962f4afd21SRandall Stewart m0->m_pkthdr.csum_flags &= ~CSUM_SCTP; 7972f4afd21SRandall Stewart } 7982f4afd21SRandall Stewart #endif 7991e78ac21SJeffrey Hsu if (len > PAGE_SIZE) { 8001e78ac21SJeffrey Hsu /* 8011e78ac21SJeffrey Hsu * Fragment large datagrams such that each segment 8021e78ac21SJeffrey Hsu * contains a multiple of PAGE_SIZE amount of data, 8031e78ac21SJeffrey Hsu * plus headers. This enables a receiver to perform 8041e78ac21SJeffrey Hsu * page-flipping zero-copy optimizations. 8051e78ac21SJeffrey Hsu * 8061e78ac21SJeffrey Hsu * XXX When does this help given that sender and receiver 8071e78ac21SJeffrey Hsu * could have different page sizes, and also mtu could 8081e78ac21SJeffrey Hsu * be less than the receiver's page size ? 8091e78ac21SJeffrey Hsu */ 8101e78ac21SJeffrey Hsu int newlen; 8111e78ac21SJeffrey Hsu 8129c0f6aa7SHans Petter Selasky off = MIN(mtu, m0->m_pkthdr.len); 8131e78ac21SJeffrey Hsu 8141e78ac21SJeffrey Hsu /* 8151e78ac21SJeffrey Hsu * firstlen (off - hlen) must be aligned on an 8161e78ac21SJeffrey Hsu * 8-byte boundary 8171e78ac21SJeffrey Hsu */ 8181e78ac21SJeffrey Hsu if (off < hlen) 8191e78ac21SJeffrey Hsu goto smart_frag_failure; 8201e78ac21SJeffrey Hsu off = ((off - hlen) & ~7) + hlen; 8211e78ac21SJeffrey Hsu newlen = (~PAGE_MASK) & mtu; 8221e78ac21SJeffrey Hsu if ((newlen + sizeof (struct ip)) > mtu) { 8231e78ac21SJeffrey Hsu /* we failed, go back the default */ 8241e78ac21SJeffrey Hsu smart_frag_failure: 8251e78ac21SJeffrey Hsu newlen = len; 8261e78ac21SJeffrey Hsu off = hlen + len; 8271e78ac21SJeffrey Hsu } 8281e78ac21SJeffrey Hsu len = newlen; 8291e78ac21SJeffrey Hsu 8301e78ac21SJeffrey Hsu } else { 8311e78ac21SJeffrey Hsu off = hlen + len; 8321e78ac21SJeffrey Hsu } 8331e78ac21SJeffrey Hsu 8341e78ac21SJeffrey Hsu firstlen = off - hlen; 8351e78ac21SJeffrey Hsu mnext = &m0->m_nextpkt; /* pointer to next packet */ 8361e78ac21SJeffrey Hsu 8371e78ac21SJeffrey Hsu /* 8381e78ac21SJeffrey Hsu * Loop through length of segment after first fragment, 8391e78ac21SJeffrey Hsu * make new header and copy data of each part and link onto chain. 8401e78ac21SJeffrey Hsu * Here, m0 is the original packet, m is the fragment being created. 8411e78ac21SJeffrey Hsu * The fragments are linked off the m_nextpkt of the original 8421e78ac21SJeffrey Hsu * packet, which after processing serves as the first fragment. 8431e78ac21SJeffrey Hsu */ 84421d172a3SGleb Smirnoff for (nfrags = 1; off < ip_len; off += len, nfrags++) { 8451e78ac21SJeffrey Hsu struct ip *mhip; /* ip header on the fragment */ 8461e78ac21SJeffrey Hsu struct mbuf *m; 8471e78ac21SJeffrey Hsu int mhlen = sizeof (struct ip); 8481e78ac21SJeffrey Hsu 849dc4ad05eSGleb Smirnoff m = m_gethdr(M_NOWAIT, MT_DATA); 8500b17fba7SAndre Oppermann if (m == NULL) { 8511e78ac21SJeffrey Hsu error = ENOBUFS; 85286425c62SRobert Watson IPSTAT_INC(ips_odropped); 8531e78ac21SJeffrey Hsu goto done; 8541e78ac21SJeffrey Hsu } 855c4c4346fSHans Petter Selasky /* 856c4c4346fSHans Petter Selasky * Make sure the complete packet header gets copied 857c4c4346fSHans Petter Selasky * from the originating mbuf to the newly created 858c4c4346fSHans Petter Selasky * mbuf. This also ensures that existing firewall 859c4c4346fSHans Petter Selasky * classification(s), VLAN tags and so on get copied 860c4c4346fSHans Petter Selasky * to the resulting fragmented packet(s): 861c4c4346fSHans Petter Selasky */ 862c4c4346fSHans Petter Selasky if (m_dup_pkthdr(m, m0, M_NOWAIT) == 0) { 863c4c4346fSHans Petter Selasky m_free(m); 864c4c4346fSHans Petter Selasky error = ENOBUFS; 865c4c4346fSHans Petter Selasky IPSTAT_INC(ips_odropped); 866c4c4346fSHans Petter Selasky goto done; 867c4c4346fSHans Petter Selasky } 8681e78ac21SJeffrey Hsu /* 8691e78ac21SJeffrey Hsu * In the first mbuf, leave room for the link header, then 8701e78ac21SJeffrey Hsu * copy the original IP header including options. The payload 8718b889dbbSBruce M Simpson * goes into an additional mbuf chain returned by m_copym(). 8721e78ac21SJeffrey Hsu */ 8731e78ac21SJeffrey Hsu m->m_data += max_linkhdr; 8741e78ac21SJeffrey Hsu mhip = mtod(m, struct ip *); 8751e78ac21SJeffrey Hsu *mhip = *ip; 8761e78ac21SJeffrey Hsu if (hlen > sizeof (struct ip)) { 8771e78ac21SJeffrey Hsu mhlen = ip_optcopy(ip, mhip) + sizeof (struct ip); 8781e78ac21SJeffrey Hsu mhip->ip_v = IPVERSION; 8791e78ac21SJeffrey Hsu mhip->ip_hl = mhlen >> 2; 8801e78ac21SJeffrey Hsu } 8811e78ac21SJeffrey Hsu m->m_len = mhlen; 88221d172a3SGleb Smirnoff /* XXX do we need to add ip_off below ? */ 88321d172a3SGleb Smirnoff mhip->ip_off = ((off - hlen) >> 3) + ip_off; 884fb86dfcdSAndre Oppermann if (off + len >= ip_len) 88521d172a3SGleb Smirnoff len = ip_len - off; 886fb86dfcdSAndre Oppermann else 8871e78ac21SJeffrey Hsu mhip->ip_off |= IP_MF; 8881e78ac21SJeffrey Hsu mhip->ip_len = htons((u_short)(len + mhlen)); 889eb1b1807SGleb Smirnoff m->m_next = m_copym(m0, off, len, M_NOWAIT); 8900b17fba7SAndre Oppermann if (m->m_next == NULL) { /* copy failed */ 8911e78ac21SJeffrey Hsu m_free(m); 8921e78ac21SJeffrey Hsu error = ENOBUFS; /* ??? */ 89386425c62SRobert Watson IPSTAT_INC(ips_odropped); 8941e78ac21SJeffrey Hsu goto done; 8951e78ac21SJeffrey Hsu } 8961e78ac21SJeffrey Hsu m->m_pkthdr.len = mhlen + len; 8971e78ac21SJeffrey Hsu #ifdef MAC 89830d239bcSRobert Watson mac_netinet_fragment(m0, m); 8991e78ac21SJeffrey Hsu #endif 9001e78ac21SJeffrey Hsu mhip->ip_off = htons(mhip->ip_off); 9011e78ac21SJeffrey Hsu mhip->ip_sum = 0; 902078468edSGleb Smirnoff if (m->m_pkthdr.csum_flags & CSUM_IP & ~if_hwassist_flags) { 9031e78ac21SJeffrey Hsu mhip->ip_sum = in_cksum(m, mhlen); 904078468edSGleb Smirnoff m->m_pkthdr.csum_flags &= ~CSUM_IP; 905078468edSGleb Smirnoff } 9061e78ac21SJeffrey Hsu *mnext = m; 9071e78ac21SJeffrey Hsu mnext = &m->m_nextpkt; 9081e78ac21SJeffrey Hsu } 90986425c62SRobert Watson IPSTAT_ADD(ips_ofragments, nfrags); 9101e78ac21SJeffrey Hsu 9111e78ac21SJeffrey Hsu /* 9121e78ac21SJeffrey Hsu * Update first fragment by trimming what's been copied out 9131e78ac21SJeffrey Hsu * and updating header. 9141e78ac21SJeffrey Hsu */ 91521d172a3SGleb Smirnoff m_adj(m0, hlen + firstlen - ip_len); 9161e78ac21SJeffrey Hsu m0->m_pkthdr.len = hlen + firstlen; 9171e78ac21SJeffrey Hsu ip->ip_len = htons((u_short)m0->m_pkthdr.len); 91821d172a3SGleb Smirnoff ip->ip_off = htons(ip_off | IP_MF); 9191e78ac21SJeffrey Hsu ip->ip_sum = 0; 920078468edSGleb Smirnoff if (m0->m_pkthdr.csum_flags & CSUM_IP & ~if_hwassist_flags) { 9211e78ac21SJeffrey Hsu ip->ip_sum = in_cksum(m0, hlen); 922078468edSGleb Smirnoff m0->m_pkthdr.csum_flags &= ~CSUM_IP; 923078468edSGleb Smirnoff } 9241e78ac21SJeffrey Hsu 9251e78ac21SJeffrey Hsu done: 9261e78ac21SJeffrey Hsu *m_frag = m0; 9271e78ac21SJeffrey Hsu return error; 9281e78ac21SJeffrey Hsu } 9291e78ac21SJeffrey Hsu 9301c238475SJonathan Lemon void 931db4f9cc7SJonathan Lemon in_delayed_cksum(struct mbuf *m) 932db4f9cc7SJonathan Lemon { 933db4f9cc7SJonathan Lemon struct ip *ip; 93423e9c6dcSGleb Smirnoff uint16_t csum, offset, ip_len; 935db4f9cc7SJonathan Lemon 936db4f9cc7SJonathan Lemon ip = mtod(m, struct ip *); 93753be11f6SPoul-Henning Kamp offset = ip->ip_hl << 2 ; 93823e9c6dcSGleb Smirnoff ip_len = ntohs(ip->ip_len); 93923e9c6dcSGleb Smirnoff csum = in_cksum_skip(m, ip_len, offset); 940206a3274SRuslan Ermilov if (m->m_pkthdr.csum_flags & CSUM_UDP && csum == 0) 941206a3274SRuslan Ermilov csum = 0xffff; 942db4f9cc7SJonathan Lemon offset += m->m_pkthdr.csum_data; /* checksum offset */ 943db4f9cc7SJonathan Lemon 9448e1d0a56SMichael Tuexen /* find the mbuf in the chain where the checksum starts*/ 9458e1d0a56SMichael Tuexen while ((m != NULL) && (offset >= m->m_len)) { 9468e1d0a56SMichael Tuexen offset -= m->m_len; 9478e1d0a56SMichael Tuexen m = m->m_next; 9488e1d0a56SMichael Tuexen } 94926461454SMichael Tuexen KASSERT(m != NULL, ("in_delayed_cksum: checksum outside mbuf chain.")); 95026461454SMichael Tuexen KASSERT(offset + sizeof(u_short) <= m->m_len, ("in_delayed_cksum: checksum split between mbufs.")); 951db4f9cc7SJonathan Lemon *(u_short *)(m->m_data + offset) = csum; 952db4f9cc7SJonathan Lemon } 953db4f9cc7SJonathan Lemon 954df8bae1dSRodney W. Grimes /* 955df8bae1dSRodney W. Grimes * IP socket option processing. 956df8bae1dSRodney W. Grimes */ 957df8bae1dSRodney W. Grimes int 958f2565d68SRobert Watson ip_ctloutput(struct socket *so, struct sockopt *sopt) 959df8bae1dSRodney W. Grimes { 960cfe8b629SGarrett Wollman struct inpcb *inp = sotoinpcb(so); 961cfe8b629SGarrett Wollman int error, optval; 962dc847eb6SAdrian Chadd #ifdef RSS 963dc847eb6SAdrian Chadd uint32_t rss_bucket; 964dc847eb6SAdrian Chadd int retval; 965dc847eb6SAdrian Chadd #endif 966df8bae1dSRodney W. Grimes 967cfe8b629SGarrett Wollman error = optval = 0; 968cfe8b629SGarrett Wollman if (sopt->sopt_level != IPPROTO_IP) { 969fc06cd42SMikolaj Golub error = EINVAL; 970fc06cd42SMikolaj Golub 971fc06cd42SMikolaj Golub if (sopt->sopt_level == SOL_SOCKET && 972fc06cd42SMikolaj Golub sopt->sopt_dir == SOPT_SET) { 973fc06cd42SMikolaj Golub switch (sopt->sopt_name) { 974fc06cd42SMikolaj Golub case SO_REUSEADDR: 975fc06cd42SMikolaj Golub INP_WLOCK(inp); 976efdf104bSMikolaj Golub if ((so->so_options & SO_REUSEADDR) != 0) 977efdf104bSMikolaj Golub inp->inp_flags2 |= INP_REUSEADDR; 978fc06cd42SMikolaj Golub else 979efdf104bSMikolaj Golub inp->inp_flags2 &= ~INP_REUSEADDR; 980fc06cd42SMikolaj Golub INP_WUNLOCK(inp); 981fc06cd42SMikolaj Golub error = 0; 982fc06cd42SMikolaj Golub break; 983fc06cd42SMikolaj Golub case SO_REUSEPORT: 984fc06cd42SMikolaj Golub INP_WLOCK(inp); 985fc06cd42SMikolaj Golub if ((so->so_options & SO_REUSEPORT) != 0) 986fc06cd42SMikolaj Golub inp->inp_flags2 |= INP_REUSEPORT; 987fc06cd42SMikolaj Golub else 988fc06cd42SMikolaj Golub inp->inp_flags2 &= ~INP_REUSEPORT; 989fc06cd42SMikolaj Golub INP_WUNLOCK(inp); 990fc06cd42SMikolaj Golub error = 0; 991fc06cd42SMikolaj Golub break; 992fc06cd42SMikolaj Golub case SO_SETFIB: 993fc06cd42SMikolaj Golub INP_WLOCK(inp); 994fc06cd42SMikolaj Golub inp->inp_inc.inc_fibnum = so->so_fibnum; 995fc06cd42SMikolaj Golub INP_WUNLOCK(inp); 996fc06cd42SMikolaj Golub error = 0; 997fc06cd42SMikolaj Golub break; 998f3e7afe2SHans Petter Selasky case SO_MAX_PACING_RATE: 999f3e7afe2SHans Petter Selasky #ifdef RATELIMIT 1000f3e7afe2SHans Petter Selasky INP_WLOCK(inp); 1001f3e7afe2SHans Petter Selasky inp->inp_flags2 |= INP_RATE_LIMIT_CHANGED; 1002f3e7afe2SHans Petter Selasky INP_WUNLOCK(inp); 1003f3e7afe2SHans Petter Selasky error = 0; 1004f3e7afe2SHans Petter Selasky #else 1005f3e7afe2SHans Petter Selasky error = EOPNOTSUPP; 1006f3e7afe2SHans Petter Selasky #endif 1007f3e7afe2SHans Petter Selasky break; 1008fc06cd42SMikolaj Golub default: 1009fc06cd42SMikolaj Golub break; 1010fc06cd42SMikolaj Golub } 1011fc06cd42SMikolaj Golub } 1012fc06cd42SMikolaj Golub return (error); 1013cfe8b629SGarrett Wollman } 1014df8bae1dSRodney W. Grimes 1015cfe8b629SGarrett Wollman switch (sopt->sopt_dir) { 1016cfe8b629SGarrett Wollman case SOPT_SET: 1017cfe8b629SGarrett Wollman switch (sopt->sopt_name) { 1018df8bae1dSRodney W. Grimes case IP_OPTIONS: 1019df8bae1dSRodney W. Grimes #ifdef notyet 1020df8bae1dSRodney W. Grimes case IP_RETOPTS: 1021df8bae1dSRodney W. Grimes #endif 1022cfe8b629SGarrett Wollman { 1023cfe8b629SGarrett Wollman struct mbuf *m; 1024cfe8b629SGarrett Wollman if (sopt->sopt_valsize > MLEN) { 1025cfe8b629SGarrett Wollman error = EMSGSIZE; 1026cfe8b629SGarrett Wollman break; 1027cfe8b629SGarrett Wollman } 1028dc4ad05eSGleb Smirnoff m = m_get(sopt->sopt_td ? M_WAITOK : M_NOWAIT, MT_DATA); 10290b17fba7SAndre Oppermann if (m == NULL) { 1030cfe8b629SGarrett Wollman error = ENOBUFS; 1031cfe8b629SGarrett Wollman break; 1032cfe8b629SGarrett Wollman } 1033cfe8b629SGarrett Wollman m->m_len = sopt->sopt_valsize; 1034cfe8b629SGarrett Wollman error = sooptcopyin(sopt, mtod(m, char *), m->m_len, 1035cfe8b629SGarrett Wollman m->m_len); 1036635354c4SMaxim Konovalov if (error) { 1037635354c4SMaxim Konovalov m_free(m); 1038635354c4SMaxim Konovalov break; 1039635354c4SMaxim Konovalov } 10408501a69cSRobert Watson INP_WLOCK(inp); 1041993d9505SRobert Watson error = ip_pcbopts(inp, sopt->sopt_name, m); 10428501a69cSRobert Watson INP_WUNLOCK(inp); 1043993d9505SRobert Watson return (error); 1044cfe8b629SGarrett Wollman } 1045df8bae1dSRodney W. Grimes 1046f44270e7SPawel Jakub Dawidek case IP_BINDANY: 1047f44270e7SPawel Jakub Dawidek if (sopt->sopt_td != NULL) { 1048f44270e7SPawel Jakub Dawidek error = priv_check(sopt->sopt_td, 1049f44270e7SPawel Jakub Dawidek PRIV_NETINET_BINDANY); 1050f44270e7SPawel Jakub Dawidek if (error) 1051be9347e3SAdrian Chadd break; 1052be9347e3SAdrian Chadd } 1053cef27294SAdrian Chadd /* FALLTHROUGH */ 10540a100a6fSAdrian Chadd case IP_BINDMULTI: 10550a100a6fSAdrian Chadd #ifdef RSS 10560a100a6fSAdrian Chadd case IP_RSS_LISTEN_BUCKET: 10570a100a6fSAdrian Chadd #endif 1058df8bae1dSRodney W. Grimes case IP_TOS: 1059df8bae1dSRodney W. Grimes case IP_TTL: 1060936cd18dSAndre Oppermann case IP_MINTTL: 1061df8bae1dSRodney W. Grimes case IP_RECVOPTS: 1062df8bae1dSRodney W. Grimes case IP_RECVRETOPTS: 1063dce33a45SErmal Luçi case IP_ORIGDSTADDR: 1064df8bae1dSRodney W. Grimes case IP_RECVDSTADDR: 10654957466bSMatthew N. Dodd case IP_RECVTTL: 106682c23ebaSBill Fenner case IP_RECVIF: 10678afa2304SBruce M Simpson case IP_ONESBCAST: 1068b2828ad2SAndre Oppermann case IP_DONTFRAG: 10693cca425bSMichael Tuexen case IP_RECVTOS: 10709d3ddf43SAdrian Chadd case IP_RECVFLOWID: 10719d3ddf43SAdrian Chadd #ifdef RSS 10729d3ddf43SAdrian Chadd case IP_RECVRSSBUCKETID: 10739d3ddf43SAdrian Chadd #endif 1074cfe8b629SGarrett Wollman error = sooptcopyin(sopt, &optval, sizeof optval, 1075cfe8b629SGarrett Wollman sizeof optval); 1076cfe8b629SGarrett Wollman if (error) 1077cfe8b629SGarrett Wollman break; 1078df8bae1dSRodney W. Grimes 1079cfe8b629SGarrett Wollman switch (sopt->sopt_name) { 1080df8bae1dSRodney W. Grimes case IP_TOS: 1081ca98b82cSDavid Greenman inp->inp_ip_tos = optval; 1082df8bae1dSRodney W. Grimes break; 1083df8bae1dSRodney W. Grimes 1084df8bae1dSRodney W. Grimes case IP_TTL: 1085ca98b82cSDavid Greenman inp->inp_ip_ttl = optval; 1086df8bae1dSRodney W. Grimes break; 1087936cd18dSAndre Oppermann 1088936cd18dSAndre Oppermann case IP_MINTTL: 1089a603c811SRobert Watson if (optval >= 0 && optval <= MAXTTL) 1090936cd18dSAndre Oppermann inp->inp_ip_minttl = optval; 1091936cd18dSAndre Oppermann else 1092936cd18dSAndre Oppermann error = EINVAL; 1093936cd18dSAndre Oppermann break; 1094936cd18dSAndre Oppermann 1095a138d217SRobert Watson #define OPTSET(bit) do { \ 10968501a69cSRobert Watson INP_WLOCK(inp); \ 1097df8bae1dSRodney W. Grimes if (optval) \ 1098df8bae1dSRodney W. Grimes inp->inp_flags |= bit; \ 1099df8bae1dSRodney W. Grimes else \ 1100a138d217SRobert Watson inp->inp_flags &= ~bit; \ 11018501a69cSRobert Watson INP_WUNLOCK(inp); \ 1102a138d217SRobert Watson } while (0) 1103df8bae1dSRodney W. Grimes 11040a100a6fSAdrian Chadd #define OPTSET2(bit, val) do { \ 11050a100a6fSAdrian Chadd INP_WLOCK(inp); \ 11060a100a6fSAdrian Chadd if (val) \ 11070a100a6fSAdrian Chadd inp->inp_flags2 |= bit; \ 11080a100a6fSAdrian Chadd else \ 11090a100a6fSAdrian Chadd inp->inp_flags2 &= ~bit; \ 11100a100a6fSAdrian Chadd INP_WUNLOCK(inp); \ 11110a100a6fSAdrian Chadd } while (0) 11120a100a6fSAdrian Chadd 1113df8bae1dSRodney W. Grimes case IP_RECVOPTS: 1114df8bae1dSRodney W. Grimes OPTSET(INP_RECVOPTS); 1115df8bae1dSRodney W. Grimes break; 1116df8bae1dSRodney W. Grimes 1117df8bae1dSRodney W. Grimes case IP_RECVRETOPTS: 1118df8bae1dSRodney W. Grimes OPTSET(INP_RECVRETOPTS); 1119df8bae1dSRodney W. Grimes break; 1120df8bae1dSRodney W. Grimes 1121df8bae1dSRodney W. Grimes case IP_RECVDSTADDR: 1122df8bae1dSRodney W. Grimes OPTSET(INP_RECVDSTADDR); 1123df8bae1dSRodney W. Grimes break; 112482c23ebaSBill Fenner 1125dce33a45SErmal Luçi case IP_ORIGDSTADDR: 1126dce33a45SErmal Luçi OPTSET2(INP_ORIGDSTADDR, optval); 1127dce33a45SErmal Luçi break; 1128dce33a45SErmal Luçi 11294957466bSMatthew N. Dodd case IP_RECVTTL: 11304957466bSMatthew N. Dodd OPTSET(INP_RECVTTL); 11314957466bSMatthew N. Dodd break; 11324957466bSMatthew N. Dodd 113382c23ebaSBill Fenner case IP_RECVIF: 113482c23ebaSBill Fenner OPTSET(INP_RECVIF); 113582c23ebaSBill Fenner break; 11366a800098SYoshinobu Inoue 11378afa2304SBruce M Simpson case IP_ONESBCAST: 11388afa2304SBruce M Simpson OPTSET(INP_ONESBCAST); 11398afa2304SBruce M Simpson break; 1140b2828ad2SAndre Oppermann case IP_DONTFRAG: 1141b2828ad2SAndre Oppermann OPTSET(INP_DONTFRAG); 1142b2828ad2SAndre Oppermann break; 1143f44270e7SPawel Jakub Dawidek case IP_BINDANY: 1144f44270e7SPawel Jakub Dawidek OPTSET(INP_BINDANY); 1145be9347e3SAdrian Chadd break; 11463cca425bSMichael Tuexen case IP_RECVTOS: 11473cca425bSMichael Tuexen OPTSET(INP_RECVTOS); 11483cca425bSMichael Tuexen break; 11490a100a6fSAdrian Chadd case IP_BINDMULTI: 11500a100a6fSAdrian Chadd OPTSET2(INP_BINDMULTI, optval); 11510a100a6fSAdrian Chadd break; 11529d3ddf43SAdrian Chadd case IP_RECVFLOWID: 11539d3ddf43SAdrian Chadd OPTSET2(INP_RECVFLOWID, optval); 11549d3ddf43SAdrian Chadd break; 11550a100a6fSAdrian Chadd #ifdef RSS 11560a100a6fSAdrian Chadd case IP_RSS_LISTEN_BUCKET: 11570a100a6fSAdrian Chadd if ((optval >= 0) && 11580a100a6fSAdrian Chadd (optval < rss_getnumbuckets())) { 11590a100a6fSAdrian Chadd inp->inp_rss_listen_bucket = optval; 11600a100a6fSAdrian Chadd OPTSET2(INP_RSS_BUCKET_SET, 1); 11610a100a6fSAdrian Chadd } else { 11620a100a6fSAdrian Chadd error = EINVAL; 11630a100a6fSAdrian Chadd } 11640a100a6fSAdrian Chadd break; 11659d3ddf43SAdrian Chadd case IP_RECVRSSBUCKETID: 11669d3ddf43SAdrian Chadd OPTSET2(INP_RECVRSSBUCKETID, optval); 11679d3ddf43SAdrian Chadd break; 11680a100a6fSAdrian Chadd #endif 1169df8bae1dSRodney W. Grimes } 1170df8bae1dSRodney W. Grimes break; 1171df8bae1dSRodney W. Grimes #undef OPTSET 11720a100a6fSAdrian Chadd #undef OPTSET2 1173df8bae1dSRodney W. Grimes 117471498f30SBruce M Simpson /* 117571498f30SBruce M Simpson * Multicast socket options are processed by the in_mcast 117671498f30SBruce M Simpson * module. 117771498f30SBruce M Simpson */ 1178df8bae1dSRodney W. Grimes case IP_MULTICAST_IF: 1179f0068c4aSGarrett Wollman case IP_MULTICAST_VIF: 1180df8bae1dSRodney W. Grimes case IP_MULTICAST_TTL: 1181df8bae1dSRodney W. Grimes case IP_MULTICAST_LOOP: 1182df8bae1dSRodney W. Grimes case IP_ADD_MEMBERSHIP: 1183df8bae1dSRodney W. Grimes case IP_DROP_MEMBERSHIP: 118471498f30SBruce M Simpson case IP_ADD_SOURCE_MEMBERSHIP: 118571498f30SBruce M Simpson case IP_DROP_SOURCE_MEMBERSHIP: 118671498f30SBruce M Simpson case IP_BLOCK_SOURCE: 118771498f30SBruce M Simpson case IP_UNBLOCK_SOURCE: 118871498f30SBruce M Simpson case IP_MSFILTER: 118971498f30SBruce M Simpson case MCAST_JOIN_GROUP: 119071498f30SBruce M Simpson case MCAST_LEAVE_GROUP: 119171498f30SBruce M Simpson case MCAST_JOIN_SOURCE_GROUP: 119271498f30SBruce M Simpson case MCAST_LEAVE_SOURCE_GROUP: 119371498f30SBruce M Simpson case MCAST_BLOCK_SOURCE: 119471498f30SBruce M Simpson case MCAST_UNBLOCK_SOURCE: 119571498f30SBruce M Simpson error = inp_setmoptions(inp, sopt); 1196df8bae1dSRodney W. Grimes break; 1197df8bae1dSRodney W. Grimes 119833b3ac06SPeter Wemm case IP_PORTRANGE: 1199cfe8b629SGarrett Wollman error = sooptcopyin(sopt, &optval, sizeof optval, 1200cfe8b629SGarrett Wollman sizeof optval); 1201cfe8b629SGarrett Wollman if (error) 1202cfe8b629SGarrett Wollman break; 120333b3ac06SPeter Wemm 12048501a69cSRobert Watson INP_WLOCK(inp); 120533b3ac06SPeter Wemm switch (optval) { 120633b3ac06SPeter Wemm case IP_PORTRANGE_DEFAULT: 120733b3ac06SPeter Wemm inp->inp_flags &= ~(INP_LOWPORT); 120833b3ac06SPeter Wemm inp->inp_flags &= ~(INP_HIGHPORT); 120933b3ac06SPeter Wemm break; 121033b3ac06SPeter Wemm 121133b3ac06SPeter Wemm case IP_PORTRANGE_HIGH: 121233b3ac06SPeter Wemm inp->inp_flags &= ~(INP_LOWPORT); 121333b3ac06SPeter Wemm inp->inp_flags |= INP_HIGHPORT; 121433b3ac06SPeter Wemm break; 121533b3ac06SPeter Wemm 121633b3ac06SPeter Wemm case IP_PORTRANGE_LOW: 121733b3ac06SPeter Wemm inp->inp_flags &= ~(INP_HIGHPORT); 121833b3ac06SPeter Wemm inp->inp_flags |= INP_LOWPORT; 121933b3ac06SPeter Wemm break; 122033b3ac06SPeter Wemm 122133b3ac06SPeter Wemm default: 122233b3ac06SPeter Wemm error = EINVAL; 122333b3ac06SPeter Wemm break; 122433b3ac06SPeter Wemm } 12258501a69cSRobert Watson INP_WUNLOCK(inp); 1226ce8c72b1SPeter Wemm break; 122733b3ac06SPeter Wemm 1228fcf59617SAndrey V. Elsukov #if defined(IPSEC) || defined(IPSEC_SUPPORT) 12296a800098SYoshinobu Inoue case IP_IPSEC_POLICY: 1230fcf59617SAndrey V. Elsukov if (IPSEC_ENABLED(ipv4)) { 1231fcf59617SAndrey V. Elsukov error = IPSEC_PCBCTL(ipv4, inp, sopt); 12326a800098SYoshinobu Inoue break; 12336a800098SYoshinobu Inoue } 1234fcf59617SAndrey V. Elsukov /* FALLTHROUGH */ 1235b2630c29SGeorge V. Neville-Neil #endif /* IPSEC */ 12366a800098SYoshinobu Inoue 1237df8bae1dSRodney W. Grimes default: 1238df8bae1dSRodney W. Grimes error = ENOPROTOOPT; 1239df8bae1dSRodney W. Grimes break; 1240df8bae1dSRodney W. Grimes } 1241df8bae1dSRodney W. Grimes break; 1242df8bae1dSRodney W. Grimes 1243cfe8b629SGarrett Wollman case SOPT_GET: 1244cfe8b629SGarrett Wollman switch (sopt->sopt_name) { 1245df8bae1dSRodney W. Grimes case IP_OPTIONS: 1246df8bae1dSRodney W. Grimes case IP_RETOPTS: 1247cfe8b629SGarrett Wollman if (inp->inp_options) 1248cfe8b629SGarrett Wollman error = sooptcopyout(sopt, 1249cfe8b629SGarrett Wollman mtod(inp->inp_options, 1250cfe8b629SGarrett Wollman char *), 1251cfe8b629SGarrett Wollman inp->inp_options->m_len); 1252cfe8b629SGarrett Wollman else 1253cfe8b629SGarrett Wollman sopt->sopt_valsize = 0; 1254df8bae1dSRodney W. Grimes break; 1255df8bae1dSRodney W. Grimes 1256df8bae1dSRodney W. Grimes case IP_TOS: 1257df8bae1dSRodney W. Grimes case IP_TTL: 1258936cd18dSAndre Oppermann case IP_MINTTL: 1259df8bae1dSRodney W. Grimes case IP_RECVOPTS: 1260df8bae1dSRodney W. Grimes case IP_RECVRETOPTS: 1261dce33a45SErmal Luçi case IP_ORIGDSTADDR: 1262df8bae1dSRodney W. Grimes case IP_RECVDSTADDR: 12634957466bSMatthew N. Dodd case IP_RECVTTL: 126482c23ebaSBill Fenner case IP_RECVIF: 1265cfe8b629SGarrett Wollman case IP_PORTRANGE: 12668afa2304SBruce M Simpson case IP_ONESBCAST: 1267b2828ad2SAndre Oppermann case IP_DONTFRAG: 12685f6bf451SAttilio Rao case IP_BINDANY: 12693cca425bSMichael Tuexen case IP_RECVTOS: 12700a100a6fSAdrian Chadd case IP_BINDMULTI: 12719c423972SAdrian Chadd case IP_FLOWID: 12729c423972SAdrian Chadd case IP_FLOWTYPE: 12739d3ddf43SAdrian Chadd case IP_RECVFLOWID: 12740a100a6fSAdrian Chadd #ifdef RSS 12750a100a6fSAdrian Chadd case IP_RSSBUCKETID: 12769d3ddf43SAdrian Chadd case IP_RECVRSSBUCKETID: 12770a100a6fSAdrian Chadd #endif 1278cfe8b629SGarrett Wollman switch (sopt->sopt_name) { 1279df8bae1dSRodney W. Grimes 1280df8bae1dSRodney W. Grimes case IP_TOS: 1281ca98b82cSDavid Greenman optval = inp->inp_ip_tos; 1282df8bae1dSRodney W. Grimes break; 1283df8bae1dSRodney W. Grimes 1284df8bae1dSRodney W. Grimes case IP_TTL: 1285ca98b82cSDavid Greenman optval = inp->inp_ip_ttl; 1286df8bae1dSRodney W. Grimes break; 1287df8bae1dSRodney W. Grimes 1288936cd18dSAndre Oppermann case IP_MINTTL: 1289936cd18dSAndre Oppermann optval = inp->inp_ip_minttl; 1290936cd18dSAndre Oppermann break; 1291936cd18dSAndre Oppermann 1292df8bae1dSRodney W. Grimes #define OPTBIT(bit) (inp->inp_flags & bit ? 1 : 0) 12930a100a6fSAdrian Chadd #define OPTBIT2(bit) (inp->inp_flags2 & bit ? 1 : 0) 1294df8bae1dSRodney W. Grimes 1295df8bae1dSRodney W. Grimes case IP_RECVOPTS: 1296df8bae1dSRodney W. Grimes optval = OPTBIT(INP_RECVOPTS); 1297df8bae1dSRodney W. Grimes break; 1298df8bae1dSRodney W. Grimes 1299df8bae1dSRodney W. Grimes case IP_RECVRETOPTS: 1300df8bae1dSRodney W. Grimes optval = OPTBIT(INP_RECVRETOPTS); 1301df8bae1dSRodney W. Grimes break; 1302df8bae1dSRodney W. Grimes 1303df8bae1dSRodney W. Grimes case IP_RECVDSTADDR: 1304df8bae1dSRodney W. Grimes optval = OPTBIT(INP_RECVDSTADDR); 1305df8bae1dSRodney W. Grimes break; 130682c23ebaSBill Fenner 1307dce33a45SErmal Luçi case IP_ORIGDSTADDR: 1308dce33a45SErmal Luçi optval = OPTBIT2(INP_ORIGDSTADDR); 1309dce33a45SErmal Luçi break; 1310dce33a45SErmal Luçi 13114957466bSMatthew N. Dodd case IP_RECVTTL: 13124957466bSMatthew N. Dodd optval = OPTBIT(INP_RECVTTL); 13134957466bSMatthew N. Dodd break; 13144957466bSMatthew N. Dodd 131582c23ebaSBill Fenner case IP_RECVIF: 131682c23ebaSBill Fenner optval = OPTBIT(INP_RECVIF); 131782c23ebaSBill Fenner break; 1318cfe8b629SGarrett Wollman 1319cfe8b629SGarrett Wollman case IP_PORTRANGE: 1320cfe8b629SGarrett Wollman if (inp->inp_flags & INP_HIGHPORT) 1321cfe8b629SGarrett Wollman optval = IP_PORTRANGE_HIGH; 1322cfe8b629SGarrett Wollman else if (inp->inp_flags & INP_LOWPORT) 1323cfe8b629SGarrett Wollman optval = IP_PORTRANGE_LOW; 1324cfe8b629SGarrett Wollman else 1325cfe8b629SGarrett Wollman optval = 0; 1326cfe8b629SGarrett Wollman break; 13276a800098SYoshinobu Inoue 13288afa2304SBruce M Simpson case IP_ONESBCAST: 13298afa2304SBruce M Simpson optval = OPTBIT(INP_ONESBCAST); 13308afa2304SBruce M Simpson break; 1331b2828ad2SAndre Oppermann case IP_DONTFRAG: 1332b2828ad2SAndre Oppermann optval = OPTBIT(INP_DONTFRAG); 1333b2828ad2SAndre Oppermann break; 13345f6bf451SAttilio Rao case IP_BINDANY: 13355f6bf451SAttilio Rao optval = OPTBIT(INP_BINDANY); 13365f6bf451SAttilio Rao break; 13373cca425bSMichael Tuexen case IP_RECVTOS: 13383cca425bSMichael Tuexen optval = OPTBIT(INP_RECVTOS); 13393cca425bSMichael Tuexen break; 13409c423972SAdrian Chadd case IP_FLOWID: 13419c423972SAdrian Chadd optval = inp->inp_flowid; 13429c423972SAdrian Chadd break; 13439c423972SAdrian Chadd case IP_FLOWTYPE: 13449c423972SAdrian Chadd optval = inp->inp_flowtype; 13459c423972SAdrian Chadd break; 13469d3ddf43SAdrian Chadd case IP_RECVFLOWID: 13479d3ddf43SAdrian Chadd optval = OPTBIT2(INP_RECVFLOWID); 13489d3ddf43SAdrian Chadd break; 13499c423972SAdrian Chadd #ifdef RSS 13507847796aSAdrian Chadd case IP_RSSBUCKETID: 13517847796aSAdrian Chadd retval = rss_hash2bucket(inp->inp_flowid, 13527847796aSAdrian Chadd inp->inp_flowtype, 13537847796aSAdrian Chadd &rss_bucket); 13547847796aSAdrian Chadd if (retval == 0) 13557847796aSAdrian Chadd optval = rss_bucket; 13567847796aSAdrian Chadd else 13577847796aSAdrian Chadd error = EINVAL; 13589c423972SAdrian Chadd break; 13599d3ddf43SAdrian Chadd case IP_RECVRSSBUCKETID: 13609d3ddf43SAdrian Chadd optval = OPTBIT2(INP_RECVRSSBUCKETID); 13619d3ddf43SAdrian Chadd break; 13629c423972SAdrian Chadd #endif 13630a100a6fSAdrian Chadd case IP_BINDMULTI: 13640a100a6fSAdrian Chadd optval = OPTBIT2(INP_BINDMULTI); 13650a100a6fSAdrian Chadd break; 1366df8bae1dSRodney W. Grimes } 1367cfe8b629SGarrett Wollman error = sooptcopyout(sopt, &optval, sizeof optval); 1368df8bae1dSRodney W. Grimes break; 1369df8bae1dSRodney W. Grimes 137071498f30SBruce M Simpson /* 137171498f30SBruce M Simpson * Multicast socket options are processed by the in_mcast 137271498f30SBruce M Simpson * module. 137371498f30SBruce M Simpson */ 1374df8bae1dSRodney W. Grimes case IP_MULTICAST_IF: 1375f0068c4aSGarrett Wollman case IP_MULTICAST_VIF: 1376df8bae1dSRodney W. Grimes case IP_MULTICAST_TTL: 1377df8bae1dSRodney W. Grimes case IP_MULTICAST_LOOP: 137871498f30SBruce M Simpson case IP_MSFILTER: 137971498f30SBruce M Simpson error = inp_getmoptions(inp, sopt); 138033b3ac06SPeter Wemm break; 138133b3ac06SPeter Wemm 1382fcf59617SAndrey V. Elsukov #if defined(IPSEC) || defined(IPSEC_SUPPORT) 13836a800098SYoshinobu Inoue case IP_IPSEC_POLICY: 1384fcf59617SAndrey V. Elsukov if (IPSEC_ENABLED(ipv4)) { 1385fcf59617SAndrey V. Elsukov error = IPSEC_PCBCTL(ipv4, inp, sopt); 13866a800098SYoshinobu Inoue break; 13876a800098SYoshinobu Inoue } 1388fcf59617SAndrey V. Elsukov /* FALLTHROUGH */ 1389b2630c29SGeorge V. Neville-Neil #endif /* IPSEC */ 13906a800098SYoshinobu Inoue 1391df8bae1dSRodney W. Grimes default: 1392df8bae1dSRodney W. Grimes error = ENOPROTOOPT; 1393df8bae1dSRodney W. Grimes break; 1394df8bae1dSRodney W. Grimes } 1395df8bae1dSRodney W. Grimes break; 1396df8bae1dSRodney W. Grimes } 1397df8bae1dSRodney W. Grimes return (error); 1398df8bae1dSRodney W. Grimes } 1399df8bae1dSRodney W. Grimes 1400df8bae1dSRodney W. Grimes /* 1401df8bae1dSRodney W. Grimes * Routine called from ip_output() to loop back a copy of an IP multicast 1402df8bae1dSRodney W. Grimes * packet to the input queue of a specified interface. Note that this 1403df8bae1dSRodney W. Grimes * calls the output routine of the loopback "driver", but with an interface 1404f5fea3ddSPaul Traina * pointer that might NOT be a loopback interface -- evil, but easier than 1405f5fea3ddSPaul Traina * replicating that code here. 1406df8bae1dSRodney W. Grimes */ 1407df8bae1dSRodney W. Grimes static void 1408331dff07SAlexander V. Chernikov ip_mloopback(struct ifnet *ifp, const struct mbuf *m, int hlen) 1409df8bae1dSRodney W. Grimes { 1410331dff07SAlexander V. Chernikov struct ip *ip; 1411df8bae1dSRodney W. Grimes struct mbuf *copym; 1412df8bae1dSRodney W. Grimes 1413e4762f75SGeorge V. Neville-Neil /* 1414e4762f75SGeorge V. Neville-Neil * Make a deep copy of the packet because we're going to 1415e4762f75SGeorge V. Neville-Neil * modify the pack in order to generate checksums. 1416e4762f75SGeorge V. Neville-Neil */ 1417eb1b1807SGleb Smirnoff copym = m_dup(m, M_NOWAIT); 1418f0cace5dSRobert Watson if (copym != NULL && (!M_WRITABLE(copym) || copym->m_len < hlen)) 141986b1d6d2SBill Fenner copym = m_pullup(copym, hlen); 1420df8bae1dSRodney W. Grimes if (copym != NULL) { 1421390cdc6aSRuslan Ermilov /* If needed, compute the checksum and mark it as valid. */ 1422390cdc6aSRuslan Ermilov if (copym->m_pkthdr.csum_flags & CSUM_DELAY_DATA) { 1423390cdc6aSRuslan Ermilov in_delayed_cksum(copym); 1424390cdc6aSRuslan Ermilov copym->m_pkthdr.csum_flags &= ~CSUM_DELAY_DATA; 1425390cdc6aSRuslan Ermilov copym->m_pkthdr.csum_flags |= 1426390cdc6aSRuslan Ermilov CSUM_DATA_VALID | CSUM_PSEUDO_HDR; 1427390cdc6aSRuslan Ermilov copym->m_pkthdr.csum_data = 0xffff; 1428390cdc6aSRuslan Ermilov } 1429df8bae1dSRodney W. Grimes /* 1430df8bae1dSRodney W. Grimes * We don't bother to fragment if the IP length is greater 1431df8bae1dSRodney W. Grimes * than the interface's MTU. Can this possibly matter? 1432df8bae1dSRodney W. Grimes */ 14338f134647SGleb Smirnoff ip = mtod(copym, struct ip *); 1434df8bae1dSRodney W. Grimes ip->ip_sum = 0; 143586b1d6d2SBill Fenner ip->ip_sum = in_cksum(copym, hlen); 1436331dff07SAlexander V. Chernikov if_simloop(ifp, copym, AF_INET, 0); 1437df8bae1dSRodney W. Grimes } 1438df8bae1dSRodney W. Grimes } 1439