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$
35 */
36
37 #include "opt_ipsec.h"
38 #include "opt_inet6.h"
39
40 #include <sys/param.h>
41 #include <sys/systm.h>
42 #include <sys/kernel.h>
43 #include <sys/malloc.h>
44 #include <sys/mbuf.h>
45 #include <sys/domain.h>
46 #include <sys/proc.h>
47 #include <sys/protosw.h>
48 #include <sys/socket.h>
49 #include <sys/socketvar.h>
50 #include <sys/sysctl.h>
51 #include <sys/syslog.h>
52
53 #include <vm/vm_zone.h>
54
55 #include <net/if.h>
56 #include <net/route.h>
57
58 #include <netinet/in.h>
59 #include <netinet/in_systm.h>
60 #include <netinet/ip.h>
61 #ifdef INET6
62 #include <netinet/ip6.h>
63 #endif
64 #include <netinet/in_pcb.h>
65 #include <netinet/in_var.h>
66 #include <netinet/ip_var.h>
67 #ifdef INET6
68 #include <netinet6/ip6_var.h>
69 #endif
70 #include <netinet/ip_icmp.h>
71 #include <netinet/icmp_var.h>
72 #include <netinet/udp.h>
73 #include <netinet/udp_var.h>
74
75 #ifdef FAST_IPSEC
76 #include <netipsec/ipsec.h>
77 #endif /*FAST_IPSEC*/
78
79 #ifdef IPSEC
80 #include <netinet6/ipsec.h>
81 #endif /*IPSEC*/
82
83 #include <machine/in_cksum.h>
84
85 /*
86 * UDP protocol implementation.
87 * Per RFC 768, August, 1980.
88 */
89 #ifndef COMPAT_42
90 static int udpcksum = 1;
91 #else
92 static int udpcksum = 0; /* XXX */
93 #endif
94 SYSCTL_INT(_net_inet_udp, UDPCTL_CHECKSUM, checksum, CTLFLAG_RW,
95 &udpcksum, 0, "");
96
97 int log_in_vain = 0;
98 SYSCTL_INT(_net_inet_udp, OID_AUTO, log_in_vain, CTLFLAG_RW,
99 &log_in_vain, 0, "Log all incoming UDP packets");
100
101 static int blackhole = 0;
102 SYSCTL_INT(_net_inet_udp, OID_AUTO, blackhole, CTLFLAG_RW,
103 &blackhole, 0, "Do not send port unreachables for refused connects");
104
105 struct inpcbhead udb; /* from udp_var.h */
106 #define udb6 udb /* for KAME src sync over BSD*'s */
107 struct inpcbinfo udbinfo;
108
109 #ifndef UDBHASHSIZE
110 #define UDBHASHSIZE 16
111 #endif
112
113 struct udpstat udpstat; /* from udp_var.h */
114 SYSCTL_STRUCT(_net_inet_udp, UDPCTL_STATS, stats, CTLFLAG_RW,
115 &udpstat, udpstat, "UDP statistics (struct udpstat, netinet/udp_var.h)");
116
117 static struct sockaddr_in udp_in = { sizeof(udp_in), AF_INET };
118 #ifdef INET6
119 struct udp_in6 {
120 struct sockaddr_in6 uin6_sin;
121 u_char uin6_init_done : 1;
122 } udp_in6 = {
123 { sizeof(udp_in6.uin6_sin), AF_INET6 },
124 0
125 };
126 struct udp_ip6 {
127 struct ip6_hdr uip6_ip6;
128 u_char uip6_init_done : 1;
129 } udp_ip6;
130 #endif /* INET6 */
131
132 static void udp_append __P((struct inpcb *last, struct ip *ip,
133 struct mbuf *n, int off));
134 #ifdef INET6
135 static void ip_2_ip6_hdr __P((struct ip6_hdr *ip6, struct ip *ip));
136 #endif
137
138 static int udp_detach __P((struct socket *so));
139 static int udp_output __P((struct inpcb *, struct mbuf *, struct sockaddr *,
140 struct mbuf *, struct proc *));
141
142 void
143 udp_init()
144 {
145 LIST_INIT(&udb);
146 udbinfo.listhead = &udb;
147 udbinfo.hashbase = hashinit(UDBHASHSIZE, M_PCB, &udbinfo.hashmask);
148 udbinfo.porthashbase = hashinit(UDBHASHSIZE, M_PCB,
149 &udbinfo.porthashmask);
150 udbinfo.ipi_zone = zinit("udpcb", sizeof(struct inpcb), maxsockets,
151 ZONE_INTERRUPT, 0);
152 }
153
154 void
155 udp_input(m, off, proto)
156 register struct mbuf *m;
157 int off, proto;
158 {
159 int iphlen = off;
160 register struct ip *ip;
161 register struct udphdr *uh;
162 register struct inpcb *inp;
163 struct mbuf *opts = 0;
164 int len;
165 struct ip save_ip;
166 struct sockaddr *append_sa;
167
168 udpstat.udps_ipackets++;
169
170 /*
171 * Strip IP options, if any; should skip this,
172 * make available to user, and use on returned packets,
173 * but we don't yet have a way to check the checksum
174 * with options still present.
175 */
176 if (iphlen > sizeof (struct ip)) {
177 ip_stripoptions(m, (struct mbuf *)0);
178 iphlen = sizeof(struct ip);
179 }
180
181 /*
182 * Get IP and UDP header together in first mbuf.
183 */
184 ip = mtod(m, struct ip *);
185 if (m->m_len < iphlen + sizeof(struct udphdr)) {
186 if ((m = m_pullup(m, iphlen + sizeof(struct udphdr))) == 0) {
187 udpstat.udps_hdrops++;
188 return;
189 }
190 ip = mtod(m, struct ip *);
191 }
192 uh = (struct udphdr *)((caddr_t)ip + iphlen);
193
194 /* destination port of 0 is illegal, based on RFC768. */
195 if (uh->uh_dport == 0)
196 goto bad;
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 bad;
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 save_ip = *ip;
216
217 /*
218 * Checksum extended UDP header and data.
219 */
220 if (uh->uh_sum) {
221 u_short uh_sum;
222 if (m->m_pkthdr.csum_flags & CSUM_DATA_VALID) {
223 if (m->m_pkthdr.csum_flags & CSUM_PSEUDO_HDR)
224 uh_sum = m->m_pkthdr.csum_data;
225 else
226 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_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_sum = in_cksum(m, len + sizeof (struct ip));
236 bcopy(b, ((struct ipovly *)ip)->ih_x1, 9);
237 }
238 if (uh_sum) {
239 udpstat.udps_badsum++;
240 m_freem(m);
241 return;
242 }
243 } else
244 udpstat.udps_nosum++;
245
246 if (IN_MULTICAST(ntohl(ip->ip_dst.s_addr)) ||
247 in_broadcast(ip->ip_dst, m->m_pkthdr.rcvif)) {
248 struct inpcb *last;
249 /*
250 * Deliver a multicast or broadcast datagram to *all* sockets
251 * for which the local and remote addresses and ports match
252 * those of the incoming datagram. This allows more than
253 * one process to receive multi/broadcasts on the same port.
254 * (This really ought to be done for unicast datagrams as
255 * well, but that would cause problems with existing
256 * applications that open both address-specific sockets and
257 * a wildcard socket listening to the same port -- they would
258 * end up receiving duplicates of every unicast datagram.
259 * Those applications open the multiple sockets to overcome an
260 * inadequacy of the UDP socket interface, but for backwards
261 * compatibility we avoid the problem here rather than
262 * fixing the interface. Maybe 4.5BSD will remedy this?)
263 */
264
265 /*
266 * Construct sockaddr format source address.
267 */
268 udp_in.sin_port = uh->uh_sport;
269 udp_in.sin_addr = ip->ip_src;
270 /*
271 * Locate pcb(s) for datagram.
272 * (Algorithm copied from raw_intr().)
273 */
274 last = NULL;
275 #ifdef INET6
276 udp_in6.uin6_init_done = udp_ip6.uip6_init_done = 0;
277 #endif
278 LIST_FOREACH(inp, &udb, inp_list) {
279 #ifdef INET6
280 if ((inp->inp_vflag & INP_IPV4) == 0)
281 continue;
282 #endif
283 if (inp->inp_lport != uh->uh_dport)
284 continue;
285 if (inp->inp_laddr.s_addr != INADDR_ANY) {
286 if (inp->inp_laddr.s_addr !=
287 ip->ip_dst.s_addr)
288 continue;
289 }
290 if (inp->inp_faddr.s_addr != INADDR_ANY) {
291 if (inp->inp_faddr.s_addr !=
292 ip->ip_src.s_addr ||
293 inp->inp_fport != uh->uh_sport)
294 continue;
295 }
296
297 if (last != NULL) {
298 struct mbuf *n;
299
300 #ifdef IPSEC
301 /* check AH/ESP integrity. */
302 if (ipsec4_in_reject_so(m, last->inp_socket))
303 ipsecstat.in_polvio++;
304 /* do not inject data to pcb */
305 else
306 #endif /*IPSEC*/
307 #ifdef FAST_IPSEC
308 /* check AH/ESP integrity. */
309 if (ipsec4_in_reject(m, last))
310 ;
311 else
312 #endif /*FAST_IPSEC*/
313 if ((n = m_copy(m, 0, M_COPYALL)) != NULL)
314 udp_append(last, ip, n,
315 iphlen +
316 sizeof(struct udphdr));
317 }
318 last = inp;
319 /*
320 * Don't look for additional matches if this one does
321 * not have either the SO_REUSEPORT or SO_REUSEADDR
322 * socket options set. This heuristic avoids searching
323 * through all pcbs in the common case of a non-shared
324 * port. It * assumes that an application will never
325 * clear these options after setting them.
326 */
327 if ((last->inp_socket->so_options&(SO_REUSEPORT|SO_REUSEADDR)) == 0)
328 break;
329 }
330
331 if (last == NULL) {
332 /*
333 * No matching pcb found; discard datagram.
334 * (No need to send an ICMP Port Unreachable
335 * for a broadcast or multicast datgram.)
336 */
337 udpstat.udps_noportbcast++;
338 goto bad;
339 }
340 #ifdef IPSEC
341 /* check AH/ESP integrity. */
342 if (ipsec4_in_reject_so(m, last->inp_socket)) {
343 ipsecstat.in_polvio++;
344 goto bad;
345 }
346 #endif /*IPSEC*/
347 #ifdef FAST_IPSEC
348 /* check AH/ESP integrity. */
349 if (ipsec4_in_reject(m, last))
350 goto bad;
351 #endif /*FAST_IPSEC*/
352 udp_append(last, ip, m, iphlen + sizeof(struct udphdr));
353 return;
354 }
355 /*
356 * Locate pcb for datagram.
357 */
358 inp = in_pcblookup_hash(&udbinfo, ip->ip_src, uh->uh_sport,
359 ip->ip_dst, uh->uh_dport, 1, m->m_pkthdr.rcvif);
360 if (inp == NULL) {
361 if (log_in_vain) {
362 char buf[4*sizeof "123"];
363
364 strcpy(buf, inet_ntoa(ip->ip_dst));
365 log(LOG_INFO,
366 "Connection attempt to UDP %s:%d from %s:%d\n",
367 buf, ntohs(uh->uh_dport), inet_ntoa(ip->ip_src),
368 ntohs(uh->uh_sport));
369 }
370 udpstat.udps_noport++;
371 if (m->m_flags & (M_BCAST | M_MCAST)) {
372 udpstat.udps_noportbcast++;
373 goto bad;
374 }
375 if (blackhole)
376 goto bad;
377 #ifdef ICMP_BANDLIM
378 if (badport_bandlim(BANDLIM_ICMP_UNREACH) < 0)
379 goto bad;
380 #endif
381 *ip = save_ip;
382 ip->ip_len += iphlen;
383 icmp_error(m, ICMP_UNREACH, ICMP_UNREACH_PORT, 0, 0);
384 return;
385 }
386 #ifdef IPSEC
387 if (ipsec4_in_reject_so(m, inp->inp_socket)) {
388 ipsecstat.in_polvio++;
389 goto bad;
390 }
391 #endif /*IPSEC*/
392 #ifdef FAST_IPSEC
393 if (ipsec4_in_reject(m, inp))
394 goto bad;
395 #endif /*FAST_IPSEC*/
396
397 /*
398 * Construct sockaddr format source address.
399 * Stuff source address and datagram in user buffer.
400 */
401 udp_in.sin_port = uh->uh_sport;
402 udp_in.sin_addr = ip->ip_src;
403 if (inp->inp_flags & INP_CONTROLOPTS
404 || inp->inp_socket->so_options & SO_TIMESTAMP) {
405 #ifdef INET6
406 if (inp->inp_vflag & INP_IPV6) {
407 int savedflags;
408
409 ip_2_ip6_hdr(&udp_ip6.uip6_ip6, ip);
410 savedflags = inp->inp_flags;
411 inp->inp_flags &= ~INP_UNMAPPABLEOPTS;
412 ip6_savecontrol(inp, &opts, &udp_ip6.uip6_ip6, m);
413 inp->inp_flags = savedflags;
414 } else
415 #endif
416 ip_savecontrol(inp, &opts, ip, m);
417 }
418 m_adj(m, iphlen + sizeof(struct udphdr));
419 #ifdef INET6
420 if (inp->inp_vflag & INP_IPV6) {
421 in6_sin_2_v4mapsin6(&udp_in, &udp_in6.uin6_sin);
422 append_sa = (struct sockaddr *)&udp_in6;
423 } else
424 #endif
425 append_sa = (struct sockaddr *)&udp_in;
426 if (sbappendaddr(&inp->inp_socket->so_rcv, append_sa, m, opts) == 0) {
427 udpstat.udps_fullsock++;
428 goto bad;
429 }
430 sorwakeup(inp->inp_socket);
431 return;
432 bad:
433 m_freem(m);
434 if (opts)
435 m_freem(opts);
436 return;
437 }
438
439 #ifdef INET6
440 static void
441 ip_2_ip6_hdr(ip6, ip)
442 struct ip6_hdr *ip6;
443 struct ip *ip;
444 {
445 bzero(ip6, sizeof(*ip6));
446
447 ip6->ip6_vfc = IPV6_VERSION;
448 ip6->ip6_plen = ip->ip_len;
449 ip6->ip6_nxt = ip->ip_p;
450 ip6->ip6_hlim = ip->ip_ttl;
451 ip6->ip6_src.s6_addr32[2] = ip6->ip6_dst.s6_addr32[2] =
452 IPV6_ADDR_INT32_SMP;
453 ip6->ip6_src.s6_addr32[3] = ip->ip_src.s_addr;
454 ip6->ip6_dst.s6_addr32[3] = ip->ip_dst.s_addr;
455 }
456 #endif
457
458 /*
459 * subroutine of udp_input(), mainly for source code readability.
460 * caller must properly init udp_ip6 and udp_in6 beforehand.
461 */
462 static void
463 udp_append(last, ip, n, off)
464 struct inpcb *last;
465 struct ip *ip;
466 struct mbuf *n;
467 int off;
468 {
469 struct sockaddr *append_sa;
470 struct mbuf *opts = 0;
471
472 if (last->inp_flags & INP_CONTROLOPTS ||
473 last->inp_socket->so_options & SO_TIMESTAMP) {
474 #ifdef INET6
475 if (last->inp_vflag & INP_IPV6) {
476 int savedflags;
477
478 if (udp_ip6.uip6_init_done == 0) {
479 ip_2_ip6_hdr(&udp_ip6.uip6_ip6, ip);
480 udp_ip6.uip6_init_done = 1;
481 }
482 savedflags = last->inp_flags;
483 last->inp_flags &= ~INP_UNMAPPABLEOPTS;
484 ip6_savecontrol(last, &opts, &udp_ip6.uip6_ip6, n);
485 last->inp_flags = savedflags;
486 } else
487 #endif
488 ip_savecontrol(last, &opts, ip, n);
489 }
490 #ifdef INET6
491 if (last->inp_vflag & INP_IPV6) {
492 if (udp_in6.uin6_init_done == 0) {
493 in6_sin_2_v4mapsin6(&udp_in, &udp_in6.uin6_sin);
494 udp_in6.uin6_init_done = 1;
495 }
496 append_sa = (struct sockaddr *)&udp_in6.uin6_sin;
497 } else
498 #endif
499 append_sa = (struct sockaddr *)&udp_in;
500 m_adj(n, off);
501 if (sbappendaddr(&last->inp_socket->so_rcv, append_sa, n, opts) == 0) {
502 m_freem(n);
503 if (opts)
504 m_freem(opts);
505 udpstat.udps_fullsock++;
506 } else
507 sorwakeup(last->inp_socket);
508 }
509
510 /*
511 * Notify a udp user of an asynchronous error;
512 * just wake up so that he can collect error status.
513 */
514 void
515 udp_notify(inp, errno)
516 register struct inpcb *inp;
517 int errno;
518 {
519 inp->inp_socket->so_error = errno;
520 sorwakeup(inp->inp_socket);
521 sowwakeup(inp->inp_socket);
522 }
523
524 void
525 udp_ctlinput(cmd, sa, vip)
526 int cmd;
527 struct sockaddr *sa;
528 void *vip;
529 {
530 struct ip *ip = vip;
531 struct udphdr *uh;
532 void (*notify) __P((struct inpcb *, int)) = udp_notify;
533 struct in_addr faddr;
534 struct inpcb *inp;
535 int s;
536
537 faddr = ((struct sockaddr_in *)sa)->sin_addr;
538 if (sa->sa_family != AF_INET || faddr.s_addr == INADDR_ANY)
539 return;
540
541 if (PRC_IS_REDIRECT(cmd)) {
542 ip = 0;
543 notify = in_rtchange;
544 } else if (cmd == PRC_HOSTDEAD)
545 ip = 0;
546 else if ((unsigned)cmd >= PRC_NCMDS || inetctlerrmap[cmd] == 0)
547 return;
548 if (ip) {
549 s = splnet();
550 uh = (struct udphdr *)((caddr_t)ip + (ip->ip_hl << 2));
551 inp = in_pcblookup_hash(&udbinfo, faddr, uh->uh_dport,
552 ip->ip_src, uh->uh_sport, 0, NULL);
553 if (inp != NULL && inp->inp_socket != NULL)
554 (*notify)(inp, inetctlerrmap[cmd]);
555 splx(s);
556 } else
557 in_pcbnotifyall(&udb, faddr, inetctlerrmap[cmd], notify);
558 }
559
560 static int
561 udp_pcblist(SYSCTL_HANDLER_ARGS)
562 {
563 int error, i, n, s;
564 struct inpcb *inp, **inp_list;
565 inp_gen_t gencnt;
566 struct xinpgen xig;
567
568 /*
569 * The process of preparing the TCB list is too time-consuming and
570 * resource-intensive to repeat twice on every request.
571 */
572 if (req->oldptr == 0) {
573 n = udbinfo.ipi_count;
574 req->oldidx = 2 * (sizeof xig)
575 + (n + n/8) * sizeof(struct xinpcb);
576 return 0;
577 }
578
579 if (req->newptr != 0)
580 return EPERM;
581
582 /*
583 * OK, now we're committed to doing something.
584 */
585 s = splnet();
586 gencnt = udbinfo.ipi_gencnt;
587 n = udbinfo.ipi_count;
588 splx(s);
589
590 xig.xig_len = sizeof xig;
591 xig.xig_count = n;
592 xig.xig_gen = gencnt;
593 xig.xig_sogen = so_gencnt;
594 error = SYSCTL_OUT(req, &xig, sizeof xig);
595 if (error)
596 return error;
597
598 inp_list = malloc(n * sizeof *inp_list, M_TEMP, M_WAITOK);
599 if (inp_list == 0)
600 return ENOMEM;
601
602 s = splnet();
603 for (inp = LIST_FIRST(udbinfo.listhead), i = 0; inp && i < n;
604 inp = LIST_NEXT(inp, inp_list)) {
605 if (inp->inp_gencnt <= gencnt && !prison_xinpcb(req->p, inp))
606 inp_list[i++] = inp;
607 }
608 splx(s);
609 n = i;
610
611 error = 0;
612 for (i = 0; i < n; i++) {
613 inp = inp_list[i];
614 if (inp->inp_gencnt <= gencnt) {
615 struct xinpcb xi;
616 bzero(&xi, sizeof(xi));
617 xi.xi_len = sizeof xi;
618 /* XXX should avoid extra copy */
619 bcopy(inp, &xi.xi_inp, sizeof *inp);
620 if (inp->inp_socket)
621 sotoxsocket(inp->inp_socket, &xi.xi_socket);
622 error = SYSCTL_OUT(req, &xi, sizeof xi);
623 }
624 }
625 if (!error) {
626 /*
627 * Give the user an updated idea of our state.
628 * If the generation differs from what we told
629 * her before, she knows that something happened
630 * while we were processing this request, and it
631 * might be necessary to retry.
632 */
633 s = splnet();
634 xig.xig_gen = udbinfo.ipi_gencnt;
635 xig.xig_sogen = so_gencnt;
636 xig.xig_count = udbinfo.ipi_count;
637 splx(s);
638 error = SYSCTL_OUT(req, &xig, sizeof xig);
639 }
640 free(inp_list, M_TEMP);
641 return error;
642 }
643
644 SYSCTL_PROC(_net_inet_udp, UDPCTL_PCBLIST, pcblist, CTLFLAG_RD, 0, 0,
645 udp_pcblist, "S,xinpcb", "List of active UDP sockets");
646
647 static int
648 udp_getcred(SYSCTL_HANDLER_ARGS)
649 {
650 struct sockaddr_in addrs[2];
651 struct inpcb *inp;
652 int error, s;
653
654 error = suser(req->p);
655 if (error)
656 return (error);
657 error = SYSCTL_IN(req, addrs, sizeof(addrs));
658 if (error)
659 return (error);
660 s = splnet();
661 inp = in_pcblookup_hash(&udbinfo, addrs[1].sin_addr, addrs[1].sin_port,
662 addrs[0].sin_addr, addrs[0].sin_port, 1, NULL);
663 if (inp == NULL || inp->inp_socket == NULL) {
664 error = ENOENT;
665 goto out;
666 }
667 error = SYSCTL_OUT(req, inp->inp_socket->so_cred, sizeof(struct ucred));
668 out:
669 splx(s);
670 return (error);
671 }
672
673 SYSCTL_PROC(_net_inet_udp, OID_AUTO, getcred, CTLTYPE_OPAQUE|CTLFLAG_RW,
674 0, 0, udp_getcred, "S,ucred", "Get the ucred of a UDP connection");
675
676 static int
677 udp_output(inp, m, addr, control, p)
678 register struct inpcb *inp;
679 struct mbuf *m;
680 struct sockaddr *addr;
681 struct mbuf *control;
682 struct proc *p;
683 {
684 register struct udpiphdr *ui;
685 register int len = m->m_pkthdr.len;
686 struct in_addr faddr, laddr;
687 struct sockaddr_in *sin;
688 int s = 0, error = 0;
689 int ipflags;
690
691 if (control)
692 m_freem(control); /* XXX */
693
694 if (len + sizeof(struct udpiphdr) > IP_MAXPACKET) {
695 error = EMSGSIZE;
696 goto release;
697 }
698
699 if (addr) {
700 sin = (struct sockaddr_in *)addr;
701 prison_remote_ip(p, 0, &sin->sin_addr.s_addr);
702 laddr = inp->inp_laddr;
703 if (inp->inp_faddr.s_addr != INADDR_ANY) {
704 error = EISCONN;
705 goto release;
706 }
707 /*
708 * Must block input while temporarily connected.
709 */
710 s = splnet();
711 error = in_pcbconnect(inp, addr, p);
712 if (error) {
713 splx(s);
714 goto release;
715 }
716 } else {
717 if (inp->inp_faddr.s_addr == INADDR_ANY) {
718 error = ENOTCONN;
719 goto release;
720 }
721 }
722 /*
723 * Calculate data length and get a mbuf
724 * for UDP and IP headers.
725 */
726 M_PREPEND(m, sizeof(struct udpiphdr), M_DONTWAIT);
727 if (m == 0) {
728 error = ENOBUFS;
729 if (addr)
730 splx(s);
731 goto release;
732 }
733
734 /*
735 * Fill in mbuf with extended UDP header
736 * and addresses and length put into network format.
737 */
738 ui = mtod(m, struct udpiphdr *);
739 bzero(ui->ui_x1, sizeof(ui->ui_x1)); /* XXX still needed? */
740 ui->ui_pr = IPPROTO_UDP;
741 ui->ui_src = inp->inp_laddr;
742 ui->ui_dst = inp->inp_faddr;
743 ui->ui_sport = inp->inp_lport;
744 ui->ui_dport = inp->inp_fport;
745 ui->ui_ulen = htons((u_short)len + sizeof(struct udphdr));
746
747 ipflags = inp->inp_socket->so_options & (SO_DONTROUTE | SO_BROADCAST);
748 if (inp->inp_flags & INP_ONESBCAST)
749 ipflags |= IP_SENDONES;
750
751 /*
752 * Set up checksum and output datagram.
753 */
754 if (udpcksum) {
755 faddr = ui->ui_dst;
756 if (inp->inp_flags & INP_ONESBCAST)
757 faddr.s_addr = INADDR_BROADCAST;
758 ui->ui_sum = in_pseudo(ui->ui_src.s_addr, faddr.s_addr,
759 htons((u_short)len + sizeof(struct udphdr) + IPPROTO_UDP));
760 m->m_pkthdr.csum_flags = CSUM_UDP;
761 m->m_pkthdr.csum_data = offsetof(struct udphdr, uh_sum);
762 } else {
763 ui->ui_sum = 0;
764 }
765 ((struct ip *)ui)->ip_len = sizeof (struct udpiphdr) + len;
766 ((struct ip *)ui)->ip_ttl = inp->inp_ip_ttl; /* XXX */
767 ((struct ip *)ui)->ip_tos = inp->inp_ip_tos; /* XXX */
768 udpstat.udps_opackets++;
769
770 error = ip_output(m, inp->inp_options, &inp->inp_route, ipflags,
771 inp->inp_moptions, inp);
772
773 if (addr) {
774 in_pcbdisconnect(inp);
775 inp->inp_laddr = laddr; /* XXX rehash? */
776 splx(s);
777 }
778 return (error);
779
780 release:
781 m_freem(m);
782 return (error);
783 }
784
785 u_long udp_sendspace = 9216; /* really max datagram size */
786 /* 40 1K datagrams */
787 SYSCTL_INT(_net_inet_udp, UDPCTL_MAXDGRAM, maxdgram, CTLFLAG_RW,
788 &udp_sendspace, 0, "Maximum outgoing UDP datagram size");
789
790 u_long udp_recvspace = 40 * (1024 +
791 #ifdef INET6
792 sizeof(struct sockaddr_in6)
793 #else
794 sizeof(struct sockaddr_in)
795 #endif
796 );
797 SYSCTL_INT(_net_inet_udp, UDPCTL_RECVSPACE, recvspace, CTLFLAG_RW,
798 &udp_recvspace, 0, "Maximum incoming UDP datagram size");
799
800 static int
801 udp_abort(struct socket *so)
802 {
803 struct inpcb *inp;
804 int s;
805
806 inp = sotoinpcb(so);
807 if (inp == 0)
808 return EINVAL; /* ??? possible? panic instead? */
809 soisdisconnected(so);
810 s = splnet();
811 in_pcbdetach(inp);
812 splx(s);
813 return 0;
814 }
815
816 static int
817 udp_attach(struct socket *so, int proto, struct proc *p)
818 {
819 struct inpcb *inp;
820 int s, error;
821
822 inp = sotoinpcb(so);
823 if (inp != 0)
824 return EINVAL;
825
826 error = soreserve(so, udp_sendspace, udp_recvspace);
827 if (error)
828 return error;
829 s = splnet();
830 error = in_pcballoc(so, &udbinfo, p);
831 splx(s);
832 if (error)
833 return error;
834
835 inp = (struct inpcb *)so->so_pcb;
836 inp->inp_vflag |= INP_IPV4;
837 inp->inp_ip_ttl = ip_defttl;
838 return 0;
839 }
840
841 static int
842 udp_bind(struct socket *so, struct sockaddr *nam, struct proc *p)
843 {
844 struct inpcb *inp;
845 int s, error;
846
847 inp = sotoinpcb(so);
848 if (inp == 0)
849 return EINVAL;
850 s = splnet();
851 error = in_pcbbind(inp, nam, p);
852 splx(s);
853 return error;
854 }
855
856 static int
857 udp_connect(struct socket *so, struct sockaddr *nam, struct proc *p)
858 {
859 struct inpcb *inp;
860 int s, error;
861 struct sockaddr_in *sin;
862
863 inp = sotoinpcb(so);
864 if (inp == 0)
865 return EINVAL;
866 if (inp->inp_faddr.s_addr != INADDR_ANY)
867 return EISCONN;
868 error = 0;
869 s = splnet();
870 if (inp->inp_laddr.s_addr == INADDR_ANY && p->p_prison != NULL)
871 error = in_pcbbind(inp, NULL, p);
872 if (error == 0) {
873 sin = (struct sockaddr_in *)nam;
874 prison_remote_ip(p, 0, &sin->sin_addr.s_addr);
875 error = in_pcbconnect(inp, nam, p);
876 }
877 splx(s);
878 if (error == 0)
879 soisconnected(so);
880 return error;
881 }
882
883 static int
884 udp_detach(struct socket *so)
885 {
886 struct inpcb *inp;
887 int s;
888
889 inp = sotoinpcb(so);
890 if (inp == 0)
891 return EINVAL;
892 s = splnet();
893 in_pcbdetach(inp);
894 splx(s);
895 return 0;
896 }
897
898 static int
899 udp_disconnect(struct socket *so)
900 {
901 struct inpcb *inp;
902 int s;
903
904 inp = sotoinpcb(so);
905 if (inp == 0)
906 return EINVAL;
907 if (inp->inp_faddr.s_addr == INADDR_ANY)
908 return ENOTCONN;
909
910 s = splnet();
911 in_pcbdisconnect(inp);
912 inp->inp_laddr.s_addr = INADDR_ANY;
913 splx(s);
914 so->so_state &= ~SS_ISCONNECTED; /* XXX */
915 return 0;
916 }
917
918 static int
919 udp_send(struct socket *so, int flags, struct mbuf *m, struct sockaddr *addr,
920 struct mbuf *control, struct proc *p)
921 {
922 struct inpcb *inp;
923
924 inp = sotoinpcb(so);
925 if (inp == 0) {
926 m_freem(m);
927 return EINVAL;
928 }
929 return udp_output(inp, m, addr, control, p);
930 }
931
932 int
933 udp_shutdown(struct socket *so)
934 {
935 struct inpcb *inp;
936
937 inp = sotoinpcb(so);
938 if (inp == 0)
939 return EINVAL;
940 socantsendmore(so);
941 return 0;
942 }
943
944 struct pr_usrreqs udp_usrreqs = {
945 udp_abort, pru_accept_notsupp, udp_attach, udp_bind, udp_connect,
946 pru_connect2_notsupp, in_control, udp_detach, udp_disconnect,
947 pru_listen_notsupp, in_setpeeraddr, pru_rcvd_notsupp,
948 pru_rcvoob_notsupp, udp_send, pru_sense_null, udp_shutdown,
949 in_setsockaddr, sosend, soreceive, sopoll
950 };
951
Cache object: 11d3306c4a5872006df397f6ebf599e5
|