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