ip_icmp.c revision 1.14 1 /* $NetBSD: ip_icmp.c,v 1.14 1995/06/01 21:36:22 mycroft 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 icp->icmp_code = code;
140 bcopy((caddr_t)oip, (caddr_t)&icp->icmp_ip, icmplen);
141 nip = &icp->icmp_ip;
142 nip->ip_len = htons((u_int16_t)(nip->ip_len + oiplen));
143
144 /*
145 * Now, copy old ip header (without options)
146 * in front of icmp message.
147 */
148 if (m->m_data - sizeof(struct ip) < m->m_pktdat)
149 panic("icmp len");
150 m->m_data -= sizeof(struct ip);
151 m->m_len += sizeof(struct ip);
152 m->m_pkthdr.len = m->m_len;
153 m->m_pkthdr.rcvif = n->m_pkthdr.rcvif;
154 nip = mtod(m, struct ip *);
155 bcopy((caddr_t)oip, (caddr_t)nip, sizeof(struct ip));
156 nip->ip_len = m->m_len;
157 nip->ip_hl = sizeof(struct ip) >> 2;
158 nip->ip_p = IPPROTO_ICMP;
159 nip->ip_tos = 0;
160 icmp_reflect(m);
161
162 freeit:
163 m_freem(n);
164 }
165
166 static struct sockaddr_in icmpsrc = { sizeof (struct sockaddr_in), AF_INET };
167 static struct sockaddr_in icmpdst = { sizeof (struct sockaddr_in), AF_INET };
168 static struct sockaddr_in icmpgw = { sizeof (struct sockaddr_in), AF_INET };
169 struct sockaddr_in icmpmask = { 8, 0 };
170
171 /*
172 * Process a received ICMP message.
173 */
174 void
175 icmp_input(m, hlen)
176 register struct mbuf *m;
177 int hlen;
178 {
179 register struct icmp *icp;
180 register struct ip *ip = mtod(m, struct ip *);
181 int icmplen = ip->ip_len;
182 register int i;
183 struct in_ifaddr *ia;
184 void (*ctlfunc) __P((int, struct sockaddr *, struct ip *));
185 int code;
186 extern u_char ip_protox[];
187
188 /*
189 * Locate icmp structure in mbuf, and check
190 * that not corrupted and of at least minimum length.
191 */
192 #ifdef ICMPPRINTFS
193 if (icmpprintfs)
194 printf("icmp_input from %x to %x, len %d\n",
195 ntohl(ip->ip_src.s_addr), ntohl(ip->ip_dst.s_addr),
196 icmplen);
197 #endif
198 if (icmplen < ICMP_MINLEN) {
199 icmpstat.icps_tooshort++;
200 goto freeit;
201 }
202 i = hlen + min(icmplen, ICMP_ADVLENMIN);
203 if (m->m_len < i && (m = m_pullup(m, i)) == 0) {
204 icmpstat.icps_tooshort++;
205 return;
206 }
207 ip = mtod(m, struct ip *);
208 m->m_len -= hlen;
209 m->m_data += hlen;
210 icp = mtod(m, struct icmp *);
211 if (in_cksum(m, icmplen)) {
212 icmpstat.icps_checksum++;
213 goto freeit;
214 }
215 m->m_len += hlen;
216 m->m_data -= hlen;
217
218 #ifdef ICMPPRINTFS
219 /*
220 * Message type specific processing.
221 */
222 if (icmpprintfs)
223 printf("icmp_input, type %d code %d\n", icp->icmp_type,
224 icp->icmp_code);
225 #endif
226 if (icp->icmp_type > ICMP_MAXTYPE)
227 goto raw;
228 icmpstat.icps_inhist[icp->icmp_type]++;
229 code = icp->icmp_code;
230 switch (icp->icmp_type) {
231
232 case ICMP_UNREACH:
233 switch (code) {
234 case ICMP_UNREACH_NET:
235 case ICMP_UNREACH_HOST:
236 case ICMP_UNREACH_PROTOCOL:
237 case ICMP_UNREACH_PORT:
238 case ICMP_UNREACH_SRCFAIL:
239 code += PRC_UNREACH_NET;
240 break;
241
242 case ICMP_UNREACH_NEEDFRAG:
243 code = PRC_MSGSIZE;
244 break;
245
246 case ICMP_UNREACH_NET_UNKNOWN:
247 case ICMP_UNREACH_NET_PROHIB:
248 case ICMP_UNREACH_TOSNET:
249 code = PRC_UNREACH_NET;
250 break;
251
252 case ICMP_UNREACH_HOST_UNKNOWN:
253 case ICMP_UNREACH_ISOLATED:
254 case ICMP_UNREACH_HOST_PROHIB:
255 case ICMP_UNREACH_TOSHOST:
256 code = PRC_UNREACH_HOST;
257 break;
258
259 default:
260 goto badcode;
261 }
262 goto deliver;
263
264 case ICMP_TIMXCEED:
265 if (code > 1)
266 goto badcode;
267 code += PRC_TIMXCEED_INTRANS;
268 goto deliver;
269
270 case ICMP_PARAMPROB:
271 if (code > 1)
272 goto badcode;
273 code = PRC_PARAMPROB;
274 goto deliver;
275
276 case ICMP_SOURCEQUENCH:
277 if (code)
278 goto badcode;
279 code = PRC_QUENCH;
280 deliver:
281 /*
282 * Problem with datagram; advise higher level routines.
283 */
284 if (icmplen < ICMP_ADVLENMIN || icmplen < ICMP_ADVLEN(icp) ||
285 icp->icmp_ip.ip_hl < (sizeof(struct ip) >> 2)) {
286 icmpstat.icps_badlen++;
287 goto freeit;
288 }
289 if (IN_MULTICAST(icp->icmp_ip.ip_dst.s_addr))
290 goto badcode;
291 NTOHS(icp->icmp_ip.ip_len);
292 #ifdef ICMPPRINTFS
293 if (icmpprintfs)
294 printf("deliver to protocol %d\n", icp->icmp_ip.ip_p);
295 #endif
296 icmpsrc.sin_addr = icp->icmp_ip.ip_dst;
297 if (ctlfunc = inetsw[ip_protox[icp->icmp_ip.ip_p]].pr_ctlinput)
298 (*ctlfunc)(code, (struct sockaddr *)&icmpsrc,
299 &icp->icmp_ip);
300 break;
301
302 badcode:
303 icmpstat.icps_badcode++;
304 break;
305
306 case ICMP_ECHO:
307 icp->icmp_type = ICMP_ECHOREPLY;
308 goto reflect;
309
310 case ICMP_TSTAMP:
311 if (icmplen < ICMP_TSLEN) {
312 icmpstat.icps_badlen++;
313 break;
314 }
315 icp->icmp_type = ICMP_TSTAMPREPLY;
316 icp->icmp_rtime = iptime();
317 icp->icmp_ttime = icp->icmp_rtime; /* bogus, do later! */
318 goto reflect;
319
320 case ICMP_MASKREQ:
321 #define satosin(sa) ((struct sockaddr_in *)(sa))
322 if (icmpmaskrepl == 0)
323 break;
324 /*
325 * We are not able to respond with all ones broadcast
326 * unless we receive it over a point-to-point interface.
327 */
328 if (icmplen < ICMP_MASKLEN)
329 break;
330 switch (ip->ip_dst.s_addr) {
331
332 case INADDR_BROADCAST:
333 case INADDR_ANY:
334 icmpdst.sin_addr = ip->ip_src;
335 break;
336
337 default:
338 icmpdst.sin_addr = ip->ip_dst;
339 }
340 ia = (struct in_ifaddr *)ifaof_ifpforaddr(
341 (struct sockaddr *)&icmpdst, m->m_pkthdr.rcvif);
342 if (ia == 0)
343 break;
344 icp->icmp_type = ICMP_MASKREPLY;
345 icp->icmp_mask = ia->ia_sockmask.sin_addr.s_addr;
346 if (ip->ip_src.s_addr == 0) {
347 if (ia->ia_ifp->if_flags & IFF_BROADCAST)
348 ip->ip_src = satosin(&ia->ia_broadaddr)->sin_addr;
349 else if (ia->ia_ifp->if_flags & IFF_POINTOPOINT)
350 ip->ip_src = satosin(&ia->ia_dstaddr)->sin_addr;
351 }
352 reflect:
353 ip->ip_len += hlen; /* since ip_input deducts this */
354 icmpstat.icps_reflect++;
355 icmpstat.icps_outhist[icp->icmp_type]++;
356 icmp_reflect(m);
357 return;
358
359 case ICMP_REDIRECT:
360 if (code > 3)
361 goto badcode;
362 if (icmplen < ICMP_ADVLENMIN || icmplen < ICMP_ADVLEN(icp) ||
363 icp->icmp_ip.ip_hl < (sizeof(struct ip) >> 2)) {
364 icmpstat.icps_badlen++;
365 break;
366 }
367 /*
368 * Short circuit routing redirects to force
369 * immediate change in the kernel's routing
370 * tables. The message is also handed to anyone
371 * listening on a raw socket (e.g. the routing
372 * daemon for use in updating its tables).
373 */
374 icmpgw.sin_addr = ip->ip_src;
375 icmpdst.sin_addr = icp->icmp_gwaddr;
376 #ifdef ICMPPRINTFS
377 if (icmpprintfs)
378 printf("redirect dst %x to %x\n", icp->icmp_ip.ip_dst,
379 icp->icmp_gwaddr);
380 #endif
381 icmpsrc.sin_addr = icp->icmp_ip.ip_dst;
382 rtredirect((struct sockaddr *)&icmpsrc,
383 (struct sockaddr *)&icmpdst,
384 (struct sockaddr *)0, RTF_GATEWAY | RTF_HOST,
385 (struct sockaddr *)&icmpgw, (struct rtentry **)0);
386 pfctlinput(PRC_REDIRECT_HOST, (struct sockaddr *)&icmpsrc);
387 break;
388
389 /*
390 * No kernel processing for the following;
391 * just fall through to send to raw listener.
392 */
393 case ICMP_ECHOREPLY:
394 case ICMP_ROUTERADVERT:
395 case ICMP_ROUTERSOLICIT:
396 case ICMP_TSTAMPREPLY:
397 case ICMP_IREQREPLY:
398 case ICMP_MASKREPLY:
399 default:
400 break;
401 }
402
403 raw:
404 rip_input(m);
405 return;
406
407 freeit:
408 m_freem(m);
409 }
410
411 /*
412 * Reflect the ip packet back to the source
413 */
414 void
415 icmp_reflect(m)
416 struct mbuf *m;
417 {
418 register struct ip *ip = mtod(m, struct ip *);
419 register struct in_ifaddr *ia;
420 struct in_addr t;
421 struct mbuf *opts = 0, *ip_srcroute();
422 int optlen = (ip->ip_hl << 2) - sizeof(struct ip);
423
424 if (!in_canforward(ip->ip_src) &&
425 ((ip->ip_src.s_addr & IN_CLASSA_NET) !=
426 htonl(IN_LOOPBACKNET << IN_CLASSA_NSHIFT))) {
427 m_freem(m); /* Bad return address */
428 goto done; /* ip_output() will check for broadcast */
429 }
430 t = ip->ip_dst;
431 ip->ip_dst = ip->ip_src;
432 /*
433 * If the incoming packet was addressed directly to us,
434 * use dst as the src for the reply. Otherwise (broadcast
435 * or anonymous), use the address which corresponds
436 * to the incoming interface.
437 */
438 for (ia = in_ifaddr; ia; ia = ia->ia_next) {
439 if (t.s_addr == IA_SIN(ia)->sin_addr.s_addr)
440 break;
441 if ((ia->ia_ifp->if_flags & IFF_BROADCAST) &&
442 t.s_addr == satosin(&ia->ia_broadaddr)->sin_addr.s_addr)
443 break;
444 }
445 icmpdst.sin_addr = t;
446 if (ia == (struct in_ifaddr *)0)
447 ia = (struct in_ifaddr *)ifaof_ifpforaddr(
448 (struct sockaddr *)&icmpdst, m->m_pkthdr.rcvif);
449 /*
450 * The following happens if the packet was not addressed to us,
451 * and was received on an interface with no IP address.
452 */
453 if (ia == (struct in_ifaddr *)0)
454 ia = in_ifaddr;
455 t = IA_SIN(ia)->sin_addr;
456 ip->ip_src = t;
457 ip->ip_ttl = MAXTTL;
458
459 if (optlen > 0) {
460 register u_char *cp;
461 int opt, cnt;
462 u_int len;
463
464 /*
465 * Retrieve any source routing from the incoming packet;
466 * add on any record-route or timestamp options.
467 */
468 cp = (u_char *) (ip + 1);
469 if ((opts = ip_srcroute()) == 0 &&
470 (opts = m_gethdr(M_DONTWAIT, MT_HEADER))) {
471 opts->m_len = sizeof(struct in_addr);
472 mtod(opts, struct in_addr *)->s_addr = 0;
473 }
474 if (opts) {
475 #ifdef ICMPPRINTFS
476 if (icmpprintfs)
477 printf("icmp_reflect optlen %d rt %d => ",
478 optlen, opts->m_len);
479 #endif
480 for (cnt = optlen; cnt > 0; cnt -= len, cp += len) {
481 opt = cp[IPOPT_OPTVAL];
482 if (opt == IPOPT_EOL)
483 break;
484 if (opt == IPOPT_NOP)
485 len = 1;
486 else {
487 len = cp[IPOPT_OLEN];
488 if (len <= 0 || len > cnt)
489 break;
490 }
491 /*
492 * Should check for overflow, but it "can't happen"
493 */
494 if (opt == IPOPT_RR || opt == IPOPT_TS ||
495 opt == IPOPT_SECURITY) {
496 bcopy((caddr_t)cp,
497 mtod(opts, caddr_t) + opts->m_len, len);
498 opts->m_len += len;
499 }
500 }
501 /* Terminate & pad, if necessary */
502 if (cnt = opts->m_len % 4) {
503 for (; cnt < 4; cnt++) {
504 *(mtod(opts, caddr_t) + opts->m_len) =
505 IPOPT_EOL;
506 opts->m_len++;
507 }
508 }
509 #ifdef ICMPPRINTFS
510 if (icmpprintfs)
511 printf("%d\n", opts->m_len);
512 #endif
513 }
514 /*
515 * Now strip out original options by copying rest of first
516 * mbuf's data back, and adjust the IP length.
517 */
518 ip->ip_len -= optlen;
519 ip->ip_hl = sizeof(struct ip) >> 2;
520 m->m_len -= optlen;
521 if (m->m_flags & M_PKTHDR)
522 m->m_pkthdr.len -= optlen;
523 optlen += sizeof(struct ip);
524 bcopy((caddr_t)ip + optlen, (caddr_t)(ip + 1),
525 (unsigned)(m->m_len - sizeof(struct ip)));
526 }
527 m->m_flags &= ~(M_BCAST|M_MCAST);
528 icmp_send(m, opts);
529 done:
530 if (opts)
531 (void)m_free(opts);
532 }
533
534 /*
535 * Send an icmp packet back to the ip level,
536 * after supplying a checksum.
537 */
538 void
539 icmp_send(m, opts)
540 register struct mbuf *m;
541 struct mbuf *opts;
542 {
543 register struct ip *ip = mtod(m, struct ip *);
544 register int hlen;
545 register struct icmp *icp;
546
547 hlen = ip->ip_hl << 2;
548 m->m_data += hlen;
549 m->m_len -= hlen;
550 icp = mtod(m, struct icmp *);
551 icp->icmp_cksum = 0;
552 icp->icmp_cksum = in_cksum(m, ip->ip_len - hlen);
553 m->m_data -= hlen;
554 m->m_len += hlen;
555 #ifdef ICMPPRINTFS
556 if (icmpprintfs)
557 printf("icmp_send dst %x src %x\n", ip->ip_dst, ip->ip_src);
558 #endif
559 (void) ip_output(m, opts, NULL, 0, NULL);
560 }
561
562 n_time
563 iptime()
564 {
565 struct timeval atv;
566 u_long t;
567
568 microtime(&atv);
569 t = (atv.tv_sec % (24*60*60)) * 1000 + atv.tv_usec / 1000;
570 return (htonl(t));
571 }
572
573 int
574 icmp_sysctl(name, namelen, oldp, oldlenp, newp, newlen)
575 int *name;
576 u_int namelen;
577 void *oldp;
578 size_t *oldlenp;
579 void *newp;
580 size_t newlen;
581 {
582
583 /* All sysctl names at this level are terminal. */
584 if (namelen != 1)
585 return (ENOTDIR);
586
587 switch (name[0]) {
588 case ICMPCTL_MASKREPL:
589 return (sysctl_int(oldp, oldlenp, newp, newlen, &icmpmaskrepl));
590 default:
591 return (ENOPROTOOPT);
592 }
593 /* NOTREACHED */
594 }
595