blob: 3cd4f13413f64f4d92b9a972362442bcbfe29ac2 [file] [log] [blame]
Thomas Graf14c0b972006-08-04 03:38:38 -07001/*
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
Thomas Graf14c0b972006-08-04 03:38:38 -070011#include <linux/types.h>
12#include <linux/kernel.h>
13#include <linux/list.h>
Eric W. Biedermane9dc8652007-09-12 13:02:17 +020014#include <net/net_namespace.h>
Eric W. Biederman881d9662007-09-17 11:56:21 -070015#include <net/sock.h>
Thomas Graf14c0b972006-08-04 03:38:38 -070016#include <net/fib_rules.h>
17
Denis V. Lunev2994c632007-11-10 22:12:03 -080018int fib_default_rule_add(struct fib_rules_ops *ops,
19 u32 pref, u32 table, u32 flags)
20{
21 struct fib_rule *r;
22
23 r = kzalloc(ops->rule_size, GFP_KERNEL);
24 if (r == NULL)
25 return -ENOMEM;
26
27 atomic_set(&r->refcnt, 1);
28 r->action = FR_ACT_TO_TBL;
29 r->pref = pref;
30 r->table = table;
31 r->flags = flags;
32
33 /* The lock is not required here, the list in unreacheable
34 * at the moment this function is called */
35 list_add_tail(&r->list, &ops->rules_list);
36 return 0;
37}
38EXPORT_SYMBOL(fib_default_rule_add);
39
Denis V. Lunev9e3a5482008-01-20 16:46:41 -080040static void notify_rule_change(int event, struct fib_rule *rule,
Thomas Grafc17084d2006-08-15 00:32:48 -070041 struct fib_rules_ops *ops, struct nlmsghdr *nlh,
42 u32 pid);
Thomas Graf14c0b972006-08-04 03:38:38 -070043
Denis V. Lunev5fd30ee2008-01-10 03:20:28 -080044static struct fib_rules_ops *lookup_rules_ops(struct net *net, int family)
Thomas Graf14c0b972006-08-04 03:38:38 -070045{
46 struct fib_rules_ops *ops;
47
48 rcu_read_lock();
Denis V. Lunev5fd30ee2008-01-10 03:20:28 -080049 list_for_each_entry_rcu(ops, &net->rules_ops, list) {
Thomas Graf14c0b972006-08-04 03:38:38 -070050 if (ops->family == family) {
51 if (!try_module_get(ops->owner))
52 ops = NULL;
53 rcu_read_unlock();
54 return ops;
55 }
56 }
57 rcu_read_unlock();
58
59 return NULL;
60}
61
62static void rules_ops_put(struct fib_rules_ops *ops)
63{
64 if (ops)
65 module_put(ops->owner);
66}
67
Thomas Graf73417f62007-03-27 13:56:52 -070068static void flush_route_cache(struct fib_rules_ops *ops)
69{
70 if (ops->flush_cache)
71 ops->flush_cache();
72}
73
Denis V. Lunev9e3a5482008-01-20 16:46:41 -080074int fib_rules_register(struct fib_rules_ops *ops)
Thomas Graf14c0b972006-08-04 03:38:38 -070075{
76 int err = -EEXIST;
77 struct fib_rules_ops *o;
Denis V. Lunev9e3a5482008-01-20 16:46:41 -080078 struct net *net;
79
80 net = ops->fro_net;
Thomas Graf14c0b972006-08-04 03:38:38 -070081
82 if (ops->rule_size < sizeof(struct fib_rule))
83 return -EINVAL;
84
85 if (ops->match == NULL || ops->configure == NULL ||
86 ops->compare == NULL || ops->fill == NULL ||
87 ops->action == NULL)
88 return -EINVAL;
89
Denis V. Lunev5fd30ee2008-01-10 03:20:28 -080090 spin_lock(&net->rules_mod_lock);
91 list_for_each_entry(o, &net->rules_ops, list)
Thomas Graf14c0b972006-08-04 03:38:38 -070092 if (ops->family == o->family)
93 goto errout;
94
Denis V. Lunev5fd30ee2008-01-10 03:20:28 -080095 hold_net(net);
96 list_add_tail_rcu(&ops->list, &net->rules_ops);
Thomas Graf14c0b972006-08-04 03:38:38 -070097 err = 0;
98errout:
Denis V. Lunev5fd30ee2008-01-10 03:20:28 -080099 spin_unlock(&net->rules_mod_lock);
Thomas Graf14c0b972006-08-04 03:38:38 -0700100
101 return err;
102}
103
104EXPORT_SYMBOL_GPL(fib_rules_register);
105
Daniel Lezcano9eb87f32007-12-07 00:42:52 -0800106void fib_rules_cleanup_ops(struct fib_rules_ops *ops)
Thomas Graf14c0b972006-08-04 03:38:38 -0700107{
108 struct fib_rule *rule, *tmp;
109
Denis V. Lunev76c72d42007-09-16 15:44:27 -0700110 list_for_each_entry_safe(rule, tmp, &ops->rules_list, list) {
Thomas Graf14c0b972006-08-04 03:38:38 -0700111 list_del_rcu(&rule->list);
112 fib_rule_put(rule);
113 }
114}
Daniel Lezcano9eb87f32007-12-07 00:42:52 -0800115EXPORT_SYMBOL_GPL(fib_rules_cleanup_ops);
Thomas Graf14c0b972006-08-04 03:38:38 -0700116
Denis V. Lunev9e3a5482008-01-20 16:46:41 -0800117void fib_rules_unregister(struct fib_rules_ops *ops)
Thomas Graf14c0b972006-08-04 03:38:38 -0700118{
Denis V. Lunev9e3a5482008-01-20 16:46:41 -0800119 struct net *net = ops->fro_net;
Thomas Graf14c0b972006-08-04 03:38:38 -0700120
Denis V. Lunev5fd30ee2008-01-10 03:20:28 -0800121 spin_lock(&net->rules_mod_lock);
Denis V. Lunev72132c1b2008-01-14 22:59:30 -0800122 list_del_rcu(&ops->list);
123 fib_rules_cleanup_ops(ops);
Denis V. Lunev5fd30ee2008-01-10 03:20:28 -0800124 spin_unlock(&net->rules_mod_lock);
Thomas Graf14c0b972006-08-04 03:38:38 -0700125
126 synchronize_rcu();
Denis V. Lunev72132c1b2008-01-14 22:59:30 -0800127 release_net(net);
Thomas Graf14c0b972006-08-04 03:38:38 -0700128}
129
130EXPORT_SYMBOL_GPL(fib_rules_unregister);
131
Thomas Graf3dfbcc42006-11-09 15:23:20 -0800132static int fib_rule_match(struct fib_rule *rule, struct fib_rules_ops *ops,
133 struct flowi *fl, int flags)
134{
135 int ret = 0;
136
137 if (rule->ifindex && (rule->ifindex != fl->iif))
138 goto out;
139
140 if ((rule->mark ^ fl->mark) & rule->mark_mask)
141 goto out;
142
143 ret = ops->match(rule, fl, flags);
144out:
145 return (rule->flags & FIB_RULE_INVERT) ? !ret : ret;
146}
147
Thomas Graf14c0b972006-08-04 03:38:38 -0700148int fib_rules_lookup(struct fib_rules_ops *ops, struct flowi *fl,
149 int flags, struct fib_lookup_arg *arg)
150{
151 struct fib_rule *rule;
152 int err;
153
154 rcu_read_lock();
155
Denis V. Lunev76c72d42007-09-16 15:44:27 -0700156 list_for_each_entry_rcu(rule, &ops->rules_list, list) {
Thomas Graf0947c9fe2007-03-26 17:14:15 -0700157jumped:
Thomas Graf3dfbcc42006-11-09 15:23:20 -0800158 if (!fib_rule_match(rule, ops, fl, flags))
Thomas Graf14c0b972006-08-04 03:38:38 -0700159 continue;
160
Thomas Graf0947c9fe2007-03-26 17:14:15 -0700161 if (rule->action == FR_ACT_GOTO) {
162 struct fib_rule *target;
163
164 target = rcu_dereference(rule->ctarget);
165 if (target == NULL) {
166 continue;
167 } else {
168 rule = target;
169 goto jumped;
170 }
Thomas Graffa0b2d12007-03-26 17:38:53 -0700171 } else if (rule->action == FR_ACT_NOP)
172 continue;
173 else
Thomas Graf0947c9fe2007-03-26 17:14:15 -0700174 err = ops->action(rule, fl, flags, arg);
175
Thomas Graf14c0b972006-08-04 03:38:38 -0700176 if (err != -EAGAIN) {
177 fib_rule_get(rule);
178 arg->rule = rule;
179 goto out;
180 }
181 }
182
Steven Whitehouse83886b62007-03-30 13:34:27 -0700183 err = -ESRCH;
Thomas Graf14c0b972006-08-04 03:38:38 -0700184out:
185 rcu_read_unlock();
186
187 return err;
188}
189
190EXPORT_SYMBOL_GPL(fib_rules_lookup);
191
Thomas Grafe1701c62007-03-24 12:46:02 -0700192static int validate_rulemsg(struct fib_rule_hdr *frh, struct nlattr **tb,
193 struct fib_rules_ops *ops)
194{
195 int err = -EINVAL;
196
197 if (frh->src_len)
198 if (tb[FRA_SRC] == NULL ||
199 frh->src_len > (ops->addr_size * 8) ||
200 nla_len(tb[FRA_SRC]) != ops->addr_size)
201 goto errout;
202
203 if (frh->dst_len)
204 if (tb[FRA_DST] == NULL ||
205 frh->dst_len > (ops->addr_size * 8) ||
206 nla_len(tb[FRA_DST]) != ops->addr_size)
207 goto errout;
208
209 err = 0;
210errout:
211 return err;
212}
213
Thomas Graf9d9e6a52007-03-25 23:20:05 -0700214static int fib_nl_newrule(struct sk_buff *skb, struct nlmsghdr* nlh, void *arg)
Thomas Graf14c0b972006-08-04 03:38:38 -0700215{
Eric W. Biederman881d9662007-09-17 11:56:21 -0700216 struct net *net = skb->sk->sk_net;
Thomas Graf14c0b972006-08-04 03:38:38 -0700217 struct fib_rule_hdr *frh = nlmsg_data(nlh);
218 struct fib_rules_ops *ops = NULL;
219 struct fib_rule *rule, *r, *last = NULL;
220 struct nlattr *tb[FRA_MAX+1];
Thomas Graf0947c9fe2007-03-26 17:14:15 -0700221 int err = -EINVAL, unresolved = 0;
Thomas Graf14c0b972006-08-04 03:38:38 -0700222
223 if (nlh->nlmsg_len < nlmsg_msg_size(sizeof(*frh)))
224 goto errout;
225
Denis V. Lunev5fd30ee2008-01-10 03:20:28 -0800226 ops = lookup_rules_ops(net, frh->family);
Thomas Graf14c0b972006-08-04 03:38:38 -0700227 if (ops == NULL) {
228 err = EAFNOSUPPORT;
229 goto errout;
230 }
231
232 err = nlmsg_parse(nlh, sizeof(*frh), tb, FRA_MAX, ops->policy);
233 if (err < 0)
234 goto errout;
235
Thomas Grafe1701c62007-03-24 12:46:02 -0700236 err = validate_rulemsg(frh, tb, ops);
237 if (err < 0)
238 goto errout;
239
Thomas Graf14c0b972006-08-04 03:38:38 -0700240 rule = kzalloc(ops->rule_size, GFP_KERNEL);
241 if (rule == NULL) {
242 err = -ENOMEM;
243 goto errout;
244 }
245
246 if (tb[FRA_PRIORITY])
247 rule->pref = nla_get_u32(tb[FRA_PRIORITY]);
248
249 if (tb[FRA_IFNAME]) {
250 struct net_device *dev;
251
252 rule->ifindex = -1;
Thomas Graf5176f912006-08-26 20:13:18 -0700253 nla_strlcpy(rule->ifname, tb[FRA_IFNAME], IFNAMSIZ);
Eric W. Biederman881d9662007-09-17 11:56:21 -0700254 dev = __dev_get_by_name(net, rule->ifname);
Thomas Graf14c0b972006-08-04 03:38:38 -0700255 if (dev)
256 rule->ifindex = dev->ifindex;
257 }
258
Thomas Grafb8964ed2006-11-09 15:22:18 -0800259 if (tb[FRA_FWMARK]) {
260 rule->mark = nla_get_u32(tb[FRA_FWMARK]);
261 if (rule->mark)
262 /* compatibility: if the mark value is non-zero all bits
263 * are compared unless a mask is explicitly specified.
264 */
265 rule->mark_mask = 0xFFFFFFFF;
266 }
267
268 if (tb[FRA_FWMASK])
269 rule->mark_mask = nla_get_u32(tb[FRA_FWMASK]);
270
Thomas Graf14c0b972006-08-04 03:38:38 -0700271 rule->action = frh->action;
272 rule->flags = frh->flags;
Patrick McHardy9e762a42006-08-10 23:09:48 -0700273 rule->table = frh_get_table(frh, tb);
Thomas Graf14c0b972006-08-04 03:38:38 -0700274
275 if (!rule->pref && ops->default_pref)
Denis V. Lunev868d13a2008-01-10 03:18:25 -0800276 rule->pref = ops->default_pref(ops);
Thomas Graf14c0b972006-08-04 03:38:38 -0700277
Thomas Graf0947c9fe2007-03-26 17:14:15 -0700278 err = -EINVAL;
279 if (tb[FRA_GOTO]) {
280 if (rule->action != FR_ACT_GOTO)
281 goto errout_free;
282
283 rule->target = nla_get_u32(tb[FRA_GOTO]);
284 /* Backward jumps are prohibited to avoid endless loops */
285 if (rule->target <= rule->pref)
286 goto errout_free;
287
Denis V. Lunev76c72d42007-09-16 15:44:27 -0700288 list_for_each_entry(r, &ops->rules_list, list) {
Thomas Graf0947c9fe2007-03-26 17:14:15 -0700289 if (r->pref == rule->target) {
290 rule->ctarget = r;
291 break;
292 }
293 }
294
295 if (rule->ctarget == NULL)
296 unresolved = 1;
297 } else if (rule->action == FR_ACT_GOTO)
298 goto errout_free;
299
Thomas Graf14c0b972006-08-04 03:38:38 -0700300 err = ops->configure(rule, skb, nlh, frh, tb);
301 if (err < 0)
302 goto errout_free;
303
Denis V. Lunev76c72d42007-09-16 15:44:27 -0700304 list_for_each_entry(r, &ops->rules_list, list) {
Thomas Graf14c0b972006-08-04 03:38:38 -0700305 if (r->pref > rule->pref)
306 break;
307 last = r;
308 }
309
310 fib_rule_get(rule);
311
Thomas Graf0947c9fe2007-03-26 17:14:15 -0700312 if (ops->unresolved_rules) {
313 /*
314 * There are unresolved goto rules in the list, check if
315 * any of them are pointing to this new rule.
316 */
Denis V. Lunev76c72d42007-09-16 15:44:27 -0700317 list_for_each_entry(r, &ops->rules_list, list) {
Thomas Graf0947c9fe2007-03-26 17:14:15 -0700318 if (r->action == FR_ACT_GOTO &&
319 r->target == rule->pref) {
320 BUG_ON(r->ctarget != NULL);
321 rcu_assign_pointer(r->ctarget, rule);
322 if (--ops->unresolved_rules == 0)
323 break;
324 }
325 }
326 }
327
328 if (rule->action == FR_ACT_GOTO)
329 ops->nr_goto_rules++;
330
331 if (unresolved)
332 ops->unresolved_rules++;
333
Thomas Graf14c0b972006-08-04 03:38:38 -0700334 if (last)
335 list_add_rcu(&rule->list, &last->list);
336 else
Denis V. Lunev76c72d42007-09-16 15:44:27 -0700337 list_add_rcu(&rule->list, &ops->rules_list);
Thomas Graf14c0b972006-08-04 03:38:38 -0700338
Denis V. Lunev9e3a5482008-01-20 16:46:41 -0800339 notify_rule_change(RTM_NEWRULE, rule, ops, nlh, NETLINK_CB(skb).pid);
Thomas Graf73417f62007-03-27 13:56:52 -0700340 flush_route_cache(ops);
Thomas Graf14c0b972006-08-04 03:38:38 -0700341 rules_ops_put(ops);
342 return 0;
343
344errout_free:
345 kfree(rule);
346errout:
347 rules_ops_put(ops);
348 return err;
349}
350
Thomas Graf9d9e6a52007-03-25 23:20:05 -0700351static int fib_nl_delrule(struct sk_buff *skb, struct nlmsghdr* nlh, void *arg)
Thomas Graf14c0b972006-08-04 03:38:38 -0700352{
Denis V. Lunevb8542722007-12-01 00:21:31 +1100353 struct net *net = skb->sk->sk_net;
Thomas Graf14c0b972006-08-04 03:38:38 -0700354 struct fib_rule_hdr *frh = nlmsg_data(nlh);
355 struct fib_rules_ops *ops = NULL;
Thomas Graf0947c9fe2007-03-26 17:14:15 -0700356 struct fib_rule *rule, *tmp;
Thomas Graf14c0b972006-08-04 03:38:38 -0700357 struct nlattr *tb[FRA_MAX+1];
358 int err = -EINVAL;
359
360 if (nlh->nlmsg_len < nlmsg_msg_size(sizeof(*frh)))
361 goto errout;
362
Denis V. Lunev5fd30ee2008-01-10 03:20:28 -0800363 ops = lookup_rules_ops(net, frh->family);
Thomas Graf14c0b972006-08-04 03:38:38 -0700364 if (ops == NULL) {
365 err = EAFNOSUPPORT;
366 goto errout;
367 }
368
369 err = nlmsg_parse(nlh, sizeof(*frh), tb, FRA_MAX, ops->policy);
370 if (err < 0)
371 goto errout;
372
Thomas Grafe1701c62007-03-24 12:46:02 -0700373 err = validate_rulemsg(frh, tb, ops);
374 if (err < 0)
375 goto errout;
376
Denis V. Lunev76c72d42007-09-16 15:44:27 -0700377 list_for_each_entry(rule, &ops->rules_list, list) {
Thomas Graf14c0b972006-08-04 03:38:38 -0700378 if (frh->action && (frh->action != rule->action))
379 continue;
380
Patrick McHardy9e762a42006-08-10 23:09:48 -0700381 if (frh->table && (frh_get_table(frh, tb) != rule->table))
Thomas Graf14c0b972006-08-04 03:38:38 -0700382 continue;
383
384 if (tb[FRA_PRIORITY] &&
385 (rule->pref != nla_get_u32(tb[FRA_PRIORITY])))
386 continue;
387
388 if (tb[FRA_IFNAME] &&
389 nla_strcmp(tb[FRA_IFNAME], rule->ifname))
390 continue;
391
Thomas Grafb8964ed2006-11-09 15:22:18 -0800392 if (tb[FRA_FWMARK] &&
393 (rule->mark != nla_get_u32(tb[FRA_FWMARK])))
394 continue;
395
396 if (tb[FRA_FWMASK] &&
397 (rule->mark_mask != nla_get_u32(tb[FRA_FWMASK])))
398 continue;
399
Thomas Graf14c0b972006-08-04 03:38:38 -0700400 if (!ops->compare(rule, frh, tb))
401 continue;
402
403 if (rule->flags & FIB_RULE_PERMANENT) {
404 err = -EPERM;
405 goto errout;
406 }
407
408 list_del_rcu(&rule->list);
Thomas Graf0947c9fe2007-03-26 17:14:15 -0700409
410 if (rule->action == FR_ACT_GOTO)
411 ops->nr_goto_rules--;
412
413 /*
414 * Check if this rule is a target to any of them. If so,
415 * disable them. As this operation is eventually very
416 * expensive, it is only performed if goto rules have
417 * actually been added.
418 */
419 if (ops->nr_goto_rules > 0) {
Denis V. Lunev76c72d42007-09-16 15:44:27 -0700420 list_for_each_entry(tmp, &ops->rules_list, list) {
Thomas Graf0947c9fe2007-03-26 17:14:15 -0700421 if (tmp->ctarget == rule) {
422 rcu_assign_pointer(tmp->ctarget, NULL);
423 ops->unresolved_rules++;
424 }
425 }
426 }
427
Thomas Graf14c0b972006-08-04 03:38:38 -0700428 synchronize_rcu();
Denis V. Lunev9e3a5482008-01-20 16:46:41 -0800429 notify_rule_change(RTM_DELRULE, rule, ops, nlh,
Thomas Grafc17084d2006-08-15 00:32:48 -0700430 NETLINK_CB(skb).pid);
Thomas Graf14c0b972006-08-04 03:38:38 -0700431 fib_rule_put(rule);
Thomas Graf73417f62007-03-27 13:56:52 -0700432 flush_route_cache(ops);
Thomas Graf14c0b972006-08-04 03:38:38 -0700433 rules_ops_put(ops);
434 return 0;
435 }
436
437 err = -ENOENT;
438errout:
439 rules_ops_put(ops);
440 return err;
441}
442
Thomas Graf339bf982006-11-10 14:10:15 -0800443static inline size_t fib_rule_nlmsg_size(struct fib_rules_ops *ops,
444 struct fib_rule *rule)
445{
446 size_t payload = NLMSG_ALIGN(sizeof(struct fib_rule_hdr))
447 + nla_total_size(IFNAMSIZ) /* FRA_IFNAME */
448 + nla_total_size(4) /* FRA_PRIORITY */
449 + nla_total_size(4) /* FRA_TABLE */
450 + nla_total_size(4) /* FRA_FWMARK */
451 + nla_total_size(4); /* FRA_FWMASK */
452
453 if (ops->nlmsg_payload)
454 payload += ops->nlmsg_payload(rule);
455
456 return payload;
457}
458
Thomas Graf14c0b972006-08-04 03:38:38 -0700459static int fib_nl_fill_rule(struct sk_buff *skb, struct fib_rule *rule,
460 u32 pid, u32 seq, int type, int flags,
461 struct fib_rules_ops *ops)
462{
463 struct nlmsghdr *nlh;
464 struct fib_rule_hdr *frh;
465
466 nlh = nlmsg_put(skb, pid, seq, type, sizeof(*frh), flags);
467 if (nlh == NULL)
Patrick McHardy26932562007-01-31 23:16:40 -0800468 return -EMSGSIZE;
Thomas Graf14c0b972006-08-04 03:38:38 -0700469
470 frh = nlmsg_data(nlh);
471 frh->table = rule->table;
Patrick McHardy9e762a42006-08-10 23:09:48 -0700472 NLA_PUT_U32(skb, FRA_TABLE, rule->table);
Thomas Graf14c0b972006-08-04 03:38:38 -0700473 frh->res1 = 0;
474 frh->res2 = 0;
475 frh->action = rule->action;
476 frh->flags = rule->flags;
477
Thomas Graf0947c9fe2007-03-26 17:14:15 -0700478 if (rule->action == FR_ACT_GOTO && rule->ctarget == NULL)
479 frh->flags |= FIB_RULE_UNRESOLVED;
480
Thomas Graf2b443682007-03-26 17:37:59 -0700481 if (rule->ifname[0]) {
Thomas Graf14c0b972006-08-04 03:38:38 -0700482 NLA_PUT_STRING(skb, FRA_IFNAME, rule->ifname);
483
Thomas Graf2b443682007-03-26 17:37:59 -0700484 if (rule->ifindex == -1)
485 frh->flags |= FIB_RULE_DEV_DETACHED;
486 }
487
Thomas Graf14c0b972006-08-04 03:38:38 -0700488 if (rule->pref)
489 NLA_PUT_U32(skb, FRA_PRIORITY, rule->pref);
490
Thomas Grafb8964ed2006-11-09 15:22:18 -0800491 if (rule->mark)
492 NLA_PUT_U32(skb, FRA_FWMARK, rule->mark);
493
494 if (rule->mark_mask || rule->mark)
495 NLA_PUT_U32(skb, FRA_FWMASK, rule->mark_mask);
496
Thomas Graf0947c9fe2007-03-26 17:14:15 -0700497 if (rule->target)
498 NLA_PUT_U32(skb, FRA_GOTO, rule->target);
499
Thomas Graf14c0b972006-08-04 03:38:38 -0700500 if (ops->fill(rule, skb, nlh, frh) < 0)
501 goto nla_put_failure;
502
503 return nlmsg_end(skb, nlh);
504
505nla_put_failure:
Patrick McHardy26932562007-01-31 23:16:40 -0800506 nlmsg_cancel(skb, nlh);
507 return -EMSGSIZE;
Thomas Graf14c0b972006-08-04 03:38:38 -0700508}
509
Thomas Grafc4546732007-03-25 23:24:24 -0700510static int dump_rules(struct sk_buff *skb, struct netlink_callback *cb,
511 struct fib_rules_ops *ops)
Thomas Graf14c0b972006-08-04 03:38:38 -0700512{
513 int idx = 0;
514 struct fib_rule *rule;
Thomas Graf14c0b972006-08-04 03:38:38 -0700515
Denis V. Lunev76c72d42007-09-16 15:44:27 -0700516 list_for_each_entry(rule, &ops->rules_list, list) {
Thomas Grafc4546732007-03-25 23:24:24 -0700517 if (idx < cb->args[1])
Thomas Graf14c0b972006-08-04 03:38:38 -0700518 goto skip;
519
520 if (fib_nl_fill_rule(skb, rule, NETLINK_CB(cb->skb).pid,
521 cb->nlh->nlmsg_seq, RTM_NEWRULE,
522 NLM_F_MULTI, ops) < 0)
523 break;
524skip:
525 idx++;
526 }
Thomas Grafc4546732007-03-25 23:24:24 -0700527 cb->args[1] = idx;
Thomas Graf14c0b972006-08-04 03:38:38 -0700528 rules_ops_put(ops);
529
530 return skb->len;
531}
532
Thomas Grafc4546732007-03-25 23:24:24 -0700533static int fib_nl_dumprule(struct sk_buff *skb, struct netlink_callback *cb)
534{
Denis V. Lunevb8542722007-12-01 00:21:31 +1100535 struct net *net = skb->sk->sk_net;
Thomas Grafc4546732007-03-25 23:24:24 -0700536 struct fib_rules_ops *ops;
537 int idx = 0, family;
538
539 family = rtnl_msg_family(cb->nlh);
540 if (family != AF_UNSPEC) {
541 /* Protocol specific dump request */
Denis V. Lunev5fd30ee2008-01-10 03:20:28 -0800542 ops = lookup_rules_ops(net, family);
Thomas Grafc4546732007-03-25 23:24:24 -0700543 if (ops == NULL)
544 return -EAFNOSUPPORT;
545
546 return dump_rules(skb, cb, ops);
547 }
548
549 rcu_read_lock();
Denis V. Lunev5fd30ee2008-01-10 03:20:28 -0800550 list_for_each_entry_rcu(ops, &net->rules_ops, list) {
Thomas Grafc4546732007-03-25 23:24:24 -0700551 if (idx < cb->args[0] || !try_module_get(ops->owner))
552 goto skip;
553
554 if (dump_rules(skb, cb, ops) < 0)
555 break;
556
557 cb->args[1] = 0;
558 skip:
559 idx++;
560 }
561 rcu_read_unlock();
562 cb->args[0] = idx;
563
564 return skb->len;
565}
Thomas Graf14c0b972006-08-04 03:38:38 -0700566
Denis V. Lunev9e3a5482008-01-20 16:46:41 -0800567static void notify_rule_change(int event, struct fib_rule *rule,
Thomas Grafc17084d2006-08-15 00:32:48 -0700568 struct fib_rules_ops *ops, struct nlmsghdr *nlh,
569 u32 pid)
Thomas Graf14c0b972006-08-04 03:38:38 -0700570{
Denis V. Lunev9e3a5482008-01-20 16:46:41 -0800571 struct net *net;
Thomas Grafc17084d2006-08-15 00:32:48 -0700572 struct sk_buff *skb;
573 int err = -ENOBUFS;
Thomas Graf14c0b972006-08-04 03:38:38 -0700574
Denis V. Lunev9e3a5482008-01-20 16:46:41 -0800575 net = ops->fro_net;
Thomas Graf339bf982006-11-10 14:10:15 -0800576 skb = nlmsg_new(fib_rule_nlmsg_size(ops, rule), GFP_KERNEL);
Thomas Graf14c0b972006-08-04 03:38:38 -0700577 if (skb == NULL)
Thomas Grafc17084d2006-08-15 00:32:48 -0700578 goto errout;
579
580 err = fib_nl_fill_rule(skb, rule, pid, nlh->nlmsg_seq, event, 0, ops);
Patrick McHardy26932562007-01-31 23:16:40 -0800581 if (err < 0) {
582 /* -EMSGSIZE implies BUG in fib_rule_nlmsg_size() */
583 WARN_ON(err == -EMSGSIZE);
584 kfree_skb(skb);
585 goto errout;
586 }
Denis V. Lunev9e3a5482008-01-20 16:46:41 -0800587
Denis V. Lunev5fd30ee2008-01-10 03:20:28 -0800588 err = rtnl_notify(skb, net, pid, ops->nlgroup, nlh, GFP_KERNEL);
Thomas Grafc17084d2006-08-15 00:32:48 -0700589errout:
590 if (err < 0)
Denis V. Lunev5fd30ee2008-01-10 03:20:28 -0800591 rtnl_set_sk_err(net, ops->nlgroup, err);
Thomas Graf14c0b972006-08-04 03:38:38 -0700592}
593
594static void attach_rules(struct list_head *rules, struct net_device *dev)
595{
596 struct fib_rule *rule;
597
598 list_for_each_entry(rule, rules, list) {
599 if (rule->ifindex == -1 &&
600 strcmp(dev->name, rule->ifname) == 0)
601 rule->ifindex = dev->ifindex;
602 }
603}
604
605static void detach_rules(struct list_head *rules, struct net_device *dev)
606{
607 struct fib_rule *rule;
608
609 list_for_each_entry(rule, rules, list)
610 if (rule->ifindex == dev->ifindex)
611 rule->ifindex = -1;
612}
613
614
615static int fib_rules_event(struct notifier_block *this, unsigned long event,
616 void *ptr)
617{
618 struct net_device *dev = ptr;
Denis V. Lunev5fd30ee2008-01-10 03:20:28 -0800619 struct net *net = dev->nd_net;
Thomas Graf14c0b972006-08-04 03:38:38 -0700620 struct fib_rules_ops *ops;
621
622 ASSERT_RTNL();
623 rcu_read_lock();
624
625 switch (event) {
626 case NETDEV_REGISTER:
Denis V. Lunev5fd30ee2008-01-10 03:20:28 -0800627 list_for_each_entry(ops, &net->rules_ops, list)
Denis V. Lunev76c72d42007-09-16 15:44:27 -0700628 attach_rules(&ops->rules_list, dev);
Thomas Graf14c0b972006-08-04 03:38:38 -0700629 break;
630
631 case NETDEV_UNREGISTER:
Denis V. Lunev5fd30ee2008-01-10 03:20:28 -0800632 list_for_each_entry(ops, &net->rules_ops, list)
Denis V. Lunev76c72d42007-09-16 15:44:27 -0700633 detach_rules(&ops->rules_list, dev);
Thomas Graf14c0b972006-08-04 03:38:38 -0700634 break;
635 }
636
637 rcu_read_unlock();
638
639 return NOTIFY_DONE;
640}
641
642static struct notifier_block fib_rules_notifier = {
643 .notifier_call = fib_rules_event,
644};
645
Denis V. Lunev5fd30ee2008-01-10 03:20:28 -0800646static int fib_rules_net_init(struct net *net)
647{
648 INIT_LIST_HEAD(&net->rules_ops);
649 spin_lock_init(&net->rules_mod_lock);
650 return 0;
651}
652
653static struct pernet_operations fib_rules_net_ops = {
654 .init = fib_rules_net_init,
655};
656
Thomas Graf14c0b972006-08-04 03:38:38 -0700657static int __init fib_rules_init(void)
658{
Denis V. Lunev5fd30ee2008-01-10 03:20:28 -0800659 int err;
Thomas Graf9d9e6a52007-03-25 23:20:05 -0700660 rtnl_register(PF_UNSPEC, RTM_NEWRULE, fib_nl_newrule, NULL);
661 rtnl_register(PF_UNSPEC, RTM_DELRULE, fib_nl_delrule, NULL);
Thomas Grafc4546732007-03-25 23:24:24 -0700662 rtnl_register(PF_UNSPEC, RTM_GETRULE, NULL, fib_nl_dumprule);
Thomas Graf9d9e6a52007-03-25 23:20:05 -0700663
Denis V. Lunev5fd30ee2008-01-10 03:20:28 -0800664 err = register_netdevice_notifier(&fib_rules_notifier);
665 if (err < 0)
666 goto fail;
667
668 err = register_pernet_subsys(&fib_rules_net_ops);
669 if (err < 0)
670 goto fail_unregister;
671 return 0;
672
673fail_unregister:
674 unregister_netdevice_notifier(&fib_rules_notifier);
675fail:
676 rtnl_unregister(PF_UNSPEC, RTM_NEWRULE);
677 rtnl_unregister(PF_UNSPEC, RTM_DELRULE);
678 rtnl_unregister(PF_UNSPEC, RTM_GETRULE);
679 return err;
Thomas Graf14c0b972006-08-04 03:38:38 -0700680}
681
682subsys_initcall(fib_rules_init);