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" 45fbd1372aSJoerg Wunsch 46df8bae1dSRodney W. Grimes #include <sys/param.h> 4726f9a767SRodney W. Grimes #include <sys/systm.h> 48f0f6d643SLuigi Rizzo #include <sys/kernel.h> 494ed84624SRobert Watson #include <sys/mac.h> 50df8bae1dSRodney W. Grimes #include <sys/malloc.h> 51df8bae1dSRodney W. Grimes #include <sys/mbuf.h> 52df8bae1dSRodney W. Grimes #include <sys/protosw.h> 53df8bae1dSRodney W. Grimes #include <sys/socket.h> 54df8bae1dSRodney W. Grimes #include <sys/socketvar.h> 55df8bae1dSRodney W. Grimes 56df8bae1dSRodney W. Grimes #include <net/if.h> 57df8bae1dSRodney W. Grimes #include <net/route.h> 58df8bae1dSRodney W. Grimes 59df8bae1dSRodney W. Grimes #include <netinet/in.h> 60df8bae1dSRodney W. Grimes #include <netinet/in_systm.h> 61df8bae1dSRodney W. Grimes #include <netinet/ip.h> 62df8bae1dSRodney W. Grimes #include <netinet/in_pcb.h> 63df8bae1dSRodney W. Grimes #include <netinet/in_var.h> 64df8bae1dSRodney W. Grimes #include <netinet/ip_var.h> 65df8bae1dSRodney W. Grimes 669c9137eaSGarrett Wollman #include <machine/in_cksum.h> 67df8bae1dSRodney W. Grimes 68a1c995b6SPoul-Henning Kamp static MALLOC_DEFINE(M_IPMOPTS, "ip_moptions", "internet multicast options"); 6955166637SPoul-Henning Kamp 706a800098SYoshinobu Inoue #ifdef IPSEC 716a800098SYoshinobu Inoue #include <netinet6/ipsec.h> 726a800098SYoshinobu Inoue #include <netkey/key.h> 736a800098SYoshinobu Inoue #ifdef IPSEC_DEBUG 746a800098SYoshinobu Inoue #include <netkey/key_debug.h> 756a800098SYoshinobu Inoue #else 766a800098SYoshinobu Inoue #define KEYDEBUG(lev,arg) 776a800098SYoshinobu Inoue #endif 786a800098SYoshinobu Inoue #endif /*IPSEC*/ 796a800098SYoshinobu Inoue 80b9234fafSSam Leffler #ifdef FAST_IPSEC 81b9234fafSSam Leffler #include <netipsec/ipsec.h> 82b9234fafSSam Leffler #include <netipsec/xform.h> 83b9234fafSSam Leffler #include <netipsec/key.h> 84b9234fafSSam Leffler #endif /*FAST_IPSEC*/ 85b9234fafSSam Leffler 86cfe8b629SGarrett Wollman #include <netinet/ip_fw.h> 87b715f178SLuigi Rizzo #include <netinet/ip_dummynet.h> 88b715f178SLuigi Rizzo 892b25acc1SLuigi Rizzo #define print_ip(x, a, y) printf("%s %d.%d.%d.%d%s",\ 902b25acc1SLuigi Rizzo x, (ntohl(a.s_addr)>>24)&0xFF,\ 91f9e354dfSJulian Elischer (ntohl(a.s_addr)>>16)&0xFF,\ 92f9e354dfSJulian Elischer (ntohl(a.s_addr)>>8)&0xFF,\ 932b25acc1SLuigi Rizzo (ntohl(a.s_addr))&0xFF, y); 94f9e354dfSJulian Elischer 95f23b4c91SGarrett Wollman u_short ip_id; 96f23b4c91SGarrett Wollman 974d77a549SAlfred Perlstein static struct mbuf *ip_insertoptions(struct mbuf *, struct mbuf *, int *); 984d77a549SAlfred Perlstein static struct ifnet *ip_multicast_if(struct in_addr *, int *); 99df8bae1dSRodney W. Grimes static void ip_mloopback 1004d77a549SAlfred Perlstein (struct ifnet *, struct mbuf *, struct sockaddr_in *, int); 1010312fbe9SPoul-Henning Kamp static int ip_getmoptions 1024d77a549SAlfred Perlstein (struct sockopt *, struct ip_moptions *); 1034d77a549SAlfred Perlstein static int ip_pcbopts(int, struct mbuf **, struct mbuf *); 1040312fbe9SPoul-Henning Kamp static int ip_setmoptions 1054d77a549SAlfred Perlstein (struct sockopt *, struct ip_moptions **); 106df8bae1dSRodney W. Grimes 1074d77a549SAlfred Perlstein int ip_optcopy(struct ip *, struct ip *); 108afed1b49SDarren Reed 109afed1b49SDarren Reed 11093e0e116SJulian Elischer extern struct protosw inetsw[]; 11193e0e116SJulian Elischer 112df8bae1dSRodney W. Grimes /* 113df8bae1dSRodney W. Grimes * IP output. The packet in mbuf chain m contains a skeletal IP 114df8bae1dSRodney W. Grimes * header (with len, off, ttl, proto, tos, src, dst). 115df8bae1dSRodney W. Grimes * The mbuf chain containing the packet will be freed. 116df8bae1dSRodney W. Grimes * The mbuf opt, if present, will not be freed. 117df8bae1dSRodney W. Grimes */ 118df8bae1dSRodney W. Grimes int 1195d846453SSam Leffler ip_output(m0, opt, ro, flags, imo, inp) 120df8bae1dSRodney W. Grimes struct mbuf *m0; 121df8bae1dSRodney W. Grimes struct mbuf *opt; 122df8bae1dSRodney W. Grimes struct route *ro; 123df8bae1dSRodney W. Grimes int flags; 124df8bae1dSRodney W. Grimes struct ip_moptions *imo; 1255d846453SSam Leffler struct inpcb *inp; 126df8bae1dSRodney W. Grimes { 12723bf9953SPoul-Henning Kamp struct ip *ip, *mhip; 1282b25acc1SLuigi Rizzo struct ifnet *ifp = NULL; /* keep compiler happy */ 1292b25acc1SLuigi Rizzo struct mbuf *m; 13023bf9953SPoul-Henning Kamp int hlen = sizeof (struct ip); 131df8bae1dSRodney W. Grimes int len, off, error = 0; 1322b25acc1SLuigi Rizzo struct sockaddr_in *dst = NULL; /* keep compiler happy */ 1333956b023SLuigi Rizzo struct in_ifaddr *ia = NULL; 134db4f9cc7SJonathan Lemon int isbroadcast, sw_csum; 1353efc3014SJulian Elischer struct in_addr pkt_dst; 1366a800098SYoshinobu Inoue #ifdef IPSEC 137e3f406b3SRuslan Ermilov struct route iproute; 1386a800098SYoshinobu Inoue struct secpolicy *sp = NULL; 1395d846453SSam Leffler struct socket *so = inp ? inp->inp_socket : NULL; 1406a800098SYoshinobu Inoue #endif 141b9234fafSSam Leffler #ifdef FAST_IPSEC 142b9234fafSSam Leffler struct route iproute; 143b9234fafSSam Leffler struct m_tag *mtag; 144b9234fafSSam Leffler struct secpolicy *sp = NULL; 145b9234fafSSam Leffler struct tdb_ident *tdbi; 146b9234fafSSam Leffler int s; 147b9234fafSSam Leffler #endif /* FAST_IPSEC */ 1482b25acc1SLuigi Rizzo struct ip_fw_args args; 1492b25acc1SLuigi Rizzo int src_was_INADDR_ANY = 0; /* as the name says... */ 150c4ac87eaSDarren Reed #ifdef PFIL_HOOKS 151c4ac87eaSDarren Reed struct packet_filter_hook *pfh; 152c4ac87eaSDarren Reed struct mbuf *m1; 153c4ac87eaSDarren Reed int rv; 154c4ac87eaSDarren Reed #endif /* PFIL_HOOKS */ 155b715f178SLuigi Rizzo 1562b25acc1SLuigi Rizzo args.eh = NULL; 1572b25acc1SLuigi Rizzo args.rule = NULL; 1582b25acc1SLuigi Rizzo args.next_hop = NULL; 1592b25acc1SLuigi Rizzo args.divert_rule = 0; /* divert cookie */ 1608948e4baSArchie Cobbs 1612b25acc1SLuigi Rizzo /* Grab info from MT_TAG mbufs prepended to the chain. */ 1622b25acc1SLuigi Rizzo for (; m0 && m0->m_type == MT_TAG; m0 = m0->m_next) { 1635d846453SSam Leffler switch(m0->_m_tag_id) { 1642b25acc1SLuigi Rizzo default: 1652b25acc1SLuigi Rizzo printf("ip_output: unrecognised MT_TAG tag %d\n", 1665d846453SSam Leffler m0->_m_tag_id); 1672b25acc1SLuigi Rizzo break; 1682b25acc1SLuigi Rizzo 1692b25acc1SLuigi Rizzo case PACKET_TAG_DUMMYNET: 170b715f178SLuigi Rizzo /* 171b715f178SLuigi Rizzo * the packet was already tagged, so part of the 172b715f178SLuigi Rizzo * processing was already done, and we need to go down. 1736bc748b0SLuigi Rizzo * Get parameters from the header. 174b715f178SLuigi Rizzo */ 1752b25acc1SLuigi Rizzo args.rule = ((struct dn_pkt *)m0)->rule; 176d1f04b29SLuigi Rizzo opt = NULL ; 1772b25acc1SLuigi Rizzo ro = & ( ((struct dn_pkt *)m0)->ro ) ; 178d1f04b29SLuigi Rizzo imo = NULL ; 1792b25acc1SLuigi Rizzo dst = ((struct dn_pkt *)m0)->dn_dst ; 1802b25acc1SLuigi Rizzo ifp = ((struct dn_pkt *)m0)->ifp ; 1812b25acc1SLuigi Rizzo flags = ((struct dn_pkt *)m0)->flags ; 1822b25acc1SLuigi Rizzo break; 183d1f04b29SLuigi Rizzo 1842b25acc1SLuigi Rizzo case PACKET_TAG_DIVERT: 1857627c6cbSMaxime Henrion args.divert_rule = (intptr_t)m0->m_data & 0xffff; 1862b25acc1SLuigi Rizzo break; 1872b25acc1SLuigi Rizzo 1882b25acc1SLuigi Rizzo case PACKET_TAG_IPFORWARD: 1892b25acc1SLuigi Rizzo args.next_hop = (struct sockaddr_in *)m0->m_data; 1902b25acc1SLuigi Rizzo break; 1912b25acc1SLuigi Rizzo } 1922b25acc1SLuigi Rizzo } 1932b25acc1SLuigi Rizzo m = m0; 1942b25acc1SLuigi Rizzo 1952b25acc1SLuigi Rizzo KASSERT(!m || (m->m_flags & M_PKTHDR) != 0, ("ip_output: no HDR")); 196b9234fafSSam Leffler #ifndef FAST_IPSEC 197b9234fafSSam Leffler KASSERT(ro != NULL, ("ip_output: no route, proto %d", 198b9234fafSSam Leffler mtod(m, struct ip *)->ip_p)); 199b9234fafSSam Leffler #endif 2002b25acc1SLuigi Rizzo 2012b25acc1SLuigi Rizzo if (args.rule != NULL) { /* dummynet already saw us */ 202b715f178SLuigi Rizzo ip = mtod(m, struct ip *); 20353be11f6SPoul-Henning Kamp hlen = ip->ip_hl << 2 ; 2043956b023SLuigi Rizzo if (ro->ro_rt) 2059a10980eSJonathan Lemon ia = ifatoia(ro->ro_rt->rt_ifa); 206b715f178SLuigi Rizzo goto sendit; 2072b25acc1SLuigi Rizzo } 208db40007dSAndrew R. Reiter 209df8bae1dSRodney W. Grimes if (opt) { 210cb7641e8SMaxim Konovalov len = 0; 211df8bae1dSRodney W. Grimes m = ip_insertoptions(m, opt, &len); 212cb7641e8SMaxim Konovalov if (len != 0) 213df8bae1dSRodney W. Grimes hlen = len; 214df8bae1dSRodney W. Grimes } 215df8bae1dSRodney W. Grimes ip = mtod(m, struct ip *); 2162b25acc1SLuigi Rizzo pkt_dst = args.next_hop ? args.next_hop->sin_addr : ip->ip_dst; 2173efc3014SJulian Elischer 218df8bae1dSRodney W. Grimes /* 219df8bae1dSRodney W. Grimes * Fill in IP header. 220df8bae1dSRodney W. Grimes */ 221df8bae1dSRodney W. Grimes if ((flags & (IP_FORWARDING|IP_RAWOUTPUT)) == 0) { 22253be11f6SPoul-Henning Kamp ip->ip_v = IPVERSION; 22353be11f6SPoul-Henning Kamp ip->ip_hl = hlen >> 2; 224df8bae1dSRodney W. Grimes ip->ip_off &= IP_DF; 22564dddc18SKris Kennaway #ifdef RANDOM_IP_ID 22664dddc18SKris Kennaway ip->ip_id = ip_randomid(); 22764dddc18SKris Kennaway #else 228e30177e0SRuslan Ermilov ip->ip_id = htons(ip_id++); 22964dddc18SKris Kennaway #endif 230df8bae1dSRodney W. Grimes ipstat.ips_localout++; 231df8bae1dSRodney W. Grimes } else { 23253be11f6SPoul-Henning Kamp hlen = ip->ip_hl << 2; 233df8bae1dSRodney W. Grimes } 2349c9137eaSGarrett Wollman 235b9234fafSSam Leffler #ifdef FAST_IPSEC 236b9234fafSSam Leffler if (ro == NULL) { 237b9234fafSSam Leffler ro = &iproute; 238b9234fafSSam Leffler bzero(ro, sizeof (*ro)); 239b9234fafSSam Leffler } 240b9234fafSSam Leffler #endif /* FAST_IPSEC */ 241df8bae1dSRodney W. Grimes dst = (struct sockaddr_in *)&ro->ro_dst; 242df8bae1dSRodney W. Grimes /* 243df8bae1dSRodney W. Grimes * If there is a cached route, 244df8bae1dSRodney W. Grimes * check that it is to the same destination 245df8bae1dSRodney W. Grimes * and is still up. If not, free it and try again. 246a4a6e773SHajimu UMEMOTO * The address family should also be checked in case of sharing the 247a4a6e773SHajimu UMEMOTO * cache with IPv6. 248df8bae1dSRodney W. Grimes */ 249df8bae1dSRodney W. Grimes if (ro->ro_rt && ((ro->ro_rt->rt_flags & RTF_UP) == 0 || 250a4a6e773SHajimu UMEMOTO dst->sin_family != AF_INET || 2513efc3014SJulian Elischer dst->sin_addr.s_addr != pkt_dst.s_addr)) { 252df8bae1dSRodney W. Grimes RTFREE(ro->ro_rt); 253df8bae1dSRodney W. Grimes ro->ro_rt = (struct rtentry *)0; 254df8bae1dSRodney W. Grimes } 255df8bae1dSRodney W. Grimes if (ro->ro_rt == 0) { 256a4a6e773SHajimu UMEMOTO bzero(dst, sizeof(*dst)); 257df8bae1dSRodney W. Grimes dst->sin_family = AF_INET; 258df8bae1dSRodney W. Grimes dst->sin_len = sizeof(*dst); 2593efc3014SJulian Elischer dst->sin_addr = pkt_dst; 260df8bae1dSRodney W. Grimes } 261df8bae1dSRodney W. Grimes /* 262df8bae1dSRodney W. Grimes * If routing to interface only, 263df8bae1dSRodney W. Grimes * short circuit routing lookup. 264df8bae1dSRodney W. Grimes */ 265df8bae1dSRodney W. Grimes if (flags & IP_ROUTETOIF) { 266df8bae1dSRodney W. Grimes if ((ia = ifatoia(ifa_ifwithdstaddr(sintosa(dst)))) == 0 && 267df8bae1dSRodney W. Grimes (ia = ifatoia(ifa_ifwithnet(sintosa(dst)))) == 0) { 268df8bae1dSRodney W. Grimes ipstat.ips_noroute++; 269df8bae1dSRodney W. Grimes error = ENETUNREACH; 270df8bae1dSRodney W. Grimes goto bad; 271df8bae1dSRodney W. Grimes } 272df8bae1dSRodney W. Grimes ifp = ia->ia_ifp; 273df8bae1dSRodney W. Grimes ip->ip_ttl = 1; 2749f9b3dc4SGarrett Wollman isbroadcast = in_broadcast(dst->sin_addr, ifp); 2753afefa39SDaniel C. Sobral } else if (IN_MULTICAST(ntohl(ip->ip_dst.s_addr)) && 27638c1bc35SRuslan Ermilov imo != NULL && imo->imo_multicast_ifp != NULL) { 2773afefa39SDaniel C. Sobral /* 27838c1bc35SRuslan Ermilov * Bypass the normal routing lookup for multicast 27938c1bc35SRuslan Ermilov * packets if the interface is specified. 2803afefa39SDaniel C. Sobral */ 28138c1bc35SRuslan Ermilov ifp = imo->imo_multicast_ifp; 28238c1bc35SRuslan Ermilov IFP_TO_IA(ifp, ia); 28338c1bc35SRuslan Ermilov isbroadcast = 0; /* fool gcc */ 284df8bae1dSRodney W. Grimes } else { 2852c17fe93SGarrett Wollman /* 286f2c2962eSRuslan Ermilov * If this is the case, we probably don't want to allocate 287f2c2962eSRuslan Ermilov * a protocol-cloned route since we didn't get one from the 288f2c2962eSRuslan Ermilov * ULP. This lets TCP do its thing, while not burdening 289f2c2962eSRuslan Ermilov * forwarding or ICMP with the overhead of cloning a route. 290f2c2962eSRuslan Ermilov * Of course, we still want to do any cloning requested by 291f2c2962eSRuslan Ermilov * the link layer, as this is probably required in all cases 292f2c2962eSRuslan Ermilov * for correct operation (as it is for ARP). 2932c17fe93SGarrett Wollman */ 294df8bae1dSRodney W. Grimes if (ro->ro_rt == 0) 2952c17fe93SGarrett Wollman rtalloc_ign(ro, RTF_PRCLONING); 296df8bae1dSRodney W. Grimes if (ro->ro_rt == 0) { 297df8bae1dSRodney W. Grimes ipstat.ips_noroute++; 298df8bae1dSRodney W. Grimes error = EHOSTUNREACH; 299df8bae1dSRodney W. Grimes goto bad; 300df8bae1dSRodney W. Grimes } 301df8bae1dSRodney W. Grimes ia = ifatoia(ro->ro_rt->rt_ifa); 302df8bae1dSRodney W. Grimes ifp = ro->ro_rt->rt_ifp; 303df8bae1dSRodney W. Grimes ro->ro_rt->rt_use++; 304df8bae1dSRodney W. Grimes if (ro->ro_rt->rt_flags & RTF_GATEWAY) 305f2c2962eSRuslan Ermilov dst = (struct sockaddr_in *)ro->ro_rt->rt_gateway; 3069f9b3dc4SGarrett Wollman if (ro->ro_rt->rt_flags & RTF_HOST) 307f2c2962eSRuslan Ermilov isbroadcast = (ro->ro_rt->rt_flags & RTF_BROADCAST); 3089f9b3dc4SGarrett Wollman else 3099f9b3dc4SGarrett Wollman isbroadcast = in_broadcast(dst->sin_addr, ifp); 310df8bae1dSRodney W. Grimes } 3113efc3014SJulian Elischer if (IN_MULTICAST(ntohl(pkt_dst.s_addr))) { 312df8bae1dSRodney W. Grimes struct in_multi *inm; 313df8bae1dSRodney W. Grimes 314df8bae1dSRodney W. Grimes m->m_flags |= M_MCAST; 315df8bae1dSRodney W. Grimes /* 316df8bae1dSRodney W. Grimes * IP destination address is multicast. Make sure "dst" 317df8bae1dSRodney W. Grimes * still points to the address in "ro". (It may have been 318df8bae1dSRodney W. Grimes * changed to point to a gateway address, above.) 319df8bae1dSRodney W. Grimes */ 320df8bae1dSRodney W. Grimes dst = (struct sockaddr_in *)&ro->ro_dst; 321df8bae1dSRodney W. Grimes /* 322df8bae1dSRodney W. Grimes * See if the caller provided any multicast options 323df8bae1dSRodney W. Grimes */ 324df8bae1dSRodney W. Grimes if (imo != NULL) { 325df8bae1dSRodney W. Grimes ip->ip_ttl = imo->imo_multicast_ttl; 3261c5de19aSGarrett Wollman if (imo->imo_multicast_vif != -1) 3271c5de19aSGarrett Wollman ip->ip_src.s_addr = 3281c5de19aSGarrett Wollman ip_mcast_src(imo->imo_multicast_vif); 329df8bae1dSRodney W. Grimes } else 330df8bae1dSRodney W. Grimes ip->ip_ttl = IP_DEFAULT_MULTICAST_TTL; 331df8bae1dSRodney W. Grimes /* 332df8bae1dSRodney W. Grimes * Confirm that the outgoing interface supports multicast. 333df8bae1dSRodney W. Grimes */ 3341c5de19aSGarrett Wollman if ((imo == NULL) || (imo->imo_multicast_vif == -1)) { 335df8bae1dSRodney W. Grimes if ((ifp->if_flags & IFF_MULTICAST) == 0) { 336df8bae1dSRodney W. Grimes ipstat.ips_noroute++; 337df8bae1dSRodney W. Grimes error = ENETUNREACH; 338df8bae1dSRodney W. Grimes goto bad; 339df8bae1dSRodney W. Grimes } 3401c5de19aSGarrett Wollman } 341df8bae1dSRodney W. Grimes /* 342df8bae1dSRodney W. Grimes * If source address not specified yet, use address 343df8bae1dSRodney W. Grimes * of outgoing interface. 344df8bae1dSRodney W. Grimes */ 345df8bae1dSRodney W. Grimes if (ip->ip_src.s_addr == INADDR_ANY) { 34638c1bc35SRuslan Ermilov /* Interface may have no addresses. */ 34738c1bc35SRuslan Ermilov if (ia != NULL) 34838c1bc35SRuslan Ermilov ip->ip_src = IA_SIN(ia)->sin_addr; 349df8bae1dSRodney W. Grimes } 350df8bae1dSRodney W. Grimes 35192bdb2faSBill Fenner if (ip_mrouter && (flags & IP_FORWARDING) == 0) { 35292bdb2faSBill Fenner /* 35392bdb2faSBill Fenner * XXX 35492bdb2faSBill Fenner * delayed checksums are not currently 35592bdb2faSBill Fenner * compatible with IP multicast routing 35692bdb2faSBill Fenner */ 35792bdb2faSBill Fenner if (m->m_pkthdr.csum_flags & CSUM_DELAY_DATA) { 35892bdb2faSBill Fenner in_delayed_cksum(m); 35992bdb2faSBill Fenner m->m_pkthdr.csum_flags &= 36092bdb2faSBill Fenner ~CSUM_DELAY_DATA; 36192bdb2faSBill Fenner } 36292bdb2faSBill Fenner } 3633efc3014SJulian Elischer IN_LOOKUP_MULTI(pkt_dst, ifp, inm); 364df8bae1dSRodney W. Grimes if (inm != NULL && 365df8bae1dSRodney W. Grimes (imo == NULL || imo->imo_multicast_loop)) { 366df8bae1dSRodney W. Grimes /* 367df8bae1dSRodney W. Grimes * If we belong to the destination multicast group 368df8bae1dSRodney W. Grimes * on the outgoing interface, and the caller did not 369df8bae1dSRodney W. Grimes * forbid loopback, loop back a copy. 370df8bae1dSRodney W. Grimes */ 37186b1d6d2SBill Fenner ip_mloopback(ifp, m, dst, hlen); 372df8bae1dSRodney W. Grimes } 373df8bae1dSRodney W. Grimes else { 374df8bae1dSRodney W. Grimes /* 375df8bae1dSRodney W. Grimes * If we are acting as a multicast router, perform 376df8bae1dSRodney W. Grimes * multicast forwarding as if the packet had just 377df8bae1dSRodney W. Grimes * arrived on the interface to which we are about 378df8bae1dSRodney W. Grimes * to send. The multicast forwarding function 379df8bae1dSRodney W. Grimes * recursively calls this function, using the 380df8bae1dSRodney W. Grimes * IP_FORWARDING flag to prevent infinite recursion. 381df8bae1dSRodney W. Grimes * 382df8bae1dSRodney W. Grimes * Multicasts that are looped back by ip_mloopback(), 383df8bae1dSRodney W. Grimes * above, will be forwarded by the ip_input() routine, 384df8bae1dSRodney W. Grimes * if necessary. 385df8bae1dSRodney W. Grimes */ 386df8bae1dSRodney W. Grimes if (ip_mrouter && (flags & IP_FORWARDING) == 0) { 387f0068c4aSGarrett Wollman /* 388f0068c4aSGarrett Wollman * Check if rsvp daemon is running. If not, don't 389f0068c4aSGarrett Wollman * set ip_moptions. This ensures that the packet 390f0068c4aSGarrett Wollman * is multicast and not just sent down one link 391f0068c4aSGarrett Wollman * as prescribed by rsvpd. 392f0068c4aSGarrett Wollman */ 393b124e4f2SGarrett Wollman if (!rsvp_on) 394f0068c4aSGarrett Wollman imo = NULL; 395f0068c4aSGarrett Wollman if (ip_mforward(ip, ifp, m, imo) != 0) { 396df8bae1dSRodney W. Grimes m_freem(m); 397df8bae1dSRodney W. Grimes goto done; 398df8bae1dSRodney W. Grimes } 399df8bae1dSRodney W. Grimes } 400df8bae1dSRodney W. Grimes } 4015e9ae478SGarrett Wollman 402df8bae1dSRodney W. Grimes /* 403df8bae1dSRodney W. Grimes * Multicasts with a time-to-live of zero may be looped- 404df8bae1dSRodney W. Grimes * back, above, but must not be transmitted on a network. 405df8bae1dSRodney W. Grimes * Also, multicasts addressed to the loopback interface 406df8bae1dSRodney W. Grimes * are not sent -- the above call to ip_mloopback() will 407df8bae1dSRodney W. Grimes * loop back a copy if this host actually belongs to the 408df8bae1dSRodney W. Grimes * destination group on the loopback interface. 409df8bae1dSRodney W. Grimes */ 410f5fea3ddSPaul Traina if (ip->ip_ttl == 0 || ifp->if_flags & IFF_LOOPBACK) { 411df8bae1dSRodney W. Grimes m_freem(m); 412df8bae1dSRodney W. Grimes goto done; 413df8bae1dSRodney W. Grimes } 414df8bae1dSRodney W. Grimes 415df8bae1dSRodney W. Grimes goto sendit; 416df8bae1dSRodney W. Grimes } 417df8bae1dSRodney W. Grimes #ifndef notdef 418df8bae1dSRodney W. Grimes /* 4192b25acc1SLuigi Rizzo * If the source address is not specified yet, use the address 4202b25acc1SLuigi Rizzo * of the outoing interface. In case, keep note we did that, so 4212b25acc1SLuigi Rizzo * if the the firewall changes the next-hop causing the output 4222b25acc1SLuigi Rizzo * interface to change, we can fix that. 423df8bae1dSRodney W. Grimes */ 424f9e354dfSJulian Elischer if (ip->ip_src.s_addr == INADDR_ANY) { 42538c1bc35SRuslan Ermilov /* Interface may have no addresses. */ 42638c1bc35SRuslan Ermilov if (ia != NULL) { 427df8bae1dSRodney W. Grimes ip->ip_src = IA_SIN(ia)->sin_addr; 4282b25acc1SLuigi Rizzo src_was_INADDR_ANY = 1; 429f9e354dfSJulian Elischer } 43038c1bc35SRuslan Ermilov } 431f9e354dfSJulian Elischer #endif /* notdef */ 432df8bae1dSRodney W. Grimes /* 433b5390296SDavid Greenman * Verify that we have any chance at all of being able to queue 434b5390296SDavid Greenman * the packet or packet fragments 435b5390296SDavid Greenman */ 436b5390296SDavid Greenman if ((ifp->if_snd.ifq_len + ip->ip_len / ifp->if_mtu + 1) >= 437b5390296SDavid Greenman ifp->if_snd.ifq_maxlen) { 438b5390296SDavid Greenman error = ENOBUFS; 43935609d45SJonathan Lemon ipstat.ips_odropped++; 440b5390296SDavid Greenman goto bad; 441b5390296SDavid Greenman } 442b5390296SDavid Greenman 443b5390296SDavid Greenman /* 444df8bae1dSRodney W. Grimes * Look for broadcast address and 4458c3f5566SRuslan Ermilov * verify user is allowed to send 446df8bae1dSRodney W. Grimes * such a packet. 447df8bae1dSRodney W. Grimes */ 4489f9b3dc4SGarrett Wollman if (isbroadcast) { 449df8bae1dSRodney W. Grimes if ((ifp->if_flags & IFF_BROADCAST) == 0) { 450df8bae1dSRodney W. Grimes error = EADDRNOTAVAIL; 451df8bae1dSRodney W. Grimes goto bad; 452df8bae1dSRodney W. Grimes } 453df8bae1dSRodney W. Grimes if ((flags & IP_ALLOWBROADCAST) == 0) { 454df8bae1dSRodney W. Grimes error = EACCES; 455df8bae1dSRodney W. Grimes goto bad; 456df8bae1dSRodney W. Grimes } 457df8bae1dSRodney W. Grimes /* don't allow broadcast messages to be fragmented */ 458df8bae1dSRodney W. Grimes if ((u_short)ip->ip_len > ifp->if_mtu) { 459df8bae1dSRodney W. Grimes error = EMSGSIZE; 460df8bae1dSRodney W. Grimes goto bad; 461df8bae1dSRodney W. Grimes } 462df8bae1dSRodney W. Grimes m->m_flags |= M_BCAST; 4639f9b3dc4SGarrett Wollman } else { 464df8bae1dSRodney W. Grimes m->m_flags &= ~M_BCAST; 4659f9b3dc4SGarrett Wollman } 466df8bae1dSRodney W. Grimes 467f1743588SDarren Reed sendit: 46833841545SHajimu UMEMOTO #ifdef IPSEC 46933841545SHajimu UMEMOTO /* get SP for this packet */ 47033841545SHajimu UMEMOTO if (so == NULL) 47133841545SHajimu UMEMOTO sp = ipsec4_getpolicybyaddr(m, IPSEC_DIR_OUTBOUND, flags, &error); 47233841545SHajimu UMEMOTO else 47333841545SHajimu UMEMOTO sp = ipsec4_getpolicybysock(m, IPSEC_DIR_OUTBOUND, so, &error); 47433841545SHajimu UMEMOTO 47533841545SHajimu UMEMOTO if (sp == NULL) { 47633841545SHajimu UMEMOTO ipsecstat.out_inval++; 47733841545SHajimu UMEMOTO goto bad; 47833841545SHajimu UMEMOTO } 47933841545SHajimu UMEMOTO 48033841545SHajimu UMEMOTO error = 0; 48133841545SHajimu UMEMOTO 48233841545SHajimu UMEMOTO /* check policy */ 48333841545SHajimu UMEMOTO switch (sp->policy) { 48433841545SHajimu UMEMOTO case IPSEC_POLICY_DISCARD: 48533841545SHajimu UMEMOTO /* 48633841545SHajimu UMEMOTO * This packet is just discarded. 48733841545SHajimu UMEMOTO */ 48833841545SHajimu UMEMOTO ipsecstat.out_polvio++; 48933841545SHajimu UMEMOTO goto bad; 49033841545SHajimu UMEMOTO 49133841545SHajimu UMEMOTO case IPSEC_POLICY_BYPASS: 49233841545SHajimu UMEMOTO case IPSEC_POLICY_NONE: 49333841545SHajimu UMEMOTO /* no need to do IPsec. */ 49433841545SHajimu UMEMOTO goto skip_ipsec; 49533841545SHajimu UMEMOTO 49633841545SHajimu UMEMOTO case IPSEC_POLICY_IPSEC: 49733841545SHajimu UMEMOTO if (sp->req == NULL) { 49833841545SHajimu UMEMOTO /* acquire a policy */ 49933841545SHajimu UMEMOTO error = key_spdacquire(sp); 50033841545SHajimu UMEMOTO goto bad; 50133841545SHajimu UMEMOTO } 50233841545SHajimu UMEMOTO break; 50333841545SHajimu UMEMOTO 50433841545SHajimu UMEMOTO case IPSEC_POLICY_ENTRUST: 50533841545SHajimu UMEMOTO default: 50633841545SHajimu UMEMOTO printf("ip_output: Invalid policy found. %d\n", sp->policy); 50733841545SHajimu UMEMOTO } 50833841545SHajimu UMEMOTO { 50933841545SHajimu UMEMOTO struct ipsec_output_state state; 51033841545SHajimu UMEMOTO bzero(&state, sizeof(state)); 51133841545SHajimu UMEMOTO state.m = m; 51233841545SHajimu UMEMOTO if (flags & IP_ROUTETOIF) { 51333841545SHajimu UMEMOTO state.ro = &iproute; 51433841545SHajimu UMEMOTO bzero(&iproute, sizeof(iproute)); 51533841545SHajimu UMEMOTO } else 51633841545SHajimu UMEMOTO state.ro = ro; 51733841545SHajimu UMEMOTO state.dst = (struct sockaddr *)dst; 51833841545SHajimu UMEMOTO 51933841545SHajimu UMEMOTO ip->ip_sum = 0; 52033841545SHajimu UMEMOTO 52133841545SHajimu UMEMOTO /* 52233841545SHajimu UMEMOTO * XXX 52333841545SHajimu UMEMOTO * delayed checksums are not currently compatible with IPsec 52433841545SHajimu UMEMOTO */ 52533841545SHajimu UMEMOTO if (m->m_pkthdr.csum_flags & CSUM_DELAY_DATA) { 52633841545SHajimu UMEMOTO in_delayed_cksum(m); 52733841545SHajimu UMEMOTO m->m_pkthdr.csum_flags &= ~CSUM_DELAY_DATA; 52833841545SHajimu UMEMOTO } 52933841545SHajimu UMEMOTO 530fd8e4ebcSMike Barcroft ip->ip_len = htons(ip->ip_len); 531fd8e4ebcSMike Barcroft ip->ip_off = htons(ip->ip_off); 53233841545SHajimu UMEMOTO 53333841545SHajimu UMEMOTO error = ipsec4_output(&state, sp, flags); 53433841545SHajimu UMEMOTO 53533841545SHajimu UMEMOTO m = state.m; 53633841545SHajimu UMEMOTO if (flags & IP_ROUTETOIF) { 53733841545SHajimu UMEMOTO /* 53833841545SHajimu UMEMOTO * if we have tunnel mode SA, we may need to ignore 53933841545SHajimu UMEMOTO * IP_ROUTETOIF. 54033841545SHajimu UMEMOTO */ 54133841545SHajimu UMEMOTO if (state.ro != &iproute || state.ro->ro_rt != NULL) { 54233841545SHajimu UMEMOTO flags &= ~IP_ROUTETOIF; 54333841545SHajimu UMEMOTO ro = state.ro; 54433841545SHajimu UMEMOTO } 54533841545SHajimu UMEMOTO } else 54633841545SHajimu UMEMOTO ro = state.ro; 54733841545SHajimu UMEMOTO dst = (struct sockaddr_in *)state.dst; 54833841545SHajimu UMEMOTO if (error) { 54933841545SHajimu UMEMOTO /* mbuf is already reclaimed in ipsec4_output. */ 55033841545SHajimu UMEMOTO m0 = NULL; 55133841545SHajimu UMEMOTO switch (error) { 55233841545SHajimu UMEMOTO case EHOSTUNREACH: 55333841545SHajimu UMEMOTO case ENETUNREACH: 55433841545SHajimu UMEMOTO case EMSGSIZE: 55533841545SHajimu UMEMOTO case ENOBUFS: 55633841545SHajimu UMEMOTO case ENOMEM: 55733841545SHajimu UMEMOTO break; 55833841545SHajimu UMEMOTO default: 55933841545SHajimu UMEMOTO printf("ip4_output (ipsec): error code %d\n", error); 56033841545SHajimu UMEMOTO /*fall through*/ 56133841545SHajimu UMEMOTO case ENOENT: 56233841545SHajimu UMEMOTO /* don't show these error codes to the user */ 56333841545SHajimu UMEMOTO error = 0; 56433841545SHajimu UMEMOTO break; 56533841545SHajimu UMEMOTO } 56633841545SHajimu UMEMOTO goto bad; 56733841545SHajimu UMEMOTO } 56833841545SHajimu UMEMOTO } 56933841545SHajimu UMEMOTO 57033841545SHajimu UMEMOTO /* be sure to update variables that are affected by ipsec4_output() */ 57133841545SHajimu UMEMOTO ip = mtod(m, struct ip *); 57233841545SHajimu UMEMOTO hlen = ip->ip_hl << 2; 57333841545SHajimu UMEMOTO if (ro->ro_rt == NULL) { 57433841545SHajimu UMEMOTO if ((flags & IP_ROUTETOIF) == 0) { 57533841545SHajimu UMEMOTO printf("ip_output: " 57633841545SHajimu UMEMOTO "can't update route after IPsec processing\n"); 57733841545SHajimu UMEMOTO error = EHOSTUNREACH; /*XXX*/ 57833841545SHajimu UMEMOTO goto bad; 57933841545SHajimu UMEMOTO } 58033841545SHajimu UMEMOTO } else { 58133841545SHajimu UMEMOTO ia = ifatoia(ro->ro_rt->rt_ifa); 58233841545SHajimu UMEMOTO ifp = ro->ro_rt->rt_ifp; 58333841545SHajimu UMEMOTO } 58433841545SHajimu UMEMOTO 58533841545SHajimu UMEMOTO /* make it flipped, again. */ 586fd8e4ebcSMike Barcroft ip->ip_len = ntohs(ip->ip_len); 587fd8e4ebcSMike Barcroft ip->ip_off = ntohs(ip->ip_off); 58833841545SHajimu UMEMOTO skip_ipsec: 58933841545SHajimu UMEMOTO #endif /*IPSEC*/ 590b9234fafSSam Leffler #ifdef FAST_IPSEC 591b9234fafSSam Leffler /* 592b9234fafSSam Leffler * Check the security policy (SP) for the packet and, if 593b9234fafSSam Leffler * required, do IPsec-related processing. There are two 594b9234fafSSam Leffler * cases here; the first time a packet is sent through 595b9234fafSSam Leffler * it will be untagged and handled by ipsec4_checkpolicy. 596b9234fafSSam Leffler * If the packet is resubmitted to ip_output (e.g. after 597b9234fafSSam Leffler * AH, ESP, etc. processing), there will be a tag to bypass 598b9234fafSSam Leffler * the lookup and related policy checking. 599b9234fafSSam Leffler */ 600b9234fafSSam Leffler mtag = m_tag_find(m, PACKET_TAG_IPSEC_PENDING_TDB, NULL); 601b9234fafSSam Leffler s = splnet(); 602b9234fafSSam Leffler if (mtag != NULL) { 603b9234fafSSam Leffler tdbi = (struct tdb_ident *)(mtag + 1); 604b9234fafSSam Leffler sp = ipsec_getpolicy(tdbi, IPSEC_DIR_OUTBOUND); 605b9234fafSSam Leffler if (sp == NULL) 606b9234fafSSam Leffler error = -EINVAL; /* force silent drop */ 607b9234fafSSam Leffler m_tag_delete(m, mtag); 608b9234fafSSam Leffler } else { 609b9234fafSSam Leffler sp = ipsec4_checkpolicy(m, IPSEC_DIR_OUTBOUND, flags, 610b9234fafSSam Leffler &error, inp); 611b9234fafSSam Leffler } 612b9234fafSSam Leffler /* 613b9234fafSSam Leffler * There are four return cases: 614b9234fafSSam Leffler * sp != NULL apply IPsec policy 615b9234fafSSam Leffler * sp == NULL, error == 0 no IPsec handling needed 616b9234fafSSam Leffler * sp == NULL, error == -EINVAL discard packet w/o error 617b9234fafSSam Leffler * sp == NULL, error != 0 discard packet, report error 618b9234fafSSam Leffler */ 619b9234fafSSam Leffler if (sp != NULL) { 620b9234fafSSam Leffler /* Loop detection, check if ipsec processing already done */ 621b9234fafSSam Leffler KASSERT(sp->req != NULL, ("ip_output: no ipsec request")); 622b9234fafSSam Leffler for (mtag = m_tag_first(m); mtag != NULL; 623b9234fafSSam Leffler mtag = m_tag_next(m, mtag)) { 624b9234fafSSam Leffler if (mtag->m_tag_cookie != MTAG_ABI_COMPAT) 625b9234fafSSam Leffler continue; 626b9234fafSSam Leffler if (mtag->m_tag_id != PACKET_TAG_IPSEC_OUT_DONE && 627b9234fafSSam Leffler mtag->m_tag_id != PACKET_TAG_IPSEC_OUT_CRYPTO_NEEDED) 628b9234fafSSam Leffler continue; 629b9234fafSSam Leffler /* 630b9234fafSSam Leffler * Check if policy has an SA associated with it. 631b9234fafSSam Leffler * This can happen when an SP has yet to acquire 632b9234fafSSam Leffler * an SA; e.g. on first reference. If it occurs, 633b9234fafSSam Leffler * then we let ipsec4_process_packet do its thing. 634b9234fafSSam Leffler */ 635b9234fafSSam Leffler if (sp->req->sav == NULL) 636b9234fafSSam Leffler break; 637b9234fafSSam Leffler tdbi = (struct tdb_ident *)(mtag + 1); 638b9234fafSSam Leffler if (tdbi->spi == sp->req->sav->spi && 639b9234fafSSam Leffler tdbi->proto == sp->req->sav->sah->saidx.proto && 640b9234fafSSam Leffler bcmp(&tdbi->dst, &sp->spidx.dst, 641b9234fafSSam Leffler sizeof (union sockaddr_union)) == 0) { 642b9234fafSSam Leffler /* 643b9234fafSSam Leffler * No IPsec processing is needed, free 644b9234fafSSam Leffler * reference to SP. 645b9234fafSSam Leffler * 646b9234fafSSam Leffler * NB: null pointer to avoid free at 647b9234fafSSam Leffler * done: below. 648b9234fafSSam Leffler */ 649b9234fafSSam Leffler KEY_FREESP(&sp), sp = NULL; 650b9234fafSSam Leffler splx(s); 651b9234fafSSam Leffler goto spd_done; 652b9234fafSSam Leffler } 653b9234fafSSam Leffler } 654b9234fafSSam Leffler 655b9234fafSSam Leffler /* 656b9234fafSSam Leffler * Do delayed checksums now because we send before 657b9234fafSSam Leffler * this is done in the normal processing path. 658b9234fafSSam Leffler */ 659b9234fafSSam Leffler if (m->m_pkthdr.csum_flags & CSUM_DELAY_DATA) { 660b9234fafSSam Leffler in_delayed_cksum(m); 661b9234fafSSam Leffler m->m_pkthdr.csum_flags &= ~CSUM_DELAY_DATA; 662b9234fafSSam Leffler } 663b9234fafSSam Leffler 664b9234fafSSam Leffler ip->ip_len = htons(ip->ip_len); 665b9234fafSSam Leffler ip->ip_off = htons(ip->ip_off); 666b9234fafSSam Leffler 667b9234fafSSam Leffler /* NB: callee frees mbuf */ 668b9234fafSSam Leffler error = ipsec4_process_packet(m, sp->req, flags, 0); 669b9234fafSSam Leffler splx(s); 670b9234fafSSam Leffler goto done; 671b9234fafSSam Leffler } else { 672b9234fafSSam Leffler splx(s); 673b9234fafSSam Leffler 674b9234fafSSam Leffler if (error != 0) { 675b9234fafSSam Leffler /* 676b9234fafSSam Leffler * Hack: -EINVAL is used to signal that a packet 677b9234fafSSam Leffler * should be silently discarded. This is typically 678b9234fafSSam Leffler * because we asked key management for an SA and 679b9234fafSSam Leffler * it was delayed (e.g. kicked up to IKE). 680b9234fafSSam Leffler */ 681b9234fafSSam Leffler if (error == -EINVAL) 682b9234fafSSam Leffler error = 0; 683b9234fafSSam Leffler goto bad; 684b9234fafSSam Leffler } else { 685b9234fafSSam Leffler /* No IPsec processing for this packet. */ 686b9234fafSSam Leffler } 687b9234fafSSam Leffler #ifdef notyet 688b9234fafSSam Leffler /* 689b9234fafSSam Leffler * If deferred crypto processing is needed, check that 690b9234fafSSam Leffler * the interface supports it. 691b9234fafSSam Leffler */ 692b9234fafSSam Leffler mtag = m_tag_find(m, PACKET_TAG_IPSEC_OUT_CRYPTO_NEEDED, NULL); 693b9234fafSSam Leffler if (mtag != NULL && (ifp->if_capenable & IFCAP_IPSEC) == 0) { 694b9234fafSSam Leffler /* notify IPsec to do its own crypto */ 695b9234fafSSam Leffler ipsp_skipcrypto_unmark((struct tdb_ident *)(mtag + 1)); 696b9234fafSSam Leffler error = EHOSTUNREACH; 697b9234fafSSam Leffler goto bad; 698b9234fafSSam Leffler } 699b9234fafSSam Leffler #endif 700b9234fafSSam Leffler } 701b9234fafSSam Leffler spd_done: 702b9234fafSSam Leffler #endif /* FAST_IPSEC */ 70333841545SHajimu UMEMOTO 704fed1c7e9SSøren Schmidt /* 705fed1c7e9SSøren Schmidt * IpHack's section. 706fed1c7e9SSøren Schmidt * - Xlate: translate packet's addr/port (NAT). 707e4676ba6SJulian Elischer * - Firewall: deny/allow/etc. 708fed1c7e9SSøren Schmidt * - Wrap: fake packet's addr/port <unimpl.> 709fed1c7e9SSøren Schmidt * - Encapsulate: put it in another IP and send out. <unimp.> 710fed1c7e9SSøren Schmidt */ 711c4ac87eaSDarren Reed #ifdef PFIL_HOOKS 712c4ac87eaSDarren Reed /* 713c4ac87eaSDarren Reed * Run through list of hooks for output packets. 714c4ac87eaSDarren Reed */ 715c4ac87eaSDarren Reed m1 = m; 716c4ac87eaSDarren Reed pfh = pfil_hook_get(PFIL_OUT, &inetsw[ip_protox[IPPROTO_IP]].pr_pfh); 717fc2ffbe6SPoul-Henning Kamp for (; pfh; pfh = TAILQ_NEXT(pfh, pfil_link)) 718c4ac87eaSDarren Reed if (pfh->pfil_func) { 719c4ac87eaSDarren Reed rv = pfh->pfil_func(ip, hlen, ifp, 1, &m1); 720c4ac87eaSDarren Reed if (rv) { 721c4ac87eaSDarren Reed error = EHOSTUNREACH; 7221ee25934SPeter Wemm goto done; 7231ee25934SPeter Wemm } 724c4ac87eaSDarren Reed m = m1; 725c4ac87eaSDarren Reed if (m == NULL) 726c4ac87eaSDarren Reed goto done; 727c4ac87eaSDarren Reed ip = mtod(m, struct ip *); 728c4ac87eaSDarren Reed } 729c4ac87eaSDarren Reed #endif /* PFIL_HOOKS */ 730fed1c7e9SSøren Schmidt 731df8bae1dSRodney W. Grimes /* 732e7319babSPoul-Henning Kamp * Check with the firewall... 7333efc3014SJulian Elischer * but not if we are already being fwd'd from a firewall. 734e7319babSPoul-Henning Kamp */ 7352b25acc1SLuigi Rizzo if (fw_enable && IPFW_LOADED && !args.next_hop) { 7369de9737fSPeter Wemm struct sockaddr_in *old = dst; 737b715f178SLuigi Rizzo 7382b25acc1SLuigi Rizzo args.m = m; 7392b25acc1SLuigi Rizzo args.next_hop = dst; 7402b25acc1SLuigi Rizzo args.oif = ifp; 7412b25acc1SLuigi Rizzo off = ip_fw_chk_ptr(&args); 7422b25acc1SLuigi Rizzo m = args.m; 7432b25acc1SLuigi Rizzo dst = args.next_hop; 7442b25acc1SLuigi Rizzo 745b715f178SLuigi Rizzo /* 746b715f178SLuigi Rizzo * On return we must do the following: 747507b4b54SLuigi Rizzo * m == NULL -> drop the pkt (old interface, deprecated) 748aa1f5daaSJonathan Lemon * (off & IP_FW_PORT_DENY_FLAG) -> drop the pkt (new interface) 749b715f178SLuigi Rizzo * 1<=off<= 0xffff -> DIVERT 750aa1f5daaSJonathan Lemon * (off & IP_FW_PORT_DYNT_FLAG) -> send to a DUMMYNET pipe 751aa1f5daaSJonathan Lemon * (off & IP_FW_PORT_TEE_FLAG) -> TEE the packet 752b715f178SLuigi Rizzo * dst != old -> IPFIREWALL_FORWARD 753b715f178SLuigi Rizzo * off==0, dst==old -> accept 754aa1f5daaSJonathan Lemon * If some of the above modules are not compiled in, then 755b715f178SLuigi Rizzo * we should't have to check the corresponding condition 756b715f178SLuigi Rizzo * (because the ipfw control socket should not accept 757b715f178SLuigi Rizzo * unsupported rules), but better play safe and drop 758b715f178SLuigi Rizzo * packets in case of doubt. 759b715f178SLuigi Rizzo */ 760d60315beSLuigi Rizzo if ( (off & IP_FW_PORT_DENY_FLAG) || m == NULL) { 761507b4b54SLuigi Rizzo if (m) 762507b4b54SLuigi Rizzo m_freem(m); 763507b4b54SLuigi Rizzo error = EACCES; 764507b4b54SLuigi Rizzo goto done; 765507b4b54SLuigi Rizzo } 766d60315beSLuigi Rizzo ip = mtod(m, struct ip *); 767b715f178SLuigi Rizzo if (off == 0 && dst == old) /* common case */ 768b715f178SLuigi Rizzo goto pass; 7697b109fa4SLuigi Rizzo if (DUMMYNET_LOADED && (off & IP_FW_PORT_DYNT_FLAG) != 0) { 770b715f178SLuigi Rizzo /* 771b715f178SLuigi Rizzo * pass the pkt to dummynet. Need to include 772f0a53591SLuigi Rizzo * pipe number, m, ifp, ro, dst because these are 773b715f178SLuigi Rizzo * not recomputed in the next pass. 774b715f178SLuigi Rizzo * All other parameters have been already used and 775b715f178SLuigi Rizzo * so they are not needed anymore. 776b715f178SLuigi Rizzo * XXX note: if the ifp or ro entry are deleted 777b715f178SLuigi Rizzo * while a pkt is in dummynet, we are in trouble! 778b715f178SLuigi Rizzo */ 7792b25acc1SLuigi Rizzo args.ro = ro; 7802b25acc1SLuigi Rizzo args.dst = dst; 7812b25acc1SLuigi Rizzo args.flags = flags; 7822b25acc1SLuigi Rizzo 7832b25acc1SLuigi Rizzo error = ip_dn_io_ptr(m, off & 0xffff, DN_TO_IP_OUT, 7842b25acc1SLuigi Rizzo &args); 785b715f178SLuigi Rizzo goto done; 786b715f178SLuigi Rizzo } 787b715f178SLuigi Rizzo #ifdef IPDIVERT 7888948e4baSArchie Cobbs if (off != 0 && (off & IP_FW_PORT_DYNT_FLAG) == 0) { 7898948e4baSArchie Cobbs struct mbuf *clone = NULL; 7908948e4baSArchie Cobbs 7918948e4baSArchie Cobbs /* Clone packet if we're doing a 'tee' */ 7928948e4baSArchie Cobbs if ((off & IP_FW_PORT_TEE_FLAG) != 0) 7938948e4baSArchie Cobbs clone = m_dup(m, M_DONTWAIT); 7948948e4baSArchie Cobbs 795ea53ecd9SJonathan Lemon /* 796ea53ecd9SJonathan Lemon * XXX 797ea53ecd9SJonathan Lemon * delayed checksums are not currently compatible 798ea53ecd9SJonathan Lemon * with divert sockets. 799ea53ecd9SJonathan Lemon */ 800ea53ecd9SJonathan Lemon if (m->m_pkthdr.csum_flags & CSUM_DELAY_DATA) { 801ea53ecd9SJonathan Lemon in_delayed_cksum(m); 802ea53ecd9SJonathan Lemon m->m_pkthdr.csum_flags &= ~CSUM_DELAY_DATA; 803ea53ecd9SJonathan Lemon } 804ea53ecd9SJonathan Lemon 8058948e4baSArchie Cobbs /* Restore packet header fields to original values */ 806fd8e4ebcSMike Barcroft ip->ip_len = htons(ip->ip_len); 807fd8e4ebcSMike Barcroft ip->ip_off = htons(ip->ip_off); 8088948e4baSArchie Cobbs 8098948e4baSArchie Cobbs /* Deliver packet to divert input routine */ 8102b25acc1SLuigi Rizzo divert_packet(m, 0, off & 0xffff, args.divert_rule); 8118948e4baSArchie Cobbs 8128948e4baSArchie Cobbs /* If 'tee', continue with original packet */ 8138948e4baSArchie Cobbs if (clone != NULL) { 8148948e4baSArchie Cobbs m = clone; 8158948e4baSArchie Cobbs ip = mtod(m, struct ip *); 8168948e4baSArchie Cobbs goto pass; 8178948e4baSArchie Cobbs } 818b715f178SLuigi Rizzo goto done; 819b715f178SLuigi Rizzo } 820b715f178SLuigi Rizzo #endif 821b715f178SLuigi Rizzo 8222b25acc1SLuigi Rizzo /* IPFIREWALL_FORWARD */ 8232b25acc1SLuigi Rizzo /* 8242b25acc1SLuigi Rizzo * Check dst to make sure it is directly reachable on the 825f9e354dfSJulian Elischer * interface we previously thought it was. 826f9e354dfSJulian Elischer * If it isn't (which may be likely in some situations) we have 827f9e354dfSJulian Elischer * to re-route it (ie, find a route for the next-hop and the 828f9e354dfSJulian Elischer * associated interface) and set them here. This is nested 829f9e354dfSJulian Elischer * forwarding which in most cases is undesirable, except where 830f9e354dfSJulian Elischer * such control is nigh impossible. So we do it here. 831f9e354dfSJulian Elischer * And I'm babbling. 832f9e354dfSJulian Elischer */ 8332b25acc1SLuigi Rizzo if (off == 0 && old != dst) { /* FORWARD, dst has changed */ 8342b25acc1SLuigi Rizzo #if 0 8352b25acc1SLuigi Rizzo /* 8362b25acc1SLuigi Rizzo * XXX To improve readability, this block should be 8372b25acc1SLuigi Rizzo * changed into a function call as below: 8382b25acc1SLuigi Rizzo */ 8392b25acc1SLuigi Rizzo error = ip_ipforward(&m, &dst, &ifp); 8402b25acc1SLuigi Rizzo if (error) 8412b25acc1SLuigi Rizzo goto bad; 8422b25acc1SLuigi Rizzo if (m == NULL) /* ip_input consumed the mbuf */ 8432b25acc1SLuigi Rizzo goto done; 8442b25acc1SLuigi Rizzo #else 845f9e354dfSJulian Elischer struct in_ifaddr *ia; 846f9e354dfSJulian Elischer 8472b25acc1SLuigi Rizzo /* 8482b25acc1SLuigi Rizzo * XXX sro_fwd below is static, and a pointer 8492b25acc1SLuigi Rizzo * to it gets passed to routines downstream. 8502b25acc1SLuigi Rizzo * This could have surprisingly bad results in 8512b25acc1SLuigi Rizzo * practice, because its content is overwritten 8522b25acc1SLuigi Rizzo * by subsequent packets. 8532b25acc1SLuigi Rizzo */ 854f9e354dfSJulian Elischer /* There must be a better way to do this next line... */ 8552b25acc1SLuigi Rizzo static struct route sro_fwd; 8562b25acc1SLuigi Rizzo struct route *ro_fwd = &sro_fwd; 8572b25acc1SLuigi Rizzo 8582b25acc1SLuigi Rizzo #if 0 8592b25acc1SLuigi Rizzo print_ip("IPFIREWALL_FORWARD: New dst ip: ", 8602b25acc1SLuigi Rizzo dst->sin_addr, "\n"); 861f9e354dfSJulian Elischer #endif 8622b25acc1SLuigi Rizzo 863f9e354dfSJulian Elischer /* 864f9e354dfSJulian Elischer * We need to figure out if we have been forwarded 8652b25acc1SLuigi Rizzo * to a local socket. If so, then we should somehow 866f9e354dfSJulian Elischer * "loop back" to ip_input, and get directed to the 867f9e354dfSJulian Elischer * PCB as if we had received this packet. This is 868f9e354dfSJulian Elischer * because it may be dificult to identify the packets 869f9e354dfSJulian Elischer * you want to forward until they are being output 870f9e354dfSJulian Elischer * and have selected an interface. (e.g. locally 871f9e354dfSJulian Elischer * initiated packets) If we used the loopback inteface, 872f9e354dfSJulian Elischer * we would not be able to control what happens 873f9e354dfSJulian Elischer * as the packet runs through ip_input() as 874f9e354dfSJulian Elischer * it is done through a ISR. 875f9e354dfSJulian Elischer */ 876ca925d9cSJonathan Lemon LIST_FOREACH(ia, 877ca925d9cSJonathan Lemon INADDR_HASH(dst->sin_addr.s_addr), ia_hash) { 878f9e354dfSJulian Elischer /* 879f9e354dfSJulian Elischer * If the addr to forward to is one 880f9e354dfSJulian Elischer * of ours, we pretend to 881f9e354dfSJulian Elischer * be the destination for this packet. 882f9e354dfSJulian Elischer */ 883f9e354dfSJulian Elischer if (IA_SIN(ia)->sin_addr.s_addr == 884f9e354dfSJulian Elischer dst->sin_addr.s_addr) 885f9e354dfSJulian Elischer break; 886f9e354dfSJulian Elischer } 8872b25acc1SLuigi Rizzo if (ia) { /* tell ip_input "dont filter" */ 8882b25acc1SLuigi Rizzo struct m_hdr tag; 8892b25acc1SLuigi Rizzo 8902b25acc1SLuigi Rizzo tag.mh_type = MT_TAG; 8912b25acc1SLuigi Rizzo tag.mh_flags = PACKET_TAG_IPFORWARD; 8922b25acc1SLuigi Rizzo tag.mh_data = (caddr_t)args.next_hop; 8932b25acc1SLuigi Rizzo tag.mh_next = m; 8942b25acc1SLuigi Rizzo 895f9e354dfSJulian Elischer if (m->m_pkthdr.rcvif == NULL) 896f9e354dfSJulian Elischer m->m_pkthdr.rcvif = ifunit("lo0"); 89720c822f3SJonathan Lemon if (m->m_pkthdr.csum_flags & CSUM_DELAY_DATA) { 89820c822f3SJonathan Lemon m->m_pkthdr.csum_flags |= 89920c822f3SJonathan Lemon CSUM_DATA_VALID | CSUM_PSEUDO_HDR; 90020c822f3SJonathan Lemon m0->m_pkthdr.csum_data = 0xffff; 90120c822f3SJonathan Lemon } 90220c822f3SJonathan Lemon m->m_pkthdr.csum_flags |= 90320c822f3SJonathan Lemon CSUM_IP_CHECKED | CSUM_IP_VALID; 904fd8e4ebcSMike Barcroft ip->ip_len = htons(ip->ip_len); 905fd8e4ebcSMike Barcroft ip->ip_off = htons(ip->ip_off); 9062b25acc1SLuigi Rizzo ip_input((struct mbuf *)&tag); 907f9e354dfSJulian Elischer goto done; 908f9e354dfSJulian Elischer } 909f9e354dfSJulian Elischer /* Some of the logic for this was 910f9e354dfSJulian Elischer * nicked from above. 911f9e354dfSJulian Elischer * 912f9e354dfSJulian Elischer * This rewrites the cached route in a local PCB. 913f9e354dfSJulian Elischer * Is this what we want to do? 914f9e354dfSJulian Elischer */ 915f9e354dfSJulian Elischer bcopy(dst, &ro_fwd->ro_dst, sizeof(*dst)); 916f9e354dfSJulian Elischer 917f9e354dfSJulian Elischer ro_fwd->ro_rt = 0; 918f9e354dfSJulian Elischer rtalloc_ign(ro_fwd, RTF_PRCLONING); 919f9e354dfSJulian Elischer 920f9e354dfSJulian Elischer if (ro_fwd->ro_rt == 0) { 921f9e354dfSJulian Elischer ipstat.ips_noroute++; 922f9e354dfSJulian Elischer error = EHOSTUNREACH; 923f9e354dfSJulian Elischer goto bad; 924f9e354dfSJulian Elischer } 925f9e354dfSJulian Elischer 926f9e354dfSJulian Elischer ia = ifatoia(ro_fwd->ro_rt->rt_ifa); 927f9e354dfSJulian Elischer ifp = ro_fwd->ro_rt->rt_ifp; 928f9e354dfSJulian Elischer ro_fwd->ro_rt->rt_use++; 929f9e354dfSJulian Elischer if (ro_fwd->ro_rt->rt_flags & RTF_GATEWAY) 9302b25acc1SLuigi Rizzo dst = (struct sockaddr_in *) 9312b25acc1SLuigi Rizzo ro_fwd->ro_rt->rt_gateway; 932f9e354dfSJulian Elischer if (ro_fwd->ro_rt->rt_flags & RTF_HOST) 933f9e354dfSJulian Elischer isbroadcast = 934f9e354dfSJulian Elischer (ro_fwd->ro_rt->rt_flags & RTF_BROADCAST); 935f9e354dfSJulian Elischer else 936f9e354dfSJulian Elischer isbroadcast = in_broadcast(dst->sin_addr, ifp); 9373f9e3122SYaroslav Tykhiy if (ro->ro_rt) 938f9e354dfSJulian Elischer RTFREE(ro->ro_rt); 939f9e354dfSJulian Elischer ro->ro_rt = ro_fwd->ro_rt; 940f9e354dfSJulian Elischer dst = (struct sockaddr_in *)&ro_fwd->ro_dst; 941f9e354dfSJulian Elischer 9422b25acc1SLuigi Rizzo #endif /* ... block to be put into a function */ 943f9e354dfSJulian Elischer /* 944f9e354dfSJulian Elischer * If we added a default src ip earlier, 945f9e354dfSJulian Elischer * which would have been gotten from the-then 946f9e354dfSJulian Elischer * interface, do it again, from the new one. 947f9e354dfSJulian Elischer */ 9482b25acc1SLuigi Rizzo if (src_was_INADDR_ANY) 949f9e354dfSJulian Elischer ip->ip_src = IA_SIN(ia)->sin_addr; 950b715f178SLuigi Rizzo goto pass ; 951f9e354dfSJulian Elischer } 9522b25acc1SLuigi Rizzo 953b715f178SLuigi Rizzo /* 954b715f178SLuigi Rizzo * if we get here, none of the above matches, and 955b715f178SLuigi Rizzo * we have to drop the pkt 956b715f178SLuigi Rizzo */ 957b715f178SLuigi Rizzo m_freem(m); 958b715f178SLuigi Rizzo error = EACCES; /* not sure this is the right error msg */ 959b715f178SLuigi Rizzo goto done; 96093e0e116SJulian Elischer } 961e7319babSPoul-Henning Kamp 962b715f178SLuigi Rizzo pass: 96351c8ec4aSRuslan Ermilov /* 127/8 must not appear on wire - RFC1122. */ 96451c8ec4aSRuslan Ermilov if ((ntohl(ip->ip_dst.s_addr) >> IN_CLASSA_NSHIFT) == IN_LOOPBACKNET || 96551c8ec4aSRuslan Ermilov (ntohl(ip->ip_src.s_addr) >> IN_CLASSA_NSHIFT) == IN_LOOPBACKNET) { 96651c8ec4aSRuslan Ermilov if ((ifp->if_flags & IFF_LOOPBACK) == 0) { 96751c8ec4aSRuslan Ermilov ipstat.ips_badaddr++; 96851c8ec4aSRuslan Ermilov error = EADDRNOTAVAIL; 96951c8ec4aSRuslan Ermilov goto bad; 97051c8ec4aSRuslan Ermilov } 97151c8ec4aSRuslan Ermilov } 97251c8ec4aSRuslan Ermilov 973206a3274SRuslan Ermilov m->m_pkthdr.csum_flags |= CSUM_IP; 974206a3274SRuslan Ermilov sw_csum = m->m_pkthdr.csum_flags & ~ifp->if_hwassist; 975db4f9cc7SJonathan Lemon if (sw_csum & CSUM_DELAY_DATA) { 976db4f9cc7SJonathan Lemon in_delayed_cksum(m); 977db4f9cc7SJonathan Lemon sw_csum &= ~CSUM_DELAY_DATA; 978db4f9cc7SJonathan Lemon } 979206a3274SRuslan Ermilov m->m_pkthdr.csum_flags &= ifp->if_hwassist; 980db4f9cc7SJonathan Lemon 981e7319babSPoul-Henning Kamp /* 982db4f9cc7SJonathan Lemon * If small enough for interface, or the interface will take 983db4f9cc7SJonathan Lemon * care of the fragmentation for us, can just send directly. 984df8bae1dSRodney W. Grimes */ 985db4f9cc7SJonathan Lemon if ((u_short)ip->ip_len <= ifp->if_mtu || 986db4f9cc7SJonathan Lemon ifp->if_hwassist & CSUM_FRAGMENT) { 987fd8e4ebcSMike Barcroft ip->ip_len = htons(ip->ip_len); 988fd8e4ebcSMike Barcroft ip->ip_off = htons(ip->ip_off); 989df8bae1dSRodney W. Grimes ip->ip_sum = 0; 99053be11f6SPoul-Henning Kamp if (sw_csum & CSUM_DELAY_IP) 991df8bae1dSRodney W. Grimes ip->ip_sum = in_cksum(m, hlen); 9925da9f8faSJosef Karthauser 9935da9f8faSJosef Karthauser /* Record statistics for this interface address. */ 99438c1bc35SRuslan Ermilov if (!(flags & IP_FORWARDING) && ia) { 9955da9f8faSJosef Karthauser ia->ia_ifa.if_opackets++; 9965da9f8faSJosef Karthauser ia->ia_ifa.if_obytes += m->m_pkthdr.len; 9975da9f8faSJosef Karthauser } 9985da9f8faSJosef Karthauser 99933841545SHajimu UMEMOTO #ifdef IPSEC 100033841545SHajimu UMEMOTO /* clean ipsec history once it goes out of the node */ 100133841545SHajimu UMEMOTO ipsec_delaux(m); 100233841545SHajimu UMEMOTO #endif 100333841545SHajimu UMEMOTO 1004df8bae1dSRodney W. Grimes error = (*ifp->if_output)(ifp, m, 1005df8bae1dSRodney W. Grimes (struct sockaddr *)dst, ro->ro_rt); 1006df8bae1dSRodney W. Grimes goto done; 1007df8bae1dSRodney W. Grimes } 1008df8bae1dSRodney W. Grimes /* 1009df8bae1dSRodney W. Grimes * Too large for interface; fragment if possible. 1010df8bae1dSRodney W. Grimes * Must be able to put at least 8 bytes per fragment. 1011df8bae1dSRodney W. Grimes */ 1012df8bae1dSRodney W. Grimes if (ip->ip_off & IP_DF) { 1013df8bae1dSRodney W. Grimes error = EMSGSIZE; 10143d1f141bSGarrett Wollman /* 10153d1f141bSGarrett Wollman * This case can happen if the user changed the MTU 10163d1f141bSGarrett Wollman * of an interface after enabling IP on it. Because 10173d1f141bSGarrett Wollman * most netifs don't keep track of routes pointing to 10183d1f141bSGarrett Wollman * them, there is no way for one to update all its 10193d1f141bSGarrett Wollman * routes when the MTU is changed. 10203d1f141bSGarrett Wollman */ 10213d1f141bSGarrett Wollman if ((ro->ro_rt->rt_flags & (RTF_UP | RTF_HOST)) 10223d1f141bSGarrett Wollman && !(ro->ro_rt->rt_rmx.rmx_locks & RTV_MTU) 10233d1f141bSGarrett Wollman && (ro->ro_rt->rt_rmx.rmx_mtu > ifp->if_mtu)) { 10243d1f141bSGarrett Wollman ro->ro_rt->rt_rmx.rmx_mtu = ifp->if_mtu; 10253d1f141bSGarrett Wollman } 1026df8bae1dSRodney W. Grimes ipstat.ips_cantfrag++; 1027df8bae1dSRodney W. Grimes goto bad; 1028df8bae1dSRodney W. Grimes } 1029df8bae1dSRodney W. Grimes len = (ifp->if_mtu - hlen) &~ 7; 1030df8bae1dSRodney W. Grimes if (len < 8) { 1031df8bae1dSRodney W. Grimes error = EMSGSIZE; 1032df8bae1dSRodney W. Grimes goto bad; 1033df8bae1dSRodney W. Grimes } 1034df8bae1dSRodney W. Grimes 1035db4f9cc7SJonathan Lemon /* 1036db4f9cc7SJonathan Lemon * if the interface will not calculate checksums on 1037db4f9cc7SJonathan Lemon * fragmented packets, then do it here. 1038db4f9cc7SJonathan Lemon */ 1039db4f9cc7SJonathan Lemon if (m->m_pkthdr.csum_flags & CSUM_DELAY_DATA && 1040db4f9cc7SJonathan Lemon (ifp->if_hwassist & CSUM_IP_FRAGS) == 0) { 1041db4f9cc7SJonathan Lemon in_delayed_cksum(m); 1042db4f9cc7SJonathan Lemon m->m_pkthdr.csum_flags &= ~CSUM_DELAY_DATA; 1043db4f9cc7SJonathan Lemon } 1044db4f9cc7SJonathan Lemon 104598cb733cSKenneth D. Merry if (len > PAGE_SIZE) { 104698cb733cSKenneth D. Merry /* 104798cb733cSKenneth D. Merry * Fragement large datagrams such that each segment 104898cb733cSKenneth D. Merry * contains a multiple of PAGE_SIZE amount of data, 104998cb733cSKenneth D. Merry * plus headers. This enables a receiver to perform 105098cb733cSKenneth D. Merry * page-flipping zero-copy optimizations. 105198cb733cSKenneth D. Merry */ 105298cb733cSKenneth D. Merry 105398cb733cSKenneth D. Merry int newlen; 105498cb733cSKenneth D. Merry struct mbuf *mtmp; 105598cb733cSKenneth D. Merry 105698cb733cSKenneth D. Merry for (mtmp = m, off = 0; 105798cb733cSKenneth D. Merry mtmp && ((off + mtmp->m_len) <= ifp->if_mtu); 105898cb733cSKenneth D. Merry mtmp = mtmp->m_next) { 105998cb733cSKenneth D. Merry off += mtmp->m_len; 106098cb733cSKenneth D. Merry } 106198cb733cSKenneth D. Merry /* 106298cb733cSKenneth D. Merry * firstlen (off - hlen) must be aligned on an 106398cb733cSKenneth D. Merry * 8-byte boundary 106498cb733cSKenneth D. Merry */ 106598cb733cSKenneth D. Merry if (off < hlen) 106698cb733cSKenneth D. Merry goto smart_frag_failure; 106798cb733cSKenneth D. Merry off = ((off - hlen) & ~7) + hlen; 106898cb733cSKenneth D. Merry newlen = (~PAGE_MASK) & ifp->if_mtu; 106998cb733cSKenneth D. Merry if ((newlen + sizeof (struct ip)) > ifp->if_mtu) { 107098cb733cSKenneth D. Merry /* we failed, go back the default */ 107198cb733cSKenneth D. Merry smart_frag_failure: 107298cb733cSKenneth D. Merry newlen = len; 107398cb733cSKenneth D. Merry off = hlen + len; 107498cb733cSKenneth D. Merry } 107598cb733cSKenneth D. Merry 107698cb733cSKenneth D. Merry /* printf("ipfrag: len = %d, hlen = %d, mhlen = %d, newlen = %d, off = %d\n", 107798cb733cSKenneth D. Merry len, hlen, sizeof (struct ip), newlen, off);*/ 107898cb733cSKenneth D. Merry 107998cb733cSKenneth D. Merry len = newlen; 108098cb733cSKenneth D. Merry 108198cb733cSKenneth D. Merry } else { 108298cb733cSKenneth D. Merry off = hlen + len; 108398cb733cSKenneth D. Merry } 108498cb733cSKenneth D. Merry 108598cb733cSKenneth D. Merry 108698cb733cSKenneth D. Merry 1087df8bae1dSRodney W. Grimes { 108898cb733cSKenneth D. Merry int mhlen, firstlen = off - hlen; 1089df8bae1dSRodney W. Grimes struct mbuf **mnext = &m->m_nextpkt; 1090db4f9cc7SJonathan Lemon int nfrags = 1; 1091df8bae1dSRodney W. Grimes 1092df8bae1dSRodney W. Grimes /* 1093df8bae1dSRodney W. Grimes * Loop through length of segment after first fragment, 1094df8bae1dSRodney W. Grimes * make new header and copy data of each part and link onto chain. 1095df8bae1dSRodney W. Grimes */ 1096df8bae1dSRodney W. Grimes m0 = m; 1097df8bae1dSRodney W. Grimes mhlen = sizeof (struct ip); 109898cb733cSKenneth D. Merry for (; off < (u_short)ip->ip_len; off += len) { 1099df8bae1dSRodney W. Grimes MGETHDR(m, M_DONTWAIT, MT_HEADER); 1100df8bae1dSRodney W. Grimes if (m == 0) { 1101df8bae1dSRodney W. Grimes error = ENOBUFS; 1102df8bae1dSRodney W. Grimes ipstat.ips_odropped++; 1103df8bae1dSRodney W. Grimes goto sendorfree; 1104df8bae1dSRodney W. Grimes } 1105db4f9cc7SJonathan Lemon m->m_flags |= (m0->m_flags & M_MCAST) | M_FRAG; 1106df8bae1dSRodney W. Grimes m->m_data += max_linkhdr; 1107df8bae1dSRodney W. Grimes mhip = mtod(m, struct ip *); 1108df8bae1dSRodney W. Grimes *mhip = *ip; 1109df8bae1dSRodney W. Grimes if (hlen > sizeof (struct ip)) { 1110df8bae1dSRodney W. Grimes mhlen = ip_optcopy(ip, mhip) + sizeof (struct ip); 111153be11f6SPoul-Henning Kamp mhip->ip_v = IPVERSION; 111253be11f6SPoul-Henning Kamp mhip->ip_hl = mhlen >> 2; 1113df8bae1dSRodney W. Grimes } 1114df8bae1dSRodney W. Grimes m->m_len = mhlen; 1115cc22c7a7SRuslan Ermilov mhip->ip_off = ((off - hlen) >> 3) + ip->ip_off; 1116df8bae1dSRodney W. Grimes if (off + len >= (u_short)ip->ip_len) 1117df8bae1dSRodney W. Grimes len = (u_short)ip->ip_len - off; 1118df8bae1dSRodney W. Grimes else 1119df8bae1dSRodney W. Grimes mhip->ip_off |= IP_MF; 1120df8bae1dSRodney W. Grimes mhip->ip_len = htons((u_short)(len + mhlen)); 1121df8bae1dSRodney W. Grimes m->m_next = m_copy(m0, off, len); 1122df8bae1dSRodney W. Grimes if (m->m_next == 0) { 1123df8bae1dSRodney W. Grimes (void) m_free(m); 1124df8bae1dSRodney W. Grimes error = ENOBUFS; /* ??? */ 1125df8bae1dSRodney W. Grimes ipstat.ips_odropped++; 1126df8bae1dSRodney W. Grimes goto sendorfree; 1127df8bae1dSRodney W. Grimes } 1128df8bae1dSRodney W. Grimes m->m_pkthdr.len = mhlen + len; 1129df8bae1dSRodney W. Grimes m->m_pkthdr.rcvif = (struct ifnet *)0; 11304ed84624SRobert Watson #ifdef MAC 11314ed84624SRobert Watson mac_create_fragment(m0, m); 11324ed84624SRobert Watson #endif 1133db4f9cc7SJonathan Lemon m->m_pkthdr.csum_flags = m0->m_pkthdr.csum_flags; 1134fd8e4ebcSMike Barcroft mhip->ip_off = htons(mhip->ip_off); 1135df8bae1dSRodney W. Grimes mhip->ip_sum = 0; 113653be11f6SPoul-Henning Kamp if (sw_csum & CSUM_DELAY_IP) 1137df8bae1dSRodney W. Grimes mhip->ip_sum = in_cksum(m, mhlen); 1138df8bae1dSRodney W. Grimes *mnext = m; 1139df8bae1dSRodney W. Grimes mnext = &m->m_nextpkt; 1140db4f9cc7SJonathan Lemon nfrags++; 1141df8bae1dSRodney W. Grimes } 1142db4f9cc7SJonathan Lemon ipstat.ips_ofragments += nfrags; 1143db4f9cc7SJonathan Lemon 1144db4f9cc7SJonathan Lemon /* set first/last markers for fragment chain */ 1145db4f9cc7SJonathan Lemon m->m_flags |= M_LASTFRAG; 1146db4f9cc7SJonathan Lemon m0->m_flags |= M_FIRSTFRAG | M_FRAG; 1147db4f9cc7SJonathan Lemon m0->m_pkthdr.csum_data = nfrags; 1148db4f9cc7SJonathan Lemon 1149df8bae1dSRodney W. Grimes /* 1150df8bae1dSRodney W. Grimes * Update first fragment by trimming what's been copied out 1151df8bae1dSRodney W. Grimes * and updating header, then send each fragment (in order). 1152df8bae1dSRodney W. Grimes */ 1153df8bae1dSRodney W. Grimes m = m0; 1154df8bae1dSRodney W. Grimes m_adj(m, hlen + firstlen - (u_short)ip->ip_len); 1155df8bae1dSRodney W. Grimes m->m_pkthdr.len = hlen + firstlen; 1156df8bae1dSRodney W. Grimes ip->ip_len = htons((u_short)m->m_pkthdr.len); 115704287599SRuslan Ermilov ip->ip_off |= IP_MF; 1158fd8e4ebcSMike Barcroft ip->ip_off = htons(ip->ip_off); 1159df8bae1dSRodney W. Grimes ip->ip_sum = 0; 116053be11f6SPoul-Henning Kamp if (sw_csum & CSUM_DELAY_IP) 1161df8bae1dSRodney W. Grimes ip->ip_sum = in_cksum(m, hlen); 1162df8bae1dSRodney W. Grimes sendorfree: 1163df8bae1dSRodney W. Grimes for (m = m0; m; m = m0) { 1164df8bae1dSRodney W. Grimes m0 = m->m_nextpkt; 1165df8bae1dSRodney W. Grimes m->m_nextpkt = 0; 116633841545SHajimu UMEMOTO #ifdef IPSEC 116733841545SHajimu UMEMOTO /* clean ipsec history once it goes out of the node */ 116833841545SHajimu UMEMOTO ipsec_delaux(m); 116933841545SHajimu UMEMOTO #endif 117007203494SDaniel C. Sobral if (error == 0) { 1171fe937674SJosef Karthauser /* Record statistics for this interface address. */ 117207203494SDaniel C. Sobral if (ia != NULL) { 1173fe937674SJosef Karthauser ia->ia_ifa.if_opackets++; 1174fe937674SJosef Karthauser ia->ia_ifa.if_obytes += m->m_pkthdr.len; 117507203494SDaniel C. Sobral } 1176fe937674SJosef Karthauser 1177df8bae1dSRodney W. Grimes error = (*ifp->if_output)(ifp, m, 1178df8bae1dSRodney W. Grimes (struct sockaddr *)dst, ro->ro_rt); 1179fe937674SJosef Karthauser } else 1180df8bae1dSRodney W. Grimes m_freem(m); 1181df8bae1dSRodney W. Grimes } 1182df8bae1dSRodney W. Grimes 1183df8bae1dSRodney W. Grimes if (error == 0) 1184df8bae1dSRodney W. Grimes ipstat.ips_fragmented++; 1185df8bae1dSRodney W. Grimes } 1186df8bae1dSRodney W. Grimes done: 11876a800098SYoshinobu Inoue #ifdef IPSEC 11886a800098SYoshinobu Inoue if (ro == &iproute && ro->ro_rt) { 11896a800098SYoshinobu Inoue RTFREE(ro->ro_rt); 11906a800098SYoshinobu Inoue ro->ro_rt = NULL; 11916a800098SYoshinobu Inoue } 11926a800098SYoshinobu Inoue if (sp != NULL) { 11936a800098SYoshinobu Inoue KEYDEBUG(KEYDEBUG_IPSEC_STAMP, 11946a800098SYoshinobu Inoue printf("DP ip_output call free SP:%p\n", sp)); 11956a800098SYoshinobu Inoue key_freesp(sp); 11966a800098SYoshinobu Inoue } 11976a800098SYoshinobu Inoue #endif /* IPSEC */ 1198b9234fafSSam Leffler #ifdef FAST_IPSEC 1199b9234fafSSam Leffler if (ro == &iproute && ro->ro_rt) { 1200b9234fafSSam Leffler RTFREE(ro->ro_rt); 1201b9234fafSSam Leffler ro->ro_rt = NULL; 1202b9234fafSSam Leffler } 1203b9234fafSSam Leffler if (sp != NULL) 1204b9234fafSSam Leffler KEY_FREESP(&sp); 1205b9234fafSSam Leffler #endif /* FAST_IPSEC */ 1206df8bae1dSRodney W. Grimes return (error); 1207df8bae1dSRodney W. Grimes bad: 12083528d68fSBill Paul m_freem(m); 1209df8bae1dSRodney W. Grimes goto done; 1210df8bae1dSRodney W. Grimes } 1211df8bae1dSRodney W. Grimes 12121c238475SJonathan Lemon void 1213db4f9cc7SJonathan Lemon in_delayed_cksum(struct mbuf *m) 1214db4f9cc7SJonathan Lemon { 1215db4f9cc7SJonathan Lemon struct ip *ip; 1216db4f9cc7SJonathan Lemon u_short csum, offset; 1217db4f9cc7SJonathan Lemon 1218db4f9cc7SJonathan Lemon ip = mtod(m, struct ip *); 121953be11f6SPoul-Henning Kamp offset = ip->ip_hl << 2 ; 1220db4f9cc7SJonathan Lemon csum = in_cksum_skip(m, ip->ip_len, offset); 1221206a3274SRuslan Ermilov if (m->m_pkthdr.csum_flags & CSUM_UDP && csum == 0) 1222206a3274SRuslan Ermilov csum = 0xffff; 1223db4f9cc7SJonathan Lemon offset += m->m_pkthdr.csum_data; /* checksum offset */ 1224db4f9cc7SJonathan Lemon 1225db4f9cc7SJonathan Lemon if (offset + sizeof(u_short) > m->m_len) { 1226db4f9cc7SJonathan Lemon printf("delayed m_pullup, m->len: %d off: %d p: %d\n", 1227db4f9cc7SJonathan Lemon m->m_len, offset, ip->ip_p); 1228db4f9cc7SJonathan Lemon /* 1229db4f9cc7SJonathan Lemon * XXX 1230db4f9cc7SJonathan Lemon * this shouldn't happen, but if it does, the 1231db4f9cc7SJonathan Lemon * correct behavior may be to insert the checksum 1232db4f9cc7SJonathan Lemon * in the existing chain instead of rearranging it. 1233db4f9cc7SJonathan Lemon */ 1234db4f9cc7SJonathan Lemon m = m_pullup(m, offset + sizeof(u_short)); 1235db4f9cc7SJonathan Lemon } 1236db4f9cc7SJonathan Lemon *(u_short *)(m->m_data + offset) = csum; 1237db4f9cc7SJonathan Lemon } 1238db4f9cc7SJonathan Lemon 1239df8bae1dSRodney W. Grimes /* 1240df8bae1dSRodney W. Grimes * Insert IP options into preformed packet. 1241df8bae1dSRodney W. Grimes * Adjust IP destination as required for IP source routing, 1242df8bae1dSRodney W. Grimes * as indicated by a non-zero in_addr at the start of the options. 1243072b9b24SPaul Traina * 1244072b9b24SPaul Traina * XXX This routine assumes that the packet has no options in place. 1245df8bae1dSRodney W. Grimes */ 1246df8bae1dSRodney W. Grimes static struct mbuf * 1247df8bae1dSRodney W. Grimes ip_insertoptions(m, opt, phlen) 1248df8bae1dSRodney W. Grimes register struct mbuf *m; 1249df8bae1dSRodney W. Grimes struct mbuf *opt; 1250df8bae1dSRodney W. Grimes int *phlen; 1251df8bae1dSRodney W. Grimes { 1252df8bae1dSRodney W. Grimes register struct ipoption *p = mtod(opt, struct ipoption *); 1253df8bae1dSRodney W. Grimes struct mbuf *n; 1254df8bae1dSRodney W. Grimes register struct ip *ip = mtod(m, struct ip *); 1255df8bae1dSRodney W. Grimes unsigned optlen; 1256df8bae1dSRodney W. Grimes 1257df8bae1dSRodney W. Grimes optlen = opt->m_len - sizeof(p->ipopt_dst); 1258cb7641e8SMaxim Konovalov if (optlen + (u_short)ip->ip_len > IP_MAXPACKET) { 1259cb7641e8SMaxim Konovalov *phlen = 0; 1260df8bae1dSRodney W. Grimes return (m); /* XXX should fail */ 1261cb7641e8SMaxim Konovalov } 1262df8bae1dSRodney W. Grimes if (p->ipopt_dst.s_addr) 1263df8bae1dSRodney W. Grimes ip->ip_dst = p->ipopt_dst; 1264df8bae1dSRodney W. Grimes if (m->m_flags & M_EXT || m->m_data - optlen < m->m_pktdat) { 1265df8bae1dSRodney W. Grimes MGETHDR(n, M_DONTWAIT, MT_HEADER); 1266cb7641e8SMaxim Konovalov if (n == 0) { 1267cb7641e8SMaxim Konovalov *phlen = 0; 1268df8bae1dSRodney W. Grimes return (m); 1269cb7641e8SMaxim Konovalov } 12705db1e34eSRuslan Ermilov n->m_pkthdr.rcvif = (struct ifnet *)0; 12714ed84624SRobert Watson #ifdef MAC 12724ed84624SRobert Watson mac_create_mbuf_from_mbuf(m, n); 12734ed84624SRobert Watson #endif 1274df8bae1dSRodney W. Grimes n->m_pkthdr.len = m->m_pkthdr.len + optlen; 1275df8bae1dSRodney W. Grimes m->m_len -= sizeof(struct ip); 1276df8bae1dSRodney W. Grimes m->m_data += sizeof(struct ip); 1277df8bae1dSRodney W. Grimes n->m_next = m; 1278df8bae1dSRodney W. Grimes m = n; 1279df8bae1dSRodney W. Grimes m->m_len = optlen + sizeof(struct ip); 1280df8bae1dSRodney W. Grimes m->m_data += max_linkhdr; 128194a5d9b6SDavid Greenman (void)memcpy(mtod(m, void *), ip, sizeof(struct ip)); 1282df8bae1dSRodney W. Grimes } else { 1283df8bae1dSRodney W. Grimes m->m_data -= optlen; 1284df8bae1dSRodney W. Grimes m->m_len += optlen; 1285df8bae1dSRodney W. Grimes m->m_pkthdr.len += optlen; 1286df8bae1dSRodney W. Grimes ovbcopy((caddr_t)ip, mtod(m, caddr_t), sizeof(struct ip)); 1287df8bae1dSRodney W. Grimes } 1288df8bae1dSRodney W. Grimes ip = mtod(m, struct ip *); 12890453d3cbSBruce Evans bcopy(p->ipopt_list, ip + 1, optlen); 1290df8bae1dSRodney W. Grimes *phlen = sizeof(struct ip) + optlen; 129153be11f6SPoul-Henning Kamp ip->ip_v = IPVERSION; 129253be11f6SPoul-Henning Kamp ip->ip_hl = *phlen >> 2; 1293df8bae1dSRodney W. Grimes ip->ip_len += optlen; 1294df8bae1dSRodney W. Grimes return (m); 1295df8bae1dSRodney W. Grimes } 1296df8bae1dSRodney W. Grimes 1297df8bae1dSRodney W. Grimes /* 1298df8bae1dSRodney W. Grimes * Copy options from ip to jp, 1299df8bae1dSRodney W. Grimes * omitting those not copied during fragmentation. 1300df8bae1dSRodney W. Grimes */ 1301beec8214SDarren Reed int 1302df8bae1dSRodney W. Grimes ip_optcopy(ip, jp) 1303df8bae1dSRodney W. Grimes struct ip *ip, *jp; 1304df8bae1dSRodney W. Grimes { 1305df8bae1dSRodney W. Grimes register u_char *cp, *dp; 1306df8bae1dSRodney W. Grimes int opt, optlen, cnt; 1307df8bae1dSRodney W. Grimes 1308df8bae1dSRodney W. Grimes cp = (u_char *)(ip + 1); 1309df8bae1dSRodney W. Grimes dp = (u_char *)(jp + 1); 131053be11f6SPoul-Henning Kamp cnt = (ip->ip_hl << 2) - sizeof (struct ip); 1311df8bae1dSRodney W. Grimes for (; cnt > 0; cnt -= optlen, cp += optlen) { 1312df8bae1dSRodney W. Grimes opt = cp[0]; 1313df8bae1dSRodney W. Grimes if (opt == IPOPT_EOL) 1314df8bae1dSRodney W. Grimes break; 1315df8bae1dSRodney W. Grimes if (opt == IPOPT_NOP) { 1316df8bae1dSRodney W. Grimes /* Preserve for IP mcast tunnel's LSRR alignment. */ 1317df8bae1dSRodney W. Grimes *dp++ = IPOPT_NOP; 1318df8bae1dSRodney W. Grimes optlen = 1; 1319df8bae1dSRodney W. Grimes continue; 1320686cdd19SJun-ichiro itojun Hagino } 1321db40007dSAndrew R. Reiter 1322db40007dSAndrew R. Reiter KASSERT(cnt >= IPOPT_OLEN + sizeof(*cp), 1323db40007dSAndrew R. Reiter ("ip_optcopy: malformed ipv4 option")); 1324df8bae1dSRodney W. Grimes optlen = cp[IPOPT_OLEN]; 1325db40007dSAndrew R. Reiter KASSERT(optlen >= IPOPT_OLEN + sizeof(*cp) && optlen <= cnt, 1326db40007dSAndrew R. Reiter ("ip_optcopy: malformed ipv4 option")); 1327db40007dSAndrew R. Reiter 1328df8bae1dSRodney W. Grimes /* bogus lengths should have been caught by ip_dooptions */ 1329df8bae1dSRodney W. Grimes if (optlen > cnt) 1330df8bae1dSRodney W. Grimes optlen = cnt; 1331df8bae1dSRodney W. Grimes if (IPOPT_COPIED(opt)) { 13320453d3cbSBruce Evans bcopy(cp, dp, optlen); 1333df8bae1dSRodney W. Grimes dp += optlen; 1334df8bae1dSRodney W. Grimes } 1335df8bae1dSRodney W. Grimes } 1336df8bae1dSRodney W. Grimes for (optlen = dp - (u_char *)(jp+1); optlen & 0x3; optlen++) 1337df8bae1dSRodney W. Grimes *dp++ = IPOPT_EOL; 1338df8bae1dSRodney W. Grimes return (optlen); 1339df8bae1dSRodney W. Grimes } 1340df8bae1dSRodney W. Grimes 1341df8bae1dSRodney W. Grimes /* 1342df8bae1dSRodney W. Grimes * IP socket option processing. 1343df8bae1dSRodney W. Grimes */ 1344df8bae1dSRodney W. Grimes int 1345cfe8b629SGarrett Wollman ip_ctloutput(so, sopt) 1346df8bae1dSRodney W. Grimes struct socket *so; 1347cfe8b629SGarrett Wollman struct sockopt *sopt; 1348df8bae1dSRodney W. Grimes { 1349cfe8b629SGarrett Wollman struct inpcb *inp = sotoinpcb(so); 1350cfe8b629SGarrett Wollman int error, optval; 1351df8bae1dSRodney W. Grimes 1352cfe8b629SGarrett Wollman error = optval = 0; 1353cfe8b629SGarrett Wollman if (sopt->sopt_level != IPPROTO_IP) { 1354cfe8b629SGarrett Wollman return (EINVAL); 1355cfe8b629SGarrett Wollman } 1356df8bae1dSRodney W. Grimes 1357cfe8b629SGarrett Wollman switch (sopt->sopt_dir) { 1358cfe8b629SGarrett Wollman case SOPT_SET: 1359cfe8b629SGarrett Wollman switch (sopt->sopt_name) { 1360df8bae1dSRodney W. Grimes case IP_OPTIONS: 1361df8bae1dSRodney W. Grimes #ifdef notyet 1362df8bae1dSRodney W. Grimes case IP_RETOPTS: 1363df8bae1dSRodney W. Grimes #endif 1364cfe8b629SGarrett Wollman { 1365cfe8b629SGarrett Wollman struct mbuf *m; 1366cfe8b629SGarrett Wollman if (sopt->sopt_valsize > MLEN) { 1367cfe8b629SGarrett Wollman error = EMSGSIZE; 1368cfe8b629SGarrett Wollman break; 1369cfe8b629SGarrett Wollman } 1370b40ce416SJulian Elischer MGET(m, sopt->sopt_td ? M_TRYWAIT : M_DONTWAIT, MT_HEADER); 1371cfe8b629SGarrett Wollman if (m == 0) { 1372cfe8b629SGarrett Wollman error = ENOBUFS; 1373cfe8b629SGarrett Wollman break; 1374cfe8b629SGarrett Wollman } 1375cfe8b629SGarrett Wollman m->m_len = sopt->sopt_valsize; 1376cfe8b629SGarrett Wollman error = sooptcopyin(sopt, mtod(m, char *), m->m_len, 1377cfe8b629SGarrett Wollman m->m_len); 1378cfe8b629SGarrett Wollman 1379cfe8b629SGarrett Wollman return (ip_pcbopts(sopt->sopt_name, &inp->inp_options, 1380cfe8b629SGarrett Wollman m)); 1381cfe8b629SGarrett Wollman } 1382df8bae1dSRodney W. Grimes 1383df8bae1dSRodney W. Grimes case IP_TOS: 1384df8bae1dSRodney W. Grimes case IP_TTL: 1385df8bae1dSRodney W. Grimes case IP_RECVOPTS: 1386df8bae1dSRodney W. Grimes case IP_RECVRETOPTS: 1387df8bae1dSRodney W. Grimes case IP_RECVDSTADDR: 138882c23ebaSBill Fenner case IP_RECVIF: 13896a800098SYoshinobu Inoue case IP_FAITH: 1390cfe8b629SGarrett Wollman error = sooptcopyin(sopt, &optval, sizeof optval, 1391cfe8b629SGarrett Wollman sizeof optval); 1392cfe8b629SGarrett Wollman if (error) 1393cfe8b629SGarrett Wollman break; 1394df8bae1dSRodney W. Grimes 1395cfe8b629SGarrett Wollman switch (sopt->sopt_name) { 1396df8bae1dSRodney W. Grimes case IP_TOS: 1397ca98b82cSDavid Greenman inp->inp_ip_tos = optval; 1398df8bae1dSRodney W. Grimes break; 1399df8bae1dSRodney W. Grimes 1400df8bae1dSRodney W. Grimes case IP_TTL: 1401ca98b82cSDavid Greenman inp->inp_ip_ttl = optval; 1402df8bae1dSRodney W. Grimes break; 1403df8bae1dSRodney W. Grimes #define OPTSET(bit) \ 1404df8bae1dSRodney W. Grimes if (optval) \ 1405df8bae1dSRodney W. Grimes inp->inp_flags |= bit; \ 1406df8bae1dSRodney W. Grimes else \ 1407df8bae1dSRodney W. Grimes inp->inp_flags &= ~bit; 1408df8bae1dSRodney W. Grimes 1409df8bae1dSRodney W. Grimes case IP_RECVOPTS: 1410df8bae1dSRodney W. Grimes OPTSET(INP_RECVOPTS); 1411df8bae1dSRodney W. Grimes break; 1412df8bae1dSRodney W. Grimes 1413df8bae1dSRodney W. Grimes case IP_RECVRETOPTS: 1414df8bae1dSRodney W. Grimes OPTSET(INP_RECVRETOPTS); 1415df8bae1dSRodney W. Grimes break; 1416df8bae1dSRodney W. Grimes 1417df8bae1dSRodney W. Grimes case IP_RECVDSTADDR: 1418df8bae1dSRodney W. Grimes OPTSET(INP_RECVDSTADDR); 1419df8bae1dSRodney W. Grimes break; 142082c23ebaSBill Fenner 142182c23ebaSBill Fenner case IP_RECVIF: 142282c23ebaSBill Fenner OPTSET(INP_RECVIF); 142382c23ebaSBill Fenner break; 14246a800098SYoshinobu Inoue 14256a800098SYoshinobu Inoue case IP_FAITH: 14266a800098SYoshinobu Inoue OPTSET(INP_FAITH); 14276a800098SYoshinobu Inoue break; 1428df8bae1dSRodney W. Grimes } 1429df8bae1dSRodney W. Grimes break; 1430df8bae1dSRodney W. Grimes #undef OPTSET 1431df8bae1dSRodney W. Grimes 1432df8bae1dSRodney W. Grimes case IP_MULTICAST_IF: 1433f0068c4aSGarrett Wollman case IP_MULTICAST_VIF: 1434df8bae1dSRodney W. Grimes case IP_MULTICAST_TTL: 1435df8bae1dSRodney W. Grimes case IP_MULTICAST_LOOP: 1436df8bae1dSRodney W. Grimes case IP_ADD_MEMBERSHIP: 1437df8bae1dSRodney W. Grimes case IP_DROP_MEMBERSHIP: 1438cfe8b629SGarrett Wollman error = ip_setmoptions(sopt, &inp->inp_moptions); 1439df8bae1dSRodney W. Grimes break; 1440df8bae1dSRodney W. Grimes 144133b3ac06SPeter Wemm case IP_PORTRANGE: 1442cfe8b629SGarrett Wollman error = sooptcopyin(sopt, &optval, sizeof optval, 1443cfe8b629SGarrett Wollman sizeof optval); 1444cfe8b629SGarrett Wollman if (error) 1445cfe8b629SGarrett Wollman break; 144633b3ac06SPeter Wemm 144733b3ac06SPeter Wemm switch (optval) { 144833b3ac06SPeter Wemm case IP_PORTRANGE_DEFAULT: 144933b3ac06SPeter Wemm inp->inp_flags &= ~(INP_LOWPORT); 145033b3ac06SPeter Wemm inp->inp_flags &= ~(INP_HIGHPORT); 145133b3ac06SPeter Wemm break; 145233b3ac06SPeter Wemm 145333b3ac06SPeter Wemm case IP_PORTRANGE_HIGH: 145433b3ac06SPeter Wemm inp->inp_flags &= ~(INP_LOWPORT); 145533b3ac06SPeter Wemm inp->inp_flags |= INP_HIGHPORT; 145633b3ac06SPeter Wemm break; 145733b3ac06SPeter Wemm 145833b3ac06SPeter Wemm case IP_PORTRANGE_LOW: 145933b3ac06SPeter Wemm inp->inp_flags &= ~(INP_HIGHPORT); 146033b3ac06SPeter Wemm inp->inp_flags |= INP_LOWPORT; 146133b3ac06SPeter Wemm break; 146233b3ac06SPeter Wemm 146333b3ac06SPeter Wemm default: 146433b3ac06SPeter Wemm error = EINVAL; 146533b3ac06SPeter Wemm break; 146633b3ac06SPeter Wemm } 1467ce8c72b1SPeter Wemm break; 146833b3ac06SPeter Wemm 1469b9234fafSSam Leffler #if defined(IPSEC) || defined(FAST_IPSEC) 14706a800098SYoshinobu Inoue case IP_IPSEC_POLICY: 14716a800098SYoshinobu Inoue { 14726a800098SYoshinobu Inoue caddr_t req; 1473686cdd19SJun-ichiro itojun Hagino size_t len = 0; 14746a800098SYoshinobu Inoue int priv; 14756a800098SYoshinobu Inoue struct mbuf *m; 14766a800098SYoshinobu Inoue int optname; 14776a800098SYoshinobu Inoue 14786a800098SYoshinobu Inoue if ((error = soopt_getm(sopt, &m)) != 0) /* XXX */ 14796a800098SYoshinobu Inoue break; 14806a800098SYoshinobu Inoue if ((error = soopt_mcopyin(sopt, m)) != 0) /* XXX */ 14816a800098SYoshinobu Inoue break; 1482b40ce416SJulian Elischer priv = (sopt->sopt_td != NULL && 148344731cabSJohn Baldwin suser(sopt->sopt_td) != 0) ? 0 : 1; 14846a800098SYoshinobu Inoue req = mtod(m, caddr_t); 1485686cdd19SJun-ichiro itojun Hagino len = m->m_len; 14866a800098SYoshinobu Inoue optname = sopt->sopt_name; 1487686cdd19SJun-ichiro itojun Hagino error = ipsec4_set_policy(inp, optname, req, len, priv); 14886a800098SYoshinobu Inoue m_freem(m); 14896a800098SYoshinobu Inoue break; 14906a800098SYoshinobu Inoue } 14916a800098SYoshinobu Inoue #endif /*IPSEC*/ 14926a800098SYoshinobu Inoue 1493df8bae1dSRodney W. Grimes default: 1494df8bae1dSRodney W. Grimes error = ENOPROTOOPT; 1495df8bae1dSRodney W. Grimes break; 1496df8bae1dSRodney W. Grimes } 1497df8bae1dSRodney W. Grimes break; 1498df8bae1dSRodney W. Grimes 1499cfe8b629SGarrett Wollman case SOPT_GET: 1500cfe8b629SGarrett Wollman switch (sopt->sopt_name) { 1501df8bae1dSRodney W. Grimes case IP_OPTIONS: 1502df8bae1dSRodney W. Grimes case IP_RETOPTS: 1503cfe8b629SGarrett Wollman if (inp->inp_options) 1504cfe8b629SGarrett Wollman error = sooptcopyout(sopt, 1505cfe8b629SGarrett Wollman mtod(inp->inp_options, 1506cfe8b629SGarrett Wollman char *), 1507cfe8b629SGarrett Wollman inp->inp_options->m_len); 1508cfe8b629SGarrett Wollman else 1509cfe8b629SGarrett Wollman sopt->sopt_valsize = 0; 1510df8bae1dSRodney W. Grimes break; 1511df8bae1dSRodney W. Grimes 1512df8bae1dSRodney W. Grimes case IP_TOS: 1513df8bae1dSRodney W. Grimes case IP_TTL: 1514df8bae1dSRodney W. Grimes case IP_RECVOPTS: 1515df8bae1dSRodney W. Grimes case IP_RECVRETOPTS: 1516df8bae1dSRodney W. Grimes case IP_RECVDSTADDR: 151782c23ebaSBill Fenner case IP_RECVIF: 1518cfe8b629SGarrett Wollman case IP_PORTRANGE: 15196a800098SYoshinobu Inoue case IP_FAITH: 1520cfe8b629SGarrett Wollman switch (sopt->sopt_name) { 1521df8bae1dSRodney W. Grimes 1522df8bae1dSRodney W. Grimes case IP_TOS: 1523ca98b82cSDavid Greenman optval = inp->inp_ip_tos; 1524df8bae1dSRodney W. Grimes break; 1525df8bae1dSRodney W. Grimes 1526df8bae1dSRodney W. Grimes case IP_TTL: 1527ca98b82cSDavid Greenman optval = inp->inp_ip_ttl; 1528df8bae1dSRodney W. Grimes break; 1529df8bae1dSRodney W. Grimes 1530df8bae1dSRodney W. Grimes #define OPTBIT(bit) (inp->inp_flags & bit ? 1 : 0) 1531df8bae1dSRodney W. Grimes 1532df8bae1dSRodney W. Grimes case IP_RECVOPTS: 1533df8bae1dSRodney W. Grimes optval = OPTBIT(INP_RECVOPTS); 1534df8bae1dSRodney W. Grimes break; 1535df8bae1dSRodney W. Grimes 1536df8bae1dSRodney W. Grimes case IP_RECVRETOPTS: 1537df8bae1dSRodney W. Grimes optval = OPTBIT(INP_RECVRETOPTS); 1538df8bae1dSRodney W. Grimes break; 1539df8bae1dSRodney W. Grimes 1540df8bae1dSRodney W. Grimes case IP_RECVDSTADDR: 1541df8bae1dSRodney W. Grimes optval = OPTBIT(INP_RECVDSTADDR); 1542df8bae1dSRodney W. Grimes break; 154382c23ebaSBill Fenner 154482c23ebaSBill Fenner case IP_RECVIF: 154582c23ebaSBill Fenner optval = OPTBIT(INP_RECVIF); 154682c23ebaSBill Fenner break; 1547cfe8b629SGarrett Wollman 1548cfe8b629SGarrett Wollman case IP_PORTRANGE: 1549cfe8b629SGarrett Wollman if (inp->inp_flags & INP_HIGHPORT) 1550cfe8b629SGarrett Wollman optval = IP_PORTRANGE_HIGH; 1551cfe8b629SGarrett Wollman else if (inp->inp_flags & INP_LOWPORT) 1552cfe8b629SGarrett Wollman optval = IP_PORTRANGE_LOW; 1553cfe8b629SGarrett Wollman else 1554cfe8b629SGarrett Wollman optval = 0; 1555cfe8b629SGarrett Wollman break; 15566a800098SYoshinobu Inoue 15576a800098SYoshinobu Inoue case IP_FAITH: 15586a800098SYoshinobu Inoue optval = OPTBIT(INP_FAITH); 15596a800098SYoshinobu Inoue break; 1560df8bae1dSRodney W. Grimes } 1561cfe8b629SGarrett Wollman error = sooptcopyout(sopt, &optval, sizeof optval); 1562df8bae1dSRodney W. Grimes break; 1563df8bae1dSRodney W. Grimes 1564df8bae1dSRodney W. Grimes case IP_MULTICAST_IF: 1565f0068c4aSGarrett Wollman case IP_MULTICAST_VIF: 1566df8bae1dSRodney W. Grimes case IP_MULTICAST_TTL: 1567df8bae1dSRodney W. Grimes case IP_MULTICAST_LOOP: 1568df8bae1dSRodney W. Grimes case IP_ADD_MEMBERSHIP: 1569df8bae1dSRodney W. Grimes case IP_DROP_MEMBERSHIP: 1570cfe8b629SGarrett Wollman error = ip_getmoptions(sopt, inp->inp_moptions); 157133b3ac06SPeter Wemm break; 157233b3ac06SPeter Wemm 1573b9234fafSSam Leffler #if defined(IPSEC) || defined(FAST_IPSEC) 15746a800098SYoshinobu Inoue case IP_IPSEC_POLICY: 15756a800098SYoshinobu Inoue { 1576f63e7634SYoshinobu Inoue struct mbuf *m = NULL; 15776a800098SYoshinobu Inoue caddr_t req = NULL; 1578686cdd19SJun-ichiro itojun Hagino size_t len = 0; 15796a800098SYoshinobu Inoue 1580686cdd19SJun-ichiro itojun Hagino if (m != 0) { 15816a800098SYoshinobu Inoue req = mtod(m, caddr_t); 1582686cdd19SJun-ichiro itojun Hagino len = m->m_len; 1583686cdd19SJun-ichiro itojun Hagino } 1584686cdd19SJun-ichiro itojun Hagino error = ipsec4_get_policy(sotoinpcb(so), req, len, &m); 15856a800098SYoshinobu Inoue if (error == 0) 15866a800098SYoshinobu Inoue error = soopt_mcopyout(sopt, m); /* XXX */ 1587f63e7634SYoshinobu Inoue if (error == 0) 15886a800098SYoshinobu Inoue m_freem(m); 15896a800098SYoshinobu Inoue break; 15906a800098SYoshinobu Inoue } 15916a800098SYoshinobu Inoue #endif /*IPSEC*/ 15926a800098SYoshinobu Inoue 1593df8bae1dSRodney W. Grimes default: 1594df8bae1dSRodney W. Grimes error = ENOPROTOOPT; 1595df8bae1dSRodney W. Grimes break; 1596df8bae1dSRodney W. Grimes } 1597df8bae1dSRodney W. Grimes break; 1598df8bae1dSRodney W. Grimes } 1599df8bae1dSRodney W. Grimes return (error); 1600df8bae1dSRodney W. Grimes } 1601df8bae1dSRodney W. Grimes 1602df8bae1dSRodney W. Grimes /* 1603df8bae1dSRodney W. Grimes * Set up IP options in pcb for insertion in output packets. 1604df8bae1dSRodney W. Grimes * Store in mbuf with pointer in pcbopt, adding pseudo-option 1605df8bae1dSRodney W. Grimes * with destination address if source routed. 1606df8bae1dSRodney W. Grimes */ 16070312fbe9SPoul-Henning Kamp static int 1608df8bae1dSRodney W. Grimes ip_pcbopts(optname, pcbopt, m) 1609df8bae1dSRodney W. Grimes int optname; 1610df8bae1dSRodney W. Grimes struct mbuf **pcbopt; 1611df8bae1dSRodney W. Grimes register struct mbuf *m; 1612df8bae1dSRodney W. Grimes { 1613d68fa50cSBruce Evans register int cnt, optlen; 1614df8bae1dSRodney W. Grimes register u_char *cp; 1615df8bae1dSRodney W. Grimes u_char opt; 1616df8bae1dSRodney W. Grimes 1617df8bae1dSRodney W. Grimes /* turn off any old options */ 1618df8bae1dSRodney W. Grimes if (*pcbopt) 1619df8bae1dSRodney W. Grimes (void)m_free(*pcbopt); 1620df8bae1dSRodney W. Grimes *pcbopt = 0; 1621df8bae1dSRodney W. Grimes if (m == (struct mbuf *)0 || m->m_len == 0) { 1622df8bae1dSRodney W. Grimes /* 1623df8bae1dSRodney W. Grimes * Only turning off any previous options. 1624df8bae1dSRodney W. Grimes */ 1625df8bae1dSRodney W. Grimes if (m) 1626df8bae1dSRodney W. Grimes (void)m_free(m); 1627df8bae1dSRodney W. Grimes return (0); 1628df8bae1dSRodney W. Grimes } 1629df8bae1dSRodney W. Grimes 16300c8d2590SBruce Evans if (m->m_len % sizeof(int32_t)) 1631df8bae1dSRodney W. Grimes goto bad; 1632df8bae1dSRodney W. Grimes /* 1633df8bae1dSRodney W. Grimes * IP first-hop destination address will be stored before 1634df8bae1dSRodney W. Grimes * actual options; move other options back 1635df8bae1dSRodney W. Grimes * and clear it when none present. 1636df8bae1dSRodney W. Grimes */ 1637df8bae1dSRodney W. Grimes if (m->m_data + m->m_len + sizeof(struct in_addr) >= &m->m_dat[MLEN]) 1638df8bae1dSRodney W. Grimes goto bad; 1639df8bae1dSRodney W. Grimes cnt = m->m_len; 1640df8bae1dSRodney W. Grimes m->m_len += sizeof(struct in_addr); 1641df8bae1dSRodney W. Grimes cp = mtod(m, u_char *) + sizeof(struct in_addr); 1642df8bae1dSRodney W. Grimes ovbcopy(mtod(m, caddr_t), (caddr_t)cp, (unsigned)cnt); 1643df8bae1dSRodney W. Grimes bzero(mtod(m, caddr_t), sizeof(struct in_addr)); 1644df8bae1dSRodney W. Grimes 1645df8bae1dSRodney W. Grimes for (; cnt > 0; cnt -= optlen, cp += optlen) { 1646df8bae1dSRodney W. Grimes opt = cp[IPOPT_OPTVAL]; 1647df8bae1dSRodney W. Grimes if (opt == IPOPT_EOL) 1648df8bae1dSRodney W. Grimes break; 1649df8bae1dSRodney W. Grimes if (opt == IPOPT_NOP) 1650df8bae1dSRodney W. Grimes optlen = 1; 1651df8bae1dSRodney W. Grimes else { 1652707d00a3SJonathan Lemon if (cnt < IPOPT_OLEN + sizeof(*cp)) 1653707d00a3SJonathan Lemon goto bad; 1654df8bae1dSRodney W. Grimes optlen = cp[IPOPT_OLEN]; 1655707d00a3SJonathan Lemon if (optlen < IPOPT_OLEN + sizeof(*cp) || optlen > cnt) 1656df8bae1dSRodney W. Grimes goto bad; 1657df8bae1dSRodney W. Grimes } 1658df8bae1dSRodney W. Grimes switch (opt) { 1659df8bae1dSRodney W. Grimes 1660df8bae1dSRodney W. Grimes default: 1661df8bae1dSRodney W. Grimes break; 1662df8bae1dSRodney W. Grimes 1663df8bae1dSRodney W. Grimes case IPOPT_LSRR: 1664df8bae1dSRodney W. Grimes case IPOPT_SSRR: 1665df8bae1dSRodney W. Grimes /* 1666df8bae1dSRodney W. Grimes * user process specifies route as: 1667df8bae1dSRodney W. Grimes * ->A->B->C->D 1668df8bae1dSRodney W. Grimes * D must be our final destination (but we can't 1669df8bae1dSRodney W. Grimes * check that since we may not have connected yet). 1670df8bae1dSRodney W. Grimes * A is first hop destination, which doesn't appear in 1671df8bae1dSRodney W. Grimes * actual IP option, but is stored before the options. 1672df8bae1dSRodney W. Grimes */ 1673df8bae1dSRodney W. Grimes if (optlen < IPOPT_MINOFF - 1 + sizeof(struct in_addr)) 1674df8bae1dSRodney W. Grimes goto bad; 1675df8bae1dSRodney W. Grimes m->m_len -= sizeof(struct in_addr); 1676df8bae1dSRodney W. Grimes cnt -= sizeof(struct in_addr); 1677df8bae1dSRodney W. Grimes optlen -= sizeof(struct in_addr); 1678df8bae1dSRodney W. Grimes cp[IPOPT_OLEN] = optlen; 1679df8bae1dSRodney W. Grimes /* 1680df8bae1dSRodney W. Grimes * Move first hop before start of options. 1681df8bae1dSRodney W. Grimes */ 1682df8bae1dSRodney W. Grimes bcopy((caddr_t)&cp[IPOPT_OFFSET+1], mtod(m, caddr_t), 1683df8bae1dSRodney W. Grimes sizeof(struct in_addr)); 1684df8bae1dSRodney W. Grimes /* 1685df8bae1dSRodney W. Grimes * Then copy rest of options back 1686df8bae1dSRodney W. Grimes * to close up the deleted entry. 1687df8bae1dSRodney W. Grimes */ 1688df8bae1dSRodney W. Grimes ovbcopy((caddr_t)(&cp[IPOPT_OFFSET+1] + 1689df8bae1dSRodney W. Grimes sizeof(struct in_addr)), 1690df8bae1dSRodney W. Grimes (caddr_t)&cp[IPOPT_OFFSET+1], 1691df8bae1dSRodney W. Grimes (unsigned)cnt + sizeof(struct in_addr)); 1692df8bae1dSRodney W. Grimes break; 1693df8bae1dSRodney W. Grimes } 1694df8bae1dSRodney W. Grimes } 1695df8bae1dSRodney W. Grimes if (m->m_len > MAX_IPOPTLEN + sizeof(struct in_addr)) 1696df8bae1dSRodney W. Grimes goto bad; 1697df8bae1dSRodney W. Grimes *pcbopt = m; 1698df8bae1dSRodney W. Grimes return (0); 1699df8bae1dSRodney W. Grimes 1700df8bae1dSRodney W. Grimes bad: 1701df8bae1dSRodney W. Grimes (void)m_free(m); 1702df8bae1dSRodney W. Grimes return (EINVAL); 1703df8bae1dSRodney W. Grimes } 1704df8bae1dSRodney W. Grimes 1705df8bae1dSRodney W. Grimes /* 1706cfe8b629SGarrett Wollman * XXX 1707cfe8b629SGarrett Wollman * The whole multicast option thing needs to be re-thought. 1708cfe8b629SGarrett Wollman * Several of these options are equally applicable to non-multicast 1709cfe8b629SGarrett Wollman * transmission, and one (IP_MULTICAST_TTL) totally duplicates a 1710cfe8b629SGarrett Wollman * standard option (IP_TTL). 1711cfe8b629SGarrett Wollman */ 171233841545SHajimu UMEMOTO 171333841545SHajimu UMEMOTO /* 171433841545SHajimu UMEMOTO * following RFC1724 section 3.3, 0.0.0.0/8 is interpreted as interface index. 171533841545SHajimu UMEMOTO */ 171633841545SHajimu UMEMOTO static struct ifnet * 171733841545SHajimu UMEMOTO ip_multicast_if(a, ifindexp) 171833841545SHajimu UMEMOTO struct in_addr *a; 171933841545SHajimu UMEMOTO int *ifindexp; 172033841545SHajimu UMEMOTO { 172133841545SHajimu UMEMOTO int ifindex; 172233841545SHajimu UMEMOTO struct ifnet *ifp; 172333841545SHajimu UMEMOTO 172433841545SHajimu UMEMOTO if (ifindexp) 172533841545SHajimu UMEMOTO *ifindexp = 0; 172633841545SHajimu UMEMOTO if (ntohl(a->s_addr) >> 24 == 0) { 172733841545SHajimu UMEMOTO ifindex = ntohl(a->s_addr) & 0xffffff; 172833841545SHajimu UMEMOTO if (ifindex < 0 || if_index < ifindex) 172933841545SHajimu UMEMOTO return NULL; 1730f9132cebSJonathan Lemon ifp = ifnet_byindex(ifindex); 173133841545SHajimu UMEMOTO if (ifindexp) 173233841545SHajimu UMEMOTO *ifindexp = ifindex; 173333841545SHajimu UMEMOTO } else { 173433841545SHajimu UMEMOTO INADDR_TO_IFP(*a, ifp); 173533841545SHajimu UMEMOTO } 173633841545SHajimu UMEMOTO return ifp; 173733841545SHajimu UMEMOTO } 173833841545SHajimu UMEMOTO 1739cfe8b629SGarrett Wollman /* 1740df8bae1dSRodney W. Grimes * Set the IP multicast options in response to user setsockopt(). 1741df8bae1dSRodney W. Grimes */ 17420312fbe9SPoul-Henning Kamp static int 1743cfe8b629SGarrett Wollman ip_setmoptions(sopt, imop) 1744cfe8b629SGarrett Wollman struct sockopt *sopt; 1745df8bae1dSRodney W. Grimes struct ip_moptions **imop; 1746df8bae1dSRodney W. Grimes { 1747cfe8b629SGarrett Wollman int error = 0; 1748cfe8b629SGarrett Wollman int i; 1749df8bae1dSRodney W. Grimes struct in_addr addr; 1750cfe8b629SGarrett Wollman struct ip_mreq mreq; 1751cfe8b629SGarrett Wollman struct ifnet *ifp; 1752cfe8b629SGarrett Wollman struct ip_moptions *imo = *imop; 1753df8bae1dSRodney W. Grimes struct route ro; 1754cfe8b629SGarrett Wollman struct sockaddr_in *dst; 175533841545SHajimu UMEMOTO int ifindex; 17569b626c29SGarrett Wollman int s; 1757df8bae1dSRodney W. Grimes 1758df8bae1dSRodney W. Grimes if (imo == NULL) { 1759df8bae1dSRodney W. Grimes /* 1760df8bae1dSRodney W. Grimes * No multicast option buffer attached to the pcb; 1761df8bae1dSRodney W. Grimes * allocate one and initialize to default values. 1762df8bae1dSRodney W. Grimes */ 1763df8bae1dSRodney W. Grimes imo = (struct ip_moptions*)malloc(sizeof(*imo), M_IPMOPTS, 1764df8bae1dSRodney W. Grimes M_WAITOK); 1765df8bae1dSRodney W. Grimes 1766df8bae1dSRodney W. Grimes if (imo == NULL) 1767df8bae1dSRodney W. Grimes return (ENOBUFS); 1768df8bae1dSRodney W. Grimes *imop = imo; 1769df8bae1dSRodney W. Grimes imo->imo_multicast_ifp = NULL; 177033841545SHajimu UMEMOTO imo->imo_multicast_addr.s_addr = INADDR_ANY; 17711c5de19aSGarrett Wollman imo->imo_multicast_vif = -1; 1772df8bae1dSRodney W. Grimes imo->imo_multicast_ttl = IP_DEFAULT_MULTICAST_TTL; 1773df8bae1dSRodney W. Grimes imo->imo_multicast_loop = IP_DEFAULT_MULTICAST_LOOP; 1774df8bae1dSRodney W. Grimes imo->imo_num_memberships = 0; 1775df8bae1dSRodney W. Grimes } 1776df8bae1dSRodney W. Grimes 1777cfe8b629SGarrett Wollman switch (sopt->sopt_name) { 1778f0068c4aSGarrett Wollman /* store an index number for the vif you wanna use in the send */ 1779f0068c4aSGarrett Wollman case IP_MULTICAST_VIF: 1780cfe8b629SGarrett Wollman if (legal_vif_num == 0) { 17815e9ae478SGarrett Wollman error = EOPNOTSUPP; 17825e9ae478SGarrett Wollman break; 17835e9ae478SGarrett Wollman } 1784cfe8b629SGarrett Wollman error = sooptcopyin(sopt, &i, sizeof i, sizeof i); 1785cfe8b629SGarrett Wollman if (error) 1786f0068c4aSGarrett Wollman break; 17871c5de19aSGarrett Wollman if (!legal_vif_num(i) && (i != -1)) { 1788f0068c4aSGarrett Wollman error = EINVAL; 1789f0068c4aSGarrett Wollman break; 1790f0068c4aSGarrett Wollman } 1791f0068c4aSGarrett Wollman imo->imo_multicast_vif = i; 1792f0068c4aSGarrett Wollman break; 1793f0068c4aSGarrett Wollman 1794df8bae1dSRodney W. Grimes case IP_MULTICAST_IF: 1795df8bae1dSRodney W. Grimes /* 1796df8bae1dSRodney W. Grimes * Select the interface for outgoing multicast packets. 1797df8bae1dSRodney W. Grimes */ 1798cfe8b629SGarrett Wollman error = sooptcopyin(sopt, &addr, sizeof addr, sizeof addr); 1799cfe8b629SGarrett Wollman if (error) 1800df8bae1dSRodney W. Grimes break; 1801df8bae1dSRodney W. Grimes /* 1802df8bae1dSRodney W. Grimes * INADDR_ANY is used to remove a previous selection. 1803df8bae1dSRodney W. Grimes * When no interface is selected, a default one is 1804df8bae1dSRodney W. Grimes * chosen every time a multicast packet is sent. 1805df8bae1dSRodney W. Grimes */ 1806df8bae1dSRodney W. Grimes if (addr.s_addr == INADDR_ANY) { 1807df8bae1dSRodney W. Grimes imo->imo_multicast_ifp = NULL; 1808df8bae1dSRodney W. Grimes break; 1809df8bae1dSRodney W. Grimes } 1810df8bae1dSRodney W. Grimes /* 1811df8bae1dSRodney W. Grimes * The selected interface is identified by its local 1812df8bae1dSRodney W. Grimes * IP address. Find the interface and confirm that 1813df8bae1dSRodney W. Grimes * it supports multicasting. 1814df8bae1dSRodney W. Grimes */ 181520e8807cSGarrett Wollman s = splimp(); 181633841545SHajimu UMEMOTO ifp = ip_multicast_if(&addr, &ifindex); 1817df8bae1dSRodney W. Grimes if (ifp == NULL || (ifp->if_flags & IFF_MULTICAST) == 0) { 1818fbc6ab00SBill Fenner splx(s); 1819df8bae1dSRodney W. Grimes error = EADDRNOTAVAIL; 1820df8bae1dSRodney W. Grimes break; 1821df8bae1dSRodney W. Grimes } 1822df8bae1dSRodney W. Grimes imo->imo_multicast_ifp = ifp; 182333841545SHajimu UMEMOTO if (ifindex) 182433841545SHajimu UMEMOTO imo->imo_multicast_addr = addr; 182533841545SHajimu UMEMOTO else 182633841545SHajimu UMEMOTO imo->imo_multicast_addr.s_addr = INADDR_ANY; 18279b626c29SGarrett Wollman splx(s); 1828df8bae1dSRodney W. Grimes break; 1829df8bae1dSRodney W. Grimes 1830df8bae1dSRodney W. Grimes case IP_MULTICAST_TTL: 1831df8bae1dSRodney W. Grimes /* 1832df8bae1dSRodney W. Grimes * Set the IP time-to-live for outgoing multicast packets. 1833cfe8b629SGarrett Wollman * The original multicast API required a char argument, 1834cfe8b629SGarrett Wollman * which is inconsistent with the rest of the socket API. 1835cfe8b629SGarrett Wollman * We allow either a char or an int. 1836df8bae1dSRodney W. Grimes */ 1837cfe8b629SGarrett Wollman if (sopt->sopt_valsize == 1) { 1838cfe8b629SGarrett Wollman u_char ttl; 1839cfe8b629SGarrett Wollman error = sooptcopyin(sopt, &ttl, 1, 1); 1840cfe8b629SGarrett Wollman if (error) 1841df8bae1dSRodney W. Grimes break; 1842cfe8b629SGarrett Wollman imo->imo_multicast_ttl = ttl; 1843cfe8b629SGarrett Wollman } else { 1844cfe8b629SGarrett Wollman u_int ttl; 1845cfe8b629SGarrett Wollman error = sooptcopyin(sopt, &ttl, sizeof ttl, 1846cfe8b629SGarrett Wollman sizeof ttl); 1847cfe8b629SGarrett Wollman if (error) 1848cfe8b629SGarrett Wollman break; 1849cfe8b629SGarrett Wollman if (ttl > 255) 1850cfe8b629SGarrett Wollman error = EINVAL; 1851cfe8b629SGarrett Wollman else 1852cfe8b629SGarrett Wollman imo->imo_multicast_ttl = ttl; 1853df8bae1dSRodney W. Grimes } 1854df8bae1dSRodney W. Grimes break; 1855df8bae1dSRodney W. Grimes 1856df8bae1dSRodney W. Grimes case IP_MULTICAST_LOOP: 1857df8bae1dSRodney W. Grimes /* 1858df8bae1dSRodney W. Grimes * Set the loopback flag for outgoing multicast packets. 1859cfe8b629SGarrett Wollman * Must be zero or one. The original multicast API required a 1860cfe8b629SGarrett Wollman * char argument, which is inconsistent with the rest 1861cfe8b629SGarrett Wollman * of the socket API. We allow either a char or an int. 1862df8bae1dSRodney W. Grimes */ 1863cfe8b629SGarrett Wollman if (sopt->sopt_valsize == 1) { 1864cfe8b629SGarrett Wollman u_char loop; 1865cfe8b629SGarrett Wollman error = sooptcopyin(sopt, &loop, 1, 1); 1866cfe8b629SGarrett Wollman if (error) 1867df8bae1dSRodney W. Grimes break; 1868cfe8b629SGarrett Wollman imo->imo_multicast_loop = !!loop; 1869cfe8b629SGarrett Wollman } else { 1870cfe8b629SGarrett Wollman u_int loop; 1871cfe8b629SGarrett Wollman error = sooptcopyin(sopt, &loop, sizeof loop, 1872cfe8b629SGarrett Wollman sizeof loop); 1873cfe8b629SGarrett Wollman if (error) 1874cfe8b629SGarrett Wollman break; 1875cfe8b629SGarrett Wollman imo->imo_multicast_loop = !!loop; 1876df8bae1dSRodney W. Grimes } 1877df8bae1dSRodney W. Grimes break; 1878df8bae1dSRodney W. Grimes 1879df8bae1dSRodney W. Grimes case IP_ADD_MEMBERSHIP: 1880df8bae1dSRodney W. Grimes /* 1881df8bae1dSRodney W. Grimes * Add a multicast group membership. 1882df8bae1dSRodney W. Grimes * Group must be a valid IP multicast address. 1883df8bae1dSRodney W. Grimes */ 1884cfe8b629SGarrett Wollman error = sooptcopyin(sopt, &mreq, sizeof mreq, sizeof mreq); 1885cfe8b629SGarrett Wollman if (error) 1886df8bae1dSRodney W. Grimes break; 1887cfe8b629SGarrett Wollman 1888cfe8b629SGarrett Wollman if (!IN_MULTICAST(ntohl(mreq.imr_multiaddr.s_addr))) { 1889df8bae1dSRodney W. Grimes error = EINVAL; 1890df8bae1dSRodney W. Grimes break; 1891df8bae1dSRodney W. Grimes } 189220e8807cSGarrett Wollman s = splimp(); 1893df8bae1dSRodney W. Grimes /* 1894df8bae1dSRodney W. Grimes * If no interface address was provided, use the interface of 1895df8bae1dSRodney W. Grimes * the route to the given multicast address. 1896df8bae1dSRodney W. Grimes */ 1897cfe8b629SGarrett Wollman if (mreq.imr_interface.s_addr == INADDR_ANY) { 18981c5de19aSGarrett Wollman bzero((caddr_t)&ro, sizeof(ro)); 1899df8bae1dSRodney W. Grimes dst = (struct sockaddr_in *)&ro.ro_dst; 1900df8bae1dSRodney W. Grimes dst->sin_len = sizeof(*dst); 1901df8bae1dSRodney W. Grimes dst->sin_family = AF_INET; 1902cfe8b629SGarrett Wollman dst->sin_addr = mreq.imr_multiaddr; 1903df8bae1dSRodney W. Grimes rtalloc(&ro); 1904df8bae1dSRodney W. Grimes if (ro.ro_rt == NULL) { 1905df8bae1dSRodney W. Grimes error = EADDRNOTAVAIL; 19069b626c29SGarrett Wollman splx(s); 1907df8bae1dSRodney W. Grimes break; 1908df8bae1dSRodney W. Grimes } 1909df8bae1dSRodney W. Grimes ifp = ro.ro_rt->rt_ifp; 1910df8bae1dSRodney W. Grimes rtfree(ro.ro_rt); 1911df8bae1dSRodney W. Grimes } 1912df8bae1dSRodney W. Grimes else { 191333841545SHajimu UMEMOTO ifp = ip_multicast_if(&mreq.imr_interface, NULL); 1914df8bae1dSRodney W. Grimes } 19159b626c29SGarrett Wollman 1916df8bae1dSRodney W. Grimes /* 1917df8bae1dSRodney W. Grimes * See if we found an interface, and confirm that it 1918df8bae1dSRodney W. Grimes * supports multicast. 1919df8bae1dSRodney W. Grimes */ 1920df8bae1dSRodney W. Grimes if (ifp == NULL || (ifp->if_flags & IFF_MULTICAST) == 0) { 1921df8bae1dSRodney W. Grimes error = EADDRNOTAVAIL; 19229b626c29SGarrett Wollman splx(s); 1923df8bae1dSRodney W. Grimes break; 1924df8bae1dSRodney W. Grimes } 1925df8bae1dSRodney W. Grimes /* 1926df8bae1dSRodney W. Grimes * See if the membership already exists or if all the 1927df8bae1dSRodney W. Grimes * membership slots are full. 1928df8bae1dSRodney W. Grimes */ 1929df8bae1dSRodney W. Grimes for (i = 0; i < imo->imo_num_memberships; ++i) { 1930df8bae1dSRodney W. Grimes if (imo->imo_membership[i]->inm_ifp == ifp && 1931df8bae1dSRodney W. Grimes imo->imo_membership[i]->inm_addr.s_addr 1932cfe8b629SGarrett Wollman == mreq.imr_multiaddr.s_addr) 1933df8bae1dSRodney W. Grimes break; 1934df8bae1dSRodney W. Grimes } 1935df8bae1dSRodney W. Grimes if (i < imo->imo_num_memberships) { 1936df8bae1dSRodney W. Grimes error = EADDRINUSE; 19379b626c29SGarrett Wollman splx(s); 1938df8bae1dSRodney W. Grimes break; 1939df8bae1dSRodney W. Grimes } 1940df8bae1dSRodney W. Grimes if (i == IP_MAX_MEMBERSHIPS) { 1941df8bae1dSRodney W. Grimes error = ETOOMANYREFS; 19429b626c29SGarrett Wollman splx(s); 1943df8bae1dSRodney W. Grimes break; 1944df8bae1dSRodney W. Grimes } 1945df8bae1dSRodney W. Grimes /* 1946df8bae1dSRodney W. Grimes * Everything looks good; add a new record to the multicast 1947df8bae1dSRodney W. Grimes * address list for the given interface. 1948df8bae1dSRodney W. Grimes */ 1949df8bae1dSRodney W. Grimes if ((imo->imo_membership[i] = 1950cfe8b629SGarrett Wollman in_addmulti(&mreq.imr_multiaddr, ifp)) == NULL) { 1951df8bae1dSRodney W. Grimes error = ENOBUFS; 19529b626c29SGarrett Wollman splx(s); 1953df8bae1dSRodney W. Grimes break; 1954df8bae1dSRodney W. Grimes } 1955df8bae1dSRodney W. Grimes ++imo->imo_num_memberships; 19569b626c29SGarrett Wollman splx(s); 1957df8bae1dSRodney W. Grimes break; 1958df8bae1dSRodney W. Grimes 1959df8bae1dSRodney W. Grimes case IP_DROP_MEMBERSHIP: 1960df8bae1dSRodney W. Grimes /* 1961df8bae1dSRodney W. Grimes * Drop a multicast group membership. 1962df8bae1dSRodney W. Grimes * Group must be a valid IP multicast address. 1963df8bae1dSRodney W. Grimes */ 1964cfe8b629SGarrett Wollman error = sooptcopyin(sopt, &mreq, sizeof mreq, sizeof mreq); 1965cfe8b629SGarrett Wollman if (error) 1966df8bae1dSRodney W. Grimes break; 1967cfe8b629SGarrett Wollman 1968cfe8b629SGarrett Wollman if (!IN_MULTICAST(ntohl(mreq.imr_multiaddr.s_addr))) { 1969df8bae1dSRodney W. Grimes error = EINVAL; 1970df8bae1dSRodney W. Grimes break; 1971df8bae1dSRodney W. Grimes } 19729b626c29SGarrett Wollman 197320e8807cSGarrett Wollman s = splimp(); 1974df8bae1dSRodney W. Grimes /* 1975df8bae1dSRodney W. Grimes * If an interface address was specified, get a pointer 1976df8bae1dSRodney W. Grimes * to its ifnet structure. 1977df8bae1dSRodney W. Grimes */ 1978cfe8b629SGarrett Wollman if (mreq.imr_interface.s_addr == INADDR_ANY) 1979df8bae1dSRodney W. Grimes ifp = NULL; 1980df8bae1dSRodney W. Grimes else { 198133841545SHajimu UMEMOTO ifp = ip_multicast_if(&mreq.imr_interface, NULL); 1982df8bae1dSRodney W. Grimes if (ifp == NULL) { 1983df8bae1dSRodney W. Grimes error = EADDRNOTAVAIL; 19849b626c29SGarrett Wollman splx(s); 1985df8bae1dSRodney W. Grimes break; 1986df8bae1dSRodney W. Grimes } 1987df8bae1dSRodney W. Grimes } 1988df8bae1dSRodney W. Grimes /* 1989df8bae1dSRodney W. Grimes * Find the membership in the membership array. 1990df8bae1dSRodney W. Grimes */ 1991df8bae1dSRodney W. Grimes for (i = 0; i < imo->imo_num_memberships; ++i) { 1992df8bae1dSRodney W. Grimes if ((ifp == NULL || 1993df8bae1dSRodney W. Grimes imo->imo_membership[i]->inm_ifp == ifp) && 1994df8bae1dSRodney W. Grimes imo->imo_membership[i]->inm_addr.s_addr == 1995cfe8b629SGarrett Wollman mreq.imr_multiaddr.s_addr) 1996df8bae1dSRodney W. Grimes break; 1997df8bae1dSRodney W. Grimes } 1998df8bae1dSRodney W. Grimes if (i == imo->imo_num_memberships) { 1999df8bae1dSRodney W. Grimes error = EADDRNOTAVAIL; 20009b626c29SGarrett Wollman splx(s); 2001df8bae1dSRodney W. Grimes break; 2002df8bae1dSRodney W. Grimes } 2003df8bae1dSRodney W. Grimes /* 2004df8bae1dSRodney W. Grimes * Give up the multicast address record to which the 2005df8bae1dSRodney W. Grimes * membership points. 2006df8bae1dSRodney W. Grimes */ 2007df8bae1dSRodney W. Grimes in_delmulti(imo->imo_membership[i]); 2008df8bae1dSRodney W. Grimes /* 2009df8bae1dSRodney W. Grimes * Remove the gap in the membership array. 2010df8bae1dSRodney W. Grimes */ 2011df8bae1dSRodney W. Grimes for (++i; i < imo->imo_num_memberships; ++i) 2012df8bae1dSRodney W. Grimes imo->imo_membership[i-1] = imo->imo_membership[i]; 2013df8bae1dSRodney W. Grimes --imo->imo_num_memberships; 20149b626c29SGarrett Wollman splx(s); 2015df8bae1dSRodney W. Grimes break; 2016df8bae1dSRodney W. Grimes 2017df8bae1dSRodney W. Grimes default: 2018df8bae1dSRodney W. Grimes error = EOPNOTSUPP; 2019df8bae1dSRodney W. Grimes break; 2020df8bae1dSRodney W. Grimes } 2021df8bae1dSRodney W. Grimes 2022df8bae1dSRodney W. Grimes /* 2023df8bae1dSRodney W. Grimes * If all options have default values, no need to keep the mbuf. 2024df8bae1dSRodney W. Grimes */ 2025df8bae1dSRodney W. Grimes if (imo->imo_multicast_ifp == NULL && 20261c5de19aSGarrett Wollman imo->imo_multicast_vif == -1 && 2027df8bae1dSRodney W. Grimes imo->imo_multicast_ttl == IP_DEFAULT_MULTICAST_TTL && 2028df8bae1dSRodney W. Grimes imo->imo_multicast_loop == IP_DEFAULT_MULTICAST_LOOP && 2029df8bae1dSRodney W. Grimes imo->imo_num_memberships == 0) { 2030df8bae1dSRodney W. Grimes free(*imop, M_IPMOPTS); 2031df8bae1dSRodney W. Grimes *imop = NULL; 2032df8bae1dSRodney W. Grimes } 2033df8bae1dSRodney W. Grimes 2034df8bae1dSRodney W. Grimes return (error); 2035df8bae1dSRodney W. Grimes } 2036df8bae1dSRodney W. Grimes 2037df8bae1dSRodney W. Grimes /* 2038df8bae1dSRodney W. Grimes * Return the IP multicast options in response to user getsockopt(). 2039df8bae1dSRodney W. Grimes */ 20400312fbe9SPoul-Henning Kamp static int 2041cfe8b629SGarrett Wollman ip_getmoptions(sopt, imo) 2042cfe8b629SGarrett Wollman struct sockopt *sopt; 2043df8bae1dSRodney W. Grimes register struct ip_moptions *imo; 2044df8bae1dSRodney W. Grimes { 2045cfe8b629SGarrett Wollman struct in_addr addr; 2046df8bae1dSRodney W. Grimes struct in_ifaddr *ia; 2047cfe8b629SGarrett Wollman int error, optval; 2048cfe8b629SGarrett Wollman u_char coptval; 2049df8bae1dSRodney W. Grimes 2050cfe8b629SGarrett Wollman error = 0; 2051cfe8b629SGarrett Wollman switch (sopt->sopt_name) { 2052f0068c4aSGarrett Wollman case IP_MULTICAST_VIF: 2053f0068c4aSGarrett Wollman if (imo != NULL) 2054cfe8b629SGarrett Wollman optval = imo->imo_multicast_vif; 2055f0068c4aSGarrett Wollman else 2056cfe8b629SGarrett Wollman optval = -1; 2057cfe8b629SGarrett Wollman error = sooptcopyout(sopt, &optval, sizeof optval); 2058cfe8b629SGarrett Wollman break; 2059f0068c4aSGarrett Wollman 2060df8bae1dSRodney W. Grimes case IP_MULTICAST_IF: 2061df8bae1dSRodney W. Grimes if (imo == NULL || imo->imo_multicast_ifp == NULL) 2062cfe8b629SGarrett Wollman addr.s_addr = INADDR_ANY; 206333841545SHajimu UMEMOTO else if (imo->imo_multicast_addr.s_addr) { 206433841545SHajimu UMEMOTO /* return the value user has set */ 206533841545SHajimu UMEMOTO addr = imo->imo_multicast_addr; 206633841545SHajimu UMEMOTO } else { 2067df8bae1dSRodney W. Grimes IFP_TO_IA(imo->imo_multicast_ifp, ia); 2068cfe8b629SGarrett Wollman addr.s_addr = (ia == NULL) ? INADDR_ANY 2069df8bae1dSRodney W. Grimes : IA_SIN(ia)->sin_addr.s_addr; 2070df8bae1dSRodney W. Grimes } 2071cfe8b629SGarrett Wollman error = sooptcopyout(sopt, &addr, sizeof addr); 2072cfe8b629SGarrett Wollman break; 2073df8bae1dSRodney W. Grimes 2074df8bae1dSRodney W. Grimes case IP_MULTICAST_TTL: 2075cfe8b629SGarrett Wollman if (imo == 0) 2076cfe8b629SGarrett Wollman optval = coptval = IP_DEFAULT_MULTICAST_TTL; 2077cfe8b629SGarrett Wollman else 2078cfe8b629SGarrett Wollman optval = coptval = imo->imo_multicast_ttl; 2079cfe8b629SGarrett Wollman if (sopt->sopt_valsize == 1) 2080cfe8b629SGarrett Wollman error = sooptcopyout(sopt, &coptval, 1); 2081cfe8b629SGarrett Wollman else 2082cfe8b629SGarrett Wollman error = sooptcopyout(sopt, &optval, sizeof optval); 2083cfe8b629SGarrett Wollman break; 2084df8bae1dSRodney W. Grimes 2085df8bae1dSRodney W. Grimes case IP_MULTICAST_LOOP: 2086cfe8b629SGarrett Wollman if (imo == 0) 2087cfe8b629SGarrett Wollman optval = coptval = IP_DEFAULT_MULTICAST_LOOP; 2088cfe8b629SGarrett Wollman else 2089cfe8b629SGarrett Wollman optval = coptval = imo->imo_multicast_loop; 2090cfe8b629SGarrett Wollman if (sopt->sopt_valsize == 1) 2091cfe8b629SGarrett Wollman error = sooptcopyout(sopt, &coptval, 1); 2092cfe8b629SGarrett Wollman else 2093cfe8b629SGarrett Wollman error = sooptcopyout(sopt, &optval, sizeof optval); 2094cfe8b629SGarrett Wollman break; 2095df8bae1dSRodney W. Grimes 2096df8bae1dSRodney W. Grimes default: 2097cfe8b629SGarrett Wollman error = ENOPROTOOPT; 2098cfe8b629SGarrett Wollman break; 2099df8bae1dSRodney W. Grimes } 2100cfe8b629SGarrett Wollman return (error); 2101df8bae1dSRodney W. Grimes } 2102df8bae1dSRodney W. Grimes 2103df8bae1dSRodney W. Grimes /* 2104df8bae1dSRodney W. Grimes * Discard the IP multicast options. 2105df8bae1dSRodney W. Grimes */ 2106df8bae1dSRodney W. Grimes void 2107df8bae1dSRodney W. Grimes ip_freemoptions(imo) 2108df8bae1dSRodney W. Grimes register struct ip_moptions *imo; 2109df8bae1dSRodney W. Grimes { 2110df8bae1dSRodney W. Grimes register int i; 2111df8bae1dSRodney W. Grimes 2112df8bae1dSRodney W. Grimes if (imo != NULL) { 2113df8bae1dSRodney W. Grimes for (i = 0; i < imo->imo_num_memberships; ++i) 2114df8bae1dSRodney W. Grimes in_delmulti(imo->imo_membership[i]); 2115df8bae1dSRodney W. Grimes free(imo, M_IPMOPTS); 2116df8bae1dSRodney W. Grimes } 2117df8bae1dSRodney W. Grimes } 2118df8bae1dSRodney W. Grimes 2119df8bae1dSRodney W. Grimes /* 2120df8bae1dSRodney W. Grimes * Routine called from ip_output() to loop back a copy of an IP multicast 2121df8bae1dSRodney W. Grimes * packet to the input queue of a specified interface. Note that this 2122df8bae1dSRodney W. Grimes * calls the output routine of the loopback "driver", but with an interface 2123f5fea3ddSPaul Traina * pointer that might NOT be a loopback interface -- evil, but easier than 2124f5fea3ddSPaul Traina * replicating that code here. 2125df8bae1dSRodney W. Grimes */ 2126df8bae1dSRodney W. Grimes static void 212786b1d6d2SBill Fenner ip_mloopback(ifp, m, dst, hlen) 2128df8bae1dSRodney W. Grimes struct ifnet *ifp; 2129df8bae1dSRodney W. Grimes register struct mbuf *m; 2130df8bae1dSRodney W. Grimes register struct sockaddr_in *dst; 213186b1d6d2SBill Fenner int hlen; 2132df8bae1dSRodney W. Grimes { 2133df8bae1dSRodney W. Grimes register struct ip *ip; 2134df8bae1dSRodney W. Grimes struct mbuf *copym; 2135df8bae1dSRodney W. Grimes 2136df8bae1dSRodney W. Grimes copym = m_copy(m, 0, M_COPYALL); 213786b1d6d2SBill Fenner if (copym != NULL && (copym->m_flags & M_EXT || copym->m_len < hlen)) 213886b1d6d2SBill Fenner copym = m_pullup(copym, hlen); 2139df8bae1dSRodney W. Grimes if (copym != NULL) { 2140df8bae1dSRodney W. Grimes /* 2141df8bae1dSRodney W. Grimes * We don't bother to fragment if the IP length is greater 2142df8bae1dSRodney W. Grimes * than the interface's MTU. Can this possibly matter? 2143df8bae1dSRodney W. Grimes */ 2144df8bae1dSRodney W. Grimes ip = mtod(copym, struct ip *); 2145fd8e4ebcSMike Barcroft ip->ip_len = htons(ip->ip_len); 2146fd8e4ebcSMike Barcroft ip->ip_off = htons(ip->ip_off); 2147df8bae1dSRodney W. Grimes ip->ip_sum = 0; 214886b1d6d2SBill Fenner ip->ip_sum = in_cksum(copym, hlen); 21499c9137eaSGarrett Wollman /* 21509c9137eaSGarrett Wollman * NB: 2151e1596dffSBill Fenner * It's not clear whether there are any lingering 2152e1596dffSBill Fenner * reentrancy problems in other areas which might 2153e1596dffSBill Fenner * be exposed by using ip_input directly (in 2154e1596dffSBill Fenner * particular, everything which modifies the packet 2155e1596dffSBill Fenner * in-place). Yet another option is using the 2156e1596dffSBill Fenner * protosw directly to deliver the looped back 2157e1596dffSBill Fenner * packet. For the moment, we'll err on the side 2158ed7509acSJulian Elischer * of safety by using if_simloop(). 21599c9137eaSGarrett Wollman */ 2160201c2527SJulian Elischer #if 1 /* XXX */ 2161201c2527SJulian Elischer if (dst->sin_family != AF_INET) { 21622b8a366cSJulian Elischer printf("ip_mloopback: bad address family %d\n", 2163201c2527SJulian Elischer dst->sin_family); 2164201c2527SJulian Elischer dst->sin_family = AF_INET; 2165201c2527SJulian Elischer } 2166201c2527SJulian Elischer #endif 2167201c2527SJulian Elischer 21689c9137eaSGarrett Wollman #ifdef notdef 2169e1596dffSBill Fenner copym->m_pkthdr.rcvif = ifp; 2170ed7509acSJulian Elischer ip_input(copym); 21719c9137eaSGarrett Wollman #else 217250c6dc99SJonathan Lemon /* if the checksum hasn't been computed, mark it as valid */ 217350c6dc99SJonathan Lemon if (copym->m_pkthdr.csum_flags & CSUM_DELAY_DATA) { 217450c6dc99SJonathan Lemon copym->m_pkthdr.csum_flags |= 217550c6dc99SJonathan Lemon CSUM_DATA_VALID | CSUM_PSEUDO_HDR; 217650c6dc99SJonathan Lemon copym->m_pkthdr.csum_data = 0xffff; 217750c6dc99SJonathan Lemon } 217806a429a3SArchie Cobbs if_simloop(ifp, copym, dst->sin_family, 0); 21799c9137eaSGarrett Wollman #endif 2180df8bae1dSRodney W. Grimes } 2181df8bae1dSRodney W. Grimes } 2182