linux: add ipv6 failed-policy routing action (by Jonas Gorski)
[openwrt/svn-archive/archive.git] / target / linux / generic / patches-3.8 / 670-ipv6-allow-rejecting-with-source-address-failed-policy.patch
1 Index: linux-3.8.13/include/net/netns/ipv6.h
2 ===================================================================
3 --- linux-3.8.13.orig/include/net/netns/ipv6.h 2013-06-10 14:14:20.910911309 +0200
4 +++ linux-3.8.13/include/net/netns/ipv6.h 2013-06-10 14:14:20.898911309 +0200
5 @@ -54,6 +54,7 @@
6 unsigned long ip6_rt_last_gc;
7 #ifdef CONFIG_IPV6_MULTIPLE_TABLES
8 struct rt6_info *ip6_prohibit_entry;
9 + struct rt6_info *ip6_failed_policy_entry;
10 struct rt6_info *ip6_blk_hole_entry;
11 struct fib6_table *fib6_local_tbl;
12 struct fib_rules_ops *fib6_rules_ops;
13 Index: linux-3.8.13/include/uapi/linux/fib_rules.h
14 ===================================================================
15 --- linux-3.8.13.orig/include/uapi/linux/fib_rules.h 2013-06-10 14:14:20.910911309 +0200
16 +++ linux-3.8.13/include/uapi/linux/fib_rules.h 2013-06-10 14:14:20.898911309 +0200
17 @@ -64,6 +64,10 @@
18 FR_ACT_BLACKHOLE, /* Drop without notification */
19 FR_ACT_UNREACHABLE, /* Drop with ENETUNREACH */
20 FR_ACT_PROHIBIT, /* Drop with EACCES */
21 + FR_ACT_RES9,
22 + FR_ACT_RES10,
23 + FR_ACT_RES11,
24 + FR_ACT_FAILED_POLICY, /* Drop with EPERM */
25 __FR_ACT_MAX,
26 };
27
28 Index: linux-3.8.13/include/uapi/linux/icmpv6.h
29 ===================================================================
30 --- linux-3.8.13.orig/include/uapi/linux/icmpv6.h 2013-06-10 14:14:20.910911309 +0200
31 +++ linux-3.8.13/include/uapi/linux/icmpv6.h 2013-06-10 14:14:20.898911309 +0200
32 @@ -115,6 +115,7 @@
33 #define ICMPV6_NOT_NEIGHBOUR 2
34 #define ICMPV6_ADDR_UNREACH 3
35 #define ICMPV6_PORT_UNREACH 4
36 +#define ICMPV6_FAILED_POLICY 5
37
38 /*
39 * Codes for Time Exceeded
40 Index: linux-3.8.13/include/uapi/linux/rtnetlink.h
41 ===================================================================
42 --- linux-3.8.13.orig/include/uapi/linux/rtnetlink.h 2013-06-10 14:14:20.910911309 +0200
43 +++ linux-3.8.13/include/uapi/linux/rtnetlink.h 2013-06-10 14:14:20.898911309 +0200
44 @@ -203,6 +203,7 @@
45 RTN_THROW, /* Not in this table */
46 RTN_NAT, /* Translate this address */
47 RTN_XRESOLVE, /* Use external resolver */
48 + RTN_FAILED_POLICY, /* Failed ingress/egress policy */
49 __RTN_MAX
50 };
51
52 Index: linux-3.8.13/net/ipv4/fib_rules.c
53 ===================================================================
54 --- linux-3.8.13.orig/net/ipv4/fib_rules.c 2013-06-10 14:14:20.910911309 +0200
55 +++ linux-3.8.13/net/ipv4/fib_rules.c 2013-06-10 14:14:20.898911309 +0200
56 @@ -84,6 +84,10 @@
57 err = -EACCES;
58 goto errout;
59
60 + case FR_ACT_FAILED_POLICY:
61 + err = -EPERM;
62 + goto errout;
63 +
64 case FR_ACT_BLACKHOLE:
65 default:
66 err = -EINVAL;
67 Index: linux-3.8.13/net/ipv4/fib_semantics.c
68 ===================================================================
69 --- linux-3.8.13.orig/net/ipv4/fib_semantics.c 2013-06-10 14:14:20.910911309 +0200
70 +++ linux-3.8.13/net/ipv4/fib_semantics.c 2013-06-10 14:14:20.898911309 +0200
71 @@ -138,6 +138,10 @@
72 .error = -EINVAL,
73 .scope = RT_SCOPE_NOWHERE,
74 },
75 + [RTN_FAILED_POLICY] = {
76 + .error = -EPERM,
77 + .scope = RT_SCOPE_UNIVERSE,
78 + },
79 };
80
81 static void rt_fibinfo_free(struct rtable __rcu **rtp)
82 Index: linux-3.8.13/net/ipv4/fib_trie.c
83 ===================================================================
84 --- linux-3.8.13.orig/net/ipv4/fib_trie.c 2013-06-10 14:14:20.910911309 +0200
85 +++ linux-3.8.13/net/ipv4/fib_trie.c 2013-06-10 14:14:20.902911309 +0200
86 @@ -2350,6 +2350,7 @@
87 [RTN_THROW] = "THROW",
88 [RTN_NAT] = "NAT",
89 [RTN_XRESOLVE] = "XRESOLVE",
90 + [RTN_FAILED_POLICY] = "FAILED_POLICY",
91 };
92
93 static inline const char *rtn_type(char *buf, size_t len, unsigned int t)
94 Index: linux-3.8.13/net/ipv4/ipmr.c
95 ===================================================================
96 --- linux-3.8.13.orig/net/ipv4/ipmr.c 2013-06-10 14:14:20.910911309 +0200
97 +++ linux-3.8.13/net/ipv4/ipmr.c 2013-06-10 14:14:20.902911309 +0200
98 @@ -181,6 +181,7 @@
99 case FR_ACT_UNREACHABLE:
100 return -ENETUNREACH;
101 case FR_ACT_PROHIBIT:
102 + case FR_ACT_FAILED_POLICY:
103 return -EACCES;
104 case FR_ACT_BLACKHOLE:
105 default:
106 Index: linux-3.8.13/net/ipv6/fib6_rules.c
107 ===================================================================
108 --- linux-3.8.13.orig/net/ipv6/fib6_rules.c 2013-06-10 14:14:20.910911309 +0200
109 +++ linux-3.8.13/net/ipv6/fib6_rules.c 2013-06-10 14:14:20.902911309 +0200
110 @@ -69,6 +69,9 @@
111 case FR_ACT_PROHIBIT:
112 rt = net->ipv6.ip6_prohibit_entry;
113 goto discard_pkt;
114 + case FR_ACT_FAILED_POLICY:
115 + rt = net->ipv6.ip6_failed_policy_entry;
116 + goto discard_pkt;
117 }
118
119 table = fib6_get_table(net, rule->table);
120 Index: linux-3.8.13/net/ipv6/ip6mr.c
121 ===================================================================
122 --- linux-3.8.13.orig/net/ipv6/ip6mr.c 2013-06-10 14:14:20.910911309 +0200
123 +++ linux-3.8.13/net/ipv6/ip6mr.c 2013-06-10 14:14:20.902911309 +0200
124 @@ -166,6 +166,8 @@
125 return -ENETUNREACH;
126 case FR_ACT_PROHIBIT:
127 return -EACCES;
128 + case FR_ACT_FAILED_POLICY:
129 + return -EPERM;
130 case FR_ACT_BLACKHOLE:
131 default:
132 return -EINVAL;
133 Index: linux-3.8.13/net/ipv6/route.c
134 ===================================================================
135 --- linux-3.8.13.orig/net/ipv6/route.c 2013-06-10 14:14:20.910911309 +0200
136 +++ linux-3.8.13/net/ipv6/route.c 2013-06-10 14:14:20.906911309 +0200
137 @@ -258,6 +258,24 @@
138 .rt6i_ref = ATOMIC_INIT(1),
139 };
140
141 +static int ip6_pkt_failed_policy(struct sk_buff *skb);
142 +static int ip6_pkt_failed_policy_out(struct sk_buff *skb);
143 +
144 +static const struct rt6_info ip6_failed_policy_entry_template = {
145 + .dst = {
146 + .__refcnt = ATOMIC_INIT(1),
147 + .__use = 1,
148 + .obsolete = DST_OBSOLETE_FORCE_CHK,
149 + .error = -EPERM,
150 + .input = ip6_pkt_failed_policy,
151 + .output = ip6_pkt_failed_policy_out,
152 + },
153 + .rt6i_flags = (RTF_REJECT | RTF_NONEXTHOP),
154 + .rt6i_protocol = RTPROT_KERNEL,
155 + .rt6i_metric = ~(u32) 0,
156 + .rt6i_ref = ATOMIC_INIT(1),
157 +};
158 +
159 static const struct rt6_info ip6_blk_hole_entry_template = {
160 .dst = {
161 .__refcnt = ATOMIC_INIT(1),
162 @@ -1516,6 +1534,9 @@
163 case RTN_THROW:
164 rt->dst.error = -EAGAIN;
165 break;
166 + case RTN_FAILED_POLICY:
167 + rt->dst.error = -EPERM;
168 + break;
169 default:
170 rt->dst.error = -ENETUNREACH;
171 break;
172 @@ -2110,6 +2131,17 @@
173 return ip6_pkt_drop(skb, ICMPV6_ADM_PROHIBITED, IPSTATS_MIB_OUTNOROUTES);
174 }
175
176 +static int ip6_pkt_failed_policy(struct sk_buff *skb)
177 +{
178 + return ip6_pkt_drop(skb, ICMPV6_FAILED_POLICY, IPSTATS_MIB_INNOROUTES);
179 +}
180 +
181 +static int ip6_pkt_failed_policy_out(struct sk_buff *skb)
182 +{
183 + skb->dev = skb_dst(skb)->dev;
184 + return ip6_pkt_drop(skb, ICMPV6_FAILED_POLICY, IPSTATS_MIB_OUTNOROUTES);
185 +}
186 +
187 #endif
188
189 /*
190 @@ -2321,7 +2353,8 @@
191 if (rtm->rtm_type == RTN_UNREACHABLE ||
192 rtm->rtm_type == RTN_BLACKHOLE ||
193 rtm->rtm_type == RTN_PROHIBIT ||
194 - rtm->rtm_type == RTN_THROW)
195 + rtm->rtm_type == RTN_THROW ||
196 + rtm->rtm_type == RTN_FAILED_POLICY)
197 cfg->fc_flags |= RTF_REJECT;
198
199 if (rtm->rtm_type == RTN_LOCAL)
200 @@ -2524,6 +2557,9 @@
201 case -EACCES:
202 rtm->rtm_type = RTN_PROHIBIT;
203 break;
204 + case -EPERM:
205 + rtm->rtm_type = RTN_FAILED_POLICY;
206 + break;
207 case -EAGAIN:
208 rtm->rtm_type = RTN_THROW;
209 break;
210 @@ -2775,6 +2811,8 @@
211 #ifdef CONFIG_IPV6_MULTIPLE_TABLES
212 net->ipv6.ip6_prohibit_entry->dst.dev = dev;
213 net->ipv6.ip6_prohibit_entry->rt6i_idev = in6_dev_get(dev);
214 + net->ipv6.ip6_failed_policy_entry->dst.dev = dev;
215 + net->ipv6.ip6_failed_policy_entry->rt6i_idev = in6_dev_get(dev);
216 net->ipv6.ip6_blk_hole_entry->dst.dev = dev;
217 net->ipv6.ip6_blk_hole_entry->rt6i_idev = in6_dev_get(dev);
218 #endif
219 @@ -3037,6 +3075,17 @@
220 net->ipv6.ip6_blk_hole_entry->dst.ops = &net->ipv6.ip6_dst_ops;
221 dst_init_metrics(&net->ipv6.ip6_blk_hole_entry->dst,
222 ip6_template_metrics, true);
223 +
224 + net->ipv6.ip6_failed_policy_entry =
225 + kmemdup(&ip6_failed_policy_entry_template,
226 + sizeof(*net->ipv6.ip6_failed_policy_entry), GFP_KERNEL);
227 + if (!net->ipv6.ip6_failed_policy_entry)
228 + goto out_ip6_blk_hole_entry;
229 + net->ipv6.ip6_failed_policy_entry->dst.path =
230 + (struct dst_entry *)net->ipv6.ip6_failed_policy_entry;
231 + net->ipv6.ip6_failed_policy_entry->dst.ops = &net->ipv6.ip6_dst_ops;
232 + dst_init_metrics(&net->ipv6.ip6_failed_policy_entry->dst,
233 + ip6_template_metrics, true);
234 #endif
235
236 net->ipv6.sysctl.flush_delay = 0;
237 @@ -3055,6 +3104,8 @@
238 return ret;
239
240 #ifdef CONFIG_IPV6_MULTIPLE_TABLES
241 +out_ip6_blk_hole_entry:
242 + kfree(net->ipv6.ip6_blk_hole_entry);
243 out_ip6_prohibit_entry:
244 kfree(net->ipv6.ip6_prohibit_entry);
245 out_ip6_null_entry:
246 @@ -3072,6 +3123,7 @@
247 #ifdef CONFIG_IPV6_MULTIPLE_TABLES
248 kfree(net->ipv6.ip6_prohibit_entry);
249 kfree(net->ipv6.ip6_blk_hole_entry);
250 + kfree(net->ipv6.ip6_failed_policy_entry);
251 #endif
252 dst_entries_destroy(&net->ipv6.ip6_dst_ops);
253 }
254 @@ -3168,6 +3220,9 @@
255 init_net.ipv6.ip6_prohibit_entry->rt6i_idev = in6_dev_get(init_net.loopback_dev);
256 init_net.ipv6.ip6_blk_hole_entry->dst.dev = init_net.loopback_dev;
257 init_net.ipv6.ip6_blk_hole_entry->rt6i_idev = in6_dev_get(init_net.loopback_dev);
258 + init_net.ipv6.ip6_failed_policy_entry->dst.dev = init_net.loopback_dev;
259 + init_net.ipv6.ip6_failed_policy_entry->rt6i_idev =
260 + in6_dev_get(init_net.loopback_dev);
261 #endif
262 ret = fib6_init();
263 if (ret)