blob: bc8425d810220558e8457ada83ea3e356c3ceffa [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;
Roopa Prabhubfff4862018-02-28 22:40:16 -050036 if (fib_rule_port_range_set(&rule->sport_range))
37 return false;
38 if (fib_rule_port_range_set(&rule->dport_range))
39 return false;
Ido Schimmel3c710062017-03-16 09:08:12 +010040 return true;
41}
42EXPORT_SYMBOL_GPL(fib_rule_matchall);
43
Denis V. Lunev2994c632007-11-10 22:12:03 -080044int fib_default_rule_add(struct fib_rules_ops *ops,
45 u32 pref, u32 table, u32 flags)
46{
47 struct fib_rule *r;
48
49 r = kzalloc(ops->rule_size, GFP_KERNEL);
50 if (r == NULL)
51 return -ENOMEM;
52
Reshetova, Elena717d1e92017-06-30 13:08:06 +030053 refcount_set(&r->refcnt, 1);
Denis V. Lunev2994c632007-11-10 22:12:03 -080054 r->action = FR_ACT_TO_TBL;
55 r->pref = pref;
56 r->table = table;
57 r->flags = flags;
Donald Sharpcac56202018-02-20 08:55:58 -050058 r->proto = RTPROT_KERNEL;
Eric W. Biedermanefd7ef12015-03-11 23:04:08 -050059 r->fr_net = ops->fro_net;
Lorenzo Colitti622ec2c2016-11-04 02:23:42 +090060 r->uid_range = fib_kuid_range_unset;
Denis V. Lunev2994c632007-11-10 22:12:03 -080061
Stefan Tomanek73f56982013-08-03 14:14:43 +020062 r->suppress_prefixlen = -1;
63 r->suppress_ifgroup = -1;
64
Denis V. Lunev2994c632007-11-10 22:12:03 -080065 /* The lock is not required here, the list in unreacheable
66 * at the moment this function is called */
67 list_add_tail(&r->list, &ops->rules_list);
68 return 0;
69}
70EXPORT_SYMBOL(fib_default_rule_add);
71
Phil Sutterf53de1e2015-09-09 14:20:56 +020072static u32 fib_default_rule_pref(struct fib_rules_ops *ops)
Patrick McHardyd8a566b2010-04-13 05:03:15 +000073{
74 struct list_head *pos;
75 struct fib_rule *rule;
76
77 if (!list_empty(&ops->rules_list)) {
78 pos = ops->rules_list.next;
79 if (pos->next != &ops->rules_list) {
80 rule = list_entry(pos->next, struct fib_rule, list);
81 if (rule->pref)
82 return rule->pref - 1;
83 }
84 }
85
86 return 0;
87}
Patrick McHardyd8a566b2010-04-13 05:03:15 +000088
Denis V. Lunev9e3a5482008-01-20 16:46:41 -080089static void notify_rule_change(int event, struct fib_rule *rule,
Thomas Grafc17084d2006-08-15 00:32:48 -070090 struct fib_rules_ops *ops, struct nlmsghdr *nlh,
91 u32 pid);
Thomas Graf14c0b972006-08-04 03:38:38 -070092
Denis V. Lunev5fd30ee2008-01-10 03:20:28 -080093static struct fib_rules_ops *lookup_rules_ops(struct net *net, int family)
Thomas Graf14c0b972006-08-04 03:38:38 -070094{
95 struct fib_rules_ops *ops;
96
97 rcu_read_lock();
Denis V. Lunev5fd30ee2008-01-10 03:20:28 -080098 list_for_each_entry_rcu(ops, &net->rules_ops, list) {
Thomas Graf14c0b972006-08-04 03:38:38 -070099 if (ops->family == family) {
100 if (!try_module_get(ops->owner))
101 ops = NULL;
102 rcu_read_unlock();
103 return ops;
104 }
105 }
106 rcu_read_unlock();
107
108 return NULL;
109}
110
111static void rules_ops_put(struct fib_rules_ops *ops)
112{
113 if (ops)
114 module_put(ops->owner);
115}
116
Thomas Graf73417f62007-03-27 13:56:52 -0700117static void flush_route_cache(struct fib_rules_ops *ops)
118{
119 if (ops->flush_cache)
Denis V. Lunevae299fc2008-07-05 19:01:28 -0700120 ops->flush_cache(ops);
Thomas Graf73417f62007-03-27 13:56:52 -0700121}
122
Eric W. Biedermane9c51582009-12-03 12:22:55 -0800123static int __fib_rules_register(struct fib_rules_ops *ops)
Thomas Graf14c0b972006-08-04 03:38:38 -0700124{
125 int err = -EEXIST;
126 struct fib_rules_ops *o;
Denis V. Lunev9e3a5482008-01-20 16:46:41 -0800127 struct net *net;
128
129 net = ops->fro_net;
Thomas Graf14c0b972006-08-04 03:38:38 -0700130
131 if (ops->rule_size < sizeof(struct fib_rule))
132 return -EINVAL;
133
134 if (ops->match == NULL || ops->configure == NULL ||
135 ops->compare == NULL || ops->fill == NULL ||
136 ops->action == NULL)
137 return -EINVAL;
138
Denis V. Lunev5fd30ee2008-01-10 03:20:28 -0800139 spin_lock(&net->rules_mod_lock);
140 list_for_each_entry(o, &net->rules_ops, list)
Thomas Graf14c0b972006-08-04 03:38:38 -0700141 if (ops->family == o->family)
142 goto errout;
143
Denis V. Lunev5fd30ee2008-01-10 03:20:28 -0800144 list_add_tail_rcu(&ops->list, &net->rules_ops);
Thomas Graf14c0b972006-08-04 03:38:38 -0700145 err = 0;
146errout:
Denis V. Lunev5fd30ee2008-01-10 03:20:28 -0800147 spin_unlock(&net->rules_mod_lock);
Thomas Graf14c0b972006-08-04 03:38:38 -0700148
149 return err;
150}
151
Eric W. Biedermane9c51582009-12-03 12:22:55 -0800152struct fib_rules_ops *
Patrick McHardy3d0c9c42010-04-26 16:02:04 +0200153fib_rules_register(const struct fib_rules_ops *tmpl, struct net *net)
Eric W. Biedermane9c51582009-12-03 12:22:55 -0800154{
155 struct fib_rules_ops *ops;
156 int err;
157
Eric Dumazet2fb35732010-03-09 20:03:38 +0000158 ops = kmemdup(tmpl, sizeof(*ops), GFP_KERNEL);
Eric W. Biedermane9c51582009-12-03 12:22:55 -0800159 if (ops == NULL)
160 return ERR_PTR(-ENOMEM);
161
162 INIT_LIST_HEAD(&ops->rules_list);
163 ops->fro_net = net;
164
165 err = __fib_rules_register(ops);
166 if (err) {
167 kfree(ops);
168 ops = ERR_PTR(err);
169 }
170
171 return ops;
172}
Thomas Graf14c0b972006-08-04 03:38:38 -0700173EXPORT_SYMBOL_GPL(fib_rules_register);
174
stephen hemminger1df99162010-10-04 20:14:17 +0000175static void fib_rules_cleanup_ops(struct fib_rules_ops *ops)
Thomas Graf14c0b972006-08-04 03:38:38 -0700176{
177 struct fib_rule *rule, *tmp;
178
Denis V. Lunev76c72d42007-09-16 15:44:27 -0700179 list_for_each_entry_safe(rule, tmp, &ops->rules_list, list) {
Thomas Graf14c0b972006-08-04 03:38:38 -0700180 list_del_rcu(&rule->list);
David S. Miller7a9bc9b2012-06-29 01:32:45 -0700181 if (ops->delete)
182 ops->delete(rule);
Thomas Graf14c0b972006-08-04 03:38:38 -0700183 fib_rule_put(rule);
184 }
185}
186
Denis V. Lunev9e3a5482008-01-20 16:46:41 -0800187void fib_rules_unregister(struct fib_rules_ops *ops)
Thomas Graf14c0b972006-08-04 03:38:38 -0700188{
Denis V. Lunev9e3a5482008-01-20 16:46:41 -0800189 struct net *net = ops->fro_net;
Thomas Graf14c0b972006-08-04 03:38:38 -0700190
Denis V. Lunev5fd30ee2008-01-10 03:20:28 -0800191 spin_lock(&net->rules_mod_lock);
Denis V. Lunev72132c1b2008-01-14 22:59:30 -0800192 list_del_rcu(&ops->list);
Denis V. Lunev5fd30ee2008-01-10 03:20:28 -0800193 spin_unlock(&net->rules_mod_lock);
Thomas Graf14c0b972006-08-04 03:38:38 -0700194
WANG Cong419df122015-03-31 11:01:46 -0700195 fib_rules_cleanup_ops(ops);
Eric W. Biedermanefd7ef12015-03-11 23:04:08 -0500196 kfree_rcu(ops, rcu);
Thomas Graf14c0b972006-08-04 03:38:38 -0700197}
Thomas Graf14c0b972006-08-04 03:38:38 -0700198EXPORT_SYMBOL_GPL(fib_rules_unregister);
199
Lorenzo Colitti622ec2c2016-11-04 02:23:42 +0900200static int uid_range_set(struct fib_kuid_range *range)
201{
202 return uid_valid(range->start) && uid_valid(range->end);
203}
204
205static struct fib_kuid_range nla_get_kuid_range(struct nlattr **tb)
206{
207 struct fib_rule_uid_range *in;
208 struct fib_kuid_range out;
209
210 in = (struct fib_rule_uid_range *)nla_data(tb[FRA_UID_RANGE]);
211
212 out.start = make_kuid(current_user_ns(), in->start);
213 out.end = make_kuid(current_user_ns(), in->end);
214
215 return out;
216}
217
218static int nla_put_uid_range(struct sk_buff *skb, struct fib_kuid_range *range)
219{
220 struct fib_rule_uid_range out = {
221 from_kuid_munged(current_user_ns(), range->start),
222 from_kuid_munged(current_user_ns(), range->end)
223 };
224
225 return nla_put(skb, FRA_UID_RANGE, sizeof(out), &out);
226}
227
Roopa Prabhubfff4862018-02-28 22:40:16 -0500228static int nla_get_port_range(struct nlattr *pattr,
229 struct fib_rule_port_range *port_range)
230{
231 const struct fib_rule_port_range *pr = nla_data(pattr);
232
233 if (!fib_rule_port_range_valid(pr))
234 return -EINVAL;
235
236 port_range->start = pr->start;
237 port_range->end = pr->end;
238
239 return 0;
240}
241
242static int nla_put_port_range(struct sk_buff *skb, int attrtype,
243 struct fib_rule_port_range *range)
244{
245 return nla_put(skb, attrtype, sizeof(*range), range);
246}
247
Thomas Graf3dfbcc42006-11-09 15:23:20 -0800248static int fib_rule_match(struct fib_rule *rule, struct fib_rules_ops *ops,
David Ahern96c63fa2016-06-08 10:55:39 -0700249 struct flowi *fl, int flags,
250 struct fib_lookup_arg *arg)
Thomas Graf3dfbcc42006-11-09 15:23:20 -0800251{
252 int ret = 0;
253
David S. Miller1d28f422011-03-12 00:29:39 -0500254 if (rule->iifindex && (rule->iifindex != fl->flowi_iif))
Thomas Graf3dfbcc42006-11-09 15:23:20 -0800255 goto out;
256
David S. Miller1d28f422011-03-12 00:29:39 -0500257 if (rule->oifindex && (rule->oifindex != fl->flowi_oif))
Patrick McHardy1b038a52009-12-03 01:25:56 +0000258 goto out;
259
David S. Miller1d28f422011-03-12 00:29:39 -0500260 if ((rule->mark ^ fl->flowi_mark) & rule->mark_mask)
Thomas Graf3dfbcc42006-11-09 15:23:20 -0800261 goto out;
262
Thomas Grafe7030872015-07-21 10:44:01 +0200263 if (rule->tun_id && (rule->tun_id != fl->flowi_tun_key.tun_id))
264 goto out;
265
David Ahern96c63fa2016-06-08 10:55:39 -0700266 if (rule->l3mdev && !l3mdev_fib_rule_match(rule->fr_net, fl, arg))
267 goto out;
268
Lorenzo Colitti622ec2c2016-11-04 02:23:42 +0900269 if (uid_lt(fl->flowi_uid, rule->uid_range.start) ||
270 uid_gt(fl->flowi_uid, rule->uid_range.end))
271 goto out;
272
Thomas Graf3dfbcc42006-11-09 15:23:20 -0800273 ret = ops->match(rule, fl, flags);
274out:
275 return (rule->flags & FIB_RULE_INVERT) ? !ret : ret;
276}
277
Thomas Graf14c0b972006-08-04 03:38:38 -0700278int fib_rules_lookup(struct fib_rules_ops *ops, struct flowi *fl,
279 int flags, struct fib_lookup_arg *arg)
280{
281 struct fib_rule *rule;
282 int err;
283
284 rcu_read_lock();
285
Denis V. Lunev76c72d42007-09-16 15:44:27 -0700286 list_for_each_entry_rcu(rule, &ops->rules_list, list) {
Thomas Graf0947c9fe2007-03-26 17:14:15 -0700287jumped:
David Ahern96c63fa2016-06-08 10:55:39 -0700288 if (!fib_rule_match(rule, ops, fl, flags, arg))
Thomas Graf14c0b972006-08-04 03:38:38 -0700289 continue;
290
Thomas Graf0947c9fe2007-03-26 17:14:15 -0700291 if (rule->action == FR_ACT_GOTO) {
292 struct fib_rule *target;
293
294 target = rcu_dereference(rule->ctarget);
295 if (target == NULL) {
296 continue;
297 } else {
298 rule = target;
299 goto jumped;
300 }
Thomas Graffa0b2d12007-03-26 17:38:53 -0700301 } else if (rule->action == FR_ACT_NOP)
302 continue;
303 else
Thomas Graf0947c9fe2007-03-26 17:14:15 -0700304 err = ops->action(rule, fl, flags, arg);
305
Stefan Tomanek7764a452013-08-01 02:17:15 +0200306 if (!err && ops->suppress && ops->suppress(rule, arg))
307 continue;
308
Thomas Graf14c0b972006-08-04 03:38:38 -0700309 if (err != -EAGAIN) {
Eric Dumazetebc0ffa2010-10-05 10:41:36 +0000310 if ((arg->flags & FIB_LOOKUP_NOREF) ||
Reshetova, Elena717d1e92017-06-30 13:08:06 +0300311 likely(refcount_inc_not_zero(&rule->refcnt))) {
Eric Dumazet7fa7cb72010-09-27 04:18:27 +0000312 arg->rule = rule;
313 goto out;
314 }
315 break;
Thomas Graf14c0b972006-08-04 03:38:38 -0700316 }
317 }
318
Steven Whitehouse83886b62007-03-30 13:34:27 -0700319 err = -ESRCH;
Thomas Graf14c0b972006-08-04 03:38:38 -0700320out:
321 rcu_read_unlock();
322
323 return err;
324}
Thomas Graf14c0b972006-08-04 03:38:38 -0700325EXPORT_SYMBOL_GPL(fib_rules_lookup);
326
Ido Schimmel1b2a4442017-08-03 13:28:14 +0200327static int call_fib_rule_notifier(struct notifier_block *nb, struct net *net,
328 enum fib_event_type event_type,
329 struct fib_rule *rule, int family)
330{
331 struct fib_rule_notifier_info info = {
332 .info.family = family,
333 .rule = rule,
334 };
335
336 return call_fib_notifier(nb, net, event_type, &info.info);
337}
338
339static int call_fib_rule_notifiers(struct net *net,
340 enum fib_event_type event_type,
341 struct fib_rule *rule,
David Ahern6c31e5a2017-10-27 17:37:13 -0700342 struct fib_rules_ops *ops,
343 struct netlink_ext_ack *extack)
Ido Schimmel1b2a4442017-08-03 13:28:14 +0200344{
345 struct fib_rule_notifier_info info = {
346 .info.family = ops->family,
David Ahern6c31e5a2017-10-27 17:37:13 -0700347 .info.extack = extack,
Ido Schimmel1b2a4442017-08-03 13:28:14 +0200348 .rule = rule,
349 };
350
351 ops->fib_rules_seq++;
352 return call_fib_notifiers(net, event_type, &info.info);
353}
354
355/* Called with rcu_read_lock() */
356int fib_rules_dump(struct net *net, struct notifier_block *nb, int family)
357{
358 struct fib_rules_ops *ops;
359 struct fib_rule *rule;
360
361 ops = lookup_rules_ops(net, family);
362 if (!ops)
363 return -EAFNOSUPPORT;
364 list_for_each_entry_rcu(rule, &ops->rules_list, list)
365 call_fib_rule_notifier(nb, net, FIB_EVENT_RULE_ADD, rule,
366 family);
367 rules_ops_put(ops);
368
369 return 0;
370}
371EXPORT_SYMBOL_GPL(fib_rules_dump);
372
373unsigned int fib_rules_seq_read(struct net *net, int family)
374{
375 unsigned int fib_rules_seq;
376 struct fib_rules_ops *ops;
377
378 ASSERT_RTNL();
379
380 ops = lookup_rules_ops(net, family);
381 if (!ops)
382 return 0;
383 fib_rules_seq = ops->fib_rules_seq;
384 rules_ops_put(ops);
385
386 return fib_rules_seq;
387}
388EXPORT_SYMBOL_GPL(fib_rules_seq_read);
389
Roopa Prabhuf9d4b0c2018-04-21 09:41:30 -0700390static struct fib_rule *rule_find(struct fib_rules_ops *ops,
391 struct fib_rule_hdr *frh,
392 struct nlattr **tb,
393 struct fib_rule *rule,
394 bool user_priority)
Mateusz Bajorski153380e2016-06-29 09:22:10 +0200395{
396 struct fib_rule *r;
397
398 list_for_each_entry(r, &ops->rules_list, list) {
Roopa Prabhuf9d4b0c2018-04-21 09:41:30 -0700399 if (rule->action && r->action != rule->action)
Mateusz Bajorski153380e2016-06-29 09:22:10 +0200400 continue;
401
Roopa Prabhuf9d4b0c2018-04-21 09:41:30 -0700402 if (rule->table && r->table != rule->table)
Mateusz Bajorski153380e2016-06-29 09:22:10 +0200403 continue;
404
Roopa Prabhuf9d4b0c2018-04-21 09:41:30 -0700405 if (user_priority && r->pref != rule->pref)
Mateusz Bajorski153380e2016-06-29 09:22:10 +0200406 continue;
407
Roopa Prabhuf9d4b0c2018-04-21 09:41:30 -0700408 if (rule->iifname[0] &&
409 memcmp(r->iifname, rule->iifname, IFNAMSIZ))
Mateusz Bajorski153380e2016-06-29 09:22:10 +0200410 continue;
411
Roopa Prabhuf9d4b0c2018-04-21 09:41:30 -0700412 if (rule->oifname[0] &&
413 memcmp(r->oifname, rule->oifname, IFNAMSIZ))
Mateusz Bajorski153380e2016-06-29 09:22:10 +0200414 continue;
415
Roopa Prabhuf9d4b0c2018-04-21 09:41:30 -0700416 if (rule->mark && r->mark != rule->mark)
Mateusz Bajorski153380e2016-06-29 09:22:10 +0200417 continue;
418
Jason A. Donenfeld7c8f4e62018-06-26 01:39:32 +0200419 if (rule->suppress_ifgroup != -1 &&
420 r->suppress_ifgroup != rule->suppress_ifgroup)
421 continue;
422
423 if (rule->suppress_prefixlen != -1 &&
424 r->suppress_prefixlen != rule->suppress_prefixlen)
425 continue;
426
Roopa Prabhuf9d4b0c2018-04-21 09:41:30 -0700427 if (rule->mark_mask && r->mark_mask != rule->mark_mask)
Mateusz Bajorski153380e2016-06-29 09:22:10 +0200428 continue;
429
Roopa Prabhuf9d4b0c2018-04-21 09:41:30 -0700430 if (rule->tun_id && r->tun_id != rule->tun_id)
Mateusz Bajorski153380e2016-06-29 09:22:10 +0200431 continue;
432
433 if (r->fr_net != rule->fr_net)
434 continue;
435
Roopa Prabhuf9d4b0c2018-04-21 09:41:30 -0700436 if (rule->l3mdev && r->l3mdev != rule->l3mdev)
Mateusz Bajorski153380e2016-06-29 09:22:10 +0200437 continue;
438
Roopa Prabhuf9d4b0c2018-04-21 09:41:30 -0700439 if (uid_range_set(&rule->uid_range) &&
440 (!uid_eq(r->uid_range.start, rule->uid_range.start) ||
441 !uid_eq(r->uid_range.end, rule->uid_range.end)))
Lorenzo Colitti35b80732016-11-07 00:16:25 +0900442 continue;
443
Roopa Prabhuf9d4b0c2018-04-21 09:41:30 -0700444 if (rule->ip_proto && r->ip_proto != rule->ip_proto)
Roopa Prabhubfff4862018-02-28 22:40:16 -0500445 continue;
446
Roopa Prabhuf9d4b0c2018-04-21 09:41:30 -0700447 if (fib_rule_port_range_set(&rule->sport_range) &&
448 !fib_rule_port_range_compare(&r->sport_range,
Roopa Prabhubfff4862018-02-28 22:40:16 -0500449 &rule->sport_range))
450 continue;
451
Roopa Prabhuf9d4b0c2018-04-21 09:41:30 -0700452 if (fib_rule_port_range_set(&rule->dport_range) &&
453 !fib_rule_port_range_compare(&r->dport_range,
Roopa Prabhubfff4862018-02-28 22:40:16 -0500454 &rule->dport_range))
455 continue;
456
Mateusz Bajorski153380e2016-06-29 09:22:10 +0200457 if (!ops->compare(r, frh, tb))
458 continue;
Roopa Prabhuf9d4b0c2018-04-21 09:41:30 -0700459 return r;
Mateusz Bajorski153380e2016-06-29 09:22:10 +0200460 }
Roopa Prabhuf9d4b0c2018-04-21 09:41:30 -0700461
462 return NULL;
Mateusz Bajorski153380e2016-06-29 09:22:10 +0200463}
464
David Ahernc77bbc62018-04-24 18:36:07 -0700465#ifdef CONFIG_NET_L3_MASTER_DEV
466static int fib_nl2rule_l3mdev(struct nlattr *nla, struct fib_rule *nlrule,
467 struct netlink_ext_ack *extack)
468{
469 nlrule->l3mdev = nla_get_u8(nla);
470 if (nlrule->l3mdev != 1) {
471 NL_SET_ERR_MSG(extack, "Invalid l3mdev attribute");
472 return -1;
473 }
474
475 return 0;
476}
477#else
478static int fib_nl2rule_l3mdev(struct nlattr *nla, struct fib_rule *nlrule,
479 struct netlink_ext_ack *extack)
480{
481 NL_SET_ERR_MSG(extack, "l3mdev support is not enabled in kernel");
482 return -1;
483}
484#endif
485
Roopa Prabhuf9d4b0c2018-04-21 09:41:30 -0700486static int fib_nl2rule(struct sk_buff *skb, struct nlmsghdr *nlh,
487 struct netlink_ext_ack *extack,
488 struct fib_rules_ops *ops,
489 struct nlattr *tb[],
490 struct fib_rule **rule,
491 bool *user_priority)
Thomas Graf14c0b972006-08-04 03:38:38 -0700492{
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +0900493 struct net *net = sock_net(skb->sk);
Thomas Graf14c0b972006-08-04 03:38:38 -0700494 struct fib_rule_hdr *frh = nlmsg_data(nlh);
Roopa Prabhuf9d4b0c2018-04-21 09:41:30 -0700495 struct fib_rule *nlrule = NULL;
496 int err = -EINVAL;
Thomas Graf14c0b972006-08-04 03:38:38 -0700497
Roopa Prabhuf9d4b0c2018-04-21 09:41:30 -0700498 if (frh->src_len)
499 if (!tb[FRA_SRC] ||
500 frh->src_len > (ops->addr_size * 8) ||
Roopa Prabhub16fb412018-04-21 09:41:31 -0700501 nla_len(tb[FRA_SRC]) != ops->addr_size) {
502 NL_SET_ERR_MSG(extack, "Invalid source address");
Roopa Prabhuf9d4b0c2018-04-21 09:41:30 -0700503 goto errout;
Roopa Prabhub16fb412018-04-21 09:41:31 -0700504 }
Thomas Graf14c0b972006-08-04 03:38:38 -0700505
Roopa Prabhuf9d4b0c2018-04-21 09:41:30 -0700506 if (frh->dst_len)
507 if (!tb[FRA_DST] ||
508 frh->dst_len > (ops->addr_size * 8) ||
Roopa Prabhub16fb412018-04-21 09:41:31 -0700509 nla_len(tb[FRA_DST]) != ops->addr_size) {
510 NL_SET_ERR_MSG(extack, "Invalid dst address");
Roopa Prabhuf9d4b0c2018-04-21 09:41:30 -0700511 goto errout;
Roopa Prabhub16fb412018-04-21 09:41:31 -0700512 }
Thomas Graf14c0b972006-08-04 03:38:38 -0700513
Roopa Prabhuf9d4b0c2018-04-21 09:41:30 -0700514 nlrule = kzalloc(ops->rule_size, GFP_KERNEL);
515 if (!nlrule) {
Thomas Graf14c0b972006-08-04 03:38:38 -0700516 err = -ENOMEM;
517 goto errout;
518 }
Roopa Prabhuf9d4b0c2018-04-21 09:41:30 -0700519 refcount_set(&nlrule->refcnt, 1);
520 nlrule->fr_net = net;
Thomas Graf14c0b972006-08-04 03:38:38 -0700521
Roopa Prabhuf9d4b0c2018-04-21 09:41:30 -0700522 if (tb[FRA_PRIORITY]) {
523 nlrule->pref = nla_get_u32(tb[FRA_PRIORITY]);
524 *user_priority = true;
525 } else {
526 nlrule->pref = fib_default_rule_pref(ops);
527 }
Thomas Graf14c0b972006-08-04 03:38:38 -0700528
Roopa Prabhuf9d4b0c2018-04-21 09:41:30 -0700529 nlrule->proto = tb[FRA_PROTOCOL] ?
Donald Sharp1b71af62018-02-23 14:01:52 -0500530 nla_get_u8(tb[FRA_PROTOCOL]) : RTPROT_UNSPEC;
531
Patrick McHardy491deb22009-12-03 01:25:54 +0000532 if (tb[FRA_IIFNAME]) {
Thomas Graf14c0b972006-08-04 03:38:38 -0700533 struct net_device *dev;
534
Roopa Prabhuf9d4b0c2018-04-21 09:41:30 -0700535 nlrule->iifindex = -1;
536 nla_strlcpy(nlrule->iifname, tb[FRA_IIFNAME], IFNAMSIZ);
537 dev = __dev_get_by_name(net, nlrule->iifname);
Thomas Graf14c0b972006-08-04 03:38:38 -0700538 if (dev)
Roopa Prabhuf9d4b0c2018-04-21 09:41:30 -0700539 nlrule->iifindex = dev->ifindex;
Thomas Graf14c0b972006-08-04 03:38:38 -0700540 }
541
Patrick McHardy1b038a52009-12-03 01:25:56 +0000542 if (tb[FRA_OIFNAME]) {
543 struct net_device *dev;
544
Roopa Prabhuf9d4b0c2018-04-21 09:41:30 -0700545 nlrule->oifindex = -1;
546 nla_strlcpy(nlrule->oifname, tb[FRA_OIFNAME], IFNAMSIZ);
547 dev = __dev_get_by_name(net, nlrule->oifname);
Patrick McHardy1b038a52009-12-03 01:25:56 +0000548 if (dev)
Roopa Prabhuf9d4b0c2018-04-21 09:41:30 -0700549 nlrule->oifindex = dev->ifindex;
Patrick McHardy1b038a52009-12-03 01:25:56 +0000550 }
551
Thomas Grafb8964ed2006-11-09 15:22:18 -0800552 if (tb[FRA_FWMARK]) {
Roopa Prabhuf9d4b0c2018-04-21 09:41:30 -0700553 nlrule->mark = nla_get_u32(tb[FRA_FWMARK]);
554 if (nlrule->mark)
Thomas Grafb8964ed2006-11-09 15:22:18 -0800555 /* compatibility: if the mark value is non-zero all bits
556 * are compared unless a mask is explicitly specified.
557 */
Roopa Prabhuf9d4b0c2018-04-21 09:41:30 -0700558 nlrule->mark_mask = 0xFFFFFFFF;
Thomas Grafb8964ed2006-11-09 15:22:18 -0800559 }
560
561 if (tb[FRA_FWMASK])
Roopa Prabhuf9d4b0c2018-04-21 09:41:30 -0700562 nlrule->mark_mask = nla_get_u32(tb[FRA_FWMASK]);
Thomas Grafb8964ed2006-11-09 15:22:18 -0800563
Thomas Grafe7030872015-07-21 10:44:01 +0200564 if (tb[FRA_TUN_ID])
Roopa Prabhuf9d4b0c2018-04-21 09:41:30 -0700565 nlrule->tun_id = nla_get_be64(tb[FRA_TUN_ID]);
Thomas Grafe7030872015-07-21 10:44:01 +0200566
Wei Yongjunadeb45c2017-04-26 14:03:50 +0000567 err = -EINVAL;
David Ahernc77bbc62018-04-24 18:36:07 -0700568 if (tb[FRA_L3MDEV] &&
569 fib_nl2rule_l3mdev(tb[FRA_L3MDEV], nlrule, extack) < 0)
570 goto errout_free;
David Ahern96c63fa2016-06-08 10:55:39 -0700571
Roopa Prabhuf9d4b0c2018-04-21 09:41:30 -0700572 nlrule->action = frh->action;
573 nlrule->flags = frh->flags;
574 nlrule->table = frh_get_table(frh, tb);
Stefan Tomanek73f56982013-08-03 14:14:43 +0200575 if (tb[FRA_SUPPRESS_PREFIXLEN])
Roopa Prabhuf9d4b0c2018-04-21 09:41:30 -0700576 nlrule->suppress_prefixlen = nla_get_u32(tb[FRA_SUPPRESS_PREFIXLEN]);
Stefan Tomanek73f56982013-08-03 14:14:43 +0200577 else
Roopa Prabhuf9d4b0c2018-04-21 09:41:30 -0700578 nlrule->suppress_prefixlen = -1;
Thomas Graf14c0b972006-08-04 03:38:38 -0700579
Stefan Tomanek6ef94cf2013-08-02 17:19:56 +0200580 if (tb[FRA_SUPPRESS_IFGROUP])
Roopa Prabhuf9d4b0c2018-04-21 09:41:30 -0700581 nlrule->suppress_ifgroup = nla_get_u32(tb[FRA_SUPPRESS_IFGROUP]);
Stefan Tomanek73f56982013-08-03 14:14:43 +0200582 else
Roopa Prabhuf9d4b0c2018-04-21 09:41:30 -0700583 nlrule->suppress_ifgroup = -1;
Stefan Tomanek6ef94cf2013-08-02 17:19:56 +0200584
Thomas Graf0947c9fe2007-03-26 17:14:15 -0700585 if (tb[FRA_GOTO]) {
Roopa Prabhub16fb412018-04-21 09:41:31 -0700586 if (nlrule->action != FR_ACT_GOTO) {
587 NL_SET_ERR_MSG(extack, "Unexpected goto");
Thomas Graf0947c9fe2007-03-26 17:14:15 -0700588 goto errout_free;
Roopa Prabhub16fb412018-04-21 09:41:31 -0700589 }
Thomas Graf0947c9fe2007-03-26 17:14:15 -0700590
Roopa Prabhuf9d4b0c2018-04-21 09:41:30 -0700591 nlrule->target = nla_get_u32(tb[FRA_GOTO]);
Thomas Graf0947c9fe2007-03-26 17:14:15 -0700592 /* Backward jumps are prohibited to avoid endless loops */
Roopa Prabhub16fb412018-04-21 09:41:31 -0700593 if (nlrule->target <= nlrule->pref) {
594 NL_SET_ERR_MSG(extack, "Backward goto not supported");
Thomas Graf0947c9fe2007-03-26 17:14:15 -0700595 goto errout_free;
Roopa Prabhub16fb412018-04-21 09:41:31 -0700596 }
Roopa Prabhuf9d4b0c2018-04-21 09:41:30 -0700597 } else if (nlrule->action == FR_ACT_GOTO) {
Roopa Prabhub16fb412018-04-21 09:41:31 -0700598 NL_SET_ERR_MSG(extack, "Missing goto target for action goto");
Thomas Graf0947c9fe2007-03-26 17:14:15 -0700599 goto errout_free;
Roopa Prabhuf9d4b0c2018-04-21 09:41:30 -0700600 }
Thomas Graf0947c9fe2007-03-26 17:14:15 -0700601
Roopa Prabhub16fb412018-04-21 09:41:31 -0700602 if (nlrule->l3mdev && nlrule->table) {
603 NL_SET_ERR_MSG(extack, "l3mdev and table are mutually exclusive");
David Ahern96c63fa2016-06-08 10:55:39 -0700604 goto errout_free;
Roopa Prabhub16fb412018-04-21 09:41:31 -0700605 }
David Ahern96c63fa2016-06-08 10:55:39 -0700606
Lorenzo Colitti622ec2c2016-11-04 02:23:42 +0900607 if (tb[FRA_UID_RANGE]) {
608 if (current_user_ns() != net->user_ns) {
609 err = -EPERM;
Roopa Prabhub16fb412018-04-21 09:41:31 -0700610 NL_SET_ERR_MSG(extack, "No permission to set uid");
Lorenzo Colitti622ec2c2016-11-04 02:23:42 +0900611 goto errout_free;
612 }
613
Roopa Prabhuf9d4b0c2018-04-21 09:41:30 -0700614 nlrule->uid_range = nla_get_kuid_range(tb);
Lorenzo Colitti622ec2c2016-11-04 02:23:42 +0900615
Roopa Prabhuf9d4b0c2018-04-21 09:41:30 -0700616 if (!uid_range_set(&nlrule->uid_range) ||
Roopa Prabhub16fb412018-04-21 09:41:31 -0700617 !uid_lte(nlrule->uid_range.start, nlrule->uid_range.end)) {
618 NL_SET_ERR_MSG(extack, "Invalid uid range");
Lorenzo Colitti622ec2c2016-11-04 02:23:42 +0900619 goto errout_free;
Roopa Prabhub16fb412018-04-21 09:41:31 -0700620 }
Lorenzo Colitti622ec2c2016-11-04 02:23:42 +0900621 } else {
Roopa Prabhuf9d4b0c2018-04-21 09:41:30 -0700622 nlrule->uid_range = fib_kuid_range_unset;
Lorenzo Colitti622ec2c2016-11-04 02:23:42 +0900623 }
624
Roopa Prabhubfff4862018-02-28 22:40:16 -0500625 if (tb[FRA_IP_PROTO])
Roopa Prabhuf9d4b0c2018-04-21 09:41:30 -0700626 nlrule->ip_proto = nla_get_u8(tb[FRA_IP_PROTO]);
Roopa Prabhubfff4862018-02-28 22:40:16 -0500627
628 if (tb[FRA_SPORT_RANGE]) {
629 err = nla_get_port_range(tb[FRA_SPORT_RANGE],
Roopa Prabhuf9d4b0c2018-04-21 09:41:30 -0700630 &nlrule->sport_range);
Roopa Prabhub16fb412018-04-21 09:41:31 -0700631 if (err) {
632 NL_SET_ERR_MSG(extack, "Invalid sport range");
Roopa Prabhubfff4862018-02-28 22:40:16 -0500633 goto errout_free;
Roopa Prabhub16fb412018-04-21 09:41:31 -0700634 }
Roopa Prabhubfff4862018-02-28 22:40:16 -0500635 }
636
637 if (tb[FRA_DPORT_RANGE]) {
638 err = nla_get_port_range(tb[FRA_DPORT_RANGE],
Roopa Prabhuf9d4b0c2018-04-21 09:41:30 -0700639 &nlrule->dport_range);
Roopa Prabhub16fb412018-04-21 09:41:31 -0700640 if (err) {
641 NL_SET_ERR_MSG(extack, "Invalid dport range");
Roopa Prabhubfff4862018-02-28 22:40:16 -0500642 goto errout_free;
Roopa Prabhub16fb412018-04-21 09:41:31 -0700643 }
Roopa Prabhubfff4862018-02-28 22:40:16 -0500644 }
645
Roopa Prabhuf9d4b0c2018-04-21 09:41:30 -0700646 *rule = nlrule;
647
648 return 0;
649
650errout_free:
651 kfree(nlrule);
652errout:
653 return err;
654}
655
656int fib_nl_newrule(struct sk_buff *skb, struct nlmsghdr *nlh,
657 struct netlink_ext_ack *extack)
658{
659 struct net *net = sock_net(skb->sk);
660 struct fib_rule_hdr *frh = nlmsg_data(nlh);
661 struct fib_rules_ops *ops = NULL;
662 struct fib_rule *rule = NULL, *r, *last = NULL;
663 struct nlattr *tb[FRA_MAX + 1];
664 int err = -EINVAL, unresolved = 0;
665 bool user_priority = false;
666
Roopa Prabhub16fb412018-04-21 09:41:31 -0700667 if (nlh->nlmsg_len < nlmsg_msg_size(sizeof(*frh))) {
668 NL_SET_ERR_MSG(extack, "Invalid msg length");
Roopa Prabhuf9d4b0c2018-04-21 09:41:30 -0700669 goto errout;
Roopa Prabhub16fb412018-04-21 09:41:31 -0700670 }
Roopa Prabhuf9d4b0c2018-04-21 09:41:30 -0700671
672 ops = lookup_rules_ops(net, frh->family);
673 if (!ops) {
674 err = -EAFNOSUPPORT;
Roopa Prabhub16fb412018-04-21 09:41:31 -0700675 NL_SET_ERR_MSG(extack, "Rule family not supported");
Roopa Prabhuf9d4b0c2018-04-21 09:41:30 -0700676 goto errout;
677 }
678
679 err = nlmsg_parse(nlh, sizeof(*frh), tb, FRA_MAX, ops->policy, extack);
Roopa Prabhub16fb412018-04-21 09:41:31 -0700680 if (err < 0) {
681 NL_SET_ERR_MSG(extack, "Error parsing msg");
Roopa Prabhuf9d4b0c2018-04-21 09:41:30 -0700682 goto errout;
Roopa Prabhub16fb412018-04-21 09:41:31 -0700683 }
Roopa Prabhuf9d4b0c2018-04-21 09:41:30 -0700684
685 err = fib_nl2rule(skb, nlh, extack, ops, tb, &rule, &user_priority);
686 if (err)
687 goto errout;
688
Mateusz Bajorski153380e2016-06-29 09:22:10 +0200689 if ((nlh->nlmsg_flags & NLM_F_EXCL) &&
Roopa Prabhuf9d4b0c2018-04-21 09:41:30 -0700690 rule_find(ops, frh, tb, rule, user_priority)) {
Mateusz Bajorski153380e2016-06-29 09:22:10 +0200691 err = -EEXIST;
692 goto errout_free;
693 }
694
Roopa Prabhub16fb412018-04-21 09:41:31 -0700695 err = ops->configure(rule, skb, frh, tb, extack);
Thomas Graf14c0b972006-08-04 03:38:38 -0700696 if (err < 0)
697 goto errout_free;
698
David Ahern9776d322018-03-27 18:21:56 -0700699 err = call_fib_rule_notifiers(net, FIB_EVENT_RULE_ADD, rule, ops,
700 extack);
701 if (err < 0)
702 goto errout_free;
703
Denis V. Lunev76c72d42007-09-16 15:44:27 -0700704 list_for_each_entry(r, &ops->rules_list, list) {
Roopa Prabhuf9d4b0c2018-04-21 09:41:30 -0700705 if (r->pref == rule->target) {
706 RCU_INIT_POINTER(rule->ctarget, r);
707 break;
708 }
709 }
710
711 if (rcu_dereference_protected(rule->ctarget, 1) == NULL)
712 unresolved = 1;
713
714 list_for_each_entry(r, &ops->rules_list, list) {
Thomas Graf14c0b972006-08-04 03:38:38 -0700715 if (r->pref > rule->pref)
716 break;
717 last = r;
718 }
719
Eric Dumazetebb9fed2010-10-23 09:44:25 +0000720 if (last)
721 list_add_rcu(&rule->list, &last->list);
722 else
723 list_add_rcu(&rule->list, &ops->rules_list);
724
Thomas Graf0947c9fe2007-03-26 17:14:15 -0700725 if (ops->unresolved_rules) {
726 /*
727 * There are unresolved goto rules in the list, check if
728 * any of them are pointing to this new rule.
729 */
Denis V. Lunev76c72d42007-09-16 15:44:27 -0700730 list_for_each_entry(r, &ops->rules_list, list) {
Thomas Graf0947c9fe2007-03-26 17:14:15 -0700731 if (r->action == FR_ACT_GOTO &&
Gao feng561dac22011-09-11 15:36:05 +0000732 r->target == rule->pref &&
733 rtnl_dereference(r->ctarget) == NULL) {
Thomas Graf0947c9fe2007-03-26 17:14:15 -0700734 rcu_assign_pointer(r->ctarget, rule);
735 if (--ops->unresolved_rules == 0)
736 break;
737 }
738 }
739 }
740
741 if (rule->action == FR_ACT_GOTO)
742 ops->nr_goto_rules++;
743
744 if (unresolved)
745 ops->unresolved_rules++;
746
Thomas Grafe7030872015-07-21 10:44:01 +0200747 if (rule->tun_id)
748 ip_tunnel_need_metadata();
749
Eric W. Biederman15e47302012-09-07 20:12:54 +0000750 notify_rule_change(RTM_NEWRULE, rule, ops, nlh, NETLINK_CB(skb).portid);
Thomas Graf73417f62007-03-27 13:56:52 -0700751 flush_route_cache(ops);
Thomas Graf14c0b972006-08-04 03:38:38 -0700752 rules_ops_put(ops);
753 return 0;
754
755errout_free:
756 kfree(rule);
757errout:
758 rules_ops_put(ops);
759 return err;
760}
David Ahern96c63fa2016-06-08 10:55:39 -0700761EXPORT_SYMBOL_GPL(fib_nl_newrule);
Thomas Graf14c0b972006-08-04 03:38:38 -0700762
David Ahernc21ef3e2017-04-16 09:48:24 -0700763int fib_nl_delrule(struct sk_buff *skb, struct nlmsghdr *nlh,
764 struct netlink_ext_ack *extack)
Thomas Graf14c0b972006-08-04 03:38:38 -0700765{
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +0900766 struct net *net = sock_net(skb->sk);
Thomas Graf14c0b972006-08-04 03:38:38 -0700767 struct fib_rule_hdr *frh = nlmsg_data(nlh);
768 struct fib_rules_ops *ops = NULL;
Roopa Prabhuf9d4b0c2018-04-21 09:41:30 -0700769 struct fib_rule *rule = NULL, *r, *nlrule = NULL;
Thomas Graf14c0b972006-08-04 03:38:38 -0700770 struct nlattr *tb[FRA_MAX+1];
771 int err = -EINVAL;
Roopa Prabhuf9d4b0c2018-04-21 09:41:30 -0700772 bool user_priority = false;
Thomas Graf14c0b972006-08-04 03:38:38 -0700773
Roopa Prabhub16fb412018-04-21 09:41:31 -0700774 if (nlh->nlmsg_len < nlmsg_msg_size(sizeof(*frh))) {
775 NL_SET_ERR_MSG(extack, "Invalid msg length");
Thomas Graf14c0b972006-08-04 03:38:38 -0700776 goto errout;
Roopa Prabhub16fb412018-04-21 09:41:31 -0700777 }
Thomas Graf14c0b972006-08-04 03:38:38 -0700778
Denis V. Lunev5fd30ee2008-01-10 03:20:28 -0800779 ops = lookup_rules_ops(net, frh->family);
Thomas Graf14c0b972006-08-04 03:38:38 -0700780 if (ops == NULL) {
Patrick McHardy2fe195c2008-07-01 19:59:37 -0700781 err = -EAFNOSUPPORT;
Roopa Prabhub16fb412018-04-21 09:41:31 -0700782 NL_SET_ERR_MSG(extack, "Rule family not supported");
Thomas Graf14c0b972006-08-04 03:38:38 -0700783 goto errout;
784 }
785
David Ahernc21ef3e2017-04-16 09:48:24 -0700786 err = nlmsg_parse(nlh, sizeof(*frh), tb, FRA_MAX, ops->policy, extack);
Roopa Prabhub16fb412018-04-21 09:41:31 -0700787 if (err < 0) {
788 NL_SET_ERR_MSG(extack, "Error parsing msg");
Thomas Graf14c0b972006-08-04 03:38:38 -0700789 goto errout;
Roopa Prabhub16fb412018-04-21 09:41:31 -0700790 }
Thomas Graf14c0b972006-08-04 03:38:38 -0700791
Roopa Prabhuf9d4b0c2018-04-21 09:41:30 -0700792 err = fib_nl2rule(skb, nlh, extack, ops, tb, &nlrule, &user_priority);
793 if (err)
Thomas Grafe1701c62007-03-24 12:46:02 -0700794 goto errout;
795
Roopa Prabhuf9d4b0c2018-04-21 09:41:30 -0700796 rule = rule_find(ops, frh, tb, nlrule, user_priority);
797 if (!rule) {
798 err = -ENOENT;
799 goto errout;
Lorenzo Colitti622ec2c2016-11-04 02:23:42 +0900800 }
801
Roopa Prabhuf9d4b0c2018-04-21 09:41:30 -0700802 if (rule->flags & FIB_RULE_PERMANENT) {
803 err = -EPERM;
804 goto errout;
805 }
806
807 if (ops->delete) {
808 err = ops->delete(rule);
Roopa Prabhubfff4862018-02-28 22:40:16 -0500809 if (err)
810 goto errout;
811 }
812
Roopa Prabhuf9d4b0c2018-04-21 09:41:30 -0700813 if (rule->tun_id)
814 ip_tunnel_unneed_metadata();
815
816 list_del_rcu(&rule->list);
817
818 if (rule->action == FR_ACT_GOTO) {
819 ops->nr_goto_rules--;
820 if (rtnl_dereference(rule->ctarget) == NULL)
821 ops->unresolved_rules--;
Roopa Prabhubfff4862018-02-28 22:40:16 -0500822 }
823
Roopa Prabhuf9d4b0c2018-04-21 09:41:30 -0700824 /*
825 * Check if this rule is a target to any of them. If so,
826 * adjust to the next one with the same preference or
827 * disable them. As this operation is eventually very
828 * expensive, it is only performed if goto rules, except
829 * current if it is goto rule, have actually been added.
830 */
831 if (ops->nr_goto_rules > 0) {
832 struct fib_rule *n;
Donald Sharpcac56202018-02-20 08:55:58 -0500833
Roopa Prabhuf9d4b0c2018-04-21 09:41:30 -0700834 n = list_next_entry(rule, list);
835 if (&n->list == &ops->rules_list || n->pref != rule->pref)
836 n = NULL;
837 list_for_each_entry(r, &ops->rules_list, list) {
838 if (rtnl_dereference(r->ctarget) != rule)
839 continue;
840 rcu_assign_pointer(r->ctarget, n);
841 if (!n)
842 ops->unresolved_rules++;
Thomas Graf14c0b972006-08-04 03:38:38 -0700843 }
Thomas Graf14c0b972006-08-04 03:38:38 -0700844 }
845
Roopa Prabhuf9d4b0c2018-04-21 09:41:30 -0700846 call_fib_rule_notifiers(net, FIB_EVENT_RULE_DEL, rule, ops,
847 NULL);
848 notify_rule_change(RTM_DELRULE, rule, ops, nlh,
849 NETLINK_CB(skb).portid);
850 fib_rule_put(rule);
851 flush_route_cache(ops);
852 rules_ops_put(ops);
853 kfree(nlrule);
854 return 0;
855
Thomas Graf14c0b972006-08-04 03:38:38 -0700856errout:
Roopa Prabhuf9d4b0c2018-04-21 09:41:30 -0700857 if (nlrule)
858 kfree(nlrule);
Thomas Graf14c0b972006-08-04 03:38:38 -0700859 rules_ops_put(ops);
860 return err;
861}
David Ahern96c63fa2016-06-08 10:55:39 -0700862EXPORT_SYMBOL_GPL(fib_nl_delrule);
Thomas Graf14c0b972006-08-04 03:38:38 -0700863
Thomas Graf339bf982006-11-10 14:10:15 -0800864static inline size_t fib_rule_nlmsg_size(struct fib_rules_ops *ops,
865 struct fib_rule *rule)
866{
867 size_t payload = NLMSG_ALIGN(sizeof(struct fib_rule_hdr))
Patrick McHardy491deb22009-12-03 01:25:54 +0000868 + nla_total_size(IFNAMSIZ) /* FRA_IIFNAME */
Patrick McHardy1b038a52009-12-03 01:25:56 +0000869 + nla_total_size(IFNAMSIZ) /* FRA_OIFNAME */
Thomas Graf339bf982006-11-10 14:10:15 -0800870 + nla_total_size(4) /* FRA_PRIORITY */
871 + nla_total_size(4) /* FRA_TABLE */
Stefan Tomanek73f56982013-08-03 14:14:43 +0200872 + nla_total_size(4) /* FRA_SUPPRESS_PREFIXLEN */
Stefan Tomanek6ef94cf2013-08-02 17:19:56 +0200873 + nla_total_size(4) /* FRA_SUPPRESS_IFGROUP */
Thomas Graf339bf982006-11-10 14:10:15 -0800874 + nla_total_size(4) /* FRA_FWMARK */
Thomas Grafe7030872015-07-21 10:44:01 +0200875 + nla_total_size(4) /* FRA_FWMASK */
Lorenzo Colitti622ec2c2016-11-04 02:23:42 +0900876 + nla_total_size_64bit(8) /* FRA_TUN_ID */
Donald Sharp1b71af62018-02-23 14:01:52 -0500877 + nla_total_size(sizeof(struct fib_kuid_range))
Roopa Prabhubfff4862018-02-28 22:40:16 -0500878 + nla_total_size(1) /* FRA_PROTOCOL */
879 + nla_total_size(1) /* FRA_IP_PROTO */
880 + nla_total_size(sizeof(struct fib_rule_port_range)) /* FRA_SPORT_RANGE */
881 + nla_total_size(sizeof(struct fib_rule_port_range)); /* FRA_DPORT_RANGE */
Thomas Graf339bf982006-11-10 14:10:15 -0800882
883 if (ops->nlmsg_payload)
884 payload += ops->nlmsg_payload(rule);
885
886 return payload;
887}
888
Thomas Graf14c0b972006-08-04 03:38:38 -0700889static int fib_nl_fill_rule(struct sk_buff *skb, struct fib_rule *rule,
890 u32 pid, u32 seq, int type, int flags,
891 struct fib_rules_ops *ops)
892{
893 struct nlmsghdr *nlh;
894 struct fib_rule_hdr *frh;
895
896 nlh = nlmsg_put(skb, pid, seq, type, sizeof(*frh), flags);
897 if (nlh == NULL)
Patrick McHardy26932562007-01-31 23:16:40 -0800898 return -EMSGSIZE;
Thomas Graf14c0b972006-08-04 03:38:38 -0700899
900 frh = nlmsg_data(nlh);
Patrick McHardy28bb1722010-04-13 05:03:16 +0000901 frh->family = ops->family;
Thomas Graf14c0b972006-08-04 03:38:38 -0700902 frh->table = rule->table;
David S. Miller0e3cea72012-04-01 20:47:01 -0400903 if (nla_put_u32(skb, FRA_TABLE, rule->table))
904 goto nla_put_failure;
Stefan Tomanek73f56982013-08-03 14:14:43 +0200905 if (nla_put_u32(skb, FRA_SUPPRESS_PREFIXLEN, rule->suppress_prefixlen))
Stefan Tomanek7764a452013-08-01 02:17:15 +0200906 goto nla_put_failure;
Thomas Graf14c0b972006-08-04 03:38:38 -0700907 frh->res1 = 0;
Donald Sharp1b71af62018-02-23 14:01:52 -0500908 frh->res2 = 0;
Thomas Graf14c0b972006-08-04 03:38:38 -0700909 frh->action = rule->action;
910 frh->flags = rule->flags;
Donald Sharp1b71af62018-02-23 14:01:52 -0500911
912 if (nla_put_u8(skb, FRA_PROTOCOL, rule->proto))
913 goto nla_put_failure;
Thomas Graf14c0b972006-08-04 03:38:38 -0700914
Eric Dumazet7a2b03c2010-10-26 09:24:55 +0000915 if (rule->action == FR_ACT_GOTO &&
Eric Dumazet33d480c2011-08-11 19:30:52 +0000916 rcu_access_pointer(rule->ctarget) == NULL)
Thomas Graf0947c9fe2007-03-26 17:14:15 -0700917 frh->flags |= FIB_RULE_UNRESOLVED;
918
Patrick McHardy491deb22009-12-03 01:25:54 +0000919 if (rule->iifname[0]) {
David S. Miller0e3cea72012-04-01 20:47:01 -0400920 if (nla_put_string(skb, FRA_IIFNAME, rule->iifname))
921 goto nla_put_failure;
Patrick McHardy491deb22009-12-03 01:25:54 +0000922 if (rule->iifindex == -1)
923 frh->flags |= FIB_RULE_IIF_DETACHED;
Thomas Graf2b443682007-03-26 17:37:59 -0700924 }
925
Patrick McHardy1b038a52009-12-03 01:25:56 +0000926 if (rule->oifname[0]) {
David S. Miller0e3cea72012-04-01 20:47:01 -0400927 if (nla_put_string(skb, FRA_OIFNAME, rule->oifname))
928 goto nla_put_failure;
Patrick McHardy1b038a52009-12-03 01:25:56 +0000929 if (rule->oifindex == -1)
930 frh->flags |= FIB_RULE_OIF_DETACHED;
931 }
932
David S. Miller0e3cea72012-04-01 20:47:01 -0400933 if ((rule->pref &&
934 nla_put_u32(skb, FRA_PRIORITY, rule->pref)) ||
935 (rule->mark &&
936 nla_put_u32(skb, FRA_FWMARK, rule->mark)) ||
937 ((rule->mark_mask || rule->mark) &&
938 nla_put_u32(skb, FRA_FWMASK, rule->mark_mask)) ||
939 (rule->target &&
Thomas Grafe7030872015-07-21 10:44:01 +0200940 nla_put_u32(skb, FRA_GOTO, rule->target)) ||
941 (rule->tun_id &&
David Ahern96c63fa2016-06-08 10:55:39 -0700942 nla_put_be64(skb, FRA_TUN_ID, rule->tun_id, FRA_PAD)) ||
943 (rule->l3mdev &&
Lorenzo Colitti622ec2c2016-11-04 02:23:42 +0900944 nla_put_u8(skb, FRA_L3MDEV, rule->l3mdev)) ||
945 (uid_range_set(&rule->uid_range) &&
Roopa Prabhubfff4862018-02-28 22:40:16 -0500946 nla_put_uid_range(skb, &rule->uid_range)) ||
947 (fib_rule_port_range_set(&rule->sport_range) &&
948 nla_put_port_range(skb, FRA_SPORT_RANGE, &rule->sport_range)) ||
949 (fib_rule_port_range_set(&rule->dport_range) &&
950 nla_put_port_range(skb, FRA_DPORT_RANGE, &rule->dport_range)) ||
951 (rule->ip_proto && nla_put_u8(skb, FRA_IP_PROTO, rule->ip_proto)))
David S. Miller0e3cea72012-04-01 20:47:01 -0400952 goto nla_put_failure;
Stefan Tomanek6ef94cf2013-08-02 17:19:56 +0200953
954 if (rule->suppress_ifgroup != -1) {
955 if (nla_put_u32(skb, FRA_SUPPRESS_IFGROUP, rule->suppress_ifgroup))
956 goto nla_put_failure;
957 }
958
Rami Rosen04af8cf2009-05-20 17:26:23 -0700959 if (ops->fill(rule, skb, frh) < 0)
Thomas Graf14c0b972006-08-04 03:38:38 -0700960 goto nla_put_failure;
961
Johannes Berg053c0952015-01-16 22:09:00 +0100962 nlmsg_end(skb, nlh);
963 return 0;
Thomas Graf14c0b972006-08-04 03:38:38 -0700964
965nla_put_failure:
Patrick McHardy26932562007-01-31 23:16:40 -0800966 nlmsg_cancel(skb, nlh);
967 return -EMSGSIZE;
Thomas Graf14c0b972006-08-04 03:38:38 -0700968}
969
Thomas Grafc4546732007-03-25 23:24:24 -0700970static int dump_rules(struct sk_buff *skb, struct netlink_callback *cb,
971 struct fib_rules_ops *ops)
Thomas Graf14c0b972006-08-04 03:38:38 -0700972{
973 int idx = 0;
974 struct fib_rule *rule;
Wilson Kok41fc0142015-09-22 21:40:22 -0700975 int err = 0;
Thomas Graf14c0b972006-08-04 03:38:38 -0700976
Eric Dumazete67f88d2011-04-27 22:56:07 +0000977 rcu_read_lock();
978 list_for_each_entry_rcu(rule, &ops->rules_list, list) {
Thomas Grafc4546732007-03-25 23:24:24 -0700979 if (idx < cb->args[1])
Thomas Graf14c0b972006-08-04 03:38:38 -0700980 goto skip;
981
Wilson Kok41fc0142015-09-22 21:40:22 -0700982 err = fib_nl_fill_rule(skb, rule, NETLINK_CB(cb->skb).portid,
983 cb->nlh->nlmsg_seq, RTM_NEWRULE,
984 NLM_F_MULTI, ops);
985 if (err)
Thomas Graf14c0b972006-08-04 03:38:38 -0700986 break;
987skip:
988 idx++;
989 }
Eric Dumazet2907c352011-05-25 07:34:04 +0000990 rcu_read_unlock();
Thomas Grafc4546732007-03-25 23:24:24 -0700991 cb->args[1] = idx;
Thomas Graf14c0b972006-08-04 03:38:38 -0700992 rules_ops_put(ops);
993
Wilson Kok41fc0142015-09-22 21:40:22 -0700994 return err;
Thomas Graf14c0b972006-08-04 03:38:38 -0700995}
996
Thomas Grafc4546732007-03-25 23:24:24 -0700997static int fib_nl_dumprule(struct sk_buff *skb, struct netlink_callback *cb)
998{
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +0900999 struct net *net = sock_net(skb->sk);
Thomas Grafc4546732007-03-25 23:24:24 -07001000 struct fib_rules_ops *ops;
1001 int idx = 0, family;
1002
1003 family = rtnl_msg_family(cb->nlh);
1004 if (family != AF_UNSPEC) {
1005 /* Protocol specific dump request */
Denis V. Lunev5fd30ee2008-01-10 03:20:28 -08001006 ops = lookup_rules_ops(net, family);
Thomas Grafc4546732007-03-25 23:24:24 -07001007 if (ops == NULL)
1008 return -EAFNOSUPPORT;
1009
Wilson Kok41fc0142015-09-22 21:40:22 -07001010 dump_rules(skb, cb, ops);
1011
1012 return skb->len;
Thomas Grafc4546732007-03-25 23:24:24 -07001013 }
1014
1015 rcu_read_lock();
Denis V. Lunev5fd30ee2008-01-10 03:20:28 -08001016 list_for_each_entry_rcu(ops, &net->rules_ops, list) {
Thomas Grafc4546732007-03-25 23:24:24 -07001017 if (idx < cb->args[0] || !try_module_get(ops->owner))
1018 goto skip;
1019
1020 if (dump_rules(skb, cb, ops) < 0)
1021 break;
1022
1023 cb->args[1] = 0;
Eric Dumazet2fb35732010-03-09 20:03:38 +00001024skip:
Thomas Grafc4546732007-03-25 23:24:24 -07001025 idx++;
1026 }
1027 rcu_read_unlock();
1028 cb->args[0] = idx;
1029
1030 return skb->len;
1031}
Thomas Graf14c0b972006-08-04 03:38:38 -07001032
Denis V. Lunev9e3a5482008-01-20 16:46:41 -08001033static void notify_rule_change(int event, struct fib_rule *rule,
Thomas Grafc17084d2006-08-15 00:32:48 -07001034 struct fib_rules_ops *ops, struct nlmsghdr *nlh,
1035 u32 pid)
Thomas Graf14c0b972006-08-04 03:38:38 -07001036{
Denis V. Lunev9e3a5482008-01-20 16:46:41 -08001037 struct net *net;
Thomas Grafc17084d2006-08-15 00:32:48 -07001038 struct sk_buff *skb;
1039 int err = -ENOBUFS;
Thomas Graf14c0b972006-08-04 03:38:38 -07001040
Denis V. Lunev9e3a5482008-01-20 16:46:41 -08001041 net = ops->fro_net;
Thomas Graf339bf982006-11-10 14:10:15 -08001042 skb = nlmsg_new(fib_rule_nlmsg_size(ops, rule), GFP_KERNEL);
Thomas Graf14c0b972006-08-04 03:38:38 -07001043 if (skb == NULL)
Thomas Grafc17084d2006-08-15 00:32:48 -07001044 goto errout;
1045
1046 err = fib_nl_fill_rule(skb, rule, pid, nlh->nlmsg_seq, event, 0, ops);
Patrick McHardy26932562007-01-31 23:16:40 -08001047 if (err < 0) {
1048 /* -EMSGSIZE implies BUG in fib_rule_nlmsg_size() */
1049 WARN_ON(err == -EMSGSIZE);
1050 kfree_skb(skb);
1051 goto errout;
1052 }
Denis V. Lunev9e3a5482008-01-20 16:46:41 -08001053
Pablo Neira Ayuso1ce85fe2009-02-24 23:18:28 -08001054 rtnl_notify(skb, net, pid, ops->nlgroup, nlh, GFP_KERNEL);
1055 return;
Thomas Grafc17084d2006-08-15 00:32:48 -07001056errout:
1057 if (err < 0)
Denis V. Lunev5fd30ee2008-01-10 03:20:28 -08001058 rtnl_set_sk_err(net, ops->nlgroup, err);
Thomas Graf14c0b972006-08-04 03:38:38 -07001059}
1060
1061static void attach_rules(struct list_head *rules, struct net_device *dev)
1062{
1063 struct fib_rule *rule;
1064
1065 list_for_each_entry(rule, rules, list) {
Patrick McHardy491deb22009-12-03 01:25:54 +00001066 if (rule->iifindex == -1 &&
1067 strcmp(dev->name, rule->iifname) == 0)
1068 rule->iifindex = dev->ifindex;
Patrick McHardy1b038a52009-12-03 01:25:56 +00001069 if (rule->oifindex == -1 &&
1070 strcmp(dev->name, rule->oifname) == 0)
1071 rule->oifindex = dev->ifindex;
Thomas Graf14c0b972006-08-04 03:38:38 -07001072 }
1073}
1074
1075static void detach_rules(struct list_head *rules, struct net_device *dev)
1076{
1077 struct fib_rule *rule;
1078
Patrick McHardy1b038a52009-12-03 01:25:56 +00001079 list_for_each_entry(rule, rules, list) {
Patrick McHardy491deb22009-12-03 01:25:54 +00001080 if (rule->iifindex == dev->ifindex)
1081 rule->iifindex = -1;
Patrick McHardy1b038a52009-12-03 01:25:56 +00001082 if (rule->oifindex == dev->ifindex)
1083 rule->oifindex = -1;
1084 }
Thomas Graf14c0b972006-08-04 03:38:38 -07001085}
1086
1087
1088static int fib_rules_event(struct notifier_block *this, unsigned long event,
Jiri Pirko351638e2013-05-28 01:30:21 +00001089 void *ptr)
Thomas Graf14c0b972006-08-04 03:38:38 -07001090{
Jiri Pirko351638e2013-05-28 01:30:21 +00001091 struct net_device *dev = netdev_notifier_info_to_dev(ptr);
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +09001092 struct net *net = dev_net(dev);
Thomas Graf14c0b972006-08-04 03:38:38 -07001093 struct fib_rules_ops *ops;
1094
Eric Dumazet748e2d92012-08-22 21:50:59 +00001095 ASSERT_RTNL();
Thomas Graf14c0b972006-08-04 03:38:38 -07001096
1097 switch (event) {
1098 case NETDEV_REGISTER:
Denis V. Lunev5fd30ee2008-01-10 03:20:28 -08001099 list_for_each_entry(ops, &net->rules_ops, list)
Denis V. Lunev76c72d42007-09-16 15:44:27 -07001100 attach_rules(&ops->rules_list, dev);
Thomas Graf14c0b972006-08-04 03:38:38 -07001101 break;
1102
Maciej Żenczykowski946c0322014-02-07 16:23:48 -08001103 case NETDEV_CHANGENAME:
1104 list_for_each_entry(ops, &net->rules_ops, list) {
1105 detach_rules(&ops->rules_list, dev);
1106 attach_rules(&ops->rules_list, dev);
1107 }
1108 break;
1109
Thomas Graf14c0b972006-08-04 03:38:38 -07001110 case NETDEV_UNREGISTER:
Denis V. Lunev5fd30ee2008-01-10 03:20:28 -08001111 list_for_each_entry(ops, &net->rules_ops, list)
Denis V. Lunev76c72d42007-09-16 15:44:27 -07001112 detach_rules(&ops->rules_list, dev);
Thomas Graf14c0b972006-08-04 03:38:38 -07001113 break;
1114 }
1115
Thomas Graf14c0b972006-08-04 03:38:38 -07001116 return NOTIFY_DONE;
1117}
1118
1119static struct notifier_block fib_rules_notifier = {
1120 .notifier_call = fib_rules_event,
1121};
1122
Alexey Dobriyan2c8c1e72010-01-17 03:35:32 +00001123static int __net_init fib_rules_net_init(struct net *net)
Denis V. Lunev5fd30ee2008-01-10 03:20:28 -08001124{
1125 INIT_LIST_HEAD(&net->rules_ops);
1126 spin_lock_init(&net->rules_mod_lock);
1127 return 0;
1128}
1129
Vasily Averince2b7db2017-11-12 22:30:01 +03001130static void __net_exit fib_rules_net_exit(struct net *net)
1131{
1132 WARN_ON_ONCE(!list_empty(&net->rules_ops));
1133}
1134
Denis V. Lunev5fd30ee2008-01-10 03:20:28 -08001135static struct pernet_operations fib_rules_net_ops = {
1136 .init = fib_rules_net_init,
Vasily Averince2b7db2017-11-12 22:30:01 +03001137 .exit = fib_rules_net_exit,
Denis V. Lunev5fd30ee2008-01-10 03:20:28 -08001138};
1139
Thomas Graf14c0b972006-08-04 03:38:38 -07001140static int __init fib_rules_init(void)
1141{
Denis V. Lunev5fd30ee2008-01-10 03:20:28 -08001142 int err;
Florian Westphalb97bac62017-08-09 20:41:48 +02001143 rtnl_register(PF_UNSPEC, RTM_NEWRULE, fib_nl_newrule, NULL, 0);
1144 rtnl_register(PF_UNSPEC, RTM_DELRULE, fib_nl_delrule, NULL, 0);
1145 rtnl_register(PF_UNSPEC, RTM_GETRULE, NULL, fib_nl_dumprule, 0);
Thomas Graf9d9e6a52007-03-25 23:20:05 -07001146
Eric W. Biederman5d6d4802008-11-07 22:52:34 -08001147 err = register_pernet_subsys(&fib_rules_net_ops);
Denis V. Lunev5fd30ee2008-01-10 03:20:28 -08001148 if (err < 0)
1149 goto fail;
1150
Eric W. Biederman5d6d4802008-11-07 22:52:34 -08001151 err = register_netdevice_notifier(&fib_rules_notifier);
Denis V. Lunev5fd30ee2008-01-10 03:20:28 -08001152 if (err < 0)
1153 goto fail_unregister;
Eric W. Biederman5d6d4802008-11-07 22:52:34 -08001154
Denis V. Lunev5fd30ee2008-01-10 03:20:28 -08001155 return 0;
1156
1157fail_unregister:
Eric W. Biederman5d6d4802008-11-07 22:52:34 -08001158 unregister_pernet_subsys(&fib_rules_net_ops);
Denis V. Lunev5fd30ee2008-01-10 03:20:28 -08001159fail:
1160 rtnl_unregister(PF_UNSPEC, RTM_NEWRULE);
1161 rtnl_unregister(PF_UNSPEC, RTM_DELRULE);
1162 rtnl_unregister(PF_UNSPEC, RTM_GETRULE);
1163 return err;
Thomas Graf14c0b972006-08-04 03:38:38 -07001164}
1165
1166subsys_initcall(fib_rules_init);