1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24#include "qemu/osdep.h"
25#include "slirp.h"
26
27#if defined(_WIN32)
28
29
30# define ntohl(n) ((uint32_t)ntohl(n))
31#endif
32
33
34
35#define LEASE_TIME (24 * 3600)
36
37static const uint8_t rfc1533_cookie[] = { RFC1533_COOKIE };
38
39#ifdef DEBUG
40#define DPRINTF(fmt, ...) \
41do if (slirp_debug & DBG_CALL) { fprintf(dfd, fmt, ## __VA_ARGS__); fflush(dfd); } while (0)
42#else
43#define DPRINTF(fmt, ...) do{}while(0)
44#endif
45
46static BOOTPClient *get_new_addr(Slirp *slirp, struct in_addr *paddr,
47 const uint8_t *macaddr)
48{
49 BOOTPClient *bc;
50 int i;
51
52 for(i = 0; i < NB_BOOTP_CLIENTS; i++) {
53 bc = &slirp->bootp_clients[i];
54 if (!bc->allocated || !memcmp(macaddr, bc->macaddr, 6))
55 goto found;
56 }
57 return NULL;
58 found:
59 bc = &slirp->bootp_clients[i];
60 bc->allocated = 1;
61 paddr->s_addr = slirp->vdhcp_startaddr.s_addr + htonl(i);
62 return bc;
63}
64
65static BOOTPClient *request_addr(Slirp *slirp, const struct in_addr *paddr,
66 const uint8_t *macaddr)
67{
68 uint32_t req_addr = ntohl(paddr->s_addr);
69 uint32_t dhcp_addr = ntohl(slirp->vdhcp_startaddr.s_addr);
70 BOOTPClient *bc;
71
72 if (req_addr >= dhcp_addr &&
73 req_addr < (dhcp_addr + NB_BOOTP_CLIENTS)) {
74 bc = &slirp->bootp_clients[req_addr - dhcp_addr];
75 if (!bc->allocated || !memcmp(macaddr, bc->macaddr, 6)) {
76 bc->allocated = 1;
77 return bc;
78 }
79 }
80 return NULL;
81}
82
83static BOOTPClient *find_addr(Slirp *slirp, struct in_addr *paddr,
84 const uint8_t *macaddr)
85{
86 BOOTPClient *bc;
87 int i;
88
89 for(i = 0; i < NB_BOOTP_CLIENTS; i++) {
90 if (!memcmp(macaddr, slirp->bootp_clients[i].macaddr, 6))
91 goto found;
92 }
93 return NULL;
94 found:
95 bc = &slirp->bootp_clients[i];
96 bc->allocated = 1;
97 paddr->s_addr = slirp->vdhcp_startaddr.s_addr + htonl(i);
98 return bc;
99}
100
101static void dhcp_decode(const struct bootp_t *bp, int *pmsg_type,
102 struct in_addr *preq_addr)
103{
104 const uint8_t *p, *p_end;
105 int len, tag;
106
107 *pmsg_type = 0;
108 preq_addr->s_addr = htonl(0L);
109
110 p = bp->bp_vend;
111 p_end = p + DHCP_OPT_LEN;
112 if (memcmp(p, rfc1533_cookie, 4) != 0)
113 return;
114 p += 4;
115 while (p < p_end) {
116 tag = p[0];
117 if (tag == RFC1533_PAD) {
118 p++;
119 } else if (tag == RFC1533_END) {
120 break;
121 } else {
122 p++;
123 if (p >= p_end)
124 break;
125 len = *p++;
126 if (p + len > p_end) {
127 break;
128 }
129 DPRINTF("dhcp: tag=%d len=%d\n", tag, len);
130
131 switch(tag) {
132 case RFC2132_MSG_TYPE:
133 if (len >= 1)
134 *pmsg_type = p[0];
135 break;
136 case RFC2132_REQ_ADDR:
137 if (len >= 4) {
138 memcpy(&(preq_addr->s_addr), p, 4);
139 }
140 break;
141 default:
142 break;
143 }
144 p += len;
145 }
146 }
147 if (*pmsg_type == DHCPREQUEST && preq_addr->s_addr == htonl(0L) &&
148 bp->bp_ciaddr.s_addr) {
149 memcpy(&(preq_addr->s_addr), &bp->bp_ciaddr, 4);
150 }
151}
152
153static void bootp_reply(Slirp *slirp, const struct bootp_t *bp)
154{
155 BOOTPClient *bc = NULL;
156 struct mbuf *m;
157 struct bootp_t *rbp;
158 struct sockaddr_in saddr, daddr;
159 struct in_addr preq_addr;
160 int dhcp_msg_type, val;
161 uint8_t *q;
162 uint8_t *end;
163 uint8_t client_ethaddr[ETH_ALEN];
164
165
166 dhcp_decode(bp, &dhcp_msg_type, &preq_addr);
167 DPRINTF("bootp packet op=%d msgtype=%d", bp->bp_op, dhcp_msg_type);
168 if (preq_addr.s_addr != htonl(0L))
169 DPRINTF(" req_addr=%08" PRIx32 "\n", ntohl(preq_addr.s_addr));
170 else
171 DPRINTF("\n");
172
173 if (dhcp_msg_type == 0)
174 dhcp_msg_type = DHCPREQUEST;
175
176 if (dhcp_msg_type != DHCPDISCOVER &&
177 dhcp_msg_type != DHCPREQUEST)
178 return;
179
180
181 memcpy(client_ethaddr, bp->bp_hwaddr, ETH_ALEN);
182
183 m = m_get(slirp);
184 if (!m) {
185 return;
186 }
187 m->m_data += IF_MAXLINKHDR;
188 rbp = (struct bootp_t *)m->m_data;
189 m->m_data += sizeof(struct udpiphdr);
190 memset(rbp, 0, sizeof(struct bootp_t));
191
192 if (dhcp_msg_type == DHCPDISCOVER) {
193 if (preq_addr.s_addr != htonl(0L)) {
194 bc = request_addr(slirp, &preq_addr, client_ethaddr);
195 if (bc) {
196 daddr.sin_addr = preq_addr;
197 }
198 }
199 if (!bc) {
200 new_addr:
201 bc = get_new_addr(slirp, &daddr.sin_addr, client_ethaddr);
202 if (!bc) {
203 DPRINTF("no address left\n");
204 return;
205 }
206 }
207 memcpy(bc->macaddr, client_ethaddr, ETH_ALEN);
208 } else if (preq_addr.s_addr != htonl(0L)) {
209 bc = request_addr(slirp, &preq_addr, client_ethaddr);
210 if (bc) {
211 daddr.sin_addr = preq_addr;
212 memcpy(bc->macaddr, client_ethaddr, ETH_ALEN);
213 } else {
214
215 daddr.sin_addr.s_addr = 0xffffffff;
216 }
217 } else {
218 bc = find_addr(slirp, &daddr.sin_addr, bp->bp_hwaddr);
219 if (!bc) {
220
221
222 goto new_addr;
223 }
224 }
225
226
227 arp_table_add(slirp, daddr.sin_addr.s_addr, client_ethaddr);
228
229 saddr.sin_addr = slirp->vhost_addr;
230 saddr.sin_port = htons(BOOTP_SERVER);
231
232 daddr.sin_port = htons(BOOTP_CLIENT);
233
234 rbp->bp_op = BOOTP_REPLY;
235 rbp->bp_xid = bp->bp_xid;
236 rbp->bp_htype = 1;
237 rbp->bp_hlen = 6;
238 memcpy(rbp->bp_hwaddr, bp->bp_hwaddr, ETH_ALEN);
239
240 rbp->bp_yiaddr = daddr.sin_addr;
241 rbp->bp_siaddr = saddr.sin_addr;
242
243 q = rbp->bp_vend;
244 end = (uint8_t *)&rbp[1];
245 memcpy(q, rfc1533_cookie, 4);
246 q += 4;
247
248 if (bc) {
249 DPRINTF("%s addr=%08" PRIx32 "\n",
250 (dhcp_msg_type == DHCPDISCOVER) ? "offered" : "ack'ed",
251 ntohl(daddr.sin_addr.s_addr));
252
253 if (dhcp_msg_type == DHCPDISCOVER) {
254 *q++ = RFC2132_MSG_TYPE;
255 *q++ = 1;
256 *q++ = DHCPOFFER;
257 } else {
258 *q++ = RFC2132_MSG_TYPE;
259 *q++ = 1;
260 *q++ = DHCPACK;
261 }
262
263 if (slirp->bootp_filename)
264 snprintf((char *)rbp->bp_file, sizeof(rbp->bp_file), "%s",
265 slirp->bootp_filename);
266
267 *q++ = RFC2132_SRV_ID;
268 *q++ = 4;
269 memcpy(q, &saddr.sin_addr, 4);
270 q += 4;
271
272 *q++ = RFC1533_NETMASK;
273 *q++ = 4;
274 memcpy(q, &slirp->vnetwork_mask, 4);
275 q += 4;
276
277 if (!slirp->restricted) {
278 *q++ = RFC1533_GATEWAY;
279 *q++ = 4;
280 memcpy(q, &saddr.sin_addr, 4);
281 q += 4;
282
283 *q++ = RFC1533_DNS;
284 *q++ = 4;
285 memcpy(q, &slirp->vnameserver_addr, 4);
286 q += 4;
287 }
288
289 *q++ = RFC2132_LEASE_TIME;
290 *q++ = 4;
291 val = htonl(LEASE_TIME);
292 memcpy(q, &val, 4);
293 q += 4;
294
295 if (*slirp->client_hostname) {
296 val = strlen(slirp->client_hostname);
297 if (q + val + 2 >= end) {
298 g_warning("DHCP packet size exceeded, "
299 "omitting host name option.");
300 } else {
301 *q++ = RFC1533_HOSTNAME;
302 *q++ = val;
303 memcpy(q, slirp->client_hostname, val);
304 q += val;
305 }
306 }
307
308 if (slirp->vdomainname) {
309 val = strlen(slirp->vdomainname);
310 if (q + val + 2 >= end) {
311 g_warning("DHCP packet size exceeded, "
312 "omitting domain name option.");
313 } else {
314 *q++ = RFC1533_DOMAINNAME;
315 *q++ = val;
316 memcpy(q, slirp->vdomainname, val);
317 q += val;
318 }
319 }
320
321 if (slirp->tftp_server_name) {
322 val = strlen(slirp->tftp_server_name);
323 if (q + val + 2 >= end) {
324 g_warning("DHCP packet size exceeded, "
325 "omitting tftp-server-name option.");
326 } else {
327 *q++ = RFC2132_TFTP_SERVER_NAME;
328 *q++ = val;
329 memcpy(q, slirp->tftp_server_name, val);
330 q += val;
331 }
332 }
333
334 if (slirp->vdnssearch) {
335 val = slirp->vdnssearch_len;
336 if (q + val >= end) {
337 g_warning("DHCP packet size exceeded, "
338 "omitting domain-search option.");
339 } else {
340 memcpy(q, slirp->vdnssearch, val);
341 q += val;
342 }
343 }
344 } else {
345 static const char nak_msg[] = "requested address not available";
346
347 DPRINTF("nak'ed addr=%08" PRIx32 "\n", ntohl(preq_addr.s_addr));
348
349 *q++ = RFC2132_MSG_TYPE;
350 *q++ = 1;
351 *q++ = DHCPNAK;
352
353 *q++ = RFC2132_MESSAGE;
354 *q++ = sizeof(nak_msg) - 1;
355 memcpy(q, nak_msg, sizeof(nak_msg) - 1);
356 q += sizeof(nak_msg) - 1;
357 }
358 assert(q < end);
359 *q = RFC1533_END;
360
361 daddr.sin_addr.s_addr = 0xffffffffu;
362
363 m->m_len = sizeof(struct bootp_t) -
364 sizeof(struct ip) - sizeof(struct udphdr);
365 udp_output(NULL, m, &saddr, &daddr, IPTOS_LOWDELAY);
366}
367
368void bootp_input(struct mbuf *m)
369{
370 struct bootp_t *bp = mtod(m, struct bootp_t *);
371
372 if (bp->bp_op == BOOTP_REQUEST) {
373 bootp_reply(m->slirp, bp);
374 }
375}
376