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