blob: a6aea805a0a263de2e79f6f5f7e3b90cf0b984a2 [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;
Donald Sharpcac56202018-02-20 08:55:58 -050054 r->proto = RTPROT_KERNEL;
Eric W. Biedermanefd7ef12015-03-11 23:04:08 -050055 r->fr_net = ops->fro_net;
Lorenzo Colitti622ec2c2016-11-04 02:23:42 +090056 r->uid_range = fib_kuid_range_unset;
Denis V. Lunev2994c632007-11-10 22:12:03 -080057
Stefan Tomanek73f56982013-08-03 14:14:43 +020058 r->suppress_prefixlen = -1;
59 r->suppress_ifgroup = -1;
60
Denis V. Lunev2994c632007-11-10 22:12:03 -080061 /* The lock is not required here, the list in unreacheable
62 * at the moment this function is called */
63 list_add_tail(&r->list, &ops->rules_list);
64 return 0;
65}
66EXPORT_SYMBOL(fib_default_rule_add);
67
Phil Sutterf53de1e2015-09-09 14:20:56 +020068static u32 fib_default_rule_pref(struct fib_rules_ops *ops)
Patrick McHardyd8a566b2010-04-13 05:03:15 +000069{
70 struct list_head *pos;
71 struct fib_rule *rule;
72
73 if (!list_empty(&ops->rules_list)) {
74 pos = ops->rules_list.next;
75 if (pos->next != &ops->rules_list) {
76 rule = list_entry(pos->next, struct fib_rule, list);
77 if (rule->pref)
78 return rule->pref - 1;
79 }
80 }
81
82 return 0;
83}
Patrick McHardyd8a566b2010-04-13 05:03:15 +000084
Denis V. Lunev9e3a5482008-01-20 16:46:41 -080085static void notify_rule_change(int event, struct fib_rule *rule,
Thomas Grafc17084d2006-08-15 00:32:48 -070086 struct fib_rules_ops *ops, struct nlmsghdr *nlh,
87 u32 pid);
Thomas Graf14c0b972006-08-04 03:38:38 -070088
Denis V. Lunev5fd30ee2008-01-10 03:20:28 -080089static struct fib_rules_ops *lookup_rules_ops(struct net *net, int family)
Thomas Graf14c0b972006-08-04 03:38:38 -070090{
91 struct fib_rules_ops *ops;
92
93 rcu_read_lock();
Denis V. Lunev5fd30ee2008-01-10 03:20:28 -080094 list_for_each_entry_rcu(ops, &net->rules_ops, list) {
Thomas Graf14c0b972006-08-04 03:38:38 -070095 if (ops->family == family) {
96 if (!try_module_get(ops->owner))
97 ops = NULL;
98 rcu_read_unlock();
99 return ops;
100 }
101 }
102 rcu_read_unlock();
103
104 return NULL;
105}
106
107static void rules_ops_put(struct fib_rules_ops *ops)
108{
109 if (ops)
110 module_put(ops->owner);
111}
112
Thomas Graf73417f62007-03-27 13:56:52 -0700113static void flush_route_cache(struct fib_rules_ops *ops)
114{
115 if (ops->flush_cache)
Denis V. Lunevae299fc2008-07-05 19:01:28 -0700116 ops->flush_cache(ops);
Thomas Graf73417f62007-03-27 13:56:52 -0700117}
118
Eric W. Biedermane9c51582009-12-03 12:22:55 -0800119static int __fib_rules_register(struct fib_rules_ops *ops)
Thomas Graf14c0b972006-08-04 03:38:38 -0700120{
121 int err = -EEXIST;
122 struct fib_rules_ops *o;
Denis V. Lunev9e3a5482008-01-20 16:46:41 -0800123 struct net *net;
124
125 net = ops->fro_net;
Thomas Graf14c0b972006-08-04 03:38:38 -0700126
127 if (ops->rule_size < sizeof(struct fib_rule))
128 return -EINVAL;
129
130 if (ops->match == NULL || ops->configure == NULL ||
131 ops->compare == NULL || ops->fill == NULL ||
132 ops->action == NULL)
133 return -EINVAL;
134
Denis V. Lunev5fd30ee2008-01-10 03:20:28 -0800135 spin_lock(&net->rules_mod_lock);
136 list_for_each_entry(o, &net->rules_ops, list)
Thomas Graf14c0b972006-08-04 03:38:38 -0700137 if (ops->family == o->family)
138 goto errout;
139
Denis V. Lunev5fd30ee2008-01-10 03:20:28 -0800140 list_add_tail_rcu(&ops->list, &net->rules_ops);
Thomas Graf14c0b972006-08-04 03:38:38 -0700141 err = 0;
142errout:
Denis V. Lunev5fd30ee2008-01-10 03:20:28 -0800143 spin_unlock(&net->rules_mod_lock);
Thomas Graf14c0b972006-08-04 03:38:38 -0700144
145 return err;
146}
147
Eric W. Biedermane9c51582009-12-03 12:22:55 -0800148struct fib_rules_ops *
Patrick McHardy3d0c9c42010-04-26 16:02:04 +0200149fib_rules_register(const struct fib_rules_ops *tmpl, struct net *net)
Eric W. Biedermane9c51582009-12-03 12:22:55 -0800150{
151 struct fib_rules_ops *ops;
152 int err;
153
Eric Dumazet2fb35732010-03-09 20:03:38 +0000154 ops = kmemdup(tmpl, sizeof(*ops), GFP_KERNEL);
Eric W. Biedermane9c51582009-12-03 12:22:55 -0800155 if (ops == NULL)
156 return ERR_PTR(-ENOMEM);
157
158 INIT_LIST_HEAD(&ops->rules_list);
159 ops->fro_net = net;
160
161 err = __fib_rules_register(ops);
162 if (err) {
163 kfree(ops);
164 ops = ERR_PTR(err);
165 }
166
167 return ops;
168}
Thomas Graf14c0b972006-08-04 03:38:38 -0700169EXPORT_SYMBOL_GPL(fib_rules_register);
170
stephen hemminger1df99162010-10-04 20:14:17 +0000171static void fib_rules_cleanup_ops(struct fib_rules_ops *ops)
Thomas Graf14c0b972006-08-04 03:38:38 -0700172{
173 struct fib_rule *rule, *tmp;
174
Denis V. Lunev76c72d42007-09-16 15:44:27 -0700175 list_for_each_entry_safe(rule, tmp, &ops->rules_list, list) {
Thomas Graf14c0b972006-08-04 03:38:38 -0700176 list_del_rcu(&rule->list);
David S. Miller7a9bc9b2012-06-29 01:32:45 -0700177 if (ops->delete)
178 ops->delete(rule);
Thomas Graf14c0b972006-08-04 03:38:38 -0700179 fib_rule_put(rule);
180 }
181}
182
Denis V. Lunev9e3a5482008-01-20 16:46:41 -0800183void fib_rules_unregister(struct fib_rules_ops *ops)
Thomas Graf14c0b972006-08-04 03:38:38 -0700184{
Denis V. Lunev9e3a5482008-01-20 16:46:41 -0800185 struct net *net = ops->fro_net;
Thomas Graf14c0b972006-08-04 03:38:38 -0700186
Denis V. Lunev5fd30ee2008-01-10 03:20:28 -0800187 spin_lock(&net->rules_mod_lock);
Denis V. Lunev72132c1b2008-01-14 22:59:30 -0800188 list_del_rcu(&ops->list);
Denis V. Lunev5fd30ee2008-01-10 03:20:28 -0800189 spin_unlock(&net->rules_mod_lock);
Thomas Graf14c0b972006-08-04 03:38:38 -0700190
WANG Cong419df122015-03-31 11:01:46 -0700191 fib_rules_cleanup_ops(ops);
Eric W. Biedermanefd7ef12015-03-11 23:04:08 -0500192 kfree_rcu(ops, rcu);
Thomas Graf14c0b972006-08-04 03:38:38 -0700193}
Thomas Graf14c0b972006-08-04 03:38:38 -0700194EXPORT_SYMBOL_GPL(fib_rules_unregister);
195
Lorenzo Colitti622ec2c2016-11-04 02:23:42 +0900196static int uid_range_set(struct fib_kuid_range *range)
197{
198 return uid_valid(range->start) && uid_valid(range->end);
199}
200
201static struct fib_kuid_range nla_get_kuid_range(struct nlattr **tb)
202{
203 struct fib_rule_uid_range *in;
204 struct fib_kuid_range out;
205
206 in = (struct fib_rule_uid_range *)nla_data(tb[FRA_UID_RANGE]);
207
208 out.start = make_kuid(current_user_ns(), in->start);
209 out.end = make_kuid(current_user_ns(), in->end);
210
211 return out;
212}
213
214static int nla_put_uid_range(struct sk_buff *skb, struct fib_kuid_range *range)
215{
216 struct fib_rule_uid_range out = {
217 from_kuid_munged(current_user_ns(), range->start),
218 from_kuid_munged(current_user_ns(), range->end)
219 };
220
221 return nla_put(skb, FRA_UID_RANGE, sizeof(out), &out);
222}
223
Thomas Graf3dfbcc42006-11-09 15:23:20 -0800224static int fib_rule_match(struct fib_rule *rule, struct fib_rules_ops *ops,
David Ahern96c63fa2016-06-08 10:55:39 -0700225 struct flowi *fl, int flags,
226 struct fib_lookup_arg *arg)
Thomas Graf3dfbcc42006-11-09 15:23:20 -0800227{
228 int ret = 0;
229
David S. Miller1d28f422011-03-12 00:29:39 -0500230 if (rule->iifindex && (rule->iifindex != fl->flowi_iif))
Thomas Graf3dfbcc42006-11-09 15:23:20 -0800231 goto out;
232
David S. Miller1d28f422011-03-12 00:29:39 -0500233 if (rule->oifindex && (rule->oifindex != fl->flowi_oif))
Patrick McHardy1b038a52009-12-03 01:25:56 +0000234 goto out;
235
David S. Miller1d28f422011-03-12 00:29:39 -0500236 if ((rule->mark ^ fl->flowi_mark) & rule->mark_mask)
Thomas Graf3dfbcc42006-11-09 15:23:20 -0800237 goto out;
238
Thomas Grafe7030872015-07-21 10:44:01 +0200239 if (rule->tun_id && (rule->tun_id != fl->flowi_tun_key.tun_id))
240 goto out;
241
David Ahern96c63fa2016-06-08 10:55:39 -0700242 if (rule->l3mdev && !l3mdev_fib_rule_match(rule->fr_net, fl, arg))
243 goto out;
244
Lorenzo Colitti622ec2c2016-11-04 02:23:42 +0900245 if (uid_lt(fl->flowi_uid, rule->uid_range.start) ||
246 uid_gt(fl->flowi_uid, rule->uid_range.end))
247 goto out;
248
Thomas Graf3dfbcc42006-11-09 15:23:20 -0800249 ret = ops->match(rule, fl, flags);
250out:
251 return (rule->flags & FIB_RULE_INVERT) ? !ret : ret;
252}
253
Thomas Graf14c0b972006-08-04 03:38:38 -0700254int fib_rules_lookup(struct fib_rules_ops *ops, struct flowi *fl,
255 int flags, struct fib_lookup_arg *arg)
256{
257 struct fib_rule *rule;
258 int err;
259
260 rcu_read_lock();
261
Denis V. Lunev76c72d42007-09-16 15:44:27 -0700262 list_for_each_entry_rcu(rule, &ops->rules_list, list) {
Thomas Graf0947c9fe2007-03-26 17:14:15 -0700263jumped:
David Ahern96c63fa2016-06-08 10:55:39 -0700264 if (!fib_rule_match(rule, ops, fl, flags, arg))
Thomas Graf14c0b972006-08-04 03:38:38 -0700265 continue;
266
Thomas Graf0947c9fe2007-03-26 17:14:15 -0700267 if (rule->action == FR_ACT_GOTO) {
268 struct fib_rule *target;
269
270 target = rcu_dereference(rule->ctarget);
271 if (target == NULL) {
272 continue;
273 } else {
274 rule = target;
275 goto jumped;
276 }
Thomas Graffa0b2d12007-03-26 17:38:53 -0700277 } else if (rule->action == FR_ACT_NOP)
278 continue;
279 else
Thomas Graf0947c9fe2007-03-26 17:14:15 -0700280 err = ops->action(rule, fl, flags, arg);
281
Stefan Tomanek7764a452013-08-01 02:17:15 +0200282 if (!err && ops->suppress && ops->suppress(rule, arg))
283 continue;
284
Thomas Graf14c0b972006-08-04 03:38:38 -0700285 if (err != -EAGAIN) {
Eric Dumazetebc0ffa2010-10-05 10:41:36 +0000286 if ((arg->flags & FIB_LOOKUP_NOREF) ||
Reshetova, Elena717d1e92017-06-30 13:08:06 +0300287 likely(refcount_inc_not_zero(&rule->refcnt))) {
Eric Dumazet7fa7cb72010-09-27 04:18:27 +0000288 arg->rule = rule;
289 goto out;
290 }
291 break;
Thomas Graf14c0b972006-08-04 03:38:38 -0700292 }
293 }
294
Steven Whitehouse83886b62007-03-30 13:34:27 -0700295 err = -ESRCH;
Thomas Graf14c0b972006-08-04 03:38:38 -0700296out:
297 rcu_read_unlock();
298
299 return err;
300}
Thomas Graf14c0b972006-08-04 03:38:38 -0700301EXPORT_SYMBOL_GPL(fib_rules_lookup);
302
Ido Schimmel1b2a4442017-08-03 13:28:14 +0200303static int call_fib_rule_notifier(struct notifier_block *nb, struct net *net,
304 enum fib_event_type event_type,
305 struct fib_rule *rule, int family)
306{
307 struct fib_rule_notifier_info info = {
308 .info.family = family,
309 .rule = rule,
310 };
311
312 return call_fib_notifier(nb, net, event_type, &info.info);
313}
314
315static int call_fib_rule_notifiers(struct net *net,
316 enum fib_event_type event_type,
317 struct fib_rule *rule,
David Ahern6c31e5a2017-10-27 17:37:13 -0700318 struct fib_rules_ops *ops,
319 struct netlink_ext_ack *extack)
Ido Schimmel1b2a4442017-08-03 13:28:14 +0200320{
321 struct fib_rule_notifier_info info = {
322 .info.family = ops->family,
David Ahern6c31e5a2017-10-27 17:37:13 -0700323 .info.extack = extack,
Ido Schimmel1b2a4442017-08-03 13:28:14 +0200324 .rule = rule,
325 };
326
327 ops->fib_rules_seq++;
328 return call_fib_notifiers(net, event_type, &info.info);
329}
330
331/* Called with rcu_read_lock() */
332int fib_rules_dump(struct net *net, struct notifier_block *nb, int family)
333{
334 struct fib_rules_ops *ops;
335 struct fib_rule *rule;
336
337 ops = lookup_rules_ops(net, family);
338 if (!ops)
339 return -EAFNOSUPPORT;
340 list_for_each_entry_rcu(rule, &ops->rules_list, list)
341 call_fib_rule_notifier(nb, net, FIB_EVENT_RULE_ADD, rule,
342 family);
343 rules_ops_put(ops);
344
345 return 0;
346}
347EXPORT_SYMBOL_GPL(fib_rules_dump);
348
349unsigned int fib_rules_seq_read(struct net *net, int family)
350{
351 unsigned int fib_rules_seq;
352 struct fib_rules_ops *ops;
353
354 ASSERT_RTNL();
355
356 ops = lookup_rules_ops(net, family);
357 if (!ops)
358 return 0;
359 fib_rules_seq = ops->fib_rules_seq;
360 rules_ops_put(ops);
361
362 return fib_rules_seq;
363}
364EXPORT_SYMBOL_GPL(fib_rules_seq_read);
365
Thomas Grafe1701c62007-03-24 12:46:02 -0700366static int validate_rulemsg(struct fib_rule_hdr *frh, struct nlattr **tb,
367 struct fib_rules_ops *ops)
368{
369 int err = -EINVAL;
370
371 if (frh->src_len)
372 if (tb[FRA_SRC] == NULL ||
373 frh->src_len > (ops->addr_size * 8) ||
374 nla_len(tb[FRA_SRC]) != ops->addr_size)
375 goto errout;
376
377 if (frh->dst_len)
378 if (tb[FRA_DST] == NULL ||
379 frh->dst_len > (ops->addr_size * 8) ||
380 nla_len(tb[FRA_DST]) != ops->addr_size)
381 goto errout;
382
383 err = 0;
384errout:
385 return err;
386}
387
Mateusz Bajorski153380e2016-06-29 09:22:10 +0200388static int rule_exists(struct fib_rules_ops *ops, struct fib_rule_hdr *frh,
389 struct nlattr **tb, struct fib_rule *rule)
390{
391 struct fib_rule *r;
392
393 list_for_each_entry(r, &ops->rules_list, list) {
394 if (r->action != rule->action)
395 continue;
396
397 if (r->table != rule->table)
398 continue;
399
400 if (r->pref != rule->pref)
401 continue;
402
403 if (memcmp(r->iifname, rule->iifname, IFNAMSIZ))
404 continue;
405
406 if (memcmp(r->oifname, rule->oifname, IFNAMSIZ))
407 continue;
408
409 if (r->mark != rule->mark)
410 continue;
411
412 if (r->mark_mask != rule->mark_mask)
413 continue;
414
415 if (r->tun_id != rule->tun_id)
416 continue;
417
418 if (r->fr_net != rule->fr_net)
419 continue;
420
421 if (r->l3mdev != rule->l3mdev)
422 continue;
423
Lorenzo Colitti35b80732016-11-07 00:16:25 +0900424 if (!uid_eq(r->uid_range.start, rule->uid_range.start) ||
425 !uid_eq(r->uid_range.end, rule->uid_range.end))
426 continue;
427
Mateusz Bajorski153380e2016-06-29 09:22:10 +0200428 if (!ops->compare(r, frh, tb))
429 continue;
430 return 1;
431 }
432 return 0;
433}
434
David Ahernc21ef3e2017-04-16 09:48:24 -0700435int fib_nl_newrule(struct sk_buff *skb, struct nlmsghdr *nlh,
436 struct netlink_ext_ack *extack)
Thomas Graf14c0b972006-08-04 03:38:38 -0700437{
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +0900438 struct net *net = sock_net(skb->sk);
Thomas Graf14c0b972006-08-04 03:38:38 -0700439 struct fib_rule_hdr *frh = nlmsg_data(nlh);
440 struct fib_rules_ops *ops = NULL;
441 struct fib_rule *rule, *r, *last = NULL;
442 struct nlattr *tb[FRA_MAX+1];
Thomas Graf0947c9fe2007-03-26 17:14:15 -0700443 int err = -EINVAL, unresolved = 0;
Thomas Graf14c0b972006-08-04 03:38:38 -0700444
445 if (nlh->nlmsg_len < nlmsg_msg_size(sizeof(*frh)))
446 goto errout;
447
Denis V. Lunev5fd30ee2008-01-10 03:20:28 -0800448 ops = lookup_rules_ops(net, frh->family);
Thomas Graf14c0b972006-08-04 03:38:38 -0700449 if (ops == NULL) {
Patrick McHardy2fe195c2008-07-01 19:59:37 -0700450 err = -EAFNOSUPPORT;
Thomas Graf14c0b972006-08-04 03:38:38 -0700451 goto errout;
452 }
453
David Ahernc21ef3e2017-04-16 09:48:24 -0700454 err = nlmsg_parse(nlh, sizeof(*frh), tb, FRA_MAX, ops->policy, extack);
Thomas Graf14c0b972006-08-04 03:38:38 -0700455 if (err < 0)
456 goto errout;
457
Thomas Grafe1701c62007-03-24 12:46:02 -0700458 err = validate_rulemsg(frh, tb, ops);
459 if (err < 0)
460 goto errout;
461
Thomas Graf14c0b972006-08-04 03:38:38 -0700462 rule = kzalloc(ops->rule_size, GFP_KERNEL);
463 if (rule == NULL) {
464 err = -ENOMEM;
465 goto errout;
466 }
David Ahern5d89fb32017-07-13 13:36:40 -0700467 refcount_set(&rule->refcnt, 1);
Eric W. Biedermanefd7ef12015-03-11 23:04:08 -0500468 rule->fr_net = net;
Thomas Graf14c0b972006-08-04 03:38:38 -0700469
Phil Sutterf53de1e2015-09-09 14:20:56 +0200470 rule->pref = tb[FRA_PRIORITY] ? nla_get_u32(tb[FRA_PRIORITY])
471 : fib_default_rule_pref(ops);
Thomas Graf14c0b972006-08-04 03:38:38 -0700472
Donald Sharp1b71af62018-02-23 14:01:52 -0500473 rule->proto = tb[FRA_PROTOCOL] ?
474 nla_get_u8(tb[FRA_PROTOCOL]) : RTPROT_UNSPEC;
475
Patrick McHardy491deb22009-12-03 01:25:54 +0000476 if (tb[FRA_IIFNAME]) {
Thomas Graf14c0b972006-08-04 03:38:38 -0700477 struct net_device *dev;
478
Patrick McHardy491deb22009-12-03 01:25:54 +0000479 rule->iifindex = -1;
480 nla_strlcpy(rule->iifname, tb[FRA_IIFNAME], IFNAMSIZ);
481 dev = __dev_get_by_name(net, rule->iifname);
Thomas Graf14c0b972006-08-04 03:38:38 -0700482 if (dev)
Patrick McHardy491deb22009-12-03 01:25:54 +0000483 rule->iifindex = dev->ifindex;
Thomas Graf14c0b972006-08-04 03:38:38 -0700484 }
485
Patrick McHardy1b038a52009-12-03 01:25:56 +0000486 if (tb[FRA_OIFNAME]) {
487 struct net_device *dev;
488
489 rule->oifindex = -1;
490 nla_strlcpy(rule->oifname, tb[FRA_OIFNAME], IFNAMSIZ);
491 dev = __dev_get_by_name(net, rule->oifname);
492 if (dev)
493 rule->oifindex = dev->ifindex;
494 }
495
Thomas Grafb8964ed2006-11-09 15:22:18 -0800496 if (tb[FRA_FWMARK]) {
497 rule->mark = nla_get_u32(tb[FRA_FWMARK]);
498 if (rule->mark)
499 /* compatibility: if the mark value is non-zero all bits
500 * are compared unless a mask is explicitly specified.
501 */
502 rule->mark_mask = 0xFFFFFFFF;
503 }
504
505 if (tb[FRA_FWMASK])
506 rule->mark_mask = nla_get_u32(tb[FRA_FWMASK]);
507
Thomas Grafe7030872015-07-21 10:44:01 +0200508 if (tb[FRA_TUN_ID])
509 rule->tun_id = nla_get_be64(tb[FRA_TUN_ID]);
510
Wei Yongjunadeb45c2017-04-26 14:03:50 +0000511 err = -EINVAL;
David Ahern96c63fa2016-06-08 10:55:39 -0700512 if (tb[FRA_L3MDEV]) {
513#ifdef CONFIG_NET_L3_MASTER_DEV
514 rule->l3mdev = nla_get_u8(tb[FRA_L3MDEV]);
515 if (rule->l3mdev != 1)
516#endif
517 goto errout_free;
518 }
519
Thomas Graf14c0b972006-08-04 03:38:38 -0700520 rule->action = frh->action;
521 rule->flags = frh->flags;
Patrick McHardy9e762a42006-08-10 23:09:48 -0700522 rule->table = frh_get_table(frh, tb);
Stefan Tomanek73f56982013-08-03 14:14:43 +0200523 if (tb[FRA_SUPPRESS_PREFIXLEN])
524 rule->suppress_prefixlen = nla_get_u32(tb[FRA_SUPPRESS_PREFIXLEN]);
525 else
526 rule->suppress_prefixlen = -1;
Thomas Graf14c0b972006-08-04 03:38:38 -0700527
Stefan Tomanek6ef94cf2013-08-02 17:19:56 +0200528 if (tb[FRA_SUPPRESS_IFGROUP])
529 rule->suppress_ifgroup = nla_get_u32(tb[FRA_SUPPRESS_IFGROUP]);
Stefan Tomanek73f56982013-08-03 14:14:43 +0200530 else
531 rule->suppress_ifgroup = -1;
Stefan Tomanek6ef94cf2013-08-02 17:19:56 +0200532
Thomas Graf0947c9fe2007-03-26 17:14:15 -0700533 if (tb[FRA_GOTO]) {
534 if (rule->action != FR_ACT_GOTO)
535 goto errout_free;
536
537 rule->target = nla_get_u32(tb[FRA_GOTO]);
538 /* Backward jumps are prohibited to avoid endless loops */
539 if (rule->target <= rule->pref)
540 goto errout_free;
541
Denis V. Lunev76c72d42007-09-16 15:44:27 -0700542 list_for_each_entry(r, &ops->rules_list, list) {
Thomas Graf0947c9fe2007-03-26 17:14:15 -0700543 if (r->pref == rule->target) {
Eric Dumazet7a2b03c2010-10-26 09:24:55 +0000544 RCU_INIT_POINTER(rule->ctarget, r);
Thomas Graf0947c9fe2007-03-26 17:14:15 -0700545 break;
546 }
547 }
548
Eric Dumazet7a2b03c2010-10-26 09:24:55 +0000549 if (rcu_dereference_protected(rule->ctarget, 1) == NULL)
Thomas Graf0947c9fe2007-03-26 17:14:15 -0700550 unresolved = 1;
551 } else if (rule->action == FR_ACT_GOTO)
552 goto errout_free;
553
David Ahern96c63fa2016-06-08 10:55:39 -0700554 if (rule->l3mdev && rule->table)
555 goto errout_free;
556
Lorenzo Colitti622ec2c2016-11-04 02:23:42 +0900557 if (tb[FRA_UID_RANGE]) {
558 if (current_user_ns() != net->user_ns) {
559 err = -EPERM;
560 goto errout_free;
561 }
562
563 rule->uid_range = nla_get_kuid_range(tb);
564
565 if (!uid_range_set(&rule->uid_range) ||
566 !uid_lte(rule->uid_range.start, rule->uid_range.end))
567 goto errout_free;
568 } else {
569 rule->uid_range = fib_kuid_range_unset;
570 }
571
Mateusz Bajorski153380e2016-06-29 09:22:10 +0200572 if ((nlh->nlmsg_flags & NLM_F_EXCL) &&
573 rule_exists(ops, frh, tb, rule)) {
574 err = -EEXIST;
575 goto errout_free;
576 }
577
Rami Rosen8b3521e2009-05-11 05:52:49 +0000578 err = ops->configure(rule, skb, frh, tb);
Thomas Graf14c0b972006-08-04 03:38:38 -0700579 if (err < 0)
580 goto errout_free;
581
Denis V. Lunev76c72d42007-09-16 15:44:27 -0700582 list_for_each_entry(r, &ops->rules_list, list) {
Thomas Graf14c0b972006-08-04 03:38:38 -0700583 if (r->pref > rule->pref)
584 break;
585 last = r;
586 }
587
Eric Dumazetebb9fed2010-10-23 09:44:25 +0000588 if (last)
589 list_add_rcu(&rule->list, &last->list);
590 else
591 list_add_rcu(&rule->list, &ops->rules_list);
592
Thomas Graf0947c9fe2007-03-26 17:14:15 -0700593 if (ops->unresolved_rules) {
594 /*
595 * There are unresolved goto rules in the list, check if
596 * any of them are pointing to this new rule.
597 */
Denis V. Lunev76c72d42007-09-16 15:44:27 -0700598 list_for_each_entry(r, &ops->rules_list, list) {
Thomas Graf0947c9fe2007-03-26 17:14:15 -0700599 if (r->action == FR_ACT_GOTO &&
Gao feng561dac22011-09-11 15:36:05 +0000600 r->target == rule->pref &&
601 rtnl_dereference(r->ctarget) == NULL) {
Thomas Graf0947c9fe2007-03-26 17:14:15 -0700602 rcu_assign_pointer(r->ctarget, rule);
603 if (--ops->unresolved_rules == 0)
604 break;
605 }
606 }
607 }
608
609 if (rule->action == FR_ACT_GOTO)
610 ops->nr_goto_rules++;
611
612 if (unresolved)
613 ops->unresolved_rules++;
614
Thomas Grafe7030872015-07-21 10:44:01 +0200615 if (rule->tun_id)
616 ip_tunnel_need_metadata();
617
David Ahern6c31e5a2017-10-27 17:37:13 -0700618 call_fib_rule_notifiers(net, FIB_EVENT_RULE_ADD, rule, ops, extack);
Eric W. Biederman15e47302012-09-07 20:12:54 +0000619 notify_rule_change(RTM_NEWRULE, rule, ops, nlh, NETLINK_CB(skb).portid);
Thomas Graf73417f62007-03-27 13:56:52 -0700620 flush_route_cache(ops);
Thomas Graf14c0b972006-08-04 03:38:38 -0700621 rules_ops_put(ops);
622 return 0;
623
624errout_free:
625 kfree(rule);
626errout:
627 rules_ops_put(ops);
628 return err;
629}
David Ahern96c63fa2016-06-08 10:55:39 -0700630EXPORT_SYMBOL_GPL(fib_nl_newrule);
Thomas Graf14c0b972006-08-04 03:38:38 -0700631
David Ahernc21ef3e2017-04-16 09:48:24 -0700632int fib_nl_delrule(struct sk_buff *skb, struct nlmsghdr *nlh,
633 struct netlink_ext_ack *extack)
Thomas Graf14c0b972006-08-04 03:38:38 -0700634{
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +0900635 struct net *net = sock_net(skb->sk);
Thomas Graf14c0b972006-08-04 03:38:38 -0700636 struct fib_rule_hdr *frh = nlmsg_data(nlh);
637 struct fib_rules_ops *ops = NULL;
Serhey Popovychbdaf32c2017-06-16 15:44:47 +0300638 struct fib_rule *rule, *r;
Thomas Graf14c0b972006-08-04 03:38:38 -0700639 struct nlattr *tb[FRA_MAX+1];
Lorenzo Colitti622ec2c2016-11-04 02:23:42 +0900640 struct fib_kuid_range range;
Thomas Graf14c0b972006-08-04 03:38:38 -0700641 int err = -EINVAL;
642
643 if (nlh->nlmsg_len < nlmsg_msg_size(sizeof(*frh)))
644 goto errout;
645
Denis V. Lunev5fd30ee2008-01-10 03:20:28 -0800646 ops = lookup_rules_ops(net, frh->family);
Thomas Graf14c0b972006-08-04 03:38:38 -0700647 if (ops == NULL) {
Patrick McHardy2fe195c2008-07-01 19:59:37 -0700648 err = -EAFNOSUPPORT;
Thomas Graf14c0b972006-08-04 03:38:38 -0700649 goto errout;
650 }
651
David Ahernc21ef3e2017-04-16 09:48:24 -0700652 err = nlmsg_parse(nlh, sizeof(*frh), tb, FRA_MAX, ops->policy, extack);
Thomas Graf14c0b972006-08-04 03:38:38 -0700653 if (err < 0)
654 goto errout;
655
Thomas Grafe1701c62007-03-24 12:46:02 -0700656 err = validate_rulemsg(frh, tb, ops);
657 if (err < 0)
658 goto errout;
659
Lorenzo Colitti622ec2c2016-11-04 02:23:42 +0900660 if (tb[FRA_UID_RANGE]) {
661 range = nla_get_kuid_range(tb);
Wei Yongjunadeb45c2017-04-26 14:03:50 +0000662 if (!uid_range_set(&range)) {
663 err = -EINVAL;
Lorenzo Colitti622ec2c2016-11-04 02:23:42 +0900664 goto errout;
Wei Yongjunadeb45c2017-04-26 14:03:50 +0000665 }
Lorenzo Colitti622ec2c2016-11-04 02:23:42 +0900666 } else {
667 range = fib_kuid_range_unset;
668 }
669
Denis V. Lunev76c72d42007-09-16 15:44:27 -0700670 list_for_each_entry(rule, &ops->rules_list, list) {
Donald Sharp1b71af62018-02-23 14:01:52 -0500671 if (tb[FRA_PROTOCOL] &&
672 (rule->proto != nla_get_u8(tb[FRA_PROTOCOL])))
Donald Sharpcac56202018-02-20 08:55:58 -0500673 continue;
674
Thomas Graf14c0b972006-08-04 03:38:38 -0700675 if (frh->action && (frh->action != rule->action))
676 continue;
677
Andreas Henriksson13eb2ab2013-11-07 18:26:38 +0100678 if (frh_get_table(frh, tb) &&
679 (frh_get_table(frh, tb) != rule->table))
Thomas Graf14c0b972006-08-04 03:38:38 -0700680 continue;
681
682 if (tb[FRA_PRIORITY] &&
683 (rule->pref != nla_get_u32(tb[FRA_PRIORITY])))
684 continue;
685
Patrick McHardy491deb22009-12-03 01:25:54 +0000686 if (tb[FRA_IIFNAME] &&
687 nla_strcmp(tb[FRA_IIFNAME], rule->iifname))
Thomas Graf14c0b972006-08-04 03:38:38 -0700688 continue;
689
Patrick McHardy1b038a52009-12-03 01:25:56 +0000690 if (tb[FRA_OIFNAME] &&
691 nla_strcmp(tb[FRA_OIFNAME], rule->oifname))
692 continue;
693
Thomas Grafb8964ed2006-11-09 15:22:18 -0800694 if (tb[FRA_FWMARK] &&
695 (rule->mark != nla_get_u32(tb[FRA_FWMARK])))
696 continue;
697
698 if (tb[FRA_FWMASK] &&
699 (rule->mark_mask != nla_get_u32(tb[FRA_FWMASK])))
700 continue;
701
Thomas Grafe7030872015-07-21 10:44:01 +0200702 if (tb[FRA_TUN_ID] &&
703 (rule->tun_id != nla_get_be64(tb[FRA_TUN_ID])))
704 continue;
705
David Ahern96c63fa2016-06-08 10:55:39 -0700706 if (tb[FRA_L3MDEV] &&
707 (rule->l3mdev != nla_get_u8(tb[FRA_L3MDEV])))
708 continue;
709
Lorenzo Colitti622ec2c2016-11-04 02:23:42 +0900710 if (uid_range_set(&range) &&
711 (!uid_eq(rule->uid_range.start, range.start) ||
712 !uid_eq(rule->uid_range.end, range.end)))
713 continue;
714
Thomas Graf14c0b972006-08-04 03:38:38 -0700715 if (!ops->compare(rule, frh, tb))
716 continue;
717
718 if (rule->flags & FIB_RULE_PERMANENT) {
719 err = -EPERM;
720 goto errout;
721 }
722
Alexander Duyck0ddcf432015-03-06 13:47:00 -0800723 if (ops->delete) {
724 err = ops->delete(rule);
725 if (err)
726 goto errout;
727 }
728
Thomas Grafe7030872015-07-21 10:44:01 +0200729 if (rule->tun_id)
730 ip_tunnel_unneed_metadata();
731
Thomas Graf14c0b972006-08-04 03:38:38 -0700732 list_del_rcu(&rule->list);
Thomas Graf0947c9fe2007-03-26 17:14:15 -0700733
Yan, Zhengafaef732011-10-17 15:20:28 +0000734 if (rule->action == FR_ACT_GOTO) {
Thomas Graf0947c9fe2007-03-26 17:14:15 -0700735 ops->nr_goto_rules--;
Yan, Zhengafaef732011-10-17 15:20:28 +0000736 if (rtnl_dereference(rule->ctarget) == NULL)
737 ops->unresolved_rules--;
738 }
Thomas Graf0947c9fe2007-03-26 17:14:15 -0700739
740 /*
741 * Check if this rule is a target to any of them. If so,
Serhey Popovychbdaf32c2017-06-16 15:44:47 +0300742 * adjust to the next one with the same preference or
Thomas Graf0947c9fe2007-03-26 17:14:15 -0700743 * disable them. As this operation is eventually very
Serhey Popovychbdaf32c2017-06-16 15:44:47 +0300744 * expensive, it is only performed if goto rules, except
745 * current if it is goto rule, have actually been added.
Thomas Graf0947c9fe2007-03-26 17:14:15 -0700746 */
747 if (ops->nr_goto_rules > 0) {
Serhey Popovychbdaf32c2017-06-16 15:44:47 +0300748 struct fib_rule *n;
749
750 n = list_next_entry(rule, list);
751 if (&n->list == &ops->rules_list || n->pref != rule->pref)
752 n = NULL;
753 list_for_each_entry(r, &ops->rules_list, list) {
754 if (rtnl_dereference(r->ctarget) != rule)
755 continue;
756 rcu_assign_pointer(r->ctarget, n);
757 if (!n)
Thomas Graf0947c9fe2007-03-26 17:14:15 -0700758 ops->unresolved_rules++;
Thomas Graf0947c9fe2007-03-26 17:14:15 -0700759 }
760 }
761
David Ahern6c31e5a2017-10-27 17:37:13 -0700762 call_fib_rule_notifiers(net, FIB_EVENT_RULE_DEL, rule, ops,
763 NULL);
Denis V. Lunev9e3a5482008-01-20 16:46:41 -0800764 notify_rule_change(RTM_DELRULE, rule, ops, nlh,
Eric W. Biederman15e47302012-09-07 20:12:54 +0000765 NETLINK_CB(skb).portid);
Thomas Graf14c0b972006-08-04 03:38:38 -0700766 fib_rule_put(rule);
Thomas Graf73417f62007-03-27 13:56:52 -0700767 flush_route_cache(ops);
Thomas Graf14c0b972006-08-04 03:38:38 -0700768 rules_ops_put(ops);
769 return 0;
770 }
771
772 err = -ENOENT;
773errout:
774 rules_ops_put(ops);
775 return err;
776}
David Ahern96c63fa2016-06-08 10:55:39 -0700777EXPORT_SYMBOL_GPL(fib_nl_delrule);
Thomas Graf14c0b972006-08-04 03:38:38 -0700778
Thomas Graf339bf982006-11-10 14:10:15 -0800779static inline size_t fib_rule_nlmsg_size(struct fib_rules_ops *ops,
780 struct fib_rule *rule)
781{
782 size_t payload = NLMSG_ALIGN(sizeof(struct fib_rule_hdr))
Patrick McHardy491deb22009-12-03 01:25:54 +0000783 + nla_total_size(IFNAMSIZ) /* FRA_IIFNAME */
Patrick McHardy1b038a52009-12-03 01:25:56 +0000784 + nla_total_size(IFNAMSIZ) /* FRA_OIFNAME */
Thomas Graf339bf982006-11-10 14:10:15 -0800785 + nla_total_size(4) /* FRA_PRIORITY */
786 + nla_total_size(4) /* FRA_TABLE */
Stefan Tomanek73f56982013-08-03 14:14:43 +0200787 + nla_total_size(4) /* FRA_SUPPRESS_PREFIXLEN */
Stefan Tomanek6ef94cf2013-08-02 17:19:56 +0200788 + nla_total_size(4) /* FRA_SUPPRESS_IFGROUP */
Thomas Graf339bf982006-11-10 14:10:15 -0800789 + nla_total_size(4) /* FRA_FWMARK */
Thomas Grafe7030872015-07-21 10:44:01 +0200790 + nla_total_size(4) /* FRA_FWMASK */
Lorenzo Colitti622ec2c2016-11-04 02:23:42 +0900791 + nla_total_size_64bit(8) /* FRA_TUN_ID */
Donald Sharp1b71af62018-02-23 14:01:52 -0500792 + nla_total_size(sizeof(struct fib_kuid_range))
793 + nla_total_size(1); /* FRA_PROTOCOL */
Thomas Graf339bf982006-11-10 14:10:15 -0800794
795 if (ops->nlmsg_payload)
796 payload += ops->nlmsg_payload(rule);
797
798 return payload;
799}
800
Thomas Graf14c0b972006-08-04 03:38:38 -0700801static int fib_nl_fill_rule(struct sk_buff *skb, struct fib_rule *rule,
802 u32 pid, u32 seq, int type, int flags,
803 struct fib_rules_ops *ops)
804{
805 struct nlmsghdr *nlh;
806 struct fib_rule_hdr *frh;
807
808 nlh = nlmsg_put(skb, pid, seq, type, sizeof(*frh), flags);
809 if (nlh == NULL)
Patrick McHardy26932562007-01-31 23:16:40 -0800810 return -EMSGSIZE;
Thomas Graf14c0b972006-08-04 03:38:38 -0700811
812 frh = nlmsg_data(nlh);
Patrick McHardy28bb1722010-04-13 05:03:16 +0000813 frh->family = ops->family;
Thomas Graf14c0b972006-08-04 03:38:38 -0700814 frh->table = rule->table;
David S. Miller0e3cea72012-04-01 20:47:01 -0400815 if (nla_put_u32(skb, FRA_TABLE, rule->table))
816 goto nla_put_failure;
Stefan Tomanek73f56982013-08-03 14:14:43 +0200817 if (nla_put_u32(skb, FRA_SUPPRESS_PREFIXLEN, rule->suppress_prefixlen))
Stefan Tomanek7764a452013-08-01 02:17:15 +0200818 goto nla_put_failure;
Thomas Graf14c0b972006-08-04 03:38:38 -0700819 frh->res1 = 0;
Donald Sharp1b71af62018-02-23 14:01:52 -0500820 frh->res2 = 0;
Thomas Graf14c0b972006-08-04 03:38:38 -0700821 frh->action = rule->action;
822 frh->flags = rule->flags;
Donald Sharp1b71af62018-02-23 14:01:52 -0500823
824 if (nla_put_u8(skb, FRA_PROTOCOL, rule->proto))
825 goto nla_put_failure;
Thomas Graf14c0b972006-08-04 03:38:38 -0700826
Eric Dumazet7a2b03c2010-10-26 09:24:55 +0000827 if (rule->action == FR_ACT_GOTO &&
Eric Dumazet33d480c2011-08-11 19:30:52 +0000828 rcu_access_pointer(rule->ctarget) == NULL)
Thomas Graf0947c9fe2007-03-26 17:14:15 -0700829 frh->flags |= FIB_RULE_UNRESOLVED;
830
Patrick McHardy491deb22009-12-03 01:25:54 +0000831 if (rule->iifname[0]) {
David S. Miller0e3cea72012-04-01 20:47:01 -0400832 if (nla_put_string(skb, FRA_IIFNAME, rule->iifname))
833 goto nla_put_failure;
Patrick McHardy491deb22009-12-03 01:25:54 +0000834 if (rule->iifindex == -1)
835 frh->flags |= FIB_RULE_IIF_DETACHED;
Thomas Graf2b443682007-03-26 17:37:59 -0700836 }
837
Patrick McHardy1b038a52009-12-03 01:25:56 +0000838 if (rule->oifname[0]) {
David S. Miller0e3cea72012-04-01 20:47:01 -0400839 if (nla_put_string(skb, FRA_OIFNAME, rule->oifname))
840 goto nla_put_failure;
Patrick McHardy1b038a52009-12-03 01:25:56 +0000841 if (rule->oifindex == -1)
842 frh->flags |= FIB_RULE_OIF_DETACHED;
843 }
844
David S. Miller0e3cea72012-04-01 20:47:01 -0400845 if ((rule->pref &&
846 nla_put_u32(skb, FRA_PRIORITY, rule->pref)) ||
847 (rule->mark &&
848 nla_put_u32(skb, FRA_FWMARK, rule->mark)) ||
849 ((rule->mark_mask || rule->mark) &&
850 nla_put_u32(skb, FRA_FWMASK, rule->mark_mask)) ||
851 (rule->target &&
Thomas Grafe7030872015-07-21 10:44:01 +0200852 nla_put_u32(skb, FRA_GOTO, rule->target)) ||
853 (rule->tun_id &&
David Ahern96c63fa2016-06-08 10:55:39 -0700854 nla_put_be64(skb, FRA_TUN_ID, rule->tun_id, FRA_PAD)) ||
855 (rule->l3mdev &&
Lorenzo Colitti622ec2c2016-11-04 02:23:42 +0900856 nla_put_u8(skb, FRA_L3MDEV, rule->l3mdev)) ||
857 (uid_range_set(&rule->uid_range) &&
858 nla_put_uid_range(skb, &rule->uid_range)))
David S. Miller0e3cea72012-04-01 20:47:01 -0400859 goto nla_put_failure;
Stefan Tomanek6ef94cf2013-08-02 17:19:56 +0200860
861 if (rule->suppress_ifgroup != -1) {
862 if (nla_put_u32(skb, FRA_SUPPRESS_IFGROUP, rule->suppress_ifgroup))
863 goto nla_put_failure;
864 }
865
Rami Rosen04af8cf2009-05-20 17:26:23 -0700866 if (ops->fill(rule, skb, frh) < 0)
Thomas Graf14c0b972006-08-04 03:38:38 -0700867 goto nla_put_failure;
868
Johannes Berg053c0952015-01-16 22:09:00 +0100869 nlmsg_end(skb, nlh);
870 return 0;
Thomas Graf14c0b972006-08-04 03:38:38 -0700871
872nla_put_failure:
Patrick McHardy26932562007-01-31 23:16:40 -0800873 nlmsg_cancel(skb, nlh);
874 return -EMSGSIZE;
Thomas Graf14c0b972006-08-04 03:38:38 -0700875}
876
Thomas Grafc4546732007-03-25 23:24:24 -0700877static int dump_rules(struct sk_buff *skb, struct netlink_callback *cb,
878 struct fib_rules_ops *ops)
Thomas Graf14c0b972006-08-04 03:38:38 -0700879{
880 int idx = 0;
881 struct fib_rule *rule;
Wilson Kok41fc0142015-09-22 21:40:22 -0700882 int err = 0;
Thomas Graf14c0b972006-08-04 03:38:38 -0700883
Eric Dumazete67f88d2011-04-27 22:56:07 +0000884 rcu_read_lock();
885 list_for_each_entry_rcu(rule, &ops->rules_list, list) {
Thomas Grafc4546732007-03-25 23:24:24 -0700886 if (idx < cb->args[1])
Thomas Graf14c0b972006-08-04 03:38:38 -0700887 goto skip;
888
Wilson Kok41fc0142015-09-22 21:40:22 -0700889 err = fib_nl_fill_rule(skb, rule, NETLINK_CB(cb->skb).portid,
890 cb->nlh->nlmsg_seq, RTM_NEWRULE,
891 NLM_F_MULTI, ops);
892 if (err)
Thomas Graf14c0b972006-08-04 03:38:38 -0700893 break;
894skip:
895 idx++;
896 }
Eric Dumazet2907c352011-05-25 07:34:04 +0000897 rcu_read_unlock();
Thomas Grafc4546732007-03-25 23:24:24 -0700898 cb->args[1] = idx;
Thomas Graf14c0b972006-08-04 03:38:38 -0700899 rules_ops_put(ops);
900
Wilson Kok41fc0142015-09-22 21:40:22 -0700901 return err;
Thomas Graf14c0b972006-08-04 03:38:38 -0700902}
903
Thomas Grafc4546732007-03-25 23:24:24 -0700904static int fib_nl_dumprule(struct sk_buff *skb, struct netlink_callback *cb)
905{
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +0900906 struct net *net = sock_net(skb->sk);
Thomas Grafc4546732007-03-25 23:24:24 -0700907 struct fib_rules_ops *ops;
908 int idx = 0, family;
909
910 family = rtnl_msg_family(cb->nlh);
911 if (family != AF_UNSPEC) {
912 /* Protocol specific dump request */
Denis V. Lunev5fd30ee2008-01-10 03:20:28 -0800913 ops = lookup_rules_ops(net, family);
Thomas Grafc4546732007-03-25 23:24:24 -0700914 if (ops == NULL)
915 return -EAFNOSUPPORT;
916
Wilson Kok41fc0142015-09-22 21:40:22 -0700917 dump_rules(skb, cb, ops);
918
919 return skb->len;
Thomas Grafc4546732007-03-25 23:24:24 -0700920 }
921
922 rcu_read_lock();
Denis V. Lunev5fd30ee2008-01-10 03:20:28 -0800923 list_for_each_entry_rcu(ops, &net->rules_ops, list) {
Thomas Grafc4546732007-03-25 23:24:24 -0700924 if (idx < cb->args[0] || !try_module_get(ops->owner))
925 goto skip;
926
927 if (dump_rules(skb, cb, ops) < 0)
928 break;
929
930 cb->args[1] = 0;
Eric Dumazet2fb35732010-03-09 20:03:38 +0000931skip:
Thomas Grafc4546732007-03-25 23:24:24 -0700932 idx++;
933 }
934 rcu_read_unlock();
935 cb->args[0] = idx;
936
937 return skb->len;
938}
Thomas Graf14c0b972006-08-04 03:38:38 -0700939
Denis V. Lunev9e3a5482008-01-20 16:46:41 -0800940static void notify_rule_change(int event, struct fib_rule *rule,
Thomas Grafc17084d2006-08-15 00:32:48 -0700941 struct fib_rules_ops *ops, struct nlmsghdr *nlh,
942 u32 pid)
Thomas Graf14c0b972006-08-04 03:38:38 -0700943{
Denis V. Lunev9e3a5482008-01-20 16:46:41 -0800944 struct net *net;
Thomas Grafc17084d2006-08-15 00:32:48 -0700945 struct sk_buff *skb;
946 int err = -ENOBUFS;
Thomas Graf14c0b972006-08-04 03:38:38 -0700947
Denis V. Lunev9e3a5482008-01-20 16:46:41 -0800948 net = ops->fro_net;
Thomas Graf339bf982006-11-10 14:10:15 -0800949 skb = nlmsg_new(fib_rule_nlmsg_size(ops, rule), GFP_KERNEL);
Thomas Graf14c0b972006-08-04 03:38:38 -0700950 if (skb == NULL)
Thomas Grafc17084d2006-08-15 00:32:48 -0700951 goto errout;
952
953 err = fib_nl_fill_rule(skb, rule, pid, nlh->nlmsg_seq, event, 0, ops);
Patrick McHardy26932562007-01-31 23:16:40 -0800954 if (err < 0) {
955 /* -EMSGSIZE implies BUG in fib_rule_nlmsg_size() */
956 WARN_ON(err == -EMSGSIZE);
957 kfree_skb(skb);
958 goto errout;
959 }
Denis V. Lunev9e3a5482008-01-20 16:46:41 -0800960
Pablo Neira Ayuso1ce85fe2009-02-24 23:18:28 -0800961 rtnl_notify(skb, net, pid, ops->nlgroup, nlh, GFP_KERNEL);
962 return;
Thomas Grafc17084d2006-08-15 00:32:48 -0700963errout:
964 if (err < 0)
Denis V. Lunev5fd30ee2008-01-10 03:20:28 -0800965 rtnl_set_sk_err(net, ops->nlgroup, err);
Thomas Graf14c0b972006-08-04 03:38:38 -0700966}
967
968static void attach_rules(struct list_head *rules, struct net_device *dev)
969{
970 struct fib_rule *rule;
971
972 list_for_each_entry(rule, rules, list) {
Patrick McHardy491deb22009-12-03 01:25:54 +0000973 if (rule->iifindex == -1 &&
974 strcmp(dev->name, rule->iifname) == 0)
975 rule->iifindex = dev->ifindex;
Patrick McHardy1b038a52009-12-03 01:25:56 +0000976 if (rule->oifindex == -1 &&
977 strcmp(dev->name, rule->oifname) == 0)
978 rule->oifindex = dev->ifindex;
Thomas Graf14c0b972006-08-04 03:38:38 -0700979 }
980}
981
982static void detach_rules(struct list_head *rules, struct net_device *dev)
983{
984 struct fib_rule *rule;
985
Patrick McHardy1b038a52009-12-03 01:25:56 +0000986 list_for_each_entry(rule, rules, list) {
Patrick McHardy491deb22009-12-03 01:25:54 +0000987 if (rule->iifindex == dev->ifindex)
988 rule->iifindex = -1;
Patrick McHardy1b038a52009-12-03 01:25:56 +0000989 if (rule->oifindex == dev->ifindex)
990 rule->oifindex = -1;
991 }
Thomas Graf14c0b972006-08-04 03:38:38 -0700992}
993
994
995static int fib_rules_event(struct notifier_block *this, unsigned long event,
Jiri Pirko351638e2013-05-28 01:30:21 +0000996 void *ptr)
Thomas Graf14c0b972006-08-04 03:38:38 -0700997{
Jiri Pirko351638e2013-05-28 01:30:21 +0000998 struct net_device *dev = netdev_notifier_info_to_dev(ptr);
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +0900999 struct net *net = dev_net(dev);
Thomas Graf14c0b972006-08-04 03:38:38 -07001000 struct fib_rules_ops *ops;
1001
Eric Dumazet748e2d92012-08-22 21:50:59 +00001002 ASSERT_RTNL();
Thomas Graf14c0b972006-08-04 03:38:38 -07001003
1004 switch (event) {
1005 case NETDEV_REGISTER:
Denis V. Lunev5fd30ee2008-01-10 03:20:28 -08001006 list_for_each_entry(ops, &net->rules_ops, list)
Denis V. Lunev76c72d42007-09-16 15:44:27 -07001007 attach_rules(&ops->rules_list, dev);
Thomas Graf14c0b972006-08-04 03:38:38 -07001008 break;
1009
Maciej Żenczykowski946c0322014-02-07 16:23:48 -08001010 case NETDEV_CHANGENAME:
1011 list_for_each_entry(ops, &net->rules_ops, list) {
1012 detach_rules(&ops->rules_list, dev);
1013 attach_rules(&ops->rules_list, dev);
1014 }
1015 break;
1016
Thomas Graf14c0b972006-08-04 03:38:38 -07001017 case NETDEV_UNREGISTER:
Denis V. Lunev5fd30ee2008-01-10 03:20:28 -08001018 list_for_each_entry(ops, &net->rules_ops, list)
Denis V. Lunev76c72d42007-09-16 15:44:27 -07001019 detach_rules(&ops->rules_list, dev);
Thomas Graf14c0b972006-08-04 03:38:38 -07001020 break;
1021 }
1022
Thomas Graf14c0b972006-08-04 03:38:38 -07001023 return NOTIFY_DONE;
1024}
1025
1026static struct notifier_block fib_rules_notifier = {
1027 .notifier_call = fib_rules_event,
1028};
1029
Alexey Dobriyan2c8c1e72010-01-17 03:35:32 +00001030static int __net_init fib_rules_net_init(struct net *net)
Denis V. Lunev5fd30ee2008-01-10 03:20:28 -08001031{
1032 INIT_LIST_HEAD(&net->rules_ops);
1033 spin_lock_init(&net->rules_mod_lock);
1034 return 0;
1035}
1036
Vasily Averince2b7db2017-11-12 22:30:01 +03001037static void __net_exit fib_rules_net_exit(struct net *net)
1038{
1039 WARN_ON_ONCE(!list_empty(&net->rules_ops));
1040}
1041
Denis V. Lunev5fd30ee2008-01-10 03:20:28 -08001042static struct pernet_operations fib_rules_net_ops = {
1043 .init = fib_rules_net_init,
Vasily Averince2b7db2017-11-12 22:30:01 +03001044 .exit = fib_rules_net_exit,
Kirill Tkhai86b63412018-02-13 12:29:03 +03001045 .async = true,
Denis V. Lunev5fd30ee2008-01-10 03:20:28 -08001046};
1047
Thomas Graf14c0b972006-08-04 03:38:38 -07001048static int __init fib_rules_init(void)
1049{
Denis V. Lunev5fd30ee2008-01-10 03:20:28 -08001050 int err;
Florian Westphalb97bac62017-08-09 20:41:48 +02001051 rtnl_register(PF_UNSPEC, RTM_NEWRULE, fib_nl_newrule, NULL, 0);
1052 rtnl_register(PF_UNSPEC, RTM_DELRULE, fib_nl_delrule, NULL, 0);
1053 rtnl_register(PF_UNSPEC, RTM_GETRULE, NULL, fib_nl_dumprule, 0);
Thomas Graf9d9e6a52007-03-25 23:20:05 -07001054
Eric W. Biederman5d6d4802008-11-07 22:52:34 -08001055 err = register_pernet_subsys(&fib_rules_net_ops);
Denis V. Lunev5fd30ee2008-01-10 03:20:28 -08001056 if (err < 0)
1057 goto fail;
1058
Eric W. Biederman5d6d4802008-11-07 22:52:34 -08001059 err = register_netdevice_notifier(&fib_rules_notifier);
Denis V. Lunev5fd30ee2008-01-10 03:20:28 -08001060 if (err < 0)
1061 goto fail_unregister;
Eric W. Biederman5d6d4802008-11-07 22:52:34 -08001062
Denis V. Lunev5fd30ee2008-01-10 03:20:28 -08001063 return 0;
1064
1065fail_unregister:
Eric W. Biederman5d6d4802008-11-07 22:52:34 -08001066 unregister_pernet_subsys(&fib_rules_net_ops);
Denis V. Lunev5fd30ee2008-01-10 03:20:28 -08001067fail:
1068 rtnl_unregister(PF_UNSPEC, RTM_NEWRULE);
1069 rtnl_unregister(PF_UNSPEC, RTM_DELRULE);
1070 rtnl_unregister(PF_UNSPEC, RTM_GETRULE);
1071 return err;
Thomas Graf14c0b972006-08-04 03:38:38 -07001072}
1073
1074subsys_initcall(fib_rules_init);