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