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