3 #include <netinet/in.h>
5 #include <sys/socket.h>
7 #if __GLIBC__ >=2 && __GLIBC_MINOR >= 1
8 #include <netpacket/packet.h>
9 #include <net/ethernet.h>
11 #include <asm/types.h>
12 #include <linux/if_packet.h>
13 #include <linux/if_ether.h>
23 void init_header(struct dhcpMessage *packet, char type)
25 memset(packet, 0, sizeof(struct dhcpMessage));
31 packet->op = BOOTREQUEST;
36 packet->op = BOOTREPLY;
38 packet->htype = ETH_10MB;
39 packet->hlen = ETH_10MB_LEN;
40 packet->cookie = htonl(DHCP_MAGIC);
41 packet->options[0] = DHCP_END;
42 add_simple_option(packet->options, DHCP_MESSAGE_TYPE, type);
46 /* read a packet from socket fd, return -1 on read error, -2 on packet error */
47 int get_packet(struct dhcpMessage *packet, int fd)
49 static const char broken_vendors[][8] = {
55 char unsigned *vendor;
57 memset(packet, 0, sizeof(struct dhcpMessage));
58 bytes = read(fd, packet, sizeof(struct dhcpMessage));
60 DEBUG(LOG_INFO, "couldn't read on listening socket, ignoring");
64 if (ntohl(packet->cookie) != DHCP_MAGIC) {
65 LOG(LOG_ERR, "received bogus message, ignoring");
68 DEBUG(LOG_INFO, "Received a packet");
70 if (packet->op == BOOTREQUEST && (vendor = get_option(packet, DHCP_VENDOR))) {
71 for (i = 0; broken_vendors[i][0]; i++) {
72 if (vendor[OPT_LEN - 2] == (uint8_t) strlen(broken_vendors[i]) &&
73 !strncmp((char*)vendor, broken_vendors[i], vendor[OPT_LEN - 2])) {
74 DEBUG(LOG_INFO, "broken client (%s), forcing broadcast",
76 packet->flags |= htons(BROADCAST_FLAG);
85 uint16_t checksum(void *addr, int count)
87 /* Compute Internet Checksum for "count" bytes
88 * beginning at location "addr".
90 register int32_t sum = 0;
91 uint16_t *source = (uint16_t *) addr;
94 /* This is the inner loop */
99 /* Add left-over byte, if any */
101 /* Make sure that the left-over byte is added correctly both
102 * with little and big endian hosts */
104 *(uint8_t *) (&tmp) = * (uint8_t *) source;
107 /* Fold 32-bit sum to 16 bits */
109 sum = (sum & 0xffff) + (sum >> 16);
115 /* Construct a ip/udp header for a packet, and specify the source and dest hardware address */
116 int raw_packet(struct dhcpMessage *payload, uint32_t source_ip, int source_port,
117 uint32_t dest_ip, int dest_port, uint8_t *dest_arp, int ifindex)
121 struct sockaddr_ll dest;
122 struct udp_dhcp_packet packet;
124 if ((fd = socket(PF_PACKET, SOCK_DGRAM, htons(ETH_P_IP))) < 0) {
125 DEBUG(LOG_ERR, "socket call failed: %m");
129 memset(&dest, 0, sizeof(dest));
130 memset(&packet, 0, sizeof(packet));
132 dest.sll_family = AF_PACKET;
133 dest.sll_protocol = htons(ETH_P_IP);
134 dest.sll_ifindex = ifindex;
136 memcpy(dest.sll_addr, dest_arp, 6);
137 if (bind(fd, (struct sockaddr *)&dest, sizeof(struct sockaddr_ll)) < 0) {
138 DEBUG(LOG_ERR, "bind call failed: %m");
143 packet.ip.protocol = IPPROTO_UDP;
144 packet.ip.saddr = source_ip;
145 packet.ip.daddr = dest_ip;
146 packet.udp.source = htons(source_port);
147 packet.udp.dest = htons(dest_port);
148 packet.udp.len = htons(sizeof(packet.udp) + sizeof(struct dhcpMessage)); /* cheat on the psuedo-header */
149 packet.ip.tot_len = packet.udp.len;
150 memcpy(&(packet.data), payload, sizeof(struct dhcpMessage));
151 packet.udp.check = checksum(&packet, sizeof(struct udp_dhcp_packet));
153 packet.ip.tot_len = htons(sizeof(struct udp_dhcp_packet));
154 packet.ip.ihl = sizeof(packet.ip) >> 2;
155 packet.ip.version = IPVERSION;
156 packet.ip.ttl = IPDEFTTL;
157 packet.ip.check = checksum(&(packet.ip), sizeof(packet.ip));
159 result = sendto(fd, &packet, sizeof(struct udp_dhcp_packet), 0, (struct sockaddr *) &dest, sizeof(dest));
161 DEBUG(LOG_ERR, "write on socket failed: %m");
168 /* Let the kernel do all the work for packet generation */
169 int kernel_packet(struct dhcpMessage *payload, uint32_t source_ip, int source_port,
170 uint32_t dest_ip, int dest_port)
174 struct sockaddr_in client;
176 if ((fd = socket(PF_INET, SOCK_DGRAM, IPPROTO_UDP)) < 0)
179 if (setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, (char *) &n, sizeof(n)) == -1) {
184 memset(&client, 0, sizeof(client));
185 client.sin_family = AF_INET;
186 client.sin_port = htons(source_port);
187 client.sin_addr.s_addr = source_ip;
189 if (bind(fd, (struct sockaddr *)&client, sizeof(struct sockaddr)) == -1) {
194 memset(&client, 0, sizeof(client));
195 client.sin_family = AF_INET;
196 client.sin_port = htons(dest_port);
197 client.sin_addr.s_addr = dest_ip;
199 if (connect(fd, (struct sockaddr *)&client, sizeof(struct sockaddr)) == -1) {
204 result = write(fd, payload, sizeof(struct dhcpMessage));