1139823Simp/*-
21541Srgrimes * Copyright (c) 1982, 1986, 1988, 1993
31541Srgrimes *	The Regents of the University of California.  All rights reserved.
41541Srgrimes *
51541Srgrimes * Redistribution and use in source and binary forms, with or without
61541Srgrimes * modification, are permitted provided that the following conditions
71541Srgrimes * are met:
81541Srgrimes * 1. Redistributions of source code must retain the above copyright
91541Srgrimes *    notice, this list of conditions and the following disclaimer.
101541Srgrimes * 2. Redistributions in binary form must reproduce the above copyright
111541Srgrimes *    notice, this list of conditions and the following disclaimer in the
121541Srgrimes *    documentation and/or other materials provided with the distribution.
131541Srgrimes * 4. Neither the name of the University nor the names of its contributors
141541Srgrimes *    may be used to endorse or promote products derived from this software
151541Srgrimes *    without specific prior written permission.
161541Srgrimes *
171541Srgrimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
181541Srgrimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
191541Srgrimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
201541Srgrimes * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
211541Srgrimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
221541Srgrimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
231541Srgrimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
241541Srgrimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
251541Srgrimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
261541Srgrimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
271541Srgrimes * SUCH DAMAGE.
281541Srgrimes *
291541Srgrimes *	@(#)ip_icmp.c	8.2 (Berkeley) 1/4/94
301541Srgrimes */
311541Srgrimes
32172467Ssilby#include <sys/cdefs.h>
33172467Ssilby__FBSDID("$FreeBSD: stable/10/sys/netinet/ip_icmp.c 341258 2018-11-29 20:14:09Z emaste $");
34172467Ssilby
35221134Sbz#include "opt_inet.h"
3655009Sshin
371541Srgrimes#include <sys/param.h>
381541Srgrimes#include <sys/systm.h>
391541Srgrimes#include <sys/mbuf.h>
401541Srgrimes#include <sys/protosw.h>
411541Srgrimes#include <sys/socket.h>
421541Srgrimes#include <sys/time.h>
431541Srgrimes#include <sys/kernel.h>
447090Sbde#include <sys/sysctl.h>
45211316Sandre#include <sys/syslog.h>
461541Srgrimes
471541Srgrimes#include <net/if.h>
4883934Sbrooks#include <net/if_types.h>
491541Srgrimes#include <net/route.h>
50195699Srwatson#include <net/vnet.h>
511541Srgrimes
521541Srgrimes#include <netinet/in.h>
53122922Sandre#include <netinet/in_pcb.h>
541541Srgrimes#include <netinet/in_systm.h>
551541Srgrimes#include <netinet/in_var.h>
561541Srgrimes#include <netinet/ip.h>
571541Srgrimes#include <netinet/ip_icmp.h>
587090Sbde#include <netinet/ip_var.h>
59152592Sandre#include <netinet/ip_options.h>
60122922Sandre#include <netinet/tcp.h>
61122922Sandre#include <netinet/tcp_var.h>
62122922Sandre#include <netinet/tcpip.h>
631541Srgrimes#include <netinet/icmp_var.h>
641541Srgrimes
65221134Sbz#ifdef INET
66105199Ssam
6760105Sjlemon#include <machine/in_cksum.h>
6860105Sjlemon
69163606Srwatson#include <security/mac/mac_framework.h>
70221134Sbz#endif /* INET */
71163606Srwatson
721541Srgrimes/*
731541Srgrimes * ICMP routines: error generation, receive packet processing, and
741541Srgrimes * routines to turnaround packets back to the originator, and
751541Srgrimes * host table maintenance routines.
761541Srgrimes */
77221134Sbzstatic VNET_DEFINE(int, icmplim) = 200;
78221134Sbz#define	V_icmplim			VNET(icmplim)
79221134SbzSYSCTL_VNET_INT(_net_inet_icmp, ICMPCTL_ICMPLIM, icmplim, CTLFLAG_RW,
80221134Sbz	&VNET_NAME(icmplim), 0,
81221134Sbz	"Maximum number of ICMP responses per second");
82221134Sbz
83221134Sbzstatic VNET_DEFINE(int, icmplim_output) = 1;
84221134Sbz#define	V_icmplim_output		VNET(icmplim_output)
85221134SbzSYSCTL_VNET_INT(_net_inet_icmp, OID_AUTO, icmplim_output, CTLFLAG_RW,
86221134Sbz	&VNET_NAME(icmplim_output), 0,
87229749Seadler	"Enable logging of ICMP response rate limiting");
88221134Sbz
89221134Sbz#ifdef INET
90253084SaeVNET_PCPUSTAT_DEFINE(struct icmpstat, icmpstat);
91253084SaeVNET_PCPUSTAT_SYSINIT(icmpstat);
92253084SaeSYSCTL_VNET_PCPUSTAT(_net_inet_icmp, ICMPCTL_STATS, stats, struct icmpstat,
93253084Sae    icmpstat, "ICMP statistics (struct icmpstat, netinet/icmp_var.h)");
94195699Srwatson
95253084Sae#ifdef VIMAGE
96253084SaeVNET_PCPUSTAT_SYSUNINIT(icmpstat);
97253084Sae#endif /* VIMAGE */
98253084Sae
99215701Sdimstatic VNET_DEFINE(int, icmpmaskrepl) = 0;
100207369Sbz#define	V_icmpmaskrepl			VNET(icmpmaskrepl)
101195699SrwatsonSYSCTL_VNET_INT(_net_inet_icmp, ICMPCTL_MASKREPL, maskrepl, CTLFLAG_RW,
102195699Srwatson	&VNET_NAME(icmpmaskrepl), 0,
103183550Szec	"Reply to ICMP Address Mask Request packets.");
10412296Sphk
105215701Sdimstatic VNET_DEFINE(u_int, icmpmaskfake) = 0;
106207369Sbz#define	V_icmpmaskfake			VNET(icmpmaskfake)
107195699SrwatsonSYSCTL_VNET_UINT(_net_inet_icmp, OID_AUTO, maskfake, CTLFLAG_RW,
108195699Srwatson	&VNET_NAME(icmpmaskfake), 0,
109195699Srwatson	"Fake reply to ICMP Address Mask Request packets.");
110112465Smdodd
111241406SmelifaroVNET_DEFINE(int, drop_redirect) = 0;
11251282Sdes
113215701Sdimstatic VNET_DEFINE(int, log_redirect) = 0;
114207369Sbz#define	V_log_redirect			VNET(log_redirect)
115195699SrwatsonSYSCTL_VNET_INT(_net_inet_icmp, OID_AUTO, log_redirect, CTLFLAG_RW,
116195699Srwatson	&VNET_NAME(log_redirect), 0,
117195699Srwatson	"Log ICMP redirects to the console");
11849603Sdes
119215701Sdimstatic VNET_DEFINE(char, reply_src[IFNAMSIZ]);
120207369Sbz#define	V_reply_src			VNET(reply_src)
121195699SrwatsonSYSCTL_VNET_STRING(_net_inet_icmp, OID_AUTO, reply_src, CTLFLAG_RW,
122195699Srwatson	&VNET_NAME(reply_src), IFNAMSIZ,
123183550Szec	"icmp reply source for non-local packets.");
124125360Sandre
125215701Sdimstatic VNET_DEFINE(int, icmp_rfi) = 0;
126207369Sbz#define	V_icmp_rfi			VNET(icmp_rfi)
127195699SrwatsonSYSCTL_VNET_INT(_net_inet_icmp, OID_AUTO, reply_from_interface, CTLFLAG_RW,
128195699Srwatson	&VNET_NAME(icmp_rfi), 0,
129195699Srwatson	"ICMP reply from incoming interface for non-local packets");
130149347Sandre
131215701Sdimstatic VNET_DEFINE(int, icmp_quotelen) = 8;
132207369Sbz#define	V_icmp_quotelen			VNET(icmp_quotelen)
133195699SrwatsonSYSCTL_VNET_INT(_net_inet_icmp, OID_AUTO, quotelen, CTLFLAG_RW,
134195699Srwatson	&VNET_NAME(icmp_quotelen), 0,
135195699Srwatson	"Number of bytes from original packet to quote in ICMP reply");
136149349Sandre
13741497Sdillon/*
13841497Sdillon * ICMP broadcast echo sysctl
13941497Sdillon */
140215701Sdimstatic VNET_DEFINE(int, icmpbmcastecho) = 0;
141207369Sbz#define	V_icmpbmcastecho		VNET(icmpbmcastecho)
142195699SrwatsonSYSCTL_VNET_INT(_net_inet_icmp, OID_AUTO, bmcastecho, CTLFLAG_RW,
143195699Srwatson	&VNET_NAME(icmpbmcastecho), 0,
144195699Srwatson	"");
14528683Swollman
146281609Smarkjstatic VNET_DEFINE(int, icmptstamprepl) = 1;
147281609Smarkj#define	V_icmptstamprepl		VNET(icmptstamprepl)
148281609SmarkjSYSCTL_INT(_net_inet_icmp, OID_AUTO, tstamprepl, CTLFLAG_RW,
149281609Smarkj	&VNET_NAME(icmptstamprepl), 0, "Respond to ICMP Timestamp packets");
15041487Sdillon
151340672Seugenstatic VNET_DEFINE(int, error_keeptags) = 0;
152340671Seugen#define	V_error_keeptags		VNET(error_keeptags)
153340671SeugenSYSCTL_INT(_net_inet_icmp, OID_AUTO, error_keeptags, CTLFLAG_VNET | CTLFLAG_RW,
154340671Seugen	&VNET_NAME(error_keeptags), 0,
155340671Seugen	"ICMP error response keeps copy of mbuf_tags of original packet");
156340671Seugen
1571541Srgrimes#ifdef ICMPPRINTFS
1581541Srgrimesint	icmpprintfs = 0;
1591541Srgrimes#endif
1601541Srgrimes
16192723Salfredstatic void	icmp_reflect(struct mbuf *);
162122708Sandrestatic void	icmp_send(struct mbuf *, struct mbuf *);
16312296Sphk
1641541Srgrimesextern	struct protosw inetsw[];
1651541Srgrimes
166241406Smelifarostatic int
167241406Smelifarosysctl_net_icmp_drop_redir(SYSCTL_HANDLER_ARGS)
168241406Smelifaro{
169241406Smelifaro	int error, new;
170241406Smelifaro	int i;
171241406Smelifaro	struct radix_node_head *rnh;
172241406Smelifaro
173241406Smelifaro	new = V_drop_redirect;
174241406Smelifaro	error = sysctl_handle_int(oidp, &new, 0, req);
175241406Smelifaro	if (error == 0 && req->newptr) {
176241406Smelifaro		new = (new != 0) ? 1 : 0;
177241406Smelifaro
178241406Smelifaro		if (new == V_drop_redirect)
179241406Smelifaro			return (0);
180241406Smelifaro
181241406Smelifaro		for (i = 0; i < rt_numfibs; i++) {
182241406Smelifaro			if ((rnh = rt_tables_get_rnh(i, AF_INET)) == NULL)
183241406Smelifaro				continue;
184241406Smelifaro			RADIX_NODE_HEAD_LOCK(rnh);
185241406Smelifaro			in_setmatchfunc(rnh, new);
186241406Smelifaro			RADIX_NODE_HEAD_UNLOCK(rnh);
187241406Smelifaro		}
188241406Smelifaro
189241406Smelifaro		V_drop_redirect = new;
190241406Smelifaro	}
191241406Smelifaro
192241406Smelifaro	return (error);
193241406Smelifaro}
194241406Smelifaro
195241406SmelifaroSYSCTL_VNET_PROC(_net_inet_icmp, OID_AUTO, drop_redirect,
196241406Smelifaro    CTLTYPE_INT|CTLFLAG_RW, 0, 0,
197241406Smelifaro    sysctl_net_icmp_drop_redir, "I", "Ignore ICMP redirects");
198241406Smelifaro
1991541Srgrimes/*
200196039Srwatson * Kernel module interface for updating icmpstat.  The argument is an index
201196039Srwatson * into icmpstat treated as an array of u_long.  While this encodes the
202196039Srwatson * general layout of icmpstat into the caller, it doesn't encode its
203196039Srwatson * location, so that future changes to add, for example, per-CPU stats
204196039Srwatson * support won't cause binary compatibility problems for kernel modules.
205196039Srwatson */
206196039Srwatsonvoid
207196039Srwatsonkmod_icmpstat_inc(int statnum)
208196039Srwatson{
209196039Srwatson
210253084Sae	counter_u64_add(VNET(icmpstat)[statnum], 1);
211196039Srwatson}
212196039Srwatson
213196039Srwatson/*
2141541Srgrimes * Generate an error packet of type error
2151541Srgrimes * in response to bad packet ip.
2161541Srgrimes */
2171541Srgrimesvoid
218188578Sluigiicmp_error(struct mbuf *n, int type, int code, uint32_t dest, int mtu)
2191541Srgrimes{
2201541Srgrimes	register struct ip *oip = mtod(n, struct ip *), *nip;
221152582Sandre	register unsigned oiphlen = oip->ip_hl << 2;
2221541Srgrimes	register struct icmp *icp;
2231541Srgrimes	register struct mbuf *m;
224152582Sandre	unsigned icmplen, icmpelen, nlen;
2251541Srgrimes
226152582Sandre	KASSERT((u_int)type <= ICMP_MAXTYPE, ("%s: illegal ICMP type", __func__));
2271541Srgrimes#ifdef ICMPPRINTFS
2281541Srgrimes	if (icmpprintfs)
2293311Sphk		printf("icmp_error(%p, %x, %d)\n", oip, type, code);
2301541Srgrimes#endif
2311541Srgrimes	if (type != ICMP_REDIRECT)
232190964Srwatson		ICMPSTAT_INC(icps_error);
2331541Srgrimes	/*
234152582Sandre	 * Don't send error:
235152582Sandre	 *  if the original packet was encrypted.
236152582Sandre	 *  if not the first fragment of message.
237152582Sandre	 *  in response to a multicast or broadcast packet.
238152582Sandre	 *  if the old packet protocol was an ICMP error message.
2391541Srgrimes	 */
240130183Sume	if (n->m_flags & M_DECRYPTED)
241130183Sume		goto freeit;
242241913Sglebius	if (oip->ip_off & htons(~(IP_MF|IP_DF)))
2431541Srgrimes		goto freeit;
244152582Sandre	if (n->m_flags & (M_BCAST|M_MCAST))
245152582Sandre		goto freeit;
2461541Srgrimes	if (oip->ip_p == IPPROTO_ICMP && type != ICMP_REDIRECT &&
247152582Sandre	  n->m_len >= oiphlen + ICMP_MINLEN &&
248152582Sandre	  !ICMP_INFOTYPE(((struct icmp *)((caddr_t)oip + oiphlen))->icmp_type)) {
249190964Srwatson		ICMPSTAT_INC(icps_oldicmp);
2501541Srgrimes		goto freeit;
2511541Srgrimes	}
252152582Sandre	/* Drop if IP header plus 8 bytes is not contignous in first mbuf. */
253152582Sandre	if (oiphlen + 8 > n->m_len)
2541541Srgrimes		goto freeit;
2551541Srgrimes	/*
256149349Sandre	 * Calculate length to quote from original packet and
257149349Sandre	 * prevent the ICMP mbuf from overflowing.
258152582Sandre	 * Unfortunatly this is non-trivial since ip_forward()
259152582Sandre	 * sends us truncated packets.
260149349Sandre	 */
261152582Sandre	nlen = m_length(n, NULL);
262149370Sandre	if (oip->ip_p == IPPROTO_TCP) {
263149370Sandre		struct tcphdr *th;
264149370Sandre		int tcphlen;
265149370Sandre
266152582Sandre		if (oiphlen + sizeof(struct tcphdr) > n->m_len &&
267152582Sandre		    n->m_next == NULL)
268152582Sandre			goto stdreply;
269152582Sandre		if (n->m_len < oiphlen + sizeof(struct tcphdr) &&
270152582Sandre		    ((n = m_pullup(n, oiphlen + sizeof(struct tcphdr))) == NULL))
271149370Sandre			goto freeit;
272152582Sandre		th = (struct tcphdr *)((caddr_t)oip + oiphlen);
273149370Sandre		tcphlen = th->th_off << 2;
274149370Sandre		if (tcphlen < sizeof(struct tcphdr))
275149370Sandre			goto freeit;
276241913Sglebius		if (ntohs(oip->ip_len) < oiphlen + tcphlen)
277149370Sandre			goto freeit;
278152582Sandre		if (oiphlen + tcphlen > n->m_len && n->m_next == NULL)
279152582Sandre			goto stdreply;
280152582Sandre		if (n->m_len < oiphlen + tcphlen &&
281152582Sandre		    ((n = m_pullup(n, oiphlen + tcphlen)) == NULL))
282149370Sandre			goto freeit;
283241913Sglebius		icmpelen = max(tcphlen, min(V_icmp_quotelen,
284241913Sglebius		    ntohs(oip->ip_len) - oiphlen));
285149370Sandre	} else
286241913Sglebiusstdreply:	icmpelen = max(8, min(V_icmp_quotelen, ntohs(oip->ip_len) - oiphlen));
287152582Sandre
288152582Sandre	icmplen = min(oiphlen + icmpelen, nlen);
28973996Siedowse	if (icmplen < sizeof(struct ip))
290149378Sandre		goto freeit;
291152582Sandre
292152582Sandre	if (MHLEN > sizeof(struct ip) + ICMP_MINLEN + icmplen)
293243882Sglebius		m = m_gethdr(M_NOWAIT, MT_DATA);
294152582Sandre	else
295243882Sglebius		m = m_getcl(M_NOWAIT, MT_DATA, M_PKTHDR);
296152582Sandre	if (m == NULL)
297152582Sandre		goto freeit;
298152582Sandre#ifdef MAC
299173102Srwatson	mac_netinet_icmp_reply(n, m);
300152582Sandre#endif
301152582Sandre	icmplen = min(icmplen, M_TRAILINGSPACE(m) - sizeof(struct ip) - ICMP_MINLEN);
302341258Semaste	m_align(m, sizeof(struct ip) + ICMP_MINLEN + icmplen);
303341258Semaste	m->m_data += sizeof(struct ip);
304152582Sandre	m->m_len = ICMP_MINLEN + icmplen;
305152582Sandre
306178888Sjulian	/* XXX MRT  make the outgoing packet use the same FIB
307178888Sjulian	 * that was associated with the incoming packet
308178888Sjulian	 */
309178888Sjulian	M_SETFIB(m, M_GETFIB(n));
3101541Srgrimes	icp = mtod(m, struct icmp *);
311190964Srwatson	ICMPSTAT_INC(icps_outhist[type]);
3121541Srgrimes	icp->icmp_type = type;
3131541Srgrimes	if (type == ICMP_REDIRECT)
3141541Srgrimes		icp->icmp_gwaddr.s_addr = dest;
3151541Srgrimes	else {
3161541Srgrimes		icp->icmp_void = 0;
3178876Srgrimes		/*
3181541Srgrimes		 * The following assignments assume an overlay with the
319152582Sandre		 * just zeroed icmp_void field.
3201541Srgrimes		 */
3211541Srgrimes		if (type == ICMP_PARAMPROB) {
3221541Srgrimes			icp->icmp_pptr = code;
3231541Srgrimes			code = 0;
3241541Srgrimes		} else if (type == ICMP_UNREACH &&
325145863Sandre			code == ICMP_UNREACH_NEEDFRAG && mtu) {
326145863Sandre			icp->icmp_nextmtu = htons(mtu);
3271541Srgrimes		}
3281541Srgrimes	}
3291541Srgrimes	icp->icmp_code = code;
3301541Srgrimes
3311541Srgrimes	/*
332152582Sandre	 * Copy the quotation into ICMP message and
333152582Sandre	 * convert quoted IP header back to network representation.
33465327Sru	 */
335152582Sandre	m_copydata(n, 0, icmplen, (caddr_t)&icp->icmp_ip);
336152582Sandre	nip = &icp->icmp_ip;
33765327Sru
33865327Sru	/*
339152582Sandre	 * Set up ICMP message mbuf and copy old IP header (without options
340152582Sandre	 * in front of ICMP message.
341132280Smlaier	 * If the original mbuf was meant to bypass the firewall, the error
342132280Smlaier	 * reply should bypass as well.
343132280Smlaier	 */
344132280Smlaier	m->m_flags |= n->m_flags & M_SKIP_FIREWALL;
345341258Semaste	KASSERT(M_LEADINGSPACE(m) >= sizeof(struct ip),
346341258Semaste	    ("insufficient space for ip header"));
3471541Srgrimes	m->m_data -= sizeof(struct ip);
3481541Srgrimes	m->m_len += sizeof(struct ip);
3491541Srgrimes	m->m_pkthdr.len = m->m_len;
3501541Srgrimes	m->m_pkthdr.rcvif = n->m_pkthdr.rcvif;
3511541Srgrimes	nip = mtod(m, struct ip *);
3521541Srgrimes	bcopy((caddr_t)oip, (caddr_t)nip, sizeof(struct ip));
353241913Sglebius	nip->ip_len = htons(m->m_len);
354105586Sphk	nip->ip_v = IPVERSION;
355105586Sphk	nip->ip_hl = 5;
3561541Srgrimes	nip->ip_p = IPPROTO_ICMP;
3571541Srgrimes	nip->ip_tos = 0;
358264221Sae	nip->ip_off = 0;
359340671Seugen
360340671Seugen	if (V_error_keeptags)
361340671Seugen		m_tag_copy_chain(m, n, M_NOWAIT);
362340671Seugen
3631541Srgrimes	icmp_reflect(m);
3641541Srgrimes
3651541Srgrimesfreeit:
3661541Srgrimes	m_freem(n);
3671541Srgrimes}
3681541Srgrimes
3691541Srgrimes/*
3701541Srgrimes * Process a received ICMP message.
3711541Srgrimes */
3721541Srgrimesvoid
373169454Srwatsonicmp_input(struct mbuf *m, int off)
3741541Srgrimes{
375122593Sandre	struct icmp *icp;
376122593Sandre	struct in_ifaddr *ia;
377122593Sandre	struct ip *ip = mtod(m, struct ip *);
378122593Sandre	struct sockaddr_in icmpsrc, icmpdst, icmpgw;
37955009Sshin	int hlen = off;
380241923Sglebius	int icmplen = ntohs(ip->ip_len) - off;
381122593Sandre	int i, code;
38292723Salfred	void (*ctlfunc)(int, struct sockaddr *, void *);
383178888Sjulian	int fibnum;
3841541Srgrimes
3851541Srgrimes	/*
3861541Srgrimes	 * Locate icmp structure in mbuf, and check
3871541Srgrimes	 * that not corrupted and of at least minimum length.
3881541Srgrimes	 */
3891541Srgrimes#ifdef ICMPPRINTFS
39010421Swollman	if (icmpprintfs) {
39110421Swollman		char buf[4 * sizeof "123"];
39210421Swollman		strcpy(buf, inet_ntoa(ip->ip_src));
39310421Swollman		printf("icmp_input from %s to %s, len %d\n",
39410421Swollman		       buf, inet_ntoa(ip->ip_dst), icmplen);
39510421Swollman	}
3961541Srgrimes#endif
3971541Srgrimes	if (icmplen < ICMP_MINLEN) {
398190964Srwatson		ICMPSTAT_INC(icps_tooshort);
3991541Srgrimes		goto freeit;
4001541Srgrimes	}
4011541Srgrimes	i = hlen + min(icmplen, ICMP_ADVLENMIN);
402198050Sbz	if (m->m_len < i && (m = m_pullup(m, i)) == NULL)  {
403190964Srwatson		ICMPSTAT_INC(icps_tooshort);
4041541Srgrimes		return;
4051541Srgrimes	}
4061541Srgrimes	ip = mtod(m, struct ip *);
4071541Srgrimes	m->m_len -= hlen;
4081541Srgrimes	m->m_data += hlen;
4091541Srgrimes	icp = mtod(m, struct icmp *);
4101541Srgrimes	if (in_cksum(m, icmplen)) {
411190964Srwatson		ICMPSTAT_INC(icps_checksum);
4121541Srgrimes		goto freeit;
4131541Srgrimes	}
4141541Srgrimes	m->m_len += hlen;
4151541Srgrimes	m->m_data -= hlen;
4161541Srgrimes
41755009Sshin	if (m->m_pkthdr.rcvif && m->m_pkthdr.rcvif->if_type == IFT_FAITH) {
41855009Sshin		/*
41955009Sshin		 * Deliver very specific ICMP type only.
42055009Sshin		 */
42155009Sshin		switch (icp->icmp_type) {
42255009Sshin		case ICMP_UNREACH:
42355009Sshin		case ICMP_TIMXCEED:
42455009Sshin			break;
42555009Sshin		default:
42655009Sshin			goto freeit;
42755009Sshin		}
42855009Sshin	}
42955009Sshin
4301541Srgrimes#ifdef ICMPPRINTFS
4311541Srgrimes	if (icmpprintfs)
4321541Srgrimes		printf("icmp_input, type %d code %d\n", icp->icmp_type,
4331541Srgrimes		    icp->icmp_code);
4341541Srgrimes#endif
4359472Swollman
4369472Swollman	/*
4379472Swollman	 * Message type specific processing.
4389472Swollman	 */
4391541Srgrimes	if (icp->icmp_type > ICMP_MAXTYPE)
4401541Srgrimes		goto raw;
441122593Sandre
442122593Sandre	/* Initialize */
443122593Sandre	bzero(&icmpsrc, sizeof(icmpsrc));
444122593Sandre	icmpsrc.sin_len = sizeof(struct sockaddr_in);
445122593Sandre	icmpsrc.sin_family = AF_INET;
446122593Sandre	bzero(&icmpdst, sizeof(icmpdst));
447122593Sandre	icmpdst.sin_len = sizeof(struct sockaddr_in);
448122593Sandre	icmpdst.sin_family = AF_INET;
449122593Sandre	bzero(&icmpgw, sizeof(icmpgw));
450122593Sandre	icmpgw.sin_len = sizeof(struct sockaddr_in);
451122593Sandre	icmpgw.sin_family = AF_INET;
452122593Sandre
453190964Srwatson	ICMPSTAT_INC(icps_inhist[icp->icmp_type]);
4541541Srgrimes	code = icp->icmp_code;
4551541Srgrimes	switch (icp->icmp_type) {
4561541Srgrimes
4571541Srgrimes	case ICMP_UNREACH:
4581541Srgrimes		switch (code) {
4591541Srgrimes			case ICMP_UNREACH_NET:
4601541Srgrimes			case ICMP_UNREACH_HOST:
4611541Srgrimes			case ICMP_UNREACH_SRCFAIL:
46272959Sjlemon			case ICMP_UNREACH_NET_UNKNOWN:
46372959Sjlemon			case ICMP_UNREACH_HOST_UNKNOWN:
46472959Sjlemon			case ICMP_UNREACH_ISOLATED:
46572959Sjlemon			case ICMP_UNREACH_TOSNET:
46672959Sjlemon			case ICMP_UNREACH_TOSHOST:
46772959Sjlemon			case ICMP_UNREACH_HOST_PRECEDENCE:
46872959Sjlemon			case ICMP_UNREACH_PRECEDENCE_CUTOFF:
46972959Sjlemon				code = PRC_UNREACH_NET;
4701541Srgrimes				break;
4711541Srgrimes
4721541Srgrimes			case ICMP_UNREACH_NEEDFRAG:
4731541Srgrimes				code = PRC_MSGSIZE;
4741541Srgrimes				break;
4758876Srgrimes
47672959Sjlemon			/*
47772959Sjlemon			 * RFC 1122, Sections 3.2.2.1 and 4.2.3.9.
47872959Sjlemon			 * Treat subcodes 2,3 as immediate RST
47972959Sjlemon			 */
48072959Sjlemon			case ICMP_UNREACH_PROTOCOL:
48172959Sjlemon			case ICMP_UNREACH_PORT:
48274937Sjesper				code = PRC_UNREACH_PORT;
48372638Sphk				break;
48472638Sphk
4851541Srgrimes			case ICMP_UNREACH_NET_PROHIB:
4861541Srgrimes			case ICMP_UNREACH_HOST_PROHIB:
48718416Spst			case ICMP_UNREACH_FILTER_PROHIB:
48872638Sphk				code = PRC_UNREACH_ADMIN_PROHIB;
48972638Sphk				break;
49070103Sphk
4911541Srgrimes			default:
4921541Srgrimes				goto badcode;
4931541Srgrimes		}
4941541Srgrimes		goto deliver;
4951541Srgrimes
4961541Srgrimes	case ICMP_TIMXCEED:
4971541Srgrimes		if (code > 1)
4981541Srgrimes			goto badcode;
4991541Srgrimes		code += PRC_TIMXCEED_INTRANS;
5001541Srgrimes		goto deliver;
5011541Srgrimes
5021541Srgrimes	case ICMP_PARAMPROB:
5031541Srgrimes		if (code > 1)
5041541Srgrimes			goto badcode;
5051541Srgrimes		code = PRC_PARAMPROB;
5061541Srgrimes		goto deliver;
5071541Srgrimes
5081541Srgrimes	case ICMP_SOURCEQUENCH:
5091541Srgrimes		if (code)
5101541Srgrimes			goto badcode;
5111541Srgrimes		code = PRC_QUENCH;
5121541Srgrimes	deliver:
5131541Srgrimes		/*
5141541Srgrimes		 * Problem with datagram; advise higher level routines.
5151541Srgrimes		 */
5161541Srgrimes		if (icmplen < ICMP_ADVLENMIN || icmplen < ICMP_ADVLEN(icp) ||
517105586Sphk		    icp->icmp_ip.ip_hl < (sizeof(struct ip) >> 2)) {
518190964Srwatson			ICMPSTAT_INC(icps_badlen);
5191541Srgrimes			goto freeit;
5201541Srgrimes		}
5219472Swollman		/* Discard ICMP's in response to multicast packets */
5229472Swollman		if (IN_MULTICAST(ntohl(icp->icmp_ip.ip_dst.s_addr)))
5239472Swollman			goto badcode;
5241541Srgrimes#ifdef ICMPPRINTFS
5251541Srgrimes		if (icmpprintfs)
5261541Srgrimes			printf("deliver to protocol %d\n", icp->icmp_ip.ip_p);
5271541Srgrimes#endif
5281541Srgrimes		icmpsrc.sin_addr = icp->icmp_ip.ip_dst;
52910881Swollman		/*
53055009Sshin		 * XXX if the packet contains [IPv4 AH TCP], we can't make a
53155009Sshin		 * notification to TCP layer.
53255009Sshin		 */
5333311Sphk		ctlfunc = inetsw[ip_protox[icp->icmp_ip.ip_p]].pr_ctlinput;
5343311Sphk		if (ctlfunc)
5351541Srgrimes			(*ctlfunc)(code, (struct sockaddr *)&icmpsrc,
53612881Sbde				   (void *)&icp->icmp_ip);
5371541Srgrimes		break;
5381541Srgrimes
5391541Srgrimes	badcode:
540190964Srwatson		ICMPSTAT_INC(icps_badcode);
5411541Srgrimes		break;
5421541Srgrimes
5431541Srgrimes	case ICMP_ECHO:
544183550Szec		if (!V_icmpbmcastecho
54536393Sdg		    && (m->m_flags & (M_MCAST | M_BCAST)) != 0) {
546190964Srwatson			ICMPSTAT_INC(icps_bmcastecho);
54728683Swollman			break;
54828683Swollman		}
5491541Srgrimes		icp->icmp_type = ICMP_ECHOREPLY;
55072357Sbmilekic		if (badport_bandlim(BANDLIM_ICMP_ECHO) < 0)
55170070Sbmilekic			goto freeit;
55270070Sbmilekic		else
55370070Sbmilekic			goto reflect;
5541541Srgrimes
5551541Srgrimes	case ICMP_TSTAMP:
556281609Smarkj		if (V_icmptstamprepl == 0)
557281609Smarkj			break;
558183550Szec		if (!V_icmpbmcastecho
55936393Sdg		    && (m->m_flags & (M_MCAST | M_BCAST)) != 0) {
560190964Srwatson			ICMPSTAT_INC(icps_bmcasttstamp);
56128723Swollman			break;
56228723Swollman		}
5631541Srgrimes		if (icmplen < ICMP_TSLEN) {
564190964Srwatson			ICMPSTAT_INC(icps_badlen);
5651541Srgrimes			break;
5661541Srgrimes		}
5671541Srgrimes		icp->icmp_type = ICMP_TSTAMPREPLY;
5681541Srgrimes		icp->icmp_rtime = iptime();
5691541Srgrimes		icp->icmp_ttime = icp->icmp_rtime;	/* bogus, do later! */
57072357Sbmilekic		if (badport_bandlim(BANDLIM_ICMP_TSTAMP) < 0)
57170070Sbmilekic			goto freeit;
57270070Sbmilekic		else
57370070Sbmilekic			goto reflect;
5748876Srgrimes
5751541Srgrimes	case ICMP_MASKREQ:
576183550Szec		if (V_icmpmaskrepl == 0)
5771541Srgrimes			break;
5781541Srgrimes		/*
5791541Srgrimes		 * We are not able to respond with all ones broadcast
5801541Srgrimes		 * unless we receive it over a point-to-point interface.
5811541Srgrimes		 */
5821541Srgrimes		if (icmplen < ICMP_MASKLEN)
5831541Srgrimes			break;
5841541Srgrimes		switch (ip->ip_dst.s_addr) {
5851541Srgrimes
5861541Srgrimes		case INADDR_BROADCAST:
5871541Srgrimes		case INADDR_ANY:
5881541Srgrimes			icmpdst.sin_addr = ip->ip_src;
5891541Srgrimes			break;
5901541Srgrimes
5911541Srgrimes		default:
5921541Srgrimes			icmpdst.sin_addr = ip->ip_dst;
5931541Srgrimes		}
5941541Srgrimes		ia = (struct in_ifaddr *)ifaof_ifpforaddr(
5951541Srgrimes			    (struct sockaddr *)&icmpdst, m->m_pkthdr.rcvif);
596194760Srwatson		if (ia == NULL)
5971541Srgrimes			break;
598194760Srwatson		if (ia->ia_ifp == NULL) {
599194760Srwatson			ifa_free(&ia->ia_ifa);
60014998Sphk			break;
601194760Srwatson		}
6021541Srgrimes		icp->icmp_type = ICMP_MASKREPLY;
603183550Szec		if (V_icmpmaskfake == 0)
604112465Smdodd			icp->icmp_mask = ia->ia_sockmask.sin_addr.s_addr;
605112465Smdodd		else
606183550Szec			icp->icmp_mask = V_icmpmaskfake;
6071541Srgrimes		if (ip->ip_src.s_addr == 0) {
6081541Srgrimes			if (ia->ia_ifp->if_flags & IFF_BROADCAST)
6091541Srgrimes			    ip->ip_src = satosin(&ia->ia_broadaddr)->sin_addr;
6101541Srgrimes			else if (ia->ia_ifp->if_flags & IFF_POINTOPOINT)
6111541Srgrimes			    ip->ip_src = satosin(&ia->ia_dstaddr)->sin_addr;
6121541Srgrimes		}
613194760Srwatson		ifa_free(&ia->ia_ifa);
6141541Srgrimesreflect:
615190964Srwatson		ICMPSTAT_INC(icps_reflect);
616190964Srwatson		ICMPSTAT_INC(icps_outhist[icp->icmp_type]);
6171541Srgrimes		icmp_reflect(m);
6181541Srgrimes		return;
6191541Srgrimes
6201541Srgrimes	case ICMP_REDIRECT:
621183550Szec		if (V_log_redirect) {
62249603Sdes			u_long src, dst, gw;
62349603Sdes
62449603Sdes			src = ntohl(ip->ip_src.s_addr);
62549603Sdes			dst = ntohl(icp->icmp_ip.ip_dst.s_addr);
62649603Sdes			gw = ntohl(icp->icmp_gwaddr.s_addr);
62749603Sdes			printf("icmp redirect from %d.%d.%d.%d: "
62849603Sdes			       "%d.%d.%d.%d => %d.%d.%d.%d\n",
62949603Sdes			       (int)(src >> 24), (int)((src >> 16) & 0xff),
63049603Sdes			       (int)((src >> 8) & 0xff), (int)(src & 0xff),
63149603Sdes			       (int)(dst >> 24), (int)((dst >> 16) & 0xff),
63249603Sdes			       (int)((dst >> 8) & 0xff), (int)(dst & 0xff),
63349603Sdes			       (int)(gw >> 24), (int)((gw >> 16) & 0xff),
63449603Sdes			       (int)((gw >> 8) & 0xff), (int)(gw & 0xff));
63549603Sdes		}
636124198Sandre		/*
637124198Sandre		 * RFC1812 says we must ignore ICMP redirects if we
638124198Sandre		 * are acting as router.
639124198Sandre		 */
640183550Szec		if (V_drop_redirect || V_ipforwarding)
64149603Sdes			break;
6421541Srgrimes		if (code > 3)
6431541Srgrimes			goto badcode;
6441541Srgrimes		if (icmplen < ICMP_ADVLENMIN || icmplen < ICMP_ADVLEN(icp) ||
645105586Sphk		    icp->icmp_ip.ip_hl < (sizeof(struct ip) >> 2)) {
646190964Srwatson			ICMPSTAT_INC(icps_badlen);
6471541Srgrimes			break;
6481541Srgrimes		}
6491541Srgrimes		/*
6501541Srgrimes		 * Short circuit routing redirects to force
6511541Srgrimes		 * immediate change in the kernel's routing
6521541Srgrimes		 * tables.  The message is also handed to anyone
6531541Srgrimes		 * listening on a raw socket (e.g. the routing
6541541Srgrimes		 * daemon for use in updating its tables).
6551541Srgrimes		 */
6561541Srgrimes		icmpgw.sin_addr = ip->ip_src;
6571541Srgrimes		icmpdst.sin_addr = icp->icmp_gwaddr;
6581541Srgrimes#ifdef	ICMPPRINTFS
65910421Swollman		if (icmpprintfs) {
66010421Swollman			char buf[4 * sizeof "123"];
66110421Swollman			strcpy(buf, inet_ntoa(icp->icmp_ip.ip_dst));
66210421Swollman
66310421Swollman			printf("redirect dst %s to %s\n",
66410421Swollman			       buf, inet_ntoa(icp->icmp_gwaddr));
66510421Swollman		}
6661541Srgrimes#endif
6671541Srgrimes		icmpsrc.sin_addr = icp->icmp_ip.ip_dst;
668178888Sjulian		for ( fibnum = 0; fibnum < rt_numfibs; fibnum++) {
669178888Sjulian			in_rtredirect((struct sockaddr *)&icmpsrc,
670178888Sjulian			  (struct sockaddr *)&icmpdst,
671178888Sjulian			  (struct sockaddr *)0, RTF_GATEWAY | RTF_HOST,
672178888Sjulian			  (struct sockaddr *)&icmpgw, fibnum);
673178888Sjulian		}
6741541Srgrimes		pfctlinput(PRC_REDIRECT_HOST, (struct sockaddr *)&icmpsrc);
6751541Srgrimes		break;
6761541Srgrimes
6771541Srgrimes	/*
6781541Srgrimes	 * No kernel processing for the following;
6791541Srgrimes	 * just fall through to send to raw listener.
6801541Srgrimes	 */
6811541Srgrimes	case ICMP_ECHOREPLY:
6821541Srgrimes	case ICMP_ROUTERADVERT:
6831541Srgrimes	case ICMP_ROUTERSOLICIT:
6841541Srgrimes	case ICMP_TSTAMPREPLY:
6851541Srgrimes	case ICMP_IREQREPLY:
6861541Srgrimes	case ICMP_MASKREPLY:
6871541Srgrimes	default:
6881541Srgrimes		break;
6891541Srgrimes	}
6901541Srgrimes
6911541Srgrimesraw:
69282884Sjulian	rip_input(m, off);
6931541Srgrimes	return;
6941541Srgrimes
6951541Srgrimesfreeit:
6961541Srgrimes	m_freem(m);
6971541Srgrimes}
6981541Srgrimes
6991541Srgrimes/*
7001541Srgrimes * Reflect the ip packet back to the source
7011541Srgrimes */
70212296Sphkstatic void
703169454Srwatsonicmp_reflect(struct mbuf *m)
7041541Srgrimes{
70584102Sjlemon	struct ip *ip = mtod(m, struct ip *);
70684102Sjlemon	struct ifaddr *ifa;
707191311Srwatson	struct ifnet *ifp;
70884102Sjlemon	struct in_ifaddr *ia;
7091541Srgrimes	struct in_addr t;
7107090Sbde	struct mbuf *opts = 0;
711105586Sphk	int optlen = (ip->ip_hl << 2) - sizeof(struct ip);
7121541Srgrimes
713178280Sgnn	if (IN_MULTICAST(ntohl(ip->ip_src.s_addr)) ||
714178280Sgnn	    IN_EXPERIMENTAL(ntohl(ip->ip_src.s_addr)) ||
715178280Sgnn	    IN_ZERONET(ntohl(ip->ip_src.s_addr)) ) {
7161541Srgrimes		m_freem(m);	/* Bad return address */
717190964Srwatson		ICMPSTAT_INC(icps_badaddr);
7181541Srgrimes		goto done;	/* Ip_output() will check for broadcast */
7191541Srgrimes	}
720178280Sgnn
7211541Srgrimes	t = ip->ip_dst;
7221541Srgrimes	ip->ip_dst = ip->ip_src;
723125349Sandre
7241541Srgrimes	/*
725125349Sandre	 * Source selection for ICMP replies:
726125349Sandre	 *
727125349Sandre	 * If the incoming packet was addressed directly to one of our
728125349Sandre	 * own addresses, use dst as the src for the reply.
7291541Srgrimes	 */
730194951Srwatson	IN_IFADDR_RLOCK();
731191311Srwatson	LIST_FOREACH(ia, INADDR_HASH(t.s_addr), ia_hash) {
732191311Srwatson		if (t.s_addr == IA_SIN(ia)->sin_addr.s_addr) {
733191311Srwatson			t = IA_SIN(ia)->sin_addr;
734194951Srwatson			IN_IFADDR_RUNLOCK();
73584102Sjlemon			goto match;
736191311Srwatson		}
737191311Srwatson	}
738194951Srwatson	IN_IFADDR_RUNLOCK();
739194951Srwatson
740125349Sandre	/*
741125349Sandre	 * If the incoming packet was addressed to one of our broadcast
742125349Sandre	 * addresses, use the first non-broadcast address which corresponds
743125349Sandre	 * to the incoming interface.
744125349Sandre	 */
745191311Srwatson	ifp = m->m_pkthdr.rcvif;
746191311Srwatson	if (ifp != NULL && ifp->if_flags & IFF_BROADCAST) {
747229621Sjhb		IF_ADDR_RLOCK(ifp);
748191311Srwatson		TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
74984102Sjlemon			if (ifa->ifa_addr->sa_family != AF_INET)
75084102Sjlemon				continue;
75187914Sjlemon			ia = ifatoia(ifa);
75287914Sjlemon			if (satosin(&ia->ia_broadaddr)->sin_addr.s_addr ==
753191311Srwatson			    t.s_addr) {
754191311Srwatson				t = IA_SIN(ia)->sin_addr;
755229621Sjhb				IF_ADDR_RUNLOCK(ifp);
75687914Sjlemon				goto match;
757191311Srwatson			}
75887914Sjlemon		}
759229621Sjhb		IF_ADDR_RUNLOCK(ifp);
76087914Sjlemon	}
761125360Sandre	/*
762149347Sandre	 * If the packet was transiting through us, use the address of
763149347Sandre	 * the interface the packet came through in.  If that interface
764149347Sandre	 * doesn't have a suitable IP address, the normal selection
765149347Sandre	 * criteria apply.
766149347Sandre	 */
767191311Srwatson	if (V_icmp_rfi && ifp != NULL) {
768229621Sjhb		IF_ADDR_RLOCK(ifp);
769191311Srwatson		TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
770149347Sandre			if (ifa->ifa_addr->sa_family != AF_INET)
771149347Sandre				continue;
772149347Sandre			ia = ifatoia(ifa);
773191311Srwatson			t = IA_SIN(ia)->sin_addr;
774229621Sjhb			IF_ADDR_RUNLOCK(ifp);
775149347Sandre			goto match;
776149347Sandre		}
777229621Sjhb		IF_ADDR_RUNLOCK(ifp);
778149347Sandre	}
779149347Sandre	/*
780125360Sandre	 * If the incoming packet was not addressed directly to us, use
781125360Sandre	 * designated interface for icmp replies specified by sysctl
782125360Sandre	 * net.inet.icmp.reply_src (default not set). Otherwise continue
783125360Sandre	 * with normal source selection.
784125360Sandre	 */
785191311Srwatson	if (V_reply_src[0] != '\0' && (ifp = ifunit(V_reply_src))) {
786229621Sjhb		IF_ADDR_RLOCK(ifp);
787191311Srwatson		TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
788125360Sandre			if (ifa->ifa_addr->sa_family != AF_INET)
789125360Sandre				continue;
790125360Sandre			ia = ifatoia(ifa);
791191311Srwatson			t = IA_SIN(ia)->sin_addr;
792229621Sjhb			IF_ADDR_RUNLOCK(ifp);
793125360Sandre			goto match;
794125360Sandre		}
795229621Sjhb		IF_ADDR_RUNLOCK(ifp);
796125360Sandre	}
797133874Srwatson	/*
798125349Sandre	 * If the packet was transiting through us, use the address of
799125349Sandre	 * the interface that is the closest to the packet source.
800125349Sandre	 * When we don't have a route back to the packet source, stop here
801125349Sandre	 * and drop the packet.
802125349Sandre	 */
803178888Sjulian	ia = ip_rtaddr(ip->ip_dst, M_GETFIB(m));
80486999Sdd	if (ia == NULL) {
80586999Sdd		m_freem(m);
806190964Srwatson		ICMPSTAT_INC(icps_noroute);
80786999Sdd		goto done;
80886999Sdd	}
809191311Srwatson	t = IA_SIN(ia)->sin_addr;
810194760Srwatson	ifa_free(&ia->ia_ifa);
81184102Sjlemonmatch:
812119245Srwatson#ifdef MAC
813173102Srwatson	mac_netinet_icmp_replyinplace(m);
814119245Srwatson#endif
8151541Srgrimes	ip->ip_src = t;
816181803Sbz	ip->ip_ttl = V_ip_defttl;
8171541Srgrimes
8181541Srgrimes	if (optlen > 0) {
8191541Srgrimes		register u_char *cp;
8201541Srgrimes		int opt, cnt;
8211541Srgrimes		u_int len;
8221541Srgrimes
8231541Srgrimes		/*
8241541Srgrimes		 * Retrieve any source routing from the incoming packet;
8251541Srgrimes		 * add on any record-route or timestamp options.
8261541Srgrimes		 */
8271541Srgrimes		cp = (u_char *) (ip + 1);
828135274Sandre		if ((opts = ip_srcroute(m)) == 0 &&
829243882Sglebius		    (opts = m_gethdr(M_NOWAIT, MT_DATA))) {
8301541Srgrimes			opts->m_len = sizeof(struct in_addr);
8311541Srgrimes			mtod(opts, struct in_addr *)->s_addr = 0;
8321541Srgrimes		}
8331541Srgrimes		if (opts) {
8341541Srgrimes#ifdef ICMPPRINTFS
8351541Srgrimes		    if (icmpprintfs)
8361541Srgrimes			    printf("icmp_reflect optlen %d rt %d => ",
8371541Srgrimes				optlen, opts->m_len);
8381541Srgrimes#endif
8391541Srgrimes		    for (cnt = optlen; cnt > 0; cnt -= len, cp += len) {
8401541Srgrimes			    opt = cp[IPOPT_OPTVAL];
8411541Srgrimes			    if (opt == IPOPT_EOL)
8421541Srgrimes				    break;
8431541Srgrimes			    if (opt == IPOPT_NOP)
8441541Srgrimes				    len = 1;
8451541Srgrimes			    else {
84661183Sjlemon				    if (cnt < IPOPT_OLEN + sizeof(*cp))
84761183Sjlemon					    break;
8481541Srgrimes				    len = cp[IPOPT_OLEN];
84961183Sjlemon				    if (len < IPOPT_OLEN + sizeof(*cp) ||
85061183Sjlemon				        len > cnt)
8511541Srgrimes					    break;
8521541Srgrimes			    }
8531541Srgrimes			    /*
8541541Srgrimes			     * Should check for overflow, but it "can't happen"
8551541Srgrimes			     */
8568876Srgrimes			    if (opt == IPOPT_RR || opt == IPOPT_TS ||
8571541Srgrimes				opt == IPOPT_SECURITY) {
8581541Srgrimes				    bcopy((caddr_t)cp,
8591541Srgrimes					mtod(opts, caddr_t) + opts->m_len, len);
8601541Srgrimes				    opts->m_len += len;
8611541Srgrimes			    }
8621541Srgrimes		    }
8631541Srgrimes		    /* Terminate & pad, if necessary */
8643311Sphk		    cnt = opts->m_len % 4;
8653311Sphk		    if (cnt) {
8661541Srgrimes			    for (; cnt < 4; cnt++) {
8671541Srgrimes				    *(mtod(opts, caddr_t) + opts->m_len) =
8681541Srgrimes					IPOPT_EOL;
8691541Srgrimes				    opts->m_len++;
8701541Srgrimes			    }
8711541Srgrimes		    }
8721541Srgrimes#ifdef ICMPPRINTFS
8731541Srgrimes		    if (icmpprintfs)
8741541Srgrimes			    printf("%d\n", opts->m_len);
8751541Srgrimes#endif
8761541Srgrimes		}
877241926Sglebius		ip_stripoptions(m);
8781541Srgrimes	}
879121645Ssam	m_tag_delete_nonpersistent(m);
8801541Srgrimes	m->m_flags &= ~(M_BCAST|M_MCAST);
881122708Sandre	icmp_send(m, opts);
8821541Srgrimesdone:
8831541Srgrimes	if (opts)
8841541Srgrimes		(void)m_free(opts);
8851541Srgrimes}
8861541Srgrimes
8871541Srgrimes/*
8881541Srgrimes * Send an icmp packet back to the ip level,
8891541Srgrimes * after supplying a checksum.
8901541Srgrimes */
89112296Sphkstatic void
892169454Srwatsonicmp_send(struct mbuf *m, struct mbuf *opts)
8931541Srgrimes{
8941541Srgrimes	register struct ip *ip = mtod(m, struct ip *);
8951541Srgrimes	register int hlen;
8961541Srgrimes	register struct icmp *icp;
8971541Srgrimes
898105586Sphk	hlen = ip->ip_hl << 2;
8991541Srgrimes	m->m_data += hlen;
9001541Srgrimes	m->m_len -= hlen;
9011541Srgrimes	icp = mtod(m, struct icmp *);
9021541Srgrimes	icp->icmp_cksum = 0;
903241913Sglebius	icp->icmp_cksum = in_cksum(m, ntohs(ip->ip_len) - hlen);
9041541Srgrimes	m->m_data -= hlen;
9051541Srgrimes	m->m_len += hlen;
90644528Sarchie	m->m_pkthdr.rcvif = (struct ifnet *)0;
9071541Srgrimes#ifdef ICMPPRINTFS
90810421Swollman	if (icmpprintfs) {
90910421Swollman		char buf[4 * sizeof "123"];
91010421Swollman		strcpy(buf, inet_ntoa(ip->ip_dst));
91110421Swollman		printf("icmp_send dst %s src %s\n",
91210421Swollman		       buf, inet_ntoa(ip->ip_src));
91310421Swollman	}
9141541Srgrimes#endif
915122708Sandre	(void) ip_output(m, opts, NULL, 0, NULL, NULL);
9161541Srgrimes}
9171541Srgrimes
918188578Sluigi/*
919188578Sluigi * Return milliseconds since 00:00 GMT in network format.
920188578Sluigi */
921188578Sluigiuint32_t
922169454Srwatsoniptime(void)
9231541Srgrimes{
9241541Srgrimes	struct timeval atv;
9251541Srgrimes	u_long t;
9261541Srgrimes
92770105Sbillf	getmicrotime(&atv);
9281541Srgrimes	t = (atv.tv_sec % (24*60*60)) * 1000 + atv.tv_usec / 1000;
9291541Srgrimes	return (htonl(t));
9301541Srgrimes}
9311541Srgrimes
93210881Swollman/*
93310881Swollman * Return the next larger or smaller MTU plateau (table from RFC 1191)
93410881Swollman * given current value MTU.  If DIR is less than zero, a larger plateau
93510881Swollman * is returned; otherwise, a smaller value is returned.
93610881Swollman */
937145360Sandreint
938169454Srwatsonip_next_mtu(int mtu, int dir)
93910881Swollman{
94010881Swollman	static int mtutab[] = {
941145866Sandre		65535, 32000, 17914, 8166, 4352, 2002, 1492, 1280, 1006, 508,
942145866Sandre		296, 68, 0
94310881Swollman	};
944154728Sandre	int i, size;
94510881Swollman
946154728Sandre	size = (sizeof mtutab) / (sizeof mtutab[0]);
947154728Sandre	if (dir >= 0) {
948154733Sglebius		for (i = 0; i < size; i++)
949154728Sandre			if (mtu > mtutab[i])
950154728Sandre				return mtutab[i];
95110881Swollman	} else {
952154728Sandre		for (i = size - 1; i >= 0; i--)
953154728Sandre			if (mtu < mtutab[i])
954154728Sandre				return mtutab[i];
955154728Sandre		if (mtu == mtutab[0])
956154728Sandre			return mtutab[0];
95710881Swollman	}
958154728Sandre	return 0;
95910881Swollman}
960221134Sbz#endif /* INET */
96141487Sdillon
96241487Sdillon
96341487Sdillon/*
96441487Sdillon * badport_bandlim() - check for ICMP bandwidth limit
96541487Sdillon *
96641487Sdillon *	Return 0 if it is ok to send an ICMP error response, -1 if we have
967133874Srwatson *	hit our bandwidth limit and it is not ok.
96841487Sdillon *
96941487Sdillon *	If icmplim is <= 0, the feature is disabled and 0 is returned.
97041487Sdillon *
97141487Sdillon *	For now we separate the TCP and UDP subsystems w/ different 'which'
97241487Sdillon *	values.  We may eventually remove this separation (and simplify the
97341487Sdillon *	code further).
97441487Sdillon *
97541487Sdillon *	Note that the printing of the error message is delayed so we can
97641487Sdillon *	properly print the icmp error rate that the system was trying to do
97741487Sdillon *	(i.e. 22000/100 pps, etc...).  This can cause long delays in printing
978133874Srwatson *	the 'final' error, but it doesn't make sense to solve the printing
97941487Sdillon *	delay with more complex code.
98041487Sdillon */
98141487Sdillon
98241487Sdillonint
98341487Sdillonbadport_bandlim(int which)
98441487Sdillon{
985183550Szec
986108144Ssam#define	N(a)	(sizeof (a) / sizeof (a[0]))
987108144Ssam	static struct rate {
988108144Ssam		const char	*type;
989108144Ssam		struct timeval	lasttime;
990132107Sstefanf		int		curpps;
991108144Ssam	} rates[BANDLIM_MAX+1] = {
992108144Ssam		{ "icmp unreach response" },
993108144Ssam		{ "icmp ping response" },
994108144Ssam		{ "icmp tstamp response" },
995108144Ssam		{ "closed port RST response" },
996171508Srwatson		{ "open port RST response" },
997237230Stuexen		{ "icmp6 unreach response" },
998237230Stuexen		{ "sctp ootb response" }
999108144Ssam	};
100041487Sdillon
100141487Sdillon	/*
1002108144Ssam	 * Return ok status if feature disabled or argument out of range.
100341487Sdillon	 */
1004183550Szec	if (V_icmplim > 0 && (u_int) which < N(rates)) {
1005108144Ssam		struct rate *r = &rates[which];
1006108144Ssam		int opps = r->curpps;
100741487Sdillon
1008183550Szec		if (!ppsratecheck(&r->lasttime, &r->curpps, V_icmplim))
1009108144Ssam			return -1;	/* discard packet */
1010108144Ssam		/*
1011108144Ssam		 * If we've dropped below the threshold after having
1012108144Ssam		 * rate-limited traffic print the message.  This preserves
1013108144Ssam		 * the previous behaviour at the expense of added complexity.
1014108144Ssam		 */
1015183550Szec		if (V_icmplim_output && opps > V_icmplim)
1016211316Sandre			log(LOG_NOTICE, "Limiting %s from %d to %d packets/sec\n",
1017183550Szec				r->type, opps, V_icmplim);
101841487Sdillon	}
1019108144Ssam	return 0;			/* okay to send packet */
1020108144Ssam#undef N
102141487Sdillon}
1022