xdp_adjust_tail_kern.c 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. /* SPDX-License-Identifier: GPL-2.0
  2. * Copyright (c) 2018 Facebook
  3. *
  4. * This program is free software; you can redistribute it and/or
  5. * modify it under the terms of version 2 of the GNU General Public
  6. * License as published by the Free Software Foundation.
  7. *
  8. * This program shows how to use bpf_xdp_adjust_tail() by
  9. * generating ICMPv4 "packet to big" (unreachable/ df bit set frag needed
  10. * to be more preice in case of v4)" where receiving packets bigger then
  11. * 600 bytes.
  12. */
  13. #define KBUILD_MODNAME "foo"
  14. #include <uapi/linux/bpf.h>
  15. #include <linux/in.h>
  16. #include <linux/if_ether.h>
  17. #include <linux/if_packet.h>
  18. #include <linux/if_vlan.h>
  19. #include <linux/ip.h>
  20. #include <linux/icmp.h>
  21. #include <bpf/bpf_helpers.h>
  22. #define DEFAULT_TTL 64
  23. #define MAX_PCKT_SIZE 600
  24. #define ICMP_TOOBIG_SIZE 98
  25. #define ICMP_TOOBIG_PAYLOAD_SIZE 92
  26. /* volatile to prevent compiler optimizations */
  27. static volatile __u32 max_pcktsz = MAX_PCKT_SIZE;
  28. struct {
  29. __uint(type, BPF_MAP_TYPE_ARRAY);
  30. __type(key, __u32);
  31. __type(value, __u64);
  32. __uint(max_entries, 1);
  33. } icmpcnt SEC(".maps");
  34. static __always_inline void count_icmp(void)
  35. {
  36. u64 key = 0;
  37. u64 *icmp_count;
  38. icmp_count = bpf_map_lookup_elem(&icmpcnt, &key);
  39. if (icmp_count)
  40. *icmp_count += 1;
  41. }
  42. static __always_inline void swap_mac(void *data, struct ethhdr *orig_eth)
  43. {
  44. struct ethhdr *eth;
  45. eth = data;
  46. memcpy(eth->h_source, orig_eth->h_dest, ETH_ALEN);
  47. memcpy(eth->h_dest, orig_eth->h_source, ETH_ALEN);
  48. eth->h_proto = orig_eth->h_proto;
  49. }
  50. static __always_inline __u16 csum_fold_helper(__u32 csum)
  51. {
  52. csum = (csum & 0xffff) + (csum >> 16);
  53. return ~((csum & 0xffff) + (csum >> 16));
  54. }
  55. static __always_inline void ipv4_csum(void *data_start, int data_size,
  56. __u32 *csum)
  57. {
  58. *csum = bpf_csum_diff(0, 0, data_start, data_size, *csum);
  59. *csum = csum_fold_helper(*csum);
  60. }
  61. static __always_inline int send_icmp4_too_big(struct xdp_md *xdp)
  62. {
  63. int headroom = (int)sizeof(struct iphdr) + (int)sizeof(struct icmphdr);
  64. if (bpf_xdp_adjust_head(xdp, 0 - headroom))
  65. return XDP_DROP;
  66. void *data = (void *)(long)xdp->data;
  67. void *data_end = (void *)(long)xdp->data_end;
  68. if (data + (ICMP_TOOBIG_SIZE + headroom) > data_end)
  69. return XDP_DROP;
  70. struct iphdr *iph, *orig_iph;
  71. struct icmphdr *icmp_hdr;
  72. struct ethhdr *orig_eth;
  73. __u32 csum = 0;
  74. __u64 off = 0;
  75. orig_eth = data + headroom;
  76. swap_mac(data, orig_eth);
  77. off += sizeof(struct ethhdr);
  78. iph = data + off;
  79. off += sizeof(struct iphdr);
  80. icmp_hdr = data + off;
  81. off += sizeof(struct icmphdr);
  82. orig_iph = data + off;
  83. icmp_hdr->type = ICMP_DEST_UNREACH;
  84. icmp_hdr->code = ICMP_FRAG_NEEDED;
  85. icmp_hdr->un.frag.mtu = htons(max_pcktsz - sizeof(struct ethhdr));
  86. icmp_hdr->checksum = 0;
  87. ipv4_csum(icmp_hdr, ICMP_TOOBIG_PAYLOAD_SIZE, &csum);
  88. icmp_hdr->checksum = csum;
  89. iph->ttl = DEFAULT_TTL;
  90. iph->daddr = orig_iph->saddr;
  91. iph->saddr = orig_iph->daddr;
  92. iph->version = 4;
  93. iph->ihl = 5;
  94. iph->protocol = IPPROTO_ICMP;
  95. iph->tos = 0;
  96. iph->tot_len = htons(
  97. ICMP_TOOBIG_SIZE + headroom - sizeof(struct ethhdr));
  98. iph->check = 0;
  99. csum = 0;
  100. ipv4_csum(iph, sizeof(struct iphdr), &csum);
  101. iph->check = csum;
  102. count_icmp();
  103. return XDP_TX;
  104. }
  105. static __always_inline int handle_ipv4(struct xdp_md *xdp)
  106. {
  107. void *data_end = (void *)(long)xdp->data_end;
  108. void *data = (void *)(long)xdp->data;
  109. int pckt_size = data_end - data;
  110. int offset;
  111. if (pckt_size > max(max_pcktsz, ICMP_TOOBIG_SIZE)) {
  112. offset = pckt_size - ICMP_TOOBIG_SIZE;
  113. if (bpf_xdp_adjust_tail(xdp, 0 - offset))
  114. return XDP_PASS;
  115. return send_icmp4_too_big(xdp);
  116. }
  117. return XDP_PASS;
  118. }
  119. SEC("xdp_icmp")
  120. int _xdp_icmp(struct xdp_md *xdp)
  121. {
  122. void *data_end = (void *)(long)xdp->data_end;
  123. void *data = (void *)(long)xdp->data;
  124. struct ethhdr *eth = data;
  125. __u16 h_proto;
  126. if (eth + 1 > data_end)
  127. return XDP_DROP;
  128. h_proto = eth->h_proto;
  129. if (h_proto == htons(ETH_P_IP))
  130. return handle_ipv4(xdp);
  131. else
  132. return XDP_PASS;
  133. }
  134. char _license[] SEC("license") = "GPL";