xdp_adjust_tail_kern.c 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  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_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. struct bpf_map_def SEC("maps") icmpcnt = {
  27. .type = BPF_MAP_TYPE_ARRAY,
  28. .key_size = sizeof(__u32),
  29. .value_size = sizeof(__u64),
  30. .max_entries = 1,
  31. };
  32. static __always_inline void count_icmp(void)
  33. {
  34. u64 key = 0;
  35. u64 *icmp_count;
  36. icmp_count = bpf_map_lookup_elem(&icmpcnt, &key);
  37. if (icmp_count)
  38. *icmp_count += 1;
  39. }
  40. static __always_inline void swap_mac(void *data, struct ethhdr *orig_eth)
  41. {
  42. struct ethhdr *eth;
  43. eth = data;
  44. memcpy(eth->h_source, orig_eth->h_dest, ETH_ALEN);
  45. memcpy(eth->h_dest, orig_eth->h_source, ETH_ALEN);
  46. eth->h_proto = orig_eth->h_proto;
  47. }
  48. static __always_inline __u16 csum_fold_helper(__u32 csum)
  49. {
  50. return ~((csum & 0xffff) + (csum >> 16));
  51. }
  52. static __always_inline void ipv4_csum(void *data_start, int data_size,
  53. __u32 *csum)
  54. {
  55. *csum = bpf_csum_diff(0, 0, data_start, data_size, *csum);
  56. *csum = csum_fold_helper(*csum);
  57. }
  58. static __always_inline int send_icmp4_too_big(struct xdp_md *xdp)
  59. {
  60. int headroom = (int)sizeof(struct iphdr) + (int)sizeof(struct icmphdr);
  61. if (bpf_xdp_adjust_head(xdp, 0 - headroom))
  62. return XDP_DROP;
  63. void *data = (void *)(long)xdp->data;
  64. void *data_end = (void *)(long)xdp->data_end;
  65. if (data + (ICMP_TOOBIG_SIZE + headroom) > data_end)
  66. return XDP_DROP;
  67. struct iphdr *iph, *orig_iph;
  68. struct icmphdr *icmp_hdr;
  69. struct ethhdr *orig_eth;
  70. __u32 csum = 0;
  71. __u64 off = 0;
  72. orig_eth = data + headroom;
  73. swap_mac(data, orig_eth);
  74. off += sizeof(struct ethhdr);
  75. iph = data + off;
  76. off += sizeof(struct iphdr);
  77. icmp_hdr = data + off;
  78. off += sizeof(struct icmphdr);
  79. orig_iph = data + off;
  80. icmp_hdr->type = ICMP_DEST_UNREACH;
  81. icmp_hdr->code = ICMP_FRAG_NEEDED;
  82. icmp_hdr->un.frag.mtu = htons(MAX_PCKT_SIZE-sizeof(struct ethhdr));
  83. icmp_hdr->checksum = 0;
  84. ipv4_csum(icmp_hdr, ICMP_TOOBIG_PAYLOAD_SIZE, &csum);
  85. icmp_hdr->checksum = csum;
  86. iph->ttl = DEFAULT_TTL;
  87. iph->daddr = orig_iph->saddr;
  88. iph->saddr = orig_iph->daddr;
  89. iph->version = 4;
  90. iph->ihl = 5;
  91. iph->protocol = IPPROTO_ICMP;
  92. iph->tos = 0;
  93. iph->tot_len = htons(
  94. ICMP_TOOBIG_SIZE + headroom - sizeof(struct ethhdr));
  95. iph->check = 0;
  96. csum = 0;
  97. ipv4_csum(iph, sizeof(struct iphdr), &csum);
  98. iph->check = csum;
  99. count_icmp();
  100. return XDP_TX;
  101. }
  102. static __always_inline int handle_ipv4(struct xdp_md *xdp)
  103. {
  104. void *data_end = (void *)(long)xdp->data_end;
  105. void *data = (void *)(long)xdp->data;
  106. int pckt_size = data_end - data;
  107. int offset;
  108. if (pckt_size > MAX_PCKT_SIZE) {
  109. offset = pckt_size - ICMP_TOOBIG_SIZE;
  110. if (bpf_xdp_adjust_tail(xdp, 0 - offset))
  111. return XDP_PASS;
  112. return send_icmp4_too_big(xdp);
  113. }
  114. return XDP_PASS;
  115. }
  116. SEC("xdp_icmp")
  117. int _xdp_icmp(struct xdp_md *xdp)
  118. {
  119. void *data_end = (void *)(long)xdp->data_end;
  120. void *data = (void *)(long)xdp->data;
  121. struct ethhdr *eth = data;
  122. __u16 h_proto;
  123. if (eth + 1 > data_end)
  124. return XDP_DROP;
  125. h_proto = eth->h_proto;
  126. if (h_proto == htons(ETH_P_IP))
  127. return handle_ipv4(xdp);
  128. else
  129. return XDP_PASS;
  130. }
  131. char _license[] SEC("license") = "GPL";