[ source navigation ] [ diff markup ] [ identifier search ] [ freetext search ] [ file search ]

FreeBSD/Linux Kernel Cross Reference
sys/netinet/ip_icmp.c

Version: -  FREEBSD  -  FREEBSD7  -  FREEBSD70  -  FREEBSD6  -  FREEBSD63  -  FREEBSD62  -  FREEBSD61  -  FREEBSD60  -  FREEBSD5  -  FREEBSD55  -  FREEBSD54  -  FREEBSD53  -  FREEBSD52  -  FREEBSD51  -  FREEBSD50  -  FREEBSD4  -  FREEBSD3  -  FREEBSD22  -  linux-2.6  -  linux-2.4.22  -  MK83  -  MK84  -  PLAN9  -  DFBSD  -  NETBSD  -  NETBSD4  -  NETBSD3  -  NETBSD20  -  OPENBSD  -  xnu-517  -  xnu-792  -  xnu-792.6.70  -  xnu-1228  -  OPENSOLARIS  -  minix-3-1-1  -  TRUSTEDBSD-SEBSD  -  FREEBSD-LIBC  -  FREEBSD7-LIBC  -  FREEBSD6-LIBC  -  GLIBC27 
SearchContext: -  none  -  excerpts  -  bigexcerpts 

  1 /*-
  2  * Copyright (c) 1982, 1986, 1988, 1993
  3  *      The Regents of the University of California.  All rights reserved.
  4  *
  5  * Redistribution and use in source and binary forms, with or without
  6  * modification, are permitted provided that the following conditions
  7  * are met:
  8  * 1. Redistributions of source code must retain the above copyright
  9  *    notice, this list of conditions and the following disclaimer.
 10  * 2. Redistributions in binary form must reproduce the above copyright
 11  *    notice, this list of conditions and the following disclaimer in the
 12  *    documentation and/or other materials provided with the distribution.
 13  * 4. Neither the name of the University nor the names of its contributors
 14  *    may be used to endorse or promote products derived from this software
 15  *    without specific prior written permission.
 16  *
 17  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
 18  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 20  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
 21  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 22  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
 23  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 24  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 25  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 26  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 27  * SUCH DAMAGE.
 28  *
 29  *      @(#)ip_icmp.c   8.2 (Berkeley) 1/4/94
 30  */
 31 
 32 #include <sys/cdefs.h>
 33 __FBSDID("$FreeBSD: src/sys/netinet/ip_icmp.c,v 1.126 2008/10/02 15:37:58 zec Exp $");
 34 
 35 #include "opt_ipsec.h"
 36 #include "opt_mac.h"
 37 
 38 #include <sys/param.h>
 39 #include <sys/systm.h>
 40 #include <sys/mbuf.h>
 41 #include <sys/protosw.h>
 42 #include <sys/socket.h>
 43 #include <sys/time.h>
 44 #include <sys/kernel.h>
 45 #include <sys/sysctl.h>
 46 #include <sys/vimage.h>
 47 
 48 #include <net/if.h>
 49 #include <net/if_types.h>
 50 #include <net/route.h>
 51 
 52 #include <netinet/in.h>
 53 #include <netinet/in_pcb.h>
 54 #include <netinet/in_systm.h>
 55 #include <netinet/in_var.h>
 56 #include <netinet/ip.h>
 57 #include <netinet/ip_icmp.h>
 58 #include <netinet/ip_var.h>
 59 #include <netinet/ip_options.h>
 60 #include <netinet/tcp.h>
 61 #include <netinet/tcp_var.h>
 62 #include <netinet/tcpip.h>
 63 #include <netinet/icmp_var.h>
 64 
 65 #ifdef IPSEC
 66 #include <netipsec/ipsec.h>
 67 #include <netipsec/key.h>
 68 #endif
 69 
 70 #include <machine/in_cksum.h>
 71 
 72 #include <security/mac/mac_framework.h>
 73 
 74 /*
 75  * ICMP routines: error generation, receive packet processing, and
 76  * routines to turnaround packets back to the originator, and
 77  * host table maintenance routines.
 78  */
 79 
 80 struct  icmpstat icmpstat;
 81 SYSCTL_V_STRUCT(V_NET, vnet_inet, _net_inet_icmp, ICMPCTL_STATS, stats,
 82         CTLFLAG_RW, icmpstat, icmpstat, "");
 83 
 84 static int      icmpmaskrepl = 0;
 85 SYSCTL_V_INT(V_NET, vnet_inet, _net_inet_icmp, ICMPCTL_MASKREPL, maskrepl,
 86         CTLFLAG_RW, icmpmaskrepl, 0,
 87         "Reply to ICMP Address Mask Request packets.");
 88 
 89 static u_int    icmpmaskfake = 0;
 90 SYSCTL_V_UINT(V_NET, vnet_inet, _net_inet_icmp, OID_AUTO, maskfake, CTLFLAG_RW,
 91         icmpmaskfake, 0, "Fake reply to ICMP Address Mask Request packets.");
 92 
 93 static int      drop_redirect = 0;
 94 SYSCTL_V_INT(V_NET, vnet_inet, _net_inet_icmp, OID_AUTO, drop_redirect,
 95         CTLFLAG_RW, drop_redirect, 0, "Ignore ICMP redirects");
 96 
 97 static int      log_redirect = 0;
 98 SYSCTL_V_INT(V_NET, vnet_inet, _net_inet_icmp, OID_AUTO, log_redirect,
 99         CTLFLAG_RW, log_redirect, 0, "Log ICMP redirects to the console");
100 
101 static int      icmplim = 200;
102 SYSCTL_V_INT(V_NET, vnet_inet, _net_inet_icmp, ICMPCTL_ICMPLIM, icmplim,
103         CTLFLAG_RW, icmplim, 0, "Maximum number of ICMP responses per second");
104 
105 static int      icmplim_output = 1;
106 SYSCTL_V_INT(V_NET, vnet_inet, _net_inet_icmp, OID_AUTO, icmplim_output,
107         CTLFLAG_RW, icmplim_output, 0,
108         "Enable rate limiting of ICMP responses");
109 
110 static char     reply_src[IFNAMSIZ];
111 SYSCTL_V_STRING(V_NET, vnet_inet, _net_inet_icmp, OID_AUTO, reply_src,
112         CTLFLAG_RW, reply_src, IFNAMSIZ,
113         "icmp reply source for non-local packets.");
114 
115 static int      icmp_rfi = 0;
116 SYSCTL_V_INT(V_NET, vnet_inet, _net_inet_icmp, OID_AUTO, reply_from_interface,
117         CTLFLAG_RW, icmp_rfi, 0, "ICMP reply from incoming interface for "
118         "non-local packets");
119 
120 static int      icmp_quotelen = 8;
121 SYSCTL_V_INT(V_NET, vnet_inet, _net_inet_icmp, OID_AUTO, quotelen, CTLFLAG_RW,
122         icmp_quotelen, 0, "Number of bytes from original packet to "
123         "quote in ICMP reply");
124 
125 /*
126  * ICMP broadcast echo sysctl
127  */
128 
129 static int      icmpbmcastecho = 0;
130 SYSCTL_V_INT(V_NET, vnet_inet, _net_inet_icmp, OID_AUTO, bmcastecho,
131         CTLFLAG_RW, icmpbmcastecho, 0, "");
132 
133 
134 #ifdef ICMPPRINTFS
135 int     icmpprintfs = 0;
136 #endif
137 
138 static void     icmp_reflect(struct mbuf *);
139 static void     icmp_send(struct mbuf *, struct mbuf *);
140 
141 extern  struct protosw inetsw[];
142 
143 /*
144  * Generate an error packet of type error
145  * in response to bad packet ip.
146  */
147 void
148 icmp_error(struct mbuf *n, int type, int code, n_long dest, int mtu)
149 {
150         INIT_VNET_INET(curvnet);
151         register struct ip *oip = mtod(n, struct ip *), *nip;
152         register unsigned oiphlen = oip->ip_hl << 2;
153         register struct icmp *icp;
154         register struct mbuf *m;
155         unsigned icmplen, icmpelen, nlen;
156 
157         KASSERT((u_int)type <= ICMP_MAXTYPE, ("%s: illegal ICMP type", __func__));
158 #ifdef ICMPPRINTFS
159         if (icmpprintfs)
160                 printf("icmp_error(%p, %x, %d)\n", oip, type, code);
161 #endif
162         if (type != ICMP_REDIRECT)
163                 V_icmpstat.icps_error++;
164         /*
165          * Don't send error:
166          *  if the original packet was encrypted.
167          *  if not the first fragment of message.
168          *  in response to a multicast or broadcast packet.
169          *  if the old packet protocol was an ICMP error message.
170          */
171         if (n->m_flags & M_DECRYPTED)
172                 goto freeit;
173         if (oip->ip_off & ~(IP_MF|IP_DF))
174                 goto freeit;
175         if (n->m_flags & (M_BCAST|M_MCAST))
176                 goto freeit;
177         if (oip->ip_p == IPPROTO_ICMP && type != ICMP_REDIRECT &&
178           n->m_len >= oiphlen + ICMP_MINLEN &&
179           !ICMP_INFOTYPE(((struct icmp *)((caddr_t)oip + oiphlen))->icmp_type)) {
180                 V_icmpstat.icps_oldicmp++;
181                 goto freeit;
182         }
183         /* Drop if IP header plus 8 bytes is not contignous in first mbuf. */
184         if (oiphlen + 8 > n->m_len)
185                 goto freeit;
186         /*
187          * Calculate length to quote from original packet and
188          * prevent the ICMP mbuf from overflowing.
189          * Unfortunatly this is non-trivial since ip_forward()
190          * sends us truncated packets.
191          */
192         nlen = m_length(n, NULL);
193         if (oip->ip_p == IPPROTO_TCP) {
194                 struct tcphdr *th;
195                 int tcphlen;
196 
197                 if (oiphlen + sizeof(struct tcphdr) > n->m_len &&
198                     n->m_next == NULL)
199                         goto stdreply;
200                 if (n->m_len < oiphlen + sizeof(struct tcphdr) &&
201                     ((n = m_pullup(n, oiphlen + sizeof(struct tcphdr))) == NULL))
202                         goto freeit;
203                 th = (struct tcphdr *)((caddr_t)oip + oiphlen);
204                 tcphlen = th->th_off << 2;
205                 if (tcphlen < sizeof(struct tcphdr))
206                         goto freeit;
207                 if (oip->ip_len < oiphlen + tcphlen)
208                         goto freeit;
209                 if (oiphlen + tcphlen > n->m_len && n->m_next == NULL)
210                         goto stdreply;
211                 if (n->m_len < oiphlen + tcphlen && 
212                     ((n = m_pullup(n, oiphlen + tcphlen)) == NULL))
213                         goto freeit;
214                 icmpelen = max(tcphlen, min(V_icmp_quotelen, oip->ip_len - oiphlen));
215         } else
216 stdreply:       icmpelen = max(8, min(V_icmp_quotelen, oip->ip_len - oiphlen));
217 
218         icmplen = min(oiphlen + icmpelen, nlen);
219         if (icmplen < sizeof(struct ip))
220                 goto freeit;
221 
222         if (MHLEN > sizeof(struct ip) + ICMP_MINLEN + icmplen)
223                 m = m_gethdr(M_DONTWAIT, MT_DATA);
224         else
225                 m = m_getcl(M_DONTWAIT, MT_DATA, M_PKTHDR);
226         if (m == NULL)
227                 goto freeit;
228 #ifdef MAC
229         mac_netinet_icmp_reply(n, m);
230 #endif
231         icmplen = min(icmplen, M_TRAILINGSPACE(m) - sizeof(struct ip) - ICMP_MINLEN);
232         m_align(m, ICMP_MINLEN + icmplen);
233         m->m_len = ICMP_MINLEN + icmplen;
234 
235         /* XXX MRT  make the outgoing packet use the same FIB
236          * that was associated with the incoming packet
237          */
238         M_SETFIB(m, M_GETFIB(n));
239         icp = mtod(m, struct icmp *);
240         V_icmpstat.icps_outhist[type]++;
241         icp->icmp_type = type;
242         if (type == ICMP_REDIRECT)
243                 icp->icmp_gwaddr.s_addr = dest;
244         else {
245                 icp->icmp_void = 0;
246                 /*
247                  * The following assignments assume an overlay with the
248                  * just zeroed icmp_void field.
249                  */
250                 if (type == ICMP_PARAMPROB) {
251                         icp->icmp_pptr = code;
252                         code = 0;
253                 } else if (type == ICMP_UNREACH &&
254                         code == ICMP_UNREACH_NEEDFRAG && mtu) {
255                         icp->icmp_nextmtu = htons(mtu);
256                 }
257         }
258         icp->icmp_code = code;
259 
260         /*
261          * Copy the quotation into ICMP message and
262          * convert quoted IP header back to network representation.
263          */
264         m_copydata(n, 0, icmplen, (caddr_t)&icp->icmp_ip);
265         nip = &icp->icmp_ip;
266         nip->ip_len = htons(nip->ip_len);
267         nip->ip_off = htons(nip->ip_off);
268 
269         /*
270          * Set up ICMP message mbuf and copy old IP header (without options
271          * in front of ICMP message.
272          * If the original mbuf was meant to bypass the firewall, the error
273          * reply should bypass as well.
274          */
275         m->m_flags |= n->m_flags & M_SKIP_FIREWALL;
276         m->m_data -= sizeof(struct ip);
277         m->m_len += sizeof(struct ip);
278         m->m_pkthdr.len = m->m_len;
279         m->m_pkthdr.rcvif = n->m_pkthdr.rcvif;
280         nip = mtod(m, struct ip *);
281         bcopy((caddr_t)oip, (caddr_t)nip, sizeof(struct ip));
282         nip->ip_len = m->m_len;
283         nip->ip_v = IPVERSION;
284         nip->ip_hl = 5;
285         nip->ip_p = IPPROTO_ICMP;
286         nip->ip_tos = 0;
287         icmp_reflect(m);
288 
289 freeit:
290         m_freem(n);
291 }
292 
293 /*
294  * Process a received ICMP message.
295  */
296 void
297 icmp_input(struct mbuf *m, int off)
298 {
299         INIT_VNET_INET(curvnet);
300         struct icmp *icp;
301         struct in_ifaddr *ia;
302         struct ip *ip = mtod(m, struct ip *);
303         struct sockaddr_in icmpsrc, icmpdst, icmpgw;
304         int hlen = off;
305         int icmplen = ip->ip_len;
306         int i, code;
307         void (*ctlfunc)(int, struct sockaddr *, void *);
308         int fibnum;
309 
310         /*
311          * Locate icmp structure in mbuf, and check
312          * that not corrupted and of at least minimum length.
313          */
314 #ifdef ICMPPRINTFS
315         if (icmpprintfs) {
316                 char buf[4 * sizeof "123"];
317                 strcpy(buf, inet_ntoa(ip->ip_src));
318                 printf("icmp_input from %s to %s, len %d\n",
319                        buf, inet_ntoa(ip->ip_dst), icmplen);
320         }
321 #endif
322         if (icmplen < ICMP_MINLEN) {
323                 V_icmpstat.icps_tooshort++;
324                 goto freeit;
325         }
326         i = hlen + min(icmplen, ICMP_ADVLENMIN);
327         if (m->m_len < i && (m = m_pullup(m, i)) == 0)  {
328                 V_icmpstat.icps_tooshort++;
329                 return;
330         }
331         ip = mtod(m, struct ip *);
332         m->m_len -= hlen;
333         m->m_data += hlen;
334         icp = mtod(m, struct icmp *);
335         if (in_cksum(m, icmplen)) {
336                 V_icmpstat.icps_checksum++;
337                 goto freeit;
338         }
339         m->m_len += hlen;
340         m->m_data -= hlen;
341 
342         if (m->m_pkthdr.rcvif && m->m_pkthdr.rcvif->if_type == IFT_FAITH) {
343                 /*
344                  * Deliver very specific ICMP type only.
345                  */
346                 switch (icp->icmp_type) {
347                 case ICMP_UNREACH:
348                 case ICMP_TIMXCEED:
349                         break;
350                 default:
351                         goto freeit;
352                 }
353         }
354 
355 #ifdef ICMPPRINTFS
356         if (icmpprintfs)
357                 printf("icmp_input, type %d code %d\n", icp->icmp_type,
358                     icp->icmp_code);
359 #endif
360 
361         /*
362          * Message type specific processing.
363          */
364         if (icp->icmp_type > ICMP_MAXTYPE)
365                 goto raw;
366 
367         /* Initialize */
368         bzero(&icmpsrc, sizeof(icmpsrc));
369         icmpsrc.sin_len = sizeof(struct sockaddr_in);
370         icmpsrc.sin_family = AF_INET;
371         bzero(&icmpdst, sizeof(icmpdst));
372         icmpdst.sin_len = sizeof(struct sockaddr_in);
373         icmpdst.sin_family = AF_INET;
374         bzero(&icmpgw, sizeof(icmpgw));
375         icmpgw.sin_len = sizeof(struct sockaddr_in);
376         icmpgw.sin_family = AF_INET;
377 
378         V_icmpstat.icps_inhist[icp->icmp_type]++;
379         code = icp->icmp_code;
380         switch (icp->icmp_type) {
381 
382         case ICMP_UNREACH:
383                 switch (code) {
384                         case ICMP_UNREACH_NET:
385                         case ICMP_UNREACH_HOST:
386                         case ICMP_UNREACH_SRCFAIL:
387                         case ICMP_UNREACH_NET_UNKNOWN:
388                         case ICMP_UNREACH_HOST_UNKNOWN:
389                         case ICMP_UNREACH_ISOLATED:
390                         case ICMP_UNREACH_TOSNET:
391                         case ICMP_UNREACH_TOSHOST:
392                         case ICMP_UNREACH_HOST_PRECEDENCE:
393                         case ICMP_UNREACH_PRECEDENCE_CUTOFF:
394                                 code = PRC_UNREACH_NET;
395                                 break;
396 
397                         case ICMP_UNREACH_NEEDFRAG:
398                                 code = PRC_MSGSIZE;
399                                 break;
400 
401                         /*
402                          * RFC 1122, Sections 3.2.2.1 and 4.2.3.9.
403                          * Treat subcodes 2,3 as immediate RST
404                          */
405                         case ICMP_UNREACH_PROTOCOL:
406                         case ICMP_UNREACH_PORT:
407                                 code = PRC_UNREACH_PORT;
408                                 break;
409 
410                         case ICMP_UNREACH_NET_PROHIB:
411                         case ICMP_UNREACH_HOST_PROHIB:
412                         case ICMP_UNREACH_FILTER_PROHIB:
413                                 code = PRC_UNREACH_ADMIN_PROHIB;
414                                 break;
415 
416                         default:
417                                 goto badcode;
418                 }
419                 goto deliver;
420 
421         case ICMP_TIMXCEED:
422                 if (code > 1)
423                         goto badcode;
424                 code += PRC_TIMXCEED_INTRANS;
425                 goto deliver;
426 
427         case ICMP_PARAMPROB:
428                 if (code > 1)
429                         goto badcode;
430                 code = PRC_PARAMPROB;
431                 goto deliver;
432 
433         case ICMP_SOURCEQUENCH:
434                 if (code)
435                         goto badcode;
436                 code = PRC_QUENCH;
437         deliver:
438                 /*
439                  * Problem with datagram; advise higher level routines.
440                  */
441                 if (icmplen < ICMP_ADVLENMIN || icmplen < ICMP_ADVLEN(icp) ||
442                     icp->icmp_ip.ip_hl < (sizeof(struct ip) >> 2)) {
443                         V_icmpstat.icps_badlen++;
444                         goto freeit;
445                 }
446                 icp->icmp_ip.ip_len = ntohs(icp->icmp_ip.ip_len);
447                 /* Discard ICMP's in response to multicast packets */
448                 if (IN_MULTICAST(ntohl(icp->icmp_ip.ip_dst.s_addr)))
449                         goto badcode;
450 #ifdef ICMPPRINTFS
451                 if (icmpprintfs)
452                         printf("deliver to protocol %d\n", icp->icmp_ip.ip_p);
453 #endif
454                 icmpsrc.sin_addr = icp->icmp_ip.ip_dst;
455                 /*
456                  * XXX if the packet contains [IPv4 AH TCP], we can't make a
457                  * notification to TCP layer.
458                  */
459                 ctlfunc = inetsw[ip_protox[icp->icmp_ip.ip_p]].pr_ctlinput;
460                 if (ctlfunc)
461                         (*ctlfunc)(code, (struct sockaddr *)&icmpsrc,
462                                    (void *)&icp->icmp_ip);
463                 break;
464 
465         badcode:
466                 V_icmpstat.icps_badcode++;
467                 break;
468 
469         case ICMP_ECHO:
470                 if (!V_icmpbmcastecho
471                     && (m->m_flags & (M_MCAST | M_BCAST)) != 0) {
472                         V_icmpstat.icps_bmcastecho++;
473                         break;
474                 }
475                 icp->icmp_type = ICMP_ECHOREPLY;
476                 if (badport_bandlim(BANDLIM_ICMP_ECHO) < 0)
477                         goto freeit;
478                 else
479                         goto reflect;
480 
481         case ICMP_TSTAMP:
482                 if (!V_icmpbmcastecho
483                     && (m->m_flags & (M_MCAST | M_BCAST)) != 0) {
484                         V_icmpstat.icps_bmcasttstamp++;
485                         break;
486                 }
487                 if (icmplen < ICMP_TSLEN) {
488                         V_icmpstat.icps_badlen++;
489                         break;
490                 }
491                 icp->icmp_type = ICMP_TSTAMPREPLY;
492                 icp->icmp_rtime = iptime();
493                 icp->icmp_ttime = icp->icmp_rtime;      /* bogus, do later! */
494                 if (badport_bandlim(BANDLIM_ICMP_TSTAMP) < 0)
495                         goto freeit;
496                 else
497                         goto reflect;
498 
499         case ICMP_MASKREQ:
500                 if (V_icmpmaskrepl == 0)
501                         break;
502                 /*
503                  * We are not able to respond with all ones broadcast
504                  * unless we receive it over a point-to-point interface.
505                  */
506                 if (icmplen < ICMP_MASKLEN)
507                         break;
508                 switch (ip->ip_dst.s_addr) {
509 
510                 case INADDR_BROADCAST:
511                 case INADDR_ANY:
512                         icmpdst.sin_addr = ip->ip_src;
513                         break;
514 
515                 default:
516                         icmpdst.sin_addr = ip->ip_dst;
517                 }
518                 ia = (struct in_ifaddr *)ifaof_ifpforaddr(
519                             (struct sockaddr *)&icmpdst, m->m_pkthdr.rcvif);
520                 if (ia == 0)
521                         break;
522                 if (ia->ia_ifp == 0)
523                         break;
524                 icp->icmp_type = ICMP_MASKREPLY;
525                 if (V_icmpmaskfake == 0)
526                         icp->icmp_mask = ia->ia_sockmask.sin_addr.s_addr;
527                 else
528                         icp->icmp_mask = V_icmpmaskfake;
529                 if (ip->ip_src.s_addr == 0) {
530                         if (ia->ia_ifp->if_flags & IFF_BROADCAST)
531                             ip->ip_src = satosin(&ia->ia_broadaddr)->sin_addr;
532                         else if (ia->ia_ifp->if_flags & IFF_POINTOPOINT)
533                             ip->ip_src = satosin(&ia->ia_dstaddr)->sin_addr;
534                 }
535 reflect:
536                 ip->ip_len += hlen;     /* since ip_input deducts this */
537                 V_icmpstat.icps_reflect++;
538                 V_icmpstat.icps_outhist[icp->icmp_type]++;
539                 icmp_reflect(m);
540                 return;
541 
542         case ICMP_REDIRECT:
543                 if (V_log_redirect) {
544                         u_long src, dst, gw;
545 
546                         src = ntohl(ip->ip_src.s_addr);
547                         dst = ntohl(icp->icmp_ip.ip_dst.s_addr);
548                         gw = ntohl(icp->icmp_gwaddr.s_addr);
549                         printf("icmp redirect from %d.%d.%d.%d: "
550                                "%d.%d.%d.%d => %d.%d.%d.%d\n",
551                                (int)(src >> 24), (int)((src >> 16) & 0xff),
552                                (int)((src >> 8) & 0xff), (int)(src & 0xff),
553                                (int)(dst >> 24), (int)((dst >> 16) & 0xff),
554                                (int)((dst >> 8) & 0xff), (int)(dst & 0xff),
555                                (int)(gw >> 24), (int)((gw >> 16) & 0xff),
556                                (int)((gw >> 8) & 0xff), (int)(gw & 0xff));
557                 }
558                 /*
559                  * RFC1812 says we must ignore ICMP redirects if we
560                  * are acting as router.
561                  */
562                 if (V_drop_redirect || V_ipforwarding)
563                         break;
564                 if (code > 3)
565                         goto badcode;
566                 if (icmplen < ICMP_ADVLENMIN || icmplen < ICMP_ADVLEN(icp) ||
567                     icp->icmp_ip.ip_hl < (sizeof(struct ip) >> 2)) {
568                         V_icmpstat.icps_badlen++;
569                         break;
570                 }
571                 /*
572                  * Short circuit routing redirects to force
573                  * immediate change in the kernel's routing
574                  * tables.  The message is also handed to anyone
575                  * listening on a raw socket (e.g. the routing
576                  * daemon for use in updating its tables).
577                  */
578                 icmpgw.sin_addr = ip->ip_src;
579                 icmpdst.sin_addr = icp->icmp_gwaddr;
580 #ifdef  ICMPPRINTFS
581                 if (icmpprintfs) {
582                         char buf[4 * sizeof "123"];
583                         strcpy(buf, inet_ntoa(icp->icmp_ip.ip_dst));
584 
585                         printf("redirect dst %s to %s\n",
586                                buf, inet_ntoa(icp->icmp_gwaddr));
587                 }
588 #endif
589                 icmpsrc.sin_addr = icp->icmp_ip.ip_dst;
590                 for ( fibnum = 0; fibnum < rt_numfibs; fibnum++) {
591                         in_rtredirect((struct sockaddr *)&icmpsrc,
592                           (struct sockaddr *)&icmpdst,
593                           (struct sockaddr *)0, RTF_GATEWAY | RTF_HOST,
594                           (struct sockaddr *)&icmpgw, fibnum);
595                 }
596                 pfctlinput(PRC_REDIRECT_HOST, (struct sockaddr *)&icmpsrc);
597 #ifdef IPSEC
598                 key_sa_routechange((struct sockaddr *)&icmpsrc);
599 #endif
600                 break;
601 
602         /*
603          * No kernel processing for the following;
604          * just fall through to send to raw listener.
605          */
606         case ICMP_ECHOREPLY:
607         case ICMP_ROUTERADVERT:
608         case ICMP_ROUTERSOLICIT:
609         case ICMP_TSTAMPREPLY:
610         case ICMP_IREQREPLY:
611         case ICMP_MASKREPLY:
612         default:
613                 break;
614         }
615 
616 raw:
617         rip_input(m, off);
618         return;
619 
620 freeit:
621         m_freem(m);
622 }
623 
624 /*
625  * Reflect the ip packet back to the source
626  */
627 static void
628 icmp_reflect(struct mbuf *m)
629 {
630         INIT_VNET_INET(curvnet);
631         struct ip *ip = mtod(m, struct ip *);
632         struct ifaddr *ifa;
633         struct ifnet *ifn;
634         struct in_ifaddr *ia;
635         struct in_addr t;
636         struct mbuf *opts = 0;
637         int optlen = (ip->ip_hl << 2) - sizeof(struct ip);
638 
639         if (IN_MULTICAST(ntohl(ip->ip_src.s_addr)) ||
640             IN_EXPERIMENTAL(ntohl(ip->ip_src.s_addr)) ||
641             IN_ZERONET(ntohl(ip->ip_src.s_addr)) ) {
642                 m_freem(m);     /* Bad return address */
643                 V_icmpstat.icps_badaddr++;
644                 goto done;      /* Ip_output() will check for broadcast */
645         }
646 
647         t = ip->ip_dst;
648         ip->ip_dst = ip->ip_src;
649 
650         /*
651          * Source selection for ICMP replies:
652          *
653          * If the incoming packet was addressed directly to one of our
654          * own addresses, use dst as the src for the reply.
655          */
656         LIST_FOREACH(ia, INADDR_HASH(t.s_addr), ia_hash)
657                 if (t.s_addr == IA_SIN(ia)->sin_addr.s_addr)
658                         goto match;
659         /*
660          * If the incoming packet was addressed to one of our broadcast
661          * addresses, use the first non-broadcast address which corresponds
662          * to the incoming interface.
663          */
664         if (m->m_pkthdr.rcvif != NULL &&
665             m->m_pkthdr.rcvif->if_flags & IFF_BROADCAST) {
666                 TAILQ_FOREACH(ifa, &m->m_pkthdr.rcvif->if_addrhead, ifa_link) {
667                         if (ifa->ifa_addr->sa_family != AF_INET)
668                                 continue;
669                         ia = ifatoia(ifa);
670                         if (satosin(&ia->ia_broadaddr)->sin_addr.s_addr ==
671                             t.s_addr)
672                                 goto match;
673                 }
674         }
675         /*
676          * If the packet was transiting through us, use the address of
677          * the interface the packet came through in.  If that interface
678          * doesn't have a suitable IP address, the normal selection
679          * criteria apply.
680          */
681         if (V_icmp_rfi && m->m_pkthdr.rcvif != NULL) {
682                 TAILQ_FOREACH(ifa, &m->m_pkthdr.rcvif->if_addrhead, ifa_link) {
683                         if (ifa->ifa_addr->sa_family != AF_INET)
684                                 continue;
685                         ia = ifatoia(ifa);
686                         goto match;
687                 }
688         }
689         /*
690          * If the incoming packet was not addressed directly to us, use
691          * designated interface for icmp replies specified by sysctl
692          * net.inet.icmp.reply_src (default not set). Otherwise continue
693          * with normal source selection.
694          */
695         if (V_reply_src[0] != '\0' && (ifn = ifunit(V_reply_src))) {
696                 TAILQ_FOREACH(ifa, &ifn->if_addrhead, ifa_link) {
697                         if (ifa->ifa_addr->sa_family != AF_INET)
698                                 continue;
699                         ia = ifatoia(ifa);
700                         goto match;
701                 }
702         }
703         /*
704          * If the packet was transiting through us, use the address of
705          * the interface that is the closest to the packet source.
706          * When we don't have a route back to the packet source, stop here
707          * and drop the packet.
708          */
709         ia = ip_rtaddr(ip->ip_dst, M_GETFIB(m));
710         if (ia == NULL) {
711                 m_freem(m);
712                 V_icmpstat.icps_noroute++;
713                 goto done;
714         }
715 match:
716 #ifdef MAC
717         mac_netinet_icmp_replyinplace(m);
718 #endif
719         t = IA_SIN(ia)->sin_addr;
720         ip->ip_src = t;
721         ip->ip_ttl = V_ip_defttl;
722 
723         if (optlen > 0) {
724                 register u_char *cp;
725                 int opt, cnt;
726                 u_int len;
727 
728                 /*
729                  * Retrieve any source routing from the incoming packet;
730                  * add on any record-route or timestamp options.
731                  */
732                 cp = (u_char *) (ip + 1);
733                 if ((opts = ip_srcroute(m)) == 0 &&
734                     (opts = m_gethdr(M_DONTWAIT, MT_DATA))) {
735                         opts->m_len = sizeof(struct in_addr);
736                         mtod(opts, struct in_addr *)->s_addr = 0;
737                 }
738                 if (opts) {
739 #ifdef ICMPPRINTFS
740                     if (icmpprintfs)
741                             printf("icmp_reflect optlen %d rt %d => ",
742                                 optlen, opts->m_len);
743 #endif
744                     for (cnt = optlen; cnt > 0; cnt -= len, cp += len) {
745                             opt = cp[IPOPT_OPTVAL];
746                             if (opt == IPOPT_EOL)
747                                     break;
748                             if (opt == IPOPT_NOP)
749                                     len = 1;
750                             else {
751                                     if (cnt < IPOPT_OLEN + sizeof(*cp))
752                                             break;
753                                     len = cp[IPOPT_OLEN];
754                                     if (len < IPOPT_OLEN + sizeof(*cp) ||
755                                         len > cnt)
756                                             break;
757                             }
758                             /*
759                              * Should check for overflow, but it "can't happen"
760                              */
761                             if (opt == IPOPT_RR || opt == IPOPT_TS ||
762                                 opt == IPOPT_SECURITY) {
763                                     bcopy((caddr_t)cp,
764                                         mtod(opts, caddr_t) + opts->m_len, len);
765                                     opts->m_len += len;
766                             }
767                     }
768                     /* Terminate & pad, if necessary */
769                     cnt = opts->m_len % 4;
770                     if (cnt) {
771                             for (; cnt < 4; cnt++) {
772                                     *(mtod(opts, caddr_t) + opts->m_len) =
773                                         IPOPT_EOL;
774                                     opts->m_len++;
775                             }
776                     }
777 #ifdef ICMPPRINTFS
778                     if (icmpprintfs)
779                             printf("%d\n", opts->m_len);
780 #endif
781                 }
782                 /*
783                  * Now strip out original options by copying rest of first
784                  * mbuf's data back, and adjust the IP length.
785                  */
786                 ip->ip_len -= optlen;
787                 ip->ip_v = IPVERSION;
788                 ip->ip_hl = 5;
789                 m->m_len -= optlen;
790                 if (m->m_flags & M_PKTHDR)
791                         m->m_pkthdr.len -= optlen;
792                 optlen += sizeof(struct ip);
793                 bcopy((caddr_t)ip + optlen, (caddr_t)(ip + 1),
794                          (unsigned)(m->m_len - sizeof(struct ip)));
795         }
796         m_tag_delete_nonpersistent(m);
797         m->m_flags &= ~(M_BCAST|M_MCAST);
798         icmp_send(m, opts);
799 done:
800         if (opts)
801                 (void)m_free(opts);
802 }
803 
804 /*
805  * Send an icmp packet back to the ip level,
806  * after supplying a checksum.
807  */
808 static void
809 icmp_send(struct mbuf *m, struct mbuf *opts)
810 {
811         register struct ip *ip = mtod(m, struct ip *);
812         register int hlen;
813         register struct icmp *icp;
814 
815         hlen = ip->ip_hl << 2;
816         m->m_data += hlen;
817         m->m_len -= hlen;
818         icp = mtod(m, struct icmp *);
819         icp->icmp_cksum = 0;
820         icp->icmp_cksum = in_cksum(m, ip->ip_len - hlen);
821         m->m_data -= hlen;
822         m->m_len += hlen;
823         m->m_pkthdr.rcvif = (struct ifnet *)0;
824 #ifdef ICMPPRINTFS
825         if (icmpprintfs) {
826                 char buf[4 * sizeof "123"];
827                 strcpy(buf, inet_ntoa(ip->ip_dst));
828                 printf("icmp_send dst %s src %s\n",
829                        buf, inet_ntoa(ip->ip_src));
830         }
831 #endif
832         (void) ip_output(m, opts, NULL, 0, NULL, NULL);
833 }
834 
835 n_time
836 iptime(void)
837 {
838         struct timeval atv;
839         u_long t;
840 
841         getmicrotime(&atv);
842         t = (atv.tv_sec % (24*60*60)) * 1000 + atv.tv_usec / 1000;
843         return (htonl(t));
844 }
845 
846 /*
847  * Return the next larger or smaller MTU plateau (table from RFC 1191)
848  * given current value MTU.  If DIR is less than zero, a larger plateau
849  * is returned; otherwise, a smaller value is returned.
850  */
851 int
852 ip_next_mtu(int mtu, int dir)
853 {
854         static int mtutab[] = {
855                 65535, 32000, 17914, 8166, 4352, 2002, 1492, 1280, 1006, 508,
856                 296, 68, 0
857         };
858         int i, size;
859 
860         size = (sizeof mtutab) / (sizeof mtutab[0]);
861         if (dir >= 0) {
862                 for (i = 0; i < size; i++)
863                         if (mtu > mtutab[i])
864                                 return mtutab[i];
865         } else {
866                 for (i = size - 1; i >= 0; i--)
867                         if (mtu < mtutab[i])
868                                 return mtutab[i];
869                 if (mtu == mtutab[0])
870                         return mtutab[0];
871         }
872         return 0;
873 }
874 
875 
876 /*
877  * badport_bandlim() - check for ICMP bandwidth limit
878  *
879  *      Return 0 if it is ok to send an ICMP error response, -1 if we have
880  *      hit our bandwidth limit and it is not ok.
881  *
882  *      If icmplim is <= 0, the feature is disabled and 0 is returned.
883  *
884  *      For now we separate the TCP and UDP subsystems w/ different 'which'
885  *      values.  We may eventually remove this separation (and simplify the
886  *      code further).
887  *
888  *      Note that the printing of the error message is delayed so we can
889  *      properly print the icmp error rate that the system was trying to do
890  *      (i.e. 22000/100 pps, etc...).  This can cause long delays in printing
891  *      the 'final' error, but it doesn't make sense to solve the printing
892  *      delay with more complex code.
893  */
894 
895 int
896 badport_bandlim(int which)
897 {
898         INIT_VNET_INET(curvnet);
899 
900 #define N(a)    (sizeof (a) / sizeof (a[0]))
901         static struct rate {
902                 const char      *type;
903                 struct timeval  lasttime;
904                 int             curpps;
905         } rates[BANDLIM_MAX+1] = {
906                 { "icmp unreach response" },
907                 { "icmp ping response" },
908                 { "icmp tstamp response" },
909                 { "closed port RST response" },
910                 { "open port RST response" },
911                 { "icmp6 unreach response" }
912         };
913 
914         /*
915          * Return ok status if feature disabled or argument out of range.
916          */
917         if (V_icmplim > 0 && (u_int) which < N(rates)) {
918                 struct rate *r = &rates[which];
919                 int opps = r->curpps;
920 
921                 if (!ppsratecheck(&r->lasttime, &r->curpps, V_icmplim))
922                         return -1;      /* discard packet */
923                 /*
924                  * If we've dropped below the threshold after having
925                  * rate-limited traffic print the message.  This preserves
926                  * the previous behaviour at the expense of added complexity.
927                  */
928                 if (V_icmplim_output && opps > V_icmplim)
929                         printf("Limiting %s from %d to %d packets/sec\n",
930                                 r->type, opps, V_icmplim);
931         }
932         return 0;                       /* okay to send packet */
933 #undef N
934 }
935 

[ source navigation ] [ diff markup ] [ identifier search ] [ freetext search ] [ file search ]


This page is part of the FreeBSD/Linux Linux Kernel Cross-Reference, and was automatically generated using a modified version of the LXR engine.