]> asedeno.scripts.mit.edu Git - linux.git/blob - net/ipv4/fib_rules.c
ab06fd73b343fec8cc4182733873889d0379cb33
[linux.git] / net / ipv4 / fib_rules.c
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  *
8  * Authors:     Alexey Kuznetsov, <kuznet@ms2.inr.ac.ru>
9  *              Thomas Graf <tgraf@suug.ch>
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:
17  *              Rani Assaf      :       local_rule cannot be deleted
18  *              Marc Boucher    :       routing by fwmark
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/nexthop.h>
35 #include <net/fib_rules.h>
36
37 struct fib4_rule {
38         struct fib_rule         common;
39         u8                      dst_len;
40         u8                      src_len;
41         u8                      tos;
42         __be32                  src;
43         __be32                  srcmask;
44         __be32                  dst;
45         __be32                  dstmask;
46 #ifdef CONFIG_IP_ROUTE_CLASSID
47         u32                     tclassid;
48 #endif
49 };
50
51 static bool fib4_rule_matchall(const struct fib_rule *rule)
52 {
53         struct fib4_rule *r = container_of(rule, struct fib4_rule, common);
54
55         if (r->dst_len || r->src_len || r->tos)
56                 return false;
57         return fib_rule_matchall(rule);
58 }
59
60 bool fib4_rule_default(const struct fib_rule *rule)
61 {
62         if (!fib4_rule_matchall(rule) || rule->action != FR_ACT_TO_TBL ||
63             rule->l3mdev)
64                 return false;
65         if (rule->table != RT_TABLE_LOCAL && rule->table != RT_TABLE_MAIN &&
66             rule->table != RT_TABLE_DEFAULT)
67                 return false;
68         return true;
69 }
70 EXPORT_SYMBOL_GPL(fib4_rule_default);
71
72 int fib4_rules_dump(struct net *net, struct notifier_block *nb)
73 {
74         return fib_rules_dump(net, nb, AF_INET);
75 }
76
77 unsigned int fib4_rules_seq_read(struct net *net)
78 {
79         return fib_rules_seq_read(net, AF_INET);
80 }
81
82 int __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         /* update flow if oif or iif point to device enslaved to l3mdev */
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 }
107 EXPORT_SYMBOL_GPL(__fib_lookup);
108
109 static 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
144 static 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                 struct fib_nh *nh = fib_info_nh(result->fi, 0);
151
152                 dev = nh->fib_nh_dev;
153         }
154
155         /* do not accept result if the route does
156          * not meet the required prefix length
157          */
158         if (result->prefixlen <= rule->suppress_prefixlen)
159                 goto suppress_route;
160
161         /* do not accept result if the route uses a device
162          * belonging to a forbidden interface group
163          */
164         if (rule->suppress_ifgroup != -1 && dev && dev->group == rule->suppress_ifgroup)
165                 goto suppress_route;
166
167         return false;
168
169 suppress_route:
170         if (!(arg->flags & FIB_LOOKUP_NOREF))
171                 fib_info_put(result->fi);
172         return true;
173 }
174
175 static int fib4_rule_match(struct fib_rule *rule, struct flowi *fl, int flags)
176 {
177         struct fib4_rule *r = (struct fib4_rule *) rule;
178         struct flowi4 *fl4 = &fl->u.ip4;
179         __be32 daddr = fl4->daddr;
180         __be32 saddr = fl4->saddr;
181
182         if (((saddr ^ r->src) & r->srcmask) ||
183             ((daddr ^ r->dst) & r->dstmask))
184                 return 0;
185
186         if (r->tos && (r->tos != fl4->flowi4_tos))
187                 return 0;
188
189         if (rule->ip_proto && (rule->ip_proto != fl4->flowi4_proto))
190                 return 0;
191
192         if (fib_rule_port_range_set(&rule->sport_range) &&
193             !fib_rule_port_inrange(&rule->sport_range, fl4->fl4_sport))
194                 return 0;
195
196         if (fib_rule_port_range_set(&rule->dport_range) &&
197             !fib_rule_port_inrange(&rule->dport_range, fl4->fl4_dport))
198                 return 0;
199
200         return 1;
201 }
202
203 static struct fib_table *fib_empty_table(struct net *net)
204 {
205         u32 id = 1;
206
207         while (1) {
208                 if (!fib_get_table(net, id))
209                         return fib_new_table(net, id);
210
211                 if (id++ == RT_TABLE_MAX)
212                         break;
213         }
214         return NULL;
215 }
216
217 static const struct nla_policy fib4_rule_policy[FRA_MAX+1] = {
218         FRA_GENERIC_POLICY,
219         [FRA_FLOW]      = { .type = NLA_U32 },
220 };
221
222 static int fib4_rule_configure(struct fib_rule *rule, struct sk_buff *skb,
223                                struct fib_rule_hdr *frh,
224                                struct nlattr **tb,
225                                struct netlink_ext_ack *extack)
226 {
227         struct net *net = sock_net(skb->sk);
228         int err = -EINVAL;
229         struct fib4_rule *rule4 = (struct fib4_rule *) rule;
230
231         if (frh->tos & ~IPTOS_TOS_MASK) {
232                 NL_SET_ERR_MSG(extack, "Invalid tos");
233                 goto errout;
234         }
235
236         /* split local/main if they are not already split */
237         err = fib_unmerge(net);
238         if (err)
239                 goto errout;
240
241         if (rule->table == RT_TABLE_UNSPEC && !rule->l3mdev) {
242                 if (rule->action == FR_ACT_TO_TBL) {
243                         struct fib_table *table;
244
245                         table = fib_empty_table(net);
246                         if (!table) {
247                                 err = -ENOBUFS;
248                                 goto errout;
249                         }
250
251                         rule->table = table->tb_id;
252                 }
253         }
254
255         if (frh->src_len)
256                 rule4->src = nla_get_in_addr(tb[FRA_SRC]);
257
258         if (frh->dst_len)
259                 rule4->dst = nla_get_in_addr(tb[FRA_DST]);
260
261 #ifdef CONFIG_IP_ROUTE_CLASSID
262         if (tb[FRA_FLOW]) {
263                 rule4->tclassid = nla_get_u32(tb[FRA_FLOW]);
264                 if (rule4->tclassid)
265                         net->ipv4.fib_num_tclassid_users++;
266         }
267 #endif
268
269         if (fib_rule_requires_fldissect(rule))
270                 net->ipv4.fib_rules_require_fldissect++;
271
272         rule4->src_len = frh->src_len;
273         rule4->srcmask = inet_make_mask(rule4->src_len);
274         rule4->dst_len = frh->dst_len;
275         rule4->dstmask = inet_make_mask(rule4->dst_len);
276         rule4->tos = frh->tos;
277
278         net->ipv4.fib_has_custom_rules = true;
279
280         err = 0;
281 errout:
282         return err;
283 }
284
285 static int fib4_rule_delete(struct fib_rule *rule)
286 {
287         struct net *net = rule->fr_net;
288         int err;
289
290         /* split local/main if they are not already split */
291         err = fib_unmerge(net);
292         if (err)
293                 goto errout;
294
295 #ifdef CONFIG_IP_ROUTE_CLASSID
296         if (((struct fib4_rule *)rule)->tclassid)
297                 net->ipv4.fib_num_tclassid_users--;
298 #endif
299         net->ipv4.fib_has_custom_rules = true;
300
301         if (net->ipv4.fib_rules_require_fldissect &&
302             fib_rule_requires_fldissect(rule))
303                 net->ipv4.fib_rules_require_fldissect--;
304 errout:
305         return err;
306 }
307
308 static int fib4_rule_compare(struct fib_rule *rule, struct fib_rule_hdr *frh,
309                              struct nlattr **tb)
310 {
311         struct fib4_rule *rule4 = (struct fib4_rule *) rule;
312
313         if (frh->src_len && (rule4->src_len != frh->src_len))
314                 return 0;
315
316         if (frh->dst_len && (rule4->dst_len != frh->dst_len))
317                 return 0;
318
319         if (frh->tos && (rule4->tos != frh->tos))
320                 return 0;
321
322 #ifdef CONFIG_IP_ROUTE_CLASSID
323         if (tb[FRA_FLOW] && (rule4->tclassid != nla_get_u32(tb[FRA_FLOW])))
324                 return 0;
325 #endif
326
327         if (frh->src_len && (rule4->src != nla_get_in_addr(tb[FRA_SRC])))
328                 return 0;
329
330         if (frh->dst_len && (rule4->dst != nla_get_in_addr(tb[FRA_DST])))
331                 return 0;
332
333         return 1;
334 }
335
336 static int fib4_rule_fill(struct fib_rule *rule, struct sk_buff *skb,
337                           struct fib_rule_hdr *frh)
338 {
339         struct fib4_rule *rule4 = (struct fib4_rule *) rule;
340
341         frh->dst_len = rule4->dst_len;
342         frh->src_len = rule4->src_len;
343         frh->tos = rule4->tos;
344
345         if ((rule4->dst_len &&
346              nla_put_in_addr(skb, FRA_DST, rule4->dst)) ||
347             (rule4->src_len &&
348              nla_put_in_addr(skb, FRA_SRC, rule4->src)))
349                 goto nla_put_failure;
350 #ifdef CONFIG_IP_ROUTE_CLASSID
351         if (rule4->tclassid &&
352             nla_put_u32(skb, FRA_FLOW, rule4->tclassid))
353                 goto nla_put_failure;
354 #endif
355         return 0;
356
357 nla_put_failure:
358         return -ENOBUFS;
359 }
360
361 static size_t fib4_rule_nlmsg_payload(struct fib_rule *rule)
362 {
363         return nla_total_size(4) /* dst */
364                + nla_total_size(4) /* src */
365                + nla_total_size(4); /* flow */
366 }
367
368 static void fib4_rule_flush_cache(struct fib_rules_ops *ops)
369 {
370         rt_cache_flush(ops->fro_net);
371 }
372
373 static const struct fib_rules_ops __net_initconst fib4_rules_ops_template = {
374         .family         = AF_INET,
375         .rule_size      = sizeof(struct fib4_rule),
376         .addr_size      = sizeof(u32),
377         .action         = fib4_rule_action,
378         .suppress       = fib4_rule_suppress,
379         .match          = fib4_rule_match,
380         .configure      = fib4_rule_configure,
381         .delete         = fib4_rule_delete,
382         .compare        = fib4_rule_compare,
383         .fill           = fib4_rule_fill,
384         .nlmsg_payload  = fib4_rule_nlmsg_payload,
385         .flush_cache    = fib4_rule_flush_cache,
386         .nlgroup        = RTNLGRP_IPV4_RULE,
387         .policy         = fib4_rule_policy,
388         .owner          = THIS_MODULE,
389 };
390
391 static int fib_default_rules_init(struct fib_rules_ops *ops)
392 {
393         int err;
394
395         err = fib_default_rule_add(ops, 0, RT_TABLE_LOCAL, 0);
396         if (err < 0)
397                 return err;
398         err = fib_default_rule_add(ops, 0x7FFE, RT_TABLE_MAIN, 0);
399         if (err < 0)
400                 return err;
401         err = fib_default_rule_add(ops, 0x7FFF, RT_TABLE_DEFAULT, 0);
402         if (err < 0)
403                 return err;
404         return 0;
405 }
406
407 int __net_init fib4_rules_init(struct net *net)
408 {
409         int err;
410         struct fib_rules_ops *ops;
411
412         ops = fib_rules_register(&fib4_rules_ops_template, net);
413         if (IS_ERR(ops))
414                 return PTR_ERR(ops);
415
416         err = fib_default_rules_init(ops);
417         if (err < 0)
418                 goto fail;
419         net->ipv4.rules_ops = ops;
420         net->ipv4.fib_has_custom_rules = false;
421         net->ipv4.fib_rules_require_fldissect = 0;
422         return 0;
423
424 fail:
425         /* also cleans all rules already added */
426         fib_rules_unregister(ops);
427         return err;
428 }
429
430 void __net_exit fib4_rules_exit(struct net *net)
431 {
432         fib_rules_unregister(net->ipv4.rules_ops);
433 }