xref: /freebsd/stand/libsa/udp.c (revision ca987d4641cdcd7f27e153db17c5bf064934faf5)
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  * @(#) Header: net.c,v 1.9 93/08/06 19:32:15 leres Exp  (LBL)
36*ca987d46SWarner Losh  */
37*ca987d46SWarner Losh 
38*ca987d46SWarner Losh #include <sys/cdefs.h>
39*ca987d46SWarner Losh __FBSDID("$FreeBSD$");
40*ca987d46SWarner Losh 
41*ca987d46SWarner Losh #include <sys/param.h>
42*ca987d46SWarner Losh #include <sys/socket.h>
43*ca987d46SWarner Losh 
44*ca987d46SWarner Losh #include <string.h>
45*ca987d46SWarner Losh 
46*ca987d46SWarner Losh #include <net/if.h>
47*ca987d46SWarner Losh #include <netinet/in.h>
48*ca987d46SWarner Losh #include <netinet/if_ether.h>
49*ca987d46SWarner Losh #include <netinet/in_systm.h>
50*ca987d46SWarner Losh 
51*ca987d46SWarner Losh #include <netinet/in_pcb.h>
52*ca987d46SWarner Losh #include <netinet/ip.h>
53*ca987d46SWarner Losh #include <netinet/ip_var.h>
54*ca987d46SWarner Losh #include <netinet/udp.h>
55*ca987d46SWarner Losh #include <netinet/udp_var.h>
56*ca987d46SWarner Losh 
57*ca987d46SWarner Losh #include "stand.h"
58*ca987d46SWarner Losh #include "net.h"
59*ca987d46SWarner Losh 
60*ca987d46SWarner Losh /* Caller must leave room for ethernet, ip and udp headers in front!! */
61*ca987d46SWarner Losh ssize_t
62*ca987d46SWarner Losh sendudp(struct iodesc *d, void *pkt, size_t len)
63*ca987d46SWarner Losh {
64*ca987d46SWarner Losh 	ssize_t cc;
65*ca987d46SWarner Losh 	struct udpiphdr *ui;
66*ca987d46SWarner Losh 	struct udphdr *uh;
67*ca987d46SWarner Losh 
68*ca987d46SWarner Losh #ifdef NET_DEBUG
69*ca987d46SWarner Losh  	if (debug) {
70*ca987d46SWarner Losh 		printf("sendudp: d=%lx called.\n", (long)d);
71*ca987d46SWarner Losh 		if (d) {
72*ca987d46SWarner Losh 			printf("saddr: %s:%d",
73*ca987d46SWarner Losh 			    inet_ntoa(d->myip), ntohs(d->myport));
74*ca987d46SWarner Losh 			printf(" daddr: %s:%d\n",
75*ca987d46SWarner Losh 			    inet_ntoa(d->destip), ntohs(d->destport));
76*ca987d46SWarner Losh 		}
77*ca987d46SWarner Losh 	}
78*ca987d46SWarner Losh #endif
79*ca987d46SWarner Losh 
80*ca987d46SWarner Losh 	ui = (struct udpiphdr *)pkt - 1;
81*ca987d46SWarner Losh 	bzero(ui, sizeof(*ui));
82*ca987d46SWarner Losh 
83*ca987d46SWarner Losh 	uh = (struct udphdr *)pkt - 1;
84*ca987d46SWarner Losh 	len += sizeof(*uh);
85*ca987d46SWarner Losh 
86*ca987d46SWarner Losh 	uh->uh_sport = d->myport;
87*ca987d46SWarner Losh 	uh->uh_dport = d->destport;
88*ca987d46SWarner Losh 	uh->uh_ulen = htons(len);
89*ca987d46SWarner Losh 
90*ca987d46SWarner Losh 	ui->ui_pr = IPPROTO_UDP;
91*ca987d46SWarner Losh 	ui->ui_len = uh->uh_ulen;
92*ca987d46SWarner Losh 	ui->ui_src = d->myip;
93*ca987d46SWarner Losh 	ui->ui_dst = d->destip;
94*ca987d46SWarner Losh 
95*ca987d46SWarner Losh #ifndef UDP_NO_CKSUM
96*ca987d46SWarner Losh 	uh->uh_sum = in_cksum(ui, len + sizeof (struct ip));
97*ca987d46SWarner Losh #endif
98*ca987d46SWarner Losh 
99*ca987d46SWarner Losh 	cc = sendip(d, uh, len, IPPROTO_UDP);
100*ca987d46SWarner Losh 	if (cc == -1)
101*ca987d46SWarner Losh 		return (-1);
102*ca987d46SWarner Losh 	if (cc != len)
103*ca987d46SWarner Losh 		panic("sendudp: bad write (%zd != %zd)", cc, len);
104*ca987d46SWarner Losh 	return (cc - sizeof(*uh));
105*ca987d46SWarner Losh }
106*ca987d46SWarner Losh 
107*ca987d46SWarner Losh /*
108*ca987d46SWarner Losh  * Receive a UDP packet and validate it is for us.
109*ca987d46SWarner Losh  */
110*ca987d46SWarner Losh ssize_t
111*ca987d46SWarner Losh readudp(struct iodesc *d, void **pkt, void **payload, time_t tleft)
112*ca987d46SWarner Losh {
113*ca987d46SWarner Losh 	ssize_t n;
114*ca987d46SWarner Losh 	struct udphdr *uh;
115*ca987d46SWarner Losh 	void *ptr;
116*ca987d46SWarner Losh 
117*ca987d46SWarner Losh #ifdef NET_DEBUG
118*ca987d46SWarner Losh 	if (debug)
119*ca987d46SWarner Losh 		printf("readudp: called\n");
120*ca987d46SWarner Losh #endif
121*ca987d46SWarner Losh 
122*ca987d46SWarner Losh 	uh = NULL;
123*ca987d46SWarner Losh 	ptr = NULL;
124*ca987d46SWarner Losh 	n = readip(d, &ptr, (void **)&uh, tleft, IPPROTO_UDP);
125*ca987d46SWarner Losh 	if (n == -1 || n < sizeof(*uh) || n != ntohs(uh->uh_ulen)) {
126*ca987d46SWarner Losh 		free(ptr);
127*ca987d46SWarner Losh 		return (-1);
128*ca987d46SWarner Losh 	}
129*ca987d46SWarner Losh 
130*ca987d46SWarner Losh 	if (uh->uh_dport != d->myport) {
131*ca987d46SWarner Losh #ifdef NET_DEBUG
132*ca987d46SWarner Losh 		if (debug)
133*ca987d46SWarner Losh 			printf("readudp: bad dport %d != %d\n",
134*ca987d46SWarner Losh 				d->myport, ntohs(uh->uh_dport));
135*ca987d46SWarner Losh #endif
136*ca987d46SWarner Losh 		free(ptr);
137*ca987d46SWarner Losh 		return (-1);
138*ca987d46SWarner Losh 	}
139*ca987d46SWarner Losh 
140*ca987d46SWarner Losh #ifndef UDP_NO_CKSUM
141*ca987d46SWarner Losh 	if (uh->uh_sum) {
142*ca987d46SWarner Losh 		struct udpiphdr *ui;
143*ca987d46SWarner Losh 		struct ip *ip;
144*ca987d46SWarner Losh 		struct ip tip;
145*ca987d46SWarner Losh 
146*ca987d46SWarner Losh 		n = ntohs(uh->uh_ulen) + sizeof(*ip);
147*ca987d46SWarner Losh 
148*ca987d46SWarner Losh 		/* Check checksum (must save and restore ip header) */
149*ca987d46SWarner Losh 		ip = (struct ip *)uh - 1;
150*ca987d46SWarner Losh 		tip = *ip;
151*ca987d46SWarner Losh 		ui = (struct udpiphdr *)ip;
152*ca987d46SWarner Losh 		bzero(&ui->ui_x1, sizeof(ui->ui_x1));
153*ca987d46SWarner Losh 		ui->ui_len = uh->uh_ulen;
154*ca987d46SWarner Losh 		if (in_cksum(ui, n) != 0) {
155*ca987d46SWarner Losh #ifdef NET_DEBUG
156*ca987d46SWarner Losh 			if (debug)
157*ca987d46SWarner Losh 				printf("readudp: bad cksum\n");
158*ca987d46SWarner Losh #endif
159*ca987d46SWarner Losh 			free(ptr);
160*ca987d46SWarner Losh 			return (-1);
161*ca987d46SWarner Losh 		}
162*ca987d46SWarner Losh 		*ip = tip;
163*ca987d46SWarner Losh 	}
164*ca987d46SWarner Losh #endif
165*ca987d46SWarner Losh 	if (ntohs(uh->uh_ulen) < sizeof(*uh)) {
166*ca987d46SWarner Losh #ifdef NET_DEBUG
167*ca987d46SWarner Losh 		if (debug)
168*ca987d46SWarner Losh 			printf("readudp: bad udp len %d < %d\n",
169*ca987d46SWarner Losh 				ntohs(uh->uh_ulen), (int)sizeof(*uh));
170*ca987d46SWarner Losh #endif
171*ca987d46SWarner Losh 		free(ptr);
172*ca987d46SWarner Losh 		return (-1);
173*ca987d46SWarner Losh 	}
174*ca987d46SWarner Losh 
175*ca987d46SWarner Losh 	n = (n > (ntohs(uh->uh_ulen) - sizeof(*uh))) ?
176*ca987d46SWarner Losh 	    ntohs(uh->uh_ulen) - sizeof(*uh) : n;
177*ca987d46SWarner Losh 	*pkt = ptr;
178*ca987d46SWarner Losh 	*payload = (void *)((uintptr_t)uh + sizeof(*uh));
179*ca987d46SWarner Losh 	return (n);
180*ca987d46SWarner Losh }
181