Skip to content

Commit 25ee328

Browse files
herbertxdavem330
authored andcommitted
[IPSEC]: Merge common code into xfrm_bundle_create
Half of the code in xfrm4_bundle_create and xfrm6_bundle_create are common. This patch extracts that logic and puts it into xfrm_bundle_create. The rest of it are then accessed through afinfo. As a result this fixes the problem with inter-family transforms where we treat every xfrm dst in the bundle as if it belongs to the top family. This patch also fixes a long-standing error-path bug where we may free the xfrm states twice. Signed-off-by: Herbert Xu <herbert@gondor.apana.org.au> Signed-off-by: David S. Miller <davem@davemloft.net>
1 parent 66cdb3c commit 25ee328

4 files changed

Lines changed: 211 additions & 253 deletions

File tree

‎include/net/xfrm.h‎

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -227,6 +227,7 @@ struct km_event
227227
u32 event;
228228
};
229229

230+
struct net_device;
230231
struct xfrm_type;
231232
struct xfrm_dst;
232233
struct xfrm_policy_afinfo {
@@ -237,13 +238,11 @@ struct xfrm_policy_afinfo {
237238
xfrm_address_t *daddr);
238239
int (*get_saddr)(xfrm_address_t *saddr, xfrm_address_t *daddr);
239240
struct dst_entry *(*find_bundle)(struct flowi *fl, struct xfrm_policy *policy);
240-
int (*bundle_create)(struct xfrm_policy *policy,
241-
struct xfrm_state **xfrm,
242-
int nx,
243-
struct flowi *fl,
244-
struct dst_entry **dst_p);
245241
void (*decode_session)(struct sk_buff *skb,
246242
struct flowi *fl);
243+
int (*get_tos)(struct flowi *fl);
244+
int (*fill_dst)(struct xfrm_dst *xdst,
245+
struct net_device *dev);
247246
};
248247

249248
extern int xfrm_policy_register_afinfo(struct xfrm_policy_afinfo *afinfo);
@@ -1094,7 +1093,6 @@ static inline int xfrm4_udp_encap_rcv(struct sock *sk, struct sk_buff *skb)
10941093
}
10951094
#endif
10961095

1097-
extern struct dst_entry *xfrm_dst_lookup(struct xfrm_state *x, int tos);
10981096
struct xfrm_policy *xfrm_policy_alloc(gfp_t gfp);
10991097
extern int xfrm_policy_walk(u8 type, int (*func)(struct xfrm_policy *, int, int, void*), void *);
11001098
int xfrm_policy_insert(int dir, struct xfrm_policy *policy, int excl);
@@ -1113,7 +1111,6 @@ extern int xfrm_policy_flush(u8 type, struct xfrm_audit *audit_info);
11131111
extern int xfrm_sk_policy_insert(struct sock *sk, int dir, struct xfrm_policy *pol);
11141112
extern int xfrm_bundle_ok(struct xfrm_policy *pol, struct xfrm_dst *xdst,
11151113
struct flowi *fl, int family, int strict);
1116-
extern void xfrm_init_pmtu(struct dst_entry *dst);
11171114

11181115
#ifdef CONFIG_XFRM_MIGRATE
11191116
extern int km_migrate(struct xfrm_selector *sel, u8 dir, u8 type,

‎net/ipv4/xfrm4_policy.c‎

Lines changed: 26 additions & 108 deletions
Original file line numberDiff line numberDiff line change
@@ -79,122 +79,39 @@ __xfrm4_find_bundle(struct flowi *fl, struct xfrm_policy *policy)
7979
return dst;
8080
}
8181

82-
/* Allocate chain of dst_entry's, attach known xfrm's, calculate
83-
* all the metrics... Shortly, bundle a bundle.
84-
*/
85-
86-
static int
87-
__xfrm4_bundle_create(struct xfrm_policy *policy, struct xfrm_state **xfrm, int nx,
88-
struct flowi *fl, struct dst_entry **dst_p)
82+
static int xfrm4_get_tos(struct flowi *fl)
8983
{
90-
struct dst_entry *dst, *dst_prev;
91-
struct rtable *rt0 = (struct rtable*)(*dst_p);
92-
struct rtable *rt = rt0;
93-
int tos = fl->fl4_tos;
94-
int i;
95-
int err;
96-
int header_len = 0;
97-
int trailer_len = 0;
98-
99-
dst = dst_prev = NULL;
100-
dst_hold(&rt->u.dst);
101-
102-
for (i = 0; i < nx; i++) {
103-
struct dst_entry *dst1 = dst_alloc(&xfrm4_dst_ops);
104-
struct xfrm_dst *xdst;
105-
106-
if (unlikely(dst1 == NULL)) {
107-
err = -ENOBUFS;
108-
dst_release(&rt->u.dst);
109-
goto error;
110-
}
84+
return fl->fl4_tos;
85+
}
11186

112-
if (!dst)
113-
dst = dst1;
114-
else {
115-
dst_prev->child = dst1;
116-
dst1->flags |= DST_NOHASH;
117-
dst_clone(dst1);
118-
}
87+
static int xfrm4_fill_dst(struct xfrm_dst *xdst, struct net_device *dev)
88+
{
89+
struct rtable *rt = (struct rtable *)xdst->route;
11990

120-
xdst = (struct xfrm_dst *)dst1;
121-
xdst->route = &rt->u.dst;
122-
xdst->genid = xfrm[i]->genid;
91+
xdst->u.rt.fl = rt->fl;
12392

124-
dst1->next = dst_prev;
125-
dst_prev = dst1;
93+
xdst->u.dst.dev = dev;
94+
dev_hold(dev);
12695

127-
header_len += xfrm[i]->props.header_len;
128-
trailer_len += xfrm[i]->props.trailer_len;
96+
xdst->u.rt.idev = in_dev_get(dev);
97+
if (!xdst->u.rt.idev)
98+
return -ENODEV;
12999

130-
if (xfrm[i]->props.mode != XFRM_MODE_TRANSPORT) {
131-
dst1 = xfrm_dst_lookup(xfrm[i], tos);
132-
err = PTR_ERR(dst1);
133-
if (IS_ERR(dst1))
134-
goto error;
100+
xdst->u.rt.peer = rt->peer;
101+
if (rt->peer)
102+
atomic_inc(&rt->peer->refcnt);
135103

136-
rt = (struct rtable *)dst1;
137-
} else
138-
dst_hold(&rt->u.dst);
139-
}
104+
/* Sheit... I remember I did this right. Apparently,
105+
* it was magically lost, so this code needs audit */
106+
xdst->u.rt.rt_flags = rt->rt_flags & (RTCF_BROADCAST | RTCF_MULTICAST |
107+
RTCF_LOCAL);
108+
xdst->u.rt.rt_type = rt->rt_type;
109+
xdst->u.rt.rt_src = rt->rt_src;
110+
xdst->u.rt.rt_dst = rt->rt_dst;
111+
xdst->u.rt.rt_gateway = rt->rt_gateway;
112+
xdst->u.rt.rt_spec_dst = rt->rt_spec_dst;
140113

141-
dst_prev->child = &rt->u.dst;
142-
dst->path = &rt->u.dst;
143-
144-
/* Copy neighbout for reachability confirmation */
145-
dst->neighbour = neigh_clone(rt->u.dst.neighbour);
146-
147-
*dst_p = dst;
148-
dst = dst_prev;
149-
150-
dst_prev = *dst_p;
151-
i = 0;
152-
err = -ENODEV;
153-
for (; dst_prev != &rt->u.dst; dst_prev = dst_prev->child) {
154-
struct xfrm_dst *x = (struct xfrm_dst*)dst_prev;
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-
goto error;
161-
dev_hold(rt->u.dst.dev);
162-
163-
x->u.rt.idev = in_dev_get(rt->u.dst.dev);
164-
if (!x->u.rt.idev)
165-
goto error;
166-
167-
dst_prev->obsolete = -1;
168-
dst_prev->flags |= DST_HOST;
169-
dst_prev->lastuse = jiffies;
170-
dst_prev->header_len = header_len;
171-
dst_prev->trailer_len = trailer_len;
172-
memcpy(&dst_prev->metrics, &x->route->metrics, sizeof(dst_prev->metrics));
173-
174-
dst_prev->input = dst_discard;
175-
dst_prev->output = dst_prev->xfrm->outer_mode->afinfo->output;
176-
if (rt0->peer)
177-
atomic_inc(&rt0->peer->refcnt);
178-
x->u.rt.peer = rt0->peer;
179-
/* Sheit... I remember I did this right. Apparently,
180-
* it was magically lost, so this code needs audit */
181-
x->u.rt.rt_flags = rt0->rt_flags&(RTCF_BROADCAST|RTCF_MULTICAST|RTCF_LOCAL);
182-
x->u.rt.rt_type = rt0->rt_type;
183-
x->u.rt.rt_src = rt0->rt_src;
184-
x->u.rt.rt_dst = rt0->rt_dst;
185-
x->u.rt.rt_gateway = rt0->rt_gateway;
186-
x->u.rt.rt_spec_dst = rt0->rt_spec_dst;
187-
header_len -= x->u.dst.xfrm->props.header_len;
188-
trailer_len -= x->u.dst.xfrm->props.trailer_len;
189-
}
190-
191-
xfrm_init_pmtu(dst);
192114
return 0;
193-
194-
error:
195-
if (dst)
196-
dst_free(dst);
197-
return err;
198115
}
199116

200117
static void
@@ -330,8 +247,9 @@ static struct xfrm_policy_afinfo xfrm4_policy_afinfo = {
330247
.dst_lookup = xfrm4_dst_lookup,
331248
.get_saddr = xfrm4_get_saddr,
332249
.find_bundle = __xfrm4_find_bundle,
333-
.bundle_create = __xfrm4_bundle_create,
334250
.decode_session = _decode_session4,
251+
.get_tos = xfrm4_get_tos,
252+
.fill_dst = xfrm4_fill_dst,
335253
};
336254

337255
static void __init xfrm4_policy_init(void)

‎net/ipv6/xfrm6_policy.c‎

Lines changed: 22 additions & 114 deletions
Original file line numberDiff line numberDiff line change
@@ -93,126 +93,33 @@ __xfrm6_find_bundle(struct flowi *fl, struct xfrm_policy *policy)
9393
return dst;
9494
}
9595

96-
/* Allocate chain of dst_entry's, attach known xfrm's, calculate
97-
* all the metrics... Shortly, bundle a bundle.
98-
*/
99-
100-
static int
101-
__xfrm6_bundle_create(struct xfrm_policy *policy, struct xfrm_state **xfrm, int nx,
102-
struct flowi *fl, struct dst_entry **dst_p)
96+
static int xfrm6_get_tos(struct flowi *fl)
10397
{
104-
struct dst_entry *dst, *dst_prev;
105-
struct rt6_info *rt0 = (struct rt6_info*)(*dst_p);
106-
struct rt6_info *rt = rt0;
107-
int i;
108-
int err;
109-
int header_len = 0;
110-
int trailer_len = 0;
111-
112-
dst = dst_prev = NULL;
113-
dst_hold(&rt->u.dst);
114-
115-
for (i = 0; i < nx; i++) {
116-
struct dst_entry *dst1 = dst_alloc(&xfrm6_dst_ops);
117-
struct xfrm_dst *xdst;
98+
return 0;
99+
}
118100

119-
if (unlikely(dst1 == NULL)) {
120-
err = -ENOBUFS;
121-
dst_release(&rt->u.dst);
122-
goto error;
123-
}
101+
static int xfrm6_fill_dst(struct xfrm_dst *xdst, struct net_device *dev)
102+
{
103+
struct rt6_info *rt = (struct rt6_info*)xdst->route;
124104

125-
if (!dst)
126-
dst = dst1;
127-
else {
128-
dst_prev->child = dst1;
129-
dst1->flags |= DST_NOHASH;
130-
dst_clone(dst1);
131-
}
105+
xdst->u.dst.dev = dev;
106+
dev_hold(dev);
132107

133-
xdst = (struct xfrm_dst *)dst1;
134-
xdst->route = &rt->u.dst;
135-
xdst->genid = xfrm[i]->genid;
136-
if (rt->rt6i_node)
137-
xdst->route_cookie = rt->rt6i_node->fn_sernum;
138-
139-
dst1->next = dst_prev;
140-
dst_prev = dst1;
141-
142-
if (xfrm[i]->type->flags & XFRM_TYPE_NON_FRAGMENT)
143-
((struct rt6_info *)dst)->nfheader_len +=
144-
xfrm[i]->props.header_len;
145-
header_len += xfrm[i]->props.header_len;
146-
trailer_len += xfrm[i]->props.trailer_len;
147-
148-
if (xfrm[i]->props.mode != XFRM_MODE_TRANSPORT) {
149-
dst1 = xfrm_dst_lookup(xfrm[i], 0);
150-
err = PTR_ERR(dst1);
151-
if (IS_ERR(dst1))
152-
goto error;
153-
154-
rt = (struct rt6_info *)dst1;
155-
} else
156-
dst_hold(&rt->u.dst);
157-
}
108+
xdst->u.rt6.rt6i_idev = in6_dev_get(rt->u.dst.dev);
109+
if (!xdst->u.rt6.rt6i_idev)
110+
return -ENODEV;
158111

159-
dst_prev->child = &rt->u.dst;
160-
dst->path = &rt->u.dst;
161-
162-
/* Copy neighbour for reachability confirmation */
163-
dst->neighbour = neigh_clone(rt->u.dst.neighbour);
164-
165-
if (rt->rt6i_node)
166-
((struct xfrm_dst *)dst)->path_cookie = rt->rt6i_node->fn_sernum;
167-
168-
*dst_p = dst;
169-
dst = dst_prev;
170-
171-
dst_prev = *dst_p;
172-
i = 0;
173-
err = -ENODEV;
174-
for (; dst_prev != &rt->u.dst; dst_prev = dst_prev->child) {
175-
struct xfrm_dst *x = (struct xfrm_dst*)dst_prev;
176-
177-
dst_prev->xfrm = xfrm[i++];
178-
dst_prev->dev = rt->u.dst.dev;
179-
if (!rt->u.dst.dev)
180-
goto error;
181-
dev_hold(rt->u.dst.dev);
182-
183-
x->u.rt6.rt6i_idev = in6_dev_get(rt->u.dst.dev);
184-
if (!x->u.rt6.rt6i_idev)
185-
goto error;
186-
187-
dst_prev->obsolete = -1;
188-
dst_prev->flags |= DST_HOST;
189-
dst_prev->lastuse = jiffies;
190-
dst_prev->header_len = header_len;
191-
dst_prev->trailer_len = trailer_len;
192-
memcpy(&dst_prev->metrics, &x->route->metrics, sizeof(dst_prev->metrics));
193-
194-
dst_prev->input = dst_discard;
195-
dst_prev->output = dst_prev->xfrm->outer_mode->afinfo->output;
196-
/* Sheit... I remember I did this right. Apparently,
197-
* it was magically lost, so this code needs audit */
198-
x->u.rt6.rt6i_flags = rt0->rt6i_flags&(RTF_ANYCAST|RTF_LOCAL);
199-
x->u.rt6.rt6i_metric = rt0->rt6i_metric;
200-
x->u.rt6.rt6i_node = rt0->rt6i_node;
201-
x->u.rt6.rt6i_gateway = rt0->rt6i_gateway;
202-
memcpy(&x->u.rt6.rt6i_gateway, &rt0->rt6i_gateway, sizeof(x->u.rt6.rt6i_gateway));
203-
x->u.rt6.rt6i_dst = rt0->rt6i_dst;
204-
x->u.rt6.rt6i_src = rt0->rt6i_src;
205-
header_len -= x->u.dst.xfrm->props.header_len;
206-
trailer_len -= x->u.dst.xfrm->props.trailer_len;
207-
}
112+
/* Sheit... I remember I did this right. Apparently,
113+
* it was magically lost, so this code needs audit */
114+
xdst->u.rt6.rt6i_flags = rt->rt6i_flags & (RTF_ANYCAST |
115+
RTF_LOCAL);
116+
xdst->u.rt6.rt6i_metric = rt->rt6i_metric;
117+
xdst->u.rt6.rt6i_node = rt->rt6i_node;
118+
xdst->u.rt6.rt6i_gateway = rt->rt6i_gateway;
119+
xdst->u.rt6.rt6i_dst = rt->rt6i_dst;
120+
xdst->u.rt6.rt6i_src = rt->rt6i_src;
208121

209-
xfrm_init_pmtu(dst);
210122
return 0;
211-
212-
error:
213-
if (dst)
214-
dst_free(dst);
215-
return err;
216123
}
217124

218125
static inline void
@@ -355,8 +262,9 @@ static struct xfrm_policy_afinfo xfrm6_policy_afinfo = {
355262
.dst_lookup = xfrm6_dst_lookup,
356263
.get_saddr = xfrm6_get_saddr,
357264
.find_bundle = __xfrm6_find_bundle,
358-
.bundle_create = __xfrm6_bundle_create,
359265
.decode_session = _decode_session6,
266+
.get_tos = xfrm6_get_tos,
267+
.fill_dst = xfrm6_fill_dst,
360268
};
361269

362270
static void __init xfrm6_policy_init(void)

0 commit comments

Comments
 (0)