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