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
27 /* Windows ntohl() returns an u_long value.
28 * Add a type cast to match the format strings. */
29 # define ntohl(n) ((uint32_t)ntohl(n))
32 /* XXX: only DHCP is supported */
34 #define LEASE_TIME (24 * 3600)
36 static const uint8_t rfc1533_cookie
[] = { RFC1533_COOKIE
};
39 #define DPRINTF(fmt, ...) \
40 do if (slirp_debug & DBG_CALL) { fprintf(dfd, fmt, ## __VA_ARGS__); fflush(dfd); } while (0)
42 #define DPRINTF(fmt, ...) do{}while(0)
45 static BOOTPClient
*get_new_addr(Slirp
*slirp
, struct in_addr
*paddr
,
46 const uint8_t *macaddr
)
51 for(i
= 0; i
< NB_BOOTP_CLIENTS
; i
++) {
52 bc
= &slirp
->bootp_clients
[i
];
53 if (!bc
->allocated
|| !memcmp(macaddr
, bc
->macaddr
, 6))
58 bc
= &slirp
->bootp_clients
[i
];
60 paddr
->s_addr
= slirp
->vdhcp_startaddr
.s_addr
+ htonl(i
);
64 static BOOTPClient
*request_addr(Slirp
*slirp
, const struct in_addr
*paddr
,
65 const uint8_t *macaddr
)
67 uint32_t req_addr
= ntohl(paddr
->s_addr
);
68 uint32_t dhcp_addr
= ntohl(slirp
->vdhcp_startaddr
.s_addr
);
71 if (req_addr
>= dhcp_addr
&&
72 req_addr
< (dhcp_addr
+ NB_BOOTP_CLIENTS
)) {
73 bc
= &slirp
->bootp_clients
[req_addr
- dhcp_addr
];
74 if (!bc
->allocated
|| !memcmp(macaddr
, bc
->macaddr
, 6)) {
82 static BOOTPClient
*find_addr(Slirp
*slirp
, struct in_addr
*paddr
,
83 const uint8_t *macaddr
)
88 for(i
= 0; i
< NB_BOOTP_CLIENTS
; i
++) {
89 if (!memcmp(macaddr
, slirp
->bootp_clients
[i
].macaddr
, 6))
94 bc
= &slirp
->bootp_clients
[i
];
96 paddr
->s_addr
= slirp
->vdhcp_startaddr
.s_addr
+ htonl(i
);
100 static void dhcp_decode(const struct bootp_t
*bp
, int *pmsg_type
,
101 struct in_addr
*preq_addr
)
103 const uint8_t *p
, *p_end
;
107 preq_addr
->s_addr
= htonl(0L);
110 p_end
= p
+ DHCP_OPT_LEN
;
111 if (memcmp(p
, rfc1533_cookie
, 4) != 0)
116 if (tag
== RFC1533_PAD
) {
118 } else if (tag
== RFC1533_END
) {
125 DPRINTF("dhcp: tag=%d len=%d\n", tag
, len
);
128 case RFC2132_MSG_TYPE
:
132 case RFC2132_REQ_ADDR
:
134 memcpy(&(preq_addr
->s_addr
), p
, 4);
143 if (*pmsg_type
== DHCPREQUEST
&& preq_addr
->s_addr
== htonl(0L) &&
144 bp
->bp_ciaddr
.s_addr
) {
145 memcpy(&(preq_addr
->s_addr
), &bp
->bp_ciaddr
, 4);
149 static void bootp_reply(Slirp
*slirp
, const struct bootp_t
*bp
)
151 BOOTPClient
*bc
= NULL
;
154 struct sockaddr_in saddr
, daddr
;
155 struct in_addr preq_addr
;
156 int dhcp_msg_type
, val
;
158 uint8_t client_ethaddr
[ETH_ALEN
];
160 /* extract exact DHCP msg type */
161 dhcp_decode(bp
, &dhcp_msg_type
, &preq_addr
);
162 DPRINTF("bootp packet op=%d msgtype=%d", bp
->bp_op
, dhcp_msg_type
);
163 if (preq_addr
.s_addr
!= htonl(0L))
164 DPRINTF(" req_addr=%08" PRIx32
"\n", ntohl(preq_addr
.s_addr
));
168 if (dhcp_msg_type
== 0)
169 dhcp_msg_type
= DHCPREQUEST
; /* Force reply for old BOOTP clients */
171 if (dhcp_msg_type
!= DHCPDISCOVER
&&
172 dhcp_msg_type
!= DHCPREQUEST
)
175 /* Get client's hardware address from bootp request */
176 memcpy(client_ethaddr
, bp
->bp_hwaddr
, ETH_ALEN
);
182 m
->m_data
+= IF_MAXLINKHDR
;
183 rbp
= (struct bootp_t
*)m
->m_data
;
184 m
->m_data
+= sizeof(struct udpiphdr
);
185 memset(rbp
, 0, sizeof(struct bootp_t
));
187 if (dhcp_msg_type
== DHCPDISCOVER
) {
188 if (preq_addr
.s_addr
!= htonl(0L)) {
189 bc
= request_addr(slirp
, &preq_addr
, client_ethaddr
);
191 daddr
.sin_addr
= preq_addr
;
196 bc
= get_new_addr(slirp
, &daddr
.sin_addr
, client_ethaddr
);
198 DPRINTF("no address left\n");
202 memcpy(bc
->macaddr
, client_ethaddr
, ETH_ALEN
);
203 } else if (preq_addr
.s_addr
!= htonl(0L)) {
204 bc
= request_addr(slirp
, &preq_addr
, client_ethaddr
);
206 daddr
.sin_addr
= preq_addr
;
207 memcpy(bc
->macaddr
, client_ethaddr
, ETH_ALEN
);
209 /* DHCPNAKs should be sent to broadcast */
210 daddr
.sin_addr
.s_addr
= 0xffffffff;
213 bc
= find_addr(slirp
, &daddr
.sin_addr
, bp
->bp_hwaddr
);
215 /* if never assigned, behaves as if it was already
216 assigned (windows fix because it remembers its address) */
221 /* Update ARP table for this IP address */
222 arp_table_add(slirp
, daddr
.sin_addr
.s_addr
, client_ethaddr
);
224 saddr
.sin_addr
= slirp
->vhost_addr
;
225 saddr
.sin_port
= htons(BOOTP_SERVER
);
227 daddr
.sin_port
= htons(BOOTP_CLIENT
);
229 rbp
->bp_op
= BOOTP_REPLY
;
230 rbp
->bp_xid
= bp
->bp_xid
;
233 memcpy(rbp
->bp_hwaddr
, bp
->bp_hwaddr
, ETH_ALEN
);
235 rbp
->bp_yiaddr
= daddr
.sin_addr
; /* Client IP address */
236 rbp
->bp_siaddr
= saddr
.sin_addr
; /* Server IP address */
239 memcpy(q
, rfc1533_cookie
, 4);
243 DPRINTF("%s addr=%08" PRIx32
"\n",
244 (dhcp_msg_type
== DHCPDISCOVER
) ? "offered" : "ack'ed",
245 ntohl(daddr
.sin_addr
.s_addr
));
247 if (dhcp_msg_type
== DHCPDISCOVER
) {
248 *q
++ = RFC2132_MSG_TYPE
;
251 } else /* DHCPREQUEST */ {
252 *q
++ = RFC2132_MSG_TYPE
;
257 if (slirp
->bootp_filename
)
258 snprintf((char *)rbp
->bp_file
, sizeof(rbp
->bp_file
), "%s",
259 slirp
->bootp_filename
);
261 *q
++ = RFC2132_SRV_ID
;
263 memcpy(q
, &saddr
.sin_addr
, 4);
266 *q
++ = RFC1533_NETMASK
;
268 memcpy(q
, &slirp
->vnetwork_mask
, 4);
271 if (!slirp
->restricted
) {
272 *q
++ = RFC1533_GATEWAY
;
274 memcpy(q
, &saddr
.sin_addr
, 4);
279 memcpy(q
, &slirp
->vnameserver_addr
, 4);
283 *q
++ = RFC2132_LEASE_TIME
;
285 val
= htonl(LEASE_TIME
);
289 if (*slirp
->client_hostname
) {
290 val
= strlen(slirp
->client_hostname
);
291 *q
++ = RFC1533_HOSTNAME
;
293 memcpy(q
, slirp
->client_hostname
, val
);
297 if (slirp
->vdnssearch
) {
298 size_t spaceleft
= sizeof(rbp
->bp_vend
) - (q
- rbp
->bp_vend
);
299 val
= slirp
->vdnssearch_len
;
300 if (val
+ 1 > spaceleft
) {
301 g_warning("DHCP packet size exceeded, "
302 "omitting domain-search option.");
304 memcpy(q
, slirp
->vdnssearch
, val
);
309 static const char nak_msg
[] = "requested address not available";
311 DPRINTF("nak'ed addr=%08" PRIx32
"\n", ntohl(preq_addr
.s_addr
));
313 *q
++ = RFC2132_MSG_TYPE
;
317 *q
++ = RFC2132_MESSAGE
;
318 *q
++ = sizeof(nak_msg
) - 1;
319 memcpy(q
, nak_msg
, sizeof(nak_msg
) - 1);
320 q
+= sizeof(nak_msg
) - 1;
324 daddr
.sin_addr
.s_addr
= 0xffffffffu
;
326 m
->m_len
= sizeof(struct bootp_t
) -
327 sizeof(struct ip
) - sizeof(struct udphdr
);
328 udp_output2(NULL
, m
, &saddr
, &daddr
, IPTOS_LOWDELAY
);
331 void bootp_input(struct mbuf
*m
)
333 struct bootp_t
*bp
= mtod(m
, struct bootp_t
*);
335 if (bp
->bp_op
== BOOTP_REQUEST
) {
336 bootp_reply(m
->slirp
, bp
);