ila_lwt.c 7.6 KB

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