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