kernel: Clean up the warning flags a bit.
[dragonfly.git] / sys / netinet / udp_usrreq.c
blobe81c0c335f3923e68dea2c3e8f0b0ed852e7d4b6
1 /*
2 * Copyright (c) 2004 Jeffrey M. Hsu. All rights reserved.
3 * Copyright (c) 2004 The DragonFly Project. All rights reserved.
5 * This code is derived from software contributed to The DragonFly Project
6 * by Jeffrey M. Hsu.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 3. Neither the name of The DragonFly Project nor the names of its
17 * contributors may be used to endorse or promote products derived
18 * from this software without specific, prior written permission.
20 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
23 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
24 * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
25 * INCIDENTAL, SPECIAL, EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING,
26 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
27 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
28 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
29 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
30 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 * SUCH DAMAGE.
35 * Copyright (c) 1982, 1986, 1988, 1990, 1993, 1995
36 * The Regents of the University of California. All rights reserved.
38 * Redistribution and use in source and binary forms, with or without
39 * modification, are permitted provided that the following conditions
40 * are met:
41 * 1. Redistributions of source code must retain the above copyright
42 * notice, this list of conditions and the following disclaimer.
43 * 2. Redistributions in binary form must reproduce the above copyright
44 * notice, this list of conditions and the following disclaimer in the
45 * documentation and/or other materials provided with the distribution.
46 * 3. Neither the name of the University nor the names of its contributors
47 * may be used to endorse or promote products derived from this software
48 * without specific prior written permission.
50 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
51 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
52 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
53 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
54 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
55 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
56 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
57 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
58 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
59 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
60 * SUCH DAMAGE.
62 * @(#)udp_usrreq.c 8.6 (Berkeley) 5/23/95
63 * $FreeBSD: src/sys/netinet/udp_usrreq.c,v 1.64.2.18 2003/01/24 05:11:34 sam Exp $
66 #include "opt_inet6.h"
68 #include <sys/param.h>
69 #include <sys/systm.h>
70 #include <sys/kernel.h>
71 #include <sys/malloc.h>
72 #include <sys/mbuf.h>
73 #include <sys/domain.h>
74 #include <sys/proc.h>
75 #include <sys/priv.h>
76 #include <sys/protosw.h>
77 #include <sys/socket.h>
78 #include <sys/socketvar.h>
79 #include <sys/sysctl.h>
80 #include <sys/syslog.h>
81 #include <sys/in_cksum.h>
82 #include <sys/ktr.h>
83 #include <sys/jail.h>
85 #include <sys/thread2.h>
86 #include <sys/socketvar2.h>
87 #include <sys/serialize.h>
89 #include <machine/stdarg.h>
91 #include <net/if.h>
92 #include <net/route.h>
93 #include <net/netmsg2.h>
94 #include <net/netisr2.h>
96 #include <netinet/in.h>
97 #include <netinet/in_systm.h>
98 #include <netinet/ip.h>
99 #ifdef INET6
100 #include <netinet/ip6.h>
101 #endif
102 #include <netinet/in_pcb.h>
103 #include <netinet/in_var.h>
104 #include <netinet/ip_var.h>
105 #ifdef INET6
106 #include <netinet6/ip6_var.h>
107 #endif
108 #include <netinet/ip_icmp.h>
109 #include <netinet/icmp_var.h>
110 #include <netinet/udp.h>
111 #include <netinet/udp_var.h>
113 #define MSGF_UDP_SEND MSGF_PROTO1
115 #define INP_DIRECT_DETACH INP_FLAG_PROTO2
117 #define UDP_KTR_STRING "inp=%p"
118 #define UDP_KTR_ARGS struct inpcb *inp
120 #ifndef KTR_UDP
121 #define KTR_UDP KTR_ALL
122 #endif
124 KTR_INFO_MASTER(udp);
125 KTR_INFO(KTR_UDP, udp, send_beg, 0, UDP_KTR_STRING, UDP_KTR_ARGS);
126 KTR_INFO(KTR_UDP, udp, send_end, 1, UDP_KTR_STRING, UDP_KTR_ARGS);
127 KTR_INFO(KTR_UDP, udp, send_ipout, 2, UDP_KTR_STRING, UDP_KTR_ARGS);
128 KTR_INFO(KTR_UDP, udp, redisp_ipout_beg, 3, UDP_KTR_STRING, UDP_KTR_ARGS);
129 KTR_INFO(KTR_UDP, udp, redisp_ipout_end, 4, UDP_KTR_STRING, UDP_KTR_ARGS);
130 KTR_INFO(KTR_UDP, udp, send_redisp, 5, UDP_KTR_STRING, UDP_KTR_ARGS);
131 KTR_INFO(KTR_UDP, udp, send_inswildcard, 6, UDP_KTR_STRING, UDP_KTR_ARGS);
133 #define logudp(name, inp) KTR_LOG(udp_##name, inp)
136 * UDP protocol implementation.
137 * Per RFC 768, August, 1980.
139 #ifndef COMPAT_42
140 static int udpcksum = 1;
141 #else
142 static int udpcksum = 0; /* XXX */
143 #endif
144 SYSCTL_INT(_net_inet_udp, UDPCTL_CHECKSUM, checksum, CTLFLAG_RW,
145 &udpcksum, 0, "Enable checksumming of UDP packets");
147 int log_in_vain = 0;
148 SYSCTL_INT(_net_inet_udp, OID_AUTO, log_in_vain, CTLFLAG_RW,
149 &log_in_vain, 0, "Log all incoming UDP packets");
151 static int blackhole = 0;
152 SYSCTL_INT(_net_inet_udp, OID_AUTO, blackhole, CTLFLAG_RW,
153 &blackhole, 0, "Do not send port unreachables for refused connects");
155 static int strict_mcast_mship = 1;
156 SYSCTL_INT(_net_inet_udp, OID_AUTO, strict_mcast_mship, CTLFLAG_RW,
157 &strict_mcast_mship, 0, "Only send multicast to member sockets");
159 int udp_sosend_async = 1;
160 SYSCTL_INT(_net_inet_udp, OID_AUTO, sosend_async, CTLFLAG_RW,
161 &udp_sosend_async, 0, "UDP asynchronized pru_send");
163 int udp_sosend_prepend = 1;
164 SYSCTL_INT(_net_inet_udp, OID_AUTO, sosend_prepend, CTLFLAG_RW,
165 &udp_sosend_prepend, 0,
166 "Prepend enough space for proto and link header in pru_send");
168 static int udp_reuseport_ext = 1;
169 SYSCTL_INT(_net_inet_udp, OID_AUTO, reuseport_ext, CTLFLAG_RW,
170 &udp_reuseport_ext, 0, "SO_REUSEPORT extension");
172 struct inpcbinfo udbinfo[MAXCPU];
174 #ifndef UDBHASHSIZE
175 #define UDBHASHSIZE 16
176 #endif
177 CTASSERT((UDBHASHSIZE & (UDBHASHSIZE - 1)) == 0);
179 struct udpstat udpstat_percpu[MAXCPU] __cachealign;
181 static void udp_append(struct inpcb *last, struct ip *ip,
182 struct mbuf *n, int off, struct sockaddr_in *udp_in);
184 static int udp_connect_oncpu(struct inpcb *inp, struct sockaddr_in *sin,
185 struct sockaddr_in *if_sin, uint16_t hash);
187 static boolean_t udp_inswildcardhash(struct inpcb *inp,
188 struct netmsg_base *msg, int error);
189 static void udp_remwildcardhash(struct inpcb *inp);
191 static __inline int
192 udp_lportcpu(short lport)
194 return (ntohs(lport) % netisr_ncpus);
197 void
198 udp_init(void)
200 struct inpcbportinfo *portinfo;
201 int cpu;
203 portinfo = kmalloc_cachealign(sizeof(*portinfo) * netisr_ncpus, M_PCB,
204 M_WAITOK);
206 for (cpu = 0; cpu < netisr_ncpus; cpu++) {
207 struct inpcbinfo *uicb = &udbinfo[cpu];
210 * NOTE:
211 * UDP pcb list, wildcard hash table and localgroup hash
212 * table are shared.
214 in_pcbinfo_init(uicb, cpu, TRUE);
215 uicb->hashbase = hashinit(UDBHASHSIZE, M_PCB, &uicb->hashmask);
217 in_pcbportinfo_init(&portinfo[cpu], UDBHASHSIZE, cpu);
218 in_pcbportinfo_set(uicb, portinfo, netisr_ncpus);
220 uicb->wildcardhashbase = hashinit(UDBHASHSIZE, M_PCB,
221 &uicb->wildcardhashmask);
222 uicb->localgrphashbase = hashinit(UDBHASHSIZE, M_PCB,
223 &uicb->localgrphashmask);
225 uicb->ipi_size = sizeof(struct inpcb);
229 * Initialize UDP statistics counters for each CPU.
231 for (cpu = 0; cpu < netisr_ncpus; ++cpu)
232 bzero(&udpstat_percpu[cpu], sizeof(struct udpstat));
235 static int
236 sysctl_udpstat(SYSCTL_HANDLER_ARGS)
238 int cpu, error = 0;
240 for (cpu = 0; cpu < netisr_ncpus; ++cpu) {
241 if ((error = SYSCTL_OUT(req, &udpstat_percpu[cpu],
242 sizeof(struct udpstat))))
243 break;
244 if ((error = SYSCTL_IN(req, &udpstat_percpu[cpu],
245 sizeof(struct udpstat))))
246 break;
249 return (error);
251 SYSCTL_PROC(_net_inet_udp, UDPCTL_STATS, stats, (CTLTYPE_OPAQUE | CTLFLAG_RW),
252 0, 0, sysctl_udpstat, "S,udpstat", "UDP statistics");
254 void
255 udp_ctloutput(netmsg_t msg)
257 struct socket *so = msg->base.nm_so;
258 struct sockopt *sopt = msg->ctloutput.nm_sopt;
259 struct inpcb *inp = so->so_pcb;
261 if (inp == NULL) {
262 lwkt_replymsg(&msg->lmsg, EINVAL);
263 return;
266 if (sopt->sopt_level == IPPROTO_IP && sopt->sopt_dir == SOPT_SET) {
267 switch (sopt->sopt_name) {
268 case IP_MULTICAST_IF:
269 case IP_MULTICAST_VIF:
270 case IP_MULTICAST_TTL:
271 case IP_MULTICAST_LOOP:
272 case IP_ADD_MEMBERSHIP:
273 case IP_DROP_MEMBERSHIP:
275 * This pr_ctloutput msg will be forwarded
276 * to netisr0 to run; we can't do direct
277 * detaching anymore.
279 * NOTE:
280 * Don't optimize for the sockets whose
281 * current so_port is netisr0's msgport.
282 * These sockets could be connect(2)'ed
283 * later and the so_port will be changed.
285 inp->inp_flags &= ~INP_DIRECT_DETACH;
286 break;
289 return ip_ctloutput(msg);
293 * Check multicast packets to make sure they are only sent to sockets with
294 * multicast memberships for the packet's destination address and arrival
295 * interface. Multicast packets to multicast-unaware sockets are also
296 * disallowed.
298 * Returns 0 if the packet is acceptable, -1 if it is not.
300 static __inline int
301 check_multicast_membership(const struct ip *ip, const struct inpcb *inp,
302 const struct mbuf *m)
304 const struct ip_moptions *mopt;
305 int mshipno;
307 if (strict_mcast_mship == 0 ||
308 !IN_MULTICAST(ntohl(ip->ip_dst.s_addr))) {
309 return (0);
312 ASSERT_NETISR0;
314 mopt = inp->inp_moptions;
315 if (mopt == NULL)
316 return (-1);
317 for (mshipno = 0; mshipno < mopt->imo_num_memberships; ++mshipno) {
318 const struct in_multi *maddr = mopt->imo_membership[mshipno];
320 if (ip->ip_dst.s_addr == maddr->inm_addr.s_addr &&
321 m->m_pkthdr.rcvif == maddr->inm_ifp) {
322 return (0);
325 return (-1);
328 struct udp_mcast_arg {
329 struct inpcb *inp;
330 struct inpcb *last;
331 struct ip *ip;
332 struct mbuf *m;
333 int iphlen;
334 struct sockaddr_in *udp_in;
337 static int
338 udp_mcast_input(struct udp_mcast_arg *arg)
340 struct inpcb *inp = arg->inp;
341 struct inpcb *last = arg->last;
342 struct ip *ip = arg->ip;
343 struct mbuf *m = arg->m;
345 if (check_multicast_membership(ip, inp, m) < 0)
346 return ERESTART; /* caller continue */
348 if (last != NULL) {
349 struct mbuf *n;
351 if ((n = m_copypacket(m, M_NOWAIT)) != NULL)
352 udp_append(last, ip, n,
353 arg->iphlen + sizeof(struct udphdr),
354 arg->udp_in);
356 arg->last = last = inp;
359 * Don't look for additional matches if this one does
360 * not have either the SO_REUSEPORT or SO_REUSEADDR
361 * socket options set. This heuristic avoids searching
362 * through all pcbs in the common case of a non-shared
363 * port. It * assumes that an application will never
364 * clear these options after setting them.
366 if (!(last->inp_socket->so_options &
367 (SO_REUSEPORT | SO_REUSEADDR)))
368 return EJUSTRETURN; /* caller stop */
369 return 0;
373 udp_input(struct mbuf **mp, int *offp, int proto)
375 struct sockaddr_in udp_in = { sizeof udp_in, AF_INET };
376 int iphlen;
377 struct ip *ip;
378 struct udphdr *uh;
379 struct inpcb *inp;
380 struct mbuf *m;
381 struct mbuf *opts = NULL;
382 int len, off;
383 struct ip save_ip;
384 struct inpcbinfo *pcbinfo = &udbinfo[mycpuid];
386 off = *offp;
387 m = *mp;
388 *mp = NULL;
390 iphlen = off;
391 udp_stat.udps_ipackets++;
394 * Strip IP options, if any; should skip this,
395 * make available to user, and use on returned packets,
396 * but we don't yet have a way to check the checksum
397 * with options still present.
399 if (iphlen > sizeof(struct ip)) {
400 ip_stripoptions(m);
401 iphlen = sizeof(struct ip);
405 * IP and UDP headers are together in first mbuf.
406 * Already checked and pulled up in ip_demux().
408 KASSERT(m->m_len >= iphlen + sizeof(struct udphdr),
409 ("UDP header not in one mbuf"));
411 ip = mtod(m, struct ip *);
412 uh = (struct udphdr *)((caddr_t)ip + iphlen);
414 /* destination port of 0 is illegal, based on RFC768. */
415 if (uh->uh_dport == 0)
416 goto bad;
419 * Make mbuf data length reflect UDP length.
420 * If not enough data to reflect UDP length, drop.
422 len = ntohs((u_short)uh->uh_ulen);
423 if (ip->ip_len != len) {
424 if (len > ip->ip_len || len < sizeof(struct udphdr)) {
425 udp_stat.udps_badlen++;
426 goto bad;
428 m_adj(m, len - ip->ip_len);
429 /* ip->ip_len = len; */
432 * Save a copy of the IP header in case we want restore it
433 * for sending an ICMP error message in response.
435 save_ip = *ip;
438 * Checksum extended UDP header and data.
440 if (uh->uh_sum) {
441 if (m->m_pkthdr.csum_flags & CSUM_DATA_VALID) {
442 if (m->m_pkthdr.csum_flags & CSUM_PSEUDO_HDR)
443 uh->uh_sum = m->m_pkthdr.csum_data;
444 else
445 uh->uh_sum = in_pseudo(ip->ip_src.s_addr,
446 ip->ip_dst.s_addr, htonl((u_short)len +
447 m->m_pkthdr.csum_data + IPPROTO_UDP));
448 uh->uh_sum ^= 0xffff;
449 } else {
450 char b[9];
452 bcopy(((struct ipovly *)ip)->ih_x1, b, 9);
453 bzero(((struct ipovly *)ip)->ih_x1, 9);
454 ((struct ipovly *)ip)->ih_len = uh->uh_ulen;
455 uh->uh_sum = in_cksum(m, len + sizeof(struct ip));
456 bcopy(b, ((struct ipovly *)ip)->ih_x1, 9);
458 if (uh->uh_sum) {
459 udp_stat.udps_badsum++;
460 m_freem(m);
461 return(IPPROTO_DONE);
463 } else
464 udp_stat.udps_nosum++;
466 if (IN_MULTICAST(ntohl(ip->ip_dst.s_addr)) ||
467 in_broadcast(ip->ip_dst, m->m_pkthdr.rcvif)) {
468 struct inpcbhead *connhead;
469 struct inpcontainer *ic, *ic_marker;
470 struct inpcontainerhead *ichead;
471 struct udp_mcast_arg arg;
472 struct inpcb *last;
473 int error;
476 * Deliver a multicast or broadcast datagram to *all* sockets
477 * for which the local and remote addresses and ports match
478 * those of the incoming datagram. This allows more than
479 * one process to receive multi/broadcasts on the same port.
480 * (This really ought to be done for unicast datagrams as
481 * well, but that would cause problems with existing
482 * applications that open both address-specific sockets and
483 * a wildcard socket listening to the same port -- they would
484 * end up receiving duplicates of every unicast datagram.
485 * Those applications open the multiple sockets to overcome an
486 * inadequacy of the UDP socket interface, but for backwards
487 * compatibility we avoid the problem here rather than
488 * fixing the interface. Maybe 4.5BSD will remedy this?)
492 * Construct sockaddr format source address.
494 udp_in.sin_port = uh->uh_sport;
495 udp_in.sin_addr = ip->ip_src;
496 arg.udp_in = &udp_in;
498 * Locate pcb(s) for datagram.
499 * (Algorithm copied from raw_intr().)
501 last = NULL;
502 arg.iphlen = iphlen;
504 connhead = &pcbinfo->hashbase[
505 INP_PCBCONNHASH(ip->ip_src.s_addr, uh->uh_sport,
506 ip->ip_dst.s_addr, uh->uh_dport, pcbinfo->hashmask)];
507 LIST_FOREACH(inp, connhead, inp_hash) {
508 #ifdef INET6
509 if (!INP_ISIPV4(inp))
510 continue;
511 #endif
512 if (!in_hosteq(inp->inp_faddr, ip->ip_src) ||
513 !in_hosteq(inp->inp_laddr, ip->ip_dst) ||
514 inp->inp_fport != uh->uh_sport ||
515 inp->inp_lport != uh->uh_dport)
516 continue;
518 arg.inp = inp;
519 arg.last = last;
520 arg.ip = ip;
521 arg.m = m;
523 error = udp_mcast_input(&arg);
524 if (error == ERESTART)
525 continue;
526 last = arg.last;
528 if (error == EJUSTRETURN)
529 goto done;
532 ichead = &pcbinfo->wildcardhashbase[
533 INP_PCBWILDCARDHASH(uh->uh_dport,
534 pcbinfo->wildcardhashmask)];
535 ic_marker = in_pcbcontainer_marker();
537 GET_PCBINFO_TOKEN(pcbinfo);
538 LIST_INSERT_HEAD(ichead, ic_marker, ic_list);
539 while ((ic = LIST_NEXT(ic_marker, ic_list)) != NULL) {
540 LIST_REMOVE(ic_marker, ic_list);
541 LIST_INSERT_AFTER(ic, ic_marker, ic_list);
543 inp = ic->ic_inp;
544 if (inp->inp_flags & INP_PLACEMARKER)
545 continue;
546 #ifdef INET6
547 if (!INP_ISIPV4(inp))
548 continue;
549 #endif
550 if (inp->inp_lport != uh->uh_dport)
551 continue;
552 if (inp->inp_laddr.s_addr != INADDR_ANY &&
553 inp->inp_laddr.s_addr != ip->ip_dst.s_addr)
554 continue;
556 arg.inp = inp;
557 arg.last = last;
558 arg.ip = ip;
559 arg.m = m;
561 error = udp_mcast_input(&arg);
562 if (error == ERESTART)
563 continue;
564 last = arg.last;
566 if (error == EJUSTRETURN)
567 break;
569 LIST_REMOVE(ic_marker, ic_list);
570 REL_PCBINFO_TOKEN(pcbinfo);
571 done:
572 if (last == NULL) {
574 * No matching pcb found; discard datagram.
575 * (No need to send an ICMP Port Unreachable
576 * for a broadcast or multicast datgram.)
578 udp_stat.udps_noportbcast++;
579 goto bad;
581 udp_append(last, ip, m, iphlen + sizeof(struct udphdr),
582 &udp_in);
583 return(IPPROTO_DONE);
586 * Locate pcb for datagram.
588 inp = in_pcblookup_pkthash(pcbinfo, ip->ip_src, uh->uh_sport,
589 ip->ip_dst, uh->uh_dport, TRUE, m->m_pkthdr.rcvif,
590 udp_reuseport_ext ? m : NULL);
591 if (inp == NULL) {
592 if (log_in_vain) {
593 char src[INET_ADDRSTRLEN], dst[INET_ADDRSTRLEN];
595 log(LOG_INFO,
596 "Connection attempt to UDP %s:%d from %s:%d\n",
597 kinet_ntoa(ip->ip_dst, dst), ntohs(uh->uh_dport),
598 kinet_ntoa(ip->ip_src, src), ntohs(uh->uh_sport));
600 udp_stat.udps_noport++;
601 if (m->m_flags & (M_BCAST | M_MCAST)) {
602 udp_stat.udps_noportbcast++;
603 goto bad;
605 if (blackhole)
606 goto bad;
607 #ifdef ICMP_BANDLIM
608 if (badport_bandlim(BANDLIM_ICMP_UNREACH) < 0)
609 goto bad;
610 #endif
611 *ip = save_ip;
612 ip->ip_len += iphlen;
613 icmp_error(m, ICMP_UNREACH, ICMP_UNREACH_PORT, 0, 0);
614 return(IPPROTO_DONE);
616 KASSERT(INP_ISIPV4(inp), ("not inet inpcb"));
618 * Check the minimum TTL for socket.
620 if (ip->ip_ttl < inp->inp_ip_minttl)
621 goto bad;
624 * Construct sockaddr format source address.
625 * Stuff source address and datagram in user buffer.
627 udp_in.sin_port = uh->uh_sport;
628 udp_in.sin_addr = ip->ip_src;
629 if ((inp->inp_flags & INP_CONTROLOPTS) ||
630 (inp->inp_socket->so_options & SO_TIMESTAMP))
631 ip_savecontrol(inp, &opts, ip, m);
632 m_adj(m, iphlen + sizeof(struct udphdr));
634 lwkt_gettoken(&inp->inp_socket->so_rcv.ssb_token);
635 if (ssb_appendaddr(&inp->inp_socket->so_rcv,
636 (struct sockaddr *)&udp_in, m, opts) == 0) {
637 lwkt_reltoken(&inp->inp_socket->so_rcv.ssb_token);
638 udp_stat.udps_fullsock++;
639 goto bad;
641 lwkt_reltoken(&inp->inp_socket->so_rcv.ssb_token);
642 sorwakeup(inp->inp_socket);
643 return(IPPROTO_DONE);
644 bad:
645 m_freem(m);
646 if (opts)
647 m_freem(opts);
648 return(IPPROTO_DONE);
652 * subroutine of udp_input(), mainly for source code readability.
653 * caller must properly init udp_ip6 and udp_in6 beforehand.
655 static void
656 udp_append(struct inpcb *last, struct ip *ip, struct mbuf *n, int off,
657 struct sockaddr_in *udp_in)
659 struct mbuf *opts = NULL;
660 int ret;
662 KASSERT(INP_ISIPV4(last), ("not inet inpcb"));
664 if (last->inp_flags & INP_CONTROLOPTS ||
665 last->inp_socket->so_options & SO_TIMESTAMP)
666 ip_savecontrol(last, &opts, ip, n);
667 m_adj(n, off);
669 lwkt_gettoken(&last->inp_socket->so_rcv.ssb_token);
670 ret = ssb_appendaddr(&last->inp_socket->so_rcv,
671 (struct sockaddr *)udp_in, n, opts);
672 lwkt_reltoken(&last->inp_socket->so_rcv.ssb_token);
673 if (ret == 0) {
674 m_freem(n);
675 if (opts)
676 m_freem(opts);
677 udp_stat.udps_fullsock++;
678 } else {
679 sorwakeup(last->inp_socket);
684 * Notify a udp user of an asynchronous error;
685 * just wake up so that he can collect error status.
687 void
688 udp_notify(struct inpcb *inp, int error)
690 inp->inp_socket->so_error = error;
691 sorwakeup(inp->inp_socket);
692 sowwakeup(inp->inp_socket);
695 struct netmsg_udp_notify {
696 struct netmsg_base base;
697 inp_notify_t nm_notify;
698 struct in_addr nm_faddr;
699 int nm_arg;
702 static void
703 udp_notifyall_oncpu(netmsg_t msg)
705 struct netmsg_udp_notify *nm = (struct netmsg_udp_notify *)msg;
706 int nextcpu, cpu = mycpuid;
708 ASSERT_NETISR_NCPUS(cpu);
710 in_pcbnotifyall(&udbinfo[cpu], nm->nm_faddr, nm->nm_arg, nm->nm_notify);
712 nextcpu = cpu + 1;
713 if (nextcpu < netisr_ncpus)
714 lwkt_forwardmsg(netisr_cpuport(nextcpu), &nm->base.lmsg);
715 else
716 lwkt_replymsg(&nm->base.lmsg, 0);
719 inp_notify_t
720 udp_get_inpnotify(int cmd, const struct sockaddr *sa,
721 struct ip **ip0, int *cpuid)
723 struct in_addr faddr;
724 struct ip *ip = *ip0;
725 inp_notify_t notify = udp_notify;
727 faddr = ((const struct sockaddr_in *)sa)->sin_addr;
728 if (sa->sa_family != AF_INET || faddr.s_addr == INADDR_ANY)
729 return NULL;
731 if (PRC_IS_REDIRECT(cmd)) {
732 ip = NULL;
733 notify = in_rtchange;
734 } else if (cmd == PRC_HOSTDEAD) {
735 ip = NULL;
736 } else if ((unsigned)cmd >= PRC_NCMDS || inetctlerrmap[cmd] == 0) {
737 return NULL;
740 if (cpuid != NULL) {
741 if (ip == NULL) {
742 /* Go through all effective netisr CPUs. */
743 *cpuid = netisr_ncpus;
744 } else {
745 const struct udphdr *uh;
747 uh = (const struct udphdr *)
748 ((caddr_t)ip + (ip->ip_hl << 2));
749 *cpuid = udp_addrcpu(faddr.s_addr, uh->uh_dport,
750 ip->ip_src.s_addr, uh->uh_sport);
754 *ip0 = ip;
755 return notify;
758 void
759 udp_ctlinput(netmsg_t msg)
761 struct sockaddr *sa = msg->ctlinput.nm_arg;
762 struct ip *ip = msg->ctlinput.nm_extra;
763 int cmd = msg->ctlinput.nm_cmd, cpuid;
764 inp_notify_t notify;
765 struct in_addr faddr;
767 ASSERT_NETISR_NCPUS(mycpuid);
769 notify = udp_get_inpnotify(cmd, sa, &ip, &cpuid);
770 if (notify == NULL)
771 goto done;
773 faddr = ((struct sockaddr_in *)sa)->sin_addr;
774 if (ip) {
775 const struct udphdr *uh;
776 struct inpcb *inp;
778 if (cpuid != mycpuid)
779 goto done;
781 uh = (const struct udphdr *)((caddr_t)ip + (ip->ip_hl << 2));
782 inp = in_pcblookup_hash(&udbinfo[mycpuid], faddr, uh->uh_dport,
783 ip->ip_src, uh->uh_sport, 0, NULL);
784 if (inp != NULL && inp->inp_socket != NULL)
785 notify(inp, inetctlerrmap[cmd]);
786 } else if (msg->ctlinput.nm_direct) {
787 if (cpuid != netisr_ncpus && cpuid != mycpuid)
788 goto done;
790 in_pcbnotifyall(&udbinfo[mycpuid], faddr, inetctlerrmap[cmd],
791 notify);
792 } else {
793 struct netmsg_udp_notify *nm;
795 ASSERT_NETISR0;
796 nm = kmalloc(sizeof(*nm), M_LWKTMSG, M_INTWAIT);
797 netmsg_init(&nm->base, NULL, &netisr_afree_rport,
798 0, udp_notifyall_oncpu);
799 nm->nm_faddr = faddr;
800 nm->nm_arg = inetctlerrmap[cmd];
801 nm->nm_notify = notify;
802 lwkt_sendmsg(netisr_cpuport(0), &nm->base.lmsg);
804 done:
805 lwkt_replymsg(&msg->lmsg, 0);
808 SYSCTL_PROC(_net_inet_udp, UDPCTL_PCBLIST, pcblist, CTLFLAG_RD, udbinfo, 0,
809 in_pcblist_ncpus, "S,xinpcb", "List of active UDP sockets");
811 static int
812 udp_getcred(SYSCTL_HANDLER_ARGS)
814 struct sockaddr_in addrs[2];
815 struct ucred cred0, *cred = NULL;
816 struct inpcb *inp;
817 int error, cpu, origcpu;
819 error = priv_check(req->td, PRIV_ROOT);
820 if (error)
821 return (error);
822 error = SYSCTL_IN(req, addrs, sizeof addrs);
823 if (error)
824 return (error);
826 origcpu = mycpuid;
827 cpu = udp_addrcpu(addrs[1].sin_addr.s_addr, addrs[1].sin_port,
828 addrs[0].sin_addr.s_addr, addrs[0].sin_port);
830 lwkt_migratecpu(cpu);
832 inp = in_pcblookup_hash(&udbinfo[cpu],
833 addrs[1].sin_addr, addrs[1].sin_port,
834 addrs[0].sin_addr, addrs[0].sin_port, TRUE, NULL);
835 if (inp == NULL || inp->inp_socket == NULL) {
836 error = ENOENT;
837 } else if (inp->inp_socket->so_cred != NULL) {
838 cred0 = *(inp->inp_socket->so_cred);
839 cred = &cred0;
842 lwkt_migratecpu(origcpu);
844 if (error)
845 return error;
847 return SYSCTL_OUT(req, cred, sizeof(struct ucred));
849 SYSCTL_PROC(_net_inet_udp, OID_AUTO, getcred, CTLTYPE_OPAQUE|CTLFLAG_RW,
850 0, 0, udp_getcred, "S,ucred", "Get the ucred of a UDP connection");
852 static void
853 udp_send_redispatch(netmsg_t msg)
855 struct mbuf *m = msg->send.nm_m;
856 int pru_flags = msg->send.nm_flags;
857 struct inpcb *inp = msg->send.base.nm_so->so_pcb;
858 struct mbuf *m_opt = msg->send.nm_control; /* XXX save ipopt */
859 int flags = msg->send.nm_priv; /* ip_output flags */
860 int error;
862 logudp(redisp_ipout_beg, inp);
865 * - Don't use inp route cache. It should only be used in the
866 * inp owner netisr.
867 * - Access to inp_moptions should be safe, since multicast UDP
868 * datagrams are redispatched to netisr0 and inp_moptions is
869 * changed only in netisr0.
871 error = ip_output(m, m_opt, NULL, flags, inp->inp_moptions, inp);
872 if ((pru_flags & PRUS_NOREPLY) == 0)
873 lwkt_replymsg(&msg->send.base.lmsg, error);
875 if (m_opt != NULL) {
876 /* Free saved ip options, if any */
877 m_freem(m_opt);
880 logudp(redisp_ipout_end, inp);
883 static void
884 udp_send(netmsg_t msg)
886 struct socket *so = msg->send.base.nm_so;
887 struct mbuf *m = msg->send.nm_m;
888 struct sockaddr *dstaddr = msg->send.nm_addr;
889 int pru_flags = msg->send.nm_flags;
890 struct inpcb *inp = so->so_pcb;
891 struct thread *td = msg->send.nm_td;
892 uint16_t hash;
893 int flags;
895 struct udpiphdr *ui;
896 int len = m->m_pkthdr.len;
897 struct sockaddr_in *sin; /* really is initialized before use */
898 int error = 0, cpu;
900 KKASSERT(msg->send.nm_control == NULL);
902 logudp(send_beg, inp);
904 if (inp == NULL) {
905 error = EINVAL;
906 goto release;
909 if (len + sizeof(struct udpiphdr) > IP_MAXPACKET) {
910 error = EMSGSIZE;
911 goto release;
914 if (inp->inp_lport == 0) { /* unbound socket */
915 boolean_t forwarded;
917 error = in_pcbbind(inp, NULL, td);
918 if (error)
919 goto release;
922 * Need to call udp_send again, after this inpcb is
923 * inserted into wildcard hash table.
925 msg->send.base.lmsg.ms_flags |= MSGF_UDP_SEND;
926 forwarded = udp_inswildcardhash(inp, &msg->send.base, 0);
927 if (forwarded) {
929 * The message is further forwarded, so we are
930 * done here.
932 logudp(send_inswildcard, inp);
933 return;
937 if (dstaddr != NULL) { /* destination address specified */
938 if (inp->inp_faddr.s_addr != INADDR_ANY) {
939 /* already connected */
940 error = EISCONN;
941 goto release;
943 sin = (struct sockaddr_in *)dstaddr;
944 if (!prison_remote_ip(td, (struct sockaddr *)&sin)) {
945 error = EAFNOSUPPORT; /* IPv6 only jail */
946 goto release;
948 } else {
949 if (inp->inp_faddr.s_addr == INADDR_ANY) {
950 /* no destination specified and not already connected */
951 error = ENOTCONN;
952 goto release;
954 sin = NULL;
958 * Calculate data length and get a mbuf
959 * for UDP and IP headers.
961 M_PREPEND(m, sizeof(struct udpiphdr), M_NOWAIT);
962 if (m == NULL) {
963 error = ENOBUFS;
964 goto release;
968 * Fill in mbuf with extended UDP header
969 * and addresses and length put into network format.
971 ui = mtod(m, struct udpiphdr *);
972 bzero(ui->ui_x1, sizeof ui->ui_x1); /* XXX still needed? */
973 ui->ui_pr = IPPROTO_UDP;
976 * Set destination address.
978 if (dstaddr != NULL) { /* use specified destination */
979 ui->ui_dst = sin->sin_addr;
980 ui->ui_dport = sin->sin_port;
981 } else { /* use connected destination */
982 ui->ui_dst = inp->inp_faddr;
983 ui->ui_dport = inp->inp_fport;
987 * Set source address.
989 if (inp->inp_laddr.s_addr == INADDR_ANY ||
990 IN_MULTICAST(ntohl(inp->inp_laddr.s_addr))) {
991 struct sockaddr_in *if_sin;
993 if (dstaddr == NULL) {
995 * connect() had (or should have) failed because
996 * the interface had no IP address, but the
997 * application proceeded to call send() anyways.
999 error = ENOTCONN;
1000 goto release;
1003 /* Look up outgoing interface. */
1004 error = in_pcbladdr_find(inp, dstaddr, &if_sin, td, 1);
1005 if (error)
1006 goto release;
1007 ui->ui_src = if_sin->sin_addr; /* use address of interface */
1008 } else {
1009 ui->ui_src = inp->inp_laddr; /* use non-null bound address */
1011 ui->ui_sport = inp->inp_lport;
1012 KASSERT(inp->inp_lport != 0, ("inp lport should have been bound"));
1015 * Release the original thread, since it is no longer used
1017 if (pru_flags & PRUS_HELDTD) {
1018 lwkt_rele(td);
1019 pru_flags &= ~PRUS_HELDTD;
1022 * Free the dest address, since it is no longer needed
1024 if (pru_flags & PRUS_FREEADDR) {
1025 kfree(dstaddr, M_SONAME);
1026 pru_flags &= ~PRUS_FREEADDR;
1029 ui->ui_ulen = htons((u_short)len + sizeof(struct udphdr));
1032 * Set up checksum and output datagram.
1034 if (udpcksum) {
1035 ui->ui_sum = in_pseudo(ui->ui_src.s_addr, ui->ui_dst.s_addr,
1036 htons((u_short)len + sizeof(struct udphdr) + IPPROTO_UDP));
1037 m->m_pkthdr.csum_flags = CSUM_UDP;
1038 m->m_pkthdr.csum_data = offsetof(struct udphdr, uh_sum);
1039 m->m_pkthdr.csum_thlen = sizeof(struct udphdr);
1040 } else {
1041 ui->ui_sum = 0;
1043 ((struct ip *)ui)->ip_len = sizeof(struct udpiphdr) + len;
1044 ((struct ip *)ui)->ip_ttl = inp->inp_ip_ttl; /* XXX */
1045 ((struct ip *)ui)->ip_tos = inp->inp_ip_tos; /* XXX */
1046 udp_stat.udps_opackets++;
1048 flags = IP_DEBUGROUTE |
1049 (inp->inp_socket->so_options & (SO_DONTROUTE | SO_BROADCAST));
1050 if (pru_flags & PRUS_DONTROUTE)
1051 flags |= SO_DONTROUTE;
1053 if (inp->inp_flags & INP_CONNECTED) {
1055 * For connected socket, this datagram has already
1056 * been in the correct netisr; no need to rehash.
1058 KASSERT(inp->inp_flags & INP_HASH, ("inpcb has no hash"));
1059 m_sethash(m, inp->inp_hashval);
1060 goto sendit;
1063 hash = udp_addrhash(ui->ui_dst.s_addr, ui->ui_dport,
1064 ui->ui_src.s_addr, ui->ui_sport);
1065 m_sethash(m, hash);
1067 cpu = netisr_hashcpu(hash);
1068 if (cpu != mycpuid) {
1069 struct mbuf *m_opt = NULL;
1070 struct netmsg_pru_send *smsg;
1071 struct lwkt_port *port = netisr_cpuport(cpu);
1074 * Not on the CPU that matches this UDP datagram hash;
1075 * redispatch to the correct CPU to do the ip_output().
1077 if (inp->inp_options != NULL) {
1079 * If there are ip options, then save a copy,
1080 * since accessing inp_options on other CPUs'
1081 * is not safe.
1083 * XXX optimize this?
1085 m_opt = m_copym(inp->inp_options, 0, M_COPYALL,
1086 M_WAITOK);
1088 if ((pru_flags & PRUS_NOREPLY) == 0) {
1090 * Change some parts of the original netmsg and
1091 * forward it to the target netisr.
1093 * NOTE: so_port MUST NOT be checked in the target
1094 * netisr.
1096 smsg = &msg->send;
1097 smsg->nm_priv = flags; /* ip_output flags */
1098 smsg->nm_m = m;
1099 smsg->nm_control = m_opt; /* XXX save ipopt */
1100 smsg->base.lmsg.ms_flags |= MSGF_IGNSOPORT;
1101 smsg->base.nm_dispatch = udp_send_redispatch;
1102 lwkt_forwardmsg(port, &smsg->base.lmsg);
1103 } else {
1105 * Recreate the netmsg, since the original mbuf
1106 * could have been changed. And send it to the
1107 * target netisr.
1109 * NOTE: so_port MUST NOT be checked in the target
1110 * netisr.
1112 smsg = &m->m_hdr.mh_sndmsg;
1113 netmsg_init(&smsg->base, so, &netisr_apanic_rport,
1114 MSGF_IGNSOPORT, udp_send_redispatch);
1115 smsg->nm_priv = flags; /* ip_output flags */
1116 smsg->nm_flags = pru_flags;
1117 smsg->nm_m = m;
1118 smsg->nm_control = m_opt; /* XXX save ipopt */
1119 lwkt_sendmsg(port, &smsg->base.lmsg);
1122 /* This UDP datagram is redispatched; done */
1123 logudp(send_redisp, inp);
1124 return;
1127 sendit:
1128 logudp(send_ipout, inp);
1129 error = ip_output(m, inp->inp_options, &inp->inp_route, flags,
1130 inp->inp_moptions, inp);
1131 m = NULL;
1133 release:
1134 if (m != NULL)
1135 m_freem(m);
1137 if (pru_flags & PRUS_HELDTD)
1138 lwkt_rele(td);
1139 if (pru_flags & PRUS_FREEADDR)
1140 kfree(dstaddr, M_SONAME);
1141 if ((pru_flags & PRUS_NOREPLY) == 0)
1142 lwkt_replymsg(&msg->send.base.lmsg, error);
1144 logudp(send_end, inp);
1147 u_long udp_sendspace = 9216; /* really max datagram size */
1148 /* 40 1K datagrams */
1149 SYSCTL_INT(_net_inet_udp, UDPCTL_MAXDGRAM, maxdgram, CTLFLAG_RW,
1150 &udp_sendspace, 0, "Maximum outgoing UDP datagram size");
1152 u_long udp_recvspace = 40 * (1024 +
1153 #ifdef INET6
1154 sizeof(struct sockaddr_in6)
1155 #else
1156 sizeof(struct sockaddr_in)
1157 #endif
1159 SYSCTL_INT(_net_inet_udp, UDPCTL_RECVSPACE, recvspace, CTLFLAG_RW,
1160 &udp_recvspace, 0, "Maximum incoming UDP datagram size");
1163 * This should never happen, since UDP socket does not support
1164 * connection acception (SO_ACCEPTCONN, i.e. listen(2)).
1166 static void
1167 udp_abort(netmsg_t msg __unused)
1169 panic("udp_abort is called");
1172 static int
1173 udp_preattach(struct socket *so, int proto __unused, struct pru_attach_info *ai)
1175 return soreserve(so, udp_sendspace, udp_recvspace, ai->sb_rlimit);
1178 static void
1179 udp_attach(netmsg_t msg)
1181 struct socket *so = msg->attach.base.nm_so;
1182 struct pru_attach_info *ai = msg->attach.nm_ai;
1183 struct inpcb *inp;
1184 int error;
1186 KASSERT(so->so_pcb == NULL, ("udp socket attached"));
1188 if (ai != NULL) {
1189 error = udp_preattach(so, 0 /* don't care */, ai);
1190 if (error)
1191 goto out;
1192 } else {
1193 /* Post attach; do nothing */
1196 error = in_pcballoc(so, &udbinfo[mycpuid]);
1197 if (error)
1198 goto out;
1200 inp = so->so_pcb;
1201 inp->inp_flags |= INP_DIRECT_DETACH;
1202 inp->inp_ip_ttl = ip_defttl;
1203 error = 0;
1204 out:
1205 lwkt_replymsg(&msg->attach.base.lmsg, error);
1208 static void
1209 udp_inswildcard_replymsg(netmsg_t msg)
1211 lwkt_msg_t lmsg = &msg->lmsg;
1213 if (lmsg->ms_flags & MSGF_UDP_SEND) {
1214 udp_send(msg);
1215 /* msg is replied by udp_send() */
1216 } else {
1217 lwkt_replymsg(lmsg, lmsg->ms_error);
1221 static void
1222 udp_soreuseport_dispatch(netmsg_t msg)
1224 /* This inpcb has already been in the wildcard hash. */
1225 in_pcblink_flags(msg->base.nm_so->so_pcb, &udbinfo[mycpuid], 0);
1226 udp_inswildcard_replymsg(msg);
1229 static void
1230 udp_sosetport(struct lwkt_msg *msg, lwkt_port_t port)
1232 sosetport(((struct netmsg_base *)msg)->nm_so, port);
1235 static boolean_t
1236 udp_inswildcardhash_oncpu(struct inpcb *inp, struct netmsg_base *msg)
1238 int cpu;
1240 KASSERT(inp->inp_pcbinfo == &udbinfo[mycpuid],
1241 ("not on owner cpu"));
1243 in_pcbinswildcardhash(inp);
1244 for (cpu = 0; cpu < netisr_ncpus; ++cpu) {
1245 if (cpu == mycpuid) {
1247 * This inpcb has been inserted by the above
1248 * in_pcbinswildcardhash().
1250 continue;
1252 in_pcbinswildcardhash_oncpu(inp, &udbinfo[cpu]);
1255 /* NOTE: inp_lgrpindex is _not_ assigned in jail. */
1256 if ((inp->inp_socket->so_options & SO_REUSEPORT) &&
1257 inp->inp_lgrpindex >= 0) {
1259 * For SO_REUSEPORT socket, redistribute it based on its
1260 * local group index.
1262 cpu = inp->inp_lgrpindex % netisr_ncpus;
1263 if (cpu != mycpuid) {
1264 struct lwkt_port *port = netisr_cpuport(cpu);
1265 lwkt_msg_t lmsg = &msg->lmsg;
1268 * We are moving the protocol processing port the
1269 * socket is on, we have to unlink here and re-link
1270 * on the target cpu (this inpcb is still left in
1271 * the wildcard hash).
1273 in_pcbunlink_flags(inp, &udbinfo[mycpuid], 0);
1274 msg->nm_dispatch = udp_soreuseport_dispatch;
1277 * See the related comment in tcp_usrreq.c
1278 * tcp_connect()
1280 lwkt_setmsg_receipt(lmsg, udp_sosetport);
1281 lwkt_forwardmsg(port, lmsg);
1282 return TRUE; /* forwarded */
1285 return FALSE;
1288 static void
1289 udp_inswildcardhash_dispatch(netmsg_t msg)
1291 struct inpcb *inp = msg->base.nm_so->so_pcb;
1292 boolean_t forwarded;
1294 KASSERT(inp->inp_lport != 0, ("local port not set yet"));
1295 KASSERT(udp_lportcpu(inp->inp_lport) == mycpuid, ("not target cpu"));
1297 in_pcblink(inp, &udbinfo[mycpuid]);
1299 forwarded = udp_inswildcardhash_oncpu(inp, &msg->base);
1300 if (forwarded) {
1301 /* The message is further forwarded, so we are done here. */
1302 return;
1304 udp_inswildcard_replymsg(msg);
1307 static boolean_t
1308 udp_inswildcardhash(struct inpcb *inp, struct netmsg_base *msg, int error)
1310 lwkt_msg_t lmsg = &msg->lmsg;
1311 int cpu;
1313 ASSERT_INP_NOTINHASH(inp);
1315 /* This inpcb could no longer be directly detached */
1316 inp->inp_flags &= ~INP_DIRECT_DETACH;
1319 * Always clear the route cache, so we don't need to
1320 * worry about any owner CPU changes later.
1322 in_pcbresetroute(inp);
1324 KASSERT(inp->inp_lport != 0, ("local port not set yet"));
1325 cpu = udp_lportcpu(inp->inp_lport);
1327 lmsg->ms_error = error;
1328 if (cpu != mycpuid) {
1329 struct lwkt_port *port = netisr_cpuport(cpu);
1332 * We are moving the protocol processing port the socket
1333 * is on, we have to unlink here and re-link on the
1334 * target cpu.
1336 in_pcbunlink(inp, &udbinfo[mycpuid]);
1337 msg->nm_dispatch = udp_inswildcardhash_dispatch;
1339 /* See the related comment in tcp_usrreq.c tcp_connect() */
1340 lwkt_setmsg_receipt(lmsg, udp_sosetport);
1341 lwkt_forwardmsg(port, lmsg);
1342 return TRUE; /* forwarded */
1345 return udp_inswildcardhash_oncpu(inp, msg);
1348 static void
1349 udp_bind(netmsg_t msg)
1351 struct socket *so = msg->bind.base.nm_so;
1352 struct inpcb *inp;
1353 int error;
1355 inp = so->so_pcb;
1356 if (inp) {
1357 struct sockaddr *nam = msg->bind.nm_nam;
1358 struct thread *td = msg->bind.nm_td;
1359 struct sockaddr_in *sin;
1360 lwkt_port_t port;
1361 int cpu;
1364 * Check "already bound" here (in_pcbbind() does the same
1365 * check though), so we don't forward a connected/bound
1366 * socket randomly which would panic in the following
1367 * in_pcbunlink().
1369 if (inp->inp_lport != 0 ||
1370 inp->inp_laddr.s_addr != INADDR_ANY) {
1371 error = EINVAL; /* already bound */
1372 goto done;
1375 if (nam->sa_len != sizeof(*sin)) {
1376 error = EINVAL;
1377 goto done;
1379 sin = (struct sockaddr_in *)nam;
1381 cpu = udp_lportcpu(sin->sin_port);
1382 port = netisr_cpuport(cpu);
1385 * See the related comment in tcp_usrreq.c tcp_usr_bind().
1386 * The exception is that we use local port based netisr
1387 * to serialize in_pcbbind().
1389 if (&curthread->td_msgport != port) {
1390 lwkt_msg_t lmsg = &msg->bind.base.lmsg;
1392 KASSERT((msg->bind.nm_flags & PRUB_RELINK) == 0,
1393 ("already asked to relink"));
1395 in_pcbunlink(so->so_pcb, &udbinfo[mycpuid]);
1396 msg->bind.nm_flags |= PRUB_RELINK;
1399 * See the related comment in tcp_usrreq.c
1400 * tcp_connect().
1402 lwkt_setmsg_receipt(lmsg, udp_sosetport);
1403 lwkt_forwardmsg(port, lmsg);
1404 /* msg invalid now */
1405 return;
1407 KASSERT(so->so_port == port, ("so_port is not netisr%d", cpu));
1409 if (msg->bind.nm_flags & PRUB_RELINK) {
1410 msg->bind.nm_flags &= ~PRUB_RELINK;
1411 in_pcblink(so->so_pcb, &udbinfo[mycpuid]);
1413 KASSERT(inp->inp_pcbinfo == &udbinfo[cpu],
1414 ("pcbinfo is not udbinfo%d", cpu));
1416 error = in_pcbbind(inp, nam, td);
1417 if (error == 0) {
1418 boolean_t forwarded;
1420 if (sin->sin_addr.s_addr != INADDR_ANY)
1421 inp->inp_flags |= INP_WASBOUND_NOTANY;
1423 forwarded = udp_inswildcardhash(inp,
1424 &msg->bind.base, 0);
1425 if (forwarded) {
1427 * The message is further forwarded, so
1428 * we are done here.
1430 return;
1433 } else {
1434 error = EINVAL;
1436 done:
1437 lwkt_replymsg(&msg->bind.base.lmsg, error);
1440 static int
1441 udp_preconnect(struct socket *so, const struct sockaddr *nam __unused,
1442 struct thread *td __unused)
1444 sosetstate(so, SS_ISCONNECTED); /* XXX */
1445 return 0;
1448 static void
1449 udp_connect(netmsg_t msg)
1451 struct socket *so = msg->connect.base.nm_so;
1452 struct sockaddr *nam = msg->connect.nm_nam;
1453 struct thread *td = msg->connect.nm_td;
1454 struct inpcb *inp;
1455 struct sockaddr_in *sin = (struct sockaddr_in *)nam;
1456 struct sockaddr_in *if_sin;
1457 struct lwkt_port *port;
1458 uint16_t hash;
1459 int error;
1461 KKASSERT(msg->connect.nm_m == NULL);
1463 inp = so->so_pcb;
1464 if (inp == NULL) {
1465 error = EINVAL;
1466 goto out;
1469 if (msg->connect.nm_flags & PRUC_RECONNECT) {
1470 msg->connect.nm_flags &= ~PRUC_RECONNECT;
1471 in_pcblink(inp, &udbinfo[mycpuid]);
1474 if (inp->inp_faddr.s_addr != INADDR_ANY) {
1475 error = EISCONN;
1476 goto out;
1478 error = 0;
1481 * Bind if we have to
1483 if (inp->inp_lport == 0) {
1484 error = in_pcbbind(inp, NULL, td);
1485 if (error)
1486 goto out;
1490 * Calculate the correct protocol processing thread. The connect
1491 * operation must run there.
1493 error = in_pcbladdr(inp, nam, &if_sin, td);
1494 if (error)
1495 goto out;
1496 if (!prison_remote_ip(td, nam)) {
1497 error = EAFNOSUPPORT; /* IPv6 only jail */
1498 goto out;
1501 hash = udp_addrhash(sin->sin_addr.s_addr, sin->sin_port,
1502 inp->inp_laddr.s_addr != INADDR_ANY ?
1503 inp->inp_laddr.s_addr : if_sin->sin_addr.s_addr, inp->inp_lport);
1504 port = netisr_hashport(hash);
1505 if (port != &curthread->td_msgport) {
1506 lwkt_msg_t lmsg = &msg->connect.base.lmsg;
1507 int nm_flags = PRUC_RECONNECT;
1510 * in_pcbladdr() may have allocated a route entry for us
1511 * on the current CPU, but we need a route entry on the
1512 * inpcb's owner CPU, so free it here.
1514 in_pcbresetroute(inp);
1516 if (inp->inp_flags & INP_WILDCARD) {
1518 * Remove this inpcb from the wildcard hash before
1519 * the socket's msgport changes.
1521 udp_remwildcardhash(inp);
1524 if (so->so_orig_port == NULL) {
1526 * First time change protocol processing port.
1527 * Save the current port for synchronization upon
1528 * udp_detach.
1530 so->so_orig_port = &curthread->td_msgport;
1531 } else {
1533 * We have changed protocol processing port more
1534 * than once. We could not do direct detach
1535 * anymore, because we lose the track of the
1536 * original protocol processing ports to perform
1537 * synchronization upon udp_detach. This should
1538 * be rare though.
1540 inp->inp_flags &= ~INP_DIRECT_DETACH;
1544 * We are moving the protocol processing port the socket
1545 * is on, we have to unlink here and re-link on the
1546 * target cpu.
1548 in_pcbunlink(inp, &udbinfo[mycpuid]);
1549 msg->connect.nm_flags |= nm_flags;
1551 /* See the related comment in tcp_usrreq.c tcp_connect() */
1552 lwkt_setmsg_receipt(lmsg, udp_sosetport);
1553 lwkt_forwardmsg(port, lmsg);
1554 /* msg invalid now */
1555 return;
1557 error = udp_connect_oncpu(inp, sin, if_sin, hash);
1558 out:
1559 if (msg->connect.nm_flags & PRUC_HELDTD)
1560 lwkt_rele(td);
1561 if (error && (msg->connect.nm_flags & PRUC_ASYNC)) {
1562 if (inp->inp_lport == 0) {
1564 * As long as we have the local port, it is fine
1565 * for connect to fail, e.g. disconnect.
1567 so->so_error = error;
1569 soclrstate(so, SS_ISCONNECTED);
1571 * Wake up callers blocked on this socket to make sure
1572 * that they can see this error.
1574 * NOTE:
1575 * sodisconnected() can't be used here, which bricks
1576 * sending and receiving.
1578 wakeup(&so->so_timeo);
1579 sowwakeup(so);
1580 sorwakeup(so);
1582 if (error && inp != NULL && inp->inp_lport != 0 &&
1583 (inp->inp_flags & INP_WILDCARD) == 0) {
1584 boolean_t forwarded;
1586 /* Connect failed; put it to wildcard hash. */
1587 forwarded = udp_inswildcardhash(inp, &msg->connect.base,
1588 error);
1589 if (forwarded) {
1591 * The message is further forwarded, so we are done
1592 * here.
1594 return;
1597 lwkt_replymsg(&msg->connect.base.lmsg, error);
1600 static void
1601 udp_remwildcardhash(struct inpcb *inp)
1603 int cpu;
1605 KASSERT(inp->inp_pcbinfo == &udbinfo[mycpuid],
1606 ("not on owner cpu"));
1608 for (cpu = 0; cpu < netisr_ncpus; ++cpu) {
1609 if (cpu == mycpuid) {
1611 * This inpcb will be removed by the later
1612 * in_pcbremwildcardhash().
1614 continue;
1616 in_pcbremwildcardhash_oncpu(inp, &udbinfo[cpu]);
1618 in_pcbremwildcardhash(inp);
1621 static int
1622 udp_connect_oncpu(struct inpcb *inp, struct sockaddr_in *sin,
1623 struct sockaddr_in *if_sin, uint16_t hash)
1625 struct socket *so = inp->inp_socket;
1626 struct inpcb *oinp;
1628 oinp = in_pcblookup_hash(inp->inp_pcbinfo,
1629 sin->sin_addr, sin->sin_port,
1630 inp->inp_laddr.s_addr != INADDR_ANY ?
1631 inp->inp_laddr : if_sin->sin_addr, inp->inp_lport, FALSE, NULL);
1632 if (oinp != NULL)
1633 return EADDRINUSE;
1636 * No more errors can occur, finish adjusting the socket
1637 * and change the processing port to reflect the connected
1638 * socket. Once set we can no longer safely mess with the
1639 * socket.
1642 if (inp->inp_flags & INP_WILDCARD)
1643 udp_remwildcardhash(inp);
1645 if (inp->inp_laddr.s_addr == INADDR_ANY)
1646 inp->inp_laddr = if_sin->sin_addr;
1647 inp->inp_faddr = sin->sin_addr;
1648 inp->inp_fport = sin->sin_port;
1649 in_pcbinsconnhash(inp);
1651 inp->inp_flags |= INP_HASH;
1652 inp->inp_hashval = hash;
1654 soisconnected(so);
1656 return 0;
1659 static void
1660 udp_detach2(struct socket *so)
1662 in_pcbdetach(so->so_pcb);
1663 sodiscard(so);
1664 sofree(so);
1667 static void
1668 udp_detach_final_dispatch(netmsg_t msg)
1670 udp_detach2(msg->base.nm_so);
1673 static void
1674 udp_detach_oncpu_dispatch(netmsg_t msg)
1676 struct netmsg_base *clomsg = &msg->base;
1677 struct socket *so = clomsg->nm_so;
1678 struct inpcb *inp = so->so_pcb;
1679 struct thread *td = curthread;
1680 int nextcpu, cpuid = mycpuid;
1682 KASSERT(td->td_type == TD_TYPE_NETISR, ("not in netisr"));
1684 if (inp->inp_flags & INP_WILDCARD) {
1686 * This inp will be removed on the inp's
1687 * owner CPU later, so don't do it now.
1689 if (&td->td_msgport != so->so_port)
1690 in_pcbremwildcardhash_oncpu(inp, &udbinfo[cpuid]);
1693 if (cpuid == 0) {
1695 * Free and clear multicast socket option,
1696 * which is only accessed in netisr0.
1698 ip_freemoptions(inp->inp_moptions);
1699 inp->inp_moptions = NULL;
1702 nextcpu = cpuid + 1;
1703 if (nextcpu < netisr_ncpus) {
1704 lwkt_forwardmsg(netisr_cpuport(nextcpu), &clomsg->lmsg);
1705 } else {
1707 * No one could see this inpcb now; destroy this
1708 * inpcb in its owner netisr.
1710 netmsg_init(clomsg, so, &netisr_apanic_rport, 0,
1711 udp_detach_final_dispatch);
1712 lwkt_sendmsg(so->so_port, &clomsg->lmsg);
1716 static void
1717 udp_detach_syncorig_dispatch(netmsg_t msg)
1719 struct netmsg_base *clomsg = &msg->base;
1720 struct socket *so = clomsg->nm_so;
1723 * Original protocol processing port is synchronized;
1724 * destroy this inpcb in its owner netisr.
1726 netmsg_init(clomsg, so, &netisr_apanic_rport, 0,
1727 udp_detach_final_dispatch);
1728 lwkt_sendmsg(so->so_port, &clomsg->lmsg);
1731 static void
1732 udp_detach(netmsg_t msg)
1734 struct socket *so = msg->detach.base.nm_so;
1735 struct netmsg_base *clomsg;
1736 struct inpcb *inp;
1738 inp = so->so_pcb;
1739 if (inp == NULL) {
1740 lwkt_replymsg(&msg->detach.base.lmsg, EINVAL);
1741 return;
1745 * Reply EJUSTRETURN ASAP, we will call sodiscard() and
1746 * sofree() later.
1748 lwkt_replymsg(&msg->detach.base.lmsg, EJUSTRETURN);
1750 if (netisr_ncpus == 1) {
1751 /* Only one CPU, detach the inpcb directly. */
1752 udp_detach2(so);
1753 return;
1757 * Remove this inpcb from the inpcb list first, so that
1758 * no one could find this inpcb from the inpcb list.
1760 in_pcbofflist(inp);
1763 * Remove this inpcb from the local port hash directly
1764 * here, so that its bound local port could be recycled
1765 * timely.
1767 in_pcbremporthash(inp);
1769 if (inp->inp_flags & INP_DIRECT_DETACH) {
1771 * Direct detaching is allowed
1773 KASSERT((inp->inp_flags & INP_WILDCARD) == 0,
1774 ("in the wildcardhash"));
1775 KASSERT(inp->inp_moptions == NULL, ("has mcast options"));
1776 if (so->so_orig_port == NULL) {
1777 udp_detach2(so);
1778 } else {
1780 * Protocol processing port changed once, so
1781 * we need to make sure that there are nothing
1782 * left on the original protocol processing
1783 * port before we destroy this socket and inpcb.
1784 * This is more lightweight than going through
1785 * all UDP processing netisrs.
1787 clomsg = &so->so_clomsg;
1788 netmsg_init(clomsg, so, &netisr_apanic_rport,
1789 MSGF_IGNSOPORT, udp_detach_syncorig_dispatch);
1790 lwkt_sendmsg(so->so_orig_port, &clomsg->lmsg);
1792 return;
1796 * Go through netisrs which process UDP to make sure
1797 * no one could find this inpcb anymore.
1799 clomsg = &so->so_clomsg;
1800 netmsg_init(clomsg, so, &netisr_apanic_rport, MSGF_IGNSOPORT,
1801 udp_detach_oncpu_dispatch);
1802 lwkt_sendmsg(netisr_cpuport(0), &clomsg->lmsg);
1805 static void
1806 udp_disconnect(netmsg_t msg)
1808 struct socket *so = msg->disconnect.base.nm_so;
1809 struct inpcb *inp;
1810 boolean_t forwarded;
1811 int error = 0;
1813 inp = so->so_pcb;
1814 if (inp == NULL) {
1815 error = EINVAL;
1816 goto out;
1818 if (inp->inp_faddr.s_addr == INADDR_ANY) {
1819 error = ENOTCONN;
1820 goto out;
1823 soclrstate(so, SS_ISCONNECTED); /* XXX */
1825 in_pcbdisconnect(inp);
1826 inp->inp_flags &= ~INP_HASH;
1829 * Follow traditional BSD behavior and retain the local port
1830 * binding. But, fix the old misbehavior of overwriting any
1831 * previously bound local address.
1833 if (!(inp->inp_flags & INP_WASBOUND_NOTANY))
1834 inp->inp_laddr.s_addr = INADDR_ANY;
1836 if (so->so_state & SS_ISCLOSING) {
1838 * If this socket is being closed, there is no need
1839 * to put this socket back into wildcard hash table.
1841 error = 0;
1842 goto out;
1845 forwarded = udp_inswildcardhash(inp, &msg->disconnect.base, 0);
1846 if (forwarded) {
1848 * The message is further forwarded, so we are done
1849 * here.
1851 return;
1853 out:
1854 lwkt_replymsg(&msg->disconnect.base.lmsg, error);
1857 void
1858 udp_shutdown(netmsg_t msg)
1860 struct socket *so = msg->shutdown.base.nm_so;
1861 struct inpcb *inp;
1862 int error;
1864 inp = so->so_pcb;
1865 if (inp) {
1866 socantsendmore(so);
1867 error = 0;
1868 } else {
1869 error = EINVAL;
1871 lwkt_replymsg(&msg->shutdown.base.lmsg, error);
1874 struct pr_usrreqs udp_usrreqs = {
1875 .pru_abort = udp_abort,
1876 .pru_accept = pr_generic_notsupp,
1877 .pru_attach = udp_attach,
1878 .pru_bind = udp_bind,
1879 .pru_connect = udp_connect,
1880 .pru_connect2 = pr_generic_notsupp,
1881 .pru_control = in_control_dispatch,
1882 .pru_detach = udp_detach,
1883 .pru_disconnect = udp_disconnect,
1884 .pru_listen = pr_generic_notsupp,
1885 .pru_peeraddr = in_setpeeraddr_dispatch,
1886 .pru_rcvd = pr_generic_notsupp,
1887 .pru_rcvoob = pr_generic_notsupp,
1888 .pru_send = udp_send,
1889 .pru_sense = pru_sense_null,
1890 .pru_shutdown = udp_shutdown,
1891 .pru_sockaddr = in_setsockaddr_dispatch,
1892 .pru_sosend = sosendudp,
1893 .pru_soreceive = soreceive,
1894 .pru_preconnect = udp_preconnect,
1895 .pru_preattach = udp_preattach