s390x/tcg: Save vregs to extended mchk save area
[qemu.git] / slirp / bootp.c
blobd396849a05ef11a24cd0f7f71ee6e572a2be17a6
1 /*
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
22 * THE SOFTWARE.
24 #include "slirp.h"
26 #if defined(_WIN32)
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))
30 #endif
32 /* XXX: only DHCP is supported */
34 #define LEASE_TIME (24 * 3600)
36 static const uint8_t rfc1533_cookie[] = { RFC1533_COOKIE };
38 #define DPRINTF(fmt, ...) DEBUG_CALL(fmt, ##__VA_ARGS__)
40 static BOOTPClient *get_new_addr(Slirp *slirp, struct in_addr *paddr,
41 const uint8_t *macaddr)
43 BOOTPClient *bc;
44 int i;
46 for(i = 0; i < NB_BOOTP_CLIENTS; i++) {
47 bc = &slirp->bootp_clients[i];
48 if (!bc->allocated || !memcmp(macaddr, bc->macaddr, 6))
49 goto found;
51 return NULL;
52 found:
53 bc = &slirp->bootp_clients[i];
54 bc->allocated = 1;
55 paddr->s_addr = slirp->vdhcp_startaddr.s_addr + htonl(i);
56 return bc;
59 static BOOTPClient *request_addr(Slirp *slirp, const struct in_addr *paddr,
60 const uint8_t *macaddr)
62 uint32_t req_addr = ntohl(paddr->s_addr);
63 uint32_t dhcp_addr = ntohl(slirp->vdhcp_startaddr.s_addr);
64 BOOTPClient *bc;
66 if (req_addr >= dhcp_addr &&
67 req_addr < (dhcp_addr + NB_BOOTP_CLIENTS)) {
68 bc = &slirp->bootp_clients[req_addr - dhcp_addr];
69 if (!bc->allocated || !memcmp(macaddr, bc->macaddr, 6)) {
70 bc->allocated = 1;
71 return bc;
74 return NULL;
77 static BOOTPClient *find_addr(Slirp *slirp, struct in_addr *paddr,
78 const uint8_t *macaddr)
80 BOOTPClient *bc;
81 int i;
83 for(i = 0; i < NB_BOOTP_CLIENTS; i++) {
84 if (!memcmp(macaddr, slirp->bootp_clients[i].macaddr, 6))
85 goto found;
87 return NULL;
88 found:
89 bc = &slirp->bootp_clients[i];
90 bc->allocated = 1;
91 paddr->s_addr = slirp->vdhcp_startaddr.s_addr + htonl(i);
92 return bc;
95 static void dhcp_decode(const struct bootp_t *bp, int *pmsg_type,
96 struct in_addr *preq_addr)
98 const uint8_t *p, *p_end;
99 int len, tag;
101 *pmsg_type = 0;
102 preq_addr->s_addr = htonl(0L);
104 p = bp->bp_vend;
105 p_end = p + DHCP_OPT_LEN;
106 if (memcmp(p, rfc1533_cookie, 4) != 0)
107 return;
108 p += 4;
109 while (p < p_end) {
110 tag = p[0];
111 if (tag == RFC1533_PAD) {
112 p++;
113 } else if (tag == RFC1533_END) {
114 break;
115 } else {
116 p++;
117 if (p >= p_end)
118 break;
119 len = *p++;
120 if (p + len > p_end) {
121 break;
123 DPRINTF("dhcp: tag=%d len=%d\n", tag, len);
125 switch(tag) {
126 case RFC2132_MSG_TYPE:
127 if (len >= 1)
128 *pmsg_type = p[0];
129 break;
130 case RFC2132_REQ_ADDR:
131 if (len >= 4) {
132 memcpy(&(preq_addr->s_addr), p, 4);
134 break;
135 default:
136 break;
138 p += len;
141 if (*pmsg_type == DHCPREQUEST && preq_addr->s_addr == htonl(0L) &&
142 bp->bp_ciaddr.s_addr) {
143 memcpy(&(preq_addr->s_addr), &bp->bp_ciaddr, 4);
147 static void bootp_reply(Slirp *slirp, const struct bootp_t *bp)
149 BOOTPClient *bc = NULL;
150 struct mbuf *m;
151 struct bootp_t *rbp;
152 struct sockaddr_in saddr, daddr;
153 struct in_addr preq_addr;
154 int dhcp_msg_type, val;
155 uint8_t *q;
156 uint8_t *end;
157 uint8_t client_ethaddr[ETH_ALEN];
159 /* extract exact DHCP msg type */
160 dhcp_decode(bp, &dhcp_msg_type, &preq_addr);
161 DPRINTF("bootp packet op=%d msgtype=%d", bp->bp_op, dhcp_msg_type);
162 if (preq_addr.s_addr != htonl(0L))
163 DPRINTF(" req_addr=%08" PRIx32 "\n", ntohl(preq_addr.s_addr));
164 else {
165 DPRINTF("\n");
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)
173 return;
175 /* Get client's hardware address from bootp request */
176 memcpy(client_ethaddr, bp->bp_hwaddr, ETH_ALEN);
178 m = m_get(slirp);
179 if (!m) {
180 return;
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);
190 if (bc) {
191 daddr.sin_addr = preq_addr;
194 if (!bc) {
195 new_addr:
196 bc = get_new_addr(slirp, &daddr.sin_addr, client_ethaddr);
197 if (!bc) {
198 DPRINTF("no address left\n");
199 return;
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);
205 if (bc) {
206 daddr.sin_addr = preq_addr;
207 memcpy(bc->macaddr, client_ethaddr, ETH_ALEN);
208 } else {
209 /* DHCPNAKs should be sent to broadcast */
210 daddr.sin_addr.s_addr = 0xffffffff;
212 } else {
213 bc = find_addr(slirp, &daddr.sin_addr, bp->bp_hwaddr);
214 if (!bc) {
215 /* if never assigned, behaves as if it was already
216 assigned (windows fix because it remembers its address) */
217 goto new_addr;
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;
231 rbp->bp_htype = 1;
232 rbp->bp_hlen = 6;
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 */
238 q = rbp->bp_vend;
239 end = (uint8_t *)&rbp[1];
240 memcpy(q, rfc1533_cookie, 4);
241 q += 4;
243 if (bc) {
244 DPRINTF("%s addr=%08" PRIx32 "\n",
245 (dhcp_msg_type == DHCPDISCOVER) ? "offered" : "ack'ed",
246 ntohl(daddr.sin_addr.s_addr));
248 if (dhcp_msg_type == DHCPDISCOVER) {
249 *q++ = RFC2132_MSG_TYPE;
250 *q++ = 1;
251 *q++ = DHCPOFFER;
252 } else /* DHCPREQUEST */ {
253 *q++ = RFC2132_MSG_TYPE;
254 *q++ = 1;
255 *q++ = DHCPACK;
258 if (slirp->bootp_filename)
259 snprintf((char *)rbp->bp_file, sizeof(rbp->bp_file), "%s",
260 slirp->bootp_filename);
262 *q++ = RFC2132_SRV_ID;
263 *q++ = 4;
264 memcpy(q, &saddr.sin_addr, 4);
265 q += 4;
267 *q++ = RFC1533_NETMASK;
268 *q++ = 4;
269 memcpy(q, &slirp->vnetwork_mask, 4);
270 q += 4;
272 if (!slirp->restricted) {
273 *q++ = RFC1533_GATEWAY;
274 *q++ = 4;
275 memcpy(q, &saddr.sin_addr, 4);
276 q += 4;
278 *q++ = RFC1533_DNS;
279 *q++ = 4;
280 memcpy(q, &slirp->vnameserver_addr, 4);
281 q += 4;
284 *q++ = RFC2132_LEASE_TIME;
285 *q++ = 4;
286 val = htonl(LEASE_TIME);
287 memcpy(q, &val, 4);
288 q += 4;
290 if (*slirp->client_hostname) {
291 val = strlen(slirp->client_hostname);
292 if (q + val + 2 >= end) {
293 g_warning("DHCP packet size exceeded, "
294 "omitting host name option.");
295 } else {
296 *q++ = RFC1533_HOSTNAME;
297 *q++ = val;
298 memcpy(q, slirp->client_hostname, val);
299 q += val;
303 if (slirp->vdomainname) {
304 val = strlen(slirp->vdomainname);
305 if (q + val + 2 >= end) {
306 g_warning("DHCP packet size exceeded, "
307 "omitting domain name option.");
308 } else {
309 *q++ = RFC1533_DOMAINNAME;
310 *q++ = val;
311 memcpy(q, slirp->vdomainname, val);
312 q += val;
316 if (slirp->tftp_server_name) {
317 val = strlen(slirp->tftp_server_name);
318 if (q + val + 2 >= end) {
319 g_warning("DHCP packet size exceeded, "
320 "omitting tftp-server-name option.");
321 } else {
322 *q++ = RFC2132_TFTP_SERVER_NAME;
323 *q++ = val;
324 memcpy(q, slirp->tftp_server_name, val);
325 q += val;
329 if (slirp->vdnssearch) {
330 val = slirp->vdnssearch_len;
331 if (q + val >= end) {
332 g_warning("DHCP packet size exceeded, "
333 "omitting domain-search option.");
334 } else {
335 memcpy(q, slirp->vdnssearch, val);
336 q += val;
339 } else {
340 static const char nak_msg[] = "requested address not available";
342 DPRINTF("nak'ed addr=%08" PRIx32 "\n", ntohl(preq_addr.s_addr));
344 *q++ = RFC2132_MSG_TYPE;
345 *q++ = 1;
346 *q++ = DHCPNAK;
348 *q++ = RFC2132_MESSAGE;
349 *q++ = sizeof(nak_msg) - 1;
350 memcpy(q, nak_msg, sizeof(nak_msg) - 1);
351 q += sizeof(nak_msg) - 1;
353 assert(q < end);
354 *q = RFC1533_END;
356 daddr.sin_addr.s_addr = 0xffffffffu;
358 m->m_len = sizeof(struct bootp_t) -
359 sizeof(struct ip) - sizeof(struct udphdr);
360 udp_output(NULL, m, &saddr, &daddr, IPTOS_LOWDELAY);
363 void bootp_input(struct mbuf *m)
365 struct bootp_t *bp = mtod(m, struct bootp_t *);
367 if (bp->bp_op == BOOTP_REQUEST) {
368 bootp_reply(m->slirp, bp);