xfrm_interface_bpf.c 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /* Unstable XFRM Helpers for TC-BPF hook
  3. *
  4. * These are called from SCHED_CLS BPF programs. Note that it is
  5. * allowed to break compatibility for these functions since the interface they
  6. * are exposed through to BPF programs is explicitly unstable.
  7. */
  8. #include <linux/bpf.h>
  9. #include <linux/btf_ids.h>
  10. #include <net/dst_metadata.h>
  11. #include <net/xfrm.h>
  12. /* bpf_xfrm_info - XFRM metadata information
  13. *
  14. * Members:
  15. * @if_id - XFRM if_id:
  16. * Transmit: if_id to be used in policy and state lookups
  17. * Receive: if_id of the state matched for the incoming packet
  18. * @link - Underlying device ifindex:
  19. * Transmit: used as the underlying device in VRF routing
  20. * Receive: the device on which the packet had been received
  21. */
  22. struct bpf_xfrm_info {
  23. u32 if_id;
  24. int link;
  25. };
  26. __bpf_kfunc_start_defs();
  27. /* bpf_skb_get_xfrm_info - Get XFRM metadata
  28. *
  29. * Parameters:
  30. * @skb_ctx - Pointer to ctx (__sk_buff) in TC program
  31. * Cannot be NULL
  32. * @to - Pointer to memory to which the metadata will be copied
  33. * Cannot be NULL
  34. */
  35. __bpf_kfunc int bpf_skb_get_xfrm_info(struct __sk_buff *skb_ctx, struct bpf_xfrm_info *to)
  36. {
  37. struct sk_buff *skb = (struct sk_buff *)skb_ctx;
  38. struct xfrm_md_info *info;
  39. info = skb_xfrm_md_info(skb);
  40. if (!info)
  41. return -EINVAL;
  42. to->if_id = info->if_id;
  43. to->link = info->link;
  44. return 0;
  45. }
  46. /* bpf_skb_get_xfrm_info - Set XFRM metadata
  47. *
  48. * Parameters:
  49. * @skb_ctx - Pointer to ctx (__sk_buff) in TC program
  50. * Cannot be NULL
  51. * @from - Pointer to memory from which the metadata will be copied
  52. * Cannot be NULL
  53. */
  54. __bpf_kfunc int bpf_skb_set_xfrm_info(struct __sk_buff *skb_ctx, const struct bpf_xfrm_info *from)
  55. {
  56. struct sk_buff *skb = (struct sk_buff *)skb_ctx;
  57. struct metadata_dst *md_dst;
  58. struct xfrm_md_info *info;
  59. if (unlikely(skb_metadata_dst(skb)))
  60. return -EINVAL;
  61. if (!xfrm_bpf_md_dst) {
  62. struct metadata_dst __percpu *tmp;
  63. tmp = metadata_dst_alloc_percpu(0, METADATA_XFRM, GFP_ATOMIC);
  64. if (!tmp)
  65. return -ENOMEM;
  66. if (cmpxchg(&xfrm_bpf_md_dst, NULL, tmp))
  67. metadata_dst_free_percpu(tmp);
  68. }
  69. md_dst = this_cpu_ptr(xfrm_bpf_md_dst);
  70. info = &md_dst->u.xfrm_info;
  71. info->if_id = from->if_id;
  72. info->link = from->link;
  73. skb_dst_force(skb);
  74. info->dst_orig = skb_dst(skb);
  75. dst_hold((struct dst_entry *)md_dst);
  76. skb_dst_set(skb, (struct dst_entry *)md_dst);
  77. return 0;
  78. }
  79. __bpf_kfunc_end_defs();
  80. BTF_KFUNCS_START(xfrm_ifc_kfunc_set)
  81. BTF_ID_FLAGS(func, bpf_skb_get_xfrm_info)
  82. BTF_ID_FLAGS(func, bpf_skb_set_xfrm_info)
  83. BTF_KFUNCS_END(xfrm_ifc_kfunc_set)
  84. static const struct btf_kfunc_id_set xfrm_interface_kfunc_set = {
  85. .owner = THIS_MODULE,
  86. .set = &xfrm_ifc_kfunc_set,
  87. };
  88. int __init register_xfrm_interface_bpf(void)
  89. {
  90. return register_btf_kfunc_id_set(BPF_PROG_TYPE_SCHED_CLS,
  91. &xfrm_interface_kfunc_set);
  92. }