Commit | Line | Data |
---|---|---|
14c0b97d TG |
1 | /* |
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 | ||
14c0b97d TG |
11 | #include <linux/types.h> |
12 | #include <linux/kernel.h> | |
5a0e3ad6 | 13 | #include <linux/slab.h> |
14c0b97d | 14 | #include <linux/list.h> |
3a9a231d | 15 | #include <linux/module.h> |
e9dc8653 | 16 | #include <net/net_namespace.h> |
881d966b | 17 | #include <net/sock.h> |
14c0b97d | 18 | #include <net/fib_rules.h> |
e7030878 | 19 | #include <net/ip_tunnels.h> |
14c0b97d | 20 | |
2994c638 DL |
21 | int 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; | |
efd7ef1c | 35 | r->fr_net = ops->fro_net; |
2994c638 | 36 | |
73f5698e ST |
37 | r->suppress_prefixlen = -1; |
38 | r->suppress_ifgroup = -1; | |
39 | ||
2994c638 DL |
40 | /* 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 | } | |
45 | EXPORT_SYMBOL(fib_default_rule_add); | |
46 | ||
f53de1e9 | 47 | static u32 fib_default_rule_pref(struct fib_rules_ops *ops) |
d8a566be PM |
48 | { |
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 | } | |
d8a566be | 63 | |
9e3a5487 | 64 | static void notify_rule_change(int event, struct fib_rule *rule, |
c17084d2 TG |
65 | struct fib_rules_ops *ops, struct nlmsghdr *nlh, |
66 | u32 pid); | |
14c0b97d | 67 | |
5fd30ee7 | 68 | static struct fib_rules_ops *lookup_rules_ops(struct net *net, int family) |
14c0b97d TG |
69 | { |
70 | struct fib_rules_ops *ops; | |
71 | ||
72 | rcu_read_lock(); | |
5fd30ee7 | 73 | list_for_each_entry_rcu(ops, &net->rules_ops, list) { |
14c0b97d TG |
74 | 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 | ||
86 | static void rules_ops_put(struct fib_rules_ops *ops) | |
87 | { | |
88 | if (ops) | |
89 | module_put(ops->owner); | |
90 | } | |
91 | ||
73417f61 TG |
92 | static void flush_route_cache(struct fib_rules_ops *ops) |
93 | { | |
94 | if (ops->flush_cache) | |
ae299fc0 | 95 | ops->flush_cache(ops); |
73417f61 TG |
96 | } |
97 | ||
e9c5158a | 98 | static int __fib_rules_register(struct fib_rules_ops *ops) |
14c0b97d TG |
99 | { |
100 | int err = -EEXIST; | |
101 | struct fib_rules_ops *o; | |
9e3a5487 DL |
102 | struct net *net; |
103 | ||
104 | net = ops->fro_net; | |
14c0b97d TG |
105 | |
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 | ||
5fd30ee7 DL |
114 | spin_lock(&net->rules_mod_lock); |
115 | list_for_each_entry(o, &net->rules_ops, list) | |
14c0b97d TG |
116 | if (ops->family == o->family) |
117 | goto errout; | |
118 | ||
5fd30ee7 | 119 | list_add_tail_rcu(&ops->list, &net->rules_ops); |
14c0b97d TG |
120 | err = 0; |
121 | errout: | |
5fd30ee7 | 122 | spin_unlock(&net->rules_mod_lock); |
14c0b97d TG |
123 | |
124 | return err; | |
125 | } | |
126 | ||
e9c5158a | 127 | struct fib_rules_ops * |
3d0c9c4e | 128 | fib_rules_register(const struct fib_rules_ops *tmpl, struct net *net) |
e9c5158a EB |
129 | { |
130 | struct fib_rules_ops *ops; | |
131 | int err; | |
132 | ||
2fb3573d | 133 | ops = kmemdup(tmpl, sizeof(*ops), GFP_KERNEL); |
e9c5158a EB |
134 | 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 | } | |
14c0b97d TG |
148 | EXPORT_SYMBOL_GPL(fib_rules_register); |
149 | ||
1df9916e | 150 | static void fib_rules_cleanup_ops(struct fib_rules_ops *ops) |
14c0b97d TG |
151 | { |
152 | struct fib_rule *rule, *tmp; | |
153 | ||
76c72d4f | 154 | list_for_each_entry_safe(rule, tmp, &ops->rules_list, list) { |
14c0b97d | 155 | list_del_rcu(&rule->list); |
7a9bc9b8 DM |
156 | if (ops->delete) |
157 | ops->delete(rule); | |
14c0b97d TG |
158 | fib_rule_put(rule); |
159 | } | |
160 | } | |
161 | ||
9e3a5487 | 162 | void fib_rules_unregister(struct fib_rules_ops *ops) |
14c0b97d | 163 | { |
9e3a5487 | 164 | struct net *net = ops->fro_net; |
14c0b97d | 165 | |
5fd30ee7 | 166 | spin_lock(&net->rules_mod_lock); |
72132c1b | 167 | list_del_rcu(&ops->list); |
5fd30ee7 | 168 | spin_unlock(&net->rules_mod_lock); |
14c0b97d | 169 | |
419df12f | 170 | fib_rules_cleanup_ops(ops); |
efd7ef1c | 171 | kfree_rcu(ops, rcu); |
14c0b97d | 172 | } |
14c0b97d TG |
173 | EXPORT_SYMBOL_GPL(fib_rules_unregister); |
174 | ||
3dfbcc41 | 175 | static int fib_rule_match(struct fib_rule *rule, struct fib_rules_ops *ops, |
96c63fa7 DA |
176 | struct flowi *fl, int flags, |
177 | struct fib_lookup_arg *arg) | |
3dfbcc41 TG |
178 | { |
179 | int ret = 0; | |
180 | ||
1d28f42c | 181 | if (rule->iifindex && (rule->iifindex != fl->flowi_iif)) |
3dfbcc41 TG |
182 | goto out; |
183 | ||
1d28f42c | 184 | if (rule->oifindex && (rule->oifindex != fl->flowi_oif)) |
1b038a5e PM |
185 | goto out; |
186 | ||
1d28f42c | 187 | if ((rule->mark ^ fl->flowi_mark) & rule->mark_mask) |
3dfbcc41 TG |
188 | goto out; |
189 | ||
e7030878 TG |
190 | if (rule->tun_id && (rule->tun_id != fl->flowi_tun_key.tun_id)) |
191 | goto out; | |
192 | ||
96c63fa7 DA |
193 | if (rule->l3mdev && !l3mdev_fib_rule_match(rule->fr_net, fl, arg)) |
194 | goto out; | |
195 | ||
3dfbcc41 TG |
196 | ret = ops->match(rule, fl, flags); |
197 | out: | |
198 | return (rule->flags & FIB_RULE_INVERT) ? !ret : ret; | |
199 | } | |
200 | ||
14c0b97d TG |
201 | int 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 | ||
76c72d4f | 209 | list_for_each_entry_rcu(rule, &ops->rules_list, list) { |
0947c9fe | 210 | jumped: |
96c63fa7 | 211 | if (!fib_rule_match(rule, ops, fl, flags, arg)) |
14c0b97d TG |
212 | continue; |
213 | ||
0947c9fe TG |
214 | 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 | } | |
fa0b2d1d TG |
224 | } else if (rule->action == FR_ACT_NOP) |
225 | continue; | |
226 | else | |
0947c9fe TG |
227 | err = ops->action(rule, fl, flags, arg); |
228 | ||
7764a45a ST |
229 | if (!err && ops->suppress && ops->suppress(rule, arg)) |
230 | continue; | |
231 | ||
14c0b97d | 232 | if (err != -EAGAIN) { |
ebc0ffae ED |
233 | if ((arg->flags & FIB_LOOKUP_NOREF) || |
234 | likely(atomic_inc_not_zero(&rule->refcnt))) { | |
7fa7cb71 ED |
235 | arg->rule = rule; |
236 | goto out; | |
237 | } | |
238 | break; | |
14c0b97d TG |
239 | } |
240 | } | |
241 | ||
83886b6b | 242 | err = -ESRCH; |
14c0b97d TG |
243 | out: |
244 | rcu_read_unlock(); | |
245 | ||
246 | return err; | |
247 | } | |
14c0b97d TG |
248 | EXPORT_SYMBOL_GPL(fib_rules_lookup); |
249 | ||
e1701c68 TG |
250 | static 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; | |
268 | errout: | |
269 | return err; | |
270 | } | |
271 | ||
153380ec MB |
272 | static 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 | ||
96c63fa7 | 315 | int fib_nl_newrule(struct sk_buff *skb, struct nlmsghdr *nlh) |
14c0b97d | 316 | { |
3b1e0a65 | 317 | struct net *net = sock_net(skb->sk); |
14c0b97d TG |
318 | 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]; | |
0947c9fe | 322 | int err = -EINVAL, unresolved = 0; |
14c0b97d TG |
323 | |
324 | if (nlh->nlmsg_len < nlmsg_msg_size(sizeof(*frh))) | |
325 | goto errout; | |
326 | ||
5fd30ee7 | 327 | ops = lookup_rules_ops(net, frh->family); |
14c0b97d | 328 | if (ops == NULL) { |
2fe195cf | 329 | err = -EAFNOSUPPORT; |
14c0b97d TG |
330 | goto errout; |
331 | } | |
332 | ||
333 | err = nlmsg_parse(nlh, sizeof(*frh), tb, FRA_MAX, ops->policy); | |
334 | if (err < 0) | |
335 | goto errout; | |
336 | ||
e1701c68 TG |
337 | err = validate_rulemsg(frh, tb, ops); |
338 | if (err < 0) | |
339 | goto errout; | |
340 | ||
14c0b97d TG |
341 | rule = kzalloc(ops->rule_size, GFP_KERNEL); |
342 | if (rule == NULL) { | |
343 | err = -ENOMEM; | |
344 | goto errout; | |
345 | } | |
efd7ef1c | 346 | rule->fr_net = net; |
14c0b97d | 347 | |
f53de1e9 PS |
348 | rule->pref = tb[FRA_PRIORITY] ? nla_get_u32(tb[FRA_PRIORITY]) |
349 | : fib_default_rule_pref(ops); | |
14c0b97d | 350 | |
491deb24 | 351 | if (tb[FRA_IIFNAME]) { |
14c0b97d TG |
352 | struct net_device *dev; |
353 | ||
491deb24 PM |
354 | rule->iifindex = -1; |
355 | nla_strlcpy(rule->iifname, tb[FRA_IIFNAME], IFNAMSIZ); | |
356 | dev = __dev_get_by_name(net, rule->iifname); | |
14c0b97d | 357 | if (dev) |
491deb24 | 358 | rule->iifindex = dev->ifindex; |
14c0b97d TG |
359 | } |
360 | ||
1b038a5e PM |
361 | 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 | ||
b8964ed9 TG |
371 | 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 | ||
e7030878 TG |
383 | if (tb[FRA_TUN_ID]) |
384 | rule->tun_id = nla_get_be64(tb[FRA_TUN_ID]); | |
385 | ||
96c63fa7 DA |
386 | 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 | ||
14c0b97d TG |
394 | rule->action = frh->action; |
395 | rule->flags = frh->flags; | |
9e762a4a | 396 | rule->table = frh_get_table(frh, tb); |
73f5698e ST |
397 | if (tb[FRA_SUPPRESS_PREFIXLEN]) |
398 | rule->suppress_prefixlen = nla_get_u32(tb[FRA_SUPPRESS_PREFIXLEN]); | |
399 | else | |
400 | rule->suppress_prefixlen = -1; | |
14c0b97d | 401 | |
6ef94cfa ST |
402 | if (tb[FRA_SUPPRESS_IFGROUP]) |
403 | rule->suppress_ifgroup = nla_get_u32(tb[FRA_SUPPRESS_IFGROUP]); | |
73f5698e ST |
404 | else |
405 | rule->suppress_ifgroup = -1; | |
6ef94cfa | 406 | |
0947c9fe TG |
407 | 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 | ||
76c72d4f | 417 | list_for_each_entry(r, &ops->rules_list, list) { |
0947c9fe | 418 | if (r->pref == rule->target) { |
7a2b03c5 | 419 | RCU_INIT_POINTER(rule->ctarget, r); |
0947c9fe TG |
420 | break; |
421 | } | |
422 | } | |
423 | ||
7a2b03c5 | 424 | if (rcu_dereference_protected(rule->ctarget, 1) == NULL) |
0947c9fe TG |
425 | unresolved = 1; |
426 | } else if (rule->action == FR_ACT_GOTO) | |
427 | goto errout_free; | |
428 | ||
96c63fa7 DA |
429 | if (rule->l3mdev && rule->table) |
430 | goto errout_free; | |
431 | ||
153380ec MB |
432 | if ((nlh->nlmsg_flags & NLM_F_EXCL) && |
433 | rule_exists(ops, frh, tb, rule)) { | |
434 | err = -EEXIST; | |
435 | goto errout_free; | |
436 | } | |
437 | ||
8b3521ee | 438 | err = ops->configure(rule, skb, frh, tb); |
14c0b97d TG |
439 | if (err < 0) |
440 | goto errout_free; | |
441 | ||
76c72d4f | 442 | list_for_each_entry(r, &ops->rules_list, list) { |
14c0b97d TG |
443 | if (r->pref > rule->pref) |
444 | break; | |
445 | last = r; | |
446 | } | |
447 | ||
448 | fib_rule_get(rule); | |
449 | ||
ebb9fed2 ED |
450 | if (last) |
451 | list_add_rcu(&rule->list, &last->list); | |
452 | else | |
453 | list_add_rcu(&rule->list, &ops->rules_list); | |
454 | ||
0947c9fe TG |
455 | if (ops->unresolved_rules) { |
456 | /* | |
457 | * There are unresolved goto rules in the list, check if | |
458 | * any of them are pointing to this new rule. | |
459 | */ | |
76c72d4f | 460 | list_for_each_entry(r, &ops->rules_list, list) { |
0947c9fe | 461 | if (r->action == FR_ACT_GOTO && |
561dac2d G |
462 | r->target == rule->pref && |
463 | rtnl_dereference(r->ctarget) == NULL) { | |
0947c9fe TG |
464 | rcu_assign_pointer(r->ctarget, rule); |
465 | if (--ops->unresolved_rules == 0) | |
466 | break; | |
467 | } | |
468 | } | |
469 | } | |
470 | ||
471 | if (rule->action == FR_ACT_GOTO) | |
472 | ops->nr_goto_rules++; | |
473 | ||
474 | if (unresolved) | |
475 | ops->unresolved_rules++; | |
476 | ||
e7030878 TG |
477 | if (rule->tun_id) |
478 | ip_tunnel_need_metadata(); | |
479 | ||
15e47304 | 480 | notify_rule_change(RTM_NEWRULE, rule, ops, nlh, NETLINK_CB(skb).portid); |
73417f61 | 481 | flush_route_cache(ops); |
14c0b97d TG |
482 | rules_ops_put(ops); |
483 | return 0; | |
484 | ||
485 | errout_free: | |
486 | kfree(rule); | |
487 | errout: | |
488 | rules_ops_put(ops); | |
489 | return err; | |
490 | } | |
96c63fa7 | 491 | EXPORT_SYMBOL_GPL(fib_nl_newrule); |
14c0b97d | 492 | |
96c63fa7 | 493 | int fib_nl_delrule(struct sk_buff *skb, struct nlmsghdr *nlh) |
14c0b97d | 494 | { |
3b1e0a65 | 495 | struct net *net = sock_net(skb->sk); |
14c0b97d TG |
496 | struct fib_rule_hdr *frh = nlmsg_data(nlh); |
497 | struct fib_rules_ops *ops = NULL; | |
0947c9fe | 498 | struct fib_rule *rule, *tmp; |
14c0b97d TG |
499 | struct nlattr *tb[FRA_MAX+1]; |
500 | int err = -EINVAL; | |
501 | ||
502 | if (nlh->nlmsg_len < nlmsg_msg_size(sizeof(*frh))) | |
503 | goto errout; | |
504 | ||
5fd30ee7 | 505 | ops = lookup_rules_ops(net, frh->family); |
14c0b97d | 506 | if (ops == NULL) { |
2fe195cf | 507 | err = -EAFNOSUPPORT; |
14c0b97d TG |
508 | goto errout; |
509 | } | |
510 | ||
511 | err = nlmsg_parse(nlh, sizeof(*frh), tb, FRA_MAX, ops->policy); | |
512 | if (err < 0) | |
513 | goto errout; | |
514 | ||
e1701c68 TG |
515 | err = validate_rulemsg(frh, tb, ops); |
516 | if (err < 0) | |
517 | goto errout; | |
518 | ||
76c72d4f | 519 | list_for_each_entry(rule, &ops->rules_list, list) { |
14c0b97d TG |
520 | if (frh->action && (frh->action != rule->action)) |
521 | continue; | |
522 | ||
13eb2ab2 AH |
523 | if (frh_get_table(frh, tb) && |
524 | (frh_get_table(frh, tb) != rule->table)) | |
14c0b97d TG |
525 | continue; |
526 | ||
527 | if (tb[FRA_PRIORITY] && | |
528 | (rule->pref != nla_get_u32(tb[FRA_PRIORITY]))) | |
529 | continue; | |
530 | ||
491deb24 PM |
531 | if (tb[FRA_IIFNAME] && |
532 | nla_strcmp(tb[FRA_IIFNAME], rule->iifname)) | |
14c0b97d TG |
533 | continue; |
534 | ||
1b038a5e PM |
535 | if (tb[FRA_OIFNAME] && |
536 | nla_strcmp(tb[FRA_OIFNAME], rule->oifname)) | |
537 | continue; | |
538 | ||
b8964ed9 TG |
539 | if (tb[FRA_FWMARK] && |
540 | (rule->mark != nla_get_u32(tb[FRA_FWMARK]))) | |
541 | continue; | |
542 | ||
543 | if (tb[FRA_FWMASK] && | |
544 | (rule->mark_mask != nla_get_u32(tb[FRA_FWMASK]))) | |
545 | continue; | |
546 | ||
e7030878 TG |
547 | if (tb[FRA_TUN_ID] && |
548 | (rule->tun_id != nla_get_be64(tb[FRA_TUN_ID]))) | |
549 | continue; | |
550 | ||
96c63fa7 DA |
551 | if (tb[FRA_L3MDEV] && |
552 | (rule->l3mdev != nla_get_u8(tb[FRA_L3MDEV]))) | |
553 | continue; | |
554 | ||
14c0b97d TG |
555 | if (!ops->compare(rule, frh, tb)) |
556 | continue; | |
557 | ||
558 | if (rule->flags & FIB_RULE_PERMANENT) { | |
559 | err = -EPERM; | |
560 | goto errout; | |
561 | } | |
562 | ||
0ddcf43d AD |
563 | if (ops->delete) { |
564 | err = ops->delete(rule); | |
565 | if (err) | |
566 | goto errout; | |
567 | } | |
568 | ||
e7030878 TG |
569 | if (rule->tun_id) |
570 | ip_tunnel_unneed_metadata(); | |
571 | ||
14c0b97d | 572 | list_del_rcu(&rule->list); |
0947c9fe | 573 | |
afaef734 | 574 | if (rule->action == FR_ACT_GOTO) { |
0947c9fe | 575 | ops->nr_goto_rules--; |
afaef734 YZ |
576 | if (rtnl_dereference(rule->ctarget) == NULL) |
577 | ops->unresolved_rules--; | |
578 | } | |
0947c9fe TG |
579 | |
580 | /* | |
581 | * Check if this rule is a target to any of them. If so, | |
582 | * disable them. As this operation is eventually very | |
583 | * expensive, it is only performed if goto rules have | |
584 | * actually been added. | |
585 | */ | |
586 | if (ops->nr_goto_rules > 0) { | |
76c72d4f | 587 | list_for_each_entry(tmp, &ops->rules_list, list) { |
7a2b03c5 | 588 | if (rtnl_dereference(tmp->ctarget) == rule) { |
a9b3cd7f | 589 | RCU_INIT_POINTER(tmp->ctarget, NULL); |
0947c9fe TG |
590 | ops->unresolved_rules++; |
591 | } | |
592 | } | |
593 | } | |
594 | ||
9e3a5487 | 595 | notify_rule_change(RTM_DELRULE, rule, ops, nlh, |
15e47304 | 596 | NETLINK_CB(skb).portid); |
14c0b97d | 597 | fib_rule_put(rule); |
73417f61 | 598 | flush_route_cache(ops); |
14c0b97d TG |
599 | rules_ops_put(ops); |
600 | return 0; | |
601 | } | |
602 | ||
603 | err = -ENOENT; | |
604 | errout: | |
605 | rules_ops_put(ops); | |
606 | return err; | |
607 | } | |
96c63fa7 | 608 | EXPORT_SYMBOL_GPL(fib_nl_delrule); |
14c0b97d | 609 | |
339bf98f TG |
610 | static inline size_t fib_rule_nlmsg_size(struct fib_rules_ops *ops, |
611 | struct fib_rule *rule) | |
612 | { | |
613 | size_t payload = NLMSG_ALIGN(sizeof(struct fib_rule_hdr)) | |
491deb24 | 614 | + nla_total_size(IFNAMSIZ) /* FRA_IIFNAME */ |
1b038a5e | 615 | + nla_total_size(IFNAMSIZ) /* FRA_OIFNAME */ |
339bf98f TG |
616 | + nla_total_size(4) /* FRA_PRIORITY */ |
617 | + nla_total_size(4) /* FRA_TABLE */ | |
73f5698e | 618 | + nla_total_size(4) /* FRA_SUPPRESS_PREFIXLEN */ |
6ef94cfa | 619 | + nla_total_size(4) /* FRA_SUPPRESS_IFGROUP */ |
339bf98f | 620 | + nla_total_size(4) /* FRA_FWMARK */ |
e7030878 | 621 | + nla_total_size(4) /* FRA_FWMASK */ |
b46f6ded | 622 | + nla_total_size_64bit(8); /* FRA_TUN_ID */ |
339bf98f TG |
623 | |
624 | if (ops->nlmsg_payload) | |
625 | payload += ops->nlmsg_payload(rule); | |
626 | ||
627 | return payload; | |
628 | } | |
629 | ||
14c0b97d TG |
630 | static int fib_nl_fill_rule(struct sk_buff *skb, struct fib_rule *rule, |
631 | u32 pid, u32 seq, int type, int flags, | |
632 | struct fib_rules_ops *ops) | |
633 | { | |
634 | struct nlmsghdr *nlh; | |
635 | struct fib_rule_hdr *frh; | |
636 | ||
637 | nlh = nlmsg_put(skb, pid, seq, type, sizeof(*frh), flags); | |
638 | if (nlh == NULL) | |
26932566 | 639 | return -EMSGSIZE; |
14c0b97d TG |
640 | |
641 | frh = nlmsg_data(nlh); | |
28bb1726 | 642 | frh->family = ops->family; |
14c0b97d | 643 | frh->table = rule->table; |
0e3cea7b DM |
644 | if (nla_put_u32(skb, FRA_TABLE, rule->table)) |
645 | goto nla_put_failure; | |
73f5698e | 646 | if (nla_put_u32(skb, FRA_SUPPRESS_PREFIXLEN, rule->suppress_prefixlen)) |
7764a45a | 647 | goto nla_put_failure; |
14c0b97d TG |
648 | frh->res1 = 0; |
649 | frh->res2 = 0; | |
650 | frh->action = rule->action; | |
651 | frh->flags = rule->flags; | |
652 | ||
7a2b03c5 | 653 | if (rule->action == FR_ACT_GOTO && |
33d480ce | 654 | rcu_access_pointer(rule->ctarget) == NULL) |
0947c9fe TG |
655 | frh->flags |= FIB_RULE_UNRESOLVED; |
656 | ||
491deb24 | 657 | if (rule->iifname[0]) { |
0e3cea7b DM |
658 | if (nla_put_string(skb, FRA_IIFNAME, rule->iifname)) |
659 | goto nla_put_failure; | |
491deb24 PM |
660 | if (rule->iifindex == -1) |
661 | frh->flags |= FIB_RULE_IIF_DETACHED; | |
2b443683 TG |
662 | } |
663 | ||
1b038a5e | 664 | if (rule->oifname[0]) { |
0e3cea7b DM |
665 | if (nla_put_string(skb, FRA_OIFNAME, rule->oifname)) |
666 | goto nla_put_failure; | |
1b038a5e PM |
667 | if (rule->oifindex == -1) |
668 | frh->flags |= FIB_RULE_OIF_DETACHED; | |
669 | } | |
670 | ||
0e3cea7b DM |
671 | if ((rule->pref && |
672 | nla_put_u32(skb, FRA_PRIORITY, rule->pref)) || | |
673 | (rule->mark && | |
674 | nla_put_u32(skb, FRA_FWMARK, rule->mark)) || | |
675 | ((rule->mark_mask || rule->mark) && | |
676 | nla_put_u32(skb, FRA_FWMASK, rule->mark_mask)) || | |
677 | (rule->target && | |
e7030878 TG |
678 | nla_put_u32(skb, FRA_GOTO, rule->target)) || |
679 | (rule->tun_id && | |
96c63fa7 DA |
680 | nla_put_be64(skb, FRA_TUN_ID, rule->tun_id, FRA_PAD)) || |
681 | (rule->l3mdev && | |
682 | nla_put_u8(skb, FRA_L3MDEV, rule->l3mdev))) | |
0e3cea7b | 683 | goto nla_put_failure; |
6ef94cfa ST |
684 | |
685 | if (rule->suppress_ifgroup != -1) { | |
686 | if (nla_put_u32(skb, FRA_SUPPRESS_IFGROUP, rule->suppress_ifgroup)) | |
687 | goto nla_put_failure; | |
688 | } | |
689 | ||
04af8cf6 | 690 | if (ops->fill(rule, skb, frh) < 0) |
14c0b97d TG |
691 | goto nla_put_failure; |
692 | ||
053c095a JB |
693 | nlmsg_end(skb, nlh); |
694 | return 0; | |
14c0b97d TG |
695 | |
696 | nla_put_failure: | |
26932566 PM |
697 | nlmsg_cancel(skb, nlh); |
698 | return -EMSGSIZE; | |
14c0b97d TG |
699 | } |
700 | ||
c454673d TG |
701 | static int dump_rules(struct sk_buff *skb, struct netlink_callback *cb, |
702 | struct fib_rules_ops *ops) | |
14c0b97d TG |
703 | { |
704 | int idx = 0; | |
705 | struct fib_rule *rule; | |
41fc0143 | 706 | int err = 0; |
14c0b97d | 707 | |
e67f88dd ED |
708 | rcu_read_lock(); |
709 | list_for_each_entry_rcu(rule, &ops->rules_list, list) { | |
c454673d | 710 | if (idx < cb->args[1]) |
14c0b97d TG |
711 | goto skip; |
712 | ||
41fc0143 WK |
713 | err = fib_nl_fill_rule(skb, rule, NETLINK_CB(cb->skb).portid, |
714 | cb->nlh->nlmsg_seq, RTM_NEWRULE, | |
715 | NLM_F_MULTI, ops); | |
716 | if (err) | |
14c0b97d TG |
717 | break; |
718 | skip: | |
719 | idx++; | |
720 | } | |
2907c35f | 721 | rcu_read_unlock(); |
c454673d | 722 | cb->args[1] = idx; |
14c0b97d TG |
723 | rules_ops_put(ops); |
724 | ||
41fc0143 | 725 | return err; |
14c0b97d TG |
726 | } |
727 | ||
c454673d TG |
728 | static int fib_nl_dumprule(struct sk_buff *skb, struct netlink_callback *cb) |
729 | { | |
3b1e0a65 | 730 | struct net *net = sock_net(skb->sk); |
c454673d TG |
731 | struct fib_rules_ops *ops; |
732 | int idx = 0, family; | |
733 | ||
734 | family = rtnl_msg_family(cb->nlh); | |
735 | if (family != AF_UNSPEC) { | |
736 | /* Protocol specific dump request */ | |
5fd30ee7 | 737 | ops = lookup_rules_ops(net, family); |
c454673d TG |
738 | if (ops == NULL) |
739 | return -EAFNOSUPPORT; | |
740 | ||
41fc0143 WK |
741 | dump_rules(skb, cb, ops); |
742 | ||
743 | return skb->len; | |
c454673d TG |
744 | } |
745 | ||
746 | rcu_read_lock(); | |
5fd30ee7 | 747 | list_for_each_entry_rcu(ops, &net->rules_ops, list) { |
c454673d TG |
748 | if (idx < cb->args[0] || !try_module_get(ops->owner)) |
749 | goto skip; | |
750 | ||
751 | if (dump_rules(skb, cb, ops) < 0) | |
752 | break; | |
753 | ||
754 | cb->args[1] = 0; | |
2fb3573d | 755 | skip: |
c454673d TG |
756 | idx++; |
757 | } | |
758 | rcu_read_unlock(); | |
759 | cb->args[0] = idx; | |
760 | ||
761 | return skb->len; | |
762 | } | |
14c0b97d | 763 | |
9e3a5487 | 764 | static void notify_rule_change(int event, struct fib_rule *rule, |
c17084d2 TG |
765 | struct fib_rules_ops *ops, struct nlmsghdr *nlh, |
766 | u32 pid) | |
14c0b97d | 767 | { |
9e3a5487 | 768 | struct net *net; |
c17084d2 TG |
769 | struct sk_buff *skb; |
770 | int err = -ENOBUFS; | |
14c0b97d | 771 | |
9e3a5487 | 772 | net = ops->fro_net; |
339bf98f | 773 | skb = nlmsg_new(fib_rule_nlmsg_size(ops, rule), GFP_KERNEL); |
14c0b97d | 774 | if (skb == NULL) |
c17084d2 TG |
775 | goto errout; |
776 | ||
777 | err = fib_nl_fill_rule(skb, rule, pid, nlh->nlmsg_seq, event, 0, ops); | |
26932566 PM |
778 | if (err < 0) { |
779 | /* -EMSGSIZE implies BUG in fib_rule_nlmsg_size() */ | |
780 | WARN_ON(err == -EMSGSIZE); | |
781 | kfree_skb(skb); | |
782 | goto errout; | |
783 | } | |
9e3a5487 | 784 | |
1ce85fe4 PNA |
785 | rtnl_notify(skb, net, pid, ops->nlgroup, nlh, GFP_KERNEL); |
786 | return; | |
c17084d2 TG |
787 | errout: |
788 | if (err < 0) | |
5fd30ee7 | 789 | rtnl_set_sk_err(net, ops->nlgroup, err); |
14c0b97d TG |
790 | } |
791 | ||
792 | static void attach_rules(struct list_head *rules, struct net_device *dev) | |
793 | { | |
794 | struct fib_rule *rule; | |
795 | ||
796 | list_for_each_entry(rule, rules, list) { | |
491deb24 PM |
797 | if (rule->iifindex == -1 && |
798 | strcmp(dev->name, rule->iifname) == 0) | |
799 | rule->iifindex = dev->ifindex; | |
1b038a5e PM |
800 | if (rule->oifindex == -1 && |
801 | strcmp(dev->name, rule->oifname) == 0) | |
802 | rule->oifindex = dev->ifindex; | |
14c0b97d TG |
803 | } |
804 | } | |
805 | ||
806 | static void detach_rules(struct list_head *rules, struct net_device *dev) | |
807 | { | |
808 | struct fib_rule *rule; | |
809 | ||
1b038a5e | 810 | list_for_each_entry(rule, rules, list) { |
491deb24 PM |
811 | if (rule->iifindex == dev->ifindex) |
812 | rule->iifindex = -1; | |
1b038a5e PM |
813 | if (rule->oifindex == dev->ifindex) |
814 | rule->oifindex = -1; | |
815 | } | |
14c0b97d TG |
816 | } |
817 | ||
818 | ||
819 | static int fib_rules_event(struct notifier_block *this, unsigned long event, | |
351638e7 | 820 | void *ptr) |
14c0b97d | 821 | { |
351638e7 | 822 | struct net_device *dev = netdev_notifier_info_to_dev(ptr); |
c346dca1 | 823 | struct net *net = dev_net(dev); |
14c0b97d TG |
824 | struct fib_rules_ops *ops; |
825 | ||
748e2d93 | 826 | ASSERT_RTNL(); |
14c0b97d TG |
827 | |
828 | switch (event) { | |
829 | case NETDEV_REGISTER: | |
5fd30ee7 | 830 | list_for_each_entry(ops, &net->rules_ops, list) |
76c72d4f | 831 | attach_rules(&ops->rules_list, dev); |
14c0b97d TG |
832 | break; |
833 | ||
946c032e MŻ |
834 | case NETDEV_CHANGENAME: |
835 | list_for_each_entry(ops, &net->rules_ops, list) { | |
836 | detach_rules(&ops->rules_list, dev); | |
837 | attach_rules(&ops->rules_list, dev); | |
838 | } | |
839 | break; | |
840 | ||
14c0b97d | 841 | case NETDEV_UNREGISTER: |
5fd30ee7 | 842 | list_for_each_entry(ops, &net->rules_ops, list) |
76c72d4f | 843 | detach_rules(&ops->rules_list, dev); |
14c0b97d TG |
844 | break; |
845 | } | |
846 | ||
14c0b97d TG |
847 | return NOTIFY_DONE; |
848 | } | |
849 | ||
850 | static struct notifier_block fib_rules_notifier = { | |
851 | .notifier_call = fib_rules_event, | |
852 | }; | |
853 | ||
2c8c1e72 | 854 | static int __net_init fib_rules_net_init(struct net *net) |
5fd30ee7 DL |
855 | { |
856 | INIT_LIST_HEAD(&net->rules_ops); | |
857 | spin_lock_init(&net->rules_mod_lock); | |
858 | return 0; | |
859 | } | |
860 | ||
861 | static struct pernet_operations fib_rules_net_ops = { | |
862 | .init = fib_rules_net_init, | |
863 | }; | |
864 | ||
14c0b97d TG |
865 | static int __init fib_rules_init(void) |
866 | { | |
5fd30ee7 | 867 | int err; |
c7ac8679 GR |
868 | rtnl_register(PF_UNSPEC, RTM_NEWRULE, fib_nl_newrule, NULL, NULL); |
869 | rtnl_register(PF_UNSPEC, RTM_DELRULE, fib_nl_delrule, NULL, NULL); | |
870 | rtnl_register(PF_UNSPEC, RTM_GETRULE, NULL, fib_nl_dumprule, NULL); | |
9d9e6a58 | 871 | |
5d6d4809 | 872 | err = register_pernet_subsys(&fib_rules_net_ops); |
5fd30ee7 DL |
873 | if (err < 0) |
874 | goto fail; | |
875 | ||
5d6d4809 | 876 | err = register_netdevice_notifier(&fib_rules_notifier); |
5fd30ee7 DL |
877 | if (err < 0) |
878 | goto fail_unregister; | |
5d6d4809 | 879 | |
5fd30ee7 DL |
880 | return 0; |
881 | ||
882 | fail_unregister: | |
5d6d4809 | 883 | unregister_pernet_subsys(&fib_rules_net_ops); |
5fd30ee7 DL |
884 | fail: |
885 | rtnl_unregister(PF_UNSPEC, RTM_NEWRULE); | |
886 | rtnl_unregister(PF_UNSPEC, RTM_DELRULE); | |
887 | rtnl_unregister(PF_UNSPEC, RTM_GETRULE); | |
888 | return err; | |
14c0b97d TG |
889 | } |
890 | ||
891 | subsys_initcall(fib_rules_init); |