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