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) 2004 Jeffrey M. Hsu. All rights reserved.
37 * License terms: all terms for the DragonFly license above plus the following:
39 * 4. All advertising materials mentioning features or use of this software
40 * must display the following acknowledgement:
42 * This product includes software developed by Jeffrey M. Hsu
43 * for the DragonFly Project.
45 * This requirement may be waived with permission from Jeffrey Hsu.
46 * This requirement will sunset and may be removed on July 8 2005,
47 * after which the standard DragonFly license (as shown above) will
52 * Copyright (c) 1982, 1986, 1988, 1990, 1993, 1995
53 * The Regents of the University of California. All rights reserved.
55 * Redistribution and use in source and binary forms, with or without
56 * modification, are permitted provided that the following conditions
58 * 1. Redistributions of source code must retain the above copyright
59 * notice, this list of conditions and the following disclaimer.
60 * 2. Redistributions in binary form must reproduce the above copyright
61 * notice, this list of conditions and the following disclaimer in the
62 * documentation and/or other materials provided with the distribution.
63 * 3. All advertising materials mentioning features or use of this software
64 * must display the following acknowledgement:
65 * This product includes software developed by the University of
66 * California, Berkeley and its contributors.
67 * 4. Neither the name of the University nor the names of its contributors
68 * may be used to endorse or promote products derived from this software
69 * without specific prior written permission.
71 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
72 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
73 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
74 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
75 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
76 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
77 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
78 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
79 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
80 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
83 * @(#)udp_usrreq.c 8.6 (Berkeley) 5/23/95
84 * $FreeBSD: src/sys/netinet/udp_usrreq.c,v 1.64.2.18 2003/01/24 05:11:34 sam Exp $
85 * $DragonFly: src/sys/netinet/udp_usrreq.c,v 1.28 2004/12/03 20:29:53 joerg Exp $
88 #include "opt_ipsec.h"
89 #include "opt_inet6.h"
91 #include <sys/param.h>
92 #include <sys/systm.h>
93 #include <sys/kernel.h>
94 #include <sys/malloc.h>
96 #include <sys/domain.h>
98 #include <sys/protosw.h>
99 #include <sys/socket.h>
100 #include <sys/socketvar.h>
101 #include <sys/sysctl.h>
102 #include <sys/syslog.h>
103 #include <sys/in_cksum.h>
105 #include <machine/stdarg.h>
107 #include <vm/vm_zone.h>
110 #include <net/route.h>
112 #include <netinet/in.h>
113 #include <netinet/in_systm.h>
114 #include <netinet/ip.h>
116 #include <netinet/ip6.h>
118 #include <netinet/in_pcb.h>
119 #include <netinet/in_var.h>
120 #include <netinet/ip_var.h>
122 #include <netinet6/ip6_var.h>
124 #include <netinet/ip_icmp.h>
125 #include <netinet/icmp_var.h>
126 #include <netinet/udp.h>
127 #include <netinet/udp_var.h>
130 #include <netproto/ipsec/ipsec.h>
131 #endif /*FAST_IPSEC*/
134 #include <netinet6/ipsec.h>
138 * UDP protocol implementation.
139 * Per RFC 768, August, 1980.
142 static int udpcksum = 1;
144 static int udpcksum = 0; /* XXX */
146 SYSCTL_INT(_net_inet_udp, UDPCTL_CHECKSUM, checksum, CTLFLAG_RW,
150 SYSCTL_INT(_net_inet_udp, OID_AUTO, log_in_vain, CTLFLAG_RW,
151 &log_in_vain, 0, "Log all incoming UDP packets");
153 static int blackhole = 0;
154 SYSCTL_INT(_net_inet_udp, OID_AUTO, blackhole, CTLFLAG_RW,
155 &blackhole, 0, "Do not send port unreachables for refused connects");
157 static int strict_mcast_mship = 1;
158 SYSCTL_INT(_net_inet_udp, OID_AUTO, strict_mcast_mship, CTLFLAG_RW,
159 &strict_mcast_mship, 0, "Only send multicast to member sockets");
161 struct inpcbinfo udbinfo;
164 #define UDBHASHSIZE 16
167 struct udpstat udpstat; /* from udp_var.h */
168 SYSCTL_STRUCT(_net_inet_udp, UDPCTL_STATS, stats, CTLFLAG_RW,
169 &udpstat, udpstat, "UDP statistics (struct udpstat, netinet/udp_var.h)");
171 static struct sockaddr_in udp_in = { sizeof(udp_in), AF_INET };
174 struct sockaddr_in6 uin6_sin;
175 u_char uin6_init_done : 1;
177 { sizeof(udp_in6.uin6_sin), AF_INET6 },
181 struct ip6_hdr uip6_ip6;
182 u_char uip6_init_done : 1;
186 static void udp_append (struct inpcb *last, struct ip *ip,
187 struct mbuf *n, int off);
189 static void ip_2_ip6_hdr (struct ip6_hdr *ip6, struct ip *ip);
192 static int udp_detach (struct socket *so);
193 static int udp_output (struct inpcb *, struct mbuf *, struct sockaddr *,
194 struct mbuf *, struct thread *);
199 in_pcbinfo_init(&udbinfo);
200 udbinfo.hashbase = hashinit(UDBHASHSIZE, M_PCB, &udbinfo.hashmask);
201 udbinfo.porthashbase = hashinit(UDBHASHSIZE, M_PCB,
202 &udbinfo.porthashmask);
203 udbinfo.wildcardhashbase = hashinit(UDBHASHSIZE, M_PCB,
204 &udbinfo.wildcardhashmask);
205 udbinfo.ipi_zone = zinit("udpcb", sizeof(struct inpcb), maxsockets,
211 * Check multicast packets to make sure they are only sent to sockets with
212 * multicast memberships for the packet's destination address and arrival
213 * interface. Multicast packets to multicast-unaware sockets are also
216 * Returns 0 if the packet is acceptable, -1 if it is not.
220 check_multicast_membership(struct ip *ip, struct inpcb *inp, struct mbuf *m)
223 struct ip_moptions *mopt;
225 if (strict_mcast_mship == 0 ||
226 !IN_MULTICAST(ntohl(ip->ip_dst.s_addr))) {
229 mopt = inp->inp_moptions;
232 for (mshipno = 0; mshipno <= mopt->imo_num_memberships; ++mshipno) {
233 if (ip->ip_dst.s_addr == mopt->imo_membership[mshipno]->inm_addr.s_addr &&
234 m->m_pkthdr.rcvif == mopt->imo_membership[mshipno]->inm_ifp) {
242 udp_input(struct mbuf *m, ...)
248 struct mbuf *opts = 0;
251 struct sockaddr *append_sa;
255 off = __va_arg(ap, int);
256 proto = __va_arg(ap, int);
260 udpstat.udps_ipackets++;
263 * Strip IP options, if any; should skip this,
264 * make available to user, and use on returned packets,
265 * but we don't yet have a way to check the checksum
266 * with options still present.
268 if (iphlen > sizeof (struct ip)) {
270 iphlen = sizeof(struct ip);
274 * IP and UDP headers are together in first mbuf.
275 * Already checked and pulled up in ip_demux().
277 KASSERT(m->m_len >= iphlen + sizeof(struct udphdr),
278 ("UDP header not in one mbuf"));
280 ip = mtod(m, struct ip *);
281 uh = (struct udphdr *)((caddr_t)ip + iphlen);
283 /* destination port of 0 is illegal, based on RFC768. */
284 if (uh->uh_dport == 0)
288 * Make mbuf data length reflect UDP length.
289 * If not enough data to reflect UDP length, drop.
291 len = ntohs((u_short)uh->uh_ulen);
292 if (ip->ip_len != len) {
293 if (len > ip->ip_len || len < sizeof(struct udphdr)) {
294 udpstat.udps_badlen++;
297 m_adj(m, len - ip->ip_len);
298 /* ip->ip_len = len; */
301 * Save a copy of the IP header in case we want restore it
302 * for sending an ICMP error message in response.
307 * Checksum extended UDP header and data.
310 if (m->m_pkthdr.csum_flags & CSUM_DATA_VALID) {
311 if (m->m_pkthdr.csum_flags & CSUM_PSEUDO_HDR)
312 uh->uh_sum = m->m_pkthdr.csum_data;
314 uh->uh_sum = in_pseudo(ip->ip_src.s_addr,
315 ip->ip_dst.s_addr, htonl((u_short)len +
316 m->m_pkthdr.csum_data + IPPROTO_UDP));
317 uh->uh_sum ^= 0xffff;
320 bcopy(((struct ipovly *)ip)->ih_x1, b, 9);
321 bzero(((struct ipovly *)ip)->ih_x1, 9);
322 ((struct ipovly *)ip)->ih_len = uh->uh_ulen;
323 uh->uh_sum = in_cksum(m, len + sizeof (struct ip));
324 bcopy(b, ((struct ipovly *)ip)->ih_x1, 9);
327 udpstat.udps_badsum++;
332 udpstat.udps_nosum++;
334 if (IN_MULTICAST(ntohl(ip->ip_dst.s_addr)) ||
335 in_broadcast(ip->ip_dst, m->m_pkthdr.rcvif)) {
338 * Deliver a multicast or broadcast datagram to *all* sockets
339 * for which the local and remote addresses and ports match
340 * those of the incoming datagram. This allows more than
341 * one process to receive multi/broadcasts on the same port.
342 * (This really ought to be done for unicast datagrams as
343 * well, but that would cause problems with existing
344 * applications that open both address-specific sockets and
345 * a wildcard socket listening to the same port -- they would
346 * end up receiving duplicates of every unicast datagram.
347 * Those applications open the multiple sockets to overcome an
348 * inadequacy of the UDP socket interface, but for backwards
349 * compatibility we avoid the problem here rather than
350 * fixing the interface. Maybe 4.5BSD will remedy this?)
354 * Construct sockaddr format source address.
356 udp_in.sin_port = uh->uh_sport;
357 udp_in.sin_addr = ip->ip_src;
359 * Locate pcb(s) for datagram.
360 * (Algorithm copied from raw_intr().)
364 udp_in6.uin6_init_done = udp_ip6.uip6_init_done = 0;
366 LIST_FOREACH(inp, &udbinfo.pcblisthead, inp_list) {
367 if (inp->inp_flags & INP_PLACEMARKER)
370 if ((inp->inp_vflag & INP_IPV4) == 0)
373 if (inp->inp_lport != uh->uh_dport)
375 if (inp->inp_laddr.s_addr != INADDR_ANY) {
376 if (inp->inp_laddr.s_addr !=
380 if (inp->inp_faddr.s_addr != INADDR_ANY) {
381 if (inp->inp_faddr.s_addr !=
383 inp->inp_fport != uh->uh_sport)
387 if (check_multicast_membership(ip, inp, m) < 0)
394 /* check AH/ESP integrity. */
395 if (ipsec4_in_reject_so(m, last->inp_socket))
396 ipsecstat.in_polvio++;
397 /* do not inject data to pcb */
401 /* check AH/ESP integrity. */
402 if (ipsec4_in_reject(m, last))
405 #endif /*FAST_IPSEC*/
406 if ((n = m_copy(m, 0, M_COPYALL)) != NULL)
407 udp_append(last, ip, n,
409 sizeof(struct udphdr));
413 * Don't look for additional matches if this one does
414 * not have either the SO_REUSEPORT or SO_REUSEADDR
415 * socket options set. This heuristic avoids searching
416 * through all pcbs in the common case of a non-shared
417 * port. It * assumes that an application will never
418 * clear these options after setting them.
420 if ((last->inp_socket->so_options&(SO_REUSEPORT|SO_REUSEADDR)) == 0)
426 * No matching pcb found; discard datagram.
427 * (No need to send an ICMP Port Unreachable
428 * for a broadcast or multicast datgram.)
430 udpstat.udps_noportbcast++;
434 /* check AH/ESP integrity. */
435 if (ipsec4_in_reject_so(m, last->inp_socket)) {
436 ipsecstat.in_polvio++;
441 /* check AH/ESP integrity. */
442 if (ipsec4_in_reject(m, last))
444 #endif /*FAST_IPSEC*/
445 udp_append(last, ip, m, iphlen + sizeof(struct udphdr));
449 * Locate pcb for datagram.
451 inp = in_pcblookup_hash(&udbinfo, ip->ip_src, uh->uh_sport,
452 ip->ip_dst, uh->uh_dport, 1, m->m_pkthdr.rcvif);
455 char buf[4*sizeof "123"];
457 strcpy(buf, inet_ntoa(ip->ip_dst));
459 "Connection attempt to UDP %s:%d from %s:%d\n",
460 buf, ntohs(uh->uh_dport), inet_ntoa(ip->ip_src),
461 ntohs(uh->uh_sport));
463 udpstat.udps_noport++;
464 if (m->m_flags & (M_BCAST | M_MCAST)) {
465 udpstat.udps_noportbcast++;
471 if (badport_bandlim(BANDLIM_ICMP_UNREACH) < 0)
475 ip->ip_len += iphlen;
476 icmp_error(m, ICMP_UNREACH, ICMP_UNREACH_PORT, 0, 0);
480 if (ipsec4_in_reject_so(m, inp->inp_socket)) {
481 ipsecstat.in_polvio++;
486 if (ipsec4_in_reject(m, inp))
488 #endif /*FAST_IPSEC*/
491 * Construct sockaddr format source address.
492 * Stuff source address and datagram in user buffer.
494 udp_in.sin_port = uh->uh_sport;
495 udp_in.sin_addr = ip->ip_src;
496 if (inp->inp_flags & INP_CONTROLOPTS
497 || inp->inp_socket->so_options & SO_TIMESTAMP) {
499 if (inp->inp_vflag & INP_IPV6) {
502 ip_2_ip6_hdr(&udp_ip6.uip6_ip6, ip);
503 savedflags = inp->inp_flags;
504 inp->inp_flags &= ~INP_UNMAPPABLEOPTS;
505 ip6_savecontrol(inp, &opts, &udp_ip6.uip6_ip6, m);
506 inp->inp_flags = savedflags;
509 ip_savecontrol(inp, &opts, ip, m);
511 m_adj(m, iphlen + sizeof(struct udphdr));
513 if (inp->inp_vflag & INP_IPV6) {
514 in6_sin_2_v4mapsin6(&udp_in, &udp_in6.uin6_sin);
515 append_sa = (struct sockaddr *)&udp_in6;
518 append_sa = (struct sockaddr *)&udp_in;
519 if (sbappendaddr(&inp->inp_socket->so_rcv, append_sa, m, opts) == 0) {
520 udpstat.udps_fullsock++;
523 sorwakeup(inp->inp_socket);
534 ip_2_ip6_hdr(ip6, 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(last, ip, n, off)
562 struct sockaddr *append_sa;
563 struct mbuf *opts = 0;
565 if (last->inp_flags & INP_CONTROLOPTS ||
566 last->inp_socket->so_options & SO_TIMESTAMP) {
568 if (last->inp_vflag & INP_IPV6) {
571 if (udp_ip6.uip6_init_done == 0) {
572 ip_2_ip6_hdr(&udp_ip6.uip6_ip6, ip);
573 udp_ip6.uip6_init_done = 1;
575 savedflags = last->inp_flags;
576 last->inp_flags &= ~INP_UNMAPPABLEOPTS;
577 ip6_savecontrol(last, &opts, &udp_ip6.uip6_ip6, n);
578 last->inp_flags = savedflags;
581 ip_savecontrol(last, &opts, ip, n);
584 if (last->inp_vflag & INP_IPV6) {
585 if (udp_in6.uin6_init_done == 0) {
586 in6_sin_2_v4mapsin6(&udp_in, &udp_in6.uin6_sin);
587 udp_in6.uin6_init_done = 1;
589 append_sa = (struct sockaddr *)&udp_in6.uin6_sin;
592 append_sa = (struct sockaddr *)&udp_in;
594 if (sbappendaddr(&last->inp_socket->so_rcv, append_sa, n, opts) == 0) {
598 udpstat.udps_fullsock++;
600 sorwakeup(last->inp_socket);
604 * Notify a udp user of an asynchronous error;
605 * just wake up so that he can collect error status.
608 udp_notify(inp, errno)
612 inp->inp_socket->so_error = errno;
613 sorwakeup(inp->inp_socket);
614 sowwakeup(inp->inp_socket);
618 udp_ctlinput(cmd, sa, vip)
625 void (*notify) (struct inpcb *, int) = udp_notify;
626 struct in_addr faddr;
630 faddr = ((struct sockaddr_in *)sa)->sin_addr;
631 if (sa->sa_family != AF_INET || faddr.s_addr == INADDR_ANY)
634 if (PRC_IS_REDIRECT(cmd)) {
636 notify = in_rtchange;
637 } else if (cmd == PRC_HOSTDEAD)
639 else if ((unsigned)cmd >= PRC_NCMDS || inetctlerrmap[cmd] == 0)
643 uh = (struct udphdr *)((caddr_t)ip + (ip->ip_hl << 2));
644 inp = in_pcblookup_hash(&udbinfo, faddr, uh->uh_dport,
645 ip->ip_src, uh->uh_sport, 0, NULL);
646 if (inp != NULL && inp->inp_socket != NULL)
647 (*notify)(inp, inetctlerrmap[cmd]);
650 in_pcbnotifyall(&udbinfo.pcblisthead, faddr, inetctlerrmap[cmd],
654 SYSCTL_PROC(_net_inet_udp, UDPCTL_PCBLIST, pcblist, CTLFLAG_RD, &udbinfo, 0,
655 in_pcblist_global, "S,xinpcb", "List of active UDP sockets");
658 udp_getcred(SYSCTL_HANDLER_ARGS)
660 struct sockaddr_in addrs[2];
664 error = suser(req->td);
667 error = SYSCTL_IN(req, addrs, sizeof(addrs));
671 inp = in_pcblookup_hash(&udbinfo, addrs[1].sin_addr, addrs[1].sin_port,
672 addrs[0].sin_addr, addrs[0].sin_port, 1, NULL);
673 if (inp == NULL || inp->inp_socket == NULL) {
677 error = SYSCTL_OUT(req, inp->inp_socket->so_cred, sizeof(struct ucred));
683 SYSCTL_PROC(_net_inet_udp, OID_AUTO, getcred, CTLTYPE_OPAQUE|CTLFLAG_RW,
684 0, 0, udp_getcred, "S,ucred", "Get the ucred of a UDP connection");
687 udp_output(inp, m, dstaddr, control, td)
690 struct sockaddr *dstaddr;
691 struct mbuf *control;
695 int len = m->m_pkthdr.len;
696 struct sockaddr_in *sin; /* really is initialized before use */
699 if (len + sizeof(struct udpiphdr) > IP_MAXPACKET) {
704 if (inp->inp_lport == 0) { /* unbound socket */
705 error = in_pcbbind(inp, (struct sockaddr *)NULL, td);
708 in_pcbinswildcardhash(inp);
711 if (dstaddr != NULL) { /* destination address specified */
712 if (inp->inp_faddr.s_addr != INADDR_ANY) {
713 /* already connected */
717 sin = (struct sockaddr_in *)dstaddr;
718 prison_remote_ip(td, 0, &sin->sin_addr.s_addr);
720 if (inp->inp_faddr.s_addr == INADDR_ANY) {
721 /* no destination specified and not already connected */
729 * Calculate data length and get a mbuf
730 * for UDP and IP headers.
732 M_PREPEND(m, sizeof(struct udpiphdr), MB_DONTWAIT);
739 * Fill in mbuf with extended UDP header
740 * and addresses and length put into network format.
742 ui = mtod(m, struct udpiphdr *);
743 bzero(ui->ui_x1, sizeof(ui->ui_x1)); /* XXX still needed? */
744 ui->ui_pr = IPPROTO_UDP;
747 * Set destination address.
749 if (dstaddr != NULL) { /* use specified destination */
750 ui->ui_dst = sin->sin_addr;
751 ui->ui_dport = sin->sin_port;
752 } else { /* use connected destination */
753 ui->ui_dst = inp->inp_faddr;
754 ui->ui_dport = inp->inp_fport;
758 * Set source address.
760 if (inp->inp_laddr.s_addr == INADDR_ANY) {
761 struct sockaddr_in *if_sin;
763 KASSERT(dstaddr != NULL,
764 ("connected UDP socket without local addr: "
765 "lport %d, faddr %x, fport %d",
766 inp->inp_lport, inp->inp_faddr.s_addr, inp->inp_fport));
768 /* Look up outgoing interface. */
769 if ((error = in_pcbladdr(inp, dstaddr, &if_sin)))
771 ui->ui_src = if_sin->sin_addr; /* use address of interface */
773 ui->ui_src = inp->inp_laddr; /* use non-null bound address */
775 ui->ui_sport = inp->inp_lport;
776 KASSERT(inp->inp_lport != 0, ("inp lport should have been bound"));
778 ui->ui_ulen = htons((u_short)len + sizeof(struct udphdr));
781 * Set up checksum and output datagram.
784 ui->ui_sum = in_pseudo(ui->ui_src.s_addr, ui->ui_dst.s_addr,
785 htons((u_short)len + sizeof(struct udphdr) + IPPROTO_UDP));
786 m->m_pkthdr.csum_flags = CSUM_UDP;
787 m->m_pkthdr.csum_data = offsetof(struct udphdr, uh_sum);
791 ((struct ip *)ui)->ip_len = sizeof (struct udpiphdr) + len;
792 ((struct ip *)ui)->ip_ttl = inp->inp_ip_ttl; /* XXX */
793 ((struct ip *)ui)->ip_tos = inp->inp_ip_tos; /* XXX */
794 udpstat.udps_opackets++;
796 error = ip_output(m, inp->inp_options, &inp->inp_route,
797 (inp->inp_socket->so_options & (SO_DONTROUTE | SO_BROADCAST)),
798 inp->inp_moptions, inp);
807 u_long udp_sendspace = 9216; /* really max datagram size */
808 /* 40 1K datagrams */
809 SYSCTL_INT(_net_inet_udp, UDPCTL_MAXDGRAM, maxdgram, CTLFLAG_RW,
810 &udp_sendspace, 0, "Maximum outgoing UDP datagram size");
812 u_long udp_recvspace = 40 * (1024 +
814 sizeof(struct sockaddr_in6)
816 sizeof(struct sockaddr_in)
819 SYSCTL_INT(_net_inet_udp, UDPCTL_RECVSPACE, recvspace, CTLFLAG_RW,
820 &udp_recvspace, 0, "Maximum incoming UDP datagram size");
823 udp_abort(struct socket *so)
830 return EINVAL; /* ??? possible? panic instead? */
831 soisdisconnected(so);
839 udp_attach(struct socket *so, int proto, struct pru_attach_info *ai)
848 error = soreserve(so, udp_sendspace, udp_recvspace, ai->sb_rlimit);
852 error = in_pcballoc(so, &udbinfo);
857 inp = (struct inpcb *)so->so_pcb;
858 inp->inp_vflag |= INP_IPV4;
859 inp->inp_ip_ttl = ip_defttl;
864 udp_bind(struct socket *so, struct sockaddr *nam, struct thread *td)
866 struct sockaddr_in *sin = (struct sockaddr_in *)nam;
874 error = in_pcbbind(inp, nam, td);
877 if (sin->sin_addr.s_addr != INADDR_ANY)
878 inp->inp_flags |= INP_WASBOUND_NOTANY;
879 in_pcbinswildcardhash(inp);
885 udp_connect(struct socket *so, struct sockaddr *nam, struct thread *td)
889 struct sockaddr_in *sin;
894 if (inp->inp_faddr.s_addr != INADDR_ANY)
898 if (td->td_proc && td->td_proc->p_ucred->cr_prison != NULL &&
899 inp->inp_laddr.s_addr == INADDR_ANY) {
900 error = in_pcbbind(inp, NULL, td);
903 sin = (struct sockaddr_in *)nam;
904 prison_remote_ip(td, 0, &sin->sin_addr.s_addr);
905 if (inp->inp_flags & INP_WILDCARD)
906 in_pcbremwildcardhash(inp);
907 error = in_pcbconnect(inp, nam, td);
912 else if (error == EAFNOSUPPORT) { /* connection dissolved */
914 * Follow traditional BSD behavior and retain
915 * the local port binding. But, fix the old misbehavior
916 * of overwriting any previously bound local address.
918 if (!(inp->inp_flags & INP_WASBOUND_NOTANY))
919 inp->inp_laddr.s_addr = INADDR_ANY;
920 in_pcbinswildcardhash(inp);
926 udp_detach(struct socket *so)
941 udp_disconnect(struct socket *so)
949 if (inp->inp_faddr.s_addr == INADDR_ANY)
953 in_pcbdisconnect(inp);
955 so->so_state &= ~SS_ISCONNECTED; /* XXX */
960 udp_send(struct socket *so, int flags, struct mbuf *m, struct sockaddr *addr,
961 struct mbuf *control, struct thread *td)
970 return udp_output(inp, m, addr, control, td);
974 udp_shutdown(struct socket *so)
985 struct pr_usrreqs udp_usrreqs = {
986 udp_abort, pru_accept_notsupp, udp_attach, udp_bind, udp_connect,
987 pru_connect2_notsupp, in_control, udp_detach, udp_disconnect,
988 pru_listen_notsupp, in_setpeeraddr, pru_rcvd_notsupp,
989 pru_rcvoob_notsupp, udp_send, pru_sense_null, udp_shutdown,
990 in_setsockaddr, sosendudp, soreceive, sopoll