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 * 3. All advertising materials mentioning features or use of this software
14 * must display the following acknowledgement:
15 * This product includes software developed by the University of
16 * California, Berkeley and its contributors.
17 * 4. Neither the name of the University nor the names of its contributors
18 * may be used to endorse or promote products derived from this software
19 * without specific prior written permission.
20 *
21 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 * SUCH DAMAGE.
32 *
33 * @(#)udp_usrreq.c 8.6 (Berkeley) 5/23/95
34 * $FreeBSD: releng/5.1/sys/netinet/udp_usrreq.c 111119 2003-02-19 05:47:46Z imp $
35 */
36
37 #include "opt_ipsec.h"
38 #include "opt_inet6.h"
39 #include "opt_mac.h"
40
41 #include <sys/param.h>
42 #include <sys/systm.h>
43 #include <sys/domain.h>
44 #include <sys/jail.h>
45 #include <sys/kernel.h>
46 #include <sys/lock.h>
47 #include <sys/mac.h>
48 #include <sys/malloc.h>
49 #include <sys/mbuf.h>
50 #include <sys/proc.h>
51 #include <sys/protosw.h>
52 #include <sys/signalvar.h>
53 #include <sys/socket.h>
54 #include <sys/socketvar.h>
55 #include <sys/sx.h>
56 #include <sys/sysctl.h>
57 #include <sys/syslog.h>
58
59 #include <vm/uma.h>
60
61 #include <net/if.h>
62 #include <net/route.h>
63
64 #include <netinet/in.h>
65 #include <netinet/in_systm.h>
66 #include <netinet/in_pcb.h>
67 #include <netinet/in_var.h>
68 #include <netinet/ip.h>
69 #ifdef INET6
70 #include <netinet/ip6.h>
71 #endif
72 #include <netinet/ip_icmp.h>
73 #include <netinet/icmp_var.h>
74 #include <netinet/ip_var.h>
75 #ifdef INET6
76 #include <netinet6/ip6_var.h>
77 #endif
78 #include <netinet/udp.h>
79 #include <netinet/udp_var.h>
80
81 #ifdef FAST_IPSEC
82 #include <netipsec/ipsec.h>
83 #endif /*FAST_IPSEC*/
84
85 #ifdef IPSEC
86 #include <netinet6/ipsec.h>
87 #endif /*IPSEC*/
88
89 #include <machine/in_cksum.h>
90
91 /*
92 * UDP protocol implementation.
93 * Per RFC 768, August, 1980.
94 */
95 #ifndef COMPAT_42
96 static int udpcksum = 1;
97 #else
98 static int udpcksum = 0; /* XXX */
99 #endif
100 SYSCTL_INT(_net_inet_udp, UDPCTL_CHECKSUM, checksum, CTLFLAG_RW,
101 &udpcksum, 0, "");
102
103 int log_in_vain = 0;
104 SYSCTL_INT(_net_inet_udp, OID_AUTO, log_in_vain, CTLFLAG_RW,
105 &log_in_vain, 0, "Log all incoming UDP packets");
106
107 static int blackhole = 0;
108 SYSCTL_INT(_net_inet_udp, OID_AUTO, blackhole, CTLFLAG_RW,
109 &blackhole, 0, "Do not send port unreachables for refused connects");
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 struct sockaddr_in udp_in = { sizeof(udp_in), AF_INET };
124 #ifdef INET6
125 struct udp_in6 {
126 struct sockaddr_in6 uin6_sin;
127 u_char uin6_init_done : 1;
128 } udp_in6 = {
129 { sizeof(udp_in6.uin6_sin), AF_INET6 },
130 0
131 };
132 struct udp_ip6 {
133 struct ip6_hdr uip6_ip6;
134 u_char uip6_init_done : 1;
135 } udp_ip6;
136 #endif /* INET6 */
137
138 static void udp_append(struct inpcb *last, struct ip *ip, struct mbuf *n,
139 int off);
140 #ifdef INET6
141 static void ip_2_ip6_hdr(struct ip6_hdr *ip6, struct ip *ip);
142 #endif
143
144 static int udp_detach(struct socket *so);
145 static int udp_output(struct inpcb *, struct mbuf *, struct sockaddr *,
146 struct mbuf *, struct thread *);
147
148 void
149 udp_init()
150 {
151 INP_INFO_LOCK_INIT(&udbinfo, "udp");
152 LIST_INIT(&udb);
153 udbinfo.listhead = &udb;
154 udbinfo.hashbase = hashinit(UDBHASHSIZE, M_PCB, &udbinfo.hashmask);
155 udbinfo.porthashbase = hashinit(UDBHASHSIZE, M_PCB,
156 &udbinfo.porthashmask);
157 udbinfo.ipi_zone = uma_zcreate("udpcb", sizeof(struct inpcb), NULL,
158 NULL, NULL, NULL, UMA_ALIGN_PTR, UMA_ZONE_NOFREE);
159 uma_zone_set_max(udbinfo.ipi_zone, maxsockets);
160 }
161
162 void
163 udp_input(m, off)
164 register struct mbuf *m;
165 int off;
166 {
167 int iphlen = off;
168 register struct ip *ip;
169 register struct udphdr *uh;
170 register struct inpcb *inp;
171 struct mbuf *opts = 0;
172 int len;
173 struct ip save_ip;
174
175 udpstat.udps_ipackets++;
176
177 /*
178 * Strip IP options, if any; should skip this,
179 * make available to user, and use on returned packets,
180 * but we don't yet have a way to check the checksum
181 * with options still present.
182 */
183 if (iphlen > sizeof (struct ip)) {
184 ip_stripoptions(m, (struct mbuf *)0);
185 iphlen = sizeof(struct ip);
186 }
187
188 /*
189 * Get IP and UDP header together in first mbuf.
190 */
191 ip = mtod(m, struct ip *);
192 if (m->m_len < iphlen + sizeof(struct udphdr)) {
193 if ((m = m_pullup(m, iphlen + sizeof(struct udphdr))) == 0) {
194 udpstat.udps_hdrops++;
195 return;
196 }
197 ip = mtod(m, struct ip *);
198 }
199 uh = (struct udphdr *)((caddr_t)ip + iphlen);
200
201 /* destination port of 0 is illegal, based on RFC768. */
202 if (uh->uh_dport == 0)
203 goto badunlocked;
204
205 /*
206 * Construct sockaddr format source address.
207 * Stuff source address and datagram in user buffer.
208 */
209 udp_in.sin_port = uh->uh_sport;
210 udp_in.sin_addr = ip->ip_src;
211 #ifdef INET6
212 udp_in6.uin6_init_done = udp_ip6.uip6_init_done = 0;
213 #endif
214
215 /*
216 * Make mbuf data length reflect UDP length.
217 * If not enough data to reflect UDP length, drop.
218 */
219 len = ntohs((u_short)uh->uh_ulen);
220 if (ip->ip_len != len) {
221 if (len > ip->ip_len || len < sizeof(struct udphdr)) {
222 udpstat.udps_badlen++;
223 goto badunlocked;
224 }
225 m_adj(m, len - ip->ip_len);
226 /* ip->ip_len = len; */
227 }
228 /*
229 * Save a copy of the IP header in case we want restore it
230 * for sending an ICMP error message in response.
231 */
232 if (!blackhole)
233 save_ip = *ip;
234
235 /*
236 * Checksum extended UDP header and data.
237 */
238 if (uh->uh_sum) {
239 if (m->m_pkthdr.csum_flags & CSUM_DATA_VALID) {
240 if (m->m_pkthdr.csum_flags & CSUM_PSEUDO_HDR)
241 uh->uh_sum = m->m_pkthdr.csum_data;
242 else
243 uh->uh_sum = in_pseudo(ip->ip_src.s_addr,
244 ip->ip_dst.s_addr, htonl((u_short)len +
245 m->m_pkthdr.csum_data + IPPROTO_UDP));
246 uh->uh_sum ^= 0xffff;
247 } else {
248 char b[9];
249 bcopy(((struct ipovly *)ip)->ih_x1, b, 9);
250 bzero(((struct ipovly *)ip)->ih_x1, 9);
251 ((struct ipovly *)ip)->ih_len = uh->uh_ulen;
252 uh->uh_sum = in_cksum(m, len + sizeof (struct ip));
253 bcopy(b, ((struct ipovly *)ip)->ih_x1, 9);
254 }
255 if (uh->uh_sum) {
256 udpstat.udps_badsum++;
257 m_freem(m);
258 return;
259 }
260 } else
261 udpstat.udps_nosum++;
262
263 INP_INFO_RLOCK(&udbinfo);
264
265 if (IN_MULTICAST(ntohl(ip->ip_dst.s_addr)) ||
266 in_broadcast(ip->ip_dst, m->m_pkthdr.rcvif)) {
267 struct inpcb *last;
268 /*
269 * Deliver a multicast or broadcast datagram to *all* sockets
270 * for which the local and remote addresses and ports match
271 * those of the incoming datagram. This allows more than
272 * one process to receive multi/broadcasts on the same port.
273 * (This really ought to be done for unicast datagrams as
274 * well, but that would cause problems with existing
275 * applications that open both address-specific sockets and
276 * a wildcard socket listening to the same port -- they would
277 * end up receiving duplicates of every unicast datagram.
278 * Those applications open the multiple sockets to overcome an
279 * inadequacy of the UDP socket interface, but for backwards
280 * compatibility we avoid the problem here rather than
281 * fixing the interface. Maybe 4.5BSD will remedy this?)
282 */
283
284 /*
285 * Locate pcb(s) for datagram.
286 * (Algorithm copied from raw_intr().)
287 */
288 last = NULL;
289 LIST_FOREACH(inp, &udb, inp_list) {
290 INP_LOCK(inp);
291 if (inp->inp_lport != uh->uh_dport) {
292 docontinue:
293 INP_UNLOCK(inp);
294 continue;
295 }
296 #ifdef INET6
297 if ((inp->inp_vflag & INP_IPV4) == 0)
298 goto docontinue;
299 #endif
300 if (inp->inp_laddr.s_addr != INADDR_ANY) {
301 if (inp->inp_laddr.s_addr != ip->ip_dst.s_addr)
302 goto docontinue;
303 }
304 if (inp->inp_faddr.s_addr != INADDR_ANY) {
305 if (inp->inp_faddr.s_addr !=
306 ip->ip_src.s_addr ||
307 inp->inp_fport != uh->uh_sport)
308 goto docontinue;
309 }
310
311 if (last != NULL) {
312 struct mbuf *n;
313
314 n = m_copy(m, 0, M_COPYALL);
315 if (n != NULL)
316 udp_append(last, ip, n,
317 iphlen +
318 sizeof(struct udphdr));
319 INP_UNLOCK(last);
320 }
321 last = inp;
322 /*
323 * Don't look for additional matches if this one does
324 * not have either the SO_REUSEPORT or SO_REUSEADDR
325 * socket options set. This heuristic avoids searching
326 * through all pcbs in the common case of a non-shared
327 * port. It * assumes that an application will never
328 * clear these options after setting them.
329 */
330 if ((last->inp_socket->so_options&(SO_REUSEPORT|SO_REUSEADDR)) == 0)
331 break;
332 }
333
334 if (last == NULL) {
335 /*
336 * No matching pcb found; discard datagram.
337 * (No need to send an ICMP Port Unreachable
338 * for a broadcast or multicast datgram.)
339 */
340 udpstat.udps_noportbcast++;
341 goto badheadlocked;
342 }
343 INP_INFO_RUNLOCK(&udbinfo);
344 udp_append(last, ip, m, iphlen + sizeof(struct udphdr));
345 INP_UNLOCK(last);
346 return;
347 }
348 /*
349 * Locate pcb for datagram.
350 */
351 inp = in_pcblookup_hash(&udbinfo, ip->ip_src, uh->uh_sport,
352 ip->ip_dst, uh->uh_dport, 1, m->m_pkthdr.rcvif);
353 if (inp == NULL) {
354 if (log_in_vain) {
355 char buf[4*sizeof "123"];
356
357 strcpy(buf, inet_ntoa(ip->ip_dst));
358 log(LOG_INFO,
359 "Connection attempt to UDP %s:%d from %s:%d\n",
360 buf, ntohs(uh->uh_dport), inet_ntoa(ip->ip_src),
361 ntohs(uh->uh_sport));
362 }
363 udpstat.udps_noport++;
364 if (m->m_flags & (M_BCAST | M_MCAST)) {
365 udpstat.udps_noportbcast++;
366 goto badheadlocked;
367 }
368 if (blackhole)
369 goto badheadlocked;
370 if (badport_bandlim(BANDLIM_ICMP_UNREACH) < 0)
371 goto badheadlocked;
372 *ip = save_ip;
373 ip->ip_len += iphlen;
374 icmp_error(m, ICMP_UNREACH, ICMP_UNREACH_PORT, 0, 0);
375 INP_INFO_RUNLOCK(&udbinfo);
376 return;
377 }
378 INP_LOCK(inp);
379 INP_INFO_RUNLOCK(&udbinfo);
380 udp_append(inp, ip, m, iphlen + sizeof(struct udphdr));
381 INP_UNLOCK(inp);
382 return;
383
384 badheadlocked:
385 INP_INFO_RUNLOCK(&udbinfo);
386 if (inp)
387 INP_UNLOCK(inp);
388 badunlocked:
389 m_freem(m);
390 if (opts)
391 m_freem(opts);
392 return;
393 }
394
395 #ifdef INET6
396 static void
397 ip_2_ip6_hdr(ip6, ip)
398 struct ip6_hdr *ip6;
399 struct ip *ip;
400 {
401 bzero(ip6, sizeof(*ip6));
402
403 ip6->ip6_vfc = IPV6_VERSION;
404 ip6->ip6_plen = ip->ip_len;
405 ip6->ip6_nxt = ip->ip_p;
406 ip6->ip6_hlim = ip->ip_ttl;
407 ip6->ip6_src.s6_addr32[2] = ip6->ip6_dst.s6_addr32[2] =
408 IPV6_ADDR_INT32_SMP;
409 ip6->ip6_src.s6_addr32[3] = ip->ip_src.s_addr;
410 ip6->ip6_dst.s6_addr32[3] = ip->ip_dst.s_addr;
411 }
412 #endif
413
414 /*
415 * subroutine of udp_input(), mainly for source code readability.
416 * caller must properly init udp_ip6 and udp_in6 beforehand.
417 */
418 static void
419 udp_append(last, ip, n, off)
420 struct inpcb *last;
421 struct ip *ip;
422 struct mbuf *n;
423 int off;
424 {
425 struct sockaddr *append_sa;
426 struct mbuf *opts = 0;
427
428 #ifdef IPSEC
429 /* check AH/ESP integrity. */
430 if (ipsec4_in_reject_so(n, last->inp_socket)) {
431 ipsecstat.in_polvio++;
432 m_freem(n);
433 return;
434 }
435 #endif /*IPSEC*/
436 #ifdef FAST_IPSEC
437 /* check AH/ESP integrity. */
438 if (ipsec4_in_reject(n, last)) {
439 m_freem(n);
440 return;
441 }
442 #endif /*FAST_IPSEC*/
443 #ifdef MAC
444 if (mac_check_socket_deliver(last->inp_socket, n) != 0) {
445 m_freem(n);
446 return;
447 }
448 #endif
449 if (last->inp_flags & INP_CONTROLOPTS ||
450 last->inp_socket->so_options & SO_TIMESTAMP) {
451 #ifdef INET6
452 if (last->inp_vflag & INP_IPV6) {
453 int savedflags;
454
455 if (udp_ip6.uip6_init_done == 0) {
456 ip_2_ip6_hdr(&udp_ip6.uip6_ip6, ip);
457 udp_ip6.uip6_init_done = 1;
458 }
459 savedflags = last->inp_flags;
460 last->inp_flags &= ~INP_UNMAPPABLEOPTS;
461 ip6_savecontrol(last, &opts, &udp_ip6.uip6_ip6, n);
462 last->inp_flags = savedflags;
463 } else
464 #endif
465 ip_savecontrol(last, &opts, ip, n);
466 }
467 #ifdef INET6
468 if (last->inp_vflag & INP_IPV6) {
469 if (udp_in6.uin6_init_done == 0) {
470 in6_sin_2_v4mapsin6(&udp_in, &udp_in6.uin6_sin);
471 udp_in6.uin6_init_done = 1;
472 }
473 append_sa = (struct sockaddr *)&udp_in6.uin6_sin;
474 } else
475 #endif
476 append_sa = (struct sockaddr *)&udp_in;
477 m_adj(n, off);
478 if (sbappendaddr(&last->inp_socket->so_rcv, append_sa, n, opts) == 0) {
479 m_freem(n);
480 if (opts)
481 m_freem(opts);
482 udpstat.udps_fullsock++;
483 } else
484 sorwakeup(last->inp_socket);
485 }
486
487 /*
488 * Notify a udp user of an asynchronous error;
489 * just wake up so that he can collect error status.
490 */
491 struct inpcb *
492 udp_notify(inp, errno)
493 register struct inpcb *inp;
494 int errno;
495 {
496 inp->inp_socket->so_error = errno;
497 sorwakeup(inp->inp_socket);
498 sowwakeup(inp->inp_socket);
499 return inp;
500 }
501
502 void
503 udp_ctlinput(cmd, sa, vip)
504 int cmd;
505 struct sockaddr *sa;
506 void *vip;
507 {
508 struct ip *ip = vip;
509 struct udphdr *uh;
510 struct inpcb *(*notify)(struct inpcb *, int) = udp_notify;
511 struct in_addr faddr;
512 struct inpcb *inp;
513 int s;
514
515 faddr = ((struct sockaddr_in *)sa)->sin_addr;
516 if (sa->sa_family != AF_INET || faddr.s_addr == INADDR_ANY)
517 return;
518
519 if (PRC_IS_REDIRECT(cmd)) {
520 ip = 0;
521 notify = in_rtchange;
522 } else if (cmd == PRC_HOSTDEAD)
523 ip = 0;
524 else if ((unsigned)cmd >= PRC_NCMDS || inetctlerrmap[cmd] == 0)
525 return;
526 if (ip) {
527 s = splnet();
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 splx(s);
541 } else
542 in_pcbnotifyall(&udbinfo, faddr, inetctlerrmap[cmd], notify);
543 }
544
545 static int
546 udp_pcblist(SYSCTL_HANDLER_ARGS)
547 {
548 int error, i, n, s;
549 struct inpcb *inp, **inp_list;
550 inp_gen_t gencnt;
551 struct xinpgen xig;
552
553 /*
554 * The process of preparing the TCB list is too time-consuming and
555 * resource-intensive to repeat twice on every request.
556 */
557 if (req->oldptr == 0) {
558 n = udbinfo.ipi_count;
559 req->oldidx = 2 * (sizeof xig)
560 + (n + n/8) * sizeof(struct xinpcb);
561 return 0;
562 }
563
564 if (req->newptr != 0)
565 return EPERM;
566
567 /*
568 * OK, now we're committed to doing something.
569 */
570 s = splnet();
571 INP_INFO_RLOCK(&udbinfo);
572 gencnt = udbinfo.ipi_gencnt;
573 n = udbinfo.ipi_count;
574 INP_INFO_RUNLOCK(&udbinfo);
575 splx(s);
576
577 sysctl_wire_old_buffer(req, 2 * (sizeof xig)
578 + n * sizeof(struct xinpcb));
579
580 xig.xig_len = sizeof xig;
581 xig.xig_count = n;
582 xig.xig_gen = gencnt;
583 xig.xig_sogen = so_gencnt;
584 error = SYSCTL_OUT(req, &xig, sizeof xig);
585 if (error)
586 return error;
587
588 inp_list = malloc(n * sizeof *inp_list, M_TEMP, M_WAITOK);
589 if (inp_list == 0)
590 return ENOMEM;
591
592 s = splnet();
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 splx(s);
604 n = i;
605
606 error = 0;
607 for (i = 0; i < n; i++) {
608 inp = inp_list[i];
609 if (inp->inp_gencnt <= gencnt) {
610 struct xinpcb 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 s = splnet();
629 INP_INFO_RLOCK(&udbinfo);
630 xig.xig_gen = udbinfo.ipi_gencnt;
631 xig.xig_sogen = so_gencnt;
632 xig.xig_count = udbinfo.ipi_count;
633 INP_INFO_RUNLOCK(&udbinfo);
634 splx(s);
635 error = SYSCTL_OUT(req, &xig, sizeof xig);
636 }
637 free(inp_list, M_TEMP);
638 return error;
639 }
640
641 SYSCTL_PROC(_net_inet_udp, UDPCTL_PCBLIST, pcblist, CTLFLAG_RD, 0, 0,
642 udp_pcblist, "S,xinpcb", "List of active UDP sockets");
643
644 static int
645 udp_getcred(SYSCTL_HANDLER_ARGS)
646 {
647 struct xucred xuc;
648 struct sockaddr_in addrs[2];
649 struct inpcb *inp;
650 int error, s;
651
652 error = suser_cred(req->td->td_ucred, PRISON_ROOT);
653 if (error)
654 return (error);
655 error = SYSCTL_IN(req, addrs, sizeof(addrs));
656 if (error)
657 return (error);
658 s = splnet();
659 INP_INFO_RLOCK(&udbinfo);
660 inp = in_pcblookup_hash(&udbinfo, addrs[1].sin_addr, addrs[1].sin_port,
661 addrs[0].sin_addr, addrs[0].sin_port, 1, NULL);
662 if (inp == NULL || inp->inp_socket == NULL) {
663 error = ENOENT;
664 goto out;
665 }
666 error = cr_canseesocket(req->td->td_ucred, inp->inp_socket);
667 if (error)
668 goto out;
669 cru2x(inp->inp_socket->so_cred, &xuc);
670 out:
671 INP_INFO_RUNLOCK(&udbinfo);
672 splx(s);
673 if (error == 0)
674 error = SYSCTL_OUT(req, &xuc, sizeof(struct xucred));
675 return (error);
676 }
677
678 SYSCTL_PROC(_net_inet_udp, OID_AUTO, getcred,
679 CTLTYPE_OPAQUE|CTLFLAG_RW|CTLFLAG_PRISON, 0, 0,
680 udp_getcred, "S,xucred", "Get the xucred of a UDP connection");
681
682 static int
683 udp_output(inp, m, addr, control, td)
684 register struct inpcb *inp;
685 struct mbuf *m;
686 struct sockaddr *addr;
687 struct mbuf *control;
688 struct thread *td;
689 {
690 register struct udpiphdr *ui;
691 register int len = m->m_pkthdr.len;
692 struct in_addr faddr, laddr;
693 struct cmsghdr *cm;
694 struct sockaddr_in *sin, src;
695 int error = 0;
696 u_short fport, lport;
697
698 #ifdef MAC
699 mac_create_mbuf_from_socket(inp->inp_socket, m);
700 #endif
701
702 if (len + sizeof(struct udpiphdr) > IP_MAXPACKET) {
703 error = EMSGSIZE;
704 if (control)
705 m_freem(control);
706 goto release;
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 error = EINVAL;
717 m_freem(control);
718 goto release;
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 goto release;
756 laddr = inp->inp_laddr;
757 lport = inp->inp_lport;
758 if (src.sin_addr.s_addr != INADDR_ANY) {
759 if (lport == 0) {
760 error = EINVAL;
761 goto release;
762 }
763 error = in_pcbbind_setup(inp, (struct sockaddr *)&src,
764 &laddr.s_addr, &lport, td);
765 if (error)
766 goto release;
767 }
768
769 if (addr) {
770 sin = (struct sockaddr_in *)addr;
771 if (td && jailed(td->td_ucred))
772 prison_remote_ip(td->td_ucred, 0, &sin->sin_addr.s_addr);
773 if (inp->inp_faddr.s_addr != INADDR_ANY) {
774 error = EISCONN;
775 goto release;
776 }
777 error = in_pcbconnect_setup(inp, addr, &laddr.s_addr, &lport,
778 &faddr.s_addr, &fport, NULL, td);
779 if (error)
780 goto release;
781
782 /* Commit the local port if newly assigned. */
783 if (inp->inp_laddr.s_addr == INADDR_ANY &&
784 inp->inp_lport == 0) {
785 inp->inp_lport = lport;
786 if (in_pcbinshash(inp) != 0) {
787 inp->inp_lport = 0;
788 error = EAGAIN;
789 goto release;
790 }
791 inp->inp_flags |= INP_ANONPORT;
792 }
793 } else {
794 faddr = inp->inp_faddr;
795 fport = inp->inp_fport;
796 if (faddr.s_addr == INADDR_ANY) {
797 error = ENOTCONN;
798 goto release;
799 }
800 }
801 /*
802 * Calculate data length and get a mbuf
803 * for UDP and IP headers.
804 */
805 M_PREPEND(m, sizeof(struct udpiphdr), M_DONTWAIT);
806 if (m == 0) {
807 error = ENOBUFS;
808 goto release;
809 }
810
811 /*
812 * Fill in mbuf with extended UDP header
813 * and addresses and length put into network format.
814 */
815 ui = mtod(m, struct udpiphdr *);
816 bzero(ui->ui_x1, sizeof(ui->ui_x1)); /* XXX still needed? */
817 ui->ui_pr = IPPROTO_UDP;
818 ui->ui_src = laddr;
819 ui->ui_dst = faddr;
820 ui->ui_sport = lport;
821 ui->ui_dport = fport;
822 ui->ui_ulen = htons((u_short)len + sizeof(struct udphdr));
823
824 /*
825 * Set up checksum and output datagram.
826 */
827 if (udpcksum) {
828 ui->ui_sum = in_pseudo(ui->ui_src.s_addr, ui->ui_dst.s_addr,
829 htons((u_short)len + sizeof(struct udphdr) + IPPROTO_UDP));
830 m->m_pkthdr.csum_flags = CSUM_UDP;
831 m->m_pkthdr.csum_data = offsetof(struct udphdr, uh_sum);
832 } else {
833 ui->ui_sum = 0;
834 }
835 ((struct ip *)ui)->ip_len = sizeof (struct udpiphdr) + len;
836 ((struct ip *)ui)->ip_ttl = inp->inp_ip_ttl; /* XXX */
837 ((struct ip *)ui)->ip_tos = inp->inp_ip_tos; /* XXX */
838 udpstat.udps_opackets++;
839
840 error = ip_output(m, inp->inp_options, &inp->inp_route,
841 (inp->inp_socket->so_options & (SO_DONTROUTE | SO_BROADCAST)),
842 inp->inp_moptions, inp);
843 return (error);
844
845 release:
846 m_freem(m);
847 return (error);
848 }
849
850 u_long udp_sendspace = 9216; /* really max datagram size */
851 /* 40 1K datagrams */
852 SYSCTL_INT(_net_inet_udp, UDPCTL_MAXDGRAM, maxdgram, CTLFLAG_RW,
853 &udp_sendspace, 0, "Maximum outgoing UDP datagram size");
854
855 u_long udp_recvspace = 40 * (1024 +
856 #ifdef INET6
857 sizeof(struct sockaddr_in6)
858 #else
859 sizeof(struct sockaddr_in)
860 #endif
861 );
862 SYSCTL_INT(_net_inet_udp, UDPCTL_RECVSPACE, recvspace, CTLFLAG_RW,
863 &udp_recvspace, 0, "Maximum incoming UDP datagram size");
864
865 static int
866 udp_abort(struct socket *so)
867 {
868 struct inpcb *inp;
869 int s;
870
871 INP_INFO_WLOCK(&udbinfo);
872 inp = sotoinpcb(so);
873 if (inp == 0) {
874 INP_INFO_WUNLOCK(&udbinfo);
875 return EINVAL; /* ??? possible? panic instead? */
876 }
877 INP_LOCK(inp);
878 soisdisconnected(so);
879 s = splnet();
880 in_pcbdetach(inp);
881 INP_INFO_WUNLOCK(&udbinfo);
882 splx(s);
883 return 0;
884 }
885
886 static int
887 udp_attach(struct socket *so, int proto, struct thread *td)
888 {
889 struct inpcb *inp;
890 int s, error;
891
892 INP_INFO_WLOCK(&udbinfo);
893 inp = sotoinpcb(so);
894 if (inp != 0) {
895 INP_INFO_WUNLOCK(&udbinfo);
896 return EINVAL;
897 }
898 error = soreserve(so, udp_sendspace, udp_recvspace);
899 if (error) {
900 INP_INFO_WUNLOCK(&udbinfo);
901 return error;
902 }
903 s = splnet();
904 error = in_pcballoc(so, &udbinfo, td);
905 splx(s);
906 if (error)
907 return error;
908
909 inp = (struct inpcb *)so->so_pcb;
910 INP_LOCK(inp);
911 INP_INFO_WUNLOCK(&udbinfo);
912 inp->inp_vflag |= INP_IPV4;
913 inp->inp_ip_ttl = ip_defttl;
914 INP_UNLOCK(inp);
915 return 0;
916 }
917
918 static int
919 udp_bind(struct socket *so, struct sockaddr *nam, struct thread *td)
920 {
921 struct inpcb *inp;
922 int s, error;
923
924 INP_INFO_WLOCK(&udbinfo);
925 inp = sotoinpcb(so);
926 if (inp == 0) {
927 INP_INFO_WUNLOCK(&udbinfo);
928 return EINVAL;
929 }
930 INP_LOCK(inp);
931 s = splnet();
932 error = in_pcbbind(inp, nam, td);
933 splx(s);
934 INP_UNLOCK(inp);
935 INP_INFO_WUNLOCK(&udbinfo);
936 return error;
937 }
938
939 static int
940 udp_connect(struct socket *so, struct sockaddr *nam, struct thread *td)
941 {
942 struct inpcb *inp;
943 int s, error;
944 struct sockaddr_in *sin;
945
946 INP_INFO_WLOCK(&udbinfo);
947 inp = sotoinpcb(so);
948 if (inp == 0) {
949 INP_INFO_WUNLOCK(&udbinfo);
950 return EINVAL;
951 }
952 INP_LOCK(inp);
953 if (inp->inp_faddr.s_addr != INADDR_ANY) {
954 INP_UNLOCK(inp);
955 INP_INFO_WUNLOCK(&udbinfo);
956 return EISCONN;
957 }
958 s = splnet();
959 sin = (struct sockaddr_in *)nam;
960 if (td && jailed(td->td_ucred))
961 prison_remote_ip(td->td_ucred, 0, &sin->sin_addr.s_addr);
962 error = in_pcbconnect(inp, nam, td);
963 splx(s);
964 if (error == 0)
965 soisconnected(so);
966 INP_UNLOCK(inp);
967 INP_INFO_WUNLOCK(&udbinfo);
968 return error;
969 }
970
971 static int
972 udp_detach(struct socket *so)
973 {
974 struct inpcb *inp;
975 int s;
976
977 INP_INFO_WLOCK(&udbinfo);
978 inp = sotoinpcb(so);
979 if (inp == 0) {
980 INP_INFO_WUNLOCK(&udbinfo);
981 return EINVAL;
982 }
983 INP_LOCK(inp);
984 s = splnet();
985 in_pcbdetach(inp);
986 INP_INFO_WUNLOCK(&udbinfo);
987 splx(s);
988 return 0;
989 }
990
991 static int
992 udp_disconnect(struct socket *so)
993 {
994 struct inpcb *inp;
995 int s;
996
997 INP_INFO_WLOCK(&udbinfo);
998 inp = sotoinpcb(so);
999 if (inp == 0) {
1000 INP_INFO_WUNLOCK(&udbinfo);
1001 return EINVAL;
1002 }
1003 INP_LOCK(inp);
1004 if (inp->inp_faddr.s_addr == INADDR_ANY) {
1005 INP_INFO_WUNLOCK(&udbinfo);
1006 INP_UNLOCK(inp);
1007 return ENOTCONN;
1008 }
1009
1010 s = splnet();
1011 in_pcbdisconnect(inp);
1012 inp->inp_laddr.s_addr = INADDR_ANY;
1013 INP_UNLOCK(inp);
1014 INP_INFO_WUNLOCK(&udbinfo);
1015 splx(s);
1016 so->so_state &= ~SS_ISCONNECTED; /* XXX */
1017 return 0;
1018 }
1019
1020 static int
1021 udp_send(struct socket *so, int flags, struct mbuf *m, struct sockaddr *addr,
1022 struct mbuf *control, struct thread *td)
1023 {
1024 struct inpcb *inp;
1025 int ret;
1026
1027 INP_INFO_WLOCK(&udbinfo);
1028 inp = sotoinpcb(so);
1029 if (inp == 0) {
1030 INP_INFO_WUNLOCK(&udbinfo);
1031 m_freem(m);
1032 return EINVAL;
1033 }
1034 INP_LOCK(inp);
1035 ret = udp_output(inp, m, addr, control, td);
1036 INP_UNLOCK(inp);
1037 INP_INFO_WUNLOCK(&udbinfo);
1038 return ret;
1039 }
1040
1041 int
1042 udp_shutdown(struct socket *so)
1043 {
1044 struct inpcb *inp;
1045
1046 INP_INFO_RLOCK(&udbinfo);
1047 inp = sotoinpcb(so);
1048 if (inp == 0) {
1049 INP_INFO_RUNLOCK(&udbinfo);
1050 return EINVAL;
1051 }
1052 INP_LOCK(inp);
1053 INP_INFO_RUNLOCK(&udbinfo);
1054 socantsendmore(so);
1055 INP_UNLOCK(inp);
1056 return 0;
1057 }
1058
1059 /*
1060 * This is the wrapper function for in_setsockaddr. We just pass down
1061 * the pcbinfo for in_setsockaddr to lock. We don't want to do the locking
1062 * here because in_setsockaddr will call malloc and might block.
1063 */
1064 static int
1065 udp_sockaddr(struct socket *so, struct sockaddr **nam)
1066 {
1067 return (in_setsockaddr(so, nam, &udbinfo));
1068 }
1069
1070 /*
1071 * This is the wrapper function for in_setpeeraddr. We just pass down
1072 * the pcbinfo for in_setpeeraddr to lock.
1073 */
1074 static int
1075 udp_peeraddr(struct socket *so, struct sockaddr **nam)
1076 {
1077 return (in_setpeeraddr(so, nam, &udbinfo));
1078 }
1079
1080 struct pr_usrreqs udp_usrreqs = {
1081 udp_abort, pru_accept_notsupp, udp_attach, udp_bind, udp_connect,
1082 pru_connect2_notsupp, in_control, udp_detach, udp_disconnect,
1083 pru_listen_notsupp, udp_peeraddr, pru_rcvd_notsupp,
1084 pru_rcvoob_notsupp, udp_send, pru_sense_null, udp_shutdown,
1085 udp_sockaddr, sosend, soreceive, sopoll
1086 };
Cache object: 97eca043a3e30b6faa0516705a3496da
|