blob: 1eb32276be773c61c4dbb83ba0752ded6dac8ead [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 *
125fib_rules_register(struct fib_rules_ops *tmpl, struct net *net)
126{
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
Daniel Lezcano9eb87f32007-12-07 00:42:52 -0800147void 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}
Daniel Lezcano9eb87f32007-12-07 00:42:52 -0800156EXPORT_SYMBOL_GPL(fib_rules_cleanup_ops);
Thomas Graf14c0b972006-08-04 03:38:38 -0700157
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
Patrick McHardy491deb22009-12-03 01:25:54 +0000185 if (rule->iifindex && (rule->iifindex != fl->iif))
Thomas Graf3dfbcc42006-11-09 15:23:20 -0800186 goto out;
187
Patrick McHardy1b038a52009-12-03 01:25:56 +0000188 if (rule->oifindex && (rule->oifindex != fl->oif))
189 goto out;
190
Thomas Graf3dfbcc42006-11-09 15:23:20 -0800191 if ((rule->mark ^ fl->mark) & rule->mark_mask)
192 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) {
228 fib_rule_get(rule);
229 arg->rule = rule;
230 goto out;
231 }
232 }
233
Steven Whitehouse83886b62007-03-30 13:34:27 -0700234 err = -ESRCH;
Thomas Graf14c0b972006-08-04 03:38:38 -0700235out:
236 rcu_read_unlock();
237
238 return err;
239}
Thomas Graf14c0b972006-08-04 03:38:38 -0700240EXPORT_SYMBOL_GPL(fib_rules_lookup);
241
Thomas Grafe1701c62007-03-24 12:46:02 -0700242static int validate_rulemsg(struct fib_rule_hdr *frh, struct nlattr **tb,
243 struct fib_rules_ops *ops)
244{
245 int err = -EINVAL;
246
247 if (frh->src_len)
248 if (tb[FRA_SRC] == NULL ||
249 frh->src_len > (ops->addr_size * 8) ||
250 nla_len(tb[FRA_SRC]) != ops->addr_size)
251 goto errout;
252
253 if (frh->dst_len)
254 if (tb[FRA_DST] == NULL ||
255 frh->dst_len > (ops->addr_size * 8) ||
256 nla_len(tb[FRA_DST]) != ops->addr_size)
257 goto errout;
258
259 err = 0;
260errout:
261 return err;
262}
263
Thomas Graf9d9e6a52007-03-25 23:20:05 -0700264static int fib_nl_newrule(struct sk_buff *skb, struct nlmsghdr* nlh, void *arg)
Thomas Graf14c0b972006-08-04 03:38:38 -0700265{
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +0900266 struct net *net = sock_net(skb->sk);
Thomas Graf14c0b972006-08-04 03:38:38 -0700267 struct fib_rule_hdr *frh = nlmsg_data(nlh);
268 struct fib_rules_ops *ops = NULL;
269 struct fib_rule *rule, *r, *last = NULL;
270 struct nlattr *tb[FRA_MAX+1];
Thomas Graf0947c9fe2007-03-26 17:14:15 -0700271 int err = -EINVAL, unresolved = 0;
Thomas Graf14c0b972006-08-04 03:38:38 -0700272
273 if (nlh->nlmsg_len < nlmsg_msg_size(sizeof(*frh)))
274 goto errout;
275
Denis V. Lunev5fd30ee2008-01-10 03:20:28 -0800276 ops = lookup_rules_ops(net, frh->family);
Thomas Graf14c0b972006-08-04 03:38:38 -0700277 if (ops == NULL) {
Patrick McHardy2fe195c2008-07-01 19:59:37 -0700278 err = -EAFNOSUPPORT;
Thomas Graf14c0b972006-08-04 03:38:38 -0700279 goto errout;
280 }
281
282 err = nlmsg_parse(nlh, sizeof(*frh), tb, FRA_MAX, ops->policy);
283 if (err < 0)
284 goto errout;
285
Thomas Grafe1701c62007-03-24 12:46:02 -0700286 err = validate_rulemsg(frh, tb, ops);
287 if (err < 0)
288 goto errout;
289
Thomas Graf14c0b972006-08-04 03:38:38 -0700290 rule = kzalloc(ops->rule_size, GFP_KERNEL);
291 if (rule == NULL) {
292 err = -ENOMEM;
293 goto errout;
294 }
Denis V. Lunev3661a912008-04-16 02:01:56 -0700295 rule->fr_net = hold_net(net);
Thomas Graf14c0b972006-08-04 03:38:38 -0700296
297 if (tb[FRA_PRIORITY])
298 rule->pref = nla_get_u32(tb[FRA_PRIORITY]);
299
Patrick McHardy491deb22009-12-03 01:25:54 +0000300 if (tb[FRA_IIFNAME]) {
Thomas Graf14c0b972006-08-04 03:38:38 -0700301 struct net_device *dev;
302
Patrick McHardy491deb22009-12-03 01:25:54 +0000303 rule->iifindex = -1;
304 nla_strlcpy(rule->iifname, tb[FRA_IIFNAME], IFNAMSIZ);
305 dev = __dev_get_by_name(net, rule->iifname);
Thomas Graf14c0b972006-08-04 03:38:38 -0700306 if (dev)
Patrick McHardy491deb22009-12-03 01:25:54 +0000307 rule->iifindex = dev->ifindex;
Thomas Graf14c0b972006-08-04 03:38:38 -0700308 }
309
Patrick McHardy1b038a52009-12-03 01:25:56 +0000310 if (tb[FRA_OIFNAME]) {
311 struct net_device *dev;
312
313 rule->oifindex = -1;
314 nla_strlcpy(rule->oifname, tb[FRA_OIFNAME], IFNAMSIZ);
315 dev = __dev_get_by_name(net, rule->oifname);
316 if (dev)
317 rule->oifindex = dev->ifindex;
318 }
319
Thomas Grafb8964ed2006-11-09 15:22:18 -0800320 if (tb[FRA_FWMARK]) {
321 rule->mark = nla_get_u32(tb[FRA_FWMARK]);
322 if (rule->mark)
323 /* compatibility: if the mark value is non-zero all bits
324 * are compared unless a mask is explicitly specified.
325 */
326 rule->mark_mask = 0xFFFFFFFF;
327 }
328
329 if (tb[FRA_FWMASK])
330 rule->mark_mask = nla_get_u32(tb[FRA_FWMASK]);
331
Thomas Graf14c0b972006-08-04 03:38:38 -0700332 rule->action = frh->action;
333 rule->flags = frh->flags;
Patrick McHardy9e762a42006-08-10 23:09:48 -0700334 rule->table = frh_get_table(frh, tb);
Thomas Graf14c0b972006-08-04 03:38:38 -0700335
Patrick McHardy5adef182009-12-03 01:25:57 +0000336 if (!tb[FRA_PRIORITY] && ops->default_pref)
Denis V. Lunev868d13a2008-01-10 03:18:25 -0800337 rule->pref = ops->default_pref(ops);
Thomas Graf14c0b972006-08-04 03:38:38 -0700338
Thomas Graf0947c9fe2007-03-26 17:14:15 -0700339 err = -EINVAL;
340 if (tb[FRA_GOTO]) {
341 if (rule->action != FR_ACT_GOTO)
342 goto errout_free;
343
344 rule->target = nla_get_u32(tb[FRA_GOTO]);
345 /* Backward jumps are prohibited to avoid endless loops */
346 if (rule->target <= rule->pref)
347 goto errout_free;
348
Denis V. Lunev76c72d42007-09-16 15:44:27 -0700349 list_for_each_entry(r, &ops->rules_list, list) {
Thomas Graf0947c9fe2007-03-26 17:14:15 -0700350 if (r->pref == rule->target) {
351 rule->ctarget = r;
352 break;
353 }
354 }
355
356 if (rule->ctarget == NULL)
357 unresolved = 1;
358 } else if (rule->action == FR_ACT_GOTO)
359 goto errout_free;
360
Rami Rosen8b3521e2009-05-11 05:52:49 +0000361 err = ops->configure(rule, skb, frh, tb);
Thomas Graf14c0b972006-08-04 03:38:38 -0700362 if (err < 0)
363 goto errout_free;
364
Denis V. Lunev76c72d42007-09-16 15:44:27 -0700365 list_for_each_entry(r, &ops->rules_list, list) {
Thomas Graf14c0b972006-08-04 03:38:38 -0700366 if (r->pref > rule->pref)
367 break;
368 last = r;
369 }
370
371 fib_rule_get(rule);
372
Thomas Graf0947c9fe2007-03-26 17:14:15 -0700373 if (ops->unresolved_rules) {
374 /*
375 * There are unresolved goto rules in the list, check if
376 * any of them are pointing to this new rule.
377 */
Denis V. Lunev76c72d42007-09-16 15:44:27 -0700378 list_for_each_entry(r, &ops->rules_list, list) {
Thomas Graf0947c9fe2007-03-26 17:14:15 -0700379 if (r->action == FR_ACT_GOTO &&
380 r->target == rule->pref) {
381 BUG_ON(r->ctarget != NULL);
382 rcu_assign_pointer(r->ctarget, rule);
383 if (--ops->unresolved_rules == 0)
384 break;
385 }
386 }
387 }
388
389 if (rule->action == FR_ACT_GOTO)
390 ops->nr_goto_rules++;
391
392 if (unresolved)
393 ops->unresolved_rules++;
394
Thomas Graf14c0b972006-08-04 03:38:38 -0700395 if (last)
396 list_add_rcu(&rule->list, &last->list);
397 else
Denis V. Lunev76c72d42007-09-16 15:44:27 -0700398 list_add_rcu(&rule->list, &ops->rules_list);
Thomas Graf14c0b972006-08-04 03:38:38 -0700399
Denis V. Lunev9e3a5482008-01-20 16:46:41 -0800400 notify_rule_change(RTM_NEWRULE, rule, ops, nlh, NETLINK_CB(skb).pid);
Thomas Graf73417f62007-03-27 13:56:52 -0700401 flush_route_cache(ops);
Thomas Graf14c0b972006-08-04 03:38:38 -0700402 rules_ops_put(ops);
403 return 0;
404
405errout_free:
Denis V. Lunev3661a912008-04-16 02:01:56 -0700406 release_net(rule->fr_net);
Thomas Graf14c0b972006-08-04 03:38:38 -0700407 kfree(rule);
408errout:
409 rules_ops_put(ops);
410 return err;
411}
412
Thomas Graf9d9e6a52007-03-25 23:20:05 -0700413static int fib_nl_delrule(struct sk_buff *skb, struct nlmsghdr* nlh, void *arg)
Thomas Graf14c0b972006-08-04 03:38:38 -0700414{
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +0900415 struct net *net = sock_net(skb->sk);
Thomas Graf14c0b972006-08-04 03:38:38 -0700416 struct fib_rule_hdr *frh = nlmsg_data(nlh);
417 struct fib_rules_ops *ops = NULL;
Thomas Graf0947c9fe2007-03-26 17:14:15 -0700418 struct fib_rule *rule, *tmp;
Thomas Graf14c0b972006-08-04 03:38:38 -0700419 struct nlattr *tb[FRA_MAX+1];
420 int err = -EINVAL;
421
422 if (nlh->nlmsg_len < nlmsg_msg_size(sizeof(*frh)))
423 goto errout;
424
Denis V. Lunev5fd30ee2008-01-10 03:20:28 -0800425 ops = lookup_rules_ops(net, frh->family);
Thomas Graf14c0b972006-08-04 03:38:38 -0700426 if (ops == NULL) {
Patrick McHardy2fe195c2008-07-01 19:59:37 -0700427 err = -EAFNOSUPPORT;
Thomas Graf14c0b972006-08-04 03:38:38 -0700428 goto errout;
429 }
430
431 err = nlmsg_parse(nlh, sizeof(*frh), tb, FRA_MAX, ops->policy);
432 if (err < 0)
433 goto errout;
434
Thomas Grafe1701c62007-03-24 12:46:02 -0700435 err = validate_rulemsg(frh, tb, ops);
436 if (err < 0)
437 goto errout;
438
Denis V. Lunev76c72d42007-09-16 15:44:27 -0700439 list_for_each_entry(rule, &ops->rules_list, list) {
Thomas Graf14c0b972006-08-04 03:38:38 -0700440 if (frh->action && (frh->action != rule->action))
441 continue;
442
Patrick McHardy9e762a42006-08-10 23:09:48 -0700443 if (frh->table && (frh_get_table(frh, tb) != rule->table))
Thomas Graf14c0b972006-08-04 03:38:38 -0700444 continue;
445
446 if (tb[FRA_PRIORITY] &&
447 (rule->pref != nla_get_u32(tb[FRA_PRIORITY])))
448 continue;
449
Patrick McHardy491deb22009-12-03 01:25:54 +0000450 if (tb[FRA_IIFNAME] &&
451 nla_strcmp(tb[FRA_IIFNAME], rule->iifname))
Thomas Graf14c0b972006-08-04 03:38:38 -0700452 continue;
453
Patrick McHardy1b038a52009-12-03 01:25:56 +0000454 if (tb[FRA_OIFNAME] &&
455 nla_strcmp(tb[FRA_OIFNAME], rule->oifname))
456 continue;
457
Thomas Grafb8964ed2006-11-09 15:22:18 -0800458 if (tb[FRA_FWMARK] &&
459 (rule->mark != nla_get_u32(tb[FRA_FWMARK])))
460 continue;
461
462 if (tb[FRA_FWMASK] &&
463 (rule->mark_mask != nla_get_u32(tb[FRA_FWMASK])))
464 continue;
465
Thomas Graf14c0b972006-08-04 03:38:38 -0700466 if (!ops->compare(rule, frh, tb))
467 continue;
468
469 if (rule->flags & FIB_RULE_PERMANENT) {
470 err = -EPERM;
471 goto errout;
472 }
473
474 list_del_rcu(&rule->list);
Thomas Graf0947c9fe2007-03-26 17:14:15 -0700475
476 if (rule->action == FR_ACT_GOTO)
477 ops->nr_goto_rules--;
478
479 /*
480 * Check if this rule is a target to any of them. If so,
481 * disable them. As this operation is eventually very
482 * expensive, it is only performed if goto rules have
483 * actually been added.
484 */
485 if (ops->nr_goto_rules > 0) {
Denis V. Lunev76c72d42007-09-16 15:44:27 -0700486 list_for_each_entry(tmp, &ops->rules_list, list) {
Thomas Graf0947c9fe2007-03-26 17:14:15 -0700487 if (tmp->ctarget == rule) {
488 rcu_assign_pointer(tmp->ctarget, NULL);
489 ops->unresolved_rules++;
490 }
491 }
492 }
493
Thomas Graf14c0b972006-08-04 03:38:38 -0700494 synchronize_rcu();
Denis V. Lunev9e3a5482008-01-20 16:46:41 -0800495 notify_rule_change(RTM_DELRULE, rule, ops, nlh,
Thomas Grafc17084d2006-08-15 00:32:48 -0700496 NETLINK_CB(skb).pid);
Thomas Graf14c0b972006-08-04 03:38:38 -0700497 fib_rule_put(rule);
Thomas Graf73417f62007-03-27 13:56:52 -0700498 flush_route_cache(ops);
Thomas Graf14c0b972006-08-04 03:38:38 -0700499 rules_ops_put(ops);
500 return 0;
501 }
502
503 err = -ENOENT;
504errout:
505 rules_ops_put(ops);
506 return err;
507}
508
Thomas Graf339bf982006-11-10 14:10:15 -0800509static inline size_t fib_rule_nlmsg_size(struct fib_rules_ops *ops,
510 struct fib_rule *rule)
511{
512 size_t payload = NLMSG_ALIGN(sizeof(struct fib_rule_hdr))
Patrick McHardy491deb22009-12-03 01:25:54 +0000513 + nla_total_size(IFNAMSIZ) /* FRA_IIFNAME */
Patrick McHardy1b038a52009-12-03 01:25:56 +0000514 + nla_total_size(IFNAMSIZ) /* FRA_OIFNAME */
Thomas Graf339bf982006-11-10 14:10:15 -0800515 + nla_total_size(4) /* FRA_PRIORITY */
516 + nla_total_size(4) /* FRA_TABLE */
517 + nla_total_size(4) /* FRA_FWMARK */
518 + nla_total_size(4); /* FRA_FWMASK */
519
520 if (ops->nlmsg_payload)
521 payload += ops->nlmsg_payload(rule);
522
523 return payload;
524}
525
Thomas Graf14c0b972006-08-04 03:38:38 -0700526static int fib_nl_fill_rule(struct sk_buff *skb, struct fib_rule *rule,
527 u32 pid, u32 seq, int type, int flags,
528 struct fib_rules_ops *ops)
529{
530 struct nlmsghdr *nlh;
531 struct fib_rule_hdr *frh;
532
533 nlh = nlmsg_put(skb, pid, seq, type, sizeof(*frh), flags);
534 if (nlh == NULL)
Patrick McHardy26932562007-01-31 23:16:40 -0800535 return -EMSGSIZE;
Thomas Graf14c0b972006-08-04 03:38:38 -0700536
537 frh = nlmsg_data(nlh);
538 frh->table = rule->table;
Patrick McHardy9e762a42006-08-10 23:09:48 -0700539 NLA_PUT_U32(skb, FRA_TABLE, rule->table);
Thomas Graf14c0b972006-08-04 03:38:38 -0700540 frh->res1 = 0;
541 frh->res2 = 0;
542 frh->action = rule->action;
543 frh->flags = rule->flags;
544
Thomas Graf0947c9fe2007-03-26 17:14:15 -0700545 if (rule->action == FR_ACT_GOTO && rule->ctarget == NULL)
546 frh->flags |= FIB_RULE_UNRESOLVED;
547
Patrick McHardy491deb22009-12-03 01:25:54 +0000548 if (rule->iifname[0]) {
549 NLA_PUT_STRING(skb, FRA_IIFNAME, rule->iifname);
Thomas Graf14c0b972006-08-04 03:38:38 -0700550
Patrick McHardy491deb22009-12-03 01:25:54 +0000551 if (rule->iifindex == -1)
552 frh->flags |= FIB_RULE_IIF_DETACHED;
Thomas Graf2b443682007-03-26 17:37:59 -0700553 }
554
Patrick McHardy1b038a52009-12-03 01:25:56 +0000555 if (rule->oifname[0]) {
556 NLA_PUT_STRING(skb, FRA_OIFNAME, rule->oifname);
557
558 if (rule->oifindex == -1)
559 frh->flags |= FIB_RULE_OIF_DETACHED;
560 }
561
Thomas Graf14c0b972006-08-04 03:38:38 -0700562 if (rule->pref)
563 NLA_PUT_U32(skb, FRA_PRIORITY, rule->pref);
564
Thomas Grafb8964ed2006-11-09 15:22:18 -0800565 if (rule->mark)
566 NLA_PUT_U32(skb, FRA_FWMARK, rule->mark);
567
568 if (rule->mark_mask || rule->mark)
569 NLA_PUT_U32(skb, FRA_FWMASK, rule->mark_mask);
570
Thomas Graf0947c9fe2007-03-26 17:14:15 -0700571 if (rule->target)
572 NLA_PUT_U32(skb, FRA_GOTO, rule->target);
573
Rami Rosen04af8cf2009-05-20 17:26:23 -0700574 if (ops->fill(rule, skb, frh) < 0)
Thomas Graf14c0b972006-08-04 03:38:38 -0700575 goto nla_put_failure;
576
577 return nlmsg_end(skb, nlh);
578
579nla_put_failure:
Patrick McHardy26932562007-01-31 23:16:40 -0800580 nlmsg_cancel(skb, nlh);
581 return -EMSGSIZE;
Thomas Graf14c0b972006-08-04 03:38:38 -0700582}
583
Thomas Grafc4546732007-03-25 23:24:24 -0700584static int dump_rules(struct sk_buff *skb, struct netlink_callback *cb,
585 struct fib_rules_ops *ops)
Thomas Graf14c0b972006-08-04 03:38:38 -0700586{
587 int idx = 0;
588 struct fib_rule *rule;
Thomas Graf14c0b972006-08-04 03:38:38 -0700589
Denis V. Lunev76c72d42007-09-16 15:44:27 -0700590 list_for_each_entry(rule, &ops->rules_list, list) {
Thomas Grafc4546732007-03-25 23:24:24 -0700591 if (idx < cb->args[1])
Thomas Graf14c0b972006-08-04 03:38:38 -0700592 goto skip;
593
594 if (fib_nl_fill_rule(skb, rule, NETLINK_CB(cb->skb).pid,
595 cb->nlh->nlmsg_seq, RTM_NEWRULE,
596 NLM_F_MULTI, ops) < 0)
597 break;
598skip:
599 idx++;
600 }
Thomas Grafc4546732007-03-25 23:24:24 -0700601 cb->args[1] = idx;
Thomas Graf14c0b972006-08-04 03:38:38 -0700602 rules_ops_put(ops);
603
604 return skb->len;
605}
606
Thomas Grafc4546732007-03-25 23:24:24 -0700607static int fib_nl_dumprule(struct sk_buff *skb, struct netlink_callback *cb)
608{
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +0900609 struct net *net = sock_net(skb->sk);
Thomas Grafc4546732007-03-25 23:24:24 -0700610 struct fib_rules_ops *ops;
611 int idx = 0, family;
612
613 family = rtnl_msg_family(cb->nlh);
614 if (family != AF_UNSPEC) {
615 /* Protocol specific dump request */
Denis V. Lunev5fd30ee2008-01-10 03:20:28 -0800616 ops = lookup_rules_ops(net, family);
Thomas Grafc4546732007-03-25 23:24:24 -0700617 if (ops == NULL)
618 return -EAFNOSUPPORT;
619
620 return dump_rules(skb, cb, ops);
621 }
622
623 rcu_read_lock();
Denis V. Lunev5fd30ee2008-01-10 03:20:28 -0800624 list_for_each_entry_rcu(ops, &net->rules_ops, list) {
Thomas Grafc4546732007-03-25 23:24:24 -0700625 if (idx < cb->args[0] || !try_module_get(ops->owner))
626 goto skip;
627
628 if (dump_rules(skb, cb, ops) < 0)
629 break;
630
631 cb->args[1] = 0;
Eric Dumazet2fb35732010-03-09 20:03:38 +0000632skip:
Thomas Grafc4546732007-03-25 23:24:24 -0700633 idx++;
634 }
635 rcu_read_unlock();
636 cb->args[0] = idx;
637
638 return skb->len;
639}
Thomas Graf14c0b972006-08-04 03:38:38 -0700640
Denis V. Lunev9e3a5482008-01-20 16:46:41 -0800641static void notify_rule_change(int event, struct fib_rule *rule,
Thomas Grafc17084d2006-08-15 00:32:48 -0700642 struct fib_rules_ops *ops, struct nlmsghdr *nlh,
643 u32 pid)
Thomas Graf14c0b972006-08-04 03:38:38 -0700644{
Denis V. Lunev9e3a5482008-01-20 16:46:41 -0800645 struct net *net;
Thomas Grafc17084d2006-08-15 00:32:48 -0700646 struct sk_buff *skb;
647 int err = -ENOBUFS;
Thomas Graf14c0b972006-08-04 03:38:38 -0700648
Denis V. Lunev9e3a5482008-01-20 16:46:41 -0800649 net = ops->fro_net;
Thomas Graf339bf982006-11-10 14:10:15 -0800650 skb = nlmsg_new(fib_rule_nlmsg_size(ops, rule), GFP_KERNEL);
Thomas Graf14c0b972006-08-04 03:38:38 -0700651 if (skb == NULL)
Thomas Grafc17084d2006-08-15 00:32:48 -0700652 goto errout;
653
654 err = fib_nl_fill_rule(skb, rule, pid, nlh->nlmsg_seq, event, 0, ops);
Patrick McHardy26932562007-01-31 23:16:40 -0800655 if (err < 0) {
656 /* -EMSGSIZE implies BUG in fib_rule_nlmsg_size() */
657 WARN_ON(err == -EMSGSIZE);
658 kfree_skb(skb);
659 goto errout;
660 }
Denis V. Lunev9e3a5482008-01-20 16:46:41 -0800661
Pablo Neira Ayuso1ce85fe2009-02-24 23:18:28 -0800662 rtnl_notify(skb, net, pid, ops->nlgroup, nlh, GFP_KERNEL);
663 return;
Thomas Grafc17084d2006-08-15 00:32:48 -0700664errout:
665 if (err < 0)
Denis V. Lunev5fd30ee2008-01-10 03:20:28 -0800666 rtnl_set_sk_err(net, ops->nlgroup, err);
Thomas Graf14c0b972006-08-04 03:38:38 -0700667}
668
669static void attach_rules(struct list_head *rules, struct net_device *dev)
670{
671 struct fib_rule *rule;
672
673 list_for_each_entry(rule, rules, list) {
Patrick McHardy491deb22009-12-03 01:25:54 +0000674 if (rule->iifindex == -1 &&
675 strcmp(dev->name, rule->iifname) == 0)
676 rule->iifindex = dev->ifindex;
Patrick McHardy1b038a52009-12-03 01:25:56 +0000677 if (rule->oifindex == -1 &&
678 strcmp(dev->name, rule->oifname) == 0)
679 rule->oifindex = dev->ifindex;
Thomas Graf14c0b972006-08-04 03:38:38 -0700680 }
681}
682
683static void detach_rules(struct list_head *rules, struct net_device *dev)
684{
685 struct fib_rule *rule;
686
Patrick McHardy1b038a52009-12-03 01:25:56 +0000687 list_for_each_entry(rule, rules, list) {
Patrick McHardy491deb22009-12-03 01:25:54 +0000688 if (rule->iifindex == dev->ifindex)
689 rule->iifindex = -1;
Patrick McHardy1b038a52009-12-03 01:25:56 +0000690 if (rule->oifindex == dev->ifindex)
691 rule->oifindex = -1;
692 }
Thomas Graf14c0b972006-08-04 03:38:38 -0700693}
694
695
696static int fib_rules_event(struct notifier_block *this, unsigned long event,
697 void *ptr)
698{
699 struct net_device *dev = ptr;
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +0900700 struct net *net = dev_net(dev);
Thomas Graf14c0b972006-08-04 03:38:38 -0700701 struct fib_rules_ops *ops;
702
703 ASSERT_RTNL();
Thomas Graf14c0b972006-08-04 03:38:38 -0700704
705 switch (event) {
706 case NETDEV_REGISTER:
Denis V. Lunev5fd30ee2008-01-10 03:20:28 -0800707 list_for_each_entry(ops, &net->rules_ops, list)
Denis V. Lunev76c72d42007-09-16 15:44:27 -0700708 attach_rules(&ops->rules_list, dev);
Thomas Graf14c0b972006-08-04 03:38:38 -0700709 break;
710
711 case NETDEV_UNREGISTER:
Denis V. Lunev5fd30ee2008-01-10 03:20:28 -0800712 list_for_each_entry(ops, &net->rules_ops, list)
Denis V. Lunev76c72d42007-09-16 15:44:27 -0700713 detach_rules(&ops->rules_list, dev);
Thomas Graf14c0b972006-08-04 03:38:38 -0700714 break;
715 }
716
Thomas Graf14c0b972006-08-04 03:38:38 -0700717 return NOTIFY_DONE;
718}
719
720static struct notifier_block fib_rules_notifier = {
721 .notifier_call = fib_rules_event,
722};
723
Alexey Dobriyan2c8c1e72010-01-17 03:35:32 +0000724static int __net_init fib_rules_net_init(struct net *net)
Denis V. Lunev5fd30ee2008-01-10 03:20:28 -0800725{
726 INIT_LIST_HEAD(&net->rules_ops);
727 spin_lock_init(&net->rules_mod_lock);
728 return 0;
729}
730
731static struct pernet_operations fib_rules_net_ops = {
732 .init = fib_rules_net_init,
733};
734
Thomas Graf14c0b972006-08-04 03:38:38 -0700735static int __init fib_rules_init(void)
736{
Denis V. Lunev5fd30ee2008-01-10 03:20:28 -0800737 int err;
Thomas Graf9d9e6a52007-03-25 23:20:05 -0700738 rtnl_register(PF_UNSPEC, RTM_NEWRULE, fib_nl_newrule, NULL);
739 rtnl_register(PF_UNSPEC, RTM_DELRULE, fib_nl_delrule, NULL);
Thomas Grafc4546732007-03-25 23:24:24 -0700740 rtnl_register(PF_UNSPEC, RTM_GETRULE, NULL, fib_nl_dumprule);
Thomas Graf9d9e6a52007-03-25 23:20:05 -0700741
Eric W. Biederman5d6d4802008-11-07 22:52:34 -0800742 err = register_pernet_subsys(&fib_rules_net_ops);
Denis V. Lunev5fd30ee2008-01-10 03:20:28 -0800743 if (err < 0)
744 goto fail;
745
Eric W. Biederman5d6d4802008-11-07 22:52:34 -0800746 err = register_netdevice_notifier(&fib_rules_notifier);
Denis V. Lunev5fd30ee2008-01-10 03:20:28 -0800747 if (err < 0)
748 goto fail_unregister;
Eric W. Biederman5d6d4802008-11-07 22:52:34 -0800749
Denis V. Lunev5fd30ee2008-01-10 03:20:28 -0800750 return 0;
751
752fail_unregister:
Eric W. Biederman5d6d4802008-11-07 22:52:34 -0800753 unregister_pernet_subsys(&fib_rules_net_ops);
Denis V. Lunev5fd30ee2008-01-10 03:20:28 -0800754fail:
755 rtnl_unregister(PF_UNSPEC, RTM_NEWRULE);
756 rtnl_unregister(PF_UNSPEC, RTM_DELRULE);
757 rtnl_unregister(PF_UNSPEC, RTM_GETRULE);
758 return err;
Thomas Graf14c0b972006-08-04 03:38:38 -0700759}
760
761subsys_initcall(fib_rules_init);