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