net.c revision 1.5 1 /* $NetBSD: net.c,v 1.5 1995/09/11 21:11:41 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 #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 /*
133 * Receive a UDP packet and validate it is for us.
134 * Caller leaves room for the headers (Ether, IP, UDP)
135 */
136 size_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 size_t hlen;
144 register struct ip *ip;
145 register struct udphdr *uh;
146 register struct udpiphdr *ui;
147 struct ip tip;
148 u_int16_t etype; /* host order */
149
150 #ifdef NET_DEBUG
151 if (debug)
152 printf("readudp: called\n");
153 #endif
154
155 uh = (struct udphdr *)pkt - 1;
156 ip = (struct ip *)uh - 1;
157
158 len = readether(d, ip, len + sizeof(*ip) + sizeof(*uh),
159 tleft, &etype);
160 if (len == -1 || len < 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 (len < ip->ip_len) {
204 #ifdef NET_DEBUG
205 if (debug)
206 printf("readudp: bad length %d < %d.\n",
207 len, ip->ip_len);
208 #endif
209 goto bad;
210 }
211 if (d->myip && ntohl(ip->ip_dst.s_addr) != d->myip) {
212 #ifdef NET_DEBUG
213 if (debug) {
214 printf("readudp: bad saddr %s != ", intoa(d->myip));
215 printf("%s\n", intoa(ntohl(ip->ip_dst.s_addr)));
216 }
217 #endif
218 goto bad;
219 }
220
221 /* If there were ip options, make them go away */
222 if (hlen != sizeof(*ip)) {
223 bcopy(((u_char *)ip) + hlen, uh, len - hlen);
224 ip->ip_len = sizeof(*ip);
225 len -= hlen - sizeof(*ip);
226 }
227 if (ntohs(uh->uh_dport) != d->myport) {
228 #ifdef NET_DEBUG
229 if (debug)
230 printf("readudp: bad dport %d != %d\n",
231 d->myport, ntohs(uh->uh_dport));
232 #endif
233 goto bad;
234 }
235
236 if (uh->uh_sum) {
237 len = ntohs(uh->uh_ulen) + sizeof(*ip);
238 if (len > RECV_SIZE - ETHER_SIZE) {
239 printf("readudp: huge packet, udp len %d\n", len);
240 goto bad;
241 }
242
243 /* Check checksum (must save and restore ip header) */
244 tip = *ip;
245 ui = (struct udpiphdr *)ip;
246 ui->ui_next = 0;
247 ui->ui_prev = 0;
248 ui->ui_x1 = 0;
249 ui->ui_len = uh->uh_ulen;
250 if (in_cksum(ui, len) != 0) {
251 #ifdef NET_DEBUG
252 if (debug)
253 printf("readudp: bad cksum\n");
254 #endif
255 *ip = tip;
256 goto bad;
257 }
258 *ip = tip;
259 }
260 NTOHS(uh->uh_dport);
261 NTOHS(uh->uh_sport);
262 NTOHS(uh->uh_ulen);
263 if (uh->uh_ulen < sizeof(*uh)) {
264 #ifdef NET_DEBUG
265 if (debug)
266 printf("readudp: bad udp len %d < %d\n",
267 uh->uh_ulen, sizeof(*uh));
268 #endif
269 goto bad;
270 }
271
272 len -= sizeof(*ip) + sizeof(*uh);
273 return (len);
274
275 bad:
276 return (-1);
277 }
278
279 /*
280 * Send a packet and wait for a reply, with exponential backoff.
281 *
282 * The send routine must return the actual number of bytes written.
283 *
284 * The receive routine can indicate success by returning the number of
285 * bytes read; it can return 0 to indicate EOF; it can return -1 with a
286 * non-zero errno to indicate failure; finally, it can return -1 with a
287 * zero errno to indicate it isn't done yet.
288 */
289 size_t
290 sendrecv(d, sproc, sbuf, ssize, rproc, rbuf, rsize)
291 register struct iodesc *d;
292 register size_t (*sproc)(struct iodesc *, void *, size_t);
293 register void *sbuf;
294 register size_t ssize;
295 register size_t (*rproc)(struct iodesc *, void *, size_t, time_t);
296 register void *rbuf;
297 register size_t rsize;
298 {
299 register size_t cc;
300 register time_t t, tmo, tlast, tleft;
301
302 #ifdef NET_DEBUG
303 if (debug)
304 printf("sendrecv: called\n");
305 #endif
306
307 tmo = MINTMO;
308 tlast = tleft = 0;
309 t = getsecs();
310 for (;;) {
311 if (tleft <= 0) {
312 cc = (*sproc)(d, sbuf, ssize);
313 if (cc == -1 || cc < ssize)
314 panic("sendrecv: short write! (%d < %d)",
315 cc, ssize);
316
317 tleft = tmo;
318 tmo <<= 1;
319 if (tmo > MAXTMO)
320 tmo = MAXTMO;
321 tlast = t;
322 }
323
324 /* Try to get a packet and process it. */
325 cc = (*rproc)(d, rbuf, rsize, tleft);
326 /* Return on data, EOF or real error. */
327 if (cc != -1 || errno != 0)
328 return (cc);
329
330 /* Timed out or didn't get the packet we're waiting for */
331 t = getsecs();
332 tleft -= t - tlast;
333 tlast = t;
334 }
335 }
336
337 /* Similar to inet_ntoa() */
338 char *
339 intoa(addr)
340 n_long addr;
341 {
342 register char *cp;
343 register u_int byte;
344 register int n;
345 static char buf[17]; /* strlen(".255.255.255.255") + 1 */
346
347 cp = &buf[sizeof buf];
348 *--cp = '\0';
349
350 n = 4;
351 do {
352 byte = addr & 0xff;
353 *--cp = byte % 10 + '0';
354 byte /= 10;
355 if (byte > 0) {
356 *--cp = byte % 10 + '0';
357 byte /= 10;
358 if (byte > 0)
359 *--cp = byte + '0';
360 }
361 *--cp = '.';
362 addr >>= 8;
363 } while (--n > 0);
364
365 return (cp+1);
366 }
367
368 static char *
369 number(s, n)
370 char *s;
371 int *n;
372 {
373 for (*n = 0; isdigit(*s); s++)
374 *n = (*n * 10) + *s - '0';
375 return s;
376 }
377
378 n_long
379 ip_convertaddr(p)
380 char *p;
381 {
382 #define IP_ANYADDR 0
383 n_long addr = 0, n;
384
385 if (p == (char *)0 || *p == '\0')
386 return IP_ANYADDR;
387 p = number(p, &n);
388 addr |= (n << 24) & 0xff000000;
389 if (*p == '\0' || *p++ != '.')
390 return IP_ANYADDR;
391 p = number(p, &n);
392 addr |= (n << 16) & 0xff0000;
393 if (*p == '\0' || *p++ != '.')
394 return IP_ANYADDR;
395 p = number(p, &n);
396 addr |= (n << 8) & 0xff00;
397 if (*p == '\0' || *p++ != '.')
398 return IP_ANYADDR;
399 p = number(p, &n);
400 addr |= n & 0xff;
401 if (*p != '\0')
402 return IP_ANYADDR;
403
404 return ntohl(addr);
405 }
406