blob: 58a4ba27dfe3117d439ada122000e1339a23f48f [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>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090013#include <linux/slab.h>
Thomas Graf14c0b972006-08-04 03:38:38 -070014#include <linux/list.h>
Paul Gortmaker3a9a2312011-05-27 09:12:25 -040015#include <linux/module.h>
Eric W. Biedermane9dc8652007-09-12 13:02:17 +020016#include <net/net_namespace.h>
Eric W. Biederman881d9662007-09-17 11:56:21 -070017#include <net/sock.h>
Thomas Graf14c0b972006-08-04 03:38:38 -070018#include <net/fib_rules.h>
19
Denis V. Lunev2994c632007-11-10 22:12:03 -080020int fib_default_rule_add(struct fib_rules_ops *ops,
21 u32 pref, u32 table, u32 flags)
22{
23 struct fib_rule *r;
24
25 r = kzalloc(ops->rule_size, GFP_KERNEL);
26 if (r == NULL)
27 return -ENOMEM;
28
29 atomic_set(&r->refcnt, 1);
30 r->action = FR_ACT_TO_TBL;
31 r->pref = pref;
32 r->table = table;
33 r->flags = flags;
Denis V. Lunev3661a912008-04-16 02:01:56 -070034 r->fr_net = hold_net(ops->fro_net);
Denis V. Lunev2994c632007-11-10 22:12:03 -080035
36 /* The lock is not required here, the list in unreacheable
37 * at the moment this function is called */
38 list_add_tail(&r->list, &ops->rules_list);
39 return 0;
40}
41EXPORT_SYMBOL(fib_default_rule_add);
42
Patrick McHardyd8a566b2010-04-13 05:03:15 +000043u32 fib_default_rule_pref(struct fib_rules_ops *ops)
44{
45 struct list_head *pos;
46 struct fib_rule *rule;
47
48 if (!list_empty(&ops->rules_list)) {
49 pos = ops->rules_list.next;
50 if (pos->next != &ops->rules_list) {
51 rule = list_entry(pos->next, struct fib_rule, list);
52 if (rule->pref)
53 return rule->pref - 1;
54 }
55 }
56
57 return 0;
58}
59EXPORT_SYMBOL(fib_default_rule_pref);
60
Denis V. Lunev9e3a5482008-01-20 16:46:41 -080061static void notify_rule_change(int event, struct fib_rule *rule,
Thomas Grafc17084d2006-08-15 00:32:48 -070062 struct fib_rules_ops *ops, struct nlmsghdr *nlh,
63 u32 pid);
Thomas Graf14c0b972006-08-04 03:38:38 -070064
Denis V. Lunev5fd30ee2008-01-10 03:20:28 -080065static struct fib_rules_ops *lookup_rules_ops(struct net *net, int family)
Thomas Graf14c0b972006-08-04 03:38:38 -070066{
67 struct fib_rules_ops *ops;
68
69 rcu_read_lock();
Denis V. Lunev5fd30ee2008-01-10 03:20:28 -080070 list_for_each_entry_rcu(ops, &net->rules_ops, list) {
Thomas Graf14c0b972006-08-04 03:38:38 -070071 if (ops->family == family) {
72 if (!try_module_get(ops->owner))
73 ops = NULL;
74 rcu_read_unlock();
75 return ops;
76 }
77 }
78 rcu_read_unlock();
79
80 return NULL;
81}
82
83static void rules_ops_put(struct fib_rules_ops *ops)
84{
85 if (ops)
86 module_put(ops->owner);
87}
88
Thomas Graf73417f62007-03-27 13:56:52 -070089static void flush_route_cache(struct fib_rules_ops *ops)
90{
91 if (ops->flush_cache)
Denis V. Lunevae299fc2008-07-05 19:01:28 -070092 ops->flush_cache(ops);
Thomas Graf73417f62007-03-27 13:56:52 -070093}
94
Eric W. Biedermane9c51582009-12-03 12:22:55 -080095static int __fib_rules_register(struct fib_rules_ops *ops)
Thomas Graf14c0b972006-08-04 03:38:38 -070096{
97 int err = -EEXIST;
98 struct fib_rules_ops *o;
Denis V. Lunev9e3a5482008-01-20 16:46:41 -080099 struct net *net;
100
101 net = ops->fro_net;
Thomas Graf14c0b972006-08-04 03:38:38 -0700102
103 if (ops->rule_size < sizeof(struct fib_rule))
104 return -EINVAL;
105
106 if (ops->match == NULL || ops->configure == NULL ||
107 ops->compare == NULL || ops->fill == NULL ||
108 ops->action == NULL)
109 return -EINVAL;
110
Denis V. Lunev5fd30ee2008-01-10 03:20:28 -0800111 spin_lock(&net->rules_mod_lock);
112 list_for_each_entry(o, &net->rules_ops, list)
Thomas Graf14c0b972006-08-04 03:38:38 -0700113 if (ops->family == o->family)
114 goto errout;
115
Denis V. Lunev5fd30ee2008-01-10 03:20:28 -0800116 hold_net(net);
117 list_add_tail_rcu(&ops->list, &net->rules_ops);
Thomas Graf14c0b972006-08-04 03:38:38 -0700118 err = 0;
119errout:
Denis V. Lunev5fd30ee2008-01-10 03:20:28 -0800120 spin_unlock(&net->rules_mod_lock);
Thomas Graf14c0b972006-08-04 03:38:38 -0700121
122 return err;
123}
124
Eric W. Biedermane9c51582009-12-03 12:22:55 -0800125struct fib_rules_ops *
Patrick McHardy3d0c9c42010-04-26 16:02:04 +0200126fib_rules_register(const struct fib_rules_ops *tmpl, struct net *net)
Eric W. Biedermane9c51582009-12-03 12:22:55 -0800127{
128 struct fib_rules_ops *ops;
129 int err;
130
Eric Dumazet2fb35732010-03-09 20:03:38 +0000131 ops = kmemdup(tmpl, sizeof(*ops), GFP_KERNEL);
Eric W. Biedermane9c51582009-12-03 12:22:55 -0800132 if (ops == NULL)
133 return ERR_PTR(-ENOMEM);
134
135 INIT_LIST_HEAD(&ops->rules_list);
136 ops->fro_net = net;
137
138 err = __fib_rules_register(ops);
139 if (err) {
140 kfree(ops);
141 ops = ERR_PTR(err);
142 }
143
144 return ops;
145}
Thomas Graf14c0b972006-08-04 03:38:38 -0700146EXPORT_SYMBOL_GPL(fib_rules_register);
147
stephen hemminger1df99162010-10-04 20:14:17 +0000148static void fib_rules_cleanup_ops(struct fib_rules_ops *ops)
Thomas Graf14c0b972006-08-04 03:38:38 -0700149{
150 struct fib_rule *rule, *tmp;
151
Denis V. Lunev76c72d42007-09-16 15:44:27 -0700152 list_for_each_entry_safe(rule, tmp, &ops->rules_list, list) {
Thomas Graf14c0b972006-08-04 03:38:38 -0700153 list_del_rcu(&rule->list);
David S. Miller7a9bc9b2012-06-29 01:32:45 -0700154 if (ops->delete)
155 ops->delete(rule);
Thomas Graf14c0b972006-08-04 03:38:38 -0700156 fib_rule_put(rule);
157 }
158}
159
Eric W. Biedermane9c51582009-12-03 12:22:55 -0800160static void fib_rules_put_rcu(struct rcu_head *head)
161{
162 struct fib_rules_ops *ops = container_of(head, struct fib_rules_ops, rcu);
163 struct net *net = ops->fro_net;
164
165 release_net(net);
166 kfree(ops);
167}
168
Denis V. Lunev9e3a5482008-01-20 16:46:41 -0800169void fib_rules_unregister(struct fib_rules_ops *ops)
Thomas Graf14c0b972006-08-04 03:38:38 -0700170{
Denis V. Lunev9e3a5482008-01-20 16:46:41 -0800171 struct net *net = ops->fro_net;
Thomas Graf14c0b972006-08-04 03:38:38 -0700172
Denis V. Lunev5fd30ee2008-01-10 03:20:28 -0800173 spin_lock(&net->rules_mod_lock);
Denis V. Lunev72132c1b2008-01-14 22:59:30 -0800174 list_del_rcu(&ops->list);
175 fib_rules_cleanup_ops(ops);
Denis V. Lunev5fd30ee2008-01-10 03:20:28 -0800176 spin_unlock(&net->rules_mod_lock);
Thomas Graf14c0b972006-08-04 03:38:38 -0700177
Eric W. Biedermane9c51582009-12-03 12:22:55 -0800178 call_rcu(&ops->rcu, fib_rules_put_rcu);
Thomas Graf14c0b972006-08-04 03:38:38 -0700179}
Thomas Graf14c0b972006-08-04 03:38:38 -0700180EXPORT_SYMBOL_GPL(fib_rules_unregister);
181
Thomas Graf3dfbcc42006-11-09 15:23:20 -0800182static int fib_rule_match(struct fib_rule *rule, struct fib_rules_ops *ops,
183 struct flowi *fl, int flags)
184{
185 int ret = 0;
186
David S. Miller1d28f422011-03-12 00:29:39 -0500187 if (rule->iifindex && (rule->iifindex != fl->flowi_iif))
Thomas Graf3dfbcc42006-11-09 15:23:20 -0800188 goto out;
189
David S. Miller1d28f422011-03-12 00:29:39 -0500190 if (rule->oifindex && (rule->oifindex != fl->flowi_oif))
Patrick McHardy1b038a52009-12-03 01:25:56 +0000191 goto out;
192
David S. Miller1d28f422011-03-12 00:29:39 -0500193 if ((rule->mark ^ fl->flowi_mark) & rule->mark_mask)
Thomas Graf3dfbcc42006-11-09 15:23:20 -0800194 goto out;
195
196 ret = ops->match(rule, fl, flags);
197out:
198 return (rule->flags & FIB_RULE_INVERT) ? !ret : ret;
199}
200
Thomas Graf14c0b972006-08-04 03:38:38 -0700201int fib_rules_lookup(struct fib_rules_ops *ops, struct flowi *fl,
202 int flags, struct fib_lookup_arg *arg)
203{
204 struct fib_rule *rule;
205 int err;
206
207 rcu_read_lock();
208
Denis V. Lunev76c72d42007-09-16 15:44:27 -0700209 list_for_each_entry_rcu(rule, &ops->rules_list, list) {
Thomas Graf0947c9fe2007-03-26 17:14:15 -0700210jumped:
Thomas Graf3dfbcc42006-11-09 15:23:20 -0800211 if (!fib_rule_match(rule, ops, fl, flags))
Thomas Graf14c0b972006-08-04 03:38:38 -0700212 continue;
213
Thomas Graf0947c9fe2007-03-26 17:14:15 -0700214 if (rule->action == FR_ACT_GOTO) {
215 struct fib_rule *target;
216
217 target = rcu_dereference(rule->ctarget);
218 if (target == NULL) {
219 continue;
220 } else {
221 rule = target;
222 goto jumped;
223 }
Thomas Graffa0b2d12007-03-26 17:38:53 -0700224 } else if (rule->action == FR_ACT_NOP)
225 continue;
226 else
Thomas Graf0947c9fe2007-03-26 17:14:15 -0700227 err = ops->action(rule, fl, flags, arg);
228
Thomas Graf14c0b972006-08-04 03:38:38 -0700229 if (err != -EAGAIN) {
Eric Dumazetebc0ffa2010-10-05 10:41:36 +0000230 if ((arg->flags & FIB_LOOKUP_NOREF) ||
231 likely(atomic_inc_not_zero(&rule->refcnt))) {
Eric Dumazet7fa7cb72010-09-27 04:18:27 +0000232 arg->rule = rule;
233 goto out;
234 }
235 break;
Thomas Graf14c0b972006-08-04 03:38:38 -0700236 }
237 }
238
Steven Whitehouse83886b62007-03-30 13:34:27 -0700239 err = -ESRCH;
Thomas Graf14c0b972006-08-04 03:38:38 -0700240out:
241 rcu_read_unlock();
242
243 return err;
244}
Thomas Graf14c0b972006-08-04 03:38:38 -0700245EXPORT_SYMBOL_GPL(fib_rules_lookup);
246
Thomas Grafe1701c62007-03-24 12:46:02 -0700247static int validate_rulemsg(struct fib_rule_hdr *frh, struct nlattr **tb,
248 struct fib_rules_ops *ops)
249{
250 int err = -EINVAL;
251
252 if (frh->src_len)
253 if (tb[FRA_SRC] == NULL ||
254 frh->src_len > (ops->addr_size * 8) ||
255 nla_len(tb[FRA_SRC]) != ops->addr_size)
256 goto errout;
257
258 if (frh->dst_len)
259 if (tb[FRA_DST] == NULL ||
260 frh->dst_len > (ops->addr_size * 8) ||
261 nla_len(tb[FRA_DST]) != ops->addr_size)
262 goto errout;
263
264 err = 0;
265errout:
266 return err;
267}
268
Thomas Graf9d9e6a52007-03-25 23:20:05 -0700269static int fib_nl_newrule(struct sk_buff *skb, struct nlmsghdr* nlh, void *arg)
Thomas Graf14c0b972006-08-04 03:38:38 -0700270{
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +0900271 struct net *net = sock_net(skb->sk);
Thomas Graf14c0b972006-08-04 03:38:38 -0700272 struct fib_rule_hdr *frh = nlmsg_data(nlh);
273 struct fib_rules_ops *ops = NULL;
274 struct fib_rule *rule, *r, *last = NULL;
275 struct nlattr *tb[FRA_MAX+1];
Thomas Graf0947c9fe2007-03-26 17:14:15 -0700276 int err = -EINVAL, unresolved = 0;
Thomas Graf14c0b972006-08-04 03:38:38 -0700277
278 if (nlh->nlmsg_len < nlmsg_msg_size(sizeof(*frh)))
279 goto errout;
280
Denis V. Lunev5fd30ee2008-01-10 03:20:28 -0800281 ops = lookup_rules_ops(net, frh->family);
Thomas Graf14c0b972006-08-04 03:38:38 -0700282 if (ops == NULL) {
Patrick McHardy2fe195c2008-07-01 19:59:37 -0700283 err = -EAFNOSUPPORT;
Thomas Graf14c0b972006-08-04 03:38:38 -0700284 goto errout;
285 }
286
287 err = nlmsg_parse(nlh, sizeof(*frh), tb, FRA_MAX, ops->policy);
288 if (err < 0)
289 goto errout;
290
Thomas Grafe1701c62007-03-24 12:46:02 -0700291 err = validate_rulemsg(frh, tb, ops);
292 if (err < 0)
293 goto errout;
294
Thomas Graf14c0b972006-08-04 03:38:38 -0700295 rule = kzalloc(ops->rule_size, GFP_KERNEL);
296 if (rule == NULL) {
297 err = -ENOMEM;
298 goto errout;
299 }
Denis V. Lunev3661a912008-04-16 02:01:56 -0700300 rule->fr_net = hold_net(net);
Thomas Graf14c0b972006-08-04 03:38:38 -0700301
302 if (tb[FRA_PRIORITY])
303 rule->pref = nla_get_u32(tb[FRA_PRIORITY]);
304
Patrick McHardy491deb24b2009-12-03 01:25:54 +0000305 if (tb[FRA_IIFNAME]) {
Thomas Graf14c0b972006-08-04 03:38:38 -0700306 struct net_device *dev;
307
Patrick McHardy491deb24b2009-12-03 01:25:54 +0000308 rule->iifindex = -1;
309 nla_strlcpy(rule->iifname, tb[FRA_IIFNAME], IFNAMSIZ);
310 dev = __dev_get_by_name(net, rule->iifname);
Thomas Graf14c0b972006-08-04 03:38:38 -0700311 if (dev)
Patrick McHardy491deb24b2009-12-03 01:25:54 +0000312 rule->iifindex = dev->ifindex;
Thomas Graf14c0b972006-08-04 03:38:38 -0700313 }
314
Patrick McHardy1b038a52009-12-03 01:25:56 +0000315 if (tb[FRA_OIFNAME]) {
316 struct net_device *dev;
317
318 rule->oifindex = -1;
319 nla_strlcpy(rule->oifname, tb[FRA_OIFNAME], IFNAMSIZ);
320 dev = __dev_get_by_name(net, rule->oifname);
321 if (dev)
322 rule->oifindex = dev->ifindex;
323 }
324
Thomas Grafb8964ed2006-11-09 15:22:18 -0800325 if (tb[FRA_FWMARK]) {
326 rule->mark = nla_get_u32(tb[FRA_FWMARK]);
327 if (rule->mark)
328 /* compatibility: if the mark value is non-zero all bits
329 * are compared unless a mask is explicitly specified.
330 */
331 rule->mark_mask = 0xFFFFFFFF;
332 }
333
334 if (tb[FRA_FWMASK])
335 rule->mark_mask = nla_get_u32(tb[FRA_FWMASK]);
336
Thomas Graf14c0b972006-08-04 03:38:38 -0700337 rule->action = frh->action;
338 rule->flags = frh->flags;
Patrick McHardy9e762a42006-08-10 23:09:48 -0700339 rule->table = frh_get_table(frh, tb);
Thomas Graf14c0b972006-08-04 03:38:38 -0700340
Patrick McHardy5adef182009-12-03 01:25:57 +0000341 if (!tb[FRA_PRIORITY] && ops->default_pref)
Denis V. Lunev868d13a2008-01-10 03:18:25 -0800342 rule->pref = ops->default_pref(ops);
Thomas Graf14c0b972006-08-04 03:38:38 -0700343
Thomas Graf0947c9fe2007-03-26 17:14:15 -0700344 err = -EINVAL;
345 if (tb[FRA_GOTO]) {
346 if (rule->action != FR_ACT_GOTO)
347 goto errout_free;
348
349 rule->target = nla_get_u32(tb[FRA_GOTO]);
350 /* Backward jumps are prohibited to avoid endless loops */
351 if (rule->target <= rule->pref)
352 goto errout_free;
353
Denis V. Lunev76c72d42007-09-16 15:44:27 -0700354 list_for_each_entry(r, &ops->rules_list, list) {
Thomas Graf0947c9fe2007-03-26 17:14:15 -0700355 if (r->pref == rule->target) {
Eric Dumazet7a2b03c2010-10-26 09:24:55 +0000356 RCU_INIT_POINTER(rule->ctarget, r);
Thomas Graf0947c9fe2007-03-26 17:14:15 -0700357 break;
358 }
359 }
360
Eric Dumazet7a2b03c2010-10-26 09:24:55 +0000361 if (rcu_dereference_protected(rule->ctarget, 1) == NULL)
Thomas Graf0947c9fe2007-03-26 17:14:15 -0700362 unresolved = 1;
363 } else if (rule->action == FR_ACT_GOTO)
364 goto errout_free;
365
Rami Rosen8b3521e2009-05-11 05:52:49 +0000366 err = ops->configure(rule, skb, frh, tb);
Thomas Graf14c0b972006-08-04 03:38:38 -0700367 if (err < 0)
368 goto errout_free;
369
Denis V. Lunev76c72d42007-09-16 15:44:27 -0700370 list_for_each_entry(r, &ops->rules_list, list) {
Thomas Graf14c0b972006-08-04 03:38:38 -0700371 if (r->pref > rule->pref)
372 break;
373 last = r;
374 }
375
376 fib_rule_get(rule);
377
Eric Dumazetebb9fed2010-10-23 09:44:25 +0000378 if (last)
379 list_add_rcu(&rule->list, &last->list);
380 else
381 list_add_rcu(&rule->list, &ops->rules_list);
382
Thomas Graf0947c9fe2007-03-26 17:14:15 -0700383 if (ops->unresolved_rules) {
384 /*
385 * There are unresolved goto rules in the list, check if
386 * any of them are pointing to this new rule.
387 */
Denis V. Lunev76c72d42007-09-16 15:44:27 -0700388 list_for_each_entry(r, &ops->rules_list, list) {
Thomas Graf0947c9fe2007-03-26 17:14:15 -0700389 if (r->action == FR_ACT_GOTO &&
Gao feng561dac22011-09-11 15:36:05 +0000390 r->target == rule->pref &&
391 rtnl_dereference(r->ctarget) == NULL) {
Thomas Graf0947c9fe2007-03-26 17:14:15 -0700392 rcu_assign_pointer(r->ctarget, rule);
393 if (--ops->unresolved_rules == 0)
394 break;
395 }
396 }
397 }
398
399 if (rule->action == FR_ACT_GOTO)
400 ops->nr_goto_rules++;
401
402 if (unresolved)
403 ops->unresolved_rules++;
404
Eric W. Biederman15e47302012-09-07 20:12:54 +0000405 notify_rule_change(RTM_NEWRULE, rule, ops, nlh, NETLINK_CB(skb).portid);
Thomas Graf73417f62007-03-27 13:56:52 -0700406 flush_route_cache(ops);
Thomas Graf14c0b972006-08-04 03:38:38 -0700407 rules_ops_put(ops);
408 return 0;
409
410errout_free:
Denis V. Lunev3661a912008-04-16 02:01:56 -0700411 release_net(rule->fr_net);
Thomas Graf14c0b972006-08-04 03:38:38 -0700412 kfree(rule);
413errout:
414 rules_ops_put(ops);
415 return err;
416}
417
Thomas Graf9d9e6a52007-03-25 23:20:05 -0700418static int fib_nl_delrule(struct sk_buff *skb, struct nlmsghdr* nlh, void *arg)
Thomas Graf14c0b972006-08-04 03:38:38 -0700419{
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +0900420 struct net *net = sock_net(skb->sk);
Thomas Graf14c0b972006-08-04 03:38:38 -0700421 struct fib_rule_hdr *frh = nlmsg_data(nlh);
422 struct fib_rules_ops *ops = NULL;
Thomas Graf0947c9fe2007-03-26 17:14:15 -0700423 struct fib_rule *rule, *tmp;
Thomas Graf14c0b972006-08-04 03:38:38 -0700424 struct nlattr *tb[FRA_MAX+1];
425 int err = -EINVAL;
426
427 if (nlh->nlmsg_len < nlmsg_msg_size(sizeof(*frh)))
428 goto errout;
429
Denis V. Lunev5fd30ee2008-01-10 03:20:28 -0800430 ops = lookup_rules_ops(net, frh->family);
Thomas Graf14c0b972006-08-04 03:38:38 -0700431 if (ops == NULL) {
Patrick McHardy2fe195c2008-07-01 19:59:37 -0700432 err = -EAFNOSUPPORT;
Thomas Graf14c0b972006-08-04 03:38:38 -0700433 goto errout;
434 }
435
436 err = nlmsg_parse(nlh, sizeof(*frh), tb, FRA_MAX, ops->policy);
437 if (err < 0)
438 goto errout;
439
Thomas Grafe1701c62007-03-24 12:46:02 -0700440 err = validate_rulemsg(frh, tb, ops);
441 if (err < 0)
442 goto errout;
443
Denis V. Lunev76c72d42007-09-16 15:44:27 -0700444 list_for_each_entry(rule, &ops->rules_list, list) {
Thomas Graf14c0b972006-08-04 03:38:38 -0700445 if (frh->action && (frh->action != rule->action))
446 continue;
447
Patrick McHardy9e762a42006-08-10 23:09:48 -0700448 if (frh->table && (frh_get_table(frh, tb) != rule->table))
Thomas Graf14c0b972006-08-04 03:38:38 -0700449 continue;
450
451 if (tb[FRA_PRIORITY] &&
452 (rule->pref != nla_get_u32(tb[FRA_PRIORITY])))
453 continue;
454
Patrick McHardy491deb24b2009-12-03 01:25:54 +0000455 if (tb[FRA_IIFNAME] &&
456 nla_strcmp(tb[FRA_IIFNAME], rule->iifname))
Thomas Graf14c0b972006-08-04 03:38:38 -0700457 continue;
458
Patrick McHardy1b038a52009-12-03 01:25:56 +0000459 if (tb[FRA_OIFNAME] &&
460 nla_strcmp(tb[FRA_OIFNAME], rule->oifname))
461 continue;
462
Thomas Grafb8964ed2006-11-09 15:22:18 -0800463 if (tb[FRA_FWMARK] &&
464 (rule->mark != nla_get_u32(tb[FRA_FWMARK])))
465 continue;
466
467 if (tb[FRA_FWMASK] &&
468 (rule->mark_mask != nla_get_u32(tb[FRA_FWMASK])))
469 continue;
470
Thomas Graf14c0b972006-08-04 03:38:38 -0700471 if (!ops->compare(rule, frh, tb))
472 continue;
473
474 if (rule->flags & FIB_RULE_PERMANENT) {
475 err = -EPERM;
476 goto errout;
477 }
478
479 list_del_rcu(&rule->list);
Thomas Graf0947c9fe2007-03-26 17:14:15 -0700480
Yan, Zhengafaef732011-10-17 15:20:28 +0000481 if (rule->action == FR_ACT_GOTO) {
Thomas Graf0947c9fe2007-03-26 17:14:15 -0700482 ops->nr_goto_rules--;
Yan, Zhengafaef732011-10-17 15:20:28 +0000483 if (rtnl_dereference(rule->ctarget) == NULL)
484 ops->unresolved_rules--;
485 }
Thomas Graf0947c9fe2007-03-26 17:14:15 -0700486
487 /*
488 * Check if this rule is a target to any of them. If so,
489 * disable them. As this operation is eventually very
490 * expensive, it is only performed if goto rules have
491 * actually been added.
492 */
493 if (ops->nr_goto_rules > 0) {
Denis V. Lunev76c72d42007-09-16 15:44:27 -0700494 list_for_each_entry(tmp, &ops->rules_list, list) {
Eric Dumazet7a2b03c2010-10-26 09:24:55 +0000495 if (rtnl_dereference(tmp->ctarget) == rule) {
Stephen Hemmingera9b3cd72011-08-01 16:19:00 +0000496 RCU_INIT_POINTER(tmp->ctarget, NULL);
Thomas Graf0947c9fe2007-03-26 17:14:15 -0700497 ops->unresolved_rules++;
498 }
499 }
500 }
501
Denis V. Lunev9e3a5482008-01-20 16:46:41 -0800502 notify_rule_change(RTM_DELRULE, rule, ops, nlh,
Eric W. Biederman15e47302012-09-07 20:12:54 +0000503 NETLINK_CB(skb).portid);
David S. Miller7a9bc9b2012-06-29 01:32:45 -0700504 if (ops->delete)
505 ops->delete(rule);
Thomas Graf14c0b972006-08-04 03:38:38 -0700506 fib_rule_put(rule);
Thomas Graf73417f62007-03-27 13:56:52 -0700507 flush_route_cache(ops);
Thomas Graf14c0b972006-08-04 03:38:38 -0700508 rules_ops_put(ops);
509 return 0;
510 }
511
512 err = -ENOENT;
513errout:
514 rules_ops_put(ops);
515 return err;
516}
517
Thomas Graf339bf982006-11-10 14:10:15 -0800518static inline size_t fib_rule_nlmsg_size(struct fib_rules_ops *ops,
519 struct fib_rule *rule)
520{
521 size_t payload = NLMSG_ALIGN(sizeof(struct fib_rule_hdr))
Patrick McHardy491deb24b2009-12-03 01:25:54 +0000522 + nla_total_size(IFNAMSIZ) /* FRA_IIFNAME */
Patrick McHardy1b038a52009-12-03 01:25:56 +0000523 + nla_total_size(IFNAMSIZ) /* FRA_OIFNAME */
Thomas Graf339bf982006-11-10 14:10:15 -0800524 + nla_total_size(4) /* FRA_PRIORITY */
525 + nla_total_size(4) /* FRA_TABLE */
526 + nla_total_size(4) /* FRA_FWMARK */
527 + nla_total_size(4); /* FRA_FWMASK */
528
529 if (ops->nlmsg_payload)
530 payload += ops->nlmsg_payload(rule);
531
532 return payload;
533}
534
Thomas Graf14c0b972006-08-04 03:38:38 -0700535static int fib_nl_fill_rule(struct sk_buff *skb, struct fib_rule *rule,
536 u32 pid, u32 seq, int type, int flags,
537 struct fib_rules_ops *ops)
538{
539 struct nlmsghdr *nlh;
540 struct fib_rule_hdr *frh;
541
542 nlh = nlmsg_put(skb, pid, seq, type, sizeof(*frh), flags);
543 if (nlh == NULL)
Patrick McHardy26932562007-01-31 23:16:40 -0800544 return -EMSGSIZE;
Thomas Graf14c0b972006-08-04 03:38:38 -0700545
546 frh = nlmsg_data(nlh);
Patrick McHardy28bb1722010-04-13 05:03:16 +0000547 frh->family = ops->family;
Thomas Graf14c0b972006-08-04 03:38:38 -0700548 frh->table = rule->table;
David S. Miller0e3cea72012-04-01 20:47:01 -0400549 if (nla_put_u32(skb, FRA_TABLE, rule->table))
550 goto nla_put_failure;
Thomas Graf14c0b972006-08-04 03:38:38 -0700551 frh->res1 = 0;
552 frh->res2 = 0;
553 frh->action = rule->action;
554 frh->flags = rule->flags;
555
Eric Dumazet7a2b03c2010-10-26 09:24:55 +0000556 if (rule->action == FR_ACT_GOTO &&
Eric Dumazet33d480c2011-08-11 19:30:52 +0000557 rcu_access_pointer(rule->ctarget) == NULL)
Thomas Graf0947c9fe2007-03-26 17:14:15 -0700558 frh->flags |= FIB_RULE_UNRESOLVED;
559
Patrick McHardy491deb24b2009-12-03 01:25:54 +0000560 if (rule->iifname[0]) {
David S. Miller0e3cea72012-04-01 20:47:01 -0400561 if (nla_put_string(skb, FRA_IIFNAME, rule->iifname))
562 goto nla_put_failure;
Patrick McHardy491deb24b2009-12-03 01:25:54 +0000563 if (rule->iifindex == -1)
564 frh->flags |= FIB_RULE_IIF_DETACHED;
Thomas Graf2b443682007-03-26 17:37:59 -0700565 }
566
Patrick McHardy1b038a52009-12-03 01:25:56 +0000567 if (rule->oifname[0]) {
David S. Miller0e3cea72012-04-01 20:47:01 -0400568 if (nla_put_string(skb, FRA_OIFNAME, rule->oifname))
569 goto nla_put_failure;
Patrick McHardy1b038a52009-12-03 01:25:56 +0000570 if (rule->oifindex == -1)
571 frh->flags |= FIB_RULE_OIF_DETACHED;
572 }
573
David S. Miller0e3cea72012-04-01 20:47:01 -0400574 if ((rule->pref &&
575 nla_put_u32(skb, FRA_PRIORITY, rule->pref)) ||
576 (rule->mark &&
577 nla_put_u32(skb, FRA_FWMARK, rule->mark)) ||
578 ((rule->mark_mask || rule->mark) &&
579 nla_put_u32(skb, FRA_FWMASK, rule->mark_mask)) ||
580 (rule->target &&
581 nla_put_u32(skb, FRA_GOTO, rule->target)))
582 goto nla_put_failure;
Rami Rosen04af8cf2009-05-20 17:26:23 -0700583 if (ops->fill(rule, skb, frh) < 0)
Thomas Graf14c0b972006-08-04 03:38:38 -0700584 goto nla_put_failure;
585
586 return nlmsg_end(skb, nlh);
587
588nla_put_failure:
Patrick McHardy26932562007-01-31 23:16:40 -0800589 nlmsg_cancel(skb, nlh);
590 return -EMSGSIZE;
Thomas Graf14c0b972006-08-04 03:38:38 -0700591}
592
Thomas Grafc4546732007-03-25 23:24:24 -0700593static int dump_rules(struct sk_buff *skb, struct netlink_callback *cb,
594 struct fib_rules_ops *ops)
Thomas Graf14c0b972006-08-04 03:38:38 -0700595{
596 int idx = 0;
597 struct fib_rule *rule;
Thomas Graf14c0b972006-08-04 03:38:38 -0700598
Eric Dumazete67f88d2011-04-27 22:56:07 +0000599 rcu_read_lock();
600 list_for_each_entry_rcu(rule, &ops->rules_list, list) {
Thomas Grafc4546732007-03-25 23:24:24 -0700601 if (idx < cb->args[1])
Thomas Graf14c0b972006-08-04 03:38:38 -0700602 goto skip;
603
Eric W. Biederman15e47302012-09-07 20:12:54 +0000604 if (fib_nl_fill_rule(skb, rule, NETLINK_CB(cb->skb).portid,
Thomas Graf14c0b972006-08-04 03:38:38 -0700605 cb->nlh->nlmsg_seq, RTM_NEWRULE,
606 NLM_F_MULTI, ops) < 0)
607 break;
608skip:
609 idx++;
610 }
Eric Dumazet2907c352011-05-25 07:34:04 +0000611 rcu_read_unlock();
Thomas Grafc4546732007-03-25 23:24:24 -0700612 cb->args[1] = idx;
Thomas Graf14c0b972006-08-04 03:38:38 -0700613 rules_ops_put(ops);
614
615 return skb->len;
616}
617
Thomas Grafc4546732007-03-25 23:24:24 -0700618static int fib_nl_dumprule(struct sk_buff *skb, struct netlink_callback *cb)
619{
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +0900620 struct net *net = sock_net(skb->sk);
Thomas Grafc4546732007-03-25 23:24:24 -0700621 struct fib_rules_ops *ops;
622 int idx = 0, family;
623
624 family = rtnl_msg_family(cb->nlh);
625 if (family != AF_UNSPEC) {
626 /* Protocol specific dump request */
Denis V. Lunev5fd30ee2008-01-10 03:20:28 -0800627 ops = lookup_rules_ops(net, family);
Thomas Grafc4546732007-03-25 23:24:24 -0700628 if (ops == NULL)
629 return -EAFNOSUPPORT;
630
631 return dump_rules(skb, cb, ops);
632 }
633
634 rcu_read_lock();
Denis V. Lunev5fd30ee2008-01-10 03:20:28 -0800635 list_for_each_entry_rcu(ops, &net->rules_ops, list) {
Thomas Grafc4546732007-03-25 23:24:24 -0700636 if (idx < cb->args[0] || !try_module_get(ops->owner))
637 goto skip;
638
639 if (dump_rules(skb, cb, ops) < 0)
640 break;
641
642 cb->args[1] = 0;
Eric Dumazet2fb35732010-03-09 20:03:38 +0000643skip:
Thomas Grafc4546732007-03-25 23:24:24 -0700644 idx++;
645 }
646 rcu_read_unlock();
647 cb->args[0] = idx;
648
649 return skb->len;
650}
Thomas Graf14c0b972006-08-04 03:38:38 -0700651
Denis V. Lunev9e3a5482008-01-20 16:46:41 -0800652static void notify_rule_change(int event, struct fib_rule *rule,
Thomas Grafc17084d2006-08-15 00:32:48 -0700653 struct fib_rules_ops *ops, struct nlmsghdr *nlh,
654 u32 pid)
Thomas Graf14c0b972006-08-04 03:38:38 -0700655{
Denis V. Lunev9e3a5482008-01-20 16:46:41 -0800656 struct net *net;
Thomas Grafc17084d2006-08-15 00:32:48 -0700657 struct sk_buff *skb;
658 int err = -ENOBUFS;
Thomas Graf14c0b972006-08-04 03:38:38 -0700659
Denis V. Lunev9e3a5482008-01-20 16:46:41 -0800660 net = ops->fro_net;
Thomas Graf339bf982006-11-10 14:10:15 -0800661 skb = nlmsg_new(fib_rule_nlmsg_size(ops, rule), GFP_KERNEL);
Thomas Graf14c0b972006-08-04 03:38:38 -0700662 if (skb == NULL)
Thomas Grafc17084d2006-08-15 00:32:48 -0700663 goto errout;
664
665 err = fib_nl_fill_rule(skb, rule, pid, nlh->nlmsg_seq, event, 0, ops);
Patrick McHardy26932562007-01-31 23:16:40 -0800666 if (err < 0) {
667 /* -EMSGSIZE implies BUG in fib_rule_nlmsg_size() */
668 WARN_ON(err == -EMSGSIZE);
669 kfree_skb(skb);
670 goto errout;
671 }
Denis V. Lunev9e3a5482008-01-20 16:46:41 -0800672
Pablo Neira Ayuso1ce85fe2009-02-24 23:18:28 -0800673 rtnl_notify(skb, net, pid, ops->nlgroup, nlh, GFP_KERNEL);
674 return;
Thomas Grafc17084d2006-08-15 00:32:48 -0700675errout:
676 if (err < 0)
Denis V. Lunev5fd30ee2008-01-10 03:20:28 -0800677 rtnl_set_sk_err(net, ops->nlgroup, err);
Thomas Graf14c0b972006-08-04 03:38:38 -0700678}
679
680static void attach_rules(struct list_head *rules, struct net_device *dev)
681{
682 struct fib_rule *rule;
683
684 list_for_each_entry(rule, rules, list) {
Patrick McHardy491deb24b2009-12-03 01:25:54 +0000685 if (rule->iifindex == -1 &&
686 strcmp(dev->name, rule->iifname) == 0)
687 rule->iifindex = dev->ifindex;
Patrick McHardy1b038a52009-12-03 01:25:56 +0000688 if (rule->oifindex == -1 &&
689 strcmp(dev->name, rule->oifname) == 0)
690 rule->oifindex = dev->ifindex;
Thomas Graf14c0b972006-08-04 03:38:38 -0700691 }
692}
693
694static void detach_rules(struct list_head *rules, struct net_device *dev)
695{
696 struct fib_rule *rule;
697
Patrick McHardy1b038a52009-12-03 01:25:56 +0000698 list_for_each_entry(rule, rules, list) {
Patrick McHardy491deb24b2009-12-03 01:25:54 +0000699 if (rule->iifindex == dev->ifindex)
700 rule->iifindex = -1;
Patrick McHardy1b038a52009-12-03 01:25:56 +0000701 if (rule->oifindex == dev->ifindex)
702 rule->oifindex = -1;
703 }
Thomas Graf14c0b972006-08-04 03:38:38 -0700704}
705
706
707static int fib_rules_event(struct notifier_block *this, unsigned long event,
708 void *ptr)
709{
710 struct net_device *dev = ptr;
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +0900711 struct net *net = dev_net(dev);
Thomas Graf14c0b972006-08-04 03:38:38 -0700712 struct fib_rules_ops *ops;
713
Eric Dumazet748e2d92012-08-22 21:50:59 +0000714 ASSERT_RTNL();
Thomas Graf14c0b972006-08-04 03:38:38 -0700715
716 switch (event) {
717 case NETDEV_REGISTER:
Denis V. Lunev5fd30ee2008-01-10 03:20:28 -0800718 list_for_each_entry(ops, &net->rules_ops, list)
Denis V. Lunev76c72d42007-09-16 15:44:27 -0700719 attach_rules(&ops->rules_list, dev);
Thomas Graf14c0b972006-08-04 03:38:38 -0700720 break;
721
722 case NETDEV_UNREGISTER:
Denis V. Lunev5fd30ee2008-01-10 03:20:28 -0800723 list_for_each_entry(ops, &net->rules_ops, list)
Denis V. Lunev76c72d42007-09-16 15:44:27 -0700724 detach_rules(&ops->rules_list, dev);
Thomas Graf14c0b972006-08-04 03:38:38 -0700725 break;
726 }
727
Thomas Graf14c0b972006-08-04 03:38:38 -0700728 return NOTIFY_DONE;
729}
730
731static struct notifier_block fib_rules_notifier = {
732 .notifier_call = fib_rules_event,
733};
734
Alexey Dobriyan2c8c1e72010-01-17 03:35:32 +0000735static int __net_init fib_rules_net_init(struct net *net)
Denis V. Lunev5fd30ee2008-01-10 03:20:28 -0800736{
737 INIT_LIST_HEAD(&net->rules_ops);
738 spin_lock_init(&net->rules_mod_lock);
739 return 0;
740}
741
742static struct pernet_operations fib_rules_net_ops = {
743 .init = fib_rules_net_init,
744};
745
Thomas Graf14c0b972006-08-04 03:38:38 -0700746static int __init fib_rules_init(void)
747{
Denis V. Lunev5fd30ee2008-01-10 03:20:28 -0800748 int err;
Greg Rosec7ac8672011-06-10 01:27:09 +0000749 rtnl_register(PF_UNSPEC, RTM_NEWRULE, fib_nl_newrule, NULL, NULL);
750 rtnl_register(PF_UNSPEC, RTM_DELRULE, fib_nl_delrule, NULL, NULL);
751 rtnl_register(PF_UNSPEC, RTM_GETRULE, NULL, fib_nl_dumprule, NULL);
Thomas Graf9d9e6a52007-03-25 23:20:05 -0700752
Eric W. Biederman5d6d4802008-11-07 22:52:34 -0800753 err = register_pernet_subsys(&fib_rules_net_ops);
Denis V. Lunev5fd30ee2008-01-10 03:20:28 -0800754 if (err < 0)
755 goto fail;
756
Eric W. Biederman5d6d4802008-11-07 22:52:34 -0800757 err = register_netdevice_notifier(&fib_rules_notifier);
Denis V. Lunev5fd30ee2008-01-10 03:20:28 -0800758 if (err < 0)
759 goto fail_unregister;
Eric W. Biederman5d6d4802008-11-07 22:52:34 -0800760
Denis V. Lunev5fd30ee2008-01-10 03:20:28 -0800761 return 0;
762
763fail_unregister:
Eric W. Biederman5d6d4802008-11-07 22:52:34 -0800764 unregister_pernet_subsys(&fib_rules_net_ops);
Denis V. Lunev5fd30ee2008-01-10 03:20:28 -0800765fail:
766 rtnl_unregister(PF_UNSPEC, RTM_NEWRULE);
767 rtnl_unregister(PF_UNSPEC, RTM_DELRULE);
768 rtnl_unregister(PF_UNSPEC, RTM_GETRULE);
769 return err;
Thomas Graf14c0b972006-08-04 03:38:38 -0700770}
771
772subsys_initcall(fib_rules_init);