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