blob: fc0b65093417d6d1c2607313a43f6ddc65076240 [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>
Thomas Grafe7030872015-07-21 10:44:01 +020019#include <net/ip_tunnels.h>
Thomas Graf14c0b972006-08-04 03:38:38 -070020
Lorenzo Colitti622ec2c2016-11-04 02:23:42 +090021static const struct fib_kuid_range fib_kuid_range_unset = {
22 KUIDT_INIT(0),
23 KUIDT_INIT(~0),
24};
25
Ido Schimmel3c710062017-03-16 09:08:12 +010026bool fib_rule_matchall(const struct fib_rule *rule)
27{
28 if (rule->iifindex || rule->oifindex || rule->mark || rule->tun_id ||
29 rule->flags)
30 return false;
31 if (rule->suppress_ifgroup != -1 || rule->suppress_prefixlen != -1)
32 return false;
33 if (!uid_eq(rule->uid_range.start, fib_kuid_range_unset.start) ||
34 !uid_eq(rule->uid_range.end, fib_kuid_range_unset.end))
35 return false;
36 return true;
37}
38EXPORT_SYMBOL_GPL(fib_rule_matchall);
39
Denis V. Lunev2994c632007-11-10 22:12:03 -080040int fib_default_rule_add(struct fib_rules_ops *ops,
41 u32 pref, u32 table, u32 flags)
42{
43 struct fib_rule *r;
44
45 r = kzalloc(ops->rule_size, GFP_KERNEL);
46 if (r == NULL)
47 return -ENOMEM;
48
Reshetova, Elena717d1e92017-06-30 13:08:06 +030049 refcount_set(&r->refcnt, 1);
Denis V. Lunev2994c632007-11-10 22:12:03 -080050 r->action = FR_ACT_TO_TBL;
51 r->pref = pref;
52 r->table = table;
53 r->flags = flags;
Eric W. Biedermanefd7ef12015-03-11 23:04:08 -050054 r->fr_net = ops->fro_net;
Lorenzo Colitti622ec2c2016-11-04 02:23:42 +090055 r->uid_range = fib_kuid_range_unset;
Denis V. Lunev2994c632007-11-10 22:12:03 -080056
Stefan Tomanek73f56982013-08-03 14:14:43 +020057 r->suppress_prefixlen = -1;
58 r->suppress_ifgroup = -1;
59
Denis V. Lunev2994c632007-11-10 22:12:03 -080060 /* The lock is not required here, the list in unreacheable
61 * at the moment this function is called */
62 list_add_tail(&r->list, &ops->rules_list);
63 return 0;
64}
65EXPORT_SYMBOL(fib_default_rule_add);
66
Phil Sutterf53de1e2015-09-09 14:20:56 +020067static u32 fib_default_rule_pref(struct fib_rules_ops *ops)
Patrick McHardyd8a566b2010-04-13 05:03:15 +000068{
69 struct list_head *pos;
70 struct fib_rule *rule;
71
72 if (!list_empty(&ops->rules_list)) {
73 pos = ops->rules_list.next;
74 if (pos->next != &ops->rules_list) {
75 rule = list_entry(pos->next, struct fib_rule, list);
76 if (rule->pref)
77 return rule->pref - 1;
78 }
79 }
80
81 return 0;
82}
Patrick McHardyd8a566b2010-04-13 05:03:15 +000083
Denis V. Lunev9e3a5482008-01-20 16:46:41 -080084static void notify_rule_change(int event, struct fib_rule *rule,
Thomas Grafc17084d2006-08-15 00:32:48 -070085 struct fib_rules_ops *ops, struct nlmsghdr *nlh,
86 u32 pid);
Thomas Graf14c0b972006-08-04 03:38:38 -070087
Denis V. Lunev5fd30ee2008-01-10 03:20:28 -080088static struct fib_rules_ops *lookup_rules_ops(struct net *net, int family)
Thomas Graf14c0b972006-08-04 03:38:38 -070089{
90 struct fib_rules_ops *ops;
91
92 rcu_read_lock();
Denis V. Lunev5fd30ee2008-01-10 03:20:28 -080093 list_for_each_entry_rcu(ops, &net->rules_ops, list) {
Thomas Graf14c0b972006-08-04 03:38:38 -070094 if (ops->family == family) {
95 if (!try_module_get(ops->owner))
96 ops = NULL;
97 rcu_read_unlock();
98 return ops;
99 }
100 }
101 rcu_read_unlock();
102
103 return NULL;
104}
105
106static void rules_ops_put(struct fib_rules_ops *ops)
107{
108 if (ops)
109 module_put(ops->owner);
110}
111
Thomas Graf73417f62007-03-27 13:56:52 -0700112static void flush_route_cache(struct fib_rules_ops *ops)
113{
114 if (ops->flush_cache)
Denis V. Lunevae299fc2008-07-05 19:01:28 -0700115 ops->flush_cache(ops);
Thomas Graf73417f62007-03-27 13:56:52 -0700116}
117
Eric W. Biedermane9c51582009-12-03 12:22:55 -0800118static int __fib_rules_register(struct fib_rules_ops *ops)
Thomas Graf14c0b972006-08-04 03:38:38 -0700119{
120 int err = -EEXIST;
121 struct fib_rules_ops *o;
Denis V. Lunev9e3a5482008-01-20 16:46:41 -0800122 struct net *net;
123
124 net = ops->fro_net;
Thomas Graf14c0b972006-08-04 03:38:38 -0700125
126 if (ops->rule_size < sizeof(struct fib_rule))
127 return -EINVAL;
128
129 if (ops->match == NULL || ops->configure == NULL ||
130 ops->compare == NULL || ops->fill == NULL ||
131 ops->action == NULL)
132 return -EINVAL;
133
Denis V. Lunev5fd30ee2008-01-10 03:20:28 -0800134 spin_lock(&net->rules_mod_lock);
135 list_for_each_entry(o, &net->rules_ops, list)
Thomas Graf14c0b972006-08-04 03:38:38 -0700136 if (ops->family == o->family)
137 goto errout;
138
Denis V. Lunev5fd30ee2008-01-10 03:20:28 -0800139 list_add_tail_rcu(&ops->list, &net->rules_ops);
Thomas Graf14c0b972006-08-04 03:38:38 -0700140 err = 0;
141errout:
Denis V. Lunev5fd30ee2008-01-10 03:20:28 -0800142 spin_unlock(&net->rules_mod_lock);
Thomas Graf14c0b972006-08-04 03:38:38 -0700143
144 return err;
145}
146
Eric W. Biedermane9c51582009-12-03 12:22:55 -0800147struct fib_rules_ops *
Patrick McHardy3d0c9c42010-04-26 16:02:04 +0200148fib_rules_register(const struct fib_rules_ops *tmpl, struct net *net)
Eric W. Biedermane9c51582009-12-03 12:22:55 -0800149{
150 struct fib_rules_ops *ops;
151 int err;
152
Eric Dumazet2fb35732010-03-09 20:03:38 +0000153 ops = kmemdup(tmpl, sizeof(*ops), GFP_KERNEL);
Eric W. Biedermane9c51582009-12-03 12:22:55 -0800154 if (ops == NULL)
155 return ERR_PTR(-ENOMEM);
156
157 INIT_LIST_HEAD(&ops->rules_list);
158 ops->fro_net = net;
159
160 err = __fib_rules_register(ops);
161 if (err) {
162 kfree(ops);
163 ops = ERR_PTR(err);
164 }
165
166 return ops;
167}
Thomas Graf14c0b972006-08-04 03:38:38 -0700168EXPORT_SYMBOL_GPL(fib_rules_register);
169
stephen hemminger1df99162010-10-04 20:14:17 +0000170static void fib_rules_cleanup_ops(struct fib_rules_ops *ops)
Thomas Graf14c0b972006-08-04 03:38:38 -0700171{
172 struct fib_rule *rule, *tmp;
173
Denis V. Lunev76c72d42007-09-16 15:44:27 -0700174 list_for_each_entry_safe(rule, tmp, &ops->rules_list, list) {
Thomas Graf14c0b972006-08-04 03:38:38 -0700175 list_del_rcu(&rule->list);
David S. Miller7a9bc9b2012-06-29 01:32:45 -0700176 if (ops->delete)
177 ops->delete(rule);
Thomas Graf14c0b972006-08-04 03:38:38 -0700178 fib_rule_put(rule);
179 }
180}
181
Denis V. Lunev9e3a5482008-01-20 16:46:41 -0800182void fib_rules_unregister(struct fib_rules_ops *ops)
Thomas Graf14c0b972006-08-04 03:38:38 -0700183{
Denis V. Lunev9e3a5482008-01-20 16:46:41 -0800184 struct net *net = ops->fro_net;
Thomas Graf14c0b972006-08-04 03:38:38 -0700185
Denis V. Lunev5fd30ee2008-01-10 03:20:28 -0800186 spin_lock(&net->rules_mod_lock);
Denis V. Lunev72132c1b2008-01-14 22:59:30 -0800187 list_del_rcu(&ops->list);
Denis V. Lunev5fd30ee2008-01-10 03:20:28 -0800188 spin_unlock(&net->rules_mod_lock);
Thomas Graf14c0b972006-08-04 03:38:38 -0700189
WANG Cong419df122015-03-31 11:01:46 -0700190 fib_rules_cleanup_ops(ops);
Eric W. Biedermanefd7ef12015-03-11 23:04:08 -0500191 kfree_rcu(ops, rcu);
Thomas Graf14c0b972006-08-04 03:38:38 -0700192}
Thomas Graf14c0b972006-08-04 03:38:38 -0700193EXPORT_SYMBOL_GPL(fib_rules_unregister);
194
Lorenzo Colitti622ec2c2016-11-04 02:23:42 +0900195static int uid_range_set(struct fib_kuid_range *range)
196{
197 return uid_valid(range->start) && uid_valid(range->end);
198}
199
200static struct fib_kuid_range nla_get_kuid_range(struct nlattr **tb)
201{
202 struct fib_rule_uid_range *in;
203 struct fib_kuid_range out;
204
205 in = (struct fib_rule_uid_range *)nla_data(tb[FRA_UID_RANGE]);
206
207 out.start = make_kuid(current_user_ns(), in->start);
208 out.end = make_kuid(current_user_ns(), in->end);
209
210 return out;
211}
212
213static int nla_put_uid_range(struct sk_buff *skb, struct fib_kuid_range *range)
214{
215 struct fib_rule_uid_range out = {
216 from_kuid_munged(current_user_ns(), range->start),
217 from_kuid_munged(current_user_ns(), range->end)
218 };
219
220 return nla_put(skb, FRA_UID_RANGE, sizeof(out), &out);
221}
222
Thomas Graf3dfbcc42006-11-09 15:23:20 -0800223static int fib_rule_match(struct fib_rule *rule, struct fib_rules_ops *ops,
David Ahern96c63fa2016-06-08 10:55:39 -0700224 struct flowi *fl, int flags,
225 struct fib_lookup_arg *arg)
Thomas Graf3dfbcc42006-11-09 15:23:20 -0800226{
227 int ret = 0;
228
David S. Miller1d28f422011-03-12 00:29:39 -0500229 if (rule->iifindex && (rule->iifindex != fl->flowi_iif))
Thomas Graf3dfbcc42006-11-09 15:23:20 -0800230 goto out;
231
David S. Miller1d28f422011-03-12 00:29:39 -0500232 if (rule->oifindex && (rule->oifindex != fl->flowi_oif))
Patrick McHardy1b038a52009-12-03 01:25:56 +0000233 goto out;
234
David S. Miller1d28f422011-03-12 00:29:39 -0500235 if ((rule->mark ^ fl->flowi_mark) & rule->mark_mask)
Thomas Graf3dfbcc42006-11-09 15:23:20 -0800236 goto out;
237
Thomas Grafe7030872015-07-21 10:44:01 +0200238 if (rule->tun_id && (rule->tun_id != fl->flowi_tun_key.tun_id))
239 goto out;
240
David Ahern96c63fa2016-06-08 10:55:39 -0700241 if (rule->l3mdev && !l3mdev_fib_rule_match(rule->fr_net, fl, arg))
242 goto out;
243
Lorenzo Colitti622ec2c2016-11-04 02:23:42 +0900244 if (uid_lt(fl->flowi_uid, rule->uid_range.start) ||
245 uid_gt(fl->flowi_uid, rule->uid_range.end))
246 goto out;
247
Thomas Graf3dfbcc42006-11-09 15:23:20 -0800248 ret = ops->match(rule, fl, flags);
249out:
250 return (rule->flags & FIB_RULE_INVERT) ? !ret : ret;
251}
252
Thomas Graf14c0b972006-08-04 03:38:38 -0700253int fib_rules_lookup(struct fib_rules_ops *ops, struct flowi *fl,
254 int flags, struct fib_lookup_arg *arg)
255{
256 struct fib_rule *rule;
257 int err;
258
259 rcu_read_lock();
260
Denis V. Lunev76c72d42007-09-16 15:44:27 -0700261 list_for_each_entry_rcu(rule, &ops->rules_list, list) {
Thomas Graf0947c9fe2007-03-26 17:14:15 -0700262jumped:
David Ahern96c63fa2016-06-08 10:55:39 -0700263 if (!fib_rule_match(rule, ops, fl, flags, arg))
Thomas Graf14c0b972006-08-04 03:38:38 -0700264 continue;
265
Thomas Graf0947c9fe2007-03-26 17:14:15 -0700266 if (rule->action == FR_ACT_GOTO) {
267 struct fib_rule *target;
268
269 target = rcu_dereference(rule->ctarget);
270 if (target == NULL) {
271 continue;
272 } else {
273 rule = target;
274 goto jumped;
275 }
Thomas Graffa0b2d12007-03-26 17:38:53 -0700276 } else if (rule->action == FR_ACT_NOP)
277 continue;
278 else
Thomas Graf0947c9fe2007-03-26 17:14:15 -0700279 err = ops->action(rule, fl, flags, arg);
280
Stefan Tomanek7764a452013-08-01 02:17:15 +0200281 if (!err && ops->suppress && ops->suppress(rule, arg))
282 continue;
283
Thomas Graf14c0b972006-08-04 03:38:38 -0700284 if (err != -EAGAIN) {
Eric Dumazetebc0ffa2010-10-05 10:41:36 +0000285 if ((arg->flags & FIB_LOOKUP_NOREF) ||
Reshetova, Elena717d1e92017-06-30 13:08:06 +0300286 likely(refcount_inc_not_zero(&rule->refcnt))) {
Eric Dumazet7fa7cb72010-09-27 04:18:27 +0000287 arg->rule = rule;
288 goto out;
289 }
290 break;
Thomas Graf14c0b972006-08-04 03:38:38 -0700291 }
292 }
293
Steven Whitehouse83886b62007-03-30 13:34:27 -0700294 err = -ESRCH;
Thomas Graf14c0b972006-08-04 03:38:38 -0700295out:
296 rcu_read_unlock();
297
298 return err;
299}
Thomas Graf14c0b972006-08-04 03:38:38 -0700300EXPORT_SYMBOL_GPL(fib_rules_lookup);
301
Ido Schimmel1b2a4442017-08-03 13:28:14 +0200302static int call_fib_rule_notifier(struct notifier_block *nb, struct net *net,
303 enum fib_event_type event_type,
304 struct fib_rule *rule, int family)
305{
306 struct fib_rule_notifier_info info = {
307 .info.family = family,
308 .rule = rule,
309 };
310
311 return call_fib_notifier(nb, net, event_type, &info.info);
312}
313
314static int call_fib_rule_notifiers(struct net *net,
315 enum fib_event_type event_type,
316 struct fib_rule *rule,
317 struct fib_rules_ops *ops)
318{
319 struct fib_rule_notifier_info info = {
320 .info.family = ops->family,
321 .rule = rule,
322 };
323
324 ops->fib_rules_seq++;
325 return call_fib_notifiers(net, event_type, &info.info);
326}
327
328/* Called with rcu_read_lock() */
329int fib_rules_dump(struct net *net, struct notifier_block *nb, int family)
330{
331 struct fib_rules_ops *ops;
332 struct fib_rule *rule;
333
334 ops = lookup_rules_ops(net, family);
335 if (!ops)
336 return -EAFNOSUPPORT;
337 list_for_each_entry_rcu(rule, &ops->rules_list, list)
338 call_fib_rule_notifier(nb, net, FIB_EVENT_RULE_ADD, rule,
339 family);
340 rules_ops_put(ops);
341
342 return 0;
343}
344EXPORT_SYMBOL_GPL(fib_rules_dump);
345
346unsigned int fib_rules_seq_read(struct net *net, int family)
347{
348 unsigned int fib_rules_seq;
349 struct fib_rules_ops *ops;
350
351 ASSERT_RTNL();
352
353 ops = lookup_rules_ops(net, family);
354 if (!ops)
355 return 0;
356 fib_rules_seq = ops->fib_rules_seq;
357 rules_ops_put(ops);
358
359 return fib_rules_seq;
360}
361EXPORT_SYMBOL_GPL(fib_rules_seq_read);
362
Thomas Grafe1701c62007-03-24 12:46:02 -0700363static int validate_rulemsg(struct fib_rule_hdr *frh, struct nlattr **tb,
364 struct fib_rules_ops *ops)
365{
366 int err = -EINVAL;
367
368 if (frh->src_len)
369 if (tb[FRA_SRC] == NULL ||
370 frh->src_len > (ops->addr_size * 8) ||
371 nla_len(tb[FRA_SRC]) != ops->addr_size)
372 goto errout;
373
374 if (frh->dst_len)
375 if (tb[FRA_DST] == NULL ||
376 frh->dst_len > (ops->addr_size * 8) ||
377 nla_len(tb[FRA_DST]) != ops->addr_size)
378 goto errout;
379
380 err = 0;
381errout:
382 return err;
383}
384
Mateusz Bajorski153380e2016-06-29 09:22:10 +0200385static int rule_exists(struct fib_rules_ops *ops, struct fib_rule_hdr *frh,
386 struct nlattr **tb, struct fib_rule *rule)
387{
388 struct fib_rule *r;
389
390 list_for_each_entry(r, &ops->rules_list, list) {
391 if (r->action != rule->action)
392 continue;
393
394 if (r->table != rule->table)
395 continue;
396
397 if (r->pref != rule->pref)
398 continue;
399
400 if (memcmp(r->iifname, rule->iifname, IFNAMSIZ))
401 continue;
402
403 if (memcmp(r->oifname, rule->oifname, IFNAMSIZ))
404 continue;
405
406 if (r->mark != rule->mark)
407 continue;
408
409 if (r->mark_mask != rule->mark_mask)
410 continue;
411
412 if (r->tun_id != rule->tun_id)
413 continue;
414
415 if (r->fr_net != rule->fr_net)
416 continue;
417
418 if (r->l3mdev != rule->l3mdev)
419 continue;
420
Lorenzo Colitti35b80732016-11-07 00:16:25 +0900421 if (!uid_eq(r->uid_range.start, rule->uid_range.start) ||
422 !uid_eq(r->uid_range.end, rule->uid_range.end))
423 continue;
424
Mateusz Bajorski153380e2016-06-29 09:22:10 +0200425 if (!ops->compare(r, frh, tb))
426 continue;
427 return 1;
428 }
429 return 0;
430}
431
David Ahernc21ef3e2017-04-16 09:48:24 -0700432int fib_nl_newrule(struct sk_buff *skb, struct nlmsghdr *nlh,
433 struct netlink_ext_ack *extack)
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;
438 struct fib_rule *rule, *r, *last = NULL;
439 struct nlattr *tb[FRA_MAX+1];
Thomas Graf0947c9fe2007-03-26 17:14:15 -0700440 int err = -EINVAL, unresolved = 0;
Thomas Graf14c0b972006-08-04 03:38:38 -0700441
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
David Ahernc21ef3e2017-04-16 09:48:24 -0700451 err = nlmsg_parse(nlh, sizeof(*frh), tb, FRA_MAX, ops->policy, extack);
Thomas Graf14c0b972006-08-04 03:38:38 -0700452 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
Thomas Graf14c0b972006-08-04 03:38:38 -0700459 rule = kzalloc(ops->rule_size, GFP_KERNEL);
460 if (rule == NULL) {
461 err = -ENOMEM;
462 goto errout;
463 }
David Ahern5d89fb32017-07-13 13:36:40 -0700464 refcount_set(&rule->refcnt, 1);
Eric W. Biedermanefd7ef12015-03-11 23:04:08 -0500465 rule->fr_net = net;
Thomas Graf14c0b972006-08-04 03:38:38 -0700466
Phil Sutterf53de1e2015-09-09 14:20:56 +0200467 rule->pref = tb[FRA_PRIORITY] ? nla_get_u32(tb[FRA_PRIORITY])
468 : fib_default_rule_pref(ops);
Thomas Graf14c0b972006-08-04 03:38:38 -0700469
Patrick McHardy491deb22009-12-03 01:25:54 +0000470 if (tb[FRA_IIFNAME]) {
Thomas Graf14c0b972006-08-04 03:38:38 -0700471 struct net_device *dev;
472
Patrick McHardy491deb22009-12-03 01:25:54 +0000473 rule->iifindex = -1;
474 nla_strlcpy(rule->iifname, tb[FRA_IIFNAME], IFNAMSIZ);
475 dev = __dev_get_by_name(net, rule->iifname);
Thomas Graf14c0b972006-08-04 03:38:38 -0700476 if (dev)
Patrick McHardy491deb22009-12-03 01:25:54 +0000477 rule->iifindex = dev->ifindex;
Thomas Graf14c0b972006-08-04 03:38:38 -0700478 }
479
Patrick McHardy1b038a52009-12-03 01:25:56 +0000480 if (tb[FRA_OIFNAME]) {
481 struct net_device *dev;
482
483 rule->oifindex = -1;
484 nla_strlcpy(rule->oifname, tb[FRA_OIFNAME], IFNAMSIZ);
485 dev = __dev_get_by_name(net, rule->oifname);
486 if (dev)
487 rule->oifindex = dev->ifindex;
488 }
489
Thomas Grafb8964ed2006-11-09 15:22:18 -0800490 if (tb[FRA_FWMARK]) {
491 rule->mark = nla_get_u32(tb[FRA_FWMARK]);
492 if (rule->mark)
493 /* compatibility: if the mark value is non-zero all bits
494 * are compared unless a mask is explicitly specified.
495 */
496 rule->mark_mask = 0xFFFFFFFF;
497 }
498
499 if (tb[FRA_FWMASK])
500 rule->mark_mask = nla_get_u32(tb[FRA_FWMASK]);
501
Thomas Grafe7030872015-07-21 10:44:01 +0200502 if (tb[FRA_TUN_ID])
503 rule->tun_id = nla_get_be64(tb[FRA_TUN_ID]);
504
Wei Yongjunadeb45c2017-04-26 14:03:50 +0000505 err = -EINVAL;
David Ahern96c63fa2016-06-08 10:55:39 -0700506 if (tb[FRA_L3MDEV]) {
507#ifdef CONFIG_NET_L3_MASTER_DEV
508 rule->l3mdev = nla_get_u8(tb[FRA_L3MDEV]);
509 if (rule->l3mdev != 1)
510#endif
511 goto errout_free;
512 }
513
Thomas Graf14c0b972006-08-04 03:38:38 -0700514 rule->action = frh->action;
515 rule->flags = frh->flags;
Patrick McHardy9e762a42006-08-10 23:09:48 -0700516 rule->table = frh_get_table(frh, tb);
Stefan Tomanek73f56982013-08-03 14:14:43 +0200517 if (tb[FRA_SUPPRESS_PREFIXLEN])
518 rule->suppress_prefixlen = nla_get_u32(tb[FRA_SUPPRESS_PREFIXLEN]);
519 else
520 rule->suppress_prefixlen = -1;
Thomas Graf14c0b972006-08-04 03:38:38 -0700521
Stefan Tomanek6ef94cf2013-08-02 17:19:56 +0200522 if (tb[FRA_SUPPRESS_IFGROUP])
523 rule->suppress_ifgroup = nla_get_u32(tb[FRA_SUPPRESS_IFGROUP]);
Stefan Tomanek73f56982013-08-03 14:14:43 +0200524 else
525 rule->suppress_ifgroup = -1;
Stefan Tomanek6ef94cf2013-08-02 17:19:56 +0200526
Thomas Graf0947c9fe2007-03-26 17:14:15 -0700527 if (tb[FRA_GOTO]) {
528 if (rule->action != FR_ACT_GOTO)
529 goto errout_free;
530
531 rule->target = nla_get_u32(tb[FRA_GOTO]);
532 /* Backward jumps are prohibited to avoid endless loops */
533 if (rule->target <= rule->pref)
534 goto errout_free;
535
Denis V. Lunev76c72d42007-09-16 15:44:27 -0700536 list_for_each_entry(r, &ops->rules_list, list) {
Thomas Graf0947c9fe2007-03-26 17:14:15 -0700537 if (r->pref == rule->target) {
Eric Dumazet7a2b03c2010-10-26 09:24:55 +0000538 RCU_INIT_POINTER(rule->ctarget, r);
Thomas Graf0947c9fe2007-03-26 17:14:15 -0700539 break;
540 }
541 }
542
Eric Dumazet7a2b03c2010-10-26 09:24:55 +0000543 if (rcu_dereference_protected(rule->ctarget, 1) == NULL)
Thomas Graf0947c9fe2007-03-26 17:14:15 -0700544 unresolved = 1;
545 } else if (rule->action == FR_ACT_GOTO)
546 goto errout_free;
547
David Ahern96c63fa2016-06-08 10:55:39 -0700548 if (rule->l3mdev && rule->table)
549 goto errout_free;
550
Lorenzo Colitti622ec2c2016-11-04 02:23:42 +0900551 if (tb[FRA_UID_RANGE]) {
552 if (current_user_ns() != net->user_ns) {
553 err = -EPERM;
554 goto errout_free;
555 }
556
557 rule->uid_range = nla_get_kuid_range(tb);
558
559 if (!uid_range_set(&rule->uid_range) ||
560 !uid_lte(rule->uid_range.start, rule->uid_range.end))
561 goto errout_free;
562 } else {
563 rule->uid_range = fib_kuid_range_unset;
564 }
565
Mateusz Bajorski153380e2016-06-29 09:22:10 +0200566 if ((nlh->nlmsg_flags & NLM_F_EXCL) &&
567 rule_exists(ops, frh, tb, rule)) {
568 err = -EEXIST;
569 goto errout_free;
570 }
571
Rami Rosen8b3521e2009-05-11 05:52:49 +0000572 err = ops->configure(rule, skb, frh, tb);
Thomas Graf14c0b972006-08-04 03:38:38 -0700573 if (err < 0)
574 goto errout_free;
575
Denis V. Lunev76c72d42007-09-16 15:44:27 -0700576 list_for_each_entry(r, &ops->rules_list, list) {
Thomas Graf14c0b972006-08-04 03:38:38 -0700577 if (r->pref > rule->pref)
578 break;
579 last = r;
580 }
581
Eric Dumazetebb9fed2010-10-23 09:44:25 +0000582 if (last)
583 list_add_rcu(&rule->list, &last->list);
584 else
585 list_add_rcu(&rule->list, &ops->rules_list);
586
Thomas Graf0947c9fe2007-03-26 17:14:15 -0700587 if (ops->unresolved_rules) {
588 /*
589 * There are unresolved goto rules in the list, check if
590 * any of them are pointing to this new rule.
591 */
Denis V. Lunev76c72d42007-09-16 15:44:27 -0700592 list_for_each_entry(r, &ops->rules_list, list) {
Thomas Graf0947c9fe2007-03-26 17:14:15 -0700593 if (r->action == FR_ACT_GOTO &&
Gao feng561dac22011-09-11 15:36:05 +0000594 r->target == rule->pref &&
595 rtnl_dereference(r->ctarget) == NULL) {
Thomas Graf0947c9fe2007-03-26 17:14:15 -0700596 rcu_assign_pointer(r->ctarget, rule);
597 if (--ops->unresolved_rules == 0)
598 break;
599 }
600 }
601 }
602
603 if (rule->action == FR_ACT_GOTO)
604 ops->nr_goto_rules++;
605
606 if (unresolved)
607 ops->unresolved_rules++;
608
Thomas Grafe7030872015-07-21 10:44:01 +0200609 if (rule->tun_id)
610 ip_tunnel_need_metadata();
611
Ido Schimmel1b2a4442017-08-03 13:28:14 +0200612 call_fib_rule_notifiers(net, FIB_EVENT_RULE_ADD, rule, ops);
Eric W. Biederman15e47302012-09-07 20:12:54 +0000613 notify_rule_change(RTM_NEWRULE, rule, ops, nlh, NETLINK_CB(skb).portid);
Thomas Graf73417f62007-03-27 13:56:52 -0700614 flush_route_cache(ops);
Thomas Graf14c0b972006-08-04 03:38:38 -0700615 rules_ops_put(ops);
616 return 0;
617
618errout_free:
619 kfree(rule);
620errout:
621 rules_ops_put(ops);
622 return err;
623}
David Ahern96c63fa2016-06-08 10:55:39 -0700624EXPORT_SYMBOL_GPL(fib_nl_newrule);
Thomas Graf14c0b972006-08-04 03:38:38 -0700625
David Ahernc21ef3e2017-04-16 09:48:24 -0700626int fib_nl_delrule(struct sk_buff *skb, struct nlmsghdr *nlh,
627 struct netlink_ext_ack *extack)
Thomas Graf14c0b972006-08-04 03:38:38 -0700628{
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +0900629 struct net *net = sock_net(skb->sk);
Thomas Graf14c0b972006-08-04 03:38:38 -0700630 struct fib_rule_hdr *frh = nlmsg_data(nlh);
631 struct fib_rules_ops *ops = NULL;
Serhey Popovychbdaf32c2017-06-16 15:44:47 +0300632 struct fib_rule *rule, *r;
Thomas Graf14c0b972006-08-04 03:38:38 -0700633 struct nlattr *tb[FRA_MAX+1];
Lorenzo Colitti622ec2c2016-11-04 02:23:42 +0900634 struct fib_kuid_range range;
Thomas Graf14c0b972006-08-04 03:38:38 -0700635 int err = -EINVAL;
636
637 if (nlh->nlmsg_len < nlmsg_msg_size(sizeof(*frh)))
638 goto errout;
639
Denis V. Lunev5fd30ee2008-01-10 03:20:28 -0800640 ops = lookup_rules_ops(net, frh->family);
Thomas Graf14c0b972006-08-04 03:38:38 -0700641 if (ops == NULL) {
Patrick McHardy2fe195c2008-07-01 19:59:37 -0700642 err = -EAFNOSUPPORT;
Thomas Graf14c0b972006-08-04 03:38:38 -0700643 goto errout;
644 }
645
David Ahernc21ef3e2017-04-16 09:48:24 -0700646 err = nlmsg_parse(nlh, sizeof(*frh), tb, FRA_MAX, ops->policy, extack);
Thomas Graf14c0b972006-08-04 03:38:38 -0700647 if (err < 0)
648 goto errout;
649
Thomas Grafe1701c62007-03-24 12:46:02 -0700650 err = validate_rulemsg(frh, tb, ops);
651 if (err < 0)
652 goto errout;
653
Lorenzo Colitti622ec2c2016-11-04 02:23:42 +0900654 if (tb[FRA_UID_RANGE]) {
655 range = nla_get_kuid_range(tb);
Wei Yongjunadeb45c2017-04-26 14:03:50 +0000656 if (!uid_range_set(&range)) {
657 err = -EINVAL;
Lorenzo Colitti622ec2c2016-11-04 02:23:42 +0900658 goto errout;
Wei Yongjunadeb45c2017-04-26 14:03:50 +0000659 }
Lorenzo Colitti622ec2c2016-11-04 02:23:42 +0900660 } else {
661 range = fib_kuid_range_unset;
662 }
663
Denis V. Lunev76c72d42007-09-16 15:44:27 -0700664 list_for_each_entry(rule, &ops->rules_list, list) {
Thomas Graf14c0b972006-08-04 03:38:38 -0700665 if (frh->action && (frh->action != rule->action))
666 continue;
667
Andreas Henriksson13eb2ab2013-11-07 18:26:38 +0100668 if (frh_get_table(frh, tb) &&
669 (frh_get_table(frh, tb) != rule->table))
Thomas Graf14c0b972006-08-04 03:38:38 -0700670 continue;
671
672 if (tb[FRA_PRIORITY] &&
673 (rule->pref != nla_get_u32(tb[FRA_PRIORITY])))
674 continue;
675
Patrick McHardy491deb22009-12-03 01:25:54 +0000676 if (tb[FRA_IIFNAME] &&
677 nla_strcmp(tb[FRA_IIFNAME], rule->iifname))
Thomas Graf14c0b972006-08-04 03:38:38 -0700678 continue;
679
Patrick McHardy1b038a52009-12-03 01:25:56 +0000680 if (tb[FRA_OIFNAME] &&
681 nla_strcmp(tb[FRA_OIFNAME], rule->oifname))
682 continue;
683
Thomas Grafb8964ed2006-11-09 15:22:18 -0800684 if (tb[FRA_FWMARK] &&
685 (rule->mark != nla_get_u32(tb[FRA_FWMARK])))
686 continue;
687
688 if (tb[FRA_FWMASK] &&
689 (rule->mark_mask != nla_get_u32(tb[FRA_FWMASK])))
690 continue;
691
Thomas Grafe7030872015-07-21 10:44:01 +0200692 if (tb[FRA_TUN_ID] &&
693 (rule->tun_id != nla_get_be64(tb[FRA_TUN_ID])))
694 continue;
695
David Ahern96c63fa2016-06-08 10:55:39 -0700696 if (tb[FRA_L3MDEV] &&
697 (rule->l3mdev != nla_get_u8(tb[FRA_L3MDEV])))
698 continue;
699
Lorenzo Colitti622ec2c2016-11-04 02:23:42 +0900700 if (uid_range_set(&range) &&
701 (!uid_eq(rule->uid_range.start, range.start) ||
702 !uid_eq(rule->uid_range.end, range.end)))
703 continue;
704
Thomas Graf14c0b972006-08-04 03:38:38 -0700705 if (!ops->compare(rule, frh, tb))
706 continue;
707
708 if (rule->flags & FIB_RULE_PERMANENT) {
709 err = -EPERM;
710 goto errout;
711 }
712
Alexander Duyck0ddcf432015-03-06 13:47:00 -0800713 if (ops->delete) {
714 err = ops->delete(rule);
715 if (err)
716 goto errout;
717 }
718
Thomas Grafe7030872015-07-21 10:44:01 +0200719 if (rule->tun_id)
720 ip_tunnel_unneed_metadata();
721
Thomas Graf14c0b972006-08-04 03:38:38 -0700722 list_del_rcu(&rule->list);
Thomas Graf0947c9fe2007-03-26 17:14:15 -0700723
Yan, Zhengafaef732011-10-17 15:20:28 +0000724 if (rule->action == FR_ACT_GOTO) {
Thomas Graf0947c9fe2007-03-26 17:14:15 -0700725 ops->nr_goto_rules--;
Yan, Zhengafaef732011-10-17 15:20:28 +0000726 if (rtnl_dereference(rule->ctarget) == NULL)
727 ops->unresolved_rules--;
728 }
Thomas Graf0947c9fe2007-03-26 17:14:15 -0700729
730 /*
731 * Check if this rule is a target to any of them. If so,
Serhey Popovychbdaf32c2017-06-16 15:44:47 +0300732 * adjust to the next one with the same preference or
Thomas Graf0947c9fe2007-03-26 17:14:15 -0700733 * disable them. As this operation is eventually very
Serhey Popovychbdaf32c2017-06-16 15:44:47 +0300734 * expensive, it is only performed if goto rules, except
735 * current if it is goto rule, have actually been added.
Thomas Graf0947c9fe2007-03-26 17:14:15 -0700736 */
737 if (ops->nr_goto_rules > 0) {
Serhey Popovychbdaf32c2017-06-16 15:44:47 +0300738 struct fib_rule *n;
739
740 n = list_next_entry(rule, list);
741 if (&n->list == &ops->rules_list || n->pref != rule->pref)
742 n = NULL;
743 list_for_each_entry(r, &ops->rules_list, list) {
744 if (rtnl_dereference(r->ctarget) != rule)
745 continue;
746 rcu_assign_pointer(r->ctarget, n);
747 if (!n)
Thomas Graf0947c9fe2007-03-26 17:14:15 -0700748 ops->unresolved_rules++;
Thomas Graf0947c9fe2007-03-26 17:14:15 -0700749 }
750 }
751
Ido Schimmel1b2a4442017-08-03 13:28:14 +0200752 call_fib_rule_notifiers(net, FIB_EVENT_RULE_DEL, rule, ops);
Denis V. Lunev9e3a5482008-01-20 16:46:41 -0800753 notify_rule_change(RTM_DELRULE, rule, ops, nlh,
Eric W. Biederman15e47302012-09-07 20:12:54 +0000754 NETLINK_CB(skb).portid);
Thomas Graf14c0b972006-08-04 03:38:38 -0700755 fib_rule_put(rule);
Thomas Graf73417f62007-03-27 13:56:52 -0700756 flush_route_cache(ops);
Thomas Graf14c0b972006-08-04 03:38:38 -0700757 rules_ops_put(ops);
758 return 0;
759 }
760
761 err = -ENOENT;
762errout:
763 rules_ops_put(ops);
764 return err;
765}
David Ahern96c63fa2016-06-08 10:55:39 -0700766EXPORT_SYMBOL_GPL(fib_nl_delrule);
Thomas Graf14c0b972006-08-04 03:38:38 -0700767
Thomas Graf339bf982006-11-10 14:10:15 -0800768static inline size_t fib_rule_nlmsg_size(struct fib_rules_ops *ops,
769 struct fib_rule *rule)
770{
771 size_t payload = NLMSG_ALIGN(sizeof(struct fib_rule_hdr))
Patrick McHardy491deb22009-12-03 01:25:54 +0000772 + nla_total_size(IFNAMSIZ) /* FRA_IIFNAME */
Patrick McHardy1b038a52009-12-03 01:25:56 +0000773 + nla_total_size(IFNAMSIZ) /* FRA_OIFNAME */
Thomas Graf339bf982006-11-10 14:10:15 -0800774 + nla_total_size(4) /* FRA_PRIORITY */
775 + nla_total_size(4) /* FRA_TABLE */
Stefan Tomanek73f56982013-08-03 14:14:43 +0200776 + nla_total_size(4) /* FRA_SUPPRESS_PREFIXLEN */
Stefan Tomanek6ef94cf2013-08-02 17:19:56 +0200777 + nla_total_size(4) /* FRA_SUPPRESS_IFGROUP */
Thomas Graf339bf982006-11-10 14:10:15 -0800778 + nla_total_size(4) /* FRA_FWMARK */
Thomas Grafe7030872015-07-21 10:44:01 +0200779 + nla_total_size(4) /* FRA_FWMASK */
Lorenzo Colitti622ec2c2016-11-04 02:23:42 +0900780 + nla_total_size_64bit(8) /* FRA_TUN_ID */
781 + nla_total_size(sizeof(struct fib_kuid_range));
Thomas Graf339bf982006-11-10 14:10:15 -0800782
783 if (ops->nlmsg_payload)
784 payload += ops->nlmsg_payload(rule);
785
786 return payload;
787}
788
Thomas Graf14c0b972006-08-04 03:38:38 -0700789static int fib_nl_fill_rule(struct sk_buff *skb, struct fib_rule *rule,
790 u32 pid, u32 seq, int type, int flags,
791 struct fib_rules_ops *ops)
792{
793 struct nlmsghdr *nlh;
794 struct fib_rule_hdr *frh;
795
796 nlh = nlmsg_put(skb, pid, seq, type, sizeof(*frh), flags);
797 if (nlh == NULL)
Patrick McHardy26932562007-01-31 23:16:40 -0800798 return -EMSGSIZE;
Thomas Graf14c0b972006-08-04 03:38:38 -0700799
800 frh = nlmsg_data(nlh);
Patrick McHardy28bb1722010-04-13 05:03:16 +0000801 frh->family = ops->family;
Thomas Graf14c0b972006-08-04 03:38:38 -0700802 frh->table = rule->table;
David S. Miller0e3cea72012-04-01 20:47:01 -0400803 if (nla_put_u32(skb, FRA_TABLE, rule->table))
804 goto nla_put_failure;
Stefan Tomanek73f56982013-08-03 14:14:43 +0200805 if (nla_put_u32(skb, FRA_SUPPRESS_PREFIXLEN, rule->suppress_prefixlen))
Stefan Tomanek7764a452013-08-01 02:17:15 +0200806 goto nla_put_failure;
Thomas Graf14c0b972006-08-04 03:38:38 -0700807 frh->res1 = 0;
808 frh->res2 = 0;
809 frh->action = rule->action;
810 frh->flags = rule->flags;
811
Eric Dumazet7a2b03c2010-10-26 09:24:55 +0000812 if (rule->action == FR_ACT_GOTO &&
Eric Dumazet33d480c2011-08-11 19:30:52 +0000813 rcu_access_pointer(rule->ctarget) == NULL)
Thomas Graf0947c9fe2007-03-26 17:14:15 -0700814 frh->flags |= FIB_RULE_UNRESOLVED;
815
Patrick McHardy491deb22009-12-03 01:25:54 +0000816 if (rule->iifname[0]) {
David S. Miller0e3cea72012-04-01 20:47:01 -0400817 if (nla_put_string(skb, FRA_IIFNAME, rule->iifname))
818 goto nla_put_failure;
Patrick McHardy491deb22009-12-03 01:25:54 +0000819 if (rule->iifindex == -1)
820 frh->flags |= FIB_RULE_IIF_DETACHED;
Thomas Graf2b443682007-03-26 17:37:59 -0700821 }
822
Patrick McHardy1b038a52009-12-03 01:25:56 +0000823 if (rule->oifname[0]) {
David S. Miller0e3cea72012-04-01 20:47:01 -0400824 if (nla_put_string(skb, FRA_OIFNAME, rule->oifname))
825 goto nla_put_failure;
Patrick McHardy1b038a52009-12-03 01:25:56 +0000826 if (rule->oifindex == -1)
827 frh->flags |= FIB_RULE_OIF_DETACHED;
828 }
829
David S. Miller0e3cea72012-04-01 20:47:01 -0400830 if ((rule->pref &&
831 nla_put_u32(skb, FRA_PRIORITY, rule->pref)) ||
832 (rule->mark &&
833 nla_put_u32(skb, FRA_FWMARK, rule->mark)) ||
834 ((rule->mark_mask || rule->mark) &&
835 nla_put_u32(skb, FRA_FWMASK, rule->mark_mask)) ||
836 (rule->target &&
Thomas Grafe7030872015-07-21 10:44:01 +0200837 nla_put_u32(skb, FRA_GOTO, rule->target)) ||
838 (rule->tun_id &&
David Ahern96c63fa2016-06-08 10:55:39 -0700839 nla_put_be64(skb, FRA_TUN_ID, rule->tun_id, FRA_PAD)) ||
840 (rule->l3mdev &&
Lorenzo Colitti622ec2c2016-11-04 02:23:42 +0900841 nla_put_u8(skb, FRA_L3MDEV, rule->l3mdev)) ||
842 (uid_range_set(&rule->uid_range) &&
843 nla_put_uid_range(skb, &rule->uid_range)))
David S. Miller0e3cea72012-04-01 20:47:01 -0400844 goto nla_put_failure;
Stefan Tomanek6ef94cf2013-08-02 17:19:56 +0200845
846 if (rule->suppress_ifgroup != -1) {
847 if (nla_put_u32(skb, FRA_SUPPRESS_IFGROUP, rule->suppress_ifgroup))
848 goto nla_put_failure;
849 }
850
Rami Rosen04af8cf2009-05-20 17:26:23 -0700851 if (ops->fill(rule, skb, frh) < 0)
Thomas Graf14c0b972006-08-04 03:38:38 -0700852 goto nla_put_failure;
853
Johannes Berg053c0952015-01-16 22:09:00 +0100854 nlmsg_end(skb, nlh);
855 return 0;
Thomas Graf14c0b972006-08-04 03:38:38 -0700856
857nla_put_failure:
Patrick McHardy26932562007-01-31 23:16:40 -0800858 nlmsg_cancel(skb, nlh);
859 return -EMSGSIZE;
Thomas Graf14c0b972006-08-04 03:38:38 -0700860}
861
Thomas Grafc4546732007-03-25 23:24:24 -0700862static int dump_rules(struct sk_buff *skb, struct netlink_callback *cb,
863 struct fib_rules_ops *ops)
Thomas Graf14c0b972006-08-04 03:38:38 -0700864{
865 int idx = 0;
866 struct fib_rule *rule;
Wilson Kok41fc0142015-09-22 21:40:22 -0700867 int err = 0;
Thomas Graf14c0b972006-08-04 03:38:38 -0700868
Eric Dumazete67f88d2011-04-27 22:56:07 +0000869 rcu_read_lock();
870 list_for_each_entry_rcu(rule, &ops->rules_list, list) {
Thomas Grafc4546732007-03-25 23:24:24 -0700871 if (idx < cb->args[1])
Thomas Graf14c0b972006-08-04 03:38:38 -0700872 goto skip;
873
Wilson Kok41fc0142015-09-22 21:40:22 -0700874 err = fib_nl_fill_rule(skb, rule, NETLINK_CB(cb->skb).portid,
875 cb->nlh->nlmsg_seq, RTM_NEWRULE,
876 NLM_F_MULTI, ops);
877 if (err)
Thomas Graf14c0b972006-08-04 03:38:38 -0700878 break;
879skip:
880 idx++;
881 }
Eric Dumazet2907c352011-05-25 07:34:04 +0000882 rcu_read_unlock();
Thomas Grafc4546732007-03-25 23:24:24 -0700883 cb->args[1] = idx;
Thomas Graf14c0b972006-08-04 03:38:38 -0700884 rules_ops_put(ops);
885
Wilson Kok41fc0142015-09-22 21:40:22 -0700886 return err;
Thomas Graf14c0b972006-08-04 03:38:38 -0700887}
888
Thomas Grafc4546732007-03-25 23:24:24 -0700889static int fib_nl_dumprule(struct sk_buff *skb, struct netlink_callback *cb)
890{
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +0900891 struct net *net = sock_net(skb->sk);
Thomas Grafc4546732007-03-25 23:24:24 -0700892 struct fib_rules_ops *ops;
893 int idx = 0, family;
894
895 family = rtnl_msg_family(cb->nlh);
896 if (family != AF_UNSPEC) {
897 /* Protocol specific dump request */
Denis V. Lunev5fd30ee2008-01-10 03:20:28 -0800898 ops = lookup_rules_ops(net, family);
Thomas Grafc4546732007-03-25 23:24:24 -0700899 if (ops == NULL)
900 return -EAFNOSUPPORT;
901
Wilson Kok41fc0142015-09-22 21:40:22 -0700902 dump_rules(skb, cb, ops);
903
904 return skb->len;
Thomas Grafc4546732007-03-25 23:24:24 -0700905 }
906
907 rcu_read_lock();
Denis V. Lunev5fd30ee2008-01-10 03:20:28 -0800908 list_for_each_entry_rcu(ops, &net->rules_ops, list) {
Thomas Grafc4546732007-03-25 23:24:24 -0700909 if (idx < cb->args[0] || !try_module_get(ops->owner))
910 goto skip;
911
912 if (dump_rules(skb, cb, ops) < 0)
913 break;
914
915 cb->args[1] = 0;
Eric Dumazet2fb35732010-03-09 20:03:38 +0000916skip:
Thomas Grafc4546732007-03-25 23:24:24 -0700917 idx++;
918 }
919 rcu_read_unlock();
920 cb->args[0] = idx;
921
922 return skb->len;
923}
Thomas Graf14c0b972006-08-04 03:38:38 -0700924
Denis V. Lunev9e3a5482008-01-20 16:46:41 -0800925static void notify_rule_change(int event, struct fib_rule *rule,
Thomas Grafc17084d2006-08-15 00:32:48 -0700926 struct fib_rules_ops *ops, struct nlmsghdr *nlh,
927 u32 pid)
Thomas Graf14c0b972006-08-04 03:38:38 -0700928{
Denis V. Lunev9e3a5482008-01-20 16:46:41 -0800929 struct net *net;
Thomas Grafc17084d2006-08-15 00:32:48 -0700930 struct sk_buff *skb;
931 int err = -ENOBUFS;
Thomas Graf14c0b972006-08-04 03:38:38 -0700932
Denis V. Lunev9e3a5482008-01-20 16:46:41 -0800933 net = ops->fro_net;
Thomas Graf339bf982006-11-10 14:10:15 -0800934 skb = nlmsg_new(fib_rule_nlmsg_size(ops, rule), GFP_KERNEL);
Thomas Graf14c0b972006-08-04 03:38:38 -0700935 if (skb == NULL)
Thomas Grafc17084d2006-08-15 00:32:48 -0700936 goto errout;
937
938 err = fib_nl_fill_rule(skb, rule, pid, nlh->nlmsg_seq, event, 0, ops);
Patrick McHardy26932562007-01-31 23:16:40 -0800939 if (err < 0) {
940 /* -EMSGSIZE implies BUG in fib_rule_nlmsg_size() */
941 WARN_ON(err == -EMSGSIZE);
942 kfree_skb(skb);
943 goto errout;
944 }
Denis V. Lunev9e3a5482008-01-20 16:46:41 -0800945
Pablo Neira Ayuso1ce85fe2009-02-24 23:18:28 -0800946 rtnl_notify(skb, net, pid, ops->nlgroup, nlh, GFP_KERNEL);
947 return;
Thomas Grafc17084d2006-08-15 00:32:48 -0700948errout:
949 if (err < 0)
Denis V. Lunev5fd30ee2008-01-10 03:20:28 -0800950 rtnl_set_sk_err(net, ops->nlgroup, err);
Thomas Graf14c0b972006-08-04 03:38:38 -0700951}
952
953static void attach_rules(struct list_head *rules, struct net_device *dev)
954{
955 struct fib_rule *rule;
956
957 list_for_each_entry(rule, rules, list) {
Patrick McHardy491deb22009-12-03 01:25:54 +0000958 if (rule->iifindex == -1 &&
959 strcmp(dev->name, rule->iifname) == 0)
960 rule->iifindex = dev->ifindex;
Patrick McHardy1b038a52009-12-03 01:25:56 +0000961 if (rule->oifindex == -1 &&
962 strcmp(dev->name, rule->oifname) == 0)
963 rule->oifindex = dev->ifindex;
Thomas Graf14c0b972006-08-04 03:38:38 -0700964 }
965}
966
967static void detach_rules(struct list_head *rules, struct net_device *dev)
968{
969 struct fib_rule *rule;
970
Patrick McHardy1b038a52009-12-03 01:25:56 +0000971 list_for_each_entry(rule, rules, list) {
Patrick McHardy491deb22009-12-03 01:25:54 +0000972 if (rule->iifindex == dev->ifindex)
973 rule->iifindex = -1;
Patrick McHardy1b038a52009-12-03 01:25:56 +0000974 if (rule->oifindex == dev->ifindex)
975 rule->oifindex = -1;
976 }
Thomas Graf14c0b972006-08-04 03:38:38 -0700977}
978
979
980static int fib_rules_event(struct notifier_block *this, unsigned long event,
Jiri Pirko351638e2013-05-28 01:30:21 +0000981 void *ptr)
Thomas Graf14c0b972006-08-04 03:38:38 -0700982{
Jiri Pirko351638e2013-05-28 01:30:21 +0000983 struct net_device *dev = netdev_notifier_info_to_dev(ptr);
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +0900984 struct net *net = dev_net(dev);
Thomas Graf14c0b972006-08-04 03:38:38 -0700985 struct fib_rules_ops *ops;
986
Eric Dumazet748e2d92012-08-22 21:50:59 +0000987 ASSERT_RTNL();
Thomas Graf14c0b972006-08-04 03:38:38 -0700988
989 switch (event) {
990 case NETDEV_REGISTER:
Denis V. Lunev5fd30ee2008-01-10 03:20:28 -0800991 list_for_each_entry(ops, &net->rules_ops, list)
Denis V. Lunev76c72d42007-09-16 15:44:27 -0700992 attach_rules(&ops->rules_list, dev);
Thomas Graf14c0b972006-08-04 03:38:38 -0700993 break;
994
Maciej Żenczykowski946c0322014-02-07 16:23:48 -0800995 case NETDEV_CHANGENAME:
996 list_for_each_entry(ops, &net->rules_ops, list) {
997 detach_rules(&ops->rules_list, dev);
998 attach_rules(&ops->rules_list, dev);
999 }
1000 break;
1001
Thomas Graf14c0b972006-08-04 03:38:38 -07001002 case NETDEV_UNREGISTER:
Denis V. Lunev5fd30ee2008-01-10 03:20:28 -08001003 list_for_each_entry(ops, &net->rules_ops, list)
Denis V. Lunev76c72d42007-09-16 15:44:27 -07001004 detach_rules(&ops->rules_list, dev);
Thomas Graf14c0b972006-08-04 03:38:38 -07001005 break;
1006 }
1007
Thomas Graf14c0b972006-08-04 03:38:38 -07001008 return NOTIFY_DONE;
1009}
1010
1011static struct notifier_block fib_rules_notifier = {
1012 .notifier_call = fib_rules_event,
1013};
1014
Alexey Dobriyan2c8c1e72010-01-17 03:35:32 +00001015static int __net_init fib_rules_net_init(struct net *net)
Denis V. Lunev5fd30ee2008-01-10 03:20:28 -08001016{
1017 INIT_LIST_HEAD(&net->rules_ops);
1018 spin_lock_init(&net->rules_mod_lock);
1019 return 0;
1020}
1021
1022static struct pernet_operations fib_rules_net_ops = {
1023 .init = fib_rules_net_init,
1024};
1025
Thomas Graf14c0b972006-08-04 03:38:38 -07001026static int __init fib_rules_init(void)
1027{
Denis V. Lunev5fd30ee2008-01-10 03:20:28 -08001028 int err;
Greg Rosec7ac8672011-06-10 01:27:09 +00001029 rtnl_register(PF_UNSPEC, RTM_NEWRULE, fib_nl_newrule, NULL, NULL);
1030 rtnl_register(PF_UNSPEC, RTM_DELRULE, fib_nl_delrule, NULL, NULL);
1031 rtnl_register(PF_UNSPEC, RTM_GETRULE, NULL, fib_nl_dumprule, NULL);
Thomas Graf9d9e6a52007-03-25 23:20:05 -07001032
Eric W. Biederman5d6d4802008-11-07 22:52:34 -08001033 err = register_pernet_subsys(&fib_rules_net_ops);
Denis V. Lunev5fd30ee2008-01-10 03:20:28 -08001034 if (err < 0)
1035 goto fail;
1036
Eric W. Biederman5d6d4802008-11-07 22:52:34 -08001037 err = register_netdevice_notifier(&fib_rules_notifier);
Denis V. Lunev5fd30ee2008-01-10 03:20:28 -08001038 if (err < 0)
1039 goto fail_unregister;
Eric W. Biederman5d6d4802008-11-07 22:52:34 -08001040
Denis V. Lunev5fd30ee2008-01-10 03:20:28 -08001041 return 0;
1042
1043fail_unregister:
Eric W. Biederman5d6d4802008-11-07 22:52:34 -08001044 unregister_pernet_subsys(&fib_rules_net_ops);
Denis V. Lunev5fd30ee2008-01-10 03:20:28 -08001045fail:
1046 rtnl_unregister(PF_UNSPEC, RTM_NEWRULE);
1047 rtnl_unregister(PF_UNSPEC, RTM_DELRULE);
1048 rtnl_unregister(PF_UNSPEC, RTM_GETRULE);
1049 return err;
Thomas Graf14c0b972006-08-04 03:38:38 -07001050}
1051
1052subsys_initcall(fib_rules_init);