#include #include #include #include #include #include #include #include #include #include #include char packet[] = "\xff\xff\xff\xff\xff\xff" "\x00\x1c\x25\xa0\xb7\x2e" "\x08\x06\x00\x01" "\x08\x00\x06\x04\x00\x01" "\x00\x1c\x25\xa0\xb7\x2e" "\x0a\x00\x00\x0d" "\xff\xff\xff\xff\xff\xff" "\x0a\x00\x00\x3d" "\x00\x00\x00\x00\x00\x00" "\x00\x00\x00\x00\x00\x00" "\x00\x00\x00\x00\x00\x00" ; int send_bpf(const char *iface, char *data, size_t len) { int fd = open("/dev/bpf", O_RDWR); if (fd < 0) { perror("open"); return -1; } struct ifreq ifr; (void) memset(&ifr, 0, sizeof ifr); (void) memcpy(&ifr.ifr_name, iface, strlen(iface)); if (ioctl(fd, BIOCSETIF, &ifr) < 0) { perror("BIOCSETIF"); return -2; } int enable; enable = 1; if (ioctl(fd, BIOCIMMEDIATE, &enable) < 0) { perror("BIOCIMMEDIATE"); return -3; } enable = 1; if (ioctl(fd, BIOCSHDRCMPLT, &enable) < 0) { perror("BIOCSHDRCMPLT"); return -4; } size_t ret = write(fd, data, len); if (ret >= 0) printf("send_bpf sent %ld bytes\n", ret); if (ret != len) { fprintf(stderr, "write returned %ld, expected %ld\n", ret, len); return -5; } if (close(fd) < 0) { perror("close"); return -6; } return 0; } int send_socket(const char *iface, char *data, size_t len) { int s = socket(AF_PACKET, SOCK_RAW, 0); if (s < 0) { perror("socket"); return -1; } struct ifreq ifr; (void) memset(&ifr, 0, sizeof ifr); (void) memcpy(&ifr.ifr_name, iface, strlen(iface)); if (ioctl(s, SIOCGIFINDEX, &ifr) < 0) { perror("SIOCGIFINDEX"); return -2; } struct sockaddr_ll lls; (void) memset(&lls, 0, sizeof lls); lls.sll_family = AF_PACKET; lls.sll_protocol = 0; lls.sll_ifindex = ifr.ifr_index; if (bind(s, (struct sockaddr *)&lls, sizeof lls) < 0) { perror("bind"); return -3; } size_t ret = send(s, data, len, 0); if (ret >= 0) printf("send_socket sent %ld bytes\n", ret); if (ret != len) { fprintf(stderr, "send returned %ld, expected %ld\n", ret, len); return -4; } if (close(s) < 0) { perror("close"); return -5; } return 0; } int main(int argc, char *argv[]) { if (argc != 2) { fprintf(stderr, "Missing interface\n"); return 1; } int ret; ret = send_bpf(argv[1], packet, sizeof packet - 1); if (ret != 0) fprintf(stderr, "send_bpf: %d\n", ret); ret = send_socket(argv[1], packet, sizeof packet - 1); if (ret != 0) fprintf(stderr, "send_socket: %d\n", ret); return 0; }