parse_varlen.c 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. /* Copyright (c) 2016 Facebook
  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 <linux/if_ether.h>
  9. #include <linux/ip.h>
  10. #include <linux/ipv6.h>
  11. #include <linux/in.h>
  12. #include <linux/tcp.h>
  13. #include <linux/udp.h>
  14. #include <uapi/linux/bpf.h>
  15. #include <net/ip.h>
  16. #include "bpf_helpers.h"
  17. #define DEFAULT_PKTGEN_UDP_PORT 9
  18. #define DEBUG 0
  19. static int tcp(void *data, uint64_t tp_off, void *data_end)
  20. {
  21. struct tcphdr *tcp = data + tp_off;
  22. if (tcp + 1 > data_end)
  23. return 0;
  24. if (tcp->dest == htons(80) || tcp->source == htons(80))
  25. return TC_ACT_SHOT;
  26. return 0;
  27. }
  28. static int udp(void *data, uint64_t tp_off, void *data_end)
  29. {
  30. struct udphdr *udp = data + tp_off;
  31. if (udp + 1 > data_end)
  32. return 0;
  33. if (udp->dest == htons(DEFAULT_PKTGEN_UDP_PORT) ||
  34. udp->source == htons(DEFAULT_PKTGEN_UDP_PORT)) {
  35. if (DEBUG) {
  36. char fmt[] = "udp port 9 indeed\n";
  37. bpf_trace_printk(fmt, sizeof(fmt));
  38. }
  39. return TC_ACT_SHOT;
  40. }
  41. return 0;
  42. }
  43. static int parse_ipv4(void *data, uint64_t nh_off, void *data_end)
  44. {
  45. struct iphdr *iph;
  46. uint64_t ihl_len;
  47. iph = data + nh_off;
  48. if (iph + 1 > data_end)
  49. return 0;
  50. if (ip_is_fragment(iph))
  51. return 0;
  52. ihl_len = iph->ihl * 4;
  53. if (iph->protocol == IPPROTO_IPIP) {
  54. iph = data + nh_off + ihl_len;
  55. if (iph + 1 > data_end)
  56. return 0;
  57. ihl_len += iph->ihl * 4;
  58. }
  59. if (iph->protocol == IPPROTO_TCP)
  60. return tcp(data, nh_off + ihl_len, data_end);
  61. else if (iph->protocol == IPPROTO_UDP)
  62. return udp(data, nh_off + ihl_len, data_end);
  63. return 0;
  64. }
  65. static int parse_ipv6(void *data, uint64_t nh_off, void *data_end)
  66. {
  67. struct ipv6hdr *ip6h;
  68. struct iphdr *iph;
  69. uint64_t ihl_len = sizeof(struct ipv6hdr);
  70. uint64_t nexthdr;
  71. ip6h = data + nh_off;
  72. if (ip6h + 1 > data_end)
  73. return 0;
  74. nexthdr = ip6h->nexthdr;
  75. if (nexthdr == IPPROTO_IPIP) {
  76. iph = data + nh_off + ihl_len;
  77. if (iph + 1 > data_end)
  78. return 0;
  79. ihl_len += iph->ihl * 4;
  80. nexthdr = iph->protocol;
  81. } else if (nexthdr == IPPROTO_IPV6) {
  82. ip6h = data + nh_off + ihl_len;
  83. if (ip6h + 1 > data_end)
  84. return 0;
  85. ihl_len += sizeof(struct ipv6hdr);
  86. nexthdr = ip6h->nexthdr;
  87. }
  88. if (nexthdr == IPPROTO_TCP)
  89. return tcp(data, nh_off + ihl_len, data_end);
  90. else if (nexthdr == IPPROTO_UDP)
  91. return udp(data, nh_off + ihl_len, data_end);
  92. return 0;
  93. }
  94. struct vlan_hdr {
  95. uint16_t h_vlan_TCI;
  96. uint16_t h_vlan_encapsulated_proto;
  97. };
  98. SEC("varlen")
  99. int handle_ingress(struct __sk_buff *skb)
  100. {
  101. void *data = (void *)(long)skb->data;
  102. struct ethhdr *eth = data;
  103. void *data_end = (void *)(long)skb->data_end;
  104. uint64_t h_proto, nh_off;
  105. nh_off = sizeof(*eth);
  106. if (data + nh_off > data_end)
  107. return 0;
  108. h_proto = eth->h_proto;
  109. if (h_proto == ETH_P_8021Q || h_proto == ETH_P_8021AD) {
  110. struct vlan_hdr *vhdr;
  111. vhdr = data + nh_off;
  112. nh_off += sizeof(struct vlan_hdr);
  113. if (data + nh_off > data_end)
  114. return 0;
  115. h_proto = vhdr->h_vlan_encapsulated_proto;
  116. }
  117. if (h_proto == ETH_P_8021Q || h_proto == ETH_P_8021AD) {
  118. struct vlan_hdr *vhdr;
  119. vhdr = data + nh_off;
  120. nh_off += sizeof(struct vlan_hdr);
  121. if (data + nh_off > data_end)
  122. return 0;
  123. h_proto = vhdr->h_vlan_encapsulated_proto;
  124. }
  125. if (h_proto == htons(ETH_P_IP))
  126. return parse_ipv4(data, nh_off, data_end);
  127. else if (h_proto == htons(ETH_P_IPV6))
  128. return parse_ipv6(data, nh_off, data_end);
  129. return 0;
  130. }
  131. char _license[] SEC("license") = "GPL";