blob: 185c341fafbd079714fe3a563b8209d5c5f7ead4 [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
Stefan Tomanek73f56982013-08-03 14:14:43 +020036 r->suppress_prefixlen = -1;
37 r->suppress_ifgroup = -1;
38
Denis V. Lunev2994c632007-11-10 22:12:03 -080039 /* The lock is not required here, the list in unreacheable
40 * at the moment this function is called */
41 list_add_tail(&r->list, &ops->rules_list);
42 return 0;
43}
44EXPORT_SYMBOL(fib_default_rule_add);
45
Patrick McHardyd8a566b2010-04-13 05:03:15 +000046u32 fib_default_rule_pref(struct fib_rules_ops *ops)
47{
48 struct list_head *pos;
49 struct fib_rule *rule;
50
51 if (!list_empty(&ops->rules_list)) {
52 pos = ops->rules_list.next;
53 if (pos->next != &ops->rules_list) {
54 rule = list_entry(pos->next, struct fib_rule, list);
55 if (rule->pref)
56 return rule->pref - 1;
57 }
58 }
59
60 return 0;
61}
62EXPORT_SYMBOL(fib_default_rule_pref);
63
Denis V. Lunev9e3a5482008-01-20 16:46:41 -080064static void notify_rule_change(int event, struct fib_rule *rule,
Thomas Grafc17084d2006-08-15 00:32:48 -070065 struct fib_rules_ops *ops, struct nlmsghdr *nlh,
66 u32 pid);
Thomas Graf14c0b972006-08-04 03:38:38 -070067
Denis V. Lunev5fd30ee2008-01-10 03:20:28 -080068static struct fib_rules_ops *lookup_rules_ops(struct net *net, int family)
Thomas Graf14c0b972006-08-04 03:38:38 -070069{
70 struct fib_rules_ops *ops;
71
72 rcu_read_lock();
Denis V. Lunev5fd30ee2008-01-10 03:20:28 -080073 list_for_each_entry_rcu(ops, &net->rules_ops, list) {
Thomas Graf14c0b972006-08-04 03:38:38 -070074 if (ops->family == family) {
75 if (!try_module_get(ops->owner))
76 ops = NULL;
77 rcu_read_unlock();
78 return ops;
79 }
80 }
81 rcu_read_unlock();
82
83 return NULL;
84}
85
86static void rules_ops_put(struct fib_rules_ops *ops)
87{
88 if (ops)
89 module_put(ops->owner);
90}
91
Thomas Graf73417f62007-03-27 13:56:52 -070092static void flush_route_cache(struct fib_rules_ops *ops)
93{
94 if (ops->flush_cache)
Denis V. Lunevae299fc2008-07-05 19:01:28 -070095 ops->flush_cache(ops);
Thomas Graf73417f62007-03-27 13:56:52 -070096}
97
Eric W. Biedermane9c51582009-12-03 12:22:55 -080098static int __fib_rules_register(struct fib_rules_ops *ops)
Thomas Graf14c0b972006-08-04 03:38:38 -070099{
100 int err = -EEXIST;
101 struct fib_rules_ops *o;
Denis V. Lunev9e3a5482008-01-20 16:46:41 -0800102 struct net *net;
103
104 net = ops->fro_net;
Thomas Graf14c0b972006-08-04 03:38:38 -0700105
106 if (ops->rule_size < sizeof(struct fib_rule))
107 return -EINVAL;
108
109 if (ops->match == NULL || ops->configure == NULL ||
110 ops->compare == NULL || ops->fill == NULL ||
111 ops->action == NULL)
112 return -EINVAL;
113
Denis V. Lunev5fd30ee2008-01-10 03:20:28 -0800114 spin_lock(&net->rules_mod_lock);
115 list_for_each_entry(o, &net->rules_ops, list)
Thomas Graf14c0b972006-08-04 03:38:38 -0700116 if (ops->family == o->family)
117 goto errout;
118
Denis V. Lunev5fd30ee2008-01-10 03:20:28 -0800119 hold_net(net);
120 list_add_tail_rcu(&ops->list, &net->rules_ops);
Thomas Graf14c0b972006-08-04 03:38:38 -0700121 err = 0;
122errout:
Denis V. Lunev5fd30ee2008-01-10 03:20:28 -0800123 spin_unlock(&net->rules_mod_lock);
Thomas Graf14c0b972006-08-04 03:38:38 -0700124
125 return err;
126}
127
Eric W. Biedermane9c51582009-12-03 12:22:55 -0800128struct fib_rules_ops *
Patrick McHardy3d0c9c42010-04-26 16:02:04 +0200129fib_rules_register(const struct fib_rules_ops *tmpl, struct net *net)
Eric W. Biedermane9c51582009-12-03 12:22:55 -0800130{
131 struct fib_rules_ops *ops;
132 int err;
133
Eric Dumazet2fb35732010-03-09 20:03:38 +0000134 ops = kmemdup(tmpl, sizeof(*ops), GFP_KERNEL);
Eric W. Biedermane9c51582009-12-03 12:22:55 -0800135 if (ops == NULL)
136 return ERR_PTR(-ENOMEM);
137
138 INIT_LIST_HEAD(&ops->rules_list);
139 ops->fro_net = net;
140
141 err = __fib_rules_register(ops);
142 if (err) {
143 kfree(ops);
144 ops = ERR_PTR(err);
145 }
146
147 return ops;
148}
Thomas Graf14c0b972006-08-04 03:38:38 -0700149EXPORT_SYMBOL_GPL(fib_rules_register);
150
stephen hemminger1df99162010-10-04 20:14:17 +0000151static void fib_rules_cleanup_ops(struct fib_rules_ops *ops)
Thomas Graf14c0b972006-08-04 03:38:38 -0700152{
153 struct fib_rule *rule, *tmp;
154
Denis V. Lunev76c72d42007-09-16 15:44:27 -0700155 list_for_each_entry_safe(rule, tmp, &ops->rules_list, list) {
Thomas Graf14c0b972006-08-04 03:38:38 -0700156 list_del_rcu(&rule->list);
David S. Miller7a9bc9b2012-06-29 01:32:45 -0700157 if (ops->delete)
158 ops->delete(rule);
Thomas Graf14c0b972006-08-04 03:38:38 -0700159 fib_rule_put(rule);
160 }
161}
162
Eric W. Biedermane9c51582009-12-03 12:22:55 -0800163static void fib_rules_put_rcu(struct rcu_head *head)
164{
165 struct fib_rules_ops *ops = container_of(head, struct fib_rules_ops, rcu);
166 struct net *net = ops->fro_net;
167
168 release_net(net);
169 kfree(ops);
170}
171
Denis V. Lunev9e3a5482008-01-20 16:46:41 -0800172void fib_rules_unregister(struct fib_rules_ops *ops)
Thomas Graf14c0b972006-08-04 03:38:38 -0700173{
Denis V. Lunev9e3a5482008-01-20 16:46:41 -0800174 struct net *net = ops->fro_net;
Thomas Graf14c0b972006-08-04 03:38:38 -0700175
Denis V. Lunev5fd30ee2008-01-10 03:20:28 -0800176 spin_lock(&net->rules_mod_lock);
Denis V. Lunev72132c1b2008-01-14 22:59:30 -0800177 list_del_rcu(&ops->list);
178 fib_rules_cleanup_ops(ops);
Denis V. Lunev5fd30ee2008-01-10 03:20:28 -0800179 spin_unlock(&net->rules_mod_lock);
Thomas Graf14c0b972006-08-04 03:38:38 -0700180
Eric W. Biedermane9c51582009-12-03 12:22:55 -0800181 call_rcu(&ops->rcu, fib_rules_put_rcu);
Thomas Graf14c0b972006-08-04 03:38:38 -0700182}
Thomas Graf14c0b972006-08-04 03:38:38 -0700183EXPORT_SYMBOL_GPL(fib_rules_unregister);
184
Thomas Graf3dfbcc42006-11-09 15:23:20 -0800185static int fib_rule_match(struct fib_rule *rule, struct fib_rules_ops *ops,
186 struct flowi *fl, int flags)
187{
188 int ret = 0;
189
David S. Miller1d28f422011-03-12 00:29:39 -0500190 if (rule->iifindex && (rule->iifindex != fl->flowi_iif))
Thomas Graf3dfbcc42006-11-09 15:23:20 -0800191 goto out;
192
David S. Miller1d28f422011-03-12 00:29:39 -0500193 if (rule->oifindex && (rule->oifindex != fl->flowi_oif))
Patrick McHardy1b038a52009-12-03 01:25:56 +0000194 goto out;
195
David S. Miller1d28f422011-03-12 00:29:39 -0500196 if ((rule->mark ^ fl->flowi_mark) & rule->mark_mask)
Thomas Graf3dfbcc42006-11-09 15:23:20 -0800197 goto out;
198
199 ret = ops->match(rule, fl, flags);
200out:
201 return (rule->flags & FIB_RULE_INVERT) ? !ret : ret;
202}
203
Thomas Graf14c0b972006-08-04 03:38:38 -0700204int fib_rules_lookup(struct fib_rules_ops *ops, struct flowi *fl,
205 int flags, struct fib_lookup_arg *arg)
206{
207 struct fib_rule *rule;
208 int err;
209
210 rcu_read_lock();
211
Denis V. Lunev76c72d42007-09-16 15:44:27 -0700212 list_for_each_entry_rcu(rule, &ops->rules_list, list) {
Thomas Graf0947c9fe2007-03-26 17:14:15 -0700213jumped:
Thomas Graf3dfbcc42006-11-09 15:23:20 -0800214 if (!fib_rule_match(rule, ops, fl, flags))
Thomas Graf14c0b972006-08-04 03:38:38 -0700215 continue;
216
Thomas Graf0947c9fe2007-03-26 17:14:15 -0700217 if (rule->action == FR_ACT_GOTO) {
218 struct fib_rule *target;
219
220 target = rcu_dereference(rule->ctarget);
221 if (target == NULL) {
222 continue;
223 } else {
224 rule = target;
225 goto jumped;
226 }
Thomas Graffa0b2d12007-03-26 17:38:53 -0700227 } else if (rule->action == FR_ACT_NOP)
228 continue;
229 else
Thomas Graf0947c9fe2007-03-26 17:14:15 -0700230 err = ops->action(rule, fl, flags, arg);
231
Stefan Tomanek7764a452013-08-01 02:17:15 +0200232 if (!err && ops->suppress && ops->suppress(rule, arg))
233 continue;
234
Thomas Graf14c0b972006-08-04 03:38:38 -0700235 if (err != -EAGAIN) {
Eric Dumazetebc0ffa2010-10-05 10:41:36 +0000236 if ((arg->flags & FIB_LOOKUP_NOREF) ||
237 likely(atomic_inc_not_zero(&rule->refcnt))) {
Eric Dumazet7fa7cb72010-09-27 04:18:27 +0000238 arg->rule = rule;
239 goto out;
240 }
241 break;
Thomas Graf14c0b972006-08-04 03:38:38 -0700242 }
243 }
244
Steven Whitehouse83886b62007-03-30 13:34:27 -0700245 err = -ESRCH;
Thomas Graf14c0b972006-08-04 03:38:38 -0700246out:
247 rcu_read_unlock();
248
249 return err;
250}
Thomas Graf14c0b972006-08-04 03:38:38 -0700251EXPORT_SYMBOL_GPL(fib_rules_lookup);
252
Thomas Grafe1701c62007-03-24 12:46:02 -0700253static int validate_rulemsg(struct fib_rule_hdr *frh, struct nlattr **tb,
254 struct fib_rules_ops *ops)
255{
256 int err = -EINVAL;
257
258 if (frh->src_len)
259 if (tb[FRA_SRC] == NULL ||
260 frh->src_len > (ops->addr_size * 8) ||
261 nla_len(tb[FRA_SRC]) != ops->addr_size)
262 goto errout;
263
264 if (frh->dst_len)
265 if (tb[FRA_DST] == NULL ||
266 frh->dst_len > (ops->addr_size * 8) ||
267 nla_len(tb[FRA_DST]) != ops->addr_size)
268 goto errout;
269
270 err = 0;
271errout:
272 return err;
273}
274
Thomas Graf661d2962013-03-21 07:45:29 +0000275static int fib_nl_newrule(struct sk_buff *skb, struct nlmsghdr* nlh)
Thomas Graf14c0b972006-08-04 03:38:38 -0700276{
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +0900277 struct net *net = sock_net(skb->sk);
Thomas Graf14c0b972006-08-04 03:38:38 -0700278 struct fib_rule_hdr *frh = nlmsg_data(nlh);
279 struct fib_rules_ops *ops = NULL;
280 struct fib_rule *rule, *r, *last = NULL;
281 struct nlattr *tb[FRA_MAX+1];
Thomas Graf0947c9fe2007-03-26 17:14:15 -0700282 int err = -EINVAL, unresolved = 0;
Thomas Graf14c0b972006-08-04 03:38:38 -0700283
284 if (nlh->nlmsg_len < nlmsg_msg_size(sizeof(*frh)))
285 goto errout;
286
Denis V. Lunev5fd30ee2008-01-10 03:20:28 -0800287 ops = lookup_rules_ops(net, frh->family);
Thomas Graf14c0b972006-08-04 03:38:38 -0700288 if (ops == NULL) {
Patrick McHardy2fe195c2008-07-01 19:59:37 -0700289 err = -EAFNOSUPPORT;
Thomas Graf14c0b972006-08-04 03:38:38 -0700290 goto errout;
291 }
292
293 err = nlmsg_parse(nlh, sizeof(*frh), tb, FRA_MAX, ops->policy);
294 if (err < 0)
295 goto errout;
296
Thomas Grafe1701c62007-03-24 12:46:02 -0700297 err = validate_rulemsg(frh, tb, ops);
298 if (err < 0)
299 goto errout;
300
Thomas Graf14c0b972006-08-04 03:38:38 -0700301 rule = kzalloc(ops->rule_size, GFP_KERNEL);
302 if (rule == NULL) {
303 err = -ENOMEM;
304 goto errout;
305 }
Denis V. Lunev3661a912008-04-16 02:01:56 -0700306 rule->fr_net = hold_net(net);
Thomas Graf14c0b972006-08-04 03:38:38 -0700307
308 if (tb[FRA_PRIORITY])
309 rule->pref = nla_get_u32(tb[FRA_PRIORITY]);
310
Patrick McHardy491deb24b2009-12-03 01:25:54 +0000311 if (tb[FRA_IIFNAME]) {
Thomas Graf14c0b972006-08-04 03:38:38 -0700312 struct net_device *dev;
313
Patrick McHardy491deb24b2009-12-03 01:25:54 +0000314 rule->iifindex = -1;
315 nla_strlcpy(rule->iifname, tb[FRA_IIFNAME], IFNAMSIZ);
316 dev = __dev_get_by_name(net, rule->iifname);
Thomas Graf14c0b972006-08-04 03:38:38 -0700317 if (dev)
Patrick McHardy491deb24b2009-12-03 01:25:54 +0000318 rule->iifindex = dev->ifindex;
Thomas Graf14c0b972006-08-04 03:38:38 -0700319 }
320
Patrick McHardy1b038a52009-12-03 01:25:56 +0000321 if (tb[FRA_OIFNAME]) {
322 struct net_device *dev;
323
324 rule->oifindex = -1;
325 nla_strlcpy(rule->oifname, tb[FRA_OIFNAME], IFNAMSIZ);
326 dev = __dev_get_by_name(net, rule->oifname);
327 if (dev)
328 rule->oifindex = dev->ifindex;
329 }
330
Thomas Grafb8964ed2006-11-09 15:22:18 -0800331 if (tb[FRA_FWMARK]) {
332 rule->mark = nla_get_u32(tb[FRA_FWMARK]);
333 if (rule->mark)
334 /* compatibility: if the mark value is non-zero all bits
335 * are compared unless a mask is explicitly specified.
336 */
337 rule->mark_mask = 0xFFFFFFFF;
338 }
339
340 if (tb[FRA_FWMASK])
341 rule->mark_mask = nla_get_u32(tb[FRA_FWMASK]);
342
Thomas Graf14c0b972006-08-04 03:38:38 -0700343 rule->action = frh->action;
344 rule->flags = frh->flags;
Patrick McHardy9e762a42006-08-10 23:09:48 -0700345 rule->table = frh_get_table(frh, tb);
Stefan Tomanek73f56982013-08-03 14:14:43 +0200346 if (tb[FRA_SUPPRESS_PREFIXLEN])
347 rule->suppress_prefixlen = nla_get_u32(tb[FRA_SUPPRESS_PREFIXLEN]);
348 else
349 rule->suppress_prefixlen = -1;
Thomas Graf14c0b972006-08-04 03:38:38 -0700350
Stefan Tomanek6ef94cf2013-08-02 17:19:56 +0200351 if (tb[FRA_SUPPRESS_IFGROUP])
352 rule->suppress_ifgroup = nla_get_u32(tb[FRA_SUPPRESS_IFGROUP]);
Stefan Tomanek73f56982013-08-03 14:14:43 +0200353 else
354 rule->suppress_ifgroup = -1;
Stefan Tomanek6ef94cf2013-08-02 17:19:56 +0200355
Patrick McHardy5adef182009-12-03 01:25:57 +0000356 if (!tb[FRA_PRIORITY] && ops->default_pref)
Denis V. Lunev868d13a2008-01-10 03:18:25 -0800357 rule->pref = ops->default_pref(ops);
Thomas Graf14c0b972006-08-04 03:38:38 -0700358
Thomas Graf0947c9fe2007-03-26 17:14:15 -0700359 err = -EINVAL;
360 if (tb[FRA_GOTO]) {
361 if (rule->action != FR_ACT_GOTO)
362 goto errout_free;
363
364 rule->target = nla_get_u32(tb[FRA_GOTO]);
365 /* Backward jumps are prohibited to avoid endless loops */
366 if (rule->target <= rule->pref)
367 goto errout_free;
368
Denis V. Lunev76c72d42007-09-16 15:44:27 -0700369 list_for_each_entry(r, &ops->rules_list, list) {
Thomas Graf0947c9fe2007-03-26 17:14:15 -0700370 if (r->pref == rule->target) {
Eric Dumazet7a2b03c2010-10-26 09:24:55 +0000371 RCU_INIT_POINTER(rule->ctarget, r);
Thomas Graf0947c9fe2007-03-26 17:14:15 -0700372 break;
373 }
374 }
375
Eric Dumazet7a2b03c2010-10-26 09:24:55 +0000376 if (rcu_dereference_protected(rule->ctarget, 1) == NULL)
Thomas Graf0947c9fe2007-03-26 17:14:15 -0700377 unresolved = 1;
378 } else if (rule->action == FR_ACT_GOTO)
379 goto errout_free;
380
Rami Rosen8b3521e2009-05-11 05:52:49 +0000381 err = ops->configure(rule, skb, frh, tb);
Thomas Graf14c0b972006-08-04 03:38:38 -0700382 if (err < 0)
383 goto errout_free;
384
Denis V. Lunev76c72d42007-09-16 15:44:27 -0700385 list_for_each_entry(r, &ops->rules_list, list) {
Thomas Graf14c0b972006-08-04 03:38:38 -0700386 if (r->pref > rule->pref)
387 break;
388 last = r;
389 }
390
391 fib_rule_get(rule);
392
Eric Dumazetebb9fed2010-10-23 09:44:25 +0000393 if (last)
394 list_add_rcu(&rule->list, &last->list);
395 else
396 list_add_rcu(&rule->list, &ops->rules_list);
397
Thomas Graf0947c9fe2007-03-26 17:14:15 -0700398 if (ops->unresolved_rules) {
399 /*
400 * There are unresolved goto rules in the list, check if
401 * any of them are pointing to this new rule.
402 */
Denis V. Lunev76c72d42007-09-16 15:44:27 -0700403 list_for_each_entry(r, &ops->rules_list, list) {
Thomas Graf0947c9fe2007-03-26 17:14:15 -0700404 if (r->action == FR_ACT_GOTO &&
Gao feng561dac22011-09-11 15:36:05 +0000405 r->target == rule->pref &&
406 rtnl_dereference(r->ctarget) == NULL) {
Thomas Graf0947c9fe2007-03-26 17:14:15 -0700407 rcu_assign_pointer(r->ctarget, rule);
408 if (--ops->unresolved_rules == 0)
409 break;
410 }
411 }
412 }
413
414 if (rule->action == FR_ACT_GOTO)
415 ops->nr_goto_rules++;
416
417 if (unresolved)
418 ops->unresolved_rules++;
419
Eric W. Biederman15e47302012-09-07 20:12:54 +0000420 notify_rule_change(RTM_NEWRULE, rule, ops, nlh, NETLINK_CB(skb).portid);
Thomas Graf73417f62007-03-27 13:56:52 -0700421 flush_route_cache(ops);
Thomas Graf14c0b972006-08-04 03:38:38 -0700422 rules_ops_put(ops);
423 return 0;
424
425errout_free:
Denis V. Lunev3661a912008-04-16 02:01:56 -0700426 release_net(rule->fr_net);
Thomas Graf14c0b972006-08-04 03:38:38 -0700427 kfree(rule);
428errout:
429 rules_ops_put(ops);
430 return err;
431}
432
Thomas Graf661d2962013-03-21 07:45:29 +0000433static int fib_nl_delrule(struct sk_buff *skb, struct nlmsghdr* nlh)
Thomas Graf14c0b972006-08-04 03:38:38 -0700434{
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +0900435 struct net *net = sock_net(skb->sk);
Thomas Graf14c0b972006-08-04 03:38:38 -0700436 struct fib_rule_hdr *frh = nlmsg_data(nlh);
437 struct fib_rules_ops *ops = NULL;
Thomas Graf0947c9fe2007-03-26 17:14:15 -0700438 struct fib_rule *rule, *tmp;
Thomas Graf14c0b972006-08-04 03:38:38 -0700439 struct nlattr *tb[FRA_MAX+1];
440 int err = -EINVAL;
441
442 if (nlh->nlmsg_len < nlmsg_msg_size(sizeof(*frh)))
443 goto errout;
444
Denis V. Lunev5fd30ee2008-01-10 03:20:28 -0800445 ops = lookup_rules_ops(net, frh->family);
Thomas Graf14c0b972006-08-04 03:38:38 -0700446 if (ops == NULL) {
Patrick McHardy2fe195c2008-07-01 19:59:37 -0700447 err = -EAFNOSUPPORT;
Thomas Graf14c0b972006-08-04 03:38:38 -0700448 goto errout;
449 }
450
451 err = nlmsg_parse(nlh, sizeof(*frh), tb, FRA_MAX, ops->policy);
452 if (err < 0)
453 goto errout;
454
Thomas Grafe1701c62007-03-24 12:46:02 -0700455 err = validate_rulemsg(frh, tb, ops);
456 if (err < 0)
457 goto errout;
458
Denis V. Lunev76c72d42007-09-16 15:44:27 -0700459 list_for_each_entry(rule, &ops->rules_list, list) {
Thomas Graf14c0b972006-08-04 03:38:38 -0700460 if (frh->action && (frh->action != rule->action))
461 continue;
462
Andreas Henriksson13eb2ab2013-11-07 18:26:38 +0100463 if (frh_get_table(frh, tb) &&
464 (frh_get_table(frh, tb) != rule->table))
Thomas Graf14c0b972006-08-04 03:38:38 -0700465 continue;
466
467 if (tb[FRA_PRIORITY] &&
468 (rule->pref != nla_get_u32(tb[FRA_PRIORITY])))
469 continue;
470
Patrick McHardy491deb24b2009-12-03 01:25:54 +0000471 if (tb[FRA_IIFNAME] &&
472 nla_strcmp(tb[FRA_IIFNAME], rule->iifname))
Thomas Graf14c0b972006-08-04 03:38:38 -0700473 continue;
474
Patrick McHardy1b038a52009-12-03 01:25:56 +0000475 if (tb[FRA_OIFNAME] &&
476 nla_strcmp(tb[FRA_OIFNAME], rule->oifname))
477 continue;
478
Thomas Grafb8964ed2006-11-09 15:22:18 -0800479 if (tb[FRA_FWMARK] &&
480 (rule->mark != nla_get_u32(tb[FRA_FWMARK])))
481 continue;
482
483 if (tb[FRA_FWMASK] &&
484 (rule->mark_mask != nla_get_u32(tb[FRA_FWMASK])))
485 continue;
486
Thomas Graf14c0b972006-08-04 03:38:38 -0700487 if (!ops->compare(rule, frh, tb))
488 continue;
489
490 if (rule->flags & FIB_RULE_PERMANENT) {
491 err = -EPERM;
492 goto errout;
493 }
494
495 list_del_rcu(&rule->list);
Thomas Graf0947c9fe2007-03-26 17:14:15 -0700496
Yan, Zhengafaef732011-10-17 15:20:28 +0000497 if (rule->action == FR_ACT_GOTO) {
Thomas Graf0947c9fe2007-03-26 17:14:15 -0700498 ops->nr_goto_rules--;
Yan, Zhengafaef732011-10-17 15:20:28 +0000499 if (rtnl_dereference(rule->ctarget) == NULL)
500 ops->unresolved_rules--;
501 }
Thomas Graf0947c9fe2007-03-26 17:14:15 -0700502
503 /*
504 * Check if this rule is a target to any of them. If so,
505 * disable them. As this operation is eventually very
506 * expensive, it is only performed if goto rules have
507 * actually been added.
508 */
509 if (ops->nr_goto_rules > 0) {
Denis V. Lunev76c72d42007-09-16 15:44:27 -0700510 list_for_each_entry(tmp, &ops->rules_list, list) {
Eric Dumazet7a2b03c2010-10-26 09:24:55 +0000511 if (rtnl_dereference(tmp->ctarget) == rule) {
Stephen Hemmingera9b3cd72011-08-01 16:19:00 +0000512 RCU_INIT_POINTER(tmp->ctarget, NULL);
Thomas Graf0947c9fe2007-03-26 17:14:15 -0700513 ops->unresolved_rules++;
514 }
515 }
516 }
517
Denis V. Lunev9e3a5482008-01-20 16:46:41 -0800518 notify_rule_change(RTM_DELRULE, rule, ops, nlh,
Eric W. Biederman15e47302012-09-07 20:12:54 +0000519 NETLINK_CB(skb).portid);
David S. Miller7a9bc9b2012-06-29 01:32:45 -0700520 if (ops->delete)
521 ops->delete(rule);
Thomas Graf14c0b972006-08-04 03:38:38 -0700522 fib_rule_put(rule);
Thomas Graf73417f62007-03-27 13:56:52 -0700523 flush_route_cache(ops);
Thomas Graf14c0b972006-08-04 03:38:38 -0700524 rules_ops_put(ops);
525 return 0;
526 }
527
528 err = -ENOENT;
529errout:
530 rules_ops_put(ops);
531 return err;
532}
533
Thomas Graf339bf982006-11-10 14:10:15 -0800534static inline size_t fib_rule_nlmsg_size(struct fib_rules_ops *ops,
535 struct fib_rule *rule)
536{
537 size_t payload = NLMSG_ALIGN(sizeof(struct fib_rule_hdr))
Patrick McHardy491deb24b2009-12-03 01:25:54 +0000538 + nla_total_size(IFNAMSIZ) /* FRA_IIFNAME */
Patrick McHardy1b038a52009-12-03 01:25:56 +0000539 + nla_total_size(IFNAMSIZ) /* FRA_OIFNAME */
Thomas Graf339bf982006-11-10 14:10:15 -0800540 + nla_total_size(4) /* FRA_PRIORITY */
541 + nla_total_size(4) /* FRA_TABLE */
Stefan Tomanek73f56982013-08-03 14:14:43 +0200542 + nla_total_size(4) /* FRA_SUPPRESS_PREFIXLEN */
Stefan Tomanek6ef94cf2013-08-02 17:19:56 +0200543 + nla_total_size(4) /* FRA_SUPPRESS_IFGROUP */
Thomas Graf339bf982006-11-10 14:10:15 -0800544 + nla_total_size(4) /* FRA_FWMARK */
545 + nla_total_size(4); /* FRA_FWMASK */
546
547 if (ops->nlmsg_payload)
548 payload += ops->nlmsg_payload(rule);
549
550 return payload;
551}
552
Thomas Graf14c0b972006-08-04 03:38:38 -0700553static int fib_nl_fill_rule(struct sk_buff *skb, struct fib_rule *rule,
554 u32 pid, u32 seq, int type, int flags,
555 struct fib_rules_ops *ops)
556{
557 struct nlmsghdr *nlh;
558 struct fib_rule_hdr *frh;
559
560 nlh = nlmsg_put(skb, pid, seq, type, sizeof(*frh), flags);
561 if (nlh == NULL)
Patrick McHardy26932562007-01-31 23:16:40 -0800562 return -EMSGSIZE;
Thomas Graf14c0b972006-08-04 03:38:38 -0700563
564 frh = nlmsg_data(nlh);
Patrick McHardy28bb1722010-04-13 05:03:16 +0000565 frh->family = ops->family;
Thomas Graf14c0b972006-08-04 03:38:38 -0700566 frh->table = rule->table;
David S. Miller0e3cea72012-04-01 20:47:01 -0400567 if (nla_put_u32(skb, FRA_TABLE, rule->table))
568 goto nla_put_failure;
Stefan Tomanek73f56982013-08-03 14:14:43 +0200569 if (nla_put_u32(skb, FRA_SUPPRESS_PREFIXLEN, rule->suppress_prefixlen))
Stefan Tomanek7764a452013-08-01 02:17:15 +0200570 goto nla_put_failure;
Thomas Graf14c0b972006-08-04 03:38:38 -0700571 frh->res1 = 0;
572 frh->res2 = 0;
573 frh->action = rule->action;
574 frh->flags = rule->flags;
575
Eric Dumazet7a2b03c2010-10-26 09:24:55 +0000576 if (rule->action == FR_ACT_GOTO &&
Eric Dumazet33d480c2011-08-11 19:30:52 +0000577 rcu_access_pointer(rule->ctarget) == NULL)
Thomas Graf0947c9fe2007-03-26 17:14:15 -0700578 frh->flags |= FIB_RULE_UNRESOLVED;
579
Patrick McHardy491deb24b2009-12-03 01:25:54 +0000580 if (rule->iifname[0]) {
David S. Miller0e3cea72012-04-01 20:47:01 -0400581 if (nla_put_string(skb, FRA_IIFNAME, rule->iifname))
582 goto nla_put_failure;
Patrick McHardy491deb24b2009-12-03 01:25:54 +0000583 if (rule->iifindex == -1)
584 frh->flags |= FIB_RULE_IIF_DETACHED;
Thomas Graf2b443682007-03-26 17:37:59 -0700585 }
586
Patrick McHardy1b038a52009-12-03 01:25:56 +0000587 if (rule->oifname[0]) {
David S. Miller0e3cea72012-04-01 20:47:01 -0400588 if (nla_put_string(skb, FRA_OIFNAME, rule->oifname))
589 goto nla_put_failure;
Patrick McHardy1b038a52009-12-03 01:25:56 +0000590 if (rule->oifindex == -1)
591 frh->flags |= FIB_RULE_OIF_DETACHED;
592 }
593
David S. Miller0e3cea72012-04-01 20:47:01 -0400594 if ((rule->pref &&
595 nla_put_u32(skb, FRA_PRIORITY, rule->pref)) ||
596 (rule->mark &&
597 nla_put_u32(skb, FRA_FWMARK, rule->mark)) ||
598 ((rule->mark_mask || rule->mark) &&
599 nla_put_u32(skb, FRA_FWMASK, rule->mark_mask)) ||
600 (rule->target &&
601 nla_put_u32(skb, FRA_GOTO, rule->target)))
602 goto nla_put_failure;
Stefan Tomanek6ef94cf2013-08-02 17:19:56 +0200603
604 if (rule->suppress_ifgroup != -1) {
605 if (nla_put_u32(skb, FRA_SUPPRESS_IFGROUP, rule->suppress_ifgroup))
606 goto nla_put_failure;
607 }
608
Rami Rosen04af8cf2009-05-20 17:26:23 -0700609 if (ops->fill(rule, skb, frh) < 0)
Thomas Graf14c0b972006-08-04 03:38:38 -0700610 goto nla_put_failure;
611
612 return nlmsg_end(skb, nlh);
613
614nla_put_failure:
Patrick McHardy26932562007-01-31 23:16:40 -0800615 nlmsg_cancel(skb, nlh);
616 return -EMSGSIZE;
Thomas Graf14c0b972006-08-04 03:38:38 -0700617}
618
Thomas Grafc4546732007-03-25 23:24:24 -0700619static int dump_rules(struct sk_buff *skb, struct netlink_callback *cb,
620 struct fib_rules_ops *ops)
Thomas Graf14c0b972006-08-04 03:38:38 -0700621{
622 int idx = 0;
623 struct fib_rule *rule;
Thomas Graf14c0b972006-08-04 03:38:38 -0700624
Eric Dumazete67f88d2011-04-27 22:56:07 +0000625 rcu_read_lock();
626 list_for_each_entry_rcu(rule, &ops->rules_list, list) {
Thomas Grafc4546732007-03-25 23:24:24 -0700627 if (idx < cb->args[1])
Thomas Graf14c0b972006-08-04 03:38:38 -0700628 goto skip;
629
Eric W. Biederman15e47302012-09-07 20:12:54 +0000630 if (fib_nl_fill_rule(skb, rule, NETLINK_CB(cb->skb).portid,
Thomas Graf14c0b972006-08-04 03:38:38 -0700631 cb->nlh->nlmsg_seq, RTM_NEWRULE,
632 NLM_F_MULTI, ops) < 0)
633 break;
634skip:
635 idx++;
636 }
Eric Dumazet2907c352011-05-25 07:34:04 +0000637 rcu_read_unlock();
Thomas Grafc4546732007-03-25 23:24:24 -0700638 cb->args[1] = idx;
Thomas Graf14c0b972006-08-04 03:38:38 -0700639 rules_ops_put(ops);
640
641 return skb->len;
642}
643
Thomas Grafc4546732007-03-25 23:24:24 -0700644static int fib_nl_dumprule(struct sk_buff *skb, struct netlink_callback *cb)
645{
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +0900646 struct net *net = sock_net(skb->sk);
Thomas Grafc4546732007-03-25 23:24:24 -0700647 struct fib_rules_ops *ops;
648 int idx = 0, family;
649
650 family = rtnl_msg_family(cb->nlh);
651 if (family != AF_UNSPEC) {
652 /* Protocol specific dump request */
Denis V. Lunev5fd30ee2008-01-10 03:20:28 -0800653 ops = lookup_rules_ops(net, family);
Thomas Grafc4546732007-03-25 23:24:24 -0700654 if (ops == NULL)
655 return -EAFNOSUPPORT;
656
657 return dump_rules(skb, cb, ops);
658 }
659
660 rcu_read_lock();
Denis V. Lunev5fd30ee2008-01-10 03:20:28 -0800661 list_for_each_entry_rcu(ops, &net->rules_ops, list) {
Thomas Grafc4546732007-03-25 23:24:24 -0700662 if (idx < cb->args[0] || !try_module_get(ops->owner))
663 goto skip;
664
665 if (dump_rules(skb, cb, ops) < 0)
666 break;
667
668 cb->args[1] = 0;
Eric Dumazet2fb35732010-03-09 20:03:38 +0000669skip:
Thomas Grafc4546732007-03-25 23:24:24 -0700670 idx++;
671 }
672 rcu_read_unlock();
673 cb->args[0] = idx;
674
675 return skb->len;
676}
Thomas Graf14c0b972006-08-04 03:38:38 -0700677
Denis V. Lunev9e3a5482008-01-20 16:46:41 -0800678static void notify_rule_change(int event, struct fib_rule *rule,
Thomas Grafc17084d2006-08-15 00:32:48 -0700679 struct fib_rules_ops *ops, struct nlmsghdr *nlh,
680 u32 pid)
Thomas Graf14c0b972006-08-04 03:38:38 -0700681{
Denis V. Lunev9e3a5482008-01-20 16:46:41 -0800682 struct net *net;
Thomas Grafc17084d2006-08-15 00:32:48 -0700683 struct sk_buff *skb;
684 int err = -ENOBUFS;
Thomas Graf14c0b972006-08-04 03:38:38 -0700685
Denis V. Lunev9e3a5482008-01-20 16:46:41 -0800686 net = ops->fro_net;
Thomas Graf339bf982006-11-10 14:10:15 -0800687 skb = nlmsg_new(fib_rule_nlmsg_size(ops, rule), GFP_KERNEL);
Thomas Graf14c0b972006-08-04 03:38:38 -0700688 if (skb == NULL)
Thomas Grafc17084d2006-08-15 00:32:48 -0700689 goto errout;
690
691 err = fib_nl_fill_rule(skb, rule, pid, nlh->nlmsg_seq, event, 0, ops);
Patrick McHardy26932562007-01-31 23:16:40 -0800692 if (err < 0) {
693 /* -EMSGSIZE implies BUG in fib_rule_nlmsg_size() */
694 WARN_ON(err == -EMSGSIZE);
695 kfree_skb(skb);
696 goto errout;
697 }
Denis V. Lunev9e3a5482008-01-20 16:46:41 -0800698
Pablo Neira Ayuso1ce85fe2009-02-24 23:18:28 -0800699 rtnl_notify(skb, net, pid, ops->nlgroup, nlh, GFP_KERNEL);
700 return;
Thomas Grafc17084d2006-08-15 00:32:48 -0700701errout:
702 if (err < 0)
Denis V. Lunev5fd30ee2008-01-10 03:20:28 -0800703 rtnl_set_sk_err(net, ops->nlgroup, err);
Thomas Graf14c0b972006-08-04 03:38:38 -0700704}
705
706static void attach_rules(struct list_head *rules, struct net_device *dev)
707{
708 struct fib_rule *rule;
709
710 list_for_each_entry(rule, rules, list) {
Patrick McHardy491deb24b2009-12-03 01:25:54 +0000711 if (rule->iifindex == -1 &&
712 strcmp(dev->name, rule->iifname) == 0)
713 rule->iifindex = dev->ifindex;
Patrick McHardy1b038a52009-12-03 01:25:56 +0000714 if (rule->oifindex == -1 &&
715 strcmp(dev->name, rule->oifname) == 0)
716 rule->oifindex = dev->ifindex;
Thomas Graf14c0b972006-08-04 03:38:38 -0700717 }
718}
719
720static void detach_rules(struct list_head *rules, struct net_device *dev)
721{
722 struct fib_rule *rule;
723
Patrick McHardy1b038a52009-12-03 01:25:56 +0000724 list_for_each_entry(rule, rules, list) {
Patrick McHardy491deb24b2009-12-03 01:25:54 +0000725 if (rule->iifindex == dev->ifindex)
726 rule->iifindex = -1;
Patrick McHardy1b038a52009-12-03 01:25:56 +0000727 if (rule->oifindex == dev->ifindex)
728 rule->oifindex = -1;
729 }
Thomas Graf14c0b972006-08-04 03:38:38 -0700730}
731
732
733static int fib_rules_event(struct notifier_block *this, unsigned long event,
Jiri Pirko351638e2013-05-28 01:30:21 +0000734 void *ptr)
Thomas Graf14c0b972006-08-04 03:38:38 -0700735{
Jiri Pirko351638e2013-05-28 01:30:21 +0000736 struct net_device *dev = netdev_notifier_info_to_dev(ptr);
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +0900737 struct net *net = dev_net(dev);
Thomas Graf14c0b972006-08-04 03:38:38 -0700738 struct fib_rules_ops *ops;
739
Eric Dumazet748e2d92012-08-22 21:50:59 +0000740 ASSERT_RTNL();
Thomas Graf14c0b972006-08-04 03:38:38 -0700741
742 switch (event) {
743 case NETDEV_REGISTER:
Denis V. Lunev5fd30ee2008-01-10 03:20:28 -0800744 list_for_each_entry(ops, &net->rules_ops, list)
Denis V. Lunev76c72d42007-09-16 15:44:27 -0700745 attach_rules(&ops->rules_list, dev);
Thomas Graf14c0b972006-08-04 03:38:38 -0700746 break;
747
Maciej Żenczykowski946c0322014-02-07 16:23:48 -0800748 case NETDEV_CHANGENAME:
749 list_for_each_entry(ops, &net->rules_ops, list) {
750 detach_rules(&ops->rules_list, dev);
751 attach_rules(&ops->rules_list, dev);
752 }
753 break;
754
Thomas Graf14c0b972006-08-04 03:38:38 -0700755 case NETDEV_UNREGISTER:
Denis V. Lunev5fd30ee2008-01-10 03:20:28 -0800756 list_for_each_entry(ops, &net->rules_ops, list)
Denis V. Lunev76c72d42007-09-16 15:44:27 -0700757 detach_rules(&ops->rules_list, dev);
Thomas Graf14c0b972006-08-04 03:38:38 -0700758 break;
759 }
760
Thomas Graf14c0b972006-08-04 03:38:38 -0700761 return NOTIFY_DONE;
762}
763
764static struct notifier_block fib_rules_notifier = {
765 .notifier_call = fib_rules_event,
766};
767
Alexey Dobriyan2c8c1e72010-01-17 03:35:32 +0000768static int __net_init fib_rules_net_init(struct net *net)
Denis V. Lunev5fd30ee2008-01-10 03:20:28 -0800769{
770 INIT_LIST_HEAD(&net->rules_ops);
771 spin_lock_init(&net->rules_mod_lock);
772 return 0;
773}
774
775static struct pernet_operations fib_rules_net_ops = {
776 .init = fib_rules_net_init,
777};
778
Thomas Graf14c0b972006-08-04 03:38:38 -0700779static int __init fib_rules_init(void)
780{
Denis V. Lunev5fd30ee2008-01-10 03:20:28 -0800781 int err;
Greg Rosec7ac8672011-06-10 01:27:09 +0000782 rtnl_register(PF_UNSPEC, RTM_NEWRULE, fib_nl_newrule, NULL, NULL);
783 rtnl_register(PF_UNSPEC, RTM_DELRULE, fib_nl_delrule, NULL, NULL);
784 rtnl_register(PF_UNSPEC, RTM_GETRULE, NULL, fib_nl_dumprule, NULL);
Thomas Graf9d9e6a52007-03-25 23:20:05 -0700785
Eric W. Biederman5d6d4802008-11-07 22:52:34 -0800786 err = register_pernet_subsys(&fib_rules_net_ops);
Denis V. Lunev5fd30ee2008-01-10 03:20:28 -0800787 if (err < 0)
788 goto fail;
789
Eric W. Biederman5d6d4802008-11-07 22:52:34 -0800790 err = register_netdevice_notifier(&fib_rules_notifier);
Denis V. Lunev5fd30ee2008-01-10 03:20:28 -0800791 if (err < 0)
792 goto fail_unregister;
Eric W. Biederman5d6d4802008-11-07 22:52:34 -0800793
Denis V. Lunev5fd30ee2008-01-10 03:20:28 -0800794 return 0;
795
796fail_unregister:
Eric W. Biederman5d6d4802008-11-07 22:52:34 -0800797 unregister_pernet_subsys(&fib_rules_net_ops);
Denis V. Lunev5fd30ee2008-01-10 03:20:28 -0800798fail:
799 rtnl_unregister(PF_UNSPEC, RTM_NEWRULE);
800 rtnl_unregister(PF_UNSPEC, RTM_DELRULE);
801 rtnl_unregister(PF_UNSPEC, RTM_GETRULE);
802 return err;
Thomas Graf14c0b972006-08-04 03:38:38 -0700803}
804
805subsys_initcall(fib_rules_init);