blob: 2ff34894357a3c7f4d1f33cd39c15f1a3a426463 [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;
Denis V. Lunev3661a912008-04-16 02:01:56 -070032 r->fr_net = hold_net(ops->fro_net);
Denis V. Lunev2994c632007-11-10 22:12:03 -080033
34 /* The lock is not required here, the list in unreacheable
35 * at the moment this function is called */
36 list_add_tail(&r->list, &ops->rules_list);
37 return 0;
38}
39EXPORT_SYMBOL(fib_default_rule_add);
40
Denis V. Lunev9e3a5482008-01-20 16:46:41 -080041static void notify_rule_change(int event, struct fib_rule *rule,
Thomas Grafc17084d2006-08-15 00:32:48 -070042 struct fib_rules_ops *ops, struct nlmsghdr *nlh,
43 u32 pid);
Thomas Graf14c0b972006-08-04 03:38:38 -070044
Denis V. Lunev5fd30ee2008-01-10 03:20:28 -080045static struct fib_rules_ops *lookup_rules_ops(struct net *net, int family)
Thomas Graf14c0b972006-08-04 03:38:38 -070046{
47 struct fib_rules_ops *ops;
48
49 rcu_read_lock();
Denis V. Lunev5fd30ee2008-01-10 03:20:28 -080050 list_for_each_entry_rcu(ops, &net->rules_ops, list) {
Thomas Graf14c0b972006-08-04 03:38:38 -070051 if (ops->family == family) {
52 if (!try_module_get(ops->owner))
53 ops = NULL;
54 rcu_read_unlock();
55 return ops;
56 }
57 }
58 rcu_read_unlock();
59
60 return NULL;
61}
62
63static void rules_ops_put(struct fib_rules_ops *ops)
64{
65 if (ops)
66 module_put(ops->owner);
67}
68
Thomas Graf73417f62007-03-27 13:56:52 -070069static void flush_route_cache(struct fib_rules_ops *ops)
70{
71 if (ops->flush_cache)
Denis V. Lunevae299fc2008-07-05 19:01:28 -070072 ops->flush_cache(ops);
Thomas Graf73417f62007-03-27 13:56:52 -070073}
74
Eric W. Biedermane9c51582009-12-03 12:22:55 -080075static int __fib_rules_register(struct fib_rules_ops *ops)
Thomas Graf14c0b972006-08-04 03:38:38 -070076{
77 int err = -EEXIST;
78 struct fib_rules_ops *o;
Denis V. Lunev9e3a5482008-01-20 16:46:41 -080079 struct net *net;
80
81 net = ops->fro_net;
Thomas Graf14c0b972006-08-04 03:38:38 -070082
83 if (ops->rule_size < sizeof(struct fib_rule))
84 return -EINVAL;
85
86 if (ops->match == NULL || ops->configure == NULL ||
87 ops->compare == NULL || ops->fill == NULL ||
88 ops->action == NULL)
89 return -EINVAL;
90
Denis V. Lunev5fd30ee2008-01-10 03:20:28 -080091 spin_lock(&net->rules_mod_lock);
92 list_for_each_entry(o, &net->rules_ops, list)
Thomas Graf14c0b972006-08-04 03:38:38 -070093 if (ops->family == o->family)
94 goto errout;
95
Denis V. Lunev5fd30ee2008-01-10 03:20:28 -080096 hold_net(net);
97 list_add_tail_rcu(&ops->list, &net->rules_ops);
Thomas Graf14c0b972006-08-04 03:38:38 -070098 err = 0;
99errout:
Denis V. Lunev5fd30ee2008-01-10 03:20:28 -0800100 spin_unlock(&net->rules_mod_lock);
Thomas Graf14c0b972006-08-04 03:38:38 -0700101
102 return err;
103}
104
Eric W. Biedermane9c51582009-12-03 12:22:55 -0800105struct fib_rules_ops *
106fib_rules_register(struct fib_rules_ops *tmpl, struct net *net)
107{
108 struct fib_rules_ops *ops;
109 int err;
110
Eric Dumazet2fb35732010-03-09 20:03:38 +0000111 ops = kmemdup(tmpl, sizeof(*ops), GFP_KERNEL);
Eric W. Biedermane9c51582009-12-03 12:22:55 -0800112 if (ops == NULL)
113 return ERR_PTR(-ENOMEM);
114
115 INIT_LIST_HEAD(&ops->rules_list);
116 ops->fro_net = net;
117
118 err = __fib_rules_register(ops);
119 if (err) {
120 kfree(ops);
121 ops = ERR_PTR(err);
122 }
123
124 return ops;
125}
Thomas Graf14c0b972006-08-04 03:38:38 -0700126EXPORT_SYMBOL_GPL(fib_rules_register);
127
Daniel Lezcano9eb87f32007-12-07 00:42:52 -0800128void fib_rules_cleanup_ops(struct fib_rules_ops *ops)
Thomas Graf14c0b972006-08-04 03:38:38 -0700129{
130 struct fib_rule *rule, *tmp;
131
Denis V. Lunev76c72d42007-09-16 15:44:27 -0700132 list_for_each_entry_safe(rule, tmp, &ops->rules_list, list) {
Thomas Graf14c0b972006-08-04 03:38:38 -0700133 list_del_rcu(&rule->list);
134 fib_rule_put(rule);
135 }
136}
Daniel Lezcano9eb87f32007-12-07 00:42:52 -0800137EXPORT_SYMBOL_GPL(fib_rules_cleanup_ops);
Thomas Graf14c0b972006-08-04 03:38:38 -0700138
Eric W. Biedermane9c51582009-12-03 12:22:55 -0800139static void fib_rules_put_rcu(struct rcu_head *head)
140{
141 struct fib_rules_ops *ops = container_of(head, struct fib_rules_ops, rcu);
142 struct net *net = ops->fro_net;
143
144 release_net(net);
145 kfree(ops);
146}
147
Denis V. Lunev9e3a5482008-01-20 16:46:41 -0800148void fib_rules_unregister(struct fib_rules_ops *ops)
Thomas Graf14c0b972006-08-04 03:38:38 -0700149{
Denis V. Lunev9e3a5482008-01-20 16:46:41 -0800150 struct net *net = ops->fro_net;
Thomas Graf14c0b972006-08-04 03:38:38 -0700151
Denis V. Lunev5fd30ee2008-01-10 03:20:28 -0800152 spin_lock(&net->rules_mod_lock);
Denis V. Lunev72132c1b2008-01-14 22:59:30 -0800153 list_del_rcu(&ops->list);
154 fib_rules_cleanup_ops(ops);
Denis V. Lunev5fd30ee2008-01-10 03:20:28 -0800155 spin_unlock(&net->rules_mod_lock);
Thomas Graf14c0b972006-08-04 03:38:38 -0700156
Eric W. Biedermane9c51582009-12-03 12:22:55 -0800157 call_rcu(&ops->rcu, fib_rules_put_rcu);
Thomas Graf14c0b972006-08-04 03:38:38 -0700158}
Thomas Graf14c0b972006-08-04 03:38:38 -0700159EXPORT_SYMBOL_GPL(fib_rules_unregister);
160
Thomas Graf3dfbcc42006-11-09 15:23:20 -0800161static int fib_rule_match(struct fib_rule *rule, struct fib_rules_ops *ops,
162 struct flowi *fl, int flags)
163{
164 int ret = 0;
165
Patrick McHardy491deb22009-12-03 01:25:54 +0000166 if (rule->iifindex && (rule->iifindex != fl->iif))
Thomas Graf3dfbcc42006-11-09 15:23:20 -0800167 goto out;
168
Patrick McHardy1b038a52009-12-03 01:25:56 +0000169 if (rule->oifindex && (rule->oifindex != fl->oif))
170 goto out;
171
Thomas Graf3dfbcc42006-11-09 15:23:20 -0800172 if ((rule->mark ^ fl->mark) & rule->mark_mask)
173 goto out;
174
175 ret = ops->match(rule, fl, flags);
176out:
177 return (rule->flags & FIB_RULE_INVERT) ? !ret : ret;
178}
179
Thomas Graf14c0b972006-08-04 03:38:38 -0700180int fib_rules_lookup(struct fib_rules_ops *ops, struct flowi *fl,
181 int flags, struct fib_lookup_arg *arg)
182{
183 struct fib_rule *rule;
184 int err;
185
186 rcu_read_lock();
187
Denis V. Lunev76c72d42007-09-16 15:44:27 -0700188 list_for_each_entry_rcu(rule, &ops->rules_list, list) {
Thomas Graf0947c9fe2007-03-26 17:14:15 -0700189jumped:
Thomas Graf3dfbcc42006-11-09 15:23:20 -0800190 if (!fib_rule_match(rule, ops, fl, flags))
Thomas Graf14c0b972006-08-04 03:38:38 -0700191 continue;
192
Thomas Graf0947c9fe2007-03-26 17:14:15 -0700193 if (rule->action == FR_ACT_GOTO) {
194 struct fib_rule *target;
195
196 target = rcu_dereference(rule->ctarget);
197 if (target == NULL) {
198 continue;
199 } else {
200 rule = target;
201 goto jumped;
202 }
Thomas Graffa0b2d12007-03-26 17:38:53 -0700203 } else if (rule->action == FR_ACT_NOP)
204 continue;
205 else
Thomas Graf0947c9fe2007-03-26 17:14:15 -0700206 err = ops->action(rule, fl, flags, arg);
207
Thomas Graf14c0b972006-08-04 03:38:38 -0700208 if (err != -EAGAIN) {
209 fib_rule_get(rule);
210 arg->rule = rule;
211 goto out;
212 }
213 }
214
Steven Whitehouse83886b62007-03-30 13:34:27 -0700215 err = -ESRCH;
Thomas Graf14c0b972006-08-04 03:38:38 -0700216out:
217 rcu_read_unlock();
218
219 return err;
220}
Thomas Graf14c0b972006-08-04 03:38:38 -0700221EXPORT_SYMBOL_GPL(fib_rules_lookup);
222
Thomas Grafe1701c62007-03-24 12:46:02 -0700223static int validate_rulemsg(struct fib_rule_hdr *frh, struct nlattr **tb,
224 struct fib_rules_ops *ops)
225{
226 int err = -EINVAL;
227
228 if (frh->src_len)
229 if (tb[FRA_SRC] == NULL ||
230 frh->src_len > (ops->addr_size * 8) ||
231 nla_len(tb[FRA_SRC]) != ops->addr_size)
232 goto errout;
233
234 if (frh->dst_len)
235 if (tb[FRA_DST] == NULL ||
236 frh->dst_len > (ops->addr_size * 8) ||
237 nla_len(tb[FRA_DST]) != ops->addr_size)
238 goto errout;
239
240 err = 0;
241errout:
242 return err;
243}
244
Thomas Graf9d9e6a52007-03-25 23:20:05 -0700245static int fib_nl_newrule(struct sk_buff *skb, struct nlmsghdr* nlh, void *arg)
Thomas Graf14c0b972006-08-04 03:38:38 -0700246{
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +0900247 struct net *net = sock_net(skb->sk);
Thomas Graf14c0b972006-08-04 03:38:38 -0700248 struct fib_rule_hdr *frh = nlmsg_data(nlh);
249 struct fib_rules_ops *ops = NULL;
250 struct fib_rule *rule, *r, *last = NULL;
251 struct nlattr *tb[FRA_MAX+1];
Thomas Graf0947c9fe2007-03-26 17:14:15 -0700252 int err = -EINVAL, unresolved = 0;
Thomas Graf14c0b972006-08-04 03:38:38 -0700253
254 if (nlh->nlmsg_len < nlmsg_msg_size(sizeof(*frh)))
255 goto errout;
256
Denis V. Lunev5fd30ee2008-01-10 03:20:28 -0800257 ops = lookup_rules_ops(net, frh->family);
Thomas Graf14c0b972006-08-04 03:38:38 -0700258 if (ops == NULL) {
Patrick McHardy2fe195c2008-07-01 19:59:37 -0700259 err = -EAFNOSUPPORT;
Thomas Graf14c0b972006-08-04 03:38:38 -0700260 goto errout;
261 }
262
263 err = nlmsg_parse(nlh, sizeof(*frh), tb, FRA_MAX, ops->policy);
264 if (err < 0)
265 goto errout;
266
Thomas Grafe1701c62007-03-24 12:46:02 -0700267 err = validate_rulemsg(frh, tb, ops);
268 if (err < 0)
269 goto errout;
270
Thomas Graf14c0b972006-08-04 03:38:38 -0700271 rule = kzalloc(ops->rule_size, GFP_KERNEL);
272 if (rule == NULL) {
273 err = -ENOMEM;
274 goto errout;
275 }
Denis V. Lunev3661a912008-04-16 02:01:56 -0700276 rule->fr_net = hold_net(net);
Thomas Graf14c0b972006-08-04 03:38:38 -0700277
278 if (tb[FRA_PRIORITY])
279 rule->pref = nla_get_u32(tb[FRA_PRIORITY]);
280
Patrick McHardy491deb22009-12-03 01:25:54 +0000281 if (tb[FRA_IIFNAME]) {
Thomas Graf14c0b972006-08-04 03:38:38 -0700282 struct net_device *dev;
283
Patrick McHardy491deb22009-12-03 01:25:54 +0000284 rule->iifindex = -1;
285 nla_strlcpy(rule->iifname, tb[FRA_IIFNAME], IFNAMSIZ);
286 dev = __dev_get_by_name(net, rule->iifname);
Thomas Graf14c0b972006-08-04 03:38:38 -0700287 if (dev)
Patrick McHardy491deb22009-12-03 01:25:54 +0000288 rule->iifindex = dev->ifindex;
Thomas Graf14c0b972006-08-04 03:38:38 -0700289 }
290
Patrick McHardy1b038a52009-12-03 01:25:56 +0000291 if (tb[FRA_OIFNAME]) {
292 struct net_device *dev;
293
294 rule->oifindex = -1;
295 nla_strlcpy(rule->oifname, tb[FRA_OIFNAME], IFNAMSIZ);
296 dev = __dev_get_by_name(net, rule->oifname);
297 if (dev)
298 rule->oifindex = dev->ifindex;
299 }
300
Thomas Grafb8964ed2006-11-09 15:22:18 -0800301 if (tb[FRA_FWMARK]) {
302 rule->mark = nla_get_u32(tb[FRA_FWMARK]);
303 if (rule->mark)
304 /* compatibility: if the mark value is non-zero all bits
305 * are compared unless a mask is explicitly specified.
306 */
307 rule->mark_mask = 0xFFFFFFFF;
308 }
309
310 if (tb[FRA_FWMASK])
311 rule->mark_mask = nla_get_u32(tb[FRA_FWMASK]);
312
Thomas Graf14c0b972006-08-04 03:38:38 -0700313 rule->action = frh->action;
314 rule->flags = frh->flags;
Patrick McHardy9e762a42006-08-10 23:09:48 -0700315 rule->table = frh_get_table(frh, tb);
Thomas Graf14c0b972006-08-04 03:38:38 -0700316
Patrick McHardy5adef182009-12-03 01:25:57 +0000317 if (!tb[FRA_PRIORITY] && ops->default_pref)
Denis V. Lunev868d13a2008-01-10 03:18:25 -0800318 rule->pref = ops->default_pref(ops);
Thomas Graf14c0b972006-08-04 03:38:38 -0700319
Thomas Graf0947c9fe2007-03-26 17:14:15 -0700320 err = -EINVAL;
321 if (tb[FRA_GOTO]) {
322 if (rule->action != FR_ACT_GOTO)
323 goto errout_free;
324
325 rule->target = nla_get_u32(tb[FRA_GOTO]);
326 /* Backward jumps are prohibited to avoid endless loops */
327 if (rule->target <= rule->pref)
328 goto errout_free;
329
Denis V. Lunev76c72d42007-09-16 15:44:27 -0700330 list_for_each_entry(r, &ops->rules_list, list) {
Thomas Graf0947c9fe2007-03-26 17:14:15 -0700331 if (r->pref == rule->target) {
332 rule->ctarget = r;
333 break;
334 }
335 }
336
337 if (rule->ctarget == NULL)
338 unresolved = 1;
339 } else if (rule->action == FR_ACT_GOTO)
340 goto errout_free;
341
Rami Rosen8b3521e2009-05-11 05:52:49 +0000342 err = ops->configure(rule, skb, frh, tb);
Thomas Graf14c0b972006-08-04 03:38:38 -0700343 if (err < 0)
344 goto errout_free;
345
Denis V. Lunev76c72d42007-09-16 15:44:27 -0700346 list_for_each_entry(r, &ops->rules_list, list) {
Thomas Graf14c0b972006-08-04 03:38:38 -0700347 if (r->pref > rule->pref)
348 break;
349 last = r;
350 }
351
352 fib_rule_get(rule);
353
Thomas Graf0947c9fe2007-03-26 17:14:15 -0700354 if (ops->unresolved_rules) {
355 /*
356 * There are unresolved goto rules in the list, check if
357 * any of them are pointing to this new rule.
358 */
Denis V. Lunev76c72d42007-09-16 15:44:27 -0700359 list_for_each_entry(r, &ops->rules_list, list) {
Thomas Graf0947c9fe2007-03-26 17:14:15 -0700360 if (r->action == FR_ACT_GOTO &&
361 r->target == rule->pref) {
362 BUG_ON(r->ctarget != NULL);
363 rcu_assign_pointer(r->ctarget, rule);
364 if (--ops->unresolved_rules == 0)
365 break;
366 }
367 }
368 }
369
370 if (rule->action == FR_ACT_GOTO)
371 ops->nr_goto_rules++;
372
373 if (unresolved)
374 ops->unresolved_rules++;
375
Thomas Graf14c0b972006-08-04 03:38:38 -0700376 if (last)
377 list_add_rcu(&rule->list, &last->list);
378 else
Denis V. Lunev76c72d42007-09-16 15:44:27 -0700379 list_add_rcu(&rule->list, &ops->rules_list);
Thomas Graf14c0b972006-08-04 03:38:38 -0700380
Denis V. Lunev9e3a5482008-01-20 16:46:41 -0800381 notify_rule_change(RTM_NEWRULE, rule, ops, nlh, NETLINK_CB(skb).pid);
Thomas Graf73417f62007-03-27 13:56:52 -0700382 flush_route_cache(ops);
Thomas Graf14c0b972006-08-04 03:38:38 -0700383 rules_ops_put(ops);
384 return 0;
385
386errout_free:
Denis V. Lunev3661a912008-04-16 02:01:56 -0700387 release_net(rule->fr_net);
Thomas Graf14c0b972006-08-04 03:38:38 -0700388 kfree(rule);
389errout:
390 rules_ops_put(ops);
391 return err;
392}
393
Thomas Graf9d9e6a52007-03-25 23:20:05 -0700394static int fib_nl_delrule(struct sk_buff *skb, struct nlmsghdr* nlh, void *arg)
Thomas Graf14c0b972006-08-04 03:38:38 -0700395{
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +0900396 struct net *net = sock_net(skb->sk);
Thomas Graf14c0b972006-08-04 03:38:38 -0700397 struct fib_rule_hdr *frh = nlmsg_data(nlh);
398 struct fib_rules_ops *ops = NULL;
Thomas Graf0947c9fe2007-03-26 17:14:15 -0700399 struct fib_rule *rule, *tmp;
Thomas Graf14c0b972006-08-04 03:38:38 -0700400 struct nlattr *tb[FRA_MAX+1];
401 int err = -EINVAL;
402
403 if (nlh->nlmsg_len < nlmsg_msg_size(sizeof(*frh)))
404 goto errout;
405
Denis V. Lunev5fd30ee2008-01-10 03:20:28 -0800406 ops = lookup_rules_ops(net, frh->family);
Thomas Graf14c0b972006-08-04 03:38:38 -0700407 if (ops == NULL) {
Patrick McHardy2fe195c2008-07-01 19:59:37 -0700408 err = -EAFNOSUPPORT;
Thomas Graf14c0b972006-08-04 03:38:38 -0700409 goto errout;
410 }
411
412 err = nlmsg_parse(nlh, sizeof(*frh), tb, FRA_MAX, ops->policy);
413 if (err < 0)
414 goto errout;
415
Thomas Grafe1701c62007-03-24 12:46:02 -0700416 err = validate_rulemsg(frh, tb, ops);
417 if (err < 0)
418 goto errout;
419
Denis V. Lunev76c72d42007-09-16 15:44:27 -0700420 list_for_each_entry(rule, &ops->rules_list, list) {
Thomas Graf14c0b972006-08-04 03:38:38 -0700421 if (frh->action && (frh->action != rule->action))
422 continue;
423
Patrick McHardy9e762a42006-08-10 23:09:48 -0700424 if (frh->table && (frh_get_table(frh, tb) != rule->table))
Thomas Graf14c0b972006-08-04 03:38:38 -0700425 continue;
426
427 if (tb[FRA_PRIORITY] &&
428 (rule->pref != nla_get_u32(tb[FRA_PRIORITY])))
429 continue;
430
Patrick McHardy491deb22009-12-03 01:25:54 +0000431 if (tb[FRA_IIFNAME] &&
432 nla_strcmp(tb[FRA_IIFNAME], rule->iifname))
Thomas Graf14c0b972006-08-04 03:38:38 -0700433 continue;
434
Patrick McHardy1b038a52009-12-03 01:25:56 +0000435 if (tb[FRA_OIFNAME] &&
436 nla_strcmp(tb[FRA_OIFNAME], rule->oifname))
437 continue;
438
Thomas Grafb8964ed2006-11-09 15:22:18 -0800439 if (tb[FRA_FWMARK] &&
440 (rule->mark != nla_get_u32(tb[FRA_FWMARK])))
441 continue;
442
443 if (tb[FRA_FWMASK] &&
444 (rule->mark_mask != nla_get_u32(tb[FRA_FWMASK])))
445 continue;
446
Thomas Graf14c0b972006-08-04 03:38:38 -0700447 if (!ops->compare(rule, frh, tb))
448 continue;
449
450 if (rule->flags & FIB_RULE_PERMANENT) {
451 err = -EPERM;
452 goto errout;
453 }
454
455 list_del_rcu(&rule->list);
Thomas Graf0947c9fe2007-03-26 17:14:15 -0700456
457 if (rule->action == FR_ACT_GOTO)
458 ops->nr_goto_rules--;
459
460 /*
461 * Check if this rule is a target to any of them. If so,
462 * disable them. As this operation is eventually very
463 * expensive, it is only performed if goto rules have
464 * actually been added.
465 */
466 if (ops->nr_goto_rules > 0) {
Denis V. Lunev76c72d42007-09-16 15:44:27 -0700467 list_for_each_entry(tmp, &ops->rules_list, list) {
Thomas Graf0947c9fe2007-03-26 17:14:15 -0700468 if (tmp->ctarget == rule) {
469 rcu_assign_pointer(tmp->ctarget, NULL);
470 ops->unresolved_rules++;
471 }
472 }
473 }
474
Thomas Graf14c0b972006-08-04 03:38:38 -0700475 synchronize_rcu();
Denis V. Lunev9e3a5482008-01-20 16:46:41 -0800476 notify_rule_change(RTM_DELRULE, rule, ops, nlh,
Thomas Grafc17084d2006-08-15 00:32:48 -0700477 NETLINK_CB(skb).pid);
Thomas Graf14c0b972006-08-04 03:38:38 -0700478 fib_rule_put(rule);
Thomas Graf73417f62007-03-27 13:56:52 -0700479 flush_route_cache(ops);
Thomas Graf14c0b972006-08-04 03:38:38 -0700480 rules_ops_put(ops);
481 return 0;
482 }
483
484 err = -ENOENT;
485errout:
486 rules_ops_put(ops);
487 return err;
488}
489
Thomas Graf339bf982006-11-10 14:10:15 -0800490static inline size_t fib_rule_nlmsg_size(struct fib_rules_ops *ops,
491 struct fib_rule *rule)
492{
493 size_t payload = NLMSG_ALIGN(sizeof(struct fib_rule_hdr))
Patrick McHardy491deb22009-12-03 01:25:54 +0000494 + nla_total_size(IFNAMSIZ) /* FRA_IIFNAME */
Patrick McHardy1b038a52009-12-03 01:25:56 +0000495 + nla_total_size(IFNAMSIZ) /* FRA_OIFNAME */
Thomas Graf339bf982006-11-10 14:10:15 -0800496 + nla_total_size(4) /* FRA_PRIORITY */
497 + nla_total_size(4) /* FRA_TABLE */
498 + nla_total_size(4) /* FRA_FWMARK */
499 + nla_total_size(4); /* FRA_FWMASK */
500
501 if (ops->nlmsg_payload)
502 payload += ops->nlmsg_payload(rule);
503
504 return payload;
505}
506
Thomas Graf14c0b972006-08-04 03:38:38 -0700507static int fib_nl_fill_rule(struct sk_buff *skb, struct fib_rule *rule,
508 u32 pid, u32 seq, int type, int flags,
509 struct fib_rules_ops *ops)
510{
511 struct nlmsghdr *nlh;
512 struct fib_rule_hdr *frh;
513
514 nlh = nlmsg_put(skb, pid, seq, type, sizeof(*frh), flags);
515 if (nlh == NULL)
Patrick McHardy26932562007-01-31 23:16:40 -0800516 return -EMSGSIZE;
Thomas Graf14c0b972006-08-04 03:38:38 -0700517
518 frh = nlmsg_data(nlh);
519 frh->table = rule->table;
Patrick McHardy9e762a42006-08-10 23:09:48 -0700520 NLA_PUT_U32(skb, FRA_TABLE, rule->table);
Thomas Graf14c0b972006-08-04 03:38:38 -0700521 frh->res1 = 0;
522 frh->res2 = 0;
523 frh->action = rule->action;
524 frh->flags = rule->flags;
525
Thomas Graf0947c9fe2007-03-26 17:14:15 -0700526 if (rule->action == FR_ACT_GOTO && rule->ctarget == NULL)
527 frh->flags |= FIB_RULE_UNRESOLVED;
528
Patrick McHardy491deb22009-12-03 01:25:54 +0000529 if (rule->iifname[0]) {
530 NLA_PUT_STRING(skb, FRA_IIFNAME, rule->iifname);
Thomas Graf14c0b972006-08-04 03:38:38 -0700531
Patrick McHardy491deb22009-12-03 01:25:54 +0000532 if (rule->iifindex == -1)
533 frh->flags |= FIB_RULE_IIF_DETACHED;
Thomas Graf2b443682007-03-26 17:37:59 -0700534 }
535
Patrick McHardy1b038a52009-12-03 01:25:56 +0000536 if (rule->oifname[0]) {
537 NLA_PUT_STRING(skb, FRA_OIFNAME, rule->oifname);
538
539 if (rule->oifindex == -1)
540 frh->flags |= FIB_RULE_OIF_DETACHED;
541 }
542
Thomas Graf14c0b972006-08-04 03:38:38 -0700543 if (rule->pref)
544 NLA_PUT_U32(skb, FRA_PRIORITY, rule->pref);
545
Thomas Grafb8964ed2006-11-09 15:22:18 -0800546 if (rule->mark)
547 NLA_PUT_U32(skb, FRA_FWMARK, rule->mark);
548
549 if (rule->mark_mask || rule->mark)
550 NLA_PUT_U32(skb, FRA_FWMASK, rule->mark_mask);
551
Thomas Graf0947c9fe2007-03-26 17:14:15 -0700552 if (rule->target)
553 NLA_PUT_U32(skb, FRA_GOTO, rule->target);
554
Rami Rosen04af8cf2009-05-20 17:26:23 -0700555 if (ops->fill(rule, skb, frh) < 0)
Thomas Graf14c0b972006-08-04 03:38:38 -0700556 goto nla_put_failure;
557
558 return nlmsg_end(skb, nlh);
559
560nla_put_failure:
Patrick McHardy26932562007-01-31 23:16:40 -0800561 nlmsg_cancel(skb, nlh);
562 return -EMSGSIZE;
Thomas Graf14c0b972006-08-04 03:38:38 -0700563}
564
Thomas Grafc4546732007-03-25 23:24:24 -0700565static int dump_rules(struct sk_buff *skb, struct netlink_callback *cb,
566 struct fib_rules_ops *ops)
Thomas Graf14c0b972006-08-04 03:38:38 -0700567{
568 int idx = 0;
569 struct fib_rule *rule;
Thomas Graf14c0b972006-08-04 03:38:38 -0700570
Denis V. Lunev76c72d42007-09-16 15:44:27 -0700571 list_for_each_entry(rule, &ops->rules_list, list) {
Thomas Grafc4546732007-03-25 23:24:24 -0700572 if (idx < cb->args[1])
Thomas Graf14c0b972006-08-04 03:38:38 -0700573 goto skip;
574
575 if (fib_nl_fill_rule(skb, rule, NETLINK_CB(cb->skb).pid,
576 cb->nlh->nlmsg_seq, RTM_NEWRULE,
577 NLM_F_MULTI, ops) < 0)
578 break;
579skip:
580 idx++;
581 }
Thomas Grafc4546732007-03-25 23:24:24 -0700582 cb->args[1] = idx;
Thomas Graf14c0b972006-08-04 03:38:38 -0700583 rules_ops_put(ops);
584
585 return skb->len;
586}
587
Thomas Grafc4546732007-03-25 23:24:24 -0700588static int fib_nl_dumprule(struct sk_buff *skb, struct netlink_callback *cb)
589{
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +0900590 struct net *net = sock_net(skb->sk);
Thomas Grafc4546732007-03-25 23:24:24 -0700591 struct fib_rules_ops *ops;
592 int idx = 0, family;
593
594 family = rtnl_msg_family(cb->nlh);
595 if (family != AF_UNSPEC) {
596 /* Protocol specific dump request */
Denis V. Lunev5fd30ee2008-01-10 03:20:28 -0800597 ops = lookup_rules_ops(net, family);
Thomas Grafc4546732007-03-25 23:24:24 -0700598 if (ops == NULL)
599 return -EAFNOSUPPORT;
600
601 return dump_rules(skb, cb, ops);
602 }
603
604 rcu_read_lock();
Denis V. Lunev5fd30ee2008-01-10 03:20:28 -0800605 list_for_each_entry_rcu(ops, &net->rules_ops, list) {
Thomas Grafc4546732007-03-25 23:24:24 -0700606 if (idx < cb->args[0] || !try_module_get(ops->owner))
607 goto skip;
608
609 if (dump_rules(skb, cb, ops) < 0)
610 break;
611
612 cb->args[1] = 0;
Eric Dumazet2fb35732010-03-09 20:03:38 +0000613skip:
Thomas Grafc4546732007-03-25 23:24:24 -0700614 idx++;
615 }
616 rcu_read_unlock();
617 cb->args[0] = idx;
618
619 return skb->len;
620}
Thomas Graf14c0b972006-08-04 03:38:38 -0700621
Denis V. Lunev9e3a5482008-01-20 16:46:41 -0800622static void notify_rule_change(int event, struct fib_rule *rule,
Thomas Grafc17084d2006-08-15 00:32:48 -0700623 struct fib_rules_ops *ops, struct nlmsghdr *nlh,
624 u32 pid)
Thomas Graf14c0b972006-08-04 03:38:38 -0700625{
Denis V. Lunev9e3a5482008-01-20 16:46:41 -0800626 struct net *net;
Thomas Grafc17084d2006-08-15 00:32:48 -0700627 struct sk_buff *skb;
628 int err = -ENOBUFS;
Thomas Graf14c0b972006-08-04 03:38:38 -0700629
Denis V. Lunev9e3a5482008-01-20 16:46:41 -0800630 net = ops->fro_net;
Thomas Graf339bf982006-11-10 14:10:15 -0800631 skb = nlmsg_new(fib_rule_nlmsg_size(ops, rule), GFP_KERNEL);
Thomas Graf14c0b972006-08-04 03:38:38 -0700632 if (skb == NULL)
Thomas Grafc17084d2006-08-15 00:32:48 -0700633 goto errout;
634
635 err = fib_nl_fill_rule(skb, rule, pid, nlh->nlmsg_seq, event, 0, ops);
Patrick McHardy26932562007-01-31 23:16:40 -0800636 if (err < 0) {
637 /* -EMSGSIZE implies BUG in fib_rule_nlmsg_size() */
638 WARN_ON(err == -EMSGSIZE);
639 kfree_skb(skb);
640 goto errout;
641 }
Denis V. Lunev9e3a5482008-01-20 16:46:41 -0800642
Pablo Neira Ayuso1ce85fe2009-02-24 23:18:28 -0800643 rtnl_notify(skb, net, pid, ops->nlgroup, nlh, GFP_KERNEL);
644 return;
Thomas Grafc17084d2006-08-15 00:32:48 -0700645errout:
646 if (err < 0)
Denis V. Lunev5fd30ee2008-01-10 03:20:28 -0800647 rtnl_set_sk_err(net, ops->nlgroup, err);
Thomas Graf14c0b972006-08-04 03:38:38 -0700648}
649
650static void attach_rules(struct list_head *rules, struct net_device *dev)
651{
652 struct fib_rule *rule;
653
654 list_for_each_entry(rule, rules, list) {
Patrick McHardy491deb22009-12-03 01:25:54 +0000655 if (rule->iifindex == -1 &&
656 strcmp(dev->name, rule->iifname) == 0)
657 rule->iifindex = dev->ifindex;
Patrick McHardy1b038a52009-12-03 01:25:56 +0000658 if (rule->oifindex == -1 &&
659 strcmp(dev->name, rule->oifname) == 0)
660 rule->oifindex = dev->ifindex;
Thomas Graf14c0b972006-08-04 03:38:38 -0700661 }
662}
663
664static void detach_rules(struct list_head *rules, struct net_device *dev)
665{
666 struct fib_rule *rule;
667
Patrick McHardy1b038a52009-12-03 01:25:56 +0000668 list_for_each_entry(rule, rules, list) {
Patrick McHardy491deb22009-12-03 01:25:54 +0000669 if (rule->iifindex == dev->ifindex)
670 rule->iifindex = -1;
Patrick McHardy1b038a52009-12-03 01:25:56 +0000671 if (rule->oifindex == dev->ifindex)
672 rule->oifindex = -1;
673 }
Thomas Graf14c0b972006-08-04 03:38:38 -0700674}
675
676
677static int fib_rules_event(struct notifier_block *this, unsigned long event,
678 void *ptr)
679{
680 struct net_device *dev = ptr;
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +0900681 struct net *net = dev_net(dev);
Thomas Graf14c0b972006-08-04 03:38:38 -0700682 struct fib_rules_ops *ops;
683
684 ASSERT_RTNL();
Thomas Graf14c0b972006-08-04 03:38:38 -0700685
686 switch (event) {
687 case NETDEV_REGISTER:
Denis V. Lunev5fd30ee2008-01-10 03:20:28 -0800688 list_for_each_entry(ops, &net->rules_ops, list)
Denis V. Lunev76c72d42007-09-16 15:44:27 -0700689 attach_rules(&ops->rules_list, dev);
Thomas Graf14c0b972006-08-04 03:38:38 -0700690 break;
691
692 case NETDEV_UNREGISTER:
Denis V. Lunev5fd30ee2008-01-10 03:20:28 -0800693 list_for_each_entry(ops, &net->rules_ops, list)
Denis V. Lunev76c72d42007-09-16 15:44:27 -0700694 detach_rules(&ops->rules_list, dev);
Thomas Graf14c0b972006-08-04 03:38:38 -0700695 break;
696 }
697
Thomas Graf14c0b972006-08-04 03:38:38 -0700698 return NOTIFY_DONE;
699}
700
701static struct notifier_block fib_rules_notifier = {
702 .notifier_call = fib_rules_event,
703};
704
Alexey Dobriyan2c8c1e72010-01-17 03:35:32 +0000705static int __net_init fib_rules_net_init(struct net *net)
Denis V. Lunev5fd30ee2008-01-10 03:20:28 -0800706{
707 INIT_LIST_HEAD(&net->rules_ops);
708 spin_lock_init(&net->rules_mod_lock);
709 return 0;
710}
711
712static struct pernet_operations fib_rules_net_ops = {
713 .init = fib_rules_net_init,
714};
715
Thomas Graf14c0b972006-08-04 03:38:38 -0700716static int __init fib_rules_init(void)
717{
Denis V. Lunev5fd30ee2008-01-10 03:20:28 -0800718 int err;
Thomas Graf9d9e6a52007-03-25 23:20:05 -0700719 rtnl_register(PF_UNSPEC, RTM_NEWRULE, fib_nl_newrule, NULL);
720 rtnl_register(PF_UNSPEC, RTM_DELRULE, fib_nl_delrule, NULL);
Thomas Grafc4546732007-03-25 23:24:24 -0700721 rtnl_register(PF_UNSPEC, RTM_GETRULE, NULL, fib_nl_dumprule);
Thomas Graf9d9e6a52007-03-25 23:20:05 -0700722
Eric W. Biederman5d6d4802008-11-07 22:52:34 -0800723 err = register_pernet_subsys(&fib_rules_net_ops);
Denis V. Lunev5fd30ee2008-01-10 03:20:28 -0800724 if (err < 0)
725 goto fail;
726
Eric W. Biederman5d6d4802008-11-07 22:52:34 -0800727 err = register_netdevice_notifier(&fib_rules_notifier);
Denis V. Lunev5fd30ee2008-01-10 03:20:28 -0800728 if (err < 0)
729 goto fail_unregister;
Eric W. Biederman5d6d4802008-11-07 22:52:34 -0800730
Denis V. Lunev5fd30ee2008-01-10 03:20:28 -0800731 return 0;
732
733fail_unregister:
Eric W. Biederman5d6d4802008-11-07 22:52:34 -0800734 unregister_pernet_subsys(&fib_rules_net_ops);
Denis V. Lunev5fd30ee2008-01-10 03:20:28 -0800735fail:
736 rtnl_unregister(PF_UNSPEC, RTM_NEWRULE);
737 rtnl_unregister(PF_UNSPEC, RTM_DELRULE);
738 rtnl_unregister(PF_UNSPEC, RTM_GETRULE);
739 return err;
Thomas Graf14c0b972006-08-04 03:38:38 -0700740}
741
742subsys_initcall(fib_rules_init);