upgraded to vde 2.1.0
[vde.git] / vde-2 / slirpvde / bootp.c
blobb08dae4a671911f96eb69471be1a8282699ae1e8
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:
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
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.
24 #include <config.h>
25 #include <slirp.h>
27 /* XXX: only DHCP is supported */
29 #define NB_ADDR 16
31 #define START_ADDR 15
33 #define LEASE_TIME (120)
35 typedef struct {
36 uint8_t allocated;
37 uint8_t macaddr[6];
38 int time;
39 } BOOTPClient;
41 BOOTPClient bootp_clients[NB_ADDR];
43 static const uint8_t rfc1533_cookie[] = { RFC1533_COOKIE };
45 #ifdef DEBUG
46 #define dprintf(fmt, args...) \
47 if (slirp_debug & DBG_CALL) { fprintf(dfd, fmt, ## args); fflush(dfd); }
48 #else
49 #define dprintf(fmt, args...)
50 #endif
52 static BOOTPClient *get_new_addr(struct in_addr *paddr)
54 BOOTPClient *bc;
55 int i;
56 int now=time(NULL);
58 for(i = 0; i < NB_ADDR; i++) {
59 if (!bootp_clients[i].allocated)
60 goto found;
62 for(i = 0; i < NB_ADDR; i++) {
63 if (now-bootp_clients[i].time > 3*LEASE_TIME)
64 goto found;
66 return NULL;
67 found:
68 bc = &bootp_clients[i];
69 paddr->s_addr = htonl(ntohl(special_addr.s_addr) | (i + START_ADDR));
70 return bc;
73 static BOOTPClient *find_addr(struct in_addr *paddr, const uint8_t *macaddr)
75 BOOTPClient *bc;
76 int i;
78 for(i = 0; i < NB_ADDR; i++) {
79 if (!memcmp(macaddr, bootp_clients[i].macaddr, 6))
80 goto found;
82 return NULL;
83 found:
84 bc = &bootp_clients[i];
85 paddr->s_addr = htonl(ntohl(special_addr.s_addr) | (i + START_ADDR));
86 return bc;
89 static BOOTPClient *find_reqaddr(struct in_addr *paddr, struct in_addr *reqaddr, const uint8_t *macaddr)
91 BOOTPClient *bc=NULL;
92 int i;
93 /*check the net prefix*/
94 if ((ntohl(reqaddr->s_addr) & 0xffffff00) ==
95 (ntohl(special_addr.s_addr) & 0xffffff00)) {
96 i=(ntohl(reqaddr->s_addr) & 0xff) - START_ADDR;
97 if (i>=0 && i< NB_ADDR) {
98 bc = &bootp_clients[i];
99 if (bc->allocated &&
100 (memcmp(macaddr, bootp_clients[i].macaddr, 6)==0)) {
101 paddr->s_addr = reqaddr->s_addr;
102 return bc;
104 else
105 bc=NULL;
108 return bc;
111 static void dhcp_decode(const uint8_t *buf, int size,
112 int *pmsg_type, struct sockaddr_in *preqaddr)
114 const uint8_t *p, *p_end;
115 int len, tag;
117 *pmsg_type = 0;
118 preqaddr->sin_addr.s_addr=htonl(0L);
120 p = buf;
121 p_end = buf + size;
122 if (size < 5)
123 return;
124 if (memcmp(p, rfc1533_cookie, 4) != 0)
125 return;
126 p += 4;
127 while (p < p_end) {
128 tag = p[0];
129 if (tag == RFC1533_PAD) {
130 p++;
131 } else if (tag == RFC1533_END) {
132 break;
133 } else {
134 p++;
135 if (p >= p_end)
136 break;
137 len = *p++;
138 dprintf("dhcp: tag=0x%02x len=%d\n", tag, len);
140 switch(tag) {
141 case RFC2132_MSG_TYPE:
142 if (len >= 1)
143 *pmsg_type = p[0];
144 break;
145 case RFC2132_REQ_ADDR:
146 if (len == 4) {
147 memcpy(&(preqaddr->sin_addr),p,4);
149 default:
150 break;
152 p += len;
157 static void bootp_reply(struct bootp_t *bp)
159 BOOTPClient *bc;
160 struct mbuf *m;
161 struct bootp_t *rbp;
162 struct sockaddr_in saddr, daddr, reqaddr;
163 struct in_addr dns_addr;
164 int dhcp_msg_type, val;
165 uint8_t *q,replytype;
166 uint8_t client_ethaddr[6];
168 /* extract exact DHCP msg type */
169 dhcp_decode(bp->bp_vend, DHCP_OPT_LEN, &dhcp_msg_type,&reqaddr);
170 dprintf("bootp packet op=%d msgtype=%d reqaddr=%x\n", bp->bp_op, dhcp_msg_type,ntohl(reqaddr.sin_addr.s_addr));
172 if (dhcp_msg_type != DHCPDISCOVER &&
173 dhcp_msg_type != DHCPREQUEST)
174 return;
175 /* XXX: this is a hack to get the client mac address */
176 memcpy(client_ethaddr, bp->bp_hwaddr, 6);
178 if ((m = m_get()) == NULL)
179 return;
180 m->m_data += if_maxlinkhdr;
181 rbp = (struct bootp_t *)m->m_data;
182 m->m_data += sizeof(struct udpiphdr);
183 memset(rbp, 0, sizeof(struct bootp_t));
185 bc=NULL;
186 daddr.sin_addr.s_addr=htonl(0L);
187 if (dhcp_msg_type == DHCPREQUEST) {
188 if (reqaddr.sin_addr.s_addr != htonl(0L))
189 bc = find_reqaddr(&daddr.sin_addr, &reqaddr.sin_addr, bp->bp_hwaddr);
190 else
191 bc = find_addr(&daddr.sin_addr, bp->bp_hwaddr);
193 else if (dhcp_msg_type == DHCPDISCOVER) {
194 bc = find_addr(&daddr.sin_addr, bp->bp_hwaddr);
195 if (!bc)
196 bc = get_new_addr(&daddr.sin_addr);
199 dprintf("offered addr=%08x\n", ntohl(daddr.sin_addr.s_addr));
201 saddr.sin_addr.s_addr = htonl(ntohl(special_addr.s_addr) | CTL_ALIAS);
202 saddr.sin_port = htons(BOOTP_SERVER);
204 daddr.sin_port = htons(BOOTP_CLIENT);
206 rbp->bp_op = BOOTP_REPLY;
207 rbp->bp_xid = bp->bp_xid;
208 rbp->bp_htype = 1;
209 rbp->bp_hlen = 6;
210 memcpy(rbp->bp_hwaddr, bp->bp_hwaddr, 6);
212 rbp->bp_yiaddr = daddr.sin_addr; /* IP address */
213 rbp->bp_siaddr = saddr.sin_addr; /* IP address */
215 q = rbp->bp_vend;
216 memcpy(q, rfc1533_cookie, 4);
217 q += 4;
219 if (bc != NULL) {
220 memcpy(bc->macaddr, client_ethaddr, 6);
221 bc->allocated = 1;
222 bc->time = time(NULL);
223 replytype=(dhcp_msg_type == DHCPDISCOVER)?DHCPOFFER:DHCPACK;
225 else
226 replytype=DHCPNACK;
228 *q++ = RFC2132_MSG_TYPE;
229 *q++ = 1;
230 *q++ = replytype;
232 if ((dhcp_msg_type == DHCPDISCOVER ||
233 dhcp_msg_type == DHCPREQUEST) && replytype!=DHCPNACK) {
234 *q++ = RFC2132_SRV_ID;
235 *q++ = 4;
236 memcpy(q, &saddr.sin_addr, 4);
237 q += 4;
239 *q++ = RFC1533_NETMASK;
240 *q++ = 4;
241 *q++ = 0xff;
242 *q++ = 0xff;
243 *q++ = 0xff;
244 *q++ = 0x00;
246 *q++ = RFC1533_GATEWAY;
247 *q++ = 4;
248 memcpy(q, &saddr.sin_addr, 4);
249 q += 4;
251 *q++ = RFC1533_DNS;
252 *q++ = 4;
253 dns_addr.s_addr = htonl(ntohl(special_addr.s_addr) | CTL_DNS);
254 memcpy(q, &dns_addr, 4);
255 q += 4;
257 *q++ = RFC2132_LEASE_TIME;
258 *q++ = 4;
259 val = htonl(LEASE_TIME);
260 memcpy(q, &val, 4);
261 q += 4;
263 *q++ = RFC1533_END;
265 //m->m_len = sizeof(struct bootp_t);
266 m->m_len = q - (uint8_t *) (m->m_data);
267 client_eth_register(client_ethaddr,&daddr.sin_addr);
268 udp_output2(NULL, m, &saddr, &daddr, IPTOS_LOWDELAY);
271 void bootp_input(struct mbuf *m)
273 struct bootp_t *bp = (struct bootp_t *)m->m_data;
275 if (bp->bp_op == BOOTP_REQUEST) {
276 bootp_reply(bp);