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
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
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
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
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. All advertising materials mentioning features or use of this software
47 * must display the following acknowledgement:
48 * This product includes software developed by the University of
49 * California, Berkeley and its contributors.
50 * 4. Neither the name of the University nor the names of its contributors
51 * may be used to endorse or promote products derived from this software
52 * without specific prior written permission.
54 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
55 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
56 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
57 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
58 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
59 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
60 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
61 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
62 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
63 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
66 * @(#)udp_usrreq.c 8.6 (Berkeley) 5/23/95
67 * $FreeBSD: src/sys/netinet/udp_usrreq.c,v 1.64.2.18 2003/01/24 05:11:34 sam Exp $
68 * $DragonFly: src/sys/netinet/udp_usrreq.c,v 1.47 2008/11/11 10:46:58 sephe Exp $
71 #include "opt_ipsec.h"
72 #include "opt_inet6.h"
74 #include <sys/param.h>
75 #include <sys/systm.h>
76 #include <sys/kernel.h>
77 #include <sys/malloc.h>
79 #include <sys/domain.h>
82 #include <sys/protosw.h>
83 #include <sys/socket.h>
84 #include <sys/socketvar.h>
85 #include <sys/sysctl.h>
86 #include <sys/syslog.h>
87 #include <sys/in_cksum.h>
89 #include <sys/thread2.h>
90 #include <sys/socketvar2.h>
91 #include <sys/serialize.h>
93 #include <machine/stdarg.h>
96 #include <net/route.h>
97 #include <net/netmsg2.h>
99 #include <netinet/in.h>
100 #include <netinet/in_systm.h>
101 #include <netinet/ip.h>
103 #include <netinet/ip6.h>
105 #include <netinet/in_pcb.h>
106 #include <netinet/in_var.h>
107 #include <netinet/ip_var.h>
109 #include <netinet6/ip6_var.h>
111 #include <netinet/ip_icmp.h>
112 #include <netinet/icmp_var.h>
113 #include <netinet/udp.h>
114 #include <netinet/udp_var.h>
117 #include <netproto/ipsec/ipsec.h>
121 #include <netinet6/ipsec.h>
125 * UDP protocol implementation.
126 * Per RFC 768, August, 1980.
129 static int udpcksum = 1;
131 static int udpcksum = 0; /* XXX */
133 SYSCTL_INT(_net_inet_udp, UDPCTL_CHECKSUM, checksum, CTLFLAG_RW,
134 &udpcksum, 0, "Enable checksumming of UDP packets");
137 SYSCTL_INT(_net_inet_udp, OID_AUTO, log_in_vain, CTLFLAG_RW,
138 &log_in_vain, 0, "Log all incoming UDP packets");
140 static int blackhole = 0;
141 SYSCTL_INT(_net_inet_udp, OID_AUTO, blackhole, CTLFLAG_RW,
142 &blackhole, 0, "Do not send port unreachables for refused connects");
144 static int strict_mcast_mship = 1;
145 SYSCTL_INT(_net_inet_udp, OID_AUTO, strict_mcast_mship, CTLFLAG_RW,
146 &strict_mcast_mship, 0, "Only send multicast to member sockets");
148 struct inpcbinfo udbinfo;
150 static struct netisr_barrier *udbinfo_br;
151 static struct lwkt_serialize udbinfo_slize = LWKT_SERIALIZE_INITIALIZER;
154 #define UDBHASHSIZE 16
157 struct udpstat udpstat; /* from udp_var.h */
158 SYSCTL_STRUCT(_net_inet_udp, UDPCTL_STATS, stats, CTLFLAG_RW,
159 &udpstat, udpstat, "UDP statistics (struct udpstat, netinet/udp_var.h)");
161 static struct sockaddr_in udp_in = { sizeof udp_in, AF_INET };
164 struct sockaddr_in6 uin6_sin;
165 u_char uin6_init_done : 1;
167 { sizeof udp_in6.uin6_sin, AF_INET6 },
171 struct ip6_hdr uip6_ip6;
172 u_char uip6_init_done : 1;
176 static void udp_append (struct inpcb *last, struct ip *ip,
177 struct mbuf *n, int off);
179 static void ip_2_ip6_hdr (struct ip6_hdr *ip6, struct ip *ip);
182 static int udp_connect_oncpu(struct socket *so, struct thread *td,
183 struct sockaddr_in *sin, struct sockaddr_in *if_sin);
184 static int udp_output (struct inpcb *, struct mbuf *, struct sockaddr *,
185 struct mbuf *, struct thread *);
190 in_pcbinfo_init(&udbinfo);
191 udbinfo.hashbase = hashinit(UDBHASHSIZE, M_PCB, &udbinfo.hashmask);
192 udbinfo.porthashbase = hashinit(UDBHASHSIZE, M_PCB,
193 &udbinfo.porthashmask);
194 udbinfo.wildcardhashbase = hashinit(UDBHASHSIZE, M_PCB,
195 &udbinfo.wildcardhashmask);
196 udbinfo.ipi_size = sizeof(struct inpcb);
198 udbinfo_br = netisr_barrier_create();
202 * Check multicast packets to make sure they are only sent to sockets with
203 * multicast memberships for the packet's destination address and arrival
204 * interface. Multicast packets to multicast-unaware sockets are also
207 * Returns 0 if the packet is acceptable, -1 if it is not.
210 check_multicast_membership(struct ip *ip, struct inpcb *inp, struct mbuf *m)
213 struct ip_moptions *mopt;
215 if (strict_mcast_mship == 0 ||
216 !IN_MULTICAST(ntohl(ip->ip_dst.s_addr))) {
219 mopt = inp->inp_moptions;
222 for (mshipno = 0; mshipno < mopt->imo_num_memberships; ++mshipno) {
223 struct in_multi *maddr = mopt->imo_membership[mshipno];
225 if (ip->ip_dst.s_addr == maddr->inm_addr.s_addr &&
226 m->m_pkthdr.rcvif == maddr->inm_ifp) {
234 udp_input(struct mbuf **mp, int *offp, int proto)
241 struct mbuf *opts = NULL;
244 struct sockaddr *append_sa;
251 udpstat.udps_ipackets++;
254 * Strip IP options, if any; should skip this,
255 * make available to user, and use on returned packets,
256 * but we don't yet have a way to check the checksum
257 * with options still present.
259 if (iphlen > sizeof(struct ip)) {
261 iphlen = sizeof(struct ip);
265 * IP and UDP headers are together in first mbuf.
266 * Already checked and pulled up in ip_demux().
268 KASSERT(m->m_len >= iphlen + sizeof(struct udphdr),
269 ("UDP header not in one mbuf"));
271 ip = mtod(m, struct ip *);
272 uh = (struct udphdr *)((caddr_t)ip + iphlen);
274 /* destination port of 0 is illegal, based on RFC768. */
275 if (uh->uh_dport == 0)
279 * Make mbuf data length reflect UDP length.
280 * If not enough data to reflect UDP length, drop.
282 len = ntohs((u_short)uh->uh_ulen);
283 if (ip->ip_len != len) {
284 if (len > ip->ip_len || len < sizeof(struct udphdr)) {
285 udpstat.udps_badlen++;
288 m_adj(m, len - ip->ip_len);
289 /* ip->ip_len = len; */
292 * Save a copy of the IP header in case we want restore it
293 * for sending an ICMP error message in response.
298 * Checksum extended UDP header and data.
301 if (m->m_pkthdr.csum_flags & CSUM_DATA_VALID) {
302 if (m->m_pkthdr.csum_flags & CSUM_PSEUDO_HDR)
303 uh->uh_sum = m->m_pkthdr.csum_data;
305 uh->uh_sum = in_pseudo(ip->ip_src.s_addr,
306 ip->ip_dst.s_addr, htonl((u_short)len +
307 m->m_pkthdr.csum_data + IPPROTO_UDP));
308 uh->uh_sum ^= 0xffff;
312 bcopy(((struct ipovly *)ip)->ih_x1, b, 9);
313 bzero(((struct ipovly *)ip)->ih_x1, 9);
314 ((struct ipovly *)ip)->ih_len = uh->uh_ulen;
315 uh->uh_sum = in_cksum(m, len + sizeof(struct ip));
316 bcopy(b, ((struct ipovly *)ip)->ih_x1, 9);
319 udpstat.udps_badsum++;
321 return(IPPROTO_DONE);
324 udpstat.udps_nosum++;
326 if (IN_MULTICAST(ntohl(ip->ip_dst.s_addr)) ||
327 in_broadcast(ip->ip_dst, m->m_pkthdr.rcvif)) {
331 * Deliver a multicast or broadcast datagram to *all* sockets
332 * for which the local and remote addresses and ports match
333 * those of the incoming datagram. This allows more than
334 * one process to receive multi/broadcasts on the same port.
335 * (This really ought to be done for unicast datagrams as
336 * well, but that would cause problems with existing
337 * applications that open both address-specific sockets and
338 * a wildcard socket listening to the same port -- they would
339 * end up receiving duplicates of every unicast datagram.
340 * Those applications open the multiple sockets to overcome an
341 * inadequacy of the UDP socket interface, but for backwards
342 * compatibility we avoid the problem here rather than
343 * fixing the interface. Maybe 4.5BSD will remedy this?)
347 * Construct sockaddr format source address.
349 udp_in.sin_port = uh->uh_sport;
350 udp_in.sin_addr = ip->ip_src;
352 * Locate pcb(s) for datagram.
353 * (Algorithm copied from raw_intr().)
357 udp_in6.uin6_init_done = udp_ip6.uip6_init_done = 0;
359 LIST_FOREACH(inp, &udbinfo.pcblisthead, inp_list) {
360 KKASSERT((inp->inp_flags & INP_PLACEMARKER) == 0);
362 if (!(inp->inp_vflag & INP_IPV4))
365 if (inp->inp_lport != uh->uh_dport)
367 if (inp->inp_laddr.s_addr != INADDR_ANY) {
368 if (inp->inp_laddr.s_addr !=
372 if (inp->inp_faddr.s_addr != INADDR_ANY) {
373 if (inp->inp_faddr.s_addr !=
375 inp->inp_fport != uh->uh_sport)
379 if (check_multicast_membership(ip, inp, m) < 0)
386 /* check AH/ESP integrity. */
387 if (ipsec4_in_reject_so(m, last->inp_socket))
388 ipsecstat.in_polvio++;
389 /* do not inject data to pcb */
393 /* check AH/ESP integrity. */
394 if (ipsec4_in_reject(m, last))
397 #endif /*FAST_IPSEC*/
398 if ((n = m_copypacket(m, MB_DONTWAIT)) != NULL)
399 udp_append(last, ip, n,
401 sizeof(struct udphdr));
405 * Don't look for additional matches if this one does
406 * not have either the SO_REUSEPORT or SO_REUSEADDR
407 * socket options set. This heuristic avoids searching
408 * through all pcbs in the common case of a non-shared
409 * port. It * assumes that an application will never
410 * clear these options after setting them.
412 if (!(last->inp_socket->so_options &
413 (SO_REUSEPORT | SO_REUSEADDR)))
419 * No matching pcb found; discard datagram.
420 * (No need to send an ICMP Port Unreachable
421 * for a broadcast or multicast datgram.)
423 udpstat.udps_noportbcast++;
427 /* check AH/ESP integrity. */
428 if (ipsec4_in_reject_so(m, last->inp_socket)) {
429 ipsecstat.in_polvio++;
434 /* check AH/ESP integrity. */
435 if (ipsec4_in_reject(m, last))
437 #endif /*FAST_IPSEC*/
438 udp_append(last, ip, m, iphlen + sizeof(struct udphdr));
439 return(IPPROTO_DONE);
442 * Locate pcb for datagram.
444 inp = in_pcblookup_hash(&udbinfo, ip->ip_src, uh->uh_sport,
445 ip->ip_dst, uh->uh_dport, 1, m->m_pkthdr.rcvif);
448 char buf[sizeof "aaa.bbb.ccc.ddd"];
450 strcpy(buf, inet_ntoa(ip->ip_dst));
452 "Connection attempt to UDP %s:%d from %s:%d\n",
453 buf, ntohs(uh->uh_dport), inet_ntoa(ip->ip_src),
454 ntohs(uh->uh_sport));
456 udpstat.udps_noport++;
457 if (m->m_flags & (M_BCAST | M_MCAST)) {
458 udpstat.udps_noportbcast++;
464 if (badport_bandlim(BANDLIM_ICMP_UNREACH) < 0)
468 ip->ip_len += iphlen;
469 icmp_error(m, ICMP_UNREACH, ICMP_UNREACH_PORT, 0, 0);
470 return(IPPROTO_DONE);
473 if (ipsec4_in_reject_so(m, inp->inp_socket)) {
474 ipsecstat.in_polvio++;
479 if (ipsec4_in_reject(m, inp))
481 #endif /*FAST_IPSEC*/
483 * Check the minimum TTL for socket.
485 if (ip->ip_ttl < inp->inp_ip_minttl)
489 * Construct sockaddr format source address.
490 * Stuff source address and datagram in user buffer.
492 udp_in.sin_port = uh->uh_sport;
493 udp_in.sin_addr = ip->ip_src;
494 if ((inp->inp_flags & INP_CONTROLOPTS) ||
495 (inp->inp_socket->so_options & SO_TIMESTAMP)) {
497 if (inp->inp_vflag & INP_IPV6) {
500 ip_2_ip6_hdr(&udp_ip6.uip6_ip6, ip);
501 savedflags = inp->inp_flags;
502 inp->inp_flags &= ~INP_UNMAPPABLEOPTS;
503 ip6_savecontrol(inp, &opts, &udp_ip6.uip6_ip6, m);
504 inp->inp_flags = savedflags;
507 ip_savecontrol(inp, &opts, ip, m);
509 m_adj(m, iphlen + sizeof(struct udphdr));
511 if (inp->inp_vflag & INP_IPV6) {
512 in6_sin_2_v4mapsin6(&udp_in, &udp_in6.uin6_sin);
513 append_sa = (struct sockaddr *)&udp_in6;
516 append_sa = (struct sockaddr *)&udp_in;
518 lwkt_gettoken(&inp->inp_socket->so_rcv.ssb_token);
519 if (ssb_appendaddr(&inp->inp_socket->so_rcv, append_sa, m, opts) == 0) {
520 udpstat.udps_fullsock++;
521 lwkt_reltoken(&inp->inp_socket->so_rcv.ssb_token);
524 lwkt_reltoken(&inp->inp_socket->so_rcv.ssb_token);
525 sorwakeup(inp->inp_socket);
526 return(IPPROTO_DONE);
531 return(IPPROTO_DONE);
536 ip_2_ip6_hdr(struct ip6_hdr *ip6, struct ip *ip)
538 bzero(ip6, sizeof *ip6);
540 ip6->ip6_vfc = IPV6_VERSION;
541 ip6->ip6_plen = ip->ip_len;
542 ip6->ip6_nxt = ip->ip_p;
543 ip6->ip6_hlim = ip->ip_ttl;
544 ip6->ip6_src.s6_addr32[2] = ip6->ip6_dst.s6_addr32[2] =
546 ip6->ip6_src.s6_addr32[3] = ip->ip_src.s_addr;
547 ip6->ip6_dst.s6_addr32[3] = ip->ip_dst.s_addr;
552 * subroutine of udp_input(), mainly for source code readability.
553 * caller must properly init udp_ip6 and udp_in6 beforehand.
556 udp_append(struct inpcb *last, struct ip *ip, struct mbuf *n, int off)
558 struct sockaddr *append_sa;
559 struct mbuf *opts = NULL;
561 if (last->inp_flags & INP_CONTROLOPTS ||
562 last->inp_socket->so_options & SO_TIMESTAMP) {
564 if (last->inp_vflag & INP_IPV6) {
567 if (udp_ip6.uip6_init_done == 0) {
568 ip_2_ip6_hdr(&udp_ip6.uip6_ip6, ip);
569 udp_ip6.uip6_init_done = 1;
571 savedflags = last->inp_flags;
572 last->inp_flags &= ~INP_UNMAPPABLEOPTS;
573 ip6_savecontrol(last, &opts, &udp_ip6.uip6_ip6, n);
574 last->inp_flags = savedflags;
577 ip_savecontrol(last, &opts, ip, n);
580 if (last->inp_vflag & INP_IPV6) {
581 if (udp_in6.uin6_init_done == 0) {
582 in6_sin_2_v4mapsin6(&udp_in, &udp_in6.uin6_sin);
583 udp_in6.uin6_init_done = 1;
585 append_sa = (struct sockaddr *)&udp_in6.uin6_sin;
588 append_sa = (struct sockaddr *)&udp_in;
590 lwkt_gettoken(&last->inp_socket->so_rcv.ssb_token);
591 if (ssb_appendaddr(&last->inp_socket->so_rcv, append_sa, n, opts) == 0) {
595 udpstat.udps_fullsock++;
597 sorwakeup(last->inp_socket);
599 lwkt_reltoken(&last->inp_socket->so_rcv.ssb_token);
603 * Notify a udp user of an asynchronous error;
604 * just wake up so that he can collect error status.
607 udp_notify(struct inpcb *inp, int error)
609 inp->inp_socket->so_error = error;
610 sorwakeup(inp->inp_socket);
611 sowwakeup(inp->inp_socket);
614 struct netmsg_udp_notify {
615 struct netmsg_base base;
616 void (*nm_notify)(struct inpcb *, int);
617 struct in_addr nm_faddr;
622 udp_notifyall_oncpu(netmsg_t msg)
624 struct netmsg_udp_notify *nm = (struct netmsg_udp_notify *)msg;
629 in_pcbnotifyall(&udbinfo.pcblisthead, nm->nm_faddr,
630 nm->nm_arg, nm->nm_notify);
631 lwkt_replymsg(&nm->base.lmsg, 0);
634 /* XXX currently udp only runs on cpu 0 */
635 nextcpu = mycpuid + 1;
636 if (nextcpu < ncpus2)
637 lwkt_forwardmsg(cpu_portfn(nextcpu), &nm->base.lmsg);
639 lwkt_replymsg(&nmsg->base.lmsg, 0);
644 udp_rtchange(struct inpcb *inp, int err)
647 /* XXX Nuke this, once UDP inpcbs are CPU localized */
648 if (inp->inp_route.ro_rt && inp->inp_route.ro_rt->rt_cpuid == mycpuid) {
649 rtfree(inp->inp_route.ro_rt);
650 inp->inp_route.ro_rt = NULL;
652 * A new route can be allocated the next time
653 * output is attempted.
657 in_rtchange(inp, err);
662 udp_ctlinput(netmsg_t msg)
664 struct sockaddr *sa = msg->ctlinput.nm_arg;
665 struct ip *ip = msg->ctlinput.nm_extra;
666 int cmd = msg->ctlinput.nm_cmd;
668 void (*notify) (struct inpcb *, int) = udp_notify;
669 struct in_addr faddr;
672 KKASSERT(&curthread->td_msgport == cpu_portfn(0));
674 faddr = ((struct sockaddr_in *)sa)->sin_addr;
675 if (sa->sa_family != AF_INET || faddr.s_addr == INADDR_ANY)
678 if (PRC_IS_REDIRECT(cmd)) {
680 notify = udp_rtchange;
681 } else if (cmd == PRC_HOSTDEAD) {
683 } else if ((unsigned)cmd >= PRC_NCMDS || inetctlerrmap[cmd] == 0) {
688 uh = (struct udphdr *)((caddr_t)ip + (ip->ip_hl << 2));
689 inp = in_pcblookup_hash(&udbinfo, faddr, uh->uh_dport,
690 ip->ip_src, uh->uh_sport, 0, NULL);
691 if (inp != NULL && inp->inp_socket != NULL)
692 (*notify)(inp, inetctlerrmap[cmd]);
693 } else if (PRC_IS_REDIRECT(cmd)) {
694 struct netmsg_udp_notify *nm;
696 KKASSERT(&curthread->td_msgport == cpu_portfn(0));
697 nm = kmalloc(sizeof(*nm), M_LWKTMSG, M_INTWAIT);
698 netmsg_init(&nm->base, NULL, &netisr_afree_rport,
699 0, udp_notifyall_oncpu);
700 nm->nm_faddr = faddr;
701 nm->nm_arg = inetctlerrmap[cmd];
702 nm->nm_notify = notify;
703 lwkt_sendmsg(cpu_portfn(0), &nm->base.lmsg);
706 * XXX We should forward msg upon PRC_HOSTHEAD and ip == NULL,
707 * once UDP inpcbs are CPU localized
709 KKASSERT(&curthread->td_msgport == cpu_portfn(0));
710 in_pcbnotifyall(&udbinfo.pcblisthead, faddr, inetctlerrmap[cmd],
714 lwkt_replymsg(&msg->lmsg, 0);
718 udp_pcblist(SYSCTL_HANDLER_ARGS)
724 error = in_pcblist_global_nomarker(oidp, arg1, arg2, req, &xi, &nxi);
728 KKASSERT(xi == NULL);
732 KKASSERT(xi == NULL);
736 for (i = 0; i < nxi; ++i) {
737 error = SYSCTL_OUT(req, &xi[i], sizeof(xi[i]));
745 SYSCTL_PROC(_net_inet_udp, UDPCTL_PCBLIST, pcblist, CTLFLAG_RD, &udbinfo, 0,
746 udp_pcblist, "S,xinpcb", "List of active UDP sockets");
749 udp_getcred(SYSCTL_HANDLER_ARGS)
751 struct sockaddr_in addrs[2];
752 struct ucred cred0, *cred = NULL;
756 error = priv_check(req->td, PRIV_ROOT);
759 error = SYSCTL_IN(req, addrs, sizeof addrs);
764 inp = in_pcblookup_hash(&udbinfo, addrs[1].sin_addr, addrs[1].sin_port,
765 addrs[0].sin_addr, addrs[0].sin_port, 1, NULL);
766 if (inp == NULL || inp->inp_socket == NULL) {
769 if (inp->inp_socket->so_cred != NULL) {
770 cred0 = *(inp->inp_socket->so_cred);
779 return SYSCTL_OUT(req, cred, sizeof(struct ucred));
782 SYSCTL_PROC(_net_inet_udp, OID_AUTO, getcred, CTLTYPE_OPAQUE|CTLFLAG_RW,
783 0, 0, udp_getcred, "S,ucred", "Get the ucred of a UDP connection");
786 udp_output(struct inpcb *inp, struct mbuf *m, struct sockaddr *dstaddr,
787 struct mbuf *control, struct thread *td)
790 int len = m->m_pkthdr.len;
791 struct sockaddr_in *sin; /* really is initialized before use */
792 int error = 0, lport_any = 0;
794 if (len + sizeof(struct udpiphdr) > IP_MAXPACKET) {
799 if (inp->inp_lport == 0) { /* unbound socket */
800 error = in_pcbbind(inp, NULL, td);
804 udbinfo_barrier_set();
805 in_pcbinswildcardhash(inp);
806 udbinfo_barrier_rem();
810 if (dstaddr != NULL) { /* destination address specified */
811 if (inp->inp_faddr.s_addr != INADDR_ANY) {
812 /* already connected */
816 sin = (struct sockaddr_in *)dstaddr;
817 if (!prison_remote_ip(td, (struct sockaddr *)&sin)) {
818 error = EAFNOSUPPORT; /* IPv6 only jail */
822 if (inp->inp_faddr.s_addr == INADDR_ANY) {
823 /* no destination specified and not already connected */
831 * Calculate data length and get a mbuf
832 * for UDP and IP headers.
834 M_PREPEND(m, sizeof(struct udpiphdr), MB_DONTWAIT);
841 * Fill in mbuf with extended UDP header
842 * and addresses and length put into network format.
844 ui = mtod(m, struct udpiphdr *);
845 bzero(ui->ui_x1, sizeof ui->ui_x1); /* XXX still needed? */
846 ui->ui_pr = IPPROTO_UDP;
849 * Set destination address.
851 if (dstaddr != NULL) { /* use specified destination */
852 ui->ui_dst = sin->sin_addr;
853 ui->ui_dport = sin->sin_port;
854 } else { /* use connected destination */
855 ui->ui_dst = inp->inp_faddr;
856 ui->ui_dport = inp->inp_fport;
860 * Set source address.
862 if (inp->inp_laddr.s_addr == INADDR_ANY) {
863 struct sockaddr_in *if_sin;
865 if (dstaddr == NULL) {
867 * connect() had (or should have) failed because
868 * the interface had no IP address, but the
869 * application proceeded to call send() anyways.
875 /* Look up outgoing interface. */
876 if ((error = in_pcbladdr(inp, dstaddr, &if_sin, td)))
878 ui->ui_src = if_sin->sin_addr; /* use address of interface */
880 ui->ui_src = inp->inp_laddr; /* use non-null bound address */
882 ui->ui_sport = inp->inp_lport;
883 KASSERT(inp->inp_lport != 0, ("inp lport should have been bound"));
885 ui->ui_ulen = htons((u_short)len + sizeof(struct udphdr));
888 * Set up checksum and output datagram.
891 ui->ui_sum = in_pseudo(ui->ui_src.s_addr, ui->ui_dst.s_addr,
892 htons((u_short)len + sizeof(struct udphdr) + IPPROTO_UDP));
893 m->m_pkthdr.csum_flags = CSUM_UDP;
894 m->m_pkthdr.csum_data = offsetof(struct udphdr, uh_sum);
898 ((struct ip *)ui)->ip_len = sizeof(struct udpiphdr) + len;
899 ((struct ip *)ui)->ip_ttl = inp->inp_ip_ttl; /* XXX */
900 ((struct ip *)ui)->ip_tos = inp->inp_ip_tos; /* XXX */
901 udpstat.udps_opackets++;
903 error = ip_output(m, inp->inp_options, &inp->inp_route,
904 (inp->inp_socket->so_options & (SO_DONTROUTE | SO_BROADCAST)) |
906 inp->inp_moptions, inp);
909 * If this is the first data gram sent on an unbound and unconnected
910 * UDP socket, lport will be changed in this function. If target
911 * CPU after this lport changing is no longer the current CPU, then
912 * free the route entry allocated on the current CPU.
915 if (udp_addrcpu(inp->inp_faddr.s_addr, inp->inp_fport,
916 inp->inp_laddr.s_addr, inp->inp_lport) != mycpuid) {
918 struct route *ro = &inp->inp_route;
920 if (ro->ro_rt != NULL)
922 bzero(ro, sizeof(*ro));
924 panic("UDP activity should only be in netisr0");
935 u_long udp_sendspace = 9216; /* really max datagram size */
936 /* 40 1K datagrams */
937 SYSCTL_INT(_net_inet_udp, UDPCTL_MAXDGRAM, maxdgram, CTLFLAG_RW,
938 &udp_sendspace, 0, "Maximum outgoing UDP datagram size");
940 u_long udp_recvspace = 40 * (1024 +
942 sizeof(struct sockaddr_in6)
944 sizeof(struct sockaddr_in)
947 SYSCTL_INT(_net_inet_udp, UDPCTL_RECVSPACE, recvspace, CTLFLAG_RW,
948 &udp_recvspace, 0, "Maximum incoming UDP datagram size");
951 * NOTE: (so) is referenced from soabort*() and netmsg_pru_abort()
952 * will sofree() it when we return.
955 udp_abort(netmsg_t msg)
957 struct socket *so = msg->abort.base.nm_so;
961 KKASSERT(&curthread->td_msgport == cpu_portfn(0));
965 soisdisconnected(so);
967 udbinfo_barrier_set();
969 udbinfo_barrier_rem();
974 lwkt_replymsg(&msg->abort.base.lmsg, error);
978 udp_attach(netmsg_t msg)
980 struct socket *so = msg->attach.base.nm_so;
981 struct pru_attach_info *ai = msg->attach.nm_ai;
985 KKASSERT(&curthread->td_msgport == cpu_portfn(0));
992 error = soreserve(so, udp_sendspace, udp_recvspace, ai->sb_rlimit);
996 udbinfo_barrier_set();
997 error = in_pcballoc(so, &udbinfo);
998 udbinfo_barrier_rem();
1004 * Set default port for protocol processing prior to bind/connect.
1006 sosetport(so, cpu_portfn(0));
1008 inp = (struct inpcb *)so->so_pcb;
1009 inp->inp_vflag |= INP_IPV4;
1010 inp->inp_ip_ttl = ip_defttl;
1013 lwkt_replymsg(&msg->attach.base.lmsg, error);
1017 udp_bind(netmsg_t msg)
1019 struct socket *so = msg->bind.base.nm_so;
1020 struct sockaddr *nam = msg->bind.nm_nam;
1021 struct thread *td = msg->bind.nm_td;
1022 struct sockaddr_in *sin = (struct sockaddr_in *)nam;
1028 error = in_pcbbind(inp, nam, td);
1030 if (sin->sin_addr.s_addr != INADDR_ANY)
1031 inp->inp_flags |= INP_WASBOUND_NOTANY;
1033 udbinfo_barrier_set();
1034 in_pcbinswildcardhash(inp);
1035 udbinfo_barrier_rem();
1040 lwkt_replymsg(&msg->bind.base.lmsg, error);
1044 udp_connect(netmsg_t msg)
1046 struct socket *so = msg->connect.base.nm_so;
1047 struct sockaddr *nam = msg->connect.nm_nam;
1048 struct thread *td = msg->connect.nm_td;
1050 struct sockaddr_in *sin = (struct sockaddr_in *)nam;
1051 struct sockaddr_in *if_sin;
1055 KKASSERT(&curthread->td_msgport == cpu_portfn(0));
1063 if (msg->connect.nm_reconnect & NMSG_RECONNECT_RECONNECT) {
1064 panic("UDP does not support RECONNECT\n");
1066 msg->connect.nm_reconnect &= ~NMSG_RECONNECT_RECONNECT;
1067 in_pcblink(inp, &udbinfo);
1071 if (inp->inp_faddr.s_addr != INADDR_ANY) {
1078 * Bind if we have to
1080 if (td->td_proc && td->td_proc->p_ucred->cr_prison != NULL &&
1081 inp->inp_laddr.s_addr == INADDR_ANY) {
1082 error = in_pcbbind(inp, NULL, td);
1088 * Calculate the correct protocol processing thread. The connect
1089 * operation must run there.
1091 error = in_pcbladdr(inp, nam, &if_sin, td);
1094 if (!prison_remote_ip(td, nam)) {
1095 error = EAFNOSUPPORT; /* IPv6 only jail */
1099 port = udp_addrport(sin->sin_addr.s_addr, sin->sin_port,
1100 inp->inp_laddr.s_addr, inp->inp_lport);
1102 if (port != &curthread->td_msgport) {
1104 struct route *ro = &inp->inp_route;
1107 * in_pcbladdr() may have allocated a route entry for us
1108 * on the current CPU, but we need a route entry on the
1109 * inpcb's owner CPU, so free it here.
1111 if (ro->ro_rt != NULL)
1113 bzero(ro, sizeof(*ro));
1116 * We are moving the protocol processing port the socket
1117 * is on, we have to unlink here and re-link on the
1120 in_pcbunlink(so->so_pcb, &udbinfo);
1121 /* in_pcbunlink(so->so_pcb, &udbinfo[mycpu->gd_cpuid]); */
1122 sosetport(so, port);
1123 msg->connect.nm_reconnect |= NMSG_RECONNECT_RECONNECT;
1124 msg->connect.base.nm_dispatch = udp_connect;
1126 lwkt_forwardmsg(port, &msg->connect.base.lmsg);
1127 /* msg invalid now */
1130 panic("UDP activity should only be in netisr0");
1134 KKASSERT(port == &curthread->td_msgport);
1135 error = udp_connect_oncpu(so, td, sin, if_sin);
1137 KKASSERT(msg->connect.nm_m == NULL);
1138 lwkt_replymsg(&msg->connect.base.lmsg, error);
1142 udp_connect_oncpu(struct socket *so, struct thread *td,
1143 struct sockaddr_in *sin, struct sockaddr_in *if_sin)
1148 udbinfo_barrier_set();
1151 if (inp->inp_flags & INP_WILDCARD)
1152 in_pcbremwildcardhash(inp);
1153 error = in_pcbconnect(inp, (struct sockaddr *)sin, td);
1157 * No more errors can occur, finish adjusting the socket
1158 * and change the processing port to reflect the connected
1159 * socket. Once set we can no longer safely mess with the
1163 } else if (error == EAFNOSUPPORT) { /* connection dissolved */
1165 * Follow traditional BSD behavior and retain
1166 * the local port binding. But, fix the old misbehavior
1167 * of overwriting any previously bound local address.
1169 if (!(inp->inp_flags & INP_WASBOUND_NOTANY))
1170 inp->inp_laddr.s_addr = INADDR_ANY;
1171 in_pcbinswildcardhash(inp);
1174 udbinfo_barrier_rem();
1179 udp_detach(netmsg_t msg)
1181 struct socket *so = msg->detach.base.nm_so;
1185 KKASSERT(&curthread->td_msgport == cpu_portfn(0));
1189 udbinfo_barrier_set();
1191 udbinfo_barrier_rem();
1196 lwkt_replymsg(&msg->detach.base.lmsg, error);
1200 udp_disconnect(netmsg_t msg)
1202 struct socket *so = msg->disconnect.base.nm_so;
1207 KKASSERT(&curthread->td_msgport == cpu_portfn(0));
1214 if (inp->inp_faddr.s_addr == INADDR_ANY) {
1221 udbinfo_barrier_set();
1222 in_pcbdisconnect(inp);
1223 udbinfo_barrier_rem();
1225 soclrstate(so, SS_ISCONNECTED); /* XXX */
1228 ro = &inp->inp_route;
1229 if (ro->ro_rt != NULL)
1231 bzero(ro, sizeof(*ro));
1234 lwkt_replymsg(&msg->disconnect.base.lmsg, error);
1238 udp_send(netmsg_t msg)
1240 struct socket *so = msg->send.base.nm_so;
1244 KKASSERT(&curthread->td_msgport == cpu_portfn(0));
1248 error = udp_output(inp,
1251 msg->send.nm_control,
1254 m_freem(msg->send.nm_m);
1257 msg->send.nm_m = NULL;
1258 lwkt_replymsg(&msg->send.base.lmsg, error);
1262 udp_shutdown(netmsg_t msg)
1264 struct socket *so = msg->shutdown.base.nm_so;
1268 KKASSERT(&curthread->td_msgport == cpu_portfn(0));
1277 lwkt_replymsg(&msg->shutdown.base.lmsg, error);
1283 lwkt_serialize_enter(&udbinfo_slize);
1287 udbinfo_unlock(void)
1289 lwkt_serialize_exit(&udbinfo_slize);
1293 udbinfo_barrier_set(void)
1295 netisr_barrier_set(udbinfo_br);
1300 udbinfo_barrier_rem(void)
1303 netisr_barrier_rem(udbinfo_br);
1306 struct pr_usrreqs udp_usrreqs = {
1307 .pru_abort = udp_abort,
1308 .pru_accept = pr_generic_notsupp,
1309 .pru_attach = udp_attach,
1310 .pru_bind = udp_bind,
1311 .pru_connect = udp_connect,
1312 .pru_connect2 = pr_generic_notsupp,
1313 .pru_control = in_control_dispatch,
1314 .pru_detach = udp_detach,
1315 .pru_disconnect = udp_disconnect,
1316 .pru_listen = pr_generic_notsupp,
1317 .pru_peeraddr = in_setpeeraddr_dispatch,
1318 .pru_rcvd = pr_generic_notsupp,
1319 .pru_rcvoob = pr_generic_notsupp,
1320 .pru_send = udp_send,
1321 .pru_sense = pru_sense_null,
1322 .pru_shutdown = udp_shutdown,
1323 .pru_sockaddr = in_setsockaddr_dispatch,
1324 .pru_sosend = sosendudp,
1325 .pru_soreceive = soreceive