1 /*-
2 * SPDX-License-Identifier: BSD-3-Clause
3 *
4 * Copyright (c) 1982, 1986, 1988, 1990, 1993, 1995
5 * The Regents of the University of California.
6 * Copyright (c) 2008 Robert N. M. Watson
7 * Copyright (c) 2010-2011 Juniper Networks, Inc.
8 * Copyright (c) 2014 Kevin Lo
9 * All rights reserved.
10 *
11 * Portions of this software were developed by Robert N. M. Watson under
12 * contract to Juniper Networks, Inc.
13 *
14 * Redistribution and use in source and binary forms, with or without
15 * modification, are permitted provided that the following conditions
16 * are met:
17 * 1. Redistributions of source code must retain the above copyright
18 * notice, this list of conditions and the following disclaimer.
19 * 2. Redistributions in binary form must reproduce the above copyright
20 * notice, this list of conditions and the following disclaimer in the
21 * documentation and/or other materials provided with the distribution.
22 * 3. Neither the name of the University nor the names of its contributors
23 * may be used to endorse or promote products derived from this software
24 * without specific prior written permission.
25 *
26 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
27 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
28 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
29 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
30 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
31 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
32 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
33 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
34 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
35 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36 * SUCH DAMAGE.
37 */
38
39 #include <sys/cdefs.h>
40 #include "opt_inet.h"
41 #include "opt_inet6.h"
42 #include "opt_ipsec.h"
43 #include "opt_route.h"
44 #include "opt_rss.h"
45
46 #include <sys/param.h>
47 #include <sys/domain.h>
48 #include <sys/eventhandler.h>
49 #include <sys/jail.h>
50 #include <sys/kernel.h>
51 #include <sys/lock.h>
52 #include <sys/malloc.h>
53 #include <sys/mbuf.h>
54 #include <sys/priv.h>
55 #include <sys/proc.h>
56 #include <sys/protosw.h>
57 #include <sys/sdt.h>
58 #include <sys/signalvar.h>
59 #include <sys/socket.h>
60 #include <sys/socketvar.h>
61 #include <sys/sx.h>
62 #include <sys/sysctl.h>
63 #include <sys/syslog.h>
64 #include <sys/systm.h>
65
66 #include <vm/uma.h>
67
68 #include <net/if.h>
69 #include <net/if_var.h>
70 #include <net/route.h>
71 #include <net/route/nhop.h>
72 #include <net/rss_config.h>
73
74 #include <netinet/in.h>
75 #include <netinet/in_kdtrace.h>
76 #include <netinet/in_fib.h>
77 #include <netinet/in_pcb.h>
78 #include <netinet/in_systm.h>
79 #include <netinet/in_var.h>
80 #include <netinet/ip.h>
81 #ifdef INET6
82 #include <netinet/ip6.h>
83 #endif
84 #include <netinet/ip_icmp.h>
85 #include <netinet/icmp_var.h>
86 #include <netinet/ip_var.h>
87 #include <netinet/ip_options.h>
88 #ifdef INET6
89 #include <netinet6/ip6_var.h>
90 #endif
91 #include <netinet/udp.h>
92 #include <netinet/udp_var.h>
93 #include <netinet/udplite.h>
94 #include <netinet/in_rss.h>
95
96 #include <netipsec/ipsec_support.h>
97
98 #include <machine/in_cksum.h>
99
100 #include <security/mac/mac_framework.h>
101
102 /*
103 * UDP and UDP-Lite protocols implementation.
104 * Per RFC 768, August, 1980.
105 * Per RFC 3828, July, 2004.
106 */
107
108 VNET_DEFINE(int, udp_bind_all_fibs) = 1;
109 SYSCTL_INT(_net_inet_udp, OID_AUTO, bind_all_fibs, CTLFLAG_VNET | CTLFLAG_RDTUN,
110 &VNET_NAME(udp_bind_all_fibs), 0,
111 "Bound sockets receive traffic from all FIBs");
112
113 /*
114 * BSD 4.2 defaulted the udp checksum to be off. Turning off udp checksums
115 * removes the only data integrity mechanism for packets and malformed
116 * packets that would otherwise be discarded due to bad checksums, and may
117 * cause problems (especially for NFS data blocks).
118 */
119 VNET_DEFINE(int, udp_cksum) = 1;
120 SYSCTL_INT(_net_inet_udp, UDPCTL_CHECKSUM, checksum, CTLFLAG_VNET | CTLFLAG_RW,
121 &VNET_NAME(udp_cksum), 0, "compute udp checksum");
122
123 VNET_DEFINE(int, udp_log_in_vain) = 0;
124 SYSCTL_INT(_net_inet_udp, OID_AUTO, log_in_vain, CTLFLAG_VNET | CTLFLAG_RW,
125 &VNET_NAME(udp_log_in_vain), 0, "Log all incoming UDP packets");
126
127 VNET_DEFINE(int, udp_blackhole) = 0;
128 SYSCTL_INT(_net_inet_udp, OID_AUTO, blackhole, CTLFLAG_VNET | CTLFLAG_RW,
129 &VNET_NAME(udp_blackhole), 0,
130 "Do not send port unreachables for refused connects");
131 VNET_DEFINE(bool, udp_blackhole_local) = false;
132 SYSCTL_BOOL(_net_inet_udp, OID_AUTO, blackhole_local, CTLFLAG_VNET |
133 CTLFLAG_RW, &VNET_NAME(udp_blackhole_local), false,
134 "Enforce net.inet.udp.blackhole for locally originated packets");
135
136 u_long udp_sendspace = 9216; /* really max datagram size */
137 SYSCTL_ULONG(_net_inet_udp, UDPCTL_MAXDGRAM, maxdgram, CTLFLAG_RW,
138 &udp_sendspace, 0, "Maximum outgoing UDP datagram size");
139
140 u_long udp_recvspace = 40 * (1024 +
141 #ifdef INET6
142 sizeof(struct sockaddr_in6)
143 #else
144 sizeof(struct sockaddr_in)
145 #endif
146 ); /* 40 1K datagrams */
147
148 SYSCTL_ULONG(_net_inet_udp, UDPCTL_RECVSPACE, recvspace, CTLFLAG_RW,
149 &udp_recvspace, 0, "Maximum space for incoming UDP datagrams");
150
151 VNET_DEFINE(struct inpcbinfo, udbinfo);
152 VNET_DEFINE(struct inpcbinfo, ulitecbinfo);
153
154 #ifndef UDBHASHSIZE
155 #define UDBHASHSIZE 128
156 #endif
157
158 VNET_PCPUSTAT_DEFINE(struct udpstat, udpstat); /* from udp_var.h */
159 VNET_PCPUSTAT_SYSINIT(udpstat);
160 SYSCTL_VNET_PCPUSTAT(_net_inet_udp, UDPCTL_STATS, stats, struct udpstat,
161 udpstat, "UDP statistics (struct udpstat, netinet/udp_var.h)");
162
163 #ifdef VIMAGE
164 VNET_PCPUSTAT_SYSUNINIT(udpstat);
165 #endif /* VIMAGE */
166 #ifdef INET
167 static void udp_detach(struct socket *so);
168 #endif
169
170 INPCBSTORAGE_DEFINE(udpcbstor, udpcb, "udpinp", "udp_inpcb", "udp", "udphash");
171 INPCBSTORAGE_DEFINE(udplitecbstor, udpcb, "udpliteinp", "udplite_inpcb",
172 "udplite", "udplitehash");
173
174 static void
udp_vnet_init(void * arg __unused)175 udp_vnet_init(void *arg __unused)
176 {
177
178 /*
179 * For now default to 2-tuple UDP hashing - until the fragment
180 * reassembly code can also update the flowid.
181 *
182 * Once we can calculate the flowid that way and re-establish
183 * a 4-tuple, flip this to 4-tuple.
184 */
185 in_pcbinfo_init(&V_udbinfo, &udpcbstor, UDBHASHSIZE, UDBHASHSIZE);
186 /* Additional pcbinfo for UDP-Lite */
187 in_pcbinfo_init(&V_ulitecbinfo, &udplitecbstor, UDBHASHSIZE,
188 UDBHASHSIZE);
189 }
190 VNET_SYSINIT(udp_vnet_init, SI_SUB_PROTO_DOMAIN, SI_ORDER_FOURTH,
191 udp_vnet_init, NULL);
192
193 /*
194 * Kernel module interface for updating udpstat. The argument is an index
195 * into udpstat treated as an array of u_long. While this encodes the
196 * general layout of udpstat into the caller, it doesn't encode its location,
197 * so that future changes to add, for example, per-CPU stats support won't
198 * cause binary compatibility problems for kernel modules.
199 */
200 void
kmod_udpstat_inc(int statnum)201 kmod_udpstat_inc(int statnum)
202 {
203
204 counter_u64_add(VNET(udpstat)[statnum], 1);
205 }
206
207 #ifdef VIMAGE
208 static void
udp_destroy(void * unused __unused)209 udp_destroy(void *unused __unused)
210 {
211
212 in_pcbinfo_destroy(&V_udbinfo);
213 in_pcbinfo_destroy(&V_ulitecbinfo);
214 }
215 VNET_SYSUNINIT(udp, SI_SUB_PROTO_DOMAIN, SI_ORDER_FOURTH, udp_destroy, NULL);
216 #endif
217
218 #ifdef INET
219 /*
220 * Subroutine of udp_input(), which appends the provided mbuf chain to the
221 * passed pcb/socket. The caller must provide a sockaddr_in via udp_in that
222 * contains the source address. If the socket ends up being an IPv6 socket,
223 * udp_append() will convert to a sockaddr_in6 before passing the address
224 * into the socket code.
225 *
226 * In the normal case udp_append() will return 0, indicating that you
227 * must unlock the inp. However if a tunneling protocol is in place we increment
228 * the inpcb refcnt and unlock the inp, on return from the tunneling protocol we
229 * then decrement the reference count. If the inp_rele returns 1, indicating the
230 * inp is gone, we return that to the caller to tell them *not* to unlock
231 * the inp. In the case of multi-cast this will cause the distribution
232 * to stop (though most tunneling protocols known currently do *not* use
233 * multicast).
234 */
235 static int
udp_append(struct inpcb * inp,struct ip * ip,struct mbuf * n,int off,struct sockaddr_in * udp_in)236 udp_append(struct inpcb *inp, struct ip *ip, struct mbuf *n, int off,
237 struct sockaddr_in *udp_in)
238 {
239 struct sockaddr *append_sa;
240 struct socket *so;
241 struct mbuf *tmpopts, *opts = NULL;
242 #ifdef INET6
243 struct sockaddr_in6 udp_in6;
244 #endif
245 struct udpcb *up;
246 bool filtered;
247
248 INP_LOCK_ASSERT(inp);
249
250 /*
251 * Engage the tunneling protocol.
252 */
253 up = intoudpcb(inp);
254 if (up->u_tun_func != NULL) {
255 in_pcbref(inp);
256 INP_RUNLOCK(inp);
257 filtered = (*up->u_tun_func)(n, off, inp, (struct sockaddr *)&udp_in[0],
258 up->u_tun_ctx);
259 INP_RLOCK(inp);
260 if (filtered)
261 return (in_pcbrele_rlocked(inp));
262 }
263
264 off += sizeof(struct udphdr);
265
266 #if defined(IPSEC) || defined(IPSEC_SUPPORT)
267 /* Check AH/ESP integrity. */
268 if (IPSEC_ENABLED(ipv4) &&
269 IPSEC_CHECK_POLICY(ipv4, n, inp) != 0) {
270 m_freem(n);
271 return (0);
272 }
273 if (up->u_flags & UF_ESPINUDP) {/* IPSec UDP encaps. */
274 if (IPSEC_ENABLED(ipv4) &&
275 UDPENCAP_INPUT(ipv4, n, off, AF_INET) != 0)
276 return (0); /* Consumed. */
277 }
278 #endif /* IPSEC */
279 #ifdef MAC
280 if (mac_inpcb_check_deliver(inp, n) != 0) {
281 m_freem(n);
282 return (0);
283 }
284 #endif /* MAC */
285 if (inp->inp_flags & INP_CONTROLOPTS ||
286 inp->inp_socket->so_options & (SO_TIMESTAMP | SO_BINTIME)) {
287 #ifdef INET6
288 if (inp->inp_vflag & INP_IPV6)
289 (void)ip6_savecontrol_v4(inp, n, &opts, NULL);
290 else
291 #endif /* INET6 */
292 ip_savecontrol(inp, &opts, ip, n);
293 }
294 if ((inp->inp_vflag & INP_IPV4) && (inp->inp_flags2 & INP_ORIGDSTADDR)) {
295 tmpopts = sbcreatecontrol(&udp_in[1],
296 sizeof(struct sockaddr_in), IP_ORIGDSTADDR, IPPROTO_IP,
297 M_NOWAIT);
298 if (tmpopts) {
299 if (opts) {
300 tmpopts->m_next = opts;
301 opts = tmpopts;
302 } else
303 opts = tmpopts;
304 }
305 }
306 #ifdef INET6
307 if (inp->inp_vflag & INP_IPV6) {
308 bzero(&udp_in6, sizeof(udp_in6));
309 udp_in6.sin6_len = sizeof(udp_in6);
310 udp_in6.sin6_family = AF_INET6;
311 in6_sin_2_v4mapsin6(&udp_in[0], &udp_in6);
312 append_sa = (struct sockaddr *)&udp_in6;
313 } else
314 #endif /* INET6 */
315 append_sa = (struct sockaddr *)&udp_in[0];
316 m_adj(n, off);
317
318 so = inp->inp_socket;
319 SOCKBUF_LOCK(&so->so_rcv);
320 if (sbappendaddr_locked(&so->so_rcv, append_sa, n, opts) == 0) {
321 soroverflow_locked(so);
322 m_freem(n);
323 if (opts)
324 m_freem(opts);
325 UDPSTAT_INC(udps_fullsock);
326 } else
327 sorwakeup_locked(so);
328 return (0);
329 }
330
331 static bool
udp_multi_match(const struct inpcb * inp,void * v)332 udp_multi_match(const struct inpcb *inp, void *v)
333 {
334 struct ip *ip = v;
335 struct udphdr *uh = (struct udphdr *)(ip + 1);
336
337 if (inp->inp_lport != uh->uh_dport)
338 return (false);
339 #ifdef INET6
340 if ((inp->inp_vflag & INP_IPV4) == 0)
341 return (false);
342 #endif
343 if (inp->inp_laddr.s_addr != INADDR_ANY &&
344 inp->inp_laddr.s_addr != ip->ip_dst.s_addr)
345 return (false);
346 if (inp->inp_faddr.s_addr != INADDR_ANY &&
347 inp->inp_faddr.s_addr != ip->ip_src.s_addr)
348 return (false);
349 if (inp->inp_fport != 0 &&
350 inp->inp_fport != uh->uh_sport)
351 return (false);
352
353 return (true);
354 }
355
356 static int
udp_multi_input(struct mbuf * m,int proto,struct sockaddr_in * udp_in)357 udp_multi_input(struct mbuf *m, int proto, struct sockaddr_in *udp_in)
358 {
359 struct ip *ip = mtod(m, struct ip *);
360 struct inpcb_iterator inpi = INP_ITERATOR(udp_get_inpcbinfo(proto),
361 INPLOOKUP_RLOCKPCB, udp_multi_match, ip);
362 #ifdef KDTRACE_HOOKS
363 struct udphdr *uh = (struct udphdr *)(ip + 1);
364 #endif
365 struct inpcb *inp;
366 struct mbuf *n;
367 int appends = 0, fib;
368
369 MPASS(ip->ip_hl == sizeof(struct ip) >> 2);
370
371 fib = M_GETFIB(m);
372
373 while ((inp = inp_next(&inpi)) != NULL) {
374 /*
375 * XXXRW: Because we weren't holding either the inpcb
376 * or the hash lock when we checked for a match
377 * before, we should probably recheck now that the
378 * inpcb lock is held.
379 */
380
381 if (V_udp_bind_all_fibs == 0 && fib != inp->inp_inc.inc_fibnum)
382 /*
383 * Sockets bound to a specific FIB can only receive
384 * packets from that FIB.
385 */
386 continue;
387
388 /*
389 * Handle socket delivery policy for any-source
390 * and source-specific multicast. [RFC3678]
391 */
392 if (IN_MULTICAST(ntohl(ip->ip_dst.s_addr))) {
393 struct ip_moptions *imo;
394 struct sockaddr_in group;
395 int blocked;
396
397 imo = inp->inp_moptions;
398 if (imo == NULL)
399 continue;
400 bzero(&group, sizeof(struct sockaddr_in));
401 group.sin_len = sizeof(struct sockaddr_in);
402 group.sin_family = AF_INET;
403 group.sin_addr = ip->ip_dst;
404
405 blocked = imo_multi_filter(imo, m->m_pkthdr.rcvif,
406 (struct sockaddr *)&group,
407 (struct sockaddr *)&udp_in[0]);
408 if (blocked != MCAST_PASS) {
409 if (blocked == MCAST_NOTGMEMBER)
410 IPSTAT_INC(ips_notmember);
411 if (blocked == MCAST_NOTSMEMBER ||
412 blocked == MCAST_MUTED)
413 UDPSTAT_INC(udps_filtermcast);
414 continue;
415 }
416 }
417 if ((n = m_copym(m, 0, M_COPYALL, M_NOWAIT)) != NULL) {
418 if (proto == IPPROTO_UDPLITE)
419 UDPLITE_PROBE(receive, NULL, inp, ip, inp, uh);
420 else
421 UDP_PROBE(receive, NULL, inp, ip, inp, uh);
422 if (udp_append(inp, ip, n, sizeof(struct ip), udp_in)) {
423 break;
424 } else
425 appends++;
426 }
427 /*
428 * Don't look for additional matches if this one does
429 * not have either the SO_REUSEPORT or SO_REUSEADDR
430 * socket options set. This heuristic avoids
431 * searching through all pcbs in the common case of a
432 * non-shared port. It assumes that an application
433 * will never clear these options after setting them.
434 */
435 if ((inp->inp_socket->so_options &
436 (SO_REUSEPORT|SO_REUSEPORT_LB|SO_REUSEADDR)) == 0) {
437 INP_RUNLOCK(inp);
438 break;
439 }
440 }
441
442 if (appends == 0) {
443 /*
444 * No matching pcb found; discard datagram. (No need
445 * to send an ICMP Port Unreachable for a broadcast
446 * or multicast datgram.)
447 */
448 UDPSTAT_INC(udps_noport);
449 if (IN_MULTICAST(ntohl(ip->ip_dst.s_addr)))
450 UDPSTAT_INC(udps_noportmcast);
451 else
452 UDPSTAT_INC(udps_noportbcast);
453 }
454 m_freem(m);
455
456 return (IPPROTO_DONE);
457 }
458
459 static int
udp_input(struct mbuf ** mp,int * offp,int proto)460 udp_input(struct mbuf **mp, int *offp, int proto)
461 {
462 struct ip *ip;
463 struct udphdr *uh;
464 struct ifnet *ifp;
465 struct inpcb *inp;
466 uint16_t len, ip_len;
467 struct inpcbinfo *pcbinfo;
468 struct sockaddr_in udp_in[2];
469 struct mbuf *m;
470 struct m_tag *fwd_tag;
471 int cscov_partial, iphlen, lookupflags;
472
473 m = *mp;
474 iphlen = *offp;
475 ifp = m->m_pkthdr.rcvif;
476 *mp = NULL;
477 UDPSTAT_INC(udps_ipackets);
478
479 /*
480 * Strip IP options, if any; should skip this, make available to
481 * user, and use on returned packets, but we don't yet have a way to
482 * check the checksum with options still present.
483 */
484 if (iphlen > sizeof (struct ip)) {
485 ip_stripoptions(m);
486 iphlen = sizeof(struct ip);
487 }
488
489 /*
490 * Get IP and UDP header together in first mbuf.
491 */
492 if (m->m_len < iphlen + sizeof(struct udphdr)) {
493 if ((m = m_pullup(m, iphlen + sizeof(struct udphdr))) == NULL) {
494 UDPSTAT_INC(udps_hdrops);
495 return (IPPROTO_DONE);
496 }
497 }
498 ip = mtod(m, struct ip *);
499 uh = (struct udphdr *)((caddr_t)ip + iphlen);
500 cscov_partial = (proto == IPPROTO_UDPLITE) ? 1 : 0;
501
502 /*
503 * Destination port of 0 is illegal, based on RFC768.
504 */
505 if (uh->uh_dport == 0)
506 goto badunlocked;
507
508 /*
509 * Construct sockaddr format source address. Stuff source address
510 * and datagram in user buffer.
511 */
512 bzero(&udp_in[0], sizeof(struct sockaddr_in) * 2);
513 udp_in[0].sin_len = sizeof(struct sockaddr_in);
514 udp_in[0].sin_family = AF_INET;
515 udp_in[0].sin_port = uh->uh_sport;
516 udp_in[0].sin_addr = ip->ip_src;
517 udp_in[1].sin_len = sizeof(struct sockaddr_in);
518 udp_in[1].sin_family = AF_INET;
519 udp_in[1].sin_port = uh->uh_dport;
520 udp_in[1].sin_addr = ip->ip_dst;
521
522 /*
523 * Make mbuf data length reflect UDP length. If not enough data to
524 * reflect UDP length, drop.
525 */
526 len = ntohs((u_short)uh->uh_ulen);
527 ip_len = ntohs(ip->ip_len) - iphlen;
528 if (proto == IPPROTO_UDPLITE && (len == 0 || len == ip_len)) {
529 /* Zero means checksum over the complete packet. */
530 if (len == 0)
531 len = ip_len;
532 cscov_partial = 0;
533 }
534 if (ip_len != len) {
535 if (len > ip_len || len < sizeof(struct udphdr)) {
536 UDPSTAT_INC(udps_badlen);
537 goto badunlocked;
538 }
539 if (proto == IPPROTO_UDP)
540 m_adj(m, len - ip_len);
541 }
542
543 /*
544 * Checksum extended UDP header and data.
545 */
546 if (uh->uh_sum) {
547 u_short uh_sum;
548
549 if ((m->m_pkthdr.csum_flags & CSUM_DATA_VALID) &&
550 !cscov_partial) {
551 if (m->m_pkthdr.csum_flags & CSUM_PSEUDO_HDR)
552 uh_sum = m->m_pkthdr.csum_data;
553 else
554 uh_sum = in_pseudo(ip->ip_src.s_addr,
555 ip->ip_dst.s_addr, htonl((u_short)len +
556 m->m_pkthdr.csum_data + proto));
557 uh_sum ^= 0xffff;
558 } else {
559 char b[offsetof(struct ipovly, ih_src)];
560 struct ipovly *ipov = (struct ipovly *)ip;
561
562 memcpy(b, ipov, sizeof(b));
563 bzero(ipov, sizeof(ipov->ih_x1));
564 ipov->ih_len = (proto == IPPROTO_UDP) ?
565 uh->uh_ulen : htons(ip_len);
566 uh_sum = in_cksum(m, len + sizeof (struct ip));
567 memcpy(ipov, b, sizeof(b));
568 }
569 if (uh_sum) {
570 UDPSTAT_INC(udps_badsum);
571 m_freem(m);
572 return (IPPROTO_DONE);
573 }
574 } else {
575 if (proto == IPPROTO_UDP) {
576 UDPSTAT_INC(udps_nosum);
577 } else {
578 /* UDPLite requires a checksum */
579 /* XXX: What is the right UDPLite MIB counter here? */
580 m_freem(m);
581 return (IPPROTO_DONE);
582 }
583 }
584
585 if (IN_MULTICAST(ntohl(ip->ip_dst.s_addr)) ||
586 in_ifnet_broadcast(ip->ip_dst, ifp))
587 return (udp_multi_input(m, proto, udp_in));
588
589 pcbinfo = udp_get_inpcbinfo(proto);
590
591 /*
592 * Locate pcb for datagram.
593 */
594 lookupflags = INPLOOKUP_RLOCKPCB |
595 (V_udp_bind_all_fibs ? 0 : INPLOOKUP_FIB);
596
597 /*
598 * Grab info from PACKET_TAG_IPFORWARD tag prepended to the chain.
599 */
600 if ((m->m_flags & M_IP_NEXTHOP) &&
601 (fwd_tag = m_tag_find(m, PACKET_TAG_IPFORWARD, NULL)) != NULL) {
602 struct sockaddr_in *next_hop;
603
604 next_hop = (struct sockaddr_in *)(fwd_tag + 1);
605
606 /*
607 * Transparently forwarded. Pretend to be the destination.
608 * Already got one like this?
609 */
610 inp = in_pcblookup_mbuf(pcbinfo, ip->ip_src, uh->uh_sport,
611 ip->ip_dst, uh->uh_dport, lookupflags, ifp, m);
612 if (!inp) {
613 /*
614 * It's new. Try to find the ambushing socket.
615 * Because we've rewritten the destination address,
616 * any hardware-generated hash is ignored.
617 */
618 inp = in_pcblookup(pcbinfo, ip->ip_src,
619 uh->uh_sport, next_hop->sin_addr,
620 next_hop->sin_port ? htons(next_hop->sin_port) :
621 uh->uh_dport, INPLOOKUP_WILDCARD | lookupflags,
622 ifp);
623 }
624 /* Remove the tag from the packet. We don't need it anymore. */
625 m_tag_delete(m, fwd_tag);
626 m->m_flags &= ~M_IP_NEXTHOP;
627 } else
628 inp = in_pcblookup_mbuf(pcbinfo, ip->ip_src, uh->uh_sport,
629 ip->ip_dst, uh->uh_dport, INPLOOKUP_WILDCARD |
630 lookupflags, ifp, m);
631 if (inp == NULL) {
632 if (V_udp_log_in_vain) {
633 char src[INET_ADDRSTRLEN];
634 char dst[INET_ADDRSTRLEN];
635
636 log(LOG_INFO,
637 "Connection attempt to UDP %s:%d from %s:%d\n",
638 inet_ntoa_r(ip->ip_dst, dst), ntohs(uh->uh_dport),
639 inet_ntoa_r(ip->ip_src, src), ntohs(uh->uh_sport));
640 }
641 if (proto == IPPROTO_UDPLITE)
642 UDPLITE_PROBE(receive, NULL, NULL, ip, NULL, uh);
643 else
644 UDP_PROBE(receive, NULL, NULL, ip, NULL, uh);
645 UDPSTAT_INC(udps_noport);
646 if (m->m_flags & (M_BCAST | M_MCAST)) {
647 UDPSTAT_INC(udps_noportbcast);
648 goto badunlocked;
649 }
650 if (V_udp_blackhole && (V_udp_blackhole_local ||
651 !in_localip(ip->ip_src)))
652 goto badunlocked;
653 if (badport_bandlim(BANDLIM_ICMP_UNREACH) < 0)
654 goto badunlocked;
655 icmp_error(m, ICMP_UNREACH, ICMP_UNREACH_PORT, 0, 0);
656 return (IPPROTO_DONE);
657 }
658
659 /*
660 * Check the minimum TTL for socket.
661 */
662 INP_RLOCK_ASSERT(inp);
663 if (inp->inp_ip_minttl && inp->inp_ip_minttl > ip->ip_ttl) {
664 if (proto == IPPROTO_UDPLITE)
665 UDPLITE_PROBE(receive, NULL, inp, ip, inp, uh);
666 else
667 UDP_PROBE(receive, NULL, inp, ip, inp, uh);
668 INP_RUNLOCK(inp);
669 m_freem(m);
670 return (IPPROTO_DONE);
671 }
672 if (cscov_partial) {
673 struct udpcb *up;
674
675 up = intoudpcb(inp);
676 if (up->u_rxcslen == 0 || up->u_rxcslen > len) {
677 INP_RUNLOCK(inp);
678 m_freem(m);
679 return (IPPROTO_DONE);
680 }
681 }
682
683 if (proto == IPPROTO_UDPLITE)
684 UDPLITE_PROBE(receive, NULL, inp, ip, inp, uh);
685 else
686 UDP_PROBE(receive, NULL, inp, ip, inp, uh);
687 if (udp_append(inp, ip, m, iphlen, udp_in) == 0)
688 INP_RUNLOCK(inp);
689 return (IPPROTO_DONE);
690
691 badunlocked:
692 m_freem(m);
693 return (IPPROTO_DONE);
694 }
695 #endif /* INET */
696
697 /*
698 * Notify a udp user of an asynchronous error; just wake up so that they can
699 * collect error status.
700 */
701 struct inpcb *
udp_notify(struct inpcb * inp,int errno)702 udp_notify(struct inpcb *inp, int errno)
703 {
704
705 INP_WLOCK_ASSERT(inp);
706 if ((errno == EHOSTUNREACH || errno == ENETUNREACH ||
707 errno == EHOSTDOWN) && inp->inp_route.ro_nh) {
708 NH_FREE(inp->inp_route.ro_nh);
709 inp->inp_route.ro_nh = (struct nhop_object *)NULL;
710 }
711
712 inp->inp_socket->so_error = errno;
713 sorwakeup(inp->inp_socket);
714 sowwakeup(inp->inp_socket);
715 return (inp);
716 }
717
718 #ifdef INET
719 static void
udp_common_ctlinput(struct icmp * icmp,struct inpcbinfo * pcbinfo)720 udp_common_ctlinput(struct icmp *icmp, struct inpcbinfo *pcbinfo)
721 {
722 struct ip *ip = &icmp->icmp_ip;
723 struct udphdr *uh;
724 struct inpcb *inp;
725
726 if (icmp_errmap(icmp) == 0)
727 return;
728
729 uh = (struct udphdr *)((caddr_t)ip + (ip->ip_hl << 2));
730 inp = in_pcblookup(pcbinfo, ip->ip_dst, uh->uh_dport, ip->ip_src,
731 uh->uh_sport, INPLOOKUP_WLOCKPCB, NULL);
732 if (inp != NULL) {
733 INP_WLOCK_ASSERT(inp);
734 if (inp->inp_socket != NULL)
735 udp_notify(inp, icmp_errmap(icmp));
736 INP_WUNLOCK(inp);
737 } else {
738 inp = in_pcblookup(pcbinfo, ip->ip_dst, uh->uh_dport,
739 ip->ip_src, uh->uh_sport,
740 INPLOOKUP_WILDCARD | INPLOOKUP_RLOCKPCB, NULL);
741 if (inp != NULL) {
742 struct udpcb *up;
743 udp_tun_icmp_t *func;
744
745 up = intoudpcb(inp);
746 func = up->u_icmp_func;
747 INP_RUNLOCK(inp);
748 if (func != NULL)
749 func(icmp);
750 }
751 }
752 }
753
754 static void
udp_ctlinput(struct icmp * icmp)755 udp_ctlinput(struct icmp *icmp)
756 {
757
758 return (udp_common_ctlinput(icmp, &V_udbinfo));
759 }
760
761 static void
udplite_ctlinput(struct icmp * icmp)762 udplite_ctlinput(struct icmp *icmp)
763 {
764
765 return (udp_common_ctlinput(icmp, &V_ulitecbinfo));
766 }
767 #endif /* INET */
768
769 static int
udp_pcblist(SYSCTL_HANDLER_ARGS)770 udp_pcblist(SYSCTL_HANDLER_ARGS)
771 {
772 struct inpcb_iterator inpi = INP_ALL_ITERATOR(&V_udbinfo,
773 INPLOOKUP_RLOCKPCB);
774 struct xinpgen xig;
775 struct inpcb *inp;
776 int error;
777
778 if (req->newptr != 0)
779 return (EPERM);
780
781 if (req->oldptr == 0) {
782 int n;
783
784 n = V_udbinfo.ipi_count;
785 n += imax(n / 8, 10);
786 req->oldidx = 2 * (sizeof xig) + n * sizeof(struct xinpcb);
787 return (0);
788 }
789
790 if ((error = sysctl_wire_old_buffer(req, 0)) != 0)
791 return (error);
792
793 bzero(&xig, sizeof(xig));
794 xig.xig_len = sizeof xig;
795 xig.xig_count = V_udbinfo.ipi_count;
796 xig.xig_gen = V_udbinfo.ipi_gencnt;
797 xig.xig_sogen = so_gencnt;
798 error = SYSCTL_OUT(req, &xig, sizeof xig);
799 if (error)
800 return (error);
801
802 while ((inp = inp_next(&inpi)) != NULL) {
803 if (inp->inp_gencnt <= xig.xig_gen &&
804 cr_canseeinpcb(req->td->td_ucred, inp) == 0) {
805 struct xinpcb xi;
806
807 in_pcbtoxinpcb(inp, &xi);
808 error = SYSCTL_OUT(req, &xi, sizeof xi);
809 if (error) {
810 INP_RUNLOCK(inp);
811 break;
812 }
813 }
814 }
815
816 if (!error) {
817 /*
818 * Give the user an updated idea of our state. If the
819 * generation differs from what we told her before, she knows
820 * that something happened while we were processing this
821 * request, and it might be necessary to retry.
822 */
823 xig.xig_gen = V_udbinfo.ipi_gencnt;
824 xig.xig_sogen = so_gencnt;
825 xig.xig_count = V_udbinfo.ipi_count;
826 error = SYSCTL_OUT(req, &xig, sizeof xig);
827 }
828
829 return (error);
830 }
831
832 SYSCTL_PROC(_net_inet_udp, UDPCTL_PCBLIST, pcblist,
833 CTLTYPE_OPAQUE | CTLFLAG_RD | CTLFLAG_MPSAFE, NULL, 0,
834 udp_pcblist, "S,xinpcb",
835 "List of active UDP sockets");
836
837 #ifdef INET
838 static int
udp_getcred(SYSCTL_HANDLER_ARGS)839 udp_getcred(SYSCTL_HANDLER_ARGS)
840 {
841 struct xucred xuc;
842 struct sockaddr_in addrs[2];
843 struct epoch_tracker et;
844 struct inpcb *inp;
845 int error;
846
847 if (req->newptr == NULL)
848 return (EINVAL);
849 error = priv_check(req->td, PRIV_NETINET_GETCRED);
850 if (error)
851 return (error);
852 error = SYSCTL_IN(req, addrs, sizeof(addrs));
853 if (error)
854 return (error);
855 NET_EPOCH_ENTER(et);
856 inp = in_pcblookup(&V_udbinfo, addrs[1].sin_addr, addrs[1].sin_port,
857 addrs[0].sin_addr, addrs[0].sin_port,
858 INPLOOKUP_WILDCARD | INPLOOKUP_RLOCKPCB, NULL);
859 NET_EPOCH_EXIT(et);
860 if (inp != NULL) {
861 INP_RLOCK_ASSERT(inp);
862 if (inp->inp_socket == NULL)
863 error = ENOENT;
864 if (error == 0)
865 error = cr_canseeinpcb(req->td->td_ucred, inp);
866 if (error == 0)
867 cru2x(inp->inp_cred, &xuc);
868 INP_RUNLOCK(inp);
869 } else
870 error = ENOENT;
871 if (error == 0)
872 error = SYSCTL_OUT(req, &xuc, sizeof(struct xucred));
873 return (error);
874 }
875
876 SYSCTL_PROC(_net_inet_udp, OID_AUTO, getcred,
877 CTLTYPE_OPAQUE | CTLFLAG_RW | CTLFLAG_PRISON | CTLFLAG_MPSAFE,
878 0, 0, udp_getcred, "S,xucred",
879 "Get the xucred of a UDP connection");
880 #endif /* INET */
881
882 int
udp_ctloutput(struct socket * so,struct sockopt * sopt)883 udp_ctloutput(struct socket *so, struct sockopt *sopt)
884 {
885 struct inpcb *inp;
886 struct udpcb *up;
887 int isudplite, error, optval;
888
889 error = 0;
890 isudplite = (so->so_proto->pr_protocol == IPPROTO_UDPLITE) ? 1 : 0;
891 inp = sotoinpcb(so);
892 KASSERT(inp != NULL, ("%s: inp == NULL", __func__));
893 INP_WLOCK(inp);
894 if (sopt->sopt_level != so->so_proto->pr_protocol) {
895 #ifdef INET6
896 if (INP_CHECK_SOCKAF(so, AF_INET6)) {
897 INP_WUNLOCK(inp);
898 error = ip6_ctloutput(so, sopt);
899 }
900 #endif
901 #if defined(INET) && defined(INET6)
902 else
903 #endif
904 #ifdef INET
905 {
906 INP_WUNLOCK(inp);
907 error = ip_ctloutput(so, sopt);
908 }
909 #endif
910 return (error);
911 }
912
913 switch (sopt->sopt_dir) {
914 case SOPT_SET:
915 switch (sopt->sopt_name) {
916 #if defined(IPSEC) || defined(IPSEC_SUPPORT)
917 #if defined(INET) || defined(INET6)
918 case UDP_ENCAP:
919 #ifdef INET
920 if (INP_SOCKAF(so) == AF_INET) {
921 if (!IPSEC_ENABLED(ipv4)) {
922 INP_WUNLOCK(inp);
923 return (ENOPROTOOPT);
924 }
925 error = UDPENCAP_PCBCTL(ipv4, inp, sopt);
926 break;
927 }
928 #endif /* INET */
929 #ifdef INET6
930 if (INP_SOCKAF(so) == AF_INET6) {
931 if (!IPSEC_ENABLED(ipv6)) {
932 INP_WUNLOCK(inp);
933 return (ENOPROTOOPT);
934 }
935 error = UDPENCAP_PCBCTL(ipv6, inp, sopt);
936 break;
937 }
938 #endif /* INET6 */
939 INP_WUNLOCK(inp);
940 return (EINVAL);
941 #endif /* INET || INET6 */
942
943 #endif /* IPSEC */
944 case UDPLITE_SEND_CSCOV:
945 case UDPLITE_RECV_CSCOV:
946 if (!isudplite) {
947 INP_WUNLOCK(inp);
948 error = ENOPROTOOPT;
949 break;
950 }
951 INP_WUNLOCK(inp);
952 error = sooptcopyin(sopt, &optval, sizeof(optval),
953 sizeof(optval));
954 if (error != 0)
955 break;
956 inp = sotoinpcb(so);
957 KASSERT(inp != NULL, ("%s: inp == NULL", __func__));
958 INP_WLOCK(inp);
959 up = intoudpcb(inp);
960 KASSERT(up != NULL, ("%s: up == NULL", __func__));
961 if ((optval != 0 && optval < 8) || (optval > 65535)) {
962 INP_WUNLOCK(inp);
963 error = EINVAL;
964 break;
965 }
966 if (sopt->sopt_name == UDPLITE_SEND_CSCOV)
967 up->u_txcslen = optval;
968 else
969 up->u_rxcslen = optval;
970 INP_WUNLOCK(inp);
971 break;
972 default:
973 INP_WUNLOCK(inp);
974 error = ENOPROTOOPT;
975 break;
976 }
977 break;
978 case SOPT_GET:
979 switch (sopt->sopt_name) {
980 #if defined(IPSEC) || defined(IPSEC_SUPPORT)
981 #if defined(INET) || defined(INET6)
982 case UDP_ENCAP:
983 #ifdef INET
984 if (INP_SOCKAF(so) == AF_INET) {
985 if (!IPSEC_ENABLED(ipv4)) {
986 INP_WUNLOCK(inp);
987 return (ENOPROTOOPT);
988 }
989 error = UDPENCAP_PCBCTL(ipv4, inp, sopt);
990 break;
991 }
992 #endif /* INET */
993 #ifdef INET6
994 if (INP_SOCKAF(so) == AF_INET6) {
995 if (!IPSEC_ENABLED(ipv6)) {
996 INP_WUNLOCK(inp);
997 return (ENOPROTOOPT);
998 }
999 error = UDPENCAP_PCBCTL(ipv6, inp, sopt);
1000 break;
1001 }
1002 #endif /* INET6 */
1003 INP_WUNLOCK(inp);
1004 return (EINVAL);
1005 #endif /* INET || INET6 */
1006
1007 #endif /* IPSEC */
1008 case UDPLITE_SEND_CSCOV:
1009 case UDPLITE_RECV_CSCOV:
1010 if (!isudplite) {
1011 INP_WUNLOCK(inp);
1012 error = ENOPROTOOPT;
1013 break;
1014 }
1015 up = intoudpcb(inp);
1016 KASSERT(up != NULL, ("%s: up == NULL", __func__));
1017 if (sopt->sopt_name == UDPLITE_SEND_CSCOV)
1018 optval = up->u_txcslen;
1019 else
1020 optval = up->u_rxcslen;
1021 INP_WUNLOCK(inp);
1022 error = sooptcopyout(sopt, &optval, sizeof(optval));
1023 break;
1024 default:
1025 INP_WUNLOCK(inp);
1026 error = ENOPROTOOPT;
1027 break;
1028 }
1029 break;
1030 }
1031 return (error);
1032 }
1033
1034 #ifdef INET
1035 #ifdef INET6
1036 /* The logic here is derived from ip6_setpktopt(). See comments there. */
1037 static int
udp_v4mapped_pktinfo(struct cmsghdr * cm,struct sockaddr_in * src,struct inpcb * inp,int flags)1038 udp_v4mapped_pktinfo(struct cmsghdr *cm, struct sockaddr_in * src,
1039 struct inpcb *inp, int flags)
1040 {
1041 struct ifnet *ifp;
1042 struct in6_pktinfo *pktinfo;
1043 struct in_addr ia;
1044
1045 NET_EPOCH_ASSERT();
1046
1047 if ((flags & PRUS_IPV6) == 0)
1048 return (0);
1049
1050 if (cm->cmsg_level != IPPROTO_IPV6)
1051 return (0);
1052
1053 if (cm->cmsg_type != IPV6_2292PKTINFO &&
1054 cm->cmsg_type != IPV6_PKTINFO)
1055 return (0);
1056
1057 if (cm->cmsg_len !=
1058 CMSG_LEN(sizeof(struct in6_pktinfo)))
1059 return (EINVAL);
1060
1061 pktinfo = (struct in6_pktinfo *)CMSG_DATA(cm);
1062 if (!IN6_IS_ADDR_V4MAPPED(&pktinfo->ipi6_addr) &&
1063 !IN6_IS_ADDR_UNSPECIFIED(&pktinfo->ipi6_addr))
1064 return (EINVAL);
1065
1066 /* Validate the interface index if specified. */
1067 if (pktinfo->ipi6_ifindex) {
1068 ifp = ifnet_byindex(pktinfo->ipi6_ifindex);
1069 if (ifp == NULL)
1070 return (ENXIO);
1071 } else
1072 ifp = NULL;
1073 if (ifp != NULL && !IN6_IS_ADDR_UNSPECIFIED(&pktinfo->ipi6_addr)) {
1074 ia.s_addr = pktinfo->ipi6_addr.s6_addr32[3];
1075 if (!in_ifhasaddr(ifp, ia))
1076 return (EADDRNOTAVAIL);
1077 }
1078
1079 bzero(src, sizeof(*src));
1080 src->sin_family = AF_INET;
1081 src->sin_len = sizeof(*src);
1082 src->sin_port = inp->inp_lport;
1083 src->sin_addr.s_addr = pktinfo->ipi6_addr.s6_addr32[3];
1084
1085 return (0);
1086 }
1087 #endif /* INET6 */
1088
1089 int
udp_send(struct socket * so,int flags,struct mbuf * m,struct sockaddr * addr,struct mbuf * control,struct thread * td)1090 udp_send(struct socket *so, int flags, struct mbuf *m, struct sockaddr *addr,
1091 struct mbuf *control, struct thread *td)
1092 {
1093 struct inpcb *inp;
1094 struct udpiphdr *ui;
1095 int len, error = 0;
1096 struct in_addr faddr, laddr;
1097 struct cmsghdr *cm;
1098 struct inpcbinfo *pcbinfo;
1099 struct sockaddr_in *sin, src;
1100 struct epoch_tracker et;
1101 int cscov_partial = 0;
1102 int ipflags = 0;
1103 u_short fport, lport;
1104 u_char tos, vflagsav;
1105 uint8_t pr;
1106 uint16_t cscov = 0;
1107 uint32_t flowid = 0;
1108 uint8_t flowtype = M_HASHTYPE_NONE;
1109 bool use_cached_route;
1110
1111 inp = sotoinpcb(so);
1112 KASSERT(inp != NULL, ("udp_send: inp == NULL"));
1113
1114 if (addr != NULL) {
1115 if (addr->sa_family != AF_INET)
1116 error = EAFNOSUPPORT;
1117 else if (addr->sa_len != sizeof(struct sockaddr_in))
1118 error = EINVAL;
1119 if (__predict_false(error != 0)) {
1120 m_freem(control);
1121 m_freem(m);
1122 return (error);
1123 }
1124 }
1125
1126 len = m->m_pkthdr.len;
1127 if (len + sizeof(struct udpiphdr) > IP_MAXPACKET) {
1128 if (control)
1129 m_freem(control);
1130 m_freem(m);
1131 return (EMSGSIZE);
1132 }
1133
1134 src.sin_family = 0;
1135 sin = (struct sockaddr_in *)addr;
1136
1137 /*
1138 * udp_send() may need to bind the current inpcb. As such, we don't
1139 * know up front whether we will need the pcbinfo lock or not. Do any
1140 * work to decide what is needed up front before acquiring any locks.
1141 *
1142 * We will need network epoch in either case, to safely lookup into
1143 * pcb hash.
1144 */
1145 use_cached_route = sin == NULL || (inp->inp_laddr.s_addr == INADDR_ANY && inp->inp_lport == 0);
1146 if (use_cached_route || (flags & PRUS_IPV6) != 0)
1147 INP_WLOCK(inp);
1148 else
1149 INP_RLOCK(inp);
1150 NET_EPOCH_ENTER(et);
1151 tos = inp->inp_ip_tos;
1152 if (control != NULL) {
1153 /*
1154 * XXX: Currently, we assume all the optional information is
1155 * stored in a single mbuf.
1156 */
1157 if (control->m_next) {
1158 m_freem(control);
1159 error = EINVAL;
1160 goto release;
1161 }
1162 for (; control->m_len > 0;
1163 control->m_data += CMSG_ALIGN(cm->cmsg_len),
1164 control->m_len -= CMSG_ALIGN(cm->cmsg_len)) {
1165 cm = mtod(control, struct cmsghdr *);
1166 if (control->m_len < sizeof(*cm) || cm->cmsg_len == 0
1167 || cm->cmsg_len > control->m_len) {
1168 error = EINVAL;
1169 break;
1170 }
1171 #ifdef INET6
1172 error = udp_v4mapped_pktinfo(cm, &src, inp, flags);
1173 if (error != 0)
1174 break;
1175 #endif
1176 if (cm->cmsg_level != IPPROTO_IP)
1177 continue;
1178
1179 switch (cm->cmsg_type) {
1180 case IP_SENDSRCADDR:
1181 if (cm->cmsg_len !=
1182 CMSG_LEN(sizeof(struct in_addr))) {
1183 error = EINVAL;
1184 break;
1185 }
1186 bzero(&src, sizeof(src));
1187 src.sin_family = AF_INET;
1188 src.sin_len = sizeof(src);
1189 src.sin_port = inp->inp_lport;
1190 src.sin_addr =
1191 *(struct in_addr *)CMSG_DATA(cm);
1192 break;
1193
1194 case IP_TOS:
1195 if (cm->cmsg_len != CMSG_LEN(sizeof(u_char))) {
1196 error = EINVAL;
1197 break;
1198 }
1199 tos = *(u_char *)CMSG_DATA(cm);
1200 break;
1201
1202 case IP_FLOWID:
1203 if (cm->cmsg_len != CMSG_LEN(sizeof(uint32_t))) {
1204 error = EINVAL;
1205 break;
1206 }
1207 flowid = *(uint32_t *) CMSG_DATA(cm);
1208 break;
1209
1210 case IP_FLOWTYPE:
1211 if (cm->cmsg_len != CMSG_LEN(sizeof(uint32_t))) {
1212 error = EINVAL;
1213 break;
1214 }
1215 flowtype = *(uint32_t *) CMSG_DATA(cm);
1216 break;
1217
1218 #ifdef RSS
1219 case IP_RSSBUCKETID:
1220 if (cm->cmsg_len != CMSG_LEN(sizeof(uint32_t))) {
1221 error = EINVAL;
1222 break;
1223 }
1224 /* This is just a placeholder for now */
1225 break;
1226 #endif /* RSS */
1227 default:
1228 error = ENOPROTOOPT;
1229 break;
1230 }
1231 if (error)
1232 break;
1233 }
1234 m_freem(control);
1235 control = NULL;
1236 }
1237 if (error)
1238 goto release;
1239
1240 pr = inp->inp_socket->so_proto->pr_protocol;
1241 pcbinfo = udp_get_inpcbinfo(pr);
1242
1243 /*
1244 * If the IP_SENDSRCADDR control message was specified, override the
1245 * source address for this datagram. Its use is invalidated if the
1246 * address thus specified is incomplete or clobbers other inpcbs.
1247 */
1248 laddr = inp->inp_laddr;
1249 lport = inp->inp_lport;
1250 if (src.sin_family == AF_INET) {
1251 if ((lport == 0) ||
1252 (laddr.s_addr == INADDR_ANY &&
1253 src.sin_addr.s_addr == INADDR_ANY)) {
1254 error = EINVAL;
1255 goto release;
1256 }
1257 if ((flags & PRUS_IPV6) != 0) {
1258 vflagsav = inp->inp_vflag;
1259 inp->inp_vflag |= INP_IPV4;
1260 inp->inp_vflag &= ~INP_IPV6;
1261 }
1262 INP_HASH_WLOCK(pcbinfo);
1263 error = in_pcbbind_setup(inp, &src, &laddr.s_addr, &lport,
1264 V_udp_bind_all_fibs ? 0 : INPBIND_FIB, td->td_ucred);
1265 INP_HASH_WUNLOCK(pcbinfo);
1266 if ((flags & PRUS_IPV6) != 0)
1267 inp->inp_vflag = vflagsav;
1268 if (error)
1269 goto release;
1270 }
1271
1272 /*
1273 * If a UDP socket has been connected, then a local address/port will
1274 * have been selected and bound.
1275 *
1276 * If a UDP socket has not been connected to, then an explicit
1277 * destination address must be used, in which case a local
1278 * address/port may not have been selected and bound.
1279 */
1280 if (sin != NULL) {
1281 INP_LOCK_ASSERT(inp);
1282 if (inp->inp_faddr.s_addr != INADDR_ANY) {
1283 error = EISCONN;
1284 goto release;
1285 }
1286
1287 /*
1288 * Jail may rewrite the destination address, so let it do
1289 * that before we use it.
1290 */
1291 error = prison_remote_ip4(td->td_ucred, &sin->sin_addr);
1292 if (error)
1293 goto release;
1294 /*
1295 * sendto(2) on unconnected UDP socket results in implicit
1296 * binding to INADDR_ANY and anonymous port. This has two
1297 * side effects:
1298 * 1) after first sendto(2) the socket will receive datagrams
1299 * destined to the selected port.
1300 * 2) subsequent sendto(2) calls will use the same source port.
1301 */
1302 if (inp->inp_lport == 0) {
1303 struct sockaddr_in wild = {
1304 .sin_family = AF_INET,
1305 .sin_len = sizeof(struct sockaddr_in),
1306 };
1307
1308 INP_HASH_WLOCK(pcbinfo);
1309 error = in_pcbbind(inp, &wild, V_udp_bind_all_fibs ?
1310 0 : INPBIND_FIB, td->td_ucred);
1311 INP_HASH_WUNLOCK(pcbinfo);
1312 if (error)
1313 goto release;
1314 lport = inp->inp_lport;
1315 laddr = inp->inp_laddr;
1316 }
1317 if (laddr.s_addr == INADDR_ANY) {
1318 error = in_pcbladdr(inp, &sin->sin_addr, &laddr,
1319 td->td_ucred);
1320 if (error)
1321 goto release;
1322 }
1323 faddr = sin->sin_addr;
1324 fport = sin->sin_port;
1325 } else {
1326 INP_LOCK_ASSERT(inp);
1327 faddr = inp->inp_faddr;
1328 fport = inp->inp_fport;
1329 if (faddr.s_addr == INADDR_ANY) {
1330 error = ENOTCONN;
1331 goto release;
1332 }
1333 }
1334
1335 /*
1336 * Calculate data length and get a mbuf for UDP, IP, and possible
1337 * link-layer headers. Immediate slide the data pointer back forward
1338 * since we won't use that space at this layer.
1339 */
1340 M_PREPEND(m, sizeof(struct udpiphdr) + max_linkhdr, M_NOWAIT);
1341 if (m == NULL) {
1342 error = ENOBUFS;
1343 goto release;
1344 }
1345 m->m_data += max_linkhdr;
1346 m->m_len -= max_linkhdr;
1347 m->m_pkthdr.len -= max_linkhdr;
1348
1349 /*
1350 * Fill in mbuf with extended UDP header and addresses and length put
1351 * into network format.
1352 */
1353 ui = mtod(m, struct udpiphdr *);
1354 /*
1355 * Filling only those fields of udpiphdr that participate in the
1356 * checksum calculation. The rest must be zeroed and will be filled
1357 * later.
1358 */
1359 bzero(ui->ui_x1, sizeof(ui->ui_x1));
1360 ui->ui_pr = pr;
1361 ui->ui_src = laddr;
1362 ui->ui_dst = faddr;
1363 ui->ui_sport = lport;
1364 ui->ui_dport = fport;
1365 ui->ui_ulen = htons((u_short)len + sizeof(struct udphdr));
1366 if (pr == IPPROTO_UDPLITE) {
1367 struct udpcb *up;
1368 uint16_t plen;
1369
1370 up = intoudpcb(inp);
1371 cscov = up->u_txcslen;
1372 plen = (u_short)len + sizeof(struct udphdr);
1373 if (cscov >= plen)
1374 cscov = 0;
1375 ui->ui_len = htons(plen);
1376 ui->ui_ulen = htons(cscov);
1377 /*
1378 * For UDP-Lite, checksum coverage length of zero means
1379 * the entire UDPLite packet is covered by the checksum.
1380 */
1381 cscov_partial = (cscov == 0) ? 0 : 1;
1382 }
1383
1384 if (inp->inp_socket->so_options & SO_DONTROUTE)
1385 ipflags |= IP_ROUTETOIF;
1386 if (inp->inp_socket->so_options & SO_BROADCAST)
1387 ipflags |= IP_ALLOWBROADCAST;
1388 if (inp->inp_flags & INP_ONESBCAST)
1389 ipflags |= IP_SENDONES;
1390
1391 #ifdef MAC
1392 mac_inpcb_create_mbuf(inp, m);
1393 #endif
1394
1395 /*
1396 * Set up checksum and output datagram.
1397 */
1398 ui->ui_sum = 0;
1399 if (pr == IPPROTO_UDPLITE) {
1400 if (inp->inp_flags & INP_ONESBCAST)
1401 faddr.s_addr = INADDR_BROADCAST;
1402 if (cscov_partial) {
1403 if ((ui->ui_sum = in_cksum(m, sizeof(struct ip) + cscov)) == 0)
1404 ui->ui_sum = 0xffff;
1405 } else {
1406 if ((ui->ui_sum = in_cksum(m, sizeof(struct udpiphdr) + len)) == 0)
1407 ui->ui_sum = 0xffff;
1408 }
1409 } else if (V_udp_cksum) {
1410 if (inp->inp_flags & INP_ONESBCAST)
1411 faddr.s_addr = INADDR_BROADCAST;
1412 ui->ui_sum = in_pseudo(ui->ui_src.s_addr, faddr.s_addr,
1413 htons((u_short)len + sizeof(struct udphdr) + pr));
1414 m->m_pkthdr.csum_flags = CSUM_UDP;
1415 m->m_pkthdr.csum_data = offsetof(struct udphdr, uh_sum);
1416 }
1417 /*
1418 * After finishing the checksum computation, fill the remaining fields
1419 * of udpiphdr.
1420 */
1421 ((struct ip *)ui)->ip_v = IPVERSION;
1422 ((struct ip *)ui)->ip_tos = tos;
1423 ((struct ip *)ui)->ip_len = htons(sizeof(struct udpiphdr) + len);
1424 if (inp->inp_flags & INP_DONTFRAG)
1425 ((struct ip *)ui)->ip_off |= htons(IP_DF);
1426 ((struct ip *)ui)->ip_ttl = inp->inp_ip_ttl;
1427 UDPSTAT_INC(udps_opackets);
1428
1429 /*
1430 * Setup flowid / RSS information for outbound socket.
1431 *
1432 * Once the UDP code decides to set a flowid some other way,
1433 * this allows the flowid to be overridden by userland.
1434 */
1435 if (flowtype != M_HASHTYPE_NONE) {
1436 m->m_pkthdr.flowid = flowid;
1437 M_HASHTYPE_SET(m, flowtype);
1438 }
1439 #if defined(ROUTE_MPATH) || defined(RSS)
1440 else if (CALC_FLOWID_OUTBOUND_SENDTO) {
1441 uint32_t hash_val, hash_type;
1442
1443 hash_val = fib4_calc_packet_hash(laddr, faddr,
1444 lport, fport, pr, &hash_type);
1445 m->m_pkthdr.flowid = hash_val;
1446 M_HASHTYPE_SET(m, hash_type);
1447 }
1448
1449 /*
1450 * Don't override with the inp cached flowid value.
1451 *
1452 * Depending upon the kind of send being done, the inp
1453 * flowid/flowtype values may actually not be appropriate
1454 * for this particular socket send.
1455 *
1456 * We should either leave the flowid at zero (which is what is
1457 * currently done) or set it to some software generated
1458 * hash value based on the packet contents.
1459 */
1460 ipflags |= IP_NODEFAULTFLOWID;
1461 #endif /* RSS */
1462
1463 if (pr == IPPROTO_UDPLITE)
1464 UDPLITE_PROBE(send, NULL, inp, &ui->ui_i, inp, &ui->ui_u);
1465 else
1466 UDP_PROBE(send, NULL, inp, &ui->ui_i, inp, &ui->ui_u);
1467 error = ip_output(m, inp->inp_options,
1468 use_cached_route ? &inp->inp_route : NULL, ipflags,
1469 inp->inp_moptions, inp);
1470 INP_UNLOCK(inp);
1471 NET_EPOCH_EXIT(et);
1472 return (error);
1473
1474 release:
1475 INP_UNLOCK(inp);
1476 NET_EPOCH_EXIT(et);
1477 m_freem(m);
1478 return (error);
1479 }
1480
1481 void
udp_abort(struct socket * so)1482 udp_abort(struct socket *so)
1483 {
1484 struct inpcb *inp;
1485 struct inpcbinfo *pcbinfo;
1486
1487 pcbinfo = udp_get_inpcbinfo(so->so_proto->pr_protocol);
1488 inp = sotoinpcb(so);
1489 KASSERT(inp != NULL, ("udp_abort: inp == NULL"));
1490 INP_WLOCK(inp);
1491 if (inp->inp_faddr.s_addr != INADDR_ANY) {
1492 INP_HASH_WLOCK(pcbinfo);
1493 in_pcbdisconnect(inp);
1494 INP_HASH_WUNLOCK(pcbinfo);
1495 soisdisconnected(so);
1496 }
1497 INP_WUNLOCK(inp);
1498 }
1499
1500 static int
udp_attach(struct socket * so,int proto,struct thread * td)1501 udp_attach(struct socket *so, int proto, struct thread *td)
1502 {
1503 static uint32_t udp_flowid;
1504 struct inpcbinfo *pcbinfo;
1505 struct inpcb *inp;
1506 struct udpcb *up;
1507 int error;
1508
1509 pcbinfo = udp_get_inpcbinfo(so->so_proto->pr_protocol);
1510 inp = sotoinpcb(so);
1511 KASSERT(inp == NULL, ("udp_attach: inp != NULL"));
1512 error = soreserve(so, udp_sendspace, udp_recvspace);
1513 if (error)
1514 return (error);
1515 error = in_pcballoc(so, pcbinfo);
1516 if (error)
1517 return (error);
1518
1519 inp = sotoinpcb(so);
1520 inp->inp_ip_ttl = V_ip_defttl;
1521 inp->inp_flowid = atomic_fetchadd_int(&udp_flowid, 1);
1522 inp->inp_flowtype = M_HASHTYPE_OPAQUE;
1523 up = intoudpcb(inp);
1524 bzero(&up->u_start_zero, u_zero_size);
1525 INP_WUNLOCK(inp);
1526
1527 return (0);
1528 }
1529 #endif /* INET */
1530
1531 int
udp_set_kernel_tunneling(struct socket * so,udp_tun_func_t f,udp_tun_icmp_t i,void * ctx)1532 udp_set_kernel_tunneling(struct socket *so, udp_tun_func_t f, udp_tun_icmp_t i, void *ctx)
1533 {
1534 struct inpcb *inp;
1535 struct udpcb *up;
1536
1537 KASSERT(so->so_type == SOCK_DGRAM,
1538 ("udp_set_kernel_tunneling: !dgram"));
1539 inp = sotoinpcb(so);
1540 KASSERT(inp != NULL, ("udp_set_kernel_tunneling: inp == NULL"));
1541 INP_WLOCK(inp);
1542 up = intoudpcb(inp);
1543 if ((f != NULL || i != NULL) && ((up->u_tun_func != NULL) ||
1544 (up->u_icmp_func != NULL))) {
1545 INP_WUNLOCK(inp);
1546 return (EBUSY);
1547 }
1548 up->u_tun_func = f;
1549 up->u_icmp_func = i;
1550 up->u_tun_ctx = ctx;
1551 INP_WUNLOCK(inp);
1552 return (0);
1553 }
1554
1555 #ifdef INET
1556 static int
udp_bind(struct socket * so,struct sockaddr * nam,struct thread * td)1557 udp_bind(struct socket *so, struct sockaddr *nam, struct thread *td)
1558 {
1559 struct inpcb *inp;
1560 struct inpcbinfo *pcbinfo;
1561 struct sockaddr_in *sinp;
1562 int error;
1563
1564 pcbinfo = udp_get_inpcbinfo(so->so_proto->pr_protocol);
1565 inp = sotoinpcb(so);
1566 KASSERT(inp != NULL, ("udp_bind: inp == NULL"));
1567
1568 sinp = (struct sockaddr_in *)nam;
1569 if (nam->sa_family != AF_INET) {
1570 /*
1571 * Preserve compatibility with old programs.
1572 */
1573 if (nam->sa_family != AF_UNSPEC ||
1574 nam->sa_len < offsetof(struct sockaddr_in, sin_zero) ||
1575 sinp->sin_addr.s_addr != INADDR_ANY)
1576 return (EAFNOSUPPORT);
1577 nam->sa_family = AF_INET;
1578 }
1579 if (nam->sa_len != sizeof(struct sockaddr_in))
1580 return (EINVAL);
1581
1582 INP_WLOCK(inp);
1583 INP_HASH_WLOCK(pcbinfo);
1584 error = in_pcbbind(inp, sinp, V_udp_bind_all_fibs ? 0 : INPBIND_FIB,
1585 td->td_ucred);
1586 INP_HASH_WUNLOCK(pcbinfo);
1587 INP_WUNLOCK(inp);
1588 return (error);
1589 }
1590
1591 static void
udp_close(struct socket * so)1592 udp_close(struct socket *so)
1593 {
1594 struct inpcb *inp;
1595 struct inpcbinfo *pcbinfo;
1596
1597 pcbinfo = udp_get_inpcbinfo(so->so_proto->pr_protocol);
1598 inp = sotoinpcb(so);
1599 KASSERT(inp != NULL, ("udp_close: inp == NULL"));
1600 INP_WLOCK(inp);
1601 if (inp->inp_faddr.s_addr != INADDR_ANY) {
1602 INP_HASH_WLOCK(pcbinfo);
1603 in_pcbdisconnect(inp);
1604 INP_HASH_WUNLOCK(pcbinfo);
1605 soisdisconnected(so);
1606 }
1607 INP_WUNLOCK(inp);
1608 }
1609
1610 static int
udp_connect(struct socket * so,struct sockaddr * nam,struct thread * td)1611 udp_connect(struct socket *so, struct sockaddr *nam, struct thread *td)
1612 {
1613 struct epoch_tracker et;
1614 struct inpcb *inp;
1615 struct inpcbinfo *pcbinfo;
1616 struct sockaddr_in *sin;
1617 int error;
1618
1619 pcbinfo = udp_get_inpcbinfo(so->so_proto->pr_protocol);
1620 inp = sotoinpcb(so);
1621 KASSERT(inp != NULL, ("udp_connect: inp == NULL"));
1622
1623 sin = (struct sockaddr_in *)nam;
1624 if (sin->sin_family != AF_INET)
1625 return (EAFNOSUPPORT);
1626 if (sin->sin_len != sizeof(*sin))
1627 return (EINVAL);
1628
1629 INP_WLOCK(inp);
1630 if (inp->inp_faddr.s_addr != INADDR_ANY) {
1631 INP_WUNLOCK(inp);
1632 return (EISCONN);
1633 }
1634 error = prison_remote_ip4(td->td_ucred, &sin->sin_addr);
1635 if (error != 0) {
1636 INP_WUNLOCK(inp);
1637 return (error);
1638 }
1639 NET_EPOCH_ENTER(et);
1640 INP_HASH_WLOCK(pcbinfo);
1641 error = in_pcbconnect(inp, sin, td->td_ucred);
1642 INP_HASH_WUNLOCK(pcbinfo);
1643 NET_EPOCH_EXIT(et);
1644 if (error == 0)
1645 soisconnected(so);
1646 INP_WUNLOCK(inp);
1647 return (error);
1648 }
1649
1650 static void
udp_detach(struct socket * so)1651 udp_detach(struct socket *so)
1652 {
1653 struct inpcb *inp;
1654
1655 inp = sotoinpcb(so);
1656 KASSERT(inp != NULL, ("udp_detach: inp == NULL"));
1657 KASSERT(inp->inp_faddr.s_addr == INADDR_ANY,
1658 ("udp_detach: not disconnected"));
1659 INP_WLOCK(inp);
1660 in_pcbfree(inp);
1661 }
1662
1663 int
udp_disconnect(struct socket * so)1664 udp_disconnect(struct socket *so)
1665 {
1666 struct inpcb *inp;
1667 struct inpcbinfo *pcbinfo;
1668
1669 pcbinfo = udp_get_inpcbinfo(so->so_proto->pr_protocol);
1670 inp = sotoinpcb(so);
1671 KASSERT(inp != NULL, ("udp_disconnect: inp == NULL"));
1672 INP_WLOCK(inp);
1673 if (inp->inp_faddr.s_addr == INADDR_ANY) {
1674 INP_WUNLOCK(inp);
1675 return (ENOTCONN);
1676 }
1677 INP_HASH_WLOCK(pcbinfo);
1678 in_pcbdisconnect(inp);
1679 INP_HASH_WUNLOCK(pcbinfo);
1680 SOCK_LOCK(so);
1681 so->so_state &= ~SS_ISCONNECTED; /* XXX */
1682 SOCK_UNLOCK(so);
1683 INP_WUNLOCK(inp);
1684 return (0);
1685 }
1686 #endif /* INET */
1687
1688 int
udp_shutdown(struct socket * so,enum shutdown_how how)1689 udp_shutdown(struct socket *so, enum shutdown_how how)
1690 {
1691 int error;
1692
1693 SOCK_LOCK(so);
1694 if (!(so->so_state & SS_ISCONNECTED))
1695 /*
1696 * POSIX mandates us to just return ENOTCONN when shutdown(2) is
1697 * invoked on a datagram sockets, however historically we would
1698 * actually tear socket down. This is known to be leveraged by
1699 * some applications to unblock process waiting in recv(2) by
1700 * other process that it shares that socket with. Try to meet
1701 * both backward-compatibility and POSIX requirements by forcing
1702 * ENOTCONN but still flushing buffers and performing wakeup(9).
1703 *
1704 * XXXGL: it remains unknown what applications expect this
1705 * behavior and is this isolated to unix/dgram or inet/dgram or
1706 * both. See: D10351, D3039.
1707 */
1708 error = ENOTCONN;
1709 else
1710 error = 0;
1711 SOCK_UNLOCK(so);
1712
1713 switch (how) {
1714 case SHUT_RD:
1715 sorflush(so);
1716 break;
1717 case SHUT_RDWR:
1718 sorflush(so);
1719 /* FALLTHROUGH */
1720 case SHUT_WR:
1721 socantsendmore(so);
1722 }
1723
1724 return (error);
1725 }
1726
1727 #ifdef INET
1728 #define UDP_PROTOSW \
1729 .pr_type = SOCK_DGRAM, \
1730 .pr_flags = PR_ATOMIC | PR_ADDR | PR_CAPATTACH, \
1731 .pr_ctloutput = udp_ctloutput, \
1732 .pr_abort = udp_abort, \
1733 .pr_attach = udp_attach, \
1734 .pr_bind = udp_bind, \
1735 .pr_connect = udp_connect, \
1736 .pr_control = in_control, \
1737 .pr_detach = udp_detach, \
1738 .pr_disconnect = udp_disconnect, \
1739 .pr_peeraddr = in_getpeeraddr, \
1740 .pr_send = udp_send, \
1741 .pr_soreceive = soreceive_dgram, \
1742 .pr_sosend = sosend_dgram, \
1743 .pr_shutdown = udp_shutdown, \
1744 .pr_sockaddr = in_getsockaddr, \
1745 .pr_sosetlabel = in_pcbsosetlabel, \
1746 .pr_close = udp_close
1747
1748 struct protosw udp_protosw = {
1749 .pr_protocol = IPPROTO_UDP,
1750 UDP_PROTOSW
1751 };
1752
1753 struct protosw udplite_protosw = {
1754 .pr_protocol = IPPROTO_UDPLITE,
1755 UDP_PROTOSW
1756 };
1757
1758 static void
udp_init(void * arg __unused)1759 udp_init(void *arg __unused)
1760 {
1761
1762 IPPROTO_REGISTER(IPPROTO_UDP, udp_input, udp_ctlinput);
1763 IPPROTO_REGISTER(IPPROTO_UDPLITE, udp_input, udplite_ctlinput);
1764 }
1765 SYSINIT(udp_init, SI_SUB_PROTO_DOMAIN, SI_ORDER_THIRD, udp_init, NULL);
1766 #endif /* INET */
1767