udp.c revision 1.1.2.2 1 1.1.2.2 bouyer /* $NetBSD: udp.c,v 1.1.2.2 2000/11/22 16:05:43 bouyer Exp $ */
2 1.1.2.2 bouyer
3 1.1.2.2 bouyer /*
4 1.1.2.2 bouyer * Copyright (c) 1992 Regents of the University of California.
5 1.1.2.2 bouyer * All rights reserved.
6 1.1.2.2 bouyer *
7 1.1.2.2 bouyer * This software was developed by the Computer Systems Engineering group
8 1.1.2.2 bouyer * at Lawrence Berkeley Laboratory under DARPA contract BG 91-66 and
9 1.1.2.2 bouyer * contributed to Berkeley.
10 1.1.2.2 bouyer *
11 1.1.2.2 bouyer * Redistribution and use in source and binary forms, with or without
12 1.1.2.2 bouyer * modification, are permitted provided that the following conditions
13 1.1.2.2 bouyer * are met:
14 1.1.2.2 bouyer * 1. Redistributions of source code must retain the above copyright
15 1.1.2.2 bouyer * notice, this list of conditions and the following disclaimer.
16 1.1.2.2 bouyer * 2. Redistributions in binary form must reproduce the above copyright
17 1.1.2.2 bouyer * notice, this list of conditions and the following disclaimer in the
18 1.1.2.2 bouyer * documentation and/or other materials provided with the distribution.
19 1.1.2.2 bouyer * 3. All advertising materials mentioning features or use of this software
20 1.1.2.2 bouyer * must display the following acknowledgement:
21 1.1.2.2 bouyer * This product includes software developed by the University of
22 1.1.2.2 bouyer * California, Lawrence Berkeley Laboratory and its contributors.
23 1.1.2.2 bouyer * 4. Neither the name of the University nor the names of its contributors
24 1.1.2.2 bouyer * may be used to endorse or promote products derived from this software
25 1.1.2.2 bouyer * without specific prior written permission.
26 1.1.2.2 bouyer *
27 1.1.2.2 bouyer * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
28 1.1.2.2 bouyer * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
29 1.1.2.2 bouyer * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
30 1.1.2.2 bouyer * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
31 1.1.2.2 bouyer * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
32 1.1.2.2 bouyer * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
33 1.1.2.2 bouyer * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
34 1.1.2.2 bouyer * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
35 1.1.2.2 bouyer * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
36 1.1.2.2 bouyer * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
37 1.1.2.2 bouyer * SUCH DAMAGE.
38 1.1.2.2 bouyer *
39 1.1.2.2 bouyer * @(#) Header: net.c,v 1.9 93/08/06 19:32:15 leres Exp (LBL)
40 1.1.2.2 bouyer */
41 1.1.2.2 bouyer
42 1.1.2.2 bouyer #include <sys/param.h>
43 1.1.2.2 bouyer #include <sys/socket.h>
44 1.1.2.2 bouyer
45 1.1.2.2 bouyer #ifdef _STANDALONE
46 1.1.2.2 bouyer #include <lib/libkern/libkern.h>
47 1.1.2.2 bouyer #else
48 1.1.2.2 bouyer #include <string.h>
49 1.1.2.2 bouyer #endif
50 1.1.2.2 bouyer
51 1.1.2.2 bouyer #include <net/if.h>
52 1.1.2.2 bouyer #include <net/if_ether.h>
53 1.1.2.2 bouyer
54 1.1.2.2 bouyer #include <netinet/in.h>
55 1.1.2.2 bouyer #include <netinet/in_systm.h>
56 1.1.2.2 bouyer #include <netinet/ip.h>
57 1.1.2.2 bouyer #include <netinet/ip_var.h>
58 1.1.2.2 bouyer #include <netinet/udp.h>
59 1.1.2.2 bouyer #include <netinet/udp_var.h>
60 1.1.2.2 bouyer
61 1.1.2.2 bouyer #include "stand.h"
62 1.1.2.2 bouyer #include "net.h"
63 1.1.2.2 bouyer
64 1.1.2.2 bouyer
65 1.1.2.2 bouyer /* Caller must leave room for ethernet, ip and udp headers in front!! */
66 1.1.2.2 bouyer ssize_t
67 1.1.2.2 bouyer sendudp(d, pkt, len)
68 1.1.2.2 bouyer struct iodesc *d;
69 1.1.2.2 bouyer void *pkt;
70 1.1.2.2 bouyer size_t len;
71 1.1.2.2 bouyer {
72 1.1.2.2 bouyer ssize_t cc;
73 1.1.2.2 bouyer struct ip *ip;
74 1.1.2.2 bouyer struct udphdr *uh;
75 1.1.2.2 bouyer u_char *ea;
76 1.1.2.2 bouyer
77 1.1.2.2 bouyer #ifdef NET_DEBUG
78 1.1.2.2 bouyer if (debug) {
79 1.1.2.2 bouyer printf("sendudp: d=%lx called.\n", (long)d);
80 1.1.2.2 bouyer if (d) {
81 1.1.2.2 bouyer printf("saddr: %s:%d",
82 1.1.2.2 bouyer inet_ntoa(d->myip), ntohs(d->myport));
83 1.1.2.2 bouyer printf(" daddr: %s:%d\n",
84 1.1.2.2 bouyer inet_ntoa(d->destip), ntohs(d->destport));
85 1.1.2.2 bouyer }
86 1.1.2.2 bouyer }
87 1.1.2.2 bouyer #endif
88 1.1.2.2 bouyer
89 1.1.2.2 bouyer uh = (struct udphdr *)pkt - 1;
90 1.1.2.2 bouyer ip = (struct ip *)uh - 1;
91 1.1.2.2 bouyer len += sizeof(*ip) + sizeof(*uh);
92 1.1.2.2 bouyer
93 1.1.2.2 bouyer bzero(ip, sizeof(*ip) + sizeof(*uh));
94 1.1.2.2 bouyer
95 1.1.2.2 bouyer ip->ip_v = IPVERSION; /* half-char */
96 1.1.2.2 bouyer ip->ip_hl = sizeof(*ip) >> 2; /* half-char */
97 1.1.2.2 bouyer ip->ip_len = htons(len);
98 1.1.2.2 bouyer ip->ip_p = IPPROTO_UDP; /* char */
99 1.1.2.2 bouyer ip->ip_ttl = IP_TTL; /* char */
100 1.1.2.2 bouyer ip->ip_src = d->myip;
101 1.1.2.2 bouyer ip->ip_dst = d->destip;
102 1.1.2.2 bouyer ip->ip_sum = in_cksum(ip, sizeof(*ip)); /* short, but special */
103 1.1.2.2 bouyer
104 1.1.2.2 bouyer uh->uh_sport = d->myport;
105 1.1.2.2 bouyer uh->uh_dport = d->destport;
106 1.1.2.2 bouyer uh->uh_ulen = htons(len - sizeof(*ip));
107 1.1.2.2 bouyer
108 1.1.2.2 bouyer #ifndef UDP_NO_CKSUM
109 1.1.2.2 bouyer {
110 1.1.2.2 bouyer struct udpiphdr *ui;
111 1.1.2.2 bouyer struct ip tip;
112 1.1.2.2 bouyer
113 1.1.2.2 bouyer /* Calculate checksum (must save and restore ip header) */
114 1.1.2.2 bouyer tip = *ip;
115 1.1.2.2 bouyer ui = (struct udpiphdr *)ip;
116 1.1.2.2 bouyer bzero(ui->ui_x1, sizeof(ui->ui_x1));
117 1.1.2.2 bouyer ui->ui_len = uh->uh_ulen;
118 1.1.2.2 bouyer uh->uh_sum = in_cksum(ui, len);
119 1.1.2.2 bouyer *ip = tip;
120 1.1.2.2 bouyer }
121 1.1.2.2 bouyer #endif
122 1.1.2.2 bouyer
123 1.1.2.2 bouyer if (ip->ip_dst.s_addr == INADDR_BROADCAST || ip->ip_src.s_addr == 0 ||
124 1.1.2.2 bouyer netmask == 0 || SAMENET(ip->ip_src, ip->ip_dst, netmask))
125 1.1.2.2 bouyer ea = arpwhohas(d, ip->ip_dst);
126 1.1.2.2 bouyer else
127 1.1.2.2 bouyer ea = arpwhohas(d, gateip);
128 1.1.2.2 bouyer
129 1.1.2.2 bouyer cc = sendether(d, ip, len, ea, ETHERTYPE_IP);
130 1.1.2.2 bouyer if (cc == -1)
131 1.1.2.2 bouyer return (-1);
132 1.1.2.2 bouyer if (cc != len)
133 1.1.2.2 bouyer panic("sendudp: bad write (%d != %d)", cc, len);
134 1.1.2.2 bouyer return (cc - (sizeof(*ip) + sizeof(*uh)));
135 1.1.2.2 bouyer }
136 1.1.2.2 bouyer
137 1.1.2.2 bouyer /*
138 1.1.2.2 bouyer * Receive a UDP packet and validate it is for us.
139 1.1.2.2 bouyer * Caller leaves room for the headers (Ether, IP, UDP)
140 1.1.2.2 bouyer */
141 1.1.2.2 bouyer ssize_t
142 1.1.2.2 bouyer readudp(d, pkt, len, tleft)
143 1.1.2.2 bouyer struct iodesc *d;
144 1.1.2.2 bouyer void *pkt;
145 1.1.2.2 bouyer size_t len;
146 1.1.2.2 bouyer time_t tleft;
147 1.1.2.2 bouyer {
148 1.1.2.2 bouyer ssize_t n;
149 1.1.2.2 bouyer size_t hlen;
150 1.1.2.2 bouyer struct ip *ip;
151 1.1.2.2 bouyer struct udphdr *uh;
152 1.1.2.2 bouyer u_int16_t etype; /* host order */
153 1.1.2.2 bouyer
154 1.1.2.2 bouyer #ifdef NET_DEBUG
155 1.1.2.2 bouyer if (debug)
156 1.1.2.2 bouyer printf("readudp: called\n");
157 1.1.2.2 bouyer #endif
158 1.1.2.2 bouyer
159 1.1.2.2 bouyer uh = (struct udphdr *)pkt - 1;
160 1.1.2.2 bouyer ip = (struct ip *)uh - 1;
161 1.1.2.2 bouyer
162 1.1.2.2 bouyer n = readether(d, ip, len + sizeof(*ip) + sizeof(*uh), tleft, &etype);
163 1.1.2.2 bouyer if (n == -1 || n < sizeof(*ip) + sizeof(*uh))
164 1.1.2.2 bouyer return -1;
165 1.1.2.2 bouyer
166 1.1.2.2 bouyer /* Ethernet address checks now in readether() */
167 1.1.2.2 bouyer
168 1.1.2.2 bouyer /* Need to respond to ARP requests. */
169 1.1.2.2 bouyer if (etype == ETHERTYPE_ARP) {
170 1.1.2.2 bouyer struct arphdr *ah = (void *)ip;
171 1.1.2.2 bouyer if (ah->ar_op == htons(ARPOP_REQUEST)) {
172 1.1.2.2 bouyer /* Send ARP reply */
173 1.1.2.2 bouyer arp_reply(d, ah);
174 1.1.2.2 bouyer }
175 1.1.2.2 bouyer return -1;
176 1.1.2.2 bouyer }
177 1.1.2.2 bouyer
178 1.1.2.2 bouyer if (etype != ETHERTYPE_IP) {
179 1.1.2.2 bouyer #ifdef NET_DEBUG
180 1.1.2.2 bouyer if (debug)
181 1.1.2.2 bouyer printf("readudp: not IP. ether_type=%x\n", etype);
182 1.1.2.2 bouyer #endif
183 1.1.2.2 bouyer return -1;
184 1.1.2.2 bouyer }
185 1.1.2.2 bouyer
186 1.1.2.2 bouyer /* Check ip header */
187 1.1.2.2 bouyer if (ip->ip_v != IPVERSION ||
188 1.1.2.2 bouyer ip->ip_p != IPPROTO_UDP) { /* half char */
189 1.1.2.2 bouyer #ifdef NET_DEBUG
190 1.1.2.2 bouyer if (debug)
191 1.1.2.2 bouyer printf("readudp: IP version or not UDP. ip_v=%d ip_p=%d\n", ip->ip_v, ip->ip_p);
192 1.1.2.2 bouyer #endif
193 1.1.2.2 bouyer return -1;
194 1.1.2.2 bouyer }
195 1.1.2.2 bouyer
196 1.1.2.2 bouyer hlen = ip->ip_hl << 2;
197 1.1.2.2 bouyer if (hlen < sizeof(*ip) ||
198 1.1.2.2 bouyer in_cksum(ip, hlen) != 0) {
199 1.1.2.2 bouyer #ifdef NET_DEBUG
200 1.1.2.2 bouyer if (debug)
201 1.1.2.2 bouyer printf("readudp: short hdr or bad cksum.\n");
202 1.1.2.2 bouyer #endif
203 1.1.2.2 bouyer return -1;
204 1.1.2.2 bouyer }
205 1.1.2.2 bouyer if (n < ntohs(ip->ip_len)) {
206 1.1.2.2 bouyer #ifdef NET_DEBUG
207 1.1.2.2 bouyer if (debug)
208 1.1.2.2 bouyer printf("readudp: bad length %d < %d.\n",
209 1.1.2.2 bouyer (int)n, ntohs(ip->ip_len));
210 1.1.2.2 bouyer #endif
211 1.1.2.2 bouyer return -1;
212 1.1.2.2 bouyer }
213 1.1.2.2 bouyer if (d->myip.s_addr && ip->ip_dst.s_addr != d->myip.s_addr) {
214 1.1.2.2 bouyer #ifdef NET_DEBUG
215 1.1.2.2 bouyer if (debug) {
216 1.1.2.2 bouyer printf("readudp: bad saddr %s != ", inet_ntoa(d->myip));
217 1.1.2.2 bouyer printf("%s\n", inet_ntoa(ip->ip_dst));
218 1.1.2.2 bouyer }
219 1.1.2.2 bouyer #endif
220 1.1.2.2 bouyer return -1;
221 1.1.2.2 bouyer }
222 1.1.2.2 bouyer
223 1.1.2.2 bouyer /* If there were ip options, make them go away */
224 1.1.2.2 bouyer if (hlen != sizeof(*ip)) {
225 1.1.2.2 bouyer bcopy(((u_char *)ip) + hlen, uh, len - hlen);
226 1.1.2.2 bouyer ip->ip_len = htons(sizeof(*ip));
227 1.1.2.2 bouyer n -= hlen - sizeof(*ip);
228 1.1.2.2 bouyer }
229 1.1.2.2 bouyer if (uh->uh_dport != d->myport) {
230 1.1.2.2 bouyer #ifdef NET_DEBUG
231 1.1.2.2 bouyer if (debug)
232 1.1.2.2 bouyer printf("readudp: bad dport %d != %d\n",
233 1.1.2.2 bouyer d->myport, ntohs(uh->uh_dport));
234 1.1.2.2 bouyer #endif
235 1.1.2.2 bouyer return -1;
236 1.1.2.2 bouyer }
237 1.1.2.2 bouyer
238 1.1.2.2 bouyer #ifndef UDP_NO_CKSUM
239 1.1.2.2 bouyer if (uh->uh_sum) {
240 1.1.2.2 bouyer struct udpiphdr *ui;
241 1.1.2.2 bouyer struct ip tip;
242 1.1.2.2 bouyer
243 1.1.2.2 bouyer n = ntohs(uh->uh_ulen) + sizeof(*ip);
244 1.1.2.2 bouyer if (n > RECV_SIZE - ETHER_SIZE) {
245 1.1.2.2 bouyer printf("readudp: huge packet, udp len %d\n", (int)n);
246 1.1.2.2 bouyer return -1;
247 1.1.2.2 bouyer }
248 1.1.2.2 bouyer
249 1.1.2.2 bouyer /* Check checksum (must save and restore ip header) */
250 1.1.2.2 bouyer tip = *ip;
251 1.1.2.2 bouyer ui = (struct udpiphdr *)ip;
252 1.1.2.2 bouyer bzero(ui->ui_x1, sizeof(ui->ui_x1));
253 1.1.2.2 bouyer ui->ui_len = uh->uh_ulen;
254 1.1.2.2 bouyer if (in_cksum(ui, n) != 0) {
255 1.1.2.2 bouyer #ifdef NET_DEBUG
256 1.1.2.2 bouyer if (debug)
257 1.1.2.2 bouyer printf("readudp: bad cksum\n");
258 1.1.2.2 bouyer #endif
259 1.1.2.2 bouyer *ip = tip;
260 1.1.2.2 bouyer return -1;
261 1.1.2.2 bouyer }
262 1.1.2.2 bouyer *ip = tip;
263 1.1.2.2 bouyer }
264 1.1.2.2 bouyer #endif
265 1.1.2.2 bouyer if (ntohs(uh->uh_ulen) < sizeof(*uh)) {
266 1.1.2.2 bouyer #ifdef NET_DEBUG
267 1.1.2.2 bouyer if (debug)
268 1.1.2.2 bouyer printf("readudp: bad udp len %d < %d\n",
269 1.1.2.2 bouyer ntohs(uh->uh_ulen), (int)sizeof(*uh));
270 1.1.2.2 bouyer #endif
271 1.1.2.2 bouyer return -1;
272 1.1.2.2 bouyer }
273 1.1.2.2 bouyer
274 1.1.2.2 bouyer n -= sizeof(*ip) + sizeof(*uh);
275 1.1.2.2 bouyer return (n);
276 1.1.2.2 bouyer }
277