Blame view

net/ipv6/fib6_rules.c 6.88 KB
101367c2f   Thomas Graf   [IPV6]: Policy Ro...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
  /*
   * net/ipv6/fib6_rules.c	IPv6 Routing Policy Rules
   *
   * Copyright (C)2003-2006 Helsinki University of Technology
   * Copyright (C)2003-2006 USAGI/WIDE Project
   *
   *	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, version 2.
   *
   * Authors
   *	Thomas Graf		<tgraf@suug.ch>
   *	Ville Nuorvala		<vnuorval@tcs.hut.fi>
   */
101367c2f   Thomas Graf   [IPV6]: Policy Ro...
15
  #include <linux/netdevice.h>
bc3b2d7fb   Paul Gortmaker   net: Add export.h...
16
  #include <linux/export.h>
101367c2f   Thomas Graf   [IPV6]: Policy Ro...
17
18
19
  
  #include <net/fib_rules.h>
  #include <net/ipv6.h>
29f6af771   YOSHIFUJI Hideaki   [IPV6] FIB6RULE: ...
20
  #include <net/addrconf.h>
101367c2f   Thomas Graf   [IPV6]: Policy Ro...
21
22
23
24
25
26
27
28
29
30
  #include <net/ip6_route.h>
  #include <net/netlink.h>
  
  struct fib6_rule
  {
  	struct fib_rule		common;
  	struct rt6key		src;
  	struct rt6key		dst;
  	u8			tclass;
  };
4c9483b2f   David S. Miller   ipv6: Convert to ...
31
  struct dst_entry *fib6_rule_lookup(struct net *net, struct flowi6 *fl6,
58f09b78b   Daniel Lezcano   [NETNS][IPV6] ip6...
32
  				   int flags, pol_lookup_t lookup)
101367c2f   Thomas Graf   [IPV6]: Policy Ro...
33
34
35
  {
  	struct fib_lookup_arg arg = {
  		.lookup_ptr = lookup,
2c1c00040   Eric Dumazet   fib6: use FIB_LOO...
36
  		.flags = FIB_LOOKUP_NOREF,
101367c2f   Thomas Graf   [IPV6]: Policy Ro...
37
  	};
4c9483b2f   David S. Miller   ipv6: Convert to ...
38
39
  	fib_rules_lookup(net->ipv6.fib6_rules_ops,
  			 flowi6_to_flowi(fl6), flags, &arg);
101367c2f   Thomas Graf   [IPV6]: Policy Ro...
40

b14295532   Ville Nuorvala   [IPV6]: Make sure...
41
  	if (arg.result)
40aa7b90a   YOSHIFUJI Hideaki   [IPV6] ROUTE: Use...
42
  		return arg.result;
b14295532   Ville Nuorvala   [IPV6]: Make sure...
43

d8d1f30b9   Changli Gao   net-next: remove ...
44
45
  	dst_hold(&net->ipv6.ip6_null_entry->dst);
  	return &net->ipv6.ip6_null_entry->dst;
101367c2f   Thomas Graf   [IPV6]: Policy Ro...
46
  }
8ce11e6a9   Adrian Bunk   [NET]: Make code ...
47
48
  static int fib6_rule_action(struct fib_rule *rule, struct flowi *flp,
  			    int flags, struct fib_lookup_arg *arg)
101367c2f   Thomas Graf   [IPV6]: Policy Ro...
49
  {
4c9483b2f   David S. Miller   ipv6: Convert to ...
50
  	struct flowi6 *flp6 = &flp->u.ip6;
101367c2f   Thomas Graf   [IPV6]: Policy Ro...
51
52
  	struct rt6_info *rt = NULL;
  	struct fib6_table *table;
8ed677896   Daniel Lezcano   [NETNS][IPV6] rt6...
53
  	struct net *net = rule->fr_net;
101367c2f   Thomas Graf   [IPV6]: Policy Ro...
54
55
56
57
58
59
  	pol_lookup_t lookup = arg->lookup_ptr;
  
  	switch (rule->action) {
  	case FR_ACT_TO_TBL:
  		break;
  	case FR_ACT_UNREACHABLE:
8ed677896   Daniel Lezcano   [NETNS][IPV6] rt6...
60
  		rt = net->ipv6.ip6_null_entry;
101367c2f   Thomas Graf   [IPV6]: Policy Ro...
61
62
63
  		goto discard_pkt;
  	default:
  	case FR_ACT_BLACKHOLE:
8ed677896   Daniel Lezcano   [NETNS][IPV6] rt6...
64
  		rt = net->ipv6.ip6_blk_hole_entry;
101367c2f   Thomas Graf   [IPV6]: Policy Ro...
65
66
  		goto discard_pkt;
  	case FR_ACT_PROHIBIT:
8ed677896   Daniel Lezcano   [NETNS][IPV6] rt6...
67
  		rt = net->ipv6.ip6_prohibit_entry;
101367c2f   Thomas Graf   [IPV6]: Policy Ro...
68
69
  		goto discard_pkt;
  	}
8ed677896   Daniel Lezcano   [NETNS][IPV6] rt6...
70
  	table = fib6_get_table(net, rule->table);
101367c2f   Thomas Graf   [IPV6]: Policy Ro...
71
  	if (table)
4c9483b2f   David S. Miller   ipv6: Convert to ...
72
  		rt = lookup(net, table, flp6, flags);
101367c2f   Thomas Graf   [IPV6]: Policy Ro...
73

8ed677896   Daniel Lezcano   [NETNS][IPV6] rt6...
74
  	if (rt != net->ipv6.ip6_null_entry) {
29f6af771   YOSHIFUJI Hideaki   [IPV6] FIB6RULE: ...
75
76
77
78
79
80
81
82
83
  		struct fib6_rule *r = (struct fib6_rule *)rule;
  
  		/*
  		 * If we need to find a source address for this traffic,
  		 * we check the result if it meets requirement of the rule.
  		 */
  		if ((rule->flags & FIB_RULE_FIND_SADDR) &&
  		    r->src.plen && !(flags & RT6_LOOKUP_F_HAS_SADDR)) {
  			struct in6_addr saddr;
7cbca67c0   YOSHIFUJI Hideaki   [IPV6]: Support S...
84

191cd5825   Brian Haley   netns: Add networ...
85
  			if (ipv6_dev_get_saddr(net,
d8d1f30b9   Changli Gao   net-next: remove ...
86
  					       ip6_dst_idev(&rt->dst)->dev,
4c9483b2f   David S. Miller   ipv6: Convert to ...
87
  					       &flp6->daddr,
0c9a2ac1f   YOSHIFUJI Hideaki / 吉藤英明   ipv6: Optmize tra...
88
  					       rt6_flags2srcprefs(flags),
7cbca67c0   YOSHIFUJI Hideaki   [IPV6]: Support S...
89
  					       &saddr))
29f6af771   YOSHIFUJI Hideaki   [IPV6] FIB6RULE: ...
90
91
92
93
  				goto again;
  			if (!ipv6_prefix_equal(&saddr, &r->src.addr,
  					       r->src.plen))
  				goto again;
4e3fd7a06   Alexey Dobriyan   net: remove ipv6_...
94
  			flp6->saddr = saddr;
29f6af771   YOSHIFUJI Hideaki   [IPV6] FIB6RULE: ...
95
  		}
101367c2f   Thomas Graf   [IPV6]: Policy Ro...
96
  		goto out;
29f6af771   YOSHIFUJI Hideaki   [IPV6] FIB6RULE: ...
97
98
  	}
  again:
d8d1f30b9   Changli Gao   net-next: remove ...
99
  	dst_release(&rt->dst);
3226f6881   Patrick McHardy   [IPV6]: Fix polic...
100
101
  	rt = NULL;
  	goto out;
101367c2f   Thomas Graf   [IPV6]: Policy Ro...
102
  discard_pkt:
d8d1f30b9   Changli Gao   net-next: remove ...
103
  	dst_hold(&rt->dst);
101367c2f   Thomas Graf   [IPV6]: Policy Ro...
104
105
106
107
108
109
110
111
112
  out:
  	arg->result = rt;
  	return rt == NULL ? -EAGAIN : 0;
  }
  
  
  static int fib6_rule_match(struct fib_rule *rule, struct flowi *fl, int flags)
  {
  	struct fib6_rule *r = (struct fib6_rule *) rule;
4c9483b2f   David S. Miller   ipv6: Convert to ...
113
  	struct flowi6 *fl6 = &fl->u.ip6;
101367c2f   Thomas Graf   [IPV6]: Policy Ro...
114

adaa70bbd   Thomas Graf   [IPv6] rules: Use...
115
  	if (r->dst.plen &&
4c9483b2f   David S. Miller   ipv6: Convert to ...
116
  	    !ipv6_prefix_equal(&fl6->daddr, &r->dst.addr, r->dst.plen))
101367c2f   Thomas Graf   [IPV6]: Policy Ro...
117
  		return 0;
29f6af771   YOSHIFUJI Hideaki   [IPV6] FIB6RULE: ...
118
119
120
121
122
  	/*
  	 * If FIB_RULE_FIND_SADDR is set and we do not have a
  	 * source address for the traffic, we defer check for
  	 * source address.
  	 */
adaa70bbd   Thomas Graf   [IPv6] rules: Use...
123
  	if (r->src.plen) {
29f6af771   YOSHIFUJI Hideaki   [IPV6] FIB6RULE: ...
124
  		if (flags & RT6_LOOKUP_F_HAS_SADDR) {
4c9483b2f   David S. Miller   ipv6: Convert to ...
125
  			if (!ipv6_prefix_equal(&fl6->saddr, &r->src.addr,
29f6af771   YOSHIFUJI Hideaki   [IPV6] FIB6RULE: ...
126
127
128
  					       r->src.plen))
  				return 0;
  		} else if (!(r->common.flags & FIB_RULE_FIND_SADDR))
adaa70bbd   Thomas Graf   [IPv6] rules: Use...
129
130
  			return 0;
  	}
101367c2f   Thomas Graf   [IPV6]: Policy Ro...
131

4c9483b2f   David S. Miller   ipv6: Convert to ...
132
  	if (r->tclass && r->tclass != ((ntohl(fl6->flowlabel) >> 20) & 0xff))
2cc67cc73   YOSHIFUJI Hideaki   [IPV6] ROUTE: Rou...
133
  		return 0;
101367c2f   Thomas Graf   [IPV6]: Policy Ro...
134
135
  	return 1;
  }
ef7c79ed6   Patrick McHardy   [NETLINK]: Mark n...
136
  static const struct nla_policy fib6_rule_policy[FRA_MAX+1] = {
1f6c9557e   Thomas Graf   [NET] rules: Shar...
137
  	FRA_GENERIC_POLICY,
101367c2f   Thomas Graf   [IPV6]: Policy Ro...
138
139
140
  };
  
  static int fib6_rule_configure(struct fib_rule *rule, struct sk_buff *skb,
8b3521eeb   Rami Rosen   ipv4: remove an u...
141
  			       struct fib_rule_hdr *frh,
101367c2f   Thomas Graf   [IPV6]: Policy Ro...
142
143
144
  			       struct nlattr **tb)
  {
  	int err = -EINVAL;
3b1e0a655   YOSHIFUJI Hideaki   [NET] NETNS: Omit...
145
  	struct net *net = sock_net(skb->sk);
101367c2f   Thomas Graf   [IPV6]: Policy Ro...
146
  	struct fib6_rule *rule6 = (struct fib6_rule *) rule;
101367c2f   Thomas Graf   [IPV6]: Policy Ro...
147
148
149
  	if (rule->action == FR_ACT_TO_TBL) {
  		if (rule->table == RT6_TABLE_UNSPEC)
  			goto errout;
dcabb819a   Daniel Lezcano   [NETNS][IPV6] fib...
150
  		if (fib6_new_table(net, rule->table) == NULL) {
101367c2f   Thomas Graf   [IPV6]: Policy Ro...
151
152
153
154
  			err = -ENOBUFS;
  			goto errout;
  		}
  	}
e1701c68c   Thomas Graf   [NET]: Fix fib_ru...
155
  	if (frh->src_len)
101367c2f   Thomas Graf   [IPV6]: Policy Ro...
156
157
  		nla_memcpy(&rule6->src.addr, tb[FRA_SRC],
  			   sizeof(struct in6_addr));
e1701c68c   Thomas Graf   [NET]: Fix fib_ru...
158
  	if (frh->dst_len)
101367c2f   Thomas Graf   [IPV6]: Policy Ro...
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
  		nla_memcpy(&rule6->dst.addr, tb[FRA_DST],
  			   sizeof(struct in6_addr));
  
  	rule6->src.plen = frh->src_len;
  	rule6->dst.plen = frh->dst_len;
  	rule6->tclass = frh->tos;
  
  	err = 0;
  errout:
  	return err;
  }
  
  static int fib6_rule_compare(struct fib_rule *rule, struct fib_rule_hdr *frh,
  			     struct nlattr **tb)
  {
  	struct fib6_rule *rule6 = (struct fib6_rule *) rule;
  
  	if (frh->src_len && (rule6->src.plen != frh->src_len))
  		return 0;
  
  	if (frh->dst_len && (rule6->dst.plen != frh->dst_len))
  		return 0;
  
  	if (frh->tos && (rule6->tclass != frh->tos))
  		return 0;
e1701c68c   Thomas Graf   [NET]: Fix fib_ru...
184
  	if (frh->src_len &&
101367c2f   Thomas Graf   [IPV6]: Policy Ro...
185
186
  	    nla_memcmp(tb[FRA_SRC], &rule6->src.addr, sizeof(struct in6_addr)))
  		return 0;
e1701c68c   Thomas Graf   [NET]: Fix fib_ru...
187
  	if (frh->dst_len &&
101367c2f   Thomas Graf   [IPV6]: Policy Ro...
188
189
190
191
192
193
194
  	    nla_memcmp(tb[FRA_DST], &rule6->dst.addr, sizeof(struct in6_addr)))
  		return 0;
  
  	return 1;
  }
  
  static int fib6_rule_fill(struct fib_rule *rule, struct sk_buff *skb,
04af8cf6f   Rami Rosen   net: Remove unuse...
195
  			  struct fib_rule_hdr *frh)
101367c2f   Thomas Graf   [IPV6]: Policy Ro...
196
197
  {
  	struct fib6_rule *rule6 = (struct fib6_rule *) rule;
101367c2f   Thomas Graf   [IPV6]: Policy Ro...
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
  	frh->dst_len = rule6->dst.plen;
  	frh->src_len = rule6->src.plen;
  	frh->tos = rule6->tclass;
  
  	if (rule6->dst.plen)
  		NLA_PUT(skb, FRA_DST, sizeof(struct in6_addr),
  			&rule6->dst.addr);
  
  	if (rule6->src.plen)
  		NLA_PUT(skb, FRA_SRC, sizeof(struct in6_addr),
  			&rule6->src.addr);
  
  	return 0;
  
  nla_put_failure:
  	return -ENOBUFS;
  }
868d13ac8   Denis V. Lunev   [NETNS]: Pass fib...
215
  static u32 fib6_rule_default_pref(struct fib_rules_ops *ops)
101367c2f   Thomas Graf   [IPV6]: Policy Ro...
216
217
218
  {
  	return 0x3FFF;
  }
339bf98ff   Thomas Graf   [NETLINK]: Do pre...
219
220
221
222
223
  static size_t fib6_rule_nlmsg_payload(struct fib_rule *rule)
  {
  	return nla_total_size(16) /* dst */
  	       + nla_total_size(16); /* src */
  }
3d0c9c4eb   Patrick McHardy   net: fib_rules: m...
224
  static const struct fib_rules_ops __net_initdata fib6_rules_ops_template = {
25239cee7   Patrick McHardy   net: rtnetlink: d...
225
  	.family			= AF_INET6,
101367c2f   Thomas Graf   [IPV6]: Policy Ro...
226
  	.rule_size		= sizeof(struct fib6_rule),
e1701c68c   Thomas Graf   [NET]: Fix fib_ru...
227
  	.addr_size		= sizeof(struct in6_addr),
101367c2f   Thomas Graf   [IPV6]: Policy Ro...
228
229
230
231
232
233
  	.action			= fib6_rule_action,
  	.match			= fib6_rule_match,
  	.configure		= fib6_rule_configure,
  	.compare		= fib6_rule_compare,
  	.fill			= fib6_rule_fill,
  	.default_pref		= fib6_rule_default_pref,
339bf98ff   Thomas Graf   [NETLINK]: Do pre...
234
  	.nlmsg_payload		= fib6_rule_nlmsg_payload,
101367c2f   Thomas Graf   [IPV6]: Policy Ro...
235
236
  	.nlgroup		= RTNLGRP_IPV6_RULE,
  	.policy			= fib6_rule_policy,
101367c2f   Thomas Graf   [IPV6]: Policy Ro...
237
  	.owner			= THIS_MODULE,
035923833   Denis V. Lunev   [FIB]: Add netns ...
238
  	.fro_net		= &init_net,
101367c2f   Thomas Graf   [IPV6]: Policy Ro...
239
  };
2c8c1e729   Alexey Dobriyan   net: spread __net...
240
  static int __net_init fib6_rules_net_init(struct net *net)
101367c2f   Thomas Graf   [IPV6]: Policy Ro...
241
  {
e9c5158ac   Eric W. Biederman   net: Allow fib_ru...
242
  	struct fib_rules_ops *ops;
dcabb819a   Daniel Lezcano   [NETNS][IPV6] fib...
243
  	int err = -ENOMEM;
2994c6386   Denis V. Lunev   [INET]: Small pos...
244

e9c5158ac   Eric W. Biederman   net: Allow fib_ru...
245
246
247
248
  	ops = fib_rules_register(&fib6_rules_ops_template, net);
  	if (IS_ERR(ops))
  		return PTR_ERR(ops);
  	net->ipv6.fib6_rules_ops = ops;
eb5564b85   Daniel Lezcano   [NETNS][IPV6] fib...
249

eb5564b85   Daniel Lezcano   [NETNS][IPV6] fib...
250

dcabb819a   Daniel Lezcano   [NETNS][IPV6] fib...
251
  	err = fib_default_rule_add(net->ipv6.fib6_rules_ops, 0,
5adef1809   Patrick McHardy   net 04/05: fib_ru...
252
  				   RT6_TABLE_LOCAL, 0);
dcabb819a   Daniel Lezcano   [NETNS][IPV6] fib...
253
254
  	if (err)
  		goto out_fib6_rules_ops;
9eb87f3f7   Daniel Lezcano   [IPV6]: Make fib6...
255

dcabb819a   Daniel Lezcano   [NETNS][IPV6] fib...
256
257
258
  	err = fib_default_rule_add(net->ipv6.fib6_rules_ops,
  				   0x7FFE, RT6_TABLE_MAIN, 0);
  	if (err)
e9c5158ac   Eric W. Biederman   net: Allow fib_ru...
259
  		goto out_fib6_rules_ops;
9eb87f3f7   Daniel Lezcano   [IPV6]: Make fib6...
260

9eb87f3f7   Daniel Lezcano   [IPV6]: Make fib6...
261
  out:
dcabb819a   Daniel Lezcano   [NETNS][IPV6] fib...
262
  	return err;
9eb87f3f7   Daniel Lezcano   [IPV6]: Make fib6...
263

dcabb819a   Daniel Lezcano   [NETNS][IPV6] fib...
264
  out_fib6_rules_ops:
e9c5158ac   Eric W. Biederman   net: Allow fib_ru...
265
  	fib_rules_unregister(ops);
9eb87f3f7   Daniel Lezcano   [IPV6]: Make fib6...
266
  	goto out;
101367c2f   Thomas Graf   [IPV6]: Policy Ro...
267
  }
2c8c1e729   Alexey Dobriyan   net: spread __net...
268
  static void __net_exit fib6_rules_net_exit(struct net *net)
dcabb819a   Daniel Lezcano   [NETNS][IPV6] fib...
269
270
  {
  	fib_rules_unregister(net->ipv6.fib6_rules_ops);
dcabb819a   Daniel Lezcano   [NETNS][IPV6] fib...
271
272
273
274
275
276
277
278
279
280
281
  }
  
  static struct pernet_operations fib6_rules_net_ops = {
  	.init = fib6_rules_net_init,
  	.exit = fib6_rules_net_exit,
  };
  
  int __init fib6_rules_init(void)
  {
  	return register_pernet_subsys(&fib6_rules_net_ops);
  }
101367c2f   Thomas Graf   [IPV6]: Policy Ro...
282
283
  void fib6_rules_cleanup(void)
  {
ff4e1fb0b   YOSHIFUJI Hideaki   [IPV6] FIB_RULE: ...
284
  	unregister_pernet_subsys(&fib6_rules_net_ops);
101367c2f   Thomas Graf   [IPV6]: Policy Ro...
285
  }