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