Hardware convenience library
[qemu/mini2440.git] / slirp / bootp.c
blobae71e460b6853015d4b43ba369d3a4f6ca0fb61f
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 /* XXX: only DHCP is supported */
28 #define NB_ADDR 16
30 #define START_ADDR 15
32 #define LEASE_TIME (24 * 3600)
34 typedef struct {
35 uint8_t allocated;
36 uint8_t macaddr[6];
37 } BOOTPClient;
39 static BOOTPClient bootp_clients[NB_ADDR];
41 const char *bootp_filename;
43 static const uint8_t rfc1533_cookie[] = { RFC1533_COOKIE };
45 #ifdef DEBUG
46 #define dprintf(fmt, ...) \
47 if (slirp_debug & DBG_CALL) { fprintf(dfd, fmt, ## __VA_ARGS__); fflush(dfd); }
48 #else
49 #define dprintf(fmt, ...)
50 #endif
52 static BOOTPClient *get_new_addr(struct in_addr *paddr)
54 BOOTPClient *bc;
55 int i;
57 for(i = 0; i < NB_ADDR; i++) {
58 if (!bootp_clients[i].allocated)
59 goto found;
61 return NULL;
62 found:
63 bc = &bootp_clients[i];
64 bc->allocated = 1;
65 paddr->s_addr = htonl(ntohl(special_addr.s_addr) | (i + START_ADDR));
66 return bc;
69 static BOOTPClient *request_addr(const struct in_addr *paddr,
70 const uint8_t *macaddr)
72 uint32_t req_addr = ntohl(paddr->s_addr);
73 uint32_t spec_addr = ntohl(special_addr.s_addr);
74 BOOTPClient *bc;
76 if (req_addr >= (spec_addr | START_ADDR) &&
77 req_addr < (spec_addr | (NB_ADDR + START_ADDR))) {
78 bc = &bootp_clients[(req_addr & 0xff) - START_ADDR];
79 if (!bc->allocated || !memcmp(macaddr, bc->macaddr, 6)) {
80 bc->allocated = 1;
81 return bc;
84 return NULL;
87 static BOOTPClient *find_addr(struct in_addr *paddr, const uint8_t *macaddr)
89 BOOTPClient *bc;
90 int i;
92 for(i = 0; i < NB_ADDR; i++) {
93 if (!memcmp(macaddr, bootp_clients[i].macaddr, 6))
94 goto found;
96 return NULL;
97 found:
98 bc = &bootp_clients[i];
99 bc->allocated = 1;
100 paddr->s_addr = htonl(ntohl(special_addr.s_addr) | (i + START_ADDR));
101 return bc;
104 static void dhcp_decode(const struct bootp_t *bp, int *pmsg_type,
105 const struct in_addr **preq_addr)
107 const uint8_t *p, *p_end;
108 int len, tag;
110 *pmsg_type = 0;
111 *preq_addr = NULL;
113 p = bp->bp_vend;
114 p_end = p + DHCP_OPT_LEN;
115 if (memcmp(p, rfc1533_cookie, 4) != 0)
116 return;
117 p += 4;
118 while (p < p_end) {
119 tag = p[0];
120 if (tag == RFC1533_PAD) {
121 p++;
122 } else if (tag == RFC1533_END) {
123 break;
124 } else {
125 p++;
126 if (p >= p_end)
127 break;
128 len = *p++;
129 dprintf("dhcp: tag=%d len=%d\n", tag, len);
131 switch(tag) {
132 case RFC2132_MSG_TYPE:
133 if (len >= 1)
134 *pmsg_type = p[0];
135 break;
136 case RFC2132_REQ_ADDR:
137 if (len >= 4)
138 *preq_addr = (struct in_addr *)p;
139 break;
140 default:
141 break;
143 p += len;
146 if (*pmsg_type == DHCPREQUEST && !*preq_addr && bp->bp_ciaddr.s_addr) {
147 *preq_addr = &bp->bp_ciaddr;
151 static void bootp_reply(const struct bootp_t *bp)
153 BOOTPClient *bc = NULL;
154 struct mbuf *m;
155 struct bootp_t *rbp;
156 struct sockaddr_in saddr, daddr;
157 struct in_addr dns_addr;
158 const struct in_addr *preq_addr;
159 int dhcp_msg_type, val;
160 uint8_t *q;
162 /* extract exact DHCP msg type */
163 dhcp_decode(bp, &dhcp_msg_type, &preq_addr);
164 dprintf("bootp packet op=%d msgtype=%d", bp->bp_op, dhcp_msg_type);
165 if (preq_addr)
166 dprintf(" req_addr=%08x\n", ntohl(preq_addr->s_addr));
167 else
168 dprintf("\n");
170 if (dhcp_msg_type == 0)
171 dhcp_msg_type = DHCPREQUEST; /* Force reply for old BOOTP clients */
173 if (dhcp_msg_type != DHCPDISCOVER &&
174 dhcp_msg_type != DHCPREQUEST)
175 return;
176 /* XXX: this is a hack to get the client mac address */
177 memcpy(client_ethaddr, bp->bp_hwaddr, 6);
179 if ((m = m_get()) == NULL)
180 return;
181 m->m_data += IF_MAXLINKHDR;
182 rbp = (struct bootp_t *)m->m_data;
183 m->m_data += sizeof(struct udpiphdr);
184 memset(rbp, 0, sizeof(struct bootp_t));
186 if (dhcp_msg_type == DHCPDISCOVER) {
187 if (preq_addr) {
188 bc = request_addr(preq_addr, client_ethaddr);
189 if (bc) {
190 daddr.sin_addr = *preq_addr;
193 if (!bc) {
194 new_addr:
195 bc = get_new_addr(&daddr.sin_addr);
196 if (!bc) {
197 dprintf("no address left\n");
198 return;
201 memcpy(bc->macaddr, client_ethaddr, 6);
202 } else if (preq_addr) {
203 bc = request_addr(preq_addr, client_ethaddr);
204 if (bc) {
205 daddr.sin_addr = *preq_addr;
206 memcpy(bc->macaddr, client_ethaddr, 6);
207 } else {
208 daddr.sin_addr.s_addr = 0;
210 } else {
211 bc = find_addr(&daddr.sin_addr, bp->bp_hwaddr);
212 if (!bc) {
213 /* if never assigned, behaves as if it was already
214 assigned (windows fix because it remembers its address) */
215 goto new_addr;
219 saddr.sin_addr.s_addr = htonl(ntohl(special_addr.s_addr) | CTL_ALIAS);
220 saddr.sin_port = htons(BOOTP_SERVER);
222 daddr.sin_port = htons(BOOTP_CLIENT);
224 rbp->bp_op = BOOTP_REPLY;
225 rbp->bp_xid = bp->bp_xid;
226 rbp->bp_htype = 1;
227 rbp->bp_hlen = 6;
228 memcpy(rbp->bp_hwaddr, bp->bp_hwaddr, 6);
230 rbp->bp_yiaddr = daddr.sin_addr; /* Client IP address */
231 rbp->bp_siaddr = saddr.sin_addr; /* Server IP address */
233 q = rbp->bp_vend;
234 memcpy(q, rfc1533_cookie, 4);
235 q += 4;
237 if (bc) {
238 dprintf("%s addr=%08x\n",
239 (dhcp_msg_type == DHCPDISCOVER) ? "offered" : "ack'ed",
240 ntohl(daddr.sin_addr.s_addr));
242 if (dhcp_msg_type == DHCPDISCOVER) {
243 *q++ = RFC2132_MSG_TYPE;
244 *q++ = 1;
245 *q++ = DHCPOFFER;
246 } else /* DHCPREQUEST */ {
247 *q++ = RFC2132_MSG_TYPE;
248 *q++ = 1;
249 *q++ = DHCPACK;
252 if (bootp_filename)
253 snprintf((char *)rbp->bp_file, sizeof(rbp->bp_file), "%s",
254 bootp_filename);
256 *q++ = RFC2132_SRV_ID;
257 *q++ = 4;
258 memcpy(q, &saddr.sin_addr, 4);
259 q += 4;
261 *q++ = RFC1533_NETMASK;
262 *q++ = 4;
263 *q++ = 0xff;
264 *q++ = 0xff;
265 *q++ = 0xff;
266 *q++ = 0x00;
268 if (!slirp_restrict) {
269 *q++ = RFC1533_GATEWAY;
270 *q++ = 4;
271 memcpy(q, &saddr.sin_addr, 4);
272 q += 4;
274 *q++ = RFC1533_DNS;
275 *q++ = 4;
276 dns_addr.s_addr = htonl(ntohl(special_addr.s_addr) | CTL_DNS);
277 memcpy(q, &dns_addr, 4);
278 q += 4;
281 *q++ = RFC2132_LEASE_TIME;
282 *q++ = 4;
283 val = htonl(LEASE_TIME);
284 memcpy(q, &val, 4);
285 q += 4;
287 if (*slirp_hostname) {
288 val = strlen(slirp_hostname);
289 *q++ = RFC1533_HOSTNAME;
290 *q++ = val;
291 memcpy(q, slirp_hostname, val);
292 q += val;
294 } else {
295 static const char nak_msg[] = "requested address not available";
297 dprintf("nak'ed addr=%08x\n", ntohl(preq_addr->s_addr));
299 *q++ = RFC2132_MSG_TYPE;
300 *q++ = 1;
301 *q++ = DHCPNAK;
303 *q++ = RFC2132_MESSAGE;
304 *q++ = sizeof(nak_msg) - 1;
305 memcpy(q, nak_msg, sizeof(nak_msg) - 1);
306 q += sizeof(nak_msg) - 1;
308 *q++ = RFC1533_END;
310 daddr.sin_addr.s_addr = 0xffffffffu;
312 m->m_len = sizeof(struct bootp_t) -
313 sizeof(struct ip) - sizeof(struct udphdr);
314 udp_output2(NULL, m, &saddr, &daddr, IPTOS_LOWDELAY);
317 void bootp_input(struct mbuf *m)
319 struct bootp_t *bp = mtod(m, struct bootp_t *);
321 if (bp->bp_op == BOOTP_REQUEST) {
322 bootp_reply(bp);