1 | /* $NetBSD: udp_usrreq.c,v 1.229 2016/11/18 06:50:04 knakahara Exp $ */ |
2 | |
3 | /* |
4 | * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project. |
5 | * All rights reserved. |
6 | * |
7 | * Redistribution and use in source and binary forms, with or without |
8 | * modification, are permitted provided that the following conditions |
9 | * are met: |
10 | * 1. Redistributions of source code must retain the above copyright |
11 | * notice, this list of conditions and the following disclaimer. |
12 | * 2. Redistributions in binary form must reproduce the above copyright |
13 | * notice, this list of conditions and the following disclaimer in the |
14 | * documentation and/or other materials provided with the distribution. |
15 | * 3. Neither the name of the project nor the names of its contributors |
16 | * may be used to endorse or promote products derived from this software |
17 | * without specific prior written permission. |
18 | * |
19 | * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND |
20 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
21 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
22 | * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE |
23 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
24 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS |
25 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
26 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT |
27 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY |
28 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF |
29 | * SUCH DAMAGE. |
30 | */ |
31 | |
32 | /* |
33 | * Copyright (c) 1982, 1986, 1988, 1990, 1993, 1995 |
34 | * The Regents of the University of California. All rights reserved. |
35 | * |
36 | * Redistribution and use in source and binary forms, with or without |
37 | * modification, are permitted provided that the following conditions |
38 | * are met: |
39 | * 1. Redistributions of source code must retain the above copyright |
40 | * notice, this list of conditions and the following disclaimer. |
41 | * 2. Redistributions in binary form must reproduce the above copyright |
42 | * notice, this list of conditions and the following disclaimer in the |
43 | * documentation and/or other materials provided with the distribution. |
44 | * 3. Neither the name of the University nor the names of its contributors |
45 | * may be used to endorse or promote products derived from this software |
46 | * without specific prior written permission. |
47 | * |
48 | * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND |
49 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
50 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
51 | * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE |
52 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
53 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS |
54 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
55 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT |
56 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY |
57 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF |
58 | * SUCH DAMAGE. |
59 | * |
60 | * @(#)udp_usrreq.c 8.6 (Berkeley) 5/23/95 |
61 | */ |
62 | |
63 | /* |
64 | * UDP protocol implementation. |
65 | * Per RFC 768, August, 1980. |
66 | */ |
67 | |
68 | #include <sys/cdefs.h> |
69 | __KERNEL_RCSID(0, "$NetBSD: udp_usrreq.c,v 1.229 2016/11/18 06:50:04 knakahara Exp $" ); |
70 | |
71 | #ifdef _KERNEL_OPT |
72 | #include "opt_inet.h" |
73 | #include "opt_compat_netbsd.h" |
74 | #include "opt_ipsec.h" |
75 | #include "opt_inet_csum.h" |
76 | #include "opt_ipkdb.h" |
77 | #include "opt_mbuftrace.h" |
78 | #include "opt_net_mpsafe.h" |
79 | #endif |
80 | |
81 | #include <sys/param.h> |
82 | #include <sys/mbuf.h> |
83 | #include <sys/once.h> |
84 | #include <sys/protosw.h> |
85 | #include <sys/socket.h> |
86 | #include <sys/socketvar.h> |
87 | #include <sys/systm.h> |
88 | #include <sys/proc.h> |
89 | #include <sys/domain.h> |
90 | #include <sys/sysctl.h> |
91 | |
92 | #include <net/if.h> |
93 | |
94 | #include <netinet/in.h> |
95 | #include <netinet/in_systm.h> |
96 | #include <netinet/in_var.h> |
97 | #include <netinet/ip.h> |
98 | #include <netinet/in_pcb.h> |
99 | #include <netinet/ip_var.h> |
100 | #include <netinet/ip_icmp.h> |
101 | #include <netinet/udp.h> |
102 | #include <netinet/udp_var.h> |
103 | #include <netinet/udp_private.h> |
104 | |
105 | #ifdef INET6 |
106 | #include <netinet/ip6.h> |
107 | #include <netinet/icmp6.h> |
108 | #include <netinet6/ip6_var.h> |
109 | #include <netinet6/ip6_private.h> |
110 | #include <netinet6/in6_pcb.h> |
111 | #include <netinet6/udp6_var.h> |
112 | #include <netinet6/udp6_private.h> |
113 | #endif |
114 | |
115 | #ifndef INET6 |
116 | /* always need ip6.h for IP6_EXTHDR_GET */ |
117 | #include <netinet/ip6.h> |
118 | #endif |
119 | |
120 | #ifdef IPSEC |
121 | #include <netipsec/ipsec.h> |
122 | #include <netipsec/ipsec_var.h> |
123 | #include <netipsec/ipsec_private.h> |
124 | #include <netipsec/esp.h> |
125 | #ifdef INET6 |
126 | #include <netipsec/ipsec6.h> |
127 | #endif |
128 | #endif /* IPSEC */ |
129 | |
130 | #ifdef COMPAT_50 |
131 | #include <compat/sys/socket.h> |
132 | #endif |
133 | |
134 | #ifdef IPKDB |
135 | #include <ipkdb/ipkdb.h> |
136 | #endif |
137 | |
138 | int udpcksum = 1; |
139 | int udp_do_loopback_cksum = 0; |
140 | |
141 | struct inpcbtable udbtable; |
142 | |
143 | percpu_t *udpstat_percpu; |
144 | |
145 | #ifdef INET |
146 | #ifdef IPSEC |
147 | static int udp4_espinudp (struct mbuf **, int, struct sockaddr *, |
148 | struct socket *); |
149 | #endif |
150 | static void udp4_sendup (struct mbuf *, int, struct sockaddr *, |
151 | struct socket *); |
152 | static int udp4_realinput (struct sockaddr_in *, struct sockaddr_in *, |
153 | struct mbuf **, int); |
154 | static int udp4_input_checksum(struct mbuf *, const struct udphdr *, int, int); |
155 | #endif |
156 | #ifdef INET |
157 | static void udp_notify (struct inpcb *, int); |
158 | #endif |
159 | |
160 | #ifndef UDBHASHSIZE |
161 | #define UDBHASHSIZE 128 |
162 | #endif |
163 | int udbhashsize = UDBHASHSIZE; |
164 | |
165 | /* |
166 | * For send - really max datagram size; for receive - 40 1K datagrams. |
167 | */ |
168 | static int udp_sendspace = 9216; |
169 | static int udp_recvspace = 40 * (1024 + sizeof(struct sockaddr_in)); |
170 | |
171 | #ifdef MBUFTRACE |
172 | struct mowner udp_mowner = MOWNER_INIT("udp" , "" ); |
173 | struct mowner udp_rx_mowner = MOWNER_INIT("udp" , "rx" ); |
174 | struct mowner udp_tx_mowner = MOWNER_INIT("udp" , "tx" ); |
175 | #endif |
176 | |
177 | #ifdef UDP_CSUM_COUNTERS |
178 | #include <sys/device.h> |
179 | |
180 | #if defined(INET) |
181 | struct evcnt udp_hwcsum_bad = EVCNT_INITIALIZER(EVCNT_TYPE_MISC, |
182 | NULL, "udp" , "hwcsum bad" ); |
183 | struct evcnt udp_hwcsum_ok = EVCNT_INITIALIZER(EVCNT_TYPE_MISC, |
184 | NULL, "udp" , "hwcsum ok" ); |
185 | struct evcnt udp_hwcsum_data = EVCNT_INITIALIZER(EVCNT_TYPE_MISC, |
186 | NULL, "udp" , "hwcsum data" ); |
187 | struct evcnt udp_swcsum = EVCNT_INITIALIZER(EVCNT_TYPE_MISC, |
188 | NULL, "udp" , "swcsum" ); |
189 | |
190 | EVCNT_ATTACH_STATIC(udp_hwcsum_bad); |
191 | EVCNT_ATTACH_STATIC(udp_hwcsum_ok); |
192 | EVCNT_ATTACH_STATIC(udp_hwcsum_data); |
193 | EVCNT_ATTACH_STATIC(udp_swcsum); |
194 | #endif /* defined(INET) */ |
195 | |
196 | #define UDP_CSUM_COUNTER_INCR(ev) (ev)->ev_count++ |
197 | #else |
198 | #define UDP_CSUM_COUNTER_INCR(ev) /* nothing */ |
199 | #endif /* UDP_CSUM_COUNTERS */ |
200 | |
201 | static void sysctl_net_inet_udp_setup(struct sysctllog **); |
202 | |
203 | static int |
204 | do_udpinit(void) |
205 | { |
206 | |
207 | in_pcbinit(&udbtable, udbhashsize, udbhashsize); |
208 | udpstat_percpu = percpu_alloc(sizeof(uint64_t) * UDP_NSTATS); |
209 | |
210 | MOWNER_ATTACH(&udp_tx_mowner); |
211 | MOWNER_ATTACH(&udp_rx_mowner); |
212 | MOWNER_ATTACH(&udp_mowner); |
213 | |
214 | return 0; |
215 | } |
216 | |
217 | void |
218 | udp_init_common(void) |
219 | { |
220 | static ONCE_DECL(doudpinit); |
221 | |
222 | RUN_ONCE(&doudpinit, do_udpinit); |
223 | } |
224 | |
225 | void |
226 | udp_init(void) |
227 | { |
228 | |
229 | sysctl_net_inet_udp_setup(NULL); |
230 | |
231 | udp_init_common(); |
232 | } |
233 | |
234 | /* |
235 | * Checksum extended UDP header and data. |
236 | */ |
237 | |
238 | int |
239 | udp_input_checksum(int af, struct mbuf *m, const struct udphdr *uh, |
240 | int iphlen, int len) |
241 | { |
242 | |
243 | switch (af) { |
244 | #ifdef INET |
245 | case AF_INET: |
246 | return udp4_input_checksum(m, uh, iphlen, len); |
247 | #endif |
248 | #ifdef INET6 |
249 | case AF_INET6: |
250 | return udp6_input_checksum(m, uh, iphlen, len); |
251 | #endif |
252 | } |
253 | #ifdef DIAGNOSTIC |
254 | panic("udp_input_checksum: unknown af %d" , af); |
255 | #endif |
256 | /* NOTREACHED */ |
257 | return -1; |
258 | } |
259 | |
260 | #ifdef INET |
261 | |
262 | /* |
263 | * Checksum extended UDP header and data. |
264 | */ |
265 | |
266 | static int |
267 | udp4_input_checksum(struct mbuf *m, const struct udphdr *uh, |
268 | int iphlen, int len) |
269 | { |
270 | |
271 | /* |
272 | * XXX it's better to record and check if this mbuf is |
273 | * already checked. |
274 | */ |
275 | |
276 | if (uh->uh_sum == 0) |
277 | return 0; |
278 | |
279 | switch (m->m_pkthdr.csum_flags & |
280 | ((m_get_rcvif_NOMPSAFE(m)->if_csum_flags_rx & M_CSUM_UDPv4) | |
281 | M_CSUM_TCP_UDP_BAD | M_CSUM_DATA)) { |
282 | case M_CSUM_UDPv4|M_CSUM_TCP_UDP_BAD: |
283 | UDP_CSUM_COUNTER_INCR(&udp_hwcsum_bad); |
284 | goto badcsum; |
285 | |
286 | case M_CSUM_UDPv4|M_CSUM_DATA: { |
287 | u_int32_t hw_csum = m->m_pkthdr.csum_data; |
288 | |
289 | UDP_CSUM_COUNTER_INCR(&udp_hwcsum_data); |
290 | if (m->m_pkthdr.csum_flags & M_CSUM_NO_PSEUDOHDR) { |
291 | const struct ip *ip = |
292 | mtod(m, const struct ip *); |
293 | |
294 | hw_csum = in_cksum_phdr(ip->ip_src.s_addr, |
295 | ip->ip_dst.s_addr, |
296 | htons(hw_csum + len + IPPROTO_UDP)); |
297 | } |
298 | if ((hw_csum ^ 0xffff) != 0) |
299 | goto badcsum; |
300 | break; |
301 | } |
302 | |
303 | case M_CSUM_UDPv4: |
304 | /* Checksum was okay. */ |
305 | UDP_CSUM_COUNTER_INCR(&udp_hwcsum_ok); |
306 | break; |
307 | |
308 | default: |
309 | /* |
310 | * Need to compute it ourselves. Maybe skip checksum |
311 | * on loopback interfaces. |
312 | */ |
313 | if (__predict_true(!(m_get_rcvif_NOMPSAFE(m)->if_flags & |
314 | IFF_LOOPBACK) || |
315 | udp_do_loopback_cksum)) { |
316 | UDP_CSUM_COUNTER_INCR(&udp_swcsum); |
317 | if (in4_cksum(m, IPPROTO_UDP, iphlen, len) != 0) |
318 | goto badcsum; |
319 | } |
320 | break; |
321 | } |
322 | |
323 | return 0; |
324 | |
325 | badcsum: |
326 | UDP_STATINC(UDP_STAT_BADSUM); |
327 | return -1; |
328 | } |
329 | |
330 | void |
331 | udp_input(struct mbuf *m, ...) |
332 | { |
333 | va_list ap; |
334 | struct sockaddr_in src, dst; |
335 | struct ip *ip; |
336 | struct udphdr *uh; |
337 | int iphlen; |
338 | int len; |
339 | int n; |
340 | u_int16_t ip_len; |
341 | |
342 | va_start(ap, m); |
343 | iphlen = va_arg(ap, int); |
344 | (void)va_arg(ap, int); /* ignore value, advance ap */ |
345 | va_end(ap); |
346 | |
347 | MCLAIM(m, &udp_rx_mowner); |
348 | UDP_STATINC(UDP_STAT_IPACKETS); |
349 | |
350 | /* |
351 | * Get IP and UDP header together in first mbuf. |
352 | */ |
353 | ip = mtod(m, struct ip *); |
354 | IP6_EXTHDR_GET(uh, struct udphdr *, m, iphlen, sizeof(struct udphdr)); |
355 | if (uh == NULL) { |
356 | UDP_STATINC(UDP_STAT_HDROPS); |
357 | return; |
358 | } |
359 | /* |
360 | * Enforce alignment requirements that are violated in |
361 | * some cases, see kern/50766 for details. |
362 | */ |
363 | if (UDP_HDR_ALIGNED_P(uh) == 0) { |
364 | m = m_copyup(m, iphlen + sizeof(struct udphdr), 0); |
365 | if (m == NULL) { |
366 | UDP_STATINC(UDP_STAT_HDROPS); |
367 | return; |
368 | } |
369 | ip = mtod(m, struct ip *); |
370 | uh = (struct udphdr *)(mtod(m, char *) + iphlen); |
371 | } |
372 | KASSERT(UDP_HDR_ALIGNED_P(uh)); |
373 | |
374 | /* destination port of 0 is illegal, based on RFC768. */ |
375 | if (uh->uh_dport == 0) |
376 | goto bad; |
377 | |
378 | /* |
379 | * Make mbuf data length reflect UDP length. |
380 | * If not enough data to reflect UDP length, drop. |
381 | */ |
382 | ip_len = ntohs(ip->ip_len); |
383 | len = ntohs((u_int16_t)uh->uh_ulen); |
384 | if (ip_len != iphlen + len) { |
385 | if (ip_len < iphlen + len || len < sizeof(struct udphdr)) { |
386 | UDP_STATINC(UDP_STAT_BADLEN); |
387 | goto bad; |
388 | } |
389 | m_adj(m, iphlen + len - ip_len); |
390 | } |
391 | |
392 | /* |
393 | * Checksum extended UDP header and data. |
394 | */ |
395 | if (udp4_input_checksum(m, uh, iphlen, len)) |
396 | goto badcsum; |
397 | |
398 | /* construct source and dst sockaddrs. */ |
399 | sockaddr_in_init(&src, &ip->ip_src, uh->uh_sport); |
400 | sockaddr_in_init(&dst, &ip->ip_dst, uh->uh_dport); |
401 | |
402 | if ((n = udp4_realinput(&src, &dst, &m, iphlen)) == -1) { |
403 | UDP_STATINC(UDP_STAT_HDROPS); |
404 | return; |
405 | } |
406 | if (m == NULL) { |
407 | /* |
408 | * packet has been processed by ESP stuff - |
409 | * e.g. dropped NAT-T-keep-alive-packet ... |
410 | */ |
411 | return; |
412 | } |
413 | ip = mtod(m, struct ip *); |
414 | #ifdef INET6 |
415 | if (IN_MULTICAST(ip->ip_dst.s_addr) || n == 0) { |
416 | struct sockaddr_in6 src6, dst6; |
417 | |
418 | memset(&src6, 0, sizeof(src6)); |
419 | src6.sin6_family = AF_INET6; |
420 | src6.sin6_len = sizeof(struct sockaddr_in6); |
421 | in6_in_2_v4mapin6(&ip->ip_src, &src6.sin6_addr); |
422 | src6.sin6_port = uh->uh_sport; |
423 | memset(&dst6, 0, sizeof(dst6)); |
424 | dst6.sin6_family = AF_INET6; |
425 | dst6.sin6_len = sizeof(struct sockaddr_in6); |
426 | in6_in_2_v4mapin6(&ip->ip_dst, &dst6.sin6_addr); |
427 | dst6.sin6_port = uh->uh_dport; |
428 | |
429 | n += udp6_realinput(AF_INET, &src6, &dst6, m, iphlen); |
430 | } |
431 | #endif |
432 | |
433 | if (n == 0) { |
434 | if (m->m_flags & (M_BCAST | M_MCAST)) { |
435 | UDP_STATINC(UDP_STAT_NOPORTBCAST); |
436 | goto bad; |
437 | } |
438 | UDP_STATINC(UDP_STAT_NOPORT); |
439 | #ifdef IPKDB |
440 | if (checkipkdb(&ip->ip_src, uh->uh_sport, uh->uh_dport, |
441 | m, iphlen + sizeof(struct udphdr), |
442 | m->m_pkthdr.len - iphlen - sizeof(struct udphdr))) { |
443 | /* |
444 | * It was a debugger connect packet, |
445 | * just drop it now |
446 | */ |
447 | goto bad; |
448 | } |
449 | #endif |
450 | icmp_error(m, ICMP_UNREACH, ICMP_UNREACH_PORT, 0, 0); |
451 | m = NULL; |
452 | } |
453 | |
454 | bad: |
455 | if (m) |
456 | m_freem(m); |
457 | return; |
458 | |
459 | badcsum: |
460 | m_freem(m); |
461 | } |
462 | #endif |
463 | |
464 | #ifdef INET |
465 | static void |
466 | udp4_sendup(struct mbuf *m, int off /* offset of data portion */, |
467 | struct sockaddr *src, struct socket *so) |
468 | { |
469 | struct mbuf *opts = NULL; |
470 | struct mbuf *n; |
471 | struct inpcb *inp = NULL; |
472 | |
473 | if (!so) |
474 | return; |
475 | switch (so->so_proto->pr_domain->dom_family) { |
476 | case AF_INET: |
477 | inp = sotoinpcb(so); |
478 | break; |
479 | #ifdef INET6 |
480 | case AF_INET6: |
481 | break; |
482 | #endif |
483 | default: |
484 | return; |
485 | } |
486 | |
487 | #if defined(IPSEC) |
488 | /* check AH/ESP integrity. */ |
489 | if (ipsec_used && so != NULL && ipsec4_in_reject_so(m, so)) { |
490 | IPSEC_STATINC(IPSEC_STAT_IN_POLVIO); |
491 | if ((n = m_copypacket(m, M_DONTWAIT)) != NULL) |
492 | icmp_error(n, ICMP_UNREACH, ICMP_UNREACH_ADMIN_PROHIBIT, |
493 | 0, 0); |
494 | return; |
495 | } |
496 | #endif /*IPSEC*/ |
497 | |
498 | if ((n = m_copypacket(m, M_DONTWAIT)) != NULL) { |
499 | if (inp && (inp->inp_flags & INP_CONTROLOPTS |
500 | #ifdef SO_OTIMESTAMP |
501 | || so->so_options & SO_OTIMESTAMP |
502 | #endif |
503 | || so->so_options & SO_TIMESTAMP)) { |
504 | struct ip *ip = mtod(n, struct ip *); |
505 | ip_savecontrol(inp, &opts, ip, n); |
506 | } |
507 | |
508 | m_adj(n, off); |
509 | if (sbappendaddr(&so->so_rcv, src, n, |
510 | opts) == 0) { |
511 | m_freem(n); |
512 | if (opts) |
513 | m_freem(opts); |
514 | so->so_rcv.sb_overflowed++; |
515 | UDP_STATINC(UDP_STAT_FULLSOCK); |
516 | } else |
517 | sorwakeup(so); |
518 | } |
519 | } |
520 | #endif |
521 | |
522 | #ifdef INET |
523 | static int |
524 | udp4_realinput(struct sockaddr_in *src, struct sockaddr_in *dst, |
525 | struct mbuf **mp, int off /* offset of udphdr */) |
526 | { |
527 | u_int16_t *sport, *dport; |
528 | int rcvcnt; |
529 | struct in_addr *src4, *dst4; |
530 | struct inpcb_hdr *inph; |
531 | struct inpcb *inp; |
532 | struct mbuf *m = *mp; |
533 | |
534 | rcvcnt = 0; |
535 | off += sizeof(struct udphdr); /* now, offset of payload */ |
536 | |
537 | if (src->sin_family != AF_INET || dst->sin_family != AF_INET) |
538 | goto bad; |
539 | |
540 | src4 = &src->sin_addr; |
541 | sport = &src->sin_port; |
542 | dst4 = &dst->sin_addr; |
543 | dport = &dst->sin_port; |
544 | |
545 | if (IN_MULTICAST(dst4->s_addr) || |
546 | in_broadcast(*dst4, m_get_rcvif_NOMPSAFE(m))) { |
547 | /* |
548 | * Deliver a multicast or broadcast datagram to *all* sockets |
549 | * for which the local and remote addresses and ports match |
550 | * those of the incoming datagram. This allows more than |
551 | * one process to receive multi/broadcasts on the same port. |
552 | * (This really ought to be done for unicast datagrams as |
553 | * well, but that would cause problems with existing |
554 | * applications that open both address-specific sockets and |
555 | * a wildcard socket listening to the same port -- they would |
556 | * end up receiving duplicates of every unicast datagram. |
557 | * Those applications open the multiple sockets to overcome an |
558 | * inadequacy of the UDP socket interface, but for backwards |
559 | * compatibility we avoid the problem here rather than |
560 | * fixing the interface. Maybe 4.5BSD will remedy this?) |
561 | */ |
562 | |
563 | /* |
564 | * KAME note: traditionally we dropped udpiphdr from mbuf here. |
565 | * we need udpiphdr for IPsec processing so we do that later. |
566 | */ |
567 | /* |
568 | * Locate pcb(s) for datagram. |
569 | */ |
570 | TAILQ_FOREACH(inph, &udbtable.inpt_queue, inph_queue) { |
571 | inp = (struct inpcb *)inph; |
572 | if (inp->inp_af != AF_INET) |
573 | continue; |
574 | |
575 | if (inp->inp_lport != *dport) |
576 | continue; |
577 | if (!in_nullhost(inp->inp_laddr)) { |
578 | if (!in_hosteq(inp->inp_laddr, *dst4)) |
579 | continue; |
580 | } |
581 | if (!in_nullhost(inp->inp_faddr)) { |
582 | if (!in_hosteq(inp->inp_faddr, *src4) || |
583 | inp->inp_fport != *sport) |
584 | continue; |
585 | } |
586 | |
587 | udp4_sendup(m, off, (struct sockaddr *)src, |
588 | inp->inp_socket); |
589 | rcvcnt++; |
590 | |
591 | /* |
592 | * Don't look for additional matches if this one does |
593 | * not have either the SO_REUSEPORT or SO_REUSEADDR |
594 | * socket options set. This heuristic avoids searching |
595 | * through all pcbs in the common case of a non-shared |
596 | * port. It assumes that an application will never |
597 | * clear these options after setting them. |
598 | */ |
599 | if ((inp->inp_socket->so_options & |
600 | (SO_REUSEPORT|SO_REUSEADDR)) == 0) |
601 | break; |
602 | } |
603 | } else { |
604 | /* |
605 | * Locate pcb for datagram. |
606 | */ |
607 | inp = in_pcblookup_connect(&udbtable, *src4, *sport, *dst4, |
608 | *dport, 0); |
609 | if (inp == 0) { |
610 | UDP_STATINC(UDP_STAT_PCBHASHMISS); |
611 | inp = in_pcblookup_bind(&udbtable, *dst4, *dport); |
612 | if (inp == 0) |
613 | return rcvcnt; |
614 | } |
615 | |
616 | #ifdef IPSEC |
617 | /* Handle ESP over UDP */ |
618 | if (inp->inp_flags & INP_ESPINUDP_ALL) { |
619 | struct sockaddr *sa = (struct sockaddr *)src; |
620 | |
621 | switch(udp4_espinudp(mp, off, sa, inp->inp_socket)) { |
622 | case -1: /* Error, m was freeed */ |
623 | rcvcnt = -1; |
624 | goto bad; |
625 | break; |
626 | |
627 | case 1: /* ESP over UDP */ |
628 | rcvcnt++; |
629 | goto bad; |
630 | break; |
631 | |
632 | case 0: /* plain UDP */ |
633 | default: /* Unexpected */ |
634 | /* |
635 | * Normal UDP processing will take place |
636 | * m may have changed. |
637 | */ |
638 | m = *mp; |
639 | break; |
640 | } |
641 | } |
642 | #endif |
643 | |
644 | /* |
645 | * Check the minimum TTL for socket. |
646 | */ |
647 | if (mtod(m, struct ip *)->ip_ttl < inp->inp_ip_minttl) |
648 | goto bad; |
649 | |
650 | udp4_sendup(m, off, (struct sockaddr *)src, inp->inp_socket); |
651 | rcvcnt++; |
652 | } |
653 | |
654 | bad: |
655 | return rcvcnt; |
656 | } |
657 | #endif |
658 | |
659 | #ifdef INET |
660 | /* |
661 | * Notify a udp user of an asynchronous error; |
662 | * just wake up so that he can collect error status. |
663 | */ |
664 | static void |
665 | udp_notify(struct inpcb *inp, int errno) |
666 | { |
667 | inp->inp_socket->so_error = errno; |
668 | sorwakeup(inp->inp_socket); |
669 | sowwakeup(inp->inp_socket); |
670 | } |
671 | |
672 | void * |
673 | udp_ctlinput(int cmd, const struct sockaddr *sa, void *v) |
674 | { |
675 | struct ip *ip = v; |
676 | struct udphdr *uh; |
677 | void (*notify)(struct inpcb *, int) = udp_notify; |
678 | int errno; |
679 | |
680 | if (sa->sa_family != AF_INET |
681 | || sa->sa_len != sizeof(struct sockaddr_in)) |
682 | return NULL; |
683 | if ((unsigned)cmd >= PRC_NCMDS) |
684 | return NULL; |
685 | errno = inetctlerrmap[cmd]; |
686 | if (PRC_IS_REDIRECT(cmd)) |
687 | notify = in_rtchange, ip = 0; |
688 | else if (cmd == PRC_HOSTDEAD) |
689 | ip = 0; |
690 | else if (errno == 0) |
691 | return NULL; |
692 | if (ip) { |
693 | uh = (struct udphdr *)((char *)ip + (ip->ip_hl << 2)); |
694 | in_pcbnotify(&udbtable, satocsin(sa)->sin_addr, uh->uh_dport, |
695 | ip->ip_src, uh->uh_sport, errno, notify); |
696 | |
697 | /* XXX mapped address case */ |
698 | } else |
699 | in_pcbnotifyall(&udbtable, satocsin(sa)->sin_addr, errno, |
700 | notify); |
701 | return NULL; |
702 | } |
703 | |
704 | int |
705 | udp_ctloutput(int op, struct socket *so, struct sockopt *sopt) |
706 | { |
707 | int s; |
708 | int error = 0; |
709 | struct inpcb *inp; |
710 | int family; |
711 | int optval; |
712 | |
713 | family = so->so_proto->pr_domain->dom_family; |
714 | |
715 | s = splsoftnet(); |
716 | switch (family) { |
717 | #ifdef INET |
718 | case PF_INET: |
719 | if (sopt->sopt_level != IPPROTO_UDP) { |
720 | error = ip_ctloutput(op, so, sopt); |
721 | goto end; |
722 | } |
723 | break; |
724 | #endif |
725 | #ifdef INET6 |
726 | case PF_INET6: |
727 | if (sopt->sopt_level != IPPROTO_UDP) { |
728 | error = ip6_ctloutput(op, so, sopt); |
729 | goto end; |
730 | } |
731 | break; |
732 | #endif |
733 | default: |
734 | error = EAFNOSUPPORT; |
735 | goto end; |
736 | } |
737 | |
738 | |
739 | switch (op) { |
740 | case PRCO_SETOPT: |
741 | inp = sotoinpcb(so); |
742 | |
743 | switch (sopt->sopt_name) { |
744 | case UDP_ENCAP: |
745 | error = sockopt_getint(sopt, &optval); |
746 | if (error) |
747 | break; |
748 | |
749 | switch(optval) { |
750 | case 0: |
751 | inp->inp_flags &= ~INP_ESPINUDP_ALL; |
752 | break; |
753 | |
754 | case UDP_ENCAP_ESPINUDP: |
755 | inp->inp_flags &= ~INP_ESPINUDP_ALL; |
756 | inp->inp_flags |= INP_ESPINUDP; |
757 | break; |
758 | |
759 | case UDP_ENCAP_ESPINUDP_NON_IKE: |
760 | inp->inp_flags &= ~INP_ESPINUDP_ALL; |
761 | inp->inp_flags |= INP_ESPINUDP_NON_IKE; |
762 | break; |
763 | default: |
764 | error = EINVAL; |
765 | break; |
766 | } |
767 | break; |
768 | |
769 | default: |
770 | error = ENOPROTOOPT; |
771 | break; |
772 | } |
773 | break; |
774 | |
775 | default: |
776 | error = EINVAL; |
777 | break; |
778 | } |
779 | |
780 | end: |
781 | splx(s); |
782 | return error; |
783 | } |
784 | |
785 | |
786 | int |
787 | udp_output(struct mbuf *m, struct inpcb *inp) |
788 | { |
789 | struct udpiphdr *ui; |
790 | struct route *ro; |
791 | int len = m->m_pkthdr.len; |
792 | int error = 0; |
793 | |
794 | MCLAIM(m, &udp_tx_mowner); |
795 | |
796 | /* |
797 | * Calculate data length and get a mbuf |
798 | * for UDP and IP headers. |
799 | */ |
800 | M_PREPEND(m, sizeof(struct udpiphdr), M_DONTWAIT); |
801 | if (m == 0) { |
802 | error = ENOBUFS; |
803 | goto release; |
804 | } |
805 | |
806 | /* |
807 | * Compute the packet length of the IP header, and |
808 | * punt if the length looks bogus. |
809 | */ |
810 | if (len + sizeof(struct udpiphdr) > IP_MAXPACKET) { |
811 | error = EMSGSIZE; |
812 | goto release; |
813 | } |
814 | |
815 | /* |
816 | * Fill in mbuf with extended UDP header |
817 | * and addresses and length put into network format. |
818 | */ |
819 | ui = mtod(m, struct udpiphdr *); |
820 | ui->ui_pr = IPPROTO_UDP; |
821 | ui->ui_src = inp->inp_laddr; |
822 | ui->ui_dst = inp->inp_faddr; |
823 | ui->ui_sport = inp->inp_lport; |
824 | ui->ui_dport = inp->inp_fport; |
825 | ui->ui_ulen = htons((u_int16_t)len + sizeof(struct udphdr)); |
826 | |
827 | ro = &inp->inp_route; |
828 | |
829 | /* |
830 | * Set up checksum and output datagram. |
831 | */ |
832 | if (udpcksum) { |
833 | /* |
834 | * XXX Cache pseudo-header checksum part for |
835 | * XXX "connected" UDP sockets. |
836 | */ |
837 | ui->ui_sum = in_cksum_phdr(ui->ui_src.s_addr, |
838 | ui->ui_dst.s_addr, htons((u_int16_t)len + |
839 | sizeof(struct udphdr) + IPPROTO_UDP)); |
840 | m->m_pkthdr.csum_flags = M_CSUM_UDPv4; |
841 | m->m_pkthdr.csum_data = offsetof(struct udphdr, uh_sum); |
842 | } else |
843 | ui->ui_sum = 0; |
844 | ((struct ip *)ui)->ip_len = htons(sizeof (struct udpiphdr) + len); |
845 | ((struct ip *)ui)->ip_ttl = inp->inp_ip.ip_ttl; /* XXX */ |
846 | ((struct ip *)ui)->ip_tos = inp->inp_ip.ip_tos; /* XXX */ |
847 | UDP_STATINC(UDP_STAT_OPACKETS); |
848 | |
849 | return (ip_output(m, inp->inp_options, ro, |
850 | inp->inp_socket->so_options & (SO_DONTROUTE | SO_BROADCAST), |
851 | inp->inp_moptions, inp->inp_socket)); |
852 | |
853 | release: |
854 | m_freem(m); |
855 | return (error); |
856 | } |
857 | |
858 | static int |
859 | udp_attach(struct socket *so, int proto) |
860 | { |
861 | struct inpcb *inp; |
862 | int error; |
863 | |
864 | KASSERT(sotoinpcb(so) == NULL); |
865 | |
866 | /* Assign the lock (must happen even if we will error out). */ |
867 | sosetlock(so); |
868 | |
869 | #ifdef MBUFTRACE |
870 | so->so_mowner = &udp_mowner; |
871 | so->so_rcv.sb_mowner = &udp_rx_mowner; |
872 | so->so_snd.sb_mowner = &udp_tx_mowner; |
873 | #endif |
874 | if (so->so_snd.sb_hiwat == 0 || so->so_rcv.sb_hiwat == 0) { |
875 | error = soreserve(so, udp_sendspace, udp_recvspace); |
876 | if (error) { |
877 | return error; |
878 | } |
879 | } |
880 | |
881 | error = in_pcballoc(so, &udbtable); |
882 | if (error) { |
883 | return error; |
884 | } |
885 | inp = sotoinpcb(so); |
886 | inp->inp_ip.ip_ttl = ip_defttl; |
887 | KASSERT(solocked(so)); |
888 | |
889 | return error; |
890 | } |
891 | |
892 | static void |
893 | udp_detach(struct socket *so) |
894 | { |
895 | struct inpcb *inp; |
896 | |
897 | KASSERT(solocked(so)); |
898 | inp = sotoinpcb(so); |
899 | KASSERT(inp != NULL); |
900 | in_pcbdetach(inp); |
901 | } |
902 | |
903 | static int |
904 | udp_accept(struct socket *so, struct sockaddr *nam) |
905 | { |
906 | KASSERT(solocked(so)); |
907 | |
908 | panic("udp_accept" ); |
909 | |
910 | return EOPNOTSUPP; |
911 | } |
912 | |
913 | static int |
914 | udp_bind(struct socket *so, struct sockaddr *nam, struct lwp *l) |
915 | { |
916 | struct inpcb *inp = sotoinpcb(so); |
917 | struct sockaddr_in *sin = (struct sockaddr_in *)nam; |
918 | int error = 0; |
919 | int s; |
920 | |
921 | KASSERT(solocked(so)); |
922 | KASSERT(inp != NULL); |
923 | KASSERT(nam != NULL); |
924 | |
925 | s = splsoftnet(); |
926 | error = in_pcbbind(inp, sin, l); |
927 | splx(s); |
928 | |
929 | return error; |
930 | } |
931 | |
932 | static int |
933 | udp_listen(struct socket *so, struct lwp *l) |
934 | { |
935 | KASSERT(solocked(so)); |
936 | |
937 | return EOPNOTSUPP; |
938 | } |
939 | |
940 | static int |
941 | udp_connect(struct socket *so, struct sockaddr *nam, struct lwp *l) |
942 | { |
943 | struct inpcb *inp = sotoinpcb(so); |
944 | int error = 0; |
945 | int s; |
946 | |
947 | KASSERT(solocked(so)); |
948 | KASSERT(inp != NULL); |
949 | KASSERT(nam != NULL); |
950 | |
951 | s = splsoftnet(); |
952 | error = in_pcbconnect(inp, (struct sockaddr_in *)nam, l); |
953 | if (! error) |
954 | soisconnected(so); |
955 | splx(s); |
956 | return error; |
957 | } |
958 | |
959 | static int |
960 | udp_connect2(struct socket *so, struct socket *so2) |
961 | { |
962 | KASSERT(solocked(so)); |
963 | |
964 | return EOPNOTSUPP; |
965 | } |
966 | |
967 | static int |
968 | udp_disconnect(struct socket *so) |
969 | { |
970 | struct inpcb *inp = sotoinpcb(so); |
971 | int s; |
972 | |
973 | KASSERT(solocked(so)); |
974 | KASSERT(inp != NULL); |
975 | |
976 | s = splsoftnet(); |
977 | /*soisdisconnected(so);*/ |
978 | so->so_state &= ~SS_ISCONNECTED; /* XXX */ |
979 | in_pcbdisconnect(inp); |
980 | inp->inp_laddr = zeroin_addr; /* XXX */ |
981 | in_pcbstate(inp, INP_BOUND); /* XXX */ |
982 | splx(s); |
983 | |
984 | return 0; |
985 | } |
986 | |
987 | static int |
988 | udp_shutdown(struct socket *so) |
989 | { |
990 | int s; |
991 | |
992 | KASSERT(solocked(so)); |
993 | |
994 | s = splsoftnet(); |
995 | socantsendmore(so); |
996 | splx(s); |
997 | |
998 | return 0; |
999 | } |
1000 | |
1001 | static int |
1002 | udp_abort(struct socket *so) |
1003 | { |
1004 | KASSERT(solocked(so)); |
1005 | |
1006 | panic("udp_abort" ); |
1007 | |
1008 | return EOPNOTSUPP; |
1009 | } |
1010 | |
1011 | static int |
1012 | udp_ioctl(struct socket *so, u_long cmd, void *nam, struct ifnet *ifp) |
1013 | { |
1014 | return in_control(so, cmd, nam, ifp); |
1015 | } |
1016 | |
1017 | static int |
1018 | udp_stat(struct socket *so, struct stat *ub) |
1019 | { |
1020 | KASSERT(solocked(so)); |
1021 | |
1022 | /* stat: don't bother with a blocksize. */ |
1023 | return 0; |
1024 | } |
1025 | |
1026 | static int |
1027 | udp_peeraddr(struct socket *so, struct sockaddr *nam) |
1028 | { |
1029 | int s; |
1030 | |
1031 | KASSERT(solocked(so)); |
1032 | KASSERT(sotoinpcb(so) != NULL); |
1033 | KASSERT(nam != NULL); |
1034 | |
1035 | s = splsoftnet(); |
1036 | in_setpeeraddr(sotoinpcb(so), (struct sockaddr_in *)nam); |
1037 | splx(s); |
1038 | |
1039 | return 0; |
1040 | } |
1041 | |
1042 | static int |
1043 | udp_sockaddr(struct socket *so, struct sockaddr *nam) |
1044 | { |
1045 | int s; |
1046 | |
1047 | KASSERT(solocked(so)); |
1048 | KASSERT(sotoinpcb(so) != NULL); |
1049 | KASSERT(nam != NULL); |
1050 | |
1051 | s = splsoftnet(); |
1052 | in_setsockaddr(sotoinpcb(so), (struct sockaddr_in *)nam); |
1053 | splx(s); |
1054 | |
1055 | return 0; |
1056 | } |
1057 | |
1058 | static int |
1059 | udp_rcvd(struct socket *so, int flags, struct lwp *l) |
1060 | { |
1061 | KASSERT(solocked(so)); |
1062 | |
1063 | return EOPNOTSUPP; |
1064 | } |
1065 | |
1066 | static int |
1067 | udp_recvoob(struct socket *so, struct mbuf *m, int flags) |
1068 | { |
1069 | KASSERT(solocked(so)); |
1070 | |
1071 | return EOPNOTSUPP; |
1072 | } |
1073 | |
1074 | static int |
1075 | udp_send(struct socket *so, struct mbuf *m, struct sockaddr *nam, |
1076 | struct mbuf *control, struct lwp *l) |
1077 | { |
1078 | struct inpcb *inp = sotoinpcb(so); |
1079 | int error = 0; |
1080 | struct in_addr laddr; /* XXX */ |
1081 | int s; |
1082 | |
1083 | KASSERT(solocked(so)); |
1084 | KASSERT(inp != NULL); |
1085 | KASSERT(m != NULL); |
1086 | |
1087 | if (control && control->m_len) { |
1088 | m_freem(control); |
1089 | m_freem(m); |
1090 | return EINVAL; |
1091 | } |
1092 | |
1093 | memset(&laddr, 0, sizeof laddr); |
1094 | |
1095 | s = splsoftnet(); |
1096 | if (nam) { |
1097 | laddr = inp->inp_laddr; /* XXX */ |
1098 | if ((so->so_state & SS_ISCONNECTED) != 0) { |
1099 | error = EISCONN; |
1100 | goto die; |
1101 | } |
1102 | error = in_pcbconnect(inp, (struct sockaddr_in *)nam, l); |
1103 | if (error) |
1104 | goto die; |
1105 | } else { |
1106 | if ((so->so_state & SS_ISCONNECTED) == 0) { |
1107 | error = ENOTCONN; |
1108 | goto die; |
1109 | } |
1110 | } |
1111 | error = udp_output(m, inp); |
1112 | m = NULL; |
1113 | if (nam) { |
1114 | in_pcbdisconnect(inp); |
1115 | inp->inp_laddr = laddr; /* XXX */ |
1116 | in_pcbstate(inp, INP_BOUND); /* XXX */ |
1117 | } |
1118 | die: |
1119 | if (m) |
1120 | m_freem(m); |
1121 | |
1122 | splx(s); |
1123 | return error; |
1124 | } |
1125 | |
1126 | static int |
1127 | udp_sendoob(struct socket *so, struct mbuf *m, struct mbuf *control) |
1128 | { |
1129 | KASSERT(solocked(so)); |
1130 | |
1131 | m_freem(m); |
1132 | m_freem(control); |
1133 | |
1134 | return EOPNOTSUPP; |
1135 | } |
1136 | |
1137 | static int |
1138 | udp_purgeif(struct socket *so, struct ifnet *ifp) |
1139 | { |
1140 | int s; |
1141 | |
1142 | s = splsoftnet(); |
1143 | #ifndef NET_MPSAFE |
1144 | mutex_enter(softnet_lock); |
1145 | #endif |
1146 | in_pcbpurgeif0(&udbtable, ifp); |
1147 | in_purgeif(ifp); |
1148 | in_pcbpurgeif(&udbtable, ifp); |
1149 | #ifndef NET_MPSAFE |
1150 | mutex_exit(softnet_lock); |
1151 | #endif |
1152 | splx(s); |
1153 | |
1154 | return 0; |
1155 | } |
1156 | |
1157 | static int |
1158 | sysctl_net_inet_udp_stats(SYSCTLFN_ARGS) |
1159 | { |
1160 | |
1161 | return (NETSTAT_SYSCTL(udpstat_percpu, UDP_NSTATS)); |
1162 | } |
1163 | |
1164 | /* |
1165 | * Sysctl for udp variables. |
1166 | */ |
1167 | static void |
1168 | sysctl_net_inet_udp_setup(struct sysctllog **clog) |
1169 | { |
1170 | |
1171 | sysctl_createv(clog, 0, NULL, NULL, |
1172 | CTLFLAG_PERMANENT, |
1173 | CTLTYPE_NODE, "inet" , NULL, |
1174 | NULL, 0, NULL, 0, |
1175 | CTL_NET, PF_INET, CTL_EOL); |
1176 | sysctl_createv(clog, 0, NULL, NULL, |
1177 | CTLFLAG_PERMANENT, |
1178 | CTLTYPE_NODE, "udp" , |
1179 | SYSCTL_DESCR("UDPv4 related settings" ), |
1180 | NULL, 0, NULL, 0, |
1181 | CTL_NET, PF_INET, IPPROTO_UDP, CTL_EOL); |
1182 | |
1183 | sysctl_createv(clog, 0, NULL, NULL, |
1184 | CTLFLAG_PERMANENT|CTLFLAG_READWRITE, |
1185 | CTLTYPE_INT, "checksum" , |
1186 | SYSCTL_DESCR("Compute UDP checksums" ), |
1187 | NULL, 0, &udpcksum, 0, |
1188 | CTL_NET, PF_INET, IPPROTO_UDP, UDPCTL_CHECKSUM, |
1189 | CTL_EOL); |
1190 | sysctl_createv(clog, 0, NULL, NULL, |
1191 | CTLFLAG_PERMANENT|CTLFLAG_READWRITE, |
1192 | CTLTYPE_INT, "sendspace" , |
1193 | SYSCTL_DESCR("Default UDP send buffer size" ), |
1194 | NULL, 0, &udp_sendspace, 0, |
1195 | CTL_NET, PF_INET, IPPROTO_UDP, UDPCTL_SENDSPACE, |
1196 | CTL_EOL); |
1197 | sysctl_createv(clog, 0, NULL, NULL, |
1198 | CTLFLAG_PERMANENT|CTLFLAG_READWRITE, |
1199 | CTLTYPE_INT, "recvspace" , |
1200 | SYSCTL_DESCR("Default UDP receive buffer size" ), |
1201 | NULL, 0, &udp_recvspace, 0, |
1202 | CTL_NET, PF_INET, IPPROTO_UDP, UDPCTL_RECVSPACE, |
1203 | CTL_EOL); |
1204 | sysctl_createv(clog, 0, NULL, NULL, |
1205 | CTLFLAG_PERMANENT|CTLFLAG_READWRITE, |
1206 | CTLTYPE_INT, "do_loopback_cksum" , |
1207 | SYSCTL_DESCR("Perform UDP checksum on loopback" ), |
1208 | NULL, 0, &udp_do_loopback_cksum, 0, |
1209 | CTL_NET, PF_INET, IPPROTO_UDP, UDPCTL_LOOPBACKCKSUM, |
1210 | CTL_EOL); |
1211 | sysctl_createv(clog, 0, NULL, NULL, |
1212 | CTLFLAG_PERMANENT, |
1213 | CTLTYPE_STRUCT, "pcblist" , |
1214 | SYSCTL_DESCR("UDP protocol control block list" ), |
1215 | sysctl_inpcblist, 0, &udbtable, 0, |
1216 | CTL_NET, PF_INET, IPPROTO_UDP, CTL_CREATE, |
1217 | CTL_EOL); |
1218 | sysctl_createv(clog, 0, NULL, NULL, |
1219 | CTLFLAG_PERMANENT, |
1220 | CTLTYPE_STRUCT, "stats" , |
1221 | SYSCTL_DESCR("UDP statistics" ), |
1222 | sysctl_net_inet_udp_stats, 0, NULL, 0, |
1223 | CTL_NET, PF_INET, IPPROTO_UDP, UDPCTL_STATS, |
1224 | CTL_EOL); |
1225 | } |
1226 | #endif |
1227 | |
1228 | void |
1229 | udp_statinc(u_int stat) |
1230 | { |
1231 | |
1232 | KASSERT(stat < UDP_NSTATS); |
1233 | UDP_STATINC(stat); |
1234 | } |
1235 | |
1236 | #if defined(INET) && defined(IPSEC) |
1237 | /* |
1238 | * Returns: |
1239 | * 1 if the packet was processed |
1240 | * 0 if normal UDP processing should take place |
1241 | * -1 if an error occurent and m was freed |
1242 | */ |
1243 | static int |
1244 | udp4_espinudp(struct mbuf **mp, int off, struct sockaddr *src, |
1245 | struct socket *so) |
1246 | { |
1247 | size_t len; |
1248 | void *data; |
1249 | struct inpcb *inp; |
1250 | size_t skip = 0; |
1251 | size_t minlen; |
1252 | size_t iphdrlen; |
1253 | struct ip *ip; |
1254 | struct m_tag *tag; |
1255 | struct udphdr *udphdr; |
1256 | u_int16_t sport, dport; |
1257 | struct mbuf *m = *mp; |
1258 | |
1259 | /* |
1260 | * Collapse the mbuf chain if the first mbuf is too short |
1261 | * The longest case is: UDP + non ESP marker + ESP |
1262 | */ |
1263 | minlen = off + sizeof(u_int64_t) + sizeof(struct esp); |
1264 | if (minlen > m->m_pkthdr.len) |
1265 | minlen = m->m_pkthdr.len; |
1266 | |
1267 | if (m->m_len < minlen) { |
1268 | if ((*mp = m_pullup(m, minlen)) == NULL) { |
1269 | printf("udp4_espinudp: m_pullup failed\n" ); |
1270 | return -1; |
1271 | } |
1272 | m = *mp; |
1273 | } |
1274 | |
1275 | len = m->m_len - off; |
1276 | data = mtod(m, char *) + off; |
1277 | inp = sotoinpcb(so); |
1278 | |
1279 | /* Ignore keepalive packets */ |
1280 | if ((len == 1) && (*(unsigned char *)data == 0xff)) { |
1281 | m_free(m); |
1282 | *mp = NULL; /* avoid any further processiong by caller ... */ |
1283 | return 1; |
1284 | } |
1285 | |
1286 | /* |
1287 | * Check that the payload is long enough to hold |
1288 | * an ESP header and compute the length of encapsulation |
1289 | * header to remove |
1290 | */ |
1291 | if (inp->inp_flags & INP_ESPINUDP) { |
1292 | u_int32_t *st = (u_int32_t *)data; |
1293 | |
1294 | if ((len <= sizeof(struct esp)) || (*st == 0)) |
1295 | return 0; /* Normal UDP processing */ |
1296 | |
1297 | skip = sizeof(struct udphdr); |
1298 | } |
1299 | |
1300 | if (inp->inp_flags & INP_ESPINUDP_NON_IKE) { |
1301 | u_int32_t *st = (u_int32_t *)data; |
1302 | |
1303 | if ((len <= sizeof(u_int64_t) + sizeof(struct esp)) |
1304 | || ((st[0] | st[1]) != 0)) |
1305 | return 0; /* Normal UDP processing */ |
1306 | |
1307 | skip = sizeof(struct udphdr) + sizeof(u_int64_t); |
1308 | } |
1309 | |
1310 | /* |
1311 | * Get the UDP ports. They are handled in network |
1312 | * order everywhere in IPSEC_NAT_T code. |
1313 | */ |
1314 | udphdr = (struct udphdr *)((char *)data - skip); |
1315 | sport = udphdr->uh_sport; |
1316 | dport = udphdr->uh_dport; |
1317 | |
1318 | /* |
1319 | * Remove the UDP header (and possibly the non ESP marker) |
1320 | * IP header lendth is iphdrlen |
1321 | * Before: |
1322 | * <--- off ---> |
1323 | * +----+------+-----+ |
1324 | * | IP | UDP | ESP | |
1325 | * +----+------+-----+ |
1326 | * <-skip-> |
1327 | * After: |
1328 | * +----+-----+ |
1329 | * | IP | ESP | |
1330 | * +----+-----+ |
1331 | * <-skip-> |
1332 | */ |
1333 | iphdrlen = off - sizeof(struct udphdr); |
1334 | memmove(mtod(m, char *) + skip, mtod(m, void *), iphdrlen); |
1335 | m_adj(m, skip); |
1336 | |
1337 | ip = mtod(m, struct ip *); |
1338 | ip->ip_len = htons(ntohs(ip->ip_len) - skip); |
1339 | ip->ip_p = IPPROTO_ESP; |
1340 | |
1341 | /* |
1342 | * We have modified the packet - it is now ESP, so we should not |
1343 | * return to UDP processing ... |
1344 | * |
1345 | * Add a PACKET_TAG_IPSEC_NAT_T_PORT tag to remember |
1346 | * the source UDP port. This is required if we want |
1347 | * to select the right SPD for multiple hosts behind |
1348 | * same NAT |
1349 | */ |
1350 | if ((tag = m_tag_get(PACKET_TAG_IPSEC_NAT_T_PORTS, |
1351 | sizeof(sport) + sizeof(dport), M_DONTWAIT)) == NULL) { |
1352 | printf("udp4_espinudp: m_tag_get failed\n" ); |
1353 | m_freem(m); |
1354 | return -1; |
1355 | } |
1356 | ((u_int16_t *)(tag + 1))[0] = sport; |
1357 | ((u_int16_t *)(tag + 1))[1] = dport; |
1358 | m_tag_prepend(m, tag); |
1359 | |
1360 | if (ipsec_used) |
1361 | ipsec4_common_input(m, iphdrlen, IPPROTO_ESP); |
1362 | /* XXX: else */ |
1363 | |
1364 | /* We handled it, it shouldn't be handled by UDP */ |
1365 | *mp = NULL; /* avoid free by caller ... */ |
1366 | return 1; |
1367 | } |
1368 | #endif |
1369 | |
1370 | PR_WRAP_USRREQS(udp) |
1371 | #define udp_attach udp_attach_wrapper |
1372 | #define udp_detach udp_detach_wrapper |
1373 | #define udp_accept udp_accept_wrapper |
1374 | #define udp_bind udp_bind_wrapper |
1375 | #define udp_listen udp_listen_wrapper |
1376 | #define udp_connect udp_connect_wrapper |
1377 | #define udp_connect2 udp_connect2_wrapper |
1378 | #define udp_disconnect udp_disconnect_wrapper |
1379 | #define udp_shutdown udp_shutdown_wrapper |
1380 | #define udp_abort udp_abort_wrapper |
1381 | #define udp_ioctl udp_ioctl_wrapper |
1382 | #define udp_stat udp_stat_wrapper |
1383 | #define udp_peeraddr udp_peeraddr_wrapper |
1384 | #define udp_sockaddr udp_sockaddr_wrapper |
1385 | #define udp_rcvd udp_rcvd_wrapper |
1386 | #define udp_recvoob udp_recvoob_wrapper |
1387 | #define udp_send udp_send_wrapper |
1388 | #define udp_sendoob udp_sendoob_wrapper |
1389 | #define udp_purgeif udp_purgeif_wrapper |
1390 | |
1391 | const struct pr_usrreqs udp_usrreqs = { |
1392 | .pr_attach = udp_attach, |
1393 | .pr_detach = udp_detach, |
1394 | .pr_accept = udp_accept, |
1395 | .pr_bind = udp_bind, |
1396 | .pr_listen = udp_listen, |
1397 | .pr_connect = udp_connect, |
1398 | .pr_connect2 = udp_connect2, |
1399 | .pr_disconnect = udp_disconnect, |
1400 | .pr_shutdown = udp_shutdown, |
1401 | .pr_abort = udp_abort, |
1402 | .pr_ioctl = udp_ioctl, |
1403 | .pr_stat = udp_stat, |
1404 | .pr_peeraddr = udp_peeraddr, |
1405 | .pr_sockaddr = udp_sockaddr, |
1406 | .pr_rcvd = udp_rcvd, |
1407 | .pr_recvoob = udp_recvoob, |
1408 | .pr_send = udp_send, |
1409 | .pr_sendoob = udp_sendoob, |
1410 | .pr_purgeif = udp_purgeif, |
1411 | }; |
1412 | |