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