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