VirtualBox

Changeset 7809 in vbox


Ignore:
Timestamp:
Apr 8, 2008 2:33:21 PM (17 years ago)
Author:
vboxsync
Message:

bootp: send DHCPNAK if the client requests an invalid IP address

Location:
trunk/src/VBox/Devices/Network/slirp
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/VBox/Devices/Network/slirp/bootp.c

    r5332 r7809  
    8585
    8686static void dhcp_decode(const uint8_t *buf, int size,
    87                         int *pmsg_type)
     87                        int *pmsg_type, struct in_addr *req_ip)
    8888{
    8989    const uint8_t *p, *p_end;
     
    113113
    114114            switch(tag) {
     115            case RFC2132_REQ_ADDR:
     116                if (len >= 4)
     117                    *req_ip = *(struct in_addr*)p;
     118                break;
    115119            case RFC2132_MSG_TYPE:
    116120                if (len >= 1)
     
    134138    int dhcp_msg_type, val;
    135139    uint8_t *q;
     140    struct in_addr requested_ip; /* the requested IP in DHCPREQUEST */
     141    int send_nak = 0;
     142    uint32_t ipv4_addr;
    136143
    137144    /* extract exact DHCP msg type */
    138     dhcp_decode(bp->bp_vend, DHCP_OPT_LEN, &dhcp_msg_type);
     145    requested_ip.s_addr = 0xffffffff;
     146    dhcp_decode(bp->bp_vend, DHCP_OPT_LEN, &dhcp_msg_type, &requested_ip);
    139147    dprintf("bootp packet op=%d msgtype=%d\n", bp->bp_op, dhcp_msg_type);
    140148
     
    143151
    144152    if (dhcp_msg_type == DHCPRELEASE) {
    145         uint32_t addr = ntohl(bp->bp_ciaddr.s_addr);
     153        ipv4_addr = ntohl(bp->bp_ciaddr.s_addr);
    146154        release_addr(pData, &bp->bp_ciaddr);
    147155        LogRel(("NAT: DHCP released IP address %u.%u.%u.%u\n",
    148                 addr >> 24, (addr >> 16) & 0xff, (addr >> 8) & 0xff, addr & 0xff));
     156                ipv4_addr >> 24, (ipv4_addr >> 16) & 0xff, (ipv4_addr >> 8) & 0xff, ipv4_addr & 0xff));
    149157        dprintf("released addr=%08x\n", ntohl(bp->bp_ciaddr.s_addr));
    150158        /* This message is not to be answered in any way. */
     
    190198        RTStrPrintf((char*)rbp->bp_file, sizeof(rbp->bp_file), "%s", bootp_filename);
    191199
    192     {
    193         uint32_t addr = ntohl(daddr.sin_addr.s_addr);
    194         LogRel(("NAT: DHCP offered IP address %u.%u.%u.%u\n",
    195                 addr >> 24, (addr >> 16) & 0xff, (addr >> 8) & 0xff, addr & 0xff));
    196     }
    197     dprintf("offered addr=%08x\n", ntohl(daddr.sin_addr.s_addr));
    198 
    199200    saddr.sin_addr.s_addr = htonl(ntohl(special_addr.s_addr) | CTL_ALIAS);
    200201    saddr.sin_port = htons(BOOTP_SERVER);
     
    222223        *q++ = RFC2132_MSG_TYPE;
    223224        *q++ = 1;
    224         *q++ = DHCPACK;
     225        if (   requested_ip.s_addr != 0xffffffff
     226            && requested_ip.s_addr != daddr.sin_addr.s_addr)
     227        {
     228            /* network changed */
     229            *q++ = DHCPNAK;
     230            send_nak = 1;
     231        }
     232        else
     233            *q++ = DHCPACK;
     234    }
     235
     236    if (send_nak)
     237    {
     238        ipv4_addr = ntohl(requested_ip.s_addr);
     239        LogRel(("NAT: Client requested IP address %u.%u.%u.%u -- sending NAK\n",
     240                ipv4_addr >> 24, (ipv4_addr >> 16) & 0xff, (ipv4_addr >> 8) & 0xff, ipv4_addr & 0xff));
     241    }
     242    else
     243    {
     244        ipv4_addr = ntohl(daddr.sin_addr.s_addr);
     245        LogRel(("NAT: DHCP offered IP address %u.%u.%u.%u\n",
     246                ipv4_addr >> 24, (ipv4_addr >> 16) & 0xff, (ipv4_addr >> 8) & 0xff, ipv4_addr & 0xff));
     247        dprintf("offered addr=%08x\n", ntohl(daddr.sin_addr.s_addr));
    225248    }
    226249
     
    231254        memcpy(q, &saddr.sin_addr, 4);
    232255        q += 4;
    233 
     256    }
     257
     258    if (!send_nak &&
     259        (dhcp_msg_type == DHCPDISCOVER ||
     260         dhcp_msg_type == DHCPREQUEST)) {
    234261        *q++ = RFC1533_NETMASK;
    235262        *q++ = 4;
  • trunk/src/VBox/Devices/Network/slirp/bootp.h

    r1076 r7809  
    7272#define DHCPREQUEST             3
    7373#define DHCPACK                 5
     74#define DHCPNAK                 6
    7475#define DHCPRELEASE             7
    7576
Note: See TracChangeset for help on using the changeset viewer.

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette