blob: 126ffc5bc630cb412e4bcf1a48869ec6711fda54 [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
Roopa Prabhuf9d4b0c2018-04-21 09:41:30 -0700419 if (rule->mark_mask && r->mark_mask != rule->mark_mask)
Mateusz Bajorski153380e2016-06-29 09:22:10 +0200420 continue;
421
Roopa Prabhuf9d4b0c2018-04-21 09:41:30 -0700422 if (rule->tun_id && r->tun_id != rule->tun_id)
Mateusz Bajorski153380e2016-06-29 09:22:10 +0200423 continue;
424
425 if (r->fr_net != rule->fr_net)
426 continue;
427
Roopa Prabhuf9d4b0c2018-04-21 09:41:30 -0700428 if (rule->l3mdev && r->l3mdev != rule->l3mdev)
Mateusz Bajorski153380e2016-06-29 09:22:10 +0200429 continue;
430
Roopa Prabhuf9d4b0c2018-04-21 09:41:30 -0700431 if (uid_range_set(&rule->uid_range) &&
432 (!uid_eq(r->uid_range.start, rule->uid_range.start) ||
433 !uid_eq(r->uid_range.end, rule->uid_range.end)))
Lorenzo Colitti35b80732016-11-07 00:16:25 +0900434 continue;
435
Roopa Prabhuf9d4b0c2018-04-21 09:41:30 -0700436 if (rule->ip_proto && r->ip_proto != rule->ip_proto)
Roopa Prabhubfff4862018-02-28 22:40:16 -0500437 continue;
438
Roopa Prabhuf9d4b0c2018-04-21 09:41:30 -0700439 if (fib_rule_port_range_set(&rule->sport_range) &&
440 !fib_rule_port_range_compare(&r->sport_range,
Roopa Prabhubfff4862018-02-28 22:40:16 -0500441 &rule->sport_range))
442 continue;
443
Roopa Prabhuf9d4b0c2018-04-21 09:41:30 -0700444 if (fib_rule_port_range_set(&rule->dport_range) &&
445 !fib_rule_port_range_compare(&r->dport_range,
Roopa Prabhubfff4862018-02-28 22:40:16 -0500446 &rule->dport_range))
447 continue;
448
Mateusz Bajorski153380e2016-06-29 09:22:10 +0200449 if (!ops->compare(r, frh, tb))
450 continue;
Roopa Prabhuf9d4b0c2018-04-21 09:41:30 -0700451 return r;
Mateusz Bajorski153380e2016-06-29 09:22:10 +0200452 }
Roopa Prabhuf9d4b0c2018-04-21 09:41:30 -0700453
454 return NULL;
Mateusz Bajorski153380e2016-06-29 09:22:10 +0200455}
456
David Ahernc77bbc62018-04-24 18:36:07 -0700457#ifdef CONFIG_NET_L3_MASTER_DEV
458static int fib_nl2rule_l3mdev(struct nlattr *nla, struct fib_rule *nlrule,
459 struct netlink_ext_ack *extack)
460{
461 nlrule->l3mdev = nla_get_u8(nla);
462 if (nlrule->l3mdev != 1) {
463 NL_SET_ERR_MSG(extack, "Invalid l3mdev attribute");
464 return -1;
465 }
466
467 return 0;
468}
469#else
470static int fib_nl2rule_l3mdev(struct nlattr *nla, struct fib_rule *nlrule,
471 struct netlink_ext_ack *extack)
472{
473 NL_SET_ERR_MSG(extack, "l3mdev support is not enabled in kernel");
474 return -1;
475}
476#endif
477
Roopa Prabhuf9d4b0c2018-04-21 09:41:30 -0700478static int fib_nl2rule(struct sk_buff *skb, struct nlmsghdr *nlh,
479 struct netlink_ext_ack *extack,
480 struct fib_rules_ops *ops,
481 struct nlattr *tb[],
482 struct fib_rule **rule,
483 bool *user_priority)
Thomas Graf14c0b972006-08-04 03:38:38 -0700484{
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +0900485 struct net *net = sock_net(skb->sk);
Thomas Graf14c0b972006-08-04 03:38:38 -0700486 struct fib_rule_hdr *frh = nlmsg_data(nlh);
Roopa Prabhuf9d4b0c2018-04-21 09:41:30 -0700487 struct fib_rule *nlrule = NULL;
488 int err = -EINVAL;
Thomas Graf14c0b972006-08-04 03:38:38 -0700489
Roopa Prabhuf9d4b0c2018-04-21 09:41:30 -0700490 if (frh->src_len)
491 if (!tb[FRA_SRC] ||
492 frh->src_len > (ops->addr_size * 8) ||
Roopa Prabhub16fb412018-04-21 09:41:31 -0700493 nla_len(tb[FRA_SRC]) != ops->addr_size) {
494 NL_SET_ERR_MSG(extack, "Invalid source address");
Roopa Prabhuf9d4b0c2018-04-21 09:41:30 -0700495 goto errout;
Roopa Prabhub16fb412018-04-21 09:41:31 -0700496 }
Thomas Graf14c0b972006-08-04 03:38:38 -0700497
Roopa Prabhuf9d4b0c2018-04-21 09:41:30 -0700498 if (frh->dst_len)
499 if (!tb[FRA_DST] ||
500 frh->dst_len > (ops->addr_size * 8) ||
Roopa Prabhub16fb412018-04-21 09:41:31 -0700501 nla_len(tb[FRA_DST]) != ops->addr_size) {
502 NL_SET_ERR_MSG(extack, "Invalid dst 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 nlrule = kzalloc(ops->rule_size, GFP_KERNEL);
507 if (!nlrule) {
Thomas Graf14c0b972006-08-04 03:38:38 -0700508 err = -ENOMEM;
509 goto errout;
510 }
Roopa Prabhuf9d4b0c2018-04-21 09:41:30 -0700511 refcount_set(&nlrule->refcnt, 1);
512 nlrule->fr_net = net;
Thomas Graf14c0b972006-08-04 03:38:38 -0700513
Roopa Prabhuf9d4b0c2018-04-21 09:41:30 -0700514 if (tb[FRA_PRIORITY]) {
515 nlrule->pref = nla_get_u32(tb[FRA_PRIORITY]);
516 *user_priority = true;
517 } else {
518 nlrule->pref = fib_default_rule_pref(ops);
519 }
Thomas Graf14c0b972006-08-04 03:38:38 -0700520
Roopa Prabhuf9d4b0c2018-04-21 09:41:30 -0700521 nlrule->proto = tb[FRA_PROTOCOL] ?
Donald Sharp1b71af62018-02-23 14:01:52 -0500522 nla_get_u8(tb[FRA_PROTOCOL]) : RTPROT_UNSPEC;
523
Patrick McHardy491deb22009-12-03 01:25:54 +0000524 if (tb[FRA_IIFNAME]) {
Thomas Graf14c0b972006-08-04 03:38:38 -0700525 struct net_device *dev;
526
Roopa Prabhuf9d4b0c2018-04-21 09:41:30 -0700527 nlrule->iifindex = -1;
528 nla_strlcpy(nlrule->iifname, tb[FRA_IIFNAME], IFNAMSIZ);
529 dev = __dev_get_by_name(net, nlrule->iifname);
Thomas Graf14c0b972006-08-04 03:38:38 -0700530 if (dev)
Roopa Prabhuf9d4b0c2018-04-21 09:41:30 -0700531 nlrule->iifindex = dev->ifindex;
Thomas Graf14c0b972006-08-04 03:38:38 -0700532 }
533
Patrick McHardy1b038a52009-12-03 01:25:56 +0000534 if (tb[FRA_OIFNAME]) {
535 struct net_device *dev;
536
Roopa Prabhuf9d4b0c2018-04-21 09:41:30 -0700537 nlrule->oifindex = -1;
538 nla_strlcpy(nlrule->oifname, tb[FRA_OIFNAME], IFNAMSIZ);
539 dev = __dev_get_by_name(net, nlrule->oifname);
Patrick McHardy1b038a52009-12-03 01:25:56 +0000540 if (dev)
Roopa Prabhuf9d4b0c2018-04-21 09:41:30 -0700541 nlrule->oifindex = dev->ifindex;
Patrick McHardy1b038a52009-12-03 01:25:56 +0000542 }
543
Thomas Grafb8964ed2006-11-09 15:22:18 -0800544 if (tb[FRA_FWMARK]) {
Roopa Prabhuf9d4b0c2018-04-21 09:41:30 -0700545 nlrule->mark = nla_get_u32(tb[FRA_FWMARK]);
546 if (nlrule->mark)
Thomas Grafb8964ed2006-11-09 15:22:18 -0800547 /* compatibility: if the mark value is non-zero all bits
548 * are compared unless a mask is explicitly specified.
549 */
Roopa Prabhuf9d4b0c2018-04-21 09:41:30 -0700550 nlrule->mark_mask = 0xFFFFFFFF;
Thomas Grafb8964ed2006-11-09 15:22:18 -0800551 }
552
553 if (tb[FRA_FWMASK])
Roopa Prabhuf9d4b0c2018-04-21 09:41:30 -0700554 nlrule->mark_mask = nla_get_u32(tb[FRA_FWMASK]);
Thomas Grafb8964ed2006-11-09 15:22:18 -0800555
Thomas Grafe7030872015-07-21 10:44:01 +0200556 if (tb[FRA_TUN_ID])
Roopa Prabhuf9d4b0c2018-04-21 09:41:30 -0700557 nlrule->tun_id = nla_get_be64(tb[FRA_TUN_ID]);
Thomas Grafe7030872015-07-21 10:44:01 +0200558
Wei Yongjunadeb45c2017-04-26 14:03:50 +0000559 err = -EINVAL;
David Ahernc77bbc62018-04-24 18:36:07 -0700560 if (tb[FRA_L3MDEV] &&
561 fib_nl2rule_l3mdev(tb[FRA_L3MDEV], nlrule, extack) < 0)
562 goto errout_free;
David Ahern96c63fa2016-06-08 10:55:39 -0700563
Roopa Prabhuf9d4b0c2018-04-21 09:41:30 -0700564 nlrule->action = frh->action;
565 nlrule->flags = frh->flags;
566 nlrule->table = frh_get_table(frh, tb);
Stefan Tomanek73f56982013-08-03 14:14:43 +0200567 if (tb[FRA_SUPPRESS_PREFIXLEN])
Roopa Prabhuf9d4b0c2018-04-21 09:41:30 -0700568 nlrule->suppress_prefixlen = nla_get_u32(tb[FRA_SUPPRESS_PREFIXLEN]);
Stefan Tomanek73f56982013-08-03 14:14:43 +0200569 else
Roopa Prabhuf9d4b0c2018-04-21 09:41:30 -0700570 nlrule->suppress_prefixlen = -1;
Thomas Graf14c0b972006-08-04 03:38:38 -0700571
Stefan Tomanek6ef94cf2013-08-02 17:19:56 +0200572 if (tb[FRA_SUPPRESS_IFGROUP])
Roopa Prabhuf9d4b0c2018-04-21 09:41:30 -0700573 nlrule->suppress_ifgroup = nla_get_u32(tb[FRA_SUPPRESS_IFGROUP]);
Stefan Tomanek73f56982013-08-03 14:14:43 +0200574 else
Roopa Prabhuf9d4b0c2018-04-21 09:41:30 -0700575 nlrule->suppress_ifgroup = -1;
Stefan Tomanek6ef94cf2013-08-02 17:19:56 +0200576
Thomas Graf0947c9fe2007-03-26 17:14:15 -0700577 if (tb[FRA_GOTO]) {
Roopa Prabhub16fb412018-04-21 09:41:31 -0700578 if (nlrule->action != FR_ACT_GOTO) {
579 NL_SET_ERR_MSG(extack, "Unexpected goto");
Thomas Graf0947c9fe2007-03-26 17:14:15 -0700580 goto errout_free;
Roopa Prabhub16fb412018-04-21 09:41:31 -0700581 }
Thomas Graf0947c9fe2007-03-26 17:14:15 -0700582
Roopa Prabhuf9d4b0c2018-04-21 09:41:30 -0700583 nlrule->target = nla_get_u32(tb[FRA_GOTO]);
Thomas Graf0947c9fe2007-03-26 17:14:15 -0700584 /* Backward jumps are prohibited to avoid endless loops */
Roopa Prabhub16fb412018-04-21 09:41:31 -0700585 if (nlrule->target <= nlrule->pref) {
586 NL_SET_ERR_MSG(extack, "Backward goto not supported");
Thomas Graf0947c9fe2007-03-26 17:14:15 -0700587 goto errout_free;
Roopa Prabhub16fb412018-04-21 09:41:31 -0700588 }
Roopa Prabhuf9d4b0c2018-04-21 09:41:30 -0700589 } else if (nlrule->action == FR_ACT_GOTO) {
Roopa Prabhub16fb412018-04-21 09:41:31 -0700590 NL_SET_ERR_MSG(extack, "Missing goto target for action goto");
Thomas Graf0947c9fe2007-03-26 17:14:15 -0700591 goto errout_free;
Roopa Prabhuf9d4b0c2018-04-21 09:41:30 -0700592 }
Thomas Graf0947c9fe2007-03-26 17:14:15 -0700593
Roopa Prabhub16fb412018-04-21 09:41:31 -0700594 if (nlrule->l3mdev && nlrule->table) {
595 NL_SET_ERR_MSG(extack, "l3mdev and table are mutually exclusive");
David Ahern96c63fa2016-06-08 10:55:39 -0700596 goto errout_free;
Roopa Prabhub16fb412018-04-21 09:41:31 -0700597 }
David Ahern96c63fa2016-06-08 10:55:39 -0700598
Lorenzo Colitti622ec2c2016-11-04 02:23:42 +0900599 if (tb[FRA_UID_RANGE]) {
600 if (current_user_ns() != net->user_ns) {
601 err = -EPERM;
Roopa Prabhub16fb412018-04-21 09:41:31 -0700602 NL_SET_ERR_MSG(extack, "No permission to set uid");
Lorenzo Colitti622ec2c2016-11-04 02:23:42 +0900603 goto errout_free;
604 }
605
Roopa Prabhuf9d4b0c2018-04-21 09:41:30 -0700606 nlrule->uid_range = nla_get_kuid_range(tb);
Lorenzo Colitti622ec2c2016-11-04 02:23:42 +0900607
Roopa Prabhuf9d4b0c2018-04-21 09:41:30 -0700608 if (!uid_range_set(&nlrule->uid_range) ||
Roopa Prabhub16fb412018-04-21 09:41:31 -0700609 !uid_lte(nlrule->uid_range.start, nlrule->uid_range.end)) {
610 NL_SET_ERR_MSG(extack, "Invalid uid range");
Lorenzo Colitti622ec2c2016-11-04 02:23:42 +0900611 goto errout_free;
Roopa Prabhub16fb412018-04-21 09:41:31 -0700612 }
Lorenzo Colitti622ec2c2016-11-04 02:23:42 +0900613 } else {
Roopa Prabhuf9d4b0c2018-04-21 09:41:30 -0700614 nlrule->uid_range = fib_kuid_range_unset;
Lorenzo Colitti622ec2c2016-11-04 02:23:42 +0900615 }
616
Roopa Prabhubfff4862018-02-28 22:40:16 -0500617 if (tb[FRA_IP_PROTO])
Roopa Prabhuf9d4b0c2018-04-21 09:41:30 -0700618 nlrule->ip_proto = nla_get_u8(tb[FRA_IP_PROTO]);
Roopa Prabhubfff4862018-02-28 22:40:16 -0500619
620 if (tb[FRA_SPORT_RANGE]) {
621 err = nla_get_port_range(tb[FRA_SPORT_RANGE],
Roopa Prabhuf9d4b0c2018-04-21 09:41:30 -0700622 &nlrule->sport_range);
Roopa Prabhub16fb412018-04-21 09:41:31 -0700623 if (err) {
624 NL_SET_ERR_MSG(extack, "Invalid sport range");
Roopa Prabhubfff4862018-02-28 22:40:16 -0500625 goto errout_free;
Roopa Prabhub16fb412018-04-21 09:41:31 -0700626 }
Roopa Prabhubfff4862018-02-28 22:40:16 -0500627 }
628
629 if (tb[FRA_DPORT_RANGE]) {
630 err = nla_get_port_range(tb[FRA_DPORT_RANGE],
Roopa Prabhuf9d4b0c2018-04-21 09:41:30 -0700631 &nlrule->dport_range);
Roopa Prabhub16fb412018-04-21 09:41:31 -0700632 if (err) {
633 NL_SET_ERR_MSG(extack, "Invalid dport range");
Roopa Prabhubfff4862018-02-28 22:40:16 -0500634 goto errout_free;
Roopa Prabhub16fb412018-04-21 09:41:31 -0700635 }
Roopa Prabhubfff4862018-02-28 22:40:16 -0500636 }
637
Roopa Prabhuf9d4b0c2018-04-21 09:41:30 -0700638 *rule = nlrule;
639
640 return 0;
641
642errout_free:
643 kfree(nlrule);
644errout:
645 return err;
646}
647
648int fib_nl_newrule(struct sk_buff *skb, struct nlmsghdr *nlh,
649 struct netlink_ext_ack *extack)
650{
651 struct net *net = sock_net(skb->sk);
652 struct fib_rule_hdr *frh = nlmsg_data(nlh);
653 struct fib_rules_ops *ops = NULL;
654 struct fib_rule *rule = NULL, *r, *last = NULL;
655 struct nlattr *tb[FRA_MAX + 1];
656 int err = -EINVAL, unresolved = 0;
657 bool user_priority = false;
658
Roopa Prabhub16fb412018-04-21 09:41:31 -0700659 if (nlh->nlmsg_len < nlmsg_msg_size(sizeof(*frh))) {
660 NL_SET_ERR_MSG(extack, "Invalid msg length");
Roopa Prabhuf9d4b0c2018-04-21 09:41:30 -0700661 goto errout;
Roopa Prabhub16fb412018-04-21 09:41:31 -0700662 }
Roopa Prabhuf9d4b0c2018-04-21 09:41:30 -0700663
664 ops = lookup_rules_ops(net, frh->family);
665 if (!ops) {
666 err = -EAFNOSUPPORT;
Roopa Prabhub16fb412018-04-21 09:41:31 -0700667 NL_SET_ERR_MSG(extack, "Rule family not supported");
Roopa Prabhuf9d4b0c2018-04-21 09:41:30 -0700668 goto errout;
669 }
670
671 err = nlmsg_parse(nlh, sizeof(*frh), tb, FRA_MAX, ops->policy, extack);
Roopa Prabhub16fb412018-04-21 09:41:31 -0700672 if (err < 0) {
673 NL_SET_ERR_MSG(extack, "Error parsing msg");
Roopa Prabhuf9d4b0c2018-04-21 09:41:30 -0700674 goto errout;
Roopa Prabhub16fb412018-04-21 09:41:31 -0700675 }
Roopa Prabhuf9d4b0c2018-04-21 09:41:30 -0700676
677 err = fib_nl2rule(skb, nlh, extack, ops, tb, &rule, &user_priority);
678 if (err)
679 goto errout;
680
Mateusz Bajorski153380e2016-06-29 09:22:10 +0200681 if ((nlh->nlmsg_flags & NLM_F_EXCL) &&
Roopa Prabhuf9d4b0c2018-04-21 09:41:30 -0700682 rule_find(ops, frh, tb, rule, user_priority)) {
Mateusz Bajorski153380e2016-06-29 09:22:10 +0200683 err = -EEXIST;
684 goto errout_free;
685 }
686
Roopa Prabhub16fb412018-04-21 09:41:31 -0700687 err = ops->configure(rule, skb, frh, tb, extack);
Thomas Graf14c0b972006-08-04 03:38:38 -0700688 if (err < 0)
689 goto errout_free;
690
David Ahern9776d322018-03-27 18:21:56 -0700691 err = call_fib_rule_notifiers(net, FIB_EVENT_RULE_ADD, rule, ops,
692 extack);
693 if (err < 0)
694 goto errout_free;
695
Denis V. Lunev76c72d42007-09-16 15:44:27 -0700696 list_for_each_entry(r, &ops->rules_list, list) {
Roopa Prabhuf9d4b0c2018-04-21 09:41:30 -0700697 if (r->pref == rule->target) {
698 RCU_INIT_POINTER(rule->ctarget, r);
699 break;
700 }
701 }
702
703 if (rcu_dereference_protected(rule->ctarget, 1) == NULL)
704 unresolved = 1;
705
706 list_for_each_entry(r, &ops->rules_list, list) {
Thomas Graf14c0b972006-08-04 03:38:38 -0700707 if (r->pref > rule->pref)
708 break;
709 last = r;
710 }
711
Eric Dumazetebb9fed2010-10-23 09:44:25 +0000712 if (last)
713 list_add_rcu(&rule->list, &last->list);
714 else
715 list_add_rcu(&rule->list, &ops->rules_list);
716
Thomas Graf0947c9fe2007-03-26 17:14:15 -0700717 if (ops->unresolved_rules) {
718 /*
719 * There are unresolved goto rules in the list, check if
720 * any of them are pointing to this new rule.
721 */
Denis V. Lunev76c72d42007-09-16 15:44:27 -0700722 list_for_each_entry(r, &ops->rules_list, list) {
Thomas Graf0947c9fe2007-03-26 17:14:15 -0700723 if (r->action == FR_ACT_GOTO &&
Gao feng561dac22011-09-11 15:36:05 +0000724 r->target == rule->pref &&
725 rtnl_dereference(r->ctarget) == NULL) {
Thomas Graf0947c9fe2007-03-26 17:14:15 -0700726 rcu_assign_pointer(r->ctarget, rule);
727 if (--ops->unresolved_rules == 0)
728 break;
729 }
730 }
731 }
732
733 if (rule->action == FR_ACT_GOTO)
734 ops->nr_goto_rules++;
735
736 if (unresolved)
737 ops->unresolved_rules++;
738
Thomas Grafe7030872015-07-21 10:44:01 +0200739 if (rule->tun_id)
740 ip_tunnel_need_metadata();
741
Eric W. Biederman15e47302012-09-07 20:12:54 +0000742 notify_rule_change(RTM_NEWRULE, rule, ops, nlh, NETLINK_CB(skb).portid);
Thomas Graf73417f62007-03-27 13:56:52 -0700743 flush_route_cache(ops);
Thomas Graf14c0b972006-08-04 03:38:38 -0700744 rules_ops_put(ops);
745 return 0;
746
747errout_free:
748 kfree(rule);
749errout:
750 rules_ops_put(ops);
751 return err;
752}
David Ahern96c63fa2016-06-08 10:55:39 -0700753EXPORT_SYMBOL_GPL(fib_nl_newrule);
Thomas Graf14c0b972006-08-04 03:38:38 -0700754
David Ahernc21ef3e2017-04-16 09:48:24 -0700755int fib_nl_delrule(struct sk_buff *skb, struct nlmsghdr *nlh,
756 struct netlink_ext_ack *extack)
Thomas Graf14c0b972006-08-04 03:38:38 -0700757{
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +0900758 struct net *net = sock_net(skb->sk);
Thomas Graf14c0b972006-08-04 03:38:38 -0700759 struct fib_rule_hdr *frh = nlmsg_data(nlh);
760 struct fib_rules_ops *ops = NULL;
Roopa Prabhuf9d4b0c2018-04-21 09:41:30 -0700761 struct fib_rule *rule = NULL, *r, *nlrule = NULL;
Thomas Graf14c0b972006-08-04 03:38:38 -0700762 struct nlattr *tb[FRA_MAX+1];
763 int err = -EINVAL;
Roopa Prabhuf9d4b0c2018-04-21 09:41:30 -0700764 bool user_priority = false;
Thomas Graf14c0b972006-08-04 03:38:38 -0700765
Roopa Prabhub16fb412018-04-21 09:41:31 -0700766 if (nlh->nlmsg_len < nlmsg_msg_size(sizeof(*frh))) {
767 NL_SET_ERR_MSG(extack, "Invalid msg length");
Thomas Graf14c0b972006-08-04 03:38:38 -0700768 goto errout;
Roopa Prabhub16fb412018-04-21 09:41:31 -0700769 }
Thomas Graf14c0b972006-08-04 03:38:38 -0700770
Denis V. Lunev5fd30ee2008-01-10 03:20:28 -0800771 ops = lookup_rules_ops(net, frh->family);
Thomas Graf14c0b972006-08-04 03:38:38 -0700772 if (ops == NULL) {
Patrick McHardy2fe195c2008-07-01 19:59:37 -0700773 err = -EAFNOSUPPORT;
Roopa Prabhub16fb412018-04-21 09:41:31 -0700774 NL_SET_ERR_MSG(extack, "Rule family not supported");
Thomas Graf14c0b972006-08-04 03:38:38 -0700775 goto errout;
776 }
777
David Ahernc21ef3e2017-04-16 09:48:24 -0700778 err = nlmsg_parse(nlh, sizeof(*frh), tb, FRA_MAX, ops->policy, extack);
Roopa Prabhub16fb412018-04-21 09:41:31 -0700779 if (err < 0) {
780 NL_SET_ERR_MSG(extack, "Error parsing msg");
Thomas Graf14c0b972006-08-04 03:38:38 -0700781 goto errout;
Roopa Prabhub16fb412018-04-21 09:41:31 -0700782 }
Thomas Graf14c0b972006-08-04 03:38:38 -0700783
Roopa Prabhuf9d4b0c2018-04-21 09:41:30 -0700784 err = fib_nl2rule(skb, nlh, extack, ops, tb, &nlrule, &user_priority);
785 if (err)
Thomas Grafe1701c62007-03-24 12:46:02 -0700786 goto errout;
787
Roopa Prabhuf9d4b0c2018-04-21 09:41:30 -0700788 rule = rule_find(ops, frh, tb, nlrule, user_priority);
789 if (!rule) {
790 err = -ENOENT;
791 goto errout;
Lorenzo Colitti622ec2c2016-11-04 02:23:42 +0900792 }
793
Roopa Prabhuf9d4b0c2018-04-21 09:41:30 -0700794 if (rule->flags & FIB_RULE_PERMANENT) {
795 err = -EPERM;
796 goto errout;
797 }
798
799 if (ops->delete) {
800 err = ops->delete(rule);
Roopa Prabhubfff4862018-02-28 22:40:16 -0500801 if (err)
802 goto errout;
803 }
804
Roopa Prabhuf9d4b0c2018-04-21 09:41:30 -0700805 if (rule->tun_id)
806 ip_tunnel_unneed_metadata();
807
808 list_del_rcu(&rule->list);
809
810 if (rule->action == FR_ACT_GOTO) {
811 ops->nr_goto_rules--;
812 if (rtnl_dereference(rule->ctarget) == NULL)
813 ops->unresolved_rules--;
Roopa Prabhubfff4862018-02-28 22:40:16 -0500814 }
815
Roopa Prabhuf9d4b0c2018-04-21 09:41:30 -0700816 /*
817 * Check if this rule is a target to any of them. If so,
818 * adjust to the next one with the same preference or
819 * disable them. As this operation is eventually very
820 * expensive, it is only performed if goto rules, except
821 * current if it is goto rule, have actually been added.
822 */
823 if (ops->nr_goto_rules > 0) {
824 struct fib_rule *n;
Donald Sharpcac56202018-02-20 08:55:58 -0500825
Roopa Prabhuf9d4b0c2018-04-21 09:41:30 -0700826 n = list_next_entry(rule, list);
827 if (&n->list == &ops->rules_list || n->pref != rule->pref)
828 n = NULL;
829 list_for_each_entry(r, &ops->rules_list, list) {
830 if (rtnl_dereference(r->ctarget) != rule)
831 continue;
832 rcu_assign_pointer(r->ctarget, n);
833 if (!n)
834 ops->unresolved_rules++;
Thomas Graf14c0b972006-08-04 03:38:38 -0700835 }
Thomas Graf14c0b972006-08-04 03:38:38 -0700836 }
837
Roopa Prabhuf9d4b0c2018-04-21 09:41:30 -0700838 call_fib_rule_notifiers(net, FIB_EVENT_RULE_DEL, rule, ops,
839 NULL);
840 notify_rule_change(RTM_DELRULE, rule, ops, nlh,
841 NETLINK_CB(skb).portid);
842 fib_rule_put(rule);
843 flush_route_cache(ops);
844 rules_ops_put(ops);
845 kfree(nlrule);
846 return 0;
847
Thomas Graf14c0b972006-08-04 03:38:38 -0700848errout:
Roopa Prabhuf9d4b0c2018-04-21 09:41:30 -0700849 if (nlrule)
850 kfree(nlrule);
Thomas Graf14c0b972006-08-04 03:38:38 -0700851 rules_ops_put(ops);
852 return err;
853}
David Ahern96c63fa2016-06-08 10:55:39 -0700854EXPORT_SYMBOL_GPL(fib_nl_delrule);
Thomas Graf14c0b972006-08-04 03:38:38 -0700855
Thomas Graf339bf982006-11-10 14:10:15 -0800856static inline size_t fib_rule_nlmsg_size(struct fib_rules_ops *ops,
857 struct fib_rule *rule)
858{
859 size_t payload = NLMSG_ALIGN(sizeof(struct fib_rule_hdr))
Patrick McHardy491deb22009-12-03 01:25:54 +0000860 + nla_total_size(IFNAMSIZ) /* FRA_IIFNAME */
Patrick McHardy1b038a52009-12-03 01:25:56 +0000861 + nla_total_size(IFNAMSIZ) /* FRA_OIFNAME */
Thomas Graf339bf982006-11-10 14:10:15 -0800862 + nla_total_size(4) /* FRA_PRIORITY */
863 + nla_total_size(4) /* FRA_TABLE */
Stefan Tomanek73f56982013-08-03 14:14:43 +0200864 + nla_total_size(4) /* FRA_SUPPRESS_PREFIXLEN */
Stefan Tomanek6ef94cf2013-08-02 17:19:56 +0200865 + nla_total_size(4) /* FRA_SUPPRESS_IFGROUP */
Thomas Graf339bf982006-11-10 14:10:15 -0800866 + nla_total_size(4) /* FRA_FWMARK */
Thomas Grafe7030872015-07-21 10:44:01 +0200867 + nla_total_size(4) /* FRA_FWMASK */
Lorenzo Colitti622ec2c2016-11-04 02:23:42 +0900868 + nla_total_size_64bit(8) /* FRA_TUN_ID */
Donald Sharp1b71af62018-02-23 14:01:52 -0500869 + nla_total_size(sizeof(struct fib_kuid_range))
Roopa Prabhubfff4862018-02-28 22:40:16 -0500870 + nla_total_size(1) /* FRA_PROTOCOL */
871 + nla_total_size(1) /* FRA_IP_PROTO */
872 + nla_total_size(sizeof(struct fib_rule_port_range)) /* FRA_SPORT_RANGE */
873 + nla_total_size(sizeof(struct fib_rule_port_range)); /* FRA_DPORT_RANGE */
Thomas Graf339bf982006-11-10 14:10:15 -0800874
875 if (ops->nlmsg_payload)
876 payload += ops->nlmsg_payload(rule);
877
878 return payload;
879}
880
Thomas Graf14c0b972006-08-04 03:38:38 -0700881static int fib_nl_fill_rule(struct sk_buff *skb, struct fib_rule *rule,
882 u32 pid, u32 seq, int type, int flags,
883 struct fib_rules_ops *ops)
884{
885 struct nlmsghdr *nlh;
886 struct fib_rule_hdr *frh;
887
888 nlh = nlmsg_put(skb, pid, seq, type, sizeof(*frh), flags);
889 if (nlh == NULL)
Patrick McHardy26932562007-01-31 23:16:40 -0800890 return -EMSGSIZE;
Thomas Graf14c0b972006-08-04 03:38:38 -0700891
892 frh = nlmsg_data(nlh);
Patrick McHardy28bb1722010-04-13 05:03:16 +0000893 frh->family = ops->family;
Thomas Graf14c0b972006-08-04 03:38:38 -0700894 frh->table = rule->table;
David S. Miller0e3cea72012-04-01 20:47:01 -0400895 if (nla_put_u32(skb, FRA_TABLE, rule->table))
896 goto nla_put_failure;
Stefan Tomanek73f56982013-08-03 14:14:43 +0200897 if (nla_put_u32(skb, FRA_SUPPRESS_PREFIXLEN, rule->suppress_prefixlen))
Stefan Tomanek7764a452013-08-01 02:17:15 +0200898 goto nla_put_failure;
Thomas Graf14c0b972006-08-04 03:38:38 -0700899 frh->res1 = 0;
Donald Sharp1b71af62018-02-23 14:01:52 -0500900 frh->res2 = 0;
Thomas Graf14c0b972006-08-04 03:38:38 -0700901 frh->action = rule->action;
902 frh->flags = rule->flags;
Donald Sharp1b71af62018-02-23 14:01:52 -0500903
904 if (nla_put_u8(skb, FRA_PROTOCOL, rule->proto))
905 goto nla_put_failure;
Thomas Graf14c0b972006-08-04 03:38:38 -0700906
Eric Dumazet7a2b03c2010-10-26 09:24:55 +0000907 if (rule->action == FR_ACT_GOTO &&
Eric Dumazet33d480c2011-08-11 19:30:52 +0000908 rcu_access_pointer(rule->ctarget) == NULL)
Thomas Graf0947c9fe2007-03-26 17:14:15 -0700909 frh->flags |= FIB_RULE_UNRESOLVED;
910
Patrick McHardy491deb22009-12-03 01:25:54 +0000911 if (rule->iifname[0]) {
David S. Miller0e3cea72012-04-01 20:47:01 -0400912 if (nla_put_string(skb, FRA_IIFNAME, rule->iifname))
913 goto nla_put_failure;
Patrick McHardy491deb22009-12-03 01:25:54 +0000914 if (rule->iifindex == -1)
915 frh->flags |= FIB_RULE_IIF_DETACHED;
Thomas Graf2b443682007-03-26 17:37:59 -0700916 }
917
Patrick McHardy1b038a52009-12-03 01:25:56 +0000918 if (rule->oifname[0]) {
David S. Miller0e3cea72012-04-01 20:47:01 -0400919 if (nla_put_string(skb, FRA_OIFNAME, rule->oifname))
920 goto nla_put_failure;
Patrick McHardy1b038a52009-12-03 01:25:56 +0000921 if (rule->oifindex == -1)
922 frh->flags |= FIB_RULE_OIF_DETACHED;
923 }
924
David S. Miller0e3cea72012-04-01 20:47:01 -0400925 if ((rule->pref &&
926 nla_put_u32(skb, FRA_PRIORITY, rule->pref)) ||
927 (rule->mark &&
928 nla_put_u32(skb, FRA_FWMARK, rule->mark)) ||
929 ((rule->mark_mask || rule->mark) &&
930 nla_put_u32(skb, FRA_FWMASK, rule->mark_mask)) ||
931 (rule->target &&
Thomas Grafe7030872015-07-21 10:44:01 +0200932 nla_put_u32(skb, FRA_GOTO, rule->target)) ||
933 (rule->tun_id &&
David Ahern96c63fa2016-06-08 10:55:39 -0700934 nla_put_be64(skb, FRA_TUN_ID, rule->tun_id, FRA_PAD)) ||
935 (rule->l3mdev &&
Lorenzo Colitti622ec2c2016-11-04 02:23:42 +0900936 nla_put_u8(skb, FRA_L3MDEV, rule->l3mdev)) ||
937 (uid_range_set(&rule->uid_range) &&
Roopa Prabhubfff4862018-02-28 22:40:16 -0500938 nla_put_uid_range(skb, &rule->uid_range)) ||
939 (fib_rule_port_range_set(&rule->sport_range) &&
940 nla_put_port_range(skb, FRA_SPORT_RANGE, &rule->sport_range)) ||
941 (fib_rule_port_range_set(&rule->dport_range) &&
942 nla_put_port_range(skb, FRA_DPORT_RANGE, &rule->dport_range)) ||
943 (rule->ip_proto && nla_put_u8(skb, FRA_IP_PROTO, rule->ip_proto)))
David S. Miller0e3cea72012-04-01 20:47:01 -0400944 goto nla_put_failure;
Stefan Tomanek6ef94cf2013-08-02 17:19:56 +0200945
946 if (rule->suppress_ifgroup != -1) {
947 if (nla_put_u32(skb, FRA_SUPPRESS_IFGROUP, rule->suppress_ifgroup))
948 goto nla_put_failure;
949 }
950
Rami Rosen04af8cf2009-05-20 17:26:23 -0700951 if (ops->fill(rule, skb, frh) < 0)
Thomas Graf14c0b972006-08-04 03:38:38 -0700952 goto nla_put_failure;
953
Johannes Berg053c0952015-01-16 22:09:00 +0100954 nlmsg_end(skb, nlh);
955 return 0;
Thomas Graf14c0b972006-08-04 03:38:38 -0700956
957nla_put_failure:
Patrick McHardy26932562007-01-31 23:16:40 -0800958 nlmsg_cancel(skb, nlh);
959 return -EMSGSIZE;
Thomas Graf14c0b972006-08-04 03:38:38 -0700960}
961
Thomas Grafc4546732007-03-25 23:24:24 -0700962static int dump_rules(struct sk_buff *skb, struct netlink_callback *cb,
963 struct fib_rules_ops *ops)
Thomas Graf14c0b972006-08-04 03:38:38 -0700964{
965 int idx = 0;
966 struct fib_rule *rule;
Wilson Kok41fc0142015-09-22 21:40:22 -0700967 int err = 0;
Thomas Graf14c0b972006-08-04 03:38:38 -0700968
Eric Dumazete67f88d2011-04-27 22:56:07 +0000969 rcu_read_lock();
970 list_for_each_entry_rcu(rule, &ops->rules_list, list) {
Thomas Grafc4546732007-03-25 23:24:24 -0700971 if (idx < cb->args[1])
Thomas Graf14c0b972006-08-04 03:38:38 -0700972 goto skip;
973
Wilson Kok41fc0142015-09-22 21:40:22 -0700974 err = fib_nl_fill_rule(skb, rule, NETLINK_CB(cb->skb).portid,
975 cb->nlh->nlmsg_seq, RTM_NEWRULE,
976 NLM_F_MULTI, ops);
977 if (err)
Thomas Graf14c0b972006-08-04 03:38:38 -0700978 break;
979skip:
980 idx++;
981 }
Eric Dumazet2907c352011-05-25 07:34:04 +0000982 rcu_read_unlock();
Thomas Grafc4546732007-03-25 23:24:24 -0700983 cb->args[1] = idx;
Thomas Graf14c0b972006-08-04 03:38:38 -0700984 rules_ops_put(ops);
985
Wilson Kok41fc0142015-09-22 21:40:22 -0700986 return err;
Thomas Graf14c0b972006-08-04 03:38:38 -0700987}
988
Thomas Grafc4546732007-03-25 23:24:24 -0700989static int fib_nl_dumprule(struct sk_buff *skb, struct netlink_callback *cb)
990{
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +0900991 struct net *net = sock_net(skb->sk);
Thomas Grafc4546732007-03-25 23:24:24 -0700992 struct fib_rules_ops *ops;
993 int idx = 0, family;
994
995 family = rtnl_msg_family(cb->nlh);
996 if (family != AF_UNSPEC) {
997 /* Protocol specific dump request */
Denis V. Lunev5fd30ee2008-01-10 03:20:28 -0800998 ops = lookup_rules_ops(net, family);
Thomas Grafc4546732007-03-25 23:24:24 -0700999 if (ops == NULL)
1000 return -EAFNOSUPPORT;
1001
Wilson Kok41fc0142015-09-22 21:40:22 -07001002 dump_rules(skb, cb, ops);
1003
1004 return skb->len;
Thomas Grafc4546732007-03-25 23:24:24 -07001005 }
1006
1007 rcu_read_lock();
Denis V. Lunev5fd30ee2008-01-10 03:20:28 -08001008 list_for_each_entry_rcu(ops, &net->rules_ops, list) {
Thomas Grafc4546732007-03-25 23:24:24 -07001009 if (idx < cb->args[0] || !try_module_get(ops->owner))
1010 goto skip;
1011
1012 if (dump_rules(skb, cb, ops) < 0)
1013 break;
1014
1015 cb->args[1] = 0;
Eric Dumazet2fb35732010-03-09 20:03:38 +00001016skip:
Thomas Grafc4546732007-03-25 23:24:24 -07001017 idx++;
1018 }
1019 rcu_read_unlock();
1020 cb->args[0] = idx;
1021
1022 return skb->len;
1023}
Thomas Graf14c0b972006-08-04 03:38:38 -07001024
Denis V. Lunev9e3a5482008-01-20 16:46:41 -08001025static void notify_rule_change(int event, struct fib_rule *rule,
Thomas Grafc17084d2006-08-15 00:32:48 -07001026 struct fib_rules_ops *ops, struct nlmsghdr *nlh,
1027 u32 pid)
Thomas Graf14c0b972006-08-04 03:38:38 -07001028{
Denis V. Lunev9e3a5482008-01-20 16:46:41 -08001029 struct net *net;
Thomas Grafc17084d2006-08-15 00:32:48 -07001030 struct sk_buff *skb;
1031 int err = -ENOBUFS;
Thomas Graf14c0b972006-08-04 03:38:38 -07001032
Denis V. Lunev9e3a5482008-01-20 16:46:41 -08001033 net = ops->fro_net;
Thomas Graf339bf982006-11-10 14:10:15 -08001034 skb = nlmsg_new(fib_rule_nlmsg_size(ops, rule), GFP_KERNEL);
Thomas Graf14c0b972006-08-04 03:38:38 -07001035 if (skb == NULL)
Thomas Grafc17084d2006-08-15 00:32:48 -07001036 goto errout;
1037
1038 err = fib_nl_fill_rule(skb, rule, pid, nlh->nlmsg_seq, event, 0, ops);
Patrick McHardy26932562007-01-31 23:16:40 -08001039 if (err < 0) {
1040 /* -EMSGSIZE implies BUG in fib_rule_nlmsg_size() */
1041 WARN_ON(err == -EMSGSIZE);
1042 kfree_skb(skb);
1043 goto errout;
1044 }
Denis V. Lunev9e3a5482008-01-20 16:46:41 -08001045
Pablo Neira Ayuso1ce85fe2009-02-24 23:18:28 -08001046 rtnl_notify(skb, net, pid, ops->nlgroup, nlh, GFP_KERNEL);
1047 return;
Thomas Grafc17084d2006-08-15 00:32:48 -07001048errout:
1049 if (err < 0)
Denis V. Lunev5fd30ee2008-01-10 03:20:28 -08001050 rtnl_set_sk_err(net, ops->nlgroup, err);
Thomas Graf14c0b972006-08-04 03:38:38 -07001051}
1052
1053static void attach_rules(struct list_head *rules, struct net_device *dev)
1054{
1055 struct fib_rule *rule;
1056
1057 list_for_each_entry(rule, rules, list) {
Patrick McHardy491deb22009-12-03 01:25:54 +00001058 if (rule->iifindex == -1 &&
1059 strcmp(dev->name, rule->iifname) == 0)
1060 rule->iifindex = dev->ifindex;
Patrick McHardy1b038a52009-12-03 01:25:56 +00001061 if (rule->oifindex == -1 &&
1062 strcmp(dev->name, rule->oifname) == 0)
1063 rule->oifindex = dev->ifindex;
Thomas Graf14c0b972006-08-04 03:38:38 -07001064 }
1065}
1066
1067static void detach_rules(struct list_head *rules, struct net_device *dev)
1068{
1069 struct fib_rule *rule;
1070
Patrick McHardy1b038a52009-12-03 01:25:56 +00001071 list_for_each_entry(rule, rules, list) {
Patrick McHardy491deb22009-12-03 01:25:54 +00001072 if (rule->iifindex == dev->ifindex)
1073 rule->iifindex = -1;
Patrick McHardy1b038a52009-12-03 01:25:56 +00001074 if (rule->oifindex == dev->ifindex)
1075 rule->oifindex = -1;
1076 }
Thomas Graf14c0b972006-08-04 03:38:38 -07001077}
1078
1079
1080static int fib_rules_event(struct notifier_block *this, unsigned long event,
Jiri Pirko351638e2013-05-28 01:30:21 +00001081 void *ptr)
Thomas Graf14c0b972006-08-04 03:38:38 -07001082{
Jiri Pirko351638e2013-05-28 01:30:21 +00001083 struct net_device *dev = netdev_notifier_info_to_dev(ptr);
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +09001084 struct net *net = dev_net(dev);
Thomas Graf14c0b972006-08-04 03:38:38 -07001085 struct fib_rules_ops *ops;
1086
Eric Dumazet748e2d92012-08-22 21:50:59 +00001087 ASSERT_RTNL();
Thomas Graf14c0b972006-08-04 03:38:38 -07001088
1089 switch (event) {
1090 case NETDEV_REGISTER:
Denis V. Lunev5fd30ee2008-01-10 03:20:28 -08001091 list_for_each_entry(ops, &net->rules_ops, list)
Denis V. Lunev76c72d42007-09-16 15:44:27 -07001092 attach_rules(&ops->rules_list, dev);
Thomas Graf14c0b972006-08-04 03:38:38 -07001093 break;
1094
Maciej Żenczykowski946c0322014-02-07 16:23:48 -08001095 case NETDEV_CHANGENAME:
1096 list_for_each_entry(ops, &net->rules_ops, list) {
1097 detach_rules(&ops->rules_list, dev);
1098 attach_rules(&ops->rules_list, dev);
1099 }
1100 break;
1101
Thomas Graf14c0b972006-08-04 03:38:38 -07001102 case NETDEV_UNREGISTER:
Denis V. Lunev5fd30ee2008-01-10 03:20:28 -08001103 list_for_each_entry(ops, &net->rules_ops, list)
Denis V. Lunev76c72d42007-09-16 15:44:27 -07001104 detach_rules(&ops->rules_list, dev);
Thomas Graf14c0b972006-08-04 03:38:38 -07001105 break;
1106 }
1107
Thomas Graf14c0b972006-08-04 03:38:38 -07001108 return NOTIFY_DONE;
1109}
1110
1111static struct notifier_block fib_rules_notifier = {
1112 .notifier_call = fib_rules_event,
1113};
1114
Alexey Dobriyan2c8c1e72010-01-17 03:35:32 +00001115static int __net_init fib_rules_net_init(struct net *net)
Denis V. Lunev5fd30ee2008-01-10 03:20:28 -08001116{
1117 INIT_LIST_HEAD(&net->rules_ops);
1118 spin_lock_init(&net->rules_mod_lock);
1119 return 0;
1120}
1121
Vasily Averince2b7db2017-11-12 22:30:01 +03001122static void __net_exit fib_rules_net_exit(struct net *net)
1123{
1124 WARN_ON_ONCE(!list_empty(&net->rules_ops));
1125}
1126
Denis V. Lunev5fd30ee2008-01-10 03:20:28 -08001127static struct pernet_operations fib_rules_net_ops = {
1128 .init = fib_rules_net_init,
Vasily Averince2b7db2017-11-12 22:30:01 +03001129 .exit = fib_rules_net_exit,
Denis V. Lunev5fd30ee2008-01-10 03:20:28 -08001130};
1131
Thomas Graf14c0b972006-08-04 03:38:38 -07001132static int __init fib_rules_init(void)
1133{
Denis V. Lunev5fd30ee2008-01-10 03:20:28 -08001134 int err;
Florian Westphalb97bac62017-08-09 20:41:48 +02001135 rtnl_register(PF_UNSPEC, RTM_NEWRULE, fib_nl_newrule, NULL, 0);
1136 rtnl_register(PF_UNSPEC, RTM_DELRULE, fib_nl_delrule, NULL, 0);
1137 rtnl_register(PF_UNSPEC, RTM_GETRULE, NULL, fib_nl_dumprule, 0);
Thomas Graf9d9e6a52007-03-25 23:20:05 -07001138
Eric W. Biederman5d6d4802008-11-07 22:52:34 -08001139 err = register_pernet_subsys(&fib_rules_net_ops);
Denis V. Lunev5fd30ee2008-01-10 03:20:28 -08001140 if (err < 0)
1141 goto fail;
1142
Eric W. Biederman5d6d4802008-11-07 22:52:34 -08001143 err = register_netdevice_notifier(&fib_rules_notifier);
Denis V. Lunev5fd30ee2008-01-10 03:20:28 -08001144 if (err < 0)
1145 goto fail_unregister;
Eric W. Biederman5d6d4802008-11-07 22:52:34 -08001146
Denis V. Lunev5fd30ee2008-01-10 03:20:28 -08001147 return 0;
1148
1149fail_unregister:
Eric W. Biederman5d6d4802008-11-07 22:52:34 -08001150 unregister_pernet_subsys(&fib_rules_net_ops);
Denis V. Lunev5fd30ee2008-01-10 03:20:28 -08001151fail:
1152 rtnl_unregister(PF_UNSPEC, RTM_NEWRULE);
1153 rtnl_unregister(PF_UNSPEC, RTM_DELRULE);
1154 rtnl_unregister(PF_UNSPEC, RTM_GETRULE);
1155 return err;
Thomas Graf14c0b972006-08-04 03:38:38 -07001156}
1157
1158subsys_initcall(fib_rules_init);