1 /*-
2 * SPDX-License-Identifier: BSD-3-Clause
3 *
4 * Copyright (c) 1982, 1986, 1988, 1993
5 * The Regents of the University of California. All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. Neither the name of the University nor the names of its contributors
16 * may be used to endorse or promote products derived from this software
17 * without specific prior written permission.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 * SUCH DAMAGE.
30 *
31 * @(#)ip_icmp.c 8.2 (Berkeley) 1/4/94
32 */
33
34 #include <sys/cdefs.h>
35 #include "opt_inet.h"
36
37 #include <sys/param.h>
38 #include <sys/systm.h>
39 #include <sys/mbuf.h>
40 #include <sys/protosw.h>
41 #include <sys/socket.h>
42 #include <sys/time.h>
43 #include <sys/kernel.h>
44 #include <sys/lock.h>
45 #include <sys/rmlock.h>
46 #include <sys/sysctl.h>
47 #include <sys/syslog.h>
48
49 #include <net/if.h>
50 #include <net/if_var.h>
51 #include <net/if_types.h>
52 #include <net/route.h>
53 #include <net/route/route_ctl.h>
54 #include <net/route/nhop.h>
55 #include <net/vnet.h>
56
57 #include <netinet/in.h>
58 #include <netinet/in_fib.h>
59 #include <netinet/in_pcb.h>
60 #include <netinet/in_systm.h>
61 #include <netinet/in_var.h>
62 #include <netinet/ip.h>
63 #include <netinet/ip_icmp.h>
64 #include <netinet/ip_var.h>
65 #include <netinet/ip_options.h>
66 #include <netinet/sctp.h>
67 #include <netinet/tcp.h>
68 #include <netinet/tcp_var.h>
69 #include <netinet/tcpip.h>
70 #include <netinet/icmp_var.h>
71
72 #ifdef INET
73
74 #include <machine/in_cksum.h>
75
76 #include <security/mac/mac_framework.h>
77 #endif /* INET */
78
79 /*
80 * ICMP routines: error generation, receive packet processing, and
81 * routines to turnaround packets back to the originator, and
82 * host table maintenance routines.
83 */
84 static int sysctl_icmplim_and_jitter(SYSCTL_HANDLER_ARGS);
85 VNET_DEFINE_STATIC(u_int, icmplim) = 200;
86 #define V_icmplim VNET(icmplim)
87 SYSCTL_PROC(_net_inet_icmp, ICMPCTL_ICMPLIM, icmplim, CTLTYPE_UINT |
88 CTLFLAG_VNET | CTLFLAG_RW, &VNET_NAME(icmplim), 0,
89 &sysctl_icmplim_and_jitter, "IU",
90 "Maximum number of ICMP responses per second");
91
92 VNET_DEFINE_STATIC(int, icmplim_curr_jitter[BANDLIM_MAX]) = {0};
93 #define V_icmplim_curr_jitter VNET(icmplim_curr_jitter)
94 VNET_DEFINE_STATIC(u_int, icmplim_jitter) = 16;
95 #define V_icmplim_jitter VNET(icmplim_jitter)
96 SYSCTL_PROC(_net_inet_icmp, OID_AUTO, icmplim_jitter, CTLTYPE_UINT |
97 CTLFLAG_VNET | CTLFLAG_RW, &VNET_NAME(icmplim_jitter), 0,
98 &sysctl_icmplim_and_jitter, "IU",
99 "Random icmplim jitter adjustment limit");
100
101 VNET_DEFINE_STATIC(int, icmplim_output) = 1;
102 #define V_icmplim_output VNET(icmplim_output)
103 SYSCTL_INT(_net_inet_icmp, OID_AUTO, icmplim_output, CTLFLAG_VNET | CTLFLAG_RW,
104 &VNET_NAME(icmplim_output), 0,
105 "Enable logging of ICMP response rate limiting");
106
107 #ifdef INET
108 VNET_PCPUSTAT_DEFINE(struct icmpstat, icmpstat);
109 VNET_PCPUSTAT_SYSINIT(icmpstat);
110 SYSCTL_VNET_PCPUSTAT(_net_inet_icmp, ICMPCTL_STATS, stats, struct icmpstat,
111 icmpstat, "ICMP statistics (struct icmpstat, netinet/icmp_var.h)");
112
113 #ifdef VIMAGE
114 VNET_PCPUSTAT_SYSUNINIT(icmpstat);
115 #endif /* VIMAGE */
116
117 VNET_DEFINE_STATIC(int, icmpmaskrepl) = 0;
118 #define V_icmpmaskrepl VNET(icmpmaskrepl)
119 SYSCTL_INT(_net_inet_icmp, ICMPCTL_MASKREPL, maskrepl, CTLFLAG_VNET | CTLFLAG_RW,
120 &VNET_NAME(icmpmaskrepl), 0,
121 "Reply to ICMP Address Mask Request packets");
122
123 VNET_DEFINE_STATIC(u_int, icmpmaskfake) = 0;
124 #define V_icmpmaskfake VNET(icmpmaskfake)
125 SYSCTL_UINT(_net_inet_icmp, OID_AUTO, maskfake, CTLFLAG_VNET | CTLFLAG_RW,
126 &VNET_NAME(icmpmaskfake), 0,
127 "Fake reply to ICMP Address Mask Request packets");
128
129 VNET_DEFINE(int, drop_redirect) = 0;
130 #define V_drop_redirect VNET(drop_redirect)
131 SYSCTL_INT(_net_inet_icmp, OID_AUTO, drop_redirect, CTLFLAG_VNET | CTLFLAG_RW,
132 &VNET_NAME(drop_redirect), 0,
133 "Ignore ICMP redirects");
134
135 VNET_DEFINE_STATIC(int, log_redirect) = 0;
136 #define V_log_redirect VNET(log_redirect)
137 SYSCTL_INT(_net_inet_icmp, OID_AUTO, log_redirect, CTLFLAG_VNET | CTLFLAG_RW,
138 &VNET_NAME(log_redirect), 0,
139 "Log ICMP redirects to the console");
140
141 VNET_DEFINE_STATIC(int, redirtimeout) = 60 * 10; /* 10 minutes */
142 #define V_redirtimeout VNET(redirtimeout)
143 SYSCTL_INT(_net_inet_icmp, OID_AUTO, redirtimeout, CTLFLAG_VNET | CTLFLAG_RW,
144 &VNET_NAME(redirtimeout), 0,
145 "Delay in seconds before expiring redirect route");
146
147 VNET_DEFINE_STATIC(char, reply_src[IFNAMSIZ]);
148 #define V_reply_src VNET(reply_src)
149 SYSCTL_STRING(_net_inet_icmp, OID_AUTO, reply_src, CTLFLAG_VNET | CTLFLAG_RW,
150 &VNET_NAME(reply_src), IFNAMSIZ,
151 "ICMP reply source for non-local packets");
152
153 VNET_DEFINE_STATIC(int, icmp_rfi) = 0;
154 #define V_icmp_rfi VNET(icmp_rfi)
155 SYSCTL_INT(_net_inet_icmp, OID_AUTO, reply_from_interface, CTLFLAG_VNET | CTLFLAG_RW,
156 &VNET_NAME(icmp_rfi), 0,
157 "ICMP reply from incoming interface for non-local packets");
158 /* Router requirements RFC 1812 section 4.3.2.3 requires 576 - 28. */
159 VNET_DEFINE_STATIC(int, icmp_quotelen) = 548;
160 #define V_icmp_quotelen VNET(icmp_quotelen)
161 SYSCTL_INT(_net_inet_icmp, OID_AUTO, quotelen, CTLFLAG_VNET | CTLFLAG_RW,
162 &VNET_NAME(icmp_quotelen), 0,
163 "Number of bytes from original packet to quote in ICMP reply");
164
165 VNET_DEFINE_STATIC(int, icmpbmcastecho) = 0;
166 #define V_icmpbmcastecho VNET(icmpbmcastecho)
167 SYSCTL_INT(_net_inet_icmp, OID_AUTO, bmcastecho, CTLFLAG_VNET | CTLFLAG_RW,
168 &VNET_NAME(icmpbmcastecho), 0,
169 "Reply to multicast ICMP Echo Request and Timestamp packets");
170
171 VNET_DEFINE_STATIC(int, icmptstamprepl) = 1;
172 #define V_icmptstamprepl VNET(icmptstamprepl)
173 SYSCTL_INT(_net_inet_icmp, OID_AUTO, tstamprepl, CTLFLAG_VNET | CTLFLAG_RW,
174 &VNET_NAME(icmptstamprepl), 0,
175 "Respond to ICMP Timestamp packets");
176
177 VNET_DEFINE_STATIC(int, error_keeptags) = 0;
178 #define V_error_keeptags VNET(error_keeptags)
179 SYSCTL_INT(_net_inet_icmp, OID_AUTO, error_keeptags, CTLFLAG_VNET | CTLFLAG_RW,
180 &VNET_NAME(error_keeptags), 0,
181 "ICMP error response keeps copy of mbuf_tags of original packet");
182
183 #ifdef ICMPPRINTFS
184 int icmpprintfs = 0;
185 #endif
186
187 static void icmp_reflect(struct mbuf *);
188 static void icmp_send(struct mbuf *, struct mbuf *);
189 static int icmp_verify_redirect_gateway(struct sockaddr_in *,
190 struct sockaddr_in *, struct sockaddr_in *, u_int);
191
192 extern struct protosw inetsw[];
193
194 /*
195 * Kernel module interface for updating icmpstat. The argument is an index
196 * into icmpstat treated as an array of u_long. While this encodes the
197 * general layout of icmpstat into the caller, it doesn't encode its
198 * location, so that future changes to add, for example, per-CPU stats
199 * support won't cause binary compatibility problems for kernel modules.
200 */
201 void
kmod_icmpstat_inc(int statnum)202 kmod_icmpstat_inc(int statnum)
203 {
204
205 counter_u64_add(VNET(icmpstat)[statnum], 1);
206 }
207
208 /*
209 * Generate an error packet of type error
210 * in response to bad packet ip.
211 */
212 void
icmp_error(struct mbuf * n,int type,int code,uint32_t dest,int mtu)213 icmp_error(struct mbuf *n, int type, int code, uint32_t dest, int mtu)
214 {
215 struct ip *oip, *nip;
216 struct icmp *icp;
217 struct mbuf *m;
218 unsigned icmplen, icmpelen, nlen, oiphlen;
219
220 KASSERT((u_int)type <= ICMP_MAXTYPE, ("%s: illegal ICMP type",
221 __func__));
222
223 if (type != ICMP_REDIRECT)
224 ICMPSTAT_INC(icps_error);
225 /*
226 * Don't send error:
227 * if the original packet was encrypted.
228 * if not the first fragment of message.
229 * in response to a multicast or broadcast packet.
230 * if the old packet protocol was an ICMP error message.
231 */
232 if (n->m_flags & M_DECRYPTED)
233 goto freeit;
234 if (n->m_flags & (M_BCAST|M_MCAST))
235 goto freeit;
236
237 /* Drop if IP header plus 8 bytes is not contiguous in first mbuf. */
238 if (n->m_len < sizeof(struct ip) + ICMP_MINLEN)
239 goto freeit;
240 oip = mtod(n, struct ip *);
241 oiphlen = oip->ip_hl << 2;
242 if (n->m_len < oiphlen + ICMP_MINLEN)
243 goto freeit;
244 #ifdef ICMPPRINTFS
245 if (icmpprintfs)
246 printf("icmp_error(%p, %x, %d)\n", oip, type, code);
247 #endif
248 if (oip->ip_off & htons(~(IP_MF|IP_DF)))
249 goto freeit;
250 if (oip->ip_p == IPPROTO_ICMP && type != ICMP_REDIRECT &&
251 !ICMP_INFOTYPE(((struct icmp *)((caddr_t)oip +
252 oiphlen))->icmp_type)) {
253 ICMPSTAT_INC(icps_oldicmp);
254 goto freeit;
255 }
256 /*
257 * Calculate length to quote from original packet and
258 * prevent the ICMP mbuf from overflowing.
259 * Unfortunately this is non-trivial since ip_forward()
260 * sends us truncated packets.
261 */
262 nlen = m_length(n, NULL);
263 if (oip->ip_p == IPPROTO_TCP) {
264 struct tcphdr *th;
265 int tcphlen;
266
267 if (oiphlen + sizeof(struct tcphdr) > n->m_len &&
268 n->m_next == NULL)
269 goto stdreply;
270 if (n->m_len < oiphlen + sizeof(struct tcphdr) &&
271 (n = m_pullup(n, oiphlen + sizeof(struct tcphdr))) == NULL)
272 goto freeit;
273 oip = mtod(n, struct ip *);
274 th = mtodo(n, oiphlen);
275 tcphlen = th->th_off << 2;
276 if (tcphlen < sizeof(struct tcphdr))
277 goto freeit;
278 if (ntohs(oip->ip_len) < oiphlen + tcphlen)
279 goto freeit;
280 if (oiphlen + tcphlen > n->m_len && n->m_next == NULL)
281 goto stdreply;
282 if (n->m_len < oiphlen + tcphlen &&
283 (n = m_pullup(n, oiphlen + tcphlen)) == NULL)
284 goto freeit;
285 oip = mtod(n, struct ip *);
286 icmpelen = max(tcphlen, min(V_icmp_quotelen,
287 ntohs(oip->ip_len) - oiphlen));
288 } else if (oip->ip_p == IPPROTO_SCTP) {
289 struct sctphdr *sh;
290 struct sctp_chunkhdr *ch;
291
292 if (ntohs(oip->ip_len) < oiphlen + sizeof(struct sctphdr))
293 goto stdreply;
294 if (oiphlen + sizeof(struct sctphdr) > n->m_len &&
295 n->m_next == NULL)
296 goto stdreply;
297 if (n->m_len < oiphlen + sizeof(struct sctphdr) &&
298 (n = m_pullup(n, oiphlen + sizeof(struct sctphdr))) == NULL)
299 goto freeit;
300 oip = mtod(n, struct ip *);
301 icmpelen = max(sizeof(struct sctphdr),
302 min(V_icmp_quotelen, ntohs(oip->ip_len) - oiphlen));
303 sh = mtodo(n, oiphlen);
304 if (ntohl(sh->v_tag) == 0 &&
305 ntohs(oip->ip_len) >= oiphlen +
306 sizeof(struct sctphdr) + 8 &&
307 (n->m_len >= oiphlen + sizeof(struct sctphdr) + 8 ||
308 n->m_next != NULL)) {
309 if (n->m_len < oiphlen + sizeof(struct sctphdr) + 8 &&
310 (n = m_pullup(n, oiphlen +
311 sizeof(struct sctphdr) + 8)) == NULL)
312 goto freeit;
313 oip = mtod(n, struct ip *);
314 sh = mtodo(n, oiphlen);
315 ch = (struct sctp_chunkhdr *)(sh + 1);
316 if (ch->chunk_type == SCTP_INITIATION) {
317 icmpelen = max(sizeof(struct sctphdr) + 8,
318 min(V_icmp_quotelen, ntohs(oip->ip_len) -
319 oiphlen));
320 }
321 }
322 } else
323 stdreply: icmpelen = max(8, min(V_icmp_quotelen, ntohs(oip->ip_len) -
324 oiphlen));
325
326 icmplen = min(oiphlen + icmpelen, nlen);
327 if (icmplen < sizeof(struct ip))
328 goto freeit;
329
330 if (MHLEN > sizeof(struct ip) + ICMP_MINLEN + icmplen)
331 m = m_gethdr(M_NOWAIT, MT_DATA);
332 else
333 m = m_getcl(M_NOWAIT, MT_DATA, M_PKTHDR);
334 if (m == NULL)
335 goto freeit;
336 #ifdef MAC
337 mac_netinet_icmp_reply(n, m);
338 #endif
339 icmplen = min(icmplen, M_TRAILINGSPACE(m) -
340 sizeof(struct ip) - ICMP_MINLEN);
341 m_align(m, sizeof(struct ip) + ICMP_MINLEN + icmplen);
342 m->m_data += sizeof(struct ip);
343 m->m_len = ICMP_MINLEN + icmplen;
344
345 /* XXX MRT make the outgoing packet use the same FIB
346 * that was associated with the incoming packet
347 */
348 M_SETFIB(m, M_GETFIB(n));
349 icp = mtod(m, struct icmp *);
350 ICMPSTAT_INC(icps_outhist[type]);
351 icp->icmp_type = type;
352 if (type == ICMP_REDIRECT)
353 icp->icmp_gwaddr.s_addr = dest;
354 else {
355 icp->icmp_void = 0;
356 /*
357 * The following assignments assume an overlay with the
358 * just zeroed icmp_void field.
359 */
360 if (type == ICMP_PARAMPROB) {
361 icp->icmp_pptr = code;
362 code = 0;
363 } else if (type == ICMP_UNREACH &&
364 code == ICMP_UNREACH_NEEDFRAG && mtu) {
365 icp->icmp_nextmtu = htons(mtu);
366 }
367 }
368 icp->icmp_code = code;
369
370 /*
371 * Copy the quotation into ICMP message and
372 * convert quoted IP header back to network representation.
373 */
374 m_copydata(n, 0, icmplen, (caddr_t)&icp->icmp_ip);
375 nip = &icp->icmp_ip;
376
377 /*
378 * Set up ICMP message mbuf and copy old IP header (without options
379 * in front of ICMP message.
380 * If the original mbuf was meant to bypass the firewall, the error
381 * reply should bypass as well.
382 */
383 m->m_flags |= n->m_flags & M_SKIP_FIREWALL;
384 KASSERT(M_LEADINGSPACE(m) >= sizeof(struct ip),
385 ("insufficient space for ip header"));
386 m->m_data -= sizeof(struct ip);
387 m->m_len += sizeof(struct ip);
388 m->m_pkthdr.len = m->m_len;
389 m->m_pkthdr.rcvif = n->m_pkthdr.rcvif;
390 nip = mtod(m, struct ip *);
391 bcopy((caddr_t)oip, (caddr_t)nip, sizeof(struct ip));
392 nip->ip_len = htons(m->m_len);
393 nip->ip_v = IPVERSION;
394 nip->ip_hl = 5;
395 nip->ip_p = IPPROTO_ICMP;
396 nip->ip_tos = 0;
397 nip->ip_off = 0;
398
399 if (V_error_keeptags)
400 m_tag_copy_chain(m, n, M_NOWAIT);
401
402 icmp_reflect(m);
403
404 freeit:
405 m_freem(n);
406 }
407
408 /*
409 * Process a received ICMP message.
410 */
411 int
icmp_input(struct mbuf ** mp,int * offp,int proto)412 icmp_input(struct mbuf **mp, int *offp, int proto)
413 {
414 struct icmp *icp;
415 struct in_ifaddr *ia;
416 struct mbuf *m = *mp;
417 struct ip *ip = mtod(m, struct ip *);
418 struct sockaddr_in icmpsrc, icmpdst, icmpgw;
419 int hlen = *offp;
420 int icmplen = ntohs(ip->ip_len) - *offp;
421 int i, code;
422 void (*ctlfunc)(int, struct sockaddr *, void *);
423 int fibnum;
424
425 NET_EPOCH_ASSERT();
426
427 *mp = NULL;
428
429 /*
430 * Locate icmp structure in mbuf, and check
431 * that not corrupted and of at least minimum length.
432 */
433 #ifdef ICMPPRINTFS
434 if (icmpprintfs) {
435 char srcbuf[INET_ADDRSTRLEN];
436 char dstbuf[INET_ADDRSTRLEN];
437
438 printf("icmp_input from %s to %s, len %d\n",
439 inet_ntoa_r(ip->ip_src, srcbuf),
440 inet_ntoa_r(ip->ip_dst, dstbuf), icmplen);
441 }
442 #endif
443 if (icmplen < ICMP_MINLEN) {
444 ICMPSTAT_INC(icps_tooshort);
445 goto freeit;
446 }
447 i = hlen + min(icmplen, ICMP_ADVLENMIN);
448 if (m->m_len < i && (m = m_pullup(m, i)) == NULL) {
449 ICMPSTAT_INC(icps_tooshort);
450 return (IPPROTO_DONE);
451 }
452 ip = mtod(m, struct ip *);
453 m->m_len -= hlen;
454 m->m_data += hlen;
455 icp = mtod(m, struct icmp *);
456 if (in_cksum(m, icmplen)) {
457 ICMPSTAT_INC(icps_checksum);
458 goto freeit;
459 }
460 m->m_len += hlen;
461 m->m_data -= hlen;
462
463 #ifdef ICMPPRINTFS
464 if (icmpprintfs)
465 printf("icmp_input, type %d code %d\n", icp->icmp_type,
466 icp->icmp_code);
467 #endif
468
469 /*
470 * Message type specific processing.
471 */
472 if (icp->icmp_type > ICMP_MAXTYPE)
473 goto raw;
474
475 /* Initialize */
476 bzero(&icmpsrc, sizeof(icmpsrc));
477 icmpsrc.sin_len = sizeof(struct sockaddr_in);
478 icmpsrc.sin_family = AF_INET;
479 bzero(&icmpdst, sizeof(icmpdst));
480 icmpdst.sin_len = sizeof(struct sockaddr_in);
481 icmpdst.sin_family = AF_INET;
482 bzero(&icmpgw, sizeof(icmpgw));
483 icmpgw.sin_len = sizeof(struct sockaddr_in);
484 icmpgw.sin_family = AF_INET;
485
486 ICMPSTAT_INC(icps_inhist[icp->icmp_type]);
487 code = icp->icmp_code;
488 switch (icp->icmp_type) {
489 case ICMP_UNREACH:
490 switch (code) {
491 case ICMP_UNREACH_NET:
492 case ICMP_UNREACH_HOST:
493 case ICMP_UNREACH_SRCFAIL:
494 case ICMP_UNREACH_NET_UNKNOWN:
495 case ICMP_UNREACH_HOST_UNKNOWN:
496 case ICMP_UNREACH_ISOLATED:
497 case ICMP_UNREACH_TOSNET:
498 case ICMP_UNREACH_TOSHOST:
499 case ICMP_UNREACH_HOST_PRECEDENCE:
500 case ICMP_UNREACH_PRECEDENCE_CUTOFF:
501 code = PRC_UNREACH_NET;
502 break;
503
504 case ICMP_UNREACH_NEEDFRAG:
505 code = PRC_MSGSIZE;
506 break;
507
508 /*
509 * RFC 1122, Sections 3.2.2.1 and 4.2.3.9.
510 * Treat subcodes 2,3 as immediate RST
511 */
512 case ICMP_UNREACH_PROTOCOL:
513 code = PRC_UNREACH_PROTOCOL;
514 break;
515 case ICMP_UNREACH_PORT:
516 code = PRC_UNREACH_PORT;
517 break;
518
519 case ICMP_UNREACH_NET_PROHIB:
520 case ICMP_UNREACH_HOST_PROHIB:
521 case ICMP_UNREACH_FILTER_PROHIB:
522 code = PRC_UNREACH_ADMIN_PROHIB;
523 break;
524
525 default:
526 goto badcode;
527 }
528 goto deliver;
529
530 case ICMP_TIMXCEED:
531 if (code > 1)
532 goto badcode;
533 code += PRC_TIMXCEED_INTRANS;
534 goto deliver;
535
536 case ICMP_PARAMPROB:
537 if (code > 1)
538 goto badcode;
539 code = PRC_PARAMPROB;
540 deliver:
541 /*
542 * Problem with datagram; advise higher level routines.
543 */
544 if (icmplen < ICMP_ADVLENMIN || icmplen < ICMP_ADVLEN(icp) ||
545 icp->icmp_ip.ip_hl < (sizeof(struct ip) >> 2)) {
546 ICMPSTAT_INC(icps_badlen);
547 goto freeit;
548 }
549 /* Discard ICMP's in response to multicast packets */
550 if (IN_MULTICAST(ntohl(icp->icmp_ip.ip_dst.s_addr)))
551 goto badcode;
552 #ifdef ICMPPRINTFS
553 if (icmpprintfs)
554 printf("deliver to protocol %d\n", icp->icmp_ip.ip_p);
555 #endif
556 icmpsrc.sin_addr = icp->icmp_ip.ip_dst;
557 /*
558 * XXX if the packet contains [IPv4 AH TCP], we can't make a
559 * notification to TCP layer.
560 */
561 i = sizeof(struct ip) + min(icmplen, ICMP_ADVLENPREF(icp));
562 ip_stripoptions(m);
563 if (m->m_len < i && (m = m_pullup(m, i)) == NULL) {
564 /* This should actually not happen */
565 ICMPSTAT_INC(icps_tooshort);
566 return (IPPROTO_DONE);
567 }
568 ip = mtod(m, struct ip *);
569 icp = (struct icmp *)(ip + 1);
570 /*
571 * The upper layer handler can rely on:
572 * - The outer IP header has no options.
573 * - The outer IP header, the ICMP header, the inner IP header,
574 * and the first n bytes of the inner payload are contiguous.
575 * n is at least 8, but might be larger based on
576 * ICMP_ADVLENPREF. See its definition in ip_icmp.h.
577 */
578 ctlfunc = inetsw[ip_protox[icp->icmp_ip.ip_p]].pr_ctlinput;
579 if (ctlfunc)
580 (*ctlfunc)(code, (struct sockaddr *)&icmpsrc,
581 (void *)&icp->icmp_ip);
582 break;
583
584 badcode:
585 ICMPSTAT_INC(icps_badcode);
586 break;
587
588 case ICMP_ECHO:
589 if (!V_icmpbmcastecho
590 && (m->m_flags & (M_MCAST | M_BCAST)) != 0) {
591 ICMPSTAT_INC(icps_bmcastecho);
592 break;
593 }
594 if (badport_bandlim(BANDLIM_ICMP_ECHO) < 0)
595 goto freeit;
596 icp->icmp_type = ICMP_ECHOREPLY;
597 goto reflect;
598
599 case ICMP_TSTAMP:
600 if (V_icmptstamprepl == 0)
601 break;
602 if (!V_icmpbmcastecho
603 && (m->m_flags & (M_MCAST | M_BCAST)) != 0) {
604 ICMPSTAT_INC(icps_bmcasttstamp);
605 break;
606 }
607 if (icmplen < ICMP_TSLEN) {
608 ICMPSTAT_INC(icps_badlen);
609 break;
610 }
611 if (badport_bandlim(BANDLIM_ICMP_TSTAMP) < 0)
612 goto freeit;
613 icp->icmp_type = ICMP_TSTAMPREPLY;
614 icp->icmp_rtime = iptime();
615 icp->icmp_ttime = icp->icmp_rtime; /* bogus, do later! */
616 goto reflect;
617
618 case ICMP_MASKREQ:
619 if (V_icmpmaskrepl == 0)
620 break;
621 /*
622 * We are not able to respond with all ones broadcast
623 * unless we receive it over a point-to-point interface.
624 */
625 if (icmplen < ICMP_MASKLEN)
626 break;
627 switch (ip->ip_dst.s_addr) {
628 case INADDR_BROADCAST:
629 case INADDR_ANY:
630 icmpdst.sin_addr = ip->ip_src;
631 break;
632
633 default:
634 icmpdst.sin_addr = ip->ip_dst;
635 }
636 ia = (struct in_ifaddr *)ifaof_ifpforaddr(
637 (struct sockaddr *)&icmpdst, m->m_pkthdr.rcvif);
638 if (ia == NULL)
639 break;
640 if (ia->ia_ifp == NULL)
641 break;
642 icp->icmp_type = ICMP_MASKREPLY;
643 if (V_icmpmaskfake == 0)
644 icp->icmp_mask = ia->ia_sockmask.sin_addr.s_addr;
645 else
646 icp->icmp_mask = V_icmpmaskfake;
647 if (ip->ip_src.s_addr == 0) {
648 if (ia->ia_ifp->if_flags & IFF_BROADCAST)
649 ip->ip_src = satosin(&ia->ia_broadaddr)->sin_addr;
650 else if (ia->ia_ifp->if_flags & IFF_POINTOPOINT)
651 ip->ip_src = satosin(&ia->ia_dstaddr)->sin_addr;
652 }
653 reflect:
654 ICMPSTAT_INC(icps_reflect);
655 ICMPSTAT_INC(icps_outhist[icp->icmp_type]);
656 icmp_reflect(m);
657 return (IPPROTO_DONE);
658
659 case ICMP_REDIRECT:
660 if (V_log_redirect) {
661 u_long src, dst, gw;
662
663 src = ntohl(ip->ip_src.s_addr);
664 dst = ntohl(icp->icmp_ip.ip_dst.s_addr);
665 gw = ntohl(icp->icmp_gwaddr.s_addr);
666 printf("icmp redirect from %d.%d.%d.%d: "
667 "%d.%d.%d.%d => %d.%d.%d.%d\n",
668 (int)(src >> 24), (int)((src >> 16) & 0xff),
669 (int)((src >> 8) & 0xff), (int)(src & 0xff),
670 (int)(dst >> 24), (int)((dst >> 16) & 0xff),
671 (int)((dst >> 8) & 0xff), (int)(dst & 0xff),
672 (int)(gw >> 24), (int)((gw >> 16) & 0xff),
673 (int)((gw >> 8) & 0xff), (int)(gw & 0xff));
674 }
675 /*
676 * RFC1812 says we must ignore ICMP redirects if we
677 * are acting as router.
678 */
679 if (V_drop_redirect || V_ipforwarding)
680 break;
681 if (code > 3)
682 goto badcode;
683 if (icmplen < ICMP_ADVLENMIN || icmplen < ICMP_ADVLEN(icp) ||
684 icp->icmp_ip.ip_hl < (sizeof(struct ip) >> 2)) {
685 ICMPSTAT_INC(icps_badlen);
686 break;
687 }
688 /*
689 * Short circuit routing redirects to force
690 * immediate change in the kernel's routing
691 * tables. The message is also handed to anyone
692 * listening on a raw socket (e.g. the routing
693 * daemon for use in updating its tables).
694 */
695 icmpgw.sin_addr = ip->ip_src;
696 icmpdst.sin_addr = icp->icmp_gwaddr;
697 #ifdef ICMPPRINTFS
698 if (icmpprintfs) {
699 char dstbuf[INET_ADDRSTRLEN];
700 char gwbuf[INET_ADDRSTRLEN];
701
702 printf("redirect dst %s to %s\n",
703 inet_ntoa_r(icp->icmp_ip.ip_dst, dstbuf),
704 inet_ntoa_r(icp->icmp_gwaddr, gwbuf));
705 }
706 #endif
707 icmpsrc.sin_addr = icp->icmp_ip.ip_dst;
708
709 /*
710 * RFC 1122 says network (code 0,2) redirects SHOULD
711 * be treated identically to the host redirects.
712 * Given that, ignore network masks.
713 */
714
715 /*
716 * Variable values:
717 * icmpsrc: route destination
718 * icmpdst: route gateway
719 * icmpgw: message source
720 */
721
722 if (icmp_verify_redirect_gateway(&icmpgw, &icmpsrc, &icmpdst,
723 M_GETFIB(m)) != 0) {
724 /* TODO: increment bad redirects here */
725 break;
726 }
727
728 for ( fibnum = 0; fibnum < rt_numfibs; fibnum++) {
729 rib_add_redirect(fibnum, (struct sockaddr *)&icmpsrc,
730 (struct sockaddr *)&icmpdst,
731 (struct sockaddr *)&icmpgw, m->m_pkthdr.rcvif,
732 RTF_GATEWAY, V_redirtimeout);
733 }
734 pfctlinput(PRC_REDIRECT_HOST, (struct sockaddr *)&icmpsrc);
735 break;
736
737 /*
738 * No kernel processing for the following;
739 * just fall through to send to raw listener.
740 */
741 case ICMP_ECHOREPLY:
742 case ICMP_ROUTERADVERT:
743 case ICMP_ROUTERSOLICIT:
744 case ICMP_TSTAMPREPLY:
745 case ICMP_IREQREPLY:
746 case ICMP_MASKREPLY:
747 case ICMP_SOURCEQUENCH:
748 default:
749 break;
750 }
751
752 raw:
753 *mp = m;
754 rip_input(mp, offp, proto);
755 return (IPPROTO_DONE);
756
757 freeit:
758 m_freem(m);
759 return (IPPROTO_DONE);
760 }
761
762 /*
763 * Reflect the ip packet back to the source
764 */
765 static void
icmp_reflect(struct mbuf * m)766 icmp_reflect(struct mbuf *m)
767 {
768 struct rm_priotracker in_ifa_tracker;
769 struct ip *ip = mtod(m, struct ip *);
770 struct ifaddr *ifa;
771 struct ifnet *ifp;
772 struct in_ifaddr *ia;
773 struct in_addr t;
774 struct nhop_object *nh;
775 struct mbuf *opts = NULL;
776 int optlen = (ip->ip_hl << 2) - sizeof(struct ip);
777
778 NET_EPOCH_ASSERT();
779
780 if (IN_MULTICAST(ntohl(ip->ip_src.s_addr)) ||
781 IN_EXPERIMENTAL(ntohl(ip->ip_src.s_addr)) ||
782 IN_ZERONET(ntohl(ip->ip_src.s_addr)) ) {
783 m_freem(m); /* Bad return address */
784 ICMPSTAT_INC(icps_badaddr);
785 goto done; /* Ip_output() will check for broadcast */
786 }
787
788 t = ip->ip_dst;
789 ip->ip_dst = ip->ip_src;
790
791 /*
792 * Source selection for ICMP replies:
793 *
794 * If the incoming packet was addressed directly to one of our
795 * own addresses, use dst as the src for the reply.
796 */
797 IN_IFADDR_RLOCK(&in_ifa_tracker);
798 LIST_FOREACH(ia, INADDR_HASH(t.s_addr), ia_hash) {
799 if (t.s_addr == IA_SIN(ia)->sin_addr.s_addr) {
800 t = IA_SIN(ia)->sin_addr;
801 IN_IFADDR_RUNLOCK(&in_ifa_tracker);
802 goto match;
803 }
804 }
805 IN_IFADDR_RUNLOCK(&in_ifa_tracker);
806
807 /*
808 * If the incoming packet was addressed to one of our broadcast
809 * addresses, use the first non-broadcast address which corresponds
810 * to the incoming interface.
811 */
812 ifp = m->m_pkthdr.rcvif;
813 if (ifp != NULL && ifp->if_flags & IFF_BROADCAST) {
814 CK_STAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
815 if (ifa->ifa_addr->sa_family != AF_INET)
816 continue;
817 ia = ifatoia(ifa);
818 if (satosin(&ia->ia_broadaddr)->sin_addr.s_addr ==
819 t.s_addr) {
820 t = IA_SIN(ia)->sin_addr;
821 goto match;
822 }
823 }
824 }
825 /*
826 * If the packet was transiting through us, use the address of
827 * the interface the packet came through in. If that interface
828 * doesn't have a suitable IP address, the normal selection
829 * criteria apply.
830 */
831 if (V_icmp_rfi && ifp != NULL) {
832 CK_STAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
833 if (ifa->ifa_addr->sa_family != AF_INET)
834 continue;
835 ia = ifatoia(ifa);
836 t = IA_SIN(ia)->sin_addr;
837 goto match;
838 }
839 }
840 /*
841 * If the incoming packet was not addressed directly to us, use
842 * designated interface for icmp replies specified by sysctl
843 * net.inet.icmp.reply_src (default not set). Otherwise continue
844 * with normal source selection.
845 */
846 if (V_reply_src[0] != '\0' && (ifp = ifunit(V_reply_src))) {
847 CK_STAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
848 if (ifa->ifa_addr->sa_family != AF_INET)
849 continue;
850 ia = ifatoia(ifa);
851 t = IA_SIN(ia)->sin_addr;
852 goto match;
853 }
854 }
855 /*
856 * If the packet was transiting through us, use the address of
857 * the interface that is the closest to the packet source.
858 * When we don't have a route back to the packet source, stop here
859 * and drop the packet.
860 */
861 nh = fib4_lookup(M_GETFIB(m), ip->ip_dst, 0, NHR_NONE, 0);
862 if (nh == NULL) {
863 m_freem(m);
864 ICMPSTAT_INC(icps_noroute);
865 goto done;
866 }
867 t = IA_SIN(ifatoia(nh->nh_ifa))->sin_addr;
868 match:
869 #ifdef MAC
870 mac_netinet_icmp_replyinplace(m);
871 #endif
872 ip->ip_src = t;
873 ip->ip_ttl = V_ip_defttl;
874
875 if (optlen > 0) {
876 u_char *cp;
877 int opt, cnt;
878 u_int len;
879
880 /*
881 * Retrieve any source routing from the incoming packet;
882 * add on any record-route or timestamp options.
883 */
884 cp = (u_char *) (ip + 1);
885 if ((opts = ip_srcroute(m)) == NULL &&
886 (opts = m_gethdr(M_NOWAIT, MT_DATA))) {
887 opts->m_len = sizeof(struct in_addr);
888 mtod(opts, struct in_addr *)->s_addr = 0;
889 }
890 if (opts) {
891 #ifdef ICMPPRINTFS
892 if (icmpprintfs)
893 printf("icmp_reflect optlen %d rt %d => ",
894 optlen, opts->m_len);
895 #endif
896 for (cnt = optlen; cnt > 0; cnt -= len, cp += len) {
897 opt = cp[IPOPT_OPTVAL];
898 if (opt == IPOPT_EOL)
899 break;
900 if (opt == IPOPT_NOP)
901 len = 1;
902 else {
903 if (cnt < IPOPT_OLEN + sizeof(*cp))
904 break;
905 len = cp[IPOPT_OLEN];
906 if (len < IPOPT_OLEN + sizeof(*cp) ||
907 len > cnt)
908 break;
909 }
910 /*
911 * Should check for overflow, but it "can't happen"
912 */
913 if (opt == IPOPT_RR || opt == IPOPT_TS ||
914 opt == IPOPT_SECURITY) {
915 bcopy((caddr_t)cp,
916 mtod(opts, caddr_t) + opts->m_len, len);
917 opts->m_len += len;
918 }
919 }
920 /* Terminate & pad, if necessary */
921 cnt = opts->m_len % 4;
922 if (cnt) {
923 for (; cnt < 4; cnt++) {
924 *(mtod(opts, caddr_t) + opts->m_len) =
925 IPOPT_EOL;
926 opts->m_len++;
927 }
928 }
929 #ifdef ICMPPRINTFS
930 if (icmpprintfs)
931 printf("%d\n", opts->m_len);
932 #endif
933 }
934 ip_stripoptions(m);
935 }
936 m_tag_delete_nonpersistent(m);
937 m->m_flags &= ~(M_BCAST|M_MCAST);
938 icmp_send(m, opts);
939 done:
940 if (opts)
941 (void)m_free(opts);
942 }
943
944 /*
945 * Verifies if redirect message is valid, according to RFC 1122
946 *
947 * @src: sockaddr with address of redirect originator
948 * @dst: sockaddr with destination in question
949 * @gateway: new proposed gateway
950 *
951 * Returns 0 on success.
952 */
953 static int
icmp_verify_redirect_gateway(struct sockaddr_in * src,struct sockaddr_in * dst,struct sockaddr_in * gateway,u_int fibnum)954 icmp_verify_redirect_gateway(struct sockaddr_in *src, struct sockaddr_in *dst,
955 struct sockaddr_in *gateway, u_int fibnum)
956 {
957 struct nhop_object *nh;
958 struct ifaddr *ifa;
959
960 NET_EPOCH_ASSERT();
961
962 /* Verify the gateway is directly reachable. */
963 if ((ifa = ifa_ifwithnet((struct sockaddr *)gateway, 0, fibnum))==NULL)
964 return (ENETUNREACH);
965
966 /* TODO: fib-aware. */
967 if (ifa_ifwithaddr_check((struct sockaddr *)gateway))
968 return (EHOSTUNREACH);
969
970 nh = fib4_lookup(fibnum, dst->sin_addr, 0, NHR_NONE, 0);
971 if (nh == NULL)
972 return (EINVAL);
973
974 /*
975 * If the redirect isn't from our current router for this dst,
976 * it's either old or wrong. If it redirects us to ourselves,
977 * we have a routing loop, perhaps as a result of an interface
978 * going down recently.
979 */
980 if (!sa_equal((struct sockaddr *)src, &nh->gw_sa))
981 return (EINVAL);
982 if (nh->nh_ifa != ifa && ifa->ifa_addr->sa_family != AF_LINK)
983 return (EINVAL);
984
985 /* If host route already exists, ignore redirect. */
986 if (nh->nh_flags & NHF_HOST)
987 return (EEXIST);
988
989 /* If the prefix is directly reachable, ignore redirect. */
990 if (!(nh->nh_flags & NHF_GATEWAY))
991 return (EEXIST);
992
993 return (0);
994 }
995
996 /*
997 * Send an icmp packet back to the ip level,
998 * after supplying a checksum.
999 */
1000 static void
icmp_send(struct mbuf * m,struct mbuf * opts)1001 icmp_send(struct mbuf *m, struct mbuf *opts)
1002 {
1003 struct ip *ip = mtod(m, struct ip *);
1004 int hlen;
1005 struct icmp *icp;
1006
1007 hlen = ip->ip_hl << 2;
1008 m->m_data += hlen;
1009 m->m_len -= hlen;
1010 icp = mtod(m, struct icmp *);
1011 icp->icmp_cksum = 0;
1012 icp->icmp_cksum = in_cksum(m, ntohs(ip->ip_len) - hlen);
1013 m->m_data -= hlen;
1014 m->m_len += hlen;
1015 m->m_pkthdr.rcvif = (struct ifnet *)0;
1016 #ifdef ICMPPRINTFS
1017 if (icmpprintfs) {
1018 char dstbuf[INET_ADDRSTRLEN];
1019 char srcbuf[INET_ADDRSTRLEN];
1020
1021 printf("icmp_send dst %s src %s\n",
1022 inet_ntoa_r(ip->ip_dst, dstbuf),
1023 inet_ntoa_r(ip->ip_src, srcbuf));
1024 }
1025 #endif
1026 (void) ip_output(m, opts, NULL, 0, NULL, NULL);
1027 }
1028
1029 /*
1030 * Return milliseconds since 00:00 UTC in network format.
1031 */
1032 uint32_t
iptime(void)1033 iptime(void)
1034 {
1035 struct timeval atv;
1036 u_long t;
1037
1038 getmicrotime(&atv);
1039 t = (atv.tv_sec % (24*60*60)) * 1000 + atv.tv_usec / 1000;
1040 return (htonl(t));
1041 }
1042
1043 /*
1044 * Return the next larger or smaller MTU plateau (table from RFC 1191)
1045 * given current value MTU. If DIR is less than zero, a larger plateau
1046 * is returned; otherwise, a smaller value is returned.
1047 */
1048 int
ip_next_mtu(int mtu,int dir)1049 ip_next_mtu(int mtu, int dir)
1050 {
1051 static int mtutab[] = {
1052 65535, 32000, 17914, 8166, 4352, 2002, 1492, 1280, 1006, 508,
1053 296, 68, 0
1054 };
1055 int i, size;
1056
1057 size = (sizeof mtutab) / (sizeof mtutab[0]);
1058 if (dir >= 0) {
1059 for (i = 0; i < size; i++)
1060 if (mtu > mtutab[i])
1061 return mtutab[i];
1062 } else {
1063 for (i = size - 1; i >= 0; i--)
1064 if (mtu < mtutab[i])
1065 return mtutab[i];
1066 if (mtu == mtutab[0])
1067 return mtutab[0];
1068 }
1069 return 0;
1070 }
1071 #endif /* INET */
1072
1073 /*
1074 * badport_bandlim() - check for ICMP bandwidth limit
1075 *
1076 * Return 0 if it is ok to send an ICMP error response, -1 if we have
1077 * hit our bandwidth limit and it is not ok.
1078 *
1079 * If icmplim is <= 0, the feature is disabled and 0 is returned.
1080 *
1081 * For now we separate the TCP and UDP subsystems w/ different 'which'
1082 * values. We may eventually remove this separation (and simplify the
1083 * code further).
1084 *
1085 * Note that the printing of the error message is delayed so we can
1086 * properly print the icmp error rate that the system was trying to do
1087 * (i.e. 22000/100 pps, etc...). This can cause long delays in printing
1088 * the 'final' error, but it doesn't make sense to solve the printing
1089 * delay with more complex code.
1090 */
1091 VNET_DEFINE_STATIC(struct counter_rate, icmp_rates[BANDLIM_MAX]);
1092 #define V_icmp_rates VNET(icmp_rates)
1093
1094 static const char *icmp_rate_descrs[BANDLIM_MAX] = {
1095 [BANDLIM_ICMP_UNREACH] = "icmp unreach",
1096 [BANDLIM_ICMP_ECHO] = "icmp ping",
1097 [BANDLIM_ICMP_TSTAMP] = "icmp tstamp",
1098 [BANDLIM_RST_CLOSEDPORT] = "closed port RST",
1099 [BANDLIM_RST_OPENPORT] = "open port RST",
1100 [BANDLIM_ICMP6_UNREACH] = "icmp6 unreach",
1101 [BANDLIM_SCTP_OOTB] = "sctp ootb",
1102 };
1103
1104 static void
icmplim_new_jitter(int which)1105 icmplim_new_jitter(int which)
1106 {
1107 /*
1108 * Adjust limit +/- to jitter the measurement to deny a side-channel
1109 * port scan as in https://dl.acm.org/doi/10.1145/3372297.3417280
1110 */
1111 KASSERT(which >= 0 && which < BANDLIM_MAX,
1112 ("%s: which %d", __func__, which));
1113 if (V_icmplim_jitter > 0)
1114 V_icmplim_curr_jitter[which] =
1115 arc4random_uniform(V_icmplim_jitter * 2 + 1) -
1116 V_icmplim_jitter;
1117 }
1118
1119 static int
sysctl_icmplim_and_jitter(SYSCTL_HANDLER_ARGS)1120 sysctl_icmplim_and_jitter(SYSCTL_HANDLER_ARGS)
1121 {
1122 uint32_t new;
1123 int error;
1124 bool lim;
1125
1126 MPASS(oidp->oid_arg1 == &VNET_NAME(icmplim) ||
1127 oidp->oid_arg1 == &VNET_NAME(icmplim_jitter));
1128
1129 lim = (oidp->oid_arg1 == &VNET_NAME(icmplim));
1130 new = lim ? V_icmplim : V_icmplim_jitter;
1131 error = sysctl_handle_int(oidp, &new, 0, req);
1132 if (error == 0 && req->newptr) {
1133 if (lim) {
1134 if (new != 0 && new <= V_icmplim_jitter)
1135 error = EINVAL;
1136 else
1137 V_icmplim = new;
1138 } else {
1139 if (new >= V_icmplim)
1140 error = EINVAL;
1141 else {
1142 V_icmplim_jitter = new;
1143 for (int i = 0; i < BANDLIM_MAX; i++) {
1144 icmplim_new_jitter(i);
1145 }
1146 }
1147 }
1148 }
1149 MPASS(V_icmplim == 0 || V_icmplim > V_icmplim_jitter);
1150
1151 return (error);
1152 }
1153
1154 static void
icmp_bandlimit_init(void)1155 icmp_bandlimit_init(void)
1156 {
1157
1158 for (int i = 0; i < BANDLIM_MAX; i++) {
1159 V_icmp_rates[i].cr_rate = counter_u64_alloc(M_WAITOK);
1160 V_icmp_rates[i].cr_ticks = ticks;
1161 icmplim_new_jitter(i);
1162 }
1163 }
1164 VNET_SYSINIT(icmp_bandlimit, SI_SUB_PROTO_DOMAIN, SI_ORDER_ANY,
1165 icmp_bandlimit_init, NULL);
1166
1167 #ifdef VIMAGE
1168 static void
icmp_bandlimit_uninit(void)1169 icmp_bandlimit_uninit(void)
1170 {
1171
1172 for (int i = 0; i < BANDLIM_MAX; i++)
1173 counter_u64_free(V_icmp_rates[i].cr_rate);
1174 }
1175 VNET_SYSUNINIT(icmp_bandlimit, SI_SUB_PROTO_DOMAIN, SI_ORDER_THIRD,
1176 icmp_bandlimit_uninit, NULL);
1177 #endif
1178
1179 int
badport_bandlim(int which)1180 badport_bandlim(int which)
1181 {
1182 int64_t pps;
1183
1184 if (V_icmplim == 0 || which == BANDLIM_UNLIMITED)
1185 return (0);
1186
1187 KASSERT(which >= 0 && which < BANDLIM_MAX,
1188 ("%s: which %d", __func__, which));
1189
1190 pps = counter_ratecheck(&V_icmp_rates[which], V_icmplim +
1191 V_icmplim_curr_jitter[which]);
1192 if (pps > 0) {
1193 if (V_icmplim_output)
1194 log(LOG_NOTICE,
1195 "Limiting %s response from %jd to %d packets/sec\n",
1196 icmp_rate_descrs[which], (intmax_t )pps,
1197 V_icmplim + V_icmplim_curr_jitter[which]);
1198 icmplim_new_jitter(which);
1199 }
1200 if (pps == -1)
1201 return (-1);
1202 return (0);
1203 }
1204