1 /* 2 * Copyright (c) 1982, 1986 Regents of the University of California. 3 * All rights reserved. 4 * 5 * Redistribution and use in source and binary forms are permitted 6 * provided that the above copyright notice and this paragraph are 7 * duplicated in all such forms and that any documentation, 8 * advertising materials, and other materials related to such 9 * distribution and use acknowledge that the software was developed 10 * by the University of California, Berkeley. The name of the 11 * University may not be used to endorse or promote products derived 12 * from this software without specific prior written permission. 13 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR 14 * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED 15 * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE. 16 * 17 * @(#)ip_icmp.c 7.9 (Berkeley) 10/12/88 18 */ 19 20 #include "param.h" 21 #include "systm.h" 22 #include "malloc.h" 23 #include "mbuf.h" 24 #include "protosw.h" 25 #include "socket.h" 26 #include "time.h" 27 #include "kernel.h" 28 29 #include "../net/route.h" 30 #include "../net/if.h" 31 32 #include "in.h" 33 #include "in_systm.h" 34 #include "in_var.h" 35 #include "ip.h" 36 #include "ip_icmp.h" 37 #include "icmp_var.h" 38 39 #ifdef ICMPPRINTFS 40 /* 41 * ICMP routines: error generation, receive packet processing, and 42 * routines to turnaround packets back to the originator, and 43 * host table maintenance routines. 44 */ 45 int icmpprintfs = 0; 46 #endif 47 48 /* 49 * Generate an error packet of type error 50 * in response to bad packet ip. 51 */ 52 /*VARARGS3*/ 53 icmp_error(n, type, code, dest) 54 struct mbuf *n; 55 int type, code; 56 struct in_addr dest; 57 { 58 register struct ip *oip = mtod(n, struct ip *), *nip; 59 register unsigned oiplen = oip->ip_hl << 2; 60 register struct icmp *icp; 61 register struct mbuf *m; 62 unsigned icmplen; 63 64 #ifdef ICMPPRINTFS 65 if (icmpprintfs) 66 printf("icmp_error(%x, %d, %d)\n", oip, type, code); 67 #endif 68 if (type != ICMP_REDIRECT) 69 icmpstat.icps_error++; 70 /* 71 * Don't send error if not the first fragment of message. 72 * Don't error if the old packet protocol was ICMP 73 * error message, only known informational types. 74 */ 75 if (oip->ip_off &~ (IP_MF|IP_DF)) 76 goto free; 77 if (oip->ip_p == IPPROTO_ICMP && type != ICMP_REDIRECT && 78 !ICMP_INFOTYPE(((struct icmp *)((caddr_t)oip + oiplen))->icmp_type)) { 79 icmpstat.icps_oldicmp++; 80 goto free; 81 } 82 83 /* 84 * First, formulate icmp message 85 */ 86 m = m_gethdr(M_DONTWAIT, MT_HEADER); 87 if (m == NULL) 88 goto free; 89 icmplen = oiplen + min(8, oip->ip_len); 90 m->m_len = icmplen + ICMP_MINLEN; 91 MH_ALIGN(m, m->m_len); 92 icp = mtod(m, struct icmp *); 93 if ((u_int)type > ICMP_MAXTYPE) 94 panic("icmp_error"); 95 icmpstat.icps_outhist[type]++; 96 icp->icmp_type = type; 97 if (type == ICMP_REDIRECT) 98 icp->icmp_gwaddr = dest; 99 else 100 icp->icmp_void = 0; 101 if (type == ICMP_PARAMPROB) { 102 icp->icmp_pptr = code; 103 code = 0; 104 } 105 icp->icmp_code = code; 106 bcopy((caddr_t)oip, (caddr_t)&icp->icmp_ip, icmplen); 107 nip = &icp->icmp_ip; 108 nip->ip_len = htons((u_short)(nip->ip_len + oiplen)); 109 110 /* 111 * Now, copy old ip header in front of icmp message. 112 */ 113 if (m->m_len + oiplen > MHLEN) 114 oiplen = sizeof(struct ip); 115 if (m->m_data - oiplen < m->m_pktdat) 116 panic("icmp len"); 117 m->m_data -= oiplen; 118 m->m_len += oiplen; 119 m->m_pkthdr.len = m->m_len; 120 m->m_pkthdr.rcvif = n->m_pkthdr.rcvif; 121 nip = mtod(m, struct ip *); 122 bcopy((caddr_t)oip, (caddr_t)nip, oiplen); 123 nip->ip_len = m->m_len; 124 nip->ip_p = IPPROTO_ICMP; 125 icmp_reflect(m); 126 127 free: 128 m_freem(n); 129 } 130 131 static struct sockproto icmproto = { AF_INET, IPPROTO_ICMP }; 132 static struct sockaddr_in icmpsrc = { AF_INET }; 133 static struct sockaddr_in icmpdst = { AF_INET }; 134 static struct sockaddr_in icmpgw = { AF_INET }; 135 struct in_ifaddr *ifptoia(); 136 137 /* 138 * Process a received ICMP message. 139 */ 140 icmp_input(m, hlen) 141 register struct mbuf *m; 142 int hlen; 143 { 144 register struct icmp *icp; 145 register struct ip *ip = mtod(m, struct ip *); 146 int icmplen = ip->ip_len; 147 register int i; 148 struct in_ifaddr *ia; 149 int (*ctlfunc)(), code; 150 extern u_char ip_protox[]; 151 extern struct in_addr in_makeaddr(); 152 153 /* 154 * Locate icmp structure in mbuf, and check 155 * that not corrupted and of at least minimum length. 156 */ 157 #ifdef ICMPPRINTFS 158 if (icmpprintfs) 159 printf("icmp_input from %x, len %d\n", ip->ip_src, icmplen); 160 #endif 161 if (icmplen < ICMP_MINLEN) { 162 icmpstat.icps_tooshort++; 163 goto free; 164 } 165 i = hlen + MIN(icmplen, ICMP_ADVLENMIN); 166 if (m->m_len < i && (m = m_pullup(m, i)) == 0) { 167 icmpstat.icps_tooshort++; 168 return; 169 } 170 ip = mtod(m, struct ip *); 171 m->m_len -= hlen; 172 m->m_data += hlen; 173 icp = mtod(m, struct icmp *); 174 if (in_cksum(m, icmplen)) { 175 icmpstat.icps_checksum++; 176 goto free; 177 } 178 m->m_len += hlen; 179 m->m_data -= hlen; 180 181 #ifdef ICMPPRINTFS 182 /* 183 * Message type specific processing. 184 */ 185 if (icmpprintfs) 186 printf("icmp_input, type %d code %d\n", icp->icmp_type, 187 icp->icmp_code); 188 #endif 189 if (icp->icmp_type > ICMP_MAXTYPE) 190 goto raw; 191 icmpstat.icps_inhist[icp->icmp_type]++; 192 code = icp->icmp_code; 193 switch (icp->icmp_type) { 194 195 case ICMP_UNREACH: 196 if (code > 5) 197 goto badcode; 198 code += PRC_UNREACH_NET; 199 goto deliver; 200 201 case ICMP_TIMXCEED: 202 if (code > 1) 203 goto badcode; 204 code += PRC_TIMXCEED_INTRANS; 205 goto deliver; 206 207 case ICMP_PARAMPROB: 208 if (code) 209 goto badcode; 210 code = PRC_PARAMPROB; 211 goto deliver; 212 213 case ICMP_SOURCEQUENCH: 214 if (code) 215 goto badcode; 216 code = PRC_QUENCH; 217 deliver: 218 /* 219 * Problem with datagram; advise higher level routines. 220 */ 221 icp->icmp_ip.ip_len = ntohs((u_short)icp->icmp_ip.ip_len); 222 if (icmplen < ICMP_ADVLENMIN || icmplen < ICMP_ADVLEN(icp)) { 223 icmpstat.icps_badlen++; 224 goto free; 225 } 226 #ifdef ICMPPRINTFS 227 if (icmpprintfs) 228 printf("deliver to protocol %d\n", icp->icmp_ip.ip_p); 229 #endif 230 icmpsrc.sin_addr = icp->icmp_ip.ip_dst; 231 if (ctlfunc = inetsw[ip_protox[icp->icmp_ip.ip_p]].pr_ctlinput) 232 (*ctlfunc)(code, (struct sockaddr *)&icmpsrc); 233 break; 234 235 badcode: 236 icmpstat.icps_badcode++; 237 break; 238 239 case ICMP_ECHO: 240 icp->icmp_type = ICMP_ECHOREPLY; 241 goto reflect; 242 243 case ICMP_TSTAMP: 244 if (icmplen < ICMP_TSLEN) { 245 icmpstat.icps_badlen++; 246 break; 247 } 248 icp->icmp_type = ICMP_TSTAMPREPLY; 249 icp->icmp_rtime = iptime(); 250 icp->icmp_ttime = icp->icmp_rtime; /* bogus, do later! */ 251 goto reflect; 252 253 case ICMP_IREQ: 254 #define satosin(sa) ((struct sockaddr_in *)(sa)) 255 if (in_netof(ip->ip_src) == 0 && 256 (ia = ifptoia(m->m_pkthdr.rcvif))) 257 ip->ip_src = in_makeaddr(in_netof(IA_SIN(ia)->sin_addr), 258 in_lnaof(ip->ip_src)); 259 icp->icmp_type = ICMP_IREQREPLY; 260 goto reflect; 261 262 case ICMP_MASKREQ: 263 if (icmplen < ICMP_MASKLEN || 264 (ia = ifptoia(m->m_pkthdr.rcvif)) == 0) 265 break; 266 icp->icmp_type = ICMP_MASKREPLY; 267 icp->icmp_mask = htonl(ia->ia_subnetmask); 268 if (ip->ip_src.s_addr == 0) { 269 if (ia->ia_ifp->if_flags & IFF_BROADCAST) 270 ip->ip_src = satosin(&ia->ia_broadaddr)->sin_addr; 271 else if (ia->ia_ifp->if_flags & IFF_POINTOPOINT) 272 ip->ip_src = satosin(&ia->ia_dstaddr)->sin_addr; 273 } 274 reflect: 275 ip->ip_len += hlen; /* since ip_input deducts this */ 276 icmpstat.icps_reflect++; 277 icmpstat.icps_outhist[icp->icmp_type]++; 278 icmp_reflect(m); 279 return; 280 281 case ICMP_REDIRECT: 282 if (icmplen < ICMP_ADVLENMIN || icmplen < ICMP_ADVLEN(icp)) { 283 icmpstat.icps_badlen++; 284 break; 285 } 286 /* 287 * Short circuit routing redirects to force 288 * immediate change in the kernel's routing 289 * tables. The message is also handed to anyone 290 * listening on a raw socket (e.g. the routing 291 * daemon for use in updating its tables). 292 */ 293 icmpgw.sin_addr = ip->ip_src; 294 icmpdst.sin_addr = icp->icmp_gwaddr; 295 #ifdef ICMPPRINTFS 296 if (icmpprintfs) 297 printf("redirect dst %x to %x\n", icp->icmp_ip.ip_dst, 298 icp->icmp_gwaddr); 299 #endif 300 if (code == ICMP_REDIRECT_NET || code == ICMP_REDIRECT_TOSNET) { 301 icmpsrc.sin_addr = 302 in_makeaddr(in_netof(icp->icmp_ip.ip_dst), INADDR_ANY); 303 rtredirect((struct sockaddr *)&icmpsrc, 304 (struct sockaddr *)&icmpdst, RTF_GATEWAY, 305 (struct sockaddr *)&icmpgw); 306 icmpsrc.sin_addr = icp->icmp_ip.ip_dst; 307 pfctlinput(PRC_REDIRECT_NET, 308 (struct sockaddr *)&icmpsrc); 309 } else { 310 icmpsrc.sin_addr = icp->icmp_ip.ip_dst; 311 rtredirect((struct sockaddr *)&icmpsrc, 312 (struct sockaddr *)&icmpdst, RTF_GATEWAY | RTF_HOST, 313 (struct sockaddr *)&icmpgw); 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 raw_input(m, &icmproto, (struct sockaddr *)&icmpsrc, 335 (struct sockaddr *)&icmpdst); 336 return; 337 338 free: 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 /* 379 * Retrieve any source routing from the incoming packet 380 * and strip out other options. Adjust the IP length. 381 */ 382 opts = ip_srcroute(); 383 bcopy((caddr_t)ip + ip->ip_len, (caddr_t)ip + sizeof(struct ip), 384 (unsigned)m->m_len - ip->ip_len); 385 ip->ip_len -= optlen; 386 m->m_len -= optlen; 387 if (m->m_flags & M_PKTHDR) 388 m->m_pkthdr.len -= optlen; 389 } 390 icmp_send(m, opts); 391 if (opts) 392 (void)m_free(opts); 393 } 394 395 struct in_ifaddr * 396 ifptoia(ifp) 397 struct ifnet *ifp; 398 { 399 register struct in_ifaddr *ia; 400 401 for (ia = in_ifaddr; ia; ia = ia->ia_next) 402 if (ia->ia_ifp == ifp) 403 return (ia); 404 return ((struct in_ifaddr *)0); 405 } 406 407 /* 408 * Send an icmp packet back to the ip level, 409 * after supplying a checksum. 410 */ 411 icmp_send(m, opts) 412 register struct mbuf *m; 413 struct mbuf *opts; 414 { 415 register struct ip *ip = mtod(m, struct ip *); 416 register int hlen; 417 register struct icmp *icp; 418 419 hlen = ip->ip_hl << 2; 420 m->m_data += hlen; 421 m->m_len -= hlen; 422 icp = mtod(m, struct icmp *); 423 icp->icmp_cksum = 0; 424 icp->icmp_cksum = in_cksum(m, ip->ip_len - hlen); 425 m->m_data -= hlen; 426 m->m_len += hlen; 427 #ifdef ICMPPRINTFS 428 if (icmpprintfs) 429 printf("icmp_send dst %x src %x\n", ip->ip_dst, ip->ip_src); 430 #endif 431 (void) ip_output(m, opts, (struct route *)0, 0); 432 } 433 434 n_time 435 iptime() 436 { 437 struct timeval atv; 438 u_long t; 439 440 microtime(&atv); 441 t = (atv.tv_sec % (24*60*60)) * 1000 + atv.tv_usec / 1000; 442 return (htonl(t)); 443 } 444