|
|
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.7 ! mycroft 34: * $Id: udp_usrreq.c,v 1.6 1994/01/08 21:22:06 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:
57: struct inpcb *udp_last_inpcb = &udb;
58:
1.7 ! mycroft 59: static void udp_detach __P((struct inpcb *));
! 60: static void udp_notify __P((struct inpcb *, int));
! 61: static struct mbuf *
! 62: udp_saveopt __P((caddr_t, int, int));
! 63:
1.1 cgd 64: /*
65: * UDP protocol implementation.
66: * Per RFC 768, August, 1980.
67: */
1.7 ! mycroft 68: void
1.1 cgd 69: udp_init()
70: {
71:
72: udb.inp_next = udb.inp_prev = &udb;
73: }
74:
75: #ifndef COMPAT_42
76: int udpcksum = 1;
77: #else
78: int udpcksum = 0; /* XXX */
79: #endif
80: int udp_ttl = UDP_TTL;
81:
82: struct sockaddr_in udp_in = { sizeof(udp_in), AF_INET };
83:
1.7 ! mycroft 84: void
1.1 cgd 85: udp_input(m, iphlen)
86: register struct mbuf *m;
87: int iphlen;
88: {
89: register struct ip *ip;
90: register struct udphdr *uh;
91: register struct inpcb *inp;
92: struct mbuf *opts = 0;
93: int len;
94: struct ip save_ip;
95:
96: udpstat.udps_ipackets++;
97:
98: /*
99: * Strip IP options, if any; should skip this,
100: * make available to user, and use on returned packets,
101: * but we don't yet have a way to check the checksum
102: * with options still present.
103: */
104: if (iphlen > sizeof (struct ip)) {
105: ip_stripoptions(m, (struct mbuf *)0);
106: iphlen = sizeof(struct ip);
107: }
108:
109: /*
110: * Get IP and UDP header together in first mbuf.
111: */
112: ip = mtod(m, struct ip *);
113: if (m->m_len < iphlen + sizeof(struct udphdr)) {
114: if ((m = m_pullup(m, iphlen + sizeof(struct udphdr))) == 0) {
115: udpstat.udps_hdrops++;
116: return;
117: }
118: ip = mtod(m, struct ip *);
119: }
120: uh = (struct udphdr *)((caddr_t)ip + iphlen);
121:
122: /*
123: * Make mbuf data length reflect UDP length.
124: * If not enough data to reflect UDP length, drop.
125: */
126: len = ntohs((u_short)uh->uh_ulen);
127: if (ip->ip_len != len) {
128: if (len > ip->ip_len) {
129: udpstat.udps_badlen++;
130: goto bad;
131: }
132: m_adj(m, len - ip->ip_len);
133: /* ip->ip_len = len; */
134: }
135: /*
136: * Save a copy of the IP header in case we want restore it
137: * for sending an ICMP error message in response.
138: */
139: save_ip = *ip;
140:
141: /*
142: * Checksum extended UDP header and data.
143: */
144: if (udpcksum && uh->uh_sum) {
145: ((struct ipovly *)ip)->ih_next = 0;
146: ((struct ipovly *)ip)->ih_prev = 0;
147: ((struct ipovly *)ip)->ih_x1 = 0;
148: ((struct ipovly *)ip)->ih_len = uh->uh_ulen;
149: if (uh->uh_sum = in_cksum(m, len + sizeof (struct ip))) {
150: udpstat.udps_badsum++;
151: m_freem(m);
152: return;
153: }
154: }
1.4 hpeyerl 155: #ifdef MULTICAST
156: if (IN_MULTICAST(ntohl(ip->ip_dst.s_addr)) ||
157: in_broadcast(ip->ip_dst)) {
158: struct socket *last;
159: /*
160: * Deliver a multicast or broadcast datagram to *all* sockets
161: * for which the local and remote addresses and ports match
162: * those of the incoming datagram. This allows more than
163: * one process to receive multi/broadcasts on the same port.
164: * (This really ought to be done for unicast datagrams as
165: * well, but that would cause problems with existing
166: * applications that open both address-specific sockets and
167: * a wildcard socket listening to the same port -- they would
168: * end up receiving duplicates of every unicast datagram.
169: * Those applications open the multiple sockets to overcome an
170: * inadequacy of the UDP socket interface, but for backwards
171: * compatibility we avoid the problem here rather than
172: * fixing the interface. Maybe 4.4BSD will remedy this?)
173: */
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,
206: (struct sockaddr *)&udp_in,
207: n, (struct mbuf *)0) == 0)
208: m_freem(n);
209: else
210: sorwakeup(last);
211: }
212: }
213: last = inp->inp_socket;
214: /*
215: * Don't look for additional matches if this one
216: * does not have the SO_REUSEADDR socket option set.
217: * This heuristic avoids searching through all pcbs
218: * in the common case of a non-shared port. It
219: * assumes that an application will never clear
220: * the SO_REUSEADDR option after setting it.
221: */
222: if ((last->so_options & SO_REUSEADDR) == 0)
223: break;
224: }
1.6 mycroft 225:
1.4 hpeyerl 226: if (last == NULL) {
227: /*
228: * No matching pcb found; discard datagram.
229: * (No need to send an ICMP Port Unreachable
230: * for a broadcast or multicast datgram.)
231: */
232: goto bad;
233: }
234: if (sbappendaddr(&last->so_rcv, (struct sockaddr *)&udp_in,
235: m, (struct mbuf *)0) == 0)
236: goto bad;
237: sorwakeup(last);
238: return;
239: }
240: #endif
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: /* don't send ICMP response for broadcast packet */
257: udpstat.udps_noport++;
1.4 hpeyerl 258: #ifndef MULTICAST
259: /* XXX why don't we do this with MULTICAST? */
260: if (m->m_flags & (M_BCAST | M_MCAST)) {
1.1 cgd 261: udpstat.udps_noportbcast++;
262: goto bad;
263: }
1.4 hpeyerl 264: #endif
1.1 cgd 265: *ip = save_ip;
266: ip->ip_len += iphlen;
267: icmp_error(m, ICMP_UNREACH, ICMP_UNREACH_PORT);
268: return;
269: }
270:
271: /*
272: * Construct sockaddr format source address.
273: * Stuff source address and datagram in user buffer.
274: */
275: udp_in.sin_port = uh->uh_sport;
276: udp_in.sin_addr = ip->ip_src;
277: if (inp->inp_flags & INP_CONTROLOPTS) {
278: struct mbuf **mp = &opts;
279:
280: if (inp->inp_flags & INP_RECVDSTADDR) {
281: *mp = udp_saveopt((caddr_t) &ip->ip_dst,
282: sizeof(struct in_addr), IP_RECVDSTADDR);
283: if (*mp)
284: mp = &(*mp)->m_next;
285: }
286: #ifdef notyet
287: /* options were tossed above */
288: if (inp->inp_flags & INP_RECVOPTS) {
289: *mp = udp_saveopt((caddr_t) opts_deleted_above,
290: sizeof(struct in_addr), IP_RECVOPTS);
291: if (*mp)
292: mp = &(*mp)->m_next;
293: }
294: /* ip_srcroute doesn't do what we want here, need to fix */
295: if (inp->inp_flags & INP_RECVRETOPTS) {
296: *mp = udp_saveopt((caddr_t) ip_srcroute(),
297: sizeof(struct in_addr), IP_RECVRETOPTS);
298: if (*mp)
299: mp = &(*mp)->m_next;
300: }
301: #endif
302: }
303: iphlen += sizeof(struct udphdr);
304: m->m_len -= iphlen;
305: m->m_pkthdr.len -= iphlen;
306: m->m_data += iphlen;
307: if (sbappendaddr(&inp->inp_socket->so_rcv, (struct sockaddr *)&udp_in,
308: m, opts) == 0) {
309: udpstat.udps_fullsock++;
310: goto bad;
311: }
312: sorwakeup(inp->inp_socket);
313: return;
314: bad:
315: m_freem(m);
316: if (opts)
317: m_freem(opts);
318: }
319:
320: /*
321: * Create a "control" mbuf containing the specified data
322: * with the specified type for presentation with a datagram.
323: */
1.7 ! mycroft 324: static struct mbuf *
1.1 cgd 325: udp_saveopt(p, size, type)
326: caddr_t p;
327: register int size;
328: int type;
329: {
330: register struct cmsghdr *cp;
331: struct mbuf *m;
332:
333: if ((m = m_get(M_DONTWAIT, MT_CONTROL)) == NULL)
334: return ((struct mbuf *) NULL);
335: cp = (struct cmsghdr *) mtod(m, struct cmsghdr *);
336: bcopy(p, (caddr_t)(cp + 1), size);
337: size += sizeof(*cp);
338: m->m_len = size;
339: cp->cmsg_len = size;
340: cp->cmsg_level = IPPROTO_IP;
341: cp->cmsg_type = type;
342: return (m);
343: }
344:
345: /*
346: * Notify a udp user of an asynchronous error;
347: * just wake up so that he can collect error status.
348: */
1.7 ! mycroft 349: static void
1.1 cgd 350: udp_notify(inp, errno)
351: register struct inpcb *inp;
1.7 ! mycroft 352: int errno;
1.1 cgd 353: {
354:
355: inp->inp_socket->so_error = errno;
356: sorwakeup(inp->inp_socket);
357: sowwakeup(inp->inp_socket);
358: }
359:
1.7 ! mycroft 360: void
1.1 cgd 361: udp_ctlinput(cmd, sa, ip)
362: int cmd;
363: struct sockaddr *sa;
364: register struct ip *ip;
365: {
366: register struct udphdr *uh;
367: extern struct in_addr zeroin_addr;
368: extern u_char inetctlerrmap[];
369:
370: if ((unsigned)cmd > PRC_NCMDS || inetctlerrmap[cmd] == 0)
371: return;
372: if (ip) {
373: uh = (struct udphdr *)((caddr_t)ip + (ip->ip_hl << 2));
374: in_pcbnotify(&udb, sa, uh->uh_dport, ip->ip_src, uh->uh_sport,
375: cmd, udp_notify);
376: } else
377: in_pcbnotify(&udb, sa, 0, zeroin_addr, 0, cmd, udp_notify);
378: }
379:
1.7 ! mycroft 380: int
1.1 cgd 381: udp_output(inp, m, addr, control)
382: register struct inpcb *inp;
383: register struct mbuf *m;
384: struct mbuf *addr, *control;
385: {
386: register struct udpiphdr *ui;
387: register int len = m->m_pkthdr.len;
388: struct in_addr laddr;
389: int s, error = 0;
390:
391: if (control)
392: m_freem(control); /* XXX */
393:
394: if (addr) {
395: laddr = inp->inp_laddr;
396: if (inp->inp_faddr.s_addr != INADDR_ANY) {
397: error = EISCONN;
398: goto release;
399: }
400: /*
401: * Must block input while temporarily connected.
402: */
403: s = splnet();
404: error = in_pcbconnect(inp, addr);
405: if (error) {
406: splx(s);
407: goto release;
408: }
409: } else {
410: if (inp->inp_faddr.s_addr == INADDR_ANY) {
411: error = ENOTCONN;
412: goto release;
413: }
414: }
415: /*
416: * Calculate data length and get a mbuf
417: * for UDP and IP headers.
418: */
419: M_PREPEND(m, sizeof(struct udpiphdr), M_WAIT);
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.4 hpeyerl 449: inp->inp_socket->so_options & (SO_DONTROUTE | SO_BROADCAST)
450: #ifdef MULTICAST
451: | IP_MULTICASTOPTS, inp->inp_moptions
452: #endif
453: );
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)
483: return (in_control(so, (int)m, (caddr_t)addr,
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;
508: ((struct inpcb *) so->so_pcb)->inp_ip.ip_ttl = udp_ttl;
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);
619: }