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_icmp.c 8.2 (Berkeley) 1/4/94 34c3aac50fSPeter Wemm * $FreeBSD$ 35df8bae1dSRodney W. Grimes */ 36df8bae1dSRodney W. Grimes 376a800098SYoshinobu Inoue #include "opt_ipsec.h" 380070e096SRobert Watson #include "opt_mac.h" 396a800098SYoshinobu Inoue 40df8bae1dSRodney W. Grimes #include <sys/param.h> 41df8bae1dSRodney W. Grimes #include <sys/systm.h> 420070e096SRobert Watson #include <sys/mac.h> 43df8bae1dSRodney W. Grimes #include <sys/mbuf.h> 44df8bae1dSRodney W. Grimes #include <sys/protosw.h> 45df8bae1dSRodney W. Grimes #include <sys/socket.h> 46df8bae1dSRodney W. Grimes #include <sys/time.h> 47df8bae1dSRodney W. Grimes #include <sys/kernel.h> 48b5e8ce9fSBruce Evans #include <sys/sysctl.h> 49df8bae1dSRodney W. Grimes 50df8bae1dSRodney W. Grimes #include <net/if.h> 519494d596SBrooks Davis #include <net/if_types.h> 52df8bae1dSRodney W. Grimes #include <net/route.h> 53df8bae1dSRodney W. Grimes 54df8bae1dSRodney W. Grimes #include <netinet/in.h> 55df8bae1dSRodney W. Grimes #include <netinet/in_systm.h> 56df8bae1dSRodney W. Grimes #include <netinet/in_var.h> 57df8bae1dSRodney W. Grimes #include <netinet/ip.h> 58df8bae1dSRodney W. Grimes #include <netinet/ip_icmp.h> 59b5e8ce9fSBruce Evans #include <netinet/ip_var.h> 60df8bae1dSRodney W. Grimes #include <netinet/icmp_var.h> 61df8bae1dSRodney W. Grimes 626a800098SYoshinobu Inoue #ifdef IPSEC 636a800098SYoshinobu Inoue #include <netinet6/ipsec.h> 646a800098SYoshinobu Inoue #include <netkey/key.h> 656a800098SYoshinobu Inoue #endif 666a800098SYoshinobu Inoue 67b9234fafSSam Leffler #ifdef FAST_IPSEC 68b9234fafSSam Leffler #include <netipsec/ipsec.h> 69b9234fafSSam Leffler #include <netipsec/key.h> 70b9234fafSSam Leffler #define IPSEC 71b9234fafSSam Leffler #endif 72b9234fafSSam Leffler 7372a52a35SJonathan Lemon #include <machine/in_cksum.h> 7472a52a35SJonathan Lemon 75df8bae1dSRodney W. Grimes /* 76df8bae1dSRodney W. Grimes * ICMP routines: error generation, receive packet processing, and 77df8bae1dSRodney W. Grimes * routines to turnaround packets back to the originator, and 78df8bae1dSRodney W. Grimes * host table maintenance routines. 79df8bae1dSRodney W. Grimes */ 80df8bae1dSRodney W. Grimes 81f708ef1bSPoul-Henning Kamp static struct icmpstat icmpstat; 82c73d99b5SRuslan Ermilov SYSCTL_STRUCT(_net_inet_icmp, ICMPCTL_STATS, stats, CTLFLAG_RW, 830312fbe9SPoul-Henning Kamp &icmpstat, icmpstat, ""); 840312fbe9SPoul-Henning Kamp 850312fbe9SPoul-Henning Kamp static int icmpmaskrepl = 0; 860312fbe9SPoul-Henning Kamp SYSCTL_INT(_net_inet_icmp, ICMPCTL_MASKREPL, maskrepl, CTLFLAG_RW, 8757842a38SMatthew N. Dodd &icmpmaskrepl, 0, "Reply to ICMP Address Mask Request packets."); 8857842a38SMatthew N. Dodd 8957842a38SMatthew N. Dodd static u_int icmpmaskfake = 0; 9057842a38SMatthew N. Dodd SYSCTL_UINT(_net_inet_icmp, OID_AUTO, maskfake, CTLFLAG_RW, 9157842a38SMatthew N. Dodd &icmpmaskfake, 0, "Fake reply to ICMP Address Mask Request packets."); 920312fbe9SPoul-Henning Kamp 9318d3153eSDag-Erling Smørgrav static int drop_redirect = 0; 9418d3153eSDag-Erling Smørgrav SYSCTL_INT(_net_inet_icmp, OID_AUTO, drop_redirect, CTLFLAG_RW, 9518d3153eSDag-Erling Smørgrav &drop_redirect, 0, ""); 9618d3153eSDag-Erling Smørgrav 976c3b5f69SDag-Erling Smørgrav static int log_redirect = 0; 986c3b5f69SDag-Erling Smørgrav SYSCTL_INT(_net_inet_icmp, OID_AUTO, log_redirect, CTLFLAG_RW, 996c3b5f69SDag-Erling Smørgrav &log_redirect, 0, ""); 1006c3b5f69SDag-Erling Smørgrav 101173c0f9fSWarner Losh static int icmplim = 200; 10251508de1SMatthew Dillon SYSCTL_INT(_net_inet_icmp, ICMPCTL_ICMPLIM, icmplim, CTLFLAG_RW, 10351508de1SMatthew Dillon &icmplim, 0, ""); 1045fce7fc4SMatthew Dillon 1054f14ee00SDan Moschuk static int icmplim_output = 1; 1064f14ee00SDan Moschuk SYSCTL_INT(_net_inet_icmp, OID_AUTO, icmplim_output, CTLFLAG_RW, 1074f14ee00SDan Moschuk &icmplim_output, 0, ""); 10851508de1SMatthew Dillon 1095fce7fc4SMatthew Dillon /* 1105fce7fc4SMatthew Dillon * ICMP broadcast echo sysctl 1115fce7fc4SMatthew Dillon */ 1125fce7fc4SMatthew Dillon 11361a4defdSJoseph Koshy static int icmpbmcastecho = 0; 11418d3153eSDag-Erling Smørgrav SYSCTL_INT(_net_inet_icmp, OID_AUTO, bmcastecho, CTLFLAG_RW, 11518d3153eSDag-Erling Smørgrav &icmpbmcastecho, 0, ""); 1167022ea0aSGarrett Wollman 11751508de1SMatthew Dillon 118df8bae1dSRodney W. Grimes #ifdef ICMPPRINTFS 119df8bae1dSRodney W. Grimes int icmpprintfs = 0; 120df8bae1dSRodney W. Grimes #endif 121df8bae1dSRodney W. Grimes 1224d77a549SAlfred Perlstein static void icmp_reflect(struct mbuf *); 1234d77a549SAlfred Perlstein static void icmp_send(struct mbuf *, struct mbuf *, struct route *); 1244d77a549SAlfred Perlstein static int ip_next_mtu(int, int); 1250312fbe9SPoul-Henning Kamp 126df8bae1dSRodney W. Grimes extern struct protosw inetsw[]; 127df8bae1dSRodney W. Grimes 128df8bae1dSRodney W. Grimes /* 129df8bae1dSRodney W. Grimes * Generate an error packet of type error 130df8bae1dSRodney W. Grimes * in response to bad packet ip. 131df8bae1dSRodney W. Grimes */ 132df8bae1dSRodney W. Grimes void 133df8bae1dSRodney W. Grimes icmp_error(n, type, code, dest, destifp) 134df8bae1dSRodney W. Grimes struct mbuf *n; 135df8bae1dSRodney W. Grimes int type, code; 136df8bae1dSRodney W. Grimes n_long dest; 137df8bae1dSRodney W. Grimes struct ifnet *destifp; 138df8bae1dSRodney W. Grimes { 139df8bae1dSRodney W. Grimes register struct ip *oip = mtod(n, struct ip *), *nip; 14053be11f6SPoul-Henning Kamp register unsigned oiplen = oip->ip_hl << 2; 141df8bae1dSRodney W. Grimes register struct icmp *icp; 142df8bae1dSRodney W. Grimes register struct mbuf *m; 143df8bae1dSRodney W. Grimes unsigned icmplen; 144df8bae1dSRodney W. Grimes 145df8bae1dSRodney W. Grimes #ifdef ICMPPRINTFS 146df8bae1dSRodney W. Grimes if (icmpprintfs) 147623ae52eSPoul-Henning Kamp printf("icmp_error(%p, %x, %d)\n", oip, type, code); 148df8bae1dSRodney W. Grimes #endif 149df8bae1dSRodney W. Grimes if (type != ICMP_REDIRECT) 150df8bae1dSRodney W. Grimes icmpstat.icps_error++; 151df8bae1dSRodney W. Grimes /* 152df8bae1dSRodney W. Grimes * Don't send error if not the first fragment of message. 153df8bae1dSRodney W. Grimes * Don't error if the old packet protocol was ICMP 154df8bae1dSRodney W. Grimes * error message, only known informational types. 155df8bae1dSRodney W. Grimes */ 156df8bae1dSRodney W. Grimes if (oip->ip_off &~ (IP_MF|IP_DF)) 157df8bae1dSRodney W. Grimes goto freeit; 158df8bae1dSRodney W. Grimes if (oip->ip_p == IPPROTO_ICMP && type != ICMP_REDIRECT && 159df8bae1dSRodney W. Grimes n->m_len >= oiplen + ICMP_MINLEN && 160df8bae1dSRodney W. Grimes !ICMP_INFOTYPE(((struct icmp *)((caddr_t)oip + oiplen))->icmp_type)) { 161df8bae1dSRodney W. Grimes icmpstat.icps_oldicmp++; 162df8bae1dSRodney W. Grimes goto freeit; 163df8bae1dSRodney W. Grimes } 164df8bae1dSRodney W. Grimes /* Don't send error in response to a multicast or broadcast packet */ 165df8bae1dSRodney W. Grimes if (n->m_flags & (M_BCAST|M_MCAST)) 166df8bae1dSRodney W. Grimes goto freeit; 167df8bae1dSRodney W. Grimes /* 168df8bae1dSRodney W. Grimes * First, formulate icmp message 169df8bae1dSRodney W. Grimes */ 170a163d034SWarner Losh m = m_gethdr(M_DONTWAIT, MT_HEADER); 171df8bae1dSRodney W. Grimes if (m == NULL) 172df8bae1dSRodney W. Grimes goto freeit; 1730070e096SRobert Watson #ifdef MAC 1740070e096SRobert Watson mac_create_mbuf_netlayer(n, m); 1750070e096SRobert Watson #endif 1761d027522SRuslan Ermilov icmplen = min(oiplen + 8, oip->ip_len); 177bfef7ed4SIan Dowse if (icmplen < sizeof(struct ip)) 178bfef7ed4SIan Dowse panic("icmp_error: bad length"); 179df8bae1dSRodney W. Grimes m->m_len = icmplen + ICMP_MINLEN; 180df8bae1dSRodney W. Grimes MH_ALIGN(m, m->m_len); 181df8bae1dSRodney W. Grimes icp = mtod(m, struct icmp *); 182df8bae1dSRodney W. Grimes if ((u_int)type > ICMP_MAXTYPE) 183df8bae1dSRodney W. Grimes panic("icmp_error"); 184df8bae1dSRodney W. Grimes icmpstat.icps_outhist[type]++; 185df8bae1dSRodney W. Grimes icp->icmp_type = type; 186df8bae1dSRodney W. Grimes if (type == ICMP_REDIRECT) 187df8bae1dSRodney W. Grimes icp->icmp_gwaddr.s_addr = dest; 188df8bae1dSRodney W. Grimes else { 189df8bae1dSRodney W. Grimes icp->icmp_void = 0; 190df8bae1dSRodney W. Grimes /* 191df8bae1dSRodney W. Grimes * The following assignments assume an overlay with the 192df8bae1dSRodney W. Grimes * zeroed icmp_void field. 193df8bae1dSRodney W. Grimes */ 194df8bae1dSRodney W. Grimes if (type == ICMP_PARAMPROB) { 195df8bae1dSRodney W. Grimes icp->icmp_pptr = code; 196df8bae1dSRodney W. Grimes code = 0; 197df8bae1dSRodney W. Grimes } else if (type == ICMP_UNREACH && 198df8bae1dSRodney W. Grimes code == ICMP_UNREACH_NEEDFRAG && destifp) { 199df8bae1dSRodney W. Grimes icp->icmp_nextmtu = htons(destifp->if_mtu); 200df8bae1dSRodney W. Grimes } 201df8bae1dSRodney W. Grimes } 202df8bae1dSRodney W. Grimes 203df8bae1dSRodney W. Grimes icp->icmp_code = code; 204bfef7ed4SIan Dowse m_copydata(n, 0, icmplen, (caddr_t)&icp->icmp_ip); 205df8bae1dSRodney W. Grimes nip = &icp->icmp_ip; 20604287599SRuslan Ermilov 20704287599SRuslan Ermilov /* 20804287599SRuslan Ermilov * Convert fields to network representation. 20904287599SRuslan Ermilov */ 210fd8e4ebcSMike Barcroft nip->ip_len = htons(nip->ip_len); 211fd8e4ebcSMike Barcroft nip->ip_off = htons(nip->ip_off); 212df8bae1dSRodney W. Grimes 213df8bae1dSRodney W. Grimes /* 214df8bae1dSRodney W. Grimes * Now, copy old ip header (without options) 215df8bae1dSRodney W. Grimes * in front of icmp message. 216df8bae1dSRodney W. Grimes */ 217df8bae1dSRodney W. Grimes if (m->m_data - sizeof(struct ip) < m->m_pktdat) 218df8bae1dSRodney W. Grimes panic("icmp len"); 219df8bae1dSRodney W. Grimes m->m_data -= sizeof(struct ip); 220df8bae1dSRodney W. Grimes m->m_len += sizeof(struct ip); 221df8bae1dSRodney W. Grimes m->m_pkthdr.len = m->m_len; 222df8bae1dSRodney W. Grimes m->m_pkthdr.rcvif = n->m_pkthdr.rcvif; 223df8bae1dSRodney W. Grimes nip = mtod(m, struct ip *); 224df8bae1dSRodney W. Grimes bcopy((caddr_t)oip, (caddr_t)nip, sizeof(struct ip)); 225df8bae1dSRodney W. Grimes nip->ip_len = m->m_len; 22653be11f6SPoul-Henning Kamp nip->ip_v = IPVERSION; 22753be11f6SPoul-Henning Kamp nip->ip_hl = 5; 228df8bae1dSRodney W. Grimes nip->ip_p = IPPROTO_ICMP; 229df8bae1dSRodney W. Grimes nip->ip_tos = 0; 230df8bae1dSRodney W. Grimes icmp_reflect(m); 231df8bae1dSRodney W. Grimes 232df8bae1dSRodney W. Grimes freeit: 233df8bae1dSRodney W. Grimes m_freem(n); 234df8bae1dSRodney W. Grimes } 235df8bae1dSRodney W. Grimes 236df8bae1dSRodney W. Grimes static struct sockaddr_in icmpsrc = { sizeof (struct sockaddr_in), AF_INET }; 237df8bae1dSRodney W. Grimes static struct sockaddr_in icmpdst = { sizeof (struct sockaddr_in), AF_INET }; 238df8bae1dSRodney W. Grimes static struct sockaddr_in icmpgw = { sizeof (struct sockaddr_in), AF_INET }; 239df8bae1dSRodney W. Grimes 240df8bae1dSRodney W. Grimes /* 241df8bae1dSRodney W. Grimes * Process a received ICMP message. 242df8bae1dSRodney W. Grimes */ 243df8bae1dSRodney W. Grimes void 244f0ffb944SJulian Elischer icmp_input(m, off) 245df8bae1dSRodney W. Grimes register struct mbuf *m; 246f0ffb944SJulian Elischer int off; 247df8bae1dSRodney W. Grimes { 2486a800098SYoshinobu Inoue int hlen = off; 249df8bae1dSRodney W. Grimes register struct icmp *icp; 250df8bae1dSRodney W. Grimes register struct ip *ip = mtod(m, struct ip *); 251df8bae1dSRodney W. Grimes int icmplen = ip->ip_len; 252df8bae1dSRodney W. Grimes register int i; 253df8bae1dSRodney W. Grimes struct in_ifaddr *ia; 2544d77a549SAlfred Perlstein void (*ctlfunc)(int, struct sockaddr *, void *); 255df8bae1dSRodney W. Grimes int code; 256df8bae1dSRodney W. Grimes 257df8bae1dSRodney W. Grimes /* 258df8bae1dSRodney W. Grimes * Locate icmp structure in mbuf, and check 259df8bae1dSRodney W. Grimes * that not corrupted and of at least minimum length. 260df8bae1dSRodney W. Grimes */ 261df8bae1dSRodney W. Grimes #ifdef ICMPPRINTFS 2622b758395SGarrett Wollman if (icmpprintfs) { 2632b758395SGarrett Wollman char buf[4 * sizeof "123"]; 2642b758395SGarrett Wollman strcpy(buf, inet_ntoa(ip->ip_src)); 2652b758395SGarrett Wollman printf("icmp_input from %s to %s, len %d\n", 2662b758395SGarrett Wollman buf, inet_ntoa(ip->ip_dst), icmplen); 2672b758395SGarrett Wollman } 268df8bae1dSRodney W. Grimes #endif 269df8bae1dSRodney W. Grimes if (icmplen < ICMP_MINLEN) { 270df8bae1dSRodney W. Grimes icmpstat.icps_tooshort++; 271df8bae1dSRodney W. Grimes goto freeit; 272df8bae1dSRodney W. Grimes } 273df8bae1dSRodney W. Grimes i = hlen + min(icmplen, ICMP_ADVLENMIN); 274df8bae1dSRodney W. Grimes if (m->m_len < i && (m = m_pullup(m, i)) == 0) { 275df8bae1dSRodney W. Grimes icmpstat.icps_tooshort++; 276df8bae1dSRodney W. Grimes return; 277df8bae1dSRodney W. Grimes } 278df8bae1dSRodney W. Grimes ip = mtod(m, struct ip *); 279df8bae1dSRodney W. Grimes m->m_len -= hlen; 280df8bae1dSRodney W. Grimes m->m_data += hlen; 281df8bae1dSRodney W. Grimes icp = mtod(m, struct icmp *); 282df8bae1dSRodney W. Grimes if (in_cksum(m, icmplen)) { 283df8bae1dSRodney W. Grimes icmpstat.icps_checksum++; 284df8bae1dSRodney W. Grimes goto freeit; 285df8bae1dSRodney W. Grimes } 286df8bae1dSRodney W. Grimes m->m_len += hlen; 287df8bae1dSRodney W. Grimes m->m_data -= hlen; 288df8bae1dSRodney W. Grimes 2896a800098SYoshinobu Inoue if (m->m_pkthdr.rcvif && m->m_pkthdr.rcvif->if_type == IFT_FAITH) { 2906a800098SYoshinobu Inoue /* 2916a800098SYoshinobu Inoue * Deliver very specific ICMP type only. 2926a800098SYoshinobu Inoue */ 2936a800098SYoshinobu Inoue switch (icp->icmp_type) { 2946a800098SYoshinobu Inoue case ICMP_UNREACH: 2956a800098SYoshinobu Inoue case ICMP_TIMXCEED: 2966a800098SYoshinobu Inoue break; 2976a800098SYoshinobu Inoue default: 2986a800098SYoshinobu Inoue goto freeit; 2996a800098SYoshinobu Inoue } 3006a800098SYoshinobu Inoue } 3016a800098SYoshinobu Inoue 302df8bae1dSRodney W. Grimes #ifdef ICMPPRINTFS 303df8bae1dSRodney W. Grimes if (icmpprintfs) 304df8bae1dSRodney W. Grimes printf("icmp_input, type %d code %d\n", icp->icmp_type, 305df8bae1dSRodney W. Grimes icp->icmp_code); 306df8bae1dSRodney W. Grimes #endif 3075b7ee6edSGarrett Wollman 3085b7ee6edSGarrett Wollman /* 3095b7ee6edSGarrett Wollman * Message type specific processing. 3105b7ee6edSGarrett Wollman */ 311df8bae1dSRodney W. Grimes if (icp->icmp_type > ICMP_MAXTYPE) 312df8bae1dSRodney W. Grimes goto raw; 313df8bae1dSRodney W. Grimes icmpstat.icps_inhist[icp->icmp_type]++; 314df8bae1dSRodney W. Grimes code = icp->icmp_code; 315df8bae1dSRodney W. Grimes switch (icp->icmp_type) { 316df8bae1dSRodney W. Grimes 317df8bae1dSRodney W. Grimes case ICMP_UNREACH: 318df8bae1dSRodney W. Grimes switch (code) { 319df8bae1dSRodney W. Grimes case ICMP_UNREACH_NET: 320df8bae1dSRodney W. Grimes case ICMP_UNREACH_HOST: 321df8bae1dSRodney W. Grimes case ICMP_UNREACH_SRCFAIL: 322e4bb5b05SJonathan Lemon case ICMP_UNREACH_NET_UNKNOWN: 323e4bb5b05SJonathan Lemon case ICMP_UNREACH_HOST_UNKNOWN: 324e4bb5b05SJonathan Lemon case ICMP_UNREACH_ISOLATED: 325e4bb5b05SJonathan Lemon case ICMP_UNREACH_TOSNET: 326e4bb5b05SJonathan Lemon case ICMP_UNREACH_TOSHOST: 327e4bb5b05SJonathan Lemon case ICMP_UNREACH_HOST_PRECEDENCE: 328e4bb5b05SJonathan Lemon case ICMP_UNREACH_PRECEDENCE_CUTOFF: 329e4bb5b05SJonathan Lemon code = PRC_UNREACH_NET; 330df8bae1dSRodney W. Grimes break; 331df8bae1dSRodney W. Grimes 332df8bae1dSRodney W. Grimes case ICMP_UNREACH_NEEDFRAG: 333df8bae1dSRodney W. Grimes code = PRC_MSGSIZE; 334df8bae1dSRodney W. Grimes break; 335df8bae1dSRodney W. Grimes 336e4bb5b05SJonathan Lemon /* 337e4bb5b05SJonathan Lemon * RFC 1122, Sections 3.2.2.1 and 4.2.3.9. 338e4bb5b05SJonathan Lemon * Treat subcodes 2,3 as immediate RST 339e4bb5b05SJonathan Lemon */ 340e4bb5b05SJonathan Lemon case ICMP_UNREACH_PROTOCOL: 341e4bb5b05SJonathan Lemon case ICMP_UNREACH_PORT: 342b77d155dSJesper Skriver code = PRC_UNREACH_PORT; 343b11d7a4aSPoul-Henning Kamp break; 34490fcbbd6SPoul-Henning Kamp 34590fcbbd6SPoul-Henning Kamp case ICMP_UNREACH_NET_PROHIB: 34690fcbbd6SPoul-Henning Kamp case ICMP_UNREACH_HOST_PROHIB: 3479c4b2574SPaul Traina case ICMP_UNREACH_FILTER_PROHIB: 34890fcbbd6SPoul-Henning Kamp code = PRC_UNREACH_ADMIN_PROHIB; 349b11d7a4aSPoul-Henning Kamp break; 350b11d7a4aSPoul-Henning Kamp 351df8bae1dSRodney W. Grimes default: 352df8bae1dSRodney W. Grimes goto badcode; 353df8bae1dSRodney W. Grimes } 354df8bae1dSRodney W. Grimes goto deliver; 355df8bae1dSRodney W. Grimes 356df8bae1dSRodney W. Grimes case ICMP_TIMXCEED: 357df8bae1dSRodney W. Grimes if (code > 1) 358df8bae1dSRodney W. Grimes goto badcode; 359df8bae1dSRodney W. Grimes code += PRC_TIMXCEED_INTRANS; 360df8bae1dSRodney W. Grimes goto deliver; 361df8bae1dSRodney W. Grimes 362df8bae1dSRodney W. Grimes case ICMP_PARAMPROB: 363df8bae1dSRodney W. Grimes if (code > 1) 364df8bae1dSRodney W. Grimes goto badcode; 365df8bae1dSRodney W. Grimes code = PRC_PARAMPROB; 366df8bae1dSRodney W. Grimes goto deliver; 367df8bae1dSRodney W. Grimes 368df8bae1dSRodney W. Grimes case ICMP_SOURCEQUENCH: 369df8bae1dSRodney W. Grimes if (code) 370df8bae1dSRodney W. Grimes goto badcode; 371df8bae1dSRodney W. Grimes code = PRC_QUENCH; 372df8bae1dSRodney W. Grimes deliver: 373df8bae1dSRodney W. Grimes /* 374df8bae1dSRodney W. Grimes * Problem with datagram; advise higher level routines. 375df8bae1dSRodney W. Grimes */ 376df8bae1dSRodney W. Grimes if (icmplen < ICMP_ADVLENMIN || icmplen < ICMP_ADVLEN(icp) || 37753be11f6SPoul-Henning Kamp icp->icmp_ip.ip_hl < (sizeof(struct ip) >> 2)) { 378df8bae1dSRodney W. Grimes icmpstat.icps_badlen++; 379df8bae1dSRodney W. Grimes goto freeit; 380df8bae1dSRodney W. Grimes } 381fd8e4ebcSMike Barcroft icp->icmp_ip.ip_len = ntohs(icp->icmp_ip.ip_len); 3825b7ee6edSGarrett Wollman /* Discard ICMP's in response to multicast packets */ 3835b7ee6edSGarrett Wollman if (IN_MULTICAST(ntohl(icp->icmp_ip.ip_dst.s_addr))) 3845b7ee6edSGarrett Wollman goto badcode; 385df8bae1dSRodney W. Grimes #ifdef ICMPPRINTFS 386df8bae1dSRodney W. Grimes if (icmpprintfs) 387df8bae1dSRodney W. Grimes printf("deliver to protocol %d\n", icp->icmp_ip.ip_p); 388df8bae1dSRodney W. Grimes #endif 389df8bae1dSRodney W. Grimes icmpsrc.sin_addr = icp->icmp_ip.ip_dst; 390b7a44e34SGarrett Wollman #if 1 3915cbf3e08SGarrett Wollman /* 3925cbf3e08SGarrett Wollman * MTU discovery: 3935cbf3e08SGarrett Wollman * If we got a needfrag and there is a host route to the 3945cbf3e08SGarrett Wollman * original destination, and the MTU is not locked, then 3955cbf3e08SGarrett Wollman * set the MTU in the route to the suggested new value 3965cbf3e08SGarrett Wollman * (if given) and then notify as usual. The ULPs will 3975cbf3e08SGarrett Wollman * notice that the MTU has changed and adapt accordingly. 3985cbf3e08SGarrett Wollman * If no new MTU was suggested, then we guess a new one 3995cbf3e08SGarrett Wollman * less than the current value. If the new MTU is 4005cbf3e08SGarrett Wollman * unreasonably small (arbitrarily set at 296), then 4015cbf3e08SGarrett Wollman * we reset the MTU to the interface value and enable the 4025cbf3e08SGarrett Wollman * lock bit, indicating that we are no longer doing MTU 4035cbf3e08SGarrett Wollman * discovery. 4045cbf3e08SGarrett Wollman */ 4055cbf3e08SGarrett Wollman if (code == PRC_MSGSIZE) { 4065cbf3e08SGarrett Wollman struct rtentry *rt; 4075cbf3e08SGarrett Wollman int mtu; 4085cbf3e08SGarrett Wollman 4095cbf3e08SGarrett Wollman rt = rtalloc1((struct sockaddr *)&icmpsrc, 0, 4105cbf3e08SGarrett Wollman RTF_CLONING | RTF_PRCLONING); 4115cbf3e08SGarrett Wollman if (rt && (rt->rt_flags & RTF_HOST) 4125cbf3e08SGarrett Wollman && !(rt->rt_rmx.rmx_locks & RTV_MTU)) { 4135cbf3e08SGarrett Wollman mtu = ntohs(icp->icmp_nextmtu); 4145cbf3e08SGarrett Wollman if (!mtu) 4155cbf3e08SGarrett Wollman mtu = ip_next_mtu(rt->rt_rmx.rmx_mtu, 4165cbf3e08SGarrett Wollman 1); 417be070f43SGarrett Wollman #ifdef DEBUG_MTUDISC 418be070f43SGarrett Wollman printf("MTU for %s reduced to %d\n", 419be070f43SGarrett Wollman inet_ntoa(icmpsrc.sin_addr), mtu); 420be070f43SGarrett Wollman #endif 421be070f43SGarrett Wollman if (mtu < 296) { 422b7a44e34SGarrett Wollman /* rt->rt_rmx.rmx_mtu = 423b7a44e34SGarrett Wollman rt->rt_ifp->if_mtu; */ 4245cbf3e08SGarrett Wollman rt->rt_rmx.rmx_locks |= RTV_MTU; 4255cbf3e08SGarrett Wollman } else if (rt->rt_rmx.rmx_mtu > mtu) { 4265cbf3e08SGarrett Wollman rt->rt_rmx.rmx_mtu = mtu; 4275cbf3e08SGarrett Wollman } 4285cbf3e08SGarrett Wollman } 4295cbf3e08SGarrett Wollman if (rt) 4305cbf3e08SGarrett Wollman RTFREE(rt); 4315cbf3e08SGarrett Wollman } 4325cbf3e08SGarrett Wollman 433b7a44e34SGarrett Wollman #endif 4346a800098SYoshinobu Inoue /* 4356a800098SYoshinobu Inoue * XXX if the packet contains [IPv4 AH TCP], we can't make a 4366a800098SYoshinobu Inoue * notification to TCP layer. 4376a800098SYoshinobu Inoue */ 438623ae52eSPoul-Henning Kamp ctlfunc = inetsw[ip_protox[icp->icmp_ip.ip_p]].pr_ctlinput; 439623ae52eSPoul-Henning Kamp if (ctlfunc) 440df8bae1dSRodney W. Grimes (*ctlfunc)(code, (struct sockaddr *)&icmpsrc, 441b62d102cSBruce Evans (void *)&icp->icmp_ip); 442df8bae1dSRodney W. Grimes break; 443df8bae1dSRodney W. Grimes 444df8bae1dSRodney W. Grimes badcode: 445df8bae1dSRodney W. Grimes icmpstat.icps_badcode++; 446df8bae1dSRodney W. Grimes break; 447df8bae1dSRodney W. Grimes 448df8bae1dSRodney W. Grimes case ICMP_ECHO: 4497022ea0aSGarrett Wollman if (!icmpbmcastecho 450d311884fSDavid Greenman && (m->m_flags & (M_MCAST | M_BCAST)) != 0) { 4517022ea0aSGarrett Wollman icmpstat.icps_bmcastecho++; 4527022ea0aSGarrett Wollman break; 4537022ea0aSGarrett Wollman } 454df8bae1dSRodney W. Grimes icp->icmp_type = ICMP_ECHOREPLY; 455a57815efSBosko Milekic if (badport_bandlim(BANDLIM_ICMP_ECHO) < 0) 45609f81a46SBosko Milekic goto freeit; 45709f81a46SBosko Milekic else 458df8bae1dSRodney W. Grimes goto reflect; 459df8bae1dSRodney W. Grimes 460df8bae1dSRodney W. Grimes case ICMP_TSTAMP: 461fe0fb8abSGarrett Wollman if (!icmpbmcastecho 462d311884fSDavid Greenman && (m->m_flags & (M_MCAST | M_BCAST)) != 0) { 463fe0fb8abSGarrett Wollman icmpstat.icps_bmcasttstamp++; 464fe0fb8abSGarrett Wollman break; 465fe0fb8abSGarrett Wollman } 466df8bae1dSRodney W. Grimes if (icmplen < ICMP_TSLEN) { 467df8bae1dSRodney W. Grimes icmpstat.icps_badlen++; 468df8bae1dSRodney W. Grimes break; 469df8bae1dSRodney W. Grimes } 470df8bae1dSRodney W. Grimes icp->icmp_type = ICMP_TSTAMPREPLY; 471df8bae1dSRodney W. Grimes icp->icmp_rtime = iptime(); 472df8bae1dSRodney W. Grimes icp->icmp_ttime = icp->icmp_rtime; /* bogus, do later! */ 473a57815efSBosko Milekic if (badport_bandlim(BANDLIM_ICMP_TSTAMP) < 0) 47409f81a46SBosko Milekic goto freeit; 47509f81a46SBosko Milekic else 476df8bae1dSRodney W. Grimes goto reflect; 477df8bae1dSRodney W. Grimes 478df8bae1dSRodney W. Grimes case ICMP_MASKREQ: 479df8bae1dSRodney W. Grimes if (icmpmaskrepl == 0) 480df8bae1dSRodney W. Grimes break; 481df8bae1dSRodney W. Grimes /* 482df8bae1dSRodney W. Grimes * We are not able to respond with all ones broadcast 483df8bae1dSRodney W. Grimes * unless we receive it over a point-to-point interface. 484df8bae1dSRodney W. Grimes */ 485df8bae1dSRodney W. Grimes if (icmplen < ICMP_MASKLEN) 486df8bae1dSRodney W. Grimes break; 487df8bae1dSRodney W. Grimes switch (ip->ip_dst.s_addr) { 488df8bae1dSRodney W. Grimes 489df8bae1dSRodney W. Grimes case INADDR_BROADCAST: 490df8bae1dSRodney W. Grimes case INADDR_ANY: 491df8bae1dSRodney W. Grimes icmpdst.sin_addr = ip->ip_src; 492df8bae1dSRodney W. Grimes break; 493df8bae1dSRodney W. Grimes 494df8bae1dSRodney W. Grimes default: 495df8bae1dSRodney W. Grimes icmpdst.sin_addr = ip->ip_dst; 496df8bae1dSRodney W. Grimes } 497df8bae1dSRodney W. Grimes ia = (struct in_ifaddr *)ifaof_ifpforaddr( 498df8bae1dSRodney W. Grimes (struct sockaddr *)&icmpdst, m->m_pkthdr.rcvif); 499df8bae1dSRodney W. Grimes if (ia == 0) 500df8bae1dSRodney W. Grimes break; 5017e6f7714SPoul-Henning Kamp if (ia->ia_ifp == 0) 5027e6f7714SPoul-Henning Kamp break; 503df8bae1dSRodney W. Grimes icp->icmp_type = ICMP_MASKREPLY; 50457842a38SMatthew N. Dodd if (icmpmaskfake == 0) 505df8bae1dSRodney W. Grimes icp->icmp_mask = ia->ia_sockmask.sin_addr.s_addr; 50657842a38SMatthew N. Dodd else 50757842a38SMatthew N. Dodd icp->icmp_mask = icmpmaskfake; 508df8bae1dSRodney W. Grimes if (ip->ip_src.s_addr == 0) { 509df8bae1dSRodney W. Grimes if (ia->ia_ifp->if_flags & IFF_BROADCAST) 510df8bae1dSRodney W. Grimes ip->ip_src = satosin(&ia->ia_broadaddr)->sin_addr; 511df8bae1dSRodney W. Grimes else if (ia->ia_ifp->if_flags & IFF_POINTOPOINT) 512df8bae1dSRodney W. Grimes ip->ip_src = satosin(&ia->ia_dstaddr)->sin_addr; 513df8bae1dSRodney W. Grimes } 514df8bae1dSRodney W. Grimes reflect: 515df8bae1dSRodney W. Grimes ip->ip_len += hlen; /* since ip_input deducts this */ 516df8bae1dSRodney W. Grimes icmpstat.icps_reflect++; 517df8bae1dSRodney W. Grimes icmpstat.icps_outhist[icp->icmp_type]++; 518df8bae1dSRodney W. Grimes icmp_reflect(m); 519df8bae1dSRodney W. Grimes return; 520df8bae1dSRodney W. Grimes 521df8bae1dSRodney W. Grimes case ICMP_REDIRECT: 52218d3153eSDag-Erling Smørgrav if (log_redirect) { 52318d3153eSDag-Erling Smørgrav u_long src, dst, gw; 52418d3153eSDag-Erling Smørgrav 52518d3153eSDag-Erling Smørgrav src = ntohl(ip->ip_src.s_addr); 52618d3153eSDag-Erling Smørgrav dst = ntohl(icp->icmp_ip.ip_dst.s_addr); 52718d3153eSDag-Erling Smørgrav gw = ntohl(icp->icmp_gwaddr.s_addr); 52818d3153eSDag-Erling Smørgrav printf("icmp redirect from %d.%d.%d.%d: " 52918d3153eSDag-Erling Smørgrav "%d.%d.%d.%d => %d.%d.%d.%d\n", 53018d3153eSDag-Erling Smørgrav (int)(src >> 24), (int)((src >> 16) & 0xff), 53118d3153eSDag-Erling Smørgrav (int)((src >> 8) & 0xff), (int)(src & 0xff), 53218d3153eSDag-Erling Smørgrav (int)(dst >> 24), (int)((dst >> 16) & 0xff), 53318d3153eSDag-Erling Smørgrav (int)((dst >> 8) & 0xff), (int)(dst & 0xff), 53418d3153eSDag-Erling Smørgrav (int)(gw >> 24), (int)((gw >> 16) & 0xff), 53518d3153eSDag-Erling Smørgrav (int)((gw >> 8) & 0xff), (int)(gw & 0xff)); 53618d3153eSDag-Erling Smørgrav } 53718d3153eSDag-Erling Smørgrav if (drop_redirect) 53818d3153eSDag-Erling Smørgrav break; 539df8bae1dSRodney W. Grimes if (code > 3) 540df8bae1dSRodney W. Grimes goto badcode; 541df8bae1dSRodney W. Grimes if (icmplen < ICMP_ADVLENMIN || icmplen < ICMP_ADVLEN(icp) || 54253be11f6SPoul-Henning Kamp icp->icmp_ip.ip_hl < (sizeof(struct ip) >> 2)) { 543df8bae1dSRodney W. Grimes icmpstat.icps_badlen++; 544df8bae1dSRodney W. Grimes break; 545df8bae1dSRodney W. Grimes } 546df8bae1dSRodney W. Grimes /* 547df8bae1dSRodney W. Grimes * Short circuit routing redirects to force 548df8bae1dSRodney W. Grimes * immediate change in the kernel's routing 549df8bae1dSRodney W. Grimes * tables. The message is also handed to anyone 550df8bae1dSRodney W. Grimes * listening on a raw socket (e.g. the routing 551df8bae1dSRodney W. Grimes * daemon for use in updating its tables). 552df8bae1dSRodney W. Grimes */ 553df8bae1dSRodney W. Grimes icmpgw.sin_addr = ip->ip_src; 554df8bae1dSRodney W. Grimes icmpdst.sin_addr = icp->icmp_gwaddr; 555df8bae1dSRodney W. Grimes #ifdef ICMPPRINTFS 5562b758395SGarrett Wollman if (icmpprintfs) { 5572b758395SGarrett Wollman char buf[4 * sizeof "123"]; 5582b758395SGarrett Wollman strcpy(buf, inet_ntoa(icp->icmp_ip.ip_dst)); 5592b758395SGarrett Wollman 5602b758395SGarrett Wollman printf("redirect dst %s to %s\n", 5612b758395SGarrett Wollman buf, inet_ntoa(icp->icmp_gwaddr)); 5622b758395SGarrett Wollman } 563df8bae1dSRodney W. Grimes #endif 564df8bae1dSRodney W. Grimes icmpsrc.sin_addr = icp->icmp_ip.ip_dst; 565df8bae1dSRodney W. Grimes rtredirect((struct sockaddr *)&icmpsrc, 566df8bae1dSRodney W. Grimes (struct sockaddr *)&icmpdst, 567df8bae1dSRodney W. Grimes (struct sockaddr *)0, RTF_GATEWAY | RTF_HOST, 568df8bae1dSRodney W. Grimes (struct sockaddr *)&icmpgw, (struct rtentry **)0); 569df8bae1dSRodney W. Grimes pfctlinput(PRC_REDIRECT_HOST, (struct sockaddr *)&icmpsrc); 5706a800098SYoshinobu Inoue #ifdef IPSEC 5716a800098SYoshinobu Inoue key_sa_routechange((struct sockaddr *)&icmpsrc); 5726a800098SYoshinobu Inoue #endif 573df8bae1dSRodney W. Grimes break; 574df8bae1dSRodney W. Grimes 575df8bae1dSRodney W. Grimes /* 576df8bae1dSRodney W. Grimes * No kernel processing for the following; 577df8bae1dSRodney W. Grimes * just fall through to send to raw listener. 578df8bae1dSRodney W. Grimes */ 579df8bae1dSRodney W. Grimes case ICMP_ECHOREPLY: 580df8bae1dSRodney W. Grimes case ICMP_ROUTERADVERT: 581df8bae1dSRodney W. Grimes case ICMP_ROUTERSOLICIT: 582df8bae1dSRodney W. Grimes case ICMP_TSTAMPREPLY: 583df8bae1dSRodney W. Grimes case ICMP_IREQREPLY: 584df8bae1dSRodney W. Grimes case ICMP_MASKREPLY: 585df8bae1dSRodney W. Grimes default: 586df8bae1dSRodney W. Grimes break; 587df8bae1dSRodney W. Grimes } 588df8bae1dSRodney W. Grimes 589df8bae1dSRodney W. Grimes raw: 590f0ffb944SJulian Elischer rip_input(m, off); 591df8bae1dSRodney W. Grimes return; 592df8bae1dSRodney W. Grimes 593df8bae1dSRodney W. Grimes freeit: 594df8bae1dSRodney W. Grimes m_freem(m); 595df8bae1dSRodney W. Grimes } 596df8bae1dSRodney W. Grimes 597df8bae1dSRodney W. Grimes /* 598df8bae1dSRodney W. Grimes * Reflect the ip packet back to the source 599df8bae1dSRodney W. Grimes */ 6000312fbe9SPoul-Henning Kamp static void 601df8bae1dSRodney W. Grimes icmp_reflect(m) 602df8bae1dSRodney W. Grimes struct mbuf *m; 603df8bae1dSRodney W. Grimes { 604ca925d9cSJonathan Lemon struct ip *ip = mtod(m, struct ip *); 605ca925d9cSJonathan Lemon struct ifaddr *ifa; 606ca925d9cSJonathan Lemon struct in_ifaddr *ia; 607df8bae1dSRodney W. Grimes struct in_addr t; 608b5e8ce9fSBruce Evans struct mbuf *opts = 0; 60953be11f6SPoul-Henning Kamp int optlen = (ip->ip_hl << 2) - sizeof(struct ip); 610bd714208SRuslan Ermilov struct route *ro = NULL, rt; 611df8bae1dSRodney W. Grimes 612df8bae1dSRodney W. Grimes if (!in_canforward(ip->ip_src) && 613df8bae1dSRodney W. Grimes ((ntohl(ip->ip_src.s_addr) & IN_CLASSA_NET) != 614df8bae1dSRodney W. Grimes (IN_LOOPBACKNET << IN_CLASSA_NSHIFT))) { 615df8bae1dSRodney W. Grimes m_freem(m); /* Bad return address */ 616bd714208SRuslan Ermilov icmpstat.icps_badaddr++; 617df8bae1dSRodney W. Grimes goto done; /* Ip_output() will check for broadcast */ 618df8bae1dSRodney W. Grimes } 619df8bae1dSRodney W. Grimes t = ip->ip_dst; 620df8bae1dSRodney W. Grimes ip->ip_dst = ip->ip_src; 621e3f406b3SRuslan Ermilov ro = &rt; 622e3f406b3SRuslan Ermilov bzero(ro, sizeof(*ro)); 623df8bae1dSRodney W. Grimes /* 624df8bae1dSRodney W. Grimes * If the incoming packet was addressed directly to us, 625df8bae1dSRodney W. Grimes * use dst as the src for the reply. Otherwise (broadcast 626df8bae1dSRodney W. Grimes * or anonymous), use the address which corresponds 627df8bae1dSRodney W. Grimes * to the incoming interface. 628df8bae1dSRodney W. Grimes */ 629ca925d9cSJonathan Lemon LIST_FOREACH(ia, INADDR_HASH(t.s_addr), ia_hash) 630df8bae1dSRodney W. Grimes if (t.s_addr == IA_SIN(ia)->sin_addr.s_addr) 631ca925d9cSJonathan Lemon goto match; 63284caf008SRuslan Ermilov if (m->m_pkthdr.rcvif != NULL && 63384caf008SRuslan Ermilov m->m_pkthdr.rcvif->if_flags & IFF_BROADCAST) { 634ca925d9cSJonathan Lemon TAILQ_FOREACH(ifa, &m->m_pkthdr.rcvif->if_addrhead, ifa_link) { 635ca925d9cSJonathan Lemon if (ifa->ifa_addr->sa_family != AF_INET) 636ca925d9cSJonathan Lemon continue; 637ca925d9cSJonathan Lemon ia = ifatoia(ifa); 638ca925d9cSJonathan Lemon if (satosin(&ia->ia_broadaddr)->sin_addr.s_addr == 639ca925d9cSJonathan Lemon t.s_addr) 640ca925d9cSJonathan Lemon goto match; 641df8bae1dSRodney W. Grimes } 642ca925d9cSJonathan Lemon } 643bd714208SRuslan Ermilov ia = ip_rtaddr(ip->ip_dst, ro); 644bd714208SRuslan Ermilov /* We need a route to do anything useful. */ 6450d4bef5dSDima Dorfman if (ia == NULL) { 6460d4bef5dSDima Dorfman m_freem(m); 647bd714208SRuslan Ermilov icmpstat.icps_noroute++; 6480d4bef5dSDima Dorfman goto done; 6490d4bef5dSDima Dorfman } 650ca925d9cSJonathan Lemon match: 651baee0c3eSRobert Watson #ifdef MAC 652baee0c3eSRobert Watson mac_reflect_mbuf_icmp(m); 653baee0c3eSRobert Watson #endif 654df8bae1dSRodney W. Grimes t = IA_SIN(ia)->sin_addr; 655df8bae1dSRodney W. Grimes ip->ip_src = t; 6568ce3f3ddSRuslan Ermilov ip->ip_ttl = ip_defttl; 657df8bae1dSRodney W. Grimes 658df8bae1dSRodney W. Grimes if (optlen > 0) { 659df8bae1dSRodney W. Grimes register u_char *cp; 660df8bae1dSRodney W. Grimes int opt, cnt; 661df8bae1dSRodney W. Grimes u_int len; 662df8bae1dSRodney W. Grimes 663df8bae1dSRodney W. Grimes /* 664df8bae1dSRodney W. Grimes * Retrieve any source routing from the incoming packet; 665df8bae1dSRodney W. Grimes * add on any record-route or timestamp options. 666df8bae1dSRodney W. Grimes */ 667df8bae1dSRodney W. Grimes cp = (u_char *) (ip + 1); 668df8bae1dSRodney W. Grimes if ((opts = ip_srcroute()) == 0 && 669a163d034SWarner Losh (opts = m_gethdr(M_DONTWAIT, MT_HEADER))) { 670df8bae1dSRodney W. Grimes opts->m_len = sizeof(struct in_addr); 671df8bae1dSRodney W. Grimes mtod(opts, struct in_addr *)->s_addr = 0; 672df8bae1dSRodney W. Grimes } 673df8bae1dSRodney W. Grimes if (opts) { 674df8bae1dSRodney W. Grimes #ifdef ICMPPRINTFS 675df8bae1dSRodney W. Grimes if (icmpprintfs) 676df8bae1dSRodney W. Grimes printf("icmp_reflect optlen %d rt %d => ", 677df8bae1dSRodney W. Grimes optlen, opts->m_len); 678df8bae1dSRodney W. Grimes #endif 679df8bae1dSRodney W. Grimes for (cnt = optlen; cnt > 0; cnt -= len, cp += len) { 680df8bae1dSRodney W. Grimes opt = cp[IPOPT_OPTVAL]; 681df8bae1dSRodney W. Grimes if (opt == IPOPT_EOL) 682df8bae1dSRodney W. Grimes break; 683df8bae1dSRodney W. Grimes if (opt == IPOPT_NOP) 684df8bae1dSRodney W. Grimes len = 1; 685df8bae1dSRodney W. Grimes else { 686707d00a3SJonathan Lemon if (cnt < IPOPT_OLEN + sizeof(*cp)) 687707d00a3SJonathan Lemon break; 688df8bae1dSRodney W. Grimes len = cp[IPOPT_OLEN]; 689707d00a3SJonathan Lemon if (len < IPOPT_OLEN + sizeof(*cp) || 690707d00a3SJonathan Lemon len > cnt) 691df8bae1dSRodney W. Grimes break; 692df8bae1dSRodney W. Grimes } 693df8bae1dSRodney W. Grimes /* 694df8bae1dSRodney W. Grimes * Should check for overflow, but it "can't happen" 695df8bae1dSRodney W. Grimes */ 696df8bae1dSRodney W. Grimes if (opt == IPOPT_RR || opt == IPOPT_TS || 697df8bae1dSRodney W. Grimes opt == IPOPT_SECURITY) { 698df8bae1dSRodney W. Grimes bcopy((caddr_t)cp, 699df8bae1dSRodney W. Grimes mtod(opts, caddr_t) + opts->m_len, len); 700df8bae1dSRodney W. Grimes opts->m_len += len; 701df8bae1dSRodney W. Grimes } 702df8bae1dSRodney W. Grimes } 703df8bae1dSRodney W. Grimes /* Terminate & pad, if necessary */ 704623ae52eSPoul-Henning Kamp cnt = opts->m_len % 4; 705623ae52eSPoul-Henning Kamp if (cnt) { 706df8bae1dSRodney W. Grimes for (; cnt < 4; cnt++) { 707df8bae1dSRodney W. Grimes *(mtod(opts, caddr_t) + opts->m_len) = 708df8bae1dSRodney W. Grimes IPOPT_EOL; 709df8bae1dSRodney W. Grimes opts->m_len++; 710df8bae1dSRodney W. Grimes } 711df8bae1dSRodney W. Grimes } 712df8bae1dSRodney W. Grimes #ifdef ICMPPRINTFS 713df8bae1dSRodney W. Grimes if (icmpprintfs) 714df8bae1dSRodney W. Grimes printf("%d\n", opts->m_len); 715df8bae1dSRodney W. Grimes #endif 716df8bae1dSRodney W. Grimes } 717df8bae1dSRodney W. Grimes /* 718df8bae1dSRodney W. Grimes * Now strip out original options by copying rest of first 719df8bae1dSRodney W. Grimes * mbuf's data back, and adjust the IP length. 720df8bae1dSRodney W. Grimes */ 721df8bae1dSRodney W. Grimes ip->ip_len -= optlen; 72253be11f6SPoul-Henning Kamp ip->ip_v = IPVERSION; 72353be11f6SPoul-Henning Kamp ip->ip_hl = 5; 724df8bae1dSRodney W. Grimes m->m_len -= optlen; 725df8bae1dSRodney W. Grimes if (m->m_flags & M_PKTHDR) 726df8bae1dSRodney W. Grimes m->m_pkthdr.len -= optlen; 727df8bae1dSRodney W. Grimes optlen += sizeof(struct ip); 728df8bae1dSRodney W. Grimes bcopy((caddr_t)ip + optlen, (caddr_t)(ip + 1), 729df8bae1dSRodney W. Grimes (unsigned)(m->m_len - sizeof(struct ip))); 730df8bae1dSRodney W. Grimes } 731df8bae1dSRodney W. Grimes m->m_flags &= ~(M_BCAST|M_MCAST); 732bd714208SRuslan Ermilov icmp_send(m, opts, ro); 733df8bae1dSRodney W. Grimes done: 734df8bae1dSRodney W. Grimes if (opts) 735df8bae1dSRodney W. Grimes (void)m_free(opts); 736bd714208SRuslan Ermilov if (ro && ro->ro_rt) 737bd714208SRuslan Ermilov RTFREE(ro->ro_rt); 738df8bae1dSRodney W. Grimes } 739df8bae1dSRodney W. Grimes 740df8bae1dSRodney W. Grimes /* 741df8bae1dSRodney W. Grimes * Send an icmp packet back to the ip level, 742df8bae1dSRodney W. Grimes * after supplying a checksum. 743df8bae1dSRodney W. Grimes */ 7440312fbe9SPoul-Henning Kamp static void 745bd714208SRuslan Ermilov icmp_send(m, opts, rt) 746df8bae1dSRodney W. Grimes register struct mbuf *m; 747df8bae1dSRodney W. Grimes struct mbuf *opts; 748bd714208SRuslan Ermilov struct route *rt; 749df8bae1dSRodney W. Grimes { 750df8bae1dSRodney W. Grimes register struct ip *ip = mtod(m, struct ip *); 751df8bae1dSRodney W. Grimes register int hlen; 752df8bae1dSRodney W. Grimes register struct icmp *icp; 753df8bae1dSRodney W. Grimes 75453be11f6SPoul-Henning Kamp hlen = ip->ip_hl << 2; 755df8bae1dSRodney W. Grimes m->m_data += hlen; 756df8bae1dSRodney W. Grimes m->m_len -= hlen; 757df8bae1dSRodney W. Grimes icp = mtod(m, struct icmp *); 758df8bae1dSRodney W. Grimes icp->icmp_cksum = 0; 759df8bae1dSRodney W. Grimes icp->icmp_cksum = in_cksum(m, ip->ip_len - hlen); 760df8bae1dSRodney W. Grimes m->m_data -= hlen; 761df8bae1dSRodney W. Grimes m->m_len += hlen; 76294446a2eSArchie Cobbs m->m_pkthdr.rcvif = (struct ifnet *)0; 763df8bae1dSRodney W. Grimes #ifdef ICMPPRINTFS 7642b758395SGarrett Wollman if (icmpprintfs) { 7652b758395SGarrett Wollman char buf[4 * sizeof "123"]; 7662b758395SGarrett Wollman strcpy(buf, inet_ntoa(ip->ip_dst)); 7672b758395SGarrett Wollman printf("icmp_send dst %s src %s\n", 7682b758395SGarrett Wollman buf, inet_ntoa(ip->ip_src)); 7692b758395SGarrett Wollman } 770df8bae1dSRodney W. Grimes #endif 7715d846453SSam Leffler (void) ip_output(m, opts, rt, 0, NULL, NULL); 772df8bae1dSRodney W. Grimes } 773df8bae1dSRodney W. Grimes 774df8bae1dSRodney W. Grimes n_time 775df8bae1dSRodney W. Grimes iptime() 776df8bae1dSRodney W. Grimes { 777df8bae1dSRodney W. Grimes struct timeval atv; 778df8bae1dSRodney W. Grimes u_long t; 779df8bae1dSRodney W. Grimes 78016cd6db0SBill Fumerola getmicrotime(&atv); 781df8bae1dSRodney W. Grimes t = (atv.tv_sec % (24*60*60)) * 1000 + atv.tv_usec / 1000; 782df8bae1dSRodney W. Grimes return (htonl(t)); 783df8bae1dSRodney W. Grimes } 784df8bae1dSRodney W. Grimes 785b7a44e34SGarrett Wollman #if 1 7865cbf3e08SGarrett Wollman /* 7875cbf3e08SGarrett Wollman * Return the next larger or smaller MTU plateau (table from RFC 1191) 7885cbf3e08SGarrett Wollman * given current value MTU. If DIR is less than zero, a larger plateau 7895cbf3e08SGarrett Wollman * is returned; otherwise, a smaller value is returned. 7905cbf3e08SGarrett Wollman */ 791f708ef1bSPoul-Henning Kamp static int 7925cbf3e08SGarrett Wollman ip_next_mtu(mtu, dir) 7935cbf3e08SGarrett Wollman int mtu; 7945cbf3e08SGarrett Wollman int dir; 7955cbf3e08SGarrett Wollman { 7965cbf3e08SGarrett Wollman static int mtutab[] = { 7975cbf3e08SGarrett Wollman 65535, 32000, 17914, 8166, 4352, 2002, 1492, 1006, 508, 296, 7985cbf3e08SGarrett Wollman 68, 0 7995cbf3e08SGarrett Wollman }; 8005cbf3e08SGarrett Wollman int i; 8015cbf3e08SGarrett Wollman 8025cbf3e08SGarrett Wollman for (i = 0; i < (sizeof mtutab) / (sizeof mtutab[0]); i++) { 8035cbf3e08SGarrett Wollman if (mtu >= mtutab[i]) 8045cbf3e08SGarrett Wollman break; 8055cbf3e08SGarrett Wollman } 8065cbf3e08SGarrett Wollman 8075cbf3e08SGarrett Wollman if (dir < 0) { 8085cbf3e08SGarrett Wollman if (i == 0) { 8095cbf3e08SGarrett Wollman return 0; 8105cbf3e08SGarrett Wollman } else { 8115cbf3e08SGarrett Wollman return mtutab[i - 1]; 8125cbf3e08SGarrett Wollman } 8135cbf3e08SGarrett Wollman } else { 8145cbf3e08SGarrett Wollman if (mtutab[i] == 0) { 8155cbf3e08SGarrett Wollman return 0; 8165cbf3e08SGarrett Wollman } else if(mtu > mtutab[i]) { 8175cbf3e08SGarrett Wollman return mtutab[i]; 8185cbf3e08SGarrett Wollman } else { 8195cbf3e08SGarrett Wollman return mtutab[i + 1]; 8205cbf3e08SGarrett Wollman } 8215cbf3e08SGarrett Wollman } 8225cbf3e08SGarrett Wollman } 823b7a44e34SGarrett Wollman #endif 82451508de1SMatthew Dillon 82551508de1SMatthew Dillon 82651508de1SMatthew Dillon /* 82751508de1SMatthew Dillon * badport_bandlim() - check for ICMP bandwidth limit 82851508de1SMatthew Dillon * 82951508de1SMatthew Dillon * Return 0 if it is ok to send an ICMP error response, -1 if we have 83051508de1SMatthew Dillon * hit our bandwidth limit and it is not ok. 83151508de1SMatthew Dillon * 83251508de1SMatthew Dillon * If icmplim is <= 0, the feature is disabled and 0 is returned. 83351508de1SMatthew Dillon * 83451508de1SMatthew Dillon * For now we separate the TCP and UDP subsystems w/ different 'which' 83551508de1SMatthew Dillon * values. We may eventually remove this separation (and simplify the 83651508de1SMatthew Dillon * code further). 83751508de1SMatthew Dillon * 83851508de1SMatthew Dillon * Note that the printing of the error message is delayed so we can 83951508de1SMatthew Dillon * properly print the icmp error rate that the system was trying to do 84051508de1SMatthew Dillon * (i.e. 22000/100 pps, etc...). This can cause long delays in printing 84151508de1SMatthew Dillon * the 'final' error, but it doesn't make sense to solve the printing 84251508de1SMatthew Dillon * delay with more complex code. 84351508de1SMatthew Dillon */ 84451508de1SMatthew Dillon 84551508de1SMatthew Dillon int 84651508de1SMatthew Dillon badport_bandlim(int which) 84751508de1SMatthew Dillon { 84800f21882SSam Leffler #define N(a) (sizeof (a) / sizeof (a[0])) 84900f21882SSam Leffler static struct rate { 85000f21882SSam Leffler const char *type; 85100f21882SSam Leffler struct timeval lasttime; 85200f21882SSam Leffler int curpps;; 85300f21882SSam Leffler } rates[BANDLIM_MAX+1] = { 85400f21882SSam Leffler { "icmp unreach response" }, 85500f21882SSam Leffler { "icmp ping response" }, 85600f21882SSam Leffler { "icmp tstamp response" }, 85700f21882SSam Leffler { "closed port RST response" }, 85800f21882SSam Leffler { "open port RST response" } 85909f81a46SBosko Milekic }; 86051508de1SMatthew Dillon 86151508de1SMatthew Dillon /* 86200f21882SSam Leffler * Return ok status if feature disabled or argument out of range. 86351508de1SMatthew Dillon */ 86400f21882SSam Leffler if (icmplim > 0 && (u_int) which < N(rates)) { 86500f21882SSam Leffler struct rate *r = &rates[which]; 86600f21882SSam Leffler int opps = r->curpps; 86751508de1SMatthew Dillon 86800f21882SSam Leffler if (!ppsratecheck(&r->lasttime, &r->curpps, icmplim)) 86900f21882SSam Leffler return -1; /* discard packet */ 87051508de1SMatthew Dillon /* 87100f21882SSam Leffler * If we've dropped below the threshold after having 87200f21882SSam Leffler * rate-limited traffic print the message. This preserves 87300f21882SSam Leffler * the previous behaviour at the expense of added complexity. 87451508de1SMatthew Dillon */ 875069f35d3SSam Leffler if (icmplim_output && opps > icmplim) 87600f21882SSam Leffler printf("Limiting %s from %d to %d packets/sec\n", 877069f35d3SSam Leffler r->type, opps, icmplim); 87851508de1SMatthew Dillon } 87900f21882SSam Leffler return 0; /* okay to send packet */ 88000f21882SSam Leffler #undef N 88151508de1SMatthew Dillon } 882