udp_usrreq.c revision 1.29.2.1 1 /* $NetBSD: udp_usrreq.c,v 1.29.2.1 1996/11/10 21:57:55 thorpej Exp $ */
2
3 /*
4 * Copyright (c) 1982, 1986, 1988, 1990, 1993
5 * The Regents of the University of California. All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. All advertising materials mentioning features or use of this software
16 * must display the following acknowledgement:
17 * This product includes software developed by the University of
18 * California, Berkeley and its contributors.
19 * 4. Neither the name of the University nor the names of its contributors
20 * may be used to endorse or promote products derived from this software
21 * without specific prior written permission.
22 *
23 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 * SUCH DAMAGE.
34 *
35 * @(#)udp_usrreq.c 8.4 (Berkeley) 1/21/94
36 */
37
38 #include <sys/param.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/systm.h>
47 #include <sys/proc.h>
48
49 #include <vm/vm.h>
50 #include <sys/sysctl.h>
51
52 #include <net/if.h>
53 #include <net/route.h>
54
55 #include <netinet/in.h>
56 #include <netinet/in_systm.h>
57 #include <netinet/in_var.h>
58 #include <netinet/ip.h>
59 #include <netinet/in_pcb.h>
60 #include <netinet/ip_var.h>
61 #include <netinet/ip_icmp.h>
62 #include <netinet/udp.h>
63 #include <netinet/udp_var.h>
64
65 #include <machine/stdarg.h>
66
67 /*
68 * UDP protocol implementation.
69 * Per RFC 768, August, 1980.
70 */
71 #ifndef COMPAT_42
72 int udpcksum = 1;
73 #else
74 int udpcksum = 0; /* XXX */
75 #endif
76
77 struct sockaddr_in udp_in = { sizeof(udp_in), AF_INET };
78
79 static void udp_detach __P((struct inpcb *));
80 static void udp_notify __P((struct inpcb *, int));
81 static struct mbuf *udp_saveopt __P((caddr_t, int, int));
82
83 #ifndef UDBHASHSIZE
84 #define UDBHASHSIZE 128
85 #endif
86 int udbhashsize = UDBHASHSIZE;
87
88 void
89 udp_init()
90 {
91
92 in_pcbinit(&udbtable, udbhashsize);
93 }
94
95 void
96 #if __STDC__
97 udp_input(struct mbuf *m, ...)
98 #else
99 udp_input(m, va_alist)
100 struct mbuf *m;
101 va_dcl
102 #endif
103 {
104 register struct ip *ip;
105 register struct udphdr *uh;
106 register struct inpcb *inp;
107 struct mbuf *opts = 0;
108 int len;
109 struct ip save_ip;
110 int iphlen;
111 va_list ap;
112
113 va_start(ap, m);
114 iphlen = va_arg(ap, int);
115 va_end(ap);
116
117 udpstat.udps_ipackets++;
118
119 /*
120 * Strip IP options, if any; should skip this,
121 * make available to user, and use on returned packets,
122 * but we don't yet have a way to check the checksum
123 * with options still present.
124 */
125 if (iphlen > sizeof (struct ip)) {
126 ip_stripoptions(m, (struct mbuf *)0);
127 iphlen = sizeof(struct ip);
128 }
129
130 /*
131 * Get IP and UDP header together in first mbuf.
132 */
133 ip = mtod(m, struct ip *);
134 if (m->m_len < iphlen + sizeof(struct udphdr)) {
135 if ((m = m_pullup(m, iphlen + sizeof(struct udphdr))) == 0) {
136 udpstat.udps_hdrops++;
137 return;
138 }
139 ip = mtod(m, struct ip *);
140 }
141 uh = (struct udphdr *)((caddr_t)ip + iphlen);
142
143 /*
144 * Make mbuf data length reflect UDP length.
145 * If not enough data to reflect UDP length, drop.
146 */
147 len = ntohs((u_int16_t)uh->uh_ulen);
148 if (ip->ip_len != len) {
149 if (len > ip->ip_len) {
150 udpstat.udps_badlen++;
151 goto bad;
152 }
153 m_adj(m, len - ip->ip_len);
154 /* ip->ip_len = len; */
155 }
156 /*
157 * Save a copy of the IP header in case we want restore it
158 * for sending an ICMP error message in response.
159 */
160 save_ip = *ip;
161
162 /*
163 * Checksum extended UDP header and data.
164 */
165 if (uh->uh_sum) {
166 bzero(((struct ipovly *)ip)->ih_x1,
167 sizeof ((struct ipovly *)ip)->ih_x1);
168 ((struct ipovly *)ip)->ih_len = uh->uh_ulen;
169 if ((uh->uh_sum = in_cksum(m, len + sizeof (struct ip))) != 0) {
170 udpstat.udps_badsum++;
171 m_freem(m);
172 return;
173 }
174 }
175
176 if (IN_MULTICAST(ip->ip_dst.s_addr) ||
177 in_broadcast(ip->ip_dst, m->m_pkthdr.rcvif)) {
178 struct socket *last;
179 /*
180 * Deliver a multicast or broadcast datagram to *all* sockets
181 * for which the local and remote addresses and ports match
182 * those of the incoming datagram. This allows more than
183 * one process to receive multi/broadcasts on the same port.
184 * (This really ought to be done for unicast datagrams as
185 * well, but that would cause problems with existing
186 * applications that open both address-specific sockets and
187 * a wildcard socket listening to the same port -- they would
188 * end up receiving duplicates of every unicast datagram.
189 * Those applications open the multiple sockets to overcome an
190 * inadequacy of the UDP socket interface, but for backwards
191 * compatibility we avoid the problem here rather than
192 * fixing the interface. Maybe 4.5BSD will remedy this?)
193 */
194
195 /*
196 * Construct sockaddr format source address.
197 */
198 udp_in.sin_port = uh->uh_sport;
199 udp_in.sin_addr = ip->ip_src;
200 m->m_len -= sizeof (struct udpiphdr);
201 m->m_data += sizeof (struct udpiphdr);
202 /*
203 * Locate pcb(s) for datagram.
204 * (Algorithm copied from raw_intr().)
205 */
206 last = NULL;
207 for (inp = udbtable.inpt_queue.cqh_first;
208 inp != (struct inpcb *)&udbtable.inpt_queue;
209 inp = inp->inp_queue.cqe_next) {
210 if (inp->inp_lport != uh->uh_dport)
211 continue;
212 if (inp->inp_laddr.s_addr != INADDR_ANY) {
213 if (inp->inp_laddr.s_addr !=
214 ip->ip_dst.s_addr)
215 continue;
216 }
217 if (inp->inp_faddr.s_addr != INADDR_ANY) {
218 if (inp->inp_faddr.s_addr !=
219 ip->ip_src.s_addr ||
220 inp->inp_fport != uh->uh_sport)
221 continue;
222 }
223
224 if (last != NULL) {
225 struct mbuf *n;
226
227 if ((n = m_copy(m, 0, M_COPYALL)) != NULL) {
228 if (sbappendaddr(&last->so_rcv,
229 sintosa(&udp_in), n,
230 (struct mbuf *)0) == 0) {
231 m_freem(n);
232 udpstat.udps_fullsock++;
233 } else
234 sorwakeup(last);
235 }
236 }
237 last = inp->inp_socket;
238 /*
239 * Don't look for additional matches if this one does
240 * not have either the SO_REUSEPORT or SO_REUSEADDR
241 * socket options set. This heuristic avoids searching
242 * through all pcbs in the common case of a non-shared
243 * port. It * assumes that an application will never
244 * clear these options after setting them.
245 */
246 if ((last->so_options&(SO_REUSEPORT|SO_REUSEADDR)) == 0)
247 break;
248 }
249
250 if (last == NULL) {
251 /*
252 * No matching pcb found; discard datagram.
253 * (No need to send an ICMP Port Unreachable
254 * for a broadcast or multicast datgram.)
255 */
256 udpstat.udps_noportbcast++;
257 goto bad;
258 }
259 if (sbappendaddr(&last->so_rcv, sintosa(&udp_in), m,
260 (struct mbuf *)0) == 0) {
261 udpstat.udps_fullsock++;
262 goto bad;
263 }
264 sorwakeup(last);
265 return;
266 }
267 /*
268 * Locate pcb for datagram.
269 */
270 inp = in_pcbhashlookup(&udbtable, ip->ip_src, uh->uh_sport,
271 ip->ip_dst, uh->uh_dport);
272 if (inp == 0) {
273 ++udpstat.udps_pcbhashmiss;
274 inp = in_pcblookup(&udbtable, ip->ip_src, uh->uh_sport,
275 ip->ip_dst, uh->uh_dport, INPLOOKUP_WILDCARD);
276 if (inp == 0) {
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 ip->ip_len += iphlen;
284 icmp_error(m, ICMP_UNREACH, ICMP_UNREACH_PORT, 0, 0);
285 return;
286 }
287 }
288
289 /*
290 * Construct sockaddr format source address.
291 * Stuff source address and datagram in user buffer.
292 */
293 udp_in.sin_port = uh->uh_sport;
294 udp_in.sin_addr = ip->ip_src;
295 if (inp->inp_flags & INP_CONTROLOPTS) {
296 struct mbuf **mp = &opts;
297
298 if (inp->inp_flags & INP_RECVDSTADDR) {
299 *mp = udp_saveopt((caddr_t) &ip->ip_dst,
300 sizeof(struct in_addr), IP_RECVDSTADDR);
301 if (*mp)
302 mp = &(*mp)->m_next;
303 }
304 #ifdef notyet
305 /* options were tossed above */
306 if (inp->inp_flags & INP_RECVOPTS) {
307 *mp = udp_saveopt((caddr_t) opts_deleted_above,
308 sizeof(struct in_addr), IP_RECVOPTS);
309 if (*mp)
310 mp = &(*mp)->m_next;
311 }
312 /* ip_srcroute doesn't do what we want here, need to fix */
313 if (inp->inp_flags & INP_RECVRETOPTS) {
314 *mp = udp_saveopt((caddr_t) ip_srcroute(),
315 sizeof(struct in_addr), IP_RECVRETOPTS);
316 if (*mp)
317 mp = &(*mp)->m_next;
318 }
319 #endif
320 }
321 iphlen += sizeof(struct udphdr);
322 m->m_len -= iphlen;
323 m->m_pkthdr.len -= iphlen;
324 m->m_data += iphlen;
325 if (sbappendaddr(&inp->inp_socket->so_rcv, sintosa(&udp_in), m,
326 opts) == 0) {
327 udpstat.udps_fullsock++;
328 goto bad;
329 }
330 sorwakeup(inp->inp_socket);
331 return;
332 bad:
333 m_freem(m);
334 if (opts)
335 m_freem(opts);
336 }
337
338 /*
339 * Create a "control" mbuf containing the specified data
340 * with the specified type for presentation with a datagram.
341 */
342 struct mbuf *
343 udp_saveopt(p, size, type)
344 caddr_t p;
345 register int size;
346 int type;
347 {
348 register struct cmsghdr *cp;
349 struct mbuf *m;
350
351 if ((m = m_get(M_DONTWAIT, MT_CONTROL)) == NULL)
352 return ((struct mbuf *) NULL);
353 cp = (struct cmsghdr *) mtod(m, struct cmsghdr *);
354 bcopy(p, CMSG_DATA(cp), size);
355 size += sizeof(*cp);
356 m->m_len = size;
357 cp->cmsg_len = size;
358 cp->cmsg_level = IPPROTO_IP;
359 cp->cmsg_type = type;
360 return (m);
361 }
362
363 /*
364 * Notify a udp user of an asynchronous error;
365 * just wake up so that he can collect error status.
366 */
367 static void
368 udp_notify(inp, errno)
369 register struct inpcb *inp;
370 int errno;
371 {
372 inp->inp_socket->so_error = errno;
373 sorwakeup(inp->inp_socket);
374 sowwakeup(inp->inp_socket);
375 }
376
377 void *
378 udp_ctlinput(cmd, sa, v)
379 int cmd;
380 struct sockaddr *sa;
381 void *v;
382 {
383 register struct ip *ip = v;
384 register struct udphdr *uh;
385 extern int inetctlerrmap[];
386 void (*notify) __P((struct inpcb *, int)) = udp_notify;
387 int errno;
388
389 if ((unsigned)cmd >= PRC_NCMDS)
390 return NULL;
391 errno = inetctlerrmap[cmd];
392 if (PRC_IS_REDIRECT(cmd))
393 notify = in_rtchange, ip = 0;
394 else if (cmd == PRC_HOSTDEAD)
395 ip = 0;
396 else if (errno == 0)
397 return NULL;
398 if (ip) {
399 uh = (struct udphdr *)((caddr_t)ip + (ip->ip_hl << 2));
400 in_pcbnotify(&udbtable, sa, uh->uh_dport, ip->ip_src,
401 uh->uh_sport, errno, notify);
402 } else
403 in_pcbnotifyall(&udbtable, sa, errno, notify);
404 return NULL;
405 }
406
407 int
408 #if __STDC__
409 udp_output(struct mbuf *m, ...)
410 #else
411 udp_output(m, va_alist)
412 struct mbuf *m;
413 va_dcl
414 #endif
415 {
416 register struct inpcb *inp;
417 struct mbuf *addr, *control;
418 register struct udpiphdr *ui;
419 register int len = m->m_pkthdr.len;
420 struct in_addr laddr;
421 int s = 0, error = 0;
422 va_list ap;
423
424 va_start(ap, m);
425 inp = va_arg(ap, struct inpcb *);
426 addr = va_arg(ap, struct mbuf *);
427 control = va_arg(ap, struct mbuf *);
428 va_end(ap);
429
430 if (control)
431 m_freem(control); /* XXX */
432
433 if (addr) {
434 laddr = inp->inp_laddr;
435 if (inp->inp_faddr.s_addr != INADDR_ANY) {
436 error = EISCONN;
437 goto release;
438 }
439 /*
440 * Must block input while temporarily connected.
441 */
442 s = splsoftnet();
443 error = in_pcbconnect(inp, addr);
444 if (error) {
445 splx(s);
446 goto release;
447 }
448 } else {
449 if (inp->inp_faddr.s_addr == INADDR_ANY) {
450 error = ENOTCONN;
451 goto release;
452 }
453 }
454 /*
455 * Calculate data length and get a mbuf
456 * for UDP and IP headers.
457 */
458 M_PREPEND(m, sizeof(struct udpiphdr), M_DONTWAIT);
459 if (m == 0) {
460 error = ENOBUFS;
461 goto release;
462 }
463
464 /*
465 * Compute the packet length of the IP header, and
466 * punt if the length looks bogus.
467 */
468 if ((len + sizeof(struct udpiphdr)) > IP_MAXPACKET) {
469 error = EMSGSIZE;
470 goto release;
471 }
472
473 /*
474 * Fill in mbuf with extended UDP header
475 * and addresses and length put into network format.
476 */
477 ui = mtod(m, struct udpiphdr *);
478 bzero(ui->ui_x1, sizeof ui->ui_x1);
479 ui->ui_pr = IPPROTO_UDP;
480 ui->ui_len = htons((u_int16_t)len + sizeof (struct udphdr));
481 ui->ui_src = inp->inp_laddr;
482 ui->ui_dst = inp->inp_faddr;
483 ui->ui_sport = inp->inp_lport;
484 ui->ui_dport = inp->inp_fport;
485 ui->ui_ulen = ui->ui_len;
486
487 /*
488 * Stuff checksum and output datagram.
489 */
490 ui->ui_sum = 0;
491 if (udpcksum) {
492 if ((ui->ui_sum = in_cksum(m, sizeof (struct udpiphdr) + len)) == 0)
493 ui->ui_sum = 0xffff;
494 }
495 ((struct ip *)ui)->ip_len = sizeof (struct udpiphdr) + len;
496 ((struct ip *)ui)->ip_ttl = inp->inp_ip.ip_ttl; /* XXX */
497 ((struct ip *)ui)->ip_tos = inp->inp_ip.ip_tos; /* XXX */
498 udpstat.udps_opackets++;
499 error = ip_output(m, inp->inp_options, &inp->inp_route,
500 inp->inp_socket->so_options & (SO_DONTROUTE | SO_BROADCAST),
501 inp->inp_moptions);
502
503 if (addr) {
504 in_pcbdisconnect(inp);
505 inp->inp_laddr = laddr;
506 splx(s);
507 }
508 return (error);
509
510 release:
511 m_freem(m);
512 return (error);
513 }
514
515 u_long udp_sendspace = 9216; /* really max datagram size */
516 u_long udp_recvspace = 40 * (1024 + sizeof(struct sockaddr_in));
517 /* 40 1K datagrams */
518
519 /*ARGSUSED*/
520 int
521 udp_usrreq(so, req, m, addr, control)
522 struct socket *so;
523 int req;
524 struct mbuf *m, *addr, *control;
525 {
526 struct inpcb *inp = sotoinpcb(so);
527 int error = 0;
528 int s;
529
530 if (req == PRU_CONTROL)
531 return (in_control(so, (long)m, (caddr_t)addr,
532 (struct ifnet *)control));
533 if (inp == NULL && req != PRU_ATTACH) {
534 error = EINVAL;
535 goto release;
536 }
537 /*
538 * Note: need to block udp_input while changing
539 * the udp pcb queue and/or pcb addresses.
540 */
541 switch (req) {
542
543 case PRU_ATTACH:
544 if (inp != NULL) {
545 error = EINVAL;
546 break;
547 }
548 s = splsoftnet();
549 error = in_pcballoc(so, &udbtable);
550 splx(s);
551 if (error)
552 break;
553 error = soreserve(so, udp_sendspace, udp_recvspace);
554 if (error)
555 break;
556 ((struct inpcb *) so->so_pcb)->inp_ip.ip_ttl = ip_defttl;
557 break;
558
559 case PRU_DETACH:
560 udp_detach(inp);
561 break;
562
563 case PRU_BIND:
564 s = splsoftnet();
565 error = in_pcbbind(inp, addr);
566 splx(s);
567 break;
568
569 case PRU_LISTEN:
570 error = EOPNOTSUPP;
571 break;
572
573 case PRU_CONNECT:
574 if (inp->inp_faddr.s_addr != INADDR_ANY) {
575 error = EISCONN;
576 break;
577 }
578 s = splsoftnet();
579 error = in_pcbconnect(inp, addr);
580 splx(s);
581 if (error == 0)
582 soisconnected(so);
583 break;
584
585 case PRU_CONNECT2:
586 error = EOPNOTSUPP;
587 break;
588
589 case PRU_ACCEPT:
590 error = EOPNOTSUPP;
591 break;
592
593 case PRU_DISCONNECT:
594 if (inp->inp_faddr.s_addr == INADDR_ANY) {
595 error = ENOTCONN;
596 break;
597 }
598 s = splsoftnet();
599 in_pcbdisconnect(inp);
600 inp->inp_laddr.s_addr = INADDR_ANY;
601 splx(s);
602 so->so_state &= ~SS_ISCONNECTED; /* XXX */
603 break;
604
605 case PRU_SHUTDOWN:
606 socantsendmore(so);
607 break;
608
609 case PRU_SEND:
610 return (udp_output(m, inp, addr, control));
611
612 case PRU_ABORT:
613 soisdisconnected(so);
614 udp_detach(inp);
615 break;
616
617 case PRU_SOCKADDR:
618 in_setsockaddr(inp, addr);
619 break;
620
621 case PRU_PEERADDR:
622 in_setpeeraddr(inp, addr);
623 break;
624
625 case PRU_SENSE:
626 /*
627 * stat: don't bother with a blocksize.
628 */
629 return (0);
630
631 case PRU_SENDOOB:
632 case PRU_FASTTIMO:
633 case PRU_SLOWTIMO:
634 case PRU_PROTORCV:
635 case PRU_PROTOSEND:
636 error = EOPNOTSUPP;
637 break;
638
639 case PRU_RCVD:
640 case PRU_RCVOOB:
641 return (EOPNOTSUPP); /* do not free mbuf's */
642
643 default:
644 panic("udp_usrreq");
645 }
646
647 release:
648 if (control) {
649 printf("udp control data unexpectedly retained\n");
650 m_freem(control);
651 }
652 if (m)
653 m_freem(m);
654 return (error);
655 }
656
657 static void
658 udp_detach(inp)
659 struct inpcb *inp;
660 {
661 int s = splsoftnet();
662
663 in_pcbdetach(inp);
664 splx(s);
665 }
666
667 /*
668 * Sysctl for udp variables.
669 */
670 int
671 udp_sysctl(name, namelen, oldp, oldlenp, newp, newlen)
672 int *name;
673 u_int namelen;
674 void *oldp;
675 size_t *oldlenp;
676 void *newp;
677 size_t newlen;
678 {
679 /* All sysctl names at this level are terminal. */
680 if (namelen != 1)
681 return (ENOTDIR);
682
683 switch (name[0]) {
684 case UDPCTL_CHECKSUM:
685 return (sysctl_int(oldp, oldlenp, newp, newlen, &udpcksum));
686 default:
687 return (ENOPROTOOPT);
688 }
689 /* NOTREACHED */
690 }
691