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 $
70 #include "opt_ipsec.h"
71 #include "opt_inet6.h"
73 #include <sys/param.h>
74 #include <sys/systm.h>
75 #include <sys/kernel.h>
76 #include <sys/malloc.h>
78 #include <sys/domain.h>
81 #include <sys/protosw.h>
82 #include <sys/socket.h>
83 #include <sys/socketvar.h>
84 #include <sys/sysctl.h>
85 #include <sys/syslog.h>
86 #include <sys/in_cksum.h>
88 #include <sys/thread2.h>
89 #include <sys/socketvar2.h>
90 #include <sys/serialize.h>
92 #include <machine/stdarg.h>
95 #include <net/route.h>
96 #include <net/netmsg2.h>
98 #include <netinet/in.h>
99 #include <netinet/in_systm.h>
100 #include <netinet/ip.h>
102 #include <netinet/ip6.h>
104 #include <netinet/in_pcb.h>
105 #include <netinet/in_var.h>
106 #include <netinet/ip_var.h>
108 #include <netinet6/ip6_var.h>
110 #include <netinet/ip_icmp.h>
111 #include <netinet/icmp_var.h>
112 #include <netinet/udp.h>
113 #include <netinet/udp_var.h>
116 #include <netproto/ipsec/ipsec.h>
120 #include <netinet6/ipsec.h>
124 * UDP protocol implementation.
125 * Per RFC 768, August, 1980.
128 static int udpcksum = 1;
130 static int udpcksum = 0; /* XXX */
132 SYSCTL_INT(_net_inet_udp, UDPCTL_CHECKSUM, checksum, CTLFLAG_RW,
133 &udpcksum, 0, "Enable checksumming of UDP packets");
136 SYSCTL_INT(_net_inet_udp, OID_AUTO, log_in_vain, CTLFLAG_RW,
137 &log_in_vain, 0, "Log all incoming UDP packets");
139 static int blackhole = 0;
140 SYSCTL_INT(_net_inet_udp, OID_AUTO, blackhole, CTLFLAG_RW,
141 &blackhole, 0, "Do not send port unreachables for refused connects");
143 static int strict_mcast_mship = 1;
144 SYSCTL_INT(_net_inet_udp, OID_AUTO, strict_mcast_mship, CTLFLAG_RW,
145 &strict_mcast_mship, 0, "Only send multicast to member sockets");
147 int udp_sosend_async = 1;
148 SYSCTL_INT(_net_inet_udp, OID_AUTO, sosend_async, CTLFLAG_RW,
149 &udp_sosend_async, 0, "UDP asynchronized pru_send");
151 struct inpcbinfo udbinfo;
153 static struct netisr_barrier *udbinfo_br;
154 static struct lwkt_serialize udbinfo_slize = LWKT_SERIALIZE_INITIALIZER;
157 #define UDBHASHSIZE 16
160 struct udpstat udpstat; /* from udp_var.h */
161 SYSCTL_STRUCT(_net_inet_udp, UDPCTL_STATS, stats, CTLFLAG_RW,
162 &udpstat, udpstat, "UDP statistics (struct udpstat, netinet/udp_var.h)");
164 static struct sockaddr_in udp_in = { sizeof udp_in, AF_INET };
167 struct sockaddr_in6 uin6_sin;
168 u_char uin6_init_done : 1;
170 { sizeof udp_in6.uin6_sin, AF_INET6 },
174 struct ip6_hdr uip6_ip6;
175 u_char uip6_init_done : 1;
179 static void udp_append (struct inpcb *last, struct ip *ip,
180 struct mbuf *n, int off);
182 static void ip_2_ip6_hdr (struct ip6_hdr *ip6, struct ip *ip);
185 static int udp_connect_oncpu(struct socket *so, struct thread *td,
186 struct sockaddr_in *sin, struct sockaddr_in *if_sin);
187 static int udp_output (struct inpcb *, struct mbuf *, struct sockaddr *,
188 struct thread *, int);
193 in_pcbinfo_init(&udbinfo);
194 udbinfo.hashbase = hashinit(UDBHASHSIZE, M_PCB, &udbinfo.hashmask);
195 udbinfo.porthashbase = hashinit(UDBHASHSIZE, M_PCB,
196 &udbinfo.porthashmask);
197 udbinfo.wildcardhashbase = hashinit(UDBHASHSIZE, M_PCB,
198 &udbinfo.wildcardhashmask);
199 udbinfo.ipi_size = sizeof(struct inpcb);
201 udbinfo_br = netisr_barrier_create();
205 * Check multicast packets to make sure they are only sent to sockets with
206 * multicast memberships for the packet's destination address and arrival
207 * interface. Multicast packets to multicast-unaware sockets are also
210 * Returns 0 if the packet is acceptable, -1 if it is not.
213 check_multicast_membership(struct ip *ip, struct inpcb *inp, struct mbuf *m)
216 struct ip_moptions *mopt;
218 if (strict_mcast_mship == 0 ||
219 !IN_MULTICAST(ntohl(ip->ip_dst.s_addr))) {
222 mopt = inp->inp_moptions;
225 for (mshipno = 0; mshipno < mopt->imo_num_memberships; ++mshipno) {
226 struct in_multi *maddr = mopt->imo_membership[mshipno];
228 if (ip->ip_dst.s_addr == maddr->inm_addr.s_addr &&
229 m->m_pkthdr.rcvif == maddr->inm_ifp) {
237 udp_input(struct mbuf **mp, int *offp, int proto)
244 struct mbuf *opts = NULL;
247 struct sockaddr *append_sa;
254 udpstat.udps_ipackets++;
257 * Strip IP options, if any; should skip this,
258 * make available to user, and use on returned packets,
259 * but we don't yet have a way to check the checksum
260 * with options still present.
262 if (iphlen > sizeof(struct ip)) {
264 iphlen = sizeof(struct ip);
268 * IP and UDP headers are together in first mbuf.
269 * Already checked and pulled up in ip_demux().
271 KASSERT(m->m_len >= iphlen + sizeof(struct udphdr),
272 ("UDP header not in one mbuf"));
274 ip = mtod(m, struct ip *);
275 uh = (struct udphdr *)((caddr_t)ip + iphlen);
277 /* destination port of 0 is illegal, based on RFC768. */
278 if (uh->uh_dport == 0)
282 * Make mbuf data length reflect UDP length.
283 * If not enough data to reflect UDP length, drop.
285 len = ntohs((u_short)uh->uh_ulen);
286 if (ip->ip_len != len) {
287 if (len > ip->ip_len || len < sizeof(struct udphdr)) {
288 udpstat.udps_badlen++;
291 m_adj(m, len - ip->ip_len);
292 /* ip->ip_len = len; */
295 * Save a copy of the IP header in case we want restore it
296 * for sending an ICMP error message in response.
301 * Checksum extended UDP header and data.
304 if (m->m_pkthdr.csum_flags & CSUM_DATA_VALID) {
305 if (m->m_pkthdr.csum_flags & CSUM_PSEUDO_HDR)
306 uh->uh_sum = m->m_pkthdr.csum_data;
308 uh->uh_sum = in_pseudo(ip->ip_src.s_addr,
309 ip->ip_dst.s_addr, htonl((u_short)len +
310 m->m_pkthdr.csum_data + IPPROTO_UDP));
311 uh->uh_sum ^= 0xffff;
315 bcopy(((struct ipovly *)ip)->ih_x1, b, 9);
316 bzero(((struct ipovly *)ip)->ih_x1, 9);
317 ((struct ipovly *)ip)->ih_len = uh->uh_ulen;
318 uh->uh_sum = in_cksum(m, len + sizeof(struct ip));
319 bcopy(b, ((struct ipovly *)ip)->ih_x1, 9);
322 udpstat.udps_badsum++;
324 return(IPPROTO_DONE);
327 udpstat.udps_nosum++;
329 if (IN_MULTICAST(ntohl(ip->ip_dst.s_addr)) ||
330 in_broadcast(ip->ip_dst, m->m_pkthdr.rcvif)) {
334 * Deliver a multicast or broadcast datagram to *all* sockets
335 * for which the local and remote addresses and ports match
336 * those of the incoming datagram. This allows more than
337 * one process to receive multi/broadcasts on the same port.
338 * (This really ought to be done for unicast datagrams as
339 * well, but that would cause problems with existing
340 * applications that open both address-specific sockets and
341 * a wildcard socket listening to the same port -- they would
342 * end up receiving duplicates of every unicast datagram.
343 * Those applications open the multiple sockets to overcome an
344 * inadequacy of the UDP socket interface, but for backwards
345 * compatibility we avoid the problem here rather than
346 * fixing the interface. Maybe 4.5BSD will remedy this?)
350 * Construct sockaddr format source address.
352 udp_in.sin_port = uh->uh_sport;
353 udp_in.sin_addr = ip->ip_src;
355 * Locate pcb(s) for datagram.
356 * (Algorithm copied from raw_intr().)
360 udp_in6.uin6_init_done = udp_ip6.uip6_init_done = 0;
362 LIST_FOREACH(inp, &udbinfo.pcblisthead, inp_list) {
363 KKASSERT((inp->inp_flags & INP_PLACEMARKER) == 0);
365 if (!(inp->inp_vflag & INP_IPV4))
368 if (inp->inp_lport != uh->uh_dport)
370 if (inp->inp_laddr.s_addr != INADDR_ANY) {
371 if (inp->inp_laddr.s_addr !=
375 if (inp->inp_faddr.s_addr != INADDR_ANY) {
376 if (inp->inp_faddr.s_addr !=
378 inp->inp_fport != uh->uh_sport)
382 if (check_multicast_membership(ip, inp, m) < 0)
389 /* check AH/ESP integrity. */
390 if (ipsec4_in_reject_so(m, last->inp_socket))
391 ipsecstat.in_polvio++;
392 /* do not inject data to pcb */
396 /* check AH/ESP integrity. */
397 if (ipsec4_in_reject(m, last))
400 #endif /*FAST_IPSEC*/
401 if ((n = m_copypacket(m, MB_DONTWAIT)) != NULL)
402 udp_append(last, ip, n,
404 sizeof(struct udphdr));
408 * Don't look for additional matches if this one does
409 * not have either the SO_REUSEPORT or SO_REUSEADDR
410 * socket options set. This heuristic avoids searching
411 * through all pcbs in the common case of a non-shared
412 * port. It * assumes that an application will never
413 * clear these options after setting them.
415 if (!(last->inp_socket->so_options &
416 (SO_REUSEPORT | SO_REUSEADDR)))
422 * No matching pcb found; discard datagram.
423 * (No need to send an ICMP Port Unreachable
424 * for a broadcast or multicast datgram.)
426 udpstat.udps_noportbcast++;
430 /* check AH/ESP integrity. */
431 if (ipsec4_in_reject_so(m, last->inp_socket)) {
432 ipsecstat.in_polvio++;
437 /* check AH/ESP integrity. */
438 if (ipsec4_in_reject(m, last))
440 #endif /*FAST_IPSEC*/
441 udp_append(last, ip, m, iphlen + sizeof(struct udphdr));
442 return(IPPROTO_DONE);
445 * Locate pcb for datagram.
447 inp = in_pcblookup_hash(&udbinfo, ip->ip_src, uh->uh_sport,
448 ip->ip_dst, uh->uh_dport, 1, m->m_pkthdr.rcvif);
451 char buf[sizeof "aaa.bbb.ccc.ddd"];
453 strcpy(buf, inet_ntoa(ip->ip_dst));
455 "Connection attempt to UDP %s:%d from %s:%d\n",
456 buf, ntohs(uh->uh_dport), inet_ntoa(ip->ip_src),
457 ntohs(uh->uh_sport));
459 udpstat.udps_noport++;
460 if (m->m_flags & (M_BCAST | M_MCAST)) {
461 udpstat.udps_noportbcast++;
467 if (badport_bandlim(BANDLIM_ICMP_UNREACH) < 0)
471 ip->ip_len += iphlen;
472 icmp_error(m, ICMP_UNREACH, ICMP_UNREACH_PORT, 0, 0);
473 return(IPPROTO_DONE);
476 if (ipsec4_in_reject_so(m, inp->inp_socket)) {
477 ipsecstat.in_polvio++;
482 if (ipsec4_in_reject(m, inp))
484 #endif /*FAST_IPSEC*/
486 * Check the minimum TTL for socket.
488 if (ip->ip_ttl < inp->inp_ip_minttl)
492 * Construct sockaddr format source address.
493 * Stuff source address and datagram in user buffer.
495 udp_in.sin_port = uh->uh_sport;
496 udp_in.sin_addr = ip->ip_src;
497 if ((inp->inp_flags & INP_CONTROLOPTS) ||
498 (inp->inp_socket->so_options & SO_TIMESTAMP)) {
500 if (inp->inp_vflag & INP_IPV6) {
503 ip_2_ip6_hdr(&udp_ip6.uip6_ip6, ip);
504 savedflags = inp->inp_flags;
505 inp->inp_flags &= ~INP_UNMAPPABLEOPTS;
506 ip6_savecontrol(inp, &opts, &udp_ip6.uip6_ip6, m);
507 inp->inp_flags = savedflags;
510 ip_savecontrol(inp, &opts, ip, m);
512 m_adj(m, iphlen + sizeof(struct udphdr));
514 if (inp->inp_vflag & INP_IPV6) {
515 in6_sin_2_v4mapsin6(&udp_in, &udp_in6.uin6_sin);
516 append_sa = (struct sockaddr *)&udp_in6;
519 append_sa = (struct sockaddr *)&udp_in;
521 lwkt_gettoken(&inp->inp_socket->so_rcv.ssb_token);
522 if (ssb_appendaddr(&inp->inp_socket->so_rcv, append_sa, m, opts) == 0) {
523 udpstat.udps_fullsock++;
524 lwkt_reltoken(&inp->inp_socket->so_rcv.ssb_token);
527 lwkt_reltoken(&inp->inp_socket->so_rcv.ssb_token);
528 sorwakeup(inp->inp_socket);
529 return(IPPROTO_DONE);
534 return(IPPROTO_DONE);
539 ip_2_ip6_hdr(struct ip6_hdr *ip6, struct ip *ip)
541 bzero(ip6, sizeof *ip6);
543 ip6->ip6_vfc = IPV6_VERSION;
544 ip6->ip6_plen = ip->ip_len;
545 ip6->ip6_nxt = ip->ip_p;
546 ip6->ip6_hlim = ip->ip_ttl;
547 ip6->ip6_src.s6_addr32[2] = ip6->ip6_dst.s6_addr32[2] =
549 ip6->ip6_src.s6_addr32[3] = ip->ip_src.s_addr;
550 ip6->ip6_dst.s6_addr32[3] = ip->ip_dst.s_addr;
555 * subroutine of udp_input(), mainly for source code readability.
556 * caller must properly init udp_ip6 and udp_in6 beforehand.
559 udp_append(struct inpcb *last, struct ip *ip, struct mbuf *n, int off)
561 struct sockaddr *append_sa;
562 struct mbuf *opts = NULL;
564 if (last->inp_flags & INP_CONTROLOPTS ||
565 last->inp_socket->so_options & SO_TIMESTAMP) {
567 if (last->inp_vflag & INP_IPV6) {
570 if (udp_ip6.uip6_init_done == 0) {
571 ip_2_ip6_hdr(&udp_ip6.uip6_ip6, ip);
572 udp_ip6.uip6_init_done = 1;
574 savedflags = last->inp_flags;
575 last->inp_flags &= ~INP_UNMAPPABLEOPTS;
576 ip6_savecontrol(last, &opts, &udp_ip6.uip6_ip6, n);
577 last->inp_flags = savedflags;
580 ip_savecontrol(last, &opts, ip, n);
583 if (last->inp_vflag & INP_IPV6) {
584 if (udp_in6.uin6_init_done == 0) {
585 in6_sin_2_v4mapsin6(&udp_in, &udp_in6.uin6_sin);
586 udp_in6.uin6_init_done = 1;
588 append_sa = (struct sockaddr *)&udp_in6.uin6_sin;
591 append_sa = (struct sockaddr *)&udp_in;
593 lwkt_gettoken(&last->inp_socket->so_rcv.ssb_token);
594 if (ssb_appendaddr(&last->inp_socket->so_rcv, append_sa, n, opts) == 0) {
598 udpstat.udps_fullsock++;
600 sorwakeup(last->inp_socket);
602 lwkt_reltoken(&last->inp_socket->so_rcv.ssb_token);
606 * Notify a udp user of an asynchronous error;
607 * just wake up so that he can collect error status.
610 udp_notify(struct inpcb *inp, int error)
612 inp->inp_socket->so_error = error;
613 sorwakeup(inp->inp_socket);
614 sowwakeup(inp->inp_socket);
617 struct netmsg_udp_notify {
618 struct netmsg_base base;
619 void (*nm_notify)(struct inpcb *, int);
620 struct in_addr nm_faddr;
625 udp_notifyall_oncpu(netmsg_t msg)
627 struct netmsg_udp_notify *nm = (struct netmsg_udp_notify *)msg;
632 in_pcbnotifyall(&udbinfo.pcblisthead, nm->nm_faddr,
633 nm->nm_arg, nm->nm_notify);
634 lwkt_replymsg(&nm->base.lmsg, 0);
637 /* XXX currently udp only runs on cpu 0 */
638 nextcpu = mycpuid + 1;
639 if (nextcpu < ncpus2)
640 lwkt_forwardmsg(cpu_portfn(nextcpu), &nm->base.lmsg);
642 lwkt_replymsg(&nmsg->base.lmsg, 0);
647 udp_rtchange(struct inpcb *inp, int err)
650 /* XXX Nuke this, once UDP inpcbs are CPU localized */
651 if (inp->inp_route.ro_rt && inp->inp_route.ro_rt->rt_cpuid == mycpuid) {
652 rtfree(inp->inp_route.ro_rt);
653 inp->inp_route.ro_rt = NULL;
655 * A new route can be allocated the next time
656 * output is attempted.
660 in_rtchange(inp, err);
665 udp_ctlinput(netmsg_t msg)
667 struct sockaddr *sa = msg->ctlinput.nm_arg;
668 struct ip *ip = msg->ctlinput.nm_extra;
669 int cmd = msg->ctlinput.nm_cmd;
671 void (*notify) (struct inpcb *, int) = udp_notify;
672 struct in_addr faddr;
675 KKASSERT(&curthread->td_msgport == cpu_portfn(0));
677 faddr = ((struct sockaddr_in *)sa)->sin_addr;
678 if (sa->sa_family != AF_INET || faddr.s_addr == INADDR_ANY)
681 if (PRC_IS_REDIRECT(cmd)) {
683 notify = udp_rtchange;
684 } else if (cmd == PRC_HOSTDEAD) {
686 } else if ((unsigned)cmd >= PRC_NCMDS || inetctlerrmap[cmd] == 0) {
691 uh = (struct udphdr *)((caddr_t)ip + (ip->ip_hl << 2));
692 inp = in_pcblookup_hash(&udbinfo, faddr, uh->uh_dport,
693 ip->ip_src, uh->uh_sport, 0, NULL);
694 if (inp != NULL && inp->inp_socket != NULL)
695 (*notify)(inp, inetctlerrmap[cmd]);
696 } else if (PRC_IS_REDIRECT(cmd)) {
697 struct netmsg_udp_notify *nm;
699 KKASSERT(&curthread->td_msgport == cpu_portfn(0));
700 nm = kmalloc(sizeof(*nm), M_LWKTMSG, M_INTWAIT);
701 netmsg_init(&nm->base, NULL, &netisr_afree_rport,
702 0, udp_notifyall_oncpu);
703 nm->nm_faddr = faddr;
704 nm->nm_arg = inetctlerrmap[cmd];
705 nm->nm_notify = notify;
706 lwkt_sendmsg(cpu_portfn(0), &nm->base.lmsg);
709 * XXX We should forward msg upon PRC_HOSTHEAD and ip == NULL,
710 * once UDP inpcbs are CPU localized
712 KKASSERT(&curthread->td_msgport == cpu_portfn(0));
713 in_pcbnotifyall(&udbinfo.pcblisthead, faddr, inetctlerrmap[cmd],
717 lwkt_replymsg(&msg->lmsg, 0);
721 udp_pcblist(SYSCTL_HANDLER_ARGS)
727 error = in_pcblist_global_nomarker(oidp, arg1, arg2, req, &xi, &nxi);
731 KKASSERT(xi == NULL);
735 KKASSERT(xi == NULL);
739 for (i = 0; i < nxi; ++i) {
740 error = SYSCTL_OUT(req, &xi[i], sizeof(xi[i]));
748 SYSCTL_PROC(_net_inet_udp, UDPCTL_PCBLIST, pcblist, CTLFLAG_RD, &udbinfo, 0,
749 udp_pcblist, "S,xinpcb", "List of active UDP sockets");
752 udp_getcred(SYSCTL_HANDLER_ARGS)
754 struct sockaddr_in addrs[2];
755 struct ucred cred0, *cred = NULL;
759 error = priv_check(req->td, PRIV_ROOT);
762 error = SYSCTL_IN(req, addrs, sizeof addrs);
767 inp = in_pcblookup_hash(&udbinfo, addrs[1].sin_addr, addrs[1].sin_port,
768 addrs[0].sin_addr, addrs[0].sin_port, 1, NULL);
769 if (inp == NULL || inp->inp_socket == NULL) {
772 if (inp->inp_socket->so_cred != NULL) {
773 cred0 = *(inp->inp_socket->so_cred);
782 return SYSCTL_OUT(req, cred, sizeof(struct ucred));
785 SYSCTL_PROC(_net_inet_udp, OID_AUTO, getcred, CTLTYPE_OPAQUE|CTLFLAG_RW,
786 0, 0, udp_getcred, "S,ucred", "Get the ucred of a UDP connection");
789 udp_output(struct inpcb *inp, struct mbuf *m, struct sockaddr *dstaddr,
790 struct thread *td, int flags)
793 int len = m->m_pkthdr.len;
794 struct sockaddr_in *sin; /* really is initialized before use */
795 int error = 0, lport_any = 0;
797 if (len + sizeof(struct udpiphdr) > IP_MAXPACKET) {
802 if (inp->inp_lport == 0) { /* unbound socket */
803 error = in_pcbbind(inp, NULL, td);
807 udbinfo_barrier_set();
808 in_pcbinswildcardhash(inp);
809 udbinfo_barrier_rem();
813 if (dstaddr != NULL) { /* destination address specified */
814 if (inp->inp_faddr.s_addr != INADDR_ANY) {
815 /* already connected */
819 sin = (struct sockaddr_in *)dstaddr;
820 if (!prison_remote_ip(td, (struct sockaddr *)&sin)) {
821 error = EAFNOSUPPORT; /* IPv6 only jail */
825 if (inp->inp_faddr.s_addr == INADDR_ANY) {
826 /* no destination specified and not already connected */
834 * Calculate data length and get a mbuf
835 * for UDP and IP headers.
837 M_PREPEND(m, sizeof(struct udpiphdr), MB_DONTWAIT);
844 * Fill in mbuf with extended UDP header
845 * and addresses and length put into network format.
847 ui = mtod(m, struct udpiphdr *);
848 bzero(ui->ui_x1, sizeof ui->ui_x1); /* XXX still needed? */
849 ui->ui_pr = IPPROTO_UDP;
852 * Set destination address.
854 if (dstaddr != NULL) { /* use specified destination */
855 ui->ui_dst = sin->sin_addr;
856 ui->ui_dport = sin->sin_port;
857 } else { /* use connected destination */
858 ui->ui_dst = inp->inp_faddr;
859 ui->ui_dport = inp->inp_fport;
863 * Set source address.
865 if (inp->inp_laddr.s_addr == INADDR_ANY) {
866 struct sockaddr_in *if_sin;
868 if (dstaddr == NULL) {
870 * connect() had (or should have) failed because
871 * the interface had no IP address, but the
872 * application proceeded to call send() anyways.
878 /* Look up outgoing interface. */
879 if ((error = in_pcbladdr(inp, dstaddr, &if_sin, td)))
881 ui->ui_src = if_sin->sin_addr; /* use address of interface */
883 ui->ui_src = inp->inp_laddr; /* use non-null bound address */
885 ui->ui_sport = inp->inp_lport;
886 KASSERT(inp->inp_lport != 0, ("inp lport should have been bound"));
888 ui->ui_ulen = htons((u_short)len + sizeof(struct udphdr));
891 * Set up checksum and output datagram.
894 ui->ui_sum = in_pseudo(ui->ui_src.s_addr, ui->ui_dst.s_addr,
895 htons((u_short)len + sizeof(struct udphdr) + IPPROTO_UDP));
896 m->m_pkthdr.csum_flags = CSUM_UDP;
897 m->m_pkthdr.csum_data = offsetof(struct udphdr, uh_sum);
901 ((struct ip *)ui)->ip_len = sizeof(struct udpiphdr) + len;
902 ((struct ip *)ui)->ip_ttl = inp->inp_ip_ttl; /* XXX */
903 ((struct ip *)ui)->ip_tos = inp->inp_ip_tos; /* XXX */
904 udpstat.udps_opackets++;
906 error = ip_output(m, inp->inp_options, &inp->inp_route,
907 (inp->inp_socket->so_options & (SO_DONTROUTE | SO_BROADCAST)) |
908 flags | IP_DEBUGROUTE,
909 inp->inp_moptions, inp);
912 * If this is the first data gram sent on an unbound and unconnected
913 * UDP socket, lport will be changed in this function. If target
914 * CPU after this lport changing is no longer the current CPU, then
915 * free the route entry allocated on the current CPU.
918 if (udp_addrcpu(inp->inp_faddr.s_addr, inp->inp_fport,
919 inp->inp_laddr.s_addr, inp->inp_lport) != mycpuid) {
921 struct route *ro = &inp->inp_route;
923 if (ro->ro_rt != NULL)
925 bzero(ro, sizeof(*ro));
927 panic("UDP activity should only be in netisr0");
938 u_long udp_sendspace = 9216; /* really max datagram size */
939 /* 40 1K datagrams */
940 SYSCTL_INT(_net_inet_udp, UDPCTL_MAXDGRAM, maxdgram, CTLFLAG_RW,
941 &udp_sendspace, 0, "Maximum outgoing UDP datagram size");
943 u_long udp_recvspace = 40 * (1024 +
945 sizeof(struct sockaddr_in6)
947 sizeof(struct sockaddr_in)
950 SYSCTL_INT(_net_inet_udp, UDPCTL_RECVSPACE, recvspace, CTLFLAG_RW,
951 &udp_recvspace, 0, "Maximum incoming UDP datagram size");
954 * NOTE: (so) is referenced from soabort*() and netmsg_pru_abort()
955 * will sofree() it when we return.
958 udp_abort(netmsg_t msg)
960 struct socket *so = msg->abort.base.nm_so;
964 KKASSERT(&curthread->td_msgport == cpu_portfn(0));
968 soisdisconnected(so);
970 udbinfo_barrier_set();
972 udbinfo_barrier_rem();
977 lwkt_replymsg(&msg->abort.base.lmsg, error);
981 udp_attach(netmsg_t msg)
983 struct socket *so = msg->attach.base.nm_so;
984 struct pru_attach_info *ai = msg->attach.nm_ai;
988 KKASSERT(&curthread->td_msgport == cpu_portfn(0));
995 error = soreserve(so, udp_sendspace, udp_recvspace, ai->sb_rlimit);
999 udbinfo_barrier_set();
1000 error = in_pcballoc(so, &udbinfo);
1001 udbinfo_barrier_rem();
1007 * Set default port for protocol processing prior to bind/connect.
1009 sosetport(so, cpu_portfn(0));
1011 inp = (struct inpcb *)so->so_pcb;
1012 inp->inp_vflag |= INP_IPV4;
1013 inp->inp_ip_ttl = ip_defttl;
1016 lwkt_replymsg(&msg->attach.base.lmsg, error);
1020 udp_bind(netmsg_t msg)
1022 struct socket *so = msg->bind.base.nm_so;
1023 struct sockaddr *nam = msg->bind.nm_nam;
1024 struct thread *td = msg->bind.nm_td;
1025 struct sockaddr_in *sin = (struct sockaddr_in *)nam;
1031 error = in_pcbbind(inp, nam, td);
1033 if (sin->sin_addr.s_addr != INADDR_ANY)
1034 inp->inp_flags |= INP_WASBOUND_NOTANY;
1036 udbinfo_barrier_set();
1037 in_pcbinswildcardhash(inp);
1038 udbinfo_barrier_rem();
1043 lwkt_replymsg(&msg->bind.base.lmsg, error);
1047 udp_connect(netmsg_t msg)
1049 struct socket *so = msg->connect.base.nm_so;
1050 struct sockaddr *nam = msg->connect.nm_nam;
1051 struct thread *td = msg->connect.nm_td;
1053 struct sockaddr_in *sin = (struct sockaddr_in *)nam;
1054 struct sockaddr_in *if_sin;
1058 KKASSERT(&curthread->td_msgport == cpu_portfn(0));
1066 if (msg->connect.nm_reconnect & NMSG_RECONNECT_RECONNECT) {
1067 panic("UDP does not support RECONNECT");
1069 msg->connect.nm_reconnect &= ~NMSG_RECONNECT_RECONNECT;
1070 in_pcblink(inp, &udbinfo);
1074 if (inp->inp_faddr.s_addr != INADDR_ANY) {
1081 * Bind if we have to
1083 if (td->td_proc && td->td_proc->p_ucred->cr_prison != NULL &&
1084 inp->inp_laddr.s_addr == INADDR_ANY) {
1085 error = in_pcbbind(inp, NULL, td);
1091 * Calculate the correct protocol processing thread. The connect
1092 * operation must run there.
1094 error = in_pcbladdr(inp, nam, &if_sin, td);
1097 if (!prison_remote_ip(td, nam)) {
1098 error = EAFNOSUPPORT; /* IPv6 only jail */
1102 port = udp_addrport(sin->sin_addr.s_addr, sin->sin_port,
1103 inp->inp_laddr.s_addr, inp->inp_lport);
1105 if (port != &curthread->td_msgport) {
1107 struct route *ro = &inp->inp_route;
1110 * in_pcbladdr() may have allocated a route entry for us
1111 * on the current CPU, but we need a route entry on the
1112 * inpcb's owner CPU, so free it here.
1114 if (ro->ro_rt != NULL)
1116 bzero(ro, sizeof(*ro));
1119 * We are moving the protocol processing port the socket
1120 * is on, we have to unlink here and re-link on the
1123 in_pcbunlink(so->so_pcb, &udbinfo);
1124 /* in_pcbunlink(so->so_pcb, &udbinfo[mycpu->gd_cpuid]); */
1125 sosetport(so, port);
1126 msg->connect.nm_reconnect |= NMSG_RECONNECT_RECONNECT;
1127 msg->connect.base.nm_dispatch = udp_connect;
1129 lwkt_forwardmsg(port, &msg->connect.base.lmsg);
1130 /* msg invalid now */
1133 panic("UDP activity should only be in netisr0");
1137 KKASSERT(port == &curthread->td_msgport);
1138 error = udp_connect_oncpu(so, td, sin, if_sin);
1140 KKASSERT(msg->connect.nm_m == NULL);
1141 lwkt_replymsg(&msg->connect.base.lmsg, error);
1145 udp_connect_oncpu(struct socket *so, struct thread *td,
1146 struct sockaddr_in *sin, struct sockaddr_in *if_sin)
1151 udbinfo_barrier_set();
1154 if (inp->inp_flags & INP_WILDCARD)
1155 in_pcbremwildcardhash(inp);
1156 error = in_pcbconnect(inp, (struct sockaddr *)sin, td);
1160 * No more errors can occur, finish adjusting the socket
1161 * and change the processing port to reflect the connected
1162 * socket. Once set we can no longer safely mess with the
1166 } else if (error == EAFNOSUPPORT) { /* connection dissolved */
1168 * Follow traditional BSD behavior and retain
1169 * the local port binding. But, fix the old misbehavior
1170 * of overwriting any previously bound local address.
1172 if (!(inp->inp_flags & INP_WASBOUND_NOTANY))
1173 inp->inp_laddr.s_addr = INADDR_ANY;
1174 in_pcbinswildcardhash(inp);
1177 udbinfo_barrier_rem();
1182 udp_detach(netmsg_t msg)
1184 struct socket *so = msg->detach.base.nm_so;
1188 KKASSERT(&curthread->td_msgport == cpu_portfn(0));
1192 udbinfo_barrier_set();
1194 udbinfo_barrier_rem();
1199 lwkt_replymsg(&msg->detach.base.lmsg, error);
1203 udp_disconnect(netmsg_t msg)
1205 struct socket *so = msg->disconnect.base.nm_so;
1210 KKASSERT(&curthread->td_msgport == cpu_portfn(0));
1217 if (inp->inp_faddr.s_addr == INADDR_ANY) {
1224 udbinfo_barrier_set();
1225 in_pcbdisconnect(inp);
1226 udbinfo_barrier_rem();
1228 soclrstate(so, SS_ISCONNECTED); /* XXX */
1231 ro = &inp->inp_route;
1232 if (ro->ro_rt != NULL)
1234 bzero(ro, sizeof(*ro));
1237 lwkt_replymsg(&msg->disconnect.base.lmsg, error);
1241 udp_send(netmsg_t msg)
1243 struct socket *so = msg->send.base.nm_so;
1244 struct mbuf *m = msg->send.nm_m;
1245 struct sockaddr *addr = msg->send.nm_addr;
1246 int pru_flags = msg->send.nm_flags;
1250 KKASSERT(&curthread->td_msgport == cpu_portfn(0));
1251 KKASSERT(msg->send.nm_control == NULL);
1255 struct thread *td = msg->send.nm_td;
1258 if (pru_flags & PRUS_DONTROUTE)
1259 flags |= SO_DONTROUTE;
1260 error = udp_output(inp, m, addr, td, flags);
1266 if (pru_flags & PRUS_FREEADDR)
1267 kfree(addr, M_SONAME);
1269 if ((pru_flags & PRUS_NOREPLY) == 0)
1270 lwkt_replymsg(&msg->send.base.lmsg, error);
1274 udp_shutdown(netmsg_t msg)
1276 struct socket *so = msg->shutdown.base.nm_so;
1280 KKASSERT(&curthread->td_msgport == cpu_portfn(0));
1289 lwkt_replymsg(&msg->shutdown.base.lmsg, error);
1295 lwkt_serialize_enter(&udbinfo_slize);
1299 udbinfo_unlock(void)
1301 lwkt_serialize_exit(&udbinfo_slize);
1305 udbinfo_barrier_set(void)
1307 netisr_barrier_set(udbinfo_br);
1312 udbinfo_barrier_rem(void)
1315 netisr_barrier_rem(udbinfo_br);
1318 struct pr_usrreqs udp_usrreqs = {
1319 .pru_abort = udp_abort,
1320 .pru_accept = pr_generic_notsupp,
1321 .pru_attach = udp_attach,
1322 .pru_bind = udp_bind,
1323 .pru_connect = udp_connect,
1324 .pru_connect2 = pr_generic_notsupp,
1325 .pru_control = in_control_dispatch,
1326 .pru_detach = udp_detach,
1327 .pru_disconnect = udp_disconnect,
1328 .pru_listen = pr_generic_notsupp,
1329 .pru_peeraddr = in_setpeeraddr_dispatch,
1330 .pru_rcvd = pr_generic_notsupp,
1331 .pru_rcvoob = pr_generic_notsupp,
1332 .pru_send = udp_send,
1333 .pru_sense = pru_sense_null,
1334 .pru_shutdown = udp_shutdown,
1335 .pru_sockaddr = in_setsockaddr_dispatch,
1336 .pru_sosend = sosendudp,
1337 .pru_soreceive = soreceive