ip_icmp.c revision 1.15 1 /* $NetBSD: ip_icmp.c,v 1.15 1995/06/01 21:46:27 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 if (ip->ip_dst.s_addr == INADDR_BROADCAST ||
331 ip->ip_dst.s_addr == INADDR_ANY)
332 icmpdst.sin_addr = ip->ip_src;
333 else
334 icmpdst.sin_addr = ip->ip_dst;
335 ia = (struct in_ifaddr *)ifaof_ifpforaddr(
336 (struct sockaddr *)&icmpdst, m->m_pkthdr.rcvif);
337 if (ia == 0)
338 break;
339 icp->icmp_type = ICMP_MASKREPLY;
340 icp->icmp_mask = ia->ia_sockmask.sin_addr.s_addr;
341 if (ip->ip_src.s_addr == 0) {
342 if (ia->ia_ifp->if_flags & IFF_BROADCAST)
343 ip->ip_src = satosin(&ia->ia_broadaddr)->sin_addr;
344 else if (ia->ia_ifp->if_flags & IFF_POINTOPOINT)
345 ip->ip_src = satosin(&ia->ia_dstaddr)->sin_addr;
346 }
347 reflect:
348 ip->ip_len += hlen; /* since ip_input deducts this */
349 icmpstat.icps_reflect++;
350 icmpstat.icps_outhist[icp->icmp_type]++;
351 icmp_reflect(m);
352 return;
353
354 case ICMP_REDIRECT:
355 if (code > 3)
356 goto badcode;
357 if (icmplen < ICMP_ADVLENMIN || icmplen < ICMP_ADVLEN(icp) ||
358 icp->icmp_ip.ip_hl < (sizeof(struct ip) >> 2)) {
359 icmpstat.icps_badlen++;
360 break;
361 }
362 /*
363 * Short circuit routing redirects to force
364 * immediate change in the kernel's routing
365 * tables. The message is also handed to anyone
366 * listening on a raw socket (e.g. the routing
367 * daemon for use in updating its tables).
368 */
369 icmpgw.sin_addr = ip->ip_src;
370 icmpdst.sin_addr = icp->icmp_gwaddr;
371 #ifdef ICMPPRINTFS
372 if (icmpprintfs)
373 printf("redirect dst %x to %x\n", icp->icmp_ip.ip_dst,
374 icp->icmp_gwaddr);
375 #endif
376 icmpsrc.sin_addr = icp->icmp_ip.ip_dst;
377 rtredirect((struct sockaddr *)&icmpsrc,
378 (struct sockaddr *)&icmpdst,
379 (struct sockaddr *)0, RTF_GATEWAY | RTF_HOST,
380 (struct sockaddr *)&icmpgw, (struct rtentry **)0);
381 pfctlinput(PRC_REDIRECT_HOST, (struct sockaddr *)&icmpsrc);
382 break;
383
384 /*
385 * No kernel processing for the following;
386 * just fall through to send to raw listener.
387 */
388 case ICMP_ECHOREPLY:
389 case ICMP_ROUTERADVERT:
390 case ICMP_ROUTERSOLICIT:
391 case ICMP_TSTAMPREPLY:
392 case ICMP_IREQREPLY:
393 case ICMP_MASKREPLY:
394 default:
395 break;
396 }
397
398 raw:
399 rip_input(m);
400 return;
401
402 freeit:
403 m_freem(m);
404 }
405
406 /*
407 * Reflect the ip packet back to the source
408 */
409 void
410 icmp_reflect(m)
411 struct mbuf *m;
412 {
413 register struct ip *ip = mtod(m, struct ip *);
414 register struct in_ifaddr *ia;
415 struct in_addr t;
416 struct mbuf *opts = 0, *ip_srcroute();
417 int optlen = (ip->ip_hl << 2) - sizeof(struct ip);
418
419 if (!in_canforward(ip->ip_src) &&
420 ((ip->ip_src.s_addr & IN_CLASSA_NET) !=
421 htonl(IN_LOOPBACKNET << IN_CLASSA_NSHIFT))) {
422 m_freem(m); /* Bad return address */
423 goto done; /* ip_output() will check for broadcast */
424 }
425 t = ip->ip_dst;
426 ip->ip_dst = ip->ip_src;
427 /*
428 * If the incoming packet was addressed directly to us,
429 * use dst as the src for the reply. Otherwise (broadcast
430 * or anonymous), use the address which corresponds
431 * to the incoming interface.
432 */
433 for (ia = in_ifaddr; ia; ia = ia->ia_next) {
434 if (t.s_addr == IA_SIN(ia)->sin_addr.s_addr)
435 break;
436 if ((ia->ia_ifp->if_flags & IFF_BROADCAST) &&
437 t.s_addr == satosin(&ia->ia_broadaddr)->sin_addr.s_addr)
438 break;
439 }
440 icmpdst.sin_addr = t;
441 if (ia == (struct in_ifaddr *)0)
442 ia = (struct in_ifaddr *)ifaof_ifpforaddr(
443 (struct sockaddr *)&icmpdst, m->m_pkthdr.rcvif);
444 /*
445 * The following happens if the packet was not addressed to us,
446 * and was received on an interface with no IP address.
447 */
448 if (ia == (struct in_ifaddr *)0)
449 ia = in_ifaddr;
450 t = IA_SIN(ia)->sin_addr;
451 ip->ip_src = t;
452 ip->ip_ttl = MAXTTL;
453
454 if (optlen > 0) {
455 register u_char *cp;
456 int opt, cnt;
457 u_int len;
458
459 /*
460 * Retrieve any source routing from the incoming packet;
461 * add on any record-route or timestamp options.
462 */
463 cp = (u_char *) (ip + 1);
464 if ((opts = ip_srcroute()) == 0 &&
465 (opts = m_gethdr(M_DONTWAIT, MT_HEADER))) {
466 opts->m_len = sizeof(struct in_addr);
467 mtod(opts, struct in_addr *)->s_addr = 0;
468 }
469 if (opts) {
470 #ifdef ICMPPRINTFS
471 if (icmpprintfs)
472 printf("icmp_reflect optlen %d rt %d => ",
473 optlen, opts->m_len);
474 #endif
475 for (cnt = optlen; cnt > 0; cnt -= len, cp += len) {
476 opt = cp[IPOPT_OPTVAL];
477 if (opt == IPOPT_EOL)
478 break;
479 if (opt == IPOPT_NOP)
480 len = 1;
481 else {
482 len = cp[IPOPT_OLEN];
483 if (len <= 0 || len > cnt)
484 break;
485 }
486 /*
487 * Should check for overflow, but it "can't happen"
488 */
489 if (opt == IPOPT_RR || opt == IPOPT_TS ||
490 opt == IPOPT_SECURITY) {
491 bcopy((caddr_t)cp,
492 mtod(opts, caddr_t) + opts->m_len, len);
493 opts->m_len += len;
494 }
495 }
496 /* Terminate & pad, if necessary */
497 if (cnt = opts->m_len % 4) {
498 for (; cnt < 4; cnt++) {
499 *(mtod(opts, caddr_t) + opts->m_len) =
500 IPOPT_EOL;
501 opts->m_len++;
502 }
503 }
504 #ifdef ICMPPRINTFS
505 if (icmpprintfs)
506 printf("%d\n", opts->m_len);
507 #endif
508 }
509 /*
510 * Now strip out original options by copying rest of first
511 * mbuf's data back, and adjust the IP length.
512 */
513 ip->ip_len -= optlen;
514 ip->ip_hl = sizeof(struct ip) >> 2;
515 m->m_len -= optlen;
516 if (m->m_flags & M_PKTHDR)
517 m->m_pkthdr.len -= optlen;
518 optlen += sizeof(struct ip);
519 bcopy((caddr_t)ip + optlen, (caddr_t)(ip + 1),
520 (unsigned)(m->m_len - sizeof(struct ip)));
521 }
522 m->m_flags &= ~(M_BCAST|M_MCAST);
523 icmp_send(m, opts);
524 done:
525 if (opts)
526 (void)m_free(opts);
527 }
528
529 /*
530 * Send an icmp packet back to the ip level,
531 * after supplying a checksum.
532 */
533 void
534 icmp_send(m, opts)
535 register struct mbuf *m;
536 struct mbuf *opts;
537 {
538 register struct ip *ip = mtod(m, struct ip *);
539 register int hlen;
540 register struct icmp *icp;
541
542 hlen = ip->ip_hl << 2;
543 m->m_data += hlen;
544 m->m_len -= hlen;
545 icp = mtod(m, struct icmp *);
546 icp->icmp_cksum = 0;
547 icp->icmp_cksum = in_cksum(m, ip->ip_len - hlen);
548 m->m_data -= hlen;
549 m->m_len += hlen;
550 #ifdef ICMPPRINTFS
551 if (icmpprintfs)
552 printf("icmp_send dst %x src %x\n", ip->ip_dst, ip->ip_src);
553 #endif
554 (void) ip_output(m, opts, NULL, 0, NULL);
555 }
556
557 n_time
558 iptime()
559 {
560 struct timeval atv;
561 u_long t;
562
563 microtime(&atv);
564 t = (atv.tv_sec % (24*60*60)) * 1000 + atv.tv_usec / 1000;
565 return (htonl(t));
566 }
567
568 int
569 icmp_sysctl(name, namelen, oldp, oldlenp, newp, newlen)
570 int *name;
571 u_int namelen;
572 void *oldp;
573 size_t *oldlenp;
574 void *newp;
575 size_t newlen;
576 {
577
578 /* All sysctl names at this level are terminal. */
579 if (namelen != 1)
580 return (ENOTDIR);
581
582 switch (name[0]) {
583 case ICMPCTL_MASKREPL:
584 return (sysctl_int(oldp, oldlenp, newp, newlen, &icmpmaskrepl));
585 default:
586 return (ENOPROTOOPT);
587 }
588 /* NOTREACHED */
589 }
590