ip_icmp.c revision 1.11 1 /* $NetBSD: ip_icmp.c,v 1.11 1995/04/13 06:31:59 cgd Exp $ */
2
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. All advertising materials mentioning features or use of this software
16 * must display the following acknowledgement:
17 * This product includes software developed by the University of
18 * California, Berkeley and its contributors.
19 * 4. Neither the name of the University nor the names of its contributors
20 * may be used to endorse or promote products derived from this software
21 * without specific prior written permission.
22 *
23 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 * SUCH DAMAGE.
34 *
35 * @(#)ip_icmp.c 8.2 (Berkeley) 1/4/94
36 */
37
38 #include <sys/param.h>
39 #include <sys/systm.h>
40 #include <sys/malloc.h>
41 #include <sys/mbuf.h>
42 #include <sys/protosw.h>
43 #include <sys/socket.h>
44 #include <sys/time.h>
45 #include <sys/kernel.h>
46
47 #include <net/if.h>
48 #include <net/route.h>
49
50 #include <netinet/in.h>
51 #include <netinet/in_systm.h>
52 #include <netinet/in_var.h>
53 #include <netinet/ip.h>
54 #include <netinet/ip_icmp.h>
55 #include <netinet/icmp_var.h>
56
57 /*
58 * ICMP routines: error generation, receive packet processing, and
59 * routines to turnaround packets back to the originator, and
60 * host table maintenance routines.
61 */
62
63 int icmpmaskrepl = 0;
64 #ifdef ICMPPRINTFS
65 int icmpprintfs = 0;
66 #endif
67
68 extern struct protosw inetsw[];
69
70 /*
71 * Generate an error packet of type error
72 * in response to bad packet ip.
73 */
74 void
75 icmp_error(n, type, code, dest, destifp)
76 struct mbuf *n;
77 int type, code;
78 n_long dest;
79 struct ifnet *destifp;
80 {
81 register struct ip *oip = mtod(n, struct ip *), *nip;
82 register unsigned oiplen = oip->ip_hl << 2;
83 register struct icmp *icp;
84 register struct mbuf *m;
85 unsigned icmplen;
86
87 #ifdef ICMPPRINTFS
88 if (icmpprintfs)
89 printf("icmp_error(%x, %d, %d)\n", oip, type, code);
90 #endif
91 if (type != ICMP_REDIRECT)
92 icmpstat.icps_error++;
93 /*
94 * Don't send error if not the first fragment of message.
95 * Don't error if the old packet protocol was ICMP
96 * error message, only known informational types.
97 */
98 if (oip->ip_off &~ (IP_MF|IP_DF))
99 goto freeit;
100 if (oip->ip_p == IPPROTO_ICMP && type != ICMP_REDIRECT &&
101 n->m_len >= oiplen + ICMP_MINLEN &&
102 !ICMP_INFOTYPE(((struct icmp *)((caddr_t)oip + oiplen))->icmp_type)) {
103 icmpstat.icps_oldicmp++;
104 goto freeit;
105 }
106 /* Don't send error in response to a multicast or broadcast packet */
107 if (n->m_flags & (M_BCAST|M_MCAST))
108 goto freeit;
109 /*
110 * First, formulate icmp message
111 */
112 m = m_gethdr(M_DONTWAIT, MT_HEADER);
113 if (m == NULL)
114 goto freeit;
115 icmplen = oiplen + min(8, oip->ip_len);
116 m->m_len = icmplen + ICMP_MINLEN;
117 MH_ALIGN(m, m->m_len);
118 icp = mtod(m, struct icmp *);
119 if ((u_int)type > ICMP_MAXTYPE)
120 panic("icmp_error");
121 icmpstat.icps_outhist[type]++;
122 icp->icmp_type = type;
123 if (type == ICMP_REDIRECT)
124 icp->icmp_gwaddr.s_addr = dest;
125 else {
126 icp->icmp_void = 0;
127 /*
128 * The following assignments assume an overlay with the
129 * zeroed icmp_void field.
130 */
131 if (type == ICMP_PARAMPROB) {
132 icp->icmp_pptr = code;
133 code = 0;
134 } else if (type == ICMP_UNREACH &&
135 code == ICMP_UNREACH_NEEDFRAG && destifp) {
136 icp->icmp_nextmtu = htons(destifp->if_mtu);
137 }
138 }
139
140 icp->icmp_code = code;
141 bcopy((caddr_t)oip, (caddr_t)&icp->icmp_ip, icmplen);
142 nip = &icp->icmp_ip;
143 nip->ip_len = htons((u_int16_t)(nip->ip_len + oiplen));
144
145 /*
146 * Now, copy old ip header (without options)
147 * in front of icmp message.
148 */
149 if (m->m_data - sizeof(struct ip) < m->m_pktdat)
150 panic("icmp len");
151 m->m_data -= sizeof(struct ip);
152 m->m_len += sizeof(struct ip);
153 m->m_pkthdr.len = m->m_len;
154 m->m_pkthdr.rcvif = n->m_pkthdr.rcvif;
155 nip = mtod(m, struct ip *);
156 bcopy((caddr_t)oip, (caddr_t)nip, sizeof(struct ip));
157 nip->ip_len = m->m_len;
158 nip->ip_hl = sizeof(struct ip) >> 2;
159 nip->ip_p = IPPROTO_ICMP;
160 nip->ip_tos = 0;
161 icmp_reflect(m);
162
163 freeit:
164 m_freem(n);
165 }
166
167 static struct sockaddr_in icmpsrc = { sizeof (struct sockaddr_in), AF_INET };
168 static struct sockaddr_in icmpdst = { sizeof (struct sockaddr_in), AF_INET };
169 static struct sockaddr_in icmpgw = { sizeof (struct sockaddr_in), AF_INET };
170 struct sockaddr_in icmpmask = { 8, 0 };
171
172 /*
173 * Process a received ICMP message.
174 */
175 void
176 icmp_input(m, hlen)
177 register struct mbuf *m;
178 int hlen;
179 {
180 register struct icmp *icp;
181 register struct ip *ip = mtod(m, struct ip *);
182 int icmplen = ip->ip_len;
183 register int i;
184 struct in_ifaddr *ia;
185 void (*ctlfunc) __P((int, struct sockaddr *, struct ip *));
186 int code;
187 extern u_char ip_protox[];
188
189 /*
190 * Locate icmp structure in mbuf, and check
191 * that not corrupted and of at least minimum length.
192 */
193 #ifdef ICMPPRINTFS
194 if (icmpprintfs)
195 printf("icmp_input from %x to %x, len %d\n",
196 ntohl(ip->ip_src.s_addr), ntohl(ip->ip_dst.s_addr),
197 icmplen);
198 #endif
199 if (icmplen < ICMP_MINLEN) {
200 icmpstat.icps_tooshort++;
201 goto freeit;
202 }
203 i = hlen + min(icmplen, ICMP_ADVLENMIN);
204 if (m->m_len < i && (m = m_pullup(m, i)) == 0) {
205 icmpstat.icps_tooshort++;
206 return;
207 }
208 ip = mtod(m, struct ip *);
209 m->m_len -= hlen;
210 m->m_data += hlen;
211 icp = mtod(m, struct icmp *);
212 if (in_cksum(m, icmplen)) {
213 icmpstat.icps_checksum++;
214 goto freeit;
215 }
216 m->m_len += hlen;
217 m->m_data -= hlen;
218
219 #ifdef ICMPPRINTFS
220 /*
221 * Message type specific processing.
222 */
223 if (icmpprintfs)
224 printf("icmp_input, type %d code %d\n", icp->icmp_type,
225 icp->icmp_code);
226 #endif
227 if (icp->icmp_type > ICMP_MAXTYPE)
228 goto raw;
229 icmpstat.icps_inhist[icp->icmp_type]++;
230 code = icp->icmp_code;
231 switch (icp->icmp_type) {
232
233 case ICMP_UNREACH:
234 switch (code) {
235 case ICMP_UNREACH_NET:
236 case ICMP_UNREACH_HOST:
237 case ICMP_UNREACH_PROTOCOL:
238 case ICMP_UNREACH_PORT:
239 case ICMP_UNREACH_SRCFAIL:
240 code += PRC_UNREACH_NET;
241 break;
242
243 case ICMP_UNREACH_NEEDFRAG:
244 code = PRC_MSGSIZE;
245 break;
246
247 case ICMP_UNREACH_NET_UNKNOWN:
248 case ICMP_UNREACH_NET_PROHIB:
249 case ICMP_UNREACH_TOSNET:
250 code = PRC_UNREACH_NET;
251 break;
252
253 case ICMP_UNREACH_HOST_UNKNOWN:
254 case ICMP_UNREACH_ISOLATED:
255 case ICMP_UNREACH_HOST_PROHIB:
256 case ICMP_UNREACH_TOSHOST:
257 code = PRC_UNREACH_HOST;
258 break;
259
260 default:
261 goto badcode;
262 }
263 goto deliver;
264
265 case ICMP_TIMXCEED:
266 if (code > 1)
267 goto badcode;
268 code += PRC_TIMXCEED_INTRANS;
269 goto deliver;
270
271 case ICMP_PARAMPROB:
272 if (code > 1)
273 goto badcode;
274 code = PRC_PARAMPROB;
275 goto deliver;
276
277 case ICMP_SOURCEQUENCH:
278 if (code)
279 goto badcode;
280 code = PRC_QUENCH;
281 deliver:
282 /*
283 * Problem with datagram; advise higher level routines.
284 */
285 if (icmplen < ICMP_ADVLENMIN || icmplen < ICMP_ADVLEN(icp) ||
286 icp->icmp_ip.ip_hl < (sizeof(struct ip) >> 2)) {
287 icmpstat.icps_badlen++;
288 goto freeit;
289 }
290 NTOHS(icp->icmp_ip.ip_len);
291 #ifdef ICMPPRINTFS
292 if (icmpprintfs)
293 printf("deliver to protocol %d\n", icp->icmp_ip.ip_p);
294 #endif
295 icmpsrc.sin_addr = icp->icmp_ip.ip_dst;
296 if (ctlfunc = inetsw[ip_protox[icp->icmp_ip.ip_p]].pr_ctlinput)
297 (*ctlfunc)(code, (struct sockaddr *)&icmpsrc,
298 &icp->icmp_ip);
299 break;
300
301 badcode:
302 icmpstat.icps_badcode++;
303 break;
304
305 case ICMP_ECHO:
306 icp->icmp_type = ICMP_ECHOREPLY;
307 goto reflect;
308
309 case ICMP_TSTAMP:
310 if (icmplen < ICMP_TSLEN) {
311 icmpstat.icps_badlen++;
312 break;
313 }
314 icp->icmp_type = ICMP_TSTAMPREPLY;
315 icp->icmp_rtime = iptime();
316 icp->icmp_ttime = icp->icmp_rtime; /* bogus, do later! */
317 goto reflect;
318
319 case ICMP_MASKREQ:
320 #define satosin(sa) ((struct sockaddr_in *)(sa))
321 if (icmpmaskrepl == 0)
322 break;
323 /*
324 * We are not able to respond with all ones broadcast
325 * unless we receive it over a point-to-point interface.
326 */
327 if (icmplen < ICMP_MASKLEN)
328 break;
329 switch (ip->ip_dst.s_addr) {
330
331 case INADDR_BROADCAST:
332 case INADDR_ANY:
333 icmpdst.sin_addr = ip->ip_src;
334 break;
335
336 default:
337 icmpdst.sin_addr = ip->ip_dst;
338 }
339 ia = (struct in_ifaddr *)ifaof_ifpforaddr(
340 (struct sockaddr *)&icmpdst, m->m_pkthdr.rcvif);
341 if (ia == 0)
342 break;
343 icp->icmp_type = ICMP_MASKREPLY;
344 icp->icmp_mask = ia->ia_sockmask.sin_addr.s_addr;
345 if (ip->ip_src.s_addr == 0) {
346 if (ia->ia_ifp->if_flags & IFF_BROADCAST)
347 ip->ip_src = satosin(&ia->ia_broadaddr)->sin_addr;
348 else if (ia->ia_ifp->if_flags & IFF_POINTOPOINT)
349 ip->ip_src = satosin(&ia->ia_dstaddr)->sin_addr;
350 }
351 reflect:
352 ip->ip_len += hlen; /* since ip_input deducts this */
353 icmpstat.icps_reflect++;
354 icmpstat.icps_outhist[icp->icmp_type]++;
355 icmp_reflect(m);
356 return;
357
358 case ICMP_REDIRECT:
359 if (code > 3)
360 goto badcode;
361 if (icmplen < ICMP_ADVLENMIN || icmplen < ICMP_ADVLEN(icp) ||
362 icp->icmp_ip.ip_hl < (sizeof(struct ip) >> 2)) {
363 icmpstat.icps_badlen++;
364 break;
365 }
366 /*
367 * Short circuit routing redirects to force
368 * immediate change in the kernel's routing
369 * tables. The message is also handed to anyone
370 * listening on a raw socket (e.g. the routing
371 * daemon for use in updating its tables).
372 */
373 icmpgw.sin_addr = ip->ip_src;
374 icmpdst.sin_addr = icp->icmp_gwaddr;
375 #ifdef ICMPPRINTFS
376 if (icmpprintfs)
377 printf("redirect dst %x to %x\n", icp->icmp_ip.ip_dst,
378 icp->icmp_gwaddr);
379 #endif
380 icmpsrc.sin_addr = icp->icmp_ip.ip_dst;
381 rtredirect((struct sockaddr *)&icmpsrc,
382 (struct sockaddr *)&icmpdst,
383 (struct sockaddr *)0, RTF_GATEWAY | RTF_HOST,
384 (struct sockaddr *)&icmpgw, (struct rtentry **)0);
385 pfctlinput(PRC_REDIRECT_HOST, (struct sockaddr *)&icmpsrc);
386 break;
387
388 /*
389 * No kernel processing for the following;
390 * just fall through to send to raw listener.
391 */
392 case ICMP_ECHOREPLY:
393 case ICMP_ROUTERADVERT:
394 case ICMP_ROUTERSOLICIT:
395 case ICMP_TSTAMPREPLY:
396 case ICMP_IREQREPLY:
397 case ICMP_MASKREPLY:
398 default:
399 break;
400 }
401
402 raw:
403 rip_input(m);
404 return;
405
406 freeit:
407 m_freem(m);
408 }
409
410 /*
411 * Reflect the ip packet back to the source
412 */
413 void
414 icmp_reflect(m)
415 struct mbuf *m;
416 {
417 register struct ip *ip = mtod(m, struct ip *);
418 register struct in_ifaddr *ia;
419 struct in_addr t;
420 struct mbuf *opts = 0, *ip_srcroute();
421 int optlen = (ip->ip_hl << 2) - sizeof(struct ip);
422
423 if (!in_canforward(ip->ip_src) &&
424 ((ntohl(ip->ip_src.s_addr) & IN_CLASSA_NET) !=
425 (IN_LOOPBACKNET << IN_CLASSA_NSHIFT))) {
426 m_freem(m); /* Bad return address */
427 goto done; /* Ip_output() will check for broadcast */
428 }
429 t = ip->ip_dst;
430 ip->ip_dst = ip->ip_src;
431 /*
432 * If the incoming packet was addressed directly to us,
433 * use dst as the src for the reply. Otherwise (broadcast
434 * or anonymous), use the address which corresponds
435 * to the incoming interface.
436 */
437 for (ia = in_ifaddr; ia; ia = ia->ia_next) {
438 if (t.s_addr == IA_SIN(ia)->sin_addr.s_addr)
439 break;
440 if ((ia->ia_ifp->if_flags & IFF_BROADCAST) &&
441 t.s_addr == satosin(&ia->ia_broadaddr)->sin_addr.s_addr)
442 break;
443 }
444 icmpdst.sin_addr = t;
445 if (ia == (struct in_ifaddr *)0)
446 ia = (struct in_ifaddr *)ifaof_ifpforaddr(
447 (struct sockaddr *)&icmpdst, m->m_pkthdr.rcvif);
448 /*
449 * The following happens if the packet was not addressed to us,
450 * and was received on an interface with no IP address.
451 */
452 if (ia == (struct in_ifaddr *)0)
453 ia = in_ifaddr;
454 t = IA_SIN(ia)->sin_addr;
455 ip->ip_src = t;
456 ip->ip_ttl = MAXTTL;
457
458 if (optlen > 0) {
459 register u_char *cp;
460 int opt, cnt;
461 u_int len;
462
463 /*
464 * Retrieve any source routing from the incoming packet;
465 * add on any record-route or timestamp options.
466 */
467 cp = (u_char *) (ip + 1);
468 if ((opts = ip_srcroute()) == 0 &&
469 (opts = m_gethdr(M_DONTWAIT, MT_HEADER))) {
470 opts->m_len = sizeof(struct in_addr);
471 mtod(opts, struct in_addr *)->s_addr = 0;
472 }
473 if (opts) {
474 #ifdef ICMPPRINTFS
475 if (icmpprintfs)
476 printf("icmp_reflect optlen %d rt %d => ",
477 optlen, opts->m_len);
478 #endif
479 for (cnt = optlen; cnt > 0; cnt -= len, cp += len) {
480 opt = cp[IPOPT_OPTVAL];
481 if (opt == IPOPT_EOL)
482 break;
483 if (opt == IPOPT_NOP)
484 len = 1;
485 else {
486 len = cp[IPOPT_OLEN];
487 if (len <= 0 || len > cnt)
488 break;
489 }
490 /*
491 * Should check for overflow, but it "can't happen"
492 */
493 if (opt == IPOPT_RR || opt == IPOPT_TS ||
494 opt == IPOPT_SECURITY) {
495 bcopy((caddr_t)cp,
496 mtod(opts, caddr_t) + opts->m_len, len);
497 opts->m_len += len;
498 }
499 }
500 /* Terminate & pad, if necessary */
501 if (cnt = opts->m_len % 4) {
502 for (; cnt < 4; cnt++) {
503 *(mtod(opts, caddr_t) + opts->m_len) =
504 IPOPT_EOL;
505 opts->m_len++;
506 }
507 }
508 #ifdef ICMPPRINTFS
509 if (icmpprintfs)
510 printf("%d\n", opts->m_len);
511 #endif
512 }
513 /*
514 * Now strip out original options by copying rest of first
515 * mbuf's data back, and adjust the IP length.
516 */
517 ip->ip_len -= optlen;
518 ip->ip_hl = sizeof(struct ip) >> 2;
519 m->m_len -= optlen;
520 if (m->m_flags & M_PKTHDR)
521 m->m_pkthdr.len -= optlen;
522 optlen += sizeof(struct ip);
523 bcopy((caddr_t)ip + optlen, (caddr_t)(ip + 1),
524 (unsigned)(m->m_len - sizeof(struct ip)));
525 }
526 m->m_flags &= ~(M_BCAST|M_MCAST);
527 icmp_send(m, opts);
528 done:
529 if (opts)
530 (void)m_free(opts);
531 }
532
533 /*
534 * Send an icmp packet back to the ip level,
535 * after supplying a checksum.
536 */
537 void
538 icmp_send(m, opts)
539 register struct mbuf *m;
540 struct mbuf *opts;
541 {
542 register struct ip *ip = mtod(m, struct ip *);
543 register int hlen;
544 register struct icmp *icp;
545
546 hlen = ip->ip_hl << 2;
547 m->m_data += hlen;
548 m->m_len -= hlen;
549 icp = mtod(m, struct icmp *);
550 icp->icmp_cksum = 0;
551 icp->icmp_cksum = in_cksum(m, ip->ip_len - hlen);
552 m->m_data -= hlen;
553 m->m_len += hlen;
554 #ifdef ICMPPRINTFS
555 if (icmpprintfs)
556 printf("icmp_send dst %x src %x\n", ip->ip_dst, ip->ip_src);
557 #endif
558 (void) ip_output(m, opts, NULL, 0, NULL);
559 }
560
561 n_time
562 iptime()
563 {
564 struct timeval atv;
565 u_long t;
566
567 microtime(&atv);
568 t = (atv.tv_sec % (24*60*60)) * 1000 + atv.tv_usec / 1000;
569 return (htonl(t));
570 }
571
572 int
573 icmp_sysctl(name, namelen, oldp, oldlenp, newp, newlen)
574 int *name;
575 u_int namelen;
576 void *oldp;
577 size_t *oldlenp;
578 void *newp;
579 size_t newlen;
580 {
581
582 /* All sysctl names at this level are terminal. */
583 if (namelen != 1)
584 return (ENOTDIR);
585
586 switch (name[0]) {
587 case ICMPCTL_MASKREPL:
588 return (sysctl_int(oldp, oldlenp, newp, newlen, &icmpmaskrepl));
589 default:
590 return (ENOPROTOOPT);
591 }
592 /* NOTREACHED */
593 }
594