Blame view

net/ipv4/fib_rules.c 9.06 KB
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1
2
3
4
5
6
7
  /*
   * INET		An implementation of the TCP/IP protocol suite for the LINUX
   *		operating system.  INET is implemented using the  BSD Socket
   *		interface as the means of communication with the user level.
   *
   *		IPv4 Forwarding Information Base: policy rules.
   *
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
8
   * Authors:	Alexey Kuznetsov, <kuznet@ms2.inr.ac.ru>
6a31d2a97   Eric Dumazet   fib: cleanups
9
   *		Thomas Graf <tgraf@suug.ch>
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
10
11
12
13
14
15
16
   *
   *		This program is free software; you can redistribute it and/or
   *		modify it under the terms of the GNU General Public License
   *		as published by the Free Software Foundation; either version
   *		2 of the License, or (at your option) any later version.
   *
   * Fixes:
6a31d2a97   Eric Dumazet   fib: cleanups
17
   *		Rani Assaf	:	local_rule cannot be deleted
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
18
19
   *		Marc Boucher	:	routing by fwmark
   */
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
20
21
  #include <linux/types.h>
  #include <linux/kernel.h>
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
22
  #include <linux/netdevice.h>
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
23
  #include <linux/netlink.h>
e1ef4bf23   Thomas Graf   [IPV4]: Use Proto...
24
  #include <linux/inetdevice.h>
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
25
  #include <linux/init.h>
7b204afd4   Robert Olsson   [IPV4]: Use RCU l...
26
27
  #include <linux/list.h>
  #include <linux/rcupdate.h>
bc3b2d7fb   Paul Gortmaker   net: Add export.h...
28
  #include <linux/export.h>
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
29
  #include <net/ip.h>
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
30
31
  #include <net/route.h>
  #include <net/tcp.h>
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
32
  #include <net/ip_fib.h>
e1ef4bf23   Thomas Graf   [IPV4]: Use Proto...
33
  #include <net/fib_rules.h>
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
34

6a31d2a97   Eric Dumazet   fib: cleanups
35
  struct fib4_rule {
e1ef4bf23   Thomas Graf   [IPV4]: Use Proto...
36
37
38
39
  	struct fib_rule		common;
  	u8			dst_len;
  	u8			src_len;
  	u8			tos;
81f7bf6cb   Al Viro   [IPV4]: net/ipv4/...
40
41
42
43
  	__be32			src;
  	__be32			srcmask;
  	__be32			dst;
  	__be32			dstmask;
c7066f70d   Patrick McHardy   netfilter: fix Kc...
44
  #ifdef CONFIG_IP_ROUTE_CLASSID
e1ef4bf23   Thomas Graf   [IPV4]: Use Proto...
45
  	u32			tclassid;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
46
  #endif
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
47
  };
3c71006d1   Ido Schimmel   ipv4: fib_rules: ...
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
  static bool fib4_rule_matchall(const struct fib_rule *rule)
  {
  	struct fib4_rule *r = container_of(rule, struct fib4_rule, common);
  
  	if (r->dst_len || r->src_len || r->tos)
  		return false;
  	return fib_rule_matchall(rule);
  }
  
  bool fib4_rule_default(const struct fib_rule *rule)
  {
  	if (!fib4_rule_matchall(rule) || rule->action != FR_ACT_TO_TBL ||
  	    rule->l3mdev)
  		return false;
  	if (rule->table != RT_TABLE_LOCAL && rule->table != RT_TABLE_MAIN &&
  	    rule->table != RT_TABLE_DEFAULT)
  		return false;
  	return true;
  }
  EXPORT_SYMBOL_GPL(fib4_rule_default);
1b2a44408   Ido Schimmel   net: fib_rules: I...
68
69
70
71
72
73
74
75
76
  int fib4_rules_dump(struct net *net, struct notifier_block *nb)
  {
  	return fib_rules_dump(net, nb, AF_INET);
  }
  
  unsigned int fib4_rules_seq_read(struct net *net)
  {
  	return fib_rules_seq_read(net, AF_INET);
  }
0eeb075fa   Andy Gospodarek   net: ipv4 sysctl ...
77
78
  int __fib_lookup(struct net *net, struct flowi4 *flp,
  		 struct fib_result *res, unsigned int flags)
e1ef4bf23   Thomas Graf   [IPV4]: Use Proto...
79
80
81
  {
  	struct fib_lookup_arg arg = {
  		.result = res,
0eeb075fa   Andy Gospodarek   net: ipv4 sysctl ...
82
  		.flags = flags,
e1ef4bf23   Thomas Graf   [IPV4]: Use Proto...
83
84
  	};
  	int err;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
85

9ee0034b8   David Ahern   net: flow: Add l3...
86
87
  	/* update flow if oif or iif point to device enslaved to l3mdev */
  	l3mdev_update_flow(net, flowi4_to_flowi(flp));
22bd5b9b1   David S. Miller   ipv4: Pass ipv4 f...
88
  	err = fib_rules_lookup(net->ipv4.rules_ops, flowi4_to_flowi(flp), 0, &arg);
85b91b033   David S. Miller   ipv4: Don't store...
89
90
91
92
93
94
  #ifdef CONFIG_IP_ROUTE_CLASSID
  	if (arg.rule)
  		res->tclassid = ((struct fib4_rule *)arg.rule)->tclassid;
  	else
  		res->tclassid = 0;
  #endif
49dd18ba4   Panu Matilainen   ipv4: Fix incorre...
95
96
97
  
  	if (err == -ESRCH)
  		err = -ENETUNREACH;
e1ef4bf23   Thomas Graf   [IPV4]: Use Proto...
98
99
  	return err;
  }
f4530fa57   David S. Miller   ipv4: Avoid overh...
100
  EXPORT_SYMBOL_GPL(__fib_lookup);
e1ef4bf23   Thomas Graf   [IPV4]: Use Proto...
101

8ce11e6a9   Adrian Bunk   [NET]: Make code ...
102
103
  static int fib4_rule_action(struct fib_rule *rule, struct flowi *flp,
  			    int flags, struct fib_lookup_arg *arg)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
104
  {
e1ef4bf23   Thomas Graf   [IPV4]: Use Proto...
105
106
  	int err = -EAGAIN;
  	struct fib_table *tbl;
96c63fa73   David Ahern   net: Add l3mdev rule
107
  	u32 tb_id;
e1ef4bf23   Thomas Graf   [IPV4]: Use Proto...
108
109
110
111
112
113
  
  	switch (rule->action) {
  	case FR_ACT_TO_TBL:
  		break;
  
  	case FR_ACT_UNREACHABLE:
345e9b542   Alexander Duyck   fib_trie: Push rc...
114
  		return -ENETUNREACH;
e1ef4bf23   Thomas Graf   [IPV4]: Use Proto...
115
116
  
  	case FR_ACT_PROHIBIT:
345e9b542   Alexander Duyck   fib_trie: Push rc...
117
  		return -EACCES;
e1ef4bf23   Thomas Graf   [IPV4]: Use Proto...
118
119
120
  
  	case FR_ACT_BLACKHOLE:
  	default:
345e9b542   Alexander Duyck   fib_trie: Push rc...
121
  		return -EINVAL;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
122
  	}
e1ef4bf23   Thomas Graf   [IPV4]: Use Proto...
123

345e9b542   Alexander Duyck   fib_trie: Push rc...
124
  	rcu_read_lock();
96c63fa73   David Ahern   net: Add l3mdev rule
125
126
  	tb_id = fib_rule_get_table(rule, arg);
  	tbl = fib_get_table(rule->fr_net, tb_id);
345e9b542   Alexander Duyck   fib_trie: Push rc...
127
128
129
130
  	if (tbl)
  		err = fib_table_lookup(tbl, &flp->u.ip4,
  				       (struct fib_result *)arg->result,
  				       arg->flags);
e1ef4bf23   Thomas Graf   [IPV4]: Use Proto...
131

345e9b542   Alexander Duyck   fib_trie: Push rc...
132
  	rcu_read_unlock();
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
133
134
  	return err;
  }
7764a45a8   Stefan Tomanek   fib_rules: add .s...
135
136
  static bool fib4_rule_suppress(struct fib_rule *rule, struct fib_lookup_arg *arg)
  {
6ef94cfaf   Stefan Tomanek   fib_rules: add ro...
137
  	struct fib_result *result = (struct fib_result *) arg->result;
673498b8e   Stefan Tomanek   inet: fix NULL po...
138
139
140
141
  	struct net_device *dev = NULL;
  
  	if (result->fi)
  		dev = result->fi->fib_dev;
6ef94cfaf   Stefan Tomanek   fib_rules: add ro...
142

7764a45a8   Stefan Tomanek   fib_rules: add .s...
143
144
145
  	/* do not accept result if the route does
  	 * not meet the required prefix length
  	 */
73f5698e7   Stefan Tomanek   fib_rules: fix su...
146
  	if (result->prefixlen <= rule->suppress_prefixlen)
6ef94cfaf   Stefan Tomanek   fib_rules: add ro...
147
148
149
150
151
152
153
  		goto suppress_route;
  
  	/* do not accept result if the route uses a device
  	 * belonging to a forbidden interface group
  	 */
  	if (rule->suppress_ifgroup != -1 && dev && dev->group == rule->suppress_ifgroup)
  		goto suppress_route;
7764a45a8   Stefan Tomanek   fib_rules: add .s...
154
  	return false;
6ef94cfaf   Stefan Tomanek   fib_rules: add ro...
155
156
157
158
159
  
  suppress_route:
  	if (!(arg->flags & FIB_LOOKUP_NOREF))
  		fib_info_put(result->fi);
  	return true;
7764a45a8   Stefan Tomanek   fib_rules: add .s...
160
  }
e1ef4bf23   Thomas Graf   [IPV4]: Use Proto...
161

e1ef4bf23   Thomas Graf   [IPV4]: Use Proto...
162
163
164
  static int fib4_rule_match(struct fib_rule *rule, struct flowi *fl, int flags)
  {
  	struct fib4_rule *r = (struct fib4_rule *) rule;
9ade22861   David S. Miller   ipv4: Use flowi4 ...
165
166
167
  	struct flowi4 *fl4 = &fl->u.ip4;
  	__be32 daddr = fl4->daddr;
  	__be32 saddr = fl4->saddr;
e1ef4bf23   Thomas Graf   [IPV4]: Use Proto...
168
169
170
171
  
  	if (((saddr ^ r->src) & r->srcmask) ||
  	    ((daddr ^ r->dst) & r->dstmask))
  		return 0;
9ade22861   David S. Miller   ipv4: Use flowi4 ...
172
  	if (r->tos && (r->tos != fl4->flowi4_tos))
e1ef4bf23   Thomas Graf   [IPV4]: Use Proto...
173
  		return 0;
e1ef4bf23   Thomas Graf   [IPV4]: Use Proto...
174
175
  	return 1;
  }
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
176

8ad4942cd   Denis V. Lunev   [NETNS]: Add netn...
177
  static struct fib_table *fib_empty_table(struct net *net)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
178
  {
2dfe55b47   Patrick McHardy   [NET]: Use u32 fo...
179
  	u32 id;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
180
181
  
  	for (id = 1; id <= RT_TABLE_MAX; id++)
51456b291   Ian Morris   ipv4: coding styl...
182
  		if (!fib_get_table(net, id))
8ad4942cd   Denis V. Lunev   [NETNS]: Add netn...
183
  			return fib_new_table(net, id);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
184
185
  	return NULL;
  }
ef7c79ed6   Patrick McHardy   [NETLINK]: Mark n...
186
  static const struct nla_policy fib4_rule_policy[FRA_MAX+1] = {
1f6c9557e   Thomas Graf   [NET] rules: Shar...
187
  	FRA_GENERIC_POLICY,
e1ef4bf23   Thomas Graf   [IPV4]: Use Proto...
188
189
  	[FRA_FLOW]	= { .type = NLA_U32 },
  };
7b204afd4   Robert Olsson   [IPV4]: Use RCU l...
190

e1ef4bf23   Thomas Graf   [IPV4]: Use Proto...
191
  static int fib4_rule_configure(struct fib_rule *rule, struct sk_buff *skb,
8b3521eeb   Rami Rosen   ipv4: remove an u...
192
  			       struct fib_rule_hdr *frh,
e1ef4bf23   Thomas Graf   [IPV4]: Use Proto...
193
  			       struct nlattr **tb)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
194
  {
3b1e0a655   YOSHIFUJI Hideaki   [NET] NETNS: Omit...
195
  	struct net *net = sock_net(skb->sk);
e1ef4bf23   Thomas Graf   [IPV4]: Use Proto...
196
197
  	int err = -EINVAL;
  	struct fib4_rule *rule4 = (struct fib4_rule *) rule;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
198

e1701c68c   Thomas Graf   [NET]: Fix fib_ru...
199
  	if (frh->tos & ~IPTOS_TOS_MASK)
e1ef4bf23   Thomas Graf   [IPV4]: Use Proto...
200
  		goto errout;
7b204afd4   Robert Olsson   [IPV4]: Use RCU l...
201

0ddcf43d5   Alexander Duyck   ipv4: FIB Local/M...
202
203
204
205
  	/* split local/main if they are not already split */
  	err = fib_unmerge(net);
  	if (err)
  		goto errout;
96c63fa73   David Ahern   net: Add l3mdev rule
206
  	if (rule->table == RT_TABLE_UNSPEC && !rule->l3mdev) {
e1ef4bf23   Thomas Graf   [IPV4]: Use Proto...
207
208
  		if (rule->action == FR_ACT_TO_TBL) {
  			struct fib_table *table;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
209

e4e4971c5   Denis V. Lunev   [NETNS]: Namespac...
210
  			table = fib_empty_table(net);
51456b291   Ian Morris   ipv4: coding styl...
211
  			if (!table) {
e1ef4bf23   Thomas Graf   [IPV4]: Use Proto...
212
213
214
  				err = -ENOBUFS;
  				goto errout;
  			}
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
215

e1ef4bf23   Thomas Graf   [IPV4]: Use Proto...
216
  			rule->table = table->tb_id;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
217
218
  		}
  	}
e1701c68c   Thomas Graf   [NET]: Fix fib_ru...
219
  	if (frh->src_len)
67b61f6c1   Jiri Benc   netlink: implemen...
220
  		rule4->src = nla_get_in_addr(tb[FRA_SRC]);
7b204afd4   Robert Olsson   [IPV4]: Use RCU l...
221

e1701c68c   Thomas Graf   [NET]: Fix fib_ru...
222
  	if (frh->dst_len)
67b61f6c1   Jiri Benc   netlink: implemen...
223
  		rule4->dst = nla_get_in_addr(tb[FRA_DST]);
7b204afd4   Robert Olsson   [IPV4]: Use RCU l...
224

c7066f70d   Patrick McHardy   netfilter: fix Kc...
225
  #ifdef CONFIG_IP_ROUTE_CLASSID
7a9bc9b81   David S. Miller   ipv4: Elide fib_v...
226
  	if (tb[FRA_FLOW]) {
e1ef4bf23   Thomas Graf   [IPV4]: Use Proto...
227
  		rule4->tclassid = nla_get_u32(tb[FRA_FLOW]);
7a9bc9b81   David S. Miller   ipv4: Elide fib_v...
228
  		if (rule4->tclassid)
f4530fa57   David S. Miller   ipv4: Avoid overh...
229
  			net->ipv4.fib_num_tclassid_users++;
7a9bc9b81   David S. Miller   ipv4: Elide fib_v...
230
  	}
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
231
  #endif
e1ef4bf23   Thomas Graf   [IPV4]: Use Proto...
232
233
234
235
236
  	rule4->src_len = frh->src_len;
  	rule4->srcmask = inet_make_mask(rule4->src_len);
  	rule4->dst_len = frh->dst_len;
  	rule4->dstmask = inet_make_mask(rule4->dst_len);
  	rule4->tos = frh->tos;
7b204afd4   Robert Olsson   [IPV4]: Use RCU l...
237

f4530fa57   David S. Miller   ipv4: Avoid overh...
238
  	net->ipv4.fib_has_custom_rules = true;
104616e74   Scott Feldman   switchdev: don't ...
239

e1ef4bf23   Thomas Graf   [IPV4]: Use Proto...
240
241
242
  	err = 0;
  errout:
  	return err;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
243
  }
0ddcf43d5   Alexander Duyck   ipv4: FIB Local/M...
244
  static int fib4_rule_delete(struct fib_rule *rule)
7a9bc9b81   David S. Miller   ipv4: Elide fib_v...
245
  {
f4530fa57   David S. Miller   ipv4: Avoid overh...
246
  	struct net *net = rule->fr_net;
0ddcf43d5   Alexander Duyck   ipv4: FIB Local/M...
247
  	int err;
7a9bc9b81   David S. Miller   ipv4: Elide fib_v...
248

0ddcf43d5   Alexander Duyck   ipv4: FIB Local/M...
249
250
251
252
253
254
255
  	/* split local/main if they are not already split */
  	err = fib_unmerge(net);
  	if (err)
  		goto errout;
  
  #ifdef CONFIG_IP_ROUTE_CLASSID
  	if (((struct fib4_rule *)rule)->tclassid)
f4530fa57   David S. Miller   ipv4: Avoid overh...
256
  		net->ipv4.fib_num_tclassid_users--;
7a9bc9b81   David S. Miller   ipv4: Elide fib_v...
257
  #endif
f4530fa57   David S. Miller   ipv4: Avoid overh...
258
  	net->ipv4.fib_has_custom_rules = true;
0ddcf43d5   Alexander Duyck   ipv4: FIB Local/M...
259
260
  errout:
  	return err;
7a9bc9b81   David S. Miller   ipv4: Elide fib_v...
261
  }
e1ef4bf23   Thomas Graf   [IPV4]: Use Proto...
262
263
  static int fib4_rule_compare(struct fib_rule *rule, struct fib_rule_hdr *frh,
  			     struct nlattr **tb)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
264
  {
e1ef4bf23   Thomas Graf   [IPV4]: Use Proto...
265
  	struct fib4_rule *rule4 = (struct fib4_rule *) rule;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
266

e1ef4bf23   Thomas Graf   [IPV4]: Use Proto...
267
268
  	if (frh->src_len && (rule4->src_len != frh->src_len))
  		return 0;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
269

e1ef4bf23   Thomas Graf   [IPV4]: Use Proto...
270
271
  	if (frh->dst_len && (rule4->dst_len != frh->dst_len))
  		return 0;
7b204afd4   Robert Olsson   [IPV4]: Use RCU l...
272

e1ef4bf23   Thomas Graf   [IPV4]: Use Proto...
273
274
  	if (frh->tos && (rule4->tos != frh->tos))
  		return 0;
7b204afd4   Robert Olsson   [IPV4]: Use RCU l...
275

c7066f70d   Patrick McHardy   netfilter: fix Kc...
276
  #ifdef CONFIG_IP_ROUTE_CLASSID
e1ef4bf23   Thomas Graf   [IPV4]: Use Proto...
277
278
279
  	if (tb[FRA_FLOW] && (rule4->tclassid != nla_get_u32(tb[FRA_FLOW])))
  		return 0;
  #endif
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
280

67b61f6c1   Jiri Benc   netlink: implemen...
281
  	if (frh->src_len && (rule4->src != nla_get_in_addr(tb[FRA_SRC])))
e1ef4bf23   Thomas Graf   [IPV4]: Use Proto...
282
  		return 0;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
283

67b61f6c1   Jiri Benc   netlink: implemen...
284
  	if (frh->dst_len && (rule4->dst != nla_get_in_addr(tb[FRA_DST])))
e1ef4bf23   Thomas Graf   [IPV4]: Use Proto...
285
  		return 0;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
286

e1ef4bf23   Thomas Graf   [IPV4]: Use Proto...
287
  	return 1;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
288
  }
e1ef4bf23   Thomas Graf   [IPV4]: Use Proto...
289
  static int fib4_rule_fill(struct fib_rule *rule, struct sk_buff *skb,
04af8cf6f   Rami Rosen   net: Remove unuse...
290
  			  struct fib_rule_hdr *frh)
e1ef4bf23   Thomas Graf   [IPV4]: Use Proto...
291
292
  {
  	struct fib4_rule *rule4 = (struct fib4_rule *) rule;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
293

e1ef4bf23   Thomas Graf   [IPV4]: Use Proto...
294
295
296
  	frh->dst_len = rule4->dst_len;
  	frh->src_len = rule4->src_len;
  	frh->tos = rule4->tos;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
297

f3756b79e   David S. Miller   ipv4: Stop using ...
298
  	if ((rule4->dst_len &&
930345ea6   Jiri Benc   netlink: implemen...
299
  	     nla_put_in_addr(skb, FRA_DST, rule4->dst)) ||
f3756b79e   David S. Miller   ipv4: Stop using ...
300
  	    (rule4->src_len &&
930345ea6   Jiri Benc   netlink: implemen...
301
  	     nla_put_in_addr(skb, FRA_SRC, rule4->src)))
f3756b79e   David S. Miller   ipv4: Stop using ...
302
  		goto nla_put_failure;
c7066f70d   Patrick McHardy   netfilter: fix Kc...
303
  #ifdef CONFIG_IP_ROUTE_CLASSID
f3756b79e   David S. Miller   ipv4: Stop using ...
304
305
306
  	if (rule4->tclassid &&
  	    nla_put_u32(skb, FRA_FLOW, rule4->tclassid))
  		goto nla_put_failure;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
307
  #endif
e1ef4bf23   Thomas Graf   [IPV4]: Use Proto...
308
  	return 0;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
309

e1ef4bf23   Thomas Graf   [IPV4]: Use Proto...
310
311
  nla_put_failure:
  	return -ENOBUFS;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
312
  }
339bf98ff   Thomas Graf   [NETLINK]: Do pre...
313
314
315
316
317
318
  static size_t fib4_rule_nlmsg_payload(struct fib_rule *rule)
  {
  	return nla_total_size(4) /* dst */
  	       + nla_total_size(4) /* src */
  	       + nla_total_size(4); /* flow */
  }
ae299fc05   Denis V. Lunev   net: add fib_rule...
319
  static void fib4_rule_flush_cache(struct fib_rules_ops *ops)
73417f617   Thomas Graf   [NET] fib_rules: ...
320
  {
bafa6d9d8   Nicolas Dichtel   ipv4/route: arg d...
321
  	rt_cache_flush(ops->fro_net);
73417f617   Thomas Graf   [NET] fib_rules: ...
322
  }
04a6f82cf   Andi Kleen   sections: fix sec...
323
  static const struct fib_rules_ops __net_initconst fib4_rules_ops_template = {
25239cee7   Patrick McHardy   net: rtnetlink: d...
324
  	.family		= AF_INET,
e1ef4bf23   Thomas Graf   [IPV4]: Use Proto...
325
  	.rule_size	= sizeof(struct fib4_rule),
e1701c68c   Thomas Graf   [NET]: Fix fib_ru...
326
  	.addr_size	= sizeof(u32),
e1ef4bf23   Thomas Graf   [IPV4]: Use Proto...
327
  	.action		= fib4_rule_action,
7764a45a8   Stefan Tomanek   fib_rules: add .s...
328
  	.suppress	= fib4_rule_suppress,
e1ef4bf23   Thomas Graf   [IPV4]: Use Proto...
329
330
  	.match		= fib4_rule_match,
  	.configure	= fib4_rule_configure,
7a9bc9b81   David S. Miller   ipv4: Elide fib_v...
331
  	.delete		= fib4_rule_delete,
e1ef4bf23   Thomas Graf   [IPV4]: Use Proto...
332
333
  	.compare	= fib4_rule_compare,
  	.fill		= fib4_rule_fill,
339bf98ff   Thomas Graf   [NETLINK]: Do pre...
334
  	.nlmsg_payload	= fib4_rule_nlmsg_payload,
73417f617   Thomas Graf   [NET] fib_rules: ...
335
  	.flush_cache	= fib4_rule_flush_cache,
e1ef4bf23   Thomas Graf   [IPV4]: Use Proto...
336
337
  	.nlgroup	= RTNLGRP_IPV4_RULE,
  	.policy		= fib4_rule_policy,
e1ef4bf23   Thomas Graf   [IPV4]: Use Proto...
338
339
  	.owner		= THIS_MODULE,
  };
e4e4971c5   Denis V. Lunev   [NETNS]: Namespac...
340
  static int fib_default_rules_init(struct fib_rules_ops *ops)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
341
  {
2994c6386   Denis V. Lunev   [INET]: Small pos...
342
  	int err;
5adef1809   Patrick McHardy   net 04/05: fib_ru...
343
  	err = fib_default_rule_add(ops, 0, RT_TABLE_LOCAL, 0);
2994c6386   Denis V. Lunev   [INET]: Small pos...
344
345
  	if (err < 0)
  		return err;
e4e4971c5   Denis V. Lunev   [NETNS]: Namespac...
346
  	err = fib_default_rule_add(ops, 0x7FFE, RT_TABLE_MAIN, 0);
2994c6386   Denis V. Lunev   [INET]: Small pos...
347
348
  	if (err < 0)
  		return err;
e4e4971c5   Denis V. Lunev   [NETNS]: Namespac...
349
  	err = fib_default_rule_add(ops, 0x7FFF, RT_TABLE_DEFAULT, 0);
2994c6386   Denis V. Lunev   [INET]: Small pos...
350
351
352
353
  	if (err < 0)
  		return err;
  	return 0;
  }
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
354

7b1a74fdb   Denis V. Lunev   [NETNS]: Refactor...
355
  int __net_init fib4_rules_init(struct net *net)
2994c6386   Denis V. Lunev   [INET]: Small pos...
356
  {
dbb50165b   Denis V. Lunev   [IPV4]: Check fib...
357
  	int err;
e4e4971c5   Denis V. Lunev   [NETNS]: Namespac...
358
  	struct fib_rules_ops *ops;
e9c5158ac   Eric W. Biederman   net: Allow fib_ru...
359
360
361
  	ops = fib_rules_register(&fib4_rules_ops_template, net);
  	if (IS_ERR(ops))
  		return PTR_ERR(ops);
dbb50165b   Denis V. Lunev   [IPV4]: Check fib...
362

e4e4971c5   Denis V. Lunev   [NETNS]: Namespac...
363
  	err = fib_default_rules_init(ops);
dbb50165b   Denis V. Lunev   [IPV4]: Check fib...
364
365
  	if (err < 0)
  		goto fail;
e4e4971c5   Denis V. Lunev   [NETNS]: Namespac...
366
  	net->ipv4.rules_ops = ops;
f4530fa57   David S. Miller   ipv4: Avoid overh...
367
  	net->ipv4.fib_has_custom_rules = false;
dbb50165b   Denis V. Lunev   [IPV4]: Check fib...
368
369
370
371
  	return 0;
  
  fail:
  	/* also cleans all rules already added */
9e3a54878   Denis V. Lunev   [NETNS]: FIB rule...
372
  	fib_rules_unregister(ops);
dbb50165b   Denis V. Lunev   [IPV4]: Check fib...
373
  	return err;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
374
  }
7b1a74fdb   Denis V. Lunev   [NETNS]: Refactor...
375
376
377
  
  void __net_exit fib4_rules_exit(struct net *net)
  {
9e3a54878   Denis V. Lunev   [NETNS]: FIB rule...
378
  	fib_rules_unregister(net->ipv4.rules_ops);
7b1a74fdb   Denis V. Lunev   [NETNS]: Refactor...
379
  }