以太網的 ARP 數據包有其固定結構,其中 op 操作參數:1 為請求,2 為應答。要動手寫代碼,先來熟悉幾個常用的數據結構。
1. 物理地址結構位於 netpacket/packet.h
struct sockaddr_ll
{
unsigned short int sll_family;
unsigned short int sll_protocol;
int sll_ifindex;
unsigned short int sll_hatype;
unsigned char sll_pkttype;
unsigned char sll_halen;
unsigned char sll_addr[8];
};
其中 sll_ifindex 是網絡(網卡)接口索引,代表從哪個接口收發數據包。
2. 網絡(網卡)接口數據結構位於 net/if.h
struct ifreq
{
# define IFHWADDRLEN 6
# define IFNAMSIZ IF_NAMESIZE
union
{
char ifrn_name[IFNAMSIZ]; /* Interface name, e.g. "en0". */
} ifr_ifrn;
union
{
struct sockaddr ifru_addr;
struct sockaddr ifru_dstaddr;
struct sockaddr ifru_broadaddr;
struct sockaddr ifru_netmask;
struct sockaddr ifru_hwaddr;
short int ifru_flags;
int ifru_ivalue;
int ifru_mtu;
struct ifmap ifru_map;
char ifru_slave[IFNAMSIZ];
char ifru_newname[IFNAMSIZ];
__caddr_t ifru_data;
} ifr_ifru;
};
該結構包含兩個 union,第一個是接口名,如 eth0、wlan0 等。可以通過 ioctl() 函數獲取對應接口的信息:IP 地址、MAC 地址、接口索引等。
3. 以太網首部結構位於 net/ethernet.h
struct ether_header
{
u_int8_t ether_dhost[ETH_ALEN]; /* destination eth addr */
u_int8_t ether_shost[ETH_ALEN]; /* source ether addr */
u_int16_t ether_type; /* packet type ID field */
} __attribute__ ((__packed__));
ether_type 為幀類型,常見的有 IP、ARP、RARP,都有對應的宏定義。
4. ARP 包結構位於 netinet/if_ether.h
struct ether_arp {
struct arphdr ea_hdr; /* fixed-size header */
u_int8_t arp_sha[ETH_ALEN]; /* sender hardware address */
u_int8_t arp_spa[4]; /* sender protocol address */
u_int8_t arp_tha[ETH_ALEN]; /* target hardware address */
u_int8_t arp_tpa[4]; /* target protocol address */
};
#define arp_hrd ea_hdr.ar_hrd
#define arp_pro ea_hdr.ar_pro
#define arp_hln ea_hdr.ar_hln
#define arp_pln ea_hdr.ar_pln
#define arp_op ea_hdr.ar_op
上面的 ether_arp 結構還包含一個 ARP 首部,位於 net/if_arp.h:
struct arphdr
{
unsigned short int ar_hrd; /* Format of hardware address. */
unsigned short int ar_pro; /* Format of protocol address. */
unsigned char ar_hln; /* Length of hardware address. */
unsigned char ar_pln; /* Length of protocol address. */
unsigned short int ar_op; /* ARP opcode (command). */
}
/**
* @file arp_request.c
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/ioctl.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <netinet/in.h>
#include <netinet/if_ether.h>
#include <net/ethernet.h>
#include <net/if_arp.h>
#include <net/if.h>
#include <netpacket/packet.h>
#define ETHER_HEADER_LEN sizeof(struct ether_header)
#define ETHER_ARP_LEN sizeof(struct ether_arp)
#define ETHER_ARP_PACKET_LEN ETHER_HEADER_LEN + ETHER_ARP_LEN
#define IP_ADDR_LEN 4
#define BROADCAST_ADDR {0xff, 0xff, 0xff, 0xff, 0xff, 0xff}
void err_exit(const char *err_msg)
{
perror(err_msg);
exit(1);
}
struct ether_arp *fill_arp_packet(const unsigned char *src_mac_addr, const char *src_ip, const char *dst_ip)
{
struct ether_arp *arp_packet;
struct in_addr src_in_addr, dst_in_addr;
unsigned char dst_mac_addr[ETH_ALEN] = BROADCAST_ADDR;
inet_pton(AF_INET, src_ip, &src_in_addr);
inet_pton(AF_INET, dst_ip, &dst_in_addr);
arp_packet = (struct ether_arp *)malloc(ETHER_ARP_LEN);
arp_packet->arp_hrd = htons(ARPHRD_ETHER);
arp_packet->arp_pro = htons(ETHERTYPE_IP);
arp_packet->arp_hln = ETH_ALEN;
arp_packet->arp_pln = IP_ADDR_LEN;
arp_packet->arp_op = htons(ARPOP_REQUEST);
memcpy(arp_packet->arp_sha, src_mac_addr, ETH_ALEN);
memcpy(arp_packet->arp_tha, dst_mac_addr, ETH_ALEN);
memcpy(arp_packet->arp_spa, &src_in_addr, IP_ADDR_LEN);
memcpy(arp_packet->arp_tpa, &dst_in_addr, IP_ADDR_LEN);
return arp_packet;
}
void arp_request(const char *if_name, const char *dst_ip)
{
struct sockaddr_ll saddr_ll;
struct ether_header *eth_header;
struct ether_arp *arp_packet;
struct ifreq ifr;
char buf[ETHER_ARP_PACKET_LEN];
unsigned char src_mac_addr[ETH_ALEN];
unsigned char dst_mac_addr[ETH_ALEN] = BROADCAST_ADDR;
char *src_ip;
int sock_raw_fd, ret_len, i;
if ((sock_raw_fd = socket(PF_PACKET, SOCK_RAW, htons(ETH_P_ARP))) == -1)
err_exit("socket()");
bzero(&saddr_ll, sizeof(struct sockaddr_ll));
bzero(&ifr, sizeof(struct ifreq));
memcpy(ifr.ifr_name, if_name, strlen(if_name));
if (ioctl(sock_raw_fd, SIOCGIFINDEX, &ifr) == -1)
err_exit("ioctl() get ifindex");
saddr_ll.sll_ifindex = ifr.ifr_ifindex;
saddr_ll.sll_family = PF_PACKET;
if (ioctl(sock_raw_fd, SIOCGIFADDR, &ifr) == -1)
err_exit("ioctl() get ip");
src_ip = inet_ntoa(((struct sockaddr_in *)&(ifr.ifr_addr))->sin_addr);
printf("local ip:%s\n", src_ip);
if (ioctl(sock_raw_fd, SIOCGIFHWADDR, &ifr))
err_exit("ioctl() get mac");
memcpy(src_mac_addr, ifr.ifr_hwaddr.sa_data, ETH_ALEN);
printf("local mac");
for (i = 0; i < ETH_ALEN; i++)
printf(":%02x", src_mac_addr[i]);
printf("\n");
bzero(buf, ETHER_ARP_PACKET_LEN);
eth_header = (struct ether_header *)buf;
memcpy(eth_header->ether_shost, src_mac_addr, ETH_ALEN);
memcpy(eth_header->ether_dhost, dst_mac_addr, ETH_ALEN);
eth_header->ether_type = htons(ETHERTYPE_ARP);
arp_packet = fill_arp_packet(src_mac_addr, src_ip, dst_ip);
memcpy(buf + ETHER_HEADER_LEN, arp_packet, ETHER_ARP_LEN);
ret_len = sendto(sock_raw_fd, buf, ETHER_ARP_PACKET_LEN, 0, (struct sockaddr *)&saddr_ll, sizeof(struct sockaddr_ll));
if ( ret_len > 0)
printf("sendto() ok!!!\n");
close(sock_raw_fd);
}
int main(int argc, const char *argv[])
{
if (argc != 3)
{
printf("usage:%s device_name dst_ip\n", argv[0]);
exit(1);
}
arp_request(argv[1], argv[2]);
return 0;
}
流程解析:命令行接收網卡接口名與目標 IP 地址,傳入 arp_request() 函數。先以 PF_PACKET 選項創建 ARP 類型的原始套接字,再用 ioctl() 通過接口名獲取 MAC 地址、IP 地址和接口索引,並把接口索引填入物理地址結構 sockaddr_ll。接著填充分以太首部,源地址填剛才拿到的網卡 MAC,目標地址填廣播地址,幀類型設為 ETHERTYPE_ARP。然後填充 ARP 數據包,同樣寫入源/目標的 IP 與 MAC,操作選項填 ARPOP_REQUEST 表示請求。全部填好後,通過 sendto() 發送到前面準備好的 sockaddr_ll。
/**
* @file arp_recv.c
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <netinet/in.h>
#include <netinet/if_ether.h>
#include <net/if_arp.h>
#include <net/ethernet.h>
#define ETHER_HEADER_LEN sizeof(struct ether_header)
#define ETHER_ARP_LEN sizeof(struct ether_arp)
#define ETHER_ARP_PACKET_LEN ETHER_HEADER_LEN + ETHER_ARP_LEN
#define IP_ADDR_LEN 4
void err_exit(const char *err_msg)
{
perror(err_msg);
exit(1);
}
int main(void)
{
struct ether_arp *arp_packet;
char buf[ETHER_ARP_PACKET_LEN];
int sock_raw_fd, ret_len, i;
if ((sock_raw_fd = socket(PF_PACKET, SOCK_RAW, htons(ETH_P_ARP))) == -1)
err_exit("socket()");
while (1)
{
bzero(buf, ETHER_ARP_PACKET_LEN);
ret_len = recv(sock_raw_fd, buf, ETHER_ARP_PACKET_LEN, 0);
if (ret_len > 0)
{
arp_packet = (struct ether_arp *)(buf + ETHER_HEADER_LEN);
if (ntohs(arp_packet->arp_op) == 2)
{
printf("==========================arp replay======================\n");
printf("from ip:");
for (i = 0; i < IP_ADDR_LEN; i++)
printf(".%u", arp_packet->arp_spa[i]);
printf("\nfrom mac");
for (i = 0; i < ETH_ALEN; i++)
printf(":%02x", arp_packet->arp_sha[i]);
printf("\n");
}
}
}
close(sock_raw_fd);
return 0;
}
流程解析:創建 ARP 類型的原始套接字後,直接調用接收函數,就能收到網卡收到的 ARP 數據包。判斷操作碼是否為 2(ARP 應答),若是,則剝去以太首部,取出源 MAC 地址與 IP 地址並打印。
為了更直觀,可以打開 Wireshark 一起觀察。這裡是 wlan 環境,監聽 wlan0。注意原始套接字需要以 root 身份運行:先啟動 arp_recv,再運行 arp_request 發送 ARP 請求。
Wireshark 的抓包結果中可以看到,第一條數據包在詢問「誰是 192.168.0.1」,緊接著第二條數據包回了一個 reply,Wireshark 裡顯示 Opcode: reply(2),其源 IP 和 MAC 地址跟我們自己接收程序打印出來的完全一致。
由此可見,借助原始套接字,我們完全可以在用戶態自己構造並解析二層數據包,深入理解 ARP 的運作機制。