2 * lwtunnel Infrastructure for light weight tunnels like mpls
4 * Authors: Roopa Prabhu, <roopa@cumulusnetworks.com>
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>
30 struct lwtunnel_state *lwtunnel_state_alloc(int encap_len)
32 struct lwtunnel_state *lws;
34 lws = kzalloc(sizeof(*lws) + encap_len, GFP_ATOMIC);
38 EXPORT_SYMBOL(lwtunnel_state_alloc);
40 static const struct lwtunnel_encap_ops __rcu *
41 lwtun_encaps[LWTUNNEL_ENCAP_MAX + 1] __read_mostly;
43 int lwtunnel_encap_add_ops(const struct lwtunnel_encap_ops *ops,
46 if (num > LWTUNNEL_ENCAP_MAX)
49 return !cmpxchg((const struct lwtunnel_encap_ops **)
53 EXPORT_SYMBOL(lwtunnel_encap_add_ops);
55 int lwtunnel_encap_del_ops(const struct lwtunnel_encap_ops *ops,
56 unsigned int encap_type)
60 if (encap_type == LWTUNNEL_ENCAP_NONE ||
61 encap_type > LWTUNNEL_ENCAP_MAX)
64 ret = (cmpxchg((const struct lwtunnel_encap_ops **)
65 &lwtun_encaps[encap_type],
66 ops, NULL) == ops) ? 0 : -1;
72 EXPORT_SYMBOL(lwtunnel_encap_del_ops);
74 int lwtunnel_build_state(struct net_device *dev, u16 encap_type,
75 struct nlattr *encap, struct lwtunnel_state **lws)
77 const struct lwtunnel_encap_ops *ops;
80 if (encap_type == LWTUNNEL_ENCAP_NONE ||
81 encap_type > LWTUNNEL_ENCAP_MAX)
86 ops = rcu_dereference(lwtun_encaps[encap_type]);
87 if (likely(ops && ops->build_state))
88 ret = ops->build_state(dev, encap, lws);
93 EXPORT_SYMBOL(lwtunnel_build_state);
95 int lwtunnel_fill_encap(struct sk_buff *skb, struct lwtunnel_state *lwtstate)
97 const struct lwtunnel_encap_ops *ops;
104 if (lwtstate->type == LWTUNNEL_ENCAP_NONE ||
105 lwtstate->type > LWTUNNEL_ENCAP_MAX)
109 nest = nla_nest_start(skb, RTA_ENCAP);
111 ops = rcu_dereference(lwtun_encaps[lwtstate->type]);
112 if (likely(ops && ops->fill_encap))
113 ret = ops->fill_encap(skb, lwtstate);
117 goto nla_put_failure;
118 nla_nest_end(skb, nest);
119 ret = nla_put_u16(skb, RTA_ENCAP_TYPE, lwtstate->type);
121 goto nla_put_failure;
126 nla_nest_cancel(skb, nest);
128 return (ret == -EOPNOTSUPP ? 0 : ret);
130 EXPORT_SYMBOL(lwtunnel_fill_encap);
132 int lwtunnel_get_encap_size(struct lwtunnel_state *lwtstate)
134 const struct lwtunnel_encap_ops *ops;
140 if (lwtstate->type == LWTUNNEL_ENCAP_NONE ||
141 lwtstate->type > LWTUNNEL_ENCAP_MAX)
145 ops = rcu_dereference(lwtun_encaps[lwtstate->type]);
146 if (likely(ops && ops->get_encap_size))
147 ret = nla_total_size(ops->get_encap_size(lwtstate));
152 EXPORT_SYMBOL(lwtunnel_get_encap_size);
154 int lwtunnel_cmp_encap(struct lwtunnel_state *a, struct lwtunnel_state *b)
156 const struct lwtunnel_encap_ops *ops;
165 if (a->type != b->type)
168 if (a->type == LWTUNNEL_ENCAP_NONE ||
169 a->type > LWTUNNEL_ENCAP_MAX)
173 ops = rcu_dereference(lwtun_encaps[a->type]);
174 if (likely(ops && ops->cmp_encap))
175 ret = ops->cmp_encap(a, b);
180 EXPORT_SYMBOL(lwtunnel_cmp_encap);
182 int __lwtunnel_output(struct sock *sk, struct sk_buff *skb,
183 struct lwtunnel_state *lwtstate)
185 const struct lwtunnel_encap_ops *ops;
191 if (lwtstate->type == LWTUNNEL_ENCAP_NONE ||
192 lwtstate->type > LWTUNNEL_ENCAP_MAX)
197 ops = rcu_dereference(lwtun_encaps[lwtstate->type]);
198 if (likely(ops && ops->output))
199 ret = ops->output(sk, skb);
202 if (ret == -EOPNOTSUPP)
213 int lwtunnel_output6(struct sock *sk, struct sk_buff *skb)
215 struct rt6_info *rt = (struct rt6_info *)skb_dst(skb);
216 struct lwtunnel_state *lwtstate = NULL;
219 lwtstate = rt->rt6i_lwtstate;
220 skb->dev = rt->dst.dev;
223 skb->protocol = htons(ETH_P_IPV6);
225 return __lwtunnel_output(sk, skb, lwtstate);
227 EXPORT_SYMBOL(lwtunnel_output6);
229 int lwtunnel_output(struct sock *sk, struct sk_buff *skb)
231 struct rtable *rt = (struct rtable *)skb_dst(skb);
232 struct lwtunnel_state *lwtstate = NULL;
235 lwtstate = rt->rt_lwtstate;
236 skb->dev = rt->dst.dev;
239 skb->protocol = htons(ETH_P_IP);
241 return __lwtunnel_output(sk, skb, lwtstate);
243 EXPORT_SYMBOL(lwtunnel_output);