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