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 = 328bbb4330bSLuigi Rizzo ip_mcast_src ? 329bbb4330bSLuigi Rizzo ip_mcast_src(imo->imo_multicast_vif) : 330bbb4330bSLuigi Rizzo INADDR_ANY; 331df8bae1dSRodney W. Grimes } else 332df8bae1dSRodney W. Grimes ip->ip_ttl = IP_DEFAULT_MULTICAST_TTL; 333df8bae1dSRodney W. Grimes /* 334df8bae1dSRodney W. Grimes * Confirm that the outgoing interface supports multicast. 335df8bae1dSRodney W. Grimes */ 3361c5de19aSGarrett Wollman if ((imo == NULL) || (imo->imo_multicast_vif == -1)) { 337df8bae1dSRodney W. Grimes if ((ifp->if_flags & IFF_MULTICAST) == 0) { 338df8bae1dSRodney W. Grimes ipstat.ips_noroute++; 339df8bae1dSRodney W. Grimes error = ENETUNREACH; 340df8bae1dSRodney W. Grimes goto bad; 341df8bae1dSRodney W. Grimes } 3421c5de19aSGarrett Wollman } 343df8bae1dSRodney W. Grimes /* 344df8bae1dSRodney W. Grimes * If source address not specified yet, use address 345df8bae1dSRodney W. Grimes * of outgoing interface. 346df8bae1dSRodney W. Grimes */ 347df8bae1dSRodney W. Grimes if (ip->ip_src.s_addr == INADDR_ANY) { 34838c1bc35SRuslan Ermilov /* Interface may have no addresses. */ 34938c1bc35SRuslan Ermilov if (ia != NULL) 35038c1bc35SRuslan Ermilov ip->ip_src = IA_SIN(ia)->sin_addr; 351df8bae1dSRodney W. Grimes } 352df8bae1dSRodney W. Grimes 35392bdb2faSBill Fenner if (ip_mrouter && (flags & IP_FORWARDING) == 0) { 35492bdb2faSBill Fenner /* 35592bdb2faSBill Fenner * XXX 35692bdb2faSBill Fenner * delayed checksums are not currently 35792bdb2faSBill Fenner * compatible with IP multicast routing 35892bdb2faSBill Fenner */ 35992bdb2faSBill Fenner if (m->m_pkthdr.csum_flags & CSUM_DELAY_DATA) { 36092bdb2faSBill Fenner in_delayed_cksum(m); 36192bdb2faSBill Fenner m->m_pkthdr.csum_flags &= 36292bdb2faSBill Fenner ~CSUM_DELAY_DATA; 36392bdb2faSBill Fenner } 36492bdb2faSBill Fenner } 3653efc3014SJulian Elischer IN_LOOKUP_MULTI(pkt_dst, ifp, inm); 366df8bae1dSRodney W. Grimes if (inm != NULL && 367df8bae1dSRodney W. Grimes (imo == NULL || imo->imo_multicast_loop)) { 368df8bae1dSRodney W. Grimes /* 369df8bae1dSRodney W. Grimes * If we belong to the destination multicast group 370df8bae1dSRodney W. Grimes * on the outgoing interface, and the caller did not 371df8bae1dSRodney W. Grimes * forbid loopback, loop back a copy. 372df8bae1dSRodney W. Grimes */ 37386b1d6d2SBill Fenner ip_mloopback(ifp, m, dst, hlen); 374df8bae1dSRodney W. Grimes } 375df8bae1dSRodney W. Grimes else { 376df8bae1dSRodney W. Grimes /* 377df8bae1dSRodney W. Grimes * If we are acting as a multicast router, perform 378df8bae1dSRodney W. Grimes * multicast forwarding as if the packet had just 379df8bae1dSRodney W. Grimes * arrived on the interface to which we are about 380df8bae1dSRodney W. Grimes * to send. The multicast forwarding function 381df8bae1dSRodney W. Grimes * recursively calls this function, using the 382df8bae1dSRodney W. Grimes * IP_FORWARDING flag to prevent infinite recursion. 383df8bae1dSRodney W. Grimes * 384df8bae1dSRodney W. Grimes * Multicasts that are looped back by ip_mloopback(), 385df8bae1dSRodney W. Grimes * above, will be forwarded by the ip_input() routine, 386df8bae1dSRodney W. Grimes * if necessary. 387df8bae1dSRodney W. Grimes */ 388df8bae1dSRodney W. Grimes if (ip_mrouter && (flags & IP_FORWARDING) == 0) { 389f0068c4aSGarrett Wollman /* 390bbb4330bSLuigi Rizzo * If rsvp daemon is not running, do not 391f0068c4aSGarrett Wollman * set ip_moptions. This ensures that the packet 392f0068c4aSGarrett Wollman * is multicast and not just sent down one link 393f0068c4aSGarrett Wollman * as prescribed by rsvpd. 394f0068c4aSGarrett Wollman */ 395b124e4f2SGarrett Wollman if (!rsvp_on) 396f0068c4aSGarrett Wollman imo = NULL; 397bbb4330bSLuigi Rizzo if (ip_mforward && 398bbb4330bSLuigi Rizzo ip_mforward(ip, ifp, m, imo) != 0) { 399df8bae1dSRodney W. Grimes m_freem(m); 400df8bae1dSRodney W. Grimes goto done; 401df8bae1dSRodney W. Grimes } 402df8bae1dSRodney W. Grimes } 403df8bae1dSRodney W. Grimes } 4045e9ae478SGarrett Wollman 405df8bae1dSRodney W. Grimes /* 406df8bae1dSRodney W. Grimes * Multicasts with a time-to-live of zero may be looped- 407df8bae1dSRodney W. Grimes * back, above, but must not be transmitted on a network. 408df8bae1dSRodney W. Grimes * Also, multicasts addressed to the loopback interface 409df8bae1dSRodney W. Grimes * are not sent -- the above call to ip_mloopback() will 410df8bae1dSRodney W. Grimes * loop back a copy if this host actually belongs to the 411df8bae1dSRodney W. Grimes * destination group on the loopback interface. 412df8bae1dSRodney W. Grimes */ 413f5fea3ddSPaul Traina if (ip->ip_ttl == 0 || ifp->if_flags & IFF_LOOPBACK) { 414df8bae1dSRodney W. Grimes m_freem(m); 415df8bae1dSRodney W. Grimes goto done; 416df8bae1dSRodney W. Grimes } 417df8bae1dSRodney W. Grimes 418df8bae1dSRodney W. Grimes goto sendit; 419df8bae1dSRodney W. Grimes } 420df8bae1dSRodney W. Grimes #ifndef notdef 421df8bae1dSRodney W. Grimes /* 4222b25acc1SLuigi Rizzo * If the source address is not specified yet, use the address 4232b25acc1SLuigi Rizzo * of the outoing interface. In case, keep note we did that, so 4242b25acc1SLuigi Rizzo * if the the firewall changes the next-hop causing the output 4252b25acc1SLuigi Rizzo * interface to change, we can fix that. 426df8bae1dSRodney W. Grimes */ 427f9e354dfSJulian Elischer if (ip->ip_src.s_addr == INADDR_ANY) { 42838c1bc35SRuslan Ermilov /* Interface may have no addresses. */ 42938c1bc35SRuslan Ermilov if (ia != NULL) { 430df8bae1dSRodney W. Grimes ip->ip_src = IA_SIN(ia)->sin_addr; 4312b25acc1SLuigi Rizzo src_was_INADDR_ANY = 1; 432f9e354dfSJulian Elischer } 43338c1bc35SRuslan Ermilov } 434f9e354dfSJulian Elischer #endif /* notdef */ 435df8bae1dSRodney W. Grimes /* 436b5390296SDavid Greenman * Verify that we have any chance at all of being able to queue 437b5390296SDavid Greenman * the packet or packet fragments 438b5390296SDavid Greenman */ 439b5390296SDavid Greenman if ((ifp->if_snd.ifq_len + ip->ip_len / ifp->if_mtu + 1) >= 440b5390296SDavid Greenman ifp->if_snd.ifq_maxlen) { 441b5390296SDavid Greenman error = ENOBUFS; 44235609d45SJonathan Lemon ipstat.ips_odropped++; 443b5390296SDavid Greenman goto bad; 444b5390296SDavid Greenman } 445b5390296SDavid Greenman 446b5390296SDavid Greenman /* 447df8bae1dSRodney W. Grimes * Look for broadcast address and 4488c3f5566SRuslan Ermilov * verify user is allowed to send 449df8bae1dSRodney W. Grimes * such a packet. 450df8bae1dSRodney W. Grimes */ 4519f9b3dc4SGarrett Wollman if (isbroadcast) { 452df8bae1dSRodney W. Grimes if ((ifp->if_flags & IFF_BROADCAST) == 0) { 453df8bae1dSRodney W. Grimes error = EADDRNOTAVAIL; 454df8bae1dSRodney W. Grimes goto bad; 455df8bae1dSRodney W. Grimes } 456df8bae1dSRodney W. Grimes if ((flags & IP_ALLOWBROADCAST) == 0) { 457df8bae1dSRodney W. Grimes error = EACCES; 458df8bae1dSRodney W. Grimes goto bad; 459df8bae1dSRodney W. Grimes } 460df8bae1dSRodney W. Grimes /* don't allow broadcast messages to be fragmented */ 461df8bae1dSRodney W. Grimes if ((u_short)ip->ip_len > ifp->if_mtu) { 462df8bae1dSRodney W. Grimes error = EMSGSIZE; 463df8bae1dSRodney W. Grimes goto bad; 464df8bae1dSRodney W. Grimes } 465df8bae1dSRodney W. Grimes m->m_flags |= M_BCAST; 4669f9b3dc4SGarrett Wollman } else { 467df8bae1dSRodney W. Grimes m->m_flags &= ~M_BCAST; 4689f9b3dc4SGarrett Wollman } 469df8bae1dSRodney W. Grimes 470f1743588SDarren Reed sendit: 47133841545SHajimu UMEMOTO #ifdef IPSEC 47233841545SHajimu UMEMOTO /* get SP for this packet */ 47333841545SHajimu UMEMOTO if (so == NULL) 47433841545SHajimu UMEMOTO sp = ipsec4_getpolicybyaddr(m, IPSEC_DIR_OUTBOUND, flags, &error); 47533841545SHajimu UMEMOTO else 47633841545SHajimu UMEMOTO sp = ipsec4_getpolicybysock(m, IPSEC_DIR_OUTBOUND, so, &error); 47733841545SHajimu UMEMOTO 47833841545SHajimu UMEMOTO if (sp == NULL) { 47933841545SHajimu UMEMOTO ipsecstat.out_inval++; 48033841545SHajimu UMEMOTO goto bad; 48133841545SHajimu UMEMOTO } 48233841545SHajimu UMEMOTO 48333841545SHajimu UMEMOTO error = 0; 48433841545SHajimu UMEMOTO 48533841545SHajimu UMEMOTO /* check policy */ 48633841545SHajimu UMEMOTO switch (sp->policy) { 48733841545SHajimu UMEMOTO case IPSEC_POLICY_DISCARD: 48833841545SHajimu UMEMOTO /* 48933841545SHajimu UMEMOTO * This packet is just discarded. 49033841545SHajimu UMEMOTO */ 49133841545SHajimu UMEMOTO ipsecstat.out_polvio++; 49233841545SHajimu UMEMOTO goto bad; 49333841545SHajimu UMEMOTO 49433841545SHajimu UMEMOTO case IPSEC_POLICY_BYPASS: 49533841545SHajimu UMEMOTO case IPSEC_POLICY_NONE: 49633841545SHajimu UMEMOTO /* no need to do IPsec. */ 49733841545SHajimu UMEMOTO goto skip_ipsec; 49833841545SHajimu UMEMOTO 49933841545SHajimu UMEMOTO case IPSEC_POLICY_IPSEC: 50033841545SHajimu UMEMOTO if (sp->req == NULL) { 50133841545SHajimu UMEMOTO /* acquire a policy */ 50233841545SHajimu UMEMOTO error = key_spdacquire(sp); 50333841545SHajimu UMEMOTO goto bad; 50433841545SHajimu UMEMOTO } 50533841545SHajimu UMEMOTO break; 50633841545SHajimu UMEMOTO 50733841545SHajimu UMEMOTO case IPSEC_POLICY_ENTRUST: 50833841545SHajimu UMEMOTO default: 50933841545SHajimu UMEMOTO printf("ip_output: Invalid policy found. %d\n", sp->policy); 51033841545SHajimu UMEMOTO } 51133841545SHajimu UMEMOTO { 51233841545SHajimu UMEMOTO struct ipsec_output_state state; 51333841545SHajimu UMEMOTO bzero(&state, sizeof(state)); 51433841545SHajimu UMEMOTO state.m = m; 51533841545SHajimu UMEMOTO if (flags & IP_ROUTETOIF) { 51633841545SHajimu UMEMOTO state.ro = &iproute; 51733841545SHajimu UMEMOTO bzero(&iproute, sizeof(iproute)); 51833841545SHajimu UMEMOTO } else 51933841545SHajimu UMEMOTO state.ro = ro; 52033841545SHajimu UMEMOTO state.dst = (struct sockaddr *)dst; 52133841545SHajimu UMEMOTO 52233841545SHajimu UMEMOTO ip->ip_sum = 0; 52333841545SHajimu UMEMOTO 52433841545SHajimu UMEMOTO /* 52533841545SHajimu UMEMOTO * XXX 52633841545SHajimu UMEMOTO * delayed checksums are not currently compatible with IPsec 52733841545SHajimu UMEMOTO */ 52833841545SHajimu UMEMOTO if (m->m_pkthdr.csum_flags & CSUM_DELAY_DATA) { 52933841545SHajimu UMEMOTO in_delayed_cksum(m); 53033841545SHajimu UMEMOTO m->m_pkthdr.csum_flags &= ~CSUM_DELAY_DATA; 53133841545SHajimu UMEMOTO } 53233841545SHajimu UMEMOTO 533fd8e4ebcSMike Barcroft ip->ip_len = htons(ip->ip_len); 534fd8e4ebcSMike Barcroft ip->ip_off = htons(ip->ip_off); 53533841545SHajimu UMEMOTO 53633841545SHajimu UMEMOTO error = ipsec4_output(&state, sp, flags); 53733841545SHajimu UMEMOTO 53833841545SHajimu UMEMOTO m = state.m; 53933841545SHajimu UMEMOTO if (flags & IP_ROUTETOIF) { 54033841545SHajimu UMEMOTO /* 54133841545SHajimu UMEMOTO * if we have tunnel mode SA, we may need to ignore 54233841545SHajimu UMEMOTO * IP_ROUTETOIF. 54333841545SHajimu UMEMOTO */ 54433841545SHajimu UMEMOTO if (state.ro != &iproute || state.ro->ro_rt != NULL) { 54533841545SHajimu UMEMOTO flags &= ~IP_ROUTETOIF; 54633841545SHajimu UMEMOTO ro = state.ro; 54733841545SHajimu UMEMOTO } 54833841545SHajimu UMEMOTO } else 54933841545SHajimu UMEMOTO ro = state.ro; 55033841545SHajimu UMEMOTO dst = (struct sockaddr_in *)state.dst; 55133841545SHajimu UMEMOTO if (error) { 55233841545SHajimu UMEMOTO /* mbuf is already reclaimed in ipsec4_output. */ 55333841545SHajimu UMEMOTO m0 = NULL; 55433841545SHajimu UMEMOTO switch (error) { 55533841545SHajimu UMEMOTO case EHOSTUNREACH: 55633841545SHajimu UMEMOTO case ENETUNREACH: 55733841545SHajimu UMEMOTO case EMSGSIZE: 55833841545SHajimu UMEMOTO case ENOBUFS: 55933841545SHajimu UMEMOTO case ENOMEM: 56033841545SHajimu UMEMOTO break; 56133841545SHajimu UMEMOTO default: 56233841545SHajimu UMEMOTO printf("ip4_output (ipsec): error code %d\n", error); 56333841545SHajimu UMEMOTO /*fall through*/ 56433841545SHajimu UMEMOTO case ENOENT: 56533841545SHajimu UMEMOTO /* don't show these error codes to the user */ 56633841545SHajimu UMEMOTO error = 0; 56733841545SHajimu UMEMOTO break; 56833841545SHajimu UMEMOTO } 56933841545SHajimu UMEMOTO goto bad; 57033841545SHajimu UMEMOTO } 57133841545SHajimu UMEMOTO } 57233841545SHajimu UMEMOTO 57333841545SHajimu UMEMOTO /* be sure to update variables that are affected by ipsec4_output() */ 57433841545SHajimu UMEMOTO ip = mtod(m, struct ip *); 57533841545SHajimu UMEMOTO hlen = ip->ip_hl << 2; 57633841545SHajimu UMEMOTO if (ro->ro_rt == NULL) { 57733841545SHajimu UMEMOTO if ((flags & IP_ROUTETOIF) == 0) { 57833841545SHajimu UMEMOTO printf("ip_output: " 57933841545SHajimu UMEMOTO "can't update route after IPsec processing\n"); 58033841545SHajimu UMEMOTO error = EHOSTUNREACH; /*XXX*/ 58133841545SHajimu UMEMOTO goto bad; 58233841545SHajimu UMEMOTO } 58333841545SHajimu UMEMOTO } else { 58433841545SHajimu UMEMOTO ia = ifatoia(ro->ro_rt->rt_ifa); 58533841545SHajimu UMEMOTO ifp = ro->ro_rt->rt_ifp; 58633841545SHajimu UMEMOTO } 58733841545SHajimu UMEMOTO 58833841545SHajimu UMEMOTO /* make it flipped, again. */ 589fd8e4ebcSMike Barcroft ip->ip_len = ntohs(ip->ip_len); 590fd8e4ebcSMike Barcroft ip->ip_off = ntohs(ip->ip_off); 59133841545SHajimu UMEMOTO skip_ipsec: 59233841545SHajimu UMEMOTO #endif /*IPSEC*/ 593b9234fafSSam Leffler #ifdef FAST_IPSEC 594b9234fafSSam Leffler /* 595b9234fafSSam Leffler * Check the security policy (SP) for the packet and, if 596b9234fafSSam Leffler * required, do IPsec-related processing. There are two 597b9234fafSSam Leffler * cases here; the first time a packet is sent through 598b9234fafSSam Leffler * it will be untagged and handled by ipsec4_checkpolicy. 599b9234fafSSam Leffler * If the packet is resubmitted to ip_output (e.g. after 600b9234fafSSam Leffler * AH, ESP, etc. processing), there will be a tag to bypass 601b9234fafSSam Leffler * the lookup and related policy checking. 602b9234fafSSam Leffler */ 603b9234fafSSam Leffler mtag = m_tag_find(m, PACKET_TAG_IPSEC_PENDING_TDB, NULL); 604b9234fafSSam Leffler s = splnet(); 605b9234fafSSam Leffler if (mtag != NULL) { 606b9234fafSSam Leffler tdbi = (struct tdb_ident *)(mtag + 1); 607b9234fafSSam Leffler sp = ipsec_getpolicy(tdbi, IPSEC_DIR_OUTBOUND); 608b9234fafSSam Leffler if (sp == NULL) 609b9234fafSSam Leffler error = -EINVAL; /* force silent drop */ 610b9234fafSSam Leffler m_tag_delete(m, mtag); 611b9234fafSSam Leffler } else { 612b9234fafSSam Leffler sp = ipsec4_checkpolicy(m, IPSEC_DIR_OUTBOUND, flags, 613b9234fafSSam Leffler &error, inp); 614b9234fafSSam Leffler } 615b9234fafSSam Leffler /* 616b9234fafSSam Leffler * There are four return cases: 617b9234fafSSam Leffler * sp != NULL apply IPsec policy 618b9234fafSSam Leffler * sp == NULL, error == 0 no IPsec handling needed 619b9234fafSSam Leffler * sp == NULL, error == -EINVAL discard packet w/o error 620b9234fafSSam Leffler * sp == NULL, error != 0 discard packet, report error 621b9234fafSSam Leffler */ 622b9234fafSSam Leffler if (sp != NULL) { 623b9234fafSSam Leffler /* Loop detection, check if ipsec processing already done */ 624b9234fafSSam Leffler KASSERT(sp->req != NULL, ("ip_output: no ipsec request")); 625b9234fafSSam Leffler for (mtag = m_tag_first(m); mtag != NULL; 626b9234fafSSam Leffler mtag = m_tag_next(m, mtag)) { 627b9234fafSSam Leffler if (mtag->m_tag_cookie != MTAG_ABI_COMPAT) 628b9234fafSSam Leffler continue; 629b9234fafSSam Leffler if (mtag->m_tag_id != PACKET_TAG_IPSEC_OUT_DONE && 630b9234fafSSam Leffler mtag->m_tag_id != PACKET_TAG_IPSEC_OUT_CRYPTO_NEEDED) 631b9234fafSSam Leffler continue; 632b9234fafSSam Leffler /* 633b9234fafSSam Leffler * Check if policy has an SA associated with it. 634b9234fafSSam Leffler * This can happen when an SP has yet to acquire 635b9234fafSSam Leffler * an SA; e.g. on first reference. If it occurs, 636b9234fafSSam Leffler * then we let ipsec4_process_packet do its thing. 637b9234fafSSam Leffler */ 638b9234fafSSam Leffler if (sp->req->sav == NULL) 639b9234fafSSam Leffler break; 640b9234fafSSam Leffler tdbi = (struct tdb_ident *)(mtag + 1); 641b9234fafSSam Leffler if (tdbi->spi == sp->req->sav->spi && 642b9234fafSSam Leffler tdbi->proto == sp->req->sav->sah->saidx.proto && 643ab94ca3cSSam Leffler bcmp(&tdbi->dst, &sp->req->sav->sah->saidx.dst, 644b9234fafSSam Leffler sizeof (union sockaddr_union)) == 0) { 645b9234fafSSam Leffler /* 646b9234fafSSam Leffler * No IPsec processing is needed, free 647b9234fafSSam Leffler * reference to SP. 648b9234fafSSam Leffler * 649b9234fafSSam Leffler * NB: null pointer to avoid free at 650b9234fafSSam Leffler * done: below. 651b9234fafSSam Leffler */ 652b9234fafSSam Leffler KEY_FREESP(&sp), sp = NULL; 653b9234fafSSam Leffler splx(s); 654b9234fafSSam Leffler goto spd_done; 655b9234fafSSam Leffler } 656b9234fafSSam Leffler } 657b9234fafSSam Leffler 658b9234fafSSam Leffler /* 659b9234fafSSam Leffler * Do delayed checksums now because we send before 660b9234fafSSam Leffler * this is done in the normal processing path. 661b9234fafSSam Leffler */ 662b9234fafSSam Leffler if (m->m_pkthdr.csum_flags & CSUM_DELAY_DATA) { 663b9234fafSSam Leffler in_delayed_cksum(m); 664b9234fafSSam Leffler m->m_pkthdr.csum_flags &= ~CSUM_DELAY_DATA; 665b9234fafSSam Leffler } 666b9234fafSSam Leffler 667b9234fafSSam Leffler ip->ip_len = htons(ip->ip_len); 668b9234fafSSam Leffler ip->ip_off = htons(ip->ip_off); 669b9234fafSSam Leffler 670b9234fafSSam Leffler /* NB: callee frees mbuf */ 671b9234fafSSam Leffler error = ipsec4_process_packet(m, sp->req, flags, 0); 672b9234fafSSam Leffler splx(s); 673b9234fafSSam Leffler goto done; 674b9234fafSSam Leffler } else { 675b9234fafSSam Leffler splx(s); 676b9234fafSSam Leffler 677b9234fafSSam Leffler if (error != 0) { 678b9234fafSSam Leffler /* 679b9234fafSSam Leffler * Hack: -EINVAL is used to signal that a packet 680b9234fafSSam Leffler * should be silently discarded. This is typically 681b9234fafSSam Leffler * because we asked key management for an SA and 682b9234fafSSam Leffler * it was delayed (e.g. kicked up to IKE). 683b9234fafSSam Leffler */ 684b9234fafSSam Leffler if (error == -EINVAL) 685b9234fafSSam Leffler error = 0; 686b9234fafSSam Leffler goto bad; 687b9234fafSSam Leffler } else { 688b9234fafSSam Leffler /* No IPsec processing for this packet. */ 689b9234fafSSam Leffler } 690b9234fafSSam Leffler #ifdef notyet 691b9234fafSSam Leffler /* 692b9234fafSSam Leffler * If deferred crypto processing is needed, check that 693b9234fafSSam Leffler * the interface supports it. 694b9234fafSSam Leffler */ 695b9234fafSSam Leffler mtag = m_tag_find(m, PACKET_TAG_IPSEC_OUT_CRYPTO_NEEDED, NULL); 696b9234fafSSam Leffler if (mtag != NULL && (ifp->if_capenable & IFCAP_IPSEC) == 0) { 697b9234fafSSam Leffler /* notify IPsec to do its own crypto */ 698b9234fafSSam Leffler ipsp_skipcrypto_unmark((struct tdb_ident *)(mtag + 1)); 699b9234fafSSam Leffler error = EHOSTUNREACH; 700b9234fafSSam Leffler goto bad; 701b9234fafSSam Leffler } 702b9234fafSSam Leffler #endif 703b9234fafSSam Leffler } 704b9234fafSSam Leffler spd_done: 705b9234fafSSam Leffler #endif /* FAST_IPSEC */ 70633841545SHajimu UMEMOTO 707fed1c7e9SSøren Schmidt /* 708fed1c7e9SSøren Schmidt * IpHack's section. 709fed1c7e9SSøren Schmidt * - Xlate: translate packet's addr/port (NAT). 710e4676ba6SJulian Elischer * - Firewall: deny/allow/etc. 711fed1c7e9SSøren Schmidt * - Wrap: fake packet's addr/port <unimpl.> 712fed1c7e9SSøren Schmidt * - Encapsulate: put it in another IP and send out. <unimp.> 713fed1c7e9SSøren Schmidt */ 714c4ac87eaSDarren Reed #ifdef PFIL_HOOKS 715c4ac87eaSDarren Reed /* 716c4ac87eaSDarren Reed * Run through list of hooks for output packets. 717c4ac87eaSDarren Reed */ 718c4ac87eaSDarren Reed m1 = m; 719c4ac87eaSDarren Reed pfh = pfil_hook_get(PFIL_OUT, &inetsw[ip_protox[IPPROTO_IP]].pr_pfh); 720fc2ffbe6SPoul-Henning Kamp for (; pfh; pfh = TAILQ_NEXT(pfh, pfil_link)) 721c4ac87eaSDarren Reed if (pfh->pfil_func) { 722c4ac87eaSDarren Reed rv = pfh->pfil_func(ip, hlen, ifp, 1, &m1); 723c4ac87eaSDarren Reed if (rv) { 724c4ac87eaSDarren Reed error = EHOSTUNREACH; 7251ee25934SPeter Wemm goto done; 7261ee25934SPeter Wemm } 727c4ac87eaSDarren Reed m = m1; 728c4ac87eaSDarren Reed if (m == NULL) 729c4ac87eaSDarren Reed goto done; 730c4ac87eaSDarren Reed ip = mtod(m, struct ip *); 731c4ac87eaSDarren Reed } 732c4ac87eaSDarren Reed #endif /* PFIL_HOOKS */ 733fed1c7e9SSøren Schmidt 734df8bae1dSRodney W. Grimes /* 735e7319babSPoul-Henning Kamp * Check with the firewall... 7363efc3014SJulian Elischer * but not if we are already being fwd'd from a firewall. 737e7319babSPoul-Henning Kamp */ 7382b25acc1SLuigi Rizzo if (fw_enable && IPFW_LOADED && !args.next_hop) { 7399de9737fSPeter Wemm struct sockaddr_in *old = dst; 740b715f178SLuigi Rizzo 7412b25acc1SLuigi Rizzo args.m = m; 7422b25acc1SLuigi Rizzo args.next_hop = dst; 7432b25acc1SLuigi Rizzo args.oif = ifp; 7442b25acc1SLuigi Rizzo off = ip_fw_chk_ptr(&args); 7452b25acc1SLuigi Rizzo m = args.m; 7462b25acc1SLuigi Rizzo dst = args.next_hop; 7472b25acc1SLuigi Rizzo 748b715f178SLuigi Rizzo /* 749b715f178SLuigi Rizzo * On return we must do the following: 750507b4b54SLuigi Rizzo * m == NULL -> drop the pkt (old interface, deprecated) 751aa1f5daaSJonathan Lemon * (off & IP_FW_PORT_DENY_FLAG) -> drop the pkt (new interface) 752b715f178SLuigi Rizzo * 1<=off<= 0xffff -> DIVERT 753aa1f5daaSJonathan Lemon * (off & IP_FW_PORT_DYNT_FLAG) -> send to a DUMMYNET pipe 754aa1f5daaSJonathan Lemon * (off & IP_FW_PORT_TEE_FLAG) -> TEE the packet 755b715f178SLuigi Rizzo * dst != old -> IPFIREWALL_FORWARD 756b715f178SLuigi Rizzo * off==0, dst==old -> accept 757aa1f5daaSJonathan Lemon * If some of the above modules are not compiled in, then 758b715f178SLuigi Rizzo * we should't have to check the corresponding condition 759b715f178SLuigi Rizzo * (because the ipfw control socket should not accept 760b715f178SLuigi Rizzo * unsupported rules), but better play safe and drop 761b715f178SLuigi Rizzo * packets in case of doubt. 762b715f178SLuigi Rizzo */ 763d60315beSLuigi Rizzo if ( (off & IP_FW_PORT_DENY_FLAG) || m == NULL) { 764507b4b54SLuigi Rizzo if (m) 765507b4b54SLuigi Rizzo m_freem(m); 766507b4b54SLuigi Rizzo error = EACCES; 767507b4b54SLuigi Rizzo goto done; 768507b4b54SLuigi Rizzo } 769d60315beSLuigi Rizzo ip = mtod(m, struct ip *); 770b715f178SLuigi Rizzo if (off == 0 && dst == old) /* common case */ 771b715f178SLuigi Rizzo goto pass; 7727b109fa4SLuigi Rizzo if (DUMMYNET_LOADED && (off & IP_FW_PORT_DYNT_FLAG) != 0) { 773b715f178SLuigi Rizzo /* 774b715f178SLuigi Rizzo * pass the pkt to dummynet. Need to include 775f0a53591SLuigi Rizzo * pipe number, m, ifp, ro, dst because these are 776b715f178SLuigi Rizzo * not recomputed in the next pass. 777b715f178SLuigi Rizzo * All other parameters have been already used and 778b715f178SLuigi Rizzo * so they are not needed anymore. 779b715f178SLuigi Rizzo * XXX note: if the ifp or ro entry are deleted 780b715f178SLuigi Rizzo * while a pkt is in dummynet, we are in trouble! 781b715f178SLuigi Rizzo */ 7822b25acc1SLuigi Rizzo args.ro = ro; 7832b25acc1SLuigi Rizzo args.dst = dst; 7842b25acc1SLuigi Rizzo args.flags = flags; 7852b25acc1SLuigi Rizzo 7862b25acc1SLuigi Rizzo error = ip_dn_io_ptr(m, off & 0xffff, DN_TO_IP_OUT, 7872b25acc1SLuigi Rizzo &args); 788b715f178SLuigi Rizzo goto done; 789b715f178SLuigi Rizzo } 790b715f178SLuigi Rizzo #ifdef IPDIVERT 7918948e4baSArchie Cobbs if (off != 0 && (off & IP_FW_PORT_DYNT_FLAG) == 0) { 7928948e4baSArchie Cobbs struct mbuf *clone = NULL; 7938948e4baSArchie Cobbs 7948948e4baSArchie Cobbs /* Clone packet if we're doing a 'tee' */ 7958948e4baSArchie Cobbs if ((off & IP_FW_PORT_TEE_FLAG) != 0) 7968948e4baSArchie Cobbs clone = m_dup(m, M_DONTWAIT); 7978948e4baSArchie Cobbs 798ea53ecd9SJonathan Lemon /* 799ea53ecd9SJonathan Lemon * XXX 800ea53ecd9SJonathan Lemon * delayed checksums are not currently compatible 801ea53ecd9SJonathan Lemon * with divert sockets. 802ea53ecd9SJonathan Lemon */ 803ea53ecd9SJonathan Lemon if (m->m_pkthdr.csum_flags & CSUM_DELAY_DATA) { 804ea53ecd9SJonathan Lemon in_delayed_cksum(m); 805ea53ecd9SJonathan Lemon m->m_pkthdr.csum_flags &= ~CSUM_DELAY_DATA; 806ea53ecd9SJonathan Lemon } 807ea53ecd9SJonathan Lemon 8088948e4baSArchie Cobbs /* Restore packet header fields to original values */ 809fd8e4ebcSMike Barcroft ip->ip_len = htons(ip->ip_len); 810fd8e4ebcSMike Barcroft ip->ip_off = htons(ip->ip_off); 8118948e4baSArchie Cobbs 8128948e4baSArchie Cobbs /* Deliver packet to divert input routine */ 8132b25acc1SLuigi Rizzo divert_packet(m, 0, off & 0xffff, args.divert_rule); 8148948e4baSArchie Cobbs 8158948e4baSArchie Cobbs /* If 'tee', continue with original packet */ 8168948e4baSArchie Cobbs if (clone != NULL) { 8178948e4baSArchie Cobbs m = clone; 8188948e4baSArchie Cobbs ip = mtod(m, struct ip *); 8198948e4baSArchie Cobbs goto pass; 8208948e4baSArchie Cobbs } 821b715f178SLuigi Rizzo goto done; 822b715f178SLuigi Rizzo } 823b715f178SLuigi Rizzo #endif 824b715f178SLuigi Rizzo 8252b25acc1SLuigi Rizzo /* IPFIREWALL_FORWARD */ 8262b25acc1SLuigi Rizzo /* 8272b25acc1SLuigi Rizzo * Check dst to make sure it is directly reachable on the 828f9e354dfSJulian Elischer * interface we previously thought it was. 829f9e354dfSJulian Elischer * If it isn't (which may be likely in some situations) we have 830f9e354dfSJulian Elischer * to re-route it (ie, find a route for the next-hop and the 831f9e354dfSJulian Elischer * associated interface) and set them here. This is nested 832f9e354dfSJulian Elischer * forwarding which in most cases is undesirable, except where 833f9e354dfSJulian Elischer * such control is nigh impossible. So we do it here. 834f9e354dfSJulian Elischer * And I'm babbling. 835f9e354dfSJulian Elischer */ 8362b25acc1SLuigi Rizzo if (off == 0 && old != dst) { /* FORWARD, dst has changed */ 8372b25acc1SLuigi Rizzo #if 0 8382b25acc1SLuigi Rizzo /* 8392b25acc1SLuigi Rizzo * XXX To improve readability, this block should be 8402b25acc1SLuigi Rizzo * changed into a function call as below: 8412b25acc1SLuigi Rizzo */ 8422b25acc1SLuigi Rizzo error = ip_ipforward(&m, &dst, &ifp); 8432b25acc1SLuigi Rizzo if (error) 8442b25acc1SLuigi Rizzo goto bad; 8452b25acc1SLuigi Rizzo if (m == NULL) /* ip_input consumed the mbuf */ 8462b25acc1SLuigi Rizzo goto done; 8472b25acc1SLuigi Rizzo #else 848f9e354dfSJulian Elischer struct in_ifaddr *ia; 849f9e354dfSJulian Elischer 8502b25acc1SLuigi Rizzo /* 8512b25acc1SLuigi Rizzo * XXX sro_fwd below is static, and a pointer 8522b25acc1SLuigi Rizzo * to it gets passed to routines downstream. 8532b25acc1SLuigi Rizzo * This could have surprisingly bad results in 8542b25acc1SLuigi Rizzo * practice, because its content is overwritten 8552b25acc1SLuigi Rizzo * by subsequent packets. 8562b25acc1SLuigi Rizzo */ 857f9e354dfSJulian Elischer /* There must be a better way to do this next line... */ 8582b25acc1SLuigi Rizzo static struct route sro_fwd; 8592b25acc1SLuigi Rizzo struct route *ro_fwd = &sro_fwd; 8602b25acc1SLuigi Rizzo 8612b25acc1SLuigi Rizzo #if 0 8622b25acc1SLuigi Rizzo print_ip("IPFIREWALL_FORWARD: New dst ip: ", 8632b25acc1SLuigi Rizzo dst->sin_addr, "\n"); 864f9e354dfSJulian Elischer #endif 8652b25acc1SLuigi Rizzo 866f9e354dfSJulian Elischer /* 867f9e354dfSJulian Elischer * We need to figure out if we have been forwarded 8682b25acc1SLuigi Rizzo * to a local socket. If so, then we should somehow 869f9e354dfSJulian Elischer * "loop back" to ip_input, and get directed to the 870f9e354dfSJulian Elischer * PCB as if we had received this packet. This is 871f9e354dfSJulian Elischer * because it may be dificult to identify the packets 872f9e354dfSJulian Elischer * you want to forward until they are being output 873f9e354dfSJulian Elischer * and have selected an interface. (e.g. locally 874f9e354dfSJulian Elischer * initiated packets) If we used the loopback inteface, 875f9e354dfSJulian Elischer * we would not be able to control what happens 876f9e354dfSJulian Elischer * as the packet runs through ip_input() as 877f9e354dfSJulian Elischer * it is done through a ISR. 878f9e354dfSJulian Elischer */ 879ca925d9cSJonathan Lemon LIST_FOREACH(ia, 880ca925d9cSJonathan Lemon INADDR_HASH(dst->sin_addr.s_addr), ia_hash) { 881f9e354dfSJulian Elischer /* 882f9e354dfSJulian Elischer * If the addr to forward to is one 883f9e354dfSJulian Elischer * of ours, we pretend to 884f9e354dfSJulian Elischer * be the destination for this packet. 885f9e354dfSJulian Elischer */ 886f9e354dfSJulian Elischer if (IA_SIN(ia)->sin_addr.s_addr == 887f9e354dfSJulian Elischer dst->sin_addr.s_addr) 888f9e354dfSJulian Elischer break; 889f9e354dfSJulian Elischer } 8902b25acc1SLuigi Rizzo if (ia) { /* tell ip_input "dont filter" */ 8912b25acc1SLuigi Rizzo struct m_hdr tag; 8922b25acc1SLuigi Rizzo 8932b25acc1SLuigi Rizzo tag.mh_type = MT_TAG; 8942b25acc1SLuigi Rizzo tag.mh_flags = PACKET_TAG_IPFORWARD; 8952b25acc1SLuigi Rizzo tag.mh_data = (caddr_t)args.next_hop; 8962b25acc1SLuigi Rizzo tag.mh_next = m; 8972b25acc1SLuigi Rizzo 898f9e354dfSJulian Elischer if (m->m_pkthdr.rcvif == NULL) 899f9e354dfSJulian Elischer m->m_pkthdr.rcvif = ifunit("lo0"); 90020c822f3SJonathan Lemon if (m->m_pkthdr.csum_flags & CSUM_DELAY_DATA) { 90120c822f3SJonathan Lemon m->m_pkthdr.csum_flags |= 90220c822f3SJonathan Lemon CSUM_DATA_VALID | CSUM_PSEUDO_HDR; 90320c822f3SJonathan Lemon m0->m_pkthdr.csum_data = 0xffff; 90420c822f3SJonathan Lemon } 90520c822f3SJonathan Lemon m->m_pkthdr.csum_flags |= 90620c822f3SJonathan Lemon CSUM_IP_CHECKED | CSUM_IP_VALID; 907fd8e4ebcSMike Barcroft ip->ip_len = htons(ip->ip_len); 908fd8e4ebcSMike Barcroft ip->ip_off = htons(ip->ip_off); 9092b25acc1SLuigi Rizzo ip_input((struct mbuf *)&tag); 910f9e354dfSJulian Elischer goto done; 911f9e354dfSJulian Elischer } 912f9e354dfSJulian Elischer /* Some of the logic for this was 913f9e354dfSJulian Elischer * nicked from above. 914f9e354dfSJulian Elischer * 915f9e354dfSJulian Elischer * This rewrites the cached route in a local PCB. 916f9e354dfSJulian Elischer * Is this what we want to do? 917f9e354dfSJulian Elischer */ 918f9e354dfSJulian Elischer bcopy(dst, &ro_fwd->ro_dst, sizeof(*dst)); 919f9e354dfSJulian Elischer 920f9e354dfSJulian Elischer ro_fwd->ro_rt = 0; 921f9e354dfSJulian Elischer rtalloc_ign(ro_fwd, RTF_PRCLONING); 922f9e354dfSJulian Elischer 923f9e354dfSJulian Elischer if (ro_fwd->ro_rt == 0) { 924f9e354dfSJulian Elischer ipstat.ips_noroute++; 925f9e354dfSJulian Elischer error = EHOSTUNREACH; 926f9e354dfSJulian Elischer goto bad; 927f9e354dfSJulian Elischer } 928f9e354dfSJulian Elischer 929f9e354dfSJulian Elischer ia = ifatoia(ro_fwd->ro_rt->rt_ifa); 930f9e354dfSJulian Elischer ifp = ro_fwd->ro_rt->rt_ifp; 931f9e354dfSJulian Elischer ro_fwd->ro_rt->rt_use++; 932f9e354dfSJulian Elischer if (ro_fwd->ro_rt->rt_flags & RTF_GATEWAY) 9332b25acc1SLuigi Rizzo dst = (struct sockaddr_in *) 9342b25acc1SLuigi Rizzo ro_fwd->ro_rt->rt_gateway; 935f9e354dfSJulian Elischer if (ro_fwd->ro_rt->rt_flags & RTF_HOST) 936f9e354dfSJulian Elischer isbroadcast = 937f9e354dfSJulian Elischer (ro_fwd->ro_rt->rt_flags & RTF_BROADCAST); 938f9e354dfSJulian Elischer else 939f9e354dfSJulian Elischer isbroadcast = in_broadcast(dst->sin_addr, ifp); 9403f9e3122SYaroslav Tykhiy if (ro->ro_rt) 941f9e354dfSJulian Elischer RTFREE(ro->ro_rt); 942f9e354dfSJulian Elischer ro->ro_rt = ro_fwd->ro_rt; 943f9e354dfSJulian Elischer dst = (struct sockaddr_in *)&ro_fwd->ro_dst; 944f9e354dfSJulian Elischer 9452b25acc1SLuigi Rizzo #endif /* ... block to be put into a function */ 946f9e354dfSJulian Elischer /* 947f9e354dfSJulian Elischer * If we added a default src ip earlier, 948f9e354dfSJulian Elischer * which would have been gotten from the-then 949f9e354dfSJulian Elischer * interface, do it again, from the new one. 950f9e354dfSJulian Elischer */ 9512b25acc1SLuigi Rizzo if (src_was_INADDR_ANY) 952f9e354dfSJulian Elischer ip->ip_src = IA_SIN(ia)->sin_addr; 953b715f178SLuigi Rizzo goto pass ; 954f9e354dfSJulian Elischer } 9552b25acc1SLuigi Rizzo 956b715f178SLuigi Rizzo /* 957b715f178SLuigi Rizzo * if we get here, none of the above matches, and 958b715f178SLuigi Rizzo * we have to drop the pkt 959b715f178SLuigi Rizzo */ 960b715f178SLuigi Rizzo m_freem(m); 961b715f178SLuigi Rizzo error = EACCES; /* not sure this is the right error msg */ 962b715f178SLuigi Rizzo goto done; 96393e0e116SJulian Elischer } 964e7319babSPoul-Henning Kamp 965b715f178SLuigi Rizzo pass: 96651c8ec4aSRuslan Ermilov /* 127/8 must not appear on wire - RFC1122. */ 96751c8ec4aSRuslan Ermilov if ((ntohl(ip->ip_dst.s_addr) >> IN_CLASSA_NSHIFT) == IN_LOOPBACKNET || 96851c8ec4aSRuslan Ermilov (ntohl(ip->ip_src.s_addr) >> IN_CLASSA_NSHIFT) == IN_LOOPBACKNET) { 96951c8ec4aSRuslan Ermilov if ((ifp->if_flags & IFF_LOOPBACK) == 0) { 97051c8ec4aSRuslan Ermilov ipstat.ips_badaddr++; 97151c8ec4aSRuslan Ermilov error = EADDRNOTAVAIL; 97251c8ec4aSRuslan Ermilov goto bad; 97351c8ec4aSRuslan Ermilov } 97451c8ec4aSRuslan Ermilov } 97551c8ec4aSRuslan Ermilov 976206a3274SRuslan Ermilov m->m_pkthdr.csum_flags |= CSUM_IP; 977206a3274SRuslan Ermilov sw_csum = m->m_pkthdr.csum_flags & ~ifp->if_hwassist; 978db4f9cc7SJonathan Lemon if (sw_csum & CSUM_DELAY_DATA) { 979db4f9cc7SJonathan Lemon in_delayed_cksum(m); 980db4f9cc7SJonathan Lemon sw_csum &= ~CSUM_DELAY_DATA; 981db4f9cc7SJonathan Lemon } 982206a3274SRuslan Ermilov m->m_pkthdr.csum_flags &= ifp->if_hwassist; 983db4f9cc7SJonathan Lemon 984e7319babSPoul-Henning Kamp /* 985db4f9cc7SJonathan Lemon * If small enough for interface, or the interface will take 986db4f9cc7SJonathan Lemon * care of the fragmentation for us, can just send directly. 987df8bae1dSRodney W. Grimes */ 988db4f9cc7SJonathan Lemon if ((u_short)ip->ip_len <= ifp->if_mtu || 989db4f9cc7SJonathan Lemon ifp->if_hwassist & CSUM_FRAGMENT) { 990fd8e4ebcSMike Barcroft ip->ip_len = htons(ip->ip_len); 991fd8e4ebcSMike Barcroft ip->ip_off = htons(ip->ip_off); 992df8bae1dSRodney W. Grimes ip->ip_sum = 0; 99353be11f6SPoul-Henning Kamp if (sw_csum & CSUM_DELAY_IP) 994df8bae1dSRodney W. Grimes ip->ip_sum = in_cksum(m, hlen); 9955da9f8faSJosef Karthauser 9965da9f8faSJosef Karthauser /* Record statistics for this interface address. */ 99738c1bc35SRuslan Ermilov if (!(flags & IP_FORWARDING) && ia) { 9985da9f8faSJosef Karthauser ia->ia_ifa.if_opackets++; 9995da9f8faSJosef Karthauser ia->ia_ifa.if_obytes += m->m_pkthdr.len; 10005da9f8faSJosef Karthauser } 10015da9f8faSJosef Karthauser 100233841545SHajimu UMEMOTO #ifdef IPSEC 100333841545SHajimu UMEMOTO /* clean ipsec history once it goes out of the node */ 100433841545SHajimu UMEMOTO ipsec_delaux(m); 100533841545SHajimu UMEMOTO #endif 100633841545SHajimu UMEMOTO 1007df8bae1dSRodney W. Grimes error = (*ifp->if_output)(ifp, m, 1008df8bae1dSRodney W. Grimes (struct sockaddr *)dst, ro->ro_rt); 1009df8bae1dSRodney W. Grimes goto done; 1010df8bae1dSRodney W. Grimes } 1011df8bae1dSRodney W. Grimes /* 1012df8bae1dSRodney W. Grimes * Too large for interface; fragment if possible. 1013df8bae1dSRodney W. Grimes * Must be able to put at least 8 bytes per fragment. 1014df8bae1dSRodney W. Grimes */ 1015df8bae1dSRodney W. Grimes if (ip->ip_off & IP_DF) { 1016df8bae1dSRodney W. Grimes error = EMSGSIZE; 10173d1f141bSGarrett Wollman /* 10183d1f141bSGarrett Wollman * This case can happen if the user changed the MTU 10193d1f141bSGarrett Wollman * of an interface after enabling IP on it. Because 10203d1f141bSGarrett Wollman * most netifs don't keep track of routes pointing to 10213d1f141bSGarrett Wollman * them, there is no way for one to update all its 10223d1f141bSGarrett Wollman * routes when the MTU is changed. 10233d1f141bSGarrett Wollman */ 10243d1f141bSGarrett Wollman if ((ro->ro_rt->rt_flags & (RTF_UP | RTF_HOST)) 10253d1f141bSGarrett Wollman && !(ro->ro_rt->rt_rmx.rmx_locks & RTV_MTU) 10263d1f141bSGarrett Wollman && (ro->ro_rt->rt_rmx.rmx_mtu > ifp->if_mtu)) { 10273d1f141bSGarrett Wollman ro->ro_rt->rt_rmx.rmx_mtu = ifp->if_mtu; 10283d1f141bSGarrett Wollman } 1029df8bae1dSRodney W. Grimes ipstat.ips_cantfrag++; 1030df8bae1dSRodney W. Grimes goto bad; 1031df8bae1dSRodney W. Grimes } 1032df8bae1dSRodney W. Grimes len = (ifp->if_mtu - hlen) &~ 7; 1033df8bae1dSRodney W. Grimes if (len < 8) { 1034df8bae1dSRodney W. Grimes error = EMSGSIZE; 1035df8bae1dSRodney W. Grimes goto bad; 1036df8bae1dSRodney W. Grimes } 1037df8bae1dSRodney W. Grimes 1038db4f9cc7SJonathan Lemon /* 1039db4f9cc7SJonathan Lemon * if the interface will not calculate checksums on 1040db4f9cc7SJonathan Lemon * fragmented packets, then do it here. 1041db4f9cc7SJonathan Lemon */ 1042db4f9cc7SJonathan Lemon if (m->m_pkthdr.csum_flags & CSUM_DELAY_DATA && 1043db4f9cc7SJonathan Lemon (ifp->if_hwassist & CSUM_IP_FRAGS) == 0) { 1044db4f9cc7SJonathan Lemon in_delayed_cksum(m); 1045db4f9cc7SJonathan Lemon m->m_pkthdr.csum_flags &= ~CSUM_DELAY_DATA; 1046db4f9cc7SJonathan Lemon } 1047db4f9cc7SJonathan Lemon 104898cb733cSKenneth D. Merry if (len > PAGE_SIZE) { 104998cb733cSKenneth D. Merry /* 105098cb733cSKenneth D. Merry * Fragement large datagrams such that each segment 105198cb733cSKenneth D. Merry * contains a multiple of PAGE_SIZE amount of data, 105298cb733cSKenneth D. Merry * plus headers. This enables a receiver to perform 105398cb733cSKenneth D. Merry * page-flipping zero-copy optimizations. 105498cb733cSKenneth D. Merry */ 105598cb733cSKenneth D. Merry 105698cb733cSKenneth D. Merry int newlen; 105798cb733cSKenneth D. Merry struct mbuf *mtmp; 105898cb733cSKenneth D. Merry 105998cb733cSKenneth D. Merry for (mtmp = m, off = 0; 106098cb733cSKenneth D. Merry mtmp && ((off + mtmp->m_len) <= ifp->if_mtu); 106198cb733cSKenneth D. Merry mtmp = mtmp->m_next) { 106298cb733cSKenneth D. Merry off += mtmp->m_len; 106398cb733cSKenneth D. Merry } 106498cb733cSKenneth D. Merry /* 106598cb733cSKenneth D. Merry * firstlen (off - hlen) must be aligned on an 106698cb733cSKenneth D. Merry * 8-byte boundary 106798cb733cSKenneth D. Merry */ 106898cb733cSKenneth D. Merry if (off < hlen) 106998cb733cSKenneth D. Merry goto smart_frag_failure; 107098cb733cSKenneth D. Merry off = ((off - hlen) & ~7) + hlen; 107198cb733cSKenneth D. Merry newlen = (~PAGE_MASK) & ifp->if_mtu; 107298cb733cSKenneth D. Merry if ((newlen + sizeof (struct ip)) > ifp->if_mtu) { 107398cb733cSKenneth D. Merry /* we failed, go back the default */ 107498cb733cSKenneth D. Merry smart_frag_failure: 107598cb733cSKenneth D. Merry newlen = len; 107698cb733cSKenneth D. Merry off = hlen + len; 107798cb733cSKenneth D. Merry } 107898cb733cSKenneth D. Merry 107998cb733cSKenneth D. Merry /* printf("ipfrag: len = %d, hlen = %d, mhlen = %d, newlen = %d, off = %d\n", 108098cb733cSKenneth D. Merry len, hlen, sizeof (struct ip), newlen, off);*/ 108198cb733cSKenneth D. Merry 108298cb733cSKenneth D. Merry len = newlen; 108398cb733cSKenneth D. Merry 108498cb733cSKenneth D. Merry } else { 108598cb733cSKenneth D. Merry off = hlen + len; 108698cb733cSKenneth D. Merry } 108798cb733cSKenneth D. Merry 108898cb733cSKenneth D. Merry 108998cb733cSKenneth D. Merry 1090df8bae1dSRodney W. Grimes { 109198cb733cSKenneth D. Merry int mhlen, firstlen = off - hlen; 1092df8bae1dSRodney W. Grimes struct mbuf **mnext = &m->m_nextpkt; 1093db4f9cc7SJonathan Lemon int nfrags = 1; 1094df8bae1dSRodney W. Grimes 1095df8bae1dSRodney W. Grimes /* 1096df8bae1dSRodney W. Grimes * Loop through length of segment after first fragment, 1097df8bae1dSRodney W. Grimes * make new header and copy data of each part and link onto chain. 1098df8bae1dSRodney W. Grimes */ 1099df8bae1dSRodney W. Grimes m0 = m; 1100df8bae1dSRodney W. Grimes mhlen = sizeof (struct ip); 110198cb733cSKenneth D. Merry for (; off < (u_short)ip->ip_len; off += len) { 1102df8bae1dSRodney W. Grimes MGETHDR(m, M_DONTWAIT, MT_HEADER); 1103df8bae1dSRodney W. Grimes if (m == 0) { 1104df8bae1dSRodney W. Grimes error = ENOBUFS; 1105df8bae1dSRodney W. Grimes ipstat.ips_odropped++; 1106df8bae1dSRodney W. Grimes goto sendorfree; 1107df8bae1dSRodney W. Grimes } 1108db4f9cc7SJonathan Lemon m->m_flags |= (m0->m_flags & M_MCAST) | M_FRAG; 1109df8bae1dSRodney W. Grimes m->m_data += max_linkhdr; 1110df8bae1dSRodney W. Grimes mhip = mtod(m, struct ip *); 1111df8bae1dSRodney W. Grimes *mhip = *ip; 1112df8bae1dSRodney W. Grimes if (hlen > sizeof (struct ip)) { 1113df8bae1dSRodney W. Grimes mhlen = ip_optcopy(ip, mhip) + sizeof (struct ip); 111453be11f6SPoul-Henning Kamp mhip->ip_v = IPVERSION; 111553be11f6SPoul-Henning Kamp mhip->ip_hl = mhlen >> 2; 1116df8bae1dSRodney W. Grimes } 1117df8bae1dSRodney W. Grimes m->m_len = mhlen; 1118cc22c7a7SRuslan Ermilov mhip->ip_off = ((off - hlen) >> 3) + ip->ip_off; 1119df8bae1dSRodney W. Grimes if (off + len >= (u_short)ip->ip_len) 1120df8bae1dSRodney W. Grimes len = (u_short)ip->ip_len - off; 1121df8bae1dSRodney W. Grimes else 1122df8bae1dSRodney W. Grimes mhip->ip_off |= IP_MF; 1123df8bae1dSRodney W. Grimes mhip->ip_len = htons((u_short)(len + mhlen)); 1124df8bae1dSRodney W. Grimes m->m_next = m_copy(m0, off, len); 1125df8bae1dSRodney W. Grimes if (m->m_next == 0) { 1126df8bae1dSRodney W. Grimes (void) m_free(m); 1127df8bae1dSRodney W. Grimes error = ENOBUFS; /* ??? */ 1128df8bae1dSRodney W. Grimes ipstat.ips_odropped++; 1129df8bae1dSRodney W. Grimes goto sendorfree; 1130df8bae1dSRodney W. Grimes } 1131df8bae1dSRodney W. Grimes m->m_pkthdr.len = mhlen + len; 1132df8bae1dSRodney W. Grimes m->m_pkthdr.rcvif = (struct ifnet *)0; 11334ed84624SRobert Watson #ifdef MAC 11344ed84624SRobert Watson mac_create_fragment(m0, m); 11354ed84624SRobert Watson #endif 1136db4f9cc7SJonathan Lemon m->m_pkthdr.csum_flags = m0->m_pkthdr.csum_flags; 1137fd8e4ebcSMike Barcroft mhip->ip_off = htons(mhip->ip_off); 1138df8bae1dSRodney W. Grimes mhip->ip_sum = 0; 113953be11f6SPoul-Henning Kamp if (sw_csum & CSUM_DELAY_IP) 1140df8bae1dSRodney W. Grimes mhip->ip_sum = in_cksum(m, mhlen); 1141df8bae1dSRodney W. Grimes *mnext = m; 1142df8bae1dSRodney W. Grimes mnext = &m->m_nextpkt; 1143db4f9cc7SJonathan Lemon nfrags++; 1144df8bae1dSRodney W. Grimes } 1145db4f9cc7SJonathan Lemon ipstat.ips_ofragments += nfrags; 1146db4f9cc7SJonathan Lemon 1147db4f9cc7SJonathan Lemon /* set first/last markers for fragment chain */ 1148db4f9cc7SJonathan Lemon m->m_flags |= M_LASTFRAG; 1149db4f9cc7SJonathan Lemon m0->m_flags |= M_FIRSTFRAG | M_FRAG; 1150db4f9cc7SJonathan Lemon m0->m_pkthdr.csum_data = nfrags; 1151db4f9cc7SJonathan Lemon 1152df8bae1dSRodney W. Grimes /* 1153df8bae1dSRodney W. Grimes * Update first fragment by trimming what's been copied out 1154df8bae1dSRodney W. Grimes * and updating header, then send each fragment (in order). 1155df8bae1dSRodney W. Grimes */ 1156df8bae1dSRodney W. Grimes m = m0; 1157df8bae1dSRodney W. Grimes m_adj(m, hlen + firstlen - (u_short)ip->ip_len); 1158df8bae1dSRodney W. Grimes m->m_pkthdr.len = hlen + firstlen; 1159df8bae1dSRodney W. Grimes ip->ip_len = htons((u_short)m->m_pkthdr.len); 116004287599SRuslan Ermilov ip->ip_off |= IP_MF; 1161fd8e4ebcSMike Barcroft ip->ip_off = htons(ip->ip_off); 1162df8bae1dSRodney W. Grimes ip->ip_sum = 0; 116353be11f6SPoul-Henning Kamp if (sw_csum & CSUM_DELAY_IP) 1164df8bae1dSRodney W. Grimes ip->ip_sum = in_cksum(m, hlen); 1165df8bae1dSRodney W. Grimes sendorfree: 1166df8bae1dSRodney W. Grimes for (m = m0; m; m = m0) { 1167df8bae1dSRodney W. Grimes m0 = m->m_nextpkt; 1168df8bae1dSRodney W. Grimes m->m_nextpkt = 0; 116933841545SHajimu UMEMOTO #ifdef IPSEC 117033841545SHajimu UMEMOTO /* clean ipsec history once it goes out of the node */ 117133841545SHajimu UMEMOTO ipsec_delaux(m); 117233841545SHajimu UMEMOTO #endif 117307203494SDaniel C. Sobral if (error == 0) { 1174fe937674SJosef Karthauser /* Record statistics for this interface address. */ 117507203494SDaniel C. Sobral if (ia != NULL) { 1176fe937674SJosef Karthauser ia->ia_ifa.if_opackets++; 1177fe937674SJosef Karthauser ia->ia_ifa.if_obytes += m->m_pkthdr.len; 117807203494SDaniel C. Sobral } 1179fe937674SJosef Karthauser 1180df8bae1dSRodney W. Grimes error = (*ifp->if_output)(ifp, m, 1181df8bae1dSRodney W. Grimes (struct sockaddr *)dst, ro->ro_rt); 1182fe937674SJosef Karthauser } else 1183df8bae1dSRodney W. Grimes m_freem(m); 1184df8bae1dSRodney W. Grimes } 1185df8bae1dSRodney W. Grimes 1186df8bae1dSRodney W. Grimes if (error == 0) 1187df8bae1dSRodney W. Grimes ipstat.ips_fragmented++; 1188df8bae1dSRodney W. Grimes } 1189df8bae1dSRodney W. Grimes done: 11906a800098SYoshinobu Inoue #ifdef IPSEC 11916a800098SYoshinobu Inoue if (ro == &iproute && ro->ro_rt) { 11926a800098SYoshinobu Inoue RTFREE(ro->ro_rt); 11936a800098SYoshinobu Inoue ro->ro_rt = NULL; 11946a800098SYoshinobu Inoue } 11956a800098SYoshinobu Inoue if (sp != NULL) { 11966a800098SYoshinobu Inoue KEYDEBUG(KEYDEBUG_IPSEC_STAMP, 11976a800098SYoshinobu Inoue printf("DP ip_output call free SP:%p\n", sp)); 11986a800098SYoshinobu Inoue key_freesp(sp); 11996a800098SYoshinobu Inoue } 12006a800098SYoshinobu Inoue #endif /* IPSEC */ 1201b9234fafSSam Leffler #ifdef FAST_IPSEC 1202b9234fafSSam Leffler if (ro == &iproute && ro->ro_rt) { 1203b9234fafSSam Leffler RTFREE(ro->ro_rt); 1204b9234fafSSam Leffler ro->ro_rt = NULL; 1205b9234fafSSam Leffler } 1206b9234fafSSam Leffler if (sp != NULL) 1207b9234fafSSam Leffler KEY_FREESP(&sp); 1208b9234fafSSam Leffler #endif /* FAST_IPSEC */ 1209df8bae1dSRodney W. Grimes return (error); 1210df8bae1dSRodney W. Grimes bad: 12113528d68fSBill Paul m_freem(m); 1212df8bae1dSRodney W. Grimes goto done; 1213df8bae1dSRodney W. Grimes } 1214df8bae1dSRodney W. Grimes 12151c238475SJonathan Lemon void 1216db4f9cc7SJonathan Lemon in_delayed_cksum(struct mbuf *m) 1217db4f9cc7SJonathan Lemon { 1218db4f9cc7SJonathan Lemon struct ip *ip; 1219db4f9cc7SJonathan Lemon u_short csum, offset; 1220db4f9cc7SJonathan Lemon 1221db4f9cc7SJonathan Lemon ip = mtod(m, struct ip *); 122253be11f6SPoul-Henning Kamp offset = ip->ip_hl << 2 ; 1223db4f9cc7SJonathan Lemon csum = in_cksum_skip(m, ip->ip_len, offset); 1224206a3274SRuslan Ermilov if (m->m_pkthdr.csum_flags & CSUM_UDP && csum == 0) 1225206a3274SRuslan Ermilov csum = 0xffff; 1226db4f9cc7SJonathan Lemon offset += m->m_pkthdr.csum_data; /* checksum offset */ 1227db4f9cc7SJonathan Lemon 1228db4f9cc7SJonathan Lemon if (offset + sizeof(u_short) > m->m_len) { 1229db4f9cc7SJonathan Lemon printf("delayed m_pullup, m->len: %d off: %d p: %d\n", 1230db4f9cc7SJonathan Lemon m->m_len, offset, ip->ip_p); 1231db4f9cc7SJonathan Lemon /* 1232db4f9cc7SJonathan Lemon * XXX 1233db4f9cc7SJonathan Lemon * this shouldn't happen, but if it does, the 1234db4f9cc7SJonathan Lemon * correct behavior may be to insert the checksum 1235db4f9cc7SJonathan Lemon * in the existing chain instead of rearranging it. 1236db4f9cc7SJonathan Lemon */ 1237db4f9cc7SJonathan Lemon m = m_pullup(m, offset + sizeof(u_short)); 1238db4f9cc7SJonathan Lemon } 1239db4f9cc7SJonathan Lemon *(u_short *)(m->m_data + offset) = csum; 1240db4f9cc7SJonathan Lemon } 1241db4f9cc7SJonathan Lemon 1242df8bae1dSRodney W. Grimes /* 1243df8bae1dSRodney W. Grimes * Insert IP options into preformed packet. 1244df8bae1dSRodney W. Grimes * Adjust IP destination as required for IP source routing, 1245df8bae1dSRodney W. Grimes * as indicated by a non-zero in_addr at the start of the options. 1246072b9b24SPaul Traina * 1247072b9b24SPaul Traina * XXX This routine assumes that the packet has no options in place. 1248df8bae1dSRodney W. Grimes */ 1249df8bae1dSRodney W. Grimes static struct mbuf * 1250df8bae1dSRodney W. Grimes ip_insertoptions(m, opt, phlen) 1251df8bae1dSRodney W. Grimes register struct mbuf *m; 1252df8bae1dSRodney W. Grimes struct mbuf *opt; 1253df8bae1dSRodney W. Grimes int *phlen; 1254df8bae1dSRodney W. Grimes { 1255df8bae1dSRodney W. Grimes register struct ipoption *p = mtod(opt, struct ipoption *); 1256df8bae1dSRodney W. Grimes struct mbuf *n; 1257df8bae1dSRodney W. Grimes register struct ip *ip = mtod(m, struct ip *); 1258df8bae1dSRodney W. Grimes unsigned optlen; 1259df8bae1dSRodney W. Grimes 1260df8bae1dSRodney W. Grimes optlen = opt->m_len - sizeof(p->ipopt_dst); 1261cb7641e8SMaxim Konovalov if (optlen + (u_short)ip->ip_len > IP_MAXPACKET) { 1262cb7641e8SMaxim Konovalov *phlen = 0; 1263df8bae1dSRodney W. Grimes return (m); /* XXX should fail */ 1264cb7641e8SMaxim Konovalov } 1265df8bae1dSRodney W. Grimes if (p->ipopt_dst.s_addr) 1266df8bae1dSRodney W. Grimes ip->ip_dst = p->ipopt_dst; 1267df8bae1dSRodney W. Grimes if (m->m_flags & M_EXT || m->m_data - optlen < m->m_pktdat) { 1268df8bae1dSRodney W. Grimes MGETHDR(n, M_DONTWAIT, MT_HEADER); 1269cb7641e8SMaxim Konovalov if (n == 0) { 1270cb7641e8SMaxim Konovalov *phlen = 0; 1271df8bae1dSRodney W. Grimes return (m); 1272cb7641e8SMaxim Konovalov } 12735db1e34eSRuslan Ermilov n->m_pkthdr.rcvif = (struct ifnet *)0; 12744ed84624SRobert Watson #ifdef MAC 12754ed84624SRobert Watson mac_create_mbuf_from_mbuf(m, n); 12764ed84624SRobert Watson #endif 1277df8bae1dSRodney W. Grimes n->m_pkthdr.len = m->m_pkthdr.len + optlen; 1278df8bae1dSRodney W. Grimes m->m_len -= sizeof(struct ip); 1279df8bae1dSRodney W. Grimes m->m_data += sizeof(struct ip); 1280df8bae1dSRodney W. Grimes n->m_next = m; 1281df8bae1dSRodney W. Grimes m = n; 1282df8bae1dSRodney W. Grimes m->m_len = optlen + sizeof(struct ip); 1283df8bae1dSRodney W. Grimes m->m_data += max_linkhdr; 128494a5d9b6SDavid Greenman (void)memcpy(mtod(m, void *), ip, sizeof(struct ip)); 1285df8bae1dSRodney W. Grimes } else { 1286df8bae1dSRodney W. Grimes m->m_data -= optlen; 1287df8bae1dSRodney W. Grimes m->m_len += optlen; 1288df8bae1dSRodney W. Grimes m->m_pkthdr.len += optlen; 1289df8bae1dSRodney W. Grimes ovbcopy((caddr_t)ip, mtod(m, caddr_t), sizeof(struct ip)); 1290df8bae1dSRodney W. Grimes } 1291df8bae1dSRodney W. Grimes ip = mtod(m, struct ip *); 12920453d3cbSBruce Evans bcopy(p->ipopt_list, ip + 1, optlen); 1293df8bae1dSRodney W. Grimes *phlen = sizeof(struct ip) + optlen; 129453be11f6SPoul-Henning Kamp ip->ip_v = IPVERSION; 129553be11f6SPoul-Henning Kamp ip->ip_hl = *phlen >> 2; 1296df8bae1dSRodney W. Grimes ip->ip_len += optlen; 1297df8bae1dSRodney W. Grimes return (m); 1298df8bae1dSRodney W. Grimes } 1299df8bae1dSRodney W. Grimes 1300df8bae1dSRodney W. Grimes /* 1301df8bae1dSRodney W. Grimes * Copy options from ip to jp, 1302df8bae1dSRodney W. Grimes * omitting those not copied during fragmentation. 1303df8bae1dSRodney W. Grimes */ 1304beec8214SDarren Reed int 1305df8bae1dSRodney W. Grimes ip_optcopy(ip, jp) 1306df8bae1dSRodney W. Grimes struct ip *ip, *jp; 1307df8bae1dSRodney W. Grimes { 1308df8bae1dSRodney W. Grimes register u_char *cp, *dp; 1309df8bae1dSRodney W. Grimes int opt, optlen, cnt; 1310df8bae1dSRodney W. Grimes 1311df8bae1dSRodney W. Grimes cp = (u_char *)(ip + 1); 1312df8bae1dSRodney W. Grimes dp = (u_char *)(jp + 1); 131353be11f6SPoul-Henning Kamp cnt = (ip->ip_hl << 2) - sizeof (struct ip); 1314df8bae1dSRodney W. Grimes for (; cnt > 0; cnt -= optlen, cp += optlen) { 1315df8bae1dSRodney W. Grimes opt = cp[0]; 1316df8bae1dSRodney W. Grimes if (opt == IPOPT_EOL) 1317df8bae1dSRodney W. Grimes break; 1318df8bae1dSRodney W. Grimes if (opt == IPOPT_NOP) { 1319df8bae1dSRodney W. Grimes /* Preserve for IP mcast tunnel's LSRR alignment. */ 1320df8bae1dSRodney W. Grimes *dp++ = IPOPT_NOP; 1321df8bae1dSRodney W. Grimes optlen = 1; 1322df8bae1dSRodney W. Grimes continue; 1323686cdd19SJun-ichiro itojun Hagino } 1324db40007dSAndrew R. Reiter 1325db40007dSAndrew R. Reiter KASSERT(cnt >= IPOPT_OLEN + sizeof(*cp), 1326db40007dSAndrew R. Reiter ("ip_optcopy: malformed ipv4 option")); 1327df8bae1dSRodney W. Grimes optlen = cp[IPOPT_OLEN]; 1328db40007dSAndrew R. Reiter KASSERT(optlen >= IPOPT_OLEN + sizeof(*cp) && optlen <= cnt, 1329db40007dSAndrew R. Reiter ("ip_optcopy: malformed ipv4 option")); 1330db40007dSAndrew R. Reiter 1331df8bae1dSRodney W. Grimes /* bogus lengths should have been caught by ip_dooptions */ 1332df8bae1dSRodney W. Grimes if (optlen > cnt) 1333df8bae1dSRodney W. Grimes optlen = cnt; 1334df8bae1dSRodney W. Grimes if (IPOPT_COPIED(opt)) { 13350453d3cbSBruce Evans bcopy(cp, dp, optlen); 1336df8bae1dSRodney W. Grimes dp += optlen; 1337df8bae1dSRodney W. Grimes } 1338df8bae1dSRodney W. Grimes } 1339df8bae1dSRodney W. Grimes for (optlen = dp - (u_char *)(jp+1); optlen & 0x3; optlen++) 1340df8bae1dSRodney W. Grimes *dp++ = IPOPT_EOL; 1341df8bae1dSRodney W. Grimes return (optlen); 1342df8bae1dSRodney W. Grimes } 1343df8bae1dSRodney W. Grimes 1344df8bae1dSRodney W. Grimes /* 1345df8bae1dSRodney W. Grimes * IP socket option processing. 1346df8bae1dSRodney W. Grimes */ 1347df8bae1dSRodney W. Grimes int 1348cfe8b629SGarrett Wollman ip_ctloutput(so, sopt) 1349df8bae1dSRodney W. Grimes struct socket *so; 1350cfe8b629SGarrett Wollman struct sockopt *sopt; 1351df8bae1dSRodney W. Grimes { 1352cfe8b629SGarrett Wollman struct inpcb *inp = sotoinpcb(so); 1353cfe8b629SGarrett Wollman int error, optval; 1354df8bae1dSRodney W. Grimes 1355cfe8b629SGarrett Wollman error = optval = 0; 1356cfe8b629SGarrett Wollman if (sopt->sopt_level != IPPROTO_IP) { 1357cfe8b629SGarrett Wollman return (EINVAL); 1358cfe8b629SGarrett Wollman } 1359df8bae1dSRodney W. Grimes 1360cfe8b629SGarrett Wollman switch (sopt->sopt_dir) { 1361cfe8b629SGarrett Wollman case SOPT_SET: 1362cfe8b629SGarrett Wollman switch (sopt->sopt_name) { 1363df8bae1dSRodney W. Grimes case IP_OPTIONS: 1364df8bae1dSRodney W. Grimes #ifdef notyet 1365df8bae1dSRodney W. Grimes case IP_RETOPTS: 1366df8bae1dSRodney W. Grimes #endif 1367cfe8b629SGarrett Wollman { 1368cfe8b629SGarrett Wollman struct mbuf *m; 1369cfe8b629SGarrett Wollman if (sopt->sopt_valsize > MLEN) { 1370cfe8b629SGarrett Wollman error = EMSGSIZE; 1371cfe8b629SGarrett Wollman break; 1372cfe8b629SGarrett Wollman } 1373b40ce416SJulian Elischer MGET(m, sopt->sopt_td ? M_TRYWAIT : M_DONTWAIT, MT_HEADER); 1374cfe8b629SGarrett Wollman if (m == 0) { 1375cfe8b629SGarrett Wollman error = ENOBUFS; 1376cfe8b629SGarrett Wollman break; 1377cfe8b629SGarrett Wollman } 1378cfe8b629SGarrett Wollman m->m_len = sopt->sopt_valsize; 1379cfe8b629SGarrett Wollman error = sooptcopyin(sopt, mtod(m, char *), m->m_len, 1380cfe8b629SGarrett Wollman m->m_len); 1381cfe8b629SGarrett Wollman 1382cfe8b629SGarrett Wollman return (ip_pcbopts(sopt->sopt_name, &inp->inp_options, 1383cfe8b629SGarrett Wollman m)); 1384cfe8b629SGarrett Wollman } 1385df8bae1dSRodney W. Grimes 1386df8bae1dSRodney W. Grimes case IP_TOS: 1387df8bae1dSRodney W. Grimes case IP_TTL: 1388df8bae1dSRodney W. Grimes case IP_RECVOPTS: 1389df8bae1dSRodney W. Grimes case IP_RECVRETOPTS: 1390df8bae1dSRodney W. Grimes case IP_RECVDSTADDR: 139182c23ebaSBill Fenner case IP_RECVIF: 13926a800098SYoshinobu Inoue case IP_FAITH: 1393cfe8b629SGarrett Wollman error = sooptcopyin(sopt, &optval, sizeof optval, 1394cfe8b629SGarrett Wollman sizeof optval); 1395cfe8b629SGarrett Wollman if (error) 1396cfe8b629SGarrett Wollman break; 1397df8bae1dSRodney W. Grimes 1398cfe8b629SGarrett Wollman switch (sopt->sopt_name) { 1399df8bae1dSRodney W. Grimes case IP_TOS: 1400ca98b82cSDavid Greenman inp->inp_ip_tos = optval; 1401df8bae1dSRodney W. Grimes break; 1402df8bae1dSRodney W. Grimes 1403df8bae1dSRodney W. Grimes case IP_TTL: 1404ca98b82cSDavid Greenman inp->inp_ip_ttl = optval; 1405df8bae1dSRodney W. Grimes break; 1406df8bae1dSRodney W. Grimes #define OPTSET(bit) \ 1407df8bae1dSRodney W. Grimes if (optval) \ 1408df8bae1dSRodney W. Grimes inp->inp_flags |= bit; \ 1409df8bae1dSRodney W. Grimes else \ 1410df8bae1dSRodney W. Grimes inp->inp_flags &= ~bit; 1411df8bae1dSRodney W. Grimes 1412df8bae1dSRodney W. Grimes case IP_RECVOPTS: 1413df8bae1dSRodney W. Grimes OPTSET(INP_RECVOPTS); 1414df8bae1dSRodney W. Grimes break; 1415df8bae1dSRodney W. Grimes 1416df8bae1dSRodney W. Grimes case IP_RECVRETOPTS: 1417df8bae1dSRodney W. Grimes OPTSET(INP_RECVRETOPTS); 1418df8bae1dSRodney W. Grimes break; 1419df8bae1dSRodney W. Grimes 1420df8bae1dSRodney W. Grimes case IP_RECVDSTADDR: 1421df8bae1dSRodney W. Grimes OPTSET(INP_RECVDSTADDR); 1422df8bae1dSRodney W. Grimes break; 142382c23ebaSBill Fenner 142482c23ebaSBill Fenner case IP_RECVIF: 142582c23ebaSBill Fenner OPTSET(INP_RECVIF); 142682c23ebaSBill Fenner break; 14276a800098SYoshinobu Inoue 14286a800098SYoshinobu Inoue case IP_FAITH: 14296a800098SYoshinobu Inoue OPTSET(INP_FAITH); 14306a800098SYoshinobu Inoue break; 1431df8bae1dSRodney W. Grimes } 1432df8bae1dSRodney W. Grimes break; 1433df8bae1dSRodney W. Grimes #undef OPTSET 1434df8bae1dSRodney W. Grimes 1435df8bae1dSRodney W. Grimes case IP_MULTICAST_IF: 1436f0068c4aSGarrett Wollman case IP_MULTICAST_VIF: 1437df8bae1dSRodney W. Grimes case IP_MULTICAST_TTL: 1438df8bae1dSRodney W. Grimes case IP_MULTICAST_LOOP: 1439df8bae1dSRodney W. Grimes case IP_ADD_MEMBERSHIP: 1440df8bae1dSRodney W. Grimes case IP_DROP_MEMBERSHIP: 1441cfe8b629SGarrett Wollman error = ip_setmoptions(sopt, &inp->inp_moptions); 1442df8bae1dSRodney W. Grimes break; 1443df8bae1dSRodney W. Grimes 144433b3ac06SPeter Wemm case IP_PORTRANGE: 1445cfe8b629SGarrett Wollman error = sooptcopyin(sopt, &optval, sizeof optval, 1446cfe8b629SGarrett Wollman sizeof optval); 1447cfe8b629SGarrett Wollman if (error) 1448cfe8b629SGarrett Wollman break; 144933b3ac06SPeter Wemm 145033b3ac06SPeter Wemm switch (optval) { 145133b3ac06SPeter Wemm case IP_PORTRANGE_DEFAULT: 145233b3ac06SPeter Wemm inp->inp_flags &= ~(INP_LOWPORT); 145333b3ac06SPeter Wemm inp->inp_flags &= ~(INP_HIGHPORT); 145433b3ac06SPeter Wemm break; 145533b3ac06SPeter Wemm 145633b3ac06SPeter Wemm case IP_PORTRANGE_HIGH: 145733b3ac06SPeter Wemm inp->inp_flags &= ~(INP_LOWPORT); 145833b3ac06SPeter Wemm inp->inp_flags |= INP_HIGHPORT; 145933b3ac06SPeter Wemm break; 146033b3ac06SPeter Wemm 146133b3ac06SPeter Wemm case IP_PORTRANGE_LOW: 146233b3ac06SPeter Wemm inp->inp_flags &= ~(INP_HIGHPORT); 146333b3ac06SPeter Wemm inp->inp_flags |= INP_LOWPORT; 146433b3ac06SPeter Wemm break; 146533b3ac06SPeter Wemm 146633b3ac06SPeter Wemm default: 146733b3ac06SPeter Wemm error = EINVAL; 146833b3ac06SPeter Wemm break; 146933b3ac06SPeter Wemm } 1470ce8c72b1SPeter Wemm break; 147133b3ac06SPeter Wemm 1472b9234fafSSam Leffler #if defined(IPSEC) || defined(FAST_IPSEC) 14736a800098SYoshinobu Inoue case IP_IPSEC_POLICY: 14746a800098SYoshinobu Inoue { 14756a800098SYoshinobu Inoue caddr_t req; 1476686cdd19SJun-ichiro itojun Hagino size_t len = 0; 14776a800098SYoshinobu Inoue int priv; 14786a800098SYoshinobu Inoue struct mbuf *m; 14796a800098SYoshinobu Inoue int optname; 14806a800098SYoshinobu Inoue 14816a800098SYoshinobu Inoue if ((error = soopt_getm(sopt, &m)) != 0) /* XXX */ 14826a800098SYoshinobu Inoue break; 14836a800098SYoshinobu Inoue if ((error = soopt_mcopyin(sopt, m)) != 0) /* XXX */ 14846a800098SYoshinobu Inoue break; 1485b40ce416SJulian Elischer priv = (sopt->sopt_td != NULL && 148644731cabSJohn Baldwin suser(sopt->sopt_td) != 0) ? 0 : 1; 14876a800098SYoshinobu Inoue req = mtod(m, caddr_t); 1488686cdd19SJun-ichiro itojun Hagino len = m->m_len; 14896a800098SYoshinobu Inoue optname = sopt->sopt_name; 1490686cdd19SJun-ichiro itojun Hagino error = ipsec4_set_policy(inp, optname, req, len, priv); 14916a800098SYoshinobu Inoue m_freem(m); 14926a800098SYoshinobu Inoue break; 14936a800098SYoshinobu Inoue } 14946a800098SYoshinobu Inoue #endif /*IPSEC*/ 14956a800098SYoshinobu Inoue 1496df8bae1dSRodney W. Grimes default: 1497df8bae1dSRodney W. Grimes error = ENOPROTOOPT; 1498df8bae1dSRodney W. Grimes break; 1499df8bae1dSRodney W. Grimes } 1500df8bae1dSRodney W. Grimes break; 1501df8bae1dSRodney W. Grimes 1502cfe8b629SGarrett Wollman case SOPT_GET: 1503cfe8b629SGarrett Wollman switch (sopt->sopt_name) { 1504df8bae1dSRodney W. Grimes case IP_OPTIONS: 1505df8bae1dSRodney W. Grimes case IP_RETOPTS: 1506cfe8b629SGarrett Wollman if (inp->inp_options) 1507cfe8b629SGarrett Wollman error = sooptcopyout(sopt, 1508cfe8b629SGarrett Wollman mtod(inp->inp_options, 1509cfe8b629SGarrett Wollman char *), 1510cfe8b629SGarrett Wollman inp->inp_options->m_len); 1511cfe8b629SGarrett Wollman else 1512cfe8b629SGarrett Wollman sopt->sopt_valsize = 0; 1513df8bae1dSRodney W. Grimes break; 1514df8bae1dSRodney W. Grimes 1515df8bae1dSRodney W. Grimes case IP_TOS: 1516df8bae1dSRodney W. Grimes case IP_TTL: 1517df8bae1dSRodney W. Grimes case IP_RECVOPTS: 1518df8bae1dSRodney W. Grimes case IP_RECVRETOPTS: 1519df8bae1dSRodney W. Grimes case IP_RECVDSTADDR: 152082c23ebaSBill Fenner case IP_RECVIF: 1521cfe8b629SGarrett Wollman case IP_PORTRANGE: 15226a800098SYoshinobu Inoue case IP_FAITH: 1523cfe8b629SGarrett Wollman switch (sopt->sopt_name) { 1524df8bae1dSRodney W. Grimes 1525df8bae1dSRodney W. Grimes case IP_TOS: 1526ca98b82cSDavid Greenman optval = inp->inp_ip_tos; 1527df8bae1dSRodney W. Grimes break; 1528df8bae1dSRodney W. Grimes 1529df8bae1dSRodney W. Grimes case IP_TTL: 1530ca98b82cSDavid Greenman optval = inp->inp_ip_ttl; 1531df8bae1dSRodney W. Grimes break; 1532df8bae1dSRodney W. Grimes 1533df8bae1dSRodney W. Grimes #define OPTBIT(bit) (inp->inp_flags & bit ? 1 : 0) 1534df8bae1dSRodney W. Grimes 1535df8bae1dSRodney W. Grimes case IP_RECVOPTS: 1536df8bae1dSRodney W. Grimes optval = OPTBIT(INP_RECVOPTS); 1537df8bae1dSRodney W. Grimes break; 1538df8bae1dSRodney W. Grimes 1539df8bae1dSRodney W. Grimes case IP_RECVRETOPTS: 1540df8bae1dSRodney W. Grimes optval = OPTBIT(INP_RECVRETOPTS); 1541df8bae1dSRodney W. Grimes break; 1542df8bae1dSRodney W. Grimes 1543df8bae1dSRodney W. Grimes case IP_RECVDSTADDR: 1544df8bae1dSRodney W. Grimes optval = OPTBIT(INP_RECVDSTADDR); 1545df8bae1dSRodney W. Grimes break; 154682c23ebaSBill Fenner 154782c23ebaSBill Fenner case IP_RECVIF: 154882c23ebaSBill Fenner optval = OPTBIT(INP_RECVIF); 154982c23ebaSBill Fenner break; 1550cfe8b629SGarrett Wollman 1551cfe8b629SGarrett Wollman case IP_PORTRANGE: 1552cfe8b629SGarrett Wollman if (inp->inp_flags & INP_HIGHPORT) 1553cfe8b629SGarrett Wollman optval = IP_PORTRANGE_HIGH; 1554cfe8b629SGarrett Wollman else if (inp->inp_flags & INP_LOWPORT) 1555cfe8b629SGarrett Wollman optval = IP_PORTRANGE_LOW; 1556cfe8b629SGarrett Wollman else 1557cfe8b629SGarrett Wollman optval = 0; 1558cfe8b629SGarrett Wollman break; 15596a800098SYoshinobu Inoue 15606a800098SYoshinobu Inoue case IP_FAITH: 15616a800098SYoshinobu Inoue optval = OPTBIT(INP_FAITH); 15626a800098SYoshinobu Inoue break; 1563df8bae1dSRodney W. Grimes } 1564cfe8b629SGarrett Wollman error = sooptcopyout(sopt, &optval, sizeof optval); 1565df8bae1dSRodney W. Grimes break; 1566df8bae1dSRodney W. Grimes 1567df8bae1dSRodney W. Grimes case IP_MULTICAST_IF: 1568f0068c4aSGarrett Wollman case IP_MULTICAST_VIF: 1569df8bae1dSRodney W. Grimes case IP_MULTICAST_TTL: 1570df8bae1dSRodney W. Grimes case IP_MULTICAST_LOOP: 1571df8bae1dSRodney W. Grimes case IP_ADD_MEMBERSHIP: 1572df8bae1dSRodney W. Grimes case IP_DROP_MEMBERSHIP: 1573cfe8b629SGarrett Wollman error = ip_getmoptions(sopt, inp->inp_moptions); 157433b3ac06SPeter Wemm break; 157533b3ac06SPeter Wemm 1576b9234fafSSam Leffler #if defined(IPSEC) || defined(FAST_IPSEC) 15776a800098SYoshinobu Inoue case IP_IPSEC_POLICY: 15786a800098SYoshinobu Inoue { 1579f63e7634SYoshinobu Inoue struct mbuf *m = NULL; 15806a800098SYoshinobu Inoue caddr_t req = NULL; 1581686cdd19SJun-ichiro itojun Hagino size_t len = 0; 15826a800098SYoshinobu Inoue 1583686cdd19SJun-ichiro itojun Hagino if (m != 0) { 15846a800098SYoshinobu Inoue req = mtod(m, caddr_t); 1585686cdd19SJun-ichiro itojun Hagino len = m->m_len; 1586686cdd19SJun-ichiro itojun Hagino } 1587686cdd19SJun-ichiro itojun Hagino error = ipsec4_get_policy(sotoinpcb(so), req, len, &m); 15886a800098SYoshinobu Inoue if (error == 0) 15896a800098SYoshinobu Inoue error = soopt_mcopyout(sopt, m); /* XXX */ 1590f63e7634SYoshinobu Inoue if (error == 0) 15916a800098SYoshinobu Inoue m_freem(m); 15926a800098SYoshinobu Inoue break; 15936a800098SYoshinobu Inoue } 15946a800098SYoshinobu Inoue #endif /*IPSEC*/ 15956a800098SYoshinobu Inoue 1596df8bae1dSRodney W. Grimes default: 1597df8bae1dSRodney W. Grimes error = ENOPROTOOPT; 1598df8bae1dSRodney W. Grimes break; 1599df8bae1dSRodney W. Grimes } 1600df8bae1dSRodney W. Grimes break; 1601df8bae1dSRodney W. Grimes } 1602df8bae1dSRodney W. Grimes return (error); 1603df8bae1dSRodney W. Grimes } 1604df8bae1dSRodney W. Grimes 1605df8bae1dSRodney W. Grimes /* 1606df8bae1dSRodney W. Grimes * Set up IP options in pcb for insertion in output packets. 1607df8bae1dSRodney W. Grimes * Store in mbuf with pointer in pcbopt, adding pseudo-option 1608df8bae1dSRodney W. Grimes * with destination address if source routed. 1609df8bae1dSRodney W. Grimes */ 16100312fbe9SPoul-Henning Kamp static int 1611df8bae1dSRodney W. Grimes ip_pcbopts(optname, pcbopt, m) 1612df8bae1dSRodney W. Grimes int optname; 1613df8bae1dSRodney W. Grimes struct mbuf **pcbopt; 1614df8bae1dSRodney W. Grimes register struct mbuf *m; 1615df8bae1dSRodney W. Grimes { 1616d68fa50cSBruce Evans register int cnt, optlen; 1617df8bae1dSRodney W. Grimes register u_char *cp; 1618df8bae1dSRodney W. Grimes u_char opt; 1619df8bae1dSRodney W. Grimes 1620df8bae1dSRodney W. Grimes /* turn off any old options */ 1621df8bae1dSRodney W. Grimes if (*pcbopt) 1622df8bae1dSRodney W. Grimes (void)m_free(*pcbopt); 1623df8bae1dSRodney W. Grimes *pcbopt = 0; 1624df8bae1dSRodney W. Grimes if (m == (struct mbuf *)0 || m->m_len == 0) { 1625df8bae1dSRodney W. Grimes /* 1626df8bae1dSRodney W. Grimes * Only turning off any previous options. 1627df8bae1dSRodney W. Grimes */ 1628df8bae1dSRodney W. Grimes if (m) 1629df8bae1dSRodney W. Grimes (void)m_free(m); 1630df8bae1dSRodney W. Grimes return (0); 1631df8bae1dSRodney W. Grimes } 1632df8bae1dSRodney W. Grimes 16330c8d2590SBruce Evans if (m->m_len % sizeof(int32_t)) 1634df8bae1dSRodney W. Grimes goto bad; 1635df8bae1dSRodney W. Grimes /* 1636df8bae1dSRodney W. Grimes * IP first-hop destination address will be stored before 1637df8bae1dSRodney W. Grimes * actual options; move other options back 1638df8bae1dSRodney W. Grimes * and clear it when none present. 1639df8bae1dSRodney W. Grimes */ 1640df8bae1dSRodney W. Grimes if (m->m_data + m->m_len + sizeof(struct in_addr) >= &m->m_dat[MLEN]) 1641df8bae1dSRodney W. Grimes goto bad; 1642df8bae1dSRodney W. Grimes cnt = m->m_len; 1643df8bae1dSRodney W. Grimes m->m_len += sizeof(struct in_addr); 1644df8bae1dSRodney W. Grimes cp = mtod(m, u_char *) + sizeof(struct in_addr); 1645df8bae1dSRodney W. Grimes ovbcopy(mtod(m, caddr_t), (caddr_t)cp, (unsigned)cnt); 1646df8bae1dSRodney W. Grimes bzero(mtod(m, caddr_t), sizeof(struct in_addr)); 1647df8bae1dSRodney W. Grimes 1648df8bae1dSRodney W. Grimes for (; cnt > 0; cnt -= optlen, cp += optlen) { 1649df8bae1dSRodney W. Grimes opt = cp[IPOPT_OPTVAL]; 1650df8bae1dSRodney W. Grimes if (opt == IPOPT_EOL) 1651df8bae1dSRodney W. Grimes break; 1652df8bae1dSRodney W. Grimes if (opt == IPOPT_NOP) 1653df8bae1dSRodney W. Grimes optlen = 1; 1654df8bae1dSRodney W. Grimes else { 1655707d00a3SJonathan Lemon if (cnt < IPOPT_OLEN + sizeof(*cp)) 1656707d00a3SJonathan Lemon goto bad; 1657df8bae1dSRodney W. Grimes optlen = cp[IPOPT_OLEN]; 1658707d00a3SJonathan Lemon if (optlen < IPOPT_OLEN + sizeof(*cp) || optlen > cnt) 1659df8bae1dSRodney W. Grimes goto bad; 1660df8bae1dSRodney W. Grimes } 1661df8bae1dSRodney W. Grimes switch (opt) { 1662df8bae1dSRodney W. Grimes 1663df8bae1dSRodney W. Grimes default: 1664df8bae1dSRodney W. Grimes break; 1665df8bae1dSRodney W. Grimes 1666df8bae1dSRodney W. Grimes case IPOPT_LSRR: 1667df8bae1dSRodney W. Grimes case IPOPT_SSRR: 1668df8bae1dSRodney W. Grimes /* 1669df8bae1dSRodney W. Grimes * user process specifies route as: 1670df8bae1dSRodney W. Grimes * ->A->B->C->D 1671df8bae1dSRodney W. Grimes * D must be our final destination (but we can't 1672df8bae1dSRodney W. Grimes * check that since we may not have connected yet). 1673df8bae1dSRodney W. Grimes * A is first hop destination, which doesn't appear in 1674df8bae1dSRodney W. Grimes * actual IP option, but is stored before the options. 1675df8bae1dSRodney W. Grimes */ 1676df8bae1dSRodney W. Grimes if (optlen < IPOPT_MINOFF - 1 + sizeof(struct in_addr)) 1677df8bae1dSRodney W. Grimes goto bad; 1678df8bae1dSRodney W. Grimes m->m_len -= sizeof(struct in_addr); 1679df8bae1dSRodney W. Grimes cnt -= sizeof(struct in_addr); 1680df8bae1dSRodney W. Grimes optlen -= sizeof(struct in_addr); 1681df8bae1dSRodney W. Grimes cp[IPOPT_OLEN] = optlen; 1682df8bae1dSRodney W. Grimes /* 1683df8bae1dSRodney W. Grimes * Move first hop before start of options. 1684df8bae1dSRodney W. Grimes */ 1685df8bae1dSRodney W. Grimes bcopy((caddr_t)&cp[IPOPT_OFFSET+1], mtod(m, caddr_t), 1686df8bae1dSRodney W. Grimes sizeof(struct in_addr)); 1687df8bae1dSRodney W. Grimes /* 1688df8bae1dSRodney W. Grimes * Then copy rest of options back 1689df8bae1dSRodney W. Grimes * to close up the deleted entry. 1690df8bae1dSRodney W. Grimes */ 1691df8bae1dSRodney W. Grimes ovbcopy((caddr_t)(&cp[IPOPT_OFFSET+1] + 1692df8bae1dSRodney W. Grimes sizeof(struct in_addr)), 1693df8bae1dSRodney W. Grimes (caddr_t)&cp[IPOPT_OFFSET+1], 1694df8bae1dSRodney W. Grimes (unsigned)cnt + sizeof(struct in_addr)); 1695df8bae1dSRodney W. Grimes break; 1696df8bae1dSRodney W. Grimes } 1697df8bae1dSRodney W. Grimes } 1698df8bae1dSRodney W. Grimes if (m->m_len > MAX_IPOPTLEN + sizeof(struct in_addr)) 1699df8bae1dSRodney W. Grimes goto bad; 1700df8bae1dSRodney W. Grimes *pcbopt = m; 1701df8bae1dSRodney W. Grimes return (0); 1702df8bae1dSRodney W. Grimes 1703df8bae1dSRodney W. Grimes bad: 1704df8bae1dSRodney W. Grimes (void)m_free(m); 1705df8bae1dSRodney W. Grimes return (EINVAL); 1706df8bae1dSRodney W. Grimes } 1707df8bae1dSRodney W. Grimes 1708df8bae1dSRodney W. Grimes /* 1709cfe8b629SGarrett Wollman * XXX 1710cfe8b629SGarrett Wollman * The whole multicast option thing needs to be re-thought. 1711cfe8b629SGarrett Wollman * Several of these options are equally applicable to non-multicast 1712cfe8b629SGarrett Wollman * transmission, and one (IP_MULTICAST_TTL) totally duplicates a 1713cfe8b629SGarrett Wollman * standard option (IP_TTL). 1714cfe8b629SGarrett Wollman */ 171533841545SHajimu UMEMOTO 171633841545SHajimu UMEMOTO /* 171733841545SHajimu UMEMOTO * following RFC1724 section 3.3, 0.0.0.0/8 is interpreted as interface index. 171833841545SHajimu UMEMOTO */ 171933841545SHajimu UMEMOTO static struct ifnet * 172033841545SHajimu UMEMOTO ip_multicast_if(a, ifindexp) 172133841545SHajimu UMEMOTO struct in_addr *a; 172233841545SHajimu UMEMOTO int *ifindexp; 172333841545SHajimu UMEMOTO { 172433841545SHajimu UMEMOTO int ifindex; 172533841545SHajimu UMEMOTO struct ifnet *ifp; 172633841545SHajimu UMEMOTO 172733841545SHajimu UMEMOTO if (ifindexp) 172833841545SHajimu UMEMOTO *ifindexp = 0; 172933841545SHajimu UMEMOTO if (ntohl(a->s_addr) >> 24 == 0) { 173033841545SHajimu UMEMOTO ifindex = ntohl(a->s_addr) & 0xffffff; 173133841545SHajimu UMEMOTO if (ifindex < 0 || if_index < ifindex) 173233841545SHajimu UMEMOTO return NULL; 1733f9132cebSJonathan Lemon ifp = ifnet_byindex(ifindex); 173433841545SHajimu UMEMOTO if (ifindexp) 173533841545SHajimu UMEMOTO *ifindexp = ifindex; 173633841545SHajimu UMEMOTO } else { 173733841545SHajimu UMEMOTO INADDR_TO_IFP(*a, ifp); 173833841545SHajimu UMEMOTO } 173933841545SHajimu UMEMOTO return ifp; 174033841545SHajimu UMEMOTO } 174133841545SHajimu UMEMOTO 1742cfe8b629SGarrett Wollman /* 1743df8bae1dSRodney W. Grimes * Set the IP multicast options in response to user setsockopt(). 1744df8bae1dSRodney W. Grimes */ 17450312fbe9SPoul-Henning Kamp static int 1746cfe8b629SGarrett Wollman ip_setmoptions(sopt, imop) 1747cfe8b629SGarrett Wollman struct sockopt *sopt; 1748df8bae1dSRodney W. Grimes struct ip_moptions **imop; 1749df8bae1dSRodney W. Grimes { 1750cfe8b629SGarrett Wollman int error = 0; 1751cfe8b629SGarrett Wollman int i; 1752df8bae1dSRodney W. Grimes struct in_addr addr; 1753cfe8b629SGarrett Wollman struct ip_mreq mreq; 1754cfe8b629SGarrett Wollman struct ifnet *ifp; 1755cfe8b629SGarrett Wollman struct ip_moptions *imo = *imop; 1756df8bae1dSRodney W. Grimes struct route ro; 1757cfe8b629SGarrett Wollman struct sockaddr_in *dst; 175833841545SHajimu UMEMOTO int ifindex; 17599b626c29SGarrett Wollman int s; 1760df8bae1dSRodney W. Grimes 1761df8bae1dSRodney W. Grimes if (imo == NULL) { 1762df8bae1dSRodney W. Grimes /* 1763df8bae1dSRodney W. Grimes * No multicast option buffer attached to the pcb; 1764df8bae1dSRodney W. Grimes * allocate one and initialize to default values. 1765df8bae1dSRodney W. Grimes */ 1766df8bae1dSRodney W. Grimes imo = (struct ip_moptions*)malloc(sizeof(*imo), M_IPMOPTS, 1767df8bae1dSRodney W. Grimes M_WAITOK); 1768df8bae1dSRodney W. Grimes 1769df8bae1dSRodney W. Grimes if (imo == NULL) 1770df8bae1dSRodney W. Grimes return (ENOBUFS); 1771df8bae1dSRodney W. Grimes *imop = imo; 1772df8bae1dSRodney W. Grimes imo->imo_multicast_ifp = NULL; 177333841545SHajimu UMEMOTO imo->imo_multicast_addr.s_addr = INADDR_ANY; 17741c5de19aSGarrett Wollman imo->imo_multicast_vif = -1; 1775df8bae1dSRodney W. Grimes imo->imo_multicast_ttl = IP_DEFAULT_MULTICAST_TTL; 1776df8bae1dSRodney W. Grimes imo->imo_multicast_loop = IP_DEFAULT_MULTICAST_LOOP; 1777df8bae1dSRodney W. Grimes imo->imo_num_memberships = 0; 1778df8bae1dSRodney W. Grimes } 1779df8bae1dSRodney W. Grimes 1780cfe8b629SGarrett Wollman switch (sopt->sopt_name) { 1781f0068c4aSGarrett Wollman /* store an index number for the vif you wanna use in the send */ 1782f0068c4aSGarrett Wollman case IP_MULTICAST_VIF: 1783cfe8b629SGarrett Wollman if (legal_vif_num == 0) { 17845e9ae478SGarrett Wollman error = EOPNOTSUPP; 17855e9ae478SGarrett Wollman break; 17865e9ae478SGarrett Wollman } 1787cfe8b629SGarrett Wollman error = sooptcopyin(sopt, &i, sizeof i, sizeof i); 1788cfe8b629SGarrett Wollman if (error) 1789f0068c4aSGarrett Wollman break; 17901c5de19aSGarrett Wollman if (!legal_vif_num(i) && (i != -1)) { 1791f0068c4aSGarrett Wollman error = EINVAL; 1792f0068c4aSGarrett Wollman break; 1793f0068c4aSGarrett Wollman } 1794f0068c4aSGarrett Wollman imo->imo_multicast_vif = i; 1795f0068c4aSGarrett Wollman break; 1796f0068c4aSGarrett Wollman 1797df8bae1dSRodney W. Grimes case IP_MULTICAST_IF: 1798df8bae1dSRodney W. Grimes /* 1799df8bae1dSRodney W. Grimes * Select the interface for outgoing multicast packets. 1800df8bae1dSRodney W. Grimes */ 1801cfe8b629SGarrett Wollman error = sooptcopyin(sopt, &addr, sizeof addr, sizeof addr); 1802cfe8b629SGarrett Wollman if (error) 1803df8bae1dSRodney W. Grimes break; 1804df8bae1dSRodney W. Grimes /* 1805df8bae1dSRodney W. Grimes * INADDR_ANY is used to remove a previous selection. 1806df8bae1dSRodney W. Grimes * When no interface is selected, a default one is 1807df8bae1dSRodney W. Grimes * chosen every time a multicast packet is sent. 1808df8bae1dSRodney W. Grimes */ 1809df8bae1dSRodney W. Grimes if (addr.s_addr == INADDR_ANY) { 1810df8bae1dSRodney W. Grimes imo->imo_multicast_ifp = NULL; 1811df8bae1dSRodney W. Grimes break; 1812df8bae1dSRodney W. Grimes } 1813df8bae1dSRodney W. Grimes /* 1814df8bae1dSRodney W. Grimes * The selected interface is identified by its local 1815df8bae1dSRodney W. Grimes * IP address. Find the interface and confirm that 1816df8bae1dSRodney W. Grimes * it supports multicasting. 1817df8bae1dSRodney W. Grimes */ 181820e8807cSGarrett Wollman s = splimp(); 181933841545SHajimu UMEMOTO ifp = ip_multicast_if(&addr, &ifindex); 1820df8bae1dSRodney W. Grimes if (ifp == NULL || (ifp->if_flags & IFF_MULTICAST) == 0) { 1821fbc6ab00SBill Fenner splx(s); 1822df8bae1dSRodney W. Grimes error = EADDRNOTAVAIL; 1823df8bae1dSRodney W. Grimes break; 1824df8bae1dSRodney W. Grimes } 1825df8bae1dSRodney W. Grimes imo->imo_multicast_ifp = ifp; 182633841545SHajimu UMEMOTO if (ifindex) 182733841545SHajimu UMEMOTO imo->imo_multicast_addr = addr; 182833841545SHajimu UMEMOTO else 182933841545SHajimu UMEMOTO imo->imo_multicast_addr.s_addr = INADDR_ANY; 18309b626c29SGarrett Wollman splx(s); 1831df8bae1dSRodney W. Grimes break; 1832df8bae1dSRodney W. Grimes 1833df8bae1dSRodney W. Grimes case IP_MULTICAST_TTL: 1834df8bae1dSRodney W. Grimes /* 1835df8bae1dSRodney W. Grimes * Set the IP time-to-live for outgoing multicast packets. 1836cfe8b629SGarrett Wollman * The original multicast API required a char argument, 1837cfe8b629SGarrett Wollman * which is inconsistent with the rest of the socket API. 1838cfe8b629SGarrett Wollman * We allow either a char or an int. 1839df8bae1dSRodney W. Grimes */ 1840cfe8b629SGarrett Wollman if (sopt->sopt_valsize == 1) { 1841cfe8b629SGarrett Wollman u_char ttl; 1842cfe8b629SGarrett Wollman error = sooptcopyin(sopt, &ttl, 1, 1); 1843cfe8b629SGarrett Wollman if (error) 1844df8bae1dSRodney W. Grimes break; 1845cfe8b629SGarrett Wollman imo->imo_multicast_ttl = ttl; 1846cfe8b629SGarrett Wollman } else { 1847cfe8b629SGarrett Wollman u_int ttl; 1848cfe8b629SGarrett Wollman error = sooptcopyin(sopt, &ttl, sizeof ttl, 1849cfe8b629SGarrett Wollman sizeof ttl); 1850cfe8b629SGarrett Wollman if (error) 1851cfe8b629SGarrett Wollman break; 1852cfe8b629SGarrett Wollman if (ttl > 255) 1853cfe8b629SGarrett Wollman error = EINVAL; 1854cfe8b629SGarrett Wollman else 1855cfe8b629SGarrett Wollman imo->imo_multicast_ttl = ttl; 1856df8bae1dSRodney W. Grimes } 1857df8bae1dSRodney W. Grimes break; 1858df8bae1dSRodney W. Grimes 1859df8bae1dSRodney W. Grimes case IP_MULTICAST_LOOP: 1860df8bae1dSRodney W. Grimes /* 1861df8bae1dSRodney W. Grimes * Set the loopback flag for outgoing multicast packets. 1862cfe8b629SGarrett Wollman * Must be zero or one. The original multicast API required a 1863cfe8b629SGarrett Wollman * char argument, which is inconsistent with the rest 1864cfe8b629SGarrett Wollman * of the socket API. We allow either a char or an int. 1865df8bae1dSRodney W. Grimes */ 1866cfe8b629SGarrett Wollman if (sopt->sopt_valsize == 1) { 1867cfe8b629SGarrett Wollman u_char loop; 1868cfe8b629SGarrett Wollman error = sooptcopyin(sopt, &loop, 1, 1); 1869cfe8b629SGarrett Wollman if (error) 1870df8bae1dSRodney W. Grimes break; 1871cfe8b629SGarrett Wollman imo->imo_multicast_loop = !!loop; 1872cfe8b629SGarrett Wollman } else { 1873cfe8b629SGarrett Wollman u_int loop; 1874cfe8b629SGarrett Wollman error = sooptcopyin(sopt, &loop, sizeof loop, 1875cfe8b629SGarrett Wollman sizeof loop); 1876cfe8b629SGarrett Wollman if (error) 1877cfe8b629SGarrett Wollman break; 1878cfe8b629SGarrett Wollman imo->imo_multicast_loop = !!loop; 1879df8bae1dSRodney W. Grimes } 1880df8bae1dSRodney W. Grimes break; 1881df8bae1dSRodney W. Grimes 1882df8bae1dSRodney W. Grimes case IP_ADD_MEMBERSHIP: 1883df8bae1dSRodney W. Grimes /* 1884df8bae1dSRodney W. Grimes * Add a multicast group membership. 1885df8bae1dSRodney W. Grimes * Group must be a valid IP multicast address. 1886df8bae1dSRodney W. Grimes */ 1887cfe8b629SGarrett Wollman error = sooptcopyin(sopt, &mreq, sizeof mreq, sizeof mreq); 1888cfe8b629SGarrett Wollman if (error) 1889df8bae1dSRodney W. Grimes break; 1890cfe8b629SGarrett Wollman 1891cfe8b629SGarrett Wollman if (!IN_MULTICAST(ntohl(mreq.imr_multiaddr.s_addr))) { 1892df8bae1dSRodney W. Grimes error = EINVAL; 1893df8bae1dSRodney W. Grimes break; 1894df8bae1dSRodney W. Grimes } 189520e8807cSGarrett Wollman s = splimp(); 1896df8bae1dSRodney W. Grimes /* 1897df8bae1dSRodney W. Grimes * If no interface address was provided, use the interface of 1898df8bae1dSRodney W. Grimes * the route to the given multicast address. 1899df8bae1dSRodney W. Grimes */ 1900cfe8b629SGarrett Wollman if (mreq.imr_interface.s_addr == INADDR_ANY) { 19011c5de19aSGarrett Wollman bzero((caddr_t)&ro, sizeof(ro)); 1902df8bae1dSRodney W. Grimes dst = (struct sockaddr_in *)&ro.ro_dst; 1903df8bae1dSRodney W. Grimes dst->sin_len = sizeof(*dst); 1904df8bae1dSRodney W. Grimes dst->sin_family = AF_INET; 1905cfe8b629SGarrett Wollman dst->sin_addr = mreq.imr_multiaddr; 1906df8bae1dSRodney W. Grimes rtalloc(&ro); 1907df8bae1dSRodney W. Grimes if (ro.ro_rt == NULL) { 1908df8bae1dSRodney W. Grimes error = EADDRNOTAVAIL; 19099b626c29SGarrett Wollman splx(s); 1910df8bae1dSRodney W. Grimes break; 1911df8bae1dSRodney W. Grimes } 1912df8bae1dSRodney W. Grimes ifp = ro.ro_rt->rt_ifp; 1913df8bae1dSRodney W. Grimes rtfree(ro.ro_rt); 1914df8bae1dSRodney W. Grimes } 1915df8bae1dSRodney W. Grimes else { 191633841545SHajimu UMEMOTO ifp = ip_multicast_if(&mreq.imr_interface, NULL); 1917df8bae1dSRodney W. Grimes } 19189b626c29SGarrett Wollman 1919df8bae1dSRodney W. Grimes /* 1920df8bae1dSRodney W. Grimes * See if we found an interface, and confirm that it 1921df8bae1dSRodney W. Grimes * supports multicast. 1922df8bae1dSRodney W. Grimes */ 1923df8bae1dSRodney W. Grimes if (ifp == NULL || (ifp->if_flags & IFF_MULTICAST) == 0) { 1924df8bae1dSRodney W. Grimes error = EADDRNOTAVAIL; 19259b626c29SGarrett Wollman splx(s); 1926df8bae1dSRodney W. Grimes break; 1927df8bae1dSRodney W. Grimes } 1928df8bae1dSRodney W. Grimes /* 1929df8bae1dSRodney W. Grimes * See if the membership already exists or if all the 1930df8bae1dSRodney W. Grimes * membership slots are full. 1931df8bae1dSRodney W. Grimes */ 1932df8bae1dSRodney W. Grimes for (i = 0; i < imo->imo_num_memberships; ++i) { 1933df8bae1dSRodney W. Grimes if (imo->imo_membership[i]->inm_ifp == ifp && 1934df8bae1dSRodney W. Grimes imo->imo_membership[i]->inm_addr.s_addr 1935cfe8b629SGarrett Wollman == mreq.imr_multiaddr.s_addr) 1936df8bae1dSRodney W. Grimes break; 1937df8bae1dSRodney W. Grimes } 1938df8bae1dSRodney W. Grimes if (i < imo->imo_num_memberships) { 1939df8bae1dSRodney W. Grimes error = EADDRINUSE; 19409b626c29SGarrett Wollman splx(s); 1941df8bae1dSRodney W. Grimes break; 1942df8bae1dSRodney W. Grimes } 1943df8bae1dSRodney W. Grimes if (i == IP_MAX_MEMBERSHIPS) { 1944df8bae1dSRodney W. Grimes error = ETOOMANYREFS; 19459b626c29SGarrett Wollman splx(s); 1946df8bae1dSRodney W. Grimes break; 1947df8bae1dSRodney W. Grimes } 1948df8bae1dSRodney W. Grimes /* 1949df8bae1dSRodney W. Grimes * Everything looks good; add a new record to the multicast 1950df8bae1dSRodney W. Grimes * address list for the given interface. 1951df8bae1dSRodney W. Grimes */ 1952df8bae1dSRodney W. Grimes if ((imo->imo_membership[i] = 1953cfe8b629SGarrett Wollman in_addmulti(&mreq.imr_multiaddr, ifp)) == NULL) { 1954df8bae1dSRodney W. Grimes error = ENOBUFS; 19559b626c29SGarrett Wollman splx(s); 1956df8bae1dSRodney W. Grimes break; 1957df8bae1dSRodney W. Grimes } 1958df8bae1dSRodney W. Grimes ++imo->imo_num_memberships; 19599b626c29SGarrett Wollman splx(s); 1960df8bae1dSRodney W. Grimes break; 1961df8bae1dSRodney W. Grimes 1962df8bae1dSRodney W. Grimes case IP_DROP_MEMBERSHIP: 1963df8bae1dSRodney W. Grimes /* 1964df8bae1dSRodney W. Grimes * Drop a multicast group membership. 1965df8bae1dSRodney W. Grimes * Group must be a valid IP multicast address. 1966df8bae1dSRodney W. Grimes */ 1967cfe8b629SGarrett Wollman error = sooptcopyin(sopt, &mreq, sizeof mreq, sizeof mreq); 1968cfe8b629SGarrett Wollman if (error) 1969df8bae1dSRodney W. Grimes break; 1970cfe8b629SGarrett Wollman 1971cfe8b629SGarrett Wollman if (!IN_MULTICAST(ntohl(mreq.imr_multiaddr.s_addr))) { 1972df8bae1dSRodney W. Grimes error = EINVAL; 1973df8bae1dSRodney W. Grimes break; 1974df8bae1dSRodney W. Grimes } 19759b626c29SGarrett Wollman 197620e8807cSGarrett Wollman s = splimp(); 1977df8bae1dSRodney W. Grimes /* 1978df8bae1dSRodney W. Grimes * If an interface address was specified, get a pointer 1979df8bae1dSRodney W. Grimes * to its ifnet structure. 1980df8bae1dSRodney W. Grimes */ 1981cfe8b629SGarrett Wollman if (mreq.imr_interface.s_addr == INADDR_ANY) 1982df8bae1dSRodney W. Grimes ifp = NULL; 1983df8bae1dSRodney W. Grimes else { 198433841545SHajimu UMEMOTO ifp = ip_multicast_if(&mreq.imr_interface, NULL); 1985df8bae1dSRodney W. Grimes if (ifp == NULL) { 1986df8bae1dSRodney W. Grimes error = EADDRNOTAVAIL; 19879b626c29SGarrett Wollman splx(s); 1988df8bae1dSRodney W. Grimes break; 1989df8bae1dSRodney W. Grimes } 1990df8bae1dSRodney W. Grimes } 1991df8bae1dSRodney W. Grimes /* 1992df8bae1dSRodney W. Grimes * Find the membership in the membership array. 1993df8bae1dSRodney W. Grimes */ 1994df8bae1dSRodney W. Grimes for (i = 0; i < imo->imo_num_memberships; ++i) { 1995df8bae1dSRodney W. Grimes if ((ifp == NULL || 1996df8bae1dSRodney W. Grimes imo->imo_membership[i]->inm_ifp == ifp) && 1997df8bae1dSRodney W. Grimes imo->imo_membership[i]->inm_addr.s_addr == 1998cfe8b629SGarrett Wollman mreq.imr_multiaddr.s_addr) 1999df8bae1dSRodney W. Grimes break; 2000df8bae1dSRodney W. Grimes } 2001df8bae1dSRodney W. Grimes if (i == imo->imo_num_memberships) { 2002df8bae1dSRodney W. Grimes error = EADDRNOTAVAIL; 20039b626c29SGarrett Wollman splx(s); 2004df8bae1dSRodney W. Grimes break; 2005df8bae1dSRodney W. Grimes } 2006df8bae1dSRodney W. Grimes /* 2007df8bae1dSRodney W. Grimes * Give up the multicast address record to which the 2008df8bae1dSRodney W. Grimes * membership points. 2009df8bae1dSRodney W. Grimes */ 2010df8bae1dSRodney W. Grimes in_delmulti(imo->imo_membership[i]); 2011df8bae1dSRodney W. Grimes /* 2012df8bae1dSRodney W. Grimes * Remove the gap in the membership array. 2013df8bae1dSRodney W. Grimes */ 2014df8bae1dSRodney W. Grimes for (++i; i < imo->imo_num_memberships; ++i) 2015df8bae1dSRodney W. Grimes imo->imo_membership[i-1] = imo->imo_membership[i]; 2016df8bae1dSRodney W. Grimes --imo->imo_num_memberships; 20179b626c29SGarrett Wollman splx(s); 2018df8bae1dSRodney W. Grimes break; 2019df8bae1dSRodney W. Grimes 2020df8bae1dSRodney W. Grimes default: 2021df8bae1dSRodney W. Grimes error = EOPNOTSUPP; 2022df8bae1dSRodney W. Grimes break; 2023df8bae1dSRodney W. Grimes } 2024df8bae1dSRodney W. Grimes 2025df8bae1dSRodney W. Grimes /* 2026df8bae1dSRodney W. Grimes * If all options have default values, no need to keep the mbuf. 2027df8bae1dSRodney W. Grimes */ 2028df8bae1dSRodney W. Grimes if (imo->imo_multicast_ifp == NULL && 20291c5de19aSGarrett Wollman imo->imo_multicast_vif == -1 && 2030df8bae1dSRodney W. Grimes imo->imo_multicast_ttl == IP_DEFAULT_MULTICAST_TTL && 2031df8bae1dSRodney W. Grimes imo->imo_multicast_loop == IP_DEFAULT_MULTICAST_LOOP && 2032df8bae1dSRodney W. Grimes imo->imo_num_memberships == 0) { 2033df8bae1dSRodney W. Grimes free(*imop, M_IPMOPTS); 2034df8bae1dSRodney W. Grimes *imop = NULL; 2035df8bae1dSRodney W. Grimes } 2036df8bae1dSRodney W. Grimes 2037df8bae1dSRodney W. Grimes return (error); 2038df8bae1dSRodney W. Grimes } 2039df8bae1dSRodney W. Grimes 2040df8bae1dSRodney W. Grimes /* 2041df8bae1dSRodney W. Grimes * Return the IP multicast options in response to user getsockopt(). 2042df8bae1dSRodney W. Grimes */ 20430312fbe9SPoul-Henning Kamp static int 2044cfe8b629SGarrett Wollman ip_getmoptions(sopt, imo) 2045cfe8b629SGarrett Wollman struct sockopt *sopt; 2046df8bae1dSRodney W. Grimes register struct ip_moptions *imo; 2047df8bae1dSRodney W. Grimes { 2048cfe8b629SGarrett Wollman struct in_addr addr; 2049df8bae1dSRodney W. Grimes struct in_ifaddr *ia; 2050cfe8b629SGarrett Wollman int error, optval; 2051cfe8b629SGarrett Wollman u_char coptval; 2052df8bae1dSRodney W. Grimes 2053cfe8b629SGarrett Wollman error = 0; 2054cfe8b629SGarrett Wollman switch (sopt->sopt_name) { 2055f0068c4aSGarrett Wollman case IP_MULTICAST_VIF: 2056f0068c4aSGarrett Wollman if (imo != NULL) 2057cfe8b629SGarrett Wollman optval = imo->imo_multicast_vif; 2058f0068c4aSGarrett Wollman else 2059cfe8b629SGarrett Wollman optval = -1; 2060cfe8b629SGarrett Wollman error = sooptcopyout(sopt, &optval, sizeof optval); 2061cfe8b629SGarrett Wollman break; 2062f0068c4aSGarrett Wollman 2063df8bae1dSRodney W. Grimes case IP_MULTICAST_IF: 2064df8bae1dSRodney W. Grimes if (imo == NULL || imo->imo_multicast_ifp == NULL) 2065cfe8b629SGarrett Wollman addr.s_addr = INADDR_ANY; 206633841545SHajimu UMEMOTO else if (imo->imo_multicast_addr.s_addr) { 206733841545SHajimu UMEMOTO /* return the value user has set */ 206833841545SHajimu UMEMOTO addr = imo->imo_multicast_addr; 206933841545SHajimu UMEMOTO } else { 2070df8bae1dSRodney W. Grimes IFP_TO_IA(imo->imo_multicast_ifp, ia); 2071cfe8b629SGarrett Wollman addr.s_addr = (ia == NULL) ? INADDR_ANY 2072df8bae1dSRodney W. Grimes : IA_SIN(ia)->sin_addr.s_addr; 2073df8bae1dSRodney W. Grimes } 2074cfe8b629SGarrett Wollman error = sooptcopyout(sopt, &addr, sizeof addr); 2075cfe8b629SGarrett Wollman break; 2076df8bae1dSRodney W. Grimes 2077df8bae1dSRodney W. Grimes case IP_MULTICAST_TTL: 2078cfe8b629SGarrett Wollman if (imo == 0) 2079cfe8b629SGarrett Wollman optval = coptval = IP_DEFAULT_MULTICAST_TTL; 2080cfe8b629SGarrett Wollman else 2081cfe8b629SGarrett Wollman optval = coptval = imo->imo_multicast_ttl; 2082cfe8b629SGarrett Wollman if (sopt->sopt_valsize == 1) 2083cfe8b629SGarrett Wollman error = sooptcopyout(sopt, &coptval, 1); 2084cfe8b629SGarrett Wollman else 2085cfe8b629SGarrett Wollman error = sooptcopyout(sopt, &optval, sizeof optval); 2086cfe8b629SGarrett Wollman break; 2087df8bae1dSRodney W. Grimes 2088df8bae1dSRodney W. Grimes case IP_MULTICAST_LOOP: 2089cfe8b629SGarrett Wollman if (imo == 0) 2090cfe8b629SGarrett Wollman optval = coptval = IP_DEFAULT_MULTICAST_LOOP; 2091cfe8b629SGarrett Wollman else 2092cfe8b629SGarrett Wollman optval = coptval = imo->imo_multicast_loop; 2093cfe8b629SGarrett Wollman if (sopt->sopt_valsize == 1) 2094cfe8b629SGarrett Wollman error = sooptcopyout(sopt, &coptval, 1); 2095cfe8b629SGarrett Wollman else 2096cfe8b629SGarrett Wollman error = sooptcopyout(sopt, &optval, sizeof optval); 2097cfe8b629SGarrett Wollman break; 2098df8bae1dSRodney W. Grimes 2099df8bae1dSRodney W. Grimes default: 2100cfe8b629SGarrett Wollman error = ENOPROTOOPT; 2101cfe8b629SGarrett Wollman break; 2102df8bae1dSRodney W. Grimes } 2103cfe8b629SGarrett Wollman return (error); 2104df8bae1dSRodney W. Grimes } 2105df8bae1dSRodney W. Grimes 2106df8bae1dSRodney W. Grimes /* 2107df8bae1dSRodney W. Grimes * Discard the IP multicast options. 2108df8bae1dSRodney W. Grimes */ 2109df8bae1dSRodney W. Grimes void 2110df8bae1dSRodney W. Grimes ip_freemoptions(imo) 2111df8bae1dSRodney W. Grimes register struct ip_moptions *imo; 2112df8bae1dSRodney W. Grimes { 2113df8bae1dSRodney W. Grimes register int i; 2114df8bae1dSRodney W. Grimes 2115df8bae1dSRodney W. Grimes if (imo != NULL) { 2116df8bae1dSRodney W. Grimes for (i = 0; i < imo->imo_num_memberships; ++i) 2117df8bae1dSRodney W. Grimes in_delmulti(imo->imo_membership[i]); 2118df8bae1dSRodney W. Grimes free(imo, M_IPMOPTS); 2119df8bae1dSRodney W. Grimes } 2120df8bae1dSRodney W. Grimes } 2121df8bae1dSRodney W. Grimes 2122df8bae1dSRodney W. Grimes /* 2123df8bae1dSRodney W. Grimes * Routine called from ip_output() to loop back a copy of an IP multicast 2124df8bae1dSRodney W. Grimes * packet to the input queue of a specified interface. Note that this 2125df8bae1dSRodney W. Grimes * calls the output routine of the loopback "driver", but with an interface 2126f5fea3ddSPaul Traina * pointer that might NOT be a loopback interface -- evil, but easier than 2127f5fea3ddSPaul Traina * replicating that code here. 2128df8bae1dSRodney W. Grimes */ 2129df8bae1dSRodney W. Grimes static void 213086b1d6d2SBill Fenner ip_mloopback(ifp, m, dst, hlen) 2131df8bae1dSRodney W. Grimes struct ifnet *ifp; 2132df8bae1dSRodney W. Grimes register struct mbuf *m; 2133df8bae1dSRodney W. Grimes register struct sockaddr_in *dst; 213486b1d6d2SBill Fenner int hlen; 2135df8bae1dSRodney W. Grimes { 2136df8bae1dSRodney W. Grimes register struct ip *ip; 2137df8bae1dSRodney W. Grimes struct mbuf *copym; 2138df8bae1dSRodney W. Grimes 2139df8bae1dSRodney W. Grimes copym = m_copy(m, 0, M_COPYALL); 214086b1d6d2SBill Fenner if (copym != NULL && (copym->m_flags & M_EXT || copym->m_len < hlen)) 214186b1d6d2SBill Fenner copym = m_pullup(copym, hlen); 2142df8bae1dSRodney W. Grimes if (copym != NULL) { 2143df8bae1dSRodney W. Grimes /* 2144df8bae1dSRodney W. Grimes * We don't bother to fragment if the IP length is greater 2145df8bae1dSRodney W. Grimes * than the interface's MTU. Can this possibly matter? 2146df8bae1dSRodney W. Grimes */ 2147df8bae1dSRodney W. Grimes ip = mtod(copym, struct ip *); 2148fd8e4ebcSMike Barcroft ip->ip_len = htons(ip->ip_len); 2149fd8e4ebcSMike Barcroft ip->ip_off = htons(ip->ip_off); 2150df8bae1dSRodney W. Grimes ip->ip_sum = 0; 215186b1d6d2SBill Fenner ip->ip_sum = in_cksum(copym, hlen); 21529c9137eaSGarrett Wollman /* 21539c9137eaSGarrett Wollman * NB: 2154e1596dffSBill Fenner * It's not clear whether there are any lingering 2155e1596dffSBill Fenner * reentrancy problems in other areas which might 2156e1596dffSBill Fenner * be exposed by using ip_input directly (in 2157e1596dffSBill Fenner * particular, everything which modifies the packet 2158e1596dffSBill Fenner * in-place). Yet another option is using the 2159e1596dffSBill Fenner * protosw directly to deliver the looped back 2160e1596dffSBill Fenner * packet. For the moment, we'll err on the side 2161ed7509acSJulian Elischer * of safety by using if_simloop(). 21629c9137eaSGarrett Wollman */ 2163201c2527SJulian Elischer #if 1 /* XXX */ 2164201c2527SJulian Elischer if (dst->sin_family != AF_INET) { 21652b8a366cSJulian Elischer printf("ip_mloopback: bad address family %d\n", 2166201c2527SJulian Elischer dst->sin_family); 2167201c2527SJulian Elischer dst->sin_family = AF_INET; 2168201c2527SJulian Elischer } 2169201c2527SJulian Elischer #endif 2170201c2527SJulian Elischer 21719c9137eaSGarrett Wollman #ifdef notdef 2172e1596dffSBill Fenner copym->m_pkthdr.rcvif = ifp; 2173ed7509acSJulian Elischer ip_input(copym); 21749c9137eaSGarrett Wollman #else 217550c6dc99SJonathan Lemon /* if the checksum hasn't been computed, mark it as valid */ 217650c6dc99SJonathan Lemon if (copym->m_pkthdr.csum_flags & CSUM_DELAY_DATA) { 217750c6dc99SJonathan Lemon copym->m_pkthdr.csum_flags |= 217850c6dc99SJonathan Lemon CSUM_DATA_VALID | CSUM_PSEUDO_HDR; 217950c6dc99SJonathan Lemon copym->m_pkthdr.csum_data = 0xffff; 218050c6dc99SJonathan Lemon } 218106a429a3SArchie Cobbs if_simloop(ifp, copym, dst->sin_family, 0); 21829c9137eaSGarrett Wollman #endif 2183df8bae1dSRodney W. Grimes } 2184df8bae1dSRodney W. Grimes } 2185