1c398230bSWarner Losh /*- 251369649SPedro F. Giffuni * SPDX-License-Identifier: BSD-3-Clause 351369649SPedro 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" 386a800098SYoshinobu Inoue #include "opt_ipsec.h" 39b2e60773SJohn Baldwin #include "opt_kern_tls.h" 4053dcc544SMike Silbersack #include "opt_mbuf_stress_test.h" 4177a01441SJohn Baldwin #include "opt_ratelimit.h" 4257f60867SMark Johnston #include "opt_route.h" 439c423972SAdrian Chadd #include "opt_rss.h" 4477a01441SJohn Baldwin #include "opt_sctp.h" 45fbd1372aSJoerg Wunsch 46df8bae1dSRodney W. Grimes #include <sys/param.h> 4726f9a767SRodney W. Grimes #include <sys/systm.h> 48f0f6d643SLuigi Rizzo #include <sys/kernel.h> 49b2e60773SJohn Baldwin #include <sys/ktls.h> 50cc0a3c8cSAndrey V. Elsukov #include <sys/lock.h> 51df8bae1dSRodney W. Grimes #include <sys/malloc.h> 52df8bae1dSRodney W. Grimes #include <sys/mbuf.h> 53acd3428bSRobert Watson #include <sys/priv.h> 54c26fe973SBjoern A. Zeeb #include <sys/proc.h> 55df8bae1dSRodney W. Grimes #include <sys/protosw.h> 56cc0a3c8cSAndrey V. Elsukov #include <sys/rmlock.h> 5757f60867SMark Johnston #include <sys/sdt.h> 58df8bae1dSRodney W. Grimes #include <sys/socket.h> 59df8bae1dSRodney W. Grimes #include <sys/socketvar.h> 609d9edc56SMike Silbersack #include <sys/sysctl.h> 61c26fe973SBjoern A. Zeeb #include <sys/ucred.h> 62df8bae1dSRodney W. Grimes 63df8bae1dSRodney W. Grimes #include <net/if.h> 6476039bc8SGleb Smirnoff #include <net/if_var.h> 65868aabb4SRichard Scheffenegger #include <net/if_vlan_var.h> 663ef5e21dSQing Li #include <net/if_llatbl.h> 67868aabb4SRichard Scheffenegger #include <net/ethernet.h> 689b932e9eSAndre Oppermann #include <net/netisr.h> 69c21fd232SAndre Oppermann #include <net/pfil.h> 70df8bae1dSRodney W. Grimes #include <net/route.h> 71983066f0SAlexander V. Chernikov #include <net/route/nhop.h> 72b2bdc62aSAdrian Chadd #include <net/rss_config.h> 734b79449eSBjoern A. Zeeb #include <net/vnet.h> 74df8bae1dSRodney W. Grimes 75df8bae1dSRodney W. Grimes #include <netinet/in.h> 766ca363ebSGleb Smirnoff #include <netinet/in_fib.h> 7757f60867SMark Johnston #include <netinet/in_kdtrace.h> 78df8bae1dSRodney W. Grimes #include <netinet/in_systm.h> 79df8bae1dSRodney W. Grimes #include <netinet/ip.h> 80983066f0SAlexander V. Chernikov #include <netinet/in_fib.h> 81df8bae1dSRodney W. Grimes #include <netinet/in_pcb.h> 829c423972SAdrian Chadd #include <netinet/in_rss.h> 83df8bae1dSRodney W. Grimes #include <netinet/in_var.h> 84df8bae1dSRodney W. Grimes #include <netinet/ip_var.h> 85ef39adf0SAndre Oppermann #include <netinet/ip_options.h> 8665634ae7SWojciech Macek #include <netinet/ip_mroute.h> 871fdbfb90STom Jones 881fdbfb90STom Jones #include <netinet/udp.h> 891fdbfb90STom Jones #include <netinet/udp_var.h> 901fdbfb90STom Jones 9195033af9SMark Johnston #if defined(SCTP) || defined(SCTP_SUPPORT) 922f4afd21SRandall Stewart #include <netinet/sctp.h> 932f4afd21SRandall Stewart #include <netinet/sctp_crc32.h> 942f4afd21SRandall Stewart #endif 95df8bae1dSRodney W. Grimes 96fcf59617SAndrey V. Elsukov #include <netipsec/ipsec_support.h> 976a800098SYoshinobu Inoue 981dfcf0d2SAndre Oppermann #include <machine/in_cksum.h> 991dfcf0d2SAndre Oppermann 100aed55708SRobert Watson #include <security/mac/mac_framework.h> 101aed55708SRobert Watson 10253dcc544SMike Silbersack #ifdef MBUF_STRESS_TEST 10305b9d121SBjoern A. Zeeb static int mbuf_frag_size = 0; 1049d9edc56SMike Silbersack SYSCTL_INT(_net_inet_ip, OID_AUTO, mbuf_frag_size, CTLFLAG_RW, 1059d9edc56SMike Silbersack &mbuf_frag_size, 0, "Fragment outgoing mbufs to this size"); 1069d9edc56SMike Silbersack #endif 1079d9edc56SMike Silbersack 108331dff07SAlexander V. Chernikov static void ip_mloopback(struct ifnet *, const struct mbuf *, int); 109afed1b49SDarren Reed 1108b889dbbSBruce M Simpson extern int in_mcast_loop; 11193e0e116SJulian Elischer extern struct protosw inetsw[]; 11293e0e116SJulian Elischer 113d9f2a782SErmal Luçi static inline int 11405fc9d78SKristof Provost ip_output_pfil(struct mbuf **mp, struct ifnet *ifp, int flags, 11505fc9d78SKristof Provost struct inpcb *inp, struct sockaddr_in *dst, int *fibnum, int *error) 116d9f2a782SErmal Luçi { 117d9f2a782SErmal Luçi struct m_tag *fwd_tag = NULL; 1188f980c01SMark Johnston struct mbuf *m; 119d9f2a782SErmal Luçi struct in_addr odst; 120d9f2a782SErmal Luçi struct ip *ip; 12105fc9d78SKristof Provost int pflags = PFIL_OUT; 12205fc9d78SKristof Provost 12305fc9d78SKristof Provost if (flags & IP_FORWARDING) 12405fc9d78SKristof Provost pflags |= PFIL_FWD; 125d9f2a782SErmal Luçi 1268f980c01SMark Johnston m = *mp; 127d9f2a782SErmal Luçi ip = mtod(m, struct ip *); 128d9f2a782SErmal Luçi 129d9f2a782SErmal Luçi /* Run through list of hooks for output packets. */ 130d9f2a782SErmal Luçi odst.s_addr = ip->ip_dst.s_addr; 13105fc9d78SKristof Provost switch (pfil_run_hooks(V_inet_pfil_head, mp, ifp, pflags, inp)) { 132b252313fSGleb Smirnoff case PFIL_DROPPED: 1338d5c56daSGleb Smirnoff *error = EACCES; 134b252313fSGleb Smirnoff /* FALLTHROUGH */ 135b252313fSGleb Smirnoff case PFIL_CONSUMED: 136d9f2a782SErmal Luçi return 1; /* Finished */ 137b252313fSGleb Smirnoff case PFIL_PASS: 138b252313fSGleb Smirnoff *error = 0; 139b252313fSGleb Smirnoff } 140b252313fSGleb Smirnoff m = *mp; 141d9f2a782SErmal Luçi ip = mtod(m, struct ip *); 142d9f2a782SErmal Luçi 143d9f2a782SErmal Luçi /* See if destination IP address was changed by packet filter. */ 144d9f2a782SErmal Luçi if (odst.s_addr != ip->ip_dst.s_addr) { 145d9f2a782SErmal Luçi m->m_flags |= M_SKIP_FIREWALL; 146d9f2a782SErmal Luçi /* If destination is now ourself drop to ip_input(). */ 147d9f2a782SErmal Luçi if (in_localip(ip->ip_dst)) { 148d9f2a782SErmal Luçi m->m_flags |= M_FASTFWD_OURS; 149d9f2a782SErmal Luçi if (m->m_pkthdr.rcvif == NULL) 150d9f2a782SErmal Luçi m->m_pkthdr.rcvif = V_loif; 151d9f2a782SErmal Luçi if (m->m_pkthdr.csum_flags & CSUM_DELAY_DATA) { 152d9f2a782SErmal Luçi m->m_pkthdr.csum_flags |= 153d9f2a782SErmal Luçi CSUM_DATA_VALID | CSUM_PSEUDO_HDR; 154d9f2a782SErmal Luçi m->m_pkthdr.csum_data = 0xffff; 155d9f2a782SErmal Luçi } 156d9f2a782SErmal Luçi m->m_pkthdr.csum_flags |= 157d9f2a782SErmal Luçi CSUM_IP_CHECKED | CSUM_IP_VALID; 15895033af9SMark Johnston #if defined(SCTP) || defined(SCTP_SUPPORT) 159d9f2a782SErmal Luçi if (m->m_pkthdr.csum_flags & CSUM_SCTP) 160d9f2a782SErmal Luçi m->m_pkthdr.csum_flags |= CSUM_SCTP_VALID; 161d9f2a782SErmal Luçi #endif 162d9f2a782SErmal Luçi *error = netisr_queue(NETISR_IP, m); 163d9f2a782SErmal Luçi return 1; /* Finished */ 164d9f2a782SErmal Luçi } 165d9f2a782SErmal Luçi 166d9f2a782SErmal Luçi bzero(dst, sizeof(*dst)); 167d9f2a782SErmal Luçi dst->sin_family = AF_INET; 168d9f2a782SErmal Luçi dst->sin_len = sizeof(*dst); 169d9f2a782SErmal Luçi dst->sin_addr = ip->ip_dst; 170d9f2a782SErmal Luçi 171d9f2a782SErmal Luçi return -1; /* Reloop */ 172d9f2a782SErmal Luçi } 173d9f2a782SErmal Luçi /* See if fib was changed by packet filter. */ 174d9f2a782SErmal Luçi if ((*fibnum) != M_GETFIB(m)) { 175d9f2a782SErmal Luçi m->m_flags |= M_SKIP_FIREWALL; 176d9f2a782SErmal Luçi *fibnum = M_GETFIB(m); 177d9f2a782SErmal Luçi return -1; /* Reloop for FIB change */ 178d9f2a782SErmal Luçi } 179d9f2a782SErmal Luçi 180d9f2a782SErmal Luçi /* See if local, if yes, send it to netisr with IP_FASTFWD_OURS. */ 181d9f2a782SErmal Luçi if (m->m_flags & M_FASTFWD_OURS) { 182d9f2a782SErmal Luçi if (m->m_pkthdr.rcvif == NULL) 183d9f2a782SErmal Luçi m->m_pkthdr.rcvif = V_loif; 184d9f2a782SErmal Luçi if (m->m_pkthdr.csum_flags & CSUM_DELAY_DATA) { 185d9f2a782SErmal Luçi m->m_pkthdr.csum_flags |= 186d9f2a782SErmal Luçi CSUM_DATA_VALID | CSUM_PSEUDO_HDR; 187d9f2a782SErmal Luçi m->m_pkthdr.csum_data = 0xffff; 188d9f2a782SErmal Luçi } 18995033af9SMark Johnston #if defined(SCTP) || defined(SCTP_SUPPORT) 190d9f2a782SErmal Luçi if (m->m_pkthdr.csum_flags & CSUM_SCTP) 191d9f2a782SErmal Luçi m->m_pkthdr.csum_flags |= CSUM_SCTP_VALID; 192d9f2a782SErmal Luçi #endif 193d9f2a782SErmal Luçi m->m_pkthdr.csum_flags |= 194d9f2a782SErmal Luçi CSUM_IP_CHECKED | CSUM_IP_VALID; 195d9f2a782SErmal Luçi 196d9f2a782SErmal Luçi *error = netisr_queue(NETISR_IP, m); 197d9f2a782SErmal Luçi return 1; /* Finished */ 198d9f2a782SErmal Luçi } 199d9f2a782SErmal Luçi /* Or forward to some other address? */ 200d9f2a782SErmal Luçi if ((m->m_flags & M_IP_NEXTHOP) && 201d9f2a782SErmal Luçi ((fwd_tag = m_tag_find(m, PACKET_TAG_IPFORWARD, NULL)) != NULL)) { 202d9f2a782SErmal Luçi bcopy((fwd_tag+1), dst, sizeof(struct sockaddr_in)); 203d9f2a782SErmal Luçi m->m_flags |= M_SKIP_FIREWALL; 204d9f2a782SErmal Luçi m->m_flags &= ~M_IP_NEXTHOP; 205d9f2a782SErmal Luçi m_tag_delete(m, fwd_tag); 206d9f2a782SErmal Luçi 207d9f2a782SErmal Luçi return -1; /* Reloop for CHANGE of dst */ 208d9f2a782SErmal Luçi } 209d9f2a782SErmal Luçi 210d9f2a782SErmal Luçi return 0; 211d9f2a782SErmal Luçi } 212d9f2a782SErmal Luçi 213fb3bc596SJohn Baldwin static int 214fb3bc596SJohn Baldwin ip_output_send(struct inpcb *inp, struct ifnet *ifp, struct mbuf *m, 21535c7bb34SRandall Stewart const struct sockaddr_in *gw, struct route *ro, bool stamp_tag) 216fb3bc596SJohn Baldwin { 217b2e60773SJohn Baldwin #ifdef KERN_TLS 218b2e60773SJohn Baldwin struct ktls_session *tls = NULL; 219b2e60773SJohn Baldwin #endif 220fb3bc596SJohn Baldwin struct m_snd_tag *mst; 221fb3bc596SJohn Baldwin int error; 222fb3bc596SJohn Baldwin 223fb3bc596SJohn Baldwin MPASS((m->m_pkthdr.csum_flags & CSUM_SND_TAG) == 0); 224fb3bc596SJohn Baldwin mst = NULL; 225fb3bc596SJohn Baldwin 226b2e60773SJohn Baldwin #ifdef KERN_TLS 227b2e60773SJohn Baldwin /* 228b2e60773SJohn Baldwin * If this is an unencrypted TLS record, save a reference to 229b2e60773SJohn Baldwin * the record. This local reference is used to call 230b2e60773SJohn Baldwin * ktls_output_eagain after the mbuf has been freed (thus 231b2e60773SJohn Baldwin * dropping the mbuf's reference) in if_output. 232b2e60773SJohn Baldwin */ 233b2e60773SJohn Baldwin if (m->m_next != NULL && mbuf_has_tls_session(m->m_next)) { 2347b6c99d0SGleb Smirnoff tls = ktls_hold(m->m_next->m_epg_tls); 235b2e60773SJohn Baldwin mst = tls->snd_tag; 236b2e60773SJohn Baldwin 237b2e60773SJohn Baldwin /* 238b2e60773SJohn Baldwin * If a TLS session doesn't have a valid tag, it must 239b2e60773SJohn Baldwin * have had an earlier ifp mismatch, so drop this 240b2e60773SJohn Baldwin * packet. 241b2e60773SJohn Baldwin */ 242b2e60773SJohn Baldwin if (mst == NULL) { 243b2e60773SJohn Baldwin error = EAGAIN; 244b2e60773SJohn Baldwin goto done; 245b2e60773SJohn Baldwin } 2466043ac20SAndrew Gallatin /* 2476043ac20SAndrew Gallatin * Always stamp tags that include NIC ktls. 2486043ac20SAndrew Gallatin */ 2496043ac20SAndrew Gallatin stamp_tag = true; 250b2e60773SJohn Baldwin } 251b2e60773SJohn Baldwin #endif 252fb3bc596SJohn Baldwin #ifdef RATELIMIT 253b2e60773SJohn Baldwin if (inp != NULL && mst == NULL) { 254fb3bc596SJohn Baldwin if ((inp->inp_flags2 & INP_RATE_LIMIT_CHANGED) != 0 || 255fb3bc596SJohn Baldwin (inp->inp_snd_tag != NULL && 256fb3bc596SJohn Baldwin inp->inp_snd_tag->ifp != ifp)) 257fb3bc596SJohn Baldwin in_pcboutput_txrtlmt(inp, ifp, m); 258fb3bc596SJohn Baldwin 259fb3bc596SJohn Baldwin if (inp->inp_snd_tag != NULL) 260fb3bc596SJohn Baldwin mst = inp->inp_snd_tag; 261fb3bc596SJohn Baldwin } 262fb3bc596SJohn Baldwin #endif 26335c7bb34SRandall Stewart if (stamp_tag && mst != NULL) { 264fb3bc596SJohn Baldwin KASSERT(m->m_pkthdr.rcvif == NULL, 265fb3bc596SJohn Baldwin ("trying to add a send tag to a forwarded packet")); 266fb3bc596SJohn Baldwin if (mst->ifp != ifp) { 267fb3bc596SJohn Baldwin error = EAGAIN; 268fb3bc596SJohn Baldwin goto done; 269fb3bc596SJohn Baldwin } 270fb3bc596SJohn Baldwin 271fb3bc596SJohn Baldwin /* stamp send tag on mbuf */ 272fb3bc596SJohn Baldwin m->m_pkthdr.snd_tag = m_snd_tag_ref(mst); 273fb3bc596SJohn Baldwin m->m_pkthdr.csum_flags |= CSUM_SND_TAG; 274fb3bc596SJohn Baldwin } 275fb3bc596SJohn Baldwin 276fb3bc596SJohn Baldwin error = (*ifp->if_output)(ifp, m, (const struct sockaddr *)gw, ro); 277fb3bc596SJohn Baldwin 278fb3bc596SJohn Baldwin done: 279fb3bc596SJohn Baldwin /* Check for route change invalidating send tags. */ 280b2e60773SJohn Baldwin #ifdef KERN_TLS 281b2e60773SJohn Baldwin if (tls != NULL) { 282b2e60773SJohn Baldwin if (error == EAGAIN) 283b2e60773SJohn Baldwin error = ktls_output_eagain(inp, tls); 284b2e60773SJohn Baldwin ktls_free(tls); 285b2e60773SJohn Baldwin } 286b2e60773SJohn Baldwin #endif 287fb3bc596SJohn Baldwin #ifdef RATELIMIT 288fb3bc596SJohn Baldwin if (error == EAGAIN) 289fb3bc596SJohn Baldwin in_pcboutput_eagain(inp); 290fb3bc596SJohn Baldwin #endif 291fb3bc596SJohn Baldwin return (error); 292fb3bc596SJohn Baldwin } 293fb3bc596SJohn Baldwin 294983066f0SAlexander V. Chernikov /* rte<>ro_flags translation */ 295983066f0SAlexander V. Chernikov static inline void 296*9748eb74SAlexander V. Chernikov rt_update_ro_flags(struct route *ro, const struct nhop_object *nh) 297983066f0SAlexander V. Chernikov { 298*9748eb74SAlexander V. Chernikov int nh_flags = nh->nh_flags; 299983066f0SAlexander V. Chernikov 300983066f0SAlexander V. Chernikov ro->ro_flags &= ~ (RT_REJECT|RT_BLACKHOLE|RT_HAS_GW); 301983066f0SAlexander V. Chernikov 302983066f0SAlexander V. Chernikov ro->ro_flags |= (nh_flags & NHF_REJECT) ? RT_REJECT : 0; 303983066f0SAlexander V. Chernikov ro->ro_flags |= (nh_flags & NHF_BLACKHOLE) ? RT_BLACKHOLE : 0; 304983066f0SAlexander V. Chernikov ro->ro_flags |= (nh_flags & NHF_GATEWAY) ? RT_HAS_GW : 0; 305983066f0SAlexander V. Chernikov } 306983066f0SAlexander V. Chernikov 307df8bae1dSRodney W. Grimes /* 308df8bae1dSRodney W. Grimes * IP output. The packet in mbuf chain m contains a skeletal IP 309df8bae1dSRodney W. Grimes * header (with len, off, ttl, proto, tos, src, dst). 310df8bae1dSRodney W. Grimes * The mbuf chain containing the packet will be freed. 311df8bae1dSRodney W. Grimes * The mbuf opt, if present, will not be freed. 312bf984051SGleb Smirnoff * If route ro is present and has ro_rt initialized, route lookup would be 313bf984051SGleb Smirnoff * skipped and ro->ro_rt would be used. If ro is present but ro->ro_rt is NULL, 314bf984051SGleb Smirnoff * then result of route lookup is stored in ro->ro_rt. 315bf984051SGleb Smirnoff * 3163de758d3SRobert Watson * In the IP forwarding case, the packet will arrive with options already 3173de758d3SRobert Watson * inserted, so must have a NULL opt pointer. 318df8bae1dSRodney W. Grimes */ 319df8bae1dSRodney W. Grimes int 320f2565d68SRobert Watson ip_output(struct mbuf *m, struct mbuf *opt, struct route *ro, int flags, 321f2565d68SRobert Watson struct ip_moptions *imo, struct inpcb *inp) 322df8bae1dSRodney W. Grimes { 32365634ae7SWojciech Macek MROUTER_RLOCK_TRACKER; 324cc0a3c8cSAndrey V. Elsukov struct rm_priotracker in_ifa_tracker; 3251e78ac21SJeffrey Hsu struct ip *ip; 326fc74d005SBjoern A. Zeeb struct ifnet *ifp = NULL; /* keep compiler happy */ 327ac9d7e26SMax Laier struct mbuf *m0; 32823bf9953SPoul-Henning Kamp int hlen = sizeof (struct ip); 329374ce248SMitchell Horne int mtu = 0; 330ca8b83b0SLuigi Rizzo int error = 0; 331868aabb4SRichard Scheffenegger int vlan_pcp = -1; 3326ca363ebSGleb Smirnoff struct sockaddr_in *dst, sin; 3334c7a6059SGleb Smirnoff const struct sockaddr_in *gw; 334374ce248SMitchell Horne struct in_ifaddr *ia = NULL; 3356ca363ebSGleb Smirnoff struct in_addr src; 33621d172a3SGleb Smirnoff int isbroadcast; 337078468edSGleb Smirnoff uint16_t ip_len, ip_off; 3389c57a5b6SHiroki Sato uint32_t fibnum; 339fcf59617SAndrey V. Elsukov #if defined(IPSEC) || defined(IPSEC_SUPPORT) 3401a499816SEdward Tomasz Napierala int no_route_but_check_spd = 0; 3411a499816SEdward Tomasz Napierala #endif 34277a01441SJohn Baldwin 343ac9d7e26SMax Laier M_ASSERTPKTHDR(m); 344b9555453SGleb Smirnoff NET_EPOCH_ASSERT(); 34536e8826fSMax Laier 346bc97ba51SJulian Elischer if (inp != NULL) { 347baa45840SRobert Watson INP_LOCK_ASSERT(inp); 34862e1ba83SRobert Watson M_SETFIB(m, inp->inp_inc.inc_fibnum); 349c2529042SHans Petter Selasky if ((flags & IP_NODEFAULTFLOWID) == 0) { 35080cb9f21SKip Macy m->m_pkthdr.flowid = inp->inp_flowid; 3519c423972SAdrian Chadd M_HASHTYPE_SET(m, inp->inp_flowtype); 35280cb9f21SKip Macy } 353868aabb4SRichard Scheffenegger if ((inp->inp_flags2 & INP_2PCP_SET) != 0) 354868aabb4SRichard Scheffenegger vlan_pcp = (inp->inp_flags2 & INP_2PCP_MASK) >> 355868aabb4SRichard Scheffenegger INP_2PCP_SHIFT; 35650575ce1SAndrew Gallatin #ifdef NUMA 35750575ce1SAndrew Gallatin m->m_pkthdr.numa_domain = inp->inp_numa_domain; 35850575ce1SAndrew Gallatin #endif 359bc97ba51SJulian Elischer } 36062e1ba83SRobert Watson 361df8bae1dSRodney W. Grimes if (opt) { 362ca8b83b0SLuigi Rizzo int len = 0; 363df8bae1dSRodney W. Grimes m = ip_insertoptions(m, opt, &len); 364cb7641e8SMaxim Konovalov if (len != 0) 365ca8b83b0SLuigi Rizzo hlen = len; /* ip->ip_hl is updated above */ 366df8bae1dSRodney W. Grimes } 367df8bae1dSRodney W. Grimes ip = mtod(m, struct ip *); 3688f134647SGleb Smirnoff ip_len = ntohs(ip->ip_len); 3698f134647SGleb Smirnoff ip_off = ntohs(ip->ip_off); 3703efc3014SJulian Elischer 371df8bae1dSRodney W. Grimes if ((flags & (IP_FORWARDING|IP_RAWOUTPUT)) == 0) { 37253be11f6SPoul-Henning Kamp ip->ip_v = IPVERSION; 37353be11f6SPoul-Henning Kamp ip->ip_hl = hlen >> 2; 3746d947416SGleb Smirnoff ip_fillid(ip); 375df8bae1dSRodney W. Grimes } else { 376ca8b83b0SLuigi Rizzo /* Header already set, fetch hlen from there */ 37753be11f6SPoul-Henning Kamp hlen = ip->ip_hl << 2; 378df8bae1dSRodney W. Grimes } 3793924dfa7SMichael Tuexen if ((flags & IP_FORWARDING) == 0) 3803924dfa7SMichael Tuexen IPSTAT_INC(ips_localout); 3819c9137eaSGarrett Wollman 382054692a4SAlexander V. Chernikov /* 383054692a4SAlexander V. Chernikov * dst/gw handling: 384054692a4SAlexander V. Chernikov * 3853c065f2fSGleb Smirnoff * gw is readonly but can point either to dst OR rt_gateway, 3863c065f2fSGleb Smirnoff * therefore we need restore gw if we're redoing lookup. 387054692a4SAlexander V. Chernikov */ 3889c57a5b6SHiroki Sato fibnum = (inp != NULL) ? inp->inp_inc.inc_fibnum : M_GETFIB(m); 3896ca363ebSGleb Smirnoff if (ro != NULL) 3906ca363ebSGleb Smirnoff dst = (struct sockaddr_in *)&ro->ro_dst; 3916ca363ebSGleb Smirnoff else 3926ca363ebSGleb Smirnoff dst = &sin; 393983066f0SAlexander V. Chernikov if (ro == NULL || ro->ro_nh == NULL) { 394a4a6e773SHajimu UMEMOTO bzero(dst, sizeof(*dst)); 395df8bae1dSRodney W. Grimes dst->sin_family = AF_INET; 396df8bae1dSRodney W. Grimes dst->sin_len = sizeof(*dst); 3979b932e9eSAndre Oppermann dst->sin_addr = ip->ip_dst; 398df8bae1dSRodney W. Grimes } 3996ca363ebSGleb Smirnoff gw = dst; 400d9f2a782SErmal Luçi again: 40184cc0778SGeorge V. Neville-Neil /* 40284cc0778SGeorge V. Neville-Neil * Validate route against routing table additions; 40384cc0778SGeorge V. Neville-Neil * a better/more specific route might have been added. 40484cc0778SGeorge V. Neville-Neil */ 405983066f0SAlexander V. Chernikov if (inp != NULL && ro != NULL && ro->ro_nh != NULL) 406983066f0SAlexander V. Chernikov NH_VALIDATE(ro, &inp->inp_rt_cookie, fibnum); 40784cc0778SGeorge V. Neville-Neil /* 40884cc0778SGeorge V. Neville-Neil * If there is a cached route, 40984cc0778SGeorge V. Neville-Neil * check that it is to the same destination 41084cc0778SGeorge V. Neville-Neil * and is still up. If not, free it and try again. 41184cc0778SGeorge V. Neville-Neil * The address family should also be checked in case of sharing the 41284cc0778SGeorge V. Neville-Neil * cache with IPv6. 41384cc0778SGeorge V. Neville-Neil * Also check whether routing cache needs invalidation. 41484cc0778SGeorge V. Neville-Neil */ 415983066f0SAlexander V. Chernikov if (ro != NULL && ro->ro_nh != NULL && 416174fb9dbSAlexander V. Chernikov ((!NH_IS_VALID(ro->ro_nh)) || dst->sin_family != AF_INET || 4176ca363ebSGleb Smirnoff dst->sin_addr.s_addr != ip->ip_dst.s_addr)) 418fc21c53fSRyan Stone RO_INVALIDATE_CACHE(ro); 419d9f2a782SErmal Luçi ia = NULL; 420df8bae1dSRodney W. Grimes /* 421a3fd02d8SBruce M Simpson * If routing to interface only, short circuit routing lookup. 422a3fd02d8SBruce M Simpson * The use of an all-ones broadcast address implies this; an 423a3fd02d8SBruce M Simpson * interface is specified by the broadcast address of an interface, 424a3fd02d8SBruce M Simpson * or the destination address of a ptp interface. 425df8bae1dSRodney W. Grimes */ 426a3fd02d8SBruce M Simpson if (flags & IP_SENDONES) { 4274f8585e0SAlan Somers if ((ia = ifatoia(ifa_ifwithbroadaddr(sintosa(dst), 42858a39d8cSAlan Somers M_GETFIB(m)))) == NULL && 4294f8585e0SAlan Somers (ia = ifatoia(ifa_ifwithdstaddr(sintosa(dst), 43058a39d8cSAlan Somers M_GETFIB(m)))) == NULL) { 43186425c62SRobert Watson IPSTAT_INC(ips_noroute); 432a3fd02d8SBruce M Simpson error = ENETUNREACH; 433a3fd02d8SBruce M Simpson goto bad; 434a3fd02d8SBruce M Simpson } 435a3fd02d8SBruce M Simpson ip->ip_dst.s_addr = INADDR_BROADCAST; 436a3fd02d8SBruce M Simpson dst->sin_addr = ip->ip_dst; 437a3fd02d8SBruce M Simpson ifp = ia->ia_ifp; 4386ca363ebSGleb Smirnoff mtu = ifp->if_mtu; 439a3fd02d8SBruce M Simpson ip->ip_ttl = 1; 440a3fd02d8SBruce M Simpson isbroadcast = 1; 4416ca363ebSGleb Smirnoff src = IA_SIN(ia)->sin_addr; 442a3fd02d8SBruce M Simpson } else if (flags & IP_ROUTETOIF) { 4434f8585e0SAlan Somers if ((ia = ifatoia(ifa_ifwithdstaddr(sintosa(dst), 44458a39d8cSAlan Somers M_GETFIB(m)))) == NULL && 4454f8585e0SAlan Somers (ia = ifatoia(ifa_ifwithnet(sintosa(dst), 0, 44658a39d8cSAlan Somers M_GETFIB(m)))) == NULL) { 44786425c62SRobert Watson IPSTAT_INC(ips_noroute); 448df8bae1dSRodney W. Grimes error = ENETUNREACH; 449df8bae1dSRodney W. Grimes goto bad; 450df8bae1dSRodney W. Grimes } 451df8bae1dSRodney W. Grimes ifp = ia->ia_ifp; 4526ca363ebSGleb Smirnoff mtu = ifp->if_mtu; 453df8bae1dSRodney W. Grimes ip->ip_ttl = 1; 4546c1bd558SRyan Stone isbroadcast = ifp->if_flags & IFF_BROADCAST ? 4556c1bd558SRyan Stone in_ifaddr_broadcast(dst->sin_addr, ia) : 0; 4566ca363ebSGleb Smirnoff src = IA_SIN(ia)->sin_addr; 4573afefa39SDaniel C. Sobral } else if (IN_MULTICAST(ntohl(ip->ip_dst.s_addr)) && 45838c1bc35SRuslan Ermilov imo != NULL && imo->imo_multicast_ifp != NULL) { 4593afefa39SDaniel C. Sobral /* 46038c1bc35SRuslan Ermilov * Bypass the normal routing lookup for multicast 46138c1bc35SRuslan Ermilov * packets if the interface is specified. 4623afefa39SDaniel C. Sobral */ 46338c1bc35SRuslan Ermilov ifp = imo->imo_multicast_ifp; 4646ca363ebSGleb Smirnoff mtu = ifp->if_mtu; 465cc0a3c8cSAndrey V. Elsukov IFP_TO_IA(ifp, ia, &in_ifa_tracker); 46638c1bc35SRuslan Ermilov isbroadcast = 0; /* fool gcc */ 46754bb7ac0SGleb Smirnoff /* Interface may have no addresses. */ 46854bb7ac0SGleb Smirnoff if (ia != NULL) 4696ca363ebSGleb Smirnoff src = IA_SIN(ia)->sin_addr; 47054bb7ac0SGleb Smirnoff else 47154bb7ac0SGleb Smirnoff src.s_addr = INADDR_ANY; 4726ca363ebSGleb Smirnoff } else if (ro != NULL) { 473983066f0SAlexander V. Chernikov if (ro->ro_nh == NULL) { 4742c17fe93SGarrett Wollman /* 4756ca363ebSGleb Smirnoff * We want to do any cloning requested by the link 4766ca363ebSGleb Smirnoff * layer, as this is probably required in all cases 4776ca363ebSGleb Smirnoff * for correct operation (as it is for ARP). 4782c17fe93SGarrett Wollman */ 4794043ee3cSAlexander V. Chernikov uint32_t flowid; 4804043ee3cSAlexander V. Chernikov flowid = m->m_pkthdr.flowid; 4814043ee3cSAlexander V. Chernikov ro->ro_nh = fib4_lookup(fibnum, dst->sin_addr, 0, 4824043ee3cSAlexander V. Chernikov NHR_REF, flowid); 4834043ee3cSAlexander V. Chernikov 484174fb9dbSAlexander V. Chernikov if (ro->ro_nh == NULL || (!NH_IS_VALID(ro->ro_nh))) { 485fcf59617SAndrey V. Elsukov #if defined(IPSEC) || defined(IPSEC_SUPPORT) 4861a499816SEdward Tomasz Napierala /* 4871a499816SEdward Tomasz Napierala * There is no route for this packet, but it is 4881a499816SEdward Tomasz Napierala * possible that a matching SPD entry exists. 4891a499816SEdward Tomasz Napierala */ 4901a499816SEdward Tomasz Napierala no_route_but_check_spd = 1; 4911a499816SEdward Tomasz Napierala goto sendit; 4921a499816SEdward Tomasz Napierala #endif 49386425c62SRobert Watson IPSTAT_INC(ips_noroute); 494df8bae1dSRodney W. Grimes error = EHOSTUNREACH; 495df8bae1dSRodney W. Grimes goto bad; 496df8bae1dSRodney W. Grimes } 4976ca363ebSGleb Smirnoff } 498*9748eb74SAlexander V. Chernikov struct nhop_object *nh = ro->ro_nh; 499*9748eb74SAlexander V. Chernikov 500*9748eb74SAlexander V. Chernikov ia = ifatoia(nh->nh_ifa); 501*9748eb74SAlexander V. Chernikov ifp = nh->nh_ifp; 502*9748eb74SAlexander V. Chernikov counter_u64_add(nh->nh_pksent, 1); 503*9748eb74SAlexander V. Chernikov rt_update_ro_flags(ro, nh); 504*9748eb74SAlexander V. Chernikov if (nh->nh_flags & NHF_GATEWAY) 505*9748eb74SAlexander V. Chernikov gw = &nh->gw4_sa; 506*9748eb74SAlexander V. Chernikov if (nh->nh_flags & NHF_HOST) 507*9748eb74SAlexander V. Chernikov isbroadcast = (nh->nh_flags & NHF_BROADCAST); 5086c1bd558SRyan Stone else if (ifp->if_flags & IFF_BROADCAST) 50990cc51a1SRyan Stone isbroadcast = in_ifaddr_broadcast(gw->sin_addr, ia); 5106c1bd558SRyan Stone else 5116c1bd558SRyan Stone isbroadcast = 0; 512*9748eb74SAlexander V. Chernikov mtu = nh->nh_mtu; 5136ca363ebSGleb Smirnoff src = IA_SIN(ia)->sin_addr; 5146ca363ebSGleb Smirnoff } else { 5153553b300SAlexander V. Chernikov struct nhop_object *nh; 5166ca363ebSGleb Smirnoff 5172259a030SAlexander V. Chernikov nh = fib4_lookup(M_GETFIB(m), ip->ip_dst, 0, NHR_NONE, 5182259a030SAlexander V. Chernikov m->m_pkthdr.flowid); 5193553b300SAlexander V. Chernikov if (nh == NULL) { 5206ca363ebSGleb Smirnoff #if defined(IPSEC) || defined(IPSEC_SUPPORT) 5216ca363ebSGleb Smirnoff /* 5226ca363ebSGleb Smirnoff * There is no route for this packet, but it is 5236ca363ebSGleb Smirnoff * possible that a matching SPD entry exists. 5246ca363ebSGleb Smirnoff */ 5256ca363ebSGleb Smirnoff no_route_but_check_spd = 1; 5266ca363ebSGleb Smirnoff goto sendit; 5276ca363ebSGleb Smirnoff #endif 5286ca363ebSGleb Smirnoff IPSTAT_INC(ips_noroute); 5296ca363ebSGleb Smirnoff error = EHOSTUNREACH; 5306ca363ebSGleb Smirnoff goto bad; 5316ca363ebSGleb Smirnoff } 5323553b300SAlexander V. Chernikov ifp = nh->nh_ifp; 5333553b300SAlexander V. Chernikov mtu = nh->nh_mtu; 5346ca363ebSGleb Smirnoff /* 5356ca363ebSGleb Smirnoff * We are rewriting here dst to be gw actually, contradicting 5366ca363ebSGleb Smirnoff * comment at the beginning of the function. However, in this 5376ca363ebSGleb Smirnoff * case we are always dealing with on stack dst. 5386ca363ebSGleb Smirnoff * In case if pfil(9) sends us back to beginning of the 5396ca363ebSGleb Smirnoff * function, the dst would be rewritten by ip_output_pfil(). 5406ca363ebSGleb Smirnoff */ 5416ca363ebSGleb Smirnoff MPASS(dst == &sin); 5423553b300SAlexander V. Chernikov if (nh->nh_flags & NHF_GATEWAY) 5433553b300SAlexander V. Chernikov dst->sin_addr = nh->gw4_sa.sin_addr; 5443553b300SAlexander V. Chernikov ia = ifatoia(nh->nh_ifa); 5453553b300SAlexander V. Chernikov src = IA_SIN(ia)->sin_addr; 5463553b300SAlexander V. Chernikov isbroadcast = (((nh->nh_flags & (NHF_HOST | NHF_BROADCAST)) == 5476ca363ebSGleb Smirnoff (NHF_HOST | NHF_BROADCAST)) || 5486ca363ebSGleb Smirnoff ((ifp->if_flags & IFF_BROADCAST) && 5496ca363ebSGleb Smirnoff in_ifaddr_broadcast(dst->sin_addr, ia))); 5506ca363ebSGleb Smirnoff } 5516ca363ebSGleb Smirnoff 552c744cde4SBjoern A. Zeeb /* Catch a possible divide by zero later. */ 553983066f0SAlexander V. Chernikov KASSERT(mtu > 0, ("%s: mtu %d <= 0, ro=%p (nh_flags=0x%08x) ifp=%p", 5546ca363ebSGleb Smirnoff __func__, mtu, ro, 555983066f0SAlexander V. Chernikov (ro != NULL && ro->ro_nh != NULL) ? ro->ro_nh->nh_flags : 0, ifp)); 556d9f2a782SErmal Luçi 5579b932e9eSAndre Oppermann if (IN_MULTICAST(ntohl(ip->ip_dst.s_addr))) { 558df8bae1dSRodney W. Grimes m->m_flags |= M_MCAST; 559df8bae1dSRodney W. Grimes /* 560183e1c86SGleb Smirnoff * IP destination address is multicast. Make sure "gw" 561183e1c86SGleb Smirnoff * still points to the address in "ro". (It may have been 562183e1c86SGleb Smirnoff * changed to point to a gateway address, above.) 563183e1c86SGleb Smirnoff */ 564183e1c86SGleb Smirnoff gw = dst; 565183e1c86SGleb Smirnoff /* 566df8bae1dSRodney W. Grimes * See if the caller provided any multicast options 567df8bae1dSRodney W. Grimes */ 568df8bae1dSRodney W. Grimes if (imo != NULL) { 569df8bae1dSRodney W. Grimes ip->ip_ttl = imo->imo_multicast_ttl; 5701c5de19aSGarrett Wollman if (imo->imo_multicast_vif != -1) 5711c5de19aSGarrett Wollman ip->ip_src.s_addr = 572bbb4330bSLuigi Rizzo ip_mcast_src ? 573bbb4330bSLuigi Rizzo ip_mcast_src(imo->imo_multicast_vif) : 574bbb4330bSLuigi Rizzo INADDR_ANY; 575df8bae1dSRodney W. Grimes } else 576df8bae1dSRodney W. Grimes ip->ip_ttl = IP_DEFAULT_MULTICAST_TTL; 577df8bae1dSRodney W. Grimes /* 578df8bae1dSRodney W. Grimes * Confirm that the outgoing interface supports multicast. 579df8bae1dSRodney W. Grimes */ 5801c5de19aSGarrett Wollman if ((imo == NULL) || (imo->imo_multicast_vif == -1)) { 581df8bae1dSRodney W. Grimes if ((ifp->if_flags & IFF_MULTICAST) == 0) { 58286425c62SRobert Watson IPSTAT_INC(ips_noroute); 583df8bae1dSRodney W. Grimes error = ENETUNREACH; 584df8bae1dSRodney W. Grimes goto bad; 585df8bae1dSRodney W. Grimes } 5861c5de19aSGarrett Wollman } 587df8bae1dSRodney W. Grimes /* 588df8bae1dSRodney W. Grimes * If source address not specified yet, use address 589df8bae1dSRodney W. Grimes * of outgoing interface. 590df8bae1dSRodney W. Grimes */ 5916ca363ebSGleb Smirnoff if (ip->ip_src.s_addr == INADDR_ANY) 5926ca363ebSGleb Smirnoff ip->ip_src = src; 593df8bae1dSRodney W. Grimes 5948b889dbbSBruce M Simpson if ((imo == NULL && in_mcast_loop) || 5958b889dbbSBruce M Simpson (imo && imo->imo_multicast_loop)) { 596df8bae1dSRodney W. Grimes /* 5978b889dbbSBruce M Simpson * Loop back multicast datagram if not expressly 5988b889dbbSBruce M Simpson * forbidden to do so, even if we are not a member 5998b889dbbSBruce M Simpson * of the group; ip_input() will filter it later, 6008b889dbbSBruce M Simpson * thus deferring a hash lookup and mutex acquisition 6018b889dbbSBruce M Simpson * at the expense of a cheap copy using m_copym(). 602df8bae1dSRodney W. Grimes */ 603331dff07SAlexander V. Chernikov ip_mloopback(ifp, m, hlen); 6048b889dbbSBruce M Simpson } else { 605df8bae1dSRodney W. Grimes /* 606df8bae1dSRodney W. Grimes * If we are acting as a multicast router, perform 607df8bae1dSRodney W. Grimes * multicast forwarding as if the packet had just 608df8bae1dSRodney W. Grimes * arrived on the interface to which we are about 609df8bae1dSRodney W. Grimes * to send. The multicast forwarding function 610df8bae1dSRodney W. Grimes * recursively calls this function, using the 611df8bae1dSRodney W. Grimes * IP_FORWARDING flag to prevent infinite recursion. 612df8bae1dSRodney W. Grimes * 613df8bae1dSRodney W. Grimes * Multicasts that are looped back by ip_mloopback(), 614df8bae1dSRodney W. Grimes * above, will be forwarded by the ip_input() routine, 615df8bae1dSRodney W. Grimes * if necessary. 616df8bae1dSRodney W. Grimes */ 61765634ae7SWojciech Macek MROUTER_RLOCK(); 618603724d3SBjoern A. Zeeb if (V_ip_mrouter && (flags & IP_FORWARDING) == 0) { 619f0068c4aSGarrett Wollman /* 620bbb4330bSLuigi Rizzo * If rsvp daemon is not running, do not 621f0068c4aSGarrett Wollman * set ip_moptions. This ensures that the packet 622f0068c4aSGarrett Wollman * is multicast and not just sent down one link 623f0068c4aSGarrett Wollman * as prescribed by rsvpd. 624f0068c4aSGarrett Wollman */ 625603724d3SBjoern A. Zeeb if (!V_rsvp_on) 626f0068c4aSGarrett Wollman imo = NULL; 627bbb4330bSLuigi Rizzo if (ip_mforward && 628bbb4330bSLuigi Rizzo ip_mforward(ip, ifp, m, imo) != 0) { 62965634ae7SWojciech Macek MROUTER_RUNLOCK(); 630df8bae1dSRodney W. Grimes m_freem(m); 631df8bae1dSRodney W. Grimes goto done; 632df8bae1dSRodney W. Grimes } 633df8bae1dSRodney W. Grimes } 63465634ae7SWojciech Macek MROUTER_RUNLOCK(); 635df8bae1dSRodney W. Grimes } 6365e9ae478SGarrett Wollman 637df8bae1dSRodney W. Grimes /* 638df8bae1dSRodney W. Grimes * Multicasts with a time-to-live of zero may be looped- 639df8bae1dSRodney W. Grimes * back, above, but must not be transmitted on a network. 640df8bae1dSRodney W. Grimes * Also, multicasts addressed to the loopback interface 641df8bae1dSRodney W. Grimes * are not sent -- the above call to ip_mloopback() will 6428b889dbbSBruce M Simpson * loop back a copy. ip_input() will drop the copy if 6438b889dbbSBruce M Simpson * this host does not belong to the destination group on 6448b889dbbSBruce M Simpson * the loopback interface. 645df8bae1dSRodney W. Grimes */ 646f5fea3ddSPaul Traina if (ip->ip_ttl == 0 || ifp->if_flags & IFF_LOOPBACK) { 647df8bae1dSRodney W. Grimes m_freem(m); 648df8bae1dSRodney W. Grimes goto done; 649df8bae1dSRodney W. Grimes } 650df8bae1dSRodney W. Grimes 651df8bae1dSRodney W. Grimes goto sendit; 652df8bae1dSRodney W. Grimes } 6536a7c943cSAndre Oppermann 654df8bae1dSRodney W. Grimes /* 6552b25acc1SLuigi Rizzo * If the source address is not specified yet, use the address 656cb459254SJohn-Mark Gurney * of the outoing interface. 657df8bae1dSRodney W. Grimes */ 6586ca363ebSGleb Smirnoff if (ip->ip_src.s_addr == INADDR_ANY) 6596ca363ebSGleb Smirnoff ip->ip_src = src; 6606a7c943cSAndre Oppermann 661ca7a789aSMax Laier /* 662df8bae1dSRodney W. Grimes * Look for broadcast address and 6638c3f5566SRuslan Ermilov * verify user is allowed to send 664df8bae1dSRodney W. Grimes * such a packet. 665df8bae1dSRodney W. Grimes */ 6669f9b3dc4SGarrett Wollman if (isbroadcast) { 667df8bae1dSRodney W. Grimes if ((ifp->if_flags & IFF_BROADCAST) == 0) { 668df8bae1dSRodney W. Grimes error = EADDRNOTAVAIL; 669df8bae1dSRodney W. Grimes goto bad; 670df8bae1dSRodney W. Grimes } 671df8bae1dSRodney W. Grimes if ((flags & IP_ALLOWBROADCAST) == 0) { 672df8bae1dSRodney W. Grimes error = EACCES; 673df8bae1dSRodney W. Grimes goto bad; 674df8bae1dSRodney W. Grimes } 675df8bae1dSRodney W. Grimes /* don't allow broadcast messages to be fragmented */ 6768f134647SGleb Smirnoff if (ip_len > mtu) { 677df8bae1dSRodney W. Grimes error = EMSGSIZE; 678df8bae1dSRodney W. Grimes goto bad; 679df8bae1dSRodney W. Grimes } 680df8bae1dSRodney W. Grimes m->m_flags |= M_BCAST; 6819f9b3dc4SGarrett Wollman } else { 682df8bae1dSRodney W. Grimes m->m_flags &= ~M_BCAST; 6839f9b3dc4SGarrett Wollman } 684df8bae1dSRodney W. Grimes 685f1743588SDarren Reed sendit: 686fcf59617SAndrey V. Elsukov #if defined(IPSEC) || defined(IPSEC_SUPPORT) 687fcf59617SAndrey V. Elsukov if (IPSEC_ENABLED(ipv4)) { 688fcf59617SAndrey V. Elsukov if ((error = IPSEC_OUTPUT(ipv4, m, inp)) != 0) { 689fcf59617SAndrey V. Elsukov if (error == EINPROGRESS) 690fcf59617SAndrey V. Elsukov error = 0; 6911dfcf0d2SAndre Oppermann goto done; 692fcf59617SAndrey V. Elsukov } 69333841545SHajimu UMEMOTO } 6941a499816SEdward Tomasz Napierala /* 6951a499816SEdward Tomasz Napierala * Check if there was a route for this packet; return error if not. 6961a499816SEdward Tomasz Napierala */ 6971a499816SEdward Tomasz Napierala if (no_route_but_check_spd) { 6981a499816SEdward Tomasz Napierala IPSTAT_INC(ips_noroute); 6991a499816SEdward Tomasz Napierala error = EHOSTUNREACH; 7001a499816SEdward Tomasz Napierala goto bad; 7011a499816SEdward Tomasz Napierala } 7021dfcf0d2SAndre Oppermann /* Update variables that are affected by ipsec4_output(). */ 70333841545SHajimu UMEMOTO ip = mtod(m, struct ip *); 70433841545SHajimu UMEMOTO hlen = ip->ip_hl << 2; 705b2630c29SGeorge V. Neville-Neil #endif /* IPSEC */ 70633841545SHajimu UMEMOTO 707c21fd232SAndre Oppermann /* Jump over all PFIL processing if hooks are not active. */ 708b252313fSGleb Smirnoff if (PFIL_HOOKED_OUT(V_inet_pfil_head)) { 70905fc9d78SKristof Provost switch (ip_output_pfil(&m, ifp, flags, inp, dst, &fibnum, 71005fc9d78SKristof Provost &error)) { 711d9f2a782SErmal Luçi case 1: /* Finished */ 712c4ac87eaSDarren Reed goto done; 7139b932e9eSAndre Oppermann 714d9f2a782SErmal Luçi case 0: /* Continue normally */ 715c4ac87eaSDarren Reed ip = mtod(m, struct ip *); 716d9f2a782SErmal Luçi break; 717fed1c7e9SSøren Schmidt 718d9f2a782SErmal Luçi case -1: /* Need to try again */ 719d9f2a782SErmal Luçi /* Reset everything for a new round */ 7206ca363ebSGleb Smirnoff if (ro != NULL) { 721983066f0SAlexander V. Chernikov RO_NHFREE(ro); 7224fb3a820SAlexander V. Chernikov ro->ro_prepend = NULL; 7236ca363ebSGleb Smirnoff } 724d9f2a782SErmal Luçi gw = dst; 725d9f2a782SErmal Luçi ip = mtod(m, struct ip *); 7269b932e9eSAndre Oppermann goto again; 727d9f2a782SErmal Luçi } 728099dd043SAndre Oppermann } 7292b25acc1SLuigi Rizzo 730868aabb4SRichard Scheffenegger if (vlan_pcp > -1) 731868aabb4SRichard Scheffenegger EVL_APPLY_PRI(m, vlan_pcp); 732868aabb4SRichard Scheffenegger 7336c1c6ae5SRodney W. Grimes /* IN_LOOPBACK must not appear on the wire - RFC1122. */ 7346c1c6ae5SRodney W. Grimes if (IN_LOOPBACK(ntohl(ip->ip_dst.s_addr)) || 7356c1c6ae5SRodney W. Grimes IN_LOOPBACK(ntohl(ip->ip_src.s_addr))) { 73651c8ec4aSRuslan Ermilov if ((ifp->if_flags & IFF_LOOPBACK) == 0) { 73786425c62SRobert Watson IPSTAT_INC(ips_badaddr); 73851c8ec4aSRuslan Ermilov error = EADDRNOTAVAIL; 73951c8ec4aSRuslan Ermilov goto bad; 74051c8ec4aSRuslan Ermilov } 74151c8ec4aSRuslan Ermilov } 74251c8ec4aSRuslan Ermilov 743206a3274SRuslan Ermilov m->m_pkthdr.csum_flags |= CSUM_IP; 744078468edSGleb Smirnoff if (m->m_pkthdr.csum_flags & CSUM_DELAY_DATA & ~ifp->if_hwassist) { 74582334850SJohn Baldwin m = mb_unmapped_to_ext(m); 74682334850SJohn Baldwin if (m == NULL) { 74782334850SJohn Baldwin IPSTAT_INC(ips_odropped); 74882334850SJohn Baldwin error = ENOBUFS; 74982334850SJohn Baldwin goto bad; 75082334850SJohn Baldwin } 751db4f9cc7SJonathan Lemon in_delayed_cksum(m); 752078468edSGleb Smirnoff m->m_pkthdr.csum_flags &= ~CSUM_DELAY_DATA; 7533f43ada9SGleb Smirnoff } else if ((ifp->if_capenable & IFCAP_MEXTPG) == 0) { 75482334850SJohn Baldwin m = mb_unmapped_to_ext(m); 75582334850SJohn Baldwin if (m == NULL) { 75682334850SJohn Baldwin IPSTAT_INC(ips_odropped); 75782334850SJohn Baldwin error = ENOBUFS; 75882334850SJohn Baldwin goto bad; 75982334850SJohn Baldwin } 760db4f9cc7SJonathan Lemon } 76195033af9SMark Johnston #if defined(SCTP) || defined(SCTP_SUPPORT) 762078468edSGleb Smirnoff if (m->m_pkthdr.csum_flags & CSUM_SCTP & ~ifp->if_hwassist) { 76382334850SJohn Baldwin m = mb_unmapped_to_ext(m); 76482334850SJohn Baldwin if (m == NULL) { 76582334850SJohn Baldwin IPSTAT_INC(ips_odropped); 76682334850SJohn Baldwin error = ENOBUFS; 76782334850SJohn Baldwin goto bad; 76882334850SJohn Baldwin } 7691966e5b5SRandall Stewart sctp_delayed_cksum(m, (uint32_t)(ip->ip_hl << 2)); 770078468edSGleb Smirnoff m->m_pkthdr.csum_flags &= ~CSUM_SCTP; 7712f4afd21SRandall Stewart } 7722f4afd21SRandall Stewart #endif 773db4f9cc7SJonathan Lemon 774e7319babSPoul-Henning Kamp /* 775db4f9cc7SJonathan Lemon * If small enough for interface, or the interface will take 776233dcce1SAndre Oppermann * care of the fragmentation for us, we can just send directly. 777b092fd6cSNavdeep Parhar * Note that if_vxlan could have requested TSO even though the outer 778b092fd6cSNavdeep Parhar * frame is UDP. It is correct to not fragment such datagrams and 779b092fd6cSNavdeep Parhar * instead just pass them on to the driver. 780df8bae1dSRodney W. Grimes */ 78121d172a3SGleb Smirnoff if (ip_len <= mtu || 782b092fd6cSNavdeep Parhar (m->m_pkthdr.csum_flags & ifp->if_hwassist & 783b092fd6cSNavdeep Parhar (CSUM_TSO | CSUM_INNER_TSO)) != 0) { 784df8bae1dSRodney W. Grimes ip->ip_sum = 0; 785078468edSGleb Smirnoff if (m->m_pkthdr.csum_flags & CSUM_IP & ~ifp->if_hwassist) { 786df8bae1dSRodney W. Grimes ip->ip_sum = in_cksum(m, hlen); 787078468edSGleb Smirnoff m->m_pkthdr.csum_flags &= ~CSUM_IP; 788078468edSGleb Smirnoff } 7895da9f8faSJosef Karthauser 790233dcce1SAndre Oppermann /* 791233dcce1SAndre Oppermann * Record statistics for this interface address. 792233dcce1SAndre Oppermann * With CSUM_TSO the byte/packet count will be slightly 793233dcce1SAndre Oppermann * incorrect because we count the IP+TCP headers only 794233dcce1SAndre Oppermann * once instead of for every generated packet. 795233dcce1SAndre Oppermann */ 79638c1bc35SRuslan Ermilov if (!(flags & IP_FORWARDING) && ia) { 797b092fd6cSNavdeep Parhar if (m->m_pkthdr.csum_flags & 798b092fd6cSNavdeep Parhar (CSUM_TSO | CSUM_INNER_TSO)) 7997caf4ab7SGleb Smirnoff counter_u64_add(ia->ia_ifa.ifa_opackets, 8007caf4ab7SGleb Smirnoff m->m_pkthdr.len / m->m_pkthdr.tso_segsz); 801233dcce1SAndre Oppermann else 8027caf4ab7SGleb Smirnoff counter_u64_add(ia->ia_ifa.ifa_opackets, 1); 8037caf4ab7SGleb Smirnoff 8047caf4ab7SGleb Smirnoff counter_u64_add(ia->ia_ifa.ifa_obytes, m->m_pkthdr.len); 8055da9f8faSJosef Karthauser } 80653dcc544SMike Silbersack #ifdef MBUF_STRESS_TEST 8073390d476SMike Silbersack if (mbuf_frag_size && m->m_pkthdr.len > mbuf_frag_size) 808eb1b1807SGleb Smirnoff m = m_fragment(m, M_NOWAIT, mbuf_frag_size); 8099d9edc56SMike Silbersack #endif 810147f74d1SAndre Oppermann /* 811147f74d1SAndre Oppermann * Reset layer specific mbuf flags 812147f74d1SAndre Oppermann * to avoid confusing lower layers. 813147f74d1SAndre Oppermann */ 81486bd0491SAndre Oppermann m_clrprotoflags(m); 81557f60867SMark Johnston IP_PROBE(send, NULL, NULL, ip, ifp, ip, NULL); 81635c7bb34SRandall Stewart error = ip_output_send(inp, ifp, m, gw, ro, 81735c7bb34SRandall Stewart (flags & IP_NO_SND_TAG_RL) ? false : true); 818df8bae1dSRodney W. Grimes goto done; 819df8bae1dSRodney W. Grimes } 8201e78ac21SJeffrey Hsu 821233dcce1SAndre Oppermann /* Balk when DF bit is set or the interface didn't support TSO. */ 822b092fd6cSNavdeep Parhar if ((ip_off & IP_DF) || 823b092fd6cSNavdeep Parhar (m->m_pkthdr.csum_flags & (CSUM_TSO | CSUM_INNER_TSO))) { 824df8bae1dSRodney W. Grimes error = EMSGSIZE; 82586425c62SRobert Watson IPSTAT_INC(ips_cantfrag); 826df8bae1dSRodney W. Grimes goto bad; 827df8bae1dSRodney W. Grimes } 8281e78ac21SJeffrey Hsu 8291e78ac21SJeffrey Hsu /* 8301e78ac21SJeffrey Hsu * Too large for interface; fragment if possible. If successful, 8311e78ac21SJeffrey Hsu * on return, m will point to a list of packets to be sent. 8321e78ac21SJeffrey Hsu */ 833078468edSGleb Smirnoff error = ip_fragment(ip, &m, mtu, ifp->if_hwassist); 8341e78ac21SJeffrey Hsu if (error) 835df8bae1dSRodney W. Grimes goto bad; 8361e78ac21SJeffrey Hsu for (; m; m = m0) { 837df8bae1dSRodney W. Grimes m0 = m->m_nextpkt; 838b375c9ecSLuigi Rizzo m->m_nextpkt = 0; 83907203494SDaniel C. Sobral if (error == 0) { 840fe937674SJosef Karthauser /* Record statistics for this interface address. */ 84107203494SDaniel C. Sobral if (ia != NULL) { 8427caf4ab7SGleb Smirnoff counter_u64_add(ia->ia_ifa.ifa_opackets, 1); 8437caf4ab7SGleb Smirnoff counter_u64_add(ia->ia_ifa.ifa_obytes, 8447caf4ab7SGleb Smirnoff m->m_pkthdr.len); 84507203494SDaniel C. Sobral } 846147f74d1SAndre Oppermann /* 847147f74d1SAndre Oppermann * Reset layer specific mbuf flags 848147f74d1SAndre Oppermann * to avoid confusing upper layers. 849147f74d1SAndre Oppermann */ 85086bd0491SAndre Oppermann m_clrprotoflags(m); 851fe937674SJosef Karthauser 8522e77d270SAndrey V. Elsukov IP_PROBE(send, NULL, NULL, mtod(m, struct ip *), ifp, 8532e77d270SAndrey V. Elsukov mtod(m, struct ip *), NULL); 85435c7bb34SRandall Stewart error = ip_output_send(inp, ifp, m, gw, ro, true); 855fe937674SJosef Karthauser } else 856df8bae1dSRodney W. Grimes m_freem(m); 857df8bae1dSRodney W. Grimes } 858df8bae1dSRodney W. Grimes 859df8bae1dSRodney W. Grimes if (error == 0) 86086425c62SRobert Watson IPSTAT_INC(ips_fragmented); 8611e78ac21SJeffrey Hsu 862df8bae1dSRodney W. Grimes done: 863df8bae1dSRodney W. Grimes return (error); 864df8bae1dSRodney W. Grimes bad: 8653528d68fSBill Paul m_freem(m); 866df8bae1dSRodney W. Grimes goto done; 867df8bae1dSRodney W. Grimes } 868df8bae1dSRodney W. Grimes 8691e78ac21SJeffrey Hsu /* 8701e78ac21SJeffrey Hsu * Create a chain of fragments which fit the given mtu. m_frag points to the 8711e78ac21SJeffrey Hsu * mbuf to be fragmented; on return it points to the chain with the fragments. 8721e78ac21SJeffrey Hsu * Return 0 if no error. If error, m_frag may contain a partially built 8731e78ac21SJeffrey Hsu * chain of fragments that should be freed by the caller. 8741e78ac21SJeffrey Hsu * 8751e78ac21SJeffrey Hsu * if_hwassist_flags is the hw offload capabilities (see if_data.ifi_hwassist) 8761e78ac21SJeffrey Hsu */ 8771e78ac21SJeffrey Hsu int 8781e78ac21SJeffrey Hsu ip_fragment(struct ip *ip, struct mbuf **m_frag, int mtu, 879078468edSGleb Smirnoff u_long if_hwassist_flags) 8801e78ac21SJeffrey Hsu { 8811e78ac21SJeffrey Hsu int error = 0; 8821e78ac21SJeffrey Hsu int hlen = ip->ip_hl << 2; 8831e78ac21SJeffrey Hsu int len = (mtu - hlen) & ~7; /* size of payload in each fragment */ 8841e78ac21SJeffrey Hsu int off; 8851e78ac21SJeffrey Hsu struct mbuf *m0 = *m_frag; /* the original packet */ 8861e78ac21SJeffrey Hsu int firstlen; 8871e78ac21SJeffrey Hsu struct mbuf **mnext; 8881e78ac21SJeffrey Hsu int nfrags; 88921d172a3SGleb Smirnoff uint16_t ip_len, ip_off; 8901e78ac21SJeffrey Hsu 89121d172a3SGleb Smirnoff ip_len = ntohs(ip->ip_len); 89221d172a3SGleb Smirnoff ip_off = ntohs(ip->ip_off); 89321d172a3SGleb Smirnoff 89421d172a3SGleb Smirnoff if (ip_off & IP_DF) { /* Fragmentation not allowed */ 89586425c62SRobert Watson IPSTAT_INC(ips_cantfrag); 8961e78ac21SJeffrey Hsu return EMSGSIZE; 8971e78ac21SJeffrey Hsu } 8981e78ac21SJeffrey Hsu 8991e78ac21SJeffrey Hsu /* 9001e78ac21SJeffrey Hsu * Must be able to put at least 8 bytes per fragment. 9011e78ac21SJeffrey Hsu */ 9021e78ac21SJeffrey Hsu if (len < 8) 9031e78ac21SJeffrey Hsu return EMSGSIZE; 9041e78ac21SJeffrey Hsu 9051e78ac21SJeffrey Hsu /* 9061e78ac21SJeffrey Hsu * If the interface will not calculate checksums on 9071e78ac21SJeffrey Hsu * fragmented packets, then do it here. 9081e78ac21SJeffrey Hsu */ 909da2299c5SAndre Oppermann if (m0->m_pkthdr.csum_flags & CSUM_DELAY_DATA) { 91082334850SJohn Baldwin m0 = mb_unmapped_to_ext(m0); 91182334850SJohn Baldwin if (m0 == NULL) { 91282334850SJohn Baldwin error = ENOBUFS; 91382334850SJohn Baldwin IPSTAT_INC(ips_odropped); 91482334850SJohn Baldwin goto done; 91582334850SJohn Baldwin } 9161e78ac21SJeffrey Hsu in_delayed_cksum(m0); 9171e78ac21SJeffrey Hsu m0->m_pkthdr.csum_flags &= ~CSUM_DELAY_DATA; 9181e78ac21SJeffrey Hsu } 91995033af9SMark Johnston #if defined(SCTP) || defined(SCTP_SUPPORT) 920da2299c5SAndre Oppermann if (m0->m_pkthdr.csum_flags & CSUM_SCTP) { 92182334850SJohn Baldwin m0 = mb_unmapped_to_ext(m0); 92282334850SJohn Baldwin if (m0 == NULL) { 92382334850SJohn Baldwin error = ENOBUFS; 92482334850SJohn Baldwin IPSTAT_INC(ips_odropped); 92582334850SJohn Baldwin goto done; 92682334850SJohn Baldwin } 9271966e5b5SRandall Stewart sctp_delayed_cksum(m0, hlen); 9282f4afd21SRandall Stewart m0->m_pkthdr.csum_flags &= ~CSUM_SCTP; 9292f4afd21SRandall Stewart } 9302f4afd21SRandall Stewart #endif 9311e78ac21SJeffrey Hsu if (len > PAGE_SIZE) { 9321e78ac21SJeffrey Hsu /* 9331e78ac21SJeffrey Hsu * Fragment large datagrams such that each segment 9341e78ac21SJeffrey Hsu * contains a multiple of PAGE_SIZE amount of data, 9351e78ac21SJeffrey Hsu * plus headers. This enables a receiver to perform 9361e78ac21SJeffrey Hsu * page-flipping zero-copy optimizations. 9371e78ac21SJeffrey Hsu * 9381e78ac21SJeffrey Hsu * XXX When does this help given that sender and receiver 9391e78ac21SJeffrey Hsu * could have different page sizes, and also mtu could 9401e78ac21SJeffrey Hsu * be less than the receiver's page size ? 9411e78ac21SJeffrey Hsu */ 9421e78ac21SJeffrey Hsu int newlen; 9431e78ac21SJeffrey Hsu 9449c0f6aa7SHans Petter Selasky off = MIN(mtu, m0->m_pkthdr.len); 9451e78ac21SJeffrey Hsu 9461e78ac21SJeffrey Hsu /* 9471e78ac21SJeffrey Hsu * firstlen (off - hlen) must be aligned on an 9481e78ac21SJeffrey Hsu * 8-byte boundary 9491e78ac21SJeffrey Hsu */ 9501e78ac21SJeffrey Hsu if (off < hlen) 9511e78ac21SJeffrey Hsu goto smart_frag_failure; 9521e78ac21SJeffrey Hsu off = ((off - hlen) & ~7) + hlen; 9531e78ac21SJeffrey Hsu newlen = (~PAGE_MASK) & mtu; 9541e78ac21SJeffrey Hsu if ((newlen + sizeof (struct ip)) > mtu) { 9551e78ac21SJeffrey Hsu /* we failed, go back the default */ 9561e78ac21SJeffrey Hsu smart_frag_failure: 9571e78ac21SJeffrey Hsu newlen = len; 9581e78ac21SJeffrey Hsu off = hlen + len; 9591e78ac21SJeffrey Hsu } 9601e78ac21SJeffrey Hsu len = newlen; 9611e78ac21SJeffrey Hsu 9621e78ac21SJeffrey Hsu } else { 9631e78ac21SJeffrey Hsu off = hlen + len; 9641e78ac21SJeffrey Hsu } 9651e78ac21SJeffrey Hsu 9661e78ac21SJeffrey Hsu firstlen = off - hlen; 9671e78ac21SJeffrey Hsu mnext = &m0->m_nextpkt; /* pointer to next packet */ 9681e78ac21SJeffrey Hsu 9691e78ac21SJeffrey Hsu /* 9701e78ac21SJeffrey Hsu * Loop through length of segment after first fragment, 9711e78ac21SJeffrey Hsu * make new header and copy data of each part and link onto chain. 9721e78ac21SJeffrey Hsu * Here, m0 is the original packet, m is the fragment being created. 9731e78ac21SJeffrey Hsu * The fragments are linked off the m_nextpkt of the original 9741e78ac21SJeffrey Hsu * packet, which after processing serves as the first fragment. 9751e78ac21SJeffrey Hsu */ 97621d172a3SGleb Smirnoff for (nfrags = 1; off < ip_len; off += len, nfrags++) { 9771e78ac21SJeffrey Hsu struct ip *mhip; /* ip header on the fragment */ 9781e78ac21SJeffrey Hsu struct mbuf *m; 9791e78ac21SJeffrey Hsu int mhlen = sizeof (struct ip); 9801e78ac21SJeffrey Hsu 981dc4ad05eSGleb Smirnoff m = m_gethdr(M_NOWAIT, MT_DATA); 9820b17fba7SAndre Oppermann if (m == NULL) { 9831e78ac21SJeffrey Hsu error = ENOBUFS; 98486425c62SRobert Watson IPSTAT_INC(ips_odropped); 9851e78ac21SJeffrey Hsu goto done; 9861e78ac21SJeffrey Hsu } 987c4c4346fSHans Petter Selasky /* 988c4c4346fSHans Petter Selasky * Make sure the complete packet header gets copied 989c4c4346fSHans Petter Selasky * from the originating mbuf to the newly created 990c4c4346fSHans Petter Selasky * mbuf. This also ensures that existing firewall 991c4c4346fSHans Petter Selasky * classification(s), VLAN tags and so on get copied 992c4c4346fSHans Petter Selasky * to the resulting fragmented packet(s): 993c4c4346fSHans Petter Selasky */ 994c4c4346fSHans Petter Selasky if (m_dup_pkthdr(m, m0, M_NOWAIT) == 0) { 995c4c4346fSHans Petter Selasky m_free(m); 996c4c4346fSHans Petter Selasky error = ENOBUFS; 997c4c4346fSHans Petter Selasky IPSTAT_INC(ips_odropped); 998c4c4346fSHans Petter Selasky goto done; 999c4c4346fSHans Petter Selasky } 10001e78ac21SJeffrey Hsu /* 10011e78ac21SJeffrey Hsu * In the first mbuf, leave room for the link header, then 10021e78ac21SJeffrey Hsu * copy the original IP header including options. The payload 10038b889dbbSBruce M Simpson * goes into an additional mbuf chain returned by m_copym(). 10041e78ac21SJeffrey Hsu */ 10051e78ac21SJeffrey Hsu m->m_data += max_linkhdr; 10061e78ac21SJeffrey Hsu mhip = mtod(m, struct ip *); 10071e78ac21SJeffrey Hsu *mhip = *ip; 10081e78ac21SJeffrey Hsu if (hlen > sizeof (struct ip)) { 10091e78ac21SJeffrey Hsu mhlen = ip_optcopy(ip, mhip) + sizeof (struct ip); 10101e78ac21SJeffrey Hsu mhip->ip_v = IPVERSION; 10111e78ac21SJeffrey Hsu mhip->ip_hl = mhlen >> 2; 10121e78ac21SJeffrey Hsu } 10131e78ac21SJeffrey Hsu m->m_len = mhlen; 101421d172a3SGleb Smirnoff /* XXX do we need to add ip_off below ? */ 101521d172a3SGleb Smirnoff mhip->ip_off = ((off - hlen) >> 3) + ip_off; 1016fb86dfcdSAndre Oppermann if (off + len >= ip_len) 101721d172a3SGleb Smirnoff len = ip_len - off; 1018fb86dfcdSAndre Oppermann else 10191e78ac21SJeffrey Hsu mhip->ip_off |= IP_MF; 10201e78ac21SJeffrey Hsu mhip->ip_len = htons((u_short)(len + mhlen)); 1021eb1b1807SGleb Smirnoff m->m_next = m_copym(m0, off, len, M_NOWAIT); 10220b17fba7SAndre Oppermann if (m->m_next == NULL) { /* copy failed */ 10231e78ac21SJeffrey Hsu m_free(m); 10241e78ac21SJeffrey Hsu error = ENOBUFS; /* ??? */ 102586425c62SRobert Watson IPSTAT_INC(ips_odropped); 10261e78ac21SJeffrey Hsu goto done; 10271e78ac21SJeffrey Hsu } 10281e78ac21SJeffrey Hsu m->m_pkthdr.len = mhlen + len; 10291e78ac21SJeffrey Hsu #ifdef MAC 103030d239bcSRobert Watson mac_netinet_fragment(m0, m); 10311e78ac21SJeffrey Hsu #endif 10321e78ac21SJeffrey Hsu mhip->ip_off = htons(mhip->ip_off); 10331e78ac21SJeffrey Hsu mhip->ip_sum = 0; 1034078468edSGleb Smirnoff if (m->m_pkthdr.csum_flags & CSUM_IP & ~if_hwassist_flags) { 10351e78ac21SJeffrey Hsu mhip->ip_sum = in_cksum(m, mhlen); 1036078468edSGleb Smirnoff m->m_pkthdr.csum_flags &= ~CSUM_IP; 1037078468edSGleb Smirnoff } 10381e78ac21SJeffrey Hsu *mnext = m; 10391e78ac21SJeffrey Hsu mnext = &m->m_nextpkt; 10401e78ac21SJeffrey Hsu } 104186425c62SRobert Watson IPSTAT_ADD(ips_ofragments, nfrags); 10421e78ac21SJeffrey Hsu 10431e78ac21SJeffrey Hsu /* 10441e78ac21SJeffrey Hsu * Update first fragment by trimming what's been copied out 10451e78ac21SJeffrey Hsu * and updating header. 10461e78ac21SJeffrey Hsu */ 104721d172a3SGleb Smirnoff m_adj(m0, hlen + firstlen - ip_len); 10481e78ac21SJeffrey Hsu m0->m_pkthdr.len = hlen + firstlen; 10491e78ac21SJeffrey Hsu ip->ip_len = htons((u_short)m0->m_pkthdr.len); 105021d172a3SGleb Smirnoff ip->ip_off = htons(ip_off | IP_MF); 10511e78ac21SJeffrey Hsu ip->ip_sum = 0; 1052078468edSGleb Smirnoff if (m0->m_pkthdr.csum_flags & CSUM_IP & ~if_hwassist_flags) { 10531e78ac21SJeffrey Hsu ip->ip_sum = in_cksum(m0, hlen); 1054078468edSGleb Smirnoff m0->m_pkthdr.csum_flags &= ~CSUM_IP; 1055078468edSGleb Smirnoff } 10561e78ac21SJeffrey Hsu 10571e78ac21SJeffrey Hsu done: 10581e78ac21SJeffrey Hsu *m_frag = m0; 10591e78ac21SJeffrey Hsu return error; 10601e78ac21SJeffrey Hsu } 10611e78ac21SJeffrey Hsu 10621c238475SJonathan Lemon void 1063db4f9cc7SJonathan Lemon in_delayed_cksum(struct mbuf *m) 1064db4f9cc7SJonathan Lemon { 1065db4f9cc7SJonathan Lemon struct ip *ip; 10661fdbfb90STom Jones struct udphdr *uh; 10671fdbfb90STom Jones uint16_t cklen, csum, offset; 1068db4f9cc7SJonathan Lemon 1069db4f9cc7SJonathan Lemon ip = mtod(m, struct ip *); 107053be11f6SPoul-Henning Kamp offset = ip->ip_hl << 2 ; 10711fdbfb90STom Jones 10721fdbfb90STom Jones if (m->m_pkthdr.csum_flags & CSUM_UDP) { 10731fdbfb90STom Jones /* if udp header is not in the first mbuf copy udplen */ 1074b6e87011STom Jones if (offset + sizeof(struct udphdr) > m->m_len) { 1075590d0a43SAndrey V. Elsukov m_copydata(m, offset + offsetof(struct udphdr, 1076590d0a43SAndrey V. Elsukov uh_ulen), sizeof(cklen), (caddr_t)&cklen); 1077b6e87011STom Jones cklen = ntohs(cklen); 1078b6e87011STom Jones } else { 1079590d0a43SAndrey V. Elsukov uh = (struct udphdr *)mtodo(m, offset); 10801fdbfb90STom Jones cklen = ntohs(uh->uh_ulen); 10811fdbfb90STom Jones } 10821fdbfb90STom Jones csum = in_cksum_skip(m, cklen + offset, offset); 10831fdbfb90STom Jones if (csum == 0) 1084206a3274SRuslan Ermilov csum = 0xffff; 10851fdbfb90STom Jones } else { 10861fdbfb90STom Jones cklen = ntohs(ip->ip_len); 10871fdbfb90STom Jones csum = in_cksum_skip(m, cklen, offset); 10881fdbfb90STom Jones } 1089db4f9cc7SJonathan Lemon offset += m->m_pkthdr.csum_data; /* checksum offset */ 1090db4f9cc7SJonathan Lemon 1091590d0a43SAndrey V. Elsukov if (offset + sizeof(csum) > m->m_len) 1092590d0a43SAndrey V. Elsukov m_copyback(m, offset, sizeof(csum), (caddr_t)&csum); 1093590d0a43SAndrey V. Elsukov else 1094590d0a43SAndrey V. Elsukov *(u_short *)mtodo(m, offset) = csum; 1095db4f9cc7SJonathan Lemon } 1096db4f9cc7SJonathan Lemon 1097df8bae1dSRodney W. Grimes /* 1098df8bae1dSRodney W. Grimes * IP socket option processing. 1099df8bae1dSRodney W. Grimes */ 1100df8bae1dSRodney W. Grimes int 1101f2565d68SRobert Watson ip_ctloutput(struct socket *so, struct sockopt *sopt) 1102df8bae1dSRodney W. Grimes { 1103cfe8b629SGarrett Wollman struct inpcb *inp = sotoinpcb(so); 1104cfe8b629SGarrett Wollman int error, optval; 1105dc847eb6SAdrian Chadd #ifdef RSS 1106dc847eb6SAdrian Chadd uint32_t rss_bucket; 1107dc847eb6SAdrian Chadd int retval; 1108dc847eb6SAdrian Chadd #endif 1109df8bae1dSRodney W. Grimes 1110cfe8b629SGarrett Wollman error = optval = 0; 1111cfe8b629SGarrett Wollman if (sopt->sopt_level != IPPROTO_IP) { 1112fc06cd42SMikolaj Golub error = EINVAL; 1113fc06cd42SMikolaj Golub 1114fc06cd42SMikolaj Golub if (sopt->sopt_level == SOL_SOCKET && 1115fc06cd42SMikolaj Golub sopt->sopt_dir == SOPT_SET) { 1116fc06cd42SMikolaj Golub switch (sopt->sopt_name) { 1117fc06cd42SMikolaj Golub case SO_REUSEADDR: 1118fc06cd42SMikolaj Golub INP_WLOCK(inp); 1119efdf104bSMikolaj Golub if ((so->so_options & SO_REUSEADDR) != 0) 1120efdf104bSMikolaj Golub inp->inp_flags2 |= INP_REUSEADDR; 1121fc06cd42SMikolaj Golub else 1122efdf104bSMikolaj Golub inp->inp_flags2 &= ~INP_REUSEADDR; 1123fc06cd42SMikolaj Golub INP_WUNLOCK(inp); 1124fc06cd42SMikolaj Golub error = 0; 1125fc06cd42SMikolaj Golub break; 1126fc06cd42SMikolaj Golub case SO_REUSEPORT: 1127fc06cd42SMikolaj Golub INP_WLOCK(inp); 1128fc06cd42SMikolaj Golub if ((so->so_options & SO_REUSEPORT) != 0) 1129fc06cd42SMikolaj Golub inp->inp_flags2 |= INP_REUSEPORT; 1130fc06cd42SMikolaj Golub else 1131fc06cd42SMikolaj Golub inp->inp_flags2 &= ~INP_REUSEPORT; 1132fc06cd42SMikolaj Golub INP_WUNLOCK(inp); 1133fc06cd42SMikolaj Golub error = 0; 1134fc06cd42SMikolaj Golub break; 11351a43cff9SSean Bruno case SO_REUSEPORT_LB: 11361a43cff9SSean Bruno INP_WLOCK(inp); 11371a43cff9SSean Bruno if ((so->so_options & SO_REUSEPORT_LB) != 0) 11381a43cff9SSean Bruno inp->inp_flags2 |= INP_REUSEPORT_LB; 11391a43cff9SSean Bruno else 11401a43cff9SSean Bruno inp->inp_flags2 &= ~INP_REUSEPORT_LB; 11411a43cff9SSean Bruno INP_WUNLOCK(inp); 11421a43cff9SSean Bruno error = 0; 11431a43cff9SSean Bruno break; 1144fc06cd42SMikolaj Golub case SO_SETFIB: 1145fc06cd42SMikolaj Golub INP_WLOCK(inp); 1146fc06cd42SMikolaj Golub inp->inp_inc.inc_fibnum = so->so_fibnum; 1147fc06cd42SMikolaj Golub INP_WUNLOCK(inp); 1148fc06cd42SMikolaj Golub error = 0; 1149fc06cd42SMikolaj Golub break; 1150f3e7afe2SHans Petter Selasky case SO_MAX_PACING_RATE: 1151f3e7afe2SHans Petter Selasky #ifdef RATELIMIT 1152f3e7afe2SHans Petter Selasky INP_WLOCK(inp); 1153f3e7afe2SHans Petter Selasky inp->inp_flags2 |= INP_RATE_LIMIT_CHANGED; 1154f3e7afe2SHans Petter Selasky INP_WUNLOCK(inp); 1155f3e7afe2SHans Petter Selasky error = 0; 1156f3e7afe2SHans Petter Selasky #else 1157f3e7afe2SHans Petter Selasky error = EOPNOTSUPP; 1158f3e7afe2SHans Petter Selasky #endif 1159f3e7afe2SHans Petter Selasky break; 1160fc06cd42SMikolaj Golub default: 1161fc06cd42SMikolaj Golub break; 1162fc06cd42SMikolaj Golub } 1163fc06cd42SMikolaj Golub } 1164fc06cd42SMikolaj Golub return (error); 1165cfe8b629SGarrett Wollman } 1166df8bae1dSRodney W. Grimes 1167cfe8b629SGarrett Wollman switch (sopt->sopt_dir) { 1168cfe8b629SGarrett Wollman case SOPT_SET: 1169cfe8b629SGarrett Wollman switch (sopt->sopt_name) { 1170df8bae1dSRodney W. Grimes case IP_OPTIONS: 1171df8bae1dSRodney W. Grimes #ifdef notyet 1172df8bae1dSRodney W. Grimes case IP_RETOPTS: 1173df8bae1dSRodney W. Grimes #endif 1174cfe8b629SGarrett Wollman { 1175cfe8b629SGarrett Wollman struct mbuf *m; 1176cfe8b629SGarrett Wollman if (sopt->sopt_valsize > MLEN) { 1177cfe8b629SGarrett Wollman error = EMSGSIZE; 1178cfe8b629SGarrett Wollman break; 1179cfe8b629SGarrett Wollman } 1180dc4ad05eSGleb Smirnoff m = m_get(sopt->sopt_td ? M_WAITOK : M_NOWAIT, MT_DATA); 11810b17fba7SAndre Oppermann if (m == NULL) { 1182cfe8b629SGarrett Wollman error = ENOBUFS; 1183cfe8b629SGarrett Wollman break; 1184cfe8b629SGarrett Wollman } 1185cfe8b629SGarrett Wollman m->m_len = sopt->sopt_valsize; 1186cfe8b629SGarrett Wollman error = sooptcopyin(sopt, mtod(m, char *), m->m_len, 1187cfe8b629SGarrett Wollman m->m_len); 1188635354c4SMaxim Konovalov if (error) { 1189635354c4SMaxim Konovalov m_free(m); 1190635354c4SMaxim Konovalov break; 1191635354c4SMaxim Konovalov } 11928501a69cSRobert Watson INP_WLOCK(inp); 1193993d9505SRobert Watson error = ip_pcbopts(inp, sopt->sopt_name, m); 11948501a69cSRobert Watson INP_WUNLOCK(inp); 1195993d9505SRobert Watson return (error); 1196cfe8b629SGarrett Wollman } 1197df8bae1dSRodney W. Grimes 1198f44270e7SPawel Jakub Dawidek case IP_BINDANY: 1199f44270e7SPawel Jakub Dawidek if (sopt->sopt_td != NULL) { 1200f44270e7SPawel Jakub Dawidek error = priv_check(sopt->sopt_td, 1201f44270e7SPawel Jakub Dawidek PRIV_NETINET_BINDANY); 1202f44270e7SPawel Jakub Dawidek if (error) 1203be9347e3SAdrian Chadd break; 1204be9347e3SAdrian Chadd } 1205cef27294SAdrian Chadd /* FALLTHROUGH */ 12060a100a6fSAdrian Chadd case IP_BINDMULTI: 12070a100a6fSAdrian Chadd #ifdef RSS 12080a100a6fSAdrian Chadd case IP_RSS_LISTEN_BUCKET: 12090a100a6fSAdrian Chadd #endif 1210df8bae1dSRodney W. Grimes case IP_TOS: 1211df8bae1dSRodney W. Grimes case IP_TTL: 1212936cd18dSAndre Oppermann case IP_MINTTL: 1213df8bae1dSRodney W. Grimes case IP_RECVOPTS: 1214df8bae1dSRodney W. Grimes case IP_RECVRETOPTS: 1215dce33a45SErmal Luçi case IP_ORIGDSTADDR: 1216df8bae1dSRodney W. Grimes case IP_RECVDSTADDR: 12174957466bSMatthew N. Dodd case IP_RECVTTL: 121882c23ebaSBill Fenner case IP_RECVIF: 12198afa2304SBruce M Simpson case IP_ONESBCAST: 1220b2828ad2SAndre Oppermann case IP_DONTFRAG: 12213cca425bSMichael Tuexen case IP_RECVTOS: 12229d3ddf43SAdrian Chadd case IP_RECVFLOWID: 12239d3ddf43SAdrian Chadd #ifdef RSS 12249d3ddf43SAdrian Chadd case IP_RECVRSSBUCKETID: 12259d3ddf43SAdrian Chadd #endif 1226868aabb4SRichard Scheffenegger case IP_VLAN_PCP: 1227cfe8b629SGarrett Wollman error = sooptcopyin(sopt, &optval, sizeof optval, 1228cfe8b629SGarrett Wollman sizeof optval); 1229cfe8b629SGarrett Wollman if (error) 1230cfe8b629SGarrett Wollman break; 1231df8bae1dSRodney W. Grimes 1232cfe8b629SGarrett Wollman switch (sopt->sopt_name) { 1233df8bae1dSRodney W. Grimes case IP_TOS: 1234ca98b82cSDavid Greenman inp->inp_ip_tos = optval; 1235df8bae1dSRodney W. Grimes break; 1236df8bae1dSRodney W. Grimes 1237df8bae1dSRodney W. Grimes case IP_TTL: 1238ca98b82cSDavid Greenman inp->inp_ip_ttl = optval; 1239df8bae1dSRodney W. Grimes break; 1240936cd18dSAndre Oppermann 1241936cd18dSAndre Oppermann case IP_MINTTL: 1242a603c811SRobert Watson if (optval >= 0 && optval <= MAXTTL) 1243936cd18dSAndre Oppermann inp->inp_ip_minttl = optval; 1244936cd18dSAndre Oppermann else 1245936cd18dSAndre Oppermann error = EINVAL; 1246936cd18dSAndre Oppermann break; 1247936cd18dSAndre Oppermann 1248a138d217SRobert Watson #define OPTSET(bit) do { \ 12498501a69cSRobert Watson INP_WLOCK(inp); \ 1250df8bae1dSRodney W. Grimes if (optval) \ 1251df8bae1dSRodney W. Grimes inp->inp_flags |= bit; \ 1252df8bae1dSRodney W. Grimes else \ 1253a138d217SRobert Watson inp->inp_flags &= ~bit; \ 12548501a69cSRobert Watson INP_WUNLOCK(inp); \ 1255a138d217SRobert Watson } while (0) 1256df8bae1dSRodney W. Grimes 12570a100a6fSAdrian Chadd #define OPTSET2(bit, val) do { \ 12580a100a6fSAdrian Chadd INP_WLOCK(inp); \ 12590a100a6fSAdrian Chadd if (val) \ 12600a100a6fSAdrian Chadd inp->inp_flags2 |= bit; \ 12610a100a6fSAdrian Chadd else \ 12620a100a6fSAdrian Chadd inp->inp_flags2 &= ~bit; \ 12630a100a6fSAdrian Chadd INP_WUNLOCK(inp); \ 12640a100a6fSAdrian Chadd } while (0) 12650a100a6fSAdrian Chadd 1266df8bae1dSRodney W. Grimes case IP_RECVOPTS: 1267df8bae1dSRodney W. Grimes OPTSET(INP_RECVOPTS); 1268df8bae1dSRodney W. Grimes break; 1269df8bae1dSRodney W. Grimes 1270df8bae1dSRodney W. Grimes case IP_RECVRETOPTS: 1271df8bae1dSRodney W. Grimes OPTSET(INP_RECVRETOPTS); 1272df8bae1dSRodney W. Grimes break; 1273df8bae1dSRodney W. Grimes 1274df8bae1dSRodney W. Grimes case IP_RECVDSTADDR: 1275df8bae1dSRodney W. Grimes OPTSET(INP_RECVDSTADDR); 1276df8bae1dSRodney W. Grimes break; 127782c23ebaSBill Fenner 1278dce33a45SErmal Luçi case IP_ORIGDSTADDR: 1279dce33a45SErmal Luçi OPTSET2(INP_ORIGDSTADDR, optval); 1280dce33a45SErmal Luçi break; 1281dce33a45SErmal Luçi 12824957466bSMatthew N. Dodd case IP_RECVTTL: 12834957466bSMatthew N. Dodd OPTSET(INP_RECVTTL); 12844957466bSMatthew N. Dodd break; 12854957466bSMatthew N. Dodd 128682c23ebaSBill Fenner case IP_RECVIF: 128782c23ebaSBill Fenner OPTSET(INP_RECVIF); 128882c23ebaSBill Fenner break; 12896a800098SYoshinobu Inoue 12908afa2304SBruce M Simpson case IP_ONESBCAST: 12918afa2304SBruce M Simpson OPTSET(INP_ONESBCAST); 12928afa2304SBruce M Simpson break; 1293b2828ad2SAndre Oppermann case IP_DONTFRAG: 1294b2828ad2SAndre Oppermann OPTSET(INP_DONTFRAG); 1295b2828ad2SAndre Oppermann break; 1296f44270e7SPawel Jakub Dawidek case IP_BINDANY: 1297f44270e7SPawel Jakub Dawidek OPTSET(INP_BINDANY); 1298be9347e3SAdrian Chadd break; 12993cca425bSMichael Tuexen case IP_RECVTOS: 13003cca425bSMichael Tuexen OPTSET(INP_RECVTOS); 13013cca425bSMichael Tuexen break; 13020a100a6fSAdrian Chadd case IP_BINDMULTI: 13030a100a6fSAdrian Chadd OPTSET2(INP_BINDMULTI, optval); 13040a100a6fSAdrian Chadd break; 13059d3ddf43SAdrian Chadd case IP_RECVFLOWID: 13069d3ddf43SAdrian Chadd OPTSET2(INP_RECVFLOWID, optval); 13079d3ddf43SAdrian Chadd break; 13080a100a6fSAdrian Chadd #ifdef RSS 13090a100a6fSAdrian Chadd case IP_RSS_LISTEN_BUCKET: 13100a100a6fSAdrian Chadd if ((optval >= 0) && 13110a100a6fSAdrian Chadd (optval < rss_getnumbuckets())) { 13120a100a6fSAdrian Chadd inp->inp_rss_listen_bucket = optval; 13130a100a6fSAdrian Chadd OPTSET2(INP_RSS_BUCKET_SET, 1); 13140a100a6fSAdrian Chadd } else { 13150a100a6fSAdrian Chadd error = EINVAL; 13160a100a6fSAdrian Chadd } 13170a100a6fSAdrian Chadd break; 13189d3ddf43SAdrian Chadd case IP_RECVRSSBUCKETID: 13199d3ddf43SAdrian Chadd OPTSET2(INP_RECVRSSBUCKETID, optval); 13209d3ddf43SAdrian Chadd break; 13210a100a6fSAdrian Chadd #endif 1322868aabb4SRichard Scheffenegger case IP_VLAN_PCP: 1323868aabb4SRichard Scheffenegger if ((optval >= -1) && (optval <= 1324868aabb4SRichard Scheffenegger (INP_2PCP_MASK >> INP_2PCP_SHIFT))) { 1325868aabb4SRichard Scheffenegger if (optval == -1) { 1326868aabb4SRichard Scheffenegger INP_WLOCK(inp); 1327868aabb4SRichard Scheffenegger inp->inp_flags2 &= 1328868aabb4SRichard Scheffenegger ~(INP_2PCP_SET | 1329868aabb4SRichard Scheffenegger INP_2PCP_MASK); 1330868aabb4SRichard Scheffenegger INP_WUNLOCK(inp); 1331868aabb4SRichard Scheffenegger } else { 1332868aabb4SRichard Scheffenegger INP_WLOCK(inp); 1333868aabb4SRichard Scheffenegger inp->inp_flags2 |= 1334868aabb4SRichard Scheffenegger INP_2PCP_SET; 1335868aabb4SRichard Scheffenegger inp->inp_flags2 &= 1336868aabb4SRichard Scheffenegger ~INP_2PCP_MASK; 1337868aabb4SRichard Scheffenegger inp->inp_flags2 |= 1338868aabb4SRichard Scheffenegger optval << INP_2PCP_SHIFT; 1339868aabb4SRichard Scheffenegger INP_WUNLOCK(inp); 1340868aabb4SRichard Scheffenegger } 1341868aabb4SRichard Scheffenegger } else 1342868aabb4SRichard Scheffenegger error = EINVAL; 1343868aabb4SRichard Scheffenegger break; 1344df8bae1dSRodney W. Grimes } 1345df8bae1dSRodney W. Grimes break; 1346df8bae1dSRodney W. Grimes #undef OPTSET 13470a100a6fSAdrian Chadd #undef OPTSET2 1348df8bae1dSRodney W. Grimes 134971498f30SBruce M Simpson /* 135071498f30SBruce M Simpson * Multicast socket options are processed by the in_mcast 135171498f30SBruce M Simpson * module. 135271498f30SBruce M Simpson */ 1353df8bae1dSRodney W. Grimes case IP_MULTICAST_IF: 1354f0068c4aSGarrett Wollman case IP_MULTICAST_VIF: 1355df8bae1dSRodney W. Grimes case IP_MULTICAST_TTL: 1356df8bae1dSRodney W. Grimes case IP_MULTICAST_LOOP: 1357df8bae1dSRodney W. Grimes case IP_ADD_MEMBERSHIP: 1358df8bae1dSRodney W. Grimes case IP_DROP_MEMBERSHIP: 135971498f30SBruce M Simpson case IP_ADD_SOURCE_MEMBERSHIP: 136071498f30SBruce M Simpson case IP_DROP_SOURCE_MEMBERSHIP: 136171498f30SBruce M Simpson case IP_BLOCK_SOURCE: 136271498f30SBruce M Simpson case IP_UNBLOCK_SOURCE: 136371498f30SBruce M Simpson case IP_MSFILTER: 136471498f30SBruce M Simpson case MCAST_JOIN_GROUP: 136571498f30SBruce M Simpson case MCAST_LEAVE_GROUP: 136671498f30SBruce M Simpson case MCAST_JOIN_SOURCE_GROUP: 136771498f30SBruce M Simpson case MCAST_LEAVE_SOURCE_GROUP: 136871498f30SBruce M Simpson case MCAST_BLOCK_SOURCE: 136971498f30SBruce M Simpson case MCAST_UNBLOCK_SOURCE: 137071498f30SBruce M Simpson error = inp_setmoptions(inp, sopt); 1371df8bae1dSRodney W. Grimes break; 1372df8bae1dSRodney W. Grimes 137333b3ac06SPeter Wemm case IP_PORTRANGE: 1374cfe8b629SGarrett Wollman error = sooptcopyin(sopt, &optval, sizeof optval, 1375cfe8b629SGarrett Wollman sizeof optval); 1376cfe8b629SGarrett Wollman if (error) 1377cfe8b629SGarrett Wollman break; 137833b3ac06SPeter Wemm 13798501a69cSRobert Watson INP_WLOCK(inp); 138033b3ac06SPeter Wemm switch (optval) { 138133b3ac06SPeter Wemm case IP_PORTRANGE_DEFAULT: 138233b3ac06SPeter Wemm inp->inp_flags &= ~(INP_LOWPORT); 138333b3ac06SPeter Wemm inp->inp_flags &= ~(INP_HIGHPORT); 138433b3ac06SPeter Wemm break; 138533b3ac06SPeter Wemm 138633b3ac06SPeter Wemm case IP_PORTRANGE_HIGH: 138733b3ac06SPeter Wemm inp->inp_flags &= ~(INP_LOWPORT); 138833b3ac06SPeter Wemm inp->inp_flags |= INP_HIGHPORT; 138933b3ac06SPeter Wemm break; 139033b3ac06SPeter Wemm 139133b3ac06SPeter Wemm case IP_PORTRANGE_LOW: 139233b3ac06SPeter Wemm inp->inp_flags &= ~(INP_HIGHPORT); 139333b3ac06SPeter Wemm inp->inp_flags |= INP_LOWPORT; 139433b3ac06SPeter Wemm break; 139533b3ac06SPeter Wemm 139633b3ac06SPeter Wemm default: 139733b3ac06SPeter Wemm error = EINVAL; 139833b3ac06SPeter Wemm break; 139933b3ac06SPeter Wemm } 14008501a69cSRobert Watson INP_WUNLOCK(inp); 1401ce8c72b1SPeter Wemm break; 140233b3ac06SPeter Wemm 1403fcf59617SAndrey V. Elsukov #if defined(IPSEC) || defined(IPSEC_SUPPORT) 14046a800098SYoshinobu Inoue case IP_IPSEC_POLICY: 1405fcf59617SAndrey V. Elsukov if (IPSEC_ENABLED(ipv4)) { 1406fcf59617SAndrey V. Elsukov error = IPSEC_PCBCTL(ipv4, inp, sopt); 14076a800098SYoshinobu Inoue break; 14086a800098SYoshinobu Inoue } 1409fcf59617SAndrey V. Elsukov /* FALLTHROUGH */ 1410b2630c29SGeorge V. Neville-Neil #endif /* IPSEC */ 14116a800098SYoshinobu Inoue 1412df8bae1dSRodney W. Grimes default: 1413df8bae1dSRodney W. Grimes error = ENOPROTOOPT; 1414df8bae1dSRodney W. Grimes break; 1415df8bae1dSRodney W. Grimes } 1416df8bae1dSRodney W. Grimes break; 1417df8bae1dSRodney W. Grimes 1418cfe8b629SGarrett Wollman case SOPT_GET: 1419cfe8b629SGarrett Wollman switch (sopt->sopt_name) { 1420df8bae1dSRodney W. Grimes case IP_OPTIONS: 1421df8bae1dSRodney W. Grimes case IP_RETOPTS: 1422e5e3e746SMatt Macy INP_RLOCK(inp); 1423e5e3e746SMatt Macy if (inp->inp_options) { 1424e5e3e746SMatt Macy struct mbuf *options; 1425e5e3e746SMatt Macy 142610731c54SMichael Tuexen options = m_copym(inp->inp_options, 0, 142710731c54SMichael Tuexen M_COPYALL, M_NOWAIT); 1428e5e3e746SMatt Macy INP_RUNLOCK(inp); 1429e5e3e746SMatt Macy if (options != NULL) { 1430cfe8b629SGarrett Wollman error = sooptcopyout(sopt, 1431e5e3e746SMatt Macy mtod(options, char *), 1432e5e3e746SMatt Macy options->m_len); 1433e5e3e746SMatt Macy m_freem(options); 1434e5e3e746SMatt Macy } else 1435e5e3e746SMatt Macy error = ENOMEM; 1436e5e3e746SMatt Macy } else { 1437e5e3e746SMatt Macy INP_RUNLOCK(inp); 1438cfe8b629SGarrett Wollman sopt->sopt_valsize = 0; 1439e5e3e746SMatt Macy } 1440df8bae1dSRodney W. Grimes break; 1441df8bae1dSRodney W. Grimes 1442df8bae1dSRodney W. Grimes case IP_TOS: 1443df8bae1dSRodney W. Grimes case IP_TTL: 1444936cd18dSAndre Oppermann case IP_MINTTL: 1445df8bae1dSRodney W. Grimes case IP_RECVOPTS: 1446df8bae1dSRodney W. Grimes case IP_RECVRETOPTS: 1447dce33a45SErmal Luçi case IP_ORIGDSTADDR: 1448df8bae1dSRodney W. Grimes case IP_RECVDSTADDR: 14494957466bSMatthew N. Dodd case IP_RECVTTL: 145082c23ebaSBill Fenner case IP_RECVIF: 1451cfe8b629SGarrett Wollman case IP_PORTRANGE: 14528afa2304SBruce M Simpson case IP_ONESBCAST: 1453b2828ad2SAndre Oppermann case IP_DONTFRAG: 14545f6bf451SAttilio Rao case IP_BINDANY: 14553cca425bSMichael Tuexen case IP_RECVTOS: 14560a100a6fSAdrian Chadd case IP_BINDMULTI: 14579c423972SAdrian Chadd case IP_FLOWID: 14589c423972SAdrian Chadd case IP_FLOWTYPE: 14599d3ddf43SAdrian Chadd case IP_RECVFLOWID: 14600a100a6fSAdrian Chadd #ifdef RSS 14610a100a6fSAdrian Chadd case IP_RSSBUCKETID: 14629d3ddf43SAdrian Chadd case IP_RECVRSSBUCKETID: 14630a100a6fSAdrian Chadd #endif 1464868aabb4SRichard Scheffenegger case IP_VLAN_PCP: 1465cfe8b629SGarrett Wollman switch (sopt->sopt_name) { 1466df8bae1dSRodney W. Grimes case IP_TOS: 1467ca98b82cSDavid Greenman optval = inp->inp_ip_tos; 1468df8bae1dSRodney W. Grimes break; 1469df8bae1dSRodney W. Grimes 1470df8bae1dSRodney W. Grimes case IP_TTL: 1471ca98b82cSDavid Greenman optval = inp->inp_ip_ttl; 1472df8bae1dSRodney W. Grimes break; 1473df8bae1dSRodney W. Grimes 1474936cd18dSAndre Oppermann case IP_MINTTL: 1475936cd18dSAndre Oppermann optval = inp->inp_ip_minttl; 1476936cd18dSAndre Oppermann break; 1477936cd18dSAndre Oppermann 1478df8bae1dSRodney W. Grimes #define OPTBIT(bit) (inp->inp_flags & bit ? 1 : 0) 14790a100a6fSAdrian Chadd #define OPTBIT2(bit) (inp->inp_flags2 & bit ? 1 : 0) 1480df8bae1dSRodney W. Grimes 1481df8bae1dSRodney W. Grimes case IP_RECVOPTS: 1482df8bae1dSRodney W. Grimes optval = OPTBIT(INP_RECVOPTS); 1483df8bae1dSRodney W. Grimes break; 1484df8bae1dSRodney W. Grimes 1485df8bae1dSRodney W. Grimes case IP_RECVRETOPTS: 1486df8bae1dSRodney W. Grimes optval = OPTBIT(INP_RECVRETOPTS); 1487df8bae1dSRodney W. Grimes break; 1488df8bae1dSRodney W. Grimes 1489df8bae1dSRodney W. Grimes case IP_RECVDSTADDR: 1490df8bae1dSRodney W. Grimes optval = OPTBIT(INP_RECVDSTADDR); 1491df8bae1dSRodney W. Grimes break; 149282c23ebaSBill Fenner 1493dce33a45SErmal Luçi case IP_ORIGDSTADDR: 1494dce33a45SErmal Luçi optval = OPTBIT2(INP_ORIGDSTADDR); 1495dce33a45SErmal Luçi break; 1496dce33a45SErmal Luçi 14974957466bSMatthew N. Dodd case IP_RECVTTL: 14984957466bSMatthew N. Dodd optval = OPTBIT(INP_RECVTTL); 14994957466bSMatthew N. Dodd break; 15004957466bSMatthew N. Dodd 150182c23ebaSBill Fenner case IP_RECVIF: 150282c23ebaSBill Fenner optval = OPTBIT(INP_RECVIF); 150382c23ebaSBill Fenner break; 1504cfe8b629SGarrett Wollman 1505cfe8b629SGarrett Wollman case IP_PORTRANGE: 1506cfe8b629SGarrett Wollman if (inp->inp_flags & INP_HIGHPORT) 1507cfe8b629SGarrett Wollman optval = IP_PORTRANGE_HIGH; 1508cfe8b629SGarrett Wollman else if (inp->inp_flags & INP_LOWPORT) 1509cfe8b629SGarrett Wollman optval = IP_PORTRANGE_LOW; 1510cfe8b629SGarrett Wollman else 1511cfe8b629SGarrett Wollman optval = 0; 1512cfe8b629SGarrett Wollman break; 15136a800098SYoshinobu Inoue 15148afa2304SBruce M Simpson case IP_ONESBCAST: 15158afa2304SBruce M Simpson optval = OPTBIT(INP_ONESBCAST); 15168afa2304SBruce M Simpson break; 1517b2828ad2SAndre Oppermann case IP_DONTFRAG: 1518b2828ad2SAndre Oppermann optval = OPTBIT(INP_DONTFRAG); 1519b2828ad2SAndre Oppermann break; 15205f6bf451SAttilio Rao case IP_BINDANY: 15215f6bf451SAttilio Rao optval = OPTBIT(INP_BINDANY); 15225f6bf451SAttilio Rao break; 15233cca425bSMichael Tuexen case IP_RECVTOS: 15243cca425bSMichael Tuexen optval = OPTBIT(INP_RECVTOS); 15253cca425bSMichael Tuexen break; 15269c423972SAdrian Chadd case IP_FLOWID: 15279c423972SAdrian Chadd optval = inp->inp_flowid; 15289c423972SAdrian Chadd break; 15299c423972SAdrian Chadd case IP_FLOWTYPE: 15309c423972SAdrian Chadd optval = inp->inp_flowtype; 15319c423972SAdrian Chadd break; 15329d3ddf43SAdrian Chadd case IP_RECVFLOWID: 15339d3ddf43SAdrian Chadd optval = OPTBIT2(INP_RECVFLOWID); 15349d3ddf43SAdrian Chadd break; 15359c423972SAdrian Chadd #ifdef RSS 15367847796aSAdrian Chadd case IP_RSSBUCKETID: 15377847796aSAdrian Chadd retval = rss_hash2bucket(inp->inp_flowid, 15387847796aSAdrian Chadd inp->inp_flowtype, 15397847796aSAdrian Chadd &rss_bucket); 15407847796aSAdrian Chadd if (retval == 0) 15417847796aSAdrian Chadd optval = rss_bucket; 15427847796aSAdrian Chadd else 15437847796aSAdrian Chadd error = EINVAL; 15449c423972SAdrian Chadd break; 15459d3ddf43SAdrian Chadd case IP_RECVRSSBUCKETID: 15469d3ddf43SAdrian Chadd optval = OPTBIT2(INP_RECVRSSBUCKETID); 15479d3ddf43SAdrian Chadd break; 15489c423972SAdrian Chadd #endif 15490a100a6fSAdrian Chadd case IP_BINDMULTI: 15500a100a6fSAdrian Chadd optval = OPTBIT2(INP_BINDMULTI); 15510a100a6fSAdrian Chadd break; 1552868aabb4SRichard Scheffenegger case IP_VLAN_PCP: 1553868aabb4SRichard Scheffenegger if (OPTBIT2(INP_2PCP_SET)) { 1554868aabb4SRichard Scheffenegger optval = (inp->inp_flags2 & 1555868aabb4SRichard Scheffenegger INP_2PCP_MASK) >> INP_2PCP_SHIFT; 1556868aabb4SRichard Scheffenegger } else { 1557868aabb4SRichard Scheffenegger optval = -1; 1558868aabb4SRichard Scheffenegger } 1559868aabb4SRichard Scheffenegger break; 1560df8bae1dSRodney W. Grimes } 1561cfe8b629SGarrett Wollman error = sooptcopyout(sopt, &optval, sizeof optval); 1562df8bae1dSRodney W. Grimes break; 1563df8bae1dSRodney W. Grimes 156471498f30SBruce M Simpson /* 156571498f30SBruce M Simpson * Multicast socket options are processed by the in_mcast 156671498f30SBruce M Simpson * module. 156771498f30SBruce M Simpson */ 1568df8bae1dSRodney W. Grimes case IP_MULTICAST_IF: 1569f0068c4aSGarrett Wollman case IP_MULTICAST_VIF: 1570df8bae1dSRodney W. Grimes case IP_MULTICAST_TTL: 1571df8bae1dSRodney W. Grimes case IP_MULTICAST_LOOP: 157271498f30SBruce M Simpson case IP_MSFILTER: 157371498f30SBruce M Simpson error = inp_getmoptions(inp, sopt); 157433b3ac06SPeter Wemm break; 157533b3ac06SPeter Wemm 1576fcf59617SAndrey V. Elsukov #if defined(IPSEC) || defined(IPSEC_SUPPORT) 15776a800098SYoshinobu Inoue case IP_IPSEC_POLICY: 1578fcf59617SAndrey V. Elsukov if (IPSEC_ENABLED(ipv4)) { 1579fcf59617SAndrey V. Elsukov error = IPSEC_PCBCTL(ipv4, inp, sopt); 15806a800098SYoshinobu Inoue break; 15816a800098SYoshinobu Inoue } 1582fcf59617SAndrey V. Elsukov /* FALLTHROUGH */ 1583b2630c29SGeorge V. Neville-Neil #endif /* IPSEC */ 15846a800098SYoshinobu Inoue 1585df8bae1dSRodney W. Grimes default: 1586df8bae1dSRodney W. Grimes error = ENOPROTOOPT; 1587df8bae1dSRodney W. Grimes break; 1588df8bae1dSRodney W. Grimes } 1589df8bae1dSRodney W. Grimes break; 1590df8bae1dSRodney W. Grimes } 1591df8bae1dSRodney W. Grimes return (error); 1592df8bae1dSRodney W. Grimes } 1593df8bae1dSRodney W. Grimes 1594df8bae1dSRodney W. Grimes /* 1595df8bae1dSRodney W. Grimes * Routine called from ip_output() to loop back a copy of an IP multicast 1596df8bae1dSRodney W. Grimes * packet to the input queue of a specified interface. Note that this 1597df8bae1dSRodney W. Grimes * calls the output routine of the loopback "driver", but with an interface 1598f5fea3ddSPaul Traina * pointer that might NOT be a loopback interface -- evil, but easier than 1599f5fea3ddSPaul Traina * replicating that code here. 1600df8bae1dSRodney W. Grimes */ 1601df8bae1dSRodney W. Grimes static void 1602331dff07SAlexander V. Chernikov ip_mloopback(struct ifnet *ifp, const struct mbuf *m, int hlen) 1603df8bae1dSRodney W. Grimes { 1604331dff07SAlexander V. Chernikov struct ip *ip; 1605df8bae1dSRodney W. Grimes struct mbuf *copym; 1606df8bae1dSRodney W. Grimes 1607e4762f75SGeorge V. Neville-Neil /* 1608e4762f75SGeorge V. Neville-Neil * Make a deep copy of the packet because we're going to 1609e4762f75SGeorge V. Neville-Neil * modify the pack in order to generate checksums. 1610e4762f75SGeorge V. Neville-Neil */ 1611eb1b1807SGleb Smirnoff copym = m_dup(m, M_NOWAIT); 1612f0cace5dSRobert Watson if (copym != NULL && (!M_WRITABLE(copym) || copym->m_len < hlen)) 161386b1d6d2SBill Fenner copym = m_pullup(copym, hlen); 1614df8bae1dSRodney W. Grimes if (copym != NULL) { 1615390cdc6aSRuslan Ermilov /* If needed, compute the checksum and mark it as valid. */ 1616390cdc6aSRuslan Ermilov if (copym->m_pkthdr.csum_flags & CSUM_DELAY_DATA) { 1617390cdc6aSRuslan Ermilov in_delayed_cksum(copym); 1618390cdc6aSRuslan Ermilov copym->m_pkthdr.csum_flags &= ~CSUM_DELAY_DATA; 1619390cdc6aSRuslan Ermilov copym->m_pkthdr.csum_flags |= 1620390cdc6aSRuslan Ermilov CSUM_DATA_VALID | CSUM_PSEUDO_HDR; 1621390cdc6aSRuslan Ermilov copym->m_pkthdr.csum_data = 0xffff; 1622390cdc6aSRuslan Ermilov } 1623df8bae1dSRodney W. Grimes /* 1624df8bae1dSRodney W. Grimes * We don't bother to fragment if the IP length is greater 1625df8bae1dSRodney W. Grimes * than the interface's MTU. Can this possibly matter? 1626df8bae1dSRodney W. Grimes */ 16278f134647SGleb Smirnoff ip = mtod(copym, struct ip *); 1628df8bae1dSRodney W. Grimes ip->ip_sum = 0; 162986b1d6d2SBill Fenner ip->ip_sum = in_cksum(copym, hlen); 1630331dff07SAlexander V. Chernikov if_simloop(ifp, copym, AF_INET, 0); 1631df8bae1dSRodney W. Grimes } 1632df8bae1dSRodney W. Grimes } 1633