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> 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> 64868aabb4SRichard Scheffenegger #include <net/if_vlan_var.h> 653ef5e21dSQing Li #include <net/if_llatbl.h> 66868aabb4SRichard Scheffenegger #include <net/ethernet.h> 679b932e9eSAndre Oppermann #include <net/netisr.h> 68c21fd232SAndre Oppermann #include <net/pfil.h> 69df8bae1dSRodney W. Grimes #include <net/route.h> 70983066f0SAlexander V. Chernikov #include <net/route/nhop.h> 71b2bdc62aSAdrian Chadd #include <net/rss_config.h> 724b79449eSBjoern A. Zeeb #include <net/vnet.h> 73df8bae1dSRodney W. Grimes 74df8bae1dSRodney W. Grimes #include <netinet/in.h> 756ca363ebSGleb Smirnoff #include <netinet/in_fib.h> 7657f60867SMark Johnston #include <netinet/in_kdtrace.h> 77df8bae1dSRodney W. Grimes #include <netinet/in_systm.h> 78df8bae1dSRodney W. Grimes #include <netinet/ip.h> 79983066f0SAlexander V. Chernikov #include <netinet/in_fib.h> 80df8bae1dSRodney W. Grimes #include <netinet/in_pcb.h> 819c423972SAdrian Chadd #include <netinet/in_rss.h> 82df8bae1dSRodney W. Grimes #include <netinet/in_var.h> 83df8bae1dSRodney W. Grimes #include <netinet/ip_var.h> 84ef39adf0SAndre Oppermann #include <netinet/ip_options.h> 8565634ae7SWojciech Macek #include <netinet/ip_mroute.h> 861fdbfb90STom Jones 871fdbfb90STom Jones #include <netinet/udp.h> 881fdbfb90STom Jones #include <netinet/udp_var.h> 891fdbfb90STom Jones 9095033af9SMark Johnston #if defined(SCTP) || defined(SCTP_SUPPORT) 912f4afd21SRandall Stewart #include <netinet/sctp.h> 922f4afd21SRandall Stewart #include <netinet/sctp_crc32.h> 932f4afd21SRandall Stewart #endif 94df8bae1dSRodney W. Grimes 95fcf59617SAndrey V. Elsukov #include <netipsec/ipsec_support.h> 966a800098SYoshinobu Inoue 971dfcf0d2SAndre Oppermann #include <machine/in_cksum.h> 981dfcf0d2SAndre Oppermann 99aed55708SRobert Watson #include <security/mac/mac_framework.h> 100aed55708SRobert Watson 10153dcc544SMike Silbersack #ifdef MBUF_STRESS_TEST 10205b9d121SBjoern A. Zeeb static int mbuf_frag_size = 0; 1039d9edc56SMike Silbersack SYSCTL_INT(_net_inet_ip, OID_AUTO, mbuf_frag_size, CTLFLAG_RW, 1049d9edc56SMike Silbersack &mbuf_frag_size, 0, "Fragment outgoing mbufs to this size"); 1059d9edc56SMike Silbersack #endif 1069d9edc56SMike Silbersack 107331dff07SAlexander V. Chernikov static void ip_mloopback(struct ifnet *, const struct mbuf *, int); 108afed1b49SDarren Reed 1098b889dbbSBruce M Simpson extern int in_mcast_loop; 11093e0e116SJulian Elischer extern struct protosw inetsw[]; 11193e0e116SJulian Elischer 112d9f2a782SErmal Luçi static inline int 11305fc9d78SKristof Provost ip_output_pfil(struct mbuf **mp, struct ifnet *ifp, int flags, 11405fc9d78SKristof Provost struct inpcb *inp, struct sockaddr_in *dst, int *fibnum, int *error) 115d9f2a782SErmal Luçi { 116d9f2a782SErmal Luçi struct m_tag *fwd_tag = NULL; 1178f980c01SMark Johnston struct mbuf *m; 118d9f2a782SErmal Luçi struct in_addr odst; 119d9f2a782SErmal Luçi struct ip *ip; 12005fc9d78SKristof Provost int pflags = PFIL_OUT; 12105fc9d78SKristof Provost 12205fc9d78SKristof Provost if (flags & IP_FORWARDING) 12305fc9d78SKristof Provost pflags |= PFIL_FWD; 124d9f2a782SErmal Luçi 1258f980c01SMark Johnston m = *mp; 126d9f2a782SErmal Luçi ip = mtod(m, struct ip *); 127d9f2a782SErmal Luçi 128d9f2a782SErmal Luçi /* Run through list of hooks for output packets. */ 129d9f2a782SErmal Luçi odst.s_addr = ip->ip_dst.s_addr; 13005fc9d78SKristof Provost switch (pfil_run_hooks(V_inet_pfil_head, mp, ifp, pflags, inp)) { 131b252313fSGleb Smirnoff case PFIL_DROPPED: 1328d5c56daSGleb Smirnoff *error = EACCES; 133b252313fSGleb Smirnoff /* FALLTHROUGH */ 134b252313fSGleb Smirnoff case PFIL_CONSUMED: 135d9f2a782SErmal Luçi return 1; /* Finished */ 136b252313fSGleb Smirnoff case PFIL_PASS: 137b252313fSGleb Smirnoff *error = 0; 138b252313fSGleb Smirnoff } 139b252313fSGleb Smirnoff m = *mp; 140d9f2a782SErmal Luçi ip = mtod(m, struct ip *); 141d9f2a782SErmal Luçi 142d9f2a782SErmal Luçi /* See if destination IP address was changed by packet filter. */ 143d9f2a782SErmal Luçi if (odst.s_addr != ip->ip_dst.s_addr) { 144d9f2a782SErmal Luçi m->m_flags |= M_SKIP_FIREWALL; 145d9f2a782SErmal Luçi /* If destination is now ourself drop to ip_input(). */ 146d9f2a782SErmal Luçi if (in_localip(ip->ip_dst)) { 147d9f2a782SErmal Luçi m->m_flags |= M_FASTFWD_OURS; 148d9f2a782SErmal Luçi if (m->m_pkthdr.rcvif == NULL) 149d9f2a782SErmal Luçi m->m_pkthdr.rcvif = V_loif; 150d9f2a782SErmal Luçi if (m->m_pkthdr.csum_flags & CSUM_DELAY_DATA) { 151d9f2a782SErmal Luçi m->m_pkthdr.csum_flags |= 152d9f2a782SErmal Luçi CSUM_DATA_VALID | CSUM_PSEUDO_HDR; 153d9f2a782SErmal Luçi m->m_pkthdr.csum_data = 0xffff; 154d9f2a782SErmal Luçi } 155d9f2a782SErmal Luçi m->m_pkthdr.csum_flags |= 156d9f2a782SErmal Luçi CSUM_IP_CHECKED | CSUM_IP_VALID; 15795033af9SMark Johnston #if defined(SCTP) || defined(SCTP_SUPPORT) 158d9f2a782SErmal Luçi if (m->m_pkthdr.csum_flags & CSUM_SCTP) 159d9f2a782SErmal Luçi m->m_pkthdr.csum_flags |= CSUM_SCTP_VALID; 160d9f2a782SErmal Luçi #endif 161d9f2a782SErmal Luçi *error = netisr_queue(NETISR_IP, m); 162d9f2a782SErmal Luçi return 1; /* Finished */ 163d9f2a782SErmal Luçi } 164d9f2a782SErmal Luçi 165d9f2a782SErmal Luçi bzero(dst, sizeof(*dst)); 166d9f2a782SErmal Luçi dst->sin_family = AF_INET; 167d9f2a782SErmal Luçi dst->sin_len = sizeof(*dst); 168d9f2a782SErmal Luçi dst->sin_addr = ip->ip_dst; 169d9f2a782SErmal Luçi 170d9f2a782SErmal Luçi return -1; /* Reloop */ 171d9f2a782SErmal Luçi } 172d9f2a782SErmal Luçi /* See if fib was changed by packet filter. */ 173d9f2a782SErmal Luçi if ((*fibnum) != M_GETFIB(m)) { 174d9f2a782SErmal Luçi m->m_flags |= M_SKIP_FIREWALL; 175d9f2a782SErmal Luçi *fibnum = M_GETFIB(m); 176d9f2a782SErmal Luçi return -1; /* Reloop for FIB change */ 177d9f2a782SErmal Luçi } 178d9f2a782SErmal Luçi 179d9f2a782SErmal Luçi /* See if local, if yes, send it to netisr with IP_FASTFWD_OURS. */ 180d9f2a782SErmal Luçi if (m->m_flags & M_FASTFWD_OURS) { 181d9f2a782SErmal Luçi if (m->m_pkthdr.rcvif == NULL) 182d9f2a782SErmal Luçi m->m_pkthdr.rcvif = V_loif; 183d9f2a782SErmal Luçi if (m->m_pkthdr.csum_flags & CSUM_DELAY_DATA) { 184d9f2a782SErmal Luçi m->m_pkthdr.csum_flags |= 185d9f2a782SErmal Luçi CSUM_DATA_VALID | CSUM_PSEUDO_HDR; 186d9f2a782SErmal Luçi m->m_pkthdr.csum_data = 0xffff; 187d9f2a782SErmal Luçi } 18895033af9SMark Johnston #if defined(SCTP) || defined(SCTP_SUPPORT) 189d9f2a782SErmal Luçi if (m->m_pkthdr.csum_flags & CSUM_SCTP) 190d9f2a782SErmal Luçi m->m_pkthdr.csum_flags |= CSUM_SCTP_VALID; 191d9f2a782SErmal Luçi #endif 192d9f2a782SErmal Luçi m->m_pkthdr.csum_flags |= 193d9f2a782SErmal Luçi CSUM_IP_CHECKED | CSUM_IP_VALID; 194d9f2a782SErmal Luçi 195d9f2a782SErmal Luçi *error = netisr_queue(NETISR_IP, m); 196d9f2a782SErmal Luçi return 1; /* Finished */ 197d9f2a782SErmal Luçi } 198d9f2a782SErmal Luçi /* Or forward to some other address? */ 199d9f2a782SErmal Luçi if ((m->m_flags & M_IP_NEXTHOP) && 200d9f2a782SErmal Luçi ((fwd_tag = m_tag_find(m, PACKET_TAG_IPFORWARD, NULL)) != NULL)) { 201d9f2a782SErmal Luçi bcopy((fwd_tag+1), dst, sizeof(struct sockaddr_in)); 202d9f2a782SErmal Luçi m->m_flags |= M_SKIP_FIREWALL; 203d9f2a782SErmal Luçi m->m_flags &= ~M_IP_NEXTHOP; 204d9f2a782SErmal Luçi m_tag_delete(m, fwd_tag); 205d9f2a782SErmal Luçi 206d9f2a782SErmal Luçi return -1; /* Reloop for CHANGE of dst */ 207d9f2a782SErmal Luçi } 208d9f2a782SErmal Luçi 209d9f2a782SErmal Luçi return 0; 210d9f2a782SErmal Luçi } 211d9f2a782SErmal Luçi 212fb3bc596SJohn Baldwin static int 213fb3bc596SJohn Baldwin ip_output_send(struct inpcb *inp, struct ifnet *ifp, struct mbuf *m, 21462e1a437SZhenlei Huang const struct sockaddr *gw, struct route *ro, bool stamp_tag) 215fb3bc596SJohn Baldwin { 216b2e60773SJohn Baldwin #ifdef KERN_TLS 217b2e60773SJohn Baldwin struct ktls_session *tls = NULL; 218b2e60773SJohn Baldwin #endif 219fb3bc596SJohn Baldwin struct m_snd_tag *mst; 220fb3bc596SJohn Baldwin int error; 221fb3bc596SJohn Baldwin 222fb3bc596SJohn Baldwin MPASS((m->m_pkthdr.csum_flags & CSUM_SND_TAG) == 0); 223fb3bc596SJohn Baldwin mst = NULL; 224fb3bc596SJohn Baldwin 225b2e60773SJohn Baldwin #ifdef KERN_TLS 226b2e60773SJohn Baldwin /* 227b2e60773SJohn Baldwin * If this is an unencrypted TLS record, save a reference to 228b2e60773SJohn Baldwin * the record. This local reference is used to call 229b2e60773SJohn Baldwin * ktls_output_eagain after the mbuf has been freed (thus 230b2e60773SJohn Baldwin * dropping the mbuf's reference) in if_output. 231b2e60773SJohn Baldwin */ 232b2e60773SJohn Baldwin if (m->m_next != NULL && mbuf_has_tls_session(m->m_next)) { 2337b6c99d0SGleb Smirnoff tls = ktls_hold(m->m_next->m_epg_tls); 234b2e60773SJohn Baldwin mst = tls->snd_tag; 235b2e60773SJohn Baldwin 236b2e60773SJohn Baldwin /* 237b2e60773SJohn Baldwin * If a TLS session doesn't have a valid tag, it must 238b2e60773SJohn Baldwin * have had an earlier ifp mismatch, so drop this 239b2e60773SJohn Baldwin * packet. 240b2e60773SJohn Baldwin */ 241b2e60773SJohn Baldwin if (mst == NULL) { 242b2e60773SJohn Baldwin error = EAGAIN; 243b2e60773SJohn Baldwin goto done; 244b2e60773SJohn Baldwin } 2456043ac20SAndrew Gallatin /* 2466043ac20SAndrew Gallatin * Always stamp tags that include NIC ktls. 2476043ac20SAndrew Gallatin */ 2486043ac20SAndrew Gallatin stamp_tag = true; 249b2e60773SJohn Baldwin } 250b2e60773SJohn Baldwin #endif 251fb3bc596SJohn Baldwin #ifdef RATELIMIT 252b2e60773SJohn Baldwin if (inp != NULL && mst == NULL) { 253fb3bc596SJohn Baldwin if ((inp->inp_flags2 & INP_RATE_LIMIT_CHANGED) != 0 || 254fb3bc596SJohn Baldwin (inp->inp_snd_tag != NULL && 255fb3bc596SJohn Baldwin inp->inp_snd_tag->ifp != ifp)) 256fb3bc596SJohn Baldwin in_pcboutput_txrtlmt(inp, ifp, m); 257fb3bc596SJohn Baldwin 258fb3bc596SJohn Baldwin if (inp->inp_snd_tag != NULL) 259fb3bc596SJohn Baldwin mst = inp->inp_snd_tag; 260fb3bc596SJohn Baldwin } 261fb3bc596SJohn Baldwin #endif 26235c7bb34SRandall Stewart if (stamp_tag && mst != NULL) { 263fb3bc596SJohn Baldwin KASSERT(m->m_pkthdr.rcvif == NULL, 264fb3bc596SJohn Baldwin ("trying to add a send tag to a forwarded packet")); 265fb3bc596SJohn Baldwin if (mst->ifp != ifp) { 266fb3bc596SJohn Baldwin error = EAGAIN; 267fb3bc596SJohn Baldwin goto done; 268fb3bc596SJohn Baldwin } 269fb3bc596SJohn Baldwin 270fb3bc596SJohn Baldwin /* stamp send tag on mbuf */ 271fb3bc596SJohn Baldwin m->m_pkthdr.snd_tag = m_snd_tag_ref(mst); 272fb3bc596SJohn Baldwin m->m_pkthdr.csum_flags |= CSUM_SND_TAG; 273fb3bc596SJohn Baldwin } 274fb3bc596SJohn Baldwin 27562e1a437SZhenlei Huang error = (*ifp->if_output)(ifp, m, gw, ro); 276fb3bc596SJohn Baldwin 277fb3bc596SJohn Baldwin done: 278fb3bc596SJohn Baldwin /* Check for route change invalidating send tags. */ 279b2e60773SJohn Baldwin #ifdef KERN_TLS 280b2e60773SJohn Baldwin if (tls != NULL) { 281b2e60773SJohn Baldwin if (error == EAGAIN) 282b2e60773SJohn Baldwin error = ktls_output_eagain(inp, tls); 283b2e60773SJohn Baldwin ktls_free(tls); 284b2e60773SJohn Baldwin } 285b2e60773SJohn Baldwin #endif 286fb3bc596SJohn Baldwin #ifdef RATELIMIT 287fb3bc596SJohn Baldwin if (error == EAGAIN) 288fb3bc596SJohn Baldwin in_pcboutput_eagain(inp); 289fb3bc596SJohn Baldwin #endif 290fb3bc596SJohn Baldwin return (error); 291fb3bc596SJohn Baldwin } 292fb3bc596SJohn Baldwin 293983066f0SAlexander V. Chernikov /* rte<>ro_flags translation */ 294983066f0SAlexander V. Chernikov static inline void 2959748eb74SAlexander V. Chernikov rt_update_ro_flags(struct route *ro, const struct nhop_object *nh) 296983066f0SAlexander V. Chernikov { 2979748eb74SAlexander V. Chernikov int nh_flags = nh->nh_flags; 298983066f0SAlexander V. Chernikov 299983066f0SAlexander V. Chernikov ro->ro_flags &= ~ (RT_REJECT|RT_BLACKHOLE|RT_HAS_GW); 300983066f0SAlexander V. Chernikov 301983066f0SAlexander V. Chernikov ro->ro_flags |= (nh_flags & NHF_REJECT) ? RT_REJECT : 0; 302983066f0SAlexander V. Chernikov ro->ro_flags |= (nh_flags & NHF_BLACKHOLE) ? RT_BLACKHOLE : 0; 303983066f0SAlexander V. Chernikov ro->ro_flags |= (nh_flags & NHF_GATEWAY) ? RT_HAS_GW : 0; 304983066f0SAlexander V. Chernikov } 305983066f0SAlexander V. Chernikov 306df8bae1dSRodney W. Grimes /* 307df8bae1dSRodney W. Grimes * IP output. The packet in mbuf chain m contains a skeletal IP 308df8bae1dSRodney W. Grimes * header (with len, off, ttl, proto, tos, src, dst). 309df8bae1dSRodney W. Grimes * The mbuf chain containing the packet will be freed. 310df8bae1dSRodney W. Grimes * The mbuf opt, if present, will not be freed. 311bf984051SGleb Smirnoff * If route ro is present and has ro_rt initialized, route lookup would be 312bf984051SGleb Smirnoff * skipped and ro->ro_rt would be used. If ro is present but ro->ro_rt is NULL, 313bf984051SGleb Smirnoff * then result of route lookup is stored in ro->ro_rt. 314bf984051SGleb Smirnoff * 3153de758d3SRobert Watson * In the IP forwarding case, the packet will arrive with options already 3163de758d3SRobert Watson * inserted, so must have a NULL opt pointer. 317df8bae1dSRodney W. Grimes */ 318df8bae1dSRodney W. Grimes int 319f2565d68SRobert Watson ip_output(struct mbuf *m, struct mbuf *opt, struct route *ro, int flags, 320f2565d68SRobert Watson struct ip_moptions *imo, struct inpcb *inp) 321df8bae1dSRodney W. Grimes { 32265634ae7SWojciech Macek MROUTER_RLOCK_TRACKER; 3231e78ac21SJeffrey Hsu struct ip *ip; 324fc74d005SBjoern A. Zeeb struct ifnet *ifp = NULL; /* keep compiler happy */ 325ac9d7e26SMax Laier struct mbuf *m0; 32623bf9953SPoul-Henning Kamp int hlen = sizeof (struct ip); 327374ce248SMitchell Horne int mtu = 0; 328ca8b83b0SLuigi Rizzo int error = 0; 329868aabb4SRichard Scheffenegger int vlan_pcp = -1; 33062e1a437SZhenlei Huang struct sockaddr_in *dst; 33162e1a437SZhenlei Huang const struct sockaddr *gw; 332374ce248SMitchell Horne struct in_ifaddr *ia = NULL; 3336ca363ebSGleb Smirnoff struct in_addr src; 33421d172a3SGleb Smirnoff int isbroadcast; 335078468edSGleb Smirnoff uint16_t ip_len, ip_off; 33662e1a437SZhenlei Huang struct route iproute; 3379c57a5b6SHiroki Sato uint32_t fibnum; 338fcf59617SAndrey V. Elsukov #if defined(IPSEC) || defined(IPSEC_SUPPORT) 3391a499816SEdward Tomasz Napierala int no_route_but_check_spd = 0; 3401a499816SEdward Tomasz Napierala #endif 34177a01441SJohn Baldwin 342ac9d7e26SMax Laier M_ASSERTPKTHDR(m); 343b9555453SGleb Smirnoff NET_EPOCH_ASSERT(); 34436e8826fSMax Laier 345bc97ba51SJulian Elischer if (inp != NULL) { 346baa45840SRobert Watson INP_LOCK_ASSERT(inp); 34762e1ba83SRobert Watson M_SETFIB(m, inp->inp_inc.inc_fibnum); 348c2529042SHans Petter Selasky if ((flags & IP_NODEFAULTFLOWID) == 0) { 34980cb9f21SKip Macy m->m_pkthdr.flowid = inp->inp_flowid; 3509c423972SAdrian Chadd M_HASHTYPE_SET(m, inp->inp_flowtype); 35180cb9f21SKip Macy } 352868aabb4SRichard Scheffenegger if ((inp->inp_flags2 & INP_2PCP_SET) != 0) 353868aabb4SRichard Scheffenegger vlan_pcp = (inp->inp_flags2 & INP_2PCP_MASK) >> 354868aabb4SRichard Scheffenegger INP_2PCP_SHIFT; 35550575ce1SAndrew Gallatin #ifdef NUMA 35650575ce1SAndrew Gallatin m->m_pkthdr.numa_domain = inp->inp_numa_domain; 35750575ce1SAndrew Gallatin #endif 358bc97ba51SJulian Elischer } 35962e1ba83SRobert Watson 360df8bae1dSRodney W. Grimes if (opt) { 361ca8b83b0SLuigi Rizzo int len = 0; 362df8bae1dSRodney W. Grimes m = ip_insertoptions(m, opt, &len); 363cb7641e8SMaxim Konovalov if (len != 0) 364ca8b83b0SLuigi Rizzo hlen = len; /* ip->ip_hl is updated above */ 365df8bae1dSRodney W. Grimes } 366df8bae1dSRodney W. Grimes ip = mtod(m, struct ip *); 3678f134647SGleb Smirnoff ip_len = ntohs(ip->ip_len); 3688f134647SGleb Smirnoff ip_off = ntohs(ip->ip_off); 3693efc3014SJulian Elischer 370df8bae1dSRodney W. Grimes if ((flags & (IP_FORWARDING|IP_RAWOUTPUT)) == 0) { 37153be11f6SPoul-Henning Kamp ip->ip_v = IPVERSION; 37253be11f6SPoul-Henning Kamp ip->ip_hl = hlen >> 2; 3736d947416SGleb Smirnoff ip_fillid(ip); 374df8bae1dSRodney W. Grimes } else { 375ca8b83b0SLuigi Rizzo /* Header already set, fetch hlen from there */ 37653be11f6SPoul-Henning Kamp hlen = ip->ip_hl << 2; 377df8bae1dSRodney W. Grimes } 3783924dfa7SMichael Tuexen if ((flags & IP_FORWARDING) == 0) 3793924dfa7SMichael Tuexen IPSTAT_INC(ips_localout); 3809c9137eaSGarrett Wollman 381054692a4SAlexander V. Chernikov /* 382054692a4SAlexander V. Chernikov * dst/gw handling: 383054692a4SAlexander V. Chernikov * 3843c065f2fSGleb Smirnoff * gw is readonly but can point either to dst OR rt_gateway, 3853c065f2fSGleb Smirnoff * therefore we need restore gw if we're redoing lookup. 386054692a4SAlexander V. Chernikov */ 3879c57a5b6SHiroki Sato fibnum = (inp != NULL) ? inp->inp_inc.inc_fibnum : M_GETFIB(m); 38862e1a437SZhenlei Huang if (ro == NULL) { 38962e1a437SZhenlei Huang ro = &iproute; 39062e1a437SZhenlei Huang bzero(ro, sizeof (*ro)); 39162e1a437SZhenlei Huang } 3926ca363ebSGleb Smirnoff dst = (struct sockaddr_in *)&ro->ro_dst; 39362e1a437SZhenlei Huang if (ro->ro_nh == NULL) { 394df8bae1dSRodney W. Grimes dst->sin_family = AF_INET; 395df8bae1dSRodney W. Grimes dst->sin_len = sizeof(*dst); 3969b932e9eSAndre Oppermann dst->sin_addr = ip->ip_dst; 397df8bae1dSRodney W. Grimes } 39862e1a437SZhenlei Huang gw = (const struct sockaddr *)dst; 399d9f2a782SErmal Luçi again: 40084cc0778SGeorge V. Neville-Neil /* 40184cc0778SGeorge V. Neville-Neil * Validate route against routing table additions; 40284cc0778SGeorge V. Neville-Neil * a better/more specific route might have been added. 40384cc0778SGeorge V. Neville-Neil */ 40462e1a437SZhenlei Huang if (inp != NULL && ro->ro_nh != NULL) 405983066f0SAlexander V. Chernikov NH_VALIDATE(ro, &inp->inp_rt_cookie, fibnum); 40684cc0778SGeorge V. Neville-Neil /* 40784cc0778SGeorge V. Neville-Neil * If there is a cached route, 40884cc0778SGeorge V. Neville-Neil * check that it is to the same destination 40984cc0778SGeorge V. Neville-Neil * and is still up. If not, free it and try again. 41084cc0778SGeorge V. Neville-Neil * The address family should also be checked in case of sharing the 41184cc0778SGeorge V. Neville-Neil * cache with IPv6. 41284cc0778SGeorge V. Neville-Neil * Also check whether routing cache needs invalidation. 41384cc0778SGeorge V. Neville-Neil */ 41462e1a437SZhenlei Huang if (ro->ro_nh != NULL && 415174fb9dbSAlexander V. Chernikov ((!NH_IS_VALID(ro->ro_nh)) || dst->sin_family != AF_INET || 4166ca363ebSGleb Smirnoff dst->sin_addr.s_addr != ip->ip_dst.s_addr)) 417fc21c53fSRyan Stone RO_INVALIDATE_CACHE(ro); 418d9f2a782SErmal Luçi ia = NULL; 419df8bae1dSRodney W. Grimes /* 420a3fd02d8SBruce M Simpson * If routing to interface only, short circuit routing lookup. 421a3fd02d8SBruce M Simpson * The use of an all-ones broadcast address implies this; an 422a3fd02d8SBruce M Simpson * interface is specified by the broadcast address of an interface, 423a3fd02d8SBruce M Simpson * or the destination address of a ptp interface. 424df8bae1dSRodney W. Grimes */ 425a3fd02d8SBruce M Simpson if (flags & IP_SENDONES) { 4264f8585e0SAlan Somers if ((ia = ifatoia(ifa_ifwithbroadaddr(sintosa(dst), 42758a39d8cSAlan Somers M_GETFIB(m)))) == NULL && 4284f8585e0SAlan Somers (ia = ifatoia(ifa_ifwithdstaddr(sintosa(dst), 42958a39d8cSAlan Somers M_GETFIB(m)))) == NULL) { 43086425c62SRobert Watson IPSTAT_INC(ips_noroute); 431a3fd02d8SBruce M Simpson error = ENETUNREACH; 432a3fd02d8SBruce M Simpson goto bad; 433a3fd02d8SBruce M Simpson } 434a3fd02d8SBruce M Simpson ip->ip_dst.s_addr = INADDR_BROADCAST; 435a3fd02d8SBruce M Simpson dst->sin_addr = ip->ip_dst; 436a3fd02d8SBruce M Simpson ifp = ia->ia_ifp; 4376ca363ebSGleb Smirnoff mtu = ifp->if_mtu; 438a3fd02d8SBruce M Simpson ip->ip_ttl = 1; 439a3fd02d8SBruce M Simpson isbroadcast = 1; 4406ca363ebSGleb Smirnoff src = IA_SIN(ia)->sin_addr; 441a3fd02d8SBruce M Simpson } else if (flags & IP_ROUTETOIF) { 4424f8585e0SAlan Somers if ((ia = ifatoia(ifa_ifwithdstaddr(sintosa(dst), 44358a39d8cSAlan Somers M_GETFIB(m)))) == NULL && 4444f8585e0SAlan Somers (ia = ifatoia(ifa_ifwithnet(sintosa(dst), 0, 44558a39d8cSAlan Somers M_GETFIB(m)))) == NULL) { 44686425c62SRobert Watson IPSTAT_INC(ips_noroute); 447df8bae1dSRodney W. Grimes error = ENETUNREACH; 448df8bae1dSRodney W. Grimes goto bad; 449df8bae1dSRodney W. Grimes } 450df8bae1dSRodney W. Grimes ifp = ia->ia_ifp; 4516ca363ebSGleb Smirnoff mtu = ifp->if_mtu; 452df8bae1dSRodney W. Grimes ip->ip_ttl = 1; 4536c1bd558SRyan Stone isbroadcast = ifp->if_flags & IFF_BROADCAST ? 4546c1bd558SRyan Stone in_ifaddr_broadcast(dst->sin_addr, ia) : 0; 4556ca363ebSGleb Smirnoff src = IA_SIN(ia)->sin_addr; 4563afefa39SDaniel C. Sobral } else if (IN_MULTICAST(ntohl(ip->ip_dst.s_addr)) && 45738c1bc35SRuslan Ermilov imo != NULL && imo->imo_multicast_ifp != NULL) { 4583afefa39SDaniel C. Sobral /* 45938c1bc35SRuslan Ermilov * Bypass the normal routing lookup for multicast 46038c1bc35SRuslan Ermilov * packets if the interface is specified. 4613afefa39SDaniel C. Sobral */ 46238c1bc35SRuslan Ermilov ifp = imo->imo_multicast_ifp; 4636ca363ebSGleb Smirnoff mtu = ifp->if_mtu; 4642144431cSGleb Smirnoff IFP_TO_IA(ifp, ia); 46538c1bc35SRuslan Ermilov isbroadcast = 0; /* fool gcc */ 46654bb7ac0SGleb Smirnoff /* Interface may have no addresses. */ 46754bb7ac0SGleb Smirnoff if (ia != NULL) 4686ca363ebSGleb Smirnoff src = IA_SIN(ia)->sin_addr; 46954bb7ac0SGleb Smirnoff else 47054bb7ac0SGleb Smirnoff src.s_addr = INADDR_ANY; 47162e1a437SZhenlei Huang } else if (ro != &iproute) { 472983066f0SAlexander V. Chernikov if (ro->ro_nh == NULL) { 4732c17fe93SGarrett Wollman /* 4746ca363ebSGleb Smirnoff * We want to do any cloning requested by the link 4756ca363ebSGleb Smirnoff * layer, as this is probably required in all cases 4766ca363ebSGleb Smirnoff * for correct operation (as it is for ARP). 4772c17fe93SGarrett Wollman */ 4784043ee3cSAlexander V. Chernikov uint32_t flowid; 4794043ee3cSAlexander V. Chernikov flowid = m->m_pkthdr.flowid; 4804043ee3cSAlexander V. Chernikov ro->ro_nh = fib4_lookup(fibnum, dst->sin_addr, 0, 4814043ee3cSAlexander V. Chernikov NHR_REF, flowid); 4824043ee3cSAlexander V. Chernikov 483174fb9dbSAlexander V. Chernikov if (ro->ro_nh == NULL || (!NH_IS_VALID(ro->ro_nh))) { 484fcf59617SAndrey V. Elsukov #if defined(IPSEC) || defined(IPSEC_SUPPORT) 4851a499816SEdward Tomasz Napierala /* 4861a499816SEdward Tomasz Napierala * There is no route for this packet, but it is 4871a499816SEdward Tomasz Napierala * possible that a matching SPD entry exists. 4881a499816SEdward Tomasz Napierala */ 4891a499816SEdward Tomasz Napierala no_route_but_check_spd = 1; 4901a499816SEdward Tomasz Napierala goto sendit; 4911a499816SEdward Tomasz Napierala #endif 49286425c62SRobert Watson IPSTAT_INC(ips_noroute); 493df8bae1dSRodney W. Grimes error = EHOSTUNREACH; 494df8bae1dSRodney W. Grimes goto bad; 495df8bae1dSRodney W. Grimes } 4966ca363ebSGleb Smirnoff } 4979748eb74SAlexander V. Chernikov struct nhop_object *nh = ro->ro_nh; 4989748eb74SAlexander V. Chernikov 4999748eb74SAlexander V. Chernikov ia = ifatoia(nh->nh_ifa); 5009748eb74SAlexander V. Chernikov ifp = nh->nh_ifp; 5019748eb74SAlexander V. Chernikov counter_u64_add(nh->nh_pksent, 1); 5029748eb74SAlexander V. Chernikov rt_update_ro_flags(ro, nh); 5039748eb74SAlexander V. Chernikov if (nh->nh_flags & NHF_GATEWAY) 50462e1a437SZhenlei Huang gw = &nh->gw_sa; 5059748eb74SAlexander V. Chernikov if (nh->nh_flags & NHF_HOST) 5069748eb74SAlexander V. Chernikov isbroadcast = (nh->nh_flags & NHF_BROADCAST); 50762e1a437SZhenlei Huang else if ((ifp->if_flags & IFF_BROADCAST) && (gw->sa_family == AF_INET)) 50862e1a437SZhenlei Huang isbroadcast = in_ifaddr_broadcast(((const struct sockaddr_in *)gw)->sin_addr, ia); 5096c1bd558SRyan Stone else 5106c1bd558SRyan Stone isbroadcast = 0; 5119748eb74SAlexander V. Chernikov mtu = nh->nh_mtu; 5126ca363ebSGleb Smirnoff src = IA_SIN(ia)->sin_addr; 5136ca363ebSGleb Smirnoff } else { 5143553b300SAlexander V. Chernikov struct nhop_object *nh; 5156ca363ebSGleb Smirnoff 5162259a030SAlexander V. Chernikov nh = fib4_lookup(M_GETFIB(m), ip->ip_dst, 0, NHR_NONE, 5172259a030SAlexander V. Chernikov m->m_pkthdr.flowid); 5183553b300SAlexander V. Chernikov if (nh == NULL) { 5196ca363ebSGleb Smirnoff #if defined(IPSEC) || defined(IPSEC_SUPPORT) 5206ca363ebSGleb Smirnoff /* 5216ca363ebSGleb Smirnoff * There is no route for this packet, but it is 5226ca363ebSGleb Smirnoff * possible that a matching SPD entry exists. 5236ca363ebSGleb Smirnoff */ 5246ca363ebSGleb Smirnoff no_route_but_check_spd = 1; 5256ca363ebSGleb Smirnoff goto sendit; 5266ca363ebSGleb Smirnoff #endif 5276ca363ebSGleb Smirnoff IPSTAT_INC(ips_noroute); 5286ca363ebSGleb Smirnoff error = EHOSTUNREACH; 5296ca363ebSGleb Smirnoff goto bad; 5306ca363ebSGleb Smirnoff } 5313553b300SAlexander V. Chernikov ifp = nh->nh_ifp; 5323553b300SAlexander V. Chernikov mtu = nh->nh_mtu; 53362e1a437SZhenlei Huang rt_update_ro_flags(ro, nh); 5343553b300SAlexander V. Chernikov if (nh->nh_flags & NHF_GATEWAY) 53562e1a437SZhenlei Huang gw = &nh->gw_sa; 5363553b300SAlexander V. Chernikov ia = ifatoia(nh->nh_ifa); 5373553b300SAlexander V. Chernikov src = IA_SIN(ia)->sin_addr; 5383553b300SAlexander V. Chernikov isbroadcast = (((nh->nh_flags & (NHF_HOST | NHF_BROADCAST)) == 5396ca363ebSGleb Smirnoff (NHF_HOST | NHF_BROADCAST)) || 5406ca363ebSGleb Smirnoff ((ifp->if_flags & IFF_BROADCAST) && 54162e1a437SZhenlei Huang (gw->sa_family == AF_INET) && 54262e1a437SZhenlei Huang in_ifaddr_broadcast(((const struct sockaddr_in *)gw)->sin_addr, ia))); 5436ca363ebSGleb Smirnoff } 5446ca363ebSGleb Smirnoff 545c744cde4SBjoern A. Zeeb /* Catch a possible divide by zero later. */ 546983066f0SAlexander V. Chernikov KASSERT(mtu > 0, ("%s: mtu %d <= 0, ro=%p (nh_flags=0x%08x) ifp=%p", 5476ca363ebSGleb Smirnoff __func__, mtu, ro, 548983066f0SAlexander V. Chernikov (ro != NULL && ro->ro_nh != NULL) ? ro->ro_nh->nh_flags : 0, ifp)); 549d9f2a782SErmal Luçi 5509b932e9eSAndre Oppermann if (IN_MULTICAST(ntohl(ip->ip_dst.s_addr))) { 551df8bae1dSRodney W. Grimes m->m_flags |= M_MCAST; 552df8bae1dSRodney W. Grimes /* 553183e1c86SGleb Smirnoff * IP destination address is multicast. Make sure "gw" 554183e1c86SGleb Smirnoff * still points to the address in "ro". (It may have been 555183e1c86SGleb Smirnoff * changed to point to a gateway address, above.) 556183e1c86SGleb Smirnoff */ 55762e1a437SZhenlei Huang gw = (const struct sockaddr *)dst; 558183e1c86SGleb Smirnoff /* 559df8bae1dSRodney W. Grimes * See if the caller provided any multicast options 560df8bae1dSRodney W. Grimes */ 561df8bae1dSRodney W. Grimes if (imo != NULL) { 562df8bae1dSRodney W. Grimes ip->ip_ttl = imo->imo_multicast_ttl; 5631c5de19aSGarrett Wollman if (imo->imo_multicast_vif != -1) 5641c5de19aSGarrett Wollman ip->ip_src.s_addr = 565bbb4330bSLuigi Rizzo ip_mcast_src ? 566bbb4330bSLuigi Rizzo ip_mcast_src(imo->imo_multicast_vif) : 567bbb4330bSLuigi Rizzo INADDR_ANY; 568df8bae1dSRodney W. Grimes } else 569df8bae1dSRodney W. Grimes ip->ip_ttl = IP_DEFAULT_MULTICAST_TTL; 570df8bae1dSRodney W. Grimes /* 571df8bae1dSRodney W. Grimes * Confirm that the outgoing interface supports multicast. 572df8bae1dSRodney W. Grimes */ 5731c5de19aSGarrett Wollman if ((imo == NULL) || (imo->imo_multicast_vif == -1)) { 574df8bae1dSRodney W. Grimes if ((ifp->if_flags & IFF_MULTICAST) == 0) { 57586425c62SRobert Watson IPSTAT_INC(ips_noroute); 576df8bae1dSRodney W. Grimes error = ENETUNREACH; 577df8bae1dSRodney W. Grimes goto bad; 578df8bae1dSRodney W. Grimes } 5791c5de19aSGarrett Wollman } 580df8bae1dSRodney W. Grimes /* 581df8bae1dSRodney W. Grimes * If source address not specified yet, use address 582df8bae1dSRodney W. Grimes * of outgoing interface. 583df8bae1dSRodney W. Grimes */ 5846ca363ebSGleb Smirnoff if (ip->ip_src.s_addr == INADDR_ANY) 5856ca363ebSGleb Smirnoff ip->ip_src = src; 586df8bae1dSRodney W. Grimes 5878b889dbbSBruce M Simpson if ((imo == NULL && in_mcast_loop) || 5888b889dbbSBruce M Simpson (imo && imo->imo_multicast_loop)) { 589df8bae1dSRodney W. Grimes /* 5908b889dbbSBruce M Simpson * Loop back multicast datagram if not expressly 5918b889dbbSBruce M Simpson * forbidden to do so, even if we are not a member 5928b889dbbSBruce M Simpson * of the group; ip_input() will filter it later, 5938b889dbbSBruce M Simpson * thus deferring a hash lookup and mutex acquisition 5948b889dbbSBruce M Simpson * at the expense of a cheap copy using m_copym(). 595df8bae1dSRodney W. Grimes */ 596331dff07SAlexander V. Chernikov ip_mloopback(ifp, m, hlen); 5978b889dbbSBruce M Simpson } else { 598df8bae1dSRodney W. Grimes /* 599df8bae1dSRodney W. Grimes * If we are acting as a multicast router, perform 600df8bae1dSRodney W. Grimes * multicast forwarding as if the packet had just 601df8bae1dSRodney W. Grimes * arrived on the interface to which we are about 602df8bae1dSRodney W. Grimes * to send. The multicast forwarding function 603df8bae1dSRodney W. Grimes * recursively calls this function, using the 604df8bae1dSRodney W. Grimes * IP_FORWARDING flag to prevent infinite recursion. 605df8bae1dSRodney W. Grimes * 606df8bae1dSRodney W. Grimes * Multicasts that are looped back by ip_mloopback(), 607df8bae1dSRodney W. Grimes * above, will be forwarded by the ip_input() routine, 608df8bae1dSRodney W. Grimes * if necessary. 609df8bae1dSRodney W. Grimes */ 61065634ae7SWojciech Macek MROUTER_RLOCK(); 611603724d3SBjoern A. Zeeb if (V_ip_mrouter && (flags & IP_FORWARDING) == 0) { 612f0068c4aSGarrett Wollman /* 613bbb4330bSLuigi Rizzo * If rsvp daemon is not running, do not 614f0068c4aSGarrett Wollman * set ip_moptions. This ensures that the packet 615f0068c4aSGarrett Wollman * is multicast and not just sent down one link 616f0068c4aSGarrett Wollman * as prescribed by rsvpd. 617f0068c4aSGarrett Wollman */ 618603724d3SBjoern A. Zeeb if (!V_rsvp_on) 619f0068c4aSGarrett Wollman imo = NULL; 620bbb4330bSLuigi Rizzo if (ip_mforward && 621bbb4330bSLuigi Rizzo ip_mforward(ip, ifp, m, imo) != 0) { 62265634ae7SWojciech Macek MROUTER_RUNLOCK(); 623df8bae1dSRodney W. Grimes m_freem(m); 624df8bae1dSRodney W. Grimes goto done; 625df8bae1dSRodney W. Grimes } 626df8bae1dSRodney W. Grimes } 62765634ae7SWojciech Macek MROUTER_RUNLOCK(); 628df8bae1dSRodney W. Grimes } 6295e9ae478SGarrett Wollman 630df8bae1dSRodney W. Grimes /* 631df8bae1dSRodney W. Grimes * Multicasts with a time-to-live of zero may be looped- 632df8bae1dSRodney W. Grimes * back, above, but must not be transmitted on a network. 633df8bae1dSRodney W. Grimes * Also, multicasts addressed to the loopback interface 634df8bae1dSRodney W. Grimes * are not sent -- the above call to ip_mloopback() will 6358b889dbbSBruce M Simpson * loop back a copy. ip_input() will drop the copy if 6368b889dbbSBruce M Simpson * this host does not belong to the destination group on 6378b889dbbSBruce M Simpson * the loopback interface. 638df8bae1dSRodney W. Grimes */ 639f5fea3ddSPaul Traina if (ip->ip_ttl == 0 || ifp->if_flags & IFF_LOOPBACK) { 640df8bae1dSRodney W. Grimes m_freem(m); 641df8bae1dSRodney W. Grimes goto done; 642df8bae1dSRodney W. Grimes } 643df8bae1dSRodney W. Grimes 644df8bae1dSRodney W. Grimes goto sendit; 645df8bae1dSRodney W. Grimes } 6466a7c943cSAndre Oppermann 647df8bae1dSRodney W. Grimes /* 6482b25acc1SLuigi Rizzo * If the source address is not specified yet, use the address 649cb459254SJohn-Mark Gurney * of the outoing interface. 650df8bae1dSRodney W. Grimes */ 6516ca363ebSGleb Smirnoff if (ip->ip_src.s_addr == INADDR_ANY) 6526ca363ebSGleb Smirnoff ip->ip_src = src; 6536a7c943cSAndre Oppermann 654ca7a789aSMax Laier /* 655df8bae1dSRodney W. Grimes * Look for broadcast address and 6568c3f5566SRuslan Ermilov * verify user is allowed to send 657df8bae1dSRodney W. Grimes * such a packet. 658df8bae1dSRodney W. Grimes */ 6599f9b3dc4SGarrett Wollman if (isbroadcast) { 660df8bae1dSRodney W. Grimes if ((ifp->if_flags & IFF_BROADCAST) == 0) { 661df8bae1dSRodney W. Grimes error = EADDRNOTAVAIL; 662df8bae1dSRodney W. Grimes goto bad; 663df8bae1dSRodney W. Grimes } 664df8bae1dSRodney W. Grimes if ((flags & IP_ALLOWBROADCAST) == 0) { 665df8bae1dSRodney W. Grimes error = EACCES; 666df8bae1dSRodney W. Grimes goto bad; 667df8bae1dSRodney W. Grimes } 668df8bae1dSRodney W. Grimes /* don't allow broadcast messages to be fragmented */ 6698f134647SGleb Smirnoff if (ip_len > mtu) { 670df8bae1dSRodney W. Grimes error = EMSGSIZE; 671df8bae1dSRodney W. Grimes goto bad; 672df8bae1dSRodney W. Grimes } 673df8bae1dSRodney W. Grimes m->m_flags |= M_BCAST; 6749f9b3dc4SGarrett Wollman } else { 675df8bae1dSRodney W. Grimes m->m_flags &= ~M_BCAST; 6769f9b3dc4SGarrett Wollman } 677df8bae1dSRodney W. Grimes 678f1743588SDarren Reed sendit: 679fcf59617SAndrey V. Elsukov #if defined(IPSEC) || defined(IPSEC_SUPPORT) 680fcf59617SAndrey V. Elsukov if (IPSEC_ENABLED(ipv4)) { 681fcf59617SAndrey V. Elsukov if ((error = IPSEC_OUTPUT(ipv4, m, inp)) != 0) { 682fcf59617SAndrey V. Elsukov if (error == EINPROGRESS) 683fcf59617SAndrey V. Elsukov error = 0; 6841dfcf0d2SAndre Oppermann goto done; 685fcf59617SAndrey V. Elsukov } 68633841545SHajimu UMEMOTO } 6871a499816SEdward Tomasz Napierala /* 6881a499816SEdward Tomasz Napierala * Check if there was a route for this packet; return error if not. 6891a499816SEdward Tomasz Napierala */ 6901a499816SEdward Tomasz Napierala if (no_route_but_check_spd) { 6911a499816SEdward Tomasz Napierala IPSTAT_INC(ips_noroute); 6921a499816SEdward Tomasz Napierala error = EHOSTUNREACH; 6931a499816SEdward Tomasz Napierala goto bad; 6941a499816SEdward Tomasz Napierala } 6951dfcf0d2SAndre Oppermann /* Update variables that are affected by ipsec4_output(). */ 69633841545SHajimu UMEMOTO ip = mtod(m, struct ip *); 69733841545SHajimu UMEMOTO hlen = ip->ip_hl << 2; 698b2630c29SGeorge V. Neville-Neil #endif /* IPSEC */ 69933841545SHajimu UMEMOTO 700c21fd232SAndre Oppermann /* Jump over all PFIL processing if hooks are not active. */ 701b252313fSGleb Smirnoff if (PFIL_HOOKED_OUT(V_inet_pfil_head)) { 70205fc9d78SKristof Provost switch (ip_output_pfil(&m, ifp, flags, inp, dst, &fibnum, 70305fc9d78SKristof Provost &error)) { 704d9f2a782SErmal Luçi case 1: /* Finished */ 705c4ac87eaSDarren Reed goto done; 7069b932e9eSAndre Oppermann 707d9f2a782SErmal Luçi case 0: /* Continue normally */ 708c4ac87eaSDarren Reed ip = mtod(m, struct ip *); 709d9f2a782SErmal Luçi break; 710fed1c7e9SSøren Schmidt 711d9f2a782SErmal Luçi case -1: /* Need to try again */ 712d9f2a782SErmal Luçi /* Reset everything for a new round */ 7136ca363ebSGleb Smirnoff if (ro != NULL) { 714983066f0SAlexander V. Chernikov RO_NHFREE(ro); 7154fb3a820SAlexander V. Chernikov ro->ro_prepend = NULL; 7166ca363ebSGleb Smirnoff } 71762e1a437SZhenlei Huang gw = (const struct sockaddr *)dst; 718d9f2a782SErmal Luçi ip = mtod(m, struct ip *); 7199b932e9eSAndre Oppermann goto again; 720d9f2a782SErmal Luçi } 721099dd043SAndre Oppermann } 7222b25acc1SLuigi Rizzo 723868aabb4SRichard Scheffenegger if (vlan_pcp > -1) 724868aabb4SRichard Scheffenegger EVL_APPLY_PRI(m, vlan_pcp); 725868aabb4SRichard Scheffenegger 7266c1c6ae5SRodney W. Grimes /* IN_LOOPBACK must not appear on the wire - RFC1122. */ 7276c1c6ae5SRodney W. Grimes if (IN_LOOPBACK(ntohl(ip->ip_dst.s_addr)) || 7286c1c6ae5SRodney W. Grimes IN_LOOPBACK(ntohl(ip->ip_src.s_addr))) { 72951c8ec4aSRuslan Ermilov if ((ifp->if_flags & IFF_LOOPBACK) == 0) { 73086425c62SRobert Watson IPSTAT_INC(ips_badaddr); 73151c8ec4aSRuslan Ermilov error = EADDRNOTAVAIL; 73251c8ec4aSRuslan Ermilov goto bad; 73351c8ec4aSRuslan Ermilov } 73451c8ec4aSRuslan Ermilov } 73551c8ec4aSRuslan Ermilov 736*44775b16SMark Johnston /* Ensure the packet data is mapped if the interface requires it. */ 737*44775b16SMark Johnston if ((ifp->if_capenable & IFCAP_MEXTPG) == 0) { 738*44775b16SMark Johnston m = mb_unmapped_to_ext(m); 739*44775b16SMark Johnston if (m == NULL) { 740*44775b16SMark Johnston IPSTAT_INC(ips_odropped); 741*44775b16SMark Johnston error = ENOBUFS; 742*44775b16SMark Johnston goto bad; 743*44775b16SMark Johnston } 744*44775b16SMark Johnston } 745*44775b16SMark Johnston 746206a3274SRuslan Ermilov m->m_pkthdr.csum_flags |= CSUM_IP; 747078468edSGleb Smirnoff if (m->m_pkthdr.csum_flags & CSUM_DELAY_DATA & ~ifp->if_hwassist) { 748db4f9cc7SJonathan Lemon in_delayed_cksum(m); 749078468edSGleb Smirnoff m->m_pkthdr.csum_flags &= ~CSUM_DELAY_DATA; 750db4f9cc7SJonathan Lemon } 75195033af9SMark Johnston #if defined(SCTP) || defined(SCTP_SUPPORT) 752078468edSGleb Smirnoff if (m->m_pkthdr.csum_flags & CSUM_SCTP & ~ifp->if_hwassist) { 7531966e5b5SRandall Stewart sctp_delayed_cksum(m, (uint32_t)(ip->ip_hl << 2)); 754078468edSGleb Smirnoff m->m_pkthdr.csum_flags &= ~CSUM_SCTP; 7552f4afd21SRandall Stewart } 7562f4afd21SRandall Stewart #endif 757db4f9cc7SJonathan Lemon 758e7319babSPoul-Henning Kamp /* 759db4f9cc7SJonathan Lemon * If small enough for interface, or the interface will take 760233dcce1SAndre Oppermann * care of the fragmentation for us, we can just send directly. 761b092fd6cSNavdeep Parhar * Note that if_vxlan could have requested TSO even though the outer 762b092fd6cSNavdeep Parhar * frame is UDP. It is correct to not fragment such datagrams and 763b092fd6cSNavdeep Parhar * instead just pass them on to the driver. 764df8bae1dSRodney W. Grimes */ 76521d172a3SGleb Smirnoff if (ip_len <= mtu || 766b092fd6cSNavdeep Parhar (m->m_pkthdr.csum_flags & ifp->if_hwassist & 767b092fd6cSNavdeep Parhar (CSUM_TSO | CSUM_INNER_TSO)) != 0) { 768df8bae1dSRodney W. Grimes ip->ip_sum = 0; 769078468edSGleb Smirnoff if (m->m_pkthdr.csum_flags & CSUM_IP & ~ifp->if_hwassist) { 770df8bae1dSRodney W. Grimes ip->ip_sum = in_cksum(m, hlen); 771078468edSGleb Smirnoff m->m_pkthdr.csum_flags &= ~CSUM_IP; 772078468edSGleb Smirnoff } 7735da9f8faSJosef Karthauser 774233dcce1SAndre Oppermann /* 775233dcce1SAndre Oppermann * Record statistics for this interface address. 776233dcce1SAndre Oppermann * With CSUM_TSO the byte/packet count will be slightly 777233dcce1SAndre Oppermann * incorrect because we count the IP+TCP headers only 778233dcce1SAndre Oppermann * once instead of for every generated packet. 779233dcce1SAndre Oppermann */ 78038c1bc35SRuslan Ermilov if (!(flags & IP_FORWARDING) && ia) { 781b092fd6cSNavdeep Parhar if (m->m_pkthdr.csum_flags & 782b092fd6cSNavdeep Parhar (CSUM_TSO | CSUM_INNER_TSO)) 7837caf4ab7SGleb Smirnoff counter_u64_add(ia->ia_ifa.ifa_opackets, 7847caf4ab7SGleb Smirnoff m->m_pkthdr.len / m->m_pkthdr.tso_segsz); 785233dcce1SAndre Oppermann else 7867caf4ab7SGleb Smirnoff counter_u64_add(ia->ia_ifa.ifa_opackets, 1); 7877caf4ab7SGleb Smirnoff 7887caf4ab7SGleb Smirnoff counter_u64_add(ia->ia_ifa.ifa_obytes, m->m_pkthdr.len); 7895da9f8faSJosef Karthauser } 79053dcc544SMike Silbersack #ifdef MBUF_STRESS_TEST 7913390d476SMike Silbersack if (mbuf_frag_size && m->m_pkthdr.len > mbuf_frag_size) 792eb1b1807SGleb Smirnoff m = m_fragment(m, M_NOWAIT, mbuf_frag_size); 7939d9edc56SMike Silbersack #endif 794147f74d1SAndre Oppermann /* 795147f74d1SAndre Oppermann * Reset layer specific mbuf flags 796147f74d1SAndre Oppermann * to avoid confusing lower layers. 797147f74d1SAndre Oppermann */ 79886bd0491SAndre Oppermann m_clrprotoflags(m); 79957f60867SMark Johnston IP_PROBE(send, NULL, NULL, ip, ifp, ip, NULL); 80035c7bb34SRandall Stewart error = ip_output_send(inp, ifp, m, gw, ro, 80135c7bb34SRandall Stewart (flags & IP_NO_SND_TAG_RL) ? false : true); 802df8bae1dSRodney W. Grimes goto done; 803df8bae1dSRodney W. Grimes } 8041e78ac21SJeffrey Hsu 805233dcce1SAndre Oppermann /* Balk when DF bit is set or the interface didn't support TSO. */ 806b092fd6cSNavdeep Parhar if ((ip_off & IP_DF) || 807b092fd6cSNavdeep Parhar (m->m_pkthdr.csum_flags & (CSUM_TSO | CSUM_INNER_TSO))) { 808df8bae1dSRodney W. Grimes error = EMSGSIZE; 80986425c62SRobert Watson IPSTAT_INC(ips_cantfrag); 810df8bae1dSRodney W. Grimes goto bad; 811df8bae1dSRodney W. Grimes } 8121e78ac21SJeffrey Hsu 8131e78ac21SJeffrey Hsu /* 8141e78ac21SJeffrey Hsu * Too large for interface; fragment if possible. If successful, 8151e78ac21SJeffrey Hsu * on return, m will point to a list of packets to be sent. 8161e78ac21SJeffrey Hsu */ 817078468edSGleb Smirnoff error = ip_fragment(ip, &m, mtu, ifp->if_hwassist); 8181e78ac21SJeffrey Hsu if (error) 819df8bae1dSRodney W. Grimes goto bad; 8201e78ac21SJeffrey Hsu for (; m; m = m0) { 821df8bae1dSRodney W. Grimes m0 = m->m_nextpkt; 822b375c9ecSLuigi Rizzo m->m_nextpkt = 0; 82307203494SDaniel C. Sobral if (error == 0) { 824fe937674SJosef Karthauser /* Record statistics for this interface address. */ 82507203494SDaniel C. Sobral if (ia != NULL) { 8267caf4ab7SGleb Smirnoff counter_u64_add(ia->ia_ifa.ifa_opackets, 1); 8277caf4ab7SGleb Smirnoff counter_u64_add(ia->ia_ifa.ifa_obytes, 8287caf4ab7SGleb Smirnoff m->m_pkthdr.len); 82907203494SDaniel C. Sobral } 830147f74d1SAndre Oppermann /* 831147f74d1SAndre Oppermann * Reset layer specific mbuf flags 832147f74d1SAndre Oppermann * to avoid confusing upper layers. 833147f74d1SAndre Oppermann */ 83486bd0491SAndre Oppermann m_clrprotoflags(m); 835fe937674SJosef Karthauser 8362e77d270SAndrey V. Elsukov IP_PROBE(send, NULL, NULL, mtod(m, struct ip *), ifp, 8372e77d270SAndrey V. Elsukov mtod(m, struct ip *), NULL); 83835c7bb34SRandall Stewart error = ip_output_send(inp, ifp, m, gw, ro, true); 839fe937674SJosef Karthauser } else 840df8bae1dSRodney W. Grimes m_freem(m); 841df8bae1dSRodney W. Grimes } 842df8bae1dSRodney W. Grimes 843df8bae1dSRodney W. Grimes if (error == 0) 84486425c62SRobert Watson IPSTAT_INC(ips_fragmented); 8451e78ac21SJeffrey Hsu 846df8bae1dSRodney W. Grimes done: 847df8bae1dSRodney W. Grimes return (error); 848df8bae1dSRodney W. Grimes bad: 8493528d68fSBill Paul m_freem(m); 850df8bae1dSRodney W. Grimes goto done; 851df8bae1dSRodney W. Grimes } 852df8bae1dSRodney W. Grimes 8531e78ac21SJeffrey Hsu /* 8541e78ac21SJeffrey Hsu * Create a chain of fragments which fit the given mtu. m_frag points to the 8551e78ac21SJeffrey Hsu * mbuf to be fragmented; on return it points to the chain with the fragments. 8561e78ac21SJeffrey Hsu * Return 0 if no error. If error, m_frag may contain a partially built 8571e78ac21SJeffrey Hsu * chain of fragments that should be freed by the caller. 8581e78ac21SJeffrey Hsu * 8591e78ac21SJeffrey Hsu * if_hwassist_flags is the hw offload capabilities (see if_data.ifi_hwassist) 8601e78ac21SJeffrey Hsu */ 8611e78ac21SJeffrey Hsu int 8621e78ac21SJeffrey Hsu ip_fragment(struct ip *ip, struct mbuf **m_frag, int mtu, 863078468edSGleb Smirnoff u_long if_hwassist_flags) 8641e78ac21SJeffrey Hsu { 8651e78ac21SJeffrey Hsu int error = 0; 8661e78ac21SJeffrey Hsu int hlen = ip->ip_hl << 2; 8671e78ac21SJeffrey Hsu int len = (mtu - hlen) & ~7; /* size of payload in each fragment */ 8681e78ac21SJeffrey Hsu int off; 8691e78ac21SJeffrey Hsu struct mbuf *m0 = *m_frag; /* the original packet */ 8701e78ac21SJeffrey Hsu int firstlen; 8711e78ac21SJeffrey Hsu struct mbuf **mnext; 8721e78ac21SJeffrey Hsu int nfrags; 87321d172a3SGleb Smirnoff uint16_t ip_len, ip_off; 8741e78ac21SJeffrey Hsu 87521d172a3SGleb Smirnoff ip_len = ntohs(ip->ip_len); 87621d172a3SGleb Smirnoff ip_off = ntohs(ip->ip_off); 87721d172a3SGleb Smirnoff 87821d172a3SGleb Smirnoff if (ip_off & IP_DF) { /* Fragmentation not allowed */ 87986425c62SRobert Watson IPSTAT_INC(ips_cantfrag); 8801e78ac21SJeffrey Hsu return EMSGSIZE; 8811e78ac21SJeffrey Hsu } 8821e78ac21SJeffrey Hsu 8831e78ac21SJeffrey Hsu /* 8841e78ac21SJeffrey Hsu * Must be able to put at least 8 bytes per fragment. 8851e78ac21SJeffrey Hsu */ 8861e78ac21SJeffrey Hsu if (len < 8) 8871e78ac21SJeffrey Hsu return EMSGSIZE; 8881e78ac21SJeffrey Hsu 8891e78ac21SJeffrey Hsu /* 8901e78ac21SJeffrey Hsu * If the interface will not calculate checksums on 8911e78ac21SJeffrey Hsu * fragmented packets, then do it here. 8921e78ac21SJeffrey Hsu */ 893da2299c5SAndre Oppermann if (m0->m_pkthdr.csum_flags & CSUM_DELAY_DATA) { 8941e78ac21SJeffrey Hsu in_delayed_cksum(m0); 8951e78ac21SJeffrey Hsu m0->m_pkthdr.csum_flags &= ~CSUM_DELAY_DATA; 8961e78ac21SJeffrey Hsu } 89795033af9SMark Johnston #if defined(SCTP) || defined(SCTP_SUPPORT) 898da2299c5SAndre Oppermann if (m0->m_pkthdr.csum_flags & CSUM_SCTP) { 8991966e5b5SRandall Stewart sctp_delayed_cksum(m0, hlen); 9002f4afd21SRandall Stewart m0->m_pkthdr.csum_flags &= ~CSUM_SCTP; 9012f4afd21SRandall Stewart } 9022f4afd21SRandall Stewart #endif 9031e78ac21SJeffrey Hsu if (len > PAGE_SIZE) { 9041e78ac21SJeffrey Hsu /* 9051e78ac21SJeffrey Hsu * Fragment large datagrams such that each segment 9061e78ac21SJeffrey Hsu * contains a multiple of PAGE_SIZE amount of data, 9071e78ac21SJeffrey Hsu * plus headers. This enables a receiver to perform 9081e78ac21SJeffrey Hsu * page-flipping zero-copy optimizations. 9091e78ac21SJeffrey Hsu * 9101e78ac21SJeffrey Hsu * XXX When does this help given that sender and receiver 9111e78ac21SJeffrey Hsu * could have different page sizes, and also mtu could 9121e78ac21SJeffrey Hsu * be less than the receiver's page size ? 9131e78ac21SJeffrey Hsu */ 9141e78ac21SJeffrey Hsu int newlen; 9151e78ac21SJeffrey Hsu 9169c0f6aa7SHans Petter Selasky off = MIN(mtu, m0->m_pkthdr.len); 9171e78ac21SJeffrey Hsu 9181e78ac21SJeffrey Hsu /* 9191e78ac21SJeffrey Hsu * firstlen (off - hlen) must be aligned on an 9201e78ac21SJeffrey Hsu * 8-byte boundary 9211e78ac21SJeffrey Hsu */ 9221e78ac21SJeffrey Hsu if (off < hlen) 9231e78ac21SJeffrey Hsu goto smart_frag_failure; 9241e78ac21SJeffrey Hsu off = ((off - hlen) & ~7) + hlen; 9251e78ac21SJeffrey Hsu newlen = (~PAGE_MASK) & mtu; 9261e78ac21SJeffrey Hsu if ((newlen + sizeof (struct ip)) > mtu) { 9271e78ac21SJeffrey Hsu /* we failed, go back the default */ 9281e78ac21SJeffrey Hsu smart_frag_failure: 9291e78ac21SJeffrey Hsu newlen = len; 9301e78ac21SJeffrey Hsu off = hlen + len; 9311e78ac21SJeffrey Hsu } 9321e78ac21SJeffrey Hsu len = newlen; 9331e78ac21SJeffrey Hsu 9341e78ac21SJeffrey Hsu } else { 9351e78ac21SJeffrey Hsu off = hlen + len; 9361e78ac21SJeffrey Hsu } 9371e78ac21SJeffrey Hsu 9381e78ac21SJeffrey Hsu firstlen = off - hlen; 9391e78ac21SJeffrey Hsu mnext = &m0->m_nextpkt; /* pointer to next packet */ 9401e78ac21SJeffrey Hsu 9411e78ac21SJeffrey Hsu /* 9421e78ac21SJeffrey Hsu * Loop through length of segment after first fragment, 9431e78ac21SJeffrey Hsu * make new header and copy data of each part and link onto chain. 9441e78ac21SJeffrey Hsu * Here, m0 is the original packet, m is the fragment being created. 9451e78ac21SJeffrey Hsu * The fragments are linked off the m_nextpkt of the original 9461e78ac21SJeffrey Hsu * packet, which after processing serves as the first fragment. 9471e78ac21SJeffrey Hsu */ 94821d172a3SGleb Smirnoff for (nfrags = 1; off < ip_len; off += len, nfrags++) { 9491e78ac21SJeffrey Hsu struct ip *mhip; /* ip header on the fragment */ 9501e78ac21SJeffrey Hsu struct mbuf *m; 9511e78ac21SJeffrey Hsu int mhlen = sizeof (struct ip); 9521e78ac21SJeffrey Hsu 953dc4ad05eSGleb Smirnoff m = m_gethdr(M_NOWAIT, MT_DATA); 9540b17fba7SAndre Oppermann if (m == NULL) { 9551e78ac21SJeffrey Hsu error = ENOBUFS; 95686425c62SRobert Watson IPSTAT_INC(ips_odropped); 9571e78ac21SJeffrey Hsu goto done; 9581e78ac21SJeffrey Hsu } 959c4c4346fSHans Petter Selasky /* 960c4c4346fSHans Petter Selasky * Make sure the complete packet header gets copied 961c4c4346fSHans Petter Selasky * from the originating mbuf to the newly created 962c4c4346fSHans Petter Selasky * mbuf. This also ensures that existing firewall 963c4c4346fSHans Petter Selasky * classification(s), VLAN tags and so on get copied 964c4c4346fSHans Petter Selasky * to the resulting fragmented packet(s): 965c4c4346fSHans Petter Selasky */ 966c4c4346fSHans Petter Selasky if (m_dup_pkthdr(m, m0, M_NOWAIT) == 0) { 967c4c4346fSHans Petter Selasky m_free(m); 968c4c4346fSHans Petter Selasky error = ENOBUFS; 969c4c4346fSHans Petter Selasky IPSTAT_INC(ips_odropped); 970c4c4346fSHans Petter Selasky goto done; 971c4c4346fSHans Petter Selasky } 9721e78ac21SJeffrey Hsu /* 9731e78ac21SJeffrey Hsu * In the first mbuf, leave room for the link header, then 9741e78ac21SJeffrey Hsu * copy the original IP header including options. The payload 9758b889dbbSBruce M Simpson * goes into an additional mbuf chain returned by m_copym(). 9761e78ac21SJeffrey Hsu */ 9771e78ac21SJeffrey Hsu m->m_data += max_linkhdr; 9781e78ac21SJeffrey Hsu mhip = mtod(m, struct ip *); 9791e78ac21SJeffrey Hsu *mhip = *ip; 9801e78ac21SJeffrey Hsu if (hlen > sizeof (struct ip)) { 9811e78ac21SJeffrey Hsu mhlen = ip_optcopy(ip, mhip) + sizeof (struct ip); 9821e78ac21SJeffrey Hsu mhip->ip_v = IPVERSION; 9831e78ac21SJeffrey Hsu mhip->ip_hl = mhlen >> 2; 9841e78ac21SJeffrey Hsu } 9851e78ac21SJeffrey Hsu m->m_len = mhlen; 98621d172a3SGleb Smirnoff /* XXX do we need to add ip_off below ? */ 98721d172a3SGleb Smirnoff mhip->ip_off = ((off - hlen) >> 3) + ip_off; 988fb86dfcdSAndre Oppermann if (off + len >= ip_len) 98921d172a3SGleb Smirnoff len = ip_len - off; 990fb86dfcdSAndre Oppermann else 9911e78ac21SJeffrey Hsu mhip->ip_off |= IP_MF; 9921e78ac21SJeffrey Hsu mhip->ip_len = htons((u_short)(len + mhlen)); 993eb1b1807SGleb Smirnoff m->m_next = m_copym(m0, off, len, M_NOWAIT); 9940b17fba7SAndre Oppermann if (m->m_next == NULL) { /* copy failed */ 9951e78ac21SJeffrey Hsu m_free(m); 9961e78ac21SJeffrey Hsu error = ENOBUFS; /* ??? */ 99786425c62SRobert Watson IPSTAT_INC(ips_odropped); 9981e78ac21SJeffrey Hsu goto done; 9991e78ac21SJeffrey Hsu } 10001e78ac21SJeffrey Hsu m->m_pkthdr.len = mhlen + len; 10011e78ac21SJeffrey Hsu #ifdef MAC 100230d239bcSRobert Watson mac_netinet_fragment(m0, m); 10031e78ac21SJeffrey Hsu #endif 10041e78ac21SJeffrey Hsu mhip->ip_off = htons(mhip->ip_off); 10051e78ac21SJeffrey Hsu mhip->ip_sum = 0; 1006078468edSGleb Smirnoff if (m->m_pkthdr.csum_flags & CSUM_IP & ~if_hwassist_flags) { 10071e78ac21SJeffrey Hsu mhip->ip_sum = in_cksum(m, mhlen); 1008078468edSGleb Smirnoff m->m_pkthdr.csum_flags &= ~CSUM_IP; 1009078468edSGleb Smirnoff } 10101e78ac21SJeffrey Hsu *mnext = m; 10111e78ac21SJeffrey Hsu mnext = &m->m_nextpkt; 10121e78ac21SJeffrey Hsu } 101386425c62SRobert Watson IPSTAT_ADD(ips_ofragments, nfrags); 10141e78ac21SJeffrey Hsu 10151e78ac21SJeffrey Hsu /* 10161e78ac21SJeffrey Hsu * Update first fragment by trimming what's been copied out 10171e78ac21SJeffrey Hsu * and updating header. 10181e78ac21SJeffrey Hsu */ 101921d172a3SGleb Smirnoff m_adj(m0, hlen + firstlen - ip_len); 10201e78ac21SJeffrey Hsu m0->m_pkthdr.len = hlen + firstlen; 10211e78ac21SJeffrey Hsu ip->ip_len = htons((u_short)m0->m_pkthdr.len); 102221d172a3SGleb Smirnoff ip->ip_off = htons(ip_off | IP_MF); 10231e78ac21SJeffrey Hsu ip->ip_sum = 0; 1024078468edSGleb Smirnoff if (m0->m_pkthdr.csum_flags & CSUM_IP & ~if_hwassist_flags) { 10251e78ac21SJeffrey Hsu ip->ip_sum = in_cksum(m0, hlen); 1026078468edSGleb Smirnoff m0->m_pkthdr.csum_flags &= ~CSUM_IP; 1027078468edSGleb Smirnoff } 10281e78ac21SJeffrey Hsu 10291e78ac21SJeffrey Hsu done: 10301e78ac21SJeffrey Hsu *m_frag = m0; 10311e78ac21SJeffrey Hsu return error; 10321e78ac21SJeffrey Hsu } 10331e78ac21SJeffrey Hsu 10341c238475SJonathan Lemon void 1035db4f9cc7SJonathan Lemon in_delayed_cksum(struct mbuf *m) 1036db4f9cc7SJonathan Lemon { 1037db4f9cc7SJonathan Lemon struct ip *ip; 10381fdbfb90STom Jones struct udphdr *uh; 10391fdbfb90STom Jones uint16_t cklen, csum, offset; 1040db4f9cc7SJonathan Lemon 1041db4f9cc7SJonathan Lemon ip = mtod(m, struct ip *); 104253be11f6SPoul-Henning Kamp offset = ip->ip_hl << 2 ; 10431fdbfb90STom Jones 10441fdbfb90STom Jones if (m->m_pkthdr.csum_flags & CSUM_UDP) { 10451fdbfb90STom Jones /* if udp header is not in the first mbuf copy udplen */ 1046b6e87011STom Jones if (offset + sizeof(struct udphdr) > m->m_len) { 1047590d0a43SAndrey V. Elsukov m_copydata(m, offset + offsetof(struct udphdr, 1048590d0a43SAndrey V. Elsukov uh_ulen), sizeof(cklen), (caddr_t)&cklen); 1049b6e87011STom Jones cklen = ntohs(cklen); 1050b6e87011STom Jones } else { 1051590d0a43SAndrey V. Elsukov uh = (struct udphdr *)mtodo(m, offset); 10521fdbfb90STom Jones cklen = ntohs(uh->uh_ulen); 10531fdbfb90STom Jones } 10541fdbfb90STom Jones csum = in_cksum_skip(m, cklen + offset, offset); 10551fdbfb90STom Jones if (csum == 0) 1056206a3274SRuslan Ermilov csum = 0xffff; 10571fdbfb90STom Jones } else { 10581fdbfb90STom Jones cklen = ntohs(ip->ip_len); 10591fdbfb90STom Jones csum = in_cksum_skip(m, cklen, offset); 10601fdbfb90STom Jones } 1061db4f9cc7SJonathan Lemon offset += m->m_pkthdr.csum_data; /* checksum offset */ 1062db4f9cc7SJonathan Lemon 1063590d0a43SAndrey V. Elsukov if (offset + sizeof(csum) > m->m_len) 1064590d0a43SAndrey V. Elsukov m_copyback(m, offset, sizeof(csum), (caddr_t)&csum); 1065590d0a43SAndrey V. Elsukov else 1066590d0a43SAndrey V. Elsukov *(u_short *)mtodo(m, offset) = csum; 1067db4f9cc7SJonathan Lemon } 1068db4f9cc7SJonathan Lemon 1069df8bae1dSRodney W. Grimes /* 1070df8bae1dSRodney W. Grimes * IP socket option processing. 1071df8bae1dSRodney W. Grimes */ 1072df8bae1dSRodney W. Grimes int 1073f2565d68SRobert Watson ip_ctloutput(struct socket *so, struct sockopt *sopt) 1074df8bae1dSRodney W. Grimes { 1075cfe8b629SGarrett Wollman struct inpcb *inp = sotoinpcb(so); 1076cfe8b629SGarrett Wollman int error, optval; 1077dc847eb6SAdrian Chadd #ifdef RSS 1078dc847eb6SAdrian Chadd uint32_t rss_bucket; 1079dc847eb6SAdrian Chadd int retval; 1080dc847eb6SAdrian Chadd #endif 1081df8bae1dSRodney W. Grimes 1082cfe8b629SGarrett Wollman error = optval = 0; 1083cfe8b629SGarrett Wollman if (sopt->sopt_level != IPPROTO_IP) { 1084fc06cd42SMikolaj Golub error = EINVAL; 1085fc06cd42SMikolaj Golub 1086fc06cd42SMikolaj Golub if (sopt->sopt_level == SOL_SOCKET && 1087fc06cd42SMikolaj Golub sopt->sopt_dir == SOPT_SET) { 1088fc06cd42SMikolaj Golub switch (sopt->sopt_name) { 1089fc06cd42SMikolaj Golub case SO_REUSEADDR: 1090fc06cd42SMikolaj Golub INP_WLOCK(inp); 1091efdf104bSMikolaj Golub if ((so->so_options & SO_REUSEADDR) != 0) 1092efdf104bSMikolaj Golub inp->inp_flags2 |= INP_REUSEADDR; 1093fc06cd42SMikolaj Golub else 1094efdf104bSMikolaj Golub inp->inp_flags2 &= ~INP_REUSEADDR; 1095fc06cd42SMikolaj Golub INP_WUNLOCK(inp); 1096fc06cd42SMikolaj Golub error = 0; 1097fc06cd42SMikolaj Golub break; 1098fc06cd42SMikolaj Golub case SO_REUSEPORT: 1099fc06cd42SMikolaj Golub INP_WLOCK(inp); 1100fc06cd42SMikolaj Golub if ((so->so_options & SO_REUSEPORT) != 0) 1101fc06cd42SMikolaj Golub inp->inp_flags2 |= INP_REUSEPORT; 1102fc06cd42SMikolaj Golub else 1103fc06cd42SMikolaj Golub inp->inp_flags2 &= ~INP_REUSEPORT; 1104fc06cd42SMikolaj Golub INP_WUNLOCK(inp); 1105fc06cd42SMikolaj Golub error = 0; 1106fc06cd42SMikolaj Golub break; 11071a43cff9SSean Bruno case SO_REUSEPORT_LB: 11081a43cff9SSean Bruno INP_WLOCK(inp); 11091a43cff9SSean Bruno if ((so->so_options & SO_REUSEPORT_LB) != 0) 11101a43cff9SSean Bruno inp->inp_flags2 |= INP_REUSEPORT_LB; 11111a43cff9SSean Bruno else 11121a43cff9SSean Bruno inp->inp_flags2 &= ~INP_REUSEPORT_LB; 11131a43cff9SSean Bruno INP_WUNLOCK(inp); 11141a43cff9SSean Bruno error = 0; 11151a43cff9SSean Bruno break; 1116fc06cd42SMikolaj Golub case SO_SETFIB: 1117fc06cd42SMikolaj Golub INP_WLOCK(inp); 1118fc06cd42SMikolaj Golub inp->inp_inc.inc_fibnum = so->so_fibnum; 1119fc06cd42SMikolaj Golub INP_WUNLOCK(inp); 1120fc06cd42SMikolaj Golub error = 0; 1121fc06cd42SMikolaj Golub break; 1122f3e7afe2SHans Petter Selasky case SO_MAX_PACING_RATE: 1123f3e7afe2SHans Petter Selasky #ifdef RATELIMIT 1124f3e7afe2SHans Petter Selasky INP_WLOCK(inp); 1125f3e7afe2SHans Petter Selasky inp->inp_flags2 |= INP_RATE_LIMIT_CHANGED; 1126f3e7afe2SHans Petter Selasky INP_WUNLOCK(inp); 1127f3e7afe2SHans Petter Selasky error = 0; 1128f3e7afe2SHans Petter Selasky #else 1129f3e7afe2SHans Petter Selasky error = EOPNOTSUPP; 1130f3e7afe2SHans Petter Selasky #endif 1131f3e7afe2SHans Petter Selasky break; 1132fc06cd42SMikolaj Golub default: 1133fc06cd42SMikolaj Golub break; 1134fc06cd42SMikolaj Golub } 1135fc06cd42SMikolaj Golub } 1136fc06cd42SMikolaj Golub return (error); 1137cfe8b629SGarrett Wollman } 1138df8bae1dSRodney W. Grimes 1139cfe8b629SGarrett Wollman switch (sopt->sopt_dir) { 1140cfe8b629SGarrett Wollman case SOPT_SET: 1141cfe8b629SGarrett Wollman switch (sopt->sopt_name) { 1142df8bae1dSRodney W. Grimes case IP_OPTIONS: 1143df8bae1dSRodney W. Grimes #ifdef notyet 1144df8bae1dSRodney W. Grimes case IP_RETOPTS: 1145df8bae1dSRodney W. Grimes #endif 1146cfe8b629SGarrett Wollman { 1147cfe8b629SGarrett Wollman struct mbuf *m; 1148cfe8b629SGarrett Wollman if (sopt->sopt_valsize > MLEN) { 1149cfe8b629SGarrett Wollman error = EMSGSIZE; 1150cfe8b629SGarrett Wollman break; 1151cfe8b629SGarrett Wollman } 1152dc4ad05eSGleb Smirnoff m = m_get(sopt->sopt_td ? M_WAITOK : M_NOWAIT, MT_DATA); 11530b17fba7SAndre Oppermann if (m == NULL) { 1154cfe8b629SGarrett Wollman error = ENOBUFS; 1155cfe8b629SGarrett Wollman break; 1156cfe8b629SGarrett Wollman } 1157cfe8b629SGarrett Wollman m->m_len = sopt->sopt_valsize; 1158cfe8b629SGarrett Wollman error = sooptcopyin(sopt, mtod(m, char *), m->m_len, 1159cfe8b629SGarrett Wollman m->m_len); 1160635354c4SMaxim Konovalov if (error) { 1161635354c4SMaxim Konovalov m_free(m); 1162635354c4SMaxim Konovalov break; 1163635354c4SMaxim Konovalov } 11648501a69cSRobert Watson INP_WLOCK(inp); 1165993d9505SRobert Watson error = ip_pcbopts(inp, sopt->sopt_name, m); 11668501a69cSRobert Watson INP_WUNLOCK(inp); 1167993d9505SRobert Watson return (error); 1168cfe8b629SGarrett Wollman } 1169df8bae1dSRodney W. Grimes 1170f44270e7SPawel Jakub Dawidek case IP_BINDANY: 1171f44270e7SPawel Jakub Dawidek if (sopt->sopt_td != NULL) { 1172f44270e7SPawel Jakub Dawidek error = priv_check(sopt->sopt_td, 1173f44270e7SPawel Jakub Dawidek PRIV_NETINET_BINDANY); 1174f44270e7SPawel Jakub Dawidek if (error) 1175be9347e3SAdrian Chadd break; 1176be9347e3SAdrian Chadd } 1177cef27294SAdrian Chadd /* FALLTHROUGH */ 11780a100a6fSAdrian Chadd case IP_BINDMULTI: 11790a100a6fSAdrian Chadd #ifdef RSS 11800a100a6fSAdrian Chadd case IP_RSS_LISTEN_BUCKET: 11810a100a6fSAdrian Chadd #endif 1182df8bae1dSRodney W. Grimes case IP_TOS: 1183df8bae1dSRodney W. Grimes case IP_TTL: 1184936cd18dSAndre Oppermann case IP_MINTTL: 1185df8bae1dSRodney W. Grimes case IP_RECVOPTS: 1186df8bae1dSRodney W. Grimes case IP_RECVRETOPTS: 1187dce33a45SErmal Luçi case IP_ORIGDSTADDR: 1188df8bae1dSRodney W. Grimes case IP_RECVDSTADDR: 11894957466bSMatthew N. Dodd case IP_RECVTTL: 119082c23ebaSBill Fenner case IP_RECVIF: 11918afa2304SBruce M Simpson case IP_ONESBCAST: 1192b2828ad2SAndre Oppermann case IP_DONTFRAG: 11933cca425bSMichael Tuexen case IP_RECVTOS: 11949d3ddf43SAdrian Chadd case IP_RECVFLOWID: 11959d3ddf43SAdrian Chadd #ifdef RSS 11969d3ddf43SAdrian Chadd case IP_RECVRSSBUCKETID: 11979d3ddf43SAdrian Chadd #endif 1198868aabb4SRichard Scheffenegger case IP_VLAN_PCP: 1199cfe8b629SGarrett Wollman error = sooptcopyin(sopt, &optval, sizeof optval, 1200cfe8b629SGarrett Wollman sizeof optval); 1201cfe8b629SGarrett Wollman if (error) 1202cfe8b629SGarrett Wollman break; 1203df8bae1dSRodney W. Grimes 1204cfe8b629SGarrett Wollman switch (sopt->sopt_name) { 1205df8bae1dSRodney W. Grimes case IP_TOS: 1206ca98b82cSDavid Greenman inp->inp_ip_tos = optval; 1207df8bae1dSRodney W. Grimes break; 1208df8bae1dSRodney W. Grimes 1209df8bae1dSRodney W. Grimes case IP_TTL: 1210ca98b82cSDavid Greenman inp->inp_ip_ttl = optval; 1211df8bae1dSRodney W. Grimes break; 1212936cd18dSAndre Oppermann 1213936cd18dSAndre Oppermann case IP_MINTTL: 1214a603c811SRobert Watson if (optval >= 0 && optval <= MAXTTL) 1215936cd18dSAndre Oppermann inp->inp_ip_minttl = optval; 1216936cd18dSAndre Oppermann else 1217936cd18dSAndre Oppermann error = EINVAL; 1218936cd18dSAndre Oppermann break; 1219936cd18dSAndre Oppermann 1220a138d217SRobert Watson #define OPTSET(bit) do { \ 12218501a69cSRobert Watson INP_WLOCK(inp); \ 1222df8bae1dSRodney W. Grimes if (optval) \ 1223df8bae1dSRodney W. Grimes inp->inp_flags |= bit; \ 1224df8bae1dSRodney W. Grimes else \ 1225a138d217SRobert Watson inp->inp_flags &= ~bit; \ 12268501a69cSRobert Watson INP_WUNLOCK(inp); \ 1227a138d217SRobert Watson } while (0) 1228df8bae1dSRodney W. Grimes 12290a100a6fSAdrian Chadd #define OPTSET2(bit, val) do { \ 12300a100a6fSAdrian Chadd INP_WLOCK(inp); \ 12310a100a6fSAdrian Chadd if (val) \ 12320a100a6fSAdrian Chadd inp->inp_flags2 |= bit; \ 12330a100a6fSAdrian Chadd else \ 12340a100a6fSAdrian Chadd inp->inp_flags2 &= ~bit; \ 12350a100a6fSAdrian Chadd INP_WUNLOCK(inp); \ 12360a100a6fSAdrian Chadd } while (0) 12370a100a6fSAdrian Chadd 1238df8bae1dSRodney W. Grimes case IP_RECVOPTS: 1239df8bae1dSRodney W. Grimes OPTSET(INP_RECVOPTS); 1240df8bae1dSRodney W. Grimes break; 1241df8bae1dSRodney W. Grimes 1242df8bae1dSRodney W. Grimes case IP_RECVRETOPTS: 1243df8bae1dSRodney W. Grimes OPTSET(INP_RECVRETOPTS); 1244df8bae1dSRodney W. Grimes break; 1245df8bae1dSRodney W. Grimes 1246df8bae1dSRodney W. Grimes case IP_RECVDSTADDR: 1247df8bae1dSRodney W. Grimes OPTSET(INP_RECVDSTADDR); 1248df8bae1dSRodney W. Grimes break; 124982c23ebaSBill Fenner 1250dce33a45SErmal Luçi case IP_ORIGDSTADDR: 1251dce33a45SErmal Luçi OPTSET2(INP_ORIGDSTADDR, optval); 1252dce33a45SErmal Luçi break; 1253dce33a45SErmal Luçi 12544957466bSMatthew N. Dodd case IP_RECVTTL: 12554957466bSMatthew N. Dodd OPTSET(INP_RECVTTL); 12564957466bSMatthew N. Dodd break; 12574957466bSMatthew N. Dodd 125882c23ebaSBill Fenner case IP_RECVIF: 125982c23ebaSBill Fenner OPTSET(INP_RECVIF); 126082c23ebaSBill Fenner break; 12616a800098SYoshinobu Inoue 12628afa2304SBruce M Simpson case IP_ONESBCAST: 12638afa2304SBruce M Simpson OPTSET(INP_ONESBCAST); 12648afa2304SBruce M Simpson break; 1265b2828ad2SAndre Oppermann case IP_DONTFRAG: 1266b2828ad2SAndre Oppermann OPTSET(INP_DONTFRAG); 1267b2828ad2SAndre Oppermann break; 1268f44270e7SPawel Jakub Dawidek case IP_BINDANY: 1269f44270e7SPawel Jakub Dawidek OPTSET(INP_BINDANY); 1270be9347e3SAdrian Chadd break; 12713cca425bSMichael Tuexen case IP_RECVTOS: 12723cca425bSMichael Tuexen OPTSET(INP_RECVTOS); 12733cca425bSMichael Tuexen break; 12740a100a6fSAdrian Chadd case IP_BINDMULTI: 12750a100a6fSAdrian Chadd OPTSET2(INP_BINDMULTI, optval); 12760a100a6fSAdrian Chadd break; 12779d3ddf43SAdrian Chadd case IP_RECVFLOWID: 12789d3ddf43SAdrian Chadd OPTSET2(INP_RECVFLOWID, optval); 12799d3ddf43SAdrian Chadd break; 12800a100a6fSAdrian Chadd #ifdef RSS 12810a100a6fSAdrian Chadd case IP_RSS_LISTEN_BUCKET: 12820a100a6fSAdrian Chadd if ((optval >= 0) && 12830a100a6fSAdrian Chadd (optval < rss_getnumbuckets())) { 12840a100a6fSAdrian Chadd inp->inp_rss_listen_bucket = optval; 12850a100a6fSAdrian Chadd OPTSET2(INP_RSS_BUCKET_SET, 1); 12860a100a6fSAdrian Chadd } else { 12870a100a6fSAdrian Chadd error = EINVAL; 12880a100a6fSAdrian Chadd } 12890a100a6fSAdrian Chadd break; 12909d3ddf43SAdrian Chadd case IP_RECVRSSBUCKETID: 12919d3ddf43SAdrian Chadd OPTSET2(INP_RECVRSSBUCKETID, optval); 12929d3ddf43SAdrian Chadd break; 12930a100a6fSAdrian Chadd #endif 1294868aabb4SRichard Scheffenegger case IP_VLAN_PCP: 1295868aabb4SRichard Scheffenegger if ((optval >= -1) && (optval <= 1296868aabb4SRichard Scheffenegger (INP_2PCP_MASK >> INP_2PCP_SHIFT))) { 1297868aabb4SRichard Scheffenegger if (optval == -1) { 1298868aabb4SRichard Scheffenegger INP_WLOCK(inp); 1299868aabb4SRichard Scheffenegger inp->inp_flags2 &= 1300868aabb4SRichard Scheffenegger ~(INP_2PCP_SET | 1301868aabb4SRichard Scheffenegger INP_2PCP_MASK); 1302868aabb4SRichard Scheffenegger INP_WUNLOCK(inp); 1303868aabb4SRichard Scheffenegger } else { 1304868aabb4SRichard Scheffenegger INP_WLOCK(inp); 1305868aabb4SRichard Scheffenegger inp->inp_flags2 |= 1306868aabb4SRichard Scheffenegger INP_2PCP_SET; 1307868aabb4SRichard Scheffenegger inp->inp_flags2 &= 1308868aabb4SRichard Scheffenegger ~INP_2PCP_MASK; 1309868aabb4SRichard Scheffenegger inp->inp_flags2 |= 1310868aabb4SRichard Scheffenegger optval << INP_2PCP_SHIFT; 1311868aabb4SRichard Scheffenegger INP_WUNLOCK(inp); 1312868aabb4SRichard Scheffenegger } 1313868aabb4SRichard Scheffenegger } else 1314868aabb4SRichard Scheffenegger error = EINVAL; 1315868aabb4SRichard Scheffenegger break; 1316df8bae1dSRodney W. Grimes } 1317df8bae1dSRodney W. Grimes break; 1318df8bae1dSRodney W. Grimes #undef OPTSET 13190a100a6fSAdrian Chadd #undef OPTSET2 1320df8bae1dSRodney W. Grimes 132171498f30SBruce M Simpson /* 132271498f30SBruce M Simpson * Multicast socket options are processed by the in_mcast 132371498f30SBruce M Simpson * module. 132471498f30SBruce M Simpson */ 1325df8bae1dSRodney W. Grimes case IP_MULTICAST_IF: 1326f0068c4aSGarrett Wollman case IP_MULTICAST_VIF: 1327df8bae1dSRodney W. Grimes case IP_MULTICAST_TTL: 1328df8bae1dSRodney W. Grimes case IP_MULTICAST_LOOP: 1329df8bae1dSRodney W. Grimes case IP_ADD_MEMBERSHIP: 1330df8bae1dSRodney W. Grimes case IP_DROP_MEMBERSHIP: 133171498f30SBruce M Simpson case IP_ADD_SOURCE_MEMBERSHIP: 133271498f30SBruce M Simpson case IP_DROP_SOURCE_MEMBERSHIP: 133371498f30SBruce M Simpson case IP_BLOCK_SOURCE: 133471498f30SBruce M Simpson case IP_UNBLOCK_SOURCE: 133571498f30SBruce M Simpson case IP_MSFILTER: 133671498f30SBruce M Simpson case MCAST_JOIN_GROUP: 133771498f30SBruce M Simpson case MCAST_LEAVE_GROUP: 133871498f30SBruce M Simpson case MCAST_JOIN_SOURCE_GROUP: 133971498f30SBruce M Simpson case MCAST_LEAVE_SOURCE_GROUP: 134071498f30SBruce M Simpson case MCAST_BLOCK_SOURCE: 134171498f30SBruce M Simpson case MCAST_UNBLOCK_SOURCE: 134271498f30SBruce M Simpson error = inp_setmoptions(inp, sopt); 1343df8bae1dSRodney W. Grimes break; 1344df8bae1dSRodney W. Grimes 134533b3ac06SPeter Wemm case IP_PORTRANGE: 1346cfe8b629SGarrett Wollman error = sooptcopyin(sopt, &optval, sizeof optval, 1347cfe8b629SGarrett Wollman sizeof optval); 1348cfe8b629SGarrett Wollman if (error) 1349cfe8b629SGarrett Wollman break; 135033b3ac06SPeter Wemm 13518501a69cSRobert Watson INP_WLOCK(inp); 135233b3ac06SPeter Wemm switch (optval) { 135333b3ac06SPeter Wemm case IP_PORTRANGE_DEFAULT: 135433b3ac06SPeter Wemm inp->inp_flags &= ~(INP_LOWPORT); 135533b3ac06SPeter Wemm inp->inp_flags &= ~(INP_HIGHPORT); 135633b3ac06SPeter Wemm break; 135733b3ac06SPeter Wemm 135833b3ac06SPeter Wemm case IP_PORTRANGE_HIGH: 135933b3ac06SPeter Wemm inp->inp_flags &= ~(INP_LOWPORT); 136033b3ac06SPeter Wemm inp->inp_flags |= INP_HIGHPORT; 136133b3ac06SPeter Wemm break; 136233b3ac06SPeter Wemm 136333b3ac06SPeter Wemm case IP_PORTRANGE_LOW: 136433b3ac06SPeter Wemm inp->inp_flags &= ~(INP_HIGHPORT); 136533b3ac06SPeter Wemm inp->inp_flags |= INP_LOWPORT; 136633b3ac06SPeter Wemm break; 136733b3ac06SPeter Wemm 136833b3ac06SPeter Wemm default: 136933b3ac06SPeter Wemm error = EINVAL; 137033b3ac06SPeter Wemm break; 137133b3ac06SPeter Wemm } 13728501a69cSRobert Watson INP_WUNLOCK(inp); 1373ce8c72b1SPeter Wemm break; 137433b3ac06SPeter Wemm 1375fcf59617SAndrey V. Elsukov #if defined(IPSEC) || defined(IPSEC_SUPPORT) 13766a800098SYoshinobu Inoue case IP_IPSEC_POLICY: 1377fcf59617SAndrey V. Elsukov if (IPSEC_ENABLED(ipv4)) { 1378fcf59617SAndrey V. Elsukov error = IPSEC_PCBCTL(ipv4, inp, sopt); 13796a800098SYoshinobu Inoue break; 13806a800098SYoshinobu Inoue } 1381fcf59617SAndrey V. Elsukov /* FALLTHROUGH */ 1382b2630c29SGeorge V. Neville-Neil #endif /* IPSEC */ 13836a800098SYoshinobu Inoue 1384df8bae1dSRodney W. Grimes default: 1385df8bae1dSRodney W. Grimes error = ENOPROTOOPT; 1386df8bae1dSRodney W. Grimes break; 1387df8bae1dSRodney W. Grimes } 1388df8bae1dSRodney W. Grimes break; 1389df8bae1dSRodney W. Grimes 1390cfe8b629SGarrett Wollman case SOPT_GET: 1391cfe8b629SGarrett Wollman switch (sopt->sopt_name) { 1392df8bae1dSRodney W. Grimes case IP_OPTIONS: 1393df8bae1dSRodney W. Grimes case IP_RETOPTS: 1394e5e3e746SMatt Macy INP_RLOCK(inp); 1395e5e3e746SMatt Macy if (inp->inp_options) { 1396e5e3e746SMatt Macy struct mbuf *options; 1397e5e3e746SMatt Macy 139810731c54SMichael Tuexen options = m_copym(inp->inp_options, 0, 139910731c54SMichael Tuexen M_COPYALL, M_NOWAIT); 1400e5e3e746SMatt Macy INP_RUNLOCK(inp); 1401e5e3e746SMatt Macy if (options != NULL) { 1402cfe8b629SGarrett Wollman error = sooptcopyout(sopt, 1403e5e3e746SMatt Macy mtod(options, char *), 1404e5e3e746SMatt Macy options->m_len); 1405e5e3e746SMatt Macy m_freem(options); 1406e5e3e746SMatt Macy } else 1407e5e3e746SMatt Macy error = ENOMEM; 1408e5e3e746SMatt Macy } else { 1409e5e3e746SMatt Macy INP_RUNLOCK(inp); 1410cfe8b629SGarrett Wollman sopt->sopt_valsize = 0; 1411e5e3e746SMatt Macy } 1412df8bae1dSRodney W. Grimes break; 1413df8bae1dSRodney W. Grimes 1414df8bae1dSRodney W. Grimes case IP_TOS: 1415df8bae1dSRodney W. Grimes case IP_TTL: 1416936cd18dSAndre Oppermann case IP_MINTTL: 1417df8bae1dSRodney W. Grimes case IP_RECVOPTS: 1418df8bae1dSRodney W. Grimes case IP_RECVRETOPTS: 1419dce33a45SErmal Luçi case IP_ORIGDSTADDR: 1420df8bae1dSRodney W. Grimes case IP_RECVDSTADDR: 14214957466bSMatthew N. Dodd case IP_RECVTTL: 142282c23ebaSBill Fenner case IP_RECVIF: 1423cfe8b629SGarrett Wollman case IP_PORTRANGE: 14248afa2304SBruce M Simpson case IP_ONESBCAST: 1425b2828ad2SAndre Oppermann case IP_DONTFRAG: 14265f6bf451SAttilio Rao case IP_BINDANY: 14273cca425bSMichael Tuexen case IP_RECVTOS: 14280a100a6fSAdrian Chadd case IP_BINDMULTI: 14299c423972SAdrian Chadd case IP_FLOWID: 14309c423972SAdrian Chadd case IP_FLOWTYPE: 14319d3ddf43SAdrian Chadd case IP_RECVFLOWID: 14320a100a6fSAdrian Chadd #ifdef RSS 14330a100a6fSAdrian Chadd case IP_RSSBUCKETID: 14349d3ddf43SAdrian Chadd case IP_RECVRSSBUCKETID: 14350a100a6fSAdrian Chadd #endif 1436868aabb4SRichard Scheffenegger case IP_VLAN_PCP: 1437cfe8b629SGarrett Wollman switch (sopt->sopt_name) { 1438df8bae1dSRodney W. Grimes case IP_TOS: 1439ca98b82cSDavid Greenman optval = inp->inp_ip_tos; 1440df8bae1dSRodney W. Grimes break; 1441df8bae1dSRodney W. Grimes 1442df8bae1dSRodney W. Grimes case IP_TTL: 1443ca98b82cSDavid Greenman optval = inp->inp_ip_ttl; 1444df8bae1dSRodney W. Grimes break; 1445df8bae1dSRodney W. Grimes 1446936cd18dSAndre Oppermann case IP_MINTTL: 1447936cd18dSAndre Oppermann optval = inp->inp_ip_minttl; 1448936cd18dSAndre Oppermann break; 1449936cd18dSAndre Oppermann 1450df8bae1dSRodney W. Grimes #define OPTBIT(bit) (inp->inp_flags & bit ? 1 : 0) 14510a100a6fSAdrian Chadd #define OPTBIT2(bit) (inp->inp_flags2 & bit ? 1 : 0) 1452df8bae1dSRodney W. Grimes 1453df8bae1dSRodney W. Grimes case IP_RECVOPTS: 1454df8bae1dSRodney W. Grimes optval = OPTBIT(INP_RECVOPTS); 1455df8bae1dSRodney W. Grimes break; 1456df8bae1dSRodney W. Grimes 1457df8bae1dSRodney W. Grimes case IP_RECVRETOPTS: 1458df8bae1dSRodney W. Grimes optval = OPTBIT(INP_RECVRETOPTS); 1459df8bae1dSRodney W. Grimes break; 1460df8bae1dSRodney W. Grimes 1461df8bae1dSRodney W. Grimes case IP_RECVDSTADDR: 1462df8bae1dSRodney W. Grimes optval = OPTBIT(INP_RECVDSTADDR); 1463df8bae1dSRodney W. Grimes break; 146482c23ebaSBill Fenner 1465dce33a45SErmal Luçi case IP_ORIGDSTADDR: 1466dce33a45SErmal Luçi optval = OPTBIT2(INP_ORIGDSTADDR); 1467dce33a45SErmal Luçi break; 1468dce33a45SErmal Luçi 14694957466bSMatthew N. Dodd case IP_RECVTTL: 14704957466bSMatthew N. Dodd optval = OPTBIT(INP_RECVTTL); 14714957466bSMatthew N. Dodd break; 14724957466bSMatthew N. Dodd 147382c23ebaSBill Fenner case IP_RECVIF: 147482c23ebaSBill Fenner optval = OPTBIT(INP_RECVIF); 147582c23ebaSBill Fenner break; 1476cfe8b629SGarrett Wollman 1477cfe8b629SGarrett Wollman case IP_PORTRANGE: 1478cfe8b629SGarrett Wollman if (inp->inp_flags & INP_HIGHPORT) 1479cfe8b629SGarrett Wollman optval = IP_PORTRANGE_HIGH; 1480cfe8b629SGarrett Wollman else if (inp->inp_flags & INP_LOWPORT) 1481cfe8b629SGarrett Wollman optval = IP_PORTRANGE_LOW; 1482cfe8b629SGarrett Wollman else 1483cfe8b629SGarrett Wollman optval = 0; 1484cfe8b629SGarrett Wollman break; 14856a800098SYoshinobu Inoue 14868afa2304SBruce M Simpson case IP_ONESBCAST: 14878afa2304SBruce M Simpson optval = OPTBIT(INP_ONESBCAST); 14888afa2304SBruce M Simpson break; 1489b2828ad2SAndre Oppermann case IP_DONTFRAG: 1490b2828ad2SAndre Oppermann optval = OPTBIT(INP_DONTFRAG); 1491b2828ad2SAndre Oppermann break; 14925f6bf451SAttilio Rao case IP_BINDANY: 14935f6bf451SAttilio Rao optval = OPTBIT(INP_BINDANY); 14945f6bf451SAttilio Rao break; 14953cca425bSMichael Tuexen case IP_RECVTOS: 14963cca425bSMichael Tuexen optval = OPTBIT(INP_RECVTOS); 14973cca425bSMichael Tuexen break; 14989c423972SAdrian Chadd case IP_FLOWID: 14999c423972SAdrian Chadd optval = inp->inp_flowid; 15009c423972SAdrian Chadd break; 15019c423972SAdrian Chadd case IP_FLOWTYPE: 15029c423972SAdrian Chadd optval = inp->inp_flowtype; 15039c423972SAdrian Chadd break; 15049d3ddf43SAdrian Chadd case IP_RECVFLOWID: 15059d3ddf43SAdrian Chadd optval = OPTBIT2(INP_RECVFLOWID); 15069d3ddf43SAdrian Chadd break; 15079c423972SAdrian Chadd #ifdef RSS 15087847796aSAdrian Chadd case IP_RSSBUCKETID: 15097847796aSAdrian Chadd retval = rss_hash2bucket(inp->inp_flowid, 15107847796aSAdrian Chadd inp->inp_flowtype, 15117847796aSAdrian Chadd &rss_bucket); 15127847796aSAdrian Chadd if (retval == 0) 15137847796aSAdrian Chadd optval = rss_bucket; 15147847796aSAdrian Chadd else 15157847796aSAdrian Chadd error = EINVAL; 15169c423972SAdrian Chadd break; 15179d3ddf43SAdrian Chadd case IP_RECVRSSBUCKETID: 15189d3ddf43SAdrian Chadd optval = OPTBIT2(INP_RECVRSSBUCKETID); 15199d3ddf43SAdrian Chadd break; 15209c423972SAdrian Chadd #endif 15210a100a6fSAdrian Chadd case IP_BINDMULTI: 15220a100a6fSAdrian Chadd optval = OPTBIT2(INP_BINDMULTI); 15230a100a6fSAdrian Chadd break; 1524868aabb4SRichard Scheffenegger case IP_VLAN_PCP: 1525868aabb4SRichard Scheffenegger if (OPTBIT2(INP_2PCP_SET)) { 1526868aabb4SRichard Scheffenegger optval = (inp->inp_flags2 & 1527868aabb4SRichard Scheffenegger INP_2PCP_MASK) >> INP_2PCP_SHIFT; 1528868aabb4SRichard Scheffenegger } else { 1529868aabb4SRichard Scheffenegger optval = -1; 1530868aabb4SRichard Scheffenegger } 1531868aabb4SRichard Scheffenegger break; 1532df8bae1dSRodney W. Grimes } 1533cfe8b629SGarrett Wollman error = sooptcopyout(sopt, &optval, sizeof optval); 1534df8bae1dSRodney W. Grimes break; 1535df8bae1dSRodney W. Grimes 153671498f30SBruce M Simpson /* 153771498f30SBruce M Simpson * Multicast socket options are processed by the in_mcast 153871498f30SBruce M Simpson * module. 153971498f30SBruce M Simpson */ 1540df8bae1dSRodney W. Grimes case IP_MULTICAST_IF: 1541f0068c4aSGarrett Wollman case IP_MULTICAST_VIF: 1542df8bae1dSRodney W. Grimes case IP_MULTICAST_TTL: 1543df8bae1dSRodney W. Grimes case IP_MULTICAST_LOOP: 154471498f30SBruce M Simpson case IP_MSFILTER: 154571498f30SBruce M Simpson error = inp_getmoptions(inp, sopt); 154633b3ac06SPeter Wemm break; 154733b3ac06SPeter Wemm 1548fcf59617SAndrey V. Elsukov #if defined(IPSEC) || defined(IPSEC_SUPPORT) 15496a800098SYoshinobu Inoue case IP_IPSEC_POLICY: 1550fcf59617SAndrey V. Elsukov if (IPSEC_ENABLED(ipv4)) { 1551fcf59617SAndrey V. Elsukov error = IPSEC_PCBCTL(ipv4, inp, sopt); 15526a800098SYoshinobu Inoue break; 15536a800098SYoshinobu Inoue } 1554fcf59617SAndrey V. Elsukov /* FALLTHROUGH */ 1555b2630c29SGeorge V. Neville-Neil #endif /* IPSEC */ 15566a800098SYoshinobu Inoue 1557df8bae1dSRodney W. Grimes default: 1558df8bae1dSRodney W. Grimes error = ENOPROTOOPT; 1559df8bae1dSRodney W. Grimes break; 1560df8bae1dSRodney W. Grimes } 1561df8bae1dSRodney W. Grimes break; 1562df8bae1dSRodney W. Grimes } 1563df8bae1dSRodney W. Grimes return (error); 1564df8bae1dSRodney W. Grimes } 1565df8bae1dSRodney W. Grimes 1566df8bae1dSRodney W. Grimes /* 1567df8bae1dSRodney W. Grimes * Routine called from ip_output() to loop back a copy of an IP multicast 1568df8bae1dSRodney W. Grimes * packet to the input queue of a specified interface. Note that this 1569df8bae1dSRodney W. Grimes * calls the output routine of the loopback "driver", but with an interface 1570f5fea3ddSPaul Traina * pointer that might NOT be a loopback interface -- evil, but easier than 1571f5fea3ddSPaul Traina * replicating that code here. 1572df8bae1dSRodney W. Grimes */ 1573df8bae1dSRodney W. Grimes static void 1574331dff07SAlexander V. Chernikov ip_mloopback(struct ifnet *ifp, const struct mbuf *m, int hlen) 1575df8bae1dSRodney W. Grimes { 1576331dff07SAlexander V. Chernikov struct ip *ip; 1577df8bae1dSRodney W. Grimes struct mbuf *copym; 1578df8bae1dSRodney W. Grimes 1579e4762f75SGeorge V. Neville-Neil /* 1580e4762f75SGeorge V. Neville-Neil * Make a deep copy of the packet because we're going to 1581e4762f75SGeorge V. Neville-Neil * modify the pack in order to generate checksums. 1582e4762f75SGeorge V. Neville-Neil */ 1583eb1b1807SGleb Smirnoff copym = m_dup(m, M_NOWAIT); 1584f0cace5dSRobert Watson if (copym != NULL && (!M_WRITABLE(copym) || copym->m_len < hlen)) 158586b1d6d2SBill Fenner copym = m_pullup(copym, hlen); 1586df8bae1dSRodney W. Grimes if (copym != NULL) { 1587390cdc6aSRuslan Ermilov /* If needed, compute the checksum and mark it as valid. */ 1588390cdc6aSRuslan Ermilov if (copym->m_pkthdr.csum_flags & CSUM_DELAY_DATA) { 1589390cdc6aSRuslan Ermilov in_delayed_cksum(copym); 1590390cdc6aSRuslan Ermilov copym->m_pkthdr.csum_flags &= ~CSUM_DELAY_DATA; 1591390cdc6aSRuslan Ermilov copym->m_pkthdr.csum_flags |= 1592390cdc6aSRuslan Ermilov CSUM_DATA_VALID | CSUM_PSEUDO_HDR; 1593390cdc6aSRuslan Ermilov copym->m_pkthdr.csum_data = 0xffff; 1594390cdc6aSRuslan Ermilov } 1595df8bae1dSRodney W. Grimes /* 1596df8bae1dSRodney W. Grimes * We don't bother to fragment if the IP length is greater 1597df8bae1dSRodney W. Grimes * than the interface's MTU. Can this possibly matter? 1598df8bae1dSRodney W. Grimes */ 15998f134647SGleb Smirnoff ip = mtod(copym, struct ip *); 1600df8bae1dSRodney W. Grimes ip->ip_sum = 0; 160186b1d6d2SBill Fenner ip->ip_sum = in_cksum(copym, hlen); 1602331dff07SAlexander V. Chernikov if_simloop(ifp, copym, AF_INET, 0); 1603df8bae1dSRodney W. Grimes } 1604df8bae1dSRodney W. Grimes } 1605