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