ip_icmp.c revision 1.123 1 /* $NetBSD: ip_icmp.c,v 1.123 2010/06/26 14:24:29 kefren Exp $ */
2
3 /*
4 * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
5 * 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 project 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 PROJECT 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 PROJECT 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
32 /*-
33 * Copyright (c) 1998, 2000 The NetBSD Foundation, Inc.
34 * All rights reserved.
35 *
36 * This code is derived from software contributed to The NetBSD Foundation
37 * by Public Access Networks Corporation ("Panix"). It was developed under
38 * contract to Panix by Eric Haszlakiewicz and Thor Lancelot Simon.
39 *
40 * This code is derived from software contributed to The NetBSD Foundation
41 * by Jason R. Thorpe of Zembu Labs, Inc.
42 *
43 * Redistribution and use in source and binary forms, with or without
44 * modification, are permitted provided that the following conditions
45 * are met:
46 * 1. Redistributions of source code must retain the above copyright
47 * notice, this list of conditions and the following disclaimer.
48 * 2. Redistributions in binary form must reproduce the above copyright
49 * notice, this list of conditions and the following disclaimer in the
50 * documentation and/or other materials provided with the distribution.
51 *
52 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
53 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
54 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
55 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
56 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
57 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
58 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
59 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
60 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
61 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
62 * POSSIBILITY OF SUCH DAMAGE.
63 */
64
65 /*
66 * Copyright (c) 1982, 1986, 1988, 1993
67 * The Regents of the University of California. All rights reserved.
68 *
69 * Redistribution and use in source and binary forms, with or without
70 * modification, are permitted provided that the following conditions
71 * are met:
72 * 1. Redistributions of source code must retain the above copyright
73 * notice, this list of conditions and the following disclaimer.
74 * 2. Redistributions in binary form must reproduce the above copyright
75 * notice, this list of conditions and the following disclaimer in the
76 * documentation and/or other materials provided with the distribution.
77 * 3. Neither the name of the University nor the names of its contributors
78 * may be used to endorse or promote products derived from this software
79 * without specific prior written permission.
80 *
81 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
82 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
83 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
84 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
85 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
86 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
87 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
88 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
89 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
90 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
91 * SUCH DAMAGE.
92 *
93 * @(#)ip_icmp.c 8.2 (Berkeley) 1/4/94
94 */
95
96 #include <sys/cdefs.h>
97 __KERNEL_RCSID(0, "$NetBSD: ip_icmp.c,v 1.123 2010/06/26 14:24:29 kefren Exp $");
98
99 #include "opt_ipsec.h"
100
101 #include <sys/param.h>
102 #include <sys/systm.h>
103 #include <sys/malloc.h>
104 #include <sys/mbuf.h>
105 #include <sys/protosw.h>
106 #include <sys/socket.h>
107 #include <sys/time.h>
108 #include <sys/kernel.h>
109 #include <sys/syslog.h>
110 #include <sys/sysctl.h>
111
112 #include <net/if.h>
113 #include <net/route.h>
114
115 #include <netinet/in.h>
116 #include <netinet/in_systm.h>
117 #include <netinet/in_var.h>
118 #include <netinet/ip.h>
119 #include <netinet/ip_icmp.h>
120 #include <netinet/ip_var.h>
121 #include <netinet/in_pcb.h>
122 #include <netinet/in_proto.h>
123 #include <netinet/icmp_var.h>
124 #include <netinet/icmp_private.h>
125
126 #ifdef IPSEC
127 #include <netinet6/ipsec.h>
128 #include <netkey/key.h>
129 #endif
130
131 #ifdef FAST_IPSEC
132 #include <netipsec/ipsec.h>
133 #include <netipsec/key.h>
134 #endif /* FAST_IPSEC*/
135
136 #include <machine/stdarg.h>
137
138 /*
139 * ICMP routines: error generation, receive packet processing, and
140 * routines to turnaround packets back to the originator, and
141 * host table maintenance routines.
142 */
143
144 int icmpmaskrepl = 0;
145 int icmpbmcastecho = 0;
146 #ifdef ICMPPRINTFS
147 int icmpprintfs = 0;
148 #endif
149 int icmpreturndatabytes = 8;
150
151 percpu_t *icmpstat_percpu;
152
153 /*
154 * List of callbacks to notify when Path MTU changes are made.
155 */
156 struct icmp_mtudisc_callback {
157 LIST_ENTRY(icmp_mtudisc_callback) mc_list;
158 void (*mc_func)(struct in_addr);
159 };
160
161 LIST_HEAD(, icmp_mtudisc_callback) icmp_mtudisc_callbacks =
162 LIST_HEAD_INITIALIZER(&icmp_mtudisc_callbacks);
163
164 #if 0
165 static u_int ip_next_mtu(u_int, int);
166 #else
167 /*static*/ u_int ip_next_mtu(u_int, int);
168 #endif
169
170 extern int icmperrppslim;
171 static int icmperrpps_count = 0;
172 static struct timeval icmperrppslim_last;
173 static int icmp_rediraccept = 1;
174 static int icmp_redirtimeout = 600;
175 static struct rttimer_queue *icmp_redirect_timeout_q = NULL;
176
177 static void icmp_mtudisc_timeout(struct rtentry *, struct rttimer *);
178 static void icmp_redirect_timeout(struct rtentry *, struct rttimer *);
179
180 static void sysctl_netinet_icmp_setup(struct sysctllog **);
181
182 void
183 icmp_init(void)
184 {
185
186 sysctl_netinet_icmp_setup(NULL);
187
188 /*
189 * This is only useful if the user initializes redirtimeout to
190 * something other than zero.
191 */
192 if (icmp_redirtimeout != 0) {
193 icmp_redirect_timeout_q =
194 rt_timer_queue_create(icmp_redirtimeout);
195 }
196
197 icmpstat_percpu = percpu_alloc(sizeof(uint64_t) * ICMP_NSTATS);
198 }
199
200 /*
201 * Register a Path MTU Discovery callback.
202 */
203 void
204 icmp_mtudisc_callback_register(void (*func)(struct in_addr))
205 {
206 struct icmp_mtudisc_callback *mc;
207
208 for (mc = LIST_FIRST(&icmp_mtudisc_callbacks); mc != NULL;
209 mc = LIST_NEXT(mc, mc_list)) {
210 if (mc->mc_func == func)
211 return;
212 }
213
214 mc = malloc(sizeof(*mc), M_PCB, M_NOWAIT);
215 if (mc == NULL)
216 panic("icmp_mtudisc_callback_register");
217
218 mc->mc_func = func;
219 LIST_INSERT_HEAD(&icmp_mtudisc_callbacks, mc, mc_list);
220 }
221
222 /*
223 * Generate an error packet of type error
224 * in response to bad packet ip.
225 */
226 void
227 icmp_error(struct mbuf *n, int type, int code, n_long dest,
228 int destmtu)
229 {
230 struct ip *oip = mtod(n, struct ip *), *nip;
231 unsigned oiplen = oip->ip_hl << 2;
232 struct icmp *icp;
233 struct mbuf *m;
234 struct m_tag *mtag;
235 unsigned icmplen, mblen;
236
237 #ifdef ICMPPRINTFS
238 if (icmpprintfs)
239 printf("icmp_error(%p, type:%d, code:%d)\n", oip, type, code);
240 #endif
241 if (type != ICMP_REDIRECT)
242 ICMP_STATINC(ICMP_STAT_ERROR);
243 /*
244 * Don't send error if the original packet was encrypted.
245 * Don't send error if not the first fragment of message.
246 * Don't error if the old packet protocol was ICMP
247 * error message, only known informational types.
248 */
249 if (n->m_flags & M_DECRYPTED)
250 goto freeit;
251 if (oip->ip_off &~ htons(IP_MF|IP_DF))
252 goto freeit;
253 if (oip->ip_p == IPPROTO_ICMP && type != ICMP_REDIRECT &&
254 n->m_len >= oiplen + ICMP_MINLEN &&
255 !ICMP_INFOTYPE(((struct icmp *)((char *)oip + oiplen))->icmp_type)) {
256 ICMP_STATINC(ICMP_STAT_OLDICMP);
257 goto freeit;
258 }
259 /* Don't send error in response to a multicast or broadcast packet */
260 if (n->m_flags & (M_BCAST|M_MCAST))
261 goto freeit;
262
263 /*
264 * First, do a rate limitation check.
265 */
266 if (icmp_ratelimit(&oip->ip_src, type, code)) {
267 /* XXX stat */
268 goto freeit;
269 }
270
271 /*
272 * Now, formulate icmp message
273 */
274 icmplen = oiplen + min(icmpreturndatabytes,
275 ntohs(oip->ip_len) - oiplen);
276 /*
277 * Defend against mbuf chains shorter than oip->ip_len - oiplen:
278 */
279 mblen = 0;
280 for (m = n; m && (mblen < icmplen); m = m->m_next)
281 mblen += m->m_len;
282 icmplen = min(mblen, icmplen);
283
284 /*
285 * As we are not required to return everything we have,
286 * we return whatever we can return at ease.
287 *
288 * Note that ICMP datagrams longer than 576 octets are out of spec
289 * according to RFC1812; the limit on icmpreturndatabytes below in
290 * icmp_sysctl will keep things below that limit.
291 */
292
293 KASSERT(ICMP_MINLEN <= MCLBYTES);
294
295 if (icmplen + ICMP_MINLEN > MCLBYTES)
296 icmplen = MCLBYTES - ICMP_MINLEN;
297
298 m = m_gethdr(M_DONTWAIT, MT_HEADER);
299 if (m && (icmplen + ICMP_MINLEN > MHLEN)) {
300 MCLGET(m, M_DONTWAIT);
301 if ((m->m_flags & M_EXT) == 0) {
302 m_freem(m);
303 m = NULL;
304 }
305 }
306 if (m == NULL)
307 goto freeit;
308 MCLAIM(m, n->m_owner);
309 m->m_len = icmplen + ICMP_MINLEN;
310 if ((m->m_flags & M_EXT) == 0)
311 MH_ALIGN(m, m->m_len);
312 icp = mtod(m, struct icmp *);
313 if ((u_int)type > ICMP_MAXTYPE)
314 panic("icmp_error");
315 ICMP_STATINC(ICMP_STAT_OUTHIST + type);
316 icp->icmp_type = type;
317 if (type == ICMP_REDIRECT)
318 icp->icmp_gwaddr.s_addr = dest;
319 else {
320 icp->icmp_void = 0;
321 /*
322 * The following assignments assume an overlay with the
323 * zeroed icmp_void field.
324 */
325 if (type == ICMP_PARAMPROB) {
326 icp->icmp_pptr = code;
327 code = 0;
328 } else if (type == ICMP_UNREACH &&
329 code == ICMP_UNREACH_NEEDFRAG && destmtu)
330 icp->icmp_nextmtu = htons(destmtu);
331 }
332
333 icp->icmp_code = code;
334 m_copydata(n, 0, icmplen, (void *)&icp->icmp_ip);
335
336 /*
337 * Now, copy old ip header (without options)
338 * in front of icmp message.
339 */
340 if (m->m_data - sizeof(struct ip) < m->m_pktdat)
341 panic("icmp len");
342 m->m_data -= sizeof(struct ip);
343 m->m_len += sizeof(struct ip);
344 m->m_pkthdr.len = m->m_len;
345 m->m_pkthdr.rcvif = n->m_pkthdr.rcvif;
346 nip = mtod(m, struct ip *);
347 /* ip_v set in ip_output */
348 nip->ip_hl = sizeof(struct ip) >> 2;
349 nip->ip_tos = 0;
350 nip->ip_len = htons(m->m_len);
351 /* ip_id set in ip_output */
352 nip->ip_off = htons(0);
353 /* ip_ttl set in icmp_reflect */
354 nip->ip_p = IPPROTO_ICMP;
355 nip->ip_src = oip->ip_src;
356 nip->ip_dst = oip->ip_dst;
357 /* move PF m_tag to new packet, if it exists */
358 mtag = m_tag_find(n, PACKET_TAG_PF, NULL);
359 if (mtag != NULL) {
360 m_tag_unlink(n, mtag);
361 m_tag_prepend(m, mtag);
362 }
363 icmp_reflect(m);
364
365 freeit:
366 m_freem(n);
367 }
368
369 struct sockaddr_in icmpsrc = {
370 .sin_len = sizeof (struct sockaddr_in),
371 .sin_family = AF_INET,
372 };
373 static struct sockaddr_in icmpdst = {
374 .sin_len = sizeof (struct sockaddr_in),
375 .sin_family = AF_INET,
376 };
377 static struct sockaddr_in icmpgw = {
378 .sin_len = sizeof (struct sockaddr_in),
379 .sin_family = AF_INET,
380 };
381 struct sockaddr_in icmpmask = {
382 .sin_len = 8,
383 .sin_family = 0,
384 };
385
386 /*
387 * Process a received ICMP message.
388 */
389 void
390 icmp_input(struct mbuf *m, ...)
391 {
392 int proto;
393 struct icmp *icp;
394 struct ip *ip = mtod(m, struct ip *);
395 int icmplen;
396 int i;
397 struct in_ifaddr *ia;
398 void *(*ctlfunc)(int, const struct sockaddr *, void *);
399 int code;
400 int hlen;
401 va_list ap;
402 struct rtentry *rt;
403
404 va_start(ap, m);
405 hlen = va_arg(ap, int);
406 proto = va_arg(ap, int);
407 va_end(ap);
408
409 /*
410 * Locate icmp structure in mbuf, and check
411 * that not corrupted and of at least minimum length.
412 */
413 icmplen = ntohs(ip->ip_len) - hlen;
414 #ifdef ICMPPRINTFS
415 if (icmpprintfs) {
416 printf("icmp_input from `%s' to ", inet_ntoa(ip->ip_src));
417 printf("`%s', len %d\n", inet_ntoa(ip->ip_dst), icmplen);
418 }
419 #endif
420 if (icmplen < ICMP_MINLEN) {
421 ICMP_STATINC(ICMP_STAT_TOOSHORT);
422 goto freeit;
423 }
424 i = hlen + min(icmplen, ICMP_ADVLENMIN);
425 if ((m->m_len < i || M_READONLY(m)) && (m = m_pullup(m, i)) == 0) {
426 ICMP_STATINC(ICMP_STAT_TOOSHORT);
427 return;
428 }
429 ip = mtod(m, struct ip *);
430 m->m_len -= hlen;
431 m->m_data += hlen;
432 icp = mtod(m, struct icmp *);
433 /* Don't need to assert alignment, here. */
434 if (in_cksum(m, icmplen)) {
435 ICMP_STATINC(ICMP_STAT_CHECKSUM);
436 goto freeit;
437 }
438 m->m_len += hlen;
439 m->m_data -= hlen;
440
441 #ifdef ICMPPRINTFS
442 /*
443 * Message type specific processing.
444 */
445 if (icmpprintfs)
446 printf("icmp_input(type:%d, code:%d)\n", icp->icmp_type,
447 icp->icmp_code);
448 #endif
449 if (icp->icmp_type > ICMP_MAXTYPE)
450 goto raw;
451 ICMP_STATINC(ICMP_STAT_INHIST + icp->icmp_type);
452 code = icp->icmp_code;
453 switch (icp->icmp_type) {
454
455 case ICMP_UNREACH:
456 switch (code) {
457 case ICMP_UNREACH_NET:
458 code = PRC_UNREACH_NET;
459 break;
460
461 case ICMP_UNREACH_HOST:
462 code = PRC_UNREACH_HOST;
463 break;
464
465 case ICMP_UNREACH_PROTOCOL:
466 code = PRC_UNREACH_PROTOCOL;
467 break;
468
469 case ICMP_UNREACH_PORT:
470 code = PRC_UNREACH_PORT;
471 break;
472
473 case ICMP_UNREACH_SRCFAIL:
474 code = PRC_UNREACH_SRCFAIL;
475 break;
476
477 case ICMP_UNREACH_NEEDFRAG:
478 code = PRC_MSGSIZE;
479 break;
480
481 case ICMP_UNREACH_NET_UNKNOWN:
482 case ICMP_UNREACH_NET_PROHIB:
483 case ICMP_UNREACH_TOSNET:
484 code = PRC_UNREACH_NET;
485 break;
486
487 case ICMP_UNREACH_HOST_UNKNOWN:
488 case ICMP_UNREACH_ISOLATED:
489 case ICMP_UNREACH_HOST_PROHIB:
490 case ICMP_UNREACH_TOSHOST:
491 code = PRC_UNREACH_HOST;
492 break;
493
494 default:
495 goto badcode;
496 }
497 goto deliver;
498
499 case ICMP_TIMXCEED:
500 if (code > 1)
501 goto badcode;
502 code += PRC_TIMXCEED_INTRANS;
503 goto deliver;
504
505 case ICMP_PARAMPROB:
506 if (code > 1)
507 goto badcode;
508 code = PRC_PARAMPROB;
509 goto deliver;
510
511 case ICMP_SOURCEQUENCH:
512 if (code)
513 goto badcode;
514 code = PRC_QUENCH;
515 goto deliver;
516
517 deliver:
518 /*
519 * Problem with datagram; advise higher level routines.
520 */
521 if (icmplen < ICMP_ADVLENMIN || icmplen < ICMP_ADVLEN(icp) ||
522 icp->icmp_ip.ip_hl < (sizeof(struct ip) >> 2)) {
523 ICMP_STATINC(ICMP_STAT_BADLEN);
524 goto freeit;
525 }
526 if (IN_MULTICAST(icp->icmp_ip.ip_dst.s_addr))
527 goto badcode;
528 #ifdef ICMPPRINTFS
529 if (icmpprintfs)
530 printf("deliver to protocol %d\n", icp->icmp_ip.ip_p);
531 #endif
532 icmpsrc.sin_addr = icp->icmp_ip.ip_dst;
533 ctlfunc = inetsw[ip_protox[icp->icmp_ip.ip_p]].pr_ctlinput;
534 if (ctlfunc)
535 (void) (*ctlfunc)(code, sintosa(&icmpsrc),
536 &icp->icmp_ip);
537 break;
538
539 badcode:
540 ICMP_STATINC(ICMP_STAT_BADCODE);
541 break;
542
543 case ICMP_ECHO:
544 if (!icmpbmcastecho &&
545 (m->m_flags & (M_MCAST | M_BCAST)) != 0) {
546 ICMP_STATINC(ICMP_STAT_BMCASTECHO);
547 break;
548 }
549 icp->icmp_type = ICMP_ECHOREPLY;
550 goto reflect;
551
552 case ICMP_TSTAMP:
553 if (icmplen < ICMP_TSLEN) {
554 ICMP_STATINC(ICMP_STAT_BADLEN);
555 break;
556 }
557 if (!icmpbmcastecho &&
558 (m->m_flags & (M_MCAST | M_BCAST)) != 0) {
559 ICMP_STATINC(ICMP_STAT_BMCASTTSTAMP);
560 break;
561 }
562 icp->icmp_type = ICMP_TSTAMPREPLY;
563 icp->icmp_rtime = iptime();
564 icp->icmp_ttime = icp->icmp_rtime; /* bogus, do later! */
565 goto reflect;
566
567 case ICMP_MASKREQ:
568 if (icmpmaskrepl == 0)
569 break;
570 /*
571 * We are not able to respond with all ones broadcast
572 * unless we receive it over a point-to-point interface.
573 */
574 if (icmplen < ICMP_MASKLEN) {
575 ICMP_STATINC(ICMP_STAT_BADLEN);
576 break;
577 }
578 if (ip->ip_dst.s_addr == INADDR_BROADCAST ||
579 in_nullhost(ip->ip_dst))
580 icmpdst.sin_addr = ip->ip_src;
581 else
582 icmpdst.sin_addr = ip->ip_dst;
583 ia = ifatoia(ifaof_ifpforaddr(sintosa(&icmpdst),
584 m->m_pkthdr.rcvif));
585 if (ia == 0)
586 break;
587 icp->icmp_type = ICMP_MASKREPLY;
588 icp->icmp_mask = ia->ia_sockmask.sin_addr.s_addr;
589 if (in_nullhost(ip->ip_src)) {
590 if (ia->ia_ifp->if_flags & IFF_BROADCAST)
591 ip->ip_src = ia->ia_broadaddr.sin_addr;
592 else if (ia->ia_ifp->if_flags & IFF_POINTOPOINT)
593 ip->ip_src = ia->ia_dstaddr.sin_addr;
594 }
595 reflect:
596 {
597 uint64_t *icps = percpu_getref(icmpstat_percpu);
598 icps[ICMP_STAT_REFLECT]++;
599 icps[ICMP_STAT_OUTHIST + icp->icmp_type]++;
600 percpu_putref(icmpstat_percpu);
601 }
602 icmp_reflect(m);
603 return;
604
605 case ICMP_REDIRECT:
606 if (code > 3)
607 goto badcode;
608 if (icmp_rediraccept == 0)
609 goto freeit;
610 if (icmplen < ICMP_ADVLENMIN || icmplen < ICMP_ADVLEN(icp) ||
611 icp->icmp_ip.ip_hl < (sizeof(struct ip) >> 2)) {
612 ICMP_STATINC(ICMP_STAT_BADLEN);
613 break;
614 }
615 /*
616 * Short circuit routing redirects to force
617 * immediate change in the kernel's routing
618 * tables. The message is also handed to anyone
619 * listening on a raw socket (e.g. the routing
620 * daemon for use in updating its tables).
621 */
622 icmpgw.sin_addr = ip->ip_src;
623 icmpdst.sin_addr = icp->icmp_gwaddr;
624 #ifdef ICMPPRINTFS
625 if (icmpprintfs) {
626 printf("redirect dst `%s' to `%s'\n",
627 inet_ntoa(icp->icmp_ip.ip_dst),
628 inet_ntoa(icp->icmp_gwaddr));
629 }
630 #endif
631 icmpsrc.sin_addr = icp->icmp_ip.ip_dst;
632 rt = NULL;
633 rtredirect(sintosa(&icmpsrc), sintosa(&icmpdst),
634 NULL, RTF_GATEWAY | RTF_HOST, sintosa(&icmpgw), &rt);
635 if (rt != NULL && icmp_redirtimeout != 0) {
636 i = rt_timer_add(rt, icmp_redirect_timeout,
637 icmp_redirect_timeout_q);
638 if (i)
639 log(LOG_ERR, "ICMP: redirect failed to "
640 "register timeout for route to %x, "
641 "code %d\n",
642 icp->icmp_ip.ip_dst.s_addr, i);
643 }
644 if (rt != NULL)
645 rtfree(rt);
646
647 pfctlinput(PRC_REDIRECT_HOST, sintosa(&icmpsrc));
648 #if defined(IPSEC) || defined(FAST_IPSEC)
649 key_sa_routechange((struct sockaddr *)&icmpsrc);
650 #endif
651 break;
652
653 /*
654 * No kernel processing for the following;
655 * just fall through to send to raw listener.
656 */
657 case ICMP_ECHOREPLY:
658 case ICMP_ROUTERADVERT:
659 case ICMP_ROUTERSOLICIT:
660 case ICMP_TSTAMPREPLY:
661 case ICMP_IREQREPLY:
662 case ICMP_MASKREPLY:
663 default:
664 break;
665 }
666
667 raw:
668 rip_input(m, hlen, proto);
669 return;
670
671 freeit:
672 m_freem(m);
673 return;
674 }
675
676 /*
677 * Reflect the ip packet back to the source
678 */
679 void
680 icmp_reflect(struct mbuf *m)
681 {
682 struct ip *ip = mtod(m, struct ip *);
683 struct in_ifaddr *ia;
684 struct ifaddr *ifa;
685 struct sockaddr_in *sin = 0;
686 struct in_addr t;
687 struct mbuf *opts = 0;
688 int optlen = (ip->ip_hl << 2) - sizeof(struct ip);
689
690 if (!in_canforward(ip->ip_src) &&
691 ((ip->ip_src.s_addr & IN_CLASSA_NET) !=
692 htonl(IN_LOOPBACKNET << IN_CLASSA_NSHIFT))) {
693 m_freem(m); /* Bad return address */
694 goto done; /* ip_output() will check for broadcast */
695 }
696 t = ip->ip_dst;
697 ip->ip_dst = ip->ip_src;
698 /*
699 * If the incoming packet was addressed directly to us, use
700 * dst as the src for the reply. Otherwise (broadcast or
701 * anonymous), use an address which corresponds to the
702 * incoming interface, with a preference for the address which
703 * corresponds to the route to the destination of the ICMP.
704 */
705
706 /* Look for packet addressed to us */
707 INADDR_TO_IA(t, ia);
708
709 /* look for packet sent to broadcast address */
710 if (ia == NULL && m->m_pkthdr.rcvif &&
711 (m->m_pkthdr.rcvif->if_flags & IFF_BROADCAST)) {
712 IFADDR_FOREACH(ifa, m->m_pkthdr.rcvif) {
713 if (ifa->ifa_addr->sa_family != AF_INET)
714 continue;
715 if (in_hosteq(t,ifatoia(ifa)->ia_broadaddr.sin_addr)) {
716 ia = ifatoia(ifa);
717 break;
718 }
719 }
720 }
721
722 if (ia)
723 sin = &ia->ia_addr;
724
725 icmpdst.sin_addr = t;
726
727 /*
728 * if the packet is addressed somewhere else, compute the
729 * source address for packets routed back to the source, and
730 * use that, if it's an address on the interface which
731 * received the packet
732 */
733 if (sin == NULL && m->m_pkthdr.rcvif) {
734 struct sockaddr_in sin_dst;
735 struct route icmproute;
736 int errornum;
737
738 sockaddr_in_init(&sin_dst, &ip->ip_dst, 0);
739 memset(&icmproute, 0, sizeof(icmproute));
740 errornum = 0;
741 sin = in_selectsrc(&sin_dst, &icmproute, 0, NULL, &errornum);
742 /* errornum is never used */
743 rtcache_free(&icmproute);
744 /* check to make sure sin is a source address on rcvif */
745 if (sin) {
746 t = sin->sin_addr;
747 sin = NULL;
748 INADDR_TO_IA(t, ia);
749 while (ia) {
750 if (ia->ia_ifp == m->m_pkthdr.rcvif) {
751 sin = &ia->ia_addr;
752 break;
753 }
754 NEXT_IA_WITH_SAME_ADDR(ia);
755 }
756 }
757 }
758
759 /*
760 * if it was not addressed to us, but the route doesn't go out
761 * the source interface, pick an address on the source
762 * interface. This can happen when routing is asymmetric, or
763 * when the incoming packet was encapsulated
764 */
765 if (sin == NULL && m->m_pkthdr.rcvif) {
766 IFADDR_FOREACH(ifa, m->m_pkthdr.rcvif) {
767 if (ifa->ifa_addr->sa_family != AF_INET)
768 continue;
769 sin = &(ifatoia(ifa)->ia_addr);
770 break;
771 }
772 }
773
774 /*
775 * The following happens if the packet was not addressed to us,
776 * and was received on an interface with no IP address:
777 * We find the first AF_INET address on the first non-loopback
778 * interface.
779 */
780 if (sin == NULL)
781 TAILQ_FOREACH(ia, &in_ifaddrhead, ia_list) {
782 if (ia->ia_ifp->if_flags & IFF_LOOPBACK)
783 continue;
784 sin = &ia->ia_addr;
785 break;
786 }
787
788 /*
789 * If we still didn't find an address, punt. We could have an
790 * interface up (and receiving packets) with no address.
791 */
792 if (sin == NULL) {
793 m_freem(m);
794 goto done;
795 }
796
797 ip->ip_src = sin->sin_addr;
798 ip->ip_ttl = MAXTTL;
799
800 if (optlen > 0) {
801 u_char *cp;
802 int opt, cnt;
803 u_int len;
804
805 /*
806 * Retrieve any source routing from the incoming packet;
807 * add on any record-route or timestamp options.
808 */
809 cp = (u_char *) (ip + 1);
810 if ((opts = ip_srcroute()) == 0 &&
811 (opts = m_gethdr(M_DONTWAIT, MT_HEADER))) {
812 MCLAIM(opts, m->m_owner);
813 opts->m_len = sizeof(struct in_addr);
814 *mtod(opts, struct in_addr *) = zeroin_addr;
815 }
816 if (opts) {
817 #ifdef ICMPPRINTFS
818 if (icmpprintfs)
819 printf("icmp_reflect optlen %d rt %d => ",
820 optlen, opts->m_len);
821 #endif
822 for (cnt = optlen; cnt > 0; cnt -= len, cp += len) {
823 opt = cp[IPOPT_OPTVAL];
824 if (opt == IPOPT_EOL)
825 break;
826 if (opt == IPOPT_NOP)
827 len = 1;
828 else {
829 if (cnt < IPOPT_OLEN + sizeof(*cp))
830 break;
831 len = cp[IPOPT_OLEN];
832 if (len < IPOPT_OLEN + sizeof(*cp) ||
833 len > cnt)
834 break;
835 }
836 /*
837 * Should check for overflow, but it "can't happen"
838 */
839 if (opt == IPOPT_RR || opt == IPOPT_TS ||
840 opt == IPOPT_SECURITY) {
841 memmove(mtod(opts, char *) + opts->m_len,
842 cp, len);
843 opts->m_len += len;
844 }
845 }
846 /* Terminate & pad, if necessary */
847 if ((cnt = opts->m_len % 4) != 0) {
848 for (; cnt < 4; cnt++) {
849 *(mtod(opts, char *) + opts->m_len) =
850 IPOPT_EOL;
851 opts->m_len++;
852 }
853 }
854 #ifdef ICMPPRINTFS
855 if (icmpprintfs)
856 printf("%d\n", opts->m_len);
857 #endif
858 }
859 /*
860 * Now strip out original options by copying rest of first
861 * mbuf's data back, and adjust the IP length.
862 */
863 ip->ip_len = htons(ntohs(ip->ip_len) - optlen);
864 ip->ip_hl = sizeof(struct ip) >> 2;
865 m->m_len -= optlen;
866 if (m->m_flags & M_PKTHDR)
867 m->m_pkthdr.len -= optlen;
868 optlen += sizeof(struct ip);
869 memmove(ip + 1, (char *)ip + optlen,
870 (unsigned)(m->m_len - sizeof(struct ip)));
871 }
872 m_tag_delete_nonpersistent(m);
873 m->m_flags &= ~(M_BCAST|M_MCAST);
874
875 /*
876 * Clear any in-bound checksum flags for this packet.
877 */
878 if (m->m_flags & M_PKTHDR)
879 m->m_pkthdr.csum_flags = 0;
880
881 icmp_send(m, opts);
882 done:
883 if (opts)
884 (void)m_free(opts);
885 }
886
887 /*
888 * Send an icmp packet back to the ip level,
889 * after supplying a checksum.
890 */
891 void
892 icmp_send(struct mbuf *m, struct mbuf *opts)
893 {
894 struct ip *ip = mtod(m, struct ip *);
895 int hlen;
896 struct icmp *icp;
897
898 hlen = ip->ip_hl << 2;
899 m->m_data += hlen;
900 m->m_len -= hlen;
901 icp = mtod(m, struct icmp *);
902 icp->icmp_cksum = 0;
903 icp->icmp_cksum = in_cksum(m, ntohs(ip->ip_len) - hlen);
904 m->m_data -= hlen;
905 m->m_len += hlen;
906 #ifdef ICMPPRINTFS
907 if (icmpprintfs) {
908 printf("icmp_send to destination `%s' from `%s'\n",
909 inet_ntoa(ip->ip_dst), inet_ntoa(ip->ip_src));
910 }
911 #endif
912 (void)ip_output(m, opts, NULL, 0, NULL, NULL);
913 }
914
915 n_time
916 iptime(void)
917 {
918 struct timeval atv;
919 u_long t;
920
921 microtime(&atv);
922 t = (atv.tv_sec % (24*60*60)) * 1000 + atv.tv_usec / 1000;
923 return (htonl(t));
924 }
925
926 /*
927 * sysctl helper routine for net.inet.icmp.returndatabytes. ensures
928 * that the new value is in the correct range.
929 */
930 static int
931 sysctl_net_inet_icmp_returndatabytes(SYSCTLFN_ARGS)
932 {
933 int error, t;
934 struct sysctlnode node;
935
936 node = *rnode;
937 node.sysctl_data = &t;
938 t = icmpreturndatabytes;
939 error = sysctl_lookup(SYSCTLFN_CALL(&node));
940 if (error || newp == NULL)
941 return (error);
942
943 if (t < 8 || t > 512)
944 return (EINVAL);
945 icmpreturndatabytes = t;
946
947 return (0);
948 }
949
950 /*
951 * sysctl helper routine for net.inet.icmp.redirtimeout. ensures that
952 * the given value is not less than zero and then resets the timeout
953 * queue.
954 */
955 static int
956 sysctl_net_inet_icmp_redirtimeout(SYSCTLFN_ARGS)
957 {
958 int error, tmp;
959 struct sysctlnode node;
960
961 node = *rnode;
962 node.sysctl_data = &tmp;
963 tmp = icmp_redirtimeout;
964 error = sysctl_lookup(SYSCTLFN_CALL(&node));
965 if (error || newp == NULL)
966 return (error);
967 if (tmp < 0)
968 return (EINVAL);
969 icmp_redirtimeout = tmp;
970
971 /*
972 * was it a *defined* side-effect that anyone even *reading*
973 * this value causes these things to happen?
974 */
975 if (icmp_redirect_timeout_q != NULL) {
976 if (icmp_redirtimeout == 0) {
977 rt_timer_queue_destroy(icmp_redirect_timeout_q,
978 true);
979 icmp_redirect_timeout_q = NULL;
980 } else {
981 rt_timer_queue_change(icmp_redirect_timeout_q,
982 icmp_redirtimeout);
983 }
984 } else if (icmp_redirtimeout > 0) {
985 icmp_redirect_timeout_q =
986 rt_timer_queue_create(icmp_redirtimeout);
987 }
988
989 return (0);
990 }
991
992 static int
993 sysctl_net_inet_icmp_stats(SYSCTLFN_ARGS)
994 {
995
996 return (NETSTAT_SYSCTL(icmpstat_percpu, ICMP_NSTATS));
997 }
998
999 static void
1000 sysctl_netinet_icmp_setup(struct sysctllog **clog)
1001 {
1002
1003 sysctl_createv(clog, 0, NULL, NULL,
1004 CTLFLAG_PERMANENT,
1005 CTLTYPE_NODE, "net", NULL,
1006 NULL, 0, NULL, 0,
1007 CTL_NET, CTL_EOL);
1008 sysctl_createv(clog, 0, NULL, NULL,
1009 CTLFLAG_PERMANENT,
1010 CTLTYPE_NODE, "inet", NULL,
1011 NULL, 0, NULL, 0,
1012 CTL_NET, PF_INET, CTL_EOL);
1013 sysctl_createv(clog, 0, NULL, NULL,
1014 CTLFLAG_PERMANENT,
1015 CTLTYPE_NODE, "icmp",
1016 SYSCTL_DESCR("ICMPv4 related settings"),
1017 NULL, 0, NULL, 0,
1018 CTL_NET, PF_INET, IPPROTO_ICMP, CTL_EOL);
1019
1020 sysctl_createv(clog, 0, NULL, NULL,
1021 CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
1022 CTLTYPE_INT, "maskrepl",
1023 SYSCTL_DESCR("Respond to ICMP_MASKREQ messages"),
1024 NULL, 0, &icmpmaskrepl, 0,
1025 CTL_NET, PF_INET, IPPROTO_ICMP,
1026 ICMPCTL_MASKREPL, CTL_EOL);
1027 sysctl_createv(clog, 0, NULL, NULL,
1028 CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
1029 CTLTYPE_INT, "returndatabytes",
1030 SYSCTL_DESCR("Number of bytes to return in an ICMP "
1031 "error message"),
1032 sysctl_net_inet_icmp_returndatabytes, 0,
1033 &icmpreturndatabytes, 0,
1034 CTL_NET, PF_INET, IPPROTO_ICMP,
1035 ICMPCTL_RETURNDATABYTES, CTL_EOL);
1036 sysctl_createv(clog, 0, NULL, NULL,
1037 CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
1038 CTLTYPE_INT, "errppslimit",
1039 SYSCTL_DESCR("Maximum number of outgoing ICMP error "
1040 "messages per second"),
1041 NULL, 0, &icmperrppslim, 0,
1042 CTL_NET, PF_INET, IPPROTO_ICMP,
1043 ICMPCTL_ERRPPSLIMIT, CTL_EOL);
1044 sysctl_createv(clog, 0, NULL, NULL,
1045 CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
1046 CTLTYPE_INT, "rediraccept",
1047 SYSCTL_DESCR("Accept ICMP_REDIRECT messages"),
1048 NULL, 0, &icmp_rediraccept, 0,
1049 CTL_NET, PF_INET, IPPROTO_ICMP,
1050 ICMPCTL_REDIRACCEPT, CTL_EOL);
1051 sysctl_createv(clog, 0, NULL, NULL,
1052 CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
1053 CTLTYPE_INT, "redirtimeout",
1054 SYSCTL_DESCR("Lifetime of ICMP_REDIRECT generated "
1055 "routes"),
1056 sysctl_net_inet_icmp_redirtimeout, 0,
1057 &icmp_redirtimeout, 0,
1058 CTL_NET, PF_INET, IPPROTO_ICMP,
1059 ICMPCTL_REDIRTIMEOUT, CTL_EOL);
1060 sysctl_createv(clog, 0, NULL, NULL,
1061 CTLFLAG_PERMANENT,
1062 CTLTYPE_STRUCT, "stats",
1063 SYSCTL_DESCR("ICMP statistics"),
1064 sysctl_net_inet_icmp_stats, 0, NULL, 0,
1065 CTL_NET, PF_INET, IPPROTO_ICMP, ICMPCTL_STATS,
1066 CTL_EOL);
1067 sysctl_createv(clog, 0, NULL, NULL,
1068 CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
1069 CTLTYPE_INT, "bmcastecho",
1070 SYSCTL_DESCR("Respond to ICMP_ECHO or ICMP_TIMESTAMP "
1071 "message to the broadcast or multicast"),
1072 NULL, 0, &icmpbmcastecho, 0,
1073 CTL_NET, PF_INET, IPPROTO_ICMP, ICMPCTL_BMCASTECHO,
1074 CTL_EOL);
1075 }
1076
1077 void
1078 icmp_statinc(u_int stat)
1079 {
1080
1081 KASSERT(stat < ICMP_NSTATS);
1082 ICMP_STATINC(stat);
1083 }
1084
1085 /* Table of common MTUs: */
1086
1087 static const u_int mtu_table[] = {
1088 65535, 65280, 32000, 17914, 9180, 8166,
1089 4352, 2002, 1492, 1006, 508, 296, 68, 0
1090 };
1091
1092 void
1093 icmp_mtudisc(struct icmp *icp, struct in_addr faddr)
1094 {
1095 struct icmp_mtudisc_callback *mc;
1096 struct sockaddr *dst = sintosa(&icmpsrc);
1097 struct rtentry *rt;
1098 u_long mtu = ntohs(icp->icmp_nextmtu); /* Why a long? IPv6 */
1099 int error;
1100
1101 rt = rtalloc1(dst, 1);
1102 if (rt == 0)
1103 return;
1104
1105 /* If we didn't get a host route, allocate one */
1106
1107 if ((rt->rt_flags & RTF_HOST) == 0) {
1108 struct rtentry *nrt;
1109
1110 error = rtrequest((int) RTM_ADD, dst,
1111 (struct sockaddr *) rt->rt_gateway,
1112 (struct sockaddr *) 0,
1113 RTF_GATEWAY | RTF_HOST | RTF_DYNAMIC, &nrt);
1114 if (error) {
1115 rtfree(rt);
1116 return;
1117 }
1118 nrt->rt_rmx = rt->rt_rmx;
1119 rtfree(rt);
1120 rt = nrt;
1121 }
1122 error = rt_timer_add(rt, icmp_mtudisc_timeout, ip_mtudisc_timeout_q);
1123 if (error) {
1124 rtfree(rt);
1125 return;
1126 }
1127
1128 if (mtu == 0) {
1129 int i = 0;
1130
1131 mtu = ntohs(icp->icmp_ip.ip_len);
1132 /* Some 4.2BSD-based routers incorrectly adjust the ip_len */
1133 if (mtu > rt->rt_rmx.rmx_mtu && rt->rt_rmx.rmx_mtu != 0)
1134 mtu -= (icp->icmp_ip.ip_hl << 2);
1135
1136 /* If we still can't guess a value, try the route */
1137
1138 if (mtu == 0) {
1139 mtu = rt->rt_rmx.rmx_mtu;
1140
1141 /* If no route mtu, default to the interface mtu */
1142
1143 if (mtu == 0)
1144 mtu = rt->rt_ifp->if_mtu;
1145 }
1146
1147 for (i = 0; i < sizeof(mtu_table) / sizeof(mtu_table[0]); i++)
1148 if (mtu > mtu_table[i]) {
1149 mtu = mtu_table[i];
1150 break;
1151 }
1152 }
1153
1154 /*
1155 * XXX: RTV_MTU is overloaded, since the admin can set it
1156 * to turn off PMTU for a route, and the kernel can
1157 * set it to indicate a serious problem with PMTU
1158 * on a route. We should be using a separate flag
1159 * for the kernel to indicate this.
1160 */
1161
1162 if ((rt->rt_rmx.rmx_locks & RTV_MTU) == 0) {
1163 if (mtu < 296 || mtu > rt->rt_ifp->if_mtu)
1164 rt->rt_rmx.rmx_locks |= RTV_MTU;
1165 else if (rt->rt_rmx.rmx_mtu > mtu ||
1166 rt->rt_rmx.rmx_mtu == 0) {
1167 ICMP_STATINC(ICMP_STAT_PMTUCHG);
1168 rt->rt_rmx.rmx_mtu = mtu;
1169 }
1170 }
1171
1172 if (rt)
1173 rtfree(rt);
1174
1175 /*
1176 * Notify protocols that the MTU for this destination
1177 * has changed.
1178 */
1179 for (mc = LIST_FIRST(&icmp_mtudisc_callbacks); mc != NULL;
1180 mc = LIST_NEXT(mc, mc_list))
1181 (*mc->mc_func)(faddr);
1182 }
1183
1184 /*
1185 * Return the next larger or smaller MTU plateau (table from RFC 1191)
1186 * given current value MTU. If DIR is less than zero, a larger plateau
1187 * is returned; otherwise, a smaller value is returned.
1188 */
1189 u_int
1190 ip_next_mtu(u_int mtu, int dir) /* XXX */
1191 {
1192 int i;
1193
1194 for (i = 0; i < (sizeof mtu_table) / (sizeof mtu_table[0]); i++) {
1195 if (mtu >= mtu_table[i])
1196 break;
1197 }
1198
1199 if (dir < 0) {
1200 if (i == 0) {
1201 return 0;
1202 } else {
1203 return mtu_table[i - 1];
1204 }
1205 } else {
1206 if (mtu_table[i] == 0) {
1207 return 0;
1208 } else if (mtu > mtu_table[i]) {
1209 return mtu_table[i];
1210 } else {
1211 return mtu_table[i + 1];
1212 }
1213 }
1214 }
1215
1216 static void
1217 icmp_mtudisc_timeout(struct rtentry *rt, struct rttimer *r)
1218 {
1219 if (rt == NULL)
1220 panic("icmp_mtudisc_timeout: bad route to timeout");
1221 if ((rt->rt_flags & (RTF_DYNAMIC | RTF_HOST)) ==
1222 (RTF_DYNAMIC | RTF_HOST)) {
1223 rtrequest((int) RTM_DELETE, rt_getkey(rt),
1224 rt->rt_gateway, rt_mask(rt), rt->rt_flags, 0);
1225 } else {
1226 if ((rt->rt_rmx.rmx_locks & RTV_MTU) == 0) {
1227 rt->rt_rmx.rmx_mtu = 0;
1228 }
1229 }
1230 }
1231
1232 static void
1233 icmp_redirect_timeout(struct rtentry *rt, struct rttimer *r)
1234 {
1235 if (rt == NULL)
1236 panic("icmp_redirect_timeout: bad route to timeout");
1237 if ((rt->rt_flags & (RTF_DYNAMIC | RTF_HOST)) ==
1238 (RTF_DYNAMIC | RTF_HOST)) {
1239 rtrequest((int) RTM_DELETE, rt_getkey(rt),
1240 rt->rt_gateway, rt_mask(rt), rt->rt_flags, 0);
1241 }
1242 }
1243
1244 /*
1245 * Perform rate limit check.
1246 * Returns 0 if it is okay to send the icmp packet.
1247 * Returns 1 if the router SHOULD NOT send this icmp packet due to rate
1248 * limitation.
1249 *
1250 * XXX per-destination/type check necessary?
1251 */
1252 int
1253 icmp_ratelimit(const struct in_addr *dst, const int type,
1254 const int code)
1255 {
1256
1257 /* PPS limit */
1258 if (!ppsratecheck(&icmperrppslim_last, &icmperrpps_count,
1259 icmperrppslim)) {
1260 /* The packet is subject to rate limit */
1261 return 1;
1262 }
1263
1264 /* okay to send */
1265 return 0;
1266 }
1267