xref: /freebsd/sys/netinet/ip_icmp.c (revision 72a52a35b4ca8f24fb134838b7ff2e33dcae05f1)
1df8bae1dSRodney W. Grimes /*
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  * 3. All advertising materials mentioning features or use of this software
14df8bae1dSRodney W. Grimes  *    must display the following acknowledgement:
15df8bae1dSRodney W. Grimes  *	This product includes software developed by the University of
16df8bae1dSRodney W. Grimes  *	California, Berkeley and its contributors.
17df8bae1dSRodney W. Grimes  * 4. Neither the name of the University nor the names of its contributors
18df8bae1dSRodney W. Grimes  *    may be used to endorse or promote products derived from this software
19df8bae1dSRodney W. Grimes  *    without specific prior written permission.
20df8bae1dSRodney W. Grimes  *
21df8bae1dSRodney W. Grimes  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22df8bae1dSRodney W. Grimes  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23df8bae1dSRodney W. Grimes  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24df8bae1dSRodney W. Grimes  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25df8bae1dSRodney W. Grimes  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26df8bae1dSRodney W. Grimes  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27df8bae1dSRodney W. Grimes  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28df8bae1dSRodney W. Grimes  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29df8bae1dSRodney W. Grimes  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30df8bae1dSRodney W. Grimes  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31df8bae1dSRodney W. Grimes  * SUCH DAMAGE.
32df8bae1dSRodney W. Grimes  *
33df8bae1dSRodney W. Grimes  *	@(#)ip_icmp.c	8.2 (Berkeley) 1/4/94
34c3aac50fSPeter Wemm  * $FreeBSD$
35df8bae1dSRodney W. Grimes  */
36df8bae1dSRodney W. Grimes 
376a800098SYoshinobu Inoue #include "opt_ipsec.h"
386a800098SYoshinobu Inoue 
39df8bae1dSRodney W. Grimes #include <sys/param.h>
40df8bae1dSRodney W. Grimes #include <sys/systm.h>
41df8bae1dSRodney W. Grimes #include <sys/mbuf.h>
42df8bae1dSRodney W. Grimes #include <sys/protosw.h>
43df8bae1dSRodney W. Grimes #include <sys/socket.h>
44df8bae1dSRodney W. Grimes #include <sys/time.h>
45df8bae1dSRodney W. Grimes #include <sys/kernel.h>
46b5e8ce9fSBruce Evans #include <sys/sysctl.h>
47df8bae1dSRodney W. Grimes 
48df8bae1dSRodney W. Grimes #include <net/if.h>
49df8bae1dSRodney W. Grimes #include <net/route.h>
50df8bae1dSRodney W. Grimes 
515e2d0696SGarrett Wollman #define _IP_VHL
52df8bae1dSRodney W. Grimes #include <netinet/in.h>
53df8bae1dSRodney W. Grimes #include <netinet/in_systm.h>
54df8bae1dSRodney W. Grimes #include <netinet/in_var.h>
55df8bae1dSRodney W. Grimes #include <netinet/ip.h>
56df8bae1dSRodney W. Grimes #include <netinet/ip_icmp.h>
57b5e8ce9fSBruce Evans #include <netinet/ip_var.h>
58df8bae1dSRodney W. Grimes #include <netinet/icmp_var.h>
59df8bae1dSRodney W. Grimes 
606a800098SYoshinobu Inoue #ifdef IPSEC
616a800098SYoshinobu Inoue #include <netinet6/ipsec.h>
626a800098SYoshinobu Inoue #include <netkey/key.h>
636a800098SYoshinobu Inoue #endif
646a800098SYoshinobu Inoue 
656a800098SYoshinobu Inoue #include "faith.h"
666a800098SYoshinobu Inoue #if defined(NFAITH) && NFAITH > 0
676a800098SYoshinobu Inoue #include <net/if_types.h>
686a800098SYoshinobu Inoue #endif
696a800098SYoshinobu Inoue 
7072a52a35SJonathan Lemon #include <machine/in_cksum.h>
7172a52a35SJonathan Lemon 
72df8bae1dSRodney W. Grimes /*
73df8bae1dSRodney W. Grimes  * ICMP routines: error generation, receive packet processing, and
74df8bae1dSRodney W. Grimes  * routines to turnaround packets back to the originator, and
75df8bae1dSRodney W. Grimes  * host table maintenance routines.
76df8bae1dSRodney W. Grimes  */
77df8bae1dSRodney W. Grimes 
78f708ef1bSPoul-Henning Kamp static struct	icmpstat icmpstat;
790312fbe9SPoul-Henning Kamp SYSCTL_STRUCT(_net_inet_icmp, ICMPCTL_STATS, stats, CTLFLAG_RD,
800312fbe9SPoul-Henning Kamp 	&icmpstat, icmpstat, "");
810312fbe9SPoul-Henning Kamp 
820312fbe9SPoul-Henning Kamp static int	icmpmaskrepl = 0;
830312fbe9SPoul-Henning Kamp SYSCTL_INT(_net_inet_icmp, ICMPCTL_MASKREPL, maskrepl, CTLFLAG_RW,
840312fbe9SPoul-Henning Kamp 	&icmpmaskrepl, 0, "");
850312fbe9SPoul-Henning Kamp 
8618d3153eSDag-Erling Smørgrav static int	drop_redirect = 0;
8718d3153eSDag-Erling Smørgrav SYSCTL_INT(_net_inet_icmp, OID_AUTO, drop_redirect, CTLFLAG_RW,
8818d3153eSDag-Erling Smørgrav 	&drop_redirect, 0, "");
8918d3153eSDag-Erling Smørgrav 
906c3b5f69SDag-Erling Smørgrav static int	log_redirect = 0;
916c3b5f69SDag-Erling Smørgrav SYSCTL_INT(_net_inet_icmp, OID_AUTO, log_redirect, CTLFLAG_RW,
926c3b5f69SDag-Erling Smørgrav 	&log_redirect, 0, "");
936c3b5f69SDag-Erling Smørgrav 
9451508de1SMatthew Dillon #ifdef ICMP_BANDLIM
9551508de1SMatthew Dillon 
9651508de1SMatthew Dillon /*
975fce7fc4SMatthew Dillon  * ICMP error-response bandwidth limiting sysctl.  If not enabled, sysctl
985fce7fc4SMatthew Dillon  *      variable content is -1 and read-only.
9951508de1SMatthew Dillon  */
10051508de1SMatthew Dillon 
101173c0f9fSWarner Losh static int      icmplim = 200;
10251508de1SMatthew Dillon SYSCTL_INT(_net_inet_icmp, ICMPCTL_ICMPLIM, icmplim, CTLFLAG_RW,
10351508de1SMatthew Dillon 	&icmplim, 0, "");
1045fce7fc4SMatthew Dillon #else
1055fce7fc4SMatthew Dillon 
1065fce7fc4SMatthew Dillon static int      icmplim = -1;
1075fce7fc4SMatthew Dillon SYSCTL_INT(_net_inet_icmp, ICMPCTL_ICMPLIM, icmplim, CTLFLAG_RD,
1085fce7fc4SMatthew Dillon 	&icmplim, 0, "");
10951508de1SMatthew Dillon 
11051508de1SMatthew Dillon #endif
11151508de1SMatthew Dillon 
1125fce7fc4SMatthew Dillon /*
1135fce7fc4SMatthew Dillon  * ICMP broadcast echo sysctl
1145fce7fc4SMatthew Dillon  */
1155fce7fc4SMatthew Dillon 
11661a4defdSJoseph Koshy static int	icmpbmcastecho = 0;
11718d3153eSDag-Erling Smørgrav SYSCTL_INT(_net_inet_icmp, OID_AUTO, bmcastecho, CTLFLAG_RW,
11818d3153eSDag-Erling Smørgrav 	&icmpbmcastecho, 0, "");
1197022ea0aSGarrett Wollman 
12051508de1SMatthew Dillon 
121df8bae1dSRodney W. Grimes #ifdef ICMPPRINTFS
122df8bae1dSRodney W. Grimes int	icmpprintfs = 0;
123df8bae1dSRodney W. Grimes #endif
124df8bae1dSRodney W. Grimes 
1250312fbe9SPoul-Henning Kamp static void	icmp_reflect __P((struct mbuf *));
1260312fbe9SPoul-Henning Kamp static void	icmp_send __P((struct mbuf *, struct mbuf *));
127f708ef1bSPoul-Henning Kamp static int	ip_next_mtu __P((int, int));
1280312fbe9SPoul-Henning Kamp 
129df8bae1dSRodney W. Grimes extern	struct protosw inetsw[];
130df8bae1dSRodney W. Grimes 
131df8bae1dSRodney W. Grimes /*
132df8bae1dSRodney W. Grimes  * Generate an error packet of type error
133df8bae1dSRodney W. Grimes  * in response to bad packet ip.
134df8bae1dSRodney W. Grimes  */
135df8bae1dSRodney W. Grimes void
136df8bae1dSRodney W. Grimes icmp_error(n, type, code, dest, destifp)
137df8bae1dSRodney W. Grimes 	struct mbuf *n;
138df8bae1dSRodney W. Grimes 	int type, code;
139df8bae1dSRodney W. Grimes 	n_long dest;
140df8bae1dSRodney W. Grimes 	struct ifnet *destifp;
141df8bae1dSRodney W. Grimes {
142df8bae1dSRodney W. Grimes 	register struct ip *oip = mtod(n, struct ip *), *nip;
1435e2d0696SGarrett Wollman 	register unsigned oiplen = IP_VHL_HL(oip->ip_vhl) << 2;
144df8bae1dSRodney W. Grimes 	register struct icmp *icp;
145df8bae1dSRodney W. Grimes 	register struct mbuf *m;
146df8bae1dSRodney W. Grimes 	unsigned icmplen;
147df8bae1dSRodney W. Grimes 
148df8bae1dSRodney W. Grimes #ifdef ICMPPRINTFS
149df8bae1dSRodney W. Grimes 	if (icmpprintfs)
150623ae52eSPoul-Henning Kamp 		printf("icmp_error(%p, %x, %d)\n", oip, type, code);
151df8bae1dSRodney W. Grimes #endif
152df8bae1dSRodney W. Grimes 	if (type != ICMP_REDIRECT)
153df8bae1dSRodney W. Grimes 		icmpstat.icps_error++;
154df8bae1dSRodney W. Grimes 	/*
155df8bae1dSRodney W. Grimes 	 * Don't send error if not the first fragment of message.
156df8bae1dSRodney W. Grimes 	 * Don't error if the old packet protocol was ICMP
157df8bae1dSRodney W. Grimes 	 * error message, only known informational types.
158df8bae1dSRodney W. Grimes 	 */
159df8bae1dSRodney W. Grimes 	if (oip->ip_off &~ (IP_MF|IP_DF))
160df8bae1dSRodney W. Grimes 		goto freeit;
161df8bae1dSRodney W. Grimes 	if (oip->ip_p == IPPROTO_ICMP && type != ICMP_REDIRECT &&
162df8bae1dSRodney W. Grimes 	  n->m_len >= oiplen + ICMP_MINLEN &&
163df8bae1dSRodney W. Grimes 	  !ICMP_INFOTYPE(((struct icmp *)((caddr_t)oip + oiplen))->icmp_type)) {
164df8bae1dSRodney W. Grimes 		icmpstat.icps_oldicmp++;
165df8bae1dSRodney W. Grimes 		goto freeit;
166df8bae1dSRodney W. Grimes 	}
167df8bae1dSRodney W. Grimes 	/* Don't send error in response to a multicast or broadcast packet */
168df8bae1dSRodney W. Grimes 	if (n->m_flags & (M_BCAST|M_MCAST))
169df8bae1dSRodney W. Grimes 		goto freeit;
170df8bae1dSRodney W. Grimes 	/*
171df8bae1dSRodney W. Grimes 	 * First, formulate icmp message
172df8bae1dSRodney W. Grimes 	 */
173df8bae1dSRodney W. Grimes 	m = m_gethdr(M_DONTWAIT, MT_HEADER);
174df8bae1dSRodney W. Grimes 	if (m == NULL)
175df8bae1dSRodney W. Grimes 		goto freeit;
176df8bae1dSRodney W. Grimes 	icmplen = oiplen + min(8, oip->ip_len);
177df8bae1dSRodney W. Grimes 	m->m_len = icmplen + ICMP_MINLEN;
178df8bae1dSRodney W. Grimes 	MH_ALIGN(m, m->m_len);
179df8bae1dSRodney W. Grimes 	icp = mtod(m, struct icmp *);
180df8bae1dSRodney W. Grimes 	if ((u_int)type > ICMP_MAXTYPE)
181df8bae1dSRodney W. Grimes 		panic("icmp_error");
182df8bae1dSRodney W. Grimes 	icmpstat.icps_outhist[type]++;
183df8bae1dSRodney W. Grimes 	icp->icmp_type = type;
184df8bae1dSRodney W. Grimes 	if (type == ICMP_REDIRECT)
185df8bae1dSRodney W. Grimes 		icp->icmp_gwaddr.s_addr = dest;
186df8bae1dSRodney W. Grimes 	else {
187df8bae1dSRodney W. Grimes 		icp->icmp_void = 0;
188df8bae1dSRodney W. Grimes 		/*
189df8bae1dSRodney W. Grimes 		 * The following assignments assume an overlay with the
190df8bae1dSRodney W. Grimes 		 * zeroed icmp_void field.
191df8bae1dSRodney W. Grimes 		 */
192df8bae1dSRodney W. Grimes 		if (type == ICMP_PARAMPROB) {
193df8bae1dSRodney W. Grimes 			icp->icmp_pptr = code;
194df8bae1dSRodney W. Grimes 			code = 0;
195df8bae1dSRodney W. Grimes 		} else if (type == ICMP_UNREACH &&
196df8bae1dSRodney W. Grimes 			code == ICMP_UNREACH_NEEDFRAG && destifp) {
197df8bae1dSRodney W. Grimes 			icp->icmp_nextmtu = htons(destifp->if_mtu);
198df8bae1dSRodney W. Grimes 		}
199df8bae1dSRodney W. Grimes 	}
200df8bae1dSRodney W. Grimes 
201df8bae1dSRodney W. Grimes 	icp->icmp_code = code;
202df8bae1dSRodney W. Grimes 	bcopy((caddr_t)oip, (caddr_t)&icp->icmp_ip, icmplen);
203df8bae1dSRodney W. Grimes 	nip = &icp->icmp_ip;
204df8bae1dSRodney W. Grimes 	nip->ip_len = htons((u_short)(nip->ip_len + oiplen));
205df8bae1dSRodney W. Grimes 
206df8bae1dSRodney W. Grimes 	/*
207df8bae1dSRodney W. Grimes 	 * Now, copy old ip header (without options)
208df8bae1dSRodney W. Grimes 	 * in front of icmp message.
209df8bae1dSRodney W. Grimes 	 */
210df8bae1dSRodney W. Grimes 	if (m->m_data - sizeof(struct ip) < m->m_pktdat)
211df8bae1dSRodney W. Grimes 		panic("icmp len");
212df8bae1dSRodney W. Grimes 	m->m_data -= sizeof(struct ip);
213df8bae1dSRodney W. Grimes 	m->m_len += sizeof(struct ip);
214df8bae1dSRodney W. Grimes 	m->m_pkthdr.len = m->m_len;
215df8bae1dSRodney W. Grimes 	m->m_pkthdr.rcvif = n->m_pkthdr.rcvif;
216df8bae1dSRodney W. Grimes 	nip = mtod(m, struct ip *);
217df8bae1dSRodney W. Grimes 	bcopy((caddr_t)oip, (caddr_t)nip, sizeof(struct ip));
218df8bae1dSRodney W. Grimes 	nip->ip_len = m->m_len;
2195e2d0696SGarrett Wollman 	nip->ip_vhl = IP_VHL_BORING;
220df8bae1dSRodney W. Grimes 	nip->ip_p = IPPROTO_ICMP;
221df8bae1dSRodney W. Grimes 	nip->ip_tos = 0;
222df8bae1dSRodney W. Grimes 	icmp_reflect(m);
223df8bae1dSRodney W. Grimes 
224df8bae1dSRodney W. Grimes freeit:
225df8bae1dSRodney W. Grimes 	m_freem(n);
226df8bae1dSRodney W. Grimes }
227df8bae1dSRodney W. Grimes 
228df8bae1dSRodney W. Grimes static struct sockaddr_in icmpsrc = { sizeof (struct sockaddr_in), AF_INET };
229df8bae1dSRodney W. Grimes static struct sockaddr_in icmpdst = { sizeof (struct sockaddr_in), AF_INET };
230df8bae1dSRodney W. Grimes static struct sockaddr_in icmpgw = { sizeof (struct sockaddr_in), AF_INET };
231df8bae1dSRodney W. Grimes 
232df8bae1dSRodney W. Grimes /*
233df8bae1dSRodney W. Grimes  * Process a received ICMP message.
234df8bae1dSRodney W. Grimes  */
235df8bae1dSRodney W. Grimes void
2366a800098SYoshinobu Inoue icmp_input(m, off, proto)
237df8bae1dSRodney W. Grimes 	register struct mbuf *m;
2386a800098SYoshinobu Inoue 	int off, proto;
239df8bae1dSRodney W. Grimes {
2406a800098SYoshinobu Inoue 	int hlen = off;
241df8bae1dSRodney W. Grimes 	register struct icmp *icp;
242df8bae1dSRodney W. Grimes 	register struct ip *ip = mtod(m, struct ip *);
243df8bae1dSRodney W. Grimes 	int icmplen = ip->ip_len;
244df8bae1dSRodney W. Grimes 	register int i;
245df8bae1dSRodney W. Grimes 	struct in_ifaddr *ia;
246b62d102cSBruce Evans 	void (*ctlfunc) __P((int, struct sockaddr *, void *));
247df8bae1dSRodney W. Grimes 	int code;
248df8bae1dSRodney W. Grimes 
249df8bae1dSRodney W. Grimes 	/*
250df8bae1dSRodney W. Grimes 	 * Locate icmp structure in mbuf, and check
251df8bae1dSRodney W. Grimes 	 * that not corrupted and of at least minimum length.
252df8bae1dSRodney W. Grimes 	 */
253df8bae1dSRodney W. Grimes #ifdef ICMPPRINTFS
2542b758395SGarrett Wollman 	if (icmpprintfs) {
2552b758395SGarrett Wollman 		char buf[4 * sizeof "123"];
2562b758395SGarrett Wollman 		strcpy(buf, inet_ntoa(ip->ip_src));
2572b758395SGarrett Wollman 		printf("icmp_input from %s to %s, len %d\n",
2582b758395SGarrett Wollman 		       buf, inet_ntoa(ip->ip_dst), icmplen);
2592b758395SGarrett Wollman 	}
260df8bae1dSRodney W. Grimes #endif
261df8bae1dSRodney W. Grimes 	if (icmplen < ICMP_MINLEN) {
262df8bae1dSRodney W. Grimes 		icmpstat.icps_tooshort++;
263df8bae1dSRodney W. Grimes 		goto freeit;
264df8bae1dSRodney W. Grimes 	}
265df8bae1dSRodney W. Grimes 	i = hlen + min(icmplen, ICMP_ADVLENMIN);
266df8bae1dSRodney W. Grimes 	if (m->m_len < i && (m = m_pullup(m, i)) == 0)  {
267df8bae1dSRodney W. Grimes 		icmpstat.icps_tooshort++;
268df8bae1dSRodney W. Grimes 		return;
269df8bae1dSRodney W. Grimes 	}
270df8bae1dSRodney W. Grimes 	ip = mtod(m, struct ip *);
271df8bae1dSRodney W. Grimes 	m->m_len -= hlen;
272df8bae1dSRodney W. Grimes 	m->m_data += hlen;
273df8bae1dSRodney W. Grimes 	icp = mtod(m, struct icmp *);
274df8bae1dSRodney W. Grimes 	if (in_cksum(m, icmplen)) {
275df8bae1dSRodney W. Grimes 		icmpstat.icps_checksum++;
276df8bae1dSRodney W. Grimes 		goto freeit;
277df8bae1dSRodney W. Grimes 	}
278df8bae1dSRodney W. Grimes 	m->m_len += hlen;
279df8bae1dSRodney W. Grimes 	m->m_data -= hlen;
280df8bae1dSRodney W. Grimes 
2816a800098SYoshinobu Inoue #if defined(NFAITH) && 0 < NFAITH
2826a800098SYoshinobu Inoue 	if (m->m_pkthdr.rcvif && m->m_pkthdr.rcvif->if_type == IFT_FAITH) {
2836a800098SYoshinobu Inoue 		/*
2846a800098SYoshinobu Inoue 		 * Deliver very specific ICMP type only.
2856a800098SYoshinobu Inoue 		 */
2866a800098SYoshinobu Inoue 		switch (icp->icmp_type) {
2876a800098SYoshinobu Inoue 		case ICMP_UNREACH:
2886a800098SYoshinobu Inoue 		case ICMP_TIMXCEED:
2896a800098SYoshinobu Inoue 			break;
2906a800098SYoshinobu Inoue 		default:
2916a800098SYoshinobu Inoue 			goto freeit;
2926a800098SYoshinobu Inoue 		}
2936a800098SYoshinobu Inoue 	}
2946a800098SYoshinobu Inoue #endif
2956a800098SYoshinobu Inoue 
296df8bae1dSRodney W. Grimes #ifdef ICMPPRINTFS
297df8bae1dSRodney W. Grimes 	if (icmpprintfs)
298df8bae1dSRodney W. Grimes 		printf("icmp_input, type %d code %d\n", icp->icmp_type,
299df8bae1dSRodney W. Grimes 		    icp->icmp_code);
300df8bae1dSRodney W. Grimes #endif
3015b7ee6edSGarrett Wollman 
3026a800098SYoshinobu Inoue #ifdef IPSEC
3036a800098SYoshinobu Inoue 	/* drop it if it does not match the policy */
3046a800098SYoshinobu Inoue 	/* XXX Is there meaning of check in here ? */
3056a800098SYoshinobu Inoue 	if (ipsec4_in_reject(m, NULL)) {
3066a800098SYoshinobu Inoue 		ipsecstat.in_polvio++;
3076a800098SYoshinobu Inoue 		goto freeit;
3086a800098SYoshinobu Inoue 	}
3096a800098SYoshinobu Inoue #endif
3106a800098SYoshinobu Inoue 
3115b7ee6edSGarrett Wollman 	/*
3125b7ee6edSGarrett Wollman 	 * Message type specific processing.
3135b7ee6edSGarrett Wollman 	 */
314df8bae1dSRodney W. Grimes 	if (icp->icmp_type > ICMP_MAXTYPE)
315df8bae1dSRodney W. Grimes 		goto raw;
316df8bae1dSRodney W. Grimes 	icmpstat.icps_inhist[icp->icmp_type]++;
317df8bae1dSRodney W. Grimes 	code = icp->icmp_code;
318df8bae1dSRodney W. Grimes 	switch (icp->icmp_type) {
319df8bae1dSRodney W. Grimes 
320df8bae1dSRodney W. Grimes 	case ICMP_UNREACH:
321df8bae1dSRodney W. Grimes 		switch (code) {
322df8bae1dSRodney W. Grimes 			case ICMP_UNREACH_NET:
323df8bae1dSRodney W. Grimes 			case ICMP_UNREACH_HOST:
324df8bae1dSRodney W. Grimes 			case ICMP_UNREACH_PROTOCOL:
325df8bae1dSRodney W. Grimes 			case ICMP_UNREACH_PORT:
326df8bae1dSRodney W. Grimes 			case ICMP_UNREACH_SRCFAIL:
327df8bae1dSRodney W. Grimes 				code += PRC_UNREACH_NET;
328df8bae1dSRodney W. Grimes 				break;
329df8bae1dSRodney W. Grimes 
330df8bae1dSRodney W. Grimes 			case ICMP_UNREACH_NEEDFRAG:
331df8bae1dSRodney W. Grimes 				code = PRC_MSGSIZE;
332df8bae1dSRodney W. Grimes 				break;
333df8bae1dSRodney W. Grimes 
334df8bae1dSRodney W. Grimes 			case ICMP_UNREACH_NET_UNKNOWN:
335df8bae1dSRodney W. Grimes 			case ICMP_UNREACH_NET_PROHIB:
336df8bae1dSRodney W. Grimes 			case ICMP_UNREACH_TOSNET:
337df8bae1dSRodney W. Grimes 				code = PRC_UNREACH_NET;
338df8bae1dSRodney W. Grimes 				break;
339df8bae1dSRodney W. Grimes 
340df8bae1dSRodney W. Grimes 			case ICMP_UNREACH_HOST_UNKNOWN:
341df8bae1dSRodney W. Grimes 			case ICMP_UNREACH_ISOLATED:
342df8bae1dSRodney W. Grimes 			case ICMP_UNREACH_HOST_PROHIB:
343df8bae1dSRodney W. Grimes 			case ICMP_UNREACH_TOSHOST:
344df8bae1dSRodney W. Grimes 				code = PRC_UNREACH_HOST;
345df8bae1dSRodney W. Grimes 				break;
346df8bae1dSRodney W. Grimes 
3479c4b2574SPaul Traina 			case ICMP_UNREACH_FILTER_PROHIB:
3489c4b2574SPaul Traina 			case ICMP_UNREACH_HOST_PRECEDENCE:
3499c4b2574SPaul Traina 			case ICMP_UNREACH_PRECEDENCE_CUTOFF:
3509c4b2574SPaul Traina 				code = PRC_UNREACH_PORT;
3519c4b2574SPaul Traina 				break;
3529c4b2574SPaul Traina 
353df8bae1dSRodney W. Grimes 			default:
354df8bae1dSRodney W. Grimes 				goto badcode;
355df8bae1dSRodney W. Grimes 		}
356df8bae1dSRodney W. Grimes 		goto deliver;
357df8bae1dSRodney W. Grimes 
358df8bae1dSRodney W. Grimes 	case ICMP_TIMXCEED:
359df8bae1dSRodney W. Grimes 		if (code > 1)
360df8bae1dSRodney W. Grimes 			goto badcode;
361df8bae1dSRodney W. Grimes 		code += PRC_TIMXCEED_INTRANS;
362df8bae1dSRodney W. Grimes 		goto deliver;
363df8bae1dSRodney W. Grimes 
364df8bae1dSRodney W. Grimes 	case ICMP_PARAMPROB:
365df8bae1dSRodney W. Grimes 		if (code > 1)
366df8bae1dSRodney W. Grimes 			goto badcode;
367df8bae1dSRodney W. Grimes 		code = PRC_PARAMPROB;
368df8bae1dSRodney W. Grimes 		goto deliver;
369df8bae1dSRodney W. Grimes 
370df8bae1dSRodney W. Grimes 	case ICMP_SOURCEQUENCH:
371df8bae1dSRodney W. Grimes 		if (code)
372df8bae1dSRodney W. Grimes 			goto badcode;
373df8bae1dSRodney W. Grimes 		code = PRC_QUENCH;
374df8bae1dSRodney W. Grimes 	deliver:
375df8bae1dSRodney W. Grimes 		/*
376df8bae1dSRodney W. Grimes 		 * Problem with datagram; advise higher level routines.
377df8bae1dSRodney W. Grimes 		 */
378df8bae1dSRodney W. Grimes 		if (icmplen < ICMP_ADVLENMIN || icmplen < ICMP_ADVLEN(icp) ||
3795e2d0696SGarrett Wollman 		    IP_VHL_HL(icp->icmp_ip.ip_vhl) < (sizeof(struct ip) >> 2)) {
380df8bae1dSRodney W. Grimes 			icmpstat.icps_badlen++;
381df8bae1dSRodney W. Grimes 			goto freeit;
382df8bae1dSRodney W. Grimes 		}
383df8bae1dSRodney W. Grimes 		NTOHS(icp->icmp_ip.ip_len);
3845b7ee6edSGarrett Wollman 		/* Discard ICMP's in response to multicast packets */
3855b7ee6edSGarrett Wollman 		if (IN_MULTICAST(ntohl(icp->icmp_ip.ip_dst.s_addr)))
3865b7ee6edSGarrett Wollman 			goto badcode;
387df8bae1dSRodney W. Grimes #ifdef ICMPPRINTFS
388df8bae1dSRodney W. Grimes 		if (icmpprintfs)
389df8bae1dSRodney W. Grimes 			printf("deliver to protocol %d\n", icp->icmp_ip.ip_p);
390df8bae1dSRodney W. Grimes #endif
391df8bae1dSRodney W. Grimes 		icmpsrc.sin_addr = icp->icmp_ip.ip_dst;
392b7a44e34SGarrett Wollman #if 1
3935cbf3e08SGarrett Wollman 		/*
3945cbf3e08SGarrett Wollman 		 * MTU discovery:
3955cbf3e08SGarrett Wollman 		 * If we got a needfrag and there is a host route to the
3965cbf3e08SGarrett Wollman 		 * original destination, and the MTU is not locked, then
3975cbf3e08SGarrett Wollman 		 * set the MTU in the route to the suggested new value
3985cbf3e08SGarrett Wollman 		 * (if given) and then notify as usual.  The ULPs will
3995cbf3e08SGarrett Wollman 		 * notice that the MTU has changed and adapt accordingly.
4005cbf3e08SGarrett Wollman 		 * If no new MTU was suggested, then we guess a new one
4015cbf3e08SGarrett Wollman 		 * less than the current value.  If the new MTU is
4025cbf3e08SGarrett Wollman 		 * unreasonably small (arbitrarily set at 296), then
4035cbf3e08SGarrett Wollman 		 * we reset the MTU to the interface value and enable the
4045cbf3e08SGarrett Wollman 		 * lock bit, indicating that we are no longer doing MTU
4055cbf3e08SGarrett Wollman 		 * discovery.
4065cbf3e08SGarrett Wollman 		 */
4075cbf3e08SGarrett Wollman 		if (code == PRC_MSGSIZE) {
4085cbf3e08SGarrett Wollman 			struct rtentry *rt;
4095cbf3e08SGarrett Wollman 			int mtu;
4105cbf3e08SGarrett Wollman 
4115cbf3e08SGarrett Wollman 			rt = rtalloc1((struct sockaddr *)&icmpsrc, 0,
4125cbf3e08SGarrett Wollman 				      RTF_CLONING | RTF_PRCLONING);
4135cbf3e08SGarrett Wollman 			if (rt && (rt->rt_flags & RTF_HOST)
4145cbf3e08SGarrett Wollman 			    && !(rt->rt_rmx.rmx_locks & RTV_MTU)) {
4155cbf3e08SGarrett Wollman 				mtu = ntohs(icp->icmp_nextmtu);
4165cbf3e08SGarrett Wollman 				if (!mtu)
4175cbf3e08SGarrett Wollman 					mtu = ip_next_mtu(rt->rt_rmx.rmx_mtu,
4185cbf3e08SGarrett Wollman 							  1);
419be070f43SGarrett Wollman #ifdef DEBUG_MTUDISC
420be070f43SGarrett Wollman 				printf("MTU for %s reduced to %d\n",
421be070f43SGarrett Wollman 					inet_ntoa(icmpsrc.sin_addr), mtu);
422be070f43SGarrett Wollman #endif
423be070f43SGarrett Wollman 				if (mtu < 296) {
424b7a44e34SGarrett Wollman 					/* rt->rt_rmx.rmx_mtu =
425b7a44e34SGarrett Wollman 						rt->rt_ifp->if_mtu; */
4265cbf3e08SGarrett Wollman 					rt->rt_rmx.rmx_locks |= RTV_MTU;
4275cbf3e08SGarrett Wollman 				} else if (rt->rt_rmx.rmx_mtu > mtu) {
4285cbf3e08SGarrett Wollman 					rt->rt_rmx.rmx_mtu = mtu;
4295cbf3e08SGarrett Wollman 				}
4305cbf3e08SGarrett Wollman 			}
4315cbf3e08SGarrett Wollman 			if (rt)
4325cbf3e08SGarrett Wollman 				RTFREE(rt);
4335cbf3e08SGarrett Wollman 		}
4345cbf3e08SGarrett Wollman 
435b7a44e34SGarrett Wollman #endif
4366a800098SYoshinobu Inoue 		/*
4376a800098SYoshinobu Inoue 		 * XXX if the packet contains [IPv4 AH TCP], we can't make a
4386a800098SYoshinobu Inoue 		 * notification to TCP layer.
4396a800098SYoshinobu Inoue 		 */
440623ae52eSPoul-Henning Kamp 		ctlfunc = inetsw[ip_protox[icp->icmp_ip.ip_p]].pr_ctlinput;
441623ae52eSPoul-Henning Kamp 		if (ctlfunc)
442df8bae1dSRodney W. Grimes 			(*ctlfunc)(code, (struct sockaddr *)&icmpsrc,
443b62d102cSBruce Evans 				   (void *)&icp->icmp_ip);
444df8bae1dSRodney W. Grimes 		break;
445df8bae1dSRodney W. Grimes 
446df8bae1dSRodney W. Grimes 	badcode:
447df8bae1dSRodney W. Grimes 		icmpstat.icps_badcode++;
448df8bae1dSRodney W. Grimes 		break;
449df8bae1dSRodney W. Grimes 
450df8bae1dSRodney W. Grimes 	case ICMP_ECHO:
4517022ea0aSGarrett Wollman 		if (!icmpbmcastecho
452d311884fSDavid Greenman 		    && (m->m_flags & (M_MCAST | M_BCAST)) != 0) {
4537022ea0aSGarrett Wollman 			icmpstat.icps_bmcastecho++;
4547022ea0aSGarrett Wollman 			break;
4557022ea0aSGarrett Wollman 		}
456df8bae1dSRodney W. Grimes 		icp->icmp_type = ICMP_ECHOREPLY;
457df8bae1dSRodney W. Grimes 		goto reflect;
458df8bae1dSRodney W. Grimes 
459df8bae1dSRodney W. Grimes 	case ICMP_TSTAMP:
460fe0fb8abSGarrett Wollman 		if (!icmpbmcastecho
461d311884fSDavid Greenman 		    && (m->m_flags & (M_MCAST | M_BCAST)) != 0) {
462fe0fb8abSGarrett Wollman 			icmpstat.icps_bmcasttstamp++;
463fe0fb8abSGarrett Wollman 			break;
464fe0fb8abSGarrett Wollman 		}
465df8bae1dSRodney W. Grimes 		if (icmplen < ICMP_TSLEN) {
466df8bae1dSRodney W. Grimes 			icmpstat.icps_badlen++;
467df8bae1dSRodney W. Grimes 			break;
468df8bae1dSRodney W. Grimes 		}
469df8bae1dSRodney W. Grimes 		icp->icmp_type = ICMP_TSTAMPREPLY;
470df8bae1dSRodney W. Grimes 		icp->icmp_rtime = iptime();
471df8bae1dSRodney W. Grimes 		icp->icmp_ttime = icp->icmp_rtime;	/* bogus, do later! */
472df8bae1dSRodney W. Grimes 		goto reflect;
473df8bae1dSRodney W. Grimes 
474df8bae1dSRodney W. Grimes 	case ICMP_MASKREQ:
475df8bae1dSRodney W. Grimes #define	satosin(sa)	((struct sockaddr_in *)(sa))
476df8bae1dSRodney W. Grimes 		if (icmpmaskrepl == 0)
477df8bae1dSRodney W. Grimes 			break;
478df8bae1dSRodney W. Grimes 		/*
479df8bae1dSRodney W. Grimes 		 * We are not able to respond with all ones broadcast
480df8bae1dSRodney W. Grimes 		 * unless we receive it over a point-to-point interface.
481df8bae1dSRodney W. Grimes 		 */
482df8bae1dSRodney W. Grimes 		if (icmplen < ICMP_MASKLEN)
483df8bae1dSRodney W. Grimes 			break;
484df8bae1dSRodney W. Grimes 		switch (ip->ip_dst.s_addr) {
485df8bae1dSRodney W. Grimes 
486df8bae1dSRodney W. Grimes 		case INADDR_BROADCAST:
487df8bae1dSRodney W. Grimes 		case INADDR_ANY:
488df8bae1dSRodney W. Grimes 			icmpdst.sin_addr = ip->ip_src;
489df8bae1dSRodney W. Grimes 			break;
490df8bae1dSRodney W. Grimes 
491df8bae1dSRodney W. Grimes 		default:
492df8bae1dSRodney W. Grimes 			icmpdst.sin_addr = ip->ip_dst;
493df8bae1dSRodney W. Grimes 		}
494df8bae1dSRodney W. Grimes 		ia = (struct in_ifaddr *)ifaof_ifpforaddr(
495df8bae1dSRodney W. Grimes 			    (struct sockaddr *)&icmpdst, m->m_pkthdr.rcvif);
496df8bae1dSRodney W. Grimes 		if (ia == 0)
497df8bae1dSRodney W. Grimes 			break;
4987e6f7714SPoul-Henning Kamp 		if (ia->ia_ifp == 0)
4997e6f7714SPoul-Henning Kamp 			break;
500df8bae1dSRodney W. Grimes 		icp->icmp_type = ICMP_MASKREPLY;
501df8bae1dSRodney W. Grimes 		icp->icmp_mask = ia->ia_sockmask.sin_addr.s_addr;
502df8bae1dSRodney W. Grimes 		if (ip->ip_src.s_addr == 0) {
503df8bae1dSRodney W. Grimes 			if (ia->ia_ifp->if_flags & IFF_BROADCAST)
504df8bae1dSRodney W. Grimes 			    ip->ip_src = satosin(&ia->ia_broadaddr)->sin_addr;
505df8bae1dSRodney W. Grimes 			else if (ia->ia_ifp->if_flags & IFF_POINTOPOINT)
506df8bae1dSRodney W. Grimes 			    ip->ip_src = satosin(&ia->ia_dstaddr)->sin_addr;
507df8bae1dSRodney W. Grimes 		}
508df8bae1dSRodney W. Grimes reflect:
509df8bae1dSRodney W. Grimes 		ip->ip_len += hlen;	/* since ip_input deducts this */
510df8bae1dSRodney W. Grimes 		icmpstat.icps_reflect++;
511df8bae1dSRodney W. Grimes 		icmpstat.icps_outhist[icp->icmp_type]++;
512df8bae1dSRodney W. Grimes 		icmp_reflect(m);
513df8bae1dSRodney W. Grimes 		return;
514df8bae1dSRodney W. Grimes 
515df8bae1dSRodney W. Grimes 	case ICMP_REDIRECT:
51618d3153eSDag-Erling Smørgrav 		if (log_redirect) {
51718d3153eSDag-Erling Smørgrav 			u_long src, dst, gw;
51818d3153eSDag-Erling Smørgrav 
51918d3153eSDag-Erling Smørgrav 			src = ntohl(ip->ip_src.s_addr);
52018d3153eSDag-Erling Smørgrav 			dst = ntohl(icp->icmp_ip.ip_dst.s_addr);
52118d3153eSDag-Erling Smørgrav 			gw = ntohl(icp->icmp_gwaddr.s_addr);
52218d3153eSDag-Erling Smørgrav 			printf("icmp redirect from %d.%d.%d.%d: "
52318d3153eSDag-Erling Smørgrav 			       "%d.%d.%d.%d => %d.%d.%d.%d\n",
52418d3153eSDag-Erling Smørgrav 			       (int)(src >> 24), (int)((src >> 16) & 0xff),
52518d3153eSDag-Erling Smørgrav 			       (int)((src >> 8) & 0xff), (int)(src & 0xff),
52618d3153eSDag-Erling Smørgrav 			       (int)(dst >> 24), (int)((dst >> 16) & 0xff),
52718d3153eSDag-Erling Smørgrav 			       (int)((dst >> 8) & 0xff), (int)(dst & 0xff),
52818d3153eSDag-Erling Smørgrav 			       (int)(gw >> 24), (int)((gw >> 16) & 0xff),
52918d3153eSDag-Erling Smørgrav 			       (int)((gw >> 8) & 0xff), (int)(gw & 0xff));
53018d3153eSDag-Erling Smørgrav 		}
53118d3153eSDag-Erling Smørgrav 		if (drop_redirect)
53218d3153eSDag-Erling Smørgrav 			break;
533df8bae1dSRodney W. Grimes 		if (code > 3)
534df8bae1dSRodney W. Grimes 			goto badcode;
535df8bae1dSRodney W. Grimes 		if (icmplen < ICMP_ADVLENMIN || icmplen < ICMP_ADVLEN(icp) ||
5365e2d0696SGarrett Wollman 		    IP_VHL_HL(icp->icmp_ip.ip_vhl) < (sizeof(struct ip) >> 2)) {
537df8bae1dSRodney W. Grimes 			icmpstat.icps_badlen++;
538df8bae1dSRodney W. Grimes 			break;
539df8bae1dSRodney W. Grimes 		}
540df8bae1dSRodney W. Grimes 		/*
541df8bae1dSRodney W. Grimes 		 * Short circuit routing redirects to force
542df8bae1dSRodney W. Grimes 		 * immediate change in the kernel's routing
543df8bae1dSRodney W. Grimes 		 * tables.  The message is also handed to anyone
544df8bae1dSRodney W. Grimes 		 * listening on a raw socket (e.g. the routing
545df8bae1dSRodney W. Grimes 		 * daemon for use in updating its tables).
546df8bae1dSRodney W. Grimes 		 */
547df8bae1dSRodney W. Grimes 		icmpgw.sin_addr = ip->ip_src;
548df8bae1dSRodney W. Grimes 		icmpdst.sin_addr = icp->icmp_gwaddr;
549df8bae1dSRodney W. Grimes #ifdef	ICMPPRINTFS
5502b758395SGarrett Wollman 		if (icmpprintfs) {
5512b758395SGarrett Wollman 			char buf[4 * sizeof "123"];
5522b758395SGarrett Wollman 			strcpy(buf, inet_ntoa(icp->icmp_ip.ip_dst));
5532b758395SGarrett Wollman 
5542b758395SGarrett Wollman 			printf("redirect dst %s to %s\n",
5552b758395SGarrett Wollman 			       buf, inet_ntoa(icp->icmp_gwaddr));
5562b758395SGarrett Wollman 		}
557df8bae1dSRodney W. Grimes #endif
558df8bae1dSRodney W. Grimes 		icmpsrc.sin_addr = icp->icmp_ip.ip_dst;
559df8bae1dSRodney W. Grimes 		rtredirect((struct sockaddr *)&icmpsrc,
560df8bae1dSRodney W. Grimes 		  (struct sockaddr *)&icmpdst,
561df8bae1dSRodney W. Grimes 		  (struct sockaddr *)0, RTF_GATEWAY | RTF_HOST,
562df8bae1dSRodney W. Grimes 		  (struct sockaddr *)&icmpgw, (struct rtentry **)0);
563df8bae1dSRodney W. Grimes 		pfctlinput(PRC_REDIRECT_HOST, (struct sockaddr *)&icmpsrc);
5646a800098SYoshinobu Inoue #ifdef IPSEC
5656a800098SYoshinobu Inoue 		key_sa_routechange((struct sockaddr *)&icmpsrc);
5666a800098SYoshinobu Inoue #endif
567df8bae1dSRodney W. Grimes 		break;
568df8bae1dSRodney W. Grimes 
569df8bae1dSRodney W. Grimes 	/*
570df8bae1dSRodney W. Grimes 	 * No kernel processing for the following;
571df8bae1dSRodney W. Grimes 	 * just fall through to send to raw listener.
572df8bae1dSRodney W. Grimes 	 */
573df8bae1dSRodney W. Grimes 	case ICMP_ECHOREPLY:
574df8bae1dSRodney W. Grimes 	case ICMP_ROUTERADVERT:
575df8bae1dSRodney W. Grimes 	case ICMP_ROUTERSOLICIT:
576df8bae1dSRodney W. Grimes 	case ICMP_TSTAMPREPLY:
577df8bae1dSRodney W. Grimes 	case ICMP_IREQREPLY:
578df8bae1dSRodney W. Grimes 	case ICMP_MASKREPLY:
579df8bae1dSRodney W. Grimes 	default:
580df8bae1dSRodney W. Grimes 		break;
581df8bae1dSRodney W. Grimes 	}
582df8bae1dSRodney W. Grimes 
583df8bae1dSRodney W. Grimes raw:
5846a800098SYoshinobu Inoue 	rip_input(m, off, proto);
585df8bae1dSRodney W. Grimes 	return;
586df8bae1dSRodney W. Grimes 
587df8bae1dSRodney W. Grimes freeit:
588df8bae1dSRodney W. Grimes 	m_freem(m);
589df8bae1dSRodney W. Grimes }
590df8bae1dSRodney W. Grimes 
591df8bae1dSRodney W. Grimes /*
592df8bae1dSRodney W. Grimes  * Reflect the ip packet back to the source
593df8bae1dSRodney W. Grimes  */
5940312fbe9SPoul-Henning Kamp static void
595df8bae1dSRodney W. Grimes icmp_reflect(m)
596df8bae1dSRodney W. Grimes 	struct mbuf *m;
597df8bae1dSRodney W. Grimes {
598df8bae1dSRodney W. Grimes 	register struct ip *ip = mtod(m, struct ip *);
599df8bae1dSRodney W. Grimes 	register struct in_ifaddr *ia;
600df8bae1dSRodney W. Grimes 	struct in_addr t;
601b5e8ce9fSBruce Evans 	struct mbuf *opts = 0;
6025e2d0696SGarrett Wollman 	int optlen = (IP_VHL_HL(ip->ip_vhl) << 2) - sizeof(struct ip);
603df8bae1dSRodney W. Grimes 
604df8bae1dSRodney W. Grimes 	if (!in_canforward(ip->ip_src) &&
605df8bae1dSRodney W. Grimes 	    ((ntohl(ip->ip_src.s_addr) & IN_CLASSA_NET) !=
606df8bae1dSRodney W. Grimes 	     (IN_LOOPBACKNET << IN_CLASSA_NSHIFT))) {
607df8bae1dSRodney W. Grimes 		m_freem(m);	/* Bad return address */
608df8bae1dSRodney W. Grimes 		goto done;	/* Ip_output() will check for broadcast */
609df8bae1dSRodney W. Grimes 	}
610df8bae1dSRodney W. Grimes 	t = ip->ip_dst;
611df8bae1dSRodney W. Grimes 	ip->ip_dst = ip->ip_src;
612df8bae1dSRodney W. Grimes 	/*
613df8bae1dSRodney W. Grimes 	 * If the incoming packet was addressed directly to us,
614df8bae1dSRodney W. Grimes 	 * use dst as the src for the reply.  Otherwise (broadcast
615df8bae1dSRodney W. Grimes 	 * or anonymous), use the address which corresponds
616df8bae1dSRodney W. Grimes 	 * to the incoming interface.
617df8bae1dSRodney W. Grimes 	 */
61859562606SGarrett Wollman 	for (ia = in_ifaddrhead.tqh_first; ia; ia = ia->ia_link.tqe_next) {
619df8bae1dSRodney W. Grimes 		if (t.s_addr == IA_SIN(ia)->sin_addr.s_addr)
620df8bae1dSRodney W. Grimes 			break;
6217e6f7714SPoul-Henning Kamp 		if (ia->ia_ifp && (ia->ia_ifp->if_flags & IFF_BROADCAST) &&
622df8bae1dSRodney W. Grimes 		    t.s_addr == satosin(&ia->ia_broadaddr)->sin_addr.s_addr)
623df8bae1dSRodney W. Grimes 			break;
624df8bae1dSRodney W. Grimes 	}
625df8bae1dSRodney W. Grimes 	icmpdst.sin_addr = t;
62641fbdc96SJulian Elischer 	if ((ia == (struct in_ifaddr *)0) && m->m_pkthdr.rcvif)
627df8bae1dSRodney W. Grimes 		ia = (struct in_ifaddr *)ifaof_ifpforaddr(
628df8bae1dSRodney W. Grimes 			(struct sockaddr *)&icmpdst, m->m_pkthdr.rcvif);
629df8bae1dSRodney W. Grimes 	/*
630df8bae1dSRodney W. Grimes 	 * The following happens if the packet was not addressed to us,
631df8bae1dSRodney W. Grimes 	 * and was received on an interface with no IP address.
632df8bae1dSRodney W. Grimes 	 */
633df8bae1dSRodney W. Grimes 	if (ia == (struct in_ifaddr *)0)
63459562606SGarrett Wollman 		ia = in_ifaddrhead.tqh_first;
635df8bae1dSRodney W. Grimes 	t = IA_SIN(ia)->sin_addr;
636df8bae1dSRodney W. Grimes 	ip->ip_src = t;
637df8bae1dSRodney W. Grimes 	ip->ip_ttl = MAXTTL;
638df8bae1dSRodney W. Grimes 
639df8bae1dSRodney W. Grimes 	if (optlen > 0) {
640df8bae1dSRodney W. Grimes 		register u_char *cp;
641df8bae1dSRodney W. Grimes 		int opt, cnt;
642df8bae1dSRodney W. Grimes 		u_int len;
643df8bae1dSRodney W. Grimes 
644df8bae1dSRodney W. Grimes 		/*
645df8bae1dSRodney W. Grimes 		 * Retrieve any source routing from the incoming packet;
646df8bae1dSRodney W. Grimes 		 * add on any record-route or timestamp options.
647df8bae1dSRodney W. Grimes 		 */
648df8bae1dSRodney W. Grimes 		cp = (u_char *) (ip + 1);
649df8bae1dSRodney W. Grimes 		if ((opts = ip_srcroute()) == 0 &&
650df8bae1dSRodney W. Grimes 		    (opts = m_gethdr(M_DONTWAIT, MT_HEADER))) {
651df8bae1dSRodney W. Grimes 			opts->m_len = sizeof(struct in_addr);
652df8bae1dSRodney W. Grimes 			mtod(opts, struct in_addr *)->s_addr = 0;
653df8bae1dSRodney W. Grimes 		}
654df8bae1dSRodney W. Grimes 		if (opts) {
655df8bae1dSRodney W. Grimes #ifdef ICMPPRINTFS
656df8bae1dSRodney W. Grimes 		    if (icmpprintfs)
657df8bae1dSRodney W. Grimes 			    printf("icmp_reflect optlen %d rt %d => ",
658df8bae1dSRodney W. Grimes 				optlen, opts->m_len);
659df8bae1dSRodney W. Grimes #endif
660df8bae1dSRodney W. Grimes 		    for (cnt = optlen; cnt > 0; cnt -= len, cp += len) {
661df8bae1dSRodney W. Grimes 			    opt = cp[IPOPT_OPTVAL];
662df8bae1dSRodney W. Grimes 			    if (opt == IPOPT_EOL)
663df8bae1dSRodney W. Grimes 				    break;
664df8bae1dSRodney W. Grimes 			    if (opt == IPOPT_NOP)
665df8bae1dSRodney W. Grimes 				    len = 1;
666df8bae1dSRodney W. Grimes 			    else {
667df8bae1dSRodney W. Grimes 				    len = cp[IPOPT_OLEN];
668df8bae1dSRodney W. Grimes 				    if (len <= 0 || len > cnt)
669df8bae1dSRodney W. Grimes 					    break;
670df8bae1dSRodney W. Grimes 			    }
671df8bae1dSRodney W. Grimes 			    /*
672df8bae1dSRodney W. Grimes 			     * Should check for overflow, but it "can't happen"
673df8bae1dSRodney W. Grimes 			     */
674df8bae1dSRodney W. Grimes 			    if (opt == IPOPT_RR || opt == IPOPT_TS ||
675df8bae1dSRodney W. Grimes 				opt == IPOPT_SECURITY) {
676df8bae1dSRodney W. Grimes 				    bcopy((caddr_t)cp,
677df8bae1dSRodney W. Grimes 					mtod(opts, caddr_t) + opts->m_len, len);
678df8bae1dSRodney W. Grimes 				    opts->m_len += len;
679df8bae1dSRodney W. Grimes 			    }
680df8bae1dSRodney W. Grimes 		    }
681df8bae1dSRodney W. Grimes 		    /* Terminate & pad, if necessary */
682623ae52eSPoul-Henning Kamp 		    cnt = opts->m_len % 4;
683623ae52eSPoul-Henning Kamp 		    if (cnt) {
684df8bae1dSRodney W. Grimes 			    for (; cnt < 4; cnt++) {
685df8bae1dSRodney W. Grimes 				    *(mtod(opts, caddr_t) + opts->m_len) =
686df8bae1dSRodney W. Grimes 					IPOPT_EOL;
687df8bae1dSRodney W. Grimes 				    opts->m_len++;
688df8bae1dSRodney W. Grimes 			    }
689df8bae1dSRodney W. Grimes 		    }
690df8bae1dSRodney W. Grimes #ifdef ICMPPRINTFS
691df8bae1dSRodney W. Grimes 		    if (icmpprintfs)
692df8bae1dSRodney W. Grimes 			    printf("%d\n", opts->m_len);
693df8bae1dSRodney W. Grimes #endif
694df8bae1dSRodney W. Grimes 		}
695df8bae1dSRodney W. Grimes 		/*
696df8bae1dSRodney W. Grimes 		 * Now strip out original options by copying rest of first
697df8bae1dSRodney W. Grimes 		 * mbuf's data back, and adjust the IP length.
698df8bae1dSRodney W. Grimes 		 */
699df8bae1dSRodney W. Grimes 		ip->ip_len -= optlen;
7005e2d0696SGarrett Wollman 		ip->ip_vhl = IP_VHL_BORING;
701df8bae1dSRodney W. Grimes 		m->m_len -= optlen;
702df8bae1dSRodney W. Grimes 		if (m->m_flags & M_PKTHDR)
703df8bae1dSRodney W. Grimes 			m->m_pkthdr.len -= optlen;
704df8bae1dSRodney W. Grimes 		optlen += sizeof(struct ip);
705df8bae1dSRodney W. Grimes 		bcopy((caddr_t)ip + optlen, (caddr_t)(ip + 1),
706df8bae1dSRodney W. Grimes 			 (unsigned)(m->m_len - sizeof(struct ip)));
707df8bae1dSRodney W. Grimes 	}
708df8bae1dSRodney W. Grimes 	m->m_flags &= ~(M_BCAST|M_MCAST);
709df8bae1dSRodney W. Grimes 	icmp_send(m, opts);
710df8bae1dSRodney W. Grimes done:
711df8bae1dSRodney W. Grimes 	if (opts)
712df8bae1dSRodney W. Grimes 		(void)m_free(opts);
713df8bae1dSRodney W. Grimes }
714df8bae1dSRodney W. Grimes 
715df8bae1dSRodney W. Grimes /*
716df8bae1dSRodney W. Grimes  * Send an icmp packet back to the ip level,
717df8bae1dSRodney W. Grimes  * after supplying a checksum.
718df8bae1dSRodney W. Grimes  */
7190312fbe9SPoul-Henning Kamp static void
720df8bae1dSRodney W. Grimes icmp_send(m, opts)
721df8bae1dSRodney W. Grimes 	register struct mbuf *m;
722df8bae1dSRodney W. Grimes 	struct mbuf *opts;
723df8bae1dSRodney W. Grimes {
724df8bae1dSRodney W. Grimes 	register struct ip *ip = mtod(m, struct ip *);
725df8bae1dSRodney W. Grimes 	register int hlen;
726df8bae1dSRodney W. Grimes 	register struct icmp *icp;
727d3d20ad1SGarrett Wollman 	struct route ro;
728df8bae1dSRodney W. Grimes 
7295e2d0696SGarrett Wollman 	hlen = IP_VHL_HL(ip->ip_vhl) << 2;
730df8bae1dSRodney W. Grimes 	m->m_data += hlen;
731df8bae1dSRodney W. Grimes 	m->m_len -= hlen;
732df8bae1dSRodney W. Grimes 	icp = mtod(m, struct icmp *);
733df8bae1dSRodney W. Grimes 	icp->icmp_cksum = 0;
734df8bae1dSRodney W. Grimes 	icp->icmp_cksum = in_cksum(m, ip->ip_len - hlen);
735df8bae1dSRodney W. Grimes 	m->m_data -= hlen;
736df8bae1dSRodney W. Grimes 	m->m_len += hlen;
73794446a2eSArchie Cobbs 	m->m_pkthdr.rcvif = (struct ifnet *)0;
738df8bae1dSRodney W. Grimes #ifdef ICMPPRINTFS
7392b758395SGarrett Wollman 	if (icmpprintfs) {
7402b758395SGarrett Wollman 		char buf[4 * sizeof "123"];
7412b758395SGarrett Wollman 		strcpy(buf, inet_ntoa(ip->ip_dst));
7422b758395SGarrett Wollman 		printf("icmp_send dst %s src %s\n",
7432b758395SGarrett Wollman 		       buf, inet_ntoa(ip->ip_src));
7442b758395SGarrett Wollman 	}
745df8bae1dSRodney W. Grimes #endif
746d3d20ad1SGarrett Wollman 	bzero(&ro, sizeof ro);
747d3d20ad1SGarrett Wollman 	(void) ip_output(m, opts, &ro, 0, NULL);
748d3d20ad1SGarrett Wollman 	if (ro.ro_rt)
749d3d20ad1SGarrett Wollman 		RTFREE(ro.ro_rt);
750df8bae1dSRodney W. Grimes }
751df8bae1dSRodney W. Grimes 
752df8bae1dSRodney W. Grimes n_time
753df8bae1dSRodney W. Grimes iptime()
754df8bae1dSRodney W. Grimes {
755df8bae1dSRodney W. Grimes 	struct timeval atv;
756df8bae1dSRodney W. Grimes 	u_long t;
757df8bae1dSRodney W. Grimes 
758df8bae1dSRodney W. Grimes 	microtime(&atv);
759df8bae1dSRodney W. Grimes 	t = (atv.tv_sec % (24*60*60)) * 1000 + atv.tv_usec / 1000;
760df8bae1dSRodney W. Grimes 	return (htonl(t));
761df8bae1dSRodney W. Grimes }
762df8bae1dSRodney W. Grimes 
763b7a44e34SGarrett Wollman #if 1
7645cbf3e08SGarrett Wollman /*
7655cbf3e08SGarrett Wollman  * Return the next larger or smaller MTU plateau (table from RFC 1191)
7665cbf3e08SGarrett Wollman  * given current value MTU.  If DIR is less than zero, a larger plateau
7675cbf3e08SGarrett Wollman  * is returned; otherwise, a smaller value is returned.
7685cbf3e08SGarrett Wollman  */
769f708ef1bSPoul-Henning Kamp static int
7705cbf3e08SGarrett Wollman ip_next_mtu(mtu, dir)
7715cbf3e08SGarrett Wollman 	int mtu;
7725cbf3e08SGarrett Wollman 	int dir;
7735cbf3e08SGarrett Wollman {
7745cbf3e08SGarrett Wollman 	static int mtutab[] = {
7755cbf3e08SGarrett Wollman 		65535, 32000, 17914, 8166, 4352, 2002, 1492, 1006, 508, 296,
7765cbf3e08SGarrett Wollman 		68, 0
7775cbf3e08SGarrett Wollman 	};
7785cbf3e08SGarrett Wollman 	int i;
7795cbf3e08SGarrett Wollman 
7805cbf3e08SGarrett Wollman 	for (i = 0; i < (sizeof mtutab) / (sizeof mtutab[0]); i++) {
7815cbf3e08SGarrett Wollman 		if (mtu >= mtutab[i])
7825cbf3e08SGarrett Wollman 			break;
7835cbf3e08SGarrett Wollman 	}
7845cbf3e08SGarrett Wollman 
7855cbf3e08SGarrett Wollman 	if (dir < 0) {
7865cbf3e08SGarrett Wollman 		if (i == 0) {
7875cbf3e08SGarrett Wollman 			return 0;
7885cbf3e08SGarrett Wollman 		} else {
7895cbf3e08SGarrett Wollman 			return mtutab[i - 1];
7905cbf3e08SGarrett Wollman 		}
7915cbf3e08SGarrett Wollman 	} else {
7925cbf3e08SGarrett Wollman 		if (mtutab[i] == 0) {
7935cbf3e08SGarrett Wollman 			return 0;
7945cbf3e08SGarrett Wollman 		} else if(mtu > mtutab[i]) {
7955cbf3e08SGarrett Wollman 			return mtutab[i];
7965cbf3e08SGarrett Wollman 		} else {
7975cbf3e08SGarrett Wollman 			return mtutab[i + 1];
7985cbf3e08SGarrett Wollman 		}
7995cbf3e08SGarrett Wollman 	}
8005cbf3e08SGarrett Wollman }
801b7a44e34SGarrett Wollman #endif
80251508de1SMatthew Dillon 
80351508de1SMatthew Dillon #ifdef ICMP_BANDLIM
80451508de1SMatthew Dillon 
80551508de1SMatthew Dillon /*
80651508de1SMatthew Dillon  * badport_bandlim() - check for ICMP bandwidth limit
80751508de1SMatthew Dillon  *
80851508de1SMatthew Dillon  *	Return 0 if it is ok to send an ICMP error response, -1 if we have
80951508de1SMatthew Dillon  *	hit our bandwidth limit and it is not ok.
81051508de1SMatthew Dillon  *
81151508de1SMatthew Dillon  *	If icmplim is <= 0, the feature is disabled and 0 is returned.
81251508de1SMatthew Dillon  *
81351508de1SMatthew Dillon  *	For now we separate the TCP and UDP subsystems w/ different 'which'
81451508de1SMatthew Dillon  *	values.  We may eventually remove this separation (and simplify the
81551508de1SMatthew Dillon  *	code further).
81651508de1SMatthew Dillon  *
81751508de1SMatthew Dillon  *	Note that the printing of the error message is delayed so we can
81851508de1SMatthew Dillon  *	properly print the icmp error rate that the system was trying to do
81951508de1SMatthew Dillon  *	(i.e. 22000/100 pps, etc...).  This can cause long delays in printing
82051508de1SMatthew Dillon  *	the 'final' error, but it doesn't make sense to solve the printing
82151508de1SMatthew Dillon  *	delay with more complex code.
82251508de1SMatthew Dillon  */
82351508de1SMatthew Dillon 
82451508de1SMatthew Dillon int
82551508de1SMatthew Dillon badport_bandlim(int which)
82651508de1SMatthew Dillon {
82751508de1SMatthew Dillon 	static int lticks[2];
82851508de1SMatthew Dillon 	static int lpackets[2];
82951508de1SMatthew Dillon 	int dticks;
83051508de1SMatthew Dillon 
83151508de1SMatthew Dillon 	/*
83251508de1SMatthew Dillon 	 * Return ok status if feature disabled or argument out of
83351508de1SMatthew Dillon 	 * ranage.
83451508de1SMatthew Dillon 	 */
83551508de1SMatthew Dillon 
83651508de1SMatthew Dillon 	if (icmplim <= 0 || which >= 2 || which < 0)
83751508de1SMatthew Dillon 		return(0);
83851508de1SMatthew Dillon 	dticks = ticks - lticks[which];
83951508de1SMatthew Dillon 
84051508de1SMatthew Dillon 	/*
84151508de1SMatthew Dillon 	 * reset stats when cumulative dt exceeds one second.
84251508de1SMatthew Dillon 	 */
84351508de1SMatthew Dillon 
84451508de1SMatthew Dillon 	if ((unsigned int)dticks > hz) {
84551508de1SMatthew Dillon 		if (lpackets[which] > icmplim) {
84651508de1SMatthew Dillon 			printf("icmp-response bandwidth limit %d/%d pps\n",
84751508de1SMatthew Dillon 				lpackets[which],
84851508de1SMatthew Dillon 				icmplim
84951508de1SMatthew Dillon 			);
85051508de1SMatthew Dillon 		}
85151508de1SMatthew Dillon 		lticks[which] = ticks;
85251508de1SMatthew Dillon 		lpackets[which] = 0;
85351508de1SMatthew Dillon 	}
85451508de1SMatthew Dillon 
85551508de1SMatthew Dillon 	/*
85651508de1SMatthew Dillon 	 * bump packet count
85751508de1SMatthew Dillon 	 */
85851508de1SMatthew Dillon 
85951508de1SMatthew Dillon 	if (++lpackets[which] > icmplim) {
86051508de1SMatthew Dillon 		return(-1);
86151508de1SMatthew Dillon 	}
86251508de1SMatthew Dillon 	return(0);
86351508de1SMatthew Dillon }
86451508de1SMatthew Dillon 
86551508de1SMatthew Dillon #endif
86651508de1SMatthew Dillon 
86751508de1SMatthew Dillon 
868