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