xref: /freebsd/stand/libsa/udp.c (revision 7c43148a974877188a930e4078a164f83da8e652)
1*ca987d46SWarner Losh /* Taken from $NetBSD: net.c,v 1.20 1997/12/26 22:41:30 scottr Exp $	*/
2*ca987d46SWarner Losh 
3*ca987d46SWarner Losh /*
4*ca987d46SWarner Losh  * Copyright (c) 1992 Regents of the University of California.
5*ca987d46SWarner Losh  * All rights reserved.
6*ca987d46SWarner Losh  *
7*ca987d46SWarner Losh  * This software was developed by the Computer Systems Engineering group
8*ca987d46SWarner Losh  * at Lawrence Berkeley Laboratory under DARPA contract BG 91-66 and
9*ca987d46SWarner Losh  * contributed to Berkeley.
10*ca987d46SWarner Losh  *
11*ca987d46SWarner Losh  * Redistribution and use in source and binary forms, with or without
12*ca987d46SWarner Losh  * modification, are permitted provided that the following conditions
13*ca987d46SWarner Losh  * are met:
14*ca987d46SWarner Losh  * 1. Redistributions of source code must retain the above copyright
15*ca987d46SWarner Losh  *    notice, this list of conditions and the following disclaimer.
16*ca987d46SWarner Losh  * 2. Redistributions in binary form must reproduce the above copyright
17*ca987d46SWarner Losh  *    notice, this list of conditions and the following disclaimer in the
18*ca987d46SWarner Losh  *    documentation and/or other materials provided with the distribution.
19*ca987d46SWarner Losh  * 3. Neither the name of the University nor the names of its contributors
20*ca987d46SWarner Losh  *    may be used to endorse or promote products derived from this software
21*ca987d46SWarner Losh  *    without specific prior written permission.
22*ca987d46SWarner Losh  *
23*ca987d46SWarner Losh  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24*ca987d46SWarner Losh  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25*ca987d46SWarner Losh  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26*ca987d46SWarner Losh  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27*ca987d46SWarner Losh  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28*ca987d46SWarner Losh  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29*ca987d46SWarner Losh  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30*ca987d46SWarner Losh  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31*ca987d46SWarner Losh  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32*ca987d46SWarner Losh  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33*ca987d46SWarner Losh  * SUCH DAMAGE.
34*ca987d46SWarner Losh  */
35*ca987d46SWarner Losh 
36*ca987d46SWarner Losh #include <sys/param.h>
37*ca987d46SWarner Losh #include <sys/socket.h>
38*ca987d46SWarner Losh 
39*ca987d46SWarner Losh #include <string.h>
40*ca987d46SWarner Losh 
41*ca987d46SWarner Losh #include <net/if.h>
42*ca987d46SWarner Losh #include <netinet/in.h>
43*ca987d46SWarner Losh #include <netinet/if_ether.h>
44*ca987d46SWarner Losh #include <netinet/in_systm.h>
45*ca987d46SWarner Losh 
46*ca987d46SWarner Losh #include <netinet/ip.h>
47*ca987d46SWarner Losh #include <netinet/ip_var.h>
48*ca987d46SWarner Losh #include <netinet/udp.h>
49*ca987d46SWarner Losh #include <netinet/udp_var.h>
50*ca987d46SWarner Losh 
51*ca987d46SWarner Losh #include "stand.h"
52*ca987d46SWarner Losh #include "net.h"
53*ca987d46SWarner Losh 
54*ca987d46SWarner Losh /* Caller must leave room for ethernet, ip and udp headers in front!! */
55*ca987d46SWarner Losh ssize_t
sendudp(struct iodesc * d,void * pkt,size_t len)56*ca987d46SWarner Losh sendudp(struct iodesc *d, void *pkt, size_t len)
57*ca987d46SWarner Losh {
58*ca987d46SWarner Losh 	ssize_t cc;
59*ca987d46SWarner Losh 	struct udpiphdr *ui;
60*ca987d46SWarner Losh 	struct udphdr *uh;
61*ca987d46SWarner Losh 
62*ca987d46SWarner Losh #ifdef NET_DEBUG
63*ca987d46SWarner Losh  	if (debug) {
64*ca987d46SWarner Losh 		printf("sendudp: d=%lx called.\n", (long)d);
65*ca987d46SWarner Losh 		if (d) {
66*ca987d46SWarner Losh 			printf("saddr: %s:%d",
67*ca987d46SWarner Losh 			    inet_ntoa(d->myip), ntohs(d->myport));
68*ca987d46SWarner Losh 			printf(" daddr: %s:%d\n",
69*ca987d46SWarner Losh 			    inet_ntoa(d->destip), ntohs(d->destport));
70*ca987d46SWarner Losh 		}
71*ca987d46SWarner Losh 	}
72*ca987d46SWarner Losh #endif
73*ca987d46SWarner Losh 
74*ca987d46SWarner Losh 	ui = (struct udpiphdr *)pkt - 1;
75*ca987d46SWarner Losh 	bzero(ui, sizeof(*ui));
76*ca987d46SWarner Losh 
77*ca987d46SWarner Losh 	uh = (struct udphdr *)pkt - 1;
78*ca987d46SWarner Losh 	len += sizeof(*uh);
79*ca987d46SWarner Losh 
80*ca987d46SWarner Losh 	uh->uh_sport = d->myport;
81*ca987d46SWarner Losh 	uh->uh_dport = d->destport;
82*ca987d46SWarner Losh 	uh->uh_ulen = htons(len);
83*ca987d46SWarner Losh 
84*ca987d46SWarner Losh 	ui->ui_pr = IPPROTO_UDP;
85*ca987d46SWarner Losh 	ui->ui_len = uh->uh_ulen;
86*ca987d46SWarner Losh 	ui->ui_src = d->myip;
87*ca987d46SWarner Losh 	ui->ui_dst = d->destip;
88*ca987d46SWarner Losh 
89*ca987d46SWarner Losh #ifndef UDP_NO_CKSUM
90*ca987d46SWarner Losh 	uh->uh_sum = in_cksum(ui, len + sizeof (struct ip));
91*ca987d46SWarner Losh #endif
92*ca987d46SWarner Losh 
93*ca987d46SWarner Losh 	cc = sendip(d, uh, len, IPPROTO_UDP);
94*ca987d46SWarner Losh 	if (cc == -1)
95*ca987d46SWarner Losh 		return (-1);
96*ca987d46SWarner Losh 	if (cc != len)
97*ca987d46SWarner Losh 		panic("sendudp: bad write (%zd != %zd)", cc, len);
98*ca987d46SWarner Losh 	return (cc - sizeof(*uh));
99*ca987d46SWarner Losh }
100*ca987d46SWarner Losh 
101*ca987d46SWarner Losh /*
102*ca987d46SWarner Losh  * Receive a UDP packet and validate it is for us.
103*ca987d46SWarner Losh  */
104*ca987d46SWarner Losh ssize_t
readudp(struct iodesc * d,void ** pkt,void ** payload,time_t tleft)105*ca987d46SWarner Losh readudp(struct iodesc *d, void **pkt, void **payload, time_t tleft)
106*ca987d46SWarner Losh {
107*ca987d46SWarner Losh 	ssize_t n;
108*ca987d46SWarner Losh 	struct udphdr *uh;
109*ca987d46SWarner Losh 	void *ptr;
110*ca987d46SWarner Losh 
111*ca987d46SWarner Losh #ifdef NET_DEBUG
112*ca987d46SWarner Losh 	if (debug)
113*ca987d46SWarner Losh 		printf("readudp: called\n");
114*ca987d46SWarner Losh #endif
115*ca987d46SWarner Losh 
116*ca987d46SWarner Losh 	uh = NULL;
117*ca987d46SWarner Losh 	ptr = NULL;
118*ca987d46SWarner Losh 	n = readip(d, &ptr, (void **)&uh, tleft, IPPROTO_UDP);
119*ca987d46SWarner Losh 	if (n == -1 || n < sizeof(*uh) || n != ntohs(uh->uh_ulen)) {
120*ca987d46SWarner Losh 		free(ptr);
121*ca987d46SWarner Losh 		return (-1);
122*ca987d46SWarner Losh 	}
123*ca987d46SWarner Losh 
124*ca987d46SWarner Losh 	if (uh->uh_dport != d->myport) {
125*ca987d46SWarner Losh #ifdef NET_DEBUG
126*ca987d46SWarner Losh 		if (debug)
127*ca987d46SWarner Losh 			printf("readudp: bad dport %d != %d\n",
128*ca987d46SWarner Losh 				d->myport, ntohs(uh->uh_dport));
129*ca987d46SWarner Losh #endif
130*ca987d46SWarner Losh 		free(ptr);
131*ca987d46SWarner Losh 		return (-1);
132*ca987d46SWarner Losh 	}
133*ca987d46SWarner Losh 
134*ca987d46SWarner Losh #ifndef UDP_NO_CKSUM
135*ca987d46SWarner Losh 	if (uh->uh_sum) {
136*ca987d46SWarner Losh 		struct udpiphdr *ui;
137*ca987d46SWarner Losh 		struct ip *ip;
138*ca987d46SWarner Losh 		struct ip tip;
139*ca987d46SWarner Losh 
140*ca987d46SWarner Losh 		n = ntohs(uh->uh_ulen) + sizeof(*ip);
141*ca987d46SWarner Losh 
142*ca987d46SWarner Losh 		/* Check checksum (must save and restore ip header) */
143*ca987d46SWarner Losh 		ip = (struct ip *)uh - 1;
144*ca987d46SWarner Losh 		tip = *ip;
145*ca987d46SWarner Losh 		ui = (struct udpiphdr *)ip;
146*ca987d46SWarner Losh 		bzero(&ui->ui_x1, sizeof(ui->ui_x1));
147*ca987d46SWarner Losh 		ui->ui_len = uh->uh_ulen;
148*ca987d46SWarner Losh 		if (in_cksum(ui, n) != 0) {
149*ca987d46SWarner Losh #ifdef NET_DEBUG
150*ca987d46SWarner Losh 			if (debug)
151*ca987d46SWarner Losh 				printf("readudp: bad cksum\n");
152*ca987d46SWarner Losh #endif
153*ca987d46SWarner Losh 			free(ptr);
154*ca987d46SWarner Losh 			return (-1);
155*ca987d46SWarner Losh 		}
156*ca987d46SWarner Losh 		*ip = tip;
157*ca987d46SWarner Losh 	}
158*ca987d46SWarner Losh #endif
159*ca987d46SWarner Losh 	if (ntohs(uh->uh_ulen) < sizeof(*uh)) {
160*ca987d46SWarner Losh #ifdef NET_DEBUG
161*ca987d46SWarner Losh 		if (debug)
162*ca987d46SWarner Losh 			printf("readudp: bad udp len %d < %d\n",
163*ca987d46SWarner Losh 				ntohs(uh->uh_ulen), (int)sizeof(*uh));
164*ca987d46SWarner Losh #endif
165*ca987d46SWarner Losh 		free(ptr);
166*ca987d46SWarner Losh 		return (-1);
167*ca987d46SWarner Losh 	}
168*ca987d46SWarner Losh 
169*ca987d46SWarner Losh 	n = (n > (ntohs(uh->uh_ulen) - sizeof(*uh))) ?
170*ca987d46SWarner Losh 	    ntohs(uh->uh_ulen) - sizeof(*uh) : n;
171*ca987d46SWarner Losh 	*pkt = ptr;
172*ca987d46SWarner Losh 	*payload = (void *)((uintptr_t)uh + sizeof(*uh));
173*ca987d46SWarner Losh 	return (n);
174*ca987d46SWarner Losh }
175