gluon-radv-filterd: updated socket filter

This commit is contained in:
Julian Labus 2016-12-15 13:26:29 +01:00 committed by Jan-Philipp Litza
parent a313af733d
commit 43664bf383
No known key found for this signature in database
GPG Key ID: 1FB658053CE27196

View File

@ -29,6 +29,7 @@
#include <signal.h> #include <signal.h>
#include <stdarg.h> #include <stdarg.h>
#include <stdbool.h> #include <stdbool.h>
#include <stddef.h>
#include <stdio.h> #include <stdio.h>
#include <stdint.h> #include <stdint.h>
#include <stdlib.h> #include <stdlib.h>
@ -173,21 +174,11 @@ static inline void warn_errno(const char *message) {
} }
static int init_packet_socket(unsigned int ifindex) { static int init_packet_socket(unsigned int ifindex) {
// generated by tcpdump -i tun "icmp6 and ip6[40] = 134" -dd
// Important: Generate on TUN interface (because the socket is SOCK_DGRAM)!
struct sock_filter radv_filter_code[] = { struct sock_filter radv_filter_code[] = {
{ 0x30, 0, 0, 0x00000000 }, BPF_STMT(BPF_LD|BPF_B|BPF_ABS, sizeof(struct ip6_hdr) + offsetof(struct icmp6_hdr, icmp6_type)),
{ 0x54, 0, 0, 0x000000f0 }, BPF_JUMP(BPF_JMP|BPF_JEQ|BPF_K, ND_ROUTER_ADVERT, 1, 0),
{ 0x15, 0, 8, 0x00000060 }, BPF_STMT(BPF_RET|BPF_K, 0),
{ 0x30, 0, 0, 0x00000006 }, BPF_STMT(BPF_RET|BPF_K, 0xffffffff),
{ 0x15, 3, 0, 0x0000003a },
{ 0x15, 0, 5, 0x0000002c },
{ 0x30, 0, 0, 0x00000028 },
{ 0x15, 0, 3, 0x0000003a },
{ 0x30, 0, 0, 0x00000028 },
{ 0x15, 0, 1, 0x00000086 },
{ 0x06, 0, 0, 0x0000ffff },
{ 0x06, 0, 0, 0x00000000 },
}; };
struct sock_fprog radv_filter = { struct sock_fprog radv_filter = {
@ -195,14 +186,16 @@ static int init_packet_socket(unsigned int ifindex) {
.filter = radv_filter_code, .filter = radv_filter_code,
}; };
int sock = socket(AF_PACKET, SOCK_DGRAM|SOCK_CLOEXEC, ETH_P_IPV6); int sock = socket(AF_PACKET, SOCK_DGRAM|SOCK_CLOEXEC, htons(ETH_P_IPV6));
if (sock < 0) if (sock < 0)
exit_errno("can't open packet socket"); exit_errno("can't open packet socket");
setsockopt(sock, SOL_SOCKET, SO_ATTACH_FILTER, &radv_filter, sizeof(radv_filter)); int ret = setsockopt(sock, SOL_SOCKET, SO_ATTACH_FILTER, &radv_filter, sizeof(radv_filter));
if (ret < 0)
exit_errno("can't attach socket filter");
struct sockaddr_ll bind_iface = { struct sockaddr_ll bind_iface = {
.sll_family = AF_PACKET, .sll_family = AF_PACKET,
.sll_protocol = ETH_P_IPV6, .sll_protocol = htons(ETH_P_IPV6),
.sll_ifindex = ifindex, .sll_ifindex = ifindex,
}; };
bind(sock, (struct sockaddr *)&bind_iface, sizeof(bind_iface)); bind(sock, (struct sockaddr *)&bind_iface, sizeof(bind_iface));