xref: /freebsd/sys/netinet/ip_icmp.c (revision 382a6bbcf1ac37ba61723f59f8d16e465cc31bd5)
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.
13fbbd9655SWarner Losh  * 3. Neither the name of the University nor the names of its contributors
14df8bae1dSRodney W. Grimes  *    may be used to endorse or promote products derived from this software
15df8bae1dSRodney W. Grimes  *    without specific prior written permission.
16df8bae1dSRodney W. Grimes  *
17df8bae1dSRodney W. Grimes  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
18df8bae1dSRodney W. Grimes  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19df8bae1dSRodney W. Grimes  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20df8bae1dSRodney W. Grimes  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
21df8bae1dSRodney W. Grimes  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22df8bae1dSRodney W. Grimes  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23df8bae1dSRodney W. Grimes  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24df8bae1dSRodney W. Grimes  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25df8bae1dSRodney W. Grimes  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26df8bae1dSRodney W. Grimes  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27df8bae1dSRodney W. Grimes  * SUCH DAMAGE.
28df8bae1dSRodney W. Grimes  *
29df8bae1dSRodney W. Grimes  *	@(#)ip_icmp.c	8.2 (Berkeley) 1/4/94
30df8bae1dSRodney W. Grimes  */
31df8bae1dSRodney W. Grimes 
324b421e2dSMike Silbersack #include <sys/cdefs.h>
334b421e2dSMike Silbersack __FBSDID("$FreeBSD$");
344b421e2dSMike Silbersack 
35b8e463e6SBjoern A. Zeeb #include "opt_inet.h"
366a800098SYoshinobu Inoue 
37df8bae1dSRodney W. Grimes #include <sys/param.h>
38df8bae1dSRodney W. Grimes #include <sys/systm.h>
39df8bae1dSRodney W. Grimes #include <sys/mbuf.h>
40df8bae1dSRodney W. Grimes #include <sys/protosw.h>
41df8bae1dSRodney W. Grimes #include <sys/socket.h>
42df8bae1dSRodney W. Grimes #include <sys/time.h>
43df8bae1dSRodney W. Grimes #include <sys/kernel.h>
44cc0a3c8cSAndrey V. Elsukov #include <sys/lock.h>
45cc0a3c8cSAndrey V. Elsukov #include <sys/rmlock.h>
46b5e8ce9fSBruce Evans #include <sys/sysctl.h>
4740fe9effSAndre Oppermann #include <sys/syslog.h>
48df8bae1dSRodney W. Grimes 
49df8bae1dSRodney W. Grimes #include <net/if.h>
5076039bc8SGleb Smirnoff #include <net/if_var.h>
519494d596SBrooks Davis #include <net/if_types.h>
52df8bae1dSRodney W. Grimes #include <net/route.h>
53eddfbb76SRobert Watson #include <net/vnet.h>
54df8bae1dSRodney W. Grimes 
55df8bae1dSRodney W. Grimes #include <netinet/in.h>
569977be4aSAlexander V. Chernikov #include <netinet/in_fib.h>
5797d8d152SAndre Oppermann #include <netinet/in_pcb.h>
58df8bae1dSRodney W. Grimes #include <netinet/in_systm.h>
59df8bae1dSRodney W. Grimes #include <netinet/in_var.h>
60df8bae1dSRodney W. Grimes #include <netinet/ip.h>
61df8bae1dSRodney W. Grimes #include <netinet/ip_icmp.h>
62b5e8ce9fSBruce Evans #include <netinet/ip_var.h>
63ef39adf0SAndre Oppermann #include <netinet/ip_options.h>
64f88d0cfeSMichael Tuexen #include <netinet/sctp.h>
6597d8d152SAndre Oppermann #include <netinet/tcp.h>
6697d8d152SAndre Oppermann #include <netinet/tcp_var.h>
6797d8d152SAndre Oppermann #include <netinet/tcpip.h>
68df8bae1dSRodney W. Grimes #include <netinet/icmp_var.h>
69df8bae1dSRodney W. Grimes 
70b8e463e6SBjoern A. Zeeb #ifdef INET
71b9234fafSSam Leffler 
7272a52a35SJonathan Lemon #include <machine/in_cksum.h>
7372a52a35SJonathan Lemon 
74aed55708SRobert Watson #include <security/mac/mac_framework.h>
75b8e463e6SBjoern A. Zeeb #endif /* INET */
76aed55708SRobert Watson 
77df8bae1dSRodney W. Grimes /*
78df8bae1dSRodney W. Grimes  * ICMP routines: error generation, receive packet processing, and
79df8bae1dSRodney W. Grimes  * routines to turnaround packets back to the originator, and
80df8bae1dSRodney W. Grimes  * host table maintenance routines.
81df8bae1dSRodney W. Grimes  */
82b8e463e6SBjoern A. Zeeb static VNET_DEFINE(int, icmplim) = 200;
83b8e463e6SBjoern A. Zeeb #define	V_icmplim			VNET(icmplim)
846df8a710SGleb Smirnoff SYSCTL_INT(_net_inet_icmp, ICMPCTL_ICMPLIM, icmplim, CTLFLAG_VNET | CTLFLAG_RW,
85b8e463e6SBjoern A. Zeeb 	&VNET_NAME(icmplim), 0,
86b8e463e6SBjoern A. Zeeb 	"Maximum number of ICMP responses per second");
87b8e463e6SBjoern A. Zeeb 
88b8e463e6SBjoern A. Zeeb static VNET_DEFINE(int, icmplim_output) = 1;
89b8e463e6SBjoern A. Zeeb #define	V_icmplim_output		VNET(icmplim_output)
906df8a710SGleb Smirnoff SYSCTL_INT(_net_inet_icmp, OID_AUTO, icmplim_output, CTLFLAG_VNET | CTLFLAG_RW,
91b8e463e6SBjoern A. Zeeb 	&VNET_NAME(icmplim_output), 0,
92316efdb3SEitan Adler 	"Enable logging of ICMP response rate limiting");
93b8e463e6SBjoern A. Zeeb 
94b8e463e6SBjoern A. Zeeb #ifdef INET
955b7cb97cSAndrey V. Elsukov VNET_PCPUSTAT_DEFINE(struct icmpstat, icmpstat);
965b7cb97cSAndrey V. Elsukov VNET_PCPUSTAT_SYSINIT(icmpstat);
975b7cb97cSAndrey V. Elsukov SYSCTL_VNET_PCPUSTAT(_net_inet_icmp, ICMPCTL_STATS, stats, struct icmpstat,
985b7cb97cSAndrey V. Elsukov     icmpstat, "ICMP statistics (struct icmpstat, netinet/icmp_var.h)");
995b7cb97cSAndrey V. Elsukov 
1005b7cb97cSAndrey V. Elsukov #ifdef VIMAGE
1015b7cb97cSAndrey V. Elsukov VNET_PCPUSTAT_SYSUNINIT(icmpstat);
1025b7cb97cSAndrey V. Elsukov #endif /* VIMAGE */
103eddfbb76SRobert Watson 
1043e288e62SDimitry Andric static VNET_DEFINE(int, icmpmaskrepl) = 0;
10582cea7e6SBjoern A. Zeeb #define	V_icmpmaskrepl			VNET(icmpmaskrepl)
1066df8a710SGleb Smirnoff SYSCTL_INT(_net_inet_icmp, ICMPCTL_MASKREPL, maskrepl, CTLFLAG_VNET | CTLFLAG_RW,
107eddfbb76SRobert Watson 	&VNET_NAME(icmpmaskrepl), 0,
108e8800f3cSMark Johnston 	"Reply to ICMP Address Mask Request packets");
10957842a38SMatthew N. Dodd 
1103e288e62SDimitry Andric static VNET_DEFINE(u_int, icmpmaskfake) = 0;
11182cea7e6SBjoern A. Zeeb #define	V_icmpmaskfake			VNET(icmpmaskfake)
1126df8a710SGleb Smirnoff SYSCTL_UINT(_net_inet_icmp, OID_AUTO, maskfake, CTLFLAG_VNET | CTLFLAG_RW,
113eddfbb76SRobert Watson 	&VNET_NAME(icmpmaskfake), 0,
114e8800f3cSMark Johnston 	"Fake reply to ICMP Address Mask Request packets");
1150312fbe9SPoul-Henning Kamp 
1163c2824b9SAlexander V. Chernikov VNET_DEFINE(int, drop_redirect) = 0;
117670e8b3bSAlexander V. Chernikov #define	V_drop_redirect			VNET(drop_redirect)
118670e8b3bSAlexander V. Chernikov SYSCTL_INT(_net_inet_icmp, OID_AUTO, drop_redirect, CTLFLAG_VNET | CTLFLAG_RW,
119e8800f3cSMark Johnston 	&VNET_NAME(drop_redirect), 0,
120e8800f3cSMark Johnston 	"Ignore ICMP redirects");
12118d3153eSDag-Erling Smørgrav 
1223e288e62SDimitry Andric static VNET_DEFINE(int, log_redirect) = 0;
12382cea7e6SBjoern A. Zeeb #define	V_log_redirect			VNET(log_redirect)
1246df8a710SGleb Smirnoff SYSCTL_INT(_net_inet_icmp, OID_AUTO, log_redirect, CTLFLAG_VNET | CTLFLAG_RW,
125eddfbb76SRobert Watson 	&VNET_NAME(log_redirect), 0,
126eddfbb76SRobert Watson 	"Log ICMP redirects to the console");
1276c3b5f69SDag-Erling Smørgrav 
1283e288e62SDimitry Andric static VNET_DEFINE(char, reply_src[IFNAMSIZ]);
12982cea7e6SBjoern A. Zeeb #define	V_reply_src			VNET(reply_src)
1306df8a710SGleb Smirnoff SYSCTL_STRING(_net_inet_icmp, OID_AUTO, reply_src, CTLFLAG_VNET | CTLFLAG_RW,
131eddfbb76SRobert Watson 	&VNET_NAME(reply_src), IFNAMSIZ,
132e8800f3cSMark Johnston 	"ICMP reply source for non-local packets");
133b74d89bbSAndre Oppermann 
1343e288e62SDimitry Andric static VNET_DEFINE(int, icmp_rfi) = 0;
13582cea7e6SBjoern A. Zeeb #define	V_icmp_rfi			VNET(icmp_rfi)
1366df8a710SGleb Smirnoff SYSCTL_INT(_net_inet_icmp, OID_AUTO, reply_from_interface, CTLFLAG_VNET | CTLFLAG_RW,
137eddfbb76SRobert Watson 	&VNET_NAME(icmp_rfi), 0,
138eddfbb76SRobert Watson 	"ICMP reply from incoming interface for non-local packets");
139a0866c8dSAndre Oppermann 
1403e288e62SDimitry Andric static VNET_DEFINE(int, icmp_quotelen) = 8;
14182cea7e6SBjoern A. Zeeb #define	V_icmp_quotelen			VNET(icmp_quotelen)
1426df8a710SGleb Smirnoff SYSCTL_INT(_net_inet_icmp, OID_AUTO, quotelen, CTLFLAG_VNET | CTLFLAG_RW,
143eddfbb76SRobert Watson 	&VNET_NAME(icmp_quotelen), 0,
144eddfbb76SRobert Watson 	"Number of bytes from original packet to quote in ICMP reply");
145e875dfb8SAndre Oppermann 
1463e288e62SDimitry Andric static VNET_DEFINE(int, icmpbmcastecho) = 0;
14782cea7e6SBjoern A. Zeeb #define	V_icmpbmcastecho		VNET(icmpbmcastecho)
1486df8a710SGleb Smirnoff SYSCTL_INT(_net_inet_icmp, OID_AUTO, bmcastecho, CTLFLAG_VNET | CTLFLAG_RW,
149eddfbb76SRobert Watson 	&VNET_NAME(icmpbmcastecho), 0,
150e8800f3cSMark Johnston 	"Reply to multicast ICMP Echo Request and Timestamp packets");
1517022ea0aSGarrett Wollman 
15200cb6befSMark Johnston static VNET_DEFINE(int, icmptstamprepl) = 1;
15300cb6befSMark Johnston #define	V_icmptstamprepl		VNET(icmptstamprepl)
15400cb6befSMark Johnston SYSCTL_INT(_net_inet_icmp, OID_AUTO, tstamprepl, CTLFLAG_RW,
155e8800f3cSMark Johnston 	&VNET_NAME(icmptstamprepl), 0,
156e8800f3cSMark Johnston 	"Respond to ICMP Timestamp packets");
15751508de1SMatthew Dillon 
158df8bae1dSRodney W. Grimes #ifdef ICMPPRINTFS
159df8bae1dSRodney W. Grimes int	icmpprintfs = 0;
160df8bae1dSRodney W. Grimes #endif
161df8bae1dSRodney W. Grimes 
1624d77a549SAlfred Perlstein static void	icmp_reflect(struct mbuf *);
16302c1c707SAndre Oppermann static void	icmp_send(struct mbuf *, struct mbuf *);
1640312fbe9SPoul-Henning Kamp 
165df8bae1dSRodney W. Grimes extern	struct protosw inetsw[];
166df8bae1dSRodney W. Grimes 
167df8bae1dSRodney W. Grimes /*
168315e3e38SRobert Watson  * Kernel module interface for updating icmpstat.  The argument is an index
169315e3e38SRobert Watson  * into icmpstat treated as an array of u_long.  While this encodes the
170315e3e38SRobert Watson  * general layout of icmpstat into the caller, it doesn't encode its
171315e3e38SRobert Watson  * location, so that future changes to add, for example, per-CPU stats
172315e3e38SRobert Watson  * support won't cause binary compatibility problems for kernel modules.
173315e3e38SRobert Watson  */
174315e3e38SRobert Watson void
175315e3e38SRobert Watson kmod_icmpstat_inc(int statnum)
176315e3e38SRobert Watson {
177315e3e38SRobert Watson 
1785b7cb97cSAndrey V. Elsukov 	counter_u64_add(VNET(icmpstat)[statnum], 1);
179315e3e38SRobert Watson }
180315e3e38SRobert Watson 
181315e3e38SRobert Watson /*
182df8bae1dSRodney W. Grimes  * Generate an error packet of type error
183df8bae1dSRodney W. Grimes  * in response to bad packet ip.
184df8bae1dSRodney W. Grimes  */
185df8bae1dSRodney W. Grimes void
186d685b6eeSLuigi Rizzo icmp_error(struct mbuf *n, int type, int code, uint32_t dest, int mtu)
187df8bae1dSRodney W. Grimes {
1883e85b721SEd Maste 	struct ip *oip = mtod(n, struct ip *), *nip;
1893e85b721SEd Maste 	unsigned oiphlen = oip->ip_hl << 2;
1903e85b721SEd Maste 	struct icmp *icp;
1913e85b721SEd Maste 	struct mbuf *m;
192e86ebebcSAndre Oppermann 	unsigned icmplen, icmpelen, nlen;
193df8bae1dSRodney W. Grimes 
194e86ebebcSAndre Oppermann 	KASSERT((u_int)type <= ICMP_MAXTYPE, ("%s: illegal ICMP type", __func__));
195df8bae1dSRodney W. Grimes #ifdef ICMPPRINTFS
196df8bae1dSRodney W. Grimes 	if (icmpprintfs)
197623ae52eSPoul-Henning Kamp 		printf("icmp_error(%p, %x, %d)\n", oip, type, code);
198df8bae1dSRodney W. Grimes #endif
199df8bae1dSRodney W. Grimes 	if (type != ICMP_REDIRECT)
200e27b0c87SRobert Watson 		ICMPSTAT_INC(icps_error);
201df8bae1dSRodney W. Grimes 	/*
202e86ebebcSAndre Oppermann 	 * Don't send error:
203e86ebebcSAndre Oppermann 	 *  if the original packet was encrypted.
204e86ebebcSAndre Oppermann 	 *  if not the first fragment of message.
205e86ebebcSAndre Oppermann 	 *  in response to a multicast or broadcast packet.
206e86ebebcSAndre Oppermann 	 *  if the old packet protocol was an ICMP error message.
207df8bae1dSRodney W. Grimes 	 */
208cad1917dSHajimu UMEMOTO 	if (n->m_flags & M_DECRYPTED)
209cad1917dSHajimu UMEMOTO 		goto freeit;
2108f134647SGleb Smirnoff 	if (oip->ip_off & htons(~(IP_MF|IP_DF)))
211df8bae1dSRodney W. Grimes 		goto freeit;
212e86ebebcSAndre Oppermann 	if (n->m_flags & (M_BCAST|M_MCAST))
213e86ebebcSAndre Oppermann 		goto freeit;
214df8bae1dSRodney W. Grimes 	if (oip->ip_p == IPPROTO_ICMP && type != ICMP_REDIRECT &&
215e86ebebcSAndre Oppermann 	  n->m_len >= oiphlen + ICMP_MINLEN &&
216e86ebebcSAndre Oppermann 	  !ICMP_INFOTYPE(((struct icmp *)((caddr_t)oip + oiphlen))->icmp_type)) {
217e27b0c87SRobert Watson 		ICMPSTAT_INC(icps_oldicmp);
218df8bae1dSRodney W. Grimes 		goto freeit;
219df8bae1dSRodney W. Grimes 	}
220e86ebebcSAndre Oppermann 	/* Drop if IP header plus 8 bytes is not contignous in first mbuf. */
221e86ebebcSAndre Oppermann 	if (oiphlen + 8 > n->m_len)
222df8bae1dSRodney W. Grimes 		goto freeit;
223df8bae1dSRodney W. Grimes 	/*
224e86ebebcSAndre Oppermann 	 * Calculate length to quote from original packet and
225e86ebebcSAndre Oppermann 	 * prevent the ICMP mbuf from overflowing.
226a4641f4eSPedro F. Giffuni 	 * Unfortunately this is non-trivial since ip_forward()
227e86ebebcSAndre Oppermann 	 * sends us truncated packets.
228df8bae1dSRodney W. Grimes 	 */
229e86ebebcSAndre Oppermann 	nlen = m_length(n, NULL);
230e86ebebcSAndre Oppermann 	if (oip->ip_p == IPPROTO_TCP) {
231e86ebebcSAndre Oppermann 		struct tcphdr *th;
232e86ebebcSAndre Oppermann 		int tcphlen;
233e86ebebcSAndre Oppermann 
234e86ebebcSAndre Oppermann 		if (oiphlen + sizeof(struct tcphdr) > n->m_len &&
235e86ebebcSAndre Oppermann 		    n->m_next == NULL)
236e86ebebcSAndre Oppermann 			goto stdreply;
237e86ebebcSAndre Oppermann 		if (n->m_len < oiphlen + sizeof(struct tcphdr) &&
238e86ebebcSAndre Oppermann 		    ((n = m_pullup(n, oiphlen + sizeof(struct tcphdr))) == NULL))
239e86ebebcSAndre Oppermann 			goto freeit;
240e86ebebcSAndre Oppermann 		th = (struct tcphdr *)((caddr_t)oip + oiphlen);
241e86ebebcSAndre Oppermann 		tcphlen = th->th_off << 2;
242e86ebebcSAndre Oppermann 		if (tcphlen < sizeof(struct tcphdr))
243e86ebebcSAndre Oppermann 			goto freeit;
2448f134647SGleb Smirnoff 		if (ntohs(oip->ip_len) < oiphlen + tcphlen)
245e86ebebcSAndre Oppermann 			goto freeit;
246e86ebebcSAndre Oppermann 		if (oiphlen + tcphlen > n->m_len && n->m_next == NULL)
247e86ebebcSAndre Oppermann 			goto stdreply;
248e86ebebcSAndre Oppermann 		if (n->m_len < oiphlen + tcphlen &&
249e86ebebcSAndre Oppermann 		    ((n = m_pullup(n, oiphlen + tcphlen)) == NULL))
250e86ebebcSAndre Oppermann 			goto freeit;
2518f134647SGleb Smirnoff 		icmpelen = max(tcphlen, min(V_icmp_quotelen,
2528f134647SGleb Smirnoff 		    ntohs(oip->ip_len) - oiphlen));
253f88d0cfeSMichael Tuexen 	} else if (oip->ip_p == IPPROTO_SCTP) {
254f88d0cfeSMichael Tuexen 		struct sctphdr *sh;
255f88d0cfeSMichael Tuexen 		struct sctp_chunkhdr *ch;
256f88d0cfeSMichael Tuexen 
257f88d0cfeSMichael Tuexen 		if (ntohs(oip->ip_len) < oiphlen + sizeof(struct sctphdr))
258f88d0cfeSMichael Tuexen 			goto stdreply;
259f88d0cfeSMichael Tuexen 		if (oiphlen + sizeof(struct sctphdr) > n->m_len &&
260f88d0cfeSMichael Tuexen 		    n->m_next == NULL)
261f88d0cfeSMichael Tuexen 			goto stdreply;
262f88d0cfeSMichael Tuexen 		if (n->m_len < oiphlen + sizeof(struct sctphdr) &&
263f88d0cfeSMichael Tuexen 		    (n = m_pullup(n, oiphlen + sizeof(struct sctphdr))) == NULL)
264f88d0cfeSMichael Tuexen 			goto freeit;
265f88d0cfeSMichael Tuexen 		icmpelen = max(sizeof(struct sctphdr),
266f88d0cfeSMichael Tuexen 		    min(V_icmp_quotelen, ntohs(oip->ip_len) - oiphlen));
267f88d0cfeSMichael Tuexen 		sh = (struct sctphdr *)((caddr_t)oip + oiphlen);
268f88d0cfeSMichael Tuexen 		if (ntohl(sh->v_tag) == 0 &&
269f88d0cfeSMichael Tuexen 		    ntohs(oip->ip_len) >= oiphlen + sizeof(struct sctphdr) + 8 &&
270f88d0cfeSMichael Tuexen 		    (n->m_len >= oiphlen + sizeof(struct sctphdr) + 8 ||
271f88d0cfeSMichael Tuexen 		     n->m_next != NULL)) {
272f88d0cfeSMichael Tuexen 			if (n->m_len < oiphlen + sizeof(struct sctphdr) + 8 &&
273f88d0cfeSMichael Tuexen 			    (n = m_pullup(n, oiphlen + sizeof(struct sctphdr) + 8)) == NULL)
274f88d0cfeSMichael Tuexen 				goto freeit;
275f88d0cfeSMichael Tuexen 			ch = (struct sctp_chunkhdr *)(sh + 1);
276f88d0cfeSMichael Tuexen 			if (ch->chunk_type == SCTP_INITIATION) {
277f88d0cfeSMichael Tuexen 				icmpelen = max(sizeof(struct sctphdr) + 8,
278f88d0cfeSMichael Tuexen 				    min(V_icmp_quotelen, ntohs(oip->ip_len) - oiphlen));
279f88d0cfeSMichael Tuexen 			}
280f88d0cfeSMichael Tuexen 		}
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  */
3618f5a8818SKevin Lo int
3628f5a8818SKevin 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;
3668f5a8818SKevin Lo 	struct mbuf *m = *mp;
36716d6c90fSAndre Oppermann 	struct ip *ip = mtod(m, struct ip *);
36816d6c90fSAndre Oppermann 	struct sockaddr_in icmpsrc, icmpdst, icmpgw;
3698f5a8818SKevin Lo 	int hlen = *offp;
3708f5a8818SKevin 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 
3758f5a8818SKevin Lo 	*mp = NULL;
3768f5a8818SKevin 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) {
3838144690aSEric van Gyzen 		char srcbuf[INET_ADDRSTRLEN];
3848144690aSEric van Gyzen 		char dstbuf[INET_ADDRSTRLEN];
3858144690aSEric van Gyzen 
3862b758395SGarrett Wollman 		printf("icmp_input from %s to %s, len %d\n",
3878144690aSEric van Gyzen 		    inet_ntoa_r(ip->ip_src, srcbuf),
3888144690aSEric van Gyzen 		    inet_ntoa_r(ip->ip_dst, dstbuf), icmplen);
3892b758395SGarrett Wollman 	}
390df8bae1dSRodney W. Grimes #endif
391df8bae1dSRodney W. Grimes 	if (icmplen < ICMP_MINLEN) {
392e27b0c87SRobert Watson 		ICMPSTAT_INC(icps_tooshort);
393df8bae1dSRodney W. Grimes 		goto freeit;
394df8bae1dSRodney W. Grimes 	}
395df8bae1dSRodney W. Grimes 	i = hlen + min(icmplen, ICMP_ADVLENMIN);
396852da713SBjoern A. Zeeb 	if (m->m_len < i && (m = m_pullup(m, i)) == NULL)  {
397e27b0c87SRobert Watson 		ICMPSTAT_INC(icps_tooshort);
3988f5a8818SKevin Lo 		return (IPPROTO_DONE);
399df8bae1dSRodney W. Grimes 	}
400df8bae1dSRodney W. Grimes 	ip = mtod(m, struct ip *);
401df8bae1dSRodney W. Grimes 	m->m_len -= hlen;
402df8bae1dSRodney W. Grimes 	m->m_data += hlen;
403df8bae1dSRodney W. Grimes 	icp = mtod(m, struct icmp *);
404df8bae1dSRodney W. Grimes 	if (in_cksum(m, icmplen)) {
405e27b0c87SRobert Watson 		ICMPSTAT_INC(icps_checksum);
406df8bae1dSRodney W. Grimes 		goto freeit;
407df8bae1dSRodney W. Grimes 	}
408df8bae1dSRodney W. Grimes 	m->m_len += hlen;
409df8bae1dSRodney W. Grimes 	m->m_data -= hlen;
410df8bae1dSRodney W. Grimes 
411df8bae1dSRodney W. Grimes #ifdef ICMPPRINTFS
412df8bae1dSRodney W. Grimes 	if (icmpprintfs)
413df8bae1dSRodney W. Grimes 		printf("icmp_input, type %d code %d\n", icp->icmp_type,
414df8bae1dSRodney W. Grimes 		    icp->icmp_code);
415df8bae1dSRodney W. Grimes #endif
4165b7ee6edSGarrett Wollman 
4175b7ee6edSGarrett Wollman 	/*
4185b7ee6edSGarrett Wollman 	 * Message type specific processing.
4195b7ee6edSGarrett Wollman 	 */
420df8bae1dSRodney W. Grimes 	if (icp->icmp_type > ICMP_MAXTYPE)
421df8bae1dSRodney W. Grimes 		goto raw;
42216d6c90fSAndre Oppermann 
42316d6c90fSAndre Oppermann 	/* Initialize */
42416d6c90fSAndre Oppermann 	bzero(&icmpsrc, sizeof(icmpsrc));
42516d6c90fSAndre Oppermann 	icmpsrc.sin_len = sizeof(struct sockaddr_in);
42616d6c90fSAndre Oppermann 	icmpsrc.sin_family = AF_INET;
42716d6c90fSAndre Oppermann 	bzero(&icmpdst, sizeof(icmpdst));
42816d6c90fSAndre Oppermann 	icmpdst.sin_len = sizeof(struct sockaddr_in);
42916d6c90fSAndre Oppermann 	icmpdst.sin_family = AF_INET;
43016d6c90fSAndre Oppermann 	bzero(&icmpgw, sizeof(icmpgw));
43116d6c90fSAndre Oppermann 	icmpgw.sin_len = sizeof(struct sockaddr_in);
43216d6c90fSAndre Oppermann 	icmpgw.sin_family = AF_INET;
43316d6c90fSAndre Oppermann 
434e27b0c87SRobert Watson 	ICMPSTAT_INC(icps_inhist[icp->icmp_type]);
435df8bae1dSRodney W. Grimes 	code = icp->icmp_code;
436df8bae1dSRodney W. Grimes 	switch (icp->icmp_type) {
437df8bae1dSRodney W. Grimes 
438df8bae1dSRodney W. Grimes 	case ICMP_UNREACH:
439df8bae1dSRodney W. Grimes 		switch (code) {
440df8bae1dSRodney W. Grimes 			case ICMP_UNREACH_NET:
441df8bae1dSRodney W. Grimes 			case ICMP_UNREACH_HOST:
442df8bae1dSRodney W. Grimes 			case ICMP_UNREACH_SRCFAIL:
443e4bb5b05SJonathan Lemon 			case ICMP_UNREACH_NET_UNKNOWN:
444e4bb5b05SJonathan Lemon 			case ICMP_UNREACH_HOST_UNKNOWN:
445e4bb5b05SJonathan Lemon 			case ICMP_UNREACH_ISOLATED:
446e4bb5b05SJonathan Lemon 			case ICMP_UNREACH_TOSNET:
447e4bb5b05SJonathan Lemon 			case ICMP_UNREACH_TOSHOST:
448e4bb5b05SJonathan Lemon 			case ICMP_UNREACH_HOST_PRECEDENCE:
449e4bb5b05SJonathan Lemon 			case ICMP_UNREACH_PRECEDENCE_CUTOFF:
450e4bb5b05SJonathan Lemon 				code = PRC_UNREACH_NET;
451df8bae1dSRodney W. Grimes 				break;
452df8bae1dSRodney W. Grimes 
453df8bae1dSRodney W. Grimes 			case ICMP_UNREACH_NEEDFRAG:
454df8bae1dSRodney W. Grimes 				code = PRC_MSGSIZE;
455df8bae1dSRodney W. Grimes 				break;
456df8bae1dSRodney W. Grimes 
457e4bb5b05SJonathan Lemon 			/*
458e4bb5b05SJonathan Lemon 			 * RFC 1122, Sections 3.2.2.1 and 4.2.3.9.
459e4bb5b05SJonathan Lemon 			 * Treat subcodes 2,3 as immediate RST
460e4bb5b05SJonathan Lemon 			 */
461e4bb5b05SJonathan Lemon 			case ICMP_UNREACH_PROTOCOL:
4623e146575SMichael Tuexen 				code = PRC_UNREACH_PROTOCOL;
4633e146575SMichael Tuexen 				break;
464e4bb5b05SJonathan Lemon 			case ICMP_UNREACH_PORT:
465b77d155dSJesper Skriver 				code = PRC_UNREACH_PORT;
466b11d7a4aSPoul-Henning Kamp 				break;
46790fcbbd6SPoul-Henning Kamp 
46890fcbbd6SPoul-Henning Kamp 			case ICMP_UNREACH_NET_PROHIB:
46990fcbbd6SPoul-Henning Kamp 			case ICMP_UNREACH_HOST_PROHIB:
4709c4b2574SPaul Traina 			case ICMP_UNREACH_FILTER_PROHIB:
47190fcbbd6SPoul-Henning Kamp 				code = PRC_UNREACH_ADMIN_PROHIB;
472b11d7a4aSPoul-Henning Kamp 				break;
473b11d7a4aSPoul-Henning Kamp 
474df8bae1dSRodney W. Grimes 			default:
475df8bae1dSRodney W. Grimes 				goto badcode;
476df8bae1dSRodney W. Grimes 		}
477df8bae1dSRodney W. Grimes 		goto deliver;
478df8bae1dSRodney W. Grimes 
479df8bae1dSRodney W. Grimes 	case ICMP_TIMXCEED:
480df8bae1dSRodney W. Grimes 		if (code > 1)
481df8bae1dSRodney W. Grimes 			goto badcode;
482df8bae1dSRodney W. Grimes 		code += PRC_TIMXCEED_INTRANS;
483df8bae1dSRodney W. Grimes 		goto deliver;
484df8bae1dSRodney W. Grimes 
485df8bae1dSRodney W. Grimes 	case ICMP_PARAMPROB:
486df8bae1dSRodney W. Grimes 		if (code > 1)
487df8bae1dSRodney W. Grimes 			goto badcode;
488df8bae1dSRodney W. Grimes 		code = PRC_PARAMPROB;
489df8bae1dSRodney W. Grimes 	deliver:
490df8bae1dSRodney W. Grimes 		/*
491df8bae1dSRodney W. Grimes 		 * Problem with datagram; advise higher level routines.
492df8bae1dSRodney W. Grimes 		 */
493df8bae1dSRodney W. Grimes 		if (icmplen < ICMP_ADVLENMIN || icmplen < ICMP_ADVLEN(icp) ||
49453be11f6SPoul-Henning Kamp 		    icp->icmp_ip.ip_hl < (sizeof(struct ip) >> 2)) {
495e27b0c87SRobert Watson 			ICMPSTAT_INC(icps_badlen);
496df8bae1dSRodney W. Grimes 			goto freeit;
497df8bae1dSRodney W. Grimes 		}
4985b7ee6edSGarrett Wollman 		/* Discard ICMP's in response to multicast packets */
4995b7ee6edSGarrett Wollman 		if (IN_MULTICAST(ntohl(icp->icmp_ip.ip_dst.s_addr)))
5005b7ee6edSGarrett Wollman 			goto badcode;
501df8bae1dSRodney W. Grimes #ifdef ICMPPRINTFS
502df8bae1dSRodney W. Grimes 		if (icmpprintfs)
503df8bae1dSRodney W. Grimes 			printf("deliver to protocol %d\n", icp->icmp_ip.ip_p);
504df8bae1dSRodney W. Grimes #endif
505df8bae1dSRodney W. Grimes 		icmpsrc.sin_addr = icp->icmp_ip.ip_dst;
5066a800098SYoshinobu Inoue 		/*
5076a800098SYoshinobu Inoue 		 * XXX if the packet contains [IPv4 AH TCP], we can't make a
5086a800098SYoshinobu Inoue 		 * notification to TCP layer.
5096a800098SYoshinobu Inoue 		 */
510f77b8427SMichael Tuexen 		i = sizeof(struct ip) + min(icmplen, ICMP_ADVLENPREF(icp));
511f77b8427SMichael Tuexen 		ip_stripoptions(m);
512f77b8427SMichael Tuexen 		if (m->m_len < i && (m = m_pullup(m, i)) == NULL) {
513f77b8427SMichael Tuexen 			/* This should actually not happen */
514f77b8427SMichael Tuexen 			ICMPSTAT_INC(icps_tooshort);
515f77b8427SMichael Tuexen 			return (IPPROTO_DONE);
516f77b8427SMichael Tuexen 		}
517f77b8427SMichael Tuexen 		ip = mtod(m, struct ip *);
518f77b8427SMichael Tuexen 		icp = (struct icmp *)(ip + 1);
519f77b8427SMichael Tuexen 		/*
520f77b8427SMichael Tuexen 		 * The upper layer handler can rely on:
521f77b8427SMichael Tuexen 		 * - The outer IP header has no options.
522f77b8427SMichael Tuexen 		 * - The outer IP header, the ICMP header, the inner IP header,
523f77b8427SMichael Tuexen 		 *   and the first n bytes of the inner payload are contiguous.
524f77b8427SMichael Tuexen 		 *   n is at least 8, but might be larger based on
525f77b8427SMichael Tuexen 		 *   ICMP_ADVLENPREF. See its definition in ip_icmp.h.
526f77b8427SMichael Tuexen 		 */
527623ae52eSPoul-Henning Kamp 		ctlfunc = inetsw[ip_protox[icp->icmp_ip.ip_p]].pr_ctlinput;
528623ae52eSPoul-Henning Kamp 		if (ctlfunc)
529df8bae1dSRodney W. Grimes 			(*ctlfunc)(code, (struct sockaddr *)&icmpsrc,
530b62d102cSBruce Evans 				   (void *)&icp->icmp_ip);
531df8bae1dSRodney W. Grimes 		break;
532df8bae1dSRodney W. Grimes 
533df8bae1dSRodney W. Grimes 	badcode:
534e27b0c87SRobert Watson 		ICMPSTAT_INC(icps_badcode);
535df8bae1dSRodney W. Grimes 		break;
536df8bae1dSRodney W. Grimes 
537df8bae1dSRodney W. Grimes 	case ICMP_ECHO:
5388b615593SMarko Zec 		if (!V_icmpbmcastecho
539d311884fSDavid Greenman 		    && (m->m_flags & (M_MCAST | M_BCAST)) != 0) {
540e27b0c87SRobert Watson 			ICMPSTAT_INC(icps_bmcastecho);
5417022ea0aSGarrett Wollman 			break;
5427022ea0aSGarrett Wollman 		}
543a57815efSBosko Milekic 		if (badport_bandlim(BANDLIM_ICMP_ECHO) < 0)
54409f81a46SBosko Milekic 			goto freeit;
545*382a6bbcSJonathan T. Looney 		icp->icmp_type = ICMP_ECHOREPLY;
546df8bae1dSRodney W. Grimes 		goto reflect;
547df8bae1dSRodney W. Grimes 
548df8bae1dSRodney W. Grimes 	case ICMP_TSTAMP:
54900cb6befSMark Johnston 		if (V_icmptstamprepl == 0)
55000cb6befSMark Johnston 			break;
5518b615593SMarko Zec 		if (!V_icmpbmcastecho
552d311884fSDavid Greenman 		    && (m->m_flags & (M_MCAST | M_BCAST)) != 0) {
553e27b0c87SRobert Watson 			ICMPSTAT_INC(icps_bmcasttstamp);
554fe0fb8abSGarrett Wollman 			break;
555fe0fb8abSGarrett Wollman 		}
556df8bae1dSRodney W. Grimes 		if (icmplen < ICMP_TSLEN) {
557e27b0c87SRobert Watson 			ICMPSTAT_INC(icps_badlen);
558df8bae1dSRodney W. Grimes 			break;
559df8bae1dSRodney W. Grimes 		}
560*382a6bbcSJonathan T. Looney 		if (badport_bandlim(BANDLIM_ICMP_TSTAMP) < 0)
561*382a6bbcSJonathan T. Looney 			goto freeit;
562df8bae1dSRodney W. Grimes 		icp->icmp_type = ICMP_TSTAMPREPLY;
563df8bae1dSRodney W. Grimes 		icp->icmp_rtime = iptime();
564df8bae1dSRodney W. Grimes 		icp->icmp_ttime = icp->icmp_rtime;	/* bogus, do later! */
565df8bae1dSRodney W. Grimes 		goto reflect;
566df8bae1dSRodney W. Grimes 
567df8bae1dSRodney W. Grimes 	case ICMP_MASKREQ:
5688b615593SMarko Zec 		if (V_icmpmaskrepl == 0)
569df8bae1dSRodney W. Grimes 			break;
570df8bae1dSRodney W. Grimes 		/*
571df8bae1dSRodney W. Grimes 		 * We are not able to respond with all ones broadcast
572df8bae1dSRodney W. Grimes 		 * unless we receive it over a point-to-point interface.
573df8bae1dSRodney W. Grimes 		 */
574df8bae1dSRodney W. Grimes 		if (icmplen < ICMP_MASKLEN)
575df8bae1dSRodney W. Grimes 			break;
576df8bae1dSRodney W. Grimes 		switch (ip->ip_dst.s_addr) {
577df8bae1dSRodney W. Grimes 
578df8bae1dSRodney W. Grimes 		case INADDR_BROADCAST:
579df8bae1dSRodney W. Grimes 		case INADDR_ANY:
580df8bae1dSRodney W. Grimes 			icmpdst.sin_addr = ip->ip_src;
581df8bae1dSRodney W. Grimes 			break;
582df8bae1dSRodney W. Grimes 
583df8bae1dSRodney W. Grimes 		default:
584df8bae1dSRodney W. Grimes 			icmpdst.sin_addr = ip->ip_dst;
585df8bae1dSRodney W. Grimes 		}
586df8bae1dSRodney W. Grimes 		ia = (struct in_ifaddr *)ifaof_ifpforaddr(
587df8bae1dSRodney W. Grimes 			    (struct sockaddr *)&icmpdst, m->m_pkthdr.rcvif);
5888c0fec80SRobert Watson 		if (ia == NULL)
589df8bae1dSRodney W. Grimes 			break;
5908c0fec80SRobert Watson 		if (ia->ia_ifp == NULL) {
5918c0fec80SRobert Watson 			ifa_free(&ia->ia_ifa);
5927e6f7714SPoul-Henning Kamp 			break;
5938c0fec80SRobert Watson 		}
594df8bae1dSRodney W. Grimes 		icp->icmp_type = ICMP_MASKREPLY;
5958b615593SMarko Zec 		if (V_icmpmaskfake == 0)
596df8bae1dSRodney W. Grimes 			icp->icmp_mask = ia->ia_sockmask.sin_addr.s_addr;
59757842a38SMatthew N. Dodd 		else
5988b615593SMarko Zec 			icp->icmp_mask = V_icmpmaskfake;
599df8bae1dSRodney W. Grimes 		if (ip->ip_src.s_addr == 0) {
600df8bae1dSRodney W. Grimes 			if (ia->ia_ifp->if_flags & IFF_BROADCAST)
601df8bae1dSRodney W. Grimes 			    ip->ip_src = satosin(&ia->ia_broadaddr)->sin_addr;
602df8bae1dSRodney W. Grimes 			else if (ia->ia_ifp->if_flags & IFF_POINTOPOINT)
603df8bae1dSRodney W. Grimes 			    ip->ip_src = satosin(&ia->ia_dstaddr)->sin_addr;
604df8bae1dSRodney W. Grimes 		}
6058c0fec80SRobert Watson 		ifa_free(&ia->ia_ifa);
606df8bae1dSRodney W. Grimes reflect:
607e27b0c87SRobert Watson 		ICMPSTAT_INC(icps_reflect);
608e27b0c87SRobert Watson 		ICMPSTAT_INC(icps_outhist[icp->icmp_type]);
609df8bae1dSRodney W. Grimes 		icmp_reflect(m);
6108f5a8818SKevin Lo 		return (IPPROTO_DONE);
611df8bae1dSRodney W. Grimes 
612df8bae1dSRodney W. Grimes 	case ICMP_REDIRECT:
6138b615593SMarko Zec 		if (V_log_redirect) {
61418d3153eSDag-Erling Smørgrav 			u_long src, dst, gw;
61518d3153eSDag-Erling Smørgrav 
61618d3153eSDag-Erling Smørgrav 			src = ntohl(ip->ip_src.s_addr);
61718d3153eSDag-Erling Smørgrav 			dst = ntohl(icp->icmp_ip.ip_dst.s_addr);
61818d3153eSDag-Erling Smørgrav 			gw = ntohl(icp->icmp_gwaddr.s_addr);
61918d3153eSDag-Erling Smørgrav 			printf("icmp redirect from %d.%d.%d.%d: "
62018d3153eSDag-Erling Smørgrav 			       "%d.%d.%d.%d => %d.%d.%d.%d\n",
62118d3153eSDag-Erling Smørgrav 			       (int)(src >> 24), (int)((src >> 16) & 0xff),
62218d3153eSDag-Erling Smørgrav 			       (int)((src >> 8) & 0xff), (int)(src & 0xff),
62318d3153eSDag-Erling Smørgrav 			       (int)(dst >> 24), (int)((dst >> 16) & 0xff),
62418d3153eSDag-Erling Smørgrav 			       (int)((dst >> 8) & 0xff), (int)(dst & 0xff),
62518d3153eSDag-Erling Smørgrav 			       (int)(gw >> 24), (int)((gw >> 16) & 0xff),
62618d3153eSDag-Erling Smørgrav 			       (int)((gw >> 8) & 0xff), (int)(gw & 0xff));
62718d3153eSDag-Erling Smørgrav 		}
62887c3bd27SAndre Oppermann 		/*
62987c3bd27SAndre Oppermann 		 * RFC1812 says we must ignore ICMP redirects if we
63087c3bd27SAndre Oppermann 		 * are acting as router.
63187c3bd27SAndre Oppermann 		 */
6328b615593SMarko Zec 		if (V_drop_redirect || V_ipforwarding)
63318d3153eSDag-Erling Smørgrav 			break;
634df8bae1dSRodney W. Grimes 		if (code > 3)
635df8bae1dSRodney W. Grimes 			goto badcode;
636df8bae1dSRodney W. Grimes 		if (icmplen < ICMP_ADVLENMIN || icmplen < ICMP_ADVLEN(icp) ||
63753be11f6SPoul-Henning Kamp 		    icp->icmp_ip.ip_hl < (sizeof(struct ip) >> 2)) {
638e27b0c87SRobert Watson 			ICMPSTAT_INC(icps_badlen);
639df8bae1dSRodney W. Grimes 			break;
640df8bae1dSRodney W. Grimes 		}
641df8bae1dSRodney W. Grimes 		/*
642df8bae1dSRodney W. Grimes 		 * Short circuit routing redirects to force
643df8bae1dSRodney W. Grimes 		 * immediate change in the kernel's routing
644df8bae1dSRodney W. Grimes 		 * tables.  The message is also handed to anyone
645df8bae1dSRodney W. Grimes 		 * listening on a raw socket (e.g. the routing
646df8bae1dSRodney W. Grimes 		 * daemon for use in updating its tables).
647df8bae1dSRodney W. Grimes 		 */
648df8bae1dSRodney W. Grimes 		icmpgw.sin_addr = ip->ip_src;
649df8bae1dSRodney W. Grimes 		icmpdst.sin_addr = icp->icmp_gwaddr;
650df8bae1dSRodney W. Grimes #ifdef	ICMPPRINTFS
6512b758395SGarrett Wollman 		if (icmpprintfs) {
6528144690aSEric van Gyzen 			char dstbuf[INET_ADDRSTRLEN];
6538144690aSEric van Gyzen 			char gwbuf[INET_ADDRSTRLEN];
6542b758395SGarrett Wollman 
6552b758395SGarrett Wollman 			printf("redirect dst %s to %s\n",
6568144690aSEric van Gyzen 			       inet_ntoa_r(icp->icmp_ip.ip_dst, dstbuf),
6578144690aSEric van Gyzen 			       inet_ntoa_r(icp->icmp_gwaddr, gwbuf));
6582b758395SGarrett Wollman 		}
659df8bae1dSRodney W. Grimes #endif
660df8bae1dSRodney W. Grimes 		icmpsrc.sin_addr = icp->icmp_ip.ip_dst;
6618b07e49aSJulian Elischer 		for ( fibnum = 0; fibnum < rt_numfibs; fibnum++) {
6628b07e49aSJulian Elischer 			in_rtredirect((struct sockaddr *)&icmpsrc,
663df8bae1dSRodney W. Grimes 			  (struct sockaddr *)&icmpdst,
664df8bae1dSRodney W. Grimes 			  (struct sockaddr *)0, RTF_GATEWAY | RTF_HOST,
6658b07e49aSJulian Elischer 			  (struct sockaddr *)&icmpgw, fibnum);
6668b07e49aSJulian Elischer 		}
667df8bae1dSRodney W. Grimes 		pfctlinput(PRC_REDIRECT_HOST, (struct sockaddr *)&icmpsrc);
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:
680d1f79a3bSAlexander V. Chernikov 	case ICMP_SOURCEQUENCH:
681df8bae1dSRodney W. Grimes 	default:
682df8bae1dSRodney W. Grimes 		break;
683df8bae1dSRodney W. Grimes 	}
684df8bae1dSRodney W. Grimes 
685df8bae1dSRodney W. Grimes raw:
6868f5a8818SKevin Lo 	*mp = m;
6878f5a8818SKevin Lo 	rip_input(mp, offp, proto);
6888f5a8818SKevin Lo 	return (IPPROTO_DONE);
689df8bae1dSRodney W. Grimes 
690df8bae1dSRodney W. Grimes freeit:
691df8bae1dSRodney W. Grimes 	m_freem(m);
6928f5a8818SKevin Lo 	return (IPPROTO_DONE);
693df8bae1dSRodney W. Grimes }
694df8bae1dSRodney W. Grimes 
695df8bae1dSRodney W. Grimes /*
696df8bae1dSRodney W. Grimes  * Reflect the ip packet back to the source
697df8bae1dSRodney W. Grimes  */
6980312fbe9SPoul-Henning Kamp static void
699f2565d68SRobert Watson icmp_reflect(struct mbuf *m)
700df8bae1dSRodney W. Grimes {
701cc0a3c8cSAndrey V. Elsukov 	struct rm_priotracker in_ifa_tracker;
702ca925d9cSJonathan Lemon 	struct ip *ip = mtod(m, struct ip *);
703ca925d9cSJonathan Lemon 	struct ifaddr *ifa;
70433c4f96dSRobert Watson 	struct ifnet *ifp;
705ca925d9cSJonathan Lemon 	struct in_ifaddr *ia;
706df8bae1dSRodney W. Grimes 	struct in_addr t;
7079977be4aSAlexander V. Chernikov 	struct nhop4_extended nh_ext;
70899d628d5SPedro F. Giffuni 	struct mbuf *opts = NULL;
70953be11f6SPoul-Henning Kamp 	int optlen = (ip->ip_hl << 2) - sizeof(struct ip);
710df8bae1dSRodney W. Grimes 
7116b9ff6b7SGeorge V. Neville-Neil 	if (IN_MULTICAST(ntohl(ip->ip_src.s_addr)) ||
7126b9ff6b7SGeorge V. Neville-Neil 	    IN_EXPERIMENTAL(ntohl(ip->ip_src.s_addr)) ||
7136b9ff6b7SGeorge V. Neville-Neil 	    IN_ZERONET(ntohl(ip->ip_src.s_addr)) ) {
714df8bae1dSRodney W. Grimes 		m_freem(m);	/* Bad return address */
715e27b0c87SRobert Watson 		ICMPSTAT_INC(icps_badaddr);
716df8bae1dSRodney W. Grimes 		goto done;	/* Ip_output() will check for broadcast */
717df8bae1dSRodney W. Grimes 	}
7186b9ff6b7SGeorge V. Neville-Neil 
719df8bae1dSRodney W. Grimes 	t = ip->ip_dst;
720df8bae1dSRodney W. Grimes 	ip->ip_dst = ip->ip_src;
7211488eac8SAndre Oppermann 
722df8bae1dSRodney W. Grimes 	/*
7231488eac8SAndre Oppermann 	 * Source selection for ICMP replies:
7241488eac8SAndre Oppermann 	 *
7251488eac8SAndre Oppermann 	 * If the incoming packet was addressed directly to one of our
7261488eac8SAndre Oppermann 	 * own addresses, use dst as the src for the reply.
727df8bae1dSRodney W. Grimes 	 */
728cc0a3c8cSAndrey V. Elsukov 	IN_IFADDR_RLOCK(&in_ifa_tracker);
72933c4f96dSRobert Watson 	LIST_FOREACH(ia, INADDR_HASH(t.s_addr), ia_hash) {
73033c4f96dSRobert Watson 		if (t.s_addr == IA_SIN(ia)->sin_addr.s_addr) {
73133c4f96dSRobert Watson 			t = IA_SIN(ia)->sin_addr;
732cc0a3c8cSAndrey V. Elsukov 			IN_IFADDR_RUNLOCK(&in_ifa_tracker);
733ca925d9cSJonathan Lemon 			goto match;
73433c4f96dSRobert Watson 		}
73533c4f96dSRobert Watson 	}
736cc0a3c8cSAndrey V. Elsukov 	IN_IFADDR_RUNLOCK(&in_ifa_tracker);
7372d9cfabaSRobert Watson 
7381488eac8SAndre Oppermann 	/*
7391488eac8SAndre Oppermann 	 * If the incoming packet was addressed to one of our broadcast
7401488eac8SAndre Oppermann 	 * addresses, use the first non-broadcast address which corresponds
7411488eac8SAndre Oppermann 	 * to the incoming interface.
7421488eac8SAndre Oppermann 	 */
74333c4f96dSRobert Watson 	ifp = m->m_pkthdr.rcvif;
74433c4f96dSRobert Watson 	if (ifp != NULL && ifp->if_flags & IFF_BROADCAST) {
745137f91e8SJohn Baldwin 		IF_ADDR_RLOCK(ifp);
74633c4f96dSRobert Watson 		TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
747ca925d9cSJonathan Lemon 			if (ifa->ifa_addr->sa_family != AF_INET)
748ca925d9cSJonathan Lemon 				continue;
749ca925d9cSJonathan Lemon 			ia = ifatoia(ifa);
750ca925d9cSJonathan Lemon 			if (satosin(&ia->ia_broadaddr)->sin_addr.s_addr ==
75133c4f96dSRobert Watson 			    t.s_addr) {
75233c4f96dSRobert Watson 				t = IA_SIN(ia)->sin_addr;
753137f91e8SJohn Baldwin 				IF_ADDR_RUNLOCK(ifp);
754ca925d9cSJonathan Lemon 				goto match;
755df8bae1dSRodney W. Grimes 			}
756ca925d9cSJonathan Lemon 		}
757137f91e8SJohn Baldwin 		IF_ADDR_RUNLOCK(ifp);
75833c4f96dSRobert Watson 	}
7591488eac8SAndre Oppermann 	/*
760a0866c8dSAndre Oppermann 	 * If the packet was transiting through us, use the address of
761a0866c8dSAndre Oppermann 	 * the interface the packet came through in.  If that interface
762a0866c8dSAndre Oppermann 	 * doesn't have a suitable IP address, the normal selection
763a0866c8dSAndre Oppermann 	 * criteria apply.
764a0866c8dSAndre Oppermann 	 */
76533c4f96dSRobert Watson 	if (V_icmp_rfi && ifp != NULL) {
766137f91e8SJohn Baldwin 		IF_ADDR_RLOCK(ifp);
76733c4f96dSRobert Watson 		TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
768a0866c8dSAndre Oppermann 			if (ifa->ifa_addr->sa_family != AF_INET)
769a0866c8dSAndre Oppermann 				continue;
770a0866c8dSAndre Oppermann 			ia = ifatoia(ifa);
77133c4f96dSRobert Watson 			t = IA_SIN(ia)->sin_addr;
772137f91e8SJohn Baldwin 			IF_ADDR_RUNLOCK(ifp);
773a0866c8dSAndre Oppermann 			goto match;
774a0866c8dSAndre Oppermann 		}
775137f91e8SJohn Baldwin 		IF_ADDR_RUNLOCK(ifp);
776a0866c8dSAndre Oppermann 	}
777a0866c8dSAndre Oppermann 	/*
778b74d89bbSAndre Oppermann 	 * If the incoming packet was not addressed directly to us, use
779b74d89bbSAndre Oppermann 	 * designated interface for icmp replies specified by sysctl
780b74d89bbSAndre Oppermann 	 * net.inet.icmp.reply_src (default not set). Otherwise continue
781b74d89bbSAndre Oppermann 	 * with normal source selection.
782b74d89bbSAndre Oppermann 	 */
78333c4f96dSRobert Watson 	if (V_reply_src[0] != '\0' && (ifp = ifunit(V_reply_src))) {
784137f91e8SJohn Baldwin 		IF_ADDR_RLOCK(ifp);
78533c4f96dSRobert Watson 		TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
786b74d89bbSAndre Oppermann 			if (ifa->ifa_addr->sa_family != AF_INET)
787b74d89bbSAndre Oppermann 				continue;
788b74d89bbSAndre Oppermann 			ia = ifatoia(ifa);
78933c4f96dSRobert Watson 			t = IA_SIN(ia)->sin_addr;
790137f91e8SJohn Baldwin 			IF_ADDR_RUNLOCK(ifp);
791b74d89bbSAndre Oppermann 			goto match;
792b74d89bbSAndre Oppermann 		}
793137f91e8SJohn Baldwin 		IF_ADDR_RUNLOCK(ifp);
794b74d89bbSAndre Oppermann 	}
795b74d89bbSAndre Oppermann 	/*
7961488eac8SAndre Oppermann 	 * If the packet was transiting through us, use the address of
7971488eac8SAndre Oppermann 	 * the interface that is the closest to the packet source.
7981488eac8SAndre Oppermann 	 * When we don't have a route back to the packet source, stop here
7991488eac8SAndre Oppermann 	 * and drop the packet.
8001488eac8SAndre Oppermann 	 */
8019977be4aSAlexander V. Chernikov 	if (fib4_lookup_nh_ext(M_GETFIB(m), ip->ip_dst, 0, 0, &nh_ext) != 0) {
8020d4bef5dSDima Dorfman 		m_freem(m);
803e27b0c87SRobert Watson 		ICMPSTAT_INC(icps_noroute);
8040d4bef5dSDima Dorfman 		goto done;
8050d4bef5dSDima Dorfman 	}
8069977be4aSAlexander V. Chernikov 	t = nh_ext.nh_src;
807ca925d9cSJonathan Lemon match:
808baee0c3eSRobert Watson #ifdef MAC
809a13e21f7SRobert Watson 	mac_netinet_icmp_replyinplace(m);
810baee0c3eSRobert Watson #endif
811df8bae1dSRodney W. Grimes 	ip->ip_src = t;
812603724d3SBjoern A. Zeeb 	ip->ip_ttl = V_ip_defttl;
813df8bae1dSRodney W. Grimes 
814df8bae1dSRodney W. Grimes 	if (optlen > 0) {
8153e85b721SEd Maste 		u_char *cp;
816df8bae1dSRodney W. Grimes 		int opt, cnt;
817df8bae1dSRodney W. Grimes 		u_int len;
818df8bae1dSRodney W. Grimes 
819df8bae1dSRodney W. Grimes 		/*
820df8bae1dSRodney W. Grimes 		 * Retrieve any source routing from the incoming packet;
821df8bae1dSRodney W. Grimes 		 * add on any record-route or timestamp options.
822df8bae1dSRodney W. Grimes 		 */
823df8bae1dSRodney W. Grimes 		cp = (u_char *) (ip + 1);
82499d628d5SPedro F. Giffuni 		if ((opts = ip_srcroute(m)) == NULL &&
825eb1b1807SGleb Smirnoff 		    (opts = m_gethdr(M_NOWAIT, MT_DATA))) {
826df8bae1dSRodney W. Grimes 			opts->m_len = sizeof(struct in_addr);
827df8bae1dSRodney W. Grimes 			mtod(opts, struct in_addr *)->s_addr = 0;
828df8bae1dSRodney W. Grimes 		}
829df8bae1dSRodney W. Grimes 		if (opts) {
830df8bae1dSRodney W. Grimes #ifdef ICMPPRINTFS
831df8bae1dSRodney W. Grimes 		    if (icmpprintfs)
832df8bae1dSRodney W. Grimes 			    printf("icmp_reflect optlen %d rt %d => ",
833df8bae1dSRodney W. Grimes 				optlen, opts->m_len);
834df8bae1dSRodney W. Grimes #endif
835df8bae1dSRodney W. Grimes 		    for (cnt = optlen; cnt > 0; cnt -= len, cp += len) {
836df8bae1dSRodney W. Grimes 			    opt = cp[IPOPT_OPTVAL];
837df8bae1dSRodney W. Grimes 			    if (opt == IPOPT_EOL)
838df8bae1dSRodney W. Grimes 				    break;
839df8bae1dSRodney W. Grimes 			    if (opt == IPOPT_NOP)
840df8bae1dSRodney W. Grimes 				    len = 1;
841df8bae1dSRodney W. Grimes 			    else {
842707d00a3SJonathan Lemon 				    if (cnt < IPOPT_OLEN + sizeof(*cp))
843707d00a3SJonathan Lemon 					    break;
844df8bae1dSRodney W. Grimes 				    len = cp[IPOPT_OLEN];
845707d00a3SJonathan Lemon 				    if (len < IPOPT_OLEN + sizeof(*cp) ||
846707d00a3SJonathan Lemon 				        len > cnt)
847df8bae1dSRodney W. Grimes 					    break;
848df8bae1dSRodney W. Grimes 			    }
849df8bae1dSRodney W. Grimes 			    /*
850df8bae1dSRodney W. Grimes 			     * Should check for overflow, but it "can't happen"
851df8bae1dSRodney W. Grimes 			     */
852df8bae1dSRodney W. Grimes 			    if (opt == IPOPT_RR || opt == IPOPT_TS ||
853df8bae1dSRodney W. Grimes 				opt == IPOPT_SECURITY) {
854df8bae1dSRodney W. Grimes 				    bcopy((caddr_t)cp,
855df8bae1dSRodney W. Grimes 					mtod(opts, caddr_t) + opts->m_len, len);
856df8bae1dSRodney W. Grimes 				    opts->m_len += len;
857df8bae1dSRodney W. Grimes 			    }
858df8bae1dSRodney W. Grimes 		    }
859df8bae1dSRodney W. Grimes 		    /* Terminate & pad, if necessary */
860623ae52eSPoul-Henning Kamp 		    cnt = opts->m_len % 4;
861623ae52eSPoul-Henning Kamp 		    if (cnt) {
862df8bae1dSRodney W. Grimes 			    for (; cnt < 4; cnt++) {
863df8bae1dSRodney W. Grimes 				    *(mtod(opts, caddr_t) + opts->m_len) =
864df8bae1dSRodney W. Grimes 					IPOPT_EOL;
865df8bae1dSRodney W. Grimes 				    opts->m_len++;
866df8bae1dSRodney W. Grimes 			    }
867df8bae1dSRodney W. Grimes 		    }
868df8bae1dSRodney W. Grimes #ifdef ICMPPRINTFS
869df8bae1dSRodney W. Grimes 		    if (icmpprintfs)
870df8bae1dSRodney W. Grimes 			    printf("%d\n", opts->m_len);
871df8bae1dSRodney W. Grimes #endif
872df8bae1dSRodney W. Grimes 		}
8739e2a372fSGleb Smirnoff 		ip_stripoptions(m);
874df8bae1dSRodney W. Grimes 	}
8759c855a36SSam Leffler 	m_tag_delete_nonpersistent(m);
876df8bae1dSRodney W. Grimes 	m->m_flags &= ~(M_BCAST|M_MCAST);
87702c1c707SAndre Oppermann 	icmp_send(m, opts);
878df8bae1dSRodney W. Grimes done:
879df8bae1dSRodney W. Grimes 	if (opts)
880df8bae1dSRodney W. Grimes 		(void)m_free(opts);
881df8bae1dSRodney W. Grimes }
882df8bae1dSRodney W. Grimes 
883df8bae1dSRodney W. Grimes /*
884df8bae1dSRodney W. Grimes  * Send an icmp packet back to the ip level,
885df8bae1dSRodney W. Grimes  * after supplying a checksum.
886df8bae1dSRodney W. Grimes  */
8870312fbe9SPoul-Henning Kamp static void
888f2565d68SRobert Watson icmp_send(struct mbuf *m, struct mbuf *opts)
889df8bae1dSRodney W. Grimes {
8903e85b721SEd Maste 	struct ip *ip = mtod(m, struct ip *);
8913e85b721SEd Maste 	int hlen;
8923e85b721SEd Maste 	struct icmp *icp;
893df8bae1dSRodney W. Grimes 
89453be11f6SPoul-Henning Kamp 	hlen = ip->ip_hl << 2;
895df8bae1dSRodney W. Grimes 	m->m_data += hlen;
896df8bae1dSRodney W. Grimes 	m->m_len -= hlen;
897df8bae1dSRodney W. Grimes 	icp = mtod(m, struct icmp *);
898df8bae1dSRodney W. Grimes 	icp->icmp_cksum = 0;
8998f134647SGleb Smirnoff 	icp->icmp_cksum = in_cksum(m, ntohs(ip->ip_len) - hlen);
900df8bae1dSRodney W. Grimes 	m->m_data -= hlen;
901df8bae1dSRodney W. Grimes 	m->m_len += hlen;
90294446a2eSArchie Cobbs 	m->m_pkthdr.rcvif = (struct ifnet *)0;
903df8bae1dSRodney W. Grimes #ifdef ICMPPRINTFS
9042b758395SGarrett Wollman 	if (icmpprintfs) {
9058144690aSEric van Gyzen 		char dstbuf[INET_ADDRSTRLEN];
9068144690aSEric van Gyzen 		char srcbuf[INET_ADDRSTRLEN];
9078144690aSEric van Gyzen 
9082b758395SGarrett Wollman 		printf("icmp_send dst %s src %s\n",
9098144690aSEric van Gyzen 		    inet_ntoa_r(ip->ip_dst, dstbuf),
9108144690aSEric van Gyzen 		    inet_ntoa_r(ip->ip_src, srcbuf));
9112b758395SGarrett Wollman 	}
912df8bae1dSRodney W. Grimes #endif
91302c1c707SAndre Oppermann 	(void) ip_output(m, opts, NULL, 0, NULL, NULL);
914df8bae1dSRodney W. Grimes }
915df8bae1dSRodney W. Grimes 
916d685b6eeSLuigi Rizzo /*
91761f26caeSWarner Losh  * Return milliseconds since 00:00 UTC in network format.
918d685b6eeSLuigi Rizzo  */
919d685b6eeSLuigi Rizzo uint32_t
920f2565d68SRobert Watson iptime(void)
921df8bae1dSRodney W. Grimes {
922df8bae1dSRodney W. Grimes 	struct timeval atv;
923df8bae1dSRodney W. Grimes 	u_long t;
924df8bae1dSRodney W. Grimes 
92516cd6db0SBill Fumerola 	getmicrotime(&atv);
926df8bae1dSRodney W. Grimes 	t = (atv.tv_sec % (24*60*60)) * 1000 + atv.tv_usec / 1000;
927df8bae1dSRodney W. Grimes 	return (htonl(t));
928df8bae1dSRodney W. Grimes }
929df8bae1dSRodney W. Grimes 
9305cbf3e08SGarrett Wollman /*
9315cbf3e08SGarrett Wollman  * Return the next larger or smaller MTU plateau (table from RFC 1191)
9325cbf3e08SGarrett Wollman  * given current value MTU.  If DIR is less than zero, a larger plateau
9335cbf3e08SGarrett Wollman  * is returned; otherwise, a smaller value is returned.
9345cbf3e08SGarrett Wollman  */
9351aedbd9cSAndre Oppermann int
936f2565d68SRobert Watson ip_next_mtu(int mtu, int dir)
9375cbf3e08SGarrett Wollman {
9385cbf3e08SGarrett Wollman 	static int mtutab[] = {
9394c037f8dSAndre Oppermann 		65535, 32000, 17914, 8166, 4352, 2002, 1492, 1280, 1006, 508,
9404c037f8dSAndre Oppermann 		296, 68, 0
9415cbf3e08SGarrett Wollman 	};
94206003a1eSAndre Oppermann 	int i, size;
9435cbf3e08SGarrett Wollman 
94406003a1eSAndre Oppermann 	size = (sizeof mtutab) / (sizeof mtutab[0]);
94506003a1eSAndre Oppermann 	if (dir >= 0) {
9461c0b0f52SGleb Smirnoff 		for (i = 0; i < size; i++)
94706003a1eSAndre Oppermann 			if (mtu > mtutab[i])
9485cbf3e08SGarrett Wollman 				return mtutab[i];
9495cbf3e08SGarrett Wollman 	} else {
95006003a1eSAndre Oppermann 		for (i = size - 1; i >= 0; i--)
95106003a1eSAndre Oppermann 			if (mtu < mtutab[i])
95206003a1eSAndre Oppermann 				return mtutab[i];
95306003a1eSAndre Oppermann 		if (mtu == mtutab[0])
95406003a1eSAndre Oppermann 			return mtutab[0];
9555cbf3e08SGarrett Wollman 	}
95606003a1eSAndre Oppermann 	return 0;
9575cbf3e08SGarrett Wollman }
958b8e463e6SBjoern A. Zeeb #endif /* INET */
95951508de1SMatthew Dillon 
96051508de1SMatthew Dillon 
96151508de1SMatthew Dillon /*
96251508de1SMatthew Dillon  * badport_bandlim() - check for ICMP bandwidth limit
96351508de1SMatthew Dillon  *
96451508de1SMatthew Dillon  *	Return 0 if it is ok to send an ICMP error response, -1 if we have
96551508de1SMatthew Dillon  *	hit our bandwidth limit and it is not ok.
96651508de1SMatthew Dillon  *
96751508de1SMatthew Dillon  *	If icmplim is <= 0, the feature is disabled and 0 is returned.
96851508de1SMatthew Dillon  *
96951508de1SMatthew Dillon  *	For now we separate the TCP and UDP subsystems w/ different 'which'
97051508de1SMatthew Dillon  *	values.  We may eventually remove this separation (and simplify the
97151508de1SMatthew Dillon  *	code further).
97251508de1SMatthew Dillon  *
97351508de1SMatthew Dillon  *	Note that the printing of the error message is delayed so we can
97451508de1SMatthew Dillon  *	properly print the icmp error rate that the system was trying to do
97551508de1SMatthew Dillon  *	(i.e. 22000/100 pps, etc...).  This can cause long delays in printing
97651508de1SMatthew Dillon  *	the 'final' error, but it doesn't make sense to solve the printing
97751508de1SMatthew Dillon  *	delay with more complex code.
97851508de1SMatthew Dillon  */
9793cbee8caSGleb Smirnoff struct icmp_rate {
9803cbee8caSGleb Smirnoff 	const char *descr;
9813cbee8caSGleb Smirnoff 	struct counter_rate cr;
9823cbee8caSGleb Smirnoff };
9833cbee8caSGleb Smirnoff static VNET_DEFINE(struct icmp_rate, icmp_rates[BANDLIM_MAX]) = {
98400f21882SSam Leffler 	{ "icmp unreach response" },
98500f21882SSam Leffler 	{ "icmp ping response" },
98600f21882SSam Leffler 	{ "icmp tstamp response" },
98700f21882SSam Leffler 	{ "closed port RST response" },
98808af97b7SRobert Watson 	{ "open port RST response" },
9895ad9e57bSMichael Tuexen 	{ "icmp6 unreach response" },
9905ad9e57bSMichael Tuexen 	{ "sctp ootb response" }
99109f81a46SBosko Milekic };
9923cbee8caSGleb Smirnoff #define	V_icmp_rates	VNET(icmp_rates)
99351508de1SMatthew Dillon 
9943cbee8caSGleb Smirnoff static void
9953cbee8caSGleb Smirnoff icmp_bandlimit_init(void)
9963cbee8caSGleb Smirnoff {
99751508de1SMatthew Dillon 
9983cbee8caSGleb Smirnoff 	for (int i = 0; i < BANDLIM_MAX; i++) {
9993cbee8caSGleb Smirnoff 		V_icmp_rates[i].cr.cr_rate = counter_u64_alloc(M_WAITOK);
10003cbee8caSGleb Smirnoff 		V_icmp_rates[i].cr.cr_ticks = ticks;
100151508de1SMatthew Dillon 	}
10023cbee8caSGleb Smirnoff }
10033cbee8caSGleb Smirnoff VNET_SYSINIT(icmp_bandlimit, SI_SUB_PROTO_DOMAIN, SI_ORDER_ANY,
10043cbee8caSGleb Smirnoff     icmp_bandlimit_init, NULL);
10053cbee8caSGleb Smirnoff 
10063cbee8caSGleb Smirnoff static void
10073cbee8caSGleb Smirnoff icmp_bandlimit_uninit(void)
10083cbee8caSGleb Smirnoff {
10093cbee8caSGleb Smirnoff 
10103cbee8caSGleb Smirnoff 	for (int i = 0; i < BANDLIM_MAX; i++)
10113cbee8caSGleb Smirnoff 		counter_u64_free(V_icmp_rates[i].cr.cr_rate);
10123cbee8caSGleb Smirnoff }
10133cbee8caSGleb Smirnoff VNET_SYSUNINIT(icmp_bandlimit, SI_SUB_PROTO_DOMAIN, SI_ORDER_THIRD,
10143cbee8caSGleb Smirnoff     icmp_bandlimit_uninit, NULL);
10153cbee8caSGleb Smirnoff 
10163cbee8caSGleb Smirnoff int
10173cbee8caSGleb Smirnoff badport_bandlim(int which)
10183cbee8caSGleb Smirnoff {
10193cbee8caSGleb Smirnoff 	int64_t pps;
10203cbee8caSGleb Smirnoff 
10213cbee8caSGleb Smirnoff 	if (V_icmplim == 0 || which == BANDLIM_UNLIMITED)
10223cbee8caSGleb Smirnoff 		return (0);
10233cbee8caSGleb Smirnoff 
10243cbee8caSGleb Smirnoff 	KASSERT(which >= 0 && which < BANDLIM_MAX,
10253cbee8caSGleb Smirnoff 	    ("%s: which %d", __func__, which));
10263cbee8caSGleb Smirnoff 
10273cbee8caSGleb Smirnoff 	pps = counter_ratecheck(&V_icmp_rates[which].cr, V_icmplim);
10283cbee8caSGleb Smirnoff 	if (pps == -1)
10293cbee8caSGleb Smirnoff 		return (-1);
10303cbee8caSGleb Smirnoff 	if (pps > 0 && V_icmplim_output)
10318c70a353SGleb Smirnoff 		log(LOG_NOTICE, "Limiting %s from %jd to %d packets/sec\n",
10328c70a353SGleb Smirnoff 			V_icmp_rates[which].descr, (intmax_t )pps, V_icmplim);
10333cbee8caSGleb Smirnoff 	return (0);
103451508de1SMatthew Dillon }
1035