Merge branch 'work.misc' of git://git.kernel.org/pub/scm/linux/kernel/git/viro/vfs
[linux/fpc-iii.git] / samples / bpf / test_cgrp2_tc_kern.c
blob4dd532a312b9dd6a257cefbebf2e988b10ceb2ce
1 /* Copyright (c) 2016 Facebook
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 */
7 #define KBUILD_MODNAME "foo"
8 #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/bpf_helpers.h>
15 /* copy of 'struct ethhdr' without __packed */
16 struct eth_hdr {
17 unsigned char h_dest[ETH_ALEN];
18 unsigned char h_source[ETH_ALEN];
19 unsigned short h_proto;
22 #define PIN_GLOBAL_NS 2
23 struct 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;
33 struct 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,
41 SEC("filter")
42 int handle_egress(struct __sk_buff *skb)
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";
52 /* single length check */
53 if (data + sizeof(*eth) + sizeof(*ip6h) > data_end)
54 return TC_ACT_OK;
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;
61 } else if (bpf_skb_under_cgroup(skb, &test_cgrp2_array_pin, 0) != 1) {
62 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;
70 char _license[] SEC("license") = "GPL";