ila_lwt.c 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332
  1. // SPDX-License-Identifier: GPL-2.0
  2. #include <linux/errno.h>
  3. #include <linux/ip.h>
  4. #include <linux/kernel.h>
  5. #include <linux/module.h>
  6. #include <linux/skbuff.h>
  7. #include <linux/socket.h>
  8. #include <linux/types.h>
  9. #include <net/checksum.h>
  10. #include <net/dst_cache.h>
  11. #include <net/ip.h>
  12. #include <net/ip6_fib.h>
  13. #include <net/ip6_route.h>
  14. #include <net/lwtunnel.h>
  15. #include <net/protocol.h>
  16. #include <uapi/linux/ila.h>
  17. #include "ila.h"
  18. struct ila_lwt {
  19. struct ila_params p;
  20. struct dst_cache dst_cache;
  21. u32 connected : 1;
  22. u32 lwt_output : 1;
  23. };
  24. static inline struct ila_lwt *ila_lwt_lwtunnel(
  25. struct lwtunnel_state *lwt)
  26. {
  27. return (struct ila_lwt *)lwt->data;
  28. }
  29. static inline struct ila_params *ila_params_lwtunnel(
  30. struct lwtunnel_state *lwt)
  31. {
  32. return &ila_lwt_lwtunnel(lwt)->p;
  33. }
  34. static int ila_output(struct net *net, struct sock *sk, struct sk_buff *skb)
  35. {
  36. struct dst_entry *orig_dst = skb_dst(skb);
  37. struct rt6_info *rt = dst_rt6_info(orig_dst);
  38. struct ila_lwt *ilwt = ila_lwt_lwtunnel(orig_dst->lwtstate);
  39. struct dst_entry *dst;
  40. int err = -EINVAL;
  41. if (skb->protocol != htons(ETH_P_IPV6))
  42. goto drop;
  43. if (ilwt->lwt_output)
  44. ila_update_ipv6_locator(skb,
  45. ila_params_lwtunnel(orig_dst->lwtstate),
  46. true);
  47. if (rt->rt6i_flags & (RTF_GATEWAY | RTF_CACHE)) {
  48. /* Already have a next hop address in route, no need for
  49. * dest cache route.
  50. */
  51. return orig_dst->lwtstate->orig_output(net, sk, skb);
  52. }
  53. local_bh_disable();
  54. dst = dst_cache_get(&ilwt->dst_cache);
  55. local_bh_enable();
  56. if (unlikely(!dst)) {
  57. struct ipv6hdr *ip6h = ipv6_hdr(skb);
  58. struct flowi6 fl6;
  59. /* Lookup a route for the new destination. Take into
  60. * account that the base route may already have a gateway.
  61. */
  62. memset(&fl6, 0, sizeof(fl6));
  63. fl6.flowi6_oif = dst_dev(orig_dst)->ifindex;
  64. fl6.flowi6_iif = LOOPBACK_IFINDEX;
  65. fl6.daddr = *rt6_nexthop(dst_rt6_info(orig_dst),
  66. &ip6h->daddr);
  67. dst = ip6_route_output(net, NULL, &fl6);
  68. if (dst->error) {
  69. err = -EHOSTUNREACH;
  70. dst_release(dst);
  71. goto drop;
  72. }
  73. dst = xfrm_lookup(net, dst, flowi6_to_flowi(&fl6), NULL, 0);
  74. if (IS_ERR(dst)) {
  75. err = PTR_ERR(dst);
  76. goto drop;
  77. }
  78. /* cache only if we don't create a dst reference loop */
  79. if (ilwt->connected && orig_dst->lwtstate != dst->lwtstate) {
  80. local_bh_disable();
  81. dst_cache_set_ip6(&ilwt->dst_cache, dst, &fl6.saddr);
  82. local_bh_enable();
  83. }
  84. }
  85. skb_dst_drop(skb);
  86. skb_dst_set(skb, dst);
  87. return dst_output(net, sk, skb);
  88. drop:
  89. kfree_skb(skb);
  90. return err;
  91. }
  92. static int ila_input(struct sk_buff *skb)
  93. {
  94. struct dst_entry *dst = skb_dst(skb);
  95. struct ila_lwt *ilwt = ila_lwt_lwtunnel(dst->lwtstate);
  96. if (skb->protocol != htons(ETH_P_IPV6))
  97. goto drop;
  98. if (!ilwt->lwt_output)
  99. ila_update_ipv6_locator(skb,
  100. ila_params_lwtunnel(dst->lwtstate),
  101. false);
  102. return dst->lwtstate->orig_input(skb);
  103. drop:
  104. kfree_skb(skb);
  105. return -EINVAL;
  106. }
  107. static const struct nla_policy ila_nl_policy[ILA_ATTR_MAX + 1] = {
  108. [ILA_ATTR_LOCATOR] = { .type = NLA_U64, },
  109. [ILA_ATTR_CSUM_MODE] = { .type = NLA_U8, },
  110. [ILA_ATTR_IDENT_TYPE] = { .type = NLA_U8, },
  111. [ILA_ATTR_HOOK_TYPE] = { .type = NLA_U8, },
  112. };
  113. static int ila_build_state(struct net *net, struct nlattr *nla,
  114. unsigned int family, const void *cfg,
  115. struct lwtunnel_state **ts,
  116. struct netlink_ext_ack *extack)
  117. {
  118. struct ila_lwt *ilwt;
  119. struct ila_params *p;
  120. struct nlattr *tb[ILA_ATTR_MAX + 1];
  121. struct lwtunnel_state *newts;
  122. const struct fib6_config *cfg6 = cfg;
  123. struct ila_addr *iaddr;
  124. u8 ident_type = ILA_ATYPE_USE_FORMAT;
  125. u8 hook_type = ILA_HOOK_ROUTE_OUTPUT;
  126. u8 csum_mode = ILA_CSUM_NO_ACTION;
  127. bool lwt_output = true;
  128. u8 eff_ident_type;
  129. int ret;
  130. if (family != AF_INET6)
  131. return -EINVAL;
  132. ret = nla_parse_nested_deprecated(tb, ILA_ATTR_MAX, nla,
  133. ila_nl_policy, extack);
  134. if (ret < 0)
  135. return ret;
  136. if (!tb[ILA_ATTR_LOCATOR])
  137. return -EINVAL;
  138. iaddr = (struct ila_addr *)&cfg6->fc_dst;
  139. if (tb[ILA_ATTR_IDENT_TYPE])
  140. ident_type = nla_get_u8(tb[ILA_ATTR_IDENT_TYPE]);
  141. if (ident_type == ILA_ATYPE_USE_FORMAT) {
  142. /* Infer identifier type from type field in formatted
  143. * identifier.
  144. */
  145. if (cfg6->fc_dst_len < 8 * sizeof(struct ila_locator) + 3) {
  146. /* Need to have full locator and at least type field
  147. * included in destination
  148. */
  149. return -EINVAL;
  150. }
  151. eff_ident_type = iaddr->ident.type;
  152. } else {
  153. eff_ident_type = ident_type;
  154. }
  155. switch (eff_ident_type) {
  156. case ILA_ATYPE_IID:
  157. /* Don't allow ILA for IID type */
  158. return -EINVAL;
  159. case ILA_ATYPE_LUID:
  160. break;
  161. case ILA_ATYPE_VIRT_V4:
  162. case ILA_ATYPE_VIRT_UNI_V6:
  163. case ILA_ATYPE_VIRT_MULTI_V6:
  164. case ILA_ATYPE_NONLOCAL_ADDR:
  165. /* These ILA formats are not supported yet. */
  166. default:
  167. return -EINVAL;
  168. }
  169. if (tb[ILA_ATTR_HOOK_TYPE])
  170. hook_type = nla_get_u8(tb[ILA_ATTR_HOOK_TYPE]);
  171. switch (hook_type) {
  172. case ILA_HOOK_ROUTE_OUTPUT:
  173. lwt_output = true;
  174. break;
  175. case ILA_HOOK_ROUTE_INPUT:
  176. lwt_output = false;
  177. break;
  178. default:
  179. return -EINVAL;
  180. }
  181. if (tb[ILA_ATTR_CSUM_MODE])
  182. csum_mode = nla_get_u8(tb[ILA_ATTR_CSUM_MODE]);
  183. if (csum_mode == ILA_CSUM_NEUTRAL_MAP &&
  184. ila_csum_neutral_set(iaddr->ident)) {
  185. /* Don't allow translation if checksum neutral bit is
  186. * configured and it's set in the SIR address.
  187. */
  188. return -EINVAL;
  189. }
  190. newts = lwtunnel_state_alloc(sizeof(*ilwt));
  191. if (!newts)
  192. return -ENOMEM;
  193. ilwt = ila_lwt_lwtunnel(newts);
  194. ret = dst_cache_init(&ilwt->dst_cache, GFP_ATOMIC);
  195. if (ret) {
  196. kfree(newts);
  197. return ret;
  198. }
  199. ilwt->lwt_output = !!lwt_output;
  200. p = ila_params_lwtunnel(newts);
  201. p->csum_mode = csum_mode;
  202. p->ident_type = ident_type;
  203. p->locator.v64 = (__force __be64)nla_get_u64(tb[ILA_ATTR_LOCATOR]);
  204. /* Precompute checksum difference for translation since we
  205. * know both the old locator and the new one.
  206. */
  207. p->locator_match = iaddr->loc;
  208. ila_init_saved_csum(p);
  209. newts->type = LWTUNNEL_ENCAP_ILA;
  210. newts->flags |= LWTUNNEL_STATE_OUTPUT_REDIRECT |
  211. LWTUNNEL_STATE_INPUT_REDIRECT;
  212. if (cfg6->fc_dst_len == 8 * sizeof(struct in6_addr))
  213. ilwt->connected = 1;
  214. *ts = newts;
  215. return 0;
  216. }
  217. static void ila_destroy_state(struct lwtunnel_state *lwt)
  218. {
  219. dst_cache_destroy(&ila_lwt_lwtunnel(lwt)->dst_cache);
  220. }
  221. static int ila_fill_encap_info(struct sk_buff *skb,
  222. struct lwtunnel_state *lwtstate)
  223. {
  224. struct ila_params *p = ila_params_lwtunnel(lwtstate);
  225. struct ila_lwt *ilwt = ila_lwt_lwtunnel(lwtstate);
  226. if (nla_put_u64_64bit(skb, ILA_ATTR_LOCATOR, (__force u64)p->locator.v64,
  227. ILA_ATTR_PAD))
  228. goto nla_put_failure;
  229. if (nla_put_u8(skb, ILA_ATTR_CSUM_MODE, (__force u8)p->csum_mode))
  230. goto nla_put_failure;
  231. if (nla_put_u8(skb, ILA_ATTR_IDENT_TYPE, (__force u8)p->ident_type))
  232. goto nla_put_failure;
  233. if (nla_put_u8(skb, ILA_ATTR_HOOK_TYPE,
  234. ilwt->lwt_output ? ILA_HOOK_ROUTE_OUTPUT :
  235. ILA_HOOK_ROUTE_INPUT))
  236. goto nla_put_failure;
  237. return 0;
  238. nla_put_failure:
  239. return -EMSGSIZE;
  240. }
  241. static int ila_encap_nlsize(struct lwtunnel_state *lwtstate)
  242. {
  243. return nla_total_size_64bit(sizeof(u64)) + /* ILA_ATTR_LOCATOR */
  244. nla_total_size(sizeof(u8)) + /* ILA_ATTR_CSUM_MODE */
  245. nla_total_size(sizeof(u8)) + /* ILA_ATTR_IDENT_TYPE */
  246. nla_total_size(sizeof(u8)) + /* ILA_ATTR_HOOK_TYPE */
  247. 0;
  248. }
  249. static int ila_encap_cmp(struct lwtunnel_state *a, struct lwtunnel_state *b)
  250. {
  251. struct ila_params *a_p = ila_params_lwtunnel(a);
  252. struct ila_params *b_p = ila_params_lwtunnel(b);
  253. return (a_p->locator.v64 != b_p->locator.v64);
  254. }
  255. static const struct lwtunnel_encap_ops ila_encap_ops = {
  256. .build_state = ila_build_state,
  257. .destroy_state = ila_destroy_state,
  258. .output = ila_output,
  259. .input = ila_input,
  260. .fill_encap = ila_fill_encap_info,
  261. .get_encap_size = ila_encap_nlsize,
  262. .cmp_encap = ila_encap_cmp,
  263. .owner = THIS_MODULE,
  264. };
  265. int ila_lwt_init(void)
  266. {
  267. return lwtunnel_encap_add_ops(&ila_encap_ops, LWTUNNEL_ENCAP_ILA);
  268. }
  269. void ila_lwt_fini(void)
  270. {
  271. lwtunnel_encap_del_ops(&ila_encap_ops, LWTUNNEL_ENCAP_ILA);
  272. }