Remove register keyword usage.
[dragonfly.git] / lib / libstand / udp.c
1 /* Taken from $NetBSD: net.c,v 1.20 1997/12/26 22:41:30 scottr 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  * $FreeBSD: src/lib/libstand/udp.c,v 1.1.2.1 2000/04/15 03:09:29 ps Exp $
41  * $DragonFly: src/lib/libstand/udp.c,v 1.3 2004/10/25 19:38:45 drhodus Exp $
42  */
43
44 #include <sys/param.h>
45 #include <sys/socket.h>
46
47 #include <string.h>
48
49 #include <net/if.h>
50 #include <netinet/in.h>
51 #include <netinet/if_ether.h>
52 #include <netinet/in_systm.h>
53
54 #include <netinet/ip.h>
55 #include <netinet/ip_var.h>
56 #include <netinet/udp.h>
57 #include <netinet/udp_var.h>
58
59 #include "stand.h"
60 #include "net.h"
61
62 /* Caller must leave room for ethernet, ip and udp headers in front!! */
63 ssize_t
64 sendudp(d, pkt, len)
65         struct iodesc *d;
66         void *pkt;
67         size_t len;
68 {
69         ssize_t cc;
70         struct ip *ip;
71         struct udphdr *uh;
72         u_char *ea;
73
74 #ifdef NET_DEBUG
75         if (debug) {
76                 printf("sendudp: d=%lx called.\n", (long)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 #ifndef UDP_NO_CKSUM
106         {
107                 struct udpiphdr *ui;
108                 struct ip tip;
109
110                 /* Calculate checksum (must save and restore ip header) */
111                 tip = *ip;
112                 ui = (struct udpiphdr *)ip;
113                 bzero(&ui->ui_x1, sizeof(ui->ui_x1));
114                 ui->ui_len = uh->uh_ulen;
115                 uh->uh_sum = in_cksum(ui, len);
116                 *ip = tip;
117         }
118 #endif
119
120         if (ip->ip_dst.s_addr == INADDR_BROADCAST || ip->ip_src.s_addr == 0 ||
121             netmask == 0 || SAMENET(ip->ip_src, ip->ip_dst, netmask))
122                 ea = arpwhohas(d, ip->ip_dst);
123         else
124                 ea = arpwhohas(d, gateip);
125
126         cc = sendether(d, ip, len, ea, ETHERTYPE_IP);
127         if (cc == -1)
128                 return (-1);
129         if (cc != len)
130                 panic("sendudp: bad write (%d != %d)", cc, len);
131         return (cc - (sizeof(*ip) + sizeof(*uh)));
132 }
133
134 /*
135  * Receive a UDP packet and validate it is for us.
136  * Caller leaves room for the headers (Ether, IP, UDP)
137  */
138 ssize_t
139 readudp(d, pkt, len, tleft)
140         struct iodesc *d;
141         void *pkt;
142         size_t len;
143         time_t tleft;
144 {
145         ssize_t n;
146         size_t hlen;
147         struct ip *ip;
148         struct udphdr *uh;
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         if (n < ntohs(ip->ip_len)) {
203 #ifdef NET_DEBUG
204                 if (debug)
205                         printf("readudp: bad length %d < %d.\n",
206                                (int)n, ntohs(ip->ip_len));
207 #endif
208                 return -1;
209         }
210         if (d->myip.s_addr && ip->ip_dst.s_addr != d->myip.s_addr) {
211 #ifdef NET_DEBUG
212                 if (debug) {
213                         printf("readudp: bad saddr %s != ", inet_ntoa(d->myip));
214                         printf("%s\n", inet_ntoa(ip->ip_dst));
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 = htons(sizeof(*ip));
224                 n -= hlen - sizeof(*ip);
225         }
226         if (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 #ifndef UDP_NO_CKSUM
236         if (uh->uh_sum) {
237                 struct udpiphdr *ui;
238                 struct ip tip;
239
240                 n = ntohs(uh->uh_ulen) + sizeof(*ip);
241                 if (n > RECV_SIZE - ETHER_SIZE) {
242                         printf("readudp: huge packet, udp len %d\n", (int)n);
243                         return -1;
244                 }
245
246                 /* Check checksum (must save and restore ip header) */
247                 tip = *ip;
248                 ui = (struct udpiphdr *)ip;
249                 bzero(&ui->ui_x1, sizeof(ui->ui_x1));
250                 ui->ui_len = uh->uh_ulen;
251                 if (in_cksum(ui, n) != 0) {
252 #ifdef NET_DEBUG
253                         if (debug)
254                                 printf("readudp: bad cksum\n");
255 #endif
256                         *ip = tip;
257                         return -1;
258                 }
259                 *ip = tip;
260         }
261 #endif
262         if (ntohs(uh->uh_ulen) < sizeof(*uh)) {
263 #ifdef NET_DEBUG
264                 if (debug)
265                         printf("readudp: bad udp len %d < %d\n",
266                                 ntohs(uh->uh_ulen), (int)sizeof(*uh));
267 #endif
268                 return -1;
269         }
270
271         n -= sizeof(*ip) + sizeof(*uh);
272         return (n);
273 }