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