udp_usrreq.c revision 1.29 1 /* $NetBSD: udp_usrreq.c,v 1.29 1996/05/20 16:56:20 mrg 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 * Fill in mbuf with extended UDP header
466 * and addresses and length put into network format.
467 */
468 ui = mtod(m, struct udpiphdr *);
469 bzero(ui->ui_x1, sizeof ui->ui_x1);
470 ui->ui_pr = IPPROTO_UDP;
471 ui->ui_len = htons((u_int16_t)len + sizeof (struct udphdr));
472 ui->ui_src = inp->inp_laddr;
473 ui->ui_dst = inp->inp_faddr;
474 ui->ui_sport = inp->inp_lport;
475 ui->ui_dport = inp->inp_fport;
476 ui->ui_ulen = ui->ui_len;
477
478 /*
479 * Stuff checksum and output datagram.
480 */
481 ui->ui_sum = 0;
482 if (udpcksum) {
483 if ((ui->ui_sum = in_cksum(m, sizeof (struct udpiphdr) + len)) == 0)
484 ui->ui_sum = 0xffff;
485 }
486 ((struct ip *)ui)->ip_len = sizeof (struct udpiphdr) + len;
487 ((struct ip *)ui)->ip_ttl = inp->inp_ip.ip_ttl; /* XXX */
488 ((struct ip *)ui)->ip_tos = inp->inp_ip.ip_tos; /* XXX */
489 udpstat.udps_opackets++;
490 error = ip_output(m, inp->inp_options, &inp->inp_route,
491 inp->inp_socket->so_options & (SO_DONTROUTE | SO_BROADCAST),
492 inp->inp_moptions);
493
494 if (addr) {
495 in_pcbdisconnect(inp);
496 inp->inp_laddr = laddr;
497 splx(s);
498 }
499 return (error);
500
501 release:
502 m_freem(m);
503 return (error);
504 }
505
506 u_long udp_sendspace = 9216; /* really max datagram size */
507 u_long udp_recvspace = 40 * (1024 + sizeof(struct sockaddr_in));
508 /* 40 1K datagrams */
509
510 /*ARGSUSED*/
511 int
512 udp_usrreq(so, req, m, addr, control)
513 struct socket *so;
514 int req;
515 struct mbuf *m, *addr, *control;
516 {
517 struct inpcb *inp = sotoinpcb(so);
518 int error = 0;
519 int s;
520
521 if (req == PRU_CONTROL)
522 return (in_control(so, (long)m, (caddr_t)addr,
523 (struct ifnet *)control));
524 if (inp == NULL && req != PRU_ATTACH) {
525 error = EINVAL;
526 goto release;
527 }
528 /*
529 * Note: need to block udp_input while changing
530 * the udp pcb queue and/or pcb addresses.
531 */
532 switch (req) {
533
534 case PRU_ATTACH:
535 if (inp != NULL) {
536 error = EINVAL;
537 break;
538 }
539 s = splsoftnet();
540 error = in_pcballoc(so, &udbtable);
541 splx(s);
542 if (error)
543 break;
544 error = soreserve(so, udp_sendspace, udp_recvspace);
545 if (error)
546 break;
547 ((struct inpcb *) so->so_pcb)->inp_ip.ip_ttl = ip_defttl;
548 break;
549
550 case PRU_DETACH:
551 udp_detach(inp);
552 break;
553
554 case PRU_BIND:
555 s = splsoftnet();
556 error = in_pcbbind(inp, addr);
557 splx(s);
558 break;
559
560 case PRU_LISTEN:
561 error = EOPNOTSUPP;
562 break;
563
564 case PRU_CONNECT:
565 if (inp->inp_faddr.s_addr != INADDR_ANY) {
566 error = EISCONN;
567 break;
568 }
569 s = splsoftnet();
570 error = in_pcbconnect(inp, addr);
571 splx(s);
572 if (error == 0)
573 soisconnected(so);
574 break;
575
576 case PRU_CONNECT2:
577 error = EOPNOTSUPP;
578 break;
579
580 case PRU_ACCEPT:
581 error = EOPNOTSUPP;
582 break;
583
584 case PRU_DISCONNECT:
585 if (inp->inp_faddr.s_addr == INADDR_ANY) {
586 error = ENOTCONN;
587 break;
588 }
589 s = splsoftnet();
590 in_pcbdisconnect(inp);
591 inp->inp_laddr.s_addr = INADDR_ANY;
592 splx(s);
593 so->so_state &= ~SS_ISCONNECTED; /* XXX */
594 break;
595
596 case PRU_SHUTDOWN:
597 socantsendmore(so);
598 break;
599
600 case PRU_SEND:
601 return (udp_output(m, inp, addr, control));
602
603 case PRU_ABORT:
604 soisdisconnected(so);
605 udp_detach(inp);
606 break;
607
608 case PRU_SOCKADDR:
609 in_setsockaddr(inp, addr);
610 break;
611
612 case PRU_PEERADDR:
613 in_setpeeraddr(inp, addr);
614 break;
615
616 case PRU_SENSE:
617 /*
618 * stat: don't bother with a blocksize.
619 */
620 return (0);
621
622 case PRU_SENDOOB:
623 case PRU_FASTTIMO:
624 case PRU_SLOWTIMO:
625 case PRU_PROTORCV:
626 case PRU_PROTOSEND:
627 error = EOPNOTSUPP;
628 break;
629
630 case PRU_RCVD:
631 case PRU_RCVOOB:
632 return (EOPNOTSUPP); /* do not free mbuf's */
633
634 default:
635 panic("udp_usrreq");
636 }
637
638 release:
639 if (control) {
640 printf("udp control data unexpectedly retained\n");
641 m_freem(control);
642 }
643 if (m)
644 m_freem(m);
645 return (error);
646 }
647
648 static void
649 udp_detach(inp)
650 struct inpcb *inp;
651 {
652 int s = splsoftnet();
653
654 in_pcbdetach(inp);
655 splx(s);
656 }
657
658 /*
659 * Sysctl for udp variables.
660 */
661 int
662 udp_sysctl(name, namelen, oldp, oldlenp, newp, newlen)
663 int *name;
664 u_int namelen;
665 void *oldp;
666 size_t *oldlenp;
667 void *newp;
668 size_t newlen;
669 {
670 /* All sysctl names at this level are terminal. */
671 if (namelen != 1)
672 return (ENOTDIR);
673
674 switch (name[0]) {
675 case UDPCTL_CHECKSUM:
676 return (sysctl_int(oldp, oldlenp, newp, newlen, &udpcksum));
677 default:
678 return (ENOPROTOOPT);
679 }
680 /* NOTREACHED */
681 }
682