blob: 31c4041f758652b4583a84d70e16992ce515c392 [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 Colitti2ec93fe2016-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
Denis V. Lunev2994c632007-11-10 22:12:03 -080026int fib_default_rule_add(struct fib_rules_ops *ops,
27 u32 pref, u32 table, u32 flags)
28{
29 struct fib_rule *r;
30
31 r = kzalloc(ops->rule_size, GFP_KERNEL);
32 if (r == NULL)
33 return -ENOMEM;
34
35 atomic_set(&r->refcnt, 1);
36 r->action = FR_ACT_TO_TBL;
37 r->pref = pref;
38 r->table = table;
39 r->flags = flags;
Eric W. Biedermanefd7ef12015-03-11 23:04:08 -050040 r->fr_net = ops->fro_net;
Lorenzo Colitti2ec93fe2016-11-04 02:23:42 +090041 r->uid_range = fib_kuid_range_unset;
Denis V. Lunev2994c632007-11-10 22:12:03 -080042
Stefan Tomanek73f56982013-08-03 14:14:43 +020043 r->suppress_prefixlen = -1;
44 r->suppress_ifgroup = -1;
45
Denis V. Lunev2994c632007-11-10 22:12:03 -080046 /* The lock is not required here, the list in unreacheable
47 * at the moment this function is called */
48 list_add_tail(&r->list, &ops->rules_list);
49 return 0;
50}
51EXPORT_SYMBOL(fib_default_rule_add);
52
Phil Sutterf53de1e2015-09-09 14:20:56 +020053static u32 fib_default_rule_pref(struct fib_rules_ops *ops)
Patrick McHardyd8a566b2010-04-13 05:03:15 +000054{
55 struct list_head *pos;
56 struct fib_rule *rule;
57
58 if (!list_empty(&ops->rules_list)) {
59 pos = ops->rules_list.next;
60 if (pos->next != &ops->rules_list) {
61 rule = list_entry(pos->next, struct fib_rule, list);
62 if (rule->pref)
63 return rule->pref - 1;
64 }
65 }
66
67 return 0;
68}
Patrick McHardyd8a566b2010-04-13 05:03:15 +000069
Denis V. Lunev9e3a5482008-01-20 16:46:41 -080070static void notify_rule_change(int event, struct fib_rule *rule,
Thomas Grafc17084d2006-08-15 00:32:48 -070071 struct fib_rules_ops *ops, struct nlmsghdr *nlh,
72 u32 pid);
Thomas Graf14c0b972006-08-04 03:38:38 -070073
Denis V. Lunev5fd30ee2008-01-10 03:20:28 -080074static struct fib_rules_ops *lookup_rules_ops(struct net *net, int family)
Thomas Graf14c0b972006-08-04 03:38:38 -070075{
76 struct fib_rules_ops *ops;
77
78 rcu_read_lock();
Denis V. Lunev5fd30ee2008-01-10 03:20:28 -080079 list_for_each_entry_rcu(ops, &net->rules_ops, list) {
Thomas Graf14c0b972006-08-04 03:38:38 -070080 if (ops->family == family) {
81 if (!try_module_get(ops->owner))
82 ops = NULL;
83 rcu_read_unlock();
84 return ops;
85 }
86 }
87 rcu_read_unlock();
88
89 return NULL;
90}
91
92static void rules_ops_put(struct fib_rules_ops *ops)
93{
94 if (ops)
95 module_put(ops->owner);
96}
97
Thomas Graf73417f62007-03-27 13:56:52 -070098static void flush_route_cache(struct fib_rules_ops *ops)
99{
100 if (ops->flush_cache)
Denis V. Lunevae299fc2008-07-05 19:01:28 -0700101 ops->flush_cache(ops);
Thomas Graf73417f62007-03-27 13:56:52 -0700102}
103
Eric W. Biedermane9c51582009-12-03 12:22:55 -0800104static int __fib_rules_register(struct fib_rules_ops *ops)
Thomas Graf14c0b972006-08-04 03:38:38 -0700105{
106 int err = -EEXIST;
107 struct fib_rules_ops *o;
Denis V. Lunev9e3a5482008-01-20 16:46:41 -0800108 struct net *net;
109
110 net = ops->fro_net;
Thomas Graf14c0b972006-08-04 03:38:38 -0700111
112 if (ops->rule_size < sizeof(struct fib_rule))
113 return -EINVAL;
114
115 if (ops->match == NULL || ops->configure == NULL ||
116 ops->compare == NULL || ops->fill == NULL ||
117 ops->action == NULL)
118 return -EINVAL;
119
Denis V. Lunev5fd30ee2008-01-10 03:20:28 -0800120 spin_lock(&net->rules_mod_lock);
121 list_for_each_entry(o, &net->rules_ops, list)
Thomas Graf14c0b972006-08-04 03:38:38 -0700122 if (ops->family == o->family)
123 goto errout;
124
Denis V. Lunev5fd30ee2008-01-10 03:20:28 -0800125 list_add_tail_rcu(&ops->list, &net->rules_ops);
Thomas Graf14c0b972006-08-04 03:38:38 -0700126 err = 0;
127errout:
Denis V. Lunev5fd30ee2008-01-10 03:20:28 -0800128 spin_unlock(&net->rules_mod_lock);
Thomas Graf14c0b972006-08-04 03:38:38 -0700129
130 return err;
131}
132
Eric W. Biedermane9c51582009-12-03 12:22:55 -0800133struct fib_rules_ops *
Patrick McHardy3d0c9c42010-04-26 16:02:04 +0200134fib_rules_register(const struct fib_rules_ops *tmpl, struct net *net)
Eric W. Biedermane9c51582009-12-03 12:22:55 -0800135{
136 struct fib_rules_ops *ops;
137 int err;
138
Eric Dumazet2fb35732010-03-09 20:03:38 +0000139 ops = kmemdup(tmpl, sizeof(*ops), GFP_KERNEL);
Eric W. Biedermane9c51582009-12-03 12:22:55 -0800140 if (ops == NULL)
141 return ERR_PTR(-ENOMEM);
142
143 INIT_LIST_HEAD(&ops->rules_list);
144 ops->fro_net = net;
145
146 err = __fib_rules_register(ops);
147 if (err) {
148 kfree(ops);
149 ops = ERR_PTR(err);
150 }
151
152 return ops;
153}
Thomas Graf14c0b972006-08-04 03:38:38 -0700154EXPORT_SYMBOL_GPL(fib_rules_register);
155
stephen hemminger1df99162010-10-04 20:14:17 +0000156static void fib_rules_cleanup_ops(struct fib_rules_ops *ops)
Thomas Graf14c0b972006-08-04 03:38:38 -0700157{
158 struct fib_rule *rule, *tmp;
159
Denis V. Lunev76c72d42007-09-16 15:44:27 -0700160 list_for_each_entry_safe(rule, tmp, &ops->rules_list, list) {
Thomas Graf14c0b972006-08-04 03:38:38 -0700161 list_del_rcu(&rule->list);
David S. Miller7a9bc9b2012-06-29 01:32:45 -0700162 if (ops->delete)
163 ops->delete(rule);
Thomas Graf14c0b972006-08-04 03:38:38 -0700164 fib_rule_put(rule);
165 }
166}
167
Denis V. Lunev9e3a5482008-01-20 16:46:41 -0800168void fib_rules_unregister(struct fib_rules_ops *ops)
Thomas Graf14c0b972006-08-04 03:38:38 -0700169{
Denis V. Lunev9e3a5482008-01-20 16:46:41 -0800170 struct net *net = ops->fro_net;
Thomas Graf14c0b972006-08-04 03:38:38 -0700171
Denis V. Lunev5fd30ee2008-01-10 03:20:28 -0800172 spin_lock(&net->rules_mod_lock);
Denis V. Lunev72132c1b2008-01-14 22:59:30 -0800173 list_del_rcu(&ops->list);
Denis V. Lunev5fd30ee2008-01-10 03:20:28 -0800174 spin_unlock(&net->rules_mod_lock);
Thomas Graf14c0b972006-08-04 03:38:38 -0700175
WANG Cong419df122015-03-31 11:01:46 -0700176 fib_rules_cleanup_ops(ops);
Eric W. Biedermanefd7ef12015-03-11 23:04:08 -0500177 kfree_rcu(ops, rcu);
Thomas Graf14c0b972006-08-04 03:38:38 -0700178}
Thomas Graf14c0b972006-08-04 03:38:38 -0700179EXPORT_SYMBOL_GPL(fib_rules_unregister);
180
Lorenzo Colitti2ec93fe2016-11-04 02:23:42 +0900181static int uid_range_set(struct fib_kuid_range *range)
182{
183 return uid_valid(range->start) && uid_valid(range->end);
184}
185
186static struct fib_kuid_range nla_get_kuid_range(struct nlattr **tb)
187{
188 struct fib_rule_uid_range *in;
189 struct fib_kuid_range out;
190
191 in = (struct fib_rule_uid_range *)nla_data(tb[FRA_UID_RANGE]);
192
193 out.start = make_kuid(current_user_ns(), in->start);
194 out.end = make_kuid(current_user_ns(), in->end);
195
196 return out;
197}
198
199static int nla_put_uid_range(struct sk_buff *skb, struct fib_kuid_range *range)
200{
201 struct fib_rule_uid_range out = {
202 from_kuid_munged(current_user_ns(), range->start),
203 from_kuid_munged(current_user_ns(), range->end)
204 };
205
206 return nla_put(skb, FRA_UID_RANGE, sizeof(out), &out);
207}
208
Thomas Graf3dfbcc42006-11-09 15:23:20 -0800209static int fib_rule_match(struct fib_rule *rule, struct fib_rules_ops *ops,
David Ahern96c63fa2016-06-08 10:55:39 -0700210 struct flowi *fl, int flags,
211 struct fib_lookup_arg *arg)
Thomas Graf3dfbcc42006-11-09 15:23:20 -0800212{
213 int ret = 0;
214
David S. Miller1d28f422011-03-12 00:29:39 -0500215 if (rule->iifindex && (rule->iifindex != fl->flowi_iif))
Thomas Graf3dfbcc42006-11-09 15:23:20 -0800216 goto out;
217
David S. Miller1d28f422011-03-12 00:29:39 -0500218 if (rule->oifindex && (rule->oifindex != fl->flowi_oif))
Patrick McHardy1b038a52009-12-03 01:25:56 +0000219 goto out;
220
David S. Miller1d28f422011-03-12 00:29:39 -0500221 if ((rule->mark ^ fl->flowi_mark) & rule->mark_mask)
Thomas Graf3dfbcc42006-11-09 15:23:20 -0800222 goto out;
223
Thomas Grafe7030872015-07-21 10:44:01 +0200224 if (rule->tun_id && (rule->tun_id != fl->flowi_tun_key.tun_id))
225 goto out;
226
David Ahern96c63fa2016-06-08 10:55:39 -0700227 if (rule->l3mdev && !l3mdev_fib_rule_match(rule->fr_net, fl, arg))
228 goto out;
229
Lorenzo Colitti2ec93fe2016-11-04 02:23:42 +0900230 if (uid_lt(fl->flowi_uid, rule->uid_range.start) ||
231 uid_gt(fl->flowi_uid, rule->uid_range.end))
232 goto out;
233
Thomas Graf3dfbcc42006-11-09 15:23:20 -0800234 ret = ops->match(rule, fl, flags);
235out:
236 return (rule->flags & FIB_RULE_INVERT) ? !ret : ret;
237}
238
Thomas Graf14c0b972006-08-04 03:38:38 -0700239int fib_rules_lookup(struct fib_rules_ops *ops, struct flowi *fl,
240 int flags, struct fib_lookup_arg *arg)
241{
242 struct fib_rule *rule;
243 int err;
244
245 rcu_read_lock();
246
Denis V. Lunev76c72d42007-09-16 15:44:27 -0700247 list_for_each_entry_rcu(rule, &ops->rules_list, list) {
Thomas Graf0947c9fe2007-03-26 17:14:15 -0700248jumped:
David Ahern96c63fa2016-06-08 10:55:39 -0700249 if (!fib_rule_match(rule, ops, fl, flags, arg))
Thomas Graf14c0b972006-08-04 03:38:38 -0700250 continue;
251
Thomas Graf0947c9fe2007-03-26 17:14:15 -0700252 if (rule->action == FR_ACT_GOTO) {
253 struct fib_rule *target;
254
255 target = rcu_dereference(rule->ctarget);
256 if (target == NULL) {
257 continue;
258 } else {
259 rule = target;
260 goto jumped;
261 }
Thomas Graffa0b2d12007-03-26 17:38:53 -0700262 } else if (rule->action == FR_ACT_NOP)
263 continue;
264 else
Thomas Graf0947c9fe2007-03-26 17:14:15 -0700265 err = ops->action(rule, fl, flags, arg);
266
Stefan Tomanek7764a452013-08-01 02:17:15 +0200267 if (!err && ops->suppress && ops->suppress(rule, arg))
268 continue;
269
Thomas Graf14c0b972006-08-04 03:38:38 -0700270 if (err != -EAGAIN) {
Eric Dumazetebc0ffa2010-10-05 10:41:36 +0000271 if ((arg->flags & FIB_LOOKUP_NOREF) ||
272 likely(atomic_inc_not_zero(&rule->refcnt))) {
Eric Dumazet7fa7cb72010-09-27 04:18:27 +0000273 arg->rule = rule;
274 goto out;
275 }
276 break;
Thomas Graf14c0b972006-08-04 03:38:38 -0700277 }
278 }
279
Steven Whitehouse83886b62007-03-30 13:34:27 -0700280 err = -ESRCH;
Thomas Graf14c0b972006-08-04 03:38:38 -0700281out:
282 rcu_read_unlock();
283
284 return err;
285}
Thomas Graf14c0b972006-08-04 03:38:38 -0700286EXPORT_SYMBOL_GPL(fib_rules_lookup);
287
Thomas Grafe1701c62007-03-24 12:46:02 -0700288static int validate_rulemsg(struct fib_rule_hdr *frh, struct nlattr **tb,
289 struct fib_rules_ops *ops)
290{
291 int err = -EINVAL;
292
293 if (frh->src_len)
294 if (tb[FRA_SRC] == NULL ||
295 frh->src_len > (ops->addr_size * 8) ||
296 nla_len(tb[FRA_SRC]) != ops->addr_size)
297 goto errout;
298
299 if (frh->dst_len)
300 if (tb[FRA_DST] == NULL ||
301 frh->dst_len > (ops->addr_size * 8) ||
302 nla_len(tb[FRA_DST]) != ops->addr_size)
303 goto errout;
304
305 err = 0;
306errout:
307 return err;
308}
309
Mateusz Bajorski153380e2016-06-29 09:22:10 +0200310static int rule_exists(struct fib_rules_ops *ops, struct fib_rule_hdr *frh,
311 struct nlattr **tb, struct fib_rule *rule)
312{
313 struct fib_rule *r;
314
315 list_for_each_entry(r, &ops->rules_list, list) {
316 if (r->action != rule->action)
317 continue;
318
319 if (r->table != rule->table)
320 continue;
321
322 if (r->pref != rule->pref)
323 continue;
324
325 if (memcmp(r->iifname, rule->iifname, IFNAMSIZ))
326 continue;
327
328 if (memcmp(r->oifname, rule->oifname, IFNAMSIZ))
329 continue;
330
331 if (r->mark != rule->mark)
332 continue;
333
334 if (r->mark_mask != rule->mark_mask)
335 continue;
336
337 if (r->tun_id != rule->tun_id)
338 continue;
339
340 if (r->fr_net != rule->fr_net)
341 continue;
342
343 if (r->l3mdev != rule->l3mdev)
344 continue;
345
Lorenzo Colittib6cfac72016-11-07 00:16:25 +0900346 if (!uid_eq(r->uid_range.start, rule->uid_range.start) ||
347 !uid_eq(r->uid_range.end, rule->uid_range.end))
348 continue;
349
Mateusz Bajorski153380e2016-06-29 09:22:10 +0200350 if (!ops->compare(r, frh, tb))
351 continue;
352 return 1;
353 }
354 return 0;
355}
356
David Ahern96c63fa2016-06-08 10:55:39 -0700357int fib_nl_newrule(struct sk_buff *skb, struct nlmsghdr *nlh)
Thomas Graf14c0b972006-08-04 03:38:38 -0700358{
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +0900359 struct net *net = sock_net(skb->sk);
Thomas Graf14c0b972006-08-04 03:38:38 -0700360 struct fib_rule_hdr *frh = nlmsg_data(nlh);
361 struct fib_rules_ops *ops = NULL;
362 struct fib_rule *rule, *r, *last = NULL;
363 struct nlattr *tb[FRA_MAX+1];
Thomas Graf0947c9fe2007-03-26 17:14:15 -0700364 int err = -EINVAL, unresolved = 0;
Thomas Graf14c0b972006-08-04 03:38:38 -0700365
366 if (nlh->nlmsg_len < nlmsg_msg_size(sizeof(*frh)))
367 goto errout;
368
Denis V. Lunev5fd30ee2008-01-10 03:20:28 -0800369 ops = lookup_rules_ops(net, frh->family);
Thomas Graf14c0b972006-08-04 03:38:38 -0700370 if (ops == NULL) {
Patrick McHardy2fe195c2008-07-01 19:59:37 -0700371 err = -EAFNOSUPPORT;
Thomas Graf14c0b972006-08-04 03:38:38 -0700372 goto errout;
373 }
374
375 err = nlmsg_parse(nlh, sizeof(*frh), tb, FRA_MAX, ops->policy);
376 if (err < 0)
377 goto errout;
378
Thomas Grafe1701c62007-03-24 12:46:02 -0700379 err = validate_rulemsg(frh, tb, ops);
380 if (err < 0)
381 goto errout;
382
Thomas Graf14c0b972006-08-04 03:38:38 -0700383 rule = kzalloc(ops->rule_size, GFP_KERNEL);
384 if (rule == NULL) {
385 err = -ENOMEM;
386 goto errout;
387 }
Eric W. Biedermanefd7ef12015-03-11 23:04:08 -0500388 rule->fr_net = net;
Thomas Graf14c0b972006-08-04 03:38:38 -0700389
Phil Sutterf53de1e2015-09-09 14:20:56 +0200390 rule->pref = tb[FRA_PRIORITY] ? nla_get_u32(tb[FRA_PRIORITY])
391 : fib_default_rule_pref(ops);
Thomas Graf14c0b972006-08-04 03:38:38 -0700392
Patrick McHardy491deb24b2009-12-03 01:25:54 +0000393 if (tb[FRA_IIFNAME]) {
Thomas Graf14c0b972006-08-04 03:38:38 -0700394 struct net_device *dev;
395
Patrick McHardy491deb24b2009-12-03 01:25:54 +0000396 rule->iifindex = -1;
397 nla_strlcpy(rule->iifname, tb[FRA_IIFNAME], IFNAMSIZ);
398 dev = __dev_get_by_name(net, rule->iifname);
Thomas Graf14c0b972006-08-04 03:38:38 -0700399 if (dev)
Patrick McHardy491deb24b2009-12-03 01:25:54 +0000400 rule->iifindex = dev->ifindex;
Thomas Graf14c0b972006-08-04 03:38:38 -0700401 }
402
Patrick McHardy1b038a52009-12-03 01:25:56 +0000403 if (tb[FRA_OIFNAME]) {
404 struct net_device *dev;
405
406 rule->oifindex = -1;
407 nla_strlcpy(rule->oifname, tb[FRA_OIFNAME], IFNAMSIZ);
408 dev = __dev_get_by_name(net, rule->oifname);
409 if (dev)
410 rule->oifindex = dev->ifindex;
411 }
412
Thomas Grafb8964ed2006-11-09 15:22:18 -0800413 if (tb[FRA_FWMARK]) {
414 rule->mark = nla_get_u32(tb[FRA_FWMARK]);
415 if (rule->mark)
416 /* compatibility: if the mark value is non-zero all bits
417 * are compared unless a mask is explicitly specified.
418 */
419 rule->mark_mask = 0xFFFFFFFF;
420 }
421
422 if (tb[FRA_FWMASK])
423 rule->mark_mask = nla_get_u32(tb[FRA_FWMASK]);
424
Thomas Grafe7030872015-07-21 10:44:01 +0200425 if (tb[FRA_TUN_ID])
426 rule->tun_id = nla_get_be64(tb[FRA_TUN_ID]);
427
Wei Yongjun145b1712017-04-26 14:03:50 +0000428 err = -EINVAL;
David Ahern96c63fa2016-06-08 10:55:39 -0700429 if (tb[FRA_L3MDEV]) {
430#ifdef CONFIG_NET_L3_MASTER_DEV
431 rule->l3mdev = nla_get_u8(tb[FRA_L3MDEV]);
432 if (rule->l3mdev != 1)
433#endif
434 goto errout_free;
435 }
436
Thomas Graf14c0b972006-08-04 03:38:38 -0700437 rule->action = frh->action;
438 rule->flags = frh->flags;
Patrick McHardy9e762a42006-08-10 23:09:48 -0700439 rule->table = frh_get_table(frh, tb);
Stefan Tomanek73f56982013-08-03 14:14:43 +0200440 if (tb[FRA_SUPPRESS_PREFIXLEN])
441 rule->suppress_prefixlen = nla_get_u32(tb[FRA_SUPPRESS_PREFIXLEN]);
442 else
443 rule->suppress_prefixlen = -1;
Thomas Graf14c0b972006-08-04 03:38:38 -0700444
Stefan Tomanek6ef94cf2013-08-02 17:19:56 +0200445 if (tb[FRA_SUPPRESS_IFGROUP])
446 rule->suppress_ifgroup = nla_get_u32(tb[FRA_SUPPRESS_IFGROUP]);
Stefan Tomanek73f56982013-08-03 14:14:43 +0200447 else
448 rule->suppress_ifgroup = -1;
Stefan Tomanek6ef94cf2013-08-02 17:19:56 +0200449
Thomas Graf0947c9fe2007-03-26 17:14:15 -0700450 if (tb[FRA_GOTO]) {
451 if (rule->action != FR_ACT_GOTO)
452 goto errout_free;
453
454 rule->target = nla_get_u32(tb[FRA_GOTO]);
455 /* Backward jumps are prohibited to avoid endless loops */
456 if (rule->target <= rule->pref)
457 goto errout_free;
458
Denis V. Lunev76c72d42007-09-16 15:44:27 -0700459 list_for_each_entry(r, &ops->rules_list, list) {
Thomas Graf0947c9fe2007-03-26 17:14:15 -0700460 if (r->pref == rule->target) {
Eric Dumazet7a2b03c2010-10-26 09:24:55 +0000461 RCU_INIT_POINTER(rule->ctarget, r);
Thomas Graf0947c9fe2007-03-26 17:14:15 -0700462 break;
463 }
464 }
465
Eric Dumazet7a2b03c2010-10-26 09:24:55 +0000466 if (rcu_dereference_protected(rule->ctarget, 1) == NULL)
Thomas Graf0947c9fe2007-03-26 17:14:15 -0700467 unresolved = 1;
468 } else if (rule->action == FR_ACT_GOTO)
469 goto errout_free;
470
David Ahern96c63fa2016-06-08 10:55:39 -0700471 if (rule->l3mdev && rule->table)
472 goto errout_free;
473
Lorenzo Colitti2ec93fe2016-11-04 02:23:42 +0900474 if (tb[FRA_UID_RANGE]) {
475 if (current_user_ns() != net->user_ns) {
476 err = -EPERM;
477 goto errout_free;
478 }
479
480 rule->uid_range = nla_get_kuid_range(tb);
481
482 if (!uid_range_set(&rule->uid_range) ||
483 !uid_lte(rule->uid_range.start, rule->uid_range.end))
484 goto errout_free;
485 } else {
486 rule->uid_range = fib_kuid_range_unset;
487 }
488
Mateusz Bajorski153380e2016-06-29 09:22:10 +0200489 if ((nlh->nlmsg_flags & NLM_F_EXCL) &&
490 rule_exists(ops, frh, tb, rule)) {
491 err = -EEXIST;
492 goto errout_free;
493 }
494
Rami Rosen8b3521e2009-05-11 05:52:49 +0000495 err = ops->configure(rule, skb, frh, tb);
Thomas Graf14c0b972006-08-04 03:38:38 -0700496 if (err < 0)
497 goto errout_free;
498
Denis V. Lunev76c72d42007-09-16 15:44:27 -0700499 list_for_each_entry(r, &ops->rules_list, list) {
Thomas Graf14c0b972006-08-04 03:38:38 -0700500 if (r->pref > rule->pref)
501 break;
502 last = r;
503 }
504
505 fib_rule_get(rule);
506
Eric Dumazetebb9fed2010-10-23 09:44:25 +0000507 if (last)
508 list_add_rcu(&rule->list, &last->list);
509 else
510 list_add_rcu(&rule->list, &ops->rules_list);
511
Thomas Graf0947c9fe2007-03-26 17:14:15 -0700512 if (ops->unresolved_rules) {
513 /*
514 * There are unresolved goto rules in the list, check if
515 * any of them are pointing to this new rule.
516 */
Denis V. Lunev76c72d42007-09-16 15:44:27 -0700517 list_for_each_entry(r, &ops->rules_list, list) {
Thomas Graf0947c9fe2007-03-26 17:14:15 -0700518 if (r->action == FR_ACT_GOTO &&
Gao feng561dac22011-09-11 15:36:05 +0000519 r->target == rule->pref &&
520 rtnl_dereference(r->ctarget) == NULL) {
Thomas Graf0947c9fe2007-03-26 17:14:15 -0700521 rcu_assign_pointer(r->ctarget, rule);
522 if (--ops->unresolved_rules == 0)
523 break;
524 }
525 }
526 }
527
528 if (rule->action == FR_ACT_GOTO)
529 ops->nr_goto_rules++;
530
531 if (unresolved)
532 ops->unresolved_rules++;
533
Thomas Grafe7030872015-07-21 10:44:01 +0200534 if (rule->tun_id)
535 ip_tunnel_need_metadata();
536
Eric W. Biederman15e47302012-09-07 20:12:54 +0000537 notify_rule_change(RTM_NEWRULE, rule, ops, nlh, NETLINK_CB(skb).portid);
Thomas Graf73417f62007-03-27 13:56:52 -0700538 flush_route_cache(ops);
Thomas Graf14c0b972006-08-04 03:38:38 -0700539 rules_ops_put(ops);
540 return 0;
541
542errout_free:
543 kfree(rule);
544errout:
545 rules_ops_put(ops);
546 return err;
547}
David Ahern96c63fa2016-06-08 10:55:39 -0700548EXPORT_SYMBOL_GPL(fib_nl_newrule);
Thomas Graf14c0b972006-08-04 03:38:38 -0700549
David Ahern96c63fa2016-06-08 10:55:39 -0700550int fib_nl_delrule(struct sk_buff *skb, struct nlmsghdr *nlh)
Thomas Graf14c0b972006-08-04 03:38:38 -0700551{
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +0900552 struct net *net = sock_net(skb->sk);
Thomas Graf14c0b972006-08-04 03:38:38 -0700553 struct fib_rule_hdr *frh = nlmsg_data(nlh);
554 struct fib_rules_ops *ops = NULL;
Thomas Graf0947c9fe2007-03-26 17:14:15 -0700555 struct fib_rule *rule, *tmp;
Thomas Graf14c0b972006-08-04 03:38:38 -0700556 struct nlattr *tb[FRA_MAX+1];
Lorenzo Colitti2ec93fe2016-11-04 02:23:42 +0900557 struct fib_kuid_range range;
Thomas Graf14c0b972006-08-04 03:38:38 -0700558 int err = -EINVAL;
559
560 if (nlh->nlmsg_len < nlmsg_msg_size(sizeof(*frh)))
561 goto errout;
562
Denis V. Lunev5fd30ee2008-01-10 03:20:28 -0800563 ops = lookup_rules_ops(net, frh->family);
Thomas Graf14c0b972006-08-04 03:38:38 -0700564 if (ops == NULL) {
Patrick McHardy2fe195c2008-07-01 19:59:37 -0700565 err = -EAFNOSUPPORT;
Thomas Graf14c0b972006-08-04 03:38:38 -0700566 goto errout;
567 }
568
569 err = nlmsg_parse(nlh, sizeof(*frh), tb, FRA_MAX, ops->policy);
570 if (err < 0)
571 goto errout;
572
Thomas Grafe1701c62007-03-24 12:46:02 -0700573 err = validate_rulemsg(frh, tb, ops);
574 if (err < 0)
575 goto errout;
576
Lorenzo Colitti2ec93fe2016-11-04 02:23:42 +0900577 if (tb[FRA_UID_RANGE]) {
578 range = nla_get_kuid_range(tb);
Wei Yongjun145b1712017-04-26 14:03:50 +0000579 if (!uid_range_set(&range)) {
580 err = -EINVAL;
Lorenzo Colitti2ec93fe2016-11-04 02:23:42 +0900581 goto errout;
Wei Yongjun145b1712017-04-26 14:03:50 +0000582 }
Lorenzo Colitti2ec93fe2016-11-04 02:23:42 +0900583 } else {
584 range = fib_kuid_range_unset;
585 }
586
Denis V. Lunev76c72d42007-09-16 15:44:27 -0700587 list_for_each_entry(rule, &ops->rules_list, list) {
Thomas Graf14c0b972006-08-04 03:38:38 -0700588 if (frh->action && (frh->action != rule->action))
589 continue;
590
Andreas Henriksson13eb2ab2013-11-07 18:26:38 +0100591 if (frh_get_table(frh, tb) &&
592 (frh_get_table(frh, tb) != rule->table))
Thomas Graf14c0b972006-08-04 03:38:38 -0700593 continue;
594
595 if (tb[FRA_PRIORITY] &&
596 (rule->pref != nla_get_u32(tb[FRA_PRIORITY])))
597 continue;
598
Patrick McHardy491deb24b2009-12-03 01:25:54 +0000599 if (tb[FRA_IIFNAME] &&
600 nla_strcmp(tb[FRA_IIFNAME], rule->iifname))
Thomas Graf14c0b972006-08-04 03:38:38 -0700601 continue;
602
Patrick McHardy1b038a52009-12-03 01:25:56 +0000603 if (tb[FRA_OIFNAME] &&
604 nla_strcmp(tb[FRA_OIFNAME], rule->oifname))
605 continue;
606
Thomas Grafb8964ed2006-11-09 15:22:18 -0800607 if (tb[FRA_FWMARK] &&
608 (rule->mark != nla_get_u32(tb[FRA_FWMARK])))
609 continue;
610
611 if (tb[FRA_FWMASK] &&
612 (rule->mark_mask != nla_get_u32(tb[FRA_FWMASK])))
613 continue;
614
Thomas Grafe7030872015-07-21 10:44:01 +0200615 if (tb[FRA_TUN_ID] &&
616 (rule->tun_id != nla_get_be64(tb[FRA_TUN_ID])))
617 continue;
618
David Ahern96c63fa2016-06-08 10:55:39 -0700619 if (tb[FRA_L3MDEV] &&
620 (rule->l3mdev != nla_get_u8(tb[FRA_L3MDEV])))
621 continue;
622
Lorenzo Colitti2ec93fe2016-11-04 02:23:42 +0900623 if (uid_range_set(&range) &&
624 (!uid_eq(rule->uid_range.start, range.start) ||
625 !uid_eq(rule->uid_range.end, range.end)))
626 continue;
627
Thomas Graf14c0b972006-08-04 03:38:38 -0700628 if (!ops->compare(rule, frh, tb))
629 continue;
630
631 if (rule->flags & FIB_RULE_PERMANENT) {
632 err = -EPERM;
633 goto errout;
634 }
635
Alexander Duyck0ddcf432015-03-06 13:47:00 -0800636 if (ops->delete) {
637 err = ops->delete(rule);
638 if (err)
639 goto errout;
640 }
641
Thomas Grafe7030872015-07-21 10:44:01 +0200642 if (rule->tun_id)
643 ip_tunnel_unneed_metadata();
644
Thomas Graf14c0b972006-08-04 03:38:38 -0700645 list_del_rcu(&rule->list);
Thomas Graf0947c9fe2007-03-26 17:14:15 -0700646
Yan, Zhengafaef732011-10-17 15:20:28 +0000647 if (rule->action == FR_ACT_GOTO) {
Thomas Graf0947c9fe2007-03-26 17:14:15 -0700648 ops->nr_goto_rules--;
Yan, Zhengafaef732011-10-17 15:20:28 +0000649 if (rtnl_dereference(rule->ctarget) == NULL)
650 ops->unresolved_rules--;
651 }
Thomas Graf0947c9fe2007-03-26 17:14:15 -0700652
653 /*
654 * Check if this rule is a target to any of them. If so,
655 * disable them. As this operation is eventually very
656 * expensive, it is only performed if goto rules have
657 * actually been added.
658 */
659 if (ops->nr_goto_rules > 0) {
Denis V. Lunev76c72d42007-09-16 15:44:27 -0700660 list_for_each_entry(tmp, &ops->rules_list, list) {
Eric Dumazet7a2b03c2010-10-26 09:24:55 +0000661 if (rtnl_dereference(tmp->ctarget) == rule) {
Stephen Hemmingera9b3cd72011-08-01 16:19:00 +0000662 RCU_INIT_POINTER(tmp->ctarget, NULL);
Thomas Graf0947c9fe2007-03-26 17:14:15 -0700663 ops->unresolved_rules++;
664 }
665 }
666 }
667
Denis V. Lunev9e3a5482008-01-20 16:46:41 -0800668 notify_rule_change(RTM_DELRULE, rule, ops, nlh,
Eric W. Biederman15e47302012-09-07 20:12:54 +0000669 NETLINK_CB(skb).portid);
Thomas Graf14c0b972006-08-04 03:38:38 -0700670 fib_rule_put(rule);
Thomas Graf73417f62007-03-27 13:56:52 -0700671 flush_route_cache(ops);
Thomas Graf14c0b972006-08-04 03:38:38 -0700672 rules_ops_put(ops);
673 return 0;
674 }
675
676 err = -ENOENT;
677errout:
678 rules_ops_put(ops);
679 return err;
680}
David Ahern96c63fa2016-06-08 10:55:39 -0700681EXPORT_SYMBOL_GPL(fib_nl_delrule);
Thomas Graf14c0b972006-08-04 03:38:38 -0700682
Thomas Graf339bf982006-11-10 14:10:15 -0800683static inline size_t fib_rule_nlmsg_size(struct fib_rules_ops *ops,
684 struct fib_rule *rule)
685{
686 size_t payload = NLMSG_ALIGN(sizeof(struct fib_rule_hdr))
Patrick McHardy491deb24b2009-12-03 01:25:54 +0000687 + nla_total_size(IFNAMSIZ) /* FRA_IIFNAME */
Patrick McHardy1b038a52009-12-03 01:25:56 +0000688 + nla_total_size(IFNAMSIZ) /* FRA_OIFNAME */
Thomas Graf339bf982006-11-10 14:10:15 -0800689 + nla_total_size(4) /* FRA_PRIORITY */
690 + nla_total_size(4) /* FRA_TABLE */
Stefan Tomanek73f56982013-08-03 14:14:43 +0200691 + nla_total_size(4) /* FRA_SUPPRESS_PREFIXLEN */
Stefan Tomanek6ef94cf2013-08-02 17:19:56 +0200692 + nla_total_size(4) /* FRA_SUPPRESS_IFGROUP */
Thomas Graf339bf982006-11-10 14:10:15 -0800693 + nla_total_size(4) /* FRA_FWMARK */
Thomas Grafe7030872015-07-21 10:44:01 +0200694 + nla_total_size(4) /* FRA_FWMASK */
Lorenzo Colitti2ec93fe2016-11-04 02:23:42 +0900695 + nla_total_size_64bit(8) /* FRA_TUN_ID */
696 + nla_total_size(sizeof(struct fib_kuid_range));
Thomas Graf339bf982006-11-10 14:10:15 -0800697
698 if (ops->nlmsg_payload)
699 payload += ops->nlmsg_payload(rule);
700
701 return payload;
702}
703
Thomas Graf14c0b972006-08-04 03:38:38 -0700704static int fib_nl_fill_rule(struct sk_buff *skb, struct fib_rule *rule,
705 u32 pid, u32 seq, int type, int flags,
706 struct fib_rules_ops *ops)
707{
708 struct nlmsghdr *nlh;
709 struct fib_rule_hdr *frh;
710
711 nlh = nlmsg_put(skb, pid, seq, type, sizeof(*frh), flags);
712 if (nlh == NULL)
Patrick McHardy26932562007-01-31 23:16:40 -0800713 return -EMSGSIZE;
Thomas Graf14c0b972006-08-04 03:38:38 -0700714
715 frh = nlmsg_data(nlh);
Patrick McHardy28bb1722010-04-13 05:03:16 +0000716 frh->family = ops->family;
Thomas Graf14c0b972006-08-04 03:38:38 -0700717 frh->table = rule->table;
David S. Miller0e3cea72012-04-01 20:47:01 -0400718 if (nla_put_u32(skb, FRA_TABLE, rule->table))
719 goto nla_put_failure;
Stefan Tomanek73f56982013-08-03 14:14:43 +0200720 if (nla_put_u32(skb, FRA_SUPPRESS_PREFIXLEN, rule->suppress_prefixlen))
Stefan Tomanek7764a452013-08-01 02:17:15 +0200721 goto nla_put_failure;
Thomas Graf14c0b972006-08-04 03:38:38 -0700722 frh->res1 = 0;
723 frh->res2 = 0;
724 frh->action = rule->action;
725 frh->flags = rule->flags;
726
Eric Dumazet7a2b03c2010-10-26 09:24:55 +0000727 if (rule->action == FR_ACT_GOTO &&
Eric Dumazet33d480c2011-08-11 19:30:52 +0000728 rcu_access_pointer(rule->ctarget) == NULL)
Thomas Graf0947c9fe2007-03-26 17:14:15 -0700729 frh->flags |= FIB_RULE_UNRESOLVED;
730
Patrick McHardy491deb24b2009-12-03 01:25:54 +0000731 if (rule->iifname[0]) {
David S. Miller0e3cea72012-04-01 20:47:01 -0400732 if (nla_put_string(skb, FRA_IIFNAME, rule->iifname))
733 goto nla_put_failure;
Patrick McHardy491deb24b2009-12-03 01:25:54 +0000734 if (rule->iifindex == -1)
735 frh->flags |= FIB_RULE_IIF_DETACHED;
Thomas Graf2b443682007-03-26 17:37:59 -0700736 }
737
Patrick McHardy1b038a52009-12-03 01:25:56 +0000738 if (rule->oifname[0]) {
David S. Miller0e3cea72012-04-01 20:47:01 -0400739 if (nla_put_string(skb, FRA_OIFNAME, rule->oifname))
740 goto nla_put_failure;
Patrick McHardy1b038a52009-12-03 01:25:56 +0000741 if (rule->oifindex == -1)
742 frh->flags |= FIB_RULE_OIF_DETACHED;
743 }
744
David S. Miller0e3cea72012-04-01 20:47:01 -0400745 if ((rule->pref &&
746 nla_put_u32(skb, FRA_PRIORITY, rule->pref)) ||
747 (rule->mark &&
748 nla_put_u32(skb, FRA_FWMARK, rule->mark)) ||
749 ((rule->mark_mask || rule->mark) &&
750 nla_put_u32(skb, FRA_FWMASK, rule->mark_mask)) ||
751 (rule->target &&
Thomas Grafe7030872015-07-21 10:44:01 +0200752 nla_put_u32(skb, FRA_GOTO, rule->target)) ||
753 (rule->tun_id &&
David Ahern96c63fa2016-06-08 10:55:39 -0700754 nla_put_be64(skb, FRA_TUN_ID, rule->tun_id, FRA_PAD)) ||
755 (rule->l3mdev &&
Lorenzo Colitti2ec93fe2016-11-04 02:23:42 +0900756 nla_put_u8(skb, FRA_L3MDEV, rule->l3mdev)) ||
757 (uid_range_set(&rule->uid_range) &&
758 nla_put_uid_range(skb, &rule->uid_range)))
David S. Miller0e3cea72012-04-01 20:47:01 -0400759 goto nla_put_failure;
Stefan Tomanek6ef94cf2013-08-02 17:19:56 +0200760
761 if (rule->suppress_ifgroup != -1) {
762 if (nla_put_u32(skb, FRA_SUPPRESS_IFGROUP, rule->suppress_ifgroup))
763 goto nla_put_failure;
764 }
765
Rami Rosen04af8cf2009-05-20 17:26:23 -0700766 if (ops->fill(rule, skb, frh) < 0)
Thomas Graf14c0b972006-08-04 03:38:38 -0700767 goto nla_put_failure;
768
Johannes Berg053c0952015-01-16 22:09:00 +0100769 nlmsg_end(skb, nlh);
770 return 0;
Thomas Graf14c0b972006-08-04 03:38:38 -0700771
772nla_put_failure:
Patrick McHardy26932562007-01-31 23:16:40 -0800773 nlmsg_cancel(skb, nlh);
774 return -EMSGSIZE;
Thomas Graf14c0b972006-08-04 03:38:38 -0700775}
776
Thomas Grafc4546732007-03-25 23:24:24 -0700777static int dump_rules(struct sk_buff *skb, struct netlink_callback *cb,
778 struct fib_rules_ops *ops)
Thomas Graf14c0b972006-08-04 03:38:38 -0700779{
780 int idx = 0;
781 struct fib_rule *rule;
Wilson Kok41fc0142015-09-22 21:40:22 -0700782 int err = 0;
Thomas Graf14c0b972006-08-04 03:38:38 -0700783
Eric Dumazete67f88d2011-04-27 22:56:07 +0000784 rcu_read_lock();
785 list_for_each_entry_rcu(rule, &ops->rules_list, list) {
Thomas Grafc4546732007-03-25 23:24:24 -0700786 if (idx < cb->args[1])
Thomas Graf14c0b972006-08-04 03:38:38 -0700787 goto skip;
788
Wilson Kok41fc0142015-09-22 21:40:22 -0700789 err = fib_nl_fill_rule(skb, rule, NETLINK_CB(cb->skb).portid,
790 cb->nlh->nlmsg_seq, RTM_NEWRULE,
791 NLM_F_MULTI, ops);
792 if (err)
Thomas Graf14c0b972006-08-04 03:38:38 -0700793 break;
794skip:
795 idx++;
796 }
Eric Dumazet2907c352011-05-25 07:34:04 +0000797 rcu_read_unlock();
Thomas Grafc4546732007-03-25 23:24:24 -0700798 cb->args[1] = idx;
Thomas Graf14c0b972006-08-04 03:38:38 -0700799 rules_ops_put(ops);
800
Wilson Kok41fc0142015-09-22 21:40:22 -0700801 return err;
Thomas Graf14c0b972006-08-04 03:38:38 -0700802}
803
Thomas Grafc4546732007-03-25 23:24:24 -0700804static int fib_nl_dumprule(struct sk_buff *skb, struct netlink_callback *cb)
805{
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +0900806 struct net *net = sock_net(skb->sk);
Thomas Grafc4546732007-03-25 23:24:24 -0700807 struct fib_rules_ops *ops;
808 int idx = 0, family;
809
810 family = rtnl_msg_family(cb->nlh);
811 if (family != AF_UNSPEC) {
812 /* Protocol specific dump request */
Denis V. Lunev5fd30ee2008-01-10 03:20:28 -0800813 ops = lookup_rules_ops(net, family);
Thomas Grafc4546732007-03-25 23:24:24 -0700814 if (ops == NULL)
815 return -EAFNOSUPPORT;
816
Wilson Kok41fc0142015-09-22 21:40:22 -0700817 dump_rules(skb, cb, ops);
818
819 return skb->len;
Thomas Grafc4546732007-03-25 23:24:24 -0700820 }
821
822 rcu_read_lock();
Denis V. Lunev5fd30ee2008-01-10 03:20:28 -0800823 list_for_each_entry_rcu(ops, &net->rules_ops, list) {
Thomas Grafc4546732007-03-25 23:24:24 -0700824 if (idx < cb->args[0] || !try_module_get(ops->owner))
825 goto skip;
826
827 if (dump_rules(skb, cb, ops) < 0)
828 break;
829
830 cb->args[1] = 0;
Eric Dumazet2fb35732010-03-09 20:03:38 +0000831skip:
Thomas Grafc4546732007-03-25 23:24:24 -0700832 idx++;
833 }
834 rcu_read_unlock();
835 cb->args[0] = idx;
836
837 return skb->len;
838}
Thomas Graf14c0b972006-08-04 03:38:38 -0700839
Denis V. Lunev9e3a5482008-01-20 16:46:41 -0800840static void notify_rule_change(int event, struct fib_rule *rule,
Thomas Grafc17084d2006-08-15 00:32:48 -0700841 struct fib_rules_ops *ops, struct nlmsghdr *nlh,
842 u32 pid)
Thomas Graf14c0b972006-08-04 03:38:38 -0700843{
Denis V. Lunev9e3a5482008-01-20 16:46:41 -0800844 struct net *net;
Thomas Grafc17084d2006-08-15 00:32:48 -0700845 struct sk_buff *skb;
846 int err = -ENOBUFS;
Thomas Graf14c0b972006-08-04 03:38:38 -0700847
Denis V. Lunev9e3a5482008-01-20 16:46:41 -0800848 net = ops->fro_net;
Thomas Graf339bf982006-11-10 14:10:15 -0800849 skb = nlmsg_new(fib_rule_nlmsg_size(ops, rule), GFP_KERNEL);
Thomas Graf14c0b972006-08-04 03:38:38 -0700850 if (skb == NULL)
Thomas Grafc17084d2006-08-15 00:32:48 -0700851 goto errout;
852
853 err = fib_nl_fill_rule(skb, rule, pid, nlh->nlmsg_seq, event, 0, ops);
Patrick McHardy26932562007-01-31 23:16:40 -0800854 if (err < 0) {
855 /* -EMSGSIZE implies BUG in fib_rule_nlmsg_size() */
856 WARN_ON(err == -EMSGSIZE);
857 kfree_skb(skb);
858 goto errout;
859 }
Denis V. Lunev9e3a5482008-01-20 16:46:41 -0800860
Pablo Neira Ayuso1ce85fe2009-02-24 23:18:28 -0800861 rtnl_notify(skb, net, pid, ops->nlgroup, nlh, GFP_KERNEL);
862 return;
Thomas Grafc17084d2006-08-15 00:32:48 -0700863errout:
864 if (err < 0)
Denis V. Lunev5fd30ee2008-01-10 03:20:28 -0800865 rtnl_set_sk_err(net, ops->nlgroup, err);
Thomas Graf14c0b972006-08-04 03:38:38 -0700866}
867
868static void attach_rules(struct list_head *rules, struct net_device *dev)
869{
870 struct fib_rule *rule;
871
872 list_for_each_entry(rule, rules, list) {
Patrick McHardy491deb24b2009-12-03 01:25:54 +0000873 if (rule->iifindex == -1 &&
874 strcmp(dev->name, rule->iifname) == 0)
875 rule->iifindex = dev->ifindex;
Patrick McHardy1b038a52009-12-03 01:25:56 +0000876 if (rule->oifindex == -1 &&
877 strcmp(dev->name, rule->oifname) == 0)
878 rule->oifindex = dev->ifindex;
Thomas Graf14c0b972006-08-04 03:38:38 -0700879 }
880}
881
882static void detach_rules(struct list_head *rules, struct net_device *dev)
883{
884 struct fib_rule *rule;
885
Patrick McHardy1b038a52009-12-03 01:25:56 +0000886 list_for_each_entry(rule, rules, list) {
Patrick McHardy491deb24b2009-12-03 01:25:54 +0000887 if (rule->iifindex == dev->ifindex)
888 rule->iifindex = -1;
Patrick McHardy1b038a52009-12-03 01:25:56 +0000889 if (rule->oifindex == dev->ifindex)
890 rule->oifindex = -1;
891 }
Thomas Graf14c0b972006-08-04 03:38:38 -0700892}
893
894
895static int fib_rules_event(struct notifier_block *this, unsigned long event,
Jiri Pirko351638e2013-05-28 01:30:21 +0000896 void *ptr)
Thomas Graf14c0b972006-08-04 03:38:38 -0700897{
Jiri Pirko351638e2013-05-28 01:30:21 +0000898 struct net_device *dev = netdev_notifier_info_to_dev(ptr);
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +0900899 struct net *net = dev_net(dev);
Thomas Graf14c0b972006-08-04 03:38:38 -0700900 struct fib_rules_ops *ops;
901
Eric Dumazet748e2d92012-08-22 21:50:59 +0000902 ASSERT_RTNL();
Thomas Graf14c0b972006-08-04 03:38:38 -0700903
904 switch (event) {
905 case NETDEV_REGISTER:
Denis V. Lunev5fd30ee2008-01-10 03:20:28 -0800906 list_for_each_entry(ops, &net->rules_ops, list)
Denis V. Lunev76c72d42007-09-16 15:44:27 -0700907 attach_rules(&ops->rules_list, dev);
Thomas Graf14c0b972006-08-04 03:38:38 -0700908 break;
909
Maciej Żenczykowski946c0322014-02-07 16:23:48 -0800910 case NETDEV_CHANGENAME:
911 list_for_each_entry(ops, &net->rules_ops, list) {
912 detach_rules(&ops->rules_list, dev);
913 attach_rules(&ops->rules_list, dev);
914 }
915 break;
916
Thomas Graf14c0b972006-08-04 03:38:38 -0700917 case NETDEV_UNREGISTER:
Denis V. Lunev5fd30ee2008-01-10 03:20:28 -0800918 list_for_each_entry(ops, &net->rules_ops, list)
Denis V. Lunev76c72d42007-09-16 15:44:27 -0700919 detach_rules(&ops->rules_list, dev);
Thomas Graf14c0b972006-08-04 03:38:38 -0700920 break;
921 }
922
Thomas Graf14c0b972006-08-04 03:38:38 -0700923 return NOTIFY_DONE;
924}
925
926static struct notifier_block fib_rules_notifier = {
927 .notifier_call = fib_rules_event,
928};
929
Alexey Dobriyan2c8c1e72010-01-17 03:35:32 +0000930static int __net_init fib_rules_net_init(struct net *net)
Denis V. Lunev5fd30ee2008-01-10 03:20:28 -0800931{
932 INIT_LIST_HEAD(&net->rules_ops);
933 spin_lock_init(&net->rules_mod_lock);
934 return 0;
935}
936
937static struct pernet_operations fib_rules_net_ops = {
938 .init = fib_rules_net_init,
939};
940
Thomas Graf14c0b972006-08-04 03:38:38 -0700941static int __init fib_rules_init(void)
942{
Denis V. Lunev5fd30ee2008-01-10 03:20:28 -0800943 int err;
Greg Rosec7ac8672011-06-10 01:27:09 +0000944 rtnl_register(PF_UNSPEC, RTM_NEWRULE, fib_nl_newrule, NULL, NULL);
945 rtnl_register(PF_UNSPEC, RTM_DELRULE, fib_nl_delrule, NULL, NULL);
946 rtnl_register(PF_UNSPEC, RTM_GETRULE, NULL, fib_nl_dumprule, NULL);
Thomas Graf9d9e6a52007-03-25 23:20:05 -0700947
Eric W. Biederman5d6d4802008-11-07 22:52:34 -0800948 err = register_pernet_subsys(&fib_rules_net_ops);
Denis V. Lunev5fd30ee2008-01-10 03:20:28 -0800949 if (err < 0)
950 goto fail;
951
Eric W. Biederman5d6d4802008-11-07 22:52:34 -0800952 err = register_netdevice_notifier(&fib_rules_notifier);
Denis V. Lunev5fd30ee2008-01-10 03:20:28 -0800953 if (err < 0)
954 goto fail_unregister;
Eric W. Biederman5d6d4802008-11-07 22:52:34 -0800955
Denis V. Lunev5fd30ee2008-01-10 03:20:28 -0800956 return 0;
957
958fail_unregister:
Eric W. Biederman5d6d4802008-11-07 22:52:34 -0800959 unregister_pernet_subsys(&fib_rules_net_ops);
Denis V. Lunev5fd30ee2008-01-10 03:20:28 -0800960fail:
961 rtnl_unregister(PF_UNSPEC, RTM_NEWRULE);
962 rtnl_unregister(PF_UNSPEC, RTM_DELRULE);
963 rtnl_unregister(PF_UNSPEC, RTM_GETRULE);
964 return err;
Thomas Graf14c0b972006-08-04 03:38:38 -0700965}
966
967subsys_initcall(fib_rules_init);