bellard | f0cbd3e | 2004-04-22 00:10:48 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 1982, 1986, 1988, 1990, 1993 |
| 3 | * The Regents of the University of California. All rights reserved. |
| 4 | * |
| 5 | * Redistribution and use in source and binary forms, with or without |
| 6 | * modification, are permitted provided that the following conditions |
| 7 | * are met: |
| 8 | * 1. Redistributions of source code must retain the above copyright |
| 9 | * notice, this list of conditions and the following disclaimer. |
| 10 | * 2. Redistributions in binary form must reproduce the above copyright |
| 11 | * notice, this list of conditions and the following disclaimer in the |
| 12 | * documentation and/or other materials provided with the distribution. |
aliguori | 2f5f899 | 2009-01-26 19:37:41 +0000 | [diff] [blame] | 13 | * 3. Neither the name of the University nor the names of its contributors |
bellard | f0cbd3e | 2004-04-22 00:10:48 +0000 | [diff] [blame] | 14 | * may be used to endorse or promote products derived from this software |
| 15 | * without specific prior written permission. |
| 16 | * |
| 17 | * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND |
| 18 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 19 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
| 20 | * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE |
| 21 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
| 22 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS |
| 23 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
| 24 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT |
| 25 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY |
| 26 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF |
| 27 | * SUCH DAMAGE. |
| 28 | * |
| 29 | * @(#)udp_usrreq.c 8.4 (Berkeley) 1/21/94 |
| 30 | * udp_usrreq.c,v 1.4 1994/10/02 17:48:45 phk Exp |
| 31 | */ |
| 32 | |
| 33 | /* |
| 34 | * Changes and additions relating to SLiRP |
| 35 | * Copyright (c) 1995 Danny Gasparovski. |
ths | 5fafdf2 | 2007-09-16 21:08:06 +0000 | [diff] [blame] | 36 | * |
| 37 | * Please read the file COPYRIGHT for the |
bellard | f0cbd3e | 2004-04-22 00:10:48 +0000 | [diff] [blame] | 38 | * terms and conditions of the copyright. |
| 39 | */ |
| 40 | |
Peter Maydell | 7df7482 | 2016-01-29 17:49:59 +0000 | [diff] [blame] | 41 | #include "qemu/osdep.h" |
Markus Armbruster | a9c9427 | 2016-06-22 19:11:19 +0200 | [diff] [blame] | 42 | #include "slirp.h" |
bellard | f0cbd3e | 2004-04-22 00:10:48 +0000 | [diff] [blame] | 43 | #include "ip_icmp.h" |
| 44 | |
Stefan Weil | b6dce92 | 2010-07-22 22:15:23 +0200 | [diff] [blame] | 45 | static uint8_t udp_tos(struct socket *so); |
blueswir1 | 9634d90 | 2007-10-26 19:01:16 +0000 | [diff] [blame] | 46 | |
bellard | f0cbd3e | 2004-04-22 00:10:48 +0000 | [diff] [blame] | 47 | void |
Jan Kiszka | 460fec6 | 2009-06-24 14:42:31 +0200 | [diff] [blame] | 48 | udp_init(Slirp *slirp) |
bellard | f0cbd3e | 2004-04-22 00:10:48 +0000 | [diff] [blame] | 49 | { |
Jan Kiszka | 460fec6 | 2009-06-24 14:42:31 +0200 | [diff] [blame] | 50 | slirp->udb.so_next = slirp->udb.so_prev = &slirp->udb; |
| 51 | slirp->udp_last_so = &slirp->udb; |
bellard | f0cbd3e | 2004-04-22 00:10:48 +0000 | [diff] [blame] | 52 | } |
Jan Kiszka | a68adc2 | 2012-02-29 19:14:23 +0100 | [diff] [blame] | 53 | |
| 54 | void udp_cleanup(Slirp *slirp) |
| 55 | { |
| 56 | while (slirp->udb.so_next != &slirp->udb) { |
| 57 | udp_detach(slirp->udb.so_next); |
| 58 | } |
| 59 | } |
| 60 | |
ths | 5fafdf2 | 2007-09-16 21:08:06 +0000 | [diff] [blame] | 61 | /* m->m_data points at ip packet header |
| 62 | * m->m_len length ip packet |
bellard | f0cbd3e | 2004-04-22 00:10:48 +0000 | [diff] [blame] | 63 | * ip->ip_len length data (IPDU) |
| 64 | */ |
| 65 | void |
blueswir1 | aeed97c | 2009-04-18 07:32:41 +0000 | [diff] [blame] | 66 | udp_input(register struct mbuf *m, int iphlen) |
bellard | f0cbd3e | 2004-04-22 00:10:48 +0000 | [diff] [blame] | 67 | { |
Jan Kiszka | 460fec6 | 2009-06-24 14:42:31 +0200 | [diff] [blame] | 68 | Slirp *slirp = m->slirp; |
bellard | f0cbd3e | 2004-04-22 00:10:48 +0000 | [diff] [blame] | 69 | register struct ip *ip; |
| 70 | register struct udphdr *uh; |
bellard | f0cbd3e | 2004-04-22 00:10:48 +0000 | [diff] [blame] | 71 | int len; |
ths | 5fafdf2 | 2007-09-16 21:08:06 +0000 | [diff] [blame] | 72 | struct ip save_ip; |
bellard | f0cbd3e | 2004-04-22 00:10:48 +0000 | [diff] [blame] | 73 | struct socket *so; |
Guillaume Subiron | 8a87f12 | 2015-12-19 22:25:01 +0100 | [diff] [blame] | 74 | struct sockaddr_storage lhost; |
| 75 | struct sockaddr_in *lhost4; |
ths | 5fafdf2 | 2007-09-16 21:08:06 +0000 | [diff] [blame] | 76 | |
bellard | f0cbd3e | 2004-04-22 00:10:48 +0000 | [diff] [blame] | 77 | DEBUG_CALL("udp_input"); |
Stefan Weil | ecc804c | 2015-08-29 09:12:35 +0200 | [diff] [blame] | 78 | DEBUG_ARG("m = %p", m); |
bellard | f0cbd3e | 2004-04-22 00:10:48 +0000 | [diff] [blame] | 79 | DEBUG_ARG("iphlen = %d", iphlen); |
ths | 5fafdf2 | 2007-09-16 21:08:06 +0000 | [diff] [blame] | 80 | |
bellard | f0cbd3e | 2004-04-22 00:10:48 +0000 | [diff] [blame] | 81 | /* |
| 82 | * Strip IP options, if any; should skip this, |
| 83 | * make available to user, and use on returned packets, |
| 84 | * but we don't yet have a way to check the checksum |
| 85 | * with options still present. |
| 86 | */ |
| 87 | if(iphlen > sizeof(struct ip)) { |
| 88 | ip_stripoptions(m, (struct mbuf *)0); |
| 89 | iphlen = sizeof(struct ip); |
| 90 | } |
| 91 | |
| 92 | /* |
| 93 | * Get IP and UDP header together in first mbuf. |
| 94 | */ |
| 95 | ip = mtod(m, struct ip *); |
| 96 | uh = (struct udphdr *)((caddr_t)ip + iphlen); |
| 97 | |
| 98 | /* |
| 99 | * Make mbuf data length reflect UDP length. |
| 100 | * If not enough data to reflect UDP length, drop. |
| 101 | */ |
Stefan Weil | b6dce92 | 2010-07-22 22:15:23 +0200 | [diff] [blame] | 102 | len = ntohs((uint16_t)uh->uh_ulen); |
bellard | f0cbd3e | 2004-04-22 00:10:48 +0000 | [diff] [blame] | 103 | |
| 104 | if (ip->ip_len != len) { |
| 105 | if (len > ip->ip_len) { |
bellard | f0cbd3e | 2004-04-22 00:10:48 +0000 | [diff] [blame] | 106 | goto bad; |
| 107 | } |
| 108 | m_adj(m, len - ip->ip_len); |
| 109 | ip->ip_len = len; |
| 110 | } |
ths | 5fafdf2 | 2007-09-16 21:08:06 +0000 | [diff] [blame] | 111 | |
bellard | f0cbd3e | 2004-04-22 00:10:48 +0000 | [diff] [blame] | 112 | /* |
| 113 | * Save a copy of the IP header in case we want restore it |
| 114 | * for sending an ICMP error message in response. |
| 115 | */ |
ths | 5fafdf2 | 2007-09-16 21:08:06 +0000 | [diff] [blame] | 116 | save_ip = *ip; |
bellard | f0cbd3e | 2004-04-22 00:10:48 +0000 | [diff] [blame] | 117 | save_ip.ip_len+= iphlen; /* tcp_input subtracts this */ |
| 118 | |
| 119 | /* |
| 120 | * Checksum extended UDP header and data. |
| 121 | */ |
Jan Kiszka | 0d62c4c | 2009-06-24 14:42:29 +0200 | [diff] [blame] | 122 | if (uh->uh_sum) { |
blueswir1 | 429d0a3 | 2009-01-13 19:48:42 +0000 | [diff] [blame] | 123 | memset(&((struct ipovly *)ip)->ih_mbuf, 0, sizeof(struct mbuf_ptr)); |
bellard | f0cbd3e | 2004-04-22 00:10:48 +0000 | [diff] [blame] | 124 | ((struct ipovly *)ip)->ih_x1 = 0; |
| 125 | ((struct ipovly *)ip)->ih_len = uh->uh_ulen; |
bellard | f0cbd3e | 2004-04-22 00:10:48 +0000 | [diff] [blame] | 126 | if(cksum(m, len + sizeof(struct ip))) { |
bellard | f0cbd3e | 2004-04-22 00:10:48 +0000 | [diff] [blame] | 127 | goto bad; |
| 128 | } |
| 129 | } |
| 130 | |
Thomas Huth | fad7fb9 | 2016-03-15 10:31:23 +0100 | [diff] [blame] | 131 | lhost.ss_family = AF_INET; |
| 132 | lhost4 = (struct sockaddr_in *) &lhost; |
| 133 | lhost4->sin_addr = ip->ip_src; |
| 134 | lhost4->sin_port = uh->uh_sport; |
| 135 | |
bellard | f0cbd3e | 2004-04-22 00:10:48 +0000 | [diff] [blame] | 136 | /* |
| 137 | * handle DHCP/BOOTP |
| 138 | */ |
Jan Kiszka | 5a82362 | 2011-07-20 12:20:15 +0200 | [diff] [blame] | 139 | if (ntohs(uh->uh_dport) == BOOTP_SERVER && |
| 140 | (ip->ip_dst.s_addr == slirp->vhost_addr.s_addr || |
| 141 | ip->ip_dst.s_addr == 0xffffffff)) { |
| 142 | bootp_input(m); |
| 143 | goto bad; |
| 144 | } |
bellard | f0cbd3e | 2004-04-22 00:10:48 +0000 | [diff] [blame] | 145 | |
bellard | c7f7464 | 2004-08-24 21:57:12 +0000 | [diff] [blame] | 146 | /* |
| 147 | * handle TFTP |
| 148 | */ |
Jan Kiszka | 5a82362 | 2011-07-20 12:20:15 +0200 | [diff] [blame] | 149 | if (ntohs(uh->uh_dport) == TFTP_SERVER && |
| 150 | ip->ip_dst.s_addr == slirp->vhost_addr.s_addr) { |
Thomas Huth | fad7fb9 | 2016-03-15 10:31:23 +0100 | [diff] [blame] | 151 | m->m_data += iphlen; |
| 152 | m->m_len -= iphlen; |
| 153 | tftp_input(&lhost, m); |
| 154 | m->m_data -= iphlen; |
| 155 | m->m_len += iphlen; |
bellard | c7f7464 | 2004-08-24 21:57:12 +0000 | [diff] [blame] | 156 | goto bad; |
| 157 | } |
| 158 | |
Jan Kiszka | 12b513d | 2011-07-20 12:20:13 +0200 | [diff] [blame] | 159 | if (slirp->restricted) { |
| 160 | goto bad; |
| 161 | } |
| 162 | |
bellard | f0cbd3e | 2004-04-22 00:10:48 +0000 | [diff] [blame] | 163 | /* |
| 164 | * Locate pcb for datagram. |
| 165 | */ |
Guillaume Subiron | 8a87f12 | 2015-12-19 22:25:01 +0100 | [diff] [blame] | 166 | so = solookup(&slirp->udp_last_so, &slirp->udb, &lhost, NULL); |
ths | 5fafdf2 | 2007-09-16 21:08:06 +0000 | [diff] [blame] | 167 | |
bellard | f0cbd3e | 2004-04-22 00:10:48 +0000 | [diff] [blame] | 168 | if (so == NULL) { |
| 169 | /* |
| 170 | * If there's no socket for this packet, |
| 171 | * create one |
| 172 | */ |
Jan Kiszka | 460fec6 | 2009-06-24 14:42:31 +0200 | [diff] [blame] | 173 | so = socreate(slirp); |
| 174 | if (!so) { |
| 175 | goto bad; |
| 176 | } |
Guillaume Subiron | 9b5a30d | 2015-12-19 22:25:02 +0100 | [diff] [blame] | 177 | if (udp_attach(so, AF_INET) == -1) { |
ths | 5fafdf2 | 2007-09-16 21:08:06 +0000 | [diff] [blame] | 178 | DEBUG_MISC((dfd," udp_attach errno = %d-%s\n", |
bellard | f0cbd3e | 2004-04-22 00:10:48 +0000 | [diff] [blame] | 179 | errno,strerror(errno))); |
| 180 | sofree(so); |
| 181 | goto bad; |
| 182 | } |
ths | 3b46e62 | 2007-09-17 08:09:54 +0000 | [diff] [blame] | 183 | |
bellard | f0cbd3e | 2004-04-22 00:10:48 +0000 | [diff] [blame] | 184 | /* |
| 185 | * Setup fields |
| 186 | */ |
Guillaume Subiron | eae303f | 2015-12-19 22:24:58 +0100 | [diff] [blame] | 187 | so->so_lfamily = AF_INET; |
bellard | f0cbd3e | 2004-04-22 00:10:48 +0000 | [diff] [blame] | 188 | so->so_laddr = ip->ip_src; |
| 189 | so->so_lport = uh->uh_sport; |
ths | 3b46e62 | 2007-09-17 08:09:54 +0000 | [diff] [blame] | 190 | |
bellard | f0cbd3e | 2004-04-22 00:10:48 +0000 | [diff] [blame] | 191 | if ((so->so_iptos = udp_tos(so)) == 0) |
| 192 | so->so_iptos = ip->ip_tos; |
ths | 3b46e62 | 2007-09-17 08:09:54 +0000 | [diff] [blame] | 193 | |
bellard | f0cbd3e | 2004-04-22 00:10:48 +0000 | [diff] [blame] | 194 | /* |
| 195 | * XXXXX Here, check if it's in udpexec_list, |
| 196 | * and if it is, do the fork_exec() etc. |
| 197 | */ |
| 198 | } |
| 199 | |
Guillaume Subiron | eae303f | 2015-12-19 22:24:58 +0100 | [diff] [blame] | 200 | so->so_ffamily = AF_INET; |
ths | 54fd9cd | 2007-01-17 22:47:40 +0000 | [diff] [blame] | 201 | so->so_faddr = ip->ip_dst; /* XXX */ |
| 202 | so->so_fport = uh->uh_dport; /* XXX */ |
| 203 | |
bellard | f0cbd3e | 2004-04-22 00:10:48 +0000 | [diff] [blame] | 204 | iphlen += sizeof(struct udphdr); |
| 205 | m->m_len -= iphlen; |
| 206 | m->m_data += iphlen; |
| 207 | |
| 208 | /* |
| 209 | * Now we sendto() the packet. |
| 210 | */ |
bellard | f0cbd3e | 2004-04-22 00:10:48 +0000 | [diff] [blame] | 211 | if(sosendto(so,m) == -1) { |
| 212 | m->m_len += iphlen; |
| 213 | m->m_data -= iphlen; |
| 214 | *ip=save_ip; |
| 215 | DEBUG_MISC((dfd,"udp tx errno = %d-%s\n",errno,strerror(errno))); |
Yann Bordenave | de40abf | 2016-03-15 10:31:19 +0100 | [diff] [blame] | 216 | icmp_send_error(m, ICMP_UNREACH, ICMP_UNREACH_NET, 0, |
| 217 | strerror(errno)); |
Guillaume Subiron | 86c9e1e | 2015-12-19 22:24:55 +0100 | [diff] [blame] | 218 | goto bad; |
bellard | f0cbd3e | 2004-04-22 00:10:48 +0000 | [diff] [blame] | 219 | } |
| 220 | |
| 221 | m_free(so->so_m); /* used for ICMP if error on sorecvfrom */ |
| 222 | |
| 223 | /* restore the orig mbuf packet */ |
| 224 | m->m_len += iphlen; |
| 225 | m->m_data -= iphlen; |
| 226 | *ip=save_ip; |
| 227 | so->so_m=m; /* ICMP backup */ |
| 228 | |
| 229 | return; |
| 230 | bad: |
Jan Kiszka | 3acccfc | 2011-07-20 12:20:16 +0200 | [diff] [blame] | 231 | m_free(m); |
bellard | f0cbd3e | 2004-04-22 00:10:48 +0000 | [diff] [blame] | 232 | } |
| 233 | |
Guillaume Subiron | 5379229 | 2015-12-19 22:24:59 +0100 | [diff] [blame] | 234 | int udp_output(struct socket *so, struct mbuf *m, |
bellard | f0cbd3e | 2004-04-22 00:10:48 +0000 | [diff] [blame] | 235 | struct sockaddr_in *saddr, struct sockaddr_in *daddr, |
| 236 | int iptos) |
| 237 | { |
| 238 | register struct udpiphdr *ui; |
| 239 | int error = 0; |
| 240 | |
| 241 | DEBUG_CALL("udp_output"); |
Stefan Weil | ecc804c | 2015-08-29 09:12:35 +0200 | [diff] [blame] | 242 | DEBUG_ARG("so = %p", so); |
| 243 | DEBUG_ARG("m = %p", m); |
bellard | f0cbd3e | 2004-04-22 00:10:48 +0000 | [diff] [blame] | 244 | DEBUG_ARG("saddr = %lx", (long)saddr->sin_addr.s_addr); |
| 245 | DEBUG_ARG("daddr = %lx", (long)daddr->sin_addr.s_addr); |
| 246 | |
| 247 | /* |
| 248 | * Adjust for header |
| 249 | */ |
| 250 | m->m_data -= sizeof(struct udpiphdr); |
| 251 | m->m_len += sizeof(struct udpiphdr); |
ths | 5fafdf2 | 2007-09-16 21:08:06 +0000 | [diff] [blame] | 252 | |
bellard | f0cbd3e | 2004-04-22 00:10:48 +0000 | [diff] [blame] | 253 | /* |
| 254 | * Fill in mbuf with extended UDP header |
| 255 | * and addresses and length put into network format. |
| 256 | */ |
| 257 | ui = mtod(m, struct udpiphdr *); |
blueswir1 | 429d0a3 | 2009-01-13 19:48:42 +0000 | [diff] [blame] | 258 | memset(&ui->ui_i.ih_mbuf, 0 , sizeof(struct mbuf_ptr)); |
bellard | f0cbd3e | 2004-04-22 00:10:48 +0000 | [diff] [blame] | 259 | ui->ui_x1 = 0; |
| 260 | ui->ui_pr = IPPROTO_UDP; |
Jan Kiszka | 0d62c4c | 2009-06-24 14:42:29 +0200 | [diff] [blame] | 261 | ui->ui_len = htons(m->m_len - sizeof(struct ip)); |
bellard | f0cbd3e | 2004-04-22 00:10:48 +0000 | [diff] [blame] | 262 | /* XXXXX Check for from-one-location sockets, or from-any-location sockets */ |
| 263 | ui->ui_src = saddr->sin_addr; |
| 264 | ui->ui_dst = daddr->sin_addr; |
| 265 | ui->ui_sport = saddr->sin_port; |
| 266 | ui->ui_dport = daddr->sin_port; |
| 267 | ui->ui_ulen = ui->ui_len; |
| 268 | |
| 269 | /* |
| 270 | * Stuff checksum and output datagram. |
| 271 | */ |
| 272 | ui->ui_sum = 0; |
Jan Kiszka | 0d62c4c | 2009-06-24 14:42:29 +0200 | [diff] [blame] | 273 | if ((ui->ui_sum = cksum(m, m->m_len)) == 0) |
bellard | f0cbd3e | 2004-04-22 00:10:48 +0000 | [diff] [blame] | 274 | ui->ui_sum = 0xffff; |
bellard | f0cbd3e | 2004-04-22 00:10:48 +0000 | [diff] [blame] | 275 | ((struct ip *)ui)->ip_len = m->m_len; |
| 276 | |
blueswir1 | 9634d90 | 2007-10-26 19:01:16 +0000 | [diff] [blame] | 277 | ((struct ip *)ui)->ip_ttl = IPDEFTTL; |
bellard | f0cbd3e | 2004-04-22 00:10:48 +0000 | [diff] [blame] | 278 | ((struct ip *)ui)->ip_tos = iptos; |
ths | 5fafdf2 | 2007-09-16 21:08:06 +0000 | [diff] [blame] | 279 | |
bellard | f0cbd3e | 2004-04-22 00:10:48 +0000 | [diff] [blame] | 280 | error = ip_output(so, m); |
ths | 5fafdf2 | 2007-09-16 21:08:06 +0000 | [diff] [blame] | 281 | |
bellard | f0cbd3e | 2004-04-22 00:10:48 +0000 | [diff] [blame] | 282 | return (error); |
| 283 | } |
| 284 | |
bellard | f0cbd3e | 2004-04-22 00:10:48 +0000 | [diff] [blame] | 285 | int |
Guillaume Subiron | 9b5a30d | 2015-12-19 22:25:02 +0100 | [diff] [blame] | 286 | udp_attach(struct socket *so, unsigned short af) |
bellard | f0cbd3e | 2004-04-22 00:10:48 +0000 | [diff] [blame] | 287 | { |
Guillaume Subiron | 9b5a30d | 2015-12-19 22:25:02 +0100 | [diff] [blame] | 288 | so->s = qemu_socket(af, SOCK_DGRAM, 0); |
| 289 | if (so->s != -1) { |
Ed Swierk | 97df1ee | 2009-07-22 17:53:15 -0700 | [diff] [blame] | 290 | so->so_expire = curtime + SO_EXPIRE; |
| 291 | insque(so, &so->slirp->udb); |
bellard | f0cbd3e | 2004-04-22 00:10:48 +0000 | [diff] [blame] | 292 | } |
| 293 | return(so->s); |
| 294 | } |
| 295 | |
| 296 | void |
blueswir1 | aeed97c | 2009-04-18 07:32:41 +0000 | [diff] [blame] | 297 | udp_detach(struct socket *so) |
bellard | f0cbd3e | 2004-04-22 00:10:48 +0000 | [diff] [blame] | 298 | { |
bellard | 379ff53 | 2004-07-12 22:33:07 +0000 | [diff] [blame] | 299 | closesocket(so->s); |
bellard | f0cbd3e | 2004-04-22 00:10:48 +0000 | [diff] [blame] | 300 | sofree(so); |
| 301 | } |
| 302 | |
blueswir1 | 9634d90 | 2007-10-26 19:01:16 +0000 | [diff] [blame] | 303 | static const struct tos_t udptos[] = { |
bellard | f0cbd3e | 2004-04-22 00:10:48 +0000 | [diff] [blame] | 304 | {0, 53, IPTOS_LOWDELAY, 0}, /* DNS */ |
bellard | f0cbd3e | 2004-04-22 00:10:48 +0000 | [diff] [blame] | 305 | {0, 0, 0, 0} |
| 306 | }; |
| 307 | |
Stefan Weil | b6dce92 | 2010-07-22 22:15:23 +0200 | [diff] [blame] | 308 | static uint8_t |
blueswir1 | 9634d90 | 2007-10-26 19:01:16 +0000 | [diff] [blame] | 309 | udp_tos(struct socket *so) |
bellard | f0cbd3e | 2004-04-22 00:10:48 +0000 | [diff] [blame] | 310 | { |
| 311 | int i = 0; |
ths | 5fafdf2 | 2007-09-16 21:08:06 +0000 | [diff] [blame] | 312 | |
bellard | f0cbd3e | 2004-04-22 00:10:48 +0000 | [diff] [blame] | 313 | while(udptos[i].tos) { |
| 314 | if ((udptos[i].fport && ntohs(so->so_fport) == udptos[i].fport) || |
| 315 | (udptos[i].lport && ntohs(so->so_lport) == udptos[i].lport)) { |
| 316 | so->so_emu = udptos[i].emu; |
| 317 | return udptos[i].tos; |
| 318 | } |
| 319 | i++; |
| 320 | } |
ths | 5fafdf2 | 2007-09-16 21:08:06 +0000 | [diff] [blame] | 321 | |
bellard | f0cbd3e | 2004-04-22 00:10:48 +0000 | [diff] [blame] | 322 | return 0; |
| 323 | } |
| 324 | |
bellard | f0cbd3e | 2004-04-22 00:10:48 +0000 | [diff] [blame] | 325 | struct socket * |
Stefan Weil | b6dce92 | 2010-07-22 22:15:23 +0200 | [diff] [blame] | 326 | udp_listen(Slirp *slirp, uint32_t haddr, u_int hport, uint32_t laddr, |
Jan Kiszka | 460fec6 | 2009-06-24 14:42:31 +0200 | [diff] [blame] | 327 | u_int lport, int flags) |
bellard | f0cbd3e | 2004-04-22 00:10:48 +0000 | [diff] [blame] | 328 | { |
| 329 | struct sockaddr_in addr; |
| 330 | struct socket *so; |
Sebastian Ottlik | aad1239 | 2013-10-02 12:23:15 +0200 | [diff] [blame] | 331 | socklen_t addrlen = sizeof(struct sockaddr_in); |
ths | 5fafdf2 | 2007-09-16 21:08:06 +0000 | [diff] [blame] | 332 | |
Jan Kiszka | 460fec6 | 2009-06-24 14:42:31 +0200 | [diff] [blame] | 333 | so = socreate(slirp); |
| 334 | if (!so) { |
| 335 | return NULL; |
bellard | f0cbd3e | 2004-04-22 00:10:48 +0000 | [diff] [blame] | 336 | } |
Kevin Wolf | 40ff6d7 | 2009-12-02 12:24:42 +0100 | [diff] [blame] | 337 | so->s = qemu_socket(AF_INET,SOCK_DGRAM,0); |
Peter Maydell | 4577b09 | 2017-02-04 23:08:33 +0000 | [diff] [blame] | 338 | if (so->s < 0) { |
| 339 | sofree(so); |
| 340 | return NULL; |
| 341 | } |
bellard | f0cbd3e | 2004-04-22 00:10:48 +0000 | [diff] [blame] | 342 | so->so_expire = curtime + SO_EXPIRE; |
Jan Kiszka | 460fec6 | 2009-06-24 14:42:31 +0200 | [diff] [blame] | 343 | insque(so, &slirp->udb); |
bellard | f0cbd3e | 2004-04-22 00:10:48 +0000 | [diff] [blame] | 344 | |
| 345 | addr.sin_family = AF_INET; |
Jan Kiszka | 3c6a058 | 2009-06-24 14:42:28 +0200 | [diff] [blame] | 346 | addr.sin_addr.s_addr = haddr; |
| 347 | addr.sin_port = hport; |
bellard | f0cbd3e | 2004-04-22 00:10:48 +0000 | [diff] [blame] | 348 | |
| 349 | if (bind(so->s,(struct sockaddr *)&addr, addrlen) < 0) { |
| 350 | udp_detach(so); |
| 351 | return NULL; |
| 352 | } |
Sebastian Ottlik | aad1239 | 2013-10-02 12:23:15 +0200 | [diff] [blame] | 353 | socket_set_fast_reuse(so->s); |
ths | 5fafdf2 | 2007-09-16 21:08:06 +0000 | [diff] [blame] | 354 | |
bellard | f0cbd3e | 2004-04-22 00:10:48 +0000 | [diff] [blame] | 355 | getsockname(so->s,(struct sockaddr *)&addr,&addrlen); |
Guillaume Subiron | 5379229 | 2015-12-19 22:24:59 +0100 | [diff] [blame] | 356 | so->fhost.sin = addr; |
| 357 | sotranslate_accept(so); |
Guillaume Subiron | eae303f | 2015-12-19 22:24:58 +0100 | [diff] [blame] | 358 | so->so_lfamily = AF_INET; |
bellard | f0cbd3e | 2004-04-22 00:10:48 +0000 | [diff] [blame] | 359 | so->so_lport = lport; |
| 360 | so->so_laddr.s_addr = laddr; |
| 361 | if (flags != SS_FACCEPTONCE) |
| 362 | so->so_expire = 0; |
ths | 5fafdf2 | 2007-09-16 21:08:06 +0000 | [diff] [blame] | 363 | |
Jan Kiszka | f932b6c | 2009-06-24 14:42:29 +0200 | [diff] [blame] | 364 | so->so_state &= SS_PERSISTENT_MASK; |
Jan Kiszka | 6dd5ffb | 2009-06-24 14:42:29 +0200 | [diff] [blame] | 365 | so->so_state |= SS_ISFCONNECTED | flags; |
ths | 5fafdf2 | 2007-09-16 21:08:06 +0000 | [diff] [blame] | 366 | |
bellard | f0cbd3e | 2004-04-22 00:10:48 +0000 | [diff] [blame] | 367 | return so; |
| 368 | } |