lwp: Bump version for shuffle lwp syscalls.
[dragonfly.git] / lib / libstand / udp.c
blob9889932f347d4d3bc1985b395aa7bb5962e77e64
1 /* Taken from $NetBSD: net.c,v 1.20 1997/12/26 22:41:30 scottr Exp $ */
3 /*
4 * Copyright (c) 1992 Regents of the University of California.
5 * All rights reserved.
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.
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. Neither the name of the University nor the names of its contributors
20 * may be used to endorse or promote products derived from this software
21 * without specific prior written permission.
23 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 * SUCH DAMAGE.
35 * @(#) Header: net.c,v 1.9 93/08/06 19:32:15 leres Exp (LBL)
36 * $FreeBSD: src/lib/libstand/udp.c,v 1.1.2.1 2000/04/15 03:09:29 ps Exp $
37 * $DragonFly: src/lib/libstand/udp.c,v 1.4 2005/12/11 02:27:26 swildner Exp $
40 #include <sys/param.h>
41 #include <sys/socket.h>
43 #include <string.h>
45 #include <net/if.h>
46 #include <netinet/in.h>
47 #include <netinet/if_ether.h>
48 #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>
55 #include "stand.h"
56 #include "net.h"
58 /* Caller must leave room for ethernet, ip and udp headers in front!! */
59 ssize_t
60 sendudp(struct iodesc *d, void *pkt, size_t len)
62 ssize_t cc;
63 struct ip *ip;
64 struct udphdr *uh;
65 u_char *ea;
67 #ifdef NET_DEBUG
68 if (debug) {
69 printf("sendudp: d=%lx called.\n", (long)d);
70 if (d) {
71 printf("saddr: %s:%d",
72 inet_ntoa(d->myip), ntohs(d->myport));
73 printf(" daddr: %s:%d\n",
74 inet_ntoa(d->destip), ntohs(d->destport));
77 #endif
79 uh = (struct udphdr *)pkt - 1;
80 ip = (struct ip *)uh - 1;
81 len += sizeof(*ip) + sizeof(*uh);
83 bzero(ip, sizeof(*ip) + sizeof(*uh));
85 ip->ip_v = IPVERSION; /* half-char */
86 ip->ip_hl = sizeof(*ip) >> 2; /* half-char */
87 ip->ip_len = htons(len);
88 ip->ip_p = IPPROTO_UDP; /* char */
89 ip->ip_ttl = IP_TTL; /* char */
90 ip->ip_src = d->myip;
91 ip->ip_dst = d->destip;
92 ip->ip_sum = in_cksum(ip, sizeof(*ip)); /* short, but special */
94 uh->uh_sport = d->myport;
95 uh->uh_dport = d->destport;
96 uh->uh_ulen = htons(len - sizeof(*ip));
98 #ifndef UDP_NO_CKSUM
100 struct udpiphdr *ui;
101 struct ip tip;
103 /* Calculate checksum (must save and restore ip header) */
104 tip = *ip;
105 ui = (struct udpiphdr *)ip;
106 bzero(&ui->ui_x1, sizeof(ui->ui_x1));
107 ui->ui_len = uh->uh_ulen;
108 uh->uh_sum = in_cksum(ui, len);
109 *ip = tip;
111 #endif
113 if (ip->ip_dst.s_addr == INADDR_BROADCAST || ip->ip_src.s_addr == 0 ||
114 netmask == 0 || SAMENET(ip->ip_src, ip->ip_dst, netmask))
115 ea = arpwhohas(d, ip->ip_dst);
116 else
117 ea = arpwhohas(d, gateip);
119 cc = sendether(d, ip, len, ea, ETHERTYPE_IP);
120 if (cc == -1)
121 return (-1);
122 if (cc != len)
123 panic("sendudp: bad write (%zd != %zu)", cc, len);
124 return (cc - (sizeof(*ip) + sizeof(*uh)));
128 * Receive a UDP packet and validate it is for us.
129 * Caller leaves room for the headers (Ether, IP, UDP)
131 ssize_t
132 readudp(struct iodesc *d, void *pkt, size_t len, time_t tleft)
134 ssize_t n;
135 size_t hlen;
136 struct ip *ip;
137 struct udphdr *uh;
138 u_int16_t etype; /* host order */
140 #ifdef NET_DEBUG
141 if (debug)
142 printf("readudp: called\n");
143 #endif
145 uh = (struct udphdr *)pkt - 1;
146 ip = (struct ip *)uh - 1;
148 n = readether(d, ip, len + sizeof(*ip) + sizeof(*uh), tleft, &etype);
149 if (n == -1 || n < sizeof(*ip) + sizeof(*uh))
150 return -1;
152 /* Ethernet address checks now in readether() */
154 /* Need to respond to ARP requests. */
155 if (etype == ETHERTYPE_ARP) {
156 struct arphdr *ah = (void *)ip;
157 if (ah->ar_op == htons(ARPOP_REQUEST)) {
158 /* Send ARP reply */
159 arp_reply(d, ah);
161 return -1;
164 if (etype != ETHERTYPE_IP) {
165 #ifdef NET_DEBUG
166 if (debug)
167 printf("readudp: not IP. ether_type=%x\n", etype);
168 #endif
169 return -1;
172 /* Check ip header */
173 if (ip->ip_v != IPVERSION ||
174 ip->ip_p != IPPROTO_UDP) { /* half char */
175 #ifdef NET_DEBUG
176 if (debug)
177 printf("readudp: IP version or not UDP. ip_v=%d ip_p=%d\n", ip->ip_v, ip->ip_p);
178 #endif
179 return -1;
182 hlen = ip->ip_hl << 2;
183 if (hlen < sizeof(*ip) ||
184 in_cksum(ip, hlen) != 0) {
185 #ifdef NET_DEBUG
186 if (debug)
187 printf("readudp: short hdr or bad cksum.\n");
188 #endif
189 return -1;
191 if (n < ntohs(ip->ip_len)) {
192 #ifdef NET_DEBUG
193 if (debug)
194 printf("readudp: bad length %d < %d.\n",
195 (int)n, ntohs(ip->ip_len));
196 #endif
197 return -1;
199 if (d->myip.s_addr && ip->ip_dst.s_addr != d->myip.s_addr) {
200 #ifdef NET_DEBUG
201 if (debug) {
202 printf("readudp: bad saddr %s != ", inet_ntoa(d->myip));
203 printf("%s\n", inet_ntoa(ip->ip_dst));
205 #endif
206 return -1;
209 /* If there were ip options, make them go away */
210 if (hlen != sizeof(*ip)) {
211 bcopy(((u_char *)ip) + hlen, uh, len - hlen);
212 ip->ip_len = htons(sizeof(*ip));
213 n -= hlen - sizeof(*ip);
215 if (uh->uh_dport != d->myport) {
216 #ifdef NET_DEBUG
217 if (debug)
218 printf("readudp: bad dport %d != %d\n",
219 d->myport, ntohs(uh->uh_dport));
220 #endif
221 return -1;
224 #ifndef UDP_NO_CKSUM
225 if (uh->uh_sum) {
226 struct udpiphdr *ui;
227 struct ip tip;
229 n = ntohs(uh->uh_ulen) + sizeof(*ip);
230 if (n > RECV_SIZE - ETHER_SIZE) {
231 printf("readudp: huge packet, udp len %d\n", (int)n);
232 return -1;
235 /* Check checksum (must save and restore ip header) */
236 tip = *ip;
237 ui = (struct udpiphdr *)ip;
238 bzero(&ui->ui_x1, sizeof(ui->ui_x1));
239 ui->ui_len = uh->uh_ulen;
240 if (in_cksum(ui, n) != 0) {
241 #ifdef NET_DEBUG
242 if (debug)
243 printf("readudp: bad cksum\n");
244 #endif
245 *ip = tip;
246 return -1;
248 *ip = tip;
250 #endif
251 if (ntohs(uh->uh_ulen) < sizeof(*uh)) {
252 #ifdef NET_DEBUG
253 if (debug)
254 printf("readudp: bad udp len %d < %d\n",
255 ntohs(uh->uh_ulen), (int)sizeof(*uh));
256 #endif
257 return -1;
260 n -= sizeof(*ip) + sizeof(*uh);
261 return (n);