1
2
3
4
5
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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
|
--- a/networking/udhcp/packet.c
+++ b/networking/udhcp/packet.c
@@ -165,6 +165,11 @@ uint16_t FAST_FUNC udhcp_checksum(void *
return ~sum;
}
+int udhcp_get_payload_len(struct dhcp_packet *dhcp_pkt)
+{
+ return sizeof(struct dhcp_packet) - DHCP_OPTIONS_BUFSIZE + udhcp_end_option(dhcp_pkt->options) + sizeof(dhcp_pkt->options[0]);
+}
+
/* Construct a ip/udp header for a packet, send packet */
int FAST_FUNC udhcp_send_raw_packet(struct dhcp_packet *dhcp_pkt,
uint32_t source_nip, int source_port,
@@ -173,11 +178,12 @@ int FAST_FUNC udhcp_send_raw_packet(stru
{
struct sockaddr_ll dest_sll;
struct ip_udp_dhcp_packet packet;
- unsigned padding;
int fd;
int result = -1;
const char *msg;
+ int p_len = udhcp_get_payload_len(dhcp_pkt);
+
fd = socket(PF_PACKET, SOCK_DGRAM, htons(ETH_P_IP));
if (fd < 0) {
msg = "socket(%s)";
@@ -186,7 +192,7 @@ int FAST_FUNC udhcp_send_raw_packet(stru
memset(&dest_sll, 0, sizeof(dest_sll));
memset(&packet, 0, offsetof(struct ip_udp_dhcp_packet, data));
- packet.data = *dhcp_pkt; /* struct copy */
+ memcpy(&(packet.data), dhcp_pkt, p_len);
dest_sll.sll_family = AF_PACKET;
dest_sll.sll_protocol = htons(ETH_P_IP);
@@ -208,27 +214,25 @@ int FAST_FUNC udhcp_send_raw_packet(stru
* In order to work with those buggy servers,
* we truncate packets after end option byte.
*/
- padding = DHCP_OPTIONS_BUFSIZE - 1 - udhcp_end_option(packet.data.options);
packet.ip.protocol = IPPROTO_UDP;
packet.ip.saddr = source_nip;
packet.ip.daddr = dest_nip;
packet.udp.source = htons(source_port);
packet.udp.dest = htons(dest_port);
- /* size, excluding IP header: */
- packet.udp.len = htons(UPD_DHCP_SIZE - padding);
- /* for UDP checksumming, ip.len is set to UDP packet len */
+ p_len += sizeof(packet.udp);
+ packet.udp.len = htons(p_len);
packet.ip.tot_len = packet.udp.len;
- packet.udp.check = udhcp_checksum(&packet, IP_UPD_DHCP_SIZE - padding);
- /* but for sending, it is set to IP packet len */
- packet.ip.tot_len = htons(IP_UPD_DHCP_SIZE - padding);
+ p_len += sizeof(packet.ip);
+ packet.udp.check = udhcp_checksum(&packet, p_len);
+ packet.ip.tot_len = htons(p_len);
packet.ip.ihl = sizeof(packet.ip) >> 2;
packet.ip.version = IPVERSION;
packet.ip.ttl = IPDEFTTL;
packet.ip.check = udhcp_checksum(&packet.ip, sizeof(packet.ip));
udhcp_dump_packet(dhcp_pkt);
- result = sendto(fd, &packet, IP_UPD_DHCP_SIZE - padding, /*flags:*/ 0,
+ result = sendto(fd, &packet, p_len, 0,
(struct sockaddr *) &dest_sll, sizeof(dest_sll));
msg = "sendto";
ret_close:
@@ -246,7 +250,6 @@ int FAST_FUNC udhcp_send_kernel_packet(s
uint32_t dest_nip, int dest_port)
{
struct sockaddr_in client;
- unsigned padding;
int fd;
int result = -1;
const char *msg;
@@ -278,8 +281,7 @@ int FAST_FUNC udhcp_send_kernel_packet(s
udhcp_dump_packet(dhcp_pkt);
- padding = DHCP_OPTIONS_BUFSIZE - 1 - udhcp_end_option(dhcp_pkt->options);
- result = safe_write(fd, dhcp_pkt, DHCP_SIZE - padding);
+ result = safe_write(fd, dhcp_pkt, udhcp_get_payload_len(dhcp_pkt));
msg = "write";
ret_close:
close(fd);
|