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