xref: /netbsd-src/sys/lib/libsa/net.c (revision 1f2744e6e4915c9da2a3f980279398c4cf7d5e6d)
1 /*	$NetBSD: net.c,v 1.4 1995/02/20 11:04:08 mycroft 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 size_t
65 sendudp(d, pkt, len)
66 	register struct iodesc *d;
67 	register void *pkt;
68 	register size_t len;
69 {
70 	register size_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 /* Check that packet is a valid udp packet for us */
133 size_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 size_t hlen;
141 	register struct ether_header *eh;
142 	register struct ip *ip;
143 	register struct udphdr *uh;
144 	register struct udpiphdr *ui;
145 	struct ip tip;
146 
147 #ifdef NET_DEBUG
148 	if (debug)
149 		printf("readudp: called\n");
150 #endif
151 
152 	uh = (struct udphdr *)pkt - 1;
153 	ip = (struct ip *)uh - 1;
154 
155 	len = readether(d, ip, len + sizeof(*ip) + sizeof(*uh), tleft);
156 	if (len == -1 || len < sizeof(*ip) + sizeof(*uh))
157 		goto bad;
158 
159 	eh = (struct ether_header *)ip - 1;
160 	/* Must be to us */
161 	if (bcmp(d->myea, eh->ether_dhost, 6) != 0 &&
162 	    bcmp(bcea, eh->ether_dhost, 6) != 0) {
163 #ifdef NET_DEBUG
164 		if (debug)
165 			printf("readudp: not ours. myea=%s bcea=%s\n",
166 				ether_sprintf(d->myea), ether_sprintf(bcea));
167 #endif
168 		goto bad;
169 	}
170 	if (ntohs(eh->ether_type) != ETHERTYPE_IP) {
171 #ifdef NET_DEBUG
172 		if (debug)
173 			printf("readudp: not IP. ether_type=%x\n", eh->ether_type);
174 #endif
175 		goto bad;
176 	}
177 
178 	/* Check ip header */
179 	if (ip->ip_v != IPVERSION ||
180 	    ip->ip_p != IPPROTO_UDP) {	/* half char */
181 #ifdef NET_DEBUG
182 		if (debug)
183 			printf("readudp: IP version or not UDP. ip_v=%d ip_p=%d\n", ip->ip_v, ip->ip_p);
184 #endif
185 		goto bad;
186 	}
187 
188 	hlen = ip->ip_hl << 2;
189 	if (hlen < sizeof(*ip) ||
190 	    in_cksum(ip, hlen) != 0) {
191 #ifdef NET_DEBUG
192 		if (debug)
193 			printf("readudp: short hdr or bad cksum.\n");
194 #endif
195 		goto bad;
196 	}
197 	NTOHS(ip->ip_len);
198 	if (len < ip->ip_len) {
199 #ifdef NET_DEBUG
200 		if (debug)
201 			printf("readudp: bad length %d < %d.\n",
202 				len, ip->ip_len);
203 #endif
204 		goto bad;
205 	}
206 	if (d->myip && ntohl(ip->ip_dst.s_addr) != d->myip) {
207 #ifdef NET_DEBUG
208 		if (debug) {
209 			printf("readudp: bad saddr %s != ", intoa(d->myip));
210 			printf("%s\n", intoa(ntohl(ip->ip_dst.s_addr)));
211 		}
212 #endif
213 		goto bad;
214 	}
215 
216 	/* If there were ip options, make them go away */
217 	if (hlen != sizeof(*ip)) {
218 		bcopy(((u_char *)ip) + hlen, uh, len - hlen);
219 		ip->ip_len = sizeof(*ip);
220 		len -= hlen - sizeof(*ip);
221 	}
222 	if (ntohs(uh->uh_dport) != d->myport) {
223 #ifdef NET_DEBUG
224 		if (debug)
225 			printf("readudp: bad dport %d != %d\n",
226 				d->myport, ntohs(uh->uh_dport));
227 #endif
228 		goto bad;
229 	}
230 
231 	if (uh->uh_sum) {
232 		len = ntohs(uh->uh_ulen) + sizeof(*ip);
233 		if (len > RECV_SIZE - sizeof(*eh)) {
234 			printf("readudp: huge packet, udp len %d\n", len);
235 			goto bad;
236 		}
237 
238 		/* Check checksum (must save and restore ip header) */
239 		tip = *ip;
240 		ui = (struct udpiphdr *)ip;
241 		ui->ui_next = 0;
242 		ui->ui_prev = 0;
243 		ui->ui_x1 = 0;
244 		ui->ui_len = uh->uh_ulen;
245 		if (in_cksum(ui, len) != 0) {
246 #ifdef NET_DEBUG
247 			if (debug)
248 				printf("readudp: bad cksum\n");
249 #endif
250 			*ip = tip;
251 			goto bad;
252 		}
253 		*ip = tip;
254 	}
255 	NTOHS(uh->uh_dport);
256 	NTOHS(uh->uh_sport);
257 	NTOHS(uh->uh_ulen);
258 	if (uh->uh_ulen < sizeof(*uh)) {
259 #ifdef NET_DEBUG
260 		if (debug)
261 			printf("readudp: bad udp len %d < %d\n",
262 				uh->uh_ulen, sizeof(*uh));
263 #endif
264 		goto bad;
265 	}
266 
267 	len -= sizeof(*ip) + sizeof(*uh);
268 	return (len);
269 
270 bad:
271 	return (-1);
272 }
273 
274 /*
275  * Send a packet and wait for a reply, with exponential backoff.
276  *
277  * The send routine must return the actual number of bytes written.
278  *
279  * The receive routine can indicate success by returning the number of
280  * bytes read; it can return 0 to indicate EOF; it can return -1 with a
281  * non-zero errno to indicate failure; finally, it can return -1 with a
282  * zero errno to indicate it isn't done yet.
283  */
284 size_t
285 sendrecv(d, sproc, sbuf, ssize, rproc, rbuf, rsize)
286 	register struct iodesc *d;
287 	register size_t (*sproc)(struct iodesc *, void *, size_t);
288 	register void *sbuf;
289 	register size_t ssize;
290 	register size_t (*rproc)(struct iodesc *, void *, size_t, time_t);
291 	register void *rbuf;
292 	register size_t rsize;
293 {
294 	register size_t cc;
295 	register time_t t, tmo, tlast, tleft;
296 
297 #ifdef NET_DEBUG
298 	if (debug)
299 		printf("sendrecv: called\n");
300 #endif
301 
302 	tmo = MINTMO;
303 	tlast = tleft = 0;
304 	t = getsecs();
305 	for (;;) {
306 		if (tleft <= 0) {
307 			cc = (*sproc)(d, sbuf, ssize);
308 			if (cc == -1 || cc < ssize)
309 				panic("sendrecv: short write! (%d < %d)",
310 				    cc, ssize);
311 
312 			tleft = tmo;
313 			tmo <<= 1;
314 			if (tmo > MAXTMO)
315 				tmo = MAXTMO;
316 			tlast = t;
317 		}
318 
319 		/* Try to get a packet and process it. */
320 		cc = (*rproc)(d, rbuf, rsize, tleft);
321 		/* Return on data, EOF or real error. */
322 		if (cc != -1 || errno != 0)
323 			return (cc);
324 
325 		/* Timed out or didn't get the packet we're waiting for */
326 		t = getsecs();
327 		tleft -= t - tlast;
328 		tlast = t;
329 	}
330 }
331 
332 /* Similar to inet_ntoa() */
333 char *
334 intoa(addr)
335 	n_long addr;
336 {
337 	register char *cp;
338 	register u_int byte;
339 	register int n;
340 	static char buf[17];	/* strlen(".255.255.255.255") + 1 */
341 
342 	cp = &buf[sizeof buf];
343 	*--cp = '\0';
344 
345 	n = 4;
346 	do {
347 		byte = addr & 0xff;
348 		*--cp = byte % 10 + '0';
349 		byte /= 10;
350 		if (byte > 0) {
351 			*--cp = byte % 10 + '0';
352 			byte /= 10;
353 			if (byte > 0)
354 				*--cp = byte + '0';
355 		}
356 		*--cp = '.';
357 		addr >>= 8;
358 	} while (--n > 0);
359 
360 	return (cp+1);
361 }
362 
363 static char *
364 number(s, n)
365 	char *s;
366 	int *n;
367 {
368 	for (*n = 0; isdigit(*s); s++)
369 		*n = (*n * 10) + *s - '0';
370 	return s;
371 }
372 
373 n_long
374 ip_convertaddr(p)
375 	char *p;
376 {
377 #define IP_ANYADDR	0
378 	n_long addr = 0, n;
379 
380 	if (p == (char *)0 || *p == '\0')
381 		return IP_ANYADDR;
382 	p = number(p, &n);
383 	addr |= (n << 24) & 0xff000000;
384 	if (*p == '\0' || *p++ != '.')
385 		return IP_ANYADDR;
386 	p = number(p, &n);
387 	addr |= (n << 16) & 0xff0000;
388 	if (*p == '\0' || *p++ != '.')
389 		return IP_ANYADDR;
390 	p = number(p, &n);
391 	addr |= (n << 8) & 0xff00;
392 	if (*p == '\0' || *p++ != '.')
393 		return IP_ANYADDR;
394 	p = number(p, &n);
395 	addr |= n & 0xff;
396 	if (*p != '\0')
397 		return IP_ANYADDR;
398 
399 	return ntohl(addr);
400 }
401