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 |
|
---|
29 | static const uint8_t rfc1533_cookie[] = { RFC1533_COOKIE };
|
---|
30 |
|
---|
31 | DECLINLINE(void) dprintf(const char *pszFormat, ...)
|
---|
32 | {
|
---|
33 | #ifdef LOG_ENABLED
|
---|
34 | va_list args;
|
---|
35 | va_start(args, pszFormat);
|
---|
36 | Log(("dhcp: %N", pszFormat, &args));
|
---|
37 | va_end(args);
|
---|
38 | #endif
|
---|
39 | }
|
---|
40 |
|
---|
41 | static BOOTPClient *get_new_addr(PNATState pData, struct in_addr *paddr)
|
---|
42 | {
|
---|
43 | BOOTPClient *bc;
|
---|
44 | int i;
|
---|
45 |
|
---|
46 | for(i = 0; i < NB_ADDR; i++) {
|
---|
47 | if (!bootp_clients[i].allocated)
|
---|
48 | goto found;
|
---|
49 | }
|
---|
50 | return NULL;
|
---|
51 | found:
|
---|
52 | bc = &bootp_clients[i];
|
---|
53 | bc->allocated = 1;
|
---|
54 | paddr->s_addr = htonl(ntohl(special_addr.s_addr) | (i + START_ADDR));
|
---|
55 | return bc;
|
---|
56 | }
|
---|
57 |
|
---|
58 | static void release_addr(PNATState pData, struct in_addr *paddr)
|
---|
59 | {
|
---|
60 | int i;
|
---|
61 |
|
---|
62 | i = ntohl(paddr->s_addr) - START_ADDR - ntohl(special_addr.s_addr);
|
---|
63 | if (i >= NB_ADDR)
|
---|
64 | return;
|
---|
65 | memset(bootp_clients[i].macaddr, '\0', 6);
|
---|
66 | bootp_clients[i].allocated = 0;
|
---|
67 | }
|
---|
68 |
|
---|
69 | static BOOTPClient *find_addr(PNATState pData, struct in_addr *paddr, const uint8_t *macaddr)
|
---|
70 | {
|
---|
71 | BOOTPClient *bc;
|
---|
72 | int i;
|
---|
73 |
|
---|
74 | for(i = 0; i < NB_ADDR; i++) {
|
---|
75 | if (!memcmp(macaddr, bootp_clients[i].macaddr, 6))
|
---|
76 | goto found;
|
---|
77 | }
|
---|
78 | return NULL;
|
---|
79 | found:
|
---|
80 | bc = &bootp_clients[i];
|
---|
81 | bc->allocated = 1;
|
---|
82 | paddr->s_addr = htonl(ntohl(special_addr.s_addr) | (i + START_ADDR));
|
---|
83 | return bc;
|
---|
84 | }
|
---|
85 |
|
---|
86 | static void dhcp_decode(const uint8_t *buf, int size,
|
---|
87 | int *pmsg_type, struct in_addr *req_ip)
|
---|
88 | {
|
---|
89 | const uint8_t *p, *p_end;
|
---|
90 | int len, tag;
|
---|
91 |
|
---|
92 | *pmsg_type = 0;
|
---|
93 |
|
---|
94 | p = buf;
|
---|
95 | p_end = buf + size;
|
---|
96 | if (size < 5)
|
---|
97 | return;
|
---|
98 | if (memcmp(p, rfc1533_cookie, 4) != 0)
|
---|
99 | return;
|
---|
100 | p += 4;
|
---|
101 | while (p < p_end) {
|
---|
102 | tag = p[0];
|
---|
103 | if (tag == RFC1533_PAD) {
|
---|
104 | p++;
|
---|
105 | } else if (tag == RFC1533_END) {
|
---|
106 | break;
|
---|
107 | } else {
|
---|
108 | p++;
|
---|
109 | if (p >= p_end)
|
---|
110 | break;
|
---|
111 | len = *p++;
|
---|
112 | dprintf("dhcp: tag=0x%02x len=%d\n", tag, len);
|
---|
113 |
|
---|
114 | switch(tag) {
|
---|
115 | case RFC2132_REQ_ADDR:
|
---|
116 | if (len >= 4)
|
---|
117 | *req_ip = *(struct in_addr*)p;
|
---|
118 | break;
|
---|
119 | case RFC2132_MSG_TYPE:
|
---|
120 | if (len >= 1)
|
---|
121 | *pmsg_type = p[0];
|
---|
122 | break;
|
---|
123 | default:
|
---|
124 | break;
|
---|
125 | }
|
---|
126 | p += len;
|
---|
127 | }
|
---|
128 | }
|
---|
129 | }
|
---|
130 |
|
---|
131 | static void bootp_reply(PNATState pData, struct bootp_t *bp)
|
---|
132 | {
|
---|
133 | BOOTPClient *bc;
|
---|
134 | struct mbuf *m;
|
---|
135 | struct bootp_t *rbp;
|
---|
136 | struct sockaddr_in saddr, daddr;
|
---|
137 | struct in_addr dns_addr_dhcp;
|
---|
138 | int dhcp_msg_type, val;
|
---|
139 | uint8_t *q;
|
---|
140 | struct in_addr requested_ip; /* the requested IP in DHCPREQUEST */
|
---|
141 | int send_nak = 0;
|
---|
142 | uint32_t ipv4_addr;
|
---|
143 |
|
---|
144 | /* extract exact DHCP msg type */
|
---|
145 | requested_ip.s_addr = 0xffffffff;
|
---|
146 | dhcp_decode(bp->bp_vend, DHCP_OPT_LEN, &dhcp_msg_type, &requested_ip);
|
---|
147 | dprintf("bootp packet op=%d msgtype=%d\n", bp->bp_op, dhcp_msg_type);
|
---|
148 |
|
---|
149 | if (dhcp_msg_type == 0)
|
---|
150 | dhcp_msg_type = DHCPREQUEST; /* Force reply for old BOOTP clients */
|
---|
151 |
|
---|
152 | if (dhcp_msg_type == DHCPRELEASE) {
|
---|
153 | ipv4_addr = ntohl(bp->bp_ciaddr.s_addr);
|
---|
154 | release_addr(pData, &bp->bp_ciaddr);
|
---|
155 | LogRel(("NAT: DHCP released IP address %u.%u.%u.%u\n",
|
---|
156 | ipv4_addr >> 24, (ipv4_addr >> 16) & 0xff, (ipv4_addr >> 8) & 0xff, ipv4_addr & 0xff));
|
---|
157 | dprintf("released addr=%08x\n", ntohl(bp->bp_ciaddr.s_addr));
|
---|
158 | /* This message is not to be answered in any way. */
|
---|
159 | return;
|
---|
160 | }
|
---|
161 | if (dhcp_msg_type != DHCPDISCOVER &&
|
---|
162 | dhcp_msg_type != DHCPREQUEST)
|
---|
163 | return;
|
---|
164 | /* XXX: this is a hack to get the client mac address */
|
---|
165 | memcpy(client_ethaddr, bp->bp_hwaddr, 6);
|
---|
166 |
|
---|
167 | if ((m = m_get(pData)) == NULL)
|
---|
168 | return;
|
---|
169 | m->m_data += if_maxlinkhdr;
|
---|
170 | rbp = (struct bootp_t *)m->m_data;
|
---|
171 | m->m_data += sizeof(struct udpiphdr);
|
---|
172 | memset(rbp, 0, sizeof(struct bootp_t));
|
---|
173 |
|
---|
174 | if (dhcp_msg_type == DHCPDISCOVER) {
|
---|
175 | /* Do not allocate a new lease for clients that forgot that they had a lease. */
|
---|
176 | bc = find_addr(pData, &daddr.sin_addr, bp->bp_hwaddr);
|
---|
177 | if (!bc)
|
---|
178 | {
|
---|
179 | new_addr:
|
---|
180 | bc = get_new_addr(pData, &daddr.sin_addr);
|
---|
181 | if (!bc) {
|
---|
182 | LogRel(("NAT: DHCP no IP address left\n"));
|
---|
183 | dprintf("no address left\n");
|
---|
184 | return;
|
---|
185 | }
|
---|
186 | memcpy(bc->macaddr, client_ethaddr, 6);
|
---|
187 | }
|
---|
188 | } else {
|
---|
189 | bc = find_addr(pData, &daddr.sin_addr, bp->bp_hwaddr);
|
---|
190 | if (!bc) {
|
---|
191 | /* if never assigned, behaves as if it was already
|
---|
192 | assigned (windows fix because it remembers its address) */
|
---|
193 | goto new_addr;
|
---|
194 | }
|
---|
195 | }
|
---|
196 |
|
---|
197 | if (tftp_prefix && RTDirExists(tftp_prefix) && bootp_filename)
|
---|
198 | RTStrPrintf((char*)rbp->bp_file, sizeof(rbp->bp_file), "%s", bootp_filename);
|
---|
199 |
|
---|
200 | saddr.sin_addr.s_addr = htonl(ntohl(special_addr.s_addr) | CTL_ALIAS);
|
---|
201 | saddr.sin_port = htons(BOOTP_SERVER);
|
---|
202 |
|
---|
203 | daddr.sin_port = htons(BOOTP_CLIENT);
|
---|
204 |
|
---|
205 | rbp->bp_op = BOOTP_REPLY;
|
---|
206 | rbp->bp_xid = bp->bp_xid;
|
---|
207 | rbp->bp_htype = 1;
|
---|
208 | rbp->bp_hlen = 6;
|
---|
209 | memcpy(rbp->bp_hwaddr, bp->bp_hwaddr, 6);
|
---|
210 |
|
---|
211 | rbp->bp_yiaddr = daddr.sin_addr; /* Client IP address */
|
---|
212 | rbp->bp_siaddr = saddr.sin_addr; /* Server IP address */
|
---|
213 |
|
---|
214 | q = rbp->bp_vend;
|
---|
215 | memcpy(q, rfc1533_cookie, 4);
|
---|
216 | q += 4;
|
---|
217 |
|
---|
218 | if (dhcp_msg_type == DHCPDISCOVER) {
|
---|
219 | *q++ = RFC2132_MSG_TYPE;
|
---|
220 | *q++ = 1;
|
---|
221 | *q++ = DHCPOFFER;
|
---|
222 | } else if (dhcp_msg_type == DHCPREQUEST) {
|
---|
223 | *q++ = RFC2132_MSG_TYPE;
|
---|
224 | *q++ = 1;
|
---|
225 | if ( requested_ip.s_addr != 0xffffffff
|
---|
226 | && requested_ip.s_addr != daddr.sin_addr.s_addr)
|
---|
227 | {
|
---|
228 | /* network changed */
|
---|
229 | *q++ = DHCPNAK;
|
---|
230 | send_nak = 1;
|
---|
231 | }
|
---|
232 | else
|
---|
233 | *q++ = DHCPACK;
|
---|
234 | }
|
---|
235 |
|
---|
236 | if (send_nak)
|
---|
237 | {
|
---|
238 | ipv4_addr = ntohl(requested_ip.s_addr);
|
---|
239 | LogRel(("NAT: Client requested IP address %u.%u.%u.%u -- sending NAK\n",
|
---|
240 | ipv4_addr >> 24, (ipv4_addr >> 16) & 0xff, (ipv4_addr >> 8) & 0xff, ipv4_addr & 0xff));
|
---|
241 | }
|
---|
242 | else
|
---|
243 | {
|
---|
244 | ipv4_addr = ntohl(daddr.sin_addr.s_addr);
|
---|
245 | LogRel(("NAT: DHCP offered IP address %u.%u.%u.%u\n",
|
---|
246 | ipv4_addr >> 24, (ipv4_addr >> 16) & 0xff, (ipv4_addr >> 8) & 0xff, ipv4_addr & 0xff));
|
---|
247 | dprintf("offered addr=%08x\n", ntohl(daddr.sin_addr.s_addr));
|
---|
248 | }
|
---|
249 |
|
---|
250 | if (dhcp_msg_type == DHCPDISCOVER ||
|
---|
251 | dhcp_msg_type == DHCPREQUEST) {
|
---|
252 | *q++ = RFC2132_SRV_ID;
|
---|
253 | *q++ = 4;
|
---|
254 | memcpy(q, &saddr.sin_addr, 4);
|
---|
255 | q += 4;
|
---|
256 | }
|
---|
257 |
|
---|
258 | if (!send_nak &&
|
---|
259 | (dhcp_msg_type == DHCPDISCOVER ||
|
---|
260 | dhcp_msg_type == DHCPREQUEST)) {
|
---|
261 | *q++ = RFC1533_NETMASK;
|
---|
262 | *q++ = 4;
|
---|
263 | *q++ = (pData->netmask & 0xff000000) >> 24;
|
---|
264 | *q++ = (pData->netmask & 0x00ff0000) >> 16;
|
---|
265 | *q++ = (pData->netmask & 0x0000ff00) >> 8;
|
---|
266 | *q++ = (pData->netmask & 0x000000ff);
|
---|
267 |
|
---|
268 | *q++ = RFC1533_GATEWAY;
|
---|
269 | *q++ = 4;
|
---|
270 | memcpy(q, &saddr.sin_addr, 4);
|
---|
271 | q += 4;
|
---|
272 |
|
---|
273 | *q++ = RFC1533_DNS;
|
---|
274 | *q++ = 4;
|
---|
275 | dns_addr_dhcp.s_addr = htonl(ntohl(special_addr.s_addr) | CTL_DNS);
|
---|
276 | memcpy(q, &dns_addr_dhcp, 4);
|
---|
277 | q += 4;
|
---|
278 |
|
---|
279 | *q++ = RFC2132_LEASE_TIME;
|
---|
280 | *q++ = 4;
|
---|
281 | val = htonl(LEASE_TIME);
|
---|
282 | memcpy(q, &val, 4);
|
---|
283 | q += 4;
|
---|
284 |
|
---|
285 | if (*slirp_hostname) {
|
---|
286 | val = strlen(slirp_hostname);
|
---|
287 | *q++ = RFC1533_HOSTNAME;
|
---|
288 | *q++ = val;
|
---|
289 | memcpy(q, slirp_hostname, val);
|
---|
290 | q += val;
|
---|
291 | }
|
---|
292 |
|
---|
293 | if (pData->pszDomain && pData->fPassDomain)
|
---|
294 | {
|
---|
295 | val = strlen(pData->pszDomain);
|
---|
296 | *q++ = RFC1533_DOMAINNAME;
|
---|
297 | *q++ = val;
|
---|
298 | memcpy(q, pData->pszDomain, val);
|
---|
299 | q += val;
|
---|
300 | }
|
---|
301 | }
|
---|
302 | *q++ = RFC1533_END;
|
---|
303 |
|
---|
304 | m->m_len = sizeof(struct bootp_t) -
|
---|
305 | sizeof(struct ip) - sizeof(struct udphdr);
|
---|
306 | /* Reply to the broadcast address, as some clients perform paranoid checks. */
|
---|
307 | daddr.sin_addr.s_addr = INADDR_BROADCAST;
|
---|
308 | udp_output2(pData, NULL, m, &saddr, &daddr, IPTOS_LOWDELAY);
|
---|
309 | }
|
---|
310 |
|
---|
311 | void bootp_input(PNATState pData, struct mbuf *m)
|
---|
312 | {
|
---|
313 | struct bootp_t *bp = mtod(m, struct bootp_t *);
|
---|
314 |
|
---|
315 | if (bp->bp_op == BOOTP_REQUEST) {
|
---|
316 | bootp_reply(pData, bp);
|
---|
317 | }
|
---|
318 | }
|
---|