Skip to content

Instantly share code, notes, and snippets.

@yxue
Created March 22, 2020 23:46
Show Gist options
  • Save yxue/9e37a6c6ff92c34710739bc17328131b to your computer and use it in GitHub Desktop.
Save yxue/9e37a6c6ff92c34710739bc17328131b to your computer and use it in GitHub Desktop.
#include <linux/bpf.h>
#include <netinet/ip.h>
#include <netinet/tcp.h>
#include <stdint.h>
#include "bpf_endian.h"
#include "bpf_helpers.h"
char _license[] SEC("license") = "GPL";
struct bpf_map_def SEC("maps") sock_map = {
.type = BPF_MAP_TYPE_SOCKMAP,
.key_size = sizeof(int),
.value_size = sizeof(int),
.max_entries = 2,
};
SEC("sk_skb_parser")
int _prog_parser(struct __sk_buff *skb)
{
char debug_msg[] = "[_,_] prog_parser called. SKB length: %u\n";
bpf_trace_printk(debug_msg, sizeof(debug_msg), skb->len);
return skb->len;
}
SEC("sk_skb_verdict")
int _prog_verdict(struct __sk_buff *skb)
{
char debug_msg[] = "[_,_] prog_parser verdict\n";
uint32_t key;
if (skb->local_port == 8000)
key = 1;
else
key = 0;
char msg[] = "local port %u\n";
bpf_trace_printk(msg, sizeof(msg), skb->local_port);
bpf_trace_printk(debug_msg, sizeof(debug_msg));
return bpf_sk_redirect_map(skb, &sock_map, key, 0);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment