xref: /freebsd/sys/netinet/ip_icmp.c (revision 8f5a8818f57e31278b4bbd415c2cfa498306f91f)
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 #include "opt_ipsec.h"
376a800098SYoshinobu Inoue 
38df8bae1dSRodney W. Grimes #include <sys/param.h>
39df8bae1dSRodney W. Grimes #include <sys/systm.h>
40df8bae1dSRodney W. Grimes #include <sys/mbuf.h>
41df8bae1dSRodney W. Grimes #include <sys/protosw.h>
42df8bae1dSRodney W. Grimes #include <sys/socket.h>
43df8bae1dSRodney W. Grimes #include <sys/time.h>
44df8bae1dSRodney W. Grimes #include <sys/kernel.h>
45b5e8ce9fSBruce Evans #include <sys/sysctl.h>
4640fe9effSAndre Oppermann #include <sys/syslog.h>
47df8bae1dSRodney W. Grimes 
48df8bae1dSRodney W. Grimes #include <net/if.h>
4976039bc8SGleb Smirnoff #include <net/if_var.h>
509494d596SBrooks Davis #include <net/if_types.h>
51df8bae1dSRodney W. Grimes #include <net/route.h>
52eddfbb76SRobert Watson #include <net/vnet.h>
53df8bae1dSRodney W. Grimes 
54df8bae1dSRodney W. Grimes #include <netinet/in.h>
5597d8d152SAndre Oppermann #include <netinet/in_pcb.h>
56df8bae1dSRodney W. Grimes #include <netinet/in_systm.h>
57df8bae1dSRodney W. Grimes #include <netinet/in_var.h>
58df8bae1dSRodney W. Grimes #include <netinet/ip.h>
59df8bae1dSRodney W. Grimes #include <netinet/ip_icmp.h>
60b5e8ce9fSBruce Evans #include <netinet/ip_var.h>
61ef39adf0SAndre Oppermann #include <netinet/ip_options.h>
6297d8d152SAndre Oppermann #include <netinet/tcp.h>
6397d8d152SAndre Oppermann #include <netinet/tcp_var.h>
6497d8d152SAndre Oppermann #include <netinet/tcpip.h>
65df8bae1dSRodney W. Grimes #include <netinet/icmp_var.h>
66df8bae1dSRodney W. Grimes 
67b8e463e6SBjoern A. Zeeb #ifdef INET
68b2630c29SGeorge V. Neville-Neil #ifdef IPSEC
69b9234fafSSam Leffler #include <netipsec/ipsec.h>
70b9234fafSSam Leffler #include <netipsec/key.h>
71b9234fafSSam Leffler #endif
72b9234fafSSam Leffler 
7372a52a35SJonathan Lemon #include <machine/in_cksum.h>
7472a52a35SJonathan Lemon 
75aed55708SRobert Watson #include <security/mac/mac_framework.h>
76b8e463e6SBjoern A. Zeeb #endif /* INET */
77aed55708SRobert Watson 
78df8bae1dSRodney W. Grimes /*
79df8bae1dSRodney W. Grimes  * ICMP routines: error generation, receive packet processing, and
80df8bae1dSRodney W. Grimes  * routines to turnaround packets back to the originator, and
81df8bae1dSRodney W. Grimes  * host table maintenance routines.
82df8bae1dSRodney W. Grimes  */
83b8e463e6SBjoern A. Zeeb static VNET_DEFINE(int, icmplim) = 200;
84b8e463e6SBjoern A. Zeeb #define	V_icmplim			VNET(icmplim)
85b8e463e6SBjoern A. Zeeb SYSCTL_VNET_INT(_net_inet_icmp, ICMPCTL_ICMPLIM, icmplim, CTLFLAG_RW,
86b8e463e6SBjoern A. Zeeb 	&VNET_NAME(icmplim), 0,
87b8e463e6SBjoern A. Zeeb 	"Maximum number of ICMP responses per second");
88b8e463e6SBjoern A. Zeeb 
89b8e463e6SBjoern A. Zeeb static VNET_DEFINE(int, icmplim_output) = 1;
90b8e463e6SBjoern A. Zeeb #define	V_icmplim_output		VNET(icmplim_output)
91b8e463e6SBjoern A. Zeeb SYSCTL_VNET_INT(_net_inet_icmp, OID_AUTO, icmplim_output, CTLFLAG_RW,
92b8e463e6SBjoern A. Zeeb 	&VNET_NAME(icmplim_output), 0,
93316efdb3SEitan Adler 	"Enable logging of ICMP response rate limiting");
94b8e463e6SBjoern A. Zeeb 
95b8e463e6SBjoern A. Zeeb #ifdef INET
965b7cb97cSAndrey V. Elsukov VNET_PCPUSTAT_DEFINE(struct icmpstat, icmpstat);
975b7cb97cSAndrey V. Elsukov VNET_PCPUSTAT_SYSINIT(icmpstat);
985b7cb97cSAndrey V. Elsukov SYSCTL_VNET_PCPUSTAT(_net_inet_icmp, ICMPCTL_STATS, stats, struct icmpstat,
995b7cb97cSAndrey V. Elsukov     icmpstat, "ICMP statistics (struct icmpstat, netinet/icmp_var.h)");
1005b7cb97cSAndrey V. Elsukov 
1015b7cb97cSAndrey V. Elsukov #ifdef VIMAGE
1025b7cb97cSAndrey V. Elsukov VNET_PCPUSTAT_SYSUNINIT(icmpstat);
1035b7cb97cSAndrey V. Elsukov #endif /* VIMAGE */
104eddfbb76SRobert Watson 
1053e288e62SDimitry Andric static VNET_DEFINE(int, icmpmaskrepl) = 0;
10682cea7e6SBjoern A. Zeeb #define	V_icmpmaskrepl			VNET(icmpmaskrepl)
107eddfbb76SRobert Watson SYSCTL_VNET_INT(_net_inet_icmp, ICMPCTL_MASKREPL, maskrepl, CTLFLAG_RW,
108eddfbb76SRobert Watson 	&VNET_NAME(icmpmaskrepl), 0,
1098b615593SMarko Zec 	"Reply to ICMP Address Mask Request packets.");
11057842a38SMatthew N. Dodd 
1113e288e62SDimitry Andric static VNET_DEFINE(u_int, icmpmaskfake) = 0;
11282cea7e6SBjoern A. Zeeb #define	V_icmpmaskfake			VNET(icmpmaskfake)
113eddfbb76SRobert Watson SYSCTL_VNET_UINT(_net_inet_icmp, OID_AUTO, maskfake, CTLFLAG_RW,
114eddfbb76SRobert Watson 	&VNET_NAME(icmpmaskfake), 0,
115eddfbb76SRobert Watson 	"Fake reply to ICMP Address Mask Request packets.");
1160312fbe9SPoul-Henning Kamp 
1173c2824b9SAlexander V. Chernikov VNET_DEFINE(int, drop_redirect) = 0;
11818d3153eSDag-Erling Smørgrav 
1193e288e62SDimitry Andric static VNET_DEFINE(int, log_redirect) = 0;
12082cea7e6SBjoern A. Zeeb #define	V_log_redirect			VNET(log_redirect)
121eddfbb76SRobert Watson SYSCTL_VNET_INT(_net_inet_icmp, OID_AUTO, log_redirect, 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)
127eddfbb76SRobert Watson SYSCTL_VNET_STRING(_net_inet_icmp, OID_AUTO, reply_src, 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)
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 
1373e288e62SDimitry Andric static VNET_DEFINE(int, icmp_quotelen) = 8;
13882cea7e6SBjoern A. Zeeb #define	V_icmp_quotelen			VNET(icmp_quotelen)
139eddfbb76SRobert Watson SYSCTL_VNET_INT(_net_inet_icmp, OID_AUTO, quotelen, 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)
148eddfbb76SRobert Watson SYSCTL_VNET_INT(_net_inet_icmp, OID_AUTO, bmcastecho, CTLFLAG_RW,
149eddfbb76SRobert Watson 	&VNET_NAME(icmpbmcastecho), 0,
150eddfbb76SRobert Watson 	"");
1517022ea0aSGarrett Wollman 
15251508de1SMatthew Dillon 
153df8bae1dSRodney W. Grimes #ifdef ICMPPRINTFS
154df8bae1dSRodney W. Grimes int	icmpprintfs = 0;
155df8bae1dSRodney W. Grimes #endif
156df8bae1dSRodney W. Grimes 
1574d77a549SAlfred Perlstein static void	icmp_reflect(struct mbuf *);
15802c1c707SAndre Oppermann static void	icmp_send(struct mbuf *, struct mbuf *);
1590312fbe9SPoul-Henning Kamp 
160df8bae1dSRodney W. Grimes extern	struct protosw inetsw[];
161df8bae1dSRodney W. Grimes 
1623c2824b9SAlexander V. Chernikov static int
1633c2824b9SAlexander V. Chernikov sysctl_net_icmp_drop_redir(SYSCTL_HANDLER_ARGS)
1643c2824b9SAlexander V. Chernikov {
1653c2824b9SAlexander V. Chernikov 	int error, new;
1663c2824b9SAlexander V. Chernikov 	int i;
1673c2824b9SAlexander V. Chernikov 	struct radix_node_head *rnh;
1683c2824b9SAlexander V. Chernikov 
1693c2824b9SAlexander V. Chernikov 	new = V_drop_redirect;
1703c2824b9SAlexander V. Chernikov 	error = sysctl_handle_int(oidp, &new, 0, req);
1713c2824b9SAlexander V. Chernikov 	if (error == 0 && req->newptr) {
1723c2824b9SAlexander V. Chernikov 		new = (new != 0) ? 1 : 0;
1733c2824b9SAlexander V. Chernikov 
1743c2824b9SAlexander V. Chernikov 		if (new == V_drop_redirect)
1753c2824b9SAlexander V. Chernikov 			return (0);
1763c2824b9SAlexander V. Chernikov 
1773c2824b9SAlexander V. Chernikov 		for (i = 0; i < rt_numfibs; i++) {
1783c2824b9SAlexander V. Chernikov 			if ((rnh = rt_tables_get_rnh(i, AF_INET)) == NULL)
1793c2824b9SAlexander V. Chernikov 				continue;
1803c2824b9SAlexander V. Chernikov 			RADIX_NODE_HEAD_LOCK(rnh);
1813c2824b9SAlexander V. Chernikov 			in_setmatchfunc(rnh, new);
1823c2824b9SAlexander V. Chernikov 			RADIX_NODE_HEAD_UNLOCK(rnh);
1833c2824b9SAlexander V. Chernikov 		}
1843c2824b9SAlexander V. Chernikov 
1853c2824b9SAlexander V. Chernikov 		V_drop_redirect = new;
1863c2824b9SAlexander V. Chernikov 	}
1873c2824b9SAlexander V. Chernikov 
1883c2824b9SAlexander V. Chernikov 	return (error);
1893c2824b9SAlexander V. Chernikov }
1903c2824b9SAlexander V. Chernikov 
1913c2824b9SAlexander V. Chernikov SYSCTL_VNET_PROC(_net_inet_icmp, OID_AUTO, drop_redirect,
1923c2824b9SAlexander V. Chernikov     CTLTYPE_INT|CTLFLAG_RW, 0, 0,
1933c2824b9SAlexander V. Chernikov     sysctl_net_icmp_drop_redir, "I", "Ignore ICMP redirects");
1943c2824b9SAlexander V. Chernikov 
195df8bae1dSRodney W. Grimes /*
196315e3e38SRobert Watson  * Kernel module interface for updating icmpstat.  The argument is an index
197315e3e38SRobert Watson  * into icmpstat treated as an array of u_long.  While this encodes the
198315e3e38SRobert Watson  * general layout of icmpstat into the caller, it doesn't encode its
199315e3e38SRobert Watson  * location, so that future changes to add, for example, per-CPU stats
200315e3e38SRobert Watson  * support won't cause binary compatibility problems for kernel modules.
201315e3e38SRobert Watson  */
202315e3e38SRobert Watson void
203315e3e38SRobert Watson kmod_icmpstat_inc(int statnum)
204315e3e38SRobert Watson {
205315e3e38SRobert Watson 
2065b7cb97cSAndrey V. Elsukov 	counter_u64_add(VNET(icmpstat)[statnum], 1);
207315e3e38SRobert Watson }
208315e3e38SRobert Watson 
209315e3e38SRobert Watson /*
210df8bae1dSRodney W. Grimes  * Generate an error packet of type error
211df8bae1dSRodney W. Grimes  * in response to bad packet ip.
212df8bae1dSRodney W. Grimes  */
213df8bae1dSRodney W. Grimes void
214d685b6eeSLuigi Rizzo icmp_error(struct mbuf *n, int type, int code, uint32_t dest, int mtu)
215df8bae1dSRodney W. Grimes {
216df8bae1dSRodney W. Grimes 	register struct ip *oip = mtod(n, struct ip *), *nip;
217e86ebebcSAndre Oppermann 	register unsigned oiphlen = oip->ip_hl << 2;
218df8bae1dSRodney W. Grimes 	register struct icmp *icp;
219df8bae1dSRodney W. Grimes 	register struct mbuf *m;
220e86ebebcSAndre Oppermann 	unsigned icmplen, icmpelen, nlen;
221df8bae1dSRodney W. Grimes 
222e86ebebcSAndre Oppermann 	KASSERT((u_int)type <= ICMP_MAXTYPE, ("%s: illegal ICMP type", __func__));
223df8bae1dSRodney W. Grimes #ifdef ICMPPRINTFS
224df8bae1dSRodney W. Grimes 	if (icmpprintfs)
225623ae52eSPoul-Henning Kamp 		printf("icmp_error(%p, %x, %d)\n", oip, type, code);
226df8bae1dSRodney W. Grimes #endif
227df8bae1dSRodney W. Grimes 	if (type != ICMP_REDIRECT)
228e27b0c87SRobert Watson 		ICMPSTAT_INC(icps_error);
229df8bae1dSRodney W. Grimes 	/*
230e86ebebcSAndre Oppermann 	 * Don't send error:
231e86ebebcSAndre Oppermann 	 *  if the original packet was encrypted.
232e86ebebcSAndre Oppermann 	 *  if not the first fragment of message.
233e86ebebcSAndre Oppermann 	 *  in response to a multicast or broadcast packet.
234e86ebebcSAndre Oppermann 	 *  if the old packet protocol was an ICMP error message.
235df8bae1dSRodney W. Grimes 	 */
236cad1917dSHajimu UMEMOTO 	if (n->m_flags & M_DECRYPTED)
237cad1917dSHajimu UMEMOTO 		goto freeit;
2388f134647SGleb Smirnoff 	if (oip->ip_off & htons(~(IP_MF|IP_DF)))
239df8bae1dSRodney W. Grimes 		goto freeit;
240e86ebebcSAndre Oppermann 	if (n->m_flags & (M_BCAST|M_MCAST))
241e86ebebcSAndre Oppermann 		goto freeit;
242df8bae1dSRodney W. Grimes 	if (oip->ip_p == IPPROTO_ICMP && type != ICMP_REDIRECT &&
243e86ebebcSAndre Oppermann 	  n->m_len >= oiphlen + ICMP_MINLEN &&
244e86ebebcSAndre Oppermann 	  !ICMP_INFOTYPE(((struct icmp *)((caddr_t)oip + oiphlen))->icmp_type)) {
245e27b0c87SRobert Watson 		ICMPSTAT_INC(icps_oldicmp);
246df8bae1dSRodney W. Grimes 		goto freeit;
247df8bae1dSRodney W. Grimes 	}
248e86ebebcSAndre Oppermann 	/* Drop if IP header plus 8 bytes is not contignous in first mbuf. */
249e86ebebcSAndre Oppermann 	if (oiphlen + 8 > n->m_len)
250df8bae1dSRodney W. Grimes 		goto freeit;
251df8bae1dSRodney W. Grimes 	/*
252e86ebebcSAndre Oppermann 	 * Calculate length to quote from original packet and
253e86ebebcSAndre Oppermann 	 * prevent the ICMP mbuf from overflowing.
254e86ebebcSAndre Oppermann 	 * Unfortunatly this is non-trivial since ip_forward()
255e86ebebcSAndre Oppermann 	 * sends us truncated packets.
256df8bae1dSRodney W. Grimes 	 */
257e86ebebcSAndre Oppermann 	nlen = m_length(n, NULL);
258e86ebebcSAndre Oppermann 	if (oip->ip_p == IPPROTO_TCP) {
259e86ebebcSAndre Oppermann 		struct tcphdr *th;
260e86ebebcSAndre Oppermann 		int tcphlen;
261e86ebebcSAndre Oppermann 
262e86ebebcSAndre Oppermann 		if (oiphlen + sizeof(struct tcphdr) > n->m_len &&
263e86ebebcSAndre Oppermann 		    n->m_next == NULL)
264e86ebebcSAndre Oppermann 			goto stdreply;
265e86ebebcSAndre Oppermann 		if (n->m_len < oiphlen + sizeof(struct tcphdr) &&
266e86ebebcSAndre Oppermann 		    ((n = m_pullup(n, oiphlen + sizeof(struct tcphdr))) == NULL))
267e86ebebcSAndre Oppermann 			goto freeit;
268e86ebebcSAndre Oppermann 		th = (struct tcphdr *)((caddr_t)oip + oiphlen);
269e86ebebcSAndre Oppermann 		tcphlen = th->th_off << 2;
270e86ebebcSAndre Oppermann 		if (tcphlen < sizeof(struct tcphdr))
271e86ebebcSAndre Oppermann 			goto freeit;
2728f134647SGleb Smirnoff 		if (ntohs(oip->ip_len) < oiphlen + tcphlen)
273e86ebebcSAndre Oppermann 			goto freeit;
274e86ebebcSAndre Oppermann 		if (oiphlen + tcphlen > n->m_len && n->m_next == NULL)
275e86ebebcSAndre Oppermann 			goto stdreply;
276e86ebebcSAndre Oppermann 		if (n->m_len < oiphlen + tcphlen &&
277e86ebebcSAndre Oppermann 		    ((n = m_pullup(n, oiphlen + tcphlen)) == NULL))
278e86ebebcSAndre Oppermann 			goto freeit;
2798f134647SGleb Smirnoff 		icmpelen = max(tcphlen, min(V_icmp_quotelen,
2808f134647SGleb Smirnoff 		    ntohs(oip->ip_len) - oiphlen));
281e86ebebcSAndre Oppermann 	} else
2828f134647SGleb Smirnoff stdreply:	icmpelen = max(8, min(V_icmp_quotelen, ntohs(oip->ip_len) - oiphlen));
283e86ebebcSAndre Oppermann 
284e86ebebcSAndre Oppermann 	icmplen = min(oiphlen + icmpelen, nlen);
285e86ebebcSAndre Oppermann 	if (icmplen < sizeof(struct ip))
286e86ebebcSAndre Oppermann 		goto freeit;
287e86ebebcSAndre Oppermann 
288e86ebebcSAndre Oppermann 	if (MHLEN > sizeof(struct ip) + ICMP_MINLEN + icmplen)
289eb1b1807SGleb Smirnoff 		m = m_gethdr(M_NOWAIT, MT_DATA);
290e86ebebcSAndre Oppermann 	else
291eb1b1807SGleb Smirnoff 		m = m_getcl(M_NOWAIT, MT_DATA, M_PKTHDR);
292df8bae1dSRodney W. Grimes 	if (m == NULL)
293df8bae1dSRodney W. Grimes 		goto freeit;
2940070e096SRobert Watson #ifdef MAC
295a13e21f7SRobert Watson 	mac_netinet_icmp_reply(n, m);
2960070e096SRobert Watson #endif
297e86ebebcSAndre Oppermann 	icmplen = min(icmplen, M_TRAILINGSPACE(m) - sizeof(struct ip) - ICMP_MINLEN);
298e86ebebcSAndre Oppermann 	m_align(m, ICMP_MINLEN + icmplen);
299e86ebebcSAndre Oppermann 	m->m_len = ICMP_MINLEN + icmplen;
3006b773dffSAndre Oppermann 
3018b07e49aSJulian Elischer 	/* XXX MRT  make the outgoing packet use the same FIB
3028b07e49aSJulian Elischer 	 * that was associated with the incoming packet
3038b07e49aSJulian Elischer 	 */
3048b07e49aSJulian Elischer 	M_SETFIB(m, M_GETFIB(n));
305df8bae1dSRodney W. Grimes 	icp = mtod(m, struct icmp *);
306e27b0c87SRobert Watson 	ICMPSTAT_INC(icps_outhist[type]);
307df8bae1dSRodney W. Grimes 	icp->icmp_type = type;
308df8bae1dSRodney W. Grimes 	if (type == ICMP_REDIRECT)
309df8bae1dSRodney W. Grimes 		icp->icmp_gwaddr.s_addr = dest;
310df8bae1dSRodney W. Grimes 	else {
311df8bae1dSRodney W. Grimes 		icp->icmp_void = 0;
312df8bae1dSRodney W. Grimes 		/*
313df8bae1dSRodney W. Grimes 		 * The following assignments assume an overlay with the
314e86ebebcSAndre Oppermann 		 * just zeroed icmp_void field.
315df8bae1dSRodney W. Grimes 		 */
316df8bae1dSRodney W. Grimes 		if (type == ICMP_PARAMPROB) {
317df8bae1dSRodney W. Grimes 			icp->icmp_pptr = code;
318df8bae1dSRodney W. Grimes 			code = 0;
319df8bae1dSRodney W. Grimes 		} else if (type == ICMP_UNREACH &&
320c773494eSAndre Oppermann 			code == ICMP_UNREACH_NEEDFRAG && mtu) {
321c773494eSAndre Oppermann 			icp->icmp_nextmtu = htons(mtu);
322df8bae1dSRodney W. Grimes 		}
323df8bae1dSRodney W. Grimes 	}
324df8bae1dSRodney W. Grimes 	icp->icmp_code = code;
32504287599SRuslan Ermilov 
32604287599SRuslan Ermilov 	/*
327e86ebebcSAndre Oppermann 	 * Copy the quotation into ICMP message and
328e86ebebcSAndre Oppermann 	 * convert quoted IP header back to network representation.
32904287599SRuslan Ermilov 	 */
330e86ebebcSAndre Oppermann 	m_copydata(n, 0, icmplen, (caddr_t)&icp->icmp_ip);
331e86ebebcSAndre Oppermann 	nip = &icp->icmp_ip;
332df8bae1dSRodney W. Grimes 
333df8bae1dSRodney W. Grimes 	/*
334e86ebebcSAndre Oppermann 	 * Set up ICMP message mbuf and copy old IP header (without options
335e86ebebcSAndre Oppermann 	 * in front of ICMP message.
336c550f220SMax Laier 	 * If the original mbuf was meant to bypass the firewall, the error
337c550f220SMax Laier 	 * reply should bypass as well.
338c550f220SMax Laier 	 */
339c550f220SMax Laier 	m->m_flags |= n->m_flags & M_SKIP_FIREWALL;
340df8bae1dSRodney W. Grimes 	m->m_data -= sizeof(struct ip);
341df8bae1dSRodney W. Grimes 	m->m_len += sizeof(struct ip);
342df8bae1dSRodney W. Grimes 	m->m_pkthdr.len = m->m_len;
343df8bae1dSRodney W. Grimes 	m->m_pkthdr.rcvif = n->m_pkthdr.rcvif;
344df8bae1dSRodney W. Grimes 	nip = mtod(m, struct ip *);
345df8bae1dSRodney W. Grimes 	bcopy((caddr_t)oip, (caddr_t)nip, sizeof(struct ip));
3468f134647SGleb Smirnoff 	nip->ip_len = htons(m->m_len);
34753be11f6SPoul-Henning Kamp 	nip->ip_v = IPVERSION;
34853be11f6SPoul-Henning Kamp 	nip->ip_hl = 5;
349df8bae1dSRodney W. Grimes 	nip->ip_p = IPPROTO_ICMP;
350df8bae1dSRodney W. Grimes 	nip->ip_tos = 0;
35141ea685cSAndrey V. Elsukov 	nip->ip_off = 0;
352df8bae1dSRodney W. Grimes 	icmp_reflect(m);
353df8bae1dSRodney W. Grimes 
354df8bae1dSRodney W. Grimes freeit:
355df8bae1dSRodney W. Grimes 	m_freem(n);
356df8bae1dSRodney W. Grimes }
357df8bae1dSRodney W. Grimes 
358df8bae1dSRodney W. Grimes /*
359df8bae1dSRodney W. Grimes  * Process a received ICMP message.
360df8bae1dSRodney W. Grimes  */
361*8f5a8818SKevin Lo int
362*8f5a8818SKevin Lo icmp_input(struct mbuf **mp, int *offp, int proto)
363df8bae1dSRodney W. Grimes {
36416d6c90fSAndre Oppermann 	struct icmp *icp;
365df8bae1dSRodney W. Grimes 	struct in_ifaddr *ia;
366*8f5a8818SKevin Lo 	struct mbuf *m = *mp;
36716d6c90fSAndre Oppermann 	struct ip *ip = mtod(m, struct ip *);
36816d6c90fSAndre Oppermann 	struct sockaddr_in icmpsrc, icmpdst, icmpgw;
369*8f5a8818SKevin Lo 	int hlen = *offp;
370*8f5a8818SKevin Lo 	int icmplen = ntohs(ip->ip_len) - *offp;
37116d6c90fSAndre Oppermann 	int i, code;
3724d77a549SAlfred Perlstein 	void (*ctlfunc)(int, struct sockaddr *, void *);
3738b07e49aSJulian Elischer 	int fibnum;
374df8bae1dSRodney W. Grimes 
375*8f5a8818SKevin Lo 	*mp = NULL;
376*8f5a8818SKevin Lo 
377df8bae1dSRodney W. Grimes 	/*
378df8bae1dSRodney W. Grimes 	 * Locate icmp structure in mbuf, and check
379df8bae1dSRodney W. Grimes 	 * that not corrupted and of at least minimum length.
380df8bae1dSRodney W. Grimes 	 */
381df8bae1dSRodney W. Grimes #ifdef ICMPPRINTFS
3822b758395SGarrett Wollman 	if (icmpprintfs) {
3832b758395SGarrett Wollman 		char buf[4 * sizeof "123"];
3842b758395SGarrett Wollman 		strcpy(buf, inet_ntoa(ip->ip_src));
3852b758395SGarrett Wollman 		printf("icmp_input from %s to %s, len %d\n",
3862b758395SGarrett Wollman 		       buf, inet_ntoa(ip->ip_dst), icmplen);
3872b758395SGarrett Wollman 	}
388df8bae1dSRodney W. Grimes #endif
389df8bae1dSRodney W. Grimes 	if (icmplen < ICMP_MINLEN) {
390e27b0c87SRobert Watson 		ICMPSTAT_INC(icps_tooshort);
391df8bae1dSRodney W. Grimes 		goto freeit;
392df8bae1dSRodney W. Grimes 	}
393df8bae1dSRodney W. Grimes 	i = hlen + min(icmplen, ICMP_ADVLENMIN);
394852da713SBjoern A. Zeeb 	if (m->m_len < i && (m = m_pullup(m, i)) == NULL)  {
395e27b0c87SRobert Watson 		ICMPSTAT_INC(icps_tooshort);
396*8f5a8818SKevin Lo 		return (IPPROTO_DONE);
397df8bae1dSRodney W. Grimes 	}
398df8bae1dSRodney W. Grimes 	ip = mtod(m, struct ip *);
399df8bae1dSRodney W. Grimes 	m->m_len -= hlen;
400df8bae1dSRodney W. Grimes 	m->m_data += hlen;
401df8bae1dSRodney W. Grimes 	icp = mtod(m, struct icmp *);
402df8bae1dSRodney W. Grimes 	if (in_cksum(m, icmplen)) {
403e27b0c87SRobert Watson 		ICMPSTAT_INC(icps_checksum);
404df8bae1dSRodney W. Grimes 		goto freeit;
405df8bae1dSRodney W. Grimes 	}
406df8bae1dSRodney W. Grimes 	m->m_len += hlen;
407df8bae1dSRodney W. Grimes 	m->m_data -= hlen;
408df8bae1dSRodney W. Grimes 
4096a800098SYoshinobu Inoue 	if (m->m_pkthdr.rcvif && m->m_pkthdr.rcvif->if_type == IFT_FAITH) {
4106a800098SYoshinobu Inoue 		/*
4116a800098SYoshinobu Inoue 		 * Deliver very specific ICMP type only.
4126a800098SYoshinobu Inoue 		 */
4136a800098SYoshinobu Inoue 		switch (icp->icmp_type) {
4146a800098SYoshinobu Inoue 		case ICMP_UNREACH:
4156a800098SYoshinobu Inoue 		case ICMP_TIMXCEED:
4166a800098SYoshinobu Inoue 			break;
4176a800098SYoshinobu Inoue 		default:
4186a800098SYoshinobu Inoue 			goto freeit;
4196a800098SYoshinobu Inoue 		}
4206a800098SYoshinobu Inoue 	}
4216a800098SYoshinobu Inoue 
422df8bae1dSRodney W. Grimes #ifdef ICMPPRINTFS
423df8bae1dSRodney W. Grimes 	if (icmpprintfs)
424df8bae1dSRodney W. Grimes 		printf("icmp_input, type %d code %d\n", icp->icmp_type,
425df8bae1dSRodney W. Grimes 		    icp->icmp_code);
426df8bae1dSRodney W. Grimes #endif
4275b7ee6edSGarrett Wollman 
4285b7ee6edSGarrett Wollman 	/*
4295b7ee6edSGarrett Wollman 	 * Message type specific processing.
4305b7ee6edSGarrett Wollman 	 */
431df8bae1dSRodney W. Grimes 	if (icp->icmp_type > ICMP_MAXTYPE)
432df8bae1dSRodney W. Grimes 		goto raw;
43316d6c90fSAndre Oppermann 
43416d6c90fSAndre Oppermann 	/* Initialize */
43516d6c90fSAndre Oppermann 	bzero(&icmpsrc, sizeof(icmpsrc));
43616d6c90fSAndre Oppermann 	icmpsrc.sin_len = sizeof(struct sockaddr_in);
43716d6c90fSAndre Oppermann 	icmpsrc.sin_family = AF_INET;
43816d6c90fSAndre Oppermann 	bzero(&icmpdst, sizeof(icmpdst));
43916d6c90fSAndre Oppermann 	icmpdst.sin_len = sizeof(struct sockaddr_in);
44016d6c90fSAndre Oppermann 	icmpdst.sin_family = AF_INET;
44116d6c90fSAndre Oppermann 	bzero(&icmpgw, sizeof(icmpgw));
44216d6c90fSAndre Oppermann 	icmpgw.sin_len = sizeof(struct sockaddr_in);
44316d6c90fSAndre Oppermann 	icmpgw.sin_family = AF_INET;
44416d6c90fSAndre Oppermann 
445e27b0c87SRobert Watson 	ICMPSTAT_INC(icps_inhist[icp->icmp_type]);
446df8bae1dSRodney W. Grimes 	code = icp->icmp_code;
447df8bae1dSRodney W. Grimes 	switch (icp->icmp_type) {
448df8bae1dSRodney W. Grimes 
449df8bae1dSRodney W. Grimes 	case ICMP_UNREACH:
450df8bae1dSRodney W. Grimes 		switch (code) {
451df8bae1dSRodney W. Grimes 			case ICMP_UNREACH_NET:
452df8bae1dSRodney W. Grimes 			case ICMP_UNREACH_HOST:
453df8bae1dSRodney W. Grimes 			case ICMP_UNREACH_SRCFAIL:
454e4bb5b05SJonathan Lemon 			case ICMP_UNREACH_NET_UNKNOWN:
455e4bb5b05SJonathan Lemon 			case ICMP_UNREACH_HOST_UNKNOWN:
456e4bb5b05SJonathan Lemon 			case ICMP_UNREACH_ISOLATED:
457e4bb5b05SJonathan Lemon 			case ICMP_UNREACH_TOSNET:
458e4bb5b05SJonathan Lemon 			case ICMP_UNREACH_TOSHOST:
459e4bb5b05SJonathan Lemon 			case ICMP_UNREACH_HOST_PRECEDENCE:
460e4bb5b05SJonathan Lemon 			case ICMP_UNREACH_PRECEDENCE_CUTOFF:
461e4bb5b05SJonathan Lemon 				code = PRC_UNREACH_NET;
462df8bae1dSRodney W. Grimes 				break;
463df8bae1dSRodney W. Grimes 
464df8bae1dSRodney W. Grimes 			case ICMP_UNREACH_NEEDFRAG:
465df8bae1dSRodney W. Grimes 				code = PRC_MSGSIZE;
466df8bae1dSRodney W. Grimes 				break;
467df8bae1dSRodney W. Grimes 
468e4bb5b05SJonathan Lemon 			/*
469e4bb5b05SJonathan Lemon 			 * RFC 1122, Sections 3.2.2.1 and 4.2.3.9.
470e4bb5b05SJonathan Lemon 			 * Treat subcodes 2,3 as immediate RST
471e4bb5b05SJonathan Lemon 			 */
472e4bb5b05SJonathan Lemon 			case ICMP_UNREACH_PROTOCOL:
473e4bb5b05SJonathan Lemon 			case ICMP_UNREACH_PORT:
474b77d155dSJesper Skriver 				code = PRC_UNREACH_PORT;
475b11d7a4aSPoul-Henning Kamp 				break;
47690fcbbd6SPoul-Henning Kamp 
47790fcbbd6SPoul-Henning Kamp 			case ICMP_UNREACH_NET_PROHIB:
47890fcbbd6SPoul-Henning Kamp 			case ICMP_UNREACH_HOST_PROHIB:
4799c4b2574SPaul Traina 			case ICMP_UNREACH_FILTER_PROHIB:
48090fcbbd6SPoul-Henning Kamp 				code = PRC_UNREACH_ADMIN_PROHIB;
481b11d7a4aSPoul-Henning Kamp 				break;
482b11d7a4aSPoul-Henning Kamp 
483df8bae1dSRodney W. Grimes 			default:
484df8bae1dSRodney W. Grimes 				goto badcode;
485df8bae1dSRodney W. Grimes 		}
486df8bae1dSRodney W. Grimes 		goto deliver;
487df8bae1dSRodney W. Grimes 
488df8bae1dSRodney W. Grimes 	case ICMP_TIMXCEED:
489df8bae1dSRodney W. Grimes 		if (code > 1)
490df8bae1dSRodney W. Grimes 			goto badcode;
491df8bae1dSRodney W. Grimes 		code += PRC_TIMXCEED_INTRANS;
492df8bae1dSRodney W. Grimes 		goto deliver;
493df8bae1dSRodney W. Grimes 
494df8bae1dSRodney W. Grimes 	case ICMP_PARAMPROB:
495df8bae1dSRodney W. Grimes 		if (code > 1)
496df8bae1dSRodney W. Grimes 			goto badcode;
497df8bae1dSRodney W. Grimes 		code = PRC_PARAMPROB;
498df8bae1dSRodney W. Grimes 		goto deliver;
499df8bae1dSRodney W. Grimes 
500df8bae1dSRodney W. Grimes 	case ICMP_SOURCEQUENCH:
501df8bae1dSRodney W. Grimes 		if (code)
502df8bae1dSRodney W. Grimes 			goto badcode;
503df8bae1dSRodney W. Grimes 		code = PRC_QUENCH;
504df8bae1dSRodney W. Grimes 	deliver:
505df8bae1dSRodney W. Grimes 		/*
506df8bae1dSRodney W. Grimes 		 * Problem with datagram; advise higher level routines.
507df8bae1dSRodney W. Grimes 		 */
508df8bae1dSRodney W. Grimes 		if (icmplen < ICMP_ADVLENMIN || icmplen < ICMP_ADVLEN(icp) ||
50953be11f6SPoul-Henning Kamp 		    icp->icmp_ip.ip_hl < (sizeof(struct ip) >> 2)) {
510e27b0c87SRobert Watson 			ICMPSTAT_INC(icps_badlen);
511df8bae1dSRodney W. Grimes 			goto freeit;
512df8bae1dSRodney W. Grimes 		}
5135b7ee6edSGarrett Wollman 		/* Discard ICMP's in response to multicast packets */
5145b7ee6edSGarrett Wollman 		if (IN_MULTICAST(ntohl(icp->icmp_ip.ip_dst.s_addr)))
5155b7ee6edSGarrett Wollman 			goto badcode;
516df8bae1dSRodney W. Grimes #ifdef ICMPPRINTFS
517df8bae1dSRodney W. Grimes 		if (icmpprintfs)
518df8bae1dSRodney W. Grimes 			printf("deliver to protocol %d\n", icp->icmp_ip.ip_p);
519df8bae1dSRodney W. Grimes #endif
520df8bae1dSRodney W. Grimes 		icmpsrc.sin_addr = icp->icmp_ip.ip_dst;
5216a800098SYoshinobu Inoue 		/*
5226a800098SYoshinobu Inoue 		 * XXX if the packet contains [IPv4 AH TCP], we can't make a
5236a800098SYoshinobu Inoue 		 * notification to TCP layer.
5246a800098SYoshinobu Inoue 		 */
525623ae52eSPoul-Henning Kamp 		ctlfunc = inetsw[ip_protox[icp->icmp_ip.ip_p]].pr_ctlinput;
526623ae52eSPoul-Henning Kamp 		if (ctlfunc)
527df8bae1dSRodney W. Grimes 			(*ctlfunc)(code, (struct sockaddr *)&icmpsrc,
528b62d102cSBruce Evans 				   (void *)&icp->icmp_ip);
529df8bae1dSRodney W. Grimes 		break;
530df8bae1dSRodney W. Grimes 
531df8bae1dSRodney W. Grimes 	badcode:
532e27b0c87SRobert Watson 		ICMPSTAT_INC(icps_badcode);
533df8bae1dSRodney W. Grimes 		break;
534df8bae1dSRodney W. Grimes 
535df8bae1dSRodney W. Grimes 	case ICMP_ECHO:
5368b615593SMarko Zec 		if (!V_icmpbmcastecho
537d311884fSDavid Greenman 		    && (m->m_flags & (M_MCAST | M_BCAST)) != 0) {
538e27b0c87SRobert Watson 			ICMPSTAT_INC(icps_bmcastecho);
5397022ea0aSGarrett Wollman 			break;
5407022ea0aSGarrett Wollman 		}
541df8bae1dSRodney W. Grimes 		icp->icmp_type = ICMP_ECHOREPLY;
542a57815efSBosko Milekic 		if (badport_bandlim(BANDLIM_ICMP_ECHO) < 0)
54309f81a46SBosko Milekic 			goto freeit;
54409f81a46SBosko Milekic 		else
545df8bae1dSRodney W. Grimes 			goto reflect;
546df8bae1dSRodney W. Grimes 
547df8bae1dSRodney W. Grimes 	case ICMP_TSTAMP:
5488b615593SMarko Zec 		if (!V_icmpbmcastecho
549d311884fSDavid Greenman 		    && (m->m_flags & (M_MCAST | M_BCAST)) != 0) {
550e27b0c87SRobert Watson 			ICMPSTAT_INC(icps_bmcasttstamp);
551fe0fb8abSGarrett Wollman 			break;
552fe0fb8abSGarrett Wollman 		}
553df8bae1dSRodney W. Grimes 		if (icmplen < ICMP_TSLEN) {
554e27b0c87SRobert Watson 			ICMPSTAT_INC(icps_badlen);
555df8bae1dSRodney W. Grimes 			break;
556df8bae1dSRodney W. Grimes 		}
557df8bae1dSRodney W. Grimes 		icp->icmp_type = ICMP_TSTAMPREPLY;
558df8bae1dSRodney W. Grimes 		icp->icmp_rtime = iptime();
559df8bae1dSRodney W. Grimes 		icp->icmp_ttime = icp->icmp_rtime;	/* bogus, do later! */
560a57815efSBosko Milekic 		if (badport_bandlim(BANDLIM_ICMP_TSTAMP) < 0)
56109f81a46SBosko Milekic 			goto freeit;
56209f81a46SBosko Milekic 		else
563df8bae1dSRodney W. Grimes 			goto reflect;
564df8bae1dSRodney W. Grimes 
565df8bae1dSRodney W. Grimes 	case ICMP_MASKREQ:
5668b615593SMarko Zec 		if (V_icmpmaskrepl == 0)
567df8bae1dSRodney W. Grimes 			break;
568df8bae1dSRodney W. Grimes 		/*
569df8bae1dSRodney W. Grimes 		 * We are not able to respond with all ones broadcast
570df8bae1dSRodney W. Grimes 		 * unless we receive it over a point-to-point interface.
571df8bae1dSRodney W. Grimes 		 */
572df8bae1dSRodney W. Grimes 		if (icmplen < ICMP_MASKLEN)
573df8bae1dSRodney W. Grimes 			break;
574df8bae1dSRodney W. Grimes 		switch (ip->ip_dst.s_addr) {
575df8bae1dSRodney W. Grimes 
576df8bae1dSRodney W. Grimes 		case INADDR_BROADCAST:
577df8bae1dSRodney W. Grimes 		case INADDR_ANY:
578df8bae1dSRodney W. Grimes 			icmpdst.sin_addr = ip->ip_src;
579df8bae1dSRodney W. Grimes 			break;
580df8bae1dSRodney W. Grimes 
581df8bae1dSRodney W. Grimes 		default:
582df8bae1dSRodney W. Grimes 			icmpdst.sin_addr = ip->ip_dst;
583df8bae1dSRodney W. Grimes 		}
584df8bae1dSRodney W. Grimes 		ia = (struct in_ifaddr *)ifaof_ifpforaddr(
585df8bae1dSRodney W. Grimes 			    (struct sockaddr *)&icmpdst, m->m_pkthdr.rcvif);
5868c0fec80SRobert Watson 		if (ia == NULL)
587df8bae1dSRodney W. Grimes 			break;
5888c0fec80SRobert Watson 		if (ia->ia_ifp == NULL) {
5898c0fec80SRobert Watson 			ifa_free(&ia->ia_ifa);
5907e6f7714SPoul-Henning Kamp 			break;
5918c0fec80SRobert Watson 		}
592df8bae1dSRodney W. Grimes 		icp->icmp_type = ICMP_MASKREPLY;
5938b615593SMarko Zec 		if (V_icmpmaskfake == 0)
594df8bae1dSRodney W. Grimes 			icp->icmp_mask = ia->ia_sockmask.sin_addr.s_addr;
59557842a38SMatthew N. Dodd 		else
5968b615593SMarko Zec 			icp->icmp_mask = V_icmpmaskfake;
597df8bae1dSRodney W. Grimes 		if (ip->ip_src.s_addr == 0) {
598df8bae1dSRodney W. Grimes 			if (ia->ia_ifp->if_flags & IFF_BROADCAST)
599df8bae1dSRodney W. Grimes 			    ip->ip_src = satosin(&ia->ia_broadaddr)->sin_addr;
600df8bae1dSRodney W. Grimes 			else if (ia->ia_ifp->if_flags & IFF_POINTOPOINT)
601df8bae1dSRodney W. Grimes 			    ip->ip_src = satosin(&ia->ia_dstaddr)->sin_addr;
602df8bae1dSRodney W. Grimes 		}
6038c0fec80SRobert Watson 		ifa_free(&ia->ia_ifa);
604df8bae1dSRodney W. Grimes reflect:
605e27b0c87SRobert Watson 		ICMPSTAT_INC(icps_reflect);
606e27b0c87SRobert Watson 		ICMPSTAT_INC(icps_outhist[icp->icmp_type]);
607df8bae1dSRodney W. Grimes 		icmp_reflect(m);
608*8f5a8818SKevin Lo 		return (IPPROTO_DONE);
609df8bae1dSRodney W. Grimes 
610df8bae1dSRodney W. Grimes 	case ICMP_REDIRECT:
6118b615593SMarko Zec 		if (V_log_redirect) {
61218d3153eSDag-Erling Smørgrav 			u_long src, dst, gw;
61318d3153eSDag-Erling Smørgrav 
61418d3153eSDag-Erling Smørgrav 			src = ntohl(ip->ip_src.s_addr);
61518d3153eSDag-Erling Smørgrav 			dst = ntohl(icp->icmp_ip.ip_dst.s_addr);
61618d3153eSDag-Erling Smørgrav 			gw = ntohl(icp->icmp_gwaddr.s_addr);
61718d3153eSDag-Erling Smørgrav 			printf("icmp redirect from %d.%d.%d.%d: "
61818d3153eSDag-Erling Smørgrav 			       "%d.%d.%d.%d => %d.%d.%d.%d\n",
61918d3153eSDag-Erling Smørgrav 			       (int)(src >> 24), (int)((src >> 16) & 0xff),
62018d3153eSDag-Erling Smørgrav 			       (int)((src >> 8) & 0xff), (int)(src & 0xff),
62118d3153eSDag-Erling Smørgrav 			       (int)(dst >> 24), (int)((dst >> 16) & 0xff),
62218d3153eSDag-Erling Smørgrav 			       (int)((dst >> 8) & 0xff), (int)(dst & 0xff),
62318d3153eSDag-Erling Smørgrav 			       (int)(gw >> 24), (int)((gw >> 16) & 0xff),
62418d3153eSDag-Erling Smørgrav 			       (int)((gw >> 8) & 0xff), (int)(gw & 0xff));
62518d3153eSDag-Erling Smørgrav 		}
62687c3bd27SAndre Oppermann 		/*
62787c3bd27SAndre Oppermann 		 * RFC1812 says we must ignore ICMP redirects if we
62887c3bd27SAndre Oppermann 		 * are acting as router.
62987c3bd27SAndre Oppermann 		 */
6308b615593SMarko Zec 		if (V_drop_redirect || V_ipforwarding)
63118d3153eSDag-Erling Smørgrav 			break;
632df8bae1dSRodney W. Grimes 		if (code > 3)
633df8bae1dSRodney W. Grimes 			goto badcode;
634df8bae1dSRodney W. Grimes 		if (icmplen < ICMP_ADVLENMIN || icmplen < ICMP_ADVLEN(icp) ||
63553be11f6SPoul-Henning Kamp 		    icp->icmp_ip.ip_hl < (sizeof(struct ip) >> 2)) {
636e27b0c87SRobert Watson 			ICMPSTAT_INC(icps_badlen);
637df8bae1dSRodney W. Grimes 			break;
638df8bae1dSRodney W. Grimes 		}
639df8bae1dSRodney W. Grimes 		/*
640df8bae1dSRodney W. Grimes 		 * Short circuit routing redirects to force
641df8bae1dSRodney W. Grimes 		 * immediate change in the kernel's routing
642df8bae1dSRodney W. Grimes 		 * tables.  The message is also handed to anyone
643df8bae1dSRodney W. Grimes 		 * listening on a raw socket (e.g. the routing
644df8bae1dSRodney W. Grimes 		 * daemon for use in updating its tables).
645df8bae1dSRodney W. Grimes 		 */
646df8bae1dSRodney W. Grimes 		icmpgw.sin_addr = ip->ip_src;
647df8bae1dSRodney W. Grimes 		icmpdst.sin_addr = icp->icmp_gwaddr;
648df8bae1dSRodney W. Grimes #ifdef	ICMPPRINTFS
6492b758395SGarrett Wollman 		if (icmpprintfs) {
6502b758395SGarrett Wollman 			char buf[4 * sizeof "123"];
6512b758395SGarrett Wollman 			strcpy(buf, inet_ntoa(icp->icmp_ip.ip_dst));
6522b758395SGarrett Wollman 
6532b758395SGarrett Wollman 			printf("redirect dst %s to %s\n",
6542b758395SGarrett Wollman 			       buf, inet_ntoa(icp->icmp_gwaddr));
6552b758395SGarrett Wollman 		}
656df8bae1dSRodney W. Grimes #endif
657df8bae1dSRodney W. Grimes 		icmpsrc.sin_addr = icp->icmp_ip.ip_dst;
6588b07e49aSJulian Elischer 		for ( fibnum = 0; fibnum < rt_numfibs; fibnum++) {
6598b07e49aSJulian Elischer 			in_rtredirect((struct sockaddr *)&icmpsrc,
660df8bae1dSRodney W. Grimes 			  (struct sockaddr *)&icmpdst,
661df8bae1dSRodney W. Grimes 			  (struct sockaddr *)0, RTF_GATEWAY | RTF_HOST,
6628b07e49aSJulian Elischer 			  (struct sockaddr *)&icmpgw, fibnum);
6638b07e49aSJulian Elischer 		}
664df8bae1dSRodney W. Grimes 		pfctlinput(PRC_REDIRECT_HOST, (struct sockaddr *)&icmpsrc);
665b2630c29SGeorge V. Neville-Neil #ifdef IPSEC
6666a800098SYoshinobu Inoue 		key_sa_routechange((struct sockaddr *)&icmpsrc);
6676a800098SYoshinobu Inoue #endif
668df8bae1dSRodney W. Grimes 		break;
669df8bae1dSRodney W. Grimes 
670df8bae1dSRodney W. Grimes 	/*
671df8bae1dSRodney W. Grimes 	 * No kernel processing for the following;
672df8bae1dSRodney W. Grimes 	 * just fall through to send to raw listener.
673df8bae1dSRodney W. Grimes 	 */
674df8bae1dSRodney W. Grimes 	case ICMP_ECHOREPLY:
675df8bae1dSRodney W. Grimes 	case ICMP_ROUTERADVERT:
676df8bae1dSRodney W. Grimes 	case ICMP_ROUTERSOLICIT:
677df8bae1dSRodney W. Grimes 	case ICMP_TSTAMPREPLY:
678df8bae1dSRodney W. Grimes 	case ICMP_IREQREPLY:
679df8bae1dSRodney W. Grimes 	case ICMP_MASKREPLY:
680df8bae1dSRodney W. Grimes 	default:
681df8bae1dSRodney W. Grimes 		break;
682df8bae1dSRodney W. Grimes 	}
683df8bae1dSRodney W. Grimes 
684df8bae1dSRodney W. Grimes raw:
685*8f5a8818SKevin Lo 	*mp = m;
686*8f5a8818SKevin Lo 	rip_input(mp, offp, proto);
687*8f5a8818SKevin Lo 	return (IPPROTO_DONE);
688df8bae1dSRodney W. Grimes 
689df8bae1dSRodney W. Grimes freeit:
690df8bae1dSRodney W. Grimes 	m_freem(m);
691*8f5a8818SKevin Lo 	return (IPPROTO_DONE);
692df8bae1dSRodney W. Grimes }
693df8bae1dSRodney W. Grimes 
694df8bae1dSRodney W. Grimes /*
695df8bae1dSRodney W. Grimes  * Reflect the ip packet back to the source
696df8bae1dSRodney W. Grimes  */
6970312fbe9SPoul-Henning Kamp static void
698f2565d68SRobert Watson icmp_reflect(struct mbuf *m)
699df8bae1dSRodney W. Grimes {
700ca925d9cSJonathan Lemon 	struct ip *ip = mtod(m, struct ip *);
701ca925d9cSJonathan Lemon 	struct ifaddr *ifa;
70233c4f96dSRobert Watson 	struct ifnet *ifp;
703ca925d9cSJonathan Lemon 	struct in_ifaddr *ia;
704df8bae1dSRodney W. Grimes 	struct in_addr t;
705b5e8ce9fSBruce Evans 	struct mbuf *opts = 0;
70653be11f6SPoul-Henning Kamp 	int optlen = (ip->ip_hl << 2) - sizeof(struct ip);
707df8bae1dSRodney W. Grimes 
7086b9ff6b7SGeorge V. Neville-Neil 	if (IN_MULTICAST(ntohl(ip->ip_src.s_addr)) ||
7096b9ff6b7SGeorge V. Neville-Neil 	    IN_EXPERIMENTAL(ntohl(ip->ip_src.s_addr)) ||
7106b9ff6b7SGeorge V. Neville-Neil 	    IN_ZERONET(ntohl(ip->ip_src.s_addr)) ) {
711df8bae1dSRodney W. Grimes 		m_freem(m);	/* Bad return address */
712e27b0c87SRobert Watson 		ICMPSTAT_INC(icps_badaddr);
713df8bae1dSRodney W. Grimes 		goto done;	/* Ip_output() will check for broadcast */
714df8bae1dSRodney W. Grimes 	}
7156b9ff6b7SGeorge V. Neville-Neil 
716df8bae1dSRodney W. Grimes 	t = ip->ip_dst;
717df8bae1dSRodney W. Grimes 	ip->ip_dst = ip->ip_src;
7181488eac8SAndre Oppermann 
719df8bae1dSRodney W. Grimes 	/*
7201488eac8SAndre Oppermann 	 * Source selection for ICMP replies:
7211488eac8SAndre Oppermann 	 *
7221488eac8SAndre Oppermann 	 * If the incoming packet was addressed directly to one of our
7231488eac8SAndre Oppermann 	 * own addresses, use dst as the src for the reply.
724df8bae1dSRodney W. Grimes 	 */
7252d9cfabaSRobert Watson 	IN_IFADDR_RLOCK();
72633c4f96dSRobert Watson 	LIST_FOREACH(ia, INADDR_HASH(t.s_addr), ia_hash) {
72733c4f96dSRobert Watson 		if (t.s_addr == IA_SIN(ia)->sin_addr.s_addr) {
72833c4f96dSRobert Watson 			t = IA_SIN(ia)->sin_addr;
7292d9cfabaSRobert Watson 			IN_IFADDR_RUNLOCK();
730ca925d9cSJonathan Lemon 			goto match;
73133c4f96dSRobert Watson 		}
73233c4f96dSRobert Watson 	}
7332d9cfabaSRobert Watson 	IN_IFADDR_RUNLOCK();
7342d9cfabaSRobert Watson 
7351488eac8SAndre Oppermann 	/*
7361488eac8SAndre Oppermann 	 * If the incoming packet was addressed to one of our broadcast
7371488eac8SAndre Oppermann 	 * addresses, use the first non-broadcast address which corresponds
7381488eac8SAndre Oppermann 	 * to the incoming interface.
7391488eac8SAndre Oppermann 	 */
74033c4f96dSRobert Watson 	ifp = m->m_pkthdr.rcvif;
74133c4f96dSRobert Watson 	if (ifp != NULL && ifp->if_flags & IFF_BROADCAST) {
742137f91e8SJohn Baldwin 		IF_ADDR_RLOCK(ifp);
74333c4f96dSRobert Watson 		TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
744ca925d9cSJonathan Lemon 			if (ifa->ifa_addr->sa_family != AF_INET)
745ca925d9cSJonathan Lemon 				continue;
746ca925d9cSJonathan Lemon 			ia = ifatoia(ifa);
747ca925d9cSJonathan Lemon 			if (satosin(&ia->ia_broadaddr)->sin_addr.s_addr ==
74833c4f96dSRobert Watson 			    t.s_addr) {
74933c4f96dSRobert Watson 				t = IA_SIN(ia)->sin_addr;
750137f91e8SJohn Baldwin 				IF_ADDR_RUNLOCK(ifp);
751ca925d9cSJonathan Lemon 				goto match;
752df8bae1dSRodney W. Grimes 			}
753ca925d9cSJonathan Lemon 		}
754137f91e8SJohn Baldwin 		IF_ADDR_RUNLOCK(ifp);
75533c4f96dSRobert Watson 	}
7561488eac8SAndre Oppermann 	/*
757a0866c8dSAndre Oppermann 	 * If the packet was transiting through us, use the address of
758a0866c8dSAndre Oppermann 	 * the interface the packet came through in.  If that interface
759a0866c8dSAndre Oppermann 	 * doesn't have a suitable IP address, the normal selection
760a0866c8dSAndre Oppermann 	 * criteria apply.
761a0866c8dSAndre Oppermann 	 */
76233c4f96dSRobert Watson 	if (V_icmp_rfi && ifp != NULL) {
763137f91e8SJohn Baldwin 		IF_ADDR_RLOCK(ifp);
76433c4f96dSRobert Watson 		TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
765a0866c8dSAndre Oppermann 			if (ifa->ifa_addr->sa_family != AF_INET)
766a0866c8dSAndre Oppermann 				continue;
767a0866c8dSAndre Oppermann 			ia = ifatoia(ifa);
76833c4f96dSRobert Watson 			t = IA_SIN(ia)->sin_addr;
769137f91e8SJohn Baldwin 			IF_ADDR_RUNLOCK(ifp);
770a0866c8dSAndre Oppermann 			goto match;
771a0866c8dSAndre Oppermann 		}
772137f91e8SJohn Baldwin 		IF_ADDR_RUNLOCK(ifp);
773a0866c8dSAndre Oppermann 	}
774a0866c8dSAndre Oppermann 	/*
775b74d89bbSAndre Oppermann 	 * If the incoming packet was not addressed directly to us, use
776b74d89bbSAndre Oppermann 	 * designated interface for icmp replies specified by sysctl
777b74d89bbSAndre Oppermann 	 * net.inet.icmp.reply_src (default not set). Otherwise continue
778b74d89bbSAndre Oppermann 	 * with normal source selection.
779b74d89bbSAndre Oppermann 	 */
78033c4f96dSRobert Watson 	if (V_reply_src[0] != '\0' && (ifp = ifunit(V_reply_src))) {
781137f91e8SJohn Baldwin 		IF_ADDR_RLOCK(ifp);
78233c4f96dSRobert Watson 		TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
783b74d89bbSAndre Oppermann 			if (ifa->ifa_addr->sa_family != AF_INET)
784b74d89bbSAndre Oppermann 				continue;
785b74d89bbSAndre Oppermann 			ia = ifatoia(ifa);
78633c4f96dSRobert Watson 			t = IA_SIN(ia)->sin_addr;
787137f91e8SJohn Baldwin 			IF_ADDR_RUNLOCK(ifp);
788b74d89bbSAndre Oppermann 			goto match;
789b74d89bbSAndre Oppermann 		}
790137f91e8SJohn Baldwin 		IF_ADDR_RUNLOCK(ifp);
791b74d89bbSAndre Oppermann 	}
792b74d89bbSAndre Oppermann 	/*
7931488eac8SAndre Oppermann 	 * If the packet was transiting through us, use the address of
7941488eac8SAndre Oppermann 	 * the interface that is the closest to the packet source.
7951488eac8SAndre Oppermann 	 * When we don't have a route back to the packet source, stop here
7961488eac8SAndre Oppermann 	 * and drop the packet.
7971488eac8SAndre Oppermann 	 */
7988b07e49aSJulian Elischer 	ia = ip_rtaddr(ip->ip_dst, M_GETFIB(m));
7990d4bef5dSDima Dorfman 	if (ia == NULL) {
8000d4bef5dSDima Dorfman 		m_freem(m);
801e27b0c87SRobert Watson 		ICMPSTAT_INC(icps_noroute);
8020d4bef5dSDima Dorfman 		goto done;
8030d4bef5dSDima Dorfman 	}
80433c4f96dSRobert Watson 	t = IA_SIN(ia)->sin_addr;
8058c0fec80SRobert Watson 	ifa_free(&ia->ia_ifa);
806ca925d9cSJonathan Lemon match:
807baee0c3eSRobert Watson #ifdef MAC
808a13e21f7SRobert Watson 	mac_netinet_icmp_replyinplace(m);
809baee0c3eSRobert Watson #endif
810df8bae1dSRodney W. Grimes 	ip->ip_src = t;
811603724d3SBjoern A. Zeeb 	ip->ip_ttl = V_ip_defttl;
812df8bae1dSRodney W. Grimes 
813df8bae1dSRodney W. Grimes 	if (optlen > 0) {
814df8bae1dSRodney W. Grimes 		register u_char *cp;
815df8bae1dSRodney W. Grimes 		int opt, cnt;
816df8bae1dSRodney W. Grimes 		u_int len;
817df8bae1dSRodney W. Grimes 
818df8bae1dSRodney W. Grimes 		/*
819df8bae1dSRodney W. Grimes 		 * Retrieve any source routing from the incoming packet;
820df8bae1dSRodney W. Grimes 		 * add on any record-route or timestamp options.
821df8bae1dSRodney W. Grimes 		 */
822df8bae1dSRodney W. Grimes 		cp = (u_char *) (ip + 1);
823e0982661SAndre Oppermann 		if ((opts = ip_srcroute(m)) == 0 &&
824eb1b1807SGleb Smirnoff 		    (opts = m_gethdr(M_NOWAIT, MT_DATA))) {
825df8bae1dSRodney W. Grimes 			opts->m_len = sizeof(struct in_addr);
826df8bae1dSRodney W. Grimes 			mtod(opts, struct in_addr *)->s_addr = 0;
827df8bae1dSRodney W. Grimes 		}
828df8bae1dSRodney W. Grimes 		if (opts) {
829df8bae1dSRodney W. Grimes #ifdef ICMPPRINTFS
830df8bae1dSRodney W. Grimes 		    if (icmpprintfs)
831df8bae1dSRodney W. Grimes 			    printf("icmp_reflect optlen %d rt %d => ",
832df8bae1dSRodney W. Grimes 				optlen, opts->m_len);
833df8bae1dSRodney W. Grimes #endif
834df8bae1dSRodney W. Grimes 		    for (cnt = optlen; cnt > 0; cnt -= len, cp += len) {
835df8bae1dSRodney W. Grimes 			    opt = cp[IPOPT_OPTVAL];
836df8bae1dSRodney W. Grimes 			    if (opt == IPOPT_EOL)
837df8bae1dSRodney W. Grimes 				    break;
838df8bae1dSRodney W. Grimes 			    if (opt == IPOPT_NOP)
839df8bae1dSRodney W. Grimes 				    len = 1;
840df8bae1dSRodney W. Grimes 			    else {
841707d00a3SJonathan Lemon 				    if (cnt < IPOPT_OLEN + sizeof(*cp))
842707d00a3SJonathan Lemon 					    break;
843df8bae1dSRodney W. Grimes 				    len = cp[IPOPT_OLEN];
844707d00a3SJonathan Lemon 				    if (len < IPOPT_OLEN + sizeof(*cp) ||
845707d00a3SJonathan Lemon 				        len > cnt)
846df8bae1dSRodney W. Grimes 					    break;
847df8bae1dSRodney W. Grimes 			    }
848df8bae1dSRodney W. Grimes 			    /*
849df8bae1dSRodney W. Grimes 			     * Should check for overflow, but it "can't happen"
850df8bae1dSRodney W. Grimes 			     */
851df8bae1dSRodney W. Grimes 			    if (opt == IPOPT_RR || opt == IPOPT_TS ||
852df8bae1dSRodney W. Grimes 				opt == IPOPT_SECURITY) {
853df8bae1dSRodney W. Grimes 				    bcopy((caddr_t)cp,
854df8bae1dSRodney W. Grimes 					mtod(opts, caddr_t) + opts->m_len, len);
855df8bae1dSRodney W. Grimes 				    opts->m_len += len;
856df8bae1dSRodney W. Grimes 			    }
857df8bae1dSRodney W. Grimes 		    }
858df8bae1dSRodney W. Grimes 		    /* Terminate & pad, if necessary */
859623ae52eSPoul-Henning Kamp 		    cnt = opts->m_len % 4;
860623ae52eSPoul-Henning Kamp 		    if (cnt) {
861df8bae1dSRodney W. Grimes 			    for (; cnt < 4; cnt++) {
862df8bae1dSRodney W. Grimes 				    *(mtod(opts, caddr_t) + opts->m_len) =
863df8bae1dSRodney W. Grimes 					IPOPT_EOL;
864df8bae1dSRodney W. Grimes 				    opts->m_len++;
865df8bae1dSRodney W. Grimes 			    }
866df8bae1dSRodney W. Grimes 		    }
867df8bae1dSRodney W. Grimes #ifdef ICMPPRINTFS
868df8bae1dSRodney W. Grimes 		    if (icmpprintfs)
869df8bae1dSRodney W. Grimes 			    printf("%d\n", opts->m_len);
870df8bae1dSRodney W. Grimes #endif
871df8bae1dSRodney W. Grimes 		}
8729e2a372fSGleb Smirnoff 		ip_stripoptions(m);
873df8bae1dSRodney W. Grimes 	}
8749c855a36SSam Leffler 	m_tag_delete_nonpersistent(m);
875df8bae1dSRodney W. Grimes 	m->m_flags &= ~(M_BCAST|M_MCAST);
87602c1c707SAndre Oppermann 	icmp_send(m, opts);
877df8bae1dSRodney W. Grimes done:
878df8bae1dSRodney W. Grimes 	if (opts)
879df8bae1dSRodney W. Grimes 		(void)m_free(opts);
880df8bae1dSRodney W. Grimes }
881df8bae1dSRodney W. Grimes 
882df8bae1dSRodney W. Grimes /*
883df8bae1dSRodney W. Grimes  * Send an icmp packet back to the ip level,
884df8bae1dSRodney W. Grimes  * after supplying a checksum.
885df8bae1dSRodney W. Grimes  */
8860312fbe9SPoul-Henning Kamp static void
887f2565d68SRobert Watson icmp_send(struct mbuf *m, struct mbuf *opts)
888df8bae1dSRodney W. Grimes {
889df8bae1dSRodney W. Grimes 	register struct ip *ip = mtod(m, struct ip *);
890df8bae1dSRodney W. Grimes 	register int hlen;
891df8bae1dSRodney W. Grimes 	register struct icmp *icp;
892df8bae1dSRodney W. Grimes 
89353be11f6SPoul-Henning Kamp 	hlen = ip->ip_hl << 2;
894df8bae1dSRodney W. Grimes 	m->m_data += hlen;
895df8bae1dSRodney W. Grimes 	m->m_len -= hlen;
896df8bae1dSRodney W. Grimes 	icp = mtod(m, struct icmp *);
897df8bae1dSRodney W. Grimes 	icp->icmp_cksum = 0;
8988f134647SGleb Smirnoff 	icp->icmp_cksum = in_cksum(m, ntohs(ip->ip_len) - hlen);
899df8bae1dSRodney W. Grimes 	m->m_data -= hlen;
900df8bae1dSRodney W. Grimes 	m->m_len += hlen;
90194446a2eSArchie Cobbs 	m->m_pkthdr.rcvif = (struct ifnet *)0;
902df8bae1dSRodney W. Grimes #ifdef ICMPPRINTFS
9032b758395SGarrett Wollman 	if (icmpprintfs) {
9042b758395SGarrett Wollman 		char buf[4 * sizeof "123"];
9052b758395SGarrett Wollman 		strcpy(buf, inet_ntoa(ip->ip_dst));
9062b758395SGarrett Wollman 		printf("icmp_send dst %s src %s\n",
9072b758395SGarrett Wollman 		       buf, inet_ntoa(ip->ip_src));
9082b758395SGarrett Wollman 	}
909df8bae1dSRodney W. Grimes #endif
91002c1c707SAndre Oppermann 	(void) ip_output(m, opts, NULL, 0, NULL, NULL);
911df8bae1dSRodney W. Grimes }
912df8bae1dSRodney W. Grimes 
913d685b6eeSLuigi Rizzo /*
914d685b6eeSLuigi Rizzo  * Return milliseconds since 00:00 GMT in network format.
915d685b6eeSLuigi Rizzo  */
916d685b6eeSLuigi Rizzo uint32_t
917f2565d68SRobert Watson iptime(void)
918df8bae1dSRodney W. Grimes {
919df8bae1dSRodney W. Grimes 	struct timeval atv;
920df8bae1dSRodney W. Grimes 	u_long t;
921df8bae1dSRodney W. Grimes 
92216cd6db0SBill Fumerola 	getmicrotime(&atv);
923df8bae1dSRodney W. Grimes 	t = (atv.tv_sec % (24*60*60)) * 1000 + atv.tv_usec / 1000;
924df8bae1dSRodney W. Grimes 	return (htonl(t));
925df8bae1dSRodney W. Grimes }
926df8bae1dSRodney W. Grimes 
9275cbf3e08SGarrett Wollman /*
9285cbf3e08SGarrett Wollman  * Return the next larger or smaller MTU plateau (table from RFC 1191)
9295cbf3e08SGarrett Wollman  * given current value MTU.  If DIR is less than zero, a larger plateau
9305cbf3e08SGarrett Wollman  * is returned; otherwise, a smaller value is returned.
9315cbf3e08SGarrett Wollman  */
9321aedbd9cSAndre Oppermann int
933f2565d68SRobert Watson ip_next_mtu(int mtu, int dir)
9345cbf3e08SGarrett Wollman {
9355cbf3e08SGarrett Wollman 	static int mtutab[] = {
9364c037f8dSAndre Oppermann 		65535, 32000, 17914, 8166, 4352, 2002, 1492, 1280, 1006, 508,
9374c037f8dSAndre Oppermann 		296, 68, 0
9385cbf3e08SGarrett Wollman 	};
93906003a1eSAndre Oppermann 	int i, size;
9405cbf3e08SGarrett Wollman 
94106003a1eSAndre Oppermann 	size = (sizeof mtutab) / (sizeof mtutab[0]);
94206003a1eSAndre Oppermann 	if (dir >= 0) {
9431c0b0f52SGleb Smirnoff 		for (i = 0; i < size; i++)
94406003a1eSAndre Oppermann 			if (mtu > mtutab[i])
9455cbf3e08SGarrett Wollman 				return mtutab[i];
9465cbf3e08SGarrett Wollman 	} else {
94706003a1eSAndre Oppermann 		for (i = size - 1; i >= 0; i--)
94806003a1eSAndre Oppermann 			if (mtu < mtutab[i])
94906003a1eSAndre Oppermann 				return mtutab[i];
95006003a1eSAndre Oppermann 		if (mtu == mtutab[0])
95106003a1eSAndre Oppermann 			return mtutab[0];
9525cbf3e08SGarrett Wollman 	}
95306003a1eSAndre Oppermann 	return 0;
9545cbf3e08SGarrett Wollman }
955b8e463e6SBjoern A. Zeeb #endif /* INET */
95651508de1SMatthew Dillon 
95751508de1SMatthew Dillon 
95851508de1SMatthew Dillon /*
95951508de1SMatthew Dillon  * badport_bandlim() - check for ICMP bandwidth limit
96051508de1SMatthew Dillon  *
96151508de1SMatthew Dillon  *	Return 0 if it is ok to send an ICMP error response, -1 if we have
96251508de1SMatthew Dillon  *	hit our bandwidth limit and it is not ok.
96351508de1SMatthew Dillon  *
96451508de1SMatthew Dillon  *	If icmplim is <= 0, the feature is disabled and 0 is returned.
96551508de1SMatthew Dillon  *
96651508de1SMatthew Dillon  *	For now we separate the TCP and UDP subsystems w/ different 'which'
96751508de1SMatthew Dillon  *	values.  We may eventually remove this separation (and simplify the
96851508de1SMatthew Dillon  *	code further).
96951508de1SMatthew Dillon  *
97051508de1SMatthew Dillon  *	Note that the printing of the error message is delayed so we can
97151508de1SMatthew Dillon  *	properly print the icmp error rate that the system was trying to do
97251508de1SMatthew Dillon  *	(i.e. 22000/100 pps, etc...).  This can cause long delays in printing
97351508de1SMatthew Dillon  *	the 'final' error, but it doesn't make sense to solve the printing
97451508de1SMatthew Dillon  *	delay with more complex code.
97551508de1SMatthew Dillon  */
97651508de1SMatthew Dillon 
97751508de1SMatthew Dillon int
97851508de1SMatthew Dillon badport_bandlim(int which)
97951508de1SMatthew Dillon {
9808b615593SMarko Zec 
98100f21882SSam Leffler #define	N(a)	(sizeof (a) / sizeof (a[0]))
98200f21882SSam Leffler 	static struct rate {
98300f21882SSam Leffler 		const char	*type;
98400f21882SSam Leffler 		struct timeval	lasttime;
985439dfb0cSStefan Farfeleder 		int		curpps;
98600f21882SSam Leffler 	} rates[BANDLIM_MAX+1] = {
98700f21882SSam Leffler 		{ "icmp unreach response" },
98800f21882SSam Leffler 		{ "icmp ping response" },
98900f21882SSam Leffler 		{ "icmp tstamp response" },
99000f21882SSam Leffler 		{ "closed port RST response" },
99108af97b7SRobert Watson 		{ "open port RST response" },
9925ad9e57bSMichael Tuexen 		{ "icmp6 unreach response" },
9935ad9e57bSMichael Tuexen 		{ "sctp ootb response" }
99409f81a46SBosko Milekic 	};
99551508de1SMatthew Dillon 
99651508de1SMatthew Dillon 	/*
99700f21882SSam Leffler 	 * Return ok status if feature disabled or argument out of range.
99851508de1SMatthew Dillon 	 */
9998b615593SMarko Zec 	if (V_icmplim > 0 && (u_int) which < N(rates)) {
100000f21882SSam Leffler 		struct rate *r = &rates[which];
100100f21882SSam Leffler 		int opps = r->curpps;
100251508de1SMatthew Dillon 
10038b615593SMarko Zec 		if (!ppsratecheck(&r->lasttime, &r->curpps, V_icmplim))
100400f21882SSam Leffler 			return -1;	/* discard packet */
100551508de1SMatthew Dillon 		/*
100600f21882SSam Leffler 		 * If we've dropped below the threshold after having
100700f21882SSam Leffler 		 * rate-limited traffic print the message.  This preserves
100800f21882SSam Leffler 		 * the previous behaviour at the expense of added complexity.
100951508de1SMatthew Dillon 		 */
10108b615593SMarko Zec 		if (V_icmplim_output && opps > V_icmplim)
101140fe9effSAndre Oppermann 			log(LOG_NOTICE, "Limiting %s from %d to %d packets/sec\n",
10128b615593SMarko Zec 				r->type, opps, V_icmplim);
101351508de1SMatthew Dillon 	}
101400f21882SSam Leffler 	return 0;			/* okay to send packet */
101500f21882SSam Leffler #undef N
101651508de1SMatthew Dillon }
1017