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_rss.h"
43
44 #include <sys/param.h>
45 #include <sys/domain.h>
46 #include <sys/eventhandler.h>
47 #include <sys/jail.h>
48 #include <sys/kernel.h>
49 #include <sys/lock.h>
50 #include <sys/malloc.h>
51 #include <sys/mbuf.h>
52 #include <sys/priv.h>
53 #include <sys/proc.h>
54 #include <sys/protosw.h>
55 #include <sys/sdt.h>
56 #include <sys/signalvar.h>
57 #include <sys/socket.h>
58 #include <sys/socketvar.h>
59 #include <sys/sx.h>
60 #include <sys/sysctl.h>
61 #include <sys/syslog.h>
62 #include <sys/systm.h>
63
64 #include <vm/uma.h>
65
66 #include <net/if.h>
67 #include <net/if_var.h>
68 #include <net/route.h>
69 #include <net/route/nhop.h>
70 #include <net/rss_config.h>
71
72 #include <netinet/in.h>
73 #include <netinet/in_kdtrace.h>
74 #include <netinet/in_fib.h>
75 #include <netinet/in_pcb.h>
76 #include <netinet/in_systm.h>
77 #include <netinet/in_var.h>
78 #include <netinet/ip.h>
79 #ifdef INET6
80 #include <netinet/ip6.h>
81 #endif
82 #include <netinet/ip_icmp.h>
83 #include <netinet/icmp_var.h>
84 #include <netinet/ip_var.h>
85 #include <netinet/ip_options.h>
86 #ifdef INET6
87 #include <netinet6/ip6_var.h>
88 #endif
89 #include <netinet/udp.h>
90 #include <netinet/udp_var.h>
91 #include <netinet/udplite.h>
92 #include <netinet/in_rss.h>
93
94 #include <netipsec/ipsec_support.h>
95
96 #include <machine/in_cksum.h>
97
98 #include <security/mac/mac_framework.h>
99
100 /*
101 * UDP and UDP-Lite protocols implementation.
102 * Per RFC 768, August, 1980.
103 * Per RFC 3828, July, 2004.
104 */
105
106 VNET_DEFINE(int, udp_bind_all_fibs) = 1;
107 SYSCTL_INT(_net_inet_udp, OID_AUTO, bind_all_fibs, CTLFLAG_VNET | CTLFLAG_RDTUN,
108 &VNET_NAME(udp_bind_all_fibs), 0,
109 "Bound sockets receive traffic from all FIBs");
110
111 /*
112 * BSD 4.2 defaulted the udp checksum to be off. Turning off udp checksums
113 * removes the only data integrity mechanism for packets and malformed
114 * packets that would otherwise be discarded due to bad checksums, and may
115 * cause problems (especially for NFS data blocks).
116 */
117 VNET_DEFINE(int, udp_cksum) = 1;
118 SYSCTL_INT(_net_inet_udp, UDPCTL_CHECKSUM, checksum, CTLFLAG_VNET | CTLFLAG_RW,
119 &VNET_NAME(udp_cksum), 0, "compute udp checksum");
120
121 VNET_DEFINE(int, udp_log_in_vain) = 0;
122 SYSCTL_INT(_net_inet_udp, OID_AUTO, log_in_vain, CTLFLAG_VNET | CTLFLAG_RW,
123 &VNET_NAME(udp_log_in_vain), 0, "Log all incoming UDP packets");
124
125 VNET_DEFINE(int, udp_blackhole) = 0;
126 SYSCTL_INT(_net_inet_udp, OID_AUTO, blackhole, CTLFLAG_VNET | CTLFLAG_RW,
127 &VNET_NAME(udp_blackhole), 0,
128 "Do not send port unreachables for refused connects");
129 VNET_DEFINE(bool, udp_blackhole_local) = false;
130 SYSCTL_BOOL(_net_inet_udp, OID_AUTO, blackhole_local, CTLFLAG_VNET |
131 CTLFLAG_RW, &VNET_NAME(udp_blackhole_local), false,
132 "Enforce net.inet.udp.blackhole for locally originated packets");
133
134 u_long udp_sendspace = 9216; /* really max datagram size */
135 SYSCTL_ULONG(_net_inet_udp, UDPCTL_MAXDGRAM, maxdgram, CTLFLAG_RW,
136 &udp_sendspace, 0, "Maximum outgoing UDP datagram size");
137
138 u_long udp_recvspace = 40 * (1024 +
139 #ifdef INET6
140 sizeof(struct sockaddr_in6)
141 #else
142 sizeof(struct sockaddr_in)
143 #endif
144 ); /* 40 1K datagrams */
145
146 SYSCTL_ULONG(_net_inet_udp, UDPCTL_RECVSPACE, recvspace, CTLFLAG_RW,
147 &udp_recvspace, 0, "Maximum space for incoming UDP datagrams");
148
149 VNET_DEFINE(struct inpcbinfo, udbinfo);
150 VNET_DEFINE(struct inpcbinfo, ulitecbinfo);
151
152 #ifndef UDBHASHSIZE
153 #define UDBHASHSIZE 128
154 #endif
155
156 VNET_PCPUSTAT_DEFINE(struct udpstat, udpstat); /* from udp_var.h */
157 VNET_PCPUSTAT_SYSINIT(udpstat);
158 SYSCTL_VNET_PCPUSTAT(_net_inet_udp, UDPCTL_STATS, stats, struct udpstat,
159 udpstat, "UDP statistics (struct udpstat, netinet/udp_var.h)");
160
161 #ifdef VIMAGE
162 VNET_PCPUSTAT_SYSUNINIT(udpstat);
163 #endif /* VIMAGE */
164 #ifdef INET
165 static void udp_detach(struct socket *so);
166 #endif
167
168 INPCBSTORAGE_DEFINE(udpcbstor, udpcb, "udpinp", "udp_inpcb", "udphash");
169 INPCBSTORAGE_DEFINE(udplitecbstor, udpcb, "udpliteinp", "udplite_inpcb",
170 "udplitehash");
171
172 static void
udp_vnet_init(void * arg __unused)173 udp_vnet_init(void *arg __unused)
174 {
175
176 /*
177 * For now default to 2-tuple UDP hashing - until the fragment
178 * reassembly code can also update the flowid.
179 *
180 * Once we can calculate the flowid that way and re-establish
181 * a 4-tuple, flip this to 4-tuple.
182 */
183 in_pcbinfo_init(&V_udbinfo, &udpcbstor, UDBHASHSIZE, UDBHASHSIZE,
184 UDBHASHSIZE);
185 /* Additional pcbinfo for UDP-Lite */
186 in_pcbinfo_init(&V_ulitecbinfo, &udplitecbstor, UDBHASHSIZE,
187 UDBHASHSIZE, 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 sockaddr_in *sin, src;
1122 struct epoch_tracker et;
1123 int cscov_partial = 0;
1124 int ipflags = 0;
1125 u_short fport, lport;
1126 u_char tos, vflagsav;
1127 uint8_t pr;
1128 uint16_t cscov = 0;
1129 uint32_t hash_val, hash_type, flowid = 0;
1130 uint8_t flowtype = M_HASHTYPE_NONE;
1131 bool use_cached_route;
1132
1133 inp = sotoinpcb(so);
1134 KASSERT(inp != NULL, ("udp_send: inp == NULL"));
1135
1136 if (addr != NULL) {
1137 if (addr->sa_family != AF_INET)
1138 error = EAFNOSUPPORT;
1139 else if (addr->sa_len != sizeof(struct sockaddr_in))
1140 error = EINVAL;
1141 if (__predict_false(error != 0)) {
1142 m_freem(control);
1143 m_freem(m);
1144 return (error);
1145 }
1146 }
1147
1148 len = m->m_pkthdr.len;
1149 if (len + sizeof(struct udpiphdr) > IP_MAXPACKET) {
1150 if (control)
1151 m_freem(control);
1152 m_freem(m);
1153 return (EMSGSIZE);
1154 }
1155
1156 src.sin_family = 0;
1157 sin = (struct sockaddr_in *)addr;
1158
1159 /*
1160 * udp_send() may need to bind the current inpcb. As such, we don't
1161 * know up front whether we will need the pcbinfo lock or not. Do any
1162 * work to decide what is needed up front before acquiring any locks.
1163 *
1164 * We will need network epoch in either case, to safely lookup into
1165 * pcb hash.
1166 */
1167 use_cached_route = sin == NULL ||
1168 (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
1294 /*
1295 * If the IP_SENDSRCADDR control message was specified, override the
1296 * source address for this datagram. Its use is invalidated if the
1297 * address thus specified is incomplete or clobbers other inpcbs.
1298 */
1299 laddr = inp->inp_laddr;
1300 lport = inp->inp_lport;
1301 if (src.sin_family == AF_INET) {
1302 if ((lport == 0) ||
1303 (laddr.s_addr == INADDR_ANY &&
1304 src.sin_addr.s_addr == INADDR_ANY)) {
1305 error = EINVAL;
1306 goto release;
1307 }
1308 if ((flags & PRUS_IPV6) != 0) {
1309 vflagsav = inp->inp_vflag;
1310 inp->inp_vflag |= INP_IPV4;
1311 inp->inp_vflag &= ~INP_IPV6;
1312 }
1313 error = in_pcbbind_setup(inp, &src, &laddr.s_addr, &lport,
1314 V_udp_bind_all_fibs ? 0 : INPBIND_FIB, td->td_ucred);
1315 if ((flags & PRUS_IPV6) != 0)
1316 inp->inp_vflag = vflagsav;
1317 if (error)
1318 goto release;
1319 }
1320
1321 /*
1322 * If a UDP socket has been connected, then a local address/port will
1323 * have been selected and bound.
1324 *
1325 * If a UDP socket has not been connected to, then an explicit
1326 * destination address must be used, in which case a local
1327 * address/port may not have been selected and bound.
1328 */
1329 if (sin != NULL) {
1330 INP_LOCK_ASSERT(inp);
1331 if (inp->inp_faddr.s_addr != INADDR_ANY) {
1332 error = EISCONN;
1333 goto release;
1334 }
1335
1336 /*
1337 * Jail may rewrite the destination address, so let it do
1338 * that before we use it.
1339 */
1340 error = prison_remote_ip4(td->td_ucred, &sin->sin_addr);
1341 if (error)
1342 goto release;
1343 /*
1344 * sendto(2) on unconnected UDP socket results in implicit
1345 * binding to INADDR_ANY and anonymous port. This has two
1346 * side effects:
1347 * 1) after first sendto(2) the socket will receive datagrams
1348 * destined to the selected port.
1349 * 2) subsequent sendto(2) calls will use the same source port.
1350 */
1351 if (inp->inp_lport == 0) {
1352 struct sockaddr_in wild = {
1353 .sin_family = AF_INET,
1354 .sin_len = sizeof(struct sockaddr_in),
1355 };
1356
1357 error = in_pcbbind(inp, &wild, V_udp_bind_all_fibs ?
1358 0 : INPBIND_FIB, td->td_ucred);
1359 if (error)
1360 goto release;
1361 lport = inp->inp_lport;
1362 laddr = inp->inp_laddr;
1363 }
1364 if (laddr.s_addr == INADDR_ANY) {
1365 error = in_pcbladdr(inp, &sin->sin_addr, &laddr,
1366 td->td_ucred);
1367 if (error)
1368 goto release;
1369 }
1370 faddr = sin->sin_addr;
1371 fport = sin->sin_port;
1372 } else {
1373 INP_LOCK_ASSERT(inp);
1374 faddr = inp->inp_faddr;
1375 fport = inp->inp_fport;
1376 if (faddr.s_addr == INADDR_ANY) {
1377 error = ENOTCONN;
1378 goto release;
1379 }
1380 }
1381
1382 /*
1383 * Calculate data length and get a mbuf for UDP, IP, and possible
1384 * link-layer headers. Immediate slide the data pointer back forward
1385 * since we won't use that space at this layer.
1386 */
1387 M_PREPEND(m, sizeof(struct udpiphdr) + max_linkhdr, M_NOWAIT);
1388 if (m == NULL) {
1389 error = ENOBUFS;
1390 goto release;
1391 }
1392 m->m_data += max_linkhdr;
1393 m->m_len -= max_linkhdr;
1394 m->m_pkthdr.len -= max_linkhdr;
1395
1396 /*
1397 * Fill in mbuf with extended UDP header and addresses and length put
1398 * into network format.
1399 */
1400 ui = mtod(m, struct udpiphdr *);
1401 /*
1402 * Filling only those fields of udpiphdr that participate in the
1403 * checksum calculation. The rest must be zeroed and will be filled
1404 * later.
1405 */
1406 bzero(ui->ui_x1, sizeof(ui->ui_x1));
1407 ui->ui_pr = pr;
1408 ui->ui_src = laddr;
1409 ui->ui_dst = faddr;
1410 ui->ui_sport = lport;
1411 ui->ui_dport = fport;
1412 ui->ui_ulen = htons((u_short)len + sizeof(struct udphdr));
1413 if (pr == IPPROTO_UDPLITE) {
1414 struct udpcb *up;
1415 uint16_t plen;
1416
1417 up = intoudpcb(inp);
1418 cscov = up->u_txcslen;
1419 plen = (u_short)len + sizeof(struct udphdr);
1420 if (cscov >= plen)
1421 cscov = 0;
1422 ui->ui_len = htons(plen);
1423 ui->ui_ulen = htons(cscov);
1424 /*
1425 * For UDP-Lite, checksum coverage length of zero means
1426 * the entire UDPLite packet is covered by the checksum.
1427 */
1428 cscov_partial = (cscov == 0) ? 0 : 1;
1429 }
1430
1431 if (inp->inp_socket->so_options & SO_DONTROUTE)
1432 ipflags |= IP_ROUTETOIF;
1433 if (inp->inp_socket->so_options & SO_BROADCAST)
1434 ipflags |= IP_ALLOWBROADCAST;
1435 if (inp->inp_flags & INP_ONESBCAST)
1436 ipflags |= IP_SENDONES;
1437
1438 #ifdef MAC
1439 mac_inpcb_create_mbuf(inp, m);
1440 #endif
1441
1442 /*
1443 * Set up checksum and output datagram.
1444 */
1445 ui->ui_sum = 0;
1446 if (pr == IPPROTO_UDPLITE) {
1447 if (inp->inp_flags & INP_ONESBCAST)
1448 faddr.s_addr = INADDR_BROADCAST;
1449 if (cscov_partial) {
1450 if ((ui->ui_sum = in_cksum(m, sizeof(struct ip) + cscov)) == 0)
1451 ui->ui_sum = 0xffff;
1452 } else {
1453 if ((ui->ui_sum = in_cksum(m, sizeof(struct udpiphdr) + len)) == 0)
1454 ui->ui_sum = 0xffff;
1455 }
1456 } else if (V_udp_cksum) {
1457 if (inp->inp_flags & INP_ONESBCAST)
1458 faddr.s_addr = INADDR_BROADCAST;
1459 ui->ui_sum = in_pseudo(ui->ui_src.s_addr, faddr.s_addr,
1460 htons((u_short)len + sizeof(struct udphdr) + pr));
1461 m->m_pkthdr.csum_flags = CSUM_UDP;
1462 m->m_pkthdr.csum_data = offsetof(struct udphdr, uh_sum);
1463 }
1464 /*
1465 * After finishing the checksum computation, fill the remaining fields
1466 * of udpiphdr.
1467 */
1468 ((struct ip *)ui)->ip_v = IPVERSION;
1469 ((struct ip *)ui)->ip_tos = tos;
1470 ((struct ip *)ui)->ip_len = htons(sizeof(struct udpiphdr) + len);
1471 if (inp->inp_flags & INP_DONTFRAG)
1472 ((struct ip *)ui)->ip_off |= htons(IP_DF);
1473 ((struct ip *)ui)->ip_ttl = inp->inp_ip_ttl;
1474 UDPSTAT_INC(udps_opackets);
1475
1476 /*
1477 * Setup flowid / RSS information for outbound socket.
1478 *
1479 * Once the UDP code decides to set a flowid some other way,
1480 * this allows the flowid to be overridden by userland.
1481 */
1482 if (flowtype != M_HASHTYPE_NONE) {
1483 m->m_pkthdr.flowid = flowid;
1484 M_HASHTYPE_SET(m, flowtype);
1485 } else if (CALC_FLOWID_OUTBOUND_SENDTO) {
1486 hash_val = fib4_calc_packet_hash(laddr, faddr,
1487 lport, fport, pr, &hash_type);
1488 m->m_pkthdr.flowid = hash_val;
1489 M_HASHTYPE_SET(m, hash_type);
1490 }
1491
1492 /*
1493 * Don't override with the inp cached flowid value.
1494 *
1495 * Depending upon the kind of send being done, the inp
1496 * flowid/flowtype values may actually not be appropriate
1497 * for this particular socket send.
1498 *
1499 * We should either leave the flowid at zero (which is what is
1500 * currently done) or set it to some software generated
1501 * hash value based on the packet contents.
1502 */
1503 ipflags |= IP_NODEFAULTFLOWID;
1504
1505 if (pr == IPPROTO_UDPLITE)
1506 UDPLITE_PROBE(send, NULL, inp, &ui->ui_i, inp, &ui->ui_u);
1507 else
1508 UDP_PROBE(send, NULL, inp, &ui->ui_i, inp, &ui->ui_u);
1509 error = ip_output(m, inp->inp_options,
1510 use_cached_route ? &inp->inp_route : NULL, ipflags,
1511 inp->inp_moptions, inp);
1512 INP_UNLOCK(inp);
1513 NET_EPOCH_EXIT(et);
1514 return (error);
1515
1516 release:
1517 INP_UNLOCK(inp);
1518 NET_EPOCH_EXIT(et);
1519 m_freem(m);
1520 return (error);
1521 }
1522
1523 void
udp_abort(struct socket * so)1524 udp_abort(struct socket *so)
1525 {
1526 struct inpcb *inp;
1527
1528 inp = sotoinpcb(so);
1529 KASSERT(inp != NULL, ("udp_abort: inp == NULL"));
1530 INP_WLOCK(inp);
1531 if (inp->inp_faddr.s_addr != INADDR_ANY) {
1532 in_pcbdisconnect(inp);
1533 inp->inp_laddr.s_addr = INADDR_ANY;
1534 soisdisconnected(so);
1535 }
1536 INP_WUNLOCK(inp);
1537 }
1538
1539 static int
udp_attach(struct socket * so,int proto,struct thread * td)1540 udp_attach(struct socket *so, int proto, struct thread *td)
1541 {
1542 static uint32_t udp_flowid;
1543 struct inpcbinfo *pcbinfo;
1544 struct inpcb *inp;
1545 struct udpcb *up;
1546 int error;
1547
1548 pcbinfo = udp_get_inpcbinfo(so->so_proto->pr_protocol);
1549 inp = sotoinpcb(so);
1550 KASSERT(inp == NULL, ("udp_attach: inp != NULL"));
1551 error = soreserve(so, udp_sendspace, udp_recvspace);
1552 if (error)
1553 return (error);
1554 error = in_pcballoc(so, pcbinfo);
1555 if (error)
1556 return (error);
1557
1558 inp = sotoinpcb(so);
1559 inp->inp_ip_ttl = V_ip_defttl;
1560 inp->inp_flowid = atomic_fetchadd_int(&udp_flowid, 1);
1561 inp->inp_flowtype = M_HASHTYPE_OPAQUE;
1562 up = intoudpcb(inp);
1563 bzero(&up->u_start_zero, u_zero_size);
1564 INP_WUNLOCK(inp);
1565
1566 return (0);
1567 }
1568 #endif /* INET */
1569
1570 int
udp_set_kernel_tunneling(struct socket * so,udp_tun_func_t f,udp_tun_icmp_t i,void * ctx)1571 udp_set_kernel_tunneling(struct socket *so, udp_tun_func_t f, udp_tun_icmp_t i, void *ctx)
1572 {
1573 struct inpcb *inp;
1574 struct udpcb *up;
1575
1576 KASSERT(so->so_type == SOCK_DGRAM,
1577 ("udp_set_kernel_tunneling: !dgram"));
1578 inp = sotoinpcb(so);
1579 KASSERT(inp != NULL, ("udp_set_kernel_tunneling: inp == NULL"));
1580 INP_WLOCK(inp);
1581 up = intoudpcb(inp);
1582 if ((f != NULL || i != NULL) && ((up->u_tun_func != NULL) ||
1583 (up->u_icmp_func != NULL))) {
1584 INP_WUNLOCK(inp);
1585 return (EBUSY);
1586 }
1587 up->u_tun_func = f;
1588 up->u_icmp_func = i;
1589 up->u_tun_ctx = ctx;
1590 INP_WUNLOCK(inp);
1591 return (0);
1592 }
1593
1594 #ifdef INET
1595 static int
udp_bind(struct socket * so,struct sockaddr * nam,struct thread * td)1596 udp_bind(struct socket *so, struct sockaddr *nam, struct thread *td)
1597 {
1598 struct inpcb *inp;
1599 struct sockaddr_in *sinp;
1600 int error;
1601
1602 inp = sotoinpcb(so);
1603 KASSERT(inp != NULL, ("udp_bind: inp == NULL"));
1604
1605 sinp = (struct sockaddr_in *)nam;
1606 if (nam->sa_family != AF_INET) {
1607 /*
1608 * Preserve compatibility with old programs.
1609 */
1610 if (nam->sa_family != AF_UNSPEC ||
1611 nam->sa_len < offsetof(struct sockaddr_in, sin_zero) ||
1612 sinp->sin_addr.s_addr != INADDR_ANY)
1613 return (EAFNOSUPPORT);
1614 nam->sa_family = AF_INET;
1615 }
1616 if (nam->sa_len != sizeof(struct sockaddr_in))
1617 return (EINVAL);
1618
1619 INP_WLOCK(inp);
1620 error = in_pcbbind(inp, sinp, V_udp_bind_all_fibs ? 0 : INPBIND_FIB,
1621 td->td_ucred);
1622 INP_WUNLOCK(inp);
1623 return (error);
1624 }
1625
1626 static void
udp_close(struct socket * so)1627 udp_close(struct socket *so)
1628 {
1629 struct inpcb *inp;
1630
1631 inp = sotoinpcb(so);
1632 KASSERT(inp != NULL, ("udp_close: inp == NULL"));
1633 INP_WLOCK(inp);
1634 if (inp->inp_faddr.s_addr != INADDR_ANY) {
1635 in_pcbdisconnect(inp);
1636 inp->inp_laddr.s_addr = INADDR_ANY;
1637 soisdisconnected(so);
1638 }
1639 INP_WUNLOCK(inp);
1640 }
1641
1642 static int
udp_connect(struct socket * so,struct sockaddr * nam,struct thread * td)1643 udp_connect(struct socket *so, struct sockaddr *nam, struct thread *td)
1644 {
1645 struct epoch_tracker et;
1646 struct inpcb *inp;
1647 struct sockaddr_in *sin;
1648 int error;
1649
1650 inp = sotoinpcb(so);
1651 KASSERT(inp != NULL, ("udp_connect: inp == NULL"));
1652
1653 sin = (struct sockaddr_in *)nam;
1654 if (sin->sin_family != AF_INET)
1655 return (EAFNOSUPPORT);
1656 if (sin->sin_len != sizeof(*sin))
1657 return (EINVAL);
1658
1659 INP_WLOCK(inp);
1660 if (inp->inp_faddr.s_addr != INADDR_ANY) {
1661 INP_WUNLOCK(inp);
1662 return (EISCONN);
1663 }
1664 error = prison_remote_ip4(td->td_ucred, &sin->sin_addr);
1665 if (error != 0) {
1666 INP_WUNLOCK(inp);
1667 return (error);
1668 }
1669 NET_EPOCH_ENTER(et);
1670 error = in_pcbconnect(inp, sin, td->td_ucred);
1671 NET_EPOCH_EXIT(et);
1672 if (error == 0)
1673 soisconnected(so);
1674 INP_WUNLOCK(inp);
1675 return (error);
1676 }
1677
1678 static void
udp_detach(struct socket * so)1679 udp_detach(struct socket *so)
1680 {
1681 struct inpcb *inp;
1682
1683 inp = sotoinpcb(so);
1684 KASSERT(inp != NULL, ("udp_detach: inp == NULL"));
1685 KASSERT(inp->inp_faddr.s_addr == INADDR_ANY,
1686 ("udp_detach: not disconnected"));
1687 INP_WLOCK(inp);
1688 in_pcbfree(inp);
1689 }
1690
1691 int
udp_disconnect(struct socket * so)1692 udp_disconnect(struct socket *so)
1693 {
1694 struct inpcb *inp;
1695
1696 inp = sotoinpcb(so);
1697 KASSERT(inp != NULL, ("udp_disconnect: inp == NULL"));
1698 INP_WLOCK(inp);
1699 if (inp->inp_faddr.s_addr == INADDR_ANY) {
1700 INP_WUNLOCK(inp);
1701 return (ENOTCONN);
1702 }
1703 in_pcbdisconnect(inp);
1704 inp->inp_laddr.s_addr = INADDR_ANY;
1705 SOCK_LOCK(so);
1706 so->so_state &= ~SS_ISCONNECTED; /* XXX */
1707 SOCK_UNLOCK(so);
1708 INP_WUNLOCK(inp);
1709 return (0);
1710 }
1711 #endif /* INET */
1712
1713 int
udp_shutdown(struct socket * so,enum shutdown_how how)1714 udp_shutdown(struct socket *so, enum shutdown_how how)
1715 {
1716 int error;
1717
1718 SOCK_LOCK(so);
1719 if (!(so->so_state & SS_ISCONNECTED))
1720 /*
1721 * POSIX mandates us to just return ENOTCONN when shutdown(2) is
1722 * invoked on a datagram sockets, however historically we would
1723 * actually tear socket down. This is known to be leveraged by
1724 * some applications to unblock process waiting in recv(2) by
1725 * other process that it shares that socket with. Try to meet
1726 * both backward-compatibility and POSIX requirements by forcing
1727 * ENOTCONN but still flushing buffers and performing wakeup(9).
1728 *
1729 * XXXGL: it remains unknown what applications expect this
1730 * behavior and is this isolated to unix/dgram or inet/dgram or
1731 * both. See: D10351, D3039.
1732 */
1733 error = ENOTCONN;
1734 else
1735 error = 0;
1736 SOCK_UNLOCK(so);
1737
1738 switch (how) {
1739 case SHUT_RD:
1740 sorflush(so);
1741 break;
1742 case SHUT_RDWR:
1743 sorflush(so);
1744 /* FALLTHROUGH */
1745 case SHUT_WR:
1746 socantsendmore(so);
1747 }
1748
1749 return (error);
1750 }
1751
1752 #ifdef INET
1753 #define UDP_PROTOSW \
1754 .pr_type = SOCK_DGRAM, \
1755 .pr_flags = PR_ATOMIC | PR_ADDR | PR_CAPATTACH, \
1756 .pr_ctloutput = udp_ctloutput, \
1757 .pr_abort = udp_abort, \
1758 .pr_attach = udp_attach, \
1759 .pr_bind = udp_bind, \
1760 .pr_connect = udp_connect, \
1761 .pr_control = in_control, \
1762 .pr_detach = udp_detach, \
1763 .pr_disconnect = udp_disconnect, \
1764 .pr_peeraddr = in_getpeeraddr, \
1765 .pr_send = udp_send, \
1766 .pr_soreceive = soreceive_dgram, \
1767 .pr_sosend = sosend_dgram, \
1768 .pr_shutdown = udp_shutdown, \
1769 .pr_sockaddr = in_getsockaddr, \
1770 .pr_sosetlabel = in_pcbsosetlabel, \
1771 .pr_close = udp_close
1772
1773 struct protosw udp_protosw = {
1774 .pr_protocol = IPPROTO_UDP,
1775 UDP_PROTOSW
1776 };
1777
1778 struct protosw udplite_protosw = {
1779 .pr_protocol = IPPROTO_UDPLITE,
1780 UDP_PROTOSW
1781 };
1782
1783 static void
udp_init(void * arg __unused)1784 udp_init(void *arg __unused)
1785 {
1786
1787 IPPROTO_REGISTER(IPPROTO_UDP, udp_input, udp_ctlinput);
1788 IPPROTO_REGISTER(IPPROTO_UDPLITE, udp_input, udplite_ctlinput);
1789 }
1790 SYSINIT(udp_init, SI_SUB_PROTO_DOMAIN, SI_ORDER_THIRD, udp_init, NULL);
1791 #endif /* INET */
1792