1 /* udp_usrreq.c 4.21 82/03/03 */ 2 3 #include "../h/param.h" 4 #include "../h/dir.h" 5 #include "../h/user.h" 6 #include "../h/mbuf.h" 7 #include "../h/protosw.h" 8 #include "../h/socket.h" 9 #include "../h/socketvar.h" 10 #include "../net/in.h" 11 #include "../net/in_pcb.h" 12 #include "../net/in_systm.h" 13 #include "../net/ip.h" 14 #include "../net/ip_var.h" 15 #include "../net/udp.h" 16 #include "../net/udp_var.h" 17 18 /* 19 * UDP protocol implementation. 20 * Per RFC 768, August, 1980. 21 */ 22 udp_init() 23 { 24 25 COUNT(UDP_INIT); 26 udb.inp_next = udb.inp_prev = &udb; 27 } 28 29 int udpcksum; 30 struct sockaddr_in udp_in = { AF_INET }; 31 32 udp_input(m0) 33 struct mbuf *m0; 34 { 35 register struct udpiphdr *ui; 36 register struct inpcb *inp; 37 register struct mbuf *m; 38 int len, ulen; 39 40 COUNT(UDP_INPUT); 41 /* 42 * Get IP and UDP header together in first mbuf. 43 */ 44 m = m0; 45 if ((m->m_off > MMAXOFF || m->m_len < sizeof (struct udpiphdr)) && 46 (m = m_pullup(m, sizeof (struct udpiphdr))) == 0) { 47 udpstat.udps_hdrops++; 48 return; 49 } 50 ui = mtod(m, struct udpiphdr *); 51 if (((struct ip *)ui)->ip_hl > (sizeof (struct ip) >> 2)) 52 ip_stripoptions((struct ip *)ui, (struct mbuf *)0); 53 54 /* 55 * Make mbuf data length reflect UDP length. 56 * If not enough data to reflect UDP length, drop. 57 */ 58 ulen = ntohs((u_short)ui->ui_ulen); 59 len = sizeof (struct udphdr) + ulen; 60 if (((struct ip *)ui)->ip_len != len) { 61 if (len > ((struct ip *)ui)->ip_len) { 62 udpstat.udps_badlen++; 63 goto bad; 64 } 65 m_adj(m, ((struct ip *)ui)->ip_len - len); 66 /* (struct ip *)ui->ip_len = len; */ 67 } 68 69 /* 70 * Checksum extended UDP header and data. 71 */ 72 if (udpcksum) { 73 ui->ui_next = ui->ui_prev = 0; 74 ui->ui_x1 = 0; 75 ui->ui_len = htons((u_short)(sizeof (struct udphdr) + ulen)); 76 if (ui->ui_sum = in_cksum(m, len)) { 77 udpstat.udps_badsum++; 78 printf("udp cksum %x\n", ui->ui_sum); 79 m_freem(m); 80 return; 81 } 82 } 83 84 /* 85 * Locate pcb for datagram. On wildcard match, update 86 * control block to anchor network and host address. 87 */ 88 inp = in_pcblookup(&udb, 89 ui->ui_src, ui->ui_sport, ui->ui_dst, ui->ui_dport, 90 INPLOOKUP_WILDCARD); 91 if (inp == 0) 92 goto bad; 93 94 /* 95 * Construct sockaddr format source address. 96 * Stuff source address and datagram in user buffer. 97 */ 98 udp_in.sin_port = ui->ui_sport; 99 udp_in.sin_addr = ui->ui_src; 100 m->m_len -= sizeof (struct udpiphdr); 101 m->m_off += sizeof (struct udpiphdr); 102 if (sbappendaddr(&inp->inp_socket->so_rcv, (struct sockaddr *)&udp_in, m) == 0) 103 goto bad; 104 sorwakeup(inp->inp_socket); 105 return; 106 bad: 107 m_freem(m); 108 } 109 110 udp_ctlinput(m) 111 struct mbuf *m; 112 { 113 114 COUNT(UDP_CTLINPUT); 115 m_freem(m); 116 } 117 118 udp_output(inp, m0) 119 struct inpcb *inp; 120 struct mbuf *m0; 121 { 122 register struct mbuf *m; 123 register struct udpiphdr *ui; 124 register int len = 0; 125 126 COUNT(UDP_OUTPUT); 127 /* 128 * Calculate data length and get a mbuf 129 * for UDP and IP headers. 130 */ 131 for (m = m0; m; m = m->m_next) 132 len += m->m_len; 133 m = m_get(M_DONTWAIT); 134 if (m == 0) 135 goto bad; 136 137 /* 138 * Fill in mbuf with extended UDP header 139 * and addresses and length put into network format. 140 */ 141 m->m_off = MMAXOFF - sizeof (struct udpiphdr); 142 m->m_len = sizeof (struct udpiphdr); 143 m->m_next = m0; 144 ui = mtod(m, struct udpiphdr *); 145 ui->ui_next = ui->ui_prev = 0; 146 ui->ui_x1 = 0; 147 ui->ui_pr = IPPROTO_UDP; 148 ui->ui_len = sizeof (struct udpiphdr) + len; 149 ui->ui_src = inp->inp_laddr; 150 ui->ui_dst = inp->inp_faddr; 151 ui->ui_sport = inp->inp_lport; 152 ui->ui_dport = inp->inp_fport; 153 ui->ui_ulen = htons((u_short)len); 154 155 /* 156 * Stuff checksum and output datagram. 157 */ 158 ui->ui_sum = 0; 159 ui->ui_sum = in_cksum(m, sizeof (struct udpiphdr) + len); 160 ((struct ip *)ui)->ip_len = sizeof (struct udpiphdr) + len; 161 ((struct ip *)ui)->ip_ttl = MAXTTL; 162 (void) ip_output(m, (struct mbuf *)0); 163 return; 164 bad: 165 m_freem(m); 166 } 167 168 udp_usrreq(so, req, m, addr) 169 struct socket *so; 170 int req; 171 struct mbuf *m; 172 caddr_t addr; 173 { 174 struct inpcb *inp = sotoinpcb(so); 175 int error; 176 177 COUNT(UDP_USRREQ); 178 if (inp == 0 && req != PRU_ATTACH) 179 return (EINVAL); 180 switch (req) { 181 182 case PRU_ATTACH: 183 if (inp != 0) 184 return (EINVAL); 185 error = in_pcbattach(so, &udb, 2048, 2048, (struct sockaddr_in *)addr); 186 if (error) 187 return (error); 188 break; 189 190 case PRU_DETACH: 191 if (inp == 0) 192 return (ENOTCONN); 193 in_pcbdetach(inp); 194 break; 195 196 case PRU_CONNECT: 197 if (inp->inp_faddr.s_addr) 198 return (EISCONN); 199 error = in_pcbconnect(inp, (struct sockaddr_in *)addr); 200 if (error) 201 return (error); 202 soisconnected(so); 203 break; 204 205 case PRU_ACCEPT: 206 return (EOPNOTSUPP); 207 208 case PRU_DISCONNECT: 209 if (inp->inp_faddr.s_addr == 0) 210 return (ENOTCONN); 211 in_pcbdisconnect(inp); 212 soisdisconnected(so); 213 break; 214 215 case PRU_SHUTDOWN: 216 socantsendmore(so); 217 break; 218 219 case PRU_SEND: { 220 struct in_addr laddr; 221 222 if (addr) { 223 laddr = inp->inp_laddr; 224 if (inp->inp_faddr.s_addr) 225 return (EISCONN); 226 error = in_pcbconnect(inp, (struct sockaddr_in *)addr); 227 if (error) 228 return (error); 229 } else { 230 if (inp->inp_faddr.s_addr == 0) 231 return (ENOTCONN); 232 } 233 udp_output(inp, m); 234 if (addr) { 235 in_pcbdisconnect(inp); 236 inp->inp_laddr = laddr; 237 in_setsockaddr(inp); 238 } 239 } 240 break; 241 242 case PRU_ABORT: 243 in_pcbdetach(inp); 244 sofree(so); 245 soisdisconnected(so); 246 break; 247 248 case PRU_CONTROL: 249 return (EOPNOTSUPP); 250 251 default: 252 panic("udp_usrreq"); 253 } 254 return (0); 255 } 256