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 * $Id: udp_usrreq.c,v 1.17 1995/12/06 23:37:44 bde Exp $
35 */
36
37#include <sys/param.h>
38#include <sys/systm.h>
39#include <sys/malloc.h>
40#include <sys/mbuf.h>
41#include <sys/protosw.h>
42#include <sys/socket.h>
43#include <sys/socketvar.h>
44#include <sys/errno.h>
45#include <sys/stat.h>
46#include <sys/queue.h>
47#include <sys/kernel.h>
48#include <sys/sysctl.h>
49
50#include <net/if.h>
51#include <net/route.h>
52
53#include <netinet/in.h>
54#include <netinet/in_systm.h>
55#include <netinet/ip.h>
56#include <netinet/in_pcb.h>
57#include <netinet/in_var.h>
58#include <netinet/ip_var.h>
59#include <netinet/ip_icmp.h>
60#include <netinet/udp.h>
61#include <netinet/udp_var.h>
62
63/*
64 * UDP protocol implementation.
65 * Per RFC 768, August, 1980.
66 */
67#ifndef COMPAT_42
68static int udpcksum = 1;
69#else
70static int udpcksum = 0; /* XXX */
71#endif
72SYSCTL_INT(_net_inet_udp, UDPCTL_CHECKSUM, checksum, CTLFLAG_RW,
73 &udpcksum, 0, "");
74
75static struct inpcbhead udb; /* from udp_var.h */
76static struct inpcbinfo udbinfo;
77
78#ifndef UDBHASHSIZE
79#define UDBHASHSIZE 64
80#endif
81
82static struct udpstat udpstat; /* from udp_var.h */
83SYSCTL_STRUCT(_net_inet_udp, UDPCTL_STATS, stats, CTLFLAG_RD,
84 &udpstat, udpstat, "");
85
86static struct sockaddr_in udp_in = { sizeof(udp_in), AF_INET };
87
88static void udp_detach __P((struct inpcb *));
89static int udp_output __P((struct inpcb *, struct mbuf *, struct mbuf *,
90 struct mbuf *));
91static void udp_notify __P((struct inpcb *, int));
92static struct mbuf *udp_saveopt __P((caddr_t, int, int));
93
94void
95udp_init()
96{
97 LIST_INIT(&udb);
98 udbinfo.listhead = &udb;
99 udbinfo.hashbase = phashinit(UDBHASHSIZE, M_PCB, &udbinfo.hashsize);
100}
101
102void
103udp_input(m, iphlen)
104 register struct mbuf *m;
105 int iphlen;
106{
107 register struct ip *ip;
108 register struct udphdr *uh;
109 register struct inpcb *inp;
110 struct mbuf *opts = 0;
111 int len;
112 struct ip save_ip;
113
114 udpstat.udps_ipackets++;
115
116 /*
117 * Strip IP options, if any; should skip this,
118 * make available to user, and use on returned packets,
119 * but we don't yet have a way to check the checksum
120 * with options still present.
121 */
122 if (iphlen > sizeof (struct ip)) {
123 ip_stripoptions(m, (struct mbuf *)0);
124 iphlen = sizeof(struct ip);
125 }
126
127 /*
128 * Get IP and UDP header together in first mbuf.
129 */
130 ip = mtod(m, struct ip *);
131 if (m->m_len < iphlen + sizeof(struct udphdr)) {
132 if ((m = m_pullup(m, iphlen + sizeof(struct udphdr))) == 0) {
133 udpstat.udps_hdrops++;
134 return;
135 }
136 ip = mtod(m, struct ip *);
137 }
138 uh = (struct udphdr *)((caddr_t)ip + iphlen);
139
140 /*
141 * Make mbuf data length reflect UDP length.
142 * If not enough data to reflect UDP length, drop.
143 */
144 len = ntohs((u_short)uh->uh_ulen);
145 if (ip->ip_len != len) {
146 if (len > ip->ip_len || len < sizeof(struct udphdr)) {
147 udpstat.udps_badlen++;
148 goto bad;
149 }
150 m_adj(m, len - ip->ip_len);
151 /* ip->ip_len = len; */
152 }
153 /*
154 * Save a copy of the IP header in case we want restore it
155 * for sending an ICMP error message in response.
156 */
157 save_ip = *ip;
158
159 /*
160 * Checksum extended UDP header and data.
161 */
162 if (uh->uh_sum) {
163 ((struct ipovly *)ip)->ih_next = 0;
164 ((struct ipovly *)ip)->ih_prev = 0;
165 ((struct ipovly *)ip)->ih_x1 = 0;
166 ((struct ipovly *)ip)->ih_len = uh->uh_ulen;
167 uh->uh_sum = in_cksum(m, len + sizeof (struct ip));
168 if (uh->uh_sum) {
169 udpstat.udps_badsum++;
170 m_freem(m);
171 return;
172 }
173 }
174
175 if (IN_MULTICAST(ntohl(ip->ip_dst.s_addr)) ||
176 in_broadcast(ip->ip_dst, m->m_pkthdr.rcvif)) {
177 struct socket *last;
178 /*
179 * Deliver a multicast or broadcast datagram to *all* sockets
180 * for which the local and remote addresses and ports match
181 * those of the incoming datagram. This allows more than
182 * one process to receive multi/broadcasts on the same port.
183 * (This really ought to be done for unicast datagrams as
184 * well, but that would cause problems with existing
185 * applications that open both address-specific sockets and
186 * a wildcard socket listening to the same port -- they would
187 * end up receiving duplicates of every unicast datagram.
188 * Those applications open the multiple sockets to overcome an
189 * inadequacy of the UDP socket interface, but for backwards
190 * compatibility we avoid the problem here rather than
191 * fixing the interface. Maybe 4.5BSD will remedy this?)
192 */
193
194 /*
195 * Construct sockaddr format source address.
196 */
197 udp_in.sin_port = uh->uh_sport;
198 udp_in.sin_addr = ip->ip_src;
199 m->m_len -= sizeof (struct udpiphdr);
200 m->m_data += sizeof (struct udpiphdr);
201 /*
202 * Locate pcb(s) for datagram.
203 * (Algorithm copied from raw_intr().)
204 */
205 last = NULL;
206 for (inp = udb.lh_first; inp != NULL; inp = inp->inp_list.le_next) {
207 if (inp->inp_lport != uh->uh_dport)
208 continue;
209 if (inp->inp_laddr.s_addr != INADDR_ANY) {
210 if (inp->inp_laddr.s_addr !=
211 ip->ip_dst.s_addr)
212 continue;
213 }
214 if (inp->inp_faddr.s_addr != INADDR_ANY) {
215 if (inp->inp_faddr.s_addr !=
216 ip->ip_src.s_addr ||
217 inp->inp_fport != uh->uh_sport)
218 continue;
219 }
220
221 if (last != NULL) {
222 struct mbuf *n;
223
224 if ((n = m_copy(m, 0, M_COPYALL)) != NULL) {
225 if (sbappendaddr(&last->so_rcv,
226 (struct sockaddr *)&udp_in,
227 n, (struct mbuf *)0) == 0) {
228 m_freem(n);
229 udpstat.udps_fullsock++;
230 } else
231 sorwakeup(last);
232 }
233 }
234 last = inp->inp_socket;
235 /*
236 * Don't look for additional matches if this one does
237 * not have either the SO_REUSEPORT or SO_REUSEADDR
238 * socket options set. This heuristic avoids searching
239 * through all pcbs in the common case of a non-shared
240 * port. It * assumes that an application will never
241 * clear these options after setting them.
242 */
243 if ((last->so_options&(SO_REUSEPORT|SO_REUSEADDR) == 0))
244 break;
245 }
246
247 if (last == NULL) {
248 /*
249 * No matching pcb found; discard datagram.
250 * (No need to send an ICMP Port Unreachable
251 * for a broadcast or multicast datgram.)
252 */
253 udpstat.udps_noportbcast++;
254 goto bad;
255 }
256 if (sbappendaddr(&last->so_rcv, (struct sockaddr *)&udp_in,
257 m, (struct mbuf *)0) == 0) {
258 udpstat.udps_fullsock++;
259 goto bad;
260 }
261 sorwakeup(last);
262 return;
263 }
264 /*
265 * Locate pcb for datagram. First look for an exact match.
266 */
267 inp = in_pcblookuphash(&udbinfo, ip->ip_src, uh->uh_sport,
268 ip->ip_dst, uh->uh_dport);
269 /*
270 * ...and if that fails, do a wildcard search.
271 */
272 if (inp == NULL) {
273 inp = in_pcblookup(&udb, ip->ip_src, uh->uh_sport, ip->ip_dst,
274 uh->uh_dport, INPLOOKUP_WILDCARD);
275 }
276 if (inp == NULL) {
277 udpstat.udps_noport++;
278 if (m->m_flags & (M_BCAST | M_MCAST)) {
279 udpstat.udps_noportbcast++;
280 goto bad;
281 }
282 *ip = save_ip;
283 icmp_error(m, ICMP_UNREACH, ICMP_UNREACH_PORT, 0, 0);
284 return;
285 }
286
287 /*
288 * Construct sockaddr format source address.
289 * Stuff source address and datagram in user buffer.
290 */
291 udp_in.sin_port = uh->uh_sport;
292 udp_in.sin_addr = ip->ip_src;
293 if (inp->inp_flags & INP_CONTROLOPTS) {
294 struct mbuf **mp = &opts;
295
296 if (inp->inp_flags & INP_RECVDSTADDR) {
297 *mp = udp_saveopt((caddr_t) &ip->ip_dst,
298 sizeof(struct in_addr), IP_RECVDSTADDR);
299 if (*mp)
300 mp = &(*mp)->m_next;
301 }
302#ifdef notyet
303 /* options were tossed above */
304 if (inp->inp_flags & INP_RECVOPTS) {
305 *mp = udp_saveopt((caddr_t) opts_deleted_above,
306 sizeof(struct in_addr), IP_RECVOPTS);
307 if (*mp)
308 mp = &(*mp)->m_next;
309 }
310 /* ip_srcroute doesn't do what we want here, need to fix */
311 if (inp->inp_flags & INP_RECVRETOPTS) {
312 *mp = udp_saveopt((caddr_t) ip_srcroute(),
313 sizeof(struct in_addr), IP_RECVRETOPTS);
314 if (*mp)
315 mp = &(*mp)->m_next;
316 }
317#endif
318 }
319 iphlen += sizeof(struct udphdr);
320 m->m_len -= iphlen;
321 m->m_pkthdr.len -= iphlen;
322 m->m_data += iphlen;
323 if (sbappendaddr(&inp->inp_socket->so_rcv, (struct sockaddr *)&udp_in,
324 m, opts) == 0) {
325 udpstat.udps_fullsock++;
326 goto bad;
327 }
328 sorwakeup(inp->inp_socket);
329 return;
330bad:
331 m_freem(m);
332 if (opts)
333 m_freem(opts);
334}
335
336/*
337 * Create a "control" mbuf containing the specified data
338 * with the specified type for presentation with a datagram.
339 */
340struct mbuf *
341udp_saveopt(p, size, type)
342 caddr_t p;
343 register int size;
344 int type;
345{
346 register struct cmsghdr *cp;
347 struct mbuf *m;
348
349 if ((m = m_get(M_DONTWAIT, MT_CONTROL)) == NULL)
350 return ((struct mbuf *) NULL);
351 cp = (struct cmsghdr *) mtod(m, struct cmsghdr *);
352 (void)memcpy(CMSG_DATA(cp), p, size);
353 size += sizeof(*cp);
354 m->m_len = size;
355 cp->cmsg_len = size;
356 cp->cmsg_level = IPPROTO_IP;
357 cp->cmsg_type = type;
358 return (m);
359}
360
361/*
362 * Notify a udp user of an asynchronous error;
363 * just wake up so that he can collect error status.
364 */
365static void
366udp_notify(inp, errno)
367 register struct inpcb *inp;
368 int errno;
369{
370 inp->inp_socket->so_error = errno;
371 sorwakeup(inp->inp_socket);
372 sowwakeup(inp->inp_socket);
373}
374
375void
376udp_ctlinput(cmd, sa, ip)
377 int cmd;
378 struct sockaddr *sa;
379 register struct ip *ip;
380{
381 register struct udphdr *uh;
382
383 if (!PRC_IS_REDIRECT(cmd) &&
384 ((unsigned)cmd >= PRC_NCMDS || inetctlerrmap[cmd] == 0))
385 return;
386 if (ip) {
387 uh = (struct udphdr *)((caddr_t)ip + (ip->ip_hl << 2));
388 in_pcbnotify(&udb, sa, uh->uh_dport, ip->ip_src, uh->uh_sport,
389 cmd, udp_notify);
390 } else
391 in_pcbnotify(&udb, sa, 0, zeroin_addr, 0, cmd, udp_notify);
392}
393
394static int
395udp_output(inp, m, addr, control)
396 register struct inpcb *inp;
397 register struct mbuf *m;
398 struct mbuf *addr, *control;
399{
400 register struct udpiphdr *ui;
401 register int len = m->m_pkthdr.len;
402 struct in_addr laddr;
403 int s = 0, error = 0;
404
405 if (control)
406 m_freem(control); /* XXX */
407
408 if (addr) {
409 laddr = inp->inp_laddr;
410 if (inp->inp_faddr.s_addr != INADDR_ANY) {
411 error = EISCONN;
412 goto release;
413 }
414 /*
415 * Must block input while temporarily connected.
416 */
417 s = splnet();
418 error = in_pcbconnect(inp, addr);
419 if (error) {
420 splx(s);
421 goto release;
422 }
423 } else {
424 if (inp->inp_faddr.s_addr == INADDR_ANY) {
425 error = ENOTCONN;
426 goto release;
427 }
428 }
429 /*
430 * Calculate data length and get a mbuf
431 * for UDP and IP headers.
432 */
433 M_PREPEND(m, sizeof(struct udpiphdr), M_DONTWAIT);
434 if (m == 0) {
435 error = ENOBUFS;
436 if (addr)
437 splx(s);
438 goto release;
439 }
440
441 /*
442 * Fill in mbuf with extended UDP header
443 * and addresses and length put into network format.
444 */
445 ui = mtod(m, struct udpiphdr *);
446 ui->ui_next = ui->ui_prev = 0;
447 ui->ui_x1 = 0;
448 ui->ui_pr = IPPROTO_UDP;
449 ui->ui_len = htons((u_short)len + sizeof (struct udphdr));
450 ui->ui_src = inp->inp_laddr;
451 ui->ui_dst = inp->inp_faddr;
452 ui->ui_sport = inp->inp_lport;
453 ui->ui_dport = inp->inp_fport;
454 ui->ui_ulen = ui->ui_len;
455
456 /*
457 * Stuff checksum and output datagram.
458 */
459 ui->ui_sum = 0;
460 if (udpcksum) {
461 if ((ui->ui_sum = in_cksum(m, sizeof (struct udpiphdr) + len)) == 0)
462 ui->ui_sum = 0xffff;
463 }
464 ((struct ip *)ui)->ip_len = sizeof (struct udpiphdr) + len;
465 ((struct ip *)ui)->ip_ttl = inp->inp_ip.ip_ttl; /* XXX */
466 ((struct ip *)ui)->ip_tos = inp->inp_ip.ip_tos; /* XXX */
467 udpstat.udps_opackets++;
468 error = ip_output(m, inp->inp_options, &inp->inp_route,
469 inp->inp_socket->so_options & (SO_DONTROUTE | SO_BROADCAST),
470 inp->inp_moptions);
471
472 if (addr) {
473 in_pcbdisconnect(inp);
474 inp->inp_laddr = laddr;
475 splx(s);
476 }
477 return (error);
478
479release:
480 m_freem(m);
481 return (error);
482}
483
484static u_long udp_sendspace = 9216; /* really max datagram size */
485 /* 40 1K datagrams */
486SYSCTL_INT(_net_inet_udp, UDPCTL_MAXDGRAM, maxdgram, CTLFLAG_RW,
487 &udp_sendspace, 0, "");
488
489static u_long udp_recvspace = 40 * (1024 + sizeof(struct sockaddr_in));
490SYSCTL_INT(_net_inet_udp, UDPCTL_RECVSPACE, recvspace, CTLFLAG_RW,
491 &udp_recvspace, 0, "");
492
493/*ARGSUSED*/
494int
495udp_usrreq(so, req, m, addr, control)
496 struct socket *so;
497 int req;
498 struct mbuf *m, *addr, *control;
499{
500 struct inpcb *inp = sotoinpcb(so);
501 int error = 0;
502 int s;
503
504 if (req == PRU_CONTROL)
505 return (in_control(so, (u_long)m, (caddr_t)addr,
506 (struct ifnet *)control));
507 if (inp == NULL && req != PRU_ATTACH) {
508 error = EINVAL;
509 goto release;
510 }
511 /*
512 * Note: need to block udp_input while changing
513 * the udp pcb queue and/or pcb addresses.
514 */
515 switch (req) {
516
517 case PRU_ATTACH:
518 if (inp != NULL) {
519 error = EINVAL;
520 break;
521 }
522 s = splnet();
523 error = in_pcballoc(so, &udbinfo);
524 splx(s);
525 if (error)
526 break;
527 error = soreserve(so, udp_sendspace, udp_recvspace);
528 if (error)
529 break;
530 ((struct inpcb *) so->so_pcb)->inp_ip.ip_ttl = ip_defttl;
531 break;
532
533 case PRU_DETACH:
534 udp_detach(inp);
535 break;
536
537 case PRU_BIND:
538 s = splnet();
539 error = in_pcbbind(inp, addr);
540 splx(s);
541 break;
542
543 case PRU_LISTEN:
544 error = EOPNOTSUPP;
545 break;
546
547 case PRU_CONNECT:
548 if (inp->inp_faddr.s_addr != INADDR_ANY) {
549 error = EISCONN;
550 break;
551 }
552 s = splnet();
553 error = in_pcbconnect(inp, addr);
554 splx(s);
555 if (error == 0)
556 soisconnected(so);
557 break;
558
559 case PRU_CONNECT2:
560 error = EOPNOTSUPP;
561 break;
562
563 case PRU_ACCEPT:
564 error = EOPNOTSUPP;
565 break;
566
567 case PRU_DISCONNECT:
568 if (inp->inp_faddr.s_addr == INADDR_ANY) {
569 error = ENOTCONN;
570 break;
571 }
572 s = splnet();
573 in_pcbdisconnect(inp);
574 inp->inp_laddr.s_addr = INADDR_ANY;
575 splx(s);
576 so->so_state &= ~SS_ISCONNECTED; /* XXX */
577 break;
578
579 case PRU_SHUTDOWN:
580 socantsendmore(so);
581 break;
582
583 case PRU_SEND:
584 return (udp_output(inp, m, addr, control));
585
586 case PRU_ABORT:
587 soisdisconnected(so);
588 udp_detach(inp);
589 break;
590
591 case PRU_SOCKADDR:
592 in_setsockaddr(inp, addr);
593 break;
594
595 case PRU_PEERADDR:
596 in_setpeeraddr(inp, addr);
597 break;
598
599 case PRU_SENSE:
600 /*
601 * stat: don't bother with a blocksize.
602 */
603 return (0);
604
605 case PRU_SENDOOB:
606 case PRU_FASTTIMO:
607 case PRU_SLOWTIMO:
608 case PRU_PROTORCV:
609 case PRU_PROTOSEND:
610 error = EOPNOTSUPP;
611 break;
612
613 case PRU_RCVD:
614 case PRU_RCVOOB:
615 return (EOPNOTSUPP); /* do not free mbuf's */
616
617 default:
618 panic("udp_usrreq");
619 }
620
621release:
622 if (control) {
623 printf("udp control data unexpectedly retained\n");
624 m_freem(control);
625 }
626 if (m)
627 m_freem(m);
628 return (error);
629}
630
631static void
632udp_detach(inp)
633 struct inpcb *inp;
634{
635 int s = splnet();
636
637 in_pcbdetach(inp);
638 splx(s);
639}
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 * $Id: udp_usrreq.c,v 1.17 1995/12/06 23:37:44 bde Exp $
35 */
36
37#include <sys/param.h>
38#include <sys/systm.h>
39#include <sys/malloc.h>
40#include <sys/mbuf.h>
41#include <sys/protosw.h>
42#include <sys/socket.h>
43#include <sys/socketvar.h>
44#include <sys/errno.h>
45#include <sys/stat.h>
46#include <sys/queue.h>
47#include <sys/kernel.h>
48#include <sys/sysctl.h>
49
50#include <net/if.h>
51#include <net/route.h>
52
53#include <netinet/in.h>
54#include <netinet/in_systm.h>
55#include <netinet/ip.h>
56#include <netinet/in_pcb.h>
57#include <netinet/in_var.h>
58#include <netinet/ip_var.h>
59#include <netinet/ip_icmp.h>
60#include <netinet/udp.h>
61#include <netinet/udp_var.h>
62
63/*
64 * UDP protocol implementation.
65 * Per RFC 768, August, 1980.
66 */
67#ifndef COMPAT_42
68static int udpcksum = 1;
69#else
70static int udpcksum = 0; /* XXX */
71#endif
72SYSCTL_INT(_net_inet_udp, UDPCTL_CHECKSUM, checksum, CTLFLAG_RW,
73 &udpcksum, 0, "");
74
75static struct inpcbhead udb; /* from udp_var.h */
76static struct inpcbinfo udbinfo;
77
78#ifndef UDBHASHSIZE
79#define UDBHASHSIZE 64
80#endif
81
82static struct udpstat udpstat; /* from udp_var.h */
83SYSCTL_STRUCT(_net_inet_udp, UDPCTL_STATS, stats, CTLFLAG_RD,
84 &udpstat, udpstat, "");
85
86static struct sockaddr_in udp_in = { sizeof(udp_in), AF_INET };
87
88static void udp_detach __P((struct inpcb *));
89static int udp_output __P((struct inpcb *, struct mbuf *, struct mbuf *,
90 struct mbuf *));
91static void udp_notify __P((struct inpcb *, int));
92static struct mbuf *udp_saveopt __P((caddr_t, int, int));
93
94void
95udp_init()
96{
97 LIST_INIT(&udb);
98 udbinfo.listhead = &udb;
99 udbinfo.hashbase = phashinit(UDBHASHSIZE, M_PCB, &udbinfo.hashsize);
100}
101
102void
103udp_input(m, iphlen)
104 register struct mbuf *m;
105 int iphlen;
106{
107 register struct ip *ip;
108 register struct udphdr *uh;
109 register struct inpcb *inp;
110 struct mbuf *opts = 0;
111 int len;
112 struct ip save_ip;
113
114 udpstat.udps_ipackets++;
115
116 /*
117 * Strip IP options, if any; should skip this,
118 * make available to user, and use on returned packets,
119 * but we don't yet have a way to check the checksum
120 * with options still present.
121 */
122 if (iphlen > sizeof (struct ip)) {
123 ip_stripoptions(m, (struct mbuf *)0);
124 iphlen = sizeof(struct ip);
125 }
126
127 /*
128 * Get IP and UDP header together in first mbuf.
129 */
130 ip = mtod(m, struct ip *);
131 if (m->m_len < iphlen + sizeof(struct udphdr)) {
132 if ((m = m_pullup(m, iphlen + sizeof(struct udphdr))) == 0) {
133 udpstat.udps_hdrops++;
134 return;
135 }
136 ip = mtod(m, struct ip *);
137 }
138 uh = (struct udphdr *)((caddr_t)ip + iphlen);
139
140 /*
141 * Make mbuf data length reflect UDP length.
142 * If not enough data to reflect UDP length, drop.
143 */
144 len = ntohs((u_short)uh->uh_ulen);
145 if (ip->ip_len != len) {
146 if (len > ip->ip_len || len < sizeof(struct udphdr)) {
147 udpstat.udps_badlen++;
148 goto bad;
149 }
150 m_adj(m, len - ip->ip_len);
151 /* ip->ip_len = len; */
152 }
153 /*
154 * Save a copy of the IP header in case we want restore it
155 * for sending an ICMP error message in response.
156 */
157 save_ip = *ip;
158
159 /*
160 * Checksum extended UDP header and data.
161 */
162 if (uh->uh_sum) {
163 ((struct ipovly *)ip)->ih_next = 0;
164 ((struct ipovly *)ip)->ih_prev = 0;
165 ((struct ipovly *)ip)->ih_x1 = 0;
166 ((struct ipovly *)ip)->ih_len = uh->uh_ulen;
167 uh->uh_sum = in_cksum(m, len + sizeof (struct ip));
168 if (uh->uh_sum) {
169 udpstat.udps_badsum++;
170 m_freem(m);
171 return;
172 }
173 }
174
175 if (IN_MULTICAST(ntohl(ip->ip_dst.s_addr)) ||
176 in_broadcast(ip->ip_dst, m->m_pkthdr.rcvif)) {
177 struct socket *last;
178 /*
179 * Deliver a multicast or broadcast datagram to *all* sockets
180 * for which the local and remote addresses and ports match
181 * those of the incoming datagram. This allows more than
182 * one process to receive multi/broadcasts on the same port.
183 * (This really ought to be done for unicast datagrams as
184 * well, but that would cause problems with existing
185 * applications that open both address-specific sockets and
186 * a wildcard socket listening to the same port -- they would
187 * end up receiving duplicates of every unicast datagram.
188 * Those applications open the multiple sockets to overcome an
189 * inadequacy of the UDP socket interface, but for backwards
190 * compatibility we avoid the problem here rather than
191 * fixing the interface. Maybe 4.5BSD will remedy this?)
192 */
193
194 /*
195 * Construct sockaddr format source address.
196 */
197 udp_in.sin_port = uh->uh_sport;
198 udp_in.sin_addr = ip->ip_src;
199 m->m_len -= sizeof (struct udpiphdr);
200 m->m_data += sizeof (struct udpiphdr);
201 /*
202 * Locate pcb(s) for datagram.
203 * (Algorithm copied from raw_intr().)
204 */
205 last = NULL;
206 for (inp = udb.lh_first; inp != NULL; inp = inp->inp_list.le_next) {
207 if (inp->inp_lport != uh->uh_dport)
208 continue;
209 if (inp->inp_laddr.s_addr != INADDR_ANY) {
210 if (inp->inp_laddr.s_addr !=
211 ip->ip_dst.s_addr)
212 continue;
213 }
214 if (inp->inp_faddr.s_addr != INADDR_ANY) {
215 if (inp->inp_faddr.s_addr !=
216 ip->ip_src.s_addr ||
217 inp->inp_fport != uh->uh_sport)
218 continue;
219 }
220
221 if (last != NULL) {
222 struct mbuf *n;
223
224 if ((n = m_copy(m, 0, M_COPYALL)) != NULL) {
225 if (sbappendaddr(&last->so_rcv,
226 (struct sockaddr *)&udp_in,
227 n, (struct mbuf *)0) == 0) {
228 m_freem(n);
229 udpstat.udps_fullsock++;
230 } else
231 sorwakeup(last);
232 }
233 }
234 last = inp->inp_socket;
235 /*
236 * Don't look for additional matches if this one does
237 * not have either the SO_REUSEPORT or SO_REUSEADDR
238 * socket options set. This heuristic avoids searching
239 * through all pcbs in the common case of a non-shared
240 * port. It * assumes that an application will never
241 * clear these options after setting them.
242 */
243 if ((last->so_options&(SO_REUSEPORT|SO_REUSEADDR) == 0))
244 break;
245 }
246
247 if (last == NULL) {
248 /*
249 * No matching pcb found; discard datagram.
250 * (No need to send an ICMP Port Unreachable
251 * for a broadcast or multicast datgram.)
252 */
253 udpstat.udps_noportbcast++;
254 goto bad;
255 }
256 if (sbappendaddr(&last->so_rcv, (struct sockaddr *)&udp_in,
257 m, (struct mbuf *)0) == 0) {
258 udpstat.udps_fullsock++;
259 goto bad;
260 }
261 sorwakeup(last);
262 return;
263 }
264 /*
265 * Locate pcb for datagram. First look for an exact match.
266 */
267 inp = in_pcblookuphash(&udbinfo, ip->ip_src, uh->uh_sport,
268 ip->ip_dst, uh->uh_dport);
269 /*
270 * ...and if that fails, do a wildcard search.
271 */
272 if (inp == NULL) {
273 inp = in_pcblookup(&udb, ip->ip_src, uh->uh_sport, ip->ip_dst,
274 uh->uh_dport, INPLOOKUP_WILDCARD);
275 }
276 if (inp == NULL) {
277 udpstat.udps_noport++;
278 if (m->m_flags & (M_BCAST | M_MCAST)) {
279 udpstat.udps_noportbcast++;
280 goto bad;
281 }
282 *ip = save_ip;
283 icmp_error(m, ICMP_UNREACH, ICMP_UNREACH_PORT, 0, 0);
284 return;
285 }
286
287 /*
288 * Construct sockaddr format source address.
289 * Stuff source address and datagram in user buffer.
290 */
291 udp_in.sin_port = uh->uh_sport;
292 udp_in.sin_addr = ip->ip_src;
293 if (inp->inp_flags & INP_CONTROLOPTS) {
294 struct mbuf **mp = &opts;
295
296 if (inp->inp_flags & INP_RECVDSTADDR) {
297 *mp = udp_saveopt((caddr_t) &ip->ip_dst,
298 sizeof(struct in_addr), IP_RECVDSTADDR);
299 if (*mp)
300 mp = &(*mp)->m_next;
301 }
302#ifdef notyet
303 /* options were tossed above */
304 if (inp->inp_flags & INP_RECVOPTS) {
305 *mp = udp_saveopt((caddr_t) opts_deleted_above,
306 sizeof(struct in_addr), IP_RECVOPTS);
307 if (*mp)
308 mp = &(*mp)->m_next;
309 }
310 /* ip_srcroute doesn't do what we want here, need to fix */
311 if (inp->inp_flags & INP_RECVRETOPTS) {
312 *mp = udp_saveopt((caddr_t) ip_srcroute(),
313 sizeof(struct in_addr), IP_RECVRETOPTS);
314 if (*mp)
315 mp = &(*mp)->m_next;
316 }
317#endif
318 }
319 iphlen += sizeof(struct udphdr);
320 m->m_len -= iphlen;
321 m->m_pkthdr.len -= iphlen;
322 m->m_data += iphlen;
323 if (sbappendaddr(&inp->inp_socket->so_rcv, (struct sockaddr *)&udp_in,
324 m, opts) == 0) {
325 udpstat.udps_fullsock++;
326 goto bad;
327 }
328 sorwakeup(inp->inp_socket);
329 return;
330bad:
331 m_freem(m);
332 if (opts)
333 m_freem(opts);
334}
335
336/*
337 * Create a "control" mbuf containing the specified data
338 * with the specified type for presentation with a datagram.
339 */
340struct mbuf *
341udp_saveopt(p, size, type)
342 caddr_t p;
343 register int size;
344 int type;
345{
346 register struct cmsghdr *cp;
347 struct mbuf *m;
348
349 if ((m = m_get(M_DONTWAIT, MT_CONTROL)) == NULL)
350 return ((struct mbuf *) NULL);
351 cp = (struct cmsghdr *) mtod(m, struct cmsghdr *);
352 (void)memcpy(CMSG_DATA(cp), p, size);
353 size += sizeof(*cp);
354 m->m_len = size;
355 cp->cmsg_len = size;
356 cp->cmsg_level = IPPROTO_IP;
357 cp->cmsg_type = type;
358 return (m);
359}
360
361/*
362 * Notify a udp user of an asynchronous error;
363 * just wake up so that he can collect error status.
364 */
365static void
366udp_notify(inp, errno)
367 register struct inpcb *inp;
368 int errno;
369{
370 inp->inp_socket->so_error = errno;
371 sorwakeup(inp->inp_socket);
372 sowwakeup(inp->inp_socket);
373}
374
375void
376udp_ctlinput(cmd, sa, ip)
377 int cmd;
378 struct sockaddr *sa;
379 register struct ip *ip;
380{
381 register struct udphdr *uh;
382
383 if (!PRC_IS_REDIRECT(cmd) &&
384 ((unsigned)cmd >= PRC_NCMDS || inetctlerrmap[cmd] == 0))
385 return;
386 if (ip) {
387 uh = (struct udphdr *)((caddr_t)ip + (ip->ip_hl << 2));
388 in_pcbnotify(&udb, sa, uh->uh_dport, ip->ip_src, uh->uh_sport,
389 cmd, udp_notify);
390 } else
391 in_pcbnotify(&udb, sa, 0, zeroin_addr, 0, cmd, udp_notify);
392}
393
394static int
395udp_output(inp, m, addr, control)
396 register struct inpcb *inp;
397 register struct mbuf *m;
398 struct mbuf *addr, *control;
399{
400 register struct udpiphdr *ui;
401 register int len = m->m_pkthdr.len;
402 struct in_addr laddr;
403 int s = 0, error = 0;
404
405 if (control)
406 m_freem(control); /* XXX */
407
408 if (addr) {
409 laddr = inp->inp_laddr;
410 if (inp->inp_faddr.s_addr != INADDR_ANY) {
411 error = EISCONN;
412 goto release;
413 }
414 /*
415 * Must block input while temporarily connected.
416 */
417 s = splnet();
418 error = in_pcbconnect(inp, addr);
419 if (error) {
420 splx(s);
421 goto release;
422 }
423 } else {
424 if (inp->inp_faddr.s_addr == INADDR_ANY) {
425 error = ENOTCONN;
426 goto release;
427 }
428 }
429 /*
430 * Calculate data length and get a mbuf
431 * for UDP and IP headers.
432 */
433 M_PREPEND(m, sizeof(struct udpiphdr), M_DONTWAIT);
434 if (m == 0) {
435 error = ENOBUFS;
436 if (addr)
437 splx(s);
438 goto release;
439 }
440
441 /*
442 * Fill in mbuf with extended UDP header
443 * and addresses and length put into network format.
444 */
445 ui = mtod(m, struct udpiphdr *);
446 ui->ui_next = ui->ui_prev = 0;
447 ui->ui_x1 = 0;
448 ui->ui_pr = IPPROTO_UDP;
449 ui->ui_len = htons((u_short)len + sizeof (struct udphdr));
450 ui->ui_src = inp->inp_laddr;
451 ui->ui_dst = inp->inp_faddr;
452 ui->ui_sport = inp->inp_lport;
453 ui->ui_dport = inp->inp_fport;
454 ui->ui_ulen = ui->ui_len;
455
456 /*
457 * Stuff checksum and output datagram.
458 */
459 ui->ui_sum = 0;
460 if (udpcksum) {
461 if ((ui->ui_sum = in_cksum(m, sizeof (struct udpiphdr) + len)) == 0)
462 ui->ui_sum = 0xffff;
463 }
464 ((struct ip *)ui)->ip_len = sizeof (struct udpiphdr) + len;
465 ((struct ip *)ui)->ip_ttl = inp->inp_ip.ip_ttl; /* XXX */
466 ((struct ip *)ui)->ip_tos = inp->inp_ip.ip_tos; /* XXX */
467 udpstat.udps_opackets++;
468 error = ip_output(m, inp->inp_options, &inp->inp_route,
469 inp->inp_socket->so_options & (SO_DONTROUTE | SO_BROADCAST),
470 inp->inp_moptions);
471
472 if (addr) {
473 in_pcbdisconnect(inp);
474 inp->inp_laddr = laddr;
475 splx(s);
476 }
477 return (error);
478
479release:
480 m_freem(m);
481 return (error);
482}
483
484static u_long udp_sendspace = 9216; /* really max datagram size */
485 /* 40 1K datagrams */
486SYSCTL_INT(_net_inet_udp, UDPCTL_MAXDGRAM, maxdgram, CTLFLAG_RW,
487 &udp_sendspace, 0, "");
488
489static u_long udp_recvspace = 40 * (1024 + sizeof(struct sockaddr_in));
490SYSCTL_INT(_net_inet_udp, UDPCTL_RECVSPACE, recvspace, CTLFLAG_RW,
491 &udp_recvspace, 0, "");
492
493/*ARGSUSED*/
494int
495udp_usrreq(so, req, m, addr, control)
496 struct socket *so;
497 int req;
498 struct mbuf *m, *addr, *control;
499{
500 struct inpcb *inp = sotoinpcb(so);
501 int error = 0;
502 int s;
503
504 if (req == PRU_CONTROL)
505 return (in_control(so, (u_long)m, (caddr_t)addr,
506 (struct ifnet *)control));
507 if (inp == NULL && req != PRU_ATTACH) {
508 error = EINVAL;
509 goto release;
510 }
511 /*
512 * Note: need to block udp_input while changing
513 * the udp pcb queue and/or pcb addresses.
514 */
515 switch (req) {
516
517 case PRU_ATTACH:
518 if (inp != NULL) {
519 error = EINVAL;
520 break;
521 }
522 s = splnet();
523 error = in_pcballoc(so, &udbinfo);
524 splx(s);
525 if (error)
526 break;
527 error = soreserve(so, udp_sendspace, udp_recvspace);
528 if (error)
529 break;
530 ((struct inpcb *) so->so_pcb)->inp_ip.ip_ttl = ip_defttl;
531 break;
532
533 case PRU_DETACH:
534 udp_detach(inp);
535 break;
536
537 case PRU_BIND:
538 s = splnet();
539 error = in_pcbbind(inp, addr);
540 splx(s);
541 break;
542
543 case PRU_LISTEN:
544 error = EOPNOTSUPP;
545 break;
546
547 case PRU_CONNECT:
548 if (inp->inp_faddr.s_addr != INADDR_ANY) {
549 error = EISCONN;
550 break;
551 }
552 s = splnet();
553 error = in_pcbconnect(inp, addr);
554 splx(s);
555 if (error == 0)
556 soisconnected(so);
557 break;
558
559 case PRU_CONNECT2:
560 error = EOPNOTSUPP;
561 break;
562
563 case PRU_ACCEPT:
564 error = EOPNOTSUPP;
565 break;
566
567 case PRU_DISCONNECT:
568 if (inp->inp_faddr.s_addr == INADDR_ANY) {
569 error = ENOTCONN;
570 break;
571 }
572 s = splnet();
573 in_pcbdisconnect(inp);
574 inp->inp_laddr.s_addr = INADDR_ANY;
575 splx(s);
576 so->so_state &= ~SS_ISCONNECTED; /* XXX */
577 break;
578
579 case PRU_SHUTDOWN:
580 socantsendmore(so);
581 break;
582
583 case PRU_SEND:
584 return (udp_output(inp, m, addr, control));
585
586 case PRU_ABORT:
587 soisdisconnected(so);
588 udp_detach(inp);
589 break;
590
591 case PRU_SOCKADDR:
592 in_setsockaddr(inp, addr);
593 break;
594
595 case PRU_PEERADDR:
596 in_setpeeraddr(inp, addr);
597 break;
598
599 case PRU_SENSE:
600 /*
601 * stat: don't bother with a blocksize.
602 */
603 return (0);
604
605 case PRU_SENDOOB:
606 case PRU_FASTTIMO:
607 case PRU_SLOWTIMO:
608 case PRU_PROTORCV:
609 case PRU_PROTOSEND:
610 error = EOPNOTSUPP;
611 break;
612
613 case PRU_RCVD:
614 case PRU_RCVOOB:
615 return (EOPNOTSUPP); /* do not free mbuf's */
616
617 default:
618 panic("udp_usrreq");
619 }
620
621release:
622 if (control) {
623 printf("udp control data unexpectedly retained\n");
624 m_freem(control);
625 }
626 if (m)
627 m_freem(m);
628 return (error);
629}
630
631static void
632udp_detach(inp)
633 struct inpcb *inp;
634{
635 int s = splnet();
636
637 in_pcbdetach(inp);
638 splx(s);
639}