tcg/i386: Implement vector minmax arithmetic
[qemu/ar7.git] / slirp / bootp.c
blob4c9a77eb98f1890d3d0ab13b93bbbd0f1a2508ec
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 "qemu/osdep.h"
25 #include "slirp.h"
27 #if defined(_WIN32)
28 /* Windows ntohl() returns an u_long value.
29 * Add a type cast to match the format strings. */
30 # define ntohl(n) ((uint32_t)ntohl(n))
31 #endif
33 /* XXX: only DHCP is supported */
35 #define LEASE_TIME (24 * 3600)
37 static const uint8_t rfc1533_cookie[] = { RFC1533_COOKIE };
39 #define DPRINTF(fmt, ...) DEBUG_CALL(fmt, ##__VA_ARGS__)
41 static BOOTPClient *get_new_addr(Slirp *slirp, struct in_addr *paddr,
42 const uint8_t *macaddr)
44 BOOTPClient *bc;
45 int i;
47 for(i = 0; i < NB_BOOTP_CLIENTS; i++) {
48 bc = &slirp->bootp_clients[i];
49 if (!bc->allocated || !memcmp(macaddr, bc->macaddr, 6))
50 goto found;
52 return NULL;
53 found:
54 bc = &slirp->bootp_clients[i];
55 bc->allocated = 1;
56 paddr->s_addr = slirp->vdhcp_startaddr.s_addr + htonl(i);
57 return bc;
60 static BOOTPClient *request_addr(Slirp *slirp, const struct in_addr *paddr,
61 const uint8_t *macaddr)
63 uint32_t req_addr = ntohl(paddr->s_addr);
64 uint32_t dhcp_addr = ntohl(slirp->vdhcp_startaddr.s_addr);
65 BOOTPClient *bc;
67 if (req_addr >= dhcp_addr &&
68 req_addr < (dhcp_addr + NB_BOOTP_CLIENTS)) {
69 bc = &slirp->bootp_clients[req_addr - dhcp_addr];
70 if (!bc->allocated || !memcmp(macaddr, bc->macaddr, 6)) {
71 bc->allocated = 1;
72 return bc;
75 return NULL;
78 static BOOTPClient *find_addr(Slirp *slirp, struct in_addr *paddr,
79 const uint8_t *macaddr)
81 BOOTPClient *bc;
82 int i;
84 for(i = 0; i < NB_BOOTP_CLIENTS; i++) {
85 if (!memcmp(macaddr, slirp->bootp_clients[i].macaddr, 6))
86 goto found;
88 return NULL;
89 found:
90 bc = &slirp->bootp_clients[i];
91 bc->allocated = 1;
92 paddr->s_addr = slirp->vdhcp_startaddr.s_addr + htonl(i);
93 return bc;
96 static void dhcp_decode(const struct bootp_t *bp, int *pmsg_type,
97 struct in_addr *preq_addr)
99 const uint8_t *p, *p_end;
100 int len, tag;
102 *pmsg_type = 0;
103 preq_addr->s_addr = htonl(0L);
105 p = bp->bp_vend;
106 p_end = p + DHCP_OPT_LEN;
107 if (memcmp(p, rfc1533_cookie, 4) != 0)
108 return;
109 p += 4;
110 while (p < p_end) {
111 tag = p[0];
112 if (tag == RFC1533_PAD) {
113 p++;
114 } else if (tag == RFC1533_END) {
115 break;
116 } else {
117 p++;
118 if (p >= p_end)
119 break;
120 len = *p++;
121 if (p + len > p_end) {
122 break;
124 DPRINTF("dhcp: tag=%d len=%d\n", tag, len);
126 switch(tag) {
127 case RFC2132_MSG_TYPE:
128 if (len >= 1)
129 *pmsg_type = p[0];
130 break;
131 case RFC2132_REQ_ADDR:
132 if (len >= 4) {
133 memcpy(&(preq_addr->s_addr), p, 4);
135 break;
136 default:
137 break;
139 p += len;
142 if (*pmsg_type == DHCPREQUEST && preq_addr->s_addr == htonl(0L) &&
143 bp->bp_ciaddr.s_addr) {
144 memcpy(&(preq_addr->s_addr), &bp->bp_ciaddr, 4);
148 static void bootp_reply(Slirp *slirp, const struct bootp_t *bp)
150 BOOTPClient *bc = NULL;
151 struct mbuf *m;
152 struct bootp_t *rbp;
153 struct sockaddr_in saddr, daddr;
154 struct in_addr preq_addr;
155 int dhcp_msg_type, val;
156 uint8_t *q;
157 uint8_t *end;
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));
165 else {
166 DPRINTF("\n");
169 if (dhcp_msg_type == 0)
170 dhcp_msg_type = DHCPREQUEST; /* Force reply for old BOOTP clients */
172 if (dhcp_msg_type != DHCPDISCOVER &&
173 dhcp_msg_type != DHCPREQUEST)
174 return;
176 /* Get client's hardware address from bootp request */
177 memcpy(client_ethaddr, bp->bp_hwaddr, ETH_ALEN);
179 m = m_get(slirp);
180 if (!m) {
181 return;
183 m->m_data += IF_MAXLINKHDR;
184 rbp = (struct bootp_t *)m->m_data;
185 m->m_data += sizeof(struct udpiphdr);
186 memset(rbp, 0, sizeof(struct bootp_t));
188 if (dhcp_msg_type == DHCPDISCOVER) {
189 if (preq_addr.s_addr != htonl(0L)) {
190 bc = request_addr(slirp, &preq_addr, client_ethaddr);
191 if (bc) {
192 daddr.sin_addr = preq_addr;
195 if (!bc) {
196 new_addr:
197 bc = get_new_addr(slirp, &daddr.sin_addr, client_ethaddr);
198 if (!bc) {
199 DPRINTF("no address left\n");
200 return;
203 memcpy(bc->macaddr, client_ethaddr, ETH_ALEN);
204 } else if (preq_addr.s_addr != htonl(0L)) {
205 bc = request_addr(slirp, &preq_addr, client_ethaddr);
206 if (bc) {
207 daddr.sin_addr = preq_addr;
208 memcpy(bc->macaddr, client_ethaddr, ETH_ALEN);
209 } else {
210 /* DHCPNAKs should be sent to broadcast */
211 daddr.sin_addr.s_addr = 0xffffffff;
213 } else {
214 bc = find_addr(slirp, &daddr.sin_addr, bp->bp_hwaddr);
215 if (!bc) {
216 /* if never assigned, behaves as if it was already
217 assigned (windows fix because it remembers its address) */
218 goto new_addr;
222 /* Update ARP table for this IP address */
223 arp_table_add(slirp, daddr.sin_addr.s_addr, client_ethaddr);
225 saddr.sin_addr = slirp->vhost_addr;
226 saddr.sin_port = htons(BOOTP_SERVER);
228 daddr.sin_port = htons(BOOTP_CLIENT);
230 rbp->bp_op = BOOTP_REPLY;
231 rbp->bp_xid = bp->bp_xid;
232 rbp->bp_htype = 1;
233 rbp->bp_hlen = 6;
234 memcpy(rbp->bp_hwaddr, bp->bp_hwaddr, ETH_ALEN);
236 rbp->bp_yiaddr = daddr.sin_addr; /* Client IP address */
237 rbp->bp_siaddr = saddr.sin_addr; /* Server IP address */
239 q = rbp->bp_vend;
240 end = (uint8_t *)&rbp[1];
241 memcpy(q, rfc1533_cookie, 4);
242 q += 4;
244 if (bc) {
245 DPRINTF("%s addr=%08" PRIx32 "\n",
246 (dhcp_msg_type == DHCPDISCOVER) ? "offered" : "ack'ed",
247 ntohl(daddr.sin_addr.s_addr));
249 if (dhcp_msg_type == DHCPDISCOVER) {
250 *q++ = RFC2132_MSG_TYPE;
251 *q++ = 1;
252 *q++ = DHCPOFFER;
253 } else /* DHCPREQUEST */ {
254 *q++ = RFC2132_MSG_TYPE;
255 *q++ = 1;
256 *q++ = DHCPACK;
259 if (slirp->bootp_filename)
260 snprintf((char *)rbp->bp_file, sizeof(rbp->bp_file), "%s",
261 slirp->bootp_filename);
263 *q++ = RFC2132_SRV_ID;
264 *q++ = 4;
265 memcpy(q, &saddr.sin_addr, 4);
266 q += 4;
268 *q++ = RFC1533_NETMASK;
269 *q++ = 4;
270 memcpy(q, &slirp->vnetwork_mask, 4);
271 q += 4;
273 if (!slirp->restricted) {
274 *q++ = RFC1533_GATEWAY;
275 *q++ = 4;
276 memcpy(q, &saddr.sin_addr, 4);
277 q += 4;
279 *q++ = RFC1533_DNS;
280 *q++ = 4;
281 memcpy(q, &slirp->vnameserver_addr, 4);
282 q += 4;
285 *q++ = RFC2132_LEASE_TIME;
286 *q++ = 4;
287 val = htonl(LEASE_TIME);
288 memcpy(q, &val, 4);
289 q += 4;
291 if (*slirp->client_hostname) {
292 val = strlen(slirp->client_hostname);
293 if (q + val + 2 >= end) {
294 g_warning("DHCP packet size exceeded, "
295 "omitting host name option.");
296 } else {
297 *q++ = RFC1533_HOSTNAME;
298 *q++ = val;
299 memcpy(q, slirp->client_hostname, val);
300 q += val;
304 if (slirp->vdomainname) {
305 val = strlen(slirp->vdomainname);
306 if (q + val + 2 >= end) {
307 g_warning("DHCP packet size exceeded, "
308 "omitting domain name option.");
309 } else {
310 *q++ = RFC1533_DOMAINNAME;
311 *q++ = val;
312 memcpy(q, slirp->vdomainname, val);
313 q += val;
317 if (slirp->tftp_server_name) {
318 val = strlen(slirp->tftp_server_name);
319 if (q + val + 2 >= end) {
320 g_warning("DHCP packet size exceeded, "
321 "omitting tftp-server-name option.");
322 } else {
323 *q++ = RFC2132_TFTP_SERVER_NAME;
324 *q++ = val;
325 memcpy(q, slirp->tftp_server_name, val);
326 q += val;
330 if (slirp->vdnssearch) {
331 val = slirp->vdnssearch_len;
332 if (q + val >= end) {
333 g_warning("DHCP packet size exceeded, "
334 "omitting domain-search option.");
335 } else {
336 memcpy(q, slirp->vdnssearch, val);
337 q += val;
340 } else {
341 static const char nak_msg[] = "requested address not available";
343 DPRINTF("nak'ed addr=%08" PRIx32 "\n", ntohl(preq_addr.s_addr));
345 *q++ = RFC2132_MSG_TYPE;
346 *q++ = 1;
347 *q++ = DHCPNAK;
349 *q++ = RFC2132_MESSAGE;
350 *q++ = sizeof(nak_msg) - 1;
351 memcpy(q, nak_msg, sizeof(nak_msg) - 1);
352 q += sizeof(nak_msg) - 1;
354 assert(q < end);
355 *q = RFC1533_END;
357 daddr.sin_addr.s_addr = 0xffffffffu;
359 m->m_len = sizeof(struct bootp_t) -
360 sizeof(struct ip) - sizeof(struct udphdr);
361 udp_output(NULL, m, &saddr, &daddr, IPTOS_LOWDELAY);
364 void bootp_input(struct mbuf *m)
366 struct bootp_t *bp = mtod(m, struct bootp_t *);
368 if (bp->bp_op == BOOTP_REQUEST) {
369 bootp_reply(m->slirp, bp);