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