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