1*18368Skarels /* udp_usrreq.c 6.10 85/03/18 */ 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; 16217165Skarels register struct socket *so; 16317165Skarels 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; 20417165Skarels so = inp->inp_socket; 20517165Skarels if (so->so_options & SO_DONTROUTE) 20617165Skarels return (ip_output(m, (struct mbuf *)0, (struct route *)0, 20717165Skarels (so->so_options & SO_BROADCAST) | IP_ROUTETOIF)); 20817165Skarels /* 20917165Skarels * Use cached route for previous datagram if 21017165Skarels * this is also to the same destination. 21117165Skarels * 21217165Skarels * NB: We don't handle broadcasts because that 21317165Skarels * would require 3 subroutine calls. 21417165Skarels */ 21517165Skarels ro = &inp->inp_route; 21616601Ssam #define satosin(sa) ((struct sockaddr_in *)(sa)) 21717165Skarels if (ro->ro_rt && 21817165Skarels satosin(&ro->ro_dst)->sin_addr.s_addr != ui->ui_dst.s_addr) { 21917165Skarels RTFREE(ro->ro_rt); 22017165Skarels ro->ro_rt = (struct rtentry *)0; 22117165Skarels } 22217165Skarels return (ip_output(m, (struct mbuf *)0, ro, 22317165Skarels so->so_options & SO_BROADCAST)); 2244784Swnj } 2254784Swnj 226*18368Skarels int udp_sendspace = 2048; /* really max datagram size */ 227*18368Skarels int udp_recvspace = 4 * (1024+sizeof(struct sockaddr_in)); /* 4 1K dgrams */ 228*18368Skarels 2298602Sroot /*ARGSUSED*/ 23012767Ssam udp_usrreq(so, req, m, nam, rights) 2314887Swnj struct socket *so; 2324784Swnj int req; 23312767Ssam struct mbuf *m, *nam, *rights; 2344784Swnj { 2354887Swnj struct inpcb *inp = sotoinpcb(so); 2366507Ssam int error = 0; 2374784Swnj 238*18368Skarels if (req == PRU_CONTROL) 239*18368Skarels return (in_control(so, (int)m, (caddr_t)nam, 240*18368Skarels (struct ifnet *)rights)); 24112767Ssam if (rights && rights->m_len) { 24212767Ssam error = EINVAL; 24312767Ssam goto release; 24412767Ssam } 24511080Ssam if (inp == NULL && req != PRU_ATTACH) { 24611080Ssam error = EINVAL; 24711080Ssam goto release; 24811080Ssam } 2494784Swnj switch (req) { 2504784Swnj 2514784Swnj case PRU_ATTACH: 25211080Ssam if (inp != NULL) { 25311080Ssam error = EINVAL; 25411080Ssam break; 25511080Ssam } 2568273Sroot error = in_pcballoc(so, &udb); 2578273Sroot if (error) 2588273Sroot break; 259*18368Skarels error = soreserve(so, udp_sendspace, udp_recvspace); 2608273Sroot if (error) 2618273Sroot break; 2624887Swnj break; 2634784Swnj 2644784Swnj case PRU_DETACH: 26511080Ssam if (inp == NULL) { 26611080Ssam error = ENOTCONN; 26711080Ssam break; 26811080Ssam } 2695166Swnj in_pcbdetach(inp); 2704887Swnj break; 2714784Swnj 2728273Sroot case PRU_BIND: 2738273Sroot error = in_pcbbind(inp, nam); 2748273Sroot break; 2758273Sroot 2768273Sroot case PRU_LISTEN: 2778273Sroot error = EOPNOTSUPP; 2788273Sroot break; 2798273Sroot 2804784Swnj case PRU_CONNECT: 28111080Ssam if (inp->inp_faddr.s_addr != INADDR_ANY) { 28211080Ssam error = EISCONN; 28311080Ssam break; 28411080Ssam } 2858273Sroot error = in_pcbconnect(inp, nam); 2866507Ssam if (error == 0) 2876507Ssam soisconnected(so); 2884887Swnj break; 2894784Swnj 29013118Ssam case PRU_CONNECT2: 29113118Ssam error = EOPNOTSUPP; 29213118Ssam break; 29313118Ssam 2944926Swnj case PRU_ACCEPT: 29511080Ssam error = EOPNOTSUPP; 29611080Ssam break; 2974926Swnj 2984784Swnj case PRU_DISCONNECT: 29911080Ssam if (inp->inp_faddr.s_addr == INADDR_ANY) { 30011080Ssam error = ENOTCONN; 30111080Ssam break; 30211080Ssam } 3035166Swnj in_pcbdisconnect(inp); 3044887Swnj soisdisconnected(so); 3054784Swnj break; 3064784Swnj 3074912Swnj case PRU_SHUTDOWN: 3084912Swnj socantsendmore(so); 3094912Swnj break; 3104912Swnj 3115996Swnj case PRU_SEND: { 3125996Swnj struct in_addr laddr; 31316799Skarels int s; 3145996Swnj 3158273Sroot if (nam) { 3165996Swnj laddr = inp->inp_laddr; 31711080Ssam if (inp->inp_faddr.s_addr != INADDR_ANY) { 31811080Ssam error = EISCONN; 31911080Ssam break; 32011080Ssam } 32116799Skarels /* 32216799Skarels * Must block input while temporarily connected. 32316799Skarels */ 32416799Skarels s = splnet(); 3258273Sroot error = in_pcbconnect(inp, nam); 32616799Skarels if (error) { 32716799Skarels splx(s); 3286507Ssam break; 32916799Skarels } 3304955Swnj } else { 33111080Ssam if (inp->inp_faddr.s_addr == INADDR_ANY) { 33211080Ssam error = ENOTCONN; 33311080Ssam break; 33411080Ssam } 3354955Swnj } 3366507Ssam error = udp_output(inp, m); 33711080Ssam m = NULL; 3388273Sroot if (nam) { 3395166Swnj in_pcbdisconnect(inp); 340*18368Skarels inp->inp_laddr = laddr; 34116799Skarels splx(s); 3425996Swnj } 3435996Swnj } 3444784Swnj break; 3454784Swnj 3464784Swnj case PRU_ABORT: 3475166Swnj in_pcbdetach(inp); 3484887Swnj sofree(so); 3494887Swnj soisdisconnected(so); 3504784Swnj break; 3514784Swnj 3526511Ssam case PRU_SOCKADDR: 3538273Sroot in_setsockaddr(inp, nam); 3546511Ssam break; 3556511Ssam 35614124Ssam case PRU_PEERADDR: 35714124Ssam in_setpeeraddr(inp, nam); 35814124Ssam break; 35914124Ssam 36016988Skarels case PRU_SENSE: 36116988Skarels /* 36216988Skarels * stat: don't bother with a blocksize. 36316988Skarels */ 36416988Skarels return (0); 36516988Skarels 36612205Ssam case PRU_SENDOOB: 36712205Ssam case PRU_FASTTIMO: 36812205Ssam case PRU_SLOWTIMO: 36912205Ssam case PRU_PROTORCV: 37012205Ssam case PRU_PROTOSEND: 37112205Ssam error = EOPNOTSUPP; 37212205Ssam break; 37313118Ssam 37416799Skarels case PRU_RCVD: 37516799Skarels case PRU_RCVOOB: 37616799Skarels return (EOPNOTSUPP); /* do not free mbuf's */ 37716799Skarels 37813118Ssam default: 37913118Ssam panic("udp_usrreq"); 3804805Swnj } 38111080Ssam release: 38211080Ssam if (m != NULL) 38311080Ssam m_freem(m); 3846507Ssam return (error); 3854784Swnj } 386