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