2 * lwtunnel Infrastructure for light weight tunnels like mpls
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version
9 * 2 of the License, or (at your option) any later version.
13 #include <linux/capability.h>
14 #include <linux/module.h>
15 #include <linux/types.h>
16 #include <linux/kernel.h>
17 #include <linux/slab.h>
18 #include <linux/uaccess.h>
19 #include <linux/skbuff.h>
20 #include <linux/netdevice.h>
21 #include <linux/lwtunnel.h>
23 #include <linux/init.h>
24 #include <linux/err.h>
26 #include <net/lwtunnel.h>
27 #include <net/rtnetlink.h>
28 #include <net/ip6_fib.h>
32 static const char *lwtunnel_encap_str(enum lwtunnel_encap_types encap_type)
34 /* Only lwt encaps implemented without using an interface for
35 * the encap need to return a string here.
38 case LWTUNNEL_ENCAP_MPLS:
40 case LWTUNNEL_ENCAP_ILA:
42 case LWTUNNEL_ENCAP_SEG6:
44 case LWTUNNEL_ENCAP_BPF:
46 case LWTUNNEL_ENCAP_IP6:
47 case LWTUNNEL_ENCAP_IP:
48 case LWTUNNEL_ENCAP_NONE:
49 case __LWTUNNEL_ENCAP_MAX:
50 /* should not have got here */
57 #endif /* CONFIG_MODULES */
59 struct lwtunnel_state *lwtunnel_state_alloc(int encap_len)
61 struct lwtunnel_state *lws;
63 lws = kzalloc(sizeof(*lws) + encap_len, GFP_ATOMIC);
67 EXPORT_SYMBOL(lwtunnel_state_alloc);
69 static const struct lwtunnel_encap_ops __rcu *
70 lwtun_encaps[LWTUNNEL_ENCAP_MAX + 1] __read_mostly;
72 int lwtunnel_encap_add_ops(const struct lwtunnel_encap_ops *ops,
75 if (num > LWTUNNEL_ENCAP_MAX)
78 return !cmpxchg((const struct lwtunnel_encap_ops **)
82 EXPORT_SYMBOL(lwtunnel_encap_add_ops);
84 int lwtunnel_encap_del_ops(const struct lwtunnel_encap_ops *ops,
85 unsigned int encap_type)
89 if (encap_type == LWTUNNEL_ENCAP_NONE ||
90 encap_type > LWTUNNEL_ENCAP_MAX)
93 ret = (cmpxchg((const struct lwtunnel_encap_ops **)
94 &lwtun_encaps[encap_type],
95 ops, NULL) == ops) ? 0 : -1;
101 EXPORT_SYMBOL(lwtunnel_encap_del_ops);
103 int lwtunnel_build_state(struct net_device *dev, u16 encap_type,
104 struct nlattr *encap, unsigned int family,
105 const void *cfg, struct lwtunnel_state **lws)
107 const struct lwtunnel_encap_ops *ops;
110 if (encap_type == LWTUNNEL_ENCAP_NONE ||
111 encap_type > LWTUNNEL_ENCAP_MAX)
116 ops = rcu_dereference(lwtun_encaps[encap_type]);
117 #ifdef CONFIG_MODULES
119 const char *encap_type_str = lwtunnel_encap_str(encap_type);
121 if (encap_type_str) {
123 request_module("rtnl-lwt-%s", encap_type_str);
125 ops = rcu_dereference(lwtun_encaps[encap_type]);
129 if (likely(ops && ops->build_state))
130 ret = ops->build_state(dev, encap, family, cfg, lws);
135 EXPORT_SYMBOL(lwtunnel_build_state);
137 void lwtstate_free(struct lwtunnel_state *lws)
139 const struct lwtunnel_encap_ops *ops = lwtun_encaps[lws->type];
141 if (ops->destroy_state) {
142 ops->destroy_state(lws);
148 EXPORT_SYMBOL(lwtstate_free);
150 int lwtunnel_fill_encap(struct sk_buff *skb, struct lwtunnel_state *lwtstate)
152 const struct lwtunnel_encap_ops *ops;
159 if (lwtstate->type == LWTUNNEL_ENCAP_NONE ||
160 lwtstate->type > LWTUNNEL_ENCAP_MAX)
164 nest = nla_nest_start(skb, RTA_ENCAP);
166 ops = rcu_dereference(lwtun_encaps[lwtstate->type]);
167 if (likely(ops && ops->fill_encap))
168 ret = ops->fill_encap(skb, lwtstate);
172 goto nla_put_failure;
173 nla_nest_end(skb, nest);
174 ret = nla_put_u16(skb, RTA_ENCAP_TYPE, lwtstate->type);
176 goto nla_put_failure;
181 nla_nest_cancel(skb, nest);
183 return (ret == -EOPNOTSUPP ? 0 : ret);
185 EXPORT_SYMBOL(lwtunnel_fill_encap);
187 int lwtunnel_get_encap_size(struct lwtunnel_state *lwtstate)
189 const struct lwtunnel_encap_ops *ops;
195 if (lwtstate->type == LWTUNNEL_ENCAP_NONE ||
196 lwtstate->type > LWTUNNEL_ENCAP_MAX)
200 ops = rcu_dereference(lwtun_encaps[lwtstate->type]);
201 if (likely(ops && ops->get_encap_size))
202 ret = nla_total_size(ops->get_encap_size(lwtstate));
207 EXPORT_SYMBOL(lwtunnel_get_encap_size);
209 int lwtunnel_cmp_encap(struct lwtunnel_state *a, struct lwtunnel_state *b)
211 const struct lwtunnel_encap_ops *ops;
220 if (a->type != b->type)
223 if (a->type == LWTUNNEL_ENCAP_NONE ||
224 a->type > LWTUNNEL_ENCAP_MAX)
228 ops = rcu_dereference(lwtun_encaps[a->type]);
229 if (likely(ops && ops->cmp_encap))
230 ret = ops->cmp_encap(a, b);
235 EXPORT_SYMBOL(lwtunnel_cmp_encap);
237 int lwtunnel_output(struct net *net, struct sock *sk, struct sk_buff *skb)
239 struct dst_entry *dst = skb_dst(skb);
240 const struct lwtunnel_encap_ops *ops;
241 struct lwtunnel_state *lwtstate;
246 lwtstate = dst->lwtstate;
248 if (lwtstate->type == LWTUNNEL_ENCAP_NONE ||
249 lwtstate->type > LWTUNNEL_ENCAP_MAX)
254 ops = rcu_dereference(lwtun_encaps[lwtstate->type]);
255 if (likely(ops && ops->output))
256 ret = ops->output(net, sk, skb);
259 if (ret == -EOPNOTSUPP)
269 EXPORT_SYMBOL(lwtunnel_output);
271 int lwtunnel_xmit(struct sk_buff *skb)
273 struct dst_entry *dst = skb_dst(skb);
274 const struct lwtunnel_encap_ops *ops;
275 struct lwtunnel_state *lwtstate;
281 lwtstate = dst->lwtstate;
283 if (lwtstate->type == LWTUNNEL_ENCAP_NONE ||
284 lwtstate->type > LWTUNNEL_ENCAP_MAX)
289 ops = rcu_dereference(lwtun_encaps[lwtstate->type]);
290 if (likely(ops && ops->xmit))
291 ret = ops->xmit(skb);
294 if (ret == -EOPNOTSUPP)
304 EXPORT_SYMBOL(lwtunnel_xmit);
306 int lwtunnel_input(struct sk_buff *skb)
308 struct dst_entry *dst = skb_dst(skb);
309 const struct lwtunnel_encap_ops *ops;
310 struct lwtunnel_state *lwtstate;
315 lwtstate = dst->lwtstate;
317 if (lwtstate->type == LWTUNNEL_ENCAP_NONE ||
318 lwtstate->type > LWTUNNEL_ENCAP_MAX)
323 ops = rcu_dereference(lwtun_encaps[lwtstate->type]);
324 if (likely(ops && ops->input))
325 ret = ops->input(skb);
328 if (ret == -EOPNOTSUPP)
338 EXPORT_SYMBOL(lwtunnel_input);