blob: 7b3232bdc4b0d665a0e637bcefe90b7204932b2b [file] [log] [blame]
bellardf0cbd3e2004-04-22 00:10:48 +00001/*
2 * QEMU BOOTP/DHCP server
ths5fafdf22007-09-16 21:08:06 +00003 *
bellardf0cbd3e2004-04-22 00:10:48 +00004 * Copyright (c) 2004 Fabrice Bellard
ths5fafdf22007-09-16 21:08:06 +00005 *
bellardf0cbd3e2004-04-22 00:10:48 +00006 * 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 */
Peter Maydell7df74822016-01-29 17:49:59 +000024#include "qemu/osdep.h"
bellardf0cbd3e2004-04-22 00:10:48 +000025#include <slirp.h>
26
Stefan Weilecc804c2015-08-29 09:12:35 +020027#if defined(_WIN32)
28/* Windows ntohl() returns an u_long value.
29 * Add a type cast to match the format strings. */
30# define ntohl(n) ((uint32_t)ntohl(n))
31#endif
32
bellardf0cbd3e2004-04-22 00:10:48 +000033/* XXX: only DHCP is supported */
34
bellardf0cbd3e2004-04-22 00:10:48 +000035#define LEASE_TIME (24 * 3600)
36
bellardf0cbd3e2004-04-22 00:10:48 +000037static const uint8_t rfc1533_cookie[] = { RFC1533_COOKIE };
38
39#ifdef DEBUG
malcd0f2c4c2010-02-07 02:03:50 +030040#define DPRINTF(fmt, ...) \
Jan Kiszka9f349492009-06-24 14:42:29 +020041do if (slirp_debug & DBG_CALL) { fprintf(dfd, fmt, ## __VA_ARGS__); fflush(dfd); } while (0)
bellardf0cbd3e2004-04-22 00:10:48 +000042#else
Jes Sorensen7390cdf2010-08-31 09:30:37 +020043#define DPRINTF(fmt, ...) do{}while(0)
bellardf0cbd3e2004-04-22 00:10:48 +000044#endif
45
Jan Kiszka460fec62009-06-24 14:42:31 +020046static BOOTPClient *get_new_addr(Slirp *slirp, struct in_addr *paddr,
Jan Kiszka0928a952009-05-21 22:43:39 +020047 const uint8_t *macaddr)
bellardf0cbd3e2004-04-22 00:10:48 +000048{
49 BOOTPClient *bc;
50 int i;
51
Jan Kiszka460fec62009-06-24 14:42:31 +020052 for(i = 0; i < NB_BOOTP_CLIENTS; i++) {
53 bc = &slirp->bootp_clients[i];
Jan Kiszka0928a952009-05-21 22:43:39 +020054 if (!bc->allocated || !memcmp(macaddr, bc->macaddr, 6))
bellardf0cbd3e2004-04-22 00:10:48 +000055 goto found;
56 }
57 return NULL;
58 found:
Jan Kiszka460fec62009-06-24 14:42:31 +020059 bc = &slirp->bootp_clients[i];
bellardf0cbd3e2004-04-22 00:10:48 +000060 bc->allocated = 1;
Jan Kiszka460fec62009-06-24 14:42:31 +020061 paddr->s_addr = slirp->vdhcp_startaddr.s_addr + htonl(i);
bellardf0cbd3e2004-04-22 00:10:48 +000062 return bc;
63}
64
Jan Kiszka460fec62009-06-24 14:42:31 +020065static BOOTPClient *request_addr(Slirp *slirp, const struct in_addr *paddr,
aliguorib63c7f62009-04-21 19:56:20 +000066 const uint8_t *macaddr)
67{
68 uint32_t req_addr = ntohl(paddr->s_addr);
Jan Kiszka460fec62009-06-24 14:42:31 +020069 uint32_t dhcp_addr = ntohl(slirp->vdhcp_startaddr.s_addr);
aliguorib63c7f62009-04-21 19:56:20 +000070 BOOTPClient *bc;
71
Jan Kiszkaa13a4122009-06-24 14:42:28 +020072 if (req_addr >= dhcp_addr &&
Jan Kiszka460fec62009-06-24 14:42:31 +020073 req_addr < (dhcp_addr + NB_BOOTP_CLIENTS)) {
74 bc = &slirp->bootp_clients[req_addr - dhcp_addr];
aliguorib63c7f62009-04-21 19:56:20 +000075 if (!bc->allocated || !memcmp(macaddr, bc->macaddr, 6)) {
76 bc->allocated = 1;
77 return bc;
78 }
79 }
80 return NULL;
81}
82
Jan Kiszka460fec62009-06-24 14:42:31 +020083static BOOTPClient *find_addr(Slirp *slirp, struct in_addr *paddr,
84 const uint8_t *macaddr)
bellard512176d2004-05-04 03:14:47 +000085{
86 BOOTPClient *bc;
87 int i;
88
Jan Kiszka460fec62009-06-24 14:42:31 +020089 for(i = 0; i < NB_BOOTP_CLIENTS; i++) {
90 if (!memcmp(macaddr, slirp->bootp_clients[i].macaddr, 6))
bellard512176d2004-05-04 03:14:47 +000091 goto found;
92 }
93 return NULL;
94 found:
Jan Kiszka460fec62009-06-24 14:42:31 +020095 bc = &slirp->bootp_clients[i];
bellard512176d2004-05-04 03:14:47 +000096 bc->allocated = 1;
Jan Kiszka460fec62009-06-24 14:42:31 +020097 paddr->s_addr = slirp->vdhcp_startaddr.s_addr + htonl(i);
bellard512176d2004-05-04 03:14:47 +000098 return bc;
99}
100
aliguorib63c7f62009-04-21 19:56:20 +0000101static void dhcp_decode(const struct bootp_t *bp, int *pmsg_type,
Aurelien Jarno8aaf42e2011-01-06 22:43:13 +0100102 struct in_addr *preq_addr)
bellardf0cbd3e2004-04-22 00:10:48 +0000103{
104 const uint8_t *p, *p_end;
105 int len, tag;
106
ths3b46e622007-09-17 08:09:54 +0000107 *pmsg_type = 0;
Aurelien Jarno8aaf42e2011-01-06 22:43:13 +0100108 preq_addr->s_addr = htonl(0L);
bellardf0cbd3e2004-04-22 00:10:48 +0000109
aliguorib63c7f62009-04-21 19:56:20 +0000110 p = bp->bp_vend;
111 p_end = p + DHCP_OPT_LEN;
bellardf0cbd3e2004-04-22 00:10:48 +0000112 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) {
ths5fafdf22007-09-16 21:08:06 +0000118 p++;
bellardf0cbd3e2004-04-22 00:10:48 +0000119 } else if (tag == RFC1533_END) {
120 break;
121 } else {
122 p++;
123 if (p >= p_end)
124 break;
125 len = *p++;
malcd0f2c4c2010-02-07 02:03:50 +0300126 DPRINTF("dhcp: tag=%d len=%d\n", tag, len);
bellardf0cbd3e2004-04-22 00:10:48 +0000127
128 switch(tag) {
129 case RFC2132_MSG_TYPE:
130 if (len >= 1)
131 *pmsg_type = p[0];
132 break;
aliguorib63c7f62009-04-21 19:56:20 +0000133 case RFC2132_REQ_ADDR:
Aurelien Jarno8aaf42e2011-01-06 22:43:13 +0100134 if (len >= 4) {
135 memcpy(&(preq_addr->s_addr), p, 4);
136 }
aliguorib63c7f62009-04-21 19:56:20 +0000137 break;
bellardf0cbd3e2004-04-22 00:10:48 +0000138 default:
139 break;
140 }
141 p += len;
142 }
143 }
Aurelien Jarno8aaf42e2011-01-06 22:43:13 +0100144 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);
aliguorib63c7f62009-04-21 19:56:20 +0000147 }
bellardf0cbd3e2004-04-22 00:10:48 +0000148}
149
Jan Kiszka460fec62009-06-24 14:42:31 +0200150static void bootp_reply(Slirp *slirp, const struct bootp_t *bp)
bellardf0cbd3e2004-04-22 00:10:48 +0000151{
aliguorib63c7f62009-04-21 19:56:20 +0000152 BOOTPClient *bc = NULL;
bellardf0cbd3e2004-04-22 00:10:48 +0000153 struct mbuf *m;
154 struct bootp_t *rbp;
155 struct sockaddr_in saddr, daddr;
Aurelien Jarno8aaf42e2011-01-06 22:43:13 +0100156 struct in_addr preq_addr;
bellardf0cbd3e2004-04-22 00:10:48 +0000157 int dhcp_msg_type, val;
158 uint8_t *q;
Fabien Chouteau1a0ca1e2011-08-03 12:52:54 +0200159 uint8_t client_ethaddr[ETH_ALEN];
bellardf0cbd3e2004-04-22 00:10:48 +0000160
161 /* extract exact DHCP msg type */
aliguorib63c7f62009-04-21 19:56:20 +0000162 dhcp_decode(bp, &dhcp_msg_type, &preq_addr);
malcd0f2c4c2010-02-07 02:03:50 +0300163 DPRINTF("bootp packet op=%d msgtype=%d", bp->bp_op, dhcp_msg_type);
Aurelien Jarno8aaf42e2011-01-06 22:43:13 +0100164 if (preq_addr.s_addr != htonl(0L))
Stefan Weilecc804c2015-08-29 09:12:35 +0200165 DPRINTF(" req_addr=%08" PRIx32 "\n", ntohl(preq_addr.s_addr));
aliguorib63c7f62009-04-21 19:56:20 +0000166 else
malcd0f2c4c2010-02-07 02:03:50 +0300167 DPRINTF("\n");
ths3b46e622007-09-17 08:09:54 +0000168
bellard487be8a2004-10-03 11:44:41 +0000169 if (dhcp_msg_type == 0)
170 dhcp_msg_type = DHCPREQUEST; /* Force reply for old BOOTP clients */
ths3b46e622007-09-17 08:09:54 +0000171
ths5fafdf22007-09-16 21:08:06 +0000172 if (dhcp_msg_type != DHCPDISCOVER &&
bellardf0cbd3e2004-04-22 00:10:48 +0000173 dhcp_msg_type != DHCPREQUEST)
174 return;
Fabien Chouteau1a0ca1e2011-08-03 12:52:54 +0200175
176 /* Get client's hardware address from bootp request */
177 memcpy(client_ethaddr, bp->bp_hwaddr, ETH_ALEN);
ths3b46e622007-09-17 08:09:54 +0000178
Jan Kiszka460fec62009-06-24 14:42:31 +0200179 m = m_get(slirp);
180 if (!m) {
bellardf0cbd3e2004-04-22 00:10:48 +0000181 return;
Jan Kiszka460fec62009-06-24 14:42:31 +0200182 }
blueswir19634d902007-10-26 19:01:16 +0000183 m->m_data += IF_MAXLINKHDR;
bellardf0cbd3e2004-04-22 00:10:48 +0000184 rbp = (struct bootp_t *)m->m_data;
185 m->m_data += sizeof(struct udpiphdr);
186 memset(rbp, 0, sizeof(struct bootp_t));
187
bellard512176d2004-05-04 03:14:47 +0000188 if (dhcp_msg_type == DHCPDISCOVER) {
Aurelien Jarno8aaf42e2011-01-06 22:43:13 +0100189 if (preq_addr.s_addr != htonl(0L)) {
Fabien Chouteau1a0ca1e2011-08-03 12:52:54 +0200190 bc = request_addr(slirp, &preq_addr, client_ethaddr);
aliguorib63c7f62009-04-21 19:56:20 +0000191 if (bc) {
Aurelien Jarno8aaf42e2011-01-06 22:43:13 +0100192 daddr.sin_addr = preq_addr;
aliguorib63c7f62009-04-21 19:56:20 +0000193 }
194 }
bellard512176d2004-05-04 03:14:47 +0000195 if (!bc) {
aliguorib63c7f62009-04-21 19:56:20 +0000196 new_addr:
Fabien Chouteau1a0ca1e2011-08-03 12:52:54 +0200197 bc = get_new_addr(slirp, &daddr.sin_addr, client_ethaddr);
aliguorib63c7f62009-04-21 19:56:20 +0000198 if (!bc) {
malcd0f2c4c2010-02-07 02:03:50 +0300199 DPRINTF("no address left\n");
aliguorib63c7f62009-04-21 19:56:20 +0000200 return;
201 }
bellard512176d2004-05-04 03:14:47 +0000202 }
Fabien Chouteau1a0ca1e2011-08-03 12:52:54 +0200203 memcpy(bc->macaddr, client_ethaddr, ETH_ALEN);
Aurelien Jarno8aaf42e2011-01-06 22:43:13 +0100204 } else if (preq_addr.s_addr != htonl(0L)) {
Fabien Chouteau1a0ca1e2011-08-03 12:52:54 +0200205 bc = request_addr(slirp, &preq_addr, client_ethaddr);
aliguorib63c7f62009-04-21 19:56:20 +0000206 if (bc) {
Aurelien Jarno8aaf42e2011-01-06 22:43:13 +0100207 daddr.sin_addr = preq_addr;
Fabien Chouteau1a0ca1e2011-08-03 12:52:54 +0200208 memcpy(bc->macaddr, client_ethaddr, ETH_ALEN);
aliguorib63c7f62009-04-21 19:56:20 +0000209 } else {
David Gibson90d74162012-02-24 01:23:28 +0100210 /* DHCPNAKs should be sent to broadcast */
211 daddr.sin_addr.s_addr = 0xffffffff;
aliguorib63c7f62009-04-21 19:56:20 +0000212 }
bellard512176d2004-05-04 03:14:47 +0000213 } else {
Jan Kiszka460fec62009-06-24 14:42:31 +0200214 bc = find_addr(slirp, &daddr.sin_addr, bp->bp_hwaddr);
bellard512176d2004-05-04 03:14:47 +0000215 if (!bc) {
bellardd981b882004-09-30 18:57:28 +0000216 /* if never assigned, behaves as if it was already
217 assigned (windows fix because it remembers its address) */
218 goto new_addr;
bellard512176d2004-05-04 03:14:47 +0000219 }
bellardf0cbd3e2004-04-22 00:10:48 +0000220 }
ths47d5d012007-02-20 00:05:08 +0000221
Fabien Chouteau1a0ca1e2011-08-03 12:52:54 +0200222 /* Update ARP table for this IP address */
223 arp_table_add(slirp, daddr.sin_addr.s_addr, client_ethaddr);
224
Jan Kiszka460fec62009-06-24 14:42:31 +0200225 saddr.sin_addr = slirp->vhost_addr;
bellardf0cbd3e2004-04-22 00:10:48 +0000226 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;
Fabien Chouteau1a0ca1e2011-08-03 12:52:54 +0200234 memcpy(rbp->bp_hwaddr, bp->bp_hwaddr, ETH_ALEN);
bellardf0cbd3e2004-04-22 00:10:48 +0000235
bellarde95c8d52004-09-30 22:22:08 +0000236 rbp->bp_yiaddr = daddr.sin_addr; /* Client IP address */
237 rbp->bp_siaddr = saddr.sin_addr; /* Server IP address */
bellardf0cbd3e2004-04-22 00:10:48 +0000238
239 q = rbp->bp_vend;
240 memcpy(q, rfc1533_cookie, 4);
241 q += 4;
242
aliguorib63c7f62009-04-21 19:56:20 +0000243 if (bc) {
Stefan Weilecc804c2015-08-29 09:12:35 +0200244 DPRINTF("%s addr=%08" PRIx32 "\n",
aliguorib63c7f62009-04-21 19:56:20 +0000245 (dhcp_msg_type == DHCPDISCOVER) ? "offered" : "ack'ed",
246 ntohl(daddr.sin_addr.s_addr));
ths3b46e622007-09-17 08:09:54 +0000247
aliguorib63c7f62009-04-21 19:56:20 +0000248 if (dhcp_msg_type == DHCPDISCOVER) {
249 *q++ = RFC2132_MSG_TYPE;
250 *q++ = 1;
251 *q++ = DHCPOFFER;
252 } else /* DHCPREQUEST */ {
253 *q++ = RFC2132_MSG_TYPE;
254 *q++ = 1;
255 *q++ = DHCPACK;
256 }
257
Jan Kiszka460fec62009-06-24 14:42:31 +0200258 if (slirp->bootp_filename)
aliguorib63c7f62009-04-21 19:56:20 +0000259 snprintf((char *)rbp->bp_file, sizeof(rbp->bp_file), "%s",
Jan Kiszka460fec62009-06-24 14:42:31 +0200260 slirp->bootp_filename);
aliguorib63c7f62009-04-21 19:56:20 +0000261
bellardf0cbd3e2004-04-22 00:10:48 +0000262 *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;
Jan Kiszka460fec62009-06-24 14:42:31 +0200269 memcpy(q, &slirp->vnetwork_mask, 4);
Jan Kiszkaa13a4122009-06-24 14:42:28 +0200270 q += 4;
ths3b46e622007-09-17 08:09:54 +0000271
Jan Kiszka460fec62009-06-24 14:42:31 +0200272 if (!slirp->restricted) {
aliguoria9ba3a82009-01-08 19:24:00 +0000273 *q++ = RFC1533_GATEWAY;
274 *q++ = 4;
275 memcpy(q, &saddr.sin_addr, 4);
276 q += 4;
ths3b46e622007-09-17 08:09:54 +0000277
aliguoria9ba3a82009-01-08 19:24:00 +0000278 *q++ = RFC1533_DNS;
279 *q++ = 4;
Jan Kiszka460fec62009-06-24 14:42:31 +0200280 memcpy(q, &slirp->vnameserver_addr, 4);
aliguoria9ba3a82009-01-08 19:24:00 +0000281 q += 4;
282 }
bellardf0cbd3e2004-04-22 00:10:48 +0000283
284 *q++ = RFC2132_LEASE_TIME;
285 *q++ = 4;
286 val = htonl(LEASE_TIME);
287 memcpy(q, &val, 4);
288 q += 4;
pbrook115defd2006-04-16 11:06:58 +0000289
Jan Kiszka460fec62009-06-24 14:42:31 +0200290 if (*slirp->client_hostname) {
291 val = strlen(slirp->client_hostname);
pbrook115defd2006-04-16 11:06:58 +0000292 *q++ = RFC1533_HOSTNAME;
293 *q++ = val;
Jan Kiszka460fec62009-06-24 14:42:31 +0200294 memcpy(q, slirp->client_hostname, val);
pbrook115defd2006-04-16 11:06:58 +0000295 q += val;
296 }
Klaus Stengel63d29602012-10-27 19:53:39 +0200297
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 }
aliguorib63c7f62009-04-21 19:56:20 +0000309 } else {
310 static const char nak_msg[] = "requested address not available";
311
Stefan Weilecc804c2015-08-29 09:12:35 +0200312 DPRINTF("nak'ed addr=%08" PRIx32 "\n", ntohl(preq_addr.s_addr));
aliguorib63c7f62009-04-21 19:56:20 +0000313
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;
bellardf0cbd3e2004-04-22 00:10:48 +0000322 }
Blue Swirl369c86e2010-03-07 13:45:37 +0000323 *q = RFC1533_END;
ths3b46e622007-09-17 08:09:54 +0000324
aliguorib63c7f62009-04-21 19:56:20 +0000325 daddr.sin_addr.s_addr = 0xffffffffu;
326
ths5fafdf22007-09-16 21:08:06 +0000327 m->m_len = sizeof(struct bootp_t) -
bellard44bbf732004-06-04 15:30:48 +0000328 sizeof(struct ip) - sizeof(struct udphdr);
Guillaume Subiron53792292015-12-19 22:24:59 +0100329 udp_output(NULL, m, &saddr, &daddr, IPTOS_LOWDELAY);
bellardf0cbd3e2004-04-22 00:10:48 +0000330}
331
332void bootp_input(struct mbuf *m)
333{
bellard101c5932005-06-05 17:11:42 +0000334 struct bootp_t *bp = mtod(m, struct bootp_t *);
bellardf0cbd3e2004-04-22 00:10:48 +0000335
336 if (bp->bp_op == BOOTP_REQUEST) {
Jan Kiszka460fec62009-06-24 14:42:31 +0200337 bootp_reply(m->slirp, bp);
bellardf0cbd3e2004-04-22 00:10:48 +0000338 }
339}