blob: a384a10beb4fa5d0f75fcceb4ac14dcdee35f080 [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
Denis V. Lunev2994c632007-11-10 22:12:03 -080021int fib_default_rule_add(struct fib_rules_ops *ops,
22 u32 pref, u32 table, u32 flags)
23{
24 struct fib_rule *r;
25
26 r = kzalloc(ops->rule_size, GFP_KERNEL);
27 if (r == NULL)
28 return -ENOMEM;
29
30 atomic_set(&r->refcnt, 1);
31 r->action = FR_ACT_TO_TBL;
32 r->pref = pref;
33 r->table = table;
34 r->flags = flags;
Eric W. Biedermanefd7ef12015-03-11 23:04:08 -050035 r->fr_net = ops->fro_net;
Lorenzo Colitti8dc318f2014-03-31 16:23:51 +090036 r->uid_start = INVALID_UID;
37 r->uid_end = INVALID_UID;
Denis V. Lunev2994c632007-11-10 22:12:03 -080038
Stefan Tomanek73f56982013-08-03 14:14:43 +020039 r->suppress_prefixlen = -1;
40 r->suppress_ifgroup = -1;
41
Denis V. Lunev2994c632007-11-10 22:12:03 -080042 /* The lock is not required here, the list in unreacheable
43 * at the moment this function is called */
44 list_add_tail(&r->list, &ops->rules_list);
45 return 0;
46}
47EXPORT_SYMBOL(fib_default_rule_add);
48
Phil Sutterf53de1e2015-09-09 14:20:56 +020049static u32 fib_default_rule_pref(struct fib_rules_ops *ops)
Patrick McHardyd8a566b2010-04-13 05:03:15 +000050{
51 struct list_head *pos;
52 struct fib_rule *rule;
53
54 if (!list_empty(&ops->rules_list)) {
55 pos = ops->rules_list.next;
56 if (pos->next != &ops->rules_list) {
57 rule = list_entry(pos->next, struct fib_rule, list);
58 if (rule->pref)
59 return rule->pref - 1;
60 }
61 }
62
63 return 0;
64}
Patrick McHardyd8a566b2010-04-13 05:03:15 +000065
Denis V. Lunev9e3a5482008-01-20 16:46:41 -080066static void notify_rule_change(int event, struct fib_rule *rule,
Thomas Grafc17084d2006-08-15 00:32:48 -070067 struct fib_rules_ops *ops, struct nlmsghdr *nlh,
68 u32 pid);
Thomas Graf14c0b972006-08-04 03:38:38 -070069
Denis V. Lunev5fd30ee2008-01-10 03:20:28 -080070static struct fib_rules_ops *lookup_rules_ops(struct net *net, int family)
Thomas Graf14c0b972006-08-04 03:38:38 -070071{
72 struct fib_rules_ops *ops;
73
74 rcu_read_lock();
Denis V. Lunev5fd30ee2008-01-10 03:20:28 -080075 list_for_each_entry_rcu(ops, &net->rules_ops, list) {
Thomas Graf14c0b972006-08-04 03:38:38 -070076 if (ops->family == family) {
77 if (!try_module_get(ops->owner))
78 ops = NULL;
79 rcu_read_unlock();
80 return ops;
81 }
82 }
83 rcu_read_unlock();
84
85 return NULL;
86}
87
88static void rules_ops_put(struct fib_rules_ops *ops)
89{
90 if (ops)
91 module_put(ops->owner);
92}
93
Thomas Graf73417f62007-03-27 13:56:52 -070094static void flush_route_cache(struct fib_rules_ops *ops)
95{
96 if (ops->flush_cache)
Denis V. Lunevae299fc2008-07-05 19:01:28 -070097 ops->flush_cache(ops);
Thomas Graf73417f62007-03-27 13:56:52 -070098}
99
Eric W. Biedermane9c51582009-12-03 12:22:55 -0800100static int __fib_rules_register(struct fib_rules_ops *ops)
Thomas Graf14c0b972006-08-04 03:38:38 -0700101{
102 int err = -EEXIST;
103 struct fib_rules_ops *o;
Denis V. Lunev9e3a5482008-01-20 16:46:41 -0800104 struct net *net;
105
106 net = ops->fro_net;
Thomas Graf14c0b972006-08-04 03:38:38 -0700107
108 if (ops->rule_size < sizeof(struct fib_rule))
109 return -EINVAL;
110
111 if (ops->match == NULL || ops->configure == NULL ||
112 ops->compare == NULL || ops->fill == NULL ||
113 ops->action == NULL)
114 return -EINVAL;
115
Denis V. Lunev5fd30ee2008-01-10 03:20:28 -0800116 spin_lock(&net->rules_mod_lock);
117 list_for_each_entry(o, &net->rules_ops, list)
Thomas Graf14c0b972006-08-04 03:38:38 -0700118 if (ops->family == o->family)
119 goto errout;
120
Denis V. Lunev5fd30ee2008-01-10 03:20:28 -0800121 list_add_tail_rcu(&ops->list, &net->rules_ops);
Thomas Graf14c0b972006-08-04 03:38:38 -0700122 err = 0;
123errout:
Denis V. Lunev5fd30ee2008-01-10 03:20:28 -0800124 spin_unlock(&net->rules_mod_lock);
Thomas Graf14c0b972006-08-04 03:38:38 -0700125
126 return err;
127}
128
Eric W. Biedermane9c51582009-12-03 12:22:55 -0800129struct fib_rules_ops *
Patrick McHardy3d0c9c42010-04-26 16:02:04 +0200130fib_rules_register(const struct fib_rules_ops *tmpl, struct net *net)
Eric W. Biedermane9c51582009-12-03 12:22:55 -0800131{
132 struct fib_rules_ops *ops;
133 int err;
134
Eric Dumazet2fb35732010-03-09 20:03:38 +0000135 ops = kmemdup(tmpl, sizeof(*ops), GFP_KERNEL);
Eric W. Biedermane9c51582009-12-03 12:22:55 -0800136 if (ops == NULL)
137 return ERR_PTR(-ENOMEM);
138
139 INIT_LIST_HEAD(&ops->rules_list);
140 ops->fro_net = net;
141
142 err = __fib_rules_register(ops);
143 if (err) {
144 kfree(ops);
145 ops = ERR_PTR(err);
146 }
147
148 return ops;
149}
Thomas Graf14c0b972006-08-04 03:38:38 -0700150EXPORT_SYMBOL_GPL(fib_rules_register);
151
stephen hemminger1df99162010-10-04 20:14:17 +0000152static void fib_rules_cleanup_ops(struct fib_rules_ops *ops)
Thomas Graf14c0b972006-08-04 03:38:38 -0700153{
154 struct fib_rule *rule, *tmp;
155
Denis V. Lunev76c72d42007-09-16 15:44:27 -0700156 list_for_each_entry_safe(rule, tmp, &ops->rules_list, list) {
Thomas Graf14c0b972006-08-04 03:38:38 -0700157 list_del_rcu(&rule->list);
David S. Miller7a9bc9b2012-06-29 01:32:45 -0700158 if (ops->delete)
159 ops->delete(rule);
Thomas Graf14c0b972006-08-04 03:38:38 -0700160 fib_rule_put(rule);
161 }
162}
163
Denis V. Lunev9e3a5482008-01-20 16:46:41 -0800164void fib_rules_unregister(struct fib_rules_ops *ops)
Thomas Graf14c0b972006-08-04 03:38:38 -0700165{
Denis V. Lunev9e3a5482008-01-20 16:46:41 -0800166 struct net *net = ops->fro_net;
Thomas Graf14c0b972006-08-04 03:38:38 -0700167
Denis V. Lunev5fd30ee2008-01-10 03:20:28 -0800168 spin_lock(&net->rules_mod_lock);
Denis V. Lunev72132c1b2008-01-14 22:59:30 -0800169 list_del_rcu(&ops->list);
Denis V. Lunev5fd30ee2008-01-10 03:20:28 -0800170 spin_unlock(&net->rules_mod_lock);
Thomas Graf14c0b972006-08-04 03:38:38 -0700171
WANG Cong419df122015-03-31 11:01:46 -0700172 fib_rules_cleanup_ops(ops);
Eric W. Biedermanefd7ef12015-03-11 23:04:08 -0500173 kfree_rcu(ops, rcu);
Thomas Graf14c0b972006-08-04 03:38:38 -0700174}
Thomas Graf14c0b972006-08-04 03:38:38 -0700175EXPORT_SYMBOL_GPL(fib_rules_unregister);
176
Lorenzo Colitti8dc318f2014-03-31 16:23:51 +0900177static inline kuid_t fib_nl_uid(struct nlattr *nla)
178{
179 return make_kuid(current_user_ns(), nla_get_u32(nla));
180}
181
182static int nla_put_uid(struct sk_buff *skb, int idx, kuid_t uid)
183{
184 return nla_put_u32(skb, idx, from_kuid_munged(current_user_ns(), uid));
185}
186
187static int fib_uid_range_match(struct flowi *fl, struct fib_rule *rule)
188{
189 return (!uid_valid(rule->uid_start) && !uid_valid(rule->uid_end)) ||
190 (uid_gte(fl->flowi_uid, rule->uid_start) &&
191 uid_lte(fl->flowi_uid, rule->uid_end));
192}
193
Thomas Graf3dfbcc42006-11-09 15:23:20 -0800194static int fib_rule_match(struct fib_rule *rule, struct fib_rules_ops *ops,
David Ahern96c63fa2016-06-08 10:55:39 -0700195 struct flowi *fl, int flags,
196 struct fib_lookup_arg *arg)
Thomas Graf3dfbcc42006-11-09 15:23:20 -0800197{
198 int ret = 0;
199
David S. Miller1d28f422011-03-12 00:29:39 -0500200 if (rule->iifindex && (rule->iifindex != fl->flowi_iif))
Thomas Graf3dfbcc42006-11-09 15:23:20 -0800201 goto out;
202
David S. Miller1d28f422011-03-12 00:29:39 -0500203 if (rule->oifindex && (rule->oifindex != fl->flowi_oif))
Patrick McHardy1b038a52009-12-03 01:25:56 +0000204 goto out;
205
David S. Miller1d28f422011-03-12 00:29:39 -0500206 if ((rule->mark ^ fl->flowi_mark) & rule->mark_mask)
Thomas Graf3dfbcc42006-11-09 15:23:20 -0800207 goto out;
208
Thomas Grafe7030872015-07-21 10:44:01 +0200209 if (rule->tun_id && (rule->tun_id != fl->flowi_tun_key.tun_id))
210 goto out;
211
David Ahern96c63fa2016-06-08 10:55:39 -0700212 if (rule->l3mdev && !l3mdev_fib_rule_match(rule->fr_net, fl, arg))
213 goto out;
214
Lorenzo Colitti8dc318f2014-03-31 16:23:51 +0900215 if (!fib_uid_range_match(fl, rule))
216 goto out;
217
Thomas Graf3dfbcc42006-11-09 15:23:20 -0800218 ret = ops->match(rule, fl, flags);
219out:
220 return (rule->flags & FIB_RULE_INVERT) ? !ret : ret;
221}
222
Thomas Graf14c0b972006-08-04 03:38:38 -0700223int fib_rules_lookup(struct fib_rules_ops *ops, struct flowi *fl,
224 int flags, struct fib_lookup_arg *arg)
225{
226 struct fib_rule *rule;
227 int err;
228
229 rcu_read_lock();
230
Denis V. Lunev76c72d42007-09-16 15:44:27 -0700231 list_for_each_entry_rcu(rule, &ops->rules_list, list) {
Thomas Graf0947c9fe2007-03-26 17:14:15 -0700232jumped:
David Ahern96c63fa2016-06-08 10:55:39 -0700233 if (!fib_rule_match(rule, ops, fl, flags, arg))
Thomas Graf14c0b972006-08-04 03:38:38 -0700234 continue;
235
Thomas Graf0947c9fe2007-03-26 17:14:15 -0700236 if (rule->action == FR_ACT_GOTO) {
237 struct fib_rule *target;
238
239 target = rcu_dereference(rule->ctarget);
240 if (target == NULL) {
241 continue;
242 } else {
243 rule = target;
244 goto jumped;
245 }
Thomas Graffa0b2d12007-03-26 17:38:53 -0700246 } else if (rule->action == FR_ACT_NOP)
247 continue;
248 else
Thomas Graf0947c9fe2007-03-26 17:14:15 -0700249 err = ops->action(rule, fl, flags, arg);
250
Stefan Tomanek7764a452013-08-01 02:17:15 +0200251 if (!err && ops->suppress && ops->suppress(rule, arg))
252 continue;
253
Thomas Graf14c0b972006-08-04 03:38:38 -0700254 if (err != -EAGAIN) {
Eric Dumazetebc0ffa2010-10-05 10:41:36 +0000255 if ((arg->flags & FIB_LOOKUP_NOREF) ||
256 likely(atomic_inc_not_zero(&rule->refcnt))) {
Eric Dumazet7fa7cb72010-09-27 04:18:27 +0000257 arg->rule = rule;
258 goto out;
259 }
260 break;
Thomas Graf14c0b972006-08-04 03:38:38 -0700261 }
262 }
263
Steven Whitehouse83886b62007-03-30 13:34:27 -0700264 err = -ESRCH;
Thomas Graf14c0b972006-08-04 03:38:38 -0700265out:
266 rcu_read_unlock();
267
268 return err;
269}
Thomas Graf14c0b972006-08-04 03:38:38 -0700270EXPORT_SYMBOL_GPL(fib_rules_lookup);
271
Thomas Grafe1701c62007-03-24 12:46:02 -0700272static int validate_rulemsg(struct fib_rule_hdr *frh, struct nlattr **tb,
273 struct fib_rules_ops *ops)
274{
275 int err = -EINVAL;
276
277 if (frh->src_len)
278 if (tb[FRA_SRC] == NULL ||
279 frh->src_len > (ops->addr_size * 8) ||
280 nla_len(tb[FRA_SRC]) != ops->addr_size)
281 goto errout;
282
283 if (frh->dst_len)
284 if (tb[FRA_DST] == NULL ||
285 frh->dst_len > (ops->addr_size * 8) ||
286 nla_len(tb[FRA_DST]) != ops->addr_size)
287 goto errout;
288
289 err = 0;
290errout:
291 return err;
292}
293
Mateusz Bajorski153380e2016-06-29 09:22:10 +0200294static int rule_exists(struct fib_rules_ops *ops, struct fib_rule_hdr *frh,
295 struct nlattr **tb, struct fib_rule *rule)
296{
297 struct fib_rule *r;
298
299 list_for_each_entry(r, &ops->rules_list, list) {
300 if (r->action != rule->action)
301 continue;
302
303 if (r->table != rule->table)
304 continue;
305
306 if (r->pref != rule->pref)
307 continue;
308
309 if (memcmp(r->iifname, rule->iifname, IFNAMSIZ))
310 continue;
311
312 if (memcmp(r->oifname, rule->oifname, IFNAMSIZ))
313 continue;
314
315 if (r->mark != rule->mark)
316 continue;
317
318 if (r->mark_mask != rule->mark_mask)
319 continue;
320
321 if (r->tun_id != rule->tun_id)
322 continue;
323
324 if (r->fr_net != rule->fr_net)
325 continue;
326
327 if (r->l3mdev != rule->l3mdev)
328 continue;
329
330 if (!ops->compare(r, frh, tb))
331 continue;
332 return 1;
333 }
334 return 0;
335}
336
David Ahern96c63fa2016-06-08 10:55:39 -0700337int fib_nl_newrule(struct sk_buff *skb, struct nlmsghdr *nlh)
Thomas Graf14c0b972006-08-04 03:38:38 -0700338{
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +0900339 struct net *net = sock_net(skb->sk);
Thomas Graf14c0b972006-08-04 03:38:38 -0700340 struct fib_rule_hdr *frh = nlmsg_data(nlh);
341 struct fib_rules_ops *ops = NULL;
342 struct fib_rule *rule, *r, *last = NULL;
343 struct nlattr *tb[FRA_MAX+1];
Thomas Graf0947c9fe2007-03-26 17:14:15 -0700344 int err = -EINVAL, unresolved = 0;
Thomas Graf14c0b972006-08-04 03:38:38 -0700345
346 if (nlh->nlmsg_len < nlmsg_msg_size(sizeof(*frh)))
347 goto errout;
348
Denis V. Lunev5fd30ee2008-01-10 03:20:28 -0800349 ops = lookup_rules_ops(net, frh->family);
Thomas Graf14c0b972006-08-04 03:38:38 -0700350 if (ops == NULL) {
Patrick McHardy2fe195c2008-07-01 19:59:37 -0700351 err = -EAFNOSUPPORT;
Thomas Graf14c0b972006-08-04 03:38:38 -0700352 goto errout;
353 }
354
355 err = nlmsg_parse(nlh, sizeof(*frh), tb, FRA_MAX, ops->policy);
356 if (err < 0)
357 goto errout;
358
Thomas Grafe1701c62007-03-24 12:46:02 -0700359 err = validate_rulemsg(frh, tb, ops);
360 if (err < 0)
361 goto errout;
362
Thomas Graf14c0b972006-08-04 03:38:38 -0700363 rule = kzalloc(ops->rule_size, GFP_KERNEL);
364 if (rule == NULL) {
365 err = -ENOMEM;
366 goto errout;
367 }
Eric W. Biedermanefd7ef12015-03-11 23:04:08 -0500368 rule->fr_net = net;
Thomas Graf14c0b972006-08-04 03:38:38 -0700369
Phil Sutterf53de1e2015-09-09 14:20:56 +0200370 rule->pref = tb[FRA_PRIORITY] ? nla_get_u32(tb[FRA_PRIORITY])
371 : fib_default_rule_pref(ops);
Thomas Graf14c0b972006-08-04 03:38:38 -0700372
Patrick McHardy491deb24b2009-12-03 01:25:54 +0000373 if (tb[FRA_IIFNAME]) {
Thomas Graf14c0b972006-08-04 03:38:38 -0700374 struct net_device *dev;
375
Patrick McHardy491deb24b2009-12-03 01:25:54 +0000376 rule->iifindex = -1;
377 nla_strlcpy(rule->iifname, tb[FRA_IIFNAME], IFNAMSIZ);
378 dev = __dev_get_by_name(net, rule->iifname);
Thomas Graf14c0b972006-08-04 03:38:38 -0700379 if (dev)
Patrick McHardy491deb24b2009-12-03 01:25:54 +0000380 rule->iifindex = dev->ifindex;
Thomas Graf14c0b972006-08-04 03:38:38 -0700381 }
382
Patrick McHardy1b038a52009-12-03 01:25:56 +0000383 if (tb[FRA_OIFNAME]) {
384 struct net_device *dev;
385
386 rule->oifindex = -1;
387 nla_strlcpy(rule->oifname, tb[FRA_OIFNAME], IFNAMSIZ);
388 dev = __dev_get_by_name(net, rule->oifname);
389 if (dev)
390 rule->oifindex = dev->ifindex;
391 }
392
Thomas Grafb8964ed2006-11-09 15:22:18 -0800393 if (tb[FRA_FWMARK]) {
394 rule->mark = nla_get_u32(tb[FRA_FWMARK]);
395 if (rule->mark)
396 /* compatibility: if the mark value is non-zero all bits
397 * are compared unless a mask is explicitly specified.
398 */
399 rule->mark_mask = 0xFFFFFFFF;
400 }
401
402 if (tb[FRA_FWMASK])
403 rule->mark_mask = nla_get_u32(tb[FRA_FWMASK]);
404
Thomas Grafe7030872015-07-21 10:44:01 +0200405 if (tb[FRA_TUN_ID])
406 rule->tun_id = nla_get_be64(tb[FRA_TUN_ID]);
407
David Ahern96c63fa2016-06-08 10:55:39 -0700408 if (tb[FRA_L3MDEV]) {
409#ifdef CONFIG_NET_L3_MASTER_DEV
410 rule->l3mdev = nla_get_u8(tb[FRA_L3MDEV]);
411 if (rule->l3mdev != 1)
412#endif
413 goto errout_free;
414 }
415
Thomas Graf14c0b972006-08-04 03:38:38 -0700416 rule->action = frh->action;
417 rule->flags = frh->flags;
Patrick McHardy9e762a42006-08-10 23:09:48 -0700418 rule->table = frh_get_table(frh, tb);
Stefan Tomanek73f56982013-08-03 14:14:43 +0200419 if (tb[FRA_SUPPRESS_PREFIXLEN])
420 rule->suppress_prefixlen = nla_get_u32(tb[FRA_SUPPRESS_PREFIXLEN]);
421 else
422 rule->suppress_prefixlen = -1;
Thomas Graf14c0b972006-08-04 03:38:38 -0700423
Stefan Tomanek6ef94cf2013-08-02 17:19:56 +0200424 if (tb[FRA_SUPPRESS_IFGROUP])
425 rule->suppress_ifgroup = nla_get_u32(tb[FRA_SUPPRESS_IFGROUP]);
Stefan Tomanek73f56982013-08-03 14:14:43 +0200426 else
427 rule->suppress_ifgroup = -1;
Stefan Tomanek6ef94cf2013-08-02 17:19:56 +0200428
Thomas Graf0947c9fe2007-03-26 17:14:15 -0700429 err = -EINVAL;
430 if (tb[FRA_GOTO]) {
431 if (rule->action != FR_ACT_GOTO)
432 goto errout_free;
433
434 rule->target = nla_get_u32(tb[FRA_GOTO]);
435 /* Backward jumps are prohibited to avoid endless loops */
436 if (rule->target <= rule->pref)
437 goto errout_free;
438
Denis V. Lunev76c72d42007-09-16 15:44:27 -0700439 list_for_each_entry(r, &ops->rules_list, list) {
Thomas Graf0947c9fe2007-03-26 17:14:15 -0700440 if (r->pref == rule->target) {
Eric Dumazet7a2b03c2010-10-26 09:24:55 +0000441 RCU_INIT_POINTER(rule->ctarget, r);
Thomas Graf0947c9fe2007-03-26 17:14:15 -0700442 break;
443 }
444 }
445
Eric Dumazet7a2b03c2010-10-26 09:24:55 +0000446 if (rcu_dereference_protected(rule->ctarget, 1) == NULL)
Thomas Graf0947c9fe2007-03-26 17:14:15 -0700447 unresolved = 1;
448 } else if (rule->action == FR_ACT_GOTO)
449 goto errout_free;
450
David Ahern96c63fa2016-06-08 10:55:39 -0700451 if (rule->l3mdev && rule->table)
452 goto errout_free;
453
Mateusz Bajorski153380e2016-06-29 09:22:10 +0200454 if ((nlh->nlmsg_flags & NLM_F_EXCL) &&
455 rule_exists(ops, frh, tb, rule)) {
456 err = -EEXIST;
457 goto errout_free;
458 }
459
Lorenzo Colitti8dc318f2014-03-31 16:23:51 +0900460 /* UID start and end must either both be valid or both unspecified. */
461 rule->uid_start = rule->uid_end = INVALID_UID;
462 if (tb[FRA_UID_START] || tb[FRA_UID_END]) {
463 if (tb[FRA_UID_START] && tb[FRA_UID_END]) {
464 rule->uid_start = fib_nl_uid(tb[FRA_UID_START]);
465 rule->uid_end = fib_nl_uid(tb[FRA_UID_END]);
466 }
467 if (!uid_valid(rule->uid_start) ||
468 !uid_valid(rule->uid_end) ||
469 !uid_lte(rule->uid_start, rule->uid_end))
470 goto errout_free;
471 }
472
Rami Rosen8b3521e2009-05-11 05:52:49 +0000473 err = ops->configure(rule, skb, frh, tb);
Thomas Graf14c0b972006-08-04 03:38:38 -0700474 if (err < 0)
475 goto errout_free;
476
Denis V. Lunev76c72d42007-09-16 15:44:27 -0700477 list_for_each_entry(r, &ops->rules_list, list) {
Thomas Graf14c0b972006-08-04 03:38:38 -0700478 if (r->pref > rule->pref)
479 break;
480 last = r;
481 }
482
483 fib_rule_get(rule);
484
Eric Dumazetebb9fed2010-10-23 09:44:25 +0000485 if (last)
486 list_add_rcu(&rule->list, &last->list);
487 else
488 list_add_rcu(&rule->list, &ops->rules_list);
489
Thomas Graf0947c9fe2007-03-26 17:14:15 -0700490 if (ops->unresolved_rules) {
491 /*
492 * There are unresolved goto rules in the list, check if
493 * any of them are pointing to this new rule.
494 */
Denis V. Lunev76c72d42007-09-16 15:44:27 -0700495 list_for_each_entry(r, &ops->rules_list, list) {
Thomas Graf0947c9fe2007-03-26 17:14:15 -0700496 if (r->action == FR_ACT_GOTO &&
Gao feng561dac22011-09-11 15:36:05 +0000497 r->target == rule->pref &&
498 rtnl_dereference(r->ctarget) == NULL) {
Thomas Graf0947c9fe2007-03-26 17:14:15 -0700499 rcu_assign_pointer(r->ctarget, rule);
500 if (--ops->unresolved_rules == 0)
501 break;
502 }
503 }
504 }
505
506 if (rule->action == FR_ACT_GOTO)
507 ops->nr_goto_rules++;
508
509 if (unresolved)
510 ops->unresolved_rules++;
511
Thomas Grafe7030872015-07-21 10:44:01 +0200512 if (rule->tun_id)
513 ip_tunnel_need_metadata();
514
Eric W. Biederman15e47302012-09-07 20:12:54 +0000515 notify_rule_change(RTM_NEWRULE, rule, ops, nlh, NETLINK_CB(skb).portid);
Thomas Graf73417f62007-03-27 13:56:52 -0700516 flush_route_cache(ops);
Thomas Graf14c0b972006-08-04 03:38:38 -0700517 rules_ops_put(ops);
518 return 0;
519
520errout_free:
521 kfree(rule);
522errout:
523 rules_ops_put(ops);
524 return err;
525}
David Ahern96c63fa2016-06-08 10:55:39 -0700526EXPORT_SYMBOL_GPL(fib_nl_newrule);
Thomas Graf14c0b972006-08-04 03:38:38 -0700527
David Ahern96c63fa2016-06-08 10:55:39 -0700528int fib_nl_delrule(struct sk_buff *skb, struct nlmsghdr *nlh)
Thomas Graf14c0b972006-08-04 03:38:38 -0700529{
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +0900530 struct net *net = sock_net(skb->sk);
Thomas Graf14c0b972006-08-04 03:38:38 -0700531 struct fib_rule_hdr *frh = nlmsg_data(nlh);
532 struct fib_rules_ops *ops = NULL;
Thomas Graf0947c9fe2007-03-26 17:14:15 -0700533 struct fib_rule *rule, *tmp;
Thomas Graf14c0b972006-08-04 03:38:38 -0700534 struct nlattr *tb[FRA_MAX+1];
535 int err = -EINVAL;
536
537 if (nlh->nlmsg_len < nlmsg_msg_size(sizeof(*frh)))
538 goto errout;
539
Denis V. Lunev5fd30ee2008-01-10 03:20:28 -0800540 ops = lookup_rules_ops(net, frh->family);
Thomas Graf14c0b972006-08-04 03:38:38 -0700541 if (ops == NULL) {
Patrick McHardy2fe195c2008-07-01 19:59:37 -0700542 err = -EAFNOSUPPORT;
Thomas Graf14c0b972006-08-04 03:38:38 -0700543 goto errout;
544 }
545
546 err = nlmsg_parse(nlh, sizeof(*frh), tb, FRA_MAX, ops->policy);
547 if (err < 0)
548 goto errout;
549
Thomas Grafe1701c62007-03-24 12:46:02 -0700550 err = validate_rulemsg(frh, tb, ops);
551 if (err < 0)
552 goto errout;
553
Denis V. Lunev76c72d42007-09-16 15:44:27 -0700554 list_for_each_entry(rule, &ops->rules_list, list) {
Thomas Graf14c0b972006-08-04 03:38:38 -0700555 if (frh->action && (frh->action != rule->action))
556 continue;
557
Andreas Henriksson13eb2ab2013-11-07 18:26:38 +0100558 if (frh_get_table(frh, tb) &&
559 (frh_get_table(frh, tb) != rule->table))
Thomas Graf14c0b972006-08-04 03:38:38 -0700560 continue;
561
562 if (tb[FRA_PRIORITY] &&
563 (rule->pref != nla_get_u32(tb[FRA_PRIORITY])))
564 continue;
565
Patrick McHardy491deb24b2009-12-03 01:25:54 +0000566 if (tb[FRA_IIFNAME] &&
567 nla_strcmp(tb[FRA_IIFNAME], rule->iifname))
Thomas Graf14c0b972006-08-04 03:38:38 -0700568 continue;
569
Patrick McHardy1b038a52009-12-03 01:25:56 +0000570 if (tb[FRA_OIFNAME] &&
571 nla_strcmp(tb[FRA_OIFNAME], rule->oifname))
572 continue;
573
Thomas Grafb8964ed2006-11-09 15:22:18 -0800574 if (tb[FRA_FWMARK] &&
575 (rule->mark != nla_get_u32(tb[FRA_FWMARK])))
576 continue;
577
578 if (tb[FRA_FWMASK] &&
579 (rule->mark_mask != nla_get_u32(tb[FRA_FWMASK])))
580 continue;
581
Thomas Grafe7030872015-07-21 10:44:01 +0200582 if (tb[FRA_TUN_ID] &&
583 (rule->tun_id != nla_get_be64(tb[FRA_TUN_ID])))
584 continue;
585
David Ahern96c63fa2016-06-08 10:55:39 -0700586 if (tb[FRA_L3MDEV] &&
587 (rule->l3mdev != nla_get_u8(tb[FRA_L3MDEV])))
588 continue;
589
Lorenzo Colitti8dc318f2014-03-31 16:23:51 +0900590 if (tb[FRA_UID_START] &&
591 !uid_eq(rule->uid_start, fib_nl_uid(tb[FRA_UID_START])))
592 continue;
593
594 if (tb[FRA_UID_END] &&
595 !uid_eq(rule->uid_end, fib_nl_uid(tb[FRA_UID_END])))
596 continue;
597
Thomas Graf14c0b972006-08-04 03:38:38 -0700598 if (!ops->compare(rule, frh, tb))
599 continue;
600
601 if (rule->flags & FIB_RULE_PERMANENT) {
602 err = -EPERM;
603 goto errout;
604 }
605
Alexander Duyck0ddcf432015-03-06 13:47:00 -0800606 if (ops->delete) {
607 err = ops->delete(rule);
608 if (err)
609 goto errout;
610 }
611
Thomas Grafe7030872015-07-21 10:44:01 +0200612 if (rule->tun_id)
613 ip_tunnel_unneed_metadata();
614
Thomas Graf14c0b972006-08-04 03:38:38 -0700615 list_del_rcu(&rule->list);
Thomas Graf0947c9fe2007-03-26 17:14:15 -0700616
Yan, Zhengafaef732011-10-17 15:20:28 +0000617 if (rule->action == FR_ACT_GOTO) {
Thomas Graf0947c9fe2007-03-26 17:14:15 -0700618 ops->nr_goto_rules--;
Yan, Zhengafaef732011-10-17 15:20:28 +0000619 if (rtnl_dereference(rule->ctarget) == NULL)
620 ops->unresolved_rules--;
621 }
Thomas Graf0947c9fe2007-03-26 17:14:15 -0700622
623 /*
624 * Check if this rule is a target to any of them. If so,
625 * disable them. As this operation is eventually very
626 * expensive, it is only performed if goto rules have
627 * actually been added.
628 */
629 if (ops->nr_goto_rules > 0) {
Denis V. Lunev76c72d42007-09-16 15:44:27 -0700630 list_for_each_entry(tmp, &ops->rules_list, list) {
Eric Dumazet7a2b03c2010-10-26 09:24:55 +0000631 if (rtnl_dereference(tmp->ctarget) == rule) {
Stephen Hemmingera9b3cd72011-08-01 16:19:00 +0000632 RCU_INIT_POINTER(tmp->ctarget, NULL);
Thomas Graf0947c9fe2007-03-26 17:14:15 -0700633 ops->unresolved_rules++;
634 }
635 }
636 }
637
Denis V. Lunev9e3a5482008-01-20 16:46:41 -0800638 notify_rule_change(RTM_DELRULE, rule, ops, nlh,
Eric W. Biederman15e47302012-09-07 20:12:54 +0000639 NETLINK_CB(skb).portid);
Thomas Graf14c0b972006-08-04 03:38:38 -0700640 fib_rule_put(rule);
Thomas Graf73417f62007-03-27 13:56:52 -0700641 flush_route_cache(ops);
Thomas Graf14c0b972006-08-04 03:38:38 -0700642 rules_ops_put(ops);
643 return 0;
644 }
645
646 err = -ENOENT;
647errout:
648 rules_ops_put(ops);
649 return err;
650}
David Ahern96c63fa2016-06-08 10:55:39 -0700651EXPORT_SYMBOL_GPL(fib_nl_delrule);
Thomas Graf14c0b972006-08-04 03:38:38 -0700652
Thomas Graf339bf982006-11-10 14:10:15 -0800653static inline size_t fib_rule_nlmsg_size(struct fib_rules_ops *ops,
654 struct fib_rule *rule)
655{
656 size_t payload = NLMSG_ALIGN(sizeof(struct fib_rule_hdr))
Patrick McHardy491deb24b2009-12-03 01:25:54 +0000657 + nla_total_size(IFNAMSIZ) /* FRA_IIFNAME */
Patrick McHardy1b038a52009-12-03 01:25:56 +0000658 + nla_total_size(IFNAMSIZ) /* FRA_OIFNAME */
Thomas Graf339bf982006-11-10 14:10:15 -0800659 + nla_total_size(4) /* FRA_PRIORITY */
660 + nla_total_size(4) /* FRA_TABLE */
Stefan Tomanek73f56982013-08-03 14:14:43 +0200661 + nla_total_size(4) /* FRA_SUPPRESS_PREFIXLEN */
Stefan Tomanek6ef94cf2013-08-02 17:19:56 +0200662 + nla_total_size(4) /* FRA_SUPPRESS_IFGROUP */
Thomas Graf339bf982006-11-10 14:10:15 -0800663 + nla_total_size(4) /* FRA_FWMARK */
Thomas Grafe7030872015-07-21 10:44:01 +0200664 + nla_total_size(4) /* FRA_FWMASK */
Lorenzo Colitti8dc318f2014-03-31 16:23:51 +0900665 + nla_total_size_64bit(8) /* FRA_TUN_ID */
666 + nla_total_size(4) /* FRA_UID_START */
667 + nla_total_size(4); /* FRA_UID_END */
Thomas Graf339bf982006-11-10 14:10:15 -0800668
669 if (ops->nlmsg_payload)
670 payload += ops->nlmsg_payload(rule);
671
672 return payload;
673}
674
Thomas Graf14c0b972006-08-04 03:38:38 -0700675static int fib_nl_fill_rule(struct sk_buff *skb, struct fib_rule *rule,
676 u32 pid, u32 seq, int type, int flags,
677 struct fib_rules_ops *ops)
678{
679 struct nlmsghdr *nlh;
680 struct fib_rule_hdr *frh;
681
682 nlh = nlmsg_put(skb, pid, seq, type, sizeof(*frh), flags);
683 if (nlh == NULL)
Patrick McHardy26932562007-01-31 23:16:40 -0800684 return -EMSGSIZE;
Thomas Graf14c0b972006-08-04 03:38:38 -0700685
686 frh = nlmsg_data(nlh);
Patrick McHardy28bb1722010-04-13 05:03:16 +0000687 frh->family = ops->family;
Thomas Graf14c0b972006-08-04 03:38:38 -0700688 frh->table = rule->table;
David S. Miller0e3cea72012-04-01 20:47:01 -0400689 if (nla_put_u32(skb, FRA_TABLE, rule->table))
690 goto nla_put_failure;
Stefan Tomanek73f56982013-08-03 14:14:43 +0200691 if (nla_put_u32(skb, FRA_SUPPRESS_PREFIXLEN, rule->suppress_prefixlen))
Stefan Tomanek7764a452013-08-01 02:17:15 +0200692 goto nla_put_failure;
Thomas Graf14c0b972006-08-04 03:38:38 -0700693 frh->res1 = 0;
694 frh->res2 = 0;
695 frh->action = rule->action;
696 frh->flags = rule->flags;
697
Eric Dumazet7a2b03c2010-10-26 09:24:55 +0000698 if (rule->action == FR_ACT_GOTO &&
Eric Dumazet33d480c2011-08-11 19:30:52 +0000699 rcu_access_pointer(rule->ctarget) == NULL)
Thomas Graf0947c9fe2007-03-26 17:14:15 -0700700 frh->flags |= FIB_RULE_UNRESOLVED;
701
Patrick McHardy491deb24b2009-12-03 01:25:54 +0000702 if (rule->iifname[0]) {
David S. Miller0e3cea72012-04-01 20:47:01 -0400703 if (nla_put_string(skb, FRA_IIFNAME, rule->iifname))
704 goto nla_put_failure;
Patrick McHardy491deb24b2009-12-03 01:25:54 +0000705 if (rule->iifindex == -1)
706 frh->flags |= FIB_RULE_IIF_DETACHED;
Thomas Graf2b443682007-03-26 17:37:59 -0700707 }
708
Patrick McHardy1b038a52009-12-03 01:25:56 +0000709 if (rule->oifname[0]) {
David S. Miller0e3cea72012-04-01 20:47:01 -0400710 if (nla_put_string(skb, FRA_OIFNAME, rule->oifname))
711 goto nla_put_failure;
Patrick McHardy1b038a52009-12-03 01:25:56 +0000712 if (rule->oifindex == -1)
713 frh->flags |= FIB_RULE_OIF_DETACHED;
714 }
715
David S. Miller0e3cea72012-04-01 20:47:01 -0400716 if ((rule->pref &&
717 nla_put_u32(skb, FRA_PRIORITY, rule->pref)) ||
718 (rule->mark &&
719 nla_put_u32(skb, FRA_FWMARK, rule->mark)) ||
720 ((rule->mark_mask || rule->mark) &&
721 nla_put_u32(skb, FRA_FWMASK, rule->mark_mask)) ||
722 (rule->target &&
Thomas Grafe7030872015-07-21 10:44:01 +0200723 nla_put_u32(skb, FRA_GOTO, rule->target)) ||
724 (rule->tun_id &&
David Ahern96c63fa2016-06-08 10:55:39 -0700725 nla_put_be64(skb, FRA_TUN_ID, rule->tun_id, FRA_PAD)) ||
726 (rule->l3mdev &&
Lorenzo Colitti8dc318f2014-03-31 16:23:51 +0900727 nla_put_u8(skb, FRA_L3MDEV, rule->l3mdev)) ||
728 (uid_valid(rule->uid_start) &&
729 nla_put_uid(skb, FRA_UID_START, rule->uid_start)) ||
730 (uid_valid(rule->uid_end) &&
731 nla_put_uid(skb, FRA_UID_END, rule->uid_end)))
David S. Miller0e3cea72012-04-01 20:47:01 -0400732 goto nla_put_failure;
Stefan Tomanek6ef94cf2013-08-02 17:19:56 +0200733
734 if (rule->suppress_ifgroup != -1) {
735 if (nla_put_u32(skb, FRA_SUPPRESS_IFGROUP, rule->suppress_ifgroup))
736 goto nla_put_failure;
737 }
738
Rami Rosen04af8cf2009-05-20 17:26:23 -0700739 if (ops->fill(rule, skb, frh) < 0)
Thomas Graf14c0b972006-08-04 03:38:38 -0700740 goto nla_put_failure;
741
Johannes Berg053c0952015-01-16 22:09:00 +0100742 nlmsg_end(skb, nlh);
743 return 0;
Thomas Graf14c0b972006-08-04 03:38:38 -0700744
745nla_put_failure:
Patrick McHardy26932562007-01-31 23:16:40 -0800746 nlmsg_cancel(skb, nlh);
747 return -EMSGSIZE;
Thomas Graf14c0b972006-08-04 03:38:38 -0700748}
749
Thomas Grafc4546732007-03-25 23:24:24 -0700750static int dump_rules(struct sk_buff *skb, struct netlink_callback *cb,
751 struct fib_rules_ops *ops)
Thomas Graf14c0b972006-08-04 03:38:38 -0700752{
753 int idx = 0;
754 struct fib_rule *rule;
Wilson Kok41fc0142015-09-22 21:40:22 -0700755 int err = 0;
Thomas Graf14c0b972006-08-04 03:38:38 -0700756
Eric Dumazete67f88d2011-04-27 22:56:07 +0000757 rcu_read_lock();
758 list_for_each_entry_rcu(rule, &ops->rules_list, list) {
Thomas Grafc4546732007-03-25 23:24:24 -0700759 if (idx < cb->args[1])
Thomas Graf14c0b972006-08-04 03:38:38 -0700760 goto skip;
761
Wilson Kok41fc0142015-09-22 21:40:22 -0700762 err = fib_nl_fill_rule(skb, rule, NETLINK_CB(cb->skb).portid,
763 cb->nlh->nlmsg_seq, RTM_NEWRULE,
764 NLM_F_MULTI, ops);
765 if (err)
Thomas Graf14c0b972006-08-04 03:38:38 -0700766 break;
767skip:
768 idx++;
769 }
Eric Dumazet2907c352011-05-25 07:34:04 +0000770 rcu_read_unlock();
Thomas Grafc4546732007-03-25 23:24:24 -0700771 cb->args[1] = idx;
Thomas Graf14c0b972006-08-04 03:38:38 -0700772 rules_ops_put(ops);
773
Wilson Kok41fc0142015-09-22 21:40:22 -0700774 return err;
Thomas Graf14c0b972006-08-04 03:38:38 -0700775}
776
Thomas Grafc4546732007-03-25 23:24:24 -0700777static int fib_nl_dumprule(struct sk_buff *skb, struct netlink_callback *cb)
778{
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +0900779 struct net *net = sock_net(skb->sk);
Thomas Grafc4546732007-03-25 23:24:24 -0700780 struct fib_rules_ops *ops;
781 int idx = 0, family;
782
783 family = rtnl_msg_family(cb->nlh);
784 if (family != AF_UNSPEC) {
785 /* Protocol specific dump request */
Denis V. Lunev5fd30ee2008-01-10 03:20:28 -0800786 ops = lookup_rules_ops(net, family);
Thomas Grafc4546732007-03-25 23:24:24 -0700787 if (ops == NULL)
788 return -EAFNOSUPPORT;
789
Wilson Kok41fc0142015-09-22 21:40:22 -0700790 dump_rules(skb, cb, ops);
791
792 return skb->len;
Thomas Grafc4546732007-03-25 23:24:24 -0700793 }
794
795 rcu_read_lock();
Denis V. Lunev5fd30ee2008-01-10 03:20:28 -0800796 list_for_each_entry_rcu(ops, &net->rules_ops, list) {
Thomas Grafc4546732007-03-25 23:24:24 -0700797 if (idx < cb->args[0] || !try_module_get(ops->owner))
798 goto skip;
799
800 if (dump_rules(skb, cb, ops) < 0)
801 break;
802
803 cb->args[1] = 0;
Eric Dumazet2fb35732010-03-09 20:03:38 +0000804skip:
Thomas Grafc4546732007-03-25 23:24:24 -0700805 idx++;
806 }
807 rcu_read_unlock();
808 cb->args[0] = idx;
809
810 return skb->len;
811}
Thomas Graf14c0b972006-08-04 03:38:38 -0700812
Denis V. Lunev9e3a5482008-01-20 16:46:41 -0800813static void notify_rule_change(int event, struct fib_rule *rule,
Thomas Grafc17084d2006-08-15 00:32:48 -0700814 struct fib_rules_ops *ops, struct nlmsghdr *nlh,
815 u32 pid)
Thomas Graf14c0b972006-08-04 03:38:38 -0700816{
Denis V. Lunev9e3a5482008-01-20 16:46:41 -0800817 struct net *net;
Thomas Grafc17084d2006-08-15 00:32:48 -0700818 struct sk_buff *skb;
819 int err = -ENOBUFS;
Thomas Graf14c0b972006-08-04 03:38:38 -0700820
Denis V. Lunev9e3a5482008-01-20 16:46:41 -0800821 net = ops->fro_net;
Thomas Graf339bf982006-11-10 14:10:15 -0800822 skb = nlmsg_new(fib_rule_nlmsg_size(ops, rule), GFP_KERNEL);
Thomas Graf14c0b972006-08-04 03:38:38 -0700823 if (skb == NULL)
Thomas Grafc17084d2006-08-15 00:32:48 -0700824 goto errout;
825
826 err = fib_nl_fill_rule(skb, rule, pid, nlh->nlmsg_seq, event, 0, ops);
Patrick McHardy26932562007-01-31 23:16:40 -0800827 if (err < 0) {
828 /* -EMSGSIZE implies BUG in fib_rule_nlmsg_size() */
829 WARN_ON(err == -EMSGSIZE);
830 kfree_skb(skb);
831 goto errout;
832 }
Denis V. Lunev9e3a5482008-01-20 16:46:41 -0800833
Pablo Neira Ayuso1ce85fe2009-02-24 23:18:28 -0800834 rtnl_notify(skb, net, pid, ops->nlgroup, nlh, GFP_KERNEL);
835 return;
Thomas Grafc17084d2006-08-15 00:32:48 -0700836errout:
837 if (err < 0)
Denis V. Lunev5fd30ee2008-01-10 03:20:28 -0800838 rtnl_set_sk_err(net, ops->nlgroup, err);
Thomas Graf14c0b972006-08-04 03:38:38 -0700839}
840
841static void attach_rules(struct list_head *rules, struct net_device *dev)
842{
843 struct fib_rule *rule;
844
845 list_for_each_entry(rule, rules, list) {
Patrick McHardy491deb24b2009-12-03 01:25:54 +0000846 if (rule->iifindex == -1 &&
847 strcmp(dev->name, rule->iifname) == 0)
848 rule->iifindex = dev->ifindex;
Patrick McHardy1b038a52009-12-03 01:25:56 +0000849 if (rule->oifindex == -1 &&
850 strcmp(dev->name, rule->oifname) == 0)
851 rule->oifindex = dev->ifindex;
Thomas Graf14c0b972006-08-04 03:38:38 -0700852 }
853}
854
855static void detach_rules(struct list_head *rules, struct net_device *dev)
856{
857 struct fib_rule *rule;
858
Patrick McHardy1b038a52009-12-03 01:25:56 +0000859 list_for_each_entry(rule, rules, list) {
Patrick McHardy491deb24b2009-12-03 01:25:54 +0000860 if (rule->iifindex == dev->ifindex)
861 rule->iifindex = -1;
Patrick McHardy1b038a52009-12-03 01:25:56 +0000862 if (rule->oifindex == dev->ifindex)
863 rule->oifindex = -1;
864 }
Thomas Graf14c0b972006-08-04 03:38:38 -0700865}
866
867
868static int fib_rules_event(struct notifier_block *this, unsigned long event,
Jiri Pirko351638e2013-05-28 01:30:21 +0000869 void *ptr)
Thomas Graf14c0b972006-08-04 03:38:38 -0700870{
Jiri Pirko351638e2013-05-28 01:30:21 +0000871 struct net_device *dev = netdev_notifier_info_to_dev(ptr);
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +0900872 struct net *net = dev_net(dev);
Thomas Graf14c0b972006-08-04 03:38:38 -0700873 struct fib_rules_ops *ops;
874
Eric Dumazet748e2d92012-08-22 21:50:59 +0000875 ASSERT_RTNL();
Thomas Graf14c0b972006-08-04 03:38:38 -0700876
877 switch (event) {
878 case NETDEV_REGISTER:
Denis V. Lunev5fd30ee2008-01-10 03:20:28 -0800879 list_for_each_entry(ops, &net->rules_ops, list)
Denis V. Lunev76c72d42007-09-16 15:44:27 -0700880 attach_rules(&ops->rules_list, dev);
Thomas Graf14c0b972006-08-04 03:38:38 -0700881 break;
882
Maciej Żenczykowski946c0322014-02-07 16:23:48 -0800883 case NETDEV_CHANGENAME:
884 list_for_each_entry(ops, &net->rules_ops, list) {
885 detach_rules(&ops->rules_list, dev);
886 attach_rules(&ops->rules_list, dev);
887 }
888 break;
889
Thomas Graf14c0b972006-08-04 03:38:38 -0700890 case NETDEV_UNREGISTER:
Denis V. Lunev5fd30ee2008-01-10 03:20:28 -0800891 list_for_each_entry(ops, &net->rules_ops, list)
Denis V. Lunev76c72d42007-09-16 15:44:27 -0700892 detach_rules(&ops->rules_list, dev);
Thomas Graf14c0b972006-08-04 03:38:38 -0700893 break;
894 }
895
Thomas Graf14c0b972006-08-04 03:38:38 -0700896 return NOTIFY_DONE;
897}
898
899static struct notifier_block fib_rules_notifier = {
900 .notifier_call = fib_rules_event,
901};
902
Alexey Dobriyan2c8c1e72010-01-17 03:35:32 +0000903static int __net_init fib_rules_net_init(struct net *net)
Denis V. Lunev5fd30ee2008-01-10 03:20:28 -0800904{
905 INIT_LIST_HEAD(&net->rules_ops);
906 spin_lock_init(&net->rules_mod_lock);
907 return 0;
908}
909
910static struct pernet_operations fib_rules_net_ops = {
911 .init = fib_rules_net_init,
912};
913
Thomas Graf14c0b972006-08-04 03:38:38 -0700914static int __init fib_rules_init(void)
915{
Denis V. Lunev5fd30ee2008-01-10 03:20:28 -0800916 int err;
Greg Rosec7ac8672011-06-10 01:27:09 +0000917 rtnl_register(PF_UNSPEC, RTM_NEWRULE, fib_nl_newrule, NULL, NULL);
918 rtnl_register(PF_UNSPEC, RTM_DELRULE, fib_nl_delrule, NULL, NULL);
919 rtnl_register(PF_UNSPEC, RTM_GETRULE, NULL, fib_nl_dumprule, NULL);
Thomas Graf9d9e6a52007-03-25 23:20:05 -0700920
Eric W. Biederman5d6d4802008-11-07 22:52:34 -0800921 err = register_pernet_subsys(&fib_rules_net_ops);
Denis V. Lunev5fd30ee2008-01-10 03:20:28 -0800922 if (err < 0)
923 goto fail;
924
Eric W. Biederman5d6d4802008-11-07 22:52:34 -0800925 err = register_netdevice_notifier(&fib_rules_notifier);
Denis V. Lunev5fd30ee2008-01-10 03:20:28 -0800926 if (err < 0)
927 goto fail_unregister;
Eric W. Biederman5d6d4802008-11-07 22:52:34 -0800928
Denis V. Lunev5fd30ee2008-01-10 03:20:28 -0800929 return 0;
930
931fail_unregister:
Eric W. Biederman5d6d4802008-11-07 22:52:34 -0800932 unregister_pernet_subsys(&fib_rules_net_ops);
Denis V. Lunev5fd30ee2008-01-10 03:20:28 -0800933fail:
934 rtnl_unregister(PF_UNSPEC, RTM_NEWRULE);
935 rtnl_unregister(PF_UNSPEC, RTM_DELRULE);
936 rtnl_unregister(PF_UNSPEC, RTM_GETRULE);
937 return err;
Thomas Graf14c0b972006-08-04 03:38:38 -0700938}
939
940subsys_initcall(fib_rules_init);