blob: 9a24377146bf0dc25163e96bfb3c9b6c0701ab40 [file] [log] [blame]
Thomas Graf14c0b972006-08-04 03:38:38 -07001/*
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
Thomas Graf14c0b972006-08-04 03:38:38 -070011#include <linux/types.h>
12#include <linux/kernel.h>
13#include <linux/list.h>
Eric W. Biedermane9dc8652007-09-12 13:02:17 +020014#include <net/net_namespace.h>
Eric W. Biederman881d9662007-09-17 11:56:21 -070015#include <net/sock.h>
Thomas Graf14c0b972006-08-04 03:38:38 -070016#include <net/fib_rules.h>
17
Denis V. Lunev2994c632007-11-10 22:12:03 -080018int 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;
Denis V. Lunev3661a912008-04-16 02:01:56 -070032 r->fr_net = hold_net(ops->fro_net);
Denis V. Lunev2994c632007-11-10 22:12:03 -080033
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}
39EXPORT_SYMBOL(fib_default_rule_add);
40
Denis V. Lunev9e3a5482008-01-20 16:46:41 -080041static void notify_rule_change(int event, struct fib_rule *rule,
Thomas Grafc17084d2006-08-15 00:32:48 -070042 struct fib_rules_ops *ops, struct nlmsghdr *nlh,
43 u32 pid);
Thomas Graf14c0b972006-08-04 03:38:38 -070044
Denis V. Lunev5fd30ee2008-01-10 03:20:28 -080045static struct fib_rules_ops *lookup_rules_ops(struct net *net, int family)
Thomas Graf14c0b972006-08-04 03:38:38 -070046{
47 struct fib_rules_ops *ops;
48
49 rcu_read_lock();
Denis V. Lunev5fd30ee2008-01-10 03:20:28 -080050 list_for_each_entry_rcu(ops, &net->rules_ops, list) {
Thomas Graf14c0b972006-08-04 03:38:38 -070051 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
63static void rules_ops_put(struct fib_rules_ops *ops)
64{
65 if (ops)
66 module_put(ops->owner);
67}
68
Thomas Graf73417f62007-03-27 13:56:52 -070069static void flush_route_cache(struct fib_rules_ops *ops)
70{
71 if (ops->flush_cache)
Denis V. Lunevae299fc2008-07-05 19:01:28 -070072 ops->flush_cache(ops);
Thomas Graf73417f62007-03-27 13:56:52 -070073}
74
Eric W. Biedermane9c51582009-12-03 12:22:55 -080075static int __fib_rules_register(struct fib_rules_ops *ops)
Thomas Graf14c0b972006-08-04 03:38:38 -070076{
77 int err = -EEXIST;
78 struct fib_rules_ops *o;
Denis V. Lunev9e3a5482008-01-20 16:46:41 -080079 struct net *net;
80
81 net = ops->fro_net;
Thomas Graf14c0b972006-08-04 03:38:38 -070082
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
Denis V. Lunev5fd30ee2008-01-10 03:20:28 -080091 spin_lock(&net->rules_mod_lock);
92 list_for_each_entry(o, &net->rules_ops, list)
Thomas Graf14c0b972006-08-04 03:38:38 -070093 if (ops->family == o->family)
94 goto errout;
95
Denis V. Lunev5fd30ee2008-01-10 03:20:28 -080096 hold_net(net);
97 list_add_tail_rcu(&ops->list, &net->rules_ops);
Thomas Graf14c0b972006-08-04 03:38:38 -070098 err = 0;
99errout:
Denis V. Lunev5fd30ee2008-01-10 03:20:28 -0800100 spin_unlock(&net->rules_mod_lock);
Thomas Graf14c0b972006-08-04 03:38:38 -0700101
102 return err;
103}
104
Eric W. Biedermane9c51582009-12-03 12:22:55 -0800105struct fib_rules_ops *
106fib_rules_register(struct fib_rules_ops *tmpl, struct net *net)
107{
108 struct fib_rules_ops *ops;
109 int err;
110
111 ops = kmemdup(tmpl, sizeof (*ops), GFP_KERNEL);
112 if (ops == NULL)
113 return ERR_PTR(-ENOMEM);
114
115 INIT_LIST_HEAD(&ops->rules_list);
116 ops->fro_net = net;
117
118 err = __fib_rules_register(ops);
119 if (err) {
120 kfree(ops);
121 ops = ERR_PTR(err);
122 }
123
124 return ops;
125}
126
Thomas Graf14c0b972006-08-04 03:38:38 -0700127EXPORT_SYMBOL_GPL(fib_rules_register);
128
Daniel Lezcano9eb87f32007-12-07 00:42:52 -0800129void fib_rules_cleanup_ops(struct fib_rules_ops *ops)
Thomas Graf14c0b972006-08-04 03:38:38 -0700130{
131 struct fib_rule *rule, *tmp;
132
Denis V. Lunev76c72d42007-09-16 15:44:27 -0700133 list_for_each_entry_safe(rule, tmp, &ops->rules_list, list) {
Thomas Graf14c0b972006-08-04 03:38:38 -0700134 list_del_rcu(&rule->list);
135 fib_rule_put(rule);
136 }
137}
Daniel Lezcano9eb87f32007-12-07 00:42:52 -0800138EXPORT_SYMBOL_GPL(fib_rules_cleanup_ops);
Thomas Graf14c0b972006-08-04 03:38:38 -0700139
Eric W. Biedermane9c51582009-12-03 12:22:55 -0800140static void fib_rules_put_rcu(struct rcu_head *head)
141{
142 struct fib_rules_ops *ops = container_of(head, struct fib_rules_ops, rcu);
143 struct net *net = ops->fro_net;
144
145 release_net(net);
146 kfree(ops);
147}
148
Denis V. Lunev9e3a5482008-01-20 16:46:41 -0800149void fib_rules_unregister(struct fib_rules_ops *ops)
Thomas Graf14c0b972006-08-04 03:38:38 -0700150{
Denis V. Lunev9e3a5482008-01-20 16:46:41 -0800151 struct net *net = ops->fro_net;
Thomas Graf14c0b972006-08-04 03:38:38 -0700152
Denis V. Lunev5fd30ee2008-01-10 03:20:28 -0800153 spin_lock(&net->rules_mod_lock);
Denis V. Lunev72132c1b2008-01-14 22:59:30 -0800154 list_del_rcu(&ops->list);
155 fib_rules_cleanup_ops(ops);
Denis V. Lunev5fd30ee2008-01-10 03:20:28 -0800156 spin_unlock(&net->rules_mod_lock);
Thomas Graf14c0b972006-08-04 03:38:38 -0700157
Eric W. Biedermane9c51582009-12-03 12:22:55 -0800158 call_rcu(&ops->rcu, fib_rules_put_rcu);
Thomas Graf14c0b972006-08-04 03:38:38 -0700159}
160
161EXPORT_SYMBOL_GPL(fib_rules_unregister);
162
Thomas Graf3dfbcc42006-11-09 15:23:20 -0800163static int fib_rule_match(struct fib_rule *rule, struct fib_rules_ops *ops,
164 struct flowi *fl, int flags)
165{
166 int ret = 0;
167
Patrick McHardy491deb24b2009-12-03 01:25:54 +0000168 if (rule->iifindex && (rule->iifindex != fl->iif))
Thomas Graf3dfbcc42006-11-09 15:23:20 -0800169 goto out;
170
Patrick McHardy1b038a52009-12-03 01:25:56 +0000171 if (rule->oifindex && (rule->oifindex != fl->oif))
172 goto out;
173
Thomas Graf3dfbcc42006-11-09 15:23:20 -0800174 if ((rule->mark ^ fl->mark) & rule->mark_mask)
175 goto out;
176
177 ret = ops->match(rule, fl, flags);
178out:
179 return (rule->flags & FIB_RULE_INVERT) ? !ret : ret;
180}
181
Thomas Graf14c0b972006-08-04 03:38:38 -0700182int fib_rules_lookup(struct fib_rules_ops *ops, struct flowi *fl,
183 int flags, struct fib_lookup_arg *arg)
184{
185 struct fib_rule *rule;
186 int err;
187
188 rcu_read_lock();
189
Denis V. Lunev76c72d42007-09-16 15:44:27 -0700190 list_for_each_entry_rcu(rule, &ops->rules_list, list) {
Thomas Graf0947c9fe2007-03-26 17:14:15 -0700191jumped:
Thomas Graf3dfbcc42006-11-09 15:23:20 -0800192 if (!fib_rule_match(rule, ops, fl, flags))
Thomas Graf14c0b972006-08-04 03:38:38 -0700193 continue;
194
Thomas Graf0947c9fe2007-03-26 17:14:15 -0700195 if (rule->action == FR_ACT_GOTO) {
196 struct fib_rule *target;
197
198 target = rcu_dereference(rule->ctarget);
199 if (target == NULL) {
200 continue;
201 } else {
202 rule = target;
203 goto jumped;
204 }
Thomas Graffa0b2d12007-03-26 17:38:53 -0700205 } else if (rule->action == FR_ACT_NOP)
206 continue;
207 else
Thomas Graf0947c9fe2007-03-26 17:14:15 -0700208 err = ops->action(rule, fl, flags, arg);
209
Thomas Graf14c0b972006-08-04 03:38:38 -0700210 if (err != -EAGAIN) {
211 fib_rule_get(rule);
212 arg->rule = rule;
213 goto out;
214 }
215 }
216
Steven Whitehouse83886b62007-03-30 13:34:27 -0700217 err = -ESRCH;
Thomas Graf14c0b972006-08-04 03:38:38 -0700218out:
219 rcu_read_unlock();
220
221 return err;
222}
223
224EXPORT_SYMBOL_GPL(fib_rules_lookup);
225
Thomas Grafe1701c62007-03-24 12:46:02 -0700226static int validate_rulemsg(struct fib_rule_hdr *frh, struct nlattr **tb,
227 struct fib_rules_ops *ops)
228{
229 int err = -EINVAL;
230
231 if (frh->src_len)
232 if (tb[FRA_SRC] == NULL ||
233 frh->src_len > (ops->addr_size * 8) ||
234 nla_len(tb[FRA_SRC]) != ops->addr_size)
235 goto errout;
236
237 if (frh->dst_len)
238 if (tb[FRA_DST] == NULL ||
239 frh->dst_len > (ops->addr_size * 8) ||
240 nla_len(tb[FRA_DST]) != ops->addr_size)
241 goto errout;
242
243 err = 0;
244errout:
245 return err;
246}
247
Thomas Graf9d9e6a52007-03-25 23:20:05 -0700248static int fib_nl_newrule(struct sk_buff *skb, struct nlmsghdr* nlh, void *arg)
Thomas Graf14c0b972006-08-04 03:38:38 -0700249{
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +0900250 struct net *net = sock_net(skb->sk);
Thomas Graf14c0b972006-08-04 03:38:38 -0700251 struct fib_rule_hdr *frh = nlmsg_data(nlh);
252 struct fib_rules_ops *ops = NULL;
253 struct fib_rule *rule, *r, *last = NULL;
254 struct nlattr *tb[FRA_MAX+1];
Thomas Graf0947c9fe2007-03-26 17:14:15 -0700255 int err = -EINVAL, unresolved = 0;
Thomas Graf14c0b972006-08-04 03:38:38 -0700256
257 if (nlh->nlmsg_len < nlmsg_msg_size(sizeof(*frh)))
258 goto errout;
259
Denis V. Lunev5fd30ee2008-01-10 03:20:28 -0800260 ops = lookup_rules_ops(net, frh->family);
Thomas Graf14c0b972006-08-04 03:38:38 -0700261 if (ops == NULL) {
Patrick McHardy2fe195c2008-07-01 19:59:37 -0700262 err = -EAFNOSUPPORT;
Thomas Graf14c0b972006-08-04 03:38:38 -0700263 goto errout;
264 }
265
266 err = nlmsg_parse(nlh, sizeof(*frh), tb, FRA_MAX, ops->policy);
267 if (err < 0)
268 goto errout;
269
Thomas Grafe1701c62007-03-24 12:46:02 -0700270 err = validate_rulemsg(frh, tb, ops);
271 if (err < 0)
272 goto errout;
273
Thomas Graf14c0b972006-08-04 03:38:38 -0700274 rule = kzalloc(ops->rule_size, GFP_KERNEL);
275 if (rule == NULL) {
276 err = -ENOMEM;
277 goto errout;
278 }
Denis V. Lunev3661a912008-04-16 02:01:56 -0700279 rule->fr_net = hold_net(net);
Thomas Graf14c0b972006-08-04 03:38:38 -0700280
281 if (tb[FRA_PRIORITY])
282 rule->pref = nla_get_u32(tb[FRA_PRIORITY]);
283
Patrick McHardy491deb24b2009-12-03 01:25:54 +0000284 if (tb[FRA_IIFNAME]) {
Thomas Graf14c0b972006-08-04 03:38:38 -0700285 struct net_device *dev;
286
Patrick McHardy491deb24b2009-12-03 01:25:54 +0000287 rule->iifindex = -1;
288 nla_strlcpy(rule->iifname, tb[FRA_IIFNAME], IFNAMSIZ);
289 dev = __dev_get_by_name(net, rule->iifname);
Thomas Graf14c0b972006-08-04 03:38:38 -0700290 if (dev)
Patrick McHardy491deb24b2009-12-03 01:25:54 +0000291 rule->iifindex = dev->ifindex;
Thomas Graf14c0b972006-08-04 03:38:38 -0700292 }
293
Patrick McHardy1b038a52009-12-03 01:25:56 +0000294 if (tb[FRA_OIFNAME]) {
295 struct net_device *dev;
296
297 rule->oifindex = -1;
298 nla_strlcpy(rule->oifname, tb[FRA_OIFNAME], IFNAMSIZ);
299 dev = __dev_get_by_name(net, rule->oifname);
300 if (dev)
301 rule->oifindex = dev->ifindex;
302 }
303
Thomas Grafb8964ed2006-11-09 15:22:18 -0800304 if (tb[FRA_FWMARK]) {
305 rule->mark = nla_get_u32(tb[FRA_FWMARK]);
306 if (rule->mark)
307 /* compatibility: if the mark value is non-zero all bits
308 * are compared unless a mask is explicitly specified.
309 */
310 rule->mark_mask = 0xFFFFFFFF;
311 }
312
313 if (tb[FRA_FWMASK])
314 rule->mark_mask = nla_get_u32(tb[FRA_FWMASK]);
315
Thomas Graf14c0b972006-08-04 03:38:38 -0700316 rule->action = frh->action;
317 rule->flags = frh->flags;
Patrick McHardy9e762a42006-08-10 23:09:48 -0700318 rule->table = frh_get_table(frh, tb);
Thomas Graf14c0b972006-08-04 03:38:38 -0700319
Patrick McHardy5adef182009-12-03 01:25:57 +0000320 if (!tb[FRA_PRIORITY] && ops->default_pref)
Denis V. Lunev868d13a2008-01-10 03:18:25 -0800321 rule->pref = ops->default_pref(ops);
Thomas Graf14c0b972006-08-04 03:38:38 -0700322
Thomas Graf0947c9fe2007-03-26 17:14:15 -0700323 err = -EINVAL;
324 if (tb[FRA_GOTO]) {
325 if (rule->action != FR_ACT_GOTO)
326 goto errout_free;
327
328 rule->target = nla_get_u32(tb[FRA_GOTO]);
329 /* Backward jumps are prohibited to avoid endless loops */
330 if (rule->target <= rule->pref)
331 goto errout_free;
332
Denis V. Lunev76c72d42007-09-16 15:44:27 -0700333 list_for_each_entry(r, &ops->rules_list, list) {
Thomas Graf0947c9fe2007-03-26 17:14:15 -0700334 if (r->pref == rule->target) {
335 rule->ctarget = r;
336 break;
337 }
338 }
339
340 if (rule->ctarget == NULL)
341 unresolved = 1;
342 } else if (rule->action == FR_ACT_GOTO)
343 goto errout_free;
344
Rami Rosen8b3521e2009-05-11 05:52:49 +0000345 err = ops->configure(rule, skb, frh, tb);
Thomas Graf14c0b972006-08-04 03:38:38 -0700346 if (err < 0)
347 goto errout_free;
348
Denis V. Lunev76c72d42007-09-16 15:44:27 -0700349 list_for_each_entry(r, &ops->rules_list, list) {
Thomas Graf14c0b972006-08-04 03:38:38 -0700350 if (r->pref > rule->pref)
351 break;
352 last = r;
353 }
354
355 fib_rule_get(rule);
356
Thomas Graf0947c9fe2007-03-26 17:14:15 -0700357 if (ops->unresolved_rules) {
358 /*
359 * There are unresolved goto rules in the list, check if
360 * any of them are pointing to this new rule.
361 */
Denis V. Lunev76c72d42007-09-16 15:44:27 -0700362 list_for_each_entry(r, &ops->rules_list, list) {
Thomas Graf0947c9fe2007-03-26 17:14:15 -0700363 if (r->action == FR_ACT_GOTO &&
364 r->target == rule->pref) {
365 BUG_ON(r->ctarget != NULL);
366 rcu_assign_pointer(r->ctarget, rule);
367 if (--ops->unresolved_rules == 0)
368 break;
369 }
370 }
371 }
372
373 if (rule->action == FR_ACT_GOTO)
374 ops->nr_goto_rules++;
375
376 if (unresolved)
377 ops->unresolved_rules++;
378
Thomas Graf14c0b972006-08-04 03:38:38 -0700379 if (last)
380 list_add_rcu(&rule->list, &last->list);
381 else
Denis V. Lunev76c72d42007-09-16 15:44:27 -0700382 list_add_rcu(&rule->list, &ops->rules_list);
Thomas Graf14c0b972006-08-04 03:38:38 -0700383
Denis V. Lunev9e3a5482008-01-20 16:46:41 -0800384 notify_rule_change(RTM_NEWRULE, rule, ops, nlh, NETLINK_CB(skb).pid);
Thomas Graf73417f62007-03-27 13:56:52 -0700385 flush_route_cache(ops);
Thomas Graf14c0b972006-08-04 03:38:38 -0700386 rules_ops_put(ops);
387 return 0;
388
389errout_free:
Denis V. Lunev3661a912008-04-16 02:01:56 -0700390 release_net(rule->fr_net);
Thomas Graf14c0b972006-08-04 03:38:38 -0700391 kfree(rule);
392errout:
393 rules_ops_put(ops);
394 return err;
395}
396
Thomas Graf9d9e6a52007-03-25 23:20:05 -0700397static int fib_nl_delrule(struct sk_buff *skb, struct nlmsghdr* nlh, void *arg)
Thomas Graf14c0b972006-08-04 03:38:38 -0700398{
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +0900399 struct net *net = sock_net(skb->sk);
Thomas Graf14c0b972006-08-04 03:38:38 -0700400 struct fib_rule_hdr *frh = nlmsg_data(nlh);
401 struct fib_rules_ops *ops = NULL;
Thomas Graf0947c9fe2007-03-26 17:14:15 -0700402 struct fib_rule *rule, *tmp;
Thomas Graf14c0b972006-08-04 03:38:38 -0700403 struct nlattr *tb[FRA_MAX+1];
404 int err = -EINVAL;
405
406 if (nlh->nlmsg_len < nlmsg_msg_size(sizeof(*frh)))
407 goto errout;
408
Denis V. Lunev5fd30ee2008-01-10 03:20:28 -0800409 ops = lookup_rules_ops(net, frh->family);
Thomas Graf14c0b972006-08-04 03:38:38 -0700410 if (ops == NULL) {
Patrick McHardy2fe195c2008-07-01 19:59:37 -0700411 err = -EAFNOSUPPORT;
Thomas Graf14c0b972006-08-04 03:38:38 -0700412 goto errout;
413 }
414
415 err = nlmsg_parse(nlh, sizeof(*frh), tb, FRA_MAX, ops->policy);
416 if (err < 0)
417 goto errout;
418
Thomas Grafe1701c62007-03-24 12:46:02 -0700419 err = validate_rulemsg(frh, tb, ops);
420 if (err < 0)
421 goto errout;
422
Denis V. Lunev76c72d42007-09-16 15:44:27 -0700423 list_for_each_entry(rule, &ops->rules_list, list) {
Thomas Graf14c0b972006-08-04 03:38:38 -0700424 if (frh->action && (frh->action != rule->action))
425 continue;
426
Patrick McHardy9e762a42006-08-10 23:09:48 -0700427 if (frh->table && (frh_get_table(frh, tb) != rule->table))
Thomas Graf14c0b972006-08-04 03:38:38 -0700428 continue;
429
430 if (tb[FRA_PRIORITY] &&
431 (rule->pref != nla_get_u32(tb[FRA_PRIORITY])))
432 continue;
433
Patrick McHardy491deb24b2009-12-03 01:25:54 +0000434 if (tb[FRA_IIFNAME] &&
435 nla_strcmp(tb[FRA_IIFNAME], rule->iifname))
Thomas Graf14c0b972006-08-04 03:38:38 -0700436 continue;
437
Patrick McHardy1b038a52009-12-03 01:25:56 +0000438 if (tb[FRA_OIFNAME] &&
439 nla_strcmp(tb[FRA_OIFNAME], rule->oifname))
440 continue;
441
Thomas Grafb8964ed2006-11-09 15:22:18 -0800442 if (tb[FRA_FWMARK] &&
443 (rule->mark != nla_get_u32(tb[FRA_FWMARK])))
444 continue;
445
446 if (tb[FRA_FWMASK] &&
447 (rule->mark_mask != nla_get_u32(tb[FRA_FWMASK])))
448 continue;
449
Thomas Graf14c0b972006-08-04 03:38:38 -0700450 if (!ops->compare(rule, frh, tb))
451 continue;
452
453 if (rule->flags & FIB_RULE_PERMANENT) {
454 err = -EPERM;
455 goto errout;
456 }
457
458 list_del_rcu(&rule->list);
Thomas Graf0947c9fe2007-03-26 17:14:15 -0700459
460 if (rule->action == FR_ACT_GOTO)
461 ops->nr_goto_rules--;
462
463 /*
464 * Check if this rule is a target to any of them. If so,
465 * disable them. As this operation is eventually very
466 * expensive, it is only performed if goto rules have
467 * actually been added.
468 */
469 if (ops->nr_goto_rules > 0) {
Denis V. Lunev76c72d42007-09-16 15:44:27 -0700470 list_for_each_entry(tmp, &ops->rules_list, list) {
Thomas Graf0947c9fe2007-03-26 17:14:15 -0700471 if (tmp->ctarget == rule) {
472 rcu_assign_pointer(tmp->ctarget, NULL);
473 ops->unresolved_rules++;
474 }
475 }
476 }
477
Thomas Graf14c0b972006-08-04 03:38:38 -0700478 synchronize_rcu();
Denis V. Lunev9e3a5482008-01-20 16:46:41 -0800479 notify_rule_change(RTM_DELRULE, rule, ops, nlh,
Thomas Grafc17084d2006-08-15 00:32:48 -0700480 NETLINK_CB(skb).pid);
Thomas Graf14c0b972006-08-04 03:38:38 -0700481 fib_rule_put(rule);
Thomas Graf73417f62007-03-27 13:56:52 -0700482 flush_route_cache(ops);
Thomas Graf14c0b972006-08-04 03:38:38 -0700483 rules_ops_put(ops);
484 return 0;
485 }
486
487 err = -ENOENT;
488errout:
489 rules_ops_put(ops);
490 return err;
491}
492
Thomas Graf339bf982006-11-10 14:10:15 -0800493static inline size_t fib_rule_nlmsg_size(struct fib_rules_ops *ops,
494 struct fib_rule *rule)
495{
496 size_t payload = NLMSG_ALIGN(sizeof(struct fib_rule_hdr))
Patrick McHardy491deb24b2009-12-03 01:25:54 +0000497 + nla_total_size(IFNAMSIZ) /* FRA_IIFNAME */
Patrick McHardy1b038a52009-12-03 01:25:56 +0000498 + nla_total_size(IFNAMSIZ) /* FRA_OIFNAME */
Thomas Graf339bf982006-11-10 14:10:15 -0800499 + nla_total_size(4) /* FRA_PRIORITY */
500 + nla_total_size(4) /* FRA_TABLE */
501 + nla_total_size(4) /* FRA_FWMARK */
502 + nla_total_size(4); /* FRA_FWMASK */
503
504 if (ops->nlmsg_payload)
505 payload += ops->nlmsg_payload(rule);
506
507 return payload;
508}
509
Thomas Graf14c0b972006-08-04 03:38:38 -0700510static int fib_nl_fill_rule(struct sk_buff *skb, struct fib_rule *rule,
511 u32 pid, u32 seq, int type, int flags,
512 struct fib_rules_ops *ops)
513{
514 struct nlmsghdr *nlh;
515 struct fib_rule_hdr *frh;
516
517 nlh = nlmsg_put(skb, pid, seq, type, sizeof(*frh), flags);
518 if (nlh == NULL)
Patrick McHardy26932562007-01-31 23:16:40 -0800519 return -EMSGSIZE;
Thomas Graf14c0b972006-08-04 03:38:38 -0700520
521 frh = nlmsg_data(nlh);
522 frh->table = rule->table;
Patrick McHardy9e762a42006-08-10 23:09:48 -0700523 NLA_PUT_U32(skb, FRA_TABLE, rule->table);
Thomas Graf14c0b972006-08-04 03:38:38 -0700524 frh->res1 = 0;
525 frh->res2 = 0;
526 frh->action = rule->action;
527 frh->flags = rule->flags;
528
Thomas Graf0947c9fe2007-03-26 17:14:15 -0700529 if (rule->action == FR_ACT_GOTO && rule->ctarget == NULL)
530 frh->flags |= FIB_RULE_UNRESOLVED;
531
Patrick McHardy491deb24b2009-12-03 01:25:54 +0000532 if (rule->iifname[0]) {
533 NLA_PUT_STRING(skb, FRA_IIFNAME, rule->iifname);
Thomas Graf14c0b972006-08-04 03:38:38 -0700534
Patrick McHardy491deb24b2009-12-03 01:25:54 +0000535 if (rule->iifindex == -1)
536 frh->flags |= FIB_RULE_IIF_DETACHED;
Thomas Graf2b443682007-03-26 17:37:59 -0700537 }
538
Patrick McHardy1b038a52009-12-03 01:25:56 +0000539 if (rule->oifname[0]) {
540 NLA_PUT_STRING(skb, FRA_OIFNAME, rule->oifname);
541
542 if (rule->oifindex == -1)
543 frh->flags |= FIB_RULE_OIF_DETACHED;
544 }
545
Thomas Graf14c0b972006-08-04 03:38:38 -0700546 if (rule->pref)
547 NLA_PUT_U32(skb, FRA_PRIORITY, rule->pref);
548
Thomas Grafb8964ed2006-11-09 15:22:18 -0800549 if (rule->mark)
550 NLA_PUT_U32(skb, FRA_FWMARK, rule->mark);
551
552 if (rule->mark_mask || rule->mark)
553 NLA_PUT_U32(skb, FRA_FWMASK, rule->mark_mask);
554
Thomas Graf0947c9fe2007-03-26 17:14:15 -0700555 if (rule->target)
556 NLA_PUT_U32(skb, FRA_GOTO, rule->target);
557
Rami Rosen04af8cf2009-05-20 17:26:23 -0700558 if (ops->fill(rule, skb, frh) < 0)
Thomas Graf14c0b972006-08-04 03:38:38 -0700559 goto nla_put_failure;
560
561 return nlmsg_end(skb, nlh);
562
563nla_put_failure:
Patrick McHardy26932562007-01-31 23:16:40 -0800564 nlmsg_cancel(skb, nlh);
565 return -EMSGSIZE;
Thomas Graf14c0b972006-08-04 03:38:38 -0700566}
567
Thomas Grafc4546732007-03-25 23:24:24 -0700568static int dump_rules(struct sk_buff *skb, struct netlink_callback *cb,
569 struct fib_rules_ops *ops)
Thomas Graf14c0b972006-08-04 03:38:38 -0700570{
571 int idx = 0;
572 struct fib_rule *rule;
Thomas Graf14c0b972006-08-04 03:38:38 -0700573
Denis V. Lunev76c72d42007-09-16 15:44:27 -0700574 list_for_each_entry(rule, &ops->rules_list, list) {
Thomas Grafc4546732007-03-25 23:24:24 -0700575 if (idx < cb->args[1])
Thomas Graf14c0b972006-08-04 03:38:38 -0700576 goto skip;
577
578 if (fib_nl_fill_rule(skb, rule, NETLINK_CB(cb->skb).pid,
579 cb->nlh->nlmsg_seq, RTM_NEWRULE,
580 NLM_F_MULTI, ops) < 0)
581 break;
582skip:
583 idx++;
584 }
Thomas Grafc4546732007-03-25 23:24:24 -0700585 cb->args[1] = idx;
Thomas Graf14c0b972006-08-04 03:38:38 -0700586 rules_ops_put(ops);
587
588 return skb->len;
589}
590
Thomas Grafc4546732007-03-25 23:24:24 -0700591static int fib_nl_dumprule(struct sk_buff *skb, struct netlink_callback *cb)
592{
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +0900593 struct net *net = sock_net(skb->sk);
Thomas Grafc4546732007-03-25 23:24:24 -0700594 struct fib_rules_ops *ops;
595 int idx = 0, family;
596
597 family = rtnl_msg_family(cb->nlh);
598 if (family != AF_UNSPEC) {
599 /* Protocol specific dump request */
Denis V. Lunev5fd30ee2008-01-10 03:20:28 -0800600 ops = lookup_rules_ops(net, family);
Thomas Grafc4546732007-03-25 23:24:24 -0700601 if (ops == NULL)
602 return -EAFNOSUPPORT;
603
604 return dump_rules(skb, cb, ops);
605 }
606
607 rcu_read_lock();
Denis V. Lunev5fd30ee2008-01-10 03:20:28 -0800608 list_for_each_entry_rcu(ops, &net->rules_ops, list) {
Thomas Grafc4546732007-03-25 23:24:24 -0700609 if (idx < cb->args[0] || !try_module_get(ops->owner))
610 goto skip;
611
612 if (dump_rules(skb, cb, ops) < 0)
613 break;
614
615 cb->args[1] = 0;
616 skip:
617 idx++;
618 }
619 rcu_read_unlock();
620 cb->args[0] = idx;
621
622 return skb->len;
623}
Thomas Graf14c0b972006-08-04 03:38:38 -0700624
Denis V. Lunev9e3a5482008-01-20 16:46:41 -0800625static void notify_rule_change(int event, struct fib_rule *rule,
Thomas Grafc17084d2006-08-15 00:32:48 -0700626 struct fib_rules_ops *ops, struct nlmsghdr *nlh,
627 u32 pid)
Thomas Graf14c0b972006-08-04 03:38:38 -0700628{
Denis V. Lunev9e3a5482008-01-20 16:46:41 -0800629 struct net *net;
Thomas Grafc17084d2006-08-15 00:32:48 -0700630 struct sk_buff *skb;
631 int err = -ENOBUFS;
Thomas Graf14c0b972006-08-04 03:38:38 -0700632
Denis V. Lunev9e3a5482008-01-20 16:46:41 -0800633 net = ops->fro_net;
Thomas Graf339bf982006-11-10 14:10:15 -0800634 skb = nlmsg_new(fib_rule_nlmsg_size(ops, rule), GFP_KERNEL);
Thomas Graf14c0b972006-08-04 03:38:38 -0700635 if (skb == NULL)
Thomas Grafc17084d2006-08-15 00:32:48 -0700636 goto errout;
637
638 err = fib_nl_fill_rule(skb, rule, pid, nlh->nlmsg_seq, event, 0, ops);
Patrick McHardy26932562007-01-31 23:16:40 -0800639 if (err < 0) {
640 /* -EMSGSIZE implies BUG in fib_rule_nlmsg_size() */
641 WARN_ON(err == -EMSGSIZE);
642 kfree_skb(skb);
643 goto errout;
644 }
Denis V. Lunev9e3a5482008-01-20 16:46:41 -0800645
Pablo Neira Ayuso1ce85fe2009-02-24 23:18:28 -0800646 rtnl_notify(skb, net, pid, ops->nlgroup, nlh, GFP_KERNEL);
647 return;
Thomas Grafc17084d2006-08-15 00:32:48 -0700648errout:
649 if (err < 0)
Denis V. Lunev5fd30ee2008-01-10 03:20:28 -0800650 rtnl_set_sk_err(net, ops->nlgroup, err);
Thomas Graf14c0b972006-08-04 03:38:38 -0700651}
652
653static void attach_rules(struct list_head *rules, struct net_device *dev)
654{
655 struct fib_rule *rule;
656
657 list_for_each_entry(rule, rules, list) {
Patrick McHardy491deb24b2009-12-03 01:25:54 +0000658 if (rule->iifindex == -1 &&
659 strcmp(dev->name, rule->iifname) == 0)
660 rule->iifindex = dev->ifindex;
Patrick McHardy1b038a52009-12-03 01:25:56 +0000661 if (rule->oifindex == -1 &&
662 strcmp(dev->name, rule->oifname) == 0)
663 rule->oifindex = dev->ifindex;
Thomas Graf14c0b972006-08-04 03:38:38 -0700664 }
665}
666
667static void detach_rules(struct list_head *rules, struct net_device *dev)
668{
669 struct fib_rule *rule;
670
Patrick McHardy1b038a52009-12-03 01:25:56 +0000671 list_for_each_entry(rule, rules, list) {
Patrick McHardy491deb24b2009-12-03 01:25:54 +0000672 if (rule->iifindex == dev->ifindex)
673 rule->iifindex = -1;
Patrick McHardy1b038a52009-12-03 01:25:56 +0000674 if (rule->oifindex == dev->ifindex)
675 rule->oifindex = -1;
676 }
Thomas Graf14c0b972006-08-04 03:38:38 -0700677}
678
679
680static int fib_rules_event(struct notifier_block *this, unsigned long event,
681 void *ptr)
682{
683 struct net_device *dev = ptr;
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +0900684 struct net *net = dev_net(dev);
Thomas Graf14c0b972006-08-04 03:38:38 -0700685 struct fib_rules_ops *ops;
686
687 ASSERT_RTNL();
688 rcu_read_lock();
689
690 switch (event) {
691 case NETDEV_REGISTER:
Denis V. Lunev5fd30ee2008-01-10 03:20:28 -0800692 list_for_each_entry(ops, &net->rules_ops, list)
Denis V. Lunev76c72d42007-09-16 15:44:27 -0700693 attach_rules(&ops->rules_list, dev);
Thomas Graf14c0b972006-08-04 03:38:38 -0700694 break;
695
696 case NETDEV_UNREGISTER:
Denis V. Lunev5fd30ee2008-01-10 03:20:28 -0800697 list_for_each_entry(ops, &net->rules_ops, list)
Denis V. Lunev76c72d42007-09-16 15:44:27 -0700698 detach_rules(&ops->rules_list, dev);
Thomas Graf14c0b972006-08-04 03:38:38 -0700699 break;
700 }
701
702 rcu_read_unlock();
703
704 return NOTIFY_DONE;
705}
706
707static struct notifier_block fib_rules_notifier = {
708 .notifier_call = fib_rules_event,
709};
710
Alexey Dobriyan2c8c1e72010-01-17 03:35:32 +0000711static int __net_init fib_rules_net_init(struct net *net)
Denis V. Lunev5fd30ee2008-01-10 03:20:28 -0800712{
713 INIT_LIST_HEAD(&net->rules_ops);
714 spin_lock_init(&net->rules_mod_lock);
715 return 0;
716}
717
718static struct pernet_operations fib_rules_net_ops = {
719 .init = fib_rules_net_init,
720};
721
Thomas Graf14c0b972006-08-04 03:38:38 -0700722static int __init fib_rules_init(void)
723{
Denis V. Lunev5fd30ee2008-01-10 03:20:28 -0800724 int err;
Thomas Graf9d9e6a52007-03-25 23:20:05 -0700725 rtnl_register(PF_UNSPEC, RTM_NEWRULE, fib_nl_newrule, NULL);
726 rtnl_register(PF_UNSPEC, RTM_DELRULE, fib_nl_delrule, NULL);
Thomas Grafc4546732007-03-25 23:24:24 -0700727 rtnl_register(PF_UNSPEC, RTM_GETRULE, NULL, fib_nl_dumprule);
Thomas Graf9d9e6a52007-03-25 23:20:05 -0700728
Eric W. Biederman5d6d4802008-11-07 22:52:34 -0800729 err = register_pernet_subsys(&fib_rules_net_ops);
Denis V. Lunev5fd30ee2008-01-10 03:20:28 -0800730 if (err < 0)
731 goto fail;
732
Eric W. Biederman5d6d4802008-11-07 22:52:34 -0800733 err = register_netdevice_notifier(&fib_rules_notifier);
Denis V. Lunev5fd30ee2008-01-10 03:20:28 -0800734 if (err < 0)
735 goto fail_unregister;
Eric W. Biederman5d6d4802008-11-07 22:52:34 -0800736
Denis V. Lunev5fd30ee2008-01-10 03:20:28 -0800737 return 0;
738
739fail_unregister:
Eric W. Biederman5d6d4802008-11-07 22:52:34 -0800740 unregister_pernet_subsys(&fib_rules_net_ops);
Denis V. Lunev5fd30ee2008-01-10 03:20:28 -0800741fail:
742 rtnl_unregister(PF_UNSPEC, RTM_NEWRULE);
743 rtnl_unregister(PF_UNSPEC, RTM_DELRULE);
744 rtnl_unregister(PF_UNSPEC, RTM_GETRULE);
745 return err;
Thomas Graf14c0b972006-08-04 03:38:38 -0700746}
747
748subsys_initcall(fib_rules_init);