]> git.proxmox.com Git - mirror_ubuntu-artful-kernel.git/blame - net/ipv4/fib_rules.c
UBUNTU: SAUCE: media: uvcvideo: Support realtek's UVC 1.5 device
[mirror_ubuntu-artful-kernel.git] / net / ipv4 / fib_rules.c
CommitLineData
1da177e4
LT
1/*
2 * INET An implementation of the TCP/IP protocol suite for the LINUX
3 * operating system. INET is implemented using the BSD Socket
4 * interface as the means of communication with the user level.
5 *
6 * IPv4 Forwarding Information Base: policy rules.
7 *
1da177e4 8 * Authors: Alexey Kuznetsov, <kuznet@ms2.inr.ac.ru>
6a31d2a9 9 * Thomas Graf <tgraf@suug.ch>
1da177e4
LT
10 *
11 * This program is free software; you can redistribute it and/or
12 * modify it under the terms of the GNU General Public License
13 * as published by the Free Software Foundation; either version
14 * 2 of the License, or (at your option) any later version.
15 *
16 * Fixes:
6a31d2a9 17 * Rani Assaf : local_rule cannot be deleted
1da177e4
LT
18 * Marc Boucher : routing by fwmark
19 */
20
1da177e4
LT
21#include <linux/types.h>
22#include <linux/kernel.h>
1da177e4 23#include <linux/netdevice.h>
1da177e4 24#include <linux/netlink.h>
e1ef4bf2 25#include <linux/inetdevice.h>
1da177e4 26#include <linux/init.h>
7b204afd
RO
27#include <linux/list.h>
28#include <linux/rcupdate.h>
bc3b2d7f 29#include <linux/export.h>
1da177e4 30#include <net/ip.h>
1da177e4
LT
31#include <net/route.h>
32#include <net/tcp.h>
1da177e4 33#include <net/ip_fib.h>
e1ef4bf2 34#include <net/fib_rules.h>
1da177e4 35
6a31d2a9 36struct fib4_rule {
e1ef4bf2
TG
37 struct fib_rule common;
38 u8 dst_len;
39 u8 src_len;
40 u8 tos;
81f7bf6c
AV
41 __be32 src;
42 __be32 srcmask;
43 __be32 dst;
44 __be32 dstmask;
c7066f70 45#ifdef CONFIG_IP_ROUTE_CLASSID
e1ef4bf2 46 u32 tclassid;
1da177e4 47#endif
1da177e4
LT
48};
49
3c71006d
IS
50static bool fib4_rule_matchall(const struct fib_rule *rule)
51{
52 struct fib4_rule *r = container_of(rule, struct fib4_rule, common);
53
54 if (r->dst_len || r->src_len || r->tos)
55 return false;
56 return fib_rule_matchall(rule);
57}
58
59bool fib4_rule_default(const struct fib_rule *rule)
60{
61 if (!fib4_rule_matchall(rule) || rule->action != FR_ACT_TO_TBL ||
62 rule->l3mdev)
63 return false;
64 if (rule->table != RT_TABLE_LOCAL && rule->table != RT_TABLE_MAIN &&
65 rule->table != RT_TABLE_DEFAULT)
66 return false;
67 return true;
68}
69EXPORT_SYMBOL_GPL(fib4_rule_default);
70
0eeb075f
AG
71int __fib_lookup(struct net *net, struct flowi4 *flp,
72 struct fib_result *res, unsigned int flags)
e1ef4bf2
TG
73{
74 struct fib_lookup_arg arg = {
75 .result = res,
0eeb075f 76 .flags = flags,
e1ef4bf2
TG
77 };
78 int err;
1da177e4 79
9ee0034b
DA
80 /* update flow if oif or iif point to device enslaved to l3mdev */
81 l3mdev_update_flow(net, flowi4_to_flowi(flp));
82
22bd5b9b 83 err = fib_rules_lookup(net->ipv4.rules_ops, flowi4_to_flowi(flp), 0, &arg);
85b91b03
DM
84#ifdef CONFIG_IP_ROUTE_CLASSID
85 if (arg.rule)
86 res->tclassid = ((struct fib4_rule *)arg.rule)->tclassid;
87 else
88 res->tclassid = 0;
89#endif
49dd18ba
PM
90
91 if (err == -ESRCH)
92 err = -ENETUNREACH;
93
e1ef4bf2
TG
94 return err;
95}
f4530fa5 96EXPORT_SYMBOL_GPL(__fib_lookup);
e1ef4bf2 97
8ce11e6a
AB
98static int fib4_rule_action(struct fib_rule *rule, struct flowi *flp,
99 int flags, struct fib_lookup_arg *arg)
1da177e4 100{
e1ef4bf2
TG
101 int err = -EAGAIN;
102 struct fib_table *tbl;
96c63fa7 103 u32 tb_id;
e1ef4bf2
TG
104
105 switch (rule->action) {
106 case FR_ACT_TO_TBL:
107 break;
108
109 case FR_ACT_UNREACHABLE:
345e9b54 110 return -ENETUNREACH;
e1ef4bf2
TG
111
112 case FR_ACT_PROHIBIT:
345e9b54 113 return -EACCES;
e1ef4bf2
TG
114
115 case FR_ACT_BLACKHOLE:
116 default:
345e9b54 117 return -EINVAL;
1da177e4 118 }
e1ef4bf2 119
345e9b54
AD
120 rcu_read_lock();
121
96c63fa7
DA
122 tb_id = fib_rule_get_table(rule, arg);
123 tbl = fib_get_table(rule->fr_net, tb_id);
345e9b54
AD
124 if (tbl)
125 err = fib_table_lookup(tbl, &flp->u.ip4,
126 (struct fib_result *)arg->result,
127 arg->flags);
e1ef4bf2 128
345e9b54 129 rcu_read_unlock();
1da177e4
LT
130 return err;
131}
132
7764a45a
ST
133static bool fib4_rule_suppress(struct fib_rule *rule, struct fib_lookup_arg *arg)
134{
6ef94cfa 135 struct fib_result *result = (struct fib_result *) arg->result;
673498b8
ST
136 struct net_device *dev = NULL;
137
138 if (result->fi)
139 dev = result->fi->fib_dev;
6ef94cfa 140
7764a45a
ST
141 /* do not accept result if the route does
142 * not meet the required prefix length
143 */
73f5698e 144 if (result->prefixlen <= rule->suppress_prefixlen)
6ef94cfa
ST
145 goto suppress_route;
146
147 /* do not accept result if the route uses a device
148 * belonging to a forbidden interface group
149 */
150 if (rule->suppress_ifgroup != -1 && dev && dev->group == rule->suppress_ifgroup)
151 goto suppress_route;
152
7764a45a 153 return false;
6ef94cfa
ST
154
155suppress_route:
156 if (!(arg->flags & FIB_LOOKUP_NOREF))
157 fib_info_put(result->fi);
158 return true;
7764a45a 159}
e1ef4bf2 160
e1ef4bf2
TG
161static int fib4_rule_match(struct fib_rule *rule, struct flowi *fl, int flags)
162{
163 struct fib4_rule *r = (struct fib4_rule *) rule;
9ade2286
DM
164 struct flowi4 *fl4 = &fl->u.ip4;
165 __be32 daddr = fl4->daddr;
166 __be32 saddr = fl4->saddr;
e1ef4bf2
TG
167
168 if (((saddr ^ r->src) & r->srcmask) ||
169 ((daddr ^ r->dst) & r->dstmask))
170 return 0;
171
9ade2286 172 if (r->tos && (r->tos != fl4->flowi4_tos))
e1ef4bf2
TG
173 return 0;
174
e1ef4bf2
TG
175 return 1;
176}
1da177e4 177
8ad4942c 178static struct fib_table *fib_empty_table(struct net *net)
1da177e4 179{
2dfe55b4 180 u32 id;
1da177e4
LT
181
182 for (id = 1; id <= RT_TABLE_MAX; id++)
51456b29 183 if (!fib_get_table(net, id))
8ad4942c 184 return fib_new_table(net, id);
1da177e4
LT
185 return NULL;
186}
187
5d7bfd14
IS
188static int call_fib_rule_notifier(struct notifier_block *nb, struct net *net,
189 enum fib_event_type event_type,
190 struct fib_rule *rule)
191{
192 struct fib_rule_notifier_info info = {
193 .rule = rule,
194 };
195
196 return call_fib_notifier(nb, net, event_type, &info.info);
197}
198
b90eb754 199static int call_fib_rule_notifiers(struct net *net,
6a003a5f
IS
200 enum fib_event_type event_type,
201 struct fib_rule *rule)
b90eb754 202{
6a003a5f
IS
203 struct fib_rule_notifier_info info = {
204 .rule = rule,
205 };
b90eb754 206
6a003a5f 207 return call_fib_notifiers(net, event_type, &info.info);
b90eb754
JP
208}
209
5d7bfd14 210/* Called with rcu_read_lock() */
d05f7a7d 211void fib_rules_notify(struct net *net, struct notifier_block *nb)
c0243892 212{
5d7bfd14
IS
213 struct fib_rules_ops *ops = net->ipv4.rules_ops;
214 struct fib_rule *rule;
c0243892 215
5d7bfd14
IS
216 list_for_each_entry_rcu(rule, &ops->rules_list, list)
217 call_fib_rule_notifier(nb, net, FIB_EVENT_RULE_ADD, rule);
c0243892
IS
218}
219
ef7c79ed 220static const struct nla_policy fib4_rule_policy[FRA_MAX+1] = {
1f6c9557 221 FRA_GENERIC_POLICY,
e1ef4bf2
TG
222 [FRA_FLOW] = { .type = NLA_U32 },
223};
7b204afd 224
e1ef4bf2 225static int fib4_rule_configure(struct fib_rule *rule, struct sk_buff *skb,
8b3521ee 226 struct fib_rule_hdr *frh,
e1ef4bf2 227 struct nlattr **tb)
1da177e4 228{
3b1e0a65 229 struct net *net = sock_net(skb->sk);
e1ef4bf2
TG
230 int err = -EINVAL;
231 struct fib4_rule *rule4 = (struct fib4_rule *) rule;
1da177e4 232
e1701c68 233 if (frh->tos & ~IPTOS_TOS_MASK)
e1ef4bf2 234 goto errout;
7b204afd 235
0ddcf43d
AD
236 /* split local/main if they are not already split */
237 err = fib_unmerge(net);
238 if (err)
239 goto errout;
240
96c63fa7 241 if (rule->table == RT_TABLE_UNSPEC && !rule->l3mdev) {
e1ef4bf2
TG
242 if (rule->action == FR_ACT_TO_TBL) {
243 struct fib_table *table;
1da177e4 244
e4e4971c 245 table = fib_empty_table(net);
51456b29 246 if (!table) {
e1ef4bf2
TG
247 err = -ENOBUFS;
248 goto errout;
249 }
1da177e4 250
e1ef4bf2 251 rule->table = table->tb_id;
1da177e4
LT
252 }
253 }
254
e1701c68 255 if (frh->src_len)
67b61f6c 256 rule4->src = nla_get_in_addr(tb[FRA_SRC]);
7b204afd 257
e1701c68 258 if (frh->dst_len)
67b61f6c 259 rule4->dst = nla_get_in_addr(tb[FRA_DST]);
7b204afd 260
c7066f70 261#ifdef CONFIG_IP_ROUTE_CLASSID
7a9bc9b8 262 if (tb[FRA_FLOW]) {
e1ef4bf2 263 rule4->tclassid = nla_get_u32(tb[FRA_FLOW]);
7a9bc9b8 264 if (rule4->tclassid)
f4530fa5 265 net->ipv4.fib_num_tclassid_users++;
7a9bc9b8 266 }
1da177e4
LT
267#endif
268
e1ef4bf2
TG
269 rule4->src_len = frh->src_len;
270 rule4->srcmask = inet_make_mask(rule4->src_len);
271 rule4->dst_len = frh->dst_len;
272 rule4->dstmask = inet_make_mask(rule4->dst_len);
273 rule4->tos = frh->tos;
7b204afd 274
f4530fa5 275 net->ipv4.fib_has_custom_rules = true;
6a003a5f 276 call_fib_rule_notifiers(net, FIB_EVENT_RULE_ADD, rule);
104616e7 277
e1ef4bf2
TG
278 err = 0;
279errout:
280 return err;
1da177e4
LT
281}
282
0ddcf43d 283static int fib4_rule_delete(struct fib_rule *rule)
7a9bc9b8 284{
f4530fa5 285 struct net *net = rule->fr_net;
0ddcf43d 286 int err;
7a9bc9b8 287
0ddcf43d
AD
288 /* split local/main if they are not already split */
289 err = fib_unmerge(net);
290 if (err)
291 goto errout;
292
293#ifdef CONFIG_IP_ROUTE_CLASSID
294 if (((struct fib4_rule *)rule)->tclassid)
f4530fa5 295 net->ipv4.fib_num_tclassid_users--;
7a9bc9b8 296#endif
f4530fa5 297 net->ipv4.fib_has_custom_rules = true;
6a003a5f 298 call_fib_rule_notifiers(net, FIB_EVENT_RULE_DEL, rule);
0ddcf43d
AD
299errout:
300 return err;
7a9bc9b8
DM
301}
302
e1ef4bf2
TG
303static int fib4_rule_compare(struct fib_rule *rule, struct fib_rule_hdr *frh,
304 struct nlattr **tb)
1da177e4 305{
e1ef4bf2 306 struct fib4_rule *rule4 = (struct fib4_rule *) rule;
1da177e4 307
e1ef4bf2
TG
308 if (frh->src_len && (rule4->src_len != frh->src_len))
309 return 0;
1da177e4 310
e1ef4bf2
TG
311 if (frh->dst_len && (rule4->dst_len != frh->dst_len))
312 return 0;
7b204afd 313
e1ef4bf2
TG
314 if (frh->tos && (rule4->tos != frh->tos))
315 return 0;
7b204afd 316
c7066f70 317#ifdef CONFIG_IP_ROUTE_CLASSID
e1ef4bf2
TG
318 if (tb[FRA_FLOW] && (rule4->tclassid != nla_get_u32(tb[FRA_FLOW])))
319 return 0;
320#endif
1da177e4 321
67b61f6c 322 if (frh->src_len && (rule4->src != nla_get_in_addr(tb[FRA_SRC])))
e1ef4bf2 323 return 0;
1da177e4 324
67b61f6c 325 if (frh->dst_len && (rule4->dst != nla_get_in_addr(tb[FRA_DST])))
e1ef4bf2 326 return 0;
1da177e4 327
e1ef4bf2 328 return 1;
1da177e4
LT
329}
330
e1ef4bf2 331static int fib4_rule_fill(struct fib_rule *rule, struct sk_buff *skb,
04af8cf6 332 struct fib_rule_hdr *frh)
e1ef4bf2
TG
333{
334 struct fib4_rule *rule4 = (struct fib4_rule *) rule;
1da177e4 335
e1ef4bf2
TG
336 frh->dst_len = rule4->dst_len;
337 frh->src_len = rule4->src_len;
338 frh->tos = rule4->tos;
1da177e4 339
f3756b79 340 if ((rule4->dst_len &&
930345ea 341 nla_put_in_addr(skb, FRA_DST, rule4->dst)) ||
f3756b79 342 (rule4->src_len &&
930345ea 343 nla_put_in_addr(skb, FRA_SRC, rule4->src)))
f3756b79 344 goto nla_put_failure;
c7066f70 345#ifdef CONFIG_IP_ROUTE_CLASSID
f3756b79
DM
346 if (rule4->tclassid &&
347 nla_put_u32(skb, FRA_FLOW, rule4->tclassid))
348 goto nla_put_failure;
1da177e4 349#endif
e1ef4bf2 350 return 0;
1da177e4 351
e1ef4bf2
TG
352nla_put_failure:
353 return -ENOBUFS;
1da177e4
LT
354}
355
339bf98f
TG
356static size_t fib4_rule_nlmsg_payload(struct fib_rule *rule)
357{
358 return nla_total_size(4) /* dst */
359 + nla_total_size(4) /* src */
360 + nla_total_size(4); /* flow */
361}
362
ae299fc0 363static void fib4_rule_flush_cache(struct fib_rules_ops *ops)
73417f61 364{
bafa6d9d 365 rt_cache_flush(ops->fro_net);
73417f61
TG
366}
367
04a6f82c 368static const struct fib_rules_ops __net_initconst fib4_rules_ops_template = {
25239cee 369 .family = AF_INET,
e1ef4bf2 370 .rule_size = sizeof(struct fib4_rule),
e1701c68 371 .addr_size = sizeof(u32),
e1ef4bf2 372 .action = fib4_rule_action,
7764a45a 373 .suppress = fib4_rule_suppress,
e1ef4bf2
TG
374 .match = fib4_rule_match,
375 .configure = fib4_rule_configure,
7a9bc9b8 376 .delete = fib4_rule_delete,
e1ef4bf2
TG
377 .compare = fib4_rule_compare,
378 .fill = fib4_rule_fill,
339bf98f 379 .nlmsg_payload = fib4_rule_nlmsg_payload,
73417f61 380 .flush_cache = fib4_rule_flush_cache,
e1ef4bf2
TG
381 .nlgroup = RTNLGRP_IPV4_RULE,
382 .policy = fib4_rule_policy,
e1ef4bf2
TG
383 .owner = THIS_MODULE,
384};
385
e4e4971c 386static int fib_default_rules_init(struct fib_rules_ops *ops)
1da177e4 387{
2994c638
DL
388 int err;
389
5adef180 390 err = fib_default_rule_add(ops, 0, RT_TABLE_LOCAL, 0);
2994c638
DL
391 if (err < 0)
392 return err;
e4e4971c 393 err = fib_default_rule_add(ops, 0x7FFE, RT_TABLE_MAIN, 0);
2994c638
DL
394 if (err < 0)
395 return err;
e4e4971c 396 err = fib_default_rule_add(ops, 0x7FFF, RT_TABLE_DEFAULT, 0);
2994c638
DL
397 if (err < 0)
398 return err;
399 return 0;
400}
1da177e4 401
7b1a74fd 402int __net_init fib4_rules_init(struct net *net)
2994c638 403{
dbb50165 404 int err;
e4e4971c
DL
405 struct fib_rules_ops *ops;
406
e9c5158a
EB
407 ops = fib_rules_register(&fib4_rules_ops_template, net);
408 if (IS_ERR(ops))
409 return PTR_ERR(ops);
dbb50165 410
e4e4971c 411 err = fib_default_rules_init(ops);
dbb50165
DL
412 if (err < 0)
413 goto fail;
e4e4971c 414 net->ipv4.rules_ops = ops;
f4530fa5 415 net->ipv4.fib_has_custom_rules = false;
dbb50165
DL
416 return 0;
417
418fail:
419 /* also cleans all rules already added */
9e3a5487 420 fib_rules_unregister(ops);
dbb50165 421 return err;
1da177e4 422}
7b1a74fd
DL
423
424void __net_exit fib4_rules_exit(struct net *net)
425{
9e3a5487 426 fib_rules_unregister(net->ipv4.rules_ops);
7b1a74fd 427}