net.c revision 1.8 1 /* $NetBSD: net.c,v 1.8 1995/09/18 21:19:30 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
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 char *
335 inet_ntoa(ia)
336 struct in_addr ia;
337 {
338 return (intoa(ia.s_addr));
339 }
340
341 /* Similar to inet_ntoa() */
342 char *
343 intoa(addr)
344 register n_long addr;
345 {
346 register char *cp;
347 register u_int byte;
348 register int n;
349 static char buf[17]; /* strlen(".255.255.255.255") + 1 */
350
351 NTOHL(addr);
352 cp = &buf[sizeof buf];
353 *--cp = '\0';
354
355 n = 4;
356 do {
357 byte = addr & 0xff;
358 *--cp = byte % 10 + '0';
359 byte /= 10;
360 if (byte > 0) {
361 *--cp = byte % 10 + '0';
362 byte /= 10;
363 if (byte > 0)
364 *--cp = byte + '0';
365 }
366 *--cp = '.';
367 addr >>= 8;
368 } while (--n > 0);
369
370 return (cp+1);
371 }
372
373 static char *
374 number(s, n)
375 char *s;
376 int *n;
377 {
378 for (*n = 0; isdigit(*s); s++)
379 *n = (*n * 10) + *s - '0';
380 return s;
381 }
382
383 n_long
384 ip_convertaddr(p)
385 char *p;
386 {
387 #define IP_ANYADDR 0
388 n_long addr = 0, n;
389
390 if (p == (char *)0 || *p == '\0')
391 return IP_ANYADDR;
392 p = number(p, &n);
393 addr |= (n << 24) & 0xff000000;
394 if (*p == '\0' || *p++ != '.')
395 return IP_ANYADDR;
396 p = number(p, &n);
397 addr |= (n << 16) & 0xff0000;
398 if (*p == '\0' || *p++ != '.')
399 return IP_ANYADDR;
400 p = number(p, &n);
401 addr |= (n << 8) & 0xff00;
402 if (*p == '\0' || *p++ != '.')
403 return IP_ANYADDR;
404 p = number(p, &n);
405 addr |= n & 0xff;
406 if (*p != '\0')
407 return IP_ANYADDR;
408
409 return htonl(addr);
410 }
411