1 /* 2 * Copyright (c) 1992 Regents of the University of California. 3 * All rights reserved. 4 * 5 * This software was developed by the Computer Systems Engineering group 6 * at Lawrence Berkeley Laboratory under DARPA contract BG 91-66 and 7 * contributed to Berkeley. 8 * 9 * Redistribution and use in source and binary forms, with or without 10 * modification, are permitted provided that the following conditions 11 * are met: 12 * 1. Redistributions of source code must retain the above copyright 13 * notice, this list of conditions and the following disclaimer. 14 * 2. Redistributions in binary form must reproduce the above copyright 15 * notice, this list of conditions and the following disclaimer in the 16 * documentation and/or other materials provided with the distribution. 17 * 3. Neither the name of the University nor the names of its contributors 18 * may be used to endorse or promote products derived from this software 19 * without specific prior written permission. 20 * 21 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 24 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 31 * SUCH DAMAGE. 32 */ 33 34 /* 35 * The send and receive functions were originally implemented in udp.c and 36 * moved here. Also it is likely some more cleanup can be done, especially 37 * once we will implement the support for tcp. 38 */ 39 40 #include <sys/cdefs.h> 41 42 #include <sys/param.h> 43 #include <sys/socket.h> 44 #include <sys/queue.h> 45 46 #include <string.h> 47 48 #include <net/if.h> 49 #include <netinet/in.h> 50 #include <netinet/if_ether.h> 51 #include <netinet/in_systm.h> 52 53 #include <netinet/ip.h> 54 #include <netinet/ip_var.h> 55 #include <netinet/udp.h> 56 #include <netinet/udp_var.h> 57 58 #include "stand.h" 59 #include "net.h" 60 61 typedef STAILQ_HEAD(ipqueue, ip_queue) ip_queue_t; 62 struct ip_queue { 63 void *ipq_pkt; 64 struct ip *ipq_hdr; 65 STAILQ_ENTRY(ip_queue) ipq_next; 66 }; 67 68 /* 69 * Fragment re-assembly queue. 70 */ 71 struct ip_reasm { 72 struct in_addr ip_src; 73 struct in_addr ip_dst; 74 uint16_t ip_id; 75 uint8_t ip_proto; 76 uint8_t ip_ttl; 77 size_t ip_total_size; 78 ip_queue_t ip_queue; 79 void *ip_pkt; 80 struct ip *ip_hdr; 81 STAILQ_ENTRY(ip_reasm) ip_next; 82 }; 83 84 STAILQ_HEAD(ire_list, ip_reasm) ire_list = STAILQ_HEAD_INITIALIZER(ire_list); 85 86 /* Caller must leave room for ethernet and ip headers in front!! */ 87 ssize_t 88 sendip(struct iodesc *d, void *pkt, size_t len, uint8_t proto) 89 { 90 ssize_t cc; 91 struct ip *ip; 92 uchar_t *ea; 93 94 #ifdef NET_DEBUG 95 if (debug) { 96 printf("sendip: proto: %x d=%p called.\n", proto, (void *)d); 97 if (d) { 98 printf("saddr: %s:%d", 99 inet_ntoa(d->myip), ntohs(d->myport)); 100 printf(" daddr: %s:%d\n", 101 inet_ntoa(d->destip), ntohs(d->destport)); 102 } 103 } 104 #endif 105 106 ip = (struct ip *)pkt - 1; 107 len += sizeof (*ip); 108 109 bzero(ip, sizeof (*ip)); 110 111 ip->ip_v = IPVERSION; /* half-char */ 112 ip->ip_hl = sizeof (*ip) >> 2; /* half-char */ 113 ip->ip_len = htons(len); 114 ip->ip_p = proto; /* char */ 115 ip->ip_ttl = IPDEFTTL; /* char */ 116 ip->ip_src = d->myip; 117 ip->ip_dst = d->destip; 118 ip->ip_sum = in_cksum(ip, sizeof (*ip)); /* short, but special */ 119 120 if (ip->ip_dst.s_addr == INADDR_BROADCAST || ip->ip_src.s_addr == 0 || 121 netmask == 0 || SAMENET(ip->ip_src, ip->ip_dst, netmask)) 122 ea = arpwhohas(d, ip->ip_dst); 123 else 124 ea = arpwhohas(d, gateip); 125 126 cc = sendether(d, ip, len, ea, ETHERTYPE_IP); 127 if (cc == -1) 128 return (-1); 129 if (cc != len) 130 panic("sendip: bad write (%zd != %zd)", cc, len); 131 return (cc - sizeof (*ip)); 132 } 133 134 static void 135 ip_reasm_free(struct ip_reasm *ipr) 136 { 137 struct ip_queue *ipq; 138 139 while ((ipq = STAILQ_FIRST(&ipr->ip_queue)) != NULL) { 140 STAILQ_REMOVE_HEAD(&ipr->ip_queue, ipq_next); 141 free(ipq->ipq_pkt); 142 free(ipq); 143 } 144 free(ipr->ip_pkt); 145 free(ipr); 146 } 147 148 static int 149 ip_reasm_add(struct ip_reasm *ipr, void *pkt, struct ip *ip) 150 { 151 struct ip_queue *ipq, *prev, *p; 152 153 if ((ipq = calloc(1, sizeof (*ipq))) == NULL) 154 return (1); 155 156 ipq->ipq_pkt = pkt; 157 ipq->ipq_hdr = ip; 158 159 prev = NULL; 160 STAILQ_FOREACH(p, &ipr->ip_queue, ipq_next) { 161 if ((ntohs(p->ipq_hdr->ip_off) & IP_OFFMASK) < 162 (ntohs(ip->ip_off) & IP_OFFMASK)) { 163 prev = p; 164 continue; 165 } 166 if (prev == NULL) 167 break; 168 169 STAILQ_INSERT_AFTER(&ipr->ip_queue, prev, ipq, ipq_next); 170 return (0); 171 } 172 STAILQ_INSERT_HEAD(&ipr->ip_queue, ipq, ipq_next); 173 return (0); 174 } 175 176 /* 177 * Receive a IP packet and validate it is for us. 178 */ 179 static ssize_t 180 readipv4(struct iodesc *d, void **pkt, void **payload, time_t tleft, 181 uint8_t proto) 182 { 183 ssize_t n; 184 size_t hlen; 185 struct ether_header *eh; 186 void *buf; 187 struct ip *ip; 188 struct udphdr *uh; 189 uint16_t etype; /* host order */ 190 char *ptr; 191 struct ip_reasm *ipr; 192 struct ip_queue *ipq, *last; 193 194 #ifdef NET_DEBUG 195 if (debug) 196 printf("readip: called\n"); 197 #endif 198 199 ip = NULL; 200 ptr = NULL; 201 n = readether(d, (void **)&ptr, (void **)&buf, tleft, &etype); 202 if (n == -1 || n < sizeof (*ip) + sizeof (*uh)) { 203 free(ptr); 204 return (-1); 205 } 206 207 /* Ethernet address checks now in readether() */ 208 209 /* Need to respond to ARP requests. */ 210 if (etype == ETHERTYPE_ARP) { 211 struct arphdr *ah = buf; 212 if (ah->ar_op == htons(ARPOP_REQUEST)) { 213 /* Send ARP reply */ 214 arp_reply(d, ah); 215 } 216 free(ptr); 217 errno = EAGAIN; /* Call me again. */ 218 return (-1); 219 } 220 221 if (etype != ETHERTYPE_IP) { 222 #ifdef NET_DEBUG 223 if (debug) 224 printf("readip: not IP. ether_type=%x\n", etype); 225 #endif 226 free(ptr); 227 return (-1); 228 } 229 230 ip = buf; 231 /* Check ip header */ 232 if (ip->ip_v != IPVERSION || /* half char */ 233 ip->ip_p != proto) { 234 #ifdef NET_DEBUG 235 if (debug) { 236 printf("readip: IP version or proto. ip_v=%d ip_p=%d\n", 237 ip->ip_v, ip->ip_p); 238 } 239 #endif 240 free(ptr); 241 return (-1); 242 } 243 244 hlen = ip->ip_hl << 2; 245 if (hlen < sizeof (*ip) || 246 in_cksum(ip, hlen) != 0) { 247 #ifdef NET_DEBUG 248 if (debug) 249 printf("readip: short hdr or bad cksum.\n"); 250 #endif 251 free(ptr); 252 return (-1); 253 } 254 if (n < ntohs(ip->ip_len)) { 255 #ifdef NET_DEBUG 256 if (debug) { 257 printf("readip: bad length %d < %d.\n", 258 (int)n, ntohs(ip->ip_len)); 259 } 260 #endif 261 free(ptr); 262 return (-1); 263 } 264 if (d->myip.s_addr && ip->ip_dst.s_addr != d->myip.s_addr) { 265 #ifdef NET_DEBUG 266 if (debug) { 267 printf("readip: bad saddr %s != ", inet_ntoa(d->myip)); 268 printf("%s\n", inet_ntoa(ip->ip_dst)); 269 } 270 #endif 271 free(ptr); 272 return (-1); 273 } 274 275 /* Unfragmented packet. */ 276 if ((ntohs(ip->ip_off) & IP_MF) == 0 && 277 (ntohs(ip->ip_off) & IP_OFFMASK) == 0) { 278 uh = (struct udphdr *)((uintptr_t)ip + sizeof (*ip)); 279 /* If there were ip options, make them go away */ 280 if (hlen != sizeof (*ip)) { 281 bcopy(((uchar_t *)ip) + hlen, uh, uh->uh_ulen - hlen); 282 ip->ip_len = htons(sizeof (*ip)); 283 n -= hlen - sizeof (*ip); 284 } 285 286 n = (n > (ntohs(ip->ip_len) - sizeof (*ip))) ? 287 ntohs(ip->ip_len) - sizeof (*ip) : n; 288 *pkt = ptr; 289 *payload = (void *)((uintptr_t)ip + sizeof (*ip)); 290 return (n); 291 } 292 293 STAILQ_FOREACH(ipr, &ire_list, ip_next) { 294 if (ipr->ip_src.s_addr == ip->ip_src.s_addr && 295 ipr->ip_dst.s_addr == ip->ip_dst.s_addr && 296 ipr->ip_id == ip->ip_id && 297 ipr->ip_proto == ip->ip_p) 298 break; 299 } 300 301 /* Allocate new reassembly entry */ 302 if (ipr == NULL) { 303 if ((ipr = calloc(1, sizeof (*ipr))) == NULL) { 304 free(ptr); 305 return (-1); 306 } 307 308 ipr->ip_src = ip->ip_src; 309 ipr->ip_dst = ip->ip_dst; 310 ipr->ip_id = ip->ip_id; 311 ipr->ip_proto = ip->ip_p; 312 ipr->ip_ttl = MAXTTL; 313 STAILQ_INIT(&ipr->ip_queue); 314 STAILQ_INSERT_TAIL(&ire_list, ipr, ip_next); 315 } 316 317 if (ip_reasm_add(ipr, ptr, ip) != 0) { 318 STAILQ_REMOVE(&ire_list, ipr, ip_reasm, ip_next); 319 free(ipr); 320 free(ptr); 321 return (-1); 322 } 323 324 if ((ntohs(ip->ip_off) & IP_MF) == 0) { 325 ipr->ip_total_size = (8 * (ntohs(ip->ip_off) & IP_OFFMASK)); 326 ipr->ip_total_size += n + sizeof (*ip); 327 ipr->ip_total_size += sizeof (struct ether_header); 328 329 ipr->ip_pkt = malloc(ipr->ip_total_size + 2); 330 if (ipr->ip_pkt == NULL) { 331 STAILQ_REMOVE(&ire_list, ipr, ip_reasm, ip_next); 332 ip_reasm_free(ipr); 333 return (-1); 334 } 335 } 336 337 /* 338 * If we do not have re-assembly buffer ipr->ip_pkt, we are still 339 * missing fragments, so just restart the read. 340 */ 341 if (ipr->ip_pkt == NULL) { 342 errno = EAGAIN; 343 return (-1); 344 } 345 346 /* 347 * Walk the packet list in reassembly queue, if we got all the 348 * fragments, build the packet. 349 */ 350 n = 0; 351 last = NULL; 352 STAILQ_FOREACH(ipq, &ipr->ip_queue, ipq_next) { 353 if ((ntohs(ipq->ipq_hdr->ip_off) & IP_OFFMASK) != n / 8) { 354 STAILQ_REMOVE(&ire_list, ipr, ip_reasm, ip_next); 355 ip_reasm_free(ipr); 356 return (-1); 357 } 358 359 n += ntohs(ipq->ipq_hdr->ip_len) - (ipq->ipq_hdr->ip_hl << 2); 360 last = ipq; 361 } 362 if ((ntohs(last->ipq_hdr->ip_off) & IP_MF) != 0) { 363 errno = EAGAIN; 364 return (-1); 365 } 366 367 ipq = STAILQ_FIRST(&ipr->ip_queue); 368 /* Fabricate ethernet header */ 369 eh = (struct ether_header *)((uintptr_t)ipr->ip_pkt + 2); 370 bcopy((void *)((uintptr_t)ipq->ipq_pkt + 2), eh, sizeof (*eh)); 371 372 /* Fabricate IP header */ 373 ipr->ip_hdr = (struct ip *)((uintptr_t)eh + sizeof (*eh)); 374 bcopy(ipq->ipq_hdr, ipr->ip_hdr, sizeof (*ipr->ip_hdr)); 375 ipr->ip_hdr->ip_hl = sizeof (*ipr->ip_hdr) >> 2; 376 ipr->ip_hdr->ip_len = htons(n); 377 ipr->ip_hdr->ip_sum = 0; 378 ipr->ip_hdr->ip_sum = in_cksum(ipr->ip_hdr, sizeof (*ipr->ip_hdr)); 379 380 n = 0; 381 ptr = (char *)((uintptr_t)ipr->ip_hdr + sizeof (*ipr->ip_hdr)); 382 STAILQ_FOREACH(ipq, &ipr->ip_queue, ipq_next) { 383 char *data; 384 size_t len; 385 386 hlen = ipq->ipq_hdr->ip_hl << 2; 387 len = ntohs(ipq->ipq_hdr->ip_len) - hlen; 388 data = (char *)((uintptr_t)ipq->ipq_hdr + hlen); 389 390 bcopy(data, ptr + n, len); 391 n += len; 392 } 393 394 *pkt = ipr->ip_pkt; 395 ipr->ip_pkt = NULL; /* Avoid free from ip_reasm_free() */ 396 *payload = ptr; 397 398 /* Clean up the reassembly list */ 399 while ((ipr = STAILQ_FIRST(&ire_list)) != NULL) { 400 STAILQ_REMOVE_HEAD(&ire_list, ip_next); 401 ip_reasm_free(ipr); 402 } 403 return (n); 404 } 405 406 /* 407 * Receive a IP packet. 408 */ 409 ssize_t 410 readip(struct iodesc *d, void **pkt, void **payload, time_t tleft, 411 uint8_t proto) 412 { 413 time_t t; 414 ssize_t ret = -1; 415 416 t = getsecs(); 417 while ((getsecs() - t) < tleft) { 418 errno = 0; 419 ret = readipv4(d, pkt, payload, tleft, proto); 420 if (ret >= 0) 421 return (ret); 422 /* Bubble up the error if it wasn't successful */ 423 if (errno != EAGAIN) 424 return (-1); 425 } 426 /* We've exhausted tleft; timeout */ 427 errno = ETIMEDOUT; 428 return (-1); 429 } 430