1 | /*
|
---|
2 | * QEMU BOOTP/DHCP server
|
---|
3 | *
|
---|
4 | * Copyright (c) 2004 Fabrice Bellard
|
---|
5 | *
|
---|
6 | * Permission is hereby granted, free of charge, to any person obtaining a copy
|
---|
7 | * of this software and associated documentation files (the "Software"), to deal
|
---|
8 | * in the Software without restriction, including without limitation the rights
|
---|
9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
---|
10 | * copies of the Software, and to permit persons to whom the Software is
|
---|
11 | * furnished to do so, subject to the following conditions:
|
---|
12 | *
|
---|
13 | * The above copyright notice and this permission notice shall be included in
|
---|
14 | * all copies or substantial portions of the Software.
|
---|
15 | *
|
---|
16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
---|
17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
---|
18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
|
---|
19 | * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
---|
20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
---|
21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
---|
22 | * THE SOFTWARE.
|
---|
23 | */
|
---|
24 | #include <slirp.h>
|
---|
25 |
|
---|
26 | /* XXX: only DHCP is supported */
|
---|
27 |
|
---|
28 | #define NB_ADDR 16
|
---|
29 |
|
---|
30 | #define START_ADDR 15
|
---|
31 |
|
---|
32 | #define LEASE_TIME (24 * 3600)
|
---|
33 |
|
---|
34 | typedef struct {
|
---|
35 | uint8_t allocated;
|
---|
36 | uint8_t macaddr[6];
|
---|
37 | } BOOTPClient;
|
---|
38 |
|
---|
39 | BOOTPClient bootp_clients[NB_ADDR];
|
---|
40 |
|
---|
41 | static const uint8_t rfc1533_cookie[] = { RFC1533_COOKIE };
|
---|
42 |
|
---|
43 | #ifndef VBOX
|
---|
44 | #ifdef DEBUG
|
---|
45 | #define dprintf(fmt, args...) \
|
---|
46 | if (slirp_debug & DBG_CALL) { fprintf(dfd, fmt, ## args); fflush(dfd); }
|
---|
47 | #else
|
---|
48 | #define dprintf(fmt, args...)
|
---|
49 | #endif
|
---|
50 | #else /* VBOX */
|
---|
51 | DECLINLINE(void) dprintf(const char *pszFormat, ...)
|
---|
52 | {
|
---|
53 | #ifdef LOG_ENABLED
|
---|
54 | va_list args;
|
---|
55 | va_start(args, pszFormat);
|
---|
56 | Log(("dhcp: %N", pszFormat, &args));
|
---|
57 | va_end(args);
|
---|
58 | #endif
|
---|
59 | }
|
---|
60 | #endif /* VBOX */
|
---|
61 |
|
---|
62 | static BOOTPClient *get_new_addr(struct in_addr *paddr)
|
---|
63 | {
|
---|
64 | BOOTPClient *bc;
|
---|
65 | int i;
|
---|
66 |
|
---|
67 | for(i = 0; i < NB_ADDR; i++) {
|
---|
68 | if (!bootp_clients[i].allocated)
|
---|
69 | goto found;
|
---|
70 | }
|
---|
71 | return NULL;
|
---|
72 | found:
|
---|
73 | bc = &bootp_clients[i];
|
---|
74 | bc->allocated = 1;
|
---|
75 | paddr->s_addr = htonl(ntohl(special_addr.s_addr) | (i + START_ADDR));
|
---|
76 | return bc;
|
---|
77 | }
|
---|
78 |
|
---|
79 | #ifdef VBOX
|
---|
80 | static void release_addr(struct in_addr *paddr)
|
---|
81 | {
|
---|
82 | int i;
|
---|
83 |
|
---|
84 | i = ntohl(paddr->s_addr) - START_ADDR - ntohl(special_addr.s_addr);
|
---|
85 | if (i >= NB_ADDR)
|
---|
86 | return;
|
---|
87 | memset(bootp_clients[i].macaddr, '\0', 6);
|
---|
88 | bootp_clients[i].allocated = 0;
|
---|
89 | }
|
---|
90 | #endif /* VBOX */
|
---|
91 |
|
---|
92 | static BOOTPClient *find_addr(struct in_addr *paddr, const uint8_t *macaddr)
|
---|
93 | {
|
---|
94 | BOOTPClient *bc;
|
---|
95 | int i;
|
---|
96 |
|
---|
97 | for(i = 0; i < NB_ADDR; i++) {
|
---|
98 | if (!memcmp(macaddr, bootp_clients[i].macaddr, 6))
|
---|
99 | goto found;
|
---|
100 | }
|
---|
101 | return NULL;
|
---|
102 | found:
|
---|
103 | bc = &bootp_clients[i];
|
---|
104 | bc->allocated = 1;
|
---|
105 | paddr->s_addr = htonl(ntohl(special_addr.s_addr) | (i + START_ADDR));
|
---|
106 | return bc;
|
---|
107 | }
|
---|
108 |
|
---|
109 | static void dhcp_decode(const uint8_t *buf, int size,
|
---|
110 | int *pmsg_type)
|
---|
111 | {
|
---|
112 | const uint8_t *p, *p_end;
|
---|
113 | int len, tag;
|
---|
114 |
|
---|
115 | *pmsg_type = 0;
|
---|
116 |
|
---|
117 | p = buf;
|
---|
118 | p_end = buf + size;
|
---|
119 | if (size < 5)
|
---|
120 | return;
|
---|
121 | if (memcmp(p, rfc1533_cookie, 4) != 0)
|
---|
122 | return;
|
---|
123 | p += 4;
|
---|
124 | while (p < p_end) {
|
---|
125 | tag = p[0];
|
---|
126 | if (tag == RFC1533_PAD) {
|
---|
127 | p++;
|
---|
128 | } else if (tag == RFC1533_END) {
|
---|
129 | break;
|
---|
130 | } else {
|
---|
131 | p++;
|
---|
132 | if (p >= p_end)
|
---|
133 | break;
|
---|
134 | len = *p++;
|
---|
135 | dprintf("dhcp: tag=0x%02x len=%d\n", tag, len);
|
---|
136 |
|
---|
137 | switch(tag) {
|
---|
138 | case RFC2132_MSG_TYPE:
|
---|
139 | if (len >= 1)
|
---|
140 | *pmsg_type = p[0];
|
---|
141 | break;
|
---|
142 | default:
|
---|
143 | break;
|
---|
144 | }
|
---|
145 | p += len;
|
---|
146 | }
|
---|
147 | }
|
---|
148 | }
|
---|
149 |
|
---|
150 | static void bootp_reply(struct bootp_t *bp)
|
---|
151 | {
|
---|
152 | BOOTPClient *bc;
|
---|
153 | struct mbuf *m;
|
---|
154 | struct bootp_t *rbp;
|
---|
155 | struct sockaddr_in saddr, daddr;
|
---|
156 | struct in_addr dns_addr;
|
---|
157 | int dhcp_msg_type, val;
|
---|
158 | uint8_t *q;
|
---|
159 |
|
---|
160 | /* extract exact DHCP msg type */
|
---|
161 | dhcp_decode(bp->bp_vend, DHCP_OPT_LEN, &dhcp_msg_type);
|
---|
162 | dprintf("bootp packet op=%d msgtype=%d\n", bp->bp_op, dhcp_msg_type);
|
---|
163 |
|
---|
164 | if (dhcp_msg_type == 0)
|
---|
165 | dhcp_msg_type = DHCPREQUEST; /* Force reply for old BOOTP clients */
|
---|
166 |
|
---|
167 | #ifdef VBOX
|
---|
168 | if (dhcp_msg_type == DHCPRELEASE) {
|
---|
169 | uint32_t addr = ntohl(bp->bp_ciaddr.s_addr);
|
---|
170 | release_addr(&bp->bp_ciaddr);
|
---|
171 | LogRel(("NAT: DHCP released IP address %u.%u.%u.%u\n",
|
---|
172 | addr >> 24, (addr >> 16) & 0xff, (addr >> 8) & 0xff, addr & 0xff));
|
---|
173 | dprintf("released addr=%08x\n", ntohl(bp->bp_ciaddr.s_addr));
|
---|
174 | /* This message is not to be answered in any way. */
|
---|
175 | return;
|
---|
176 | }
|
---|
177 | #endif /* VBOX */
|
---|
178 | if (dhcp_msg_type != DHCPDISCOVER &&
|
---|
179 | dhcp_msg_type != DHCPREQUEST)
|
---|
180 | return;
|
---|
181 | /* XXX: this is a hack to get the client mac address */
|
---|
182 | memcpy(client_ethaddr, bp->bp_hwaddr, 6);
|
---|
183 |
|
---|
184 | if ((m = m_get()) == NULL)
|
---|
185 | return;
|
---|
186 | m->m_data += if_maxlinkhdr;
|
---|
187 | rbp = (struct bootp_t *)m->m_data;
|
---|
188 | m->m_data += sizeof(struct udpiphdr);
|
---|
189 | memset(rbp, 0, sizeof(struct bootp_t));
|
---|
190 |
|
---|
191 | if (dhcp_msg_type == DHCPDISCOVER) {
|
---|
192 | #ifdef VBOX
|
---|
193 | /* Do not allocate a new lease for clients that forgot that they had a lease. */
|
---|
194 | bc = find_addr(&daddr.sin_addr, bp->bp_hwaddr);
|
---|
195 | if (bc)
|
---|
196 | goto reuse_lease;
|
---|
197 | #endif /* VBOX */
|
---|
198 | new_addr:
|
---|
199 | bc = get_new_addr(&daddr.sin_addr);
|
---|
200 | if (!bc) {
|
---|
201 | #ifdef VBOX
|
---|
202 | LogRel(("NAT: DHCP no IP address left\n"));
|
---|
203 | #endif /* VBOX */
|
---|
204 | dprintf("no address left\n");
|
---|
205 | return;
|
---|
206 | }
|
---|
207 | memcpy(bc->macaddr, client_ethaddr, 6);
|
---|
208 | #ifdef VBOX
|
---|
209 | reuse_lease: ;
|
---|
210 | #endif /* VBOX */
|
---|
211 | } else {
|
---|
212 | bc = find_addr(&daddr.sin_addr, bp->bp_hwaddr);
|
---|
213 | if (!bc) {
|
---|
214 | /* if never assigned, behaves as if it was already
|
---|
215 | assigned (windows fix because it remembers its address) */
|
---|
216 | goto new_addr;
|
---|
217 | }
|
---|
218 | }
|
---|
219 | #ifdef VBOX
|
---|
220 | {
|
---|
221 | uint32_t addr = ntohl(daddr.sin_addr.s_addr);
|
---|
222 | LogRel(("NAT: DHCP offered IP address %u.%u.%u.%u\n",
|
---|
223 | addr >> 24, (addr >> 16) & 0xff, (addr >> 8) & 0xff, addr & 0xff));
|
---|
224 | }
|
---|
225 | #endif /* VBOX */
|
---|
226 | dprintf("offered addr=%08x\n", ntohl(daddr.sin_addr.s_addr));
|
---|
227 |
|
---|
228 | saddr.sin_addr.s_addr = htonl(ntohl(special_addr.s_addr) | CTL_ALIAS);
|
---|
229 | saddr.sin_port = htons(BOOTP_SERVER);
|
---|
230 |
|
---|
231 | daddr.sin_port = htons(BOOTP_CLIENT);
|
---|
232 |
|
---|
233 | rbp->bp_op = BOOTP_REPLY;
|
---|
234 | rbp->bp_xid = bp->bp_xid;
|
---|
235 | rbp->bp_htype = 1;
|
---|
236 | rbp->bp_hlen = 6;
|
---|
237 | memcpy(rbp->bp_hwaddr, bp->bp_hwaddr, 6);
|
---|
238 |
|
---|
239 | rbp->bp_yiaddr = daddr.sin_addr; /* Client IP address */
|
---|
240 | rbp->bp_siaddr = saddr.sin_addr; /* Server IP address */
|
---|
241 |
|
---|
242 | q = rbp->bp_vend;
|
---|
243 | memcpy(q, rfc1533_cookie, 4);
|
---|
244 | q += 4;
|
---|
245 |
|
---|
246 | if (dhcp_msg_type == DHCPDISCOVER) {
|
---|
247 | *q++ = RFC2132_MSG_TYPE;
|
---|
248 | *q++ = 1;
|
---|
249 | *q++ = DHCPOFFER;
|
---|
250 | } else if (dhcp_msg_type == DHCPREQUEST) {
|
---|
251 | *q++ = RFC2132_MSG_TYPE;
|
---|
252 | *q++ = 1;
|
---|
253 | *q++ = DHCPACK;
|
---|
254 | }
|
---|
255 |
|
---|
256 | if (dhcp_msg_type == DHCPDISCOVER ||
|
---|
257 | dhcp_msg_type == DHCPREQUEST) {
|
---|
258 | *q++ = RFC2132_SRV_ID;
|
---|
259 | *q++ = 4;
|
---|
260 | memcpy(q, &saddr.sin_addr, 4);
|
---|
261 | q += 4;
|
---|
262 |
|
---|
263 | *q++ = RFC1533_NETMASK;
|
---|
264 | *q++ = 4;
|
---|
265 | *q++ = 0xff;
|
---|
266 | *q++ = 0xff;
|
---|
267 | *q++ = 0xff;
|
---|
268 | *q++ = 0x00;
|
---|
269 |
|
---|
270 | *q++ = RFC1533_GATEWAY;
|
---|
271 | *q++ = 4;
|
---|
272 | memcpy(q, &saddr.sin_addr, 4);
|
---|
273 | q += 4;
|
---|
274 |
|
---|
275 | *q++ = RFC1533_DNS;
|
---|
276 | *q++ = 4;
|
---|
277 | dns_addr.s_addr = htonl(ntohl(special_addr.s_addr) | CTL_DNS);
|
---|
278 | memcpy(q, &dns_addr, 4);
|
---|
279 | q += 4;
|
---|
280 |
|
---|
281 | *q++ = RFC2132_LEASE_TIME;
|
---|
282 | *q++ = 4;
|
---|
283 | val = htonl(LEASE_TIME);
|
---|
284 | memcpy(q, &val, 4);
|
---|
285 | q += 4;
|
---|
286 |
|
---|
287 | if (*slirp_hostname) {
|
---|
288 | val = strlen(slirp_hostname);
|
---|
289 | *q++ = RFC1533_HOSTNAME;
|
---|
290 | *q++ = val;
|
---|
291 | memcpy(q, slirp_hostname, val);
|
---|
292 | q += val;
|
---|
293 | }
|
---|
294 | }
|
---|
295 | *q++ = RFC1533_END;
|
---|
296 |
|
---|
297 | m->m_len = sizeof(struct bootp_t) -
|
---|
298 | sizeof(struct ip) - sizeof(struct udphdr);
|
---|
299 | #ifdef VBOX
|
---|
300 | /* Reply to the broadcast address, as some clients perform paranoid checks. */
|
---|
301 | daddr.sin_addr.s_addr = INADDR_BROADCAST;
|
---|
302 | #endif /* VBOX */
|
---|
303 | udp_output2(NULL, m, &saddr, &daddr, IPTOS_LOWDELAY);
|
---|
304 | }
|
---|
305 |
|
---|
306 | void bootp_input(struct mbuf *m)
|
---|
307 | {
|
---|
308 | struct bootp_t *bp = mtod(m, struct bootp_t *);
|
---|
309 |
|
---|
310 | if (bp->bp_op == BOOTP_REQUEST) {
|
---|
311 | bootp_reply(bp);
|
---|
312 | }
|
---|
313 | }
|
---|