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