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 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(tb, ILA_ATTR_MAX, nla, ila_nl_policy, extack);
  126. if (ret < 0)
  127. return ret;
  128. if (!tb[ILA_ATTR_LOCATOR])
  129. return -EINVAL;
  130. iaddr = (struct ila_addr *)&cfg6->fc_dst;
  131. if (tb[ILA_ATTR_IDENT_TYPE])
  132. ident_type = nla_get_u8(tb[ILA_ATTR_IDENT_TYPE]);
  133. if (ident_type == ILA_ATYPE_USE_FORMAT) {
  134. /* Infer identifier type from type field in formatted
  135. * identifier.
  136. */
  137. if (cfg6->fc_dst_len < 8 * sizeof(struct ila_locator) + 3) {
  138. /* Need to have full locator and at least type field
  139. * included in destination
  140. */
  141. return -EINVAL;
  142. }
  143. eff_ident_type = iaddr->ident.type;
  144. } else {
  145. eff_ident_type = ident_type;
  146. }
  147. switch (eff_ident_type) {
  148. case ILA_ATYPE_IID:
  149. /* Don't allow ILA for IID type */
  150. return -EINVAL;
  151. case ILA_ATYPE_LUID:
  152. break;
  153. case ILA_ATYPE_VIRT_V4:
  154. case ILA_ATYPE_VIRT_UNI_V6:
  155. case ILA_ATYPE_VIRT_MULTI_V6:
  156. case ILA_ATYPE_NONLOCAL_ADDR:
  157. /* These ILA formats are not supported yet. */
  158. default:
  159. return -EINVAL;
  160. }
  161. if (tb[ILA_ATTR_HOOK_TYPE])
  162. hook_type = nla_get_u8(tb[ILA_ATTR_HOOK_TYPE]);
  163. switch (hook_type) {
  164. case ILA_HOOK_ROUTE_OUTPUT:
  165. lwt_output = true;
  166. break;
  167. case ILA_HOOK_ROUTE_INPUT:
  168. lwt_output = false;
  169. break;
  170. default:
  171. return -EINVAL;
  172. }
  173. if (tb[ILA_ATTR_CSUM_MODE])
  174. csum_mode = nla_get_u8(tb[ILA_ATTR_CSUM_MODE]);
  175. if (csum_mode == ILA_CSUM_NEUTRAL_MAP &&
  176. ila_csum_neutral_set(iaddr->ident)) {
  177. /* Don't allow translation if checksum neutral bit is
  178. * configured and it's set in the SIR address.
  179. */
  180. return -EINVAL;
  181. }
  182. newts = lwtunnel_state_alloc(sizeof(*ilwt));
  183. if (!newts)
  184. return -ENOMEM;
  185. ilwt = ila_lwt_lwtunnel(newts);
  186. ret = dst_cache_init(&ilwt->dst_cache, GFP_ATOMIC);
  187. if (ret) {
  188. kfree(newts);
  189. return ret;
  190. }
  191. ilwt->lwt_output = !!lwt_output;
  192. p = ila_params_lwtunnel(newts);
  193. p->csum_mode = csum_mode;
  194. p->ident_type = ident_type;
  195. p->locator.v64 = (__force __be64)nla_get_u64(tb[ILA_ATTR_LOCATOR]);
  196. /* Precompute checksum difference for translation since we
  197. * know both the old locator and the new one.
  198. */
  199. p->locator_match = iaddr->loc;
  200. ila_init_saved_csum(p);
  201. newts->type = LWTUNNEL_ENCAP_ILA;
  202. newts->flags |= LWTUNNEL_STATE_OUTPUT_REDIRECT |
  203. LWTUNNEL_STATE_INPUT_REDIRECT;
  204. if (cfg6->fc_dst_len == 8 * sizeof(struct in6_addr))
  205. ilwt->connected = 1;
  206. *ts = newts;
  207. return 0;
  208. }
  209. static void ila_destroy_state(struct lwtunnel_state *lwt)
  210. {
  211. dst_cache_destroy(&ila_lwt_lwtunnel(lwt)->dst_cache);
  212. }
  213. static int ila_fill_encap_info(struct sk_buff *skb,
  214. struct lwtunnel_state *lwtstate)
  215. {
  216. struct ila_params *p = ila_params_lwtunnel(lwtstate);
  217. struct ila_lwt *ilwt = ila_lwt_lwtunnel(lwtstate);
  218. if (nla_put_u64_64bit(skb, ILA_ATTR_LOCATOR, (__force u64)p->locator.v64,
  219. ILA_ATTR_PAD))
  220. goto nla_put_failure;
  221. if (nla_put_u8(skb, ILA_ATTR_CSUM_MODE, (__force u8)p->csum_mode))
  222. goto nla_put_failure;
  223. if (nla_put_u8(skb, ILA_ATTR_IDENT_TYPE, (__force u8)p->ident_type))
  224. goto nla_put_failure;
  225. if (nla_put_u8(skb, ILA_ATTR_HOOK_TYPE,
  226. ilwt->lwt_output ? ILA_HOOK_ROUTE_OUTPUT :
  227. ILA_HOOK_ROUTE_INPUT))
  228. goto nla_put_failure;
  229. return 0;
  230. nla_put_failure:
  231. return -EMSGSIZE;
  232. }
  233. static int ila_encap_nlsize(struct lwtunnel_state *lwtstate)
  234. {
  235. return nla_total_size_64bit(sizeof(u64)) + /* ILA_ATTR_LOCATOR */
  236. nla_total_size(sizeof(u8)) + /* ILA_ATTR_CSUM_MODE */
  237. nla_total_size(sizeof(u8)) + /* ILA_ATTR_IDENT_TYPE */
  238. nla_total_size(sizeof(u8)) + /* ILA_ATTR_HOOK_TYPE */
  239. 0;
  240. }
  241. static int ila_encap_cmp(struct lwtunnel_state *a, struct lwtunnel_state *b)
  242. {
  243. struct ila_params *a_p = ila_params_lwtunnel(a);
  244. struct ila_params *b_p = ila_params_lwtunnel(b);
  245. return (a_p->locator.v64 != b_p->locator.v64);
  246. }
  247. static const struct lwtunnel_encap_ops ila_encap_ops = {
  248. .build_state = ila_build_state,
  249. .destroy_state = ila_destroy_state,
  250. .output = ila_output,
  251. .input = ila_input,
  252. .fill_encap = ila_fill_encap_info,
  253. .get_encap_size = ila_encap_nlsize,
  254. .cmp_encap = ila_encap_cmp,
  255. .owner = THIS_MODULE,
  256. };
  257. int ila_lwt_init(void)
  258. {
  259. return lwtunnel_encap_add_ops(&ila_encap_ops, LWTUNNEL_ENCAP_ILA);
  260. }
  261. void ila_lwt_fini(void)
  262. {
  263. lwtunnel_encap_del_ops(&ila_encap_ops, LWTUNNEL_ENCAP_ILA);
  264. }