]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - lib/libstand/udp.c
This commit was generated by cvs2svn to compensate for changes in r160157,
[FreeBSD/FreeBSD.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  */
41
42 #include <sys/cdefs.h>
43 __FBSDID("$FreeBSD$");
44
45 #include <sys/param.h>
46 #include <sys/socket.h>
47
48 #include <string.h>
49
50 #include <net/if.h>
51 #include <netinet/in.h>
52 #include <netinet/if_ether.h>
53 #include <netinet/in_systm.h>
54
55 #include <netinet/ip.h>
56 #include <netinet/ip_var.h>
57 #include <netinet/udp.h>
58 #include <netinet/udp_var.h>
59
60 #include "stand.h"
61 #include "net.h"
62
63 /* Caller must leave room for ethernet, ip and udp headers in front!! */
64 ssize_t
65 sendudp(d, pkt, len)
66         struct iodesc *d;
67         void *pkt;
68         size_t len;
69 {
70         ssize_t cc;
71         struct ip *ip;
72         struct udphdr *uh;
73         u_char *ea;
74
75 #ifdef NET_DEBUG
76         if (debug) {
77                 printf("sendudp: d=%lx called.\n", (long)d);
78                 if (d) {
79                         printf("saddr: %s:%d",
80                             inet_ntoa(d->myip), ntohs(d->myport));
81                         printf(" daddr: %s:%d\n",
82                             inet_ntoa(d->destip), ntohs(d->destport));
83                 }
84         }
85 #endif
86
87         uh = (struct udphdr *)pkt - 1;
88         ip = (struct ip *)uh - 1;
89         len += sizeof(*ip) + sizeof(*uh);
90
91         bzero(ip, sizeof(*ip) + sizeof(*uh));
92
93         ip->ip_v = IPVERSION;                   /* half-char */
94         ip->ip_hl = sizeof(*ip) >> 2;           /* half-char */
95         ip->ip_len = htons(len);
96         ip->ip_p = IPPROTO_UDP;                 /* char */
97         ip->ip_ttl = IP_TTL;                    /* char */
98         ip->ip_src = d->myip;
99         ip->ip_dst = d->destip;
100         ip->ip_sum = in_cksum(ip, sizeof(*ip));  /* short, but special */
101
102         uh->uh_sport = d->myport;
103         uh->uh_dport = d->destport;
104         uh->uh_ulen = htons(len - sizeof(*ip));
105
106 #ifndef UDP_NO_CKSUM
107         {
108                 struct udpiphdr *ui;
109                 struct ip tip;
110
111                 /* Calculate checksum (must save and restore ip header) */
112                 tip = *ip;
113                 ui = (struct udpiphdr *)ip;
114                 bzero(&ui->ui_x1, sizeof(ui->ui_x1));
115                 ui->ui_len = uh->uh_ulen;
116                 uh->uh_sum = in_cksum(ui, len);
117                 *ip = tip;
118         }
119 #endif
120
121         if (ip->ip_dst.s_addr == INADDR_BROADCAST || ip->ip_src.s_addr == 0 ||
122             netmask == 0 || SAMENET(ip->ip_src, ip->ip_dst, netmask))
123                 ea = arpwhohas(d, ip->ip_dst);
124         else
125                 ea = arpwhohas(d, gateip);
126
127         cc = sendether(d, ip, len, ea, ETHERTYPE_IP);
128         if (cc == -1)
129                 return (-1);
130         if (cc != len)
131                 panic("sendudp: bad write (%zd != %zd)", cc, len);
132         return (cc - (sizeof(*ip) + sizeof(*uh)));
133 }
134
135 /*
136  * Receive a UDP packet and validate it is for us.
137  * Caller leaves room for the headers (Ether, IP, UDP)
138  */
139 ssize_t
140 readudp(d, pkt, len, tleft)
141         struct iodesc *d;
142         void *pkt;
143         size_t len;
144         time_t tleft;
145 {
146         ssize_t n;
147         size_t hlen;
148         struct ip *ip;
149         struct udphdr *uh;
150         u_int16_t etype;        /* host order */
151
152 #ifdef NET_DEBUG
153         if (debug)
154                 printf("readudp: called\n");
155 #endif
156
157         uh = (struct udphdr *)pkt - 1;
158         ip = (struct ip *)uh - 1;
159
160         n = readether(d, ip, len + sizeof(*ip) + sizeof(*uh), tleft, &etype);
161         if (n == -1 || n < sizeof(*ip) + sizeof(*uh))
162                 return -1;
163
164         /* Ethernet address checks now in readether() */
165
166         /* Need to respond to ARP requests. */
167         if (etype == ETHERTYPE_ARP) {
168                 struct arphdr *ah = (void *)ip;
169                 if (ah->ar_op == htons(ARPOP_REQUEST)) {
170                         /* Send ARP reply */
171                         arp_reply(d, ah);
172                 }
173                 return -1;
174         }
175
176         if (etype != ETHERTYPE_IP) {
177 #ifdef NET_DEBUG
178                 if (debug)
179                         printf("readudp: not IP. ether_type=%x\n", etype);
180 #endif
181                 return -1;
182         }
183
184         /* Check ip header */
185         if (ip->ip_v != IPVERSION ||
186             ip->ip_p != IPPROTO_UDP) {  /* half char */
187 #ifdef NET_DEBUG
188                 if (debug)
189                         printf("readudp: IP version or not UDP. ip_v=%d ip_p=%d\n", ip->ip_v, ip->ip_p);
190 #endif
191                 return -1;
192         }
193
194         hlen = ip->ip_hl << 2;
195         if (hlen < sizeof(*ip) ||
196             in_cksum(ip, hlen) != 0) {
197 #ifdef NET_DEBUG
198                 if (debug)
199                         printf("readudp: short hdr or bad cksum.\n");
200 #endif
201                 return -1;
202         }
203         if (n < ntohs(ip->ip_len)) {
204 #ifdef NET_DEBUG
205                 if (debug)
206                         printf("readudp: bad length %d < %d.\n",
207                                (int)n, ntohs(ip->ip_len));
208 #endif
209                 return -1;
210         }
211         if (d->myip.s_addr && ip->ip_dst.s_addr != d->myip.s_addr) {
212 #ifdef NET_DEBUG
213                 if (debug) {
214                         printf("readudp: bad saddr %s != ", inet_ntoa(d->myip));
215                         printf("%s\n", inet_ntoa(ip->ip_dst));
216                 }
217 #endif
218                 return -1;
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 = htons(sizeof(*ip));
225                 n -= hlen - sizeof(*ip);
226         }
227         if (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                 return -1;
234         }
235
236 #ifndef UDP_NO_CKSUM
237         if (uh->uh_sum) {
238                 struct udpiphdr *ui;
239                 struct ip tip;
240
241                 n = ntohs(uh->uh_ulen) + sizeof(*ip);
242                 if (n > RECV_SIZE - ETHER_SIZE) {
243                         printf("readudp: huge packet, udp len %d\n", (int)n);
244                         return -1;
245                 }
246
247                 /* Check checksum (must save and restore ip header) */
248                 tip = *ip;
249                 ui = (struct udpiphdr *)ip;
250                 bzero(&ui->ui_x1, sizeof(ui->ui_x1));
251                 ui->ui_len = uh->uh_ulen;
252                 if (in_cksum(ui, n) != 0) {
253 #ifdef NET_DEBUG
254                         if (debug)
255                                 printf("readudp: bad cksum\n");
256 #endif
257                         *ip = tip;
258                         return -1;
259                 }
260                 *ip = tip;
261         }
262 #endif
263         if (ntohs(uh->uh_ulen) < sizeof(*uh)) {
264 #ifdef NET_DEBUG
265                 if (debug)
266                         printf("readudp: bad udp len %d < %d\n",
267                                 ntohs(uh->uh_ulen), (int)sizeof(*uh));
268 #endif
269                 return -1;
270         }
271
272         n = (n > (ntohs(uh->uh_ulen) - sizeof(*uh))) ? 
273             ntohs(uh->uh_ulen) - sizeof(*uh) : n;
274         return (n);
275 }