sockex1_kern.c 661 B

123456789101112131415161718192021222324252627282930
  1. #include <uapi/linux/bpf.h>
  2. #include <uapi/linux/if_ether.h>
  3. #include <uapi/linux/if_packet.h>
  4. #include <uapi/linux/ip.h>
  5. #include <bpf/bpf_helpers.h>
  6. #include "bpf_legacy.h"
  7. struct {
  8. __uint(type, BPF_MAP_TYPE_ARRAY);
  9. __type(key, u32);
  10. __type(value, long);
  11. __uint(max_entries, 256);
  12. } my_map SEC(".maps");
  13. SEC("socket1")
  14. int bpf_prog1(struct __sk_buff *skb)
  15. {
  16. int index = load_byte(skb, ETH_HLEN + offsetof(struct iphdr, protocol));
  17. long *value;
  18. if (skb->pkt_type != PACKET_OUTGOING)
  19. return 0;
  20. value = bpf_map_lookup_elem(&my_map, &index);
  21. if (value)
  22. __sync_fetch_and_add(value, skb->len);
  23. return 0;
  24. }
  25. char _license[] SEC("license") = "GPL";