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