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