2 * QEMU BOOTP/DHCP server
4 * Copyright (c) 2004 Fabrice Bellard
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:
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
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
26 /* XXX: only DHCP is supported */
32 #define LEASE_TIME (24 * 3600)
39 static BOOTPClient bootp_clients[NB_ADDR];
41 const char *bootp_filename;
43 static const uint8_t rfc1533_cookie[] = { RFC1533_COOKIE };
46 #define dprintf(fmt, args...) \
47 if (slirp_debug & DBG_CALL) { fprintf(dfd, fmt, ## args); fflush(dfd); }
49 #define dprintf(fmt, args...)
52 static BOOTPClient *get_new_addr(struct in_addr *paddr)
57 for(i = 0; i < NB_ADDR; i++) {
58 if (!bootp_clients[i].allocated)
63 bc = &bootp_clients[i];
65 paddr->s_addr = htonl(ntohl(special_addr.s_addr) | (i + START_ADDR));
69 static BOOTPClient *request_addr(const struct in_addr *paddr,
70 const uint8_t *macaddr)
72 uint32_t req_addr = ntohl(paddr->s_addr);
73 uint32_t spec_addr = ntohl(special_addr.s_addr);
76 if (req_addr >= (spec_addr | START_ADDR) &&
77 req_addr < (spec_addr | (NB_ADDR + START_ADDR))) {
78 bc = &bootp_clients[(req_addr & 0xff) - START_ADDR];
79 if (!bc->allocated || !memcmp(macaddr, bc->macaddr, 6)) {
87 static BOOTPClient *find_addr(struct in_addr *paddr, const uint8_t *macaddr)
92 for(i = 0; i < NB_ADDR; i++) {
93 if (!memcmp(macaddr, bootp_clients[i].macaddr, 6))
98 bc = &bootp_clients[i];
100 paddr->s_addr = htonl(ntohl(special_addr.s_addr) | (i + START_ADDR));
104 static void dhcp_decode(const struct bootp_t *bp, int *pmsg_type,
105 const struct in_addr **preq_addr)
107 const uint8_t *p, *p_end;
114 p_end = p + DHCP_OPT_LEN;
115 if (memcmp(p, rfc1533_cookie, 4) != 0)
120 if (tag == RFC1533_PAD) {
122 } else if (tag == RFC1533_END) {
129 dprintf("dhcp: tag=%d len=%d\n", tag, len);
132 case RFC2132_MSG_TYPE:
136 case RFC2132_REQ_ADDR:
138 *preq_addr = (struct in_addr *)p;
146 if (*pmsg_type == DHCPREQUEST && !*preq_addr && bp->bp_ciaddr.s_addr) {
147 *preq_addr = &bp->bp_ciaddr;
151 static void bootp_reply(const struct bootp_t *bp)
153 BOOTPClient *bc = NULL;
156 struct sockaddr_in saddr, daddr;
157 struct in_addr dns_addr;
158 const struct in_addr *preq_addr;
159 int dhcp_msg_type, val;
162 /* extract exact DHCP msg type */
163 dhcp_decode(bp, &dhcp_msg_type, &preq_addr);
164 dprintf("bootp packet op=%d msgtype=%d", bp->bp_op, dhcp_msg_type);
166 dprintf(" req_addr=%08x\n", ntohl(preq_addr->s_addr));
170 if (dhcp_msg_type == 0)
171 dhcp_msg_type = DHCPREQUEST; /* Force reply for old BOOTP clients */
173 if (dhcp_msg_type != DHCPDISCOVER &&
174 dhcp_msg_type != DHCPREQUEST)
176 /* XXX: this is a hack to get the client mac address */
177 memcpy(client_ethaddr, bp->bp_hwaddr, 6);
179 if ((m = m_get()) == NULL)
181 m->m_data += IF_MAXLINKHDR;
182 rbp = (struct bootp_t *)m->m_data;
183 m->m_data += sizeof(struct udpiphdr);
184 memset(rbp, 0, sizeof(struct bootp_t));
186 if (dhcp_msg_type == DHCPDISCOVER) {
188 bc = request_addr(preq_addr, client_ethaddr);
190 daddr.sin_addr = *preq_addr;
195 bc = get_new_addr(&daddr.sin_addr);
197 dprintf("no address left\n");
201 memcpy(bc->macaddr, client_ethaddr, 6);
202 } else if (preq_addr) {
203 bc = request_addr(preq_addr, client_ethaddr);
205 daddr.sin_addr = *preq_addr;
206 memcpy(bc->macaddr, client_ethaddr, 6);
208 daddr.sin_addr.s_addr = 0;
211 bc = find_addr(&daddr.sin_addr, bp->bp_hwaddr);
213 /* if never assigned, behaves as if it was already
214 assigned (windows fix because it remembers its address) */
219 saddr.sin_addr.s_addr = htonl(ntohl(special_addr.s_addr) | CTL_ALIAS);
220 saddr.sin_port = htons(BOOTP_SERVER);
222 daddr.sin_port = htons(BOOTP_CLIENT);
224 rbp->bp_op = BOOTP_REPLY;
225 rbp->bp_xid = bp->bp_xid;
228 memcpy(rbp->bp_hwaddr, bp->bp_hwaddr, 6);
230 rbp->bp_yiaddr = daddr.sin_addr; /* Client IP address */
231 rbp->bp_siaddr = saddr.sin_addr; /* Server IP address */
234 memcpy(q, rfc1533_cookie, 4);
238 dprintf("%s addr=%08x\n",
239 (dhcp_msg_type == DHCPDISCOVER) ? "offered" : "ack'ed",
240 ntohl(daddr.sin_addr.s_addr));
242 if (dhcp_msg_type == DHCPDISCOVER) {
243 *q++ = RFC2132_MSG_TYPE;
246 } else /* DHCPREQUEST */ {
247 *q++ = RFC2132_MSG_TYPE;
253 snprintf((char *)rbp->bp_file, sizeof(rbp->bp_file), "%s",
256 *q++ = RFC2132_SRV_ID;
258 memcpy(q, &saddr.sin_addr, 4);
261 *q++ = RFC1533_NETMASK;
268 if (!slirp_restrict) {
269 *q++ = RFC1533_GATEWAY;
271 memcpy(q, &saddr.sin_addr, 4);
276 dns_addr.s_addr = htonl(ntohl(special_addr.s_addr) | CTL_DNS);
277 memcpy(q, &dns_addr, 4);
281 *q++ = RFC2132_LEASE_TIME;
283 val = htonl(LEASE_TIME);
287 if (*slirp_hostname) {
288 val = strlen(slirp_hostname);
289 *q++ = RFC1533_HOSTNAME;
291 memcpy(q, slirp_hostname, val);
295 static const char nak_msg[] = "requested address not available";
297 dprintf("nak'ed addr=%08x\n", ntohl(preq_addr->s_addr));
299 *q++ = RFC2132_MSG_TYPE;
303 *q++ = RFC2132_MESSAGE;
304 *q++ = sizeof(nak_msg) - 1;
305 memcpy(q, nak_msg, sizeof(nak_msg) - 1);
306 q += sizeof(nak_msg) - 1;
310 daddr.sin_addr.s_addr = 0xffffffffu;
312 m->m_len = sizeof(struct bootp_t) -
313 sizeof(struct ip) - sizeof(struct udphdr);
314 udp_output2(NULL, m, &saddr, &daddr, IPTOS_LOWDELAY);
317 void bootp_input(struct mbuf *m)
319 struct bootp_t *bp = mtod(m, struct bootp_t *);
321 if (bp->bp_op == BOOTP_REQUEST) {