]>
Commit | Line | Data |
---|---|---|
e905a9ed | 1 | /* |
1da177e4 LT |
2 | * xfrm4_policy.c |
3 | * | |
4 | * Changes: | |
5 | * Kazunori MIYAZAWA @USAGI | |
6 | * YOSHIFUJI Hideaki @USAGI | |
7 | * Split up af-specific portion | |
e905a9ed | 8 | * |
1da177e4 LT |
9 | */ |
10 | ||
66cdb3ca HX |
11 | #include <linux/err.h> |
12 | #include <linux/kernel.h> | |
aabc9761 | 13 | #include <linux/inetdevice.h> |
cc9ff19d | 14 | #include <linux/if_tunnel.h> |
45ff5a3f | 15 | #include <net/dst.h> |
1da177e4 LT |
16 | #include <net/xfrm.h> |
17 | #include <net/ip.h> | |
385add90 | 18 | #include <net/l3mdev.h> |
1da177e4 | 19 | |
1da177e4 LT |
20 | static struct xfrm_policy_afinfo xfrm4_policy_afinfo; |
21 | ||
8f01cb08 | 22 | static struct dst_entry *__xfrm4_dst_lookup(struct net *net, struct flowi4 *fl4, |
42a7b32b | 23 | int tos, int oif, |
8f01cb08 DM |
24 | const xfrm_address_t *saddr, |
25 | const xfrm_address_t *daddr) | |
1da177e4 | 26 | { |
66cdb3ca | 27 | struct rtable *rt; |
a1e59abf | 28 | |
8f01cb08 DM |
29 | memset(fl4, 0, sizeof(*fl4)); |
30 | fl4->daddr = daddr->a4; | |
31 | fl4->flowi4_tos = tos; | |
42a7b32b | 32 | fl4->flowi4_oif = oif; |
66cdb3ca | 33 | if (saddr) |
8f01cb08 | 34 | fl4->saddr = saddr->a4; |
66cdb3ca | 35 | |
58189ca7 DA |
36 | fl4->flowi4_flags = FLOWI_FLAG_SKIP_NH_OIF; |
37 | ||
8f01cb08 | 38 | rt = __ip_route_output_key(net, fl4); |
b23dd4fe DM |
39 | if (!IS_ERR(rt)) |
40 | return &rt->dst; | |
41 | ||
42 | return ERR_CAST(rt); | |
66cdb3ca HX |
43 | } |
44 | ||
42a7b32b | 45 | static struct dst_entry *xfrm4_dst_lookup(struct net *net, int tos, int oif, |
8f01cb08 DM |
46 | const xfrm_address_t *saddr, |
47 | const xfrm_address_t *daddr) | |
48 | { | |
49 | struct flowi4 fl4; | |
50 | ||
42a7b32b | 51 | return __xfrm4_dst_lookup(net, &fl4, tos, oif, saddr, daddr); |
8f01cb08 DM |
52 | } |
53 | ||
42a7b32b | 54 | static int xfrm4_get_saddr(struct net *net, int oif, |
fbda33b2 | 55 | xfrm_address_t *saddr, xfrm_address_t *daddr) |
66cdb3ca HX |
56 | { |
57 | struct dst_entry *dst; | |
8f01cb08 | 58 | struct flowi4 fl4; |
66cdb3ca | 59 | |
42a7b32b | 60 | dst = __xfrm4_dst_lookup(net, &fl4, 0, oif, NULL, daddr); |
66cdb3ca HX |
61 | if (IS_ERR(dst)) |
62 | return -EHOSTUNREACH; | |
63 | ||
8f01cb08 | 64 | saddr->a4 = fl4.saddr; |
66cdb3ca HX |
65 | dst_release(dst); |
66 | return 0; | |
a1e59abf PM |
67 | } |
68 | ||
05d84025 | 69 | static int xfrm4_get_tos(const struct flowi *fl) |
1da177e4 | 70 | { |
7e1dc7b6 | 71 | return IPTOS_RT_MASK & fl->u.ip4.flowi4_tos; /* Strip ECN bits */ |
25ee3286 | 72 | } |
1da177e4 | 73 | |
a1b05140 MN |
74 | static int xfrm4_init_path(struct xfrm_dst *path, struct dst_entry *dst, |
75 | int nfheader_len) | |
76 | { | |
77 | return 0; | |
78 | } | |
79 | ||
87c1e12b | 80 | static int xfrm4_fill_dst(struct xfrm_dst *xdst, struct net_device *dev, |
0c7b3eef | 81 | const struct flowi *fl) |
25ee3286 HX |
82 | { |
83 | struct rtable *rt = (struct rtable *)xdst->route; | |
7e1dc7b6 | 84 | const struct flowi4 *fl4 = &fl->u.ip4; |
1da177e4 | 85 | |
b7323396 | 86 | xdst->u.rt.rt_iif = fl4->flowi4_iif; |
1da177e4 | 87 | |
25ee3286 HX |
88 | xdst->u.dst.dev = dev; |
89 | dev_hold(dev); | |
43372262 | 90 | |
25ee3286 HX |
91 | /* Sheit... I remember I did this right. Apparently, |
92 | * it was magically lost, so this code needs audit */ | |
9917e1e8 | 93 | xdst->u.rt.rt_is_input = rt->rt_is_input; |
25ee3286 HX |
94 | xdst->u.rt.rt_flags = rt->rt_flags & (RTCF_BROADCAST | RTCF_MULTICAST | |
95 | RTCF_LOCAL); | |
96 | xdst->u.rt.rt_type = rt->rt_type; | |
25ee3286 | 97 | xdst->u.rt.rt_gateway = rt->rt_gateway; |
155e8336 | 98 | xdst->u.rt.rt_uses_gateway = rt->rt_uses_gateway; |
5943634f | 99 | xdst->u.rt.rt_pmtu = rt->rt_pmtu; |
b7503e0c | 100 | xdst->u.rt.rt_table_id = rt->rt_table_id; |
caacf05e | 101 | INIT_LIST_HEAD(&xdst->u.rt.rt_uncached); |
1da177e4 | 102 | |
1da177e4 | 103 | return 0; |
1da177e4 LT |
104 | } |
105 | ||
106 | static void | |
d5422efe | 107 | _decode_session4(struct sk_buff *skb, struct flowi *fl, int reverse) |
1da177e4 | 108 | { |
b71d1d42 | 109 | const struct iphdr *iph = ip_hdr(skb); |
d56f90a7 | 110 | u8 *xprth = skb_network_header(skb) + iph->ihl * 4; |
7e1dc7b6 | 111 | struct flowi4 *fl4 = &fl->u.ip4; |
84502b5e SK |
112 | int oif = 0; |
113 | ||
385add90 DA |
114 | if (skb_dst(skb)) |
115 | oif = l3mdev_fib_oif(skb_dst(skb)->dev); | |
1da177e4 | 116 | |
7e1dc7b6 DM |
117 | memset(fl4, 0, sizeof(struct flowi4)); |
118 | fl4->flowi4_mark = skb->mark; | |
84502b5e | 119 | fl4->flowi4_oif = reverse ? skb->skb_iif : oif; |
44b451f1 | 120 | |
56f8a75c | 121 | if (!ip_is_fragment(iph)) { |
1da177e4 LT |
122 | switch (iph->protocol) { |
123 | case IPPROTO_UDP: | |
ba4e58ec | 124 | case IPPROTO_UDPLITE: |
1da177e4 LT |
125 | case IPPROTO_TCP: |
126 | case IPPROTO_SCTP: | |
9e999993 | 127 | case IPPROTO_DCCP: |
c615c9f3 WY |
128 | if (xprth + 4 < skb->data || |
129 | pskb_may_pull(skb, xprth + 4 - skb->data)) { | |
ea673a4d SK |
130 | __be16 *ports; |
131 | ||
132 | xprth = skb_network_header(skb) + iph->ihl * 4; | |
133 | ports = (__be16 *)xprth; | |
1da177e4 | 134 | |
9cce96df DM |
135 | fl4->fl4_sport = ports[!!reverse]; |
136 | fl4->fl4_dport = ports[!reverse]; | |
1da177e4 LT |
137 | } |
138 | break; | |
139 | ||
140 | case IPPROTO_ICMP: | |
1a14f1e5 SK |
141 | if (xprth + 2 < skb->data || |
142 | pskb_may_pull(skb, xprth + 2 - skb->data)) { | |
ea673a4d SK |
143 | u8 *icmp; |
144 | ||
145 | xprth = skb_network_header(skb) + iph->ihl * 4; | |
146 | icmp = xprth; | |
1da177e4 | 147 | |
9cce96df DM |
148 | fl4->fl4_icmp_type = icmp[0]; |
149 | fl4->fl4_icmp_code = icmp[1]; | |
1da177e4 LT |
150 | } |
151 | break; | |
152 | ||
153 | case IPPROTO_ESP: | |
1a14f1e5 SK |
154 | if (xprth + 4 < skb->data || |
155 | pskb_may_pull(skb, xprth + 4 - skb->data)) { | |
ea673a4d SK |
156 | __be32 *ehdr; |
157 | ||
158 | xprth = skb_network_header(skb) + iph->ihl * 4; | |
159 | ehdr = (__be32 *)xprth; | |
1da177e4 | 160 | |
9cce96df | 161 | fl4->fl4_ipsec_spi = ehdr[0]; |
1da177e4 LT |
162 | } |
163 | break; | |
164 | ||
165 | case IPPROTO_AH: | |
1a14f1e5 SK |
166 | if (xprth + 8 < skb->data || |
167 | pskb_may_pull(skb, xprth + 8 - skb->data)) { | |
ea673a4d SK |
168 | __be32 *ah_hdr; |
169 | ||
170 | xprth = skb_network_header(skb) + iph->ihl * 4; | |
171 | ah_hdr = (__be32 *)xprth; | |
1da177e4 | 172 | |
9cce96df | 173 | fl4->fl4_ipsec_spi = ah_hdr[1]; |
1da177e4 LT |
174 | } |
175 | break; | |
176 | ||
177 | case IPPROTO_COMP: | |
1a14f1e5 SK |
178 | if (xprth + 4 < skb->data || |
179 | pskb_may_pull(skb, xprth + 4 - skb->data)) { | |
ea673a4d SK |
180 | __be16 *ipcomp_hdr; |
181 | ||
182 | xprth = skb_network_header(skb) + iph->ihl * 4; | |
183 | ipcomp_hdr = (__be16 *)xprth; | |
1da177e4 | 184 | |
9cce96df | 185 | fl4->fl4_ipsec_spi = htonl(ntohs(ipcomp_hdr[1])); |
1da177e4 LT |
186 | } |
187 | break; | |
cc9ff19d TT |
188 | |
189 | case IPPROTO_GRE: | |
1a14f1e5 SK |
190 | if (xprth + 12 < skb->data || |
191 | pskb_may_pull(skb, xprth + 12 - skb->data)) { | |
ea673a4d SK |
192 | __be16 *greflags; |
193 | __be32 *gre_hdr; | |
194 | ||
195 | xprth = skb_network_header(skb) + iph->ihl * 4; | |
196 | greflags = (__be16 *)xprth; | |
197 | gre_hdr = (__be32 *)xprth; | |
cc9ff19d TT |
198 | |
199 | if (greflags[0] & GRE_KEY) { | |
200 | if (greflags[0] & GRE_CSUM) | |
201 | gre_hdr++; | |
9cce96df | 202 | fl4->fl4_gre_key = gre_hdr[1]; |
cc9ff19d TT |
203 | } |
204 | } | |
205 | break; | |
206 | ||
1da177e4 | 207 | default: |
9cce96df | 208 | fl4->fl4_ipsec_spi = 0; |
1da177e4 | 209 | break; |
3ff50b79 | 210 | } |
1da177e4 | 211 | } |
7e1dc7b6 DM |
212 | fl4->flowi4_proto = iph->protocol; |
213 | fl4->daddr = reverse ? iph->saddr : iph->daddr; | |
214 | fl4->saddr = reverse ? iph->daddr : iph->saddr; | |
215 | fl4->flowi4_tos = iph->tos; | |
1da177e4 LT |
216 | } |
217 | ||
569d3645 | 218 | static inline int xfrm4_garbage_collect(struct dst_ops *ops) |
1da177e4 | 219 | { |
d7c7544c AD |
220 | struct net *net = container_of(ops, struct net, xfrm.xfrm4_dst_ops); |
221 | ||
222 | xfrm4_policy_afinfo.garbage_collect(net); | |
fc66f95c | 223 | return (dst_entries_get_slow(ops) > ops->gc_thresh * 2); |
1da177e4 LT |
224 | } |
225 | ||
6700c270 DM |
226 | static void xfrm4_update_pmtu(struct dst_entry *dst, struct sock *sk, |
227 | struct sk_buff *skb, u32 mtu) | |
1da177e4 LT |
228 | { |
229 | struct xfrm_dst *xdst = (struct xfrm_dst *)dst; | |
230 | struct dst_entry *path = xdst->route; | |
231 | ||
6700c270 | 232 | path->ops->update_pmtu(path, sk, skb, mtu); |
1da177e4 LT |
233 | } |
234 | ||
6700c270 DM |
235 | static void xfrm4_redirect(struct dst_entry *dst, struct sock *sk, |
236 | struct sk_buff *skb) | |
55be7a9c DM |
237 | { |
238 | struct xfrm_dst *xdst = (struct xfrm_dst *)dst; | |
239 | struct dst_entry *path = xdst->route; | |
240 | ||
6700c270 | 241 | path->ops->redirect(path, sk, skb); |
55be7a9c DM |
242 | } |
243 | ||
aabc9761 HX |
244 | static void xfrm4_dst_destroy(struct dst_entry *dst) |
245 | { | |
246 | struct xfrm_dst *xdst = (struct xfrm_dst *)dst; | |
247 | ||
62fa8a84 DM |
248 | dst_destroy_metrics_generic(dst); |
249 | ||
aabc9761 HX |
250 | xfrm_dst_destroy(xdst); |
251 | } | |
252 | ||
253 | static void xfrm4_dst_ifdown(struct dst_entry *dst, struct net_device *dev, | |
254 | int unregister) | |
255 | { | |
aabc9761 HX |
256 | if (!unregister) |
257 | return; | |
258 | ||
aabc9761 HX |
259 | xfrm_dst_ifdown(dst, dev); |
260 | } | |
261 | ||
1da177e4 LT |
262 | static struct dst_ops xfrm4_dst_ops = { |
263 | .family = AF_INET, | |
1da177e4 LT |
264 | .gc = xfrm4_garbage_collect, |
265 | .update_pmtu = xfrm4_update_pmtu, | |
55be7a9c | 266 | .redirect = xfrm4_redirect, |
62fa8a84 | 267 | .cow_metrics = dst_cow_metrics_generic, |
aabc9761 HX |
268 | .destroy = xfrm4_dst_destroy, |
269 | .ifdown = xfrm4_dst_ifdown, | |
862b82c6 | 270 | .local_out = __ip_local_out, |
c386578f | 271 | .gc_thresh = INT_MAX, |
1da177e4 LT |
272 | }; |
273 | ||
274 | static struct xfrm_policy_afinfo xfrm4_policy_afinfo = { | |
275 | .family = AF_INET, | |
1da177e4 LT |
276 | .dst_ops = &xfrm4_dst_ops, |
277 | .dst_lookup = xfrm4_dst_lookup, | |
a1e59abf | 278 | .get_saddr = xfrm4_get_saddr, |
1da177e4 | 279 | .decode_session = _decode_session4, |
25ee3286 | 280 | .get_tos = xfrm4_get_tos, |
a1b05140 | 281 | .init_path = xfrm4_init_path, |
25ee3286 | 282 | .fill_dst = xfrm4_fill_dst, |
2774c131 | 283 | .blackhole_route = ipv4_blackhole_route, |
1da177e4 LT |
284 | }; |
285 | ||
f816700a | 286 | #ifdef CONFIG_SYSCTL |
a44a4a00 NH |
287 | static struct ctl_table xfrm4_policy_table[] = { |
288 | { | |
a44a4a00 | 289 | .procname = "xfrm4_gc_thresh", |
d7c7544c | 290 | .data = &init_net.xfrm.xfrm4_dst_ops.gc_thresh, |
a44a4a00 NH |
291 | .maxlen = sizeof(int), |
292 | .mode = 0644, | |
293 | .proc_handler = proc_dointvec, | |
294 | }, | |
295 | { } | |
296 | }; | |
297 | ||
8d068875 MK |
298 | static int __net_init xfrm4_net_init(struct net *net) |
299 | { | |
300 | struct ctl_table *table; | |
301 | struct ctl_table_header *hdr; | |
302 | ||
303 | table = xfrm4_policy_table; | |
304 | if (!net_eq(net, &init_net)) { | |
305 | table = kmemdup(table, sizeof(xfrm4_policy_table), GFP_KERNEL); | |
306 | if (!table) | |
307 | goto err_alloc; | |
308 | ||
309 | table[0].data = &net->xfrm.xfrm4_dst_ops.gc_thresh; | |
310 | } | |
311 | ||
312 | hdr = register_net_sysctl(net, "net/ipv4", table); | |
313 | if (!hdr) | |
314 | goto err_reg; | |
315 | ||
316 | net->ipv4.xfrm4_hdr = hdr; | |
317 | return 0; | |
318 | ||
319 | err_reg: | |
320 | if (!net_eq(net, &init_net)) | |
321 | kfree(table); | |
322 | err_alloc: | |
323 | return -ENOMEM; | |
324 | } | |
325 | ||
326 | static void __net_exit xfrm4_net_exit(struct net *net) | |
327 | { | |
328 | struct ctl_table *table; | |
329 | ||
51456b29 | 330 | if (!net->ipv4.xfrm4_hdr) |
8d068875 MK |
331 | return; |
332 | ||
333 | table = net->ipv4.xfrm4_hdr->ctl_table_arg; | |
334 | unregister_net_sysctl_table(net->ipv4.xfrm4_hdr); | |
335 | if (!net_eq(net, &init_net)) | |
336 | kfree(table); | |
337 | } | |
338 | ||
339 | static struct pernet_operations __net_initdata xfrm4_net_ops = { | |
340 | .init = xfrm4_net_init, | |
341 | .exit = xfrm4_net_exit, | |
342 | }; | |
f816700a | 343 | #endif |
a44a4a00 | 344 | |
1da177e4 LT |
345 | static void __init xfrm4_policy_init(void) |
346 | { | |
347 | xfrm_policy_register_afinfo(&xfrm4_policy_afinfo); | |
348 | } | |
349 | ||
703fb94e | 350 | void __init xfrm4_init(void) |
1da177e4 | 351 | { |
fc66f95c | 352 | dst_entries_init(&xfrm4_dst_ops); |
d7c7544c AD |
353 | |
354 | xfrm4_state_init(); | |
355 | xfrm4_policy_init(); | |
2f32b51b | 356 | xfrm4_protocol_init(); |
f816700a | 357 | #ifdef CONFIG_SYSCTL |
8d068875 | 358 | register_pernet_subsys(&xfrm4_net_ops); |
f816700a | 359 | #endif |
1da177e4 LT |
360 | } |
361 |