1 #include <linux/errno.h>
3 #include <linux/kernel.h>
4 #include <linux/module.h>
5 #include <linux/skbuff.h>
6 #include <linux/socket.h>
7 #include <linux/types.h>
8 #include <net/checksum.h>
10 #include <net/ip6_fib.h>
11 #include <net/lwtunnel.h>
12 #include <net/protocol.h>
13 #include <uapi/linux/ila.h>
16 static inline struct ila_params *ila_params_lwtunnel(
17 struct lwtunnel_state *lwstate)
19 return (struct ila_params *)lwstate->data;
22 static int ila_output(struct net *net, struct sock *sk, struct sk_buff *skb)
24 struct dst_entry *dst = skb_dst(skb);
26 if (skb->protocol != htons(ETH_P_IPV6))
29 ila_update_ipv6_locator(skb, ila_params_lwtunnel(dst->lwtstate));
31 return dst->lwtstate->orig_output(net, sk, skb);
38 static int ila_input(struct sk_buff *skb)
40 struct dst_entry *dst = skb_dst(skb);
42 if (skb->protocol != htons(ETH_P_IPV6))
45 ila_update_ipv6_locator(skb, ila_params_lwtunnel(dst->lwtstate));
47 return dst->lwtstate->orig_input(skb);
54 static struct nla_policy ila_nl_policy[ILA_ATTR_MAX + 1] = {
55 [ILA_ATTR_LOCATOR] = { .type = NLA_U64, },
58 static int ila_build_state(struct net_device *dev, struct nlattr *nla,
59 unsigned int family, const void *cfg,
60 struct lwtunnel_state **ts)
63 struct nlattr *tb[ILA_ATTR_MAX + 1];
64 size_t encap_len = sizeof(*p);
65 struct lwtunnel_state *newts;
66 const struct fib6_config *cfg6 = cfg;
67 struct ila_addr *iaddr;
70 if (family != AF_INET6)
73 if (cfg6->fc_dst_len < sizeof(struct ila_locator) + 1) {
74 /* Need to have full locator and at least type field
75 * included in destination
80 iaddr = (struct ila_addr *)&cfg6->fc_dst;
82 if (!ila_addr_is_ila(iaddr)) {
83 /* Don't allow setting a translation for a non-ILA address */
87 ret = nla_parse_nested(tb, ILA_ATTR_MAX, nla,
92 if (!tb[ILA_ATTR_LOCATOR])
95 newts = lwtunnel_state_alloc(encap_len);
99 newts->len = encap_len;
100 p = ila_params_lwtunnel(newts);
102 p->locator.v64 = (__force __be64)nla_get_u64(tb[ILA_ATTR_LOCATOR]);
104 /* Precompute checksum difference for translation since we
105 * know both the old locator and the new one.
107 p->locator_match = iaddr->loc;
108 p->csum_diff = compute_csum_diff8(
109 (__be32 *)&p->locator_match, (__be32 *)&p->locator);
111 newts->type = LWTUNNEL_ENCAP_ILA;
112 newts->flags |= LWTUNNEL_STATE_OUTPUT_REDIRECT |
113 LWTUNNEL_STATE_INPUT_REDIRECT;
120 static int ila_fill_encap_info(struct sk_buff *skb,
121 struct lwtunnel_state *lwtstate)
123 struct ila_params *p = ila_params_lwtunnel(lwtstate);
125 if (nla_put_u64_64bit(skb, ILA_ATTR_LOCATOR, (__force u64)p->locator.v64,
127 goto nla_put_failure;
135 static int ila_encap_nlsize(struct lwtunnel_state *lwtstate)
137 /* No encapsulation overhead */
141 static int ila_encap_cmp(struct lwtunnel_state *a, struct lwtunnel_state *b)
143 struct ila_params *a_p = ila_params_lwtunnel(a);
144 struct ila_params *b_p = ila_params_lwtunnel(b);
146 return (a_p->locator.v64 != b_p->locator.v64);
149 static const struct lwtunnel_encap_ops ila_encap_ops = {
150 .build_state = ila_build_state,
151 .output = ila_output,
153 .fill_encap = ila_fill_encap_info,
154 .get_encap_size = ila_encap_nlsize,
155 .cmp_encap = ila_encap_cmp,
158 int ila_lwt_init(void)
160 return lwtunnel_encap_add_ops(&ila_encap_ops, LWTUNNEL_ENCAP_ILA);
163 void ila_lwt_fini(void)
165 lwtunnel_encap_del_ops(&ila_encap_ops, LWTUNNEL_ENCAP_ILA);