1*17165Skarels /* udp_usrreq.c 6.9 84/09/05 */ 24784Swnj 317065Sbloom #include "param.h" 417065Sbloom #include "dir.h" 517065Sbloom #include "user.h" 617065Sbloom #include "mbuf.h" 717065Sbloom #include "protosw.h" 817065Sbloom #include "socket.h" 917065Sbloom #include "socketvar.h" 1017065Sbloom #include "errno.h" 1117065Sbloom #include "stat.h" 1210897Ssam 136584Ssam #include "../net/if.h" 146354Ssam #include "../net/route.h" 1510897Ssam 1617065Sbloom #include "in.h" 1717065Sbloom #include "in_pcb.h" 1817065Sbloom #include "in_systm.h" 1917065Sbloom #include "ip.h" 2017065Sbloom #include "ip_var.h" 2117065Sbloom #include "ip_icmp.h" 2217065Sbloom #include "udp.h" 2317065Sbloom #include "udp_var.h" 244784Swnj 254926Swnj /* 264926Swnj * UDP protocol implementation. 274926Swnj * Per RFC 768, August, 1980. 284926Swnj */ 294805Swnj udp_init() 304805Swnj { 314805Swnj 324901Swnj udb.inp_next = udb.inp_prev = &udb; 334805Swnj } 344805Swnj 3515539Skarels int udpcksum = 0; 364926Swnj struct sockaddr_in udp_in = { AF_INET }; 374901Swnj 384926Swnj udp_input(m0) 394926Swnj struct mbuf *m0; 404784Swnj { 414901Swnj register struct udpiphdr *ui; 424887Swnj register struct inpcb *inp; 434926Swnj register struct mbuf *m; 447844Sroot int len; 454784Swnj 464926Swnj /* 475246Sroot * Get IP and UDP header together in first mbuf. 484926Swnj */ 494926Swnj m = m0; 505308Sroot if ((m->m_off > MMAXOFF || m->m_len < sizeof (struct udpiphdr)) && 515308Sroot (m = m_pullup(m, sizeof (struct udpiphdr))) == 0) { 524926Swnj udpstat.udps_hdrops++; 535308Sroot return; 544926Swnj } 555050Swnj ui = mtod(m, struct udpiphdr *); 565246Sroot if (((struct ip *)ui)->ip_hl > (sizeof (struct ip) >> 2)) 575220Swnj ip_stripoptions((struct ip *)ui, (struct mbuf *)0); 584926Swnj 594926Swnj /* 605246Sroot * Make mbuf data length reflect UDP length. 615246Sroot * If not enough data to reflect UDP length, drop. 624926Swnj */ 637844Sroot len = ntohs((u_short)ui->ui_ulen); 644926Swnj if (((struct ip *)ui)->ip_len != len) { 654926Swnj if (len > ((struct ip *)ui)->ip_len) { 664926Swnj udpstat.udps_badlen++; 674926Swnj goto bad; 684926Swnj } 6915539Skarels m_adj(m, len - ((struct ip *)ui)->ip_len); 704926Swnj /* (struct ip *)ui->ip_len = len; */ 714926Swnj } 724926Swnj 734926Swnj /* 745246Sroot * Checksum extended UDP header and data. 754926Swnj */ 7615539Skarels if (udpcksum && ui->ui_sum) { 774926Swnj ui->ui_next = ui->ui_prev = 0; 784926Swnj ui->ui_x1 = 0; 797844Sroot ui->ui_len = htons((u_short)len); 807844Sroot if (ui->ui_sum = in_cksum(m, len + sizeof (struct ip))) { 814926Swnj udpstat.udps_badsum++; 824901Swnj m_freem(m); 834901Swnj return; 844901Swnj } 854901Swnj } 864926Swnj 874926Swnj /* 887844Sroot * Locate pcb for datagram. 894926Swnj */ 904926Swnj inp = in_pcblookup(&udb, 916029Sroot ui->ui_src, ui->ui_sport, ui->ui_dst, ui->ui_dport, 926029Sroot INPLOOKUP_WILDCARD); 936584Ssam if (inp == 0) { 9410144Ssam /* don't send ICMP response for broadcast packet */ 9510144Ssam if (in_lnaof(ui->ui_dst) == INADDR_ANY) 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); 10912767Ssam if (sbappendaddr(&inp->inp_socket->so_rcv, (struct sockaddr *)&udp_in, 11012767Ssam m, (struct mbuf *)0) == 0) 1114926Swnj goto bad; 1125050Swnj sorwakeup(inp->inp_socket); 1134887Swnj return; 1144926Swnj bad: 1154887Swnj m_freem(m); 1164784Swnj } 1174784Swnj 1186584Ssam udp_abort(inp) 1196584Ssam struct inpcb *inp; 1206584Ssam { 1216584Ssam struct socket *so = inp->inp_socket; 1226584Ssam 1236584Ssam in_pcbdisconnect(inp); 1246584Ssam soisdisconnected(so); 1256584Ssam } 1266584Ssam 1276591Ssam udp_ctlinput(cmd, arg) 1286591Ssam int cmd; 1296591Ssam caddr_t arg; 1306591Ssam { 1316591Ssam struct in_addr *sin; 1326591Ssam extern u_char inetctlerrmap[]; 1336591Ssam 1346591Ssam if (cmd < 0 || cmd > PRC_NCMDS) 1356591Ssam return; 1366591Ssam switch (cmd) { 1376591Ssam 1386591Ssam case PRC_ROUTEDEAD: 1396591Ssam break; 1406591Ssam 1416591Ssam case PRC_QUENCH: 1426591Ssam break; 1436591Ssam 1446591Ssam /* these are handled by ip */ 1456591Ssam case PRC_IFDOWN: 1466591Ssam case PRC_HOSTDEAD: 1476591Ssam case PRC_HOSTUNREACH: 1486591Ssam break; 1496591Ssam 1506591Ssam default: 1516591Ssam sin = &((struct icmp *)arg)->icmp_ip.ip_dst; 1528641Sroot in_pcbnotify(&udb, sin, (int)inetctlerrmap[cmd], udp_abort); 1536591Ssam } 1546591Ssam } 1556591Ssam 1564955Swnj udp_output(inp, m0) 1574926Swnj struct inpcb *inp; 1584926Swnj struct mbuf *m0; 1594784Swnj { 1604926Swnj register struct mbuf *m; 1614926Swnj register struct udpiphdr *ui; 162*17165Skarels register struct socket *so; 163*17165Skarels register int len = 0; 16416601Ssam register struct route *ro; 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; 1729644Ssam m = m_get(M_DONTWAIT, MT_HEADER); 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; 18915226Ssam ui->ui_len = htons((u_short)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; 19415226Ssam ui->ui_ulen = ui->ui_len; 1954784Swnj 1964926Swnj /* 1974926Swnj * Stuff checksum and output datagram. 1984926Swnj */ 1994926Swnj ui->ui_sum = 0; 20015539Skarels if ((ui->ui_sum = in_cksum(m, sizeof (struct udpiphdr) + len)) == 0) 20115539Skarels ui->ui_sum = -1; 2025050Swnj ((struct ip *)ui)->ip_len = sizeof (struct udpiphdr) + len; 2035050Swnj ((struct ip *)ui)->ip_ttl = MAXTTL; 204*17165Skarels so = inp->inp_socket; 205*17165Skarels if (so->so_options & SO_DONTROUTE) 206*17165Skarels return (ip_output(m, (struct mbuf *)0, (struct route *)0, 207*17165Skarels (so->so_options & SO_BROADCAST) | IP_ROUTETOIF)); 208*17165Skarels /* 209*17165Skarels * Use cached route for previous datagram if 210*17165Skarels * this is also to the same destination. 211*17165Skarels * 212*17165Skarels * NB: We don't handle broadcasts because that 213*17165Skarels * would require 3 subroutine calls. 214*17165Skarels */ 215*17165Skarels ro = &inp->inp_route; 21616601Ssam #define satosin(sa) ((struct sockaddr_in *)(sa)) 217*17165Skarels if (ro->ro_rt && 218*17165Skarels satosin(&ro->ro_dst)->sin_addr.s_addr != ui->ui_dst.s_addr) { 219*17165Skarels RTFREE(ro->ro_rt); 220*17165Skarels ro->ro_rt = (struct rtentry *)0; 221*17165Skarels } 222*17165Skarels return (ip_output(m, (struct mbuf *)0, ro, 223*17165Skarels so->so_options & SO_BROADCAST)); 2244784Swnj } 2254784Swnj 2268602Sroot /*ARGSUSED*/ 22712767Ssam udp_usrreq(so, req, m, nam, rights) 2284887Swnj struct socket *so; 2294784Swnj int req; 23012767Ssam struct mbuf *m, *nam, *rights; 2314784Swnj { 2324887Swnj struct inpcb *inp = sotoinpcb(so); 2336507Ssam int error = 0; 2344784Swnj 23512767Ssam if (rights && rights->m_len) { 23612767Ssam error = EINVAL; 23712767Ssam goto release; 23812767Ssam } 23911080Ssam if (inp == NULL && req != PRU_ATTACH) { 24011080Ssam error = EINVAL; 24111080Ssam goto release; 24211080Ssam } 2434784Swnj switch (req) { 2444784Swnj 2454784Swnj case PRU_ATTACH: 24611080Ssam if (inp != NULL) { 24711080Ssam error = EINVAL; 24811080Ssam break; 24911080Ssam } 2508273Sroot error = in_pcballoc(so, &udb); 2518273Sroot if (error) 2528273Sroot break; 2539032Sroot error = soreserve(so, 2048, 2048); 2548273Sroot if (error) 2558273Sroot break; 2564887Swnj break; 2574784Swnj 2584784Swnj case PRU_DETACH: 25911080Ssam if (inp == NULL) { 26011080Ssam error = ENOTCONN; 26111080Ssam break; 26211080Ssam } 2635166Swnj in_pcbdetach(inp); 2644887Swnj break; 2654784Swnj 2668273Sroot case PRU_BIND: 2678273Sroot error = in_pcbbind(inp, nam); 2688273Sroot break; 2698273Sroot 2708273Sroot case PRU_LISTEN: 2718273Sroot error = EOPNOTSUPP; 2728273Sroot break; 2738273Sroot 2744784Swnj case PRU_CONNECT: 27511080Ssam if (inp->inp_faddr.s_addr != INADDR_ANY) { 27611080Ssam error = EISCONN; 27711080Ssam break; 27811080Ssam } 2798273Sroot error = in_pcbconnect(inp, nam); 2806507Ssam if (error == 0) 2816507Ssam soisconnected(so); 2824887Swnj break; 2834784Swnj 28413118Ssam case PRU_CONNECT2: 28513118Ssam error = EOPNOTSUPP; 28613118Ssam break; 28713118Ssam 2884926Swnj case PRU_ACCEPT: 28911080Ssam error = EOPNOTSUPP; 29011080Ssam break; 2914926Swnj 2924784Swnj case PRU_DISCONNECT: 29311080Ssam if (inp->inp_faddr.s_addr == INADDR_ANY) { 29411080Ssam error = ENOTCONN; 29511080Ssam break; 29611080Ssam } 2975166Swnj in_pcbdisconnect(inp); 2984887Swnj soisdisconnected(so); 2994784Swnj break; 3004784Swnj 3014912Swnj case PRU_SHUTDOWN: 3024912Swnj socantsendmore(so); 3034912Swnj break; 3044912Swnj 3055996Swnj case PRU_SEND: { 3065996Swnj struct in_addr laddr; 30716799Skarels int s; 3085996Swnj 3098273Sroot if (nam) { 3105996Swnj laddr = inp->inp_laddr; 31111080Ssam if (inp->inp_faddr.s_addr != INADDR_ANY) { 31211080Ssam error = EISCONN; 31311080Ssam break; 31411080Ssam } 31516799Skarels /* 31616799Skarels * Must block input while temporarily connected. 31716799Skarels */ 31816799Skarels s = splnet(); 3198273Sroot error = in_pcbconnect(inp, nam); 32016799Skarels if (error) { 32116799Skarels splx(s); 3226507Ssam break; 32316799Skarels } 3244955Swnj } else { 32511080Ssam if (inp->inp_faddr.s_addr == INADDR_ANY) { 32611080Ssam error = ENOTCONN; 32711080Ssam break; 32811080Ssam } 3294955Swnj } 3306507Ssam error = udp_output(inp, m); 33111080Ssam m = NULL; 3328273Sroot if (nam) { 3335166Swnj in_pcbdisconnect(inp); 33416799Skarels splx(s); 3355996Swnj inp->inp_laddr = laddr; 3365996Swnj } 3375996Swnj } 3384784Swnj break; 3394784Swnj 3404784Swnj case PRU_ABORT: 3415166Swnj in_pcbdetach(inp); 3424887Swnj sofree(so); 3434887Swnj soisdisconnected(so); 3444784Swnj break; 3454784Swnj 3466511Ssam case PRU_SOCKADDR: 3478273Sroot in_setsockaddr(inp, nam); 3486511Ssam break; 3496511Ssam 35014124Ssam case PRU_PEERADDR: 35114124Ssam in_setpeeraddr(inp, nam); 35214124Ssam break; 35314124Ssam 35416988Skarels case PRU_SENSE: 35516988Skarels /* 35616988Skarels * stat: don't bother with a blocksize. 35716988Skarels */ 35816988Skarels return (0); 35916988Skarels 36012205Ssam case PRU_SENDOOB: 36112205Ssam case PRU_FASTTIMO: 36212205Ssam case PRU_SLOWTIMO: 36312205Ssam case PRU_PROTORCV: 36412205Ssam case PRU_PROTOSEND: 36512205Ssam error = EOPNOTSUPP; 36612205Ssam break; 36713118Ssam 36816799Skarels case PRU_CONTROL: 36916799Skarels case PRU_RCVD: 37016799Skarels case PRU_RCVOOB: 37116799Skarels return (EOPNOTSUPP); /* do not free mbuf's */ 37216799Skarels 37313118Ssam default: 37413118Ssam panic("udp_usrreq"); 3754805Swnj } 37611080Ssam release: 37711080Ssam if (m != NULL) 37811080Ssam m_freem(m); 3796507Ssam return (error); 3804784Swnj } 381