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 35b8e463e6SBjoern A. Zeeb #include "opt_inet.h" 366a800098SYoshinobu Inoue #include "opt_ipsec.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> 4640fe9effSAndre Oppermann #include <sys/syslog.h> 47df8bae1dSRodney W. Grimes 48df8bae1dSRodney W. Grimes #include <net/if.h> 499494d596SBrooks Davis #include <net/if_types.h> 50df8bae1dSRodney W. Grimes #include <net/route.h> 51eddfbb76SRobert Watson #include <net/vnet.h> 52df8bae1dSRodney W. Grimes 53df8bae1dSRodney W. Grimes #include <netinet/in.h> 5497d8d152SAndre Oppermann #include <netinet/in_pcb.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> 60ef39adf0SAndre Oppermann #include <netinet/ip_options.h> 6197d8d152SAndre Oppermann #include <netinet/tcp.h> 6297d8d152SAndre Oppermann #include <netinet/tcp_var.h> 6397d8d152SAndre Oppermann #include <netinet/tcpip.h> 64df8bae1dSRodney W. Grimes #include <netinet/icmp_var.h> 65df8bae1dSRodney W. Grimes 66b8e463e6SBjoern A. Zeeb #ifdef INET 67b2630c29SGeorge V. Neville-Neil #ifdef IPSEC 68b9234fafSSam Leffler #include <netipsec/ipsec.h> 69b9234fafSSam Leffler #include <netipsec/key.h> 70b9234fafSSam Leffler #endif 71b9234fafSSam Leffler 7272a52a35SJonathan Lemon #include <machine/in_cksum.h> 7372a52a35SJonathan Lemon 74aed55708SRobert Watson #include <security/mac/mac_framework.h> 75b8e463e6SBjoern A. Zeeb #endif /* INET */ 76aed55708SRobert Watson 77df8bae1dSRodney W. Grimes /* 78df8bae1dSRodney W. Grimes * ICMP routines: error generation, receive packet processing, and 79df8bae1dSRodney W. Grimes * routines to turnaround packets back to the originator, and 80df8bae1dSRodney W. Grimes * host table maintenance routines. 81df8bae1dSRodney W. Grimes */ 82b8e463e6SBjoern A. Zeeb static VNET_DEFINE(int, icmplim) = 200; 83b8e463e6SBjoern A. Zeeb #define V_icmplim VNET(icmplim) 84b8e463e6SBjoern A. Zeeb SYSCTL_VNET_INT(_net_inet_icmp, ICMPCTL_ICMPLIM, icmplim, CTLFLAG_RW, 85b8e463e6SBjoern A. Zeeb &VNET_NAME(icmplim), 0, 86b8e463e6SBjoern A. Zeeb "Maximum number of ICMP responses per second"); 87b8e463e6SBjoern A. Zeeb 88b8e463e6SBjoern A. Zeeb static VNET_DEFINE(int, icmplim_output) = 1; 89b8e463e6SBjoern A. Zeeb #define V_icmplim_output VNET(icmplim_output) 90b8e463e6SBjoern A. Zeeb SYSCTL_VNET_INT(_net_inet_icmp, OID_AUTO, icmplim_output, CTLFLAG_RW, 91b8e463e6SBjoern A. Zeeb &VNET_NAME(icmplim_output), 0, 92316efdb3SEitan Adler "Enable logging of ICMP response rate limiting"); 93b8e463e6SBjoern A. Zeeb 94b8e463e6SBjoern A. Zeeb #ifdef INET 95eddfbb76SRobert Watson VNET_DEFINE(struct icmpstat, icmpstat); 96eddfbb76SRobert Watson SYSCTL_VNET_STRUCT(_net_inet_icmp, ICMPCTL_STATS, stats, CTLFLAG_RW, 97eddfbb76SRobert Watson &VNET_NAME(icmpstat), icmpstat, ""); 98eddfbb76SRobert Watson 993e288e62SDimitry Andric static VNET_DEFINE(int, icmpmaskrepl) = 0; 10082cea7e6SBjoern A. Zeeb #define V_icmpmaskrepl VNET(icmpmaskrepl) 101eddfbb76SRobert Watson SYSCTL_VNET_INT(_net_inet_icmp, ICMPCTL_MASKREPL, maskrepl, CTLFLAG_RW, 102eddfbb76SRobert Watson &VNET_NAME(icmpmaskrepl), 0, 1038b615593SMarko Zec "Reply to ICMP Address Mask Request packets."); 10457842a38SMatthew N. Dodd 1053e288e62SDimitry Andric static VNET_DEFINE(u_int, icmpmaskfake) = 0; 10682cea7e6SBjoern A. Zeeb #define V_icmpmaskfake VNET(icmpmaskfake) 107eddfbb76SRobert Watson SYSCTL_VNET_UINT(_net_inet_icmp, OID_AUTO, maskfake, CTLFLAG_RW, 108eddfbb76SRobert Watson &VNET_NAME(icmpmaskfake), 0, 109eddfbb76SRobert Watson "Fake reply to ICMP Address Mask Request packets."); 1100312fbe9SPoul-Henning Kamp 1113c2824b9SAlexander V. Chernikov VNET_DEFINE(int, drop_redirect) = 0; 11218d3153eSDag-Erling Smørgrav 1133e288e62SDimitry Andric static VNET_DEFINE(int, log_redirect) = 0; 11482cea7e6SBjoern A. Zeeb #define V_log_redirect VNET(log_redirect) 115eddfbb76SRobert Watson SYSCTL_VNET_INT(_net_inet_icmp, OID_AUTO, log_redirect, CTLFLAG_RW, 116eddfbb76SRobert Watson &VNET_NAME(log_redirect), 0, 117eddfbb76SRobert Watson "Log ICMP redirects to the console"); 1186c3b5f69SDag-Erling Smørgrav 1193e288e62SDimitry Andric static VNET_DEFINE(char, reply_src[IFNAMSIZ]); 12082cea7e6SBjoern A. Zeeb #define V_reply_src VNET(reply_src) 121eddfbb76SRobert Watson SYSCTL_VNET_STRING(_net_inet_icmp, OID_AUTO, reply_src, CTLFLAG_RW, 122eddfbb76SRobert Watson &VNET_NAME(reply_src), IFNAMSIZ, 1238b615593SMarko Zec "icmp reply source for non-local packets."); 124b74d89bbSAndre Oppermann 1253e288e62SDimitry Andric static VNET_DEFINE(int, icmp_rfi) = 0; 12682cea7e6SBjoern A. Zeeb #define V_icmp_rfi VNET(icmp_rfi) 127eddfbb76SRobert Watson SYSCTL_VNET_INT(_net_inet_icmp, OID_AUTO, reply_from_interface, CTLFLAG_RW, 128eddfbb76SRobert Watson &VNET_NAME(icmp_rfi), 0, 129eddfbb76SRobert Watson "ICMP reply from incoming interface for non-local packets"); 130a0866c8dSAndre Oppermann 1313e288e62SDimitry Andric static VNET_DEFINE(int, icmp_quotelen) = 8; 13282cea7e6SBjoern A. Zeeb #define V_icmp_quotelen VNET(icmp_quotelen) 133eddfbb76SRobert Watson SYSCTL_VNET_INT(_net_inet_icmp, OID_AUTO, quotelen, CTLFLAG_RW, 134eddfbb76SRobert Watson &VNET_NAME(icmp_quotelen), 0, 135eddfbb76SRobert Watson "Number of bytes from original packet to quote in ICMP reply"); 136e875dfb8SAndre Oppermann 1375fce7fc4SMatthew Dillon /* 1385fce7fc4SMatthew Dillon * ICMP broadcast echo sysctl 1395fce7fc4SMatthew Dillon */ 1403e288e62SDimitry Andric static VNET_DEFINE(int, icmpbmcastecho) = 0; 14182cea7e6SBjoern A. Zeeb #define V_icmpbmcastecho VNET(icmpbmcastecho) 142eddfbb76SRobert Watson SYSCTL_VNET_INT(_net_inet_icmp, OID_AUTO, bmcastecho, CTLFLAG_RW, 143eddfbb76SRobert Watson &VNET_NAME(icmpbmcastecho), 0, 144eddfbb76SRobert Watson ""); 1457022ea0aSGarrett Wollman 14651508de1SMatthew Dillon 147df8bae1dSRodney W. Grimes #ifdef ICMPPRINTFS 148df8bae1dSRodney W. Grimes int icmpprintfs = 0; 149df8bae1dSRodney W. Grimes #endif 150df8bae1dSRodney W. Grimes 1514d77a549SAlfred Perlstein static void icmp_reflect(struct mbuf *); 15202c1c707SAndre Oppermann static void icmp_send(struct mbuf *, struct mbuf *); 1530312fbe9SPoul-Henning Kamp 154df8bae1dSRodney W. Grimes extern struct protosw inetsw[]; 155df8bae1dSRodney W. Grimes 1563c2824b9SAlexander V. Chernikov static int 1573c2824b9SAlexander V. Chernikov sysctl_net_icmp_drop_redir(SYSCTL_HANDLER_ARGS) 1583c2824b9SAlexander V. Chernikov { 1593c2824b9SAlexander V. Chernikov int error, new; 1603c2824b9SAlexander V. Chernikov int i; 1613c2824b9SAlexander V. Chernikov struct radix_node_head *rnh; 1623c2824b9SAlexander V. Chernikov 1633c2824b9SAlexander V. Chernikov new = V_drop_redirect; 1643c2824b9SAlexander V. Chernikov error = sysctl_handle_int(oidp, &new, 0, req); 1653c2824b9SAlexander V. Chernikov if (error == 0 && req->newptr) { 1663c2824b9SAlexander V. Chernikov new = (new != 0) ? 1 : 0; 1673c2824b9SAlexander V. Chernikov 1683c2824b9SAlexander V. Chernikov if (new == V_drop_redirect) 1693c2824b9SAlexander V. Chernikov return (0); 1703c2824b9SAlexander V. Chernikov 1713c2824b9SAlexander V. Chernikov for (i = 0; i < rt_numfibs; i++) { 1723c2824b9SAlexander V. Chernikov if ((rnh = rt_tables_get_rnh(i, AF_INET)) == NULL) 1733c2824b9SAlexander V. Chernikov continue; 1743c2824b9SAlexander V. Chernikov RADIX_NODE_HEAD_LOCK(rnh); 1753c2824b9SAlexander V. Chernikov in_setmatchfunc(rnh, new); 1763c2824b9SAlexander V. Chernikov RADIX_NODE_HEAD_UNLOCK(rnh); 1773c2824b9SAlexander V. Chernikov } 1783c2824b9SAlexander V. Chernikov 1793c2824b9SAlexander V. Chernikov V_drop_redirect = new; 1803c2824b9SAlexander V. Chernikov } 1813c2824b9SAlexander V. Chernikov 1823c2824b9SAlexander V. Chernikov return (error); 1833c2824b9SAlexander V. Chernikov } 1843c2824b9SAlexander V. Chernikov 1853c2824b9SAlexander V. Chernikov SYSCTL_VNET_PROC(_net_inet_icmp, OID_AUTO, drop_redirect, 1863c2824b9SAlexander V. Chernikov CTLTYPE_INT|CTLFLAG_RW, 0, 0, 1873c2824b9SAlexander V. Chernikov sysctl_net_icmp_drop_redir, "I", "Ignore ICMP redirects"); 1883c2824b9SAlexander V. Chernikov 189df8bae1dSRodney W. Grimes /* 190315e3e38SRobert Watson * Kernel module interface for updating icmpstat. The argument is an index 191315e3e38SRobert Watson * into icmpstat treated as an array of u_long. While this encodes the 192315e3e38SRobert Watson * general layout of icmpstat into the caller, it doesn't encode its 193315e3e38SRobert Watson * location, so that future changes to add, for example, per-CPU stats 194315e3e38SRobert Watson * support won't cause binary compatibility problems for kernel modules. 195315e3e38SRobert Watson */ 196315e3e38SRobert Watson void 197315e3e38SRobert Watson kmod_icmpstat_inc(int statnum) 198315e3e38SRobert Watson { 199315e3e38SRobert Watson 200315e3e38SRobert Watson (*((u_long *)&V_icmpstat + statnum))++; 201315e3e38SRobert Watson } 202315e3e38SRobert Watson 203315e3e38SRobert Watson /* 204df8bae1dSRodney W. Grimes * Generate an error packet of type error 205df8bae1dSRodney W. Grimes * in response to bad packet ip. 206df8bae1dSRodney W. Grimes */ 207df8bae1dSRodney W. Grimes void 208d685b6eeSLuigi Rizzo icmp_error(struct mbuf *n, int type, int code, uint32_t dest, int mtu) 209df8bae1dSRodney W. Grimes { 210df8bae1dSRodney W. Grimes register struct ip *oip = mtod(n, struct ip *), *nip; 211e86ebebcSAndre Oppermann register unsigned oiphlen = oip->ip_hl << 2; 212df8bae1dSRodney W. Grimes register struct icmp *icp; 213df8bae1dSRodney W. Grimes register struct mbuf *m; 214e86ebebcSAndre Oppermann unsigned icmplen, icmpelen, nlen; 215df8bae1dSRodney W. Grimes 216e86ebebcSAndre Oppermann KASSERT((u_int)type <= ICMP_MAXTYPE, ("%s: illegal ICMP type", __func__)); 217df8bae1dSRodney W. Grimes #ifdef ICMPPRINTFS 218df8bae1dSRodney W. Grimes if (icmpprintfs) 219623ae52eSPoul-Henning Kamp printf("icmp_error(%p, %x, %d)\n", oip, type, code); 220df8bae1dSRodney W. Grimes #endif 221df8bae1dSRodney W. Grimes if (type != ICMP_REDIRECT) 222e27b0c87SRobert Watson ICMPSTAT_INC(icps_error); 223df8bae1dSRodney W. Grimes /* 224e86ebebcSAndre Oppermann * Don't send error: 225e86ebebcSAndre Oppermann * if the original packet was encrypted. 226e86ebebcSAndre Oppermann * if not the first fragment of message. 227e86ebebcSAndre Oppermann * in response to a multicast or broadcast packet. 228e86ebebcSAndre Oppermann * if the old packet protocol was an ICMP error message. 229df8bae1dSRodney W. Grimes */ 230cad1917dSHajimu UMEMOTO if (n->m_flags & M_DECRYPTED) 231cad1917dSHajimu UMEMOTO goto freeit; 2328f134647SGleb Smirnoff if (oip->ip_off & htons(~(IP_MF|IP_DF))) 233df8bae1dSRodney W. Grimes goto freeit; 234e86ebebcSAndre Oppermann if (n->m_flags & (M_BCAST|M_MCAST)) 235e86ebebcSAndre Oppermann goto freeit; 236df8bae1dSRodney W. Grimes if (oip->ip_p == IPPROTO_ICMP && type != ICMP_REDIRECT && 237e86ebebcSAndre Oppermann n->m_len >= oiphlen + ICMP_MINLEN && 238e86ebebcSAndre Oppermann !ICMP_INFOTYPE(((struct icmp *)((caddr_t)oip + oiphlen))->icmp_type)) { 239e27b0c87SRobert Watson ICMPSTAT_INC(icps_oldicmp); 240df8bae1dSRodney W. Grimes goto freeit; 241df8bae1dSRodney W. Grimes } 242e86ebebcSAndre Oppermann /* Drop if IP header plus 8 bytes is not contignous in first mbuf. */ 243e86ebebcSAndre Oppermann if (oiphlen + 8 > n->m_len) 244df8bae1dSRodney W. Grimes goto freeit; 245df8bae1dSRodney W. Grimes /* 246e86ebebcSAndre Oppermann * Calculate length to quote from original packet and 247e86ebebcSAndre Oppermann * prevent the ICMP mbuf from overflowing. 248e86ebebcSAndre Oppermann * Unfortunatly this is non-trivial since ip_forward() 249e86ebebcSAndre Oppermann * sends us truncated packets. 250df8bae1dSRodney W. Grimes */ 251e86ebebcSAndre Oppermann nlen = m_length(n, NULL); 252e86ebebcSAndre Oppermann if (oip->ip_p == IPPROTO_TCP) { 253e86ebebcSAndre Oppermann struct tcphdr *th; 254e86ebebcSAndre Oppermann int tcphlen; 255e86ebebcSAndre Oppermann 256e86ebebcSAndre Oppermann if (oiphlen + sizeof(struct tcphdr) > n->m_len && 257e86ebebcSAndre Oppermann n->m_next == NULL) 258e86ebebcSAndre Oppermann goto stdreply; 259e86ebebcSAndre Oppermann if (n->m_len < oiphlen + sizeof(struct tcphdr) && 260e86ebebcSAndre Oppermann ((n = m_pullup(n, oiphlen + sizeof(struct tcphdr))) == NULL)) 261e86ebebcSAndre Oppermann goto freeit; 262e86ebebcSAndre Oppermann th = (struct tcphdr *)((caddr_t)oip + oiphlen); 263e86ebebcSAndre Oppermann tcphlen = th->th_off << 2; 264e86ebebcSAndre Oppermann if (tcphlen < sizeof(struct tcphdr)) 265e86ebebcSAndre Oppermann goto freeit; 2668f134647SGleb Smirnoff if (ntohs(oip->ip_len) < oiphlen + tcphlen) 267e86ebebcSAndre Oppermann goto freeit; 268e86ebebcSAndre Oppermann if (oiphlen + tcphlen > n->m_len && n->m_next == NULL) 269e86ebebcSAndre Oppermann goto stdreply; 270e86ebebcSAndre Oppermann if (n->m_len < oiphlen + tcphlen && 271e86ebebcSAndre Oppermann ((n = m_pullup(n, oiphlen + tcphlen)) == NULL)) 272e86ebebcSAndre Oppermann goto freeit; 2738f134647SGleb Smirnoff icmpelen = max(tcphlen, min(V_icmp_quotelen, 2748f134647SGleb Smirnoff ntohs(oip->ip_len) - oiphlen)); 275e86ebebcSAndre Oppermann } else 2768f134647SGleb Smirnoff stdreply: icmpelen = max(8, min(V_icmp_quotelen, ntohs(oip->ip_len) - oiphlen)); 277e86ebebcSAndre Oppermann 278e86ebebcSAndre Oppermann icmplen = min(oiphlen + icmpelen, nlen); 279e86ebebcSAndre Oppermann if (icmplen < sizeof(struct ip)) 280e86ebebcSAndre Oppermann goto freeit; 281e86ebebcSAndre Oppermann 282e86ebebcSAndre Oppermann if (MHLEN > sizeof(struct ip) + ICMP_MINLEN + icmplen) 283*eb1b1807SGleb Smirnoff m = m_gethdr(M_NOWAIT, MT_DATA); 284e86ebebcSAndre Oppermann else 285*eb1b1807SGleb Smirnoff m = m_getcl(M_NOWAIT, MT_DATA, M_PKTHDR); 286df8bae1dSRodney W. Grimes if (m == NULL) 287df8bae1dSRodney W. Grimes goto freeit; 2880070e096SRobert Watson #ifdef MAC 289a13e21f7SRobert Watson mac_netinet_icmp_reply(n, m); 2900070e096SRobert Watson #endif 291e86ebebcSAndre Oppermann icmplen = min(icmplen, M_TRAILINGSPACE(m) - sizeof(struct ip) - ICMP_MINLEN); 292e86ebebcSAndre Oppermann m_align(m, ICMP_MINLEN + icmplen); 293e86ebebcSAndre Oppermann m->m_len = ICMP_MINLEN + icmplen; 2946b773dffSAndre Oppermann 2958b07e49aSJulian Elischer /* XXX MRT make the outgoing packet use the same FIB 2968b07e49aSJulian Elischer * that was associated with the incoming packet 2978b07e49aSJulian Elischer */ 2988b07e49aSJulian Elischer M_SETFIB(m, M_GETFIB(n)); 299df8bae1dSRodney W. Grimes icp = mtod(m, struct icmp *); 300e27b0c87SRobert Watson ICMPSTAT_INC(icps_outhist[type]); 301df8bae1dSRodney W. Grimes icp->icmp_type = type; 302df8bae1dSRodney W. Grimes if (type == ICMP_REDIRECT) 303df8bae1dSRodney W. Grimes icp->icmp_gwaddr.s_addr = dest; 304df8bae1dSRodney W. Grimes else { 305df8bae1dSRodney W. Grimes icp->icmp_void = 0; 306df8bae1dSRodney W. Grimes /* 307df8bae1dSRodney W. Grimes * The following assignments assume an overlay with the 308e86ebebcSAndre Oppermann * just zeroed icmp_void field. 309df8bae1dSRodney W. Grimes */ 310df8bae1dSRodney W. Grimes if (type == ICMP_PARAMPROB) { 311df8bae1dSRodney W. Grimes icp->icmp_pptr = code; 312df8bae1dSRodney W. Grimes code = 0; 313df8bae1dSRodney W. Grimes } else if (type == ICMP_UNREACH && 314c773494eSAndre Oppermann code == ICMP_UNREACH_NEEDFRAG && mtu) { 315c773494eSAndre Oppermann icp->icmp_nextmtu = htons(mtu); 316df8bae1dSRodney W. Grimes } 317df8bae1dSRodney W. Grimes } 318df8bae1dSRodney W. Grimes icp->icmp_code = code; 31904287599SRuslan Ermilov 32004287599SRuslan Ermilov /* 321e86ebebcSAndre Oppermann * Copy the quotation into ICMP message and 322e86ebebcSAndre Oppermann * convert quoted IP header back to network representation. 32304287599SRuslan Ermilov */ 324e86ebebcSAndre Oppermann m_copydata(n, 0, icmplen, (caddr_t)&icp->icmp_ip); 325e86ebebcSAndre Oppermann nip = &icp->icmp_ip; 326df8bae1dSRodney W. Grimes 327df8bae1dSRodney W. Grimes /* 328e86ebebcSAndre Oppermann * Set up ICMP message mbuf and copy old IP header (without options 329e86ebebcSAndre Oppermann * in front of ICMP message. 330c550f220SMax Laier * If the original mbuf was meant to bypass the firewall, the error 331c550f220SMax Laier * reply should bypass as well. 332c550f220SMax Laier */ 333c550f220SMax Laier m->m_flags |= n->m_flags & M_SKIP_FIREWALL; 334df8bae1dSRodney W. Grimes m->m_data -= sizeof(struct ip); 335df8bae1dSRodney W. Grimes m->m_len += sizeof(struct ip); 336df8bae1dSRodney W. Grimes m->m_pkthdr.len = m->m_len; 337df8bae1dSRodney W. Grimes m->m_pkthdr.rcvif = n->m_pkthdr.rcvif; 338df8bae1dSRodney W. Grimes nip = mtod(m, struct ip *); 339df8bae1dSRodney W. Grimes bcopy((caddr_t)oip, (caddr_t)nip, sizeof(struct ip)); 3408f134647SGleb Smirnoff nip->ip_len = htons(m->m_len); 34153be11f6SPoul-Henning Kamp nip->ip_v = IPVERSION; 34253be11f6SPoul-Henning Kamp nip->ip_hl = 5; 343df8bae1dSRodney W. Grimes nip->ip_p = IPPROTO_ICMP; 344df8bae1dSRodney W. Grimes nip->ip_tos = 0; 345df8bae1dSRodney W. Grimes icmp_reflect(m); 346df8bae1dSRodney W. Grimes 347df8bae1dSRodney W. Grimes freeit: 348df8bae1dSRodney W. Grimes m_freem(n); 349df8bae1dSRodney W. Grimes } 350df8bae1dSRodney W. Grimes 351df8bae1dSRodney W. Grimes /* 352df8bae1dSRodney W. Grimes * Process a received ICMP message. 353df8bae1dSRodney W. Grimes */ 354df8bae1dSRodney W. Grimes void 355f2565d68SRobert Watson icmp_input(struct mbuf *m, int off) 356df8bae1dSRodney W. Grimes { 35716d6c90fSAndre Oppermann struct icmp *icp; 358df8bae1dSRodney W. Grimes struct in_ifaddr *ia; 35916d6c90fSAndre Oppermann struct ip *ip = mtod(m, struct ip *); 36016d6c90fSAndre Oppermann struct sockaddr_in icmpsrc, icmpdst, icmpgw; 36116d6c90fSAndre Oppermann int hlen = off; 3628ad458a4SGleb Smirnoff int icmplen = ntohs(ip->ip_len) - off; 36316d6c90fSAndre Oppermann int i, code; 3644d77a549SAlfred Perlstein void (*ctlfunc)(int, struct sockaddr *, void *); 3658b07e49aSJulian Elischer int fibnum; 366df8bae1dSRodney W. Grimes 367df8bae1dSRodney W. Grimes /* 368df8bae1dSRodney W. Grimes * Locate icmp structure in mbuf, and check 369df8bae1dSRodney W. Grimes * that not corrupted and of at least minimum length. 370df8bae1dSRodney W. Grimes */ 371df8bae1dSRodney W. Grimes #ifdef ICMPPRINTFS 3722b758395SGarrett Wollman if (icmpprintfs) { 3732b758395SGarrett Wollman char buf[4 * sizeof "123"]; 3742b758395SGarrett Wollman strcpy(buf, inet_ntoa(ip->ip_src)); 3752b758395SGarrett Wollman printf("icmp_input from %s to %s, len %d\n", 3762b758395SGarrett Wollman buf, inet_ntoa(ip->ip_dst), icmplen); 3772b758395SGarrett Wollman } 378df8bae1dSRodney W. Grimes #endif 379df8bae1dSRodney W. Grimes if (icmplen < ICMP_MINLEN) { 380e27b0c87SRobert Watson ICMPSTAT_INC(icps_tooshort); 381df8bae1dSRodney W. Grimes goto freeit; 382df8bae1dSRodney W. Grimes } 383df8bae1dSRodney W. Grimes i = hlen + min(icmplen, ICMP_ADVLENMIN); 384852da713SBjoern A. Zeeb if (m->m_len < i && (m = m_pullup(m, i)) == NULL) { 385e27b0c87SRobert Watson ICMPSTAT_INC(icps_tooshort); 386df8bae1dSRodney W. Grimes return; 387df8bae1dSRodney W. Grimes } 388df8bae1dSRodney W. Grimes ip = mtod(m, struct ip *); 389df8bae1dSRodney W. Grimes m->m_len -= hlen; 390df8bae1dSRodney W. Grimes m->m_data += hlen; 391df8bae1dSRodney W. Grimes icp = mtod(m, struct icmp *); 392df8bae1dSRodney W. Grimes if (in_cksum(m, icmplen)) { 393e27b0c87SRobert Watson ICMPSTAT_INC(icps_checksum); 394df8bae1dSRodney W. Grimes goto freeit; 395df8bae1dSRodney W. Grimes } 396df8bae1dSRodney W. Grimes m->m_len += hlen; 397df8bae1dSRodney W. Grimes m->m_data -= hlen; 398df8bae1dSRodney W. Grimes 3996a800098SYoshinobu Inoue if (m->m_pkthdr.rcvif && m->m_pkthdr.rcvif->if_type == IFT_FAITH) { 4006a800098SYoshinobu Inoue /* 4016a800098SYoshinobu Inoue * Deliver very specific ICMP type only. 4026a800098SYoshinobu Inoue */ 4036a800098SYoshinobu Inoue switch (icp->icmp_type) { 4046a800098SYoshinobu Inoue case ICMP_UNREACH: 4056a800098SYoshinobu Inoue case ICMP_TIMXCEED: 4066a800098SYoshinobu Inoue break; 4076a800098SYoshinobu Inoue default: 4086a800098SYoshinobu Inoue goto freeit; 4096a800098SYoshinobu Inoue } 4106a800098SYoshinobu Inoue } 4116a800098SYoshinobu Inoue 412df8bae1dSRodney W. Grimes #ifdef ICMPPRINTFS 413df8bae1dSRodney W. Grimes if (icmpprintfs) 414df8bae1dSRodney W. Grimes printf("icmp_input, type %d code %d\n", icp->icmp_type, 415df8bae1dSRodney W. Grimes icp->icmp_code); 416df8bae1dSRodney W. Grimes #endif 4175b7ee6edSGarrett Wollman 4185b7ee6edSGarrett Wollman /* 4195b7ee6edSGarrett Wollman * Message type specific processing. 4205b7ee6edSGarrett Wollman */ 421df8bae1dSRodney W. Grimes if (icp->icmp_type > ICMP_MAXTYPE) 422df8bae1dSRodney W. Grimes goto raw; 42316d6c90fSAndre Oppermann 42416d6c90fSAndre Oppermann /* Initialize */ 42516d6c90fSAndre Oppermann bzero(&icmpsrc, sizeof(icmpsrc)); 42616d6c90fSAndre Oppermann icmpsrc.sin_len = sizeof(struct sockaddr_in); 42716d6c90fSAndre Oppermann icmpsrc.sin_family = AF_INET; 42816d6c90fSAndre Oppermann bzero(&icmpdst, sizeof(icmpdst)); 42916d6c90fSAndre Oppermann icmpdst.sin_len = sizeof(struct sockaddr_in); 43016d6c90fSAndre Oppermann icmpdst.sin_family = AF_INET; 43116d6c90fSAndre Oppermann bzero(&icmpgw, sizeof(icmpgw)); 43216d6c90fSAndre Oppermann icmpgw.sin_len = sizeof(struct sockaddr_in); 43316d6c90fSAndre Oppermann icmpgw.sin_family = AF_INET; 43416d6c90fSAndre Oppermann 435e27b0c87SRobert Watson ICMPSTAT_INC(icps_inhist[icp->icmp_type]); 436df8bae1dSRodney W. Grimes code = icp->icmp_code; 437df8bae1dSRodney W. Grimes switch (icp->icmp_type) { 438df8bae1dSRodney W. Grimes 439df8bae1dSRodney W. Grimes case ICMP_UNREACH: 440df8bae1dSRodney W. Grimes switch (code) { 441df8bae1dSRodney W. Grimes case ICMP_UNREACH_NET: 442df8bae1dSRodney W. Grimes case ICMP_UNREACH_HOST: 443df8bae1dSRodney W. Grimes case ICMP_UNREACH_SRCFAIL: 444e4bb5b05SJonathan Lemon case ICMP_UNREACH_NET_UNKNOWN: 445e4bb5b05SJonathan Lemon case ICMP_UNREACH_HOST_UNKNOWN: 446e4bb5b05SJonathan Lemon case ICMP_UNREACH_ISOLATED: 447e4bb5b05SJonathan Lemon case ICMP_UNREACH_TOSNET: 448e4bb5b05SJonathan Lemon case ICMP_UNREACH_TOSHOST: 449e4bb5b05SJonathan Lemon case ICMP_UNREACH_HOST_PRECEDENCE: 450e4bb5b05SJonathan Lemon case ICMP_UNREACH_PRECEDENCE_CUTOFF: 451e4bb5b05SJonathan Lemon code = PRC_UNREACH_NET; 452df8bae1dSRodney W. Grimes break; 453df8bae1dSRodney W. Grimes 454df8bae1dSRodney W. Grimes case ICMP_UNREACH_NEEDFRAG: 455df8bae1dSRodney W. Grimes code = PRC_MSGSIZE; 456df8bae1dSRodney W. Grimes break; 457df8bae1dSRodney W. Grimes 458e4bb5b05SJonathan Lemon /* 459e4bb5b05SJonathan Lemon * RFC 1122, Sections 3.2.2.1 and 4.2.3.9. 460e4bb5b05SJonathan Lemon * Treat subcodes 2,3 as immediate RST 461e4bb5b05SJonathan Lemon */ 462e4bb5b05SJonathan Lemon case ICMP_UNREACH_PROTOCOL: 463e4bb5b05SJonathan Lemon case ICMP_UNREACH_PORT: 464b77d155dSJesper Skriver code = PRC_UNREACH_PORT; 465b11d7a4aSPoul-Henning Kamp break; 46690fcbbd6SPoul-Henning Kamp 46790fcbbd6SPoul-Henning Kamp case ICMP_UNREACH_NET_PROHIB: 46890fcbbd6SPoul-Henning Kamp case ICMP_UNREACH_HOST_PROHIB: 4699c4b2574SPaul Traina case ICMP_UNREACH_FILTER_PROHIB: 47090fcbbd6SPoul-Henning Kamp code = PRC_UNREACH_ADMIN_PROHIB; 471b11d7a4aSPoul-Henning Kamp break; 472b11d7a4aSPoul-Henning Kamp 473df8bae1dSRodney W. Grimes default: 474df8bae1dSRodney W. Grimes goto badcode; 475df8bae1dSRodney W. Grimes } 476df8bae1dSRodney W. Grimes goto deliver; 477df8bae1dSRodney W. Grimes 478df8bae1dSRodney W. Grimes case ICMP_TIMXCEED: 479df8bae1dSRodney W. Grimes if (code > 1) 480df8bae1dSRodney W. Grimes goto badcode; 481df8bae1dSRodney W. Grimes code += PRC_TIMXCEED_INTRANS; 482df8bae1dSRodney W. Grimes goto deliver; 483df8bae1dSRodney W. Grimes 484df8bae1dSRodney W. Grimes case ICMP_PARAMPROB: 485df8bae1dSRodney W. Grimes if (code > 1) 486df8bae1dSRodney W. Grimes goto badcode; 487df8bae1dSRodney W. Grimes code = PRC_PARAMPROB; 488df8bae1dSRodney W. Grimes goto deliver; 489df8bae1dSRodney W. Grimes 490df8bae1dSRodney W. Grimes case ICMP_SOURCEQUENCH: 491df8bae1dSRodney W. Grimes if (code) 492df8bae1dSRodney W. Grimes goto badcode; 493df8bae1dSRodney W. Grimes code = PRC_QUENCH; 494df8bae1dSRodney W. Grimes deliver: 495df8bae1dSRodney W. Grimes /* 496df8bae1dSRodney W. Grimes * Problem with datagram; advise higher level routines. 497df8bae1dSRodney W. Grimes */ 498df8bae1dSRodney W. Grimes if (icmplen < ICMP_ADVLENMIN || icmplen < ICMP_ADVLEN(icp) || 49953be11f6SPoul-Henning Kamp icp->icmp_ip.ip_hl < (sizeof(struct ip) >> 2)) { 500e27b0c87SRobert Watson ICMPSTAT_INC(icps_badlen); 501df8bae1dSRodney W. Grimes goto freeit; 502df8bae1dSRodney W. Grimes } 5035b7ee6edSGarrett Wollman /* Discard ICMP's in response to multicast packets */ 5045b7ee6edSGarrett Wollman if (IN_MULTICAST(ntohl(icp->icmp_ip.ip_dst.s_addr))) 5055b7ee6edSGarrett Wollman goto badcode; 506df8bae1dSRodney W. Grimes #ifdef ICMPPRINTFS 507df8bae1dSRodney W. Grimes if (icmpprintfs) 508df8bae1dSRodney W. Grimes printf("deliver to protocol %d\n", icp->icmp_ip.ip_p); 509df8bae1dSRodney W. Grimes #endif 510df8bae1dSRodney W. Grimes icmpsrc.sin_addr = icp->icmp_ip.ip_dst; 5116a800098SYoshinobu Inoue /* 5126a800098SYoshinobu Inoue * XXX if the packet contains [IPv4 AH TCP], we can't make a 5136a800098SYoshinobu Inoue * notification to TCP layer. 5146a800098SYoshinobu Inoue */ 515623ae52eSPoul-Henning Kamp ctlfunc = inetsw[ip_protox[icp->icmp_ip.ip_p]].pr_ctlinput; 516623ae52eSPoul-Henning Kamp if (ctlfunc) 517df8bae1dSRodney W. Grimes (*ctlfunc)(code, (struct sockaddr *)&icmpsrc, 518b62d102cSBruce Evans (void *)&icp->icmp_ip); 519df8bae1dSRodney W. Grimes break; 520df8bae1dSRodney W. Grimes 521df8bae1dSRodney W. Grimes badcode: 522e27b0c87SRobert Watson ICMPSTAT_INC(icps_badcode); 523df8bae1dSRodney W. Grimes break; 524df8bae1dSRodney W. Grimes 525df8bae1dSRodney W. Grimes case ICMP_ECHO: 5268b615593SMarko Zec if (!V_icmpbmcastecho 527d311884fSDavid Greenman && (m->m_flags & (M_MCAST | M_BCAST)) != 0) { 528e27b0c87SRobert Watson ICMPSTAT_INC(icps_bmcastecho); 5297022ea0aSGarrett Wollman break; 5307022ea0aSGarrett Wollman } 531df8bae1dSRodney W. Grimes icp->icmp_type = ICMP_ECHOREPLY; 532a57815efSBosko Milekic if (badport_bandlim(BANDLIM_ICMP_ECHO) < 0) 53309f81a46SBosko Milekic goto freeit; 53409f81a46SBosko Milekic else 535df8bae1dSRodney W. Grimes goto reflect; 536df8bae1dSRodney W. Grimes 537df8bae1dSRodney W. Grimes case ICMP_TSTAMP: 5388b615593SMarko Zec if (!V_icmpbmcastecho 539d311884fSDavid Greenman && (m->m_flags & (M_MCAST | M_BCAST)) != 0) { 540e27b0c87SRobert Watson ICMPSTAT_INC(icps_bmcasttstamp); 541fe0fb8abSGarrett Wollman break; 542fe0fb8abSGarrett Wollman } 543df8bae1dSRodney W. Grimes if (icmplen < ICMP_TSLEN) { 544e27b0c87SRobert Watson ICMPSTAT_INC(icps_badlen); 545df8bae1dSRodney W. Grimes break; 546df8bae1dSRodney W. Grimes } 547df8bae1dSRodney W. Grimes icp->icmp_type = ICMP_TSTAMPREPLY; 548df8bae1dSRodney W. Grimes icp->icmp_rtime = iptime(); 549df8bae1dSRodney W. Grimes icp->icmp_ttime = icp->icmp_rtime; /* bogus, do later! */ 550a57815efSBosko Milekic if (badport_bandlim(BANDLIM_ICMP_TSTAMP) < 0) 55109f81a46SBosko Milekic goto freeit; 55209f81a46SBosko Milekic else 553df8bae1dSRodney W. Grimes goto reflect; 554df8bae1dSRodney W. Grimes 555df8bae1dSRodney W. Grimes case ICMP_MASKREQ: 5568b615593SMarko Zec if (V_icmpmaskrepl == 0) 557df8bae1dSRodney W. Grimes break; 558df8bae1dSRodney W. Grimes /* 559df8bae1dSRodney W. Grimes * We are not able to respond with all ones broadcast 560df8bae1dSRodney W. Grimes * unless we receive it over a point-to-point interface. 561df8bae1dSRodney W. Grimes */ 562df8bae1dSRodney W. Grimes if (icmplen < ICMP_MASKLEN) 563df8bae1dSRodney W. Grimes break; 564df8bae1dSRodney W. Grimes switch (ip->ip_dst.s_addr) { 565df8bae1dSRodney W. Grimes 566df8bae1dSRodney W. Grimes case INADDR_BROADCAST: 567df8bae1dSRodney W. Grimes case INADDR_ANY: 568df8bae1dSRodney W. Grimes icmpdst.sin_addr = ip->ip_src; 569df8bae1dSRodney W. Grimes break; 570df8bae1dSRodney W. Grimes 571df8bae1dSRodney W. Grimes default: 572df8bae1dSRodney W. Grimes icmpdst.sin_addr = ip->ip_dst; 573df8bae1dSRodney W. Grimes } 574df8bae1dSRodney W. Grimes ia = (struct in_ifaddr *)ifaof_ifpforaddr( 575df8bae1dSRodney W. Grimes (struct sockaddr *)&icmpdst, m->m_pkthdr.rcvif); 5768c0fec80SRobert Watson if (ia == NULL) 577df8bae1dSRodney W. Grimes break; 5788c0fec80SRobert Watson if (ia->ia_ifp == NULL) { 5798c0fec80SRobert Watson ifa_free(&ia->ia_ifa); 5807e6f7714SPoul-Henning Kamp break; 5818c0fec80SRobert Watson } 582df8bae1dSRodney W. Grimes icp->icmp_type = ICMP_MASKREPLY; 5838b615593SMarko Zec if (V_icmpmaskfake == 0) 584df8bae1dSRodney W. Grimes icp->icmp_mask = ia->ia_sockmask.sin_addr.s_addr; 58557842a38SMatthew N. Dodd else 5868b615593SMarko Zec icp->icmp_mask = V_icmpmaskfake; 587df8bae1dSRodney W. Grimes if (ip->ip_src.s_addr == 0) { 588df8bae1dSRodney W. Grimes if (ia->ia_ifp->if_flags & IFF_BROADCAST) 589df8bae1dSRodney W. Grimes ip->ip_src = satosin(&ia->ia_broadaddr)->sin_addr; 590df8bae1dSRodney W. Grimes else if (ia->ia_ifp->if_flags & IFF_POINTOPOINT) 591df8bae1dSRodney W. Grimes ip->ip_src = satosin(&ia->ia_dstaddr)->sin_addr; 592df8bae1dSRodney W. Grimes } 5938c0fec80SRobert Watson ifa_free(&ia->ia_ifa); 594df8bae1dSRodney W. Grimes reflect: 595e27b0c87SRobert Watson ICMPSTAT_INC(icps_reflect); 596e27b0c87SRobert Watson ICMPSTAT_INC(icps_outhist[icp->icmp_type]); 597df8bae1dSRodney W. Grimes icmp_reflect(m); 598df8bae1dSRodney W. Grimes return; 599df8bae1dSRodney W. Grimes 600df8bae1dSRodney W. Grimes case ICMP_REDIRECT: 6018b615593SMarko Zec if (V_log_redirect) { 60218d3153eSDag-Erling Smørgrav u_long src, dst, gw; 60318d3153eSDag-Erling Smørgrav 60418d3153eSDag-Erling Smørgrav src = ntohl(ip->ip_src.s_addr); 60518d3153eSDag-Erling Smørgrav dst = ntohl(icp->icmp_ip.ip_dst.s_addr); 60618d3153eSDag-Erling Smørgrav gw = ntohl(icp->icmp_gwaddr.s_addr); 60718d3153eSDag-Erling Smørgrav printf("icmp redirect from %d.%d.%d.%d: " 60818d3153eSDag-Erling Smørgrav "%d.%d.%d.%d => %d.%d.%d.%d\n", 60918d3153eSDag-Erling Smørgrav (int)(src >> 24), (int)((src >> 16) & 0xff), 61018d3153eSDag-Erling Smørgrav (int)((src >> 8) & 0xff), (int)(src & 0xff), 61118d3153eSDag-Erling Smørgrav (int)(dst >> 24), (int)((dst >> 16) & 0xff), 61218d3153eSDag-Erling Smørgrav (int)((dst >> 8) & 0xff), (int)(dst & 0xff), 61318d3153eSDag-Erling Smørgrav (int)(gw >> 24), (int)((gw >> 16) & 0xff), 61418d3153eSDag-Erling Smørgrav (int)((gw >> 8) & 0xff), (int)(gw & 0xff)); 61518d3153eSDag-Erling Smørgrav } 61687c3bd27SAndre Oppermann /* 61787c3bd27SAndre Oppermann * RFC1812 says we must ignore ICMP redirects if we 61887c3bd27SAndre Oppermann * are acting as router. 61987c3bd27SAndre Oppermann */ 6208b615593SMarko Zec if (V_drop_redirect || V_ipforwarding) 62118d3153eSDag-Erling Smørgrav break; 622df8bae1dSRodney W. Grimes if (code > 3) 623df8bae1dSRodney W. Grimes goto badcode; 624df8bae1dSRodney W. Grimes if (icmplen < ICMP_ADVLENMIN || icmplen < ICMP_ADVLEN(icp) || 62553be11f6SPoul-Henning Kamp icp->icmp_ip.ip_hl < (sizeof(struct ip) >> 2)) { 626e27b0c87SRobert Watson ICMPSTAT_INC(icps_badlen); 627df8bae1dSRodney W. Grimes break; 628df8bae1dSRodney W. Grimes } 629df8bae1dSRodney W. Grimes /* 630df8bae1dSRodney W. Grimes * Short circuit routing redirects to force 631df8bae1dSRodney W. Grimes * immediate change in the kernel's routing 632df8bae1dSRodney W. Grimes * tables. The message is also handed to anyone 633df8bae1dSRodney W. Grimes * listening on a raw socket (e.g. the routing 634df8bae1dSRodney W. Grimes * daemon for use in updating its tables). 635df8bae1dSRodney W. Grimes */ 636df8bae1dSRodney W. Grimes icmpgw.sin_addr = ip->ip_src; 637df8bae1dSRodney W. Grimes icmpdst.sin_addr = icp->icmp_gwaddr; 638df8bae1dSRodney W. Grimes #ifdef ICMPPRINTFS 6392b758395SGarrett Wollman if (icmpprintfs) { 6402b758395SGarrett Wollman char buf[4 * sizeof "123"]; 6412b758395SGarrett Wollman strcpy(buf, inet_ntoa(icp->icmp_ip.ip_dst)); 6422b758395SGarrett Wollman 6432b758395SGarrett Wollman printf("redirect dst %s to %s\n", 6442b758395SGarrett Wollman buf, inet_ntoa(icp->icmp_gwaddr)); 6452b758395SGarrett Wollman } 646df8bae1dSRodney W. Grimes #endif 647df8bae1dSRodney W. Grimes icmpsrc.sin_addr = icp->icmp_ip.ip_dst; 6488b07e49aSJulian Elischer for ( fibnum = 0; fibnum < rt_numfibs; fibnum++) { 6498b07e49aSJulian Elischer in_rtredirect((struct sockaddr *)&icmpsrc, 650df8bae1dSRodney W. Grimes (struct sockaddr *)&icmpdst, 651df8bae1dSRodney W. Grimes (struct sockaddr *)0, RTF_GATEWAY | RTF_HOST, 6528b07e49aSJulian Elischer (struct sockaddr *)&icmpgw, fibnum); 6538b07e49aSJulian Elischer } 654df8bae1dSRodney W. Grimes pfctlinput(PRC_REDIRECT_HOST, (struct sockaddr *)&icmpsrc); 655b2630c29SGeorge V. Neville-Neil #ifdef IPSEC 6566a800098SYoshinobu Inoue key_sa_routechange((struct sockaddr *)&icmpsrc); 6576a800098SYoshinobu Inoue #endif 658df8bae1dSRodney W. Grimes break; 659df8bae1dSRodney W. Grimes 660df8bae1dSRodney W. Grimes /* 661df8bae1dSRodney W. Grimes * No kernel processing for the following; 662df8bae1dSRodney W. Grimes * just fall through to send to raw listener. 663df8bae1dSRodney W. Grimes */ 664df8bae1dSRodney W. Grimes case ICMP_ECHOREPLY: 665df8bae1dSRodney W. Grimes case ICMP_ROUTERADVERT: 666df8bae1dSRodney W. Grimes case ICMP_ROUTERSOLICIT: 667df8bae1dSRodney W. Grimes case ICMP_TSTAMPREPLY: 668df8bae1dSRodney W. Grimes case ICMP_IREQREPLY: 669df8bae1dSRodney W. Grimes case ICMP_MASKREPLY: 670df8bae1dSRodney W. Grimes default: 671df8bae1dSRodney W. Grimes break; 672df8bae1dSRodney W. Grimes } 673df8bae1dSRodney W. Grimes 674df8bae1dSRodney W. Grimes raw: 675f0ffb944SJulian Elischer rip_input(m, off); 676df8bae1dSRodney W. Grimes return; 677df8bae1dSRodney W. Grimes 678df8bae1dSRodney W. Grimes freeit: 679df8bae1dSRodney W. Grimes m_freem(m); 680df8bae1dSRodney W. Grimes } 681df8bae1dSRodney W. Grimes 682df8bae1dSRodney W. Grimes /* 683df8bae1dSRodney W. Grimes * Reflect the ip packet back to the source 684df8bae1dSRodney W. Grimes */ 6850312fbe9SPoul-Henning Kamp static void 686f2565d68SRobert Watson icmp_reflect(struct mbuf *m) 687df8bae1dSRodney W. Grimes { 688ca925d9cSJonathan Lemon struct ip *ip = mtod(m, struct ip *); 689ca925d9cSJonathan Lemon struct ifaddr *ifa; 69033c4f96dSRobert Watson struct ifnet *ifp; 691ca925d9cSJonathan Lemon struct in_ifaddr *ia; 692df8bae1dSRodney W. Grimes struct in_addr t; 693b5e8ce9fSBruce Evans struct mbuf *opts = 0; 69453be11f6SPoul-Henning Kamp int optlen = (ip->ip_hl << 2) - sizeof(struct ip); 695df8bae1dSRodney W. Grimes 6966b9ff6b7SGeorge V. Neville-Neil if (IN_MULTICAST(ntohl(ip->ip_src.s_addr)) || 6976b9ff6b7SGeorge V. Neville-Neil IN_EXPERIMENTAL(ntohl(ip->ip_src.s_addr)) || 6986b9ff6b7SGeorge V. Neville-Neil IN_ZERONET(ntohl(ip->ip_src.s_addr)) ) { 699df8bae1dSRodney W. Grimes m_freem(m); /* Bad return address */ 700e27b0c87SRobert Watson ICMPSTAT_INC(icps_badaddr); 701df8bae1dSRodney W. Grimes goto done; /* Ip_output() will check for broadcast */ 702df8bae1dSRodney W. Grimes } 7036b9ff6b7SGeorge V. Neville-Neil 704df8bae1dSRodney W. Grimes t = ip->ip_dst; 705df8bae1dSRodney W. Grimes ip->ip_dst = ip->ip_src; 7061488eac8SAndre Oppermann 707df8bae1dSRodney W. Grimes /* 7081488eac8SAndre Oppermann * Source selection for ICMP replies: 7091488eac8SAndre Oppermann * 7101488eac8SAndre Oppermann * If the incoming packet was addressed directly to one of our 7111488eac8SAndre Oppermann * own addresses, use dst as the src for the reply. 712df8bae1dSRodney W. Grimes */ 7132d9cfabaSRobert Watson IN_IFADDR_RLOCK(); 71433c4f96dSRobert Watson LIST_FOREACH(ia, INADDR_HASH(t.s_addr), ia_hash) { 71533c4f96dSRobert Watson if (t.s_addr == IA_SIN(ia)->sin_addr.s_addr) { 71633c4f96dSRobert Watson t = IA_SIN(ia)->sin_addr; 7172d9cfabaSRobert Watson IN_IFADDR_RUNLOCK(); 718ca925d9cSJonathan Lemon goto match; 71933c4f96dSRobert Watson } 72033c4f96dSRobert Watson } 7212d9cfabaSRobert Watson IN_IFADDR_RUNLOCK(); 7222d9cfabaSRobert Watson 7231488eac8SAndre Oppermann /* 7241488eac8SAndre Oppermann * If the incoming packet was addressed to one of our broadcast 7251488eac8SAndre Oppermann * addresses, use the first non-broadcast address which corresponds 7261488eac8SAndre Oppermann * to the incoming interface. 7271488eac8SAndre Oppermann */ 72833c4f96dSRobert Watson ifp = m->m_pkthdr.rcvif; 72933c4f96dSRobert Watson if (ifp != NULL && ifp->if_flags & IFF_BROADCAST) { 730137f91e8SJohn Baldwin IF_ADDR_RLOCK(ifp); 73133c4f96dSRobert Watson TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) { 732ca925d9cSJonathan Lemon if (ifa->ifa_addr->sa_family != AF_INET) 733ca925d9cSJonathan Lemon continue; 734ca925d9cSJonathan Lemon ia = ifatoia(ifa); 735ca925d9cSJonathan Lemon if (satosin(&ia->ia_broadaddr)->sin_addr.s_addr == 73633c4f96dSRobert Watson t.s_addr) { 73733c4f96dSRobert Watson t = IA_SIN(ia)->sin_addr; 738137f91e8SJohn Baldwin IF_ADDR_RUNLOCK(ifp); 739ca925d9cSJonathan Lemon goto match; 740df8bae1dSRodney W. Grimes } 741ca925d9cSJonathan Lemon } 742137f91e8SJohn Baldwin IF_ADDR_RUNLOCK(ifp); 74333c4f96dSRobert Watson } 7441488eac8SAndre Oppermann /* 745a0866c8dSAndre Oppermann * If the packet was transiting through us, use the address of 746a0866c8dSAndre Oppermann * the interface the packet came through in. If that interface 747a0866c8dSAndre Oppermann * doesn't have a suitable IP address, the normal selection 748a0866c8dSAndre Oppermann * criteria apply. 749a0866c8dSAndre Oppermann */ 75033c4f96dSRobert Watson if (V_icmp_rfi && ifp != NULL) { 751137f91e8SJohn Baldwin IF_ADDR_RLOCK(ifp); 75233c4f96dSRobert Watson TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) { 753a0866c8dSAndre Oppermann if (ifa->ifa_addr->sa_family != AF_INET) 754a0866c8dSAndre Oppermann continue; 755a0866c8dSAndre Oppermann ia = ifatoia(ifa); 75633c4f96dSRobert Watson t = IA_SIN(ia)->sin_addr; 757137f91e8SJohn Baldwin IF_ADDR_RUNLOCK(ifp); 758a0866c8dSAndre Oppermann goto match; 759a0866c8dSAndre Oppermann } 760137f91e8SJohn Baldwin IF_ADDR_RUNLOCK(ifp); 761a0866c8dSAndre Oppermann } 762a0866c8dSAndre Oppermann /* 763b74d89bbSAndre Oppermann * If the incoming packet was not addressed directly to us, use 764b74d89bbSAndre Oppermann * designated interface for icmp replies specified by sysctl 765b74d89bbSAndre Oppermann * net.inet.icmp.reply_src (default not set). Otherwise continue 766b74d89bbSAndre Oppermann * with normal source selection. 767b74d89bbSAndre Oppermann */ 76833c4f96dSRobert Watson if (V_reply_src[0] != '\0' && (ifp = ifunit(V_reply_src))) { 769137f91e8SJohn Baldwin IF_ADDR_RLOCK(ifp); 77033c4f96dSRobert Watson TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) { 771b74d89bbSAndre Oppermann if (ifa->ifa_addr->sa_family != AF_INET) 772b74d89bbSAndre Oppermann continue; 773b74d89bbSAndre Oppermann ia = ifatoia(ifa); 77433c4f96dSRobert Watson t = IA_SIN(ia)->sin_addr; 775137f91e8SJohn Baldwin IF_ADDR_RUNLOCK(ifp); 776b74d89bbSAndre Oppermann goto match; 777b74d89bbSAndre Oppermann } 778137f91e8SJohn Baldwin IF_ADDR_RUNLOCK(ifp); 779b74d89bbSAndre Oppermann } 780b74d89bbSAndre Oppermann /* 7811488eac8SAndre Oppermann * If the packet was transiting through us, use the address of 7821488eac8SAndre Oppermann * the interface that is the closest to the packet source. 7831488eac8SAndre Oppermann * When we don't have a route back to the packet source, stop here 7841488eac8SAndre Oppermann * and drop the packet. 7851488eac8SAndre Oppermann */ 7868b07e49aSJulian Elischer ia = ip_rtaddr(ip->ip_dst, M_GETFIB(m)); 7870d4bef5dSDima Dorfman if (ia == NULL) { 7880d4bef5dSDima Dorfman m_freem(m); 789e27b0c87SRobert Watson ICMPSTAT_INC(icps_noroute); 7900d4bef5dSDima Dorfman goto done; 7910d4bef5dSDima Dorfman } 79233c4f96dSRobert Watson t = IA_SIN(ia)->sin_addr; 7938c0fec80SRobert Watson ifa_free(&ia->ia_ifa); 794ca925d9cSJonathan Lemon match: 795baee0c3eSRobert Watson #ifdef MAC 796a13e21f7SRobert Watson mac_netinet_icmp_replyinplace(m); 797baee0c3eSRobert Watson #endif 798df8bae1dSRodney W. Grimes ip->ip_src = t; 799603724d3SBjoern A. Zeeb ip->ip_ttl = V_ip_defttl; 800df8bae1dSRodney W. Grimes 801df8bae1dSRodney W. Grimes if (optlen > 0) { 802df8bae1dSRodney W. Grimes register u_char *cp; 803df8bae1dSRodney W. Grimes int opt, cnt; 804df8bae1dSRodney W. Grimes u_int len; 805df8bae1dSRodney W. Grimes 806df8bae1dSRodney W. Grimes /* 807df8bae1dSRodney W. Grimes * Retrieve any source routing from the incoming packet; 808df8bae1dSRodney W. Grimes * add on any record-route or timestamp options. 809df8bae1dSRodney W. Grimes */ 810df8bae1dSRodney W. Grimes cp = (u_char *) (ip + 1); 811e0982661SAndre Oppermann if ((opts = ip_srcroute(m)) == 0 && 812*eb1b1807SGleb Smirnoff (opts = m_gethdr(M_NOWAIT, MT_DATA))) { 813df8bae1dSRodney W. Grimes opts->m_len = sizeof(struct in_addr); 814df8bae1dSRodney W. Grimes mtod(opts, struct in_addr *)->s_addr = 0; 815df8bae1dSRodney W. Grimes } 816df8bae1dSRodney W. Grimes if (opts) { 817df8bae1dSRodney W. Grimes #ifdef ICMPPRINTFS 818df8bae1dSRodney W. Grimes if (icmpprintfs) 819df8bae1dSRodney W. Grimes printf("icmp_reflect optlen %d rt %d => ", 820df8bae1dSRodney W. Grimes optlen, opts->m_len); 821df8bae1dSRodney W. Grimes #endif 822df8bae1dSRodney W. Grimes for (cnt = optlen; cnt > 0; cnt -= len, cp += len) { 823df8bae1dSRodney W. Grimes opt = cp[IPOPT_OPTVAL]; 824df8bae1dSRodney W. Grimes if (opt == IPOPT_EOL) 825df8bae1dSRodney W. Grimes break; 826df8bae1dSRodney W. Grimes if (opt == IPOPT_NOP) 827df8bae1dSRodney W. Grimes len = 1; 828df8bae1dSRodney W. Grimes else { 829707d00a3SJonathan Lemon if (cnt < IPOPT_OLEN + sizeof(*cp)) 830707d00a3SJonathan Lemon break; 831df8bae1dSRodney W. Grimes len = cp[IPOPT_OLEN]; 832707d00a3SJonathan Lemon if (len < IPOPT_OLEN + sizeof(*cp) || 833707d00a3SJonathan Lemon len > cnt) 834df8bae1dSRodney W. Grimes break; 835df8bae1dSRodney W. Grimes } 836df8bae1dSRodney W. Grimes /* 837df8bae1dSRodney W. Grimes * Should check for overflow, but it "can't happen" 838df8bae1dSRodney W. Grimes */ 839df8bae1dSRodney W. Grimes if (opt == IPOPT_RR || opt == IPOPT_TS || 840df8bae1dSRodney W. Grimes opt == IPOPT_SECURITY) { 841df8bae1dSRodney W. Grimes bcopy((caddr_t)cp, 842df8bae1dSRodney W. Grimes mtod(opts, caddr_t) + opts->m_len, len); 843df8bae1dSRodney W. Grimes opts->m_len += len; 844df8bae1dSRodney W. Grimes } 845df8bae1dSRodney W. Grimes } 846df8bae1dSRodney W. Grimes /* Terminate & pad, if necessary */ 847623ae52eSPoul-Henning Kamp cnt = opts->m_len % 4; 848623ae52eSPoul-Henning Kamp if (cnt) { 849df8bae1dSRodney W. Grimes for (; cnt < 4; cnt++) { 850df8bae1dSRodney W. Grimes *(mtod(opts, caddr_t) + opts->m_len) = 851df8bae1dSRodney W. Grimes IPOPT_EOL; 852df8bae1dSRodney W. Grimes opts->m_len++; 853df8bae1dSRodney W. Grimes } 854df8bae1dSRodney W. Grimes } 855df8bae1dSRodney W. Grimes #ifdef ICMPPRINTFS 856df8bae1dSRodney W. Grimes if (icmpprintfs) 857df8bae1dSRodney W. Grimes printf("%d\n", opts->m_len); 858df8bae1dSRodney W. Grimes #endif 859df8bae1dSRodney W. Grimes } 8609e2a372fSGleb Smirnoff ip_stripoptions(m); 861df8bae1dSRodney W. Grimes } 8629c855a36SSam Leffler m_tag_delete_nonpersistent(m); 863df8bae1dSRodney W. Grimes m->m_flags &= ~(M_BCAST|M_MCAST); 86402c1c707SAndre Oppermann icmp_send(m, opts); 865df8bae1dSRodney W. Grimes done: 866df8bae1dSRodney W. Grimes if (opts) 867df8bae1dSRodney W. Grimes (void)m_free(opts); 868df8bae1dSRodney W. Grimes } 869df8bae1dSRodney W. Grimes 870df8bae1dSRodney W. Grimes /* 871df8bae1dSRodney W. Grimes * Send an icmp packet back to the ip level, 872df8bae1dSRodney W. Grimes * after supplying a checksum. 873df8bae1dSRodney W. Grimes */ 8740312fbe9SPoul-Henning Kamp static void 875f2565d68SRobert Watson icmp_send(struct mbuf *m, struct mbuf *opts) 876df8bae1dSRodney W. Grimes { 877df8bae1dSRodney W. Grimes register struct ip *ip = mtod(m, struct ip *); 878df8bae1dSRodney W. Grimes register int hlen; 879df8bae1dSRodney W. Grimes register struct icmp *icp; 880df8bae1dSRodney W. Grimes 88153be11f6SPoul-Henning Kamp hlen = ip->ip_hl << 2; 882df8bae1dSRodney W. Grimes m->m_data += hlen; 883df8bae1dSRodney W. Grimes m->m_len -= hlen; 884df8bae1dSRodney W. Grimes icp = mtod(m, struct icmp *); 885df8bae1dSRodney W. Grimes icp->icmp_cksum = 0; 8868f134647SGleb Smirnoff icp->icmp_cksum = in_cksum(m, ntohs(ip->ip_len) - hlen); 887df8bae1dSRodney W. Grimes m->m_data -= hlen; 888df8bae1dSRodney W. Grimes m->m_len += hlen; 88994446a2eSArchie Cobbs m->m_pkthdr.rcvif = (struct ifnet *)0; 890df8bae1dSRodney W. Grimes #ifdef ICMPPRINTFS 8912b758395SGarrett Wollman if (icmpprintfs) { 8922b758395SGarrett Wollman char buf[4 * sizeof "123"]; 8932b758395SGarrett Wollman strcpy(buf, inet_ntoa(ip->ip_dst)); 8942b758395SGarrett Wollman printf("icmp_send dst %s src %s\n", 8952b758395SGarrett Wollman buf, inet_ntoa(ip->ip_src)); 8962b758395SGarrett Wollman } 897df8bae1dSRodney W. Grimes #endif 89802c1c707SAndre Oppermann (void) ip_output(m, opts, NULL, 0, NULL, NULL); 899df8bae1dSRodney W. Grimes } 900df8bae1dSRodney W. Grimes 901d685b6eeSLuigi Rizzo /* 902d685b6eeSLuigi Rizzo * Return milliseconds since 00:00 GMT in network format. 903d685b6eeSLuigi Rizzo */ 904d685b6eeSLuigi Rizzo uint32_t 905f2565d68SRobert Watson iptime(void) 906df8bae1dSRodney W. Grimes { 907df8bae1dSRodney W. Grimes struct timeval atv; 908df8bae1dSRodney W. Grimes u_long t; 909df8bae1dSRodney W. Grimes 91016cd6db0SBill Fumerola getmicrotime(&atv); 911df8bae1dSRodney W. Grimes t = (atv.tv_sec % (24*60*60)) * 1000 + atv.tv_usec / 1000; 912df8bae1dSRodney W. Grimes return (htonl(t)); 913df8bae1dSRodney W. Grimes } 914df8bae1dSRodney W. Grimes 9155cbf3e08SGarrett Wollman /* 9165cbf3e08SGarrett Wollman * Return the next larger or smaller MTU plateau (table from RFC 1191) 9175cbf3e08SGarrett Wollman * given current value MTU. If DIR is less than zero, a larger plateau 9185cbf3e08SGarrett Wollman * is returned; otherwise, a smaller value is returned. 9195cbf3e08SGarrett Wollman */ 9201aedbd9cSAndre Oppermann int 921f2565d68SRobert Watson ip_next_mtu(int mtu, int dir) 9225cbf3e08SGarrett Wollman { 9235cbf3e08SGarrett Wollman static int mtutab[] = { 9244c037f8dSAndre Oppermann 65535, 32000, 17914, 8166, 4352, 2002, 1492, 1280, 1006, 508, 9254c037f8dSAndre Oppermann 296, 68, 0 9265cbf3e08SGarrett Wollman }; 92706003a1eSAndre Oppermann int i, size; 9285cbf3e08SGarrett Wollman 92906003a1eSAndre Oppermann size = (sizeof mtutab) / (sizeof mtutab[0]); 93006003a1eSAndre Oppermann if (dir >= 0) { 9311c0b0f52SGleb Smirnoff for (i = 0; i < size; i++) 93206003a1eSAndre Oppermann if (mtu > mtutab[i]) 9335cbf3e08SGarrett Wollman return mtutab[i]; 9345cbf3e08SGarrett Wollman } else { 93506003a1eSAndre Oppermann for (i = size - 1; i >= 0; i--) 93606003a1eSAndre Oppermann if (mtu < mtutab[i]) 93706003a1eSAndre Oppermann return mtutab[i]; 93806003a1eSAndre Oppermann if (mtu == mtutab[0]) 93906003a1eSAndre Oppermann return mtutab[0]; 9405cbf3e08SGarrett Wollman } 94106003a1eSAndre Oppermann return 0; 9425cbf3e08SGarrett Wollman } 943b8e463e6SBjoern A. Zeeb #endif /* INET */ 94451508de1SMatthew Dillon 94551508de1SMatthew Dillon 94651508de1SMatthew Dillon /* 94751508de1SMatthew Dillon * badport_bandlim() - check for ICMP bandwidth limit 94851508de1SMatthew Dillon * 94951508de1SMatthew Dillon * Return 0 if it is ok to send an ICMP error response, -1 if we have 95051508de1SMatthew Dillon * hit our bandwidth limit and it is not ok. 95151508de1SMatthew Dillon * 95251508de1SMatthew Dillon * If icmplim is <= 0, the feature is disabled and 0 is returned. 95351508de1SMatthew Dillon * 95451508de1SMatthew Dillon * For now we separate the TCP and UDP subsystems w/ different 'which' 95551508de1SMatthew Dillon * values. We may eventually remove this separation (and simplify the 95651508de1SMatthew Dillon * code further). 95751508de1SMatthew Dillon * 95851508de1SMatthew Dillon * Note that the printing of the error message is delayed so we can 95951508de1SMatthew Dillon * properly print the icmp error rate that the system was trying to do 96051508de1SMatthew Dillon * (i.e. 22000/100 pps, etc...). This can cause long delays in printing 96151508de1SMatthew Dillon * the 'final' error, but it doesn't make sense to solve the printing 96251508de1SMatthew Dillon * delay with more complex code. 96351508de1SMatthew Dillon */ 96451508de1SMatthew Dillon 96551508de1SMatthew Dillon int 96651508de1SMatthew Dillon badport_bandlim(int which) 96751508de1SMatthew Dillon { 9688b615593SMarko Zec 96900f21882SSam Leffler #define N(a) (sizeof (a) / sizeof (a[0])) 97000f21882SSam Leffler static struct rate { 97100f21882SSam Leffler const char *type; 97200f21882SSam Leffler struct timeval lasttime; 973439dfb0cSStefan Farfeleder int curpps; 97400f21882SSam Leffler } rates[BANDLIM_MAX+1] = { 97500f21882SSam Leffler { "icmp unreach response" }, 97600f21882SSam Leffler { "icmp ping response" }, 97700f21882SSam Leffler { "icmp tstamp response" }, 97800f21882SSam Leffler { "closed port RST response" }, 97908af97b7SRobert Watson { "open port RST response" }, 9805ad9e57bSMichael Tuexen { "icmp6 unreach response" }, 9815ad9e57bSMichael Tuexen { "sctp ootb response" } 98209f81a46SBosko Milekic }; 98351508de1SMatthew Dillon 98451508de1SMatthew Dillon /* 98500f21882SSam Leffler * Return ok status if feature disabled or argument out of range. 98651508de1SMatthew Dillon */ 9878b615593SMarko Zec if (V_icmplim > 0 && (u_int) which < N(rates)) { 98800f21882SSam Leffler struct rate *r = &rates[which]; 98900f21882SSam Leffler int opps = r->curpps; 99051508de1SMatthew Dillon 9918b615593SMarko Zec if (!ppsratecheck(&r->lasttime, &r->curpps, V_icmplim)) 99200f21882SSam Leffler return -1; /* discard packet */ 99351508de1SMatthew Dillon /* 99400f21882SSam Leffler * If we've dropped below the threshold after having 99500f21882SSam Leffler * rate-limited traffic print the message. This preserves 99600f21882SSam Leffler * the previous behaviour at the expense of added complexity. 99751508de1SMatthew Dillon */ 9988b615593SMarko Zec if (V_icmplim_output && opps > V_icmplim) 99940fe9effSAndre Oppermann log(LOG_NOTICE, "Limiting %s from %d to %d packets/sec\n", 10008b615593SMarko Zec r->type, opps, V_icmplim); 100151508de1SMatthew Dillon } 100200f21882SSam Leffler return 0; /* okay to send packet */ 100300f21882SSam Leffler #undef N 100451508de1SMatthew Dillon } 1005