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