]> git.saurik.com Git - apple/xnu.git/blame - bsd/netinet/udp_usrreq.c
xnu-344.21.74.tar.gz
[apple/xnu.git] / bsd / netinet / udp_usrreq.c
CommitLineData
1c79356b
A
1/*
2 * Copyright (c) 2000 Apple Computer, Inc. All rights reserved.
3 *
4 * @APPLE_LICENSE_HEADER_START@
5 *
d7e50217 6 * Copyright (c) 1999-2003 Apple Computer, Inc. All Rights Reserved.
1c79356b 7 *
d7e50217
A
8 * This file contains Original Code and/or Modifications of Original Code
9 * as defined in and that are subject to the Apple Public Source License
10 * Version 2.0 (the 'License'). You may not use this file except in
11 * compliance with the License. Please obtain a copy of the License at
12 * http://www.opensource.apple.com/apsl/ and read it before using this
13 * file.
14 *
15 * The Original Code and all software distributed under the License are
16 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
1c79356b
A
17 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
18 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
d7e50217
A
19 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
20 * Please see the License for the specific language governing rights and
21 * limitations under the License.
1c79356b
A
22 *
23 * @APPLE_LICENSE_HEADER_END@
24 */
25/*
26 * Copyright (c) 1982, 1986, 1988, 1990, 1993, 1995
27 * The Regents of the University of California. All rights reserved.
28 *
29 * Redistribution and use in source and binary forms, with or without
30 * modification, are permitted provided that the following conditions
31 * are met:
32 * 1. Redistributions of source code must retain the above copyright
33 * notice, this list of conditions and the following disclaimer.
34 * 2. Redistributions in binary form must reproduce the above copyright
35 * notice, this list of conditions and the following disclaimer in the
36 * documentation and/or other materials provided with the distribution.
37 * 3. All advertising materials mentioning features or use of this software
38 * must display the following acknowledgement:
39 * This product includes software developed by the University of
40 * California, Berkeley and its contributors.
41 * 4. Neither the name of the University nor the names of its contributors
42 * may be used to endorse or promote products derived from this software
43 * without specific prior written permission.
44 *
45 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
46 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
47 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
48 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
49 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
50 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
51 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
52 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
53 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
54 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
55 * SUCH DAMAGE.
56 *
57 * @(#)udp_usrreq.c 8.6 (Berkeley) 5/23/95
9bccf70c 58 * $FreeBSD: src/sys/netinet/udp_usrreq.c,v 1.64.2.13 2001/08/08 18:59:54 ghelmer Exp $
1c79356b
A
59 */
60
61#include <sys/param.h>
62#include <sys/systm.h>
63#include <sys/kernel.h>
64#include <sys/malloc.h>
65#include <sys/mbuf.h>
1c79356b 66#include <sys/domain.h>
1c79356b
A
67#include <sys/protosw.h>
68#include <sys/socket.h>
69#include <sys/socketvar.h>
70#include <sys/sysctl.h>
71#include <sys/syslog.h>
72
1c79356b
A
73#include <net/if.h>
74#include <net/route.h>
75
76#include <netinet/in.h>
77#include <netinet/in_systm.h>
78#include <netinet/ip.h>
9bccf70c
A
79#if INET6
80#include <netinet/ip6.h>
81#endif
1c79356b
A
82#include <netinet/in_pcb.h>
83#include <netinet/in_var.h>
84#include <netinet/ip_var.h>
1c79356b 85#if INET6
1c79356b
A
86#include <netinet6/ip6_var.h>
87#endif
9bccf70c
A
88#include <netinet/ip_icmp.h>
89#include <netinet/icmp_var.h>
1c79356b
A
90#include <netinet/udp.h>
91#include <netinet/udp_var.h>
92#include <sys/kdebug.h>
93
94#if IPSEC
95#include <netinet6/ipsec.h>
9bccf70c 96extern int ipsec_bypass;
1c79356b
A
97#endif /*IPSEC*/
98
99
100#define DBG_LAYER_IN_BEG NETDBG_CODE(DBG_NETUDP, 0)
101#define DBG_LAYER_IN_END NETDBG_CODE(DBG_NETUDP, 2)
102#define DBG_LAYER_OUT_BEG NETDBG_CODE(DBG_NETUDP, 1)
103#define DBG_LAYER_OUT_END NETDBG_CODE(DBG_NETUDP, 3)
104#define DBG_FNC_UDP_INPUT NETDBG_CODE(DBG_NETUDP, (5 << 8))
105#define DBG_FNC_UDP_OUTPUT NETDBG_CODE(DBG_NETUDP, (6 << 8) | 1)
106
0b4e3aa0 107
1c79356b
A
108#define __STDC__ 1
109/*
110 * UDP protocol implementation.
111 * Per RFC 768, August, 1980.
112 */
113#ifndef COMPAT_42
114static int udpcksum = 1;
115#else
116static int udpcksum = 0; /* XXX */
117#endif
118SYSCTL_INT(_net_inet_udp, UDPCTL_CHECKSUM, checksum, CTLFLAG_RW,
119 &udpcksum, 0, "");
120
9bccf70c 121int log_in_vain = 0;
1c79356b 122SYSCTL_INT(_net_inet_udp, OID_AUTO, log_in_vain, CTLFLAG_RW,
9bccf70c
A
123 &log_in_vain, 0, "Log all incoming UDP packets");
124
125static int blackhole = 0;
126SYSCTL_INT(_net_inet_udp, OID_AUTO, blackhole, CTLFLAG_RW,
127 &blackhole, 0, "Do not send port unreachables for refused connects");
1c79356b
A
128
129struct inpcbhead udb; /* from udp_var.h */
130#define udb6 udb /* for KAME src sync over BSD*'s */
131struct inpcbinfo udbinfo;
132
133#ifndef UDBHASHSIZE
134#define UDBHASHSIZE 16
135#endif
136
0b4e3aa0
A
137extern int apple_hwcksum_rx;
138
1c79356b
A
139struct udpstat udpstat; /* from udp_var.h */
140SYSCTL_STRUCT(_net_inet_udp, UDPCTL_STATS, stats, CTLFLAG_RD,
9bccf70c 141 &udpstat, udpstat, "UDP statistics (struct udpstat, netinet/udp_var.h)");
1c79356b
A
142
143static struct sockaddr_in udp_in = { sizeof(udp_in), AF_INET };
144#if INET6
145struct udp_in6 {
146 struct sockaddr_in6 uin6_sin;
147 u_char uin6_init_done : 1;
148} udp_in6 = {
149 { sizeof(udp_in6.uin6_sin), AF_INET6 },
150 0
151};
152struct udp_ip6 {
153 struct ip6_hdr uip6_ip6;
154 u_char uip6_init_done : 1;
155} udp_ip6;
156#endif /* INET6 */
157
158static void udp_append __P((struct inpcb *last, struct ip *ip,
159 struct mbuf *n, int off));
160#if INET6
161static void ip_2_ip6_hdr __P((struct ip6_hdr *ip6, struct ip *ip));
162#endif
163
164static int udp_detach __P((struct socket *so));
9bccf70c
A
165static int udp_output __P((struct inpcb *, struct mbuf *, struct sockaddr *,
166 struct mbuf *, struct proc *));
1c79356b
A
167
168void
169udp_init()
170{
171 vm_size_t str_size;
172 int stat;
173 u_char fake_owner;
174 struct in_addr laddr;
175 struct in_addr faddr;
176 u_short lport;
177
178 LIST_INIT(&udb);
179 udbinfo.listhead = &udb;
180 udbinfo.hashbase = hashinit(UDBHASHSIZE, M_PCB, &udbinfo.hashmask);
181 udbinfo.porthashbase = hashinit(UDBHASHSIZE, M_PCB,
182 &udbinfo.porthashmask);
9bccf70c 183#ifdef __APPLE__
1c79356b 184 str_size = (vm_size_t) sizeof(struct inpcb);
9bccf70c 185 udbinfo.ipi_zone = (void *) zinit(str_size, 80000*str_size, 8192, "udpcb");
1c79356b
A
186
187 udbinfo.last_pcb = 0;
188 in_pcb_nat_init(&udbinfo, AF_INET, IPPROTO_UDP, SOCK_DGRAM);
9bccf70c
A
189#else
190 udbinfo.ipi_zone = zinit("udpcb", sizeof(struct inpcb), maxsockets,
191 ZONE_INTERRUPT, 0);
192#endif
1c79356b
A
193
194#if 0
9bccf70c 195 /* for pcb sharing testing only */
1c79356b
A
196 stat = in_pcb_new_share_client(&udbinfo, &fake_owner);
197 kprintf("udp_init in_pcb_new_share_client - stat = %d\n", stat);
198
199 laddr.s_addr = 0x11646464;
200 faddr.s_addr = 0x11646465;
201
202 lport = 1500;
203 in_pcb_grab_port(&udbinfo, 0, laddr, &lport, faddr, 1600, 0, fake_owner);
204 kprintf("udp_init in_pcb_grab_port - stat = %d\n", stat);
205
206 stat = in_pcb_rem_share_client(&udbinfo, fake_owner);
207 kprintf("udp_init in_pcb_rem_share_client - stat = %d\n", stat);
208
209 stat = in_pcb_new_share_client(&udbinfo, &fake_owner);
210 kprintf("udp_init in_pcb_new_share_client(2) - stat = %d\n", stat);
211
212 laddr.s_addr = 0x11646464;
213 faddr.s_addr = 0x11646465;
214
215 lport = 1500;
216 stat = in_pcb_grab_port(&udbinfo, 0, laddr, &lport, faddr, 1600, 0, fake_owner);
217 kprintf("udp_init in_pcb_grab_port(2) - stat = %d\n", stat);
218#endif
219}
220
221void
222udp_input(m, iphlen)
223 register struct mbuf *m;
224 int iphlen;
225{
226 register struct ip *ip;
227 register struct udphdr *uh;
228 register struct inpcb *inp;
229 struct mbuf *opts = 0;
1c79356b
A
230 int len;
231 struct ip save_ip;
232 struct sockaddr *append_sa;
233
234 udpstat.udps_ipackets++;
1c79356b
A
235
236 KERNEL_DEBUG(DBG_FNC_UDP_INPUT | DBG_FUNC_START, 0,0,0,0,0);
9bccf70c
A
237 if (m->m_pkthdr.csum_flags & CSUM_TCP_SUM16)
238 m->m_pkthdr.csum_flags = 0; /* invalidate hwcksum for UDP */
1c79356b
A
239
240 /*
241 * Strip IP options, if any; should skip this,
242 * make available to user, and use on returned packets,
243 * but we don't yet have a way to check the checksum
244 * with options still present.
245 */
246 if (iphlen > sizeof (struct ip)) {
247 ip_stripoptions(m, (struct mbuf *)0);
248 iphlen = sizeof(struct ip);
249 }
250
251 /*
252 * Get IP and UDP header together in first mbuf.
253 */
254 ip = mtod(m, struct ip *);
255 if (m->m_len < iphlen + sizeof(struct udphdr)) {
256 if ((m = m_pullup(m, iphlen + sizeof(struct udphdr))) == 0) {
257 udpstat.udps_hdrops++;
258 KERNEL_DEBUG(DBG_FNC_UDP_INPUT | DBG_FUNC_END, 0,0,0,0,0);
259 return;
260 }
261 ip = mtod(m, struct ip *);
262 }
263 uh = (struct udphdr *)((caddr_t)ip + iphlen);
264
9bccf70c
A
265 /* destination port of 0 is illegal, based on RFC768. */
266 if (uh->uh_dport == 0)
267 goto bad;
268
1c79356b
A
269 KERNEL_DEBUG(DBG_LAYER_IN_BEG, uh->uh_dport, uh->uh_sport,
270 ip->ip_src.s_addr, ip->ip_dst.s_addr, uh->uh_ulen);
271
272 /*
273 * Make mbuf data length reflect UDP length.
274 * If not enough data to reflect UDP length, drop.
275 */
276 len = ntohs((u_short)uh->uh_ulen);
277 if (ip->ip_len != len) {
278 if (len > ip->ip_len || len < sizeof(struct udphdr)) {
279 udpstat.udps_badlen++;
280 goto bad;
281 }
282 m_adj(m, len - ip->ip_len);
283 /* ip->ip_len = len; */
284 }
285 /*
286 * Save a copy of the IP header in case we want restore it
287 * for sending an ICMP error message in response.
288 */
289 save_ip = *ip;
290
291 /*
292 * Checksum extended UDP header and data.
293 */
294 if (uh->uh_sum) {
0b4e3aa0
A
295 if (m->m_pkthdr.csum_flags & CSUM_DATA_VALID) {
296 if (m->m_pkthdr.csum_flags & CSUM_PSEUDO_HDR)
297 uh->uh_sum = m->m_pkthdr.csum_data;
9bccf70c
A
298 else
299 goto doudpcksum;
0b4e3aa0
A
300 uh->uh_sum ^= 0xffff;
301 } else {
302doudpcksum:
303 bzero(((struct ipovly *)ip)->ih_x1, 9);
304 ((struct ipovly *)ip)->ih_len = uh->uh_ulen;
305 uh->uh_sum = in_cksum(m, len + sizeof (struct ip));
306 }
1c79356b
A
307 if (uh->uh_sum) {
308 udpstat.udps_badsum++;
309 m_freem(m);
310 KERNEL_DEBUG(DBG_FNC_UDP_INPUT | DBG_FUNC_END, 0,0,0,0,0);
311 return;
312 }
313 }
9bccf70c
A
314#ifndef __APPLE__
315 else
316 udpstat.udps_nosum++;
317#endif
1c79356b
A
318
319 if (IN_MULTICAST(ntohl(ip->ip_dst.s_addr)) ||
320 in_broadcast(ip->ip_dst, m->m_pkthdr.rcvif)) {
321 struct inpcb *last;
322 /*
323 * Deliver a multicast or broadcast datagram to *all* sockets
324 * for which the local and remote addresses and ports match
325 * those of the incoming datagram. This allows more than
326 * one process to receive multi/broadcasts on the same port.
327 * (This really ought to be done for unicast datagrams as
328 * well, but that would cause problems with existing
329 * applications that open both address-specific sockets and
330 * a wildcard socket listening to the same port -- they would
331 * end up receiving duplicates of every unicast datagram.
332 * Those applications open the multiple sockets to overcome an
333 * inadequacy of the UDP socket interface, but for backwards
334 * compatibility we avoid the problem here rather than
335 * fixing the interface. Maybe 4.5BSD will remedy this?)
336 */
337
338 /*
339 * Construct sockaddr format source address.
340 */
341 udp_in.sin_port = uh->uh_sport;
342 udp_in.sin_addr = ip->ip_src;
343 /*
344 * Locate pcb(s) for datagram.
345 * (Algorithm copied from raw_intr().)
346 */
347 last = NULL;
348#if INET6
349 udp_in6.uin6_init_done = udp_ip6.uip6_init_done = 0;
350#endif
351 LIST_FOREACH(inp, &udb, inp_list) {
9bccf70c
A
352#ifdef __APPLE__
353 /* Ignore nat/SharedIP dummy pcbs */
354 if (inp->inp_socket == &udbinfo.nat_dummy_socket)
355 continue;
356#endif
1c79356b
A
357#if INET6
358 if ((inp->inp_vflag & INP_IPV4) == 0)
359 continue;
360#endif
361 if (inp->inp_lport != uh->uh_dport)
362 continue;
363 if (inp->inp_laddr.s_addr != INADDR_ANY) {
364 if (inp->inp_laddr.s_addr !=
365 ip->ip_dst.s_addr)
366 continue;
367 }
368 if (inp->inp_faddr.s_addr != INADDR_ANY) {
369 if (inp->inp_faddr.s_addr !=
370 ip->ip_src.s_addr ||
371 inp->inp_fport != uh->uh_sport)
372 continue;
373 }
374
375 if (last != NULL) {
376 struct mbuf *n;
377
378#if IPSEC
379 /* check AH/ESP integrity. */
9bccf70c 380 if (ipsec_bypass == 0 && ipsec4_in_reject_so(m, last->inp_socket)) {
1c79356b
A
381 ipsecstat.in_polvio++;
382 /* do not inject data to pcb */
383 } else
384#endif /*IPSEC*/
385 if ((n = m_copy(m, 0, M_COPYALL)) != NULL) {
9bccf70c
A
386 udp_append(last, ip, n,
387 iphlen +
388 sizeof(struct udphdr));
1c79356b
A
389 }
390 }
391 last = inp;
392 /*
393 * Don't look for additional matches if this one does
394 * not have either the SO_REUSEPORT or SO_REUSEADDR
395 * socket options set. This heuristic avoids searching
396 * through all pcbs in the common case of a non-shared
397 * port. It * assumes that an application will never
398 * clear these options after setting them.
399 */
400 if ((last->inp_socket->so_options&(SO_REUSEPORT|SO_REUSEADDR)) == 0)
401 break;
402 }
403
404 if (last == NULL) {
405 /*
406 * No matching pcb found; discard datagram.
407 * (No need to send an ICMP Port Unreachable
408 * for a broadcast or multicast datgram.)
409 */
410 udpstat.udps_noportbcast++;
411 goto bad;
412 }
413#if IPSEC
1c79356b 414 /* check AH/ESP integrity. */
9bccf70c 415 if (ipsec_bypass == 0 && m && ipsec4_in_reject_so(m, last->inp_socket)) {
1c79356b
A
416 ipsecstat.in_polvio++;
417 goto bad;
418 }
419#endif /*IPSEC*/
9bccf70c 420 udp_append(last, ip, m, iphlen + sizeof(struct udphdr));
1c79356b
A
421 return;
422 }
423 /*
424 * Locate pcb for datagram.
425 */
426 inp = in_pcblookup_hash(&udbinfo, ip->ip_src, uh->uh_sport,
427 ip->ip_dst, uh->uh_dport, 1, m->m_pkthdr.rcvif);
428 if (inp == NULL) {
429 if (log_in_vain) {
430 char buf[4*sizeof "123"];
431
432 strcpy(buf, inet_ntoa(ip->ip_dst));
433 log(LOG_INFO,
434 "Connection attempt to UDP %s:%d from %s:%d\n",
435 buf, ntohs(uh->uh_dport), inet_ntoa(ip->ip_src),
436 ntohs(uh->uh_sport));
437 }
438 udpstat.udps_noport++;
439 if (m->m_flags & (M_BCAST | M_MCAST)) {
440 udpstat.udps_noportbcast++;
441 goto bad;
442 }
1c79356b 443#if ICMP_BANDLIM
9bccf70c 444 if (badport_bandlim(BANDLIM_ICMP_UNREACH) < 0)
1c79356b
A
445 goto bad;
446#endif
9bccf70c
A
447 if (blackhole)
448 goto bad;
449 *ip = save_ip;
450 ip->ip_len += iphlen;
1c79356b
A
451 icmp_error(m, ICMP_UNREACH, ICMP_UNREACH_PORT, 0, 0);
452 KERNEL_DEBUG(DBG_FNC_UDP_INPUT | DBG_FUNC_END, 0,0,0,0,0);
453 return;
454 }
455#if IPSEC
9bccf70c 456 if (ipsec_bypass == 0 && inp != NULL && ipsec4_in_reject_so(m, inp->inp_socket)) {
1c79356b
A
457 ipsecstat.in_polvio++;
458 goto bad;
459 }
460#endif /*IPSEC*/
461
462 /*
463 * Construct sockaddr format source address.
464 * Stuff source address and datagram in user buffer.
465 */
466 udp_in.sin_port = uh->uh_sport;
467 udp_in.sin_addr = ip->ip_src;
468 if (inp->inp_flags & INP_CONTROLOPTS
469 || inp->inp_socket->so_options & SO_TIMESTAMP) {
470#if INET6
471 if (inp->inp_vflag & INP_IPV6) {
472 int savedflags;
473
474 ip_2_ip6_hdr(&udp_ip6.uip6_ip6, ip);
475 savedflags = inp->inp_flags;
476 inp->inp_flags &= ~INP_UNMAPPABLEOPTS;
9bccf70c 477 ip6_savecontrol(inp, &opts, &udp_ip6.uip6_ip6, m);
1c79356b
A
478 inp->inp_flags = savedflags;
479 } else
480#endif
481 ip_savecontrol(inp, &opts, ip, m);
482 }
9bccf70c 483 m_adj(m, iphlen + sizeof(struct udphdr));
1c79356b
A
484
485 KERNEL_DEBUG(DBG_LAYER_IN_END, uh->uh_dport, uh->uh_sport,
486 save_ip.ip_src.s_addr, save_ip.ip_dst.s_addr, uh->uh_ulen);
487
488#if INET6
489 if (inp->inp_vflag & INP_IPV6) {
490 in6_sin_2_v4mapsin6(&udp_in, &udp_in6.uin6_sin);
491 append_sa = (struct sockaddr *)&udp_in6;
1c79356b
A
492 } else
493#endif
494 append_sa = (struct sockaddr *)&udp_in;
495 if (sbappendaddr(&inp->inp_socket->so_rcv, append_sa, m, opts) == 0) {
496 udpstat.udps_fullsock++;
497 goto bad;
498 }
499 sorwakeup(inp->inp_socket);
500 KERNEL_DEBUG(DBG_FNC_UDP_INPUT | DBG_FUNC_END, 0,0,0,0,0);
501 return;
502bad:
503 m_freem(m);
504 if (opts)
505 m_freem(opts);
506 KERNEL_DEBUG(DBG_FNC_UDP_INPUT | DBG_FUNC_END, 0,0,0,0,0);
9bccf70c 507 return;
1c79356b
A
508}
509
510#if INET6
511static void
512ip_2_ip6_hdr(ip6, ip)
513 struct ip6_hdr *ip6;
514 struct ip *ip;
515{
516 bzero(ip6, sizeof(*ip6));
517
518 ip6->ip6_vfc = IPV6_VERSION;
519 ip6->ip6_plen = ip->ip_len;
520 ip6->ip6_nxt = ip->ip_p;
521 ip6->ip6_hlim = ip->ip_ttl;
522 ip6->ip6_src.s6_addr32[2] = ip6->ip6_dst.s6_addr32[2] =
523 IPV6_ADDR_INT32_SMP;
524 ip6->ip6_src.s6_addr32[3] = ip->ip_src.s_addr;
525 ip6->ip6_dst.s6_addr32[3] = ip->ip_dst.s_addr;
526}
527#endif
528
529/*
530 * subroutine of udp_input(), mainly for source code readability.
531 * caller must properly init udp_ip6 and udp_in6 beforehand.
532 */
533static void
534udp_append(last, ip, n, off)
535 struct inpcb *last;
536 struct ip *ip;
537 struct mbuf *n;
9bccf70c 538 int off;
1c79356b
A
539{
540 struct sockaddr *append_sa;
541 struct mbuf *opts = 0;
1c79356b
A
542
543 if (last->inp_flags & INP_CONTROLOPTS ||
544 last->inp_socket->so_options & SO_TIMESTAMP) {
545#if INET6
546 if (last->inp_vflag & INP_IPV6) {
547 int savedflags;
548
549 if (udp_ip6.uip6_init_done == 0) {
550 ip_2_ip6_hdr(&udp_ip6.uip6_ip6, ip);
551 udp_ip6.uip6_init_done = 1;
552 }
553 savedflags = last->inp_flags;
554 last->inp_flags &= ~INP_UNMAPPABLEOPTS;
9bccf70c 555 ip6_savecontrol(last, &opts, &udp_ip6.uip6_ip6, n);
1c79356b
A
556 last->inp_flags = savedflags;
557 } else
558#endif
559 ip_savecontrol(last, &opts, ip, n);
560 }
561#if INET6
562 if (last->inp_vflag & INP_IPV6) {
563 if (udp_in6.uin6_init_done == 0) {
564 in6_sin_2_v4mapsin6(&udp_in, &udp_in6.uin6_sin);
565 udp_in6.uin6_init_done = 1;
566 }
567 append_sa = (struct sockaddr *)&udp_in6.uin6_sin;
1c79356b
A
568 } else
569#endif
570 append_sa = (struct sockaddr *)&udp_in;
571 m_adj(n, off);
1c79356b
A
572 if (sbappendaddr(&last->inp_socket->so_rcv, append_sa, n, opts) == 0) {
573 m_freem(n);
574 if (opts)
575 m_freem(opts);
576 udpstat.udps_fullsock++;
577 } else
578 sorwakeup(last->inp_socket);
579}
580
1c79356b
A
581/*
582 * Notify a udp user of an asynchronous error;
583 * just wake up so that he can collect error status.
584 */
585void
586udp_notify(inp, errno)
587 register struct inpcb *inp;
588 int errno;
589{
590 inp->inp_socket->so_error = errno;
591 sorwakeup(inp->inp_socket);
592 sowwakeup(inp->inp_socket);
593}
594
595void
596udp_ctlinput(cmd, sa, vip)
597 int cmd;
598 struct sockaddr *sa;
599 void *vip;
600{
9bccf70c
A
601 struct ip *ip = vip;
602 struct udphdr *uh;
603 void (*notify) __P((struct inpcb *, int)) = udp_notify;
604 struct in_addr faddr;
605 struct inpcb *inp;
606 int s;
607
608 faddr = ((struct sockaddr_in *)sa)->sin_addr;
609 if (sa->sa_family != AF_INET || faddr.s_addr == INADDR_ANY)
610 return;
1c79356b 611
9bccf70c
A
612 if (PRC_IS_REDIRECT(cmd)) {
613 ip = 0;
614 notify = in_rtchange;
615 } else if (cmd == PRC_HOSTDEAD)
616 ip = 0;
617 else if ((unsigned)cmd >= PRC_NCMDS || inetctlerrmap[cmd] == 0)
1c79356b
A
618 return;
619 if (ip) {
9bccf70c 620 s = splnet();
1c79356b 621 uh = (struct udphdr *)((caddr_t)ip + (ip->ip_hl << 2));
9bccf70c
A
622 inp = in_pcblookup_hash(&udbinfo, faddr, uh->uh_dport,
623 ip->ip_src, uh->uh_sport, 0, NULL);
624 if (inp != NULL && inp->inp_socket != NULL)
625 (*notify)(inp, inetctlerrmap[cmd]);
626 splx(s);
1c79356b 627 } else
9bccf70c 628 in_pcbnotifyall(&udb, faddr, inetctlerrmap[cmd], notify);
1c79356b
A
629}
630
1c79356b
A
631static int
632udp_pcblist SYSCTL_HANDLER_ARGS
633{
634 int error, i, n, s;
635 struct inpcb *inp, **inp_list;
636 inp_gen_t gencnt;
637 struct xinpgen xig;
638
639 /*
640 * The process of preparing the TCB list is too time-consuming and
641 * resource-intensive to repeat twice on every request.
642 */
643 if (req->oldptr == 0) {
644 n = udbinfo.ipi_count;
645 req->oldidx = 2 * (sizeof xig)
646 + (n + n/8) * sizeof(struct xinpcb);
647 return 0;
648 }
649
650 if (req->newptr != 0)
651 return EPERM;
652
653 /*
654 * OK, now we're committed to doing something.
655 */
656 s = splnet();
657 gencnt = udbinfo.ipi_gencnt;
658 n = udbinfo.ipi_count;
659 splx(s);
660
661 xig.xig_len = sizeof xig;
662 xig.xig_count = n;
663 xig.xig_gen = gencnt;
664 xig.xig_sogen = so_gencnt;
665 error = SYSCTL_OUT(req, &xig, sizeof xig);
666 if (error)
667 return error;
9bccf70c
A
668 /*
669 * We are done if there is no pcb
670 */
671 if (n == 0)
672 return 0;
1c79356b
A
673
674 inp_list = _MALLOC(n * sizeof *inp_list, M_TEMP, M_WAITOK);
675 if (inp_list == 0) {
676 return ENOMEM;
677 }
9bccf70c
A
678
679 for (inp = LIST_FIRST(udbinfo.listhead), i = 0; inp && i < n;
680 inp = LIST_NEXT(inp, inp_list)) {
1c79356b
A
681 if (inp->inp_gencnt <= gencnt)
682 inp_list[i++] = inp;
683 }
684 splx(s);
685 n = i;
686
687 error = 0;
688 for (i = 0; i < n; i++) {
689 inp = inp_list[i];
690 if (inp->inp_gencnt <= gencnt) {
691 struct xinpcb xi;
692 xi.xi_len = sizeof xi;
693 /* XXX should avoid extra copy */
694 bcopy(inp, &xi.xi_inp, sizeof *inp);
695 if (inp->inp_socket)
696 sotoxsocket(inp->inp_socket, &xi.xi_socket);
697 error = SYSCTL_OUT(req, &xi, sizeof xi);
698 }
699 }
700 if (!error) {
701 /*
702 * Give the user an updated idea of our state.
703 * If the generation differs from what we told
704 * her before, she knows that something happened
705 * while we were processing this request, and it
706 * might be necessary to retry.
707 */
708 s = splnet();
709 xig.xig_gen = udbinfo.ipi_gencnt;
710 xig.xig_sogen = so_gencnt;
711 xig.xig_count = udbinfo.ipi_count;
712 splx(s);
713 error = SYSCTL_OUT(req, &xig, sizeof xig);
714 }
715 FREE(inp_list, M_TEMP);
716 return error;
717}
718
719SYSCTL_PROC(_net_inet_udp, UDPCTL_PCBLIST, pcblist, CTLFLAG_RD, 0, 0,
720 udp_pcblist, "S,xinpcb", "List of active UDP sockets");
721
722
723
724static int
725udp_output(inp, m, addr, control, p)
726 register struct inpcb *inp;
9bccf70c 727 struct mbuf *m;
1c79356b
A
728 struct sockaddr *addr;
729 struct mbuf *control;
730 struct proc *p;
731{
732 register struct udpiphdr *ui;
733 register int len = m->m_pkthdr.len;
734 struct in_addr laddr;
735 int s = 0, error = 0;
736
737 KERNEL_DEBUG(DBG_FNC_UDP_OUTPUT | DBG_FUNC_START, 0,0,0,0,0);
738
739 if (control)
740 m_freem(control); /* XXX */
741
742 KERNEL_DEBUG(DBG_LAYER_OUT_BEG, inp->inp_fport, inp->inp_lport,
743 inp->inp_laddr.s_addr, inp->inp_faddr.s_addr,
744 (htons((u_short)len + sizeof (struct udphdr))));
745
746 if (len + sizeof(struct udpiphdr) > IP_MAXPACKET) {
747 error = EMSGSIZE;
748 goto release;
749 }
750
751 if (addr) {
752 laddr = inp->inp_laddr;
753 if (inp->inp_faddr.s_addr != INADDR_ANY) {
754 error = EISCONN;
755 goto release;
756 }
757 /*
758 * Must block input while temporarily connected.
759 */
760 s = splnet();
761 error = in_pcbconnect(inp, addr, p);
762 if (error) {
763 splx(s);
764 goto release;
765 }
766 } else {
767 if (inp->inp_faddr.s_addr == INADDR_ANY) {
768 error = ENOTCONN;
769 goto release;
770 }
771 }
772 /*
773 * Calculate data length and get a mbuf
774 * for UDP and IP headers.
775 */
776 M_PREPEND(m, sizeof(struct udpiphdr), M_DONTWAIT);
777 if (m == 0) {
778 error = ENOBUFS;
779 if (addr)
780 splx(s);
781 goto release;
782 }
783
784 /*
785 * Fill in mbuf with extended UDP header
786 * and addresses and length put into network format.
787 */
788 ui = mtod(m, struct udpiphdr *);
9bccf70c 789 bzero(ui->ui_x1, sizeof(ui->ui_x1)); /* XXX still needed? */
1c79356b 790 ui->ui_pr = IPPROTO_UDP;
1c79356b
A
791 ui->ui_src = inp->inp_laddr;
792 ui->ui_dst = inp->inp_faddr;
793 ui->ui_sport = inp->inp_lport;
794 ui->ui_dport = inp->inp_fport;
9bccf70c 795 ui->ui_ulen = htons((u_short)len + sizeof(struct udphdr));
1c79356b
A
796
797 /*
9bccf70c 798 * Set up checksum and output datagram.
1c79356b 799 */
1c79356b 800 if (udpcksum) {
9bccf70c
A
801 ui->ui_sum = in_pseudo(ui->ui_src.s_addr, ui->ui_dst.s_addr,
802 htons((u_short)len + sizeof(struct udphdr) + IPPROTO_UDP));
803 m->m_pkthdr.csum_flags = CSUM_UDP;
804 m->m_pkthdr.csum_data = offsetof(struct udphdr, uh_sum);
805 } else {
0b4e3aa0 806 ui->ui_sum = 0;
9bccf70c 807 }
1c79356b
A
808 ((struct ip *)ui)->ip_len = sizeof (struct udpiphdr) + len;
809 ((struct ip *)ui)->ip_ttl = inp->inp_ip_ttl; /* XXX */
810 ((struct ip *)ui)->ip_tos = inp->inp_ip_tos; /* XXX */
811 udpstat.udps_opackets++;
812
813 KERNEL_DEBUG(DBG_LAYER_OUT_END, ui->ui_dport, ui->ui_sport,
814 ui->ui_src.s_addr, ui->ui_dst.s_addr, ui->ui_ulen);
815
1c79356b 816#if IPSEC
9bccf70c
A
817 if (ipsec_bypass == 0 && ipsec_setsocket(m, inp->inp_socket) != 0) {
818 error = ENOBUFS;
819 goto release;
820 }
1c79356b 821#endif /*IPSEC*/
1c79356b 822 error = ip_output(m, inp->inp_options, &inp->inp_route,
9bccf70c 823 (inp->inp_socket->so_options & (SO_DONTROUTE | SO_BROADCAST)),
1c79356b
A
824 inp->inp_moptions);
825
826 if (addr) {
827 in_pcbdisconnect(inp);
828 inp->inp_laddr = laddr; /* XXX rehash? */
829 splx(s);
830 }
831 KERNEL_DEBUG(DBG_FNC_UDP_OUTPUT | DBG_FUNC_END, error, 0,0,0,0);
832 return (error);
833
834release:
835 m_freem(m);
836 KERNEL_DEBUG(DBG_FNC_UDP_OUTPUT | DBG_FUNC_END, error, 0,0,0,0);
837 return (error);
838}
839
840u_long udp_sendspace = 9216; /* really max datagram size */
841 /* 40 1K datagrams */
842SYSCTL_INT(_net_inet_udp, UDPCTL_MAXDGRAM, maxdgram, CTLFLAG_RW,
9bccf70c 843 &udp_sendspace, 0, "Maximum outgoing UDP datagram size");
1c79356b 844
9bccf70c 845u_long udp_recvspace = 40 * (1024 +
1c79356b
A
846#if INET6
847 sizeof(struct sockaddr_in6)
9bccf70c 848#else
1c79356b 849 sizeof(struct sockaddr_in)
9bccf70c 850#endif
1c79356b
A
851 );
852SYSCTL_INT(_net_inet_udp, UDPCTL_RECVSPACE, recvspace, CTLFLAG_RW,
9bccf70c 853 &udp_recvspace, 0, "Maximum incoming UDP datagram size");
1c79356b
A
854
855static int
856udp_abort(struct socket *so)
857{
858 struct inpcb *inp;
859 int s;
860
861 inp = sotoinpcb(so);
862 if (inp == 0)
863 return EINVAL; /* ??? possible? panic instead? */
864 soisdisconnected(so);
865 s = splnet();
866 in_pcbdetach(inp);
867 splx(s);
868 return 0;
869}
870
871static int
872udp_attach(struct socket *so, int proto, struct proc *p)
873{
874 struct inpcb *inp;
875 int error; long s;
876
9bccf70c
A
877 inp = sotoinpcb(so);
878 if (inp != 0)
879 return EINVAL;
880
881 error = soreserve(so, udp_sendspace, udp_recvspace);
882 if (error)
883 return error;
1c79356b
A
884 s = splnet();
885 error = in_pcballoc(so, &udbinfo, p);
886 splx(s);
1c79356b
A
887 if (error)
888 return error;
889 inp = (struct inpcb *)so->so_pcb;
890 inp->inp_vflag |= INP_IPV4;
891 inp->inp_ip_ttl = ip_defttl;
1c79356b
A
892 return 0;
893}
894
895static int
896udp_bind(struct socket *so, struct sockaddr *nam, struct proc *p)
897{
898 struct inpcb *inp;
899 int s, error;
900
901 inp = sotoinpcb(so);
902 if (inp == 0)
903 return EINVAL;
904 s = splnet();
905 error = in_pcbbind(inp, nam, p);
906 splx(s);
907 return error;
908}
909
910static int
911udp_connect(struct socket *so, struct sockaddr *nam, struct proc *p)
912{
913 struct inpcb *inp;
914 int s, error;
915
916 inp = sotoinpcb(so);
917 if (inp == 0)
918 return EINVAL;
919 if (inp->inp_faddr.s_addr != INADDR_ANY)
920 return EISCONN;
921 s = splnet();
922 error = in_pcbconnect(inp, nam, p);
923 splx(s);
924 if (error == 0)
925 soisconnected(so);
926 return error;
927}
928
929static int
930udp_detach(struct socket *so)
931{
932 struct inpcb *inp;
933 int s;
934
935 inp = sotoinpcb(so);
936 if (inp == 0)
937 return EINVAL;
938 s = splnet();
939 in_pcbdetach(inp);
940 splx(s);
941 return 0;
942}
943
944static int
945udp_disconnect(struct socket *so)
946{
947 struct inpcb *inp;
948 int s;
949
950 inp = sotoinpcb(so);
951 if (inp == 0)
952 return EINVAL;
953 if (inp->inp_faddr.s_addr == INADDR_ANY)
954 return ENOTCONN;
955
956 s = splnet();
957 in_pcbdisconnect(inp);
958 inp->inp_laddr.s_addr = INADDR_ANY;
959 splx(s);
960 so->so_state &= ~SS_ISCONNECTED; /* XXX */
961 return 0;
962}
963
964static int
965udp_send(struct socket *so, int flags, struct mbuf *m, struct sockaddr *addr,
966 struct mbuf *control, struct proc *p)
967{
968 struct inpcb *inp;
969
970 inp = sotoinpcb(so);
971 if (inp == 0) {
972 m_freem(m);
973 return EINVAL;
974 }
975 return udp_output(inp, m, addr, control, p);
976}
977
978int
979udp_shutdown(struct socket *so)
980{
981 struct inpcb *inp;
982
983 inp = sotoinpcb(so);
984 if (inp == 0)
985 return EINVAL;
986 socantsendmore(so);
987 return 0;
988}
989
990struct pr_usrreqs udp_usrreqs = {
991 udp_abort, pru_accept_notsupp, udp_attach, udp_bind, udp_connect,
992 pru_connect2_notsupp, in_control, udp_detach, udp_disconnect,
993 pru_listen_notsupp, in_setpeeraddr, pru_rcvd_notsupp,
994 pru_rcvoob_notsupp, udp_send, pru_sense_null, udp_shutdown,
995 in_setsockaddr, sosend, soreceive, sopoll
996};
997