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