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.
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_helpers.h"
15 /* copy of 'struct ethhdr' without __packed */
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
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
,
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
)
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
);
61 } else if (bpf_skb_under_cgroup(skb
, &test_cgrp2_array_pin
, 0) != 1) {
62 bpf_trace_printk(pass_msg
, sizeof(pass_msg
));
65 bpf_trace_printk(reject_msg
, sizeof(reject_msg
));
70 char _license
[] SEC("license") = "GPL";