]> git.karo-electronics.de Git - karo-tx-linux.git/blob - net/ipv4/fib_rules.c
ipv4: fib_rules: Add notifier info to FIB rules notifications
[karo-tx-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/fib_rules.h>
35
36 struct 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
50 static 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
59 bool 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 }
69 EXPORT_SYMBOL_GPL(fib4_rule_default);
70
71 int __fib_lookup(struct net *net, struct flowi4 *flp,
72                  struct fib_result *res, unsigned int flags)
73 {
74         struct fib_lookup_arg arg = {
75                 .result = res,
76                 .flags = flags,
77         };
78         int err;
79
80         /* update flow if oif or iif point to device enslaved to l3mdev */
81         l3mdev_update_flow(net, flowi4_to_flowi(flp));
82
83         err = fib_rules_lookup(net->ipv4.rules_ops, flowi4_to_flowi(flp), 0, &arg);
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
90
91         if (err == -ESRCH)
92                 err = -ENETUNREACH;
93
94         return err;
95 }
96 EXPORT_SYMBOL_GPL(__fib_lookup);
97
98 static int fib4_rule_action(struct fib_rule *rule, struct flowi *flp,
99                             int flags, struct fib_lookup_arg *arg)
100 {
101         int err = -EAGAIN;
102         struct fib_table *tbl;
103         u32 tb_id;
104
105         switch (rule->action) {
106         case FR_ACT_TO_TBL:
107                 break;
108
109         case FR_ACT_UNREACHABLE:
110                 return -ENETUNREACH;
111
112         case FR_ACT_PROHIBIT:
113                 return -EACCES;
114
115         case FR_ACT_BLACKHOLE:
116         default:
117                 return -EINVAL;
118         }
119
120         rcu_read_lock();
121
122         tb_id = fib_rule_get_table(rule, arg);
123         tbl = fib_get_table(rule->fr_net, tb_id);
124         if (tbl)
125                 err = fib_table_lookup(tbl, &flp->u.ip4,
126                                        (struct fib_result *)arg->result,
127                                        arg->flags);
128
129         rcu_read_unlock();
130         return err;
131 }
132
133 static bool fib4_rule_suppress(struct fib_rule *rule, struct fib_lookup_arg *arg)
134 {
135         struct fib_result *result = (struct fib_result *) arg->result;
136         struct net_device *dev = NULL;
137
138         if (result->fi)
139                 dev = result->fi->fib_dev;
140
141         /* do not accept result if the route does
142          * not meet the required prefix length
143          */
144         if (result->prefixlen <= rule->suppress_prefixlen)
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
153         return false;
154
155 suppress_route:
156         if (!(arg->flags & FIB_LOOKUP_NOREF))
157                 fib_info_put(result->fi);
158         return true;
159 }
160
161 static int fib4_rule_match(struct fib_rule *rule, struct flowi *fl, int flags)
162 {
163         struct fib4_rule *r = (struct fib4_rule *) rule;
164         struct flowi4 *fl4 = &fl->u.ip4;
165         __be32 daddr = fl4->daddr;
166         __be32 saddr = fl4->saddr;
167
168         if (((saddr ^ r->src) & r->srcmask) ||
169             ((daddr ^ r->dst) & r->dstmask))
170                 return 0;
171
172         if (r->tos && (r->tos != fl4->flowi4_tos))
173                 return 0;
174
175         return 1;
176 }
177
178 static struct fib_table *fib_empty_table(struct net *net)
179 {
180         u32 id;
181
182         for (id = 1; id <= RT_TABLE_MAX; id++)
183                 if (!fib_get_table(net, id))
184                         return fib_new_table(net, id);
185         return NULL;
186 }
187
188 static int call_fib_rule_notifiers(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_notifiers(net, event_type, &info.info);
197 }
198
199 void fib_rules_notify(struct net *net, struct notifier_block *nb)
200 {
201         struct fib_notifier_info info;
202
203         if (net->ipv4.fib_has_custom_rules)
204                 call_fib_notifier(nb, net, FIB_EVENT_RULE_ADD, &info);
205 }
206
207 static const struct nla_policy fib4_rule_policy[FRA_MAX+1] = {
208         FRA_GENERIC_POLICY,
209         [FRA_FLOW]      = { .type = NLA_U32 },
210 };
211
212 static int fib4_rule_configure(struct fib_rule *rule, struct sk_buff *skb,
213                                struct fib_rule_hdr *frh,
214                                struct nlattr **tb)
215 {
216         struct net *net = sock_net(skb->sk);
217         int err = -EINVAL;
218         struct fib4_rule *rule4 = (struct fib4_rule *) rule;
219
220         if (frh->tos & ~IPTOS_TOS_MASK)
221                 goto errout;
222
223         /* split local/main if they are not already split */
224         err = fib_unmerge(net);
225         if (err)
226                 goto errout;
227
228         if (rule->table == RT_TABLE_UNSPEC && !rule->l3mdev) {
229                 if (rule->action == FR_ACT_TO_TBL) {
230                         struct fib_table *table;
231
232                         table = fib_empty_table(net);
233                         if (!table) {
234                                 err = -ENOBUFS;
235                                 goto errout;
236                         }
237
238                         rule->table = table->tb_id;
239                 }
240         }
241
242         if (frh->src_len)
243                 rule4->src = nla_get_in_addr(tb[FRA_SRC]);
244
245         if (frh->dst_len)
246                 rule4->dst = nla_get_in_addr(tb[FRA_DST]);
247
248 #ifdef CONFIG_IP_ROUTE_CLASSID
249         if (tb[FRA_FLOW]) {
250                 rule4->tclassid = nla_get_u32(tb[FRA_FLOW]);
251                 if (rule4->tclassid)
252                         net->ipv4.fib_num_tclassid_users++;
253         }
254 #endif
255
256         rule4->src_len = frh->src_len;
257         rule4->srcmask = inet_make_mask(rule4->src_len);
258         rule4->dst_len = frh->dst_len;
259         rule4->dstmask = inet_make_mask(rule4->dst_len);
260         rule4->tos = frh->tos;
261
262         net->ipv4.fib_has_custom_rules = true;
263         call_fib_rule_notifiers(net, FIB_EVENT_RULE_ADD, rule);
264
265         err = 0;
266 errout:
267         return err;
268 }
269
270 static int fib4_rule_delete(struct fib_rule *rule)
271 {
272         struct net *net = rule->fr_net;
273         int err;
274
275         /* split local/main if they are not already split */
276         err = fib_unmerge(net);
277         if (err)
278                 goto errout;
279
280 #ifdef CONFIG_IP_ROUTE_CLASSID
281         if (((struct fib4_rule *)rule)->tclassid)
282                 net->ipv4.fib_num_tclassid_users--;
283 #endif
284         net->ipv4.fib_has_custom_rules = true;
285         call_fib_rule_notifiers(net, FIB_EVENT_RULE_DEL, rule);
286 errout:
287         return err;
288 }
289
290 static int fib4_rule_compare(struct fib_rule *rule, struct fib_rule_hdr *frh,
291                              struct nlattr **tb)
292 {
293         struct fib4_rule *rule4 = (struct fib4_rule *) rule;
294
295         if (frh->src_len && (rule4->src_len != frh->src_len))
296                 return 0;
297
298         if (frh->dst_len && (rule4->dst_len != frh->dst_len))
299                 return 0;
300
301         if (frh->tos && (rule4->tos != frh->tos))
302                 return 0;
303
304 #ifdef CONFIG_IP_ROUTE_CLASSID
305         if (tb[FRA_FLOW] && (rule4->tclassid != nla_get_u32(tb[FRA_FLOW])))
306                 return 0;
307 #endif
308
309         if (frh->src_len && (rule4->src != nla_get_in_addr(tb[FRA_SRC])))
310                 return 0;
311
312         if (frh->dst_len && (rule4->dst != nla_get_in_addr(tb[FRA_DST])))
313                 return 0;
314
315         return 1;
316 }
317
318 static int fib4_rule_fill(struct fib_rule *rule, struct sk_buff *skb,
319                           struct fib_rule_hdr *frh)
320 {
321         struct fib4_rule *rule4 = (struct fib4_rule *) rule;
322
323         frh->dst_len = rule4->dst_len;
324         frh->src_len = rule4->src_len;
325         frh->tos = rule4->tos;
326
327         if ((rule4->dst_len &&
328              nla_put_in_addr(skb, FRA_DST, rule4->dst)) ||
329             (rule4->src_len &&
330              nla_put_in_addr(skb, FRA_SRC, rule4->src)))
331                 goto nla_put_failure;
332 #ifdef CONFIG_IP_ROUTE_CLASSID
333         if (rule4->tclassid &&
334             nla_put_u32(skb, FRA_FLOW, rule4->tclassid))
335                 goto nla_put_failure;
336 #endif
337         return 0;
338
339 nla_put_failure:
340         return -ENOBUFS;
341 }
342
343 static size_t fib4_rule_nlmsg_payload(struct fib_rule *rule)
344 {
345         return nla_total_size(4) /* dst */
346                + nla_total_size(4) /* src */
347                + nla_total_size(4); /* flow */
348 }
349
350 static void fib4_rule_flush_cache(struct fib_rules_ops *ops)
351 {
352         rt_cache_flush(ops->fro_net);
353 }
354
355 static const struct fib_rules_ops __net_initconst fib4_rules_ops_template = {
356         .family         = AF_INET,
357         .rule_size      = sizeof(struct fib4_rule),
358         .addr_size      = sizeof(u32),
359         .action         = fib4_rule_action,
360         .suppress       = fib4_rule_suppress,
361         .match          = fib4_rule_match,
362         .configure      = fib4_rule_configure,
363         .delete         = fib4_rule_delete,
364         .compare        = fib4_rule_compare,
365         .fill           = fib4_rule_fill,
366         .nlmsg_payload  = fib4_rule_nlmsg_payload,
367         .flush_cache    = fib4_rule_flush_cache,
368         .nlgroup        = RTNLGRP_IPV4_RULE,
369         .policy         = fib4_rule_policy,
370         .owner          = THIS_MODULE,
371 };
372
373 static int fib_default_rules_init(struct fib_rules_ops *ops)
374 {
375         int err;
376
377         err = fib_default_rule_add(ops, 0, RT_TABLE_LOCAL, 0);
378         if (err < 0)
379                 return err;
380         err = fib_default_rule_add(ops, 0x7FFE, RT_TABLE_MAIN, 0);
381         if (err < 0)
382                 return err;
383         err = fib_default_rule_add(ops, 0x7FFF, RT_TABLE_DEFAULT, 0);
384         if (err < 0)
385                 return err;
386         return 0;
387 }
388
389 int __net_init fib4_rules_init(struct net *net)
390 {
391         int err;
392         struct fib_rules_ops *ops;
393
394         ops = fib_rules_register(&fib4_rules_ops_template, net);
395         if (IS_ERR(ops))
396                 return PTR_ERR(ops);
397
398         err = fib_default_rules_init(ops);
399         if (err < 0)
400                 goto fail;
401         net->ipv4.rules_ops = ops;
402         net->ipv4.fib_has_custom_rules = false;
403         return 0;
404
405 fail:
406         /* also cleans all rules already added */
407         fib_rules_unregister(ops);
408         return err;
409 }
410
411 void __net_exit fib4_rules_exit(struct net *net)
412 {
413         fib_rules_unregister(net->ipv4.rules_ops);
414 }