blob: c02e63c908da6972228735ba247dfcda6798b233 [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);
154 fib_rule_put(rule);
155 }
156}
157
Eric W. Biedermane9c51582009-12-03 12:22:55 -0800158static void fib_rules_put_rcu(struct rcu_head *head)
159{
160 struct fib_rules_ops *ops = container_of(head, struct fib_rules_ops, rcu);
161 struct net *net = ops->fro_net;
162
163 release_net(net);
164 kfree(ops);
165}
166
Denis V. Lunev9e3a5482008-01-20 16:46:41 -0800167void fib_rules_unregister(struct fib_rules_ops *ops)
Thomas Graf14c0b972006-08-04 03:38:38 -0700168{
Denis V. Lunev9e3a5482008-01-20 16:46:41 -0800169 struct net *net = ops->fro_net;
Thomas Graf14c0b972006-08-04 03:38:38 -0700170
Denis V. Lunev5fd30ee2008-01-10 03:20:28 -0800171 spin_lock(&net->rules_mod_lock);
Denis V. Lunev72132c1b2008-01-14 22:59:30 -0800172 list_del_rcu(&ops->list);
173 fib_rules_cleanup_ops(ops);
Denis V. Lunev5fd30ee2008-01-10 03:20:28 -0800174 spin_unlock(&net->rules_mod_lock);
Thomas Graf14c0b972006-08-04 03:38:38 -0700175
Eric W. Biedermane9c51582009-12-03 12:22:55 -0800176 call_rcu(&ops->rcu, fib_rules_put_rcu);
Thomas Graf14c0b972006-08-04 03:38:38 -0700177}
Thomas Graf14c0b972006-08-04 03:38:38 -0700178EXPORT_SYMBOL_GPL(fib_rules_unregister);
179
Thomas Graf3dfbcc42006-11-09 15:23:20 -0800180static int fib_rule_match(struct fib_rule *rule, struct fib_rules_ops *ops,
181 struct flowi *fl, int flags)
182{
183 int ret = 0;
184
David S. Miller1d28f422011-03-12 00:29:39 -0500185 if (rule->iifindex && (rule->iifindex != fl->flowi_iif))
Thomas Graf3dfbcc42006-11-09 15:23:20 -0800186 goto out;
187
David S. Miller1d28f422011-03-12 00:29:39 -0500188 if (rule->oifindex && (rule->oifindex != fl->flowi_oif))
Patrick McHardy1b038a52009-12-03 01:25:56 +0000189 goto out;
190
David S. Miller1d28f422011-03-12 00:29:39 -0500191 if ((rule->mark ^ fl->flowi_mark) & rule->mark_mask)
Thomas Graf3dfbcc42006-11-09 15:23:20 -0800192 goto out;
193
194 ret = ops->match(rule, fl, flags);
195out:
196 return (rule->flags & FIB_RULE_INVERT) ? !ret : ret;
197}
198
Thomas Graf14c0b972006-08-04 03:38:38 -0700199int fib_rules_lookup(struct fib_rules_ops *ops, struct flowi *fl,
200 int flags, struct fib_lookup_arg *arg)
201{
202 struct fib_rule *rule;
203 int err;
204
205 rcu_read_lock();
206
Denis V. Lunev76c72d42007-09-16 15:44:27 -0700207 list_for_each_entry_rcu(rule, &ops->rules_list, list) {
Thomas Graf0947c9fe2007-03-26 17:14:15 -0700208jumped:
Thomas Graf3dfbcc42006-11-09 15:23:20 -0800209 if (!fib_rule_match(rule, ops, fl, flags))
Thomas Graf14c0b972006-08-04 03:38:38 -0700210 continue;
211
Thomas Graf0947c9fe2007-03-26 17:14:15 -0700212 if (rule->action == FR_ACT_GOTO) {
213 struct fib_rule *target;
214
215 target = rcu_dereference(rule->ctarget);
216 if (target == NULL) {
217 continue;
218 } else {
219 rule = target;
220 goto jumped;
221 }
Thomas Graffa0b2d12007-03-26 17:38:53 -0700222 } else if (rule->action == FR_ACT_NOP)
223 continue;
224 else
Thomas Graf0947c9fe2007-03-26 17:14:15 -0700225 err = ops->action(rule, fl, flags, arg);
226
Thomas Graf14c0b972006-08-04 03:38:38 -0700227 if (err != -EAGAIN) {
Eric Dumazetebc0ffa2010-10-05 10:41:36 +0000228 if ((arg->flags & FIB_LOOKUP_NOREF) ||
229 likely(atomic_inc_not_zero(&rule->refcnt))) {
Eric Dumazet7fa7cb72010-09-27 04:18:27 +0000230 arg->rule = rule;
231 goto out;
232 }
233 break;
Thomas Graf14c0b972006-08-04 03:38:38 -0700234 }
235 }
236
Steven Whitehouse83886b62007-03-30 13:34:27 -0700237 err = -ESRCH;
Thomas Graf14c0b972006-08-04 03:38:38 -0700238out:
239 rcu_read_unlock();
240
241 return err;
242}
Thomas Graf14c0b972006-08-04 03:38:38 -0700243EXPORT_SYMBOL_GPL(fib_rules_lookup);
244
Thomas Grafe1701c62007-03-24 12:46:02 -0700245static int validate_rulemsg(struct fib_rule_hdr *frh, struct nlattr **tb,
246 struct fib_rules_ops *ops)
247{
248 int err = -EINVAL;
249
250 if (frh->src_len)
251 if (tb[FRA_SRC] == NULL ||
252 frh->src_len > (ops->addr_size * 8) ||
253 nla_len(tb[FRA_SRC]) != ops->addr_size)
254 goto errout;
255
256 if (frh->dst_len)
257 if (tb[FRA_DST] == NULL ||
258 frh->dst_len > (ops->addr_size * 8) ||
259 nla_len(tb[FRA_DST]) != ops->addr_size)
260 goto errout;
261
262 err = 0;
263errout:
264 return err;
265}
266
Thomas Graf9d9e6a52007-03-25 23:20:05 -0700267static int fib_nl_newrule(struct sk_buff *skb, struct nlmsghdr* nlh, void *arg)
Thomas Graf14c0b972006-08-04 03:38:38 -0700268{
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +0900269 struct net *net = sock_net(skb->sk);
Thomas Graf14c0b972006-08-04 03:38:38 -0700270 struct fib_rule_hdr *frh = nlmsg_data(nlh);
271 struct fib_rules_ops *ops = NULL;
272 struct fib_rule *rule, *r, *last = NULL;
273 struct nlattr *tb[FRA_MAX+1];
Thomas Graf0947c9fe2007-03-26 17:14:15 -0700274 int err = -EINVAL, unresolved = 0;
Thomas Graf14c0b972006-08-04 03:38:38 -0700275
276 if (nlh->nlmsg_len < nlmsg_msg_size(sizeof(*frh)))
277 goto errout;
278
Denis V. Lunev5fd30ee2008-01-10 03:20:28 -0800279 ops = lookup_rules_ops(net, frh->family);
Thomas Graf14c0b972006-08-04 03:38:38 -0700280 if (ops == NULL) {
Patrick McHardy2fe195c2008-07-01 19:59:37 -0700281 err = -EAFNOSUPPORT;
Thomas Graf14c0b972006-08-04 03:38:38 -0700282 goto errout;
283 }
284
285 err = nlmsg_parse(nlh, sizeof(*frh), tb, FRA_MAX, ops->policy);
286 if (err < 0)
287 goto errout;
288
Thomas Grafe1701c62007-03-24 12:46:02 -0700289 err = validate_rulemsg(frh, tb, ops);
290 if (err < 0)
291 goto errout;
292
Thomas Graf14c0b972006-08-04 03:38:38 -0700293 rule = kzalloc(ops->rule_size, GFP_KERNEL);
294 if (rule == NULL) {
295 err = -ENOMEM;
296 goto errout;
297 }
Denis V. Lunev3661a912008-04-16 02:01:56 -0700298 rule->fr_net = hold_net(net);
Thomas Graf14c0b972006-08-04 03:38:38 -0700299
300 if (tb[FRA_PRIORITY])
301 rule->pref = nla_get_u32(tb[FRA_PRIORITY]);
302
Patrick McHardy491deb24b2009-12-03 01:25:54 +0000303 if (tb[FRA_IIFNAME]) {
Thomas Graf14c0b972006-08-04 03:38:38 -0700304 struct net_device *dev;
305
Patrick McHardy491deb24b2009-12-03 01:25:54 +0000306 rule->iifindex = -1;
307 nla_strlcpy(rule->iifname, tb[FRA_IIFNAME], IFNAMSIZ);
308 dev = __dev_get_by_name(net, rule->iifname);
Thomas Graf14c0b972006-08-04 03:38:38 -0700309 if (dev)
Patrick McHardy491deb24b2009-12-03 01:25:54 +0000310 rule->iifindex = dev->ifindex;
Thomas Graf14c0b972006-08-04 03:38:38 -0700311 }
312
Patrick McHardy1b038a52009-12-03 01:25:56 +0000313 if (tb[FRA_OIFNAME]) {
314 struct net_device *dev;
315
316 rule->oifindex = -1;
317 nla_strlcpy(rule->oifname, tb[FRA_OIFNAME], IFNAMSIZ);
318 dev = __dev_get_by_name(net, rule->oifname);
319 if (dev)
320 rule->oifindex = dev->ifindex;
321 }
322
Thomas Grafb8964ed2006-11-09 15:22:18 -0800323 if (tb[FRA_FWMARK]) {
324 rule->mark = nla_get_u32(tb[FRA_FWMARK]);
325 if (rule->mark)
326 /* compatibility: if the mark value is non-zero all bits
327 * are compared unless a mask is explicitly specified.
328 */
329 rule->mark_mask = 0xFFFFFFFF;
330 }
331
332 if (tb[FRA_FWMASK])
333 rule->mark_mask = nla_get_u32(tb[FRA_FWMASK]);
334
Thomas Graf14c0b972006-08-04 03:38:38 -0700335 rule->action = frh->action;
336 rule->flags = frh->flags;
Patrick McHardy9e762a42006-08-10 23:09:48 -0700337 rule->table = frh_get_table(frh, tb);
Thomas Graf14c0b972006-08-04 03:38:38 -0700338
Patrick McHardy5adef182009-12-03 01:25:57 +0000339 if (!tb[FRA_PRIORITY] && ops->default_pref)
Denis V. Lunev868d13a2008-01-10 03:18:25 -0800340 rule->pref = ops->default_pref(ops);
Thomas Graf14c0b972006-08-04 03:38:38 -0700341
Thomas Graf0947c9fe2007-03-26 17:14:15 -0700342 err = -EINVAL;
343 if (tb[FRA_GOTO]) {
344 if (rule->action != FR_ACT_GOTO)
345 goto errout_free;
346
347 rule->target = nla_get_u32(tb[FRA_GOTO]);
348 /* Backward jumps are prohibited to avoid endless loops */
349 if (rule->target <= rule->pref)
350 goto errout_free;
351
Denis V. Lunev76c72d42007-09-16 15:44:27 -0700352 list_for_each_entry(r, &ops->rules_list, list) {
Thomas Graf0947c9fe2007-03-26 17:14:15 -0700353 if (r->pref == rule->target) {
Eric Dumazet7a2b03c2010-10-26 09:24:55 +0000354 RCU_INIT_POINTER(rule->ctarget, r);
Thomas Graf0947c9fe2007-03-26 17:14:15 -0700355 break;
356 }
357 }
358
Eric Dumazet7a2b03c2010-10-26 09:24:55 +0000359 if (rcu_dereference_protected(rule->ctarget, 1) == NULL)
Thomas Graf0947c9fe2007-03-26 17:14:15 -0700360 unresolved = 1;
361 } else if (rule->action == FR_ACT_GOTO)
362 goto errout_free;
363
Rami Rosen8b3521e2009-05-11 05:52:49 +0000364 err = ops->configure(rule, skb, frh, tb);
Thomas Graf14c0b972006-08-04 03:38:38 -0700365 if (err < 0)
366 goto errout_free;
367
Denis V. Lunev76c72d42007-09-16 15:44:27 -0700368 list_for_each_entry(r, &ops->rules_list, list) {
Thomas Graf14c0b972006-08-04 03:38:38 -0700369 if (r->pref > rule->pref)
370 break;
371 last = r;
372 }
373
374 fib_rule_get(rule);
375
Eric Dumazetebb9fed2010-10-23 09:44:25 +0000376 if (last)
377 list_add_rcu(&rule->list, &last->list);
378 else
379 list_add_rcu(&rule->list, &ops->rules_list);
380
Thomas Graf0947c9fe2007-03-26 17:14:15 -0700381 if (ops->unresolved_rules) {
382 /*
383 * There are unresolved goto rules in the list, check if
384 * any of them are pointing to this new rule.
385 */
Denis V. Lunev76c72d42007-09-16 15:44:27 -0700386 list_for_each_entry(r, &ops->rules_list, list) {
Thomas Graf0947c9fe2007-03-26 17:14:15 -0700387 if (r->action == FR_ACT_GOTO &&
Gao feng561dac22011-09-11 15:36:05 +0000388 r->target == rule->pref &&
389 rtnl_dereference(r->ctarget) == NULL) {
Thomas Graf0947c9fe2007-03-26 17:14:15 -0700390 rcu_assign_pointer(r->ctarget, rule);
391 if (--ops->unresolved_rules == 0)
392 break;
393 }
394 }
395 }
396
397 if (rule->action == FR_ACT_GOTO)
398 ops->nr_goto_rules++;
399
400 if (unresolved)
401 ops->unresolved_rules++;
402
Denis V. Lunev9e3a5482008-01-20 16:46:41 -0800403 notify_rule_change(RTM_NEWRULE, rule, ops, nlh, NETLINK_CB(skb).pid);
Thomas Graf73417f62007-03-27 13:56:52 -0700404 flush_route_cache(ops);
Thomas Graf14c0b972006-08-04 03:38:38 -0700405 rules_ops_put(ops);
406 return 0;
407
408errout_free:
Denis V. Lunev3661a912008-04-16 02:01:56 -0700409 release_net(rule->fr_net);
Thomas Graf14c0b972006-08-04 03:38:38 -0700410 kfree(rule);
411errout:
412 rules_ops_put(ops);
413 return err;
414}
415
Thomas Graf9d9e6a52007-03-25 23:20:05 -0700416static int fib_nl_delrule(struct sk_buff *skb, struct nlmsghdr* nlh, void *arg)
Thomas Graf14c0b972006-08-04 03:38:38 -0700417{
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +0900418 struct net *net = sock_net(skb->sk);
Thomas Graf14c0b972006-08-04 03:38:38 -0700419 struct fib_rule_hdr *frh = nlmsg_data(nlh);
420 struct fib_rules_ops *ops = NULL;
Thomas Graf0947c9fe2007-03-26 17:14:15 -0700421 struct fib_rule *rule, *tmp;
Thomas Graf14c0b972006-08-04 03:38:38 -0700422 struct nlattr *tb[FRA_MAX+1];
423 int err = -EINVAL;
424
425 if (nlh->nlmsg_len < nlmsg_msg_size(sizeof(*frh)))
426 goto errout;
427
Denis V. Lunev5fd30ee2008-01-10 03:20:28 -0800428 ops = lookup_rules_ops(net, frh->family);
Thomas Graf14c0b972006-08-04 03:38:38 -0700429 if (ops == NULL) {
Patrick McHardy2fe195c2008-07-01 19:59:37 -0700430 err = -EAFNOSUPPORT;
Thomas Graf14c0b972006-08-04 03:38:38 -0700431 goto errout;
432 }
433
434 err = nlmsg_parse(nlh, sizeof(*frh), tb, FRA_MAX, ops->policy);
435 if (err < 0)
436 goto errout;
437
Thomas Grafe1701c62007-03-24 12:46:02 -0700438 err = validate_rulemsg(frh, tb, ops);
439 if (err < 0)
440 goto errout;
441
Denis V. Lunev76c72d42007-09-16 15:44:27 -0700442 list_for_each_entry(rule, &ops->rules_list, list) {
Thomas Graf14c0b972006-08-04 03:38:38 -0700443 if (frh->action && (frh->action != rule->action))
444 continue;
445
Patrick McHardy9e762a42006-08-10 23:09:48 -0700446 if (frh->table && (frh_get_table(frh, tb) != rule->table))
Thomas Graf14c0b972006-08-04 03:38:38 -0700447 continue;
448
449 if (tb[FRA_PRIORITY] &&
450 (rule->pref != nla_get_u32(tb[FRA_PRIORITY])))
451 continue;
452
Patrick McHardy491deb24b2009-12-03 01:25:54 +0000453 if (tb[FRA_IIFNAME] &&
454 nla_strcmp(tb[FRA_IIFNAME], rule->iifname))
Thomas Graf14c0b972006-08-04 03:38:38 -0700455 continue;
456
Patrick McHardy1b038a52009-12-03 01:25:56 +0000457 if (tb[FRA_OIFNAME] &&
458 nla_strcmp(tb[FRA_OIFNAME], rule->oifname))
459 continue;
460
Thomas Grafb8964ed2006-11-09 15:22:18 -0800461 if (tb[FRA_FWMARK] &&
462 (rule->mark != nla_get_u32(tb[FRA_FWMARK])))
463 continue;
464
465 if (tb[FRA_FWMASK] &&
466 (rule->mark_mask != nla_get_u32(tb[FRA_FWMASK])))
467 continue;
468
Thomas Graf14c0b972006-08-04 03:38:38 -0700469 if (!ops->compare(rule, frh, tb))
470 continue;
471
472 if (rule->flags & FIB_RULE_PERMANENT) {
473 err = -EPERM;
474 goto errout;
475 }
476
477 list_del_rcu(&rule->list);
Thomas Graf0947c9fe2007-03-26 17:14:15 -0700478
Yan, Zhengafaef732011-10-17 15:20:28 +0000479 if (rule->action == FR_ACT_GOTO) {
Thomas Graf0947c9fe2007-03-26 17:14:15 -0700480 ops->nr_goto_rules--;
Yan, Zhengafaef732011-10-17 15:20:28 +0000481 if (rtnl_dereference(rule->ctarget) == NULL)
482 ops->unresolved_rules--;
483 }
Thomas Graf0947c9fe2007-03-26 17:14:15 -0700484
485 /*
486 * Check if this rule is a target to any of them. If so,
487 * disable them. As this operation is eventually very
488 * expensive, it is only performed if goto rules have
489 * actually been added.
490 */
491 if (ops->nr_goto_rules > 0) {
Denis V. Lunev76c72d42007-09-16 15:44:27 -0700492 list_for_each_entry(tmp, &ops->rules_list, list) {
Eric Dumazet7a2b03c2010-10-26 09:24:55 +0000493 if (rtnl_dereference(tmp->ctarget) == rule) {
Stephen Hemmingera9b3cd72011-08-01 16:19:00 +0000494 RCU_INIT_POINTER(tmp->ctarget, NULL);
Thomas Graf0947c9fe2007-03-26 17:14:15 -0700495 ops->unresolved_rules++;
496 }
497 }
498 }
499
Denis V. Lunev9e3a5482008-01-20 16:46:41 -0800500 notify_rule_change(RTM_DELRULE, rule, ops, nlh,
Thomas Grafc17084d2006-08-15 00:32:48 -0700501 NETLINK_CB(skb).pid);
Thomas Graf14c0b972006-08-04 03:38:38 -0700502 fib_rule_put(rule);
Thomas Graf73417f62007-03-27 13:56:52 -0700503 flush_route_cache(ops);
Thomas Graf14c0b972006-08-04 03:38:38 -0700504 rules_ops_put(ops);
505 return 0;
506 }
507
508 err = -ENOENT;
509errout:
510 rules_ops_put(ops);
511 return err;
512}
513
Thomas Graf339bf982006-11-10 14:10:15 -0800514static inline size_t fib_rule_nlmsg_size(struct fib_rules_ops *ops,
515 struct fib_rule *rule)
516{
517 size_t payload = NLMSG_ALIGN(sizeof(struct fib_rule_hdr))
Patrick McHardy491deb24b2009-12-03 01:25:54 +0000518 + nla_total_size(IFNAMSIZ) /* FRA_IIFNAME */
Patrick McHardy1b038a52009-12-03 01:25:56 +0000519 + nla_total_size(IFNAMSIZ) /* FRA_OIFNAME */
Thomas Graf339bf982006-11-10 14:10:15 -0800520 + nla_total_size(4) /* FRA_PRIORITY */
521 + nla_total_size(4) /* FRA_TABLE */
522 + nla_total_size(4) /* FRA_FWMARK */
523 + nla_total_size(4); /* FRA_FWMASK */
524
525 if (ops->nlmsg_payload)
526 payload += ops->nlmsg_payload(rule);
527
528 return payload;
529}
530
Thomas Graf14c0b972006-08-04 03:38:38 -0700531static int fib_nl_fill_rule(struct sk_buff *skb, struct fib_rule *rule,
532 u32 pid, u32 seq, int type, int flags,
533 struct fib_rules_ops *ops)
534{
535 struct nlmsghdr *nlh;
536 struct fib_rule_hdr *frh;
537
538 nlh = nlmsg_put(skb, pid, seq, type, sizeof(*frh), flags);
539 if (nlh == NULL)
Patrick McHardy26932562007-01-31 23:16:40 -0800540 return -EMSGSIZE;
Thomas Graf14c0b972006-08-04 03:38:38 -0700541
542 frh = nlmsg_data(nlh);
Patrick McHardy28bb1722010-04-13 05:03:16 +0000543 frh->family = ops->family;
Thomas Graf14c0b972006-08-04 03:38:38 -0700544 frh->table = rule->table;
Patrick McHardy9e762a42006-08-10 23:09:48 -0700545 NLA_PUT_U32(skb, FRA_TABLE, rule->table);
Thomas Graf14c0b972006-08-04 03:38:38 -0700546 frh->res1 = 0;
547 frh->res2 = 0;
548 frh->action = rule->action;
549 frh->flags = rule->flags;
550
Eric Dumazet7a2b03c2010-10-26 09:24:55 +0000551 if (rule->action == FR_ACT_GOTO &&
Eric Dumazet33d480c2011-08-11 19:30:52 +0000552 rcu_access_pointer(rule->ctarget) == NULL)
Thomas Graf0947c9fe2007-03-26 17:14:15 -0700553 frh->flags |= FIB_RULE_UNRESOLVED;
554
Patrick McHardy491deb24b2009-12-03 01:25:54 +0000555 if (rule->iifname[0]) {
556 NLA_PUT_STRING(skb, FRA_IIFNAME, rule->iifname);
Thomas Graf14c0b972006-08-04 03:38:38 -0700557
Patrick McHardy491deb24b2009-12-03 01:25:54 +0000558 if (rule->iifindex == -1)
559 frh->flags |= FIB_RULE_IIF_DETACHED;
Thomas Graf2b443682007-03-26 17:37:59 -0700560 }
561
Patrick McHardy1b038a52009-12-03 01:25:56 +0000562 if (rule->oifname[0]) {
563 NLA_PUT_STRING(skb, FRA_OIFNAME, rule->oifname);
564
565 if (rule->oifindex == -1)
566 frh->flags |= FIB_RULE_OIF_DETACHED;
567 }
568
Thomas Graf14c0b972006-08-04 03:38:38 -0700569 if (rule->pref)
570 NLA_PUT_U32(skb, FRA_PRIORITY, rule->pref);
571
Thomas Grafb8964ed2006-11-09 15:22:18 -0800572 if (rule->mark)
573 NLA_PUT_U32(skb, FRA_FWMARK, rule->mark);
574
575 if (rule->mark_mask || rule->mark)
576 NLA_PUT_U32(skb, FRA_FWMASK, rule->mark_mask);
577
Thomas Graf0947c9fe2007-03-26 17:14:15 -0700578 if (rule->target)
579 NLA_PUT_U32(skb, FRA_GOTO, rule->target);
580
Rami Rosen04af8cf2009-05-20 17:26:23 -0700581 if (ops->fill(rule, skb, frh) < 0)
Thomas Graf14c0b972006-08-04 03:38:38 -0700582 goto nla_put_failure;
583
584 return nlmsg_end(skb, nlh);
585
586nla_put_failure:
Patrick McHardy26932562007-01-31 23:16:40 -0800587 nlmsg_cancel(skb, nlh);
588 return -EMSGSIZE;
Thomas Graf14c0b972006-08-04 03:38:38 -0700589}
590
Thomas Grafc4546732007-03-25 23:24:24 -0700591static int dump_rules(struct sk_buff *skb, struct netlink_callback *cb,
592 struct fib_rules_ops *ops)
Thomas Graf14c0b972006-08-04 03:38:38 -0700593{
594 int idx = 0;
595 struct fib_rule *rule;
Thomas Graf14c0b972006-08-04 03:38:38 -0700596
Eric Dumazete67f88d2011-04-27 22:56:07 +0000597 rcu_read_lock();
598 list_for_each_entry_rcu(rule, &ops->rules_list, list) {
Thomas Grafc4546732007-03-25 23:24:24 -0700599 if (idx < cb->args[1])
Thomas Graf14c0b972006-08-04 03:38:38 -0700600 goto skip;
601
602 if (fib_nl_fill_rule(skb, rule, NETLINK_CB(cb->skb).pid,
603 cb->nlh->nlmsg_seq, RTM_NEWRULE,
604 NLM_F_MULTI, ops) < 0)
605 break;
606skip:
607 idx++;
608 }
Eric Dumazet2907c352011-05-25 07:34:04 +0000609 rcu_read_unlock();
Thomas Grafc4546732007-03-25 23:24:24 -0700610 cb->args[1] = idx;
Thomas Graf14c0b972006-08-04 03:38:38 -0700611 rules_ops_put(ops);
612
613 return skb->len;
614}
615
Thomas Grafc4546732007-03-25 23:24:24 -0700616static int fib_nl_dumprule(struct sk_buff *skb, struct netlink_callback *cb)
617{
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +0900618 struct net *net = sock_net(skb->sk);
Thomas Grafc4546732007-03-25 23:24:24 -0700619 struct fib_rules_ops *ops;
620 int idx = 0, family;
621
622 family = rtnl_msg_family(cb->nlh);
623 if (family != AF_UNSPEC) {
624 /* Protocol specific dump request */
Denis V. Lunev5fd30ee2008-01-10 03:20:28 -0800625 ops = lookup_rules_ops(net, family);
Thomas Grafc4546732007-03-25 23:24:24 -0700626 if (ops == NULL)
627 return -EAFNOSUPPORT;
628
629 return dump_rules(skb, cb, ops);
630 }
631
632 rcu_read_lock();
Denis V. Lunev5fd30ee2008-01-10 03:20:28 -0800633 list_for_each_entry_rcu(ops, &net->rules_ops, list) {
Thomas Grafc4546732007-03-25 23:24:24 -0700634 if (idx < cb->args[0] || !try_module_get(ops->owner))
635 goto skip;
636
637 if (dump_rules(skb, cb, ops) < 0)
638 break;
639
640 cb->args[1] = 0;
Eric Dumazet2fb35732010-03-09 20:03:38 +0000641skip:
Thomas Grafc4546732007-03-25 23:24:24 -0700642 idx++;
643 }
644 rcu_read_unlock();
645 cb->args[0] = idx;
646
647 return skb->len;
648}
Thomas Graf14c0b972006-08-04 03:38:38 -0700649
Denis V. Lunev9e3a5482008-01-20 16:46:41 -0800650static void notify_rule_change(int event, struct fib_rule *rule,
Thomas Grafc17084d2006-08-15 00:32:48 -0700651 struct fib_rules_ops *ops, struct nlmsghdr *nlh,
652 u32 pid)
Thomas Graf14c0b972006-08-04 03:38:38 -0700653{
Denis V. Lunev9e3a5482008-01-20 16:46:41 -0800654 struct net *net;
Thomas Grafc17084d2006-08-15 00:32:48 -0700655 struct sk_buff *skb;
656 int err = -ENOBUFS;
Thomas Graf14c0b972006-08-04 03:38:38 -0700657
Denis V. Lunev9e3a5482008-01-20 16:46:41 -0800658 net = ops->fro_net;
Thomas Graf339bf982006-11-10 14:10:15 -0800659 skb = nlmsg_new(fib_rule_nlmsg_size(ops, rule), GFP_KERNEL);
Thomas Graf14c0b972006-08-04 03:38:38 -0700660 if (skb == NULL)
Thomas Grafc17084d2006-08-15 00:32:48 -0700661 goto errout;
662
663 err = fib_nl_fill_rule(skb, rule, pid, nlh->nlmsg_seq, event, 0, ops);
Patrick McHardy26932562007-01-31 23:16:40 -0800664 if (err < 0) {
665 /* -EMSGSIZE implies BUG in fib_rule_nlmsg_size() */
666 WARN_ON(err == -EMSGSIZE);
667 kfree_skb(skb);
668 goto errout;
669 }
Denis V. Lunev9e3a5482008-01-20 16:46:41 -0800670
Pablo Neira Ayuso1ce85fe2009-02-24 23:18:28 -0800671 rtnl_notify(skb, net, pid, ops->nlgroup, nlh, GFP_KERNEL);
672 return;
Thomas Grafc17084d2006-08-15 00:32:48 -0700673errout:
674 if (err < 0)
Denis V. Lunev5fd30ee2008-01-10 03:20:28 -0800675 rtnl_set_sk_err(net, ops->nlgroup, err);
Thomas Graf14c0b972006-08-04 03:38:38 -0700676}
677
678static void attach_rules(struct list_head *rules, struct net_device *dev)
679{
680 struct fib_rule *rule;
681
682 list_for_each_entry(rule, rules, list) {
Patrick McHardy491deb24b2009-12-03 01:25:54 +0000683 if (rule->iifindex == -1 &&
684 strcmp(dev->name, rule->iifname) == 0)
685 rule->iifindex = dev->ifindex;
Patrick McHardy1b038a52009-12-03 01:25:56 +0000686 if (rule->oifindex == -1 &&
687 strcmp(dev->name, rule->oifname) == 0)
688 rule->oifindex = dev->ifindex;
Thomas Graf14c0b972006-08-04 03:38:38 -0700689 }
690}
691
692static void detach_rules(struct list_head *rules, struct net_device *dev)
693{
694 struct fib_rule *rule;
695
Patrick McHardy1b038a52009-12-03 01:25:56 +0000696 list_for_each_entry(rule, rules, list) {
Patrick McHardy491deb24b2009-12-03 01:25:54 +0000697 if (rule->iifindex == dev->ifindex)
698 rule->iifindex = -1;
Patrick McHardy1b038a52009-12-03 01:25:56 +0000699 if (rule->oifindex == dev->ifindex)
700 rule->oifindex = -1;
701 }
Thomas Graf14c0b972006-08-04 03:38:38 -0700702}
703
704
705static int fib_rules_event(struct notifier_block *this, unsigned long event,
706 void *ptr)
707{
708 struct net_device *dev = ptr;
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +0900709 struct net *net = dev_net(dev);
Thomas Graf14c0b972006-08-04 03:38:38 -0700710 struct fib_rules_ops *ops;
711
712 ASSERT_RTNL();
Thomas Graf14c0b972006-08-04 03:38:38 -0700713
714 switch (event) {
715 case NETDEV_REGISTER:
Denis V. Lunev5fd30ee2008-01-10 03:20:28 -0800716 list_for_each_entry(ops, &net->rules_ops, list)
Denis V. Lunev76c72d42007-09-16 15:44:27 -0700717 attach_rules(&ops->rules_list, dev);
Thomas Graf14c0b972006-08-04 03:38:38 -0700718 break;
719
720 case NETDEV_UNREGISTER:
Denis V. Lunev5fd30ee2008-01-10 03:20:28 -0800721 list_for_each_entry(ops, &net->rules_ops, list)
Denis V. Lunev76c72d42007-09-16 15:44:27 -0700722 detach_rules(&ops->rules_list, dev);
Thomas Graf14c0b972006-08-04 03:38:38 -0700723 break;
724 }
725
Thomas Graf14c0b972006-08-04 03:38:38 -0700726 return NOTIFY_DONE;
727}
728
729static struct notifier_block fib_rules_notifier = {
730 .notifier_call = fib_rules_event,
731};
732
Alexey Dobriyan2c8c1e72010-01-17 03:35:32 +0000733static int __net_init fib_rules_net_init(struct net *net)
Denis V. Lunev5fd30ee2008-01-10 03:20:28 -0800734{
735 INIT_LIST_HEAD(&net->rules_ops);
736 spin_lock_init(&net->rules_mod_lock);
737 return 0;
738}
739
740static struct pernet_operations fib_rules_net_ops = {
741 .init = fib_rules_net_init,
742};
743
Thomas Graf14c0b972006-08-04 03:38:38 -0700744static int __init fib_rules_init(void)
745{
Denis V. Lunev5fd30ee2008-01-10 03:20:28 -0800746 int err;
Greg Rosec7ac8672011-06-10 01:27:09 +0000747 rtnl_register(PF_UNSPEC, RTM_NEWRULE, fib_nl_newrule, NULL, NULL);
748 rtnl_register(PF_UNSPEC, RTM_DELRULE, fib_nl_delrule, NULL, NULL);
749 rtnl_register(PF_UNSPEC, RTM_GETRULE, NULL, fib_nl_dumprule, NULL);
Thomas Graf9d9e6a52007-03-25 23:20:05 -0700750
Eric W. Biederman5d6d4802008-11-07 22:52:34 -0800751 err = register_pernet_subsys(&fib_rules_net_ops);
Denis V. Lunev5fd30ee2008-01-10 03:20:28 -0800752 if (err < 0)
753 goto fail;
754
Eric W. Biederman5d6d4802008-11-07 22:52:34 -0800755 err = register_netdevice_notifier(&fib_rules_notifier);
Denis V. Lunev5fd30ee2008-01-10 03:20:28 -0800756 if (err < 0)
757 goto fail_unregister;
Eric W. Biederman5d6d4802008-11-07 22:52:34 -0800758
Denis V. Lunev5fd30ee2008-01-10 03:20:28 -0800759 return 0;
760
761fail_unregister:
Eric W. Biederman5d6d4802008-11-07 22:52:34 -0800762 unregister_pernet_subsys(&fib_rules_net_ops);
Denis V. Lunev5fd30ee2008-01-10 03:20:28 -0800763fail:
764 rtnl_unregister(PF_UNSPEC, RTM_NEWRULE);
765 rtnl_unregister(PF_UNSPEC, RTM_DELRULE);
766 rtnl_unregister(PF_UNSPEC, RTM_GETRULE);
767 return err;
Thomas Graf14c0b972006-08-04 03:38:38 -0700768}
769
770subsys_initcall(fib_rules_init);