blob: 57e8f95110e6c39d0c562bbda5e3e140897765d3 [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>
Eric W. Biedermane9dc8652007-09-12 13:02:17 +020015#include <net/net_namespace.h>
Eric W. Biederman881d9662007-09-17 11:56:21 -070016#include <net/sock.h>
Thomas Graf14c0b972006-08-04 03:38:38 -070017#include <net/fib_rules.h>
18
Denis V. Lunev2994c632007-11-10 22:12:03 -080019int fib_default_rule_add(struct fib_rules_ops *ops,
20 u32 pref, u32 table, u32 flags)
21{
22 struct fib_rule *r;
23
24 r = kzalloc(ops->rule_size, GFP_KERNEL);
25 if (r == NULL)
26 return -ENOMEM;
27
28 atomic_set(&r->refcnt, 1);
29 r->action = FR_ACT_TO_TBL;
30 r->pref = pref;
31 r->table = table;
32 r->flags = flags;
Denis V. Lunev3661a912008-04-16 02:01:56 -070033 r->fr_net = hold_net(ops->fro_net);
Denis V. Lunev2994c632007-11-10 22:12:03 -080034
35 /* The lock is not required here, the list in unreacheable
36 * at the moment this function is called */
37 list_add_tail(&r->list, &ops->rules_list);
38 return 0;
39}
40EXPORT_SYMBOL(fib_default_rule_add);
41
Patrick McHardyd8a566b2010-04-13 05:03:15 +000042u32 fib_default_rule_pref(struct fib_rules_ops *ops)
43{
44 struct list_head *pos;
45 struct fib_rule *rule;
46
47 if (!list_empty(&ops->rules_list)) {
48 pos = ops->rules_list.next;
49 if (pos->next != &ops->rules_list) {
50 rule = list_entry(pos->next, struct fib_rule, list);
51 if (rule->pref)
52 return rule->pref - 1;
53 }
54 }
55
56 return 0;
57}
58EXPORT_SYMBOL(fib_default_rule_pref);
59
Denis V. Lunev9e3a5482008-01-20 16:46:41 -080060static void notify_rule_change(int event, struct fib_rule *rule,
Thomas Grafc17084d2006-08-15 00:32:48 -070061 struct fib_rules_ops *ops, struct nlmsghdr *nlh,
62 u32 pid);
Thomas Graf14c0b972006-08-04 03:38:38 -070063
Denis V. Lunev5fd30ee2008-01-10 03:20:28 -080064static struct fib_rules_ops *lookup_rules_ops(struct net *net, int family)
Thomas Graf14c0b972006-08-04 03:38:38 -070065{
66 struct fib_rules_ops *ops;
67
68 rcu_read_lock();
Denis V. Lunev5fd30ee2008-01-10 03:20:28 -080069 list_for_each_entry_rcu(ops, &net->rules_ops, list) {
Thomas Graf14c0b972006-08-04 03:38:38 -070070 if (ops->family == family) {
71 if (!try_module_get(ops->owner))
72 ops = NULL;
73 rcu_read_unlock();
74 return ops;
75 }
76 }
77 rcu_read_unlock();
78
79 return NULL;
80}
81
82static void rules_ops_put(struct fib_rules_ops *ops)
83{
84 if (ops)
85 module_put(ops->owner);
86}
87
Thomas Graf73417f62007-03-27 13:56:52 -070088static void flush_route_cache(struct fib_rules_ops *ops)
89{
90 if (ops->flush_cache)
Denis V. Lunevae299fc2008-07-05 19:01:28 -070091 ops->flush_cache(ops);
Thomas Graf73417f62007-03-27 13:56:52 -070092}
93
Eric W. Biedermane9c51582009-12-03 12:22:55 -080094static int __fib_rules_register(struct fib_rules_ops *ops)
Thomas Graf14c0b972006-08-04 03:38:38 -070095{
96 int err = -EEXIST;
97 struct fib_rules_ops *o;
Denis V. Lunev9e3a5482008-01-20 16:46:41 -080098 struct net *net;
99
100 net = ops->fro_net;
Thomas Graf14c0b972006-08-04 03:38:38 -0700101
102 if (ops->rule_size < sizeof(struct fib_rule))
103 return -EINVAL;
104
105 if (ops->match == NULL || ops->configure == NULL ||
106 ops->compare == NULL || ops->fill == NULL ||
107 ops->action == NULL)
108 return -EINVAL;
109
Denis V. Lunev5fd30ee2008-01-10 03:20:28 -0800110 spin_lock(&net->rules_mod_lock);
111 list_for_each_entry(o, &net->rules_ops, list)
Thomas Graf14c0b972006-08-04 03:38:38 -0700112 if (ops->family == o->family)
113 goto errout;
114
Denis V. Lunev5fd30ee2008-01-10 03:20:28 -0800115 hold_net(net);
116 list_add_tail_rcu(&ops->list, &net->rules_ops);
Thomas Graf14c0b972006-08-04 03:38:38 -0700117 err = 0;
118errout:
Denis V. Lunev5fd30ee2008-01-10 03:20:28 -0800119 spin_unlock(&net->rules_mod_lock);
Thomas Graf14c0b972006-08-04 03:38:38 -0700120
121 return err;
122}
123
Eric W. Biedermane9c51582009-12-03 12:22:55 -0800124struct fib_rules_ops *
Patrick McHardy3d0c9c42010-04-26 16:02:04 +0200125fib_rules_register(const struct fib_rules_ops *tmpl, struct net *net)
Eric W. Biedermane9c51582009-12-03 12:22:55 -0800126{
127 struct fib_rules_ops *ops;
128 int err;
129
Eric Dumazet2fb35732010-03-09 20:03:38 +0000130 ops = kmemdup(tmpl, sizeof(*ops), GFP_KERNEL);
Eric W. Biedermane9c51582009-12-03 12:22:55 -0800131 if (ops == NULL)
132 return ERR_PTR(-ENOMEM);
133
134 INIT_LIST_HEAD(&ops->rules_list);
135 ops->fro_net = net;
136
137 err = __fib_rules_register(ops);
138 if (err) {
139 kfree(ops);
140 ops = ERR_PTR(err);
141 }
142
143 return ops;
144}
Thomas Graf14c0b972006-08-04 03:38:38 -0700145EXPORT_SYMBOL_GPL(fib_rules_register);
146
stephen hemminger1df99162010-10-04 20:14:17 +0000147static void fib_rules_cleanup_ops(struct fib_rules_ops *ops)
Thomas Graf14c0b972006-08-04 03:38:38 -0700148{
149 struct fib_rule *rule, *tmp;
150
Denis V. Lunev76c72d42007-09-16 15:44:27 -0700151 list_for_each_entry_safe(rule, tmp, &ops->rules_list, list) {
Thomas Graf14c0b972006-08-04 03:38:38 -0700152 list_del_rcu(&rule->list);
153 fib_rule_put(rule);
154 }
155}
156
Eric W. Biedermane9c51582009-12-03 12:22:55 -0800157static void fib_rules_put_rcu(struct rcu_head *head)
158{
159 struct fib_rules_ops *ops = container_of(head, struct fib_rules_ops, rcu);
160 struct net *net = ops->fro_net;
161
162 release_net(net);
163 kfree(ops);
164}
165
Denis V. Lunev9e3a5482008-01-20 16:46:41 -0800166void fib_rules_unregister(struct fib_rules_ops *ops)
Thomas Graf14c0b972006-08-04 03:38:38 -0700167{
Denis V. Lunev9e3a5482008-01-20 16:46:41 -0800168 struct net *net = ops->fro_net;
Thomas Graf14c0b972006-08-04 03:38:38 -0700169
Denis V. Lunev5fd30ee2008-01-10 03:20:28 -0800170 spin_lock(&net->rules_mod_lock);
Denis V. Lunev72132c1b2008-01-14 22:59:30 -0800171 list_del_rcu(&ops->list);
172 fib_rules_cleanup_ops(ops);
Denis V. Lunev5fd30ee2008-01-10 03:20:28 -0800173 spin_unlock(&net->rules_mod_lock);
Thomas Graf14c0b972006-08-04 03:38:38 -0700174
Eric W. Biedermane9c51582009-12-03 12:22:55 -0800175 call_rcu(&ops->rcu, fib_rules_put_rcu);
Thomas Graf14c0b972006-08-04 03:38:38 -0700176}
Thomas Graf14c0b972006-08-04 03:38:38 -0700177EXPORT_SYMBOL_GPL(fib_rules_unregister);
178
Thomas Graf3dfbcc42006-11-09 15:23:20 -0800179static int fib_rule_match(struct fib_rule *rule, struct fib_rules_ops *ops,
180 struct flowi *fl, int flags)
181{
182 int ret = 0;
183
David S. Miller1d28f422011-03-12 00:29:39 -0500184 if (rule->iifindex && (rule->iifindex != fl->flowi_iif))
Thomas Graf3dfbcc42006-11-09 15:23:20 -0800185 goto out;
186
David S. Miller1d28f422011-03-12 00:29:39 -0500187 if (rule->oifindex && (rule->oifindex != fl->flowi_oif))
Patrick McHardy1b038a52009-12-03 01:25:56 +0000188 goto out;
189
David S. Miller1d28f422011-03-12 00:29:39 -0500190 if ((rule->mark ^ fl->flowi_mark) & rule->mark_mask)
Thomas Graf3dfbcc42006-11-09 15:23:20 -0800191 goto out;
192
193 ret = ops->match(rule, fl, flags);
194out:
195 return (rule->flags & FIB_RULE_INVERT) ? !ret : ret;
196}
197
Thomas Graf14c0b972006-08-04 03:38:38 -0700198int fib_rules_lookup(struct fib_rules_ops *ops, struct flowi *fl,
199 int flags, struct fib_lookup_arg *arg)
200{
201 struct fib_rule *rule;
202 int err;
203
204 rcu_read_lock();
205
Denis V. Lunev76c72d42007-09-16 15:44:27 -0700206 list_for_each_entry_rcu(rule, &ops->rules_list, list) {
Thomas Graf0947c9fe2007-03-26 17:14:15 -0700207jumped:
Thomas Graf3dfbcc42006-11-09 15:23:20 -0800208 if (!fib_rule_match(rule, ops, fl, flags))
Thomas Graf14c0b972006-08-04 03:38:38 -0700209 continue;
210
Thomas Graf0947c9fe2007-03-26 17:14:15 -0700211 if (rule->action == FR_ACT_GOTO) {
212 struct fib_rule *target;
213
214 target = rcu_dereference(rule->ctarget);
215 if (target == NULL) {
216 continue;
217 } else {
218 rule = target;
219 goto jumped;
220 }
Thomas Graffa0b2d12007-03-26 17:38:53 -0700221 } else if (rule->action == FR_ACT_NOP)
222 continue;
223 else
Thomas Graf0947c9fe2007-03-26 17:14:15 -0700224 err = ops->action(rule, fl, flags, arg);
225
Thomas Graf14c0b972006-08-04 03:38:38 -0700226 if (err != -EAGAIN) {
Eric Dumazetebc0ffa2010-10-05 10:41:36 +0000227 if ((arg->flags & FIB_LOOKUP_NOREF) ||
228 likely(atomic_inc_not_zero(&rule->refcnt))) {
Eric Dumazet7fa7cb72010-09-27 04:18:27 +0000229 arg->rule = rule;
230 goto out;
231 }
232 break;
Thomas Graf14c0b972006-08-04 03:38:38 -0700233 }
234 }
235
Steven Whitehouse83886b62007-03-30 13:34:27 -0700236 err = -ESRCH;
Thomas Graf14c0b972006-08-04 03:38:38 -0700237out:
238 rcu_read_unlock();
239
240 return err;
241}
Thomas Graf14c0b972006-08-04 03:38:38 -0700242EXPORT_SYMBOL_GPL(fib_rules_lookup);
243
Thomas Grafe1701c62007-03-24 12:46:02 -0700244static int validate_rulemsg(struct fib_rule_hdr *frh, struct nlattr **tb,
245 struct fib_rules_ops *ops)
246{
247 int err = -EINVAL;
248
249 if (frh->src_len)
250 if (tb[FRA_SRC] == NULL ||
251 frh->src_len > (ops->addr_size * 8) ||
252 nla_len(tb[FRA_SRC]) != ops->addr_size)
253 goto errout;
254
255 if (frh->dst_len)
256 if (tb[FRA_DST] == NULL ||
257 frh->dst_len > (ops->addr_size * 8) ||
258 nla_len(tb[FRA_DST]) != ops->addr_size)
259 goto errout;
260
261 err = 0;
262errout:
263 return err;
264}
265
Thomas Graf9d9e6a52007-03-25 23:20:05 -0700266static int fib_nl_newrule(struct sk_buff *skb, struct nlmsghdr* nlh, void *arg)
Thomas Graf14c0b972006-08-04 03:38:38 -0700267{
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +0900268 struct net *net = sock_net(skb->sk);
Thomas Graf14c0b972006-08-04 03:38:38 -0700269 struct fib_rule_hdr *frh = nlmsg_data(nlh);
270 struct fib_rules_ops *ops = NULL;
271 struct fib_rule *rule, *r, *last = NULL;
272 struct nlattr *tb[FRA_MAX+1];
Thomas Graf0947c9fe2007-03-26 17:14:15 -0700273 int err = -EINVAL, unresolved = 0;
Thomas Graf14c0b972006-08-04 03:38:38 -0700274
275 if (nlh->nlmsg_len < nlmsg_msg_size(sizeof(*frh)))
276 goto errout;
277
Denis V. Lunev5fd30ee2008-01-10 03:20:28 -0800278 ops = lookup_rules_ops(net, frh->family);
Thomas Graf14c0b972006-08-04 03:38:38 -0700279 if (ops == NULL) {
Patrick McHardy2fe195c2008-07-01 19:59:37 -0700280 err = -EAFNOSUPPORT;
Thomas Graf14c0b972006-08-04 03:38:38 -0700281 goto errout;
282 }
283
284 err = nlmsg_parse(nlh, sizeof(*frh), tb, FRA_MAX, ops->policy);
285 if (err < 0)
286 goto errout;
287
Thomas Grafe1701c62007-03-24 12:46:02 -0700288 err = validate_rulemsg(frh, tb, ops);
289 if (err < 0)
290 goto errout;
291
Thomas Graf14c0b972006-08-04 03:38:38 -0700292 rule = kzalloc(ops->rule_size, GFP_KERNEL);
293 if (rule == NULL) {
294 err = -ENOMEM;
295 goto errout;
296 }
Denis V. Lunev3661a912008-04-16 02:01:56 -0700297 rule->fr_net = hold_net(net);
Thomas Graf14c0b972006-08-04 03:38:38 -0700298
299 if (tb[FRA_PRIORITY])
300 rule->pref = nla_get_u32(tb[FRA_PRIORITY]);
301
Patrick McHardy491deb22009-12-03 01:25:54 +0000302 if (tb[FRA_IIFNAME]) {
Thomas Graf14c0b972006-08-04 03:38:38 -0700303 struct net_device *dev;
304
Patrick McHardy491deb22009-12-03 01:25:54 +0000305 rule->iifindex = -1;
306 nla_strlcpy(rule->iifname, tb[FRA_IIFNAME], IFNAMSIZ);
307 dev = __dev_get_by_name(net, rule->iifname);
Thomas Graf14c0b972006-08-04 03:38:38 -0700308 if (dev)
Patrick McHardy491deb22009-12-03 01:25:54 +0000309 rule->iifindex = dev->ifindex;
Thomas Graf14c0b972006-08-04 03:38:38 -0700310 }
311
Patrick McHardy1b038a52009-12-03 01:25:56 +0000312 if (tb[FRA_OIFNAME]) {
313 struct net_device *dev;
314
315 rule->oifindex = -1;
316 nla_strlcpy(rule->oifname, tb[FRA_OIFNAME], IFNAMSIZ);
317 dev = __dev_get_by_name(net, rule->oifname);
318 if (dev)
319 rule->oifindex = dev->ifindex;
320 }
321
Thomas Grafb8964ed2006-11-09 15:22:18 -0800322 if (tb[FRA_FWMARK]) {
323 rule->mark = nla_get_u32(tb[FRA_FWMARK]);
324 if (rule->mark)
325 /* compatibility: if the mark value is non-zero all bits
326 * are compared unless a mask is explicitly specified.
327 */
328 rule->mark_mask = 0xFFFFFFFF;
329 }
330
331 if (tb[FRA_FWMASK])
332 rule->mark_mask = nla_get_u32(tb[FRA_FWMASK]);
333
Thomas Graf14c0b972006-08-04 03:38:38 -0700334 rule->action = frh->action;
335 rule->flags = frh->flags;
Patrick McHardy9e762a42006-08-10 23:09:48 -0700336 rule->table = frh_get_table(frh, tb);
Thomas Graf14c0b972006-08-04 03:38:38 -0700337
Patrick McHardy5adef182009-12-03 01:25:57 +0000338 if (!tb[FRA_PRIORITY] && ops->default_pref)
Denis V. Lunev868d13a2008-01-10 03:18:25 -0800339 rule->pref = ops->default_pref(ops);
Thomas Graf14c0b972006-08-04 03:38:38 -0700340
Thomas Graf0947c9fe2007-03-26 17:14:15 -0700341 err = -EINVAL;
342 if (tb[FRA_GOTO]) {
343 if (rule->action != FR_ACT_GOTO)
344 goto errout_free;
345
346 rule->target = nla_get_u32(tb[FRA_GOTO]);
347 /* Backward jumps are prohibited to avoid endless loops */
348 if (rule->target <= rule->pref)
349 goto errout_free;
350
Denis V. Lunev76c72d42007-09-16 15:44:27 -0700351 list_for_each_entry(r, &ops->rules_list, list) {
Thomas Graf0947c9fe2007-03-26 17:14:15 -0700352 if (r->pref == rule->target) {
Eric Dumazet7a2b03c2010-10-26 09:24:55 +0000353 RCU_INIT_POINTER(rule->ctarget, r);
Thomas Graf0947c9fe2007-03-26 17:14:15 -0700354 break;
355 }
356 }
357
Eric Dumazet7a2b03c2010-10-26 09:24:55 +0000358 if (rcu_dereference_protected(rule->ctarget, 1) == NULL)
Thomas Graf0947c9fe2007-03-26 17:14:15 -0700359 unresolved = 1;
360 } else if (rule->action == FR_ACT_GOTO)
361 goto errout_free;
362
Rami Rosen8b3521e2009-05-11 05:52:49 +0000363 err = ops->configure(rule, skb, frh, tb);
Thomas Graf14c0b972006-08-04 03:38:38 -0700364 if (err < 0)
365 goto errout_free;
366
Denis V. Lunev76c72d42007-09-16 15:44:27 -0700367 list_for_each_entry(r, &ops->rules_list, list) {
Thomas Graf14c0b972006-08-04 03:38:38 -0700368 if (r->pref > rule->pref)
369 break;
370 last = r;
371 }
372
373 fib_rule_get(rule);
374
Eric Dumazetebb9fed2010-10-23 09:44:25 +0000375 if (last)
376 list_add_rcu(&rule->list, &last->list);
377 else
378 list_add_rcu(&rule->list, &ops->rules_list);
379
Thomas Graf0947c9fe2007-03-26 17:14:15 -0700380 if (ops->unresolved_rules) {
381 /*
382 * There are unresolved goto rules in the list, check if
383 * any of them are pointing to this new rule.
384 */
Denis V. Lunev76c72d42007-09-16 15:44:27 -0700385 list_for_each_entry(r, &ops->rules_list, list) {
Thomas Graf0947c9fe2007-03-26 17:14:15 -0700386 if (r->action == FR_ACT_GOTO &&
Gao feng561dac22011-09-11 15:36:05 +0000387 r->target == rule->pref &&
388 rtnl_dereference(r->ctarget) == NULL) {
Thomas Graf0947c9fe2007-03-26 17:14:15 -0700389 rcu_assign_pointer(r->ctarget, rule);
390 if (--ops->unresolved_rules == 0)
391 break;
392 }
393 }
394 }
395
396 if (rule->action == FR_ACT_GOTO)
397 ops->nr_goto_rules++;
398
399 if (unresolved)
400 ops->unresolved_rules++;
401
Denis V. Lunev9e3a5482008-01-20 16:46:41 -0800402 notify_rule_change(RTM_NEWRULE, rule, ops, nlh, NETLINK_CB(skb).pid);
Thomas Graf73417f62007-03-27 13:56:52 -0700403 flush_route_cache(ops);
Thomas Graf14c0b972006-08-04 03:38:38 -0700404 rules_ops_put(ops);
405 return 0;
406
407errout_free:
Denis V. Lunev3661a912008-04-16 02:01:56 -0700408 release_net(rule->fr_net);
Thomas Graf14c0b972006-08-04 03:38:38 -0700409 kfree(rule);
410errout:
411 rules_ops_put(ops);
412 return err;
413}
414
Thomas Graf9d9e6a52007-03-25 23:20:05 -0700415static int fib_nl_delrule(struct sk_buff *skb, struct nlmsghdr* nlh, void *arg)
Thomas Graf14c0b972006-08-04 03:38:38 -0700416{
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +0900417 struct net *net = sock_net(skb->sk);
Thomas Graf14c0b972006-08-04 03:38:38 -0700418 struct fib_rule_hdr *frh = nlmsg_data(nlh);
419 struct fib_rules_ops *ops = NULL;
Thomas Graf0947c9fe2007-03-26 17:14:15 -0700420 struct fib_rule *rule, *tmp;
Thomas Graf14c0b972006-08-04 03:38:38 -0700421 struct nlattr *tb[FRA_MAX+1];
422 int err = -EINVAL;
423
424 if (nlh->nlmsg_len < nlmsg_msg_size(sizeof(*frh)))
425 goto errout;
426
Denis V. Lunev5fd30ee2008-01-10 03:20:28 -0800427 ops = lookup_rules_ops(net, frh->family);
Thomas Graf14c0b972006-08-04 03:38:38 -0700428 if (ops == NULL) {
Patrick McHardy2fe195c2008-07-01 19:59:37 -0700429 err = -EAFNOSUPPORT;
Thomas Graf14c0b972006-08-04 03:38:38 -0700430 goto errout;
431 }
432
433 err = nlmsg_parse(nlh, sizeof(*frh), tb, FRA_MAX, ops->policy);
434 if (err < 0)
435 goto errout;
436
Thomas Grafe1701c62007-03-24 12:46:02 -0700437 err = validate_rulemsg(frh, tb, ops);
438 if (err < 0)
439 goto errout;
440
Denis V. Lunev76c72d42007-09-16 15:44:27 -0700441 list_for_each_entry(rule, &ops->rules_list, list) {
Thomas Graf14c0b972006-08-04 03:38:38 -0700442 if (frh->action && (frh->action != rule->action))
443 continue;
444
Patrick McHardy9e762a42006-08-10 23:09:48 -0700445 if (frh->table && (frh_get_table(frh, tb) != rule->table))
Thomas Graf14c0b972006-08-04 03:38:38 -0700446 continue;
447
448 if (tb[FRA_PRIORITY] &&
449 (rule->pref != nla_get_u32(tb[FRA_PRIORITY])))
450 continue;
451
Patrick McHardy491deb22009-12-03 01:25:54 +0000452 if (tb[FRA_IIFNAME] &&
453 nla_strcmp(tb[FRA_IIFNAME], rule->iifname))
Thomas Graf14c0b972006-08-04 03:38:38 -0700454 continue;
455
Patrick McHardy1b038a52009-12-03 01:25:56 +0000456 if (tb[FRA_OIFNAME] &&
457 nla_strcmp(tb[FRA_OIFNAME], rule->oifname))
458 continue;
459
Thomas Grafb8964ed2006-11-09 15:22:18 -0800460 if (tb[FRA_FWMARK] &&
461 (rule->mark != nla_get_u32(tb[FRA_FWMARK])))
462 continue;
463
464 if (tb[FRA_FWMASK] &&
465 (rule->mark_mask != nla_get_u32(tb[FRA_FWMASK])))
466 continue;
467
Thomas Graf14c0b972006-08-04 03:38:38 -0700468 if (!ops->compare(rule, frh, tb))
469 continue;
470
471 if (rule->flags & FIB_RULE_PERMANENT) {
472 err = -EPERM;
473 goto errout;
474 }
475
476 list_del_rcu(&rule->list);
Thomas Graf0947c9fe2007-03-26 17:14:15 -0700477
Yan, Zhengafaef732011-10-17 15:20:28 +0000478 if (rule->action == FR_ACT_GOTO) {
Thomas Graf0947c9fe2007-03-26 17:14:15 -0700479 ops->nr_goto_rules--;
Yan, Zhengafaef732011-10-17 15:20:28 +0000480 if (rtnl_dereference(rule->ctarget) == NULL)
481 ops->unresolved_rules--;
482 }
Thomas Graf0947c9fe2007-03-26 17:14:15 -0700483
484 /*
485 * Check if this rule is a target to any of them. If so,
486 * disable them. As this operation is eventually very
487 * expensive, it is only performed if goto rules have
488 * actually been added.
489 */
490 if (ops->nr_goto_rules > 0) {
Denis V. Lunev76c72d42007-09-16 15:44:27 -0700491 list_for_each_entry(tmp, &ops->rules_list, list) {
Eric Dumazet7a2b03c2010-10-26 09:24:55 +0000492 if (rtnl_dereference(tmp->ctarget) == rule) {
Stephen Hemmingera9b3cd72011-08-01 16:19:00 +0000493 RCU_INIT_POINTER(tmp->ctarget, NULL);
Thomas Graf0947c9fe2007-03-26 17:14:15 -0700494 ops->unresolved_rules++;
495 }
496 }
497 }
498
Denis V. Lunev9e3a5482008-01-20 16:46:41 -0800499 notify_rule_change(RTM_DELRULE, rule, ops, nlh,
Thomas Grafc17084d2006-08-15 00:32:48 -0700500 NETLINK_CB(skb).pid);
Thomas Graf14c0b972006-08-04 03:38:38 -0700501 fib_rule_put(rule);
Thomas Graf73417f62007-03-27 13:56:52 -0700502 flush_route_cache(ops);
Thomas Graf14c0b972006-08-04 03:38:38 -0700503 rules_ops_put(ops);
504 return 0;
505 }
506
507 err = -ENOENT;
508errout:
509 rules_ops_put(ops);
510 return err;
511}
512
Thomas Graf339bf982006-11-10 14:10:15 -0800513static inline size_t fib_rule_nlmsg_size(struct fib_rules_ops *ops,
514 struct fib_rule *rule)
515{
516 size_t payload = NLMSG_ALIGN(sizeof(struct fib_rule_hdr))
Patrick McHardy491deb22009-12-03 01:25:54 +0000517 + nla_total_size(IFNAMSIZ) /* FRA_IIFNAME */
Patrick McHardy1b038a52009-12-03 01:25:56 +0000518 + nla_total_size(IFNAMSIZ) /* FRA_OIFNAME */
Thomas Graf339bf982006-11-10 14:10:15 -0800519 + nla_total_size(4) /* FRA_PRIORITY */
520 + nla_total_size(4) /* FRA_TABLE */
521 + nla_total_size(4) /* FRA_FWMARK */
522 + nla_total_size(4); /* FRA_FWMASK */
523
524 if (ops->nlmsg_payload)
525 payload += ops->nlmsg_payload(rule);
526
527 return payload;
528}
529
Thomas Graf14c0b972006-08-04 03:38:38 -0700530static int fib_nl_fill_rule(struct sk_buff *skb, struct fib_rule *rule,
531 u32 pid, u32 seq, int type, int flags,
532 struct fib_rules_ops *ops)
533{
534 struct nlmsghdr *nlh;
535 struct fib_rule_hdr *frh;
536
537 nlh = nlmsg_put(skb, pid, seq, type, sizeof(*frh), flags);
538 if (nlh == NULL)
Patrick McHardy26932562007-01-31 23:16:40 -0800539 return -EMSGSIZE;
Thomas Graf14c0b972006-08-04 03:38:38 -0700540
541 frh = nlmsg_data(nlh);
Patrick McHardy28bb1722010-04-13 05:03:16 +0000542 frh->family = ops->family;
Thomas Graf14c0b972006-08-04 03:38:38 -0700543 frh->table = rule->table;
Patrick McHardy9e762a42006-08-10 23:09:48 -0700544 NLA_PUT_U32(skb, FRA_TABLE, rule->table);
Thomas Graf14c0b972006-08-04 03:38:38 -0700545 frh->res1 = 0;
546 frh->res2 = 0;
547 frh->action = rule->action;
548 frh->flags = rule->flags;
549
Eric Dumazet7a2b03c2010-10-26 09:24:55 +0000550 if (rule->action == FR_ACT_GOTO &&
Eric Dumazet33d480c2011-08-11 19:30:52 +0000551 rcu_access_pointer(rule->ctarget) == NULL)
Thomas Graf0947c9fe2007-03-26 17:14:15 -0700552 frh->flags |= FIB_RULE_UNRESOLVED;
553
Patrick McHardy491deb22009-12-03 01:25:54 +0000554 if (rule->iifname[0]) {
555 NLA_PUT_STRING(skb, FRA_IIFNAME, rule->iifname);
Thomas Graf14c0b972006-08-04 03:38:38 -0700556
Patrick McHardy491deb22009-12-03 01:25:54 +0000557 if (rule->iifindex == -1)
558 frh->flags |= FIB_RULE_IIF_DETACHED;
Thomas Graf2b443682007-03-26 17:37:59 -0700559 }
560
Patrick McHardy1b038a52009-12-03 01:25:56 +0000561 if (rule->oifname[0]) {
562 NLA_PUT_STRING(skb, FRA_OIFNAME, rule->oifname);
563
564 if (rule->oifindex == -1)
565 frh->flags |= FIB_RULE_OIF_DETACHED;
566 }
567
Thomas Graf14c0b972006-08-04 03:38:38 -0700568 if (rule->pref)
569 NLA_PUT_U32(skb, FRA_PRIORITY, rule->pref);
570
Thomas Grafb8964ed2006-11-09 15:22:18 -0800571 if (rule->mark)
572 NLA_PUT_U32(skb, FRA_FWMARK, rule->mark);
573
574 if (rule->mark_mask || rule->mark)
575 NLA_PUT_U32(skb, FRA_FWMASK, rule->mark_mask);
576
Thomas Graf0947c9fe2007-03-26 17:14:15 -0700577 if (rule->target)
578 NLA_PUT_U32(skb, FRA_GOTO, rule->target);
579
Rami Rosen04af8cf2009-05-20 17:26:23 -0700580 if (ops->fill(rule, skb, frh) < 0)
Thomas Graf14c0b972006-08-04 03:38:38 -0700581 goto nla_put_failure;
582
583 return nlmsg_end(skb, nlh);
584
585nla_put_failure:
Patrick McHardy26932562007-01-31 23:16:40 -0800586 nlmsg_cancel(skb, nlh);
587 return -EMSGSIZE;
Thomas Graf14c0b972006-08-04 03:38:38 -0700588}
589
Thomas Grafc4546732007-03-25 23:24:24 -0700590static int dump_rules(struct sk_buff *skb, struct netlink_callback *cb,
591 struct fib_rules_ops *ops)
Thomas Graf14c0b972006-08-04 03:38:38 -0700592{
593 int idx = 0;
594 struct fib_rule *rule;
Thomas Graf14c0b972006-08-04 03:38:38 -0700595
Eric Dumazete67f88d2011-04-27 22:56:07 +0000596 rcu_read_lock();
597 list_for_each_entry_rcu(rule, &ops->rules_list, list) {
Thomas Grafc4546732007-03-25 23:24:24 -0700598 if (idx < cb->args[1])
Thomas Graf14c0b972006-08-04 03:38:38 -0700599 goto skip;
600
601 if (fib_nl_fill_rule(skb, rule, NETLINK_CB(cb->skb).pid,
602 cb->nlh->nlmsg_seq, RTM_NEWRULE,
603 NLM_F_MULTI, ops) < 0)
604 break;
605skip:
606 idx++;
607 }
Eric Dumazet2907c352011-05-25 07:34:04 +0000608 rcu_read_unlock();
Thomas Grafc4546732007-03-25 23:24:24 -0700609 cb->args[1] = idx;
Thomas Graf14c0b972006-08-04 03:38:38 -0700610 rules_ops_put(ops);
611
612 return skb->len;
613}
614
Thomas Grafc4546732007-03-25 23:24:24 -0700615static int fib_nl_dumprule(struct sk_buff *skb, struct netlink_callback *cb)
616{
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +0900617 struct net *net = sock_net(skb->sk);
Thomas Grafc4546732007-03-25 23:24:24 -0700618 struct fib_rules_ops *ops;
619 int idx = 0, family;
620
621 family = rtnl_msg_family(cb->nlh);
622 if (family != AF_UNSPEC) {
623 /* Protocol specific dump request */
Denis V. Lunev5fd30ee2008-01-10 03:20:28 -0800624 ops = lookup_rules_ops(net, family);
Thomas Grafc4546732007-03-25 23:24:24 -0700625 if (ops == NULL)
626 return -EAFNOSUPPORT;
627
628 return dump_rules(skb, cb, ops);
629 }
630
631 rcu_read_lock();
Denis V. Lunev5fd30ee2008-01-10 03:20:28 -0800632 list_for_each_entry_rcu(ops, &net->rules_ops, list) {
Thomas Grafc4546732007-03-25 23:24:24 -0700633 if (idx < cb->args[0] || !try_module_get(ops->owner))
634 goto skip;
635
636 if (dump_rules(skb, cb, ops) < 0)
637 break;
638
639 cb->args[1] = 0;
Eric Dumazet2fb35732010-03-09 20:03:38 +0000640skip:
Thomas Grafc4546732007-03-25 23:24:24 -0700641 idx++;
642 }
643 rcu_read_unlock();
644 cb->args[0] = idx;
645
646 return skb->len;
647}
Thomas Graf14c0b972006-08-04 03:38:38 -0700648
Denis V. Lunev9e3a5482008-01-20 16:46:41 -0800649static void notify_rule_change(int event, struct fib_rule *rule,
Thomas Grafc17084d2006-08-15 00:32:48 -0700650 struct fib_rules_ops *ops, struct nlmsghdr *nlh,
651 u32 pid)
Thomas Graf14c0b972006-08-04 03:38:38 -0700652{
Denis V. Lunev9e3a5482008-01-20 16:46:41 -0800653 struct net *net;
Thomas Grafc17084d2006-08-15 00:32:48 -0700654 struct sk_buff *skb;
655 int err = -ENOBUFS;
Thomas Graf14c0b972006-08-04 03:38:38 -0700656
Denis V. Lunev9e3a5482008-01-20 16:46:41 -0800657 net = ops->fro_net;
Thomas Graf339bf982006-11-10 14:10:15 -0800658 skb = nlmsg_new(fib_rule_nlmsg_size(ops, rule), GFP_KERNEL);
Thomas Graf14c0b972006-08-04 03:38:38 -0700659 if (skb == NULL)
Thomas Grafc17084d2006-08-15 00:32:48 -0700660 goto errout;
661
662 err = fib_nl_fill_rule(skb, rule, pid, nlh->nlmsg_seq, event, 0, ops);
Patrick McHardy26932562007-01-31 23:16:40 -0800663 if (err < 0) {
664 /* -EMSGSIZE implies BUG in fib_rule_nlmsg_size() */
665 WARN_ON(err == -EMSGSIZE);
666 kfree_skb(skb);
667 goto errout;
668 }
Denis V. Lunev9e3a5482008-01-20 16:46:41 -0800669
Pablo Neira Ayuso1ce85fe2009-02-24 23:18:28 -0800670 rtnl_notify(skb, net, pid, ops->nlgroup, nlh, GFP_KERNEL);
671 return;
Thomas Grafc17084d2006-08-15 00:32:48 -0700672errout:
673 if (err < 0)
Denis V. Lunev5fd30ee2008-01-10 03:20:28 -0800674 rtnl_set_sk_err(net, ops->nlgroup, err);
Thomas Graf14c0b972006-08-04 03:38:38 -0700675}
676
677static void attach_rules(struct list_head *rules, struct net_device *dev)
678{
679 struct fib_rule *rule;
680
681 list_for_each_entry(rule, rules, list) {
Patrick McHardy491deb22009-12-03 01:25:54 +0000682 if (rule->iifindex == -1 &&
683 strcmp(dev->name, rule->iifname) == 0)
684 rule->iifindex = dev->ifindex;
Patrick McHardy1b038a52009-12-03 01:25:56 +0000685 if (rule->oifindex == -1 &&
686 strcmp(dev->name, rule->oifname) == 0)
687 rule->oifindex = dev->ifindex;
Thomas Graf14c0b972006-08-04 03:38:38 -0700688 }
689}
690
691static void detach_rules(struct list_head *rules, struct net_device *dev)
692{
693 struct fib_rule *rule;
694
Patrick McHardy1b038a52009-12-03 01:25:56 +0000695 list_for_each_entry(rule, rules, list) {
Patrick McHardy491deb22009-12-03 01:25:54 +0000696 if (rule->iifindex == dev->ifindex)
697 rule->iifindex = -1;
Patrick McHardy1b038a52009-12-03 01:25:56 +0000698 if (rule->oifindex == dev->ifindex)
699 rule->oifindex = -1;
700 }
Thomas Graf14c0b972006-08-04 03:38:38 -0700701}
702
703
704static int fib_rules_event(struct notifier_block *this, unsigned long event,
705 void *ptr)
706{
707 struct net_device *dev = ptr;
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +0900708 struct net *net = dev_net(dev);
Thomas Graf14c0b972006-08-04 03:38:38 -0700709 struct fib_rules_ops *ops;
710
711 ASSERT_RTNL();
Thomas Graf14c0b972006-08-04 03:38:38 -0700712
713 switch (event) {
714 case NETDEV_REGISTER:
Denis V. Lunev5fd30ee2008-01-10 03:20:28 -0800715 list_for_each_entry(ops, &net->rules_ops, list)
Denis V. Lunev76c72d42007-09-16 15:44:27 -0700716 attach_rules(&ops->rules_list, dev);
Thomas Graf14c0b972006-08-04 03:38:38 -0700717 break;
718
719 case NETDEV_UNREGISTER:
Denis V. Lunev5fd30ee2008-01-10 03:20:28 -0800720 list_for_each_entry(ops, &net->rules_ops, list)
Denis V. Lunev76c72d42007-09-16 15:44:27 -0700721 detach_rules(&ops->rules_list, dev);
Thomas Graf14c0b972006-08-04 03:38:38 -0700722 break;
723 }
724
Thomas Graf14c0b972006-08-04 03:38:38 -0700725 return NOTIFY_DONE;
726}
727
728static struct notifier_block fib_rules_notifier = {
729 .notifier_call = fib_rules_event,
730};
731
Alexey Dobriyan2c8c1e72010-01-17 03:35:32 +0000732static int __net_init fib_rules_net_init(struct net *net)
Denis V. Lunev5fd30ee2008-01-10 03:20:28 -0800733{
734 INIT_LIST_HEAD(&net->rules_ops);
735 spin_lock_init(&net->rules_mod_lock);
736 return 0;
737}
738
739static struct pernet_operations fib_rules_net_ops = {
740 .init = fib_rules_net_init,
741};
742
Thomas Graf14c0b972006-08-04 03:38:38 -0700743static int __init fib_rules_init(void)
744{
Denis V. Lunev5fd30ee2008-01-10 03:20:28 -0800745 int err;
Greg Rosec7ac8672011-06-10 01:27:09 +0000746 rtnl_register(PF_UNSPEC, RTM_NEWRULE, fib_nl_newrule, NULL, NULL);
747 rtnl_register(PF_UNSPEC, RTM_DELRULE, fib_nl_delrule, NULL, NULL);
748 rtnl_register(PF_UNSPEC, RTM_GETRULE, NULL, fib_nl_dumprule, NULL);
Thomas Graf9d9e6a52007-03-25 23:20:05 -0700749
Eric W. Biederman5d6d4802008-11-07 22:52:34 -0800750 err = register_pernet_subsys(&fib_rules_net_ops);
Denis V. Lunev5fd30ee2008-01-10 03:20:28 -0800751 if (err < 0)
752 goto fail;
753
Eric W. Biederman5d6d4802008-11-07 22:52:34 -0800754 err = register_netdevice_notifier(&fib_rules_notifier);
Denis V. Lunev5fd30ee2008-01-10 03:20:28 -0800755 if (err < 0)
756 goto fail_unregister;
Eric W. Biederman5d6d4802008-11-07 22:52:34 -0800757
Denis V. Lunev5fd30ee2008-01-10 03:20:28 -0800758 return 0;
759
760fail_unregister:
Eric W. Biederman5d6d4802008-11-07 22:52:34 -0800761 unregister_pernet_subsys(&fib_rules_net_ops);
Denis V. Lunev5fd30ee2008-01-10 03:20:28 -0800762fail:
763 rtnl_unregister(PF_UNSPEC, RTM_NEWRULE);
764 rtnl_unregister(PF_UNSPEC, RTM_DELRULE);
765 rtnl_unregister(PF_UNSPEC, RTM_GETRULE);
766 return err;
Thomas Graf14c0b972006-08-04 03:38:38 -0700767}
768
769subsys_initcall(fib_rules_init);