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 */
28 #define LEASE_TIME (24 * 3600)
30 static const uint8_t rfc1533_cookie
[] = { RFC1533_COOKIE
};
33 #define DPRINTF(fmt, ...) \
34 do if (slirp_debug & DBG_CALL) { fprintf(dfd, fmt, ## __VA_ARGS__); fflush(dfd); } while (0)
36 #define DPRINTF(fmt, ...) do{}while(0)
39 static BOOTPClient
*get_new_addr(Slirp
*slirp
, struct in_addr
*paddr
,
40 const uint8_t *macaddr
)
45 for(i
= 0; i
< NB_BOOTP_CLIENTS
; i
++) {
46 bc
= &slirp
->bootp_clients
[i
];
47 if (!bc
->allocated
|| !memcmp(macaddr
, bc
->macaddr
, 6))
52 bc
= &slirp
->bootp_clients
[i
];
54 paddr
->s_addr
= slirp
->vdhcp_startaddr
.s_addr
+ htonl(i
);
58 static BOOTPClient
*request_addr(Slirp
*slirp
, const struct in_addr
*paddr
,
59 const uint8_t *macaddr
)
61 uint32_t req_addr
= ntohl(paddr
->s_addr
);
62 uint32_t dhcp_addr
= ntohl(slirp
->vdhcp_startaddr
.s_addr
);
65 if (req_addr
>= dhcp_addr
&&
66 req_addr
< (dhcp_addr
+ NB_BOOTP_CLIENTS
)) {
67 bc
= &slirp
->bootp_clients
[req_addr
- dhcp_addr
];
68 if (!bc
->allocated
|| !memcmp(macaddr
, bc
->macaddr
, 6)) {
76 static BOOTPClient
*find_addr(Slirp
*slirp
, struct in_addr
*paddr
,
77 const uint8_t *macaddr
)
82 for(i
= 0; i
< NB_BOOTP_CLIENTS
; i
++) {
83 if (!memcmp(macaddr
, slirp
->bootp_clients
[i
].macaddr
, 6))
88 bc
= &slirp
->bootp_clients
[i
];
90 paddr
->s_addr
= slirp
->vdhcp_startaddr
.s_addr
+ htonl(i
);
94 static void dhcp_decode(const struct bootp_t
*bp
, int *pmsg_type
,
95 struct in_addr
*preq_addr
)
97 const uint8_t *p
, *p_end
;
101 preq_addr
->s_addr
= htonl(0L);
104 p_end
= p
+ DHCP_OPT_LEN
;
105 if (memcmp(p
, rfc1533_cookie
, 4) != 0)
110 if (tag
== RFC1533_PAD
) {
112 } else if (tag
== RFC1533_END
) {
119 DPRINTF("dhcp: tag=%d len=%d\n", tag
, len
);
122 case RFC2132_MSG_TYPE
:
126 case RFC2132_REQ_ADDR
:
128 memcpy(&(preq_addr
->s_addr
), p
, 4);
137 if (*pmsg_type
== DHCPREQUEST
&& preq_addr
->s_addr
== htonl(0L) &&
138 bp
->bp_ciaddr
.s_addr
) {
139 memcpy(&(preq_addr
->s_addr
), &bp
->bp_ciaddr
, 4);
143 static void bootp_reply(Slirp
*slirp
, const struct bootp_t
*bp
)
145 BOOTPClient
*bc
= NULL
;
148 struct sockaddr_in saddr
, daddr
;
149 struct in_addr preq_addr
;
150 int dhcp_msg_type
, val
;
153 /* extract exact DHCP msg type */
154 dhcp_decode(bp
, &dhcp_msg_type
, &preq_addr
);
155 DPRINTF("bootp packet op=%d msgtype=%d", bp
->bp_op
, dhcp_msg_type
);
156 if (preq_addr
.s_addr
!= htonl(0L))
157 DPRINTF(" req_addr=%08x\n", ntohl(preq_addr
.s_addr
));
161 if (dhcp_msg_type
== 0)
162 dhcp_msg_type
= DHCPREQUEST
; /* Force reply for old BOOTP clients */
164 if (dhcp_msg_type
!= DHCPDISCOVER
&&
165 dhcp_msg_type
!= DHCPREQUEST
)
167 /* XXX: this is a hack to get the client mac address */
168 memcpy(slirp
->client_ethaddr
, bp
->bp_hwaddr
, 6);
174 m
->m_data
+= IF_MAXLINKHDR
;
175 rbp
= (struct bootp_t
*)m
->m_data
;
176 m
->m_data
+= sizeof(struct udpiphdr
);
177 memset(rbp
, 0, sizeof(struct bootp_t
));
179 if (dhcp_msg_type
== DHCPDISCOVER
) {
180 if (preq_addr
.s_addr
!= htonl(0L)) {
181 bc
= request_addr(slirp
, &preq_addr
, slirp
->client_ethaddr
);
183 daddr
.sin_addr
= preq_addr
;
188 bc
= get_new_addr(slirp
, &daddr
.sin_addr
, slirp
->client_ethaddr
);
190 DPRINTF("no address left\n");
194 memcpy(bc
->macaddr
, slirp
->client_ethaddr
, 6);
195 } else if (preq_addr
.s_addr
!= htonl(0L)) {
196 bc
= request_addr(slirp
, &preq_addr
, slirp
->client_ethaddr
);
198 daddr
.sin_addr
= preq_addr
;
199 memcpy(bc
->macaddr
, slirp
->client_ethaddr
, 6);
201 daddr
.sin_addr
.s_addr
= 0;
204 bc
= find_addr(slirp
, &daddr
.sin_addr
, bp
->bp_hwaddr
);
206 /* if never assigned, behaves as if it was already
207 assigned (windows fix because it remembers its address) */
212 saddr
.sin_addr
= slirp
->vhost_addr
;
213 saddr
.sin_port
= htons(BOOTP_SERVER
);
215 daddr
.sin_port
= htons(BOOTP_CLIENT
);
217 rbp
->bp_op
= BOOTP_REPLY
;
218 rbp
->bp_xid
= bp
->bp_xid
;
221 memcpy(rbp
->bp_hwaddr
, bp
->bp_hwaddr
, 6);
223 rbp
->bp_yiaddr
= daddr
.sin_addr
; /* Client IP address */
224 rbp
->bp_siaddr
= saddr
.sin_addr
; /* Server IP address */
227 memcpy(q
, rfc1533_cookie
, 4);
231 DPRINTF("%s addr=%08x\n",
232 (dhcp_msg_type
== DHCPDISCOVER
) ? "offered" : "ack'ed",
233 ntohl(daddr
.sin_addr
.s_addr
));
235 if (dhcp_msg_type
== DHCPDISCOVER
) {
236 *q
++ = RFC2132_MSG_TYPE
;
239 } else /* DHCPREQUEST */ {
240 *q
++ = RFC2132_MSG_TYPE
;
245 if (slirp
->bootp_filename
)
246 snprintf((char *)rbp
->bp_file
, sizeof(rbp
->bp_file
), "%s",
247 slirp
->bootp_filename
);
249 *q
++ = RFC2132_SRV_ID
;
251 memcpy(q
, &saddr
.sin_addr
, 4);
254 *q
++ = RFC1533_NETMASK
;
256 memcpy(q
, &slirp
->vnetwork_mask
, 4);
259 if (!slirp
->restricted
) {
260 *q
++ = RFC1533_GATEWAY
;
262 memcpy(q
, &saddr
.sin_addr
, 4);
267 memcpy(q
, &slirp
->vnameserver_addr
, 4);
271 *q
++ = RFC2132_LEASE_TIME
;
273 val
= htonl(LEASE_TIME
);
277 if (*slirp
->client_hostname
) {
278 val
= strlen(slirp
->client_hostname
);
279 *q
++ = RFC1533_HOSTNAME
;
281 memcpy(q
, slirp
->client_hostname
, val
);
285 static const char nak_msg
[] = "requested address not available";
287 DPRINTF("nak'ed addr=%08x\n", ntohl(preq_addr
->s_addr
));
289 *q
++ = RFC2132_MSG_TYPE
;
293 *q
++ = RFC2132_MESSAGE
;
294 *q
++ = sizeof(nak_msg
) - 1;
295 memcpy(q
, nak_msg
, sizeof(nak_msg
) - 1);
296 q
+= sizeof(nak_msg
) - 1;
300 daddr
.sin_addr
.s_addr
= 0xffffffffu
;
302 m
->m_len
= sizeof(struct bootp_t
) -
303 sizeof(struct ip
) - sizeof(struct udphdr
);
304 udp_output2(NULL
, m
, &saddr
, &daddr
, IPTOS_LOWDELAY
);
307 void bootp_input(struct mbuf
*m
)
309 struct bootp_t
*bp
= mtod(m
, struct bootp_t
*);
311 if (bp
->bp_op
== BOOTP_REQUEST
) {
312 bootp_reply(m
->slirp
, bp
);