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