]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blame - net/ipv4/xfrm4_policy.c
[SK_BUFF]: Introduce skb_network_header()
[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
aabc9761 11#include <linux/compiler.h>
aabc9761 12#include <linux/inetdevice.h>
1da177e4
LT
13#include <net/xfrm.h>
14#include <net/ip.h>
15
16static struct dst_ops xfrm4_dst_ops;
17static struct xfrm_policy_afinfo xfrm4_policy_afinfo;
18
1da177e4
LT
19static int xfrm4_dst_lookup(struct xfrm_dst **dst, struct flowi *fl)
20{
21 return __ip_route_output_key((struct rtable**)dst, fl);
22}
23
a1e59abf
PM
24static int xfrm4_get_saddr(xfrm_address_t *saddr, xfrm_address_t *daddr)
25{
26 struct rtable *rt;
27 struct flowi fl_tunnel = {
28 .nl_u = {
29 .ip4_u = {
30 .daddr = daddr->a4,
31 },
32 },
33 };
34
35 if (!xfrm4_dst_lookup((struct xfrm_dst **)&rt, &fl_tunnel)) {
36 saddr->a4 = rt->rt_src;
37 dst_release(&rt->u.dst);
38 return 0;
39 }
40 return -EHOSTUNREACH;
41}
42
1da177e4
LT
43static struct dst_entry *
44__xfrm4_find_bundle(struct flowi *fl, struct xfrm_policy *policy)
45{
46 struct dst_entry *dst;
47
48 read_lock_bh(&policy->lock);
49 for (dst = policy->bundles; dst; dst = dst->next) {
50 struct xfrm_dst *xdst = (struct xfrm_dst*)dst;
51 if (xdst->u.rt.fl.oif == fl->oif && /*XXX*/
52 xdst->u.rt.fl.fl4_dst == fl->fl4_dst &&
e905a9ed
YH
53 xdst->u.rt.fl.fl4_src == fl->fl4_src &&
54 xdst->u.rt.fl.fl4_tos == fl->fl4_tos &&
5b368e61 55 xfrm_bundle_ok(policy, xdst, fl, AF_INET, 0)) {
1da177e4
LT
56 dst_clone(dst);
57 break;
58 }
59 }
60 read_unlock_bh(&policy->lock);
61 return dst;
62}
63
64/* Allocate chain of dst_entry's, attach known xfrm's, calculate
65 * all the metrics... Shortly, bundle a bundle.
66 */
67
68static int
69__xfrm4_bundle_create(struct xfrm_policy *policy, struct xfrm_state **xfrm, int nx,
70 struct flowi *fl, struct dst_entry **dst_p)
71{
72 struct dst_entry *dst, *dst_prev;
73 struct rtable *rt0 = (struct rtable*)(*dst_p);
74 struct rtable *rt = rt0;
1da177e4
LT
75 struct flowi fl_tunnel = {
76 .nl_u = {
77 .ip4_u = {
43372262
MK
78 .saddr = fl->fl4_src,
79 .daddr = fl->fl4_dst,
4da3089f 80 .tos = fl->fl4_tos
1da177e4
LT
81 }
82 }
83 };
84 int i;
85 int err;
86 int header_len = 0;
87 int trailer_len = 0;
88
89 dst = dst_prev = NULL;
90 dst_hold(&rt->u.dst);
91
92 for (i = 0; i < nx; i++) {
93 struct dst_entry *dst1 = dst_alloc(&xfrm4_dst_ops);
94 struct xfrm_dst *xdst;
1da177e4
LT
95
96 if (unlikely(dst1 == NULL)) {
97 err = -ENOBUFS;
98 dst_release(&rt->u.dst);
99 goto error;
100 }
101
102 if (!dst)
103 dst = dst1;
104 else {
105 dst_prev->child = dst1;
106 dst1->flags |= DST_NOHASH;
107 dst_clone(dst1);
108 }
109
110 xdst = (struct xfrm_dst *)dst1;
111 xdst->route = &rt->u.dst;
9d4a706d 112 xdst->genid = xfrm[i]->genid;
1da177e4
LT
113
114 dst1->next = dst_prev;
115 dst_prev = dst1;
43372262 116
1da177e4
LT
117 header_len += xfrm[i]->props.header_len;
118 trailer_len += xfrm[i]->props.trailer_len;
119
43372262
MK
120 if (xfrm[i]->props.mode == XFRM_MODE_TUNNEL) {
121 unsigned short encap_family = xfrm[i]->props.family;
132adf54 122 switch (encap_family) {
43372262
MK
123 case AF_INET:
124 fl_tunnel.fl4_dst = xfrm[i]->id.daddr.a4;
125 fl_tunnel.fl4_src = xfrm[i]->props.saddr.a4;
126 break;
127#if defined(CONFIG_IPV6) || defined (CONFIG_IPV6_MODULE)
128 case AF_INET6:
129 ipv6_addr_copy(&fl_tunnel.fl6_dst, (struct in6_addr*)&xfrm[i]->id.daddr.a6);
130 ipv6_addr_copy(&fl_tunnel.fl6_src, (struct in6_addr*)&xfrm[i]->props.saddr.a6);
131 break;
132#endif
133 default:
134 BUG_ON(1);
135 }
1da177e4 136 err = xfrm_dst_lookup((struct xfrm_dst **)&rt,
43372262 137 &fl_tunnel, encap_family);
1da177e4
LT
138 if (err)
139 goto error;
140 } else
141 dst_hold(&rt->u.dst);
142 }
143
144 dst_prev->child = &rt->u.dst;
145 dst->path = &rt->u.dst;
146
147 *dst_p = dst;
148 dst = dst_prev;
149
150 dst_prev = *dst_p;
151 i = 0;
152 for (; dst_prev != &rt->u.dst; dst_prev = dst_prev->child) {
153 struct xfrm_dst *x = (struct xfrm_dst*)dst_prev;
43372262 154 struct xfrm_state_afinfo *afinfo;
1da177e4
LT
155 x->u.rt.fl = *fl;
156
157 dst_prev->xfrm = xfrm[i++];
158 dst_prev->dev = rt->u.dst.dev;
159 if (rt->u.dst.dev)
160 dev_hold(rt->u.dst.dev);
161 dst_prev->obsolete = -1;
162 dst_prev->flags |= DST_HOST;
163 dst_prev->lastuse = jiffies;
164 dst_prev->header_len = header_len;
1b5c2299 165 dst_prev->nfheader_len = 0;
1da177e4
LT
166 dst_prev->trailer_len = trailer_len;
167 memcpy(&dst_prev->metrics, &x->route->metrics, sizeof(dst_prev->metrics));
168
169 /* Copy neighbout for reachability confirmation */
170 dst_prev->neighbour = neigh_clone(rt->u.dst.neighbour);
171 dst_prev->input = rt->u.dst.input;
43372262
MK
172 /* XXX: When IPv6 module can be unloaded, we should manage reference
173 * to xfrm6_output in afinfo->output. Miyazawa
174 * */
175 afinfo = xfrm_state_get_afinfo(dst_prev->xfrm->props.family);
176 if (!afinfo) {
177 dst = *dst_p;
f2f2102d 178 err = -EAFNOSUPPORT;
43372262
MK
179 goto error;
180 }
181 dst_prev->output = afinfo->output;
182 xfrm_state_put_afinfo(afinfo);
183 if (dst_prev->xfrm->props.family == AF_INET && rt->peer)
1da177e4
LT
184 atomic_inc(&rt->peer->refcnt);
185 x->u.rt.peer = rt->peer;
186 /* Sheit... I remember I did this right. Apparently,
187 * it was magically lost, so this code needs audit */
188 x->u.rt.rt_flags = rt0->rt_flags&(RTCF_BROADCAST|RTCF_MULTICAST|RTCF_LOCAL);
189 x->u.rt.rt_type = rt->rt_type;
190 x->u.rt.rt_src = rt0->rt_src;
191 x->u.rt.rt_dst = rt0->rt_dst;
192 x->u.rt.rt_gateway = rt->rt_gateway;
193 x->u.rt.rt_spec_dst = rt0->rt_spec_dst;
aabc9761
HX
194 x->u.rt.idev = rt0->idev;
195 in_dev_hold(rt0->idev);
1da177e4
LT
196 header_len -= x->u.dst.xfrm->props.header_len;
197 trailer_len -= x->u.dst.xfrm->props.trailer_len;
198 }
199
200 xfrm_init_pmtu(dst);
201 return 0;
202
203error:
204 if (dst)
205 dst_free(dst);
206 return err;
207}
208
209static void
210_decode_session4(struct sk_buff *skb, struct flowi *fl)
211{
212 struct iphdr *iph = skb->nh.iph;
d56f90a7 213 u8 *xprth = skb_network_header(skb) + iph->ihl * 4;
1da177e4
LT
214
215 memset(fl, 0, sizeof(struct flowi));
216 if (!(iph->frag_off & htons(IP_MF | IP_OFFSET))) {
217 switch (iph->protocol) {
218 case IPPROTO_UDP:
ba4e58ec 219 case IPPROTO_UDPLITE:
1da177e4
LT
220 case IPPROTO_TCP:
221 case IPPROTO_SCTP:
9e999993 222 case IPPROTO_DCCP:
1da177e4 223 if (pskb_may_pull(skb, xprth + 4 - skb->data)) {
8c689a6e 224 __be16 *ports = (__be16 *)xprth;
1da177e4
LT
225
226 fl->fl_ip_sport = ports[0];
227 fl->fl_ip_dport = ports[1];
228 }
229 break;
230
231 case IPPROTO_ICMP:
232 if (pskb_may_pull(skb, xprth + 2 - skb->data)) {
233 u8 *icmp = xprth;
234
235 fl->fl_icmp_type = icmp[0];
236 fl->fl_icmp_code = icmp[1];
237 }
238 break;
239
240 case IPPROTO_ESP:
241 if (pskb_may_pull(skb, xprth + 4 - skb->data)) {
4324a174 242 __be32 *ehdr = (__be32 *)xprth;
1da177e4
LT
243
244 fl->fl_ipsec_spi = ehdr[0];
245 }
246 break;
247
248 case IPPROTO_AH:
249 if (pskb_may_pull(skb, xprth + 8 - skb->data)) {
4324a174 250 __be32 *ah_hdr = (__be32*)xprth;
1da177e4
LT
251
252 fl->fl_ipsec_spi = ah_hdr[1];
253 }
254 break;
255
256 case IPPROTO_COMP:
257 if (pskb_may_pull(skb, xprth + 4 - skb->data)) {
4324a174 258 __be16 *ipcomp_hdr = (__be16 *)xprth;
1da177e4 259
4195f814 260 fl->fl_ipsec_spi = htonl(ntohs(ipcomp_hdr[1]));
1da177e4
LT
261 }
262 break;
263 default:
264 fl->fl_ipsec_spi = 0;
265 break;
266 };
267 }
268 fl->proto = iph->protocol;
269 fl->fl4_dst = iph->daddr;
270 fl->fl4_src = iph->saddr;
4da3089f 271 fl->fl4_tos = iph->tos;
1da177e4
LT
272}
273
274static inline int xfrm4_garbage_collect(void)
275{
1da177e4 276 xfrm4_policy_afinfo.garbage_collect();
1da177e4
LT
277 return (atomic_read(&xfrm4_dst_ops.entries) > xfrm4_dst_ops.gc_thresh*2);
278}
279
280static void xfrm4_update_pmtu(struct dst_entry *dst, u32 mtu)
281{
282 struct xfrm_dst *xdst = (struct xfrm_dst *)dst;
283 struct dst_entry *path = xdst->route;
284
285 path->ops->update_pmtu(path, mtu);
286}
287
aabc9761
HX
288static void xfrm4_dst_destroy(struct dst_entry *dst)
289{
290 struct xfrm_dst *xdst = (struct xfrm_dst *)dst;
291
292 if (likely(xdst->u.rt.idev))
293 in_dev_put(xdst->u.rt.idev);
aef8811a 294 if (dst->xfrm && dst->xfrm->props.family == AF_INET && likely(xdst->u.rt.peer))
26db1677 295 inet_putpeer(xdst->u.rt.peer);
aabc9761
HX
296 xfrm_dst_destroy(xdst);
297}
298
299static void xfrm4_dst_ifdown(struct dst_entry *dst, struct net_device *dev,
300 int unregister)
301{
302 struct xfrm_dst *xdst;
303
304 if (!unregister)
305 return;
306
307 xdst = (struct xfrm_dst *)dst;
308 if (xdst->u.rt.idev->dev == dev) {
309 struct in_device *loopback_idev = in_dev_get(&loopback_dev);
310 BUG_ON(!loopback_idev);
311
312 do {
313 in_dev_put(xdst->u.rt.idev);
314 xdst->u.rt.idev = loopback_idev;
315 in_dev_hold(loopback_idev);
316 xdst = (struct xfrm_dst *)xdst->u.dst.child;
317 } while (xdst->u.dst.xfrm);
318
319 __in_dev_put(loopback_idev);
320 }
321
322 xfrm_dst_ifdown(dst, dev);
323}
324
1da177e4
LT
325static struct dst_ops xfrm4_dst_ops = {
326 .family = AF_INET,
327 .protocol = __constant_htons(ETH_P_IP),
328 .gc = xfrm4_garbage_collect,
329 .update_pmtu = xfrm4_update_pmtu,
aabc9761
HX
330 .destroy = xfrm4_dst_destroy,
331 .ifdown = xfrm4_dst_ifdown,
1da177e4
LT
332 .gc_thresh = 1024,
333 .entry_size = sizeof(struct xfrm_dst),
334};
335
336static struct xfrm_policy_afinfo xfrm4_policy_afinfo = {
337 .family = AF_INET,
1da177e4
LT
338 .dst_ops = &xfrm4_dst_ops,
339 .dst_lookup = xfrm4_dst_lookup,
a1e59abf 340 .get_saddr = xfrm4_get_saddr,
1da177e4
LT
341 .find_bundle = __xfrm4_find_bundle,
342 .bundle_create = __xfrm4_bundle_create,
343 .decode_session = _decode_session4,
344};
345
346static void __init xfrm4_policy_init(void)
347{
348 xfrm_policy_register_afinfo(&xfrm4_policy_afinfo);
349}
350
351static void __exit xfrm4_policy_fini(void)
352{
353 xfrm_policy_unregister_afinfo(&xfrm4_policy_afinfo);
354}
355
356void __init xfrm4_init(void)
357{
358 xfrm4_state_init();
359 xfrm4_policy_init();
360}
361