AF_IPN is no longer protocol #34 (assigned to AF_ISDN).
[vde.git] / vde-2 / src / slirpvde / bootp.c
blob9cbd3d998b25bb17588221d80923d031dcaa334e
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 "slirp.h"
26 #include <config.h>
27 #include <vde.h>
28 #include <vdecommon.h>
30 /* XXX: only DHCP is supported */
32 #define NB_ADDR 16
34 #define START_ADDR 15
36 #define LEASE_TIME (120)
38 typedef struct {
39 uint8_t allocated;
40 uint8_t macaddr[6];
41 int time;
42 } BOOTPClient;
44 BOOTPClient bootp_clients[NB_ADDR];
46 static const uint8_t rfc1533_cookie[] = { RFC1533_COOKIE };
48 #ifdef DEBUG
49 #define dprintf(fmt, args...) \
50 if (slirp_debug & DBG_CALL) { fprintf(dfd, fmt, ## args); fflush(dfd); }
51 #else
52 #define dprintf(fmt, args...)
53 #endif
55 static BOOTPClient *get_new_addr(struct in_addr *paddr)
57 BOOTPClient *bc;
58 int i;
59 int now=time(NULL);
61 for(i = 0; i < NB_ADDR; i++) {
62 if (!bootp_clients[i].allocated)
63 goto found;
65 for(i = 0; i < NB_ADDR; i++) {
66 if (now-bootp_clients[i].time > 3*LEASE_TIME)
67 goto found;
69 return NULL;
70 found:
71 bc = &bootp_clients[i];
72 paddr->s_addr = htonl(ntohl(special_addr.s_addr) | (i + START_ADDR));
73 return bc;
76 static BOOTPClient *find_addr(struct in_addr *paddr, const uint8_t *macaddr)
78 BOOTPClient *bc;
79 int i;
81 for(i = 0; i < NB_ADDR; i++) {
82 if (!memcmp(macaddr, bootp_clients[i].macaddr, 6))
83 goto found;
85 return NULL;
86 found:
87 bc = &bootp_clients[i];
88 paddr->s_addr = htonl(ntohl(special_addr.s_addr) | (i + START_ADDR));
89 return bc;
92 static BOOTPClient *find_reqaddr(struct in_addr *paddr, struct in_addr *reqaddr, const uint8_t *macaddr)
94 BOOTPClient *bc=NULL;
95 int i;
96 /*check the net prefix*/
97 if ((ntohl(reqaddr->s_addr) & 0xffffff00) ==
98 (ntohl(special_addr.s_addr) & 0xffffff00)) {
99 i=(ntohl(reqaddr->s_addr) & 0xff) - START_ADDR;
100 if (i>=0 && i< NB_ADDR) {
101 bc = &bootp_clients[i];
102 if (bc->allocated &&
103 (memcmp(macaddr, bootp_clients[i].macaddr, 6)==0)) {
104 paddr->s_addr = reqaddr->s_addr;
105 return bc;
107 else
108 bc=NULL;
111 return bc;
114 static void dhcp_decode(const uint8_t *buf, int size,
115 int *pmsg_type, struct sockaddr_in *preqaddr)
117 const uint8_t *p, *p_end;
118 int len, tag;
120 *pmsg_type = 0;
121 preqaddr->sin_addr.s_addr=htonl(0L);
123 p = buf;
124 p_end = buf + size;
125 if (size < 5)
126 return;
127 if (memcmp(p, rfc1533_cookie, 4) != 0)
128 return;
129 p += 4;
130 while (p < p_end) {
131 tag = p[0];
132 if (tag == RFC1533_PAD) {
133 p++;
134 } else if (tag == RFC1533_END) {
135 break;
136 } else {
137 p++;
138 if (p >= p_end)
139 break;
140 len = *p++;
141 dprintf("dhcp: tag=0x%02x len=%d\n", tag, len);
143 switch(tag) {
144 case RFC2132_MSG_TYPE:
145 if (len >= 1)
146 *pmsg_type = p[0];
147 break;
148 case RFC2132_REQ_ADDR:
149 if (len == 4) {
150 memcpy(&(preqaddr->sin_addr),p,4);
152 default:
153 break;
155 p += len;
160 static void bootp_reply(struct bootp_t *bp)
162 BOOTPClient *bc;
163 struct mbuf *m;
164 struct bootp_t *rbp;
165 struct sockaddr_in saddr, daddr, reqaddr;
166 struct in_addr dns_addr;
167 int dhcp_msg_type, val;
168 uint8_t *q,replytype;
169 uint8_t client_ethaddr[6];
171 /* extract exact DHCP msg type */
172 dhcp_decode(bp->bp_vend, DHCP_OPT_LEN, &dhcp_msg_type,&reqaddr);
173 dprintf("bootp packet op=%d msgtype=%d reqaddr=%x\n", bp->bp_op, dhcp_msg_type,ntohl(reqaddr.sin_addr.s_addr));
175 if (dhcp_msg_type != DHCPDISCOVER &&
176 dhcp_msg_type != DHCPREQUEST)
177 return;
178 /* XXX: this is a hack to get the client mac address */
179 memcpy(client_ethaddr, bp->bp_hwaddr, 6);
181 if ((m = m_get()) == NULL)
182 return;
183 m->m_data += if_maxlinkhdr;
184 rbp = (struct bootp_t *)m->m_data;
185 m->m_data += sizeof(struct udpiphdr);
186 memset(rbp, 0, sizeof(struct bootp_t));
188 bc=NULL;
189 daddr.sin_addr.s_addr=htonl(0L);
190 if (dhcp_msg_type == DHCPREQUEST) {
191 if (reqaddr.sin_addr.s_addr != htonl(0L))
192 bc = find_reqaddr(&daddr.sin_addr, &reqaddr.sin_addr, bp->bp_hwaddr);
193 else
194 bc = find_addr(&daddr.sin_addr, bp->bp_hwaddr);
196 else if (dhcp_msg_type == DHCPDISCOVER) {
197 bc = find_addr(&daddr.sin_addr, bp->bp_hwaddr);
198 if (!bc)
199 bc = get_new_addr(&daddr.sin_addr);
202 dprintf("offered addr=%08x\n", ntohl(daddr.sin_addr.s_addr));
204 saddr.sin_addr.s_addr = htonl(ntohl(special_addr.s_addr) | CTL_ALIAS);
205 saddr.sin_port = htons(BOOTP_SERVER);
207 daddr.sin_port = htons(BOOTP_CLIENT);
209 rbp->bp_op = BOOTP_REPLY;
210 rbp->bp_xid = bp->bp_xid;
211 rbp->bp_htype = 1;
212 rbp->bp_hlen = 6;
213 memcpy(rbp->bp_hwaddr, bp->bp_hwaddr, 6);
215 rbp->bp_yiaddr = daddr.sin_addr; /* IP address */
216 rbp->bp_siaddr = saddr.sin_addr; /* IP address */
218 q = rbp->bp_vend;
219 memcpy(q, rfc1533_cookie, 4);
220 q += 4;
222 if (bc != NULL) {
223 memcpy(bc->macaddr, client_ethaddr, 6);
224 bc->allocated = 1;
225 bc->time = time(NULL);
226 replytype=(dhcp_msg_type == DHCPDISCOVER)?DHCPOFFER:DHCPACK;
228 else
229 replytype=DHCPNACK;
231 *q++ = RFC2132_MSG_TYPE;
232 *q++ = 1;
233 *q++ = replytype;
235 if ((dhcp_msg_type == DHCPDISCOVER ||
236 dhcp_msg_type == DHCPREQUEST) && replytype!=DHCPNACK) {
237 *q++ = RFC2132_SRV_ID;
238 *q++ = 4;
239 memcpy(q, &saddr.sin_addr, 4);
240 q += 4;
242 *q++ = RFC1533_NETMASK;
243 *q++ = 4;
244 *q++ = 0xff;
245 *q++ = 0xff;
246 *q++ = 0xff;
247 *q++ = 0x00;
249 *q++ = RFC1533_GATEWAY;
250 *q++ = 4;
251 memcpy(q, &saddr.sin_addr, 4);
252 q += 4;
254 *q++ = RFC1533_DNS;
255 *q++ = 4;
256 dns_addr.s_addr = htonl(ntohl(special_addr.s_addr) | CTL_DNS);
257 memcpy(q, &dns_addr, 4);
258 q += 4;
260 *q++ = RFC2132_LEASE_TIME;
261 *q++ = 4;
262 val = htonl(LEASE_TIME);
263 memcpy(q, &val, 4);
264 q += 4;
266 *q++ = RFC1533_END;
268 //m->m_len = sizeof(struct bootp_t);
269 m->m_len = q - (uint8_t *) (m->m_data);
270 client_eth_register(client_ethaddr,&daddr.sin_addr);
271 udp_output2(NULL, m, &saddr, &daddr, IPTOS_LOWDELAY);
274 void bootp_input(struct mbuf *m)
276 struct bootp_t *bp = (struct bootp_t *)m->m_data;
278 if (bp->bp_op == BOOTP_REQUEST) {
279 bootp_reply(bp);