Deleted Added
full compact
udp_usrreq.c (162071) udp_usrreq.c (163606)
1/*-
2 * Copyright (c) 1982, 1986, 1988, 1990, 1993, 1995
3 * The Regents of the University of California. All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
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 * 4. Neither the name of the University nor the names of its contributors
14 * may be used to endorse or promote products derived from this software
15 * without specific prior written permission.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27 * SUCH DAMAGE.
28 *
29 * @(#)udp_usrreq.c 8.6 (Berkeley) 5/23/95
1/*-
2 * Copyright (c) 1982, 1986, 1988, 1990, 1993, 1995
3 * The Regents of the University of California. All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
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 * 4. Neither the name of the University nor the names of its contributors
14 * may be used to endorse or promote products derived from this software
15 * without specific prior written permission.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27 * SUCH DAMAGE.
28 *
29 * @(#)udp_usrreq.c 8.6 (Berkeley) 5/23/95
30 * $FreeBSD: head/sys/netinet/udp_usrreq.c 162071 2006-09-06 19:04:36Z andre $
30 * $FreeBSD: head/sys/netinet/udp_usrreq.c 163606 2006-10-22 11:52:19Z rwatson $
31 */
32
33#include "opt_ipfw.h"
34#include "opt_ipsec.h"
35#include "opt_inet6.h"
36#include "opt_mac.h"
37
38#include <sys/param.h>
39#include <sys/systm.h>
40#include <sys/domain.h>
41#include <sys/eventhandler.h>
42#include <sys/jail.h>
43#include <sys/kernel.h>
44#include <sys/lock.h>
31 */
32
33#include "opt_ipfw.h"
34#include "opt_ipsec.h"
35#include "opt_inet6.h"
36#include "opt_mac.h"
37
38#include <sys/param.h>
39#include <sys/systm.h>
40#include <sys/domain.h>
41#include <sys/eventhandler.h>
42#include <sys/jail.h>
43#include <sys/kernel.h>
44#include <sys/lock.h>
45#include <sys/mac.h>
46#include <sys/malloc.h>
47#include <sys/mbuf.h>
48#include <sys/proc.h>
49#include <sys/protosw.h>
50#include <sys/signalvar.h>
51#include <sys/socket.h>
52#include <sys/socketvar.h>
53#include <sys/sx.h>
54#include <sys/sysctl.h>
55#include <sys/syslog.h>
56
57#include <vm/uma.h>
58
59#include <net/if.h>
60#include <net/route.h>
61
62#include <netinet/in.h>
63#include <netinet/in_systm.h>
64#include <netinet/in_pcb.h>
65#include <netinet/in_var.h>
66#include <netinet/ip.h>
67#ifdef INET6
68#include <netinet/ip6.h>
69#endif
70#include <netinet/ip_icmp.h>
71#include <netinet/icmp_var.h>
72#include <netinet/ip_var.h>
73#include <netinet/ip_options.h>
74#ifdef INET6
75#include <netinet6/ip6_var.h>
76#endif
77#include <netinet/udp.h>
78#include <netinet/udp_var.h>
79
80#ifdef FAST_IPSEC
81#include <netipsec/ipsec.h>
82#endif /*FAST_IPSEC*/
83
84#ifdef IPSEC
85#include <netinet6/ipsec.h>
86#endif /*IPSEC*/
87
88#include <machine/in_cksum.h>
89
45#include <sys/malloc.h>
46#include <sys/mbuf.h>
47#include <sys/proc.h>
48#include <sys/protosw.h>
49#include <sys/signalvar.h>
50#include <sys/socket.h>
51#include <sys/socketvar.h>
52#include <sys/sx.h>
53#include <sys/sysctl.h>
54#include <sys/syslog.h>
55
56#include <vm/uma.h>
57
58#include <net/if.h>
59#include <net/route.h>
60
61#include <netinet/in.h>
62#include <netinet/in_systm.h>
63#include <netinet/in_pcb.h>
64#include <netinet/in_var.h>
65#include <netinet/ip.h>
66#ifdef INET6
67#include <netinet/ip6.h>
68#endif
69#include <netinet/ip_icmp.h>
70#include <netinet/icmp_var.h>
71#include <netinet/ip_var.h>
72#include <netinet/ip_options.h>
73#ifdef INET6
74#include <netinet6/ip6_var.h>
75#endif
76#include <netinet/udp.h>
77#include <netinet/udp_var.h>
78
79#ifdef FAST_IPSEC
80#include <netipsec/ipsec.h>
81#endif /*FAST_IPSEC*/
82
83#ifdef IPSEC
84#include <netinet6/ipsec.h>
85#endif /*IPSEC*/
86
87#include <machine/in_cksum.h>
88
89#include <security/mac/mac_framework.h>
90
90/*
91 * UDP protocol implementation.
92 * Per RFC 768, August, 1980.
93 */
94#ifndef COMPAT_42
95static int udpcksum = 1;
96#else
97static int udpcksum = 0; /* XXX */
98#endif
99SYSCTL_INT(_net_inet_udp, UDPCTL_CHECKSUM, checksum, CTLFLAG_RW,
100 &udpcksum, 0, "");
101
102int log_in_vain = 0;
103SYSCTL_INT(_net_inet_udp, OID_AUTO, log_in_vain, CTLFLAG_RW,
104 &log_in_vain, 0, "Log all incoming UDP packets");
105
106static int blackhole = 0;
107SYSCTL_INT(_net_inet_udp, OID_AUTO, blackhole, CTLFLAG_RW,
108 &blackhole, 0, "Do not send port unreachables for refused connects");
109
110static int strict_mcast_mship = 0;
111SYSCTL_INT(_net_inet_udp, OID_AUTO, strict_mcast_mship, CTLFLAG_RW,
112 &strict_mcast_mship, 0, "Only send multicast to member sockets");
113
114struct inpcbhead udb; /* from udp_var.h */
115#define udb6 udb /* for KAME src sync over BSD*'s */
116struct inpcbinfo udbinfo;
117
118#ifndef UDBHASHSIZE
119#define UDBHASHSIZE 16
120#endif
121
122struct udpstat udpstat; /* from udp_var.h */
123SYSCTL_STRUCT(_net_inet_udp, UDPCTL_STATS, stats, CTLFLAG_RW,
124 &udpstat, udpstat, "UDP statistics (struct udpstat, netinet/udp_var.h)");
125
126static void udp_append(struct inpcb *last, struct ip *ip, struct mbuf *n,
127 int off, struct sockaddr_in *udp_in);
128
129static void udp_detach(struct socket *so);
130static int udp_output(struct inpcb *, struct mbuf *, struct sockaddr *,
131 struct mbuf *, struct thread *);
132
133static void
134udp_zone_change(void *tag)
135{
136
137 uma_zone_set_max(udbinfo.ipi_zone, maxsockets);
138}
139
140static int
141udp_inpcb_init(void *mem, int size, int flags)
142{
143 struct inpcb *inp = (struct inpcb *) mem;
144 INP_LOCK_INIT(inp, "inp", "udpinp");
145 return (0);
146}
147
148void
149udp_init()
150{
151 INP_INFO_LOCK_INIT(&udbinfo, "udp");
152 LIST_INIT(&udb);
153 udbinfo.listhead = &udb;
154 udbinfo.hashbase = hashinit(UDBHASHSIZE, M_PCB, &udbinfo.hashmask);
155 udbinfo.porthashbase = hashinit(UDBHASHSIZE, M_PCB,
156 &udbinfo.porthashmask);
157 udbinfo.ipi_zone = uma_zcreate("udpcb", sizeof(struct inpcb), NULL,
158 NULL, udp_inpcb_init, NULL, UMA_ALIGN_PTR, UMA_ZONE_NOFREE);
159 uma_zone_set_max(udbinfo.ipi_zone, maxsockets);
160 EVENTHANDLER_REGISTER(maxsockets_change, udp_zone_change, NULL,
161 EVENTHANDLER_PRI_ANY);
162}
163
164void
165udp_input(m, off)
166 register struct mbuf *m;
167 int off;
168{
169 int iphlen = off;
170 register struct ip *ip;
171 register struct udphdr *uh;
172 register struct inpcb *inp;
173 int len;
174 struct ip save_ip;
175 struct sockaddr_in udp_in;
176#ifdef IPFIREWALL_FORWARD
177 struct m_tag *fwd_tag;
178#endif
179
180 udpstat.udps_ipackets++;
181
182 /*
183 * Strip IP options, if any; should skip this,
184 * make available to user, and use on returned packets,
185 * but we don't yet have a way to check the checksum
186 * with options still present.
187 */
188 if (iphlen > sizeof (struct ip)) {
189 ip_stripoptions(m, (struct mbuf *)0);
190 iphlen = sizeof(struct ip);
191 }
192
193 /*
194 * Get IP and UDP header together in first mbuf.
195 */
196 ip = mtod(m, struct ip *);
197 if (m->m_len < iphlen + sizeof(struct udphdr)) {
198 if ((m = m_pullup(m, iphlen + sizeof(struct udphdr))) == 0) {
199 udpstat.udps_hdrops++;
200 return;
201 }
202 ip = mtod(m, struct ip *);
203 }
204 uh = (struct udphdr *)((caddr_t)ip + iphlen);
205
206 /* destination port of 0 is illegal, based on RFC768. */
207 if (uh->uh_dport == 0)
208 goto badunlocked;
209
210 /*
211 * Construct sockaddr format source address.
212 * Stuff source address and datagram in user buffer.
213 */
214 bzero(&udp_in, sizeof(udp_in));
215 udp_in.sin_len = sizeof(udp_in);
216 udp_in.sin_family = AF_INET;
217 udp_in.sin_port = uh->uh_sport;
218 udp_in.sin_addr = ip->ip_src;
219
220 /*
221 * Make mbuf data length reflect UDP length.
222 * If not enough data to reflect UDP length, drop.
223 */
224 len = ntohs((u_short)uh->uh_ulen);
225 if (ip->ip_len != len) {
226 if (len > ip->ip_len || len < sizeof(struct udphdr)) {
227 udpstat.udps_badlen++;
228 goto badunlocked;
229 }
230 m_adj(m, len - ip->ip_len);
231 /* ip->ip_len = len; */
232 }
233 /*
234 * Save a copy of the IP header in case we want restore it
235 * for sending an ICMP error message in response.
236 */
237 if (!blackhole)
238 save_ip = *ip;
239
240 /*
241 * Checksum extended UDP header and data.
242 */
243 if (uh->uh_sum) {
244 if (m->m_pkthdr.csum_flags & CSUM_DATA_VALID) {
245 if (m->m_pkthdr.csum_flags & CSUM_PSEUDO_HDR)
246 uh->uh_sum = m->m_pkthdr.csum_data;
247 else
248 uh->uh_sum = in_pseudo(ip->ip_src.s_addr,
249 ip->ip_dst.s_addr, htonl((u_short)len +
250 m->m_pkthdr.csum_data + IPPROTO_UDP));
251 uh->uh_sum ^= 0xffff;
252 } else {
253 char b[9];
254 bcopy(((struct ipovly *)ip)->ih_x1, b, 9);
255 bzero(((struct ipovly *)ip)->ih_x1, 9);
256 ((struct ipovly *)ip)->ih_len = uh->uh_ulen;
257 uh->uh_sum = in_cksum(m, len + sizeof (struct ip));
258 bcopy(b, ((struct ipovly *)ip)->ih_x1, 9);
259 }
260 if (uh->uh_sum) {
261 udpstat.udps_badsum++;
262 m_freem(m);
263 return;
264 }
265 } else
266 udpstat.udps_nosum++;
267
268#ifdef IPFIREWALL_FORWARD
269 /* Grab info from PACKET_TAG_IPFORWARD tag prepended to the chain. */
270 fwd_tag = m_tag_find(m, PACKET_TAG_IPFORWARD, NULL);
271
272 if (fwd_tag != NULL) {
273 struct sockaddr_in *next_hop;
274
275 /* Do the hack. */
276 next_hop = (struct sockaddr_in *)(fwd_tag + 1);
277 ip->ip_dst = next_hop->sin_addr;
278 uh->uh_dport = ntohs(next_hop->sin_port);
279 /* Remove the tag from the packet. We don't need it anymore. */
280 m_tag_delete(m, fwd_tag);
281 }
282#endif
283
284 INP_INFO_RLOCK(&udbinfo);
285
286 if (IN_MULTICAST(ntohl(ip->ip_dst.s_addr)) ||
287 in_broadcast(ip->ip_dst, m->m_pkthdr.rcvif)) {
288 struct inpcb *last;
289 /*
290 * Deliver a multicast or broadcast datagram to *all* sockets
291 * for which the local and remote addresses and ports match
292 * those of the incoming datagram. This allows more than
293 * one process to receive multi/broadcasts on the same port.
294 * (This really ought to be done for unicast datagrams as
295 * well, but that would cause problems with existing
296 * applications that open both address-specific sockets and
297 * a wildcard socket listening to the same port -- they would
298 * end up receiving duplicates of every unicast datagram.
299 * Those applications open the multiple sockets to overcome an
300 * inadequacy of the UDP socket interface, but for backwards
301 * compatibility we avoid the problem here rather than
302 * fixing the interface. Maybe 4.5BSD will remedy this?)
303 */
304
305 /*
306 * Locate pcb(s) for datagram.
307 * (Algorithm copied from raw_intr().)
308 */
309 last = NULL;
310 LIST_FOREACH(inp, &udb, inp_list) {
311 if (inp->inp_lport != uh->uh_dport)
312 continue;
313#ifdef INET6
314 if ((inp->inp_vflag & INP_IPV4) == 0)
315 continue;
316#endif
317 if (inp->inp_laddr.s_addr != INADDR_ANY) {
318 if (inp->inp_laddr.s_addr != ip->ip_dst.s_addr)
319 continue;
320 }
321 if (inp->inp_faddr.s_addr != INADDR_ANY) {
322 if (inp->inp_faddr.s_addr !=
323 ip->ip_src.s_addr ||
324 inp->inp_fport != uh->uh_sport)
325 continue;
326 }
327 INP_LOCK(inp);
328
329 /*
330 * Check multicast packets to make sure they are only
331 * sent to sockets with multicast memberships for the
332 * packet's destination address and arrival interface
333 */
334#define MSHIP(_inp, n) ((_inp)->inp_moptions->imo_membership[(n)])
335#define NMSHIPS(_inp) ((_inp)->inp_moptions->imo_num_memberships)
336 if (strict_mcast_mship && inp->inp_moptions != NULL) {
337 int mship, foundmship = 0;
338
339 for (mship = 0; mship < NMSHIPS(inp); mship++) {
340 if (MSHIP(inp, mship)->inm_addr.s_addr
341 == ip->ip_dst.s_addr &&
342 MSHIP(inp, mship)->inm_ifp
343 == m->m_pkthdr.rcvif) {
344 foundmship = 1;
345 break;
346 }
347 }
348 if (foundmship == 0) {
349 INP_UNLOCK(inp);
350 continue;
351 }
352 }
353#undef NMSHIPS
354#undef MSHIP
355 if (last != NULL) {
356 struct mbuf *n;
357
358 n = m_copy(m, 0, M_COPYALL);
359 if (n != NULL)
360 udp_append(last, ip, n,
361 iphlen +
362 sizeof(struct udphdr),
363 &udp_in);
364 INP_UNLOCK(last);
365 }
366 last = inp;
367 /*
368 * Don't look for additional matches if this one does
369 * not have either the SO_REUSEPORT or SO_REUSEADDR
370 * socket options set. This heuristic avoids searching
371 * through all pcbs in the common case of a non-shared
372 * port. It assumes that an application will never
373 * clear these options after setting them.
374 */
375 if ((last->inp_socket->so_options&(SO_REUSEPORT|SO_REUSEADDR)) == 0)
376 break;
377 }
378
379 if (last == NULL) {
380 /*
381 * No matching pcb found; discard datagram.
382 * (No need to send an ICMP Port Unreachable
383 * for a broadcast or multicast datgram.)
384 */
385 udpstat.udps_noportbcast++;
386 goto badheadlocked;
387 }
388 udp_append(last, ip, m, iphlen + sizeof(struct udphdr),
389 &udp_in);
390 INP_UNLOCK(last);
391 INP_INFO_RUNLOCK(&udbinfo);
392 return;
393 }
394 /*
395 * Locate pcb for datagram.
396 */
397 inp = in_pcblookup_hash(&udbinfo, ip->ip_src, uh->uh_sport,
398 ip->ip_dst, uh->uh_dport, 1, m->m_pkthdr.rcvif);
399 if (inp == NULL) {
400 if (log_in_vain) {
401 char buf[4*sizeof "123"];
402
403 strcpy(buf, inet_ntoa(ip->ip_dst));
404 log(LOG_INFO,
405 "Connection attempt to UDP %s:%d from %s:%d\n",
406 buf, ntohs(uh->uh_dport), inet_ntoa(ip->ip_src),
407 ntohs(uh->uh_sport));
408 }
409 udpstat.udps_noport++;
410 if (m->m_flags & (M_BCAST | M_MCAST)) {
411 udpstat.udps_noportbcast++;
412 goto badheadlocked;
413 }
414 if (blackhole)
415 goto badheadlocked;
416 if (badport_bandlim(BANDLIM_ICMP_UNREACH) < 0)
417 goto badheadlocked;
418 *ip = save_ip;
419 ip->ip_len += iphlen;
420 icmp_error(m, ICMP_UNREACH, ICMP_UNREACH_PORT, 0, 0);
421 INP_INFO_RUNLOCK(&udbinfo);
422 return;
423 }
424 INP_LOCK(inp);
425 /* Check the minimum TTL for socket. */
426 if (inp->inp_ip_minttl && inp->inp_ip_minttl > ip->ip_ttl)
427 goto badheadlocked;
428 udp_append(inp, ip, m, iphlen + sizeof(struct udphdr), &udp_in);
429 INP_UNLOCK(inp);
430 INP_INFO_RUNLOCK(&udbinfo);
431 return;
432
433badheadlocked:
434 if (inp)
435 INP_UNLOCK(inp);
436 INP_INFO_RUNLOCK(&udbinfo);
437badunlocked:
438 m_freem(m);
439 return;
440}
441
442/*
443 * Subroutine of udp_input(), which appends the provided mbuf chain to the
444 * passed pcb/socket. The caller must provide a sockaddr_in via udp_in that
445 * contains the source address. If the socket ends up being an IPv6 socket,
446 * udp_append() will convert to a sockaddr_in6 before passing the address
447 * into the socket code.
448 */
449static void
450udp_append(inp, ip, n, off, udp_in)
451 struct inpcb *inp;
452 struct ip *ip;
453 struct mbuf *n;
454 int off;
455 struct sockaddr_in *udp_in;
456{
457 struct sockaddr *append_sa;
458 struct socket *so;
459 struct mbuf *opts = 0;
460#ifdef INET6
461 struct sockaddr_in6 udp_in6;
462#endif
463
464 INP_LOCK_ASSERT(inp);
465
466#if defined(IPSEC) || defined(FAST_IPSEC)
467 /* check AH/ESP integrity. */
468 if (ipsec4_in_reject(n, inp)) {
469#ifdef IPSEC
470 ipsecstat.in_polvio++;
471#endif /*IPSEC*/
472 m_freem(n);
473 return;
474 }
475#endif /*IPSEC || FAST_IPSEC*/
476#ifdef MAC
477 if (mac_check_inpcb_deliver(inp, n) != 0) {
478 m_freem(n);
479 return;
480 }
481#endif
482 if (inp->inp_flags & INP_CONTROLOPTS ||
483 inp->inp_socket->so_options & (SO_TIMESTAMP | SO_BINTIME)) {
484#ifdef INET6
485 if (inp->inp_vflag & INP_IPV6) {
486 int savedflags;
487
488 savedflags = inp->inp_flags;
489 inp->inp_flags &= ~INP_UNMAPPABLEOPTS;
490 ip6_savecontrol(inp, n, &opts);
491 inp->inp_flags = savedflags;
492 } else
493#endif
494 ip_savecontrol(inp, &opts, ip, n);
495 }
496#ifdef INET6
497 if (inp->inp_vflag & INP_IPV6) {
498 bzero(&udp_in6, sizeof(udp_in6));
499 udp_in6.sin6_len = sizeof(udp_in6);
500 udp_in6.sin6_family = AF_INET6;
501 in6_sin_2_v4mapsin6(udp_in, &udp_in6);
502 append_sa = (struct sockaddr *)&udp_in6;
503 } else
504#endif
505 append_sa = (struct sockaddr *)udp_in;
506 m_adj(n, off);
507
508 so = inp->inp_socket;
509 SOCKBUF_LOCK(&so->so_rcv);
510 if (sbappendaddr_locked(&so->so_rcv, append_sa, n, opts) == 0) {
511 m_freem(n);
512 if (opts)
513 m_freem(opts);
514 udpstat.udps_fullsock++;
515 SOCKBUF_UNLOCK(&so->so_rcv);
516 } else
517 sorwakeup_locked(so);
518}
519
520/*
521 * Notify a udp user of an asynchronous error;
522 * just wake up so that he can collect error status.
523 */
524struct inpcb *
525udp_notify(inp, errno)
526 register struct inpcb *inp;
527 int errno;
528{
529 inp->inp_socket->so_error = errno;
530 sorwakeup(inp->inp_socket);
531 sowwakeup(inp->inp_socket);
532 return inp;
533}
534
535void
536udp_ctlinput(cmd, sa, vip)
537 int cmd;
538 struct sockaddr *sa;
539 void *vip;
540{
541 struct ip *ip = vip;
542 struct udphdr *uh;
543 struct inpcb *(*notify)(struct inpcb *, int) = udp_notify;
544 struct in_addr faddr;
545 struct inpcb *inp;
546
547 faddr = ((struct sockaddr_in *)sa)->sin_addr;
548 if (sa->sa_family != AF_INET || faddr.s_addr == INADDR_ANY)
549 return;
550
551 /*
552 * Redirects don't need to be handled up here.
553 */
554 if (PRC_IS_REDIRECT(cmd))
555 return;
556 /*
557 * Hostdead is ugly because it goes linearly through all PCBs.
558 * XXX: We never get this from ICMP, otherwise it makes an
559 * excellent DoS attack on machines with many connections.
560 */
561 if (cmd == PRC_HOSTDEAD)
562 ip = 0;
563 else if ((unsigned)cmd >= PRC_NCMDS || inetctlerrmap[cmd] == 0)
564 return;
565 if (ip) {
566 uh = (struct udphdr *)((caddr_t)ip + (ip->ip_hl << 2));
567 INP_INFO_RLOCK(&udbinfo);
568 inp = in_pcblookup_hash(&udbinfo, faddr, uh->uh_dport,
569 ip->ip_src, uh->uh_sport, 0, NULL);
570 if (inp != NULL) {
571 INP_LOCK(inp);
572 if (inp->inp_socket != NULL) {
573 (*notify)(inp, inetctlerrmap[cmd]);
574 }
575 INP_UNLOCK(inp);
576 }
577 INP_INFO_RUNLOCK(&udbinfo);
578 } else
579 in_pcbnotifyall(&udbinfo, faddr, inetctlerrmap[cmd], notify);
580}
581
582static int
583udp_pcblist(SYSCTL_HANDLER_ARGS)
584{
585 int error, i, n;
586 struct inpcb *inp, **inp_list;
587 inp_gen_t gencnt;
588 struct xinpgen xig;
589
590 /*
591 * The process of preparing the TCB list is too time-consuming and
592 * resource-intensive to repeat twice on every request.
593 */
594 if (req->oldptr == 0) {
595 n = udbinfo.ipi_count;
596 req->oldidx = 2 * (sizeof xig)
597 + (n + n/8) * sizeof(struct xinpcb);
598 return 0;
599 }
600
601 if (req->newptr != 0)
602 return EPERM;
603
604 /*
605 * OK, now we're committed to doing something.
606 */
607 INP_INFO_RLOCK(&udbinfo);
608 gencnt = udbinfo.ipi_gencnt;
609 n = udbinfo.ipi_count;
610 INP_INFO_RUNLOCK(&udbinfo);
611
612 error = sysctl_wire_old_buffer(req, 2 * (sizeof xig)
613 + n * sizeof(struct xinpcb));
614 if (error != 0)
615 return (error);
616
617 xig.xig_len = sizeof xig;
618 xig.xig_count = n;
619 xig.xig_gen = gencnt;
620 xig.xig_sogen = so_gencnt;
621 error = SYSCTL_OUT(req, &xig, sizeof xig);
622 if (error)
623 return error;
624
625 inp_list = malloc(n * sizeof *inp_list, M_TEMP, M_WAITOK);
626 if (inp_list == 0)
627 return ENOMEM;
628
629 INP_INFO_RLOCK(&udbinfo);
630 for (inp = LIST_FIRST(udbinfo.listhead), i = 0; inp && i < n;
631 inp = LIST_NEXT(inp, inp_list)) {
632 INP_LOCK(inp);
633 if (inp->inp_gencnt <= gencnt &&
634 cr_canseesocket(req->td->td_ucred, inp->inp_socket) == 0)
635 inp_list[i++] = inp;
636 INP_UNLOCK(inp);
637 }
638 INP_INFO_RUNLOCK(&udbinfo);
639 n = i;
640
641 error = 0;
642 for (i = 0; i < n; i++) {
643 inp = inp_list[i];
644 INP_LOCK(inp);
645 if (inp->inp_gencnt <= gencnt) {
646 struct xinpcb xi;
647 bzero(&xi, sizeof(xi));
648 xi.xi_len = sizeof xi;
649 /* XXX should avoid extra copy */
650 bcopy(inp, &xi.xi_inp, sizeof *inp);
651 if (inp->inp_socket)
652 sotoxsocket(inp->inp_socket, &xi.xi_socket);
653 xi.xi_inp.inp_gencnt = inp->inp_gencnt;
654 INP_UNLOCK(inp);
655 error = SYSCTL_OUT(req, &xi, sizeof xi);
656 } else
657 INP_UNLOCK(inp);
658 }
659 if (!error) {
660 /*
661 * Give the user an updated idea of our state.
662 * If the generation differs from what we told
663 * her before, she knows that something happened
664 * while we were processing this request, and it
665 * might be necessary to retry.
666 */
667 INP_INFO_RLOCK(&udbinfo);
668 xig.xig_gen = udbinfo.ipi_gencnt;
669 xig.xig_sogen = so_gencnt;
670 xig.xig_count = udbinfo.ipi_count;
671 INP_INFO_RUNLOCK(&udbinfo);
672 error = SYSCTL_OUT(req, &xig, sizeof xig);
673 }
674 free(inp_list, M_TEMP);
675 return error;
676}
677
678SYSCTL_PROC(_net_inet_udp, UDPCTL_PCBLIST, pcblist, CTLFLAG_RD, 0, 0,
679 udp_pcblist, "S,xinpcb", "List of active UDP sockets");
680
681static int
682udp_getcred(SYSCTL_HANDLER_ARGS)
683{
684 struct xucred xuc;
685 struct sockaddr_in addrs[2];
686 struct inpcb *inp;
687 int error;
688
689 error = suser_cred(req->td->td_ucred, SUSER_ALLOWJAIL);
690 if (error)
691 return (error);
692 error = SYSCTL_IN(req, addrs, sizeof(addrs));
693 if (error)
694 return (error);
695 INP_INFO_RLOCK(&udbinfo);
696 inp = in_pcblookup_hash(&udbinfo, addrs[1].sin_addr, addrs[1].sin_port,
697 addrs[0].sin_addr, addrs[0].sin_port, 1, NULL);
698 if (inp == NULL || inp->inp_socket == NULL) {
699 error = ENOENT;
700 goto out;
701 }
702 error = cr_canseesocket(req->td->td_ucred, inp->inp_socket);
703 if (error)
704 goto out;
705 cru2x(inp->inp_socket->so_cred, &xuc);
706out:
707 INP_INFO_RUNLOCK(&udbinfo);
708 if (error == 0)
709 error = SYSCTL_OUT(req, &xuc, sizeof(struct xucred));
710 return (error);
711}
712
713SYSCTL_PROC(_net_inet_udp, OID_AUTO, getcred,
714 CTLTYPE_OPAQUE|CTLFLAG_RW|CTLFLAG_PRISON, 0, 0,
715 udp_getcred, "S,xucred", "Get the xucred of a UDP connection");
716
717static int
718udp_output(inp, m, addr, control, td)
719 register struct inpcb *inp;
720 struct mbuf *m;
721 struct sockaddr *addr;
722 struct mbuf *control;
723 struct thread *td;
724{
725 register struct udpiphdr *ui;
726 register int len = m->m_pkthdr.len;
727 struct in_addr faddr, laddr;
728 struct cmsghdr *cm;
729 struct sockaddr_in *sin, src;
730 int error = 0;
731 int ipflags;
732 u_short fport, lport;
733 int unlock_udbinfo;
734
735 /*
736 * udp_output() may need to temporarily bind or connect the current
737 * inpcb. As such, we don't know up front what inpcb locks we will
738 * need. Do any work to decide what is needed up front before
739 * acquiring locks.
740 */
741 if (len + sizeof(struct udpiphdr) > IP_MAXPACKET) {
742 if (control)
743 m_freem(control);
744 m_freem(m);
745 return EMSGSIZE;
746 }
747
748 src.sin_addr.s_addr = INADDR_ANY;
749 if (control != NULL) {
750 /*
751 * XXX: Currently, we assume all the optional information
752 * is stored in a single mbuf.
753 */
754 if (control->m_next) {
755 m_freem(control);
756 m_freem(m);
757 return EINVAL;
758 }
759 for (; control->m_len > 0;
760 control->m_data += CMSG_ALIGN(cm->cmsg_len),
761 control->m_len -= CMSG_ALIGN(cm->cmsg_len)) {
762 cm = mtod(control, struct cmsghdr *);
763 if (control->m_len < sizeof(*cm) || cm->cmsg_len == 0 ||
764 cm->cmsg_len > control->m_len) {
765 error = EINVAL;
766 break;
767 }
768 if (cm->cmsg_level != IPPROTO_IP)
769 continue;
770
771 switch (cm->cmsg_type) {
772 case IP_SENDSRCADDR:
773 if (cm->cmsg_len !=
774 CMSG_LEN(sizeof(struct in_addr))) {
775 error = EINVAL;
776 break;
777 }
778 bzero(&src, sizeof(src));
779 src.sin_family = AF_INET;
780 src.sin_len = sizeof(src);
781 src.sin_port = inp->inp_lport;
782 src.sin_addr = *(struct in_addr *)CMSG_DATA(cm);
783 break;
784 default:
785 error = ENOPROTOOPT;
786 break;
787 }
788 if (error)
789 break;
790 }
791 m_freem(control);
792 }
793 if (error) {
794 m_freem(m);
795 return error;
796 }
797
798 if (src.sin_addr.s_addr != INADDR_ANY ||
799 addr != NULL) {
800 INP_INFO_WLOCK(&udbinfo);
801 unlock_udbinfo = 1;
802 } else
803 unlock_udbinfo = 0;
804 INP_LOCK(inp);
805
806#ifdef MAC
807 mac_create_mbuf_from_inpcb(inp, m);
808#endif
809
810 laddr = inp->inp_laddr;
811 lport = inp->inp_lport;
812 if (src.sin_addr.s_addr != INADDR_ANY) {
813 if (lport == 0) {
814 error = EINVAL;
815 goto release;
816 }
817 error = in_pcbbind_setup(inp, (struct sockaddr *)&src,
818 &laddr.s_addr, &lport, td->td_ucred);
819 if (error)
820 goto release;
821 }
822
823 if (addr) {
824 sin = (struct sockaddr_in *)addr;
825 if (jailed(td->td_ucred))
826 prison_remote_ip(td->td_ucred, 0, &sin->sin_addr.s_addr);
827 if (inp->inp_faddr.s_addr != INADDR_ANY) {
828 error = EISCONN;
829 goto release;
830 }
831 error = in_pcbconnect_setup(inp, addr, &laddr.s_addr, &lport,
832 &faddr.s_addr, &fport, NULL, td->td_ucred);
833 if (error)
834 goto release;
835
836 /* Commit the local port if newly assigned. */
837 if (inp->inp_laddr.s_addr == INADDR_ANY &&
838 inp->inp_lport == 0) {
839 /*
840 * Remember addr if jailed, to prevent rebinding.
841 */
842 if (jailed(td->td_ucred))
843 inp->inp_laddr = laddr;
844 inp->inp_lport = lport;
845 if (in_pcbinshash(inp) != 0) {
846 inp->inp_lport = 0;
847 error = EAGAIN;
848 goto release;
849 }
850 inp->inp_flags |= INP_ANONPORT;
851 }
852 } else {
853 faddr = inp->inp_faddr;
854 fport = inp->inp_fport;
855 if (faddr.s_addr == INADDR_ANY) {
856 error = ENOTCONN;
857 goto release;
858 }
859 }
860
861 /*
862 * Calculate data length and get a mbuf for UDP, IP, and possible
863 * link-layer headers. Immediate slide the data pointer back forward
864 * since we won't use that space at this layer.
865 */
866 M_PREPEND(m, sizeof(struct udpiphdr) + max_linkhdr, M_DONTWAIT);
867 if (m == NULL) {
868 error = ENOBUFS;
869 goto release;
870 }
871 m->m_data += max_linkhdr;
872 m->m_len -= max_linkhdr;
873 m->m_pkthdr.len -= max_linkhdr;
874
875 /*
876 * Fill in mbuf with extended UDP header
877 * and addresses and length put into network format.
878 */
879 ui = mtod(m, struct udpiphdr *);
880 bzero(ui->ui_x1, sizeof(ui->ui_x1)); /* XXX still needed? */
881 ui->ui_pr = IPPROTO_UDP;
882 ui->ui_src = laddr;
883 ui->ui_dst = faddr;
884 ui->ui_sport = lport;
885 ui->ui_dport = fport;
886 ui->ui_ulen = htons((u_short)len + sizeof(struct udphdr));
887
888 /*
889 * Set the Don't Fragment bit in the IP header.
890 */
891 if (inp->inp_flags & INP_DONTFRAG) {
892 struct ip *ip;
893 ip = (struct ip *)&ui->ui_i;
894 ip->ip_off |= IP_DF;
895 }
896
897 ipflags = 0;
898 if (inp->inp_socket->so_options & SO_DONTROUTE)
899 ipflags |= IP_ROUTETOIF;
900 if (inp->inp_socket->so_options & SO_BROADCAST)
901 ipflags |= IP_ALLOWBROADCAST;
902 if (inp->inp_flags & INP_ONESBCAST)
903 ipflags |= IP_SENDONES;
904
905 /*
906 * Set up checksum and output datagram.
907 */
908 if (udpcksum) {
909 if (inp->inp_flags & INP_ONESBCAST)
910 faddr.s_addr = INADDR_BROADCAST;
911 ui->ui_sum = in_pseudo(ui->ui_src.s_addr, faddr.s_addr,
912 htons((u_short)len + sizeof(struct udphdr) + IPPROTO_UDP));
913 m->m_pkthdr.csum_flags = CSUM_UDP;
914 m->m_pkthdr.csum_data = offsetof(struct udphdr, uh_sum);
915 } else {
916 ui->ui_sum = 0;
917 }
918 ((struct ip *)ui)->ip_len = sizeof (struct udpiphdr) + len;
919 ((struct ip *)ui)->ip_ttl = inp->inp_ip_ttl; /* XXX */
920 ((struct ip *)ui)->ip_tos = inp->inp_ip_tos; /* XXX */
921 udpstat.udps_opackets++;
922
923 if (unlock_udbinfo)
924 INP_INFO_WUNLOCK(&udbinfo);
925 error = ip_output(m, inp->inp_options, NULL, ipflags,
926 inp->inp_moptions, inp);
927 INP_UNLOCK(inp);
928 return (error);
929
930release:
931 INP_UNLOCK(inp);
932 if (unlock_udbinfo)
933 INP_INFO_WUNLOCK(&udbinfo);
934 m_freem(m);
935 return (error);
936}
937
938u_long udp_sendspace = 9216; /* really max datagram size */
939 /* 40 1K datagrams */
940SYSCTL_ULONG(_net_inet_udp, UDPCTL_MAXDGRAM, maxdgram, CTLFLAG_RW,
941 &udp_sendspace, 0, "Maximum outgoing UDP datagram size");
942
943u_long udp_recvspace = 40 * (1024 +
944#ifdef INET6
945 sizeof(struct sockaddr_in6)
946#else
947 sizeof(struct sockaddr_in)
948#endif
949 );
950SYSCTL_ULONG(_net_inet_udp, UDPCTL_RECVSPACE, recvspace, CTLFLAG_RW,
951 &udp_recvspace, 0, "Maximum space for incoming UDP datagrams");
952
953static void
954udp_abort(struct socket *so)
955{
956 struct inpcb *inp;
957
958 inp = sotoinpcb(so);
959 KASSERT(inp != NULL, ("udp_abort: inp == NULL"));
960 INP_INFO_WLOCK(&udbinfo);
961 INP_LOCK(inp);
962 if (inp->inp_faddr.s_addr != INADDR_ANY) {
963 in_pcbdisconnect(inp);
964 inp->inp_laddr.s_addr = INADDR_ANY;
965 soisdisconnected(so);
966 }
967 INP_UNLOCK(inp);
968 INP_INFO_WUNLOCK(&udbinfo);
969}
970
971static int
972udp_attach(struct socket *so, int proto, struct thread *td)
973{
974 struct inpcb *inp;
975 int error;
976
977 inp = sotoinpcb(so);
978 KASSERT(inp == NULL, ("udp_attach: inp != NULL"));
979 error = soreserve(so, udp_sendspace, udp_recvspace);
980 if (error)
981 return error;
982 INP_INFO_WLOCK(&udbinfo);
983 error = in_pcballoc(so, &udbinfo);
984 if (error) {
985 INP_INFO_WUNLOCK(&udbinfo);
986 return error;
987 }
988
989 inp = (struct inpcb *)so->so_pcb;
990 INP_INFO_WUNLOCK(&udbinfo);
991 inp->inp_vflag |= INP_IPV4;
992 inp->inp_ip_ttl = ip_defttl;
993 INP_UNLOCK(inp);
994 return 0;
995}
996
997static int
998udp_bind(struct socket *so, struct sockaddr *nam, struct thread *td)
999{
1000 struct inpcb *inp;
1001 int error;
1002
1003 inp = sotoinpcb(so);
1004 KASSERT(inp != NULL, ("udp_bind: inp == NULL"));
1005 INP_INFO_WLOCK(&udbinfo);
1006 INP_LOCK(inp);
1007 error = in_pcbbind(inp, nam, td->td_ucred);
1008 INP_UNLOCK(inp);
1009 INP_INFO_WUNLOCK(&udbinfo);
1010 return error;
1011}
1012
1013static void
1014udp_close(struct socket *so)
1015{
1016 struct inpcb *inp;
1017
1018 inp = sotoinpcb(so);
1019 KASSERT(inp != NULL, ("udp_close: inp == NULL"));
1020 INP_INFO_WLOCK(&udbinfo);
1021 INP_LOCK(inp);
1022 if (inp->inp_faddr.s_addr != INADDR_ANY) {
1023 in_pcbdisconnect(inp);
1024 inp->inp_laddr.s_addr = INADDR_ANY;
1025 soisdisconnected(so);
1026 }
1027 INP_UNLOCK(inp);
1028 INP_INFO_WUNLOCK(&udbinfo);
1029}
1030
1031static int
1032udp_connect(struct socket *so, struct sockaddr *nam, struct thread *td)
1033{
1034 struct inpcb *inp;
1035 int error;
1036 struct sockaddr_in *sin;
1037
1038 inp = sotoinpcb(so);
1039 KASSERT(inp != NULL, ("udp_connect: inp == NULL"));
1040 INP_INFO_WLOCK(&udbinfo);
1041 INP_LOCK(inp);
1042 if (inp->inp_faddr.s_addr != INADDR_ANY) {
1043 INP_UNLOCK(inp);
1044 INP_INFO_WUNLOCK(&udbinfo);
1045 return EISCONN;
1046 }
1047 sin = (struct sockaddr_in *)nam;
1048 if (jailed(td->td_ucred))
1049 prison_remote_ip(td->td_ucred, 0, &sin->sin_addr.s_addr);
1050 error = in_pcbconnect(inp, nam, td->td_ucred);
1051 if (error == 0)
1052 soisconnected(so);
1053 INP_UNLOCK(inp);
1054 INP_INFO_WUNLOCK(&udbinfo);
1055 return error;
1056}
1057
1058static void
1059udp_detach(struct socket *so)
1060{
1061 struct inpcb *inp;
1062
1063 inp = sotoinpcb(so);
1064 KASSERT(inp != NULL, ("udp_detach: inp == NULL"));
1065 KASSERT(inp->inp_faddr.s_addr == INADDR_ANY,
1066 ("udp_detach: not disconnected"));
1067 INP_INFO_WLOCK(&udbinfo);
1068 INP_LOCK(inp);
1069 in_pcbdetach(inp);
1070 in_pcbfree(inp);
1071 INP_INFO_WUNLOCK(&udbinfo);
1072}
1073
1074static int
1075udp_disconnect(struct socket *so)
1076{
1077 struct inpcb *inp;
1078
1079 inp = sotoinpcb(so);
1080 KASSERT(inp != NULL, ("udp_disconnect: inp == NULL"));
1081 INP_INFO_WLOCK(&udbinfo);
1082 INP_LOCK(inp);
1083 if (inp->inp_faddr.s_addr == INADDR_ANY) {
1084 INP_INFO_WUNLOCK(&udbinfo);
1085 INP_UNLOCK(inp);
1086 return ENOTCONN;
1087 }
1088
1089 in_pcbdisconnect(inp);
1090 inp->inp_laddr.s_addr = INADDR_ANY;
1091 SOCK_LOCK(so);
1092 so->so_state &= ~SS_ISCONNECTED; /* XXX */
1093 SOCK_UNLOCK(so);
1094 INP_UNLOCK(inp);
1095 INP_INFO_WUNLOCK(&udbinfo);
1096 return 0;
1097}
1098
1099static int
1100udp_send(struct socket *so, int flags, struct mbuf *m, struct sockaddr *addr,
1101 struct mbuf *control, struct thread *td)
1102{
1103 struct inpcb *inp;
1104
1105 inp = sotoinpcb(so);
1106 KASSERT(inp != NULL, ("udp_send: inp == NULL"));
1107 return udp_output(inp, m, addr, control, td);
1108}
1109
1110int
1111udp_shutdown(struct socket *so)
1112{
1113 struct inpcb *inp;
1114
1115 inp = sotoinpcb(so);
1116 KASSERT(inp != NULL, ("udp_shutdown: inp == NULL"));
1117 INP_LOCK(inp);
1118 socantsendmore(so);
1119 INP_UNLOCK(inp);
1120 return 0;
1121}
1122
1123/*
1124 * This is the wrapper function for in_setsockaddr. We just pass down
1125 * the pcbinfo for in_setsockaddr to lock. We don't want to do the locking
1126 * here because in_setsockaddr will call malloc and might block.
1127 */
1128static int
1129udp_sockaddr(struct socket *so, struct sockaddr **nam)
1130{
1131 return (in_setsockaddr(so, nam, &udbinfo));
1132}
1133
1134/*
1135 * This is the wrapper function for in_setpeeraddr. We just pass down
1136 * the pcbinfo for in_setpeeraddr to lock.
1137 */
1138static int
1139udp_peeraddr(struct socket *so, struct sockaddr **nam)
1140{
1141 return (in_setpeeraddr(so, nam, &udbinfo));
1142}
1143
1144struct pr_usrreqs udp_usrreqs = {
1145 .pru_abort = udp_abort,
1146 .pru_attach = udp_attach,
1147 .pru_bind = udp_bind,
1148 .pru_connect = udp_connect,
1149 .pru_control = in_control,
1150 .pru_detach = udp_detach,
1151 .pru_disconnect = udp_disconnect,
1152 .pru_peeraddr = udp_peeraddr,
1153 .pru_send = udp_send,
1154 .pru_sosend = sosend_dgram,
1155 .pru_shutdown = udp_shutdown,
1156 .pru_sockaddr = udp_sockaddr,
1157 .pru_sosetlabel = in_pcbsosetlabel,
1158 .pru_close = udp_close,
1159};
91/*
92 * UDP protocol implementation.
93 * Per RFC 768, August, 1980.
94 */
95#ifndef COMPAT_42
96static int udpcksum = 1;
97#else
98static int udpcksum = 0; /* XXX */
99#endif
100SYSCTL_INT(_net_inet_udp, UDPCTL_CHECKSUM, checksum, CTLFLAG_RW,
101 &udpcksum, 0, "");
102
103int log_in_vain = 0;
104SYSCTL_INT(_net_inet_udp, OID_AUTO, log_in_vain, CTLFLAG_RW,
105 &log_in_vain, 0, "Log all incoming UDP packets");
106
107static int blackhole = 0;
108SYSCTL_INT(_net_inet_udp, OID_AUTO, blackhole, CTLFLAG_RW,
109 &blackhole, 0, "Do not send port unreachables for refused connects");
110
111static int strict_mcast_mship = 0;
112SYSCTL_INT(_net_inet_udp, OID_AUTO, strict_mcast_mship, CTLFLAG_RW,
113 &strict_mcast_mship, 0, "Only send multicast to member sockets");
114
115struct inpcbhead udb; /* from udp_var.h */
116#define udb6 udb /* for KAME src sync over BSD*'s */
117struct inpcbinfo udbinfo;
118
119#ifndef UDBHASHSIZE
120#define UDBHASHSIZE 16
121#endif
122
123struct udpstat udpstat; /* from udp_var.h */
124SYSCTL_STRUCT(_net_inet_udp, UDPCTL_STATS, stats, CTLFLAG_RW,
125 &udpstat, udpstat, "UDP statistics (struct udpstat, netinet/udp_var.h)");
126
127static void udp_append(struct inpcb *last, struct ip *ip, struct mbuf *n,
128 int off, struct sockaddr_in *udp_in);
129
130static void udp_detach(struct socket *so);
131static int udp_output(struct inpcb *, struct mbuf *, struct sockaddr *,
132 struct mbuf *, struct thread *);
133
134static void
135udp_zone_change(void *tag)
136{
137
138 uma_zone_set_max(udbinfo.ipi_zone, maxsockets);
139}
140
141static int
142udp_inpcb_init(void *mem, int size, int flags)
143{
144 struct inpcb *inp = (struct inpcb *) mem;
145 INP_LOCK_INIT(inp, "inp", "udpinp");
146 return (0);
147}
148
149void
150udp_init()
151{
152 INP_INFO_LOCK_INIT(&udbinfo, "udp");
153 LIST_INIT(&udb);
154 udbinfo.listhead = &udb;
155 udbinfo.hashbase = hashinit(UDBHASHSIZE, M_PCB, &udbinfo.hashmask);
156 udbinfo.porthashbase = hashinit(UDBHASHSIZE, M_PCB,
157 &udbinfo.porthashmask);
158 udbinfo.ipi_zone = uma_zcreate("udpcb", sizeof(struct inpcb), NULL,
159 NULL, udp_inpcb_init, NULL, UMA_ALIGN_PTR, UMA_ZONE_NOFREE);
160 uma_zone_set_max(udbinfo.ipi_zone, maxsockets);
161 EVENTHANDLER_REGISTER(maxsockets_change, udp_zone_change, NULL,
162 EVENTHANDLER_PRI_ANY);
163}
164
165void
166udp_input(m, off)
167 register struct mbuf *m;
168 int off;
169{
170 int iphlen = off;
171 register struct ip *ip;
172 register struct udphdr *uh;
173 register struct inpcb *inp;
174 int len;
175 struct ip save_ip;
176 struct sockaddr_in udp_in;
177#ifdef IPFIREWALL_FORWARD
178 struct m_tag *fwd_tag;
179#endif
180
181 udpstat.udps_ipackets++;
182
183 /*
184 * Strip IP options, if any; should skip this,
185 * make available to user, and use on returned packets,
186 * but we don't yet have a way to check the checksum
187 * with options still present.
188 */
189 if (iphlen > sizeof (struct ip)) {
190 ip_stripoptions(m, (struct mbuf *)0);
191 iphlen = sizeof(struct ip);
192 }
193
194 /*
195 * Get IP and UDP header together in first mbuf.
196 */
197 ip = mtod(m, struct ip *);
198 if (m->m_len < iphlen + sizeof(struct udphdr)) {
199 if ((m = m_pullup(m, iphlen + sizeof(struct udphdr))) == 0) {
200 udpstat.udps_hdrops++;
201 return;
202 }
203 ip = mtod(m, struct ip *);
204 }
205 uh = (struct udphdr *)((caddr_t)ip + iphlen);
206
207 /* destination port of 0 is illegal, based on RFC768. */
208 if (uh->uh_dport == 0)
209 goto badunlocked;
210
211 /*
212 * Construct sockaddr format source address.
213 * Stuff source address and datagram in user buffer.
214 */
215 bzero(&udp_in, sizeof(udp_in));
216 udp_in.sin_len = sizeof(udp_in);
217 udp_in.sin_family = AF_INET;
218 udp_in.sin_port = uh->uh_sport;
219 udp_in.sin_addr = ip->ip_src;
220
221 /*
222 * Make mbuf data length reflect UDP length.
223 * If not enough data to reflect UDP length, drop.
224 */
225 len = ntohs((u_short)uh->uh_ulen);
226 if (ip->ip_len != len) {
227 if (len > ip->ip_len || len < sizeof(struct udphdr)) {
228 udpstat.udps_badlen++;
229 goto badunlocked;
230 }
231 m_adj(m, len - ip->ip_len);
232 /* ip->ip_len = len; */
233 }
234 /*
235 * Save a copy of the IP header in case we want restore it
236 * for sending an ICMP error message in response.
237 */
238 if (!blackhole)
239 save_ip = *ip;
240
241 /*
242 * Checksum extended UDP header and data.
243 */
244 if (uh->uh_sum) {
245 if (m->m_pkthdr.csum_flags & CSUM_DATA_VALID) {
246 if (m->m_pkthdr.csum_flags & CSUM_PSEUDO_HDR)
247 uh->uh_sum = m->m_pkthdr.csum_data;
248 else
249 uh->uh_sum = in_pseudo(ip->ip_src.s_addr,
250 ip->ip_dst.s_addr, htonl((u_short)len +
251 m->m_pkthdr.csum_data + IPPROTO_UDP));
252 uh->uh_sum ^= 0xffff;
253 } else {
254 char b[9];
255 bcopy(((struct ipovly *)ip)->ih_x1, b, 9);
256 bzero(((struct ipovly *)ip)->ih_x1, 9);
257 ((struct ipovly *)ip)->ih_len = uh->uh_ulen;
258 uh->uh_sum = in_cksum(m, len + sizeof (struct ip));
259 bcopy(b, ((struct ipovly *)ip)->ih_x1, 9);
260 }
261 if (uh->uh_sum) {
262 udpstat.udps_badsum++;
263 m_freem(m);
264 return;
265 }
266 } else
267 udpstat.udps_nosum++;
268
269#ifdef IPFIREWALL_FORWARD
270 /* Grab info from PACKET_TAG_IPFORWARD tag prepended to the chain. */
271 fwd_tag = m_tag_find(m, PACKET_TAG_IPFORWARD, NULL);
272
273 if (fwd_tag != NULL) {
274 struct sockaddr_in *next_hop;
275
276 /* Do the hack. */
277 next_hop = (struct sockaddr_in *)(fwd_tag + 1);
278 ip->ip_dst = next_hop->sin_addr;
279 uh->uh_dport = ntohs(next_hop->sin_port);
280 /* Remove the tag from the packet. We don't need it anymore. */
281 m_tag_delete(m, fwd_tag);
282 }
283#endif
284
285 INP_INFO_RLOCK(&udbinfo);
286
287 if (IN_MULTICAST(ntohl(ip->ip_dst.s_addr)) ||
288 in_broadcast(ip->ip_dst, m->m_pkthdr.rcvif)) {
289 struct inpcb *last;
290 /*
291 * Deliver a multicast or broadcast datagram to *all* sockets
292 * for which the local and remote addresses and ports match
293 * those of the incoming datagram. This allows more than
294 * one process to receive multi/broadcasts on the same port.
295 * (This really ought to be done for unicast datagrams as
296 * well, but that would cause problems with existing
297 * applications that open both address-specific sockets and
298 * a wildcard socket listening to the same port -- they would
299 * end up receiving duplicates of every unicast datagram.
300 * Those applications open the multiple sockets to overcome an
301 * inadequacy of the UDP socket interface, but for backwards
302 * compatibility we avoid the problem here rather than
303 * fixing the interface. Maybe 4.5BSD will remedy this?)
304 */
305
306 /*
307 * Locate pcb(s) for datagram.
308 * (Algorithm copied from raw_intr().)
309 */
310 last = NULL;
311 LIST_FOREACH(inp, &udb, inp_list) {
312 if (inp->inp_lport != uh->uh_dport)
313 continue;
314#ifdef INET6
315 if ((inp->inp_vflag & INP_IPV4) == 0)
316 continue;
317#endif
318 if (inp->inp_laddr.s_addr != INADDR_ANY) {
319 if (inp->inp_laddr.s_addr != ip->ip_dst.s_addr)
320 continue;
321 }
322 if (inp->inp_faddr.s_addr != INADDR_ANY) {
323 if (inp->inp_faddr.s_addr !=
324 ip->ip_src.s_addr ||
325 inp->inp_fport != uh->uh_sport)
326 continue;
327 }
328 INP_LOCK(inp);
329
330 /*
331 * Check multicast packets to make sure they are only
332 * sent to sockets with multicast memberships for the
333 * packet's destination address and arrival interface
334 */
335#define MSHIP(_inp, n) ((_inp)->inp_moptions->imo_membership[(n)])
336#define NMSHIPS(_inp) ((_inp)->inp_moptions->imo_num_memberships)
337 if (strict_mcast_mship && inp->inp_moptions != NULL) {
338 int mship, foundmship = 0;
339
340 for (mship = 0; mship < NMSHIPS(inp); mship++) {
341 if (MSHIP(inp, mship)->inm_addr.s_addr
342 == ip->ip_dst.s_addr &&
343 MSHIP(inp, mship)->inm_ifp
344 == m->m_pkthdr.rcvif) {
345 foundmship = 1;
346 break;
347 }
348 }
349 if (foundmship == 0) {
350 INP_UNLOCK(inp);
351 continue;
352 }
353 }
354#undef NMSHIPS
355#undef MSHIP
356 if (last != NULL) {
357 struct mbuf *n;
358
359 n = m_copy(m, 0, M_COPYALL);
360 if (n != NULL)
361 udp_append(last, ip, n,
362 iphlen +
363 sizeof(struct udphdr),
364 &udp_in);
365 INP_UNLOCK(last);
366 }
367 last = inp;
368 /*
369 * Don't look for additional matches if this one does
370 * not have either the SO_REUSEPORT or SO_REUSEADDR
371 * socket options set. This heuristic avoids searching
372 * through all pcbs in the common case of a non-shared
373 * port. It assumes that an application will never
374 * clear these options after setting them.
375 */
376 if ((last->inp_socket->so_options&(SO_REUSEPORT|SO_REUSEADDR)) == 0)
377 break;
378 }
379
380 if (last == NULL) {
381 /*
382 * No matching pcb found; discard datagram.
383 * (No need to send an ICMP Port Unreachable
384 * for a broadcast or multicast datgram.)
385 */
386 udpstat.udps_noportbcast++;
387 goto badheadlocked;
388 }
389 udp_append(last, ip, m, iphlen + sizeof(struct udphdr),
390 &udp_in);
391 INP_UNLOCK(last);
392 INP_INFO_RUNLOCK(&udbinfo);
393 return;
394 }
395 /*
396 * Locate pcb for datagram.
397 */
398 inp = in_pcblookup_hash(&udbinfo, ip->ip_src, uh->uh_sport,
399 ip->ip_dst, uh->uh_dport, 1, m->m_pkthdr.rcvif);
400 if (inp == NULL) {
401 if (log_in_vain) {
402 char buf[4*sizeof "123"];
403
404 strcpy(buf, inet_ntoa(ip->ip_dst));
405 log(LOG_INFO,
406 "Connection attempt to UDP %s:%d from %s:%d\n",
407 buf, ntohs(uh->uh_dport), inet_ntoa(ip->ip_src),
408 ntohs(uh->uh_sport));
409 }
410 udpstat.udps_noport++;
411 if (m->m_flags & (M_BCAST | M_MCAST)) {
412 udpstat.udps_noportbcast++;
413 goto badheadlocked;
414 }
415 if (blackhole)
416 goto badheadlocked;
417 if (badport_bandlim(BANDLIM_ICMP_UNREACH) < 0)
418 goto badheadlocked;
419 *ip = save_ip;
420 ip->ip_len += iphlen;
421 icmp_error(m, ICMP_UNREACH, ICMP_UNREACH_PORT, 0, 0);
422 INP_INFO_RUNLOCK(&udbinfo);
423 return;
424 }
425 INP_LOCK(inp);
426 /* Check the minimum TTL for socket. */
427 if (inp->inp_ip_minttl && inp->inp_ip_minttl > ip->ip_ttl)
428 goto badheadlocked;
429 udp_append(inp, ip, m, iphlen + sizeof(struct udphdr), &udp_in);
430 INP_UNLOCK(inp);
431 INP_INFO_RUNLOCK(&udbinfo);
432 return;
433
434badheadlocked:
435 if (inp)
436 INP_UNLOCK(inp);
437 INP_INFO_RUNLOCK(&udbinfo);
438badunlocked:
439 m_freem(m);
440 return;
441}
442
443/*
444 * Subroutine of udp_input(), which appends the provided mbuf chain to the
445 * passed pcb/socket. The caller must provide a sockaddr_in via udp_in that
446 * contains the source address. If the socket ends up being an IPv6 socket,
447 * udp_append() will convert to a sockaddr_in6 before passing the address
448 * into the socket code.
449 */
450static void
451udp_append(inp, ip, n, off, udp_in)
452 struct inpcb *inp;
453 struct ip *ip;
454 struct mbuf *n;
455 int off;
456 struct sockaddr_in *udp_in;
457{
458 struct sockaddr *append_sa;
459 struct socket *so;
460 struct mbuf *opts = 0;
461#ifdef INET6
462 struct sockaddr_in6 udp_in6;
463#endif
464
465 INP_LOCK_ASSERT(inp);
466
467#if defined(IPSEC) || defined(FAST_IPSEC)
468 /* check AH/ESP integrity. */
469 if (ipsec4_in_reject(n, inp)) {
470#ifdef IPSEC
471 ipsecstat.in_polvio++;
472#endif /*IPSEC*/
473 m_freem(n);
474 return;
475 }
476#endif /*IPSEC || FAST_IPSEC*/
477#ifdef MAC
478 if (mac_check_inpcb_deliver(inp, n) != 0) {
479 m_freem(n);
480 return;
481 }
482#endif
483 if (inp->inp_flags & INP_CONTROLOPTS ||
484 inp->inp_socket->so_options & (SO_TIMESTAMP | SO_BINTIME)) {
485#ifdef INET6
486 if (inp->inp_vflag & INP_IPV6) {
487 int savedflags;
488
489 savedflags = inp->inp_flags;
490 inp->inp_flags &= ~INP_UNMAPPABLEOPTS;
491 ip6_savecontrol(inp, n, &opts);
492 inp->inp_flags = savedflags;
493 } else
494#endif
495 ip_savecontrol(inp, &opts, ip, n);
496 }
497#ifdef INET6
498 if (inp->inp_vflag & INP_IPV6) {
499 bzero(&udp_in6, sizeof(udp_in6));
500 udp_in6.sin6_len = sizeof(udp_in6);
501 udp_in6.sin6_family = AF_INET6;
502 in6_sin_2_v4mapsin6(udp_in, &udp_in6);
503 append_sa = (struct sockaddr *)&udp_in6;
504 } else
505#endif
506 append_sa = (struct sockaddr *)udp_in;
507 m_adj(n, off);
508
509 so = inp->inp_socket;
510 SOCKBUF_LOCK(&so->so_rcv);
511 if (sbappendaddr_locked(&so->so_rcv, append_sa, n, opts) == 0) {
512 m_freem(n);
513 if (opts)
514 m_freem(opts);
515 udpstat.udps_fullsock++;
516 SOCKBUF_UNLOCK(&so->so_rcv);
517 } else
518 sorwakeup_locked(so);
519}
520
521/*
522 * Notify a udp user of an asynchronous error;
523 * just wake up so that he can collect error status.
524 */
525struct inpcb *
526udp_notify(inp, errno)
527 register struct inpcb *inp;
528 int errno;
529{
530 inp->inp_socket->so_error = errno;
531 sorwakeup(inp->inp_socket);
532 sowwakeup(inp->inp_socket);
533 return inp;
534}
535
536void
537udp_ctlinput(cmd, sa, vip)
538 int cmd;
539 struct sockaddr *sa;
540 void *vip;
541{
542 struct ip *ip = vip;
543 struct udphdr *uh;
544 struct inpcb *(*notify)(struct inpcb *, int) = udp_notify;
545 struct in_addr faddr;
546 struct inpcb *inp;
547
548 faddr = ((struct sockaddr_in *)sa)->sin_addr;
549 if (sa->sa_family != AF_INET || faddr.s_addr == INADDR_ANY)
550 return;
551
552 /*
553 * Redirects don't need to be handled up here.
554 */
555 if (PRC_IS_REDIRECT(cmd))
556 return;
557 /*
558 * Hostdead is ugly because it goes linearly through all PCBs.
559 * XXX: We never get this from ICMP, otherwise it makes an
560 * excellent DoS attack on machines with many connections.
561 */
562 if (cmd == PRC_HOSTDEAD)
563 ip = 0;
564 else if ((unsigned)cmd >= PRC_NCMDS || inetctlerrmap[cmd] == 0)
565 return;
566 if (ip) {
567 uh = (struct udphdr *)((caddr_t)ip + (ip->ip_hl << 2));
568 INP_INFO_RLOCK(&udbinfo);
569 inp = in_pcblookup_hash(&udbinfo, faddr, uh->uh_dport,
570 ip->ip_src, uh->uh_sport, 0, NULL);
571 if (inp != NULL) {
572 INP_LOCK(inp);
573 if (inp->inp_socket != NULL) {
574 (*notify)(inp, inetctlerrmap[cmd]);
575 }
576 INP_UNLOCK(inp);
577 }
578 INP_INFO_RUNLOCK(&udbinfo);
579 } else
580 in_pcbnotifyall(&udbinfo, faddr, inetctlerrmap[cmd], notify);
581}
582
583static int
584udp_pcblist(SYSCTL_HANDLER_ARGS)
585{
586 int error, i, n;
587 struct inpcb *inp, **inp_list;
588 inp_gen_t gencnt;
589 struct xinpgen xig;
590
591 /*
592 * The process of preparing the TCB list is too time-consuming and
593 * resource-intensive to repeat twice on every request.
594 */
595 if (req->oldptr == 0) {
596 n = udbinfo.ipi_count;
597 req->oldidx = 2 * (sizeof xig)
598 + (n + n/8) * sizeof(struct xinpcb);
599 return 0;
600 }
601
602 if (req->newptr != 0)
603 return EPERM;
604
605 /*
606 * OK, now we're committed to doing something.
607 */
608 INP_INFO_RLOCK(&udbinfo);
609 gencnt = udbinfo.ipi_gencnt;
610 n = udbinfo.ipi_count;
611 INP_INFO_RUNLOCK(&udbinfo);
612
613 error = sysctl_wire_old_buffer(req, 2 * (sizeof xig)
614 + n * sizeof(struct xinpcb));
615 if (error != 0)
616 return (error);
617
618 xig.xig_len = sizeof xig;
619 xig.xig_count = n;
620 xig.xig_gen = gencnt;
621 xig.xig_sogen = so_gencnt;
622 error = SYSCTL_OUT(req, &xig, sizeof xig);
623 if (error)
624 return error;
625
626 inp_list = malloc(n * sizeof *inp_list, M_TEMP, M_WAITOK);
627 if (inp_list == 0)
628 return ENOMEM;
629
630 INP_INFO_RLOCK(&udbinfo);
631 for (inp = LIST_FIRST(udbinfo.listhead), i = 0; inp && i < n;
632 inp = LIST_NEXT(inp, inp_list)) {
633 INP_LOCK(inp);
634 if (inp->inp_gencnt <= gencnt &&
635 cr_canseesocket(req->td->td_ucred, inp->inp_socket) == 0)
636 inp_list[i++] = inp;
637 INP_UNLOCK(inp);
638 }
639 INP_INFO_RUNLOCK(&udbinfo);
640 n = i;
641
642 error = 0;
643 for (i = 0; i < n; i++) {
644 inp = inp_list[i];
645 INP_LOCK(inp);
646 if (inp->inp_gencnt <= gencnt) {
647 struct xinpcb xi;
648 bzero(&xi, sizeof(xi));
649 xi.xi_len = sizeof xi;
650 /* XXX should avoid extra copy */
651 bcopy(inp, &xi.xi_inp, sizeof *inp);
652 if (inp->inp_socket)
653 sotoxsocket(inp->inp_socket, &xi.xi_socket);
654 xi.xi_inp.inp_gencnt = inp->inp_gencnt;
655 INP_UNLOCK(inp);
656 error = SYSCTL_OUT(req, &xi, sizeof xi);
657 } else
658 INP_UNLOCK(inp);
659 }
660 if (!error) {
661 /*
662 * Give the user an updated idea of our state.
663 * If the generation differs from what we told
664 * her before, she knows that something happened
665 * while we were processing this request, and it
666 * might be necessary to retry.
667 */
668 INP_INFO_RLOCK(&udbinfo);
669 xig.xig_gen = udbinfo.ipi_gencnt;
670 xig.xig_sogen = so_gencnt;
671 xig.xig_count = udbinfo.ipi_count;
672 INP_INFO_RUNLOCK(&udbinfo);
673 error = SYSCTL_OUT(req, &xig, sizeof xig);
674 }
675 free(inp_list, M_TEMP);
676 return error;
677}
678
679SYSCTL_PROC(_net_inet_udp, UDPCTL_PCBLIST, pcblist, CTLFLAG_RD, 0, 0,
680 udp_pcblist, "S,xinpcb", "List of active UDP sockets");
681
682static int
683udp_getcred(SYSCTL_HANDLER_ARGS)
684{
685 struct xucred xuc;
686 struct sockaddr_in addrs[2];
687 struct inpcb *inp;
688 int error;
689
690 error = suser_cred(req->td->td_ucred, SUSER_ALLOWJAIL);
691 if (error)
692 return (error);
693 error = SYSCTL_IN(req, addrs, sizeof(addrs));
694 if (error)
695 return (error);
696 INP_INFO_RLOCK(&udbinfo);
697 inp = in_pcblookup_hash(&udbinfo, addrs[1].sin_addr, addrs[1].sin_port,
698 addrs[0].sin_addr, addrs[0].sin_port, 1, NULL);
699 if (inp == NULL || inp->inp_socket == NULL) {
700 error = ENOENT;
701 goto out;
702 }
703 error = cr_canseesocket(req->td->td_ucred, inp->inp_socket);
704 if (error)
705 goto out;
706 cru2x(inp->inp_socket->so_cred, &xuc);
707out:
708 INP_INFO_RUNLOCK(&udbinfo);
709 if (error == 0)
710 error = SYSCTL_OUT(req, &xuc, sizeof(struct xucred));
711 return (error);
712}
713
714SYSCTL_PROC(_net_inet_udp, OID_AUTO, getcred,
715 CTLTYPE_OPAQUE|CTLFLAG_RW|CTLFLAG_PRISON, 0, 0,
716 udp_getcred, "S,xucred", "Get the xucred of a UDP connection");
717
718static int
719udp_output(inp, m, addr, control, td)
720 register struct inpcb *inp;
721 struct mbuf *m;
722 struct sockaddr *addr;
723 struct mbuf *control;
724 struct thread *td;
725{
726 register struct udpiphdr *ui;
727 register int len = m->m_pkthdr.len;
728 struct in_addr faddr, laddr;
729 struct cmsghdr *cm;
730 struct sockaddr_in *sin, src;
731 int error = 0;
732 int ipflags;
733 u_short fport, lport;
734 int unlock_udbinfo;
735
736 /*
737 * udp_output() may need to temporarily bind or connect the current
738 * inpcb. As such, we don't know up front what inpcb locks we will
739 * need. Do any work to decide what is needed up front before
740 * acquiring locks.
741 */
742 if (len + sizeof(struct udpiphdr) > IP_MAXPACKET) {
743 if (control)
744 m_freem(control);
745 m_freem(m);
746 return EMSGSIZE;
747 }
748
749 src.sin_addr.s_addr = INADDR_ANY;
750 if (control != NULL) {
751 /*
752 * XXX: Currently, we assume all the optional information
753 * is stored in a single mbuf.
754 */
755 if (control->m_next) {
756 m_freem(control);
757 m_freem(m);
758 return EINVAL;
759 }
760 for (; control->m_len > 0;
761 control->m_data += CMSG_ALIGN(cm->cmsg_len),
762 control->m_len -= CMSG_ALIGN(cm->cmsg_len)) {
763 cm = mtod(control, struct cmsghdr *);
764 if (control->m_len < sizeof(*cm) || cm->cmsg_len == 0 ||
765 cm->cmsg_len > control->m_len) {
766 error = EINVAL;
767 break;
768 }
769 if (cm->cmsg_level != IPPROTO_IP)
770 continue;
771
772 switch (cm->cmsg_type) {
773 case IP_SENDSRCADDR:
774 if (cm->cmsg_len !=
775 CMSG_LEN(sizeof(struct in_addr))) {
776 error = EINVAL;
777 break;
778 }
779 bzero(&src, sizeof(src));
780 src.sin_family = AF_INET;
781 src.sin_len = sizeof(src);
782 src.sin_port = inp->inp_lport;
783 src.sin_addr = *(struct in_addr *)CMSG_DATA(cm);
784 break;
785 default:
786 error = ENOPROTOOPT;
787 break;
788 }
789 if (error)
790 break;
791 }
792 m_freem(control);
793 }
794 if (error) {
795 m_freem(m);
796 return error;
797 }
798
799 if (src.sin_addr.s_addr != INADDR_ANY ||
800 addr != NULL) {
801 INP_INFO_WLOCK(&udbinfo);
802 unlock_udbinfo = 1;
803 } else
804 unlock_udbinfo = 0;
805 INP_LOCK(inp);
806
807#ifdef MAC
808 mac_create_mbuf_from_inpcb(inp, m);
809#endif
810
811 laddr = inp->inp_laddr;
812 lport = inp->inp_lport;
813 if (src.sin_addr.s_addr != INADDR_ANY) {
814 if (lport == 0) {
815 error = EINVAL;
816 goto release;
817 }
818 error = in_pcbbind_setup(inp, (struct sockaddr *)&src,
819 &laddr.s_addr, &lport, td->td_ucred);
820 if (error)
821 goto release;
822 }
823
824 if (addr) {
825 sin = (struct sockaddr_in *)addr;
826 if (jailed(td->td_ucred))
827 prison_remote_ip(td->td_ucred, 0, &sin->sin_addr.s_addr);
828 if (inp->inp_faddr.s_addr != INADDR_ANY) {
829 error = EISCONN;
830 goto release;
831 }
832 error = in_pcbconnect_setup(inp, addr, &laddr.s_addr, &lport,
833 &faddr.s_addr, &fport, NULL, td->td_ucred);
834 if (error)
835 goto release;
836
837 /* Commit the local port if newly assigned. */
838 if (inp->inp_laddr.s_addr == INADDR_ANY &&
839 inp->inp_lport == 0) {
840 /*
841 * Remember addr if jailed, to prevent rebinding.
842 */
843 if (jailed(td->td_ucred))
844 inp->inp_laddr = laddr;
845 inp->inp_lport = lport;
846 if (in_pcbinshash(inp) != 0) {
847 inp->inp_lport = 0;
848 error = EAGAIN;
849 goto release;
850 }
851 inp->inp_flags |= INP_ANONPORT;
852 }
853 } else {
854 faddr = inp->inp_faddr;
855 fport = inp->inp_fport;
856 if (faddr.s_addr == INADDR_ANY) {
857 error = ENOTCONN;
858 goto release;
859 }
860 }
861
862 /*
863 * Calculate data length and get a mbuf for UDP, IP, and possible
864 * link-layer headers. Immediate slide the data pointer back forward
865 * since we won't use that space at this layer.
866 */
867 M_PREPEND(m, sizeof(struct udpiphdr) + max_linkhdr, M_DONTWAIT);
868 if (m == NULL) {
869 error = ENOBUFS;
870 goto release;
871 }
872 m->m_data += max_linkhdr;
873 m->m_len -= max_linkhdr;
874 m->m_pkthdr.len -= max_linkhdr;
875
876 /*
877 * Fill in mbuf with extended UDP header
878 * and addresses and length put into network format.
879 */
880 ui = mtod(m, struct udpiphdr *);
881 bzero(ui->ui_x1, sizeof(ui->ui_x1)); /* XXX still needed? */
882 ui->ui_pr = IPPROTO_UDP;
883 ui->ui_src = laddr;
884 ui->ui_dst = faddr;
885 ui->ui_sport = lport;
886 ui->ui_dport = fport;
887 ui->ui_ulen = htons((u_short)len + sizeof(struct udphdr));
888
889 /*
890 * Set the Don't Fragment bit in the IP header.
891 */
892 if (inp->inp_flags & INP_DONTFRAG) {
893 struct ip *ip;
894 ip = (struct ip *)&ui->ui_i;
895 ip->ip_off |= IP_DF;
896 }
897
898 ipflags = 0;
899 if (inp->inp_socket->so_options & SO_DONTROUTE)
900 ipflags |= IP_ROUTETOIF;
901 if (inp->inp_socket->so_options & SO_BROADCAST)
902 ipflags |= IP_ALLOWBROADCAST;
903 if (inp->inp_flags & INP_ONESBCAST)
904 ipflags |= IP_SENDONES;
905
906 /*
907 * Set up checksum and output datagram.
908 */
909 if (udpcksum) {
910 if (inp->inp_flags & INP_ONESBCAST)
911 faddr.s_addr = INADDR_BROADCAST;
912 ui->ui_sum = in_pseudo(ui->ui_src.s_addr, faddr.s_addr,
913 htons((u_short)len + sizeof(struct udphdr) + IPPROTO_UDP));
914 m->m_pkthdr.csum_flags = CSUM_UDP;
915 m->m_pkthdr.csum_data = offsetof(struct udphdr, uh_sum);
916 } else {
917 ui->ui_sum = 0;
918 }
919 ((struct ip *)ui)->ip_len = sizeof (struct udpiphdr) + len;
920 ((struct ip *)ui)->ip_ttl = inp->inp_ip_ttl; /* XXX */
921 ((struct ip *)ui)->ip_tos = inp->inp_ip_tos; /* XXX */
922 udpstat.udps_opackets++;
923
924 if (unlock_udbinfo)
925 INP_INFO_WUNLOCK(&udbinfo);
926 error = ip_output(m, inp->inp_options, NULL, ipflags,
927 inp->inp_moptions, inp);
928 INP_UNLOCK(inp);
929 return (error);
930
931release:
932 INP_UNLOCK(inp);
933 if (unlock_udbinfo)
934 INP_INFO_WUNLOCK(&udbinfo);
935 m_freem(m);
936 return (error);
937}
938
939u_long udp_sendspace = 9216; /* really max datagram size */
940 /* 40 1K datagrams */
941SYSCTL_ULONG(_net_inet_udp, UDPCTL_MAXDGRAM, maxdgram, CTLFLAG_RW,
942 &udp_sendspace, 0, "Maximum outgoing UDP datagram size");
943
944u_long udp_recvspace = 40 * (1024 +
945#ifdef INET6
946 sizeof(struct sockaddr_in6)
947#else
948 sizeof(struct sockaddr_in)
949#endif
950 );
951SYSCTL_ULONG(_net_inet_udp, UDPCTL_RECVSPACE, recvspace, CTLFLAG_RW,
952 &udp_recvspace, 0, "Maximum space for incoming UDP datagrams");
953
954static void
955udp_abort(struct socket *so)
956{
957 struct inpcb *inp;
958
959 inp = sotoinpcb(so);
960 KASSERT(inp != NULL, ("udp_abort: inp == NULL"));
961 INP_INFO_WLOCK(&udbinfo);
962 INP_LOCK(inp);
963 if (inp->inp_faddr.s_addr != INADDR_ANY) {
964 in_pcbdisconnect(inp);
965 inp->inp_laddr.s_addr = INADDR_ANY;
966 soisdisconnected(so);
967 }
968 INP_UNLOCK(inp);
969 INP_INFO_WUNLOCK(&udbinfo);
970}
971
972static int
973udp_attach(struct socket *so, int proto, struct thread *td)
974{
975 struct inpcb *inp;
976 int error;
977
978 inp = sotoinpcb(so);
979 KASSERT(inp == NULL, ("udp_attach: inp != NULL"));
980 error = soreserve(so, udp_sendspace, udp_recvspace);
981 if (error)
982 return error;
983 INP_INFO_WLOCK(&udbinfo);
984 error = in_pcballoc(so, &udbinfo);
985 if (error) {
986 INP_INFO_WUNLOCK(&udbinfo);
987 return error;
988 }
989
990 inp = (struct inpcb *)so->so_pcb;
991 INP_INFO_WUNLOCK(&udbinfo);
992 inp->inp_vflag |= INP_IPV4;
993 inp->inp_ip_ttl = ip_defttl;
994 INP_UNLOCK(inp);
995 return 0;
996}
997
998static int
999udp_bind(struct socket *so, struct sockaddr *nam, struct thread *td)
1000{
1001 struct inpcb *inp;
1002 int error;
1003
1004 inp = sotoinpcb(so);
1005 KASSERT(inp != NULL, ("udp_bind: inp == NULL"));
1006 INP_INFO_WLOCK(&udbinfo);
1007 INP_LOCK(inp);
1008 error = in_pcbbind(inp, nam, td->td_ucred);
1009 INP_UNLOCK(inp);
1010 INP_INFO_WUNLOCK(&udbinfo);
1011 return error;
1012}
1013
1014static void
1015udp_close(struct socket *so)
1016{
1017 struct inpcb *inp;
1018
1019 inp = sotoinpcb(so);
1020 KASSERT(inp != NULL, ("udp_close: inp == NULL"));
1021 INP_INFO_WLOCK(&udbinfo);
1022 INP_LOCK(inp);
1023 if (inp->inp_faddr.s_addr != INADDR_ANY) {
1024 in_pcbdisconnect(inp);
1025 inp->inp_laddr.s_addr = INADDR_ANY;
1026 soisdisconnected(so);
1027 }
1028 INP_UNLOCK(inp);
1029 INP_INFO_WUNLOCK(&udbinfo);
1030}
1031
1032static int
1033udp_connect(struct socket *so, struct sockaddr *nam, struct thread *td)
1034{
1035 struct inpcb *inp;
1036 int error;
1037 struct sockaddr_in *sin;
1038
1039 inp = sotoinpcb(so);
1040 KASSERT(inp != NULL, ("udp_connect: inp == NULL"));
1041 INP_INFO_WLOCK(&udbinfo);
1042 INP_LOCK(inp);
1043 if (inp->inp_faddr.s_addr != INADDR_ANY) {
1044 INP_UNLOCK(inp);
1045 INP_INFO_WUNLOCK(&udbinfo);
1046 return EISCONN;
1047 }
1048 sin = (struct sockaddr_in *)nam;
1049 if (jailed(td->td_ucred))
1050 prison_remote_ip(td->td_ucred, 0, &sin->sin_addr.s_addr);
1051 error = in_pcbconnect(inp, nam, td->td_ucred);
1052 if (error == 0)
1053 soisconnected(so);
1054 INP_UNLOCK(inp);
1055 INP_INFO_WUNLOCK(&udbinfo);
1056 return error;
1057}
1058
1059static void
1060udp_detach(struct socket *so)
1061{
1062 struct inpcb *inp;
1063
1064 inp = sotoinpcb(so);
1065 KASSERT(inp != NULL, ("udp_detach: inp == NULL"));
1066 KASSERT(inp->inp_faddr.s_addr == INADDR_ANY,
1067 ("udp_detach: not disconnected"));
1068 INP_INFO_WLOCK(&udbinfo);
1069 INP_LOCK(inp);
1070 in_pcbdetach(inp);
1071 in_pcbfree(inp);
1072 INP_INFO_WUNLOCK(&udbinfo);
1073}
1074
1075static int
1076udp_disconnect(struct socket *so)
1077{
1078 struct inpcb *inp;
1079
1080 inp = sotoinpcb(so);
1081 KASSERT(inp != NULL, ("udp_disconnect: inp == NULL"));
1082 INP_INFO_WLOCK(&udbinfo);
1083 INP_LOCK(inp);
1084 if (inp->inp_faddr.s_addr == INADDR_ANY) {
1085 INP_INFO_WUNLOCK(&udbinfo);
1086 INP_UNLOCK(inp);
1087 return ENOTCONN;
1088 }
1089
1090 in_pcbdisconnect(inp);
1091 inp->inp_laddr.s_addr = INADDR_ANY;
1092 SOCK_LOCK(so);
1093 so->so_state &= ~SS_ISCONNECTED; /* XXX */
1094 SOCK_UNLOCK(so);
1095 INP_UNLOCK(inp);
1096 INP_INFO_WUNLOCK(&udbinfo);
1097 return 0;
1098}
1099
1100static int
1101udp_send(struct socket *so, int flags, struct mbuf *m, struct sockaddr *addr,
1102 struct mbuf *control, struct thread *td)
1103{
1104 struct inpcb *inp;
1105
1106 inp = sotoinpcb(so);
1107 KASSERT(inp != NULL, ("udp_send: inp == NULL"));
1108 return udp_output(inp, m, addr, control, td);
1109}
1110
1111int
1112udp_shutdown(struct socket *so)
1113{
1114 struct inpcb *inp;
1115
1116 inp = sotoinpcb(so);
1117 KASSERT(inp != NULL, ("udp_shutdown: inp == NULL"));
1118 INP_LOCK(inp);
1119 socantsendmore(so);
1120 INP_UNLOCK(inp);
1121 return 0;
1122}
1123
1124/*
1125 * This is the wrapper function for in_setsockaddr. We just pass down
1126 * the pcbinfo for in_setsockaddr to lock. We don't want to do the locking
1127 * here because in_setsockaddr will call malloc and might block.
1128 */
1129static int
1130udp_sockaddr(struct socket *so, struct sockaddr **nam)
1131{
1132 return (in_setsockaddr(so, nam, &udbinfo));
1133}
1134
1135/*
1136 * This is the wrapper function for in_setpeeraddr. We just pass down
1137 * the pcbinfo for in_setpeeraddr to lock.
1138 */
1139static int
1140udp_peeraddr(struct socket *so, struct sockaddr **nam)
1141{
1142 return (in_setpeeraddr(so, nam, &udbinfo));
1143}
1144
1145struct pr_usrreqs udp_usrreqs = {
1146 .pru_abort = udp_abort,
1147 .pru_attach = udp_attach,
1148 .pru_bind = udp_bind,
1149 .pru_connect = udp_connect,
1150 .pru_control = in_control,
1151 .pru_detach = udp_detach,
1152 .pru_disconnect = udp_disconnect,
1153 .pru_peeraddr = udp_peeraddr,
1154 .pru_send = udp_send,
1155 .pru_sosend = sosend_dgram,
1156 .pru_shutdown = udp_shutdown,
1157 .pru_sockaddr = udp_sockaddr,
1158 .pru_sosetlabel = in_pcbsosetlabel,
1159 .pru_close = udp_close,
1160};