1 /* udp_usrreq.c 4.31 82/08/15 */ 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/if.h" 12 #include "../net/route.h" 13 #include "../net/in_pcb.h" 14 #include "../net/in_systm.h" 15 #include "../net/ip.h" 16 #include "../net/ip_var.h" 17 #include "../net/ip_icmp.h" 18 #include "../net/udp.h" 19 #include "../net/udp_var.h" 20 #include <errno.h> 21 22 /* 23 * UDP protocol implementation. 24 * Per RFC 768, August, 1980. 25 */ 26 udp_init() 27 { 28 29 udb.inp_next = udb.inp_prev = &udb; 30 } 31 32 int udpcksum; 33 struct sockaddr_in udp_in = { AF_INET }; 34 35 udp_input(m0) 36 struct mbuf *m0; 37 { 38 register struct udpiphdr *ui; 39 register struct inpcb *inp; 40 register struct mbuf *m; 41 int len; 42 43 /* 44 * Get IP and UDP header together in first mbuf. 45 */ 46 m = m0; 47 if ((m->m_off > MMAXOFF || m->m_len < sizeof (struct udpiphdr)) && 48 (m = m_pullup(m, sizeof (struct udpiphdr))) == 0) { 49 udpstat.udps_hdrops++; 50 return; 51 } 52 ui = mtod(m, struct udpiphdr *); 53 if (((struct ip *)ui)->ip_hl > (sizeof (struct ip) >> 2)) 54 ip_stripoptions((struct ip *)ui, (struct mbuf *)0); 55 56 /* 57 * Make mbuf data length reflect UDP length. 58 * If not enough data to reflect UDP length, drop. 59 */ 60 len = ntohs((u_short)ui->ui_ulen); 61 if (((struct ip *)ui)->ip_len != len) { 62 if (len > ((struct ip *)ui)->ip_len) { 63 udpstat.udps_badlen++; 64 goto bad; 65 } 66 m_adj(m, ((struct ip *)ui)->ip_len - len); 67 /* (struct ip *)ui->ip_len = len; */ 68 } 69 70 /* 71 * Checksum extended UDP header and data. 72 */ 73 if (udpcksum) { 74 ui->ui_next = ui->ui_prev = 0; 75 ui->ui_x1 = 0; 76 ui->ui_len = htons((u_short)len); 77 if (ui->ui_sum = in_cksum(m, len + sizeof (struct ip))) { 78 udpstat.udps_badsum++; 79 printf("udp cksum %x\n", ui->ui_sum); 80 m_freem(m); 81 return; 82 } 83 } 84 85 /* 86 * Locate pcb for datagram. 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 struct in_addr broadcastaddr; 93 94 broadcastaddr = if_makeaddr(ui->ui_dst.s_net, INADDR_ANY); 95 if (ui->ui_dst.s_addr == broadcastaddr.s_addr) 96 goto bad; 97 icmp_error((struct ip *)ui, ICMP_UNREACH, ICMP_UNREACH_PORT); 98 return; 99 } 100 101 /* 102 * Construct sockaddr format source address. 103 * Stuff source address and datagram in user buffer. 104 */ 105 udp_in.sin_port = ui->ui_sport; 106 udp_in.sin_addr = ui->ui_src; 107 m->m_len -= sizeof (struct udpiphdr); 108 m->m_off += sizeof (struct udpiphdr); 109 if (sbappendaddr(&inp->inp_socket->so_rcv, (struct sockaddr *)&udp_in, m) == 0) 110 goto bad; 111 sorwakeup(inp->inp_socket); 112 return; 113 bad: 114 m_freem(m); 115 } 116 117 udp_abort(inp) 118 struct inpcb *inp; 119 { 120 struct socket *so = inp->inp_socket; 121 122 in_pcbdisconnect(inp); 123 soisdisconnected(so); 124 } 125 126 udp_ctlinput(cmd, arg) 127 int cmd; 128 caddr_t arg; 129 { 130 struct in_addr *sin; 131 extern u_char inetctlerrmap[]; 132 133 if (cmd < 0 || cmd > PRC_NCMDS) 134 return; 135 switch (cmd) { 136 137 case PRC_ROUTEDEAD: 138 break; 139 140 case PRC_QUENCH: 141 break; 142 143 /* these are handled by ip */ 144 case PRC_IFDOWN: 145 case PRC_HOSTDEAD: 146 case PRC_HOSTUNREACH: 147 break; 148 149 default: 150 sin = &((struct icmp *)arg)->icmp_ip.ip_dst; 151 in_pcbnotify(&udb, sin, inetctlerrmap[cmd], udp_abort); 152 } 153 } 154 155 udp_output(inp, m0) 156 struct inpcb *inp; 157 struct mbuf *m0; 158 { 159 register struct mbuf *m; 160 register struct udpiphdr *ui; 161 register struct socket *so; 162 register int len = 0; 163 164 /* 165 * Calculate data length and get a mbuf 166 * for UDP and IP headers. 167 */ 168 for (m = m0; m; m = m->m_next) 169 len += m->m_len; 170 m = m_get(M_DONTWAIT); 171 if (m == 0) { 172 m_freem(m0); 173 return (ENOBUFS); 174 } 175 176 /* 177 * Fill in mbuf with extended UDP header 178 * and addresses and length put into network format. 179 */ 180 m->m_off = MMAXOFF - sizeof (struct udpiphdr); 181 m->m_len = sizeof (struct udpiphdr); 182 m->m_next = m0; 183 ui = mtod(m, struct udpiphdr *); 184 ui->ui_next = ui->ui_prev = 0; 185 ui->ui_x1 = 0; 186 ui->ui_pr = IPPROTO_UDP; 187 ui->ui_len = len + sizeof (struct udphdr); 188 ui->ui_src = inp->inp_laddr; 189 ui->ui_dst = inp->inp_faddr; 190 ui->ui_sport = inp->inp_lport; 191 ui->ui_dport = inp->inp_fport; 192 ui->ui_ulen = htons((u_short)ui->ui_len); 193 194 /* 195 * Stuff checksum and output datagram. 196 */ 197 ui->ui_sum = 0; 198 ui->ui_sum = in_cksum(m, sizeof (struct udpiphdr) + len); 199 ((struct ip *)ui)->ip_len = sizeof (struct udpiphdr) + len; 200 ((struct ip *)ui)->ip_ttl = MAXTTL; 201 so = inp->inp_socket; 202 return (ip_output(m, (struct mbuf *)0, 203 (so->so_options & SO_DONTROUTE) ? &routetoif : (struct route *)0, 204 so->so_state & SS_PRIV)); 205 } 206 207 udp_usrreq(so, req, m, addr) 208 struct socket *so; 209 int req; 210 struct mbuf *m; 211 caddr_t addr; 212 { 213 struct inpcb *inp = sotoinpcb(so); 214 int error = 0; 215 216 if (inp == 0 && req != PRU_ATTACH) 217 return (EINVAL); 218 switch (req) { 219 220 case PRU_ATTACH: 221 if (inp != 0) 222 return (EINVAL); 223 error = in_pcbattach(so, &udb, 2048, 2048, 224 (struct sockaddr_in *)addr); 225 break; 226 227 case PRU_DETACH: 228 if (inp == 0) 229 return (ENOTCONN); 230 in_pcbdetach(inp); 231 break; 232 233 case PRU_CONNECT: 234 if (inp->inp_faddr.s_addr) 235 return (EISCONN); 236 error = in_pcbconnect(inp, (struct sockaddr_in *)addr); 237 if (error == 0) 238 soisconnected(so); 239 break; 240 241 case PRU_ACCEPT: 242 return (EOPNOTSUPP); 243 244 case PRU_DISCONNECT: 245 if (inp->inp_faddr.s_addr == 0) 246 return (ENOTCONN); 247 in_pcbdisconnect(inp); 248 soisdisconnected(so); 249 break; 250 251 case PRU_SHUTDOWN: 252 socantsendmore(so); 253 break; 254 255 case PRU_SEND: { 256 struct in_addr laddr; 257 258 if (addr) { 259 laddr = inp->inp_laddr; 260 if (inp->inp_faddr.s_addr) 261 return (EISCONN); 262 error = in_pcbconnect(inp, (struct sockaddr_in *)addr); 263 if (error) 264 break; 265 } else { 266 if (inp->inp_faddr.s_addr == 0) 267 return (ENOTCONN); 268 } 269 error = udp_output(inp, m); 270 if (addr) { 271 in_pcbdisconnect(inp); 272 inp->inp_laddr = laddr; 273 } 274 } 275 break; 276 277 case PRU_ABORT: 278 in_pcbdetach(inp); 279 sofree(so); 280 soisdisconnected(so); 281 break; 282 283 case PRU_CONTROL: 284 return (EOPNOTSUPP); 285 286 case PRU_SOCKADDR: 287 in_setsockaddr((struct sockaddr_in *)addr, inp); 288 break; 289 290 default: 291 panic("udp_usrreq"); 292 } 293 return (error); 294 } 295