1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21#include <linux/types.h>
22#include <linux/kernel.h>
23#include <linux/netdevice.h>
24#include <linux/netlink.h>
25#include <linux/inetdevice.h>
26#include <linux/init.h>
27#include <linux/list.h>
28#include <linux/rcupdate.h>
29#include <linux/export.h>
30#include <net/ip.h>
31#include <net/route.h>
32#include <net/tcp.h>
33#include <net/ip_fib.h>
34#include <net/fib_rules.h>
35
36struct fib4_rule {
37 struct fib_rule common;
38 u8 dst_len;
39 u8 src_len;
40 u8 tos;
41 __be32 src;
42 __be32 srcmask;
43 __be32 dst;
44 __be32 dstmask;
45#ifdef CONFIG_IP_ROUTE_CLASSID
46 u32 tclassid;
47#endif
48};
49
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
71int fib4_rules_dump(struct net *net, struct notifier_block *nb,
72 struct netlink_ext_ack *extack)
73{
74 return fib_rules_dump(net, nb, AF_INET, extack);
75}
76
77unsigned int fib4_rules_seq_read(struct net *net)
78{
79 return fib_rules_seq_read(net, AF_INET);
80}
81
82int __fib_lookup(struct net *net, struct flowi4 *flp,
83 struct fib_result *res, unsigned int flags)
84{
85 struct fib_lookup_arg arg = {
86 .result = res,
87 .flags = flags,
88 };
89 int err;
90
91
92 l3mdev_update_flow(net, flowi4_to_flowi(flp));
93
94 err = fib_rules_lookup(net->ipv4.rules_ops, flowi4_to_flowi(flp), 0, &arg);
95#ifdef CONFIG_IP_ROUTE_CLASSID
96 if (arg.rule)
97 res->tclassid = ((struct fib4_rule *)arg.rule)->tclassid;
98 else
99 res->tclassid = 0;
100#endif
101
102 if (err == -ESRCH)
103 err = -ENETUNREACH;
104
105 return err;
106}
107EXPORT_SYMBOL_GPL(__fib_lookup);
108
109static int fib4_rule_action(struct fib_rule *rule, struct flowi *flp,
110 int flags, struct fib_lookup_arg *arg)
111{
112 int err = -EAGAIN;
113 struct fib_table *tbl;
114 u32 tb_id;
115
116 switch (rule->action) {
117 case FR_ACT_TO_TBL:
118 break;
119
120 case FR_ACT_UNREACHABLE:
121 return -ENETUNREACH;
122
123 case FR_ACT_PROHIBIT:
124 return -EACCES;
125
126 case FR_ACT_BLACKHOLE:
127 default:
128 return -EINVAL;
129 }
130
131 rcu_read_lock();
132
133 tb_id = fib_rule_get_table(rule, arg);
134 tbl = fib_get_table(rule->fr_net, tb_id);
135 if (tbl)
136 err = fib_table_lookup(tbl, &flp->u.ip4,
137 (struct fib_result *)arg->result,
138 arg->flags);
139
140 rcu_read_unlock();
141 return err;
142}
143
144static bool fib4_rule_suppress(struct fib_rule *rule, struct fib_lookup_arg *arg)
145{
146 struct fib_result *result = (struct fib_result *) arg->result;
147 struct net_device *dev = NULL;
148
149 if (result->fi)
150 dev = result->fi->fib_dev;
151
152
153
154
155 if (result->prefixlen <= rule->suppress_prefixlen)
156 goto suppress_route;
157
158
159
160
161 if (rule->suppress_ifgroup != -1 && dev && dev->group == rule->suppress_ifgroup)
162 goto suppress_route;
163
164 return false;
165
166suppress_route:
167 if (!(arg->flags & FIB_LOOKUP_NOREF))
168 fib_info_put(result->fi);
169 return true;
170}
171
172static int fib4_rule_match(struct fib_rule *rule, struct flowi *fl, int flags)
173{
174 struct fib4_rule *r = (struct fib4_rule *) rule;
175 struct flowi4 *fl4 = &fl->u.ip4;
176 __be32 daddr = fl4->daddr;
177 __be32 saddr = fl4->saddr;
178
179 if (((saddr ^ r->src) & r->srcmask) ||
180 ((daddr ^ r->dst) & r->dstmask))
181 return 0;
182
183 if (r->tos && (r->tos != fl4->flowi4_tos))
184 return 0;
185
186 if (rule->ip_proto && (rule->ip_proto != fl4->flowi4_proto))
187 return 0;
188
189 if (fib_rule_port_range_set(&rule->sport_range) &&
190 !fib_rule_port_inrange(&rule->sport_range, fl4->fl4_sport))
191 return 0;
192
193 if (fib_rule_port_range_set(&rule->dport_range) &&
194 !fib_rule_port_inrange(&rule->dport_range, fl4->fl4_dport))
195 return 0;
196
197 return 1;
198}
199
200static struct fib_table *fib_empty_table(struct net *net)
201{
202 u32 id;
203
204 for (id = 1; id <= RT_TABLE_MAX; id++)
205 if (!fib_get_table(net, id))
206 return fib_new_table(net, id);
207 return NULL;
208}
209
210static const struct nla_policy fib4_rule_policy[FRA_MAX+1] = {
211 FRA_GENERIC_POLICY,
212 [FRA_FLOW] = { .type = NLA_U32 },
213};
214
215static int fib4_rule_configure(struct fib_rule *rule, struct sk_buff *skb,
216 struct fib_rule_hdr *frh,
217 struct nlattr **tb,
218 struct netlink_ext_ack *extack)
219{
220 struct net *net = sock_net(skb->sk);
221 int err = -EINVAL;
222 struct fib4_rule *rule4 = (struct fib4_rule *) rule;
223
224 if (frh->tos & ~IPTOS_TOS_MASK) {
225 NL_SET_ERR_MSG(extack, "Invalid tos");
226 goto errout;
227 }
228
229
230 err = fib_unmerge(net);
231 if (err)
232 goto errout;
233
234 if (rule->table == RT_TABLE_UNSPEC && !rule->l3mdev) {
235 if (rule->action == FR_ACT_TO_TBL) {
236 struct fib_table *table;
237
238 table = fib_empty_table(net);
239 if (!table) {
240 err = -ENOBUFS;
241 goto errout;
242 }
243
244 rule->table = table->tb_id;
245 }
246 }
247
248 if (frh->src_len)
249 rule4->src = nla_get_in_addr(tb[FRA_SRC]);
250
251 if (frh->dst_len)
252 rule4->dst = nla_get_in_addr(tb[FRA_DST]);
253
254#ifdef CONFIG_IP_ROUTE_CLASSID
255 if (tb[FRA_FLOW]) {
256 rule4->tclassid = nla_get_u32(tb[FRA_FLOW]);
257 if (rule4->tclassid)
258 net->ipv4.fib_num_tclassid_users++;
259 }
260#endif
261
262 if (fib_rule_requires_fldissect(rule))
263 net->ipv4.fib_rules_require_fldissect++;
264
265 rule4->src_len = frh->src_len;
266 rule4->srcmask = inet_make_mask(rule4->src_len);
267 rule4->dst_len = frh->dst_len;
268 rule4->dstmask = inet_make_mask(rule4->dst_len);
269 rule4->tos = frh->tos;
270
271 net->ipv4.fib_has_custom_rules = true;
272
273 err = 0;
274errout:
275 return err;
276}
277
278static int fib4_rule_delete(struct fib_rule *rule)
279{
280 struct net *net = rule->fr_net;
281 int err;
282
283
284 err = fib_unmerge(net);
285 if (err)
286 goto errout;
287
288#ifdef CONFIG_IP_ROUTE_CLASSID
289 if (((struct fib4_rule *)rule)->tclassid)
290 net->ipv4.fib_num_tclassid_users--;
291#endif
292 net->ipv4.fib_has_custom_rules = true;
293
294 if (net->ipv4.fib_rules_require_fldissect &&
295 fib_rule_requires_fldissect(rule))
296 net->ipv4.fib_rules_require_fldissect--;
297errout:
298 return err;
299}
300
301static int fib4_rule_compare(struct fib_rule *rule, struct fib_rule_hdr *frh,
302 struct nlattr **tb)
303{
304 struct fib4_rule *rule4 = (struct fib4_rule *) rule;
305
306 if (frh->src_len && (rule4->src_len != frh->src_len))
307 return 0;
308
309 if (frh->dst_len && (rule4->dst_len != frh->dst_len))
310 return 0;
311
312 if (frh->tos && (rule4->tos != frh->tos))
313 return 0;
314
315#ifdef CONFIG_IP_ROUTE_CLASSID
316 if (tb[FRA_FLOW] && (rule4->tclassid != nla_get_u32(tb[FRA_FLOW])))
317 return 0;
318#endif
319
320 if (frh->src_len && (rule4->src != nla_get_in_addr(tb[FRA_SRC])))
321 return 0;
322
323 if (frh->dst_len && (rule4->dst != nla_get_in_addr(tb[FRA_DST])))
324 return 0;
325
326 return 1;
327}
328
329static int fib4_rule_fill(struct fib_rule *rule, struct sk_buff *skb,
330 struct fib_rule_hdr *frh)
331{
332 struct fib4_rule *rule4 = (struct fib4_rule *) rule;
333
334 frh->dst_len = rule4->dst_len;
335 frh->src_len = rule4->src_len;
336 frh->tos = rule4->tos;
337
338 if ((rule4->dst_len &&
339 nla_put_in_addr(skb, FRA_DST, rule4->dst)) ||
340 (rule4->src_len &&
341 nla_put_in_addr(skb, FRA_SRC, rule4->src)))
342 goto nla_put_failure;
343#ifdef CONFIG_IP_ROUTE_CLASSID
344 if (rule4->tclassid &&
345 nla_put_u32(skb, FRA_FLOW, rule4->tclassid))
346 goto nla_put_failure;
347#endif
348 return 0;
349
350nla_put_failure:
351 return -ENOBUFS;
352}
353
354static size_t fib4_rule_nlmsg_payload(struct fib_rule *rule)
355{
356 return nla_total_size(4)
357 + nla_total_size(4)
358 + nla_total_size(4);
359}
360
361static void fib4_rule_flush_cache(struct fib_rules_ops *ops)
362{
363 rt_cache_flush(ops->fro_net);
364}
365
366static const struct fib_rules_ops __net_initconst fib4_rules_ops_template = {
367 .family = AF_INET,
368 .rule_size = sizeof(struct fib4_rule),
369 .addr_size = sizeof(u32),
370 .action = fib4_rule_action,
371 .suppress = fib4_rule_suppress,
372 .match = fib4_rule_match,
373 .configure = fib4_rule_configure,
374 .delete = fib4_rule_delete,
375 .compare = fib4_rule_compare,
376 .fill = fib4_rule_fill,
377 .nlmsg_payload = fib4_rule_nlmsg_payload,
378 .flush_cache = fib4_rule_flush_cache,
379 .nlgroup = RTNLGRP_IPV4_RULE,
380 .policy = fib4_rule_policy,
381 .owner = THIS_MODULE,
382};
383
384static int fib_default_rules_init(struct fib_rules_ops *ops)
385{
386 int err;
387
388 err = fib_default_rule_add(ops, 0, RT_TABLE_LOCAL, 0);
389 if (err < 0)
390 return err;
391 err = fib_default_rule_add(ops, 0x7FFE, RT_TABLE_MAIN, 0);
392 if (err < 0)
393 return err;
394 err = fib_default_rule_add(ops, 0x7FFF, RT_TABLE_DEFAULT, 0);
395 if (err < 0)
396 return err;
397 return 0;
398}
399
400int __net_init fib4_rules_init(struct net *net)
401{
402 int err;
403 struct fib_rules_ops *ops;
404
405 ops = fib_rules_register(&fib4_rules_ops_template, net);
406 if (IS_ERR(ops))
407 return PTR_ERR(ops);
408
409 err = fib_default_rules_init(ops);
410 if (err < 0)
411 goto fail;
412 net->ipv4.rules_ops = ops;
413 net->ipv4.fib_has_custom_rules = false;
414 net->ipv4.fib_rules_require_fldissect = 0;
415 return 0;
416
417fail:
418
419 fib_rules_unregister(ops);
420 return err;
421}
422
423void __net_exit fib4_rules_exit(struct net *net)
424{
425 fib_rules_unregister(net->ipv4.rules_ops);
426}
427