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