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