1df8bae1dSRodney W. Grimes /* 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 * 3. All advertising materials mentioning features or use of this software 14df8bae1dSRodney W. Grimes * must display the following acknowledgement: 15df8bae1dSRodney W. Grimes * This product includes software developed by the University of 16df8bae1dSRodney W. Grimes * California, Berkeley and its contributors. 17df8bae1dSRodney W. Grimes * 4. Neither the name of the University nor the names of its contributors 18df8bae1dSRodney W. Grimes * may be used to endorse or promote products derived from this software 19df8bae1dSRodney W. Grimes * without specific prior written permission. 20df8bae1dSRodney W. Grimes * 21df8bae1dSRodney W. Grimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 22df8bae1dSRodney W. Grimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 23df8bae1dSRodney W. Grimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 24df8bae1dSRodney W. Grimes * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 25df8bae1dSRodney W. Grimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 26df8bae1dSRodney W. Grimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 27df8bae1dSRodney W. Grimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 28df8bae1dSRodney W. Grimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 29df8bae1dSRodney W. Grimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 30df8bae1dSRodney W. Grimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 31df8bae1dSRodney W. Grimes * SUCH DAMAGE. 32df8bae1dSRodney W. Grimes * 33df8bae1dSRodney W. Grimes * @(#)ip_output.c 8.3 (Berkeley) 1/21/94 34c3aac50fSPeter Wemm * $FreeBSD$ 35df8bae1dSRodney W. Grimes */ 36df8bae1dSRodney W. Grimes 371f7e052cSJulian Elischer #include "opt_ipfw.h" 38b715f178SLuigi Rizzo #include "opt_ipdn.h" 39fbd1372aSJoerg Wunsch #include "opt_ipdivert.h" 401ee25934SPeter Wemm #include "opt_ipfilter.h" 416a800098SYoshinobu Inoue #include "opt_ipsec.h" 424ed84624SRobert Watson #include "opt_mac.h" 43c4ac87eaSDarren Reed #include "opt_pfil_hooks.h" 4464dddc18SKris Kennaway #include "opt_random_ip_id.h" 4553dcc544SMike Silbersack #include "opt_mbuf_stress_test.h" 46fbd1372aSJoerg Wunsch 47df8bae1dSRodney W. Grimes #include <sys/param.h> 4826f9a767SRodney W. Grimes #include <sys/systm.h> 49f0f6d643SLuigi Rizzo #include <sys/kernel.h> 504ed84624SRobert Watson #include <sys/mac.h> 51df8bae1dSRodney W. Grimes #include <sys/malloc.h> 52df8bae1dSRodney W. Grimes #include <sys/mbuf.h> 53df8bae1dSRodney W. Grimes #include <sys/protosw.h> 54df8bae1dSRodney W. Grimes #include <sys/socket.h> 55df8bae1dSRodney W. Grimes #include <sys/socketvar.h> 569d9edc56SMike Silbersack #include <sys/sysctl.h> 57df8bae1dSRodney W. Grimes 58df8bae1dSRodney W. Grimes #include <net/if.h> 59df8bae1dSRodney W. Grimes #include <net/route.h> 60df8bae1dSRodney W. Grimes 61df8bae1dSRodney W. Grimes #include <netinet/in.h> 62df8bae1dSRodney W. Grimes #include <netinet/in_systm.h> 63df8bae1dSRodney W. Grimes #include <netinet/ip.h> 64df8bae1dSRodney W. Grimes #include <netinet/in_pcb.h> 65df8bae1dSRodney W. Grimes #include <netinet/in_var.h> 66df8bae1dSRodney W. Grimes #include <netinet/ip_var.h> 67df8bae1dSRodney W. Grimes 68134ea224SSam Leffler #ifdef PFIL_HOOKS 69134ea224SSam Leffler #include <net/pfil.h> 70134ea224SSam Leffler #endif 71134ea224SSam Leffler 729c9137eaSGarrett Wollman #include <machine/in_cksum.h> 73df8bae1dSRodney W. Grimes 74a1c995b6SPoul-Henning Kamp static MALLOC_DEFINE(M_IPMOPTS, "ip_moptions", "internet multicast options"); 7555166637SPoul-Henning Kamp 766a800098SYoshinobu Inoue #ifdef IPSEC 776a800098SYoshinobu Inoue #include <netinet6/ipsec.h> 786a800098SYoshinobu Inoue #include <netkey/key.h> 796a800098SYoshinobu Inoue #ifdef IPSEC_DEBUG 806a800098SYoshinobu Inoue #include <netkey/key_debug.h> 816a800098SYoshinobu Inoue #else 826a800098SYoshinobu Inoue #define KEYDEBUG(lev,arg) 836a800098SYoshinobu Inoue #endif 846a800098SYoshinobu Inoue #endif /*IPSEC*/ 856a800098SYoshinobu Inoue 86b9234fafSSam Leffler #ifdef FAST_IPSEC 87b9234fafSSam Leffler #include <netipsec/ipsec.h> 88b9234fafSSam Leffler #include <netipsec/xform.h> 89b9234fafSSam Leffler #include <netipsec/key.h> 90b9234fafSSam Leffler #endif /*FAST_IPSEC*/ 91b9234fafSSam Leffler 92cfe8b629SGarrett Wollman #include <netinet/ip_fw.h> 93b715f178SLuigi Rizzo #include <netinet/ip_dummynet.h> 94b715f178SLuigi Rizzo 952b25acc1SLuigi Rizzo #define print_ip(x, a, y) printf("%s %d.%d.%d.%d%s",\ 962b25acc1SLuigi Rizzo x, (ntohl(a.s_addr)>>24)&0xFF,\ 97f9e354dfSJulian Elischer (ntohl(a.s_addr)>>16)&0xFF,\ 98f9e354dfSJulian Elischer (ntohl(a.s_addr)>>8)&0xFF,\ 992b25acc1SLuigi Rizzo (ntohl(a.s_addr))&0xFF, y); 100f9e354dfSJulian Elischer 101f23b4c91SGarrett Wollman u_short ip_id; 102f23b4c91SGarrett Wollman 10353dcc544SMike Silbersack #ifdef MBUF_STRESS_TEST 1049d9edc56SMike Silbersack int mbuf_frag_size = 0; 1059d9edc56SMike Silbersack SYSCTL_INT(_net_inet_ip, OID_AUTO, mbuf_frag_size, CTLFLAG_RW, 1069d9edc56SMike Silbersack &mbuf_frag_size, 0, "Fragment outgoing mbufs to this size"); 1079d9edc56SMike Silbersack #endif 1089d9edc56SMike Silbersack 1094d77a549SAlfred Perlstein static struct mbuf *ip_insertoptions(struct mbuf *, struct mbuf *, int *); 1104d77a549SAlfred Perlstein static struct ifnet *ip_multicast_if(struct in_addr *, int *); 111df8bae1dSRodney W. Grimes static void ip_mloopback 1124d77a549SAlfred Perlstein (struct ifnet *, struct mbuf *, struct sockaddr_in *, int); 1130312fbe9SPoul-Henning Kamp static int ip_getmoptions 1144d77a549SAlfred Perlstein (struct sockopt *, struct ip_moptions *); 1154d77a549SAlfred Perlstein static int ip_pcbopts(int, struct mbuf **, struct mbuf *); 1160312fbe9SPoul-Henning Kamp static int ip_setmoptions 1174d77a549SAlfred Perlstein (struct sockopt *, struct ip_moptions **); 118df8bae1dSRodney W. Grimes 1194d77a549SAlfred Perlstein int ip_optcopy(struct ip *, struct ip *); 120afed1b49SDarren Reed 121afed1b49SDarren Reed 12293e0e116SJulian Elischer extern struct protosw inetsw[]; 12393e0e116SJulian Elischer 124df8bae1dSRodney W. Grimes /* 125df8bae1dSRodney W. Grimes * IP output. The packet in mbuf chain m contains a skeletal IP 126df8bae1dSRodney W. Grimes * header (with len, off, ttl, proto, tos, src, dst). 127df8bae1dSRodney W. Grimes * The mbuf chain containing the packet will be freed. 128df8bae1dSRodney W. Grimes * The mbuf opt, if present, will not be freed. 1293de758d3SRobert Watson * In the IP forwarding case, the packet will arrive with options already 1303de758d3SRobert Watson * inserted, so must have a NULL opt pointer. 131df8bae1dSRodney W. Grimes */ 132df8bae1dSRodney W. Grimes int 1331e78ac21SJeffrey Hsu ip_output(struct mbuf *m0, struct mbuf *opt, struct route *ro, 1341e78ac21SJeffrey Hsu int flags, struct ip_moptions *imo, struct inpcb *inp) 135df8bae1dSRodney W. Grimes { 1361e78ac21SJeffrey Hsu struct ip *ip; 1372b25acc1SLuigi Rizzo struct ifnet *ifp = NULL; /* keep compiler happy */ 1382b25acc1SLuigi Rizzo struct mbuf *m; 13923bf9953SPoul-Henning Kamp int hlen = sizeof (struct ip); 140df8bae1dSRodney W. Grimes int len, off, error = 0; 1412b25acc1SLuigi Rizzo struct sockaddr_in *dst = NULL; /* keep compiler happy */ 1423956b023SLuigi Rizzo struct in_ifaddr *ia = NULL; 143db4f9cc7SJonathan Lemon int isbroadcast, sw_csum; 1443efc3014SJulian Elischer struct in_addr pkt_dst; 1456a800098SYoshinobu Inoue #ifdef IPSEC 146e3f406b3SRuslan Ermilov struct route iproute; 1476a800098SYoshinobu Inoue struct secpolicy *sp = NULL; 1486a800098SYoshinobu Inoue #endif 149b9234fafSSam Leffler #ifdef FAST_IPSEC 150b9234fafSSam Leffler struct route iproute; 151b9234fafSSam Leffler struct m_tag *mtag; 152b9234fafSSam Leffler struct secpolicy *sp = NULL; 153b9234fafSSam Leffler struct tdb_ident *tdbi; 154b9234fafSSam Leffler int s; 155b9234fafSSam Leffler #endif /* FAST_IPSEC */ 1562b25acc1SLuigi Rizzo struct ip_fw_args args; 1572b25acc1SLuigi Rizzo int src_was_INADDR_ANY = 0; /* as the name says... */ 158b715f178SLuigi Rizzo 1592b25acc1SLuigi Rizzo args.eh = NULL; 1602b25acc1SLuigi Rizzo args.rule = NULL; 1612b25acc1SLuigi Rizzo args.next_hop = NULL; 1622b25acc1SLuigi Rizzo args.divert_rule = 0; /* divert cookie */ 1638948e4baSArchie Cobbs 1642b25acc1SLuigi Rizzo /* Grab info from MT_TAG mbufs prepended to the chain. */ 1652b25acc1SLuigi Rizzo for (; m0 && m0->m_type == MT_TAG; m0 = m0->m_next) { 1665d846453SSam Leffler switch(m0->_m_tag_id) { 1672b25acc1SLuigi Rizzo default: 1682b25acc1SLuigi Rizzo printf("ip_output: unrecognised MT_TAG tag %d\n", 1695d846453SSam Leffler m0->_m_tag_id); 1702b25acc1SLuigi Rizzo break; 1712b25acc1SLuigi Rizzo 1722b25acc1SLuigi Rizzo case PACKET_TAG_DUMMYNET: 173b715f178SLuigi Rizzo /* 174b715f178SLuigi Rizzo * the packet was already tagged, so part of the 175b715f178SLuigi Rizzo * processing was already done, and we need to go down. 1766bc748b0SLuigi Rizzo * Get parameters from the header. 177b715f178SLuigi Rizzo */ 1782b25acc1SLuigi Rizzo args.rule = ((struct dn_pkt *)m0)->rule; 179d1f04b29SLuigi Rizzo opt = NULL ; 1802b25acc1SLuigi Rizzo ro = & ( ((struct dn_pkt *)m0)->ro ) ; 181d1f04b29SLuigi Rizzo imo = NULL ; 1822b25acc1SLuigi Rizzo dst = ((struct dn_pkt *)m0)->dn_dst ; 1832b25acc1SLuigi Rizzo ifp = ((struct dn_pkt *)m0)->ifp ; 1842b25acc1SLuigi Rizzo flags = ((struct dn_pkt *)m0)->flags ; 1852b25acc1SLuigi Rizzo break; 186d1f04b29SLuigi Rizzo 1872b25acc1SLuigi Rizzo case PACKET_TAG_DIVERT: 1887627c6cbSMaxime Henrion args.divert_rule = (intptr_t)m0->m_data & 0xffff; 1892b25acc1SLuigi Rizzo break; 1902b25acc1SLuigi Rizzo 1912b25acc1SLuigi Rizzo case PACKET_TAG_IPFORWARD: 1922b25acc1SLuigi Rizzo args.next_hop = (struct sockaddr_in *)m0->m_data; 1932b25acc1SLuigi Rizzo break; 1942b25acc1SLuigi Rizzo } 1952b25acc1SLuigi Rizzo } 1962b25acc1SLuigi Rizzo m = m0; 1972b25acc1SLuigi Rizzo 198fe584538SDag-Erling Smørgrav M_ASSERTPKTHDR(m); 199b9234fafSSam Leffler #ifndef FAST_IPSEC 200b9234fafSSam Leffler KASSERT(ro != NULL, ("ip_output: no route, proto %d", 201b9234fafSSam Leffler mtod(m, struct ip *)->ip_p)); 202b9234fafSSam Leffler #endif 2032b25acc1SLuigi Rizzo 2042b25acc1SLuigi Rizzo if (args.rule != NULL) { /* dummynet already saw us */ 205b715f178SLuigi Rizzo ip = mtod(m, struct ip *); 20653be11f6SPoul-Henning Kamp hlen = ip->ip_hl << 2 ; 2073956b023SLuigi Rizzo if (ro->ro_rt) 2089a10980eSJonathan Lemon ia = ifatoia(ro->ro_rt->rt_ifa); 209b715f178SLuigi Rizzo goto sendit; 2102b25acc1SLuigi Rizzo } 211db40007dSAndrew R. Reiter 212df8bae1dSRodney W. Grimes if (opt) { 213cb7641e8SMaxim Konovalov len = 0; 214df8bae1dSRodney W. Grimes m = ip_insertoptions(m, opt, &len); 215cb7641e8SMaxim Konovalov if (len != 0) 216df8bae1dSRodney W. Grimes hlen = len; 217df8bae1dSRodney W. Grimes } 218df8bae1dSRodney W. Grimes ip = mtod(m, struct ip *); 2192b25acc1SLuigi Rizzo pkt_dst = args.next_hop ? args.next_hop->sin_addr : ip->ip_dst; 2203efc3014SJulian Elischer 221df8bae1dSRodney W. Grimes /* 2226e49b1feSGarrett Wollman * Fill in IP header. If we are not allowing fragmentation, 2236e49b1feSGarrett Wollman * then the ip_id field is meaningless, so send it as zero 2246e49b1feSGarrett Wollman * to reduce information leakage. Otherwise, if we are not 2256e49b1feSGarrett Wollman * randomizing ip_id, then don't bother to convert it to network 2266e49b1feSGarrett Wollman * byte order -- it's just a nonce. Note that a 16-bit counter 2276e49b1feSGarrett Wollman * will wrap around in less than 10 seconds at 100 Mbit/s on a 2286e49b1feSGarrett Wollman * medium with MTU 1500. See Steven M. Bellovin, "A Technique 2296e49b1feSGarrett Wollman * for Counting NATted Hosts", Proc. IMW'02, available at 2306e49b1feSGarrett Wollman * <http://www.research.att.com/~smb/papers/fnat.pdf>. 231df8bae1dSRodney W. Grimes */ 232df8bae1dSRodney W. Grimes if ((flags & (IP_FORWARDING|IP_RAWOUTPUT)) == 0) { 23353be11f6SPoul-Henning Kamp ip->ip_v = IPVERSION; 23453be11f6SPoul-Henning Kamp ip->ip_hl = hlen >> 2; 2356e49b1feSGarrett Wollman if ((ip->ip_off & IP_DF) == 0) { 2366e49b1feSGarrett Wollman ip->ip_off = 0; 23764dddc18SKris Kennaway #ifdef RANDOM_IP_ID 23864dddc18SKris Kennaway ip->ip_id = ip_randomid(); 23964dddc18SKris Kennaway #else 2406e49b1feSGarrett Wollman ip->ip_id = ip_id++; 24164dddc18SKris Kennaway #endif 2426e49b1feSGarrett Wollman } else { 2436e49b1feSGarrett Wollman ip->ip_off = IP_DF; 2446e49b1feSGarrett Wollman ip->ip_id = 0; 2456e49b1feSGarrett Wollman } 246df8bae1dSRodney W. Grimes ipstat.ips_localout++; 247df8bae1dSRodney W. Grimes } else { 24853be11f6SPoul-Henning Kamp hlen = ip->ip_hl << 2; 249df8bae1dSRodney W. Grimes } 2509c9137eaSGarrett Wollman 251b9234fafSSam Leffler #ifdef FAST_IPSEC 252b9234fafSSam Leffler if (ro == NULL) { 253b9234fafSSam Leffler ro = &iproute; 254b9234fafSSam Leffler bzero(ro, sizeof (*ro)); 255b9234fafSSam Leffler } 256b9234fafSSam Leffler #endif /* FAST_IPSEC */ 257df8bae1dSRodney W. Grimes dst = (struct sockaddr_in *)&ro->ro_dst; 258df8bae1dSRodney W. Grimes /* 259df8bae1dSRodney W. Grimes * If there is a cached route, 260df8bae1dSRodney W. Grimes * check that it is to the same destination 261df8bae1dSRodney W. Grimes * and is still up. If not, free it and try again. 262a4a6e773SHajimu UMEMOTO * The address family should also be checked in case of sharing the 263a4a6e773SHajimu UMEMOTO * cache with IPv6. 264df8bae1dSRodney W. Grimes */ 265df8bae1dSRodney W. Grimes if (ro->ro_rt && ((ro->ro_rt->rt_flags & RTF_UP) == 0 || 266a4a6e773SHajimu UMEMOTO dst->sin_family != AF_INET || 2673efc3014SJulian Elischer dst->sin_addr.s_addr != pkt_dst.s_addr)) { 268df8bae1dSRodney W. Grimes RTFREE(ro->ro_rt); 269df8bae1dSRodney W. Grimes ro->ro_rt = (struct rtentry *)0; 270df8bae1dSRodney W. Grimes } 271df8bae1dSRodney W. Grimes if (ro->ro_rt == 0) { 272a4a6e773SHajimu UMEMOTO bzero(dst, sizeof(*dst)); 273df8bae1dSRodney W. Grimes dst->sin_family = AF_INET; 274df8bae1dSRodney W. Grimes dst->sin_len = sizeof(*dst); 2753efc3014SJulian Elischer dst->sin_addr = pkt_dst; 276df8bae1dSRodney W. Grimes } 277df8bae1dSRodney W. Grimes /* 278df8bae1dSRodney W. Grimes * If routing to interface only, 279df8bae1dSRodney W. Grimes * short circuit routing lookup. 280df8bae1dSRodney W. Grimes */ 281df8bae1dSRodney W. Grimes if (flags & IP_ROUTETOIF) { 282df8bae1dSRodney W. Grimes if ((ia = ifatoia(ifa_ifwithdstaddr(sintosa(dst)))) == 0 && 283df8bae1dSRodney W. Grimes (ia = ifatoia(ifa_ifwithnet(sintosa(dst)))) == 0) { 284df8bae1dSRodney W. Grimes ipstat.ips_noroute++; 285df8bae1dSRodney W. Grimes error = ENETUNREACH; 286df8bae1dSRodney W. Grimes goto bad; 287df8bae1dSRodney W. Grimes } 288df8bae1dSRodney W. Grimes ifp = ia->ia_ifp; 289df8bae1dSRodney W. Grimes ip->ip_ttl = 1; 2909f9b3dc4SGarrett Wollman isbroadcast = in_broadcast(dst->sin_addr, ifp); 2913afefa39SDaniel C. Sobral } else if (IN_MULTICAST(ntohl(ip->ip_dst.s_addr)) && 29238c1bc35SRuslan Ermilov imo != NULL && imo->imo_multicast_ifp != NULL) { 2933afefa39SDaniel C. Sobral /* 29438c1bc35SRuslan Ermilov * Bypass the normal routing lookup for multicast 29538c1bc35SRuslan Ermilov * packets if the interface is specified. 2963afefa39SDaniel C. Sobral */ 29738c1bc35SRuslan Ermilov ifp = imo->imo_multicast_ifp; 29838c1bc35SRuslan Ermilov IFP_TO_IA(ifp, ia); 29938c1bc35SRuslan Ermilov isbroadcast = 0; /* fool gcc */ 300df8bae1dSRodney W. Grimes } else { 3012c17fe93SGarrett Wollman /* 302f2c2962eSRuslan Ermilov * If this is the case, we probably don't want to allocate 303f2c2962eSRuslan Ermilov * a protocol-cloned route since we didn't get one from the 304f2c2962eSRuslan Ermilov * ULP. This lets TCP do its thing, while not burdening 305f2c2962eSRuslan Ermilov * forwarding or ICMP with the overhead of cloning a route. 306f2c2962eSRuslan Ermilov * Of course, we still want to do any cloning requested by 307f2c2962eSRuslan Ermilov * the link layer, as this is probably required in all cases 308f2c2962eSRuslan Ermilov * for correct operation (as it is for ARP). 3092c17fe93SGarrett Wollman */ 310df8bae1dSRodney W. Grimes if (ro->ro_rt == 0) 3112c17fe93SGarrett Wollman rtalloc_ign(ro, RTF_PRCLONING); 312df8bae1dSRodney W. Grimes if (ro->ro_rt == 0) { 313df8bae1dSRodney W. Grimes ipstat.ips_noroute++; 314df8bae1dSRodney W. Grimes error = EHOSTUNREACH; 315df8bae1dSRodney W. Grimes goto bad; 316df8bae1dSRodney W. Grimes } 317df8bae1dSRodney W. Grimes ia = ifatoia(ro->ro_rt->rt_ifa); 318df8bae1dSRodney W. Grimes ifp = ro->ro_rt->rt_ifp; 319df8bae1dSRodney W. Grimes ro->ro_rt->rt_use++; 320df8bae1dSRodney W. Grimes if (ro->ro_rt->rt_flags & RTF_GATEWAY) 321f2c2962eSRuslan Ermilov dst = (struct sockaddr_in *)ro->ro_rt->rt_gateway; 3229f9b3dc4SGarrett Wollman if (ro->ro_rt->rt_flags & RTF_HOST) 323f2c2962eSRuslan Ermilov isbroadcast = (ro->ro_rt->rt_flags & RTF_BROADCAST); 3249f9b3dc4SGarrett Wollman else 3259f9b3dc4SGarrett Wollman isbroadcast = in_broadcast(dst->sin_addr, ifp); 326df8bae1dSRodney W. Grimes } 3273efc3014SJulian Elischer if (IN_MULTICAST(ntohl(pkt_dst.s_addr))) { 328df8bae1dSRodney W. Grimes struct in_multi *inm; 329df8bae1dSRodney W. Grimes 330df8bae1dSRodney W. Grimes m->m_flags |= M_MCAST; 331df8bae1dSRodney W. Grimes /* 332df8bae1dSRodney W. Grimes * IP destination address is multicast. Make sure "dst" 333df8bae1dSRodney W. Grimes * still points to the address in "ro". (It may have been 334df8bae1dSRodney W. Grimes * changed to point to a gateway address, above.) 335df8bae1dSRodney W. Grimes */ 336df8bae1dSRodney W. Grimes dst = (struct sockaddr_in *)&ro->ro_dst; 337df8bae1dSRodney W. Grimes /* 338df8bae1dSRodney W. Grimes * See if the caller provided any multicast options 339df8bae1dSRodney W. Grimes */ 340df8bae1dSRodney W. Grimes if (imo != NULL) { 341df8bae1dSRodney W. Grimes ip->ip_ttl = imo->imo_multicast_ttl; 3421c5de19aSGarrett Wollman if (imo->imo_multicast_vif != -1) 3431c5de19aSGarrett Wollman ip->ip_src.s_addr = 344bbb4330bSLuigi Rizzo ip_mcast_src ? 345bbb4330bSLuigi Rizzo ip_mcast_src(imo->imo_multicast_vif) : 346bbb4330bSLuigi Rizzo INADDR_ANY; 347df8bae1dSRodney W. Grimes } else 348df8bae1dSRodney W. Grimes ip->ip_ttl = IP_DEFAULT_MULTICAST_TTL; 349df8bae1dSRodney W. Grimes /* 350df8bae1dSRodney W. Grimes * Confirm that the outgoing interface supports multicast. 351df8bae1dSRodney W. Grimes */ 3521c5de19aSGarrett Wollman if ((imo == NULL) || (imo->imo_multicast_vif == -1)) { 353df8bae1dSRodney W. Grimes if ((ifp->if_flags & IFF_MULTICAST) == 0) { 354df8bae1dSRodney W. Grimes ipstat.ips_noroute++; 355df8bae1dSRodney W. Grimes error = ENETUNREACH; 356df8bae1dSRodney W. Grimes goto bad; 357df8bae1dSRodney W. Grimes } 3581c5de19aSGarrett Wollman } 359df8bae1dSRodney W. Grimes /* 360df8bae1dSRodney W. Grimes * If source address not specified yet, use address 361df8bae1dSRodney W. Grimes * of outgoing interface. 362df8bae1dSRodney W. Grimes */ 363df8bae1dSRodney W. Grimes if (ip->ip_src.s_addr == INADDR_ANY) { 36438c1bc35SRuslan Ermilov /* Interface may have no addresses. */ 36538c1bc35SRuslan Ermilov if (ia != NULL) 36638c1bc35SRuslan Ermilov ip->ip_src = IA_SIN(ia)->sin_addr; 367df8bae1dSRodney W. Grimes } 368df8bae1dSRodney W. Grimes 36992bdb2faSBill Fenner if (ip_mrouter && (flags & IP_FORWARDING) == 0) { 37092bdb2faSBill Fenner /* 37192bdb2faSBill Fenner * XXX 37292bdb2faSBill Fenner * delayed checksums are not currently 37392bdb2faSBill Fenner * compatible with IP multicast routing 37492bdb2faSBill Fenner */ 37592bdb2faSBill Fenner if (m->m_pkthdr.csum_flags & CSUM_DELAY_DATA) { 37692bdb2faSBill Fenner in_delayed_cksum(m); 37792bdb2faSBill Fenner m->m_pkthdr.csum_flags &= 37892bdb2faSBill Fenner ~CSUM_DELAY_DATA; 37992bdb2faSBill Fenner } 38092bdb2faSBill Fenner } 3813efc3014SJulian Elischer IN_LOOKUP_MULTI(pkt_dst, ifp, inm); 382df8bae1dSRodney W. Grimes if (inm != NULL && 383df8bae1dSRodney W. Grimes (imo == NULL || imo->imo_multicast_loop)) { 384df8bae1dSRodney W. Grimes /* 385df8bae1dSRodney W. Grimes * If we belong to the destination multicast group 386df8bae1dSRodney W. Grimes * on the outgoing interface, and the caller did not 387df8bae1dSRodney W. Grimes * forbid loopback, loop back a copy. 388df8bae1dSRodney W. Grimes */ 38986b1d6d2SBill Fenner ip_mloopback(ifp, m, dst, hlen); 390df8bae1dSRodney W. Grimes } 391df8bae1dSRodney W. Grimes else { 392df8bae1dSRodney W. Grimes /* 393df8bae1dSRodney W. Grimes * If we are acting as a multicast router, perform 394df8bae1dSRodney W. Grimes * multicast forwarding as if the packet had just 395df8bae1dSRodney W. Grimes * arrived on the interface to which we are about 396df8bae1dSRodney W. Grimes * to send. The multicast forwarding function 397df8bae1dSRodney W. Grimes * recursively calls this function, using the 398df8bae1dSRodney W. Grimes * IP_FORWARDING flag to prevent infinite recursion. 399df8bae1dSRodney W. Grimes * 400df8bae1dSRodney W. Grimes * Multicasts that are looped back by ip_mloopback(), 401df8bae1dSRodney W. Grimes * above, will be forwarded by the ip_input() routine, 402df8bae1dSRodney W. Grimes * if necessary. 403df8bae1dSRodney W. Grimes */ 404df8bae1dSRodney W. Grimes if (ip_mrouter && (flags & IP_FORWARDING) == 0) { 405f0068c4aSGarrett Wollman /* 406bbb4330bSLuigi Rizzo * If rsvp daemon is not running, do not 407f0068c4aSGarrett Wollman * set ip_moptions. This ensures that the packet 408f0068c4aSGarrett Wollman * is multicast and not just sent down one link 409f0068c4aSGarrett Wollman * as prescribed by rsvpd. 410f0068c4aSGarrett Wollman */ 411b124e4f2SGarrett Wollman if (!rsvp_on) 412f0068c4aSGarrett Wollman imo = NULL; 413bbb4330bSLuigi Rizzo if (ip_mforward && 414bbb4330bSLuigi Rizzo ip_mforward(ip, ifp, m, imo) != 0) { 415df8bae1dSRodney W. Grimes m_freem(m); 416df8bae1dSRodney W. Grimes goto done; 417df8bae1dSRodney W. Grimes } 418df8bae1dSRodney W. Grimes } 419df8bae1dSRodney W. Grimes } 4205e9ae478SGarrett Wollman 421df8bae1dSRodney W. Grimes /* 422df8bae1dSRodney W. Grimes * Multicasts with a time-to-live of zero may be looped- 423df8bae1dSRodney W. Grimes * back, above, but must not be transmitted on a network. 424df8bae1dSRodney W. Grimes * Also, multicasts addressed to the loopback interface 425df8bae1dSRodney W. Grimes * are not sent -- the above call to ip_mloopback() will 426df8bae1dSRodney W. Grimes * loop back a copy if this host actually belongs to the 427df8bae1dSRodney W. Grimes * destination group on the loopback interface. 428df8bae1dSRodney W. Grimes */ 429f5fea3ddSPaul Traina if (ip->ip_ttl == 0 || ifp->if_flags & IFF_LOOPBACK) { 430df8bae1dSRodney W. Grimes m_freem(m); 431df8bae1dSRodney W. Grimes goto done; 432df8bae1dSRodney W. Grimes } 433df8bae1dSRodney W. Grimes 434df8bae1dSRodney W. Grimes goto sendit; 435df8bae1dSRodney W. Grimes } 436df8bae1dSRodney W. Grimes #ifndef notdef 437df8bae1dSRodney W. Grimes /* 4382b25acc1SLuigi Rizzo * If the source address is not specified yet, use the address 4392b25acc1SLuigi Rizzo * of the outoing interface. In case, keep note we did that, so 4402b25acc1SLuigi Rizzo * if the the firewall changes the next-hop causing the output 4412b25acc1SLuigi Rizzo * interface to change, we can fix that. 442df8bae1dSRodney W. Grimes */ 443f9e354dfSJulian Elischer if (ip->ip_src.s_addr == INADDR_ANY) { 44438c1bc35SRuslan Ermilov /* Interface may have no addresses. */ 44538c1bc35SRuslan Ermilov if (ia != NULL) { 446df8bae1dSRodney W. Grimes ip->ip_src = IA_SIN(ia)->sin_addr; 4472b25acc1SLuigi Rizzo src_was_INADDR_ANY = 1; 448f9e354dfSJulian Elischer } 44938c1bc35SRuslan Ermilov } 450f9e354dfSJulian Elischer #endif /* notdef */ 451df8bae1dSRodney W. Grimes /* 452b5390296SDavid Greenman * Verify that we have any chance at all of being able to queue 453b5390296SDavid Greenman * the packet or packet fragments 454b5390296SDavid Greenman */ 455b5390296SDavid Greenman if ((ifp->if_snd.ifq_len + ip->ip_len / ifp->if_mtu + 1) >= 456b5390296SDavid Greenman ifp->if_snd.ifq_maxlen) { 457b5390296SDavid Greenman error = ENOBUFS; 45835609d45SJonathan Lemon ipstat.ips_odropped++; 459b5390296SDavid Greenman goto bad; 460b5390296SDavid Greenman } 461b5390296SDavid Greenman 462b5390296SDavid Greenman /* 463df8bae1dSRodney W. Grimes * Look for broadcast address and 4648c3f5566SRuslan Ermilov * verify user is allowed to send 465df8bae1dSRodney W. Grimes * such a packet. 466df8bae1dSRodney W. Grimes */ 4679f9b3dc4SGarrett Wollman if (isbroadcast) { 468df8bae1dSRodney W. Grimes if ((ifp->if_flags & IFF_BROADCAST) == 0) { 469df8bae1dSRodney W. Grimes error = EADDRNOTAVAIL; 470df8bae1dSRodney W. Grimes goto bad; 471df8bae1dSRodney W. Grimes } 472df8bae1dSRodney W. Grimes if ((flags & IP_ALLOWBROADCAST) == 0) { 473df8bae1dSRodney W. Grimes error = EACCES; 474df8bae1dSRodney W. Grimes goto bad; 475df8bae1dSRodney W. Grimes } 476df8bae1dSRodney W. Grimes /* don't allow broadcast messages to be fragmented */ 4771e78ac21SJeffrey Hsu if (ip->ip_len > ifp->if_mtu) { 478df8bae1dSRodney W. Grimes error = EMSGSIZE; 479df8bae1dSRodney W. Grimes goto bad; 480df8bae1dSRodney W. Grimes } 4818afa2304SBruce M Simpson if (flags & IP_SENDONES) 4828afa2304SBruce M Simpson ip->ip_dst.s_addr = INADDR_BROADCAST; 483df8bae1dSRodney W. Grimes m->m_flags |= M_BCAST; 4849f9b3dc4SGarrett Wollman } else { 485df8bae1dSRodney W. Grimes m->m_flags &= ~M_BCAST; 4869f9b3dc4SGarrett Wollman } 487df8bae1dSRodney W. Grimes 488f1743588SDarren Reed sendit: 48933841545SHajimu UMEMOTO #ifdef IPSEC 49033841545SHajimu UMEMOTO /* get SP for this packet */ 491340c35deSJonathan Lemon if (inp == NULL) 49233841545SHajimu UMEMOTO sp = ipsec4_getpolicybyaddr(m, IPSEC_DIR_OUTBOUND, flags, &error); 49333841545SHajimu UMEMOTO else 494340c35deSJonathan Lemon sp = ipsec4_getpolicybypcb(m, IPSEC_DIR_OUTBOUND, inp, &error); 49533841545SHajimu UMEMOTO 49633841545SHajimu UMEMOTO if (sp == NULL) { 49733841545SHajimu UMEMOTO ipsecstat.out_inval++; 49833841545SHajimu UMEMOTO goto bad; 49933841545SHajimu UMEMOTO } 50033841545SHajimu UMEMOTO 50133841545SHajimu UMEMOTO error = 0; 50233841545SHajimu UMEMOTO 50333841545SHajimu UMEMOTO /* check policy */ 50433841545SHajimu UMEMOTO switch (sp->policy) { 50533841545SHajimu UMEMOTO case IPSEC_POLICY_DISCARD: 50633841545SHajimu UMEMOTO /* 50733841545SHajimu UMEMOTO * This packet is just discarded. 50833841545SHajimu UMEMOTO */ 50933841545SHajimu UMEMOTO ipsecstat.out_polvio++; 51033841545SHajimu UMEMOTO goto bad; 51133841545SHajimu UMEMOTO 51233841545SHajimu UMEMOTO case IPSEC_POLICY_BYPASS: 51333841545SHajimu UMEMOTO case IPSEC_POLICY_NONE: 51433841545SHajimu UMEMOTO /* no need to do IPsec. */ 51533841545SHajimu UMEMOTO goto skip_ipsec; 51633841545SHajimu UMEMOTO 51733841545SHajimu UMEMOTO case IPSEC_POLICY_IPSEC: 51833841545SHajimu UMEMOTO if (sp->req == NULL) { 51933841545SHajimu UMEMOTO /* acquire a policy */ 52033841545SHajimu UMEMOTO error = key_spdacquire(sp); 52133841545SHajimu UMEMOTO goto bad; 52233841545SHajimu UMEMOTO } 52333841545SHajimu UMEMOTO break; 52433841545SHajimu UMEMOTO 52533841545SHajimu UMEMOTO case IPSEC_POLICY_ENTRUST: 52633841545SHajimu UMEMOTO default: 52733841545SHajimu UMEMOTO printf("ip_output: Invalid policy found. %d\n", sp->policy); 52833841545SHajimu UMEMOTO } 52933841545SHajimu UMEMOTO { 53033841545SHajimu UMEMOTO struct ipsec_output_state state; 53133841545SHajimu UMEMOTO bzero(&state, sizeof(state)); 53233841545SHajimu UMEMOTO state.m = m; 53333841545SHajimu UMEMOTO if (flags & IP_ROUTETOIF) { 53433841545SHajimu UMEMOTO state.ro = &iproute; 53533841545SHajimu UMEMOTO bzero(&iproute, sizeof(iproute)); 53633841545SHajimu UMEMOTO } else 53733841545SHajimu UMEMOTO state.ro = ro; 53833841545SHajimu UMEMOTO state.dst = (struct sockaddr *)dst; 53933841545SHajimu UMEMOTO 54033841545SHajimu UMEMOTO ip->ip_sum = 0; 54133841545SHajimu UMEMOTO 54233841545SHajimu UMEMOTO /* 54333841545SHajimu UMEMOTO * XXX 54433841545SHajimu UMEMOTO * delayed checksums are not currently compatible with IPsec 54533841545SHajimu UMEMOTO */ 54633841545SHajimu UMEMOTO if (m->m_pkthdr.csum_flags & CSUM_DELAY_DATA) { 54733841545SHajimu UMEMOTO in_delayed_cksum(m); 54833841545SHajimu UMEMOTO m->m_pkthdr.csum_flags &= ~CSUM_DELAY_DATA; 54933841545SHajimu UMEMOTO } 55033841545SHajimu UMEMOTO 551fd8e4ebcSMike Barcroft ip->ip_len = htons(ip->ip_len); 552fd8e4ebcSMike Barcroft ip->ip_off = htons(ip->ip_off); 55333841545SHajimu UMEMOTO 55433841545SHajimu UMEMOTO error = ipsec4_output(&state, sp, flags); 55533841545SHajimu UMEMOTO 55633841545SHajimu UMEMOTO m = state.m; 55733841545SHajimu UMEMOTO if (flags & IP_ROUTETOIF) { 55833841545SHajimu UMEMOTO /* 55933841545SHajimu UMEMOTO * if we have tunnel mode SA, we may need to ignore 56033841545SHajimu UMEMOTO * IP_ROUTETOIF. 56133841545SHajimu UMEMOTO */ 56233841545SHajimu UMEMOTO if (state.ro != &iproute || state.ro->ro_rt != NULL) { 56333841545SHajimu UMEMOTO flags &= ~IP_ROUTETOIF; 56433841545SHajimu UMEMOTO ro = state.ro; 56533841545SHajimu UMEMOTO } 56633841545SHajimu UMEMOTO } else 56733841545SHajimu UMEMOTO ro = state.ro; 56833841545SHajimu UMEMOTO dst = (struct sockaddr_in *)state.dst; 56933841545SHajimu UMEMOTO if (error) { 57033841545SHajimu UMEMOTO /* mbuf is already reclaimed in ipsec4_output. */ 57133841545SHajimu UMEMOTO m0 = NULL; 57233841545SHajimu UMEMOTO switch (error) { 57333841545SHajimu UMEMOTO case EHOSTUNREACH: 57433841545SHajimu UMEMOTO case ENETUNREACH: 57533841545SHajimu UMEMOTO case EMSGSIZE: 57633841545SHajimu UMEMOTO case ENOBUFS: 57733841545SHajimu UMEMOTO case ENOMEM: 57833841545SHajimu UMEMOTO break; 57933841545SHajimu UMEMOTO default: 58033841545SHajimu UMEMOTO printf("ip4_output (ipsec): error code %d\n", error); 58133841545SHajimu UMEMOTO /*fall through*/ 58233841545SHajimu UMEMOTO case ENOENT: 58333841545SHajimu UMEMOTO /* don't show these error codes to the user */ 58433841545SHajimu UMEMOTO error = 0; 58533841545SHajimu UMEMOTO break; 58633841545SHajimu UMEMOTO } 58733841545SHajimu UMEMOTO goto bad; 58833841545SHajimu UMEMOTO } 58933841545SHajimu UMEMOTO } 59033841545SHajimu UMEMOTO 59133841545SHajimu UMEMOTO /* be sure to update variables that are affected by ipsec4_output() */ 59233841545SHajimu UMEMOTO ip = mtod(m, struct ip *); 59333841545SHajimu UMEMOTO hlen = ip->ip_hl << 2; 59433841545SHajimu UMEMOTO if (ro->ro_rt == NULL) { 59533841545SHajimu UMEMOTO if ((flags & IP_ROUTETOIF) == 0) { 59633841545SHajimu UMEMOTO printf("ip_output: " 59733841545SHajimu UMEMOTO "can't update route after IPsec processing\n"); 59833841545SHajimu UMEMOTO error = EHOSTUNREACH; /*XXX*/ 59933841545SHajimu UMEMOTO goto bad; 60033841545SHajimu UMEMOTO } 60133841545SHajimu UMEMOTO } else { 60233841545SHajimu UMEMOTO ia = ifatoia(ro->ro_rt->rt_ifa); 60333841545SHajimu UMEMOTO ifp = ro->ro_rt->rt_ifp; 60433841545SHajimu UMEMOTO } 60533841545SHajimu UMEMOTO 60633841545SHajimu UMEMOTO /* make it flipped, again. */ 607fd8e4ebcSMike Barcroft ip->ip_len = ntohs(ip->ip_len); 608fd8e4ebcSMike Barcroft ip->ip_off = ntohs(ip->ip_off); 60933841545SHajimu UMEMOTO skip_ipsec: 61033841545SHajimu UMEMOTO #endif /*IPSEC*/ 611b9234fafSSam Leffler #ifdef FAST_IPSEC 612b9234fafSSam Leffler /* 613b9234fafSSam Leffler * Check the security policy (SP) for the packet and, if 614b9234fafSSam Leffler * required, do IPsec-related processing. There are two 615b9234fafSSam Leffler * cases here; the first time a packet is sent through 616b9234fafSSam Leffler * it will be untagged and handled by ipsec4_checkpolicy. 617b9234fafSSam Leffler * If the packet is resubmitted to ip_output (e.g. after 618b9234fafSSam Leffler * AH, ESP, etc. processing), there will be a tag to bypass 619b9234fafSSam Leffler * the lookup and related policy checking. 620b9234fafSSam Leffler */ 621b9234fafSSam Leffler mtag = m_tag_find(m, PACKET_TAG_IPSEC_PENDING_TDB, NULL); 622b9234fafSSam Leffler s = splnet(); 623b9234fafSSam Leffler if (mtag != NULL) { 624b9234fafSSam Leffler tdbi = (struct tdb_ident *)(mtag + 1); 625b9234fafSSam Leffler sp = ipsec_getpolicy(tdbi, IPSEC_DIR_OUTBOUND); 626b9234fafSSam Leffler if (sp == NULL) 627b9234fafSSam Leffler error = -EINVAL; /* force silent drop */ 628b9234fafSSam Leffler m_tag_delete(m, mtag); 629b9234fafSSam Leffler } else { 630b9234fafSSam Leffler sp = ipsec4_checkpolicy(m, IPSEC_DIR_OUTBOUND, flags, 631b9234fafSSam Leffler &error, inp); 632b9234fafSSam Leffler } 633b9234fafSSam Leffler /* 634b9234fafSSam Leffler * There are four return cases: 635b9234fafSSam Leffler * sp != NULL apply IPsec policy 636b9234fafSSam Leffler * sp == NULL, error == 0 no IPsec handling needed 637b9234fafSSam Leffler * sp == NULL, error == -EINVAL discard packet w/o error 638b9234fafSSam Leffler * sp == NULL, error != 0 discard packet, report error 639b9234fafSSam Leffler */ 640b9234fafSSam Leffler if (sp != NULL) { 641b9234fafSSam Leffler /* Loop detection, check if ipsec processing already done */ 642b9234fafSSam Leffler KASSERT(sp->req != NULL, ("ip_output: no ipsec request")); 643b9234fafSSam Leffler for (mtag = m_tag_first(m); mtag != NULL; 644b9234fafSSam Leffler mtag = m_tag_next(m, mtag)) { 645b9234fafSSam Leffler if (mtag->m_tag_cookie != MTAG_ABI_COMPAT) 646b9234fafSSam Leffler continue; 647b9234fafSSam Leffler if (mtag->m_tag_id != PACKET_TAG_IPSEC_OUT_DONE && 648b9234fafSSam Leffler mtag->m_tag_id != PACKET_TAG_IPSEC_OUT_CRYPTO_NEEDED) 649b9234fafSSam Leffler continue; 650b9234fafSSam Leffler /* 651b9234fafSSam Leffler * Check if policy has an SA associated with it. 652b9234fafSSam Leffler * This can happen when an SP has yet to acquire 653b9234fafSSam Leffler * an SA; e.g. on first reference. If it occurs, 654b9234fafSSam Leffler * then we let ipsec4_process_packet do its thing. 655b9234fafSSam Leffler */ 656b9234fafSSam Leffler if (sp->req->sav == NULL) 657b9234fafSSam Leffler break; 658b9234fafSSam Leffler tdbi = (struct tdb_ident *)(mtag + 1); 659b9234fafSSam Leffler if (tdbi->spi == sp->req->sav->spi && 660b9234fafSSam Leffler tdbi->proto == sp->req->sav->sah->saidx.proto && 661ab94ca3cSSam Leffler bcmp(&tdbi->dst, &sp->req->sav->sah->saidx.dst, 662b9234fafSSam Leffler sizeof (union sockaddr_union)) == 0) { 663b9234fafSSam Leffler /* 664b9234fafSSam Leffler * No IPsec processing is needed, free 665b9234fafSSam Leffler * reference to SP. 666b9234fafSSam Leffler * 667b9234fafSSam Leffler * NB: null pointer to avoid free at 668b9234fafSSam Leffler * done: below. 669b9234fafSSam Leffler */ 670b9234fafSSam Leffler KEY_FREESP(&sp), sp = NULL; 671b9234fafSSam Leffler splx(s); 672b9234fafSSam Leffler goto spd_done; 673b9234fafSSam Leffler } 674b9234fafSSam Leffler } 675b9234fafSSam Leffler 676b9234fafSSam Leffler /* 677b9234fafSSam Leffler * Do delayed checksums now because we send before 678b9234fafSSam Leffler * this is done in the normal processing path. 679b9234fafSSam Leffler */ 680b9234fafSSam Leffler if (m->m_pkthdr.csum_flags & CSUM_DELAY_DATA) { 681b9234fafSSam Leffler in_delayed_cksum(m); 682b9234fafSSam Leffler m->m_pkthdr.csum_flags &= ~CSUM_DELAY_DATA; 683b9234fafSSam Leffler } 684b9234fafSSam Leffler 685b9234fafSSam Leffler ip->ip_len = htons(ip->ip_len); 686b9234fafSSam Leffler ip->ip_off = htons(ip->ip_off); 687b9234fafSSam Leffler 688b9234fafSSam Leffler /* NB: callee frees mbuf */ 689b9234fafSSam Leffler error = ipsec4_process_packet(m, sp->req, flags, 0); 6909359ad86SSam Leffler /* 6919359ad86SSam Leffler * Preserve KAME behaviour: ENOENT can be returned 6929359ad86SSam Leffler * when an SA acquire is in progress. Don't propagate 6939359ad86SSam Leffler * this to user-level; it confuses applications. 6949359ad86SSam Leffler * 6959359ad86SSam Leffler * XXX this will go away when the SADB is redone. 6969359ad86SSam Leffler */ 6979359ad86SSam Leffler if (error == ENOENT) 6989359ad86SSam Leffler error = 0; 699b9234fafSSam Leffler splx(s); 700b9234fafSSam Leffler goto done; 701b9234fafSSam Leffler } else { 702b9234fafSSam Leffler splx(s); 703b9234fafSSam Leffler 704b9234fafSSam Leffler if (error != 0) { 705b9234fafSSam Leffler /* 706b9234fafSSam Leffler * Hack: -EINVAL is used to signal that a packet 707b9234fafSSam Leffler * should be silently discarded. This is typically 708b9234fafSSam Leffler * because we asked key management for an SA and 709b9234fafSSam Leffler * it was delayed (e.g. kicked up to IKE). 710b9234fafSSam Leffler */ 711b9234fafSSam Leffler if (error == -EINVAL) 712b9234fafSSam Leffler error = 0; 713b9234fafSSam Leffler goto bad; 714b9234fafSSam Leffler } else { 715b9234fafSSam Leffler /* No IPsec processing for this packet. */ 716b9234fafSSam Leffler } 717b9234fafSSam Leffler #ifdef notyet 718b9234fafSSam Leffler /* 719b9234fafSSam Leffler * If deferred crypto processing is needed, check that 720b9234fafSSam Leffler * the interface supports it. 721b9234fafSSam Leffler */ 722b9234fafSSam Leffler mtag = m_tag_find(m, PACKET_TAG_IPSEC_OUT_CRYPTO_NEEDED, NULL); 723b9234fafSSam Leffler if (mtag != NULL && (ifp->if_capenable & IFCAP_IPSEC) == 0) { 724b9234fafSSam Leffler /* notify IPsec to do its own crypto */ 725b9234fafSSam Leffler ipsp_skipcrypto_unmark((struct tdb_ident *)(mtag + 1)); 726b9234fafSSam Leffler error = EHOSTUNREACH; 727b9234fafSSam Leffler goto bad; 728b9234fafSSam Leffler } 729b9234fafSSam Leffler #endif 730b9234fafSSam Leffler } 731b9234fafSSam Leffler spd_done: 732b9234fafSSam Leffler #endif /* FAST_IPSEC */ 73333841545SHajimu UMEMOTO 734fed1c7e9SSøren Schmidt /* 735fed1c7e9SSøren Schmidt * IpHack's section. 736fed1c7e9SSøren Schmidt * - Xlate: translate packet's addr/port (NAT). 737e4676ba6SJulian Elischer * - Firewall: deny/allow/etc. 738fed1c7e9SSøren Schmidt * - Wrap: fake packet's addr/port <unimpl.> 739fed1c7e9SSøren Schmidt * - Encapsulate: put it in another IP and send out. <unimp.> 740fed1c7e9SSøren Schmidt */ 741c4ac87eaSDarren Reed #ifdef PFIL_HOOKS 742c4ac87eaSDarren Reed /* 743c4ac87eaSDarren Reed * Run through list of hooks for output packets. 744c4ac87eaSDarren Reed */ 745134ea224SSam Leffler error = pfil_run_hooks(&inet_pfil_hook, &m, ifp, PFIL_OUT); 746134ea224SSam Leffler if (error != 0 || m == NULL) 747c4ac87eaSDarren Reed goto done; 748c4ac87eaSDarren Reed ip = mtod(m, struct ip *); 749c4ac87eaSDarren Reed #endif /* PFIL_HOOKS */ 750fed1c7e9SSøren Schmidt 751df8bae1dSRodney W. Grimes /* 752e7319babSPoul-Henning Kamp * Check with the firewall... 7533efc3014SJulian Elischer * but not if we are already being fwd'd from a firewall. 754e7319babSPoul-Henning Kamp */ 7552b25acc1SLuigi Rizzo if (fw_enable && IPFW_LOADED && !args.next_hop) { 7569de9737fSPeter Wemm struct sockaddr_in *old = dst; 757b715f178SLuigi Rizzo 7582b25acc1SLuigi Rizzo args.m = m; 7592b25acc1SLuigi Rizzo args.next_hop = dst; 7602b25acc1SLuigi Rizzo args.oif = ifp; 7612b25acc1SLuigi Rizzo off = ip_fw_chk_ptr(&args); 7622b25acc1SLuigi Rizzo m = args.m; 7632b25acc1SLuigi Rizzo dst = args.next_hop; 7642b25acc1SLuigi Rizzo 765b715f178SLuigi Rizzo /* 766b715f178SLuigi Rizzo * On return we must do the following: 767507b4b54SLuigi Rizzo * m == NULL -> drop the pkt (old interface, deprecated) 768aa1f5daaSJonathan Lemon * (off & IP_FW_PORT_DENY_FLAG) -> drop the pkt (new interface) 769b715f178SLuigi Rizzo * 1<=off<= 0xffff -> DIVERT 770aa1f5daaSJonathan Lemon * (off & IP_FW_PORT_DYNT_FLAG) -> send to a DUMMYNET pipe 771aa1f5daaSJonathan Lemon * (off & IP_FW_PORT_TEE_FLAG) -> TEE the packet 772b715f178SLuigi Rizzo * dst != old -> IPFIREWALL_FORWARD 773b715f178SLuigi Rizzo * off==0, dst==old -> accept 774aa1f5daaSJonathan Lemon * If some of the above modules are not compiled in, then 775b715f178SLuigi Rizzo * we should't have to check the corresponding condition 776b715f178SLuigi Rizzo * (because the ipfw control socket should not accept 777b715f178SLuigi Rizzo * unsupported rules), but better play safe and drop 778b715f178SLuigi Rizzo * packets in case of doubt. 779b715f178SLuigi Rizzo */ 780d60315beSLuigi Rizzo if ( (off & IP_FW_PORT_DENY_FLAG) || m == NULL) { 781507b4b54SLuigi Rizzo if (m) 782507b4b54SLuigi Rizzo m_freem(m); 783507b4b54SLuigi Rizzo error = EACCES; 784507b4b54SLuigi Rizzo goto done; 785507b4b54SLuigi Rizzo } 786d60315beSLuigi Rizzo ip = mtod(m, struct ip *); 787b715f178SLuigi Rizzo if (off == 0 && dst == old) /* common case */ 788b715f178SLuigi Rizzo goto pass; 7897b109fa4SLuigi Rizzo if (DUMMYNET_LOADED && (off & IP_FW_PORT_DYNT_FLAG) != 0) { 790b715f178SLuigi Rizzo /* 791b715f178SLuigi Rizzo * pass the pkt to dummynet. Need to include 792f0a53591SLuigi Rizzo * pipe number, m, ifp, ro, dst because these are 793b715f178SLuigi Rizzo * not recomputed in the next pass. 794b715f178SLuigi Rizzo * All other parameters have been already used and 795b715f178SLuigi Rizzo * so they are not needed anymore. 796b715f178SLuigi Rizzo * XXX note: if the ifp or ro entry are deleted 797b715f178SLuigi Rizzo * while a pkt is in dummynet, we are in trouble! 798b715f178SLuigi Rizzo */ 7992b25acc1SLuigi Rizzo args.ro = ro; 8002b25acc1SLuigi Rizzo args.dst = dst; 8012b25acc1SLuigi Rizzo args.flags = flags; 8022b25acc1SLuigi Rizzo 8032b25acc1SLuigi Rizzo error = ip_dn_io_ptr(m, off & 0xffff, DN_TO_IP_OUT, 8042b25acc1SLuigi Rizzo &args); 805b715f178SLuigi Rizzo goto done; 806b715f178SLuigi Rizzo } 807b715f178SLuigi Rizzo #ifdef IPDIVERT 8088948e4baSArchie Cobbs if (off != 0 && (off & IP_FW_PORT_DYNT_FLAG) == 0) { 8098948e4baSArchie Cobbs struct mbuf *clone = NULL; 8108948e4baSArchie Cobbs 8118948e4baSArchie Cobbs /* Clone packet if we're doing a 'tee' */ 8128948e4baSArchie Cobbs if ((off & IP_FW_PORT_TEE_FLAG) != 0) 813a163d034SWarner Losh clone = m_dup(m, M_DONTWAIT); 8148948e4baSArchie Cobbs 815ea53ecd9SJonathan Lemon /* 816ea53ecd9SJonathan Lemon * XXX 817ea53ecd9SJonathan Lemon * delayed checksums are not currently compatible 818ea53ecd9SJonathan Lemon * with divert sockets. 819ea53ecd9SJonathan Lemon */ 820ea53ecd9SJonathan Lemon if (m->m_pkthdr.csum_flags & CSUM_DELAY_DATA) { 821ea53ecd9SJonathan Lemon in_delayed_cksum(m); 822ea53ecd9SJonathan Lemon m->m_pkthdr.csum_flags &= ~CSUM_DELAY_DATA; 823ea53ecd9SJonathan Lemon } 824ea53ecd9SJonathan Lemon 8258948e4baSArchie Cobbs /* Restore packet header fields to original values */ 826fd8e4ebcSMike Barcroft ip->ip_len = htons(ip->ip_len); 827fd8e4ebcSMike Barcroft ip->ip_off = htons(ip->ip_off); 8288948e4baSArchie Cobbs 8298948e4baSArchie Cobbs /* Deliver packet to divert input routine */ 8302b25acc1SLuigi Rizzo divert_packet(m, 0, off & 0xffff, args.divert_rule); 8318948e4baSArchie Cobbs 8328948e4baSArchie Cobbs /* If 'tee', continue with original packet */ 8338948e4baSArchie Cobbs if (clone != NULL) { 8348948e4baSArchie Cobbs m = clone; 8358948e4baSArchie Cobbs ip = mtod(m, struct ip *); 8368948e4baSArchie Cobbs goto pass; 8378948e4baSArchie Cobbs } 838b715f178SLuigi Rizzo goto done; 839b715f178SLuigi Rizzo } 840b715f178SLuigi Rizzo #endif 841b715f178SLuigi Rizzo 8422b25acc1SLuigi Rizzo /* IPFIREWALL_FORWARD */ 8432b25acc1SLuigi Rizzo /* 8442b25acc1SLuigi Rizzo * Check dst to make sure it is directly reachable on the 845f9e354dfSJulian Elischer * interface we previously thought it was. 846f9e354dfSJulian Elischer * If it isn't (which may be likely in some situations) we have 847f9e354dfSJulian Elischer * to re-route it (ie, find a route for the next-hop and the 848f9e354dfSJulian Elischer * associated interface) and set them here. This is nested 849f9e354dfSJulian Elischer * forwarding which in most cases is undesirable, except where 850f9e354dfSJulian Elischer * such control is nigh impossible. So we do it here. 851f9e354dfSJulian Elischer * And I'm babbling. 852f9e354dfSJulian Elischer */ 8532b25acc1SLuigi Rizzo if (off == 0 && old != dst) { /* FORWARD, dst has changed */ 8542b25acc1SLuigi Rizzo #if 0 8552b25acc1SLuigi Rizzo /* 8562b25acc1SLuigi Rizzo * XXX To improve readability, this block should be 8572b25acc1SLuigi Rizzo * changed into a function call as below: 8582b25acc1SLuigi Rizzo */ 8592b25acc1SLuigi Rizzo error = ip_ipforward(&m, &dst, &ifp); 8602b25acc1SLuigi Rizzo if (error) 8612b25acc1SLuigi Rizzo goto bad; 8622b25acc1SLuigi Rizzo if (m == NULL) /* ip_input consumed the mbuf */ 8632b25acc1SLuigi Rizzo goto done; 8642b25acc1SLuigi Rizzo #else 865f9e354dfSJulian Elischer struct in_ifaddr *ia; 866f9e354dfSJulian Elischer 8672b25acc1SLuigi Rizzo /* 8682b25acc1SLuigi Rizzo * XXX sro_fwd below is static, and a pointer 8692b25acc1SLuigi Rizzo * to it gets passed to routines downstream. 8702b25acc1SLuigi Rizzo * This could have surprisingly bad results in 8712b25acc1SLuigi Rizzo * practice, because its content is overwritten 8722b25acc1SLuigi Rizzo * by subsequent packets. 8732b25acc1SLuigi Rizzo */ 874f9e354dfSJulian Elischer /* There must be a better way to do this next line... */ 8752b25acc1SLuigi Rizzo static struct route sro_fwd; 8762b25acc1SLuigi Rizzo struct route *ro_fwd = &sro_fwd; 8772b25acc1SLuigi Rizzo 8782b25acc1SLuigi Rizzo #if 0 8792b25acc1SLuigi Rizzo print_ip("IPFIREWALL_FORWARD: New dst ip: ", 8802b25acc1SLuigi Rizzo dst->sin_addr, "\n"); 881f9e354dfSJulian Elischer #endif 8822b25acc1SLuigi Rizzo 883f9e354dfSJulian Elischer /* 884f9e354dfSJulian Elischer * We need to figure out if we have been forwarded 8852b25acc1SLuigi Rizzo * to a local socket. If so, then we should somehow 886f9e354dfSJulian Elischer * "loop back" to ip_input, and get directed to the 887f9e354dfSJulian Elischer * PCB as if we had received this packet. This is 888f9e354dfSJulian Elischer * because it may be dificult to identify the packets 889f9e354dfSJulian Elischer * you want to forward until they are being output 890f9e354dfSJulian Elischer * and have selected an interface. (e.g. locally 891f9e354dfSJulian Elischer * initiated packets) If we used the loopback inteface, 892f9e354dfSJulian Elischer * we would not be able to control what happens 893f9e354dfSJulian Elischer * as the packet runs through ip_input() as 8949d5abbddSJens Schweikhardt * it is done through an ISR. 895f9e354dfSJulian Elischer */ 896ca925d9cSJonathan Lemon LIST_FOREACH(ia, 897ca925d9cSJonathan Lemon INADDR_HASH(dst->sin_addr.s_addr), ia_hash) { 898f9e354dfSJulian Elischer /* 899f9e354dfSJulian Elischer * If the addr to forward to is one 900f9e354dfSJulian Elischer * of ours, we pretend to 901f9e354dfSJulian Elischer * be the destination for this packet. 902f9e354dfSJulian Elischer */ 903f9e354dfSJulian Elischer if (IA_SIN(ia)->sin_addr.s_addr == 904f9e354dfSJulian Elischer dst->sin_addr.s_addr) 905f9e354dfSJulian Elischer break; 906f9e354dfSJulian Elischer } 9072b25acc1SLuigi Rizzo if (ia) { /* tell ip_input "dont filter" */ 9082b25acc1SLuigi Rizzo struct m_hdr tag; 9092b25acc1SLuigi Rizzo 9102b25acc1SLuigi Rizzo tag.mh_type = MT_TAG; 9112b25acc1SLuigi Rizzo tag.mh_flags = PACKET_TAG_IPFORWARD; 9122b25acc1SLuigi Rizzo tag.mh_data = (caddr_t)args.next_hop; 9132b25acc1SLuigi Rizzo tag.mh_next = m; 9142b25acc1SLuigi Rizzo 915f9e354dfSJulian Elischer if (m->m_pkthdr.rcvif == NULL) 916f9e354dfSJulian Elischer m->m_pkthdr.rcvif = ifunit("lo0"); 91720c822f3SJonathan Lemon if (m->m_pkthdr.csum_flags & CSUM_DELAY_DATA) { 91820c822f3SJonathan Lemon m->m_pkthdr.csum_flags |= 91920c822f3SJonathan Lemon CSUM_DATA_VALID | CSUM_PSEUDO_HDR; 92020c822f3SJonathan Lemon m0->m_pkthdr.csum_data = 0xffff; 92120c822f3SJonathan Lemon } 92220c822f3SJonathan Lemon m->m_pkthdr.csum_flags |= 92320c822f3SJonathan Lemon CSUM_IP_CHECKED | CSUM_IP_VALID; 924fd8e4ebcSMike Barcroft ip->ip_len = htons(ip->ip_len); 925fd8e4ebcSMike Barcroft ip->ip_off = htons(ip->ip_off); 9262b25acc1SLuigi Rizzo ip_input((struct mbuf *)&tag); 927f9e354dfSJulian Elischer goto done; 928f9e354dfSJulian Elischer } 929f9e354dfSJulian Elischer /* Some of the logic for this was 930f9e354dfSJulian Elischer * nicked from above. 931f9e354dfSJulian Elischer * 932f9e354dfSJulian Elischer * This rewrites the cached route in a local PCB. 933f9e354dfSJulian Elischer * Is this what we want to do? 934f9e354dfSJulian Elischer */ 935f9e354dfSJulian Elischer bcopy(dst, &ro_fwd->ro_dst, sizeof(*dst)); 936f9e354dfSJulian Elischer 937f9e354dfSJulian Elischer ro_fwd->ro_rt = 0; 938f9e354dfSJulian Elischer rtalloc_ign(ro_fwd, RTF_PRCLONING); 939f9e354dfSJulian Elischer 940f9e354dfSJulian Elischer if (ro_fwd->ro_rt == 0) { 941f9e354dfSJulian Elischer ipstat.ips_noroute++; 942f9e354dfSJulian Elischer error = EHOSTUNREACH; 943f9e354dfSJulian Elischer goto bad; 944f9e354dfSJulian Elischer } 945f9e354dfSJulian Elischer 946f9e354dfSJulian Elischer ia = ifatoia(ro_fwd->ro_rt->rt_ifa); 947f9e354dfSJulian Elischer ifp = ro_fwd->ro_rt->rt_ifp; 948f9e354dfSJulian Elischer ro_fwd->ro_rt->rt_use++; 949f9e354dfSJulian Elischer if (ro_fwd->ro_rt->rt_flags & RTF_GATEWAY) 9502b25acc1SLuigi Rizzo dst = (struct sockaddr_in *) 9512b25acc1SLuigi Rizzo ro_fwd->ro_rt->rt_gateway; 952f9e354dfSJulian Elischer if (ro_fwd->ro_rt->rt_flags & RTF_HOST) 953f9e354dfSJulian Elischer isbroadcast = 954f9e354dfSJulian Elischer (ro_fwd->ro_rt->rt_flags & RTF_BROADCAST); 955f9e354dfSJulian Elischer else 956f9e354dfSJulian Elischer isbroadcast = in_broadcast(dst->sin_addr, ifp); 9573f9e3122SYaroslav Tykhiy if (ro->ro_rt) 958f9e354dfSJulian Elischer RTFREE(ro->ro_rt); 959f9e354dfSJulian Elischer ro->ro_rt = ro_fwd->ro_rt; 960f9e354dfSJulian Elischer dst = (struct sockaddr_in *)&ro_fwd->ro_dst; 961f9e354dfSJulian Elischer 9622b25acc1SLuigi Rizzo #endif /* ... block to be put into a function */ 963f9e354dfSJulian Elischer /* 964f9e354dfSJulian Elischer * If we added a default src ip earlier, 965f9e354dfSJulian Elischer * which would have been gotten from the-then 966f9e354dfSJulian Elischer * interface, do it again, from the new one. 967f9e354dfSJulian Elischer */ 9682b25acc1SLuigi Rizzo if (src_was_INADDR_ANY) 969f9e354dfSJulian Elischer ip->ip_src = IA_SIN(ia)->sin_addr; 970b715f178SLuigi Rizzo goto pass ; 971f9e354dfSJulian Elischer } 9722b25acc1SLuigi Rizzo 973b715f178SLuigi Rizzo /* 974b715f178SLuigi Rizzo * if we get here, none of the above matches, and 975b715f178SLuigi Rizzo * we have to drop the pkt 976b715f178SLuigi Rizzo */ 977b715f178SLuigi Rizzo m_freem(m); 978b715f178SLuigi Rizzo error = EACCES; /* not sure this is the right error msg */ 979b715f178SLuigi Rizzo goto done; 98093e0e116SJulian Elischer } 981e7319babSPoul-Henning Kamp 982b715f178SLuigi Rizzo pass: 98351c8ec4aSRuslan Ermilov /* 127/8 must not appear on wire - RFC1122. */ 98451c8ec4aSRuslan Ermilov if ((ntohl(ip->ip_dst.s_addr) >> IN_CLASSA_NSHIFT) == IN_LOOPBACKNET || 98551c8ec4aSRuslan Ermilov (ntohl(ip->ip_src.s_addr) >> IN_CLASSA_NSHIFT) == IN_LOOPBACKNET) { 98651c8ec4aSRuslan Ermilov if ((ifp->if_flags & IFF_LOOPBACK) == 0) { 98751c8ec4aSRuslan Ermilov ipstat.ips_badaddr++; 98851c8ec4aSRuslan Ermilov error = EADDRNOTAVAIL; 98951c8ec4aSRuslan Ermilov goto bad; 99051c8ec4aSRuslan Ermilov } 99151c8ec4aSRuslan Ermilov } 99251c8ec4aSRuslan Ermilov 993206a3274SRuslan Ermilov m->m_pkthdr.csum_flags |= CSUM_IP; 994206a3274SRuslan Ermilov sw_csum = m->m_pkthdr.csum_flags & ~ifp->if_hwassist; 995db4f9cc7SJonathan Lemon if (sw_csum & CSUM_DELAY_DATA) { 996db4f9cc7SJonathan Lemon in_delayed_cksum(m); 997db4f9cc7SJonathan Lemon sw_csum &= ~CSUM_DELAY_DATA; 998db4f9cc7SJonathan Lemon } 999206a3274SRuslan Ermilov m->m_pkthdr.csum_flags &= ifp->if_hwassist; 1000db4f9cc7SJonathan Lemon 1001e7319babSPoul-Henning Kamp /* 1002db4f9cc7SJonathan Lemon * If small enough for interface, or the interface will take 1003db4f9cc7SJonathan Lemon * care of the fragmentation for us, can just send directly. 1004df8bae1dSRodney W. Grimes */ 10051e78ac21SJeffrey Hsu if (ip->ip_len <= ifp->if_mtu || ifp->if_hwassist & CSUM_FRAGMENT) { 1006fd8e4ebcSMike Barcroft ip->ip_len = htons(ip->ip_len); 1007fd8e4ebcSMike Barcroft ip->ip_off = htons(ip->ip_off); 1008df8bae1dSRodney W. Grimes ip->ip_sum = 0; 100953be11f6SPoul-Henning Kamp if (sw_csum & CSUM_DELAY_IP) 1010df8bae1dSRodney W. Grimes ip->ip_sum = in_cksum(m, hlen); 10115da9f8faSJosef Karthauser 10125da9f8faSJosef Karthauser /* Record statistics for this interface address. */ 101338c1bc35SRuslan Ermilov if (!(flags & IP_FORWARDING) && ia) { 10145da9f8faSJosef Karthauser ia->ia_ifa.if_opackets++; 10155da9f8faSJosef Karthauser ia->ia_ifa.if_obytes += m->m_pkthdr.len; 10165da9f8faSJosef Karthauser } 10175da9f8faSJosef Karthauser 101833841545SHajimu UMEMOTO #ifdef IPSEC 101933841545SHajimu UMEMOTO /* clean ipsec history once it goes out of the node */ 102033841545SHajimu UMEMOTO ipsec_delaux(m); 102133841545SHajimu UMEMOTO #endif 102233841545SHajimu UMEMOTO 102353dcc544SMike Silbersack #ifdef MBUF_STRESS_TEST 10243390d476SMike Silbersack if (mbuf_frag_size && m->m_pkthdr.len > mbuf_frag_size) 10253390d476SMike Silbersack m = m_fragment(m, M_DONTWAIT, mbuf_frag_size); 10269d9edc56SMike Silbersack #endif 1027df8bae1dSRodney W. Grimes error = (*ifp->if_output)(ifp, m, 1028df8bae1dSRodney W. Grimes (struct sockaddr *)dst, ro->ro_rt); 1029df8bae1dSRodney W. Grimes goto done; 1030df8bae1dSRodney W. Grimes } 10311e78ac21SJeffrey Hsu 1032df8bae1dSRodney W. Grimes if (ip->ip_off & IP_DF) { 1033df8bae1dSRodney W. Grimes error = EMSGSIZE; 10343d1f141bSGarrett Wollman /* 10353d1f141bSGarrett Wollman * This case can happen if the user changed the MTU 10363d1f141bSGarrett Wollman * of an interface after enabling IP on it. Because 10373d1f141bSGarrett Wollman * most netifs don't keep track of routes pointing to 10383d1f141bSGarrett Wollman * them, there is no way for one to update all its 10393d1f141bSGarrett Wollman * routes when the MTU is changed. 10403d1f141bSGarrett Wollman */ 10411e78ac21SJeffrey Hsu if ((ro->ro_rt->rt_flags & (RTF_UP | RTF_HOST)) && 10421e78ac21SJeffrey Hsu !(ro->ro_rt->rt_rmx.rmx_locks & RTV_MTU) && 10431e78ac21SJeffrey Hsu (ro->ro_rt->rt_rmx.rmx_mtu > ifp->if_mtu)) { 10443d1f141bSGarrett Wollman ro->ro_rt->rt_rmx.rmx_mtu = ifp->if_mtu; 10453d1f141bSGarrett Wollman } 1046df8bae1dSRodney W. Grimes ipstat.ips_cantfrag++; 1047df8bae1dSRodney W. Grimes goto bad; 1048df8bae1dSRodney W. Grimes } 10491e78ac21SJeffrey Hsu 10501e78ac21SJeffrey Hsu /* 10511e78ac21SJeffrey Hsu * Too large for interface; fragment if possible. If successful, 10521e78ac21SJeffrey Hsu * on return, m will point to a list of packets to be sent. 10531e78ac21SJeffrey Hsu */ 10541e78ac21SJeffrey Hsu error = ip_fragment(ip, &m, ifp->if_mtu, ifp->if_hwassist, sw_csum); 10551e78ac21SJeffrey Hsu if (error) 1056df8bae1dSRodney W. Grimes goto bad; 10571e78ac21SJeffrey Hsu for (; m; m = m0) { 1058df8bae1dSRodney W. Grimes m0 = m->m_nextpkt; 1059b375c9ecSLuigi Rizzo m->m_nextpkt = 0; 106033841545SHajimu UMEMOTO #ifdef IPSEC 106133841545SHajimu UMEMOTO /* clean ipsec history once it goes out of the node */ 106233841545SHajimu UMEMOTO ipsec_delaux(m); 106333841545SHajimu UMEMOTO #endif 106407203494SDaniel C. Sobral if (error == 0) { 1065fe937674SJosef Karthauser /* Record statistics for this interface address. */ 106607203494SDaniel C. Sobral if (ia != NULL) { 1067fe937674SJosef Karthauser ia->ia_ifa.if_opackets++; 1068fe937674SJosef Karthauser ia->ia_ifa.if_obytes += m->m_pkthdr.len; 106907203494SDaniel C. Sobral } 1070fe937674SJosef Karthauser 1071df8bae1dSRodney W. Grimes error = (*ifp->if_output)(ifp, m, 1072df8bae1dSRodney W. Grimes (struct sockaddr *)dst, ro->ro_rt); 1073fe937674SJosef Karthauser } else 1074df8bae1dSRodney W. Grimes m_freem(m); 1075df8bae1dSRodney W. Grimes } 1076df8bae1dSRodney W. Grimes 1077df8bae1dSRodney W. Grimes if (error == 0) 1078df8bae1dSRodney W. Grimes ipstat.ips_fragmented++; 10791e78ac21SJeffrey Hsu 1080df8bae1dSRodney W. Grimes done: 10816a800098SYoshinobu Inoue #ifdef IPSEC 10826a800098SYoshinobu Inoue if (ro == &iproute && ro->ro_rt) { 10836a800098SYoshinobu Inoue RTFREE(ro->ro_rt); 10846a800098SYoshinobu Inoue ro->ro_rt = NULL; 10856a800098SYoshinobu Inoue } 10866a800098SYoshinobu Inoue if (sp != NULL) { 10876a800098SYoshinobu Inoue KEYDEBUG(KEYDEBUG_IPSEC_STAMP, 10886a800098SYoshinobu Inoue printf("DP ip_output call free SP:%p\n", sp)); 10896a800098SYoshinobu Inoue key_freesp(sp); 10906a800098SYoshinobu Inoue } 10911e78ac21SJeffrey Hsu #endif 1092b9234fafSSam Leffler #ifdef FAST_IPSEC 1093b9234fafSSam Leffler if (ro == &iproute && ro->ro_rt) { 1094b9234fafSSam Leffler RTFREE(ro->ro_rt); 1095b9234fafSSam Leffler ro->ro_rt = NULL; 1096b9234fafSSam Leffler } 1097b9234fafSSam Leffler if (sp != NULL) 1098b9234fafSSam Leffler KEY_FREESP(&sp); 10991e78ac21SJeffrey Hsu #endif 1100df8bae1dSRodney W. Grimes return (error); 1101df8bae1dSRodney W. Grimes bad: 11023528d68fSBill Paul m_freem(m); 1103df8bae1dSRodney W. Grimes goto done; 1104df8bae1dSRodney W. Grimes } 1105df8bae1dSRodney W. Grimes 11061e78ac21SJeffrey Hsu /* 11071e78ac21SJeffrey Hsu * Create a chain of fragments which fit the given mtu. m_frag points to the 11081e78ac21SJeffrey Hsu * mbuf to be fragmented; on return it points to the chain with the fragments. 11091e78ac21SJeffrey Hsu * Return 0 if no error. If error, m_frag may contain a partially built 11101e78ac21SJeffrey Hsu * chain of fragments that should be freed by the caller. 11111e78ac21SJeffrey Hsu * 11121e78ac21SJeffrey Hsu * if_hwassist_flags is the hw offload capabilities (see if_data.ifi_hwassist) 11131e78ac21SJeffrey Hsu * sw_csum contains the delayed checksums flags (e.g., CSUM_DELAY_IP). 11141e78ac21SJeffrey Hsu */ 11151e78ac21SJeffrey Hsu int 11161e78ac21SJeffrey Hsu ip_fragment(struct ip *ip, struct mbuf **m_frag, int mtu, 11171e78ac21SJeffrey Hsu u_long if_hwassist_flags, int sw_csum) 11181e78ac21SJeffrey Hsu { 11191e78ac21SJeffrey Hsu int error = 0; 11201e78ac21SJeffrey Hsu int hlen = ip->ip_hl << 2; 11211e78ac21SJeffrey Hsu int len = (mtu - hlen) & ~7; /* size of payload in each fragment */ 11221e78ac21SJeffrey Hsu int off; 11231e78ac21SJeffrey Hsu struct mbuf *m0 = *m_frag; /* the original packet */ 11241e78ac21SJeffrey Hsu int firstlen; 11251e78ac21SJeffrey Hsu struct mbuf **mnext; 11261e78ac21SJeffrey Hsu int nfrags; 11271e78ac21SJeffrey Hsu 11281e78ac21SJeffrey Hsu if (ip->ip_off & IP_DF) { /* Fragmentation not allowed */ 11291e78ac21SJeffrey Hsu ipstat.ips_cantfrag++; 11301e78ac21SJeffrey Hsu return EMSGSIZE; 11311e78ac21SJeffrey Hsu } 11321e78ac21SJeffrey Hsu 11331e78ac21SJeffrey Hsu /* 11341e78ac21SJeffrey Hsu * Must be able to put at least 8 bytes per fragment. 11351e78ac21SJeffrey Hsu */ 11361e78ac21SJeffrey Hsu if (len < 8) 11371e78ac21SJeffrey Hsu return EMSGSIZE; 11381e78ac21SJeffrey Hsu 11391e78ac21SJeffrey Hsu /* 11401e78ac21SJeffrey Hsu * If the interface will not calculate checksums on 11411e78ac21SJeffrey Hsu * fragmented packets, then do it here. 11421e78ac21SJeffrey Hsu */ 11431e78ac21SJeffrey Hsu if (m0->m_pkthdr.csum_flags & CSUM_DELAY_DATA && 11441e78ac21SJeffrey Hsu (if_hwassist_flags & CSUM_IP_FRAGS) == 0) { 11451e78ac21SJeffrey Hsu in_delayed_cksum(m0); 11461e78ac21SJeffrey Hsu m0->m_pkthdr.csum_flags &= ~CSUM_DELAY_DATA; 11471e78ac21SJeffrey Hsu } 11481e78ac21SJeffrey Hsu 11491e78ac21SJeffrey Hsu if (len > PAGE_SIZE) { 11501e78ac21SJeffrey Hsu /* 11511e78ac21SJeffrey Hsu * Fragment large datagrams such that each segment 11521e78ac21SJeffrey Hsu * contains a multiple of PAGE_SIZE amount of data, 11531e78ac21SJeffrey Hsu * plus headers. This enables a receiver to perform 11541e78ac21SJeffrey Hsu * page-flipping zero-copy optimizations. 11551e78ac21SJeffrey Hsu * 11561e78ac21SJeffrey Hsu * XXX When does this help given that sender and receiver 11571e78ac21SJeffrey Hsu * could have different page sizes, and also mtu could 11581e78ac21SJeffrey Hsu * be less than the receiver's page size ? 11591e78ac21SJeffrey Hsu */ 11601e78ac21SJeffrey Hsu int newlen; 11611e78ac21SJeffrey Hsu struct mbuf *m; 11621e78ac21SJeffrey Hsu 11631e78ac21SJeffrey Hsu for (m = m0, off = 0; m && (off+m->m_len) <= mtu; m = m->m_next) 11641e78ac21SJeffrey Hsu off += m->m_len; 11651e78ac21SJeffrey Hsu 11661e78ac21SJeffrey Hsu /* 11671e78ac21SJeffrey Hsu * firstlen (off - hlen) must be aligned on an 11681e78ac21SJeffrey Hsu * 8-byte boundary 11691e78ac21SJeffrey Hsu */ 11701e78ac21SJeffrey Hsu if (off < hlen) 11711e78ac21SJeffrey Hsu goto smart_frag_failure; 11721e78ac21SJeffrey Hsu off = ((off - hlen) & ~7) + hlen; 11731e78ac21SJeffrey Hsu newlen = (~PAGE_MASK) & mtu; 11741e78ac21SJeffrey Hsu if ((newlen + sizeof (struct ip)) > mtu) { 11751e78ac21SJeffrey Hsu /* we failed, go back the default */ 11761e78ac21SJeffrey Hsu smart_frag_failure: 11771e78ac21SJeffrey Hsu newlen = len; 11781e78ac21SJeffrey Hsu off = hlen + len; 11791e78ac21SJeffrey Hsu } 11801e78ac21SJeffrey Hsu len = newlen; 11811e78ac21SJeffrey Hsu 11821e78ac21SJeffrey Hsu } else { 11831e78ac21SJeffrey Hsu off = hlen + len; 11841e78ac21SJeffrey Hsu } 11851e78ac21SJeffrey Hsu 11861e78ac21SJeffrey Hsu firstlen = off - hlen; 11871e78ac21SJeffrey Hsu mnext = &m0->m_nextpkt; /* pointer to next packet */ 11881e78ac21SJeffrey Hsu 11891e78ac21SJeffrey Hsu /* 11901e78ac21SJeffrey Hsu * Loop through length of segment after first fragment, 11911e78ac21SJeffrey Hsu * make new header and copy data of each part and link onto chain. 11921e78ac21SJeffrey Hsu * Here, m0 is the original packet, m is the fragment being created. 11931e78ac21SJeffrey Hsu * The fragments are linked off the m_nextpkt of the original 11941e78ac21SJeffrey Hsu * packet, which after processing serves as the first fragment. 11951e78ac21SJeffrey Hsu */ 11961e78ac21SJeffrey Hsu for (nfrags = 1; off < ip->ip_len; off += len, nfrags++) { 11971e78ac21SJeffrey Hsu struct ip *mhip; /* ip header on the fragment */ 11981e78ac21SJeffrey Hsu struct mbuf *m; 11991e78ac21SJeffrey Hsu int mhlen = sizeof (struct ip); 12001e78ac21SJeffrey Hsu 12011e78ac21SJeffrey Hsu MGETHDR(m, M_DONTWAIT, MT_HEADER); 12021e78ac21SJeffrey Hsu if (m == 0) { 12031e78ac21SJeffrey Hsu error = ENOBUFS; 12041e78ac21SJeffrey Hsu ipstat.ips_odropped++; 12051e78ac21SJeffrey Hsu goto done; 12061e78ac21SJeffrey Hsu } 12071e78ac21SJeffrey Hsu m->m_flags |= (m0->m_flags & M_MCAST) | M_FRAG; 12081e78ac21SJeffrey Hsu /* 12091e78ac21SJeffrey Hsu * In the first mbuf, leave room for the link header, then 12101e78ac21SJeffrey Hsu * copy the original IP header including options. The payload 12111e78ac21SJeffrey Hsu * goes into an additional mbuf chain returned by m_copy(). 12121e78ac21SJeffrey Hsu */ 12131e78ac21SJeffrey Hsu m->m_data += max_linkhdr; 12141e78ac21SJeffrey Hsu mhip = mtod(m, struct ip *); 12151e78ac21SJeffrey Hsu *mhip = *ip; 12161e78ac21SJeffrey Hsu if (hlen > sizeof (struct ip)) { 12171e78ac21SJeffrey Hsu mhlen = ip_optcopy(ip, mhip) + sizeof (struct ip); 12181e78ac21SJeffrey Hsu mhip->ip_v = IPVERSION; 12191e78ac21SJeffrey Hsu mhip->ip_hl = mhlen >> 2; 12201e78ac21SJeffrey Hsu } 12211e78ac21SJeffrey Hsu m->m_len = mhlen; 12221e78ac21SJeffrey Hsu /* XXX do we need to add ip->ip_off below ? */ 12231e78ac21SJeffrey Hsu mhip->ip_off = ((off - hlen) >> 3) + ip->ip_off; 12241e78ac21SJeffrey Hsu if (off + len >= ip->ip_len) { /* last fragment */ 12251e78ac21SJeffrey Hsu len = ip->ip_len - off; 12261e78ac21SJeffrey Hsu m->m_flags |= M_LASTFRAG; 12271e78ac21SJeffrey Hsu } else 12281e78ac21SJeffrey Hsu mhip->ip_off |= IP_MF; 12291e78ac21SJeffrey Hsu mhip->ip_len = htons((u_short)(len + mhlen)); 12301e78ac21SJeffrey Hsu m->m_next = m_copy(m0, off, len); 12311e78ac21SJeffrey Hsu if (m->m_next == 0) { /* copy failed */ 12321e78ac21SJeffrey Hsu m_free(m); 12331e78ac21SJeffrey Hsu error = ENOBUFS; /* ??? */ 12341e78ac21SJeffrey Hsu ipstat.ips_odropped++; 12351e78ac21SJeffrey Hsu goto done; 12361e78ac21SJeffrey Hsu } 12371e78ac21SJeffrey Hsu m->m_pkthdr.len = mhlen + len; 12381e78ac21SJeffrey Hsu m->m_pkthdr.rcvif = (struct ifnet *)0; 12391e78ac21SJeffrey Hsu #ifdef MAC 12401e78ac21SJeffrey Hsu mac_create_fragment(m0, m); 12411e78ac21SJeffrey Hsu #endif 12421e78ac21SJeffrey Hsu m->m_pkthdr.csum_flags = m0->m_pkthdr.csum_flags; 12431e78ac21SJeffrey Hsu mhip->ip_off = htons(mhip->ip_off); 12441e78ac21SJeffrey Hsu mhip->ip_sum = 0; 12451e78ac21SJeffrey Hsu if (sw_csum & CSUM_DELAY_IP) 12461e78ac21SJeffrey Hsu mhip->ip_sum = in_cksum(m, mhlen); 12471e78ac21SJeffrey Hsu *mnext = m; 12481e78ac21SJeffrey Hsu mnext = &m->m_nextpkt; 12491e78ac21SJeffrey Hsu } 12501e78ac21SJeffrey Hsu ipstat.ips_ofragments += nfrags; 12511e78ac21SJeffrey Hsu 12521e78ac21SJeffrey Hsu /* set first marker for fragment chain */ 12531e78ac21SJeffrey Hsu m0->m_flags |= M_FIRSTFRAG | M_FRAG; 12541e78ac21SJeffrey Hsu m0->m_pkthdr.csum_data = nfrags; 12551e78ac21SJeffrey Hsu 12561e78ac21SJeffrey Hsu /* 12571e78ac21SJeffrey Hsu * Update first fragment by trimming what's been copied out 12581e78ac21SJeffrey Hsu * and updating header. 12591e78ac21SJeffrey Hsu */ 12601e78ac21SJeffrey Hsu m_adj(m0, hlen + firstlen - ip->ip_len); 12611e78ac21SJeffrey Hsu m0->m_pkthdr.len = hlen + firstlen; 12621e78ac21SJeffrey Hsu ip->ip_len = htons((u_short)m0->m_pkthdr.len); 12631e78ac21SJeffrey Hsu ip->ip_off |= IP_MF; 12641e78ac21SJeffrey Hsu ip->ip_off = htons(ip->ip_off); 12651e78ac21SJeffrey Hsu ip->ip_sum = 0; 12661e78ac21SJeffrey Hsu if (sw_csum & CSUM_DELAY_IP) 12671e78ac21SJeffrey Hsu ip->ip_sum = in_cksum(m0, hlen); 12681e78ac21SJeffrey Hsu 12691e78ac21SJeffrey Hsu done: 12701e78ac21SJeffrey Hsu *m_frag = m0; 12711e78ac21SJeffrey Hsu return error; 12721e78ac21SJeffrey Hsu } 12731e78ac21SJeffrey Hsu 12741c238475SJonathan Lemon void 1275db4f9cc7SJonathan Lemon in_delayed_cksum(struct mbuf *m) 1276db4f9cc7SJonathan Lemon { 1277db4f9cc7SJonathan Lemon struct ip *ip; 1278db4f9cc7SJonathan Lemon u_short csum, offset; 1279db4f9cc7SJonathan Lemon 1280db4f9cc7SJonathan Lemon ip = mtod(m, struct ip *); 128153be11f6SPoul-Henning Kamp offset = ip->ip_hl << 2 ; 1282db4f9cc7SJonathan Lemon csum = in_cksum_skip(m, ip->ip_len, offset); 1283206a3274SRuslan Ermilov if (m->m_pkthdr.csum_flags & CSUM_UDP && csum == 0) 1284206a3274SRuslan Ermilov csum = 0xffff; 1285db4f9cc7SJonathan Lemon offset += m->m_pkthdr.csum_data; /* checksum offset */ 1286db4f9cc7SJonathan Lemon 1287db4f9cc7SJonathan Lemon if (offset + sizeof(u_short) > m->m_len) { 1288db4f9cc7SJonathan Lemon printf("delayed m_pullup, m->len: %d off: %d p: %d\n", 1289db4f9cc7SJonathan Lemon m->m_len, offset, ip->ip_p); 1290db4f9cc7SJonathan Lemon /* 1291db4f9cc7SJonathan Lemon * XXX 1292db4f9cc7SJonathan Lemon * this shouldn't happen, but if it does, the 1293db4f9cc7SJonathan Lemon * correct behavior may be to insert the checksum 1294db4f9cc7SJonathan Lemon * in the existing chain instead of rearranging it. 1295db4f9cc7SJonathan Lemon */ 1296db4f9cc7SJonathan Lemon m = m_pullup(m, offset + sizeof(u_short)); 1297db4f9cc7SJonathan Lemon } 1298db4f9cc7SJonathan Lemon *(u_short *)(m->m_data + offset) = csum; 1299db4f9cc7SJonathan Lemon } 1300db4f9cc7SJonathan Lemon 1301df8bae1dSRodney W. Grimes /* 1302df8bae1dSRodney W. Grimes * Insert IP options into preformed packet. 1303df8bae1dSRodney W. Grimes * Adjust IP destination as required for IP source routing, 1304df8bae1dSRodney W. Grimes * as indicated by a non-zero in_addr at the start of the options. 1305072b9b24SPaul Traina * 1306072b9b24SPaul Traina * XXX This routine assumes that the packet has no options in place. 1307df8bae1dSRodney W. Grimes */ 1308df8bae1dSRodney W. Grimes static struct mbuf * 1309b375c9ecSLuigi Rizzo ip_insertoptions(m, opt, phlen) 1310b375c9ecSLuigi Rizzo register struct mbuf *m; 1311b375c9ecSLuigi Rizzo struct mbuf *opt; 1312b375c9ecSLuigi Rizzo int *phlen; 1313df8bae1dSRodney W. Grimes { 1314b375c9ecSLuigi Rizzo register struct ipoption *p = mtod(opt, struct ipoption *); 1315df8bae1dSRodney W. Grimes struct mbuf *n; 1316b375c9ecSLuigi Rizzo register struct ip *ip = mtod(m, struct ip *); 1317df8bae1dSRodney W. Grimes unsigned optlen; 1318df8bae1dSRodney W. Grimes 1319df8bae1dSRodney W. Grimes optlen = opt->m_len - sizeof(p->ipopt_dst); 13201e78ac21SJeffrey Hsu if (optlen + ip->ip_len > IP_MAXPACKET) { 1321cb7641e8SMaxim Konovalov *phlen = 0; 1322df8bae1dSRodney W. Grimes return (m); /* XXX should fail */ 1323cb7641e8SMaxim Konovalov } 1324df8bae1dSRodney W. Grimes if (p->ipopt_dst.s_addr) 1325df8bae1dSRodney W. Grimes ip->ip_dst = p->ipopt_dst; 1326df8bae1dSRodney W. Grimes if (m->m_flags & M_EXT || m->m_data - optlen < m->m_pktdat) { 1327a163d034SWarner Losh MGETHDR(n, M_DONTWAIT, MT_HEADER); 1328cb7641e8SMaxim Konovalov if (n == 0) { 1329cb7641e8SMaxim Konovalov *phlen = 0; 1330df8bae1dSRodney W. Grimes return (m); 1331cb7641e8SMaxim Konovalov } 13325db1e34eSRuslan Ermilov n->m_pkthdr.rcvif = (struct ifnet *)0; 13334ed84624SRobert Watson #ifdef MAC 13344ed84624SRobert Watson mac_create_mbuf_from_mbuf(m, n); 13354ed84624SRobert Watson #endif 1336df8bae1dSRodney W. Grimes n->m_pkthdr.len = m->m_pkthdr.len + optlen; 1337df8bae1dSRodney W. Grimes m->m_len -= sizeof(struct ip); 1338df8bae1dSRodney W. Grimes m->m_data += sizeof(struct ip); 1339df8bae1dSRodney W. Grimes n->m_next = m; 1340df8bae1dSRodney W. Grimes m = n; 1341df8bae1dSRodney W. Grimes m->m_len = optlen + sizeof(struct ip); 1342df8bae1dSRodney W. Grimes m->m_data += max_linkhdr; 1343212059bdSDag-Erling Smørgrav bcopy(ip, mtod(m, void *), sizeof(struct ip)); 1344df8bae1dSRodney W. Grimes } else { 1345df8bae1dSRodney W. Grimes m->m_data -= optlen; 1346df8bae1dSRodney W. Grimes m->m_len += optlen; 1347df8bae1dSRodney W. Grimes m->m_pkthdr.len += optlen; 1348212059bdSDag-Erling Smørgrav bcopy(ip, mtod(m, void *), sizeof(struct ip)); 1349df8bae1dSRodney W. Grimes } 1350df8bae1dSRodney W. Grimes ip = mtod(m, struct ip *); 13510453d3cbSBruce Evans bcopy(p->ipopt_list, ip + 1, optlen); 1352df8bae1dSRodney W. Grimes *phlen = sizeof(struct ip) + optlen; 135353be11f6SPoul-Henning Kamp ip->ip_v = IPVERSION; 135453be11f6SPoul-Henning Kamp ip->ip_hl = *phlen >> 2; 1355df8bae1dSRodney W. Grimes ip->ip_len += optlen; 1356df8bae1dSRodney W. Grimes return (m); 1357df8bae1dSRodney W. Grimes } 1358df8bae1dSRodney W. Grimes 1359df8bae1dSRodney W. Grimes /* 1360df8bae1dSRodney W. Grimes * Copy options from ip to jp, 1361df8bae1dSRodney W. Grimes * omitting those not copied during fragmentation. 1362df8bae1dSRodney W. Grimes */ 1363beec8214SDarren Reed int 1364b375c9ecSLuigi Rizzo ip_optcopy(ip, jp) 1365b375c9ecSLuigi Rizzo struct ip *ip, *jp; 1366df8bae1dSRodney W. Grimes { 1367b375c9ecSLuigi Rizzo register u_char *cp, *dp; 1368df8bae1dSRodney W. Grimes int opt, optlen, cnt; 1369df8bae1dSRodney W. Grimes 1370df8bae1dSRodney W. Grimes cp = (u_char *)(ip + 1); 1371df8bae1dSRodney W. Grimes dp = (u_char *)(jp + 1); 137253be11f6SPoul-Henning Kamp cnt = (ip->ip_hl << 2) - sizeof (struct ip); 1373df8bae1dSRodney W. Grimes for (; cnt > 0; cnt -= optlen, cp += optlen) { 1374df8bae1dSRodney W. Grimes opt = cp[0]; 1375df8bae1dSRodney W. Grimes if (opt == IPOPT_EOL) 1376df8bae1dSRodney W. Grimes break; 1377df8bae1dSRodney W. Grimes if (opt == IPOPT_NOP) { 1378df8bae1dSRodney W. Grimes /* Preserve for IP mcast tunnel's LSRR alignment. */ 1379df8bae1dSRodney W. Grimes *dp++ = IPOPT_NOP; 1380df8bae1dSRodney W. Grimes optlen = 1; 1381df8bae1dSRodney W. Grimes continue; 1382686cdd19SJun-ichiro itojun Hagino } 1383db40007dSAndrew R. Reiter 1384db40007dSAndrew R. Reiter KASSERT(cnt >= IPOPT_OLEN + sizeof(*cp), 1385db40007dSAndrew R. Reiter ("ip_optcopy: malformed ipv4 option")); 1386df8bae1dSRodney W. Grimes optlen = cp[IPOPT_OLEN]; 1387db40007dSAndrew R. Reiter KASSERT(optlen >= IPOPT_OLEN + sizeof(*cp) && optlen <= cnt, 1388db40007dSAndrew R. Reiter ("ip_optcopy: malformed ipv4 option")); 1389db40007dSAndrew R. Reiter 1390df8bae1dSRodney W. Grimes /* bogus lengths should have been caught by ip_dooptions */ 1391df8bae1dSRodney W. Grimes if (optlen > cnt) 1392df8bae1dSRodney W. Grimes optlen = cnt; 1393df8bae1dSRodney W. Grimes if (IPOPT_COPIED(opt)) { 13940453d3cbSBruce Evans bcopy(cp, dp, optlen); 1395df8bae1dSRodney W. Grimes dp += optlen; 1396df8bae1dSRodney W. Grimes } 1397df8bae1dSRodney W. Grimes } 1398df8bae1dSRodney W. Grimes for (optlen = dp - (u_char *)(jp+1); optlen & 0x3; optlen++) 1399df8bae1dSRodney W. Grimes *dp++ = IPOPT_EOL; 1400df8bae1dSRodney W. Grimes return (optlen); 1401df8bae1dSRodney W. Grimes } 1402df8bae1dSRodney W. Grimes 1403df8bae1dSRodney W. Grimes /* 1404df8bae1dSRodney W. Grimes * IP socket option processing. 1405df8bae1dSRodney W. Grimes */ 1406df8bae1dSRodney W. Grimes int 1407b375c9ecSLuigi Rizzo ip_ctloutput(so, sopt) 1408b375c9ecSLuigi Rizzo struct socket *so; 1409b375c9ecSLuigi Rizzo struct sockopt *sopt; 1410df8bae1dSRodney W. Grimes { 1411cfe8b629SGarrett Wollman struct inpcb *inp = sotoinpcb(so); 1412cfe8b629SGarrett Wollman int error, optval; 1413df8bae1dSRodney W. Grimes 1414cfe8b629SGarrett Wollman error = optval = 0; 1415cfe8b629SGarrett Wollman if (sopt->sopt_level != IPPROTO_IP) { 1416cfe8b629SGarrett Wollman return (EINVAL); 1417cfe8b629SGarrett Wollman } 1418df8bae1dSRodney W. Grimes 1419cfe8b629SGarrett Wollman switch (sopt->sopt_dir) { 1420cfe8b629SGarrett Wollman case SOPT_SET: 1421cfe8b629SGarrett Wollman switch (sopt->sopt_name) { 1422df8bae1dSRodney W. Grimes case IP_OPTIONS: 1423df8bae1dSRodney W. Grimes #ifdef notyet 1424df8bae1dSRodney W. Grimes case IP_RETOPTS: 1425df8bae1dSRodney W. Grimes #endif 1426cfe8b629SGarrett Wollman { 1427cfe8b629SGarrett Wollman struct mbuf *m; 1428cfe8b629SGarrett Wollman if (sopt->sopt_valsize > MLEN) { 1429cfe8b629SGarrett Wollman error = EMSGSIZE; 1430cfe8b629SGarrett Wollman break; 1431cfe8b629SGarrett Wollman } 1432a163d034SWarner Losh MGET(m, sopt->sopt_td ? M_TRYWAIT : M_DONTWAIT, MT_HEADER); 1433cfe8b629SGarrett Wollman if (m == 0) { 1434cfe8b629SGarrett Wollman error = ENOBUFS; 1435cfe8b629SGarrett Wollman break; 1436cfe8b629SGarrett Wollman } 1437cfe8b629SGarrett Wollman m->m_len = sopt->sopt_valsize; 1438cfe8b629SGarrett Wollman error = sooptcopyin(sopt, mtod(m, char *), m->m_len, 1439cfe8b629SGarrett Wollman m->m_len); 1440cfe8b629SGarrett Wollman 1441cfe8b629SGarrett Wollman return (ip_pcbopts(sopt->sopt_name, &inp->inp_options, 1442cfe8b629SGarrett Wollman m)); 1443cfe8b629SGarrett Wollman } 1444df8bae1dSRodney W. Grimes 1445df8bae1dSRodney W. Grimes case IP_TOS: 1446df8bae1dSRodney W. Grimes case IP_TTL: 1447df8bae1dSRodney W. Grimes case IP_RECVOPTS: 1448df8bae1dSRodney W. Grimes case IP_RECVRETOPTS: 1449df8bae1dSRodney W. Grimes case IP_RECVDSTADDR: 14504957466bSMatthew N. Dodd case IP_RECVTTL: 145182c23ebaSBill Fenner case IP_RECVIF: 14526a800098SYoshinobu Inoue case IP_FAITH: 14538afa2304SBruce M Simpson case IP_ONESBCAST: 1454cfe8b629SGarrett Wollman error = sooptcopyin(sopt, &optval, sizeof optval, 1455cfe8b629SGarrett Wollman sizeof optval); 1456cfe8b629SGarrett Wollman if (error) 1457cfe8b629SGarrett Wollman break; 1458df8bae1dSRodney W. Grimes 1459cfe8b629SGarrett Wollman switch (sopt->sopt_name) { 1460df8bae1dSRodney W. Grimes case IP_TOS: 1461ca98b82cSDavid Greenman inp->inp_ip_tos = optval; 1462df8bae1dSRodney W. Grimes break; 1463df8bae1dSRodney W. Grimes 1464df8bae1dSRodney W. Grimes case IP_TTL: 1465ca98b82cSDavid Greenman inp->inp_ip_ttl = optval; 1466df8bae1dSRodney W. Grimes break; 1467df8bae1dSRodney W. Grimes #define OPTSET(bit) \ 1468df8bae1dSRodney W. Grimes if (optval) \ 1469df8bae1dSRodney W. Grimes inp->inp_flags |= bit; \ 1470df8bae1dSRodney W. Grimes else \ 1471df8bae1dSRodney W. Grimes inp->inp_flags &= ~bit; 1472df8bae1dSRodney W. Grimes 1473df8bae1dSRodney W. Grimes case IP_RECVOPTS: 1474df8bae1dSRodney W. Grimes OPTSET(INP_RECVOPTS); 1475df8bae1dSRodney W. Grimes break; 1476df8bae1dSRodney W. Grimes 1477df8bae1dSRodney W. Grimes case IP_RECVRETOPTS: 1478df8bae1dSRodney W. Grimes OPTSET(INP_RECVRETOPTS); 1479df8bae1dSRodney W. Grimes break; 1480df8bae1dSRodney W. Grimes 1481df8bae1dSRodney W. Grimes case IP_RECVDSTADDR: 1482df8bae1dSRodney W. Grimes OPTSET(INP_RECVDSTADDR); 1483df8bae1dSRodney W. Grimes break; 148482c23ebaSBill Fenner 14854957466bSMatthew N. Dodd case IP_RECVTTL: 14864957466bSMatthew N. Dodd OPTSET(INP_RECVTTL); 14874957466bSMatthew N. Dodd break; 14884957466bSMatthew N. Dodd 148982c23ebaSBill Fenner case IP_RECVIF: 149082c23ebaSBill Fenner OPTSET(INP_RECVIF); 149182c23ebaSBill Fenner break; 14926a800098SYoshinobu Inoue 14936a800098SYoshinobu Inoue case IP_FAITH: 14946a800098SYoshinobu Inoue OPTSET(INP_FAITH); 14956a800098SYoshinobu Inoue break; 14968afa2304SBruce M Simpson 14978afa2304SBruce M Simpson case IP_ONESBCAST: 14988afa2304SBruce M Simpson OPTSET(INP_ONESBCAST); 14998afa2304SBruce M Simpson break; 1500df8bae1dSRodney W. Grimes } 1501df8bae1dSRodney W. Grimes break; 1502df8bae1dSRodney W. Grimes #undef OPTSET 1503df8bae1dSRodney W. Grimes 1504df8bae1dSRodney W. Grimes case IP_MULTICAST_IF: 1505f0068c4aSGarrett Wollman case IP_MULTICAST_VIF: 1506df8bae1dSRodney W. Grimes case IP_MULTICAST_TTL: 1507df8bae1dSRodney W. Grimes case IP_MULTICAST_LOOP: 1508df8bae1dSRodney W. Grimes case IP_ADD_MEMBERSHIP: 1509df8bae1dSRodney W. Grimes case IP_DROP_MEMBERSHIP: 1510cfe8b629SGarrett Wollman error = ip_setmoptions(sopt, &inp->inp_moptions); 1511df8bae1dSRodney W. Grimes break; 1512df8bae1dSRodney W. Grimes 151333b3ac06SPeter Wemm case IP_PORTRANGE: 1514cfe8b629SGarrett Wollman error = sooptcopyin(sopt, &optval, sizeof optval, 1515cfe8b629SGarrett Wollman sizeof optval); 1516cfe8b629SGarrett Wollman if (error) 1517cfe8b629SGarrett Wollman break; 151833b3ac06SPeter Wemm 151933b3ac06SPeter Wemm switch (optval) { 152033b3ac06SPeter Wemm case IP_PORTRANGE_DEFAULT: 152133b3ac06SPeter Wemm inp->inp_flags &= ~(INP_LOWPORT); 152233b3ac06SPeter Wemm inp->inp_flags &= ~(INP_HIGHPORT); 152333b3ac06SPeter Wemm break; 152433b3ac06SPeter Wemm 152533b3ac06SPeter Wemm case IP_PORTRANGE_HIGH: 152633b3ac06SPeter Wemm inp->inp_flags &= ~(INP_LOWPORT); 152733b3ac06SPeter Wemm inp->inp_flags |= INP_HIGHPORT; 152833b3ac06SPeter Wemm break; 152933b3ac06SPeter Wemm 153033b3ac06SPeter Wemm case IP_PORTRANGE_LOW: 153133b3ac06SPeter Wemm inp->inp_flags &= ~(INP_HIGHPORT); 153233b3ac06SPeter Wemm inp->inp_flags |= INP_LOWPORT; 153333b3ac06SPeter Wemm break; 153433b3ac06SPeter Wemm 153533b3ac06SPeter Wemm default: 153633b3ac06SPeter Wemm error = EINVAL; 153733b3ac06SPeter Wemm break; 153833b3ac06SPeter Wemm } 1539ce8c72b1SPeter Wemm break; 154033b3ac06SPeter Wemm 1541b9234fafSSam Leffler #if defined(IPSEC) || defined(FAST_IPSEC) 15426a800098SYoshinobu Inoue case IP_IPSEC_POLICY: 15436a800098SYoshinobu Inoue { 15446a800098SYoshinobu Inoue caddr_t req; 1545686cdd19SJun-ichiro itojun Hagino size_t len = 0; 15466a800098SYoshinobu Inoue int priv; 15476a800098SYoshinobu Inoue struct mbuf *m; 15486a800098SYoshinobu Inoue int optname; 15496a800098SYoshinobu Inoue 15506a800098SYoshinobu Inoue if ((error = soopt_getm(sopt, &m)) != 0) /* XXX */ 15516a800098SYoshinobu Inoue break; 15526a800098SYoshinobu Inoue if ((error = soopt_mcopyin(sopt, m)) != 0) /* XXX */ 15536a800098SYoshinobu Inoue break; 1554b40ce416SJulian Elischer priv = (sopt->sopt_td != NULL && 155544731cabSJohn Baldwin suser(sopt->sopt_td) != 0) ? 0 : 1; 15566a800098SYoshinobu Inoue req = mtod(m, caddr_t); 1557686cdd19SJun-ichiro itojun Hagino len = m->m_len; 15586a800098SYoshinobu Inoue optname = sopt->sopt_name; 1559686cdd19SJun-ichiro itojun Hagino error = ipsec4_set_policy(inp, optname, req, len, priv); 15606a800098SYoshinobu Inoue m_freem(m); 15616a800098SYoshinobu Inoue break; 15626a800098SYoshinobu Inoue } 15636a800098SYoshinobu Inoue #endif /*IPSEC*/ 15646a800098SYoshinobu Inoue 1565df8bae1dSRodney W. Grimes default: 1566df8bae1dSRodney W. Grimes error = ENOPROTOOPT; 1567df8bae1dSRodney W. Grimes break; 1568df8bae1dSRodney W. Grimes } 1569df8bae1dSRodney W. Grimes break; 1570df8bae1dSRodney W. Grimes 1571cfe8b629SGarrett Wollman case SOPT_GET: 1572cfe8b629SGarrett Wollman switch (sopt->sopt_name) { 1573df8bae1dSRodney W. Grimes case IP_OPTIONS: 1574df8bae1dSRodney W. Grimes case IP_RETOPTS: 1575cfe8b629SGarrett Wollman if (inp->inp_options) 1576cfe8b629SGarrett Wollman error = sooptcopyout(sopt, 1577cfe8b629SGarrett Wollman mtod(inp->inp_options, 1578cfe8b629SGarrett Wollman char *), 1579cfe8b629SGarrett Wollman inp->inp_options->m_len); 1580cfe8b629SGarrett Wollman else 1581cfe8b629SGarrett Wollman sopt->sopt_valsize = 0; 1582df8bae1dSRodney W. Grimes break; 1583df8bae1dSRodney W. Grimes 1584df8bae1dSRodney W. Grimes case IP_TOS: 1585df8bae1dSRodney W. Grimes case IP_TTL: 1586df8bae1dSRodney W. Grimes case IP_RECVOPTS: 1587df8bae1dSRodney W. Grimes case IP_RECVRETOPTS: 1588df8bae1dSRodney W. Grimes case IP_RECVDSTADDR: 15894957466bSMatthew N. Dodd case IP_RECVTTL: 159082c23ebaSBill Fenner case IP_RECVIF: 1591cfe8b629SGarrett Wollman case IP_PORTRANGE: 15926a800098SYoshinobu Inoue case IP_FAITH: 15938afa2304SBruce M Simpson case IP_ONESBCAST: 1594cfe8b629SGarrett Wollman switch (sopt->sopt_name) { 1595df8bae1dSRodney W. Grimes 1596df8bae1dSRodney W. Grimes case IP_TOS: 1597ca98b82cSDavid Greenman optval = inp->inp_ip_tos; 1598df8bae1dSRodney W. Grimes break; 1599df8bae1dSRodney W. Grimes 1600df8bae1dSRodney W. Grimes case IP_TTL: 1601ca98b82cSDavid Greenman optval = inp->inp_ip_ttl; 1602df8bae1dSRodney W. Grimes break; 1603df8bae1dSRodney W. Grimes 1604df8bae1dSRodney W. Grimes #define OPTBIT(bit) (inp->inp_flags & bit ? 1 : 0) 1605df8bae1dSRodney W. Grimes 1606df8bae1dSRodney W. Grimes case IP_RECVOPTS: 1607df8bae1dSRodney W. Grimes optval = OPTBIT(INP_RECVOPTS); 1608df8bae1dSRodney W. Grimes break; 1609df8bae1dSRodney W. Grimes 1610df8bae1dSRodney W. Grimes case IP_RECVRETOPTS: 1611df8bae1dSRodney W. Grimes optval = OPTBIT(INP_RECVRETOPTS); 1612df8bae1dSRodney W. Grimes break; 1613df8bae1dSRodney W. Grimes 1614df8bae1dSRodney W. Grimes case IP_RECVDSTADDR: 1615df8bae1dSRodney W. Grimes optval = OPTBIT(INP_RECVDSTADDR); 1616df8bae1dSRodney W. Grimes break; 161782c23ebaSBill Fenner 16184957466bSMatthew N. Dodd case IP_RECVTTL: 16194957466bSMatthew N. Dodd optval = OPTBIT(INP_RECVTTL); 16204957466bSMatthew N. Dodd break; 16214957466bSMatthew N. Dodd 162282c23ebaSBill Fenner case IP_RECVIF: 162382c23ebaSBill Fenner optval = OPTBIT(INP_RECVIF); 162482c23ebaSBill Fenner break; 1625cfe8b629SGarrett Wollman 1626cfe8b629SGarrett Wollman case IP_PORTRANGE: 1627cfe8b629SGarrett Wollman if (inp->inp_flags & INP_HIGHPORT) 1628cfe8b629SGarrett Wollman optval = IP_PORTRANGE_HIGH; 1629cfe8b629SGarrett Wollman else if (inp->inp_flags & INP_LOWPORT) 1630cfe8b629SGarrett Wollman optval = IP_PORTRANGE_LOW; 1631cfe8b629SGarrett Wollman else 1632cfe8b629SGarrett Wollman optval = 0; 1633cfe8b629SGarrett Wollman break; 16346a800098SYoshinobu Inoue 16356a800098SYoshinobu Inoue case IP_FAITH: 16366a800098SYoshinobu Inoue optval = OPTBIT(INP_FAITH); 16376a800098SYoshinobu Inoue break; 16388afa2304SBruce M Simpson 16398afa2304SBruce M Simpson case IP_ONESBCAST: 16408afa2304SBruce M Simpson optval = OPTBIT(INP_ONESBCAST); 16418afa2304SBruce M Simpson break; 1642df8bae1dSRodney W. Grimes } 1643cfe8b629SGarrett Wollman error = sooptcopyout(sopt, &optval, sizeof optval); 1644df8bae1dSRodney W. Grimes break; 1645df8bae1dSRodney W. Grimes 1646df8bae1dSRodney W. Grimes case IP_MULTICAST_IF: 1647f0068c4aSGarrett Wollman case IP_MULTICAST_VIF: 1648df8bae1dSRodney W. Grimes case IP_MULTICAST_TTL: 1649df8bae1dSRodney W. Grimes case IP_MULTICAST_LOOP: 1650df8bae1dSRodney W. Grimes case IP_ADD_MEMBERSHIP: 1651df8bae1dSRodney W. Grimes case IP_DROP_MEMBERSHIP: 1652cfe8b629SGarrett Wollman error = ip_getmoptions(sopt, inp->inp_moptions); 165333b3ac06SPeter Wemm break; 165433b3ac06SPeter Wemm 1655b9234fafSSam Leffler #if defined(IPSEC) || defined(FAST_IPSEC) 16566a800098SYoshinobu Inoue case IP_IPSEC_POLICY: 16576a800098SYoshinobu Inoue { 1658f63e7634SYoshinobu Inoue struct mbuf *m = NULL; 16596a800098SYoshinobu Inoue caddr_t req = NULL; 1660686cdd19SJun-ichiro itojun Hagino size_t len = 0; 16616a800098SYoshinobu Inoue 1662686cdd19SJun-ichiro itojun Hagino if (m != 0) { 16636a800098SYoshinobu Inoue req = mtod(m, caddr_t); 1664686cdd19SJun-ichiro itojun Hagino len = m->m_len; 1665686cdd19SJun-ichiro itojun Hagino } 1666686cdd19SJun-ichiro itojun Hagino error = ipsec4_get_policy(sotoinpcb(so), req, len, &m); 16676a800098SYoshinobu Inoue if (error == 0) 16686a800098SYoshinobu Inoue error = soopt_mcopyout(sopt, m); /* XXX */ 1669f63e7634SYoshinobu Inoue if (error == 0) 16706a800098SYoshinobu Inoue m_freem(m); 16716a800098SYoshinobu Inoue break; 16726a800098SYoshinobu Inoue } 16736a800098SYoshinobu Inoue #endif /*IPSEC*/ 16746a800098SYoshinobu Inoue 1675df8bae1dSRodney W. Grimes default: 1676df8bae1dSRodney W. Grimes error = ENOPROTOOPT; 1677df8bae1dSRodney W. Grimes break; 1678df8bae1dSRodney W. Grimes } 1679df8bae1dSRodney W. Grimes break; 1680df8bae1dSRodney W. Grimes } 1681df8bae1dSRodney W. Grimes return (error); 1682df8bae1dSRodney W. Grimes } 1683df8bae1dSRodney W. Grimes 1684df8bae1dSRodney W. Grimes /* 1685df8bae1dSRodney W. Grimes * Set up IP options in pcb for insertion in output packets. 1686df8bae1dSRodney W. Grimes * Store in mbuf with pointer in pcbopt, adding pseudo-option 1687df8bae1dSRodney W. Grimes * with destination address if source routed. 1688df8bae1dSRodney W. Grimes */ 16890312fbe9SPoul-Henning Kamp static int 1690b375c9ecSLuigi Rizzo ip_pcbopts(optname, pcbopt, m) 1691b375c9ecSLuigi Rizzo int optname; 1692b375c9ecSLuigi Rizzo struct mbuf **pcbopt; 1693b375c9ecSLuigi Rizzo register struct mbuf *m; 1694df8bae1dSRodney W. Grimes { 1695b375c9ecSLuigi Rizzo register int cnt, optlen; 1696b375c9ecSLuigi Rizzo register u_char *cp; 1697df8bae1dSRodney W. Grimes u_char opt; 1698df8bae1dSRodney W. Grimes 1699df8bae1dSRodney W. Grimes /* turn off any old options */ 1700df8bae1dSRodney W. Grimes if (*pcbopt) 1701df8bae1dSRodney W. Grimes (void)m_free(*pcbopt); 1702df8bae1dSRodney W. Grimes *pcbopt = 0; 1703df8bae1dSRodney W. Grimes if (m == (struct mbuf *)0 || m->m_len == 0) { 1704df8bae1dSRodney W. Grimes /* 1705df8bae1dSRodney W. Grimes * Only turning off any previous options. 1706df8bae1dSRodney W. Grimes */ 1707df8bae1dSRodney W. Grimes if (m) 1708df8bae1dSRodney W. Grimes (void)m_free(m); 1709df8bae1dSRodney W. Grimes return (0); 1710df8bae1dSRodney W. Grimes } 1711df8bae1dSRodney W. Grimes 17120c8d2590SBruce Evans if (m->m_len % sizeof(int32_t)) 1713df8bae1dSRodney W. Grimes goto bad; 1714df8bae1dSRodney W. Grimes /* 1715df8bae1dSRodney W. Grimes * IP first-hop destination address will be stored before 1716df8bae1dSRodney W. Grimes * actual options; move other options back 1717df8bae1dSRodney W. Grimes * and clear it when none present. 1718df8bae1dSRodney W. Grimes */ 1719df8bae1dSRodney W. Grimes if (m->m_data + m->m_len + sizeof(struct in_addr) >= &m->m_dat[MLEN]) 1720df8bae1dSRodney W. Grimes goto bad; 1721df8bae1dSRodney W. Grimes cnt = m->m_len; 1722df8bae1dSRodney W. Grimes m->m_len += sizeof(struct in_addr); 1723df8bae1dSRodney W. Grimes cp = mtod(m, u_char *) + sizeof(struct in_addr); 1724212059bdSDag-Erling Smørgrav bcopy(mtod(m, void *), cp, (unsigned)cnt); 1725212059bdSDag-Erling Smørgrav bzero(mtod(m, void *), sizeof(struct in_addr)); 1726df8bae1dSRodney W. Grimes 1727df8bae1dSRodney W. Grimes for (; cnt > 0; cnt -= optlen, cp += optlen) { 1728df8bae1dSRodney W. Grimes opt = cp[IPOPT_OPTVAL]; 1729df8bae1dSRodney W. Grimes if (opt == IPOPT_EOL) 1730df8bae1dSRodney W. Grimes break; 1731df8bae1dSRodney W. Grimes if (opt == IPOPT_NOP) 1732df8bae1dSRodney W. Grimes optlen = 1; 1733df8bae1dSRodney W. Grimes else { 1734707d00a3SJonathan Lemon if (cnt < IPOPT_OLEN + sizeof(*cp)) 1735707d00a3SJonathan Lemon goto bad; 1736df8bae1dSRodney W. Grimes optlen = cp[IPOPT_OLEN]; 1737707d00a3SJonathan Lemon if (optlen < IPOPT_OLEN + sizeof(*cp) || optlen > cnt) 1738df8bae1dSRodney W. Grimes goto bad; 1739df8bae1dSRodney W. Grimes } 1740df8bae1dSRodney W. Grimes switch (opt) { 1741df8bae1dSRodney W. Grimes 1742df8bae1dSRodney W. Grimes default: 1743df8bae1dSRodney W. Grimes break; 1744df8bae1dSRodney W. Grimes 1745df8bae1dSRodney W. Grimes case IPOPT_LSRR: 1746df8bae1dSRodney W. Grimes case IPOPT_SSRR: 1747df8bae1dSRodney W. Grimes /* 1748df8bae1dSRodney W. Grimes * user process specifies route as: 1749df8bae1dSRodney W. Grimes * ->A->B->C->D 1750df8bae1dSRodney W. Grimes * D must be our final destination (but we can't 1751df8bae1dSRodney W. Grimes * check that since we may not have connected yet). 1752df8bae1dSRodney W. Grimes * A is first hop destination, which doesn't appear in 1753df8bae1dSRodney W. Grimes * actual IP option, but is stored before the options. 1754df8bae1dSRodney W. Grimes */ 1755df8bae1dSRodney W. Grimes if (optlen < IPOPT_MINOFF - 1 + sizeof(struct in_addr)) 1756df8bae1dSRodney W. Grimes goto bad; 1757df8bae1dSRodney W. Grimes m->m_len -= sizeof(struct in_addr); 1758df8bae1dSRodney W. Grimes cnt -= sizeof(struct in_addr); 1759df8bae1dSRodney W. Grimes optlen -= sizeof(struct in_addr); 1760df8bae1dSRodney W. Grimes cp[IPOPT_OLEN] = optlen; 1761df8bae1dSRodney W. Grimes /* 1762df8bae1dSRodney W. Grimes * Move first hop before start of options. 1763df8bae1dSRodney W. Grimes */ 1764df8bae1dSRodney W. Grimes bcopy((caddr_t)&cp[IPOPT_OFFSET+1], mtod(m, caddr_t), 1765df8bae1dSRodney W. Grimes sizeof(struct in_addr)); 1766df8bae1dSRodney W. Grimes /* 1767df8bae1dSRodney W. Grimes * Then copy rest of options back 1768df8bae1dSRodney W. Grimes * to close up the deleted entry. 1769df8bae1dSRodney W. Grimes */ 1770212059bdSDag-Erling Smørgrav bcopy((&cp[IPOPT_OFFSET+1] + sizeof(struct in_addr)), 1771212059bdSDag-Erling Smørgrav &cp[IPOPT_OFFSET+1], 1772df8bae1dSRodney W. Grimes (unsigned)cnt + sizeof(struct in_addr)); 1773df8bae1dSRodney W. Grimes break; 1774df8bae1dSRodney W. Grimes } 1775df8bae1dSRodney W. Grimes } 1776df8bae1dSRodney W. Grimes if (m->m_len > MAX_IPOPTLEN + sizeof(struct in_addr)) 1777df8bae1dSRodney W. Grimes goto bad; 1778df8bae1dSRodney W. Grimes *pcbopt = m; 1779df8bae1dSRodney W. Grimes return (0); 1780df8bae1dSRodney W. Grimes 1781df8bae1dSRodney W. Grimes bad: 1782df8bae1dSRodney W. Grimes (void)m_free(m); 1783df8bae1dSRodney W. Grimes return (EINVAL); 1784df8bae1dSRodney W. Grimes } 1785df8bae1dSRodney W. Grimes 1786df8bae1dSRodney W. Grimes /* 1787cfe8b629SGarrett Wollman * XXX 1788cfe8b629SGarrett Wollman * The whole multicast option thing needs to be re-thought. 1789cfe8b629SGarrett Wollman * Several of these options are equally applicable to non-multicast 1790cfe8b629SGarrett Wollman * transmission, and one (IP_MULTICAST_TTL) totally duplicates a 1791cfe8b629SGarrett Wollman * standard option (IP_TTL). 1792cfe8b629SGarrett Wollman */ 179333841545SHajimu UMEMOTO 179433841545SHajimu UMEMOTO /* 179533841545SHajimu UMEMOTO * following RFC1724 section 3.3, 0.0.0.0/8 is interpreted as interface index. 179633841545SHajimu UMEMOTO */ 179733841545SHajimu UMEMOTO static struct ifnet * 1798b375c9ecSLuigi Rizzo ip_multicast_if(a, ifindexp) 1799b375c9ecSLuigi Rizzo struct in_addr *a; 1800b375c9ecSLuigi Rizzo int *ifindexp; 180133841545SHajimu UMEMOTO { 180233841545SHajimu UMEMOTO int ifindex; 180333841545SHajimu UMEMOTO struct ifnet *ifp; 180433841545SHajimu UMEMOTO 180533841545SHajimu UMEMOTO if (ifindexp) 180633841545SHajimu UMEMOTO *ifindexp = 0; 180733841545SHajimu UMEMOTO if (ntohl(a->s_addr) >> 24 == 0) { 180833841545SHajimu UMEMOTO ifindex = ntohl(a->s_addr) & 0xffffff; 180933841545SHajimu UMEMOTO if (ifindex < 0 || if_index < ifindex) 181033841545SHajimu UMEMOTO return NULL; 1811f9132cebSJonathan Lemon ifp = ifnet_byindex(ifindex); 181233841545SHajimu UMEMOTO if (ifindexp) 181333841545SHajimu UMEMOTO *ifindexp = ifindex; 181433841545SHajimu UMEMOTO } else { 181533841545SHajimu UMEMOTO INADDR_TO_IFP(*a, ifp); 181633841545SHajimu UMEMOTO } 181733841545SHajimu UMEMOTO return ifp; 181833841545SHajimu UMEMOTO } 181933841545SHajimu UMEMOTO 1820cfe8b629SGarrett Wollman /* 1821df8bae1dSRodney W. Grimes * Set the IP multicast options in response to user setsockopt(). 1822df8bae1dSRodney W. Grimes */ 18230312fbe9SPoul-Henning Kamp static int 1824b375c9ecSLuigi Rizzo ip_setmoptions(sopt, imop) 1825b375c9ecSLuigi Rizzo struct sockopt *sopt; 1826b375c9ecSLuigi Rizzo struct ip_moptions **imop; 1827df8bae1dSRodney W. Grimes { 1828cfe8b629SGarrett Wollman int error = 0; 1829cfe8b629SGarrett Wollman int i; 1830df8bae1dSRodney W. Grimes struct in_addr addr; 1831cfe8b629SGarrett Wollman struct ip_mreq mreq; 1832cfe8b629SGarrett Wollman struct ifnet *ifp; 1833cfe8b629SGarrett Wollman struct ip_moptions *imo = *imop; 1834df8bae1dSRodney W. Grimes struct route ro; 1835cfe8b629SGarrett Wollman struct sockaddr_in *dst; 183633841545SHajimu UMEMOTO int ifindex; 18379b626c29SGarrett Wollman int s; 1838df8bae1dSRodney W. Grimes 1839df8bae1dSRodney W. Grimes if (imo == NULL) { 1840df8bae1dSRodney W. Grimes /* 1841df8bae1dSRodney W. Grimes * No multicast option buffer attached to the pcb; 1842df8bae1dSRodney W. Grimes * allocate one and initialize to default values. 1843df8bae1dSRodney W. Grimes */ 1844df8bae1dSRodney W. Grimes imo = (struct ip_moptions*)malloc(sizeof(*imo), M_IPMOPTS, 1845a163d034SWarner Losh M_WAITOK); 1846df8bae1dSRodney W. Grimes 1847df8bae1dSRodney W. Grimes if (imo == NULL) 1848df8bae1dSRodney W. Grimes return (ENOBUFS); 1849df8bae1dSRodney W. Grimes *imop = imo; 1850df8bae1dSRodney W. Grimes imo->imo_multicast_ifp = NULL; 185133841545SHajimu UMEMOTO imo->imo_multicast_addr.s_addr = INADDR_ANY; 18521c5de19aSGarrett Wollman imo->imo_multicast_vif = -1; 1853df8bae1dSRodney W. Grimes imo->imo_multicast_ttl = IP_DEFAULT_MULTICAST_TTL; 1854df8bae1dSRodney W. Grimes imo->imo_multicast_loop = IP_DEFAULT_MULTICAST_LOOP; 1855df8bae1dSRodney W. Grimes imo->imo_num_memberships = 0; 1856df8bae1dSRodney W. Grimes } 1857df8bae1dSRodney W. Grimes 1858cfe8b629SGarrett Wollman switch (sopt->sopt_name) { 1859f0068c4aSGarrett Wollman /* store an index number for the vif you wanna use in the send */ 1860f0068c4aSGarrett Wollman case IP_MULTICAST_VIF: 1861cfe8b629SGarrett Wollman if (legal_vif_num == 0) { 18625e9ae478SGarrett Wollman error = EOPNOTSUPP; 18635e9ae478SGarrett Wollman break; 18645e9ae478SGarrett Wollman } 1865cfe8b629SGarrett Wollman error = sooptcopyin(sopt, &i, sizeof i, sizeof i); 1866cfe8b629SGarrett Wollman if (error) 1867f0068c4aSGarrett Wollman break; 18681c5de19aSGarrett Wollman if (!legal_vif_num(i) && (i != -1)) { 1869f0068c4aSGarrett Wollman error = EINVAL; 1870f0068c4aSGarrett Wollman break; 1871f0068c4aSGarrett Wollman } 1872f0068c4aSGarrett Wollman imo->imo_multicast_vif = i; 1873f0068c4aSGarrett Wollman break; 1874f0068c4aSGarrett Wollman 1875df8bae1dSRodney W. Grimes case IP_MULTICAST_IF: 1876df8bae1dSRodney W. Grimes /* 1877df8bae1dSRodney W. Grimes * Select the interface for outgoing multicast packets. 1878df8bae1dSRodney W. Grimes */ 1879cfe8b629SGarrett Wollman error = sooptcopyin(sopt, &addr, sizeof addr, sizeof addr); 1880cfe8b629SGarrett Wollman if (error) 1881df8bae1dSRodney W. Grimes break; 1882df8bae1dSRodney W. Grimes /* 1883df8bae1dSRodney W. Grimes * INADDR_ANY is used to remove a previous selection. 1884df8bae1dSRodney W. Grimes * When no interface is selected, a default one is 1885df8bae1dSRodney W. Grimes * chosen every time a multicast packet is sent. 1886df8bae1dSRodney W. Grimes */ 1887df8bae1dSRodney W. Grimes if (addr.s_addr == INADDR_ANY) { 1888df8bae1dSRodney W. Grimes imo->imo_multicast_ifp = NULL; 1889df8bae1dSRodney W. Grimes break; 1890df8bae1dSRodney W. Grimes } 1891df8bae1dSRodney W. Grimes /* 1892df8bae1dSRodney W. Grimes * The selected interface is identified by its local 1893df8bae1dSRodney W. Grimes * IP address. Find the interface and confirm that 1894df8bae1dSRodney W. Grimes * it supports multicasting. 1895df8bae1dSRodney W. Grimes */ 189620e8807cSGarrett Wollman s = splimp(); 189733841545SHajimu UMEMOTO ifp = ip_multicast_if(&addr, &ifindex); 1898df8bae1dSRodney W. Grimes if (ifp == NULL || (ifp->if_flags & IFF_MULTICAST) == 0) { 1899fbc6ab00SBill Fenner splx(s); 1900df8bae1dSRodney W. Grimes error = EADDRNOTAVAIL; 1901df8bae1dSRodney W. Grimes break; 1902df8bae1dSRodney W. Grimes } 1903df8bae1dSRodney W. Grimes imo->imo_multicast_ifp = ifp; 190433841545SHajimu UMEMOTO if (ifindex) 190533841545SHajimu UMEMOTO imo->imo_multicast_addr = addr; 190633841545SHajimu UMEMOTO else 190733841545SHajimu UMEMOTO imo->imo_multicast_addr.s_addr = INADDR_ANY; 19089b626c29SGarrett Wollman splx(s); 1909df8bae1dSRodney W. Grimes break; 1910df8bae1dSRodney W. Grimes 1911df8bae1dSRodney W. Grimes case IP_MULTICAST_TTL: 1912df8bae1dSRodney W. Grimes /* 1913df8bae1dSRodney W. Grimes * Set the IP time-to-live for outgoing multicast packets. 1914cfe8b629SGarrett Wollman * The original multicast API required a char argument, 1915cfe8b629SGarrett Wollman * which is inconsistent with the rest of the socket API. 1916cfe8b629SGarrett Wollman * We allow either a char or an int. 1917df8bae1dSRodney W. Grimes */ 1918cfe8b629SGarrett Wollman if (sopt->sopt_valsize == 1) { 1919cfe8b629SGarrett Wollman u_char ttl; 1920cfe8b629SGarrett Wollman error = sooptcopyin(sopt, &ttl, 1, 1); 1921cfe8b629SGarrett Wollman if (error) 1922df8bae1dSRodney W. Grimes break; 1923cfe8b629SGarrett Wollman imo->imo_multicast_ttl = ttl; 1924cfe8b629SGarrett Wollman } else { 1925cfe8b629SGarrett Wollman u_int ttl; 1926cfe8b629SGarrett Wollman error = sooptcopyin(sopt, &ttl, sizeof ttl, 1927cfe8b629SGarrett Wollman sizeof ttl); 1928cfe8b629SGarrett Wollman if (error) 1929cfe8b629SGarrett Wollman break; 1930cfe8b629SGarrett Wollman if (ttl > 255) 1931cfe8b629SGarrett Wollman error = EINVAL; 1932cfe8b629SGarrett Wollman else 1933cfe8b629SGarrett Wollman imo->imo_multicast_ttl = ttl; 1934df8bae1dSRodney W. Grimes } 1935df8bae1dSRodney W. Grimes break; 1936df8bae1dSRodney W. Grimes 1937df8bae1dSRodney W. Grimes case IP_MULTICAST_LOOP: 1938df8bae1dSRodney W. Grimes /* 1939df8bae1dSRodney W. Grimes * Set the loopback flag for outgoing multicast packets. 1940cfe8b629SGarrett Wollman * Must be zero or one. The original multicast API required a 1941cfe8b629SGarrett Wollman * char argument, which is inconsistent with the rest 1942cfe8b629SGarrett Wollman * of the socket API. We allow either a char or an int. 1943df8bae1dSRodney W. Grimes */ 1944cfe8b629SGarrett Wollman if (sopt->sopt_valsize == 1) { 1945cfe8b629SGarrett Wollman u_char loop; 1946cfe8b629SGarrett Wollman error = sooptcopyin(sopt, &loop, 1, 1); 1947cfe8b629SGarrett Wollman if (error) 1948df8bae1dSRodney W. Grimes break; 1949cfe8b629SGarrett Wollman imo->imo_multicast_loop = !!loop; 1950cfe8b629SGarrett Wollman } else { 1951cfe8b629SGarrett Wollman u_int loop; 1952cfe8b629SGarrett Wollman error = sooptcopyin(sopt, &loop, sizeof loop, 1953cfe8b629SGarrett Wollman sizeof loop); 1954cfe8b629SGarrett Wollman if (error) 1955cfe8b629SGarrett Wollman break; 1956cfe8b629SGarrett Wollman imo->imo_multicast_loop = !!loop; 1957df8bae1dSRodney W. Grimes } 1958df8bae1dSRodney W. Grimes break; 1959df8bae1dSRodney W. Grimes 1960df8bae1dSRodney W. Grimes case IP_ADD_MEMBERSHIP: 1961df8bae1dSRodney W. Grimes /* 1962df8bae1dSRodney W. Grimes * Add a multicast group membership. 1963df8bae1dSRodney W. Grimes * Group must be a valid IP multicast address. 1964df8bae1dSRodney W. Grimes */ 1965cfe8b629SGarrett Wollman error = sooptcopyin(sopt, &mreq, sizeof mreq, sizeof mreq); 1966cfe8b629SGarrett Wollman if (error) 1967df8bae1dSRodney W. Grimes break; 1968cfe8b629SGarrett Wollman 1969cfe8b629SGarrett Wollman if (!IN_MULTICAST(ntohl(mreq.imr_multiaddr.s_addr))) { 1970df8bae1dSRodney W. Grimes error = EINVAL; 1971df8bae1dSRodney W. Grimes break; 1972df8bae1dSRodney W. Grimes } 197320e8807cSGarrett Wollman s = splimp(); 1974df8bae1dSRodney W. Grimes /* 1975df8bae1dSRodney W. Grimes * If no interface address was provided, use the interface of 1976df8bae1dSRodney W. Grimes * the route to the given multicast address. 1977df8bae1dSRodney W. Grimes */ 1978cfe8b629SGarrett Wollman if (mreq.imr_interface.s_addr == INADDR_ANY) { 19791c5de19aSGarrett Wollman bzero((caddr_t)&ro, sizeof(ro)); 1980df8bae1dSRodney W. Grimes dst = (struct sockaddr_in *)&ro.ro_dst; 1981df8bae1dSRodney W. Grimes dst->sin_len = sizeof(*dst); 1982df8bae1dSRodney W. Grimes dst->sin_family = AF_INET; 1983cfe8b629SGarrett Wollman dst->sin_addr = mreq.imr_multiaddr; 1984df8bae1dSRodney W. Grimes rtalloc(&ro); 1985df8bae1dSRodney W. Grimes if (ro.ro_rt == NULL) { 1986df8bae1dSRodney W. Grimes error = EADDRNOTAVAIL; 19879b626c29SGarrett Wollman splx(s); 1988df8bae1dSRodney W. Grimes break; 1989df8bae1dSRodney W. Grimes } 1990df8bae1dSRodney W. Grimes ifp = ro.ro_rt->rt_ifp; 1991d1dd20beSSam Leffler RTFREE(ro.ro_rt); 1992df8bae1dSRodney W. Grimes } 1993df8bae1dSRodney W. Grimes else { 199433841545SHajimu UMEMOTO ifp = ip_multicast_if(&mreq.imr_interface, NULL); 1995df8bae1dSRodney W. Grimes } 19969b626c29SGarrett Wollman 1997df8bae1dSRodney W. Grimes /* 1998df8bae1dSRodney W. Grimes * See if we found an interface, and confirm that it 1999df8bae1dSRodney W. Grimes * supports multicast. 2000df8bae1dSRodney W. Grimes */ 2001df8bae1dSRodney W. Grimes if (ifp == NULL || (ifp->if_flags & IFF_MULTICAST) == 0) { 2002df8bae1dSRodney W. Grimes error = EADDRNOTAVAIL; 20039b626c29SGarrett Wollman splx(s); 2004df8bae1dSRodney W. Grimes break; 2005df8bae1dSRodney W. Grimes } 2006df8bae1dSRodney W. Grimes /* 2007df8bae1dSRodney W. Grimes * See if the membership already exists or if all the 2008df8bae1dSRodney W. Grimes * membership slots are full. 2009df8bae1dSRodney W. Grimes */ 2010df8bae1dSRodney W. Grimes for (i = 0; i < imo->imo_num_memberships; ++i) { 2011df8bae1dSRodney W. Grimes if (imo->imo_membership[i]->inm_ifp == ifp && 2012df8bae1dSRodney W. Grimes imo->imo_membership[i]->inm_addr.s_addr 2013cfe8b629SGarrett Wollman == mreq.imr_multiaddr.s_addr) 2014df8bae1dSRodney W. Grimes break; 2015df8bae1dSRodney W. Grimes } 2016df8bae1dSRodney W. Grimes if (i < imo->imo_num_memberships) { 2017df8bae1dSRodney W. Grimes error = EADDRINUSE; 20189b626c29SGarrett Wollman splx(s); 2019df8bae1dSRodney W. Grimes break; 2020df8bae1dSRodney W. Grimes } 2021df8bae1dSRodney W. Grimes if (i == IP_MAX_MEMBERSHIPS) { 2022df8bae1dSRodney W. Grimes error = ETOOMANYREFS; 20239b626c29SGarrett Wollman splx(s); 2024df8bae1dSRodney W. Grimes break; 2025df8bae1dSRodney W. Grimes } 2026df8bae1dSRodney W. Grimes /* 2027df8bae1dSRodney W. Grimes * Everything looks good; add a new record to the multicast 2028df8bae1dSRodney W. Grimes * address list for the given interface. 2029df8bae1dSRodney W. Grimes */ 2030df8bae1dSRodney W. Grimes if ((imo->imo_membership[i] = 2031cfe8b629SGarrett Wollman in_addmulti(&mreq.imr_multiaddr, ifp)) == NULL) { 2032df8bae1dSRodney W. Grimes error = ENOBUFS; 20339b626c29SGarrett Wollman splx(s); 2034df8bae1dSRodney W. Grimes break; 2035df8bae1dSRodney W. Grimes } 2036df8bae1dSRodney W. Grimes ++imo->imo_num_memberships; 20379b626c29SGarrett Wollman splx(s); 2038df8bae1dSRodney W. Grimes break; 2039df8bae1dSRodney W. Grimes 2040df8bae1dSRodney W. Grimes case IP_DROP_MEMBERSHIP: 2041df8bae1dSRodney W. Grimes /* 2042df8bae1dSRodney W. Grimes * Drop a multicast group membership. 2043df8bae1dSRodney W. Grimes * Group must be a valid IP multicast address. 2044df8bae1dSRodney W. Grimes */ 2045cfe8b629SGarrett Wollman error = sooptcopyin(sopt, &mreq, sizeof mreq, sizeof mreq); 2046cfe8b629SGarrett Wollman if (error) 2047df8bae1dSRodney W. Grimes break; 2048cfe8b629SGarrett Wollman 2049cfe8b629SGarrett Wollman if (!IN_MULTICAST(ntohl(mreq.imr_multiaddr.s_addr))) { 2050df8bae1dSRodney W. Grimes error = EINVAL; 2051df8bae1dSRodney W. Grimes break; 2052df8bae1dSRodney W. Grimes } 20539b626c29SGarrett Wollman 205420e8807cSGarrett Wollman s = splimp(); 2055df8bae1dSRodney W. Grimes /* 2056df8bae1dSRodney W. Grimes * If an interface address was specified, get a pointer 2057df8bae1dSRodney W. Grimes * to its ifnet structure. 2058df8bae1dSRodney W. Grimes */ 2059cfe8b629SGarrett Wollman if (mreq.imr_interface.s_addr == INADDR_ANY) 2060df8bae1dSRodney W. Grimes ifp = NULL; 2061df8bae1dSRodney W. Grimes else { 206233841545SHajimu UMEMOTO ifp = ip_multicast_if(&mreq.imr_interface, NULL); 2063df8bae1dSRodney W. Grimes if (ifp == NULL) { 2064df8bae1dSRodney W. Grimes error = EADDRNOTAVAIL; 20659b626c29SGarrett Wollman splx(s); 2066df8bae1dSRodney W. Grimes break; 2067df8bae1dSRodney W. Grimes } 2068df8bae1dSRodney W. Grimes } 2069df8bae1dSRodney W. Grimes /* 2070df8bae1dSRodney W. Grimes * Find the membership in the membership array. 2071df8bae1dSRodney W. Grimes */ 2072df8bae1dSRodney W. Grimes for (i = 0; i < imo->imo_num_memberships; ++i) { 2073df8bae1dSRodney W. Grimes if ((ifp == NULL || 2074df8bae1dSRodney W. Grimes imo->imo_membership[i]->inm_ifp == ifp) && 2075df8bae1dSRodney W. Grimes imo->imo_membership[i]->inm_addr.s_addr == 2076cfe8b629SGarrett Wollman mreq.imr_multiaddr.s_addr) 2077df8bae1dSRodney W. Grimes break; 2078df8bae1dSRodney W. Grimes } 2079df8bae1dSRodney W. Grimes if (i == imo->imo_num_memberships) { 2080df8bae1dSRodney W. Grimes error = EADDRNOTAVAIL; 20819b626c29SGarrett Wollman splx(s); 2082df8bae1dSRodney W. Grimes break; 2083df8bae1dSRodney W. Grimes } 2084df8bae1dSRodney W. Grimes /* 2085df8bae1dSRodney W. Grimes * Give up the multicast address record to which the 2086df8bae1dSRodney W. Grimes * membership points. 2087df8bae1dSRodney W. Grimes */ 2088df8bae1dSRodney W. Grimes in_delmulti(imo->imo_membership[i]); 2089df8bae1dSRodney W. Grimes /* 2090df8bae1dSRodney W. Grimes * Remove the gap in the membership array. 2091df8bae1dSRodney W. Grimes */ 2092df8bae1dSRodney W. Grimes for (++i; i < imo->imo_num_memberships; ++i) 2093df8bae1dSRodney W. Grimes imo->imo_membership[i-1] = imo->imo_membership[i]; 2094df8bae1dSRodney W. Grimes --imo->imo_num_memberships; 20959b626c29SGarrett Wollman splx(s); 2096df8bae1dSRodney W. Grimes break; 2097df8bae1dSRodney W. Grimes 2098df8bae1dSRodney W. Grimes default: 2099df8bae1dSRodney W. Grimes error = EOPNOTSUPP; 2100df8bae1dSRodney W. Grimes break; 2101df8bae1dSRodney W. Grimes } 2102df8bae1dSRodney W. Grimes 2103df8bae1dSRodney W. Grimes /* 2104df8bae1dSRodney W. Grimes * If all options have default values, no need to keep the mbuf. 2105df8bae1dSRodney W. Grimes */ 2106df8bae1dSRodney W. Grimes if (imo->imo_multicast_ifp == NULL && 21071c5de19aSGarrett Wollman imo->imo_multicast_vif == -1 && 2108df8bae1dSRodney W. Grimes imo->imo_multicast_ttl == IP_DEFAULT_MULTICAST_TTL && 2109df8bae1dSRodney W. Grimes imo->imo_multicast_loop == IP_DEFAULT_MULTICAST_LOOP && 2110df8bae1dSRodney W. Grimes imo->imo_num_memberships == 0) { 2111df8bae1dSRodney W. Grimes free(*imop, M_IPMOPTS); 2112df8bae1dSRodney W. Grimes *imop = NULL; 2113df8bae1dSRodney W. Grimes } 2114df8bae1dSRodney W. Grimes 2115df8bae1dSRodney W. Grimes return (error); 2116df8bae1dSRodney W. Grimes } 2117df8bae1dSRodney W. Grimes 2118df8bae1dSRodney W. Grimes /* 2119df8bae1dSRodney W. Grimes * Return the IP multicast options in response to user getsockopt(). 2120df8bae1dSRodney W. Grimes */ 21210312fbe9SPoul-Henning Kamp static int 2122b375c9ecSLuigi Rizzo ip_getmoptions(sopt, imo) 2123b375c9ecSLuigi Rizzo struct sockopt *sopt; 2124b375c9ecSLuigi Rizzo register struct ip_moptions *imo; 2125df8bae1dSRodney W. Grimes { 2126cfe8b629SGarrett Wollman struct in_addr addr; 2127df8bae1dSRodney W. Grimes struct in_ifaddr *ia; 2128cfe8b629SGarrett Wollman int error, optval; 2129cfe8b629SGarrett Wollman u_char coptval; 2130df8bae1dSRodney W. Grimes 2131cfe8b629SGarrett Wollman error = 0; 2132cfe8b629SGarrett Wollman switch (sopt->sopt_name) { 2133f0068c4aSGarrett Wollman case IP_MULTICAST_VIF: 2134f0068c4aSGarrett Wollman if (imo != NULL) 2135cfe8b629SGarrett Wollman optval = imo->imo_multicast_vif; 2136f0068c4aSGarrett Wollman else 2137cfe8b629SGarrett Wollman optval = -1; 2138cfe8b629SGarrett Wollman error = sooptcopyout(sopt, &optval, sizeof optval); 2139cfe8b629SGarrett Wollman break; 2140f0068c4aSGarrett Wollman 2141df8bae1dSRodney W. Grimes case IP_MULTICAST_IF: 2142df8bae1dSRodney W. Grimes if (imo == NULL || imo->imo_multicast_ifp == NULL) 2143cfe8b629SGarrett Wollman addr.s_addr = INADDR_ANY; 214433841545SHajimu UMEMOTO else if (imo->imo_multicast_addr.s_addr) { 214533841545SHajimu UMEMOTO /* return the value user has set */ 214633841545SHajimu UMEMOTO addr = imo->imo_multicast_addr; 214733841545SHajimu UMEMOTO } else { 2148df8bae1dSRodney W. Grimes IFP_TO_IA(imo->imo_multicast_ifp, ia); 2149cfe8b629SGarrett Wollman addr.s_addr = (ia == NULL) ? INADDR_ANY 2150df8bae1dSRodney W. Grimes : IA_SIN(ia)->sin_addr.s_addr; 2151df8bae1dSRodney W. Grimes } 2152cfe8b629SGarrett Wollman error = sooptcopyout(sopt, &addr, sizeof addr); 2153cfe8b629SGarrett Wollman break; 2154df8bae1dSRodney W. Grimes 2155df8bae1dSRodney W. Grimes case IP_MULTICAST_TTL: 2156cfe8b629SGarrett Wollman if (imo == 0) 2157cfe8b629SGarrett Wollman optval = coptval = IP_DEFAULT_MULTICAST_TTL; 2158cfe8b629SGarrett Wollman else 2159cfe8b629SGarrett Wollman optval = coptval = imo->imo_multicast_ttl; 2160cfe8b629SGarrett Wollman if (sopt->sopt_valsize == 1) 2161cfe8b629SGarrett Wollman error = sooptcopyout(sopt, &coptval, 1); 2162cfe8b629SGarrett Wollman else 2163cfe8b629SGarrett Wollman error = sooptcopyout(sopt, &optval, sizeof optval); 2164cfe8b629SGarrett Wollman break; 2165df8bae1dSRodney W. Grimes 2166df8bae1dSRodney W. Grimes case IP_MULTICAST_LOOP: 2167cfe8b629SGarrett Wollman if (imo == 0) 2168cfe8b629SGarrett Wollman optval = coptval = IP_DEFAULT_MULTICAST_LOOP; 2169cfe8b629SGarrett Wollman else 2170cfe8b629SGarrett Wollman optval = coptval = imo->imo_multicast_loop; 2171cfe8b629SGarrett Wollman if (sopt->sopt_valsize == 1) 2172cfe8b629SGarrett Wollman error = sooptcopyout(sopt, &coptval, 1); 2173cfe8b629SGarrett Wollman else 2174cfe8b629SGarrett Wollman error = sooptcopyout(sopt, &optval, sizeof optval); 2175cfe8b629SGarrett Wollman break; 2176df8bae1dSRodney W. Grimes 2177df8bae1dSRodney W. Grimes default: 2178cfe8b629SGarrett Wollman error = ENOPROTOOPT; 2179cfe8b629SGarrett Wollman break; 2180df8bae1dSRodney W. Grimes } 2181cfe8b629SGarrett Wollman return (error); 2182df8bae1dSRodney W. Grimes } 2183df8bae1dSRodney W. Grimes 2184df8bae1dSRodney W. Grimes /* 2185df8bae1dSRodney W. Grimes * Discard the IP multicast options. 2186df8bae1dSRodney W. Grimes */ 2187df8bae1dSRodney W. Grimes void 2188b375c9ecSLuigi Rizzo ip_freemoptions(imo) 2189b375c9ecSLuigi Rizzo register struct ip_moptions *imo; 2190df8bae1dSRodney W. Grimes { 2191b375c9ecSLuigi Rizzo register int i; 2192df8bae1dSRodney W. Grimes 2193df8bae1dSRodney W. Grimes if (imo != NULL) { 2194df8bae1dSRodney W. Grimes for (i = 0; i < imo->imo_num_memberships; ++i) 2195df8bae1dSRodney W. Grimes in_delmulti(imo->imo_membership[i]); 2196df8bae1dSRodney W. Grimes free(imo, M_IPMOPTS); 2197df8bae1dSRodney W. Grimes } 2198df8bae1dSRodney W. Grimes } 2199df8bae1dSRodney W. Grimes 2200df8bae1dSRodney W. Grimes /* 2201df8bae1dSRodney W. Grimes * Routine called from ip_output() to loop back a copy of an IP multicast 2202df8bae1dSRodney W. Grimes * packet to the input queue of a specified interface. Note that this 2203df8bae1dSRodney W. Grimes * calls the output routine of the loopback "driver", but with an interface 2204f5fea3ddSPaul Traina * pointer that might NOT be a loopback interface -- evil, but easier than 2205f5fea3ddSPaul Traina * replicating that code here. 2206df8bae1dSRodney W. Grimes */ 2207df8bae1dSRodney W. Grimes static void 2208b375c9ecSLuigi Rizzo ip_mloopback(ifp, m, dst, hlen) 2209b375c9ecSLuigi Rizzo struct ifnet *ifp; 2210b375c9ecSLuigi Rizzo register struct mbuf *m; 2211b375c9ecSLuigi Rizzo register struct sockaddr_in *dst; 2212b375c9ecSLuigi Rizzo int hlen; 2213df8bae1dSRodney W. Grimes { 2214b375c9ecSLuigi Rizzo register struct ip *ip; 2215df8bae1dSRodney W. Grimes struct mbuf *copym; 2216df8bae1dSRodney W. Grimes 2217b375c9ecSLuigi Rizzo copym = m_copy(m, 0, M_COPYALL); 221886b1d6d2SBill Fenner if (copym != NULL && (copym->m_flags & M_EXT || copym->m_len < hlen)) 221986b1d6d2SBill Fenner copym = m_pullup(copym, hlen); 2220df8bae1dSRodney W. Grimes if (copym != NULL) { 2221df8bae1dSRodney W. Grimes /* 2222df8bae1dSRodney W. Grimes * We don't bother to fragment if the IP length is greater 2223df8bae1dSRodney W. Grimes * than the interface's MTU. Can this possibly matter? 2224df8bae1dSRodney W. Grimes */ 2225df8bae1dSRodney W. Grimes ip = mtod(copym, struct ip *); 2226fd8e4ebcSMike Barcroft ip->ip_len = htons(ip->ip_len); 2227fd8e4ebcSMike Barcroft ip->ip_off = htons(ip->ip_off); 2228df8bae1dSRodney W. Grimes ip->ip_sum = 0; 222986b1d6d2SBill Fenner ip->ip_sum = in_cksum(copym, hlen); 22309c9137eaSGarrett Wollman /* 22319c9137eaSGarrett Wollman * NB: 2232e1596dffSBill Fenner * It's not clear whether there are any lingering 2233e1596dffSBill Fenner * reentrancy problems in other areas which might 2234e1596dffSBill Fenner * be exposed by using ip_input directly (in 2235e1596dffSBill Fenner * particular, everything which modifies the packet 2236e1596dffSBill Fenner * in-place). Yet another option is using the 2237e1596dffSBill Fenner * protosw directly to deliver the looped back 2238e1596dffSBill Fenner * packet. For the moment, we'll err on the side 2239ed7509acSJulian Elischer * of safety by using if_simloop(). 22409c9137eaSGarrett Wollman */ 2241201c2527SJulian Elischer #if 1 /* XXX */ 2242201c2527SJulian Elischer if (dst->sin_family != AF_INET) { 22432b8a366cSJulian Elischer printf("ip_mloopback: bad address family %d\n", 2244201c2527SJulian Elischer dst->sin_family); 2245201c2527SJulian Elischer dst->sin_family = AF_INET; 2246201c2527SJulian Elischer } 2247201c2527SJulian Elischer #endif 2248201c2527SJulian Elischer 22499c9137eaSGarrett Wollman #ifdef notdef 2250e1596dffSBill Fenner copym->m_pkthdr.rcvif = ifp; 2251ed7509acSJulian Elischer ip_input(copym); 22529c9137eaSGarrett Wollman #else 225350c6dc99SJonathan Lemon /* if the checksum hasn't been computed, mark it as valid */ 225450c6dc99SJonathan Lemon if (copym->m_pkthdr.csum_flags & CSUM_DELAY_DATA) { 225550c6dc99SJonathan Lemon copym->m_pkthdr.csum_flags |= 225650c6dc99SJonathan Lemon CSUM_DATA_VALID | CSUM_PSEUDO_HDR; 225750c6dc99SJonathan Lemon copym->m_pkthdr.csum_data = 0xffff; 225850c6dc99SJonathan Lemon } 225906a429a3SArchie Cobbs if_simloop(ifp, copym, dst->sin_family, 0); 22609c9137eaSGarrett Wollman #endif 2261df8bae1dSRodney W. Grimes } 2262df8bae1dSRodney W. Grimes } 2263