xref: /freebsd/sys/netinet/ip_icmp.c (revision 315e3e38faa1ac7e775bbbbca0079c23fa3513ea)
1c398230bSWarner Losh /*-
2df8bae1dSRodney W. Grimes  * Copyright (c) 1982, 1986, 1988, 1993
3df8bae1dSRodney W. Grimes  *	The Regents of the University of California.  All rights reserved.
4df8bae1dSRodney W. Grimes  *
5df8bae1dSRodney W. Grimes  * Redistribution and use in source and binary forms, with or without
6df8bae1dSRodney W. Grimes  * modification, are permitted provided that the following conditions
7df8bae1dSRodney W. Grimes  * are met:
8df8bae1dSRodney W. Grimes  * 1. Redistributions of source code must retain the above copyright
9df8bae1dSRodney W. Grimes  *    notice, this list of conditions and the following disclaimer.
10df8bae1dSRodney W. Grimes  * 2. Redistributions in binary form must reproduce the above copyright
11df8bae1dSRodney W. Grimes  *    notice, this list of conditions and the following disclaimer in the
12df8bae1dSRodney W. Grimes  *    documentation and/or other materials provided with the distribution.
13df8bae1dSRodney W. Grimes  * 4. Neither the name of the University nor the names of its contributors
14df8bae1dSRodney W. Grimes  *    may be used to endorse or promote products derived from this software
15df8bae1dSRodney W. Grimes  *    without specific prior written permission.
16df8bae1dSRodney W. Grimes  *
17df8bae1dSRodney W. Grimes  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
18df8bae1dSRodney W. Grimes  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19df8bae1dSRodney W. Grimes  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20df8bae1dSRodney W. Grimes  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
21df8bae1dSRodney W. Grimes  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22df8bae1dSRodney W. Grimes  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23df8bae1dSRodney W. Grimes  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24df8bae1dSRodney W. Grimes  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25df8bae1dSRodney W. Grimes  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26df8bae1dSRodney W. Grimes  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27df8bae1dSRodney W. Grimes  * SUCH DAMAGE.
28df8bae1dSRodney W. Grimes  *
29df8bae1dSRodney W. Grimes  *	@(#)ip_icmp.c	8.2 (Berkeley) 1/4/94
30df8bae1dSRodney W. Grimes  */
31df8bae1dSRodney W. Grimes 
324b421e2dSMike Silbersack #include <sys/cdefs.h>
334b421e2dSMike Silbersack __FBSDID("$FreeBSD$");
344b421e2dSMike Silbersack 
356a800098SYoshinobu Inoue #include "opt_ipsec.h"
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>
44b5e8ce9fSBruce Evans #include <sys/sysctl.h>
45df8bae1dSRodney W. Grimes 
46df8bae1dSRodney W. Grimes #include <net/if.h>
479494d596SBrooks Davis #include <net/if_types.h>
48df8bae1dSRodney W. Grimes #include <net/route.h>
49eddfbb76SRobert Watson #include <net/vnet.h>
50df8bae1dSRodney W. Grimes 
51df8bae1dSRodney W. Grimes #include <netinet/in.h>
5297d8d152SAndre Oppermann #include <netinet/in_pcb.h>
53df8bae1dSRodney W. Grimes #include <netinet/in_systm.h>
54df8bae1dSRodney W. Grimes #include <netinet/in_var.h>
55df8bae1dSRodney W. Grimes #include <netinet/ip.h>
56df8bae1dSRodney W. Grimes #include <netinet/ip_icmp.h>
57b5e8ce9fSBruce Evans #include <netinet/ip_var.h>
58ef39adf0SAndre Oppermann #include <netinet/ip_options.h>
5997d8d152SAndre Oppermann #include <netinet/tcp.h>
6097d8d152SAndre Oppermann #include <netinet/tcp_var.h>
6197d8d152SAndre Oppermann #include <netinet/tcpip.h>
62df8bae1dSRodney W. Grimes #include <netinet/icmp_var.h>
63df8bae1dSRodney W. Grimes 
64b2630c29SGeorge V. Neville-Neil #ifdef IPSEC
65b9234fafSSam Leffler #include <netipsec/ipsec.h>
66b9234fafSSam Leffler #include <netipsec/key.h>
67b9234fafSSam Leffler #endif
68b9234fafSSam Leffler 
6972a52a35SJonathan Lemon #include <machine/in_cksum.h>
7072a52a35SJonathan Lemon 
71aed55708SRobert Watson #include <security/mac/mac_framework.h>
72aed55708SRobert Watson 
73df8bae1dSRodney W. Grimes /*
74df8bae1dSRodney W. Grimes  * ICMP routines: error generation, receive packet processing, and
75df8bae1dSRodney W. Grimes  * routines to turnaround packets back to the originator, and
76df8bae1dSRodney W. Grimes  * host table maintenance routines.
77df8bae1dSRodney W. Grimes  */
78df8bae1dSRodney W. Grimes 
79eddfbb76SRobert Watson VNET_DEFINE(struct icmpstat, icmpstat);
80eddfbb76SRobert Watson static VNET_DEFINE(int, icmpmaskrepl);
81eddfbb76SRobert Watson static VNET_DEFINE(u_int, icmpmaskfake);
82eddfbb76SRobert Watson static VNET_DEFINE(int, drop_redirect);
83eddfbb76SRobert Watson static VNET_DEFINE(int, log_redirect);
84eddfbb76SRobert Watson static VNET_DEFINE(int, icmplim);
85eddfbb76SRobert Watson static VNET_DEFINE(int, icmplim_output);
86eddfbb76SRobert Watson static VNET_DEFINE(char, reply_src[IFNAMSIZ]);
87eddfbb76SRobert Watson static VNET_DEFINE(int, icmp_rfi);
88eddfbb76SRobert Watson static VNET_DEFINE(int, icmp_quotelen);
89eddfbb76SRobert Watson static VNET_DEFINE(int, icmpbmcastecho);
9044e33a07SMarko Zec 
911e77c105SRobert Watson #define	V_icmpmaskrepl			VNET(icmpmaskrepl)
921e77c105SRobert Watson #define	V_icmpmaskfake			VNET(icmpmaskfake)
931e77c105SRobert Watson #define	V_drop_redirect			VNET(drop_redirect)
941e77c105SRobert Watson #define	V_log_redirect			VNET(log_redirect)
951e77c105SRobert Watson #define	V_icmplim			VNET(icmplim)
961e77c105SRobert Watson #define	V_icmplim_output		VNET(icmplim_output)
971e77c105SRobert Watson #define	V_reply_src			VNET(reply_src)
981e77c105SRobert Watson #define	V_icmp_rfi			VNET(icmp_rfi)
991e77c105SRobert Watson #define	V_icmp_quotelen			VNET(icmp_quotelen)
1001e77c105SRobert Watson #define	V_icmpbmcastecho		VNET(icmpbmcastecho)
1010312fbe9SPoul-Henning Kamp 
102eddfbb76SRobert Watson SYSCTL_VNET_STRUCT(_net_inet_icmp, ICMPCTL_STATS, stats, CTLFLAG_RW,
103eddfbb76SRobert Watson 	&VNET_NAME(icmpstat), icmpstat, "");
104eddfbb76SRobert Watson 
105eddfbb76SRobert Watson SYSCTL_VNET_INT(_net_inet_icmp, ICMPCTL_MASKREPL, maskrepl, CTLFLAG_RW,
106eddfbb76SRobert Watson 	&VNET_NAME(icmpmaskrepl), 0,
1078b615593SMarko Zec 	"Reply to ICMP Address Mask Request packets.");
10857842a38SMatthew N. Dodd 
109eddfbb76SRobert Watson SYSCTL_VNET_UINT(_net_inet_icmp, OID_AUTO, maskfake, CTLFLAG_RW,
110eddfbb76SRobert Watson 	&VNET_NAME(icmpmaskfake), 0,
111eddfbb76SRobert Watson 	"Fake reply to ICMP Address Mask Request packets.");
1120312fbe9SPoul-Henning Kamp 
113eddfbb76SRobert Watson SYSCTL_VNET_INT(_net_inet_icmp, OID_AUTO, drop_redirect, CTLFLAG_RW,
114eddfbb76SRobert Watson 	&VNET_NAME(drop_redirect), 0,
115eddfbb76SRobert Watson 	"Ignore ICMP redirects");
11618d3153eSDag-Erling Smørgrav 
117eddfbb76SRobert Watson SYSCTL_VNET_INT(_net_inet_icmp, OID_AUTO, log_redirect, CTLFLAG_RW,
118eddfbb76SRobert Watson 	&VNET_NAME(log_redirect), 0,
119eddfbb76SRobert Watson 	"Log ICMP redirects to the console");
1206c3b5f69SDag-Erling Smørgrav 
121eddfbb76SRobert Watson SYSCTL_VNET_INT(_net_inet_icmp, ICMPCTL_ICMPLIM, icmplim, CTLFLAG_RW,
122eddfbb76SRobert Watson 	&VNET_NAME(icmplim), 0,
123eddfbb76SRobert Watson 	"Maximum number of ICMP responses per second");
1245fce7fc4SMatthew Dillon 
125eddfbb76SRobert Watson SYSCTL_VNET_INT(_net_inet_icmp, OID_AUTO, icmplim_output, CTLFLAG_RW,
126eddfbb76SRobert Watson 	&VNET_NAME(icmplim_output), 0,
1278b615593SMarko Zec 	"Enable rate limiting of ICMP responses");
12851508de1SMatthew Dillon 
129eddfbb76SRobert Watson SYSCTL_VNET_STRING(_net_inet_icmp, OID_AUTO, reply_src, CTLFLAG_RW,
130eddfbb76SRobert Watson 	&VNET_NAME(reply_src), IFNAMSIZ,
1318b615593SMarko Zec 	"icmp reply source for non-local packets.");
132b74d89bbSAndre Oppermann 
133eddfbb76SRobert Watson SYSCTL_VNET_INT(_net_inet_icmp, OID_AUTO, reply_from_interface, CTLFLAG_RW,
134eddfbb76SRobert Watson 	&VNET_NAME(icmp_rfi), 0,
135eddfbb76SRobert Watson 	"ICMP reply from incoming interface for non-local packets");
136a0866c8dSAndre Oppermann 
137eddfbb76SRobert Watson SYSCTL_VNET_INT(_net_inet_icmp, OID_AUTO, quotelen, CTLFLAG_RW,
138eddfbb76SRobert Watson 	&VNET_NAME(icmp_quotelen), 0,
139eddfbb76SRobert Watson 	"Number of bytes from original packet to quote in ICMP reply");
140e875dfb8SAndre Oppermann 
1415fce7fc4SMatthew Dillon /*
1425fce7fc4SMatthew Dillon  * ICMP broadcast echo sysctl
1435fce7fc4SMatthew Dillon  */
1445fce7fc4SMatthew Dillon 
145eddfbb76SRobert Watson SYSCTL_VNET_INT(_net_inet_icmp, OID_AUTO, bmcastecho, CTLFLAG_RW,
146eddfbb76SRobert Watson 	&VNET_NAME(icmpbmcastecho), 0,
147eddfbb76SRobert Watson 	"");
1487022ea0aSGarrett Wollman 
14951508de1SMatthew Dillon 
150df8bae1dSRodney W. Grimes #ifdef ICMPPRINTFS
151df8bae1dSRodney W. Grimes int	icmpprintfs = 0;
152df8bae1dSRodney W. Grimes #endif
153df8bae1dSRodney W. Grimes 
1544d77a549SAlfred Perlstein static void	icmp_reflect(struct mbuf *);
15502c1c707SAndre Oppermann static void	icmp_send(struct mbuf *, struct mbuf *);
1560312fbe9SPoul-Henning Kamp 
157df8bae1dSRodney W. Grimes extern	struct protosw inetsw[];
158df8bae1dSRodney W. Grimes 
15944e33a07SMarko Zec void
16044e33a07SMarko Zec icmp_init(void)
16144e33a07SMarko Zec {
16244e33a07SMarko Zec 
16344e33a07SMarko Zec 	V_icmpmaskrepl = 0;
16444e33a07SMarko Zec 	V_icmpmaskfake = 0;
16544e33a07SMarko Zec 	V_drop_redirect = 0;
16644e33a07SMarko Zec 	V_log_redirect = 0;
16744e33a07SMarko Zec 	V_icmplim = 200;
16844e33a07SMarko Zec 	V_icmplim_output = 1;
16944e33a07SMarko Zec 	V_icmp_rfi = 0;
17044e33a07SMarko Zec 	V_icmp_quotelen = 8;
17144e33a07SMarko Zec 	V_icmpbmcastecho = 0;
17244e33a07SMarko Zec }
17344e33a07SMarko Zec 
174df8bae1dSRodney W. Grimes /*
175315e3e38SRobert Watson  * Kernel module interface for updating icmpstat.  The argument is an index
176315e3e38SRobert Watson  * into icmpstat treated as an array of u_long.  While this encodes the
177315e3e38SRobert Watson  * general layout of icmpstat into the caller, it doesn't encode its
178315e3e38SRobert Watson  * location, so that future changes to add, for example, per-CPU stats
179315e3e38SRobert Watson  * support won't cause binary compatibility problems for kernel modules.
180315e3e38SRobert Watson  */
181315e3e38SRobert Watson void
182315e3e38SRobert Watson kmod_icmpstat_inc(int statnum)
183315e3e38SRobert Watson {
184315e3e38SRobert Watson 
185315e3e38SRobert Watson 	(*((u_long *)&V_icmpstat + statnum))++;
186315e3e38SRobert Watson }
187315e3e38SRobert Watson 
188315e3e38SRobert Watson /*
189df8bae1dSRodney W. Grimes  * Generate an error packet of type error
190df8bae1dSRodney W. Grimes  * in response to bad packet ip.
191df8bae1dSRodney W. Grimes  */
192df8bae1dSRodney W. Grimes void
193d685b6eeSLuigi Rizzo icmp_error(struct mbuf *n, int type, int code, uint32_t dest, int mtu)
194df8bae1dSRodney W. Grimes {
195df8bae1dSRodney W. Grimes 	register struct ip *oip = mtod(n, struct ip *), *nip;
196e86ebebcSAndre Oppermann 	register unsigned oiphlen = oip->ip_hl << 2;
197df8bae1dSRodney W. Grimes 	register struct icmp *icp;
198df8bae1dSRodney W. Grimes 	register struct mbuf *m;
199e86ebebcSAndre Oppermann 	unsigned icmplen, icmpelen, nlen;
200df8bae1dSRodney W. Grimes 
201e86ebebcSAndre Oppermann 	KASSERT((u_int)type <= ICMP_MAXTYPE, ("%s: illegal ICMP type", __func__));
202df8bae1dSRodney W. Grimes #ifdef ICMPPRINTFS
203df8bae1dSRodney W. Grimes 	if (icmpprintfs)
204623ae52eSPoul-Henning Kamp 		printf("icmp_error(%p, %x, %d)\n", oip, type, code);
205df8bae1dSRodney W. Grimes #endif
206df8bae1dSRodney W. Grimes 	if (type != ICMP_REDIRECT)
207e27b0c87SRobert Watson 		ICMPSTAT_INC(icps_error);
208df8bae1dSRodney W. Grimes 	/*
209e86ebebcSAndre Oppermann 	 * Don't send error:
210e86ebebcSAndre Oppermann 	 *  if the original packet was encrypted.
211e86ebebcSAndre Oppermann 	 *  if not the first fragment of message.
212e86ebebcSAndre Oppermann 	 *  in response to a multicast or broadcast packet.
213e86ebebcSAndre Oppermann 	 *  if the old packet protocol was an ICMP error message.
214df8bae1dSRodney W. Grimes 	 */
215cad1917dSHajimu UMEMOTO 	if (n->m_flags & M_DECRYPTED)
216cad1917dSHajimu UMEMOTO 		goto freeit;
217df8bae1dSRodney W. Grimes 	if (oip->ip_off & ~(IP_MF|IP_DF))
218df8bae1dSRodney W. Grimes 		goto freeit;
219e86ebebcSAndre Oppermann 	if (n->m_flags & (M_BCAST|M_MCAST))
220e86ebebcSAndre Oppermann 		goto freeit;
221df8bae1dSRodney W. Grimes 	if (oip->ip_p == IPPROTO_ICMP && type != ICMP_REDIRECT &&
222e86ebebcSAndre Oppermann 	  n->m_len >= oiphlen + ICMP_MINLEN &&
223e86ebebcSAndre Oppermann 	  !ICMP_INFOTYPE(((struct icmp *)((caddr_t)oip + oiphlen))->icmp_type)) {
224e27b0c87SRobert Watson 		ICMPSTAT_INC(icps_oldicmp);
225df8bae1dSRodney W. Grimes 		goto freeit;
226df8bae1dSRodney W. Grimes 	}
227e86ebebcSAndre Oppermann 	/* Drop if IP header plus 8 bytes is not contignous in first mbuf. */
228e86ebebcSAndre Oppermann 	if (oiphlen + 8 > n->m_len)
229df8bae1dSRodney W. Grimes 		goto freeit;
230df8bae1dSRodney W. Grimes 	/*
231e86ebebcSAndre Oppermann 	 * Calculate length to quote from original packet and
232e86ebebcSAndre Oppermann 	 * prevent the ICMP mbuf from overflowing.
233e86ebebcSAndre Oppermann 	 * Unfortunatly this is non-trivial since ip_forward()
234e86ebebcSAndre Oppermann 	 * sends us truncated packets.
235df8bae1dSRodney W. Grimes 	 */
236e86ebebcSAndre Oppermann 	nlen = m_length(n, NULL);
237e86ebebcSAndre Oppermann 	if (oip->ip_p == IPPROTO_TCP) {
238e86ebebcSAndre Oppermann 		struct tcphdr *th;
239e86ebebcSAndre Oppermann 		int tcphlen;
240e86ebebcSAndre Oppermann 
241e86ebebcSAndre Oppermann 		if (oiphlen + sizeof(struct tcphdr) > n->m_len &&
242e86ebebcSAndre Oppermann 		    n->m_next == NULL)
243e86ebebcSAndre Oppermann 			goto stdreply;
244e86ebebcSAndre Oppermann 		if (n->m_len < oiphlen + sizeof(struct tcphdr) &&
245e86ebebcSAndre Oppermann 		    ((n = m_pullup(n, oiphlen + sizeof(struct tcphdr))) == NULL))
246e86ebebcSAndre Oppermann 			goto freeit;
247e86ebebcSAndre Oppermann 		th = (struct tcphdr *)((caddr_t)oip + oiphlen);
248e86ebebcSAndre Oppermann 		tcphlen = th->th_off << 2;
249e86ebebcSAndre Oppermann 		if (tcphlen < sizeof(struct tcphdr))
250e86ebebcSAndre Oppermann 			goto freeit;
251e86ebebcSAndre Oppermann 		if (oip->ip_len < oiphlen + tcphlen)
252e86ebebcSAndre Oppermann 			goto freeit;
253e86ebebcSAndre Oppermann 		if (oiphlen + tcphlen > n->m_len && n->m_next == NULL)
254e86ebebcSAndre Oppermann 			goto stdreply;
255e86ebebcSAndre Oppermann 		if (n->m_len < oiphlen + tcphlen &&
256e86ebebcSAndre Oppermann 		    ((n = m_pullup(n, oiphlen + tcphlen)) == NULL))
257e86ebebcSAndre Oppermann 			goto freeit;
2588b615593SMarko Zec 		icmpelen = max(tcphlen, min(V_icmp_quotelen, oip->ip_len - oiphlen));
259e86ebebcSAndre Oppermann 	} else
2608b615593SMarko Zec stdreply:	icmpelen = max(8, min(V_icmp_quotelen, oip->ip_len - oiphlen));
261e86ebebcSAndre Oppermann 
262e86ebebcSAndre Oppermann 	icmplen = min(oiphlen + icmpelen, nlen);
263e86ebebcSAndre Oppermann 	if (icmplen < sizeof(struct ip))
264e86ebebcSAndre Oppermann 		goto freeit;
265e86ebebcSAndre Oppermann 
266e86ebebcSAndre Oppermann 	if (MHLEN > sizeof(struct ip) + ICMP_MINLEN + icmplen)
267e86ebebcSAndre Oppermann 		m = m_gethdr(M_DONTWAIT, MT_DATA);
268e86ebebcSAndre Oppermann 	else
269e86ebebcSAndre Oppermann 		m = m_getcl(M_DONTWAIT, MT_DATA, M_PKTHDR);
270df8bae1dSRodney W. Grimes 	if (m == NULL)
271df8bae1dSRodney W. Grimes 		goto freeit;
2720070e096SRobert Watson #ifdef MAC
273a13e21f7SRobert Watson 	mac_netinet_icmp_reply(n, m);
2740070e096SRobert Watson #endif
275e86ebebcSAndre Oppermann 	icmplen = min(icmplen, M_TRAILINGSPACE(m) - sizeof(struct ip) - ICMP_MINLEN);
276e86ebebcSAndre Oppermann 	m_align(m, ICMP_MINLEN + icmplen);
277e86ebebcSAndre Oppermann 	m->m_len = ICMP_MINLEN + icmplen;
2786b773dffSAndre Oppermann 
2798b07e49aSJulian Elischer 	/* XXX MRT  make the outgoing packet use the same FIB
2808b07e49aSJulian Elischer 	 * that was associated with the incoming packet
2818b07e49aSJulian Elischer 	 */
2828b07e49aSJulian Elischer 	M_SETFIB(m, M_GETFIB(n));
283df8bae1dSRodney W. Grimes 	icp = mtod(m, struct icmp *);
284e27b0c87SRobert Watson 	ICMPSTAT_INC(icps_outhist[type]);
285df8bae1dSRodney W. Grimes 	icp->icmp_type = type;
286df8bae1dSRodney W. Grimes 	if (type == ICMP_REDIRECT)
287df8bae1dSRodney W. Grimes 		icp->icmp_gwaddr.s_addr = dest;
288df8bae1dSRodney W. Grimes 	else {
289df8bae1dSRodney W. Grimes 		icp->icmp_void = 0;
290df8bae1dSRodney W. Grimes 		/*
291df8bae1dSRodney W. Grimes 		 * The following assignments assume an overlay with the
292e86ebebcSAndre Oppermann 		 * just zeroed icmp_void field.
293df8bae1dSRodney W. Grimes 		 */
294df8bae1dSRodney W. Grimes 		if (type == ICMP_PARAMPROB) {
295df8bae1dSRodney W. Grimes 			icp->icmp_pptr = code;
296df8bae1dSRodney W. Grimes 			code = 0;
297df8bae1dSRodney W. Grimes 		} else if (type == ICMP_UNREACH &&
298c773494eSAndre Oppermann 			code == ICMP_UNREACH_NEEDFRAG && mtu) {
299c773494eSAndre Oppermann 			icp->icmp_nextmtu = htons(mtu);
300df8bae1dSRodney W. Grimes 		}
301df8bae1dSRodney W. Grimes 	}
302df8bae1dSRodney W. Grimes 	icp->icmp_code = code;
30304287599SRuslan Ermilov 
30404287599SRuslan Ermilov 	/*
305e86ebebcSAndre Oppermann 	 * Copy the quotation into ICMP message and
306e86ebebcSAndre Oppermann 	 * convert quoted IP header back to network representation.
30704287599SRuslan Ermilov 	 */
308e86ebebcSAndre Oppermann 	m_copydata(n, 0, icmplen, (caddr_t)&icp->icmp_ip);
309e86ebebcSAndre Oppermann 	nip = &icp->icmp_ip;
310fd8e4ebcSMike Barcroft 	nip->ip_len = htons(nip->ip_len);
311fd8e4ebcSMike Barcroft 	nip->ip_off = htons(nip->ip_off);
312df8bae1dSRodney W. Grimes 
313df8bae1dSRodney W. Grimes 	/*
314e86ebebcSAndre Oppermann 	 * Set up ICMP message mbuf and copy old IP header (without options
315e86ebebcSAndre Oppermann 	 * in front of ICMP message.
316c550f220SMax Laier 	 * If the original mbuf was meant to bypass the firewall, the error
317c550f220SMax Laier 	 * reply should bypass as well.
318c550f220SMax Laier 	 */
319c550f220SMax Laier 	m->m_flags |= n->m_flags & M_SKIP_FIREWALL;
320df8bae1dSRodney W. Grimes 	m->m_data -= sizeof(struct ip);
321df8bae1dSRodney W. Grimes 	m->m_len += sizeof(struct ip);
322df8bae1dSRodney W. Grimes 	m->m_pkthdr.len = m->m_len;
323df8bae1dSRodney W. Grimes 	m->m_pkthdr.rcvif = n->m_pkthdr.rcvif;
324df8bae1dSRodney W. Grimes 	nip = mtod(m, struct ip *);
325df8bae1dSRodney W. Grimes 	bcopy((caddr_t)oip, (caddr_t)nip, sizeof(struct ip));
326df8bae1dSRodney W. Grimes 	nip->ip_len = m->m_len;
32753be11f6SPoul-Henning Kamp 	nip->ip_v = IPVERSION;
32853be11f6SPoul-Henning Kamp 	nip->ip_hl = 5;
329df8bae1dSRodney W. Grimes 	nip->ip_p = IPPROTO_ICMP;
330df8bae1dSRodney W. Grimes 	nip->ip_tos = 0;
331df8bae1dSRodney W. Grimes 	icmp_reflect(m);
332df8bae1dSRodney W. Grimes 
333df8bae1dSRodney W. Grimes freeit:
334df8bae1dSRodney W. Grimes 	m_freem(n);
335df8bae1dSRodney W. Grimes }
336df8bae1dSRodney W. Grimes 
337df8bae1dSRodney W. Grimes /*
338df8bae1dSRodney W. Grimes  * Process a received ICMP message.
339df8bae1dSRodney W. Grimes  */
340df8bae1dSRodney W. Grimes void
341f2565d68SRobert Watson icmp_input(struct mbuf *m, int off)
342df8bae1dSRodney W. Grimes {
34316d6c90fSAndre Oppermann 	struct icmp *icp;
344df8bae1dSRodney W. Grimes 	struct in_ifaddr *ia;
34516d6c90fSAndre Oppermann 	struct ip *ip = mtod(m, struct ip *);
34616d6c90fSAndre Oppermann 	struct sockaddr_in icmpsrc, icmpdst, icmpgw;
34716d6c90fSAndre Oppermann 	int hlen = off;
34816d6c90fSAndre Oppermann 	int icmplen = ip->ip_len;
34916d6c90fSAndre Oppermann 	int i, code;
3504d77a549SAlfred Perlstein 	void (*ctlfunc)(int, struct sockaddr *, void *);
3518b07e49aSJulian Elischer 	int fibnum;
352df8bae1dSRodney W. Grimes 
353df8bae1dSRodney W. Grimes 	/*
354df8bae1dSRodney W. Grimes 	 * Locate icmp structure in mbuf, and check
355df8bae1dSRodney W. Grimes 	 * that not corrupted and of at least minimum length.
356df8bae1dSRodney W. Grimes 	 */
357df8bae1dSRodney W. Grimes #ifdef ICMPPRINTFS
3582b758395SGarrett Wollman 	if (icmpprintfs) {
3592b758395SGarrett Wollman 		char buf[4 * sizeof "123"];
3602b758395SGarrett Wollman 		strcpy(buf, inet_ntoa(ip->ip_src));
3612b758395SGarrett Wollman 		printf("icmp_input from %s to %s, len %d\n",
3622b758395SGarrett Wollman 		       buf, inet_ntoa(ip->ip_dst), icmplen);
3632b758395SGarrett Wollman 	}
364df8bae1dSRodney W. Grimes #endif
365df8bae1dSRodney W. Grimes 	if (icmplen < ICMP_MINLEN) {
366e27b0c87SRobert Watson 		ICMPSTAT_INC(icps_tooshort);
367df8bae1dSRodney W. Grimes 		goto freeit;
368df8bae1dSRodney W. Grimes 	}
369df8bae1dSRodney W. Grimes 	i = hlen + min(icmplen, ICMP_ADVLENMIN);
370df8bae1dSRodney W. Grimes 	if (m->m_len < i && (m = m_pullup(m, i)) == 0)  {
371e27b0c87SRobert Watson 		ICMPSTAT_INC(icps_tooshort);
372df8bae1dSRodney W. Grimes 		return;
373df8bae1dSRodney W. Grimes 	}
374df8bae1dSRodney W. Grimes 	ip = mtod(m, struct ip *);
375df8bae1dSRodney W. Grimes 	m->m_len -= hlen;
376df8bae1dSRodney W. Grimes 	m->m_data += hlen;
377df8bae1dSRodney W. Grimes 	icp = mtod(m, struct icmp *);
378df8bae1dSRodney W. Grimes 	if (in_cksum(m, icmplen)) {
379e27b0c87SRobert Watson 		ICMPSTAT_INC(icps_checksum);
380df8bae1dSRodney W. Grimes 		goto freeit;
381df8bae1dSRodney W. Grimes 	}
382df8bae1dSRodney W. Grimes 	m->m_len += hlen;
383df8bae1dSRodney W. Grimes 	m->m_data -= hlen;
384df8bae1dSRodney W. Grimes 
3856a800098SYoshinobu Inoue 	if (m->m_pkthdr.rcvif && m->m_pkthdr.rcvif->if_type == IFT_FAITH) {
3866a800098SYoshinobu Inoue 		/*
3876a800098SYoshinobu Inoue 		 * Deliver very specific ICMP type only.
3886a800098SYoshinobu Inoue 		 */
3896a800098SYoshinobu Inoue 		switch (icp->icmp_type) {
3906a800098SYoshinobu Inoue 		case ICMP_UNREACH:
3916a800098SYoshinobu Inoue 		case ICMP_TIMXCEED:
3926a800098SYoshinobu Inoue 			break;
3936a800098SYoshinobu Inoue 		default:
3946a800098SYoshinobu Inoue 			goto freeit;
3956a800098SYoshinobu Inoue 		}
3966a800098SYoshinobu Inoue 	}
3976a800098SYoshinobu Inoue 
398df8bae1dSRodney W. Grimes #ifdef ICMPPRINTFS
399df8bae1dSRodney W. Grimes 	if (icmpprintfs)
400df8bae1dSRodney W. Grimes 		printf("icmp_input, type %d code %d\n", icp->icmp_type,
401df8bae1dSRodney W. Grimes 		    icp->icmp_code);
402df8bae1dSRodney W. Grimes #endif
4035b7ee6edSGarrett Wollman 
4045b7ee6edSGarrett Wollman 	/*
4055b7ee6edSGarrett Wollman 	 * Message type specific processing.
4065b7ee6edSGarrett Wollman 	 */
407df8bae1dSRodney W. Grimes 	if (icp->icmp_type > ICMP_MAXTYPE)
408df8bae1dSRodney W. Grimes 		goto raw;
40916d6c90fSAndre Oppermann 
41016d6c90fSAndre Oppermann 	/* Initialize */
41116d6c90fSAndre Oppermann 	bzero(&icmpsrc, sizeof(icmpsrc));
41216d6c90fSAndre Oppermann 	icmpsrc.sin_len = sizeof(struct sockaddr_in);
41316d6c90fSAndre Oppermann 	icmpsrc.sin_family = AF_INET;
41416d6c90fSAndre Oppermann 	bzero(&icmpdst, sizeof(icmpdst));
41516d6c90fSAndre Oppermann 	icmpdst.sin_len = sizeof(struct sockaddr_in);
41616d6c90fSAndre Oppermann 	icmpdst.sin_family = AF_INET;
41716d6c90fSAndre Oppermann 	bzero(&icmpgw, sizeof(icmpgw));
41816d6c90fSAndre Oppermann 	icmpgw.sin_len = sizeof(struct sockaddr_in);
41916d6c90fSAndre Oppermann 	icmpgw.sin_family = AF_INET;
42016d6c90fSAndre Oppermann 
421e27b0c87SRobert Watson 	ICMPSTAT_INC(icps_inhist[icp->icmp_type]);
422df8bae1dSRodney W. Grimes 	code = icp->icmp_code;
423df8bae1dSRodney W. Grimes 	switch (icp->icmp_type) {
424df8bae1dSRodney W. Grimes 
425df8bae1dSRodney W. Grimes 	case ICMP_UNREACH:
426df8bae1dSRodney W. Grimes 		switch (code) {
427df8bae1dSRodney W. Grimes 			case ICMP_UNREACH_NET:
428df8bae1dSRodney W. Grimes 			case ICMP_UNREACH_HOST:
429df8bae1dSRodney W. Grimes 			case ICMP_UNREACH_SRCFAIL:
430e4bb5b05SJonathan Lemon 			case ICMP_UNREACH_NET_UNKNOWN:
431e4bb5b05SJonathan Lemon 			case ICMP_UNREACH_HOST_UNKNOWN:
432e4bb5b05SJonathan Lemon 			case ICMP_UNREACH_ISOLATED:
433e4bb5b05SJonathan Lemon 			case ICMP_UNREACH_TOSNET:
434e4bb5b05SJonathan Lemon 			case ICMP_UNREACH_TOSHOST:
435e4bb5b05SJonathan Lemon 			case ICMP_UNREACH_HOST_PRECEDENCE:
436e4bb5b05SJonathan Lemon 			case ICMP_UNREACH_PRECEDENCE_CUTOFF:
437e4bb5b05SJonathan Lemon 				code = PRC_UNREACH_NET;
438df8bae1dSRodney W. Grimes 				break;
439df8bae1dSRodney W. Grimes 
440df8bae1dSRodney W. Grimes 			case ICMP_UNREACH_NEEDFRAG:
441df8bae1dSRodney W. Grimes 				code = PRC_MSGSIZE;
442df8bae1dSRodney W. Grimes 				break;
443df8bae1dSRodney W. Grimes 
444e4bb5b05SJonathan Lemon 			/*
445e4bb5b05SJonathan Lemon 			 * RFC 1122, Sections 3.2.2.1 and 4.2.3.9.
446e4bb5b05SJonathan Lemon 			 * Treat subcodes 2,3 as immediate RST
447e4bb5b05SJonathan Lemon 			 */
448e4bb5b05SJonathan Lemon 			case ICMP_UNREACH_PROTOCOL:
449e4bb5b05SJonathan Lemon 			case ICMP_UNREACH_PORT:
450b77d155dSJesper Skriver 				code = PRC_UNREACH_PORT;
451b11d7a4aSPoul-Henning Kamp 				break;
45290fcbbd6SPoul-Henning Kamp 
45390fcbbd6SPoul-Henning Kamp 			case ICMP_UNREACH_NET_PROHIB:
45490fcbbd6SPoul-Henning Kamp 			case ICMP_UNREACH_HOST_PROHIB:
4559c4b2574SPaul Traina 			case ICMP_UNREACH_FILTER_PROHIB:
45690fcbbd6SPoul-Henning Kamp 				code = PRC_UNREACH_ADMIN_PROHIB;
457b11d7a4aSPoul-Henning Kamp 				break;
458b11d7a4aSPoul-Henning Kamp 
459df8bae1dSRodney W. Grimes 			default:
460df8bae1dSRodney W. Grimes 				goto badcode;
461df8bae1dSRodney W. Grimes 		}
462df8bae1dSRodney W. Grimes 		goto deliver;
463df8bae1dSRodney W. Grimes 
464df8bae1dSRodney W. Grimes 	case ICMP_TIMXCEED:
465df8bae1dSRodney W. Grimes 		if (code > 1)
466df8bae1dSRodney W. Grimes 			goto badcode;
467df8bae1dSRodney W. Grimes 		code += PRC_TIMXCEED_INTRANS;
468df8bae1dSRodney W. Grimes 		goto deliver;
469df8bae1dSRodney W. Grimes 
470df8bae1dSRodney W. Grimes 	case ICMP_PARAMPROB:
471df8bae1dSRodney W. Grimes 		if (code > 1)
472df8bae1dSRodney W. Grimes 			goto badcode;
473df8bae1dSRodney W. Grimes 		code = PRC_PARAMPROB;
474df8bae1dSRodney W. Grimes 		goto deliver;
475df8bae1dSRodney W. Grimes 
476df8bae1dSRodney W. Grimes 	case ICMP_SOURCEQUENCH:
477df8bae1dSRodney W. Grimes 		if (code)
478df8bae1dSRodney W. Grimes 			goto badcode;
479df8bae1dSRodney W. Grimes 		code = PRC_QUENCH;
480df8bae1dSRodney W. Grimes 	deliver:
481df8bae1dSRodney W. Grimes 		/*
482df8bae1dSRodney W. Grimes 		 * Problem with datagram; advise higher level routines.
483df8bae1dSRodney W. Grimes 		 */
484df8bae1dSRodney W. Grimes 		if (icmplen < ICMP_ADVLENMIN || icmplen < ICMP_ADVLEN(icp) ||
48553be11f6SPoul-Henning Kamp 		    icp->icmp_ip.ip_hl < (sizeof(struct ip) >> 2)) {
486e27b0c87SRobert Watson 			ICMPSTAT_INC(icps_badlen);
487df8bae1dSRodney W. Grimes 			goto freeit;
488df8bae1dSRodney W. Grimes 		}
489fd8e4ebcSMike Barcroft 		icp->icmp_ip.ip_len = ntohs(icp->icmp_ip.ip_len);
4905b7ee6edSGarrett Wollman 		/* Discard ICMP's in response to multicast packets */
4915b7ee6edSGarrett Wollman 		if (IN_MULTICAST(ntohl(icp->icmp_ip.ip_dst.s_addr)))
4925b7ee6edSGarrett Wollman 			goto badcode;
493df8bae1dSRodney W. Grimes #ifdef ICMPPRINTFS
494df8bae1dSRodney W. Grimes 		if (icmpprintfs)
495df8bae1dSRodney W. Grimes 			printf("deliver to protocol %d\n", icp->icmp_ip.ip_p);
496df8bae1dSRodney W. Grimes #endif
497df8bae1dSRodney W. Grimes 		icmpsrc.sin_addr = icp->icmp_ip.ip_dst;
4986a800098SYoshinobu Inoue 		/*
4996a800098SYoshinobu Inoue 		 * XXX if the packet contains [IPv4 AH TCP], we can't make a
5006a800098SYoshinobu Inoue 		 * notification to TCP layer.
5016a800098SYoshinobu Inoue 		 */
502623ae52eSPoul-Henning Kamp 		ctlfunc = inetsw[ip_protox[icp->icmp_ip.ip_p]].pr_ctlinput;
503623ae52eSPoul-Henning Kamp 		if (ctlfunc)
504df8bae1dSRodney W. Grimes 			(*ctlfunc)(code, (struct sockaddr *)&icmpsrc,
505b62d102cSBruce Evans 				   (void *)&icp->icmp_ip);
506df8bae1dSRodney W. Grimes 		break;
507df8bae1dSRodney W. Grimes 
508df8bae1dSRodney W. Grimes 	badcode:
509e27b0c87SRobert Watson 		ICMPSTAT_INC(icps_badcode);
510df8bae1dSRodney W. Grimes 		break;
511df8bae1dSRodney W. Grimes 
512df8bae1dSRodney W. Grimes 	case ICMP_ECHO:
5138b615593SMarko Zec 		if (!V_icmpbmcastecho
514d311884fSDavid Greenman 		    && (m->m_flags & (M_MCAST | M_BCAST)) != 0) {
515e27b0c87SRobert Watson 			ICMPSTAT_INC(icps_bmcastecho);
5167022ea0aSGarrett Wollman 			break;
5177022ea0aSGarrett Wollman 		}
518df8bae1dSRodney W. Grimes 		icp->icmp_type = ICMP_ECHOREPLY;
519a57815efSBosko Milekic 		if (badport_bandlim(BANDLIM_ICMP_ECHO) < 0)
52009f81a46SBosko Milekic 			goto freeit;
52109f81a46SBosko Milekic 		else
522df8bae1dSRodney W. Grimes 			goto reflect;
523df8bae1dSRodney W. Grimes 
524df8bae1dSRodney W. Grimes 	case ICMP_TSTAMP:
5258b615593SMarko Zec 		if (!V_icmpbmcastecho
526d311884fSDavid Greenman 		    && (m->m_flags & (M_MCAST | M_BCAST)) != 0) {
527e27b0c87SRobert Watson 			ICMPSTAT_INC(icps_bmcasttstamp);
528fe0fb8abSGarrett Wollman 			break;
529fe0fb8abSGarrett Wollman 		}
530df8bae1dSRodney W. Grimes 		if (icmplen < ICMP_TSLEN) {
531e27b0c87SRobert Watson 			ICMPSTAT_INC(icps_badlen);
532df8bae1dSRodney W. Grimes 			break;
533df8bae1dSRodney W. Grimes 		}
534df8bae1dSRodney W. Grimes 		icp->icmp_type = ICMP_TSTAMPREPLY;
535df8bae1dSRodney W. Grimes 		icp->icmp_rtime = iptime();
536df8bae1dSRodney W. Grimes 		icp->icmp_ttime = icp->icmp_rtime;	/* bogus, do later! */
537a57815efSBosko Milekic 		if (badport_bandlim(BANDLIM_ICMP_TSTAMP) < 0)
53809f81a46SBosko Milekic 			goto freeit;
53909f81a46SBosko Milekic 		else
540df8bae1dSRodney W. Grimes 			goto reflect;
541df8bae1dSRodney W. Grimes 
542df8bae1dSRodney W. Grimes 	case ICMP_MASKREQ:
5438b615593SMarko Zec 		if (V_icmpmaskrepl == 0)
544df8bae1dSRodney W. Grimes 			break;
545df8bae1dSRodney W. Grimes 		/*
546df8bae1dSRodney W. Grimes 		 * We are not able to respond with all ones broadcast
547df8bae1dSRodney W. Grimes 		 * unless we receive it over a point-to-point interface.
548df8bae1dSRodney W. Grimes 		 */
549df8bae1dSRodney W. Grimes 		if (icmplen < ICMP_MASKLEN)
550df8bae1dSRodney W. Grimes 			break;
551df8bae1dSRodney W. Grimes 		switch (ip->ip_dst.s_addr) {
552df8bae1dSRodney W. Grimes 
553df8bae1dSRodney W. Grimes 		case INADDR_BROADCAST:
554df8bae1dSRodney W. Grimes 		case INADDR_ANY:
555df8bae1dSRodney W. Grimes 			icmpdst.sin_addr = ip->ip_src;
556df8bae1dSRodney W. Grimes 			break;
557df8bae1dSRodney W. Grimes 
558df8bae1dSRodney W. Grimes 		default:
559df8bae1dSRodney W. Grimes 			icmpdst.sin_addr = ip->ip_dst;
560df8bae1dSRodney W. Grimes 		}
561df8bae1dSRodney W. Grimes 		ia = (struct in_ifaddr *)ifaof_ifpforaddr(
562df8bae1dSRodney W. Grimes 			    (struct sockaddr *)&icmpdst, m->m_pkthdr.rcvif);
5638c0fec80SRobert Watson 		if (ia == NULL)
564df8bae1dSRodney W. Grimes 			break;
5658c0fec80SRobert Watson 		if (ia->ia_ifp == NULL) {
5668c0fec80SRobert Watson 			ifa_free(&ia->ia_ifa);
5677e6f7714SPoul-Henning Kamp 			break;
5688c0fec80SRobert Watson 		}
569df8bae1dSRodney W. Grimes 		icp->icmp_type = ICMP_MASKREPLY;
5708b615593SMarko Zec 		if (V_icmpmaskfake == 0)
571df8bae1dSRodney W. Grimes 			icp->icmp_mask = ia->ia_sockmask.sin_addr.s_addr;
57257842a38SMatthew N. Dodd 		else
5738b615593SMarko Zec 			icp->icmp_mask = V_icmpmaskfake;
574df8bae1dSRodney W. Grimes 		if (ip->ip_src.s_addr == 0) {
575df8bae1dSRodney W. Grimes 			if (ia->ia_ifp->if_flags & IFF_BROADCAST)
576df8bae1dSRodney W. Grimes 			    ip->ip_src = satosin(&ia->ia_broadaddr)->sin_addr;
577df8bae1dSRodney W. Grimes 			else if (ia->ia_ifp->if_flags & IFF_POINTOPOINT)
578df8bae1dSRodney W. Grimes 			    ip->ip_src = satosin(&ia->ia_dstaddr)->sin_addr;
579df8bae1dSRodney W. Grimes 		}
5808c0fec80SRobert Watson 		ifa_free(&ia->ia_ifa);
581df8bae1dSRodney W. Grimes reflect:
582df8bae1dSRodney W. Grimes 		ip->ip_len += hlen;	/* since ip_input deducts this */
583e27b0c87SRobert Watson 		ICMPSTAT_INC(icps_reflect);
584e27b0c87SRobert Watson 		ICMPSTAT_INC(icps_outhist[icp->icmp_type]);
585df8bae1dSRodney W. Grimes 		icmp_reflect(m);
586df8bae1dSRodney W. Grimes 		return;
587df8bae1dSRodney W. Grimes 
588df8bae1dSRodney W. Grimes 	case ICMP_REDIRECT:
5898b615593SMarko Zec 		if (V_log_redirect) {
59018d3153eSDag-Erling Smørgrav 			u_long src, dst, gw;
59118d3153eSDag-Erling Smørgrav 
59218d3153eSDag-Erling Smørgrav 			src = ntohl(ip->ip_src.s_addr);
59318d3153eSDag-Erling Smørgrav 			dst = ntohl(icp->icmp_ip.ip_dst.s_addr);
59418d3153eSDag-Erling Smørgrav 			gw = ntohl(icp->icmp_gwaddr.s_addr);
59518d3153eSDag-Erling Smørgrav 			printf("icmp redirect from %d.%d.%d.%d: "
59618d3153eSDag-Erling Smørgrav 			       "%d.%d.%d.%d => %d.%d.%d.%d\n",
59718d3153eSDag-Erling Smørgrav 			       (int)(src >> 24), (int)((src >> 16) & 0xff),
59818d3153eSDag-Erling Smørgrav 			       (int)((src >> 8) & 0xff), (int)(src & 0xff),
59918d3153eSDag-Erling Smørgrav 			       (int)(dst >> 24), (int)((dst >> 16) & 0xff),
60018d3153eSDag-Erling Smørgrav 			       (int)((dst >> 8) & 0xff), (int)(dst & 0xff),
60118d3153eSDag-Erling Smørgrav 			       (int)(gw >> 24), (int)((gw >> 16) & 0xff),
60218d3153eSDag-Erling Smørgrav 			       (int)((gw >> 8) & 0xff), (int)(gw & 0xff));
60318d3153eSDag-Erling Smørgrav 		}
60487c3bd27SAndre Oppermann 		/*
60587c3bd27SAndre Oppermann 		 * RFC1812 says we must ignore ICMP redirects if we
60687c3bd27SAndre Oppermann 		 * are acting as router.
60787c3bd27SAndre Oppermann 		 */
6088b615593SMarko Zec 		if (V_drop_redirect || V_ipforwarding)
60918d3153eSDag-Erling Smørgrav 			break;
610df8bae1dSRodney W. Grimes 		if (code > 3)
611df8bae1dSRodney W. Grimes 			goto badcode;
612df8bae1dSRodney W. Grimes 		if (icmplen < ICMP_ADVLENMIN || icmplen < ICMP_ADVLEN(icp) ||
61353be11f6SPoul-Henning Kamp 		    icp->icmp_ip.ip_hl < (sizeof(struct ip) >> 2)) {
614e27b0c87SRobert Watson 			ICMPSTAT_INC(icps_badlen);
615df8bae1dSRodney W. Grimes 			break;
616df8bae1dSRodney W. Grimes 		}
617df8bae1dSRodney W. Grimes 		/*
618df8bae1dSRodney W. Grimes 		 * Short circuit routing redirects to force
619df8bae1dSRodney W. Grimes 		 * immediate change in the kernel's routing
620df8bae1dSRodney W. Grimes 		 * tables.  The message is also handed to anyone
621df8bae1dSRodney W. Grimes 		 * listening on a raw socket (e.g. the routing
622df8bae1dSRodney W. Grimes 		 * daemon for use in updating its tables).
623df8bae1dSRodney W. Grimes 		 */
624df8bae1dSRodney W. Grimes 		icmpgw.sin_addr = ip->ip_src;
625df8bae1dSRodney W. Grimes 		icmpdst.sin_addr = icp->icmp_gwaddr;
626df8bae1dSRodney W. Grimes #ifdef	ICMPPRINTFS
6272b758395SGarrett Wollman 		if (icmpprintfs) {
6282b758395SGarrett Wollman 			char buf[4 * sizeof "123"];
6292b758395SGarrett Wollman 			strcpy(buf, inet_ntoa(icp->icmp_ip.ip_dst));
6302b758395SGarrett Wollman 
6312b758395SGarrett Wollman 			printf("redirect dst %s to %s\n",
6322b758395SGarrett Wollman 			       buf, inet_ntoa(icp->icmp_gwaddr));
6332b758395SGarrett Wollman 		}
634df8bae1dSRodney W. Grimes #endif
635df8bae1dSRodney W. Grimes 		icmpsrc.sin_addr = icp->icmp_ip.ip_dst;
6368b07e49aSJulian Elischer 		for ( fibnum = 0; fibnum < rt_numfibs; fibnum++) {
6378b07e49aSJulian Elischer 			in_rtredirect((struct sockaddr *)&icmpsrc,
638df8bae1dSRodney W. Grimes 			  (struct sockaddr *)&icmpdst,
639df8bae1dSRodney W. Grimes 			  (struct sockaddr *)0, RTF_GATEWAY | RTF_HOST,
6408b07e49aSJulian Elischer 			  (struct sockaddr *)&icmpgw, fibnum);
6418b07e49aSJulian Elischer 		}
642df8bae1dSRodney W. Grimes 		pfctlinput(PRC_REDIRECT_HOST, (struct sockaddr *)&icmpsrc);
643b2630c29SGeorge V. Neville-Neil #ifdef IPSEC
6446a800098SYoshinobu Inoue 		key_sa_routechange((struct sockaddr *)&icmpsrc);
6456a800098SYoshinobu Inoue #endif
646df8bae1dSRodney W. Grimes 		break;
647df8bae1dSRodney W. Grimes 
648df8bae1dSRodney W. Grimes 	/*
649df8bae1dSRodney W. Grimes 	 * No kernel processing for the following;
650df8bae1dSRodney W. Grimes 	 * just fall through to send to raw listener.
651df8bae1dSRodney W. Grimes 	 */
652df8bae1dSRodney W. Grimes 	case ICMP_ECHOREPLY:
653df8bae1dSRodney W. Grimes 	case ICMP_ROUTERADVERT:
654df8bae1dSRodney W. Grimes 	case ICMP_ROUTERSOLICIT:
655df8bae1dSRodney W. Grimes 	case ICMP_TSTAMPREPLY:
656df8bae1dSRodney W. Grimes 	case ICMP_IREQREPLY:
657df8bae1dSRodney W. Grimes 	case ICMP_MASKREPLY:
658df8bae1dSRodney W. Grimes 	default:
659df8bae1dSRodney W. Grimes 		break;
660df8bae1dSRodney W. Grimes 	}
661df8bae1dSRodney W. Grimes 
662df8bae1dSRodney W. Grimes raw:
663f0ffb944SJulian Elischer 	rip_input(m, off);
664df8bae1dSRodney W. Grimes 	return;
665df8bae1dSRodney W. Grimes 
666df8bae1dSRodney W. Grimes freeit:
667df8bae1dSRodney W. Grimes 	m_freem(m);
668df8bae1dSRodney W. Grimes }
669df8bae1dSRodney W. Grimes 
670df8bae1dSRodney W. Grimes /*
671df8bae1dSRodney W. Grimes  * Reflect the ip packet back to the source
672df8bae1dSRodney W. Grimes  */
6730312fbe9SPoul-Henning Kamp static void
674f2565d68SRobert Watson icmp_reflect(struct mbuf *m)
675df8bae1dSRodney W. Grimes {
676ca925d9cSJonathan Lemon 	struct ip *ip = mtod(m, struct ip *);
677ca925d9cSJonathan Lemon 	struct ifaddr *ifa;
67833c4f96dSRobert Watson 	struct ifnet *ifp;
679ca925d9cSJonathan Lemon 	struct in_ifaddr *ia;
680df8bae1dSRodney W. Grimes 	struct in_addr t;
681b5e8ce9fSBruce Evans 	struct mbuf *opts = 0;
68253be11f6SPoul-Henning Kamp 	int optlen = (ip->ip_hl << 2) - sizeof(struct ip);
683df8bae1dSRodney W. Grimes 
6846b9ff6b7SGeorge V. Neville-Neil 	if (IN_MULTICAST(ntohl(ip->ip_src.s_addr)) ||
6856b9ff6b7SGeorge V. Neville-Neil 	    IN_EXPERIMENTAL(ntohl(ip->ip_src.s_addr)) ||
6866b9ff6b7SGeorge V. Neville-Neil 	    IN_ZERONET(ntohl(ip->ip_src.s_addr)) ) {
687df8bae1dSRodney W. Grimes 		m_freem(m);	/* Bad return address */
688e27b0c87SRobert Watson 		ICMPSTAT_INC(icps_badaddr);
689df8bae1dSRodney W. Grimes 		goto done;	/* Ip_output() will check for broadcast */
690df8bae1dSRodney W. Grimes 	}
6916b9ff6b7SGeorge V. Neville-Neil 
692df8bae1dSRodney W. Grimes 	t = ip->ip_dst;
693df8bae1dSRodney W. Grimes 	ip->ip_dst = ip->ip_src;
6941488eac8SAndre Oppermann 
695df8bae1dSRodney W. Grimes 	/*
6961488eac8SAndre Oppermann 	 * Source selection for ICMP replies:
6971488eac8SAndre Oppermann 	 *
6981488eac8SAndre Oppermann 	 * If the incoming packet was addressed directly to one of our
6991488eac8SAndre Oppermann 	 * own addresses, use dst as the src for the reply.
700df8bae1dSRodney W. Grimes 	 */
7012d9cfabaSRobert Watson 	IN_IFADDR_RLOCK();
70233c4f96dSRobert Watson 	LIST_FOREACH(ia, INADDR_HASH(t.s_addr), ia_hash) {
70333c4f96dSRobert Watson 		if (t.s_addr == IA_SIN(ia)->sin_addr.s_addr) {
70433c4f96dSRobert Watson 			t = IA_SIN(ia)->sin_addr;
7052d9cfabaSRobert Watson 			IN_IFADDR_RUNLOCK();
706ca925d9cSJonathan Lemon 			goto match;
70733c4f96dSRobert Watson 		}
70833c4f96dSRobert Watson 	}
7092d9cfabaSRobert Watson 	IN_IFADDR_RUNLOCK();
7102d9cfabaSRobert Watson 
7111488eac8SAndre Oppermann 	/*
7121488eac8SAndre Oppermann 	 * If the incoming packet was addressed to one of our broadcast
7131488eac8SAndre Oppermann 	 * addresses, use the first non-broadcast address which corresponds
7141488eac8SAndre Oppermann 	 * to the incoming interface.
7151488eac8SAndre Oppermann 	 */
71633c4f96dSRobert Watson 	ifp = m->m_pkthdr.rcvif;
71733c4f96dSRobert Watson 	if (ifp != NULL && ifp->if_flags & IFF_BROADCAST) {
71833c4f96dSRobert Watson 		IF_ADDR_LOCK(ifp);
71933c4f96dSRobert Watson 		TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
720ca925d9cSJonathan Lemon 			if (ifa->ifa_addr->sa_family != AF_INET)
721ca925d9cSJonathan Lemon 				continue;
722ca925d9cSJonathan Lemon 			ia = ifatoia(ifa);
723ca925d9cSJonathan Lemon 			if (satosin(&ia->ia_broadaddr)->sin_addr.s_addr ==
72433c4f96dSRobert Watson 			    t.s_addr) {
72533c4f96dSRobert Watson 				t = IA_SIN(ia)->sin_addr;
72633c4f96dSRobert Watson 				IF_ADDR_UNLOCK(ifp);
727ca925d9cSJonathan Lemon 				goto match;
728df8bae1dSRodney W. Grimes 			}
729ca925d9cSJonathan Lemon 		}
73033c4f96dSRobert Watson 		IF_ADDR_UNLOCK(ifp);
73133c4f96dSRobert Watson 	}
7321488eac8SAndre Oppermann 	/*
733a0866c8dSAndre Oppermann 	 * If the packet was transiting through us, use the address of
734a0866c8dSAndre Oppermann 	 * the interface the packet came through in.  If that interface
735a0866c8dSAndre Oppermann 	 * doesn't have a suitable IP address, the normal selection
736a0866c8dSAndre Oppermann 	 * criteria apply.
737a0866c8dSAndre Oppermann 	 */
73833c4f96dSRobert Watson 	if (V_icmp_rfi && ifp != NULL) {
73933c4f96dSRobert Watson 		IF_ADDR_LOCK(ifp);
74033c4f96dSRobert Watson 		TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
741a0866c8dSAndre Oppermann 			if (ifa->ifa_addr->sa_family != AF_INET)
742a0866c8dSAndre Oppermann 				continue;
743a0866c8dSAndre Oppermann 			ia = ifatoia(ifa);
74433c4f96dSRobert Watson 			t = IA_SIN(ia)->sin_addr;
74533c4f96dSRobert Watson 			IF_ADDR_UNLOCK(ifp);
746a0866c8dSAndre Oppermann 			goto match;
747a0866c8dSAndre Oppermann 		}
74833c4f96dSRobert Watson 		IF_ADDR_UNLOCK(ifp);
749a0866c8dSAndre Oppermann 	}
750a0866c8dSAndre Oppermann 	/*
751b74d89bbSAndre Oppermann 	 * If the incoming packet was not addressed directly to us, use
752b74d89bbSAndre Oppermann 	 * designated interface for icmp replies specified by sysctl
753b74d89bbSAndre Oppermann 	 * net.inet.icmp.reply_src (default not set). Otherwise continue
754b74d89bbSAndre Oppermann 	 * with normal source selection.
755b74d89bbSAndre Oppermann 	 */
75633c4f96dSRobert Watson 	if (V_reply_src[0] != '\0' && (ifp = ifunit(V_reply_src))) {
75733c4f96dSRobert Watson 		IF_ADDR_LOCK(ifp);
75833c4f96dSRobert Watson 		TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
759b74d89bbSAndre Oppermann 			if (ifa->ifa_addr->sa_family != AF_INET)
760b74d89bbSAndre Oppermann 				continue;
761b74d89bbSAndre Oppermann 			ia = ifatoia(ifa);
76233c4f96dSRobert Watson 			t = IA_SIN(ia)->sin_addr;
76333c4f96dSRobert Watson 			IF_ADDR_UNLOCK(ifp);
764b74d89bbSAndre Oppermann 			goto match;
765b74d89bbSAndre Oppermann 		}
76633c4f96dSRobert Watson 		IF_ADDR_UNLOCK(ifp);
767b74d89bbSAndre Oppermann 	}
768b74d89bbSAndre Oppermann 	/*
7691488eac8SAndre Oppermann 	 * If the packet was transiting through us, use the address of
7701488eac8SAndre Oppermann 	 * the interface that is the closest to the packet source.
7711488eac8SAndre Oppermann 	 * When we don't have a route back to the packet source, stop here
7721488eac8SAndre Oppermann 	 * and drop the packet.
7731488eac8SAndre Oppermann 	 */
7748b07e49aSJulian Elischer 	ia = ip_rtaddr(ip->ip_dst, M_GETFIB(m));
7750d4bef5dSDima Dorfman 	if (ia == NULL) {
7760d4bef5dSDima Dorfman 		m_freem(m);
777e27b0c87SRobert Watson 		ICMPSTAT_INC(icps_noroute);
7780d4bef5dSDima Dorfman 		goto done;
7790d4bef5dSDima Dorfman 	}
78033c4f96dSRobert Watson 	t = IA_SIN(ia)->sin_addr;
7818c0fec80SRobert Watson 	ifa_free(&ia->ia_ifa);
782ca925d9cSJonathan Lemon match:
783baee0c3eSRobert Watson #ifdef MAC
784a13e21f7SRobert Watson 	mac_netinet_icmp_replyinplace(m);
785baee0c3eSRobert Watson #endif
786df8bae1dSRodney W. Grimes 	ip->ip_src = t;
787603724d3SBjoern A. Zeeb 	ip->ip_ttl = V_ip_defttl;
788df8bae1dSRodney W. Grimes 
789df8bae1dSRodney W. Grimes 	if (optlen > 0) {
790df8bae1dSRodney W. Grimes 		register u_char *cp;
791df8bae1dSRodney W. Grimes 		int opt, cnt;
792df8bae1dSRodney W. Grimes 		u_int len;
793df8bae1dSRodney W. Grimes 
794df8bae1dSRodney W. Grimes 		/*
795df8bae1dSRodney W. Grimes 		 * Retrieve any source routing from the incoming packet;
796df8bae1dSRodney W. Grimes 		 * add on any record-route or timestamp options.
797df8bae1dSRodney W. Grimes 		 */
798df8bae1dSRodney W. Grimes 		cp = (u_char *) (ip + 1);
799e0982661SAndre Oppermann 		if ((opts = ip_srcroute(m)) == 0 &&
80034333b16SAndre Oppermann 		    (opts = m_gethdr(M_DONTWAIT, MT_DATA))) {
801df8bae1dSRodney W. Grimes 			opts->m_len = sizeof(struct in_addr);
802df8bae1dSRodney W. Grimes 			mtod(opts, struct in_addr *)->s_addr = 0;
803df8bae1dSRodney W. Grimes 		}
804df8bae1dSRodney W. Grimes 		if (opts) {
805df8bae1dSRodney W. Grimes #ifdef ICMPPRINTFS
806df8bae1dSRodney W. Grimes 		    if (icmpprintfs)
807df8bae1dSRodney W. Grimes 			    printf("icmp_reflect optlen %d rt %d => ",
808df8bae1dSRodney W. Grimes 				optlen, opts->m_len);
809df8bae1dSRodney W. Grimes #endif
810df8bae1dSRodney W. Grimes 		    for (cnt = optlen; cnt > 0; cnt -= len, cp += len) {
811df8bae1dSRodney W. Grimes 			    opt = cp[IPOPT_OPTVAL];
812df8bae1dSRodney W. Grimes 			    if (opt == IPOPT_EOL)
813df8bae1dSRodney W. Grimes 				    break;
814df8bae1dSRodney W. Grimes 			    if (opt == IPOPT_NOP)
815df8bae1dSRodney W. Grimes 				    len = 1;
816df8bae1dSRodney W. Grimes 			    else {
817707d00a3SJonathan Lemon 				    if (cnt < IPOPT_OLEN + sizeof(*cp))
818707d00a3SJonathan Lemon 					    break;
819df8bae1dSRodney W. Grimes 				    len = cp[IPOPT_OLEN];
820707d00a3SJonathan Lemon 				    if (len < IPOPT_OLEN + sizeof(*cp) ||
821707d00a3SJonathan Lemon 				        len > cnt)
822df8bae1dSRodney W. Grimes 					    break;
823df8bae1dSRodney W. Grimes 			    }
824df8bae1dSRodney W. Grimes 			    /*
825df8bae1dSRodney W. Grimes 			     * Should check for overflow, but it "can't happen"
826df8bae1dSRodney W. Grimes 			     */
827df8bae1dSRodney W. Grimes 			    if (opt == IPOPT_RR || opt == IPOPT_TS ||
828df8bae1dSRodney W. Grimes 				opt == IPOPT_SECURITY) {
829df8bae1dSRodney W. Grimes 				    bcopy((caddr_t)cp,
830df8bae1dSRodney W. Grimes 					mtod(opts, caddr_t) + opts->m_len, len);
831df8bae1dSRodney W. Grimes 				    opts->m_len += len;
832df8bae1dSRodney W. Grimes 			    }
833df8bae1dSRodney W. Grimes 		    }
834df8bae1dSRodney W. Grimes 		    /* Terminate & pad, if necessary */
835623ae52eSPoul-Henning Kamp 		    cnt = opts->m_len % 4;
836623ae52eSPoul-Henning Kamp 		    if (cnt) {
837df8bae1dSRodney W. Grimes 			    for (; cnt < 4; cnt++) {
838df8bae1dSRodney W. Grimes 				    *(mtod(opts, caddr_t) + opts->m_len) =
839df8bae1dSRodney W. Grimes 					IPOPT_EOL;
840df8bae1dSRodney W. Grimes 				    opts->m_len++;
841df8bae1dSRodney W. Grimes 			    }
842df8bae1dSRodney W. Grimes 		    }
843df8bae1dSRodney W. Grimes #ifdef ICMPPRINTFS
844df8bae1dSRodney W. Grimes 		    if (icmpprintfs)
845df8bae1dSRodney W. Grimes 			    printf("%d\n", opts->m_len);
846df8bae1dSRodney W. Grimes #endif
847df8bae1dSRodney W. Grimes 		}
848df8bae1dSRodney W. Grimes 		/*
849df8bae1dSRodney W. Grimes 		 * Now strip out original options by copying rest of first
850df8bae1dSRodney W. Grimes 		 * mbuf's data back, and adjust the IP length.
851df8bae1dSRodney W. Grimes 		 */
852df8bae1dSRodney W. Grimes 		ip->ip_len -= optlen;
85353be11f6SPoul-Henning Kamp 		ip->ip_v = IPVERSION;
85453be11f6SPoul-Henning Kamp 		ip->ip_hl = 5;
855df8bae1dSRodney W. Grimes 		m->m_len -= optlen;
856df8bae1dSRodney W. Grimes 		if (m->m_flags & M_PKTHDR)
857df8bae1dSRodney W. Grimes 			m->m_pkthdr.len -= optlen;
858df8bae1dSRodney W. Grimes 		optlen += sizeof(struct ip);
859df8bae1dSRodney W. Grimes 		bcopy((caddr_t)ip + optlen, (caddr_t)(ip + 1),
860df8bae1dSRodney W. Grimes 			 (unsigned)(m->m_len - sizeof(struct ip)));
861df8bae1dSRodney W. Grimes 	}
8629c855a36SSam Leffler 	m_tag_delete_nonpersistent(m);
863df8bae1dSRodney W. Grimes 	m->m_flags &= ~(M_BCAST|M_MCAST);
86402c1c707SAndre Oppermann 	icmp_send(m, opts);
865df8bae1dSRodney W. Grimes done:
866df8bae1dSRodney W. Grimes 	if (opts)
867df8bae1dSRodney W. Grimes 		(void)m_free(opts);
868df8bae1dSRodney W. Grimes }
869df8bae1dSRodney W. Grimes 
870df8bae1dSRodney W. Grimes /*
871df8bae1dSRodney W. Grimes  * Send an icmp packet back to the ip level,
872df8bae1dSRodney W. Grimes  * after supplying a checksum.
873df8bae1dSRodney W. Grimes  */
8740312fbe9SPoul-Henning Kamp static void
875f2565d68SRobert Watson icmp_send(struct mbuf *m, struct mbuf *opts)
876df8bae1dSRodney W. Grimes {
877df8bae1dSRodney W. Grimes 	register struct ip *ip = mtod(m, struct ip *);
878df8bae1dSRodney W. Grimes 	register int hlen;
879df8bae1dSRodney W. Grimes 	register struct icmp *icp;
880df8bae1dSRodney W. Grimes 
88153be11f6SPoul-Henning Kamp 	hlen = ip->ip_hl << 2;
882df8bae1dSRodney W. Grimes 	m->m_data += hlen;
883df8bae1dSRodney W. Grimes 	m->m_len -= hlen;
884df8bae1dSRodney W. Grimes 	icp = mtod(m, struct icmp *);
885df8bae1dSRodney W. Grimes 	icp->icmp_cksum = 0;
886df8bae1dSRodney W. Grimes 	icp->icmp_cksum = in_cksum(m, ip->ip_len - hlen);
887df8bae1dSRodney W. Grimes 	m->m_data -= hlen;
888df8bae1dSRodney W. Grimes 	m->m_len += hlen;
88994446a2eSArchie Cobbs 	m->m_pkthdr.rcvif = (struct ifnet *)0;
890df8bae1dSRodney W. Grimes #ifdef ICMPPRINTFS
8912b758395SGarrett Wollman 	if (icmpprintfs) {
8922b758395SGarrett Wollman 		char buf[4 * sizeof "123"];
8932b758395SGarrett Wollman 		strcpy(buf, inet_ntoa(ip->ip_dst));
8942b758395SGarrett Wollman 		printf("icmp_send dst %s src %s\n",
8952b758395SGarrett Wollman 		       buf, inet_ntoa(ip->ip_src));
8962b758395SGarrett Wollman 	}
897df8bae1dSRodney W. Grimes #endif
89802c1c707SAndre Oppermann 	(void) ip_output(m, opts, NULL, 0, NULL, NULL);
899df8bae1dSRodney W. Grimes }
900df8bae1dSRodney W. Grimes 
901d685b6eeSLuigi Rizzo /*
902d685b6eeSLuigi Rizzo  * Return milliseconds since 00:00 GMT in network format.
903d685b6eeSLuigi Rizzo  */
904d685b6eeSLuigi Rizzo uint32_t
905f2565d68SRobert Watson iptime(void)
906df8bae1dSRodney W. Grimes {
907df8bae1dSRodney W. Grimes 	struct timeval atv;
908df8bae1dSRodney W. Grimes 	u_long t;
909df8bae1dSRodney W. Grimes 
91016cd6db0SBill Fumerola 	getmicrotime(&atv);
911df8bae1dSRodney W. Grimes 	t = (atv.tv_sec % (24*60*60)) * 1000 + atv.tv_usec / 1000;
912df8bae1dSRodney W. Grimes 	return (htonl(t));
913df8bae1dSRodney W. Grimes }
914df8bae1dSRodney W. Grimes 
9155cbf3e08SGarrett Wollman /*
9165cbf3e08SGarrett Wollman  * Return the next larger or smaller MTU plateau (table from RFC 1191)
9175cbf3e08SGarrett Wollman  * given current value MTU.  If DIR is less than zero, a larger plateau
9185cbf3e08SGarrett Wollman  * is returned; otherwise, a smaller value is returned.
9195cbf3e08SGarrett Wollman  */
9201aedbd9cSAndre Oppermann int
921f2565d68SRobert Watson ip_next_mtu(int mtu, int dir)
9225cbf3e08SGarrett Wollman {
9235cbf3e08SGarrett Wollman 	static int mtutab[] = {
9244c037f8dSAndre Oppermann 		65535, 32000, 17914, 8166, 4352, 2002, 1492, 1280, 1006, 508,
9254c037f8dSAndre Oppermann 		296, 68, 0
9265cbf3e08SGarrett Wollman 	};
92706003a1eSAndre Oppermann 	int i, size;
9285cbf3e08SGarrett Wollman 
92906003a1eSAndre Oppermann 	size = (sizeof mtutab) / (sizeof mtutab[0]);
93006003a1eSAndre Oppermann 	if (dir >= 0) {
9311c0b0f52SGleb Smirnoff 		for (i = 0; i < size; i++)
93206003a1eSAndre Oppermann 			if (mtu > mtutab[i])
9335cbf3e08SGarrett Wollman 				return mtutab[i];
9345cbf3e08SGarrett Wollman 	} else {
93506003a1eSAndre Oppermann 		for (i = size - 1; i >= 0; i--)
93606003a1eSAndre Oppermann 			if (mtu < mtutab[i])
93706003a1eSAndre Oppermann 				return mtutab[i];
93806003a1eSAndre Oppermann 		if (mtu == mtutab[0])
93906003a1eSAndre Oppermann 			return mtutab[0];
9405cbf3e08SGarrett Wollman 	}
94106003a1eSAndre Oppermann 	return 0;
9425cbf3e08SGarrett Wollman }
94351508de1SMatthew Dillon 
94451508de1SMatthew Dillon 
94551508de1SMatthew Dillon /*
94651508de1SMatthew Dillon  * badport_bandlim() - check for ICMP bandwidth limit
94751508de1SMatthew Dillon  *
94851508de1SMatthew Dillon  *	Return 0 if it is ok to send an ICMP error response, -1 if we have
94951508de1SMatthew Dillon  *	hit our bandwidth limit and it is not ok.
95051508de1SMatthew Dillon  *
95151508de1SMatthew Dillon  *	If icmplim is <= 0, the feature is disabled and 0 is returned.
95251508de1SMatthew Dillon  *
95351508de1SMatthew Dillon  *	For now we separate the TCP and UDP subsystems w/ different 'which'
95451508de1SMatthew Dillon  *	values.  We may eventually remove this separation (and simplify the
95551508de1SMatthew Dillon  *	code further).
95651508de1SMatthew Dillon  *
95751508de1SMatthew Dillon  *	Note that the printing of the error message is delayed so we can
95851508de1SMatthew Dillon  *	properly print the icmp error rate that the system was trying to do
95951508de1SMatthew Dillon  *	(i.e. 22000/100 pps, etc...).  This can cause long delays in printing
96051508de1SMatthew Dillon  *	the 'final' error, but it doesn't make sense to solve the printing
96151508de1SMatthew Dillon  *	delay with more complex code.
96251508de1SMatthew Dillon  */
96351508de1SMatthew Dillon 
96451508de1SMatthew Dillon int
96551508de1SMatthew Dillon badport_bandlim(int which)
96651508de1SMatthew Dillon {
9678b615593SMarko Zec 
96800f21882SSam Leffler #define	N(a)	(sizeof (a) / sizeof (a[0]))
96900f21882SSam Leffler 	static struct rate {
97000f21882SSam Leffler 		const char	*type;
97100f21882SSam Leffler 		struct timeval	lasttime;
972439dfb0cSStefan Farfeleder 		int		curpps;
97300f21882SSam Leffler 	} rates[BANDLIM_MAX+1] = {
97400f21882SSam Leffler 		{ "icmp unreach response" },
97500f21882SSam Leffler 		{ "icmp ping response" },
97600f21882SSam Leffler 		{ "icmp tstamp response" },
97700f21882SSam Leffler 		{ "closed port RST response" },
97808af97b7SRobert Watson 		{ "open port RST response" },
97908af97b7SRobert Watson 		{ "icmp6 unreach response" }
98009f81a46SBosko Milekic 	};
98151508de1SMatthew Dillon 
98251508de1SMatthew Dillon 	/*
98300f21882SSam Leffler 	 * Return ok status if feature disabled or argument out of range.
98451508de1SMatthew Dillon 	 */
9858b615593SMarko Zec 	if (V_icmplim > 0 && (u_int) which < N(rates)) {
98600f21882SSam Leffler 		struct rate *r = &rates[which];
98700f21882SSam Leffler 		int opps = r->curpps;
98851508de1SMatthew Dillon 
9898b615593SMarko Zec 		if (!ppsratecheck(&r->lasttime, &r->curpps, V_icmplim))
99000f21882SSam Leffler 			return -1;	/* discard packet */
99151508de1SMatthew Dillon 		/*
99200f21882SSam Leffler 		 * If we've dropped below the threshold after having
99300f21882SSam Leffler 		 * rate-limited traffic print the message.  This preserves
99400f21882SSam Leffler 		 * the previous behaviour at the expense of added complexity.
99551508de1SMatthew Dillon 		 */
9968b615593SMarko Zec 		if (V_icmplim_output && opps > V_icmplim)
99700f21882SSam Leffler 			printf("Limiting %s from %d to %d packets/sec\n",
9988b615593SMarko Zec 				r->type, opps, V_icmplim);
99951508de1SMatthew Dillon 	}
100000f21882SSam Leffler 	return 0;			/* okay to send packet */
100100f21882SSam Leffler #undef N
100251508de1SMatthew Dillon }
1003