xref: /csrg-svn/sys/netinet/udp_usrreq.c (revision 8551)
1*8551Sroot /*	udp_usrreq.c	4.34	82/10/16	*/
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) {
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);
109*8551Sroot SBCHECK(&inp->inp_socket->so_rcv, "udpinput before");
1105050Swnj 	if (sbappendaddr(&inp->inp_socket->so_rcv, (struct sockaddr *)&udp_in, m) == 0)
1114926Swnj 		goto bad;
112*8551Sroot SBCHECK(&inp->inp_socket->so_rcv, "udpinput after");
1135050Swnj 	sorwakeup(inp->inp_socket);
1144887Swnj 	return;
1154926Swnj bad:
1164887Swnj 	m_freem(m);
1174784Swnj }
1184784Swnj 
1196584Ssam udp_abort(inp)
1206584Ssam 	struct inpcb *inp;
1216584Ssam {
1226584Ssam 	struct socket *so = inp->inp_socket;
1236584Ssam 
1246584Ssam 	in_pcbdisconnect(inp);
1256584Ssam 	soisdisconnected(so);
1266584Ssam }
1276584Ssam 
1286591Ssam udp_ctlinput(cmd, arg)
1296591Ssam 	int cmd;
1306591Ssam 	caddr_t arg;
1316591Ssam {
1326591Ssam 	struct in_addr *sin;
1336591Ssam 	extern u_char inetctlerrmap[];
1346591Ssam 
1356591Ssam 	if (cmd < 0 || cmd > PRC_NCMDS)
1366591Ssam 		return;
1376591Ssam 	switch (cmd) {
1386591Ssam 
1396591Ssam 	case PRC_ROUTEDEAD:
1406591Ssam 		break;
1416591Ssam 
1426591Ssam 	case PRC_QUENCH:
1436591Ssam 		break;
1446591Ssam 
1456591Ssam 	/* these are handled by ip */
1466591Ssam 	case PRC_IFDOWN:
1476591Ssam 	case PRC_HOSTDEAD:
1486591Ssam 	case PRC_HOSTUNREACH:
1496591Ssam 		break;
1506591Ssam 
1516591Ssam 	default:
1526591Ssam 		sin = &((struct icmp *)arg)->icmp_ip.ip_dst;
1536591Ssam 		in_pcbnotify(&udb, sin, inetctlerrmap[cmd], udp_abort);
1546591Ssam 	}
1556591Ssam }
1566591Ssam 
1574955Swnj udp_output(inp, m0)
1584926Swnj 	struct inpcb *inp;
1594926Swnj 	struct mbuf *m0;
1604784Swnj {
1614926Swnj 	register struct mbuf *m;
1624926Swnj 	register struct udpiphdr *ui;
1637157Swnj 	register struct socket *so;
1644926Swnj 	register int len = 0;
1654784Swnj 
1664926Swnj 	/*
1674926Swnj 	 * Calculate data length and get a mbuf
1685246Sroot 	 * for UDP and IP headers.
1694926Swnj 	 */
1704926Swnj 	for (m = m0; m; m = m->m_next)
1714926Swnj 		len += m->m_len;
1725585Sroot 	m = m_get(M_DONTWAIT);
1736507Ssam 	if (m == 0) {
1746507Ssam 		m_freem(m0);
1756507Ssam 		return (ENOBUFS);
1766507Ssam 	}
1774784Swnj 
1784926Swnj 	/*
1795246Sroot 	 * Fill in mbuf with extended UDP header
1804926Swnj 	 * and addresses and length put into network format.
1814926Swnj 	 */
1824926Swnj 	m->m_off = MMAXOFF - sizeof (struct udpiphdr);
1834926Swnj 	m->m_len = sizeof (struct udpiphdr);
1844926Swnj 	m->m_next = m0;
1854926Swnj 	ui = mtod(m, struct udpiphdr *);
1864926Swnj 	ui->ui_next = ui->ui_prev = 0;
1874926Swnj 	ui->ui_x1 = 0;
1884926Swnj 	ui->ui_pr = IPPROTO_UDP;
1897844Sroot 	ui->ui_len = len + sizeof (struct udphdr);
1905050Swnj 	ui->ui_src = inp->inp_laddr;
1915050Swnj 	ui->ui_dst = inp->inp_faddr;
1925050Swnj 	ui->ui_sport = inp->inp_lport;
1935050Swnj 	ui->ui_dport = inp->inp_fport;
1947844Sroot 	ui->ui_ulen = htons((u_short)ui->ui_len);
1954784Swnj 
1964926Swnj 	/*
1974926Swnj 	 * Stuff checksum and output datagram.
1984926Swnj 	 */
1994926Swnj 	ui->ui_sum = 0;
2005094Swnj 	ui->ui_sum = in_cksum(m, sizeof (struct udpiphdr) + len);
2015050Swnj 	((struct ip *)ui)->ip_len = sizeof (struct udpiphdr) + len;
2025050Swnj 	((struct ip *)ui)->ip_ttl = MAXTTL;
2037157Swnj 	so = inp->inp_socket;
2047157Swnj 	return (ip_output(m, (struct mbuf *)0,
2057157Swnj 	    (so->so_options & SO_DONTROUTE) ? &routetoif : (struct route *)0,
2067157Swnj 	    so->so_state & SS_PRIV));
2074784Swnj }
2084784Swnj 
2098273Sroot udp_usrreq(so, req, m, nam, opt)
2104887Swnj 	struct socket *so;
2114784Swnj 	int req;
2128273Sroot 	struct mbuf *m, *nam;
2138273Sroot 	struct socketopt *opt;
2144784Swnj {
2154887Swnj 	struct inpcb *inp = sotoinpcb(so);
2166507Ssam 	int error = 0;
2174784Swnj 
2185051Swnj 	if (inp == 0 && req != PRU_ATTACH)
2195050Swnj 		return (EINVAL);
2204784Swnj 	switch (req) {
2214784Swnj 
2224784Swnj 	case PRU_ATTACH:
2234887Swnj 		if (inp != 0)
2244887Swnj 			return (EINVAL);
2258273Sroot 		error = in_pcballoc(so, &udb);
2268273Sroot 		if (error)
2278273Sroot 			break;
2288273Sroot 		error = in_pcbreserve(so, 2048, 2048);
2298273Sroot 		if (error)
2308273Sroot 			break;
2314887Swnj 		break;
2324784Swnj 
2334784Swnj 	case PRU_DETACH:
2344887Swnj 		if (inp == 0)
2354887Swnj 			return (ENOTCONN);
2365166Swnj 		in_pcbdetach(inp);
2374887Swnj 		break;
2384784Swnj 
2398273Sroot 	case PRU_BIND:
2408273Sroot 		error = in_pcbbind(inp, nam);
2418273Sroot 		break;
2428273Sroot 
2438273Sroot 	case PRU_LISTEN:
2448273Sroot 		error = EOPNOTSUPP;
2458273Sroot 		break;
2468273Sroot 
2474784Swnj 	case PRU_CONNECT:
2484955Swnj 		if (inp->inp_faddr.s_addr)
2494887Swnj 			return (EISCONN);
2508273Sroot 		error = in_pcbconnect(inp, nam);
2516507Ssam 		if (error == 0)
2526507Ssam 			soisconnected(so);
2534887Swnj 		break;
2544784Swnj 
2554926Swnj 	case PRU_ACCEPT:
2564926Swnj 		return (EOPNOTSUPP);
2574926Swnj 
2584784Swnj 	case PRU_DISCONNECT:
2594955Swnj 		if (inp->inp_faddr.s_addr == 0)
2604887Swnj 			return (ENOTCONN);
2615166Swnj 		in_pcbdisconnect(inp);
2624887Swnj 		soisdisconnected(so);
2634784Swnj 		break;
2644784Swnj 
2654912Swnj 	case PRU_SHUTDOWN:
2664912Swnj 		socantsendmore(so);
2674912Swnj 		break;
2684912Swnj 
2695996Swnj 	case PRU_SEND: {
2705996Swnj 		struct in_addr laddr;
2715996Swnj 
2728273Sroot 		if (nam) {
2735996Swnj 			laddr = inp->inp_laddr;
2745224Swnj 			if (inp->inp_faddr.s_addr)
2754887Swnj 				return (EISCONN);
2768273Sroot 			error = in_pcbconnect(inp, nam);
2775224Swnj 			if (error)
2786507Ssam 				break;
2794955Swnj 		} else {
2804955Swnj 			if (inp->inp_faddr.s_addr == 0)
2814955Swnj 				return (ENOTCONN);
2824955Swnj 		}
2836507Ssam 		error = udp_output(inp, m);
2848273Sroot 		if (nam) {
2855166Swnj 			in_pcbdisconnect(inp);
2865996Swnj 			inp->inp_laddr = laddr;
2875996Swnj 		}
2885996Swnj 		}
2894784Swnj 		break;
2904784Swnj 
2914784Swnj 	case PRU_ABORT:
2925166Swnj 		in_pcbdetach(inp);
2934887Swnj 		sofree(so);
2944887Swnj 		soisdisconnected(so);
2954784Swnj 		break;
2964784Swnj 
2974784Swnj 	case PRU_CONTROL:
2984887Swnj 		return (EOPNOTSUPP);
2994784Swnj 
3006511Ssam 	case PRU_SOCKADDR:
3018273Sroot 		in_setsockaddr(inp, nam);
3026511Ssam 		break;
3036511Ssam 
3044784Swnj 	default:
3054784Swnj 		panic("udp_usrreq");
3064805Swnj 	}
3076507Ssam 	return (error);
3084784Swnj }
309