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 BOOTPClient bootp_clients[NB_ADDR];
41 static const uint8_t rfc1533_cookie[] = { RFC1533_COOKIE };
44 #define dprintf(fmt, args...) \
45 if (slirp_debug & DBG_CALL) { fprintf(dfd, fmt, ## args); fflush(dfd); }
47 #define dprintf(fmt, args...)
50 static BOOTPClient *get_new_addr(struct in_addr *paddr)
55 for(i = 0; i < NB_ADDR; i++) {
56 if (!bootp_clients[i].allocated)
61 bc = &bootp_clients[i];
63 paddr->s_addr = htonl(ntohl(special_addr.s_addr) | (i + START_ADDR));
67 static BOOTPClient *find_addr(struct in_addr *paddr, const uint8_t *macaddr)
72 for(i = 0; i < NB_ADDR; i++) {
73 if (!memcmp(macaddr, bootp_clients[i].macaddr, 6))
78 bc = &bootp_clients[i];
80 paddr->s_addr = htonl(ntohl(special_addr.s_addr) | (i + START_ADDR));
84 static void dhcp_decode(const uint8_t *buf, int size,
87 const uint8_t *p, *p_end;
96 if (memcmp(p, rfc1533_cookie, 4) != 0)
101 if (tag == RFC1533_PAD) {
103 } else if (tag == RFC1533_END) {
110 dprintf("dhcp: tag=0x%02x len=%d\n", tag, len);
113 case RFC2132_MSG_TYPE:
125 static void bootp_reply(struct bootp_t *bp)
130 struct sockaddr_in saddr, daddr;
131 struct in_addr dns_addr;
132 int dhcp_msg_type, val;
135 /* extract exact DHCP msg type */
136 dhcp_decode(bp->bp_vend, DHCP_OPT_LEN, &dhcp_msg_type);
137 dprintf("bootp packet op=%d msgtype=%d\n", bp->bp_op, dhcp_msg_type);
139 if (dhcp_msg_type == 0)
140 dhcp_msg_type = DHCPREQUEST; /* Force reply for old BOOTP clients */
142 if (dhcp_msg_type != DHCPDISCOVER &&
143 dhcp_msg_type != DHCPREQUEST)
145 /* XXX: this is a hack to get the client mac address */
146 memcpy(client_ethaddr, bp->bp_hwaddr, 6);
148 if ((m = m_get()) == NULL)
150 m->m_data += if_maxlinkhdr;
151 rbp = (struct bootp_t *)m->m_data;
152 m->m_data += sizeof(struct udpiphdr);
153 memset(rbp, 0, sizeof(struct bootp_t));
155 if (dhcp_msg_type == DHCPDISCOVER) {
157 bc = get_new_addr(&daddr.sin_addr);
159 dprintf("no address left\n");
162 memcpy(bc->macaddr, client_ethaddr, 6);
164 bc = find_addr(&daddr.sin_addr, bp->bp_hwaddr);
166 /* if never assigned, behaves as if it was already
167 assigned (windows fix because it remembers its address) */
171 dprintf("offered addr=%08x\n", ntohl(daddr.sin_addr.s_addr));
173 saddr.sin_addr.s_addr = htonl(ntohl(special_addr.s_addr) | CTL_ALIAS);
174 saddr.sin_port = htons(BOOTP_SERVER);
176 daddr.sin_port = htons(BOOTP_CLIENT);
178 rbp->bp_op = BOOTP_REPLY;
179 rbp->bp_xid = bp->bp_xid;
182 memcpy(rbp->bp_hwaddr, bp->bp_hwaddr, 6);
184 rbp->bp_yiaddr = daddr.sin_addr; /* Client IP address */
185 rbp->bp_siaddr = saddr.sin_addr; /* Server IP address */
188 memcpy(q, rfc1533_cookie, 4);
191 if (dhcp_msg_type == DHCPDISCOVER) {
192 *q++ = RFC2132_MSG_TYPE;
195 } else if (dhcp_msg_type == DHCPREQUEST) {
196 *q++ = RFC2132_MSG_TYPE;
201 if (dhcp_msg_type == DHCPDISCOVER ||
202 dhcp_msg_type == DHCPREQUEST) {
203 *q++ = RFC2132_SRV_ID;
205 memcpy(q, &saddr.sin_addr, 4);
208 *q++ = RFC1533_NETMASK;
215 *q++ = RFC1533_GATEWAY;
217 memcpy(q, &saddr.sin_addr, 4);
222 dns_addr.s_addr = htonl(ntohl(special_addr.s_addr) | CTL_DNS);
223 memcpy(q, &dns_addr, 4);
226 *q++ = RFC2132_LEASE_TIME;
228 val = htonl(LEASE_TIME);
234 m->m_len = sizeof(struct bootp_t) -
235 sizeof(struct ip) - sizeof(struct udphdr);
236 udp_output2(NULL, m, &saddr, &daddr, IPTOS_LOWDELAY);
239 void bootp_input(struct mbuf *m)
241 struct bootp_t *bp = mtod(m, struct bootp_t *);
243 if (bp->bp_op == BOOTP_REQUEST) {