1 /* Taken from $NetBSD: net.c,v 1.20 1997/12/26 22:41:30 scottr Exp $ */ 2 3 /* 4 * Copyright (c) 1992 Regents of the University of California. 5 * All rights reserved. 6 * 7 * This software was developed by the Computer Systems Engineering group 8 * at Lawrence Berkeley Laboratory under DARPA contract BG 91-66 and 9 * contributed to Berkeley. 10 * 11 * Redistribution and use in source and binary forms, with or without 12 * modification, are permitted provided that the following conditions 13 * are met: 14 * 1. Redistributions of source code must retain the above copyright 15 * notice, this list of conditions and the following disclaimer. 16 * 2. Redistributions in binary form must reproduce the above copyright 17 * notice, this list of conditions and the following disclaimer in the 18 * documentation and/or other materials provided with the distribution. 19 * 4. Neither the name of the University nor the names of its contributors 20 * may be used to endorse or promote products derived from this software 21 * without specific prior written permission. 22 * 23 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 24 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 25 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 26 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 27 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 28 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 29 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 30 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 31 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 32 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 33 * SUCH DAMAGE. 34 * 35 * @(#) Header: net.c,v 1.9 93/08/06 19:32:15 leres Exp (LBL) 36 */ 37 38 #include <sys/cdefs.h> 39 40 #include <sys/param.h> 41 #include <sys/socket.h> 42 43 #include <string.h> 44 45 #include <net/if.h> 46 #include <netinet/in.h> 47 #include <netinet/if_ether.h> 48 #include <netinet/in_systm.h> 49 50 #include <netinet/in_pcb.h> 51 #include <netinet/ip.h> 52 #include <netinet/ip_var.h> 53 #include <netinet/udp.h> 54 #include <netinet/udp_var.h> 55 56 #include "stand.h" 57 #include "net.h" 58 59 /* Caller must leave room for ethernet, ip and udp headers in front!! */ 60 ssize_t 61 sendudp(struct iodesc *d, void *pkt, size_t len) 62 { 63 ssize_t cc; 64 struct ip *ip; 65 struct udphdr *uh; 66 u_char *ea; 67 68 #ifdef NET_DEBUG 69 if (debug) { 70 printf("sendudp: d=%lx called.\n", (long)d); 71 if (d) { 72 printf("saddr: %s:%d", 73 inet_ntoa(d->myip), ntohs(d->myport)); 74 printf(" daddr: %s:%d\n", 75 inet_ntoa(d->destip), ntohs(d->destport)); 76 } 77 } 78 #endif 79 80 uh = (struct udphdr *)pkt - 1; 81 ip = (struct ip *)uh - 1; 82 len += sizeof(*ip) + sizeof(*uh); 83 84 bzero(ip, sizeof(*ip) + sizeof(*uh)); 85 86 ip->ip_v = IPVERSION; /* half-char */ 87 ip->ip_hl = sizeof(*ip) >> 2; /* half-char */ 88 ip->ip_len = htons(len); 89 ip->ip_p = IPPROTO_UDP; /* char */ 90 ip->ip_ttl = IPDEFTTL; /* char */ 91 ip->ip_src = d->myip; 92 ip->ip_dst = d->destip; 93 ip->ip_sum = in_cksum(ip, sizeof(*ip)); /* short, but special */ 94 95 uh->uh_sport = d->myport; 96 uh->uh_dport = d->destport; 97 uh->uh_ulen = htons(len - sizeof(*ip)); 98 99 #ifndef UDP_NO_CKSUM 100 { 101 struct udpiphdr *ui; 102 struct ip tip; 103 104 /* Calculate checksum (must save and restore ip header) */ 105 tip = *ip; 106 ui = (struct udpiphdr *)ip; 107 bzero(&ui->ui_x1, sizeof(ui->ui_x1)); 108 ui->ui_len = uh->uh_ulen; 109 uh->uh_sum = in_cksum(ui, len); 110 *ip = tip; 111 } 112 #endif 113 114 if (ip->ip_dst.s_addr == INADDR_BROADCAST || ip->ip_src.s_addr == 0 || 115 netmask == 0 || SAMENET(ip->ip_src, ip->ip_dst, netmask)) 116 ea = arpwhohas(d, ip->ip_dst); 117 else 118 ea = arpwhohas(d, gateip); 119 120 cc = sendether(d, ip, len, ea, ETHERTYPE_IP); 121 if (cc == -1) 122 return (-1); 123 if (cc != len) 124 panic("sendudp: bad write (%zd != %zd)", cc, len); 125 return (cc - (sizeof(*ip) + sizeof(*uh))); 126 } 127 128 /* 129 * Receive a UDP packet and validate it is for us. 130 */ 131 ssize_t 132 readudp(struct iodesc *d, void **pkt, void **payload, time_t tleft) 133 { 134 ssize_t n; 135 size_t hlen; 136 struct ip *ip; 137 struct udphdr *uh; 138 uint16_t etype; /* host order */ 139 void *ptr; 140 141 #ifdef NET_DEBUG 142 if (debug) 143 printf("readudp: called\n"); 144 #endif 145 146 ip = NULL; 147 ptr = NULL; 148 n = readether(d, &ptr, (void **)&ip, tleft, &etype); 149 if (n == -1 || n < sizeof(*ip) + sizeof(*uh)) { 150 free(ptr); 151 return (-1); 152 } 153 154 /* Ethernet address checks now in readether() */ 155 156 /* Need to respond to ARP requests. */ 157 if (etype == ETHERTYPE_ARP) { 158 struct arphdr *ah = (void *)ip; 159 if (ah->ar_op == htons(ARPOP_REQUEST)) { 160 /* Send ARP reply */ 161 arp_reply(d, ah); 162 } 163 free(ptr); 164 return (-1); 165 } 166 167 if (etype != ETHERTYPE_IP) { 168 #ifdef NET_DEBUG 169 if (debug) 170 printf("readudp: not IP. ether_type=%x\n", etype); 171 #endif 172 free(ptr); 173 return (-1); 174 } 175 176 /* Check ip header */ 177 if (ip->ip_v != IPVERSION || 178 ip->ip_p != IPPROTO_UDP) { /* half char */ 179 #ifdef NET_DEBUG 180 if (debug) 181 printf("readudp: IP version or not UDP. ip_v=%d ip_p=%d\n", ip->ip_v, ip->ip_p); 182 #endif 183 free(ptr); 184 return (-1); 185 } 186 187 hlen = ip->ip_hl << 2; 188 if (hlen < sizeof(*ip) || 189 in_cksum(ip, hlen) != 0) { 190 #ifdef NET_DEBUG 191 if (debug) 192 printf("readudp: short hdr or bad cksum.\n"); 193 #endif 194 free(ptr); 195 return (-1); 196 } 197 if (n < ntohs(ip->ip_len)) { 198 #ifdef NET_DEBUG 199 if (debug) 200 printf("readudp: bad length %d < %d.\n", 201 (int)n, ntohs(ip->ip_len)); 202 #endif 203 free(ptr); 204 return (-1); 205 } 206 if (d->myip.s_addr && ip->ip_dst.s_addr != d->myip.s_addr) { 207 #ifdef NET_DEBUG 208 if (debug) { 209 printf("readudp: bad saddr %s != ", inet_ntoa(d->myip)); 210 printf("%s\n", inet_ntoa(ip->ip_dst)); 211 } 212 #endif 213 free(ptr); 214 return (-1); 215 } 216 217 uh = (struct udphdr *)((uintptr_t)ip + sizeof (*ip)); 218 /* If there were ip options, make them go away */ 219 if (hlen != sizeof(*ip)) { 220 bcopy(((u_char *)ip) + hlen, uh, uh->uh_ulen - hlen); 221 ip->ip_len = htons(sizeof(*ip)); 222 n -= hlen - sizeof(*ip); 223 } 224 if (uh->uh_dport != d->myport) { 225 #ifdef NET_DEBUG 226 if (debug) 227 printf("readudp: bad dport %d != %d\n", 228 d->myport, ntohs(uh->uh_dport)); 229 #endif 230 free(ptr); 231 return (-1); 232 } 233 234 #ifndef UDP_NO_CKSUM 235 if (uh->uh_sum) { 236 struct udpiphdr *ui; 237 struct ip tip; 238 239 n = ntohs(uh->uh_ulen) + sizeof(*ip); 240 if (n > RECV_SIZE - ETHER_SIZE) { 241 printf("readudp: huge packet, udp len %d\n", (int)n); 242 free(ptr); 243 return (-1); 244 } 245 246 /* Check checksum (must save and restore ip header) */ 247 tip = *ip; 248 ui = (struct udpiphdr *)ip; 249 bzero(&ui->ui_x1, sizeof(ui->ui_x1)); 250 ui->ui_len = uh->uh_ulen; 251 if (in_cksum(ui, n) != 0) { 252 #ifdef NET_DEBUG 253 if (debug) 254 printf("readudp: bad cksum\n"); 255 #endif 256 free(ptr); 257 return (-1); 258 } 259 *ip = tip; 260 } 261 #endif 262 if (ntohs(uh->uh_ulen) < sizeof(*uh)) { 263 #ifdef NET_DEBUG 264 if (debug) 265 printf("readudp: bad udp len %d < %d\n", 266 ntohs(uh->uh_ulen), (int)sizeof(*uh)); 267 #endif 268 free(ptr); 269 return (-1); 270 } 271 272 n = (n > (ntohs(uh->uh_ulen) - sizeof(*uh))) ? 273 ntohs(uh->uh_ulen) - sizeof(*uh) : n; 274 *pkt = ptr; 275 *payload = (void *)((uintptr_t)uh + sizeof(*uh)); 276 return (n); 277 } 278