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 DPRINTF("dhcp: tag=%d len=%d\n", tag, len);
127
128 switch(tag) {
129 case RFC2132_MSG_TYPE:
130 if (len >= 1)
131 *pmsg_type = p[0];
132 break;
133 case RFC2132_REQ_ADDR:
134 if (len >= 4) {
135 memcpy(&(preq_addr->s_addr), p, 4);
136 }
137 break;
138 default:
139 break;
140 }
141 p += len;
142 }
143 }
144 if (*pmsg_type == DHCPREQUEST && preq_addr->s_addr == htonl(0L) &&
145 bp->bp_ciaddr.s_addr) {
146 memcpy(&(preq_addr->s_addr), &bp->bp_ciaddr, 4);
147 }
148}
149
150static void bootp_reply(Slirp *slirp, const struct bootp_t *bp)
151{
152 BOOTPClient *bc = NULL;
153 struct mbuf *m;
154 struct bootp_t *rbp;
155 struct sockaddr_in saddr, daddr;
156 struct in_addr preq_addr;
157 int dhcp_msg_type, val;
158 uint8_t *q;
159 uint8_t client_ethaddr[ETH_ALEN];
160
161
162 dhcp_decode(bp, &dhcp_msg_type, &preq_addr);
163 DPRINTF("bootp packet op=%d msgtype=%d", bp->bp_op, dhcp_msg_type);
164 if (preq_addr.s_addr != htonl(0L))
165 DPRINTF(" req_addr=%08" PRIx32 "\n", ntohl(preq_addr.s_addr));
166 else
167 DPRINTF("\n");
168
169 if (dhcp_msg_type == 0)
170 dhcp_msg_type = DHCPREQUEST;
171
172 if (dhcp_msg_type != DHCPDISCOVER &&
173 dhcp_msg_type != DHCPREQUEST)
174 return;
175
176
177 memcpy(client_ethaddr, bp->bp_hwaddr, ETH_ALEN);
178
179 m = m_get(slirp);
180 if (!m) {
181 return;
182 }
183 m->m_data += IF_MAXLINKHDR;
184 rbp = (struct bootp_t *)m->m_data;
185 m->m_data += sizeof(struct udpiphdr);
186 memset(rbp, 0, sizeof(struct bootp_t));
187
188 if (dhcp_msg_type == DHCPDISCOVER) {
189 if (preq_addr.s_addr != htonl(0L)) {
190 bc = request_addr(slirp, &preq_addr, client_ethaddr);
191 if (bc) {
192 daddr.sin_addr = preq_addr;
193 }
194 }
195 if (!bc) {
196 new_addr:
197 bc = get_new_addr(slirp, &daddr.sin_addr, client_ethaddr);
198 if (!bc) {
199 DPRINTF("no address left\n");
200 return;
201 }
202 }
203 memcpy(bc->macaddr, client_ethaddr, ETH_ALEN);
204 } else if (preq_addr.s_addr != htonl(0L)) {
205 bc = request_addr(slirp, &preq_addr, client_ethaddr);
206 if (bc) {
207 daddr.sin_addr = preq_addr;
208 memcpy(bc->macaddr, client_ethaddr, ETH_ALEN);
209 } else {
210
211 daddr.sin_addr.s_addr = 0xffffffff;
212 }
213 } else {
214 bc = find_addr(slirp, &daddr.sin_addr, bp->bp_hwaddr);
215 if (!bc) {
216
217
218 goto new_addr;
219 }
220 }
221
222
223 arp_table_add(slirp, daddr.sin_addr.s_addr, client_ethaddr);
224
225 saddr.sin_addr = slirp->vhost_addr;
226 saddr.sin_port = htons(BOOTP_SERVER);
227
228 daddr.sin_port = htons(BOOTP_CLIENT);
229
230 rbp->bp_op = BOOTP_REPLY;
231 rbp->bp_xid = bp->bp_xid;
232 rbp->bp_htype = 1;
233 rbp->bp_hlen = 6;
234 memcpy(rbp->bp_hwaddr, bp->bp_hwaddr, ETH_ALEN);
235
236 rbp->bp_yiaddr = daddr.sin_addr;
237 rbp->bp_siaddr = saddr.sin_addr;
238
239 q = rbp->bp_vend;
240 memcpy(q, rfc1533_cookie, 4);
241 q += 4;
242
243 if (bc) {
244 DPRINTF("%s addr=%08" PRIx32 "\n",
245 (dhcp_msg_type == DHCPDISCOVER) ? "offered" : "ack'ed",
246 ntohl(daddr.sin_addr.s_addr));
247
248 if (dhcp_msg_type == DHCPDISCOVER) {
249 *q++ = RFC2132_MSG_TYPE;
250 *q++ = 1;
251 *q++ = DHCPOFFER;
252 } else {
253 *q++ = RFC2132_MSG_TYPE;
254 *q++ = 1;
255 *q++ = DHCPACK;
256 }
257
258 if (slirp->bootp_filename)
259 snprintf((char *)rbp->bp_file, sizeof(rbp->bp_file), "%s",
260 slirp->bootp_filename);
261
262 *q++ = RFC2132_SRV_ID;
263 *q++ = 4;
264 memcpy(q, &saddr.sin_addr, 4);
265 q += 4;
266
267 *q++ = RFC1533_NETMASK;
268 *q++ = 4;
269 memcpy(q, &slirp->vnetwork_mask, 4);
270 q += 4;
271
272 if (!slirp->restricted) {
273 *q++ = RFC1533_GATEWAY;
274 *q++ = 4;
275 memcpy(q, &saddr.sin_addr, 4);
276 q += 4;
277
278 *q++ = RFC1533_DNS;
279 *q++ = 4;
280 memcpy(q, &slirp->vnameserver_addr, 4);
281 q += 4;
282 }
283
284 *q++ = RFC2132_LEASE_TIME;
285 *q++ = 4;
286 val = htonl(LEASE_TIME);
287 memcpy(q, &val, 4);
288 q += 4;
289
290 if (*slirp->client_hostname) {
291 val = strlen(slirp->client_hostname);
292 *q++ = RFC1533_HOSTNAME;
293 *q++ = val;
294 memcpy(q, slirp->client_hostname, val);
295 q += val;
296 }
297
298 if (slirp->vdnssearch) {
299 size_t spaceleft = sizeof(rbp->bp_vend) - (q - rbp->bp_vend);
300 val = slirp->vdnssearch_len;
301 if (val + 1 > spaceleft) {
302 g_warning("DHCP packet size exceeded, "
303 "omitting domain-search option.");
304 } else {
305 memcpy(q, slirp->vdnssearch, val);
306 q += val;
307 }
308 }
309 } else {
310 static const char nak_msg[] = "requested address not available";
311
312 DPRINTF("nak'ed addr=%08" PRIx32 "\n", ntohl(preq_addr.s_addr));
313
314 *q++ = RFC2132_MSG_TYPE;
315 *q++ = 1;
316 *q++ = DHCPNAK;
317
318 *q++ = RFC2132_MESSAGE;
319 *q++ = sizeof(nak_msg) - 1;
320 memcpy(q, nak_msg, sizeof(nak_msg) - 1);
321 q += sizeof(nak_msg) - 1;
322 }
323 *q = RFC1533_END;
324
325 daddr.sin_addr.s_addr = 0xffffffffu;
326
327 m->m_len = sizeof(struct bootp_t) -
328 sizeof(struct ip) - sizeof(struct udphdr);
329 udp_output(NULL, m, &saddr, &daddr, IPTOS_LOWDELAY);
330}
331
332void bootp_input(struct mbuf *m)
333{
334 struct bootp_t *bp = mtod(m, struct bootp_t *);
335
336 if (bp->bp_op == BOOTP_REQUEST) {
337 bootp_reply(m->slirp, bp);
338 }
339}
340