Expand PMF_FN_* macros.
[netbsd-mini2440.git] / sys / lib / libsa / udp.c
blobf0cdd74f434eac461cfa0dfac70789740bab4f71
1 /* $NetBSD: udp.c,v 1.7 2008/03/25 21:23:51 christos 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. 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.
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.
39 * @(#) Header: net.c,v 1.9 93/08/06 19:32:15 leres Exp (LBL)
42 #include <sys/param.h>
43 #include <sys/socket.h>
45 #ifdef _STANDALONE
46 #include <lib/libkern/libkern.h>
47 #else
48 #include <string.h>
49 #endif
51 #include <net/if.h>
52 #include <net/if_ether.h>
54 #include <netinet/in.h>
55 #include <netinet/in_systm.h>
56 #include <netinet/ip.h>
57 #include <netinet/ip_var.h>
58 #include <netinet/udp.h>
59 #include <netinet/udp_var.h>
61 #include "stand.h"
62 #include "net.h"
65 /* Caller must leave room for ethernet, ip and udp headers in front!! */
66 ssize_t
67 sendudp(struct iodesc *d, void *pkt, size_t len)
69 ssize_t cc;
70 struct ip *ip;
71 struct udphdr *uh;
72 u_char *ea;
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));
84 #endif
86 uh = (struct udphdr *)pkt - 1;
87 ip = (struct ip *)uh - 1;
88 len += sizeof(*ip) + sizeof(*uh);
90 (void)memset(ip, 0, sizeof(*ip) + sizeof(*uh));
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 = IPDEFTTL; /* char */
97 ip->ip_src = d->myip;
98 ip->ip_dst = d->destip;
99 ip->ip_sum = ip_cksum(ip, sizeof(*ip)); /* short, but special */
101 uh->uh_sport = d->myport;
102 uh->uh_dport = d->destport;
103 uh->uh_ulen = htons(len - sizeof(*ip));
105 #ifndef UDP_NO_CKSUM
107 struct udpiphdr *ui;
108 struct ip tip;
110 /* Calculate checksum (must save and restore ip header) */
111 tip = *ip;
112 ui = (struct udpiphdr *)ip;
113 (void)memset(ui->ui_x1, 0, sizeof(ui->ui_x1));
114 ui->ui_len = uh->uh_ulen;
115 uh->uh_sum = ip_cksum(ui, len);
116 *ip = tip;
118 #endif
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);
126 cc = sendether(d, ip, len, ea, ETHERTYPE_IP);
127 if (cc == -1)
128 return -1;
129 if ((size_t)cc != len)
130 panic("sendudp: bad write (%d != %d)", cc, len);
131 return (cc - (sizeof(*ip) + sizeof(*uh)));
135 * Receive a UDP packet and validate it is for us.
136 * Caller leaves room for the headers (Ether, IP, UDP)
138 ssize_t
139 readudp(struct iodesc *d, void *pkt, size_t len, saseconds_t tleft)
141 ssize_t n;
142 size_t hlen;
143 struct ip *ip;
144 struct udphdr *uh;
145 u_int16_t etype; /* host order */
147 #ifdef NET_DEBUG
148 if (debug)
149 printf("readudp: called\n");
150 #endif
152 uh = (struct udphdr *)pkt - 1;
153 ip = (struct ip *)uh - 1;
155 n = readether(d, ip, len + sizeof(*ip) + sizeof(*uh), tleft, &etype);
156 if (n == -1 || (size_t)n < sizeof(*ip) + sizeof(*uh))
157 return -1;
159 /* Ethernet address checks now in readether() */
161 /* Need to respond to ARP requests. */
162 if (etype == ETHERTYPE_ARP) {
163 struct arphdr *ah = (void *)ip;
164 if (ah->ar_op == htons(ARPOP_REQUEST)) {
165 /* Send ARP reply */
166 arp_reply(d, ah);
168 return -1;
171 if (etype != ETHERTYPE_IP) {
172 #ifdef NET_DEBUG
173 if (debug)
174 printf("readudp: not IP. ether_type=%x\n", etype);
175 #endif
176 return -1;
179 /* Check ip header */
180 if (ip->ip_v != IPVERSION ||
181 ip->ip_p != IPPROTO_UDP) { /* half char */
182 #ifdef NET_DEBUG
183 if (debug) {
184 printf("readudp: IP version or not UDP. "
185 "ip_v=%d ip_p=%d\n", ip->ip_v, ip->ip_p);
187 #endif
188 return -1;
191 hlen = ip->ip_hl << 2;
192 if (hlen < sizeof(*ip) || ip_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;
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;
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));
213 #endif
214 return -1;
217 /* If there were ip options, make them go away */
218 if (hlen != sizeof(*ip)) {
219 (void)memcpy(uh, ((u_char *)ip) + hlen, len - hlen);
220 ip->ip_len = htons(sizeof(*ip));
221 n -= hlen - sizeof(*ip);
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;
232 #ifndef UDP_NO_CKSUM
233 if (uh->uh_sum) {
234 struct udpiphdr *ui;
235 struct ip tip;
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;
243 /* Check checksum (must save and restore ip header) */
244 tip = *ip;
245 ui = (struct udpiphdr *)ip;
246 (void)memset(ui->ui_x1, 0, sizeof(ui->ui_x1));
247 ui->ui_len = uh->uh_ulen;
248 if (ip_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;
256 *ip = tip;
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;
268 n -= sizeof(*ip) + sizeof(*uh);
269 return n;