Russ Dill | 61fb489 | 2002-10-14 21:41:28 +0000 | [diff] [blame] | 1 | #include <unistd.h> |
| 2 | #include <string.h> |
| 3 | #include <netinet/in.h> |
| 4 | #include <sys/types.h> |
| 5 | #include <sys/socket.h> |
| 6 | #include <features.h> |
| 7 | #if __GLIBC__ >=2 && __GLIBC_MINOR >= 1 |
| 8 | #include <netpacket/packet.h> |
| 9 | #include <net/ethernet.h> |
| 10 | #else |
| 11 | #include <asm/types.h> |
| 12 | #include <linux/if_packet.h> |
| 13 | #include <linux/if_ether.h> |
| 14 | #endif |
| 15 | #include <errno.h> |
| 16 | |
Russ Dill | 76729b8 | 2003-12-16 20:44:15 +0000 | [diff] [blame] | 17 | #include "packet.h" |
Russ Dill | 61fb489 | 2002-10-14 21:41:28 +0000 | [diff] [blame] | 18 | #include "dhcpd.h" |
| 19 | #include "options.h" |
Glenn L McGrath | 2483343 | 2003-06-10 17:22:49 +0000 | [diff] [blame] | 20 | #include "common.h" |
Russ Dill | 61fb489 | 2002-10-14 21:41:28 +0000 | [diff] [blame] | 21 | |
| 22 | |
| 23 | void init_header(struct dhcpMessage *packet, char type) |
| 24 | { |
| 25 | memset(packet, 0, sizeof(struct dhcpMessage)); |
| 26 | switch (type) { |
| 27 | case DHCPDISCOVER: |
| 28 | case DHCPREQUEST: |
| 29 | case DHCPRELEASE: |
| 30 | case DHCPINFORM: |
| 31 | packet->op = BOOTREQUEST; |
| 32 | break; |
| 33 | case DHCPOFFER: |
| 34 | case DHCPACK: |
| 35 | case DHCPNAK: |
| 36 | packet->op = BOOTREPLY; |
| 37 | } |
| 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); |
| 43 | } |
| 44 | |
| 45 | |
| 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) |
| 48 | { |
| 49 | int bytes; |
| 50 | int i; |
| 51 | const char broken_vendors[][8] = { |
| 52 | "MSFT 98", |
| 53 | "" |
| 54 | }; |
| 55 | char unsigned *vendor; |
| 56 | |
| 57 | memset(packet, 0, sizeof(struct dhcpMessage)); |
| 58 | bytes = read(fd, packet, sizeof(struct dhcpMessage)); |
| 59 | if (bytes < 0) { |
| 60 | DEBUG(LOG_INFO, "couldn't read on listening socket, ignoring"); |
| 61 | return -1; |
| 62 | } |
| 63 | |
| 64 | if (ntohl(packet->cookie) != DHCP_MAGIC) { |
| 65 | LOG(LOG_ERR, "received bogus message, ignoring"); |
| 66 | return -2; |
| 67 | } |
| 68 | DEBUG(LOG_INFO, "Received a packet"); |
| 69 | |
| 70 | if (packet->op == BOOTREQUEST && (vendor = get_option(packet, DHCP_VENDOR))) { |
| 71 | for (i = 0; broken_vendors[i][0]; i++) { |
Eric Andersen | ad95373 | 2004-01-30 23:45:53 +0000 | [diff] [blame] | 72 | if (vendor[OPT_LEN - 2] == (uint8_t) strlen(broken_vendors[i]) && |
Russ Dill | 61fb489 | 2002-10-14 21:41:28 +0000 | [diff] [blame] | 73 | !strncmp(vendor, broken_vendors[i], vendor[OPT_LEN - 2])) { |
| 74 | DEBUG(LOG_INFO, "broken client (%s), forcing broadcast", |
| 75 | broken_vendors[i]); |
| 76 | packet->flags |= htons(BROADCAST_FLAG); |
| 77 | } |
| 78 | } |
| 79 | } |
| 80 | |
| 81 | |
| 82 | return bytes; |
| 83 | } |
| 84 | |
| 85 | |
Eric Andersen | ad95373 | 2004-01-30 23:45:53 +0000 | [diff] [blame] | 86 | uint16_t checksum(void *addr, int count) |
Russ Dill | 61fb489 | 2002-10-14 21:41:28 +0000 | [diff] [blame] | 87 | { |
| 88 | /* Compute Internet Checksum for "count" bytes |
| 89 | * beginning at location "addr". |
| 90 | */ |
| 91 | register int32_t sum = 0; |
Eric Andersen | ad95373 | 2004-01-30 23:45:53 +0000 | [diff] [blame] | 92 | uint16_t *source = (uint16_t *) addr; |
Russ Dill | 61fb489 | 2002-10-14 21:41:28 +0000 | [diff] [blame] | 93 | |
| 94 | while (count > 1) { |
| 95 | /* This is the inner loop */ |
| 96 | sum += *source++; |
| 97 | count -= 2; |
| 98 | } |
| 99 | |
| 100 | /* Add left-over byte, if any */ |
| 101 | if (count > 0) { |
| 102 | /* Make sure that the left-over byte is added correctly both |
| 103 | * with little and big endian hosts */ |
Eric Andersen | ad95373 | 2004-01-30 23:45:53 +0000 | [diff] [blame] | 104 | uint16_t tmp = 0; |
| 105 | *(uint8_t *) (&tmp) = * (uint8_t *) source; |
Russ Dill | 61fb489 | 2002-10-14 21:41:28 +0000 | [diff] [blame] | 106 | sum += tmp; |
| 107 | } |
| 108 | /* Fold 32-bit sum to 16 bits */ |
| 109 | while (sum >> 16) |
| 110 | sum = (sum & 0xffff) + (sum >> 16); |
| 111 | |
| 112 | return ~sum; |
| 113 | } |
| 114 | |
| 115 | |
| 116 | /* Constuct a ip/udp header for a packet, and specify the source and dest hardware address */ |
Eric Andersen | ad95373 | 2004-01-30 23:45:53 +0000 | [diff] [blame] | 117 | int raw_packet(struct dhcpMessage *payload, uint32_t source_ip, int source_port, |
| 118 | uint32_t dest_ip, int dest_port, uint8_t *dest_arp, int ifindex) |
Russ Dill | 61fb489 | 2002-10-14 21:41:28 +0000 | [diff] [blame] | 119 | { |
| 120 | int fd; |
| 121 | int result; |
| 122 | struct sockaddr_ll dest; |
| 123 | struct udp_dhcp_packet packet; |
| 124 | |
| 125 | if ((fd = socket(PF_PACKET, SOCK_DGRAM, htons(ETH_P_IP))) < 0) { |
Glenn L McGrath | 2483343 | 2003-06-10 17:22:49 +0000 | [diff] [blame] | 126 | DEBUG(LOG_ERR, "socket call failed: %m"); |
Russ Dill | 61fb489 | 2002-10-14 21:41:28 +0000 | [diff] [blame] | 127 | return -1; |
| 128 | } |
| 129 | |
| 130 | memset(&dest, 0, sizeof(dest)); |
| 131 | memset(&packet, 0, sizeof(packet)); |
| 132 | |
| 133 | dest.sll_family = AF_PACKET; |
| 134 | dest.sll_protocol = htons(ETH_P_IP); |
| 135 | dest.sll_ifindex = ifindex; |
| 136 | dest.sll_halen = 6; |
| 137 | memcpy(dest.sll_addr, dest_arp, 6); |
| 138 | if (bind(fd, (struct sockaddr *)&dest, sizeof(struct sockaddr_ll)) < 0) { |
Glenn L McGrath | 2483343 | 2003-06-10 17:22:49 +0000 | [diff] [blame] | 139 | DEBUG(LOG_ERR, "bind call failed: %m"); |
Russ Dill | 61fb489 | 2002-10-14 21:41:28 +0000 | [diff] [blame] | 140 | close(fd); |
| 141 | return -1; |
| 142 | } |
| 143 | |
| 144 | packet.ip.protocol = IPPROTO_UDP; |
| 145 | packet.ip.saddr = source_ip; |
| 146 | packet.ip.daddr = dest_ip; |
| 147 | packet.udp.source = htons(source_port); |
| 148 | packet.udp.dest = htons(dest_port); |
| 149 | packet.udp.len = htons(sizeof(packet.udp) + sizeof(struct dhcpMessage)); /* cheat on the psuedo-header */ |
| 150 | packet.ip.tot_len = packet.udp.len; |
| 151 | memcpy(&(packet.data), payload, sizeof(struct dhcpMessage)); |
| 152 | packet.udp.check = checksum(&packet, sizeof(struct udp_dhcp_packet)); |
| 153 | |
| 154 | packet.ip.tot_len = htons(sizeof(struct udp_dhcp_packet)); |
| 155 | packet.ip.ihl = sizeof(packet.ip) >> 2; |
| 156 | packet.ip.version = IPVERSION; |
| 157 | packet.ip.ttl = IPDEFTTL; |
| 158 | packet.ip.check = checksum(&(packet.ip), sizeof(packet.ip)); |
| 159 | |
| 160 | result = sendto(fd, &packet, sizeof(struct udp_dhcp_packet), 0, (struct sockaddr *) &dest, sizeof(dest)); |
| 161 | if (result <= 0) { |
Glenn L McGrath | 2483343 | 2003-06-10 17:22:49 +0000 | [diff] [blame] | 162 | DEBUG(LOG_ERR, "write on socket failed: %m"); |
Russ Dill | 61fb489 | 2002-10-14 21:41:28 +0000 | [diff] [blame] | 163 | } |
| 164 | close(fd); |
| 165 | return result; |
| 166 | } |
| 167 | |
| 168 | |
| 169 | /* Let the kernel do all the work for packet generation */ |
Eric Andersen | ad95373 | 2004-01-30 23:45:53 +0000 | [diff] [blame] | 170 | int kernel_packet(struct dhcpMessage *payload, uint32_t source_ip, int source_port, |
| 171 | uint32_t dest_ip, int dest_port) |
Russ Dill | 61fb489 | 2002-10-14 21:41:28 +0000 | [diff] [blame] | 172 | { |
| 173 | int n = 1; |
| 174 | int fd, result; |
| 175 | struct sockaddr_in client; |
| 176 | |
| 177 | if ((fd = socket(PF_INET, SOCK_DGRAM, IPPROTO_UDP)) < 0) |
| 178 | return -1; |
| 179 | |
| 180 | if (setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, (char *) &n, sizeof(n)) == -1) |
| 181 | return -1; |
| 182 | |
| 183 | memset(&client, 0, sizeof(client)); |
| 184 | client.sin_family = AF_INET; |
| 185 | client.sin_port = htons(source_port); |
| 186 | client.sin_addr.s_addr = source_ip; |
| 187 | |
| 188 | if (bind(fd, (struct sockaddr *)&client, sizeof(struct sockaddr)) == -1) |
| 189 | return -1; |
| 190 | |
| 191 | memset(&client, 0, sizeof(client)); |
| 192 | client.sin_family = AF_INET; |
| 193 | client.sin_port = htons(dest_port); |
| 194 | client.sin_addr.s_addr = dest_ip; |
| 195 | |
| 196 | if (connect(fd, (struct sockaddr *)&client, sizeof(struct sockaddr)) == -1) |
| 197 | return -1; |
| 198 | |
| 199 | result = write(fd, payload, sizeof(struct dhcpMessage)); |
| 200 | close(fd); |
| 201 | return result; |
| 202 | } |