Home | History | Annotate | Line # | Download | only in netinet
udp_usrreq.c revision 1.12
      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  *
     33  *	from: @(#)udp_usrreq.c	7.20 (Berkeley) 4/20/91
     34  *	$Id: udp_usrreq.c,v 1.12 1994/02/10 18:46:06 mycroft Exp $
     35  */
     36 
     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>
     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>
     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 int	udp_ttl = UDP_TTL;
     67 
     68 struct	sockaddr_in udp_in = { sizeof(udp_in), AF_INET };
     69 struct	inpcb *udp_last_inpcb = &udb;
     70 
     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
     77 udp_init()
     78 {
     79 
     80 	udb.inp_next = udb.inp_prev = &udb;
     81 }
     82 
     83 void
     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 	}
    154 	if (IN_MULTICAST(ntohl(ip->ip_dst.s_addr)) ||
    155 	    in_broadcast(ip->ip_dst)) {
    156 		struct socket *last;
    157 		/*
    158 		 * Deliver a multicast or broadcast datagram to *all* sockets
    159 		 * for which the local and remote addresses and ports match
    160 		 * those of the incoming datagram.  This allows more than
    161 		 * one process to receive multi/broadcasts on the same port.
    162 		 * (This really ought to be done for unicast datagrams as
    163 		 * well, but that would cause problems with existing
    164 		 * applications that open both address-specific sockets and
    165 		 * a wildcard socket listening to the same port -- they would
    166 		 * end up receiving duplicates of every unicast datagram.
    167 		 * Those applications open the multiple sockets to overcome an
    168 		 * inadequacy of the UDP socket interface, but for backwards
    169 		 * compatibility we avoid the problem here rather than
    170 		 * fixing the interface.  Maybe 4.4BSD will remedy this?)
    171 		 */
    172 		/*
    173 		 * Construct sockaddr format source address.
    174 		 */
    175 		udp_in.sin_port = uh->uh_sport;
    176 		udp_in.sin_addr = ip->ip_src;
    177 		m->m_len -= sizeof (struct udpiphdr);
    178 		m->m_data += sizeof (struct udpiphdr);
    179 		/*
    180 		 * Locate pcb(s) for datagram.
    181 		 * (Algorithm copied from raw_intr().)
    182 		 */
    183 		last = NULL;
    184 		for (inp = udb.inp_next; inp != &udb; inp = inp->inp_next) {
    185 			if (inp->inp_lport != uh->uh_dport)
    186 				continue;
    187 			if (inp->inp_laddr.s_addr != INADDR_ANY) {
    188 				if (inp->inp_laddr.s_addr !=
    189 				    ip->ip_dst.s_addr)
    190 					continue;
    191 			}
    192 			if (inp->inp_faddr.s_addr != INADDR_ANY) {
    193 				if (inp->inp_faddr.s_addr !=
    194 				    ip->ip_src.s_addr ||
    195 				    inp->inp_fport != uh->uh_sport)
    196 					continue;
    197 			}
    198 
    199 			if (last != NULL) {
    200 				struct mbuf *n;
    201 
    202 				if ((n = m_copy(m, 0, M_COPYALL)) != NULL) {
    203 					if (sbappendaddr(&last->so_rcv,
    204 						(struct sockaddr *)&udp_in,
    205 						n, (struct mbuf *)0) == 0)
    206 						m_freem(n);
    207 					else
    208 						sorwakeup(last);
    209 				}
    210 			}
    211 			last = inp->inp_socket;
    212 			/*
    213 			 * Don't look for additional matches if this one
    214 			 * does not have the SO_REUSEADDR socket option set.
    215 			 * This heuristic avoids searching through all pcbs
    216 			 * in the common case of a non-shared port.  It
    217 			 * assumes that an application will never clear
    218 			 * the SO_REUSEADDR option after setting it.
    219 			 */
    220 			if ((last->so_options & 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 			goto bad;
    231 		}
    232 		if (sbappendaddr(&last->so_rcv, (struct sockaddr *)&udp_in,
    233 		     m, (struct mbuf *)0) == 0)
    234 			goto bad;
    235 		sorwakeup(last);
    236 		return;
    237 	}
    238 	/*
    239 	 * Locate pcb for datagram.
    240 	 */
    241 	inp = udp_last_inpcb;
    242 	if (inp->inp_lport != uh->uh_dport ||
    243 	    inp->inp_fport != uh->uh_sport ||
    244 	    inp->inp_faddr.s_addr != ip->ip_src.s_addr ||
    245 	    inp->inp_laddr.s_addr != ip->ip_dst.s_addr) {
    246 		inp = in_pcblookup(&udb, ip->ip_src, uh->uh_sport,
    247 		    ip->ip_dst, uh->uh_dport, INPLOOKUP_WILDCARD);
    248 		if (inp)
    249 			udp_last_inpcb = inp;
    250 		udpstat.udpps_pcbcachemiss++;
    251 	}
    252 	if (inp == 0) {
    253 		/* don't send ICMP response for broadcast packet */
    254 		udpstat.udps_noport++;
    255 		*ip = save_ip;
    256 		ip->ip_len += iphlen;
    257 	    {
    258 		register struct in_addr foo = {};
    259 		icmp_error(m, ICMP_UNREACH, ICMP_UNREACH_PORT, foo);
    260 	    }
    261 		return;
    262 	}
    263 
    264 	/*
    265 	 * Construct sockaddr format source address.
    266 	 * Stuff source address and datagram in user buffer.
    267 	 */
    268 	udp_in.sin_port = uh->uh_sport;
    269 	udp_in.sin_addr = ip->ip_src;
    270 	if (inp->inp_flags & INP_CONTROLOPTS) {
    271 		struct mbuf **mp = &opts;
    272 
    273 		if (inp->inp_flags & INP_RECVDSTADDR) {
    274 			*mp = udp_saveopt((caddr_t) &ip->ip_dst,
    275 			    sizeof(struct in_addr), IP_RECVDSTADDR);
    276 			if (*mp)
    277 				mp = &(*mp)->m_next;
    278 		}
    279 #ifdef notyet
    280 		/* options were tossed above */
    281 		if (inp->inp_flags & INP_RECVOPTS) {
    282 			*mp = udp_saveopt((caddr_t) opts_deleted_above,
    283 			    sizeof(struct in_addr), IP_RECVOPTS);
    284 			if (*mp)
    285 				mp = &(*mp)->m_next;
    286 		}
    287 		/* ip_srcroute doesn't do what we want here, need to fix */
    288 		if (inp->inp_flags & INP_RECVRETOPTS) {
    289 			*mp = udp_saveopt((caddr_t) ip_srcroute(),
    290 			    sizeof(struct in_addr), IP_RECVRETOPTS);
    291 			if (*mp)
    292 				mp = &(*mp)->m_next;
    293 		}
    294 #endif
    295 	}
    296 	iphlen += sizeof(struct udphdr);
    297 	m->m_len -= iphlen;
    298 	m->m_pkthdr.len -= iphlen;
    299 	m->m_data += iphlen;
    300 	if (sbappendaddr(&inp->inp_socket->so_rcv, (struct sockaddr *)&udp_in,
    301 	    m, opts) == 0) {
    302 		udpstat.udps_fullsock++;
    303 		goto bad;
    304 	}
    305 	sorwakeup(inp->inp_socket);
    306 	return;
    307 bad:
    308 	m_freem(m);
    309 	if (opts)
    310 		m_freem(opts);
    311 }
    312 
    313 /*
    314  * Create a "control" mbuf containing the specified data
    315  * with the specified type for presentation with a datagram.
    316  */
    317 static struct mbuf *
    318 udp_saveopt(p, size, type)
    319 	caddr_t p;
    320 	register int size;
    321 	int type;
    322 {
    323 	register struct cmsghdr *cp;
    324 	struct mbuf *m;
    325 
    326 	if ((m = m_get(M_DONTWAIT, MT_CONTROL)) == NULL)
    327 		return ((struct mbuf *) NULL);
    328 	cp = (struct cmsghdr *) mtod(m, struct cmsghdr *);
    329 	bcopy(p, (caddr_t)(cp + 1), size);
    330 	size += sizeof(*cp);
    331 	m->m_len = size;
    332 	cp->cmsg_len = size;
    333 	cp->cmsg_level = IPPROTO_IP;
    334 	cp->cmsg_type = type;
    335 	return (m);
    336 }
    337 
    338 /*
    339  * Notify a udp user of an asynchronous error;
    340  * just wake up so that he can collect error status.
    341  */
    342 static void
    343 udp_notify(inp, errno)
    344 	register struct inpcb *inp;
    345 	int errno;
    346 {
    347 
    348 	inp->inp_socket->so_error = errno;
    349 	sorwakeup(inp->inp_socket);
    350 	sowwakeup(inp->inp_socket);
    351 }
    352 
    353 void
    354 udp_ctlinput(cmd, sa, ip)
    355 	int cmd;
    356 	struct sockaddr *sa;
    357 	register struct ip *ip;
    358 {
    359 	register struct udphdr *uh;
    360 	extern struct in_addr zeroin_addr;
    361 	extern u_char inetctlerrmap[];
    362 
    363 	if ((unsigned)cmd > PRC_NCMDS || inetctlerrmap[cmd] == 0)
    364 		return;
    365 	if (ip) {
    366 		uh = (struct udphdr *)((caddr_t)ip + (ip->ip_hl << 2));
    367 		in_pcbnotify(&udb, sa, uh->uh_dport, ip->ip_src, uh->uh_sport,
    368 			cmd, udp_notify);
    369 	} else
    370 		in_pcbnotify(&udb, sa, 0, zeroin_addr, 0, cmd, udp_notify);
    371 }
    372 
    373 int
    374 udp_output(inp, m, addr, control)
    375 	register struct inpcb *inp;
    376 	register struct mbuf *m;
    377 	struct mbuf *addr, *control;
    378 {
    379 	register struct udpiphdr *ui;
    380 	register int len = m->m_pkthdr.len;
    381 	struct in_addr laddr;
    382 	int s, error = 0;
    383 
    384 	if (control)
    385 		m_freem(control);		/* XXX */
    386 
    387 	if (addr) {
    388 		laddr = inp->inp_laddr;
    389 		if (inp->inp_faddr.s_addr != INADDR_ANY) {
    390 			error = EISCONN;
    391 			goto release;
    392 		}
    393 		/*
    394 		 * Must block input while temporarily connected.
    395 		 */
    396 		s = splnet();
    397 		error = in_pcbconnect(inp, addr);
    398 		if (error) {
    399 			splx(s);
    400 			goto release;
    401 		}
    402 	} else {
    403 		if (inp->inp_faddr.s_addr == INADDR_ANY) {
    404 			error = ENOTCONN;
    405 			goto release;
    406 		}
    407 	}
    408 	/*
    409 	 * Calculate data length and get a mbuf
    410 	 * for UDP and IP headers.
    411 	 */
    412 	M_PREPEND(m, sizeof(struct udpiphdr), M_WAIT);
    413 
    414 	/*
    415 	 * Fill in mbuf with extended UDP header
    416 	 * and addresses and length put into network format.
    417 	 */
    418 	ui = mtod(m, struct udpiphdr *);
    419 	ui->ui_next = ui->ui_prev = 0;
    420 	ui->ui_x1 = 0;
    421 	ui->ui_pr = IPPROTO_UDP;
    422 	ui->ui_len = htons((u_short)len + sizeof (struct udphdr));
    423 	ui->ui_src = inp->inp_laddr;
    424 	ui->ui_dst = inp->inp_faddr;
    425 	ui->ui_sport = inp->inp_lport;
    426 	ui->ui_dport = inp->inp_fport;
    427 	ui->ui_ulen = ui->ui_len;
    428 
    429 	/*
    430 	 * Stuff checksum and output datagram.
    431 	 */
    432 	ui->ui_sum = 0;
    433 	if (udpcksum) {
    434 	    if ((ui->ui_sum = in_cksum(m, sizeof (struct udpiphdr) + len)) == 0)
    435 		ui->ui_sum = 0xffff;
    436 	}
    437 	((struct ip *)ui)->ip_len = sizeof (struct udpiphdr) + len;
    438 	((struct ip *)ui)->ip_ttl = inp->inp_ip.ip_ttl;	/* XXX */
    439 	((struct ip *)ui)->ip_tos = inp->inp_ip.ip_tos;	/* XXX */
    440 	udpstat.udps_opackets++;
    441 	error = ip_output(m, inp->inp_options, &inp->inp_route,
    442 	    inp->inp_socket->so_options & (SO_DONTROUTE | SO_BROADCAST),
    443 	    inp->inp_moptions);
    444 
    445 	if (addr) {
    446 		in_pcbdisconnect(inp);
    447 		inp->inp_laddr = laddr;
    448 		splx(s);
    449 	}
    450 	return (error);
    451 
    452 release:
    453 	m_freem(m);
    454 	return (error);
    455 }
    456 
    457 u_long	udp_sendspace = 9216;		/* really max datagram size */
    458 u_long	udp_recvspace = 40 * (1024 + sizeof(struct sockaddr_in));
    459 					/* 40 1K datagrams */
    460 
    461 /*ARGSUSED*/
    462 int
    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 static void
    600 udp_detach(inp)
    601 	struct inpcb *inp;
    602 {
    603 	int s = splnet();
    604 
    605 	if (inp == udp_last_inpcb)
    606 		udp_last_inpcb = &udb;
    607 	in_pcbdetach(inp);
    608 	splx(s);
    609 }
    610