slirp: Cleanup and basic reanimation of debug code
[qemu] / slirp / bootp.c
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 LEASE_TIME (24 * 3600)
31
32 typedef struct {
33     uint8_t allocated;
34     uint8_t macaddr[6];
35 } BOOTPClient;
36
37 static BOOTPClient bootp_clients[NB_ADDR];
38
39 char *bootp_filename;
40
41 static const uint8_t rfc1533_cookie[] = { RFC1533_COOKIE };
42
43 #ifdef DEBUG
44 #define dprintf(fmt, ...) \
45 do if (slirp_debug & DBG_CALL) { fprintf(dfd, fmt, ##  __VA_ARGS__); fflush(dfd); } while (0)
46 #else
47 #define dprintf(fmt, ...)
48 #endif
49
50 static BOOTPClient *get_new_addr(struct in_addr *paddr,
51                                  const uint8_t *macaddr)
52 {
53     BOOTPClient *bc;
54     int i;
55
56     for(i = 0; i < NB_ADDR; i++) {
57         bc = &bootp_clients[i];
58         if (!bc->allocated || !memcmp(macaddr, bc->macaddr, 6))
59             goto found;
60     }
61     return NULL;
62  found:
63     bc = &bootp_clients[i];
64     bc->allocated = 1;
65     paddr->s_addr = vdhcp_startaddr.s_addr + htonl(i);
66     return bc;
67 }
68
69 static BOOTPClient *request_addr(const struct in_addr *paddr,
70                                  const uint8_t *macaddr)
71 {
72     uint32_t req_addr = ntohl(paddr->s_addr);
73     uint32_t dhcp_addr = ntohl(vdhcp_startaddr.s_addr);
74     BOOTPClient *bc;
75
76     if (req_addr >= dhcp_addr &&
77         req_addr < (dhcp_addr + NB_ADDR)) {
78         bc = &bootp_clients[req_addr - dhcp_addr];
79         if (!bc->allocated || !memcmp(macaddr, bc->macaddr, 6)) {
80             bc->allocated = 1;
81             return bc;
82         }
83     }
84     return NULL;
85 }
86
87 static BOOTPClient *find_addr(struct in_addr *paddr, const uint8_t *macaddr)
88 {
89     BOOTPClient *bc;
90     int i;
91
92     for(i = 0; i < NB_ADDR; i++) {
93         if (!memcmp(macaddr, bootp_clients[i].macaddr, 6))
94             goto found;
95     }
96     return NULL;
97  found:
98     bc = &bootp_clients[i];
99     bc->allocated = 1;
100     paddr->s_addr = vdhcp_startaddr.s_addr + htonl(i);
101     return bc;
102 }
103
104 static void dhcp_decode(const struct bootp_t *bp, int *pmsg_type,
105                         const struct in_addr **preq_addr)
106 {
107     const uint8_t *p, *p_end;
108     int len, tag;
109
110     *pmsg_type = 0;
111     *preq_addr = NULL;
112
113     p = bp->bp_vend;
114     p_end = p + DHCP_OPT_LEN;
115     if (memcmp(p, rfc1533_cookie, 4) != 0)
116         return;
117     p += 4;
118     while (p < p_end) {
119         tag = p[0];
120         if (tag == RFC1533_PAD) {
121             p++;
122         } else if (tag == RFC1533_END) {
123             break;
124         } else {
125             p++;
126             if (p >= p_end)
127                 break;
128             len = *p++;
129             dprintf("dhcp: tag=%d len=%d\n", tag, len);
130
131             switch(tag) {
132             case RFC2132_MSG_TYPE:
133                 if (len >= 1)
134                     *pmsg_type = p[0];
135                 break;
136             case RFC2132_REQ_ADDR:
137                 if (len >= 4)
138                     *preq_addr = (struct in_addr *)p;
139                 break;
140             default:
141                 break;
142             }
143             p += len;
144         }
145     }
146     if (*pmsg_type == DHCPREQUEST && !*preq_addr && bp->bp_ciaddr.s_addr) {
147         *preq_addr = &bp->bp_ciaddr;
148     }
149 }
150
151 static void bootp_reply(const struct bootp_t *bp)
152 {
153     BOOTPClient *bc = NULL;
154     struct mbuf *m;
155     struct bootp_t *rbp;
156     struct sockaddr_in saddr, daddr;
157     const struct in_addr *preq_addr;
158     int dhcp_msg_type, val;
159     uint8_t *q;
160
161     /* extract exact DHCP msg type */
162     dhcp_decode(bp, &dhcp_msg_type, &preq_addr);
163     dprintf("bootp packet op=%d msgtype=%d", bp->bp_op, dhcp_msg_type);
164     if (preq_addr)
165         dprintf(" req_addr=%08x\n", ntohl(preq_addr->s_addr));
166     else
167         dprintf("\n");
168
169     if (dhcp_msg_type == 0)
170         dhcp_msg_type = DHCPREQUEST; /* Force reply for old BOOTP clients */
171
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);
177
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));
184
185     if (dhcp_msg_type == DHCPDISCOVER) {
186         if (preq_addr) {
187             bc = request_addr(preq_addr, client_ethaddr);
188             if (bc) {
189                 daddr.sin_addr = *preq_addr;
190             }
191         }
192         if (!bc) {
193          new_addr:
194             bc = get_new_addr(&daddr.sin_addr, client_ethaddr);
195             if (!bc) {
196                 dprintf("no address left\n");
197                 return;
198             }
199         }
200         memcpy(bc->macaddr, client_ethaddr, 6);
201     } else if (preq_addr) {
202         bc = request_addr(preq_addr, client_ethaddr);
203         if (bc) {
204             daddr.sin_addr = *preq_addr;
205             memcpy(bc->macaddr, client_ethaddr, 6);
206         } else {
207             daddr.sin_addr.s_addr = 0;
208         }
209     } else {
210         bc = find_addr(&daddr.sin_addr, bp->bp_hwaddr);
211         if (!bc) {
212             /* if never assigned, behaves as if it was already
213                assigned (windows fix because it remembers its address) */
214             goto new_addr;
215         }
216     }
217
218     saddr.sin_addr = vhost_addr;
219     saddr.sin_port = htons(BOOTP_SERVER);
220
221     daddr.sin_port = htons(BOOTP_CLIENT);
222
223     rbp->bp_op = BOOTP_REPLY;
224     rbp->bp_xid = bp->bp_xid;
225     rbp->bp_htype = 1;
226     rbp->bp_hlen = 6;
227     memcpy(rbp->bp_hwaddr, bp->bp_hwaddr, 6);
228
229     rbp->bp_yiaddr = daddr.sin_addr; /* Client IP address */
230     rbp->bp_siaddr = saddr.sin_addr; /* Server IP address */
231
232     q = rbp->bp_vend;
233     memcpy(q, rfc1533_cookie, 4);
234     q += 4;
235
236     if (bc) {
237         dprintf("%s addr=%08x\n",
238                 (dhcp_msg_type == DHCPDISCOVER) ? "offered" : "ack'ed",
239                 ntohl(daddr.sin_addr.s_addr));
240
241         if (dhcp_msg_type == DHCPDISCOVER) {
242             *q++ = RFC2132_MSG_TYPE;
243             *q++ = 1;
244             *q++ = DHCPOFFER;
245         } else /* DHCPREQUEST */ {
246             *q++ = RFC2132_MSG_TYPE;
247             *q++ = 1;
248             *q++ = DHCPACK;
249         }
250
251         if (bootp_filename)
252             snprintf((char *)rbp->bp_file, sizeof(rbp->bp_file), "%s",
253                      bootp_filename);
254
255         *q++ = RFC2132_SRV_ID;
256         *q++ = 4;
257         memcpy(q, &saddr.sin_addr, 4);
258         q += 4;
259
260         *q++ = RFC1533_NETMASK;
261         *q++ = 4;
262         memcpy(q, &vnetwork_mask, 4);
263         q += 4;
264
265         if (!slirp_restrict) {
266             *q++ = RFC1533_GATEWAY;
267             *q++ = 4;
268             memcpy(q, &saddr.sin_addr, 4);
269             q += 4;
270
271             *q++ = RFC1533_DNS;
272             *q++ = 4;
273             memcpy(q, &vnameserver_addr, 4);
274             q += 4;
275         }
276
277         *q++ = RFC2132_LEASE_TIME;
278         *q++ = 4;
279         val = htonl(LEASE_TIME);
280         memcpy(q, &val, 4);
281         q += 4;
282
283         if (*slirp_hostname) {
284             val = strlen(slirp_hostname);
285             *q++ = RFC1533_HOSTNAME;
286             *q++ = val;
287             memcpy(q, slirp_hostname, val);
288             q += val;
289         }
290     } else {
291         static const char nak_msg[] = "requested address not available";
292
293         dprintf("nak'ed addr=%08x\n", ntohl(preq_addr->s_addr));
294
295         *q++ = RFC2132_MSG_TYPE;
296         *q++ = 1;
297         *q++ = DHCPNAK;
298
299         *q++ = RFC2132_MESSAGE;
300         *q++ = sizeof(nak_msg) - 1;
301         memcpy(q, nak_msg, sizeof(nak_msg) - 1);
302         q += sizeof(nak_msg) - 1;
303     }
304     *q++ = RFC1533_END;
305
306     daddr.sin_addr.s_addr = 0xffffffffu;
307
308     m->m_len = sizeof(struct bootp_t) -
309         sizeof(struct ip) - sizeof(struct udphdr);
310     udp_output2(NULL, m, &saddr, &daddr, IPTOS_LOWDELAY);
311 }
312
313 void bootp_input(struct mbuf *m)
314 {
315     struct bootp_t *bp = mtod(m, struct bootp_t *);
316
317     if (bp->bp_op == BOOTP_REQUEST) {
318         bootp_reply(bp);
319     }
320 }