root / slirp / bootp.c @ d8bcbabf
History | View | Annotate | Download (8.8 kB)
1 | f0cbd3ec | bellard | /*
|
---|---|---|---|
2 | f0cbd3ec | bellard | * QEMU BOOTP/DHCP server
|
3 | 5fafdf24 | ths | *
|
4 | f0cbd3ec | bellard | * Copyright (c) 2004 Fabrice Bellard
|
5 | 5fafdf24 | ths | *
|
6 | f0cbd3ec | bellard | * Permission is hereby granted, free of charge, to any person obtaining a copy
|
7 | f0cbd3ec | bellard | * of this software and associated documentation files (the "Software"), to deal
|
8 | f0cbd3ec | bellard | * in the Software without restriction, including without limitation the rights
|
9 | f0cbd3ec | bellard | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
10 | f0cbd3ec | bellard | * copies of the Software, and to permit persons to whom the Software is
|
11 | f0cbd3ec | bellard | * furnished to do so, subject to the following conditions:
|
12 | f0cbd3ec | bellard | *
|
13 | f0cbd3ec | bellard | * The above copyright notice and this permission notice shall be included in
|
14 | f0cbd3ec | bellard | * all copies or substantial portions of the Software.
|
15 | f0cbd3ec | bellard | *
|
16 | f0cbd3ec | bellard | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
17 | f0cbd3ec | bellard | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
18 | f0cbd3ec | bellard | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
|
19 | f0cbd3ec | bellard | * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
20 | f0cbd3ec | bellard | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
21 | f0cbd3ec | bellard | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
22 | f0cbd3ec | bellard | * THE SOFTWARE.
|
23 | f0cbd3ec | bellard | */
|
24 | f0cbd3ec | bellard | #include <slirp.h> |
25 | f0cbd3ec | bellard | |
26 | f0cbd3ec | bellard | /* XXX: only DHCP is supported */
|
27 | f0cbd3ec | bellard | |
28 | f0cbd3ec | bellard | #define LEASE_TIME (24 * 3600) |
29 | f0cbd3ec | bellard | |
30 | f0cbd3ec | bellard | static const uint8_t rfc1533_cookie[] = { RFC1533_COOKIE }; |
31 | f0cbd3ec | bellard | |
32 | f0cbd3ec | bellard | #ifdef DEBUG
|
33 | 001faf32 | Blue Swirl | #define dprintf(fmt, ...) \
|
34 | 9f349498 | Jan Kiszka | do if (slirp_debug & DBG_CALL) { fprintf(dfd, fmt, ## __VA_ARGS__); fflush(dfd); } while (0) |
35 | f0cbd3ec | bellard | #else
|
36 | 001faf32 | Blue Swirl | #define dprintf(fmt, ...)
|
37 | f0cbd3ec | bellard | #endif
|
38 | f0cbd3ec | bellard | |
39 | 460fec67 | Jan Kiszka | static BOOTPClient *get_new_addr(Slirp *slirp, struct in_addr *paddr, |
40 | 0928a95f | Jan Kiszka | const uint8_t *macaddr)
|
41 | f0cbd3ec | bellard | { |
42 | f0cbd3ec | bellard | BOOTPClient *bc; |
43 | f0cbd3ec | bellard | int i;
|
44 | f0cbd3ec | bellard | |
45 | 460fec67 | Jan Kiszka | for(i = 0; i < NB_BOOTP_CLIENTS; i++) { |
46 | 460fec67 | Jan Kiszka | bc = &slirp->bootp_clients[i]; |
47 | 0928a95f | Jan Kiszka | if (!bc->allocated || !memcmp(macaddr, bc->macaddr, 6)) |
48 | f0cbd3ec | bellard | goto found;
|
49 | f0cbd3ec | bellard | } |
50 | f0cbd3ec | bellard | return NULL; |
51 | f0cbd3ec | bellard | found:
|
52 | 460fec67 | Jan Kiszka | bc = &slirp->bootp_clients[i]; |
53 | f0cbd3ec | bellard | bc->allocated = 1;
|
54 | 460fec67 | Jan Kiszka | paddr->s_addr = slirp->vdhcp_startaddr.s_addr + htonl(i); |
55 | f0cbd3ec | bellard | return bc;
|
56 | f0cbd3ec | bellard | } |
57 | f0cbd3ec | bellard | |
58 | 460fec67 | Jan Kiszka | static BOOTPClient *request_addr(Slirp *slirp, const struct in_addr *paddr, |
59 | b63c7f6b | aliguori | const uint8_t *macaddr)
|
60 | b63c7f6b | aliguori | { |
61 | b63c7f6b | aliguori | uint32_t req_addr = ntohl(paddr->s_addr); |
62 | 460fec67 | Jan Kiszka | uint32_t dhcp_addr = ntohl(slirp->vdhcp_startaddr.s_addr); |
63 | b63c7f6b | aliguori | BOOTPClient *bc; |
64 | b63c7f6b | aliguori | |
65 | a13a4126 | Jan Kiszka | if (req_addr >= dhcp_addr &&
|
66 | 460fec67 | Jan Kiszka | req_addr < (dhcp_addr + NB_BOOTP_CLIENTS)) { |
67 | 460fec67 | Jan Kiszka | bc = &slirp->bootp_clients[req_addr - dhcp_addr]; |
68 | b63c7f6b | aliguori | if (!bc->allocated || !memcmp(macaddr, bc->macaddr, 6)) { |
69 | b63c7f6b | aliguori | bc->allocated = 1;
|
70 | b63c7f6b | aliguori | return bc;
|
71 | b63c7f6b | aliguori | } |
72 | b63c7f6b | aliguori | } |
73 | b63c7f6b | aliguori | return NULL; |
74 | b63c7f6b | aliguori | } |
75 | b63c7f6b | aliguori | |
76 | 460fec67 | Jan Kiszka | static BOOTPClient *find_addr(Slirp *slirp, struct in_addr *paddr, |
77 | 460fec67 | Jan Kiszka | const uint8_t *macaddr)
|
78 | 512176db | bellard | { |
79 | 512176db | bellard | BOOTPClient *bc; |
80 | 512176db | bellard | int i;
|
81 | 512176db | bellard | |
82 | 460fec67 | Jan Kiszka | for(i = 0; i < NB_BOOTP_CLIENTS; i++) { |
83 | 460fec67 | Jan Kiszka | if (!memcmp(macaddr, slirp->bootp_clients[i].macaddr, 6)) |
84 | 512176db | bellard | goto found;
|
85 | 512176db | bellard | } |
86 | 512176db | bellard | return NULL; |
87 | 512176db | bellard | found:
|
88 | 460fec67 | Jan Kiszka | bc = &slirp->bootp_clients[i]; |
89 | 512176db | bellard | bc->allocated = 1;
|
90 | 460fec67 | Jan Kiszka | paddr->s_addr = slirp->vdhcp_startaddr.s_addr + htonl(i); |
91 | 512176db | bellard | return bc;
|
92 | 512176db | bellard | } |
93 | 512176db | bellard | |
94 | b63c7f6b | aliguori | static void dhcp_decode(const struct bootp_t *bp, int *pmsg_type, |
95 | b63c7f6b | aliguori | const struct in_addr **preq_addr) |
96 | f0cbd3ec | bellard | { |
97 | f0cbd3ec | bellard | const uint8_t *p, *p_end;
|
98 | f0cbd3ec | bellard | int len, tag;
|
99 | f0cbd3ec | bellard | |
100 | 3b46e624 | ths | *pmsg_type = 0;
|
101 | b63c7f6b | aliguori | *preq_addr = NULL;
|
102 | f0cbd3ec | bellard | |
103 | b63c7f6b | aliguori | p = bp->bp_vend; |
104 | b63c7f6b | aliguori | p_end = p + DHCP_OPT_LEN; |
105 | f0cbd3ec | bellard | if (memcmp(p, rfc1533_cookie, 4) != 0) |
106 | f0cbd3ec | bellard | return;
|
107 | f0cbd3ec | bellard | p += 4;
|
108 | f0cbd3ec | bellard | while (p < p_end) {
|
109 | f0cbd3ec | bellard | tag = p[0];
|
110 | f0cbd3ec | bellard | if (tag == RFC1533_PAD) {
|
111 | 5fafdf24 | ths | p++; |
112 | f0cbd3ec | bellard | } else if (tag == RFC1533_END) { |
113 | f0cbd3ec | bellard | break;
|
114 | f0cbd3ec | bellard | } else {
|
115 | f0cbd3ec | bellard | p++; |
116 | f0cbd3ec | bellard | if (p >= p_end)
|
117 | f0cbd3ec | bellard | break;
|
118 | f0cbd3ec | bellard | len = *p++; |
119 | b63c7f6b | aliguori | dprintf("dhcp: tag=%d len=%d\n", tag, len);
|
120 | f0cbd3ec | bellard | |
121 | f0cbd3ec | bellard | switch(tag) {
|
122 | f0cbd3ec | bellard | case RFC2132_MSG_TYPE:
|
123 | f0cbd3ec | bellard | if (len >= 1) |
124 | f0cbd3ec | bellard | *pmsg_type = p[0];
|
125 | f0cbd3ec | bellard | break;
|
126 | b63c7f6b | aliguori | case RFC2132_REQ_ADDR:
|
127 | b63c7f6b | aliguori | if (len >= 4) |
128 | b63c7f6b | aliguori | *preq_addr = (struct in_addr *)p;
|
129 | b63c7f6b | aliguori | break;
|
130 | f0cbd3ec | bellard | default:
|
131 | f0cbd3ec | bellard | break;
|
132 | f0cbd3ec | bellard | } |
133 | f0cbd3ec | bellard | p += len; |
134 | f0cbd3ec | bellard | } |
135 | f0cbd3ec | bellard | } |
136 | b63c7f6b | aliguori | if (*pmsg_type == DHCPREQUEST && !*preq_addr && bp->bp_ciaddr.s_addr) {
|
137 | b63c7f6b | aliguori | *preq_addr = &bp->bp_ciaddr; |
138 | b63c7f6b | aliguori | } |
139 | f0cbd3ec | bellard | } |
140 | f0cbd3ec | bellard | |
141 | 460fec67 | Jan Kiszka | static void bootp_reply(Slirp *slirp, const struct bootp_t *bp) |
142 | f0cbd3ec | bellard | { |
143 | b63c7f6b | aliguori | BOOTPClient *bc = NULL;
|
144 | f0cbd3ec | bellard | struct mbuf *m;
|
145 | f0cbd3ec | bellard | struct bootp_t *rbp;
|
146 | f0cbd3ec | bellard | struct sockaddr_in saddr, daddr;
|
147 | b63c7f6b | aliguori | const struct in_addr *preq_addr; |
148 | f0cbd3ec | bellard | int dhcp_msg_type, val;
|
149 | f0cbd3ec | bellard | uint8_t *q; |
150 | f0cbd3ec | bellard | |
151 | f0cbd3ec | bellard | /* extract exact DHCP msg type */
|
152 | b63c7f6b | aliguori | dhcp_decode(bp, &dhcp_msg_type, &preq_addr); |
153 | b63c7f6b | aliguori | dprintf("bootp packet op=%d msgtype=%d", bp->bp_op, dhcp_msg_type);
|
154 | b63c7f6b | aliguori | if (preq_addr)
|
155 | b63c7f6b | aliguori | dprintf(" req_addr=%08x\n", ntohl(preq_addr->s_addr));
|
156 | b63c7f6b | aliguori | else
|
157 | b63c7f6b | aliguori | dprintf("\n");
|
158 | 3b46e624 | ths | |
159 | 487be8a1 | bellard | if (dhcp_msg_type == 0) |
160 | 487be8a1 | bellard | dhcp_msg_type = DHCPREQUEST; /* Force reply for old BOOTP clients */
|
161 | 3b46e624 | ths | |
162 | 5fafdf24 | ths | if (dhcp_msg_type != DHCPDISCOVER &&
|
163 | f0cbd3ec | bellard | dhcp_msg_type != DHCPREQUEST) |
164 | f0cbd3ec | bellard | return;
|
165 | f0cbd3ec | bellard | /* XXX: this is a hack to get the client mac address */
|
166 | 460fec67 | Jan Kiszka | memcpy(slirp->client_ethaddr, bp->bp_hwaddr, 6);
|
167 | 3b46e624 | ths | |
168 | 460fec67 | Jan Kiszka | m = m_get(slirp); |
169 | 460fec67 | Jan Kiszka | if (!m) {
|
170 | f0cbd3ec | bellard | return;
|
171 | 460fec67 | Jan Kiszka | } |
172 | 9634d903 | blueswir1 | m->m_data += IF_MAXLINKHDR; |
173 | f0cbd3ec | bellard | rbp = (struct bootp_t *)m->m_data;
|
174 | f0cbd3ec | bellard | m->m_data += sizeof(struct udpiphdr); |
175 | f0cbd3ec | bellard | memset(rbp, 0, sizeof(struct bootp_t)); |
176 | f0cbd3ec | bellard | |
177 | 512176db | bellard | if (dhcp_msg_type == DHCPDISCOVER) {
|
178 | b63c7f6b | aliguori | if (preq_addr) {
|
179 | 460fec67 | Jan Kiszka | bc = request_addr(slirp, preq_addr, slirp->client_ethaddr); |
180 | b63c7f6b | aliguori | if (bc) {
|
181 | b63c7f6b | aliguori | daddr.sin_addr = *preq_addr; |
182 | b63c7f6b | aliguori | } |
183 | b63c7f6b | aliguori | } |
184 | 512176db | bellard | if (!bc) {
|
185 | b63c7f6b | aliguori | new_addr:
|
186 | 460fec67 | Jan Kiszka | bc = get_new_addr(slirp, &daddr.sin_addr, slirp->client_ethaddr); |
187 | b63c7f6b | aliguori | if (!bc) {
|
188 | b63c7f6b | aliguori | dprintf("no address left\n");
|
189 | b63c7f6b | aliguori | return;
|
190 | b63c7f6b | aliguori | } |
191 | 512176db | bellard | } |
192 | 460fec67 | Jan Kiszka | memcpy(bc->macaddr, slirp->client_ethaddr, 6);
|
193 | b63c7f6b | aliguori | } else if (preq_addr) { |
194 | 460fec67 | Jan Kiszka | bc = request_addr(slirp, preq_addr, slirp->client_ethaddr); |
195 | b63c7f6b | aliguori | if (bc) {
|
196 | b63c7f6b | aliguori | daddr.sin_addr = *preq_addr; |
197 | 460fec67 | Jan Kiszka | memcpy(bc->macaddr, slirp->client_ethaddr, 6);
|
198 | b63c7f6b | aliguori | } else {
|
199 | b63c7f6b | aliguori | daddr.sin_addr.s_addr = 0;
|
200 | b63c7f6b | aliguori | } |
201 | 512176db | bellard | } else {
|
202 | 460fec67 | Jan Kiszka | bc = find_addr(slirp, &daddr.sin_addr, bp->bp_hwaddr); |
203 | 512176db | bellard | if (!bc) {
|
204 | d981b883 | bellard | /* if never assigned, behaves as if it was already
|
205 | d981b883 | bellard | assigned (windows fix because it remembers its address) */
|
206 | d981b883 | bellard | goto new_addr;
|
207 | 512176db | bellard | } |
208 | f0cbd3ec | bellard | } |
209 | 47d5d01a | ths | |
210 | 460fec67 | Jan Kiszka | saddr.sin_addr = slirp->vhost_addr; |
211 | f0cbd3ec | bellard | saddr.sin_port = htons(BOOTP_SERVER); |
212 | f0cbd3ec | bellard | |
213 | f0cbd3ec | bellard | daddr.sin_port = htons(BOOTP_CLIENT); |
214 | f0cbd3ec | bellard | |
215 | f0cbd3ec | bellard | rbp->bp_op = BOOTP_REPLY; |
216 | f0cbd3ec | bellard | rbp->bp_xid = bp->bp_xid; |
217 | f0cbd3ec | bellard | rbp->bp_htype = 1;
|
218 | f0cbd3ec | bellard | rbp->bp_hlen = 6;
|
219 | f0cbd3ec | bellard | memcpy(rbp->bp_hwaddr, bp->bp_hwaddr, 6);
|
220 | f0cbd3ec | bellard | |
221 | e95c8d51 | bellard | rbp->bp_yiaddr = daddr.sin_addr; /* Client IP address */
|
222 | e95c8d51 | bellard | rbp->bp_siaddr = saddr.sin_addr; /* Server IP address */
|
223 | f0cbd3ec | bellard | |
224 | f0cbd3ec | bellard | q = rbp->bp_vend; |
225 | f0cbd3ec | bellard | memcpy(q, rfc1533_cookie, 4);
|
226 | f0cbd3ec | bellard | q += 4;
|
227 | f0cbd3ec | bellard | |
228 | b63c7f6b | aliguori | if (bc) {
|
229 | b63c7f6b | aliguori | dprintf("%s addr=%08x\n",
|
230 | b63c7f6b | aliguori | (dhcp_msg_type == DHCPDISCOVER) ? "offered" : "ack'ed", |
231 | b63c7f6b | aliguori | ntohl(daddr.sin_addr.s_addr)); |
232 | b63c7f6b | aliguori | |
233 | b63c7f6b | aliguori | if (dhcp_msg_type == DHCPDISCOVER) {
|
234 | b63c7f6b | aliguori | *q++ = RFC2132_MSG_TYPE; |
235 | b63c7f6b | aliguori | *q++ = 1;
|
236 | b63c7f6b | aliguori | *q++ = DHCPOFFER; |
237 | b63c7f6b | aliguori | } else /* DHCPREQUEST */ { |
238 | b63c7f6b | aliguori | *q++ = RFC2132_MSG_TYPE; |
239 | b63c7f6b | aliguori | *q++ = 1;
|
240 | b63c7f6b | aliguori | *q++ = DHCPACK; |
241 | b63c7f6b | aliguori | } |
242 | b63c7f6b | aliguori | |
243 | 460fec67 | Jan Kiszka | if (slirp->bootp_filename)
|
244 | b63c7f6b | aliguori | snprintf((char *)rbp->bp_file, sizeof(rbp->bp_file), "%s", |
245 | 460fec67 | Jan Kiszka | slirp->bootp_filename); |
246 | 3b46e624 | ths | |
247 | f0cbd3ec | bellard | *q++ = RFC2132_SRV_ID; |
248 | f0cbd3ec | bellard | *q++ = 4;
|
249 | f0cbd3ec | bellard | memcpy(q, &saddr.sin_addr, 4);
|
250 | f0cbd3ec | bellard | q += 4;
|
251 | f0cbd3ec | bellard | |
252 | f0cbd3ec | bellard | *q++ = RFC1533_NETMASK; |
253 | f0cbd3ec | bellard | *q++ = 4;
|
254 | 460fec67 | Jan Kiszka | memcpy(q, &slirp->vnetwork_mask, 4);
|
255 | a13a4126 | Jan Kiszka | q += 4;
|
256 | 3b46e624 | ths | |
257 | 460fec67 | Jan Kiszka | if (!slirp->restricted) {
|
258 | a9ba3a85 | aliguori | *q++ = RFC1533_GATEWAY; |
259 | a9ba3a85 | aliguori | *q++ = 4;
|
260 | a9ba3a85 | aliguori | memcpy(q, &saddr.sin_addr, 4);
|
261 | a9ba3a85 | aliguori | q += 4;
|
262 | a9ba3a85 | aliguori | |
263 | a9ba3a85 | aliguori | *q++ = RFC1533_DNS; |
264 | a9ba3a85 | aliguori | *q++ = 4;
|
265 | 460fec67 | Jan Kiszka | memcpy(q, &slirp->vnameserver_addr, 4);
|
266 | a9ba3a85 | aliguori | q += 4;
|
267 | a9ba3a85 | aliguori | } |
268 | f0cbd3ec | bellard | |
269 | f0cbd3ec | bellard | *q++ = RFC2132_LEASE_TIME; |
270 | f0cbd3ec | bellard | *q++ = 4;
|
271 | f0cbd3ec | bellard | val = htonl(LEASE_TIME); |
272 | f0cbd3ec | bellard | memcpy(q, &val, 4);
|
273 | f0cbd3ec | bellard | q += 4;
|
274 | 115defd1 | pbrook | |
275 | 460fec67 | Jan Kiszka | if (*slirp->client_hostname) {
|
276 | 460fec67 | Jan Kiszka | val = strlen(slirp->client_hostname); |
277 | 115defd1 | pbrook | *q++ = RFC1533_HOSTNAME; |
278 | 115defd1 | pbrook | *q++ = val; |
279 | 460fec67 | Jan Kiszka | memcpy(q, slirp->client_hostname, val); |
280 | 115defd1 | pbrook | q += val; |
281 | 115defd1 | pbrook | } |
282 | b63c7f6b | aliguori | } else {
|
283 | b63c7f6b | aliguori | static const char nak_msg[] = "requested address not available"; |
284 | b63c7f6b | aliguori | |
285 | b63c7f6b | aliguori | dprintf("nak'ed addr=%08x\n", ntohl(preq_addr->s_addr));
|
286 | b63c7f6b | aliguori | |
287 | b63c7f6b | aliguori | *q++ = RFC2132_MSG_TYPE; |
288 | b63c7f6b | aliguori | *q++ = 1;
|
289 | b63c7f6b | aliguori | *q++ = DHCPNAK; |
290 | b63c7f6b | aliguori | |
291 | b63c7f6b | aliguori | *q++ = RFC2132_MESSAGE; |
292 | b63c7f6b | aliguori | *q++ = sizeof(nak_msg) - 1; |
293 | b63c7f6b | aliguori | memcpy(q, nak_msg, sizeof(nak_msg) - 1); |
294 | b63c7f6b | aliguori | q += sizeof(nak_msg) - 1; |
295 | f0cbd3ec | bellard | } |
296 | f0cbd3ec | bellard | *q++ = RFC1533_END; |
297 | 3b46e624 | ths | |
298 | b63c7f6b | aliguori | daddr.sin_addr.s_addr = 0xffffffffu;
|
299 | b63c7f6b | aliguori | |
300 | 5fafdf24 | ths | m->m_len = sizeof(struct bootp_t) - |
301 | 44bbf73f | bellard | sizeof(struct ip) - sizeof(struct udphdr); |
302 | f0cbd3ec | bellard | udp_output2(NULL, m, &saddr, &daddr, IPTOS_LOWDELAY);
|
303 | f0cbd3ec | bellard | } |
304 | f0cbd3ec | bellard | |
305 | f0cbd3ec | bellard | void bootp_input(struct mbuf *m) |
306 | f0cbd3ec | bellard | { |
307 | 101c5935 | bellard | struct bootp_t *bp = mtod(m, struct bootp_t *); |
308 | f0cbd3ec | bellard | |
309 | f0cbd3ec | bellard | if (bp->bp_op == BOOTP_REQUEST) {
|
310 | 460fec67 | Jan Kiszka | bootp_reply(m->slirp, bp); |
311 | f0cbd3ec | bellard | } |
312 | f0cbd3ec | bellard | } |