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 * @(#)ip_icmp.c 8.2 (Berkeley) 1/4/94 32df8bae1dSRodney W. Grimes */ 33df8bae1dSRodney W. Grimes 344b421e2dSMike Silbersack #include <sys/cdefs.h> 354b421e2dSMike Silbersack __FBSDID("$FreeBSD$"); 364b421e2dSMike Silbersack 37b8e463e6SBjoern A. Zeeb #include "opt_inet.h" 386a800098SYoshinobu Inoue 39df8bae1dSRodney W. Grimes #include <sys/param.h> 40df8bae1dSRodney W. Grimes #include <sys/systm.h> 41df8bae1dSRodney W. Grimes #include <sys/mbuf.h> 42df8bae1dSRodney W. Grimes #include <sys/protosw.h> 43df8bae1dSRodney W. Grimes #include <sys/socket.h> 44df8bae1dSRodney W. Grimes #include <sys/time.h> 45df8bae1dSRodney W. Grimes #include <sys/kernel.h> 46cc0a3c8cSAndrey V. Elsukov #include <sys/lock.h> 47cc0a3c8cSAndrey V. Elsukov #include <sys/rmlock.h> 48b5e8ce9fSBruce Evans #include <sys/sysctl.h> 4940fe9effSAndre Oppermann #include <sys/syslog.h> 50df8bae1dSRodney W. Grimes 51df8bae1dSRodney W. Grimes #include <net/if.h> 5276039bc8SGleb Smirnoff #include <net/if_var.h> 539494d596SBrooks Davis #include <net/if_types.h> 54df8bae1dSRodney W. Grimes #include <net/route.h> 55eddfbb76SRobert Watson #include <net/vnet.h> 56df8bae1dSRodney W. Grimes 57df8bae1dSRodney W. Grimes #include <netinet/in.h> 589977be4aSAlexander V. Chernikov #include <netinet/in_fib.h> 5997d8d152SAndre Oppermann #include <netinet/in_pcb.h> 60df8bae1dSRodney W. Grimes #include <netinet/in_systm.h> 61df8bae1dSRodney W. Grimes #include <netinet/in_var.h> 62df8bae1dSRodney W. Grimes #include <netinet/ip.h> 63df8bae1dSRodney W. Grimes #include <netinet/ip_icmp.h> 64b5e8ce9fSBruce Evans #include <netinet/ip_var.h> 65ef39adf0SAndre Oppermann #include <netinet/ip_options.h> 66f88d0cfeSMichael Tuexen #include <netinet/sctp.h> 6797d8d152SAndre Oppermann #include <netinet/tcp.h> 6897d8d152SAndre Oppermann #include <netinet/tcp_var.h> 6997d8d152SAndre Oppermann #include <netinet/tcpip.h> 70df8bae1dSRodney W. Grimes #include <netinet/icmp_var.h> 71df8bae1dSRodney W. Grimes 72d7c5a620SMatt Macy 73b8e463e6SBjoern A. Zeeb #ifdef INET 74b9234fafSSam Leffler 7572a52a35SJonathan Lemon #include <machine/in_cksum.h> 7672a52a35SJonathan Lemon 77aed55708SRobert Watson #include <security/mac/mac_framework.h> 78b8e463e6SBjoern A. Zeeb #endif /* INET */ 79aed55708SRobert Watson 80df8bae1dSRodney W. Grimes /* 81df8bae1dSRodney W. Grimes * ICMP routines: error generation, receive packet processing, and 82df8bae1dSRodney W. Grimes * routines to turnaround packets back to the originator, and 83df8bae1dSRodney W. Grimes * host table maintenance routines. 84df8bae1dSRodney W. Grimes */ 855f901c92SAndrew Turner VNET_DEFINE_STATIC(int, icmplim) = 200; 86b8e463e6SBjoern A. Zeeb #define V_icmplim VNET(icmplim) 876df8a710SGleb Smirnoff SYSCTL_INT(_net_inet_icmp, ICMPCTL_ICMPLIM, icmplim, CTLFLAG_VNET | CTLFLAG_RW, 88b8e463e6SBjoern A. Zeeb &VNET_NAME(icmplim), 0, 89b8e463e6SBjoern A. Zeeb "Maximum number of ICMP responses per second"); 90b8e463e6SBjoern A. Zeeb 915f901c92SAndrew Turner VNET_DEFINE_STATIC(int, icmplim_output) = 1; 92b8e463e6SBjoern A. Zeeb #define V_icmplim_output VNET(icmplim_output) 936df8a710SGleb Smirnoff SYSCTL_INT(_net_inet_icmp, OID_AUTO, icmplim_output, CTLFLAG_VNET | CTLFLAG_RW, 94b8e463e6SBjoern A. Zeeb &VNET_NAME(icmplim_output), 0, 95316efdb3SEitan Adler "Enable logging of ICMP response rate limiting"); 96b8e463e6SBjoern A. Zeeb 97b8e463e6SBjoern A. Zeeb #ifdef INET 985b7cb97cSAndrey V. Elsukov VNET_PCPUSTAT_DEFINE(struct icmpstat, icmpstat); 995b7cb97cSAndrey V. Elsukov VNET_PCPUSTAT_SYSINIT(icmpstat); 1005b7cb97cSAndrey V. Elsukov SYSCTL_VNET_PCPUSTAT(_net_inet_icmp, ICMPCTL_STATS, stats, struct icmpstat, 1015b7cb97cSAndrey V. Elsukov icmpstat, "ICMP statistics (struct icmpstat, netinet/icmp_var.h)"); 1025b7cb97cSAndrey V. Elsukov 1035b7cb97cSAndrey V. Elsukov #ifdef VIMAGE 1045b7cb97cSAndrey V. Elsukov VNET_PCPUSTAT_SYSUNINIT(icmpstat); 1055b7cb97cSAndrey V. Elsukov #endif /* VIMAGE */ 106eddfbb76SRobert Watson 1075f901c92SAndrew Turner VNET_DEFINE_STATIC(int, icmpmaskrepl) = 0; 10882cea7e6SBjoern A. Zeeb #define V_icmpmaskrepl VNET(icmpmaskrepl) 1096df8a710SGleb Smirnoff SYSCTL_INT(_net_inet_icmp, ICMPCTL_MASKREPL, maskrepl, CTLFLAG_VNET | CTLFLAG_RW, 110eddfbb76SRobert Watson &VNET_NAME(icmpmaskrepl), 0, 111e8800f3cSMark Johnston "Reply to ICMP Address Mask Request packets"); 11257842a38SMatthew N. Dodd 1135f901c92SAndrew Turner VNET_DEFINE_STATIC(u_int, icmpmaskfake) = 0; 11482cea7e6SBjoern A. Zeeb #define V_icmpmaskfake VNET(icmpmaskfake) 1156df8a710SGleb Smirnoff SYSCTL_UINT(_net_inet_icmp, OID_AUTO, maskfake, CTLFLAG_VNET | CTLFLAG_RW, 116eddfbb76SRobert Watson &VNET_NAME(icmpmaskfake), 0, 117e8800f3cSMark Johnston "Fake reply to ICMP Address Mask Request packets"); 1180312fbe9SPoul-Henning Kamp 1193c2824b9SAlexander V. Chernikov VNET_DEFINE(int, drop_redirect) = 0; 120670e8b3bSAlexander V. Chernikov #define V_drop_redirect VNET(drop_redirect) 121670e8b3bSAlexander V. Chernikov SYSCTL_INT(_net_inet_icmp, OID_AUTO, drop_redirect, CTLFLAG_VNET | CTLFLAG_RW, 122e8800f3cSMark Johnston &VNET_NAME(drop_redirect), 0, 123e8800f3cSMark Johnston "Ignore ICMP redirects"); 12418d3153eSDag-Erling Smørgrav 1255f901c92SAndrew Turner VNET_DEFINE_STATIC(int, log_redirect) = 0; 12682cea7e6SBjoern A. Zeeb #define V_log_redirect VNET(log_redirect) 1276df8a710SGleb Smirnoff SYSCTL_INT(_net_inet_icmp, OID_AUTO, log_redirect, CTLFLAG_VNET | CTLFLAG_RW, 128eddfbb76SRobert Watson &VNET_NAME(log_redirect), 0, 129eddfbb76SRobert Watson "Log ICMP redirects to the console"); 1306c3b5f69SDag-Erling Smørgrav 1315f901c92SAndrew Turner VNET_DEFINE_STATIC(char, reply_src[IFNAMSIZ]); 13282cea7e6SBjoern A. Zeeb #define V_reply_src VNET(reply_src) 1336df8a710SGleb Smirnoff SYSCTL_STRING(_net_inet_icmp, OID_AUTO, reply_src, CTLFLAG_VNET | CTLFLAG_RW, 134eddfbb76SRobert Watson &VNET_NAME(reply_src), IFNAMSIZ, 135e8800f3cSMark Johnston "ICMP reply source for non-local packets"); 136b74d89bbSAndre Oppermann 1375f901c92SAndrew Turner VNET_DEFINE_STATIC(int, icmp_rfi) = 0; 13882cea7e6SBjoern A. Zeeb #define V_icmp_rfi VNET(icmp_rfi) 1396df8a710SGleb Smirnoff SYSCTL_INT(_net_inet_icmp, OID_AUTO, reply_from_interface, CTLFLAG_VNET | CTLFLAG_RW, 140eddfbb76SRobert Watson &VNET_NAME(icmp_rfi), 0, 141eddfbb76SRobert Watson "ICMP reply from incoming interface for non-local packets"); 1428de9ac5eSRandall Stewart /* Router requirements RFC 1812 section 4.3.2.3 requires 576 - 28. */ 1431e0582fdSAndrew Turner VNET_DEFINE_STATIC(int, icmp_quotelen) = 548; 14482cea7e6SBjoern A. Zeeb #define V_icmp_quotelen VNET(icmp_quotelen) 1456df8a710SGleb Smirnoff SYSCTL_INT(_net_inet_icmp, OID_AUTO, quotelen, CTLFLAG_VNET | CTLFLAG_RW, 146eddfbb76SRobert Watson &VNET_NAME(icmp_quotelen), 0, 147eddfbb76SRobert Watson "Number of bytes from original packet to quote in ICMP reply"); 148e875dfb8SAndre Oppermann 1495f901c92SAndrew Turner VNET_DEFINE_STATIC(int, icmpbmcastecho) = 0; 15082cea7e6SBjoern A. Zeeb #define V_icmpbmcastecho VNET(icmpbmcastecho) 1516df8a710SGleb Smirnoff SYSCTL_INT(_net_inet_icmp, OID_AUTO, bmcastecho, CTLFLAG_VNET | CTLFLAG_RW, 152eddfbb76SRobert Watson &VNET_NAME(icmpbmcastecho), 0, 153e8800f3cSMark Johnston "Reply to multicast ICMP Echo Request and Timestamp packets"); 1547022ea0aSGarrett Wollman 1555f901c92SAndrew Turner VNET_DEFINE_STATIC(int, icmptstamprepl) = 1; 15600cb6befSMark Johnston #define V_icmptstamprepl VNET(icmptstamprepl) 15700cb6befSMark Johnston SYSCTL_INT(_net_inet_icmp, OID_AUTO, tstamprepl, CTLFLAG_RW, 158e8800f3cSMark Johnston &VNET_NAME(icmptstamprepl), 0, 159e8800f3cSMark Johnston "Respond to ICMP Timestamp packets"); 16051508de1SMatthew Dillon 161*410634efSEugene Grosbein VNET_DEFINE_STATIC(int, error_keeptags) = 0; 162*410634efSEugene Grosbein #define V_error_keeptags VNET(error_keeptags) 163*410634efSEugene Grosbein SYSCTL_INT(_net_inet_icmp, OID_AUTO, error_keeptags, CTLFLAG_VNET | CTLFLAG_RW, 164*410634efSEugene Grosbein &VNET_NAME(error_keeptags), 0, 165*410634efSEugene Grosbein "ICMP error response keeps copy of mbuf_tags of original packet"); 166*410634efSEugene Grosbein 167df8bae1dSRodney W. Grimes #ifdef ICMPPRINTFS 168df8bae1dSRodney W. Grimes int icmpprintfs = 0; 169df8bae1dSRodney W. Grimes #endif 170df8bae1dSRodney W. Grimes 1714d77a549SAlfred Perlstein static void icmp_reflect(struct mbuf *); 17202c1c707SAndre Oppermann static void icmp_send(struct mbuf *, struct mbuf *); 1730312fbe9SPoul-Henning Kamp 174df8bae1dSRodney W. Grimes extern struct protosw inetsw[]; 175df8bae1dSRodney W. Grimes 176df8bae1dSRodney W. Grimes /* 177315e3e38SRobert Watson * Kernel module interface for updating icmpstat. The argument is an index 178315e3e38SRobert Watson * into icmpstat treated as an array of u_long. While this encodes the 179315e3e38SRobert Watson * general layout of icmpstat into the caller, it doesn't encode its 180315e3e38SRobert Watson * location, so that future changes to add, for example, per-CPU stats 181315e3e38SRobert Watson * support won't cause binary compatibility problems for kernel modules. 182315e3e38SRobert Watson */ 183315e3e38SRobert Watson void 184315e3e38SRobert Watson kmod_icmpstat_inc(int statnum) 185315e3e38SRobert Watson { 186315e3e38SRobert Watson 1875b7cb97cSAndrey V. Elsukov counter_u64_add(VNET(icmpstat)[statnum], 1); 188315e3e38SRobert Watson } 189315e3e38SRobert Watson 190315e3e38SRobert Watson /* 191df8bae1dSRodney W. Grimes * Generate an error packet of type error 192df8bae1dSRodney W. Grimes * in response to bad packet ip. 193df8bae1dSRodney W. Grimes */ 194df8bae1dSRodney W. Grimes void 195d685b6eeSLuigi Rizzo icmp_error(struct mbuf *n, int type, int code, uint32_t dest, int mtu) 196df8bae1dSRodney W. Grimes { 197f415d666SAndrey V. Elsukov struct ip *oip, *nip; 1983e85b721SEd Maste struct icmp *icp; 1993e85b721SEd Maste struct mbuf *m; 200f415d666SAndrey V. Elsukov unsigned icmplen, icmpelen, nlen, oiphlen; 201df8bae1dSRodney W. Grimes 202f415d666SAndrey V. Elsukov KASSERT((u_int)type <= ICMP_MAXTYPE, ("%s: illegal ICMP type", 203f415d666SAndrey V. Elsukov __func__)); 204f415d666SAndrey V. Elsukov 205df8bae1dSRodney W. Grimes if (type != ICMP_REDIRECT) 206e27b0c87SRobert Watson ICMPSTAT_INC(icps_error); 207df8bae1dSRodney W. Grimes /* 208e86ebebcSAndre Oppermann * Don't send error: 209e86ebebcSAndre Oppermann * if the original packet was encrypted. 210e86ebebcSAndre Oppermann * if not the first fragment of message. 211e86ebebcSAndre Oppermann * in response to a multicast or broadcast packet. 212e86ebebcSAndre Oppermann * if the old packet protocol was an ICMP error message. 213df8bae1dSRodney W. Grimes */ 214cad1917dSHajimu UMEMOTO if (n->m_flags & M_DECRYPTED) 215cad1917dSHajimu UMEMOTO goto freeit; 216e86ebebcSAndre Oppermann if (n->m_flags & (M_BCAST|M_MCAST)) 217e86ebebcSAndre Oppermann goto freeit; 218f415d666SAndrey V. Elsukov 219f415d666SAndrey V. Elsukov /* Drop if IP header plus 8 bytes is not contiguous in first mbuf. */ 220f415d666SAndrey V. Elsukov if (n->m_len < sizeof(struct ip) + ICMP_MINLEN) 221f415d666SAndrey V. Elsukov goto freeit; 222f415d666SAndrey V. Elsukov oip = mtod(n, struct ip *); 223f415d666SAndrey V. Elsukov oiphlen = oip->ip_hl << 2; 224f415d666SAndrey V. Elsukov if (n->m_len < oiphlen + ICMP_MINLEN) 225f415d666SAndrey V. Elsukov goto freeit; 226f415d666SAndrey V. Elsukov #ifdef ICMPPRINTFS 227f415d666SAndrey V. Elsukov if (icmpprintfs) 228f415d666SAndrey V. Elsukov printf("icmp_error(%p, %x, %d)\n", oip, type, code); 229f415d666SAndrey V. Elsukov #endif 230f415d666SAndrey V. Elsukov if (oip->ip_off & htons(~(IP_MF|IP_DF))) 231f415d666SAndrey V. Elsukov goto freeit; 232df8bae1dSRodney W. Grimes if (oip->ip_p == IPPROTO_ICMP && type != ICMP_REDIRECT && 233f415d666SAndrey V. Elsukov !ICMP_INFOTYPE(((struct icmp *)((caddr_t)oip + 234f415d666SAndrey V. Elsukov oiphlen))->icmp_type)) { 235e27b0c87SRobert Watson ICMPSTAT_INC(icps_oldicmp); 236df8bae1dSRodney W. Grimes goto freeit; 237df8bae1dSRodney W. Grimes } 238df8bae1dSRodney W. Grimes /* 239e86ebebcSAndre Oppermann * Calculate length to quote from original packet and 240e86ebebcSAndre Oppermann * prevent the ICMP mbuf from overflowing. 241a4641f4eSPedro F. Giffuni * Unfortunately this is non-trivial since ip_forward() 242e86ebebcSAndre Oppermann * sends us truncated packets. 243df8bae1dSRodney W. Grimes */ 244e86ebebcSAndre Oppermann nlen = m_length(n, NULL); 245e86ebebcSAndre Oppermann if (oip->ip_p == IPPROTO_TCP) { 246e86ebebcSAndre Oppermann struct tcphdr *th; 247e86ebebcSAndre Oppermann int tcphlen; 248e86ebebcSAndre Oppermann 249e86ebebcSAndre Oppermann if (oiphlen + sizeof(struct tcphdr) > n->m_len && 250e86ebebcSAndre Oppermann n->m_next == NULL) 251e86ebebcSAndre Oppermann goto stdreply; 252e86ebebcSAndre Oppermann if (n->m_len < oiphlen + sizeof(struct tcphdr) && 253f415d666SAndrey V. Elsukov (n = m_pullup(n, oiphlen + sizeof(struct tcphdr))) == NULL) 254e86ebebcSAndre Oppermann goto freeit; 255f415d666SAndrey V. Elsukov oip = mtod(n, struct ip *); 256f415d666SAndrey V. Elsukov th = mtodo(n, oiphlen); 257e86ebebcSAndre Oppermann tcphlen = th->th_off << 2; 258e86ebebcSAndre Oppermann if (tcphlen < sizeof(struct tcphdr)) 259e86ebebcSAndre Oppermann goto freeit; 2608f134647SGleb Smirnoff if (ntohs(oip->ip_len) < oiphlen + tcphlen) 261e86ebebcSAndre Oppermann goto freeit; 262e86ebebcSAndre Oppermann if (oiphlen + tcphlen > n->m_len && n->m_next == NULL) 263e86ebebcSAndre Oppermann goto stdreply; 264e86ebebcSAndre Oppermann if (n->m_len < oiphlen + tcphlen && 265f415d666SAndrey V. Elsukov (n = m_pullup(n, oiphlen + tcphlen)) == NULL) 266e86ebebcSAndre Oppermann goto freeit; 2678f134647SGleb Smirnoff icmpelen = max(tcphlen, min(V_icmp_quotelen, 2688f134647SGleb Smirnoff ntohs(oip->ip_len) - oiphlen)); 269f88d0cfeSMichael Tuexen } else if (oip->ip_p == IPPROTO_SCTP) { 270f88d0cfeSMichael Tuexen struct sctphdr *sh; 271f88d0cfeSMichael Tuexen struct sctp_chunkhdr *ch; 272f88d0cfeSMichael Tuexen 273f88d0cfeSMichael Tuexen if (ntohs(oip->ip_len) < oiphlen + sizeof(struct sctphdr)) 274f88d0cfeSMichael Tuexen goto stdreply; 275f88d0cfeSMichael Tuexen if (oiphlen + sizeof(struct sctphdr) > n->m_len && 276f88d0cfeSMichael Tuexen n->m_next == NULL) 277f88d0cfeSMichael Tuexen goto stdreply; 278f88d0cfeSMichael Tuexen if (n->m_len < oiphlen + sizeof(struct sctphdr) && 279f88d0cfeSMichael Tuexen (n = m_pullup(n, oiphlen + sizeof(struct sctphdr))) == NULL) 280f88d0cfeSMichael Tuexen goto freeit; 281f415d666SAndrey V. Elsukov oip = mtod(n, struct ip *); 282f88d0cfeSMichael Tuexen icmpelen = max(sizeof(struct sctphdr), 283f88d0cfeSMichael Tuexen min(V_icmp_quotelen, ntohs(oip->ip_len) - oiphlen)); 284f415d666SAndrey V. Elsukov sh = mtodo(n, oiphlen); 285f88d0cfeSMichael Tuexen if (ntohl(sh->v_tag) == 0 && 286f415d666SAndrey V. Elsukov ntohs(oip->ip_len) >= oiphlen + 287f415d666SAndrey V. Elsukov sizeof(struct sctphdr) + 8 && 288f88d0cfeSMichael Tuexen (n->m_len >= oiphlen + sizeof(struct sctphdr) + 8 || 289f88d0cfeSMichael Tuexen n->m_next != NULL)) { 290f88d0cfeSMichael Tuexen if (n->m_len < oiphlen + sizeof(struct sctphdr) + 8 && 291f415d666SAndrey V. Elsukov (n = m_pullup(n, oiphlen + 292f415d666SAndrey V. Elsukov sizeof(struct sctphdr) + 8)) == NULL) 293f88d0cfeSMichael Tuexen goto freeit; 294f415d666SAndrey V. Elsukov oip = mtod(n, struct ip *); 295f415d666SAndrey V. Elsukov sh = mtodo(n, oiphlen); 296f88d0cfeSMichael Tuexen ch = (struct sctp_chunkhdr *)(sh + 1); 297f88d0cfeSMichael Tuexen if (ch->chunk_type == SCTP_INITIATION) { 298f88d0cfeSMichael Tuexen icmpelen = max(sizeof(struct sctphdr) + 8, 299f415d666SAndrey V. Elsukov min(V_icmp_quotelen, ntohs(oip->ip_len) - 300f415d666SAndrey V. Elsukov oiphlen)); 301f88d0cfeSMichael Tuexen } 302f88d0cfeSMichael Tuexen } 303e86ebebcSAndre Oppermann } else 304f415d666SAndrey V. Elsukov stdreply: icmpelen = max(8, min(V_icmp_quotelen, ntohs(oip->ip_len) - 305f415d666SAndrey V. Elsukov oiphlen)); 306e86ebebcSAndre Oppermann 307e86ebebcSAndre Oppermann icmplen = min(oiphlen + icmpelen, nlen); 308e86ebebcSAndre Oppermann if (icmplen < sizeof(struct ip)) 309e86ebebcSAndre Oppermann goto freeit; 310e86ebebcSAndre Oppermann 311e86ebebcSAndre Oppermann if (MHLEN > sizeof(struct ip) + ICMP_MINLEN + icmplen) 312eb1b1807SGleb Smirnoff m = m_gethdr(M_NOWAIT, MT_DATA); 313e86ebebcSAndre Oppermann else 314eb1b1807SGleb Smirnoff m = m_getcl(M_NOWAIT, MT_DATA, M_PKTHDR); 315df8bae1dSRodney W. Grimes if (m == NULL) 316df8bae1dSRodney W. Grimes goto freeit; 3170070e096SRobert Watson #ifdef MAC 318a13e21f7SRobert Watson mac_netinet_icmp_reply(n, m); 3190070e096SRobert Watson #endif 320f415d666SAndrey V. Elsukov icmplen = min(icmplen, M_TRAILINGSPACE(m) - 321f415d666SAndrey V. Elsukov sizeof(struct ip) - ICMP_MINLEN); 322e86ebebcSAndre Oppermann m_align(m, ICMP_MINLEN + icmplen); 323e86ebebcSAndre Oppermann m->m_len = ICMP_MINLEN + icmplen; 3246b773dffSAndre Oppermann 3258b07e49aSJulian Elischer /* XXX MRT make the outgoing packet use the same FIB 3268b07e49aSJulian Elischer * that was associated with the incoming packet 3278b07e49aSJulian Elischer */ 3288b07e49aSJulian Elischer M_SETFIB(m, M_GETFIB(n)); 329df8bae1dSRodney W. Grimes icp = mtod(m, struct icmp *); 330e27b0c87SRobert Watson ICMPSTAT_INC(icps_outhist[type]); 331df8bae1dSRodney W. Grimes icp->icmp_type = type; 332df8bae1dSRodney W. Grimes if (type == ICMP_REDIRECT) 333df8bae1dSRodney W. Grimes icp->icmp_gwaddr.s_addr = dest; 334df8bae1dSRodney W. Grimes else { 335df8bae1dSRodney W. Grimes icp->icmp_void = 0; 336df8bae1dSRodney W. Grimes /* 337df8bae1dSRodney W. Grimes * The following assignments assume an overlay with the 338e86ebebcSAndre Oppermann * just zeroed icmp_void field. 339df8bae1dSRodney W. Grimes */ 340df8bae1dSRodney W. Grimes if (type == ICMP_PARAMPROB) { 341df8bae1dSRodney W. Grimes icp->icmp_pptr = code; 342df8bae1dSRodney W. Grimes code = 0; 343df8bae1dSRodney W. Grimes } else if (type == ICMP_UNREACH && 344c773494eSAndre Oppermann code == ICMP_UNREACH_NEEDFRAG && mtu) { 345c773494eSAndre Oppermann icp->icmp_nextmtu = htons(mtu); 346df8bae1dSRodney W. Grimes } 347df8bae1dSRodney W. Grimes } 348df8bae1dSRodney W. Grimes icp->icmp_code = code; 34904287599SRuslan Ermilov 35004287599SRuslan Ermilov /* 351e86ebebcSAndre Oppermann * Copy the quotation into ICMP message and 352e86ebebcSAndre Oppermann * convert quoted IP header back to network representation. 35304287599SRuslan Ermilov */ 354e86ebebcSAndre Oppermann m_copydata(n, 0, icmplen, (caddr_t)&icp->icmp_ip); 355e86ebebcSAndre Oppermann nip = &icp->icmp_ip; 356df8bae1dSRodney W. Grimes 357df8bae1dSRodney W. Grimes /* 358e86ebebcSAndre Oppermann * Set up ICMP message mbuf and copy old IP header (without options 359e86ebebcSAndre Oppermann * in front of ICMP message. 360c550f220SMax Laier * If the original mbuf was meant to bypass the firewall, the error 361c550f220SMax Laier * reply should bypass as well. 362c550f220SMax Laier */ 363c550f220SMax Laier m->m_flags |= n->m_flags & M_SKIP_FIREWALL; 364df8bae1dSRodney W. Grimes m->m_data -= sizeof(struct ip); 365df8bae1dSRodney W. Grimes m->m_len += sizeof(struct ip); 366df8bae1dSRodney W. Grimes m->m_pkthdr.len = m->m_len; 367df8bae1dSRodney W. Grimes m->m_pkthdr.rcvif = n->m_pkthdr.rcvif; 368df8bae1dSRodney W. Grimes nip = mtod(m, struct ip *); 369df8bae1dSRodney W. Grimes bcopy((caddr_t)oip, (caddr_t)nip, sizeof(struct ip)); 3708f134647SGleb Smirnoff nip->ip_len = htons(m->m_len); 37153be11f6SPoul-Henning Kamp nip->ip_v = IPVERSION; 37253be11f6SPoul-Henning Kamp nip->ip_hl = 5; 373df8bae1dSRodney W. Grimes nip->ip_p = IPPROTO_ICMP; 374df8bae1dSRodney W. Grimes nip->ip_tos = 0; 37541ea685cSAndrey V. Elsukov nip->ip_off = 0; 376*410634efSEugene Grosbein 377*410634efSEugene Grosbein if (V_error_keeptags) 378*410634efSEugene Grosbein m_tag_copy_chain(m, n, M_NOWAIT); 379*410634efSEugene Grosbein 380df8bae1dSRodney W. Grimes icmp_reflect(m); 381df8bae1dSRodney W. Grimes 382df8bae1dSRodney W. Grimes freeit: 383df8bae1dSRodney W. Grimes m_freem(n); 384df8bae1dSRodney W. Grimes } 385df8bae1dSRodney W. Grimes 386df8bae1dSRodney W. Grimes /* 387df8bae1dSRodney W. Grimes * Process a received ICMP message. 388df8bae1dSRodney W. Grimes */ 3898f5a8818SKevin Lo int 3908f5a8818SKevin Lo icmp_input(struct mbuf **mp, int *offp, int proto) 391df8bae1dSRodney W. Grimes { 39216d6c90fSAndre Oppermann struct icmp *icp; 393df8bae1dSRodney W. Grimes struct in_ifaddr *ia; 3948f5a8818SKevin Lo struct mbuf *m = *mp; 39516d6c90fSAndre Oppermann struct ip *ip = mtod(m, struct ip *); 39616d6c90fSAndre Oppermann struct sockaddr_in icmpsrc, icmpdst, icmpgw; 3978f5a8818SKevin Lo int hlen = *offp; 3988f5a8818SKevin Lo int icmplen = ntohs(ip->ip_len) - *offp; 39916d6c90fSAndre Oppermann int i, code; 4004d77a549SAlfred Perlstein void (*ctlfunc)(int, struct sockaddr *, void *); 4018b07e49aSJulian Elischer int fibnum; 402df8bae1dSRodney W. Grimes 4038f5a8818SKevin Lo *mp = NULL; 4048f5a8818SKevin Lo 405df8bae1dSRodney W. Grimes /* 406df8bae1dSRodney W. Grimes * Locate icmp structure in mbuf, and check 407df8bae1dSRodney W. Grimes * that not corrupted and of at least minimum length. 408df8bae1dSRodney W. Grimes */ 409df8bae1dSRodney W. Grimes #ifdef ICMPPRINTFS 4102b758395SGarrett Wollman if (icmpprintfs) { 4118144690aSEric van Gyzen char srcbuf[INET_ADDRSTRLEN]; 4128144690aSEric van Gyzen char dstbuf[INET_ADDRSTRLEN]; 4138144690aSEric van Gyzen 4142b758395SGarrett Wollman printf("icmp_input from %s to %s, len %d\n", 4158144690aSEric van Gyzen inet_ntoa_r(ip->ip_src, srcbuf), 4168144690aSEric van Gyzen inet_ntoa_r(ip->ip_dst, dstbuf), icmplen); 4172b758395SGarrett Wollman } 418df8bae1dSRodney W. Grimes #endif 4194f6c66ccSMatt Macy NET_EPOCH_ENTER(); 420df8bae1dSRodney W. Grimes if (icmplen < ICMP_MINLEN) { 421e27b0c87SRobert Watson ICMPSTAT_INC(icps_tooshort); 422df8bae1dSRodney W. Grimes goto freeit; 423df8bae1dSRodney W. Grimes } 424df8bae1dSRodney W. Grimes i = hlen + min(icmplen, ICMP_ADVLENMIN); 425852da713SBjoern A. Zeeb if (m->m_len < i && (m = m_pullup(m, i)) == NULL) { 426e27b0c87SRobert Watson ICMPSTAT_INC(icps_tooshort); 4274f6c66ccSMatt Macy NET_EPOCH_EXIT(); 4288f5a8818SKevin Lo return (IPPROTO_DONE); 429df8bae1dSRodney W. Grimes } 430df8bae1dSRodney W. Grimes ip = mtod(m, struct ip *); 431df8bae1dSRodney W. Grimes m->m_len -= hlen; 432df8bae1dSRodney W. Grimes m->m_data += hlen; 433df8bae1dSRodney W. Grimes icp = mtod(m, struct icmp *); 434df8bae1dSRodney W. Grimes if (in_cksum(m, icmplen)) { 435e27b0c87SRobert Watson ICMPSTAT_INC(icps_checksum); 436df8bae1dSRodney W. Grimes goto freeit; 437df8bae1dSRodney W. Grimes } 438df8bae1dSRodney W. Grimes m->m_len += hlen; 439df8bae1dSRodney W. Grimes m->m_data -= hlen; 440df8bae1dSRodney W. Grimes 441df8bae1dSRodney W. Grimes #ifdef ICMPPRINTFS 442df8bae1dSRodney W. Grimes if (icmpprintfs) 443df8bae1dSRodney W. Grimes printf("icmp_input, type %d code %d\n", icp->icmp_type, 444df8bae1dSRodney W. Grimes icp->icmp_code); 445df8bae1dSRodney W. Grimes #endif 4465b7ee6edSGarrett Wollman 4475b7ee6edSGarrett Wollman /* 4485b7ee6edSGarrett Wollman * Message type specific processing. 4495b7ee6edSGarrett Wollman */ 450df8bae1dSRodney W. Grimes if (icp->icmp_type > ICMP_MAXTYPE) 451df8bae1dSRodney W. Grimes goto raw; 45216d6c90fSAndre Oppermann 45316d6c90fSAndre Oppermann /* Initialize */ 45416d6c90fSAndre Oppermann bzero(&icmpsrc, sizeof(icmpsrc)); 45516d6c90fSAndre Oppermann icmpsrc.sin_len = sizeof(struct sockaddr_in); 45616d6c90fSAndre Oppermann icmpsrc.sin_family = AF_INET; 45716d6c90fSAndre Oppermann bzero(&icmpdst, sizeof(icmpdst)); 45816d6c90fSAndre Oppermann icmpdst.sin_len = sizeof(struct sockaddr_in); 45916d6c90fSAndre Oppermann icmpdst.sin_family = AF_INET; 46016d6c90fSAndre Oppermann bzero(&icmpgw, sizeof(icmpgw)); 46116d6c90fSAndre Oppermann icmpgw.sin_len = sizeof(struct sockaddr_in); 46216d6c90fSAndre Oppermann icmpgw.sin_family = AF_INET; 46316d6c90fSAndre Oppermann 464e27b0c87SRobert Watson ICMPSTAT_INC(icps_inhist[icp->icmp_type]); 465df8bae1dSRodney W. Grimes code = icp->icmp_code; 466df8bae1dSRodney W. Grimes switch (icp->icmp_type) { 467df8bae1dSRodney W. Grimes 468df8bae1dSRodney W. Grimes case ICMP_UNREACH: 469df8bae1dSRodney W. Grimes switch (code) { 470df8bae1dSRodney W. Grimes case ICMP_UNREACH_NET: 471df8bae1dSRodney W. Grimes case ICMP_UNREACH_HOST: 472df8bae1dSRodney W. Grimes case ICMP_UNREACH_SRCFAIL: 473e4bb5b05SJonathan Lemon case ICMP_UNREACH_NET_UNKNOWN: 474e4bb5b05SJonathan Lemon case ICMP_UNREACH_HOST_UNKNOWN: 475e4bb5b05SJonathan Lemon case ICMP_UNREACH_ISOLATED: 476e4bb5b05SJonathan Lemon case ICMP_UNREACH_TOSNET: 477e4bb5b05SJonathan Lemon case ICMP_UNREACH_TOSHOST: 478e4bb5b05SJonathan Lemon case ICMP_UNREACH_HOST_PRECEDENCE: 479e4bb5b05SJonathan Lemon case ICMP_UNREACH_PRECEDENCE_CUTOFF: 480e4bb5b05SJonathan Lemon code = PRC_UNREACH_NET; 481df8bae1dSRodney W. Grimes break; 482df8bae1dSRodney W. Grimes 483df8bae1dSRodney W. Grimes case ICMP_UNREACH_NEEDFRAG: 484df8bae1dSRodney W. Grimes code = PRC_MSGSIZE; 485df8bae1dSRodney W. Grimes break; 486df8bae1dSRodney W. Grimes 487e4bb5b05SJonathan Lemon /* 488e4bb5b05SJonathan Lemon * RFC 1122, Sections 3.2.2.1 and 4.2.3.9. 489e4bb5b05SJonathan Lemon * Treat subcodes 2,3 as immediate RST 490e4bb5b05SJonathan Lemon */ 491e4bb5b05SJonathan Lemon case ICMP_UNREACH_PROTOCOL: 4923e146575SMichael Tuexen code = PRC_UNREACH_PROTOCOL; 4933e146575SMichael Tuexen break; 494e4bb5b05SJonathan Lemon case ICMP_UNREACH_PORT: 495b77d155dSJesper Skriver code = PRC_UNREACH_PORT; 496b11d7a4aSPoul-Henning Kamp break; 49790fcbbd6SPoul-Henning Kamp 49890fcbbd6SPoul-Henning Kamp case ICMP_UNREACH_NET_PROHIB: 49990fcbbd6SPoul-Henning Kamp case ICMP_UNREACH_HOST_PROHIB: 5009c4b2574SPaul Traina case ICMP_UNREACH_FILTER_PROHIB: 50190fcbbd6SPoul-Henning Kamp code = PRC_UNREACH_ADMIN_PROHIB; 502b11d7a4aSPoul-Henning Kamp break; 503b11d7a4aSPoul-Henning Kamp 504df8bae1dSRodney W. Grimes default: 505df8bae1dSRodney W. Grimes goto badcode; 506df8bae1dSRodney W. Grimes } 507df8bae1dSRodney W. Grimes goto deliver; 508df8bae1dSRodney W. Grimes 509df8bae1dSRodney W. Grimes case ICMP_TIMXCEED: 510df8bae1dSRodney W. Grimes if (code > 1) 511df8bae1dSRodney W. Grimes goto badcode; 512df8bae1dSRodney W. Grimes code += PRC_TIMXCEED_INTRANS; 513df8bae1dSRodney W. Grimes goto deliver; 514df8bae1dSRodney W. Grimes 515df8bae1dSRodney W. Grimes case ICMP_PARAMPROB: 516df8bae1dSRodney W. Grimes if (code > 1) 517df8bae1dSRodney W. Grimes goto badcode; 518df8bae1dSRodney W. Grimes code = PRC_PARAMPROB; 519df8bae1dSRodney W. Grimes deliver: 520df8bae1dSRodney W. Grimes /* 521df8bae1dSRodney W. Grimes * Problem with datagram; advise higher level routines. 522df8bae1dSRodney W. Grimes */ 523df8bae1dSRodney W. Grimes if (icmplen < ICMP_ADVLENMIN || icmplen < ICMP_ADVLEN(icp) || 52453be11f6SPoul-Henning Kamp icp->icmp_ip.ip_hl < (sizeof(struct ip) >> 2)) { 525e27b0c87SRobert Watson ICMPSTAT_INC(icps_badlen); 526df8bae1dSRodney W. Grimes goto freeit; 527df8bae1dSRodney W. Grimes } 5285b7ee6edSGarrett Wollman /* Discard ICMP's in response to multicast packets */ 5295b7ee6edSGarrett Wollman if (IN_MULTICAST(ntohl(icp->icmp_ip.ip_dst.s_addr))) 5305b7ee6edSGarrett Wollman goto badcode; 531df8bae1dSRodney W. Grimes #ifdef ICMPPRINTFS 532df8bae1dSRodney W. Grimes if (icmpprintfs) 533df8bae1dSRodney W. Grimes printf("deliver to protocol %d\n", icp->icmp_ip.ip_p); 534df8bae1dSRodney W. Grimes #endif 535df8bae1dSRodney W. Grimes icmpsrc.sin_addr = icp->icmp_ip.ip_dst; 5366a800098SYoshinobu Inoue /* 5376a800098SYoshinobu Inoue * XXX if the packet contains [IPv4 AH TCP], we can't make a 5386a800098SYoshinobu Inoue * notification to TCP layer. 5396a800098SYoshinobu Inoue */ 540f77b8427SMichael Tuexen i = sizeof(struct ip) + min(icmplen, ICMP_ADVLENPREF(icp)); 541f77b8427SMichael Tuexen ip_stripoptions(m); 542f77b8427SMichael Tuexen if (m->m_len < i && (m = m_pullup(m, i)) == NULL) { 543f77b8427SMichael Tuexen /* This should actually not happen */ 544f77b8427SMichael Tuexen ICMPSTAT_INC(icps_tooshort); 5454f6c66ccSMatt Macy NET_EPOCH_EXIT(); 546f77b8427SMichael Tuexen return (IPPROTO_DONE); 547f77b8427SMichael Tuexen } 548f77b8427SMichael Tuexen ip = mtod(m, struct ip *); 549f77b8427SMichael Tuexen icp = (struct icmp *)(ip + 1); 550f77b8427SMichael Tuexen /* 551f77b8427SMichael Tuexen * The upper layer handler can rely on: 552f77b8427SMichael Tuexen * - The outer IP header has no options. 553f77b8427SMichael Tuexen * - The outer IP header, the ICMP header, the inner IP header, 554f77b8427SMichael Tuexen * and the first n bytes of the inner payload are contiguous. 555f77b8427SMichael Tuexen * n is at least 8, but might be larger based on 556f77b8427SMichael Tuexen * ICMP_ADVLENPREF. See its definition in ip_icmp.h. 557f77b8427SMichael Tuexen */ 558623ae52eSPoul-Henning Kamp ctlfunc = inetsw[ip_protox[icp->icmp_ip.ip_p]].pr_ctlinput; 559623ae52eSPoul-Henning Kamp if (ctlfunc) 560df8bae1dSRodney W. Grimes (*ctlfunc)(code, (struct sockaddr *)&icmpsrc, 561b62d102cSBruce Evans (void *)&icp->icmp_ip); 562df8bae1dSRodney W. Grimes break; 563df8bae1dSRodney W. Grimes 564df8bae1dSRodney W. Grimes badcode: 565e27b0c87SRobert Watson ICMPSTAT_INC(icps_badcode); 566df8bae1dSRodney W. Grimes break; 567df8bae1dSRodney W. Grimes 568df8bae1dSRodney W. Grimes case ICMP_ECHO: 5698b615593SMarko Zec if (!V_icmpbmcastecho 570d311884fSDavid Greenman && (m->m_flags & (M_MCAST | M_BCAST)) != 0) { 571e27b0c87SRobert Watson ICMPSTAT_INC(icps_bmcastecho); 5727022ea0aSGarrett Wollman break; 5737022ea0aSGarrett Wollman } 574a57815efSBosko Milekic if (badport_bandlim(BANDLIM_ICMP_ECHO) < 0) 57509f81a46SBosko Milekic goto freeit; 576382a6bbcSJonathan T. Looney icp->icmp_type = ICMP_ECHOREPLY; 577df8bae1dSRodney W. Grimes goto reflect; 578df8bae1dSRodney W. Grimes 579df8bae1dSRodney W. Grimes case ICMP_TSTAMP: 58000cb6befSMark Johnston if (V_icmptstamprepl == 0) 58100cb6befSMark Johnston break; 5828b615593SMarko Zec if (!V_icmpbmcastecho 583d311884fSDavid Greenman && (m->m_flags & (M_MCAST | M_BCAST)) != 0) { 584e27b0c87SRobert Watson ICMPSTAT_INC(icps_bmcasttstamp); 585fe0fb8abSGarrett Wollman break; 586fe0fb8abSGarrett Wollman } 587df8bae1dSRodney W. Grimes if (icmplen < ICMP_TSLEN) { 588e27b0c87SRobert Watson ICMPSTAT_INC(icps_badlen); 589df8bae1dSRodney W. Grimes break; 590df8bae1dSRodney W. Grimes } 591382a6bbcSJonathan T. Looney if (badport_bandlim(BANDLIM_ICMP_TSTAMP) < 0) 592382a6bbcSJonathan T. Looney goto freeit; 593df8bae1dSRodney W. Grimes icp->icmp_type = ICMP_TSTAMPREPLY; 594df8bae1dSRodney W. Grimes icp->icmp_rtime = iptime(); 595df8bae1dSRodney W. Grimes icp->icmp_ttime = icp->icmp_rtime; /* bogus, do later! */ 596df8bae1dSRodney W. Grimes goto reflect; 597df8bae1dSRodney W. Grimes 598df8bae1dSRodney W. Grimes case ICMP_MASKREQ: 5998b615593SMarko Zec if (V_icmpmaskrepl == 0) 600df8bae1dSRodney W. Grimes break; 601df8bae1dSRodney W. Grimes /* 602df8bae1dSRodney W. Grimes * We are not able to respond with all ones broadcast 603df8bae1dSRodney W. Grimes * unless we receive it over a point-to-point interface. 604df8bae1dSRodney W. Grimes */ 605df8bae1dSRodney W. Grimes if (icmplen < ICMP_MASKLEN) 606df8bae1dSRodney W. Grimes break; 607df8bae1dSRodney W. Grimes switch (ip->ip_dst.s_addr) { 608df8bae1dSRodney W. Grimes 609df8bae1dSRodney W. Grimes case INADDR_BROADCAST: 610df8bae1dSRodney W. Grimes case INADDR_ANY: 611df8bae1dSRodney W. Grimes icmpdst.sin_addr = ip->ip_src; 612df8bae1dSRodney W. Grimes break; 613df8bae1dSRodney W. Grimes 614df8bae1dSRodney W. Grimes default: 615df8bae1dSRodney W. Grimes icmpdst.sin_addr = ip->ip_dst; 616df8bae1dSRodney W. Grimes } 617df8bae1dSRodney W. Grimes ia = (struct in_ifaddr *)ifaof_ifpforaddr( 618df8bae1dSRodney W. Grimes (struct sockaddr *)&icmpdst, m->m_pkthdr.rcvif); 6198c0fec80SRobert Watson if (ia == NULL) 620df8bae1dSRodney W. Grimes break; 6214f6c66ccSMatt Macy if (ia->ia_ifp == NULL) 6227e6f7714SPoul-Henning Kamp break; 623df8bae1dSRodney W. Grimes icp->icmp_type = ICMP_MASKREPLY; 6248b615593SMarko Zec if (V_icmpmaskfake == 0) 625df8bae1dSRodney W. Grimes icp->icmp_mask = ia->ia_sockmask.sin_addr.s_addr; 62657842a38SMatthew N. Dodd else 6278b615593SMarko Zec icp->icmp_mask = V_icmpmaskfake; 628df8bae1dSRodney W. Grimes if (ip->ip_src.s_addr == 0) { 629df8bae1dSRodney W. Grimes if (ia->ia_ifp->if_flags & IFF_BROADCAST) 630df8bae1dSRodney W. Grimes ip->ip_src = satosin(&ia->ia_broadaddr)->sin_addr; 631df8bae1dSRodney W. Grimes else if (ia->ia_ifp->if_flags & IFF_POINTOPOINT) 632df8bae1dSRodney W. Grimes ip->ip_src = satosin(&ia->ia_dstaddr)->sin_addr; 633df8bae1dSRodney W. Grimes } 634df8bae1dSRodney W. Grimes reflect: 635e27b0c87SRobert Watson ICMPSTAT_INC(icps_reflect); 636e27b0c87SRobert Watson ICMPSTAT_INC(icps_outhist[icp->icmp_type]); 637df8bae1dSRodney W. Grimes icmp_reflect(m); 6384f6c66ccSMatt Macy NET_EPOCH_EXIT(); 6398f5a8818SKevin Lo return (IPPROTO_DONE); 640df8bae1dSRodney W. Grimes 641df8bae1dSRodney W. Grimes case ICMP_REDIRECT: 6428b615593SMarko Zec if (V_log_redirect) { 64318d3153eSDag-Erling Smørgrav u_long src, dst, gw; 64418d3153eSDag-Erling Smørgrav 64518d3153eSDag-Erling Smørgrav src = ntohl(ip->ip_src.s_addr); 64618d3153eSDag-Erling Smørgrav dst = ntohl(icp->icmp_ip.ip_dst.s_addr); 64718d3153eSDag-Erling Smørgrav gw = ntohl(icp->icmp_gwaddr.s_addr); 64818d3153eSDag-Erling Smørgrav printf("icmp redirect from %d.%d.%d.%d: " 64918d3153eSDag-Erling Smørgrav "%d.%d.%d.%d => %d.%d.%d.%d\n", 65018d3153eSDag-Erling Smørgrav (int)(src >> 24), (int)((src >> 16) & 0xff), 65118d3153eSDag-Erling Smørgrav (int)((src >> 8) & 0xff), (int)(src & 0xff), 65218d3153eSDag-Erling Smørgrav (int)(dst >> 24), (int)((dst >> 16) & 0xff), 65318d3153eSDag-Erling Smørgrav (int)((dst >> 8) & 0xff), (int)(dst & 0xff), 65418d3153eSDag-Erling Smørgrav (int)(gw >> 24), (int)((gw >> 16) & 0xff), 65518d3153eSDag-Erling Smørgrav (int)((gw >> 8) & 0xff), (int)(gw & 0xff)); 65618d3153eSDag-Erling Smørgrav } 65787c3bd27SAndre Oppermann /* 65887c3bd27SAndre Oppermann * RFC1812 says we must ignore ICMP redirects if we 65987c3bd27SAndre Oppermann * are acting as router. 66087c3bd27SAndre Oppermann */ 6618b615593SMarko Zec if (V_drop_redirect || V_ipforwarding) 66218d3153eSDag-Erling Smørgrav break; 663df8bae1dSRodney W. Grimes if (code > 3) 664df8bae1dSRodney W. Grimes goto badcode; 665df8bae1dSRodney W. Grimes if (icmplen < ICMP_ADVLENMIN || icmplen < ICMP_ADVLEN(icp) || 66653be11f6SPoul-Henning Kamp icp->icmp_ip.ip_hl < (sizeof(struct ip) >> 2)) { 667e27b0c87SRobert Watson ICMPSTAT_INC(icps_badlen); 668df8bae1dSRodney W. Grimes break; 669df8bae1dSRodney W. Grimes } 670df8bae1dSRodney W. Grimes /* 671df8bae1dSRodney W. Grimes * Short circuit routing redirects to force 672df8bae1dSRodney W. Grimes * immediate change in the kernel's routing 673df8bae1dSRodney W. Grimes * tables. The message is also handed to anyone 674df8bae1dSRodney W. Grimes * listening on a raw socket (e.g. the routing 675df8bae1dSRodney W. Grimes * daemon for use in updating its tables). 676df8bae1dSRodney W. Grimes */ 677df8bae1dSRodney W. Grimes icmpgw.sin_addr = ip->ip_src; 678df8bae1dSRodney W. Grimes icmpdst.sin_addr = icp->icmp_gwaddr; 679df8bae1dSRodney W. Grimes #ifdef ICMPPRINTFS 6802b758395SGarrett Wollman if (icmpprintfs) { 6818144690aSEric van Gyzen char dstbuf[INET_ADDRSTRLEN]; 6828144690aSEric van Gyzen char gwbuf[INET_ADDRSTRLEN]; 6832b758395SGarrett Wollman 6842b758395SGarrett Wollman printf("redirect dst %s to %s\n", 6858144690aSEric van Gyzen inet_ntoa_r(icp->icmp_ip.ip_dst, dstbuf), 6868144690aSEric van Gyzen inet_ntoa_r(icp->icmp_gwaddr, gwbuf)); 6872b758395SGarrett Wollman } 688df8bae1dSRodney W. Grimes #endif 689df8bae1dSRodney W. Grimes icmpsrc.sin_addr = icp->icmp_ip.ip_dst; 6908b07e49aSJulian Elischer for ( fibnum = 0; fibnum < rt_numfibs; fibnum++) { 6918b07e49aSJulian Elischer in_rtredirect((struct sockaddr *)&icmpsrc, 692df8bae1dSRodney W. Grimes (struct sockaddr *)&icmpdst, 693df8bae1dSRodney W. Grimes (struct sockaddr *)0, RTF_GATEWAY | RTF_HOST, 6948b07e49aSJulian Elischer (struct sockaddr *)&icmpgw, fibnum); 6958b07e49aSJulian Elischer } 696df8bae1dSRodney W. Grimes pfctlinput(PRC_REDIRECT_HOST, (struct sockaddr *)&icmpsrc); 697df8bae1dSRodney W. Grimes break; 698df8bae1dSRodney W. Grimes 699df8bae1dSRodney W. Grimes /* 700df8bae1dSRodney W. Grimes * No kernel processing for the following; 701df8bae1dSRodney W. Grimes * just fall through to send to raw listener. 702df8bae1dSRodney W. Grimes */ 703df8bae1dSRodney W. Grimes case ICMP_ECHOREPLY: 704df8bae1dSRodney W. Grimes case ICMP_ROUTERADVERT: 705df8bae1dSRodney W. Grimes case ICMP_ROUTERSOLICIT: 706df8bae1dSRodney W. Grimes case ICMP_TSTAMPREPLY: 707df8bae1dSRodney W. Grimes case ICMP_IREQREPLY: 708df8bae1dSRodney W. Grimes case ICMP_MASKREPLY: 709d1f79a3bSAlexander V. Chernikov case ICMP_SOURCEQUENCH: 710df8bae1dSRodney W. Grimes default: 711df8bae1dSRodney W. Grimes break; 712df8bae1dSRodney W. Grimes } 713df8bae1dSRodney W. Grimes 714df8bae1dSRodney W. Grimes raw: 7154f6c66ccSMatt Macy NET_EPOCH_EXIT(); 7168f5a8818SKevin Lo *mp = m; 7178f5a8818SKevin Lo rip_input(mp, offp, proto); 7188f5a8818SKevin Lo return (IPPROTO_DONE); 719df8bae1dSRodney W. Grimes 720df8bae1dSRodney W. Grimes freeit: 7214f6c66ccSMatt Macy NET_EPOCH_EXIT(); 722df8bae1dSRodney W. Grimes m_freem(m); 7238f5a8818SKevin Lo return (IPPROTO_DONE); 724df8bae1dSRodney W. Grimes } 725df8bae1dSRodney W. Grimes 726df8bae1dSRodney W. Grimes /* 727df8bae1dSRodney W. Grimes * Reflect the ip packet back to the source 728df8bae1dSRodney W. Grimes */ 7290312fbe9SPoul-Henning Kamp static void 730f2565d68SRobert Watson icmp_reflect(struct mbuf *m) 731df8bae1dSRodney W. Grimes { 732cc0a3c8cSAndrey V. Elsukov struct rm_priotracker in_ifa_tracker; 733ca925d9cSJonathan Lemon struct ip *ip = mtod(m, struct ip *); 734ca925d9cSJonathan Lemon struct ifaddr *ifa; 73533c4f96dSRobert Watson struct ifnet *ifp; 736ca925d9cSJonathan Lemon struct in_ifaddr *ia; 737df8bae1dSRodney W. Grimes struct in_addr t; 7389977be4aSAlexander V. Chernikov struct nhop4_extended nh_ext; 73999d628d5SPedro F. Giffuni struct mbuf *opts = NULL; 74053be11f6SPoul-Henning Kamp int optlen = (ip->ip_hl << 2) - sizeof(struct ip); 741df8bae1dSRodney W. Grimes 7426b9ff6b7SGeorge V. Neville-Neil if (IN_MULTICAST(ntohl(ip->ip_src.s_addr)) || 7436b9ff6b7SGeorge V. Neville-Neil IN_EXPERIMENTAL(ntohl(ip->ip_src.s_addr)) || 7446b9ff6b7SGeorge V. Neville-Neil IN_ZERONET(ntohl(ip->ip_src.s_addr)) ) { 745df8bae1dSRodney W. Grimes m_freem(m); /* Bad return address */ 746e27b0c87SRobert Watson ICMPSTAT_INC(icps_badaddr); 747df8bae1dSRodney W. Grimes goto done; /* Ip_output() will check for broadcast */ 748df8bae1dSRodney W. Grimes } 7496b9ff6b7SGeorge V. Neville-Neil 750df8bae1dSRodney W. Grimes t = ip->ip_dst; 751df8bae1dSRodney W. Grimes ip->ip_dst = ip->ip_src; 7521488eac8SAndre Oppermann 753df8bae1dSRodney W. Grimes /* 7541488eac8SAndre Oppermann * Source selection for ICMP replies: 7551488eac8SAndre Oppermann * 7561488eac8SAndre Oppermann * If the incoming packet was addressed directly to one of our 7571488eac8SAndre Oppermann * own addresses, use dst as the src for the reply. 758df8bae1dSRodney W. Grimes */ 759cc0a3c8cSAndrey V. Elsukov IN_IFADDR_RLOCK(&in_ifa_tracker); 76033c4f96dSRobert Watson LIST_FOREACH(ia, INADDR_HASH(t.s_addr), ia_hash) { 76133c4f96dSRobert Watson if (t.s_addr == IA_SIN(ia)->sin_addr.s_addr) { 76233c4f96dSRobert Watson t = IA_SIN(ia)->sin_addr; 763cc0a3c8cSAndrey V. Elsukov IN_IFADDR_RUNLOCK(&in_ifa_tracker); 764ca925d9cSJonathan Lemon goto match; 76533c4f96dSRobert Watson } 76633c4f96dSRobert Watson } 767cc0a3c8cSAndrey V. Elsukov IN_IFADDR_RUNLOCK(&in_ifa_tracker); 7682d9cfabaSRobert Watson 7691488eac8SAndre Oppermann /* 7701488eac8SAndre Oppermann * If the incoming packet was addressed to one of our broadcast 7711488eac8SAndre Oppermann * addresses, use the first non-broadcast address which corresponds 7721488eac8SAndre Oppermann * to the incoming interface. 7731488eac8SAndre Oppermann */ 77433c4f96dSRobert Watson ifp = m->m_pkthdr.rcvif; 77533c4f96dSRobert Watson if (ifp != NULL && ifp->if_flags & IFF_BROADCAST) { 776137f91e8SJohn Baldwin IF_ADDR_RLOCK(ifp); 777d7c5a620SMatt Macy CK_STAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) { 778ca925d9cSJonathan Lemon if (ifa->ifa_addr->sa_family != AF_INET) 779ca925d9cSJonathan Lemon continue; 780ca925d9cSJonathan Lemon ia = ifatoia(ifa); 781ca925d9cSJonathan Lemon if (satosin(&ia->ia_broadaddr)->sin_addr.s_addr == 78233c4f96dSRobert Watson t.s_addr) { 78333c4f96dSRobert Watson t = IA_SIN(ia)->sin_addr; 784137f91e8SJohn Baldwin IF_ADDR_RUNLOCK(ifp); 785ca925d9cSJonathan Lemon goto match; 786df8bae1dSRodney W. Grimes } 787ca925d9cSJonathan Lemon } 788137f91e8SJohn Baldwin IF_ADDR_RUNLOCK(ifp); 78933c4f96dSRobert Watson } 7901488eac8SAndre Oppermann /* 791a0866c8dSAndre Oppermann * If the packet was transiting through us, use the address of 792a0866c8dSAndre Oppermann * the interface the packet came through in. If that interface 793a0866c8dSAndre Oppermann * doesn't have a suitable IP address, the normal selection 794a0866c8dSAndre Oppermann * criteria apply. 795a0866c8dSAndre Oppermann */ 79633c4f96dSRobert Watson if (V_icmp_rfi && ifp != NULL) { 797137f91e8SJohn Baldwin IF_ADDR_RLOCK(ifp); 798d7c5a620SMatt Macy CK_STAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) { 799a0866c8dSAndre Oppermann if (ifa->ifa_addr->sa_family != AF_INET) 800a0866c8dSAndre Oppermann continue; 801a0866c8dSAndre Oppermann ia = ifatoia(ifa); 80233c4f96dSRobert Watson t = IA_SIN(ia)->sin_addr; 803137f91e8SJohn Baldwin IF_ADDR_RUNLOCK(ifp); 804a0866c8dSAndre Oppermann goto match; 805a0866c8dSAndre Oppermann } 806137f91e8SJohn Baldwin IF_ADDR_RUNLOCK(ifp); 807a0866c8dSAndre Oppermann } 808a0866c8dSAndre Oppermann /* 809b74d89bbSAndre Oppermann * If the incoming packet was not addressed directly to us, use 810b74d89bbSAndre Oppermann * designated interface for icmp replies specified by sysctl 811b74d89bbSAndre Oppermann * net.inet.icmp.reply_src (default not set). Otherwise continue 812b74d89bbSAndre Oppermann * with normal source selection. 813b74d89bbSAndre Oppermann */ 81433c4f96dSRobert Watson if (V_reply_src[0] != '\0' && (ifp = ifunit(V_reply_src))) { 815137f91e8SJohn Baldwin IF_ADDR_RLOCK(ifp); 816d7c5a620SMatt Macy CK_STAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) { 817b74d89bbSAndre Oppermann if (ifa->ifa_addr->sa_family != AF_INET) 818b74d89bbSAndre Oppermann continue; 819b74d89bbSAndre Oppermann ia = ifatoia(ifa); 82033c4f96dSRobert Watson t = IA_SIN(ia)->sin_addr; 821137f91e8SJohn Baldwin IF_ADDR_RUNLOCK(ifp); 822b74d89bbSAndre Oppermann goto match; 823b74d89bbSAndre Oppermann } 824137f91e8SJohn Baldwin IF_ADDR_RUNLOCK(ifp); 825b74d89bbSAndre Oppermann } 826b74d89bbSAndre Oppermann /* 8271488eac8SAndre Oppermann * If the packet was transiting through us, use the address of 8281488eac8SAndre Oppermann * the interface that is the closest to the packet source. 8291488eac8SAndre Oppermann * When we don't have a route back to the packet source, stop here 8301488eac8SAndre Oppermann * and drop the packet. 8311488eac8SAndre Oppermann */ 8329977be4aSAlexander V. Chernikov if (fib4_lookup_nh_ext(M_GETFIB(m), ip->ip_dst, 0, 0, &nh_ext) != 0) { 8330d4bef5dSDima Dorfman m_freem(m); 834e27b0c87SRobert Watson ICMPSTAT_INC(icps_noroute); 8350d4bef5dSDima Dorfman goto done; 8360d4bef5dSDima Dorfman } 8379977be4aSAlexander V. Chernikov t = nh_ext.nh_src; 838ca925d9cSJonathan Lemon match: 839baee0c3eSRobert Watson #ifdef MAC 840a13e21f7SRobert Watson mac_netinet_icmp_replyinplace(m); 841baee0c3eSRobert Watson #endif 842df8bae1dSRodney W. Grimes ip->ip_src = t; 843603724d3SBjoern A. Zeeb ip->ip_ttl = V_ip_defttl; 844df8bae1dSRodney W. Grimes 845df8bae1dSRodney W. Grimes if (optlen > 0) { 8463e85b721SEd Maste u_char *cp; 847df8bae1dSRodney W. Grimes int opt, cnt; 848df8bae1dSRodney W. Grimes u_int len; 849df8bae1dSRodney W. Grimes 850df8bae1dSRodney W. Grimes /* 851df8bae1dSRodney W. Grimes * Retrieve any source routing from the incoming packet; 852df8bae1dSRodney W. Grimes * add on any record-route or timestamp options. 853df8bae1dSRodney W. Grimes */ 854df8bae1dSRodney W. Grimes cp = (u_char *) (ip + 1); 85599d628d5SPedro F. Giffuni if ((opts = ip_srcroute(m)) == NULL && 856eb1b1807SGleb Smirnoff (opts = m_gethdr(M_NOWAIT, MT_DATA))) { 857df8bae1dSRodney W. Grimes opts->m_len = sizeof(struct in_addr); 858df8bae1dSRodney W. Grimes mtod(opts, struct in_addr *)->s_addr = 0; 859df8bae1dSRodney W. Grimes } 860df8bae1dSRodney W. Grimes if (opts) { 861df8bae1dSRodney W. Grimes #ifdef ICMPPRINTFS 862df8bae1dSRodney W. Grimes if (icmpprintfs) 863df8bae1dSRodney W. Grimes printf("icmp_reflect optlen %d rt %d => ", 864df8bae1dSRodney W. Grimes optlen, opts->m_len); 865df8bae1dSRodney W. Grimes #endif 866df8bae1dSRodney W. Grimes for (cnt = optlen; cnt > 0; cnt -= len, cp += len) { 867df8bae1dSRodney W. Grimes opt = cp[IPOPT_OPTVAL]; 868df8bae1dSRodney W. Grimes if (opt == IPOPT_EOL) 869df8bae1dSRodney W. Grimes break; 870df8bae1dSRodney W. Grimes if (opt == IPOPT_NOP) 871df8bae1dSRodney W. Grimes len = 1; 872df8bae1dSRodney W. Grimes else { 873707d00a3SJonathan Lemon if (cnt < IPOPT_OLEN + sizeof(*cp)) 874707d00a3SJonathan Lemon break; 875df8bae1dSRodney W. Grimes len = cp[IPOPT_OLEN]; 876707d00a3SJonathan Lemon if (len < IPOPT_OLEN + sizeof(*cp) || 877707d00a3SJonathan Lemon len > cnt) 878df8bae1dSRodney W. Grimes break; 879df8bae1dSRodney W. Grimes } 880df8bae1dSRodney W. Grimes /* 881df8bae1dSRodney W. Grimes * Should check for overflow, but it "can't happen" 882df8bae1dSRodney W. Grimes */ 883df8bae1dSRodney W. Grimes if (opt == IPOPT_RR || opt == IPOPT_TS || 884df8bae1dSRodney W. Grimes opt == IPOPT_SECURITY) { 885df8bae1dSRodney W. Grimes bcopy((caddr_t)cp, 886df8bae1dSRodney W. Grimes mtod(opts, caddr_t) + opts->m_len, len); 887df8bae1dSRodney W. Grimes opts->m_len += len; 888df8bae1dSRodney W. Grimes } 889df8bae1dSRodney W. Grimes } 890df8bae1dSRodney W. Grimes /* Terminate & pad, if necessary */ 891623ae52eSPoul-Henning Kamp cnt = opts->m_len % 4; 892623ae52eSPoul-Henning Kamp if (cnt) { 893df8bae1dSRodney W. Grimes for (; cnt < 4; cnt++) { 894df8bae1dSRodney W. Grimes *(mtod(opts, caddr_t) + opts->m_len) = 895df8bae1dSRodney W. Grimes IPOPT_EOL; 896df8bae1dSRodney W. Grimes opts->m_len++; 897df8bae1dSRodney W. Grimes } 898df8bae1dSRodney W. Grimes } 899df8bae1dSRodney W. Grimes #ifdef ICMPPRINTFS 900df8bae1dSRodney W. Grimes if (icmpprintfs) 901df8bae1dSRodney W. Grimes printf("%d\n", opts->m_len); 902df8bae1dSRodney W. Grimes #endif 903df8bae1dSRodney W. Grimes } 9049e2a372fSGleb Smirnoff ip_stripoptions(m); 905df8bae1dSRodney W. Grimes } 9069c855a36SSam Leffler m_tag_delete_nonpersistent(m); 907df8bae1dSRodney W. Grimes m->m_flags &= ~(M_BCAST|M_MCAST); 90802c1c707SAndre Oppermann icmp_send(m, opts); 909df8bae1dSRodney W. Grimes done: 910df8bae1dSRodney W. Grimes if (opts) 911df8bae1dSRodney W. Grimes (void)m_free(opts); 912df8bae1dSRodney W. Grimes } 913df8bae1dSRodney W. Grimes 914df8bae1dSRodney W. Grimes /* 915df8bae1dSRodney W. Grimes * Send an icmp packet back to the ip level, 916df8bae1dSRodney W. Grimes * after supplying a checksum. 917df8bae1dSRodney W. Grimes */ 9180312fbe9SPoul-Henning Kamp static void 919f2565d68SRobert Watson icmp_send(struct mbuf *m, struct mbuf *opts) 920df8bae1dSRodney W. Grimes { 9213e85b721SEd Maste struct ip *ip = mtod(m, struct ip *); 9223e85b721SEd Maste int hlen; 9233e85b721SEd Maste struct icmp *icp; 924df8bae1dSRodney W. Grimes 92553be11f6SPoul-Henning Kamp hlen = ip->ip_hl << 2; 926df8bae1dSRodney W. Grimes m->m_data += hlen; 927df8bae1dSRodney W. Grimes m->m_len -= hlen; 928df8bae1dSRodney W. Grimes icp = mtod(m, struct icmp *); 929df8bae1dSRodney W. Grimes icp->icmp_cksum = 0; 9308f134647SGleb Smirnoff icp->icmp_cksum = in_cksum(m, ntohs(ip->ip_len) - hlen); 931df8bae1dSRodney W. Grimes m->m_data -= hlen; 932df8bae1dSRodney W. Grimes m->m_len += hlen; 93394446a2eSArchie Cobbs m->m_pkthdr.rcvif = (struct ifnet *)0; 934df8bae1dSRodney W. Grimes #ifdef ICMPPRINTFS 9352b758395SGarrett Wollman if (icmpprintfs) { 9368144690aSEric van Gyzen char dstbuf[INET_ADDRSTRLEN]; 9378144690aSEric van Gyzen char srcbuf[INET_ADDRSTRLEN]; 9388144690aSEric van Gyzen 9392b758395SGarrett Wollman printf("icmp_send dst %s src %s\n", 9408144690aSEric van Gyzen inet_ntoa_r(ip->ip_dst, dstbuf), 9418144690aSEric van Gyzen inet_ntoa_r(ip->ip_src, srcbuf)); 9422b758395SGarrett Wollman } 943df8bae1dSRodney W. Grimes #endif 94402c1c707SAndre Oppermann (void) ip_output(m, opts, NULL, 0, NULL, NULL); 945df8bae1dSRodney W. Grimes } 946df8bae1dSRodney W. Grimes 947d685b6eeSLuigi Rizzo /* 94861f26caeSWarner Losh * Return milliseconds since 00:00 UTC in network format. 949d685b6eeSLuigi Rizzo */ 950d685b6eeSLuigi Rizzo uint32_t 951f2565d68SRobert Watson iptime(void) 952df8bae1dSRodney W. Grimes { 953df8bae1dSRodney W. Grimes struct timeval atv; 954df8bae1dSRodney W. Grimes u_long t; 955df8bae1dSRodney W. Grimes 95616cd6db0SBill Fumerola getmicrotime(&atv); 957df8bae1dSRodney W. Grimes t = (atv.tv_sec % (24*60*60)) * 1000 + atv.tv_usec / 1000; 958df8bae1dSRodney W. Grimes return (htonl(t)); 959df8bae1dSRodney W. Grimes } 960df8bae1dSRodney W. Grimes 9615cbf3e08SGarrett Wollman /* 9625cbf3e08SGarrett Wollman * Return the next larger or smaller MTU plateau (table from RFC 1191) 9635cbf3e08SGarrett Wollman * given current value MTU. If DIR is less than zero, a larger plateau 9645cbf3e08SGarrett Wollman * is returned; otherwise, a smaller value is returned. 9655cbf3e08SGarrett Wollman */ 9661aedbd9cSAndre Oppermann int 967f2565d68SRobert Watson ip_next_mtu(int mtu, int dir) 9685cbf3e08SGarrett Wollman { 9695cbf3e08SGarrett Wollman static int mtutab[] = { 9704c037f8dSAndre Oppermann 65535, 32000, 17914, 8166, 4352, 2002, 1492, 1280, 1006, 508, 9714c037f8dSAndre Oppermann 296, 68, 0 9725cbf3e08SGarrett Wollman }; 97306003a1eSAndre Oppermann int i, size; 9745cbf3e08SGarrett Wollman 97506003a1eSAndre Oppermann size = (sizeof mtutab) / (sizeof mtutab[0]); 97606003a1eSAndre Oppermann if (dir >= 0) { 9771c0b0f52SGleb Smirnoff for (i = 0; i < size; i++) 97806003a1eSAndre Oppermann if (mtu > mtutab[i]) 9795cbf3e08SGarrett Wollman return mtutab[i]; 9805cbf3e08SGarrett Wollman } else { 98106003a1eSAndre Oppermann for (i = size - 1; i >= 0; i--) 98206003a1eSAndre Oppermann if (mtu < mtutab[i]) 98306003a1eSAndre Oppermann return mtutab[i]; 98406003a1eSAndre Oppermann if (mtu == mtutab[0]) 98506003a1eSAndre Oppermann return mtutab[0]; 9865cbf3e08SGarrett Wollman } 98706003a1eSAndre Oppermann return 0; 9885cbf3e08SGarrett Wollman } 989b8e463e6SBjoern A. Zeeb #endif /* INET */ 99051508de1SMatthew Dillon 99151508de1SMatthew Dillon 99251508de1SMatthew Dillon /* 99351508de1SMatthew Dillon * badport_bandlim() - check for ICMP bandwidth limit 99451508de1SMatthew Dillon * 99551508de1SMatthew Dillon * Return 0 if it is ok to send an ICMP error response, -1 if we have 99651508de1SMatthew Dillon * hit our bandwidth limit and it is not ok. 99751508de1SMatthew Dillon * 99851508de1SMatthew Dillon * If icmplim is <= 0, the feature is disabled and 0 is returned. 99951508de1SMatthew Dillon * 100051508de1SMatthew Dillon * For now we separate the TCP and UDP subsystems w/ different 'which' 100151508de1SMatthew Dillon * values. We may eventually remove this separation (and simplify the 100251508de1SMatthew Dillon * code further). 100351508de1SMatthew Dillon * 100451508de1SMatthew Dillon * Note that the printing of the error message is delayed so we can 100551508de1SMatthew Dillon * properly print the icmp error rate that the system was trying to do 100651508de1SMatthew Dillon * (i.e. 22000/100 pps, etc...). This can cause long delays in printing 100751508de1SMatthew Dillon * the 'final' error, but it doesn't make sense to solve the printing 100851508de1SMatthew Dillon * delay with more complex code. 100951508de1SMatthew Dillon */ 10103cbee8caSGleb Smirnoff struct icmp_rate { 10113cbee8caSGleb Smirnoff const char *descr; 10123cbee8caSGleb Smirnoff struct counter_rate cr; 10133cbee8caSGleb Smirnoff }; 10145f901c92SAndrew Turner VNET_DEFINE_STATIC(struct icmp_rate, icmp_rates[BANDLIM_MAX]) = { 101500f21882SSam Leffler { "icmp unreach response" }, 101600f21882SSam Leffler { "icmp ping response" }, 101700f21882SSam Leffler { "icmp tstamp response" }, 101800f21882SSam Leffler { "closed port RST response" }, 101908af97b7SRobert Watson { "open port RST response" }, 10205ad9e57bSMichael Tuexen { "icmp6 unreach response" }, 10215ad9e57bSMichael Tuexen { "sctp ootb response" } 102209f81a46SBosko Milekic }; 10233cbee8caSGleb Smirnoff #define V_icmp_rates VNET(icmp_rates) 102451508de1SMatthew Dillon 10253cbee8caSGleb Smirnoff static void 10263cbee8caSGleb Smirnoff icmp_bandlimit_init(void) 10273cbee8caSGleb Smirnoff { 102851508de1SMatthew Dillon 10293cbee8caSGleb Smirnoff for (int i = 0; i < BANDLIM_MAX; i++) { 10303cbee8caSGleb Smirnoff V_icmp_rates[i].cr.cr_rate = counter_u64_alloc(M_WAITOK); 10313cbee8caSGleb Smirnoff V_icmp_rates[i].cr.cr_ticks = ticks; 103251508de1SMatthew Dillon } 10333cbee8caSGleb Smirnoff } 10343cbee8caSGleb Smirnoff VNET_SYSINIT(icmp_bandlimit, SI_SUB_PROTO_DOMAIN, SI_ORDER_ANY, 10353cbee8caSGleb Smirnoff icmp_bandlimit_init, NULL); 10363cbee8caSGleb Smirnoff 10373cbee8caSGleb Smirnoff static void 10383cbee8caSGleb Smirnoff icmp_bandlimit_uninit(void) 10393cbee8caSGleb Smirnoff { 10403cbee8caSGleb Smirnoff 10413cbee8caSGleb Smirnoff for (int i = 0; i < BANDLIM_MAX; i++) 10423cbee8caSGleb Smirnoff counter_u64_free(V_icmp_rates[i].cr.cr_rate); 10433cbee8caSGleb Smirnoff } 10443cbee8caSGleb Smirnoff VNET_SYSUNINIT(icmp_bandlimit, SI_SUB_PROTO_DOMAIN, SI_ORDER_THIRD, 10453cbee8caSGleb Smirnoff icmp_bandlimit_uninit, NULL); 10463cbee8caSGleb Smirnoff 10473cbee8caSGleb Smirnoff int 10483cbee8caSGleb Smirnoff badport_bandlim(int which) 10493cbee8caSGleb Smirnoff { 10503cbee8caSGleb Smirnoff int64_t pps; 10513cbee8caSGleb Smirnoff 10523cbee8caSGleb Smirnoff if (V_icmplim == 0 || which == BANDLIM_UNLIMITED) 10533cbee8caSGleb Smirnoff return (0); 10543cbee8caSGleb Smirnoff 10553cbee8caSGleb Smirnoff KASSERT(which >= 0 && which < BANDLIM_MAX, 10563cbee8caSGleb Smirnoff ("%s: which %d", __func__, which)); 10573cbee8caSGleb Smirnoff 10583cbee8caSGleb Smirnoff pps = counter_ratecheck(&V_icmp_rates[which].cr, V_icmplim); 10593cbee8caSGleb Smirnoff if (pps == -1) 10603cbee8caSGleb Smirnoff return (-1); 10613cbee8caSGleb Smirnoff if (pps > 0 && V_icmplim_output) 10628c70a353SGleb Smirnoff log(LOG_NOTICE, "Limiting %s from %jd to %d packets/sec\n", 10638c70a353SGleb Smirnoff V_icmp_rates[which].descr, (intmax_t )pps, V_icmplim); 10643cbee8caSGleb Smirnoff return (0); 106551508de1SMatthew Dillon } 1066