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.46 2008/09/23 11:28:49 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>
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/thread2.h>
87 #include <sys/in_cksum.h>
89 #include <machine/stdarg.h>
91 #include <vm/vm_zone.h>
94 #include <net/route.h>
96 #include <netinet/in.h>
97 #include <netinet/in_systm.h>
98 #include <netinet/ip.h>
100 #include <netinet/ip6.h>
102 #include <netinet/in_pcb.h>
103 #include <netinet/in_var.h>
104 #include <netinet/ip_var.h>
106 #include <netinet6/ip6_var.h>
108 #include <netinet/ip_icmp.h>
109 #include <netinet/icmp_var.h>
110 #include <netinet/udp.h>
111 #include <netinet/udp_var.h>
114 #include <netproto/ipsec/ipsec.h>
118 #include <netinet6/ipsec.h>
121 int udp_mpsafe_proto = 0;
122 TUNABLE_INT("net.inet.udp.mpsafe_proto", &udp_mpsafe_proto);
124 int udp_mpsafe_thread = 0;
125 TUNABLE_INT("net.inet.udp.mpsafe_thread", &udp_mpsafe_thread);
126 SYSCTL_INT(_net_inet_udp, OID_AUTO, mpsafe_thread, CTLFLAG_RW,
127 &udp_mpsafe_thread, 0,
128 "0:BGL, 1:Adaptive BGL, 2:No BGL(experimental)");
131 * UDP protocol implementation.
132 * Per RFC 768, August, 1980.
135 static int udpcksum = 1;
137 static int udpcksum = 0; /* XXX */
139 SYSCTL_INT(_net_inet_udp, UDPCTL_CHECKSUM, checksum, CTLFLAG_RW,
143 SYSCTL_INT(_net_inet_udp, OID_AUTO, log_in_vain, CTLFLAG_RW,
144 &log_in_vain, 0, "Log all incoming UDP packets");
146 static int blackhole = 0;
147 SYSCTL_INT(_net_inet_udp, OID_AUTO, blackhole, CTLFLAG_RW,
148 &blackhole, 0, "Do not send port unreachables for refused connects");
150 static int strict_mcast_mship = 1;
151 SYSCTL_INT(_net_inet_udp, OID_AUTO, strict_mcast_mship, CTLFLAG_RW,
152 &strict_mcast_mship, 0, "Only send multicast to member sockets");
154 struct inpcbinfo udbinfo;
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_detach (struct socket *so);
186 static int udp_output (struct inpcb *, struct mbuf *, struct sockaddr *,
187 struct mbuf *, struct thread *);
192 in_pcbinfo_init(&udbinfo);
193 udbinfo.hashbase = hashinit(UDBHASHSIZE, M_PCB, &udbinfo.hashmask);
194 udbinfo.porthashbase = hashinit(UDBHASHSIZE, M_PCB,
195 &udbinfo.porthashmask);
196 udbinfo.wildcardhashbase = hashinit(UDBHASHSIZE, M_PCB,
197 &udbinfo.wildcardhashmask);
198 udbinfo.ipi_zone = zinit("udpcb", sizeof(struct inpcb), maxsockets,
204 * Check multicast packets to make sure they are only sent to sockets with
205 * multicast memberships for the packet's destination address and arrival
206 * interface. Multicast packets to multicast-unaware sockets are also
209 * Returns 0 if the packet is acceptable, -1 if it is not.
212 check_multicast_membership(struct ip *ip, struct inpcb *inp, struct mbuf *m)
215 struct ip_moptions *mopt;
217 if (strict_mcast_mship == 0 ||
218 !IN_MULTICAST(ntohl(ip->ip_dst.s_addr))) {
221 mopt = inp->inp_moptions;
224 for (mshipno = 0; mshipno <= mopt->imo_num_memberships; ++mshipno) {
225 struct in_multi *maddr = mopt->imo_membership[mshipno];
227 if (ip->ip_dst.s_addr == maddr->inm_addr.s_addr &&
228 m->m_pkthdr.rcvif == maddr->inm_ifp) {
236 udp_input(struct mbuf *m, ...)
242 struct mbuf *opts = NULL;
245 struct sockaddr *append_sa;
249 off = __va_arg(ap, int);
250 proto = __va_arg(ap, int);
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++;
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 if (inp->inp_flags & INP_PLACEMARKER)
366 if (!(inp->inp_vflag & INP_IPV4))
369 if (inp->inp_lport != uh->uh_dport)
371 if (inp->inp_laddr.s_addr != INADDR_ANY) {
372 if (inp->inp_laddr.s_addr !=
376 if (inp->inp_faddr.s_addr != INADDR_ANY) {
377 if (inp->inp_faddr.s_addr !=
379 inp->inp_fport != uh->uh_sport)
383 if (check_multicast_membership(ip, inp, m) < 0)
390 /* check AH/ESP integrity. */
391 if (ipsec4_in_reject_so(m, last->inp_socket))
392 ipsecstat.in_polvio++;
393 /* do not inject data to pcb */
397 /* check AH/ESP integrity. */
398 if (ipsec4_in_reject(m, last))
401 #endif /*FAST_IPSEC*/
402 if ((n = m_copypacket(m, MB_DONTWAIT)) != NULL)
403 udp_append(last, ip, n,
405 sizeof(struct udphdr));
409 * Don't look for additional matches if this one does
410 * not have either the SO_REUSEPORT or SO_REUSEADDR
411 * socket options set. This heuristic avoids searching
412 * through all pcbs in the common case of a non-shared
413 * port. It * assumes that an application will never
414 * clear these options after setting them.
416 if (!(last->inp_socket->so_options &
417 (SO_REUSEPORT | SO_REUSEADDR)))
423 * No matching pcb found; discard datagram.
424 * (No need to send an ICMP Port Unreachable
425 * for a broadcast or multicast datgram.)
427 udpstat.udps_noportbcast++;
431 /* check AH/ESP integrity. */
432 if (ipsec4_in_reject_so(m, last->inp_socket)) {
433 ipsecstat.in_polvio++;
438 /* check AH/ESP integrity. */
439 if (ipsec4_in_reject(m, last))
441 #endif /*FAST_IPSEC*/
442 udp_append(last, ip, m, iphlen + sizeof(struct udphdr));
446 * Locate pcb for datagram.
448 inp = in_pcblookup_hash(&udbinfo, ip->ip_src, uh->uh_sport,
449 ip->ip_dst, uh->uh_dport, 1, m->m_pkthdr.rcvif);
452 char buf[sizeof "aaa.bbb.ccc.ddd"];
454 strcpy(buf, inet_ntoa(ip->ip_dst));
456 "Connection attempt to UDP %s:%d from %s:%d\n",
457 buf, ntohs(uh->uh_dport), inet_ntoa(ip->ip_src),
458 ntohs(uh->uh_sport));
460 udpstat.udps_noport++;
461 if (m->m_flags & (M_BCAST | M_MCAST)) {
462 udpstat.udps_noportbcast++;
468 if (badport_bandlim(BANDLIM_ICMP_UNREACH) < 0)
472 ip->ip_len += iphlen;
473 icmp_error(m, ICMP_UNREACH, ICMP_UNREACH_PORT, 0, 0);
477 if (ipsec4_in_reject_so(m, inp->inp_socket)) {
478 ipsecstat.in_polvio++;
483 if (ipsec4_in_reject(m, inp))
485 #endif /*FAST_IPSEC*/
487 * Check the minimum TTL for socket.
489 if (ip->ip_ttl < inp->inp_ip_minttl)
493 * Construct sockaddr format source address.
494 * Stuff source address and datagram in user buffer.
496 udp_in.sin_port = uh->uh_sport;
497 udp_in.sin_addr = ip->ip_src;
498 if ((inp->inp_flags & INP_CONTROLOPTS) ||
499 (inp->inp_socket->so_options & SO_TIMESTAMP)) {
501 if (inp->inp_vflag & INP_IPV6) {
504 ip_2_ip6_hdr(&udp_ip6.uip6_ip6, ip);
505 savedflags = inp->inp_flags;
506 inp->inp_flags &= ~INP_UNMAPPABLEOPTS;
507 ip6_savecontrol(inp, &opts, &udp_ip6.uip6_ip6, m);
508 inp->inp_flags = savedflags;
511 ip_savecontrol(inp, &opts, ip, m);
513 m_adj(m, iphlen + sizeof(struct udphdr));
515 if (inp->inp_vflag & INP_IPV6) {
516 in6_sin_2_v4mapsin6(&udp_in, &udp_in6.uin6_sin);
517 append_sa = (struct sockaddr *)&udp_in6;
520 append_sa = (struct sockaddr *)&udp_in;
521 if (ssb_appendaddr(&inp->inp_socket->so_rcv, append_sa, m, opts) == 0) {
522 udpstat.udps_fullsock++;
525 sorwakeup(inp->inp_socket);
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 if (ssb_appendaddr(&last->inp_socket->so_rcv, append_sa, n, opts) == 0) {
594 udpstat.udps_fullsock++;
596 sorwakeup(last->inp_socket);
600 * Notify a udp user of an asynchronous error;
601 * just wake up so that he can collect error status.
604 udp_notify(struct inpcb *inp, int error)
606 inp->inp_socket->so_error = error;
607 sorwakeup(inp->inp_socket);
608 sowwakeup(inp->inp_socket);
612 udp_ctlinput(int cmd, struct sockaddr *sa, void *vip)
616 void (*notify) (struct inpcb *, int) = udp_notify;
617 struct in_addr faddr;
620 faddr = ((struct sockaddr_in *)sa)->sin_addr;
621 if (sa->sa_family != AF_INET || faddr.s_addr == INADDR_ANY)
624 if (PRC_IS_REDIRECT(cmd)) {
626 notify = in_rtchange;
627 } else if (cmd == PRC_HOSTDEAD)
629 else if ((unsigned)cmd >= PRC_NCMDS || inetctlerrmap[cmd] == 0)
633 uh = (struct udphdr *)((caddr_t)ip + (ip->ip_hl << 2));
634 inp = in_pcblookup_hash(&udbinfo, faddr, uh->uh_dport,
635 ip->ip_src, uh->uh_sport, 0, NULL);
636 if (inp != NULL && inp->inp_socket != NULL)
637 (*notify)(inp, inetctlerrmap[cmd]);
640 in_pcbnotifyall(&udbinfo.pcblisthead, faddr, inetctlerrmap[cmd],
644 SYSCTL_PROC(_net_inet_udp, UDPCTL_PCBLIST, pcblist, CTLFLAG_RD, &udbinfo, 0,
645 in_pcblist_global, "S,xinpcb", "List of active UDP sockets");
648 udp_getcred(SYSCTL_HANDLER_ARGS)
650 struct sockaddr_in addrs[2];
654 error = suser(req->td);
657 error = SYSCTL_IN(req, addrs, sizeof addrs);
661 inp = in_pcblookup_hash(&udbinfo, addrs[1].sin_addr, addrs[1].sin_port,
662 addrs[0].sin_addr, addrs[0].sin_port, 1, NULL);
663 if (inp == NULL || inp->inp_socket == NULL) {
667 error = SYSCTL_OUT(req, inp->inp_socket->so_cred, sizeof(struct ucred));
673 SYSCTL_PROC(_net_inet_udp, OID_AUTO, getcred, CTLTYPE_OPAQUE|CTLFLAG_RW,
674 0, 0, udp_getcred, "S,ucred", "Get the ucred of a UDP connection");
677 udp_output(struct inpcb *inp, struct mbuf *m, struct sockaddr *dstaddr,
678 struct mbuf *control, struct thread *td)
681 int len = m->m_pkthdr.len;
682 struct sockaddr_in *sin; /* really is initialized before use */
683 int error = 0, lport_any = 0;
685 if (len + sizeof(struct udpiphdr) > IP_MAXPACKET) {
690 if (inp->inp_lport == 0) { /* unbound socket */
691 error = in_pcbbind(inp, (struct sockaddr *)NULL, td);
694 in_pcbinswildcardhash(inp);
698 if (dstaddr != NULL) { /* destination address specified */
699 if (inp->inp_faddr.s_addr != INADDR_ANY) {
700 /* already connected */
704 sin = (struct sockaddr_in *)dstaddr;
705 if (!prison_remote_ip(td, (struct sockaddr *)&sin)) {
706 error = EAFNOSUPPORT; /* IPv6 only jail */
710 if (inp->inp_faddr.s_addr == INADDR_ANY) {
711 /* no destination specified and not already connected */
719 * Calculate data length and get a mbuf
720 * for UDP and IP headers.
722 M_PREPEND(m, sizeof(struct udpiphdr), MB_DONTWAIT);
729 * Fill in mbuf with extended UDP header
730 * and addresses and length put into network format.
732 ui = mtod(m, struct udpiphdr *);
733 bzero(ui->ui_x1, sizeof ui->ui_x1); /* XXX still needed? */
734 ui->ui_pr = IPPROTO_UDP;
737 * Set destination address.
739 if (dstaddr != NULL) { /* use specified destination */
740 ui->ui_dst = sin->sin_addr;
741 ui->ui_dport = sin->sin_port;
742 } else { /* use connected destination */
743 ui->ui_dst = inp->inp_faddr;
744 ui->ui_dport = inp->inp_fport;
748 * Set source address.
750 if (inp->inp_laddr.s_addr == INADDR_ANY) {
751 struct sockaddr_in *if_sin;
753 if (dstaddr == NULL) {
755 * connect() had (or should have) failed because
756 * the interface had no IP address, but the
757 * application proceeded to call send() anyways.
763 /* Look up outgoing interface. */
764 if ((error = in_pcbladdr(inp, dstaddr, &if_sin, td)))
766 ui->ui_src = if_sin->sin_addr; /* use address of interface */
768 ui->ui_src = inp->inp_laddr; /* use non-null bound address */
770 ui->ui_sport = inp->inp_lport;
771 KASSERT(inp->inp_lport != 0, ("inp lport should have been bound"));
773 ui->ui_ulen = htons((u_short)len + sizeof(struct udphdr));
776 * Set up checksum and output datagram.
779 ui->ui_sum = in_pseudo(ui->ui_src.s_addr, ui->ui_dst.s_addr,
780 htons((u_short)len + sizeof(struct udphdr) + IPPROTO_UDP));
781 m->m_pkthdr.csum_flags = CSUM_UDP;
782 m->m_pkthdr.csum_data = offsetof(struct udphdr, uh_sum);
786 ((struct ip *)ui)->ip_len = sizeof(struct udpiphdr) + len;
787 ((struct ip *)ui)->ip_ttl = inp->inp_ip_ttl; /* XXX */
788 ((struct ip *)ui)->ip_tos = inp->inp_ip_tos; /* XXX */
789 udpstat.udps_opackets++;
791 error = ip_output(m, inp->inp_options, &inp->inp_route,
792 (inp->inp_socket->so_options & (SO_DONTROUTE | SO_BROADCAST)) |
794 inp->inp_moptions, inp);
797 * If this is the first data gram sent on an unbound and unconnected
798 * UDP socket, lport will be changed in this function. If target
799 * CPU after this lport changing is no longer the current CPU, then
800 * free the route entry allocated on the current CPU.
803 if (udp_addrcpu(inp->inp_faddr.s_addr, inp->inp_fport,
804 inp->inp_laddr.s_addr, inp->inp_lport) != mycpuid) {
805 struct route *ro = &inp->inp_route;
807 if (ro->ro_rt != NULL)
809 bzero(ro, sizeof(*ro));
819 u_long udp_sendspace = 9216; /* really max datagram size */
820 /* 40 1K datagrams */
821 SYSCTL_INT(_net_inet_udp, UDPCTL_MAXDGRAM, maxdgram, CTLFLAG_RW,
822 &udp_sendspace, 0, "Maximum outgoing UDP datagram size");
824 u_long udp_recvspace = 40 * (1024 +
826 sizeof(struct sockaddr_in6)
828 sizeof(struct sockaddr_in)
831 SYSCTL_INT(_net_inet_udp, UDPCTL_RECVSPACE, recvspace, CTLFLAG_RW,
832 &udp_recvspace, 0, "Maximum incoming UDP datagram size");
835 udp_abort(struct socket *so)
841 return EINVAL; /* ??? possible? panic instead? */
842 soisdisconnected(so);
850 udp_attach(struct socket *so, int proto, struct pru_attach_info *ai)
859 error = soreserve(so, udp_sendspace, udp_recvspace, ai->sb_rlimit);
863 error = in_pcballoc(so, &udbinfo);
868 inp = (struct inpcb *)so->so_pcb;
869 inp->inp_vflag |= INP_IPV4;
870 inp->inp_ip_ttl = ip_defttl;
875 udp_bind(struct socket *so, struct sockaddr *nam, struct thread *td)
877 struct sockaddr_in *sin = (struct sockaddr_in *)nam;
885 error = in_pcbbind(inp, nam, td);
888 if (sin->sin_addr.s_addr != INADDR_ANY)
889 inp->inp_flags |= INP_WASBOUND_NOTANY;
890 in_pcbinswildcardhash(inp);
896 udp_connect(struct socket *so, struct sockaddr *nam, struct thread *td)
904 if (inp->inp_faddr.s_addr != INADDR_ANY)
908 if (td->td_proc && td->td_proc->p_ucred->cr_prison != NULL &&
909 inp->inp_laddr.s_addr == INADDR_ANY) {
910 error = in_pcbbind(inp, NULL, td);
913 if (!prison_remote_ip(td, nam))
914 return(EAFNOSUPPORT); /* IPv6 only jail */
915 if (inp->inp_flags & INP_WILDCARD)
916 in_pcbremwildcardhash(inp);
917 error = in_pcbconnect(inp, nam, td);
924 * Make sure that the new target CPU is same as current CPU,
925 * if it is not, then we will have to free the route entry
926 * allocated on the current CPU.
928 if (udp_addrcpu(inp->inp_faddr.s_addr, inp->inp_fport,
929 inp->inp_laddr.s_addr, inp->inp_lport) != mycpuid) {
930 struct route *ro = &inp->inp_route;
932 if (ro->ro_rt != NULL)
934 bzero(ro, sizeof(*ro));
936 } else if (error == EAFNOSUPPORT) { /* connection dissolved */
938 * Follow traditional BSD behavior and retain
939 * the local port binding. But, fix the old misbehavior
940 * of overwriting any previously bound local address.
942 if (!(inp->inp_flags & INP_WASBOUND_NOTANY))
943 inp->inp_laddr.s_addr = INADDR_ANY;
944 in_pcbinswildcardhash(inp);
950 udp_detach(struct socket *so)
964 udp_disconnect(struct socket *so)
972 if (inp->inp_faddr.s_addr == INADDR_ANY)
976 in_pcbdisconnect(inp);
978 so->so_state &= ~SS_ISCONNECTED; /* XXX */
980 ro = &inp->inp_route;
981 if (ro->ro_rt != NULL)
983 bzero(ro, sizeof(*ro));
989 udp_send(struct socket *so, int flags, struct mbuf *m, struct sockaddr *addr,
990 struct mbuf *control, struct thread *td)
999 return udp_output(inp, m, addr, control, td);
1003 udp_shutdown(struct socket *so)
1014 struct pr_usrreqs udp_usrreqs = {
1015 .pru_abort = udp_abort,
1016 .pru_accept = pru_accept_notsupp,
1017 .pru_attach = udp_attach,
1018 .pru_bind = udp_bind,
1019 .pru_connect = udp_connect,
1020 .pru_connect2 = pru_connect2_notsupp,
1021 .pru_control = in_control,
1022 .pru_detach = udp_detach,
1023 .pru_disconnect = udp_disconnect,
1024 .pru_listen = pru_listen_notsupp,
1025 .pru_peeraddr = in_setpeeraddr,
1026 .pru_rcvd = pru_rcvd_notsupp,
1027 .pru_rcvoob = pru_rcvoob_notsupp,
1028 .pru_send = udp_send,
1029 .pru_sense = pru_sense_null,
1030 .pru_shutdown = udp_shutdown,
1031 .pru_sockaddr = in_setsockaddr,
1032 .pru_sosend = sosendudp,
1033 .pru_soreceive = soreceive,
1034 .pru_sopoll = sopoll