]> git.saurik.com Git - apple/xnu.git/blob - bsd/netinet/udp_usrreq.c
xnu-517.3.15.tar.gz
[apple/xnu.git] / bsd / netinet / udp_usrreq.c
1 /*
2 * Copyright (c) 2000 Apple Computer, Inc. All rights reserved.
3 *
4 * @APPLE_LICENSE_HEADER_START@
5 *
6 * Copyright (c) 1999-2003 Apple Computer, Inc. All Rights Reserved.
7 *
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
17 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
18 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
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.
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
58 * $FreeBSD: src/sys/netinet/udp_usrreq.c,v 1.64.2.13 2001/08/08 18:59:54 ghelmer Exp $
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>
66 #include <sys/domain.h>
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
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>
79 #if INET6
80 #include <netinet/ip6.h>
81 #endif
82 #include <netinet/in_pcb.h>
83 #include <netinet/in_var.h>
84 #include <netinet/ip_var.h>
85 #if INET6
86 #include <netinet6/ip6_var.h>
87 #endif
88 #include <netinet/ip_icmp.h>
89 #include <netinet/icmp_var.h>
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>
96 extern int ipsec_bypass;
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
107 /*
108 * UDP protocol implementation.
109 * Per RFC 768, August, 1980.
110 */
111 #ifndef COMPAT_42
112 static int udpcksum = 1;
113 #else
114 static int udpcksum = 0; /* XXX */
115 #endif
116 SYSCTL_INT(_net_inet_udp, UDPCTL_CHECKSUM, checksum, CTLFLAG_RW,
117 &udpcksum, 0, "");
118
119 int log_in_vain = 0;
120 SYSCTL_INT(_net_inet_udp, OID_AUTO, log_in_vain, CTLFLAG_RW,
121 &log_in_vain, 0, "Log all incoming UDP packets");
122
123 static int blackhole = 0;
124 SYSCTL_INT(_net_inet_udp, OID_AUTO, blackhole, CTLFLAG_RW,
125 &blackhole, 0, "Do not send port unreachables for refused connects");
126
127 struct inpcbhead udb; /* from udp_var.h */
128 #define udb6 udb /* for KAME src sync over BSD*'s */
129 struct inpcbinfo udbinfo;
130
131 #ifndef UDBHASHSIZE
132 #define UDBHASHSIZE 16
133 #endif
134
135 extern int apple_hwcksum_rx;
136 extern int esp_udp_encap_port;
137 extern u_long route_generation;
138
139 struct udpstat udpstat; /* from udp_var.h */
140 SYSCTL_STRUCT(_net_inet_udp, UDPCTL_STATS, stats, CTLFLAG_RD,
141 &udpstat, udpstat, "UDP statistics (struct udpstat, netinet/udp_var.h)");
142
143 static struct sockaddr_in udp_in = { sizeof(udp_in), AF_INET };
144 #if INET6
145 struct 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 };
152 struct udp_ip6 {
153 struct ip6_hdr uip6_ip6;
154 u_char uip6_init_done : 1;
155 } udp_ip6;
156 #endif /* INET6 */
157
158 static void udp_append __P((struct inpcb *last, struct ip *ip,
159 struct mbuf *n, int off));
160 #if INET6
161 static void ip_2_ip6_hdr __P((struct ip6_hdr *ip6, struct ip *ip));
162 #endif
163
164 static int udp_detach __P((struct socket *so));
165 static int udp_output __P((struct inpcb *, struct mbuf *, struct sockaddr *,
166 struct mbuf *, struct proc *));
167
168 void
169 udp_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);
183 #ifdef __APPLE__
184 str_size = (vm_size_t) sizeof(struct inpcb);
185 udbinfo.ipi_zone = (void *) zinit(str_size, 80000*str_size, 8192, "udpcb");
186
187 udbinfo.last_pcb = 0;
188 in_pcb_nat_init(&udbinfo, AF_INET, IPPROTO_UDP, SOCK_DGRAM);
189 #else
190 udbinfo.ipi_zone = zinit("udpcb", sizeof(struct inpcb), maxsockets,
191 ZONE_INTERRUPT, 0);
192 #endif
193
194 #if 0
195 /* for pcb sharing testing only */
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
221 void
222 udp_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;
230 int len;
231 struct ip save_ip;
232 struct sockaddr *append_sa;
233
234 udpstat.udps_ipackets++;
235
236 KERNEL_DEBUG(DBG_FNC_UDP_INPUT | DBG_FUNC_START, 0,0,0,0,0);
237 if (m->m_pkthdr.csum_flags & CSUM_TCP_SUM16)
238 m->m_pkthdr.csum_flags = 0; /* invalidate hwcksum for UDP */
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
265 /* destination port of 0 is illegal, based on RFC768. */
266 if (uh->uh_dport == 0)
267 goto bad;
268
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) {
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;
298 else
299 goto doudpcksum;
300 uh->uh_sum ^= 0xffff;
301 } else {
302 char b[9];
303 doudpcksum:
304 *(uint32_t*)&b[0] = *(uint32_t*)&((struct ipovly *)ip)->ih_x1[0];
305 *(uint32_t*)&b[4] = *(uint32_t*)&((struct ipovly *)ip)->ih_x1[4];
306 *(uint8_t*)&b[8] = *(uint8_t*)&((struct ipovly *)ip)->ih_x1[8];
307
308 bzero(((struct ipovly *)ip)->ih_x1, 9);
309 ((struct ipovly *)ip)->ih_len = uh->uh_ulen;
310 uh->uh_sum = in_cksum(m, len + sizeof (struct ip));
311
312 *(uint32_t*)&((struct ipovly *)ip)->ih_x1[0] = *(uint32_t*)&b[0];
313 *(uint32_t*)&((struct ipovly *)ip)->ih_x1[4] = *(uint32_t*)&b[4];
314 *(uint8_t*)&((struct ipovly *)ip)->ih_x1[8] = *(uint8_t*)&b[8];
315 }
316 if (uh->uh_sum) {
317 udpstat.udps_badsum++;
318 m_freem(m);
319 KERNEL_DEBUG(DBG_FNC_UDP_INPUT | DBG_FUNC_END, 0,0,0,0,0);
320 return;
321 }
322 }
323 #ifndef __APPLE__
324 else
325 udpstat.udps_nosum++;
326 #endif
327
328 if (IN_MULTICAST(ntohl(ip->ip_dst.s_addr)) ||
329 in_broadcast(ip->ip_dst, m->m_pkthdr.rcvif)) {
330 struct inpcb *last;
331 /*
332 * Deliver a multicast or broadcast datagram to *all* sockets
333 * for which the local and remote addresses and ports match
334 * those of the incoming datagram. This allows more than
335 * one process to receive multi/broadcasts on the same port.
336 * (This really ought to be done for unicast datagrams as
337 * well, but that would cause problems with existing
338 * applications that open both address-specific sockets and
339 * a wildcard socket listening to the same port -- they would
340 * end up receiving duplicates of every unicast datagram.
341 * Those applications open the multiple sockets to overcome an
342 * inadequacy of the UDP socket interface, but for backwards
343 * compatibility we avoid the problem here rather than
344 * fixing the interface. Maybe 4.5BSD will remedy this?)
345 */
346
347 /*
348 * Construct sockaddr format source address.
349 */
350 udp_in.sin_port = uh->uh_sport;
351 udp_in.sin_addr = ip->ip_src;
352 /*
353 * Locate pcb(s) for datagram.
354 * (Algorithm copied from raw_intr().)
355 */
356 last = NULL;
357 #if INET6
358 udp_in6.uin6_init_done = udp_ip6.uip6_init_done = 0;
359 #endif
360 LIST_FOREACH(inp, &udb, inp_list) {
361 #ifdef __APPLE__
362 /* Ignore nat/SharedIP dummy pcbs */
363 if (inp->inp_socket == &udbinfo.nat_dummy_socket)
364 continue;
365 #endif
366 #if INET6
367 if ((inp->inp_vflag & INP_IPV4) == 0)
368 continue;
369 #endif
370 if (inp->inp_lport != uh->uh_dport)
371 continue;
372 if (inp->inp_laddr.s_addr != INADDR_ANY) {
373 if (inp->inp_laddr.s_addr !=
374 ip->ip_dst.s_addr)
375 continue;
376 }
377 if (inp->inp_faddr.s_addr != INADDR_ANY) {
378 if (inp->inp_faddr.s_addr !=
379 ip->ip_src.s_addr ||
380 inp->inp_fport != uh->uh_sport)
381 continue;
382 }
383
384 if (last != NULL) {
385 struct mbuf *n;
386
387 #if IPSEC
388 /* check AH/ESP integrity. */
389 if (ipsec_bypass == 0 && ipsec4_in_reject_so(m, last->inp_socket)) {
390 ipsecstat.in_polvio++;
391 /* do not inject data to pcb */
392 } else
393 #endif /*IPSEC*/
394 if ((n = m_copy(m, 0, M_COPYALL)) != NULL) {
395 udp_append(last, ip, n,
396 iphlen +
397 sizeof(struct udphdr));
398 }
399 }
400 last = inp;
401 /*
402 * Don't look for additional matches if this one does
403 * not have either the SO_REUSEPORT or SO_REUSEADDR
404 * socket options set. This heuristic avoids searching
405 * through all pcbs in the common case of a non-shared
406 * port. It * assumes that an application will never
407 * clear these options after setting them.
408 */
409 if ((last->inp_socket->so_options&(SO_REUSEPORT|SO_REUSEADDR)) == 0)
410 break;
411 }
412
413 if (last == NULL) {
414 /*
415 * No matching pcb found; discard datagram.
416 * (No need to send an ICMP Port Unreachable
417 * for a broadcast or multicast datgram.)
418 */
419 udpstat.udps_noportbcast++;
420 goto bad;
421 }
422 #if IPSEC
423 /* check AH/ESP integrity. */
424 if (ipsec_bypass == 0 && m && ipsec4_in_reject_so(m, last->inp_socket)) {
425 ipsecstat.in_polvio++;
426 goto bad;
427 }
428 #endif /*IPSEC*/
429 udp_append(last, ip, m, iphlen + sizeof(struct udphdr));
430 return;
431 }
432
433 /*
434 * UDP to port 4500 with a payload where the first four bytes are
435 * not zero is a UDP encapsulated IPSec packet. Packets where
436 * the payload is one byte and that byte is 0xFF are NAT keepalive
437 * packets. Decapsulate the ESP packet and carry on with IPSec input
438 * or discard the NAT keep-alive.
439 */
440 if (ipsec_bypass == 0 && (esp_udp_encap_port & 0xFFFF) != 0 &&
441 uh->uh_dport == ntohs((u_short)esp_udp_encap_port)) {
442 int payload_len = len - sizeof(struct udphdr) > 4 ? 4 : len - sizeof(struct udphdr);
443 if (m->m_len < iphlen + sizeof(struct udphdr) + payload_len) {
444 if ((m = m_pullup(m, iphlen + sizeof(struct udphdr) + payload_len)) == 0) {
445 udpstat.udps_hdrops++;
446 KERNEL_DEBUG(DBG_FNC_UDP_INPUT | DBG_FUNC_END, 0,0,0,0,0);
447 return;
448 }
449 ip = mtod(m, struct ip *);
450 uh = (struct udphdr *)((caddr_t)ip + iphlen);
451 }
452 /* Check for NAT keepalive packet */
453 if (payload_len == 1 && *(u_int8_t*)((caddr_t)uh + sizeof(struct udphdr)) == 0xFF) {
454 m_freem(m);
455 KERNEL_DEBUG(DBG_FNC_UDP_INPUT | DBG_FUNC_END, 0,0,0,0,0);
456 return;
457 }
458 else if (payload_len == 4 && *(u_int32_t*)((caddr_t)uh + sizeof(struct udphdr)) != 0) {
459 /* UDP encapsulated IPSec packet to pass through NAT */
460 size_t stripsiz;
461
462 stripsiz = sizeof(struct udphdr);
463
464 ip = mtod(m, struct ip *);
465 ovbcopy((caddr_t)ip, (caddr_t)(((u_char *)ip) + stripsiz), iphlen);
466 m->m_data += stripsiz;
467 m->m_len -= stripsiz;
468 m->m_pkthdr.len -= stripsiz;
469 ip = mtod(m, struct ip *);
470 ip->ip_len = ip->ip_len - stripsiz;
471 ip->ip_p = IPPROTO_ESP;
472
473 KERNEL_DEBUG(DBG_FNC_UDP_INPUT | DBG_FUNC_END, 0,0,0,0,0);
474 esp4_input(m, iphlen);
475 return;
476 }
477 }
478
479 /*
480 * Locate pcb for datagram.
481 */
482 inp = in_pcblookup_hash(&udbinfo, ip->ip_src, uh->uh_sport,
483 ip->ip_dst, uh->uh_dport, 1, m->m_pkthdr.rcvif);
484 if (inp == NULL) {
485 if (log_in_vain) {
486 char buf[4*sizeof "123"];
487
488 strcpy(buf, inet_ntoa(ip->ip_dst));
489 log(LOG_INFO,
490 "Connection attempt to UDP %s:%d from %s:%d\n",
491 buf, ntohs(uh->uh_dport), inet_ntoa(ip->ip_src),
492 ntohs(uh->uh_sport));
493 }
494 udpstat.udps_noport++;
495 if (m->m_flags & (M_BCAST | M_MCAST)) {
496 udpstat.udps_noportbcast++;
497 goto bad;
498 }
499 #if ICMP_BANDLIM
500 if (badport_bandlim(BANDLIM_ICMP_UNREACH) < 0)
501 goto bad;
502 #endif
503 if (blackhole)
504 goto bad;
505 *ip = save_ip;
506 ip->ip_len += iphlen;
507 icmp_error(m, ICMP_UNREACH, ICMP_UNREACH_PORT, 0, 0);
508 KERNEL_DEBUG(DBG_FNC_UDP_INPUT | DBG_FUNC_END, 0,0,0,0,0);
509 return;
510 }
511 #if IPSEC
512 if (ipsec_bypass == 0 && inp != NULL && ipsec4_in_reject_so(m, inp->inp_socket)) {
513 ipsecstat.in_polvio++;
514 goto bad;
515 }
516 #endif /*IPSEC*/
517
518 /*
519 * Construct sockaddr format source address.
520 * Stuff source address and datagram in user buffer.
521 */
522 udp_in.sin_port = uh->uh_sport;
523 udp_in.sin_addr = ip->ip_src;
524 if (inp->inp_flags & INP_CONTROLOPTS
525 || inp->inp_socket->so_options & SO_TIMESTAMP) {
526 #if INET6
527 if (inp->inp_vflag & INP_IPV6) {
528 int savedflags;
529
530 ip_2_ip6_hdr(&udp_ip6.uip6_ip6, ip);
531 savedflags = inp->inp_flags;
532 inp->inp_flags &= ~INP_UNMAPPABLEOPTS;
533 ip6_savecontrol(inp, &opts, &udp_ip6.uip6_ip6, m);
534 inp->inp_flags = savedflags;
535 } else
536 #endif
537 ip_savecontrol(inp, &opts, ip, m);
538 }
539 m_adj(m, iphlen + sizeof(struct udphdr));
540
541 KERNEL_DEBUG(DBG_LAYER_IN_END, uh->uh_dport, uh->uh_sport,
542 save_ip.ip_src.s_addr, save_ip.ip_dst.s_addr, uh->uh_ulen);
543
544 #if INET6
545 if (inp->inp_vflag & INP_IPV6) {
546 in6_sin_2_v4mapsin6(&udp_in, &udp_in6.uin6_sin);
547 append_sa = (struct sockaddr *)&udp_in6;
548 } else
549 #endif
550 append_sa = (struct sockaddr *)&udp_in;
551 if (sbappendaddr(&inp->inp_socket->so_rcv, append_sa, m, opts) == 0) {
552 udpstat.udps_fullsock++;
553 goto bad;
554 }
555 sorwakeup(inp->inp_socket);
556 KERNEL_DEBUG(DBG_FNC_UDP_INPUT | DBG_FUNC_END, 0,0,0,0,0);
557 return;
558 bad:
559 m_freem(m);
560 if (opts)
561 m_freem(opts);
562 KERNEL_DEBUG(DBG_FNC_UDP_INPUT | DBG_FUNC_END, 0,0,0,0,0);
563 return;
564 }
565
566 #if INET6
567 static void
568 ip_2_ip6_hdr(ip6, ip)
569 struct ip6_hdr *ip6;
570 struct ip *ip;
571 {
572 bzero(ip6, sizeof(*ip6));
573
574 ip6->ip6_vfc = IPV6_VERSION;
575 ip6->ip6_plen = ip->ip_len;
576 ip6->ip6_nxt = ip->ip_p;
577 ip6->ip6_hlim = ip->ip_ttl;
578 ip6->ip6_src.s6_addr32[2] = ip6->ip6_dst.s6_addr32[2] =
579 IPV6_ADDR_INT32_SMP;
580 ip6->ip6_src.s6_addr32[3] = ip->ip_src.s_addr;
581 ip6->ip6_dst.s6_addr32[3] = ip->ip_dst.s_addr;
582 }
583 #endif
584
585 /*
586 * subroutine of udp_input(), mainly for source code readability.
587 * caller must properly init udp_ip6 and udp_in6 beforehand.
588 */
589 static void
590 udp_append(last, ip, n, off)
591 struct inpcb *last;
592 struct ip *ip;
593 struct mbuf *n;
594 int off;
595 {
596 struct sockaddr *append_sa;
597 struct mbuf *opts = 0;
598
599 if (last->inp_flags & INP_CONTROLOPTS ||
600 last->inp_socket->so_options & SO_TIMESTAMP) {
601 #if INET6
602 if (last->inp_vflag & INP_IPV6) {
603 int savedflags;
604
605 if (udp_ip6.uip6_init_done == 0) {
606 ip_2_ip6_hdr(&udp_ip6.uip6_ip6, ip);
607 udp_ip6.uip6_init_done = 1;
608 }
609 savedflags = last->inp_flags;
610 last->inp_flags &= ~INP_UNMAPPABLEOPTS;
611 ip6_savecontrol(last, &opts, &udp_ip6.uip6_ip6, n);
612 last->inp_flags = savedflags;
613 } else
614 #endif
615 ip_savecontrol(last, &opts, ip, n);
616 }
617 #if INET6
618 if (last->inp_vflag & INP_IPV6) {
619 if (udp_in6.uin6_init_done == 0) {
620 in6_sin_2_v4mapsin6(&udp_in, &udp_in6.uin6_sin);
621 udp_in6.uin6_init_done = 1;
622 }
623 append_sa = (struct sockaddr *)&udp_in6.uin6_sin;
624 } else
625 #endif
626 append_sa = (struct sockaddr *)&udp_in;
627 m_adj(n, off);
628 if (sbappendaddr(&last->inp_socket->so_rcv, append_sa, n, opts) == 0) {
629 m_freem(n);
630 if (opts)
631 m_freem(opts);
632 udpstat.udps_fullsock++;
633 } else
634 sorwakeup(last->inp_socket);
635 }
636
637 /*
638 * Notify a udp user of an asynchronous error;
639 * just wake up so that he can collect error status.
640 */
641 void
642 udp_notify(inp, errno)
643 register struct inpcb *inp;
644 int errno;
645 {
646 inp->inp_socket->so_error = errno;
647 sorwakeup(inp->inp_socket);
648 sowwakeup(inp->inp_socket);
649 }
650
651 void
652 udp_ctlinput(cmd, sa, vip)
653 int cmd;
654 struct sockaddr *sa;
655 void *vip;
656 {
657 struct ip *ip = vip;
658 struct udphdr *uh;
659 void (*notify) __P((struct inpcb *, int)) = udp_notify;
660 struct in_addr faddr;
661 struct inpcb *inp;
662 int s;
663
664 faddr = ((struct sockaddr_in *)sa)->sin_addr;
665 if (sa->sa_family != AF_INET || faddr.s_addr == INADDR_ANY)
666 return;
667
668 if (PRC_IS_REDIRECT(cmd)) {
669 ip = 0;
670 notify = in_rtchange;
671 } else if (cmd == PRC_HOSTDEAD)
672 ip = 0;
673 else if ((unsigned)cmd >= PRC_NCMDS || inetctlerrmap[cmd] == 0)
674 return;
675 if (ip) {
676 s = splnet();
677 uh = (struct udphdr *)((caddr_t)ip + (ip->ip_hl << 2));
678 inp = in_pcblookup_hash(&udbinfo, faddr, uh->uh_dport,
679 ip->ip_src, uh->uh_sport, 0, NULL);
680 if (inp != NULL && inp->inp_socket != NULL)
681 (*notify)(inp, inetctlerrmap[cmd]);
682 splx(s);
683 } else
684 in_pcbnotifyall(&udb, faddr, inetctlerrmap[cmd], notify);
685 }
686
687 static int
688 udp_pcblist SYSCTL_HANDLER_ARGS
689 {
690 int error, i, n, s;
691 struct inpcb *inp, **inp_list;
692 inp_gen_t gencnt;
693 struct xinpgen xig;
694
695 /*
696 * The process of preparing the TCB list is too time-consuming and
697 * resource-intensive to repeat twice on every request.
698 */
699 if (req->oldptr == 0) {
700 n = udbinfo.ipi_count;
701 req->oldidx = 2 * (sizeof xig)
702 + (n + n/8) * sizeof(struct xinpcb);
703 return 0;
704 }
705
706 if (req->newptr != 0)
707 return EPERM;
708
709 /*
710 * OK, now we're committed to doing something.
711 */
712 s = splnet();
713 gencnt = udbinfo.ipi_gencnt;
714 n = udbinfo.ipi_count;
715 splx(s);
716
717 xig.xig_len = sizeof xig;
718 xig.xig_count = n;
719 xig.xig_gen = gencnt;
720 xig.xig_sogen = so_gencnt;
721 error = SYSCTL_OUT(req, &xig, sizeof xig);
722 if (error)
723 return error;
724 /*
725 * We are done if there is no pcb
726 */
727 if (n == 0)
728 return 0;
729
730 inp_list = _MALLOC(n * sizeof *inp_list, M_TEMP, M_WAITOK);
731 if (inp_list == 0) {
732 return ENOMEM;
733 }
734
735 for (inp = LIST_FIRST(udbinfo.listhead), i = 0; inp && i < n;
736 inp = LIST_NEXT(inp, inp_list)) {
737 if (inp->inp_gencnt <= gencnt)
738 inp_list[i++] = inp;
739 }
740 splx(s);
741 n = i;
742
743 error = 0;
744 for (i = 0; i < n; i++) {
745 inp = inp_list[i];
746 if (inp->inp_gencnt <= gencnt) {
747 struct xinpcb xi;
748 xi.xi_len = sizeof xi;
749 /* XXX should avoid extra copy */
750 bcopy(inp, &xi.xi_inp, sizeof *inp);
751 if (inp->inp_socket)
752 sotoxsocket(inp->inp_socket, &xi.xi_socket);
753 error = SYSCTL_OUT(req, &xi, sizeof xi);
754 }
755 }
756 if (!error) {
757 /*
758 * Give the user an updated idea of our state.
759 * If the generation differs from what we told
760 * her before, she knows that something happened
761 * while we were processing this request, and it
762 * might be necessary to retry.
763 */
764 s = splnet();
765 xig.xig_gen = udbinfo.ipi_gencnt;
766 xig.xig_sogen = so_gencnt;
767 xig.xig_count = udbinfo.ipi_count;
768 splx(s);
769 error = SYSCTL_OUT(req, &xig, sizeof xig);
770 }
771 FREE(inp_list, M_TEMP);
772 return error;
773 }
774
775 SYSCTL_PROC(_net_inet_udp, UDPCTL_PCBLIST, pcblist, CTLFLAG_RD, 0, 0,
776 udp_pcblist, "S,xinpcb", "List of active UDP sockets");
777
778
779
780 static int
781 udp_output(inp, m, addr, control, p)
782 register struct inpcb *inp;
783 struct mbuf *m;
784 struct sockaddr *addr;
785 struct mbuf *control;
786 struct proc *p;
787 {
788 register struct udpiphdr *ui;
789 register int len = m->m_pkthdr.len;
790 struct in_addr laddr;
791 int s = 0, error = 0;
792
793 KERNEL_DEBUG(DBG_FNC_UDP_OUTPUT | DBG_FUNC_START, 0,0,0,0,0);
794
795 if (control)
796 m_freem(control); /* XXX */
797
798 KERNEL_DEBUG(DBG_LAYER_OUT_BEG, inp->inp_fport, inp->inp_lport,
799 inp->inp_laddr.s_addr, inp->inp_faddr.s_addr,
800 (htons((u_short)len + sizeof (struct udphdr))));
801
802 if (len + sizeof(struct udpiphdr) > IP_MAXPACKET) {
803 error = EMSGSIZE;
804 goto release;
805 }
806
807 /* If there was a routing change, discard cached route and check
808 * that we have a valid source address.
809 * Reacquire a new source address if INADDR_ANY was specified
810 */
811
812 if (inp->inp_route.ro_rt && inp->inp_route.ro_rt->generation_id != route_generation) {
813 if (ifa_foraddr(inp->inp_laddr.s_addr) == NULL) { /* src address is gone */
814 if (inp->inp_flags & INP_INADDR_ANY)
815 inp->inp_faddr.s_addr = INADDR_ANY; /* new src will be set later */
816 else {
817 error = EADDRNOTAVAIL;
818 goto release;
819 }
820 }
821 rtfree(inp->inp_route.ro_rt);
822 inp->inp_route.ro_rt = (struct rtentry *)0;
823 }
824
825 if (addr) {
826 laddr = inp->inp_laddr;
827 if (inp->inp_faddr.s_addr != INADDR_ANY) {
828 error = EISCONN;
829 goto release;
830 }
831 /*
832 * Must block input while temporarily connected.
833 */
834 s = splnet();
835 error = in_pcbconnect(inp, addr, p);
836 if (error) {
837 splx(s);
838 goto release;
839 }
840 } else {
841 if (inp->inp_faddr.s_addr == INADDR_ANY) {
842 error = ENOTCONN;
843 goto release;
844 }
845 }
846
847
848 /*
849 * Calculate data length and get a mbuf
850 * for UDP and IP headers.
851 */
852 M_PREPEND(m, sizeof(struct udpiphdr), M_DONTWAIT);
853 if (m == 0) {
854 error = ENOBUFS;
855 goto abort;
856 }
857
858 /*
859 * Fill in mbuf with extended UDP header
860 * and addresses and length put into network format.
861 */
862 ui = mtod(m, struct udpiphdr *);
863 bzero(ui->ui_x1, sizeof(ui->ui_x1)); /* XXX still needed? */
864 ui->ui_pr = IPPROTO_UDP;
865 ui->ui_src = inp->inp_laddr;
866 ui->ui_dst = inp->inp_faddr;
867 ui->ui_sport = inp->inp_lport;
868 ui->ui_dport = inp->inp_fport;
869 ui->ui_ulen = htons((u_short)len + sizeof(struct udphdr));
870
871 /*
872 * Set up checksum and output datagram.
873 */
874 if (udpcksum) {
875 ui->ui_sum = in_pseudo(ui->ui_src.s_addr, ui->ui_dst.s_addr,
876 htons((u_short)len + sizeof(struct udphdr) + IPPROTO_UDP));
877 m->m_pkthdr.csum_flags = CSUM_UDP;
878 m->m_pkthdr.csum_data = offsetof(struct udphdr, uh_sum);
879 } else {
880 ui->ui_sum = 0;
881 }
882 ((struct ip *)ui)->ip_len = sizeof (struct udpiphdr) + len;
883 ((struct ip *)ui)->ip_ttl = inp->inp_ip_ttl; /* XXX */
884 ((struct ip *)ui)->ip_tos = inp->inp_ip_tos; /* XXX */
885 udpstat.udps_opackets++;
886
887 KERNEL_DEBUG(DBG_LAYER_OUT_END, ui->ui_dport, ui->ui_sport,
888 ui->ui_src.s_addr, ui->ui_dst.s_addr, ui->ui_ulen);
889
890 #if IPSEC
891 if (ipsec_bypass == 0 && ipsec_setsocket(m, inp->inp_socket) != 0) {
892 error = ENOBUFS;
893 goto abort;
894 }
895 #endif /*IPSEC*/
896 error = ip_output(m, inp->inp_options, &inp->inp_route,
897 (inp->inp_socket->so_options & (SO_DONTROUTE | SO_BROADCAST)),
898 inp->inp_moptions);
899
900 if (addr) {
901 in_pcbdisconnect(inp);
902 inp->inp_laddr = laddr; /* XXX rehash? */
903 splx(s);
904 }
905 KERNEL_DEBUG(DBG_FNC_UDP_OUTPUT | DBG_FUNC_END, error, 0,0,0,0);
906 return (error);
907
908 abort:
909 if (addr) {
910 in_pcbdisconnect(inp);
911 inp->inp_laddr = laddr; /* XXX rehash? */
912 splx(s);
913 }
914
915 release:
916 m_freem(m);
917 KERNEL_DEBUG(DBG_FNC_UDP_OUTPUT | DBG_FUNC_END, error, 0,0,0,0);
918 return (error);
919 }
920
921 u_long udp_sendspace = 9216; /* really max datagram size */
922 /* 40 1K datagrams */
923 SYSCTL_INT(_net_inet_udp, UDPCTL_MAXDGRAM, maxdgram, CTLFLAG_RW,
924 &udp_sendspace, 0, "Maximum outgoing UDP datagram size");
925
926 u_long udp_recvspace = 40 * (1024 +
927 #if INET6
928 sizeof(struct sockaddr_in6)
929 #else
930 sizeof(struct sockaddr_in)
931 #endif
932 );
933 SYSCTL_INT(_net_inet_udp, UDPCTL_RECVSPACE, recvspace, CTLFLAG_RW,
934 &udp_recvspace, 0, "Maximum incoming UDP datagram size");
935
936 static int
937 udp_abort(struct socket *so)
938 {
939 struct inpcb *inp;
940 int s;
941
942 inp = sotoinpcb(so);
943 if (inp == 0)
944 return EINVAL; /* ??? possible? panic instead? */
945 soisdisconnected(so);
946 s = splnet();
947 in_pcbdetach(inp);
948 splx(s);
949 return 0;
950 }
951
952 static int
953 udp_attach(struct socket *so, int proto, struct proc *p)
954 {
955 struct inpcb *inp;
956 int error; long s;
957
958 inp = sotoinpcb(so);
959 if (inp != 0)
960 return EINVAL;
961
962 error = soreserve(so, udp_sendspace, udp_recvspace);
963 if (error)
964 return error;
965 s = splnet();
966 error = in_pcballoc(so, &udbinfo, p);
967 splx(s);
968 if (error)
969 return error;
970 inp = (struct inpcb *)so->so_pcb;
971 inp->inp_vflag |= INP_IPV4;
972 inp->inp_ip_ttl = ip_defttl;
973 return 0;
974 }
975
976 static int
977 udp_bind(struct socket *so, struct sockaddr *nam, struct proc *p)
978 {
979 struct inpcb *inp;
980 int s, error;
981
982 inp = sotoinpcb(so);
983 if (inp == 0)
984 return EINVAL;
985 s = splnet();
986 error = in_pcbbind(inp, nam, p);
987 splx(s);
988 return error;
989 }
990
991 static int
992 udp_connect(struct socket *so, struct sockaddr *nam, struct proc *p)
993 {
994 struct inpcb *inp;
995 int s, error;
996
997 inp = sotoinpcb(so);
998 if (inp == 0)
999 return EINVAL;
1000 if (inp->inp_faddr.s_addr != INADDR_ANY)
1001 return EISCONN;
1002 s = splnet();
1003 error = in_pcbconnect(inp, nam, p);
1004 splx(s);
1005 if (error == 0)
1006 soisconnected(so);
1007 return error;
1008 }
1009
1010 static int
1011 udp_detach(struct socket *so)
1012 {
1013 struct inpcb *inp;
1014 int s;
1015
1016 inp = sotoinpcb(so);
1017 if (inp == 0)
1018 return EINVAL;
1019 s = splnet();
1020 in_pcbdetach(inp);
1021 splx(s);
1022 return 0;
1023 }
1024
1025 static int
1026 udp_disconnect(struct socket *so)
1027 {
1028 struct inpcb *inp;
1029 int s;
1030
1031 inp = sotoinpcb(so);
1032 if (inp == 0)
1033 return EINVAL;
1034 if (inp->inp_faddr.s_addr == INADDR_ANY)
1035 return ENOTCONN;
1036
1037 s = splnet();
1038 in_pcbdisconnect(inp);
1039 inp->inp_laddr.s_addr = INADDR_ANY;
1040 splx(s);
1041 so->so_state &= ~SS_ISCONNECTED; /* XXX */
1042 return 0;
1043 }
1044
1045 static int
1046 udp_send(struct socket *so, int flags, struct mbuf *m, struct sockaddr *addr,
1047 struct mbuf *control, struct proc *p)
1048 {
1049 struct inpcb *inp;
1050
1051 inp = sotoinpcb(so);
1052 if (inp == 0) {
1053 m_freem(m);
1054 return EINVAL;
1055 }
1056 return udp_output(inp, m, addr, control, p);
1057 }
1058
1059 int
1060 udp_shutdown(struct socket *so)
1061 {
1062 struct inpcb *inp;
1063
1064 inp = sotoinpcb(so);
1065 if (inp == 0)
1066 return EINVAL;
1067 socantsendmore(so);
1068 return 0;
1069 }
1070
1071 struct pr_usrreqs udp_usrreqs = {
1072 udp_abort, pru_accept_notsupp, udp_attach, udp_bind, udp_connect,
1073 pru_connect2_notsupp, in_control, udp_detach, udp_disconnect,
1074 pru_listen_notsupp, in_setpeeraddr, pru_rcvd_notsupp,
1075 pru_rcvoob_notsupp, udp_send, pru_sense_null, udp_shutdown,
1076 in_setsockaddr, sosend, soreceive, sopoll
1077 };
1078