]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blame - net/ipv4/xfrm4_policy.c
xfrm4: Reload skb header pointers after calling pskb_may_pull.
[mirror_ubuntu-bionic-kernel.git] / net / ipv4 / xfrm4_policy.c
CommitLineData
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>
4ec3b28c 18#include <net/vrf.h>
1da177e4 19
1da177e4
LT
20static struct xfrm_policy_afinfo xfrm4_policy_afinfo;
21
8f01cb08 22static 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 45static 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 54static 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 69static 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
74static int xfrm4_init_path(struct xfrm_dst *path, struct dst_entry *dst,
75 int nfheader_len)
76{
77 return 0;
78}
79
87c1e12b 80static 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
106static 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
4ec3b28c
DA
114 if (skb_dst(skb)) {
115 oif = vrf_master_ifindex(skb_dst(skb)->dev) ?
116 : skb_dst(skb)->dev->ifindex;
117 }
1da177e4 118
7e1dc7b6
DM
119 memset(fl4, 0, sizeof(struct flowi4));
120 fl4->flowi4_mark = skb->mark;
84502b5e 121 fl4->flowi4_oif = reverse ? skb->skb_iif : oif;
44b451f1 122
56f8a75c 123 if (!ip_is_fragment(iph)) {
1da177e4
LT
124 switch (iph->protocol) {
125 case IPPROTO_UDP:
ba4e58ec 126 case IPPROTO_UDPLITE:
1da177e4
LT
127 case IPPROTO_TCP:
128 case IPPROTO_SCTP:
9e999993 129 case IPPROTO_DCCP:
c615c9f3
WY
130 if (xprth + 4 < skb->data ||
131 pskb_may_pull(skb, xprth + 4 - skb->data)) {
ea673a4d
SK
132 __be16 *ports;
133
134 xprth = skb_network_header(skb) + iph->ihl * 4;
135 ports = (__be16 *)xprth;
1da177e4 136
9cce96df
DM
137 fl4->fl4_sport = ports[!!reverse];
138 fl4->fl4_dport = ports[!reverse];
1da177e4
LT
139 }
140 break;
141
142 case IPPROTO_ICMP:
1a14f1e5
SK
143 if (xprth + 2 < skb->data ||
144 pskb_may_pull(skb, xprth + 2 - skb->data)) {
ea673a4d
SK
145 u8 *icmp;
146
147 xprth = skb_network_header(skb) + iph->ihl * 4;
148 icmp = xprth;
1da177e4 149
9cce96df
DM
150 fl4->fl4_icmp_type = icmp[0];
151 fl4->fl4_icmp_code = icmp[1];
1da177e4
LT
152 }
153 break;
154
155 case IPPROTO_ESP:
1a14f1e5
SK
156 if (xprth + 4 < skb->data ||
157 pskb_may_pull(skb, xprth + 4 - skb->data)) {
ea673a4d
SK
158 __be32 *ehdr;
159
160 xprth = skb_network_header(skb) + iph->ihl * 4;
161 ehdr = (__be32 *)xprth;
1da177e4 162
9cce96df 163 fl4->fl4_ipsec_spi = ehdr[0];
1da177e4
LT
164 }
165 break;
166
167 case IPPROTO_AH:
1a14f1e5
SK
168 if (xprth + 8 < skb->data ||
169 pskb_may_pull(skb, xprth + 8 - skb->data)) {
ea673a4d
SK
170 __be32 *ah_hdr;
171
172 xprth = skb_network_header(skb) + iph->ihl * 4;
173 ah_hdr = (__be32 *)xprth;
1da177e4 174
9cce96df 175 fl4->fl4_ipsec_spi = ah_hdr[1];
1da177e4
LT
176 }
177 break;
178
179 case IPPROTO_COMP:
1a14f1e5
SK
180 if (xprth + 4 < skb->data ||
181 pskb_may_pull(skb, xprth + 4 - skb->data)) {
ea673a4d
SK
182 __be16 *ipcomp_hdr;
183
184 xprth = skb_network_header(skb) + iph->ihl * 4;
185 ipcomp_hdr = (__be16 *)xprth;
1da177e4 186
9cce96df 187 fl4->fl4_ipsec_spi = htonl(ntohs(ipcomp_hdr[1]));
1da177e4
LT
188 }
189 break;
cc9ff19d
TT
190
191 case IPPROTO_GRE:
1a14f1e5
SK
192 if (xprth + 12 < skb->data ||
193 pskb_may_pull(skb, xprth + 12 - skb->data)) {
ea673a4d
SK
194 __be16 *greflags;
195 __be32 *gre_hdr;
196
197 xprth = skb_network_header(skb) + iph->ihl * 4;
198 greflags = (__be16 *)xprth;
199 gre_hdr = (__be32 *)xprth;
cc9ff19d
TT
200
201 if (greflags[0] & GRE_KEY) {
202 if (greflags[0] & GRE_CSUM)
203 gre_hdr++;
9cce96df 204 fl4->fl4_gre_key = gre_hdr[1];
cc9ff19d
TT
205 }
206 }
207 break;
208
1da177e4 209 default:
9cce96df 210 fl4->fl4_ipsec_spi = 0;
1da177e4 211 break;
3ff50b79 212 }
1da177e4 213 }
7e1dc7b6
DM
214 fl4->flowi4_proto = iph->protocol;
215 fl4->daddr = reverse ? iph->saddr : iph->daddr;
216 fl4->saddr = reverse ? iph->daddr : iph->saddr;
217 fl4->flowi4_tos = iph->tos;
1da177e4
LT
218}
219
569d3645 220static inline int xfrm4_garbage_collect(struct dst_ops *ops)
1da177e4 221{
d7c7544c
AD
222 struct net *net = container_of(ops, struct net, xfrm.xfrm4_dst_ops);
223
224 xfrm4_policy_afinfo.garbage_collect(net);
fc66f95c 225 return (dst_entries_get_slow(ops) > ops->gc_thresh * 2);
1da177e4
LT
226}
227
6700c270
DM
228static void xfrm4_update_pmtu(struct dst_entry *dst, struct sock *sk,
229 struct sk_buff *skb, u32 mtu)
1da177e4
LT
230{
231 struct xfrm_dst *xdst = (struct xfrm_dst *)dst;
232 struct dst_entry *path = xdst->route;
233
6700c270 234 path->ops->update_pmtu(path, sk, skb, mtu);
1da177e4
LT
235}
236
6700c270
DM
237static void xfrm4_redirect(struct dst_entry *dst, struct sock *sk,
238 struct sk_buff *skb)
55be7a9c
DM
239{
240 struct xfrm_dst *xdst = (struct xfrm_dst *)dst;
241 struct dst_entry *path = xdst->route;
242
6700c270 243 path->ops->redirect(path, sk, skb);
55be7a9c
DM
244}
245
aabc9761
HX
246static void xfrm4_dst_destroy(struct dst_entry *dst)
247{
248 struct xfrm_dst *xdst = (struct xfrm_dst *)dst;
249
62fa8a84
DM
250 dst_destroy_metrics_generic(dst);
251
aabc9761
HX
252 xfrm_dst_destroy(xdst);
253}
254
255static void xfrm4_dst_ifdown(struct dst_entry *dst, struct net_device *dev,
256 int unregister)
257{
aabc9761
HX
258 if (!unregister)
259 return;
260
aabc9761
HX
261 xfrm_dst_ifdown(dst, dev);
262}
263
1da177e4
LT
264static struct dst_ops xfrm4_dst_ops = {
265 .family = AF_INET,
1da177e4
LT
266 .gc = xfrm4_garbage_collect,
267 .update_pmtu = xfrm4_update_pmtu,
55be7a9c 268 .redirect = xfrm4_redirect,
62fa8a84 269 .cow_metrics = dst_cow_metrics_generic,
aabc9761
HX
270 .destroy = xfrm4_dst_destroy,
271 .ifdown = xfrm4_dst_ifdown,
862b82c6 272 .local_out = __ip_local_out,
c386578f 273 .gc_thresh = INT_MAX,
1da177e4
LT
274};
275
276static struct xfrm_policy_afinfo xfrm4_policy_afinfo = {
277 .family = AF_INET,
1da177e4
LT
278 .dst_ops = &xfrm4_dst_ops,
279 .dst_lookup = xfrm4_dst_lookup,
a1e59abf 280 .get_saddr = xfrm4_get_saddr,
1da177e4 281 .decode_session = _decode_session4,
25ee3286 282 .get_tos = xfrm4_get_tos,
a1b05140 283 .init_path = xfrm4_init_path,
25ee3286 284 .fill_dst = xfrm4_fill_dst,
2774c131 285 .blackhole_route = ipv4_blackhole_route,
1da177e4
LT
286};
287
f816700a 288#ifdef CONFIG_SYSCTL
a44a4a00
NH
289static struct ctl_table xfrm4_policy_table[] = {
290 {
a44a4a00 291 .procname = "xfrm4_gc_thresh",
d7c7544c 292 .data = &init_net.xfrm.xfrm4_dst_ops.gc_thresh,
a44a4a00
NH
293 .maxlen = sizeof(int),
294 .mode = 0644,
295 .proc_handler = proc_dointvec,
296 },
297 { }
298};
299
8d068875
MK
300static int __net_init xfrm4_net_init(struct net *net)
301{
302 struct ctl_table *table;
303 struct ctl_table_header *hdr;
304
305 table = xfrm4_policy_table;
306 if (!net_eq(net, &init_net)) {
307 table = kmemdup(table, sizeof(xfrm4_policy_table), GFP_KERNEL);
308 if (!table)
309 goto err_alloc;
310
311 table[0].data = &net->xfrm.xfrm4_dst_ops.gc_thresh;
312 }
313
314 hdr = register_net_sysctl(net, "net/ipv4", table);
315 if (!hdr)
316 goto err_reg;
317
318 net->ipv4.xfrm4_hdr = hdr;
319 return 0;
320
321err_reg:
322 if (!net_eq(net, &init_net))
323 kfree(table);
324err_alloc:
325 return -ENOMEM;
326}
327
328static void __net_exit xfrm4_net_exit(struct net *net)
329{
330 struct ctl_table *table;
331
51456b29 332 if (!net->ipv4.xfrm4_hdr)
8d068875
MK
333 return;
334
335 table = net->ipv4.xfrm4_hdr->ctl_table_arg;
336 unregister_net_sysctl_table(net->ipv4.xfrm4_hdr);
337 if (!net_eq(net, &init_net))
338 kfree(table);
339}
340
341static struct pernet_operations __net_initdata xfrm4_net_ops = {
342 .init = xfrm4_net_init,
343 .exit = xfrm4_net_exit,
344};
f816700a 345#endif
a44a4a00 346
1da177e4
LT
347static void __init xfrm4_policy_init(void)
348{
349 xfrm_policy_register_afinfo(&xfrm4_policy_afinfo);
350}
351
703fb94e 352void __init xfrm4_init(void)
1da177e4 353{
fc66f95c 354 dst_entries_init(&xfrm4_dst_ops);
d7c7544c
AD
355
356 xfrm4_state_init();
357 xfrm4_policy_init();
2f32b51b 358 xfrm4_protocol_init();
f816700a 359#ifdef CONFIG_SYSCTL
8d068875 360 register_pernet_subsys(&xfrm4_net_ops);
f816700a 361#endif
1da177e4
LT
362}
363