1*11080Ssam /* udp_usrreq.c 4.45 83/02/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" 1010897Ssam #include "../h/errno.h" 1110897Ssam 126584Ssam #include "../net/if.h" 136354Ssam #include "../net/route.h" 1410897Ssam 1510897Ssam #include "../netinet/in.h" 168411Swnj #include "../netinet/in_pcb.h" 178411Swnj #include "../netinet/in_systm.h" 188411Swnj #include "../netinet/ip.h" 198411Swnj #include "../netinet/ip_var.h" 208411Swnj #include "../netinet/ip_icmp.h" 218411Swnj #include "../netinet/udp.h" 228411Swnj #include "../netinet/udp_var.h" 234784Swnj 244926Swnj /* 254926Swnj * UDP protocol implementation. 264926Swnj * Per RFC 768, August, 1980. 274926Swnj */ 284805Swnj udp_init() 294805Swnj { 304805Swnj 314901Swnj udb.inp_next = udb.inp_prev = &udb; 324805Swnj } 334805Swnj 344901Swnj int udpcksum; 354926Swnj struct sockaddr_in udp_in = { AF_INET }; 364901Swnj 374926Swnj udp_input(m0) 384926Swnj struct mbuf *m0; 394784Swnj { 404901Swnj register struct udpiphdr *ui; 414887Swnj register struct inpcb *inp; 424926Swnj register struct mbuf *m; 437844Sroot int len; 444784Swnj 454926Swnj /* 465246Sroot * Get IP and UDP header together in first mbuf. 474926Swnj */ 484926Swnj m = m0; 495308Sroot if ((m->m_off > MMAXOFF || m->m_len < sizeof (struct udpiphdr)) && 505308Sroot (m = m_pullup(m, sizeof (struct udpiphdr))) == 0) { 514926Swnj udpstat.udps_hdrops++; 525308Sroot return; 534926Swnj } 545050Swnj ui = mtod(m, struct udpiphdr *); 555246Sroot if (((struct ip *)ui)->ip_hl > (sizeof (struct ip) >> 2)) 565220Swnj ip_stripoptions((struct ip *)ui, (struct mbuf *)0); 574926Swnj 584926Swnj /* 595246Sroot * Make mbuf data length reflect UDP length. 605246Sroot * If not enough data to reflect UDP length, drop. 614926Swnj */ 627844Sroot len = ntohs((u_short)ui->ui_ulen); 634926Swnj if (((struct ip *)ui)->ip_len != len) { 644926Swnj if (len > ((struct ip *)ui)->ip_len) { 654926Swnj udpstat.udps_badlen++; 664926Swnj goto bad; 674926Swnj } 684926Swnj m_adj(m, ((struct ip *)ui)->ip_len - len); 694926Swnj /* (struct ip *)ui->ip_len = len; */ 704926Swnj } 714926Swnj 724926Swnj /* 735246Sroot * Checksum extended UDP header and data. 744926Swnj */ 754901Swnj if (udpcksum) { 764926Swnj ui->ui_next = ui->ui_prev = 0; 774926Swnj ui->ui_x1 = 0; 787844Sroot ui->ui_len = htons((u_short)len); 797844Sroot if (ui->ui_sum = in_cksum(m, len + sizeof (struct ip))) { 804926Swnj udpstat.udps_badsum++; 814901Swnj printf("udp cksum %x\n", ui->ui_sum); 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); 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; 1518641Sroot in_pcbnotify(&udb, sin, (int)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; 1709644Ssam m = m_get(M_DONTWAIT, MT_HEADER); 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; 1877844Sroot 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; 1927844Sroot 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 2078602Sroot /*ARGSUSED*/ 20810262Ssam udp_usrreq(so, req, m, nam) 2094887Swnj struct socket *so; 2104784Swnj int req; 2118273Sroot struct mbuf *m, *nam; 2124784Swnj { 2134887Swnj struct inpcb *inp = sotoinpcb(so); 2146507Ssam int error = 0; 2154784Swnj 216*11080Ssam if (inp == NULL && req != PRU_ATTACH) { 217*11080Ssam error = EINVAL; 218*11080Ssam goto release; 219*11080Ssam } 2204784Swnj switch (req) { 2214784Swnj 2224784Swnj case PRU_ATTACH: 223*11080Ssam if (inp != NULL) { 224*11080Ssam error = EINVAL; 225*11080Ssam break; 226*11080Ssam } 2278273Sroot error = in_pcballoc(so, &udb); 2288273Sroot if (error) 2298273Sroot break; 2309032Sroot error = soreserve(so, 2048, 2048); 2318273Sroot if (error) 2328273Sroot break; 2334887Swnj break; 2344784Swnj 2354784Swnj case PRU_DETACH: 236*11080Ssam if (inp == NULL) { 237*11080Ssam error = ENOTCONN; 238*11080Ssam break; 239*11080Ssam } 2405166Swnj in_pcbdetach(inp); 2414887Swnj break; 2424784Swnj 2438273Sroot case PRU_BIND: 2448273Sroot error = in_pcbbind(inp, nam); 2458273Sroot break; 2468273Sroot 2478273Sroot case PRU_LISTEN: 2488273Sroot error = EOPNOTSUPP; 2498273Sroot break; 2508273Sroot 2514784Swnj case PRU_CONNECT: 252*11080Ssam if (inp->inp_faddr.s_addr != INADDR_ANY) { 253*11080Ssam error = EISCONN; 254*11080Ssam break; 255*11080Ssam } 2568273Sroot error = in_pcbconnect(inp, nam); 2576507Ssam if (error == 0) 2586507Ssam soisconnected(so); 2594887Swnj break; 2604784Swnj 2614926Swnj case PRU_ACCEPT: 262*11080Ssam error = EOPNOTSUPP; 263*11080Ssam break; 2644926Swnj 2654784Swnj case PRU_DISCONNECT: 266*11080Ssam if (inp->inp_faddr.s_addr == INADDR_ANY) { 267*11080Ssam error = ENOTCONN; 268*11080Ssam break; 269*11080Ssam } 2705166Swnj in_pcbdisconnect(inp); 2714887Swnj soisdisconnected(so); 2724784Swnj break; 2734784Swnj 2744912Swnj case PRU_SHUTDOWN: 2754912Swnj socantsendmore(so); 2764912Swnj break; 2774912Swnj 2785996Swnj case PRU_SEND: { 2795996Swnj struct in_addr laddr; 2805996Swnj 2818273Sroot if (nam) { 2825996Swnj laddr = inp->inp_laddr; 283*11080Ssam if (inp->inp_faddr.s_addr != INADDR_ANY) { 284*11080Ssam error = EISCONN; 285*11080Ssam break; 286*11080Ssam } 2878273Sroot error = in_pcbconnect(inp, nam); 2885224Swnj if (error) 2896507Ssam break; 2904955Swnj } else { 291*11080Ssam if (inp->inp_faddr.s_addr == INADDR_ANY) { 292*11080Ssam error = ENOTCONN; 293*11080Ssam break; 294*11080Ssam } 2954955Swnj } 2966507Ssam error = udp_output(inp, m); 297*11080Ssam m = NULL; 2988273Sroot if (nam) { 2995166Swnj in_pcbdisconnect(inp); 3005996Swnj inp->inp_laddr = laddr; 3015996Swnj } 3025996Swnj } 3034784Swnj break; 3044784Swnj 3054784Swnj case PRU_ABORT: 3065166Swnj in_pcbdetach(inp); 3074887Swnj sofree(so); 3084887Swnj soisdisconnected(so); 3094784Swnj break; 3104784Swnj 3114784Swnj case PRU_CONTROL: 312*11080Ssam error = EOPNOTSUPP; 313*11080Ssam break; 3144784Swnj 3156511Ssam case PRU_SOCKADDR: 3168273Sroot in_setsockaddr(inp, nam); 3176511Ssam break; 3186511Ssam 3194784Swnj default: 3204784Swnj panic("udp_usrreq"); 3214805Swnj } 322*11080Ssam release: 323*11080Ssam if (m != NULL) 324*11080Ssam m_freem(m); 3256507Ssam return (error); 3264784Swnj } 327