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