xref: /csrg-svn/sys/netinet/udp_usrreq.c (revision 7844)
1*7844Sroot /*	udp_usrreq.c	4.31	82/08/15	*/
24784Swnj 
34784Swnj #include "../h/param.h"
44887Swnj #include "../h/dir.h"
54887Swnj #include "../h/user.h"
64784Swnj #include "../h/mbuf.h"
74805Swnj #include "../h/protosw.h"
84887Swnj #include "../h/socket.h"
94887Swnj #include "../h/socketvar.h"
105094Swnj #include "../net/in.h"
116584Ssam #include "../net/if.h"
126354Ssam #include "../net/route.h"
135094Swnj #include "../net/in_pcb.h"
145094Swnj #include "../net/in_systm.h"
154901Swnj #include "../net/ip.h"
164901Swnj #include "../net/ip_var.h"
176584Ssam #include "../net/ip_icmp.h"
184887Swnj #include "../net/udp.h"
194887Swnj #include "../net/udp_var.h"
206507Ssam #include <errno.h>
214784Swnj 
224926Swnj /*
234926Swnj  * UDP protocol implementation.
244926Swnj  * Per RFC 768, August, 1980.
254926Swnj  */
264805Swnj udp_init()
274805Swnj {
284805Swnj 
294901Swnj 	udb.inp_next = udb.inp_prev = &udb;
304805Swnj }
314805Swnj 
324901Swnj int	udpcksum;
334926Swnj struct	sockaddr_in udp_in = { AF_INET };
344901Swnj 
354926Swnj udp_input(m0)
364926Swnj 	struct mbuf *m0;
374784Swnj {
384901Swnj 	register struct udpiphdr *ui;
394887Swnj 	register struct inpcb *inp;
404926Swnj 	register struct mbuf *m;
41*7844Sroot 	int len;
424784Swnj 
434926Swnj 	/*
445246Sroot 	 * Get IP and UDP header together in first mbuf.
454926Swnj 	 */
464926Swnj 	m = m0;
475308Sroot 	if ((m->m_off > MMAXOFF || m->m_len < sizeof (struct udpiphdr)) &&
485308Sroot 	    (m = m_pullup(m, sizeof (struct udpiphdr))) == 0) {
494926Swnj 		udpstat.udps_hdrops++;
505308Sroot 		return;
514926Swnj 	}
525050Swnj 	ui = mtod(m, struct udpiphdr *);
535246Sroot 	if (((struct ip *)ui)->ip_hl > (sizeof (struct ip) >> 2))
545220Swnj 		ip_stripoptions((struct ip *)ui, (struct mbuf *)0);
554926Swnj 
564926Swnj 	/*
575246Sroot 	 * Make mbuf data length reflect UDP length.
585246Sroot 	 * If not enough data to reflect UDP length, drop.
594926Swnj 	 */
60*7844Sroot 	len = ntohs((u_short)ui->ui_ulen);
614926Swnj 	if (((struct ip *)ui)->ip_len != len) {
624926Swnj 		if (len > ((struct ip *)ui)->ip_len) {
634926Swnj 			udpstat.udps_badlen++;
644926Swnj 			goto bad;
654926Swnj 		}
664926Swnj 		m_adj(m, ((struct ip *)ui)->ip_len - len);
674926Swnj 		/* (struct ip *)ui->ip_len = len; */
684926Swnj 	}
694926Swnj 
704926Swnj 	/*
715246Sroot 	 * Checksum extended UDP header and data.
724926Swnj 	 */
734901Swnj 	if (udpcksum) {
744926Swnj 		ui->ui_next = ui->ui_prev = 0;
754926Swnj 		ui->ui_x1 = 0;
76*7844Sroot 		ui->ui_len = htons((u_short)len);
77*7844Sroot 		if (ui->ui_sum = in_cksum(m, len + sizeof (struct ip))) {
784926Swnj 			udpstat.udps_badsum++;
794901Swnj 			printf("udp cksum %x\n", ui->ui_sum);
804901Swnj 			m_freem(m);
814901Swnj 			return;
824901Swnj 		}
834901Swnj 	}
844926Swnj 
854926Swnj 	/*
86*7844Sroot 	 * Locate pcb for datagram.
874926Swnj 	 */
884926Swnj 	inp = in_pcblookup(&udb,
896029Sroot 	    ui->ui_src, ui->ui_sport, ui->ui_dst, ui->ui_dport,
906029Sroot 		INPLOOKUP_WILDCARD);
916584Ssam 	if (inp == 0) {
926584Ssam 		struct in_addr broadcastaddr;
934926Swnj 
946584Ssam 		broadcastaddr = if_makeaddr(ui->ui_dst.s_net, INADDR_ANY);
956591Ssam 		if (ui->ui_dst.s_addr == broadcastaddr.s_addr)
966584Ssam 			goto bad;
976584Ssam 		icmp_error((struct ip *)ui, ICMP_UNREACH, ICMP_UNREACH_PORT);
986584Ssam 		return;
996584Ssam 	}
1006584Ssam 
1014926Swnj 	/*
1024926Swnj 	 * Construct sockaddr format source address.
1034926Swnj 	 * Stuff source address and datagram in user buffer.
1044926Swnj 	 */
1054926Swnj 	udp_in.sin_port = ui->ui_sport;
1064926Swnj 	udp_in.sin_addr = ui->ui_src;
1075050Swnj 	m->m_len -= sizeof (struct udpiphdr);
1085050Swnj 	m->m_off += sizeof (struct udpiphdr);
1095050Swnj 	if (sbappendaddr(&inp->inp_socket->so_rcv, (struct sockaddr *)&udp_in, m) == 0)
1104926Swnj 		goto bad;
1115050Swnj 	sorwakeup(inp->inp_socket);
1124887Swnj 	return;
1134926Swnj bad:
1144887Swnj 	m_freem(m);
1154784Swnj }
1164784Swnj 
1176584Ssam udp_abort(inp)
1186584Ssam 	struct inpcb *inp;
1196584Ssam {
1206584Ssam 	struct socket *so = inp->inp_socket;
1216584Ssam 
1226584Ssam 	in_pcbdisconnect(inp);
1236584Ssam 	soisdisconnected(so);
1246584Ssam }
1256584Ssam 
1266591Ssam udp_ctlinput(cmd, arg)
1276591Ssam 	int cmd;
1286591Ssam 	caddr_t arg;
1296591Ssam {
1306591Ssam 	struct in_addr *sin;
1316591Ssam 	extern u_char inetctlerrmap[];
1326591Ssam 
1336591Ssam 	if (cmd < 0 || cmd > PRC_NCMDS)
1346591Ssam 		return;
1356591Ssam 	switch (cmd) {
1366591Ssam 
1376591Ssam 	case PRC_ROUTEDEAD:
1386591Ssam 		break;
1396591Ssam 
1406591Ssam 	case PRC_QUENCH:
1416591Ssam 		break;
1426591Ssam 
1436591Ssam 	/* these are handled by ip */
1446591Ssam 	case PRC_IFDOWN:
1456591Ssam 	case PRC_HOSTDEAD:
1466591Ssam 	case PRC_HOSTUNREACH:
1476591Ssam 		break;
1486591Ssam 
1496591Ssam 	default:
1506591Ssam 		sin = &((struct icmp *)arg)->icmp_ip.ip_dst;
1516591Ssam 		in_pcbnotify(&udb, sin, inetctlerrmap[cmd], udp_abort);
1526591Ssam 	}
1536591Ssam }
1546591Ssam 
1554955Swnj udp_output(inp, m0)
1564926Swnj 	struct inpcb *inp;
1574926Swnj 	struct mbuf *m0;
1584784Swnj {
1594926Swnj 	register struct mbuf *m;
1604926Swnj 	register struct udpiphdr *ui;
1617157Swnj 	register struct socket *so;
1624926Swnj 	register int len = 0;
1634784Swnj 
1644926Swnj 	/*
1654926Swnj 	 * Calculate data length and get a mbuf
1665246Sroot 	 * for UDP and IP headers.
1674926Swnj 	 */
1684926Swnj 	for (m = m0; m; m = m->m_next)
1694926Swnj 		len += m->m_len;
1705585Sroot 	m = m_get(M_DONTWAIT);
1716507Ssam 	if (m == 0) {
1726507Ssam 		m_freem(m0);
1736507Ssam 		return (ENOBUFS);
1746507Ssam 	}
1754784Swnj 
1764926Swnj 	/*
1775246Sroot 	 * Fill in mbuf with extended UDP header
1784926Swnj 	 * and addresses and length put into network format.
1794926Swnj 	 */
1804926Swnj 	m->m_off = MMAXOFF - sizeof (struct udpiphdr);
1814926Swnj 	m->m_len = sizeof (struct udpiphdr);
1824926Swnj 	m->m_next = m0;
1834926Swnj 	ui = mtod(m, struct udpiphdr *);
1844926Swnj 	ui->ui_next = ui->ui_prev = 0;
1854926Swnj 	ui->ui_x1 = 0;
1864926Swnj 	ui->ui_pr = IPPROTO_UDP;
187*7844Sroot 	ui->ui_len = len + sizeof (struct udphdr);
1885050Swnj 	ui->ui_src = inp->inp_laddr;
1895050Swnj 	ui->ui_dst = inp->inp_faddr;
1905050Swnj 	ui->ui_sport = inp->inp_lport;
1915050Swnj 	ui->ui_dport = inp->inp_fport;
192*7844Sroot 	ui->ui_ulen = htons((u_short)ui->ui_len);
1934784Swnj 
1944926Swnj 	/*
1954926Swnj 	 * Stuff checksum and output datagram.
1964926Swnj 	 */
1974926Swnj 	ui->ui_sum = 0;
1985094Swnj 	ui->ui_sum = in_cksum(m, sizeof (struct udpiphdr) + len);
1995050Swnj 	((struct ip *)ui)->ip_len = sizeof (struct udpiphdr) + len;
2005050Swnj 	((struct ip *)ui)->ip_ttl = MAXTTL;
2017157Swnj 	so = inp->inp_socket;
2027157Swnj 	return (ip_output(m, (struct mbuf *)0,
2037157Swnj 	    (so->so_options & SO_DONTROUTE) ? &routetoif : (struct route *)0,
2047157Swnj 	    so->so_state & SS_PRIV));
2054784Swnj }
2064784Swnj 
2074887Swnj udp_usrreq(so, req, m, addr)
2084887Swnj 	struct socket *so;
2094784Swnj 	int req;
2104784Swnj 	struct mbuf *m;
2114912Swnj 	caddr_t addr;
2124784Swnj {
2134887Swnj 	struct inpcb *inp = sotoinpcb(so);
2146507Ssam 	int error = 0;
2154784Swnj 
2165051Swnj 	if (inp == 0 && req != PRU_ATTACH)
2175050Swnj 		return (EINVAL);
2184784Swnj 	switch (req) {
2194784Swnj 
2204784Swnj 	case PRU_ATTACH:
2214887Swnj 		if (inp != 0)
2224887Swnj 			return (EINVAL);
2236507Ssam 		error = in_pcbattach(so, &udb, 2048, 2048,
2246507Ssam 				(struct sockaddr_in *)addr);
2254887Swnj 		break;
2264784Swnj 
2274784Swnj 	case PRU_DETACH:
2284887Swnj 		if (inp == 0)
2294887Swnj 			return (ENOTCONN);
2305166Swnj 		in_pcbdetach(inp);
2314887Swnj 		break;
2324784Swnj 
2334784Swnj 	case PRU_CONNECT:
2344955Swnj 		if (inp->inp_faddr.s_addr)
2354887Swnj 			return (EISCONN);
2365166Swnj 		error = in_pcbconnect(inp, (struct sockaddr_in *)addr);
2376507Ssam 		if (error == 0)
2386507Ssam 			soisconnected(so);
2394887Swnj 		break;
2404784Swnj 
2414926Swnj 	case PRU_ACCEPT:
2424926Swnj 		return (EOPNOTSUPP);
2434926Swnj 
2444784Swnj 	case PRU_DISCONNECT:
2454955Swnj 		if (inp->inp_faddr.s_addr == 0)
2464887Swnj 			return (ENOTCONN);
2475166Swnj 		in_pcbdisconnect(inp);
2484887Swnj 		soisdisconnected(so);
2494784Swnj 		break;
2504784Swnj 
2514912Swnj 	case PRU_SHUTDOWN:
2524912Swnj 		socantsendmore(so);
2534912Swnj 		break;
2544912Swnj 
2555996Swnj 	case PRU_SEND: {
2565996Swnj 		struct in_addr laddr;
2575996Swnj 
2584887Swnj 		if (addr) {
2595996Swnj 			laddr = inp->inp_laddr;
2605224Swnj 			if (inp->inp_faddr.s_addr)
2614887Swnj 				return (EISCONN);
2625166Swnj 			error = in_pcbconnect(inp, (struct sockaddr_in *)addr);
2635224Swnj 			if (error)
2646507Ssam 				break;
2654955Swnj 		} else {
2664955Swnj 			if (inp->inp_faddr.s_addr == 0)
2674955Swnj 				return (ENOTCONN);
2684955Swnj 		}
2696507Ssam 		error = udp_output(inp, m);
2705996Swnj 		if (addr) {
2715166Swnj 			in_pcbdisconnect(inp);
2725996Swnj 			inp->inp_laddr = laddr;
2735996Swnj 		}
2745996Swnj 		}
2754784Swnj 		break;
2764784Swnj 
2774784Swnj 	case PRU_ABORT:
2785166Swnj 		in_pcbdetach(inp);
2794887Swnj 		sofree(so);
2804887Swnj 		soisdisconnected(so);
2814784Swnj 		break;
2824784Swnj 
2834784Swnj 	case PRU_CONTROL:
2844887Swnj 		return (EOPNOTSUPP);
2854784Swnj 
2866511Ssam 	case PRU_SOCKADDR:
2876511Ssam 		in_setsockaddr((struct sockaddr_in *)addr, inp);
2886511Ssam 		break;
2896511Ssam 
2904784Swnj 	default:
2914784Swnj 		panic("udp_usrreq");
2924805Swnj 	}
2936507Ssam 	return (error);
2944784Swnj }
295