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.18 1995/12/14 09:53:49 phk 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, vip)
377 int cmd;
378 struct sockaddr *sa;
379 void *vip;
380{
381 register struct ip *ip = vip;
382 register struct udphdr *uh;
383
384 if (!PRC_IS_REDIRECT(cmd) &&
385 ((unsigned)cmd >= PRC_NCMDS || inetctlerrmap[cmd] == 0))
386 return;
387 if (ip) {
388 uh = (struct udphdr *)((caddr_t)ip + (ip->ip_hl << 2));
389 in_pcbnotify(&udb, sa, uh->uh_dport, ip->ip_src, uh->uh_sport,
390 cmd, udp_notify);
391 } else
392 in_pcbnotify(&udb, sa, 0, zeroin_addr, 0, cmd, udp_notify);
393}
394
395static int
396udp_output(inp, m, addr, control)
397 register struct inpcb *inp;
398 register struct mbuf *m;
399 struct mbuf *addr, *control;
400{
401 register struct udpiphdr *ui;
402 register int len = m->m_pkthdr.len;
403 struct in_addr laddr;
404 int s = 0, error = 0;
405
406 if (control)
407 m_freem(control); /* XXX */
408
409 if (addr) {
410 laddr = inp->inp_laddr;
411 if (inp->inp_faddr.s_addr != INADDR_ANY) {
412 error = EISCONN;
413 goto release;
414 }
415 /*
416 * Must block input while temporarily connected.
417 */
418 s = splnet();
419 error = in_pcbconnect(inp, addr);
420 if (error) {
421 splx(s);
422 goto release;
423 }
424 } else {
425 if (inp->inp_faddr.s_addr == INADDR_ANY) {
426 error = ENOTCONN;
427 goto release;
428 }
429 }
430 /*
431 * Calculate data length and get a mbuf
432 * for UDP and IP headers.
433 */
434 M_PREPEND(m, sizeof(struct udpiphdr), M_DONTWAIT);
435 if (m == 0) {
436 error = ENOBUFS;
437 if (addr)
438 splx(s);
439 goto release;
440 }
441
442 /*
443 * Fill in mbuf with extended UDP header
444 * and addresses and length put into network format.
445 */
446 ui = mtod(m, struct udpiphdr *);
447 ui->ui_next = ui->ui_prev = 0;
448 ui->ui_x1 = 0;
449 ui->ui_pr = IPPROTO_UDP;
450 ui->ui_len = htons((u_short)len + sizeof (struct udphdr));
451 ui->ui_src = inp->inp_laddr;
452 ui->ui_dst = inp->inp_faddr;
453 ui->ui_sport = inp->inp_lport;
454 ui->ui_dport = inp->inp_fport;
455 ui->ui_ulen = ui->ui_len;
456
457 /*
458 * Stuff checksum and output datagram.
459 */
460 ui->ui_sum = 0;
461 if (udpcksum) {
462 if ((ui->ui_sum = in_cksum(m, sizeof (struct udpiphdr) + len)) == 0)
463 ui->ui_sum = 0xffff;
464 }
465 ((struct ip *)ui)->ip_len = sizeof (struct udpiphdr) + len;
466 ((struct ip *)ui)->ip_ttl = inp->inp_ip.ip_ttl; /* XXX */
467 ((struct ip *)ui)->ip_tos = inp->inp_ip.ip_tos; /* XXX */
468 udpstat.udps_opackets++;
469 error = ip_output(m, inp->inp_options, &inp->inp_route,
470 inp->inp_socket->so_options & (SO_DONTROUTE | SO_BROADCAST),
471 inp->inp_moptions);
472
473 if (addr) {
474 in_pcbdisconnect(inp);
475 inp->inp_laddr = laddr;
476 splx(s);
477 }
478 return (error);
479
480release:
481 m_freem(m);
482 return (error);
483}
484
485static u_long udp_sendspace = 9216; /* really max datagram size */
486 /* 40 1K datagrams */
487SYSCTL_INT(_net_inet_udp, UDPCTL_MAXDGRAM, maxdgram, CTLFLAG_RW,
488 &udp_sendspace, 0, "");
489
490static u_long udp_recvspace = 40 * (1024 + sizeof(struct sockaddr_in));
491SYSCTL_INT(_net_inet_udp, UDPCTL_RECVSPACE, recvspace, CTLFLAG_RW,
492 &udp_recvspace, 0, "");
493
494/*ARGSUSED*/
495int
496udp_usrreq(so, req, m, addr, control)
497 struct socket *so;
498 int req;
499 struct mbuf *m, *addr, *control;
500{
501 struct inpcb *inp = sotoinpcb(so);
502 int error = 0;
503 int s;
504
505 if (req == PRU_CONTROL)
506 return (in_control(so, (u_long)m, (caddr_t)addr,
507 (struct ifnet *)control));
508 if (inp == NULL && req != PRU_ATTACH) {
509 error = EINVAL;
510 goto release;
511 }
512 /*
513 * Note: need to block udp_input while changing
514 * the udp pcb queue and/or pcb addresses.
515 */
516 switch (req) {
517
518 case PRU_ATTACH:
519 if (inp != NULL) {
520 error = EINVAL;
521 break;
522 }
523 s = splnet();
524 error = in_pcballoc(so, &udbinfo);
525 splx(s);
526 if (error)
527 break;
528 error = soreserve(so, udp_sendspace, udp_recvspace);
529 if (error)
530 break;
531 ((struct inpcb *) so->so_pcb)->inp_ip.ip_ttl = ip_defttl;
532 break;
533
534 case PRU_DETACH:
535 udp_detach(inp);
536 break;
537
538 case PRU_BIND:
539 s = splnet();
540 error = in_pcbbind(inp, addr);
541 splx(s);
542 break;
543
544 case PRU_LISTEN:
545 error = EOPNOTSUPP;
546 break;
547
548 case PRU_CONNECT:
549 if (inp->inp_faddr.s_addr != INADDR_ANY) {
550 error = EISCONN;
551 break;
552 }
553 s = splnet();
554 error = in_pcbconnect(inp, addr);
555 splx(s);
556 if (error == 0)
557 soisconnected(so);
558 break;
559
560 case PRU_CONNECT2:
561 error = EOPNOTSUPP;
562 break;
563
564 case PRU_ACCEPT:
565 error = EOPNOTSUPP;
566 break;
567
568 case PRU_DISCONNECT:
569 if (inp->inp_faddr.s_addr == INADDR_ANY) {
570 error = ENOTCONN;
571 break;
572 }
573 s = splnet();
574 in_pcbdisconnect(inp);
575 inp->inp_laddr.s_addr = INADDR_ANY;
576 splx(s);
577 so->so_state &= ~SS_ISCONNECTED; /* XXX */
578 break;
579
580 case PRU_SHUTDOWN:
581 socantsendmore(so);
582 break;
583
584 case PRU_SEND:
585 return (udp_output(inp, m, addr, control));
586
587 case PRU_ABORT:
588 soisdisconnected(so);
589 udp_detach(inp);
590 break;
591
592 case PRU_SOCKADDR:
593 in_setsockaddr(inp, addr);
594 break;
595
596 case PRU_PEERADDR:
597 in_setpeeraddr(inp, addr);
598 break;
599
600 case PRU_SENSE:
601 /*
602 * stat: don't bother with a blocksize.
603 */
604 return (0);
605
606 case PRU_SENDOOB:
607 case PRU_FASTTIMO:
608 case PRU_SLOWTIMO:
609 case PRU_PROTORCV:
610 case PRU_PROTOSEND:
611 error = EOPNOTSUPP;
612 break;
613
614 case PRU_RCVD:
615 case PRU_RCVOOB:
616 return (EOPNOTSUPP); /* do not free mbuf's */
617
618 default:
619 panic("udp_usrreq");
620 }
621
622release:
623 if (control) {
624 printf("udp control data unexpectedly retained\n");
625 m_freem(control);
626 }
627 if (m)
628 m_freem(m);
629 return (error);
630}
631
632static void
633udp_detach(inp)
634 struct inpcb *inp;
635{
636 int s = splnet();
637
638 in_pcbdetach(inp);
639 splx(s);
640}
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.18 1995/12/14 09:53:49 phk 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, vip)
377 int cmd;
378 struct sockaddr *sa;
379 void *vip;
380{
381 register struct ip *ip = vip;
382 register struct udphdr *uh;
383
384 if (!PRC_IS_REDIRECT(cmd) &&
385 ((unsigned)cmd >= PRC_NCMDS || inetctlerrmap[cmd] == 0))
386 return;
387 if (ip) {
388 uh = (struct udphdr *)((caddr_t)ip + (ip->ip_hl << 2));
389 in_pcbnotify(&udb, sa, uh->uh_dport, ip->ip_src, uh->uh_sport,
390 cmd, udp_notify);
391 } else
392 in_pcbnotify(&udb, sa, 0, zeroin_addr, 0, cmd, udp_notify);
393}
394
395static int
396udp_output(inp, m, addr, control)
397 register struct inpcb *inp;
398 register struct mbuf *m;
399 struct mbuf *addr, *control;
400{
401 register struct udpiphdr *ui;
402 register int len = m->m_pkthdr.len;
403 struct in_addr laddr;
404 int s = 0, error = 0;
405
406 if (control)
407 m_freem(control); /* XXX */
408
409 if (addr) {
410 laddr = inp->inp_laddr;
411 if (inp->inp_faddr.s_addr != INADDR_ANY) {
412 error = EISCONN;
413 goto release;
414 }
415 /*
416 * Must block input while temporarily connected.
417 */
418 s = splnet();
419 error = in_pcbconnect(inp, addr);
420 if (error) {
421 splx(s);
422 goto release;
423 }
424 } else {
425 if (inp->inp_faddr.s_addr == INADDR_ANY) {
426 error = ENOTCONN;
427 goto release;
428 }
429 }
430 /*
431 * Calculate data length and get a mbuf
432 * for UDP and IP headers.
433 */
434 M_PREPEND(m, sizeof(struct udpiphdr), M_DONTWAIT);
435 if (m == 0) {
436 error = ENOBUFS;
437 if (addr)
438 splx(s);
439 goto release;
440 }
441
442 /*
443 * Fill in mbuf with extended UDP header
444 * and addresses and length put into network format.
445 */
446 ui = mtod(m, struct udpiphdr *);
447 ui->ui_next = ui->ui_prev = 0;
448 ui->ui_x1 = 0;
449 ui->ui_pr = IPPROTO_UDP;
450 ui->ui_len = htons((u_short)len + sizeof (struct udphdr));
451 ui->ui_src = inp->inp_laddr;
452 ui->ui_dst = inp->inp_faddr;
453 ui->ui_sport = inp->inp_lport;
454 ui->ui_dport = inp->inp_fport;
455 ui->ui_ulen = ui->ui_len;
456
457 /*
458 * Stuff checksum and output datagram.
459 */
460 ui->ui_sum = 0;
461 if (udpcksum) {
462 if ((ui->ui_sum = in_cksum(m, sizeof (struct udpiphdr) + len)) == 0)
463 ui->ui_sum = 0xffff;
464 }
465 ((struct ip *)ui)->ip_len = sizeof (struct udpiphdr) + len;
466 ((struct ip *)ui)->ip_ttl = inp->inp_ip.ip_ttl; /* XXX */
467 ((struct ip *)ui)->ip_tos = inp->inp_ip.ip_tos; /* XXX */
468 udpstat.udps_opackets++;
469 error = ip_output(m, inp->inp_options, &inp->inp_route,
470 inp->inp_socket->so_options & (SO_DONTROUTE | SO_BROADCAST),
471 inp->inp_moptions);
472
473 if (addr) {
474 in_pcbdisconnect(inp);
475 inp->inp_laddr = laddr;
476 splx(s);
477 }
478 return (error);
479
480release:
481 m_freem(m);
482 return (error);
483}
484
485static u_long udp_sendspace = 9216; /* really max datagram size */
486 /* 40 1K datagrams */
487SYSCTL_INT(_net_inet_udp, UDPCTL_MAXDGRAM, maxdgram, CTLFLAG_RW,
488 &udp_sendspace, 0, "");
489
490static u_long udp_recvspace = 40 * (1024 + sizeof(struct sockaddr_in));
491SYSCTL_INT(_net_inet_udp, UDPCTL_RECVSPACE, recvspace, CTLFLAG_RW,
492 &udp_recvspace, 0, "");
493
494/*ARGSUSED*/
495int
496udp_usrreq(so, req, m, addr, control)
497 struct socket *so;
498 int req;
499 struct mbuf *m, *addr, *control;
500{
501 struct inpcb *inp = sotoinpcb(so);
502 int error = 0;
503 int s;
504
505 if (req == PRU_CONTROL)
506 return (in_control(so, (u_long)m, (caddr_t)addr,
507 (struct ifnet *)control));
508 if (inp == NULL && req != PRU_ATTACH) {
509 error = EINVAL;
510 goto release;
511 }
512 /*
513 * Note: need to block udp_input while changing
514 * the udp pcb queue and/or pcb addresses.
515 */
516 switch (req) {
517
518 case PRU_ATTACH:
519 if (inp != NULL) {
520 error = EINVAL;
521 break;
522 }
523 s = splnet();
524 error = in_pcballoc(so, &udbinfo);
525 splx(s);
526 if (error)
527 break;
528 error = soreserve(so, udp_sendspace, udp_recvspace);
529 if (error)
530 break;
531 ((struct inpcb *) so->so_pcb)->inp_ip.ip_ttl = ip_defttl;
532 break;
533
534 case PRU_DETACH:
535 udp_detach(inp);
536 break;
537
538 case PRU_BIND:
539 s = splnet();
540 error = in_pcbbind(inp, addr);
541 splx(s);
542 break;
543
544 case PRU_LISTEN:
545 error = EOPNOTSUPP;
546 break;
547
548 case PRU_CONNECT:
549 if (inp->inp_faddr.s_addr != INADDR_ANY) {
550 error = EISCONN;
551 break;
552 }
553 s = splnet();
554 error = in_pcbconnect(inp, addr);
555 splx(s);
556 if (error == 0)
557 soisconnected(so);
558 break;
559
560 case PRU_CONNECT2:
561 error = EOPNOTSUPP;
562 break;
563
564 case PRU_ACCEPT:
565 error = EOPNOTSUPP;
566 break;
567
568 case PRU_DISCONNECT:
569 if (inp->inp_faddr.s_addr == INADDR_ANY) {
570 error = ENOTCONN;
571 break;
572 }
573 s = splnet();
574 in_pcbdisconnect(inp);
575 inp->inp_laddr.s_addr = INADDR_ANY;
576 splx(s);
577 so->so_state &= ~SS_ISCONNECTED; /* XXX */
578 break;
579
580 case PRU_SHUTDOWN:
581 socantsendmore(so);
582 break;
583
584 case PRU_SEND:
585 return (udp_output(inp, m, addr, control));
586
587 case PRU_ABORT:
588 soisdisconnected(so);
589 udp_detach(inp);
590 break;
591
592 case PRU_SOCKADDR:
593 in_setsockaddr(inp, addr);
594 break;
595
596 case PRU_PEERADDR:
597 in_setpeeraddr(inp, addr);
598 break;
599
600 case PRU_SENSE:
601 /*
602 * stat: don't bother with a blocksize.
603 */
604 return (0);
605
606 case PRU_SENDOOB:
607 case PRU_FASTTIMO:
608 case PRU_SLOWTIMO:
609 case PRU_PROTORCV:
610 case PRU_PROTOSEND:
611 error = EOPNOTSUPP;
612 break;
613
614 case PRU_RCVD:
615 case PRU_RCVOOB:
616 return (EOPNOTSUPP); /* do not free mbuf's */
617
618 default:
619 panic("udp_usrreq");
620 }
621
622release:
623 if (control) {
624 printf("udp control data unexpectedly retained\n");
625 m_freem(control);
626 }
627 if (m)
628 m_freem(m);
629 return (error);
630}
631
632static void
633udp_detach(inp)
634 struct inpcb *inp;
635{
636 int s = splnet();
637
638 in_pcbdetach(inp);
639 splx(s);
640}