1df8bae1dSRodney W. Grimes /* 2df8bae1dSRodney W. Grimes * Copyright (c) 1982, 1986, 1988, 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_input.c 8.2 (Berkeley) 1/4/94 340312fbe9SPoul-Henning Kamp * $Id: ip_input.c,v 1.27 1995/11/01 17:18:27 wollman Exp $ 35df8bae1dSRodney W. Grimes */ 36df8bae1dSRodney W. Grimes 37df8bae1dSRodney W. Grimes #include <sys/param.h> 38df8bae1dSRodney W. Grimes #include <sys/systm.h> 39df8bae1dSRodney W. Grimes #include <sys/malloc.h> 40df8bae1dSRodney W. Grimes #include <sys/mbuf.h> 41df8bae1dSRodney W. Grimes #include <sys/domain.h> 42df8bae1dSRodney W. Grimes #include <sys/protosw.h> 43df8bae1dSRodney W. Grimes #include <sys/socket.h> 44df8bae1dSRodney W. Grimes #include <sys/errno.h> 45df8bae1dSRodney W. Grimes #include <sys/time.h> 46df8bae1dSRodney W. Grimes #include <sys/kernel.h> 471025071fSGarrett Wollman #include <sys/syslog.h> 481025071fSGarrett Wollman 49b5e8ce9fSBruce Evans #include <vm/vm.h> 50b5e8ce9fSBruce Evans #include <sys/sysctl.h> 51df8bae1dSRodney W. Grimes 52df8bae1dSRodney W. Grimes #include <net/if.h> 53df8bae1dSRodney W. Grimes #include <net/route.h> 54748e0b0aSGarrett Wollman #include <net/netisr.h> 55df8bae1dSRodney W. Grimes 56df8bae1dSRodney W. Grimes #include <netinet/in.h> 57df8bae1dSRodney W. Grimes #include <netinet/in_systm.h> 58b5e8ce9fSBruce Evans #include <netinet/in_var.h> 59df8bae1dSRodney W. Grimes #include <netinet/ip.h> 60df8bae1dSRodney W. Grimes #include <netinet/in_pcb.h> 61df8bae1dSRodney W. Grimes #include <netinet/in_var.h> 62df8bae1dSRodney W. Grimes #include <netinet/ip_var.h> 63df8bae1dSRodney W. Grimes #include <netinet/ip_icmp.h> 64df8bae1dSRodney W. Grimes 65100ba1a6SJordan K. Hubbard #include <netinet/ip_fw.h> 66100ba1a6SJordan K. Hubbard 67f0068c4aSGarrett Wollman #include <sys/socketvar.h> 681c5de19aSGarrett Wollman int rsvp_on = 0; 691c5de19aSGarrett Wollman int ip_rsvp_on; 70f0068c4aSGarrett Wollman struct socket *ip_rsvpd; 71f0068c4aSGarrett Wollman 72df8bae1dSRodney W. Grimes #ifndef IPFORWARDING 73df8bae1dSRodney W. Grimes #ifdef GATEWAY 74df8bae1dSRodney W. Grimes #define IPFORWARDING 1 /* forward IP packets not for us */ 75df8bae1dSRodney W. Grimes #else /* GATEWAY */ 76df8bae1dSRodney W. Grimes #define IPFORWARDING 0 /* don't forward IP packets not for us */ 77df8bae1dSRodney W. Grimes #endif /* GATEWAY */ 78df8bae1dSRodney W. Grimes #endif /* IPFORWARDING */ 79df8bae1dSRodney W. Grimes #ifndef IPSENDREDIRECTS 80df8bae1dSRodney W. Grimes #define IPSENDREDIRECTS 1 81df8bae1dSRodney W. Grimes #endif 8242c03a52SPeter Wemm #ifndef DIRECTED_BROADCAST 8342c03a52SPeter Wemm #define DIRECTED_BROADCAST 0 8442c03a52SPeter Wemm #endif 850312fbe9SPoul-Henning Kamp 860312fbe9SPoul-Henning Kamp static int ipforwarding = IPFORWARDING; 870312fbe9SPoul-Henning Kamp SYSCTL_INT(_net_inet_ip, IPCTL_FORWARDING, forwarding, CTLFLAG_RW, 880312fbe9SPoul-Henning Kamp &ipforwarding, 0, ""); 890312fbe9SPoul-Henning Kamp 900312fbe9SPoul-Henning Kamp static int ipsendredirects = IPSENDREDIRECTS; 910312fbe9SPoul-Henning Kamp SYSCTL_INT(_net_inet_ip, IPCTL_SENDREDIRECTS, redirect, CTLFLAG_RW, 920312fbe9SPoul-Henning Kamp &ipsendredirects, 0, ""); 930312fbe9SPoul-Henning Kamp 940312fbe9SPoul-Henning Kamp static int ipdirbroadcast = DIRECTED_BROADCAST; 950312fbe9SPoul-Henning Kamp SYSCTL_INT(_net_inet_ip, IPCTL_DIRECTEDBROADCAST, directed_broadcast, 960312fbe9SPoul-Henning Kamp CTLFLAG_RW, &ipdirbroadcast, 0, ""); 970312fbe9SPoul-Henning Kamp 98df8bae1dSRodney W. Grimes int ip_defttl = IPDEFTTL; 990312fbe9SPoul-Henning Kamp SYSCTL_INT(_net_inet_ip, IPCTL_DEFTTL, ttl, CTLFLAG_RW, 1000312fbe9SPoul-Henning Kamp &ip_defttl, 0, ""); 1010312fbe9SPoul-Henning Kamp 1020312fbe9SPoul-Henning Kamp static int ip_dosourceroute = 0; 1030312fbe9SPoul-Henning Kamp SYSCTL_INT(_net_inet_ip, IPCTL_SOURCEROUTE, sourceroute, CTLFLAG_RW, 1040312fbe9SPoul-Henning Kamp &ip_dosourceroute, 0, ""); 105df8bae1dSRodney W. Grimes #ifdef DIAGNOSTIC 1060312fbe9SPoul-Henning Kamp static int ipprintfs = 0; 107df8bae1dSRodney W. Grimes #endif 108df8bae1dSRodney W. Grimes 109df8bae1dSRodney W. Grimes extern struct domain inetdomain; 110df8bae1dSRodney W. Grimes extern struct protosw inetsw[]; 111df8bae1dSRodney W. Grimes u_char ip_protox[IPPROTO_MAX]; 1120312fbe9SPoul-Henning Kamp static int ipqmaxlen = IFQ_MAXLEN; 113df8bae1dSRodney W. Grimes struct in_ifaddr *in_ifaddr; /* first inet address */ 114df8bae1dSRodney W. Grimes struct ifqueue ipintrq; 1150312fbe9SPoul-Henning Kamp SYSCTL_INT(_net_inet_ip, IPCTL_INTRQMAXLEN, intr_queue_maxlen, CTLFLAG_RD, 1160312fbe9SPoul-Henning Kamp &ipintrq.ifq_maxlen, 0, ""); 1170312fbe9SPoul-Henning Kamp SYSCTL_INT(_net_inet_ip, IPCTL_INTRQDROPS, intr_queue_drops, CTLFLAG_RD, 1180312fbe9SPoul-Henning Kamp &ipintrq.ifq_drops, 0, ""); 119df8bae1dSRodney W. Grimes 120f23b4c91SGarrett Wollman struct ipstat ipstat; 121f23b4c91SGarrett Wollman struct ipq ipq; 122f23b4c91SGarrett Wollman 1230312fbe9SPoul-Henning Kamp #ifdef IPCTL_DEFMTU 1240312fbe9SPoul-Henning Kamp SYSCTL_INT(_net_inet_ip, IPCTL_DEFMTU, mtu, CTLFLAG_RW, 1250312fbe9SPoul-Henning Kamp &ip_mtu, 0, ""); 1260312fbe9SPoul-Henning Kamp #endif 1270312fbe9SPoul-Henning Kamp 128df8bae1dSRodney W. Grimes /* 129df8bae1dSRodney W. Grimes * We need to save the IP options in case a protocol wants to respond 130df8bae1dSRodney W. Grimes * to an incoming packet over the same route if the packet got here 131df8bae1dSRodney W. Grimes * using IP source routing. This allows connection establishment and 132df8bae1dSRodney W. Grimes * maintenance when the remote end is on a network that is not known 133df8bae1dSRodney W. Grimes * to us. 134df8bae1dSRodney W. Grimes */ 1350312fbe9SPoul-Henning Kamp static int ip_nhops = 0; 136df8bae1dSRodney W. Grimes static struct ip_srcrt { 137df8bae1dSRodney W. Grimes struct in_addr dst; /* final destination */ 138df8bae1dSRodney W. Grimes char nop; /* one NOP to align */ 139df8bae1dSRodney W. Grimes char srcopt[IPOPT_OFFSET + 1]; /* OPTVAL, OLEN and OFFSET */ 140df8bae1dSRodney W. Grimes struct in_addr route[MAX_IPOPTLEN/sizeof(struct in_addr)]; 141df8bae1dSRodney W. Grimes } ip_srcrt; 142df8bae1dSRodney W. Grimes 143df8bae1dSRodney W. Grimes static void save_rte __P((u_char *, struct in_addr)); 1440312fbe9SPoul-Henning Kamp static void ip_deq __P((struct ipasfrag *)); 1450312fbe9SPoul-Henning Kamp static int ip_dooptions __P((struct mbuf *)); 1460312fbe9SPoul-Henning Kamp static void ip_enq __P((struct ipasfrag *, struct ipasfrag *)); 1470312fbe9SPoul-Henning Kamp static void ip_forward __P((struct mbuf *, int)); 1480312fbe9SPoul-Henning Kamp static void ip_freef __P((struct ipq *)); 1490312fbe9SPoul-Henning Kamp static struct ip * 1500312fbe9SPoul-Henning Kamp ip_reass __P((struct ipasfrag *, struct ipq *)); 1510312fbe9SPoul-Henning Kamp static struct in_ifaddr * 1520312fbe9SPoul-Henning Kamp ip_rtaddr __P((struct in_addr)); 1530312fbe9SPoul-Henning Kamp static void ipintr __P((void)); 154df8bae1dSRodney W. Grimes /* 155df8bae1dSRodney W. Grimes * IP initialization: fill in IP protocol switch table. 156df8bae1dSRodney W. Grimes * All protocols not implemented in kernel go to raw IP protocol handler. 157df8bae1dSRodney W. Grimes */ 158df8bae1dSRodney W. Grimes void 159df8bae1dSRodney W. Grimes ip_init() 160df8bae1dSRodney W. Grimes { 161df8bae1dSRodney W. Grimes register struct protosw *pr; 162df8bae1dSRodney W. Grimes register int i; 163df8bae1dSRodney W. Grimes 164df8bae1dSRodney W. Grimes pr = pffindproto(PF_INET, IPPROTO_RAW, SOCK_RAW); 165df8bae1dSRodney W. Grimes if (pr == 0) 166df8bae1dSRodney W. Grimes panic("ip_init"); 167df8bae1dSRodney W. Grimes for (i = 0; i < IPPROTO_MAX; i++) 168df8bae1dSRodney W. Grimes ip_protox[i] = pr - inetsw; 169df8bae1dSRodney W. Grimes for (pr = inetdomain.dom_protosw; 170df8bae1dSRodney W. Grimes pr < inetdomain.dom_protoswNPROTOSW; pr++) 171df8bae1dSRodney W. Grimes if (pr->pr_domain->dom_family == PF_INET && 172df8bae1dSRodney W. Grimes pr->pr_protocol && pr->pr_protocol != IPPROTO_RAW) 173df8bae1dSRodney W. Grimes ip_protox[pr->pr_protocol] = pr - inetsw; 174df8bae1dSRodney W. Grimes ipq.next = ipq.prev = &ipq; 175df8bae1dSRodney W. Grimes ip_id = time.tv_sec & 0xffff; 176df8bae1dSRodney W. Grimes ipintrq.ifq_maxlen = ipqmaxlen; 177df8bae1dSRodney W. Grimes } 178df8bae1dSRodney W. Grimes 1790312fbe9SPoul-Henning Kamp static struct sockaddr_in ipaddr = { sizeof(ipaddr), AF_INET }; 180df8bae1dSRodney W. Grimes struct route ipforward_rt; 181df8bae1dSRodney W. Grimes 182df8bae1dSRodney W. Grimes /* 183df8bae1dSRodney W. Grimes * Ip input routine. Checksum and byte swap header. If fragmented 184df8bae1dSRodney W. Grimes * try to reassemble. Process options. Pass to next level. 185df8bae1dSRodney W. Grimes */ 1860312fbe9SPoul-Henning Kamp static void 187748e0b0aSGarrett Wollman ipintr(void) 188df8bae1dSRodney W. Grimes { 189df8bae1dSRodney W. Grimes register struct ip *ip; 190df8bae1dSRodney W. Grimes register struct mbuf *m; 191df8bae1dSRodney W. Grimes register struct ipq *fp; 192df8bae1dSRodney W. Grimes register struct in_ifaddr *ia; 193df8bae1dSRodney W. Grimes int hlen, s; 194df8bae1dSRodney W. Grimes 195df8bae1dSRodney W. Grimes next: 196df8bae1dSRodney W. Grimes /* 197df8bae1dSRodney W. Grimes * Get next datagram off input queue and get IP header 198df8bae1dSRodney W. Grimes * in first mbuf. 199df8bae1dSRodney W. Grimes */ 200df8bae1dSRodney W. Grimes s = splimp(); 201df8bae1dSRodney W. Grimes IF_DEQUEUE(&ipintrq, m); 202df8bae1dSRodney W. Grimes splx(s); 203df8bae1dSRodney W. Grimes if (m == 0) 204df8bae1dSRodney W. Grimes return; 205df8bae1dSRodney W. Grimes #ifdef DIAGNOSTIC 206df8bae1dSRodney W. Grimes if ((m->m_flags & M_PKTHDR) == 0) 207df8bae1dSRodney W. Grimes panic("ipintr no HDR"); 208df8bae1dSRodney W. Grimes #endif 209df8bae1dSRodney W. Grimes /* 210df8bae1dSRodney W. Grimes * If no IP addresses have been set yet but the interfaces 211df8bae1dSRodney W. Grimes * are receiving, can't do anything with incoming packets yet. 212df8bae1dSRodney W. Grimes */ 213df8bae1dSRodney W. Grimes if (in_ifaddr == NULL) 214df8bae1dSRodney W. Grimes goto bad; 215df8bae1dSRodney W. Grimes ipstat.ips_total++; 216df8bae1dSRodney W. Grimes if (m->m_len < sizeof (struct ip) && 217df8bae1dSRodney W. Grimes (m = m_pullup(m, sizeof (struct ip))) == 0) { 218df8bae1dSRodney W. Grimes ipstat.ips_toosmall++; 219df8bae1dSRodney W. Grimes goto next; 220df8bae1dSRodney W. Grimes } 221df8bae1dSRodney W. Grimes ip = mtod(m, struct ip *); 222df8bae1dSRodney W. Grimes if (ip->ip_v != IPVERSION) { 223df8bae1dSRodney W. Grimes ipstat.ips_badvers++; 224df8bae1dSRodney W. Grimes goto bad; 225df8bae1dSRodney W. Grimes } 226df8bae1dSRodney W. Grimes hlen = ip->ip_hl << 2; 227df8bae1dSRodney W. Grimes if (hlen < sizeof(struct ip)) { /* minimum header length */ 228df8bae1dSRodney W. Grimes ipstat.ips_badhlen++; 229df8bae1dSRodney W. Grimes goto bad; 230df8bae1dSRodney W. Grimes } 231df8bae1dSRodney W. Grimes if (hlen > m->m_len) { 232df8bae1dSRodney W. Grimes if ((m = m_pullup(m, hlen)) == 0) { 233df8bae1dSRodney W. Grimes ipstat.ips_badhlen++; 234df8bae1dSRodney W. Grimes goto next; 235df8bae1dSRodney W. Grimes } 236df8bae1dSRodney W. Grimes ip = mtod(m, struct ip *); 237df8bae1dSRodney W. Grimes } 238623ae52eSPoul-Henning Kamp ip->ip_sum = in_cksum(m, hlen); 239623ae52eSPoul-Henning Kamp if (ip->ip_sum) { 240df8bae1dSRodney W. Grimes ipstat.ips_badsum++; 241df8bae1dSRodney W. Grimes goto bad; 242df8bae1dSRodney W. Grimes } 243df8bae1dSRodney W. Grimes 244df8bae1dSRodney W. Grimes /* 245df8bae1dSRodney W. Grimes * Convert fields to host representation. 246df8bae1dSRodney W. Grimes */ 247df8bae1dSRodney W. Grimes NTOHS(ip->ip_len); 248df8bae1dSRodney W. Grimes if (ip->ip_len < hlen) { 249df8bae1dSRodney W. Grimes ipstat.ips_badlen++; 250df8bae1dSRodney W. Grimes goto bad; 251df8bae1dSRodney W. Grimes } 252df8bae1dSRodney W. Grimes NTOHS(ip->ip_id); 253df8bae1dSRodney W. Grimes NTOHS(ip->ip_off); 254df8bae1dSRodney W. Grimes 255df8bae1dSRodney W. Grimes /* 256df8bae1dSRodney W. Grimes * Check that the amount of data in the buffers 257df8bae1dSRodney W. Grimes * is as at least much as the IP header would have us expect. 258df8bae1dSRodney W. Grimes * Trim mbufs if longer than we expect. 259df8bae1dSRodney W. Grimes * Drop packet if shorter than we expect. 260df8bae1dSRodney W. Grimes */ 261df8bae1dSRodney W. Grimes if (m->m_pkthdr.len < ip->ip_len) { 262df8bae1dSRodney W. Grimes ipstat.ips_tooshort++; 263df8bae1dSRodney W. Grimes goto bad; 264df8bae1dSRodney W. Grimes } 265df8bae1dSRodney W. Grimes if (m->m_pkthdr.len > ip->ip_len) { 266df8bae1dSRodney W. Grimes if (m->m_len == m->m_pkthdr.len) { 267df8bae1dSRodney W. Grimes m->m_len = ip->ip_len; 268df8bae1dSRodney W. Grimes m->m_pkthdr.len = ip->ip_len; 269df8bae1dSRodney W. Grimes } else 270df8bae1dSRodney W. Grimes m_adj(m, ip->ip_len - m->m_pkthdr.len); 271df8bae1dSRodney W. Grimes } 2724dd1662bSUgen J.S. Antsilevich /* 2734dd1662bSUgen J.S. Antsilevich * IpHack's section. 2744dd1662bSUgen J.S. Antsilevich * Right now when no processing on packet has done 2754dd1662bSUgen J.S. Antsilevich * and it is still fresh out of network we do our black 2764dd1662bSUgen J.S. Antsilevich * deals with it. 2774dd1662bSUgen J.S. Antsilevich * - Firewall: deny/allow 2784dd1662bSUgen J.S. Antsilevich * - Wrap: fake packet's addr/port <unimpl.> 2794dd1662bSUgen J.S. Antsilevich * - Encapsulate: put it in another IP and send out. <unimp.> 2804dd1662bSUgen J.S. Antsilevich */ 281df8bae1dSRodney W. Grimes 2824dd1662bSUgen J.S. Antsilevich if (ip_fw_chk_ptr!=NULL) 283c6e8c357SDavid Greenman if (!(*ip_fw_chk_ptr)(m,ip,m->m_pkthdr.rcvif,ip_fw_chain) ) { 284a0aa52a6SGuido van Rooij goto next; 285100ba1a6SJordan K. Hubbard } 286100ba1a6SJordan K. Hubbard 287df8bae1dSRodney W. Grimes /* 288df8bae1dSRodney W. Grimes * Process options and, if not destined for us, 289df8bae1dSRodney W. Grimes * ship it on. ip_dooptions returns 1 when an 290df8bae1dSRodney W. Grimes * error was detected (causing an icmp message 291df8bae1dSRodney W. Grimes * to be sent and the original packet to be freed). 292df8bae1dSRodney W. Grimes */ 293df8bae1dSRodney W. Grimes ip_nhops = 0; /* for source routed packets */ 294df8bae1dSRodney W. Grimes if (hlen > sizeof (struct ip) && ip_dooptions(m)) 295df8bae1dSRodney W. Grimes goto next; 296df8bae1dSRodney W. Grimes 297f0068c4aSGarrett Wollman /* greedy RSVP, snatches any PATH packet of the RSVP protocol and no 298f0068c4aSGarrett Wollman * matter if it is destined to another node, or whether it is 299f0068c4aSGarrett Wollman * a multicast one, RSVP wants it! and prevents it from being forwarded 300f0068c4aSGarrett Wollman * anywhere else. Also checks if the rsvp daemon is running before 301f0068c4aSGarrett Wollman * grabbing the packet. 302f0068c4aSGarrett Wollman */ 3031c5de19aSGarrett Wollman if (rsvp_on && ip->ip_p==IPPROTO_RSVP) 304f0068c4aSGarrett Wollman goto ours; 305f0068c4aSGarrett Wollman 306df8bae1dSRodney W. Grimes /* 307df8bae1dSRodney W. Grimes * Check our list of addresses, to see if the packet is for us. 308df8bae1dSRodney W. Grimes */ 309df8bae1dSRodney W. Grimes for (ia = in_ifaddr; ia; ia = ia->ia_next) { 310df8bae1dSRodney W. Grimes #define satosin(sa) ((struct sockaddr_in *)(sa)) 311df8bae1dSRodney W. Grimes 312df8bae1dSRodney W. Grimes if (IA_SIN(ia)->sin_addr.s_addr == ip->ip_dst.s_addr) 313df8bae1dSRodney W. Grimes goto ours; 31442c03a52SPeter Wemm if ((!ipdirbroadcast || ia->ia_ifp == m->m_pkthdr.rcvif) && 315df8bae1dSRodney W. Grimes (ia->ia_ifp->if_flags & IFF_BROADCAST)) { 316df8bae1dSRodney W. Grimes u_long t; 317df8bae1dSRodney W. Grimes 318df8bae1dSRodney W. Grimes if (satosin(&ia->ia_broadaddr)->sin_addr.s_addr == 319df8bae1dSRodney W. Grimes ip->ip_dst.s_addr) 320df8bae1dSRodney W. Grimes goto ours; 321df8bae1dSRodney W. Grimes if (ip->ip_dst.s_addr == ia->ia_netbroadcast.s_addr) 322df8bae1dSRodney W. Grimes goto ours; 323df8bae1dSRodney W. Grimes /* 324df8bae1dSRodney W. Grimes * Look for all-0's host part (old broadcast addr), 325df8bae1dSRodney W. Grimes * either for subnet or net. 326df8bae1dSRodney W. Grimes */ 327df8bae1dSRodney W. Grimes t = ntohl(ip->ip_dst.s_addr); 328df8bae1dSRodney W. Grimes if (t == ia->ia_subnet) 329df8bae1dSRodney W. Grimes goto ours; 330df8bae1dSRodney W. Grimes if (t == ia->ia_net) 331df8bae1dSRodney W. Grimes goto ours; 332df8bae1dSRodney W. Grimes } 333df8bae1dSRodney W. Grimes } 334df8bae1dSRodney W. Grimes if (IN_MULTICAST(ntohl(ip->ip_dst.s_addr))) { 335df8bae1dSRodney W. Grimes struct in_multi *inm; 336df8bae1dSRodney W. Grimes if (ip_mrouter) { 337df8bae1dSRodney W. Grimes /* 338df8bae1dSRodney W. Grimes * If we are acting as a multicast router, all 339df8bae1dSRodney W. Grimes * incoming multicast packets are passed to the 340df8bae1dSRodney W. Grimes * kernel-level multicast forwarding function. 341df8bae1dSRodney W. Grimes * The packet is returned (relatively) intact; if 342df8bae1dSRodney W. Grimes * ip_mforward() returns a non-zero value, the packet 343df8bae1dSRodney W. Grimes * must be discarded, else it may be accepted below. 344df8bae1dSRodney W. Grimes * 345df8bae1dSRodney W. Grimes * (The IP ident field is put in the same byte order 346df8bae1dSRodney W. Grimes * as expected when ip_mforward() is called from 347df8bae1dSRodney W. Grimes * ip_output().) 348df8bae1dSRodney W. Grimes */ 349df8bae1dSRodney W. Grimes ip->ip_id = htons(ip->ip_id); 350f0068c4aSGarrett Wollman if (ip_mforward(ip, m->m_pkthdr.rcvif, m, 0) != 0) { 351df8bae1dSRodney W. Grimes ipstat.ips_cantforward++; 352df8bae1dSRodney W. Grimes m_freem(m); 353df8bae1dSRodney W. Grimes goto next; 354df8bae1dSRodney W. Grimes } 355df8bae1dSRodney W. Grimes ip->ip_id = ntohs(ip->ip_id); 356df8bae1dSRodney W. Grimes 357df8bae1dSRodney W. Grimes /* 358df8bae1dSRodney W. Grimes * The process-level routing demon needs to receive 359df8bae1dSRodney W. Grimes * all multicast IGMP packets, whether or not this 360df8bae1dSRodney W. Grimes * host belongs to their destination groups. 361df8bae1dSRodney W. Grimes */ 362df8bae1dSRodney W. Grimes if (ip->ip_p == IPPROTO_IGMP) 363df8bae1dSRodney W. Grimes goto ours; 364df8bae1dSRodney W. Grimes ipstat.ips_forward++; 365df8bae1dSRodney W. Grimes } 366df8bae1dSRodney W. Grimes /* 367df8bae1dSRodney W. Grimes * See if we belong to the destination multicast group on the 368df8bae1dSRodney W. Grimes * arrival interface. 369df8bae1dSRodney W. Grimes */ 370df8bae1dSRodney W. Grimes IN_LOOKUP_MULTI(ip->ip_dst, m->m_pkthdr.rcvif, inm); 371df8bae1dSRodney W. Grimes if (inm == NULL) { 372df8bae1dSRodney W. Grimes ipstat.ips_cantforward++; 373df8bae1dSRodney W. Grimes m_freem(m); 374df8bae1dSRodney W. Grimes goto next; 375df8bae1dSRodney W. Grimes } 376df8bae1dSRodney W. Grimes goto ours; 377df8bae1dSRodney W. Grimes } 378df8bae1dSRodney W. Grimes if (ip->ip_dst.s_addr == (u_long)INADDR_BROADCAST) 379df8bae1dSRodney W. Grimes goto ours; 380df8bae1dSRodney W. Grimes if (ip->ip_dst.s_addr == INADDR_ANY) 381df8bae1dSRodney W. Grimes goto ours; 382df8bae1dSRodney W. Grimes 383df8bae1dSRodney W. Grimes /* 384df8bae1dSRodney W. Grimes * Not for us; forward if possible and desirable. 385df8bae1dSRodney W. Grimes */ 386df8bae1dSRodney W. Grimes if (ipforwarding == 0) { 387df8bae1dSRodney W. Grimes ipstat.ips_cantforward++; 388df8bae1dSRodney W. Grimes m_freem(m); 389df8bae1dSRodney W. Grimes } else 390df8bae1dSRodney W. Grimes ip_forward(m, 0); 391df8bae1dSRodney W. Grimes goto next; 392df8bae1dSRodney W. Grimes 393df8bae1dSRodney W. Grimes ours: 394100ba1a6SJordan K. Hubbard 39563f8d699SJordan K. Hubbard /* 39663f8d699SJordan K. Hubbard * If packet came to us we count it... 39763f8d699SJordan K. Hubbard * This way we count all incoming packets which has 39863f8d699SJordan K. Hubbard * not been forwarded... 39963f8d699SJordan K. Hubbard * Do not convert ip_len to host byte order when 40063f8d699SJordan K. Hubbard * counting,ppl already made it for us before.. 40163f8d699SJordan K. Hubbard */ 4024dd1662bSUgen J.S. Antsilevich if (ip_acct_cnt_ptr!=NULL) 4034dd1662bSUgen J.S. Antsilevich (*ip_acct_cnt_ptr)(ip,m->m_pkthdr.rcvif,ip_acct_chain,0); 40463f8d699SJordan K. Hubbard 405df8bae1dSRodney W. Grimes /* 406df8bae1dSRodney W. Grimes * If offset or IP_MF are set, must reassemble. 407df8bae1dSRodney W. Grimes * Otherwise, nothing need be done. 408df8bae1dSRodney W. Grimes * (We could look in the reassembly queue to see 409df8bae1dSRodney W. Grimes * if the packet was previously fragmented, 410df8bae1dSRodney W. Grimes * but it's not worth the time; just let them time out.) 411df8bae1dSRodney W. Grimes */ 412df8bae1dSRodney W. Grimes if (ip->ip_off &~ IP_DF) { 413df8bae1dSRodney W. Grimes if (m->m_flags & M_EXT) { /* XXX */ 414df8bae1dSRodney W. Grimes if ((m = m_pullup(m, sizeof (struct ip))) == 0) { 415df8bae1dSRodney W. Grimes ipstat.ips_toosmall++; 416df8bae1dSRodney W. Grimes goto next; 417df8bae1dSRodney W. Grimes } 418df8bae1dSRodney W. Grimes ip = mtod(m, struct ip *); 419df8bae1dSRodney W. Grimes } 420df8bae1dSRodney W. Grimes /* 421df8bae1dSRodney W. Grimes * Look for queue of fragments 422df8bae1dSRodney W. Grimes * of this datagram. 423df8bae1dSRodney W. Grimes */ 424df8bae1dSRodney W. Grimes for (fp = ipq.next; fp != &ipq; fp = fp->next) 425df8bae1dSRodney W. Grimes if (ip->ip_id == fp->ipq_id && 426df8bae1dSRodney W. Grimes ip->ip_src.s_addr == fp->ipq_src.s_addr && 427df8bae1dSRodney W. Grimes ip->ip_dst.s_addr == fp->ipq_dst.s_addr && 428df8bae1dSRodney W. Grimes ip->ip_p == fp->ipq_p) 429df8bae1dSRodney W. Grimes goto found; 430df8bae1dSRodney W. Grimes fp = 0; 431df8bae1dSRodney W. Grimes found: 432df8bae1dSRodney W. Grimes 433df8bae1dSRodney W. Grimes /* 434df8bae1dSRodney W. Grimes * Adjust ip_len to not reflect header, 435df8bae1dSRodney W. Grimes * set ip_mff if more fragments are expected, 436df8bae1dSRodney W. Grimes * convert offset of this to bytes. 437df8bae1dSRodney W. Grimes */ 438df8bae1dSRodney W. Grimes ip->ip_len -= hlen; 439df8bae1dSRodney W. Grimes ((struct ipasfrag *)ip)->ipf_mff &= ~1; 440df8bae1dSRodney W. Grimes if (ip->ip_off & IP_MF) 441df8bae1dSRodney W. Grimes ((struct ipasfrag *)ip)->ipf_mff |= 1; 442df8bae1dSRodney W. Grimes ip->ip_off <<= 3; 443df8bae1dSRodney W. Grimes 444df8bae1dSRodney W. Grimes /* 445df8bae1dSRodney W. Grimes * If datagram marked as having more fragments 446df8bae1dSRodney W. Grimes * or if this is not the first fragment, 447df8bae1dSRodney W. Grimes * attempt reassembly; if it succeeds, proceed. 448df8bae1dSRodney W. Grimes */ 449df8bae1dSRodney W. Grimes if (((struct ipasfrag *)ip)->ipf_mff & 1 || ip->ip_off) { 450df8bae1dSRodney W. Grimes ipstat.ips_fragments++; 451df8bae1dSRodney W. Grimes ip = ip_reass((struct ipasfrag *)ip, fp); 452df8bae1dSRodney W. Grimes if (ip == 0) 453df8bae1dSRodney W. Grimes goto next; 454df8bae1dSRodney W. Grimes ipstat.ips_reassembled++; 455df8bae1dSRodney W. Grimes m = dtom(ip); 456df8bae1dSRodney W. Grimes } else 457df8bae1dSRodney W. Grimes if (fp) 458df8bae1dSRodney W. Grimes ip_freef(fp); 459df8bae1dSRodney W. Grimes } else 460df8bae1dSRodney W. Grimes ip->ip_len -= hlen; 461df8bae1dSRodney W. Grimes 462df8bae1dSRodney W. Grimes /* 463df8bae1dSRodney W. Grimes * Switch out to protocol's input routine. 464df8bae1dSRodney W. Grimes */ 465df8bae1dSRodney W. Grimes ipstat.ips_delivered++; 466df8bae1dSRodney W. Grimes (*inetsw[ip_protox[ip->ip_p]].pr_input)(m, hlen); 467df8bae1dSRodney W. Grimes goto next; 468df8bae1dSRodney W. Grimes bad: 469df8bae1dSRodney W. Grimes m_freem(m); 470df8bae1dSRodney W. Grimes goto next; 471df8bae1dSRodney W. Grimes } 472df8bae1dSRodney W. Grimes 473748e0b0aSGarrett Wollman NETISR_SET(NETISR_IP, ipintr); 474748e0b0aSGarrett Wollman 475df8bae1dSRodney W. Grimes /* 476df8bae1dSRodney W. Grimes * Take incoming datagram fragment and try to 477df8bae1dSRodney W. Grimes * reassemble it into whole datagram. If a chain for 478df8bae1dSRodney W. Grimes * reassembly of this datagram already exists, then it 479df8bae1dSRodney W. Grimes * is given as fp; otherwise have to make a chain. 480df8bae1dSRodney W. Grimes */ 4810312fbe9SPoul-Henning Kamp static struct ip * 482df8bae1dSRodney W. Grimes ip_reass(ip, fp) 483df8bae1dSRodney W. Grimes register struct ipasfrag *ip; 484df8bae1dSRodney W. Grimes register struct ipq *fp; 485df8bae1dSRodney W. Grimes { 486df8bae1dSRodney W. Grimes register struct mbuf *m = dtom(ip); 487df8bae1dSRodney W. Grimes register struct ipasfrag *q; 488df8bae1dSRodney W. Grimes struct mbuf *t; 489df8bae1dSRodney W. Grimes int hlen = ip->ip_hl << 2; 490df8bae1dSRodney W. Grimes int i, next; 491df8bae1dSRodney W. Grimes 492df8bae1dSRodney W. Grimes /* 493df8bae1dSRodney W. Grimes * Presence of header sizes in mbufs 494df8bae1dSRodney W. Grimes * would confuse code below. 495df8bae1dSRodney W. Grimes */ 496df8bae1dSRodney W. Grimes m->m_data += hlen; 497df8bae1dSRodney W. Grimes m->m_len -= hlen; 498df8bae1dSRodney W. Grimes 499df8bae1dSRodney W. Grimes /* 500df8bae1dSRodney W. Grimes * If first fragment to arrive, create a reassembly queue. 501df8bae1dSRodney W. Grimes */ 502df8bae1dSRodney W. Grimes if (fp == 0) { 503df8bae1dSRodney W. Grimes if ((t = m_get(M_DONTWAIT, MT_FTABLE)) == NULL) 504df8bae1dSRodney W. Grimes goto dropfrag; 505df8bae1dSRodney W. Grimes fp = mtod(t, struct ipq *); 506df8bae1dSRodney W. Grimes insque(fp, &ipq); 507df8bae1dSRodney W. Grimes fp->ipq_ttl = IPFRAGTTL; 508df8bae1dSRodney W. Grimes fp->ipq_p = ip->ip_p; 509df8bae1dSRodney W. Grimes fp->ipq_id = ip->ip_id; 510df8bae1dSRodney W. Grimes fp->ipq_next = fp->ipq_prev = (struct ipasfrag *)fp; 511df8bae1dSRodney W. Grimes fp->ipq_src = ((struct ip *)ip)->ip_src; 512df8bae1dSRodney W. Grimes fp->ipq_dst = ((struct ip *)ip)->ip_dst; 513df8bae1dSRodney W. Grimes q = (struct ipasfrag *)fp; 514df8bae1dSRodney W. Grimes goto insert; 515df8bae1dSRodney W. Grimes } 516df8bae1dSRodney W. Grimes 517df8bae1dSRodney W. Grimes /* 518df8bae1dSRodney W. Grimes * Find a segment which begins after this one does. 519df8bae1dSRodney W. Grimes */ 520df8bae1dSRodney W. Grimes for (q = fp->ipq_next; q != (struct ipasfrag *)fp; q = q->ipf_next) 521df8bae1dSRodney W. Grimes if (q->ip_off > ip->ip_off) 522df8bae1dSRodney W. Grimes break; 523df8bae1dSRodney W. Grimes 524df8bae1dSRodney W. Grimes /* 525df8bae1dSRodney W. Grimes * If there is a preceding segment, it may provide some of 526df8bae1dSRodney W. Grimes * our data already. If so, drop the data from the incoming 527df8bae1dSRodney W. Grimes * segment. If it provides all of our data, drop us. 528df8bae1dSRodney W. Grimes */ 529df8bae1dSRodney W. Grimes if (q->ipf_prev != (struct ipasfrag *)fp) { 530df8bae1dSRodney W. Grimes i = q->ipf_prev->ip_off + q->ipf_prev->ip_len - ip->ip_off; 531df8bae1dSRodney W. Grimes if (i > 0) { 532df8bae1dSRodney W. Grimes if (i >= ip->ip_len) 533df8bae1dSRodney W. Grimes goto dropfrag; 534df8bae1dSRodney W. Grimes m_adj(dtom(ip), i); 535df8bae1dSRodney W. Grimes ip->ip_off += i; 536df8bae1dSRodney W. Grimes ip->ip_len -= i; 537df8bae1dSRodney W. Grimes } 538df8bae1dSRodney W. Grimes } 539df8bae1dSRodney W. Grimes 540df8bae1dSRodney W. Grimes /* 541df8bae1dSRodney W. Grimes * While we overlap succeeding segments trim them or, 542df8bae1dSRodney W. Grimes * if they are completely covered, dequeue them. 543df8bae1dSRodney W. Grimes */ 544df8bae1dSRodney W. Grimes while (q != (struct ipasfrag *)fp && ip->ip_off + ip->ip_len > q->ip_off) { 545df8bae1dSRodney W. Grimes i = (ip->ip_off + ip->ip_len) - q->ip_off; 546df8bae1dSRodney W. Grimes if (i < q->ip_len) { 547df8bae1dSRodney W. Grimes q->ip_len -= i; 548df8bae1dSRodney W. Grimes q->ip_off += i; 549df8bae1dSRodney W. Grimes m_adj(dtom(q), i); 550df8bae1dSRodney W. Grimes break; 551df8bae1dSRodney W. Grimes } 552df8bae1dSRodney W. Grimes q = q->ipf_next; 553df8bae1dSRodney W. Grimes m_freem(dtom(q->ipf_prev)); 554df8bae1dSRodney W. Grimes ip_deq(q->ipf_prev); 555df8bae1dSRodney W. Grimes } 556df8bae1dSRodney W. Grimes 557df8bae1dSRodney W. Grimes insert: 558df8bae1dSRodney W. Grimes /* 559df8bae1dSRodney W. Grimes * Stick new segment in its place; 560df8bae1dSRodney W. Grimes * check for complete reassembly. 561df8bae1dSRodney W. Grimes */ 562df8bae1dSRodney W. Grimes ip_enq(ip, q->ipf_prev); 563df8bae1dSRodney W. Grimes next = 0; 564df8bae1dSRodney W. Grimes for (q = fp->ipq_next; q != (struct ipasfrag *)fp; q = q->ipf_next) { 565df8bae1dSRodney W. Grimes if (q->ip_off != next) 566df8bae1dSRodney W. Grimes return (0); 567df8bae1dSRodney W. Grimes next += q->ip_len; 568df8bae1dSRodney W. Grimes } 569df8bae1dSRodney W. Grimes if (q->ipf_prev->ipf_mff & 1) 570df8bae1dSRodney W. Grimes return (0); 571df8bae1dSRodney W. Grimes 572df8bae1dSRodney W. Grimes /* 573df8bae1dSRodney W. Grimes * Reassembly is complete; concatenate fragments. 574df8bae1dSRodney W. Grimes */ 575df8bae1dSRodney W. Grimes q = fp->ipq_next; 576df8bae1dSRodney W. Grimes m = dtom(q); 577df8bae1dSRodney W. Grimes t = m->m_next; 578df8bae1dSRodney W. Grimes m->m_next = 0; 579df8bae1dSRodney W. Grimes m_cat(m, t); 580df8bae1dSRodney W. Grimes q = q->ipf_next; 581df8bae1dSRodney W. Grimes while (q != (struct ipasfrag *)fp) { 582df8bae1dSRodney W. Grimes t = dtom(q); 583df8bae1dSRodney W. Grimes q = q->ipf_next; 584df8bae1dSRodney W. Grimes m_cat(m, t); 585df8bae1dSRodney W. Grimes } 586df8bae1dSRodney W. Grimes 587df8bae1dSRodney W. Grimes /* 588df8bae1dSRodney W. Grimes * Create header for new ip packet by 589df8bae1dSRodney W. Grimes * modifying header of first packet; 590df8bae1dSRodney W. Grimes * dequeue and discard fragment reassembly header. 591df8bae1dSRodney W. Grimes * Make header visible. 592df8bae1dSRodney W. Grimes */ 593df8bae1dSRodney W. Grimes ip = fp->ipq_next; 594df8bae1dSRodney W. Grimes ip->ip_len = next; 595df8bae1dSRodney W. Grimes ip->ipf_mff &= ~1; 596df8bae1dSRodney W. Grimes ((struct ip *)ip)->ip_src = fp->ipq_src; 597df8bae1dSRodney W. Grimes ((struct ip *)ip)->ip_dst = fp->ipq_dst; 598df8bae1dSRodney W. Grimes remque(fp); 599df8bae1dSRodney W. Grimes (void) m_free(dtom(fp)); 600df8bae1dSRodney W. Grimes m = dtom(ip); 601df8bae1dSRodney W. Grimes m->m_len += (ip->ip_hl << 2); 602df8bae1dSRodney W. Grimes m->m_data -= (ip->ip_hl << 2); 603df8bae1dSRodney W. Grimes /* some debugging cruft by sklower, below, will go away soon */ 604df8bae1dSRodney W. Grimes if (m->m_flags & M_PKTHDR) { /* XXX this should be done elsewhere */ 605df8bae1dSRodney W. Grimes register int plen = 0; 606df8bae1dSRodney W. Grimes for (t = m; m; m = m->m_next) 607df8bae1dSRodney W. Grimes plen += m->m_len; 608df8bae1dSRodney W. Grimes t->m_pkthdr.len = plen; 609df8bae1dSRodney W. Grimes } 610df8bae1dSRodney W. Grimes return ((struct ip *)ip); 611df8bae1dSRodney W. Grimes 612df8bae1dSRodney W. Grimes dropfrag: 613df8bae1dSRodney W. Grimes ipstat.ips_fragdropped++; 614df8bae1dSRodney W. Grimes m_freem(m); 615df8bae1dSRodney W. Grimes return (0); 616df8bae1dSRodney W. Grimes } 617df8bae1dSRodney W. Grimes 618df8bae1dSRodney W. Grimes /* 619df8bae1dSRodney W. Grimes * Free a fragment reassembly header and all 620df8bae1dSRodney W. Grimes * associated datagrams. 621df8bae1dSRodney W. Grimes */ 6220312fbe9SPoul-Henning Kamp static void 623df8bae1dSRodney W. Grimes ip_freef(fp) 624df8bae1dSRodney W. Grimes struct ipq *fp; 625df8bae1dSRodney W. Grimes { 626df8bae1dSRodney W. Grimes register struct ipasfrag *q, *p; 627df8bae1dSRodney W. Grimes 628df8bae1dSRodney W. Grimes for (q = fp->ipq_next; q != (struct ipasfrag *)fp; q = p) { 629df8bae1dSRodney W. Grimes p = q->ipf_next; 630df8bae1dSRodney W. Grimes ip_deq(q); 631df8bae1dSRodney W. Grimes m_freem(dtom(q)); 632df8bae1dSRodney W. Grimes } 633df8bae1dSRodney W. Grimes remque(fp); 634df8bae1dSRodney W. Grimes (void) m_free(dtom(fp)); 635df8bae1dSRodney W. Grimes } 636df8bae1dSRodney W. Grimes 637df8bae1dSRodney W. Grimes /* 638df8bae1dSRodney W. Grimes * Put an ip fragment on a reassembly chain. 639df8bae1dSRodney W. Grimes * Like insque, but pointers in middle of structure. 640df8bae1dSRodney W. Grimes */ 6410312fbe9SPoul-Henning Kamp static void 642df8bae1dSRodney W. Grimes ip_enq(p, prev) 643df8bae1dSRodney W. Grimes register struct ipasfrag *p, *prev; 644df8bae1dSRodney W. Grimes { 645df8bae1dSRodney W. Grimes 646df8bae1dSRodney W. Grimes p->ipf_prev = prev; 647df8bae1dSRodney W. Grimes p->ipf_next = prev->ipf_next; 648df8bae1dSRodney W. Grimes prev->ipf_next->ipf_prev = p; 649df8bae1dSRodney W. Grimes prev->ipf_next = p; 650df8bae1dSRodney W. Grimes } 651df8bae1dSRodney W. Grimes 652df8bae1dSRodney W. Grimes /* 653df8bae1dSRodney W. Grimes * To ip_enq as remque is to insque. 654df8bae1dSRodney W. Grimes */ 6550312fbe9SPoul-Henning Kamp static void 656df8bae1dSRodney W. Grimes ip_deq(p) 657df8bae1dSRodney W. Grimes register struct ipasfrag *p; 658df8bae1dSRodney W. Grimes { 659df8bae1dSRodney W. Grimes 660df8bae1dSRodney W. Grimes p->ipf_prev->ipf_next = p->ipf_next; 661df8bae1dSRodney W. Grimes p->ipf_next->ipf_prev = p->ipf_prev; 662df8bae1dSRodney W. Grimes } 663df8bae1dSRodney W. Grimes 664df8bae1dSRodney W. Grimes /* 665df8bae1dSRodney W. Grimes * IP timer processing; 666df8bae1dSRodney W. Grimes * if a timer expires on a reassembly 667df8bae1dSRodney W. Grimes * queue, discard it. 668df8bae1dSRodney W. Grimes */ 669df8bae1dSRodney W. Grimes void 670df8bae1dSRodney W. Grimes ip_slowtimo() 671df8bae1dSRodney W. Grimes { 672df8bae1dSRodney W. Grimes register struct ipq *fp; 673df8bae1dSRodney W. Grimes int s = splnet(); 674df8bae1dSRodney W. Grimes 675df8bae1dSRodney W. Grimes fp = ipq.next; 676df8bae1dSRodney W. Grimes if (fp == 0) { 677df8bae1dSRodney W. Grimes splx(s); 678df8bae1dSRodney W. Grimes return; 679df8bae1dSRodney W. Grimes } 680df8bae1dSRodney W. Grimes while (fp != &ipq) { 681df8bae1dSRodney W. Grimes --fp->ipq_ttl; 682df8bae1dSRodney W. Grimes fp = fp->next; 683df8bae1dSRodney W. Grimes if (fp->prev->ipq_ttl == 0) { 684df8bae1dSRodney W. Grimes ipstat.ips_fragtimeout++; 685df8bae1dSRodney W. Grimes ip_freef(fp->prev); 686df8bae1dSRodney W. Grimes } 687df8bae1dSRodney W. Grimes } 688df8bae1dSRodney W. Grimes splx(s); 689df8bae1dSRodney W. Grimes } 690df8bae1dSRodney W. Grimes 691df8bae1dSRodney W. Grimes /* 692df8bae1dSRodney W. Grimes * Drain off all datagram fragments. 693df8bae1dSRodney W. Grimes */ 694df8bae1dSRodney W. Grimes void 695df8bae1dSRodney W. Grimes ip_drain() 696df8bae1dSRodney W. Grimes { 697df8bae1dSRodney W. Grimes 698df8bae1dSRodney W. Grimes while (ipq.next != &ipq) { 699df8bae1dSRodney W. Grimes ipstat.ips_fragdropped++; 700df8bae1dSRodney W. Grimes ip_freef(ipq.next); 701df8bae1dSRodney W. Grimes } 702df8bae1dSRodney W. Grimes } 703df8bae1dSRodney W. Grimes 704df8bae1dSRodney W. Grimes /* 705df8bae1dSRodney W. Grimes * Do option processing on a datagram, 706df8bae1dSRodney W. Grimes * possibly discarding it if bad options are encountered, 707df8bae1dSRodney W. Grimes * or forwarding it if source-routed. 708df8bae1dSRodney W. Grimes * Returns 1 if packet has been forwarded/freed, 709df8bae1dSRodney W. Grimes * 0 if the packet should be processed further. 710df8bae1dSRodney W. Grimes */ 7110312fbe9SPoul-Henning Kamp static int 712df8bae1dSRodney W. Grimes ip_dooptions(m) 713df8bae1dSRodney W. Grimes struct mbuf *m; 714df8bae1dSRodney W. Grimes { 715df8bae1dSRodney W. Grimes register struct ip *ip = mtod(m, struct ip *); 716df8bae1dSRodney W. Grimes register u_char *cp; 717df8bae1dSRodney W. Grimes register struct ip_timestamp *ipt; 718df8bae1dSRodney W. Grimes register struct in_ifaddr *ia; 719df8bae1dSRodney W. Grimes int opt, optlen, cnt, off, code, type = ICMP_PARAMPROB, forward = 0; 720df8bae1dSRodney W. Grimes struct in_addr *sin, dst; 721df8bae1dSRodney W. Grimes n_time ntime; 722df8bae1dSRodney W. Grimes 723df8bae1dSRodney W. Grimes dst = ip->ip_dst; 724df8bae1dSRodney W. Grimes cp = (u_char *)(ip + 1); 725df8bae1dSRodney W. Grimes cnt = (ip->ip_hl << 2) - sizeof (struct ip); 726df8bae1dSRodney W. Grimes for (; cnt > 0; cnt -= optlen, cp += optlen) { 727df8bae1dSRodney W. Grimes opt = cp[IPOPT_OPTVAL]; 728df8bae1dSRodney W. Grimes if (opt == IPOPT_EOL) 729df8bae1dSRodney W. Grimes break; 730df8bae1dSRodney W. Grimes if (opt == IPOPT_NOP) 731df8bae1dSRodney W. Grimes optlen = 1; 732df8bae1dSRodney W. Grimes else { 733df8bae1dSRodney W. Grimes optlen = cp[IPOPT_OLEN]; 734df8bae1dSRodney W. Grimes if (optlen <= 0 || optlen > cnt) { 735df8bae1dSRodney W. Grimes code = &cp[IPOPT_OLEN] - (u_char *)ip; 736df8bae1dSRodney W. Grimes goto bad; 737df8bae1dSRodney W. Grimes } 738df8bae1dSRodney W. Grimes } 739df8bae1dSRodney W. Grimes switch (opt) { 740df8bae1dSRodney W. Grimes 741df8bae1dSRodney W. Grimes default: 742df8bae1dSRodney W. Grimes break; 743df8bae1dSRodney W. Grimes 744df8bae1dSRodney W. Grimes /* 745df8bae1dSRodney W. Grimes * Source routing with record. 746df8bae1dSRodney W. Grimes * Find interface with current destination address. 747df8bae1dSRodney W. Grimes * If none on this machine then drop if strictly routed, 748df8bae1dSRodney W. Grimes * or do nothing if loosely routed. 749df8bae1dSRodney W. Grimes * Record interface address and bring up next address 750df8bae1dSRodney W. Grimes * component. If strictly routed make sure next 751df8bae1dSRodney W. Grimes * address is on directly accessible net. 752df8bae1dSRodney W. Grimes */ 753df8bae1dSRodney W. Grimes case IPOPT_LSRR: 754df8bae1dSRodney W. Grimes case IPOPT_SSRR: 755df8bae1dSRodney W. Grimes if ((off = cp[IPOPT_OFFSET]) < IPOPT_MINOFF) { 756df8bae1dSRodney W. Grimes code = &cp[IPOPT_OFFSET] - (u_char *)ip; 757df8bae1dSRodney W. Grimes goto bad; 758df8bae1dSRodney W. Grimes } 759df8bae1dSRodney W. Grimes ipaddr.sin_addr = ip->ip_dst; 760df8bae1dSRodney W. Grimes ia = (struct in_ifaddr *) 761df8bae1dSRodney W. Grimes ifa_ifwithaddr((struct sockaddr *)&ipaddr); 762df8bae1dSRodney W. Grimes if (ia == 0) { 763df8bae1dSRodney W. Grimes if (opt == IPOPT_SSRR) { 764df8bae1dSRodney W. Grimes type = ICMP_UNREACH; 765df8bae1dSRodney W. Grimes code = ICMP_UNREACH_SRCFAIL; 766df8bae1dSRodney W. Grimes goto bad; 767df8bae1dSRodney W. Grimes } 768df8bae1dSRodney W. Grimes /* 769df8bae1dSRodney W. Grimes * Loose routing, and not at next destination 770df8bae1dSRodney W. Grimes * yet; nothing to do except forward. 771df8bae1dSRodney W. Grimes */ 772df8bae1dSRodney W. Grimes break; 773df8bae1dSRodney W. Grimes } 774df8bae1dSRodney W. Grimes off--; /* 0 origin */ 775df8bae1dSRodney W. Grimes if (off > optlen - sizeof(struct in_addr)) { 776df8bae1dSRodney W. Grimes /* 777df8bae1dSRodney W. Grimes * End of source route. Should be for us. 778df8bae1dSRodney W. Grimes */ 779df8bae1dSRodney W. Grimes save_rte(cp, ip->ip_src); 780df8bae1dSRodney W. Grimes break; 781df8bae1dSRodney W. Grimes } 7821025071fSGarrett Wollman 7831025071fSGarrett Wollman if (!ip_dosourceroute) { 7841025071fSGarrett Wollman char buf[4*sizeof "123"]; 7851025071fSGarrett Wollman strcpy(buf, inet_ntoa(ip->ip_dst)); 7861025071fSGarrett Wollman 7871025071fSGarrett Wollman log(LOG_WARNING, 7881025071fSGarrett Wollman "attempted source route from %s to %s\n", 7891025071fSGarrett Wollman inet_ntoa(ip->ip_src), buf); 7901025071fSGarrett Wollman type = ICMP_UNREACH; 7911025071fSGarrett Wollman code = ICMP_UNREACH_SRCFAIL; 7921025071fSGarrett Wollman goto bad; 7931025071fSGarrett Wollman } 7941025071fSGarrett Wollman 795df8bae1dSRodney W. Grimes /* 796df8bae1dSRodney W. Grimes * locate outgoing interface 797df8bae1dSRodney W. Grimes */ 79894a5d9b6SDavid Greenman (void)memcpy(&ipaddr.sin_addr, cp + off, 799df8bae1dSRodney W. Grimes sizeof(ipaddr.sin_addr)); 8001025071fSGarrett Wollman 801df8bae1dSRodney W. Grimes if (opt == IPOPT_SSRR) { 802df8bae1dSRodney W. Grimes #define INA struct in_ifaddr * 803df8bae1dSRodney W. Grimes #define SA struct sockaddr * 804df8bae1dSRodney W. Grimes if ((ia = (INA)ifa_ifwithdstaddr((SA)&ipaddr)) == 0) 805df8bae1dSRodney W. Grimes ia = (INA)ifa_ifwithnet((SA)&ipaddr); 806df8bae1dSRodney W. Grimes } else 807df8bae1dSRodney W. Grimes ia = ip_rtaddr(ipaddr.sin_addr); 808df8bae1dSRodney W. Grimes if (ia == 0) { 809df8bae1dSRodney W. Grimes type = ICMP_UNREACH; 810df8bae1dSRodney W. Grimes code = ICMP_UNREACH_SRCFAIL; 811df8bae1dSRodney W. Grimes goto bad; 812df8bae1dSRodney W. Grimes } 813df8bae1dSRodney W. Grimes ip->ip_dst = ipaddr.sin_addr; 81494a5d9b6SDavid Greenman (void)memcpy(cp + off, &(IA_SIN(ia)->sin_addr), 81594a5d9b6SDavid Greenman sizeof(struct in_addr)); 816df8bae1dSRodney W. Grimes cp[IPOPT_OFFSET] += sizeof(struct in_addr); 817df8bae1dSRodney W. Grimes /* 818df8bae1dSRodney W. Grimes * Let ip_intr's mcast routing check handle mcast pkts 819df8bae1dSRodney W. Grimes */ 820df8bae1dSRodney W. Grimes forward = !IN_MULTICAST(ntohl(ip->ip_dst.s_addr)); 821df8bae1dSRodney W. Grimes break; 822df8bae1dSRodney W. Grimes 823df8bae1dSRodney W. Grimes case IPOPT_RR: 824df8bae1dSRodney W. Grimes if ((off = cp[IPOPT_OFFSET]) < IPOPT_MINOFF) { 825df8bae1dSRodney W. Grimes code = &cp[IPOPT_OFFSET] - (u_char *)ip; 826df8bae1dSRodney W. Grimes goto bad; 827df8bae1dSRodney W. Grimes } 828df8bae1dSRodney W. Grimes /* 829df8bae1dSRodney W. Grimes * If no space remains, ignore. 830df8bae1dSRodney W. Grimes */ 831df8bae1dSRodney W. Grimes off--; /* 0 origin */ 832df8bae1dSRodney W. Grimes if (off > optlen - sizeof(struct in_addr)) 833df8bae1dSRodney W. Grimes break; 83494a5d9b6SDavid Greenman (void)memcpy(&ipaddr.sin_addr, &ip->ip_dst, 835df8bae1dSRodney W. Grimes sizeof(ipaddr.sin_addr)); 836df8bae1dSRodney W. Grimes /* 837df8bae1dSRodney W. Grimes * locate outgoing interface; if we're the destination, 838df8bae1dSRodney W. Grimes * use the incoming interface (should be same). 839df8bae1dSRodney W. Grimes */ 840df8bae1dSRodney W. Grimes if ((ia = (INA)ifa_ifwithaddr((SA)&ipaddr)) == 0 && 841df8bae1dSRodney W. Grimes (ia = ip_rtaddr(ipaddr.sin_addr)) == 0) { 842df8bae1dSRodney W. Grimes type = ICMP_UNREACH; 843df8bae1dSRodney W. Grimes code = ICMP_UNREACH_HOST; 844df8bae1dSRodney W. Grimes goto bad; 845df8bae1dSRodney W. Grimes } 84694a5d9b6SDavid Greenman (void)memcpy(cp + off, &(IA_SIN(ia)->sin_addr), 84794a5d9b6SDavid Greenman sizeof(struct in_addr)); 848df8bae1dSRodney W. Grimes cp[IPOPT_OFFSET] += sizeof(struct in_addr); 849df8bae1dSRodney W. Grimes break; 850df8bae1dSRodney W. Grimes 851df8bae1dSRodney W. Grimes case IPOPT_TS: 852df8bae1dSRodney W. Grimes code = cp - (u_char *)ip; 853df8bae1dSRodney W. Grimes ipt = (struct ip_timestamp *)cp; 854df8bae1dSRodney W. Grimes if (ipt->ipt_len < 5) 855df8bae1dSRodney W. Grimes goto bad; 856df8bae1dSRodney W. Grimes if (ipt->ipt_ptr > ipt->ipt_len - sizeof (long)) { 857df8bae1dSRodney W. Grimes if (++ipt->ipt_oflw == 0) 858df8bae1dSRodney W. Grimes goto bad; 859df8bae1dSRodney W. Grimes break; 860df8bae1dSRodney W. Grimes } 861df8bae1dSRodney W. Grimes sin = (struct in_addr *)(cp + ipt->ipt_ptr - 1); 862df8bae1dSRodney W. Grimes switch (ipt->ipt_flg) { 863df8bae1dSRodney W. Grimes 864df8bae1dSRodney W. Grimes case IPOPT_TS_TSONLY: 865df8bae1dSRodney W. Grimes break; 866df8bae1dSRodney W. Grimes 867df8bae1dSRodney W. Grimes case IPOPT_TS_TSANDADDR: 868df8bae1dSRodney W. Grimes if (ipt->ipt_ptr + sizeof(n_time) + 869df8bae1dSRodney W. Grimes sizeof(struct in_addr) > ipt->ipt_len) 870df8bae1dSRodney W. Grimes goto bad; 871df8bae1dSRodney W. Grimes ipaddr.sin_addr = dst; 872df8bae1dSRodney W. Grimes ia = (INA)ifaof_ifpforaddr((SA)&ipaddr, 873df8bae1dSRodney W. Grimes m->m_pkthdr.rcvif); 874df8bae1dSRodney W. Grimes if (ia == 0) 875df8bae1dSRodney W. Grimes continue; 87694a5d9b6SDavid Greenman (void)memcpy(sin, &IA_SIN(ia)->sin_addr, 87794a5d9b6SDavid Greenman sizeof(struct in_addr)); 878df8bae1dSRodney W. Grimes ipt->ipt_ptr += sizeof(struct in_addr); 879df8bae1dSRodney W. Grimes break; 880df8bae1dSRodney W. Grimes 881df8bae1dSRodney W. Grimes case IPOPT_TS_PRESPEC: 882df8bae1dSRodney W. Grimes if (ipt->ipt_ptr + sizeof(n_time) + 883df8bae1dSRodney W. Grimes sizeof(struct in_addr) > ipt->ipt_len) 884df8bae1dSRodney W. Grimes goto bad; 88594a5d9b6SDavid Greenman (void)memcpy(&ipaddr.sin_addr, sin, 886df8bae1dSRodney W. Grimes sizeof(struct in_addr)); 887df8bae1dSRodney W. Grimes if (ifa_ifwithaddr((SA)&ipaddr) == 0) 888df8bae1dSRodney W. Grimes continue; 889df8bae1dSRodney W. Grimes ipt->ipt_ptr += sizeof(struct in_addr); 890df8bae1dSRodney W. Grimes break; 891df8bae1dSRodney W. Grimes 892df8bae1dSRodney W. Grimes default: 893df8bae1dSRodney W. Grimes goto bad; 894df8bae1dSRodney W. Grimes } 895df8bae1dSRodney W. Grimes ntime = iptime(); 89694a5d9b6SDavid Greenman (void)memcpy(cp + ipt->ipt_ptr - 1, &ntime, 897df8bae1dSRodney W. Grimes sizeof(n_time)); 898df8bae1dSRodney W. Grimes ipt->ipt_ptr += sizeof(n_time); 899df8bae1dSRodney W. Grimes } 900df8bae1dSRodney W. Grimes } 901df8bae1dSRodney W. Grimes if (forward) { 902df8bae1dSRodney W. Grimes ip_forward(m, 1); 903df8bae1dSRodney W. Grimes return (1); 904df8bae1dSRodney W. Grimes } 905df8bae1dSRodney W. Grimes return (0); 906df8bae1dSRodney W. Grimes bad: 907df8bae1dSRodney W. Grimes ip->ip_len -= ip->ip_hl << 2; /* XXX icmp_error adds in hdr length */ 908df8bae1dSRodney W. Grimes icmp_error(m, type, code, 0, 0); 909df8bae1dSRodney W. Grimes ipstat.ips_badoptions++; 910df8bae1dSRodney W. Grimes return (1); 911df8bae1dSRodney W. Grimes } 912df8bae1dSRodney W. Grimes 913df8bae1dSRodney W. Grimes /* 914df8bae1dSRodney W. Grimes * Given address of next destination (final or next hop), 915df8bae1dSRodney W. Grimes * return internet address info of interface to be used to get there. 916df8bae1dSRodney W. Grimes */ 9170312fbe9SPoul-Henning Kamp static struct in_ifaddr * 918df8bae1dSRodney W. Grimes ip_rtaddr(dst) 919df8bae1dSRodney W. Grimes struct in_addr dst; 920df8bae1dSRodney W. Grimes { 921df8bae1dSRodney W. Grimes register struct sockaddr_in *sin; 922df8bae1dSRodney W. Grimes 923df8bae1dSRodney W. Grimes sin = (struct sockaddr_in *) &ipforward_rt.ro_dst; 924df8bae1dSRodney W. Grimes 925df8bae1dSRodney W. Grimes if (ipforward_rt.ro_rt == 0 || dst.s_addr != sin->sin_addr.s_addr) { 926df8bae1dSRodney W. Grimes if (ipforward_rt.ro_rt) { 927df8bae1dSRodney W. Grimes RTFREE(ipforward_rt.ro_rt); 928df8bae1dSRodney W. Grimes ipforward_rt.ro_rt = 0; 929df8bae1dSRodney W. Grimes } 930df8bae1dSRodney W. Grimes sin->sin_family = AF_INET; 931df8bae1dSRodney W. Grimes sin->sin_len = sizeof(*sin); 932df8bae1dSRodney W. Grimes sin->sin_addr = dst; 933df8bae1dSRodney W. Grimes 9342c17fe93SGarrett Wollman rtalloc_ign(&ipforward_rt, RTF_PRCLONING); 935df8bae1dSRodney W. Grimes } 936df8bae1dSRodney W. Grimes if (ipforward_rt.ro_rt == 0) 937df8bae1dSRodney W. Grimes return ((struct in_ifaddr *)0); 938df8bae1dSRodney W. Grimes return ((struct in_ifaddr *) ipforward_rt.ro_rt->rt_ifa); 939df8bae1dSRodney W. Grimes } 940df8bae1dSRodney W. Grimes 941df8bae1dSRodney W. Grimes /* 942df8bae1dSRodney W. Grimes * Save incoming source route for use in replies, 943df8bae1dSRodney W. Grimes * to be picked up later by ip_srcroute if the receiver is interested. 944df8bae1dSRodney W. Grimes */ 945df8bae1dSRodney W. Grimes void 946df8bae1dSRodney W. Grimes save_rte(option, dst) 947df8bae1dSRodney W. Grimes u_char *option; 948df8bae1dSRodney W. Grimes struct in_addr dst; 949df8bae1dSRodney W. Grimes { 950df8bae1dSRodney W. Grimes unsigned olen; 951df8bae1dSRodney W. Grimes 952df8bae1dSRodney W. Grimes olen = option[IPOPT_OLEN]; 953df8bae1dSRodney W. Grimes #ifdef DIAGNOSTIC 954df8bae1dSRodney W. Grimes if (ipprintfs) 955df8bae1dSRodney W. Grimes printf("save_rte: olen %d\n", olen); 956df8bae1dSRodney W. Grimes #endif 957df8bae1dSRodney W. Grimes if (olen > sizeof(ip_srcrt) - (1 + sizeof(dst))) 958df8bae1dSRodney W. Grimes return; 95994a5d9b6SDavid Greenman (void)memcpy(ip_srcrt.srcopt, option, olen); 960df8bae1dSRodney W. Grimes ip_nhops = (olen - IPOPT_OFFSET - 1) / sizeof(struct in_addr); 961df8bae1dSRodney W. Grimes ip_srcrt.dst = dst; 962df8bae1dSRodney W. Grimes } 963df8bae1dSRodney W. Grimes 964df8bae1dSRodney W. Grimes /* 965df8bae1dSRodney W. Grimes * Retrieve incoming source route for use in replies, 966df8bae1dSRodney W. Grimes * in the same form used by setsockopt. 967df8bae1dSRodney W. Grimes * The first hop is placed before the options, will be removed later. 968df8bae1dSRodney W. Grimes */ 969df8bae1dSRodney W. Grimes struct mbuf * 970df8bae1dSRodney W. Grimes ip_srcroute() 971df8bae1dSRodney W. Grimes { 972df8bae1dSRodney W. Grimes register struct in_addr *p, *q; 973df8bae1dSRodney W. Grimes register struct mbuf *m; 974df8bae1dSRodney W. Grimes 975df8bae1dSRodney W. Grimes if (ip_nhops == 0) 976df8bae1dSRodney W. Grimes return ((struct mbuf *)0); 977df8bae1dSRodney W. Grimes m = m_get(M_DONTWAIT, MT_SOOPTS); 978df8bae1dSRodney W. Grimes if (m == 0) 979df8bae1dSRodney W. Grimes return ((struct mbuf *)0); 980df8bae1dSRodney W. Grimes 981df8bae1dSRodney W. Grimes #define OPTSIZ (sizeof(ip_srcrt.nop) + sizeof(ip_srcrt.srcopt)) 982df8bae1dSRodney W. Grimes 983df8bae1dSRodney W. Grimes /* length is (nhops+1)*sizeof(addr) + sizeof(nop + srcrt header) */ 984df8bae1dSRodney W. Grimes m->m_len = ip_nhops * sizeof(struct in_addr) + sizeof(struct in_addr) + 985df8bae1dSRodney W. Grimes OPTSIZ; 986df8bae1dSRodney W. Grimes #ifdef DIAGNOSTIC 987df8bae1dSRodney W. Grimes if (ipprintfs) 988df8bae1dSRodney W. Grimes printf("ip_srcroute: nhops %d mlen %d", ip_nhops, m->m_len); 989df8bae1dSRodney W. Grimes #endif 990df8bae1dSRodney W. Grimes 991df8bae1dSRodney W. Grimes /* 992df8bae1dSRodney W. Grimes * First save first hop for return route 993df8bae1dSRodney W. Grimes */ 994df8bae1dSRodney W. Grimes p = &ip_srcrt.route[ip_nhops - 1]; 995df8bae1dSRodney W. Grimes *(mtod(m, struct in_addr *)) = *p--; 996df8bae1dSRodney W. Grimes #ifdef DIAGNOSTIC 997df8bae1dSRodney W. Grimes if (ipprintfs) 998df8bae1dSRodney W. Grimes printf(" hops %lx", ntohl(mtod(m, struct in_addr *)->s_addr)); 999df8bae1dSRodney W. Grimes #endif 1000df8bae1dSRodney W. Grimes 1001df8bae1dSRodney W. Grimes /* 1002df8bae1dSRodney W. Grimes * Copy option fields and padding (nop) to mbuf. 1003df8bae1dSRodney W. Grimes */ 1004df8bae1dSRodney W. Grimes ip_srcrt.nop = IPOPT_NOP; 1005df8bae1dSRodney W. Grimes ip_srcrt.srcopt[IPOPT_OFFSET] = IPOPT_MINOFF; 100694a5d9b6SDavid Greenman (void)memcpy(mtod(m, caddr_t) + sizeof(struct in_addr), 100794a5d9b6SDavid Greenman &ip_srcrt.nop, OPTSIZ); 1008df8bae1dSRodney W. Grimes q = (struct in_addr *)(mtod(m, caddr_t) + 1009df8bae1dSRodney W. Grimes sizeof(struct in_addr) + OPTSIZ); 1010df8bae1dSRodney W. Grimes #undef OPTSIZ 1011df8bae1dSRodney W. Grimes /* 1012df8bae1dSRodney W. Grimes * Record return path as an IP source route, 1013df8bae1dSRodney W. Grimes * reversing the path (pointers are now aligned). 1014df8bae1dSRodney W. Grimes */ 1015df8bae1dSRodney W. Grimes while (p >= ip_srcrt.route) { 1016df8bae1dSRodney W. Grimes #ifdef DIAGNOSTIC 1017df8bae1dSRodney W. Grimes if (ipprintfs) 1018df8bae1dSRodney W. Grimes printf(" %lx", ntohl(q->s_addr)); 1019df8bae1dSRodney W. Grimes #endif 1020df8bae1dSRodney W. Grimes *q++ = *p--; 1021df8bae1dSRodney W. Grimes } 1022df8bae1dSRodney W. Grimes /* 1023df8bae1dSRodney W. Grimes * Last hop goes to final destination. 1024df8bae1dSRodney W. Grimes */ 1025df8bae1dSRodney W. Grimes *q = ip_srcrt.dst; 1026df8bae1dSRodney W. Grimes #ifdef DIAGNOSTIC 1027df8bae1dSRodney W. Grimes if (ipprintfs) 1028df8bae1dSRodney W. Grimes printf(" %lx\n", ntohl(q->s_addr)); 1029df8bae1dSRodney W. Grimes #endif 1030df8bae1dSRodney W. Grimes return (m); 1031df8bae1dSRodney W. Grimes } 1032df8bae1dSRodney W. Grimes 1033df8bae1dSRodney W. Grimes /* 1034df8bae1dSRodney W. Grimes * Strip out IP options, at higher 1035df8bae1dSRodney W. Grimes * level protocol in the kernel. 1036df8bae1dSRodney W. Grimes * Second argument is buffer to which options 1037df8bae1dSRodney W. Grimes * will be moved, and return value is their length. 1038df8bae1dSRodney W. Grimes * XXX should be deleted; last arg currently ignored. 1039df8bae1dSRodney W. Grimes */ 1040df8bae1dSRodney W. Grimes void 1041df8bae1dSRodney W. Grimes ip_stripoptions(m, mopt) 1042df8bae1dSRodney W. Grimes register struct mbuf *m; 1043df8bae1dSRodney W. Grimes struct mbuf *mopt; 1044df8bae1dSRodney W. Grimes { 1045df8bae1dSRodney W. Grimes register int i; 1046df8bae1dSRodney W. Grimes struct ip *ip = mtod(m, struct ip *); 1047df8bae1dSRodney W. Grimes register caddr_t opts; 1048df8bae1dSRodney W. Grimes int olen; 1049df8bae1dSRodney W. Grimes 1050df8bae1dSRodney W. Grimes olen = (ip->ip_hl<<2) - sizeof (struct ip); 1051df8bae1dSRodney W. Grimes opts = (caddr_t)(ip + 1); 1052df8bae1dSRodney W. Grimes i = m->m_len - (sizeof (struct ip) + olen); 1053df8bae1dSRodney W. Grimes bcopy(opts + olen, opts, (unsigned)i); 1054df8bae1dSRodney W. Grimes m->m_len -= olen; 1055df8bae1dSRodney W. Grimes if (m->m_flags & M_PKTHDR) 1056df8bae1dSRodney W. Grimes m->m_pkthdr.len -= olen; 1057df8bae1dSRodney W. Grimes ip->ip_hl = sizeof(struct ip) >> 2; 1058df8bae1dSRodney W. Grimes } 1059df8bae1dSRodney W. Grimes 1060df8bae1dSRodney W. Grimes u_char inetctlerrmap[PRC_NCMDS] = { 1061df8bae1dSRodney W. Grimes 0, 0, 0, 0, 1062df8bae1dSRodney W. Grimes 0, EMSGSIZE, EHOSTDOWN, EHOSTUNREACH, 1063df8bae1dSRodney W. Grimes EHOSTUNREACH, EHOSTUNREACH, ECONNREFUSED, ECONNREFUSED, 1064df8bae1dSRodney W. Grimes EMSGSIZE, EHOSTUNREACH, 0, 0, 1065df8bae1dSRodney W. Grimes 0, 0, 0, 0, 1066df8bae1dSRodney W. Grimes ENOPROTOOPT 1067df8bae1dSRodney W. Grimes }; 1068df8bae1dSRodney W. Grimes 1069df8bae1dSRodney W. Grimes /* 1070df8bae1dSRodney W. Grimes * Forward a packet. If some error occurs return the sender 1071df8bae1dSRodney W. Grimes * an icmp packet. Note we can't always generate a meaningful 1072df8bae1dSRodney W. Grimes * icmp message because icmp doesn't have a large enough repertoire 1073df8bae1dSRodney W. Grimes * of codes and types. 1074df8bae1dSRodney W. Grimes * 1075df8bae1dSRodney W. Grimes * If not forwarding, just drop the packet. This could be confusing 1076df8bae1dSRodney W. Grimes * if ipforwarding was zero but some routing protocol was advancing 1077df8bae1dSRodney W. Grimes * us as a gateway to somewhere. However, we must let the routing 1078df8bae1dSRodney W. Grimes * protocol deal with that. 1079df8bae1dSRodney W. Grimes * 1080df8bae1dSRodney W. Grimes * The srcrt parameter indicates whether the packet is being forwarded 1081df8bae1dSRodney W. Grimes * via a source route. 1082df8bae1dSRodney W. Grimes */ 10830312fbe9SPoul-Henning Kamp static void 1084df8bae1dSRodney W. Grimes ip_forward(m, srcrt) 1085df8bae1dSRodney W. Grimes struct mbuf *m; 1086df8bae1dSRodney W. Grimes int srcrt; 1087df8bae1dSRodney W. Grimes { 1088df8bae1dSRodney W. Grimes register struct ip *ip = mtod(m, struct ip *); 1089df8bae1dSRodney W. Grimes register struct sockaddr_in *sin; 1090df8bae1dSRodney W. Grimes register struct rtentry *rt; 109126f9a767SRodney W. Grimes int error, type = 0, code = 0; 1092df8bae1dSRodney W. Grimes struct mbuf *mcopy; 1093df8bae1dSRodney W. Grimes n_long dest; 1094df8bae1dSRodney W. Grimes struct ifnet *destifp; 1095df8bae1dSRodney W. Grimes 1096df8bae1dSRodney W. Grimes dest = 0; 1097df8bae1dSRodney W. Grimes #ifdef DIAGNOSTIC 1098df8bae1dSRodney W. Grimes if (ipprintfs) 109961ce519bSPoul-Henning Kamp printf("forward: src %lx dst %lx ttl %x\n", 1100623ae52eSPoul-Henning Kamp ip->ip_src.s_addr, ip->ip_dst.s_addr, ip->ip_ttl); 1101df8bae1dSRodney W. Grimes #endif 1102100ba1a6SJordan K. Hubbard 1103100ba1a6SJordan K. Hubbard 1104df8bae1dSRodney W. Grimes if (m->m_flags & M_BCAST || in_canforward(ip->ip_dst) == 0) { 1105df8bae1dSRodney W. Grimes ipstat.ips_cantforward++; 1106df8bae1dSRodney W. Grimes m_freem(m); 1107df8bae1dSRodney W. Grimes return; 1108df8bae1dSRodney W. Grimes } 1109df8bae1dSRodney W. Grimes HTONS(ip->ip_id); 1110df8bae1dSRodney W. Grimes if (ip->ip_ttl <= IPTTLDEC) { 1111df8bae1dSRodney W. Grimes icmp_error(m, ICMP_TIMXCEED, ICMP_TIMXCEED_INTRANS, dest, 0); 1112df8bae1dSRodney W. Grimes return; 1113df8bae1dSRodney W. Grimes } 1114df8bae1dSRodney W. Grimes ip->ip_ttl -= IPTTLDEC; 1115df8bae1dSRodney W. Grimes 1116df8bae1dSRodney W. Grimes sin = (struct sockaddr_in *)&ipforward_rt.ro_dst; 1117df8bae1dSRodney W. Grimes if ((rt = ipforward_rt.ro_rt) == 0 || 1118df8bae1dSRodney W. Grimes ip->ip_dst.s_addr != sin->sin_addr.s_addr) { 1119df8bae1dSRodney W. Grimes if (ipforward_rt.ro_rt) { 1120df8bae1dSRodney W. Grimes RTFREE(ipforward_rt.ro_rt); 1121df8bae1dSRodney W. Grimes ipforward_rt.ro_rt = 0; 1122df8bae1dSRodney W. Grimes } 1123df8bae1dSRodney W. Grimes sin->sin_family = AF_INET; 1124df8bae1dSRodney W. Grimes sin->sin_len = sizeof(*sin); 1125df8bae1dSRodney W. Grimes sin->sin_addr = ip->ip_dst; 1126df8bae1dSRodney W. Grimes 11272c17fe93SGarrett Wollman rtalloc_ign(&ipforward_rt, RTF_PRCLONING); 1128df8bae1dSRodney W. Grimes if (ipforward_rt.ro_rt == 0) { 1129df8bae1dSRodney W. Grimes icmp_error(m, ICMP_UNREACH, ICMP_UNREACH_HOST, dest, 0); 1130df8bae1dSRodney W. Grimes return; 1131df8bae1dSRodney W. Grimes } 1132df8bae1dSRodney W. Grimes rt = ipforward_rt.ro_rt; 1133df8bae1dSRodney W. Grimes } 1134df8bae1dSRodney W. Grimes 1135df8bae1dSRodney W. Grimes /* 1136df8bae1dSRodney W. Grimes * Save at most 64 bytes of the packet in case 1137df8bae1dSRodney W. Grimes * we need to generate an ICMP message to the src. 1138df8bae1dSRodney W. Grimes */ 1139df8bae1dSRodney W. Grimes mcopy = m_copy(m, 0, imin((int)ip->ip_len, 64)); 1140df8bae1dSRodney W. Grimes 1141df8bae1dSRodney W. Grimes /* 1142df8bae1dSRodney W. Grimes * If forwarding packet using same interface that it came in on, 1143df8bae1dSRodney W. Grimes * perhaps should send a redirect to sender to shortcut a hop. 1144df8bae1dSRodney W. Grimes * Only send redirect if source is sending directly to us, 1145df8bae1dSRodney W. Grimes * and if packet was not source routed (or has any options). 1146df8bae1dSRodney W. Grimes * Also, don't send redirect if forwarding using a default route 1147df8bae1dSRodney W. Grimes * or a route modified by a redirect. 1148df8bae1dSRodney W. Grimes */ 1149df8bae1dSRodney W. Grimes #define satosin(sa) ((struct sockaddr_in *)(sa)) 1150df8bae1dSRodney W. Grimes if (rt->rt_ifp == m->m_pkthdr.rcvif && 1151df8bae1dSRodney W. Grimes (rt->rt_flags & (RTF_DYNAMIC|RTF_MODIFIED)) == 0 && 1152df8bae1dSRodney W. Grimes satosin(rt_key(rt))->sin_addr.s_addr != 0 && 1153df8bae1dSRodney W. Grimes ipsendredirects && !srcrt) { 1154df8bae1dSRodney W. Grimes #define RTA(rt) ((struct in_ifaddr *)(rt->rt_ifa)) 1155df8bae1dSRodney W. Grimes u_long src = ntohl(ip->ip_src.s_addr); 1156df8bae1dSRodney W. Grimes 1157df8bae1dSRodney W. Grimes if (RTA(rt) && 1158df8bae1dSRodney W. Grimes (src & RTA(rt)->ia_subnetmask) == RTA(rt)->ia_subnet) { 1159df8bae1dSRodney W. Grimes if (rt->rt_flags & RTF_GATEWAY) 1160df8bae1dSRodney W. Grimes dest = satosin(rt->rt_gateway)->sin_addr.s_addr; 1161df8bae1dSRodney W. Grimes else 1162df8bae1dSRodney W. Grimes dest = ip->ip_dst.s_addr; 1163df8bae1dSRodney W. Grimes /* Router requirements says to only send host redirects */ 1164df8bae1dSRodney W. Grimes type = ICMP_REDIRECT; 1165df8bae1dSRodney W. Grimes code = ICMP_REDIRECT_HOST; 1166df8bae1dSRodney W. Grimes #ifdef DIAGNOSTIC 1167df8bae1dSRodney W. Grimes if (ipprintfs) 1168df8bae1dSRodney W. Grimes printf("redirect (%d) to %lx\n", code, (u_long)dest); 1169df8bae1dSRodney W. Grimes #endif 1170df8bae1dSRodney W. Grimes } 1171df8bae1dSRodney W. Grimes } 1172df8bae1dSRodney W. Grimes 1173df8bae1dSRodney W. Grimes error = ip_output(m, (struct mbuf *)0, &ipforward_rt, IP_FORWARDING 1174df8bae1dSRodney W. Grimes #ifdef DIRECTED_BROADCAST 1175df8bae1dSRodney W. Grimes | IP_ALLOWBROADCAST 1176df8bae1dSRodney W. Grimes #endif 1177df8bae1dSRodney W. Grimes , 0); 1178df8bae1dSRodney W. Grimes if (error) 1179df8bae1dSRodney W. Grimes ipstat.ips_cantforward++; 1180df8bae1dSRodney W. Grimes else { 1181df8bae1dSRodney W. Grimes ipstat.ips_forward++; 1182df8bae1dSRodney W. Grimes if (type) 1183df8bae1dSRodney W. Grimes ipstat.ips_redirectsent++; 1184df8bae1dSRodney W. Grimes else { 1185df8bae1dSRodney W. Grimes if (mcopy) 1186df8bae1dSRodney W. Grimes m_freem(mcopy); 1187df8bae1dSRodney W. Grimes return; 1188df8bae1dSRodney W. Grimes } 1189df8bae1dSRodney W. Grimes } 1190df8bae1dSRodney W. Grimes if (mcopy == NULL) 1191df8bae1dSRodney W. Grimes return; 1192df8bae1dSRodney W. Grimes destifp = NULL; 1193df8bae1dSRodney W. Grimes 1194df8bae1dSRodney W. Grimes switch (error) { 1195df8bae1dSRodney W. Grimes 1196df8bae1dSRodney W. Grimes case 0: /* forwarded, but need redirect */ 1197df8bae1dSRodney W. Grimes /* type, code set above */ 1198df8bae1dSRodney W. Grimes break; 1199df8bae1dSRodney W. Grimes 1200df8bae1dSRodney W. Grimes case ENETUNREACH: /* shouldn't happen, checked above */ 1201df8bae1dSRodney W. Grimes case EHOSTUNREACH: 1202df8bae1dSRodney W. Grimes case ENETDOWN: 1203df8bae1dSRodney W. Grimes case EHOSTDOWN: 1204df8bae1dSRodney W. Grimes default: 1205df8bae1dSRodney W. Grimes type = ICMP_UNREACH; 1206df8bae1dSRodney W. Grimes code = ICMP_UNREACH_HOST; 1207df8bae1dSRodney W. Grimes break; 1208df8bae1dSRodney W. Grimes 1209df8bae1dSRodney W. Grimes case EMSGSIZE: 1210df8bae1dSRodney W. Grimes type = ICMP_UNREACH; 1211df8bae1dSRodney W. Grimes code = ICMP_UNREACH_NEEDFRAG; 1212df8bae1dSRodney W. Grimes if (ipforward_rt.ro_rt) 1213df8bae1dSRodney W. Grimes destifp = ipforward_rt.ro_rt->rt_ifp; 1214df8bae1dSRodney W. Grimes ipstat.ips_cantfrag++; 1215df8bae1dSRodney W. Grimes break; 1216df8bae1dSRodney W. Grimes 1217df8bae1dSRodney W. Grimes case ENOBUFS: 1218df8bae1dSRodney W. Grimes type = ICMP_SOURCEQUENCH; 1219df8bae1dSRodney W. Grimes code = 0; 1220df8bae1dSRodney W. Grimes break; 1221df8bae1dSRodney W. Grimes } 1222df8bae1dSRodney W. Grimes icmp_error(mcopy, type, code, dest, destifp); 1223df8bae1dSRodney W. Grimes } 1224df8bae1dSRodney W. Grimes 1225df8bae1dSRodney W. Grimes int 1226f0068c4aSGarrett Wollman ip_rsvp_init(struct socket *so) 1227f0068c4aSGarrett Wollman { 1228f0068c4aSGarrett Wollman if (so->so_type != SOCK_RAW || 1229f0068c4aSGarrett Wollman so->so_proto->pr_protocol != IPPROTO_RSVP) 1230f0068c4aSGarrett Wollman return EOPNOTSUPP; 1231f0068c4aSGarrett Wollman 1232f0068c4aSGarrett Wollman if (ip_rsvpd != NULL) 1233f0068c4aSGarrett Wollman return EADDRINUSE; 1234f0068c4aSGarrett Wollman 1235f0068c4aSGarrett Wollman ip_rsvpd = so; 12361c5de19aSGarrett Wollman /* 12371c5de19aSGarrett Wollman * This may seem silly, but we need to be sure we don't over-increment 12381c5de19aSGarrett Wollman * the RSVP counter, in case something slips up. 12391c5de19aSGarrett Wollman */ 12401c5de19aSGarrett Wollman if (!ip_rsvp_on) { 12411c5de19aSGarrett Wollman ip_rsvp_on = 1; 12421c5de19aSGarrett Wollman rsvp_on++; 12431c5de19aSGarrett Wollman } 1244f0068c4aSGarrett Wollman 1245f0068c4aSGarrett Wollman return 0; 1246f0068c4aSGarrett Wollman } 1247f0068c4aSGarrett Wollman 1248f0068c4aSGarrett Wollman int 1249f0068c4aSGarrett Wollman ip_rsvp_done(void) 1250f0068c4aSGarrett Wollman { 1251f0068c4aSGarrett Wollman ip_rsvpd = NULL; 12521c5de19aSGarrett Wollman /* 12531c5de19aSGarrett Wollman * This may seem silly, but we need to be sure we don't over-decrement 12541c5de19aSGarrett Wollman * the RSVP counter, in case something slips up. 12551c5de19aSGarrett Wollman */ 12561c5de19aSGarrett Wollman if (ip_rsvp_on) { 12571c5de19aSGarrett Wollman ip_rsvp_on = 0; 12581c5de19aSGarrett Wollman rsvp_on--; 12591c5de19aSGarrett Wollman } 1260f0068c4aSGarrett Wollman return 0; 1261f0068c4aSGarrett Wollman } 1262