blob: 9a12668f7d62720c6ca18f09d13c45ea3e2ca2b2 [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;
Eric W. Biedermanefd7ef12015-03-11 23:04:08 -050034 r->fr_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 list_add_tail_rcu(&ops->list, &net->rules_ops);
Thomas Graf14c0b972006-08-04 03:38:38 -0700120 err = 0;
121errout:
Denis V. Lunev5fd30ee2008-01-10 03:20:28 -0800122 spin_unlock(&net->rules_mod_lock);
Thomas Graf14c0b972006-08-04 03:38:38 -0700123
124 return err;
125}
126
Eric W. Biedermane9c51582009-12-03 12:22:55 -0800127struct fib_rules_ops *
Patrick McHardy3d0c9c42010-04-26 16:02:04 +0200128fib_rules_register(const struct fib_rules_ops *tmpl, struct net *net)
Eric W. Biedermane9c51582009-12-03 12:22:55 -0800129{
130 struct fib_rules_ops *ops;
131 int err;
132
Eric Dumazet2fb35732010-03-09 20:03:38 +0000133 ops = kmemdup(tmpl, sizeof(*ops), GFP_KERNEL);
Eric W. Biedermane9c51582009-12-03 12:22:55 -0800134 if (ops == NULL)
135 return ERR_PTR(-ENOMEM);
136
137 INIT_LIST_HEAD(&ops->rules_list);
138 ops->fro_net = net;
139
140 err = __fib_rules_register(ops);
141 if (err) {
142 kfree(ops);
143 ops = ERR_PTR(err);
144 }
145
146 return ops;
147}
Thomas Graf14c0b972006-08-04 03:38:38 -0700148EXPORT_SYMBOL_GPL(fib_rules_register);
149
stephen hemminger1df99162010-10-04 20:14:17 +0000150static void fib_rules_cleanup_ops(struct fib_rules_ops *ops)
Thomas Graf14c0b972006-08-04 03:38:38 -0700151{
152 struct fib_rule *rule, *tmp;
153
Denis V. Lunev76c72d42007-09-16 15:44:27 -0700154 list_for_each_entry_safe(rule, tmp, &ops->rules_list, list) {
Thomas Graf14c0b972006-08-04 03:38:38 -0700155 list_del_rcu(&rule->list);
David S. Miller7a9bc9b2012-06-29 01:32:45 -0700156 if (ops->delete)
157 ops->delete(rule);
Thomas Graf14c0b972006-08-04 03:38:38 -0700158 fib_rule_put(rule);
159 }
160}
161
Denis V. Lunev9e3a5482008-01-20 16:46:41 -0800162void fib_rules_unregister(struct fib_rules_ops *ops)
Thomas Graf14c0b972006-08-04 03:38:38 -0700163{
Denis V. Lunev9e3a5482008-01-20 16:46:41 -0800164 struct net *net = ops->fro_net;
Thomas Graf14c0b972006-08-04 03:38:38 -0700165
Denis V. Lunev5fd30ee2008-01-10 03:20:28 -0800166 spin_lock(&net->rules_mod_lock);
Denis V. Lunev72132c1b2008-01-14 22:59:30 -0800167 list_del_rcu(&ops->list);
Denis V. Lunev5fd30ee2008-01-10 03:20:28 -0800168 spin_unlock(&net->rules_mod_lock);
Thomas Graf14c0b972006-08-04 03:38:38 -0700169
WANG Cong419df122015-03-31 11:01:46 -0700170 fib_rules_cleanup_ops(ops);
Eric W. Biedermanefd7ef12015-03-11 23:04:08 -0500171 kfree_rcu(ops, rcu);
Thomas Graf14c0b972006-08-04 03:38:38 -0700172}
Thomas Graf14c0b972006-08-04 03:38:38 -0700173EXPORT_SYMBOL_GPL(fib_rules_unregister);
174
Thomas Graf3dfbcc42006-11-09 15:23:20 -0800175static int fib_rule_match(struct fib_rule *rule, struct fib_rules_ops *ops,
176 struct flowi *fl, int flags)
177{
178 int ret = 0;
179
David S. Miller1d28f422011-03-12 00:29:39 -0500180 if (rule->iifindex && (rule->iifindex != fl->flowi_iif))
Thomas Graf3dfbcc42006-11-09 15:23:20 -0800181 goto out;
182
David S. Miller1d28f422011-03-12 00:29:39 -0500183 if (rule->oifindex && (rule->oifindex != fl->flowi_oif))
Patrick McHardy1b038a52009-12-03 01:25:56 +0000184 goto out;
185
David S. Miller1d28f422011-03-12 00:29:39 -0500186 if ((rule->mark ^ fl->flowi_mark) & rule->mark_mask)
Thomas Graf3dfbcc42006-11-09 15:23:20 -0800187 goto out;
188
189 ret = ops->match(rule, fl, flags);
190out:
191 return (rule->flags & FIB_RULE_INVERT) ? !ret : ret;
192}
193
Thomas Graf14c0b972006-08-04 03:38:38 -0700194int fib_rules_lookup(struct fib_rules_ops *ops, struct flowi *fl,
195 int flags, struct fib_lookup_arg *arg)
196{
197 struct fib_rule *rule;
198 int err;
199
200 rcu_read_lock();
201
Denis V. Lunev76c72d42007-09-16 15:44:27 -0700202 list_for_each_entry_rcu(rule, &ops->rules_list, list) {
Thomas Graf0947c9fe2007-03-26 17:14:15 -0700203jumped:
Thomas Graf3dfbcc42006-11-09 15:23:20 -0800204 if (!fib_rule_match(rule, ops, fl, flags))
Thomas Graf14c0b972006-08-04 03:38:38 -0700205 continue;
206
Thomas Graf0947c9fe2007-03-26 17:14:15 -0700207 if (rule->action == FR_ACT_GOTO) {
208 struct fib_rule *target;
209
210 target = rcu_dereference(rule->ctarget);
211 if (target == NULL) {
212 continue;
213 } else {
214 rule = target;
215 goto jumped;
216 }
Thomas Graffa0b2d12007-03-26 17:38:53 -0700217 } else if (rule->action == FR_ACT_NOP)
218 continue;
219 else
Thomas Graf0947c9fe2007-03-26 17:14:15 -0700220 err = ops->action(rule, fl, flags, arg);
221
Stefan Tomanek7764a452013-08-01 02:17:15 +0200222 if (!err && ops->suppress && ops->suppress(rule, arg))
223 continue;
224
Thomas Graf14c0b972006-08-04 03:38:38 -0700225 if (err != -EAGAIN) {
Eric Dumazetebc0ffa2010-10-05 10:41:36 +0000226 if ((arg->flags & FIB_LOOKUP_NOREF) ||
227 likely(atomic_inc_not_zero(&rule->refcnt))) {
Eric Dumazet7fa7cb72010-09-27 04:18:27 +0000228 arg->rule = rule;
229 goto out;
230 }
231 break;
Thomas Graf14c0b972006-08-04 03:38:38 -0700232 }
233 }
234
Steven Whitehouse83886b62007-03-30 13:34:27 -0700235 err = -ESRCH;
Thomas Graf14c0b972006-08-04 03:38:38 -0700236out:
237 rcu_read_unlock();
238
239 return err;
240}
Thomas Graf14c0b972006-08-04 03:38:38 -0700241EXPORT_SYMBOL_GPL(fib_rules_lookup);
242
Thomas Grafe1701c62007-03-24 12:46:02 -0700243static int validate_rulemsg(struct fib_rule_hdr *frh, struct nlattr **tb,
244 struct fib_rules_ops *ops)
245{
246 int err = -EINVAL;
247
248 if (frh->src_len)
249 if (tb[FRA_SRC] == NULL ||
250 frh->src_len > (ops->addr_size * 8) ||
251 nla_len(tb[FRA_SRC]) != ops->addr_size)
252 goto errout;
253
254 if (frh->dst_len)
255 if (tb[FRA_DST] == NULL ||
256 frh->dst_len > (ops->addr_size * 8) ||
257 nla_len(tb[FRA_DST]) != ops->addr_size)
258 goto errout;
259
260 err = 0;
261errout:
262 return err;
263}
264
Thomas Graf661d2962013-03-21 07:45:29 +0000265static int fib_nl_newrule(struct sk_buff *skb, struct nlmsghdr* nlh)
Thomas Graf14c0b972006-08-04 03:38:38 -0700266{
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +0900267 struct net *net = sock_net(skb->sk);
Thomas Graf14c0b972006-08-04 03:38:38 -0700268 struct fib_rule_hdr *frh = nlmsg_data(nlh);
269 struct fib_rules_ops *ops = NULL;
270 struct fib_rule *rule, *r, *last = NULL;
271 struct nlattr *tb[FRA_MAX+1];
Thomas Graf0947c9fe2007-03-26 17:14:15 -0700272 int err = -EINVAL, unresolved = 0;
Thomas Graf14c0b972006-08-04 03:38:38 -0700273
274 if (nlh->nlmsg_len < nlmsg_msg_size(sizeof(*frh)))
275 goto errout;
276
Denis V. Lunev5fd30ee2008-01-10 03:20:28 -0800277 ops = lookup_rules_ops(net, frh->family);
Thomas Graf14c0b972006-08-04 03:38:38 -0700278 if (ops == NULL) {
Patrick McHardy2fe195c2008-07-01 19:59:37 -0700279 err = -EAFNOSUPPORT;
Thomas Graf14c0b972006-08-04 03:38:38 -0700280 goto errout;
281 }
282
283 err = nlmsg_parse(nlh, sizeof(*frh), tb, FRA_MAX, ops->policy);
284 if (err < 0)
285 goto errout;
286
Thomas Grafe1701c62007-03-24 12:46:02 -0700287 err = validate_rulemsg(frh, tb, ops);
288 if (err < 0)
289 goto errout;
290
Thomas Graf14c0b972006-08-04 03:38:38 -0700291 rule = kzalloc(ops->rule_size, GFP_KERNEL);
292 if (rule == NULL) {
293 err = -ENOMEM;
294 goto errout;
295 }
Eric W. Biedermanefd7ef12015-03-11 23:04:08 -0500296 rule->fr_net = net;
Thomas Graf14c0b972006-08-04 03:38:38 -0700297
298 if (tb[FRA_PRIORITY])
299 rule->pref = nla_get_u32(tb[FRA_PRIORITY]);
300
Patrick McHardy491deb24b2009-12-03 01:25:54 +0000301 if (tb[FRA_IIFNAME]) {
Thomas Graf14c0b972006-08-04 03:38:38 -0700302 struct net_device *dev;
303
Patrick McHardy491deb24b2009-12-03 01:25:54 +0000304 rule->iifindex = -1;
305 nla_strlcpy(rule->iifname, tb[FRA_IIFNAME], IFNAMSIZ);
306 dev = __dev_get_by_name(net, rule->iifname);
Thomas Graf14c0b972006-08-04 03:38:38 -0700307 if (dev)
Patrick McHardy491deb24b2009-12-03 01:25:54 +0000308 rule->iifindex = dev->ifindex;
Thomas Graf14c0b972006-08-04 03:38:38 -0700309 }
310
Patrick McHardy1b038a52009-12-03 01:25:56 +0000311 if (tb[FRA_OIFNAME]) {
312 struct net_device *dev;
313
314 rule->oifindex = -1;
315 nla_strlcpy(rule->oifname, tb[FRA_OIFNAME], IFNAMSIZ);
316 dev = __dev_get_by_name(net, rule->oifname);
317 if (dev)
318 rule->oifindex = dev->ifindex;
319 }
320
Thomas Grafb8964ed2006-11-09 15:22:18 -0800321 if (tb[FRA_FWMARK]) {
322 rule->mark = nla_get_u32(tb[FRA_FWMARK]);
323 if (rule->mark)
324 /* compatibility: if the mark value is non-zero all bits
325 * are compared unless a mask is explicitly specified.
326 */
327 rule->mark_mask = 0xFFFFFFFF;
328 }
329
330 if (tb[FRA_FWMASK])
331 rule->mark_mask = nla_get_u32(tb[FRA_FWMASK]);
332
Thomas Graf14c0b972006-08-04 03:38:38 -0700333 rule->action = frh->action;
334 rule->flags = frh->flags;
Patrick McHardy9e762a42006-08-10 23:09:48 -0700335 rule->table = frh_get_table(frh, tb);
Stefan Tomanek73f56982013-08-03 14:14:43 +0200336 if (tb[FRA_SUPPRESS_PREFIXLEN])
337 rule->suppress_prefixlen = nla_get_u32(tb[FRA_SUPPRESS_PREFIXLEN]);
338 else
339 rule->suppress_prefixlen = -1;
Thomas Graf14c0b972006-08-04 03:38:38 -0700340
Stefan Tomanek6ef94cf2013-08-02 17:19:56 +0200341 if (tb[FRA_SUPPRESS_IFGROUP])
342 rule->suppress_ifgroup = nla_get_u32(tb[FRA_SUPPRESS_IFGROUP]);
Stefan Tomanek73f56982013-08-03 14:14:43 +0200343 else
344 rule->suppress_ifgroup = -1;
Stefan Tomanek6ef94cf2013-08-02 17:19:56 +0200345
Patrick McHardy5adef182009-12-03 01:25:57 +0000346 if (!tb[FRA_PRIORITY] && ops->default_pref)
Denis V. Lunev868d13a2008-01-10 03:18:25 -0800347 rule->pref = ops->default_pref(ops);
Thomas Graf14c0b972006-08-04 03:38:38 -0700348
Thomas Graf0947c9fe2007-03-26 17:14:15 -0700349 err = -EINVAL;
350 if (tb[FRA_GOTO]) {
351 if (rule->action != FR_ACT_GOTO)
352 goto errout_free;
353
354 rule->target = nla_get_u32(tb[FRA_GOTO]);
355 /* Backward jumps are prohibited to avoid endless loops */
356 if (rule->target <= rule->pref)
357 goto errout_free;
358
Denis V. Lunev76c72d42007-09-16 15:44:27 -0700359 list_for_each_entry(r, &ops->rules_list, list) {
Thomas Graf0947c9fe2007-03-26 17:14:15 -0700360 if (r->pref == rule->target) {
Eric Dumazet7a2b03c2010-10-26 09:24:55 +0000361 RCU_INIT_POINTER(rule->ctarget, r);
Thomas Graf0947c9fe2007-03-26 17:14:15 -0700362 break;
363 }
364 }
365
Eric Dumazet7a2b03c2010-10-26 09:24:55 +0000366 if (rcu_dereference_protected(rule->ctarget, 1) == NULL)
Thomas Graf0947c9fe2007-03-26 17:14:15 -0700367 unresolved = 1;
368 } else if (rule->action == FR_ACT_GOTO)
369 goto errout_free;
370
Rami Rosen8b3521e2009-05-11 05:52:49 +0000371 err = ops->configure(rule, skb, frh, tb);
Thomas Graf14c0b972006-08-04 03:38:38 -0700372 if (err < 0)
373 goto errout_free;
374
Denis V. Lunev76c72d42007-09-16 15:44:27 -0700375 list_for_each_entry(r, &ops->rules_list, list) {
Thomas Graf14c0b972006-08-04 03:38:38 -0700376 if (r->pref > rule->pref)
377 break;
378 last = r;
379 }
380
381 fib_rule_get(rule);
382
Eric Dumazetebb9fed2010-10-23 09:44:25 +0000383 if (last)
384 list_add_rcu(&rule->list, &last->list);
385 else
386 list_add_rcu(&rule->list, &ops->rules_list);
387
Thomas Graf0947c9fe2007-03-26 17:14:15 -0700388 if (ops->unresolved_rules) {
389 /*
390 * There are unresolved goto rules in the list, check if
391 * any of them are pointing to this new rule.
392 */
Denis V. Lunev76c72d42007-09-16 15:44:27 -0700393 list_for_each_entry(r, &ops->rules_list, list) {
Thomas Graf0947c9fe2007-03-26 17:14:15 -0700394 if (r->action == FR_ACT_GOTO &&
Gao feng561dac22011-09-11 15:36:05 +0000395 r->target == rule->pref &&
396 rtnl_dereference(r->ctarget) == NULL) {
Thomas Graf0947c9fe2007-03-26 17:14:15 -0700397 rcu_assign_pointer(r->ctarget, rule);
398 if (--ops->unresolved_rules == 0)
399 break;
400 }
401 }
402 }
403
404 if (rule->action == FR_ACT_GOTO)
405 ops->nr_goto_rules++;
406
407 if (unresolved)
408 ops->unresolved_rules++;
409
Eric W. Biederman15e47302012-09-07 20:12:54 +0000410 notify_rule_change(RTM_NEWRULE, rule, ops, nlh, NETLINK_CB(skb).portid);
Thomas Graf73417f62007-03-27 13:56:52 -0700411 flush_route_cache(ops);
Thomas Graf14c0b972006-08-04 03:38:38 -0700412 rules_ops_put(ops);
413 return 0;
414
415errout_free:
416 kfree(rule);
417errout:
418 rules_ops_put(ops);
419 return err;
420}
421
Thomas Graf661d2962013-03-21 07:45:29 +0000422static int fib_nl_delrule(struct sk_buff *skb, struct nlmsghdr* nlh)
Thomas Graf14c0b972006-08-04 03:38:38 -0700423{
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +0900424 struct net *net = sock_net(skb->sk);
Thomas Graf14c0b972006-08-04 03:38:38 -0700425 struct fib_rule_hdr *frh = nlmsg_data(nlh);
426 struct fib_rules_ops *ops = NULL;
Thomas Graf0947c9fe2007-03-26 17:14:15 -0700427 struct fib_rule *rule, *tmp;
Thomas Graf14c0b972006-08-04 03:38:38 -0700428 struct nlattr *tb[FRA_MAX+1];
429 int err = -EINVAL;
430
431 if (nlh->nlmsg_len < nlmsg_msg_size(sizeof(*frh)))
432 goto errout;
433
Denis V. Lunev5fd30ee2008-01-10 03:20:28 -0800434 ops = lookup_rules_ops(net, frh->family);
Thomas Graf14c0b972006-08-04 03:38:38 -0700435 if (ops == NULL) {
Patrick McHardy2fe195c2008-07-01 19:59:37 -0700436 err = -EAFNOSUPPORT;
Thomas Graf14c0b972006-08-04 03:38:38 -0700437 goto errout;
438 }
439
440 err = nlmsg_parse(nlh, sizeof(*frh), tb, FRA_MAX, ops->policy);
441 if (err < 0)
442 goto errout;
443
Thomas Grafe1701c62007-03-24 12:46:02 -0700444 err = validate_rulemsg(frh, tb, ops);
445 if (err < 0)
446 goto errout;
447
Denis V. Lunev76c72d42007-09-16 15:44:27 -0700448 list_for_each_entry(rule, &ops->rules_list, list) {
Thomas Graf14c0b972006-08-04 03:38:38 -0700449 if (frh->action && (frh->action != rule->action))
450 continue;
451
Andreas Henriksson13eb2ab2013-11-07 18:26:38 +0100452 if (frh_get_table(frh, tb) &&
453 (frh_get_table(frh, tb) != rule->table))
Thomas Graf14c0b972006-08-04 03:38:38 -0700454 continue;
455
456 if (tb[FRA_PRIORITY] &&
457 (rule->pref != nla_get_u32(tb[FRA_PRIORITY])))
458 continue;
459
Patrick McHardy491deb24b2009-12-03 01:25:54 +0000460 if (tb[FRA_IIFNAME] &&
461 nla_strcmp(tb[FRA_IIFNAME], rule->iifname))
Thomas Graf14c0b972006-08-04 03:38:38 -0700462 continue;
463
Patrick McHardy1b038a52009-12-03 01:25:56 +0000464 if (tb[FRA_OIFNAME] &&
465 nla_strcmp(tb[FRA_OIFNAME], rule->oifname))
466 continue;
467
Thomas Grafb8964ed2006-11-09 15:22:18 -0800468 if (tb[FRA_FWMARK] &&
469 (rule->mark != nla_get_u32(tb[FRA_FWMARK])))
470 continue;
471
472 if (tb[FRA_FWMASK] &&
473 (rule->mark_mask != nla_get_u32(tb[FRA_FWMASK])))
474 continue;
475
Thomas Graf14c0b972006-08-04 03:38:38 -0700476 if (!ops->compare(rule, frh, tb))
477 continue;
478
479 if (rule->flags & FIB_RULE_PERMANENT) {
480 err = -EPERM;
481 goto errout;
482 }
483
Alexander Duyck0ddcf432015-03-06 13:47:00 -0800484 if (ops->delete) {
485 err = ops->delete(rule);
486 if (err)
487 goto errout;
488 }
489
Thomas Graf14c0b972006-08-04 03:38:38 -0700490 list_del_rcu(&rule->list);
Thomas Graf0947c9fe2007-03-26 17:14:15 -0700491
Yan, Zhengafaef732011-10-17 15:20:28 +0000492 if (rule->action == FR_ACT_GOTO) {
Thomas Graf0947c9fe2007-03-26 17:14:15 -0700493 ops->nr_goto_rules--;
Yan, Zhengafaef732011-10-17 15:20:28 +0000494 if (rtnl_dereference(rule->ctarget) == NULL)
495 ops->unresolved_rules--;
496 }
Thomas Graf0947c9fe2007-03-26 17:14:15 -0700497
498 /*
499 * Check if this rule is a target to any of them. If so,
500 * disable them. As this operation is eventually very
501 * expensive, it is only performed if goto rules have
502 * actually been added.
503 */
504 if (ops->nr_goto_rules > 0) {
Denis V. Lunev76c72d42007-09-16 15:44:27 -0700505 list_for_each_entry(tmp, &ops->rules_list, list) {
Eric Dumazet7a2b03c2010-10-26 09:24:55 +0000506 if (rtnl_dereference(tmp->ctarget) == rule) {
Stephen Hemmingera9b3cd72011-08-01 16:19:00 +0000507 RCU_INIT_POINTER(tmp->ctarget, NULL);
Thomas Graf0947c9fe2007-03-26 17:14:15 -0700508 ops->unresolved_rules++;
509 }
510 }
511 }
512
Denis V. Lunev9e3a5482008-01-20 16:46:41 -0800513 notify_rule_change(RTM_DELRULE, rule, ops, nlh,
Eric W. Biederman15e47302012-09-07 20:12:54 +0000514 NETLINK_CB(skb).portid);
Thomas Graf14c0b972006-08-04 03:38:38 -0700515 fib_rule_put(rule);
Thomas Graf73417f62007-03-27 13:56:52 -0700516 flush_route_cache(ops);
Thomas Graf14c0b972006-08-04 03:38:38 -0700517 rules_ops_put(ops);
518 return 0;
519 }
520
521 err = -ENOENT;
522errout:
523 rules_ops_put(ops);
524 return err;
525}
526
Thomas Graf339bf982006-11-10 14:10:15 -0800527static inline size_t fib_rule_nlmsg_size(struct fib_rules_ops *ops,
528 struct fib_rule *rule)
529{
530 size_t payload = NLMSG_ALIGN(sizeof(struct fib_rule_hdr))
Patrick McHardy491deb24b2009-12-03 01:25:54 +0000531 + nla_total_size(IFNAMSIZ) /* FRA_IIFNAME */
Patrick McHardy1b038a52009-12-03 01:25:56 +0000532 + nla_total_size(IFNAMSIZ) /* FRA_OIFNAME */
Thomas Graf339bf982006-11-10 14:10:15 -0800533 + nla_total_size(4) /* FRA_PRIORITY */
534 + nla_total_size(4) /* FRA_TABLE */
Stefan Tomanek73f56982013-08-03 14:14:43 +0200535 + nla_total_size(4) /* FRA_SUPPRESS_PREFIXLEN */
Stefan Tomanek6ef94cf2013-08-02 17:19:56 +0200536 + nla_total_size(4) /* FRA_SUPPRESS_IFGROUP */
Thomas Graf339bf982006-11-10 14:10:15 -0800537 + nla_total_size(4) /* FRA_FWMARK */
538 + nla_total_size(4); /* FRA_FWMASK */
539
540 if (ops->nlmsg_payload)
541 payload += ops->nlmsg_payload(rule);
542
543 return payload;
544}
545
Thomas Graf14c0b972006-08-04 03:38:38 -0700546static int fib_nl_fill_rule(struct sk_buff *skb, struct fib_rule *rule,
547 u32 pid, u32 seq, int type, int flags,
548 struct fib_rules_ops *ops)
549{
550 struct nlmsghdr *nlh;
551 struct fib_rule_hdr *frh;
552
553 nlh = nlmsg_put(skb, pid, seq, type, sizeof(*frh), flags);
554 if (nlh == NULL)
Patrick McHardy26932562007-01-31 23:16:40 -0800555 return -EMSGSIZE;
Thomas Graf14c0b972006-08-04 03:38:38 -0700556
557 frh = nlmsg_data(nlh);
Patrick McHardy28bb1722010-04-13 05:03:16 +0000558 frh->family = ops->family;
Thomas Graf14c0b972006-08-04 03:38:38 -0700559 frh->table = rule->table;
David S. Miller0e3cea72012-04-01 20:47:01 -0400560 if (nla_put_u32(skb, FRA_TABLE, rule->table))
561 goto nla_put_failure;
Stefan Tomanek73f56982013-08-03 14:14:43 +0200562 if (nla_put_u32(skb, FRA_SUPPRESS_PREFIXLEN, rule->suppress_prefixlen))
Stefan Tomanek7764a452013-08-01 02:17:15 +0200563 goto nla_put_failure;
Thomas Graf14c0b972006-08-04 03:38:38 -0700564 frh->res1 = 0;
565 frh->res2 = 0;
566 frh->action = rule->action;
567 frh->flags = rule->flags;
568
Eric Dumazet7a2b03c2010-10-26 09:24:55 +0000569 if (rule->action == FR_ACT_GOTO &&
Eric Dumazet33d480c2011-08-11 19:30:52 +0000570 rcu_access_pointer(rule->ctarget) == NULL)
Thomas Graf0947c9fe2007-03-26 17:14:15 -0700571 frh->flags |= FIB_RULE_UNRESOLVED;
572
Patrick McHardy491deb24b2009-12-03 01:25:54 +0000573 if (rule->iifname[0]) {
David S. Miller0e3cea72012-04-01 20:47:01 -0400574 if (nla_put_string(skb, FRA_IIFNAME, rule->iifname))
575 goto nla_put_failure;
Patrick McHardy491deb24b2009-12-03 01:25:54 +0000576 if (rule->iifindex == -1)
577 frh->flags |= FIB_RULE_IIF_DETACHED;
Thomas Graf2b443682007-03-26 17:37:59 -0700578 }
579
Patrick McHardy1b038a52009-12-03 01:25:56 +0000580 if (rule->oifname[0]) {
David S. Miller0e3cea72012-04-01 20:47:01 -0400581 if (nla_put_string(skb, FRA_OIFNAME, rule->oifname))
582 goto nla_put_failure;
Patrick McHardy1b038a52009-12-03 01:25:56 +0000583 if (rule->oifindex == -1)
584 frh->flags |= FIB_RULE_OIF_DETACHED;
585 }
586
David S. Miller0e3cea72012-04-01 20:47:01 -0400587 if ((rule->pref &&
588 nla_put_u32(skb, FRA_PRIORITY, rule->pref)) ||
589 (rule->mark &&
590 nla_put_u32(skb, FRA_FWMARK, rule->mark)) ||
591 ((rule->mark_mask || rule->mark) &&
592 nla_put_u32(skb, FRA_FWMASK, rule->mark_mask)) ||
593 (rule->target &&
594 nla_put_u32(skb, FRA_GOTO, rule->target)))
595 goto nla_put_failure;
Stefan Tomanek6ef94cf2013-08-02 17:19:56 +0200596
597 if (rule->suppress_ifgroup != -1) {
598 if (nla_put_u32(skb, FRA_SUPPRESS_IFGROUP, rule->suppress_ifgroup))
599 goto nla_put_failure;
600 }
601
Rami Rosen04af8cf2009-05-20 17:26:23 -0700602 if (ops->fill(rule, skb, frh) < 0)
Thomas Graf14c0b972006-08-04 03:38:38 -0700603 goto nla_put_failure;
604
Johannes Berg053c0952015-01-16 22:09:00 +0100605 nlmsg_end(skb, nlh);
606 return 0;
Thomas Graf14c0b972006-08-04 03:38:38 -0700607
608nla_put_failure:
Patrick McHardy26932562007-01-31 23:16:40 -0800609 nlmsg_cancel(skb, nlh);
610 return -EMSGSIZE;
Thomas Graf14c0b972006-08-04 03:38:38 -0700611}
612
Thomas Grafc4546732007-03-25 23:24:24 -0700613static int dump_rules(struct sk_buff *skb, struct netlink_callback *cb,
614 struct fib_rules_ops *ops)
Thomas Graf14c0b972006-08-04 03:38:38 -0700615{
616 int idx = 0;
617 struct fib_rule *rule;
Thomas Graf14c0b972006-08-04 03:38:38 -0700618
Eric Dumazete67f88d2011-04-27 22:56:07 +0000619 rcu_read_lock();
620 list_for_each_entry_rcu(rule, &ops->rules_list, list) {
Thomas Grafc4546732007-03-25 23:24:24 -0700621 if (idx < cb->args[1])
Thomas Graf14c0b972006-08-04 03:38:38 -0700622 goto skip;
623
Eric W. Biederman15e47302012-09-07 20:12:54 +0000624 if (fib_nl_fill_rule(skb, rule, NETLINK_CB(cb->skb).portid,
Thomas Graf14c0b972006-08-04 03:38:38 -0700625 cb->nlh->nlmsg_seq, RTM_NEWRULE,
626 NLM_F_MULTI, ops) < 0)
627 break;
628skip:
629 idx++;
630 }
Eric Dumazet2907c352011-05-25 07:34:04 +0000631 rcu_read_unlock();
Thomas Grafc4546732007-03-25 23:24:24 -0700632 cb->args[1] = idx;
Thomas Graf14c0b972006-08-04 03:38:38 -0700633 rules_ops_put(ops);
634
635 return skb->len;
636}
637
Thomas Grafc4546732007-03-25 23:24:24 -0700638static int fib_nl_dumprule(struct sk_buff *skb, struct netlink_callback *cb)
639{
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +0900640 struct net *net = sock_net(skb->sk);
Thomas Grafc4546732007-03-25 23:24:24 -0700641 struct fib_rules_ops *ops;
642 int idx = 0, family;
643
644 family = rtnl_msg_family(cb->nlh);
645 if (family != AF_UNSPEC) {
646 /* Protocol specific dump request */
Denis V. Lunev5fd30ee2008-01-10 03:20:28 -0800647 ops = lookup_rules_ops(net, family);
Thomas Grafc4546732007-03-25 23:24:24 -0700648 if (ops == NULL)
649 return -EAFNOSUPPORT;
650
651 return dump_rules(skb, cb, ops);
652 }
653
654 rcu_read_lock();
Denis V. Lunev5fd30ee2008-01-10 03:20:28 -0800655 list_for_each_entry_rcu(ops, &net->rules_ops, list) {
Thomas Grafc4546732007-03-25 23:24:24 -0700656 if (idx < cb->args[0] || !try_module_get(ops->owner))
657 goto skip;
658
659 if (dump_rules(skb, cb, ops) < 0)
660 break;
661
662 cb->args[1] = 0;
Eric Dumazet2fb35732010-03-09 20:03:38 +0000663skip:
Thomas Grafc4546732007-03-25 23:24:24 -0700664 idx++;
665 }
666 rcu_read_unlock();
667 cb->args[0] = idx;
668
669 return skb->len;
670}
Thomas Graf14c0b972006-08-04 03:38:38 -0700671
Denis V. Lunev9e3a5482008-01-20 16:46:41 -0800672static void notify_rule_change(int event, struct fib_rule *rule,
Thomas Grafc17084d2006-08-15 00:32:48 -0700673 struct fib_rules_ops *ops, struct nlmsghdr *nlh,
674 u32 pid)
Thomas Graf14c0b972006-08-04 03:38:38 -0700675{
Denis V. Lunev9e3a5482008-01-20 16:46:41 -0800676 struct net *net;
Thomas Grafc17084d2006-08-15 00:32:48 -0700677 struct sk_buff *skb;
678 int err = -ENOBUFS;
Thomas Graf14c0b972006-08-04 03:38:38 -0700679
Denis V. Lunev9e3a5482008-01-20 16:46:41 -0800680 net = ops->fro_net;
Thomas Graf339bf982006-11-10 14:10:15 -0800681 skb = nlmsg_new(fib_rule_nlmsg_size(ops, rule), GFP_KERNEL);
Thomas Graf14c0b972006-08-04 03:38:38 -0700682 if (skb == NULL)
Thomas Grafc17084d2006-08-15 00:32:48 -0700683 goto errout;
684
685 err = fib_nl_fill_rule(skb, rule, pid, nlh->nlmsg_seq, event, 0, ops);
Patrick McHardy26932562007-01-31 23:16:40 -0800686 if (err < 0) {
687 /* -EMSGSIZE implies BUG in fib_rule_nlmsg_size() */
688 WARN_ON(err == -EMSGSIZE);
689 kfree_skb(skb);
690 goto errout;
691 }
Denis V. Lunev9e3a5482008-01-20 16:46:41 -0800692
Pablo Neira Ayuso1ce85fe2009-02-24 23:18:28 -0800693 rtnl_notify(skb, net, pid, ops->nlgroup, nlh, GFP_KERNEL);
694 return;
Thomas Grafc17084d2006-08-15 00:32:48 -0700695errout:
696 if (err < 0)
Denis V. Lunev5fd30ee2008-01-10 03:20:28 -0800697 rtnl_set_sk_err(net, ops->nlgroup, err);
Thomas Graf14c0b972006-08-04 03:38:38 -0700698}
699
700static void attach_rules(struct list_head *rules, struct net_device *dev)
701{
702 struct fib_rule *rule;
703
704 list_for_each_entry(rule, rules, list) {
Patrick McHardy491deb24b2009-12-03 01:25:54 +0000705 if (rule->iifindex == -1 &&
706 strcmp(dev->name, rule->iifname) == 0)
707 rule->iifindex = dev->ifindex;
Patrick McHardy1b038a52009-12-03 01:25:56 +0000708 if (rule->oifindex == -1 &&
709 strcmp(dev->name, rule->oifname) == 0)
710 rule->oifindex = dev->ifindex;
Thomas Graf14c0b972006-08-04 03:38:38 -0700711 }
712}
713
714static void detach_rules(struct list_head *rules, struct net_device *dev)
715{
716 struct fib_rule *rule;
717
Patrick McHardy1b038a52009-12-03 01:25:56 +0000718 list_for_each_entry(rule, rules, list) {
Patrick McHardy491deb24b2009-12-03 01:25:54 +0000719 if (rule->iifindex == dev->ifindex)
720 rule->iifindex = -1;
Patrick McHardy1b038a52009-12-03 01:25:56 +0000721 if (rule->oifindex == dev->ifindex)
722 rule->oifindex = -1;
723 }
Thomas Graf14c0b972006-08-04 03:38:38 -0700724}
725
726
727static int fib_rules_event(struct notifier_block *this, unsigned long event,
Jiri Pirko351638e2013-05-28 01:30:21 +0000728 void *ptr)
Thomas Graf14c0b972006-08-04 03:38:38 -0700729{
Jiri Pirko351638e2013-05-28 01:30:21 +0000730 struct net_device *dev = netdev_notifier_info_to_dev(ptr);
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +0900731 struct net *net = dev_net(dev);
Thomas Graf14c0b972006-08-04 03:38:38 -0700732 struct fib_rules_ops *ops;
733
Eric Dumazet748e2d92012-08-22 21:50:59 +0000734 ASSERT_RTNL();
Thomas Graf14c0b972006-08-04 03:38:38 -0700735
736 switch (event) {
737 case NETDEV_REGISTER:
Denis V. Lunev5fd30ee2008-01-10 03:20:28 -0800738 list_for_each_entry(ops, &net->rules_ops, list)
Denis V. Lunev76c72d42007-09-16 15:44:27 -0700739 attach_rules(&ops->rules_list, dev);
Thomas Graf14c0b972006-08-04 03:38:38 -0700740 break;
741
Maciej Żenczykowski946c0322014-02-07 16:23:48 -0800742 case NETDEV_CHANGENAME:
743 list_for_each_entry(ops, &net->rules_ops, list) {
744 detach_rules(&ops->rules_list, dev);
745 attach_rules(&ops->rules_list, dev);
746 }
747 break;
748
Thomas Graf14c0b972006-08-04 03:38:38 -0700749 case NETDEV_UNREGISTER:
Denis V. Lunev5fd30ee2008-01-10 03:20:28 -0800750 list_for_each_entry(ops, &net->rules_ops, list)
Denis V. Lunev76c72d42007-09-16 15:44:27 -0700751 detach_rules(&ops->rules_list, dev);
Thomas Graf14c0b972006-08-04 03:38:38 -0700752 break;
753 }
754
Thomas Graf14c0b972006-08-04 03:38:38 -0700755 return NOTIFY_DONE;
756}
757
758static struct notifier_block fib_rules_notifier = {
759 .notifier_call = fib_rules_event,
760};
761
Alexey Dobriyan2c8c1e72010-01-17 03:35:32 +0000762static int __net_init fib_rules_net_init(struct net *net)
Denis V. Lunev5fd30ee2008-01-10 03:20:28 -0800763{
764 INIT_LIST_HEAD(&net->rules_ops);
765 spin_lock_init(&net->rules_mod_lock);
766 return 0;
767}
768
769static struct pernet_operations fib_rules_net_ops = {
770 .init = fib_rules_net_init,
771};
772
Thomas Graf14c0b972006-08-04 03:38:38 -0700773static int __init fib_rules_init(void)
774{
Denis V. Lunev5fd30ee2008-01-10 03:20:28 -0800775 int err;
Greg Rosec7ac8672011-06-10 01:27:09 +0000776 rtnl_register(PF_UNSPEC, RTM_NEWRULE, fib_nl_newrule, NULL, NULL);
777 rtnl_register(PF_UNSPEC, RTM_DELRULE, fib_nl_delrule, NULL, NULL);
778 rtnl_register(PF_UNSPEC, RTM_GETRULE, NULL, fib_nl_dumprule, NULL);
Thomas Graf9d9e6a52007-03-25 23:20:05 -0700779
Eric W. Biederman5d6d4802008-11-07 22:52:34 -0800780 err = register_pernet_subsys(&fib_rules_net_ops);
Denis V. Lunev5fd30ee2008-01-10 03:20:28 -0800781 if (err < 0)
782 goto fail;
783
Eric W. Biederman5d6d4802008-11-07 22:52:34 -0800784 err = register_netdevice_notifier(&fib_rules_notifier);
Denis V. Lunev5fd30ee2008-01-10 03:20:28 -0800785 if (err < 0)
786 goto fail_unregister;
Eric W. Biederman5d6d4802008-11-07 22:52:34 -0800787
Denis V. Lunev5fd30ee2008-01-10 03:20:28 -0800788 return 0;
789
790fail_unregister:
Eric W. Biederman5d6d4802008-11-07 22:52:34 -0800791 unregister_pernet_subsys(&fib_rules_net_ops);
Denis V. Lunev5fd30ee2008-01-10 03:20:28 -0800792fail:
793 rtnl_unregister(PF_UNSPEC, RTM_NEWRULE);
794 rtnl_unregister(PF_UNSPEC, RTM_DELRULE);
795 rtnl_unregister(PF_UNSPEC, RTM_GETRULE);
796 return err;
Thomas Graf14c0b972006-08-04 03:38:38 -0700797}
798
799subsys_initcall(fib_rules_init);