2024-07-22 23:06:02 +00:00
|
|
|
#define _GNU_SOURCE
|
2024-07-30 20:10:00 +00:00
|
|
|
#ifndef __linux__
|
|
|
|
#error "The package is linux only!"
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef KERNEL_SPACE
|
|
|
|
#error "The build aims to the kernel, not userspace"
|
|
|
|
#endif
|
|
|
|
|
2024-07-21 14:44:46 +00:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
|
2024-08-10 23:10:18 +00:00
|
|
|
#include <linux/netfilter/nfnetlink_queue.h>
|
2024-07-21 14:44:46 +00:00
|
|
|
#include <libmnl/libmnl.h>
|
|
|
|
#include <libnetfilter_queue/libnetfilter_queue.h>
|
|
|
|
#include <libnetfilter_queue/libnetfilter_queue_ipv4.h>
|
|
|
|
#include <libnetfilter_queue/libnetfilter_queue_tcp.h>
|
|
|
|
#include <libnetfilter_queue/libnetfilter_queue_udp.h>
|
|
|
|
#include <libnetfilter_queue/pktbuff.h>
|
|
|
|
#include <netinet/ip.h>
|
|
|
|
#include <netinet/tcp.h>
|
|
|
|
#include <netinet/udp.h>
|
|
|
|
|
|
|
|
#include <linux/if_ether.h>
|
2024-08-04 09:57:16 +00:00
|
|
|
#include <linux/netfilter.h>
|
2024-07-30 12:01:58 +00:00
|
|
|
#include <pthread.h>
|
2024-08-04 09:57:16 +00:00
|
|
|
#include <sys/socket.h>
|
2024-07-30 12:01:58 +00:00
|
|
|
|
2024-08-03 23:20:09 +00:00
|
|
|
#include "config.h"
|
2024-08-04 09:57:16 +00:00
|
|
|
#include "mangle.h"
|
2024-08-10 18:44:04 +00:00
|
|
|
#include "args.h"
|
2024-07-21 14:44:46 +00:00
|
|
|
|
2024-08-07 10:32:01 +00:00
|
|
|
pthread_mutex_t rawsocket_lock;
|
2024-08-10 23:10:18 +00:00
|
|
|
int rawsocket = -2;
|
|
|
|
|
2024-07-30 12:01:58 +00:00
|
|
|
static int open_socket(struct mnl_socket **_nl) {
|
2024-07-21 14:44:46 +00:00
|
|
|
struct mnl_socket *nl = NULL;
|
|
|
|
nl = mnl_socket_open(NETLINK_NETFILTER);
|
|
|
|
|
|
|
|
if (nl == NULL) {
|
|
|
|
perror("mnl_socket_open");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (mnl_socket_bind(nl, 0, MNL_SOCKET_AUTOPID) < 0) {
|
|
|
|
perror("mnl_socket_bind");
|
|
|
|
mnl_socket_close(nl);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2024-07-30 12:01:58 +00:00
|
|
|
*_nl = nl;
|
2024-07-21 14:44:46 +00:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2024-07-30 12:01:58 +00:00
|
|
|
static int close_socket(struct mnl_socket **_nl) {
|
|
|
|
struct mnl_socket *nl = *_nl;
|
|
|
|
if (nl == NULL) return 1;
|
|
|
|
if (mnl_socket_close(nl) < 0) {
|
2024-07-21 14:44:46 +00:00
|
|
|
perror("mnl_socket_close");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2024-07-30 12:01:58 +00:00
|
|
|
*_nl = NULL;
|
2024-07-21 14:44:46 +00:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int open_raw_socket(void) {
|
2024-08-10 23:10:18 +00:00
|
|
|
if (rawsocket != -2) {
|
2024-07-21 14:44:46 +00:00
|
|
|
errno = EALREADY;
|
|
|
|
perror("Raw socket is already opened");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2024-08-10 23:10:18 +00:00
|
|
|
rawsocket = socket(AF_INET, SOCK_RAW, IPPROTO_RAW);
|
|
|
|
if (rawsocket == -1) {
|
2024-07-21 14:44:46 +00:00
|
|
|
perror("Unable to create raw socket");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
int mark = RAWSOCKET_MARK;
|
2024-08-10 23:10:18 +00:00
|
|
|
if (setsockopt(rawsocket, SOL_SOCKET, SO_MARK, &mark, sizeof(mark)) < 0)
|
2024-07-21 14:44:46 +00:00
|
|
|
{
|
2024-07-30 12:01:58 +00:00
|
|
|
fprintf(stderr, "setsockopt(SO_MARK, %d) failed\n", mark);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2024-08-07 10:32:01 +00:00
|
|
|
int mst = pthread_mutex_init(&rawsocket_lock, NULL);
|
2024-07-30 12:01:58 +00:00
|
|
|
if (mst) {
|
|
|
|
fprintf(stderr, "Mutex err: %d\n", mst);
|
2024-08-10 23:10:18 +00:00
|
|
|
close(rawsocket);
|
2024-07-30 12:01:58 +00:00
|
|
|
errno = mst;
|
|
|
|
|
2024-07-21 14:44:46 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2024-07-30 12:01:58 +00:00
|
|
|
|
2024-08-10 23:10:18 +00:00
|
|
|
return rawsocket;
|
2024-07-21 14:44:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static int close_raw_socket(void) {
|
2024-08-10 23:10:18 +00:00
|
|
|
if (rawsocket < 0) {
|
2024-07-21 14:44:46 +00:00
|
|
|
errno = EALREADY;
|
|
|
|
perror("Raw socket is not set");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2024-08-10 23:10:18 +00:00
|
|
|
if (close(rawsocket)) {
|
2024-07-21 14:44:46 +00:00
|
|
|
perror("Unable to close raw socket");
|
2024-08-07 10:32:01 +00:00
|
|
|
pthread_mutex_destroy(&rawsocket_lock);
|
2024-07-21 14:44:46 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2024-08-07 10:32:01 +00:00
|
|
|
pthread_mutex_destroy(&rawsocket_lock);
|
2024-07-30 12:01:58 +00:00
|
|
|
|
2024-08-10 23:10:18 +00:00
|
|
|
rawsocket = -2;
|
2024-07-21 14:44:46 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2024-07-21 22:43:02 +00:00
|
|
|
|
2024-07-30 20:10:00 +00:00
|
|
|
static int send_raw_socket(const uint8_t *pkt, uint32_t pktlen) {
|
2024-08-04 09:57:16 +00:00
|
|
|
int ret;
|
|
|
|
|
2024-07-30 20:10:00 +00:00
|
|
|
if (pktlen > AVAILABLE_MTU) {
|
2024-08-07 00:31:10 +00:00
|
|
|
if (config.verbose)
|
|
|
|
printf("Split packet!\n");
|
2024-07-21 22:43:02 +00:00
|
|
|
|
2024-07-30 20:10:00 +00:00
|
|
|
uint8_t buff1[MNL_SOCKET_BUFFER_SIZE];
|
|
|
|
uint32_t buff1_size = MNL_SOCKET_BUFFER_SIZE;
|
|
|
|
uint8_t buff2[MNL_SOCKET_BUFFER_SIZE];
|
|
|
|
uint32_t buff2_size = MNL_SOCKET_BUFFER_SIZE;
|
2024-07-21 22:43:02 +00:00
|
|
|
|
2024-08-07 00:31:10 +00:00
|
|
|
switch (config.fragmentation_strategy) {
|
|
|
|
case FRAG_STRAT_TCP:
|
|
|
|
if ((ret = tcp4_frag(pkt, pktlen, AVAILABLE_MTU-128,
|
|
|
|
buff1, &buff1_size, buff2, &buff2_size)) < 0) {
|
|
|
|
|
|
|
|
errno = -ret;
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case FRAG_STRAT_IP:
|
|
|
|
if ((ret = ip4_frag(pkt, pktlen, AVAILABLE_MTU-128,
|
|
|
|
buff1, &buff1_size, buff2, &buff2_size)) < 0) {
|
|
|
|
|
|
|
|
errno = -ret;
|
|
|
|
return ret;
|
2024-08-07 10:32:01 +00:00
|
|
|
}
|
2024-08-07 00:31:10 +00:00
|
|
|
break;
|
2024-08-07 10:32:01 +00:00
|
|
|
default:
|
|
|
|
errno = EINVAL;
|
|
|
|
printf("send_raw_socket: Packet is too big but fragmentation is disabled!\n");
|
2024-08-07 00:31:10 +00:00
|
|
|
return -EINVAL;
|
2024-08-04 09:57:16 +00:00
|
|
|
}
|
2024-07-21 22:43:02 +00:00
|
|
|
|
|
|
|
int sent = 0;
|
2024-07-30 20:10:00 +00:00
|
|
|
int status = send_raw_socket(buff1, buff1_size);
|
2024-07-21 22:43:02 +00:00
|
|
|
|
|
|
|
if (status >= 0) sent += status;
|
|
|
|
else {
|
|
|
|
return status;
|
|
|
|
}
|
|
|
|
|
2024-07-30 20:10:00 +00:00
|
|
|
status = send_raw_socket(buff2, buff2_size);
|
2024-07-21 22:43:02 +00:00
|
|
|
if (status >= 0) sent += status;
|
|
|
|
else {
|
|
|
|
return status;
|
|
|
|
}
|
|
|
|
|
|
|
|
return sent;
|
|
|
|
}
|
|
|
|
|
2024-07-21 14:44:46 +00:00
|
|
|
|
2024-07-30 20:10:00 +00:00
|
|
|
struct iphdr *iph;
|
|
|
|
|
2024-08-04 09:57:16 +00:00
|
|
|
if ((ret = ip4_payload_split(
|
2024-07-30 20:10:00 +00:00
|
|
|
(uint8_t *)pkt, pktlen, &iph, NULL, NULL, NULL)) < 0) {
|
2024-08-04 09:57:16 +00:00
|
|
|
errno = -ret;
|
|
|
|
return ret;
|
2024-07-30 20:10:00 +00:00
|
|
|
}
|
2024-07-21 14:44:46 +00:00
|
|
|
|
|
|
|
struct sockaddr_in daddr = {
|
|
|
|
.sin_family = AF_INET,
|
2024-08-04 09:57:16 +00:00
|
|
|
/* Always 0 for raw socket */
|
|
|
|
.sin_port = 0,
|
2024-07-21 14:44:46 +00:00
|
|
|
.sin_addr = {
|
|
|
|
.s_addr = iph->daddr
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2024-08-07 10:32:01 +00:00
|
|
|
pthread_mutex_lock(&rawsocket_lock);
|
2024-07-30 12:01:58 +00:00
|
|
|
|
2024-08-10 23:10:18 +00:00
|
|
|
int sent = sendto(rawsocket,
|
2024-07-30 20:10:00 +00:00
|
|
|
pkt, pktlen, 0,
|
2024-07-21 14:44:46 +00:00
|
|
|
(struct sockaddr *)&daddr, sizeof(daddr));
|
2024-07-30 12:01:58 +00:00
|
|
|
|
2024-08-07 10:32:01 +00:00
|
|
|
pthread_mutex_unlock(&rawsocket_lock);
|
2024-07-30 12:01:58 +00:00
|
|
|
|
2024-08-04 09:57:16 +00:00
|
|
|
/* The function will return -errno on error as well as errno value set itself */
|
|
|
|
if (sent < 0) sent = -errno;
|
|
|
|
|
2024-07-21 14:44:46 +00:00
|
|
|
return sent;
|
|
|
|
}
|
|
|
|
|
2024-08-10 23:10:18 +00:00
|
|
|
|
|
|
|
|
2024-07-21 14:44:46 +00:00
|
|
|
struct packet_data {
|
|
|
|
uint32_t id;
|
|
|
|
uint16_t hw_proto;
|
|
|
|
uint8_t hook;
|
|
|
|
|
|
|
|
void *payload;
|
|
|
|
uint16_t payload_len;
|
|
|
|
};
|
|
|
|
|
2024-07-30 12:01:58 +00:00
|
|
|
// Per-queue data. Passed to queue_cb.
|
|
|
|
struct queue_data {
|
|
|
|
struct mnl_socket **_nl;
|
|
|
|
int queue_num;
|
|
|
|
};
|
2024-07-30 20:10:00 +00:00
|
|
|
|
2024-07-30 12:01:58 +00:00
|
|
|
/**
|
|
|
|
* Used to accept unsupported packets (GSOs)
|
|
|
|
*/
|
|
|
|
static int fallback_accept_packet(uint32_t id, struct queue_data qdata) {
|
|
|
|
char buf[MNL_SOCKET_BUFFER_SIZE];
|
|
|
|
struct nlmsghdr *verdnlh;
|
|
|
|
verdnlh = nfq_nlmsg_put(buf, NFQNL_MSG_VERDICT, qdata.queue_num);
|
|
|
|
nfq_nlmsg_verdict_put(verdnlh, id, NF_ACCEPT);
|
|
|
|
|
|
|
|
if (mnl_socket_sendto(*qdata._nl, verdnlh, verdnlh->nlmsg_len) < 0) {
|
|
|
|
perror("mnl_socket_send");
|
|
|
|
return MNL_CB_ERROR;
|
|
|
|
}
|
|
|
|
|
|
|
|
return MNL_CB_OK;
|
|
|
|
}
|
|
|
|
|
2024-07-21 14:44:46 +00:00
|
|
|
|
2024-08-01 09:35:03 +00:00
|
|
|
struct dps_t {
|
2024-08-03 23:20:09 +00:00
|
|
|
uint8_t *pkt;
|
|
|
|
uint32_t pktlen;
|
2024-08-01 09:35:03 +00:00
|
|
|
// Time for the packet in milliseconds
|
|
|
|
uint32_t timer;
|
|
|
|
};
|
|
|
|
// Note that the thread will automatically release dps_t and pkt_buff
|
2024-08-10 23:10:18 +00:00
|
|
|
void *delay_packet_send_fn(void *data) {
|
2024-08-01 09:35:03 +00:00
|
|
|
struct dps_t *dpdt = data;
|
2024-08-03 23:20:09 +00:00
|
|
|
|
|
|
|
uint8_t *pkt = dpdt->pkt;
|
|
|
|
uint32_t pktlen = dpdt->pktlen;
|
2024-08-01 09:35:03 +00:00
|
|
|
|
|
|
|
usleep(dpdt->timer * 1000);
|
2024-08-04 09:57:16 +00:00
|
|
|
int ret = send_raw_socket(pkt, pktlen);
|
|
|
|
if (ret < 0) {
|
|
|
|
errno = -ret;
|
|
|
|
perror("send delayed raw packet");
|
|
|
|
}
|
2024-07-21 14:44:46 +00:00
|
|
|
|
2024-08-03 23:20:09 +00:00
|
|
|
free(pkt);
|
2024-08-01 09:35:03 +00:00
|
|
|
free(dpdt);
|
|
|
|
return NULL;
|
|
|
|
}
|
2024-08-07 00:31:10 +00:00
|
|
|
|
2024-08-10 23:10:18 +00:00
|
|
|
void delay_packet_send(const unsigned char *data, unsigned int data_len, unsigned int delay_ms) {
|
|
|
|
struct dps_t *dpdt = malloc(sizeof(struct dps_t));
|
|
|
|
dpdt->pkt = malloc(data_len);
|
|
|
|
memcpy(dpdt->pkt, data, data_len);
|
|
|
|
dpdt->pktlen = data_len;
|
|
|
|
dpdt->timer = delay_ms;
|
|
|
|
pthread_t thr;
|
|
|
|
pthread_create(&thr, NULL, delay_packet_send_fn, dpdt);
|
|
|
|
pthread_detach(thr);
|
|
|
|
}
|
2024-07-21 14:44:46 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
2024-08-10 23:10:18 +00:00
|
|
|
|
2024-07-21 14:44:46 +00:00
|
|
|
static int queue_cb(const struct nlmsghdr *nlh, void *data) {
|
2024-08-10 23:10:18 +00:00
|
|
|
char buf[MNL_SOCKET_BUFFER_SIZE];
|
2024-07-30 12:01:58 +00:00
|
|
|
|
|
|
|
struct queue_data *qdata = data;
|
|
|
|
|
2024-07-21 14:44:46 +00:00
|
|
|
struct nfqnl_msg_packet_hdr *ph = NULL;
|
|
|
|
struct nlattr *attr[NFQA_MAX+1] = {0};
|
|
|
|
struct packet_data packet = {0};
|
|
|
|
|
|
|
|
if (nfq_nlmsg_parse(nlh, attr) < 0) {
|
|
|
|
perror("Attr parse");
|
|
|
|
return MNL_CB_ERROR;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (attr[NFQA_PACKET_HDR] == NULL) {
|
|
|
|
errno = ENODATA;
|
|
|
|
perror("Metaheader not set");
|
|
|
|
return MNL_CB_ERROR;
|
|
|
|
}
|
2024-07-21 22:43:02 +00:00
|
|
|
|
2024-07-21 14:44:46 +00:00
|
|
|
ph = mnl_attr_get_payload(attr[NFQA_PACKET_HDR]);
|
|
|
|
|
|
|
|
packet.id = ntohl(ph->packet_id);
|
|
|
|
packet.hw_proto = ntohs(ph->hw_protocol);
|
|
|
|
packet.hook = ph->hook;
|
|
|
|
packet.payload_len = mnl_attr_get_payload_len(attr[NFQA_PAYLOAD]);
|
|
|
|
packet.payload = mnl_attr_get_payload(attr[NFQA_PAYLOAD]);
|
|
|
|
|
2024-07-21 22:43:02 +00:00
|
|
|
if (attr[NFQA_CAP_LEN] != NULL && ntohl(mnl_attr_get_u32(attr[NFQA_CAP_LEN])) != packet.payload_len) {
|
|
|
|
fprintf(stderr, "The packet was truncated! Skip!\n");
|
2024-07-30 12:01:58 +00:00
|
|
|
return fallback_accept_packet(packet.id, *qdata);
|
2024-07-21 14:44:46 +00:00
|
|
|
}
|
|
|
|
|
2024-07-21 21:14:11 +00:00
|
|
|
if (attr[NFQA_MARK] != NULL) {
|
|
|
|
// Skip packets sent by rawsocket to escape infinity loop.
|
2024-08-02 20:00:08 +00:00
|
|
|
if ((ntohl(mnl_attr_get_u32(attr[NFQA_MARK])) & RAWSOCKET_MARK) ==
|
2024-07-21 21:14:11 +00:00
|
|
|
RAWSOCKET_MARK) {
|
2024-07-30 12:01:58 +00:00
|
|
|
return fallback_accept_packet(packet.id, *qdata);
|
2024-07-21 21:14:11 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-07-22 06:57:51 +00:00
|
|
|
|
2024-08-10 23:10:18 +00:00
|
|
|
struct nlmsghdr *verdnlh;
|
|
|
|
verdnlh = nfq_nlmsg_put(buf, NFQNL_MSG_VERDICT, qdata->queue_num);
|
|
|
|
|
|
|
|
int ret = process_packet(packet.payload, packet.payload_len);
|
|
|
|
|
|
|
|
switch (ret) {
|
|
|
|
case PKT_DROP:
|
|
|
|
nfq_nlmsg_verdict_put(verdnlh, packet.id, NF_DROP);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
nfq_nlmsg_verdict_put(verdnlh, packet.id, NF_ACCEPT);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (mnl_socket_sendto(*qdata->_nl, verdnlh, verdnlh->nlmsg_len) < 0) {
|
|
|
|
perror("mnl_socket_send");
|
|
|
|
return MNL_CB_ERROR;
|
|
|
|
}
|
|
|
|
|
|
|
|
return MNL_CB_OK;
|
2024-07-21 14:44:46 +00:00
|
|
|
}
|
|
|
|
|
2024-07-30 20:10:00 +00:00
|
|
|
#define BUF_SIZE (0xffff + (MNL_SOCKET_BUFFER_SIZE / 2))
|
|
|
|
|
2024-07-30 12:01:58 +00:00
|
|
|
int init_queue(int queue_num) {
|
|
|
|
struct mnl_socket *nl;
|
2024-07-23 14:18:51 +00:00
|
|
|
|
2024-07-30 12:01:58 +00:00
|
|
|
if (open_socket(&nl)) {
|
2024-07-21 14:44:46 +00:00
|
|
|
perror("Unable to open socket");
|
2024-07-30 12:01:58 +00:00
|
|
|
return -1;
|
2024-07-21 14:44:46 +00:00
|
|
|
}
|
|
|
|
|
2024-07-30 12:01:58 +00:00
|
|
|
uint32_t portid = mnl_socket_get_portid(nl);
|
2024-07-21 14:44:46 +00:00
|
|
|
|
|
|
|
struct nlmsghdr *nlh;
|
2024-07-30 20:10:00 +00:00
|
|
|
char buf[BUF_SIZE];
|
2024-07-21 14:44:46 +00:00
|
|
|
|
2024-07-30 12:01:58 +00:00
|
|
|
nlh = nfq_nlmsg_put(buf, NFQNL_MSG_CONFIG, queue_num);
|
2024-07-21 14:44:46 +00:00
|
|
|
nfq_nlmsg_cfg_put_cmd(nlh, AF_INET, NFQNL_CFG_CMD_BIND);
|
|
|
|
|
2024-07-30 12:01:58 +00:00
|
|
|
if (mnl_socket_sendto(nl, nlh, nlh->nlmsg_len) < 0) {
|
2024-07-21 14:44:46 +00:00
|
|
|
perror("mnl_socket_send");
|
2024-07-30 12:01:58 +00:00
|
|
|
goto die;
|
2024-07-21 14:44:46 +00:00
|
|
|
}
|
|
|
|
|
2024-07-30 12:01:58 +00:00
|
|
|
nlh = nfq_nlmsg_put(buf, NFQNL_MSG_CONFIG, queue_num);
|
2024-07-21 14:44:46 +00:00
|
|
|
nfq_nlmsg_cfg_put_params(nlh, NFQNL_COPY_PACKET, 0xffff);
|
|
|
|
|
2024-08-07 10:32:01 +00:00
|
|
|
if (config.use_gso) {
|
|
|
|
mnl_attr_put_u32(nlh, NFQA_CFG_FLAGS, htonl(NFQA_CFG_F_GSO));
|
|
|
|
mnl_attr_put_u32(nlh, NFQA_CFG_MASK, htonl(NFQA_CFG_F_GSO));
|
2024-08-07 00:31:10 +00:00
|
|
|
}
|
2024-07-21 14:44:46 +00:00
|
|
|
|
2024-07-30 12:01:58 +00:00
|
|
|
if (mnl_socket_sendto(nl, nlh, nlh->nlmsg_len) < 0) {
|
2024-07-21 14:44:46 +00:00
|
|
|
perror("mnl_socket_send");
|
2024-07-30 12:01:58 +00:00
|
|
|
goto die;
|
2024-07-21 14:44:46 +00:00
|
|
|
}
|
|
|
|
|
2024-07-30 12:01:58 +00:00
|
|
|
|
2024-07-21 14:44:46 +00:00
|
|
|
/* ENOBUFS is signalled to userspace when packets were lost
|
|
|
|
* on kernel side. In most cases, userspace isn't interested
|
|
|
|
* in this information, so turn it off.
|
|
|
|
*/
|
|
|
|
int ret = 1;
|
2024-07-30 12:01:58 +00:00
|
|
|
mnl_socket_setsockopt(nl, NETLINK_NO_ENOBUFS, &ret, sizeof(int));
|
|
|
|
|
|
|
|
struct queue_data qdata = {
|
|
|
|
._nl = &nl,
|
|
|
|
.queue_num = queue_num
|
|
|
|
};
|
|
|
|
|
2024-08-10 18:38:25 +00:00
|
|
|
printf("Queue %d started\n", qdata.queue_num);
|
2024-07-21 14:44:46 +00:00
|
|
|
|
|
|
|
while (1) {
|
2024-07-30 20:10:00 +00:00
|
|
|
ret = mnl_socket_recvfrom(nl, buf, BUF_SIZE);
|
2024-07-21 14:44:46 +00:00
|
|
|
if (ret == -1) {
|
|
|
|
perror("mnl_socket_recvfrom");
|
2024-07-30 12:01:58 +00:00
|
|
|
continue;
|
2024-07-21 14:44:46 +00:00
|
|
|
}
|
|
|
|
|
2024-07-30 12:01:58 +00:00
|
|
|
ret = mnl_cb_run(buf, ret, 0, portid, queue_cb, &qdata);
|
2024-07-21 14:44:46 +00:00
|
|
|
if (ret < 0) {
|
|
|
|
perror("mnl_cb_run");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2024-07-30 12:01:58 +00:00
|
|
|
close_socket(&nl);
|
2024-07-21 14:44:46 +00:00
|
|
|
return 0;
|
|
|
|
|
2024-07-30 12:01:58 +00:00
|
|
|
die:
|
|
|
|
close_socket(&nl);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Per-queue config. Used to initialize a queue. Passed to wrapper
|
|
|
|
struct queue_conf {
|
|
|
|
uint16_t i;
|
|
|
|
int queue_num;
|
|
|
|
};
|
|
|
|
|
|
|
|
struct queue_res {
|
|
|
|
int status;
|
|
|
|
};
|
2024-08-07 10:32:01 +00:00
|
|
|
static struct queue_res defqres = {0};
|
2024-07-30 12:01:58 +00:00
|
|
|
|
|
|
|
static struct queue_res threads_reses[MAX_THREADS];
|
|
|
|
|
|
|
|
void *init_queue_wrapper(void *qdconf) {
|
|
|
|
struct queue_conf *qconf = qdconf;
|
|
|
|
struct queue_res *thres = threads_reses + qconf->i;
|
|
|
|
|
|
|
|
thres->status = init_queue(qconf->queue_num);
|
|
|
|
|
|
|
|
fprintf(stderr, "Thread %d exited with status %d\n", qconf->i, thres->status);
|
|
|
|
|
|
|
|
return thres;
|
|
|
|
}
|
|
|
|
|
2024-08-10 23:10:18 +00:00
|
|
|
struct instance_config_t instance_config = {
|
|
|
|
.send_raw_packet = send_raw_socket,
|
|
|
|
.send_delayed_packet = delay_packet_send,
|
|
|
|
};
|
|
|
|
|
2024-08-09 12:40:04 +00:00
|
|
|
int main(int argc, char *argv[]) {
|
2024-08-09 21:35:36 +00:00
|
|
|
int ret;
|
|
|
|
if ((ret = parse_args(argc, argv)) != 0) {
|
|
|
|
if (ret < 0) {
|
2024-08-09 12:21:54 +00:00
|
|
|
perror("Unable to parse args");
|
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
}
|
|
|
|
exit(EXIT_SUCCESS);
|
2024-07-30 12:01:58 +00:00
|
|
|
}
|
|
|
|
|
2024-08-11 18:43:32 +00:00
|
|
|
print_version();
|
|
|
|
print_welcome();
|
2024-07-30 12:01:58 +00:00
|
|
|
|
|
|
|
if (open_raw_socket() < 0) {
|
|
|
|
perror("Unable to open raw socket");
|
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
}
|
|
|
|
|
2024-08-07 10:32:01 +00:00
|
|
|
struct queue_res *qres = &defqres;
|
2024-08-03 23:20:09 +00:00
|
|
|
|
2024-08-07 00:31:10 +00:00
|
|
|
if (config.threads == 1) {
|
|
|
|
struct queue_conf tconf = {
|
|
|
|
.i = 0,
|
|
|
|
.queue_num = config.queue_start_num
|
|
|
|
};
|
2024-08-03 23:20:09 +00:00
|
|
|
|
2024-08-07 00:31:10 +00:00
|
|
|
qres = init_queue_wrapper(&tconf);
|
2024-08-08 13:18:50 +00:00
|
|
|
} else {
|
2024-08-07 00:31:10 +00:00
|
|
|
printf("%d threads wil be used\n", config.threads);
|
2024-07-30 12:01:58 +00:00
|
|
|
|
2024-08-07 00:31:10 +00:00
|
|
|
struct queue_conf thread_confs[MAX_THREADS];
|
|
|
|
pthread_t threads[MAX_THREADS];
|
|
|
|
for (int i = 0; i < config.threads; i++) {
|
|
|
|
struct queue_conf *tconf = thread_confs + i;
|
|
|
|
pthread_t *thr = threads + i;
|
2024-07-30 12:01:58 +00:00
|
|
|
|
2024-08-07 00:31:10 +00:00
|
|
|
tconf->queue_num = config.queue_start_num + i;
|
|
|
|
tconf->i = i;
|
2024-07-30 12:01:58 +00:00
|
|
|
|
2024-08-07 00:31:10 +00:00
|
|
|
pthread_create(thr, NULL, init_queue_wrapper, tconf);
|
|
|
|
}
|
2024-07-30 12:01:58 +00:00
|
|
|
|
2024-08-07 00:31:10 +00:00
|
|
|
void *res;
|
|
|
|
for (int i = 0; i < config.threads; i++) {
|
|
|
|
pthread_join(threads[i], &res);
|
2024-07-30 12:01:58 +00:00
|
|
|
|
2024-08-07 00:31:10 +00:00
|
|
|
qres = res;
|
|
|
|
}
|
2024-07-30 12:01:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (close_raw_socket() < 0) {
|
|
|
|
perror("Unable to close raw socket");
|
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
}
|
|
|
|
|
|
|
|
return qres->status;
|
2024-07-21 14:44:46 +00:00
|
|
|
}
|
|
|
|
|