tcbpf1_kern.c 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. #define KBUILD_MODNAME "foo"
  2. #include <uapi/linux/bpf.h>
  3. #include <uapi/linux/if_ether.h>
  4. #include <uapi/linux/if_packet.h>
  5. #include <uapi/linux/ip.h>
  6. #include <uapi/linux/in.h>
  7. #include <uapi/linux/tcp.h>
  8. #include <uapi/linux/filter.h>
  9. #include <uapi/linux/pkt_cls.h>
  10. #include "bpf_helpers.h"
  11. /* compiler workaround */
  12. #define _htonl __builtin_bswap32
  13. static inline void set_dst_mac(struct __sk_buff *skb, char *mac)
  14. {
  15. bpf_skb_store_bytes(skb, 0, mac, ETH_ALEN, 1);
  16. }
  17. #define IP_CSUM_OFF (ETH_HLEN + offsetof(struct iphdr, check))
  18. #define TOS_OFF (ETH_HLEN + offsetof(struct iphdr, tos))
  19. static inline void set_ip_tos(struct __sk_buff *skb, __u8 new_tos)
  20. {
  21. __u8 old_tos = load_byte(skb, TOS_OFF);
  22. bpf_l3_csum_replace(skb, IP_CSUM_OFF, htons(old_tos), htons(new_tos), 2);
  23. bpf_skb_store_bytes(skb, TOS_OFF, &new_tos, sizeof(new_tos), 0);
  24. }
  25. #define TCP_CSUM_OFF (ETH_HLEN + sizeof(struct iphdr) + offsetof(struct tcphdr, check))
  26. #define IP_SRC_OFF (ETH_HLEN + offsetof(struct iphdr, saddr))
  27. #define IS_PSEUDO 0x10
  28. static inline void set_tcp_ip_src(struct __sk_buff *skb, __u32 new_ip)
  29. {
  30. __u32 old_ip = _htonl(load_word(skb, IP_SRC_OFF));
  31. bpf_l4_csum_replace(skb, TCP_CSUM_OFF, old_ip, new_ip, IS_PSEUDO | sizeof(new_ip));
  32. bpf_l3_csum_replace(skb, IP_CSUM_OFF, old_ip, new_ip, sizeof(new_ip));
  33. bpf_skb_store_bytes(skb, IP_SRC_OFF, &new_ip, sizeof(new_ip), 0);
  34. }
  35. #define TCP_DPORT_OFF (ETH_HLEN + sizeof(struct iphdr) + offsetof(struct tcphdr, dest))
  36. static inline void set_tcp_dest_port(struct __sk_buff *skb, __u16 new_port)
  37. {
  38. __u16 old_port = htons(load_half(skb, TCP_DPORT_OFF));
  39. bpf_l4_csum_replace(skb, TCP_CSUM_OFF, old_port, new_port, sizeof(new_port));
  40. bpf_skb_store_bytes(skb, TCP_DPORT_OFF, &new_port, sizeof(new_port), 0);
  41. }
  42. SEC("classifier")
  43. int bpf_prog1(struct __sk_buff *skb)
  44. {
  45. __u8 proto = load_byte(skb, ETH_HLEN + offsetof(struct iphdr, protocol));
  46. long *value;
  47. if (proto == IPPROTO_TCP) {
  48. set_ip_tos(skb, 8);
  49. set_tcp_ip_src(skb, 0xA010101);
  50. set_tcp_dest_port(skb, 5001);
  51. }
  52. return 0;
  53. }
  54. SEC("redirect_xmit")
  55. int _redirect_xmit(struct __sk_buff *skb)
  56. {
  57. return bpf_redirect(skb->ifindex + 1, 0);
  58. }
  59. SEC("redirect_recv")
  60. int _redirect_recv(struct __sk_buff *skb)
  61. {
  62. return bpf_redirect(skb->ifindex + 1, 1);
  63. }
  64. SEC("clone_redirect_xmit")
  65. int _clone_redirect_xmit(struct __sk_buff *skb)
  66. {
  67. bpf_clone_redirect(skb, skb->ifindex + 1, 0);
  68. return TC_ACT_SHOT;
  69. }
  70. SEC("clone_redirect_recv")
  71. int _clone_redirect_recv(struct __sk_buff *skb)
  72. {
  73. bpf_clone_redirect(skb, skb->ifindex + 1, 1);
  74. return TC_ACT_SHOT;
  75. }
  76. char _license[] SEC("license") = "GPL";