root / slirp / bootp.c @ fcc941fe
History | View | Annotate | Download (6.2 kB)
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 <slirp.h> |
25 |
|
26 |
/* XXX: only DHCP is supported */
|
27 |
|
28 |
#define NB_ADDR 16 |
29 |
|
30 |
#define START_ADDR 15 |
31 |
|
32 |
#define LEASE_TIME (24 * 3600) |
33 |
|
34 |
typedef struct { |
35 |
uint8_t allocated; |
36 |
uint8_t macaddr[6];
|
37 |
} BOOTPClient; |
38 |
|
39 |
BOOTPClient bootp_clients[NB_ADDR]; |
40 |
|
41 |
static const uint8_t rfc1533_cookie[] = { RFC1533_COOKIE }; |
42 |
|
43 |
#ifdef DEBUG
|
44 |
#define dprintf(fmt, args...) \
|
45 |
if (slirp_debug & DBG_CALL) { fprintf(dfd, fmt, ## args); fflush(dfd); } |
46 |
#else
|
47 |
#define dprintf(fmt, args...)
|
48 |
#endif
|
49 |
|
50 |
static BOOTPClient *get_new_addr(struct in_addr *paddr) |
51 |
{ |
52 |
BOOTPClient *bc; |
53 |
int i;
|
54 |
|
55 |
for(i = 0; i < NB_ADDR; i++) { |
56 |
if (!bootp_clients[i].allocated)
|
57 |
goto found;
|
58 |
} |
59 |
return NULL; |
60 |
found:
|
61 |
bc = &bootp_clients[i]; |
62 |
bc->allocated = 1;
|
63 |
paddr->s_addr = htonl(ntohl(special_addr.s_addr) | (i + START_ADDR)); |
64 |
return bc;
|
65 |
} |
66 |
|
67 |
static BOOTPClient *find_addr(struct in_addr *paddr, const uint8_t *macaddr) |
68 |
{ |
69 |
BOOTPClient *bc; |
70 |
int i;
|
71 |
|
72 |
for(i = 0; i < NB_ADDR; i++) { |
73 |
if (!memcmp(macaddr, bootp_clients[i].macaddr, 6)) |
74 |
goto found;
|
75 |
} |
76 |
return NULL; |
77 |
found:
|
78 |
bc = &bootp_clients[i]; |
79 |
bc->allocated = 1;
|
80 |
paddr->s_addr = htonl(ntohl(special_addr.s_addr) | (i + START_ADDR)); |
81 |
return bc;
|
82 |
} |
83 |
|
84 |
static void dhcp_decode(const uint8_t *buf, int size, |
85 |
int *pmsg_type)
|
86 |
{ |
87 |
const uint8_t *p, *p_end;
|
88 |
int len, tag;
|
89 |
|
90 |
*pmsg_type = 0;
|
91 |
|
92 |
p = buf; |
93 |
p_end = buf + size; |
94 |
if (size < 5) |
95 |
return;
|
96 |
if (memcmp(p, rfc1533_cookie, 4) != 0) |
97 |
return;
|
98 |
p += 4;
|
99 |
while (p < p_end) {
|
100 |
tag = p[0];
|
101 |
if (tag == RFC1533_PAD) {
|
102 |
p++; |
103 |
} else if (tag == RFC1533_END) { |
104 |
break;
|
105 |
} else {
|
106 |
p++; |
107 |
if (p >= p_end)
|
108 |
break;
|
109 |
len = *p++; |
110 |
dprintf("dhcp: tag=0x%02x len=%d\n", tag, len);
|
111 |
|
112 |
switch(tag) {
|
113 |
case RFC2132_MSG_TYPE:
|
114 |
if (len >= 1) |
115 |
*pmsg_type = p[0];
|
116 |
break;
|
117 |
default:
|
118 |
break;
|
119 |
} |
120 |
p += len; |
121 |
} |
122 |
} |
123 |
} |
124 |
|
125 |
static void bootp_reply(struct bootp_t *bp) |
126 |
{ |
127 |
BOOTPClient *bc; |
128 |
struct mbuf *m;
|
129 |
struct bootp_t *rbp;
|
130 |
struct sockaddr_in saddr, daddr;
|
131 |
struct in_addr dns_addr;
|
132 |
int dhcp_msg_type, val;
|
133 |
uint8_t *q; |
134 |
|
135 |
/* extract exact DHCP msg type */
|
136 |
dhcp_decode(bp->bp_vend, DHCP_OPT_LEN, &dhcp_msg_type); |
137 |
dprintf("bootp packet op=%d msgtype=%d\n", bp->bp_op, dhcp_msg_type);
|
138 |
|
139 |
if (dhcp_msg_type != DHCPDISCOVER &&
|
140 |
dhcp_msg_type != DHCPREQUEST) |
141 |
return;
|
142 |
/* XXX: this is a hack to get the client mac address */
|
143 |
memcpy(client_ethaddr, bp->bp_hwaddr, 6);
|
144 |
|
145 |
if ((m = m_get()) == NULL) |
146 |
return;
|
147 |
m->m_data += if_maxlinkhdr; |
148 |
rbp = (struct bootp_t *)m->m_data;
|
149 |
m->m_data += sizeof(struct udpiphdr); |
150 |
memset(rbp, 0, sizeof(struct bootp_t)); |
151 |
|
152 |
if (dhcp_msg_type == DHCPDISCOVER) {
|
153 |
bc = get_new_addr(&daddr.sin_addr); |
154 |
if (!bc) {
|
155 |
dprintf("no address left\n");
|
156 |
return;
|
157 |
} |
158 |
memcpy(bc->macaddr, client_ethaddr, 6);
|
159 |
} else {
|
160 |
bc = find_addr(&daddr.sin_addr, bp->bp_hwaddr); |
161 |
if (!bc) {
|
162 |
dprintf("no address assigned\n");
|
163 |
return;
|
164 |
} |
165 |
} |
166 |
dprintf("offered addr=%08x\n", ntohl(daddr.sin_addr.s_addr));
|
167 |
|
168 |
saddr.sin_addr.s_addr = htonl(ntohl(special_addr.s_addr) | CTL_ALIAS); |
169 |
saddr.sin_port = htons(BOOTP_SERVER); |
170 |
|
171 |
daddr.sin_port = htons(BOOTP_CLIENT); |
172 |
|
173 |
rbp->bp_op = BOOTP_REPLY; |
174 |
rbp->bp_xid = bp->bp_xid; |
175 |
rbp->bp_htype = 1;
|
176 |
rbp->bp_hlen = 6;
|
177 |
memcpy(rbp->bp_hwaddr, bp->bp_hwaddr, 6);
|
178 |
|
179 |
rbp->bp_yiaddr = daddr.sin_addr; /* IP address */
|
180 |
|
181 |
q = rbp->bp_vend; |
182 |
memcpy(q, rfc1533_cookie, 4);
|
183 |
q += 4;
|
184 |
|
185 |
if (dhcp_msg_type == DHCPDISCOVER) {
|
186 |
*q++ = RFC2132_MSG_TYPE; |
187 |
*q++ = 1;
|
188 |
*q++ = DHCPOFFER; |
189 |
} else if (dhcp_msg_type == DHCPREQUEST) { |
190 |
*q++ = RFC2132_MSG_TYPE; |
191 |
*q++ = 1;
|
192 |
*q++ = DHCPACK; |
193 |
} |
194 |
|
195 |
if (dhcp_msg_type == DHCPDISCOVER ||
|
196 |
dhcp_msg_type == DHCPREQUEST) { |
197 |
*q++ = RFC2132_SRV_ID; |
198 |
*q++ = 4;
|
199 |
memcpy(q, &saddr.sin_addr, 4);
|
200 |
q += 4;
|
201 |
|
202 |
*q++ = RFC1533_NETMASK; |
203 |
*q++ = 4;
|
204 |
*q++ = 0xff;
|
205 |
*q++ = 0xff;
|
206 |
*q++ = 0xff;
|
207 |
*q++ = 0x00;
|
208 |
|
209 |
*q++ = RFC1533_GATEWAY; |
210 |
*q++ = 4;
|
211 |
memcpy(q, &saddr.sin_addr, 4);
|
212 |
q += 4;
|
213 |
|
214 |
*q++ = RFC1533_DNS; |
215 |
*q++ = 4;
|
216 |
dns_addr.s_addr = htonl(ntohl(special_addr.s_addr) | CTL_DNS); |
217 |
memcpy(q, &dns_addr, 4);
|
218 |
q += 4;
|
219 |
|
220 |
*q++ = RFC2132_LEASE_TIME; |
221 |
*q++ = 4;
|
222 |
val = htonl(LEASE_TIME); |
223 |
memcpy(q, &val, 4);
|
224 |
q += 4;
|
225 |
} |
226 |
*q++ = RFC1533_END; |
227 |
|
228 |
m->m_len = sizeof(struct bootp_t) - |
229 |
sizeof(struct ip) - sizeof(struct udphdr); |
230 |
udp_output2(NULL, m, &saddr, &daddr, IPTOS_LOWDELAY);
|
231 |
} |
232 |
|
233 |
void bootp_input(struct mbuf *m) |
234 |
{ |
235 |
struct bootp_t *bp = (struct bootp_t *)m->m_data; |
236 |
|
237 |
if (bp->bp_op == BOOTP_REQUEST) {
|
238 |
bootp_reply(bp); |
239 |
} |
240 |
} |