|
|
1.1 cgd 1: /*
2: * Copyright (c) 1982, 1986, 1988, 1990 Regents of the University of California.
3: * All rights reserved.
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.2 cgd 33: * from: @(#)udp_usrreq.c 7.20 (Berkeley) 4/20/91
1.9 ! mycroft 34: * $Id: udp_usrreq.c,v 1.8 1994/01/08 23:19:48 mycroft Exp $
1.1 cgd 35: */
36:
1.5 mycroft 37: #include <sys/param.h>
38: #include <sys/malloc.h>
39: #include <sys/mbuf.h>
40: #include <sys/protosw.h>
41: #include <sys/socket.h>
42: #include <sys/socketvar.h>
43: #include <sys/stat.h>
1.1 cgd 44:
1.5 mycroft 45: #include <net/if.h>
46: #include <net/route.h>
1.1 cgd 47:
1.5 mycroft 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:
1.8 mycroft 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: int udp_ttl = UDP_TTL;
67:
68: struct sockaddr_in udp_in = { sizeof(udp_in), AF_INET };
1.1 cgd 69: struct inpcb *udp_last_inpcb = &udb;
70:
1.7 mycroft 71: static void udp_detach __P((struct inpcb *));
72: static void udp_notify __P((struct inpcb *, int));
73: static struct mbuf *
74: udp_saveopt __P((caddr_t, int, int));
75:
76: void
1.1 cgd 77: udp_init()
78: {
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: */
125: len = ntohs((u_short)uh->uh_ulen);
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.4 hpeyerl 154: #ifdef MULTICAST
155: if (IN_MULTICAST(ntohl(ip->ip_dst.s_addr)) ||
156: in_broadcast(ip->ip_dst)) {
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
171: * fixing the interface. Maybe 4.4BSD will remedy this?)
172: */
173: /*
174: * Construct sockaddr format source address.
175: */
176: udp_in.sin_port = uh->uh_sport;
177: udp_in.sin_addr = ip->ip_src;
178: m->m_len -= sizeof (struct udpiphdr);
179: m->m_data += sizeof (struct udpiphdr);
180: /*
181: * Locate pcb(s) for datagram.
182: * (Algorithm copied from raw_intr().)
183: */
184: last = NULL;
185: for (inp = udb.inp_next; inp != &udb; inp = inp->inp_next) {
186: if (inp->inp_lport != uh->uh_dport)
187: continue;
188: if (inp->inp_laddr.s_addr != INADDR_ANY) {
189: if (inp->inp_laddr.s_addr !=
190: ip->ip_dst.s_addr)
191: continue;
192: }
193: if (inp->inp_faddr.s_addr != INADDR_ANY) {
1.6 mycroft 194: if (inp->inp_faddr.s_addr !=
1.4 hpeyerl 195: ip->ip_src.s_addr ||
196: inp->inp_fport != uh->uh_sport)
197: continue;
198: }
199:
200: if (last != NULL) {
201: struct mbuf *n;
202:
203: if ((n = m_copy(m, 0, M_COPYALL)) != NULL) {
204: if (sbappendaddr(&last->so_rcv,
205: (struct sockaddr *)&udp_in,
206: n, (struct mbuf *)0) == 0)
207: m_freem(n);
208: else
209: sorwakeup(last);
210: }
211: }
212: last = inp->inp_socket;
213: /*
214: * Don't look for additional matches if this one
215: * does not have the SO_REUSEADDR socket option set.
216: * This heuristic avoids searching through all pcbs
217: * in the common case of a non-shared port. It
218: * assumes that an application will never clear
219: * the SO_REUSEADDR option after setting it.
220: */
221: if ((last->so_options & SO_REUSEADDR) == 0)
222: break;
223: }
1.6 mycroft 224:
1.4 hpeyerl 225: if (last == NULL) {
226: /*
227: * No matching pcb found; discard datagram.
228: * (No need to send an ICMP Port Unreachable
229: * for a broadcast or multicast datgram.)
230: */
231: goto bad;
232: }
233: if (sbappendaddr(&last->so_rcv, (struct sockaddr *)&udp_in,
234: m, (struct mbuf *)0) == 0)
235: goto bad;
236: sorwakeup(last);
237: return;
238: }
239: #endif
1.1 cgd 240: /*
241: * Locate pcb for datagram.
242: */
243: inp = udp_last_inpcb;
244: if (inp->inp_lport != uh->uh_dport ||
245: inp->inp_fport != uh->uh_sport ||
246: inp->inp_faddr.s_addr != ip->ip_src.s_addr ||
247: inp->inp_laddr.s_addr != ip->ip_dst.s_addr) {
248: inp = in_pcblookup(&udb, ip->ip_src, uh->uh_sport,
249: ip->ip_dst, uh->uh_dport, INPLOOKUP_WILDCARD);
250: if (inp)
251: udp_last_inpcb = inp;
252: udpstat.udpps_pcbcachemiss++;
253: }
254: if (inp == 0) {
255: /* don't send ICMP response for broadcast packet */
256: udpstat.udps_noport++;
1.4 hpeyerl 257: #ifndef MULTICAST
258: /* XXX why don't we do this with MULTICAST? */
259: if (m->m_flags & (M_BCAST | M_MCAST)) {
1.1 cgd 260: udpstat.udps_noportbcast++;
261: goto bad;
262: }
1.4 hpeyerl 263: #endif
1.1 cgd 264: *ip = save_ip;
265: ip->ip_len += iphlen;
1.9 ! mycroft 266: {
! 267: register struct in_addr foo = {};
! 268: icmp_error(m, ICMP_UNREACH, ICMP_UNREACH_PORT, foo);
! 269: }
1.1 cgd 270: return;
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, (struct sockaddr *)&udp_in,
310: m, 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: */
1.7 mycroft 326: static struct mbuf *
1.1 cgd 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, (caddr_t)(cp + 1), 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: */
1.7 mycroft 351: static void
1.1 cgd 352: udp_notify(inp, errno)
353: register struct inpcb *inp;
1.7 mycroft 354: int errno;
1.1 cgd 355: {
356:
357: inp->inp_socket->so_error = errno;
358: sorwakeup(inp->inp_socket);
359: sowwakeup(inp->inp_socket);
360: }
361:
1.7 mycroft 362: void
1.1 cgd 363: udp_ctlinput(cmd, sa, ip)
364: int cmd;
365: struct sockaddr *sa;
366: register struct ip *ip;
367: {
368: register struct udphdr *uh;
369: extern struct in_addr zeroin_addr;
370: extern u_char inetctlerrmap[];
371:
372: if ((unsigned)cmd > PRC_NCMDS || inetctlerrmap[cmd] == 0)
373: return;
374: if (ip) {
375: uh = (struct udphdr *)((caddr_t)ip + (ip->ip_hl << 2));
376: in_pcbnotify(&udb, sa, uh->uh_dport, ip->ip_src, uh->uh_sport,
377: cmd, udp_notify);
378: } else
379: in_pcbnotify(&udb, sa, 0, zeroin_addr, 0, cmd, udp_notify);
380: }
381:
1.7 mycroft 382: int
1.1 cgd 383: udp_output(inp, m, addr, control)
384: register struct inpcb *inp;
385: register struct mbuf *m;
386: struct mbuf *addr, *control;
387: {
388: register struct udpiphdr *ui;
389: register int len = m->m_pkthdr.len;
390: struct in_addr laddr;
391: int s, error = 0;
392:
393: if (control)
394: m_freem(control); /* XXX */
395:
396: if (addr) {
397: laddr = inp->inp_laddr;
398: if (inp->inp_faddr.s_addr != INADDR_ANY) {
399: error = EISCONN;
400: goto release;
401: }
402: /*
403: * Must block input while temporarily connected.
404: */
405: s = splnet();
406: error = in_pcbconnect(inp, addr);
407: if (error) {
408: splx(s);
409: goto release;
410: }
411: } else {
412: if (inp->inp_faddr.s_addr == INADDR_ANY) {
413: error = ENOTCONN;
414: goto release;
415: }
416: }
417: /*
418: * Calculate data length and get a mbuf
419: * for UDP and IP headers.
420: */
421: M_PREPEND(m, sizeof(struct udpiphdr), M_WAIT);
422:
423: /*
424: * Fill in mbuf with extended UDP header
425: * and addresses and length put into network format.
426: */
427: ui = mtod(m, struct udpiphdr *);
428: ui->ui_next = ui->ui_prev = 0;
429: ui->ui_x1 = 0;
430: ui->ui_pr = IPPROTO_UDP;
431: ui->ui_len = htons((u_short)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: error = ip_output(m, inp->inp_options, &inp->inp_route,
1.4 hpeyerl 451: inp->inp_socket->so_options & (SO_DONTROUTE | SO_BROADCAST)
452: #ifdef MULTICAST
453: | IP_MULTICASTOPTS, inp->inp_moptions
454: #endif
455: );
1.1 cgd 456:
457: if (addr) {
458: in_pcbdisconnect(inp);
459: inp->inp_laddr = laddr;
460: splx(s);
461: }
462: return (error);
463:
464: release:
465: m_freem(m);
466: return (error);
467: }
468:
469: u_long udp_sendspace = 9216; /* really max datagram size */
470: u_long udp_recvspace = 40 * (1024 + sizeof(struct sockaddr_in));
471: /* 40 1K datagrams */
472:
473: /*ARGSUSED*/
1.7 mycroft 474: int
1.1 cgd 475: udp_usrreq(so, req, m, addr, control)
476: struct socket *so;
477: int req;
478: struct mbuf *m, *addr, *control;
479: {
480: struct inpcb *inp = sotoinpcb(so);
481: int error = 0;
482: int s;
483:
484: if (req == PRU_CONTROL)
485: return (in_control(so, (int)m, (caddr_t)addr,
486: (struct ifnet *)control));
487: if (inp == NULL && req != PRU_ATTACH) {
488: error = EINVAL;
489: goto release;
490: }
491: /*
492: * Note: need to block udp_input while changing
493: * the udp pcb queue and/or pcb addresses.
494: */
495: switch (req) {
496:
497: case PRU_ATTACH:
498: if (inp != NULL) {
499: error = EINVAL;
500: break;
501: }
502: s = splnet();
503: error = in_pcballoc(so, &udb);
504: splx(s);
505: if (error)
506: break;
507: error = soreserve(so, udp_sendspace, udp_recvspace);
508: if (error)
509: break;
510: ((struct inpcb *) so->so_pcb)->inp_ip.ip_ttl = udp_ttl;
511: break;
512:
513: case PRU_DETACH:
514: udp_detach(inp);
515: break;
516:
517: case PRU_BIND:
518: s = splnet();
519: error = in_pcbbind(inp, addr);
520: splx(s);
521: break;
522:
523: case PRU_LISTEN:
524: error = EOPNOTSUPP;
525: break;
526:
527: case PRU_CONNECT:
528: if (inp->inp_faddr.s_addr != INADDR_ANY) {
529: error = EISCONN;
530: break;
531: }
532: s = splnet();
533: error = in_pcbconnect(inp, addr);
534: splx(s);
535: if (error == 0)
536: soisconnected(so);
537: break;
538:
539: case PRU_CONNECT2:
540: error = EOPNOTSUPP;
541: break;
542:
543: case PRU_ACCEPT:
544: error = EOPNOTSUPP;
545: break;
546:
547: case PRU_DISCONNECT:
548: if (inp->inp_faddr.s_addr == INADDR_ANY) {
549: error = ENOTCONN;
550: break;
551: }
552: s = splnet();
553: in_pcbdisconnect(inp);
554: inp->inp_laddr.s_addr = INADDR_ANY;
555: splx(s);
556: so->so_state &= ~SS_ISCONNECTED; /* XXX */
557: break;
558:
559: case PRU_SHUTDOWN:
560: socantsendmore(so);
561: break;
562:
563: case PRU_SEND:
564: return (udp_output(inp, m, addr, control));
565:
566: case PRU_ABORT:
567: soisdisconnected(so);
568: udp_detach(inp);
569: break;
570:
571: case PRU_SOCKADDR:
572: in_setsockaddr(inp, addr);
573: break;
574:
575: case PRU_PEERADDR:
576: in_setpeeraddr(inp, addr);
577: break;
578:
579: case PRU_SENSE:
580: /*
581: * stat: don't bother with a blocksize.
582: */
583: return (0);
584:
585: case PRU_SENDOOB:
586: case PRU_FASTTIMO:
587: case PRU_SLOWTIMO:
588: case PRU_PROTORCV:
589: case PRU_PROTOSEND:
590: error = EOPNOTSUPP;
591: break;
592:
593: case PRU_RCVD:
594: case PRU_RCVOOB:
595: return (EOPNOTSUPP); /* do not free mbuf's */
596:
597: default:
598: panic("udp_usrreq");
599: }
600:
601: release:
602: if (control) {
603: printf("udp control data unexpectedly retained\n");
604: m_freem(control);
605: }
606: if (m)
607: m_freem(m);
608: return (error);
609: }
610:
1.7 mycroft 611: static void
1.1 cgd 612: udp_detach(inp)
613: struct inpcb *inp;
614: {
615: int s = splnet();
616:
617: if (inp == udp_last_inpcb)
618: udp_last_inpcb = &udb;
619: in_pcbdetach(inp);
620: splx(s);
621: }