ila_lwt.c 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330
  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 = orig_dst->dev->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. if (ilwt->connected) {
  79. local_bh_disable();
  80. dst_cache_set_ip6(&ilwt->dst_cache, dst, &fl6.saddr);
  81. local_bh_enable();
  82. }
  83. }
  84. skb_dst_set(skb, dst);
  85. return dst_output(net, sk, skb);
  86. drop:
  87. kfree_skb(skb);
  88. return err;
  89. }
  90. static int ila_input(struct sk_buff *skb)
  91. {
  92. struct dst_entry *dst = skb_dst(skb);
  93. struct ila_lwt *ilwt = ila_lwt_lwtunnel(dst->lwtstate);
  94. if (skb->protocol != htons(ETH_P_IPV6))
  95. goto drop;
  96. if (!ilwt->lwt_output)
  97. ila_update_ipv6_locator(skb,
  98. ila_params_lwtunnel(dst->lwtstate),
  99. false);
  100. return dst->lwtstate->orig_input(skb);
  101. drop:
  102. kfree_skb(skb);
  103. return -EINVAL;
  104. }
  105. static const struct nla_policy ila_nl_policy[ILA_ATTR_MAX + 1] = {
  106. [ILA_ATTR_LOCATOR] = { .type = NLA_U64, },
  107. [ILA_ATTR_CSUM_MODE] = { .type = NLA_U8, },
  108. [ILA_ATTR_IDENT_TYPE] = { .type = NLA_U8, },
  109. [ILA_ATTR_HOOK_TYPE] = { .type = NLA_U8, },
  110. };
  111. static int ila_build_state(struct net *net, struct nlattr *nla,
  112. unsigned int family, const void *cfg,
  113. struct lwtunnel_state **ts,
  114. struct netlink_ext_ack *extack)
  115. {
  116. struct ila_lwt *ilwt;
  117. struct ila_params *p;
  118. struct nlattr *tb[ILA_ATTR_MAX + 1];
  119. struct lwtunnel_state *newts;
  120. const struct fib6_config *cfg6 = cfg;
  121. struct ila_addr *iaddr;
  122. u8 ident_type = ILA_ATYPE_USE_FORMAT;
  123. u8 hook_type = ILA_HOOK_ROUTE_OUTPUT;
  124. u8 csum_mode = ILA_CSUM_NO_ACTION;
  125. bool lwt_output = true;
  126. u8 eff_ident_type;
  127. int ret;
  128. if (family != AF_INET6)
  129. return -EINVAL;
  130. ret = nla_parse_nested_deprecated(tb, ILA_ATTR_MAX, nla,
  131. ila_nl_policy, extack);
  132. if (ret < 0)
  133. return ret;
  134. if (!tb[ILA_ATTR_LOCATOR])
  135. return -EINVAL;
  136. iaddr = (struct ila_addr *)&cfg6->fc_dst;
  137. if (tb[ILA_ATTR_IDENT_TYPE])
  138. ident_type = nla_get_u8(tb[ILA_ATTR_IDENT_TYPE]);
  139. if (ident_type == ILA_ATYPE_USE_FORMAT) {
  140. /* Infer identifier type from type field in formatted
  141. * identifier.
  142. */
  143. if (cfg6->fc_dst_len < 8 * sizeof(struct ila_locator) + 3) {
  144. /* Need to have full locator and at least type field
  145. * included in destination
  146. */
  147. return -EINVAL;
  148. }
  149. eff_ident_type = iaddr->ident.type;
  150. } else {
  151. eff_ident_type = ident_type;
  152. }
  153. switch (eff_ident_type) {
  154. case ILA_ATYPE_IID:
  155. /* Don't allow ILA for IID type */
  156. return -EINVAL;
  157. case ILA_ATYPE_LUID:
  158. break;
  159. case ILA_ATYPE_VIRT_V4:
  160. case ILA_ATYPE_VIRT_UNI_V6:
  161. case ILA_ATYPE_VIRT_MULTI_V6:
  162. case ILA_ATYPE_NONLOCAL_ADDR:
  163. /* These ILA formats are not supported yet. */
  164. default:
  165. return -EINVAL;
  166. }
  167. if (tb[ILA_ATTR_HOOK_TYPE])
  168. hook_type = nla_get_u8(tb[ILA_ATTR_HOOK_TYPE]);
  169. switch (hook_type) {
  170. case ILA_HOOK_ROUTE_OUTPUT:
  171. lwt_output = true;
  172. break;
  173. case ILA_HOOK_ROUTE_INPUT:
  174. lwt_output = false;
  175. break;
  176. default:
  177. return -EINVAL;
  178. }
  179. if (tb[ILA_ATTR_CSUM_MODE])
  180. csum_mode = nla_get_u8(tb[ILA_ATTR_CSUM_MODE]);
  181. if (csum_mode == ILA_CSUM_NEUTRAL_MAP &&
  182. ila_csum_neutral_set(iaddr->ident)) {
  183. /* Don't allow translation if checksum neutral bit is
  184. * configured and it's set in the SIR address.
  185. */
  186. return -EINVAL;
  187. }
  188. newts = lwtunnel_state_alloc(sizeof(*ilwt));
  189. if (!newts)
  190. return -ENOMEM;
  191. ilwt = ila_lwt_lwtunnel(newts);
  192. ret = dst_cache_init(&ilwt->dst_cache, GFP_ATOMIC);
  193. if (ret) {
  194. kfree(newts);
  195. return ret;
  196. }
  197. ilwt->lwt_output = !!lwt_output;
  198. p = ila_params_lwtunnel(newts);
  199. p->csum_mode = csum_mode;
  200. p->ident_type = ident_type;
  201. p->locator.v64 = (__force __be64)nla_get_u64(tb[ILA_ATTR_LOCATOR]);
  202. /* Precompute checksum difference for translation since we
  203. * know both the old locator and the new one.
  204. */
  205. p->locator_match = iaddr->loc;
  206. ila_init_saved_csum(p);
  207. newts->type = LWTUNNEL_ENCAP_ILA;
  208. newts->flags |= LWTUNNEL_STATE_OUTPUT_REDIRECT |
  209. LWTUNNEL_STATE_INPUT_REDIRECT;
  210. if (cfg6->fc_dst_len == 8 * sizeof(struct in6_addr))
  211. ilwt->connected = 1;
  212. *ts = newts;
  213. return 0;
  214. }
  215. static void ila_destroy_state(struct lwtunnel_state *lwt)
  216. {
  217. dst_cache_destroy(&ila_lwt_lwtunnel(lwt)->dst_cache);
  218. }
  219. static int ila_fill_encap_info(struct sk_buff *skb,
  220. struct lwtunnel_state *lwtstate)
  221. {
  222. struct ila_params *p = ila_params_lwtunnel(lwtstate);
  223. struct ila_lwt *ilwt = ila_lwt_lwtunnel(lwtstate);
  224. if (nla_put_u64_64bit(skb, ILA_ATTR_LOCATOR, (__force u64)p->locator.v64,
  225. ILA_ATTR_PAD))
  226. goto nla_put_failure;
  227. if (nla_put_u8(skb, ILA_ATTR_CSUM_MODE, (__force u8)p->csum_mode))
  228. goto nla_put_failure;
  229. if (nla_put_u8(skb, ILA_ATTR_IDENT_TYPE, (__force u8)p->ident_type))
  230. goto nla_put_failure;
  231. if (nla_put_u8(skb, ILA_ATTR_HOOK_TYPE,
  232. ilwt->lwt_output ? ILA_HOOK_ROUTE_OUTPUT :
  233. ILA_HOOK_ROUTE_INPUT))
  234. goto nla_put_failure;
  235. return 0;
  236. nla_put_failure:
  237. return -EMSGSIZE;
  238. }
  239. static int ila_encap_nlsize(struct lwtunnel_state *lwtstate)
  240. {
  241. return nla_total_size_64bit(sizeof(u64)) + /* ILA_ATTR_LOCATOR */
  242. nla_total_size(sizeof(u8)) + /* ILA_ATTR_CSUM_MODE */
  243. nla_total_size(sizeof(u8)) + /* ILA_ATTR_IDENT_TYPE */
  244. nla_total_size(sizeof(u8)) + /* ILA_ATTR_HOOK_TYPE */
  245. 0;
  246. }
  247. static int ila_encap_cmp(struct lwtunnel_state *a, struct lwtunnel_state *b)
  248. {
  249. struct ila_params *a_p = ila_params_lwtunnel(a);
  250. struct ila_params *b_p = ila_params_lwtunnel(b);
  251. return (a_p->locator.v64 != b_p->locator.v64);
  252. }
  253. static const struct lwtunnel_encap_ops ila_encap_ops = {
  254. .build_state = ila_build_state,
  255. .destroy_state = ila_destroy_state,
  256. .output = ila_output,
  257. .input = ila_input,
  258. .fill_encap = ila_fill_encap_info,
  259. .get_encap_size = ila_encap_nlsize,
  260. .cmp_encap = ila_encap_cmp,
  261. .owner = THIS_MODULE,
  262. };
  263. int ila_lwt_init(void)
  264. {
  265. return lwtunnel_encap_add_ops(&ila_encap_ops, LWTUNNEL_ENCAP_ILA);
  266. }
  267. void ila_lwt_fini(void)
  268. {
  269. lwtunnel_encap_del_ops(&ila_encap_ops, LWTUNNEL_ENCAP_ILA);
  270. }