1c398230bSWarner Losh /*- 251369649SPedro F. Giffuni * SPDX-License-Identifier: BSD-3-Clause 351369649SPedro F. Giffuni * 4df8bae1dSRodney W. Grimes * Copyright (c) 1982, 1986, 1988, 1993 5df8bae1dSRodney W. Grimes * The Regents of the University of California. All rights reserved. 6df8bae1dSRodney W. Grimes * 7df8bae1dSRodney W. Grimes * Redistribution and use in source and binary forms, with or without 8df8bae1dSRodney W. Grimes * modification, are permitted provided that the following conditions 9df8bae1dSRodney W. Grimes * are met: 10df8bae1dSRodney W. Grimes * 1. Redistributions of source code must retain the above copyright 11df8bae1dSRodney W. Grimes * notice, this list of conditions and the following disclaimer. 12df8bae1dSRodney W. Grimes * 2. Redistributions in binary form must reproduce the above copyright 13df8bae1dSRodney W. Grimes * notice, this list of conditions and the following disclaimer in the 14df8bae1dSRodney W. Grimes * documentation and/or other materials provided with the distribution. 15fbbd9655SWarner Losh * 3. Neither the name of the University nor the names of its contributors 16df8bae1dSRodney W. Grimes * may be used to endorse or promote products derived from this software 17df8bae1dSRodney W. Grimes * without specific prior written permission. 18df8bae1dSRodney W. Grimes * 19df8bae1dSRodney W. Grimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 20df8bae1dSRodney W. Grimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 21df8bae1dSRodney W. Grimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 22df8bae1dSRodney W. Grimes * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 23df8bae1dSRodney W. Grimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 24df8bae1dSRodney W. Grimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 25df8bae1dSRodney W. Grimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 26df8bae1dSRodney W. Grimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 27df8bae1dSRodney W. Grimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 28df8bae1dSRodney W. Grimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 29df8bae1dSRodney W. Grimes * SUCH DAMAGE. 30df8bae1dSRodney W. Grimes */ 31df8bae1dSRodney W. Grimes 324b421e2dSMike Silbersack #include <sys/cdefs.h> 33b8e463e6SBjoern A. Zeeb #include "opt_inet.h" 346a800098SYoshinobu Inoue 35df8bae1dSRodney W. Grimes #include <sys/param.h> 36df8bae1dSRodney W. Grimes #include <sys/systm.h> 37df8bae1dSRodney W. Grimes #include <sys/mbuf.h> 38df8bae1dSRodney W. Grimes #include <sys/protosw.h> 39df8bae1dSRodney W. Grimes #include <sys/socket.h> 40df8bae1dSRodney W. Grimes #include <sys/time.h> 41df8bae1dSRodney W. Grimes #include <sys/kernel.h> 42cc0a3c8cSAndrey V. Elsukov #include <sys/lock.h> 43b5e8ce9fSBruce Evans #include <sys/sysctl.h> 4440fe9effSAndre Oppermann #include <sys/syslog.h> 45df8bae1dSRodney W. Grimes 46df8bae1dSRodney W. Grimes #include <net/if.h> 4776039bc8SGleb Smirnoff #include <net/if_var.h> 483d0d5b21SJustin Hibbits #include <net/if_private.h> 499494d596SBrooks Davis #include <net/if_types.h> 50df8bae1dSRodney W. Grimes #include <net/route.h> 51da187ddbSAlexander V. Chernikov #include <net/route/route_ctl.h> 529ac7c6cfSAlexander V. Chernikov #include <net/route/nhop.h> 53eddfbb76SRobert Watson #include <net/vnet.h> 54df8bae1dSRodney W. Grimes 55df8bae1dSRodney W. Grimes #include <netinet/in.h> 569977be4aSAlexander V. Chernikov #include <netinet/in_fib.h> 5797d8d152SAndre Oppermann #include <netinet/in_pcb.h> 58df8bae1dSRodney W. Grimes #include <netinet/in_systm.h> 59df8bae1dSRodney W. Grimes #include <netinet/in_var.h> 60df8bae1dSRodney W. Grimes #include <netinet/ip.h> 61df8bae1dSRodney W. Grimes #include <netinet/ip_icmp.h> 62b5e8ce9fSBruce Evans #include <netinet/ip_var.h> 63ef39adf0SAndre Oppermann #include <netinet/ip_options.h> 64f88d0cfeSMichael Tuexen #include <netinet/sctp.h> 6597d8d152SAndre Oppermann #include <netinet/tcp.h> 6697d8d152SAndre Oppermann #include <netinet/tcpip.h> 67df8bae1dSRodney W. Grimes #include <netinet/icmp_var.h> 68df8bae1dSRodney W. Grimes 69b8e463e6SBjoern A. Zeeb #ifdef INET 70b9234fafSSam Leffler 7172a52a35SJonathan Lemon #include <machine/in_cksum.h> 7272a52a35SJonathan Lemon 73aed55708SRobert Watson #include <security/mac/mac_framework.h> 74b8e463e6SBjoern A. Zeeb #endif /* INET */ 75aed55708SRobert Watson 7678b1fc05SGleb Smirnoff extern ipproto_ctlinput_t *ip_ctlprotox[]; 7778b1fc05SGleb Smirnoff 78df8bae1dSRodney W. Grimes /* 79df8bae1dSRodney W. Grimes * ICMP routines: error generation, receive packet processing, and 80df8bae1dSRodney W. Grimes * routines to turnaround packets back to the originator, and 81df8bae1dSRodney W. Grimes * host table maintenance routines. 82df8bae1dSRodney W. Grimes */ 83ac44739fSGleb Smirnoff static int sysctl_icmplim_and_jitter(SYSCTL_HANDLER_ARGS); 84ac44739fSGleb Smirnoff VNET_DEFINE_STATIC(u_int, icmplim) = 200; 85b8e463e6SBjoern A. Zeeb #define V_icmplim VNET(icmplim) 86ac44739fSGleb Smirnoff SYSCTL_PROC(_net_inet_icmp, ICMPCTL_ICMPLIM, icmplim, CTLTYPE_UINT | 87ac44739fSGleb Smirnoff CTLFLAG_VNET | CTLFLAG_RW, &VNET_NAME(icmplim), 0, 88ac44739fSGleb Smirnoff &sysctl_icmplim_and_jitter, "IU", 89b8e463e6SBjoern A. Zeeb "Maximum number of ICMP responses per second"); 90b8e463e6SBjoern A. Zeeb 91923c223fSMichael Tuexen VNET_DEFINE_STATIC(int, icmplim_curr_jitter[BANDLIM_MAX]) = {0}; 92ca4cd20cSGeorge V. Neville-Neil #define V_icmplim_curr_jitter VNET(icmplim_curr_jitter) 93ac44739fSGleb Smirnoff VNET_DEFINE_STATIC(u_int, icmplim_jitter) = 16; 94ca4cd20cSGeorge V. Neville-Neil #define V_icmplim_jitter VNET(icmplim_jitter) 95ac44739fSGleb Smirnoff SYSCTL_PROC(_net_inet_icmp, OID_AUTO, icmplim_jitter, CTLTYPE_UINT | 96ac44739fSGleb Smirnoff CTLFLAG_VNET | CTLFLAG_RW, &VNET_NAME(icmplim_jitter), 0, 97ac44739fSGleb Smirnoff &sysctl_icmplim_and_jitter, "IU", 98ca4cd20cSGeorge V. Neville-Neil "Random icmplim jitter adjustment limit"); 99ca4cd20cSGeorge V. Neville-Neil 1005f901c92SAndrew Turner VNET_DEFINE_STATIC(int, icmplim_output) = 1; 101b8e463e6SBjoern A. Zeeb #define V_icmplim_output VNET(icmplim_output) 1026df8a710SGleb Smirnoff SYSCTL_INT(_net_inet_icmp, OID_AUTO, icmplim_output, CTLFLAG_VNET | CTLFLAG_RW, 103b8e463e6SBjoern A. Zeeb &VNET_NAME(icmplim_output), 0, 104316efdb3SEitan Adler "Enable logging of ICMP response rate limiting"); 105b8e463e6SBjoern A. Zeeb 106b8e463e6SBjoern A. Zeeb #ifdef INET 1075b7cb97cSAndrey V. Elsukov VNET_PCPUSTAT_DEFINE(struct icmpstat, icmpstat); 1085b7cb97cSAndrey V. Elsukov VNET_PCPUSTAT_SYSINIT(icmpstat); 1095b7cb97cSAndrey V. Elsukov SYSCTL_VNET_PCPUSTAT(_net_inet_icmp, ICMPCTL_STATS, stats, struct icmpstat, 1105b7cb97cSAndrey V. Elsukov icmpstat, "ICMP statistics (struct icmpstat, netinet/icmp_var.h)"); 1115b7cb97cSAndrey V. Elsukov 1125b7cb97cSAndrey V. Elsukov #ifdef VIMAGE 1135b7cb97cSAndrey V. Elsukov VNET_PCPUSTAT_SYSUNINIT(icmpstat); 1145b7cb97cSAndrey V. Elsukov #endif /* VIMAGE */ 115eddfbb76SRobert Watson 1165f901c92SAndrew Turner VNET_DEFINE_STATIC(int, icmpmaskrepl) = 0; 11782cea7e6SBjoern A. Zeeb #define V_icmpmaskrepl VNET(icmpmaskrepl) 1186df8a710SGleb Smirnoff SYSCTL_INT(_net_inet_icmp, ICMPCTL_MASKREPL, maskrepl, CTLFLAG_VNET | CTLFLAG_RW, 119eddfbb76SRobert Watson &VNET_NAME(icmpmaskrepl), 0, 120e8800f3cSMark Johnston "Reply to ICMP Address Mask Request packets"); 12157842a38SMatthew N. Dodd 1225f901c92SAndrew Turner VNET_DEFINE_STATIC(u_int, icmpmaskfake) = 0; 12382cea7e6SBjoern A. Zeeb #define V_icmpmaskfake VNET(icmpmaskfake) 1246df8a710SGleb Smirnoff SYSCTL_UINT(_net_inet_icmp, OID_AUTO, maskfake, CTLFLAG_VNET | CTLFLAG_RW, 125eddfbb76SRobert Watson &VNET_NAME(icmpmaskfake), 0, 126e8800f3cSMark Johnston "Fake reply to ICMP Address Mask Request packets"); 1270312fbe9SPoul-Henning Kamp 1283c2824b9SAlexander V. Chernikov VNET_DEFINE(int, drop_redirect) = 0; 129670e8b3bSAlexander V. Chernikov #define V_drop_redirect VNET(drop_redirect) 130670e8b3bSAlexander V. Chernikov SYSCTL_INT(_net_inet_icmp, OID_AUTO, drop_redirect, CTLFLAG_VNET | CTLFLAG_RW, 131e8800f3cSMark Johnston &VNET_NAME(drop_redirect), 0, 132e8800f3cSMark Johnston "Ignore ICMP redirects"); 13318d3153eSDag-Erling Smørgrav 1345f901c92SAndrew Turner VNET_DEFINE_STATIC(int, log_redirect) = 0; 13582cea7e6SBjoern A. Zeeb #define V_log_redirect VNET(log_redirect) 1366df8a710SGleb Smirnoff SYSCTL_INT(_net_inet_icmp, OID_AUTO, log_redirect, CTLFLAG_VNET | CTLFLAG_RW, 137eddfbb76SRobert Watson &VNET_NAME(log_redirect), 0, 138eddfbb76SRobert Watson "Log ICMP redirects to the console"); 1396c3b5f69SDag-Erling Smørgrav 14034a5582cSAlexander V. Chernikov VNET_DEFINE_STATIC(int, redirtimeout) = 60 * 10; /* 10 minutes */ 14134a5582cSAlexander V. Chernikov #define V_redirtimeout VNET(redirtimeout) 14234a5582cSAlexander V. Chernikov SYSCTL_INT(_net_inet_icmp, OID_AUTO, redirtimeout, CTLFLAG_VNET | CTLFLAG_RW, 14334a5582cSAlexander V. Chernikov &VNET_NAME(redirtimeout), 0, 14434a5582cSAlexander V. Chernikov "Delay in seconds before expiring redirect route"); 14534a5582cSAlexander V. Chernikov 1465f901c92SAndrew Turner VNET_DEFINE_STATIC(char, reply_src[IFNAMSIZ]); 14782cea7e6SBjoern A. Zeeb #define V_reply_src VNET(reply_src) 1486df8a710SGleb Smirnoff SYSCTL_STRING(_net_inet_icmp, OID_AUTO, reply_src, CTLFLAG_VNET | CTLFLAG_RW, 149eddfbb76SRobert Watson &VNET_NAME(reply_src), IFNAMSIZ, 150e8800f3cSMark Johnston "ICMP reply source for non-local packets"); 151b74d89bbSAndre Oppermann 1525f901c92SAndrew Turner VNET_DEFINE_STATIC(int, icmp_rfi) = 0; 15382cea7e6SBjoern A. Zeeb #define V_icmp_rfi VNET(icmp_rfi) 1546df8a710SGleb Smirnoff SYSCTL_INT(_net_inet_icmp, OID_AUTO, reply_from_interface, CTLFLAG_VNET | CTLFLAG_RW, 155eddfbb76SRobert Watson &VNET_NAME(icmp_rfi), 0, 156eddfbb76SRobert Watson "ICMP reply from incoming interface for non-local packets"); 1578de9ac5eSRandall Stewart /* Router requirements RFC 1812 section 4.3.2.3 requires 576 - 28. */ 1581e0582fdSAndrew Turner VNET_DEFINE_STATIC(int, icmp_quotelen) = 548; 15982cea7e6SBjoern A. Zeeb #define V_icmp_quotelen VNET(icmp_quotelen) 1606df8a710SGleb Smirnoff SYSCTL_INT(_net_inet_icmp, OID_AUTO, quotelen, CTLFLAG_VNET | CTLFLAG_RW, 161eddfbb76SRobert Watson &VNET_NAME(icmp_quotelen), 0, 162eddfbb76SRobert Watson "Number of bytes from original packet to quote in ICMP reply"); 163e875dfb8SAndre Oppermann 1645f901c92SAndrew Turner VNET_DEFINE_STATIC(int, icmpbmcastecho) = 0; 16582cea7e6SBjoern A. Zeeb #define V_icmpbmcastecho VNET(icmpbmcastecho) 1666df8a710SGleb Smirnoff SYSCTL_INT(_net_inet_icmp, OID_AUTO, bmcastecho, CTLFLAG_VNET | CTLFLAG_RW, 167eddfbb76SRobert Watson &VNET_NAME(icmpbmcastecho), 0, 168e8800f3cSMark Johnston "Reply to multicast ICMP Echo Request and Timestamp packets"); 1697022ea0aSGarrett Wollman 1705f901c92SAndrew Turner VNET_DEFINE_STATIC(int, icmptstamprepl) = 1; 17100cb6befSMark Johnston #define V_icmptstamprepl VNET(icmptstamprepl) 1727762bbc3SMark Johnston SYSCTL_INT(_net_inet_icmp, OID_AUTO, tstamprepl, CTLFLAG_VNET | CTLFLAG_RW, 173e8800f3cSMark Johnston &VNET_NAME(icmptstamprepl), 0, 174e8800f3cSMark Johnston "Respond to ICMP Timestamp packets"); 17551508de1SMatthew Dillon 176410634efSEugene Grosbein VNET_DEFINE_STATIC(int, error_keeptags) = 0; 177410634efSEugene Grosbein #define V_error_keeptags VNET(error_keeptags) 178410634efSEugene Grosbein SYSCTL_INT(_net_inet_icmp, OID_AUTO, error_keeptags, CTLFLAG_VNET | CTLFLAG_RW, 179410634efSEugene Grosbein &VNET_NAME(error_keeptags), 0, 180410634efSEugene Grosbein "ICMP error response keeps copy of mbuf_tags of original packet"); 181410634efSEugene Grosbein 182df8bae1dSRodney W. Grimes #ifdef ICMPPRINTFS 183df8bae1dSRodney W. Grimes int icmpprintfs = 0; 184df8bae1dSRodney W. Grimes #endif 185df8bae1dSRodney W. Grimes 1864d77a549SAlfred Perlstein static void icmp_reflect(struct mbuf *); 18702c1c707SAndre Oppermann static void icmp_send(struct mbuf *, struct mbuf *); 18834a5582cSAlexander V. Chernikov static int icmp_verify_redirect_gateway(struct sockaddr_in *, 18934a5582cSAlexander V. Chernikov struct sockaddr_in *, struct sockaddr_in *, u_int); 1900312fbe9SPoul-Henning Kamp 191df8bae1dSRodney W. Grimes /* 192315e3e38SRobert Watson * Kernel module interface for updating icmpstat. The argument is an index 193315e3e38SRobert Watson * into icmpstat treated as an array of u_long. While this encodes the 194315e3e38SRobert Watson * general layout of icmpstat into the caller, it doesn't encode its 195315e3e38SRobert Watson * location, so that future changes to add, for example, per-CPU stats 196315e3e38SRobert Watson * support won't cause binary compatibility problems for kernel modules. 197315e3e38SRobert Watson */ 198315e3e38SRobert Watson void 199315e3e38SRobert Watson kmod_icmpstat_inc(int statnum) 200315e3e38SRobert Watson { 201315e3e38SRobert Watson 2025b7cb97cSAndrey V. Elsukov counter_u64_add(VNET(icmpstat)[statnum], 1); 203315e3e38SRobert Watson } 204315e3e38SRobert Watson 205315e3e38SRobert Watson /* 206df8bae1dSRodney W. Grimes * Generate an error packet of type error 207df8bae1dSRodney W. Grimes * in response to bad packet ip. 208df8bae1dSRodney W. Grimes */ 209df8bae1dSRodney W. Grimes void 210d685b6eeSLuigi Rizzo icmp_error(struct mbuf *n, int type, int code, uint32_t dest, int mtu) 211df8bae1dSRodney W. Grimes { 212f415d666SAndrey V. Elsukov struct ip *oip, *nip; 2133e85b721SEd Maste struct icmp *icp; 2143e85b721SEd Maste struct mbuf *m; 215f415d666SAndrey V. Elsukov unsigned icmplen, icmpelen, nlen, oiphlen; 216df8bae1dSRodney W. Grimes 217f415d666SAndrey V. Elsukov KASSERT((u_int)type <= ICMP_MAXTYPE, ("%s: illegal ICMP type", 218f415d666SAndrey V. Elsukov __func__)); 219f415d666SAndrey V. Elsukov 220df8bae1dSRodney W. Grimes if (type != ICMP_REDIRECT) 221e27b0c87SRobert Watson ICMPSTAT_INC(icps_error); 222df8bae1dSRodney W. Grimes /* 223e86ebebcSAndre Oppermann * Don't send error: 224e86ebebcSAndre Oppermann * if the original packet was encrypted. 225e86ebebcSAndre Oppermann * if not the first fragment of message. 226e86ebebcSAndre Oppermann * in response to a multicast or broadcast packet. 227e86ebebcSAndre Oppermann * if the old packet protocol was an ICMP error message. 228df8bae1dSRodney W. Grimes */ 229cad1917dSHajimu UMEMOTO if (n->m_flags & M_DECRYPTED) 230cad1917dSHajimu UMEMOTO goto freeit; 231e86ebebcSAndre Oppermann if (n->m_flags & (M_BCAST|M_MCAST)) 232e86ebebcSAndre Oppermann goto freeit; 233f415d666SAndrey V. Elsukov 234f415d666SAndrey V. Elsukov /* Drop if IP header plus 8 bytes is not contiguous in first mbuf. */ 235f415d666SAndrey V. Elsukov if (n->m_len < sizeof(struct ip) + ICMP_MINLEN) 236f415d666SAndrey V. Elsukov goto freeit; 237f415d666SAndrey V. Elsukov oip = mtod(n, struct ip *); 238f415d666SAndrey V. Elsukov oiphlen = oip->ip_hl << 2; 239f415d666SAndrey V. Elsukov if (n->m_len < oiphlen + ICMP_MINLEN) 240f415d666SAndrey V. Elsukov goto freeit; 241f415d666SAndrey V. Elsukov #ifdef ICMPPRINTFS 242f415d666SAndrey V. Elsukov if (icmpprintfs) 243f415d666SAndrey V. Elsukov printf("icmp_error(%p, %x, %d)\n", oip, type, code); 244f415d666SAndrey V. Elsukov #endif 245f415d666SAndrey V. Elsukov if (oip->ip_off & htons(~(IP_MF|IP_DF))) 246f415d666SAndrey V. Elsukov goto freeit; 247df8bae1dSRodney W. Grimes if (oip->ip_p == IPPROTO_ICMP && type != ICMP_REDIRECT && 248f415d666SAndrey V. Elsukov !ICMP_INFOTYPE(((struct icmp *)((caddr_t)oip + 249f415d666SAndrey V. Elsukov oiphlen))->icmp_type)) { 250e27b0c87SRobert Watson ICMPSTAT_INC(icps_oldicmp); 251df8bae1dSRodney W. Grimes goto freeit; 252df8bae1dSRodney W. Grimes } 253df8bae1dSRodney W. Grimes /* 254e86ebebcSAndre Oppermann * Calculate length to quote from original packet and 255e86ebebcSAndre Oppermann * prevent the ICMP mbuf from overflowing. 256a4641f4eSPedro F. Giffuni * Unfortunately this is non-trivial since ip_forward() 257e86ebebcSAndre Oppermann * sends us truncated packets. 258df8bae1dSRodney W. Grimes */ 259e86ebebcSAndre Oppermann nlen = m_length(n, NULL); 260e86ebebcSAndre Oppermann if (oip->ip_p == IPPROTO_TCP) { 261e86ebebcSAndre Oppermann struct tcphdr *th; 262e86ebebcSAndre Oppermann int tcphlen; 263e86ebebcSAndre Oppermann 264e86ebebcSAndre Oppermann if (oiphlen + sizeof(struct tcphdr) > n->m_len && 265e86ebebcSAndre Oppermann n->m_next == NULL) 266e86ebebcSAndre Oppermann goto stdreply; 267e86ebebcSAndre Oppermann if (n->m_len < oiphlen + sizeof(struct tcphdr) && 268f415d666SAndrey V. Elsukov (n = m_pullup(n, oiphlen + sizeof(struct tcphdr))) == NULL) 269e86ebebcSAndre Oppermann goto freeit; 270f415d666SAndrey V. Elsukov oip = mtod(n, struct ip *); 271f415d666SAndrey V. Elsukov th = mtodo(n, oiphlen); 272e86ebebcSAndre Oppermann tcphlen = th->th_off << 2; 273e86ebebcSAndre Oppermann if (tcphlen < sizeof(struct tcphdr)) 274e86ebebcSAndre Oppermann goto freeit; 2758f134647SGleb Smirnoff if (ntohs(oip->ip_len) < oiphlen + tcphlen) 276e86ebebcSAndre Oppermann goto freeit; 277e86ebebcSAndre Oppermann if (oiphlen + tcphlen > n->m_len && n->m_next == NULL) 278e86ebebcSAndre Oppermann goto stdreply; 279e86ebebcSAndre Oppermann if (n->m_len < oiphlen + tcphlen && 280f415d666SAndrey V. Elsukov (n = m_pullup(n, oiphlen + tcphlen)) == NULL) 281e86ebebcSAndre Oppermann goto freeit; 28254e67534SJonathan T. Looney oip = mtod(n, struct ip *); 2838f134647SGleb Smirnoff icmpelen = max(tcphlen, min(V_icmp_quotelen, 2848f134647SGleb Smirnoff ntohs(oip->ip_len) - oiphlen)); 285f88d0cfeSMichael Tuexen } else if (oip->ip_p == IPPROTO_SCTP) { 286f88d0cfeSMichael Tuexen struct sctphdr *sh; 287f88d0cfeSMichael Tuexen struct sctp_chunkhdr *ch; 288f88d0cfeSMichael Tuexen 289f88d0cfeSMichael Tuexen if (ntohs(oip->ip_len) < oiphlen + sizeof(struct sctphdr)) 290f88d0cfeSMichael Tuexen goto stdreply; 291f88d0cfeSMichael Tuexen if (oiphlen + sizeof(struct sctphdr) > n->m_len && 292f88d0cfeSMichael Tuexen n->m_next == NULL) 293f88d0cfeSMichael Tuexen goto stdreply; 294f88d0cfeSMichael Tuexen if (n->m_len < oiphlen + sizeof(struct sctphdr) && 295f88d0cfeSMichael Tuexen (n = m_pullup(n, oiphlen + sizeof(struct sctphdr))) == NULL) 296f88d0cfeSMichael Tuexen goto freeit; 297f415d666SAndrey V. Elsukov oip = mtod(n, struct ip *); 298f88d0cfeSMichael Tuexen icmpelen = max(sizeof(struct sctphdr), 299f88d0cfeSMichael Tuexen min(V_icmp_quotelen, ntohs(oip->ip_len) - oiphlen)); 300f415d666SAndrey V. Elsukov sh = mtodo(n, oiphlen); 301f88d0cfeSMichael Tuexen if (ntohl(sh->v_tag) == 0 && 302f415d666SAndrey V. Elsukov ntohs(oip->ip_len) >= oiphlen + 303f415d666SAndrey V. Elsukov sizeof(struct sctphdr) + 8 && 304f88d0cfeSMichael Tuexen (n->m_len >= oiphlen + sizeof(struct sctphdr) + 8 || 305f88d0cfeSMichael Tuexen n->m_next != NULL)) { 306f88d0cfeSMichael Tuexen if (n->m_len < oiphlen + sizeof(struct sctphdr) + 8 && 307f415d666SAndrey V. Elsukov (n = m_pullup(n, oiphlen + 308f415d666SAndrey V. Elsukov sizeof(struct sctphdr) + 8)) == NULL) 309f88d0cfeSMichael Tuexen goto freeit; 310f415d666SAndrey V. Elsukov oip = mtod(n, struct ip *); 311f415d666SAndrey V. Elsukov sh = mtodo(n, oiphlen); 312f88d0cfeSMichael Tuexen ch = (struct sctp_chunkhdr *)(sh + 1); 313f88d0cfeSMichael Tuexen if (ch->chunk_type == SCTP_INITIATION) { 314f88d0cfeSMichael Tuexen icmpelen = max(sizeof(struct sctphdr) + 8, 315f415d666SAndrey V. Elsukov min(V_icmp_quotelen, ntohs(oip->ip_len) - 316f415d666SAndrey V. Elsukov oiphlen)); 317f88d0cfeSMichael Tuexen } 318f88d0cfeSMichael Tuexen } 319e86ebebcSAndre Oppermann } else 320f415d666SAndrey V. Elsukov stdreply: icmpelen = max(8, min(V_icmp_quotelen, ntohs(oip->ip_len) - 321f415d666SAndrey V. Elsukov oiphlen)); 322e86ebebcSAndre Oppermann 323e86ebebcSAndre Oppermann icmplen = min(oiphlen + icmpelen, nlen); 324e86ebebcSAndre Oppermann if (icmplen < sizeof(struct ip)) 325e86ebebcSAndre Oppermann goto freeit; 326e86ebebcSAndre Oppermann 327e86ebebcSAndre Oppermann if (MHLEN > sizeof(struct ip) + ICMP_MINLEN + icmplen) 328eb1b1807SGleb Smirnoff m = m_gethdr(M_NOWAIT, MT_DATA); 329e86ebebcSAndre Oppermann else 330eb1b1807SGleb Smirnoff m = m_getcl(M_NOWAIT, MT_DATA, M_PKTHDR); 331df8bae1dSRodney W. Grimes if (m == NULL) 332df8bae1dSRodney W. Grimes goto freeit; 3330070e096SRobert Watson #ifdef MAC 334a13e21f7SRobert Watson mac_netinet_icmp_reply(n, m); 3350070e096SRobert Watson #endif 336f415d666SAndrey V. Elsukov icmplen = min(icmplen, M_TRAILINGSPACE(m) - 337f415d666SAndrey V. Elsukov sizeof(struct ip) - ICMP_MINLEN); 3382bfaf585SEd Maste m_align(m, sizeof(struct ip) + ICMP_MINLEN + icmplen); 3392bfaf585SEd Maste m->m_data += sizeof(struct ip); 340e86ebebcSAndre Oppermann m->m_len = ICMP_MINLEN + icmplen; 3416b773dffSAndre Oppermann 3428b07e49aSJulian Elischer /* XXX MRT make the outgoing packet use the same FIB 3438b07e49aSJulian Elischer * that was associated with the incoming packet 3448b07e49aSJulian Elischer */ 3458b07e49aSJulian Elischer M_SETFIB(m, M_GETFIB(n)); 346df8bae1dSRodney W. Grimes icp = mtod(m, struct icmp *); 34760d8dbbeSKristof Provost ICMPSTAT_INC2(icps_outhist, type); 348df8bae1dSRodney W. Grimes icp->icmp_type = type; 349df8bae1dSRodney W. Grimes if (type == ICMP_REDIRECT) 350df8bae1dSRodney W. Grimes icp->icmp_gwaddr.s_addr = dest; 351df8bae1dSRodney W. Grimes else { 352df8bae1dSRodney W. Grimes icp->icmp_void = 0; 353df8bae1dSRodney W. Grimes /* 354df8bae1dSRodney W. Grimes * The following assignments assume an overlay with the 355e86ebebcSAndre Oppermann * just zeroed icmp_void field. 356df8bae1dSRodney W. Grimes */ 357df8bae1dSRodney W. Grimes if (type == ICMP_PARAMPROB) { 358df8bae1dSRodney W. Grimes icp->icmp_pptr = code; 359df8bae1dSRodney W. Grimes code = 0; 360df8bae1dSRodney W. Grimes } else if (type == ICMP_UNREACH && 361c773494eSAndre Oppermann code == ICMP_UNREACH_NEEDFRAG && mtu) { 362c773494eSAndre Oppermann icp->icmp_nextmtu = htons(mtu); 363df8bae1dSRodney W. Grimes } 364df8bae1dSRodney W. Grimes } 365df8bae1dSRodney W. Grimes icp->icmp_code = code; 36604287599SRuslan Ermilov 36704287599SRuslan Ermilov /* 368e86ebebcSAndre Oppermann * Copy the quotation into ICMP message and 369e86ebebcSAndre Oppermann * convert quoted IP header back to network representation. 37004287599SRuslan Ermilov */ 371e86ebebcSAndre Oppermann m_copydata(n, 0, icmplen, (caddr_t)&icp->icmp_ip); 372e86ebebcSAndre Oppermann nip = &icp->icmp_ip; 373df8bae1dSRodney W. Grimes 374df8bae1dSRodney W. Grimes /* 375e86ebebcSAndre Oppermann * Set up ICMP message mbuf and copy old IP header (without options 376e86ebebcSAndre Oppermann * in front of ICMP message. 377c550f220SMax Laier * If the original mbuf was meant to bypass the firewall, the error 378c550f220SMax Laier * reply should bypass as well. 379c550f220SMax Laier */ 380c550f220SMax Laier m->m_flags |= n->m_flags & M_SKIP_FIREWALL; 3812bfaf585SEd Maste KASSERT(M_LEADINGSPACE(m) >= sizeof(struct ip), 3822bfaf585SEd Maste ("insufficient space for ip header")); 383df8bae1dSRodney W. Grimes m->m_data -= sizeof(struct ip); 384df8bae1dSRodney W. Grimes m->m_len += sizeof(struct ip); 385df8bae1dSRodney W. Grimes m->m_pkthdr.len = m->m_len; 386df8bae1dSRodney W. Grimes m->m_pkthdr.rcvif = n->m_pkthdr.rcvif; 387df8bae1dSRodney W. Grimes nip = mtod(m, struct ip *); 388df8bae1dSRodney W. Grimes bcopy((caddr_t)oip, (caddr_t)nip, sizeof(struct ip)); 3898f134647SGleb Smirnoff nip->ip_len = htons(m->m_len); 39053be11f6SPoul-Henning Kamp nip->ip_v = IPVERSION; 39153be11f6SPoul-Henning Kamp nip->ip_hl = 5; 392df8bae1dSRodney W. Grimes nip->ip_p = IPPROTO_ICMP; 393df8bae1dSRodney W. Grimes nip->ip_tos = 0; 39441ea685cSAndrey V. Elsukov nip->ip_off = 0; 395410634efSEugene Grosbein 396410634efSEugene Grosbein if (V_error_keeptags) 397410634efSEugene Grosbein m_tag_copy_chain(m, n, M_NOWAIT); 398410634efSEugene Grosbein 399df8bae1dSRodney W. Grimes icmp_reflect(m); 400df8bae1dSRodney W. Grimes 401df8bae1dSRodney W. Grimes freeit: 402df8bae1dSRodney W. Grimes m_freem(n); 403df8bae1dSRodney W. Grimes } 404df8bae1dSRodney W. Grimes 405fcb3f813SGleb Smirnoff int 406fcb3f813SGleb Smirnoff icmp_errmap(const struct icmp *icp) 407fcb3f813SGleb Smirnoff { 408fcb3f813SGleb Smirnoff 409fcb3f813SGleb Smirnoff switch (icp->icmp_type) { 410fcb3f813SGleb Smirnoff case ICMP_UNREACH: 411fcb3f813SGleb Smirnoff switch (icp->icmp_code) { 412fcb3f813SGleb Smirnoff case ICMP_UNREACH_NET: 413fcb3f813SGleb Smirnoff case ICMP_UNREACH_HOST: 414fcb3f813SGleb Smirnoff case ICMP_UNREACH_SRCFAIL: 415fcb3f813SGleb Smirnoff case ICMP_UNREACH_NET_UNKNOWN: 416fcb3f813SGleb Smirnoff case ICMP_UNREACH_HOST_UNKNOWN: 417fcb3f813SGleb Smirnoff case ICMP_UNREACH_ISOLATED: 418fcb3f813SGleb Smirnoff case ICMP_UNREACH_TOSNET: 419fcb3f813SGleb Smirnoff case ICMP_UNREACH_TOSHOST: 420fcb3f813SGleb Smirnoff case ICMP_UNREACH_HOST_PRECEDENCE: 421fcb3f813SGleb Smirnoff case ICMP_UNREACH_PRECEDENCE_CUTOFF: 422fcb3f813SGleb Smirnoff return (EHOSTUNREACH); 423fcb3f813SGleb Smirnoff case ICMP_UNREACH_NEEDFRAG: 424fcb3f813SGleb Smirnoff return (EMSGSIZE); 425fcb3f813SGleb Smirnoff case ICMP_UNREACH_PROTOCOL: 426fcb3f813SGleb Smirnoff case ICMP_UNREACH_PORT: 427fcb3f813SGleb Smirnoff case ICMP_UNREACH_NET_PROHIB: 428fcb3f813SGleb Smirnoff case ICMP_UNREACH_HOST_PROHIB: 429fcb3f813SGleb Smirnoff case ICMP_UNREACH_FILTER_PROHIB: 430fcb3f813SGleb Smirnoff return (ECONNREFUSED); 431fcb3f813SGleb Smirnoff default: 432fcb3f813SGleb Smirnoff return (0); 433fcb3f813SGleb Smirnoff } 434fcb3f813SGleb Smirnoff case ICMP_TIMXCEED: 435fcb3f813SGleb Smirnoff switch (icp->icmp_code) { 436fcb3f813SGleb Smirnoff case ICMP_TIMXCEED_INTRANS: 437fcb3f813SGleb Smirnoff return (EHOSTUNREACH); 438fcb3f813SGleb Smirnoff default: 439fcb3f813SGleb Smirnoff return (0); 440fcb3f813SGleb Smirnoff } 441fcb3f813SGleb Smirnoff case ICMP_PARAMPROB: 442fcb3f813SGleb Smirnoff switch (icp->icmp_code) { 443fcb3f813SGleb Smirnoff case ICMP_PARAMPROB_ERRATPTR: 444fcb3f813SGleb Smirnoff case ICMP_PARAMPROB_OPTABSENT: 445fcb3f813SGleb Smirnoff return (ENOPROTOOPT); 446fcb3f813SGleb Smirnoff default: 447fcb3f813SGleb Smirnoff return (0); 448fcb3f813SGleb Smirnoff } 449fcb3f813SGleb Smirnoff default: 450fcb3f813SGleb Smirnoff return (0); 451fcb3f813SGleb Smirnoff } 452fcb3f813SGleb Smirnoff } 453fcb3f813SGleb Smirnoff 454df8bae1dSRodney W. Grimes /* 455df8bae1dSRodney W. Grimes * Process a received ICMP message. 456df8bae1dSRodney W. Grimes */ 4578f5a8818SKevin Lo int 4588f5a8818SKevin Lo icmp_input(struct mbuf **mp, int *offp, int proto) 459df8bae1dSRodney W. Grimes { 46016d6c90fSAndre Oppermann struct icmp *icp; 461df8bae1dSRodney W. Grimes struct in_ifaddr *ia; 4628f5a8818SKevin Lo struct mbuf *m = *mp; 46316d6c90fSAndre Oppermann struct ip *ip = mtod(m, struct ip *); 46416d6c90fSAndre Oppermann struct sockaddr_in icmpsrc, icmpdst, icmpgw; 4658f5a8818SKevin Lo int hlen = *offp; 4668f5a8818SKevin Lo int icmplen = ntohs(ip->ip_len) - *offp; 46716d6c90fSAndre Oppermann int i, code; 4688b07e49aSJulian Elischer int fibnum; 469df8bae1dSRodney W. Grimes 470b8a6e03fSGleb Smirnoff NET_EPOCH_ASSERT(); 471b8a6e03fSGleb Smirnoff 4728f5a8818SKevin Lo *mp = NULL; 4738f5a8818SKevin Lo 474df8bae1dSRodney W. Grimes /* 475df8bae1dSRodney W. Grimes * Locate icmp structure in mbuf, and check 476df8bae1dSRodney W. Grimes * that not corrupted and of at least minimum length. 477df8bae1dSRodney W. Grimes */ 478df8bae1dSRodney W. Grimes #ifdef ICMPPRINTFS 4792b758395SGarrett Wollman if (icmpprintfs) { 4808144690aSEric van Gyzen char srcbuf[INET_ADDRSTRLEN]; 4818144690aSEric van Gyzen char dstbuf[INET_ADDRSTRLEN]; 4828144690aSEric van Gyzen 4832b758395SGarrett Wollman printf("icmp_input from %s to %s, len %d\n", 4848144690aSEric van Gyzen inet_ntoa_r(ip->ip_src, srcbuf), 4858144690aSEric van Gyzen inet_ntoa_r(ip->ip_dst, dstbuf), icmplen); 4862b758395SGarrett Wollman } 487df8bae1dSRodney W. Grimes #endif 488df8bae1dSRodney W. Grimes if (icmplen < ICMP_MINLEN) { 489e27b0c87SRobert Watson ICMPSTAT_INC(icps_tooshort); 490df8bae1dSRodney W. Grimes goto freeit; 491df8bae1dSRodney W. Grimes } 492df8bae1dSRodney W. Grimes i = hlen + min(icmplen, ICMP_ADVLENMIN); 493852da713SBjoern A. Zeeb if (m->m_len < i && (m = m_pullup(m, i)) == NULL) { 494e27b0c87SRobert Watson ICMPSTAT_INC(icps_tooshort); 4958f5a8818SKevin Lo return (IPPROTO_DONE); 496df8bae1dSRodney W. Grimes } 497df8bae1dSRodney W. Grimes ip = mtod(m, struct ip *); 498df8bae1dSRodney W. Grimes m->m_len -= hlen; 499df8bae1dSRodney W. Grimes m->m_data += hlen; 500df8bae1dSRodney W. Grimes icp = mtod(m, struct icmp *); 501df8bae1dSRodney W. Grimes if (in_cksum(m, icmplen)) { 502e27b0c87SRobert Watson ICMPSTAT_INC(icps_checksum); 503df8bae1dSRodney W. Grimes goto freeit; 504df8bae1dSRodney W. Grimes } 505df8bae1dSRodney W. Grimes m->m_len += hlen; 506df8bae1dSRodney W. Grimes m->m_data -= hlen; 507df8bae1dSRodney W. Grimes 508df8bae1dSRodney W. Grimes #ifdef ICMPPRINTFS 509df8bae1dSRodney W. Grimes if (icmpprintfs) 510df8bae1dSRodney W. Grimes printf("icmp_input, type %d code %d\n", icp->icmp_type, 511df8bae1dSRodney W. Grimes icp->icmp_code); 512df8bae1dSRodney W. Grimes #endif 5135b7ee6edSGarrett Wollman 5145b7ee6edSGarrett Wollman /* 5155b7ee6edSGarrett Wollman * Message type specific processing. 5165b7ee6edSGarrett Wollman */ 517df8bae1dSRodney W. Grimes if (icp->icmp_type > ICMP_MAXTYPE) 518df8bae1dSRodney W. Grimes goto raw; 51916d6c90fSAndre Oppermann 52016d6c90fSAndre Oppermann /* Initialize */ 52116d6c90fSAndre Oppermann bzero(&icmpsrc, sizeof(icmpsrc)); 52216d6c90fSAndre Oppermann icmpsrc.sin_len = sizeof(struct sockaddr_in); 52316d6c90fSAndre Oppermann icmpsrc.sin_family = AF_INET; 52416d6c90fSAndre Oppermann bzero(&icmpdst, sizeof(icmpdst)); 52516d6c90fSAndre Oppermann icmpdst.sin_len = sizeof(struct sockaddr_in); 52616d6c90fSAndre Oppermann icmpdst.sin_family = AF_INET; 52716d6c90fSAndre Oppermann bzero(&icmpgw, sizeof(icmpgw)); 52816d6c90fSAndre Oppermann icmpgw.sin_len = sizeof(struct sockaddr_in); 52916d6c90fSAndre Oppermann icmpgw.sin_family = AF_INET; 53016d6c90fSAndre Oppermann 53160d8dbbeSKristof Provost ICMPSTAT_INC2(icps_inhist, icp->icmp_type); 532df8bae1dSRodney W. Grimes code = icp->icmp_code; 533df8bae1dSRodney W. Grimes switch (icp->icmp_type) { 534df8bae1dSRodney W. Grimes case ICMP_UNREACH: 535fcb3f813SGleb Smirnoff if (code > ICMP_UNREACH_PRECEDENCE_CUTOFF) 536df8bae1dSRodney W. Grimes goto badcode; 537fcb3f813SGleb Smirnoff else 538df8bae1dSRodney W. Grimes goto deliver; 539df8bae1dSRodney W. Grimes 540df8bae1dSRodney W. Grimes case ICMP_TIMXCEED: 541fcb3f813SGleb Smirnoff if (code > ICMP_TIMXCEED_REASS) 542df8bae1dSRodney W. Grimes goto badcode; 543fcb3f813SGleb Smirnoff else 544df8bae1dSRodney W. Grimes goto deliver; 545df8bae1dSRodney W. Grimes 546df8bae1dSRodney W. Grimes case ICMP_PARAMPROB: 547fcb3f813SGleb Smirnoff if (code > ICMP_PARAMPROB_LENGTH) 548df8bae1dSRodney W. Grimes goto badcode; 549fcb3f813SGleb Smirnoff 550df8bae1dSRodney W. Grimes deliver: 551df8bae1dSRodney W. Grimes /* 552df8bae1dSRodney W. Grimes * Problem with datagram; advise higher level routines. 553df8bae1dSRodney W. Grimes */ 554df8bae1dSRodney W. Grimes if (icmplen < ICMP_ADVLENMIN || icmplen < ICMP_ADVLEN(icp) || 55553be11f6SPoul-Henning Kamp icp->icmp_ip.ip_hl < (sizeof(struct ip) >> 2)) { 556e27b0c87SRobert Watson ICMPSTAT_INC(icps_badlen); 557df8bae1dSRodney W. Grimes goto freeit; 558df8bae1dSRodney W. Grimes } 5595b7ee6edSGarrett Wollman /* Discard ICMP's in response to multicast packets */ 5605b7ee6edSGarrett Wollman if (IN_MULTICAST(ntohl(icp->icmp_ip.ip_dst.s_addr))) 5615b7ee6edSGarrett Wollman goto badcode; 5627f3b00a8SGleb Smirnoff /* Filter out responses to INADDR_ANY, protocols ignore it. */ 563aa71d6b4SMark Johnston if (icp->icmp_ip.ip_dst.s_addr == INADDR_ANY || 564aa71d6b4SMark Johnston icp->icmp_ip.ip_src.s_addr == INADDR_ANY) 5657f3b00a8SGleb Smirnoff goto freeit; 566df8bae1dSRodney W. Grimes #ifdef ICMPPRINTFS 567df8bae1dSRodney W. Grimes if (icmpprintfs) 568df8bae1dSRodney W. Grimes printf("deliver to protocol %d\n", icp->icmp_ip.ip_p); 569df8bae1dSRodney W. Grimes #endif 5706a800098SYoshinobu Inoue /* 5716a800098SYoshinobu Inoue * XXX if the packet contains [IPv4 AH TCP], we can't make a 5726a800098SYoshinobu Inoue * notification to TCP layer. 5736a800098SYoshinobu Inoue */ 574f77b8427SMichael Tuexen i = sizeof(struct ip) + min(icmplen, ICMP_ADVLENPREF(icp)); 575f77b8427SMichael Tuexen ip_stripoptions(m); 576f77b8427SMichael Tuexen if (m->m_len < i && (m = m_pullup(m, i)) == NULL) { 577f77b8427SMichael Tuexen /* This should actually not happen */ 578f77b8427SMichael Tuexen ICMPSTAT_INC(icps_tooshort); 579f77b8427SMichael Tuexen return (IPPROTO_DONE); 580f77b8427SMichael Tuexen } 581f77b8427SMichael Tuexen ip = mtod(m, struct ip *); 582f77b8427SMichael Tuexen icp = (struct icmp *)(ip + 1); 583f77b8427SMichael Tuexen /* 584f77b8427SMichael Tuexen * The upper layer handler can rely on: 585f77b8427SMichael Tuexen * - The outer IP header has no options. 586f77b8427SMichael Tuexen * - The outer IP header, the ICMP header, the inner IP header, 587f77b8427SMichael Tuexen * and the first n bytes of the inner payload are contiguous. 588f77b8427SMichael Tuexen * n is at least 8, but might be larger based on 589f77b8427SMichael Tuexen * ICMP_ADVLENPREF. See its definition in ip_icmp.h. 590f77b8427SMichael Tuexen */ 59178b1fc05SGleb Smirnoff if (ip_ctlprotox[icp->icmp_ip.ip_p] != NULL) 592fcb3f813SGleb Smirnoff ip_ctlprotox[icp->icmp_ip.ip_p](icp); 593df8bae1dSRodney W. Grimes break; 594df8bae1dSRodney W. Grimes 595df8bae1dSRodney W. Grimes badcode: 596e27b0c87SRobert Watson ICMPSTAT_INC(icps_badcode); 597df8bae1dSRodney W. Grimes break; 598df8bae1dSRodney W. Grimes 599df8bae1dSRodney W. Grimes case ICMP_ECHO: 6008b615593SMarko Zec if (!V_icmpbmcastecho 601d311884fSDavid Greenman && (m->m_flags & (M_MCAST | M_BCAST)) != 0) { 602e27b0c87SRobert Watson ICMPSTAT_INC(icps_bmcastecho); 6037022ea0aSGarrett Wollman break; 6047022ea0aSGarrett Wollman } 605a57815efSBosko Milekic if (badport_bandlim(BANDLIM_ICMP_ECHO) < 0) 60609f81a46SBosko Milekic goto freeit; 607382a6bbcSJonathan T. Looney icp->icmp_type = ICMP_ECHOREPLY; 608df8bae1dSRodney W. Grimes goto reflect; 609df8bae1dSRodney W. Grimes 610df8bae1dSRodney W. Grimes case ICMP_TSTAMP: 61100cb6befSMark Johnston if (V_icmptstamprepl == 0) 61200cb6befSMark Johnston break; 6138b615593SMarko Zec if (!V_icmpbmcastecho 614d311884fSDavid Greenman && (m->m_flags & (M_MCAST | M_BCAST)) != 0) { 615e27b0c87SRobert Watson ICMPSTAT_INC(icps_bmcasttstamp); 616fe0fb8abSGarrett Wollman break; 617fe0fb8abSGarrett Wollman } 618df8bae1dSRodney W. Grimes if (icmplen < ICMP_TSLEN) { 619e27b0c87SRobert Watson ICMPSTAT_INC(icps_badlen); 620df8bae1dSRodney W. Grimes break; 621df8bae1dSRodney W. Grimes } 622382a6bbcSJonathan T. Looney if (badport_bandlim(BANDLIM_ICMP_TSTAMP) < 0) 623382a6bbcSJonathan T. Looney goto freeit; 624df8bae1dSRodney W. Grimes icp->icmp_type = ICMP_TSTAMPREPLY; 625df8bae1dSRodney W. Grimes icp->icmp_rtime = iptime(); 626df8bae1dSRodney W. Grimes icp->icmp_ttime = icp->icmp_rtime; /* bogus, do later! */ 627df8bae1dSRodney W. Grimes goto reflect; 628df8bae1dSRodney W. Grimes 629df8bae1dSRodney W. Grimes case ICMP_MASKREQ: 6308b615593SMarko Zec if (V_icmpmaskrepl == 0) 631df8bae1dSRodney W. Grimes break; 632df8bae1dSRodney W. Grimes /* 633df8bae1dSRodney W. Grimes * We are not able to respond with all ones broadcast 634df8bae1dSRodney W. Grimes * unless we receive it over a point-to-point interface. 635df8bae1dSRodney W. Grimes */ 636df8bae1dSRodney W. Grimes if (icmplen < ICMP_MASKLEN) 637df8bae1dSRodney W. Grimes break; 638532106f7SGleb Smirnoff if (in_broadcast(ip->ip_dst)) 639df8bae1dSRodney W. Grimes icmpdst.sin_addr = ip->ip_src; 640532106f7SGleb Smirnoff else 641df8bae1dSRodney W. Grimes icmpdst.sin_addr = ip->ip_dst; 642df8bae1dSRodney W. Grimes ia = (struct in_ifaddr *)ifaof_ifpforaddr( 643df8bae1dSRodney W. Grimes (struct sockaddr *)&icmpdst, m->m_pkthdr.rcvif); 6448c0fec80SRobert Watson if (ia == NULL) 645df8bae1dSRodney W. Grimes break; 6464f6c66ccSMatt Macy if (ia->ia_ifp == NULL) 6477e6f7714SPoul-Henning Kamp break; 648df8bae1dSRodney W. Grimes icp->icmp_type = ICMP_MASKREPLY; 6498b615593SMarko Zec if (V_icmpmaskfake == 0) 650df8bae1dSRodney W. Grimes icp->icmp_mask = ia->ia_sockmask.sin_addr.s_addr; 65157842a38SMatthew N. Dodd else 6528b615593SMarko Zec icp->icmp_mask = V_icmpmaskfake; 653df8bae1dSRodney W. Grimes if (ip->ip_src.s_addr == 0) { 654df8bae1dSRodney W. Grimes if (ia->ia_ifp->if_flags & IFF_BROADCAST) 655df8bae1dSRodney W. Grimes ip->ip_src = satosin(&ia->ia_broadaddr)->sin_addr; 656df8bae1dSRodney W. Grimes else if (ia->ia_ifp->if_flags & IFF_POINTOPOINT) 657df8bae1dSRodney W. Grimes ip->ip_src = satosin(&ia->ia_dstaddr)->sin_addr; 658df8bae1dSRodney W. Grimes } 659df8bae1dSRodney W. Grimes reflect: 660e27b0c87SRobert Watson ICMPSTAT_INC(icps_reflect); 66160d8dbbeSKristof Provost ICMPSTAT_INC2(icps_outhist, icp->icmp_type); 662df8bae1dSRodney W. Grimes icmp_reflect(m); 6638f5a8818SKevin Lo return (IPPROTO_DONE); 664df8bae1dSRodney W. Grimes 665df8bae1dSRodney W. Grimes case ICMP_REDIRECT: 6668b615593SMarko Zec if (V_log_redirect) { 66718d3153eSDag-Erling Smørgrav u_long src, dst, gw; 66818d3153eSDag-Erling Smørgrav 66918d3153eSDag-Erling Smørgrav src = ntohl(ip->ip_src.s_addr); 67018d3153eSDag-Erling Smørgrav dst = ntohl(icp->icmp_ip.ip_dst.s_addr); 67118d3153eSDag-Erling Smørgrav gw = ntohl(icp->icmp_gwaddr.s_addr); 67218d3153eSDag-Erling Smørgrav printf("icmp redirect from %d.%d.%d.%d: " 67318d3153eSDag-Erling Smørgrav "%d.%d.%d.%d => %d.%d.%d.%d\n", 67418d3153eSDag-Erling Smørgrav (int)(src >> 24), (int)((src >> 16) & 0xff), 67518d3153eSDag-Erling Smørgrav (int)((src >> 8) & 0xff), (int)(src & 0xff), 67618d3153eSDag-Erling Smørgrav (int)(dst >> 24), (int)((dst >> 16) & 0xff), 67718d3153eSDag-Erling Smørgrav (int)((dst >> 8) & 0xff), (int)(dst & 0xff), 67818d3153eSDag-Erling Smørgrav (int)(gw >> 24), (int)((gw >> 16) & 0xff), 67918d3153eSDag-Erling Smørgrav (int)((gw >> 8) & 0xff), (int)(gw & 0xff)); 68018d3153eSDag-Erling Smørgrav } 68187c3bd27SAndre Oppermann /* 68287c3bd27SAndre Oppermann * RFC1812 says we must ignore ICMP redirects if we 68387c3bd27SAndre Oppermann * are acting as router. 68487c3bd27SAndre Oppermann */ 6858b615593SMarko Zec if (V_drop_redirect || V_ipforwarding) 68618d3153eSDag-Erling Smørgrav break; 687df8bae1dSRodney W. Grimes if (code > 3) 688df8bae1dSRodney W. Grimes goto badcode; 689df8bae1dSRodney W. Grimes if (icmplen < ICMP_ADVLENMIN || icmplen < ICMP_ADVLEN(icp) || 69053be11f6SPoul-Henning Kamp icp->icmp_ip.ip_hl < (sizeof(struct ip) >> 2)) { 691e27b0c87SRobert Watson ICMPSTAT_INC(icps_badlen); 692df8bae1dSRodney W. Grimes break; 693df8bae1dSRodney W. Grimes } 694df8bae1dSRodney W. Grimes /* 695df8bae1dSRodney W. Grimes * Short circuit routing redirects to force 696df8bae1dSRodney W. Grimes * immediate change in the kernel's routing 697df8bae1dSRodney W. Grimes * tables. The message is also handed to anyone 698df8bae1dSRodney W. Grimes * listening on a raw socket (e.g. the routing 699df8bae1dSRodney W. Grimes * daemon for use in updating its tables). 700df8bae1dSRodney W. Grimes */ 701df8bae1dSRodney W. Grimes icmpgw.sin_addr = ip->ip_src; 702df8bae1dSRodney W. Grimes icmpdst.sin_addr = icp->icmp_gwaddr; 703df8bae1dSRodney W. Grimes #ifdef ICMPPRINTFS 7042b758395SGarrett Wollman if (icmpprintfs) { 7058144690aSEric van Gyzen char dstbuf[INET_ADDRSTRLEN]; 7068144690aSEric van Gyzen char gwbuf[INET_ADDRSTRLEN]; 7072b758395SGarrett Wollman 7082b758395SGarrett Wollman printf("redirect dst %s to %s\n", 7098144690aSEric van Gyzen inet_ntoa_r(icp->icmp_ip.ip_dst, dstbuf), 7108144690aSEric van Gyzen inet_ntoa_r(icp->icmp_gwaddr, gwbuf)); 7112b758395SGarrett Wollman } 712df8bae1dSRodney W. Grimes #endif 713df8bae1dSRodney W. Grimes icmpsrc.sin_addr = icp->icmp_ip.ip_dst; 71434a5582cSAlexander V. Chernikov 71534a5582cSAlexander V. Chernikov /* 71634a5582cSAlexander V. Chernikov * RFC 1122 says network (code 0,2) redirects SHOULD 71734a5582cSAlexander V. Chernikov * be treated identically to the host redirects. 71834a5582cSAlexander V. Chernikov * Given that, ignore network masks. 71934a5582cSAlexander V. Chernikov */ 72034a5582cSAlexander V. Chernikov 72134a5582cSAlexander V. Chernikov /* 72234a5582cSAlexander V. Chernikov * Variable values: 72334a5582cSAlexander V. Chernikov * icmpsrc: route destination 72434a5582cSAlexander V. Chernikov * icmpdst: route gateway 72534a5582cSAlexander V. Chernikov * icmpgw: message source 72634a5582cSAlexander V. Chernikov */ 72734a5582cSAlexander V. Chernikov 72834a5582cSAlexander V. Chernikov if (icmp_verify_redirect_gateway(&icmpgw, &icmpsrc, &icmpdst, 72934a5582cSAlexander V. Chernikov M_GETFIB(m)) != 0) { 73034a5582cSAlexander V. Chernikov /* TODO: increment bad redirects here */ 73134a5582cSAlexander V. Chernikov break; 73234a5582cSAlexander V. Chernikov } 73334a5582cSAlexander V. Chernikov 7348b07e49aSJulian Elischer for ( fibnum = 0; fibnum < rt_numfibs; fibnum++) { 73534a5582cSAlexander V. Chernikov rib_add_redirect(fibnum, (struct sockaddr *)&icmpsrc, 736df8bae1dSRodney W. Grimes (struct sockaddr *)&icmpdst, 73734a5582cSAlexander V. Chernikov (struct sockaddr *)&icmpgw, m->m_pkthdr.rcvif, 73834a5582cSAlexander V. Chernikov RTF_GATEWAY, V_redirtimeout); 7398b07e49aSJulian Elischer } 740df8bae1dSRodney W. Grimes break; 741df8bae1dSRodney W. Grimes 742df8bae1dSRodney W. Grimes /* 743df8bae1dSRodney W. Grimes * No kernel processing for the following; 744df8bae1dSRodney W. Grimes * just fall through to send to raw listener. 745df8bae1dSRodney W. Grimes */ 746df8bae1dSRodney W. Grimes case ICMP_ECHOREPLY: 747df8bae1dSRodney W. Grimes case ICMP_ROUTERADVERT: 748df8bae1dSRodney W. Grimes case ICMP_ROUTERSOLICIT: 749df8bae1dSRodney W. Grimes case ICMP_TSTAMPREPLY: 750df8bae1dSRodney W. Grimes case ICMP_IREQREPLY: 751df8bae1dSRodney W. Grimes case ICMP_MASKREPLY: 752d1f79a3bSAlexander V. Chernikov case ICMP_SOURCEQUENCH: 753df8bae1dSRodney W. Grimes default: 754df8bae1dSRodney W. Grimes break; 755df8bae1dSRodney W. Grimes } 756df8bae1dSRodney W. Grimes 757df8bae1dSRodney W. Grimes raw: 7588f5a8818SKevin Lo *mp = m; 7598f5a8818SKevin Lo rip_input(mp, offp, proto); 7608f5a8818SKevin Lo return (IPPROTO_DONE); 761df8bae1dSRodney W. Grimes 762df8bae1dSRodney W. Grimes freeit: 763df8bae1dSRodney W. Grimes m_freem(m); 7648f5a8818SKevin Lo return (IPPROTO_DONE); 765df8bae1dSRodney W. Grimes } 766df8bae1dSRodney W. Grimes 767df8bae1dSRodney W. Grimes /* 768df8bae1dSRodney W. Grimes * Reflect the ip packet back to the source 769df8bae1dSRodney W. Grimes */ 7700312fbe9SPoul-Henning Kamp static void 771f2565d68SRobert Watson icmp_reflect(struct mbuf *m) 772df8bae1dSRodney W. Grimes { 773ca925d9cSJonathan Lemon struct ip *ip = mtod(m, struct ip *); 774ca925d9cSJonathan Lemon struct ifaddr *ifa; 77533c4f96dSRobert Watson struct ifnet *ifp; 776ca925d9cSJonathan Lemon struct in_ifaddr *ia; 777df8bae1dSRodney W. Grimes struct in_addr t; 7783553b300SAlexander V. Chernikov struct nhop_object *nh; 77999d628d5SPedro F. Giffuni struct mbuf *opts = NULL; 78053be11f6SPoul-Henning Kamp int optlen = (ip->ip_hl << 2) - sizeof(struct ip); 781df8bae1dSRodney W. Grimes 782b8a6e03fSGleb Smirnoff NET_EPOCH_ASSERT(); 783b8a6e03fSGleb Smirnoff 7846b9ff6b7SGeorge V. Neville-Neil if (IN_MULTICAST(ntohl(ip->ip_src.s_addr)) || 785efe58855SMike Karels (IN_EXPERIMENTAL(ntohl(ip->ip_src.s_addr)) && !V_ip_allow_net240) || 786*f7174eb2SZhenlei Huang (IN_ZERONET(ntohl(ip->ip_src.s_addr)) && !V_ip_allow_net0) || 787*f7174eb2SZhenlei Huang in_nullhost(ip->ip_src) ) { 788df8bae1dSRodney W. Grimes m_freem(m); /* Bad return address */ 789e27b0c87SRobert Watson ICMPSTAT_INC(icps_badaddr); 790*f7174eb2SZhenlei Huang goto done; /* ip_output() will check for broadcast */ 791df8bae1dSRodney W. Grimes } 7926b9ff6b7SGeorge V. Neville-Neil 793df8bae1dSRodney W. Grimes t = ip->ip_dst; 794df8bae1dSRodney W. Grimes ip->ip_dst = ip->ip_src; 7951488eac8SAndre Oppermann 796df8bae1dSRodney W. Grimes /* 7971488eac8SAndre Oppermann * Source selection for ICMP replies: 7981488eac8SAndre Oppermann * 7991488eac8SAndre Oppermann * If the incoming packet was addressed directly to one of our 8001488eac8SAndre Oppermann * own addresses, use dst as the src for the reply. 801df8bae1dSRodney W. Grimes */ 802c8ee75f2SGleb Smirnoff CK_LIST_FOREACH(ia, INADDR_HASH(t.s_addr), ia_hash) { 80333c4f96dSRobert Watson if (t.s_addr == IA_SIN(ia)->sin_addr.s_addr) { 80433c4f96dSRobert Watson t = IA_SIN(ia)->sin_addr; 805ca925d9cSJonathan Lemon goto match; 80633c4f96dSRobert Watson } 80733c4f96dSRobert Watson } 8082d9cfabaSRobert Watson 8091488eac8SAndre Oppermann /* 8101488eac8SAndre Oppermann * If the incoming packet was addressed to one of our broadcast 8111488eac8SAndre Oppermann * addresses, use the first non-broadcast address which corresponds 8121488eac8SAndre Oppermann * to the incoming interface. 8131488eac8SAndre Oppermann */ 81433c4f96dSRobert Watson ifp = m->m_pkthdr.rcvif; 81533c4f96dSRobert Watson if (ifp != NULL && ifp->if_flags & IFF_BROADCAST) { 816d7c5a620SMatt Macy CK_STAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) { 817ca925d9cSJonathan Lemon if (ifa->ifa_addr->sa_family != AF_INET) 818ca925d9cSJonathan Lemon continue; 819ca925d9cSJonathan Lemon ia = ifatoia(ifa); 820ca925d9cSJonathan Lemon if (satosin(&ia->ia_broadaddr)->sin_addr.s_addr == 82133c4f96dSRobert Watson t.s_addr) { 82233c4f96dSRobert Watson t = IA_SIN(ia)->sin_addr; 823ca925d9cSJonathan Lemon goto match; 824df8bae1dSRodney W. Grimes } 825ca925d9cSJonathan Lemon } 82633c4f96dSRobert Watson } 8271488eac8SAndre Oppermann /* 828a0866c8dSAndre Oppermann * If the packet was transiting through us, use the address of 829a0866c8dSAndre Oppermann * the interface the packet came through in. If that interface 830a0866c8dSAndre Oppermann * doesn't have a suitable IP address, the normal selection 831a0866c8dSAndre Oppermann * criteria apply. 832a0866c8dSAndre Oppermann */ 83333c4f96dSRobert Watson if (V_icmp_rfi && ifp != NULL) { 834d7c5a620SMatt Macy CK_STAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) { 835a0866c8dSAndre Oppermann if (ifa->ifa_addr->sa_family != AF_INET) 836a0866c8dSAndre Oppermann continue; 837a0866c8dSAndre Oppermann ia = ifatoia(ifa); 83833c4f96dSRobert Watson t = IA_SIN(ia)->sin_addr; 839a0866c8dSAndre Oppermann goto match; 840a0866c8dSAndre Oppermann } 841a0866c8dSAndre Oppermann } 842a0866c8dSAndre Oppermann /* 843b74d89bbSAndre Oppermann * If the incoming packet was not addressed directly to us, use 844b74d89bbSAndre Oppermann * designated interface for icmp replies specified by sysctl 845b74d89bbSAndre Oppermann * net.inet.icmp.reply_src (default not set). Otherwise continue 846b74d89bbSAndre Oppermann * with normal source selection. 847b74d89bbSAndre Oppermann */ 84833c4f96dSRobert Watson if (V_reply_src[0] != '\0' && (ifp = ifunit(V_reply_src))) { 849d7c5a620SMatt Macy CK_STAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) { 850b74d89bbSAndre Oppermann if (ifa->ifa_addr->sa_family != AF_INET) 851b74d89bbSAndre Oppermann continue; 852b74d89bbSAndre Oppermann ia = ifatoia(ifa); 85333c4f96dSRobert Watson t = IA_SIN(ia)->sin_addr; 854b74d89bbSAndre Oppermann goto match; 855b74d89bbSAndre Oppermann } 856b74d89bbSAndre Oppermann } 857b74d89bbSAndre Oppermann /* 8581488eac8SAndre Oppermann * If the packet was transiting through us, use the address of 8591488eac8SAndre Oppermann * the interface that is the closest to the packet source. 8601488eac8SAndre Oppermann * When we don't have a route back to the packet source, stop here 8611488eac8SAndre Oppermann * and drop the packet. 8621488eac8SAndre Oppermann */ 8633553b300SAlexander V. Chernikov nh = fib4_lookup(M_GETFIB(m), ip->ip_dst, 0, NHR_NONE, 0); 8643553b300SAlexander V. Chernikov if (nh == NULL) { 8650d4bef5dSDima Dorfman m_freem(m); 866e27b0c87SRobert Watson ICMPSTAT_INC(icps_noroute); 8670d4bef5dSDima Dorfman goto done; 8680d4bef5dSDima Dorfman } 8693553b300SAlexander V. Chernikov t = IA_SIN(ifatoia(nh->nh_ifa))->sin_addr; 870ca925d9cSJonathan Lemon match: 871baee0c3eSRobert Watson #ifdef MAC 872a13e21f7SRobert Watson mac_netinet_icmp_replyinplace(m); 873baee0c3eSRobert Watson #endif 874df8bae1dSRodney W. Grimes ip->ip_src = t; 875603724d3SBjoern A. Zeeb ip->ip_ttl = V_ip_defttl; 876df8bae1dSRodney W. Grimes 877df8bae1dSRodney W. Grimes if (optlen > 0) { 8783e85b721SEd Maste u_char *cp; 879df8bae1dSRodney W. Grimes int opt, cnt; 880df8bae1dSRodney W. Grimes u_int len; 881df8bae1dSRodney W. Grimes 882df8bae1dSRodney W. Grimes /* 883df8bae1dSRodney W. Grimes * Retrieve any source routing from the incoming packet; 884df8bae1dSRodney W. Grimes * add on any record-route or timestamp options. 885df8bae1dSRodney W. Grimes */ 886df8bae1dSRodney W. Grimes cp = (u_char *) (ip + 1); 88799d628d5SPedro F. Giffuni if ((opts = ip_srcroute(m)) == NULL && 888eb1b1807SGleb Smirnoff (opts = m_gethdr(M_NOWAIT, MT_DATA))) { 889df8bae1dSRodney W. Grimes opts->m_len = sizeof(struct in_addr); 890df8bae1dSRodney W. Grimes mtod(opts, struct in_addr *)->s_addr = 0; 891df8bae1dSRodney W. Grimes } 892df8bae1dSRodney W. Grimes if (opts) { 893df8bae1dSRodney W. Grimes #ifdef ICMPPRINTFS 894df8bae1dSRodney W. Grimes if (icmpprintfs) 895df8bae1dSRodney W. Grimes printf("icmp_reflect optlen %d rt %d => ", 896df8bae1dSRodney W. Grimes optlen, opts->m_len); 897df8bae1dSRodney W. Grimes #endif 898df8bae1dSRodney W. Grimes for (cnt = optlen; cnt > 0; cnt -= len, cp += len) { 899df8bae1dSRodney W. Grimes opt = cp[IPOPT_OPTVAL]; 900df8bae1dSRodney W. Grimes if (opt == IPOPT_EOL) 901df8bae1dSRodney W. Grimes break; 902df8bae1dSRodney W. Grimes if (opt == IPOPT_NOP) 903df8bae1dSRodney W. Grimes len = 1; 904df8bae1dSRodney W. Grimes else { 905707d00a3SJonathan Lemon if (cnt < IPOPT_OLEN + sizeof(*cp)) 906707d00a3SJonathan Lemon break; 907df8bae1dSRodney W. Grimes len = cp[IPOPT_OLEN]; 908707d00a3SJonathan Lemon if (len < IPOPT_OLEN + sizeof(*cp) || 909707d00a3SJonathan Lemon len > cnt) 910df8bae1dSRodney W. Grimes break; 911df8bae1dSRodney W. Grimes } 912df8bae1dSRodney W. Grimes /* 913df8bae1dSRodney W. Grimes * Should check for overflow, but it "can't happen" 914df8bae1dSRodney W. Grimes */ 915df8bae1dSRodney W. Grimes if (opt == IPOPT_RR || opt == IPOPT_TS || 916df8bae1dSRodney W. Grimes opt == IPOPT_SECURITY) { 917df8bae1dSRodney W. Grimes bcopy((caddr_t)cp, 918df8bae1dSRodney W. Grimes mtod(opts, caddr_t) + opts->m_len, len); 919df8bae1dSRodney W. Grimes opts->m_len += len; 920df8bae1dSRodney W. Grimes } 921df8bae1dSRodney W. Grimes } 922df8bae1dSRodney W. Grimes /* Terminate & pad, if necessary */ 923623ae52eSPoul-Henning Kamp cnt = opts->m_len % 4; 924623ae52eSPoul-Henning Kamp if (cnt) { 925df8bae1dSRodney W. Grimes for (; cnt < 4; cnt++) { 926df8bae1dSRodney W. Grimes *(mtod(opts, caddr_t) + opts->m_len) = 927df8bae1dSRodney W. Grimes IPOPT_EOL; 928df8bae1dSRodney W. Grimes opts->m_len++; 929df8bae1dSRodney W. Grimes } 930df8bae1dSRodney W. Grimes } 931df8bae1dSRodney W. Grimes #ifdef ICMPPRINTFS 932df8bae1dSRodney W. Grimes if (icmpprintfs) 933df8bae1dSRodney W. Grimes printf("%d\n", opts->m_len); 934df8bae1dSRodney W. Grimes #endif 935df8bae1dSRodney W. Grimes } 9369e2a372fSGleb Smirnoff ip_stripoptions(m); 937df8bae1dSRodney W. Grimes } 9389c855a36SSam Leffler m_tag_delete_nonpersistent(m); 939df8bae1dSRodney W. Grimes m->m_flags &= ~(M_BCAST|M_MCAST); 94002c1c707SAndre Oppermann icmp_send(m, opts); 941df8bae1dSRodney W. Grimes done: 942df8bae1dSRodney W. Grimes if (opts) 943df8bae1dSRodney W. Grimes (void)m_free(opts); 944df8bae1dSRodney W. Grimes } 945df8bae1dSRodney W. Grimes 946df8bae1dSRodney W. Grimes /* 94734a5582cSAlexander V. Chernikov * Verifies if redirect message is valid, according to RFC 1122 94834a5582cSAlexander V. Chernikov * 94934a5582cSAlexander V. Chernikov * @src: sockaddr with address of redirect originator 95034a5582cSAlexander V. Chernikov * @dst: sockaddr with destination in question 95134a5582cSAlexander V. Chernikov * @gateway: new proposed gateway 95234a5582cSAlexander V. Chernikov * 95334a5582cSAlexander V. Chernikov * Returns 0 on success. 95434a5582cSAlexander V. Chernikov */ 95534a5582cSAlexander V. Chernikov static int 95634a5582cSAlexander V. Chernikov icmp_verify_redirect_gateway(struct sockaddr_in *src, struct sockaddr_in *dst, 95734a5582cSAlexander V. Chernikov struct sockaddr_in *gateway, u_int fibnum) 95834a5582cSAlexander V. Chernikov { 9599ac7c6cfSAlexander V. Chernikov struct nhop_object *nh; 96034a5582cSAlexander V. Chernikov struct ifaddr *ifa; 96134a5582cSAlexander V. Chernikov 96234a5582cSAlexander V. Chernikov NET_EPOCH_ASSERT(); 96334a5582cSAlexander V. Chernikov 96434a5582cSAlexander V. Chernikov /* Verify the gateway is directly reachable. */ 96534a5582cSAlexander V. Chernikov if ((ifa = ifa_ifwithnet((struct sockaddr *)gateway, 0, fibnum))==NULL) 96634a5582cSAlexander V. Chernikov return (ENETUNREACH); 96734a5582cSAlexander V. Chernikov 96834a5582cSAlexander V. Chernikov /* TODO: fib-aware. */ 96934a5582cSAlexander V. Chernikov if (ifa_ifwithaddr_check((struct sockaddr *)gateway)) 97034a5582cSAlexander V. Chernikov return (EHOSTUNREACH); 97134a5582cSAlexander V. Chernikov 9729ac7c6cfSAlexander V. Chernikov nh = fib4_lookup(fibnum, dst->sin_addr, 0, NHR_NONE, 0); 9739ac7c6cfSAlexander V. Chernikov if (nh == NULL) 97434a5582cSAlexander V. Chernikov return (EINVAL); 97534a5582cSAlexander V. Chernikov 97634a5582cSAlexander V. Chernikov /* 97734a5582cSAlexander V. Chernikov * If the redirect isn't from our current router for this dst, 97834a5582cSAlexander V. Chernikov * it's either old or wrong. If it redirects us to ourselves, 97934a5582cSAlexander V. Chernikov * we have a routing loop, perhaps as a result of an interface 98034a5582cSAlexander V. Chernikov * going down recently. 98134a5582cSAlexander V. Chernikov */ 9829ac7c6cfSAlexander V. Chernikov if (!sa_equal((struct sockaddr *)src, &nh->gw_sa)) 98334a5582cSAlexander V. Chernikov return (EINVAL); 9849ac7c6cfSAlexander V. Chernikov if (nh->nh_ifa != ifa && ifa->ifa_addr->sa_family != AF_LINK) 98534a5582cSAlexander V. Chernikov return (EINVAL); 98634a5582cSAlexander V. Chernikov 98734a5582cSAlexander V. Chernikov /* If host route already exists, ignore redirect. */ 9889ac7c6cfSAlexander V. Chernikov if (nh->nh_flags & NHF_HOST) 98934a5582cSAlexander V. Chernikov return (EEXIST); 99034a5582cSAlexander V. Chernikov 99134a5582cSAlexander V. Chernikov /* If the prefix is directly reachable, ignore redirect. */ 9929ac7c6cfSAlexander V. Chernikov if (!(nh->nh_flags & NHF_GATEWAY)) 99334a5582cSAlexander V. Chernikov return (EEXIST); 99434a5582cSAlexander V. Chernikov 99534a5582cSAlexander V. Chernikov return (0); 99634a5582cSAlexander V. Chernikov } 99734a5582cSAlexander V. Chernikov 99834a5582cSAlexander V. Chernikov /* 999df8bae1dSRodney W. Grimes * Send an icmp packet back to the ip level, 1000df8bae1dSRodney W. Grimes * after supplying a checksum. 1001df8bae1dSRodney W. Grimes */ 10020312fbe9SPoul-Henning Kamp static void 1003f2565d68SRobert Watson icmp_send(struct mbuf *m, struct mbuf *opts) 1004df8bae1dSRodney W. Grimes { 10053e85b721SEd Maste struct ip *ip = mtod(m, struct ip *); 10063e85b721SEd Maste int hlen; 10073e85b721SEd Maste struct icmp *icp; 1008df8bae1dSRodney W. Grimes 100953be11f6SPoul-Henning Kamp hlen = ip->ip_hl << 2; 1010df8bae1dSRodney W. Grimes m->m_data += hlen; 1011df8bae1dSRodney W. Grimes m->m_len -= hlen; 1012df8bae1dSRodney W. Grimes icp = mtod(m, struct icmp *); 1013df8bae1dSRodney W. Grimes icp->icmp_cksum = 0; 10148f134647SGleb Smirnoff icp->icmp_cksum = in_cksum(m, ntohs(ip->ip_len) - hlen); 1015df8bae1dSRodney W. Grimes m->m_data -= hlen; 1016df8bae1dSRodney W. Grimes m->m_len += hlen; 101794446a2eSArchie Cobbs m->m_pkthdr.rcvif = (struct ifnet *)0; 1018df8bae1dSRodney W. Grimes #ifdef ICMPPRINTFS 10192b758395SGarrett Wollman if (icmpprintfs) { 10208144690aSEric van Gyzen char dstbuf[INET_ADDRSTRLEN]; 10218144690aSEric van Gyzen char srcbuf[INET_ADDRSTRLEN]; 10228144690aSEric van Gyzen 10232b758395SGarrett Wollman printf("icmp_send dst %s src %s\n", 10248144690aSEric van Gyzen inet_ntoa_r(ip->ip_dst, dstbuf), 10258144690aSEric van Gyzen inet_ntoa_r(ip->ip_src, srcbuf)); 10262b758395SGarrett Wollman } 1027df8bae1dSRodney W. Grimes #endif 102802c1c707SAndre Oppermann (void) ip_output(m, opts, NULL, 0, NULL, NULL); 1029df8bae1dSRodney W. Grimes } 1030df8bae1dSRodney W. Grimes 1031d685b6eeSLuigi Rizzo /* 103261f26caeSWarner Losh * Return milliseconds since 00:00 UTC in network format. 1033d685b6eeSLuigi Rizzo */ 1034d685b6eeSLuigi Rizzo uint32_t 1035f2565d68SRobert Watson iptime(void) 1036df8bae1dSRodney W. Grimes { 1037df8bae1dSRodney W. Grimes struct timeval atv; 1038df8bae1dSRodney W. Grimes u_long t; 1039df8bae1dSRodney W. Grimes 104016cd6db0SBill Fumerola getmicrotime(&atv); 1041df8bae1dSRodney W. Grimes t = (atv.tv_sec % (24*60*60)) * 1000 + atv.tv_usec / 1000; 1042df8bae1dSRodney W. Grimes return (htonl(t)); 1043df8bae1dSRodney W. Grimes } 1044df8bae1dSRodney W. Grimes 10455cbf3e08SGarrett Wollman /* 10465cbf3e08SGarrett Wollman * Return the next larger or smaller MTU plateau (table from RFC 1191) 10475cbf3e08SGarrett Wollman * given current value MTU. If DIR is less than zero, a larger plateau 10485cbf3e08SGarrett Wollman * is returned; otherwise, a smaller value is returned. 10495cbf3e08SGarrett Wollman */ 10501aedbd9cSAndre Oppermann int 1051f2565d68SRobert Watson ip_next_mtu(int mtu, int dir) 10525cbf3e08SGarrett Wollman { 10535cbf3e08SGarrett Wollman static int mtutab[] = { 10544c037f8dSAndre Oppermann 65535, 32000, 17914, 8166, 4352, 2002, 1492, 1280, 1006, 508, 10554c037f8dSAndre Oppermann 296, 68, 0 10565cbf3e08SGarrett Wollman }; 105706003a1eSAndre Oppermann int i, size; 10585cbf3e08SGarrett Wollman 105906003a1eSAndre Oppermann size = (sizeof mtutab) / (sizeof mtutab[0]); 106006003a1eSAndre Oppermann if (dir >= 0) { 10611c0b0f52SGleb Smirnoff for (i = 0; i < size; i++) 106206003a1eSAndre Oppermann if (mtu > mtutab[i]) 10635cbf3e08SGarrett Wollman return mtutab[i]; 10645cbf3e08SGarrett Wollman } else { 106506003a1eSAndre Oppermann for (i = size - 1; i >= 0; i--) 106606003a1eSAndre Oppermann if (mtu < mtutab[i]) 106706003a1eSAndre Oppermann return mtutab[i]; 106806003a1eSAndre Oppermann if (mtu == mtutab[0]) 106906003a1eSAndre Oppermann return mtutab[0]; 10705cbf3e08SGarrett Wollman } 107106003a1eSAndre Oppermann return 0; 10725cbf3e08SGarrett Wollman } 1073b8e463e6SBjoern A. Zeeb #endif /* INET */ 107451508de1SMatthew Dillon 107551508de1SMatthew Dillon /* 107651508de1SMatthew Dillon * badport_bandlim() - check for ICMP bandwidth limit 107751508de1SMatthew Dillon * 107851508de1SMatthew Dillon * Return 0 if it is ok to send an ICMP error response, -1 if we have 107951508de1SMatthew Dillon * hit our bandwidth limit and it is not ok. 108051508de1SMatthew Dillon * 108151508de1SMatthew Dillon * If icmplim is <= 0, the feature is disabled and 0 is returned. 108251508de1SMatthew Dillon * 108351508de1SMatthew Dillon * For now we separate the TCP and UDP subsystems w/ different 'which' 108451508de1SMatthew Dillon * values. We may eventually remove this separation (and simplify the 108551508de1SMatthew Dillon * code further). 108651508de1SMatthew Dillon * 108751508de1SMatthew Dillon * Note that the printing of the error message is delayed so we can 108851508de1SMatthew Dillon * properly print the icmp error rate that the system was trying to do 108951508de1SMatthew Dillon * (i.e. 22000/100 pps, etc...). This can cause long delays in printing 109051508de1SMatthew Dillon * the 'final' error, but it doesn't make sense to solve the printing 109151508de1SMatthew Dillon * delay with more complex code. 109251508de1SMatthew Dillon */ 10937142ab47SGleb Smirnoff VNET_DEFINE_STATIC(struct counter_rate, icmp_rates[BANDLIM_MAX]); 10943cbee8caSGleb Smirnoff #define V_icmp_rates VNET(icmp_rates) 109551508de1SMatthew Dillon 10967142ab47SGleb Smirnoff static const char *icmp_rate_descrs[BANDLIM_MAX] = { 10977142ab47SGleb Smirnoff [BANDLIM_ICMP_UNREACH] = "icmp unreach", 10987142ab47SGleb Smirnoff [BANDLIM_ICMP_ECHO] = "icmp ping", 10997142ab47SGleb Smirnoff [BANDLIM_ICMP_TSTAMP] = "icmp tstamp", 11007142ab47SGleb Smirnoff [BANDLIM_RST_CLOSEDPORT] = "closed port RST", 11017142ab47SGleb Smirnoff [BANDLIM_RST_OPENPORT] = "open port RST", 11027142ab47SGleb Smirnoff [BANDLIM_ICMP6_UNREACH] = "icmp6 unreach", 11037142ab47SGleb Smirnoff [BANDLIM_SCTP_OOTB] = "sctp ootb", 11047142ab47SGleb Smirnoff }; 11057142ab47SGleb Smirnoff 11063cbee8caSGleb Smirnoff static void 1107923c223fSMichael Tuexen icmplim_new_jitter(int which) 1108ac44739fSGleb Smirnoff { 1109ac44739fSGleb Smirnoff /* 1110ac44739fSGleb Smirnoff * Adjust limit +/- to jitter the measurement to deny a side-channel 1111ac44739fSGleb Smirnoff * port scan as in https://dl.acm.org/doi/10.1145/3372297.3417280 1112ac44739fSGleb Smirnoff */ 1113923c223fSMichael Tuexen KASSERT(which >= 0 && which < BANDLIM_MAX, 1114923c223fSMichael Tuexen ("%s: which %d", __func__, which)); 1115ac44739fSGleb Smirnoff if (V_icmplim_jitter > 0) 1116923c223fSMichael Tuexen V_icmplim_curr_jitter[which] = 1117ac44739fSGleb Smirnoff arc4random_uniform(V_icmplim_jitter * 2 + 1) - 1118ac44739fSGleb Smirnoff V_icmplim_jitter; 1119ac44739fSGleb Smirnoff } 1120ac44739fSGleb Smirnoff 1121ac44739fSGleb Smirnoff static int 1122ac44739fSGleb Smirnoff sysctl_icmplim_and_jitter(SYSCTL_HANDLER_ARGS) 1123ac44739fSGleb Smirnoff { 1124ac44739fSGleb Smirnoff uint32_t new; 1125ac44739fSGleb Smirnoff int error; 1126ac44739fSGleb Smirnoff bool lim; 1127ac44739fSGleb Smirnoff 1128ac44739fSGleb Smirnoff MPASS(oidp->oid_arg1 == &VNET_NAME(icmplim) || 1129ac44739fSGleb Smirnoff oidp->oid_arg1 == &VNET_NAME(icmplim_jitter)); 1130ac44739fSGleb Smirnoff 1131ac44739fSGleb Smirnoff lim = (oidp->oid_arg1 == &VNET_NAME(icmplim)); 1132ac44739fSGleb Smirnoff new = lim ? V_icmplim : V_icmplim_jitter; 1133ac44739fSGleb Smirnoff error = sysctl_handle_int(oidp, &new, 0, req); 1134ac44739fSGleb Smirnoff if (error == 0 && req->newptr) { 1135ac44739fSGleb Smirnoff if (lim) { 11364399e055SGleb Smirnoff if (new != 0 && new <= V_icmplim_jitter) 1137ac44739fSGleb Smirnoff error = EINVAL; 1138ac44739fSGleb Smirnoff else 1139ac44739fSGleb Smirnoff V_icmplim = new; 1140ac44739fSGleb Smirnoff } else { 1141ac44739fSGleb Smirnoff if (new >= V_icmplim) 1142ac44739fSGleb Smirnoff error = EINVAL; 1143ac44739fSGleb Smirnoff else { 1144ac44739fSGleb Smirnoff V_icmplim_jitter = new; 1145923c223fSMichael Tuexen for (int i = 0; i < BANDLIM_MAX; i++) { 1146923c223fSMichael Tuexen icmplim_new_jitter(i); 1147923c223fSMichael Tuexen } 1148ac44739fSGleb Smirnoff } 1149ac44739fSGleb Smirnoff } 1150ac44739fSGleb Smirnoff } 1151c9febea3SMichael Tuexen MPASS(V_icmplim == 0 || V_icmplim > V_icmplim_jitter); 1152ac44739fSGleb Smirnoff 1153ac44739fSGleb Smirnoff return (error); 1154ac44739fSGleb Smirnoff } 1155ac44739fSGleb Smirnoff 1156ac44739fSGleb Smirnoff static void 11573cbee8caSGleb Smirnoff icmp_bandlimit_init(void) 11583cbee8caSGleb Smirnoff { 115951508de1SMatthew Dillon 11603cbee8caSGleb Smirnoff for (int i = 0; i < BANDLIM_MAX; i++) { 11617142ab47SGleb Smirnoff V_icmp_rates[i].cr_rate = counter_u64_alloc(M_WAITOK); 11627142ab47SGleb Smirnoff V_icmp_rates[i].cr_ticks = ticks; 1163923c223fSMichael Tuexen icmplim_new_jitter(i); 116451508de1SMatthew Dillon } 11653cbee8caSGleb Smirnoff } 11663cbee8caSGleb Smirnoff VNET_SYSINIT(icmp_bandlimit, SI_SUB_PROTO_DOMAIN, SI_ORDER_ANY, 11673cbee8caSGleb Smirnoff icmp_bandlimit_init, NULL); 11683cbee8caSGleb Smirnoff 11699d7f17d7SGleb Smirnoff #ifdef VIMAGE 11703cbee8caSGleb Smirnoff static void 11713cbee8caSGleb Smirnoff icmp_bandlimit_uninit(void) 11723cbee8caSGleb Smirnoff { 11733cbee8caSGleb Smirnoff 11743cbee8caSGleb Smirnoff for (int i = 0; i < BANDLIM_MAX; i++) 11757142ab47SGleb Smirnoff counter_u64_free(V_icmp_rates[i].cr_rate); 11763cbee8caSGleb Smirnoff } 11773cbee8caSGleb Smirnoff VNET_SYSUNINIT(icmp_bandlimit, SI_SUB_PROTO_DOMAIN, SI_ORDER_THIRD, 11783cbee8caSGleb Smirnoff icmp_bandlimit_uninit, NULL); 11799d7f17d7SGleb Smirnoff #endif 11803cbee8caSGleb Smirnoff 11813cbee8caSGleb Smirnoff int 11823cbee8caSGleb Smirnoff badport_bandlim(int which) 11833cbee8caSGleb Smirnoff { 11843cbee8caSGleb Smirnoff int64_t pps; 11853cbee8caSGleb Smirnoff 11863cbee8caSGleb Smirnoff if (V_icmplim == 0 || which == BANDLIM_UNLIMITED) 11873cbee8caSGleb Smirnoff return (0); 11883cbee8caSGleb Smirnoff 11893cbee8caSGleb Smirnoff KASSERT(which >= 0 && which < BANDLIM_MAX, 11903cbee8caSGleb Smirnoff ("%s: which %d", __func__, which)); 11913cbee8caSGleb Smirnoff 11927142ab47SGleb Smirnoff pps = counter_ratecheck(&V_icmp_rates[which], V_icmplim + 1193923c223fSMichael Tuexen V_icmplim_curr_jitter[which]); 1194ca4cd20cSGeorge V. Neville-Neil if (pps > 0) { 1195b508545cSGleb Smirnoff if (V_icmplim_output) 1196b508545cSGleb Smirnoff log(LOG_NOTICE, 1197b508545cSGleb Smirnoff "Limiting %s response from %jd to %d packets/sec\n", 1198b508545cSGleb Smirnoff icmp_rate_descrs[which], (intmax_t )pps, 1199923c223fSMichael Tuexen V_icmplim + V_icmplim_curr_jitter[which]); 1200923c223fSMichael Tuexen icmplim_new_jitter(which); 1201ca4cd20cSGeorge V. Neville-Neil } 12023cbee8caSGleb Smirnoff if (pps == -1) 12033cbee8caSGleb Smirnoff return (-1); 12043cbee8caSGleb Smirnoff return (0); 120551508de1SMatthew Dillon } 1206