blob: e924c2e08554094e824449e3829d42f148906e4a [file] [log] [blame]
Roopa Prabhu499a2422015-07-21 10:43:46 +02001/*
2 * lwtunnel Infrastructure for light weight tunnels like mpls
3 *
4 * Authors: Roopa Prabhu, <roopa@cumulusnetworks.com>
5 *
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.
10 *
11 */
12
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>
25
26#include <net/lwtunnel.h>
27#include <net/rtnetlink.h>
Roopa Prabhuffce4192015-07-21 10:43:49 +020028#include <net/ip6_fib.h>
Roopa Prabhu499a2422015-07-21 10:43:46 +020029
30struct lwtunnel_state *lwtunnel_state_alloc(int encap_len)
31{
32 struct lwtunnel_state *lws;
33
34 lws = kzalloc(sizeof(*lws) + encap_len, GFP_ATOMIC);
35
36 return lws;
37}
38EXPORT_SYMBOL(lwtunnel_state_alloc);
39
Thomas Graf92a99bf2015-07-29 09:45:40 +020040static const struct lwtunnel_encap_ops __rcu *
Roopa Prabhu499a2422015-07-21 10:43:46 +020041 lwtun_encaps[LWTUNNEL_ENCAP_MAX + 1] __read_mostly;
42
43int lwtunnel_encap_add_ops(const struct lwtunnel_encap_ops *ops,
44 unsigned int num)
45{
46 if (num > LWTUNNEL_ENCAP_MAX)
47 return -ERANGE;
48
49 return !cmpxchg((const struct lwtunnel_encap_ops **)
50 &lwtun_encaps[num],
51 NULL, ops) ? 0 : -1;
52}
53EXPORT_SYMBOL(lwtunnel_encap_add_ops);
54
55int lwtunnel_encap_del_ops(const struct lwtunnel_encap_ops *ops,
56 unsigned int encap_type)
57{
58 int ret;
59
60 if (encap_type == LWTUNNEL_ENCAP_NONE ||
61 encap_type > LWTUNNEL_ENCAP_MAX)
62 return -ERANGE;
63
64 ret = (cmpxchg((const struct lwtunnel_encap_ops **)
65 &lwtun_encaps[encap_type],
66 ops, NULL) == ops) ? 0 : -1;
67
68 synchronize_net();
69
70 return ret;
71}
72EXPORT_SYMBOL(lwtunnel_encap_del_ops);
73
74int lwtunnel_build_state(struct net_device *dev, u16 encap_type,
75 struct nlattr *encap, struct lwtunnel_state **lws)
76{
77 const struct lwtunnel_encap_ops *ops;
78 int ret = -EINVAL;
79
80 if (encap_type == LWTUNNEL_ENCAP_NONE ||
81 encap_type > LWTUNNEL_ENCAP_MAX)
82 return ret;
83
84 ret = -EOPNOTSUPP;
85 rcu_read_lock();
86 ops = rcu_dereference(lwtun_encaps[encap_type]);
87 if (likely(ops && ops->build_state))
88 ret = ops->build_state(dev, encap, lws);
89 rcu_read_unlock();
90
91 return ret;
92}
93EXPORT_SYMBOL(lwtunnel_build_state);
94
95int lwtunnel_fill_encap(struct sk_buff *skb, struct lwtunnel_state *lwtstate)
96{
97 const struct lwtunnel_encap_ops *ops;
98 struct nlattr *nest;
99 int ret = -EINVAL;
100
101 if (!lwtstate)
102 return 0;
103
104 if (lwtstate->type == LWTUNNEL_ENCAP_NONE ||
105 lwtstate->type > LWTUNNEL_ENCAP_MAX)
106 return 0;
107
108 ret = -EOPNOTSUPP;
109 nest = nla_nest_start(skb, RTA_ENCAP);
110 rcu_read_lock();
111 ops = rcu_dereference(lwtun_encaps[lwtstate->type]);
112 if (likely(ops && ops->fill_encap))
113 ret = ops->fill_encap(skb, lwtstate);
114 rcu_read_unlock();
115
116 if (ret)
117 goto nla_put_failure;
118 nla_nest_end(skb, nest);
119 ret = nla_put_u16(skb, RTA_ENCAP_TYPE, lwtstate->type);
120 if (ret)
121 goto nla_put_failure;
122
123 return 0;
124
125nla_put_failure:
126 nla_nest_cancel(skb, nest);
127
128 return (ret == -EOPNOTSUPP ? 0 : ret);
129}
130EXPORT_SYMBOL(lwtunnel_fill_encap);
131
132int lwtunnel_get_encap_size(struct lwtunnel_state *lwtstate)
133{
134 const struct lwtunnel_encap_ops *ops;
135 int ret = 0;
136
137 if (!lwtstate)
138 return 0;
139
140 if (lwtstate->type == LWTUNNEL_ENCAP_NONE ||
141 lwtstate->type > LWTUNNEL_ENCAP_MAX)
142 return 0;
143
144 rcu_read_lock();
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));
148 rcu_read_unlock();
149
150 return ret;
151}
152EXPORT_SYMBOL(lwtunnel_get_encap_size);
153
154int lwtunnel_cmp_encap(struct lwtunnel_state *a, struct lwtunnel_state *b)
155{
156 const struct lwtunnel_encap_ops *ops;
157 int ret = 0;
158
159 if (!a && !b)
160 return 0;
161
162 if (!a || !b)
163 return 1;
164
165 if (a->type != b->type)
166 return 1;
167
168 if (a->type == LWTUNNEL_ENCAP_NONE ||
169 a->type > LWTUNNEL_ENCAP_MAX)
170 return 0;
171
172 rcu_read_lock();
173 ops = rcu_dereference(lwtun_encaps[a->type]);
174 if (likely(ops && ops->cmp_encap))
175 ret = ops->cmp_encap(a, b);
176 rcu_read_unlock();
177
178 return ret;
179}
180EXPORT_SYMBOL(lwtunnel_cmp_encap);
Roopa Prabhuffce4192015-07-21 10:43:49 +0200181
Jiri Benc61adedf2015-08-20 13:56:25 +0200182int lwtunnel_output(struct sock *sk, struct sk_buff *skb)
Roopa Prabhuffce4192015-07-21 10:43:49 +0200183{
Jiri Benc61adedf2015-08-20 13:56:25 +0200184 struct dst_entry *dst = skb_dst(skb);
Roopa Prabhuffce4192015-07-21 10:43:49 +0200185 const struct lwtunnel_encap_ops *ops;
Jiri Benc61adedf2015-08-20 13:56:25 +0200186 struct lwtunnel_state *lwtstate;
Roopa Prabhuffce4192015-07-21 10:43:49 +0200187 int ret = -EINVAL;
188
Jiri Benc61adedf2015-08-20 13:56:25 +0200189 if (!dst)
Roopa Prabhuffce4192015-07-21 10:43:49 +0200190 goto drop;
Jiri Benc61adedf2015-08-20 13:56:25 +0200191 lwtstate = dst->lwtstate;
Roopa Prabhuffce4192015-07-21 10:43:49 +0200192
193 if (lwtstate->type == LWTUNNEL_ENCAP_NONE ||
194 lwtstate->type > LWTUNNEL_ENCAP_MAX)
195 return 0;
196
197 ret = -EOPNOTSUPP;
198 rcu_read_lock();
199 ops = rcu_dereference(lwtun_encaps[lwtstate->type]);
200 if (likely(ops && ops->output))
201 ret = ops->output(sk, skb);
202 rcu_read_unlock();
203
204 if (ret == -EOPNOTSUPP)
205 goto drop;
206
207 return ret;
208
209drop:
Dan Carpentere11f40b2015-07-27 11:07:47 +0300210 kfree_skb(skb);
Roopa Prabhuffce4192015-07-21 10:43:49 +0200211
212 return ret;
213}
Roopa Prabhuffce4192015-07-21 10:43:49 +0200214EXPORT_SYMBOL(lwtunnel_output);
Tom Herbert25368622015-08-17 13:42:24 -0700215
Jiri Benc61adedf2015-08-20 13:56:25 +0200216int lwtunnel_input(struct sk_buff *skb)
Tom Herbert25368622015-08-17 13:42:24 -0700217{
Jiri Benc61adedf2015-08-20 13:56:25 +0200218 struct dst_entry *dst = skb_dst(skb);
Tom Herbert25368622015-08-17 13:42:24 -0700219 const struct lwtunnel_encap_ops *ops;
Jiri Benc61adedf2015-08-20 13:56:25 +0200220 struct lwtunnel_state *lwtstate;
Tom Herbert25368622015-08-17 13:42:24 -0700221 int ret = -EINVAL;
222
Jiri Benc61adedf2015-08-20 13:56:25 +0200223 if (!dst)
Tom Herbert25368622015-08-17 13:42:24 -0700224 goto drop;
Jiri Benc61adedf2015-08-20 13:56:25 +0200225 lwtstate = dst->lwtstate;
Tom Herbert25368622015-08-17 13:42:24 -0700226
227 if (lwtstate->type == LWTUNNEL_ENCAP_NONE ||
228 lwtstate->type > LWTUNNEL_ENCAP_MAX)
229 return 0;
230
231 ret = -EOPNOTSUPP;
232 rcu_read_lock();
233 ops = rcu_dereference(lwtun_encaps[lwtstate->type]);
234 if (likely(ops && ops->input))
235 ret = ops->input(skb);
236 rcu_read_unlock();
237
238 if (ret == -EOPNOTSUPP)
239 goto drop;
240
241 return ret;
242
243drop:
244 kfree_skb(skb);
245
246 return ret;
247}
Tom Herbert25368622015-08-17 13:42:24 -0700248EXPORT_SYMBOL(lwtunnel_input);