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