Home | History | Annotate | Line # | Download | only in libsa
net.c revision 1.6
      1 /*	$NetBSD: net.c,v 1.6 1995/09/14 23:45:26 pk 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 #include "netif.h"
     60 
     61 n_long	myip;
     62 
     63 /* Caller must leave room for ethernet, ip and udp headers in front!! */
     64 ssize_t
     65 sendudp(d, pkt, len)
     66 	register struct iodesc *d;
     67 	register void *pkt;
     68 	register size_t len;
     69 {
     70 	register ssize_t cc;
     71 	register struct ip *ip;
     72 	register struct udpiphdr *ui;
     73 	register struct udphdr *uh;
     74 	register u_char *ea;
     75 	struct ip tip;
     76 
     77 #ifdef NET_DEBUG
     78  	if (debug) {
     79 		printf("sendudp: d=%x called.\n", (u_int)d);
     80 		if (d) {
     81 			printf("saddr: %s:%d",
     82 				intoa(d->myip), d->myport);
     83 			printf(" daddr: %s:%d\n",
     84 				intoa(d->destip), d->destport);
     85 		}
     86 	}
     87 #endif
     88 
     89 	uh = (struct udphdr *)pkt - 1;
     90 	ip = (struct ip *)uh - 1;
     91 	len += sizeof(*ip) + sizeof(*uh);
     92 
     93 	bzero(ip, sizeof(*ip) + sizeof(*uh));
     94 
     95 	ip->ip_v = IPVERSION;			/* half-char */
     96 	ip->ip_hl = sizeof(*ip) >> 2;		/* half-char */
     97 	ip->ip_len = htons(len);
     98 	ip->ip_p = IPPROTO_UDP;			/* char */
     99 	ip->ip_ttl = IP_TTL;			/* char */
    100 	ip->ip_src.s_addr = htonl(d->myip);
    101 	ip->ip_dst.s_addr = htonl(d->destip);
    102 	ip->ip_sum = in_cksum(ip, sizeof(*ip));	 /* short, but special */
    103 
    104 	uh->uh_sport = htons(d->myport);
    105 	uh->uh_dport = htons(d->destport);
    106 	uh->uh_ulen = htons(len - sizeof(*ip));
    107 
    108 	/* Calculate checksum (must save and restore ip header) */
    109 	tip = *ip;
    110 	ui = (struct udpiphdr *)ip;
    111 	ui->ui_next = 0;
    112 	ui->ui_prev = 0;
    113 	ui->ui_x1 = 0;
    114 	ui->ui_len = uh->uh_ulen;
    115 	uh->uh_sum = in_cksum(ui, len);
    116 	*ip = tip;
    117 
    118 	if (ip->ip_dst.s_addr == INADDR_BROADCAST || ip->ip_src.s_addr == 0 ||
    119 	    mask == 0 || SAMENET(ip->ip_src.s_addr, ip->ip_dst.s_addr, mask))
    120 		ea = arpwhohas(d, ip->ip_dst.s_addr);
    121 	else
    122 		ea = arpwhohas(d, htonl(gateip));
    123 
    124 	cc = sendether(d, ip, len, ea, ETHERTYPE_IP);
    125 	if (cc == -1)
    126 		return (-1);
    127 	if (cc != len)
    128 		panic("sendudp: bad write (%d != %d)", cc, len);
    129 	return (cc - (sizeof(*ip) + sizeof(*uh)));
    130 }
    131 
    132 /*
    133  * Receive a UDP packet and validate it is for us.
    134  * Caller leaves room for the headers (Ether, IP, UDP)
    135  */
    136 ssize_t
    137 readudp(d, pkt, len, tleft)
    138 	register struct iodesc *d;
    139 	register void *pkt;
    140 	register size_t len;
    141 	time_t tleft;
    142 {
    143 	register ssize_t n;
    144 	register size_t hlen;
    145 	register struct ip *ip;
    146 	register struct udphdr *uh;
    147 	register struct udpiphdr *ui;
    148 	struct ip tip;
    149 	u_int16_t etype;	/* host order */
    150 
    151 #ifdef NET_DEBUG
    152 	if (debug)
    153 		printf("readudp: called\n");
    154 #endif
    155 
    156 	uh = (struct udphdr *)pkt - 1;
    157 	ip = (struct ip *)uh - 1;
    158 
    159 	n = readether(d, ip, len + sizeof(*ip) + sizeof(*uh), tleft, &etype);
    160 	if (n == -1 || n < sizeof(*ip) + sizeof(*uh))
    161 		goto bad;
    162 
    163 	/* Ethernet address checks now in readether() */
    164 
    165 	/* Need to respond to ARP requests. */
    166 	if (etype == ETHERTYPE_ARP) {
    167 		struct arphdr *ah = (void *)ip;
    168         if (ah->ar_op == htons(ARPOP_REQUEST)) {
    169 			/* Send ARP reply */
    170 			arp_reply(d, ah);
    171 		}
    172 		goto bad;
    173 	}
    174 
    175 	if (etype != ETHERTYPE_IP) {
    176 #ifdef NET_DEBUG
    177 		if (debug)
    178 			printf("readudp: not IP. ether_type=%x\n", etype);
    179 #endif
    180 		goto bad;
    181 	}
    182 
    183 	/* Check ip header */
    184 	if (ip->ip_v != IPVERSION ||
    185 	    ip->ip_p != IPPROTO_UDP) {	/* half char */
    186 #ifdef NET_DEBUG
    187 		if (debug)
    188 			printf("readudp: IP version or not UDP. ip_v=%d ip_p=%d\n", ip->ip_v, ip->ip_p);
    189 #endif
    190 		goto bad;
    191 	}
    192 
    193 	hlen = ip->ip_hl << 2;
    194 	if (hlen < sizeof(*ip) ||
    195 	    in_cksum(ip, hlen) != 0) {
    196 #ifdef NET_DEBUG
    197 		if (debug)
    198 			printf("readudp: short hdr or bad cksum.\n");
    199 #endif
    200 		goto bad;
    201 	}
    202 	NTOHS(ip->ip_len);
    203 	if (n < ip->ip_len) {
    204 #ifdef NET_DEBUG
    205 		if (debug)
    206 			printf("readudp: bad length %d < %d.\n", n, ip->ip_len);
    207 #endif
    208 		goto bad;
    209 	}
    210 	if (d->myip && ntohl(ip->ip_dst.s_addr) != d->myip) {
    211 #ifdef NET_DEBUG
    212 		if (debug) {
    213 			printf("readudp: bad saddr %s != ", intoa(d->myip));
    214 			printf("%s\n", intoa(ntohl(ip->ip_dst.s_addr)));
    215 		}
    216 #endif
    217 		goto bad;
    218 	}
    219 
    220 	/* If there were ip options, make them go away */
    221 	if (hlen != sizeof(*ip)) {
    222 		bcopy(((u_char *)ip) + hlen, uh, len - hlen);
    223 		ip->ip_len = sizeof(*ip);
    224 		n -= hlen - sizeof(*ip);
    225 	}
    226 	if (ntohs(uh->uh_dport) != d->myport) {
    227 #ifdef NET_DEBUG
    228 		if (debug)
    229 			printf("readudp: bad dport %d != %d\n",
    230 				d->myport, ntohs(uh->uh_dport));
    231 #endif
    232 		goto bad;
    233 	}
    234 
    235 	if (uh->uh_sum) {
    236 		n = ntohs(uh->uh_ulen) + sizeof(*ip);
    237 		if (n > RECV_SIZE - ETHER_SIZE) {
    238 			printf("readudp: huge packet, udp len %d\n", n);
    239 			goto bad;
    240 		}
    241 
    242 		/* Check checksum (must save and restore ip header) */
    243 		tip = *ip;
    244 		ui = (struct udpiphdr *)ip;
    245 		ui->ui_next = 0;
    246 		ui->ui_prev = 0;
    247 		ui->ui_x1 = 0;
    248 		ui->ui_len = uh->uh_ulen;
    249 		if (in_cksum(ui, n) != 0) {
    250 #ifdef NET_DEBUG
    251 			if (debug)
    252 				printf("readudp: bad cksum\n");
    253 #endif
    254 			*ip = tip;
    255 			goto bad;
    256 		}
    257 		*ip = tip;
    258 	}
    259 	NTOHS(uh->uh_dport);
    260 	NTOHS(uh->uh_sport);
    261 	NTOHS(uh->uh_ulen);
    262 	if (uh->uh_ulen < sizeof(*uh)) {
    263 #ifdef NET_DEBUG
    264 		if (debug)
    265 			printf("readudp: bad udp len %d < %d\n",
    266 				uh->uh_ulen, sizeof(*uh));
    267 #endif
    268 		goto bad;
    269 	}
    270 
    271 	n -= sizeof(*ip) + sizeof(*uh);
    272 	return (n);
    273 
    274 bad:
    275 	return (-1);
    276 }
    277 
    278 /*
    279  * Send a packet and wait for a reply, with exponential backoff.
    280  *
    281  * The send routine must return the actual number of bytes written.
    282  *
    283  * The receive routine can indicate success by returning the number of
    284  * bytes read; it can return 0 to indicate EOF; it can return -1 with a
    285  * non-zero errno to indicate failure; finally, it can return -1 with a
    286  * zero errno to indicate it isn't done yet.
    287  */
    288 ssize_t
    289 sendrecv(d, sproc, sbuf, ssize, rproc, rbuf, rsize)
    290 	register struct iodesc *d;
    291 	register ssize_t (*sproc)(struct iodesc *, void *, size_t);
    292 	register void *sbuf;
    293 	register size_t ssize;
    294 	register ssize_t (*rproc)(struct iodesc *, void *, size_t, time_t);
    295 	register void *rbuf;
    296 	register size_t rsize;
    297 {
    298 	register ssize_t cc;
    299 	register time_t t, tmo, tlast, tleft;
    300 
    301 #ifdef NET_DEBUG
    302 	if (debug)
    303 		printf("sendrecv: called\n");
    304 #endif
    305 
    306 	tmo = MINTMO;
    307 	tlast = tleft = 0;
    308 	t = getsecs();
    309 	for (;;) {
    310 		if (tleft <= 0) {
    311 			cc = (*sproc)(d, sbuf, ssize);
    312 			if (cc == -1 || cc < ssize)
    313 				panic("sendrecv: short write! (%d < %d)",
    314 				    cc, ssize);
    315 
    316 			tleft = tmo;
    317 			tmo <<= 1;
    318 			if (tmo > MAXTMO)
    319 				tmo = MAXTMO;
    320 			tlast = t;
    321 		}
    322 
    323 		/* Try to get a packet and process it. */
    324 		cc = (*rproc)(d, rbuf, rsize, tleft);
    325 		/* Return on data, EOF or real error. */
    326 		if (cc != -1 || errno != 0)
    327 			return (cc);
    328 
    329 		/* Timed out or didn't get the packet we're waiting for */
    330 		t = getsecs();
    331 		tleft -= t - tlast;
    332 		tlast = t;
    333 	}
    334 }
    335 
    336 /* Similar to inet_ntoa() */
    337 char *
    338 intoa(addr)
    339 	n_long addr;
    340 {
    341 	register char *cp;
    342 	register u_int byte;
    343 	register int n;
    344 	static char buf[17];	/* strlen(".255.255.255.255") + 1 */
    345 
    346 	cp = &buf[sizeof buf];
    347 	*--cp = '\0';
    348 
    349 	n = 4;
    350 	do {
    351 		byte = addr & 0xff;
    352 		*--cp = byte % 10 + '0';
    353 		byte /= 10;
    354 		if (byte > 0) {
    355 			*--cp = byte % 10 + '0';
    356 			byte /= 10;
    357 			if (byte > 0)
    358 				*--cp = byte + '0';
    359 		}
    360 		*--cp = '.';
    361 		addr >>= 8;
    362 	} while (--n > 0);
    363 
    364 	return (cp+1);
    365 }
    366 
    367 static char *
    368 number(s, n)
    369 	char *s;
    370 	int *n;
    371 {
    372 	for (*n = 0; isdigit(*s); s++)
    373 		*n = (*n * 10) + *s - '0';
    374 	return s;
    375 }
    376 
    377 n_long
    378 ip_convertaddr(p)
    379 	char *p;
    380 {
    381 #define IP_ANYADDR	0
    382 	n_long addr = 0, n;
    383 
    384 	if (p == (char *)0 || *p == '\0')
    385 		return IP_ANYADDR;
    386 	p = number(p, &n);
    387 	addr |= (n << 24) & 0xff000000;
    388 	if (*p == '\0' || *p++ != '.')
    389 		return IP_ANYADDR;
    390 	p = number(p, &n);
    391 	addr |= (n << 16) & 0xff0000;
    392 	if (*p == '\0' || *p++ != '.')
    393 		return IP_ANYADDR;
    394 	p = number(p, &n);
    395 	addr |= (n << 8) & 0xff00;
    396 	if (*p == '\0' || *p++ != '.')
    397 		return IP_ANYADDR;
    398 	p = number(p, &n);
    399 	addr |= n & 0xff;
    400 	if (*p != '\0')
    401 		return IP_ANYADDR;
    402 
    403 	return ntohl(addr);
    404 }
    405