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