[NETLINK]: Improve string attribute validation
[deliverable/linux.git] / net / core / fib_rules.c
CommitLineData
14c0b97d
TG
1/*
2 * net/core/fib_rules.c Generic Routing Rules
3 *
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License as
6 * published by the Free Software Foundation, version 2.
7 *
8 * Authors: Thomas Graf <tgraf@suug.ch>
9 */
10
11#include <linux/config.h>
12#include <linux/types.h>
13#include <linux/kernel.h>
14#include <linux/list.h>
15#include <net/fib_rules.h>
16
17static LIST_HEAD(rules_ops);
18static DEFINE_SPINLOCK(rules_mod_lock);
19
20static void notify_rule_change(int event, struct fib_rule *rule,
c17084d2
TG
21 struct fib_rules_ops *ops, struct nlmsghdr *nlh,
22 u32 pid);
14c0b97d
TG
23
24static struct fib_rules_ops *lookup_rules_ops(int family)
25{
26 struct fib_rules_ops *ops;
27
28 rcu_read_lock();
29 list_for_each_entry_rcu(ops, &rules_ops, list) {
30 if (ops->family == family) {
31 if (!try_module_get(ops->owner))
32 ops = NULL;
33 rcu_read_unlock();
34 return ops;
35 }
36 }
37 rcu_read_unlock();
38
39 return NULL;
40}
41
42static void rules_ops_put(struct fib_rules_ops *ops)
43{
44 if (ops)
45 module_put(ops->owner);
46}
47
48int fib_rules_register(struct fib_rules_ops *ops)
49{
50 int err = -EEXIST;
51 struct fib_rules_ops *o;
52
53 if (ops->rule_size < sizeof(struct fib_rule))
54 return -EINVAL;
55
56 if (ops->match == NULL || ops->configure == NULL ||
57 ops->compare == NULL || ops->fill == NULL ||
58 ops->action == NULL)
59 return -EINVAL;
60
61 spin_lock(&rules_mod_lock);
62 list_for_each_entry(o, &rules_ops, list)
63 if (ops->family == o->family)
64 goto errout;
65
66 list_add_tail_rcu(&ops->list, &rules_ops);
67 err = 0;
68errout:
69 spin_unlock(&rules_mod_lock);
70
71 return err;
72}
73
74EXPORT_SYMBOL_GPL(fib_rules_register);
75
76static void cleanup_ops(struct fib_rules_ops *ops)
77{
78 struct fib_rule *rule, *tmp;
79
80 list_for_each_entry_safe(rule, tmp, ops->rules_list, list) {
81 list_del_rcu(&rule->list);
82 fib_rule_put(rule);
83 }
84}
85
86int fib_rules_unregister(struct fib_rules_ops *ops)
87{
88 int err = 0;
89 struct fib_rules_ops *o;
90
91 spin_lock(&rules_mod_lock);
92 list_for_each_entry(o, &rules_ops, list) {
93 if (o == ops) {
94 list_del_rcu(&o->list);
95 cleanup_ops(ops);
96 goto out;
97 }
98 }
99
100 err = -ENOENT;
101out:
102 spin_unlock(&rules_mod_lock);
103
104 synchronize_rcu();
105
106 return err;
107}
108
109EXPORT_SYMBOL_GPL(fib_rules_unregister);
110
111int fib_rules_lookup(struct fib_rules_ops *ops, struct flowi *fl,
112 int flags, struct fib_lookup_arg *arg)
113{
114 struct fib_rule *rule;
115 int err;
116
117 rcu_read_lock();
118
119 list_for_each_entry_rcu(rule, ops->rules_list, list) {
120 if (rule->ifindex && (rule->ifindex != fl->iif))
121 continue;
122
123 if (!ops->match(rule, fl, flags))
124 continue;
125
126 err = ops->action(rule, fl, flags, arg);
127 if (err != -EAGAIN) {
128 fib_rule_get(rule);
129 arg->rule = rule;
130 goto out;
131 }
132 }
133
134 err = -ENETUNREACH;
135out:
136 rcu_read_unlock();
137
138 return err;
139}
140
141EXPORT_SYMBOL_GPL(fib_rules_lookup);
142
143int fib_nl_newrule(struct sk_buff *skb, struct nlmsghdr* nlh, void *arg)
144{
145 struct fib_rule_hdr *frh = nlmsg_data(nlh);
146 struct fib_rules_ops *ops = NULL;
147 struct fib_rule *rule, *r, *last = NULL;
148 struct nlattr *tb[FRA_MAX+1];
149 int err = -EINVAL;
150
151 if (nlh->nlmsg_len < nlmsg_msg_size(sizeof(*frh)))
152 goto errout;
153
154 ops = lookup_rules_ops(frh->family);
155 if (ops == NULL) {
156 err = EAFNOSUPPORT;
157 goto errout;
158 }
159
160 err = nlmsg_parse(nlh, sizeof(*frh), tb, FRA_MAX, ops->policy);
161 if (err < 0)
162 goto errout;
163
164 if (tb[FRA_IFNAME] && nla_len(tb[FRA_IFNAME]) > IFNAMSIZ)
165 goto errout;
166
167 rule = kzalloc(ops->rule_size, GFP_KERNEL);
168 if (rule == NULL) {
169 err = -ENOMEM;
170 goto errout;
171 }
172
173 if (tb[FRA_PRIORITY])
174 rule->pref = nla_get_u32(tb[FRA_PRIORITY]);
175
176 if (tb[FRA_IFNAME]) {
177 struct net_device *dev;
178
179 rule->ifindex = -1;
180 if (nla_strlcpy(rule->ifname, tb[FRA_IFNAME],
181 IFNAMSIZ) >= IFNAMSIZ)
182 goto errout_free;
183
184 dev = __dev_get_by_name(rule->ifname);
185 if (dev)
186 rule->ifindex = dev->ifindex;
187 }
188
189 rule->action = frh->action;
190 rule->flags = frh->flags;
9e762a4a 191 rule->table = frh_get_table(frh, tb);
14c0b97d
TG
192
193 if (!rule->pref && ops->default_pref)
194 rule->pref = ops->default_pref();
195
196 err = ops->configure(rule, skb, nlh, frh, tb);
197 if (err < 0)
198 goto errout_free;
199
200 list_for_each_entry(r, ops->rules_list, list) {
201 if (r->pref > rule->pref)
202 break;
203 last = r;
204 }
205
206 fib_rule_get(rule);
207
208 if (last)
209 list_add_rcu(&rule->list, &last->list);
210 else
211 list_add_rcu(&rule->list, ops->rules_list);
212
c17084d2 213 notify_rule_change(RTM_NEWRULE, rule, ops, nlh, NETLINK_CB(skb).pid);
14c0b97d
TG
214 rules_ops_put(ops);
215 return 0;
216
217errout_free:
218 kfree(rule);
219errout:
220 rules_ops_put(ops);
221 return err;
222}
223
224int fib_nl_delrule(struct sk_buff *skb, struct nlmsghdr* nlh, void *arg)
225{
226 struct fib_rule_hdr *frh = nlmsg_data(nlh);
227 struct fib_rules_ops *ops = NULL;
228 struct fib_rule *rule;
229 struct nlattr *tb[FRA_MAX+1];
230 int err = -EINVAL;
231
232 if (nlh->nlmsg_len < nlmsg_msg_size(sizeof(*frh)))
233 goto errout;
234
235 ops = lookup_rules_ops(frh->family);
236 if (ops == NULL) {
237 err = EAFNOSUPPORT;
238 goto errout;
239 }
240
241 err = nlmsg_parse(nlh, sizeof(*frh), tb, FRA_MAX, ops->policy);
242 if (err < 0)
243 goto errout;
244
245 list_for_each_entry(rule, ops->rules_list, list) {
246 if (frh->action && (frh->action != rule->action))
247 continue;
248
9e762a4a 249 if (frh->table && (frh_get_table(frh, tb) != rule->table))
14c0b97d
TG
250 continue;
251
252 if (tb[FRA_PRIORITY] &&
253 (rule->pref != nla_get_u32(tb[FRA_PRIORITY])))
254 continue;
255
256 if (tb[FRA_IFNAME] &&
257 nla_strcmp(tb[FRA_IFNAME], rule->ifname))
258 continue;
259
260 if (!ops->compare(rule, frh, tb))
261 continue;
262
263 if (rule->flags & FIB_RULE_PERMANENT) {
264 err = -EPERM;
265 goto errout;
266 }
267
268 list_del_rcu(&rule->list);
269 synchronize_rcu();
c17084d2
TG
270 notify_rule_change(RTM_DELRULE, rule, ops, nlh,
271 NETLINK_CB(skb).pid);
14c0b97d
TG
272 fib_rule_put(rule);
273 rules_ops_put(ops);
274 return 0;
275 }
276
277 err = -ENOENT;
278errout:
279 rules_ops_put(ops);
280 return err;
281}
282
283static int fib_nl_fill_rule(struct sk_buff *skb, struct fib_rule *rule,
284 u32 pid, u32 seq, int type, int flags,
285 struct fib_rules_ops *ops)
286{
287 struct nlmsghdr *nlh;
288 struct fib_rule_hdr *frh;
289
290 nlh = nlmsg_put(skb, pid, seq, type, sizeof(*frh), flags);
291 if (nlh == NULL)
292 return -1;
293
294 frh = nlmsg_data(nlh);
295 frh->table = rule->table;
9e762a4a 296 NLA_PUT_U32(skb, FRA_TABLE, rule->table);
14c0b97d
TG
297 frh->res1 = 0;
298 frh->res2 = 0;
299 frh->action = rule->action;
300 frh->flags = rule->flags;
301
302 if (rule->ifname[0])
303 NLA_PUT_STRING(skb, FRA_IFNAME, rule->ifname);
304
305 if (rule->pref)
306 NLA_PUT_U32(skb, FRA_PRIORITY, rule->pref);
307
308 if (ops->fill(rule, skb, nlh, frh) < 0)
309 goto nla_put_failure;
310
311 return nlmsg_end(skb, nlh);
312
313nla_put_failure:
314 return nlmsg_cancel(skb, nlh);
315}
316
317int fib_rules_dump(struct sk_buff *skb, struct netlink_callback *cb, int family)
318{
319 int idx = 0;
320 struct fib_rule *rule;
321 struct fib_rules_ops *ops;
322
323 ops = lookup_rules_ops(family);
324 if (ops == NULL)
325 return -EAFNOSUPPORT;
326
327 rcu_read_lock();
328 list_for_each_entry(rule, ops->rules_list, list) {
329 if (idx < cb->args[0])
330 goto skip;
331
332 if (fib_nl_fill_rule(skb, rule, NETLINK_CB(cb->skb).pid,
333 cb->nlh->nlmsg_seq, RTM_NEWRULE,
334 NLM_F_MULTI, ops) < 0)
335 break;
336skip:
337 idx++;
338 }
339 rcu_read_unlock();
340 cb->args[0] = idx;
341 rules_ops_put(ops);
342
343 return skb->len;
344}
345
346EXPORT_SYMBOL_GPL(fib_rules_dump);
347
348static void notify_rule_change(int event, struct fib_rule *rule,
c17084d2
TG
349 struct fib_rules_ops *ops, struct nlmsghdr *nlh,
350 u32 pid)
14c0b97d 351{
c17084d2
TG
352 struct sk_buff *skb;
353 int err = -ENOBUFS;
14c0b97d 354
c17084d2 355 skb = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);
14c0b97d 356 if (skb == NULL)
c17084d2
TG
357 goto errout;
358
359 err = fib_nl_fill_rule(skb, rule, pid, nlh->nlmsg_seq, event, 0, ops);
360 if (err < 0) {
14c0b97d 361 kfree_skb(skb);
c17084d2
TG
362 goto errout;
363 }
364
365 err = rtnl_notify(skb, pid, ops->nlgroup, nlh, GFP_KERNEL);
366errout:
367 if (err < 0)
368 rtnl_set_sk_err(ops->nlgroup, err);
14c0b97d
TG
369}
370
371static void attach_rules(struct list_head *rules, struct net_device *dev)
372{
373 struct fib_rule *rule;
374
375 list_for_each_entry(rule, rules, list) {
376 if (rule->ifindex == -1 &&
377 strcmp(dev->name, rule->ifname) == 0)
378 rule->ifindex = dev->ifindex;
379 }
380}
381
382static void detach_rules(struct list_head *rules, struct net_device *dev)
383{
384 struct fib_rule *rule;
385
386 list_for_each_entry(rule, rules, list)
387 if (rule->ifindex == dev->ifindex)
388 rule->ifindex = -1;
389}
390
391
392static int fib_rules_event(struct notifier_block *this, unsigned long event,
393 void *ptr)
394{
395 struct net_device *dev = ptr;
396 struct fib_rules_ops *ops;
397
398 ASSERT_RTNL();
399 rcu_read_lock();
400
401 switch (event) {
402 case NETDEV_REGISTER:
403 list_for_each_entry(ops, &rules_ops, list)
404 attach_rules(ops->rules_list, dev);
405 break;
406
407 case NETDEV_UNREGISTER:
408 list_for_each_entry(ops, &rules_ops, list)
409 detach_rules(ops->rules_list, dev);
410 break;
411 }
412
413 rcu_read_unlock();
414
415 return NOTIFY_DONE;
416}
417
418static struct notifier_block fib_rules_notifier = {
419 .notifier_call = fib_rules_event,
420};
421
422static int __init fib_rules_init(void)
423{
424 return register_netdevice_notifier(&fib_rules_notifier);
425}
426
427subsys_initcall(fib_rules_init);
This page took 0.046451 seconds and 5 git commands to generate.