bootp.c 9.13 KB
Newer Older
bellard's avatar
bellard committed
1 2
/*
 * QEMU BOOTP/DHCP server
3
 *
bellard's avatar
bellard committed
4
 * Copyright (c) 2004 Fabrice Bellard
5
 *
bellard's avatar
bellard committed
6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 */
#include <slirp.h>

/* XXX: only DHCP is supported */

#define LEASE_TIME (24 * 3600)

static const uint8_t rfc1533_cookie[] = { RFC1533_COOKIE };

#ifdef DEBUG
malc's avatar
malc committed
33
#define DPRINTF(fmt, ...) \
34
do if (slirp_debug & DBG_CALL) { fprintf(dfd, fmt, ##  __VA_ARGS__); fflush(dfd); } while (0)
bellard's avatar
bellard committed
35
#else
36
#define DPRINTF(fmt, ...) do{}while(0)
bellard's avatar
bellard committed
37 38
#endif

39
static BOOTPClient *get_new_addr(Slirp *slirp, struct in_addr *paddr,
40
                                 const uint8_t *macaddr)
bellard's avatar
bellard committed
41 42 43 44
{
    BOOTPClient *bc;
    int i;

45 46
    for(i = 0; i < NB_BOOTP_CLIENTS; i++) {
        bc = &slirp->bootp_clients[i];
47
        if (!bc->allocated || !memcmp(macaddr, bc->macaddr, 6))
bellard's avatar
bellard committed
48 49 50 51
            goto found;
    }
    return NULL;
 found:
52
    bc = &slirp->bootp_clients[i];
bellard's avatar
bellard committed
53
    bc->allocated = 1;
54
    paddr->s_addr = slirp->vdhcp_startaddr.s_addr + htonl(i);
bellard's avatar
bellard committed
55 56 57
    return bc;
}

58
static BOOTPClient *request_addr(Slirp *slirp, const struct in_addr *paddr,
59 60 61
                                 const uint8_t *macaddr)
{
    uint32_t req_addr = ntohl(paddr->s_addr);
62
    uint32_t dhcp_addr = ntohl(slirp->vdhcp_startaddr.s_addr);
63 64
    BOOTPClient *bc;

65
    if (req_addr >= dhcp_addr &&
66 67
        req_addr < (dhcp_addr + NB_BOOTP_CLIENTS)) {
        bc = &slirp->bootp_clients[req_addr - dhcp_addr];
68 69 70 71 72 73 74 75
        if (!bc->allocated || !memcmp(macaddr, bc->macaddr, 6)) {
            bc->allocated = 1;
            return bc;
        }
    }
    return NULL;
}

76 77
static BOOTPClient *find_addr(Slirp *slirp, struct in_addr *paddr,
                              const uint8_t *macaddr)
bellard's avatar
bellard committed
78 79 80 81
{
    BOOTPClient *bc;
    int i;

82 83
    for(i = 0; i < NB_BOOTP_CLIENTS; i++) {
        if (!memcmp(macaddr, slirp->bootp_clients[i].macaddr, 6))
bellard's avatar
bellard committed
84 85 86 87
            goto found;
    }
    return NULL;
 found:
88
    bc = &slirp->bootp_clients[i];
bellard's avatar
bellard committed
89
    bc->allocated = 1;
90
    paddr->s_addr = slirp->vdhcp_startaddr.s_addr + htonl(i);
bellard's avatar
bellard committed
91 92 93
    return bc;
}

94
static void dhcp_decode(const struct bootp_t *bp, int *pmsg_type,
95
                        struct in_addr *preq_addr)
bellard's avatar
bellard committed
96 97 98 99
{
    const uint8_t *p, *p_end;
    int len, tag;

100
    *pmsg_type = 0;
101
    preq_addr->s_addr = htonl(0L);
bellard's avatar
bellard committed
102

103 104
    p = bp->bp_vend;
    p_end = p + DHCP_OPT_LEN;
bellard's avatar
bellard committed
105 106 107 108 109 110
    if (memcmp(p, rfc1533_cookie, 4) != 0)
        return;
    p += 4;
    while (p < p_end) {
        tag = p[0];
        if (tag == RFC1533_PAD) {
111
            p++;
bellard's avatar
bellard committed
112 113 114 115 116 117 118
        } else if (tag == RFC1533_END) {
            break;
        } else {
            p++;
            if (p >= p_end)
                break;
            len = *p++;
malc's avatar
malc committed
119
            DPRINTF("dhcp: tag=%d len=%d\n", tag, len);
bellard's avatar
bellard committed
120 121 122 123 124 125

            switch(tag) {
            case RFC2132_MSG_TYPE:
                if (len >= 1)
                    *pmsg_type = p[0];
                break;
126
            case RFC2132_REQ_ADDR:
127 128 129
                if (len >= 4) {
                    memcpy(&(preq_addr->s_addr), p, 4);
                }
130
                break;
bellard's avatar
bellard committed
131 132 133 134 135 136
            default:
                break;
            }
            p += len;
        }
    }
137 138 139
    if (*pmsg_type == DHCPREQUEST && preq_addr->s_addr == htonl(0L) &&
        bp->bp_ciaddr.s_addr) {
        memcpy(&(preq_addr->s_addr), &bp->bp_ciaddr, 4);
140
    }
bellard's avatar
bellard committed
141 142
}

143
static void bootp_reply(Slirp *slirp, const struct bootp_t *bp)
bellard's avatar
bellard committed
144
{
145
    BOOTPClient *bc = NULL;
bellard's avatar
bellard committed
146 147 148
    struct mbuf *m;
    struct bootp_t *rbp;
    struct sockaddr_in saddr, daddr;
149
    struct in_addr preq_addr;
bellard's avatar
bellard committed
150 151
    int dhcp_msg_type, val;
    uint8_t *q;
Fabien Chouteau's avatar
Fabien Chouteau committed
152
    uint8_t client_ethaddr[ETH_ALEN];
bellard's avatar
bellard committed
153 154

    /* extract exact DHCP msg type */
155
    dhcp_decode(bp, &dhcp_msg_type, &preq_addr);
malc's avatar
malc committed
156
    DPRINTF("bootp packet op=%d msgtype=%d", bp->bp_op, dhcp_msg_type);
157 158
    if (preq_addr.s_addr != htonl(0L))
        DPRINTF(" req_addr=%08x\n", ntohl(preq_addr.s_addr));
159
    else
malc's avatar
malc committed
160
        DPRINTF("\n");
161

bellard's avatar
bellard committed
162 163
    if (dhcp_msg_type == 0)
        dhcp_msg_type = DHCPREQUEST; /* Force reply for old BOOTP clients */
164

165
    if (dhcp_msg_type != DHCPDISCOVER &&
bellard's avatar
bellard committed
166 167
        dhcp_msg_type != DHCPREQUEST)
        return;
Fabien Chouteau's avatar
Fabien Chouteau committed
168 169 170

    /* Get client's hardware address from bootp request */
    memcpy(client_ethaddr, bp->bp_hwaddr, ETH_ALEN);
171

172 173
    m = m_get(slirp);
    if (!m) {
bellard's avatar
bellard committed
174
        return;
175
    }
176
    m->m_data += IF_MAXLINKHDR;
bellard's avatar
bellard committed
177 178 179 180
    rbp = (struct bootp_t *)m->m_data;
    m->m_data += sizeof(struct udpiphdr);
    memset(rbp, 0, sizeof(struct bootp_t));

bellard's avatar
bellard committed
181
    if (dhcp_msg_type == DHCPDISCOVER) {
182
        if (preq_addr.s_addr != htonl(0L)) {
Fabien Chouteau's avatar
Fabien Chouteau committed
183
            bc = request_addr(slirp, &preq_addr, client_ethaddr);
184
            if (bc) {
185
                daddr.sin_addr = preq_addr;
186 187
            }
        }
bellard's avatar
bellard committed
188
        if (!bc) {
189
         new_addr:
Fabien Chouteau's avatar
Fabien Chouteau committed
190
            bc = get_new_addr(slirp, &daddr.sin_addr, client_ethaddr);
191
            if (!bc) {
malc's avatar
malc committed
192
                DPRINTF("no address left\n");
193 194
                return;
            }
bellard's avatar
bellard committed
195
        }
Fabien Chouteau's avatar
Fabien Chouteau committed
196
        memcpy(bc->macaddr, client_ethaddr, ETH_ALEN);
197
    } else if (preq_addr.s_addr != htonl(0L)) {
Fabien Chouteau's avatar
Fabien Chouteau committed
198
        bc = request_addr(slirp, &preq_addr, client_ethaddr);
199
        if (bc) {
200
            daddr.sin_addr = preq_addr;
Fabien Chouteau's avatar
Fabien Chouteau committed
201
            memcpy(bc->macaddr, client_ethaddr, ETH_ALEN);
202
        } else {
203 204
            /* DHCPNAKs should be sent to broadcast */
            daddr.sin_addr.s_addr = 0xffffffff;
205
        }
bellard's avatar
bellard committed
206
    } else {
207
        bc = find_addr(slirp, &daddr.sin_addr, bp->bp_hwaddr);
bellard's avatar
bellard committed
208
        if (!bc) {
209 210 211
            /* if never assigned, behaves as if it was already
               assigned (windows fix because it remembers its address) */
            goto new_addr;
bellard's avatar
bellard committed
212
        }
bellard's avatar
bellard committed
213
    }
214

Fabien Chouteau's avatar
Fabien Chouteau committed
215 216 217
    /* Update ARP table for this IP address */
    arp_table_add(slirp, daddr.sin_addr.s_addr, client_ethaddr);

218
    saddr.sin_addr = slirp->vhost_addr;
bellard's avatar
bellard committed
219 220 221 222 223 224 225 226
    saddr.sin_port = htons(BOOTP_SERVER);

    daddr.sin_port = htons(BOOTP_CLIENT);

    rbp->bp_op = BOOTP_REPLY;
    rbp->bp_xid = bp->bp_xid;
    rbp->bp_htype = 1;
    rbp->bp_hlen = 6;
Fabien Chouteau's avatar
Fabien Chouteau committed
227
    memcpy(rbp->bp_hwaddr, bp->bp_hwaddr, ETH_ALEN);
bellard's avatar
bellard committed
228

229 230
    rbp->bp_yiaddr = daddr.sin_addr; /* Client IP address */
    rbp->bp_siaddr = saddr.sin_addr; /* Server IP address */
bellard's avatar
bellard committed
231 232 233 234 235

    q = rbp->bp_vend;
    memcpy(q, rfc1533_cookie, 4);
    q += 4;

236
    if (bc) {
malc's avatar
malc committed
237
        DPRINTF("%s addr=%08x\n",
238 239 240 241 242 243 244 245 246 247 248 249 250
                (dhcp_msg_type == DHCPDISCOVER) ? "offered" : "ack'ed",
                ntohl(daddr.sin_addr.s_addr));

        if (dhcp_msg_type == DHCPDISCOVER) {
            *q++ = RFC2132_MSG_TYPE;
            *q++ = 1;
            *q++ = DHCPOFFER;
        } else /* DHCPREQUEST */ {
            *q++ = RFC2132_MSG_TYPE;
            *q++ = 1;
            *q++ = DHCPACK;
        }

251
        if (slirp->bootp_filename)
252
            snprintf((char *)rbp->bp_file, sizeof(rbp->bp_file), "%s",
253
                     slirp->bootp_filename);
254

bellard's avatar
bellard committed
255 256 257 258 259 260 261
        *q++ = RFC2132_SRV_ID;
        *q++ = 4;
        memcpy(q, &saddr.sin_addr, 4);
        q += 4;

        *q++ = RFC1533_NETMASK;
        *q++ = 4;
262
        memcpy(q, &slirp->vnetwork_mask, 4);
263
        q += 4;
264

265
        if (!slirp->restricted) {
266 267 268 269 270 271 272
            *q++ = RFC1533_GATEWAY;
            *q++ = 4;
            memcpy(q, &saddr.sin_addr, 4);
            q += 4;

            *q++ = RFC1533_DNS;
            *q++ = 4;
273
            memcpy(q, &slirp->vnameserver_addr, 4);
274 275
            q += 4;
        }
bellard's avatar
bellard committed
276 277 278 279 280 281

        *q++ = RFC2132_LEASE_TIME;
        *q++ = 4;
        val = htonl(LEASE_TIME);
        memcpy(q, &val, 4);
        q += 4;
pbrook's avatar
pbrook committed
282

283 284
        if (*slirp->client_hostname) {
            val = strlen(slirp->client_hostname);
pbrook's avatar
pbrook committed
285 286
            *q++ = RFC1533_HOSTNAME;
            *q++ = val;
287
            memcpy(q, slirp->client_hostname, val);
pbrook's avatar
pbrook committed
288 289
            q += val;
        }
290 291 292
    } else {
        static const char nak_msg[] = "requested address not available";

293
        DPRINTF("nak'ed addr=%08x\n", ntohl(preq_addr.s_addr));
294 295 296 297 298 299 300 301 302

        *q++ = RFC2132_MSG_TYPE;
        *q++ = 1;
        *q++ = DHCPNAK;

        *q++ = RFC2132_MESSAGE;
        *q++ = sizeof(nak_msg) - 1;
        memcpy(q, nak_msg, sizeof(nak_msg) - 1);
        q += sizeof(nak_msg) - 1;
bellard's avatar
bellard committed
303
    }
304
    *q = RFC1533_END;
305

306 307
    daddr.sin_addr.s_addr = 0xffffffffu;

308
    m->m_len = sizeof(struct bootp_t) -
309
        sizeof(struct ip) - sizeof(struct udphdr);
bellard's avatar
bellard committed
310 311 312 313 314
    udp_output2(NULL, m, &saddr, &daddr, IPTOS_LOWDELAY);
}

void bootp_input(struct mbuf *m)
{
315
    struct bootp_t *bp = mtod(m, struct bootp_t *);
bellard's avatar
bellard committed
316 317

    if (bp->bp_op == BOOTP_REQUEST) {
318
        bootp_reply(m->slirp, bp);
bellard's avatar
bellard committed
319 320
    }
}