blob: c03dd2104d330044b6c92a49dbbaf340c5b71d52 [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;
Denis V. Lunev2994c632007-11-10 22:12:03 -080036
Stefan Tomanek73f56982013-08-03 14:14:43 +020037 r->suppress_prefixlen = -1;
38 r->suppress_ifgroup = -1;
39
Denis V. Lunev2994c632007-11-10 22:12:03 -080040 /* The lock is not required here, the list in unreacheable
41 * at the moment this function is called */
42 list_add_tail(&r->list, &ops->rules_list);
43 return 0;
44}
45EXPORT_SYMBOL(fib_default_rule_add);
46
Phil Sutterf53de1e2015-09-09 14:20:56 +020047static u32 fib_default_rule_pref(struct fib_rules_ops *ops)
Patrick McHardyd8a566b2010-04-13 05:03:15 +000048{
49 struct list_head *pos;
50 struct fib_rule *rule;
51
52 if (!list_empty(&ops->rules_list)) {
53 pos = ops->rules_list.next;
54 if (pos->next != &ops->rules_list) {
55 rule = list_entry(pos->next, struct fib_rule, list);
56 if (rule->pref)
57 return rule->pref - 1;
58 }
59 }
60
61 return 0;
62}
Patrick McHardyd8a566b2010-04-13 05:03:15 +000063
Denis V. Lunev9e3a5482008-01-20 16:46:41 -080064static void notify_rule_change(int event, struct fib_rule *rule,
Thomas Grafc17084d2006-08-15 00:32:48 -070065 struct fib_rules_ops *ops, struct nlmsghdr *nlh,
66 u32 pid);
Thomas Graf14c0b972006-08-04 03:38:38 -070067
Denis V. Lunev5fd30ee2008-01-10 03:20:28 -080068static struct fib_rules_ops *lookup_rules_ops(struct net *net, int family)
Thomas Graf14c0b972006-08-04 03:38:38 -070069{
70 struct fib_rules_ops *ops;
71
72 rcu_read_lock();
Denis V. Lunev5fd30ee2008-01-10 03:20:28 -080073 list_for_each_entry_rcu(ops, &net->rules_ops, list) {
Thomas Graf14c0b972006-08-04 03:38:38 -070074 if (ops->family == family) {
75 if (!try_module_get(ops->owner))
76 ops = NULL;
77 rcu_read_unlock();
78 return ops;
79 }
80 }
81 rcu_read_unlock();
82
83 return NULL;
84}
85
86static void rules_ops_put(struct fib_rules_ops *ops)
87{
88 if (ops)
89 module_put(ops->owner);
90}
91
Thomas Graf73417f62007-03-27 13:56:52 -070092static void flush_route_cache(struct fib_rules_ops *ops)
93{
94 if (ops->flush_cache)
Denis V. Lunevae299fc2008-07-05 19:01:28 -070095 ops->flush_cache(ops);
Thomas Graf73417f62007-03-27 13:56:52 -070096}
97
Eric W. Biedermane9c51582009-12-03 12:22:55 -080098static int __fib_rules_register(struct fib_rules_ops *ops)
Thomas Graf14c0b972006-08-04 03:38:38 -070099{
100 int err = -EEXIST;
101 struct fib_rules_ops *o;
Denis V. Lunev9e3a5482008-01-20 16:46:41 -0800102 struct net *net;
103
104 net = ops->fro_net;
Thomas Graf14c0b972006-08-04 03:38:38 -0700105
106 if (ops->rule_size < sizeof(struct fib_rule))
107 return -EINVAL;
108
109 if (ops->match == NULL || ops->configure == NULL ||
110 ops->compare == NULL || ops->fill == NULL ||
111 ops->action == NULL)
112 return -EINVAL;
113
Denis V. Lunev5fd30ee2008-01-10 03:20:28 -0800114 spin_lock(&net->rules_mod_lock);
115 list_for_each_entry(o, &net->rules_ops, list)
Thomas Graf14c0b972006-08-04 03:38:38 -0700116 if (ops->family == o->family)
117 goto errout;
118
Denis V. Lunev5fd30ee2008-01-10 03:20:28 -0800119 list_add_tail_rcu(&ops->list, &net->rules_ops);
Thomas Graf14c0b972006-08-04 03:38:38 -0700120 err = 0;
121errout:
Denis V. Lunev5fd30ee2008-01-10 03:20:28 -0800122 spin_unlock(&net->rules_mod_lock);
Thomas Graf14c0b972006-08-04 03:38:38 -0700123
124 return err;
125}
126
Eric W. Biedermane9c51582009-12-03 12:22:55 -0800127struct fib_rules_ops *
Patrick McHardy3d0c9c42010-04-26 16:02:04 +0200128fib_rules_register(const struct fib_rules_ops *tmpl, struct net *net)
Eric W. Biedermane9c51582009-12-03 12:22:55 -0800129{
130 struct fib_rules_ops *ops;
131 int err;
132
Eric Dumazet2fb35732010-03-09 20:03:38 +0000133 ops = kmemdup(tmpl, sizeof(*ops), GFP_KERNEL);
Eric W. Biedermane9c51582009-12-03 12:22:55 -0800134 if (ops == NULL)
135 return ERR_PTR(-ENOMEM);
136
137 INIT_LIST_HEAD(&ops->rules_list);
138 ops->fro_net = net;
139
140 err = __fib_rules_register(ops);
141 if (err) {
142 kfree(ops);
143 ops = ERR_PTR(err);
144 }
145
146 return ops;
147}
Thomas Graf14c0b972006-08-04 03:38:38 -0700148EXPORT_SYMBOL_GPL(fib_rules_register);
149
stephen hemminger1df99162010-10-04 20:14:17 +0000150static void fib_rules_cleanup_ops(struct fib_rules_ops *ops)
Thomas Graf14c0b972006-08-04 03:38:38 -0700151{
152 struct fib_rule *rule, *tmp;
153
Denis V. Lunev76c72d42007-09-16 15:44:27 -0700154 list_for_each_entry_safe(rule, tmp, &ops->rules_list, list) {
Thomas Graf14c0b972006-08-04 03:38:38 -0700155 list_del_rcu(&rule->list);
David S. Miller7a9bc9b2012-06-29 01:32:45 -0700156 if (ops->delete)
157 ops->delete(rule);
Thomas Graf14c0b972006-08-04 03:38:38 -0700158 fib_rule_put(rule);
159 }
160}
161
Denis V. Lunev9e3a5482008-01-20 16:46:41 -0800162void fib_rules_unregister(struct fib_rules_ops *ops)
Thomas Graf14c0b972006-08-04 03:38:38 -0700163{
Denis V. Lunev9e3a5482008-01-20 16:46:41 -0800164 struct net *net = ops->fro_net;
Thomas Graf14c0b972006-08-04 03:38:38 -0700165
Denis V. Lunev5fd30ee2008-01-10 03:20:28 -0800166 spin_lock(&net->rules_mod_lock);
Denis V. Lunev72132c1b2008-01-14 22:59:30 -0800167 list_del_rcu(&ops->list);
Denis V. Lunev5fd30ee2008-01-10 03:20:28 -0800168 spin_unlock(&net->rules_mod_lock);
Thomas Graf14c0b972006-08-04 03:38:38 -0700169
WANG Cong419df122015-03-31 11:01:46 -0700170 fib_rules_cleanup_ops(ops);
Eric W. Biedermanefd7ef12015-03-11 23:04:08 -0500171 kfree_rcu(ops, rcu);
Thomas Graf14c0b972006-08-04 03:38:38 -0700172}
Thomas Graf14c0b972006-08-04 03:38:38 -0700173EXPORT_SYMBOL_GPL(fib_rules_unregister);
174
Thomas Graf3dfbcc42006-11-09 15:23:20 -0800175static int fib_rule_match(struct fib_rule *rule, struct fib_rules_ops *ops,
David Ahern96c63fa2016-06-08 10:55:39 -0700176 struct flowi *fl, int flags,
177 struct fib_lookup_arg *arg)
Thomas Graf3dfbcc42006-11-09 15:23:20 -0800178{
179 int ret = 0;
180
David S. Miller1d28f422011-03-12 00:29:39 -0500181 if (rule->iifindex && (rule->iifindex != fl->flowi_iif))
Thomas Graf3dfbcc42006-11-09 15:23:20 -0800182 goto out;
183
David S. Miller1d28f422011-03-12 00:29:39 -0500184 if (rule->oifindex && (rule->oifindex != fl->flowi_oif))
Patrick McHardy1b038a52009-12-03 01:25:56 +0000185 goto out;
186
David S. Miller1d28f422011-03-12 00:29:39 -0500187 if ((rule->mark ^ fl->flowi_mark) & rule->mark_mask)
Thomas Graf3dfbcc42006-11-09 15:23:20 -0800188 goto out;
189
Thomas Grafe7030872015-07-21 10:44:01 +0200190 if (rule->tun_id && (rule->tun_id != fl->flowi_tun_key.tun_id))
191 goto out;
192
David Ahern96c63fa2016-06-08 10:55:39 -0700193 if (rule->l3mdev && !l3mdev_fib_rule_match(rule->fr_net, fl, arg))
194 goto out;
195
Thomas Graf3dfbcc42006-11-09 15:23:20 -0800196 ret = ops->match(rule, fl, flags);
197out:
198 return (rule->flags & FIB_RULE_INVERT) ? !ret : ret;
199}
200
Thomas Graf14c0b972006-08-04 03:38:38 -0700201int fib_rules_lookup(struct fib_rules_ops *ops, struct flowi *fl,
202 int flags, struct fib_lookup_arg *arg)
203{
204 struct fib_rule *rule;
205 int err;
206
207 rcu_read_lock();
208
Denis V. Lunev76c72d42007-09-16 15:44:27 -0700209 list_for_each_entry_rcu(rule, &ops->rules_list, list) {
Thomas Graf0947c9fe2007-03-26 17:14:15 -0700210jumped:
David Ahern96c63fa2016-06-08 10:55:39 -0700211 if (!fib_rule_match(rule, ops, fl, flags, arg))
Thomas Graf14c0b972006-08-04 03:38:38 -0700212 continue;
213
Thomas Graf0947c9fe2007-03-26 17:14:15 -0700214 if (rule->action == FR_ACT_GOTO) {
215 struct fib_rule *target;
216
217 target = rcu_dereference(rule->ctarget);
218 if (target == NULL) {
219 continue;
220 } else {
221 rule = target;
222 goto jumped;
223 }
Thomas Graffa0b2d12007-03-26 17:38:53 -0700224 } else if (rule->action == FR_ACT_NOP)
225 continue;
226 else
Thomas Graf0947c9fe2007-03-26 17:14:15 -0700227 err = ops->action(rule, fl, flags, arg);
228
Stefan Tomanek7764a452013-08-01 02:17:15 +0200229 if (!err && ops->suppress && ops->suppress(rule, arg))
230 continue;
231
Thomas Graf14c0b972006-08-04 03:38:38 -0700232 if (err != -EAGAIN) {
Eric Dumazetebc0ffa2010-10-05 10:41:36 +0000233 if ((arg->flags & FIB_LOOKUP_NOREF) ||
234 likely(atomic_inc_not_zero(&rule->refcnt))) {
Eric Dumazet7fa7cb72010-09-27 04:18:27 +0000235 arg->rule = rule;
236 goto out;
237 }
238 break;
Thomas Graf14c0b972006-08-04 03:38:38 -0700239 }
240 }
241
Steven Whitehouse83886b62007-03-30 13:34:27 -0700242 err = -ESRCH;
Thomas Graf14c0b972006-08-04 03:38:38 -0700243out:
244 rcu_read_unlock();
245
246 return err;
247}
Thomas Graf14c0b972006-08-04 03:38:38 -0700248EXPORT_SYMBOL_GPL(fib_rules_lookup);
249
Thomas Grafe1701c62007-03-24 12:46:02 -0700250static int validate_rulemsg(struct fib_rule_hdr *frh, struct nlattr **tb,
251 struct fib_rules_ops *ops)
252{
253 int err = -EINVAL;
254
255 if (frh->src_len)
256 if (tb[FRA_SRC] == NULL ||
257 frh->src_len > (ops->addr_size * 8) ||
258 nla_len(tb[FRA_SRC]) != ops->addr_size)
259 goto errout;
260
261 if (frh->dst_len)
262 if (tb[FRA_DST] == NULL ||
263 frh->dst_len > (ops->addr_size * 8) ||
264 nla_len(tb[FRA_DST]) != ops->addr_size)
265 goto errout;
266
267 err = 0;
268errout:
269 return err;
270}
271
Mateusz Bajorski153380e2016-06-29 09:22:10 +0200272static int rule_exists(struct fib_rules_ops *ops, struct fib_rule_hdr *frh,
273 struct nlattr **tb, struct fib_rule *rule)
274{
275 struct fib_rule *r;
276
277 list_for_each_entry(r, &ops->rules_list, list) {
278 if (r->action != rule->action)
279 continue;
280
281 if (r->table != rule->table)
282 continue;
283
284 if (r->pref != rule->pref)
285 continue;
286
287 if (memcmp(r->iifname, rule->iifname, IFNAMSIZ))
288 continue;
289
290 if (memcmp(r->oifname, rule->oifname, IFNAMSIZ))
291 continue;
292
293 if (r->mark != rule->mark)
294 continue;
295
296 if (r->mark_mask != rule->mark_mask)
297 continue;
298
299 if (r->tun_id != rule->tun_id)
300 continue;
301
302 if (r->fr_net != rule->fr_net)
303 continue;
304
305 if (r->l3mdev != rule->l3mdev)
306 continue;
307
308 if (!ops->compare(r, frh, tb))
309 continue;
310 return 1;
311 }
312 return 0;
313}
314
David Ahern96c63fa2016-06-08 10:55:39 -0700315int fib_nl_newrule(struct sk_buff *skb, struct nlmsghdr *nlh)
Thomas Graf14c0b972006-08-04 03:38:38 -0700316{
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +0900317 struct net *net = sock_net(skb->sk);
Thomas Graf14c0b972006-08-04 03:38:38 -0700318 struct fib_rule_hdr *frh = nlmsg_data(nlh);
319 struct fib_rules_ops *ops = NULL;
320 struct fib_rule *rule, *r, *last = NULL;
321 struct nlattr *tb[FRA_MAX+1];
Thomas Graf0947c9fe2007-03-26 17:14:15 -0700322 int err = -EINVAL, unresolved = 0;
Thomas Graf14c0b972006-08-04 03:38:38 -0700323
324 if (nlh->nlmsg_len < nlmsg_msg_size(sizeof(*frh)))
325 goto errout;
326
Denis V. Lunev5fd30ee2008-01-10 03:20:28 -0800327 ops = lookup_rules_ops(net, frh->family);
Thomas Graf14c0b972006-08-04 03:38:38 -0700328 if (ops == NULL) {
Patrick McHardy2fe195c2008-07-01 19:59:37 -0700329 err = -EAFNOSUPPORT;
Thomas Graf14c0b972006-08-04 03:38:38 -0700330 goto errout;
331 }
332
333 err = nlmsg_parse(nlh, sizeof(*frh), tb, FRA_MAX, ops->policy);
334 if (err < 0)
335 goto errout;
336
Thomas Grafe1701c62007-03-24 12:46:02 -0700337 err = validate_rulemsg(frh, tb, ops);
338 if (err < 0)
339 goto errout;
340
Thomas Graf14c0b972006-08-04 03:38:38 -0700341 rule = kzalloc(ops->rule_size, GFP_KERNEL);
342 if (rule == NULL) {
343 err = -ENOMEM;
344 goto errout;
345 }
Eric W. Biedermanefd7ef12015-03-11 23:04:08 -0500346 rule->fr_net = net;
Thomas Graf14c0b972006-08-04 03:38:38 -0700347
Phil Sutterf53de1e2015-09-09 14:20:56 +0200348 rule->pref = tb[FRA_PRIORITY] ? nla_get_u32(tb[FRA_PRIORITY])
349 : fib_default_rule_pref(ops);
Thomas Graf14c0b972006-08-04 03:38:38 -0700350
Patrick McHardy491deb24b2009-12-03 01:25:54 +0000351 if (tb[FRA_IIFNAME]) {
Thomas Graf14c0b972006-08-04 03:38:38 -0700352 struct net_device *dev;
353
Patrick McHardy491deb24b2009-12-03 01:25:54 +0000354 rule->iifindex = -1;
355 nla_strlcpy(rule->iifname, tb[FRA_IIFNAME], IFNAMSIZ);
356 dev = __dev_get_by_name(net, rule->iifname);
Thomas Graf14c0b972006-08-04 03:38:38 -0700357 if (dev)
Patrick McHardy491deb24b2009-12-03 01:25:54 +0000358 rule->iifindex = dev->ifindex;
Thomas Graf14c0b972006-08-04 03:38:38 -0700359 }
360
Patrick McHardy1b038a52009-12-03 01:25:56 +0000361 if (tb[FRA_OIFNAME]) {
362 struct net_device *dev;
363
364 rule->oifindex = -1;
365 nla_strlcpy(rule->oifname, tb[FRA_OIFNAME], IFNAMSIZ);
366 dev = __dev_get_by_name(net, rule->oifname);
367 if (dev)
368 rule->oifindex = dev->ifindex;
369 }
370
Thomas Grafb8964ed2006-11-09 15:22:18 -0800371 if (tb[FRA_FWMARK]) {
372 rule->mark = nla_get_u32(tb[FRA_FWMARK]);
373 if (rule->mark)
374 /* compatibility: if the mark value is non-zero all bits
375 * are compared unless a mask is explicitly specified.
376 */
377 rule->mark_mask = 0xFFFFFFFF;
378 }
379
380 if (tb[FRA_FWMASK])
381 rule->mark_mask = nla_get_u32(tb[FRA_FWMASK]);
382
Thomas Grafe7030872015-07-21 10:44:01 +0200383 if (tb[FRA_TUN_ID])
384 rule->tun_id = nla_get_be64(tb[FRA_TUN_ID]);
385
David Ahern96c63fa2016-06-08 10:55:39 -0700386 if (tb[FRA_L3MDEV]) {
387#ifdef CONFIG_NET_L3_MASTER_DEV
388 rule->l3mdev = nla_get_u8(tb[FRA_L3MDEV]);
389 if (rule->l3mdev != 1)
390#endif
391 goto errout_free;
392 }
393
Thomas Graf14c0b972006-08-04 03:38:38 -0700394 rule->action = frh->action;
395 rule->flags = frh->flags;
Patrick McHardy9e762a42006-08-10 23:09:48 -0700396 rule->table = frh_get_table(frh, tb);
Stefan Tomanek73f56982013-08-03 14:14:43 +0200397 if (tb[FRA_SUPPRESS_PREFIXLEN])
398 rule->suppress_prefixlen = nla_get_u32(tb[FRA_SUPPRESS_PREFIXLEN]);
399 else
400 rule->suppress_prefixlen = -1;
Thomas Graf14c0b972006-08-04 03:38:38 -0700401
Stefan Tomanek6ef94cf2013-08-02 17:19:56 +0200402 if (tb[FRA_SUPPRESS_IFGROUP])
403 rule->suppress_ifgroup = nla_get_u32(tb[FRA_SUPPRESS_IFGROUP]);
Stefan Tomanek73f56982013-08-03 14:14:43 +0200404 else
405 rule->suppress_ifgroup = -1;
Stefan Tomanek6ef94cf2013-08-02 17:19:56 +0200406
Thomas Graf0947c9fe2007-03-26 17:14:15 -0700407 err = -EINVAL;
408 if (tb[FRA_GOTO]) {
409 if (rule->action != FR_ACT_GOTO)
410 goto errout_free;
411
412 rule->target = nla_get_u32(tb[FRA_GOTO]);
413 /* Backward jumps are prohibited to avoid endless loops */
414 if (rule->target <= rule->pref)
415 goto errout_free;
416
Denis V. Lunev76c72d42007-09-16 15:44:27 -0700417 list_for_each_entry(r, &ops->rules_list, list) {
Thomas Graf0947c9fe2007-03-26 17:14:15 -0700418 if (r->pref == rule->target) {
Eric Dumazet7a2b03c2010-10-26 09:24:55 +0000419 RCU_INIT_POINTER(rule->ctarget, r);
Thomas Graf0947c9fe2007-03-26 17:14:15 -0700420 break;
421 }
422 }
423
Eric Dumazet7a2b03c2010-10-26 09:24:55 +0000424 if (rcu_dereference_protected(rule->ctarget, 1) == NULL)
Thomas Graf0947c9fe2007-03-26 17:14:15 -0700425 unresolved = 1;
426 } else if (rule->action == FR_ACT_GOTO)
427 goto errout_free;
428
David Ahern96c63fa2016-06-08 10:55:39 -0700429 if (rule->l3mdev && rule->table)
430 goto errout_free;
431
Hangbin Liu1fff19a2019-05-07 17:11:18 +0800432 if (rule_exists(ops, frh, tb, rule)) {
Greg Kroah-Hartmand5c71a72019-05-20 11:07:29 +0200433 err = 0;
Hangbin Liu1fff19a2019-05-07 17:11:18 +0800434 if (nlh->nlmsg_flags & NLM_F_EXCL)
435 err = -EEXIST;
Mateusz Bajorski153380e2016-06-29 09:22:10 +0200436 goto errout_free;
437 }
438
Rami Rosen8b3521e2009-05-11 05:52:49 +0000439 err = ops->configure(rule, skb, frh, tb);
Thomas Graf14c0b972006-08-04 03:38:38 -0700440 if (err < 0)
441 goto errout_free;
442
Denis V. Lunev76c72d42007-09-16 15:44:27 -0700443 list_for_each_entry(r, &ops->rules_list, list) {
Thomas Graf14c0b972006-08-04 03:38:38 -0700444 if (r->pref > rule->pref)
445 break;
446 last = r;
447 }
448
449 fib_rule_get(rule);
450
Eric Dumazetebb9fed2010-10-23 09:44:25 +0000451 if (last)
452 list_add_rcu(&rule->list, &last->list);
453 else
454 list_add_rcu(&rule->list, &ops->rules_list);
455
Thomas Graf0947c9fe2007-03-26 17:14:15 -0700456 if (ops->unresolved_rules) {
457 /*
458 * There are unresolved goto rules in the list, check if
459 * any of them are pointing to this new rule.
460 */
Denis V. Lunev76c72d42007-09-16 15:44:27 -0700461 list_for_each_entry(r, &ops->rules_list, list) {
Thomas Graf0947c9fe2007-03-26 17:14:15 -0700462 if (r->action == FR_ACT_GOTO &&
Gao feng561dac22011-09-11 15:36:05 +0000463 r->target == rule->pref &&
464 rtnl_dereference(r->ctarget) == NULL) {
Thomas Graf0947c9fe2007-03-26 17:14:15 -0700465 rcu_assign_pointer(r->ctarget, rule);
466 if (--ops->unresolved_rules == 0)
467 break;
468 }
469 }
470 }
471
472 if (rule->action == FR_ACT_GOTO)
473 ops->nr_goto_rules++;
474
475 if (unresolved)
476 ops->unresolved_rules++;
477
Thomas Grafe7030872015-07-21 10:44:01 +0200478 if (rule->tun_id)
479 ip_tunnel_need_metadata();
480
Eric W. Biederman15e47302012-09-07 20:12:54 +0000481 notify_rule_change(RTM_NEWRULE, rule, ops, nlh, NETLINK_CB(skb).portid);
Thomas Graf73417f62007-03-27 13:56:52 -0700482 flush_route_cache(ops);
Thomas Graf14c0b972006-08-04 03:38:38 -0700483 rules_ops_put(ops);
484 return 0;
485
486errout_free:
487 kfree(rule);
488errout:
489 rules_ops_put(ops);
490 return err;
491}
David Ahern96c63fa2016-06-08 10:55:39 -0700492EXPORT_SYMBOL_GPL(fib_nl_newrule);
Thomas Graf14c0b972006-08-04 03:38:38 -0700493
David Ahern96c63fa2016-06-08 10:55:39 -0700494int fib_nl_delrule(struct sk_buff *skb, struct nlmsghdr *nlh)
Thomas Graf14c0b972006-08-04 03:38:38 -0700495{
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +0900496 struct net *net = sock_net(skb->sk);
Thomas Graf14c0b972006-08-04 03:38:38 -0700497 struct fib_rule_hdr *frh = nlmsg_data(nlh);
498 struct fib_rules_ops *ops = NULL;
Thomas Graf0947c9fe2007-03-26 17:14:15 -0700499 struct fib_rule *rule, *tmp;
Thomas Graf14c0b972006-08-04 03:38:38 -0700500 struct nlattr *tb[FRA_MAX+1];
501 int err = -EINVAL;
502
503 if (nlh->nlmsg_len < nlmsg_msg_size(sizeof(*frh)))
504 goto errout;
505
Denis V. Lunev5fd30ee2008-01-10 03:20:28 -0800506 ops = lookup_rules_ops(net, frh->family);
Thomas Graf14c0b972006-08-04 03:38:38 -0700507 if (ops == NULL) {
Patrick McHardy2fe195c2008-07-01 19:59:37 -0700508 err = -EAFNOSUPPORT;
Thomas Graf14c0b972006-08-04 03:38:38 -0700509 goto errout;
510 }
511
512 err = nlmsg_parse(nlh, sizeof(*frh), tb, FRA_MAX, ops->policy);
513 if (err < 0)
514 goto errout;
515
Thomas Grafe1701c62007-03-24 12:46:02 -0700516 err = validate_rulemsg(frh, tb, ops);
517 if (err < 0)
518 goto errout;
519
Denis V. Lunev76c72d42007-09-16 15:44:27 -0700520 list_for_each_entry(rule, &ops->rules_list, list) {
Thomas Graf14c0b972006-08-04 03:38:38 -0700521 if (frh->action && (frh->action != rule->action))
522 continue;
523
Andreas Henriksson13eb2ab2013-11-07 18:26:38 +0100524 if (frh_get_table(frh, tb) &&
525 (frh_get_table(frh, tb) != rule->table))
Thomas Graf14c0b972006-08-04 03:38:38 -0700526 continue;
527
528 if (tb[FRA_PRIORITY] &&
529 (rule->pref != nla_get_u32(tb[FRA_PRIORITY])))
530 continue;
531
Patrick McHardy491deb24b2009-12-03 01:25:54 +0000532 if (tb[FRA_IIFNAME] &&
533 nla_strcmp(tb[FRA_IIFNAME], rule->iifname))
Thomas Graf14c0b972006-08-04 03:38:38 -0700534 continue;
535
Patrick McHardy1b038a52009-12-03 01:25:56 +0000536 if (tb[FRA_OIFNAME] &&
537 nla_strcmp(tb[FRA_OIFNAME], rule->oifname))
538 continue;
539
Thomas Grafb8964ed2006-11-09 15:22:18 -0800540 if (tb[FRA_FWMARK] &&
541 (rule->mark != nla_get_u32(tb[FRA_FWMARK])))
542 continue;
543
544 if (tb[FRA_FWMASK] &&
545 (rule->mark_mask != nla_get_u32(tb[FRA_FWMASK])))
546 continue;
547
Thomas Grafe7030872015-07-21 10:44:01 +0200548 if (tb[FRA_TUN_ID] &&
549 (rule->tun_id != nla_get_be64(tb[FRA_TUN_ID])))
550 continue;
551
David Ahern96c63fa2016-06-08 10:55:39 -0700552 if (tb[FRA_L3MDEV] &&
553 (rule->l3mdev != nla_get_u8(tb[FRA_L3MDEV])))
554 continue;
555
Thomas Graf14c0b972006-08-04 03:38:38 -0700556 if (!ops->compare(rule, frh, tb))
557 continue;
558
559 if (rule->flags & FIB_RULE_PERMANENT) {
560 err = -EPERM;
561 goto errout;
562 }
563
Alexander Duyck0ddcf432015-03-06 13:47:00 -0800564 if (ops->delete) {
565 err = ops->delete(rule);
566 if (err)
567 goto errout;
568 }
569
Thomas Grafe7030872015-07-21 10:44:01 +0200570 if (rule->tun_id)
571 ip_tunnel_unneed_metadata();
572
Thomas Graf14c0b972006-08-04 03:38:38 -0700573 list_del_rcu(&rule->list);
Thomas Graf0947c9fe2007-03-26 17:14:15 -0700574
Yan, Zhengafaef732011-10-17 15:20:28 +0000575 if (rule->action == FR_ACT_GOTO) {
Thomas Graf0947c9fe2007-03-26 17:14:15 -0700576 ops->nr_goto_rules--;
Yan, Zhengafaef732011-10-17 15:20:28 +0000577 if (rtnl_dereference(rule->ctarget) == NULL)
578 ops->unresolved_rules--;
579 }
Thomas Graf0947c9fe2007-03-26 17:14:15 -0700580
581 /*
582 * Check if this rule is a target to any of them. If so,
583 * disable them. As this operation is eventually very
584 * expensive, it is only performed if goto rules have
585 * actually been added.
586 */
587 if (ops->nr_goto_rules > 0) {
Denis V. Lunev76c72d42007-09-16 15:44:27 -0700588 list_for_each_entry(tmp, &ops->rules_list, list) {
Eric Dumazet7a2b03c2010-10-26 09:24:55 +0000589 if (rtnl_dereference(tmp->ctarget) == rule) {
Stephen Hemmingera9b3cd72011-08-01 16:19:00 +0000590 RCU_INIT_POINTER(tmp->ctarget, NULL);
Thomas Graf0947c9fe2007-03-26 17:14:15 -0700591 ops->unresolved_rules++;
592 }
593 }
594 }
595
Denis V. Lunev9e3a5482008-01-20 16:46:41 -0800596 notify_rule_change(RTM_DELRULE, rule, ops, nlh,
Eric W. Biederman15e47302012-09-07 20:12:54 +0000597 NETLINK_CB(skb).portid);
Thomas Graf14c0b972006-08-04 03:38:38 -0700598 fib_rule_put(rule);
Thomas Graf73417f62007-03-27 13:56:52 -0700599 flush_route_cache(ops);
Thomas Graf14c0b972006-08-04 03:38:38 -0700600 rules_ops_put(ops);
601 return 0;
602 }
603
604 err = -ENOENT;
605errout:
606 rules_ops_put(ops);
607 return err;
608}
David Ahern96c63fa2016-06-08 10:55:39 -0700609EXPORT_SYMBOL_GPL(fib_nl_delrule);
Thomas Graf14c0b972006-08-04 03:38:38 -0700610
Thomas Graf339bf982006-11-10 14:10:15 -0800611static inline size_t fib_rule_nlmsg_size(struct fib_rules_ops *ops,
612 struct fib_rule *rule)
613{
614 size_t payload = NLMSG_ALIGN(sizeof(struct fib_rule_hdr))
Patrick McHardy491deb24b2009-12-03 01:25:54 +0000615 + nla_total_size(IFNAMSIZ) /* FRA_IIFNAME */
Patrick McHardy1b038a52009-12-03 01:25:56 +0000616 + nla_total_size(IFNAMSIZ) /* FRA_OIFNAME */
Thomas Graf339bf982006-11-10 14:10:15 -0800617 + nla_total_size(4) /* FRA_PRIORITY */
618 + nla_total_size(4) /* FRA_TABLE */
Stefan Tomanek73f56982013-08-03 14:14:43 +0200619 + nla_total_size(4) /* FRA_SUPPRESS_PREFIXLEN */
Stefan Tomanek6ef94cf2013-08-02 17:19:56 +0200620 + nla_total_size(4) /* FRA_SUPPRESS_IFGROUP */
Thomas Graf339bf982006-11-10 14:10:15 -0800621 + nla_total_size(4) /* FRA_FWMARK */
Thomas Grafe7030872015-07-21 10:44:01 +0200622 + nla_total_size(4) /* FRA_FWMASK */
Nicolas Dichtelb46f6de2016-04-22 17:31:18 +0200623 + nla_total_size_64bit(8); /* FRA_TUN_ID */
Thomas Graf339bf982006-11-10 14:10:15 -0800624
625 if (ops->nlmsg_payload)
626 payload += ops->nlmsg_payload(rule);
627
628 return payload;
629}
630
Thomas Graf14c0b972006-08-04 03:38:38 -0700631static int fib_nl_fill_rule(struct sk_buff *skb, struct fib_rule *rule,
632 u32 pid, u32 seq, int type, int flags,
633 struct fib_rules_ops *ops)
634{
635 struct nlmsghdr *nlh;
636 struct fib_rule_hdr *frh;
637
638 nlh = nlmsg_put(skb, pid, seq, type, sizeof(*frh), flags);
639 if (nlh == NULL)
Patrick McHardy26932562007-01-31 23:16:40 -0800640 return -EMSGSIZE;
Thomas Graf14c0b972006-08-04 03:38:38 -0700641
642 frh = nlmsg_data(nlh);
Patrick McHardy28bb1722010-04-13 05:03:16 +0000643 frh->family = ops->family;
Thomas Graf14c0b972006-08-04 03:38:38 -0700644 frh->table = rule->table;
David S. Miller0e3cea72012-04-01 20:47:01 -0400645 if (nla_put_u32(skb, FRA_TABLE, rule->table))
646 goto nla_put_failure;
Stefan Tomanek73f56982013-08-03 14:14:43 +0200647 if (nla_put_u32(skb, FRA_SUPPRESS_PREFIXLEN, rule->suppress_prefixlen))
Stefan Tomanek7764a452013-08-01 02:17:15 +0200648 goto nla_put_failure;
Thomas Graf14c0b972006-08-04 03:38:38 -0700649 frh->res1 = 0;
650 frh->res2 = 0;
651 frh->action = rule->action;
652 frh->flags = rule->flags;
653
Eric Dumazet7a2b03c2010-10-26 09:24:55 +0000654 if (rule->action == FR_ACT_GOTO &&
Eric Dumazet33d480c2011-08-11 19:30:52 +0000655 rcu_access_pointer(rule->ctarget) == NULL)
Thomas Graf0947c9fe2007-03-26 17:14:15 -0700656 frh->flags |= FIB_RULE_UNRESOLVED;
657
Patrick McHardy491deb24b2009-12-03 01:25:54 +0000658 if (rule->iifname[0]) {
David S. Miller0e3cea72012-04-01 20:47:01 -0400659 if (nla_put_string(skb, FRA_IIFNAME, rule->iifname))
660 goto nla_put_failure;
Patrick McHardy491deb24b2009-12-03 01:25:54 +0000661 if (rule->iifindex == -1)
662 frh->flags |= FIB_RULE_IIF_DETACHED;
Thomas Graf2b443682007-03-26 17:37:59 -0700663 }
664
Patrick McHardy1b038a52009-12-03 01:25:56 +0000665 if (rule->oifname[0]) {
David S. Miller0e3cea72012-04-01 20:47:01 -0400666 if (nla_put_string(skb, FRA_OIFNAME, rule->oifname))
667 goto nla_put_failure;
Patrick McHardy1b038a52009-12-03 01:25:56 +0000668 if (rule->oifindex == -1)
669 frh->flags |= FIB_RULE_OIF_DETACHED;
670 }
671
David S. Miller0e3cea72012-04-01 20:47:01 -0400672 if ((rule->pref &&
673 nla_put_u32(skb, FRA_PRIORITY, rule->pref)) ||
674 (rule->mark &&
675 nla_put_u32(skb, FRA_FWMARK, rule->mark)) ||
676 ((rule->mark_mask || rule->mark) &&
677 nla_put_u32(skb, FRA_FWMASK, rule->mark_mask)) ||
678 (rule->target &&
Thomas Grafe7030872015-07-21 10:44:01 +0200679 nla_put_u32(skb, FRA_GOTO, rule->target)) ||
680 (rule->tun_id &&
David Ahern96c63fa2016-06-08 10:55:39 -0700681 nla_put_be64(skb, FRA_TUN_ID, rule->tun_id, FRA_PAD)) ||
682 (rule->l3mdev &&
683 nla_put_u8(skb, FRA_L3MDEV, rule->l3mdev)))
David S. Miller0e3cea72012-04-01 20:47:01 -0400684 goto nla_put_failure;
Stefan Tomanek6ef94cf2013-08-02 17:19:56 +0200685
686 if (rule->suppress_ifgroup != -1) {
687 if (nla_put_u32(skb, FRA_SUPPRESS_IFGROUP, rule->suppress_ifgroup))
688 goto nla_put_failure;
689 }
690
Rami Rosen04af8cf2009-05-20 17:26:23 -0700691 if (ops->fill(rule, skb, frh) < 0)
Thomas Graf14c0b972006-08-04 03:38:38 -0700692 goto nla_put_failure;
693
Johannes Berg053c0952015-01-16 22:09:00 +0100694 nlmsg_end(skb, nlh);
695 return 0;
Thomas Graf14c0b972006-08-04 03:38:38 -0700696
697nla_put_failure:
Patrick McHardy26932562007-01-31 23:16:40 -0800698 nlmsg_cancel(skb, nlh);
699 return -EMSGSIZE;
Thomas Graf14c0b972006-08-04 03:38:38 -0700700}
701
Thomas Grafc4546732007-03-25 23:24:24 -0700702static int dump_rules(struct sk_buff *skb, struct netlink_callback *cb,
703 struct fib_rules_ops *ops)
Thomas Graf14c0b972006-08-04 03:38:38 -0700704{
705 int idx = 0;
706 struct fib_rule *rule;
Wilson Kok41fc0142015-09-22 21:40:22 -0700707 int err = 0;
Thomas Graf14c0b972006-08-04 03:38:38 -0700708
Eric Dumazete67f88d2011-04-27 22:56:07 +0000709 rcu_read_lock();
710 list_for_each_entry_rcu(rule, &ops->rules_list, list) {
Thomas Grafc4546732007-03-25 23:24:24 -0700711 if (idx < cb->args[1])
Thomas Graf14c0b972006-08-04 03:38:38 -0700712 goto skip;
713
Wilson Kok41fc0142015-09-22 21:40:22 -0700714 err = fib_nl_fill_rule(skb, rule, NETLINK_CB(cb->skb).portid,
715 cb->nlh->nlmsg_seq, RTM_NEWRULE,
716 NLM_F_MULTI, ops);
717 if (err)
Thomas Graf14c0b972006-08-04 03:38:38 -0700718 break;
719skip:
720 idx++;
721 }
Eric Dumazet2907c352011-05-25 07:34:04 +0000722 rcu_read_unlock();
Thomas Grafc4546732007-03-25 23:24:24 -0700723 cb->args[1] = idx;
Thomas Graf14c0b972006-08-04 03:38:38 -0700724 rules_ops_put(ops);
725
Wilson Kok41fc0142015-09-22 21:40:22 -0700726 return err;
Thomas Graf14c0b972006-08-04 03:38:38 -0700727}
728
Thomas Grafc4546732007-03-25 23:24:24 -0700729static int fib_nl_dumprule(struct sk_buff *skb, struct netlink_callback *cb)
730{
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +0900731 struct net *net = sock_net(skb->sk);
Thomas Grafc4546732007-03-25 23:24:24 -0700732 struct fib_rules_ops *ops;
733 int idx = 0, family;
734
735 family = rtnl_msg_family(cb->nlh);
736 if (family != AF_UNSPEC) {
737 /* Protocol specific dump request */
Denis V. Lunev5fd30ee2008-01-10 03:20:28 -0800738 ops = lookup_rules_ops(net, family);
Thomas Grafc4546732007-03-25 23:24:24 -0700739 if (ops == NULL)
740 return -EAFNOSUPPORT;
741
Wilson Kok41fc0142015-09-22 21:40:22 -0700742 dump_rules(skb, cb, ops);
743
744 return skb->len;
Thomas Grafc4546732007-03-25 23:24:24 -0700745 }
746
747 rcu_read_lock();
Denis V. Lunev5fd30ee2008-01-10 03:20:28 -0800748 list_for_each_entry_rcu(ops, &net->rules_ops, list) {
Thomas Grafc4546732007-03-25 23:24:24 -0700749 if (idx < cb->args[0] || !try_module_get(ops->owner))
750 goto skip;
751
752 if (dump_rules(skb, cb, ops) < 0)
753 break;
754
755 cb->args[1] = 0;
Eric Dumazet2fb35732010-03-09 20:03:38 +0000756skip:
Thomas Grafc4546732007-03-25 23:24:24 -0700757 idx++;
758 }
759 rcu_read_unlock();
760 cb->args[0] = idx;
761
762 return skb->len;
763}
Thomas Graf14c0b972006-08-04 03:38:38 -0700764
Denis V. Lunev9e3a5482008-01-20 16:46:41 -0800765static void notify_rule_change(int event, struct fib_rule *rule,
Thomas Grafc17084d2006-08-15 00:32:48 -0700766 struct fib_rules_ops *ops, struct nlmsghdr *nlh,
767 u32 pid)
Thomas Graf14c0b972006-08-04 03:38:38 -0700768{
Denis V. Lunev9e3a5482008-01-20 16:46:41 -0800769 struct net *net;
Thomas Grafc17084d2006-08-15 00:32:48 -0700770 struct sk_buff *skb;
771 int err = -ENOBUFS;
Thomas Graf14c0b972006-08-04 03:38:38 -0700772
Denis V. Lunev9e3a5482008-01-20 16:46:41 -0800773 net = ops->fro_net;
Thomas Graf339bf982006-11-10 14:10:15 -0800774 skb = nlmsg_new(fib_rule_nlmsg_size(ops, rule), GFP_KERNEL);
Thomas Graf14c0b972006-08-04 03:38:38 -0700775 if (skb == NULL)
Thomas Grafc17084d2006-08-15 00:32:48 -0700776 goto errout;
777
778 err = fib_nl_fill_rule(skb, rule, pid, nlh->nlmsg_seq, event, 0, ops);
Patrick McHardy26932562007-01-31 23:16:40 -0800779 if (err < 0) {
780 /* -EMSGSIZE implies BUG in fib_rule_nlmsg_size() */
781 WARN_ON(err == -EMSGSIZE);
782 kfree_skb(skb);
783 goto errout;
784 }
Denis V. Lunev9e3a5482008-01-20 16:46:41 -0800785
Pablo Neira Ayuso1ce85fe2009-02-24 23:18:28 -0800786 rtnl_notify(skb, net, pid, ops->nlgroup, nlh, GFP_KERNEL);
787 return;
Thomas Grafc17084d2006-08-15 00:32:48 -0700788errout:
789 if (err < 0)
Denis V. Lunev5fd30ee2008-01-10 03:20:28 -0800790 rtnl_set_sk_err(net, ops->nlgroup, err);
Thomas Graf14c0b972006-08-04 03:38:38 -0700791}
792
793static void attach_rules(struct list_head *rules, struct net_device *dev)
794{
795 struct fib_rule *rule;
796
797 list_for_each_entry(rule, rules, list) {
Patrick McHardy491deb24b2009-12-03 01:25:54 +0000798 if (rule->iifindex == -1 &&
799 strcmp(dev->name, rule->iifname) == 0)
800 rule->iifindex = dev->ifindex;
Patrick McHardy1b038a52009-12-03 01:25:56 +0000801 if (rule->oifindex == -1 &&
802 strcmp(dev->name, rule->oifname) == 0)
803 rule->oifindex = dev->ifindex;
Thomas Graf14c0b972006-08-04 03:38:38 -0700804 }
805}
806
807static void detach_rules(struct list_head *rules, struct net_device *dev)
808{
809 struct fib_rule *rule;
810
Patrick McHardy1b038a52009-12-03 01:25:56 +0000811 list_for_each_entry(rule, rules, list) {
Patrick McHardy491deb24b2009-12-03 01:25:54 +0000812 if (rule->iifindex == dev->ifindex)
813 rule->iifindex = -1;
Patrick McHardy1b038a52009-12-03 01:25:56 +0000814 if (rule->oifindex == dev->ifindex)
815 rule->oifindex = -1;
816 }
Thomas Graf14c0b972006-08-04 03:38:38 -0700817}
818
819
820static int fib_rules_event(struct notifier_block *this, unsigned long event,
Jiri Pirko351638e2013-05-28 01:30:21 +0000821 void *ptr)
Thomas Graf14c0b972006-08-04 03:38:38 -0700822{
Jiri Pirko351638e2013-05-28 01:30:21 +0000823 struct net_device *dev = netdev_notifier_info_to_dev(ptr);
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +0900824 struct net *net = dev_net(dev);
Thomas Graf14c0b972006-08-04 03:38:38 -0700825 struct fib_rules_ops *ops;
826
Eric Dumazet748e2d92012-08-22 21:50:59 +0000827 ASSERT_RTNL();
Thomas Graf14c0b972006-08-04 03:38:38 -0700828
829 switch (event) {
830 case NETDEV_REGISTER:
Denis V. Lunev5fd30ee2008-01-10 03:20:28 -0800831 list_for_each_entry(ops, &net->rules_ops, list)
Denis V. Lunev76c72d42007-09-16 15:44:27 -0700832 attach_rules(&ops->rules_list, dev);
Thomas Graf14c0b972006-08-04 03:38:38 -0700833 break;
834
Maciej Żenczykowski946c0322014-02-07 16:23:48 -0800835 case NETDEV_CHANGENAME:
836 list_for_each_entry(ops, &net->rules_ops, list) {
837 detach_rules(&ops->rules_list, dev);
838 attach_rules(&ops->rules_list, dev);
839 }
840 break;
841
Thomas Graf14c0b972006-08-04 03:38:38 -0700842 case NETDEV_UNREGISTER:
Denis V. Lunev5fd30ee2008-01-10 03:20:28 -0800843 list_for_each_entry(ops, &net->rules_ops, list)
Denis V. Lunev76c72d42007-09-16 15:44:27 -0700844 detach_rules(&ops->rules_list, dev);
Thomas Graf14c0b972006-08-04 03:38:38 -0700845 break;
846 }
847
Thomas Graf14c0b972006-08-04 03:38:38 -0700848 return NOTIFY_DONE;
849}
850
851static struct notifier_block fib_rules_notifier = {
852 .notifier_call = fib_rules_event,
853};
854
Alexey Dobriyan2c8c1e72010-01-17 03:35:32 +0000855static int __net_init fib_rules_net_init(struct net *net)
Denis V. Lunev5fd30ee2008-01-10 03:20:28 -0800856{
857 INIT_LIST_HEAD(&net->rules_ops);
858 spin_lock_init(&net->rules_mod_lock);
859 return 0;
860}
861
862static struct pernet_operations fib_rules_net_ops = {
863 .init = fib_rules_net_init,
864};
865
Thomas Graf14c0b972006-08-04 03:38:38 -0700866static int __init fib_rules_init(void)
867{
Denis V. Lunev5fd30ee2008-01-10 03:20:28 -0800868 int err;
Greg Rosec7ac8672011-06-10 01:27:09 +0000869 rtnl_register(PF_UNSPEC, RTM_NEWRULE, fib_nl_newrule, NULL, NULL);
870 rtnl_register(PF_UNSPEC, RTM_DELRULE, fib_nl_delrule, NULL, NULL);
871 rtnl_register(PF_UNSPEC, RTM_GETRULE, NULL, fib_nl_dumprule, NULL);
Thomas Graf9d9e6a52007-03-25 23:20:05 -0700872
Eric W. Biederman5d6d4802008-11-07 22:52:34 -0800873 err = register_pernet_subsys(&fib_rules_net_ops);
Denis V. Lunev5fd30ee2008-01-10 03:20:28 -0800874 if (err < 0)
875 goto fail;
876
Eric W. Biederman5d6d4802008-11-07 22:52:34 -0800877 err = register_netdevice_notifier(&fib_rules_notifier);
Denis V. Lunev5fd30ee2008-01-10 03:20:28 -0800878 if (err < 0)
879 goto fail_unregister;
Eric W. Biederman5d6d4802008-11-07 22:52:34 -0800880
Denis V. Lunev5fd30ee2008-01-10 03:20:28 -0800881 return 0;
882
883fail_unregister:
Eric W. Biederman5d6d4802008-11-07 22:52:34 -0800884 unregister_pernet_subsys(&fib_rules_net_ops);
Denis V. Lunev5fd30ee2008-01-10 03:20:28 -0800885fail:
886 rtnl_unregister(PF_UNSPEC, RTM_NEWRULE);
887 rtnl_unregister(PF_UNSPEC, RTM_DELRULE);
888 rtnl_unregister(PF_UNSPEC, RTM_GETRULE);
889 return err;
Thomas Graf14c0b972006-08-04 03:38:38 -0700890}
891
892subsys_initcall(fib_rules_init);