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