net.c revision 1.7 1 /* $NetBSD: net.c,v 1.7 1995/09/17 00:49:41 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 return -1;
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 return -1;
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 return -1;
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 return -1;
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 return -1;
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 return -1;
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 return -1;
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 return -1;
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 return -1;
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 return -1;
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 return -1;
269 }
270
271 n -= sizeof(*ip) + sizeof(*uh);
272 return (n);
273 }
274
275 /*
276 * Send a packet and wait for a reply, with exponential backoff.
277 *
278 * The send routine must return the actual number of bytes written.
279 *
280 * The receive routine can indicate success by returning the number of
281 * bytes read; it can return 0 to indicate EOF; it can return -1 with a
282 * non-zero errno to indicate failure; finally, it can return -1 with a
283 * zero errno to indicate it isn't done yet.
284 */
285 ssize_t
286 sendrecv(d, sproc, sbuf, ssize, rproc, rbuf, rsize)
287 register struct iodesc *d;
288 register ssize_t (*sproc)(struct iodesc *, void *, size_t);
289 register void *sbuf;
290 register size_t ssize;
291 register ssize_t (*rproc)(struct iodesc *, void *, size_t, time_t);
292 register void *rbuf;
293 register size_t rsize;
294 {
295 register ssize_t cc;
296 register time_t t, tmo, tlast, tleft;
297
298 #ifdef NET_DEBUG
299 if (debug)
300 printf("sendrecv: called\n");
301 #endif
302
303 tmo = MINTMO;
304 tlast = tleft = 0;
305 t = getsecs();
306 for (;;) {
307 if (tleft <= 0) {
308 cc = (*sproc)(d, sbuf, ssize);
309 if (cc == -1 || cc < ssize)
310 panic("sendrecv: short write! (%d < %d)",
311 cc, ssize);
312
313 tleft = tmo;
314 tmo <<= 1;
315 if (tmo > MAXTMO)
316 tmo = MAXTMO;
317 tlast = t;
318 }
319
320 /* Try to get a packet and process it. */
321 cc = (*rproc)(d, rbuf, rsize, tleft);
322 /* Return on data, EOF or real error. */
323 if (cc != -1 || errno != 0)
324 return (cc);
325
326 /* Timed out or didn't get the packet we're waiting for */
327 t = getsecs();
328 tleft -= t - tlast;
329 tlast = t;
330 }
331 }
332
333 /* Similar to inet_ntoa() */
334 char *
335 intoa(addr)
336 n_long addr;
337 {
338 register char *cp;
339 register u_int byte;
340 register int n;
341 static char buf[17]; /* strlen(".255.255.255.255") + 1 */
342
343 cp = &buf[sizeof buf];
344 *--cp = '\0';
345
346 n = 4;
347 do {
348 byte = addr & 0xff;
349 *--cp = byte % 10 + '0';
350 byte /= 10;
351 if (byte > 0) {
352 *--cp = byte % 10 + '0';
353 byte /= 10;
354 if (byte > 0)
355 *--cp = byte + '0';
356 }
357 *--cp = '.';
358 addr >>= 8;
359 } while (--n > 0);
360
361 return (cp+1);
362 }
363
364 static char *
365 number(s, n)
366 char *s;
367 int *n;
368 {
369 for (*n = 0; isdigit(*s); s++)
370 *n = (*n * 10) + *s - '0';
371 return s;
372 }
373
374 n_long
375 ip_convertaddr(p)
376 char *p;
377 {
378 #define IP_ANYADDR 0
379 n_long addr = 0, n;
380
381 if (p == (char *)0 || *p == '\0')
382 return IP_ANYADDR;
383 p = number(p, &n);
384 addr |= (n << 24) & 0xff000000;
385 if (*p == '\0' || *p++ != '.')
386 return IP_ANYADDR;
387 p = number(p, &n);
388 addr |= (n << 16) & 0xff0000;
389 if (*p == '\0' || *p++ != '.')
390 return IP_ANYADDR;
391 p = number(p, &n);
392 addr |= (n << 8) & 0xff00;
393 if (*p == '\0' || *p++ != '.')
394 return IP_ANYADDR;
395 p = number(p, &n);
396 addr |= n & 0xff;
397 if (*p != '\0')
398 return IP_ANYADDR;
399
400 return ntohl(addr);
401 }
402