1c398230bSWarner Losh /*- 2df8bae1dSRodney W. Grimes * Copyright (c) 1982, 1986, 1988, 1990, 1993 3df8bae1dSRodney W. Grimes * The Regents of the University of California. All rights reserved. 4df8bae1dSRodney W. Grimes * 5df8bae1dSRodney W. Grimes * Redistribution and use in source and binary forms, with or without 6df8bae1dSRodney W. Grimes * modification, are permitted provided that the following conditions 7df8bae1dSRodney W. Grimes * are met: 8df8bae1dSRodney W. Grimes * 1. Redistributions of source code must retain the above copyright 9df8bae1dSRodney W. Grimes * notice, this list of conditions and the following disclaimer. 10df8bae1dSRodney W. Grimes * 2. Redistributions in binary form must reproduce the above copyright 11df8bae1dSRodney W. Grimes * notice, this list of conditions and the following disclaimer in the 12df8bae1dSRodney W. Grimes * documentation and/or other materials provided with the distribution. 13df8bae1dSRodney W. Grimes * 4. Neither the name of the University nor the names of its contributors 14df8bae1dSRodney W. Grimes * may be used to endorse or promote products derived from this software 15df8bae1dSRodney W. Grimes * without specific prior written permission. 16df8bae1dSRodney W. Grimes * 17df8bae1dSRodney W. Grimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 18df8bae1dSRodney W. Grimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 19df8bae1dSRodney W. Grimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 20df8bae1dSRodney W. Grimes * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 21df8bae1dSRodney W. Grimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 22df8bae1dSRodney W. Grimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 23df8bae1dSRodney W. Grimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 24df8bae1dSRodney W. Grimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 25df8bae1dSRodney W. Grimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 26df8bae1dSRodney W. Grimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 27df8bae1dSRodney W. Grimes * SUCH DAMAGE. 28df8bae1dSRodney W. Grimes * 29df8bae1dSRodney W. Grimes * @(#)ip_output.c 8.3 (Berkeley) 1/21/94 30df8bae1dSRodney W. Grimes */ 31df8bae1dSRodney W. Grimes 324b421e2dSMike Silbersack #include <sys/cdefs.h> 334b421e2dSMike Silbersack __FBSDID("$FreeBSD$"); 344b421e2dSMike Silbersack 351f7e052cSJulian Elischer #include "opt_ipfw.h" 366a800098SYoshinobu Inoue #include "opt_ipsec.h" 374ed84624SRobert Watson #include "opt_mac.h" 3853dcc544SMike Silbersack #include "opt_mbuf_stress_test.h" 39fbd1372aSJoerg Wunsch 40df8bae1dSRodney W. Grimes #include <sys/param.h> 4126f9a767SRodney W. Grimes #include <sys/systm.h> 42f0f6d643SLuigi Rizzo #include <sys/kernel.h> 43df8bae1dSRodney W. Grimes #include <sys/malloc.h> 44df8bae1dSRodney W. Grimes #include <sys/mbuf.h> 45acd3428bSRobert Watson #include <sys/priv.h> 46c26fe973SBjoern A. Zeeb #include <sys/proc.h> 47df8bae1dSRodney W. Grimes #include <sys/protosw.h> 48df8bae1dSRodney W. Grimes #include <sys/socket.h> 49df8bae1dSRodney W. Grimes #include <sys/socketvar.h> 509d9edc56SMike Silbersack #include <sys/sysctl.h> 51c26fe973SBjoern A. Zeeb #include <sys/ucred.h> 52df8bae1dSRodney W. Grimes 53df8bae1dSRodney W. Grimes #include <net/if.h> 549b932e9eSAndre Oppermann #include <net/netisr.h> 55c21fd232SAndre Oppermann #include <net/pfil.h> 56df8bae1dSRodney W. Grimes #include <net/route.h> 57df8bae1dSRodney W. Grimes 58df8bae1dSRodney W. Grimes #include <netinet/in.h> 59df8bae1dSRodney W. Grimes #include <netinet/in_systm.h> 60df8bae1dSRodney W. Grimes #include <netinet/ip.h> 61df8bae1dSRodney W. Grimes #include <netinet/in_pcb.h> 62df8bae1dSRodney W. Grimes #include <netinet/in_var.h> 63df8bae1dSRodney W. Grimes #include <netinet/ip_var.h> 64ef39adf0SAndre Oppermann #include <netinet/ip_options.h> 65df8bae1dSRodney W. Grimes 66b2630c29SGeorge V. Neville-Neil #ifdef IPSEC 672cb64cb2SGeorge V. Neville-Neil #include <netinet/ip_ipsec.h> 681dfcf0d2SAndre Oppermann #include <netipsec/ipsec.h> 69b2630c29SGeorge V. Neville-Neil #endif /* IPSEC*/ 706a800098SYoshinobu Inoue 711dfcf0d2SAndre Oppermann #include <machine/in_cksum.h> 721dfcf0d2SAndre Oppermann 73aed55708SRobert Watson #include <security/mac/mac_framework.h> 74aed55708SRobert Watson 752b25acc1SLuigi Rizzo #define print_ip(x, a, y) printf("%s %d.%d.%d.%d%s",\ 762b25acc1SLuigi Rizzo x, (ntohl(a.s_addr)>>24)&0xFF,\ 77f9e354dfSJulian Elischer (ntohl(a.s_addr)>>16)&0xFF,\ 78f9e354dfSJulian Elischer (ntohl(a.s_addr)>>8)&0xFF,\ 792b25acc1SLuigi Rizzo (ntohl(a.s_addr))&0xFF, y); 80f9e354dfSJulian Elischer 81f23b4c91SGarrett Wollman u_short ip_id; 82f23b4c91SGarrett Wollman 8353dcc544SMike Silbersack #ifdef MBUF_STRESS_TEST 849d9edc56SMike Silbersack int mbuf_frag_size = 0; 859d9edc56SMike Silbersack SYSCTL_INT(_net_inet_ip, OID_AUTO, mbuf_frag_size, CTLFLAG_RW, 869d9edc56SMike Silbersack &mbuf_frag_size, 0, "Fragment outgoing mbufs to this size"); 879d9edc56SMike Silbersack #endif 889d9edc56SMike Silbersack 89df8bae1dSRodney W. Grimes static void ip_mloopback 904d77a549SAlfred Perlstein (struct ifnet *, struct mbuf *, struct sockaddr_in *, int); 91afed1b49SDarren Reed 92afed1b49SDarren Reed 9393e0e116SJulian Elischer extern struct protosw inetsw[]; 9493e0e116SJulian Elischer 95df8bae1dSRodney W. Grimes /* 96df8bae1dSRodney W. Grimes * IP output. The packet in mbuf chain m contains a skeletal IP 97df8bae1dSRodney W. Grimes * header (with len, off, ttl, proto, tos, src, dst). 98df8bae1dSRodney W. Grimes * The mbuf chain containing the packet will be freed. 99df8bae1dSRodney W. Grimes * The mbuf opt, if present, will not be freed. 1003de758d3SRobert Watson * In the IP forwarding case, the packet will arrive with options already 1013de758d3SRobert Watson * inserted, so must have a NULL opt pointer. 102df8bae1dSRodney W. Grimes */ 103df8bae1dSRodney W. Grimes int 104f2565d68SRobert Watson ip_output(struct mbuf *m, struct mbuf *opt, struct route *ro, int flags, 105f2565d68SRobert Watson struct ip_moptions *imo, struct inpcb *inp) 106df8bae1dSRodney W. Grimes { 1071e78ac21SJeffrey Hsu struct ip *ip; 1082b25acc1SLuigi Rizzo struct ifnet *ifp = NULL; /* keep compiler happy */ 109ac9d7e26SMax Laier struct mbuf *m0; 11023bf9953SPoul-Henning Kamp int hlen = sizeof (struct ip); 1113ae2ad08SJohn-Mark Gurney int mtu; 1129b932e9eSAndre Oppermann int len, error = 0; 1132b25acc1SLuigi Rizzo struct sockaddr_in *dst = NULL; /* keep compiler happy */ 1143956b023SLuigi Rizzo struct in_ifaddr *ia = NULL; 115db4f9cc7SJonathan Lemon int isbroadcast, sw_csum; 116e3f406b3SRuslan Ermilov struct route iproute; 1179b932e9eSAndre Oppermann struct in_addr odst; 1189b932e9eSAndre Oppermann #ifdef IPFIREWALL_FORWARD 1199b932e9eSAndre Oppermann struct m_tag *fwd_tag = NULL; 1209b932e9eSAndre Oppermann #endif 121ac9d7e26SMax Laier M_ASSERTPKTHDR(m); 12236e8826fSMax Laier 12302c1c707SAndre Oppermann if (ro == NULL) { 12402c1c707SAndre Oppermann ro = &iproute; 12502c1c707SAndre Oppermann bzero(ro, sizeof (*ro)); 12602c1c707SAndre Oppermann } 12702c1c707SAndre Oppermann 12884843845SSam Leffler if (inp != NULL) 12984843845SSam Leffler INP_LOCK_ASSERT(inp); 1302b25acc1SLuigi Rizzo 131df8bae1dSRodney W. Grimes if (opt) { 132cb7641e8SMaxim Konovalov len = 0; 133df8bae1dSRodney W. Grimes m = ip_insertoptions(m, opt, &len); 134cb7641e8SMaxim Konovalov if (len != 0) 135df8bae1dSRodney W. Grimes hlen = len; 136df8bae1dSRodney W. Grimes } 137df8bae1dSRodney W. Grimes ip = mtod(m, struct ip *); 1383efc3014SJulian Elischer 139df8bae1dSRodney W. Grimes /* 1406e49b1feSGarrett Wollman * Fill in IP header. If we are not allowing fragmentation, 141e0f630eaSAndre Oppermann * then the ip_id field is meaningless, but we don't set it 142e0f630eaSAndre Oppermann * to zero. Doing so causes various problems when devices along 143e0f630eaSAndre Oppermann * the path (routers, load balancers, firewalls, etc.) illegally 144e0f630eaSAndre Oppermann * disable DF on our packet. Note that a 16-bit counter 1456e49b1feSGarrett Wollman * will wrap around in less than 10 seconds at 100 Mbit/s on a 1466e49b1feSGarrett Wollman * medium with MTU 1500. See Steven M. Bellovin, "A Technique 1476e49b1feSGarrett Wollman * for Counting NATted Hosts", Proc. IMW'02, available at 1484d09f5a0SGleb Smirnoff * <http://www.cs.columbia.edu/~smb/papers/fnat.pdf>. 149df8bae1dSRodney W. Grimes */ 150df8bae1dSRodney W. Grimes if ((flags & (IP_FORWARDING|IP_RAWOUTPUT)) == 0) { 15153be11f6SPoul-Henning Kamp ip->ip_v = IPVERSION; 15253be11f6SPoul-Henning Kamp ip->ip_hl = hlen >> 2; 1531f44b0a1SDavid Malone ip->ip_id = ip_newid(); 154df8bae1dSRodney W. Grimes ipstat.ips_localout++; 155df8bae1dSRodney W. Grimes } else { 15653be11f6SPoul-Henning Kamp hlen = ip->ip_hl << 2; 157df8bae1dSRodney W. Grimes } 1589c9137eaSGarrett Wollman 159df8bae1dSRodney W. Grimes dst = (struct sockaddr_in *)&ro->ro_dst; 1609b932e9eSAndre Oppermann again: 161df8bae1dSRodney W. Grimes /* 162df8bae1dSRodney W. Grimes * If there is a cached route, 163df8bae1dSRodney W. Grimes * check that it is to the same destination 164df8bae1dSRodney W. Grimes * and is still up. If not, free it and try again. 165a4a6e773SHajimu UMEMOTO * The address family should also be checked in case of sharing the 166a4a6e773SHajimu UMEMOTO * cache with IPv6. 167df8bae1dSRodney W. Grimes */ 168df8bae1dSRodney W. Grimes if (ro->ro_rt && ((ro->ro_rt->rt_flags & RTF_UP) == 0 || 169a4a6e773SHajimu UMEMOTO dst->sin_family != AF_INET || 1709b932e9eSAndre Oppermann dst->sin_addr.s_addr != ip->ip_dst.s_addr)) { 171df8bae1dSRodney W. Grimes RTFREE(ro->ro_rt); 1723ae2ad08SJohn-Mark Gurney ro->ro_rt = (struct rtentry *)NULL; 173df8bae1dSRodney W. Grimes } 1749b932e9eSAndre Oppermann #ifdef IPFIREWALL_FORWARD 1759b932e9eSAndre Oppermann if (ro->ro_rt == NULL && fwd_tag == NULL) { 1769b932e9eSAndre Oppermann #else 1770b17fba7SAndre Oppermann if (ro->ro_rt == NULL) { 1789b932e9eSAndre Oppermann #endif 179a4a6e773SHajimu UMEMOTO bzero(dst, sizeof(*dst)); 180df8bae1dSRodney W. Grimes dst->sin_family = AF_INET; 181df8bae1dSRodney W. Grimes dst->sin_len = sizeof(*dst); 1829b932e9eSAndre Oppermann dst->sin_addr = ip->ip_dst; 183df8bae1dSRodney W. Grimes } 184df8bae1dSRodney W. Grimes /* 185a3fd02d8SBruce M Simpson * If routing to interface only, short circuit routing lookup. 186a3fd02d8SBruce M Simpson * The use of an all-ones broadcast address implies this; an 187a3fd02d8SBruce M Simpson * interface is specified by the broadcast address of an interface, 188a3fd02d8SBruce M Simpson * or the destination address of a ptp interface. 189df8bae1dSRodney W. Grimes */ 190a3fd02d8SBruce M Simpson if (flags & IP_SENDONES) { 191a3fd02d8SBruce M Simpson if ((ia = ifatoia(ifa_ifwithbroadaddr(sintosa(dst)))) == NULL && 192a3fd02d8SBruce M Simpson (ia = ifatoia(ifa_ifwithdstaddr(sintosa(dst)))) == NULL) { 193a3fd02d8SBruce M Simpson ipstat.ips_noroute++; 194a3fd02d8SBruce M Simpson error = ENETUNREACH; 195a3fd02d8SBruce M Simpson goto bad; 196a3fd02d8SBruce M Simpson } 197a3fd02d8SBruce M Simpson ip->ip_dst.s_addr = INADDR_BROADCAST; 198a3fd02d8SBruce M Simpson dst->sin_addr = ip->ip_dst; 199a3fd02d8SBruce M Simpson ifp = ia->ia_ifp; 200a3fd02d8SBruce M Simpson ip->ip_ttl = 1; 201a3fd02d8SBruce M Simpson isbroadcast = 1; 202a3fd02d8SBruce M Simpson } else if (flags & IP_ROUTETOIF) { 2030b17fba7SAndre Oppermann if ((ia = ifatoia(ifa_ifwithdstaddr(sintosa(dst)))) == NULL && 2040b17fba7SAndre Oppermann (ia = ifatoia(ifa_ifwithnet(sintosa(dst)))) == NULL) { 205df8bae1dSRodney W. Grimes ipstat.ips_noroute++; 206df8bae1dSRodney W. Grimes error = ENETUNREACH; 207df8bae1dSRodney W. Grimes goto bad; 208df8bae1dSRodney W. Grimes } 209df8bae1dSRodney W. Grimes ifp = ia->ia_ifp; 210df8bae1dSRodney W. Grimes ip->ip_ttl = 1; 2119f9b3dc4SGarrett Wollman isbroadcast = in_broadcast(dst->sin_addr, ifp); 2123afefa39SDaniel C. Sobral } else if (IN_MULTICAST(ntohl(ip->ip_dst.s_addr)) && 21338c1bc35SRuslan Ermilov imo != NULL && imo->imo_multicast_ifp != NULL) { 2143afefa39SDaniel C. Sobral /* 21538c1bc35SRuslan Ermilov * Bypass the normal routing lookup for multicast 21638c1bc35SRuslan Ermilov * packets if the interface is specified. 2173afefa39SDaniel C. Sobral */ 21838c1bc35SRuslan Ermilov ifp = imo->imo_multicast_ifp; 21938c1bc35SRuslan Ermilov IFP_TO_IA(ifp, ia); 22038c1bc35SRuslan Ermilov isbroadcast = 0; /* fool gcc */ 221df8bae1dSRodney W. Grimes } else { 2222c17fe93SGarrett Wollman /* 22397d8d152SAndre Oppermann * We want to do any cloning requested by the link layer, 22497d8d152SAndre Oppermann * as this is probably required in all cases for correct 22597d8d152SAndre Oppermann * operation (as it is for ARP). 2262c17fe93SGarrett Wollman */ 2270b17fba7SAndre Oppermann if (ro->ro_rt == NULL) 228e6e51f05SLuigi Rizzo rtalloc_ign(ro, 0); 2290b17fba7SAndre Oppermann if (ro->ro_rt == NULL) { 230df8bae1dSRodney W. Grimes ipstat.ips_noroute++; 231df8bae1dSRodney W. Grimes error = EHOSTUNREACH; 232df8bae1dSRodney W. Grimes goto bad; 233df8bae1dSRodney W. Grimes } 234df8bae1dSRodney W. Grimes ia = ifatoia(ro->ro_rt->rt_ifa); 235df8bae1dSRodney W. Grimes ifp = ro->ro_rt->rt_ifp; 23697d8d152SAndre Oppermann ro->ro_rt->rt_rmx.rmx_pksent++; 237df8bae1dSRodney W. Grimes if (ro->ro_rt->rt_flags & RTF_GATEWAY) 238f2c2962eSRuslan Ermilov dst = (struct sockaddr_in *)ro->ro_rt->rt_gateway; 2399f9b3dc4SGarrett Wollman if (ro->ro_rt->rt_flags & RTF_HOST) 240f2c2962eSRuslan Ermilov isbroadcast = (ro->ro_rt->rt_flags & RTF_BROADCAST); 2419f9b3dc4SGarrett Wollman else 2429f9b3dc4SGarrett Wollman isbroadcast = in_broadcast(dst->sin_addr, ifp); 243df8bae1dSRodney W. Grimes } 2443ae2ad08SJohn-Mark Gurney /* 2453ae2ad08SJohn-Mark Gurney * Calculate MTU. If we have a route that is up, use that, 2463ae2ad08SJohn-Mark Gurney * otherwise use the interface's MTU. 2473ae2ad08SJohn-Mark Gurney */ 248384a05bfSAndre Oppermann if (ro->ro_rt != NULL && (ro->ro_rt->rt_flags & (RTF_UP|RTF_HOST))) { 2493ae2ad08SJohn-Mark Gurney /* 2503ae2ad08SJohn-Mark Gurney * This case can happen if the user changed the MTU 2513ae2ad08SJohn-Mark Gurney * of an interface after enabling IP on it. Because 2523ae2ad08SJohn-Mark Gurney * most netifs don't keep track of routes pointing to 2533ae2ad08SJohn-Mark Gurney * them, there is no way for one to update all its 2543ae2ad08SJohn-Mark Gurney * routes when the MTU is changed. 2553ae2ad08SJohn-Mark Gurney */ 2563ae2ad08SJohn-Mark Gurney if (ro->ro_rt->rt_rmx.rmx_mtu > ifp->if_mtu) 2573ae2ad08SJohn-Mark Gurney ro->ro_rt->rt_rmx.rmx_mtu = ifp->if_mtu; 2583ae2ad08SJohn-Mark Gurney mtu = ro->ro_rt->rt_rmx.rmx_mtu; 2593ae2ad08SJohn-Mark Gurney } else { 2603ae2ad08SJohn-Mark Gurney mtu = ifp->if_mtu; 2613ae2ad08SJohn-Mark Gurney } 2629b932e9eSAndre Oppermann if (IN_MULTICAST(ntohl(ip->ip_dst.s_addr))) { 263df8bae1dSRodney W. Grimes struct in_multi *inm; 264df8bae1dSRodney W. Grimes 265df8bae1dSRodney W. Grimes m->m_flags |= M_MCAST; 266df8bae1dSRodney W. Grimes /* 267df8bae1dSRodney W. Grimes * IP destination address is multicast. Make sure "dst" 268df8bae1dSRodney W. Grimes * still points to the address in "ro". (It may have been 269df8bae1dSRodney W. Grimes * changed to point to a gateway address, above.) 270df8bae1dSRodney W. Grimes */ 271df8bae1dSRodney W. Grimes dst = (struct sockaddr_in *)&ro->ro_dst; 272df8bae1dSRodney W. Grimes /* 273df8bae1dSRodney W. Grimes * See if the caller provided any multicast options 274df8bae1dSRodney W. Grimes */ 275df8bae1dSRodney W. Grimes if (imo != NULL) { 276df8bae1dSRodney W. Grimes ip->ip_ttl = imo->imo_multicast_ttl; 2771c5de19aSGarrett Wollman if (imo->imo_multicast_vif != -1) 2781c5de19aSGarrett Wollman ip->ip_src.s_addr = 279bbb4330bSLuigi Rizzo ip_mcast_src ? 280bbb4330bSLuigi Rizzo ip_mcast_src(imo->imo_multicast_vif) : 281bbb4330bSLuigi Rizzo INADDR_ANY; 282df8bae1dSRodney W. Grimes } else 283df8bae1dSRodney W. Grimes ip->ip_ttl = IP_DEFAULT_MULTICAST_TTL; 284df8bae1dSRodney W. Grimes /* 285df8bae1dSRodney W. Grimes * Confirm that the outgoing interface supports multicast. 286df8bae1dSRodney W. Grimes */ 2871c5de19aSGarrett Wollman if ((imo == NULL) || (imo->imo_multicast_vif == -1)) { 288df8bae1dSRodney W. Grimes if ((ifp->if_flags & IFF_MULTICAST) == 0) { 289df8bae1dSRodney W. Grimes ipstat.ips_noroute++; 290df8bae1dSRodney W. Grimes error = ENETUNREACH; 291df8bae1dSRodney W. Grimes goto bad; 292df8bae1dSRodney W. Grimes } 2931c5de19aSGarrett Wollman } 294df8bae1dSRodney W. Grimes /* 295df8bae1dSRodney W. Grimes * If source address not specified yet, use address 296df8bae1dSRodney W. Grimes * of outgoing interface. 297df8bae1dSRodney W. Grimes */ 298df8bae1dSRodney W. Grimes if (ip->ip_src.s_addr == INADDR_ANY) { 29938c1bc35SRuslan Ermilov /* Interface may have no addresses. */ 30038c1bc35SRuslan Ermilov if (ia != NULL) 30138c1bc35SRuslan Ermilov ip->ip_src = IA_SIN(ia)->sin_addr; 302df8bae1dSRodney W. Grimes } 303df8bae1dSRodney W. Grimes 304dd5a318bSRobert Watson IN_MULTI_LOCK(); 3059b932e9eSAndre Oppermann IN_LOOKUP_MULTI(ip->ip_dst, ifp, inm); 306df8bae1dSRodney W. Grimes if (inm != NULL && 307df8bae1dSRodney W. Grimes (imo == NULL || imo->imo_multicast_loop)) { 308dd5a318bSRobert Watson IN_MULTI_UNLOCK(); 309df8bae1dSRodney W. Grimes /* 310df8bae1dSRodney W. Grimes * If we belong to the destination multicast group 311df8bae1dSRodney W. Grimes * on the outgoing interface, and the caller did not 312df8bae1dSRodney W. Grimes * forbid loopback, loop back a copy. 313df8bae1dSRodney W. Grimes */ 31486b1d6d2SBill Fenner ip_mloopback(ifp, m, dst, hlen); 315df8bae1dSRodney W. Grimes } 316df8bae1dSRodney W. Grimes else { 317dd5a318bSRobert Watson IN_MULTI_UNLOCK(); 318df8bae1dSRodney W. Grimes /* 319df8bae1dSRodney W. Grimes * If we are acting as a multicast router, perform 320df8bae1dSRodney W. Grimes * multicast forwarding as if the packet had just 321df8bae1dSRodney W. Grimes * arrived on the interface to which we are about 322df8bae1dSRodney W. Grimes * to send. The multicast forwarding function 323df8bae1dSRodney W. Grimes * recursively calls this function, using the 324df8bae1dSRodney W. Grimes * IP_FORWARDING flag to prevent infinite recursion. 325df8bae1dSRodney W. Grimes * 326df8bae1dSRodney W. Grimes * Multicasts that are looped back by ip_mloopback(), 327df8bae1dSRodney W. Grimes * above, will be forwarded by the ip_input() routine, 328df8bae1dSRodney W. Grimes * if necessary. 329df8bae1dSRodney W. Grimes */ 330df8bae1dSRodney W. Grimes if (ip_mrouter && (flags & IP_FORWARDING) == 0) { 331f0068c4aSGarrett Wollman /* 332bbb4330bSLuigi Rizzo * If rsvp daemon is not running, do not 333f0068c4aSGarrett Wollman * set ip_moptions. This ensures that the packet 334f0068c4aSGarrett Wollman * is multicast and not just sent down one link 335f0068c4aSGarrett Wollman * as prescribed by rsvpd. 336f0068c4aSGarrett Wollman */ 337b124e4f2SGarrett Wollman if (!rsvp_on) 338f0068c4aSGarrett Wollman imo = NULL; 339bbb4330bSLuigi Rizzo if (ip_mforward && 340bbb4330bSLuigi Rizzo ip_mforward(ip, ifp, m, imo) != 0) { 341df8bae1dSRodney W. Grimes m_freem(m); 342df8bae1dSRodney W. Grimes goto done; 343df8bae1dSRodney W. Grimes } 344df8bae1dSRodney W. Grimes } 345df8bae1dSRodney W. Grimes } 3465e9ae478SGarrett Wollman 347df8bae1dSRodney W. Grimes /* 348df8bae1dSRodney W. Grimes * Multicasts with a time-to-live of zero may be looped- 349df8bae1dSRodney W. Grimes * back, above, but must not be transmitted on a network. 350df8bae1dSRodney W. Grimes * Also, multicasts addressed to the loopback interface 351df8bae1dSRodney W. Grimes * are not sent -- the above call to ip_mloopback() will 352df8bae1dSRodney W. Grimes * loop back a copy if this host actually belongs to the 353df8bae1dSRodney W. Grimes * destination group on the loopback interface. 354df8bae1dSRodney W. Grimes */ 355f5fea3ddSPaul Traina if (ip->ip_ttl == 0 || ifp->if_flags & IFF_LOOPBACK) { 356df8bae1dSRodney W. Grimes m_freem(m); 357df8bae1dSRodney W. Grimes goto done; 358df8bae1dSRodney W. Grimes } 359df8bae1dSRodney W. Grimes 360df8bae1dSRodney W. Grimes goto sendit; 361df8bae1dSRodney W. Grimes } 3626a7c943cSAndre Oppermann 363df8bae1dSRodney W. Grimes /* 3642b25acc1SLuigi Rizzo * If the source address is not specified yet, use the address 365cb459254SJohn-Mark Gurney * of the outoing interface. 366df8bae1dSRodney W. Grimes */ 367f9e354dfSJulian Elischer if (ip->ip_src.s_addr == INADDR_ANY) { 36838c1bc35SRuslan Ermilov /* Interface may have no addresses. */ 36938c1bc35SRuslan Ermilov if (ia != NULL) { 370df8bae1dSRodney W. Grimes ip->ip_src = IA_SIN(ia)->sin_addr; 371f9e354dfSJulian Elischer } 37238c1bc35SRuslan Ermilov } 3736a7c943cSAndre Oppermann 374ca7a789aSMax Laier /* 375ca7a789aSMax Laier * Verify that we have any chance at all of being able to queue the 376ca7a789aSMax Laier * packet or packet fragments, unless ALTQ is enabled on the given 377ca7a789aSMax Laier * interface in which case packetdrop should be done by queueing. 378ca7a789aSMax Laier */ 37902b199f1SMax Laier #ifdef ALTQ 380ca7a789aSMax Laier if ((!ALTQ_IS_ENABLED(&ifp->if_snd)) && 3813ae2ad08SJohn-Mark Gurney ((ifp->if_snd.ifq_len + ip->ip_len / mtu + 1) >= 382ca7a789aSMax Laier ifp->if_snd.ifq_maxlen)) 383ca7a789aSMax Laier #else 3843ae2ad08SJohn-Mark Gurney if ((ifp->if_snd.ifq_len + ip->ip_len / mtu + 1) >= 385ca7a789aSMax Laier ifp->if_snd.ifq_maxlen) 386ca7a789aSMax Laier #endif /* ALTQ */ 387ca7a789aSMax Laier { 388b5390296SDavid Greenman error = ENOBUFS; 38935609d45SJonathan Lemon ipstat.ips_odropped++; 390bbce982bSGleb Smirnoff ifp->if_snd.ifq_drops += (ip->ip_len / ifp->if_mtu + 1); 391b5390296SDavid Greenman goto bad; 392b5390296SDavid Greenman } 393b5390296SDavid Greenman 394b5390296SDavid Greenman /* 395df8bae1dSRodney W. Grimes * Look for broadcast address and 3968c3f5566SRuslan Ermilov * verify user is allowed to send 397df8bae1dSRodney W. Grimes * such a packet. 398df8bae1dSRodney W. Grimes */ 3999f9b3dc4SGarrett Wollman if (isbroadcast) { 400df8bae1dSRodney W. Grimes if ((ifp->if_flags & IFF_BROADCAST) == 0) { 401df8bae1dSRodney W. Grimes error = EADDRNOTAVAIL; 402df8bae1dSRodney W. Grimes goto bad; 403df8bae1dSRodney W. Grimes } 404df8bae1dSRodney W. Grimes if ((flags & IP_ALLOWBROADCAST) == 0) { 405df8bae1dSRodney W. Grimes error = EACCES; 406df8bae1dSRodney W. Grimes goto bad; 407df8bae1dSRodney W. Grimes } 408df8bae1dSRodney W. Grimes /* don't allow broadcast messages to be fragmented */ 4093ae2ad08SJohn-Mark Gurney if (ip->ip_len > mtu) { 410df8bae1dSRodney W. Grimes error = EMSGSIZE; 411df8bae1dSRodney W. Grimes goto bad; 412df8bae1dSRodney W. Grimes } 413df8bae1dSRodney W. Grimes m->m_flags |= M_BCAST; 4149f9b3dc4SGarrett Wollman } else { 415df8bae1dSRodney W. Grimes m->m_flags &= ~M_BCAST; 4169f9b3dc4SGarrett Wollman } 417df8bae1dSRodney W. Grimes 418f1743588SDarren Reed sendit: 419b2630c29SGeorge V. Neville-Neil #ifdef IPSEC 4201dfcf0d2SAndre Oppermann switch(ip_ipsec_output(&m, inp, &flags, &error, &ro, &iproute, &dst, &ia, &ifp)) { 4211dfcf0d2SAndre Oppermann case 1: 42233841545SHajimu UMEMOTO goto bad; 4231dfcf0d2SAndre Oppermann case -1: 4241dfcf0d2SAndre Oppermann goto done; 4251dfcf0d2SAndre Oppermann case 0: 42633841545SHajimu UMEMOTO default: 4271dfcf0d2SAndre Oppermann break; /* Continue with packet processing. */ 42833841545SHajimu UMEMOTO } 4291dfcf0d2SAndre Oppermann /* Update variables that are affected by ipsec4_output(). */ 43033841545SHajimu UMEMOTO ip = mtod(m, struct ip *); 43133841545SHajimu UMEMOTO hlen = ip->ip_hl << 2; 432b2630c29SGeorge V. Neville-Neil #endif /* IPSEC */ 43333841545SHajimu UMEMOTO 434c21fd232SAndre Oppermann /* Jump over all PFIL processing if hooks are not active. */ 435604afec4SChristian S.J. Peron if (!PFIL_HOOKED(&inet_pfil_hook)) 436c21fd232SAndre Oppermann goto passout; 437c21fd232SAndre Oppermann 438c21fd232SAndre Oppermann /* Run through list of hooks for output packets. */ 4399b932e9eSAndre Oppermann odst.s_addr = ip->ip_dst.s_addr; 440d6a8d588SMax Laier error = pfil_run_hooks(&inet_pfil_hook, &m, ifp, PFIL_OUT, inp); 441134ea224SSam Leffler if (error != 0 || m == NULL) 442c4ac87eaSDarren Reed goto done; 4439b932e9eSAndre Oppermann 444c4ac87eaSDarren Reed ip = mtod(m, struct ip *); 445fed1c7e9SSøren Schmidt 4469b932e9eSAndre Oppermann /* See if destination IP address was changed by packet filter. */ 4479b932e9eSAndre Oppermann if (odst.s_addr != ip->ip_dst.s_addr) { 4489b932e9eSAndre Oppermann m->m_flags |= M_SKIP_FIREWALL; 449f4fca2d8SAndre Oppermann /* If destination is now ourself drop to ip_input(). */ 4509b932e9eSAndre Oppermann if (in_localip(ip->ip_dst)) { 4519b932e9eSAndre Oppermann m->m_flags |= M_FASTFWD_OURS; 452f9e354dfSJulian Elischer if (m->m_pkthdr.rcvif == NULL) 453a9c92b54SAndre Oppermann m->m_pkthdr.rcvif = loif; 45420c822f3SJonathan Lemon if (m->m_pkthdr.csum_flags & CSUM_DELAY_DATA) { 45520c822f3SJonathan Lemon m->m_pkthdr.csum_flags |= 45620c822f3SJonathan Lemon CSUM_DATA_VALID | CSUM_PSEUDO_HDR; 457ac9d7e26SMax Laier m->m_pkthdr.csum_data = 0xffff; 45820c822f3SJonathan Lemon } 45920c822f3SJonathan Lemon m->m_pkthdr.csum_flags |= 46020c822f3SJonathan Lemon CSUM_IP_CHECKED | CSUM_IP_VALID; 4619b932e9eSAndre Oppermann 4629b932e9eSAndre Oppermann error = netisr_queue(NETISR_IP, m); 4639b932e9eSAndre Oppermann goto done; 4649b932e9eSAndre Oppermann } else 465f4fca2d8SAndre Oppermann goto again; /* Redo the routing table lookup. */ 4669b932e9eSAndre Oppermann } 4679b932e9eSAndre Oppermann 4689b932e9eSAndre Oppermann #ifdef IPFIREWALL_FORWARD 4699b932e9eSAndre Oppermann /* See if local, if yes, send it to netisr with IP_FASTFWD_OURS. */ 4709b932e9eSAndre Oppermann if (m->m_flags & M_FASTFWD_OURS) { 4719b932e9eSAndre Oppermann if (m->m_pkthdr.rcvif == NULL) 472a9c92b54SAndre Oppermann m->m_pkthdr.rcvif = loif; 4739b932e9eSAndre Oppermann if (m->m_pkthdr.csum_flags & CSUM_DELAY_DATA) { 4749b932e9eSAndre Oppermann m->m_pkthdr.csum_flags |= 4759b932e9eSAndre Oppermann CSUM_DATA_VALID | CSUM_PSEUDO_HDR; 4769b932e9eSAndre Oppermann m->m_pkthdr.csum_data = 0xffff; 4779b932e9eSAndre Oppermann } 4789b932e9eSAndre Oppermann m->m_pkthdr.csum_flags |= 4799b932e9eSAndre Oppermann CSUM_IP_CHECKED | CSUM_IP_VALID; 4809b932e9eSAndre Oppermann 4819b932e9eSAndre Oppermann error = netisr_queue(NETISR_IP, m); 482f9e354dfSJulian Elischer goto done; 483f9e354dfSJulian Elischer } 4849b932e9eSAndre Oppermann /* Or forward to some other address? */ 4859b932e9eSAndre Oppermann fwd_tag = m_tag_find(m, PACKET_TAG_IPFORWARD, NULL); 4869b932e9eSAndre Oppermann if (fwd_tag) { 4879b932e9eSAndre Oppermann dst = (struct sockaddr_in *)&ro->ro_dst; 4889b932e9eSAndre Oppermann bcopy((fwd_tag+1), dst, sizeof(struct sockaddr_in)); 4899b932e9eSAndre Oppermann m->m_flags |= M_SKIP_FIREWALL; 4909b932e9eSAndre Oppermann m_tag_delete(m, fwd_tag); 4919b932e9eSAndre Oppermann goto again; 492099dd043SAndre Oppermann } 493099dd043SAndre Oppermann #endif /* IPFIREWALL_FORWARD */ 4942b25acc1SLuigi Rizzo 495c21fd232SAndre Oppermann passout: 49651c8ec4aSRuslan Ermilov /* 127/8 must not appear on wire - RFC1122. */ 49751c8ec4aSRuslan Ermilov if ((ntohl(ip->ip_dst.s_addr) >> IN_CLASSA_NSHIFT) == IN_LOOPBACKNET || 49851c8ec4aSRuslan Ermilov (ntohl(ip->ip_src.s_addr) >> IN_CLASSA_NSHIFT) == IN_LOOPBACKNET) { 49951c8ec4aSRuslan Ermilov if ((ifp->if_flags & IFF_LOOPBACK) == 0) { 50051c8ec4aSRuslan Ermilov ipstat.ips_badaddr++; 50151c8ec4aSRuslan Ermilov error = EADDRNOTAVAIL; 50251c8ec4aSRuslan Ermilov goto bad; 50351c8ec4aSRuslan Ermilov } 50451c8ec4aSRuslan Ermilov } 50551c8ec4aSRuslan Ermilov 506206a3274SRuslan Ermilov m->m_pkthdr.csum_flags |= CSUM_IP; 507206a3274SRuslan Ermilov sw_csum = m->m_pkthdr.csum_flags & ~ifp->if_hwassist; 508db4f9cc7SJonathan Lemon if (sw_csum & CSUM_DELAY_DATA) { 509db4f9cc7SJonathan Lemon in_delayed_cksum(m); 510db4f9cc7SJonathan Lemon sw_csum &= ~CSUM_DELAY_DATA; 511db4f9cc7SJonathan Lemon } 512206a3274SRuslan Ermilov m->m_pkthdr.csum_flags &= ifp->if_hwassist; 513db4f9cc7SJonathan Lemon 514e7319babSPoul-Henning Kamp /* 515db4f9cc7SJonathan Lemon * If small enough for interface, or the interface will take 516233dcce1SAndre Oppermann * care of the fragmentation for us, we can just send directly. 517df8bae1dSRodney W. Grimes */ 5183ae2ad08SJohn-Mark Gurney if (ip->ip_len <= mtu || 519233dcce1SAndre Oppermann (m->m_pkthdr.csum_flags & ifp->if_hwassist & CSUM_TSO) != 0 || 520233dcce1SAndre Oppermann ((ip->ip_off & IP_DF) == 0 && (ifp->if_hwassist & CSUM_FRAGMENT))) { 521fd8e4ebcSMike Barcroft ip->ip_len = htons(ip->ip_len); 522fd8e4ebcSMike Barcroft ip->ip_off = htons(ip->ip_off); 523df8bae1dSRodney W. Grimes ip->ip_sum = 0; 52453be11f6SPoul-Henning Kamp if (sw_csum & CSUM_DELAY_IP) 525df8bae1dSRodney W. Grimes ip->ip_sum = in_cksum(m, hlen); 5265da9f8faSJosef Karthauser 527233dcce1SAndre Oppermann /* 528233dcce1SAndre Oppermann * Record statistics for this interface address. 529233dcce1SAndre Oppermann * With CSUM_TSO the byte/packet count will be slightly 530233dcce1SAndre Oppermann * incorrect because we count the IP+TCP headers only 531233dcce1SAndre Oppermann * once instead of for every generated packet. 532233dcce1SAndre Oppermann */ 53338c1bc35SRuslan Ermilov if (!(flags & IP_FORWARDING) && ia) { 534233dcce1SAndre Oppermann if (m->m_pkthdr.csum_flags & CSUM_TSO) 5353dbee59bSBruce M Simpson ia->ia_ifa.if_opackets += 536233dcce1SAndre Oppermann m->m_pkthdr.len / m->m_pkthdr.tso_segsz; 537233dcce1SAndre Oppermann else 5383dbee59bSBruce M Simpson ia->ia_ifa.if_opackets++; 5393dbee59bSBruce M Simpson ia->ia_ifa.if_obytes += m->m_pkthdr.len; 5405da9f8faSJosef Karthauser } 54153dcc544SMike Silbersack #ifdef MBUF_STRESS_TEST 5423390d476SMike Silbersack if (mbuf_frag_size && m->m_pkthdr.len > mbuf_frag_size) 5433390d476SMike Silbersack m = m_fragment(m, M_DONTWAIT, mbuf_frag_size); 5449d9edc56SMike Silbersack #endif 545147f74d1SAndre Oppermann /* 546147f74d1SAndre Oppermann * Reset layer specific mbuf flags 547147f74d1SAndre Oppermann * to avoid confusing lower layers. 548147f74d1SAndre Oppermann */ 549147f74d1SAndre Oppermann m->m_flags &= ~(M_PROTOFLAGS); 550147f74d1SAndre Oppermann 551df8bae1dSRodney W. Grimes error = (*ifp->if_output)(ifp, m, 552df8bae1dSRodney W. Grimes (struct sockaddr *)dst, ro->ro_rt); 553df8bae1dSRodney W. Grimes goto done; 554df8bae1dSRodney W. Grimes } 5551e78ac21SJeffrey Hsu 556233dcce1SAndre Oppermann /* Balk when DF bit is set or the interface didn't support TSO. */ 557233dcce1SAndre Oppermann if ((ip->ip_off & IP_DF) || (m->m_pkthdr.csum_flags & CSUM_TSO)) { 558df8bae1dSRodney W. Grimes error = EMSGSIZE; 559df8bae1dSRodney W. Grimes ipstat.ips_cantfrag++; 560df8bae1dSRodney W. Grimes goto bad; 561df8bae1dSRodney W. Grimes } 5621e78ac21SJeffrey Hsu 5631e78ac21SJeffrey Hsu /* 5641e78ac21SJeffrey Hsu * Too large for interface; fragment if possible. If successful, 5651e78ac21SJeffrey Hsu * on return, m will point to a list of packets to be sent. 5661e78ac21SJeffrey Hsu */ 5673ae2ad08SJohn-Mark Gurney error = ip_fragment(ip, &m, mtu, ifp->if_hwassist, sw_csum); 5681e78ac21SJeffrey Hsu if (error) 569df8bae1dSRodney W. Grimes goto bad; 5701e78ac21SJeffrey Hsu for (; m; m = m0) { 571df8bae1dSRodney W. Grimes m0 = m->m_nextpkt; 572b375c9ecSLuigi Rizzo m->m_nextpkt = 0; 57307203494SDaniel C. Sobral if (error == 0) { 574fe937674SJosef Karthauser /* Record statistics for this interface address. */ 57507203494SDaniel C. Sobral if (ia != NULL) { 5763dbee59bSBruce M Simpson ia->ia_ifa.if_opackets++; 5773dbee59bSBruce M Simpson ia->ia_ifa.if_obytes += m->m_pkthdr.len; 57807203494SDaniel C. Sobral } 579147f74d1SAndre Oppermann /* 580147f74d1SAndre Oppermann * Reset layer specific mbuf flags 581147f74d1SAndre Oppermann * to avoid confusing upper layers. 582147f74d1SAndre Oppermann */ 583147f74d1SAndre Oppermann m->m_flags &= ~(M_PROTOFLAGS); 584fe937674SJosef Karthauser 585df8bae1dSRodney W. Grimes error = (*ifp->if_output)(ifp, m, 586df8bae1dSRodney W. Grimes (struct sockaddr *)dst, ro->ro_rt); 587fe937674SJosef Karthauser } else 588df8bae1dSRodney W. Grimes m_freem(m); 589df8bae1dSRodney W. Grimes } 590df8bae1dSRodney W. Grimes 591df8bae1dSRodney W. Grimes if (error == 0) 592df8bae1dSRodney W. Grimes ipstat.ips_fragmented++; 5931e78ac21SJeffrey Hsu 594df8bae1dSRodney W. Grimes done: 5956a800098SYoshinobu Inoue if (ro == &iproute && ro->ro_rt) { 5966a800098SYoshinobu Inoue RTFREE(ro->ro_rt); 597ac9d7e26SMax Laier } 598df8bae1dSRodney W. Grimes return (error); 599df8bae1dSRodney W. Grimes bad: 6003528d68fSBill Paul m_freem(m); 601df8bae1dSRodney W. Grimes goto done; 602df8bae1dSRodney W. Grimes } 603df8bae1dSRodney W. Grimes 6041e78ac21SJeffrey Hsu /* 6051e78ac21SJeffrey Hsu * Create a chain of fragments which fit the given mtu. m_frag points to the 6061e78ac21SJeffrey Hsu * mbuf to be fragmented; on return it points to the chain with the fragments. 6071e78ac21SJeffrey Hsu * Return 0 if no error. If error, m_frag may contain a partially built 6081e78ac21SJeffrey Hsu * chain of fragments that should be freed by the caller. 6091e78ac21SJeffrey Hsu * 6101e78ac21SJeffrey Hsu * if_hwassist_flags is the hw offload capabilities (see if_data.ifi_hwassist) 6111e78ac21SJeffrey Hsu * sw_csum contains the delayed checksums flags (e.g., CSUM_DELAY_IP). 6121e78ac21SJeffrey Hsu */ 6131e78ac21SJeffrey Hsu int 6141e78ac21SJeffrey Hsu ip_fragment(struct ip *ip, struct mbuf **m_frag, int mtu, 6151e78ac21SJeffrey Hsu u_long if_hwassist_flags, int sw_csum) 6161e78ac21SJeffrey Hsu { 6171e78ac21SJeffrey Hsu int error = 0; 6181e78ac21SJeffrey Hsu int hlen = ip->ip_hl << 2; 6191e78ac21SJeffrey Hsu int len = (mtu - hlen) & ~7; /* size of payload in each fragment */ 6201e78ac21SJeffrey Hsu int off; 6211e78ac21SJeffrey Hsu struct mbuf *m0 = *m_frag; /* the original packet */ 6221e78ac21SJeffrey Hsu int firstlen; 6231e78ac21SJeffrey Hsu struct mbuf **mnext; 6241e78ac21SJeffrey Hsu int nfrags; 6251e78ac21SJeffrey Hsu 6261e78ac21SJeffrey Hsu if (ip->ip_off & IP_DF) { /* Fragmentation not allowed */ 6271e78ac21SJeffrey Hsu ipstat.ips_cantfrag++; 6281e78ac21SJeffrey Hsu return EMSGSIZE; 6291e78ac21SJeffrey Hsu } 6301e78ac21SJeffrey Hsu 6311e78ac21SJeffrey Hsu /* 6321e78ac21SJeffrey Hsu * Must be able to put at least 8 bytes per fragment. 6331e78ac21SJeffrey Hsu */ 6341e78ac21SJeffrey Hsu if (len < 8) 6351e78ac21SJeffrey Hsu return EMSGSIZE; 6361e78ac21SJeffrey Hsu 6371e78ac21SJeffrey Hsu /* 6381e78ac21SJeffrey Hsu * If the interface will not calculate checksums on 6391e78ac21SJeffrey Hsu * fragmented packets, then do it here. 6401e78ac21SJeffrey Hsu */ 6411e78ac21SJeffrey Hsu if (m0->m_pkthdr.csum_flags & CSUM_DELAY_DATA && 6421e78ac21SJeffrey Hsu (if_hwassist_flags & CSUM_IP_FRAGS) == 0) { 6431e78ac21SJeffrey Hsu in_delayed_cksum(m0); 6441e78ac21SJeffrey Hsu m0->m_pkthdr.csum_flags &= ~CSUM_DELAY_DATA; 6451e78ac21SJeffrey Hsu } 6461e78ac21SJeffrey Hsu 6471e78ac21SJeffrey Hsu if (len > PAGE_SIZE) { 6481e78ac21SJeffrey Hsu /* 6491e78ac21SJeffrey Hsu * Fragment large datagrams such that each segment 6501e78ac21SJeffrey Hsu * contains a multiple of PAGE_SIZE amount of data, 6511e78ac21SJeffrey Hsu * plus headers. This enables a receiver to perform 6521e78ac21SJeffrey Hsu * page-flipping zero-copy optimizations. 6531e78ac21SJeffrey Hsu * 6541e78ac21SJeffrey Hsu * XXX When does this help given that sender and receiver 6551e78ac21SJeffrey Hsu * could have different page sizes, and also mtu could 6561e78ac21SJeffrey Hsu * be less than the receiver's page size ? 6571e78ac21SJeffrey Hsu */ 6581e78ac21SJeffrey Hsu int newlen; 6591e78ac21SJeffrey Hsu struct mbuf *m; 6601e78ac21SJeffrey Hsu 6611e78ac21SJeffrey Hsu for (m = m0, off = 0; m && (off+m->m_len) <= mtu; m = m->m_next) 6621e78ac21SJeffrey Hsu off += m->m_len; 6631e78ac21SJeffrey Hsu 6641e78ac21SJeffrey Hsu /* 6651e78ac21SJeffrey Hsu * firstlen (off - hlen) must be aligned on an 6661e78ac21SJeffrey Hsu * 8-byte boundary 6671e78ac21SJeffrey Hsu */ 6681e78ac21SJeffrey Hsu if (off < hlen) 6691e78ac21SJeffrey Hsu goto smart_frag_failure; 6701e78ac21SJeffrey Hsu off = ((off - hlen) & ~7) + hlen; 6711e78ac21SJeffrey Hsu newlen = (~PAGE_MASK) & mtu; 6721e78ac21SJeffrey Hsu if ((newlen + sizeof (struct ip)) > mtu) { 6731e78ac21SJeffrey Hsu /* we failed, go back the default */ 6741e78ac21SJeffrey Hsu smart_frag_failure: 6751e78ac21SJeffrey Hsu newlen = len; 6761e78ac21SJeffrey Hsu off = hlen + len; 6771e78ac21SJeffrey Hsu } 6781e78ac21SJeffrey Hsu len = newlen; 6791e78ac21SJeffrey Hsu 6801e78ac21SJeffrey Hsu } else { 6811e78ac21SJeffrey Hsu off = hlen + len; 6821e78ac21SJeffrey Hsu } 6831e78ac21SJeffrey Hsu 6841e78ac21SJeffrey Hsu firstlen = off - hlen; 6851e78ac21SJeffrey Hsu mnext = &m0->m_nextpkt; /* pointer to next packet */ 6861e78ac21SJeffrey Hsu 6871e78ac21SJeffrey Hsu /* 6881e78ac21SJeffrey Hsu * Loop through length of segment after first fragment, 6891e78ac21SJeffrey Hsu * make new header and copy data of each part and link onto chain. 6901e78ac21SJeffrey Hsu * Here, m0 is the original packet, m is the fragment being created. 6911e78ac21SJeffrey Hsu * The fragments are linked off the m_nextpkt of the original 6921e78ac21SJeffrey Hsu * packet, which after processing serves as the first fragment. 6931e78ac21SJeffrey Hsu */ 6941e78ac21SJeffrey Hsu for (nfrags = 1; off < ip->ip_len; off += len, nfrags++) { 6951e78ac21SJeffrey Hsu struct ip *mhip; /* ip header on the fragment */ 6961e78ac21SJeffrey Hsu struct mbuf *m; 6971e78ac21SJeffrey Hsu int mhlen = sizeof (struct ip); 6981e78ac21SJeffrey Hsu 69934333b16SAndre Oppermann MGETHDR(m, M_DONTWAIT, MT_DATA); 7000b17fba7SAndre Oppermann if (m == NULL) { 7011e78ac21SJeffrey Hsu error = ENOBUFS; 7021e78ac21SJeffrey Hsu ipstat.ips_odropped++; 7031e78ac21SJeffrey Hsu goto done; 7041e78ac21SJeffrey Hsu } 7051e78ac21SJeffrey Hsu m->m_flags |= (m0->m_flags & M_MCAST) | M_FRAG; 7061e78ac21SJeffrey Hsu /* 7071e78ac21SJeffrey Hsu * In the first mbuf, leave room for the link header, then 7081e78ac21SJeffrey Hsu * copy the original IP header including options. The payload 7091e78ac21SJeffrey Hsu * goes into an additional mbuf chain returned by m_copy(). 7101e78ac21SJeffrey Hsu */ 7111e78ac21SJeffrey Hsu m->m_data += max_linkhdr; 7121e78ac21SJeffrey Hsu mhip = mtod(m, struct ip *); 7131e78ac21SJeffrey Hsu *mhip = *ip; 7141e78ac21SJeffrey Hsu if (hlen > sizeof (struct ip)) { 7151e78ac21SJeffrey Hsu mhlen = ip_optcopy(ip, mhip) + sizeof (struct ip); 7161e78ac21SJeffrey Hsu mhip->ip_v = IPVERSION; 7171e78ac21SJeffrey Hsu mhip->ip_hl = mhlen >> 2; 7181e78ac21SJeffrey Hsu } 7191e78ac21SJeffrey Hsu m->m_len = mhlen; 7201e78ac21SJeffrey Hsu /* XXX do we need to add ip->ip_off below ? */ 7211e78ac21SJeffrey Hsu mhip->ip_off = ((off - hlen) >> 3) + ip->ip_off; 7221e78ac21SJeffrey Hsu if (off + len >= ip->ip_len) { /* last fragment */ 7231e78ac21SJeffrey Hsu len = ip->ip_len - off; 7241e78ac21SJeffrey Hsu m->m_flags |= M_LASTFRAG; 7251e78ac21SJeffrey Hsu } else 7261e78ac21SJeffrey Hsu mhip->ip_off |= IP_MF; 7271e78ac21SJeffrey Hsu mhip->ip_len = htons((u_short)(len + mhlen)); 7281e78ac21SJeffrey Hsu m->m_next = m_copy(m0, off, len); 7290b17fba7SAndre Oppermann if (m->m_next == NULL) { /* copy failed */ 7301e78ac21SJeffrey Hsu m_free(m); 7311e78ac21SJeffrey Hsu error = ENOBUFS; /* ??? */ 7321e78ac21SJeffrey Hsu ipstat.ips_odropped++; 7331e78ac21SJeffrey Hsu goto done; 7341e78ac21SJeffrey Hsu } 7351e78ac21SJeffrey Hsu m->m_pkthdr.len = mhlen + len; 736fc74a9f9SBrooks Davis m->m_pkthdr.rcvif = NULL; 7371e78ac21SJeffrey Hsu #ifdef MAC 73830d239bcSRobert Watson mac_netinet_fragment(m0, m); 7391e78ac21SJeffrey Hsu #endif 7401e78ac21SJeffrey Hsu m->m_pkthdr.csum_flags = m0->m_pkthdr.csum_flags; 7411e78ac21SJeffrey Hsu mhip->ip_off = htons(mhip->ip_off); 7421e78ac21SJeffrey Hsu mhip->ip_sum = 0; 7431e78ac21SJeffrey Hsu if (sw_csum & CSUM_DELAY_IP) 7441e78ac21SJeffrey Hsu mhip->ip_sum = in_cksum(m, mhlen); 7451e78ac21SJeffrey Hsu *mnext = m; 7461e78ac21SJeffrey Hsu mnext = &m->m_nextpkt; 7471e78ac21SJeffrey Hsu } 7481e78ac21SJeffrey Hsu ipstat.ips_ofragments += nfrags; 7491e78ac21SJeffrey Hsu 7501e78ac21SJeffrey Hsu /* set first marker for fragment chain */ 7511e78ac21SJeffrey Hsu m0->m_flags |= M_FIRSTFRAG | M_FRAG; 7521e78ac21SJeffrey Hsu m0->m_pkthdr.csum_data = nfrags; 7531e78ac21SJeffrey Hsu 7541e78ac21SJeffrey Hsu /* 7551e78ac21SJeffrey Hsu * Update first fragment by trimming what's been copied out 7561e78ac21SJeffrey Hsu * and updating header. 7571e78ac21SJeffrey Hsu */ 7581e78ac21SJeffrey Hsu m_adj(m0, hlen + firstlen - ip->ip_len); 7591e78ac21SJeffrey Hsu m0->m_pkthdr.len = hlen + firstlen; 7601e78ac21SJeffrey Hsu ip->ip_len = htons((u_short)m0->m_pkthdr.len); 7611e78ac21SJeffrey Hsu ip->ip_off |= IP_MF; 7621e78ac21SJeffrey Hsu ip->ip_off = htons(ip->ip_off); 7631e78ac21SJeffrey Hsu ip->ip_sum = 0; 7641e78ac21SJeffrey Hsu if (sw_csum & CSUM_DELAY_IP) 7651e78ac21SJeffrey Hsu ip->ip_sum = in_cksum(m0, hlen); 7661e78ac21SJeffrey Hsu 7671e78ac21SJeffrey Hsu done: 7681e78ac21SJeffrey Hsu *m_frag = m0; 7691e78ac21SJeffrey Hsu return error; 7701e78ac21SJeffrey Hsu } 7711e78ac21SJeffrey Hsu 7721c238475SJonathan Lemon void 773db4f9cc7SJonathan Lemon in_delayed_cksum(struct mbuf *m) 774db4f9cc7SJonathan Lemon { 775db4f9cc7SJonathan Lemon struct ip *ip; 776db4f9cc7SJonathan Lemon u_short csum, offset; 777db4f9cc7SJonathan Lemon 778db4f9cc7SJonathan Lemon ip = mtod(m, struct ip *); 77953be11f6SPoul-Henning Kamp offset = ip->ip_hl << 2 ; 780db4f9cc7SJonathan Lemon csum = in_cksum_skip(m, ip->ip_len, offset); 781206a3274SRuslan Ermilov if (m->m_pkthdr.csum_flags & CSUM_UDP && csum == 0) 782206a3274SRuslan Ermilov csum = 0xffff; 783db4f9cc7SJonathan Lemon offset += m->m_pkthdr.csum_data; /* checksum offset */ 784db4f9cc7SJonathan Lemon 785db4f9cc7SJonathan Lemon if (offset + sizeof(u_short) > m->m_len) { 786db4f9cc7SJonathan Lemon printf("delayed m_pullup, m->len: %d off: %d p: %d\n", 787db4f9cc7SJonathan Lemon m->m_len, offset, ip->ip_p); 788db4f9cc7SJonathan Lemon /* 789db4f9cc7SJonathan Lemon * XXX 790db4f9cc7SJonathan Lemon * this shouldn't happen, but if it does, the 791db4f9cc7SJonathan Lemon * correct behavior may be to insert the checksum 7928f8d29f6SAndre Oppermann * in the appropriate next mbuf in the chain. 793db4f9cc7SJonathan Lemon */ 7948f8d29f6SAndre Oppermann return; 795db4f9cc7SJonathan Lemon } 796db4f9cc7SJonathan Lemon *(u_short *)(m->m_data + offset) = csum; 797db4f9cc7SJonathan Lemon } 798db4f9cc7SJonathan Lemon 799df8bae1dSRodney W. Grimes /* 800df8bae1dSRodney W. Grimes * IP socket option processing. 801df8bae1dSRodney W. Grimes */ 802df8bae1dSRodney W. Grimes int 803f2565d68SRobert Watson ip_ctloutput(struct socket *so, struct sockopt *sopt) 804df8bae1dSRodney W. Grimes { 805cfe8b629SGarrett Wollman struct inpcb *inp = sotoinpcb(so); 806cfe8b629SGarrett Wollman int error, optval; 807df8bae1dSRodney W. Grimes 808cfe8b629SGarrett Wollman error = optval = 0; 809cfe8b629SGarrett Wollman if (sopt->sopt_level != IPPROTO_IP) { 810cfe8b629SGarrett Wollman return (EINVAL); 811cfe8b629SGarrett Wollman } 812df8bae1dSRodney W. Grimes 813cfe8b629SGarrett Wollman switch (sopt->sopt_dir) { 814cfe8b629SGarrett Wollman case SOPT_SET: 815cfe8b629SGarrett Wollman switch (sopt->sopt_name) { 816df8bae1dSRodney W. Grimes case IP_OPTIONS: 817df8bae1dSRodney W. Grimes #ifdef notyet 818df8bae1dSRodney W. Grimes case IP_RETOPTS: 819df8bae1dSRodney W. Grimes #endif 820cfe8b629SGarrett Wollman { 821cfe8b629SGarrett Wollman struct mbuf *m; 822cfe8b629SGarrett Wollman if (sopt->sopt_valsize > MLEN) { 823cfe8b629SGarrett Wollman error = EMSGSIZE; 824cfe8b629SGarrett Wollman break; 825cfe8b629SGarrett Wollman } 826e0aec682SAndre Oppermann MGET(m, sopt->sopt_td ? M_TRYWAIT : M_DONTWAIT, MT_DATA); 8270b17fba7SAndre Oppermann if (m == NULL) { 828cfe8b629SGarrett Wollman error = ENOBUFS; 829cfe8b629SGarrett Wollman break; 830cfe8b629SGarrett Wollman } 831cfe8b629SGarrett Wollman m->m_len = sopt->sopt_valsize; 832cfe8b629SGarrett Wollman error = sooptcopyin(sopt, mtod(m, char *), m->m_len, 833cfe8b629SGarrett Wollman m->m_len); 834635354c4SMaxim Konovalov if (error) { 835635354c4SMaxim Konovalov m_free(m); 836635354c4SMaxim Konovalov break; 837635354c4SMaxim Konovalov } 838993d9505SRobert Watson INP_LOCK(inp); 839993d9505SRobert Watson error = ip_pcbopts(inp, sopt->sopt_name, m); 840993d9505SRobert Watson INP_UNLOCK(inp); 841993d9505SRobert Watson return (error); 842cfe8b629SGarrett Wollman } 843df8bae1dSRodney W. Grimes 844df8bae1dSRodney W. Grimes case IP_TOS: 845df8bae1dSRodney W. Grimes case IP_TTL: 846936cd18dSAndre Oppermann case IP_MINTTL: 847df8bae1dSRodney W. Grimes case IP_RECVOPTS: 848df8bae1dSRodney W. Grimes case IP_RECVRETOPTS: 849df8bae1dSRodney W. Grimes case IP_RECVDSTADDR: 8504957466bSMatthew N. Dodd case IP_RECVTTL: 85182c23ebaSBill Fenner case IP_RECVIF: 8526a800098SYoshinobu Inoue case IP_FAITH: 8538afa2304SBruce M Simpson case IP_ONESBCAST: 854b2828ad2SAndre Oppermann case IP_DONTFRAG: 855cfe8b629SGarrett Wollman error = sooptcopyin(sopt, &optval, sizeof optval, 856cfe8b629SGarrett Wollman sizeof optval); 857cfe8b629SGarrett Wollman if (error) 858cfe8b629SGarrett Wollman break; 859df8bae1dSRodney W. Grimes 860cfe8b629SGarrett Wollman switch (sopt->sopt_name) { 861df8bae1dSRodney W. Grimes case IP_TOS: 862ca98b82cSDavid Greenman inp->inp_ip_tos = optval; 863df8bae1dSRodney W. Grimes break; 864df8bae1dSRodney W. Grimes 865df8bae1dSRodney W. Grimes case IP_TTL: 866ca98b82cSDavid Greenman inp->inp_ip_ttl = optval; 867df8bae1dSRodney W. Grimes break; 868936cd18dSAndre Oppermann 869936cd18dSAndre Oppermann case IP_MINTTL: 870936cd18dSAndre Oppermann if (optval > 0 && optval <= MAXTTL) 871936cd18dSAndre Oppermann inp->inp_ip_minttl = optval; 872936cd18dSAndre Oppermann else 873936cd18dSAndre Oppermann error = EINVAL; 874936cd18dSAndre Oppermann break; 875936cd18dSAndre Oppermann 876a138d217SRobert Watson #define OPTSET(bit) do { \ 877a138d217SRobert Watson INP_LOCK(inp); \ 878df8bae1dSRodney W. Grimes if (optval) \ 879df8bae1dSRodney W. Grimes inp->inp_flags |= bit; \ 880df8bae1dSRodney W. Grimes else \ 881a138d217SRobert Watson inp->inp_flags &= ~bit; \ 882a138d217SRobert Watson INP_UNLOCK(inp); \ 883a138d217SRobert Watson } while (0) 884df8bae1dSRodney W. Grimes 885df8bae1dSRodney W. Grimes case IP_RECVOPTS: 886df8bae1dSRodney W. Grimes OPTSET(INP_RECVOPTS); 887df8bae1dSRodney W. Grimes break; 888df8bae1dSRodney W. Grimes 889df8bae1dSRodney W. Grimes case IP_RECVRETOPTS: 890df8bae1dSRodney W. Grimes OPTSET(INP_RECVRETOPTS); 891df8bae1dSRodney W. Grimes break; 892df8bae1dSRodney W. Grimes 893df8bae1dSRodney W. Grimes case IP_RECVDSTADDR: 894df8bae1dSRodney W. Grimes OPTSET(INP_RECVDSTADDR); 895df8bae1dSRodney W. Grimes break; 89682c23ebaSBill Fenner 8974957466bSMatthew N. Dodd case IP_RECVTTL: 8984957466bSMatthew N. Dodd OPTSET(INP_RECVTTL); 8994957466bSMatthew N. Dodd break; 9004957466bSMatthew N. Dodd 90182c23ebaSBill Fenner case IP_RECVIF: 90282c23ebaSBill Fenner OPTSET(INP_RECVIF); 90382c23ebaSBill Fenner break; 9046a800098SYoshinobu Inoue 9056a800098SYoshinobu Inoue case IP_FAITH: 9066a800098SYoshinobu Inoue OPTSET(INP_FAITH); 9076a800098SYoshinobu Inoue break; 9088afa2304SBruce M Simpson 9098afa2304SBruce M Simpson case IP_ONESBCAST: 9108afa2304SBruce M Simpson OPTSET(INP_ONESBCAST); 9118afa2304SBruce M Simpson break; 912b2828ad2SAndre Oppermann case IP_DONTFRAG: 913b2828ad2SAndre Oppermann OPTSET(INP_DONTFRAG); 914b2828ad2SAndre Oppermann break; 915df8bae1dSRodney W. Grimes } 916df8bae1dSRodney W. Grimes break; 917df8bae1dSRodney W. Grimes #undef OPTSET 918df8bae1dSRodney W. Grimes 91971498f30SBruce M Simpson /* 92071498f30SBruce M Simpson * Multicast socket options are processed by the in_mcast 92171498f30SBruce M Simpson * module. 92271498f30SBruce M Simpson */ 923df8bae1dSRodney W. Grimes case IP_MULTICAST_IF: 924f0068c4aSGarrett Wollman case IP_MULTICAST_VIF: 925df8bae1dSRodney W. Grimes case IP_MULTICAST_TTL: 926df8bae1dSRodney W. Grimes case IP_MULTICAST_LOOP: 927df8bae1dSRodney W. Grimes case IP_ADD_MEMBERSHIP: 928df8bae1dSRodney W. Grimes case IP_DROP_MEMBERSHIP: 92971498f30SBruce M Simpson case IP_ADD_SOURCE_MEMBERSHIP: 93071498f30SBruce M Simpson case IP_DROP_SOURCE_MEMBERSHIP: 93171498f30SBruce M Simpson case IP_BLOCK_SOURCE: 93271498f30SBruce M Simpson case IP_UNBLOCK_SOURCE: 93371498f30SBruce M Simpson case IP_MSFILTER: 93471498f30SBruce M Simpson case MCAST_JOIN_GROUP: 93571498f30SBruce M Simpson case MCAST_LEAVE_GROUP: 93671498f30SBruce M Simpson case MCAST_JOIN_SOURCE_GROUP: 93771498f30SBruce M Simpson case MCAST_LEAVE_SOURCE_GROUP: 93871498f30SBruce M Simpson case MCAST_BLOCK_SOURCE: 93971498f30SBruce M Simpson case MCAST_UNBLOCK_SOURCE: 94071498f30SBruce M Simpson error = inp_setmoptions(inp, sopt); 941df8bae1dSRodney W. Grimes break; 942df8bae1dSRodney W. Grimes 94333b3ac06SPeter Wemm case IP_PORTRANGE: 944cfe8b629SGarrett Wollman error = sooptcopyin(sopt, &optval, sizeof optval, 945cfe8b629SGarrett Wollman sizeof optval); 946cfe8b629SGarrett Wollman if (error) 947cfe8b629SGarrett Wollman break; 94833b3ac06SPeter Wemm 949a138d217SRobert Watson INP_LOCK(inp); 95033b3ac06SPeter Wemm switch (optval) { 95133b3ac06SPeter Wemm case IP_PORTRANGE_DEFAULT: 95233b3ac06SPeter Wemm inp->inp_flags &= ~(INP_LOWPORT); 95333b3ac06SPeter Wemm inp->inp_flags &= ~(INP_HIGHPORT); 95433b3ac06SPeter Wemm break; 95533b3ac06SPeter Wemm 95633b3ac06SPeter Wemm case IP_PORTRANGE_HIGH: 95733b3ac06SPeter Wemm inp->inp_flags &= ~(INP_LOWPORT); 95833b3ac06SPeter Wemm inp->inp_flags |= INP_HIGHPORT; 95933b3ac06SPeter Wemm break; 96033b3ac06SPeter Wemm 96133b3ac06SPeter Wemm case IP_PORTRANGE_LOW: 96233b3ac06SPeter Wemm inp->inp_flags &= ~(INP_HIGHPORT); 96333b3ac06SPeter Wemm inp->inp_flags |= INP_LOWPORT; 96433b3ac06SPeter Wemm break; 96533b3ac06SPeter Wemm 96633b3ac06SPeter Wemm default: 96733b3ac06SPeter Wemm error = EINVAL; 96833b3ac06SPeter Wemm break; 96933b3ac06SPeter Wemm } 970a138d217SRobert Watson INP_UNLOCK(inp); 971ce8c72b1SPeter Wemm break; 97233b3ac06SPeter Wemm 973b2630c29SGeorge V. Neville-Neil #ifdef IPSEC 9746a800098SYoshinobu Inoue case IP_IPSEC_POLICY: 9756a800098SYoshinobu Inoue { 9766a800098SYoshinobu Inoue caddr_t req; 9776a800098SYoshinobu Inoue struct mbuf *m; 9786a800098SYoshinobu Inoue 9796a800098SYoshinobu Inoue if ((error = soopt_getm(sopt, &m)) != 0) /* XXX */ 9806a800098SYoshinobu Inoue break; 9816a800098SYoshinobu Inoue if ((error = soopt_mcopyin(sopt, m)) != 0) /* XXX */ 9826a800098SYoshinobu Inoue break; 9836a800098SYoshinobu Inoue req = mtod(m, caddr_t); 984c26fe973SBjoern A. Zeeb error = ipsec4_set_policy(inp, sopt->sopt_name, req, 985c26fe973SBjoern A. Zeeb m->m_len, (sopt->sopt_td != NULL) ? 986c26fe973SBjoern A. Zeeb sopt->sopt_td->td_ucred : NULL); 9876a800098SYoshinobu Inoue m_freem(m); 9886a800098SYoshinobu Inoue break; 9896a800098SYoshinobu Inoue } 990b2630c29SGeorge V. Neville-Neil #endif /* IPSEC */ 9916a800098SYoshinobu Inoue 992df8bae1dSRodney W. Grimes default: 993df8bae1dSRodney W. Grimes error = ENOPROTOOPT; 994df8bae1dSRodney W. Grimes break; 995df8bae1dSRodney W. Grimes } 996df8bae1dSRodney W. Grimes break; 997df8bae1dSRodney W. Grimes 998cfe8b629SGarrett Wollman case SOPT_GET: 999cfe8b629SGarrett Wollman switch (sopt->sopt_name) { 1000df8bae1dSRodney W. Grimes case IP_OPTIONS: 1001df8bae1dSRodney W. Grimes case IP_RETOPTS: 1002cfe8b629SGarrett Wollman if (inp->inp_options) 1003cfe8b629SGarrett Wollman error = sooptcopyout(sopt, 1004cfe8b629SGarrett Wollman mtod(inp->inp_options, 1005cfe8b629SGarrett Wollman char *), 1006cfe8b629SGarrett Wollman inp->inp_options->m_len); 1007cfe8b629SGarrett Wollman else 1008cfe8b629SGarrett Wollman sopt->sopt_valsize = 0; 1009df8bae1dSRodney W. Grimes break; 1010df8bae1dSRodney W. Grimes 1011df8bae1dSRodney W. Grimes case IP_TOS: 1012df8bae1dSRodney W. Grimes case IP_TTL: 1013936cd18dSAndre Oppermann case IP_MINTTL: 1014df8bae1dSRodney W. Grimes case IP_RECVOPTS: 1015df8bae1dSRodney W. Grimes case IP_RECVRETOPTS: 1016df8bae1dSRodney W. Grimes case IP_RECVDSTADDR: 10174957466bSMatthew N. Dodd case IP_RECVTTL: 101882c23ebaSBill Fenner case IP_RECVIF: 1019cfe8b629SGarrett Wollman case IP_PORTRANGE: 10206a800098SYoshinobu Inoue case IP_FAITH: 10218afa2304SBruce M Simpson case IP_ONESBCAST: 1022b2828ad2SAndre Oppermann case IP_DONTFRAG: 1023cfe8b629SGarrett Wollman switch (sopt->sopt_name) { 1024df8bae1dSRodney W. Grimes 1025df8bae1dSRodney W. Grimes case IP_TOS: 1026ca98b82cSDavid Greenman optval = inp->inp_ip_tos; 1027df8bae1dSRodney W. Grimes break; 1028df8bae1dSRodney W. Grimes 1029df8bae1dSRodney W. Grimes case IP_TTL: 1030ca98b82cSDavid Greenman optval = inp->inp_ip_ttl; 1031df8bae1dSRodney W. Grimes break; 1032df8bae1dSRodney W. Grimes 1033936cd18dSAndre Oppermann case IP_MINTTL: 1034936cd18dSAndre Oppermann optval = inp->inp_ip_minttl; 1035936cd18dSAndre Oppermann break; 1036936cd18dSAndre Oppermann 1037df8bae1dSRodney W. Grimes #define OPTBIT(bit) (inp->inp_flags & bit ? 1 : 0) 1038df8bae1dSRodney W. Grimes 1039df8bae1dSRodney W. Grimes case IP_RECVOPTS: 1040df8bae1dSRodney W. Grimes optval = OPTBIT(INP_RECVOPTS); 1041df8bae1dSRodney W. Grimes break; 1042df8bae1dSRodney W. Grimes 1043df8bae1dSRodney W. Grimes case IP_RECVRETOPTS: 1044df8bae1dSRodney W. Grimes optval = OPTBIT(INP_RECVRETOPTS); 1045df8bae1dSRodney W. Grimes break; 1046df8bae1dSRodney W. Grimes 1047df8bae1dSRodney W. Grimes case IP_RECVDSTADDR: 1048df8bae1dSRodney W. Grimes optval = OPTBIT(INP_RECVDSTADDR); 1049df8bae1dSRodney W. Grimes break; 105082c23ebaSBill Fenner 10514957466bSMatthew N. Dodd case IP_RECVTTL: 10524957466bSMatthew N. Dodd optval = OPTBIT(INP_RECVTTL); 10534957466bSMatthew N. Dodd break; 10544957466bSMatthew N. Dodd 105582c23ebaSBill Fenner case IP_RECVIF: 105682c23ebaSBill Fenner optval = OPTBIT(INP_RECVIF); 105782c23ebaSBill Fenner break; 1058cfe8b629SGarrett Wollman 1059cfe8b629SGarrett Wollman case IP_PORTRANGE: 1060cfe8b629SGarrett Wollman if (inp->inp_flags & INP_HIGHPORT) 1061cfe8b629SGarrett Wollman optval = IP_PORTRANGE_HIGH; 1062cfe8b629SGarrett Wollman else if (inp->inp_flags & INP_LOWPORT) 1063cfe8b629SGarrett Wollman optval = IP_PORTRANGE_LOW; 1064cfe8b629SGarrett Wollman else 1065cfe8b629SGarrett Wollman optval = 0; 1066cfe8b629SGarrett Wollman break; 10676a800098SYoshinobu Inoue 10686a800098SYoshinobu Inoue case IP_FAITH: 10696a800098SYoshinobu Inoue optval = OPTBIT(INP_FAITH); 10706a800098SYoshinobu Inoue break; 10718afa2304SBruce M Simpson 10728afa2304SBruce M Simpson case IP_ONESBCAST: 10738afa2304SBruce M Simpson optval = OPTBIT(INP_ONESBCAST); 10748afa2304SBruce M Simpson break; 1075b2828ad2SAndre Oppermann case IP_DONTFRAG: 1076b2828ad2SAndre Oppermann optval = OPTBIT(INP_DONTFRAG); 1077b2828ad2SAndre Oppermann break; 1078df8bae1dSRodney W. Grimes } 1079cfe8b629SGarrett Wollman error = sooptcopyout(sopt, &optval, sizeof optval); 1080df8bae1dSRodney W. Grimes break; 1081df8bae1dSRodney W. Grimes 108271498f30SBruce M Simpson /* 108371498f30SBruce M Simpson * Multicast socket options are processed by the in_mcast 108471498f30SBruce M Simpson * module. 108571498f30SBruce M Simpson */ 1086df8bae1dSRodney W. Grimes case IP_MULTICAST_IF: 1087f0068c4aSGarrett Wollman case IP_MULTICAST_VIF: 1088df8bae1dSRodney W. Grimes case IP_MULTICAST_TTL: 1089df8bae1dSRodney W. Grimes case IP_MULTICAST_LOOP: 109071498f30SBruce M Simpson case IP_MSFILTER: 109171498f30SBruce M Simpson error = inp_getmoptions(inp, sopt); 109233b3ac06SPeter Wemm break; 109333b3ac06SPeter Wemm 1094b2630c29SGeorge V. Neville-Neil #ifdef IPSEC 10956a800098SYoshinobu Inoue case IP_IPSEC_POLICY: 10966a800098SYoshinobu Inoue { 1097f63e7634SYoshinobu Inoue struct mbuf *m = NULL; 10986a800098SYoshinobu Inoue caddr_t req = NULL; 1099686cdd19SJun-ichiro itojun Hagino size_t len = 0; 11006a800098SYoshinobu Inoue 1101686cdd19SJun-ichiro itojun Hagino if (m != 0) { 11026a800098SYoshinobu Inoue req = mtod(m, caddr_t); 1103686cdd19SJun-ichiro itojun Hagino len = m->m_len; 1104686cdd19SJun-ichiro itojun Hagino } 1105686cdd19SJun-ichiro itojun Hagino error = ipsec4_get_policy(sotoinpcb(so), req, len, &m); 11066a800098SYoshinobu Inoue if (error == 0) 11076a800098SYoshinobu Inoue error = soopt_mcopyout(sopt, m); /* XXX */ 1108f63e7634SYoshinobu Inoue if (error == 0) 11096a800098SYoshinobu Inoue m_freem(m); 11106a800098SYoshinobu Inoue break; 11116a800098SYoshinobu Inoue } 1112b2630c29SGeorge V. Neville-Neil #endif /* IPSEC */ 11136a800098SYoshinobu Inoue 1114df8bae1dSRodney W. Grimes default: 1115df8bae1dSRodney W. Grimes error = ENOPROTOOPT; 1116df8bae1dSRodney W. Grimes break; 1117df8bae1dSRodney W. Grimes } 1118df8bae1dSRodney W. Grimes break; 1119df8bae1dSRodney W. Grimes } 1120df8bae1dSRodney W. Grimes return (error); 1121df8bae1dSRodney W. Grimes } 1122df8bae1dSRodney W. Grimes 1123df8bae1dSRodney W. Grimes /* 1124df8bae1dSRodney W. Grimes * Routine called from ip_output() to loop back a copy of an IP multicast 1125df8bae1dSRodney W. Grimes * packet to the input queue of a specified interface. Note that this 1126df8bae1dSRodney W. Grimes * calls the output routine of the loopback "driver", but with an interface 1127f5fea3ddSPaul Traina * pointer that might NOT be a loopback interface -- evil, but easier than 1128f5fea3ddSPaul Traina * replicating that code here. 1129df8bae1dSRodney W. Grimes */ 1130df8bae1dSRodney W. Grimes static void 1131f2565d68SRobert Watson ip_mloopback(struct ifnet *ifp, struct mbuf *m, struct sockaddr_in *dst, 1132f2565d68SRobert Watson int hlen) 1133df8bae1dSRodney W. Grimes { 1134b375c9ecSLuigi Rizzo register struct ip *ip; 1135df8bae1dSRodney W. Grimes struct mbuf *copym; 1136df8bae1dSRodney W. Grimes 1137b375c9ecSLuigi Rizzo copym = m_copy(m, 0, M_COPYALL); 113886b1d6d2SBill Fenner if (copym != NULL && (copym->m_flags & M_EXT || copym->m_len < hlen)) 113986b1d6d2SBill Fenner copym = m_pullup(copym, hlen); 1140df8bae1dSRodney W. Grimes if (copym != NULL) { 1141390cdc6aSRuslan Ermilov /* If needed, compute the checksum and mark it as valid. */ 1142390cdc6aSRuslan Ermilov if (copym->m_pkthdr.csum_flags & CSUM_DELAY_DATA) { 1143390cdc6aSRuslan Ermilov in_delayed_cksum(copym); 1144390cdc6aSRuslan Ermilov copym->m_pkthdr.csum_flags &= ~CSUM_DELAY_DATA; 1145390cdc6aSRuslan Ermilov copym->m_pkthdr.csum_flags |= 1146390cdc6aSRuslan Ermilov CSUM_DATA_VALID | CSUM_PSEUDO_HDR; 1147390cdc6aSRuslan Ermilov copym->m_pkthdr.csum_data = 0xffff; 1148390cdc6aSRuslan Ermilov } 1149df8bae1dSRodney W. Grimes /* 1150df8bae1dSRodney W. Grimes * We don't bother to fragment if the IP length is greater 1151df8bae1dSRodney W. Grimes * than the interface's MTU. Can this possibly matter? 1152df8bae1dSRodney W. Grimes */ 1153df8bae1dSRodney W. Grimes ip = mtod(copym, struct ip *); 1154fd8e4ebcSMike Barcroft ip->ip_len = htons(ip->ip_len); 1155fd8e4ebcSMike Barcroft ip->ip_off = htons(ip->ip_off); 1156df8bae1dSRodney W. Grimes ip->ip_sum = 0; 115786b1d6d2SBill Fenner ip->ip_sum = in_cksum(copym, hlen); 11589c9137eaSGarrett Wollman /* 11599c9137eaSGarrett Wollman * NB: 1160e1596dffSBill Fenner * It's not clear whether there are any lingering 1161e1596dffSBill Fenner * reentrancy problems in other areas which might 1162e1596dffSBill Fenner * be exposed by using ip_input directly (in 1163e1596dffSBill Fenner * particular, everything which modifies the packet 1164e1596dffSBill Fenner * in-place). Yet another option is using the 1165e1596dffSBill Fenner * protosw directly to deliver the looped back 1166e1596dffSBill Fenner * packet. For the moment, we'll err on the side 1167ed7509acSJulian Elischer * of safety by using if_simloop(). 11689c9137eaSGarrett Wollman */ 1169201c2527SJulian Elischer #if 1 /* XXX */ 1170201c2527SJulian Elischer if (dst->sin_family != AF_INET) { 11712b8a366cSJulian Elischer printf("ip_mloopback: bad address family %d\n", 1172201c2527SJulian Elischer dst->sin_family); 1173201c2527SJulian Elischer dst->sin_family = AF_INET; 1174201c2527SJulian Elischer } 1175201c2527SJulian Elischer #endif 1176201c2527SJulian Elischer 11779c9137eaSGarrett Wollman #ifdef notdef 1178e1596dffSBill Fenner copym->m_pkthdr.rcvif = ifp; 1179ed7509acSJulian Elischer ip_input(copym); 11809c9137eaSGarrett Wollman #else 118106a429a3SArchie Cobbs if_simloop(ifp, copym, dst->sin_family, 0); 11829c9137eaSGarrett Wollman #endif 1183df8bae1dSRodney W. Grimes } 1184df8bae1dSRodney W. Grimes } 1185