| 1 | /* ip_icmp.c 6.11 85/05/27 */ |
| 2 | |
| 3 | #include "param.h" |
| 4 | #include "systm.h" |
| 5 | #include "mbuf.h" |
| 6 | #include "protosw.h" |
| 7 | #include "socket.h" |
| 8 | #include "time.h" |
| 9 | #include "kernel.h" |
| 10 | |
| 11 | #include "../net/route.h" |
| 12 | #include "../net/if.h" |
| 13 | |
| 14 | #include "in.h" |
| 15 | #include "in_systm.h" |
| 16 | #include "in_var.h" |
| 17 | #include "ip.h" |
| 18 | #include "ip_icmp.h" |
| 19 | #include "icmp_var.h" |
| 20 | |
| 21 | #ifdef ICMPPRINTFS |
| 22 | /* |
| 23 | * ICMP routines: error generation, receive packet processing, and |
| 24 | * routines to turnaround packets back to the originator, and |
| 25 | * host table maintenance routines. |
| 26 | */ |
| 27 | int icmpprintfs = 0; |
| 28 | #endif |
| 29 | |
| 30 | /* |
| 31 | * Generate an error packet of type error |
| 32 | * in response to bad packet ip. |
| 33 | */ |
| 34 | icmp_error(oip, type, code) |
| 35 | struct ip *oip; |
| 36 | int type, code; |
| 37 | { |
| 38 | register unsigned oiplen = oip->ip_hl << 2; |
| 39 | register struct icmp *icp; |
| 40 | struct mbuf *m; |
| 41 | struct ip *nip; |
| 42 | |
| 43 | #ifdef ICMPPRINTFS |
| 44 | if (icmpprintfs) |
| 45 | printf("icmp_error(%x, %d, %d)\n", oip, type, code); |
| 46 | #endif |
| 47 | icmpstat.icps_error++; |
| 48 | /* |
| 49 | * Make sure that the old IP packet had 8 bytes of data to return; |
| 50 | * if not, don't bother. Also don't EVER error if the old |
| 51 | * packet protocol was ICMP. |
| 52 | */ |
| 53 | if (oip->ip_len < 8) { |
| 54 | icmpstat.icps_oldshort++; |
| 55 | goto free; |
| 56 | } |
| 57 | if (oip->ip_p == IPPROTO_ICMP) { |
| 58 | icmpstat.icps_oldicmp++; |
| 59 | goto free; |
| 60 | } |
| 61 | |
| 62 | /* |
| 63 | * First, formulate icmp message |
| 64 | */ |
| 65 | m = m_get(M_DONTWAIT, MT_HEADER); |
| 66 | if (m == NULL) |
| 67 | goto free; |
| 68 | m->m_len = oiplen + 8 + ICMP_MINLEN; |
| 69 | m->m_off = MMAXOFF - m->m_len; |
| 70 | icp = mtod(m, struct icmp *); |
| 71 | if ((u_int)type > ICMP_IREQREPLY) |
| 72 | panic("icmp_error"); |
| 73 | icmpstat.icps_outhist[type]++; |
| 74 | icp->icmp_type = type; |
| 75 | icp->icmp_void = 0; |
| 76 | if (type == ICMP_PARAMPROB) { |
| 77 | icp->icmp_pptr = code; |
| 78 | code = 0; |
| 79 | } |
| 80 | icp->icmp_code = code; |
| 81 | bcopy((caddr_t)oip, (caddr_t)&icp->icmp_ip, oiplen + 8); |
| 82 | nip = &icp->icmp_ip; |
| 83 | nip->ip_len += oiplen; |
| 84 | nip->ip_len = htons((u_short)nip->ip_len); |
| 85 | |
| 86 | /* |
| 87 | * Now, copy old ip header in front of icmp |
| 88 | * message. This allows us to reuse any source |
| 89 | * routing info present. |
| 90 | */ |
| 91 | m->m_off -= oiplen; |
| 92 | nip = mtod(m, struct ip *); |
| 93 | bcopy((caddr_t)oip, (caddr_t)nip, oiplen); |
| 94 | nip->ip_len = m->m_len + oiplen; |
| 95 | nip->ip_p = IPPROTO_ICMP; |
| 96 | /* icmp_send adds ip header to m_off and m_len, so we deduct here */ |
| 97 | m->m_off += oiplen; |
| 98 | icmp_reflect(nip); |
| 99 | |
| 100 | free: |
| 101 | m_freem(dtom(oip)); |
| 102 | } |
| 103 | |
| 104 | static struct sockproto icmproto = { AF_INET, IPPROTO_ICMP }; |
| 105 | static struct sockaddr_in icmpsrc = { AF_INET }; |
| 106 | static struct sockaddr_in icmpdst = { AF_INET }; |
| 107 | |
| 108 | /* |
| 109 | * Process a received ICMP message. |
| 110 | */ |
| 111 | icmp_input(m) |
| 112 | struct mbuf *m; |
| 113 | { |
| 114 | register struct icmp *icp; |
| 115 | register struct ip *ip = mtod(m, struct ip *); |
| 116 | int icmplen = ip->ip_len, hlen = ip->ip_hl << 2; |
| 117 | register int i; |
| 118 | int (*ctlfunc)(), code; |
| 119 | extern u_char ip_protox[]; |
| 120 | extern struct in_addr in_makeaddr(); |
| 121 | |
| 122 | /* |
| 123 | * Locate icmp structure in mbuf, and check |
| 124 | * that not corrupted and of at least minimum length. |
| 125 | */ |
| 126 | #ifdef ICMPPRINTFS |
| 127 | if (icmpprintfs) |
| 128 | printf("icmp_input from %x, len %d\n", ip->ip_src, icmplen); |
| 129 | #endif |
| 130 | if (icmplen < ICMP_MINLEN) { |
| 131 | icmpstat.icps_tooshort++; |
| 132 | goto free; |
| 133 | } |
| 134 | /* THIS LENGTH CHECK STILL MISSES ANY IP OPTIONS IN ICMP_IP */ |
| 135 | i = MIN(icmplen, ICMP_ADVLENMIN + hlen); |
| 136 | if ((m->m_off > MMAXOFF || m->m_len < i) && |
| 137 | (m = m_pullup(m, i)) == 0) { |
| 138 | icmpstat.icps_tooshort++; |
| 139 | return; |
| 140 | } |
| 141 | ip = mtod(m, struct ip *); |
| 142 | m->m_len -= hlen; |
| 143 | m->m_off += hlen; |
| 144 | icp = mtod(m, struct icmp *); |
| 145 | if (in_cksum(m, icmplen)) { |
| 146 | icmpstat.icps_checksum++; |
| 147 | goto free; |
| 148 | } |
| 149 | |
| 150 | #ifdef ICMPPRINTFS |
| 151 | /* |
| 152 | * Message type specific processing. |
| 153 | */ |
| 154 | if (icmpprintfs) |
| 155 | printf("icmp_input, type %d code %d\n", icp->icmp_type, |
| 156 | icp->icmp_code); |
| 157 | #endif |
| 158 | if (icp->icmp_type > ICMP_IREQREPLY) |
| 159 | goto free; |
| 160 | icmpstat.icps_inhist[icp->icmp_type]++; |
| 161 | code = icp->icmp_code; |
| 162 | switch (icp->icmp_type) { |
| 163 | |
| 164 | case ICMP_UNREACH: |
| 165 | if (code > 5) |
| 166 | goto badcode; |
| 167 | code += PRC_UNREACH_NET; |
| 168 | goto deliver; |
| 169 | |
| 170 | case ICMP_TIMXCEED: |
| 171 | if (code > 1) |
| 172 | goto badcode; |
| 173 | code += PRC_TIMXCEED_INTRANS; |
| 174 | goto deliver; |
| 175 | |
| 176 | case ICMP_PARAMPROB: |
| 177 | if (code) |
| 178 | goto badcode; |
| 179 | code = PRC_PARAMPROB; |
| 180 | goto deliver; |
| 181 | |
| 182 | case ICMP_SOURCEQUENCH: |
| 183 | if (code) |
| 184 | goto badcode; |
| 185 | code = PRC_QUENCH; |
| 186 | deliver: |
| 187 | /* |
| 188 | * Problem with datagram; advise higher level routines. |
| 189 | */ |
| 190 | icp->icmp_ip.ip_len = ntohs((u_short)icp->icmp_ip.ip_len); |
| 191 | if (icmplen < ICMP_ADVLENMIN || icmplen < ICMP_ADVLEN(icp)) { |
| 192 | icmpstat.icps_badlen++; |
| 193 | goto free; |
| 194 | } |
| 195 | #ifdef ICMPPRINTFS |
| 196 | if (icmpprintfs) |
| 197 | printf("deliver to protocol %d\n", icp->icmp_ip.ip_p); |
| 198 | #endif |
| 199 | if (ctlfunc = inetsw[ip_protox[icp->icmp_ip.ip_p]].pr_ctlinput) |
| 200 | (*ctlfunc)(code, (caddr_t)icp); |
| 201 | goto free; |
| 202 | |
| 203 | badcode: |
| 204 | icmpstat.icps_badcode++; |
| 205 | goto free; |
| 206 | |
| 207 | case ICMP_ECHO: |
| 208 | icp->icmp_type = ICMP_ECHOREPLY; |
| 209 | goto reflect; |
| 210 | |
| 211 | case ICMP_TSTAMP: |
| 212 | if (icmplen < ICMP_TSLEN) { |
| 213 | icmpstat.icps_badlen++; |
| 214 | goto free; |
| 215 | } |
| 216 | icp->icmp_type = ICMP_TSTAMPREPLY; |
| 217 | icp->icmp_rtime = iptime(); |
| 218 | icp->icmp_ttime = icp->icmp_rtime; /* bogus, do later! */ |
| 219 | goto reflect; |
| 220 | |
| 221 | case ICMP_IREQ: |
| 222 | #define satosin(sa) ((struct sockaddr_in *)(sa)) |
| 223 | if (in_netof(ip->ip_src) == 0) |
| 224 | ip->ip_src = in_makeaddr( |
| 225 | in_netof(satosin(&in_ifaddr->ia_addr)->sin_addr), |
| 226 | in_lnaof(ip->ip_src)); |
| 227 | icp->icmp_type = ICMP_IREQREPLY; |
| 228 | goto reflect; |
| 229 | |
| 230 | case ICMP_REDIRECT: |
| 231 | if (icmplen < ICMP_ADVLENMIN || icmplen < ICMP_ADVLEN(icp)) { |
| 232 | icmpstat.icps_badlen++; |
| 233 | goto free; |
| 234 | } |
| 235 | /* |
| 236 | * Short circuit routing redirects to force |
| 237 | * immediate change in the kernel's routing |
| 238 | * tables. The message is also handed to anyone |
| 239 | * listening on a raw socket (e.g. the routing |
| 240 | * daemon for use in updating its tables). |
| 241 | */ |
| 242 | icmpdst.sin_addr = icp->icmp_gwaddr; |
| 243 | if (code == ICMP_REDIRECT_NET || code == ICMP_REDIRECT_TOSNET) { |
| 244 | icmpsrc.sin_addr = |
| 245 | in_makeaddr(in_netof(icp->icmp_ip.ip_dst), INADDR_ANY); |
| 246 | rtredirect((struct sockaddr *)&icmpsrc, |
| 247 | (struct sockaddr *)&icmpdst, RTF_GATEWAY); |
| 248 | pfctlinput(PRC_REDIRECT_NET, (caddr_t)icp); |
| 249 | } else { |
| 250 | icmpsrc.sin_addr = icp->icmp_ip.ip_dst; |
| 251 | rtredirect((struct sockaddr *)&icmpsrc, |
| 252 | (struct sockaddr *)&icmpdst, RTF_GATEWAY | RTF_HOST); |
| 253 | pfctlinput(PRC_REDIRECT_HOST, (caddr_t)icp); |
| 254 | } |
| 255 | /* FALL THROUGH */ |
| 256 | |
| 257 | case ICMP_ECHOREPLY: |
| 258 | case ICMP_TSTAMPREPLY: |
| 259 | case ICMP_IREQREPLY: |
| 260 | icmpsrc.sin_addr = ip->ip_src; |
| 261 | icmpdst.sin_addr = ip->ip_dst; |
| 262 | raw_input(dtom(icp), &icmproto, (struct sockaddr *)&icmpsrc, |
| 263 | (struct sockaddr *)&icmpdst); |
| 264 | return; |
| 265 | |
| 266 | default: |
| 267 | goto free; |
| 268 | } |
| 269 | reflect: |
| 270 | ip->ip_len += hlen; /* since ip_input deducts this */ |
| 271 | icmpstat.icps_reflect++; |
| 272 | icmpstat.icps_outhist[icp->icmp_type]++; |
| 273 | icmp_reflect(ip); |
| 274 | return; |
| 275 | free: |
| 276 | m_freem(dtom(ip)); |
| 277 | } |
| 278 | |
| 279 | /* |
| 280 | * Reflect the ip packet back to the source |
| 281 | * TODO: rearrange ip source routing options. |
| 282 | */ |
| 283 | icmp_reflect(ip) |
| 284 | struct ip *ip; |
| 285 | { |
| 286 | register struct in_addr t; |
| 287 | register struct in_ifaddr *ia; |
| 288 | |
| 289 | t = ip->ip_dst; |
| 290 | if (t.s_addr == INADDR_ANY) |
| 291 | t = IA_SIN(in_ifaddr)->sin_addr; |
| 292 | else for (ia = in_ifaddr; ia; ia = ia->ia_next) |
| 293 | if (t.s_addr == satosin(&ia->ia_broadaddr)->sin_addr.s_addr && |
| 294 | (ia->ia_ifp->if_flags & IFF_BROADCAST)) { |
| 295 | t = IA_SIN(ia)->sin_addr; |
| 296 | break; |
| 297 | } |
| 298 | ip->ip_dst = ip->ip_src; |
| 299 | ip->ip_src = t; |
| 300 | icmp_send(ip); |
| 301 | } |
| 302 | |
| 303 | /* |
| 304 | * Send an icmp packet back to the ip level, |
| 305 | * after supplying a checksum. |
| 306 | */ |
| 307 | icmp_send(ip) |
| 308 | struct ip *ip; |
| 309 | { |
| 310 | register int hlen; |
| 311 | register struct icmp *icp; |
| 312 | register struct mbuf *m; |
| 313 | |
| 314 | m = dtom(ip); |
| 315 | hlen = ip->ip_hl << 2; |
| 316 | icp = mtod(m, struct icmp *); |
| 317 | icp->icmp_cksum = 0; |
| 318 | icp->icmp_cksum = in_cksum(m, ip->ip_len - hlen); |
| 319 | m->m_off -= hlen; |
| 320 | m->m_len += hlen; |
| 321 | #ifdef ICMPPRINTFS |
| 322 | if (icmpprintfs) |
| 323 | printf("icmp_send dst %x src %x\n", ip->ip_dst, ip->ip_src); |
| 324 | #endif |
| 325 | (void) ip_output(m, (struct mbuf *)0, (struct route *)0, 0); |
| 326 | } |
| 327 | |
| 328 | n_time |
| 329 | iptime() |
| 330 | { |
| 331 | int s = spl6(); |
| 332 | u_long t; |
| 333 | |
| 334 | t = (time.tv_sec % (24*60*60)) * 1000 + time.tv_usec / 1000; |
| 335 | splx(s); |
| 336 | return (htonl(t)); |
| 337 | } |