]> Git Repo - qemu.git/blob - slirp/bootp.c
5dd1a415b59c5abb78808c4e0d966230ef057ccc
[qemu.git] / slirp / bootp.c
1 /*
2  * QEMU BOOTP/DHCP server
3  *
4  * Copyright (c) 2004 Fabrice Bellard
5  *
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:
12  *
13  * The above copyright notice and this permission notice shall be included in
14  * all copies or substantial portions of the Software.
15  *
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.
23  */
24 #include "qemu/osdep.h"
25 #include "slirp.h"
26
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
32
33 /* XXX: only DHCP is supported */
34
35 #define LEASE_TIME (24 * 3600)
36
37 static const uint8_t rfc1533_cookie[] = { RFC1533_COOKIE };
38
39 #ifdef DEBUG
40 #define DPRINTF(fmt, ...) \
41 do if (slirp_debug & DBG_CALL) { fprintf(dfd, fmt, ##  __VA_ARGS__); fflush(dfd); } while (0)
42 #else
43 #define DPRINTF(fmt, ...) do{}while(0)
44 #endif
45
46 static BOOTPClient *get_new_addr(Slirp *slirp, struct in_addr *paddr,
47                                  const uint8_t *macaddr)
48 {
49     BOOTPClient *bc;
50     int i;
51
52     for(i = 0; i < NB_BOOTP_CLIENTS; i++) {
53         bc = &slirp->bootp_clients[i];
54         if (!bc->allocated || !memcmp(macaddr, bc->macaddr, 6))
55             goto found;
56     }
57     return NULL;
58  found:
59     bc = &slirp->bootp_clients[i];
60     bc->allocated = 1;
61     paddr->s_addr = slirp->vdhcp_startaddr.s_addr + htonl(i);
62     return bc;
63 }
64
65 static BOOTPClient *request_addr(Slirp *slirp, const struct in_addr *paddr,
66                                  const uint8_t *macaddr)
67 {
68     uint32_t req_addr = ntohl(paddr->s_addr);
69     uint32_t dhcp_addr = ntohl(slirp->vdhcp_startaddr.s_addr);
70     BOOTPClient *bc;
71
72     if (req_addr >= dhcp_addr &&
73         req_addr < (dhcp_addr + NB_BOOTP_CLIENTS)) {
74         bc = &slirp->bootp_clients[req_addr - dhcp_addr];
75         if (!bc->allocated || !memcmp(macaddr, bc->macaddr, 6)) {
76             bc->allocated = 1;
77             return bc;
78         }
79     }
80     return NULL;
81 }
82
83 static BOOTPClient *find_addr(Slirp *slirp, struct in_addr *paddr,
84                               const uint8_t *macaddr)
85 {
86     BOOTPClient *bc;
87     int i;
88
89     for(i = 0; i < NB_BOOTP_CLIENTS; i++) {
90         if (!memcmp(macaddr, slirp->bootp_clients[i].macaddr, 6))
91             goto found;
92     }
93     return NULL;
94  found:
95     bc = &slirp->bootp_clients[i];
96     bc->allocated = 1;
97     paddr->s_addr = slirp->vdhcp_startaddr.s_addr + htonl(i);
98     return bc;
99 }
100
101 static void dhcp_decode(const struct bootp_t *bp, int *pmsg_type,
102                         struct in_addr *preq_addr)
103 {
104     const uint8_t *p, *p_end;
105     int len, tag;
106
107     *pmsg_type = 0;
108     preq_addr->s_addr = htonl(0L);
109
110     p = bp->bp_vend;
111     p_end = p + DHCP_OPT_LEN;
112     if (memcmp(p, rfc1533_cookie, 4) != 0)
113         return;
114     p += 4;
115     while (p < p_end) {
116         tag = p[0];
117         if (tag == RFC1533_PAD) {
118             p++;
119         } else if (tag == RFC1533_END) {
120             break;
121         } else {
122             p++;
123             if (p >= p_end)
124                 break;
125             len = *p++;
126             if (p + len > p_end) {
127                 break;
128             }
129             DPRINTF("dhcp: tag=%d len=%d\n", tag, len);
130
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                     memcpy(&(preq_addr->s_addr), p, 4);
139                 }
140                 break;
141             default:
142                 break;
143             }
144             p += len;
145         }
146     }
147     if (*pmsg_type == DHCPREQUEST && preq_addr->s_addr == htonl(0L) &&
148         bp->bp_ciaddr.s_addr) {
149         memcpy(&(preq_addr->s_addr), &bp->bp_ciaddr, 4);
150     }
151 }
152
153 static void bootp_reply(Slirp *slirp, const struct bootp_t *bp)
154 {
155     BOOTPClient *bc = NULL;
156     struct mbuf *m;
157     struct bootp_t *rbp;
158     struct sockaddr_in saddr, daddr;
159     struct in_addr preq_addr;
160     int dhcp_msg_type, val;
161     uint8_t *q;
162     uint8_t client_ethaddr[ETH_ALEN];
163
164     /* extract exact DHCP msg type */
165     dhcp_decode(bp, &dhcp_msg_type, &preq_addr);
166     DPRINTF("bootp packet op=%d msgtype=%d", bp->bp_op, dhcp_msg_type);
167     if (preq_addr.s_addr != htonl(0L))
168         DPRINTF(" req_addr=%08" PRIx32 "\n", ntohl(preq_addr.s_addr));
169     else
170         DPRINTF("\n");
171
172     if (dhcp_msg_type == 0)
173         dhcp_msg_type = DHCPREQUEST; /* Force reply for old BOOTP clients */
174
175     if (dhcp_msg_type != DHCPDISCOVER &&
176         dhcp_msg_type != DHCPREQUEST)
177         return;
178
179     /* Get client's hardware address from bootp request */
180     memcpy(client_ethaddr, bp->bp_hwaddr, ETH_ALEN);
181
182     m = m_get(slirp);
183     if (!m) {
184         return;
185     }
186     m->m_data += IF_MAXLINKHDR;
187     rbp = (struct bootp_t *)m->m_data;
188     m->m_data += sizeof(struct udpiphdr);
189     memset(rbp, 0, sizeof(struct bootp_t));
190
191     if (dhcp_msg_type == DHCPDISCOVER) {
192         if (preq_addr.s_addr != htonl(0L)) {
193             bc = request_addr(slirp, &preq_addr, client_ethaddr);
194             if (bc) {
195                 daddr.sin_addr = preq_addr;
196             }
197         }
198         if (!bc) {
199          new_addr:
200             bc = get_new_addr(slirp, &daddr.sin_addr, client_ethaddr);
201             if (!bc) {
202                 DPRINTF("no address left\n");
203                 return;
204             }
205         }
206         memcpy(bc->macaddr, client_ethaddr, ETH_ALEN);
207     } else if (preq_addr.s_addr != htonl(0L)) {
208         bc = request_addr(slirp, &preq_addr, client_ethaddr);
209         if (bc) {
210             daddr.sin_addr = preq_addr;
211             memcpy(bc->macaddr, client_ethaddr, ETH_ALEN);
212         } else {
213             /* DHCPNAKs should be sent to broadcast */
214             daddr.sin_addr.s_addr = 0xffffffff;
215         }
216     } else {
217         bc = find_addr(slirp, &daddr.sin_addr, bp->bp_hwaddr);
218         if (!bc) {
219             /* if never assigned, behaves as if it was already
220                assigned (windows fix because it remembers its address) */
221             goto new_addr;
222         }
223     }
224
225     /* Update ARP table for this IP address */
226     arp_table_add(slirp, daddr.sin_addr.s_addr, client_ethaddr);
227
228     saddr.sin_addr = slirp->vhost_addr;
229     saddr.sin_port = htons(BOOTP_SERVER);
230
231     daddr.sin_port = htons(BOOTP_CLIENT);
232
233     rbp->bp_op = BOOTP_REPLY;
234     rbp->bp_xid = bp->bp_xid;
235     rbp->bp_htype = 1;
236     rbp->bp_hlen = 6;
237     memcpy(rbp->bp_hwaddr, bp->bp_hwaddr, ETH_ALEN);
238
239     rbp->bp_yiaddr = daddr.sin_addr; /* Client IP address */
240     rbp->bp_siaddr = saddr.sin_addr; /* Server IP address */
241
242     q = rbp->bp_vend;
243     memcpy(q, rfc1533_cookie, 4);
244     q += 4;
245
246     if (bc) {
247         DPRINTF("%s addr=%08" PRIx32 "\n",
248                 (dhcp_msg_type == DHCPDISCOVER) ? "offered" : "ack'ed",
249                 ntohl(daddr.sin_addr.s_addr));
250
251         if (dhcp_msg_type == DHCPDISCOVER) {
252             *q++ = RFC2132_MSG_TYPE;
253             *q++ = 1;
254             *q++ = DHCPOFFER;
255         } else /* DHCPREQUEST */ {
256             *q++ = RFC2132_MSG_TYPE;
257             *q++ = 1;
258             *q++ = DHCPACK;
259         }
260
261         if (slirp->bootp_filename)
262             snprintf((char *)rbp->bp_file, sizeof(rbp->bp_file), "%s",
263                      slirp->bootp_filename);
264
265         *q++ = RFC2132_SRV_ID;
266         *q++ = 4;
267         memcpy(q, &saddr.sin_addr, 4);
268         q += 4;
269
270         *q++ = RFC1533_NETMASK;
271         *q++ = 4;
272         memcpy(q, &slirp->vnetwork_mask, 4);
273         q += 4;
274
275         if (!slirp->restricted) {
276             *q++ = RFC1533_GATEWAY;
277             *q++ = 4;
278             memcpy(q, &saddr.sin_addr, 4);
279             q += 4;
280
281             *q++ = RFC1533_DNS;
282             *q++ = 4;
283             memcpy(q, &slirp->vnameserver_addr, 4);
284             q += 4;
285         }
286
287         *q++ = RFC2132_LEASE_TIME;
288         *q++ = 4;
289         val = htonl(LEASE_TIME);
290         memcpy(q, &val, 4);
291         q += 4;
292
293         if (*slirp->client_hostname) {
294             val = strlen(slirp->client_hostname);
295             *q++ = RFC1533_HOSTNAME;
296             *q++ = val;
297             memcpy(q, slirp->client_hostname, val);
298             q += val;
299         }
300
301         if (slirp->vdnssearch) {
302             size_t spaceleft = sizeof(rbp->bp_vend) - (q - rbp->bp_vend);
303             val = slirp->vdnssearch_len;
304             if (val + 1 > spaceleft) {
305                 g_warning("DHCP packet size exceeded, "
306                     "omitting domain-search option.");
307             } else {
308                 memcpy(q, slirp->vdnssearch, val);
309                 q += val;
310             }
311         }
312     } else {
313         static const char nak_msg[] = "requested address not available";
314
315         DPRINTF("nak'ed addr=%08" PRIx32 "\n", ntohl(preq_addr.s_addr));
316
317         *q++ = RFC2132_MSG_TYPE;
318         *q++ = 1;
319         *q++ = DHCPNAK;
320
321         *q++ = RFC2132_MESSAGE;
322         *q++ = sizeof(nak_msg) - 1;
323         memcpy(q, nak_msg, sizeof(nak_msg) - 1);
324         q += sizeof(nak_msg) - 1;
325     }
326     *q = RFC1533_END;
327
328     daddr.sin_addr.s_addr = 0xffffffffu;
329
330     m->m_len = sizeof(struct bootp_t) -
331         sizeof(struct ip) - sizeof(struct udphdr);
332     udp_output(NULL, m, &saddr, &daddr, IPTOS_LOWDELAY);
333 }
334
335 void bootp_input(struct mbuf *m)
336 {
337     struct bootp_t *bp = mtod(m, struct bootp_t *);
338
339     if (bp->bp_op == BOOTP_REQUEST) {
340         bootp_reply(m->slirp, bp);
341     }
342 }
This page took 0.032618 seconds and 2 git commands to generate.