1c398230bSWarner Losh /*- 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 * 4. Neither the name of the University nor the names of its contributors 14df8bae1dSRodney W. Grimes * may be used to endorse or promote products derived from this software 15df8bae1dSRodney W. Grimes * without specific prior written permission. 16df8bae1dSRodney W. Grimes * 17df8bae1dSRodney W. Grimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 18df8bae1dSRodney W. Grimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 19df8bae1dSRodney W. Grimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 20df8bae1dSRodney W. Grimes * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 21df8bae1dSRodney W. Grimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 22df8bae1dSRodney W. Grimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 23df8bae1dSRodney W. Grimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 24df8bae1dSRodney W. Grimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 25df8bae1dSRodney W. Grimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 26df8bae1dSRodney W. Grimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 27df8bae1dSRodney W. Grimes * SUCH DAMAGE. 28df8bae1dSRodney W. Grimes * 29df8bae1dSRodney W. Grimes * @(#)ip_icmp.c 8.2 (Berkeley) 1/4/94 30df8bae1dSRodney W. Grimes */ 31df8bae1dSRodney W. Grimes 324b421e2dSMike Silbersack #include <sys/cdefs.h> 334b421e2dSMike Silbersack __FBSDID("$FreeBSD$"); 344b421e2dSMike Silbersack 356a800098SYoshinobu Inoue #include "opt_ipsec.h" 360070e096SRobert Watson #include "opt_mac.h" 376a800098SYoshinobu Inoue 38df8bae1dSRodney W. Grimes #include <sys/param.h> 39df8bae1dSRodney W. Grimes #include <sys/systm.h> 40df8bae1dSRodney W. Grimes #include <sys/mbuf.h> 41df8bae1dSRodney W. Grimes #include <sys/protosw.h> 42df8bae1dSRodney W. Grimes #include <sys/socket.h> 43df8bae1dSRodney W. Grimes #include <sys/time.h> 44df8bae1dSRodney W. Grimes #include <sys/kernel.h> 45b5e8ce9fSBruce Evans #include <sys/sysctl.h> 46df8bae1dSRodney W. Grimes 47df8bae1dSRodney W. Grimes #include <net/if.h> 489494d596SBrooks Davis #include <net/if_types.h> 49df8bae1dSRodney W. Grimes #include <net/route.h> 50df8bae1dSRodney W. Grimes 51df8bae1dSRodney W. Grimes #include <netinet/in.h> 5297d8d152SAndre Oppermann #include <netinet/in_pcb.h> 53df8bae1dSRodney W. Grimes #include <netinet/in_systm.h> 54df8bae1dSRodney W. Grimes #include <netinet/in_var.h> 55df8bae1dSRodney W. Grimes #include <netinet/ip.h> 56df8bae1dSRodney W. Grimes #include <netinet/ip_icmp.h> 57b5e8ce9fSBruce Evans #include <netinet/ip_var.h> 58ef39adf0SAndre Oppermann #include <netinet/ip_options.h> 5997d8d152SAndre Oppermann #include <netinet/tcp.h> 6097d8d152SAndre Oppermann #include <netinet/tcp_var.h> 6197d8d152SAndre Oppermann #include <netinet/tcpip.h> 62df8bae1dSRodney W. Grimes #include <netinet/icmp_var.h> 63df8bae1dSRodney W. Grimes 64b2630c29SGeorge V. Neville-Neil #ifdef IPSEC 65b9234fafSSam Leffler #include <netipsec/ipsec.h> 66b9234fafSSam Leffler #include <netipsec/key.h> 67b9234fafSSam Leffler #endif 68b9234fafSSam Leffler 6972a52a35SJonathan Lemon #include <machine/in_cksum.h> 7072a52a35SJonathan Lemon 71aed55708SRobert Watson #include <security/mac/mac_framework.h> 72aed55708SRobert Watson 73df8bae1dSRodney W. Grimes /* 74df8bae1dSRodney W. Grimes * ICMP routines: error generation, receive packet processing, and 75df8bae1dSRodney W. Grimes * routines to turnaround packets back to the originator, and 76df8bae1dSRodney W. Grimes * host table maintenance routines. 77df8bae1dSRodney W. Grimes */ 78df8bae1dSRodney W. Grimes 79cc5934f5SMax Laier struct icmpstat icmpstat; 80c73d99b5SRuslan Ermilov SYSCTL_STRUCT(_net_inet_icmp, ICMPCTL_STATS, stats, CTLFLAG_RW, 810312fbe9SPoul-Henning Kamp &icmpstat, icmpstat, ""); 820312fbe9SPoul-Henning Kamp 830312fbe9SPoul-Henning Kamp static int icmpmaskrepl = 0; 840312fbe9SPoul-Henning Kamp SYSCTL_INT(_net_inet_icmp, ICMPCTL_MASKREPL, maskrepl, CTLFLAG_RW, 8557842a38SMatthew N. Dodd &icmpmaskrepl, 0, "Reply to ICMP Address Mask Request packets."); 8657842a38SMatthew N. Dodd 8757842a38SMatthew N. Dodd static u_int icmpmaskfake = 0; 8857842a38SMatthew N. Dodd SYSCTL_UINT(_net_inet_icmp, OID_AUTO, maskfake, CTLFLAG_RW, 8957842a38SMatthew N. Dodd &icmpmaskfake, 0, "Fake reply to ICMP Address Mask Request packets."); 900312fbe9SPoul-Henning Kamp 9118d3153eSDag-Erling Smørgrav static int drop_redirect = 0; 9218d3153eSDag-Erling Smørgrav SYSCTL_INT(_net_inet_icmp, OID_AUTO, drop_redirect, CTLFLAG_RW, 93f92e18f4SGiorgos Keramidas &drop_redirect, 0, "Ignore ICMP redirects"); 9418d3153eSDag-Erling Smørgrav 956c3b5f69SDag-Erling Smørgrav static int log_redirect = 0; 966c3b5f69SDag-Erling Smørgrav SYSCTL_INT(_net_inet_icmp, OID_AUTO, log_redirect, CTLFLAG_RW, 97f92e18f4SGiorgos Keramidas &log_redirect, 0, "Log ICMP redirects to the console"); 986c3b5f69SDag-Erling Smørgrav 99173c0f9fSWarner Losh static int icmplim = 200; 10051508de1SMatthew Dillon SYSCTL_INT(_net_inet_icmp, ICMPCTL_ICMPLIM, icmplim, CTLFLAG_RW, 101f92e18f4SGiorgos Keramidas &icmplim, 0, "Maximum number of ICMP responses per second"); 1025fce7fc4SMatthew Dillon 1034f14ee00SDan Moschuk static int icmplim_output = 1; 1044f14ee00SDan Moschuk SYSCTL_INT(_net_inet_icmp, OID_AUTO, icmplim_output, CTLFLAG_RW, 105f92e18f4SGiorgos Keramidas &icmplim_output, 0, "Enable rate limiting of ICMP responses"); 10651508de1SMatthew Dillon 107d0946241SMaxim Konovalov static char reply_src[IFNAMSIZ]; 108b74d89bbSAndre Oppermann SYSCTL_STRING(_net_inet_icmp, OID_AUTO, reply_src, CTLFLAG_RW, 109b74d89bbSAndre Oppermann &reply_src, IFNAMSIZ, "icmp reply source for non-local packets."); 110b74d89bbSAndre Oppermann 111a0866c8dSAndre Oppermann static int icmp_rfi = 0; 112a0866c8dSAndre Oppermann SYSCTL_INT(_net_inet_icmp, OID_AUTO, reply_from_interface, CTLFLAG_RW, 113a0866c8dSAndre Oppermann &icmp_rfi, 0, "ICMP reply from incoming interface for " 114a0866c8dSAndre Oppermann "non-local packets"); 115a0866c8dSAndre Oppermann 116e875dfb8SAndre Oppermann static int icmp_quotelen = 8; 117e875dfb8SAndre Oppermann SYSCTL_INT(_net_inet_icmp, OID_AUTO, quotelen, CTLFLAG_RW, 118e875dfb8SAndre Oppermann &icmp_quotelen, 0, "Number of bytes from original packet to " 119e875dfb8SAndre Oppermann "quote in ICMP reply"); 120e875dfb8SAndre Oppermann 1215fce7fc4SMatthew Dillon /* 1225fce7fc4SMatthew Dillon * ICMP broadcast echo sysctl 1235fce7fc4SMatthew Dillon */ 1245fce7fc4SMatthew Dillon 12561a4defdSJoseph Koshy static int icmpbmcastecho = 0; 12618d3153eSDag-Erling Smørgrav SYSCTL_INT(_net_inet_icmp, OID_AUTO, bmcastecho, CTLFLAG_RW, 12718d3153eSDag-Erling Smørgrav &icmpbmcastecho, 0, ""); 1287022ea0aSGarrett Wollman 12951508de1SMatthew Dillon 130df8bae1dSRodney W. Grimes #ifdef ICMPPRINTFS 131df8bae1dSRodney W. Grimes int icmpprintfs = 0; 132df8bae1dSRodney W. Grimes #endif 133df8bae1dSRodney W. Grimes 1344d77a549SAlfred Perlstein static void icmp_reflect(struct mbuf *); 13502c1c707SAndre Oppermann static void icmp_send(struct mbuf *, struct mbuf *); 1360312fbe9SPoul-Henning Kamp 137df8bae1dSRodney W. Grimes extern struct protosw inetsw[]; 138df8bae1dSRodney W. Grimes 139df8bae1dSRodney W. Grimes /* 140df8bae1dSRodney W. Grimes * Generate an error packet of type error 141df8bae1dSRodney W. Grimes * in response to bad packet ip. 142df8bae1dSRodney W. Grimes */ 143df8bae1dSRodney W. Grimes void 144f2565d68SRobert Watson icmp_error(struct mbuf *n, int type, int code, n_long dest, int mtu) 145df8bae1dSRodney W. Grimes { 146df8bae1dSRodney W. Grimes register struct ip *oip = mtod(n, struct ip *), *nip; 147e86ebebcSAndre Oppermann register unsigned oiphlen = oip->ip_hl << 2; 148df8bae1dSRodney W. Grimes register struct icmp *icp; 149df8bae1dSRodney W. Grimes register struct mbuf *m; 150e86ebebcSAndre Oppermann unsigned icmplen, icmpelen, nlen; 151df8bae1dSRodney W. Grimes 152e86ebebcSAndre Oppermann KASSERT((u_int)type <= ICMP_MAXTYPE, ("%s: illegal ICMP type", __func__)); 153df8bae1dSRodney W. Grimes #ifdef ICMPPRINTFS 154df8bae1dSRodney W. Grimes if (icmpprintfs) 155623ae52eSPoul-Henning Kamp printf("icmp_error(%p, %x, %d)\n", oip, type, code); 156df8bae1dSRodney W. Grimes #endif 157df8bae1dSRodney W. Grimes if (type != ICMP_REDIRECT) 158df8bae1dSRodney W. Grimes icmpstat.icps_error++; 159df8bae1dSRodney W. Grimes /* 160e86ebebcSAndre Oppermann * Don't send error: 161e86ebebcSAndre Oppermann * if the original packet was encrypted. 162e86ebebcSAndre Oppermann * if not the first fragment of message. 163e86ebebcSAndre Oppermann * in response to a multicast or broadcast packet. 164e86ebebcSAndre Oppermann * if the old packet protocol was an ICMP error message. 165df8bae1dSRodney W. Grimes */ 166cad1917dSHajimu UMEMOTO if (n->m_flags & M_DECRYPTED) 167cad1917dSHajimu UMEMOTO goto freeit; 168df8bae1dSRodney W. Grimes if (oip->ip_off & ~(IP_MF|IP_DF)) 169df8bae1dSRodney W. Grimes goto freeit; 170e86ebebcSAndre Oppermann if (n->m_flags & (M_BCAST|M_MCAST)) 171e86ebebcSAndre Oppermann goto freeit; 172df8bae1dSRodney W. Grimes if (oip->ip_p == IPPROTO_ICMP && type != ICMP_REDIRECT && 173e86ebebcSAndre Oppermann n->m_len >= oiphlen + ICMP_MINLEN && 174e86ebebcSAndre Oppermann !ICMP_INFOTYPE(((struct icmp *)((caddr_t)oip + oiphlen))->icmp_type)) { 175df8bae1dSRodney W. Grimes icmpstat.icps_oldicmp++; 176df8bae1dSRodney W. Grimes goto freeit; 177df8bae1dSRodney W. Grimes } 178e86ebebcSAndre Oppermann /* Drop if IP header plus 8 bytes is not contignous in first mbuf. */ 179e86ebebcSAndre Oppermann if (oiphlen + 8 > n->m_len) 180df8bae1dSRodney W. Grimes goto freeit; 181df8bae1dSRodney W. Grimes /* 182e86ebebcSAndre Oppermann * Calculate length to quote from original packet and 183e86ebebcSAndre Oppermann * prevent the ICMP mbuf from overflowing. 184e86ebebcSAndre Oppermann * Unfortunatly this is non-trivial since ip_forward() 185e86ebebcSAndre Oppermann * sends us truncated packets. 186df8bae1dSRodney W. Grimes */ 187e86ebebcSAndre Oppermann nlen = m_length(n, NULL); 188e86ebebcSAndre Oppermann if (oip->ip_p == IPPROTO_TCP) { 189e86ebebcSAndre Oppermann struct tcphdr *th; 190e86ebebcSAndre Oppermann int tcphlen; 191e86ebebcSAndre Oppermann 192e86ebebcSAndre Oppermann if (oiphlen + sizeof(struct tcphdr) > n->m_len && 193e86ebebcSAndre Oppermann n->m_next == NULL) 194e86ebebcSAndre Oppermann goto stdreply; 195e86ebebcSAndre Oppermann if (n->m_len < oiphlen + sizeof(struct tcphdr) && 196e86ebebcSAndre Oppermann ((n = m_pullup(n, oiphlen + sizeof(struct tcphdr))) == NULL)) 197e86ebebcSAndre Oppermann goto freeit; 198e86ebebcSAndre Oppermann th = (struct tcphdr *)((caddr_t)oip + oiphlen); 199e86ebebcSAndre Oppermann tcphlen = th->th_off << 2; 200e86ebebcSAndre Oppermann if (tcphlen < sizeof(struct tcphdr)) 201e86ebebcSAndre Oppermann goto freeit; 202e86ebebcSAndre Oppermann if (oip->ip_len < oiphlen + tcphlen) 203e86ebebcSAndre Oppermann goto freeit; 204e86ebebcSAndre Oppermann if (oiphlen + tcphlen > n->m_len && n->m_next == NULL) 205e86ebebcSAndre Oppermann goto stdreply; 206e86ebebcSAndre Oppermann if (n->m_len < oiphlen + tcphlen && 207e86ebebcSAndre Oppermann ((n = m_pullup(n, oiphlen + tcphlen)) == NULL)) 208e86ebebcSAndre Oppermann goto freeit; 209e86ebebcSAndre Oppermann icmpelen = max(tcphlen, min(icmp_quotelen, oip->ip_len - oiphlen)); 210e86ebebcSAndre Oppermann } else 211e86ebebcSAndre Oppermann stdreply: icmpelen = max(8, min(icmp_quotelen, oip->ip_len - oiphlen)); 212e86ebebcSAndre Oppermann 213e86ebebcSAndre Oppermann icmplen = min(oiphlen + icmpelen, nlen); 214e86ebebcSAndre Oppermann if (icmplen < sizeof(struct ip)) 215e86ebebcSAndre Oppermann goto freeit; 216e86ebebcSAndre Oppermann 217e86ebebcSAndre Oppermann if (MHLEN > sizeof(struct ip) + ICMP_MINLEN + icmplen) 218e86ebebcSAndre Oppermann m = m_gethdr(M_DONTWAIT, MT_DATA); 219e86ebebcSAndre Oppermann else 220e86ebebcSAndre Oppermann m = m_getcl(M_DONTWAIT, MT_DATA, M_PKTHDR); 221df8bae1dSRodney W. Grimes if (m == NULL) 222df8bae1dSRodney W. Grimes goto freeit; 2230070e096SRobert Watson #ifdef MAC 224a13e21f7SRobert Watson mac_netinet_icmp_reply(n, m); 2250070e096SRobert Watson #endif 226e86ebebcSAndre Oppermann icmplen = min(icmplen, M_TRAILINGSPACE(m) - sizeof(struct ip) - ICMP_MINLEN); 227e86ebebcSAndre Oppermann m_align(m, ICMP_MINLEN + icmplen); 228e86ebebcSAndre Oppermann m->m_len = ICMP_MINLEN + icmplen; 2296b773dffSAndre Oppermann 2308b07e49aSJulian Elischer /* XXX MRT make the outgoing packet use the same FIB 2318b07e49aSJulian Elischer * that was associated with the incoming packet 2328b07e49aSJulian Elischer */ 2338b07e49aSJulian Elischer M_SETFIB(m, M_GETFIB(n)); 234df8bae1dSRodney W. Grimes icp = mtod(m, struct icmp *); 235df8bae1dSRodney W. Grimes icmpstat.icps_outhist[type]++; 236df8bae1dSRodney W. Grimes icp->icmp_type = type; 237df8bae1dSRodney W. Grimes if (type == ICMP_REDIRECT) 238df8bae1dSRodney W. Grimes icp->icmp_gwaddr.s_addr = dest; 239df8bae1dSRodney W. Grimes else { 240df8bae1dSRodney W. Grimes icp->icmp_void = 0; 241df8bae1dSRodney W. Grimes /* 242df8bae1dSRodney W. Grimes * The following assignments assume an overlay with the 243e86ebebcSAndre Oppermann * just zeroed icmp_void field. 244df8bae1dSRodney W. Grimes */ 245df8bae1dSRodney W. Grimes if (type == ICMP_PARAMPROB) { 246df8bae1dSRodney W. Grimes icp->icmp_pptr = code; 247df8bae1dSRodney W. Grimes code = 0; 248df8bae1dSRodney W. Grimes } else if (type == ICMP_UNREACH && 249c773494eSAndre Oppermann code == ICMP_UNREACH_NEEDFRAG && mtu) { 250c773494eSAndre Oppermann icp->icmp_nextmtu = htons(mtu); 251df8bae1dSRodney W. Grimes } 252df8bae1dSRodney W. Grimes } 253df8bae1dSRodney W. Grimes icp->icmp_code = code; 25404287599SRuslan Ermilov 25504287599SRuslan Ermilov /* 256e86ebebcSAndre Oppermann * Copy the quotation into ICMP message and 257e86ebebcSAndre Oppermann * convert quoted IP header back to network representation. 25804287599SRuslan Ermilov */ 259e86ebebcSAndre Oppermann m_copydata(n, 0, icmplen, (caddr_t)&icp->icmp_ip); 260e86ebebcSAndre Oppermann nip = &icp->icmp_ip; 261fd8e4ebcSMike Barcroft nip->ip_len = htons(nip->ip_len); 262fd8e4ebcSMike Barcroft nip->ip_off = htons(nip->ip_off); 263df8bae1dSRodney W. Grimes 264df8bae1dSRodney W. Grimes /* 265e86ebebcSAndre Oppermann * Set up ICMP message mbuf and copy old IP header (without options 266e86ebebcSAndre Oppermann * in front of ICMP message. 267c550f220SMax Laier * If the original mbuf was meant to bypass the firewall, the error 268c550f220SMax Laier * reply should bypass as well. 269c550f220SMax Laier */ 270c550f220SMax Laier m->m_flags |= n->m_flags & M_SKIP_FIREWALL; 271df8bae1dSRodney W. Grimes m->m_data -= sizeof(struct ip); 272df8bae1dSRodney W. Grimes m->m_len += sizeof(struct ip); 273df8bae1dSRodney W. Grimes m->m_pkthdr.len = m->m_len; 274df8bae1dSRodney W. Grimes m->m_pkthdr.rcvif = n->m_pkthdr.rcvif; 275df8bae1dSRodney W. Grimes nip = mtod(m, struct ip *); 276df8bae1dSRodney W. Grimes bcopy((caddr_t)oip, (caddr_t)nip, sizeof(struct ip)); 277df8bae1dSRodney W. Grimes nip->ip_len = m->m_len; 27853be11f6SPoul-Henning Kamp nip->ip_v = IPVERSION; 27953be11f6SPoul-Henning Kamp nip->ip_hl = 5; 280df8bae1dSRodney W. Grimes nip->ip_p = IPPROTO_ICMP; 281df8bae1dSRodney W. Grimes nip->ip_tos = 0; 282df8bae1dSRodney W. Grimes icmp_reflect(m); 283df8bae1dSRodney W. Grimes 284df8bae1dSRodney W. Grimes freeit: 285df8bae1dSRodney W. Grimes m_freem(n); 286df8bae1dSRodney W. Grimes } 287df8bae1dSRodney W. Grimes 288df8bae1dSRodney W. Grimes /* 289df8bae1dSRodney W. Grimes * Process a received ICMP message. 290df8bae1dSRodney W. Grimes */ 291df8bae1dSRodney W. Grimes void 292f2565d68SRobert Watson icmp_input(struct mbuf *m, int off) 293df8bae1dSRodney W. Grimes { 29416d6c90fSAndre Oppermann struct icmp *icp; 295df8bae1dSRodney W. Grimes struct in_ifaddr *ia; 29616d6c90fSAndre Oppermann struct ip *ip = mtod(m, struct ip *); 29716d6c90fSAndre Oppermann struct sockaddr_in icmpsrc, icmpdst, icmpgw; 29816d6c90fSAndre Oppermann int hlen = off; 29916d6c90fSAndre Oppermann int icmplen = ip->ip_len; 30016d6c90fSAndre Oppermann int i, code; 3014d77a549SAlfred Perlstein void (*ctlfunc)(int, struct sockaddr *, void *); 3028b07e49aSJulian Elischer int fibnum; 303df8bae1dSRodney W. Grimes 304df8bae1dSRodney W. Grimes /* 305df8bae1dSRodney W. Grimes * Locate icmp structure in mbuf, and check 306df8bae1dSRodney W. Grimes * that not corrupted and of at least minimum length. 307df8bae1dSRodney W. Grimes */ 308df8bae1dSRodney W. Grimes #ifdef ICMPPRINTFS 3092b758395SGarrett Wollman if (icmpprintfs) { 3102b758395SGarrett Wollman char buf[4 * sizeof "123"]; 3112b758395SGarrett Wollman strcpy(buf, inet_ntoa(ip->ip_src)); 3122b758395SGarrett Wollman printf("icmp_input from %s to %s, len %d\n", 3132b758395SGarrett Wollman buf, inet_ntoa(ip->ip_dst), icmplen); 3142b758395SGarrett Wollman } 315df8bae1dSRodney W. Grimes #endif 316df8bae1dSRodney W. Grimes if (icmplen < ICMP_MINLEN) { 317df8bae1dSRodney W. Grimes icmpstat.icps_tooshort++; 318df8bae1dSRodney W. Grimes goto freeit; 319df8bae1dSRodney W. Grimes } 320df8bae1dSRodney W. Grimes i = hlen + min(icmplen, ICMP_ADVLENMIN); 321df8bae1dSRodney W. Grimes if (m->m_len < i && (m = m_pullup(m, i)) == 0) { 322df8bae1dSRodney W. Grimes icmpstat.icps_tooshort++; 323df8bae1dSRodney W. Grimes return; 324df8bae1dSRodney W. Grimes } 325df8bae1dSRodney W. Grimes ip = mtod(m, struct ip *); 326df8bae1dSRodney W. Grimes m->m_len -= hlen; 327df8bae1dSRodney W. Grimes m->m_data += hlen; 328df8bae1dSRodney W. Grimes icp = mtod(m, struct icmp *); 329df8bae1dSRodney W. Grimes if (in_cksum(m, icmplen)) { 330df8bae1dSRodney W. Grimes icmpstat.icps_checksum++; 331df8bae1dSRodney W. Grimes goto freeit; 332df8bae1dSRodney W. Grimes } 333df8bae1dSRodney W. Grimes m->m_len += hlen; 334df8bae1dSRodney W. Grimes m->m_data -= hlen; 335df8bae1dSRodney W. Grimes 3366a800098SYoshinobu Inoue if (m->m_pkthdr.rcvif && m->m_pkthdr.rcvif->if_type == IFT_FAITH) { 3376a800098SYoshinobu Inoue /* 3386a800098SYoshinobu Inoue * Deliver very specific ICMP type only. 3396a800098SYoshinobu Inoue */ 3406a800098SYoshinobu Inoue switch (icp->icmp_type) { 3416a800098SYoshinobu Inoue case ICMP_UNREACH: 3426a800098SYoshinobu Inoue case ICMP_TIMXCEED: 3436a800098SYoshinobu Inoue break; 3446a800098SYoshinobu Inoue default: 3456a800098SYoshinobu Inoue goto freeit; 3466a800098SYoshinobu Inoue } 3476a800098SYoshinobu Inoue } 3486a800098SYoshinobu Inoue 349df8bae1dSRodney W. Grimes #ifdef ICMPPRINTFS 350df8bae1dSRodney W. Grimes if (icmpprintfs) 351df8bae1dSRodney W. Grimes printf("icmp_input, type %d code %d\n", icp->icmp_type, 352df8bae1dSRodney W. Grimes icp->icmp_code); 353df8bae1dSRodney W. Grimes #endif 3545b7ee6edSGarrett Wollman 3555b7ee6edSGarrett Wollman /* 3565b7ee6edSGarrett Wollman * Message type specific processing. 3575b7ee6edSGarrett Wollman */ 358df8bae1dSRodney W. Grimes if (icp->icmp_type > ICMP_MAXTYPE) 359df8bae1dSRodney W. Grimes goto raw; 36016d6c90fSAndre Oppermann 36116d6c90fSAndre Oppermann /* Initialize */ 36216d6c90fSAndre Oppermann bzero(&icmpsrc, sizeof(icmpsrc)); 36316d6c90fSAndre Oppermann icmpsrc.sin_len = sizeof(struct sockaddr_in); 36416d6c90fSAndre Oppermann icmpsrc.sin_family = AF_INET; 36516d6c90fSAndre Oppermann bzero(&icmpdst, sizeof(icmpdst)); 36616d6c90fSAndre Oppermann icmpdst.sin_len = sizeof(struct sockaddr_in); 36716d6c90fSAndre Oppermann icmpdst.sin_family = AF_INET; 36816d6c90fSAndre Oppermann bzero(&icmpgw, sizeof(icmpgw)); 36916d6c90fSAndre Oppermann icmpgw.sin_len = sizeof(struct sockaddr_in); 37016d6c90fSAndre Oppermann icmpgw.sin_family = AF_INET; 37116d6c90fSAndre Oppermann 372df8bae1dSRodney W. Grimes icmpstat.icps_inhist[icp->icmp_type]++; 373df8bae1dSRodney W. Grimes code = icp->icmp_code; 374df8bae1dSRodney W. Grimes switch (icp->icmp_type) { 375df8bae1dSRodney W. Grimes 376df8bae1dSRodney W. Grimes case ICMP_UNREACH: 377df8bae1dSRodney W. Grimes switch (code) { 378df8bae1dSRodney W. Grimes case ICMP_UNREACH_NET: 379df8bae1dSRodney W. Grimes case ICMP_UNREACH_HOST: 380df8bae1dSRodney W. Grimes case ICMP_UNREACH_SRCFAIL: 381e4bb5b05SJonathan Lemon case ICMP_UNREACH_NET_UNKNOWN: 382e4bb5b05SJonathan Lemon case ICMP_UNREACH_HOST_UNKNOWN: 383e4bb5b05SJonathan Lemon case ICMP_UNREACH_ISOLATED: 384e4bb5b05SJonathan Lemon case ICMP_UNREACH_TOSNET: 385e4bb5b05SJonathan Lemon case ICMP_UNREACH_TOSHOST: 386e4bb5b05SJonathan Lemon case ICMP_UNREACH_HOST_PRECEDENCE: 387e4bb5b05SJonathan Lemon case ICMP_UNREACH_PRECEDENCE_CUTOFF: 388e4bb5b05SJonathan Lemon code = PRC_UNREACH_NET; 389df8bae1dSRodney W. Grimes break; 390df8bae1dSRodney W. Grimes 391df8bae1dSRodney W. Grimes case ICMP_UNREACH_NEEDFRAG: 392df8bae1dSRodney W. Grimes code = PRC_MSGSIZE; 393df8bae1dSRodney W. Grimes break; 394df8bae1dSRodney W. Grimes 395e4bb5b05SJonathan Lemon /* 396e4bb5b05SJonathan Lemon * RFC 1122, Sections 3.2.2.1 and 4.2.3.9. 397e4bb5b05SJonathan Lemon * Treat subcodes 2,3 as immediate RST 398e4bb5b05SJonathan Lemon */ 399e4bb5b05SJonathan Lemon case ICMP_UNREACH_PROTOCOL: 400e4bb5b05SJonathan Lemon case ICMP_UNREACH_PORT: 401b77d155dSJesper Skriver code = PRC_UNREACH_PORT; 402b11d7a4aSPoul-Henning Kamp break; 40390fcbbd6SPoul-Henning Kamp 40490fcbbd6SPoul-Henning Kamp case ICMP_UNREACH_NET_PROHIB: 40590fcbbd6SPoul-Henning Kamp case ICMP_UNREACH_HOST_PROHIB: 4069c4b2574SPaul Traina case ICMP_UNREACH_FILTER_PROHIB: 40790fcbbd6SPoul-Henning Kamp code = PRC_UNREACH_ADMIN_PROHIB; 408b11d7a4aSPoul-Henning Kamp break; 409b11d7a4aSPoul-Henning Kamp 410df8bae1dSRodney W. Grimes default: 411df8bae1dSRodney W. Grimes goto badcode; 412df8bae1dSRodney W. Grimes } 413df8bae1dSRodney W. Grimes goto deliver; 414df8bae1dSRodney W. Grimes 415df8bae1dSRodney W. Grimes case ICMP_TIMXCEED: 416df8bae1dSRodney W. Grimes if (code > 1) 417df8bae1dSRodney W. Grimes goto badcode; 418df8bae1dSRodney W. Grimes code += PRC_TIMXCEED_INTRANS; 419df8bae1dSRodney W. Grimes goto deliver; 420df8bae1dSRodney W. Grimes 421df8bae1dSRodney W. Grimes case ICMP_PARAMPROB: 422df8bae1dSRodney W. Grimes if (code > 1) 423df8bae1dSRodney W. Grimes goto badcode; 424df8bae1dSRodney W. Grimes code = PRC_PARAMPROB; 425df8bae1dSRodney W. Grimes goto deliver; 426df8bae1dSRodney W. Grimes 427df8bae1dSRodney W. Grimes case ICMP_SOURCEQUENCH: 428df8bae1dSRodney W. Grimes if (code) 429df8bae1dSRodney W. Grimes goto badcode; 430df8bae1dSRodney W. Grimes code = PRC_QUENCH; 431df8bae1dSRodney W. Grimes deliver: 432df8bae1dSRodney W. Grimes /* 433df8bae1dSRodney W. Grimes * Problem with datagram; advise higher level routines. 434df8bae1dSRodney W. Grimes */ 435df8bae1dSRodney W. Grimes if (icmplen < ICMP_ADVLENMIN || icmplen < ICMP_ADVLEN(icp) || 43653be11f6SPoul-Henning Kamp icp->icmp_ip.ip_hl < (sizeof(struct ip) >> 2)) { 437df8bae1dSRodney W. Grimes icmpstat.icps_badlen++; 438df8bae1dSRodney W. Grimes goto freeit; 439df8bae1dSRodney W. Grimes } 440fd8e4ebcSMike Barcroft icp->icmp_ip.ip_len = ntohs(icp->icmp_ip.ip_len); 4415b7ee6edSGarrett Wollman /* Discard ICMP's in response to multicast packets */ 4425b7ee6edSGarrett Wollman if (IN_MULTICAST(ntohl(icp->icmp_ip.ip_dst.s_addr))) 4435b7ee6edSGarrett Wollman goto badcode; 444df8bae1dSRodney W. Grimes #ifdef ICMPPRINTFS 445df8bae1dSRodney W. Grimes if (icmpprintfs) 446df8bae1dSRodney W. Grimes printf("deliver to protocol %d\n", icp->icmp_ip.ip_p); 447df8bae1dSRodney W. Grimes #endif 448df8bae1dSRodney W. Grimes icmpsrc.sin_addr = icp->icmp_ip.ip_dst; 4496a800098SYoshinobu Inoue /* 4506a800098SYoshinobu Inoue * XXX if the packet contains [IPv4 AH TCP], we can't make a 4516a800098SYoshinobu Inoue * notification to TCP layer. 4526a800098SYoshinobu Inoue */ 453623ae52eSPoul-Henning Kamp ctlfunc = inetsw[ip_protox[icp->icmp_ip.ip_p]].pr_ctlinput; 454623ae52eSPoul-Henning Kamp if (ctlfunc) 455df8bae1dSRodney W. Grimes (*ctlfunc)(code, (struct sockaddr *)&icmpsrc, 456b62d102cSBruce Evans (void *)&icp->icmp_ip); 457df8bae1dSRodney W. Grimes break; 458df8bae1dSRodney W. Grimes 459df8bae1dSRodney W. Grimes badcode: 460df8bae1dSRodney W. Grimes icmpstat.icps_badcode++; 461df8bae1dSRodney W. Grimes break; 462df8bae1dSRodney W. Grimes 463df8bae1dSRodney W. Grimes case ICMP_ECHO: 4647022ea0aSGarrett Wollman if (!icmpbmcastecho 465d311884fSDavid Greenman && (m->m_flags & (M_MCAST | M_BCAST)) != 0) { 4667022ea0aSGarrett Wollman icmpstat.icps_bmcastecho++; 4677022ea0aSGarrett Wollman break; 4687022ea0aSGarrett Wollman } 469df8bae1dSRodney W. Grimes icp->icmp_type = ICMP_ECHOREPLY; 470a57815efSBosko Milekic if (badport_bandlim(BANDLIM_ICMP_ECHO) < 0) 47109f81a46SBosko Milekic goto freeit; 47209f81a46SBosko Milekic else 473df8bae1dSRodney W. Grimes goto reflect; 474df8bae1dSRodney W. Grimes 475df8bae1dSRodney W. Grimes case ICMP_TSTAMP: 476fe0fb8abSGarrett Wollman if (!icmpbmcastecho 477d311884fSDavid Greenman && (m->m_flags & (M_MCAST | M_BCAST)) != 0) { 478fe0fb8abSGarrett Wollman icmpstat.icps_bmcasttstamp++; 479fe0fb8abSGarrett Wollman break; 480fe0fb8abSGarrett Wollman } 481df8bae1dSRodney W. Grimes if (icmplen < ICMP_TSLEN) { 482df8bae1dSRodney W. Grimes icmpstat.icps_badlen++; 483df8bae1dSRodney W. Grimes break; 484df8bae1dSRodney W. Grimes } 485df8bae1dSRodney W. Grimes icp->icmp_type = ICMP_TSTAMPREPLY; 486df8bae1dSRodney W. Grimes icp->icmp_rtime = iptime(); 487df8bae1dSRodney W. Grimes icp->icmp_ttime = icp->icmp_rtime; /* bogus, do later! */ 488a57815efSBosko Milekic if (badport_bandlim(BANDLIM_ICMP_TSTAMP) < 0) 48909f81a46SBosko Milekic goto freeit; 49009f81a46SBosko Milekic else 491df8bae1dSRodney W. Grimes goto reflect; 492df8bae1dSRodney W. Grimes 493df8bae1dSRodney W. Grimes case ICMP_MASKREQ: 494df8bae1dSRodney W. Grimes if (icmpmaskrepl == 0) 495df8bae1dSRodney W. Grimes break; 496df8bae1dSRodney W. Grimes /* 497df8bae1dSRodney W. Grimes * We are not able to respond with all ones broadcast 498df8bae1dSRodney W. Grimes * unless we receive it over a point-to-point interface. 499df8bae1dSRodney W. Grimes */ 500df8bae1dSRodney W. Grimes if (icmplen < ICMP_MASKLEN) 501df8bae1dSRodney W. Grimes break; 502df8bae1dSRodney W. Grimes switch (ip->ip_dst.s_addr) { 503df8bae1dSRodney W. Grimes 504df8bae1dSRodney W. Grimes case INADDR_BROADCAST: 505df8bae1dSRodney W. Grimes case INADDR_ANY: 506df8bae1dSRodney W. Grimes icmpdst.sin_addr = ip->ip_src; 507df8bae1dSRodney W. Grimes break; 508df8bae1dSRodney W. Grimes 509df8bae1dSRodney W. Grimes default: 510df8bae1dSRodney W. Grimes icmpdst.sin_addr = ip->ip_dst; 511df8bae1dSRodney W. Grimes } 512df8bae1dSRodney W. Grimes ia = (struct in_ifaddr *)ifaof_ifpforaddr( 513df8bae1dSRodney W. Grimes (struct sockaddr *)&icmpdst, m->m_pkthdr.rcvif); 514df8bae1dSRodney W. Grimes if (ia == 0) 515df8bae1dSRodney W. Grimes break; 5167e6f7714SPoul-Henning Kamp if (ia->ia_ifp == 0) 5177e6f7714SPoul-Henning Kamp break; 518df8bae1dSRodney W. Grimes icp->icmp_type = ICMP_MASKREPLY; 51957842a38SMatthew N. Dodd if (icmpmaskfake == 0) 520df8bae1dSRodney W. Grimes icp->icmp_mask = ia->ia_sockmask.sin_addr.s_addr; 52157842a38SMatthew N. Dodd else 52257842a38SMatthew N. Dodd icp->icmp_mask = icmpmaskfake; 523df8bae1dSRodney W. Grimes if (ip->ip_src.s_addr == 0) { 524df8bae1dSRodney W. Grimes if (ia->ia_ifp->if_flags & IFF_BROADCAST) 525df8bae1dSRodney W. Grimes ip->ip_src = satosin(&ia->ia_broadaddr)->sin_addr; 526df8bae1dSRodney W. Grimes else if (ia->ia_ifp->if_flags & IFF_POINTOPOINT) 527df8bae1dSRodney W. Grimes ip->ip_src = satosin(&ia->ia_dstaddr)->sin_addr; 528df8bae1dSRodney W. Grimes } 529df8bae1dSRodney W. Grimes reflect: 530df8bae1dSRodney W. Grimes ip->ip_len += hlen; /* since ip_input deducts this */ 531df8bae1dSRodney W. Grimes icmpstat.icps_reflect++; 532df8bae1dSRodney W. Grimes icmpstat.icps_outhist[icp->icmp_type]++; 533df8bae1dSRodney W. Grimes icmp_reflect(m); 534df8bae1dSRodney W. Grimes return; 535df8bae1dSRodney W. Grimes 536df8bae1dSRodney W. Grimes case ICMP_REDIRECT: 53718d3153eSDag-Erling Smørgrav if (log_redirect) { 53818d3153eSDag-Erling Smørgrav u_long src, dst, gw; 53918d3153eSDag-Erling Smørgrav 54018d3153eSDag-Erling Smørgrav src = ntohl(ip->ip_src.s_addr); 54118d3153eSDag-Erling Smørgrav dst = ntohl(icp->icmp_ip.ip_dst.s_addr); 54218d3153eSDag-Erling Smørgrav gw = ntohl(icp->icmp_gwaddr.s_addr); 54318d3153eSDag-Erling Smørgrav printf("icmp redirect from %d.%d.%d.%d: " 54418d3153eSDag-Erling Smørgrav "%d.%d.%d.%d => %d.%d.%d.%d\n", 54518d3153eSDag-Erling Smørgrav (int)(src >> 24), (int)((src >> 16) & 0xff), 54618d3153eSDag-Erling Smørgrav (int)((src >> 8) & 0xff), (int)(src & 0xff), 54718d3153eSDag-Erling Smørgrav (int)(dst >> 24), (int)((dst >> 16) & 0xff), 54818d3153eSDag-Erling Smørgrav (int)((dst >> 8) & 0xff), (int)(dst & 0xff), 54918d3153eSDag-Erling Smørgrav (int)(gw >> 24), (int)((gw >> 16) & 0xff), 55018d3153eSDag-Erling Smørgrav (int)((gw >> 8) & 0xff), (int)(gw & 0xff)); 55118d3153eSDag-Erling Smørgrav } 55287c3bd27SAndre Oppermann /* 55387c3bd27SAndre Oppermann * RFC1812 says we must ignore ICMP redirects if we 55487c3bd27SAndre Oppermann * are acting as router. 55587c3bd27SAndre Oppermann */ 55687c3bd27SAndre Oppermann if (drop_redirect || ipforwarding) 55718d3153eSDag-Erling Smørgrav break; 558df8bae1dSRodney W. Grimes if (code > 3) 559df8bae1dSRodney W. Grimes goto badcode; 560df8bae1dSRodney W. Grimes if (icmplen < ICMP_ADVLENMIN || icmplen < ICMP_ADVLEN(icp) || 56153be11f6SPoul-Henning Kamp icp->icmp_ip.ip_hl < (sizeof(struct ip) >> 2)) { 562df8bae1dSRodney W. Grimes icmpstat.icps_badlen++; 563df8bae1dSRodney W. Grimes break; 564df8bae1dSRodney W. Grimes } 565df8bae1dSRodney W. Grimes /* 566df8bae1dSRodney W. Grimes * Short circuit routing redirects to force 567df8bae1dSRodney W. Grimes * immediate change in the kernel's routing 568df8bae1dSRodney W. Grimes * tables. The message is also handed to anyone 569df8bae1dSRodney W. Grimes * listening on a raw socket (e.g. the routing 570df8bae1dSRodney W. Grimes * daemon for use in updating its tables). 571df8bae1dSRodney W. Grimes */ 572df8bae1dSRodney W. Grimes icmpgw.sin_addr = ip->ip_src; 573df8bae1dSRodney W. Grimes icmpdst.sin_addr = icp->icmp_gwaddr; 574df8bae1dSRodney W. Grimes #ifdef ICMPPRINTFS 5752b758395SGarrett Wollman if (icmpprintfs) { 5762b758395SGarrett Wollman char buf[4 * sizeof "123"]; 5772b758395SGarrett Wollman strcpy(buf, inet_ntoa(icp->icmp_ip.ip_dst)); 5782b758395SGarrett Wollman 5792b758395SGarrett Wollman printf("redirect dst %s to %s\n", 5802b758395SGarrett Wollman buf, inet_ntoa(icp->icmp_gwaddr)); 5812b758395SGarrett Wollman } 582df8bae1dSRodney W. Grimes #endif 583df8bae1dSRodney W. Grimes icmpsrc.sin_addr = icp->icmp_ip.ip_dst; 5848b07e49aSJulian Elischer for ( fibnum = 0; fibnum < rt_numfibs; fibnum++) { 5858b07e49aSJulian Elischer in_rtredirect((struct sockaddr *)&icmpsrc, 586df8bae1dSRodney W. Grimes (struct sockaddr *)&icmpdst, 587df8bae1dSRodney W. Grimes (struct sockaddr *)0, RTF_GATEWAY | RTF_HOST, 5888b07e49aSJulian Elischer (struct sockaddr *)&icmpgw, fibnum); 5898b07e49aSJulian Elischer } 590df8bae1dSRodney W. Grimes pfctlinput(PRC_REDIRECT_HOST, (struct sockaddr *)&icmpsrc); 591b2630c29SGeorge V. Neville-Neil #ifdef IPSEC 5926a800098SYoshinobu Inoue key_sa_routechange((struct sockaddr *)&icmpsrc); 5936a800098SYoshinobu Inoue #endif 594df8bae1dSRodney W. Grimes break; 595df8bae1dSRodney W. Grimes 596df8bae1dSRodney W. Grimes /* 597df8bae1dSRodney W. Grimes * No kernel processing for the following; 598df8bae1dSRodney W. Grimes * just fall through to send to raw listener. 599df8bae1dSRodney W. Grimes */ 600df8bae1dSRodney W. Grimes case ICMP_ECHOREPLY: 601df8bae1dSRodney W. Grimes case ICMP_ROUTERADVERT: 602df8bae1dSRodney W. Grimes case ICMP_ROUTERSOLICIT: 603df8bae1dSRodney W. Grimes case ICMP_TSTAMPREPLY: 604df8bae1dSRodney W. Grimes case ICMP_IREQREPLY: 605df8bae1dSRodney W. Grimes case ICMP_MASKREPLY: 606df8bae1dSRodney W. Grimes default: 607df8bae1dSRodney W. Grimes break; 608df8bae1dSRodney W. Grimes } 609df8bae1dSRodney W. Grimes 610df8bae1dSRodney W. Grimes raw: 611f0ffb944SJulian Elischer rip_input(m, off); 612df8bae1dSRodney W. Grimes return; 613df8bae1dSRodney W. Grimes 614df8bae1dSRodney W. Grimes freeit: 615df8bae1dSRodney W. Grimes m_freem(m); 616df8bae1dSRodney W. Grimes } 617df8bae1dSRodney W. Grimes 618df8bae1dSRodney W. Grimes /* 619df8bae1dSRodney W. Grimes * Reflect the ip packet back to the source 620df8bae1dSRodney W. Grimes */ 6210312fbe9SPoul-Henning Kamp static void 622f2565d68SRobert Watson icmp_reflect(struct mbuf *m) 623df8bae1dSRodney W. Grimes { 624ca925d9cSJonathan Lemon struct ip *ip = mtod(m, struct ip *); 625ca925d9cSJonathan Lemon struct ifaddr *ifa; 626b74d89bbSAndre Oppermann struct ifnet *ifn; 627ca925d9cSJonathan Lemon struct in_ifaddr *ia; 628df8bae1dSRodney W. Grimes struct in_addr t; 629b5e8ce9fSBruce Evans struct mbuf *opts = 0; 63053be11f6SPoul-Henning Kamp int optlen = (ip->ip_hl << 2) - sizeof(struct ip); 631df8bae1dSRodney W. Grimes 6326b9ff6b7SGeorge V. Neville-Neil if (IN_MULTICAST(ntohl(ip->ip_src.s_addr)) || 6336b9ff6b7SGeorge V. Neville-Neil IN_EXPERIMENTAL(ntohl(ip->ip_src.s_addr)) || 6340327aeb9SGeorge V. Neville-Neil IN_LOOPBACK(ntohl(ip->ip_src.s_addr)) || 6356b9ff6b7SGeorge V. Neville-Neil IN_ZERONET(ntohl(ip->ip_src.s_addr)) ) { 636df8bae1dSRodney W. Grimes m_freem(m); /* Bad return address */ 637bd714208SRuslan Ermilov icmpstat.icps_badaddr++; 638df8bae1dSRodney W. Grimes goto done; /* Ip_output() will check for broadcast */ 639df8bae1dSRodney W. Grimes } 6406b9ff6b7SGeorge V. Neville-Neil 641df8bae1dSRodney W. Grimes t = ip->ip_dst; 642df8bae1dSRodney W. Grimes ip->ip_dst = ip->ip_src; 6431488eac8SAndre Oppermann 644df8bae1dSRodney W. Grimes /* 6451488eac8SAndre Oppermann * Source selection for ICMP replies: 6461488eac8SAndre Oppermann * 6471488eac8SAndre Oppermann * If the incoming packet was addressed directly to one of our 6481488eac8SAndre Oppermann * own addresses, use dst as the src for the reply. 649df8bae1dSRodney W. Grimes */ 650ca925d9cSJonathan Lemon LIST_FOREACH(ia, INADDR_HASH(t.s_addr), ia_hash) 651df8bae1dSRodney W. Grimes if (t.s_addr == IA_SIN(ia)->sin_addr.s_addr) 652ca925d9cSJonathan Lemon goto match; 6531488eac8SAndre Oppermann /* 6541488eac8SAndre Oppermann * If the incoming packet was addressed to one of our broadcast 6551488eac8SAndre Oppermann * addresses, use the first non-broadcast address which corresponds 6561488eac8SAndre Oppermann * to the incoming interface. 6571488eac8SAndre Oppermann */ 65884caf008SRuslan Ermilov if (m->m_pkthdr.rcvif != NULL && 65984caf008SRuslan Ermilov m->m_pkthdr.rcvif->if_flags & IFF_BROADCAST) { 660ca925d9cSJonathan Lemon TAILQ_FOREACH(ifa, &m->m_pkthdr.rcvif->if_addrhead, ifa_link) { 661ca925d9cSJonathan Lemon if (ifa->ifa_addr->sa_family != AF_INET) 662ca925d9cSJonathan Lemon continue; 663ca925d9cSJonathan Lemon ia = ifatoia(ifa); 664ca925d9cSJonathan Lemon if (satosin(&ia->ia_broadaddr)->sin_addr.s_addr == 665ca925d9cSJonathan Lemon t.s_addr) 666ca925d9cSJonathan Lemon goto match; 667df8bae1dSRodney W. Grimes } 668ca925d9cSJonathan Lemon } 6691488eac8SAndre Oppermann /* 670a0866c8dSAndre Oppermann * If the packet was transiting through us, use the address of 671a0866c8dSAndre Oppermann * the interface the packet came through in. If that interface 672a0866c8dSAndre Oppermann * doesn't have a suitable IP address, the normal selection 673a0866c8dSAndre Oppermann * criteria apply. 674a0866c8dSAndre Oppermann */ 675a0866c8dSAndre Oppermann if (icmp_rfi && m->m_pkthdr.rcvif != NULL) { 676a0866c8dSAndre Oppermann TAILQ_FOREACH(ifa, &m->m_pkthdr.rcvif->if_addrhead, ifa_link) { 677a0866c8dSAndre Oppermann if (ifa->ifa_addr->sa_family != AF_INET) 678a0866c8dSAndre Oppermann continue; 679a0866c8dSAndre Oppermann ia = ifatoia(ifa); 680a0866c8dSAndre Oppermann goto match; 681a0866c8dSAndre Oppermann } 682a0866c8dSAndre Oppermann } 683a0866c8dSAndre Oppermann /* 684b74d89bbSAndre Oppermann * If the incoming packet was not addressed directly to us, use 685b74d89bbSAndre Oppermann * designated interface for icmp replies specified by sysctl 686b74d89bbSAndre Oppermann * net.inet.icmp.reply_src (default not set). Otherwise continue 687b74d89bbSAndre Oppermann * with normal source selection. 688b74d89bbSAndre Oppermann */ 689b74d89bbSAndre Oppermann if (reply_src[0] != '\0' && (ifn = ifunit(reply_src))) { 690b74d89bbSAndre Oppermann TAILQ_FOREACH(ifa, &ifn->if_addrhead, ifa_link) { 691b74d89bbSAndre Oppermann if (ifa->ifa_addr->sa_family != AF_INET) 692b74d89bbSAndre Oppermann continue; 693b74d89bbSAndre Oppermann ia = ifatoia(ifa); 694b74d89bbSAndre Oppermann goto match; 695b74d89bbSAndre Oppermann } 696b74d89bbSAndre Oppermann } 697b74d89bbSAndre Oppermann /* 6981488eac8SAndre Oppermann * If the packet was transiting through us, use the address of 6991488eac8SAndre Oppermann * the interface that is the closest to the packet source. 7001488eac8SAndre Oppermann * When we don't have a route back to the packet source, stop here 7011488eac8SAndre Oppermann * and drop the packet. 7021488eac8SAndre Oppermann */ 7038b07e49aSJulian Elischer ia = ip_rtaddr(ip->ip_dst, M_GETFIB(m)); 7040d4bef5dSDima Dorfman if (ia == NULL) { 7050d4bef5dSDima Dorfman m_freem(m); 706bd714208SRuslan Ermilov icmpstat.icps_noroute++; 7070d4bef5dSDima Dorfman goto done; 7080d4bef5dSDima Dorfman } 709ca925d9cSJonathan Lemon match: 710baee0c3eSRobert Watson #ifdef MAC 711a13e21f7SRobert Watson mac_netinet_icmp_replyinplace(m); 712baee0c3eSRobert Watson #endif 713df8bae1dSRodney W. Grimes t = IA_SIN(ia)->sin_addr; 714df8bae1dSRodney W. Grimes ip->ip_src = t; 7158ce3f3ddSRuslan Ermilov ip->ip_ttl = ip_defttl; 716df8bae1dSRodney W. Grimes 717df8bae1dSRodney W. Grimes if (optlen > 0) { 718df8bae1dSRodney W. Grimes register u_char *cp; 719df8bae1dSRodney W. Grimes int opt, cnt; 720df8bae1dSRodney W. Grimes u_int len; 721df8bae1dSRodney W. Grimes 722df8bae1dSRodney W. Grimes /* 723df8bae1dSRodney W. Grimes * Retrieve any source routing from the incoming packet; 724df8bae1dSRodney W. Grimes * add on any record-route or timestamp options. 725df8bae1dSRodney W. Grimes */ 726df8bae1dSRodney W. Grimes cp = (u_char *) (ip + 1); 727e0982661SAndre Oppermann if ((opts = ip_srcroute(m)) == 0 && 72834333b16SAndre Oppermann (opts = m_gethdr(M_DONTWAIT, MT_DATA))) { 729df8bae1dSRodney W. Grimes opts->m_len = sizeof(struct in_addr); 730df8bae1dSRodney W. Grimes mtod(opts, struct in_addr *)->s_addr = 0; 731df8bae1dSRodney W. Grimes } 732df8bae1dSRodney W. Grimes if (opts) { 733df8bae1dSRodney W. Grimes #ifdef ICMPPRINTFS 734df8bae1dSRodney W. Grimes if (icmpprintfs) 735df8bae1dSRodney W. Grimes printf("icmp_reflect optlen %d rt %d => ", 736df8bae1dSRodney W. Grimes optlen, opts->m_len); 737df8bae1dSRodney W. Grimes #endif 738df8bae1dSRodney W. Grimes for (cnt = optlen; cnt > 0; cnt -= len, cp += len) { 739df8bae1dSRodney W. Grimes opt = cp[IPOPT_OPTVAL]; 740df8bae1dSRodney W. Grimes if (opt == IPOPT_EOL) 741df8bae1dSRodney W. Grimes break; 742df8bae1dSRodney W. Grimes if (opt == IPOPT_NOP) 743df8bae1dSRodney W. Grimes len = 1; 744df8bae1dSRodney W. Grimes else { 745707d00a3SJonathan Lemon if (cnt < IPOPT_OLEN + sizeof(*cp)) 746707d00a3SJonathan Lemon break; 747df8bae1dSRodney W. Grimes len = cp[IPOPT_OLEN]; 748707d00a3SJonathan Lemon if (len < IPOPT_OLEN + sizeof(*cp) || 749707d00a3SJonathan Lemon len > cnt) 750df8bae1dSRodney W. Grimes break; 751df8bae1dSRodney W. Grimes } 752df8bae1dSRodney W. Grimes /* 753df8bae1dSRodney W. Grimes * Should check for overflow, but it "can't happen" 754df8bae1dSRodney W. Grimes */ 755df8bae1dSRodney W. Grimes if (opt == IPOPT_RR || opt == IPOPT_TS || 756df8bae1dSRodney W. Grimes opt == IPOPT_SECURITY) { 757df8bae1dSRodney W. Grimes bcopy((caddr_t)cp, 758df8bae1dSRodney W. Grimes mtod(opts, caddr_t) + opts->m_len, len); 759df8bae1dSRodney W. Grimes opts->m_len += len; 760df8bae1dSRodney W. Grimes } 761df8bae1dSRodney W. Grimes } 762df8bae1dSRodney W. Grimes /* Terminate & pad, if necessary */ 763623ae52eSPoul-Henning Kamp cnt = opts->m_len % 4; 764623ae52eSPoul-Henning Kamp if (cnt) { 765df8bae1dSRodney W. Grimes for (; cnt < 4; cnt++) { 766df8bae1dSRodney W. Grimes *(mtod(opts, caddr_t) + opts->m_len) = 767df8bae1dSRodney W. Grimes IPOPT_EOL; 768df8bae1dSRodney W. Grimes opts->m_len++; 769df8bae1dSRodney W. Grimes } 770df8bae1dSRodney W. Grimes } 771df8bae1dSRodney W. Grimes #ifdef ICMPPRINTFS 772df8bae1dSRodney W. Grimes if (icmpprintfs) 773df8bae1dSRodney W. Grimes printf("%d\n", opts->m_len); 774df8bae1dSRodney W. Grimes #endif 775df8bae1dSRodney W. Grimes } 776df8bae1dSRodney W. Grimes /* 777df8bae1dSRodney W. Grimes * Now strip out original options by copying rest of first 778df8bae1dSRodney W. Grimes * mbuf's data back, and adjust the IP length. 779df8bae1dSRodney W. Grimes */ 780df8bae1dSRodney W. Grimes ip->ip_len -= optlen; 78153be11f6SPoul-Henning Kamp ip->ip_v = IPVERSION; 78253be11f6SPoul-Henning Kamp ip->ip_hl = 5; 783df8bae1dSRodney W. Grimes m->m_len -= optlen; 784df8bae1dSRodney W. Grimes if (m->m_flags & M_PKTHDR) 785df8bae1dSRodney W. Grimes m->m_pkthdr.len -= optlen; 786df8bae1dSRodney W. Grimes optlen += sizeof(struct ip); 787df8bae1dSRodney W. Grimes bcopy((caddr_t)ip + optlen, (caddr_t)(ip + 1), 788df8bae1dSRodney W. Grimes (unsigned)(m->m_len - sizeof(struct ip))); 789df8bae1dSRodney W. Grimes } 7909c855a36SSam Leffler m_tag_delete_nonpersistent(m); 791df8bae1dSRodney W. Grimes m->m_flags &= ~(M_BCAST|M_MCAST); 79202c1c707SAndre Oppermann icmp_send(m, opts); 793df8bae1dSRodney W. Grimes done: 794df8bae1dSRodney W. Grimes if (opts) 795df8bae1dSRodney W. Grimes (void)m_free(opts); 796df8bae1dSRodney W. Grimes } 797df8bae1dSRodney W. Grimes 798df8bae1dSRodney W. Grimes /* 799df8bae1dSRodney W. Grimes * Send an icmp packet back to the ip level, 800df8bae1dSRodney W. Grimes * after supplying a checksum. 801df8bae1dSRodney W. Grimes */ 8020312fbe9SPoul-Henning Kamp static void 803f2565d68SRobert Watson icmp_send(struct mbuf *m, struct mbuf *opts) 804df8bae1dSRodney W. Grimes { 805df8bae1dSRodney W. Grimes register struct ip *ip = mtod(m, struct ip *); 806df8bae1dSRodney W. Grimes register int hlen; 807df8bae1dSRodney W. Grimes register struct icmp *icp; 808df8bae1dSRodney W. Grimes 80953be11f6SPoul-Henning Kamp hlen = ip->ip_hl << 2; 810df8bae1dSRodney W. Grimes m->m_data += hlen; 811df8bae1dSRodney W. Grimes m->m_len -= hlen; 812df8bae1dSRodney W. Grimes icp = mtod(m, struct icmp *); 813df8bae1dSRodney W. Grimes icp->icmp_cksum = 0; 814df8bae1dSRodney W. Grimes icp->icmp_cksum = in_cksum(m, ip->ip_len - hlen); 815df8bae1dSRodney W. Grimes m->m_data -= hlen; 816df8bae1dSRodney W. Grimes m->m_len += hlen; 81794446a2eSArchie Cobbs m->m_pkthdr.rcvif = (struct ifnet *)0; 818df8bae1dSRodney W. Grimes #ifdef ICMPPRINTFS 8192b758395SGarrett Wollman if (icmpprintfs) { 8202b758395SGarrett Wollman char buf[4 * sizeof "123"]; 8212b758395SGarrett Wollman strcpy(buf, inet_ntoa(ip->ip_dst)); 8222b758395SGarrett Wollman printf("icmp_send dst %s src %s\n", 8232b758395SGarrett Wollman buf, inet_ntoa(ip->ip_src)); 8242b758395SGarrett Wollman } 825df8bae1dSRodney W. Grimes #endif 82602c1c707SAndre Oppermann (void) ip_output(m, opts, NULL, 0, NULL, NULL); 827df8bae1dSRodney W. Grimes } 828df8bae1dSRodney W. Grimes 829df8bae1dSRodney W. Grimes n_time 830f2565d68SRobert Watson iptime(void) 831df8bae1dSRodney W. Grimes { 832df8bae1dSRodney W. Grimes struct timeval atv; 833df8bae1dSRodney W. Grimes u_long t; 834df8bae1dSRodney W. Grimes 83516cd6db0SBill Fumerola getmicrotime(&atv); 836df8bae1dSRodney W. Grimes t = (atv.tv_sec % (24*60*60)) * 1000 + atv.tv_usec / 1000; 837df8bae1dSRodney W. Grimes return (htonl(t)); 838df8bae1dSRodney W. Grimes } 839df8bae1dSRodney W. Grimes 8405cbf3e08SGarrett Wollman /* 8415cbf3e08SGarrett Wollman * Return the next larger or smaller MTU plateau (table from RFC 1191) 8425cbf3e08SGarrett Wollman * given current value MTU. If DIR is less than zero, a larger plateau 8435cbf3e08SGarrett Wollman * is returned; otherwise, a smaller value is returned. 8445cbf3e08SGarrett Wollman */ 8451aedbd9cSAndre Oppermann int 846f2565d68SRobert Watson ip_next_mtu(int mtu, int dir) 8475cbf3e08SGarrett Wollman { 8485cbf3e08SGarrett Wollman static int mtutab[] = { 8494c037f8dSAndre Oppermann 65535, 32000, 17914, 8166, 4352, 2002, 1492, 1280, 1006, 508, 8504c037f8dSAndre Oppermann 296, 68, 0 8515cbf3e08SGarrett Wollman }; 85206003a1eSAndre Oppermann int i, size; 8535cbf3e08SGarrett Wollman 85406003a1eSAndre Oppermann size = (sizeof mtutab) / (sizeof mtutab[0]); 85506003a1eSAndre Oppermann if (dir >= 0) { 8561c0b0f52SGleb Smirnoff for (i = 0; i < size; i++) 85706003a1eSAndre Oppermann if (mtu > mtutab[i]) 8585cbf3e08SGarrett Wollman return mtutab[i]; 8595cbf3e08SGarrett Wollman } else { 86006003a1eSAndre Oppermann for (i = size - 1; i >= 0; i--) 86106003a1eSAndre Oppermann if (mtu < mtutab[i]) 86206003a1eSAndre Oppermann return mtutab[i]; 86306003a1eSAndre Oppermann if (mtu == mtutab[0]) 86406003a1eSAndre Oppermann return mtutab[0]; 8655cbf3e08SGarrett Wollman } 86606003a1eSAndre Oppermann return 0; 8675cbf3e08SGarrett Wollman } 86851508de1SMatthew Dillon 86951508de1SMatthew Dillon 87051508de1SMatthew Dillon /* 87151508de1SMatthew Dillon * badport_bandlim() - check for ICMP bandwidth limit 87251508de1SMatthew Dillon * 87351508de1SMatthew Dillon * Return 0 if it is ok to send an ICMP error response, -1 if we have 87451508de1SMatthew Dillon * hit our bandwidth limit and it is not ok. 87551508de1SMatthew Dillon * 87651508de1SMatthew Dillon * If icmplim is <= 0, the feature is disabled and 0 is returned. 87751508de1SMatthew Dillon * 87851508de1SMatthew Dillon * For now we separate the TCP and UDP subsystems w/ different 'which' 87951508de1SMatthew Dillon * values. We may eventually remove this separation (and simplify the 88051508de1SMatthew Dillon * code further). 88151508de1SMatthew Dillon * 88251508de1SMatthew Dillon * Note that the printing of the error message is delayed so we can 88351508de1SMatthew Dillon * properly print the icmp error rate that the system was trying to do 88451508de1SMatthew Dillon * (i.e. 22000/100 pps, etc...). This can cause long delays in printing 88551508de1SMatthew Dillon * the 'final' error, but it doesn't make sense to solve the printing 88651508de1SMatthew Dillon * delay with more complex code. 88751508de1SMatthew Dillon */ 88851508de1SMatthew Dillon 88951508de1SMatthew Dillon int 89051508de1SMatthew Dillon badport_bandlim(int which) 89151508de1SMatthew Dillon { 89200f21882SSam Leffler #define N(a) (sizeof (a) / sizeof (a[0])) 89300f21882SSam Leffler static struct rate { 89400f21882SSam Leffler const char *type; 89500f21882SSam Leffler struct timeval lasttime; 896439dfb0cSStefan Farfeleder int curpps; 89700f21882SSam Leffler } rates[BANDLIM_MAX+1] = { 89800f21882SSam Leffler { "icmp unreach response" }, 89900f21882SSam Leffler { "icmp ping response" }, 90000f21882SSam Leffler { "icmp tstamp response" }, 90100f21882SSam Leffler { "closed port RST response" }, 90208af97b7SRobert Watson { "open port RST response" }, 90308af97b7SRobert Watson { "icmp6 unreach response" } 90409f81a46SBosko Milekic }; 90551508de1SMatthew Dillon 90651508de1SMatthew Dillon /* 90700f21882SSam Leffler * Return ok status if feature disabled or argument out of range. 90851508de1SMatthew Dillon */ 90900f21882SSam Leffler if (icmplim > 0 && (u_int) which < N(rates)) { 91000f21882SSam Leffler struct rate *r = &rates[which]; 91100f21882SSam Leffler int opps = r->curpps; 91251508de1SMatthew Dillon 91300f21882SSam Leffler if (!ppsratecheck(&r->lasttime, &r->curpps, icmplim)) 91400f21882SSam Leffler return -1; /* discard packet */ 91551508de1SMatthew Dillon /* 91600f21882SSam Leffler * If we've dropped below the threshold after having 91700f21882SSam Leffler * rate-limited traffic print the message. This preserves 91800f21882SSam Leffler * the previous behaviour at the expense of added complexity. 91951508de1SMatthew Dillon */ 920069f35d3SSam Leffler if (icmplim_output && opps > icmplim) 92100f21882SSam Leffler printf("Limiting %s from %d to %d packets/sec\n", 922069f35d3SSam Leffler r->type, opps, icmplim); 92351508de1SMatthew Dillon } 92400f21882SSam Leffler return 0; /* okay to send packet */ 92500f21882SSam Leffler #undef N 92651508de1SMatthew Dillon } 927