-
Notifications
You must be signed in to change notification settings - Fork 40
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Enable ability to use blocklist to block packets from specified interface. Support blocklist in vwifi kernel module, used as interfaces pair such as "owl2 blocks owl1", allow maximum blocklist size to be 1024 bytes now and maintained as global content within struct owl_content. When we detect the packet's source interface and destination interface is in the blocklist, we discard the packet. Using userspace program with netlink to communicate with kernel and allow the ability to dynamically alter the blocklist maintaining in vwifi kernel module.
- Loading branch information
Showing
5 changed files
with
232 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
#include <linux/netlink.h> | ||
#include <stdio.h> | ||
#include <stdlib.h> | ||
#include <string.h> | ||
#include <sys/socket.h> | ||
#include <unistd.h> | ||
|
||
#define MAX_PAYLOAD 1024 | ||
#define LINE_LENGTH 20 | ||
|
||
int main(int argc, char *argv[]) | ||
{ | ||
/* Read interface pair from blocklist.txt */ | ||
if (argc != 2) { | ||
printf("Error: Unspecified file name\n"); | ||
exit(1); | ||
} | ||
char *filename = argv[1]; | ||
FILE *fp = fopen(filename, "r"); | ||
if (!fp) { | ||
printf("Error: Couldn't open file %s\n", filename); | ||
exit(1); | ||
} | ||
|
||
char buffer[NLMSG_SPACE(MAX_PAYLOAD)]; | ||
char read_buf[LINE_LENGTH]; | ||
memset(buffer, '\0', sizeof(buffer)); | ||
|
||
for (int i = 1; fgets(read_buf, LINE_LENGTH, fp); i++) { | ||
if (strlen(read_buf) + strlen(buffer) < NLMSG_SPACE(MAX_PAYLOAD)) | ||
strcat(buffer, read_buf); | ||
else { | ||
printf( | ||
"Error: Blocklist size exceeds the maximum size of buffer\n"); | ||
exit(1); | ||
} | ||
} | ||
fclose(fp); | ||
|
||
printf("%s\n", buffer); | ||
|
||
int sock_fd = socket(PF_NETLINK, SOCK_RAW, MAX_LINKS); | ||
if (sock_fd < 0) { | ||
printf("Error: Can't open socket\n"); | ||
exit(1); | ||
} | ||
|
||
struct sockaddr_nl src_addr = { | ||
.nl_family = AF_NETLINK, | ||
.nl_pid = getpid(), | ||
}; | ||
|
||
bind(sock_fd, (struct sockaddr *) &src_addr, sizeof(src_addr)); | ||
|
||
struct sockaddr_nl dest_addr = { | ||
.nl_family = AF_NETLINK, | ||
.nl_pid = 0, | ||
.nl_groups = 0, | ||
}; | ||
|
||
struct nlmsghdr *nlh = | ||
(struct nlmsghdr *) calloc(1, NLMSG_SPACE(MAX_PAYLOAD)); | ||
nlh->nlmsg_len = NLMSG_SPACE(MAX_PAYLOAD); | ||
nlh->nlmsg_pid = getpid(); | ||
nlh->nlmsg_flags = 0; | ||
|
||
strncpy(NLMSG_DATA(nlh), buffer, NLMSG_SPACE(MAX_PAYLOAD)); | ||
|
||
struct iovec iov = { | ||
.iov_base = (void *) nlh, | ||
.iov_len = nlh->nlmsg_len, | ||
}; | ||
|
||
struct msghdr msg = { | ||
.msg_name = (void *) &dest_addr, | ||
.msg_namelen = sizeof(dest_addr), | ||
.msg_iov = &iov, | ||
.msg_iovlen = 1, | ||
}; | ||
|
||
printf("Configuring blocklist for vwifi...\n"); | ||
sendmsg(sock_fd, &msg, 0); | ||
|
||
recvmsg(sock_fd, &msg, 0); | ||
printf("Message from vwifi: %s\n", (char *) NLMSG_DATA(nlh)); | ||
|
||
close(sock_fd); | ||
|
||
return 0; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
owl2 blocks owl1 | ||
owl1 blocks owl2 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters