xref: /freebsd/sys/netinet/ip_icmp.c (revision cc0a3c8ca4561297b4df88fec347ba004d04277c)
1c398230bSWarner Losh /*-
2df8bae1dSRodney W. Grimes  * Copyright (c) 1982, 1986, 1988, 1993
3df8bae1dSRodney W. Grimes  *	The Regents of the University of California.  All rights reserved.
4df8bae1dSRodney W. Grimes  *
5df8bae1dSRodney W. Grimes  * Redistribution and use in source and binary forms, with or without
6df8bae1dSRodney W. Grimes  * modification, are permitted provided that the following conditions
7df8bae1dSRodney W. Grimes  * are met:
8df8bae1dSRodney W. Grimes  * 1. Redistributions of source code must retain the above copyright
9df8bae1dSRodney W. Grimes  *    notice, this list of conditions and the following disclaimer.
10df8bae1dSRodney W. Grimes  * 2. Redistributions in binary form must reproduce the above copyright
11df8bae1dSRodney W. Grimes  *    notice, this list of conditions and the following disclaimer in the
12df8bae1dSRodney W. Grimes  *    documentation and/or other materials provided with the distribution.
13df8bae1dSRodney W. Grimes  * 4. Neither the name of the University nor the names of its contributors
14df8bae1dSRodney W. Grimes  *    may be used to endorse or promote products derived from this software
15df8bae1dSRodney W. Grimes  *    without specific prior written permission.
16df8bae1dSRodney W. Grimes  *
17df8bae1dSRodney W. Grimes  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
18df8bae1dSRodney W. Grimes  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19df8bae1dSRodney W. Grimes  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20df8bae1dSRodney W. Grimes  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
21df8bae1dSRodney W. Grimes  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22df8bae1dSRodney W. Grimes  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23df8bae1dSRodney W. Grimes  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24df8bae1dSRodney W. Grimes  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25df8bae1dSRodney W. Grimes  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26df8bae1dSRodney W. Grimes  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27df8bae1dSRodney W. Grimes  * SUCH DAMAGE.
28df8bae1dSRodney W. Grimes  *
29df8bae1dSRodney W. Grimes  *	@(#)ip_icmp.c	8.2 (Berkeley) 1/4/94
30df8bae1dSRodney W. Grimes  */
31df8bae1dSRodney W. Grimes 
324b421e2dSMike Silbersack #include <sys/cdefs.h>
334b421e2dSMike Silbersack __FBSDID("$FreeBSD$");
344b421e2dSMike Silbersack 
35b8e463e6SBjoern A. Zeeb #include "opt_inet.h"
366a800098SYoshinobu Inoue 
37df8bae1dSRodney W. Grimes #include <sys/param.h>
38df8bae1dSRodney W. Grimes #include <sys/systm.h>
39df8bae1dSRodney W. Grimes #include <sys/mbuf.h>
40df8bae1dSRodney W. Grimes #include <sys/protosw.h>
41df8bae1dSRodney W. Grimes #include <sys/socket.h>
42df8bae1dSRodney W. Grimes #include <sys/time.h>
43df8bae1dSRodney W. Grimes #include <sys/kernel.h>
44*cc0a3c8cSAndrey V. Elsukov #include <sys/lock.h>
45*cc0a3c8cSAndrey V. Elsukov #include <sys/rmlock.h>
46b5e8ce9fSBruce Evans #include <sys/sysctl.h>
4740fe9effSAndre Oppermann #include <sys/syslog.h>
48df8bae1dSRodney W. Grimes 
49df8bae1dSRodney W. Grimes #include <net/if.h>
5076039bc8SGleb Smirnoff #include <net/if_var.h>
519494d596SBrooks Davis #include <net/if_types.h>
52df8bae1dSRodney W. Grimes #include <net/route.h>
53eddfbb76SRobert Watson #include <net/vnet.h>
54df8bae1dSRodney W. Grimes 
55df8bae1dSRodney W. Grimes #include <netinet/in.h>
5697d8d152SAndre Oppermann #include <netinet/in_pcb.h>
57df8bae1dSRodney W. Grimes #include <netinet/in_systm.h>
58df8bae1dSRodney W. Grimes #include <netinet/in_var.h>
59df8bae1dSRodney W. Grimes #include <netinet/ip.h>
60df8bae1dSRodney W. Grimes #include <netinet/ip_icmp.h>
61b5e8ce9fSBruce Evans #include <netinet/ip_var.h>
62ef39adf0SAndre Oppermann #include <netinet/ip_options.h>
6397d8d152SAndre Oppermann #include <netinet/tcp.h>
6497d8d152SAndre Oppermann #include <netinet/tcp_var.h>
6597d8d152SAndre Oppermann #include <netinet/tcpip.h>
66df8bae1dSRodney W. Grimes #include <netinet/icmp_var.h>
67df8bae1dSRodney W. Grimes 
68b8e463e6SBjoern A. Zeeb #ifdef INET
69b9234fafSSam Leffler 
7072a52a35SJonathan Lemon #include <machine/in_cksum.h>
7172a52a35SJonathan Lemon 
72aed55708SRobert Watson #include <security/mac/mac_framework.h>
73b8e463e6SBjoern A. Zeeb #endif /* INET */
74aed55708SRobert Watson 
75df8bae1dSRodney W. Grimes /*
76df8bae1dSRodney W. Grimes  * ICMP routines: error generation, receive packet processing, and
77df8bae1dSRodney W. Grimes  * routines to turnaround packets back to the originator, and
78df8bae1dSRodney W. Grimes  * host table maintenance routines.
79df8bae1dSRodney W. Grimes  */
80b8e463e6SBjoern A. Zeeb static VNET_DEFINE(int, icmplim) = 200;
81b8e463e6SBjoern A. Zeeb #define	V_icmplim			VNET(icmplim)
826df8a710SGleb Smirnoff SYSCTL_INT(_net_inet_icmp, ICMPCTL_ICMPLIM, icmplim, CTLFLAG_VNET | CTLFLAG_RW,
83b8e463e6SBjoern A. Zeeb 	&VNET_NAME(icmplim), 0,
84b8e463e6SBjoern A. Zeeb 	"Maximum number of ICMP responses per second");
85b8e463e6SBjoern A. Zeeb 
86b8e463e6SBjoern A. Zeeb static VNET_DEFINE(int, icmplim_output) = 1;
87b8e463e6SBjoern A. Zeeb #define	V_icmplim_output		VNET(icmplim_output)
886df8a710SGleb Smirnoff SYSCTL_INT(_net_inet_icmp, OID_AUTO, icmplim_output, CTLFLAG_VNET | CTLFLAG_RW,
89b8e463e6SBjoern A. Zeeb 	&VNET_NAME(icmplim_output), 0,
90316efdb3SEitan Adler 	"Enable logging of ICMP response rate limiting");
91b8e463e6SBjoern A. Zeeb 
92b8e463e6SBjoern A. Zeeb #ifdef INET
935b7cb97cSAndrey V. Elsukov VNET_PCPUSTAT_DEFINE(struct icmpstat, icmpstat);
945b7cb97cSAndrey V. Elsukov VNET_PCPUSTAT_SYSINIT(icmpstat);
955b7cb97cSAndrey V. Elsukov SYSCTL_VNET_PCPUSTAT(_net_inet_icmp, ICMPCTL_STATS, stats, struct icmpstat,
965b7cb97cSAndrey V. Elsukov     icmpstat, "ICMP statistics (struct icmpstat, netinet/icmp_var.h)");
975b7cb97cSAndrey V. Elsukov 
985b7cb97cSAndrey V. Elsukov #ifdef VIMAGE
995b7cb97cSAndrey V. Elsukov VNET_PCPUSTAT_SYSUNINIT(icmpstat);
1005b7cb97cSAndrey V. Elsukov #endif /* VIMAGE */
101eddfbb76SRobert Watson 
1023e288e62SDimitry Andric static VNET_DEFINE(int, icmpmaskrepl) = 0;
10382cea7e6SBjoern A. Zeeb #define	V_icmpmaskrepl			VNET(icmpmaskrepl)
1046df8a710SGleb Smirnoff SYSCTL_INT(_net_inet_icmp, ICMPCTL_MASKREPL, maskrepl, CTLFLAG_VNET | CTLFLAG_RW,
105eddfbb76SRobert Watson 	&VNET_NAME(icmpmaskrepl), 0,
1068b615593SMarko Zec 	"Reply to ICMP Address Mask Request packets.");
10757842a38SMatthew N. Dodd 
1083e288e62SDimitry Andric static VNET_DEFINE(u_int, icmpmaskfake) = 0;
10982cea7e6SBjoern A. Zeeb #define	V_icmpmaskfake			VNET(icmpmaskfake)
1106df8a710SGleb Smirnoff SYSCTL_UINT(_net_inet_icmp, OID_AUTO, maskfake, CTLFLAG_VNET | CTLFLAG_RW,
111eddfbb76SRobert Watson 	&VNET_NAME(icmpmaskfake), 0,
112eddfbb76SRobert Watson 	"Fake reply to ICMP Address Mask Request packets.");
1130312fbe9SPoul-Henning Kamp 
1143c2824b9SAlexander V. Chernikov VNET_DEFINE(int, drop_redirect) = 0;
115670e8b3bSAlexander V. Chernikov #define	V_drop_redirect			VNET(drop_redirect)
116670e8b3bSAlexander V. Chernikov SYSCTL_INT(_net_inet_icmp, OID_AUTO, drop_redirect, CTLFLAG_VNET | CTLFLAG_RW,
117670e8b3bSAlexander V. Chernikov 	&VNET_NAME(drop_redirect), 0, "Ignore ICMP redirects");
11818d3153eSDag-Erling Smørgrav 
1193e288e62SDimitry Andric static VNET_DEFINE(int, log_redirect) = 0;
12082cea7e6SBjoern A. Zeeb #define	V_log_redirect			VNET(log_redirect)
1216df8a710SGleb Smirnoff SYSCTL_INT(_net_inet_icmp, OID_AUTO, log_redirect, CTLFLAG_VNET | CTLFLAG_RW,
122eddfbb76SRobert Watson 	&VNET_NAME(log_redirect), 0,
123eddfbb76SRobert Watson 	"Log ICMP redirects to the console");
1246c3b5f69SDag-Erling Smørgrav 
1253e288e62SDimitry Andric static VNET_DEFINE(char, reply_src[IFNAMSIZ]);
12682cea7e6SBjoern A. Zeeb #define	V_reply_src			VNET(reply_src)
1276df8a710SGleb Smirnoff SYSCTL_STRING(_net_inet_icmp, OID_AUTO, reply_src, CTLFLAG_VNET | CTLFLAG_RW,
128eddfbb76SRobert Watson 	&VNET_NAME(reply_src), IFNAMSIZ,
1298b615593SMarko Zec 	"icmp reply source for non-local packets.");
130b74d89bbSAndre Oppermann 
1313e288e62SDimitry Andric static VNET_DEFINE(int, icmp_rfi) = 0;
13282cea7e6SBjoern A. Zeeb #define	V_icmp_rfi			VNET(icmp_rfi)
1336df8a710SGleb Smirnoff SYSCTL_INT(_net_inet_icmp, OID_AUTO, reply_from_interface, CTLFLAG_VNET | CTLFLAG_RW,
134eddfbb76SRobert Watson 	&VNET_NAME(icmp_rfi), 0,
135eddfbb76SRobert Watson 	"ICMP reply from incoming interface for non-local packets");
136a0866c8dSAndre Oppermann 
1373e288e62SDimitry Andric static VNET_DEFINE(int, icmp_quotelen) = 8;
13882cea7e6SBjoern A. Zeeb #define	V_icmp_quotelen			VNET(icmp_quotelen)
1396df8a710SGleb Smirnoff SYSCTL_INT(_net_inet_icmp, OID_AUTO, quotelen, CTLFLAG_VNET | CTLFLAG_RW,
140eddfbb76SRobert Watson 	&VNET_NAME(icmp_quotelen), 0,
141eddfbb76SRobert Watson 	"Number of bytes from original packet to quote in ICMP reply");
142e875dfb8SAndre Oppermann 
1435fce7fc4SMatthew Dillon /*
1445fce7fc4SMatthew Dillon  * ICMP broadcast echo sysctl
1455fce7fc4SMatthew Dillon  */
1463e288e62SDimitry Andric static VNET_DEFINE(int, icmpbmcastecho) = 0;
14782cea7e6SBjoern A. Zeeb #define	V_icmpbmcastecho		VNET(icmpbmcastecho)
1486df8a710SGleb Smirnoff SYSCTL_INT(_net_inet_icmp, OID_AUTO, bmcastecho, CTLFLAG_VNET | CTLFLAG_RW,
149eddfbb76SRobert Watson 	&VNET_NAME(icmpbmcastecho), 0,
150eddfbb76SRobert Watson 	"");
1517022ea0aSGarrett Wollman 
15200cb6befSMark Johnston static VNET_DEFINE(int, icmptstamprepl) = 1;
15300cb6befSMark Johnston #define	V_icmptstamprepl		VNET(icmptstamprepl)
15400cb6befSMark Johnston SYSCTL_INT(_net_inet_icmp, OID_AUTO, tstamprepl, CTLFLAG_RW,
15500cb6befSMark Johnston 	&VNET_NAME(icmptstamprepl), 0, "Respond to ICMP Timestamp packets");
15651508de1SMatthew Dillon 
157df8bae1dSRodney W. Grimes #ifdef ICMPPRINTFS
158df8bae1dSRodney W. Grimes int	icmpprintfs = 0;
159df8bae1dSRodney W. Grimes #endif
160df8bae1dSRodney W. Grimes 
1614d77a549SAlfred Perlstein static void	icmp_reflect(struct mbuf *);
16202c1c707SAndre Oppermann static void	icmp_send(struct mbuf *, struct mbuf *);
1630312fbe9SPoul-Henning Kamp 
164df8bae1dSRodney W. Grimes extern	struct protosw inetsw[];
165df8bae1dSRodney W. Grimes 
166df8bae1dSRodney W. Grimes /*
167315e3e38SRobert Watson  * Kernel module interface for updating icmpstat.  The argument is an index
168315e3e38SRobert Watson  * into icmpstat treated as an array of u_long.  While this encodes the
169315e3e38SRobert Watson  * general layout of icmpstat into the caller, it doesn't encode its
170315e3e38SRobert Watson  * location, so that future changes to add, for example, per-CPU stats
171315e3e38SRobert Watson  * support won't cause binary compatibility problems for kernel modules.
172315e3e38SRobert Watson  */
173315e3e38SRobert Watson void
174315e3e38SRobert Watson kmod_icmpstat_inc(int statnum)
175315e3e38SRobert Watson {
176315e3e38SRobert Watson 
1775b7cb97cSAndrey V. Elsukov 	counter_u64_add(VNET(icmpstat)[statnum], 1);
178315e3e38SRobert Watson }
179315e3e38SRobert Watson 
180315e3e38SRobert Watson /*
181df8bae1dSRodney W. Grimes  * Generate an error packet of type error
182df8bae1dSRodney W. Grimes  * in response to bad packet ip.
183df8bae1dSRodney W. Grimes  */
184df8bae1dSRodney W. Grimes void
185d685b6eeSLuigi Rizzo icmp_error(struct mbuf *n, int type, int code, uint32_t dest, int mtu)
186df8bae1dSRodney W. Grimes {
187df8bae1dSRodney W. Grimes 	register struct ip *oip = mtod(n, struct ip *), *nip;
188e86ebebcSAndre Oppermann 	register unsigned oiphlen = oip->ip_hl << 2;
189df8bae1dSRodney W. Grimes 	register struct icmp *icp;
190df8bae1dSRodney W. Grimes 	register struct mbuf *m;
191e86ebebcSAndre Oppermann 	unsigned icmplen, icmpelen, nlen;
192df8bae1dSRodney W. Grimes 
193e86ebebcSAndre Oppermann 	KASSERT((u_int)type <= ICMP_MAXTYPE, ("%s: illegal ICMP type", __func__));
194df8bae1dSRodney W. Grimes #ifdef ICMPPRINTFS
195df8bae1dSRodney W. Grimes 	if (icmpprintfs)
196623ae52eSPoul-Henning Kamp 		printf("icmp_error(%p, %x, %d)\n", oip, type, code);
197df8bae1dSRodney W. Grimes #endif
198df8bae1dSRodney W. Grimes 	if (type != ICMP_REDIRECT)
199e27b0c87SRobert Watson 		ICMPSTAT_INC(icps_error);
200df8bae1dSRodney W. Grimes 	/*
201e86ebebcSAndre Oppermann 	 * Don't send error:
202e86ebebcSAndre Oppermann 	 *  if the original packet was encrypted.
203e86ebebcSAndre Oppermann 	 *  if not the first fragment of message.
204e86ebebcSAndre Oppermann 	 *  in response to a multicast or broadcast packet.
205e86ebebcSAndre Oppermann 	 *  if the old packet protocol was an ICMP error message.
206df8bae1dSRodney W. Grimes 	 */
207cad1917dSHajimu UMEMOTO 	if (n->m_flags & M_DECRYPTED)
208cad1917dSHajimu UMEMOTO 		goto freeit;
2098f134647SGleb Smirnoff 	if (oip->ip_off & htons(~(IP_MF|IP_DF)))
210df8bae1dSRodney W. Grimes 		goto freeit;
211e86ebebcSAndre Oppermann 	if (n->m_flags & (M_BCAST|M_MCAST))
212e86ebebcSAndre Oppermann 		goto freeit;
213df8bae1dSRodney W. Grimes 	if (oip->ip_p == IPPROTO_ICMP && type != ICMP_REDIRECT &&
214e86ebebcSAndre Oppermann 	  n->m_len >= oiphlen + ICMP_MINLEN &&
215e86ebebcSAndre Oppermann 	  !ICMP_INFOTYPE(((struct icmp *)((caddr_t)oip + oiphlen))->icmp_type)) {
216e27b0c87SRobert Watson 		ICMPSTAT_INC(icps_oldicmp);
217df8bae1dSRodney W. Grimes 		goto freeit;
218df8bae1dSRodney W. Grimes 	}
219e86ebebcSAndre Oppermann 	/* Drop if IP header plus 8 bytes is not contignous in first mbuf. */
220e86ebebcSAndre Oppermann 	if (oiphlen + 8 > n->m_len)
221df8bae1dSRodney W. Grimes 		goto freeit;
222df8bae1dSRodney W. Grimes 	/*
223e86ebebcSAndre Oppermann 	 * Calculate length to quote from original packet and
224e86ebebcSAndre Oppermann 	 * prevent the ICMP mbuf from overflowing.
225e86ebebcSAndre Oppermann 	 * Unfortunatly this is non-trivial since ip_forward()
226e86ebebcSAndre Oppermann 	 * sends us truncated packets.
227df8bae1dSRodney W. Grimes 	 */
228e86ebebcSAndre Oppermann 	nlen = m_length(n, NULL);
229e86ebebcSAndre Oppermann 	if (oip->ip_p == IPPROTO_TCP) {
230e86ebebcSAndre Oppermann 		struct tcphdr *th;
231e86ebebcSAndre Oppermann 		int tcphlen;
232e86ebebcSAndre Oppermann 
233e86ebebcSAndre Oppermann 		if (oiphlen + sizeof(struct tcphdr) > n->m_len &&
234e86ebebcSAndre Oppermann 		    n->m_next == NULL)
235e86ebebcSAndre Oppermann 			goto stdreply;
236e86ebebcSAndre Oppermann 		if (n->m_len < oiphlen + sizeof(struct tcphdr) &&
237e86ebebcSAndre Oppermann 		    ((n = m_pullup(n, oiphlen + sizeof(struct tcphdr))) == NULL))
238e86ebebcSAndre Oppermann 			goto freeit;
239e86ebebcSAndre Oppermann 		th = (struct tcphdr *)((caddr_t)oip + oiphlen);
240e86ebebcSAndre Oppermann 		tcphlen = th->th_off << 2;
241e86ebebcSAndre Oppermann 		if (tcphlen < sizeof(struct tcphdr))
242e86ebebcSAndre Oppermann 			goto freeit;
2438f134647SGleb Smirnoff 		if (ntohs(oip->ip_len) < oiphlen + tcphlen)
244e86ebebcSAndre Oppermann 			goto freeit;
245e86ebebcSAndre Oppermann 		if (oiphlen + tcphlen > n->m_len && n->m_next == NULL)
246e86ebebcSAndre Oppermann 			goto stdreply;
247e86ebebcSAndre Oppermann 		if (n->m_len < oiphlen + tcphlen &&
248e86ebebcSAndre Oppermann 		    ((n = m_pullup(n, oiphlen + tcphlen)) == NULL))
249e86ebebcSAndre Oppermann 			goto freeit;
2508f134647SGleb Smirnoff 		icmpelen = max(tcphlen, min(V_icmp_quotelen,
2518f134647SGleb Smirnoff 		    ntohs(oip->ip_len) - oiphlen));
252e86ebebcSAndre Oppermann 	} else
2538f134647SGleb Smirnoff stdreply:	icmpelen = max(8, min(V_icmp_quotelen, ntohs(oip->ip_len) - oiphlen));
254e86ebebcSAndre Oppermann 
255e86ebebcSAndre Oppermann 	icmplen = min(oiphlen + icmpelen, nlen);
256e86ebebcSAndre Oppermann 	if (icmplen < sizeof(struct ip))
257e86ebebcSAndre Oppermann 		goto freeit;
258e86ebebcSAndre Oppermann 
259e86ebebcSAndre Oppermann 	if (MHLEN > sizeof(struct ip) + ICMP_MINLEN + icmplen)
260eb1b1807SGleb Smirnoff 		m = m_gethdr(M_NOWAIT, MT_DATA);
261e86ebebcSAndre Oppermann 	else
262eb1b1807SGleb Smirnoff 		m = m_getcl(M_NOWAIT, MT_DATA, M_PKTHDR);
263df8bae1dSRodney W. Grimes 	if (m == NULL)
264df8bae1dSRodney W. Grimes 		goto freeit;
2650070e096SRobert Watson #ifdef MAC
266a13e21f7SRobert Watson 	mac_netinet_icmp_reply(n, m);
2670070e096SRobert Watson #endif
268e86ebebcSAndre Oppermann 	icmplen = min(icmplen, M_TRAILINGSPACE(m) - sizeof(struct ip) - ICMP_MINLEN);
269e86ebebcSAndre Oppermann 	m_align(m, ICMP_MINLEN + icmplen);
270e86ebebcSAndre Oppermann 	m->m_len = ICMP_MINLEN + icmplen;
2716b773dffSAndre Oppermann 
2728b07e49aSJulian Elischer 	/* XXX MRT  make the outgoing packet use the same FIB
2738b07e49aSJulian Elischer 	 * that was associated with the incoming packet
2748b07e49aSJulian Elischer 	 */
2758b07e49aSJulian Elischer 	M_SETFIB(m, M_GETFIB(n));
276df8bae1dSRodney W. Grimes 	icp = mtod(m, struct icmp *);
277e27b0c87SRobert Watson 	ICMPSTAT_INC(icps_outhist[type]);
278df8bae1dSRodney W. Grimes 	icp->icmp_type = type;
279df8bae1dSRodney W. Grimes 	if (type == ICMP_REDIRECT)
280df8bae1dSRodney W. Grimes 		icp->icmp_gwaddr.s_addr = dest;
281df8bae1dSRodney W. Grimes 	else {
282df8bae1dSRodney W. Grimes 		icp->icmp_void = 0;
283df8bae1dSRodney W. Grimes 		/*
284df8bae1dSRodney W. Grimes 		 * The following assignments assume an overlay with the
285e86ebebcSAndre Oppermann 		 * just zeroed icmp_void field.
286df8bae1dSRodney W. Grimes 		 */
287df8bae1dSRodney W. Grimes 		if (type == ICMP_PARAMPROB) {
288df8bae1dSRodney W. Grimes 			icp->icmp_pptr = code;
289df8bae1dSRodney W. Grimes 			code = 0;
290df8bae1dSRodney W. Grimes 		} else if (type == ICMP_UNREACH &&
291c773494eSAndre Oppermann 			code == ICMP_UNREACH_NEEDFRAG && mtu) {
292c773494eSAndre Oppermann 			icp->icmp_nextmtu = htons(mtu);
293df8bae1dSRodney W. Grimes 		}
294df8bae1dSRodney W. Grimes 	}
295df8bae1dSRodney W. Grimes 	icp->icmp_code = code;
29604287599SRuslan Ermilov 
29704287599SRuslan Ermilov 	/*
298e86ebebcSAndre Oppermann 	 * Copy the quotation into ICMP message and
299e86ebebcSAndre Oppermann 	 * convert quoted IP header back to network representation.
30004287599SRuslan Ermilov 	 */
301e86ebebcSAndre Oppermann 	m_copydata(n, 0, icmplen, (caddr_t)&icp->icmp_ip);
302e86ebebcSAndre Oppermann 	nip = &icp->icmp_ip;
303df8bae1dSRodney W. Grimes 
304df8bae1dSRodney W. Grimes 	/*
305e86ebebcSAndre Oppermann 	 * Set up ICMP message mbuf and copy old IP header (without options
306e86ebebcSAndre Oppermann 	 * in front of ICMP message.
307c550f220SMax Laier 	 * If the original mbuf was meant to bypass the firewall, the error
308c550f220SMax Laier 	 * reply should bypass as well.
309c550f220SMax Laier 	 */
310c550f220SMax Laier 	m->m_flags |= n->m_flags & M_SKIP_FIREWALL;
311df8bae1dSRodney W. Grimes 	m->m_data -= sizeof(struct ip);
312df8bae1dSRodney W. Grimes 	m->m_len += sizeof(struct ip);
313df8bae1dSRodney W. Grimes 	m->m_pkthdr.len = m->m_len;
314df8bae1dSRodney W. Grimes 	m->m_pkthdr.rcvif = n->m_pkthdr.rcvif;
315df8bae1dSRodney W. Grimes 	nip = mtod(m, struct ip *);
316df8bae1dSRodney W. Grimes 	bcopy((caddr_t)oip, (caddr_t)nip, sizeof(struct ip));
3178f134647SGleb Smirnoff 	nip->ip_len = htons(m->m_len);
31853be11f6SPoul-Henning Kamp 	nip->ip_v = IPVERSION;
31953be11f6SPoul-Henning Kamp 	nip->ip_hl = 5;
320df8bae1dSRodney W. Grimes 	nip->ip_p = IPPROTO_ICMP;
321df8bae1dSRodney W. Grimes 	nip->ip_tos = 0;
32241ea685cSAndrey V. Elsukov 	nip->ip_off = 0;
323df8bae1dSRodney W. Grimes 	icmp_reflect(m);
324df8bae1dSRodney W. Grimes 
325df8bae1dSRodney W. Grimes freeit:
326df8bae1dSRodney W. Grimes 	m_freem(n);
327df8bae1dSRodney W. Grimes }
328df8bae1dSRodney W. Grimes 
329df8bae1dSRodney W. Grimes /*
330df8bae1dSRodney W. Grimes  * Process a received ICMP message.
331df8bae1dSRodney W. Grimes  */
3328f5a8818SKevin Lo int
3338f5a8818SKevin Lo icmp_input(struct mbuf **mp, int *offp, int proto)
334df8bae1dSRodney W. Grimes {
33516d6c90fSAndre Oppermann 	struct icmp *icp;
336df8bae1dSRodney W. Grimes 	struct in_ifaddr *ia;
3378f5a8818SKevin Lo 	struct mbuf *m = *mp;
33816d6c90fSAndre Oppermann 	struct ip *ip = mtod(m, struct ip *);
33916d6c90fSAndre Oppermann 	struct sockaddr_in icmpsrc, icmpdst, icmpgw;
3408f5a8818SKevin Lo 	int hlen = *offp;
3418f5a8818SKevin Lo 	int icmplen = ntohs(ip->ip_len) - *offp;
34216d6c90fSAndre Oppermann 	int i, code;
3434d77a549SAlfred Perlstein 	void (*ctlfunc)(int, struct sockaddr *, void *);
3448b07e49aSJulian Elischer 	int fibnum;
345df8bae1dSRodney W. Grimes 
3468f5a8818SKevin Lo 	*mp = NULL;
3478f5a8818SKevin Lo 
348df8bae1dSRodney W. Grimes 	/*
349df8bae1dSRodney W. Grimes 	 * Locate icmp structure in mbuf, and check
350df8bae1dSRodney W. Grimes 	 * that not corrupted and of at least minimum length.
351df8bae1dSRodney W. Grimes 	 */
352df8bae1dSRodney W. Grimes #ifdef ICMPPRINTFS
3532b758395SGarrett Wollman 	if (icmpprintfs) {
3542b758395SGarrett Wollman 		char buf[4 * sizeof "123"];
3552b758395SGarrett Wollman 		strcpy(buf, inet_ntoa(ip->ip_src));
3562b758395SGarrett Wollman 		printf("icmp_input from %s to %s, len %d\n",
3572b758395SGarrett Wollman 		       buf, inet_ntoa(ip->ip_dst), icmplen);
3582b758395SGarrett Wollman 	}
359df8bae1dSRodney W. Grimes #endif
360df8bae1dSRodney W. Grimes 	if (icmplen < ICMP_MINLEN) {
361e27b0c87SRobert Watson 		ICMPSTAT_INC(icps_tooshort);
362df8bae1dSRodney W. Grimes 		goto freeit;
363df8bae1dSRodney W. Grimes 	}
364df8bae1dSRodney W. Grimes 	i = hlen + min(icmplen, ICMP_ADVLENMIN);
365852da713SBjoern A. Zeeb 	if (m->m_len < i && (m = m_pullup(m, i)) == NULL)  {
366e27b0c87SRobert Watson 		ICMPSTAT_INC(icps_tooshort);
3678f5a8818SKevin Lo 		return (IPPROTO_DONE);
368df8bae1dSRodney W. Grimes 	}
369df8bae1dSRodney W. Grimes 	ip = mtod(m, struct ip *);
370df8bae1dSRodney W. Grimes 	m->m_len -= hlen;
371df8bae1dSRodney W. Grimes 	m->m_data += hlen;
372df8bae1dSRodney W. Grimes 	icp = mtod(m, struct icmp *);
373df8bae1dSRodney W. Grimes 	if (in_cksum(m, icmplen)) {
374e27b0c87SRobert Watson 		ICMPSTAT_INC(icps_checksum);
375df8bae1dSRodney W. Grimes 		goto freeit;
376df8bae1dSRodney W. Grimes 	}
377df8bae1dSRodney W. Grimes 	m->m_len += hlen;
378df8bae1dSRodney W. Grimes 	m->m_data -= hlen;
379df8bae1dSRodney W. Grimes 
380df8bae1dSRodney W. Grimes #ifdef ICMPPRINTFS
381df8bae1dSRodney W. Grimes 	if (icmpprintfs)
382df8bae1dSRodney W. Grimes 		printf("icmp_input, type %d code %d\n", icp->icmp_type,
383df8bae1dSRodney W. Grimes 		    icp->icmp_code);
384df8bae1dSRodney W. Grimes #endif
3855b7ee6edSGarrett Wollman 
3865b7ee6edSGarrett Wollman 	/*
3875b7ee6edSGarrett Wollman 	 * Message type specific processing.
3885b7ee6edSGarrett Wollman 	 */
389df8bae1dSRodney W. Grimes 	if (icp->icmp_type > ICMP_MAXTYPE)
390df8bae1dSRodney W. Grimes 		goto raw;
39116d6c90fSAndre Oppermann 
39216d6c90fSAndre Oppermann 	/* Initialize */
39316d6c90fSAndre Oppermann 	bzero(&icmpsrc, sizeof(icmpsrc));
39416d6c90fSAndre Oppermann 	icmpsrc.sin_len = sizeof(struct sockaddr_in);
39516d6c90fSAndre Oppermann 	icmpsrc.sin_family = AF_INET;
39616d6c90fSAndre Oppermann 	bzero(&icmpdst, sizeof(icmpdst));
39716d6c90fSAndre Oppermann 	icmpdst.sin_len = sizeof(struct sockaddr_in);
39816d6c90fSAndre Oppermann 	icmpdst.sin_family = AF_INET;
39916d6c90fSAndre Oppermann 	bzero(&icmpgw, sizeof(icmpgw));
40016d6c90fSAndre Oppermann 	icmpgw.sin_len = sizeof(struct sockaddr_in);
40116d6c90fSAndre Oppermann 	icmpgw.sin_family = AF_INET;
40216d6c90fSAndre Oppermann 
403e27b0c87SRobert Watson 	ICMPSTAT_INC(icps_inhist[icp->icmp_type]);
404df8bae1dSRodney W. Grimes 	code = icp->icmp_code;
405df8bae1dSRodney W. Grimes 	switch (icp->icmp_type) {
406df8bae1dSRodney W. Grimes 
407df8bae1dSRodney W. Grimes 	case ICMP_UNREACH:
408df8bae1dSRodney W. Grimes 		switch (code) {
409df8bae1dSRodney W. Grimes 			case ICMP_UNREACH_NET:
410df8bae1dSRodney W. Grimes 			case ICMP_UNREACH_HOST:
411df8bae1dSRodney W. Grimes 			case ICMP_UNREACH_SRCFAIL:
412e4bb5b05SJonathan Lemon 			case ICMP_UNREACH_NET_UNKNOWN:
413e4bb5b05SJonathan Lemon 			case ICMP_UNREACH_HOST_UNKNOWN:
414e4bb5b05SJonathan Lemon 			case ICMP_UNREACH_ISOLATED:
415e4bb5b05SJonathan Lemon 			case ICMP_UNREACH_TOSNET:
416e4bb5b05SJonathan Lemon 			case ICMP_UNREACH_TOSHOST:
417e4bb5b05SJonathan Lemon 			case ICMP_UNREACH_HOST_PRECEDENCE:
418e4bb5b05SJonathan Lemon 			case ICMP_UNREACH_PRECEDENCE_CUTOFF:
419e4bb5b05SJonathan Lemon 				code = PRC_UNREACH_NET;
420df8bae1dSRodney W. Grimes 				break;
421df8bae1dSRodney W. Grimes 
422df8bae1dSRodney W. Grimes 			case ICMP_UNREACH_NEEDFRAG:
423df8bae1dSRodney W. Grimes 				code = PRC_MSGSIZE;
424df8bae1dSRodney W. Grimes 				break;
425df8bae1dSRodney W. Grimes 
426e4bb5b05SJonathan Lemon 			/*
427e4bb5b05SJonathan Lemon 			 * RFC 1122, Sections 3.2.2.1 and 4.2.3.9.
428e4bb5b05SJonathan Lemon 			 * Treat subcodes 2,3 as immediate RST
429e4bb5b05SJonathan Lemon 			 */
430e4bb5b05SJonathan Lemon 			case ICMP_UNREACH_PROTOCOL:
431e4bb5b05SJonathan Lemon 			case ICMP_UNREACH_PORT:
432b77d155dSJesper Skriver 				code = PRC_UNREACH_PORT;
433b11d7a4aSPoul-Henning Kamp 				break;
43490fcbbd6SPoul-Henning Kamp 
43590fcbbd6SPoul-Henning Kamp 			case ICMP_UNREACH_NET_PROHIB:
43690fcbbd6SPoul-Henning Kamp 			case ICMP_UNREACH_HOST_PROHIB:
4379c4b2574SPaul Traina 			case ICMP_UNREACH_FILTER_PROHIB:
43890fcbbd6SPoul-Henning Kamp 				code = PRC_UNREACH_ADMIN_PROHIB;
439b11d7a4aSPoul-Henning Kamp 				break;
440b11d7a4aSPoul-Henning Kamp 
441df8bae1dSRodney W. Grimes 			default:
442df8bae1dSRodney W. Grimes 				goto badcode;
443df8bae1dSRodney W. Grimes 		}
444df8bae1dSRodney W. Grimes 		goto deliver;
445df8bae1dSRodney W. Grimes 
446df8bae1dSRodney W. Grimes 	case ICMP_TIMXCEED:
447df8bae1dSRodney W. Grimes 		if (code > 1)
448df8bae1dSRodney W. Grimes 			goto badcode;
449df8bae1dSRodney W. Grimes 		code += PRC_TIMXCEED_INTRANS;
450df8bae1dSRodney W. Grimes 		goto deliver;
451df8bae1dSRodney W. Grimes 
452df8bae1dSRodney W. Grimes 	case ICMP_PARAMPROB:
453df8bae1dSRodney W. Grimes 		if (code > 1)
454df8bae1dSRodney W. Grimes 			goto badcode;
455df8bae1dSRodney W. Grimes 		code = PRC_PARAMPROB;
456df8bae1dSRodney W. Grimes 	deliver:
457df8bae1dSRodney W. Grimes 		/*
458df8bae1dSRodney W. Grimes 		 * Problem with datagram; advise higher level routines.
459df8bae1dSRodney W. Grimes 		 */
460df8bae1dSRodney W. Grimes 		if (icmplen < ICMP_ADVLENMIN || icmplen < ICMP_ADVLEN(icp) ||
46153be11f6SPoul-Henning Kamp 		    icp->icmp_ip.ip_hl < (sizeof(struct ip) >> 2)) {
462e27b0c87SRobert Watson 			ICMPSTAT_INC(icps_badlen);
463df8bae1dSRodney W. Grimes 			goto freeit;
464df8bae1dSRodney W. Grimes 		}
4655b7ee6edSGarrett Wollman 		/* Discard ICMP's in response to multicast packets */
4665b7ee6edSGarrett Wollman 		if (IN_MULTICAST(ntohl(icp->icmp_ip.ip_dst.s_addr)))
4675b7ee6edSGarrett Wollman 			goto badcode;
468df8bae1dSRodney W. Grimes #ifdef ICMPPRINTFS
469df8bae1dSRodney W. Grimes 		if (icmpprintfs)
470df8bae1dSRodney W. Grimes 			printf("deliver to protocol %d\n", icp->icmp_ip.ip_p);
471df8bae1dSRodney W. Grimes #endif
472df8bae1dSRodney W. Grimes 		icmpsrc.sin_addr = icp->icmp_ip.ip_dst;
4736a800098SYoshinobu Inoue 		/*
4746a800098SYoshinobu Inoue 		 * XXX if the packet contains [IPv4 AH TCP], we can't make a
4756a800098SYoshinobu Inoue 		 * notification to TCP layer.
4766a800098SYoshinobu Inoue 		 */
477623ae52eSPoul-Henning Kamp 		ctlfunc = inetsw[ip_protox[icp->icmp_ip.ip_p]].pr_ctlinput;
478623ae52eSPoul-Henning Kamp 		if (ctlfunc)
479df8bae1dSRodney W. Grimes 			(*ctlfunc)(code, (struct sockaddr *)&icmpsrc,
480b62d102cSBruce Evans 				   (void *)&icp->icmp_ip);
481df8bae1dSRodney W. Grimes 		break;
482df8bae1dSRodney W. Grimes 
483df8bae1dSRodney W. Grimes 	badcode:
484e27b0c87SRobert Watson 		ICMPSTAT_INC(icps_badcode);
485df8bae1dSRodney W. Grimes 		break;
486df8bae1dSRodney W. Grimes 
487df8bae1dSRodney W. Grimes 	case ICMP_ECHO:
4888b615593SMarko Zec 		if (!V_icmpbmcastecho
489d311884fSDavid Greenman 		    && (m->m_flags & (M_MCAST | M_BCAST)) != 0) {
490e27b0c87SRobert Watson 			ICMPSTAT_INC(icps_bmcastecho);
4917022ea0aSGarrett Wollman 			break;
4927022ea0aSGarrett Wollman 		}
493df8bae1dSRodney W. Grimes 		icp->icmp_type = ICMP_ECHOREPLY;
494a57815efSBosko Milekic 		if (badport_bandlim(BANDLIM_ICMP_ECHO) < 0)
49509f81a46SBosko Milekic 			goto freeit;
49609f81a46SBosko Milekic 		else
497df8bae1dSRodney W. Grimes 			goto reflect;
498df8bae1dSRodney W. Grimes 
499df8bae1dSRodney W. Grimes 	case ICMP_TSTAMP:
50000cb6befSMark Johnston 		if (V_icmptstamprepl == 0)
50100cb6befSMark Johnston 			break;
5028b615593SMarko Zec 		if (!V_icmpbmcastecho
503d311884fSDavid Greenman 		    && (m->m_flags & (M_MCAST | M_BCAST)) != 0) {
504e27b0c87SRobert Watson 			ICMPSTAT_INC(icps_bmcasttstamp);
505fe0fb8abSGarrett Wollman 			break;
506fe0fb8abSGarrett Wollman 		}
507df8bae1dSRodney W. Grimes 		if (icmplen < ICMP_TSLEN) {
508e27b0c87SRobert Watson 			ICMPSTAT_INC(icps_badlen);
509df8bae1dSRodney W. Grimes 			break;
510df8bae1dSRodney W. Grimes 		}
511df8bae1dSRodney W. Grimes 		icp->icmp_type = ICMP_TSTAMPREPLY;
512df8bae1dSRodney W. Grimes 		icp->icmp_rtime = iptime();
513df8bae1dSRodney W. Grimes 		icp->icmp_ttime = icp->icmp_rtime;	/* bogus, do later! */
514a57815efSBosko Milekic 		if (badport_bandlim(BANDLIM_ICMP_TSTAMP) < 0)
51509f81a46SBosko Milekic 			goto freeit;
51609f81a46SBosko Milekic 		else
517df8bae1dSRodney W. Grimes 			goto reflect;
518df8bae1dSRodney W. Grimes 
519df8bae1dSRodney W. Grimes 	case ICMP_MASKREQ:
5208b615593SMarko Zec 		if (V_icmpmaskrepl == 0)
521df8bae1dSRodney W. Grimes 			break;
522df8bae1dSRodney W. Grimes 		/*
523df8bae1dSRodney W. Grimes 		 * We are not able to respond with all ones broadcast
524df8bae1dSRodney W. Grimes 		 * unless we receive it over a point-to-point interface.
525df8bae1dSRodney W. Grimes 		 */
526df8bae1dSRodney W. Grimes 		if (icmplen < ICMP_MASKLEN)
527df8bae1dSRodney W. Grimes 			break;
528df8bae1dSRodney W. Grimes 		switch (ip->ip_dst.s_addr) {
529df8bae1dSRodney W. Grimes 
530df8bae1dSRodney W. Grimes 		case INADDR_BROADCAST:
531df8bae1dSRodney W. Grimes 		case INADDR_ANY:
532df8bae1dSRodney W. Grimes 			icmpdst.sin_addr = ip->ip_src;
533df8bae1dSRodney W. Grimes 			break;
534df8bae1dSRodney W. Grimes 
535df8bae1dSRodney W. Grimes 		default:
536df8bae1dSRodney W. Grimes 			icmpdst.sin_addr = ip->ip_dst;
537df8bae1dSRodney W. Grimes 		}
538df8bae1dSRodney W. Grimes 		ia = (struct in_ifaddr *)ifaof_ifpforaddr(
539df8bae1dSRodney W. Grimes 			    (struct sockaddr *)&icmpdst, m->m_pkthdr.rcvif);
5408c0fec80SRobert Watson 		if (ia == NULL)
541df8bae1dSRodney W. Grimes 			break;
5428c0fec80SRobert Watson 		if (ia->ia_ifp == NULL) {
5438c0fec80SRobert Watson 			ifa_free(&ia->ia_ifa);
5447e6f7714SPoul-Henning Kamp 			break;
5458c0fec80SRobert Watson 		}
546df8bae1dSRodney W. Grimes 		icp->icmp_type = ICMP_MASKREPLY;
5478b615593SMarko Zec 		if (V_icmpmaskfake == 0)
548df8bae1dSRodney W. Grimes 			icp->icmp_mask = ia->ia_sockmask.sin_addr.s_addr;
54957842a38SMatthew N. Dodd 		else
5508b615593SMarko Zec 			icp->icmp_mask = V_icmpmaskfake;
551df8bae1dSRodney W. Grimes 		if (ip->ip_src.s_addr == 0) {
552df8bae1dSRodney W. Grimes 			if (ia->ia_ifp->if_flags & IFF_BROADCAST)
553df8bae1dSRodney W. Grimes 			    ip->ip_src = satosin(&ia->ia_broadaddr)->sin_addr;
554df8bae1dSRodney W. Grimes 			else if (ia->ia_ifp->if_flags & IFF_POINTOPOINT)
555df8bae1dSRodney W. Grimes 			    ip->ip_src = satosin(&ia->ia_dstaddr)->sin_addr;
556df8bae1dSRodney W. Grimes 		}
5578c0fec80SRobert Watson 		ifa_free(&ia->ia_ifa);
558df8bae1dSRodney W. Grimes reflect:
559e27b0c87SRobert Watson 		ICMPSTAT_INC(icps_reflect);
560e27b0c87SRobert Watson 		ICMPSTAT_INC(icps_outhist[icp->icmp_type]);
561df8bae1dSRodney W. Grimes 		icmp_reflect(m);
5628f5a8818SKevin Lo 		return (IPPROTO_DONE);
563df8bae1dSRodney W. Grimes 
564df8bae1dSRodney W. Grimes 	case ICMP_REDIRECT:
5658b615593SMarko Zec 		if (V_log_redirect) {
56618d3153eSDag-Erling Smørgrav 			u_long src, dst, gw;
56718d3153eSDag-Erling Smørgrav 
56818d3153eSDag-Erling Smørgrav 			src = ntohl(ip->ip_src.s_addr);
56918d3153eSDag-Erling Smørgrav 			dst = ntohl(icp->icmp_ip.ip_dst.s_addr);
57018d3153eSDag-Erling Smørgrav 			gw = ntohl(icp->icmp_gwaddr.s_addr);
57118d3153eSDag-Erling Smørgrav 			printf("icmp redirect from %d.%d.%d.%d: "
57218d3153eSDag-Erling Smørgrav 			       "%d.%d.%d.%d => %d.%d.%d.%d\n",
57318d3153eSDag-Erling Smørgrav 			       (int)(src >> 24), (int)((src >> 16) & 0xff),
57418d3153eSDag-Erling Smørgrav 			       (int)((src >> 8) & 0xff), (int)(src & 0xff),
57518d3153eSDag-Erling Smørgrav 			       (int)(dst >> 24), (int)((dst >> 16) & 0xff),
57618d3153eSDag-Erling Smørgrav 			       (int)((dst >> 8) & 0xff), (int)(dst & 0xff),
57718d3153eSDag-Erling Smørgrav 			       (int)(gw >> 24), (int)((gw >> 16) & 0xff),
57818d3153eSDag-Erling Smørgrav 			       (int)((gw >> 8) & 0xff), (int)(gw & 0xff));
57918d3153eSDag-Erling Smørgrav 		}
58087c3bd27SAndre Oppermann 		/*
58187c3bd27SAndre Oppermann 		 * RFC1812 says we must ignore ICMP redirects if we
58287c3bd27SAndre Oppermann 		 * are acting as router.
58387c3bd27SAndre Oppermann 		 */
5848b615593SMarko Zec 		if (V_drop_redirect || V_ipforwarding)
58518d3153eSDag-Erling Smørgrav 			break;
586df8bae1dSRodney W. Grimes 		if (code > 3)
587df8bae1dSRodney W. Grimes 			goto badcode;
588df8bae1dSRodney W. Grimes 		if (icmplen < ICMP_ADVLENMIN || icmplen < ICMP_ADVLEN(icp) ||
58953be11f6SPoul-Henning Kamp 		    icp->icmp_ip.ip_hl < (sizeof(struct ip) >> 2)) {
590e27b0c87SRobert Watson 			ICMPSTAT_INC(icps_badlen);
591df8bae1dSRodney W. Grimes 			break;
592df8bae1dSRodney W. Grimes 		}
593df8bae1dSRodney W. Grimes 		/*
594df8bae1dSRodney W. Grimes 		 * Short circuit routing redirects to force
595df8bae1dSRodney W. Grimes 		 * immediate change in the kernel's routing
596df8bae1dSRodney W. Grimes 		 * tables.  The message is also handed to anyone
597df8bae1dSRodney W. Grimes 		 * listening on a raw socket (e.g. the routing
598df8bae1dSRodney W. Grimes 		 * daemon for use in updating its tables).
599df8bae1dSRodney W. Grimes 		 */
600df8bae1dSRodney W. Grimes 		icmpgw.sin_addr = ip->ip_src;
601df8bae1dSRodney W. Grimes 		icmpdst.sin_addr = icp->icmp_gwaddr;
602df8bae1dSRodney W. Grimes #ifdef	ICMPPRINTFS
6032b758395SGarrett Wollman 		if (icmpprintfs) {
6042b758395SGarrett Wollman 			char buf[4 * sizeof "123"];
6052b758395SGarrett Wollman 			strcpy(buf, inet_ntoa(icp->icmp_ip.ip_dst));
6062b758395SGarrett Wollman 
6072b758395SGarrett Wollman 			printf("redirect dst %s to %s\n",
6082b758395SGarrett Wollman 			       buf, inet_ntoa(icp->icmp_gwaddr));
6092b758395SGarrett Wollman 		}
610df8bae1dSRodney W. Grimes #endif
611df8bae1dSRodney W. Grimes 		icmpsrc.sin_addr = icp->icmp_ip.ip_dst;
6128b07e49aSJulian Elischer 		for ( fibnum = 0; fibnum < rt_numfibs; fibnum++) {
6138b07e49aSJulian Elischer 			in_rtredirect((struct sockaddr *)&icmpsrc,
614df8bae1dSRodney W. Grimes 			  (struct sockaddr *)&icmpdst,
615df8bae1dSRodney W. Grimes 			  (struct sockaddr *)0, RTF_GATEWAY | RTF_HOST,
6168b07e49aSJulian Elischer 			  (struct sockaddr *)&icmpgw, fibnum);
6178b07e49aSJulian Elischer 		}
618df8bae1dSRodney W. Grimes 		pfctlinput(PRC_REDIRECT_HOST, (struct sockaddr *)&icmpsrc);
619df8bae1dSRodney W. Grimes 		break;
620df8bae1dSRodney W. Grimes 
621df8bae1dSRodney W. Grimes 	/*
622df8bae1dSRodney W. Grimes 	 * No kernel processing for the following;
623df8bae1dSRodney W. Grimes 	 * just fall through to send to raw listener.
624df8bae1dSRodney W. Grimes 	 */
625df8bae1dSRodney W. Grimes 	case ICMP_ECHOREPLY:
626df8bae1dSRodney W. Grimes 	case ICMP_ROUTERADVERT:
627df8bae1dSRodney W. Grimes 	case ICMP_ROUTERSOLICIT:
628df8bae1dSRodney W. Grimes 	case ICMP_TSTAMPREPLY:
629df8bae1dSRodney W. Grimes 	case ICMP_IREQREPLY:
630df8bae1dSRodney W. Grimes 	case ICMP_MASKREPLY:
631d1f79a3bSAlexander V. Chernikov 	case ICMP_SOURCEQUENCH:
632df8bae1dSRodney W. Grimes 	default:
633df8bae1dSRodney W. Grimes 		break;
634df8bae1dSRodney W. Grimes 	}
635df8bae1dSRodney W. Grimes 
636df8bae1dSRodney W. Grimes raw:
6378f5a8818SKevin Lo 	*mp = m;
6388f5a8818SKevin Lo 	rip_input(mp, offp, proto);
6398f5a8818SKevin Lo 	return (IPPROTO_DONE);
640df8bae1dSRodney W. Grimes 
641df8bae1dSRodney W. Grimes freeit:
642df8bae1dSRodney W. Grimes 	m_freem(m);
6438f5a8818SKevin Lo 	return (IPPROTO_DONE);
644df8bae1dSRodney W. Grimes }
645df8bae1dSRodney W. Grimes 
646df8bae1dSRodney W. Grimes /*
647df8bae1dSRodney W. Grimes  * Reflect the ip packet back to the source
648df8bae1dSRodney W. Grimes  */
6490312fbe9SPoul-Henning Kamp static void
650f2565d68SRobert Watson icmp_reflect(struct mbuf *m)
651df8bae1dSRodney W. Grimes {
652*cc0a3c8cSAndrey V. Elsukov 	struct rm_priotracker in_ifa_tracker;
653ca925d9cSJonathan Lemon 	struct ip *ip = mtod(m, struct ip *);
654ca925d9cSJonathan Lemon 	struct ifaddr *ifa;
65533c4f96dSRobert Watson 	struct ifnet *ifp;
656ca925d9cSJonathan Lemon 	struct in_ifaddr *ia;
657df8bae1dSRodney W. Grimes 	struct in_addr t;
658b5e8ce9fSBruce Evans 	struct mbuf *opts = 0;
65953be11f6SPoul-Henning Kamp 	int optlen = (ip->ip_hl << 2) - sizeof(struct ip);
660df8bae1dSRodney W. Grimes 
6616b9ff6b7SGeorge V. Neville-Neil 	if (IN_MULTICAST(ntohl(ip->ip_src.s_addr)) ||
6626b9ff6b7SGeorge V. Neville-Neil 	    IN_EXPERIMENTAL(ntohl(ip->ip_src.s_addr)) ||
6636b9ff6b7SGeorge V. Neville-Neil 	    IN_ZERONET(ntohl(ip->ip_src.s_addr)) ) {
664df8bae1dSRodney W. Grimes 		m_freem(m);	/* Bad return address */
665e27b0c87SRobert Watson 		ICMPSTAT_INC(icps_badaddr);
666df8bae1dSRodney W. Grimes 		goto done;	/* Ip_output() will check for broadcast */
667df8bae1dSRodney W. Grimes 	}
6686b9ff6b7SGeorge V. Neville-Neil 
669df8bae1dSRodney W. Grimes 	t = ip->ip_dst;
670df8bae1dSRodney W. Grimes 	ip->ip_dst = ip->ip_src;
6711488eac8SAndre Oppermann 
672df8bae1dSRodney W. Grimes 	/*
6731488eac8SAndre Oppermann 	 * Source selection for ICMP replies:
6741488eac8SAndre Oppermann 	 *
6751488eac8SAndre Oppermann 	 * If the incoming packet was addressed directly to one of our
6761488eac8SAndre Oppermann 	 * own addresses, use dst as the src for the reply.
677df8bae1dSRodney W. Grimes 	 */
678*cc0a3c8cSAndrey V. Elsukov 	IN_IFADDR_RLOCK(&in_ifa_tracker);
67933c4f96dSRobert Watson 	LIST_FOREACH(ia, INADDR_HASH(t.s_addr), ia_hash) {
68033c4f96dSRobert Watson 		if (t.s_addr == IA_SIN(ia)->sin_addr.s_addr) {
68133c4f96dSRobert Watson 			t = IA_SIN(ia)->sin_addr;
682*cc0a3c8cSAndrey V. Elsukov 			IN_IFADDR_RUNLOCK(&in_ifa_tracker);
683ca925d9cSJonathan Lemon 			goto match;
68433c4f96dSRobert Watson 		}
68533c4f96dSRobert Watson 	}
686*cc0a3c8cSAndrey V. Elsukov 	IN_IFADDR_RUNLOCK(&in_ifa_tracker);
6872d9cfabaSRobert Watson 
6881488eac8SAndre Oppermann 	/*
6891488eac8SAndre Oppermann 	 * If the incoming packet was addressed to one of our broadcast
6901488eac8SAndre Oppermann 	 * addresses, use the first non-broadcast address which corresponds
6911488eac8SAndre Oppermann 	 * to the incoming interface.
6921488eac8SAndre Oppermann 	 */
69333c4f96dSRobert Watson 	ifp = m->m_pkthdr.rcvif;
69433c4f96dSRobert Watson 	if (ifp != NULL && ifp->if_flags & IFF_BROADCAST) {
695137f91e8SJohn Baldwin 		IF_ADDR_RLOCK(ifp);
69633c4f96dSRobert Watson 		TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
697ca925d9cSJonathan Lemon 			if (ifa->ifa_addr->sa_family != AF_INET)
698ca925d9cSJonathan Lemon 				continue;
699ca925d9cSJonathan Lemon 			ia = ifatoia(ifa);
700ca925d9cSJonathan Lemon 			if (satosin(&ia->ia_broadaddr)->sin_addr.s_addr ==
70133c4f96dSRobert Watson 			    t.s_addr) {
70233c4f96dSRobert Watson 				t = IA_SIN(ia)->sin_addr;
703137f91e8SJohn Baldwin 				IF_ADDR_RUNLOCK(ifp);
704ca925d9cSJonathan Lemon 				goto match;
705df8bae1dSRodney W. Grimes 			}
706ca925d9cSJonathan Lemon 		}
707137f91e8SJohn Baldwin 		IF_ADDR_RUNLOCK(ifp);
70833c4f96dSRobert Watson 	}
7091488eac8SAndre Oppermann 	/*
710a0866c8dSAndre Oppermann 	 * If the packet was transiting through us, use the address of
711a0866c8dSAndre Oppermann 	 * the interface the packet came through in.  If that interface
712a0866c8dSAndre Oppermann 	 * doesn't have a suitable IP address, the normal selection
713a0866c8dSAndre Oppermann 	 * criteria apply.
714a0866c8dSAndre Oppermann 	 */
71533c4f96dSRobert Watson 	if (V_icmp_rfi && ifp != NULL) {
716137f91e8SJohn Baldwin 		IF_ADDR_RLOCK(ifp);
71733c4f96dSRobert Watson 		TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
718a0866c8dSAndre Oppermann 			if (ifa->ifa_addr->sa_family != AF_INET)
719a0866c8dSAndre Oppermann 				continue;
720a0866c8dSAndre Oppermann 			ia = ifatoia(ifa);
72133c4f96dSRobert Watson 			t = IA_SIN(ia)->sin_addr;
722137f91e8SJohn Baldwin 			IF_ADDR_RUNLOCK(ifp);
723a0866c8dSAndre Oppermann 			goto match;
724a0866c8dSAndre Oppermann 		}
725137f91e8SJohn Baldwin 		IF_ADDR_RUNLOCK(ifp);
726a0866c8dSAndre Oppermann 	}
727a0866c8dSAndre Oppermann 	/*
728b74d89bbSAndre Oppermann 	 * If the incoming packet was not addressed directly to us, use
729b74d89bbSAndre Oppermann 	 * designated interface for icmp replies specified by sysctl
730b74d89bbSAndre Oppermann 	 * net.inet.icmp.reply_src (default not set). Otherwise continue
731b74d89bbSAndre Oppermann 	 * with normal source selection.
732b74d89bbSAndre Oppermann 	 */
73333c4f96dSRobert Watson 	if (V_reply_src[0] != '\0' && (ifp = ifunit(V_reply_src))) {
734137f91e8SJohn Baldwin 		IF_ADDR_RLOCK(ifp);
73533c4f96dSRobert Watson 		TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
736b74d89bbSAndre Oppermann 			if (ifa->ifa_addr->sa_family != AF_INET)
737b74d89bbSAndre Oppermann 				continue;
738b74d89bbSAndre Oppermann 			ia = ifatoia(ifa);
73933c4f96dSRobert Watson 			t = IA_SIN(ia)->sin_addr;
740137f91e8SJohn Baldwin 			IF_ADDR_RUNLOCK(ifp);
741b74d89bbSAndre Oppermann 			goto match;
742b74d89bbSAndre Oppermann 		}
743137f91e8SJohn Baldwin 		IF_ADDR_RUNLOCK(ifp);
744b74d89bbSAndre Oppermann 	}
745b74d89bbSAndre Oppermann 	/*
7461488eac8SAndre Oppermann 	 * If the packet was transiting through us, use the address of
7471488eac8SAndre Oppermann 	 * the interface that is the closest to the packet source.
7481488eac8SAndre Oppermann 	 * When we don't have a route back to the packet source, stop here
7491488eac8SAndre Oppermann 	 * and drop the packet.
7501488eac8SAndre Oppermann 	 */
7518b07e49aSJulian Elischer 	ia = ip_rtaddr(ip->ip_dst, M_GETFIB(m));
7520d4bef5dSDima Dorfman 	if (ia == NULL) {
7530d4bef5dSDima Dorfman 		m_freem(m);
754e27b0c87SRobert Watson 		ICMPSTAT_INC(icps_noroute);
7550d4bef5dSDima Dorfman 		goto done;
7560d4bef5dSDima Dorfman 	}
75733c4f96dSRobert Watson 	t = IA_SIN(ia)->sin_addr;
7588c0fec80SRobert Watson 	ifa_free(&ia->ia_ifa);
759ca925d9cSJonathan Lemon match:
760baee0c3eSRobert Watson #ifdef MAC
761a13e21f7SRobert Watson 	mac_netinet_icmp_replyinplace(m);
762baee0c3eSRobert Watson #endif
763df8bae1dSRodney W. Grimes 	ip->ip_src = t;
764603724d3SBjoern A. Zeeb 	ip->ip_ttl = V_ip_defttl;
765df8bae1dSRodney W. Grimes 
766df8bae1dSRodney W. Grimes 	if (optlen > 0) {
767df8bae1dSRodney W. Grimes 		register u_char *cp;
768df8bae1dSRodney W. Grimes 		int opt, cnt;
769df8bae1dSRodney W. Grimes 		u_int len;
770df8bae1dSRodney W. Grimes 
771df8bae1dSRodney W. Grimes 		/*
772df8bae1dSRodney W. Grimes 		 * Retrieve any source routing from the incoming packet;
773df8bae1dSRodney W. Grimes 		 * add on any record-route or timestamp options.
774df8bae1dSRodney W. Grimes 		 */
775df8bae1dSRodney W. Grimes 		cp = (u_char *) (ip + 1);
776e0982661SAndre Oppermann 		if ((opts = ip_srcroute(m)) == 0 &&
777eb1b1807SGleb Smirnoff 		    (opts = m_gethdr(M_NOWAIT, MT_DATA))) {
778df8bae1dSRodney W. Grimes 			opts->m_len = sizeof(struct in_addr);
779df8bae1dSRodney W. Grimes 			mtod(opts, struct in_addr *)->s_addr = 0;
780df8bae1dSRodney W. Grimes 		}
781df8bae1dSRodney W. Grimes 		if (opts) {
782df8bae1dSRodney W. Grimes #ifdef ICMPPRINTFS
783df8bae1dSRodney W. Grimes 		    if (icmpprintfs)
784df8bae1dSRodney W. Grimes 			    printf("icmp_reflect optlen %d rt %d => ",
785df8bae1dSRodney W. Grimes 				optlen, opts->m_len);
786df8bae1dSRodney W. Grimes #endif
787df8bae1dSRodney W. Grimes 		    for (cnt = optlen; cnt > 0; cnt -= len, cp += len) {
788df8bae1dSRodney W. Grimes 			    opt = cp[IPOPT_OPTVAL];
789df8bae1dSRodney W. Grimes 			    if (opt == IPOPT_EOL)
790df8bae1dSRodney W. Grimes 				    break;
791df8bae1dSRodney W. Grimes 			    if (opt == IPOPT_NOP)
792df8bae1dSRodney W. Grimes 				    len = 1;
793df8bae1dSRodney W. Grimes 			    else {
794707d00a3SJonathan Lemon 				    if (cnt < IPOPT_OLEN + sizeof(*cp))
795707d00a3SJonathan Lemon 					    break;
796df8bae1dSRodney W. Grimes 				    len = cp[IPOPT_OLEN];
797707d00a3SJonathan Lemon 				    if (len < IPOPT_OLEN + sizeof(*cp) ||
798707d00a3SJonathan Lemon 				        len > cnt)
799df8bae1dSRodney W. Grimes 					    break;
800df8bae1dSRodney W. Grimes 			    }
801df8bae1dSRodney W. Grimes 			    /*
802df8bae1dSRodney W. Grimes 			     * Should check for overflow, but it "can't happen"
803df8bae1dSRodney W. Grimes 			     */
804df8bae1dSRodney W. Grimes 			    if (opt == IPOPT_RR || opt == IPOPT_TS ||
805df8bae1dSRodney W. Grimes 				opt == IPOPT_SECURITY) {
806df8bae1dSRodney W. Grimes 				    bcopy((caddr_t)cp,
807df8bae1dSRodney W. Grimes 					mtod(opts, caddr_t) + opts->m_len, len);
808df8bae1dSRodney W. Grimes 				    opts->m_len += len;
809df8bae1dSRodney W. Grimes 			    }
810df8bae1dSRodney W. Grimes 		    }
811df8bae1dSRodney W. Grimes 		    /* Terminate & pad, if necessary */
812623ae52eSPoul-Henning Kamp 		    cnt = opts->m_len % 4;
813623ae52eSPoul-Henning Kamp 		    if (cnt) {
814df8bae1dSRodney W. Grimes 			    for (; cnt < 4; cnt++) {
815df8bae1dSRodney W. Grimes 				    *(mtod(opts, caddr_t) + opts->m_len) =
816df8bae1dSRodney W. Grimes 					IPOPT_EOL;
817df8bae1dSRodney W. Grimes 				    opts->m_len++;
818df8bae1dSRodney W. Grimes 			    }
819df8bae1dSRodney W. Grimes 		    }
820df8bae1dSRodney W. Grimes #ifdef ICMPPRINTFS
821df8bae1dSRodney W. Grimes 		    if (icmpprintfs)
822df8bae1dSRodney W. Grimes 			    printf("%d\n", opts->m_len);
823df8bae1dSRodney W. Grimes #endif
824df8bae1dSRodney W. Grimes 		}
8259e2a372fSGleb Smirnoff 		ip_stripoptions(m);
826df8bae1dSRodney W. Grimes 	}
8279c855a36SSam Leffler 	m_tag_delete_nonpersistent(m);
828df8bae1dSRodney W. Grimes 	m->m_flags &= ~(M_BCAST|M_MCAST);
82902c1c707SAndre Oppermann 	icmp_send(m, opts);
830df8bae1dSRodney W. Grimes done:
831df8bae1dSRodney W. Grimes 	if (opts)
832df8bae1dSRodney W. Grimes 		(void)m_free(opts);
833df8bae1dSRodney W. Grimes }
834df8bae1dSRodney W. Grimes 
835df8bae1dSRodney W. Grimes /*
836df8bae1dSRodney W. Grimes  * Send an icmp packet back to the ip level,
837df8bae1dSRodney W. Grimes  * after supplying a checksum.
838df8bae1dSRodney W. Grimes  */
8390312fbe9SPoul-Henning Kamp static void
840f2565d68SRobert Watson icmp_send(struct mbuf *m, struct mbuf *opts)
841df8bae1dSRodney W. Grimes {
842df8bae1dSRodney W. Grimes 	register struct ip *ip = mtod(m, struct ip *);
843df8bae1dSRodney W. Grimes 	register int hlen;
844df8bae1dSRodney W. Grimes 	register struct icmp *icp;
845df8bae1dSRodney W. Grimes 
84653be11f6SPoul-Henning Kamp 	hlen = ip->ip_hl << 2;
847df8bae1dSRodney W. Grimes 	m->m_data += hlen;
848df8bae1dSRodney W. Grimes 	m->m_len -= hlen;
849df8bae1dSRodney W. Grimes 	icp = mtod(m, struct icmp *);
850df8bae1dSRodney W. Grimes 	icp->icmp_cksum = 0;
8518f134647SGleb Smirnoff 	icp->icmp_cksum = in_cksum(m, ntohs(ip->ip_len) - hlen);
852df8bae1dSRodney W. Grimes 	m->m_data -= hlen;
853df8bae1dSRodney W. Grimes 	m->m_len += hlen;
85494446a2eSArchie Cobbs 	m->m_pkthdr.rcvif = (struct ifnet *)0;
855df8bae1dSRodney W. Grimes #ifdef ICMPPRINTFS
8562b758395SGarrett Wollman 	if (icmpprintfs) {
8572b758395SGarrett Wollman 		char buf[4 * sizeof "123"];
8582b758395SGarrett Wollman 		strcpy(buf, inet_ntoa(ip->ip_dst));
8592b758395SGarrett Wollman 		printf("icmp_send dst %s src %s\n",
8602b758395SGarrett Wollman 		       buf, inet_ntoa(ip->ip_src));
8612b758395SGarrett Wollman 	}
862df8bae1dSRodney W. Grimes #endif
86302c1c707SAndre Oppermann 	(void) ip_output(m, opts, NULL, 0, NULL, NULL);
864df8bae1dSRodney W. Grimes }
865df8bae1dSRodney W. Grimes 
866d685b6eeSLuigi Rizzo /*
86761f26caeSWarner Losh  * Return milliseconds since 00:00 UTC in network format.
868d685b6eeSLuigi Rizzo  */
869d685b6eeSLuigi Rizzo uint32_t
870f2565d68SRobert Watson iptime(void)
871df8bae1dSRodney W. Grimes {
872df8bae1dSRodney W. Grimes 	struct timeval atv;
873df8bae1dSRodney W. Grimes 	u_long t;
874df8bae1dSRodney W. Grimes 
87516cd6db0SBill Fumerola 	getmicrotime(&atv);
876df8bae1dSRodney W. Grimes 	t = (atv.tv_sec % (24*60*60)) * 1000 + atv.tv_usec / 1000;
877df8bae1dSRodney W. Grimes 	return (htonl(t));
878df8bae1dSRodney W. Grimes }
879df8bae1dSRodney W. Grimes 
8805cbf3e08SGarrett Wollman /*
8815cbf3e08SGarrett Wollman  * Return the next larger or smaller MTU plateau (table from RFC 1191)
8825cbf3e08SGarrett Wollman  * given current value MTU.  If DIR is less than zero, a larger plateau
8835cbf3e08SGarrett Wollman  * is returned; otherwise, a smaller value is returned.
8845cbf3e08SGarrett Wollman  */
8851aedbd9cSAndre Oppermann int
886f2565d68SRobert Watson ip_next_mtu(int mtu, int dir)
8875cbf3e08SGarrett Wollman {
8885cbf3e08SGarrett Wollman 	static int mtutab[] = {
8894c037f8dSAndre Oppermann 		65535, 32000, 17914, 8166, 4352, 2002, 1492, 1280, 1006, 508,
8904c037f8dSAndre Oppermann 		296, 68, 0
8915cbf3e08SGarrett Wollman 	};
89206003a1eSAndre Oppermann 	int i, size;
8935cbf3e08SGarrett Wollman 
89406003a1eSAndre Oppermann 	size = (sizeof mtutab) / (sizeof mtutab[0]);
89506003a1eSAndre Oppermann 	if (dir >= 0) {
8961c0b0f52SGleb Smirnoff 		for (i = 0; i < size; i++)
89706003a1eSAndre Oppermann 			if (mtu > mtutab[i])
8985cbf3e08SGarrett Wollman 				return mtutab[i];
8995cbf3e08SGarrett Wollman 	} else {
90006003a1eSAndre Oppermann 		for (i = size - 1; i >= 0; i--)
90106003a1eSAndre Oppermann 			if (mtu < mtutab[i])
90206003a1eSAndre Oppermann 				return mtutab[i];
90306003a1eSAndre Oppermann 		if (mtu == mtutab[0])
90406003a1eSAndre Oppermann 			return mtutab[0];
9055cbf3e08SGarrett Wollman 	}
90606003a1eSAndre Oppermann 	return 0;
9075cbf3e08SGarrett Wollman }
908b8e463e6SBjoern A. Zeeb #endif /* INET */
90951508de1SMatthew Dillon 
91051508de1SMatthew Dillon 
91151508de1SMatthew Dillon /*
91251508de1SMatthew Dillon  * badport_bandlim() - check for ICMP bandwidth limit
91351508de1SMatthew Dillon  *
91451508de1SMatthew Dillon  *	Return 0 if it is ok to send an ICMP error response, -1 if we have
91551508de1SMatthew Dillon  *	hit our bandwidth limit and it is not ok.
91651508de1SMatthew Dillon  *
91751508de1SMatthew Dillon  *	If icmplim is <= 0, the feature is disabled and 0 is returned.
91851508de1SMatthew Dillon  *
91951508de1SMatthew Dillon  *	For now we separate the TCP and UDP subsystems w/ different 'which'
92051508de1SMatthew Dillon  *	values.  We may eventually remove this separation (and simplify the
92151508de1SMatthew Dillon  *	code further).
92251508de1SMatthew Dillon  *
92351508de1SMatthew Dillon  *	Note that the printing of the error message is delayed so we can
92451508de1SMatthew Dillon  *	properly print the icmp error rate that the system was trying to do
92551508de1SMatthew Dillon  *	(i.e. 22000/100 pps, etc...).  This can cause long delays in printing
92651508de1SMatthew Dillon  *	the 'final' error, but it doesn't make sense to solve the printing
92751508de1SMatthew Dillon  *	delay with more complex code.
92851508de1SMatthew Dillon  */
92951508de1SMatthew Dillon 
93051508de1SMatthew Dillon int
93151508de1SMatthew Dillon badport_bandlim(int which)
93251508de1SMatthew Dillon {
9338b615593SMarko Zec 
93400f21882SSam Leffler #define	N(a)	(sizeof (a) / sizeof (a[0]))
93500f21882SSam Leffler 	static struct rate {
93600f21882SSam Leffler 		const char	*type;
93700f21882SSam Leffler 		struct timeval	lasttime;
938439dfb0cSStefan Farfeleder 		int		curpps;
93900f21882SSam Leffler 	} rates[BANDLIM_MAX+1] = {
94000f21882SSam Leffler 		{ "icmp unreach response" },
94100f21882SSam Leffler 		{ "icmp ping response" },
94200f21882SSam Leffler 		{ "icmp tstamp response" },
94300f21882SSam Leffler 		{ "closed port RST response" },
94408af97b7SRobert Watson 		{ "open port RST response" },
9455ad9e57bSMichael Tuexen 		{ "icmp6 unreach response" },
9465ad9e57bSMichael Tuexen 		{ "sctp ootb response" }
94709f81a46SBosko Milekic 	};
94851508de1SMatthew Dillon 
94951508de1SMatthew Dillon 	/*
95000f21882SSam Leffler 	 * Return ok status if feature disabled or argument out of range.
95151508de1SMatthew Dillon 	 */
9528b615593SMarko Zec 	if (V_icmplim > 0 && (u_int) which < N(rates)) {
95300f21882SSam Leffler 		struct rate *r = &rates[which];
95400f21882SSam Leffler 		int opps = r->curpps;
95551508de1SMatthew Dillon 
9568b615593SMarko Zec 		if (!ppsratecheck(&r->lasttime, &r->curpps, V_icmplim))
95700f21882SSam Leffler 			return -1;	/* discard packet */
95851508de1SMatthew Dillon 		/*
95900f21882SSam Leffler 		 * If we've dropped below the threshold after having
96000f21882SSam Leffler 		 * rate-limited traffic print the message.  This preserves
96100f21882SSam Leffler 		 * the previous behaviour at the expense of added complexity.
96251508de1SMatthew Dillon 		 */
9638b615593SMarko Zec 		if (V_icmplim_output && opps > V_icmplim)
96440fe9effSAndre Oppermann 			log(LOG_NOTICE, "Limiting %s from %d to %d packets/sec\n",
9658b615593SMarko Zec 				r->type, opps, V_icmplim);
96651508de1SMatthew Dillon 	}
96700f21882SSam Leffler 	return 0;			/* okay to send packet */
96800f21882SSam Leffler #undef N
96951508de1SMatthew Dillon }
970