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