2 * Copyright (c) 2004 Jeffrey Hsu. All rights reserved.
3 * Copyright (c) 1982, 1986, 1988, 1990, 1993, 1995
4 * The Regents of the University of California. All rights reserved.
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 * 3. All advertising materials mentioning features or use of this software
15 * must display the following acknowledgement:
16 * This product includes software developed by the University of
17 * California, Berkeley and its contributors.
18 * 4. Neither the name of the University nor the names of its contributors
19 * may be used to endorse or promote products derived from this software
20 * without specific prior written permission.
22 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34 * @(#)udp_usrreq.c 8.6 (Berkeley) 5/23/95
35 * $FreeBSD: src/sys/netinet/udp_usrreq.c,v 1.64.2.18 2003/01/24 05:11:34 sam Exp $
36 * $DragonFly: src/sys/netinet/udp_usrreq.c,v 1.25 2004/06/07 02:36:22 dillon Exp $
39 #include "opt_ipsec.h"
40 #include "opt_inet6.h"
42 #include <sys/param.h>
43 #include <sys/systm.h>
44 #include <sys/kernel.h>
45 #include <sys/malloc.h>
47 #include <sys/domain.h>
49 #include <sys/protosw.h>
50 #include <sys/socket.h>
51 #include <sys/socketvar.h>
52 #include <sys/sysctl.h>
53 #include <sys/syslog.h>
54 #include <sys/in_cksum.h>
56 #include <machine/stdarg.h>
58 #include <vm/vm_zone.h>
61 #include <net/route.h>
63 #include <netinet/in.h>
64 #include <netinet/in_systm.h>
65 #include <netinet/ip.h>
67 #include <netinet/ip6.h>
69 #include <netinet/in_pcb.h>
70 #include <netinet/in_var.h>
71 #include <netinet/ip_var.h>
73 #include <netinet6/ip6_var.h>
75 #include <netinet/ip_icmp.h>
76 #include <netinet/icmp_var.h>
77 #include <netinet/udp.h>
78 #include <netinet/udp_var.h>
81 #include <netipsec/ipsec.h>
85 #include <netinet6/ipsec.h>
89 * UDP protocol implementation.
90 * Per RFC 768, August, 1980.
93 static int udpcksum = 1;
95 static int udpcksum = 0; /* XXX */
97 SYSCTL_INT(_net_inet_udp, UDPCTL_CHECKSUM, checksum, CTLFLAG_RW,
101 SYSCTL_INT(_net_inet_udp, OID_AUTO, log_in_vain, CTLFLAG_RW,
102 &log_in_vain, 0, "Log all incoming UDP packets");
104 static int blackhole = 0;
105 SYSCTL_INT(_net_inet_udp, OID_AUTO, blackhole, CTLFLAG_RW,
106 &blackhole, 0, "Do not send port unreachables for refused connects");
108 static int strict_mcast_mship = 1;
109 SYSCTL_INT(_net_inet_udp, OID_AUTO, strict_mcast_mship, CTLFLAG_RW,
110 &strict_mcast_mship, 0, "Only send multicast to member sockets");
112 struct inpcbinfo udbinfo;
115 #define UDBHASHSIZE 16
118 struct udpstat udpstat; /* from udp_var.h */
119 SYSCTL_STRUCT(_net_inet_udp, UDPCTL_STATS, stats, CTLFLAG_RW,
120 &udpstat, udpstat, "UDP statistics (struct udpstat, netinet/udp_var.h)");
122 static struct sockaddr_in udp_in = { sizeof(udp_in), AF_INET };
125 struct sockaddr_in6 uin6_sin;
126 u_char uin6_init_done : 1;
128 { sizeof(udp_in6.uin6_sin), AF_INET6 },
132 struct ip6_hdr uip6_ip6;
133 u_char uip6_init_done : 1;
137 static void udp_append (struct inpcb *last, struct ip *ip,
138 struct mbuf *n, int off);
140 static void ip_2_ip6_hdr (struct ip6_hdr *ip6, struct ip *ip);
143 static int udp_detach (struct socket *so);
144 static int udp_output (struct inpcb *, struct mbuf *, struct sockaddr *,
145 struct mbuf *, struct thread *);
150 in_pcbinfo_init(&udbinfo);
151 udbinfo.hashbase = hashinit(UDBHASHSIZE, M_PCB, &udbinfo.hashmask);
152 udbinfo.porthashbase = hashinit(UDBHASHSIZE, M_PCB,
153 &udbinfo.porthashmask);
154 udbinfo.wildcardhashbase = hashinit(UDBHASHSIZE, M_PCB,
155 &udbinfo.wildcardhashmask);
156 udbinfo.ipi_zone = zinit("udpcb", sizeof(struct inpcb), maxsockets,
162 * Check multicast packets to make sure they are only sent to sockets with
163 * multicast memberships for the packet's destination address and arrival
164 * interface. Multicast packets to multicast-unaware sockets are also
167 * Returns 0 if the packet is acceptable, -1 if it is not.
171 check_multicast_membership(struct ip *ip, struct inpcb *inp, struct mbuf *m)
174 struct ip_moptions *mopt;
176 if (strict_mcast_mship == 0 ||
177 !IN_MULTICAST(ntohl(ip->ip_dst.s_addr))) {
180 mopt = inp->inp_moptions;
183 for (mshipno = 0; mshipno <= mopt->imo_num_memberships; ++mshipno) {
184 if (ip->ip_dst.s_addr == mopt->imo_membership[mshipno]->inm_addr.s_addr &&
185 m->m_pkthdr.rcvif == mopt->imo_membership[mshipno]->inm_ifp) {
193 udp_input(struct mbuf *m, ...)
199 struct mbuf *opts = 0;
202 struct sockaddr *append_sa;
206 off = __va_arg(ap, int);
207 proto = __va_arg(ap, int);
211 udpstat.udps_ipackets++;
214 * Strip IP options, if any; should skip this,
215 * make available to user, and use on returned packets,
216 * but we don't yet have a way to check the checksum
217 * with options still present.
219 if (iphlen > sizeof (struct ip)) {
221 iphlen = sizeof(struct ip);
225 * IP and UDP headers are together in first mbuf.
226 * Already checked and pulled up in ip_demux().
228 KASSERT(m->m_len >= iphlen + sizeof(struct udphdr),
229 ("UDP header not in one mbuf"));
231 ip = mtod(m, struct ip *);
232 uh = (struct udphdr *)((caddr_t)ip + iphlen);
234 /* destination port of 0 is illegal, based on RFC768. */
235 if (uh->uh_dport == 0)
239 * Make mbuf data length reflect UDP length.
240 * If not enough data to reflect UDP length, drop.
242 len = ntohs((u_short)uh->uh_ulen);
243 if (ip->ip_len != len) {
244 if (len > ip->ip_len || len < sizeof(struct udphdr)) {
245 udpstat.udps_badlen++;
248 m_adj(m, len - ip->ip_len);
249 /* ip->ip_len = len; */
252 * Save a copy of the IP header in case we want restore it
253 * for sending an ICMP error message in response.
258 * Checksum extended UDP header and data.
261 if (m->m_pkthdr.csum_flags & CSUM_DATA_VALID) {
262 if (m->m_pkthdr.csum_flags & CSUM_PSEUDO_HDR)
263 uh->uh_sum = m->m_pkthdr.csum_data;
265 uh->uh_sum = in_pseudo(ip->ip_src.s_addr,
266 ip->ip_dst.s_addr, htonl((u_short)len +
267 m->m_pkthdr.csum_data + IPPROTO_UDP));
268 uh->uh_sum ^= 0xffff;
271 bcopy(((struct ipovly *)ip)->ih_x1, b, 9);
272 bzero(((struct ipovly *)ip)->ih_x1, 9);
273 ((struct ipovly *)ip)->ih_len = uh->uh_ulen;
274 uh->uh_sum = in_cksum(m, len + sizeof (struct ip));
275 bcopy(b, ((struct ipovly *)ip)->ih_x1, 9);
278 udpstat.udps_badsum++;
283 udpstat.udps_nosum++;
285 if (IN_MULTICAST(ntohl(ip->ip_dst.s_addr)) ||
286 in_broadcast(ip->ip_dst, m->m_pkthdr.rcvif)) {
289 * Deliver a multicast or broadcast datagram to *all* sockets
290 * for which the local and remote addresses and ports match
291 * those of the incoming datagram. This allows more than
292 * one process to receive multi/broadcasts on the same port.
293 * (This really ought to be done for unicast datagrams as
294 * well, but that would cause problems with existing
295 * applications that open both address-specific sockets and
296 * a wildcard socket listening to the same port -- they would
297 * end up receiving duplicates of every unicast datagram.
298 * Those applications open the multiple sockets to overcome an
299 * inadequacy of the UDP socket interface, but for backwards
300 * compatibility we avoid the problem here rather than
301 * fixing the interface. Maybe 4.5BSD will remedy this?)
305 * Construct sockaddr format source address.
307 udp_in.sin_port = uh->uh_sport;
308 udp_in.sin_addr = ip->ip_src;
310 * Locate pcb(s) for datagram.
311 * (Algorithm copied from raw_intr().)
315 udp_in6.uin6_init_done = udp_ip6.uip6_init_done = 0;
317 LIST_FOREACH(inp, &udbinfo.pcblisthead, inp_list) {
318 if (inp->inp_flags & INP_PLACEMARKER)
321 if ((inp->inp_vflag & INP_IPV4) == 0)
324 if (inp->inp_lport != uh->uh_dport)
326 if (inp->inp_laddr.s_addr != INADDR_ANY) {
327 if (inp->inp_laddr.s_addr !=
331 if (inp->inp_faddr.s_addr != INADDR_ANY) {
332 if (inp->inp_faddr.s_addr !=
334 inp->inp_fport != uh->uh_sport)
338 if (check_multicast_membership(ip, inp, m) < 0)
345 /* check AH/ESP integrity. */
346 if (ipsec4_in_reject_so(m, last->inp_socket))
347 ipsecstat.in_polvio++;
348 /* do not inject data to pcb */
352 /* check AH/ESP integrity. */
353 if (ipsec4_in_reject(m, last))
356 #endif /*FAST_IPSEC*/
357 if ((n = m_copy(m, 0, M_COPYALL)) != NULL)
358 udp_append(last, ip, n,
360 sizeof(struct udphdr));
364 * Don't look for additional matches if this one does
365 * not have either the SO_REUSEPORT or SO_REUSEADDR
366 * socket options set. This heuristic avoids searching
367 * through all pcbs in the common case of a non-shared
368 * port. It * assumes that an application will never
369 * clear these options after setting them.
371 if ((last->inp_socket->so_options&(SO_REUSEPORT|SO_REUSEADDR)) == 0)
377 * No matching pcb found; discard datagram.
378 * (No need to send an ICMP Port Unreachable
379 * for a broadcast or multicast datgram.)
381 udpstat.udps_noportbcast++;
385 /* check AH/ESP integrity. */
386 if (ipsec4_in_reject_so(m, last->inp_socket)) {
387 ipsecstat.in_polvio++;
392 /* check AH/ESP integrity. */
393 if (ipsec4_in_reject(m, last))
395 #endif /*FAST_IPSEC*/
396 udp_append(last, ip, m, iphlen + sizeof(struct udphdr));
400 * Locate pcb for datagram.
402 inp = in_pcblookup_hash(&udbinfo, ip->ip_src, uh->uh_sport,
403 ip->ip_dst, uh->uh_dport, 1, m->m_pkthdr.rcvif);
406 char buf[4*sizeof "123"];
408 strcpy(buf, inet_ntoa(ip->ip_dst));
410 "Connection attempt to UDP %s:%d from %s:%d\n",
411 buf, ntohs(uh->uh_dport), inet_ntoa(ip->ip_src),
412 ntohs(uh->uh_sport));
414 udpstat.udps_noport++;
415 if (m->m_flags & (M_BCAST | M_MCAST)) {
416 udpstat.udps_noportbcast++;
422 if (badport_bandlim(BANDLIM_ICMP_UNREACH) < 0)
426 ip->ip_len += iphlen;
427 icmp_error(m, ICMP_UNREACH, ICMP_UNREACH_PORT, 0, 0);
431 if (ipsec4_in_reject_so(m, inp->inp_socket)) {
432 ipsecstat.in_polvio++;
437 if (ipsec4_in_reject(m, inp))
439 #endif /*FAST_IPSEC*/
442 * Construct sockaddr format source address.
443 * Stuff source address and datagram in user buffer.
445 udp_in.sin_port = uh->uh_sport;
446 udp_in.sin_addr = ip->ip_src;
447 if (inp->inp_flags & INP_CONTROLOPTS
448 || inp->inp_socket->so_options & SO_TIMESTAMP) {
450 if (inp->inp_vflag & INP_IPV6) {
453 ip_2_ip6_hdr(&udp_ip6.uip6_ip6, ip);
454 savedflags = inp->inp_flags;
455 inp->inp_flags &= ~INP_UNMAPPABLEOPTS;
456 ip6_savecontrol(inp, &opts, &udp_ip6.uip6_ip6, m);
457 inp->inp_flags = savedflags;
460 ip_savecontrol(inp, &opts, ip, m);
462 m_adj(m, iphlen + sizeof(struct udphdr));
464 if (inp->inp_vflag & INP_IPV6) {
465 in6_sin_2_v4mapsin6(&udp_in, &udp_in6.uin6_sin);
466 append_sa = (struct sockaddr *)&udp_in6;
469 append_sa = (struct sockaddr *)&udp_in;
470 if (sbappendaddr(&inp->inp_socket->so_rcv, append_sa, m, opts) == 0) {
471 udpstat.udps_fullsock++;
474 sorwakeup(inp->inp_socket);
485 ip_2_ip6_hdr(ip6, ip)
489 bzero(ip6, sizeof(*ip6));
491 ip6->ip6_vfc = IPV6_VERSION;
492 ip6->ip6_plen = ip->ip_len;
493 ip6->ip6_nxt = ip->ip_p;
494 ip6->ip6_hlim = ip->ip_ttl;
495 ip6->ip6_src.s6_addr32[2] = ip6->ip6_dst.s6_addr32[2] =
497 ip6->ip6_src.s6_addr32[3] = ip->ip_src.s_addr;
498 ip6->ip6_dst.s6_addr32[3] = ip->ip_dst.s_addr;
503 * subroutine of udp_input(), mainly for source code readability.
504 * caller must properly init udp_ip6 and udp_in6 beforehand.
507 udp_append(last, ip, n, off)
513 struct sockaddr *append_sa;
514 struct mbuf *opts = 0;
516 if (last->inp_flags & INP_CONTROLOPTS ||
517 last->inp_socket->so_options & SO_TIMESTAMP) {
519 if (last->inp_vflag & INP_IPV6) {
522 if (udp_ip6.uip6_init_done == 0) {
523 ip_2_ip6_hdr(&udp_ip6.uip6_ip6, ip);
524 udp_ip6.uip6_init_done = 1;
526 savedflags = last->inp_flags;
527 last->inp_flags &= ~INP_UNMAPPABLEOPTS;
528 ip6_savecontrol(last, &opts, &udp_ip6.uip6_ip6, n);
529 last->inp_flags = savedflags;
532 ip_savecontrol(last, &opts, ip, n);
535 if (last->inp_vflag & INP_IPV6) {
536 if (udp_in6.uin6_init_done == 0) {
537 in6_sin_2_v4mapsin6(&udp_in, &udp_in6.uin6_sin);
538 udp_in6.uin6_init_done = 1;
540 append_sa = (struct sockaddr *)&udp_in6.uin6_sin;
543 append_sa = (struct sockaddr *)&udp_in;
545 if (sbappendaddr(&last->inp_socket->so_rcv, append_sa, n, opts) == 0) {
549 udpstat.udps_fullsock++;
551 sorwakeup(last->inp_socket);
555 * Notify a udp user of an asynchronous error;
556 * just wake up so that he can collect error status.
559 udp_notify(inp, errno)
563 inp->inp_socket->so_error = errno;
564 sorwakeup(inp->inp_socket);
565 sowwakeup(inp->inp_socket);
569 udp_ctlinput(cmd, sa, vip)
576 void (*notify) (struct inpcb *, int) = udp_notify;
577 struct in_addr faddr;
581 faddr = ((struct sockaddr_in *)sa)->sin_addr;
582 if (sa->sa_family != AF_INET || faddr.s_addr == INADDR_ANY)
585 if (PRC_IS_REDIRECT(cmd)) {
587 notify = in_rtchange;
588 } else if (cmd == PRC_HOSTDEAD)
590 else if ((unsigned)cmd >= PRC_NCMDS || inetctlerrmap[cmd] == 0)
594 uh = (struct udphdr *)((caddr_t)ip + (ip->ip_hl << 2));
595 inp = in_pcblookup_hash(&udbinfo, faddr, uh->uh_dport,
596 ip->ip_src, uh->uh_sport, 0, NULL);
597 if (inp != NULL && inp->inp_socket != NULL)
598 (*notify)(inp, inetctlerrmap[cmd]);
601 in_pcbnotifyall(&udbinfo.pcblisthead, faddr, inetctlerrmap[cmd],
606 udp_pcblist(SYSCTL_HANDLER_ARGS)
610 struct inpcb *marker;
616 * The process of preparing the TCB list is too time-consuming and
617 * resource-intensive to repeat twice on every request.
619 if (req->oldptr == 0) {
620 n = udbinfo.ipi_count;
621 req->oldidx = 2 * (sizeof xig)
622 + (n + n/8) * sizeof(struct xinpcb);
626 if (req->newptr != 0)
630 * OK, now we're committed to doing something.
632 gencnt = udbinfo.ipi_gencnt;
633 n = udbinfo.ipi_count;
635 xig.xig_len = sizeof xig;
637 xig.xig_gen = gencnt;
638 xig.xig_sogen = so_gencnt;
640 error = SYSCTL_OUT(req, &xig, sizeof xig);
644 marker = malloc(sizeof(struct inpcb), M_TEMP, M_WAITOK|M_ZERO);
645 marker->inp_flags |= INP_PLACEMARKER;
647 LIST_INSERT_HEAD(&udbinfo.pcblisthead, marker, inp_list);
649 while ((inp = LIST_NEXT(marker, inp_list)) != NULL && i < n) {
650 LIST_REMOVE(marker, inp_list);
651 LIST_INSERT_AFTER(inp, marker, inp_list);
652 if (inp->inp_flags & INP_PLACEMARKER)
654 if (inp->inp_gencnt > gencnt)
656 if (prison_xinpcb(req->td, inp))
658 xi.xi_len = sizeof xi;
659 bcopy(inp, &xi.xi_inp, sizeof *inp);
661 sotoxsocket(inp->inp_socket, &xi.xi_socket);
662 if ((error = SYSCTL_OUT(req, &xi, sizeof xi)) != 0)
666 LIST_REMOVE(marker, inp_list);
667 if (error == 0 && i < n) {
668 bzero(&xi, sizeof(xi));
669 xi.xi_len = sizeof(xi);
671 if ((error = SYSCTL_OUT(req, &xi, sizeof(xi))) != 0)
678 * Give the user an updated idea of our state.
679 * If the generation differs from what we told
680 * her before, she knows that something happened
681 * while we were processing this request, and it
682 * might be necessary to retry.
684 xig.xig_gen = udbinfo.ipi_gencnt;
685 xig.xig_sogen = so_gencnt;
686 xig.xig_count = udbinfo.ipi_count;
687 error = SYSCTL_OUT(req, &xig, sizeof xig);
689 free(marker, M_TEMP);
693 SYSCTL_PROC(_net_inet_udp, UDPCTL_PCBLIST, pcblist, CTLFLAG_RD, 0, 0,
694 udp_pcblist, "S,xinpcb", "List of active UDP sockets");
697 udp_getcred(SYSCTL_HANDLER_ARGS)
699 struct sockaddr_in addrs[2];
703 error = suser(req->td);
706 error = SYSCTL_IN(req, addrs, sizeof(addrs));
710 inp = in_pcblookup_hash(&udbinfo, addrs[1].sin_addr, addrs[1].sin_port,
711 addrs[0].sin_addr, addrs[0].sin_port, 1, NULL);
712 if (inp == NULL || inp->inp_socket == NULL) {
716 error = SYSCTL_OUT(req, inp->inp_socket->so_cred, sizeof(struct ucred));
722 SYSCTL_PROC(_net_inet_udp, OID_AUTO, getcred, CTLTYPE_OPAQUE|CTLFLAG_RW,
723 0, 0, udp_getcred, "S,ucred", "Get the ucred of a UDP connection");
726 udp_output(inp, m, dstaddr, control, td)
729 struct sockaddr *dstaddr;
730 struct mbuf *control;
734 int len = m->m_pkthdr.len;
735 struct sockaddr_in *sin; /* really is initialized before use */
738 if (len + sizeof(struct udpiphdr) > IP_MAXPACKET) {
743 if (inp->inp_lport == 0) { /* unbound socket */
744 error = in_pcbbind(inp, (struct sockaddr *)NULL, td);
747 in_pcbinswildcardhash(inp);
750 if (dstaddr != NULL) { /* destination address specified */
751 if (inp->inp_faddr.s_addr != INADDR_ANY) {
752 /* already connected */
756 sin = (struct sockaddr_in *)dstaddr;
757 prison_remote_ip(td, 0, &sin->sin_addr.s_addr);
759 if (inp->inp_faddr.s_addr == INADDR_ANY) {
760 /* no destination specified and not already connected */
768 * Calculate data length and get a mbuf
769 * for UDP and IP headers.
771 M_PREPEND(m, sizeof(struct udpiphdr), MB_DONTWAIT);
778 * Fill in mbuf with extended UDP header
779 * and addresses and length put into network format.
781 ui = mtod(m, struct udpiphdr *);
782 bzero(ui->ui_x1, sizeof(ui->ui_x1)); /* XXX still needed? */
783 ui->ui_pr = IPPROTO_UDP;
786 * Set destination address.
788 if (dstaddr != NULL) { /* use specified destination */
789 ui->ui_dst = sin->sin_addr;
790 ui->ui_dport = sin->sin_port;
791 } else { /* use connected destination */
792 ui->ui_dst = inp->inp_faddr;
793 ui->ui_dport = inp->inp_fport;
797 * Set source address.
799 if (inp->inp_laddr.s_addr == INADDR_ANY) {
800 struct sockaddr_in *if_sin;
802 KASSERT(dstaddr != NULL,
803 ("connected UDP socket without local addr: "
804 "lport %d, faddr %x, fport %d",
805 inp->inp_lport, inp->inp_faddr.s_addr, inp->inp_fport));
807 /* Look up outgoing interface. */
808 if ((error = in_pcbladdr(inp, dstaddr, &if_sin)))
810 ui->ui_src = if_sin->sin_addr; /* use address of interface */
812 ui->ui_src = inp->inp_laddr; /* use non-null bound address */
814 ui->ui_sport = inp->inp_lport;
815 KASSERT(inp->inp_lport != 0, ("inp lport should have been bound"));
817 ui->ui_ulen = htons((u_short)len + sizeof(struct udphdr));
820 * Set up checksum and output datagram.
823 ui->ui_sum = in_pseudo(ui->ui_src.s_addr, ui->ui_dst.s_addr,
824 htons((u_short)len + sizeof(struct udphdr) + IPPROTO_UDP));
825 m->m_pkthdr.csum_flags = CSUM_UDP;
826 m->m_pkthdr.csum_data = offsetof(struct udphdr, uh_sum);
830 ((struct ip *)ui)->ip_len = sizeof (struct udpiphdr) + len;
831 ((struct ip *)ui)->ip_ttl = inp->inp_ip_ttl; /* XXX */
832 ((struct ip *)ui)->ip_tos = inp->inp_ip_tos; /* XXX */
833 udpstat.udps_opackets++;
835 error = ip_output(m, inp->inp_options, &inp->inp_route,
836 (inp->inp_socket->so_options & (SO_DONTROUTE | SO_BROADCAST)),
837 inp->inp_moptions, inp);
846 u_long udp_sendspace = 9216; /* really max datagram size */
847 /* 40 1K datagrams */
848 SYSCTL_INT(_net_inet_udp, UDPCTL_MAXDGRAM, maxdgram, CTLFLAG_RW,
849 &udp_sendspace, 0, "Maximum outgoing UDP datagram size");
851 u_long udp_recvspace = 40 * (1024 +
853 sizeof(struct sockaddr_in6)
855 sizeof(struct sockaddr_in)
858 SYSCTL_INT(_net_inet_udp, UDPCTL_RECVSPACE, recvspace, CTLFLAG_RW,
859 &udp_recvspace, 0, "Maximum incoming UDP datagram size");
862 udp_abort(struct socket *so)
869 return EINVAL; /* ??? possible? panic instead? */
870 soisdisconnected(so);
878 udp_attach(struct socket *so, int proto, struct pru_attach_info *ai)
887 error = soreserve(so, udp_sendspace, udp_recvspace, ai->sb_rlimit);
891 error = in_pcballoc(so, &udbinfo);
896 inp = (struct inpcb *)so->so_pcb;
897 inp->inp_vflag |= INP_IPV4;
898 inp->inp_ip_ttl = ip_defttl;
903 udp_bind(struct socket *so, struct sockaddr *nam, struct thread *td)
905 struct sockaddr_in *sin = (struct sockaddr_in *)nam;
913 error = in_pcbbind(inp, nam, td);
916 if (sin->sin_addr.s_addr != INADDR_ANY)
917 inp->inp_flags |= INP_WASBOUND_NOTANY;
918 in_pcbinswildcardhash(inp);
924 udp_connect(struct socket *so, struct sockaddr *nam, struct thread *td)
928 struct sockaddr_in *sin;
933 if (inp->inp_faddr.s_addr != INADDR_ANY)
937 if (td->td_proc && td->td_proc->p_ucred->cr_prison != NULL &&
938 inp->inp_laddr.s_addr == INADDR_ANY) {
939 error = in_pcbbind(inp, NULL, td);
942 sin = (struct sockaddr_in *)nam;
943 prison_remote_ip(td, 0, &sin->sin_addr.s_addr);
944 if (inp->inp_flags & INP_WILDCARD)
945 in_pcbremwildcardhash(inp);
946 error = in_pcbconnect(inp, nam, td);
951 else if (error == EAFNOSUPPORT) { /* connection dissolved */
953 * Follow traditional BSD behavior and retain
954 * the local port binding. But, fix the old misbehavior
955 * of overwriting any previously bound local address.
957 if (!(inp->inp_flags & INP_WASBOUND_NOTANY))
958 inp->inp_laddr.s_addr = INADDR_ANY;
959 in_pcbinswildcardhash(inp);
965 udp_detach(struct socket *so)
980 udp_disconnect(struct socket *so)
988 if (inp->inp_faddr.s_addr == INADDR_ANY)
992 in_pcbdisconnect(inp);
994 so->so_state &= ~SS_ISCONNECTED; /* XXX */
999 udp_send(struct socket *so, int flags, struct mbuf *m, struct sockaddr *addr,
1000 struct mbuf *control, struct thread *td)
1004 inp = sotoinpcb(so);
1009 return udp_output(inp, m, addr, control, td);
1013 udp_shutdown(struct socket *so)
1017 inp = sotoinpcb(so);
1024 struct pr_usrreqs udp_usrreqs = {
1025 udp_abort, pru_accept_notsupp, udp_attach, udp_bind, udp_connect,
1026 pru_connect2_notsupp, in_control, udp_detach, udp_disconnect,
1027 pru_listen_notsupp, in_setpeeraddr, pru_rcvd_notsupp,
1028 pru_rcvoob_notsupp, udp_send, pru_sense_null, udp_shutdown,
1029 in_setsockaddr, sosendudp, soreceive, sopoll