xref: /csrg-svn/sys/netinet/udp_usrreq.c (revision 10262)
1*10262Ssam /*	udp_usrreq.c	4.43	83/01/13	*/
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"
108411Swnj #include "../netinet/in.h"
116584Ssam #include "../net/if.h"
126354Ssam #include "../net/route.h"
138411Swnj #include "../netinet/in_pcb.h"
148411Swnj #include "../netinet/in_systm.h"
158411Swnj #include "../netinet/ip.h"
168411Swnj #include "../netinet/ip_var.h"
178411Swnj #include "../netinet/ip_icmp.h"
188411Swnj #include "../netinet/udp.h"
198411Swnj #include "../netinet/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;
417844Sroot 	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 	 */
607844Sroot 	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;
767844Sroot 		ui->ui_len = htons((u_short)len);
777844Sroot 		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 	/*
867844Sroot 	 * 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) {
9210144Ssam 		/* don't send ICMP response for broadcast packet */
9310144Ssam 		if (in_lnaof(ui->ui_dst) == INADDR_ANY)
946584Ssam 			goto bad;
956584Ssam 		icmp_error((struct ip *)ui, ICMP_UNREACH, ICMP_UNREACH_PORT);
966584Ssam 		return;
976584Ssam 	}
986584Ssam 
994926Swnj 	/*
1004926Swnj 	 * Construct sockaddr format source address.
1014926Swnj 	 * Stuff source address and datagram in user buffer.
1024926Swnj 	 */
1034926Swnj 	udp_in.sin_port = ui->ui_sport;
1044926Swnj 	udp_in.sin_addr = ui->ui_src;
1055050Swnj 	m->m_len -= sizeof (struct udpiphdr);
1065050Swnj 	m->m_off += sizeof (struct udpiphdr);
1075050Swnj 	if (sbappendaddr(&inp->inp_socket->so_rcv, (struct sockaddr *)&udp_in, m) == 0)
1084926Swnj 		goto bad;
1095050Swnj 	sorwakeup(inp->inp_socket);
1104887Swnj 	return;
1114926Swnj bad:
1124887Swnj 	m_freem(m);
1134784Swnj }
1144784Swnj 
1156584Ssam udp_abort(inp)
1166584Ssam 	struct inpcb *inp;
1176584Ssam {
1186584Ssam 	struct socket *so = inp->inp_socket;
1196584Ssam 
1206584Ssam 	in_pcbdisconnect(inp);
1216584Ssam 	soisdisconnected(so);
1226584Ssam }
1236584Ssam 
1246591Ssam udp_ctlinput(cmd, arg)
1256591Ssam 	int cmd;
1266591Ssam 	caddr_t arg;
1276591Ssam {
1286591Ssam 	struct in_addr *sin;
1296591Ssam 	extern u_char inetctlerrmap[];
1306591Ssam 
1316591Ssam 	if (cmd < 0 || cmd > PRC_NCMDS)
1326591Ssam 		return;
1336591Ssam 	switch (cmd) {
1346591Ssam 
1356591Ssam 	case PRC_ROUTEDEAD:
1366591Ssam 		break;
1376591Ssam 
1386591Ssam 	case PRC_QUENCH:
1396591Ssam 		break;
1406591Ssam 
1416591Ssam 	/* these are handled by ip */
1426591Ssam 	case PRC_IFDOWN:
1436591Ssam 	case PRC_HOSTDEAD:
1446591Ssam 	case PRC_HOSTUNREACH:
1456591Ssam 		break;
1466591Ssam 
1476591Ssam 	default:
1486591Ssam 		sin = &((struct icmp *)arg)->icmp_ip.ip_dst;
1498641Sroot 		in_pcbnotify(&udb, sin, (int)inetctlerrmap[cmd], udp_abort);
1506591Ssam 	}
1516591Ssam }
1526591Ssam 
1534955Swnj udp_output(inp, m0)
1544926Swnj 	struct inpcb *inp;
1554926Swnj 	struct mbuf *m0;
1564784Swnj {
1574926Swnj 	register struct mbuf *m;
1584926Swnj 	register struct udpiphdr *ui;
1597157Swnj 	register struct socket *so;
1604926Swnj 	register int len = 0;
1614784Swnj 
1624926Swnj 	/*
1634926Swnj 	 * Calculate data length and get a mbuf
1645246Sroot 	 * for UDP and IP headers.
1654926Swnj 	 */
1664926Swnj 	for (m = m0; m; m = m->m_next)
1674926Swnj 		len += m->m_len;
1689644Ssam 	m = m_get(M_DONTWAIT, MT_HEADER);
1696507Ssam 	if (m == 0) {
1706507Ssam 		m_freem(m0);
1716507Ssam 		return (ENOBUFS);
1726507Ssam 	}
1734784Swnj 
1744926Swnj 	/*
1755246Sroot 	 * Fill in mbuf with extended UDP header
1764926Swnj 	 * and addresses and length put into network format.
1774926Swnj 	 */
1784926Swnj 	m->m_off = MMAXOFF - sizeof (struct udpiphdr);
1794926Swnj 	m->m_len = sizeof (struct udpiphdr);
1804926Swnj 	m->m_next = m0;
1814926Swnj 	ui = mtod(m, struct udpiphdr *);
1824926Swnj 	ui->ui_next = ui->ui_prev = 0;
1834926Swnj 	ui->ui_x1 = 0;
1844926Swnj 	ui->ui_pr = IPPROTO_UDP;
1857844Sroot 	ui->ui_len = len + sizeof (struct udphdr);
1865050Swnj 	ui->ui_src = inp->inp_laddr;
1875050Swnj 	ui->ui_dst = inp->inp_faddr;
1885050Swnj 	ui->ui_sport = inp->inp_lport;
1895050Swnj 	ui->ui_dport = inp->inp_fport;
1907844Sroot 	ui->ui_ulen = htons((u_short)ui->ui_len);
1914784Swnj 
1924926Swnj 	/*
1934926Swnj 	 * Stuff checksum and output datagram.
1944926Swnj 	 */
1954926Swnj 	ui->ui_sum = 0;
1965094Swnj 	ui->ui_sum = in_cksum(m, sizeof (struct udpiphdr) + len);
1975050Swnj 	((struct ip *)ui)->ip_len = sizeof (struct udpiphdr) + len;
1985050Swnj 	((struct ip *)ui)->ip_ttl = MAXTTL;
1997157Swnj 	so = inp->inp_socket;
2007157Swnj 	return (ip_output(m, (struct mbuf *)0,
2017157Swnj 	    (so->so_options & SO_DONTROUTE) ? &routetoif : (struct route *)0,
2027157Swnj 	    so->so_state & SS_PRIV));
2034784Swnj }
2044784Swnj 
2058602Sroot /*ARGSUSED*/
206*10262Ssam udp_usrreq(so, req, m, nam)
2074887Swnj 	struct socket *so;
2084784Swnj 	int req;
2098273Sroot 	struct mbuf *m, *nam;
2104784Swnj {
2114887Swnj 	struct inpcb *inp = sotoinpcb(so);
2126507Ssam 	int error = 0;
2134784Swnj 
2145051Swnj 	if (inp == 0 && req != PRU_ATTACH)
2155050Swnj 		return (EINVAL);
2164784Swnj 	switch (req) {
2174784Swnj 
2184784Swnj 	case PRU_ATTACH:
2194887Swnj 		if (inp != 0)
2204887Swnj 			return (EINVAL);
2218273Sroot 		error = in_pcballoc(so, &udb);
2228273Sroot 		if (error)
2238273Sroot 			break;
2249032Sroot 		error = soreserve(so, 2048, 2048);
2258273Sroot 		if (error)
2268273Sroot 			break;
2274887Swnj 		break;
2284784Swnj 
2294784Swnj 	case PRU_DETACH:
2304887Swnj 		if (inp == 0)
2314887Swnj 			return (ENOTCONN);
2325166Swnj 		in_pcbdetach(inp);
2334887Swnj 		break;
2344784Swnj 
2358273Sroot 	case PRU_BIND:
2368273Sroot 		error = in_pcbbind(inp, nam);
2378273Sroot 		break;
2388273Sroot 
2398273Sroot 	case PRU_LISTEN:
2408273Sroot 		error = EOPNOTSUPP;
2418273Sroot 		break;
2428273Sroot 
2434784Swnj 	case PRU_CONNECT:
24410144Ssam 		if (inp->inp_faddr.s_addr != INADDR_ANY)
2454887Swnj 			return (EISCONN);
2468273Sroot 		error = in_pcbconnect(inp, nam);
2476507Ssam 		if (error == 0)
2486507Ssam 			soisconnected(so);
2494887Swnj 		break;
2504784Swnj 
2514926Swnj 	case PRU_ACCEPT:
2524926Swnj 		return (EOPNOTSUPP);
2534926Swnj 
2544784Swnj 	case PRU_DISCONNECT:
25510144Ssam 		if (inp->inp_faddr.s_addr == INADDR_ANY)
2564887Swnj 			return (ENOTCONN);
2575166Swnj 		in_pcbdisconnect(inp);
2584887Swnj 		soisdisconnected(so);
2594784Swnj 		break;
2604784Swnj 
2614912Swnj 	case PRU_SHUTDOWN:
2624912Swnj 		socantsendmore(so);
2634912Swnj 		break;
2644912Swnj 
2655996Swnj 	case PRU_SEND: {
2665996Swnj 		struct in_addr laddr;
2675996Swnj 
2688273Sroot 		if (nam) {
2695996Swnj 			laddr = inp->inp_laddr;
27010144Ssam 			if (inp->inp_faddr.s_addr != INADDR_ANY)
2714887Swnj 				return (EISCONN);
2728273Sroot 			error = in_pcbconnect(inp, nam);
2735224Swnj 			if (error)
2746507Ssam 				break;
2754955Swnj 		} else {
27610144Ssam 			if (inp->inp_faddr.s_addr == INADDR_ANY)
2774955Swnj 				return (ENOTCONN);
2784955Swnj 		}
2796507Ssam 		error = udp_output(inp, m);
2808273Sroot 		if (nam) {
2815166Swnj 			in_pcbdisconnect(inp);
2825996Swnj 			inp->inp_laddr = laddr;
2835996Swnj 		}
2845996Swnj 		}
2854784Swnj 		break;
2864784Swnj 
2874784Swnj 	case PRU_ABORT:
2885166Swnj 		in_pcbdetach(inp);
2894887Swnj 		sofree(so);
2904887Swnj 		soisdisconnected(so);
2914784Swnj 		break;
2924784Swnj 
2934784Swnj 	case PRU_CONTROL:
2944887Swnj 		return (EOPNOTSUPP);
2954784Swnj 
2966511Ssam 	case PRU_SOCKADDR:
2978273Sroot 		in_setsockaddr(inp, nam);
2986511Ssam 		break;
2996511Ssam 
3004784Swnj 	default:
3014784Swnj 		panic("udp_usrreq");
3024805Swnj 	}
3036507Ssam 	return (error);
3044784Swnj }
305