udp_usrreq.c revision 1.49 1 1.49 drochner /* $NetBSD: udp_usrreq.c,v 1.49 1999/07/06 14:37:47 drochner Exp $ */
2 1.48 itojun
3 1.48 itojun /*
4 1.48 itojun * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
5 1.48 itojun * All rights reserved.
6 1.48 itojun *
7 1.48 itojun * Redistribution and use in source and binary forms, with or without
8 1.48 itojun * modification, are permitted provided that the following conditions
9 1.48 itojun * are met:
10 1.48 itojun * 1. Redistributions of source code must retain the above copyright
11 1.48 itojun * notice, this list of conditions and the following disclaimer.
12 1.48 itojun * 2. Redistributions in binary form must reproduce the above copyright
13 1.48 itojun * notice, this list of conditions and the following disclaimer in the
14 1.48 itojun * documentation and/or other materials provided with the distribution.
15 1.48 itojun * 3. Neither the name of the project nor the names of its contributors
16 1.48 itojun * may be used to endorse or promote products derived from this software
17 1.48 itojun * without specific prior written permission.
18 1.48 itojun *
19 1.48 itojun * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
20 1.48 itojun * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 1.48 itojun * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 1.48 itojun * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
23 1.48 itojun * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 1.48 itojun * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 1.48 itojun * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 1.48 itojun * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 1.48 itojun * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 1.48 itojun * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 1.48 itojun * SUCH DAMAGE.
30 1.48 itojun */
31 1.14 cgd
32 1.1 cgd /*
33 1.44 thorpej * Copyright (c) 1982, 1986, 1988, 1990, 1993, 1995
34 1.13 mycroft * The Regents of the University of California. All rights reserved.
35 1.1 cgd *
36 1.1 cgd * Redistribution and use in source and binary forms, with or without
37 1.1 cgd * modification, are permitted provided that the following conditions
38 1.1 cgd * are met:
39 1.1 cgd * 1. Redistributions of source code must retain the above copyright
40 1.1 cgd * notice, this list of conditions and the following disclaimer.
41 1.1 cgd * 2. Redistributions in binary form must reproduce the above copyright
42 1.1 cgd * notice, this list of conditions and the following disclaimer in the
43 1.1 cgd * documentation and/or other materials provided with the distribution.
44 1.1 cgd * 3. All advertising materials mentioning features or use of this software
45 1.1 cgd * must display the following acknowledgement:
46 1.1 cgd * This product includes software developed by the University of
47 1.1 cgd * California, Berkeley and its contributors.
48 1.1 cgd * 4. Neither the name of the University nor the names of its contributors
49 1.1 cgd * may be used to endorse or promote products derived from this software
50 1.1 cgd * without specific prior written permission.
51 1.1 cgd *
52 1.1 cgd * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
53 1.1 cgd * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
54 1.1 cgd * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
55 1.1 cgd * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
56 1.1 cgd * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
57 1.1 cgd * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
58 1.1 cgd * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
59 1.1 cgd * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
60 1.1 cgd * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
61 1.1 cgd * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
62 1.1 cgd * SUCH DAMAGE.
63 1.1 cgd *
64 1.44 thorpej * @(#)udp_usrreq.c 8.6 (Berkeley) 5/23/95
65 1.1 cgd */
66 1.38 ws #include "ipkdb.h"
67 1.1 cgd
68 1.48 itojun /* XXX MAPPED_ADDR_ENABLED should be revisited */
69 1.48 itojun
70 1.5 mycroft #include <sys/param.h>
71 1.5 mycroft #include <sys/malloc.h>
72 1.5 mycroft #include <sys/mbuf.h>
73 1.48 itojun #ifdef MAPPED_ADDR_ENABLED
74 1.48 itojun #include <sys/domain.h>
75 1.48 itojun #endif /* MAPPED_ADDR_ENABLED */
76 1.5 mycroft #include <sys/protosw.h>
77 1.5 mycroft #include <sys/socket.h>
78 1.5 mycroft #include <sys/socketvar.h>
79 1.13 mycroft #include <sys/errno.h>
80 1.5 mycroft #include <sys/stat.h>
81 1.27 christos #include <sys/systm.h>
82 1.27 christos #include <sys/proc.h>
83 1.27 christos
84 1.27 christos #include <vm/vm.h>
85 1.27 christos #include <sys/sysctl.h>
86 1.1 cgd
87 1.5 mycroft #include <net/if.h>
88 1.5 mycroft #include <net/route.h>
89 1.1 cgd
90 1.5 mycroft #include <netinet/in.h>
91 1.5 mycroft #include <netinet/in_systm.h>
92 1.15 cgd #include <netinet/in_var.h>
93 1.5 mycroft #include <netinet/ip.h>
94 1.5 mycroft #include <netinet/in_pcb.h>
95 1.5 mycroft #include <netinet/ip_var.h>
96 1.5 mycroft #include <netinet/ip_icmp.h>
97 1.5 mycroft #include <netinet/udp.h>
98 1.5 mycroft #include <netinet/udp_var.h>
99 1.1 cgd
100 1.27 christos #include <machine/stdarg.h>
101 1.27 christos
102 1.48 itojun #ifdef IPSEC
103 1.48 itojun #include <netinet6/ipsec.h>
104 1.48 itojun #include <netkey/key.h>
105 1.48 itojun #include <netkey/key_debug.h>
106 1.48 itojun #endif /*IPSEC*/
107 1.48 itojun
108 1.8 mycroft /*
109 1.8 mycroft * UDP protocol implementation.
110 1.8 mycroft * Per RFC 768, August, 1980.
111 1.8 mycroft */
112 1.8 mycroft #ifndef COMPAT_42
113 1.8 mycroft int udpcksum = 1;
114 1.8 mycroft #else
115 1.8 mycroft int udpcksum = 0; /* XXX */
116 1.8 mycroft #endif
117 1.8 mycroft
118 1.13 mycroft static void udp_notify __P((struct inpcb *, int));
119 1.7 mycroft
120 1.26 mycroft #ifndef UDBHASHSIZE
121 1.26 mycroft #define UDBHASHSIZE 128
122 1.26 mycroft #endif
123 1.26 mycroft int udbhashsize = UDBHASHSIZE;
124 1.26 mycroft
125 1.7 mycroft void
126 1.1 cgd udp_init()
127 1.1 cgd {
128 1.18 mycroft
129 1.35 mycroft in_pcbinit(&udbtable, udbhashsize, udbhashsize);
130 1.1 cgd }
131 1.1 cgd
132 1.7 mycroft void
133 1.27 christos #if __STDC__
134 1.27 christos udp_input(struct mbuf *m, ...)
135 1.27 christos #else
136 1.27 christos udp_input(m, va_alist)
137 1.27 christos struct mbuf *m;
138 1.27 christos va_dcl
139 1.27 christos #endif
140 1.1 cgd {
141 1.48 itojun int proto;
142 1.1 cgd register struct ip *ip;
143 1.1 cgd register struct udphdr *uh;
144 1.1 cgd register struct inpcb *inp;
145 1.1 cgd struct mbuf *opts = 0;
146 1.1 cgd int len;
147 1.1 cgd struct ip save_ip;
148 1.27 christos int iphlen;
149 1.27 christos va_list ap;
150 1.34 mycroft struct sockaddr_in udpsrc;
151 1.48 itojun #ifdef MAPPED_ADDR_ENABLED
152 1.48 itojun struct sockaddr_in6 mapped;
153 1.48 itojun #endif
154 1.48 itojun struct sockaddr *sa;
155 1.27 christos
156 1.27 christos va_start(ap, m);
157 1.27 christos iphlen = va_arg(ap, int);
158 1.48 itojun proto = va_arg(ap, int);
159 1.27 christos va_end(ap);
160 1.1 cgd
161 1.1 cgd udpstat.udps_ipackets++;
162 1.1 cgd
163 1.1 cgd /*
164 1.1 cgd * Strip IP options, if any; should skip this,
165 1.1 cgd * make available to user, and use on returned packets,
166 1.1 cgd * but we don't yet have a way to check the checksum
167 1.1 cgd * with options still present.
168 1.1 cgd */
169 1.1 cgd if (iphlen > sizeof (struct ip)) {
170 1.1 cgd ip_stripoptions(m, (struct mbuf *)0);
171 1.1 cgd iphlen = sizeof(struct ip);
172 1.1 cgd }
173 1.1 cgd
174 1.1 cgd /*
175 1.1 cgd * Get IP and UDP header together in first mbuf.
176 1.1 cgd */
177 1.1 cgd ip = mtod(m, struct ip *);
178 1.1 cgd if (m->m_len < iphlen + sizeof(struct udphdr)) {
179 1.1 cgd if ((m = m_pullup(m, iphlen + sizeof(struct udphdr))) == 0) {
180 1.1 cgd udpstat.udps_hdrops++;
181 1.1 cgd return;
182 1.1 cgd }
183 1.1 cgd ip = mtod(m, struct ip *);
184 1.1 cgd }
185 1.1 cgd uh = (struct udphdr *)((caddr_t)ip + iphlen);
186 1.1 cgd
187 1.1 cgd /*
188 1.1 cgd * Make mbuf data length reflect UDP length.
189 1.1 cgd * If not enough data to reflect UDP length, drop.
190 1.1 cgd */
191 1.15 cgd len = ntohs((u_int16_t)uh->uh_ulen);
192 1.47 mycroft if (ip->ip_len != iphlen + len) {
193 1.47 mycroft if (ip->ip_len < iphlen + len) {
194 1.1 cgd udpstat.udps_badlen++;
195 1.1 cgd goto bad;
196 1.1 cgd }
197 1.47 mycroft m_adj(m, iphlen + len - ip->ip_len);
198 1.1 cgd }
199 1.1 cgd /*
200 1.1 cgd * Save a copy of the IP header in case we want restore it
201 1.1 cgd * for sending an ICMP error message in response.
202 1.1 cgd */
203 1.1 cgd save_ip = *ip;
204 1.1 cgd
205 1.1 cgd /*
206 1.1 cgd * Checksum extended UDP header and data.
207 1.1 cgd */
208 1.29 mrg if (uh->uh_sum) {
209 1.25 cgd bzero(((struct ipovly *)ip)->ih_x1,
210 1.25 cgd sizeof ((struct ipovly *)ip)->ih_x1);
211 1.1 cgd ((struct ipovly *)ip)->ih_len = uh->uh_ulen;
212 1.46 mycroft if (in_cksum(m, len + sizeof (struct ip)) != 0) {
213 1.1 cgd udpstat.udps_badsum++;
214 1.1 cgd m_freem(m);
215 1.1 cgd return;
216 1.1 cgd }
217 1.1 cgd }
218 1.13 mycroft
219 1.48 itojun /*
220 1.48 itojun * Construct sockaddr format source address.
221 1.48 itojun */
222 1.48 itojun udpsrc.sin_family = AF_INET;
223 1.48 itojun udpsrc.sin_len = sizeof(struct sockaddr_in);
224 1.48 itojun udpsrc.sin_addr = ip->ip_src;
225 1.48 itojun udpsrc.sin_port = uh->uh_sport;
226 1.48 itojun bzero((caddr_t)udpsrc.sin_zero, sizeof(udpsrc.sin_zero));
227 1.48 itojun
228 1.16 mycroft if (IN_MULTICAST(ip->ip_dst.s_addr) ||
229 1.13 mycroft in_broadcast(ip->ip_dst, m->m_pkthdr.rcvif)) {
230 1.40 thorpej struct inpcb *last;
231 1.4 hpeyerl /*
232 1.4 hpeyerl * Deliver a multicast or broadcast datagram to *all* sockets
233 1.4 hpeyerl * for which the local and remote addresses and ports match
234 1.4 hpeyerl * those of the incoming datagram. This allows more than
235 1.4 hpeyerl * one process to receive multi/broadcasts on the same port.
236 1.4 hpeyerl * (This really ought to be done for unicast datagrams as
237 1.4 hpeyerl * well, but that would cause problems with existing
238 1.4 hpeyerl * applications that open both address-specific sockets and
239 1.4 hpeyerl * a wildcard socket listening to the same port -- they would
240 1.4 hpeyerl * end up receiving duplicates of every unicast datagram.
241 1.4 hpeyerl * Those applications open the multiple sockets to overcome an
242 1.4 hpeyerl * inadequacy of the UDP socket interface, but for backwards
243 1.4 hpeyerl * compatibility we avoid the problem here rather than
244 1.13 mycroft * fixing the interface. Maybe 4.5BSD will remedy this?)
245 1.4 hpeyerl */
246 1.13 mycroft
247 1.48 itojun iphlen += sizeof(struct udphdr);
248 1.4 hpeyerl /*
249 1.48 itojun * KAME note: usually we drop udpiphdr from mbuf here.
250 1.48 itojun * we need udpiphdr for iPsec processing so we do that later.
251 1.4 hpeyerl */
252 1.4 hpeyerl /*
253 1.4 hpeyerl * Locate pcb(s) for datagram.
254 1.4 hpeyerl * (Algorithm copied from raw_intr().)
255 1.4 hpeyerl */
256 1.4 hpeyerl last = NULL;
257 1.22 cgd for (inp = udbtable.inpt_queue.cqh_first;
258 1.22 cgd inp != (struct inpcb *)&udbtable.inpt_queue;
259 1.22 cgd inp = inp->inp_queue.cqe_next) {
260 1.4 hpeyerl if (inp->inp_lport != uh->uh_dport)
261 1.4 hpeyerl continue;
262 1.34 mycroft if (!in_nullhost(inp->inp_laddr)) {
263 1.34 mycroft if (!in_hosteq(inp->inp_laddr, ip->ip_dst))
264 1.4 hpeyerl continue;
265 1.4 hpeyerl }
266 1.34 mycroft if (!in_nullhost(inp->inp_faddr)) {
267 1.34 mycroft if (!in_hosteq(inp->inp_faddr, ip->ip_src) ||
268 1.4 hpeyerl inp->inp_fport != uh->uh_sport)
269 1.4 hpeyerl continue;
270 1.4 hpeyerl }
271 1.4 hpeyerl
272 1.4 hpeyerl if (last != NULL) {
273 1.4 hpeyerl struct mbuf *n;
274 1.4 hpeyerl
275 1.48 itojun #ifdef IPSEC
276 1.48 itojun /* check AH/ESP integrity. */
277 1.48 itojun if (last != NULL && ipsec4_in_reject(m, last)) {
278 1.48 itojun ipsecstat.in_polvio++;
279 1.48 itojun /* do not inject data to pcb */
280 1.48 itojun } else
281 1.48 itojun #endif /*IPSEC*/
282 1.4 hpeyerl if ((n = m_copy(m, 0, M_COPYALL)) != NULL) {
283 1.40 thorpej if (last->inp_flags & INP_CONTROLOPTS
284 1.40 thorpej || last->inp_socket->so_options &
285 1.40 thorpej SO_TIMESTAMP) {
286 1.40 thorpej ip_savecontrol(last, &opts,
287 1.40 thorpej ip, n);
288 1.40 thorpej }
289 1.49 drochner m_adj(n, iphlen);
290 1.48 itojun sa = (struct sockaddr *)&udpsrc;
291 1.48 itojun #ifdef MAPPED_ADDR_ENABLED
292 1.49 drochner if (last->inp_socket->so_proto->
293 1.49 drochner pr_domain->dom_family == AF_INET6) {
294 1.48 itojun in6_sin_2_v4mapsin6(&udpsrc,
295 1.48 itojun &mapped);
296 1.48 itojun sa = (struct sockaddr *)&mapped;
297 1.48 itojun }
298 1.48 itojun #endif /* MAPPED_ADDR_ENABLED */
299 1.40 thorpej if (sbappendaddr(
300 1.40 thorpej &last->inp_socket->so_rcv,
301 1.48 itojun sa, n, opts) == 0) {
302 1.4 hpeyerl m_freem(n);
303 1.40 thorpej if (opts)
304 1.40 thorpej m_freem(opts);
305 1.13 mycroft } else
306 1.40 thorpej sorwakeup(last->inp_socket);
307 1.40 thorpej opts = 0;
308 1.4 hpeyerl }
309 1.4 hpeyerl }
310 1.40 thorpej last = inp;
311 1.4 hpeyerl /*
312 1.13 mycroft * Don't look for additional matches if this one does
313 1.13 mycroft * not have either the SO_REUSEPORT or SO_REUSEADDR
314 1.13 mycroft * socket options set. This heuristic avoids searching
315 1.13 mycroft * through all pcbs in the common case of a non-shared
316 1.13 mycroft * port. It * assumes that an application will never
317 1.13 mycroft * clear these options after setting them.
318 1.4 hpeyerl */
319 1.40 thorpej if ((last->inp_socket->so_options &
320 1.40 thorpej (SO_REUSEPORT|SO_REUSEADDR)) == 0)
321 1.4 hpeyerl break;
322 1.4 hpeyerl }
323 1.6 mycroft
324 1.4 hpeyerl if (last == NULL) {
325 1.4 hpeyerl /*
326 1.4 hpeyerl * No matching pcb found; discard datagram.
327 1.4 hpeyerl * (No need to send an ICMP Port Unreachable
328 1.4 hpeyerl * for a broadcast or multicast datgram.)
329 1.4 hpeyerl */
330 1.48 itojun udpstat.udps_noport++;
331 1.13 mycroft udpstat.udps_noportbcast++;
332 1.4 hpeyerl goto bad;
333 1.4 hpeyerl }
334 1.48 itojun #ifdef IPSEC
335 1.48 itojun /* check AH/ESP integrity. */
336 1.48 itojun if (last != NULL && ipsec4_in_reject(m, last)) {
337 1.48 itojun ipsecstat.in_polvio++;
338 1.48 itojun goto bad;
339 1.48 itojun }
340 1.48 itojun #endif /*IPSEC*/
341 1.40 thorpej if (last->inp_flags & INP_CONTROLOPTS ||
342 1.40 thorpej last->inp_socket->so_options & SO_TIMESTAMP)
343 1.40 thorpej ip_savecontrol(last, &opts, ip, m);
344 1.49 drochner m->m_len -= iphlen;
345 1.49 drochner m->m_pkthdr.len -= iphlen;
346 1.49 drochner m->m_data += iphlen;
347 1.48 itojun sa = (struct sockaddr *)&udpsrc;
348 1.48 itojun #ifdef MAPPED_ADDR_ENABLED
349 1.48 itojun if (last->inp_socket->so_proto->pr_domain->dom_family ==
350 1.48 itojun AF_INET6) {
351 1.48 itojun in6_sin_2_v4mapsin6(&udpsrc, &mapped);
352 1.48 itojun sa = (struct sockaddr *)&mapped;
353 1.48 itojun }
354 1.48 itojun #endif /* MAPPED_ADDR_ENABLED */
355 1.48 itojun if (sbappendaddr(&last->inp_socket->so_rcv, sa, m, opts) == 0) {
356 1.13 mycroft udpstat.udps_fullsock++;
357 1.4 hpeyerl goto bad;
358 1.13 mycroft }
359 1.40 thorpej sorwakeup(last->inp_socket);
360 1.4 hpeyerl return;
361 1.4 hpeyerl }
362 1.1 cgd /*
363 1.1 cgd * Locate pcb for datagram.
364 1.1 cgd */
365 1.35 mycroft inp = in_pcblookup_connect(&udbtable, ip->ip_src, uh->uh_sport,
366 1.26 mycroft ip->ip_dst, uh->uh_dport);
367 1.26 mycroft if (inp == 0) {
368 1.26 mycroft ++udpstat.udps_pcbhashmiss;
369 1.35 mycroft inp = in_pcblookup_bind(&udbtable, ip->ip_dst, uh->uh_dport);
370 1.18 mycroft if (inp == 0) {
371 1.18 mycroft udpstat.udps_noport++;
372 1.18 mycroft if (m->m_flags & (M_BCAST | M_MCAST)) {
373 1.18 mycroft udpstat.udps_noportbcast++;
374 1.18 mycroft goto bad;
375 1.18 mycroft }
376 1.18 mycroft *ip = save_ip;
377 1.38 ws #if NIPKDB > 0
378 1.38 ws if (checkipkdb(&ip->ip_src,
379 1.38 ws uh->uh_sport,
380 1.38 ws uh->uh_dport,
381 1.38 ws m,
382 1.38 ws iphlen + sizeof(struct udphdr),
383 1.38 ws len - sizeof(struct udphdr)))
384 1.37 ws /* It was a debugger connect packet, just drop it now */
385 1.37 ws goto bad;
386 1.37 ws #endif
387 1.18 mycroft icmp_error(m, ICMP_UNREACH, ICMP_UNREACH_PORT, 0, 0);
388 1.18 mycroft return;
389 1.13 mycroft }
390 1.1 cgd }
391 1.48 itojun #ifdef IPSEC
392 1.48 itojun if (inp != NULL && ipsec4_in_reject(m, inp)) {
393 1.48 itojun ipsecstat.in_polvio++;
394 1.48 itojun goto bad;
395 1.48 itojun }
396 1.48 itojun #endif /*IPSEC*/
397 1.1 cgd
398 1.1 cgd /*
399 1.1 cgd * Stuff source address and datagram in user buffer.
400 1.1 cgd */
401 1.40 thorpej if (inp->inp_flags & INP_CONTROLOPTS ||
402 1.40 thorpej inp->inp_socket->so_options & SO_TIMESTAMP)
403 1.40 thorpej ip_savecontrol(inp, &opts, ip, m);
404 1.1 cgd iphlen += sizeof(struct udphdr);
405 1.1 cgd m->m_len -= iphlen;
406 1.1 cgd m->m_pkthdr.len -= iphlen;
407 1.1 cgd m->m_data += iphlen;
408 1.48 itojun sa = (struct sockaddr *)&udpsrc;
409 1.48 itojun #ifdef MAPPED_ADDR_ENABLED
410 1.48 itojun if (inp->inp_socket->so_proto->pr_domain->dom_family == AF_INET6) {
411 1.48 itojun in6_sin_2_v4mapsin6(&udpsrc, &mapped);
412 1.48 itojun sa = (struct sockaddr *)&mapped;
413 1.48 itojun }
414 1.48 itojun #endif /* MAPPED_ADDR_ENABLED */
415 1.48 itojun if (sbappendaddr(&inp->inp_socket->so_rcv, sa, m, opts) == 0) {
416 1.1 cgd udpstat.udps_fullsock++;
417 1.1 cgd goto bad;
418 1.1 cgd }
419 1.1 cgd sorwakeup(inp->inp_socket);
420 1.1 cgd return;
421 1.1 cgd bad:
422 1.1 cgd m_freem(m);
423 1.1 cgd if (opts)
424 1.1 cgd m_freem(opts);
425 1.1 cgd }
426 1.1 cgd
427 1.1 cgd /*
428 1.1 cgd * Notify a udp user of an asynchronous error;
429 1.1 cgd * just wake up so that he can collect error status.
430 1.1 cgd */
431 1.7 mycroft static void
432 1.1 cgd udp_notify(inp, errno)
433 1.1 cgd register struct inpcb *inp;
434 1.7 mycroft int errno;
435 1.1 cgd {
436 1.34 mycroft
437 1.1 cgd inp->inp_socket->so_error = errno;
438 1.1 cgd sorwakeup(inp->inp_socket);
439 1.1 cgd sowwakeup(inp->inp_socket);
440 1.1 cgd }
441 1.1 cgd
442 1.27 christos void *
443 1.27 christos udp_ctlinput(cmd, sa, v)
444 1.1 cgd int cmd;
445 1.1 cgd struct sockaddr *sa;
446 1.27 christos void *v;
447 1.1 cgd {
448 1.27 christos register struct ip *ip = v;
449 1.1 cgd register struct udphdr *uh;
450 1.21 mycroft extern int inetctlerrmap[];
451 1.18 mycroft void (*notify) __P((struct inpcb *, int)) = udp_notify;
452 1.21 mycroft int errno;
453 1.1 cgd
454 1.20 mycroft if ((unsigned)cmd >= PRC_NCMDS)
455 1.27 christos return NULL;
456 1.20 mycroft errno = inetctlerrmap[cmd];
457 1.18 mycroft if (PRC_IS_REDIRECT(cmd))
458 1.19 mycroft notify = in_rtchange, ip = 0;
459 1.18 mycroft else if (cmd == PRC_HOSTDEAD)
460 1.19 mycroft ip = 0;
461 1.23 cgd else if (errno == 0)
462 1.27 christos return NULL;
463 1.19 mycroft if (ip) {
464 1.1 cgd uh = (struct udphdr *)((caddr_t)ip + (ip->ip_hl << 2));
465 1.34 mycroft in_pcbnotify(&udbtable, satosin(sa)->sin_addr, uh->uh_dport,
466 1.34 mycroft ip->ip_src, uh->uh_sport, errno, notify);
467 1.19 mycroft } else
468 1.34 mycroft in_pcbnotifyall(&udbtable, satosin(sa)->sin_addr, errno,
469 1.34 mycroft notify);
470 1.27 christos return NULL;
471 1.1 cgd }
472 1.1 cgd
473 1.7 mycroft int
474 1.27 christos #if __STDC__
475 1.27 christos udp_output(struct mbuf *m, ...)
476 1.27 christos #else
477 1.27 christos udp_output(m, va_alist)
478 1.27 christos struct mbuf *m;
479 1.27 christos va_dcl
480 1.27 christos #endif
481 1.27 christos {
482 1.1 cgd register struct inpcb *inp;
483 1.1 cgd register struct udpiphdr *ui;
484 1.1 cgd register int len = m->m_pkthdr.len;
485 1.31 mycroft int error = 0;
486 1.27 christos va_list ap;
487 1.27 christos
488 1.27 christos va_start(ap, m);
489 1.27 christos inp = va_arg(ap, struct inpcb *);
490 1.27 christos va_end(ap);
491 1.1 cgd
492 1.1 cgd /*
493 1.1 cgd * Calculate data length and get a mbuf
494 1.1 cgd * for UDP and IP headers.
495 1.1 cgd */
496 1.13 mycroft M_PREPEND(m, sizeof(struct udpiphdr), M_DONTWAIT);
497 1.13 mycroft if (m == 0) {
498 1.13 mycroft error = ENOBUFS;
499 1.39 thorpej goto release;
500 1.39 thorpej }
501 1.39 thorpej
502 1.39 thorpej /*
503 1.39 thorpej * Compute the packet length of the IP header, and
504 1.39 thorpej * punt if the length looks bogus.
505 1.39 thorpej */
506 1.39 thorpej if ((len + sizeof(struct udpiphdr)) > IP_MAXPACKET) {
507 1.39 thorpej error = EMSGSIZE;
508 1.13 mycroft goto release;
509 1.13 mycroft }
510 1.1 cgd
511 1.1 cgd /*
512 1.1 cgd * Fill in mbuf with extended UDP header
513 1.1 cgd * and addresses and length put into network format.
514 1.1 cgd */
515 1.1 cgd ui = mtod(m, struct udpiphdr *);
516 1.25 cgd bzero(ui->ui_x1, sizeof ui->ui_x1);
517 1.1 cgd ui->ui_pr = IPPROTO_UDP;
518 1.15 cgd ui->ui_len = htons((u_int16_t)len + sizeof (struct udphdr));
519 1.1 cgd ui->ui_src = inp->inp_laddr;
520 1.1 cgd ui->ui_dst = inp->inp_faddr;
521 1.1 cgd ui->ui_sport = inp->inp_lport;
522 1.1 cgd ui->ui_dport = inp->inp_fport;
523 1.1 cgd ui->ui_ulen = ui->ui_len;
524 1.1 cgd
525 1.1 cgd /*
526 1.1 cgd * Stuff checksum and output datagram.
527 1.1 cgd */
528 1.1 cgd ui->ui_sum = 0;
529 1.1 cgd if (udpcksum) {
530 1.1 cgd if ((ui->ui_sum = in_cksum(m, sizeof (struct udpiphdr) + len)) == 0)
531 1.1 cgd ui->ui_sum = 0xffff;
532 1.1 cgd }
533 1.1 cgd ((struct ip *)ui)->ip_len = sizeof (struct udpiphdr) + len;
534 1.1 cgd ((struct ip *)ui)->ip_ttl = inp->inp_ip.ip_ttl; /* XXX */
535 1.1 cgd ((struct ip *)ui)->ip_tos = inp->inp_ip.ip_tos; /* XXX */
536 1.1 cgd udpstat.udps_opackets++;
537 1.48 itojun
538 1.48 itojun #ifdef IPSEC
539 1.48 itojun m->m_pkthdr.rcvif = (struct ifnet *)inp->inp_socket;
540 1.48 itojun #endif /*IPSEC*/
541 1.48 itojun
542 1.31 mycroft return (ip_output(m, inp->inp_options, &inp->inp_route,
543 1.12 mycroft inp->inp_socket->so_options & (SO_DONTROUTE | SO_BROADCAST),
544 1.31 mycroft inp->inp_moptions));
545 1.1 cgd
546 1.1 cgd release:
547 1.1 cgd m_freem(m);
548 1.1 cgd return (error);
549 1.1 cgd }
550 1.1 cgd
551 1.42 thorpej int udp_sendspace = 9216; /* really max datagram size */
552 1.42 thorpej int udp_recvspace = 40 * (1024 + sizeof(struct sockaddr_in));
553 1.1 cgd /* 40 1K datagrams */
554 1.1 cgd
555 1.1 cgd /*ARGSUSED*/
556 1.7 mycroft int
557 1.31 mycroft udp_usrreq(so, req, m, nam, control, p)
558 1.1 cgd struct socket *so;
559 1.1 cgd int req;
560 1.31 mycroft struct mbuf *m, *nam, *control;
561 1.30 mycroft struct proc *p;
562 1.1 cgd {
563 1.31 mycroft register struct inpcb *inp;
564 1.1 cgd int s;
565 1.31 mycroft register int error = 0;
566 1.1 cgd
567 1.1 cgd if (req == PRU_CONTROL)
568 1.31 mycroft return (in_control(so, (long)m, (caddr_t)nam,
569 1.30 mycroft (struct ifnet *)control, p));
570 1.31 mycroft
571 1.31 mycroft s = splsoftnet();
572 1.31 mycroft inp = sotoinpcb(so);
573 1.32 mycroft #ifdef DIAGNOSTIC
574 1.32 mycroft if (req != PRU_SEND && req != PRU_SENDOOB && control)
575 1.32 mycroft panic("udp_usrreq: unexpected control mbuf");
576 1.32 mycroft #endif
577 1.31 mycroft if (inp == 0 && req != PRU_ATTACH) {
578 1.31 mycroft error = EINVAL;
579 1.31 mycroft goto release;
580 1.31 mycroft }
581 1.31 mycroft
582 1.1 cgd /*
583 1.1 cgd * Note: need to block udp_input while changing
584 1.1 cgd * the udp pcb queue and/or pcb addresses.
585 1.1 cgd */
586 1.1 cgd switch (req) {
587 1.1 cgd
588 1.1 cgd case PRU_ATTACH:
589 1.31 mycroft if (inp != 0) {
590 1.31 mycroft error = EISCONN;
591 1.1 cgd break;
592 1.1 cgd }
593 1.31 mycroft if (so->so_snd.sb_hiwat == 0 || so->so_rcv.sb_hiwat == 0) {
594 1.31 mycroft error = soreserve(so, udp_sendspace, udp_recvspace);
595 1.31 mycroft if (error)
596 1.31 mycroft break;
597 1.31 mycroft }
598 1.18 mycroft error = in_pcballoc(so, &udbtable);
599 1.1 cgd if (error)
600 1.1 cgd break;
601 1.31 mycroft inp = sotoinpcb(so);
602 1.31 mycroft inp->inp_ip.ip_ttl = ip_defttl;
603 1.48 itojun #ifdef IPSEC
604 1.48 itojun inp = (struct inpcb *)so->so_pcb;
605 1.48 itojun error = ipsec_init_policy(&inp->inp_sp);
606 1.48 itojun #endif /*IPSEC*/
607 1.1 cgd break;
608 1.1 cgd
609 1.1 cgd case PRU_DETACH:
610 1.31 mycroft in_pcbdetach(inp);
611 1.1 cgd break;
612 1.1 cgd
613 1.1 cgd case PRU_BIND:
614 1.31 mycroft error = in_pcbbind(inp, nam, p);
615 1.1 cgd break;
616 1.1 cgd
617 1.1 cgd case PRU_LISTEN:
618 1.1 cgd error = EOPNOTSUPP;
619 1.1 cgd break;
620 1.1 cgd
621 1.1 cgd case PRU_CONNECT:
622 1.31 mycroft error = in_pcbconnect(inp, nam);
623 1.31 mycroft if (error)
624 1.1 cgd break;
625 1.31 mycroft soisconnected(so);
626 1.1 cgd break;
627 1.1 cgd
628 1.1 cgd case PRU_CONNECT2:
629 1.1 cgd error = EOPNOTSUPP;
630 1.1 cgd break;
631 1.1 cgd
632 1.1 cgd case PRU_DISCONNECT:
633 1.31 mycroft /*soisdisconnected(so);*/
634 1.31 mycroft so->so_state &= ~SS_ISCONNECTED; /* XXX */
635 1.1 cgd in_pcbdisconnect(inp);
636 1.34 mycroft inp->inp_laddr = zeroin_addr; /* XXX */
637 1.35 mycroft in_pcbstate(inp, INP_BOUND); /* XXX */
638 1.1 cgd break;
639 1.1 cgd
640 1.1 cgd case PRU_SHUTDOWN:
641 1.1 cgd socantsendmore(so);
642 1.1 cgd break;
643 1.1 cgd
644 1.31 mycroft case PRU_RCVD:
645 1.31 mycroft error = EOPNOTSUPP;
646 1.1 cgd break;
647 1.1 cgd
648 1.31 mycroft case PRU_SEND:
649 1.32 mycroft if (control && control->m_len) {
650 1.32 mycroft m_freem(control);
651 1.32 mycroft m_freem(m);
652 1.32 mycroft error = EINVAL;
653 1.32 mycroft break;
654 1.32 mycroft }
655 1.31 mycroft {
656 1.35 mycroft struct in_addr laddr; /* XXX */
657 1.1 cgd
658 1.31 mycroft if (nam) {
659 1.35 mycroft laddr = inp->inp_laddr; /* XXX */
660 1.31 mycroft if ((so->so_state & SS_ISCONNECTED) != 0) {
661 1.31 mycroft error = EISCONN;
662 1.32 mycroft goto die;
663 1.31 mycroft }
664 1.31 mycroft error = in_pcbconnect(inp, nam);
665 1.32 mycroft if (error) {
666 1.32 mycroft die:
667 1.32 mycroft m_freem(m);
668 1.31 mycroft break;
669 1.32 mycroft }
670 1.31 mycroft } else {
671 1.31 mycroft if ((so->so_state & SS_ISCONNECTED) == 0) {
672 1.31 mycroft error = ENOTCONN;
673 1.32 mycroft goto die;
674 1.31 mycroft }
675 1.31 mycroft }
676 1.33 mycroft error = udp_output(m, inp);
677 1.31 mycroft if (nam) {
678 1.31 mycroft in_pcbdisconnect(inp);
679 1.35 mycroft inp->inp_laddr = laddr; /* XXX */
680 1.35 mycroft in_pcbstate(inp, INP_BOUND); /* XXX */
681 1.31 mycroft }
682 1.31 mycroft }
683 1.1 cgd break;
684 1.1 cgd
685 1.1 cgd case PRU_SENSE:
686 1.1 cgd /*
687 1.1 cgd * stat: don't bother with a blocksize.
688 1.1 cgd */
689 1.31 mycroft splx(s);
690 1.1 cgd return (0);
691 1.1 cgd
692 1.31 mycroft case PRU_RCVOOB:
693 1.31 mycroft error = EOPNOTSUPP;
694 1.31 mycroft break;
695 1.31 mycroft
696 1.1 cgd case PRU_SENDOOB:
697 1.32 mycroft m_freem(control);
698 1.31 mycroft m_freem(m);
699 1.1 cgd error = EOPNOTSUPP;
700 1.1 cgd break;
701 1.1 cgd
702 1.31 mycroft case PRU_SOCKADDR:
703 1.31 mycroft in_setsockaddr(inp, nam);
704 1.31 mycroft break;
705 1.31 mycroft
706 1.31 mycroft case PRU_PEERADDR:
707 1.31 mycroft in_setpeeraddr(inp, nam);
708 1.31 mycroft break;
709 1.1 cgd
710 1.1 cgd default:
711 1.1 cgd panic("udp_usrreq");
712 1.1 cgd }
713 1.1 cgd
714 1.1 cgd release:
715 1.31 mycroft splx(s);
716 1.1 cgd return (error);
717 1.13 mycroft }
718 1.13 mycroft
719 1.13 mycroft /*
720 1.13 mycroft * Sysctl for udp variables.
721 1.13 mycroft */
722 1.27 christos int
723 1.13 mycroft udp_sysctl(name, namelen, oldp, oldlenp, newp, newlen)
724 1.13 mycroft int *name;
725 1.13 mycroft u_int namelen;
726 1.13 mycroft void *oldp;
727 1.13 mycroft size_t *oldlenp;
728 1.13 mycroft void *newp;
729 1.13 mycroft size_t newlen;
730 1.13 mycroft {
731 1.13 mycroft /* All sysctl names at this level are terminal. */
732 1.13 mycroft if (namelen != 1)
733 1.13 mycroft return (ENOTDIR);
734 1.13 mycroft
735 1.13 mycroft switch (name[0]) {
736 1.13 mycroft case UDPCTL_CHECKSUM:
737 1.13 mycroft return (sysctl_int(oldp, oldlenp, newp, newlen, &udpcksum));
738 1.42 thorpej case UDPCTL_SENDSPACE:
739 1.42 thorpej return (sysctl_int(oldp, oldlenp, newp, newlen,
740 1.42 thorpej &udp_sendspace));
741 1.42 thorpej case UDPCTL_RECVSPACE:
742 1.42 thorpej return (sysctl_int(oldp, oldlenp, newp, newlen,
743 1.42 thorpej &udp_recvspace));
744 1.13 mycroft default:
745 1.13 mycroft return (ENOPROTOOPT);
746 1.13 mycroft }
747 1.13 mycroft /* NOTREACHED */
748 1.1 cgd }
749