| 1 | /* udp_usrreq.c 6.11 85/04/29 */ |
| 2 | |
| 3 | #include "param.h" |
| 4 | #include "dir.h" |
| 5 | #include "user.h" |
| 6 | #include "mbuf.h" |
| 7 | #include "protosw.h" |
| 8 | #include "socket.h" |
| 9 | #include "socketvar.h" |
| 10 | #include "errno.h" |
| 11 | #include "stat.h" |
| 12 | |
| 13 | #include "../net/if.h" |
| 14 | #include "../net/route.h" |
| 15 | |
| 16 | #include "in.h" |
| 17 | #include "in_pcb.h" |
| 18 | #include "in_systm.h" |
| 19 | #include "ip.h" |
| 20 | #include "ip_var.h" |
| 21 | #include "ip_icmp.h" |
| 22 | #include "udp.h" |
| 23 | #include "udp_var.h" |
| 24 | |
| 25 | /* |
| 26 | * UDP protocol implementation. |
| 27 | * Per RFC 768, August, 1980. |
| 28 | */ |
| 29 | udp_init() |
| 30 | { |
| 31 | |
| 32 | udb.inp_next = udb.inp_prev = &udb; |
| 33 | } |
| 34 | |
| 35 | int udpcksum = 0; |
| 36 | struct sockaddr_in udp_in = { AF_INET }; |
| 37 | |
| 38 | udp_input(m0) |
| 39 | struct mbuf *m0; |
| 40 | { |
| 41 | register struct udpiphdr *ui; |
| 42 | register struct inpcb *inp; |
| 43 | register struct mbuf *m; |
| 44 | int len; |
| 45 | |
| 46 | /* |
| 47 | * Get IP and UDP header together in first mbuf. |
| 48 | */ |
| 49 | m = m0; |
| 50 | if ((m->m_off > MMAXOFF || m->m_len < sizeof (struct udpiphdr)) && |
| 51 | (m = m_pullup(m, sizeof (struct udpiphdr))) == 0) { |
| 52 | udpstat.udps_hdrops++; |
| 53 | return; |
| 54 | } |
| 55 | ui = mtod(m, struct udpiphdr *); |
| 56 | if (((struct ip *)ui)->ip_hl > (sizeof (struct ip) >> 2)) |
| 57 | ip_stripoptions((struct ip *)ui, (struct mbuf *)0); |
| 58 | |
| 59 | /* |
| 60 | * Make mbuf data length reflect UDP length. |
| 61 | * If not enough data to reflect UDP length, drop. |
| 62 | */ |
| 63 | len = ntohs((u_short)ui->ui_ulen); |
| 64 | if (((struct ip *)ui)->ip_len != len) { |
| 65 | if (len > ((struct ip *)ui)->ip_len) { |
| 66 | udpstat.udps_badlen++; |
| 67 | goto bad; |
| 68 | } |
| 69 | m_adj(m, len - ((struct ip *)ui)->ip_len); |
| 70 | /* (struct ip *)ui->ip_len = len; */ |
| 71 | } |
| 72 | |
| 73 | /* |
| 74 | * Checksum extended UDP header and data. |
| 75 | */ |
| 76 | if (udpcksum && ui->ui_sum) { |
| 77 | ui->ui_next = ui->ui_prev = 0; |
| 78 | ui->ui_x1 = 0; |
| 79 | ui->ui_len = htons((u_short)len); |
| 80 | if (ui->ui_sum = in_cksum(m, len + sizeof (struct ip))) { |
| 81 | udpstat.udps_badsum++; |
| 82 | m_freem(m); |
| 83 | return; |
| 84 | } |
| 85 | } |
| 86 | |
| 87 | /* |
| 88 | * Locate pcb for datagram. |
| 89 | */ |
| 90 | inp = in_pcblookup(&udb, |
| 91 | ui->ui_src, ui->ui_sport, ui->ui_dst, ui->ui_dport, |
| 92 | INPLOOKUP_WILDCARD); |
| 93 | if (inp == 0) { |
| 94 | /* don't send ICMP response for broadcast packet */ |
| 95 | if (in_lnaof(ui->ui_dst) == INADDR_ANY) |
| 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, |
| 110 | m, (struct mbuf *)0) == 0) |
| 111 | goto bad; |
| 112 | sorwakeup(inp->inp_socket); |
| 113 | return; |
| 114 | bad: |
| 115 | m_freem(m); |
| 116 | } |
| 117 | |
| 118 | udp_abort(inp) |
| 119 | struct inpcb *inp; |
| 120 | { |
| 121 | struct socket *so = inp->inp_socket; |
| 122 | |
| 123 | in_pcbdisconnect(inp); |
| 124 | soisdisconnected(so); |
| 125 | } |
| 126 | |
| 127 | udp_ctlinput(cmd, arg) |
| 128 | int cmd; |
| 129 | caddr_t arg; |
| 130 | { |
| 131 | struct in_addr *sin; |
| 132 | extern u_char inetctlerrmap[]; |
| 133 | |
| 134 | if (cmd < 0 || cmd > PRC_NCMDS) |
| 135 | return; |
| 136 | switch (cmd) { |
| 137 | |
| 138 | case PRC_ROUTEDEAD: |
| 139 | break; |
| 140 | |
| 141 | case PRC_QUENCH: |
| 142 | break; |
| 143 | |
| 144 | /* these are handled by ip */ |
| 145 | case PRC_IFDOWN: |
| 146 | case PRC_HOSTDEAD: |
| 147 | case PRC_HOSTUNREACH: |
| 148 | break; |
| 149 | |
| 150 | default: |
| 151 | sin = &((struct icmp *)arg)->icmp_ip.ip_dst; |
| 152 | in_pcbnotify(&udb, sin, (int)inetctlerrmap[cmd], udp_abort); |
| 153 | } |
| 154 | } |
| 155 | |
| 156 | udp_output(inp, m0) |
| 157 | struct inpcb *inp; |
| 158 | struct mbuf *m0; |
| 159 | { |
| 160 | register struct mbuf *m; |
| 161 | register struct udpiphdr *ui; |
| 162 | register struct socket *so; |
| 163 | register int len = 0; |
| 164 | register struct route *ro; |
| 165 | |
| 166 | /* |
| 167 | * Calculate data length and get a mbuf |
| 168 | * for UDP and IP headers. |
| 169 | */ |
| 170 | for (m = m0; m; m = m->m_next) |
| 171 | len += m->m_len; |
| 172 | m = m_get(M_DONTWAIT, MT_HEADER); |
| 173 | if (m == 0) { |
| 174 | m_freem(m0); |
| 175 | return (ENOBUFS); |
| 176 | } |
| 177 | |
| 178 | /* |
| 179 | * Fill in mbuf with extended UDP header |
| 180 | * and addresses and length put into network format. |
| 181 | */ |
| 182 | m->m_off = MMAXOFF - sizeof (struct udpiphdr); |
| 183 | m->m_len = sizeof (struct udpiphdr); |
| 184 | m->m_next = m0; |
| 185 | ui = mtod(m, struct udpiphdr *); |
| 186 | ui->ui_next = ui->ui_prev = 0; |
| 187 | ui->ui_x1 = 0; |
| 188 | ui->ui_pr = IPPROTO_UDP; |
| 189 | ui->ui_len = htons((u_short)len + sizeof (struct udphdr)); |
| 190 | ui->ui_src = inp->inp_laddr; |
| 191 | ui->ui_dst = inp->inp_faddr; |
| 192 | ui->ui_sport = inp->inp_lport; |
| 193 | ui->ui_dport = inp->inp_fport; |
| 194 | ui->ui_ulen = ui->ui_len; |
| 195 | |
| 196 | /* |
| 197 | * Stuff checksum and output datagram. |
| 198 | */ |
| 199 | ui->ui_sum = 0; |
| 200 | if ((ui->ui_sum = in_cksum(m, sizeof (struct udpiphdr) + len)) == 0) |
| 201 | ui->ui_sum = -1; |
| 202 | ((struct ip *)ui)->ip_len = sizeof (struct udpiphdr) + len; |
| 203 | ((struct ip *)ui)->ip_ttl = MAXTTL; |
| 204 | so = inp->inp_socket; |
| 205 | if (so->so_options & SO_DONTROUTE) |
| 206 | return (ip_output(m, (struct mbuf *)0, (struct route *)0, |
| 207 | (so->so_options & SO_BROADCAST) | IP_ROUTETOIF)); |
| 208 | /* |
| 209 | * Use cached route for previous datagram if |
| 210 | * this is also to the same destination. |
| 211 | * |
| 212 | * NB: We don't handle broadcasts because that |
| 213 | * would require 3 subroutine calls. |
| 214 | */ |
| 215 | ro = &inp->inp_route; |
| 216 | #define satosin(sa) ((struct sockaddr_in *)(sa)) |
| 217 | if (ro->ro_rt && |
| 218 | satosin(&ro->ro_dst)->sin_addr.s_addr != ui->ui_dst.s_addr) { |
| 219 | RTFREE(ro->ro_rt); |
| 220 | ro->ro_rt = (struct rtentry *)0; |
| 221 | } |
| 222 | return (ip_output(m, (struct mbuf *)0, ro, |
| 223 | so->so_options & SO_BROADCAST)); |
| 224 | } |
| 225 | |
| 226 | int udp_sendspace = 2048; /* really max datagram size */ |
| 227 | int udp_recvspace = 4 * (1024+sizeof(struct sockaddr_in)); /* 4 1K dgrams */ |
| 228 | |
| 229 | /*ARGSUSED*/ |
| 230 | udp_usrreq(so, req, m, nam, rights) |
| 231 | struct socket *so; |
| 232 | int req; |
| 233 | struct mbuf *m, *nam, *rights; |
| 234 | { |
| 235 | struct inpcb *inp = sotoinpcb(so); |
| 236 | int error = 0; |
| 237 | |
| 238 | if (req == PRU_CONTROL) |
| 239 | return (in_control(so, (int)m, (caddr_t)nam, |
| 240 | (struct ifnet *)rights)); |
| 241 | if (rights && rights->m_len) { |
| 242 | error = EINVAL; |
| 243 | goto release; |
| 244 | } |
| 245 | if (inp == NULL && req != PRU_ATTACH) { |
| 246 | error = EINVAL; |
| 247 | goto release; |
| 248 | } |
| 249 | switch (req) { |
| 250 | |
| 251 | case PRU_ATTACH: |
| 252 | if (inp != NULL) { |
| 253 | error = EINVAL; |
| 254 | break; |
| 255 | } |
| 256 | error = in_pcballoc(so, &udb); |
| 257 | if (error) |
| 258 | break; |
| 259 | error = soreserve(so, udp_sendspace, udp_recvspace); |
| 260 | if (error) |
| 261 | break; |
| 262 | break; |
| 263 | |
| 264 | case PRU_DETACH: |
| 265 | if (inp == NULL) { |
| 266 | error = ENOTCONN; |
| 267 | break; |
| 268 | } |
| 269 | in_pcbdetach(inp); |
| 270 | break; |
| 271 | |
| 272 | case PRU_BIND: |
| 273 | error = in_pcbbind(inp, nam); |
| 274 | break; |
| 275 | |
| 276 | case PRU_LISTEN: |
| 277 | error = EOPNOTSUPP; |
| 278 | break; |
| 279 | |
| 280 | case PRU_CONNECT: |
| 281 | if (inp->inp_faddr.s_addr != INADDR_ANY) { |
| 282 | error = EISCONN; |
| 283 | break; |
| 284 | } |
| 285 | error = in_pcbconnect(inp, nam); |
| 286 | if (error == 0) |
| 287 | soisconnected(so); |
| 288 | break; |
| 289 | |
| 290 | case PRU_CONNECT2: |
| 291 | error = EOPNOTSUPP; |
| 292 | break; |
| 293 | |
| 294 | case PRU_ACCEPT: |
| 295 | error = EOPNOTSUPP; |
| 296 | break; |
| 297 | |
| 298 | case PRU_DISCONNECT: |
| 299 | if (inp->inp_faddr.s_addr == INADDR_ANY) { |
| 300 | error = ENOTCONN; |
| 301 | break; |
| 302 | } |
| 303 | in_pcbdisconnect(inp); |
| 304 | soisdisconnected(so); |
| 305 | break; |
| 306 | |
| 307 | case PRU_SHUTDOWN: |
| 308 | socantsendmore(so); |
| 309 | break; |
| 310 | |
| 311 | case PRU_SEND: { |
| 312 | struct in_addr laddr; |
| 313 | int s; |
| 314 | |
| 315 | if (nam) { |
| 316 | laddr = inp->inp_laddr; |
| 317 | if (inp->inp_faddr.s_addr != INADDR_ANY) { |
| 318 | error = EISCONN; |
| 319 | break; |
| 320 | } |
| 321 | /* |
| 322 | * Must block input while temporarily connected. |
| 323 | */ |
| 324 | s = splnet(); |
| 325 | error = in_pcbconnect(inp, nam); |
| 326 | if (error) { |
| 327 | splx(s); |
| 328 | break; |
| 329 | } |
| 330 | } else { |
| 331 | if (inp->inp_faddr.s_addr == INADDR_ANY) { |
| 332 | error = ENOTCONN; |
| 333 | break; |
| 334 | } |
| 335 | } |
| 336 | error = udp_output(inp, m); |
| 337 | m = NULL; |
| 338 | if (nam) { |
| 339 | in_pcbdisconnect(inp); |
| 340 | inp->inp_laddr = laddr; |
| 341 | splx(s); |
| 342 | } |
| 343 | } |
| 344 | break; |
| 345 | |
| 346 | case PRU_ABORT: |
| 347 | in_pcbdetach(inp); |
| 348 | sofree(so); |
| 349 | soisdisconnected(so); |
| 350 | break; |
| 351 | |
| 352 | case PRU_SOCKADDR: |
| 353 | in_setsockaddr(inp, nam); |
| 354 | break; |
| 355 | |
| 356 | case PRU_PEERADDR: |
| 357 | in_setpeeraddr(inp, nam); |
| 358 | break; |
| 359 | |
| 360 | case PRU_SENSE: |
| 361 | /* |
| 362 | * stat: don't bother with a blocksize. |
| 363 | */ |
| 364 | return (0); |
| 365 | |
| 366 | case PRU_SENDOOB: |
| 367 | case PRU_FASTTIMO: |
| 368 | case PRU_SLOWTIMO: |
| 369 | case PRU_PROTORCV: |
| 370 | case PRU_PROTOSEND: |
| 371 | error = EOPNOTSUPP; |
| 372 | break; |
| 373 | |
| 374 | case PRU_RCVD: |
| 375 | case PRU_RCVOOB: |
| 376 | return (EOPNOTSUPP); /* do not free mbuf's */ |
| 377 | |
| 378 | default: |
| 379 | panic("udp_usrreq"); |
| 380 | } |
| 381 | release: |
| 382 | if (m != NULL) |
| 383 | m_freem(m); |
| 384 | return (error); |
| 385 | } |