2 * Copyright (c) 1982, 1986, 1988, 1990, 1993, 1995
3 * The Regents of the University of California. All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. All advertising materials mentioning features or use of this software
14 * must display the following acknowledgement:
15 * This product includes software developed by the University of
16 * California, Berkeley and its contributors.
17 * 4. Neither the name of the University nor the names of its contributors
18 * may be used to endorse or promote products derived from this software
19 * without specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 * @(#)udp_usrreq.c 8.6 (Berkeley) 5/23/95
37 #include <sys/param.h>
38 #include <sys/systm.h>
39 #include <sys/kernel.h>
40 #include <sys/malloc.h>
43 #include <sys/protosw.h>
44 #include <sys/socket.h>
45 #include <sys/socketvar.h>
46 #include <sys/sysctl.h>
47 #include <sys/syslog.h>
49 #include <vm/vm_zone.h>
52 #include <net/route.h>
54 #include <netinet/in.h>
55 #include <netinet/in_systm.h>
56 #include <netinet/ip.h>
57 #include <netinet/in_pcb.h>
58 #include <netinet/in_var.h>
59 #include <netinet/ip_var.h>
60 #include <netinet/ip_icmp.h>
61 #include <netinet/icmp_var.h>
62 #include <netinet/udp.h>
63 #include <netinet/udp_var.h>
66 * UDP protocol implementation.
67 * Per RFC 768, August, 1980.
70 static int udpcksum = 1;
72 static int udpcksum = 0; /* XXX */
74 SYSCTL_INT(_net_inet_udp, UDPCTL_CHECKSUM, checksum, CTLFLAG_RW,
78 SYSCTL_INT(_net_inet_udp, OID_AUTO, log_in_vain, CTLFLAG_RW,
79 &log_in_vain, 0, "Log all incoming UDP packets");
81 static int blackhole = 0;
82 SYSCTL_INT(_net_inet_udp, OID_AUTO, blackhole, CTLFLAG_RW,
83 &blackhole, 0, "Do not send port unreachables for refused connects");
85 struct inpcbhead udb; /* from udp_var.h */
86 struct inpcbinfo udbinfo;
89 #define UDBHASHSIZE 16
92 struct udpstat udpstat; /* from udp_var.h */
93 SYSCTL_STRUCT(_net_inet_udp, UDPCTL_STATS, stats, CTLFLAG_RD,
94 &udpstat, udpstat, "UDP statistics (struct udpstat, netinet/udp_var.h)");
96 static struct sockaddr_in udp_in = { sizeof(udp_in), AF_INET };
98 static int udp_output __P((struct inpcb *, struct mbuf *, struct sockaddr *,
99 struct mbuf *, struct proc *));
105 udbinfo.listhead = &udb;
106 udbinfo.hashbase = hashinit(UDBHASHSIZE, M_PCB, &udbinfo.hashmask);
107 udbinfo.porthashbase = hashinit(UDBHASHSIZE, M_PCB,
108 &udbinfo.porthashmask);
109 udbinfo.ipi_zone = zinit("udpcb", sizeof(struct inpcb), maxsockets,
115 register struct mbuf *m;
118 register struct ip *ip;
119 register struct udphdr *uh;
120 register struct inpcb *inp;
121 struct mbuf *opts = 0;
125 udpstat.udps_ipackets++;
128 * Strip IP options, if any; should skip this,
129 * make available to user, and use on returned packets,
130 * but we don't yet have a way to check the checksum
131 * with options still present.
133 if (iphlen > sizeof (struct ip)) {
134 ip_stripoptions(m, (struct mbuf *)0);
135 iphlen = sizeof(struct ip);
139 * Get IP and UDP header together in first mbuf.
141 ip = mtod(m, struct ip *);
142 if (m->m_len < iphlen + sizeof(struct udphdr)) {
143 if ((m = m_pullup(m, iphlen + sizeof(struct udphdr))) == 0) {
144 udpstat.udps_hdrops++;
147 ip = mtod(m, struct ip *);
149 uh = (struct udphdr *)((caddr_t)ip + iphlen);
152 * Make mbuf data length reflect UDP length.
153 * If not enough data to reflect UDP length, drop.
155 len = ntohs((u_short)uh->uh_ulen);
156 if (ip->ip_len != len) {
157 if (len > ip->ip_len || len < sizeof(struct udphdr)) {
158 udpstat.udps_badlen++;
161 m_adj(m, len - ip->ip_len);
162 /* ip->ip_len = len; */
165 * Save a copy of the IP header in case we want restore it
166 * for sending an ICMP error message in response.
171 * Checksum extended UDP header and data.
174 bzero(((struct ipovly *)ip)->ih_x1, 9);
175 ((struct ipovly *)ip)->ih_len = uh->uh_ulen;
176 uh->uh_sum = in_cksum(m, len + sizeof (struct ip));
178 udpstat.udps_badsum++;
184 if (IN_MULTICAST(ntohl(ip->ip_dst.s_addr)) ||
185 in_broadcast(ip->ip_dst, m->m_pkthdr.rcvif)) {
188 * Deliver a multicast or broadcast datagram to *all* sockets
189 * for which the local and remote addresses and ports match
190 * those of the incoming datagram. This allows more than
191 * one process to receive multi/broadcasts on the same port.
192 * (This really ought to be done for unicast datagrams as
193 * well, but that would cause problems with existing
194 * applications that open both address-specific sockets and
195 * a wildcard socket listening to the same port -- they would
196 * end up receiving duplicates of every unicast datagram.
197 * Those applications open the multiple sockets to overcome an
198 * inadequacy of the UDP socket interface, but for backwards
199 * compatibility we avoid the problem here rather than
200 * fixing the interface. Maybe 4.5BSD will remedy this?)
204 * Construct sockaddr format source address.
206 udp_in.sin_port = uh->uh_sport;
207 udp_in.sin_addr = ip->ip_src;
208 m->m_len -= sizeof (struct udpiphdr);
209 m->m_data += sizeof (struct udpiphdr);
211 * Locate pcb(s) for datagram.
212 * (Algorithm copied from raw_intr().)
215 for (inp = udb.lh_first; inp != NULL; inp = inp->inp_list.le_next) {
216 if (inp->inp_lport != uh->uh_dport)
218 if (inp->inp_laddr.s_addr != INADDR_ANY) {
219 if (inp->inp_laddr.s_addr !=
223 if (inp->inp_faddr.s_addr != INADDR_ANY) {
224 if (inp->inp_faddr.s_addr !=
226 inp->inp_fport != uh->uh_sport)
233 if ((n = m_copy(m, 0, M_COPYALL)) != NULL) {
234 if (last->inp_flags & INP_CONTROLOPTS
235 || last->inp_socket->so_options & SO_TIMESTAMP)
236 ip_savecontrol(last, &opts, ip, n);
237 if (sbappendaddr(&last->inp_socket->so_rcv,
238 (struct sockaddr *)&udp_in,
243 udpstat.udps_fullsock++;
245 sorwakeup(last->inp_socket);
251 * Don't look for additional matches if this one does
252 * not have either the SO_REUSEPORT or SO_REUSEADDR
253 * socket options set. This heuristic avoids searching
254 * through all pcbs in the common case of a non-shared
255 * port. It * assumes that an application will never
256 * clear these options after setting them.
258 if ((last->inp_socket->so_options&(SO_REUSEPORT|SO_REUSEADDR)) == 0)
264 * No matching pcb found; discard datagram.
265 * (No need to send an ICMP Port Unreachable
266 * for a broadcast or multicast datgram.)
268 udpstat.udps_noportbcast++;
271 if (last->inp_flags & INP_CONTROLOPTS
272 || last->inp_socket->so_options & SO_TIMESTAMP)
273 ip_savecontrol(last, &opts, ip, m);
274 if (sbappendaddr(&last->inp_socket->so_rcv,
275 (struct sockaddr *)&udp_in,
277 udpstat.udps_fullsock++;
280 sorwakeup(last->inp_socket);
284 * Locate pcb for datagram.
286 inp = in_pcblookup_hash(&udbinfo, ip->ip_src, uh->uh_sport,
287 ip->ip_dst, uh->uh_dport, 1);
290 char buf[4*sizeof "123"];
292 strcpy(buf, inet_ntoa(ip->ip_dst));
294 "Connection attempt to UDP %s:%d from %s:%d\n",
295 buf, ntohs(uh->uh_dport), inet_ntoa(ip->ip_src),
296 ntohs(uh->uh_sport));
298 udpstat.udps_noport++;
299 if (m->m_flags & (M_BCAST | M_MCAST)) {
300 udpstat.udps_noportbcast++;
305 if (badport_bandlim(0) < 0)
309 icmp_error(m, ICMP_UNREACH, ICMP_UNREACH_PORT, 0, 0);
316 * Construct sockaddr format source address.
317 * Stuff source address and datagram in user buffer.
319 udp_in.sin_port = uh->uh_sport;
320 udp_in.sin_addr = ip->ip_src;
321 if (inp->inp_flags & INP_CONTROLOPTS
322 || inp->inp_socket->so_options & SO_TIMESTAMP)
323 ip_savecontrol(inp, &opts, ip, m);
324 iphlen += sizeof(struct udphdr);
326 m->m_pkthdr.len -= iphlen;
328 if (sbappendaddr(&inp->inp_socket->so_rcv, (struct sockaddr *)&udp_in,
330 udpstat.udps_fullsock++;
333 sorwakeup(inp->inp_socket);
342 * Notify a udp user of an asynchronous error;
343 * just wake up so that he can collect error status.
346 udp_notify(inp, errno)
347 register struct inpcb *inp;
350 inp->inp_socket->so_error = errno;
351 sorwakeup(inp->inp_socket);
352 sowwakeup(inp->inp_socket);
356 udp_ctlinput(cmd, sa, vip)
361 register struct ip *ip = vip;
362 register struct udphdr *uh;
364 if (!PRC_IS_REDIRECT(cmd) &&
365 ((unsigned)cmd >= PRC_NCMDS || inetctlerrmap[cmd] == 0))
368 uh = (struct udphdr *)((caddr_t)ip + (ip->ip_hl << 2));
369 in_pcbnotify(&udb, sa, uh->uh_dport, ip->ip_src, uh->uh_sport,
372 in_pcbnotify(&udb, sa, 0, zeroin_addr, 0, cmd, udp_notify);
376 udp_pcblist SYSCTL_HANDLER_ARGS
379 struct inpcb *inp, **inp_list;
384 * The process of preparing the TCB list is too time-consuming and
385 * resource-intensive to repeat twice on every request.
387 if (req->oldptr == 0) {
388 n = udbinfo.ipi_count;
389 req->oldidx = 2 * (sizeof xig)
390 + (n + n/8) * sizeof(struct xinpcb);
394 if (req->newptr != 0)
398 * OK, now we're committed to doing something.
401 gencnt = udbinfo.ipi_gencnt;
402 n = udbinfo.ipi_count;
405 xig.xig_len = sizeof xig;
407 xig.xig_gen = gencnt;
408 xig.xig_sogen = so_gencnt;
409 error = SYSCTL_OUT(req, &xig, sizeof xig);
413 inp_list = malloc(n * sizeof *inp_list, M_TEMP, M_WAITOK);
418 for (inp = udbinfo.listhead->lh_first, i = 0; inp && i < n;
419 inp = inp->inp_list.le_next) {
420 if (inp->inp_gencnt <= gencnt && !prison_xinpcb(req->p, inp))
427 for (i = 0; i < n; i++) {
429 if (inp->inp_gencnt <= gencnt) {
431 xi.xi_len = sizeof xi;
432 /* XXX should avoid extra copy */
433 bcopy(inp, &xi.xi_inp, sizeof *inp);
435 sotoxsocket(inp->inp_socket, &xi.xi_socket);
436 error = SYSCTL_OUT(req, &xi, sizeof xi);
441 * Give the user an updated idea of our state.
442 * If the generation differs from what we told
443 * her before, she knows that something happened
444 * while we were processing this request, and it
445 * might be necessary to retry.
448 xig.xig_gen = udbinfo.ipi_gencnt;
449 xig.xig_sogen = so_gencnt;
450 xig.xig_count = udbinfo.ipi_count;
452 error = SYSCTL_OUT(req, &xig, sizeof xig);
454 free(inp_list, M_TEMP);
458 SYSCTL_PROC(_net_inet_udp, UDPCTL_PCBLIST, pcblist, CTLFLAG_RD, 0, 0,
459 udp_pcblist, "S,xinpcb", "List of active UDP sockets");
462 udp_getcred SYSCTL_HANDLER_ARGS
464 struct sockaddr_in addrs[2];
468 error = suser(req->p);
471 error = SYSCTL_IN(req, addrs, sizeof(addrs));
475 inp = in_pcblookup_hash(&udbinfo, addrs[1].sin_addr, addrs[1].sin_port,
476 addrs[0].sin_addr, addrs[0].sin_port, 1);
477 if (inp == NULL || inp->inp_socket == NULL) {
481 error = SYSCTL_OUT(req, inp->inp_socket->so_cred, sizeof(struct ucred));
487 SYSCTL_PROC(_net_inet_udp, OID_AUTO, getcred, CTLTYPE_OPAQUE|CTLFLAG_RW,
488 0, 0, udp_getcred, "S,ucred", "Get the ucred of a UDP connection");
491 udp_output(inp, m, addr, control, p)
492 register struct inpcb *inp;
493 register struct mbuf *m;
494 struct sockaddr *addr;
495 struct mbuf *control;
498 register struct udpiphdr *ui;
499 register int len = m->m_pkthdr.len;
500 struct in_addr laddr;
501 struct sockaddr_in *sin;
502 int s = 0, error = 0;
505 m_freem(control); /* XXX */
507 if (len + sizeof(struct udpiphdr) > IP_MAXPACKET) {
513 sin = (struct sockaddr_in *)addr;
514 prison_remote_ip(p, 0, &sin->sin_addr.s_addr);
515 laddr = inp->inp_laddr;
516 if (inp->inp_faddr.s_addr != INADDR_ANY) {
521 * Must block input while temporarily connected.
524 error = in_pcbconnect(inp, addr, p);
530 if (inp->inp_faddr.s_addr == INADDR_ANY) {
536 * Calculate data length and get a mbuf
537 * for UDP and IP headers.
539 M_PREPEND(m, sizeof(struct udpiphdr), M_DONTWAIT);
548 * Fill in mbuf with extended UDP header
549 * and addresses and length put into network format.
551 ui = mtod(m, struct udpiphdr *);
552 bzero(ui->ui_x1, sizeof(ui->ui_x1));
553 ui->ui_pr = IPPROTO_UDP;
554 ui->ui_len = htons((u_short)len + sizeof (struct udphdr));
555 ui->ui_src = inp->inp_laddr;
556 ui->ui_dst = inp->inp_faddr;
557 ui->ui_sport = inp->inp_lport;
558 ui->ui_dport = inp->inp_fport;
559 ui->ui_ulen = ui->ui_len;
562 * Stuff checksum and output datagram.
566 if ((ui->ui_sum = in_cksum(m, sizeof (struct udpiphdr) + len)) == 0)
569 ((struct ip *)ui)->ip_len = sizeof (struct udpiphdr) + len;
570 ((struct ip *)ui)->ip_ttl = inp->inp_ip_ttl; /* XXX */
571 ((struct ip *)ui)->ip_tos = inp->inp_ip_tos; /* XXX */
572 udpstat.udps_opackets++;
573 error = ip_output(m, inp->inp_options, &inp->inp_route,
574 inp->inp_socket->so_options & (SO_DONTROUTE | SO_BROADCAST),
578 in_pcbdisconnect(inp);
579 inp->inp_laddr = laddr; /* XXX rehash? */
589 u_long udp_sendspace = 9216; /* really max datagram size */
590 /* 40 1K datagrams */
591 SYSCTL_INT(_net_inet_udp, UDPCTL_MAXDGRAM, maxdgram, CTLFLAG_RW,
592 &udp_sendspace, 0, "Maximum outgoing UDP datagram size");
594 u_long udp_recvspace = 40 * (1024 + sizeof(struct sockaddr_in));
595 SYSCTL_INT(_net_inet_udp, UDPCTL_RECVSPACE, recvspace, CTLFLAG_RW,
596 &udp_recvspace, 0, "Maximum incoming UDP datagram size");
599 udp_abort(struct socket *so)
606 return EINVAL; /* ??? possible? panic instead? */
607 soisdisconnected(so);
615 udp_attach(struct socket *so, int proto, struct proc *p)
625 error = in_pcballoc(so, &udbinfo, p);
629 error = soreserve(so, udp_sendspace, udp_recvspace);
632 ((struct inpcb *) so->so_pcb)->inp_ip_ttl = ip_defttl;
637 udp_bind(struct socket *so, struct sockaddr *nam, struct proc *p)
646 error = in_pcbbind(inp, nam, p);
652 udp_connect(struct socket *so, struct sockaddr *nam, struct proc *p)
656 struct sockaddr_in *sin;
661 if (inp->inp_faddr.s_addr != INADDR_ANY)
664 sin = (struct sockaddr_in *)nam;
665 prison_remote_ip(p, 0, &sin->sin_addr.s_addr);
666 error = in_pcbconnect(inp, nam, p);
674 udp_detach(struct socket *so)
689 udp_disconnect(struct socket *so)
697 if (inp->inp_faddr.s_addr == INADDR_ANY)
701 in_pcbdisconnect(inp);
702 inp->inp_laddr.s_addr = INADDR_ANY;
704 so->so_state &= ~SS_ISCONNECTED; /* XXX */
709 udp_send(struct socket *so, int flags, struct mbuf *m, struct sockaddr *addr,
710 struct mbuf *control, struct proc *p)
719 return udp_output(inp, m, addr, control, p);
723 udp_shutdown(struct socket *so)
734 struct pr_usrreqs udp_usrreqs = {
735 udp_abort, pru_accept_notsupp, udp_attach, udp_bind, udp_connect,
736 pru_connect2_notsupp, in_control, udp_detach, udp_disconnect,
737 pru_listen_notsupp, in_setpeeraddr, pru_rcvd_notsupp,
738 pru_rcvoob_notsupp, udp_send, pru_sense_null, udp_shutdown,
739 in_setsockaddr, sosend, soreceive, sopoll