ipv4: Don't do expensive useless work during inetdev destroy.
[linux-2.6/btrfs-unstable.git] / net / core / lwtunnel.c
blob669ecc9f884eabab925a1ef02ec834a7d22e6f4b
1 /*
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>
22 #include <linux/in.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 #ifdef CONFIG_MODULES
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.
37 switch (encap_type) {
38 case LWTUNNEL_ENCAP_MPLS:
39 return "MPLS";
40 case LWTUNNEL_ENCAP_ILA:
41 return "ILA";
42 case LWTUNNEL_ENCAP_IP6:
43 case LWTUNNEL_ENCAP_IP:
44 case LWTUNNEL_ENCAP_NONE:
45 case __LWTUNNEL_ENCAP_MAX:
46 /* should not have got here */
47 WARN_ON(1);
48 break;
50 return NULL;
53 #endif /* CONFIG_MODULES */
55 struct lwtunnel_state *lwtunnel_state_alloc(int encap_len)
57 struct lwtunnel_state *lws;
59 lws = kzalloc(sizeof(*lws) + encap_len, GFP_ATOMIC);
61 return lws;
63 EXPORT_SYMBOL(lwtunnel_state_alloc);
65 static const struct lwtunnel_encap_ops __rcu *
66 lwtun_encaps[LWTUNNEL_ENCAP_MAX + 1] __read_mostly;
68 int lwtunnel_encap_add_ops(const struct lwtunnel_encap_ops *ops,
69 unsigned int num)
71 if (num > LWTUNNEL_ENCAP_MAX)
72 return -ERANGE;
74 return !cmpxchg((const struct lwtunnel_encap_ops **)
75 &lwtun_encaps[num],
76 NULL, ops) ? 0 : -1;
78 EXPORT_SYMBOL(lwtunnel_encap_add_ops);
80 int lwtunnel_encap_del_ops(const struct lwtunnel_encap_ops *ops,
81 unsigned int encap_type)
83 int ret;
85 if (encap_type == LWTUNNEL_ENCAP_NONE ||
86 encap_type > LWTUNNEL_ENCAP_MAX)
87 return -ERANGE;
89 ret = (cmpxchg((const struct lwtunnel_encap_ops **)
90 &lwtun_encaps[encap_type],
91 ops, NULL) == ops) ? 0 : -1;
93 synchronize_net();
95 return ret;
97 EXPORT_SYMBOL(lwtunnel_encap_del_ops);
99 int lwtunnel_build_state(struct net_device *dev, u16 encap_type,
100 struct nlattr *encap, unsigned int family,
101 const void *cfg, struct lwtunnel_state **lws)
103 const struct lwtunnel_encap_ops *ops;
104 int ret = -EINVAL;
106 if (encap_type == LWTUNNEL_ENCAP_NONE ||
107 encap_type > LWTUNNEL_ENCAP_MAX)
108 return ret;
110 ret = -EOPNOTSUPP;
111 rcu_read_lock();
112 ops = rcu_dereference(lwtun_encaps[encap_type]);
113 #ifdef CONFIG_MODULES
114 if (!ops) {
115 const char *encap_type_str = lwtunnel_encap_str(encap_type);
117 if (encap_type_str) {
118 rcu_read_unlock();
119 request_module("rtnl-lwt-%s", encap_type_str);
120 rcu_read_lock();
121 ops = rcu_dereference(lwtun_encaps[encap_type]);
124 #endif
125 if (likely(ops && ops->build_state))
126 ret = ops->build_state(dev, encap, family, cfg, lws);
127 rcu_read_unlock();
129 return ret;
131 EXPORT_SYMBOL(lwtunnel_build_state);
133 int lwtunnel_fill_encap(struct sk_buff *skb, struct lwtunnel_state *lwtstate)
135 const struct lwtunnel_encap_ops *ops;
136 struct nlattr *nest;
137 int ret = -EINVAL;
139 if (!lwtstate)
140 return 0;
142 if (lwtstate->type == LWTUNNEL_ENCAP_NONE ||
143 lwtstate->type > LWTUNNEL_ENCAP_MAX)
144 return 0;
146 ret = -EOPNOTSUPP;
147 nest = nla_nest_start(skb, RTA_ENCAP);
148 rcu_read_lock();
149 ops = rcu_dereference(lwtun_encaps[lwtstate->type]);
150 if (likely(ops && ops->fill_encap))
151 ret = ops->fill_encap(skb, lwtstate);
152 rcu_read_unlock();
154 if (ret)
155 goto nla_put_failure;
156 nla_nest_end(skb, nest);
157 ret = nla_put_u16(skb, RTA_ENCAP_TYPE, lwtstate->type);
158 if (ret)
159 goto nla_put_failure;
161 return 0;
163 nla_put_failure:
164 nla_nest_cancel(skb, nest);
166 return (ret == -EOPNOTSUPP ? 0 : ret);
168 EXPORT_SYMBOL(lwtunnel_fill_encap);
170 int lwtunnel_get_encap_size(struct lwtunnel_state *lwtstate)
172 const struct lwtunnel_encap_ops *ops;
173 int ret = 0;
175 if (!lwtstate)
176 return 0;
178 if (lwtstate->type == LWTUNNEL_ENCAP_NONE ||
179 lwtstate->type > LWTUNNEL_ENCAP_MAX)
180 return 0;
182 rcu_read_lock();
183 ops = rcu_dereference(lwtun_encaps[lwtstate->type]);
184 if (likely(ops && ops->get_encap_size))
185 ret = nla_total_size(ops->get_encap_size(lwtstate));
186 rcu_read_unlock();
188 return ret;
190 EXPORT_SYMBOL(lwtunnel_get_encap_size);
192 int lwtunnel_cmp_encap(struct lwtunnel_state *a, struct lwtunnel_state *b)
194 const struct lwtunnel_encap_ops *ops;
195 int ret = 0;
197 if (!a && !b)
198 return 0;
200 if (!a || !b)
201 return 1;
203 if (a->type != b->type)
204 return 1;
206 if (a->type == LWTUNNEL_ENCAP_NONE ||
207 a->type > LWTUNNEL_ENCAP_MAX)
208 return 0;
210 rcu_read_lock();
211 ops = rcu_dereference(lwtun_encaps[a->type]);
212 if (likely(ops && ops->cmp_encap))
213 ret = ops->cmp_encap(a, b);
214 rcu_read_unlock();
216 return ret;
218 EXPORT_SYMBOL(lwtunnel_cmp_encap);
220 int lwtunnel_output(struct net *net, struct sock *sk, struct sk_buff *skb)
222 struct dst_entry *dst = skb_dst(skb);
223 const struct lwtunnel_encap_ops *ops;
224 struct lwtunnel_state *lwtstate;
225 int ret = -EINVAL;
227 if (!dst)
228 goto drop;
229 lwtstate = dst->lwtstate;
231 if (lwtstate->type == LWTUNNEL_ENCAP_NONE ||
232 lwtstate->type > LWTUNNEL_ENCAP_MAX)
233 return 0;
235 ret = -EOPNOTSUPP;
236 rcu_read_lock();
237 ops = rcu_dereference(lwtun_encaps[lwtstate->type]);
238 if (likely(ops && ops->output))
239 ret = ops->output(net, sk, skb);
240 rcu_read_unlock();
242 if (ret == -EOPNOTSUPP)
243 goto drop;
245 return ret;
247 drop:
248 kfree_skb(skb);
250 return ret;
252 EXPORT_SYMBOL(lwtunnel_output);
254 int lwtunnel_input(struct sk_buff *skb)
256 struct dst_entry *dst = skb_dst(skb);
257 const struct lwtunnel_encap_ops *ops;
258 struct lwtunnel_state *lwtstate;
259 int ret = -EINVAL;
261 if (!dst)
262 goto drop;
263 lwtstate = dst->lwtstate;
265 if (lwtstate->type == LWTUNNEL_ENCAP_NONE ||
266 lwtstate->type > LWTUNNEL_ENCAP_MAX)
267 return 0;
269 ret = -EOPNOTSUPP;
270 rcu_read_lock();
271 ops = rcu_dereference(lwtun_encaps[lwtstate->type]);
272 if (likely(ops && ops->input))
273 ret = ops->input(skb);
274 rcu_read_unlock();
276 if (ret == -EOPNOTSUPP)
277 goto drop;
279 return ret;
281 drop:
282 kfree_skb(skb);
284 return ret;
286 EXPORT_SYMBOL(lwtunnel_input);