xdp1_kern.c 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. /* Copyright (c) 2016 PLUMgrid
  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. */
  7. #define KBUILD_MODNAME "foo"
  8. #include <uapi/linux/bpf.h>
  9. #include <linux/in.h>
  10. #include <linux/if_ether.h>
  11. #include <linux/if_packet.h>
  12. #include <linux/if_vlan.h>
  13. #include <linux/ip.h>
  14. #include <linux/ipv6.h>
  15. #include <bpf/bpf_helpers.h>
  16. struct {
  17. __uint(type, BPF_MAP_TYPE_PERCPU_ARRAY);
  18. __type(key, u32);
  19. __type(value, long);
  20. __uint(max_entries, 256);
  21. } rxcnt SEC(".maps");
  22. static int parse_ipv4(void *data, u64 nh_off, void *data_end)
  23. {
  24. struct iphdr *iph = data + nh_off;
  25. if (iph + 1 > data_end)
  26. return 0;
  27. return iph->protocol;
  28. }
  29. static int parse_ipv6(void *data, u64 nh_off, void *data_end)
  30. {
  31. struct ipv6hdr *ip6h = data + nh_off;
  32. if (ip6h + 1 > data_end)
  33. return 0;
  34. return ip6h->nexthdr;
  35. }
  36. #define XDPBUFSIZE 60
  37. SEC("xdp.frags")
  38. int xdp_prog1(struct xdp_md *ctx)
  39. {
  40. __u8 pkt[XDPBUFSIZE] = {};
  41. void *data_end = &pkt[XDPBUFSIZE-1];
  42. void *data = pkt;
  43. struct ethhdr *eth = data;
  44. int rc = XDP_DROP;
  45. long *value;
  46. u16 h_proto;
  47. u64 nh_off;
  48. u32 ipproto;
  49. if (bpf_xdp_load_bytes(ctx, 0, pkt, sizeof(pkt)))
  50. return rc;
  51. nh_off = sizeof(*eth);
  52. if (data + nh_off > data_end)
  53. return rc;
  54. h_proto = eth->h_proto;
  55. /* Handle VLAN tagged packet */
  56. if (h_proto == htons(ETH_P_8021Q) || h_proto == htons(ETH_P_8021AD)) {
  57. struct vlan_hdr *vhdr;
  58. vhdr = data + nh_off;
  59. nh_off += sizeof(struct vlan_hdr);
  60. if (data + nh_off > data_end)
  61. return rc;
  62. h_proto = vhdr->h_vlan_encapsulated_proto;
  63. }
  64. /* Handle double VLAN tagged packet */
  65. if (h_proto == htons(ETH_P_8021Q) || h_proto == htons(ETH_P_8021AD)) {
  66. struct vlan_hdr *vhdr;
  67. vhdr = data + nh_off;
  68. nh_off += sizeof(struct vlan_hdr);
  69. if (data + nh_off > data_end)
  70. return rc;
  71. h_proto = vhdr->h_vlan_encapsulated_proto;
  72. }
  73. if (h_proto == htons(ETH_P_IP))
  74. ipproto = parse_ipv4(data, nh_off, data_end);
  75. else if (h_proto == htons(ETH_P_IPV6))
  76. ipproto = parse_ipv6(data, nh_off, data_end);
  77. else
  78. ipproto = 0;
  79. value = bpf_map_lookup_elem(&rxcnt, &ipproto);
  80. if (value)
  81. *value += 1;
  82. return rc;
  83. }
  84. char _license[] SEC("license") = "GPL";