18c2ecf20Sopenharmony_ci/* Copyright (c) 2016 Facebook 28c2ecf20Sopenharmony_ci * 38c2ecf20Sopenharmony_ci * This program is free software; you can redistribute it and/or 48c2ecf20Sopenharmony_ci * modify it under the terms of version 2 of the GNU General Public 58c2ecf20Sopenharmony_ci * License as published by the Free Software Foundation. 68c2ecf20Sopenharmony_ci */ 78c2ecf20Sopenharmony_ci#define KBUILD_MODNAME "foo" 88c2ecf20Sopenharmony_ci#include <uapi/linux/if_ether.h> 98c2ecf20Sopenharmony_ci#include <uapi/linux/in6.h> 108c2ecf20Sopenharmony_ci#include <uapi/linux/ipv6.h> 118c2ecf20Sopenharmony_ci#include <uapi/linux/pkt_cls.h> 128c2ecf20Sopenharmony_ci#include <uapi/linux/bpf.h> 138c2ecf20Sopenharmony_ci#include <bpf/bpf_helpers.h> 148c2ecf20Sopenharmony_ci 158c2ecf20Sopenharmony_ci/* copy of 'struct ethhdr' without __packed */ 168c2ecf20Sopenharmony_cistruct eth_hdr { 178c2ecf20Sopenharmony_ci unsigned char h_dest[ETH_ALEN]; 188c2ecf20Sopenharmony_ci unsigned char h_source[ETH_ALEN]; 198c2ecf20Sopenharmony_ci unsigned short h_proto; 208c2ecf20Sopenharmony_ci}; 218c2ecf20Sopenharmony_ci 228c2ecf20Sopenharmony_ci#define PIN_GLOBAL_NS 2 238c2ecf20Sopenharmony_cistruct bpf_elf_map { 248c2ecf20Sopenharmony_ci __u32 type; 258c2ecf20Sopenharmony_ci __u32 size_key; 268c2ecf20Sopenharmony_ci __u32 size_value; 278c2ecf20Sopenharmony_ci __u32 max_elem; 288c2ecf20Sopenharmony_ci __u32 flags; 298c2ecf20Sopenharmony_ci __u32 id; 308c2ecf20Sopenharmony_ci __u32 pinning; 318c2ecf20Sopenharmony_ci}; 328c2ecf20Sopenharmony_ci 338c2ecf20Sopenharmony_cistruct bpf_elf_map SEC("maps") test_cgrp2_array_pin = { 348c2ecf20Sopenharmony_ci .type = BPF_MAP_TYPE_CGROUP_ARRAY, 358c2ecf20Sopenharmony_ci .size_key = sizeof(uint32_t), 368c2ecf20Sopenharmony_ci .size_value = sizeof(uint32_t), 378c2ecf20Sopenharmony_ci .pinning = PIN_GLOBAL_NS, 388c2ecf20Sopenharmony_ci .max_elem = 1, 398c2ecf20Sopenharmony_ci}; 408c2ecf20Sopenharmony_ci 418c2ecf20Sopenharmony_ciSEC("filter") 428c2ecf20Sopenharmony_ciint handle_egress(struct __sk_buff *skb) 438c2ecf20Sopenharmony_ci{ 448c2ecf20Sopenharmony_ci void *data = (void *)(long)skb->data; 458c2ecf20Sopenharmony_ci struct eth_hdr *eth = data; 468c2ecf20Sopenharmony_ci struct ipv6hdr *ip6h = data + sizeof(*eth); 478c2ecf20Sopenharmony_ci void *data_end = (void *)(long)skb->data_end; 488c2ecf20Sopenharmony_ci char dont_care_msg[] = "dont care %04x %d\n"; 498c2ecf20Sopenharmony_ci char pass_msg[] = "pass\n"; 508c2ecf20Sopenharmony_ci char reject_msg[] = "reject\n"; 518c2ecf20Sopenharmony_ci 528c2ecf20Sopenharmony_ci /* single length check */ 538c2ecf20Sopenharmony_ci if (data + sizeof(*eth) + sizeof(*ip6h) > data_end) 548c2ecf20Sopenharmony_ci return TC_ACT_OK; 558c2ecf20Sopenharmony_ci 568c2ecf20Sopenharmony_ci if (eth->h_proto != htons(ETH_P_IPV6) || 578c2ecf20Sopenharmony_ci ip6h->nexthdr != IPPROTO_ICMPV6) { 588c2ecf20Sopenharmony_ci bpf_trace_printk(dont_care_msg, sizeof(dont_care_msg), 598c2ecf20Sopenharmony_ci eth->h_proto, ip6h->nexthdr); 608c2ecf20Sopenharmony_ci return TC_ACT_OK; 618c2ecf20Sopenharmony_ci } else if (bpf_skb_under_cgroup(skb, &test_cgrp2_array_pin, 0) != 1) { 628c2ecf20Sopenharmony_ci bpf_trace_printk(pass_msg, sizeof(pass_msg)); 638c2ecf20Sopenharmony_ci return TC_ACT_OK; 648c2ecf20Sopenharmony_ci } else { 658c2ecf20Sopenharmony_ci bpf_trace_printk(reject_msg, sizeof(reject_msg)); 668c2ecf20Sopenharmony_ci return TC_ACT_SHOT; 678c2ecf20Sopenharmony_ci } 688c2ecf20Sopenharmony_ci} 698c2ecf20Sopenharmony_ci 708c2ecf20Sopenharmony_cichar _license[] SEC("license") = "GPL"; 71