blob: 41460ffaa59d457052dbe8a88b21129d0b5b1432 [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 */
24#include <slirp.h>
25
26/* XXX: only DHCP is supported */
27
bellardf0cbd3e2004-04-22 00:10:48 +000028#define LEASE_TIME (24 * 3600)
29
bellardf0cbd3e2004-04-22 00:10:48 +000030static const uint8_t rfc1533_cookie[] = { RFC1533_COOKIE };
31
32#ifdef DEBUG
malcd0f2c4c2010-02-07 02:03:50 +030033#define DPRINTF(fmt, ...) \
Jan Kiszka9f349492009-06-24 14:42:29 +020034do if (slirp_debug & DBG_CALL) { fprintf(dfd, fmt, ## __VA_ARGS__); fflush(dfd); } while (0)
bellardf0cbd3e2004-04-22 00:10:48 +000035#else
Jes Sorensen7390cdf2010-08-31 09:30:37 +020036#define DPRINTF(fmt, ...) do{}while(0)
bellardf0cbd3e2004-04-22 00:10:48 +000037#endif
38
Jan Kiszka460fec62009-06-24 14:42:31 +020039static BOOTPClient *get_new_addr(Slirp *slirp, struct in_addr *paddr,
Jan Kiszka0928a952009-05-21 22:43:39 +020040 const uint8_t *macaddr)
bellardf0cbd3e2004-04-22 00:10:48 +000041{
42 BOOTPClient *bc;
43 int i;
44
Jan Kiszka460fec62009-06-24 14:42:31 +020045 for(i = 0; i < NB_BOOTP_CLIENTS; i++) {
46 bc = &slirp->bootp_clients[i];
Jan Kiszka0928a952009-05-21 22:43:39 +020047 if (!bc->allocated || !memcmp(macaddr, bc->macaddr, 6))
bellardf0cbd3e2004-04-22 00:10:48 +000048 goto found;
49 }
50 return NULL;
51 found:
Jan Kiszka460fec62009-06-24 14:42:31 +020052 bc = &slirp->bootp_clients[i];
bellardf0cbd3e2004-04-22 00:10:48 +000053 bc->allocated = 1;
Jan Kiszka460fec62009-06-24 14:42:31 +020054 paddr->s_addr = slirp->vdhcp_startaddr.s_addr + htonl(i);
bellardf0cbd3e2004-04-22 00:10:48 +000055 return bc;
56}
57
Jan Kiszka460fec62009-06-24 14:42:31 +020058static BOOTPClient *request_addr(Slirp *slirp, const struct in_addr *paddr,
aliguorib63c7f62009-04-21 19:56:20 +000059 const uint8_t *macaddr)
60{
61 uint32_t req_addr = ntohl(paddr->s_addr);
Jan Kiszka460fec62009-06-24 14:42:31 +020062 uint32_t dhcp_addr = ntohl(slirp->vdhcp_startaddr.s_addr);
aliguorib63c7f62009-04-21 19:56:20 +000063 BOOTPClient *bc;
64
Jan Kiszkaa13a4122009-06-24 14:42:28 +020065 if (req_addr >= dhcp_addr &&
Jan Kiszka460fec62009-06-24 14:42:31 +020066 req_addr < (dhcp_addr + NB_BOOTP_CLIENTS)) {
67 bc = &slirp->bootp_clients[req_addr - dhcp_addr];
aliguorib63c7f62009-04-21 19:56:20 +000068 if (!bc->allocated || !memcmp(macaddr, bc->macaddr, 6)) {
69 bc->allocated = 1;
70 return bc;
71 }
72 }
73 return NULL;
74}
75
Jan Kiszka460fec62009-06-24 14:42:31 +020076static BOOTPClient *find_addr(Slirp *slirp, struct in_addr *paddr,
77 const uint8_t *macaddr)
bellard512176d2004-05-04 03:14:47 +000078{
79 BOOTPClient *bc;
80 int i;
81
Jan Kiszka460fec62009-06-24 14:42:31 +020082 for(i = 0; i < NB_BOOTP_CLIENTS; i++) {
83 if (!memcmp(macaddr, slirp->bootp_clients[i].macaddr, 6))
bellard512176d2004-05-04 03:14:47 +000084 goto found;
85 }
86 return NULL;
87 found:
Jan Kiszka460fec62009-06-24 14:42:31 +020088 bc = &slirp->bootp_clients[i];
bellard512176d2004-05-04 03:14:47 +000089 bc->allocated = 1;
Jan Kiszka460fec62009-06-24 14:42:31 +020090 paddr->s_addr = slirp->vdhcp_startaddr.s_addr + htonl(i);
bellard512176d2004-05-04 03:14:47 +000091 return bc;
92}
93
aliguorib63c7f62009-04-21 19:56:20 +000094static void dhcp_decode(const struct bootp_t *bp, int *pmsg_type,
95 const struct in_addr **preq_addr)
bellardf0cbd3e2004-04-22 00:10:48 +000096{
97 const uint8_t *p, *p_end;
98 int len, tag;
99
ths3b46e622007-09-17 08:09:54 +0000100 *pmsg_type = 0;
aliguorib63c7f62009-04-21 19:56:20 +0000101 *preq_addr = NULL;
bellardf0cbd3e2004-04-22 00:10:48 +0000102
aliguorib63c7f62009-04-21 19:56:20 +0000103 p = bp->bp_vend;
104 p_end = p + DHCP_OPT_LEN;
bellardf0cbd3e2004-04-22 00:10:48 +0000105 if (memcmp(p, rfc1533_cookie, 4) != 0)
106 return;
107 p += 4;
108 while (p < p_end) {
109 tag = p[0];
110 if (tag == RFC1533_PAD) {
ths5fafdf22007-09-16 21:08:06 +0000111 p++;
bellardf0cbd3e2004-04-22 00:10:48 +0000112 } else if (tag == RFC1533_END) {
113 break;
114 } else {
115 p++;
116 if (p >= p_end)
117 break;
118 len = *p++;
malcd0f2c4c2010-02-07 02:03:50 +0300119 DPRINTF("dhcp: tag=%d len=%d\n", tag, len);
bellardf0cbd3e2004-04-22 00:10:48 +0000120
121 switch(tag) {
122 case RFC2132_MSG_TYPE:
123 if (len >= 1)
124 *pmsg_type = p[0];
125 break;
aliguorib63c7f62009-04-21 19:56:20 +0000126 case RFC2132_REQ_ADDR:
127 if (len >= 4)
128 *preq_addr = (struct in_addr *)p;
129 break;
bellardf0cbd3e2004-04-22 00:10:48 +0000130 default:
131 break;
132 }
133 p += len;
134 }
135 }
aliguorib63c7f62009-04-21 19:56:20 +0000136 if (*pmsg_type == DHCPREQUEST && !*preq_addr && bp->bp_ciaddr.s_addr) {
137 *preq_addr = &bp->bp_ciaddr;
138 }
bellardf0cbd3e2004-04-22 00:10:48 +0000139}
140
Jan Kiszka460fec62009-06-24 14:42:31 +0200141static void bootp_reply(Slirp *slirp, const struct bootp_t *bp)
bellardf0cbd3e2004-04-22 00:10:48 +0000142{
aliguorib63c7f62009-04-21 19:56:20 +0000143 BOOTPClient *bc = NULL;
bellardf0cbd3e2004-04-22 00:10:48 +0000144 struct mbuf *m;
145 struct bootp_t *rbp;
146 struct sockaddr_in saddr, daddr;
aliguorib63c7f62009-04-21 19:56:20 +0000147 const struct in_addr *preq_addr;
bellardf0cbd3e2004-04-22 00:10:48 +0000148 int dhcp_msg_type, val;
149 uint8_t *q;
150
151 /* extract exact DHCP msg type */
aliguorib63c7f62009-04-21 19:56:20 +0000152 dhcp_decode(bp, &dhcp_msg_type, &preq_addr);
malcd0f2c4c2010-02-07 02:03:50 +0300153 DPRINTF("bootp packet op=%d msgtype=%d", bp->bp_op, dhcp_msg_type);
aliguorib63c7f62009-04-21 19:56:20 +0000154 if (preq_addr)
malcd0f2c4c2010-02-07 02:03:50 +0300155 DPRINTF(" req_addr=%08x\n", ntohl(preq_addr->s_addr));
aliguorib63c7f62009-04-21 19:56:20 +0000156 else
malcd0f2c4c2010-02-07 02:03:50 +0300157 DPRINTF("\n");
ths3b46e622007-09-17 08:09:54 +0000158
bellard487be8a2004-10-03 11:44:41 +0000159 if (dhcp_msg_type == 0)
160 dhcp_msg_type = DHCPREQUEST; /* Force reply for old BOOTP clients */
ths3b46e622007-09-17 08:09:54 +0000161
ths5fafdf22007-09-16 21:08:06 +0000162 if (dhcp_msg_type != DHCPDISCOVER &&
bellardf0cbd3e2004-04-22 00:10:48 +0000163 dhcp_msg_type != DHCPREQUEST)
164 return;
165 /* XXX: this is a hack to get the client mac address */
Jan Kiszka460fec62009-06-24 14:42:31 +0200166 memcpy(slirp->client_ethaddr, bp->bp_hwaddr, 6);
ths3b46e622007-09-17 08:09:54 +0000167
Jan Kiszka460fec62009-06-24 14:42:31 +0200168 m = m_get(slirp);
169 if (!m) {
bellardf0cbd3e2004-04-22 00:10:48 +0000170 return;
Jan Kiszka460fec62009-06-24 14:42:31 +0200171 }
blueswir19634d902007-10-26 19:01:16 +0000172 m->m_data += IF_MAXLINKHDR;
bellardf0cbd3e2004-04-22 00:10:48 +0000173 rbp = (struct bootp_t *)m->m_data;
174 m->m_data += sizeof(struct udpiphdr);
175 memset(rbp, 0, sizeof(struct bootp_t));
176
bellard512176d2004-05-04 03:14:47 +0000177 if (dhcp_msg_type == DHCPDISCOVER) {
aliguorib63c7f62009-04-21 19:56:20 +0000178 if (preq_addr) {
Jan Kiszka460fec62009-06-24 14:42:31 +0200179 bc = request_addr(slirp, preq_addr, slirp->client_ethaddr);
aliguorib63c7f62009-04-21 19:56:20 +0000180 if (bc) {
181 daddr.sin_addr = *preq_addr;
182 }
183 }
bellard512176d2004-05-04 03:14:47 +0000184 if (!bc) {
aliguorib63c7f62009-04-21 19:56:20 +0000185 new_addr:
Jan Kiszka460fec62009-06-24 14:42:31 +0200186 bc = get_new_addr(slirp, &daddr.sin_addr, slirp->client_ethaddr);
aliguorib63c7f62009-04-21 19:56:20 +0000187 if (!bc) {
malcd0f2c4c2010-02-07 02:03:50 +0300188 DPRINTF("no address left\n");
aliguorib63c7f62009-04-21 19:56:20 +0000189 return;
190 }
bellard512176d2004-05-04 03:14:47 +0000191 }
Jan Kiszka460fec62009-06-24 14:42:31 +0200192 memcpy(bc->macaddr, slirp->client_ethaddr, 6);
aliguorib63c7f62009-04-21 19:56:20 +0000193 } else if (preq_addr) {
Jan Kiszka460fec62009-06-24 14:42:31 +0200194 bc = request_addr(slirp, preq_addr, slirp->client_ethaddr);
aliguorib63c7f62009-04-21 19:56:20 +0000195 if (bc) {
196 daddr.sin_addr = *preq_addr;
Jan Kiszka460fec62009-06-24 14:42:31 +0200197 memcpy(bc->macaddr, slirp->client_ethaddr, 6);
aliguorib63c7f62009-04-21 19:56:20 +0000198 } else {
199 daddr.sin_addr.s_addr = 0;
200 }
bellard512176d2004-05-04 03:14:47 +0000201 } else {
Jan Kiszka460fec62009-06-24 14:42:31 +0200202 bc = find_addr(slirp, &daddr.sin_addr, bp->bp_hwaddr);
bellard512176d2004-05-04 03:14:47 +0000203 if (!bc) {
bellardd981b882004-09-30 18:57:28 +0000204 /* if never assigned, behaves as if it was already
205 assigned (windows fix because it remembers its address) */
206 goto new_addr;
bellard512176d2004-05-04 03:14:47 +0000207 }
bellardf0cbd3e2004-04-22 00:10:48 +0000208 }
ths47d5d012007-02-20 00:05:08 +0000209
Jan Kiszka460fec62009-06-24 14:42:31 +0200210 saddr.sin_addr = slirp->vhost_addr;
bellardf0cbd3e2004-04-22 00:10:48 +0000211 saddr.sin_port = htons(BOOTP_SERVER);
212
213 daddr.sin_port = htons(BOOTP_CLIENT);
214
215 rbp->bp_op = BOOTP_REPLY;
216 rbp->bp_xid = bp->bp_xid;
217 rbp->bp_htype = 1;
218 rbp->bp_hlen = 6;
219 memcpy(rbp->bp_hwaddr, bp->bp_hwaddr, 6);
220
bellarde95c8d52004-09-30 22:22:08 +0000221 rbp->bp_yiaddr = daddr.sin_addr; /* Client IP address */
222 rbp->bp_siaddr = saddr.sin_addr; /* Server IP address */
bellardf0cbd3e2004-04-22 00:10:48 +0000223
224 q = rbp->bp_vend;
225 memcpy(q, rfc1533_cookie, 4);
226 q += 4;
227
aliguorib63c7f62009-04-21 19:56:20 +0000228 if (bc) {
malcd0f2c4c2010-02-07 02:03:50 +0300229 DPRINTF("%s addr=%08x\n",
aliguorib63c7f62009-04-21 19:56:20 +0000230 (dhcp_msg_type == DHCPDISCOVER) ? "offered" : "ack'ed",
231 ntohl(daddr.sin_addr.s_addr));
ths3b46e622007-09-17 08:09:54 +0000232
aliguorib63c7f62009-04-21 19:56:20 +0000233 if (dhcp_msg_type == DHCPDISCOVER) {
234 *q++ = RFC2132_MSG_TYPE;
235 *q++ = 1;
236 *q++ = DHCPOFFER;
237 } else /* DHCPREQUEST */ {
238 *q++ = RFC2132_MSG_TYPE;
239 *q++ = 1;
240 *q++ = DHCPACK;
241 }
242
Jan Kiszka460fec62009-06-24 14:42:31 +0200243 if (slirp->bootp_filename)
aliguorib63c7f62009-04-21 19:56:20 +0000244 snprintf((char *)rbp->bp_file, sizeof(rbp->bp_file), "%s",
Jan Kiszka460fec62009-06-24 14:42:31 +0200245 slirp->bootp_filename);
aliguorib63c7f62009-04-21 19:56:20 +0000246
bellardf0cbd3e2004-04-22 00:10:48 +0000247 *q++ = RFC2132_SRV_ID;
248 *q++ = 4;
249 memcpy(q, &saddr.sin_addr, 4);
250 q += 4;
251
252 *q++ = RFC1533_NETMASK;
253 *q++ = 4;
Jan Kiszka460fec62009-06-24 14:42:31 +0200254 memcpy(q, &slirp->vnetwork_mask, 4);
Jan Kiszkaa13a4122009-06-24 14:42:28 +0200255 q += 4;
ths3b46e622007-09-17 08:09:54 +0000256
Jan Kiszka460fec62009-06-24 14:42:31 +0200257 if (!slirp->restricted) {
aliguoria9ba3a82009-01-08 19:24:00 +0000258 *q++ = RFC1533_GATEWAY;
259 *q++ = 4;
260 memcpy(q, &saddr.sin_addr, 4);
261 q += 4;
ths3b46e622007-09-17 08:09:54 +0000262
aliguoria9ba3a82009-01-08 19:24:00 +0000263 *q++ = RFC1533_DNS;
264 *q++ = 4;
Jan Kiszka460fec62009-06-24 14:42:31 +0200265 memcpy(q, &slirp->vnameserver_addr, 4);
aliguoria9ba3a82009-01-08 19:24:00 +0000266 q += 4;
267 }
bellardf0cbd3e2004-04-22 00:10:48 +0000268
269 *q++ = RFC2132_LEASE_TIME;
270 *q++ = 4;
271 val = htonl(LEASE_TIME);
272 memcpy(q, &val, 4);
273 q += 4;
pbrook115defd2006-04-16 11:06:58 +0000274
Jan Kiszka460fec62009-06-24 14:42:31 +0200275 if (*slirp->client_hostname) {
276 val = strlen(slirp->client_hostname);
pbrook115defd2006-04-16 11:06:58 +0000277 *q++ = RFC1533_HOSTNAME;
278 *q++ = val;
Jan Kiszka460fec62009-06-24 14:42:31 +0200279 memcpy(q, slirp->client_hostname, val);
pbrook115defd2006-04-16 11:06:58 +0000280 q += val;
281 }
aliguorib63c7f62009-04-21 19:56:20 +0000282 } else {
283 static const char nak_msg[] = "requested address not available";
284
malcd0f2c4c2010-02-07 02:03:50 +0300285 DPRINTF("nak'ed addr=%08x\n", ntohl(preq_addr->s_addr));
aliguorib63c7f62009-04-21 19:56:20 +0000286
287 *q++ = RFC2132_MSG_TYPE;
288 *q++ = 1;
289 *q++ = DHCPNAK;
290
291 *q++ = RFC2132_MESSAGE;
292 *q++ = sizeof(nak_msg) - 1;
293 memcpy(q, nak_msg, sizeof(nak_msg) - 1);
294 q += sizeof(nak_msg) - 1;
bellardf0cbd3e2004-04-22 00:10:48 +0000295 }
Blue Swirl369c86e2010-03-07 13:45:37 +0000296 *q = RFC1533_END;
ths3b46e622007-09-17 08:09:54 +0000297
aliguorib63c7f62009-04-21 19:56:20 +0000298 daddr.sin_addr.s_addr = 0xffffffffu;
299
ths5fafdf22007-09-16 21:08:06 +0000300 m->m_len = sizeof(struct bootp_t) -
bellard44bbf732004-06-04 15:30:48 +0000301 sizeof(struct ip) - sizeof(struct udphdr);
bellardf0cbd3e2004-04-22 00:10:48 +0000302 udp_output2(NULL, m, &saddr, &daddr, IPTOS_LOWDELAY);
303}
304
305void bootp_input(struct mbuf *m)
306{
bellard101c5932005-06-05 17:11:42 +0000307 struct bootp_t *bp = mtod(m, struct bootp_t *);
bellardf0cbd3e2004-04-22 00:10:48 +0000308
309 if (bp->bp_op == BOOTP_REQUEST) {
Jan Kiszka460fec62009-06-24 14:42:31 +0200310 bootp_reply(m->slirp, bp);
bellardf0cbd3e2004-04-22 00:10:48 +0000311 }
312}