blob: 1547b36a7b7b9bd5251dd1be6ae2a451cdca0a29 [file] [log] [blame]
Martin KaFai Laua3f74612016-06-30 10:28:45 -07001/* Copyright (c) 2016 Facebook
2 *
3 * This program is free software; you can redistribute it and/or
4 * modify it under the terms of version 2 of the GNU General Public
5 * License as published by the Free Software Foundation.
6 */
Daniel Borkmann96a8eb12016-10-26 00:37:53 +02007#define KBUILD_MODNAME "foo"
Martin KaFai Laua3f74612016-06-30 10:28:45 -07008#include <uapi/linux/if_ether.h>
9#include <uapi/linux/in6.h>
10#include <uapi/linux/ipv6.h>
11#include <uapi/linux/pkt_cls.h>
12#include <uapi/linux/bpf.h>
13#include "bpf_helpers.h"
14
15/* copy of 'struct ethhdr' without __packed */
16struct eth_hdr {
17 unsigned char h_dest[ETH_ALEN];
18 unsigned char h_source[ETH_ALEN];
19 unsigned short h_proto;
20};
21
22#define PIN_GLOBAL_NS 2
23struct bpf_elf_map {
24 __u32 type;
25 __u32 size_key;
26 __u32 size_value;
27 __u32 max_elem;
28 __u32 flags;
29 __u32 id;
30 __u32 pinning;
31};
32
33struct bpf_elf_map SEC("maps") test_cgrp2_array_pin = {
34 .type = BPF_MAP_TYPE_CGROUP_ARRAY,
35 .size_key = sizeof(uint32_t),
36 .size_value = sizeof(uint32_t),
37 .pinning = PIN_GLOBAL_NS,
38 .max_elem = 1,
39};
40
41SEC("filter")
42int handle_egress(struct __sk_buff *skb)
43{
44 void *data = (void *)(long)skb->data;
45 struct eth_hdr *eth = data;
46 struct ipv6hdr *ip6h = data + sizeof(*eth);
47 void *data_end = (void *)(long)skb->data_end;
48 char dont_care_msg[] = "dont care %04x %d\n";
49 char pass_msg[] = "pass\n";
50 char reject_msg[] = "reject\n";
51
52 /* single length check */
53 if (data + sizeof(*eth) + sizeof(*ip6h) > data_end)
54 return TC_ACT_OK;
55
56 if (eth->h_proto != htons(ETH_P_IPV6) ||
57 ip6h->nexthdr != IPPROTO_ICMPV6) {
58 bpf_trace_printk(dont_care_msg, sizeof(dont_care_msg),
59 eth->h_proto, ip6h->nexthdr);
60 return TC_ACT_OK;
Daniel Borkmann747ea552016-08-12 22:17:17 +020061 } else if (bpf_skb_under_cgroup(skb, &test_cgrp2_array_pin, 0) != 1) {
Martin KaFai Laua3f74612016-06-30 10:28:45 -070062 bpf_trace_printk(pass_msg, sizeof(pass_msg));
63 return TC_ACT_OK;
64 } else {
65 bpf_trace_printk(reject_msg, sizeof(reject_msg));
66 return TC_ACT_SHOT;
67 }
68}
69
70char _license[] SEC("license") = "GPL";