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