ppc: cleanup logging
[qemu/cris-port.git] / slirp / bootp.c
blob1baaab1ab1482f1d0f9a5ac639838339d89aba30
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 #ifdef DEBUG
39 #define DPRINTF(fmt, ...) \
40 do if (slirp_debug & DBG_CALL) { fprintf(dfd, fmt, ## __VA_ARGS__); fflush(dfd); } while (0)
41 #else
42 #define DPRINTF(fmt, ...) do{}while(0)
43 #endif
45 static BOOTPClient *get_new_addr(Slirp *slirp, struct in_addr *paddr,
46 const uint8_t *macaddr)
48 BOOTPClient *bc;
49 int i;
51 for(i = 0; i < NB_BOOTP_CLIENTS; i++) {
52 bc = &slirp->bootp_clients[i];
53 if (!bc->allocated || !memcmp(macaddr, bc->macaddr, 6))
54 goto found;
56 return NULL;
57 found:
58 bc = &slirp->bootp_clients[i];
59 bc->allocated = 1;
60 paddr->s_addr = slirp->vdhcp_startaddr.s_addr + htonl(i);
61 return bc;
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);
69 BOOTPClient *bc;
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)) {
75 bc->allocated = 1;
76 return bc;
79 return NULL;
82 static BOOTPClient *find_addr(Slirp *slirp, struct in_addr *paddr,
83 const uint8_t *macaddr)
85 BOOTPClient *bc;
86 int i;
88 for(i = 0; i < NB_BOOTP_CLIENTS; i++) {
89 if (!memcmp(macaddr, slirp->bootp_clients[i].macaddr, 6))
90 goto found;
92 return NULL;
93 found:
94 bc = &slirp->bootp_clients[i];
95 bc->allocated = 1;
96 paddr->s_addr = slirp->vdhcp_startaddr.s_addr + htonl(i);
97 return bc;
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;
104 int len, tag;
106 *pmsg_type = 0;
107 preq_addr->s_addr = htonl(0L);
109 p = bp->bp_vend;
110 p_end = p + DHCP_OPT_LEN;
111 if (memcmp(p, rfc1533_cookie, 4) != 0)
112 return;
113 p += 4;
114 while (p < p_end) {
115 tag = p[0];
116 if (tag == RFC1533_PAD) {
117 p++;
118 } else if (tag == RFC1533_END) {
119 break;
120 } else {
121 p++;
122 if (p >= p_end)
123 break;
124 len = *p++;
125 DPRINTF("dhcp: tag=%d len=%d\n", tag, len);
127 switch(tag) {
128 case RFC2132_MSG_TYPE:
129 if (len >= 1)
130 *pmsg_type = p[0];
131 break;
132 case RFC2132_REQ_ADDR:
133 if (len >= 4) {
134 memcpy(&(preq_addr->s_addr), p, 4);
136 break;
137 default:
138 break;
140 p += len;
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;
152 struct mbuf *m;
153 struct bootp_t *rbp;
154 struct sockaddr_in saddr, daddr;
155 struct in_addr preq_addr;
156 int dhcp_msg_type, val;
157 uint8_t *q;
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");
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 memcpy(q, rfc1533_cookie, 4);
240 q += 4;
242 if (bc) {
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;
249 *q++ = 1;
250 *q++ = DHCPOFFER;
251 } else /* DHCPREQUEST */ {
252 *q++ = RFC2132_MSG_TYPE;
253 *q++ = 1;
254 *q++ = DHCPACK;
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;
262 *q++ = 4;
263 memcpy(q, &saddr.sin_addr, 4);
264 q += 4;
266 *q++ = RFC1533_NETMASK;
267 *q++ = 4;
268 memcpy(q, &slirp->vnetwork_mask, 4);
269 q += 4;
271 if (!slirp->restricted) {
272 *q++ = RFC1533_GATEWAY;
273 *q++ = 4;
274 memcpy(q, &saddr.sin_addr, 4);
275 q += 4;
277 *q++ = RFC1533_DNS;
278 *q++ = 4;
279 memcpy(q, &slirp->vnameserver_addr, 4);
280 q += 4;
283 *q++ = RFC2132_LEASE_TIME;
284 *q++ = 4;
285 val = htonl(LEASE_TIME);
286 memcpy(q, &val, 4);
287 q += 4;
289 if (*slirp->client_hostname) {
290 val = strlen(slirp->client_hostname);
291 *q++ = RFC1533_HOSTNAME;
292 *q++ = val;
293 memcpy(q, slirp->client_hostname, val);
294 q += 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.");
303 } else {
304 memcpy(q, slirp->vdnssearch, val);
305 q += val;
308 } else {
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;
314 *q++ = 1;
315 *q++ = DHCPNAK;
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;
322 *q = RFC1533_END;
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);