1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42#include "slirp.h"
43#include "ip_icmp.h"
44
45static uint8_t udp_tos(struct socket *so);
46
47void
48udp_init(Slirp *slirp)
49{
50 slirp->udb.so_next = slirp->udb.so_prev = &slirp->udb;
51 slirp->udp_last_so = &slirp->udb;
52}
53
54void udp_cleanup(Slirp *slirp)
55{
56 while (slirp->udb.so_next != &slirp->udb) {
57 udp_detach(slirp->udb.so_next);
58 }
59}
60
61
62
63
64
65void
66udp_input(register struct mbuf *m, int iphlen)
67{
68 Slirp *slirp = m->slirp;
69 register struct ip *ip;
70 register struct udphdr *uh;
71 int len;
72 struct ip save_ip;
73 struct socket *so;
74 struct sockaddr_storage lhost;
75 struct sockaddr_in *lhost4;
76
77 DEBUG_CALL("udp_input");
78 DEBUG_ARG("m = %p", m);
79 DEBUG_ARG("iphlen = %d", iphlen);
80
81
82
83
84
85
86
87 if(iphlen > sizeof(struct ip)) {
88 ip_stripoptions(m, (struct mbuf *)0);
89 iphlen = sizeof(struct ip);
90 }
91
92
93
94
95 ip = mtod(m, struct ip *);
96 uh = (struct udphdr *)((char *)ip + iphlen);
97
98
99
100
101
102 len = ntohs((uint16_t)uh->uh_ulen);
103
104 if (ip->ip_len != len) {
105 if (len > ip->ip_len) {
106 goto bad;
107 }
108 m_adj(m, len - ip->ip_len);
109 ip->ip_len = len;
110 }
111
112
113
114
115
116 save_ip = *ip;
117 save_ip.ip_len+= iphlen;
118
119
120
121
122 if (uh->uh_sum) {
123 memset(&((struct ipovly *)ip)->ih_mbuf, 0, sizeof(struct mbuf_ptr));
124 ((struct ipovly *)ip)->ih_x1 = 0;
125 ((struct ipovly *)ip)->ih_len = uh->uh_ulen;
126 if(cksum(m, len + sizeof(struct ip))) {
127 goto bad;
128 }
129 }
130
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
136
137
138
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 }
145
146
147
148
149 if (ntohs(uh->uh_dport) == TFTP_SERVER &&
150 ip->ip_dst.s_addr == slirp->vhost_addr.s_addr) {
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;
156 goto bad;
157 }
158
159 if (slirp->restricted) {
160 goto bad;
161 }
162
163
164
165
166 so = solookup(&slirp->udp_last_so, &slirp->udb, &lhost, NULL);
167
168 if (so == NULL) {
169
170
171
172
173 so = socreate(slirp);
174 if (udp_attach(so, AF_INET) == -1) {
175 DEBUG_MISC(" udp_attach errno = %d-%s", errno, strerror(errno));
176 sofree(so);
177 goto bad;
178 }
179
180
181
182
183 so->so_lfamily = AF_INET;
184 so->so_laddr = ip->ip_src;
185 so->so_lport = uh->uh_sport;
186
187 if ((so->so_iptos = udp_tos(so)) == 0)
188 so->so_iptos = ip->ip_tos;
189
190
191
192
193
194 }
195
196 so->so_ffamily = AF_INET;
197 so->so_faddr = ip->ip_dst;
198 so->so_fport = uh->uh_dport;
199
200 iphlen += sizeof(struct udphdr);
201 m->m_len -= iphlen;
202 m->m_data += iphlen;
203
204
205
206
207 if(sosendto(so,m) == -1) {
208 m->m_len += iphlen;
209 m->m_data -= iphlen;
210 *ip=save_ip;
211 DEBUG_MISC("udp tx errno = %d-%s", errno, strerror(errno));
212 icmp_send_error(m, ICMP_UNREACH, ICMP_UNREACH_NET, 0,
213 strerror(errno));
214 goto bad;
215 }
216
217 m_free(so->so_m);
218
219
220 m->m_len += iphlen;
221 m->m_data -= iphlen;
222 *ip=save_ip;
223 so->so_m=m;
224
225 return;
226bad:
227 m_free(m);
228}
229
230int udp_output(struct socket *so, struct mbuf *m,
231 struct sockaddr_in *saddr, struct sockaddr_in *daddr,
232 int iptos)
233{
234 register struct udpiphdr *ui;
235 int error = 0;
236
237 DEBUG_CALL("udp_output");
238 DEBUG_ARG("so = %p", so);
239 DEBUG_ARG("m = %p", m);
240 DEBUG_ARG("saddr = %s", inet_ntoa(saddr->sin_addr));
241 DEBUG_ARG("daddr = %s", inet_ntoa(daddr->sin_addr));
242
243
244
245
246 m->m_data -= sizeof(struct udpiphdr);
247 m->m_len += sizeof(struct udpiphdr);
248
249
250
251
252
253 ui = mtod(m, struct udpiphdr *);
254 memset(&ui->ui_i.ih_mbuf, 0 , sizeof(struct mbuf_ptr));
255 ui->ui_x1 = 0;
256 ui->ui_pr = IPPROTO_UDP;
257 ui->ui_len = htons(m->m_len - sizeof(struct ip));
258
259 ui->ui_src = saddr->sin_addr;
260 ui->ui_dst = daddr->sin_addr;
261 ui->ui_sport = saddr->sin_port;
262 ui->ui_dport = daddr->sin_port;
263 ui->ui_ulen = ui->ui_len;
264
265
266
267
268 ui->ui_sum = 0;
269 if ((ui->ui_sum = cksum(m, m->m_len)) == 0)
270 ui->ui_sum = 0xffff;
271 ((struct ip *)ui)->ip_len = m->m_len;
272
273 ((struct ip *)ui)->ip_ttl = IPDEFTTL;
274 ((struct ip *)ui)->ip_tos = iptos;
275
276 error = ip_output(so, m);
277
278 return (error);
279}
280
281int
282udp_attach(struct socket *so, unsigned short af)
283{
284 so->s = slirp_socket(af, SOCK_DGRAM, 0);
285 if (so->s != -1) {
286 so->so_expire = curtime + SO_EXPIRE;
287 insque(so, &so->slirp->udb);
288 }
289 return(so->s);
290}
291
292void
293udp_detach(struct socket *so)
294{
295 so->slirp->cb->unregister_poll_fd(so->s, so->slirp->opaque);
296 closesocket(so->s);
297 sofree(so);
298}
299
300static const struct tos_t udptos[] = {
301 {0, 53, IPTOS_LOWDELAY, 0},
302 {0, 0, 0, 0}
303};
304
305static uint8_t
306udp_tos(struct socket *so)
307{
308 int i = 0;
309
310 while(udptos[i].tos) {
311 if ((udptos[i].fport && ntohs(so->so_fport) == udptos[i].fport) ||
312 (udptos[i].lport && ntohs(so->so_lport) == udptos[i].lport)) {
313 so->so_emu = udptos[i].emu;
314 return udptos[i].tos;
315 }
316 i++;
317 }
318
319 return 0;
320}
321
322struct socket *
323udp_listen(Slirp *slirp, uint32_t haddr, unsigned hport, uint32_t laddr,
324 unsigned lport, int flags)
325{
326
327 struct sockaddr_in addr;
328 struct socket *so;
329 socklen_t addrlen = sizeof(struct sockaddr_in);
330
331 so = socreate(slirp);
332 so->s = slirp_socket(AF_INET,SOCK_DGRAM,0);
333 if (so->s < 0) {
334 sofree(so);
335 return NULL;
336 }
337 so->so_expire = curtime + SO_EXPIRE;
338 insque(so, &slirp->udb);
339
340 addr.sin_family = AF_INET;
341 addr.sin_addr.s_addr = haddr;
342 addr.sin_port = hport;
343
344 if (bind(so->s,(struct sockaddr *)&addr, addrlen) < 0) {
345 udp_detach(so);
346 return NULL;
347 }
348 slirp_socket_set_fast_reuse(so->s);
349
350 getsockname(so->s,(struct sockaddr *)&addr,&addrlen);
351 so->fhost.sin = addr;
352 sotranslate_accept(so);
353 so->so_lfamily = AF_INET;
354 so->so_lport = lport;
355 so->so_laddr.s_addr = laddr;
356 if (flags != SS_FACCEPTONCE)
357 so->so_expire = 0;
358
359 so->so_state &= SS_PERSISTENT_MASK;
360 so->so_state |= SS_ISFCONNECTED | flags;
361
362 return so;
363}
364