net.c revision 1.24 1 /* $NetBSD: net.c,v 1.24 1999/11/11 20:23:17 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 <net/if.h>
46 #include <net/if_ether.h>
47
48 #include <netinet/in.h>
49 #include <netinet/in_systm.h>
50 #include <netinet/ip.h>
51 #include <netinet/ip_var.h>
52 #include <netinet/udp.h>
53 #include <netinet/udp_var.h>
54
55 #include "stand.h"
56 #include "net.h"
57
58
59 /* Caller must leave room for ethernet, ip and udp headers in front!! */
60 ssize_t
61 sendudp(d, pkt, len)
62 register struct iodesc *d;
63 register void *pkt;
64 register size_t len;
65 {
66 register ssize_t cc;
67 register struct ip *ip;
68 register struct udphdr *uh;
69 register u_char *ea;
70
71 #ifdef NET_DEBUG
72 if (debug) {
73 printf("sendudp: d=%lx called.\n", (long)d);
74 if (d) {
75 printf("saddr: %s:%d",
76 inet_ntoa(d->myip), ntohs(d->myport));
77 printf(" daddr: %s:%d\n",
78 inet_ntoa(d->destip), ntohs(d->destport));
79 }
80 }
81 #endif
82
83 uh = (struct udphdr *)pkt - 1;
84 ip = (struct ip *)uh - 1;
85 len += sizeof(*ip) + sizeof(*uh);
86
87 bzero(ip, sizeof(*ip) + sizeof(*uh));
88
89 ip->ip_v = IPVERSION; /* half-char */
90 ip->ip_hl = sizeof(*ip) >> 2; /* half-char */
91 ip->ip_len = htons(len);
92 ip->ip_p = IPPROTO_UDP; /* char */
93 ip->ip_ttl = IP_TTL; /* char */
94 ip->ip_src = d->myip;
95 ip->ip_dst = d->destip;
96 ip->ip_sum = in_cksum(ip, sizeof(*ip)); /* short, but special */
97
98 uh->uh_sport = d->myport;
99 uh->uh_dport = d->destport;
100 uh->uh_ulen = htons(len - sizeof(*ip));
101
102 #ifndef UDP_NO_CKSUM
103 {
104 register struct udpiphdr *ui;
105 struct ip tip;
106
107 /* Calculate checksum (must save and restore ip header) */
108 tip = *ip;
109 ui = (struct udpiphdr *)ip;
110 bzero(ui->ui_x1, sizeof(ui->ui_x1));
111 ui->ui_len = uh->uh_ulen;
112 uh->uh_sum = in_cksum(ui, len);
113 *ip = tip;
114 }
115 #endif
116
117 if (ip->ip_dst.s_addr == INADDR_BROADCAST || ip->ip_src.s_addr == 0 ||
118 netmask == 0 || SAMENET(ip->ip_src, ip->ip_dst, netmask))
119 ea = arpwhohas(d, ip->ip_dst);
120 else
121 ea = arpwhohas(d, gateip);
122
123 cc = sendether(d, ip, len, ea, ETHERTYPE_IP);
124 if (cc == -1)
125 return (-1);
126 if (cc != len)
127 panic("sendudp: bad write (%d != %d)", cc, len);
128 return (cc - (sizeof(*ip) + sizeof(*uh)));
129 }
130
131 /*
132 * Receive a UDP packet and validate it is for us.
133 * Caller leaves room for the headers (Ether, IP, UDP)
134 */
135 ssize_t
136 readudp(d, pkt, len, tleft)
137 register struct iodesc *d;
138 register void *pkt;
139 register size_t len;
140 time_t tleft;
141 {
142 register ssize_t n;
143 register size_t hlen;
144 register struct ip *ip;
145 register struct udphdr *uh;
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 if (n < ntohs(ip->ip_len)) {
200 #ifdef NET_DEBUG
201 if (debug)
202 printf("readudp: bad length %d < %d.\n",
203 (int)n, ntohs(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 = htons(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 #ifndef UDP_NO_CKSUM
233 if (uh->uh_sum) {
234 register struct udpiphdr *ui;
235 struct ip tip;
236
237 n = ntohs(uh->uh_ulen) + sizeof(*ip);
238 if (n > RECV_SIZE - ETHER_SIZE) {
239 printf("readudp: huge packet, udp len %d\n", (int)n);
240 return -1;
241 }
242
243 /* Check checksum (must save and restore ip header) */
244 tip = *ip;
245 ui = (struct udpiphdr *)ip;
246 bzero(ui->ui_x1, sizeof(ui->ui_x1));
247 ui->ui_len = uh->uh_ulen;
248 if (in_cksum(ui, n) != 0) {
249 #ifdef NET_DEBUG
250 if (debug)
251 printf("readudp: bad cksum\n");
252 #endif
253 *ip = tip;
254 return -1;
255 }
256 *ip = tip;
257 }
258 #endif
259 if (ntohs(uh->uh_ulen) < sizeof(*uh)) {
260 #ifdef NET_DEBUG
261 if (debug)
262 printf("readudp: bad udp len %d < %d\n",
263 ntohs(uh->uh_ulen), (int)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 * or -1 on error.
277 *
278 * The receive routine can indicate success by returning the number of
279 * bytes read; it can return 0 to indicate EOF; it can return -1 with a
280 * non-zero errno to indicate failure; finally, it can return -1 with a
281 * zero errno to indicate it isn't done yet.
282 */
283 ssize_t
284 sendrecv(d, sproc, sbuf, ssize, rproc, rbuf, rsize)
285 register struct iodesc *d;
286 register ssize_t (*sproc)(struct iodesc *, void *, size_t);
287 register void *sbuf;
288 register size_t ssize;
289 register ssize_t (*rproc)(struct iodesc *, void *, size_t, time_t);
290 register void *rbuf;
291 register size_t rsize;
292 {
293 register ssize_t cc;
294 register time_t t, tmo, tlast;
295 long 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 if (tmo >= MAXTMO) {
308 errno = ETIMEDOUT;
309 return -1;
310 }
311 cc = (*sproc)(d, sbuf, ssize);
312 if (cc != -1 && cc < ssize)
313 panic("sendrecv: short write! (%d < %d)",
314 cc, ssize);
315
316 tleft = tmo;
317 tmo <<= 1;
318 if (tmo > MAXTMO)
319 tmo = MAXTMO;
320
321 if (cc == -1) {
322 /* Error on transmit; wait before retrying */
323 while ((getsecs() - t) < tmo);
324 tleft = 0;
325 continue;
326 }
327
328 tlast = t;
329 }
330
331 /* Try to get a packet and process it. */
332 cc = (*rproc)(d, rbuf, rsize, tleft);
333 /* Return on data, EOF or real error. */
334 if (cc != -1 || errno != 0)
335 return (cc);
336
337 /* Timed out or didn't get the packet we're waiting for */
338 t = getsecs();
339 tleft -= t - tlast;
340 tlast = t;
341 }
342 }
343