blob: 365de66436aca8dba3868aa565d4cb77353b58d1 [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,
176 struct flowi *fl, int flags)
177{
178 int ret = 0;
179
David S. Miller1d28f422011-03-12 00:29:39 -0500180 if (rule->iifindex && (rule->iifindex != fl->flowi_iif))
Thomas Graf3dfbcc42006-11-09 15:23:20 -0800181 goto out;
182
David S. Miller1d28f422011-03-12 00:29:39 -0500183 if (rule->oifindex && (rule->oifindex != fl->flowi_oif))
Patrick McHardy1b038a52009-12-03 01:25:56 +0000184 goto out;
185
David S. Miller1d28f422011-03-12 00:29:39 -0500186 if ((rule->mark ^ fl->flowi_mark) & rule->mark_mask)
Thomas Graf3dfbcc42006-11-09 15:23:20 -0800187 goto out;
188
Thomas Grafe7030872015-07-21 10:44:01 +0200189 if (rule->tun_id && (rule->tun_id != fl->flowi_tun_key.tun_id))
190 goto out;
191
Thomas Graf3dfbcc42006-11-09 15:23:20 -0800192 ret = ops->match(rule, fl, flags);
193out:
194 return (rule->flags & FIB_RULE_INVERT) ? !ret : ret;
195}
196
Thomas Graf14c0b972006-08-04 03:38:38 -0700197int fib_rules_lookup(struct fib_rules_ops *ops, struct flowi *fl,
198 int flags, struct fib_lookup_arg *arg)
199{
200 struct fib_rule *rule;
201 int err;
202
203 rcu_read_lock();
204
Denis V. Lunev76c72d42007-09-16 15:44:27 -0700205 list_for_each_entry_rcu(rule, &ops->rules_list, list) {
Thomas Graf0947c9fe2007-03-26 17:14:15 -0700206jumped:
Thomas Graf3dfbcc42006-11-09 15:23:20 -0800207 if (!fib_rule_match(rule, ops, fl, flags))
Thomas Graf14c0b972006-08-04 03:38:38 -0700208 continue;
209
Thomas Graf0947c9fe2007-03-26 17:14:15 -0700210 if (rule->action == FR_ACT_GOTO) {
211 struct fib_rule *target;
212
213 target = rcu_dereference(rule->ctarget);
214 if (target == NULL) {
215 continue;
216 } else {
217 rule = target;
218 goto jumped;
219 }
Thomas Graffa0b2d12007-03-26 17:38:53 -0700220 } else if (rule->action == FR_ACT_NOP)
221 continue;
222 else
Thomas Graf0947c9fe2007-03-26 17:14:15 -0700223 err = ops->action(rule, fl, flags, arg);
224
Stefan Tomanek7764a452013-08-01 02:17:15 +0200225 if (!err && ops->suppress && ops->suppress(rule, arg))
226 continue;
227
Thomas Graf14c0b972006-08-04 03:38:38 -0700228 if (err != -EAGAIN) {
Eric Dumazetebc0ffa2010-10-05 10:41:36 +0000229 if ((arg->flags & FIB_LOOKUP_NOREF) ||
230 likely(atomic_inc_not_zero(&rule->refcnt))) {
Eric Dumazet7fa7cb72010-09-27 04:18:27 +0000231 arg->rule = rule;
232 goto out;
233 }
234 break;
Thomas Graf14c0b972006-08-04 03:38:38 -0700235 }
236 }
237
Steven Whitehouse83886b62007-03-30 13:34:27 -0700238 err = -ESRCH;
Thomas Graf14c0b972006-08-04 03:38:38 -0700239out:
240 rcu_read_unlock();
241
242 return err;
243}
Thomas Graf14c0b972006-08-04 03:38:38 -0700244EXPORT_SYMBOL_GPL(fib_rules_lookup);
245
Thomas Grafe1701c62007-03-24 12:46:02 -0700246static int validate_rulemsg(struct fib_rule_hdr *frh, struct nlattr **tb,
247 struct fib_rules_ops *ops)
248{
249 int err = -EINVAL;
250
251 if (frh->src_len)
252 if (tb[FRA_SRC] == NULL ||
253 frh->src_len > (ops->addr_size * 8) ||
254 nla_len(tb[FRA_SRC]) != ops->addr_size)
255 goto errout;
256
257 if (frh->dst_len)
258 if (tb[FRA_DST] == NULL ||
259 frh->dst_len > (ops->addr_size * 8) ||
260 nla_len(tb[FRA_DST]) != ops->addr_size)
261 goto errout;
262
263 err = 0;
264errout:
265 return err;
266}
267
Thomas Graf661d2962013-03-21 07:45:29 +0000268static int fib_nl_newrule(struct sk_buff *skb, struct nlmsghdr* nlh)
Thomas Graf14c0b972006-08-04 03:38:38 -0700269{
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +0900270 struct net *net = sock_net(skb->sk);
Thomas Graf14c0b972006-08-04 03:38:38 -0700271 struct fib_rule_hdr *frh = nlmsg_data(nlh);
272 struct fib_rules_ops *ops = NULL;
273 struct fib_rule *rule, *r, *last = NULL;
274 struct nlattr *tb[FRA_MAX+1];
Thomas Graf0947c9fe2007-03-26 17:14:15 -0700275 int err = -EINVAL, unresolved = 0;
Thomas Graf14c0b972006-08-04 03:38:38 -0700276
277 if (nlh->nlmsg_len < nlmsg_msg_size(sizeof(*frh)))
278 goto errout;
279
Denis V. Lunev5fd30ee2008-01-10 03:20:28 -0800280 ops = lookup_rules_ops(net, frh->family);
Thomas Graf14c0b972006-08-04 03:38:38 -0700281 if (ops == NULL) {
Patrick McHardy2fe195c2008-07-01 19:59:37 -0700282 err = -EAFNOSUPPORT;
Thomas Graf14c0b972006-08-04 03:38:38 -0700283 goto errout;
284 }
285
286 err = nlmsg_parse(nlh, sizeof(*frh), tb, FRA_MAX, ops->policy);
287 if (err < 0)
288 goto errout;
289
Thomas Grafe1701c62007-03-24 12:46:02 -0700290 err = validate_rulemsg(frh, tb, ops);
291 if (err < 0)
292 goto errout;
293
Thomas Graf14c0b972006-08-04 03:38:38 -0700294 rule = kzalloc(ops->rule_size, GFP_KERNEL);
295 if (rule == NULL) {
296 err = -ENOMEM;
297 goto errout;
298 }
Eric W. Biedermanefd7ef12015-03-11 23:04:08 -0500299 rule->fr_net = net;
Thomas Graf14c0b972006-08-04 03:38:38 -0700300
Phil Sutterf53de1e2015-09-09 14:20:56 +0200301 rule->pref = tb[FRA_PRIORITY] ? nla_get_u32(tb[FRA_PRIORITY])
302 : fib_default_rule_pref(ops);
Thomas Graf14c0b972006-08-04 03:38:38 -0700303
Patrick McHardy491deb22009-12-03 01:25:54 +0000304 if (tb[FRA_IIFNAME]) {
Thomas Graf14c0b972006-08-04 03:38:38 -0700305 struct net_device *dev;
306
Patrick McHardy491deb22009-12-03 01:25:54 +0000307 rule->iifindex = -1;
308 nla_strlcpy(rule->iifname, tb[FRA_IIFNAME], IFNAMSIZ);
309 dev = __dev_get_by_name(net, rule->iifname);
Thomas Graf14c0b972006-08-04 03:38:38 -0700310 if (dev)
Patrick McHardy491deb22009-12-03 01:25:54 +0000311 rule->iifindex = dev->ifindex;
Thomas Graf14c0b972006-08-04 03:38:38 -0700312 }
313
Patrick McHardy1b038a52009-12-03 01:25:56 +0000314 if (tb[FRA_OIFNAME]) {
315 struct net_device *dev;
316
317 rule->oifindex = -1;
318 nla_strlcpy(rule->oifname, tb[FRA_OIFNAME], IFNAMSIZ);
319 dev = __dev_get_by_name(net, rule->oifname);
320 if (dev)
321 rule->oifindex = dev->ifindex;
322 }
323
Thomas Grafb8964ed2006-11-09 15:22:18 -0800324 if (tb[FRA_FWMARK]) {
325 rule->mark = nla_get_u32(tb[FRA_FWMARK]);
326 if (rule->mark)
327 /* compatibility: if the mark value is non-zero all bits
328 * are compared unless a mask is explicitly specified.
329 */
330 rule->mark_mask = 0xFFFFFFFF;
331 }
332
333 if (tb[FRA_FWMASK])
334 rule->mark_mask = nla_get_u32(tb[FRA_FWMASK]);
335
Thomas Grafe7030872015-07-21 10:44:01 +0200336 if (tb[FRA_TUN_ID])
337 rule->tun_id = nla_get_be64(tb[FRA_TUN_ID]);
338
Thomas Graf14c0b972006-08-04 03:38:38 -0700339 rule->action = frh->action;
340 rule->flags = frh->flags;
Patrick McHardy9e762a42006-08-10 23:09:48 -0700341 rule->table = frh_get_table(frh, tb);
Stefan Tomanek73f56982013-08-03 14:14:43 +0200342 if (tb[FRA_SUPPRESS_PREFIXLEN])
343 rule->suppress_prefixlen = nla_get_u32(tb[FRA_SUPPRESS_PREFIXLEN]);
344 else
345 rule->suppress_prefixlen = -1;
Thomas Graf14c0b972006-08-04 03:38:38 -0700346
Stefan Tomanek6ef94cf2013-08-02 17:19:56 +0200347 if (tb[FRA_SUPPRESS_IFGROUP])
348 rule->suppress_ifgroup = nla_get_u32(tb[FRA_SUPPRESS_IFGROUP]);
Stefan Tomanek73f56982013-08-03 14:14:43 +0200349 else
350 rule->suppress_ifgroup = -1;
Stefan Tomanek6ef94cf2013-08-02 17:19:56 +0200351
Thomas Graf0947c9fe2007-03-26 17:14:15 -0700352 err = -EINVAL;
353 if (tb[FRA_GOTO]) {
354 if (rule->action != FR_ACT_GOTO)
355 goto errout_free;
356
357 rule->target = nla_get_u32(tb[FRA_GOTO]);
358 /* Backward jumps are prohibited to avoid endless loops */
359 if (rule->target <= rule->pref)
360 goto errout_free;
361
Denis V. Lunev76c72d42007-09-16 15:44:27 -0700362 list_for_each_entry(r, &ops->rules_list, list) {
Thomas Graf0947c9fe2007-03-26 17:14:15 -0700363 if (r->pref == rule->target) {
Eric Dumazet7a2b03c2010-10-26 09:24:55 +0000364 RCU_INIT_POINTER(rule->ctarget, r);
Thomas Graf0947c9fe2007-03-26 17:14:15 -0700365 break;
366 }
367 }
368
Eric Dumazet7a2b03c2010-10-26 09:24:55 +0000369 if (rcu_dereference_protected(rule->ctarget, 1) == NULL)
Thomas Graf0947c9fe2007-03-26 17:14:15 -0700370 unresolved = 1;
371 } else if (rule->action == FR_ACT_GOTO)
372 goto errout_free;
373
Rami Rosen8b3521e2009-05-11 05:52:49 +0000374 err = ops->configure(rule, skb, frh, tb);
Thomas Graf14c0b972006-08-04 03:38:38 -0700375 if (err < 0)
376 goto errout_free;
377
Denis V. Lunev76c72d42007-09-16 15:44:27 -0700378 list_for_each_entry(r, &ops->rules_list, list) {
Thomas Graf14c0b972006-08-04 03:38:38 -0700379 if (r->pref > rule->pref)
380 break;
381 last = r;
382 }
383
384 fib_rule_get(rule);
385
Eric Dumazetebb9fed2010-10-23 09:44:25 +0000386 if (last)
387 list_add_rcu(&rule->list, &last->list);
388 else
389 list_add_rcu(&rule->list, &ops->rules_list);
390
Thomas Graf0947c9fe2007-03-26 17:14:15 -0700391 if (ops->unresolved_rules) {
392 /*
393 * There are unresolved goto rules in the list, check if
394 * any of them are pointing to this new rule.
395 */
Denis V. Lunev76c72d42007-09-16 15:44:27 -0700396 list_for_each_entry(r, &ops->rules_list, list) {
Thomas Graf0947c9fe2007-03-26 17:14:15 -0700397 if (r->action == FR_ACT_GOTO &&
Gao feng561dac22011-09-11 15:36:05 +0000398 r->target == rule->pref &&
399 rtnl_dereference(r->ctarget) == NULL) {
Thomas Graf0947c9fe2007-03-26 17:14:15 -0700400 rcu_assign_pointer(r->ctarget, rule);
401 if (--ops->unresolved_rules == 0)
402 break;
403 }
404 }
405 }
406
407 if (rule->action == FR_ACT_GOTO)
408 ops->nr_goto_rules++;
409
410 if (unresolved)
411 ops->unresolved_rules++;
412
Thomas Grafe7030872015-07-21 10:44:01 +0200413 if (rule->tun_id)
414 ip_tunnel_need_metadata();
415
Eric W. Biederman15e47302012-09-07 20:12:54 +0000416 notify_rule_change(RTM_NEWRULE, rule, ops, nlh, NETLINK_CB(skb).portid);
Thomas Graf73417f62007-03-27 13:56:52 -0700417 flush_route_cache(ops);
Thomas Graf14c0b972006-08-04 03:38:38 -0700418 rules_ops_put(ops);
419 return 0;
420
421errout_free:
422 kfree(rule);
423errout:
424 rules_ops_put(ops);
425 return err;
426}
427
Thomas Graf661d2962013-03-21 07:45:29 +0000428static int fib_nl_delrule(struct sk_buff *skb, struct nlmsghdr* nlh)
Thomas Graf14c0b972006-08-04 03:38:38 -0700429{
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +0900430 struct net *net = sock_net(skb->sk);
Thomas Graf14c0b972006-08-04 03:38:38 -0700431 struct fib_rule_hdr *frh = nlmsg_data(nlh);
432 struct fib_rules_ops *ops = NULL;
Thomas Graf0947c9fe2007-03-26 17:14:15 -0700433 struct fib_rule *rule, *tmp;
Thomas Graf14c0b972006-08-04 03:38:38 -0700434 struct nlattr *tb[FRA_MAX+1];
435 int err = -EINVAL;
436
437 if (nlh->nlmsg_len < nlmsg_msg_size(sizeof(*frh)))
438 goto errout;
439
Denis V. Lunev5fd30ee2008-01-10 03:20:28 -0800440 ops = lookup_rules_ops(net, frh->family);
Thomas Graf14c0b972006-08-04 03:38:38 -0700441 if (ops == NULL) {
Patrick McHardy2fe195c2008-07-01 19:59:37 -0700442 err = -EAFNOSUPPORT;
Thomas Graf14c0b972006-08-04 03:38:38 -0700443 goto errout;
444 }
445
446 err = nlmsg_parse(nlh, sizeof(*frh), tb, FRA_MAX, ops->policy);
447 if (err < 0)
448 goto errout;
449
Thomas Grafe1701c62007-03-24 12:46:02 -0700450 err = validate_rulemsg(frh, tb, ops);
451 if (err < 0)
452 goto errout;
453
Denis V. Lunev76c72d42007-09-16 15:44:27 -0700454 list_for_each_entry(rule, &ops->rules_list, list) {
Thomas Graf14c0b972006-08-04 03:38:38 -0700455 if (frh->action && (frh->action != rule->action))
456 continue;
457
Andreas Henriksson13eb2ab2013-11-07 18:26:38 +0100458 if (frh_get_table(frh, tb) &&
459 (frh_get_table(frh, tb) != rule->table))
Thomas Graf14c0b972006-08-04 03:38:38 -0700460 continue;
461
462 if (tb[FRA_PRIORITY] &&
463 (rule->pref != nla_get_u32(tb[FRA_PRIORITY])))
464 continue;
465
Patrick McHardy491deb22009-12-03 01:25:54 +0000466 if (tb[FRA_IIFNAME] &&
467 nla_strcmp(tb[FRA_IIFNAME], rule->iifname))
Thomas Graf14c0b972006-08-04 03:38:38 -0700468 continue;
469
Patrick McHardy1b038a52009-12-03 01:25:56 +0000470 if (tb[FRA_OIFNAME] &&
471 nla_strcmp(tb[FRA_OIFNAME], rule->oifname))
472 continue;
473
Thomas Grafb8964ed2006-11-09 15:22:18 -0800474 if (tb[FRA_FWMARK] &&
475 (rule->mark != nla_get_u32(tb[FRA_FWMARK])))
476 continue;
477
478 if (tb[FRA_FWMASK] &&
479 (rule->mark_mask != nla_get_u32(tb[FRA_FWMASK])))
480 continue;
481
Thomas Grafe7030872015-07-21 10:44:01 +0200482 if (tb[FRA_TUN_ID] &&
483 (rule->tun_id != nla_get_be64(tb[FRA_TUN_ID])))
484 continue;
485
Thomas Graf14c0b972006-08-04 03:38:38 -0700486 if (!ops->compare(rule, frh, tb))
487 continue;
488
489 if (rule->flags & FIB_RULE_PERMANENT) {
490 err = -EPERM;
491 goto errout;
492 }
493
Alexander Duyck0ddcf432015-03-06 13:47:00 -0800494 if (ops->delete) {
495 err = ops->delete(rule);
496 if (err)
497 goto errout;
498 }
499
Thomas Grafe7030872015-07-21 10:44:01 +0200500 if (rule->tun_id)
501 ip_tunnel_unneed_metadata();
502
Thomas Graf14c0b972006-08-04 03:38:38 -0700503 list_del_rcu(&rule->list);
Thomas Graf0947c9fe2007-03-26 17:14:15 -0700504
Yan, Zhengafaef732011-10-17 15:20:28 +0000505 if (rule->action == FR_ACT_GOTO) {
Thomas Graf0947c9fe2007-03-26 17:14:15 -0700506 ops->nr_goto_rules--;
Yan, Zhengafaef732011-10-17 15:20:28 +0000507 if (rtnl_dereference(rule->ctarget) == NULL)
508 ops->unresolved_rules--;
509 }
Thomas Graf0947c9fe2007-03-26 17:14:15 -0700510
511 /*
512 * Check if this rule is a target to any of them. If so,
513 * disable them. As this operation is eventually very
514 * expensive, it is only performed if goto rules have
515 * actually been added.
516 */
517 if (ops->nr_goto_rules > 0) {
Denis V. Lunev76c72d42007-09-16 15:44:27 -0700518 list_for_each_entry(tmp, &ops->rules_list, list) {
Eric Dumazet7a2b03c2010-10-26 09:24:55 +0000519 if (rtnl_dereference(tmp->ctarget) == rule) {
Stephen Hemmingera9b3cd72011-08-01 16:19:00 +0000520 RCU_INIT_POINTER(tmp->ctarget, NULL);
Thomas Graf0947c9fe2007-03-26 17:14:15 -0700521 ops->unresolved_rules++;
522 }
523 }
524 }
525
Denis V. Lunev9e3a5482008-01-20 16:46:41 -0800526 notify_rule_change(RTM_DELRULE, rule, ops, nlh,
Eric W. Biederman15e47302012-09-07 20:12:54 +0000527 NETLINK_CB(skb).portid);
Thomas Graf14c0b972006-08-04 03:38:38 -0700528 fib_rule_put(rule);
Thomas Graf73417f62007-03-27 13:56:52 -0700529 flush_route_cache(ops);
Thomas Graf14c0b972006-08-04 03:38:38 -0700530 rules_ops_put(ops);
531 return 0;
532 }
533
534 err = -ENOENT;
535errout:
536 rules_ops_put(ops);
537 return err;
538}
539
Thomas Graf339bf982006-11-10 14:10:15 -0800540static inline size_t fib_rule_nlmsg_size(struct fib_rules_ops *ops,
541 struct fib_rule *rule)
542{
543 size_t payload = NLMSG_ALIGN(sizeof(struct fib_rule_hdr))
Patrick McHardy491deb22009-12-03 01:25:54 +0000544 + nla_total_size(IFNAMSIZ) /* FRA_IIFNAME */
Patrick McHardy1b038a52009-12-03 01:25:56 +0000545 + nla_total_size(IFNAMSIZ) /* FRA_OIFNAME */
Thomas Graf339bf982006-11-10 14:10:15 -0800546 + nla_total_size(4) /* FRA_PRIORITY */
547 + nla_total_size(4) /* FRA_TABLE */
Stefan Tomanek73f56982013-08-03 14:14:43 +0200548 + nla_total_size(4) /* FRA_SUPPRESS_PREFIXLEN */
Stefan Tomanek6ef94cf2013-08-02 17:19:56 +0200549 + nla_total_size(4) /* FRA_SUPPRESS_IFGROUP */
Thomas Graf339bf982006-11-10 14:10:15 -0800550 + nla_total_size(4) /* FRA_FWMARK */
Thomas Grafe7030872015-07-21 10:44:01 +0200551 + nla_total_size(4) /* FRA_FWMASK */
552 + nla_total_size(8); /* FRA_TUN_ID */
Thomas Graf339bf982006-11-10 14:10:15 -0800553
554 if (ops->nlmsg_payload)
555 payload += ops->nlmsg_payload(rule);
556
557 return payload;
558}
559
Thomas Graf14c0b972006-08-04 03:38:38 -0700560static int fib_nl_fill_rule(struct sk_buff *skb, struct fib_rule *rule,
561 u32 pid, u32 seq, int type, int flags,
562 struct fib_rules_ops *ops)
563{
564 struct nlmsghdr *nlh;
565 struct fib_rule_hdr *frh;
566
567 nlh = nlmsg_put(skb, pid, seq, type, sizeof(*frh), flags);
568 if (nlh == NULL)
Patrick McHardy26932562007-01-31 23:16:40 -0800569 return -EMSGSIZE;
Thomas Graf14c0b972006-08-04 03:38:38 -0700570
571 frh = nlmsg_data(nlh);
Patrick McHardy28bb1722010-04-13 05:03:16 +0000572 frh->family = ops->family;
Thomas Graf14c0b972006-08-04 03:38:38 -0700573 frh->table = rule->table;
David S. Miller0e3cea72012-04-01 20:47:01 -0400574 if (nla_put_u32(skb, FRA_TABLE, rule->table))
575 goto nla_put_failure;
Stefan Tomanek73f56982013-08-03 14:14:43 +0200576 if (nla_put_u32(skb, FRA_SUPPRESS_PREFIXLEN, rule->suppress_prefixlen))
Stefan Tomanek7764a452013-08-01 02:17:15 +0200577 goto nla_put_failure;
Thomas Graf14c0b972006-08-04 03:38:38 -0700578 frh->res1 = 0;
579 frh->res2 = 0;
580 frh->action = rule->action;
581 frh->flags = rule->flags;
582
Eric Dumazet7a2b03c2010-10-26 09:24:55 +0000583 if (rule->action == FR_ACT_GOTO &&
Eric Dumazet33d480c2011-08-11 19:30:52 +0000584 rcu_access_pointer(rule->ctarget) == NULL)
Thomas Graf0947c9fe2007-03-26 17:14:15 -0700585 frh->flags |= FIB_RULE_UNRESOLVED;
586
Patrick McHardy491deb22009-12-03 01:25:54 +0000587 if (rule->iifname[0]) {
David S. Miller0e3cea72012-04-01 20:47:01 -0400588 if (nla_put_string(skb, FRA_IIFNAME, rule->iifname))
589 goto nla_put_failure;
Patrick McHardy491deb22009-12-03 01:25:54 +0000590 if (rule->iifindex == -1)
591 frh->flags |= FIB_RULE_IIF_DETACHED;
Thomas Graf2b443682007-03-26 17:37:59 -0700592 }
593
Patrick McHardy1b038a52009-12-03 01:25:56 +0000594 if (rule->oifname[0]) {
David S. Miller0e3cea72012-04-01 20:47:01 -0400595 if (nla_put_string(skb, FRA_OIFNAME, rule->oifname))
596 goto nla_put_failure;
Patrick McHardy1b038a52009-12-03 01:25:56 +0000597 if (rule->oifindex == -1)
598 frh->flags |= FIB_RULE_OIF_DETACHED;
599 }
600
David S. Miller0e3cea72012-04-01 20:47:01 -0400601 if ((rule->pref &&
602 nla_put_u32(skb, FRA_PRIORITY, rule->pref)) ||
603 (rule->mark &&
604 nla_put_u32(skb, FRA_FWMARK, rule->mark)) ||
605 ((rule->mark_mask || rule->mark) &&
606 nla_put_u32(skb, FRA_FWMASK, rule->mark_mask)) ||
607 (rule->target &&
Thomas Grafe7030872015-07-21 10:44:01 +0200608 nla_put_u32(skb, FRA_GOTO, rule->target)) ||
609 (rule->tun_id &&
610 nla_put_be64(skb, FRA_TUN_ID, rule->tun_id)))
David S. Miller0e3cea72012-04-01 20:47:01 -0400611 goto nla_put_failure;
Stefan Tomanek6ef94cf2013-08-02 17:19:56 +0200612
613 if (rule->suppress_ifgroup != -1) {
614 if (nla_put_u32(skb, FRA_SUPPRESS_IFGROUP, rule->suppress_ifgroup))
615 goto nla_put_failure;
616 }
617
Rami Rosen04af8cf2009-05-20 17:26:23 -0700618 if (ops->fill(rule, skb, frh) < 0)
Thomas Graf14c0b972006-08-04 03:38:38 -0700619 goto nla_put_failure;
620
Johannes Berg053c0952015-01-16 22:09:00 +0100621 nlmsg_end(skb, nlh);
622 return 0;
Thomas Graf14c0b972006-08-04 03:38:38 -0700623
624nla_put_failure:
Patrick McHardy26932562007-01-31 23:16:40 -0800625 nlmsg_cancel(skb, nlh);
626 return -EMSGSIZE;
Thomas Graf14c0b972006-08-04 03:38:38 -0700627}
628
Thomas Grafc4546732007-03-25 23:24:24 -0700629static int dump_rules(struct sk_buff *skb, struct netlink_callback *cb,
630 struct fib_rules_ops *ops)
Thomas Graf14c0b972006-08-04 03:38:38 -0700631{
632 int idx = 0;
633 struct fib_rule *rule;
Wilson Kok41fc0142015-09-22 21:40:22 -0700634 int err = 0;
Thomas Graf14c0b972006-08-04 03:38:38 -0700635
Eric Dumazete67f88d2011-04-27 22:56:07 +0000636 rcu_read_lock();
637 list_for_each_entry_rcu(rule, &ops->rules_list, list) {
Thomas Grafc4546732007-03-25 23:24:24 -0700638 if (idx < cb->args[1])
Thomas Graf14c0b972006-08-04 03:38:38 -0700639 goto skip;
640
Wilson Kok41fc0142015-09-22 21:40:22 -0700641 err = fib_nl_fill_rule(skb, rule, NETLINK_CB(cb->skb).portid,
642 cb->nlh->nlmsg_seq, RTM_NEWRULE,
643 NLM_F_MULTI, ops);
644 if (err)
Thomas Graf14c0b972006-08-04 03:38:38 -0700645 break;
646skip:
647 idx++;
648 }
Eric Dumazet2907c352011-05-25 07:34:04 +0000649 rcu_read_unlock();
Thomas Grafc4546732007-03-25 23:24:24 -0700650 cb->args[1] = idx;
Thomas Graf14c0b972006-08-04 03:38:38 -0700651 rules_ops_put(ops);
652
Wilson Kok41fc0142015-09-22 21:40:22 -0700653 return err;
Thomas Graf14c0b972006-08-04 03:38:38 -0700654}
655
Thomas Grafc4546732007-03-25 23:24:24 -0700656static int fib_nl_dumprule(struct sk_buff *skb, struct netlink_callback *cb)
657{
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +0900658 struct net *net = sock_net(skb->sk);
Thomas Grafc4546732007-03-25 23:24:24 -0700659 struct fib_rules_ops *ops;
660 int idx = 0, family;
661
662 family = rtnl_msg_family(cb->nlh);
663 if (family != AF_UNSPEC) {
664 /* Protocol specific dump request */
Denis V. Lunev5fd30ee2008-01-10 03:20:28 -0800665 ops = lookup_rules_ops(net, family);
Thomas Grafc4546732007-03-25 23:24:24 -0700666 if (ops == NULL)
667 return -EAFNOSUPPORT;
668
Wilson Kok41fc0142015-09-22 21:40:22 -0700669 dump_rules(skb, cb, ops);
670
671 return skb->len;
Thomas Grafc4546732007-03-25 23:24:24 -0700672 }
673
674 rcu_read_lock();
Denis V. Lunev5fd30ee2008-01-10 03:20:28 -0800675 list_for_each_entry_rcu(ops, &net->rules_ops, list) {
Thomas Grafc4546732007-03-25 23:24:24 -0700676 if (idx < cb->args[0] || !try_module_get(ops->owner))
677 goto skip;
678
679 if (dump_rules(skb, cb, ops) < 0)
680 break;
681
682 cb->args[1] = 0;
Eric Dumazet2fb35732010-03-09 20:03:38 +0000683skip:
Thomas Grafc4546732007-03-25 23:24:24 -0700684 idx++;
685 }
686 rcu_read_unlock();
687 cb->args[0] = idx;
688
689 return skb->len;
690}
Thomas Graf14c0b972006-08-04 03:38:38 -0700691
Denis V. Lunev9e3a5482008-01-20 16:46:41 -0800692static void notify_rule_change(int event, struct fib_rule *rule,
Thomas Grafc17084d2006-08-15 00:32:48 -0700693 struct fib_rules_ops *ops, struct nlmsghdr *nlh,
694 u32 pid)
Thomas Graf14c0b972006-08-04 03:38:38 -0700695{
Denis V. Lunev9e3a5482008-01-20 16:46:41 -0800696 struct net *net;
Thomas Grafc17084d2006-08-15 00:32:48 -0700697 struct sk_buff *skb;
698 int err = -ENOBUFS;
Thomas Graf14c0b972006-08-04 03:38:38 -0700699
Denis V. Lunev9e3a5482008-01-20 16:46:41 -0800700 net = ops->fro_net;
Thomas Graf339bf982006-11-10 14:10:15 -0800701 skb = nlmsg_new(fib_rule_nlmsg_size(ops, rule), GFP_KERNEL);
Thomas Graf14c0b972006-08-04 03:38:38 -0700702 if (skb == NULL)
Thomas Grafc17084d2006-08-15 00:32:48 -0700703 goto errout;
704
705 err = fib_nl_fill_rule(skb, rule, pid, nlh->nlmsg_seq, event, 0, ops);
Patrick McHardy26932562007-01-31 23:16:40 -0800706 if (err < 0) {
707 /* -EMSGSIZE implies BUG in fib_rule_nlmsg_size() */
708 WARN_ON(err == -EMSGSIZE);
709 kfree_skb(skb);
710 goto errout;
711 }
Denis V. Lunev9e3a5482008-01-20 16:46:41 -0800712
Pablo Neira Ayuso1ce85fe2009-02-24 23:18:28 -0800713 rtnl_notify(skb, net, pid, ops->nlgroup, nlh, GFP_KERNEL);
714 return;
Thomas Grafc17084d2006-08-15 00:32:48 -0700715errout:
716 if (err < 0)
Denis V. Lunev5fd30ee2008-01-10 03:20:28 -0800717 rtnl_set_sk_err(net, ops->nlgroup, err);
Thomas Graf14c0b972006-08-04 03:38:38 -0700718}
719
720static void attach_rules(struct list_head *rules, struct net_device *dev)
721{
722 struct fib_rule *rule;
723
724 list_for_each_entry(rule, rules, list) {
Patrick McHardy491deb22009-12-03 01:25:54 +0000725 if (rule->iifindex == -1 &&
726 strcmp(dev->name, rule->iifname) == 0)
727 rule->iifindex = dev->ifindex;
Patrick McHardy1b038a52009-12-03 01:25:56 +0000728 if (rule->oifindex == -1 &&
729 strcmp(dev->name, rule->oifname) == 0)
730 rule->oifindex = dev->ifindex;
Thomas Graf14c0b972006-08-04 03:38:38 -0700731 }
732}
733
734static void detach_rules(struct list_head *rules, struct net_device *dev)
735{
736 struct fib_rule *rule;
737
Patrick McHardy1b038a52009-12-03 01:25:56 +0000738 list_for_each_entry(rule, rules, list) {
Patrick McHardy491deb22009-12-03 01:25:54 +0000739 if (rule->iifindex == dev->ifindex)
740 rule->iifindex = -1;
Patrick McHardy1b038a52009-12-03 01:25:56 +0000741 if (rule->oifindex == dev->ifindex)
742 rule->oifindex = -1;
743 }
Thomas Graf14c0b972006-08-04 03:38:38 -0700744}
745
746
747static int fib_rules_event(struct notifier_block *this, unsigned long event,
Jiri Pirko351638e2013-05-28 01:30:21 +0000748 void *ptr)
Thomas Graf14c0b972006-08-04 03:38:38 -0700749{
Jiri Pirko351638e2013-05-28 01:30:21 +0000750 struct net_device *dev = netdev_notifier_info_to_dev(ptr);
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +0900751 struct net *net = dev_net(dev);
Thomas Graf14c0b972006-08-04 03:38:38 -0700752 struct fib_rules_ops *ops;
753
Eric Dumazet748e2d92012-08-22 21:50:59 +0000754 ASSERT_RTNL();
Thomas Graf14c0b972006-08-04 03:38:38 -0700755
756 switch (event) {
757 case NETDEV_REGISTER:
Denis V. Lunev5fd30ee2008-01-10 03:20:28 -0800758 list_for_each_entry(ops, &net->rules_ops, list)
Denis V. Lunev76c72d42007-09-16 15:44:27 -0700759 attach_rules(&ops->rules_list, dev);
Thomas Graf14c0b972006-08-04 03:38:38 -0700760 break;
761
Maciej Żenczykowski946c0322014-02-07 16:23:48 -0800762 case NETDEV_CHANGENAME:
763 list_for_each_entry(ops, &net->rules_ops, list) {
764 detach_rules(&ops->rules_list, dev);
765 attach_rules(&ops->rules_list, dev);
766 }
767 break;
768
Thomas Graf14c0b972006-08-04 03:38:38 -0700769 case NETDEV_UNREGISTER:
Denis V. Lunev5fd30ee2008-01-10 03:20:28 -0800770 list_for_each_entry(ops, &net->rules_ops, list)
Denis V. Lunev76c72d42007-09-16 15:44:27 -0700771 detach_rules(&ops->rules_list, dev);
Thomas Graf14c0b972006-08-04 03:38:38 -0700772 break;
773 }
774
Thomas Graf14c0b972006-08-04 03:38:38 -0700775 return NOTIFY_DONE;
776}
777
778static struct notifier_block fib_rules_notifier = {
779 .notifier_call = fib_rules_event,
780};
781
Alexey Dobriyan2c8c1e72010-01-17 03:35:32 +0000782static int __net_init fib_rules_net_init(struct net *net)
Denis V. Lunev5fd30ee2008-01-10 03:20:28 -0800783{
784 INIT_LIST_HEAD(&net->rules_ops);
785 spin_lock_init(&net->rules_mod_lock);
786 return 0;
787}
788
789static struct pernet_operations fib_rules_net_ops = {
790 .init = fib_rules_net_init,
791};
792
Thomas Graf14c0b972006-08-04 03:38:38 -0700793static int __init fib_rules_init(void)
794{
Denis V. Lunev5fd30ee2008-01-10 03:20:28 -0800795 int err;
Greg Rosec7ac8672011-06-10 01:27:09 +0000796 rtnl_register(PF_UNSPEC, RTM_NEWRULE, fib_nl_newrule, NULL, NULL);
797 rtnl_register(PF_UNSPEC, RTM_DELRULE, fib_nl_delrule, NULL, NULL);
798 rtnl_register(PF_UNSPEC, RTM_GETRULE, NULL, fib_nl_dumprule, NULL);
Thomas Graf9d9e6a52007-03-25 23:20:05 -0700799
Eric W. Biederman5d6d4802008-11-07 22:52:34 -0800800 err = register_pernet_subsys(&fib_rules_net_ops);
Denis V. Lunev5fd30ee2008-01-10 03:20:28 -0800801 if (err < 0)
802 goto fail;
803
Eric W. Biederman5d6d4802008-11-07 22:52:34 -0800804 err = register_netdevice_notifier(&fib_rules_notifier);
Denis V. Lunev5fd30ee2008-01-10 03:20:28 -0800805 if (err < 0)
806 goto fail_unregister;
Eric W. Biederman5d6d4802008-11-07 22:52:34 -0800807
Denis V. Lunev5fd30ee2008-01-10 03:20:28 -0800808 return 0;
809
810fail_unregister:
Eric W. Biederman5d6d4802008-11-07 22:52:34 -0800811 unregister_pernet_subsys(&fib_rules_net_ops);
Denis V. Lunev5fd30ee2008-01-10 03:20:28 -0800812fail:
813 rtnl_unregister(PF_UNSPEC, RTM_NEWRULE);
814 rtnl_unregister(PF_UNSPEC, RTM_DELRULE);
815 rtnl_unregister(PF_UNSPEC, RTM_GETRULE);
816 return err;
Thomas Graf14c0b972006-08-04 03:38:38 -0700817}
818
819subsys_initcall(fib_rules_init);