Home | History | Annotate | Line # | Download | only in libsa
net.c revision 1.9
      1 /*	$NetBSD: net.c,v 1.9 1995/09/23 17:14:40 thorpej 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  * 3. All advertising materials mentioning features or use of this software
     20  *    must display the following acknowledgement:
     21  *	This product includes software developed by the University of
     22  *	California, Lawrence Berkeley Laboratory and its contributors.
     23  * 4. Neither the name of the University nor the names of its contributors
     24  *    may be used to endorse or promote products derived from this software
     25  *    without specific prior written permission.
     26  *
     27  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     28  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     29  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     30  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     31  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     32  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     33  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     34  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     35  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     36  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     37  * SUCH DAMAGE.
     38  *
     39  * @(#) Header: net.c,v 1.9 93/08/06 19:32:15 leres Exp  (LBL)
     40  */
     41 
     42 #include <sys/param.h>
     43 #include <sys/socket.h>
     44 
     45 #include <string.h>
     46 
     47 #include <net/if.h>
     48 
     49 #include <netinet/in.h>
     50 #include <netinet/if_ether.h>
     51 #include <netinet/in_systm.h>
     52 #include <netinet/ip.h>
     53 #include <netinet/ip_var.h>
     54 #include <netinet/udp.h>
     55 #include <netinet/udp_var.h>
     56 
     57 #include "stand.h"
     58 #include "net.h"
     59 
     60 /* Caller must leave room for ethernet, ip and udp headers in front!! */
     61 ssize_t
     62 sendudp(d, pkt, len)
     63 	register struct iodesc *d;
     64 	register void *pkt;
     65 	register size_t len;
     66 {
     67 	register ssize_t cc;
     68 	register struct ip *ip;
     69 	register struct udpiphdr *ui;
     70 	register struct udphdr *uh;
     71 	register u_char *ea;
     72 	struct ip tip;
     73 
     74 #ifdef NET_DEBUG
     75  	if (debug) {
     76 		printf("sendudp: d=%x called.\n", (u_int)d);
     77 		if (d) {
     78 			printf("saddr: %s:%d",
     79 				inet_ntoa(d->myip), ntohs(d->myport));
     80 			printf(" daddr: %s:%d\n",
     81 				inet_ntoa(d->destip), ntohs(d->destport));
     82 		}
     83 	}
     84 #endif
     85 
     86 	uh = (struct udphdr *)pkt - 1;
     87 	ip = (struct ip *)uh - 1;
     88 	len += sizeof(*ip) + sizeof(*uh);
     89 
     90 	bzero(ip, sizeof(*ip) + sizeof(*uh));
     91 
     92 	ip->ip_v = IPVERSION;			/* half-char */
     93 	ip->ip_hl = sizeof(*ip) >> 2;		/* half-char */
     94 	ip->ip_len = htons(len);
     95 	ip->ip_p = IPPROTO_UDP;			/* char */
     96 	ip->ip_ttl = IP_TTL;			/* char */
     97 	ip->ip_src = d->myip;
     98 	ip->ip_dst = d->destip;
     99 	ip->ip_sum = in_cksum(ip, sizeof(*ip));	 /* short, but special */
    100 
    101 	uh->uh_sport = d->myport;
    102 	uh->uh_dport = d->destport;
    103 	uh->uh_ulen = htons(len - sizeof(*ip));
    104 
    105 	/* Calculate checksum (must save and restore ip header) */
    106 	tip = *ip;
    107 	ui = (struct udpiphdr *)ip;
    108 	ui->ui_next = 0;
    109 	ui->ui_prev = 0;
    110 	ui->ui_x1 = 0;
    111 	ui->ui_len = uh->uh_ulen;
    112 	uh->uh_sum = in_cksum(ui, len);
    113 	*ip = tip;
    114 
    115 	if (ip->ip_dst.s_addr == INADDR_BROADCAST || ip->ip_src.s_addr == 0 ||
    116 	    netmask == 0 || SAMENET(ip->ip_src, ip->ip_dst, netmask))
    117 		ea = arpwhohas(d, ip->ip_dst);
    118 	else
    119 		ea = arpwhohas(d, gateip);
    120 
    121 	cc = sendether(d, ip, len, ea, ETHERTYPE_IP);
    122 	if (cc == -1)
    123 		return (-1);
    124 	if (cc != len)
    125 		panic("sendudp: bad write (%d != %d)", cc, len);
    126 	return (cc - (sizeof(*ip) + sizeof(*uh)));
    127 }
    128 
    129 /*
    130  * Receive a UDP packet and validate it is for us.
    131  * Caller leaves room for the headers (Ether, IP, UDP)
    132  */
    133 ssize_t
    134 readudp(d, pkt, len, tleft)
    135 	register struct iodesc *d;
    136 	register void *pkt;
    137 	register size_t len;
    138 	time_t tleft;
    139 {
    140 	register ssize_t n;
    141 	register size_t hlen;
    142 	register struct ip *ip;
    143 	register struct udphdr *uh;
    144 	register struct udpiphdr *ui;
    145 	struct ip tip;
    146 	u_int16_t etype;	/* host order */
    147 
    148 #ifdef NET_DEBUG
    149 	if (debug)
    150 		printf("readudp: called\n");
    151 #endif
    152 
    153 	uh = (struct udphdr *)pkt - 1;
    154 	ip = (struct ip *)uh - 1;
    155 
    156 	n = readether(d, ip, len + sizeof(*ip) + sizeof(*uh), tleft, &etype);
    157 	if (n == -1 || n < sizeof(*ip) + sizeof(*uh))
    158 		return -1;
    159 
    160 	/* Ethernet address checks now in readether() */
    161 
    162 	/* Need to respond to ARP requests. */
    163 	if (etype == ETHERTYPE_ARP) {
    164 		struct arphdr *ah = (void *)ip;
    165 		if (ah->ar_op == htons(ARPOP_REQUEST)) {
    166 			/* Send ARP reply */
    167 			arp_reply(d, ah);
    168 		}
    169 		return -1;
    170 	}
    171 
    172 	if (etype != ETHERTYPE_IP) {
    173 #ifdef NET_DEBUG
    174 		if (debug)
    175 			printf("readudp: not IP. ether_type=%x\n", etype);
    176 #endif
    177 		return -1;
    178 	}
    179 
    180 	/* Check ip header */
    181 	if (ip->ip_v != IPVERSION ||
    182 	    ip->ip_p != IPPROTO_UDP) {	/* half char */
    183 #ifdef NET_DEBUG
    184 		if (debug)
    185 			printf("readudp: IP version or not UDP. ip_v=%d ip_p=%d\n", ip->ip_v, ip->ip_p);
    186 #endif
    187 		return -1;
    188 	}
    189 
    190 	hlen = ip->ip_hl << 2;
    191 	if (hlen < sizeof(*ip) ||
    192 	    in_cksum(ip, hlen) != 0) {
    193 #ifdef NET_DEBUG
    194 		if (debug)
    195 			printf("readudp: short hdr or bad cksum.\n");
    196 #endif
    197 		return -1;
    198 	}
    199 	NTOHS(ip->ip_len);
    200 	if (n < ip->ip_len) {
    201 #ifdef NET_DEBUG
    202 		if (debug)
    203 			printf("readudp: bad length %d < %d.\n", n, ip->ip_len);
    204 #endif
    205 		return -1;
    206 	}
    207 	if (d->myip.s_addr && ip->ip_dst.s_addr != d->myip.s_addr) {
    208 #ifdef NET_DEBUG
    209 		if (debug) {
    210 			printf("readudp: bad saddr %s != ", inet_ntoa(d->myip));
    211 			printf("%s\n", inet_ntoa(ip->ip_dst));
    212 		}
    213 #endif
    214 		return -1;
    215 	}
    216 
    217 	/* If there were ip options, make them go away */
    218 	if (hlen != sizeof(*ip)) {
    219 		bcopy(((u_char *)ip) + hlen, uh, len - hlen);
    220 		ip->ip_len = sizeof(*ip);
    221 		n -= hlen - sizeof(*ip);
    222 	}
    223 	if (uh->uh_dport != d->myport) {
    224 #ifdef NET_DEBUG
    225 		if (debug)
    226 			printf("readudp: bad dport %d != %d\n",
    227 				d->myport, ntohs(uh->uh_dport));
    228 #endif
    229 		return -1;
    230 	}
    231 
    232 	if (uh->uh_sum) {
    233 		n = ntohs(uh->uh_ulen) + sizeof(*ip);
    234 		if (n > RECV_SIZE - ETHER_SIZE) {
    235 			printf("readudp: huge packet, udp len %d\n", n);
    236 			return -1;
    237 		}
    238 
    239 		/* Check checksum (must save and restore ip header) */
    240 		tip = *ip;
    241 		ui = (struct udpiphdr *)ip;
    242 		ui->ui_next = 0;
    243 		ui->ui_prev = 0;
    244 		ui->ui_x1 = 0;
    245 		ui->ui_len = uh->uh_ulen;
    246 		if (in_cksum(ui, n) != 0) {
    247 #ifdef NET_DEBUG
    248 			if (debug)
    249 				printf("readudp: bad cksum\n");
    250 #endif
    251 			*ip = tip;
    252 			return -1;
    253 		}
    254 		*ip = tip;
    255 	}
    256 	NTOHS(uh->uh_dport);
    257 	NTOHS(uh->uh_sport);
    258 	NTOHS(uh->uh_ulen);
    259 	if (uh->uh_ulen < sizeof(*uh)) {
    260 #ifdef NET_DEBUG
    261 		if (debug)
    262 			printf("readudp: bad udp len %d < %d\n",
    263 				uh->uh_ulen, sizeof(*uh));
    264 #endif
    265 		return -1;
    266 	}
    267 
    268 	n -= sizeof(*ip) + sizeof(*uh);
    269 	return (n);
    270 }
    271 
    272 /*
    273  * Send a packet and wait for a reply, with exponential backoff.
    274  *
    275  * The send routine must return the actual number of bytes written.
    276  *
    277  * The receive routine can indicate success by returning the number of
    278  * bytes read; it can return 0 to indicate EOF; it can return -1 with a
    279  * non-zero errno to indicate failure; finally, it can return -1 with a
    280  * zero errno to indicate it isn't done yet.
    281  */
    282 ssize_t
    283 sendrecv(d, sproc, sbuf, ssize, rproc, rbuf, rsize)
    284 	register struct iodesc *d;
    285 	register ssize_t (*sproc)(struct iodesc *, void *, size_t);
    286 	register void *sbuf;
    287 	register size_t ssize;
    288 	register ssize_t (*rproc)(struct iodesc *, void *, size_t, time_t);
    289 	register void *rbuf;
    290 	register size_t rsize;
    291 {
    292 	register ssize_t cc;
    293 	register time_t t, tmo, tlast, tleft;
    294 
    295 #ifdef NET_DEBUG
    296 	if (debug)
    297 		printf("sendrecv: called\n");
    298 #endif
    299 
    300 	tmo = MINTMO;
    301 	tlast = tleft = 0;
    302 	t = getsecs();
    303 	for (;;) {
    304 		if (tleft <= 0) {
    305 			if (tmo == MAXTMO) {
    306 				errno = ETIMEDOUT;
    307 				return -1;
    308 			}
    309 			cc = (*sproc)(d, sbuf, ssize);
    310 			if (cc == -1 || cc < ssize)
    311 				panic("sendrecv: short write! (%d < %d)",
    312 				    cc, ssize);
    313 
    314 			tleft = tmo;
    315 			tmo <<= 1;
    316 			if (tmo > MAXTMO)
    317 				tmo = MAXTMO;
    318 			tlast = t;
    319 		}
    320 
    321 		/* Try to get a packet and process it. */
    322 		cc = (*rproc)(d, rbuf, rsize, tleft);
    323 		/* Return on data, EOF or real error. */
    324 		if (cc != -1 || errno != 0)
    325 			return (cc);
    326 
    327 		/* Timed out or didn't get the packet we're waiting for */
    328 		t = getsecs();
    329 		tleft -= t - tlast;
    330 		tlast = t;
    331 	}
    332 }
    333 
    334 /*
    335  * Like inet_addr() in the C library, but we only accept base-10.
    336  * Return values are in network order.
    337  */
    338 n_long
    339 inet_addr(cp)
    340 	char *cp;
    341 {
    342 	register u_long val;
    343 	register int n;
    344 	register char c;
    345 	u_int parts[4];
    346 	register u_int *pp = parts;
    347 
    348 	for (;;) {
    349 		/*
    350 		 * Collect number up to ``.''.
    351 		 * Values are specified as for C:
    352 		 * 0x=hex, 0=octal, other=decimal.
    353 		 */
    354 		val = 0;
    355 		while ((c = *cp) != '\0') {
    356 			if (c >= '0' && c <= '9') {
    357 				val = (val * 10) + (c - '0');
    358 				cp++;
    359 				continue;
    360 			}
    361 			break;
    362 		}
    363 		if (*cp == '.') {
    364 			/*
    365 			 * Internet format:
    366 			 *	a.b.c.d
    367 			 *	a.b.c	(with c treated as 16-bits)
    368 			 *	a.b	(with b treated as 24 bits)
    369 			 */
    370 			if (pp >= parts + 3 || val > 0xff)
    371 				goto bad;
    372 			*pp++ = val, cp++;
    373 		} else
    374 			break;
    375 	}
    376 	/*
    377 	 * Check for trailing characters.
    378 	 */
    379 	if (*cp != '\0')
    380 		goto bad;
    381 
    382 	/*
    383 	 * Concoct the address according to
    384 	 * the number of parts specified.
    385 	 */
    386 	n = pp - parts + 1;
    387 	switch (n) {
    388 
    389 	case 1:				/* a -- 32 bits */
    390 		break;
    391 
    392 	case 2:				/* a.b -- 8.24 bits */
    393 		if (val > 0xffffff)
    394 			goto bad;
    395 		val |= parts[0] << 24;
    396 		break;
    397 
    398 	case 3:				/* a.b.c -- 8.8.16 bits */
    399 		if (val > 0xffff)
    400 			goto bad;
    401 		val |= (parts[0] << 24) | (parts[1] << 16);
    402 		break;
    403 
    404 	case 4:				/* a.b.c.d -- 8.8.8.8 bits */
    405 		if (val > 0xff)
    406 			goto bad;
    407 		val |= (parts[0] << 24) | (parts[1] << 16) | (parts[2] << 8);
    408 		break;
    409 	}
    410 
    411 	return (htonl(val));
    412  bad:
    413 	return (htonl(INADDR_NONE));
    414 }
    415 
    416 char *
    417 inet_ntoa(ia)
    418 	struct in_addr ia;
    419 {
    420 	return (intoa(ia.s_addr));
    421 }
    422 
    423 /* Similar to inet_ntoa() */
    424 char *
    425 intoa(addr)
    426 	register n_long addr;
    427 {
    428 	register char *cp;
    429 	register u_int byte;
    430 	register int n;
    431 	static char buf[17];	/* strlen(".255.255.255.255") + 1 */
    432 
    433 	NTOHL(addr);
    434 	cp = &buf[sizeof buf];
    435 	*--cp = '\0';
    436 
    437 	n = 4;
    438 	do {
    439 		byte = addr & 0xff;
    440 		*--cp = byte % 10 + '0';
    441 		byte /= 10;
    442 		if (byte > 0) {
    443 			*--cp = byte % 10 + '0';
    444 			byte /= 10;
    445 			if (byte > 0)
    446 				*--cp = byte + '0';
    447 		}
    448 		*--cp = '.';
    449 		addr >>= 8;
    450 	} while (--n > 0);
    451 
    452 	return (cp+1);
    453 }
    454 
    455 static char *
    456 number(s, n)
    457 	char *s;
    458 	int *n;
    459 {
    460 	for (*n = 0; isdigit(*s); s++)
    461 		*n = (*n * 10) + *s - '0';
    462 	return s;
    463 }
    464 
    465 n_long
    466 ip_convertaddr(p)
    467 	char *p;
    468 {
    469 #define IP_ANYADDR	0
    470 	n_long addr = 0, n;
    471 
    472 	if (p == (char *)0 || *p == '\0')
    473 		return IP_ANYADDR;
    474 	p = number(p, &n);
    475 	addr |= (n << 24) & 0xff000000;
    476 	if (*p == '\0' || *p++ != '.')
    477 		return IP_ANYADDR;
    478 	p = number(p, &n);
    479 	addr |= (n << 16) & 0xff0000;
    480 	if (*p == '\0' || *p++ != '.')
    481 		return IP_ANYADDR;
    482 	p = number(p, &n);
    483 	addr |= (n << 8) & 0xff00;
    484 	if (*p == '\0' || *p++ != '.')
    485 		return IP_ANYADDR;
    486 	p = number(p, &n);
    487 	addr |= n & 0xff;
    488 	if (*p != '\0')
    489 		return IP_ANYADDR;
    490 
    491 	return htonl(addr);
    492 }
    493