mirror of
https://github.com/systemd/systemd-stable.git
synced 2025-01-24 02:03:54 +03:00
sd-dhcp-client: move magic cookie into DHCPMessage struct
Also move the checking of it to the main message handler, rather than the options parser. Fix a bug, so we now drop the packet if any of the magic bytes don't match. Before we used to only drop the packet if they were all wrong.
This commit is contained in:
parent
19aa72f74e
commit
3b7ca119fd
@ -149,16 +149,12 @@ int dhcp_option_parse(DHCPMessage *message, size_t len,
|
|||||||
if (!message)
|
if (!message)
|
||||||
return -EINVAL;
|
return -EINVAL;
|
||||||
|
|
||||||
if (len < sizeof(DHCPMessage) + 4)
|
if (len < sizeof(DHCPMessage))
|
||||||
return -EINVAL;
|
return -EINVAL;
|
||||||
|
|
||||||
len -= sizeof(DHCPMessage) + 4;
|
len -= sizeof(DHCPMessage);
|
||||||
|
|
||||||
if (opt[0] != 0x63 && opt[1] != 0x82 && opt[2] != 0x53 &&
|
res = parse_options(opt, len, &overload, &message_type,
|
||||||
opt[3] != 0x63)
|
|
||||||
return -EINVAL;
|
|
||||||
|
|
||||||
res = parse_options(&opt[4], len, &overload, &message_type,
|
|
||||||
cb, user_data);
|
cb, user_data);
|
||||||
if (res < 0)
|
if (res < 0)
|
||||||
return res;
|
return res;
|
||||||
|
@ -43,23 +43,13 @@ int dhcp_message_init(DHCPMessage *message, uint8_t op, uint32_t xid,
|
|||||||
|
|
||||||
assert(op == BOOTREQUEST || op == BOOTREPLY);
|
assert(op == BOOTREQUEST || op == BOOTREPLY);
|
||||||
|
|
||||||
*opt = (uint8_t *)(message + 1);
|
|
||||||
|
|
||||||
if (*optlen < 4)
|
|
||||||
return -ENOBUFS;
|
|
||||||
*optlen -= 4;
|
|
||||||
|
|
||||||
message->op = op;
|
message->op = op;
|
||||||
message->htype = ARPHRD_ETHER;
|
message->htype = ARPHRD_ETHER;
|
||||||
message->hlen = ETHER_ADDR_LEN;
|
message->hlen = ETHER_ADDR_LEN;
|
||||||
message->xid = htobe32(xid);
|
message->xid = htobe32(xid);
|
||||||
|
message->magic = htobe32(DHCP_MAGIC_COOKIE);
|
||||||
|
|
||||||
(*opt)[0] = 0x63;
|
*opt = (uint8_t *)(message + 1);
|
||||||
(*opt)[1] = 0x82;
|
|
||||||
(*opt)[2] = 0x53;
|
|
||||||
(*opt)[3] = 0x63;
|
|
||||||
|
|
||||||
*opt += 4;
|
|
||||||
|
|
||||||
err = dhcp_option_append(opt, optlen, DHCP_OPTION_MESSAGE_TYPE, 1,
|
err = dhcp_option_append(opt, optlen, DHCP_OPTION_MESSAGE_TYPE, 1,
|
||||||
&type);
|
&type);
|
||||||
|
@ -43,6 +43,7 @@ struct DHCPMessage {
|
|||||||
uint8_t chaddr[16];
|
uint8_t chaddr[16];
|
||||||
uint8_t sname[64];
|
uint8_t sname[64];
|
||||||
uint8_t file[128];
|
uint8_t file[128];
|
||||||
|
be32_t magic;
|
||||||
} _packed_;
|
} _packed_;
|
||||||
|
|
||||||
typedef struct DHCPMessage DHCPMessage;
|
typedef struct DHCPMessage DHCPMessage;
|
||||||
@ -58,7 +59,8 @@ typedef struct DHCPPacket DHCPPacket;
|
|||||||
#define DHCP_IP_SIZE (int32_t)(sizeof(struct iphdr))
|
#define DHCP_IP_SIZE (int32_t)(sizeof(struct iphdr))
|
||||||
#define DHCP_IP_UDP_SIZE (int32_t)(sizeof(struct udphdr) + DHCP_IP_SIZE)
|
#define DHCP_IP_UDP_SIZE (int32_t)(sizeof(struct udphdr) + DHCP_IP_SIZE)
|
||||||
#define DHCP_MESSAGE_SIZE (int32_t)(sizeof(DHCPMessage))
|
#define DHCP_MESSAGE_SIZE (int32_t)(sizeof(DHCPMessage))
|
||||||
#define DHCP_MIN_OPTIONS_SIZE 312
|
#define DHCP_MIN_OPTIONS_SIZE 308
|
||||||
|
#define DHCP_MAGIC_COOKIE (uint32_t)(0x63825363)
|
||||||
|
|
||||||
enum {
|
enum {
|
||||||
DHCP_PORT_SERVER = 67,
|
DHCP_PORT_SERVER = 67,
|
||||||
|
@ -22,6 +22,7 @@
|
|||||||
#include <string.h>
|
#include <string.h>
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <net/ethernet.h>
|
#include <net/ethernet.h>
|
||||||
|
#include <net/if_arp.h>
|
||||||
#include <sys/param.h>
|
#include <sys/param.h>
|
||||||
#include <sys/ioctl.h>
|
#include <sys/ioctl.h>
|
||||||
|
|
||||||
@ -936,6 +937,11 @@ static int client_handle_message(sd_dhcp_client *client, DHCPMessage *message,
|
|||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (be32toh(message->magic) != DHCP_MAGIC_COOKIE) {
|
||||||
|
log_dhcp_client(client, "not a DHCP message: ignoring");
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
if (message->op != BOOTREPLY) {
|
if (message->op != BOOTREPLY) {
|
||||||
log_dhcp_client(client, "not a BOOTREPLY message: ignoring");
|
log_dhcp_client(client, "not a BOOTREPLY message: ignoring");
|
||||||
return 0;
|
return 0;
|
||||||
@ -948,6 +954,11 @@ static int client_handle_message(sd_dhcp_client *client, DHCPMessage *message,
|
|||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (message->htype != ARPHRD_ETHER || message->hlen != ETHER_ADDR_LEN) {
|
||||||
|
log_dhcp_client(client, "not an ethernet packet");
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
if (memcmp(&message->chaddr[0], &client->client_id.mac_addr,
|
if (memcmp(&message->chaddr[0], &client->client_id.mac_addr,
|
||||||
ETH_ALEN)) {
|
ETH_ALEN)) {
|
||||||
log_dhcp_client(client, "received chaddr does not match "
|
log_dhcp_client(client, "received chaddr does not match "
|
||||||
|
Loading…
x
Reference in New Issue
Block a user