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