xfrm_nat_keepalive.c 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * xfrm_nat_keepalive.c
  4. *
  5. * (c) 2024 Eyal Birger <eyal.birger@gmail.com>
  6. */
  7. #include <net/inet_common.h>
  8. #include <net/ip6_checksum.h>
  9. #include <net/xfrm.h>
  10. static DEFINE_PER_CPU(struct sock *, nat_keepalive_sk_ipv4);
  11. #if IS_ENABLED(CONFIG_IPV6)
  12. static DEFINE_PER_CPU(struct sock *, nat_keepalive_sk_ipv6);
  13. #endif
  14. struct nat_keepalive {
  15. struct net *net;
  16. u16 family;
  17. xfrm_address_t saddr;
  18. xfrm_address_t daddr;
  19. __be16 encap_sport;
  20. __be16 encap_dport;
  21. __u32 smark;
  22. };
  23. static void nat_keepalive_init(struct nat_keepalive *ka, struct xfrm_state *x)
  24. {
  25. ka->net = xs_net(x);
  26. ka->family = x->props.family;
  27. ka->saddr = x->props.saddr;
  28. ka->daddr = x->id.daddr;
  29. ka->encap_sport = x->encap->encap_sport;
  30. ka->encap_dport = x->encap->encap_dport;
  31. ka->smark = xfrm_smark_get(0, x);
  32. }
  33. static int nat_keepalive_send_ipv4(struct sk_buff *skb,
  34. struct nat_keepalive *ka)
  35. {
  36. struct net *net = ka->net;
  37. struct flowi4 fl4;
  38. struct rtable *rt;
  39. struct sock *sk;
  40. __u8 tos = 0;
  41. int err;
  42. flowi4_init_output(&fl4, 0 /* oif */, skb->mark, tos,
  43. RT_SCOPE_UNIVERSE, IPPROTO_UDP, 0,
  44. ka->daddr.a4, ka->saddr.a4, ka->encap_dport,
  45. ka->encap_sport, sock_net_uid(net, NULL));
  46. rt = ip_route_output_key(net, &fl4);
  47. if (IS_ERR(rt))
  48. return PTR_ERR(rt);
  49. skb_dst_set(skb, &rt->dst);
  50. sk = *this_cpu_ptr(&nat_keepalive_sk_ipv4);
  51. sock_net_set(sk, net);
  52. err = ip_build_and_send_pkt(skb, sk, fl4.saddr, fl4.daddr, NULL, tos);
  53. sock_net_set(sk, &init_net);
  54. return err;
  55. }
  56. #if IS_ENABLED(CONFIG_IPV6)
  57. static int nat_keepalive_send_ipv6(struct sk_buff *skb,
  58. struct nat_keepalive *ka,
  59. struct udphdr *uh)
  60. {
  61. struct net *net = ka->net;
  62. struct dst_entry *dst;
  63. struct flowi6 fl6;
  64. struct sock *sk;
  65. __wsum csum;
  66. int err;
  67. csum = skb_checksum(skb, 0, skb->len, 0);
  68. uh->check = csum_ipv6_magic(&ka->saddr.in6, &ka->daddr.in6,
  69. skb->len, IPPROTO_UDP, csum);
  70. if (uh->check == 0)
  71. uh->check = CSUM_MANGLED_0;
  72. memset(&fl6, 0, sizeof(fl6));
  73. fl6.flowi6_mark = skb->mark;
  74. fl6.saddr = ka->saddr.in6;
  75. fl6.daddr = ka->daddr.in6;
  76. fl6.flowi6_proto = IPPROTO_UDP;
  77. fl6.fl6_sport = ka->encap_sport;
  78. fl6.fl6_dport = ka->encap_dport;
  79. sk = *this_cpu_ptr(&nat_keepalive_sk_ipv6);
  80. sock_net_set(sk, net);
  81. dst = ipv6_stub->ipv6_dst_lookup_flow(net, sk, &fl6, NULL);
  82. if (IS_ERR(dst))
  83. return PTR_ERR(dst);
  84. skb_dst_set(skb, dst);
  85. err = ipv6_stub->ip6_xmit(sk, skb, &fl6, skb->mark, NULL, 0, 0);
  86. sock_net_set(sk, &init_net);
  87. return err;
  88. }
  89. #endif
  90. static void nat_keepalive_send(struct nat_keepalive *ka)
  91. {
  92. const int nat_ka_hdrs_len = max(sizeof(struct iphdr),
  93. sizeof(struct ipv6hdr)) +
  94. sizeof(struct udphdr);
  95. const u8 nat_ka_payload = 0xFF;
  96. int err = -EAFNOSUPPORT;
  97. struct sk_buff *skb;
  98. struct udphdr *uh;
  99. skb = alloc_skb(nat_ka_hdrs_len + sizeof(nat_ka_payload), GFP_ATOMIC);
  100. if (unlikely(!skb))
  101. return;
  102. skb_reserve(skb, nat_ka_hdrs_len);
  103. skb_put_u8(skb, nat_ka_payload);
  104. uh = skb_push(skb, sizeof(*uh));
  105. uh->source = ka->encap_sport;
  106. uh->dest = ka->encap_dport;
  107. uh->len = htons(skb->len);
  108. uh->check = 0;
  109. skb->mark = ka->smark;
  110. switch (ka->family) {
  111. case AF_INET:
  112. err = nat_keepalive_send_ipv4(skb, ka);
  113. break;
  114. #if IS_ENABLED(CONFIG_IPV6)
  115. case AF_INET6:
  116. err = nat_keepalive_send_ipv6(skb, ka, uh);
  117. break;
  118. #endif
  119. }
  120. if (err)
  121. kfree_skb(skb);
  122. }
  123. struct nat_keepalive_work_ctx {
  124. time64_t next_run;
  125. time64_t now;
  126. };
  127. static int nat_keepalive_work_single(struct xfrm_state *x, int count, void *ptr)
  128. {
  129. struct nat_keepalive_work_ctx *ctx = ptr;
  130. bool send_keepalive = false;
  131. struct nat_keepalive ka;
  132. time64_t next_run;
  133. u32 interval;
  134. int delta;
  135. interval = x->nat_keepalive_interval;
  136. if (!interval)
  137. return 0;
  138. spin_lock(&x->lock);
  139. delta = (int)(ctx->now - x->lastused);
  140. if (delta < interval) {
  141. x->nat_keepalive_expiration = ctx->now + interval - delta;
  142. next_run = x->nat_keepalive_expiration;
  143. } else if (x->nat_keepalive_expiration > ctx->now) {
  144. next_run = x->nat_keepalive_expiration;
  145. } else {
  146. next_run = ctx->now + interval;
  147. nat_keepalive_init(&ka, x);
  148. send_keepalive = true;
  149. }
  150. spin_unlock(&x->lock);
  151. if (send_keepalive)
  152. nat_keepalive_send(&ka);
  153. if (!ctx->next_run || next_run < ctx->next_run)
  154. ctx->next_run = next_run;
  155. return 0;
  156. }
  157. static void nat_keepalive_work(struct work_struct *work)
  158. {
  159. struct nat_keepalive_work_ctx ctx;
  160. struct xfrm_state_walk walk;
  161. struct net *net;
  162. ctx.next_run = 0;
  163. ctx.now = ktime_get_real_seconds();
  164. net = container_of(work, struct net, xfrm.nat_keepalive_work.work);
  165. xfrm_state_walk_init(&walk, IPPROTO_ESP, NULL);
  166. xfrm_state_walk(net, &walk, nat_keepalive_work_single, &ctx);
  167. xfrm_state_walk_done(&walk, net);
  168. if (ctx.next_run)
  169. schedule_delayed_work(&net->xfrm.nat_keepalive_work,
  170. (ctx.next_run - ctx.now) * HZ);
  171. }
  172. static int nat_keepalive_sk_init(struct sock * __percpu *socks,
  173. unsigned short family)
  174. {
  175. struct sock *sk;
  176. int err, i;
  177. for_each_possible_cpu(i) {
  178. err = inet_ctl_sock_create(&sk, family, SOCK_RAW, IPPROTO_UDP,
  179. &init_net);
  180. if (err < 0)
  181. goto err;
  182. *per_cpu_ptr(socks, i) = sk;
  183. }
  184. return 0;
  185. err:
  186. for_each_possible_cpu(i)
  187. inet_ctl_sock_destroy(*per_cpu_ptr(socks, i));
  188. return err;
  189. }
  190. static void nat_keepalive_sk_fini(struct sock * __percpu *socks)
  191. {
  192. int i;
  193. for_each_possible_cpu(i)
  194. inet_ctl_sock_destroy(*per_cpu_ptr(socks, i));
  195. }
  196. void xfrm_nat_keepalive_state_updated(struct xfrm_state *x)
  197. {
  198. struct net *net;
  199. if (!x->nat_keepalive_interval)
  200. return;
  201. net = xs_net(x);
  202. schedule_delayed_work(&net->xfrm.nat_keepalive_work, 0);
  203. }
  204. int __net_init xfrm_nat_keepalive_net_init(struct net *net)
  205. {
  206. INIT_DELAYED_WORK(&net->xfrm.nat_keepalive_work, nat_keepalive_work);
  207. return 0;
  208. }
  209. int xfrm_nat_keepalive_net_fini(struct net *net)
  210. {
  211. cancel_delayed_work_sync(&net->xfrm.nat_keepalive_work);
  212. return 0;
  213. }
  214. int xfrm_nat_keepalive_init(unsigned short family)
  215. {
  216. int err = -EAFNOSUPPORT;
  217. switch (family) {
  218. case AF_INET:
  219. err = nat_keepalive_sk_init(&nat_keepalive_sk_ipv4, PF_INET);
  220. break;
  221. #if IS_ENABLED(CONFIG_IPV6)
  222. case AF_INET6:
  223. err = nat_keepalive_sk_init(&nat_keepalive_sk_ipv6, PF_INET6);
  224. break;
  225. #endif
  226. }
  227. if (err)
  228. pr_err("xfrm nat keepalive init: failed to init err:%d\n", err);
  229. return err;
  230. }
  231. EXPORT_SYMBOL_GPL(xfrm_nat_keepalive_init);
  232. void xfrm_nat_keepalive_fini(unsigned short family)
  233. {
  234. switch (family) {
  235. case AF_INET:
  236. nat_keepalive_sk_fini(&nat_keepalive_sk_ipv4);
  237. break;
  238. #if IS_ENABLED(CONFIG_IPV6)
  239. case AF_INET6:
  240. nat_keepalive_sk_fini(&nat_keepalive_sk_ipv6);
  241. break;
  242. #endif
  243. }
  244. }
  245. EXPORT_SYMBOL_GPL(xfrm_nat_keepalive_fini);