]>
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 <[email protected]> | |
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> |
e9dc8653 | 15 | #include <net/net_namespace.h> |
881d966b | 16 | #include <net/sock.h> |
14c0b97d TG |
17 | #include <net/fib_rules.h> |
18 | ||
2994c638 DL |
19 | int fib_default_rule_add(struct fib_rules_ops *ops, |
20 | u32 pref, u32 table, u32 flags) | |
21 | { | |
22 | struct fib_rule *r; | |
23 | ||
24 | r = kzalloc(ops->rule_size, GFP_KERNEL); | |
25 | if (r == NULL) | |
26 | return -ENOMEM; | |
27 | ||
28 | atomic_set(&r->refcnt, 1); | |
29 | r->action = FR_ACT_TO_TBL; | |
30 | r->pref = pref; | |
31 | r->table = table; | |
32 | r->flags = flags; | |
3661a910 | 33 | r->fr_net = hold_net(ops->fro_net); |
2994c638 DL |
34 | |
35 | /* The lock is not required here, the list in unreacheable | |
36 | * at the moment this function is called */ | |
37 | list_add_tail(&r->list, &ops->rules_list); | |
38 | return 0; | |
39 | } | |
40 | EXPORT_SYMBOL(fib_default_rule_add); | |
41 | ||
d8a566be PM |
42 | u32 fib_default_rule_pref(struct fib_rules_ops *ops) |
43 | { | |
44 | struct list_head *pos; | |
45 | struct fib_rule *rule; | |
46 | ||
47 | if (!list_empty(&ops->rules_list)) { | |
48 | pos = ops->rules_list.next; | |
49 | if (pos->next != &ops->rules_list) { | |
50 | rule = list_entry(pos->next, struct fib_rule, list); | |
51 | if (rule->pref) | |
52 | return rule->pref - 1; | |
53 | } | |
54 | } | |
55 | ||
56 | return 0; | |
57 | } | |
58 | EXPORT_SYMBOL(fib_default_rule_pref); | |
59 | ||
9e3a5487 | 60 | static void notify_rule_change(int event, struct fib_rule *rule, |
c17084d2 TG |
61 | struct fib_rules_ops *ops, struct nlmsghdr *nlh, |
62 | u32 pid); | |
14c0b97d | 63 | |
5fd30ee7 | 64 | static struct fib_rules_ops *lookup_rules_ops(struct net *net, int family) |
14c0b97d TG |
65 | { |
66 | struct fib_rules_ops *ops; | |
67 | ||
68 | rcu_read_lock(); | |
5fd30ee7 | 69 | list_for_each_entry_rcu(ops, &net->rules_ops, list) { |
14c0b97d TG |
70 | if (ops->family == family) { |
71 | if (!try_module_get(ops->owner)) | |
72 | ops = NULL; | |
73 | rcu_read_unlock(); | |
74 | return ops; | |
75 | } | |
76 | } | |
77 | rcu_read_unlock(); | |
78 | ||
79 | return NULL; | |
80 | } | |
81 | ||
82 | static void rules_ops_put(struct fib_rules_ops *ops) | |
83 | { | |
84 | if (ops) | |
85 | module_put(ops->owner); | |
86 | } | |
87 | ||
73417f61 TG |
88 | static void flush_route_cache(struct fib_rules_ops *ops) |
89 | { | |
90 | if (ops->flush_cache) | |
ae299fc0 | 91 | ops->flush_cache(ops); |
73417f61 TG |
92 | } |
93 | ||
e9c5158a | 94 | static int __fib_rules_register(struct fib_rules_ops *ops) |
14c0b97d TG |
95 | { |
96 | int err = -EEXIST; | |
97 | struct fib_rules_ops *o; | |
9e3a5487 DL |
98 | struct net *net; |
99 | ||
100 | net = ops->fro_net; | |
14c0b97d TG |
101 | |
102 | if (ops->rule_size < sizeof(struct fib_rule)) | |
103 | return -EINVAL; | |
104 | ||
105 | if (ops->match == NULL || ops->configure == NULL || | |
106 | ops->compare == NULL || ops->fill == NULL || | |
107 | ops->action == NULL) | |
108 | return -EINVAL; | |
109 | ||
5fd30ee7 DL |
110 | spin_lock(&net->rules_mod_lock); |
111 | list_for_each_entry(o, &net->rules_ops, list) | |
14c0b97d TG |
112 | if (ops->family == o->family) |
113 | goto errout; | |
114 | ||
5fd30ee7 DL |
115 | hold_net(net); |
116 | list_add_tail_rcu(&ops->list, &net->rules_ops); | |
14c0b97d TG |
117 | err = 0; |
118 | errout: | |
5fd30ee7 | 119 | spin_unlock(&net->rules_mod_lock); |
14c0b97d TG |
120 | |
121 | return err; | |
122 | } | |
123 | ||
e9c5158a | 124 | struct fib_rules_ops * |
3d0c9c4e | 125 | fib_rules_register(const struct fib_rules_ops *tmpl, struct net *net) |
e9c5158a EB |
126 | { |
127 | struct fib_rules_ops *ops; | |
128 | int err; | |
129 | ||
2fb3573d | 130 | ops = kmemdup(tmpl, sizeof(*ops), GFP_KERNEL); |
e9c5158a EB |
131 | if (ops == NULL) |
132 | return ERR_PTR(-ENOMEM); | |
133 | ||
134 | INIT_LIST_HEAD(&ops->rules_list); | |
135 | ops->fro_net = net; | |
136 | ||
137 | err = __fib_rules_register(ops); | |
138 | if (err) { | |
139 | kfree(ops); | |
140 | ops = ERR_PTR(err); | |
141 | } | |
142 | ||
143 | return ops; | |
144 | } | |
14c0b97d TG |
145 | EXPORT_SYMBOL_GPL(fib_rules_register); |
146 | ||
9eb87f3f | 147 | void fib_rules_cleanup_ops(struct fib_rules_ops *ops) |
14c0b97d TG |
148 | { |
149 | struct fib_rule *rule, *tmp; | |
150 | ||
76c72d4f | 151 | list_for_each_entry_safe(rule, tmp, &ops->rules_list, list) { |
14c0b97d TG |
152 | list_del_rcu(&rule->list); |
153 | fib_rule_put(rule); | |
154 | } | |
155 | } | |
9eb87f3f | 156 | EXPORT_SYMBOL_GPL(fib_rules_cleanup_ops); |
14c0b97d | 157 | |
e9c5158a EB |
158 | static void fib_rules_put_rcu(struct rcu_head *head) |
159 | { | |
160 | struct fib_rules_ops *ops = container_of(head, struct fib_rules_ops, rcu); | |
161 | struct net *net = ops->fro_net; | |
162 | ||
163 | release_net(net); | |
164 | kfree(ops); | |
165 | } | |
166 | ||
9e3a5487 | 167 | void fib_rules_unregister(struct fib_rules_ops *ops) |
14c0b97d | 168 | { |
9e3a5487 | 169 | struct net *net = ops->fro_net; |
14c0b97d | 170 | |
5fd30ee7 | 171 | spin_lock(&net->rules_mod_lock); |
72132c1b DL |
172 | list_del_rcu(&ops->list); |
173 | fib_rules_cleanup_ops(ops); | |
5fd30ee7 | 174 | spin_unlock(&net->rules_mod_lock); |
14c0b97d | 175 | |
e9c5158a | 176 | call_rcu(&ops->rcu, fib_rules_put_rcu); |
14c0b97d | 177 | } |
14c0b97d TG |
178 | EXPORT_SYMBOL_GPL(fib_rules_unregister); |
179 | ||
3dfbcc41 TG |
180 | static int fib_rule_match(struct fib_rule *rule, struct fib_rules_ops *ops, |
181 | struct flowi *fl, int flags) | |
182 | { | |
183 | int ret = 0; | |
184 | ||
491deb24 | 185 | if (rule->iifindex && (rule->iifindex != fl->iif)) |
3dfbcc41 TG |
186 | goto out; |
187 | ||
1b038a5e PM |
188 | if (rule->oifindex && (rule->oifindex != fl->oif)) |
189 | goto out; | |
190 | ||
3dfbcc41 TG |
191 | if ((rule->mark ^ fl->mark) & rule->mark_mask) |
192 | goto out; | |
193 | ||
194 | ret = ops->match(rule, fl, flags); | |
195 | out: | |
196 | return (rule->flags & FIB_RULE_INVERT) ? !ret : ret; | |
197 | } | |
198 | ||
14c0b97d TG |
199 | int fib_rules_lookup(struct fib_rules_ops *ops, struct flowi *fl, |
200 | int flags, struct fib_lookup_arg *arg) | |
201 | { | |
202 | struct fib_rule *rule; | |
203 | int err; | |
204 | ||
205 | rcu_read_lock(); | |
206 | ||
76c72d4f | 207 | list_for_each_entry_rcu(rule, &ops->rules_list, list) { |
0947c9fe | 208 | jumped: |
3dfbcc41 | 209 | if (!fib_rule_match(rule, ops, fl, flags)) |
14c0b97d TG |
210 | continue; |
211 | ||
0947c9fe TG |
212 | if (rule->action == FR_ACT_GOTO) { |
213 | struct fib_rule *target; | |
214 | ||
215 | target = rcu_dereference(rule->ctarget); | |
216 | if (target == NULL) { | |
217 | continue; | |
218 | } else { | |
219 | rule = target; | |
220 | goto jumped; | |
221 | } | |
fa0b2d1d TG |
222 | } else if (rule->action == FR_ACT_NOP) |
223 | continue; | |
224 | else | |
0947c9fe TG |
225 | err = ops->action(rule, fl, flags, arg); |
226 | ||
14c0b97d TG |
227 | if (err != -EAGAIN) { |
228 | fib_rule_get(rule); | |
229 | arg->rule = rule; | |
230 | goto out; | |
231 | } | |
232 | } | |
233 | ||
83886b6b | 234 | err = -ESRCH; |
14c0b97d TG |
235 | out: |
236 | rcu_read_unlock(); | |
237 | ||
238 | return err; | |
239 | } | |
14c0b97d TG |
240 | EXPORT_SYMBOL_GPL(fib_rules_lookup); |
241 | ||
e1701c68 TG |
242 | static int validate_rulemsg(struct fib_rule_hdr *frh, struct nlattr **tb, |
243 | struct fib_rules_ops *ops) | |
244 | { | |
245 | int err = -EINVAL; | |
246 | ||
247 | if (frh->src_len) | |
248 | if (tb[FRA_SRC] == NULL || | |
249 | frh->src_len > (ops->addr_size * 8) || | |
250 | nla_len(tb[FRA_SRC]) != ops->addr_size) | |
251 | goto errout; | |
252 | ||
253 | if (frh->dst_len) | |
254 | if (tb[FRA_DST] == NULL || | |
255 | frh->dst_len > (ops->addr_size * 8) || | |
256 | nla_len(tb[FRA_DST]) != ops->addr_size) | |
257 | goto errout; | |
258 | ||
259 | err = 0; | |
260 | errout: | |
261 | return err; | |
262 | } | |
263 | ||
9d9e6a58 | 264 | static int fib_nl_newrule(struct sk_buff *skb, struct nlmsghdr* nlh, void *arg) |
14c0b97d | 265 | { |
3b1e0a65 | 266 | struct net *net = sock_net(skb->sk); |
14c0b97d TG |
267 | struct fib_rule_hdr *frh = nlmsg_data(nlh); |
268 | struct fib_rules_ops *ops = NULL; | |
269 | struct fib_rule *rule, *r, *last = NULL; | |
270 | struct nlattr *tb[FRA_MAX+1]; | |
0947c9fe | 271 | int err = -EINVAL, unresolved = 0; |
14c0b97d TG |
272 | |
273 | if (nlh->nlmsg_len < nlmsg_msg_size(sizeof(*frh))) | |
274 | goto errout; | |
275 | ||
5fd30ee7 | 276 | ops = lookup_rules_ops(net, frh->family); |
14c0b97d | 277 | if (ops == NULL) { |
2fe195cf | 278 | err = -EAFNOSUPPORT; |
14c0b97d TG |
279 | goto errout; |
280 | } | |
281 | ||
282 | err = nlmsg_parse(nlh, sizeof(*frh), tb, FRA_MAX, ops->policy); | |
283 | if (err < 0) | |
284 | goto errout; | |
285 | ||
e1701c68 TG |
286 | err = validate_rulemsg(frh, tb, ops); |
287 | if (err < 0) | |
288 | goto errout; | |
289 | ||
14c0b97d TG |
290 | rule = kzalloc(ops->rule_size, GFP_KERNEL); |
291 | if (rule == NULL) { | |
292 | err = -ENOMEM; | |
293 | goto errout; | |
294 | } | |
3661a910 | 295 | rule->fr_net = hold_net(net); |
14c0b97d TG |
296 | |
297 | if (tb[FRA_PRIORITY]) | |
298 | rule->pref = nla_get_u32(tb[FRA_PRIORITY]); | |
299 | ||
491deb24 | 300 | if (tb[FRA_IIFNAME]) { |
14c0b97d TG |
301 | struct net_device *dev; |
302 | ||
491deb24 PM |
303 | rule->iifindex = -1; |
304 | nla_strlcpy(rule->iifname, tb[FRA_IIFNAME], IFNAMSIZ); | |
305 | dev = __dev_get_by_name(net, rule->iifname); | |
14c0b97d | 306 | if (dev) |
491deb24 | 307 | rule->iifindex = dev->ifindex; |
14c0b97d TG |
308 | } |
309 | ||
1b038a5e PM |
310 | if (tb[FRA_OIFNAME]) { |
311 | struct net_device *dev; | |
312 | ||
313 | rule->oifindex = -1; | |
314 | nla_strlcpy(rule->oifname, tb[FRA_OIFNAME], IFNAMSIZ); | |
315 | dev = __dev_get_by_name(net, rule->oifname); | |
316 | if (dev) | |
317 | rule->oifindex = dev->ifindex; | |
318 | } | |
319 | ||
b8964ed9 TG |
320 | if (tb[FRA_FWMARK]) { |
321 | rule->mark = nla_get_u32(tb[FRA_FWMARK]); | |
322 | if (rule->mark) | |
323 | /* compatibility: if the mark value is non-zero all bits | |
324 | * are compared unless a mask is explicitly specified. | |
325 | */ | |
326 | rule->mark_mask = 0xFFFFFFFF; | |
327 | } | |
328 | ||
329 | if (tb[FRA_FWMASK]) | |
330 | rule->mark_mask = nla_get_u32(tb[FRA_FWMASK]); | |
331 | ||
14c0b97d TG |
332 | rule->action = frh->action; |
333 | rule->flags = frh->flags; | |
9e762a4a | 334 | rule->table = frh_get_table(frh, tb); |
14c0b97d | 335 | |
5adef180 | 336 | if (!tb[FRA_PRIORITY] && ops->default_pref) |
868d13ac | 337 | rule->pref = ops->default_pref(ops); |
14c0b97d | 338 | |
0947c9fe TG |
339 | err = -EINVAL; |
340 | if (tb[FRA_GOTO]) { | |
341 | if (rule->action != FR_ACT_GOTO) | |
342 | goto errout_free; | |
343 | ||
344 | rule->target = nla_get_u32(tb[FRA_GOTO]); | |
345 | /* Backward jumps are prohibited to avoid endless loops */ | |
346 | if (rule->target <= rule->pref) | |
347 | goto errout_free; | |
348 | ||
76c72d4f | 349 | list_for_each_entry(r, &ops->rules_list, list) { |
0947c9fe TG |
350 | if (r->pref == rule->target) { |
351 | rule->ctarget = r; | |
352 | break; | |
353 | } | |
354 | } | |
355 | ||
356 | if (rule->ctarget == NULL) | |
357 | unresolved = 1; | |
358 | } else if (rule->action == FR_ACT_GOTO) | |
359 | goto errout_free; | |
360 | ||
8b3521ee | 361 | err = ops->configure(rule, skb, frh, tb); |
14c0b97d TG |
362 | if (err < 0) |
363 | goto errout_free; | |
364 | ||
76c72d4f | 365 | list_for_each_entry(r, &ops->rules_list, list) { |
14c0b97d TG |
366 | if (r->pref > rule->pref) |
367 | break; | |
368 | last = r; | |
369 | } | |
370 | ||
371 | fib_rule_get(rule); | |
372 | ||
0947c9fe TG |
373 | if (ops->unresolved_rules) { |
374 | /* | |
375 | * There are unresolved goto rules in the list, check if | |
376 | * any of them are pointing to this new rule. | |
377 | */ | |
76c72d4f | 378 | list_for_each_entry(r, &ops->rules_list, list) { |
0947c9fe TG |
379 | if (r->action == FR_ACT_GOTO && |
380 | r->target == rule->pref) { | |
381 | BUG_ON(r->ctarget != NULL); | |
382 | rcu_assign_pointer(r->ctarget, rule); | |
383 | if (--ops->unresolved_rules == 0) | |
384 | break; | |
385 | } | |
386 | } | |
387 | } | |
388 | ||
389 | if (rule->action == FR_ACT_GOTO) | |
390 | ops->nr_goto_rules++; | |
391 | ||
392 | if (unresolved) | |
393 | ops->unresolved_rules++; | |
394 | ||
14c0b97d TG |
395 | if (last) |
396 | list_add_rcu(&rule->list, &last->list); | |
397 | else | |
76c72d4f | 398 | list_add_rcu(&rule->list, &ops->rules_list); |
14c0b97d | 399 | |
9e3a5487 | 400 | notify_rule_change(RTM_NEWRULE, rule, ops, nlh, NETLINK_CB(skb).pid); |
73417f61 | 401 | flush_route_cache(ops); |
14c0b97d TG |
402 | rules_ops_put(ops); |
403 | return 0; | |
404 | ||
405 | errout_free: | |
3661a910 | 406 | release_net(rule->fr_net); |
14c0b97d TG |
407 | kfree(rule); |
408 | errout: | |
409 | rules_ops_put(ops); | |
410 | return err; | |
411 | } | |
412 | ||
9d9e6a58 | 413 | static int fib_nl_delrule(struct sk_buff *skb, struct nlmsghdr* nlh, void *arg) |
14c0b97d | 414 | { |
3b1e0a65 | 415 | struct net *net = sock_net(skb->sk); |
14c0b97d TG |
416 | struct fib_rule_hdr *frh = nlmsg_data(nlh); |
417 | struct fib_rules_ops *ops = NULL; | |
0947c9fe | 418 | struct fib_rule *rule, *tmp; |
14c0b97d TG |
419 | struct nlattr *tb[FRA_MAX+1]; |
420 | int err = -EINVAL; | |
421 | ||
422 | if (nlh->nlmsg_len < nlmsg_msg_size(sizeof(*frh))) | |
423 | goto errout; | |
424 | ||
5fd30ee7 | 425 | ops = lookup_rules_ops(net, frh->family); |
14c0b97d | 426 | if (ops == NULL) { |
2fe195cf | 427 | err = -EAFNOSUPPORT; |
14c0b97d TG |
428 | goto errout; |
429 | } | |
430 | ||
431 | err = nlmsg_parse(nlh, sizeof(*frh), tb, FRA_MAX, ops->policy); | |
432 | if (err < 0) | |
433 | goto errout; | |
434 | ||
e1701c68 TG |
435 | err = validate_rulemsg(frh, tb, ops); |
436 | if (err < 0) | |
437 | goto errout; | |
438 | ||
76c72d4f | 439 | list_for_each_entry(rule, &ops->rules_list, list) { |
14c0b97d TG |
440 | if (frh->action && (frh->action != rule->action)) |
441 | continue; | |
442 | ||
9e762a4a | 443 | if (frh->table && (frh_get_table(frh, tb) != rule->table)) |
14c0b97d TG |
444 | continue; |
445 | ||
446 | if (tb[FRA_PRIORITY] && | |
447 | (rule->pref != nla_get_u32(tb[FRA_PRIORITY]))) | |
448 | continue; | |
449 | ||
491deb24 PM |
450 | if (tb[FRA_IIFNAME] && |
451 | nla_strcmp(tb[FRA_IIFNAME], rule->iifname)) | |
14c0b97d TG |
452 | continue; |
453 | ||
1b038a5e PM |
454 | if (tb[FRA_OIFNAME] && |
455 | nla_strcmp(tb[FRA_OIFNAME], rule->oifname)) | |
456 | continue; | |
457 | ||
b8964ed9 TG |
458 | if (tb[FRA_FWMARK] && |
459 | (rule->mark != nla_get_u32(tb[FRA_FWMARK]))) | |
460 | continue; | |
461 | ||
462 | if (tb[FRA_FWMASK] && | |
463 | (rule->mark_mask != nla_get_u32(tb[FRA_FWMASK]))) | |
464 | continue; | |
465 | ||
14c0b97d TG |
466 | if (!ops->compare(rule, frh, tb)) |
467 | continue; | |
468 | ||
469 | if (rule->flags & FIB_RULE_PERMANENT) { | |
470 | err = -EPERM; | |
471 | goto errout; | |
472 | } | |
473 | ||
474 | list_del_rcu(&rule->list); | |
0947c9fe TG |
475 | |
476 | if (rule->action == FR_ACT_GOTO) | |
477 | ops->nr_goto_rules--; | |
478 | ||
479 | /* | |
480 | * Check if this rule is a target to any of them. If so, | |
481 | * disable them. As this operation is eventually very | |
482 | * expensive, it is only performed if goto rules have | |
483 | * actually been added. | |
484 | */ | |
485 | if (ops->nr_goto_rules > 0) { | |
76c72d4f | 486 | list_for_each_entry(tmp, &ops->rules_list, list) { |
0947c9fe TG |
487 | if (tmp->ctarget == rule) { |
488 | rcu_assign_pointer(tmp->ctarget, NULL); | |
489 | ops->unresolved_rules++; | |
490 | } | |
491 | } | |
492 | } | |
493 | ||
14c0b97d | 494 | synchronize_rcu(); |
9e3a5487 | 495 | notify_rule_change(RTM_DELRULE, rule, ops, nlh, |
c17084d2 | 496 | NETLINK_CB(skb).pid); |
14c0b97d | 497 | fib_rule_put(rule); |
73417f61 | 498 | flush_route_cache(ops); |
14c0b97d TG |
499 | rules_ops_put(ops); |
500 | return 0; | |
501 | } | |
502 | ||
503 | err = -ENOENT; | |
504 | errout: | |
505 | rules_ops_put(ops); | |
506 | return err; | |
507 | } | |
508 | ||
339bf98f TG |
509 | static inline size_t fib_rule_nlmsg_size(struct fib_rules_ops *ops, |
510 | struct fib_rule *rule) | |
511 | { | |
512 | size_t payload = NLMSG_ALIGN(sizeof(struct fib_rule_hdr)) | |
491deb24 | 513 | + nla_total_size(IFNAMSIZ) /* FRA_IIFNAME */ |
1b038a5e | 514 | + nla_total_size(IFNAMSIZ) /* FRA_OIFNAME */ |
339bf98f TG |
515 | + nla_total_size(4) /* FRA_PRIORITY */ |
516 | + nla_total_size(4) /* FRA_TABLE */ | |
517 | + nla_total_size(4) /* FRA_FWMARK */ | |
518 | + nla_total_size(4); /* FRA_FWMASK */ | |
519 | ||
520 | if (ops->nlmsg_payload) | |
521 | payload += ops->nlmsg_payload(rule); | |
522 | ||
523 | return payload; | |
524 | } | |
525 | ||
14c0b97d TG |
526 | static int fib_nl_fill_rule(struct sk_buff *skb, struct fib_rule *rule, |
527 | u32 pid, u32 seq, int type, int flags, | |
528 | struct fib_rules_ops *ops) | |
529 | { | |
530 | struct nlmsghdr *nlh; | |
531 | struct fib_rule_hdr *frh; | |
532 | ||
533 | nlh = nlmsg_put(skb, pid, seq, type, sizeof(*frh), flags); | |
534 | if (nlh == NULL) | |
26932566 | 535 | return -EMSGSIZE; |
14c0b97d TG |
536 | |
537 | frh = nlmsg_data(nlh); | |
28bb1726 | 538 | frh->family = ops->family; |
14c0b97d | 539 | frh->table = rule->table; |
9e762a4a | 540 | NLA_PUT_U32(skb, FRA_TABLE, rule->table); |
14c0b97d TG |
541 | frh->res1 = 0; |
542 | frh->res2 = 0; | |
543 | frh->action = rule->action; | |
544 | frh->flags = rule->flags; | |
545 | ||
0947c9fe TG |
546 | if (rule->action == FR_ACT_GOTO && rule->ctarget == NULL) |
547 | frh->flags |= FIB_RULE_UNRESOLVED; | |
548 | ||
491deb24 PM |
549 | if (rule->iifname[0]) { |
550 | NLA_PUT_STRING(skb, FRA_IIFNAME, rule->iifname); | |
14c0b97d | 551 | |
491deb24 PM |
552 | if (rule->iifindex == -1) |
553 | frh->flags |= FIB_RULE_IIF_DETACHED; | |
2b443683 TG |
554 | } |
555 | ||
1b038a5e PM |
556 | if (rule->oifname[0]) { |
557 | NLA_PUT_STRING(skb, FRA_OIFNAME, rule->oifname); | |
558 | ||
559 | if (rule->oifindex == -1) | |
560 | frh->flags |= FIB_RULE_OIF_DETACHED; | |
561 | } | |
562 | ||
14c0b97d TG |
563 | if (rule->pref) |
564 | NLA_PUT_U32(skb, FRA_PRIORITY, rule->pref); | |
565 | ||
b8964ed9 TG |
566 | if (rule->mark) |
567 | NLA_PUT_U32(skb, FRA_FWMARK, rule->mark); | |
568 | ||
569 | if (rule->mark_mask || rule->mark) | |
570 | NLA_PUT_U32(skb, FRA_FWMASK, rule->mark_mask); | |
571 | ||
0947c9fe TG |
572 | if (rule->target) |
573 | NLA_PUT_U32(skb, FRA_GOTO, rule->target); | |
574 | ||
04af8cf6 | 575 | if (ops->fill(rule, skb, frh) < 0) |
14c0b97d TG |
576 | goto nla_put_failure; |
577 | ||
578 | return nlmsg_end(skb, nlh); | |
579 | ||
580 | nla_put_failure: | |
26932566 PM |
581 | nlmsg_cancel(skb, nlh); |
582 | return -EMSGSIZE; | |
14c0b97d TG |
583 | } |
584 | ||
c454673d TG |
585 | static int dump_rules(struct sk_buff *skb, struct netlink_callback *cb, |
586 | struct fib_rules_ops *ops) | |
14c0b97d TG |
587 | { |
588 | int idx = 0; | |
589 | struct fib_rule *rule; | |
14c0b97d | 590 | |
76c72d4f | 591 | list_for_each_entry(rule, &ops->rules_list, list) { |
c454673d | 592 | if (idx < cb->args[1]) |
14c0b97d TG |
593 | goto skip; |
594 | ||
595 | if (fib_nl_fill_rule(skb, rule, NETLINK_CB(cb->skb).pid, | |
596 | cb->nlh->nlmsg_seq, RTM_NEWRULE, | |
597 | NLM_F_MULTI, ops) < 0) | |
598 | break; | |
599 | skip: | |
600 | idx++; | |
601 | } | |
c454673d | 602 | cb->args[1] = idx; |
14c0b97d TG |
603 | rules_ops_put(ops); |
604 | ||
605 | return skb->len; | |
606 | } | |
607 | ||
c454673d TG |
608 | static int fib_nl_dumprule(struct sk_buff *skb, struct netlink_callback *cb) |
609 | { | |
3b1e0a65 | 610 | struct net *net = sock_net(skb->sk); |
c454673d TG |
611 | struct fib_rules_ops *ops; |
612 | int idx = 0, family; | |
613 | ||
614 | family = rtnl_msg_family(cb->nlh); | |
615 | if (family != AF_UNSPEC) { | |
616 | /* Protocol specific dump request */ | |
5fd30ee7 | 617 | ops = lookup_rules_ops(net, family); |
c454673d TG |
618 | if (ops == NULL) |
619 | return -EAFNOSUPPORT; | |
620 | ||
621 | return dump_rules(skb, cb, ops); | |
622 | } | |
623 | ||
624 | rcu_read_lock(); | |
5fd30ee7 | 625 | list_for_each_entry_rcu(ops, &net->rules_ops, list) { |
c454673d TG |
626 | if (idx < cb->args[0] || !try_module_get(ops->owner)) |
627 | goto skip; | |
628 | ||
629 | if (dump_rules(skb, cb, ops) < 0) | |
630 | break; | |
631 | ||
632 | cb->args[1] = 0; | |
2fb3573d | 633 | skip: |
c454673d TG |
634 | idx++; |
635 | } | |
636 | rcu_read_unlock(); | |
637 | cb->args[0] = idx; | |
638 | ||
639 | return skb->len; | |
640 | } | |
14c0b97d | 641 | |
9e3a5487 | 642 | static void notify_rule_change(int event, struct fib_rule *rule, |
c17084d2 TG |
643 | struct fib_rules_ops *ops, struct nlmsghdr *nlh, |
644 | u32 pid) | |
14c0b97d | 645 | { |
9e3a5487 | 646 | struct net *net; |
c17084d2 TG |
647 | struct sk_buff *skb; |
648 | int err = -ENOBUFS; | |
14c0b97d | 649 | |
9e3a5487 | 650 | net = ops->fro_net; |
339bf98f | 651 | skb = nlmsg_new(fib_rule_nlmsg_size(ops, rule), GFP_KERNEL); |
14c0b97d | 652 | if (skb == NULL) |
c17084d2 TG |
653 | goto errout; |
654 | ||
655 | err = fib_nl_fill_rule(skb, rule, pid, nlh->nlmsg_seq, event, 0, ops); | |
26932566 PM |
656 | if (err < 0) { |
657 | /* -EMSGSIZE implies BUG in fib_rule_nlmsg_size() */ | |
658 | WARN_ON(err == -EMSGSIZE); | |
659 | kfree_skb(skb); | |
660 | goto errout; | |
661 | } | |
9e3a5487 | 662 | |
1ce85fe4 PNA |
663 | rtnl_notify(skb, net, pid, ops->nlgroup, nlh, GFP_KERNEL); |
664 | return; | |
c17084d2 TG |
665 | errout: |
666 | if (err < 0) | |
5fd30ee7 | 667 | rtnl_set_sk_err(net, ops->nlgroup, err); |
14c0b97d TG |
668 | } |
669 | ||
670 | static void attach_rules(struct list_head *rules, struct net_device *dev) | |
671 | { | |
672 | struct fib_rule *rule; | |
673 | ||
674 | list_for_each_entry(rule, rules, list) { | |
491deb24 PM |
675 | if (rule->iifindex == -1 && |
676 | strcmp(dev->name, rule->iifname) == 0) | |
677 | rule->iifindex = dev->ifindex; | |
1b038a5e PM |
678 | if (rule->oifindex == -1 && |
679 | strcmp(dev->name, rule->oifname) == 0) | |
680 | rule->oifindex = dev->ifindex; | |
14c0b97d TG |
681 | } |
682 | } | |
683 | ||
684 | static void detach_rules(struct list_head *rules, struct net_device *dev) | |
685 | { | |
686 | struct fib_rule *rule; | |
687 | ||
1b038a5e | 688 | list_for_each_entry(rule, rules, list) { |
491deb24 PM |
689 | if (rule->iifindex == dev->ifindex) |
690 | rule->iifindex = -1; | |
1b038a5e PM |
691 | if (rule->oifindex == dev->ifindex) |
692 | rule->oifindex = -1; | |
693 | } | |
14c0b97d TG |
694 | } |
695 | ||
696 | ||
697 | static int fib_rules_event(struct notifier_block *this, unsigned long event, | |
698 | void *ptr) | |
699 | { | |
700 | struct net_device *dev = ptr; | |
c346dca1 | 701 | struct net *net = dev_net(dev); |
14c0b97d TG |
702 | struct fib_rules_ops *ops; |
703 | ||
704 | ASSERT_RTNL(); | |
14c0b97d TG |
705 | |
706 | switch (event) { | |
707 | case NETDEV_REGISTER: | |
5fd30ee7 | 708 | list_for_each_entry(ops, &net->rules_ops, list) |
76c72d4f | 709 | attach_rules(&ops->rules_list, dev); |
14c0b97d TG |
710 | break; |
711 | ||
712 | case NETDEV_UNREGISTER: | |
5fd30ee7 | 713 | list_for_each_entry(ops, &net->rules_ops, list) |
76c72d4f | 714 | detach_rules(&ops->rules_list, dev); |
14c0b97d TG |
715 | break; |
716 | } | |
717 | ||
14c0b97d TG |
718 | return NOTIFY_DONE; |
719 | } | |
720 | ||
721 | static struct notifier_block fib_rules_notifier = { | |
722 | .notifier_call = fib_rules_event, | |
723 | }; | |
724 | ||
2c8c1e72 | 725 | static int __net_init fib_rules_net_init(struct net *net) |
5fd30ee7 DL |
726 | { |
727 | INIT_LIST_HEAD(&net->rules_ops); | |
728 | spin_lock_init(&net->rules_mod_lock); | |
729 | return 0; | |
730 | } | |
731 | ||
732 | static struct pernet_operations fib_rules_net_ops = { | |
733 | .init = fib_rules_net_init, | |
734 | }; | |
735 | ||
14c0b97d TG |
736 | static int __init fib_rules_init(void) |
737 | { | |
5fd30ee7 | 738 | int err; |
9d9e6a58 TG |
739 | rtnl_register(PF_UNSPEC, RTM_NEWRULE, fib_nl_newrule, NULL); |
740 | rtnl_register(PF_UNSPEC, RTM_DELRULE, fib_nl_delrule, NULL); | |
c454673d | 741 | rtnl_register(PF_UNSPEC, RTM_GETRULE, NULL, fib_nl_dumprule); |
9d9e6a58 | 742 | |
5d6d4809 | 743 | err = register_pernet_subsys(&fib_rules_net_ops); |
5fd30ee7 DL |
744 | if (err < 0) |
745 | goto fail; | |
746 | ||
5d6d4809 | 747 | err = register_netdevice_notifier(&fib_rules_notifier); |
5fd30ee7 DL |
748 | if (err < 0) |
749 | goto fail_unregister; | |
5d6d4809 | 750 | |
5fd30ee7 DL |
751 | return 0; |
752 | ||
753 | fail_unregister: | |
5d6d4809 | 754 | unregister_pernet_subsys(&fib_rules_net_ops); |
5fd30ee7 DL |
755 | fail: |
756 | rtnl_unregister(PF_UNSPEC, RTM_NEWRULE); | |
757 | rtnl_unregister(PF_UNSPEC, RTM_DELRULE); | |
758 | rtnl_unregister(PF_UNSPEC, RTM_GETRULE); | |
759 | return err; | |
14c0b97d TG |
760 | } |
761 | ||
762 | subsys_initcall(fib_rules_init); |