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> 46603724d3SBjoern A. Zeeb #include <sys/vimage.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> 51df8bae1dSRodney W. Grimes 52df8bae1dSRodney W. Grimes #include <netinet/in.h> 5397d8d152SAndre Oppermann #include <netinet/in_pcb.h> 54df8bae1dSRodney W. Grimes #include <netinet/in_systm.h> 55df8bae1dSRodney W. Grimes #include <netinet/in_var.h> 56df8bae1dSRodney W. Grimes #include <netinet/ip.h> 57df8bae1dSRodney W. Grimes #include <netinet/ip_icmp.h> 58b5e8ce9fSBruce Evans #include <netinet/ip_var.h> 59ef39adf0SAndre Oppermann #include <netinet/ip_options.h> 6097d8d152SAndre Oppermann #include <netinet/tcp.h> 6197d8d152SAndre Oppermann #include <netinet/tcp_var.h> 6297d8d152SAndre Oppermann #include <netinet/tcpip.h> 63df8bae1dSRodney W. Grimes #include <netinet/icmp_var.h> 644b79449eSBjoern A. Zeeb #include <netinet/vinet.h> 65df8bae1dSRodney W. Grimes 66b2630c29SGeorge V. Neville-Neil #ifdef IPSEC 67b9234fafSSam Leffler #include <netipsec/ipsec.h> 68b9234fafSSam Leffler #include <netipsec/key.h> 69b9234fafSSam Leffler #endif 70b9234fafSSam Leffler 7172a52a35SJonathan Lemon #include <machine/in_cksum.h> 7272a52a35SJonathan Lemon 73aed55708SRobert Watson #include <security/mac/mac_framework.h> 74aed55708SRobert Watson 75df8bae1dSRodney W. Grimes /* 76df8bae1dSRodney W. Grimes * ICMP routines: error generation, receive packet processing, and 77df8bae1dSRodney W. Grimes * routines to turnaround packets back to the originator, and 78df8bae1dSRodney W. Grimes * host table maintenance routines. 79df8bae1dSRodney W. Grimes */ 80df8bae1dSRodney W. Grimes 8144e33a07SMarko Zec #ifdef VIMAGE_GLOBALS 82cc5934f5SMax Laier struct icmpstat icmpstat; 8344e33a07SMarko Zec static int icmpmaskrepl; 8444e33a07SMarko Zec static u_int icmpmaskfake; 8544e33a07SMarko Zec static int drop_redirect; 8644e33a07SMarko Zec static int log_redirect; 8744e33a07SMarko Zec static int icmplim; 8844e33a07SMarko Zec static int icmplim_output; 8944e33a07SMarko Zec static char reply_src[IFNAMSIZ]; 9044e33a07SMarko Zec static int icmp_rfi; 9144e33a07SMarko Zec static int icmp_quotelen; 9244e33a07SMarko Zec static int icmpbmcastecho; 9344e33a07SMarko Zec #endif 9444e33a07SMarko Zec 958b615593SMarko Zec SYSCTL_V_STRUCT(V_NET, vnet_inet, _net_inet_icmp, ICMPCTL_STATS, stats, 968b615593SMarko Zec CTLFLAG_RW, icmpstat, icmpstat, ""); 970312fbe9SPoul-Henning Kamp 988b615593SMarko Zec SYSCTL_V_INT(V_NET, vnet_inet, _net_inet_icmp, ICMPCTL_MASKREPL, maskrepl, 998b615593SMarko Zec CTLFLAG_RW, icmpmaskrepl, 0, 1008b615593SMarko Zec "Reply to ICMP Address Mask Request packets."); 10157842a38SMatthew N. Dodd 1028b615593SMarko Zec SYSCTL_V_UINT(V_NET, vnet_inet, _net_inet_icmp, OID_AUTO, maskfake, CTLFLAG_RW, 1038b615593SMarko Zec icmpmaskfake, 0, "Fake reply to ICMP Address Mask Request packets."); 1040312fbe9SPoul-Henning Kamp 1058b615593SMarko Zec SYSCTL_V_INT(V_NET, vnet_inet, _net_inet_icmp, OID_AUTO, drop_redirect, 1068b615593SMarko Zec CTLFLAG_RW, drop_redirect, 0, "Ignore ICMP redirects"); 10718d3153eSDag-Erling Smørgrav 1088b615593SMarko Zec SYSCTL_V_INT(V_NET, vnet_inet, _net_inet_icmp, OID_AUTO, log_redirect, 1098b615593SMarko Zec CTLFLAG_RW, log_redirect, 0, "Log ICMP redirects to the console"); 1106c3b5f69SDag-Erling Smørgrav 1118b615593SMarko Zec SYSCTL_V_INT(V_NET, vnet_inet, _net_inet_icmp, ICMPCTL_ICMPLIM, icmplim, 1128b615593SMarko Zec CTLFLAG_RW, icmplim, 0, "Maximum number of ICMP responses per second"); 1135fce7fc4SMatthew Dillon 1148b615593SMarko Zec SYSCTL_V_INT(V_NET, vnet_inet, _net_inet_icmp, OID_AUTO, icmplim_output, 1158b615593SMarko Zec CTLFLAG_RW, icmplim_output, 0, 1168b615593SMarko Zec "Enable rate limiting of ICMP responses"); 11751508de1SMatthew Dillon 1188b615593SMarko Zec SYSCTL_V_STRING(V_NET, vnet_inet, _net_inet_icmp, OID_AUTO, reply_src, 1198b615593SMarko Zec CTLFLAG_RW, reply_src, IFNAMSIZ, 1208b615593SMarko Zec "icmp reply source for non-local packets."); 121b74d89bbSAndre Oppermann 1228b615593SMarko Zec SYSCTL_V_INT(V_NET, vnet_inet, _net_inet_icmp, OID_AUTO, reply_from_interface, 1238b615593SMarko Zec CTLFLAG_RW, icmp_rfi, 0, "ICMP reply from incoming interface for " 124a0866c8dSAndre Oppermann "non-local packets"); 125a0866c8dSAndre Oppermann 1268b615593SMarko Zec SYSCTL_V_INT(V_NET, vnet_inet, _net_inet_icmp, OID_AUTO, quotelen, CTLFLAG_RW, 1278b615593SMarko Zec icmp_quotelen, 0, "Number of bytes from original packet to " 128e875dfb8SAndre Oppermann "quote in ICMP reply"); 129e875dfb8SAndre Oppermann 1305fce7fc4SMatthew Dillon /* 1315fce7fc4SMatthew Dillon * ICMP broadcast echo sysctl 1325fce7fc4SMatthew Dillon */ 1335fce7fc4SMatthew Dillon 1348b615593SMarko Zec SYSCTL_V_INT(V_NET, vnet_inet, _net_inet_icmp, OID_AUTO, bmcastecho, 1358b615593SMarko Zec CTLFLAG_RW, icmpbmcastecho, 0, ""); 1367022ea0aSGarrett Wollman 13751508de1SMatthew Dillon 138df8bae1dSRodney W. Grimes #ifdef ICMPPRINTFS 139df8bae1dSRodney W. Grimes int icmpprintfs = 0; 140df8bae1dSRodney W. Grimes #endif 141df8bae1dSRodney W. Grimes 1424d77a549SAlfred Perlstein static void icmp_reflect(struct mbuf *); 14302c1c707SAndre Oppermann static void icmp_send(struct mbuf *, struct mbuf *); 1440312fbe9SPoul-Henning Kamp 145df8bae1dSRodney W. Grimes extern struct protosw inetsw[]; 146df8bae1dSRodney W. Grimes 14744e33a07SMarko Zec void 14844e33a07SMarko Zec icmp_init(void) 14944e33a07SMarko Zec { 15044e33a07SMarko Zec INIT_VNET_INET(curvnet); 15144e33a07SMarko Zec 15244e33a07SMarko Zec V_icmpmaskrepl = 0; 15344e33a07SMarko Zec V_icmpmaskfake = 0; 15444e33a07SMarko Zec V_drop_redirect = 0; 15544e33a07SMarko Zec V_log_redirect = 0; 15644e33a07SMarko Zec V_icmplim = 200; 15744e33a07SMarko Zec V_icmplim_output = 1; 15844e33a07SMarko Zec V_icmp_rfi = 0; 15944e33a07SMarko Zec V_icmp_quotelen = 8; 16044e33a07SMarko Zec V_icmpbmcastecho = 0; 16144e33a07SMarko Zec } 16244e33a07SMarko Zec 163df8bae1dSRodney W. Grimes /* 164df8bae1dSRodney W. Grimes * Generate an error packet of type error 165df8bae1dSRodney W. Grimes * in response to bad packet ip. 166df8bae1dSRodney W. Grimes */ 167df8bae1dSRodney W. Grimes void 168d685b6eeSLuigi Rizzo icmp_error(struct mbuf *n, int type, int code, uint32_t dest, int mtu) 169df8bae1dSRodney W. Grimes { 1708b615593SMarko Zec INIT_VNET_INET(curvnet); 171df8bae1dSRodney W. Grimes register struct ip *oip = mtod(n, struct ip *), *nip; 172e86ebebcSAndre Oppermann register unsigned oiphlen = oip->ip_hl << 2; 173df8bae1dSRodney W. Grimes register struct icmp *icp; 174df8bae1dSRodney W. Grimes register struct mbuf *m; 175e86ebebcSAndre Oppermann unsigned icmplen, icmpelen, nlen; 176df8bae1dSRodney W. Grimes 177e86ebebcSAndre Oppermann KASSERT((u_int)type <= ICMP_MAXTYPE, ("%s: illegal ICMP type", __func__)); 178df8bae1dSRodney W. Grimes #ifdef ICMPPRINTFS 179df8bae1dSRodney W. Grimes if (icmpprintfs) 180623ae52eSPoul-Henning Kamp printf("icmp_error(%p, %x, %d)\n", oip, type, code); 181df8bae1dSRodney W. Grimes #endif 182df8bae1dSRodney W. Grimes if (type != ICMP_REDIRECT) 183603724d3SBjoern A. Zeeb V_icmpstat.icps_error++; 184df8bae1dSRodney W. Grimes /* 185e86ebebcSAndre Oppermann * Don't send error: 186e86ebebcSAndre Oppermann * if the original packet was encrypted. 187e86ebebcSAndre Oppermann * if not the first fragment of message. 188e86ebebcSAndre Oppermann * in response to a multicast or broadcast packet. 189e86ebebcSAndre Oppermann * if the old packet protocol was an ICMP error message. 190df8bae1dSRodney W. Grimes */ 191cad1917dSHajimu UMEMOTO if (n->m_flags & M_DECRYPTED) 192cad1917dSHajimu UMEMOTO goto freeit; 193df8bae1dSRodney W. Grimes if (oip->ip_off & ~(IP_MF|IP_DF)) 194df8bae1dSRodney W. Grimes goto freeit; 195e86ebebcSAndre Oppermann if (n->m_flags & (M_BCAST|M_MCAST)) 196e86ebebcSAndre Oppermann goto freeit; 197df8bae1dSRodney W. Grimes if (oip->ip_p == IPPROTO_ICMP && type != ICMP_REDIRECT && 198e86ebebcSAndre Oppermann n->m_len >= oiphlen + ICMP_MINLEN && 199e86ebebcSAndre Oppermann !ICMP_INFOTYPE(((struct icmp *)((caddr_t)oip + oiphlen))->icmp_type)) { 200603724d3SBjoern A. Zeeb V_icmpstat.icps_oldicmp++; 201df8bae1dSRodney W. Grimes goto freeit; 202df8bae1dSRodney W. Grimes } 203e86ebebcSAndre Oppermann /* Drop if IP header plus 8 bytes is not contignous in first mbuf. */ 204e86ebebcSAndre Oppermann if (oiphlen + 8 > n->m_len) 205df8bae1dSRodney W. Grimes goto freeit; 206df8bae1dSRodney W. Grimes /* 207e86ebebcSAndre Oppermann * Calculate length to quote from original packet and 208e86ebebcSAndre Oppermann * prevent the ICMP mbuf from overflowing. 209e86ebebcSAndre Oppermann * Unfortunatly this is non-trivial since ip_forward() 210e86ebebcSAndre Oppermann * sends us truncated packets. 211df8bae1dSRodney W. Grimes */ 212e86ebebcSAndre Oppermann nlen = m_length(n, NULL); 213e86ebebcSAndre Oppermann if (oip->ip_p == IPPROTO_TCP) { 214e86ebebcSAndre Oppermann struct tcphdr *th; 215e86ebebcSAndre Oppermann int tcphlen; 216e86ebebcSAndre Oppermann 217e86ebebcSAndre Oppermann if (oiphlen + sizeof(struct tcphdr) > n->m_len && 218e86ebebcSAndre Oppermann n->m_next == NULL) 219e86ebebcSAndre Oppermann goto stdreply; 220e86ebebcSAndre Oppermann if (n->m_len < oiphlen + sizeof(struct tcphdr) && 221e86ebebcSAndre Oppermann ((n = m_pullup(n, oiphlen + sizeof(struct tcphdr))) == NULL)) 222e86ebebcSAndre Oppermann goto freeit; 223e86ebebcSAndre Oppermann th = (struct tcphdr *)((caddr_t)oip + oiphlen); 224e86ebebcSAndre Oppermann tcphlen = th->th_off << 2; 225e86ebebcSAndre Oppermann if (tcphlen < sizeof(struct tcphdr)) 226e86ebebcSAndre Oppermann goto freeit; 227e86ebebcSAndre Oppermann if (oip->ip_len < oiphlen + tcphlen) 228e86ebebcSAndre Oppermann goto freeit; 229e86ebebcSAndre Oppermann if (oiphlen + tcphlen > n->m_len && n->m_next == NULL) 230e86ebebcSAndre Oppermann goto stdreply; 231e86ebebcSAndre Oppermann if (n->m_len < oiphlen + tcphlen && 232e86ebebcSAndre Oppermann ((n = m_pullup(n, oiphlen + tcphlen)) == NULL)) 233e86ebebcSAndre Oppermann goto freeit; 2348b615593SMarko Zec icmpelen = max(tcphlen, min(V_icmp_quotelen, oip->ip_len - oiphlen)); 235e86ebebcSAndre Oppermann } else 2368b615593SMarko Zec stdreply: icmpelen = max(8, min(V_icmp_quotelen, oip->ip_len - oiphlen)); 237e86ebebcSAndre Oppermann 238e86ebebcSAndre Oppermann icmplen = min(oiphlen + icmpelen, nlen); 239e86ebebcSAndre Oppermann if (icmplen < sizeof(struct ip)) 240e86ebebcSAndre Oppermann goto freeit; 241e86ebebcSAndre Oppermann 242e86ebebcSAndre Oppermann if (MHLEN > sizeof(struct ip) + ICMP_MINLEN + icmplen) 243e86ebebcSAndre Oppermann m = m_gethdr(M_DONTWAIT, MT_DATA); 244e86ebebcSAndre Oppermann else 245e86ebebcSAndre Oppermann m = m_getcl(M_DONTWAIT, MT_DATA, M_PKTHDR); 246df8bae1dSRodney W. Grimes if (m == NULL) 247df8bae1dSRodney W. Grimes goto freeit; 2480070e096SRobert Watson #ifdef MAC 249a13e21f7SRobert Watson mac_netinet_icmp_reply(n, m); 2500070e096SRobert Watson #endif 251e86ebebcSAndre Oppermann icmplen = min(icmplen, M_TRAILINGSPACE(m) - sizeof(struct ip) - ICMP_MINLEN); 252e86ebebcSAndre Oppermann m_align(m, ICMP_MINLEN + icmplen); 253e86ebebcSAndre Oppermann m->m_len = ICMP_MINLEN + icmplen; 2546b773dffSAndre Oppermann 2558b07e49aSJulian Elischer /* XXX MRT make the outgoing packet use the same FIB 2568b07e49aSJulian Elischer * that was associated with the incoming packet 2578b07e49aSJulian Elischer */ 2588b07e49aSJulian Elischer M_SETFIB(m, M_GETFIB(n)); 259df8bae1dSRodney W. Grimes icp = mtod(m, struct icmp *); 260603724d3SBjoern A. Zeeb V_icmpstat.icps_outhist[type]++; 261df8bae1dSRodney W. Grimes icp->icmp_type = type; 262df8bae1dSRodney W. Grimes if (type == ICMP_REDIRECT) 263df8bae1dSRodney W. Grimes icp->icmp_gwaddr.s_addr = dest; 264df8bae1dSRodney W. Grimes else { 265df8bae1dSRodney W. Grimes icp->icmp_void = 0; 266df8bae1dSRodney W. Grimes /* 267df8bae1dSRodney W. Grimes * The following assignments assume an overlay with the 268e86ebebcSAndre Oppermann * just zeroed icmp_void field. 269df8bae1dSRodney W. Grimes */ 270df8bae1dSRodney W. Grimes if (type == ICMP_PARAMPROB) { 271df8bae1dSRodney W. Grimes icp->icmp_pptr = code; 272df8bae1dSRodney W. Grimes code = 0; 273df8bae1dSRodney W. Grimes } else if (type == ICMP_UNREACH && 274c773494eSAndre Oppermann code == ICMP_UNREACH_NEEDFRAG && mtu) { 275c773494eSAndre Oppermann icp->icmp_nextmtu = htons(mtu); 276df8bae1dSRodney W. Grimes } 277df8bae1dSRodney W. Grimes } 278df8bae1dSRodney W. Grimes icp->icmp_code = code; 27904287599SRuslan Ermilov 28004287599SRuslan Ermilov /* 281e86ebebcSAndre Oppermann * Copy the quotation into ICMP message and 282e86ebebcSAndre Oppermann * convert quoted IP header back to network representation. 28304287599SRuslan Ermilov */ 284e86ebebcSAndre Oppermann m_copydata(n, 0, icmplen, (caddr_t)&icp->icmp_ip); 285e86ebebcSAndre Oppermann nip = &icp->icmp_ip; 286fd8e4ebcSMike Barcroft nip->ip_len = htons(nip->ip_len); 287fd8e4ebcSMike Barcroft nip->ip_off = htons(nip->ip_off); 288df8bae1dSRodney W. Grimes 289df8bae1dSRodney W. Grimes /* 290e86ebebcSAndre Oppermann * Set up ICMP message mbuf and copy old IP header (without options 291e86ebebcSAndre Oppermann * in front of ICMP message. 292c550f220SMax Laier * If the original mbuf was meant to bypass the firewall, the error 293c550f220SMax Laier * reply should bypass as well. 294c550f220SMax Laier */ 295c550f220SMax Laier m->m_flags |= n->m_flags & M_SKIP_FIREWALL; 296df8bae1dSRodney W. Grimes m->m_data -= sizeof(struct ip); 297df8bae1dSRodney W. Grimes m->m_len += sizeof(struct ip); 298df8bae1dSRodney W. Grimes m->m_pkthdr.len = m->m_len; 299df8bae1dSRodney W. Grimes m->m_pkthdr.rcvif = n->m_pkthdr.rcvif; 300df8bae1dSRodney W. Grimes nip = mtod(m, struct ip *); 301df8bae1dSRodney W. Grimes bcopy((caddr_t)oip, (caddr_t)nip, sizeof(struct ip)); 302df8bae1dSRodney W. Grimes nip->ip_len = m->m_len; 30353be11f6SPoul-Henning Kamp nip->ip_v = IPVERSION; 30453be11f6SPoul-Henning Kamp nip->ip_hl = 5; 305df8bae1dSRodney W. Grimes nip->ip_p = IPPROTO_ICMP; 306df8bae1dSRodney W. Grimes nip->ip_tos = 0; 307df8bae1dSRodney W. Grimes icmp_reflect(m); 308df8bae1dSRodney W. Grimes 309df8bae1dSRodney W. Grimes freeit: 310df8bae1dSRodney W. Grimes m_freem(n); 311df8bae1dSRodney W. Grimes } 312df8bae1dSRodney W. Grimes 313df8bae1dSRodney W. Grimes /* 314df8bae1dSRodney W. Grimes * Process a received ICMP message. 315df8bae1dSRodney W. Grimes */ 316df8bae1dSRodney W. Grimes void 317f2565d68SRobert Watson icmp_input(struct mbuf *m, int off) 318df8bae1dSRodney W. Grimes { 3198b615593SMarko Zec INIT_VNET_INET(curvnet); 32016d6c90fSAndre Oppermann struct icmp *icp; 321df8bae1dSRodney W. Grimes struct in_ifaddr *ia; 32216d6c90fSAndre Oppermann struct ip *ip = mtod(m, struct ip *); 32316d6c90fSAndre Oppermann struct sockaddr_in icmpsrc, icmpdst, icmpgw; 32416d6c90fSAndre Oppermann int hlen = off; 32516d6c90fSAndre Oppermann int icmplen = ip->ip_len; 32616d6c90fSAndre Oppermann int i, code; 3274d77a549SAlfred Perlstein void (*ctlfunc)(int, struct sockaddr *, void *); 3288b07e49aSJulian Elischer int fibnum; 329df8bae1dSRodney W. Grimes 330df8bae1dSRodney W. Grimes /* 331df8bae1dSRodney W. Grimes * Locate icmp structure in mbuf, and check 332df8bae1dSRodney W. Grimes * that not corrupted and of at least minimum length. 333df8bae1dSRodney W. Grimes */ 334df8bae1dSRodney W. Grimes #ifdef ICMPPRINTFS 3352b758395SGarrett Wollman if (icmpprintfs) { 3362b758395SGarrett Wollman char buf[4 * sizeof "123"]; 3372b758395SGarrett Wollman strcpy(buf, inet_ntoa(ip->ip_src)); 3382b758395SGarrett Wollman printf("icmp_input from %s to %s, len %d\n", 3392b758395SGarrett Wollman buf, inet_ntoa(ip->ip_dst), icmplen); 3402b758395SGarrett Wollman } 341df8bae1dSRodney W. Grimes #endif 342df8bae1dSRodney W. Grimes if (icmplen < ICMP_MINLEN) { 343603724d3SBjoern A. Zeeb V_icmpstat.icps_tooshort++; 344df8bae1dSRodney W. Grimes goto freeit; 345df8bae1dSRodney W. Grimes } 346df8bae1dSRodney W. Grimes i = hlen + min(icmplen, ICMP_ADVLENMIN); 347df8bae1dSRodney W. Grimes if (m->m_len < i && (m = m_pullup(m, i)) == 0) { 348603724d3SBjoern A. Zeeb V_icmpstat.icps_tooshort++; 349df8bae1dSRodney W. Grimes return; 350df8bae1dSRodney W. Grimes } 351df8bae1dSRodney W. Grimes ip = mtod(m, struct ip *); 352df8bae1dSRodney W. Grimes m->m_len -= hlen; 353df8bae1dSRodney W. Grimes m->m_data += hlen; 354df8bae1dSRodney W. Grimes icp = mtod(m, struct icmp *); 355df8bae1dSRodney W. Grimes if (in_cksum(m, icmplen)) { 356603724d3SBjoern A. Zeeb V_icmpstat.icps_checksum++; 357df8bae1dSRodney W. Grimes goto freeit; 358df8bae1dSRodney W. Grimes } 359df8bae1dSRodney W. Grimes m->m_len += hlen; 360df8bae1dSRodney W. Grimes m->m_data -= hlen; 361df8bae1dSRodney W. Grimes 3626a800098SYoshinobu Inoue if (m->m_pkthdr.rcvif && m->m_pkthdr.rcvif->if_type == IFT_FAITH) { 3636a800098SYoshinobu Inoue /* 3646a800098SYoshinobu Inoue * Deliver very specific ICMP type only. 3656a800098SYoshinobu Inoue */ 3666a800098SYoshinobu Inoue switch (icp->icmp_type) { 3676a800098SYoshinobu Inoue case ICMP_UNREACH: 3686a800098SYoshinobu Inoue case ICMP_TIMXCEED: 3696a800098SYoshinobu Inoue break; 3706a800098SYoshinobu Inoue default: 3716a800098SYoshinobu Inoue goto freeit; 3726a800098SYoshinobu Inoue } 3736a800098SYoshinobu Inoue } 3746a800098SYoshinobu Inoue 375df8bae1dSRodney W. Grimes #ifdef ICMPPRINTFS 376df8bae1dSRodney W. Grimes if (icmpprintfs) 377df8bae1dSRodney W. Grimes printf("icmp_input, type %d code %d\n", icp->icmp_type, 378df8bae1dSRodney W. Grimes icp->icmp_code); 379df8bae1dSRodney W. Grimes #endif 3805b7ee6edSGarrett Wollman 3815b7ee6edSGarrett Wollman /* 3825b7ee6edSGarrett Wollman * Message type specific processing. 3835b7ee6edSGarrett Wollman */ 384df8bae1dSRodney W. Grimes if (icp->icmp_type > ICMP_MAXTYPE) 385df8bae1dSRodney W. Grimes goto raw; 38616d6c90fSAndre Oppermann 38716d6c90fSAndre Oppermann /* Initialize */ 38816d6c90fSAndre Oppermann bzero(&icmpsrc, sizeof(icmpsrc)); 38916d6c90fSAndre Oppermann icmpsrc.sin_len = sizeof(struct sockaddr_in); 39016d6c90fSAndre Oppermann icmpsrc.sin_family = AF_INET; 39116d6c90fSAndre Oppermann bzero(&icmpdst, sizeof(icmpdst)); 39216d6c90fSAndre Oppermann icmpdst.sin_len = sizeof(struct sockaddr_in); 39316d6c90fSAndre Oppermann icmpdst.sin_family = AF_INET; 39416d6c90fSAndre Oppermann bzero(&icmpgw, sizeof(icmpgw)); 39516d6c90fSAndre Oppermann icmpgw.sin_len = sizeof(struct sockaddr_in); 39616d6c90fSAndre Oppermann icmpgw.sin_family = AF_INET; 39716d6c90fSAndre Oppermann 398603724d3SBjoern A. Zeeb V_icmpstat.icps_inhist[icp->icmp_type]++; 399df8bae1dSRodney W. Grimes code = icp->icmp_code; 400df8bae1dSRodney W. Grimes switch (icp->icmp_type) { 401df8bae1dSRodney W. Grimes 402df8bae1dSRodney W. Grimes case ICMP_UNREACH: 403df8bae1dSRodney W. Grimes switch (code) { 404df8bae1dSRodney W. Grimes case ICMP_UNREACH_NET: 405df8bae1dSRodney W. Grimes case ICMP_UNREACH_HOST: 406df8bae1dSRodney W. Grimes case ICMP_UNREACH_SRCFAIL: 407e4bb5b05SJonathan Lemon case ICMP_UNREACH_NET_UNKNOWN: 408e4bb5b05SJonathan Lemon case ICMP_UNREACH_HOST_UNKNOWN: 409e4bb5b05SJonathan Lemon case ICMP_UNREACH_ISOLATED: 410e4bb5b05SJonathan Lemon case ICMP_UNREACH_TOSNET: 411e4bb5b05SJonathan Lemon case ICMP_UNREACH_TOSHOST: 412e4bb5b05SJonathan Lemon case ICMP_UNREACH_HOST_PRECEDENCE: 413e4bb5b05SJonathan Lemon case ICMP_UNREACH_PRECEDENCE_CUTOFF: 414e4bb5b05SJonathan Lemon code = PRC_UNREACH_NET; 415df8bae1dSRodney W. Grimes break; 416df8bae1dSRodney W. Grimes 417df8bae1dSRodney W. Grimes case ICMP_UNREACH_NEEDFRAG: 418df8bae1dSRodney W. Grimes code = PRC_MSGSIZE; 419df8bae1dSRodney W. Grimes break; 420df8bae1dSRodney W. Grimes 421e4bb5b05SJonathan Lemon /* 422e4bb5b05SJonathan Lemon * RFC 1122, Sections 3.2.2.1 and 4.2.3.9. 423e4bb5b05SJonathan Lemon * Treat subcodes 2,3 as immediate RST 424e4bb5b05SJonathan Lemon */ 425e4bb5b05SJonathan Lemon case ICMP_UNREACH_PROTOCOL: 426e4bb5b05SJonathan Lemon case ICMP_UNREACH_PORT: 427b77d155dSJesper Skriver code = PRC_UNREACH_PORT; 428b11d7a4aSPoul-Henning Kamp break; 42990fcbbd6SPoul-Henning Kamp 43090fcbbd6SPoul-Henning Kamp case ICMP_UNREACH_NET_PROHIB: 43190fcbbd6SPoul-Henning Kamp case ICMP_UNREACH_HOST_PROHIB: 4329c4b2574SPaul Traina case ICMP_UNREACH_FILTER_PROHIB: 43390fcbbd6SPoul-Henning Kamp code = PRC_UNREACH_ADMIN_PROHIB; 434b11d7a4aSPoul-Henning Kamp break; 435b11d7a4aSPoul-Henning Kamp 436df8bae1dSRodney W. Grimes default: 437df8bae1dSRodney W. Grimes goto badcode; 438df8bae1dSRodney W. Grimes } 439df8bae1dSRodney W. Grimes goto deliver; 440df8bae1dSRodney W. Grimes 441df8bae1dSRodney W. Grimes case ICMP_TIMXCEED: 442df8bae1dSRodney W. Grimes if (code > 1) 443df8bae1dSRodney W. Grimes goto badcode; 444df8bae1dSRodney W. Grimes code += PRC_TIMXCEED_INTRANS; 445df8bae1dSRodney W. Grimes goto deliver; 446df8bae1dSRodney W. Grimes 447df8bae1dSRodney W. Grimes case ICMP_PARAMPROB: 448df8bae1dSRodney W. Grimes if (code > 1) 449df8bae1dSRodney W. Grimes goto badcode; 450df8bae1dSRodney W. Grimes code = PRC_PARAMPROB; 451df8bae1dSRodney W. Grimes goto deliver; 452df8bae1dSRodney W. Grimes 453df8bae1dSRodney W. Grimes case ICMP_SOURCEQUENCH: 454df8bae1dSRodney W. Grimes if (code) 455df8bae1dSRodney W. Grimes goto badcode; 456df8bae1dSRodney W. Grimes code = PRC_QUENCH; 457df8bae1dSRodney W. Grimes deliver: 458df8bae1dSRodney W. Grimes /* 459df8bae1dSRodney W. Grimes * Problem with datagram; advise higher level routines. 460df8bae1dSRodney W. Grimes */ 461df8bae1dSRodney W. Grimes if (icmplen < ICMP_ADVLENMIN || icmplen < ICMP_ADVLEN(icp) || 46253be11f6SPoul-Henning Kamp icp->icmp_ip.ip_hl < (sizeof(struct ip) >> 2)) { 463603724d3SBjoern A. Zeeb V_icmpstat.icps_badlen++; 464df8bae1dSRodney W. Grimes goto freeit; 465df8bae1dSRodney W. Grimes } 466fd8e4ebcSMike Barcroft icp->icmp_ip.ip_len = ntohs(icp->icmp_ip.ip_len); 4675b7ee6edSGarrett Wollman /* Discard ICMP's in response to multicast packets */ 4685b7ee6edSGarrett Wollman if (IN_MULTICAST(ntohl(icp->icmp_ip.ip_dst.s_addr))) 4695b7ee6edSGarrett Wollman goto badcode; 470df8bae1dSRodney W. Grimes #ifdef ICMPPRINTFS 471df8bae1dSRodney W. Grimes if (icmpprintfs) 472df8bae1dSRodney W. Grimes printf("deliver to protocol %d\n", icp->icmp_ip.ip_p); 473df8bae1dSRodney W. Grimes #endif 474df8bae1dSRodney W. Grimes icmpsrc.sin_addr = icp->icmp_ip.ip_dst; 4756a800098SYoshinobu Inoue /* 4766a800098SYoshinobu Inoue * XXX if the packet contains [IPv4 AH TCP], we can't make a 4776a800098SYoshinobu Inoue * notification to TCP layer. 4786a800098SYoshinobu Inoue */ 479623ae52eSPoul-Henning Kamp ctlfunc = inetsw[ip_protox[icp->icmp_ip.ip_p]].pr_ctlinput; 480623ae52eSPoul-Henning Kamp if (ctlfunc) 481df8bae1dSRodney W. Grimes (*ctlfunc)(code, (struct sockaddr *)&icmpsrc, 482b62d102cSBruce Evans (void *)&icp->icmp_ip); 483df8bae1dSRodney W. Grimes break; 484df8bae1dSRodney W. Grimes 485df8bae1dSRodney W. Grimes badcode: 486603724d3SBjoern A. Zeeb V_icmpstat.icps_badcode++; 487df8bae1dSRodney W. Grimes break; 488df8bae1dSRodney W. Grimes 489df8bae1dSRodney W. Grimes case ICMP_ECHO: 4908b615593SMarko Zec if (!V_icmpbmcastecho 491d311884fSDavid Greenman && (m->m_flags & (M_MCAST | M_BCAST)) != 0) { 492603724d3SBjoern A. Zeeb V_icmpstat.icps_bmcastecho++; 4937022ea0aSGarrett Wollman break; 4947022ea0aSGarrett Wollman } 495df8bae1dSRodney W. Grimes icp->icmp_type = ICMP_ECHOREPLY; 496a57815efSBosko Milekic if (badport_bandlim(BANDLIM_ICMP_ECHO) < 0) 49709f81a46SBosko Milekic goto freeit; 49809f81a46SBosko Milekic else 499df8bae1dSRodney W. Grimes goto reflect; 500df8bae1dSRodney W. Grimes 501df8bae1dSRodney W. Grimes case ICMP_TSTAMP: 5028b615593SMarko Zec if (!V_icmpbmcastecho 503d311884fSDavid Greenman && (m->m_flags & (M_MCAST | M_BCAST)) != 0) { 504603724d3SBjoern A. Zeeb V_icmpstat.icps_bmcasttstamp++; 505fe0fb8abSGarrett Wollman break; 506fe0fb8abSGarrett Wollman } 507df8bae1dSRodney W. Grimes if (icmplen < ICMP_TSLEN) { 508603724d3SBjoern A. Zeeb V_icmpstat.icps_badlen++; 509df8bae1dSRodney W. Grimes break; 510df8bae1dSRodney W. Grimes } 511df8bae1dSRodney W. Grimes icp->icmp_type = ICMP_TSTAMPREPLY; 512df8bae1dSRodney W. Grimes icp->icmp_rtime = iptime(); 513df8bae1dSRodney W. Grimes icp->icmp_ttime = icp->icmp_rtime; /* bogus, do later! */ 514a57815efSBosko Milekic if (badport_bandlim(BANDLIM_ICMP_TSTAMP) < 0) 51509f81a46SBosko Milekic goto freeit; 51609f81a46SBosko Milekic else 517df8bae1dSRodney W. Grimes goto reflect; 518df8bae1dSRodney W. Grimes 519df8bae1dSRodney W. Grimes case ICMP_MASKREQ: 5208b615593SMarko Zec if (V_icmpmaskrepl == 0) 521df8bae1dSRodney W. Grimes break; 522df8bae1dSRodney W. Grimes /* 523df8bae1dSRodney W. Grimes * We are not able to respond with all ones broadcast 524df8bae1dSRodney W. Grimes * unless we receive it over a point-to-point interface. 525df8bae1dSRodney W. Grimes */ 526df8bae1dSRodney W. Grimes if (icmplen < ICMP_MASKLEN) 527df8bae1dSRodney W. Grimes break; 528df8bae1dSRodney W. Grimes switch (ip->ip_dst.s_addr) { 529df8bae1dSRodney W. Grimes 530df8bae1dSRodney W. Grimes case INADDR_BROADCAST: 531df8bae1dSRodney W. Grimes case INADDR_ANY: 532df8bae1dSRodney W. Grimes icmpdst.sin_addr = ip->ip_src; 533df8bae1dSRodney W. Grimes break; 534df8bae1dSRodney W. Grimes 535df8bae1dSRodney W. Grimes default: 536df8bae1dSRodney W. Grimes icmpdst.sin_addr = ip->ip_dst; 537df8bae1dSRodney W. Grimes } 538df8bae1dSRodney W. Grimes ia = (struct in_ifaddr *)ifaof_ifpforaddr( 539df8bae1dSRodney W. Grimes (struct sockaddr *)&icmpdst, m->m_pkthdr.rcvif); 540df8bae1dSRodney W. Grimes if (ia == 0) 541df8bae1dSRodney W. Grimes break; 5427e6f7714SPoul-Henning Kamp if (ia->ia_ifp == 0) 5437e6f7714SPoul-Henning Kamp break; 544df8bae1dSRodney W. Grimes icp->icmp_type = ICMP_MASKREPLY; 5458b615593SMarko Zec if (V_icmpmaskfake == 0) 546df8bae1dSRodney W. Grimes icp->icmp_mask = ia->ia_sockmask.sin_addr.s_addr; 54757842a38SMatthew N. Dodd else 5488b615593SMarko Zec icp->icmp_mask = V_icmpmaskfake; 549df8bae1dSRodney W. Grimes if (ip->ip_src.s_addr == 0) { 550df8bae1dSRodney W. Grimes if (ia->ia_ifp->if_flags & IFF_BROADCAST) 551df8bae1dSRodney W. Grimes ip->ip_src = satosin(&ia->ia_broadaddr)->sin_addr; 552df8bae1dSRodney W. Grimes else if (ia->ia_ifp->if_flags & IFF_POINTOPOINT) 553df8bae1dSRodney W. Grimes ip->ip_src = satosin(&ia->ia_dstaddr)->sin_addr; 554df8bae1dSRodney W. Grimes } 555df8bae1dSRodney W. Grimes reflect: 556df8bae1dSRodney W. Grimes ip->ip_len += hlen; /* since ip_input deducts this */ 557603724d3SBjoern A. Zeeb V_icmpstat.icps_reflect++; 558603724d3SBjoern A. Zeeb V_icmpstat.icps_outhist[icp->icmp_type]++; 559df8bae1dSRodney W. Grimes icmp_reflect(m); 560df8bae1dSRodney W. Grimes return; 561df8bae1dSRodney W. Grimes 562df8bae1dSRodney W. Grimes case ICMP_REDIRECT: 5638b615593SMarko Zec if (V_log_redirect) { 56418d3153eSDag-Erling Smørgrav u_long src, dst, gw; 56518d3153eSDag-Erling Smørgrav 56618d3153eSDag-Erling Smørgrav src = ntohl(ip->ip_src.s_addr); 56718d3153eSDag-Erling Smørgrav dst = ntohl(icp->icmp_ip.ip_dst.s_addr); 56818d3153eSDag-Erling Smørgrav gw = ntohl(icp->icmp_gwaddr.s_addr); 56918d3153eSDag-Erling Smørgrav printf("icmp redirect from %d.%d.%d.%d: " 57018d3153eSDag-Erling Smørgrav "%d.%d.%d.%d => %d.%d.%d.%d\n", 57118d3153eSDag-Erling Smørgrav (int)(src >> 24), (int)((src >> 16) & 0xff), 57218d3153eSDag-Erling Smørgrav (int)((src >> 8) & 0xff), (int)(src & 0xff), 57318d3153eSDag-Erling Smørgrav (int)(dst >> 24), (int)((dst >> 16) & 0xff), 57418d3153eSDag-Erling Smørgrav (int)((dst >> 8) & 0xff), (int)(dst & 0xff), 57518d3153eSDag-Erling Smørgrav (int)(gw >> 24), (int)((gw >> 16) & 0xff), 57618d3153eSDag-Erling Smørgrav (int)((gw >> 8) & 0xff), (int)(gw & 0xff)); 57718d3153eSDag-Erling Smørgrav } 57887c3bd27SAndre Oppermann /* 57987c3bd27SAndre Oppermann * RFC1812 says we must ignore ICMP redirects if we 58087c3bd27SAndre Oppermann * are acting as router. 58187c3bd27SAndre Oppermann */ 5828b615593SMarko Zec if (V_drop_redirect || V_ipforwarding) 58318d3153eSDag-Erling Smørgrav break; 584df8bae1dSRodney W. Grimes if (code > 3) 585df8bae1dSRodney W. Grimes goto badcode; 586df8bae1dSRodney W. Grimes if (icmplen < ICMP_ADVLENMIN || icmplen < ICMP_ADVLEN(icp) || 58753be11f6SPoul-Henning Kamp icp->icmp_ip.ip_hl < (sizeof(struct ip) >> 2)) { 588603724d3SBjoern A. Zeeb V_icmpstat.icps_badlen++; 589df8bae1dSRodney W. Grimes break; 590df8bae1dSRodney W. Grimes } 591df8bae1dSRodney W. Grimes /* 592df8bae1dSRodney W. Grimes * Short circuit routing redirects to force 593df8bae1dSRodney W. Grimes * immediate change in the kernel's routing 594df8bae1dSRodney W. Grimes * tables. The message is also handed to anyone 595df8bae1dSRodney W. Grimes * listening on a raw socket (e.g. the routing 596df8bae1dSRodney W. Grimes * daemon for use in updating its tables). 597df8bae1dSRodney W. Grimes */ 598df8bae1dSRodney W. Grimes icmpgw.sin_addr = ip->ip_src; 599df8bae1dSRodney W. Grimes icmpdst.sin_addr = icp->icmp_gwaddr; 600df8bae1dSRodney W. Grimes #ifdef ICMPPRINTFS 6012b758395SGarrett Wollman if (icmpprintfs) { 6022b758395SGarrett Wollman char buf[4 * sizeof "123"]; 6032b758395SGarrett Wollman strcpy(buf, inet_ntoa(icp->icmp_ip.ip_dst)); 6042b758395SGarrett Wollman 6052b758395SGarrett Wollman printf("redirect dst %s to %s\n", 6062b758395SGarrett Wollman buf, inet_ntoa(icp->icmp_gwaddr)); 6072b758395SGarrett Wollman } 608df8bae1dSRodney W. Grimes #endif 609df8bae1dSRodney W. Grimes icmpsrc.sin_addr = icp->icmp_ip.ip_dst; 6108b07e49aSJulian Elischer for ( fibnum = 0; fibnum < rt_numfibs; fibnum++) { 6118b07e49aSJulian Elischer in_rtredirect((struct sockaddr *)&icmpsrc, 612df8bae1dSRodney W. Grimes (struct sockaddr *)&icmpdst, 613df8bae1dSRodney W. Grimes (struct sockaddr *)0, RTF_GATEWAY | RTF_HOST, 6148b07e49aSJulian Elischer (struct sockaddr *)&icmpgw, fibnum); 6158b07e49aSJulian Elischer } 616df8bae1dSRodney W. Grimes pfctlinput(PRC_REDIRECT_HOST, (struct sockaddr *)&icmpsrc); 617b2630c29SGeorge V. Neville-Neil #ifdef IPSEC 6186a800098SYoshinobu Inoue key_sa_routechange((struct sockaddr *)&icmpsrc); 6196a800098SYoshinobu Inoue #endif 620df8bae1dSRodney W. Grimes break; 621df8bae1dSRodney W. Grimes 622df8bae1dSRodney W. Grimes /* 623df8bae1dSRodney W. Grimes * No kernel processing for the following; 624df8bae1dSRodney W. Grimes * just fall through to send to raw listener. 625df8bae1dSRodney W. Grimes */ 626df8bae1dSRodney W. Grimes case ICMP_ECHOREPLY: 627df8bae1dSRodney W. Grimes case ICMP_ROUTERADVERT: 628df8bae1dSRodney W. Grimes case ICMP_ROUTERSOLICIT: 629df8bae1dSRodney W. Grimes case ICMP_TSTAMPREPLY: 630df8bae1dSRodney W. Grimes case ICMP_IREQREPLY: 631df8bae1dSRodney W. Grimes case ICMP_MASKREPLY: 632df8bae1dSRodney W. Grimes default: 633df8bae1dSRodney W. Grimes break; 634df8bae1dSRodney W. Grimes } 635df8bae1dSRodney W. Grimes 636df8bae1dSRodney W. Grimes raw: 637f0ffb944SJulian Elischer rip_input(m, off); 638df8bae1dSRodney W. Grimes return; 639df8bae1dSRodney W. Grimes 640df8bae1dSRodney W. Grimes freeit: 641df8bae1dSRodney W. Grimes m_freem(m); 642df8bae1dSRodney W. Grimes } 643df8bae1dSRodney W. Grimes 644df8bae1dSRodney W. Grimes /* 645df8bae1dSRodney W. Grimes * Reflect the ip packet back to the source 646df8bae1dSRodney W. Grimes */ 6470312fbe9SPoul-Henning Kamp static void 648f2565d68SRobert Watson icmp_reflect(struct mbuf *m) 649df8bae1dSRodney W. Grimes { 6508b615593SMarko Zec INIT_VNET_INET(curvnet); 651ca925d9cSJonathan Lemon struct ip *ip = mtod(m, struct ip *); 652ca925d9cSJonathan Lemon struct ifaddr *ifa; 653b74d89bbSAndre Oppermann struct ifnet *ifn; 654ca925d9cSJonathan Lemon struct in_ifaddr *ia; 655df8bae1dSRodney W. Grimes struct in_addr t; 656b5e8ce9fSBruce Evans struct mbuf *opts = 0; 65753be11f6SPoul-Henning Kamp int optlen = (ip->ip_hl << 2) - sizeof(struct ip); 658df8bae1dSRodney W. Grimes 6596b9ff6b7SGeorge V. Neville-Neil if (IN_MULTICAST(ntohl(ip->ip_src.s_addr)) || 6606b9ff6b7SGeorge V. Neville-Neil IN_EXPERIMENTAL(ntohl(ip->ip_src.s_addr)) || 6616b9ff6b7SGeorge V. Neville-Neil IN_ZERONET(ntohl(ip->ip_src.s_addr)) ) { 662df8bae1dSRodney W. Grimes m_freem(m); /* Bad return address */ 663603724d3SBjoern A. Zeeb V_icmpstat.icps_badaddr++; 664df8bae1dSRodney W. Grimes goto done; /* Ip_output() will check for broadcast */ 665df8bae1dSRodney W. Grimes } 6666b9ff6b7SGeorge V. Neville-Neil 667df8bae1dSRodney W. Grimes t = ip->ip_dst; 668df8bae1dSRodney W. Grimes ip->ip_dst = ip->ip_src; 6691488eac8SAndre Oppermann 670df8bae1dSRodney W. Grimes /* 6711488eac8SAndre Oppermann * Source selection for ICMP replies: 6721488eac8SAndre Oppermann * 6731488eac8SAndre Oppermann * If the incoming packet was addressed directly to one of our 6741488eac8SAndre Oppermann * own addresses, use dst as the src for the reply. 675df8bae1dSRodney W. Grimes */ 676ca925d9cSJonathan Lemon LIST_FOREACH(ia, INADDR_HASH(t.s_addr), ia_hash) 677df8bae1dSRodney W. Grimes if (t.s_addr == IA_SIN(ia)->sin_addr.s_addr) 678ca925d9cSJonathan Lemon goto match; 6791488eac8SAndre Oppermann /* 6801488eac8SAndre Oppermann * If the incoming packet was addressed to one of our broadcast 6811488eac8SAndre Oppermann * addresses, use the first non-broadcast address which corresponds 6821488eac8SAndre Oppermann * to the incoming interface. 6831488eac8SAndre Oppermann */ 68484caf008SRuslan Ermilov if (m->m_pkthdr.rcvif != NULL && 68584caf008SRuslan Ermilov m->m_pkthdr.rcvif->if_flags & IFF_BROADCAST) { 686ca925d9cSJonathan Lemon TAILQ_FOREACH(ifa, &m->m_pkthdr.rcvif->if_addrhead, ifa_link) { 687ca925d9cSJonathan Lemon if (ifa->ifa_addr->sa_family != AF_INET) 688ca925d9cSJonathan Lemon continue; 689ca925d9cSJonathan Lemon ia = ifatoia(ifa); 690ca925d9cSJonathan Lemon if (satosin(&ia->ia_broadaddr)->sin_addr.s_addr == 691ca925d9cSJonathan Lemon t.s_addr) 692ca925d9cSJonathan Lemon goto match; 693df8bae1dSRodney W. Grimes } 694ca925d9cSJonathan Lemon } 6951488eac8SAndre Oppermann /* 696a0866c8dSAndre Oppermann * If the packet was transiting through us, use the address of 697a0866c8dSAndre Oppermann * the interface the packet came through in. If that interface 698a0866c8dSAndre Oppermann * doesn't have a suitable IP address, the normal selection 699a0866c8dSAndre Oppermann * criteria apply. 700a0866c8dSAndre Oppermann */ 7018b615593SMarko Zec if (V_icmp_rfi && m->m_pkthdr.rcvif != NULL) { 702a0866c8dSAndre Oppermann TAILQ_FOREACH(ifa, &m->m_pkthdr.rcvif->if_addrhead, ifa_link) { 703a0866c8dSAndre Oppermann if (ifa->ifa_addr->sa_family != AF_INET) 704a0866c8dSAndre Oppermann continue; 705a0866c8dSAndre Oppermann ia = ifatoia(ifa); 706a0866c8dSAndre Oppermann goto match; 707a0866c8dSAndre Oppermann } 708a0866c8dSAndre Oppermann } 709a0866c8dSAndre Oppermann /* 710b74d89bbSAndre Oppermann * If the incoming packet was not addressed directly to us, use 711b74d89bbSAndre Oppermann * designated interface for icmp replies specified by sysctl 712b74d89bbSAndre Oppermann * net.inet.icmp.reply_src (default not set). Otherwise continue 713b74d89bbSAndre Oppermann * with normal source selection. 714b74d89bbSAndre Oppermann */ 7158b615593SMarko Zec if (V_reply_src[0] != '\0' && (ifn = ifunit(V_reply_src))) { 716b74d89bbSAndre Oppermann TAILQ_FOREACH(ifa, &ifn->if_addrhead, ifa_link) { 717b74d89bbSAndre Oppermann if (ifa->ifa_addr->sa_family != AF_INET) 718b74d89bbSAndre Oppermann continue; 719b74d89bbSAndre Oppermann ia = ifatoia(ifa); 720b74d89bbSAndre Oppermann goto match; 721b74d89bbSAndre Oppermann } 722b74d89bbSAndre Oppermann } 723b74d89bbSAndre Oppermann /* 7241488eac8SAndre Oppermann * If the packet was transiting through us, use the address of 7251488eac8SAndre Oppermann * the interface that is the closest to the packet source. 7261488eac8SAndre Oppermann * When we don't have a route back to the packet source, stop here 7271488eac8SAndre Oppermann * and drop the packet. 7281488eac8SAndre Oppermann */ 7298b07e49aSJulian Elischer ia = ip_rtaddr(ip->ip_dst, M_GETFIB(m)); 7300d4bef5dSDima Dorfman if (ia == NULL) { 7310d4bef5dSDima Dorfman m_freem(m); 732603724d3SBjoern A. Zeeb V_icmpstat.icps_noroute++; 7330d4bef5dSDima Dorfman goto done; 7340d4bef5dSDima Dorfman } 735ca925d9cSJonathan Lemon match: 736baee0c3eSRobert Watson #ifdef MAC 737a13e21f7SRobert Watson mac_netinet_icmp_replyinplace(m); 738baee0c3eSRobert Watson #endif 739df8bae1dSRodney W. Grimes t = IA_SIN(ia)->sin_addr; 740df8bae1dSRodney W. Grimes ip->ip_src = t; 741603724d3SBjoern A. Zeeb ip->ip_ttl = V_ip_defttl; 742df8bae1dSRodney W. Grimes 743df8bae1dSRodney W. Grimes if (optlen > 0) { 744df8bae1dSRodney W. Grimes register u_char *cp; 745df8bae1dSRodney W. Grimes int opt, cnt; 746df8bae1dSRodney W. Grimes u_int len; 747df8bae1dSRodney W. Grimes 748df8bae1dSRodney W. Grimes /* 749df8bae1dSRodney W. Grimes * Retrieve any source routing from the incoming packet; 750df8bae1dSRodney W. Grimes * add on any record-route or timestamp options. 751df8bae1dSRodney W. Grimes */ 752df8bae1dSRodney W. Grimes cp = (u_char *) (ip + 1); 753e0982661SAndre Oppermann if ((opts = ip_srcroute(m)) == 0 && 75434333b16SAndre Oppermann (opts = m_gethdr(M_DONTWAIT, MT_DATA))) { 755df8bae1dSRodney W. Grimes opts->m_len = sizeof(struct in_addr); 756df8bae1dSRodney W. Grimes mtod(opts, struct in_addr *)->s_addr = 0; 757df8bae1dSRodney W. Grimes } 758df8bae1dSRodney W. Grimes if (opts) { 759df8bae1dSRodney W. Grimes #ifdef ICMPPRINTFS 760df8bae1dSRodney W. Grimes if (icmpprintfs) 761df8bae1dSRodney W. Grimes printf("icmp_reflect optlen %d rt %d => ", 762df8bae1dSRodney W. Grimes optlen, opts->m_len); 763df8bae1dSRodney W. Grimes #endif 764df8bae1dSRodney W. Grimes for (cnt = optlen; cnt > 0; cnt -= len, cp += len) { 765df8bae1dSRodney W. Grimes opt = cp[IPOPT_OPTVAL]; 766df8bae1dSRodney W. Grimes if (opt == IPOPT_EOL) 767df8bae1dSRodney W. Grimes break; 768df8bae1dSRodney W. Grimes if (opt == IPOPT_NOP) 769df8bae1dSRodney W. Grimes len = 1; 770df8bae1dSRodney W. Grimes else { 771707d00a3SJonathan Lemon if (cnt < IPOPT_OLEN + sizeof(*cp)) 772707d00a3SJonathan Lemon break; 773df8bae1dSRodney W. Grimes len = cp[IPOPT_OLEN]; 774707d00a3SJonathan Lemon if (len < IPOPT_OLEN + sizeof(*cp) || 775707d00a3SJonathan Lemon len > cnt) 776df8bae1dSRodney W. Grimes break; 777df8bae1dSRodney W. Grimes } 778df8bae1dSRodney W. Grimes /* 779df8bae1dSRodney W. Grimes * Should check for overflow, but it "can't happen" 780df8bae1dSRodney W. Grimes */ 781df8bae1dSRodney W. Grimes if (opt == IPOPT_RR || opt == IPOPT_TS || 782df8bae1dSRodney W. Grimes opt == IPOPT_SECURITY) { 783df8bae1dSRodney W. Grimes bcopy((caddr_t)cp, 784df8bae1dSRodney W. Grimes mtod(opts, caddr_t) + opts->m_len, len); 785df8bae1dSRodney W. Grimes opts->m_len += len; 786df8bae1dSRodney W. Grimes } 787df8bae1dSRodney W. Grimes } 788df8bae1dSRodney W. Grimes /* Terminate & pad, if necessary */ 789623ae52eSPoul-Henning Kamp cnt = opts->m_len % 4; 790623ae52eSPoul-Henning Kamp if (cnt) { 791df8bae1dSRodney W. Grimes for (; cnt < 4; cnt++) { 792df8bae1dSRodney W. Grimes *(mtod(opts, caddr_t) + opts->m_len) = 793df8bae1dSRodney W. Grimes IPOPT_EOL; 794df8bae1dSRodney W. Grimes opts->m_len++; 795df8bae1dSRodney W. Grimes } 796df8bae1dSRodney W. Grimes } 797df8bae1dSRodney W. Grimes #ifdef ICMPPRINTFS 798df8bae1dSRodney W. Grimes if (icmpprintfs) 799df8bae1dSRodney W. Grimes printf("%d\n", opts->m_len); 800df8bae1dSRodney W. Grimes #endif 801df8bae1dSRodney W. Grimes } 802df8bae1dSRodney W. Grimes /* 803df8bae1dSRodney W. Grimes * Now strip out original options by copying rest of first 804df8bae1dSRodney W. Grimes * mbuf's data back, and adjust the IP length. 805df8bae1dSRodney W. Grimes */ 806df8bae1dSRodney W. Grimes ip->ip_len -= optlen; 80753be11f6SPoul-Henning Kamp ip->ip_v = IPVERSION; 80853be11f6SPoul-Henning Kamp ip->ip_hl = 5; 809df8bae1dSRodney W. Grimes m->m_len -= optlen; 810df8bae1dSRodney W. Grimes if (m->m_flags & M_PKTHDR) 811df8bae1dSRodney W. Grimes m->m_pkthdr.len -= optlen; 812df8bae1dSRodney W. Grimes optlen += sizeof(struct ip); 813df8bae1dSRodney W. Grimes bcopy((caddr_t)ip + optlen, (caddr_t)(ip + 1), 814df8bae1dSRodney W. Grimes (unsigned)(m->m_len - sizeof(struct ip))); 815df8bae1dSRodney W. Grimes } 8169c855a36SSam Leffler m_tag_delete_nonpersistent(m); 817df8bae1dSRodney W. Grimes m->m_flags &= ~(M_BCAST|M_MCAST); 81802c1c707SAndre Oppermann icmp_send(m, opts); 819df8bae1dSRodney W. Grimes done: 820df8bae1dSRodney W. Grimes if (opts) 821df8bae1dSRodney W. Grimes (void)m_free(opts); 822df8bae1dSRodney W. Grimes } 823df8bae1dSRodney W. Grimes 824df8bae1dSRodney W. Grimes /* 825df8bae1dSRodney W. Grimes * Send an icmp packet back to the ip level, 826df8bae1dSRodney W. Grimes * after supplying a checksum. 827df8bae1dSRodney W. Grimes */ 8280312fbe9SPoul-Henning Kamp static void 829f2565d68SRobert Watson icmp_send(struct mbuf *m, struct mbuf *opts) 830df8bae1dSRodney W. Grimes { 831df8bae1dSRodney W. Grimes register struct ip *ip = mtod(m, struct ip *); 832df8bae1dSRodney W. Grimes register int hlen; 833df8bae1dSRodney W. Grimes register struct icmp *icp; 834df8bae1dSRodney W. Grimes 83553be11f6SPoul-Henning Kamp hlen = ip->ip_hl << 2; 836df8bae1dSRodney W. Grimes m->m_data += hlen; 837df8bae1dSRodney W. Grimes m->m_len -= hlen; 838df8bae1dSRodney W. Grimes icp = mtod(m, struct icmp *); 839df8bae1dSRodney W. Grimes icp->icmp_cksum = 0; 840df8bae1dSRodney W. Grimes icp->icmp_cksum = in_cksum(m, ip->ip_len - hlen); 841df8bae1dSRodney W. Grimes m->m_data -= hlen; 842df8bae1dSRodney W. Grimes m->m_len += hlen; 84394446a2eSArchie Cobbs m->m_pkthdr.rcvif = (struct ifnet *)0; 844df8bae1dSRodney W. Grimes #ifdef ICMPPRINTFS 8452b758395SGarrett Wollman if (icmpprintfs) { 8462b758395SGarrett Wollman char buf[4 * sizeof "123"]; 8472b758395SGarrett Wollman strcpy(buf, inet_ntoa(ip->ip_dst)); 8482b758395SGarrett Wollman printf("icmp_send dst %s src %s\n", 8492b758395SGarrett Wollman buf, inet_ntoa(ip->ip_src)); 8502b758395SGarrett Wollman } 851df8bae1dSRodney W. Grimes #endif 85202c1c707SAndre Oppermann (void) ip_output(m, opts, NULL, 0, NULL, NULL); 853df8bae1dSRodney W. Grimes } 854df8bae1dSRodney W. Grimes 855d685b6eeSLuigi Rizzo /* 856d685b6eeSLuigi Rizzo * Return milliseconds since 00:00 GMT in network format. 857d685b6eeSLuigi Rizzo */ 858d685b6eeSLuigi Rizzo uint32_t 859f2565d68SRobert Watson iptime(void) 860df8bae1dSRodney W. Grimes { 861df8bae1dSRodney W. Grimes struct timeval atv; 862df8bae1dSRodney W. Grimes u_long t; 863df8bae1dSRodney W. Grimes 86416cd6db0SBill Fumerola getmicrotime(&atv); 865df8bae1dSRodney W. Grimes t = (atv.tv_sec % (24*60*60)) * 1000 + atv.tv_usec / 1000; 866df8bae1dSRodney W. Grimes return (htonl(t)); 867df8bae1dSRodney W. Grimes } 868df8bae1dSRodney W. Grimes 8695cbf3e08SGarrett Wollman /* 8705cbf3e08SGarrett Wollman * Return the next larger or smaller MTU plateau (table from RFC 1191) 8715cbf3e08SGarrett Wollman * given current value MTU. If DIR is less than zero, a larger plateau 8725cbf3e08SGarrett Wollman * is returned; otherwise, a smaller value is returned. 8735cbf3e08SGarrett Wollman */ 8741aedbd9cSAndre Oppermann int 875f2565d68SRobert Watson ip_next_mtu(int mtu, int dir) 8765cbf3e08SGarrett Wollman { 8775cbf3e08SGarrett Wollman static int mtutab[] = { 8784c037f8dSAndre Oppermann 65535, 32000, 17914, 8166, 4352, 2002, 1492, 1280, 1006, 508, 8794c037f8dSAndre Oppermann 296, 68, 0 8805cbf3e08SGarrett Wollman }; 88106003a1eSAndre Oppermann int i, size; 8825cbf3e08SGarrett Wollman 88306003a1eSAndre Oppermann size = (sizeof mtutab) / (sizeof mtutab[0]); 88406003a1eSAndre Oppermann if (dir >= 0) { 8851c0b0f52SGleb Smirnoff for (i = 0; i < size; i++) 88606003a1eSAndre Oppermann if (mtu > mtutab[i]) 8875cbf3e08SGarrett Wollman return mtutab[i]; 8885cbf3e08SGarrett Wollman } else { 88906003a1eSAndre Oppermann for (i = size - 1; i >= 0; i--) 89006003a1eSAndre Oppermann if (mtu < mtutab[i]) 89106003a1eSAndre Oppermann return mtutab[i]; 89206003a1eSAndre Oppermann if (mtu == mtutab[0]) 89306003a1eSAndre Oppermann return mtutab[0]; 8945cbf3e08SGarrett Wollman } 89506003a1eSAndre Oppermann return 0; 8965cbf3e08SGarrett Wollman } 89751508de1SMatthew Dillon 89851508de1SMatthew Dillon 89951508de1SMatthew Dillon /* 90051508de1SMatthew Dillon * badport_bandlim() - check for ICMP bandwidth limit 90151508de1SMatthew Dillon * 90251508de1SMatthew Dillon * Return 0 if it is ok to send an ICMP error response, -1 if we have 90351508de1SMatthew Dillon * hit our bandwidth limit and it is not ok. 90451508de1SMatthew Dillon * 90551508de1SMatthew Dillon * If icmplim is <= 0, the feature is disabled and 0 is returned. 90651508de1SMatthew Dillon * 90751508de1SMatthew Dillon * For now we separate the TCP and UDP subsystems w/ different 'which' 90851508de1SMatthew Dillon * values. We may eventually remove this separation (and simplify the 90951508de1SMatthew Dillon * code further). 91051508de1SMatthew Dillon * 91151508de1SMatthew Dillon * Note that the printing of the error message is delayed so we can 91251508de1SMatthew Dillon * properly print the icmp error rate that the system was trying to do 91351508de1SMatthew Dillon * (i.e. 22000/100 pps, etc...). This can cause long delays in printing 91451508de1SMatthew Dillon * the 'final' error, but it doesn't make sense to solve the printing 91551508de1SMatthew Dillon * delay with more complex code. 91651508de1SMatthew Dillon */ 91751508de1SMatthew Dillon 91851508de1SMatthew Dillon int 91951508de1SMatthew Dillon badport_bandlim(int which) 92051508de1SMatthew Dillon { 9218b615593SMarko Zec INIT_VNET_INET(curvnet); 9228b615593SMarko Zec 92300f21882SSam Leffler #define N(a) (sizeof (a) / sizeof (a[0])) 92400f21882SSam Leffler static struct rate { 92500f21882SSam Leffler const char *type; 92600f21882SSam Leffler struct timeval lasttime; 927439dfb0cSStefan Farfeleder int curpps; 92800f21882SSam Leffler } rates[BANDLIM_MAX+1] = { 92900f21882SSam Leffler { "icmp unreach response" }, 93000f21882SSam Leffler { "icmp ping response" }, 93100f21882SSam Leffler { "icmp tstamp response" }, 93200f21882SSam Leffler { "closed port RST response" }, 93308af97b7SRobert Watson { "open port RST response" }, 93408af97b7SRobert Watson { "icmp6 unreach response" } 93509f81a46SBosko Milekic }; 93651508de1SMatthew Dillon 93751508de1SMatthew Dillon /* 93800f21882SSam Leffler * Return ok status if feature disabled or argument out of range. 93951508de1SMatthew Dillon */ 9408b615593SMarko Zec if (V_icmplim > 0 && (u_int) which < N(rates)) { 94100f21882SSam Leffler struct rate *r = &rates[which]; 94200f21882SSam Leffler int opps = r->curpps; 94351508de1SMatthew Dillon 9448b615593SMarko Zec if (!ppsratecheck(&r->lasttime, &r->curpps, V_icmplim)) 94500f21882SSam Leffler return -1; /* discard packet */ 94651508de1SMatthew Dillon /* 94700f21882SSam Leffler * If we've dropped below the threshold after having 94800f21882SSam Leffler * rate-limited traffic print the message. This preserves 94900f21882SSam Leffler * the previous behaviour at the expense of added complexity. 95051508de1SMatthew Dillon */ 9518b615593SMarko Zec if (V_icmplim_output && opps > V_icmplim) 95200f21882SSam Leffler printf("Limiting %s from %d to %d packets/sec\n", 9538b615593SMarko Zec r->type, opps, V_icmplim); 95451508de1SMatthew Dillon } 95500f21882SSam Leffler return 0; /* okay to send packet */ 95600f21882SSam Leffler #undef N 95751508de1SMatthew Dillon } 958