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