Skip to content

Commit 9ab1546

Browse files
Richard Alpedavem330
authored andcommitted
tipc: convert legacy nl bearer enable/disable to nl compat
Introduce a framework for transcoding legacy nl action into actions (.doit) calls from the new nl API. This is done by converting the incoming TLV data into netlink data with nested netlink attributes. Unfortunately due to the randomness of the legacy API we can't do this generically so each legacy netlink command requires a specific transcoding recipe. In this case for bearer enable and bearer disable. Convert TIPC_CMD_ENABLE_BEARER and TIPC_CMD_DISABLE_BEARER into doit compat calls. Signed-off-by: Richard Alpe <richard.alpe@ericsson.com> Reviewed-by: Erik Hugne <erik.hugne@ericsson.com> Reviewed-by: Ying Xue <ying.xue@windriver.com> Reviewed-by: Jon Maloy <jon.maloy@ericsson.com> Signed-off-by: David S. Miller <davem@davemloft.net>
1 parent d0796d1 commit 9ab1546

5 files changed

Lines changed: 154 additions & 57 deletions

File tree

‎include/uapi/linux/tipc_config.h‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -277,6 +277,11 @@ static inline int TLV_GET_LEN(struct tlv_desc *tlv)
277277
return ntohs(tlv->tlv_len);
278278
}
279279

280+
static inline int TLV_CHECK_TYPE(struct tlv_desc *tlv, __u16 type)
281+
{
282+
return (ntohs(tlv->tlv_type) == type);
283+
}
284+
280285
static inline int TLV_SET(void *tlv, __u16 type, void *data, __u16 len)
281286
{
282287
struct tlv_desc *tlv_ptr;

‎net/tipc/bearer.c‎

Lines changed: 5 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -236,8 +236,8 @@ void tipc_bearer_remove_dest(struct net *net, u32 bearer_id, u32 dest)
236236
/**
237237
* tipc_enable_bearer - enable bearer with the given name
238238
*/
239-
int tipc_enable_bearer(struct net *net, const char *name, u32 disc_domain,
240-
u32 priority)
239+
static int tipc_enable_bearer(struct net *net, const char *name,
240+
u32 disc_domain, u32 priority)
241241
{
242242
struct tipc_net *tn = net_generic(net, tipc_net_id);
243243
struct tipc_bearer *b_ptr;
@@ -393,22 +393,6 @@ static void bearer_disable(struct net *net, struct tipc_bearer *b_ptr,
393393
kfree_rcu(b_ptr, rcu);
394394
}
395395

396-
int tipc_disable_bearer(struct net *net, const char *name)
397-
{
398-
struct tipc_bearer *b_ptr;
399-
int res;
400-
401-
b_ptr = tipc_bearer_find(net, name);
402-
if (b_ptr == NULL) {
403-
pr_warn("Attempt to disable unknown bearer <%s>\n", name);
404-
res = -EINVAL;
405-
} else {
406-
bearer_disable(net, b_ptr, false);
407-
res = 0;
408-
}
409-
return res;
410-
}
411-
412396
int tipc_enable_l2_media(struct net *net, struct tipc_bearer *b)
413397
{
414398
struct net_device *dev;
@@ -756,7 +740,7 @@ int tipc_nl_bearer_disable(struct sk_buff *skb, struct genl_info *info)
756740
char *name;
757741
struct tipc_bearer *bearer;
758742
struct nlattr *attrs[TIPC_NLA_BEARER_MAX + 1];
759-
struct net *net = genl_info_net(info);
743+
struct net *net = sock_net(skb->sk);
760744

761745
if (!info->attrs[TIPC_NLA_BEARER])
762746
return -EINVAL;
@@ -787,11 +771,11 @@ int tipc_nl_bearer_disable(struct sk_buff *skb, struct genl_info *info)
787771

788772
int tipc_nl_bearer_enable(struct sk_buff *skb, struct genl_info *info)
789773
{
790-
struct net *net = genl_info_net(info);
791-
struct tipc_net *tn = net_generic(net, tipc_net_id);
792774
int err;
793775
char *bearer;
794776
struct nlattr *attrs[TIPC_NLA_BEARER_MAX + 1];
777+
struct net *net = sock_net(skb->sk);
778+
struct tipc_net *tn = net_generic(net, tipc_net_id);
795779
u32 domain;
796780
u32 prio;
797781

‎net/tipc/bearer.h‎

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -173,9 +173,6 @@ struct tipc_bearer_names {
173173
*/
174174

175175
void tipc_rcv(struct net *net, struct sk_buff *skb, struct tipc_bearer *b_ptr);
176-
int tipc_enable_bearer(struct net *net, const char *bearer_name,
177-
u32 disc_domain, u32 priority);
178-
int tipc_disable_bearer(struct net *net, const char *name);
179176

180177
/*
181178
* Routines made available to TIPC by supported media types

‎net/tipc/config.c‎

Lines changed: 0 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -134,33 +134,6 @@ static struct sk_buff *tipc_show_stats(void)
134134
return buf;
135135
}
136136

137-
static struct sk_buff *cfg_enable_bearer(struct net *net)
138-
{
139-
struct tipc_bearer_config *args;
140-
141-
if (!TLV_CHECK(req_tlv_area, req_tlv_space, TIPC_TLV_BEARER_CONFIG))
142-
return tipc_cfg_reply_error_string(TIPC_CFG_TLV_ERROR);
143-
144-
args = (struct tipc_bearer_config *)TLV_DATA(req_tlv_area);
145-
if (tipc_enable_bearer(net, args->name,
146-
ntohl(args->disc_domain),
147-
ntohl(args->priority)))
148-
return tipc_cfg_reply_error_string("unable to enable bearer");
149-
150-
return tipc_cfg_reply_none();
151-
}
152-
153-
static struct sk_buff *cfg_disable_bearer(struct net *net)
154-
{
155-
if (!TLV_CHECK(req_tlv_area, req_tlv_space, TIPC_TLV_BEARER_NAME))
156-
return tipc_cfg_reply_error_string(TIPC_CFG_TLV_ERROR);
157-
158-
if (tipc_disable_bearer(net, (char *)TLV_DATA(req_tlv_area)))
159-
return tipc_cfg_reply_error_string("unable to disable bearer");
160-
161-
return tipc_cfg_reply_none();
162-
}
163-
164137
static struct sk_buff *cfg_set_own_addr(struct net *net)
165138
{
166139
struct tipc_net *tn = net_generic(net, tipc_net_id);
@@ -267,12 +240,6 @@ struct sk_buff *tipc_cfg_do_cmd(struct net *net, u32 orig_node, u16 cmd,
267240
rep_tlv_buf = tipc_link_cmd_config(net, req_tlv_area,
268241
req_tlv_space, cmd);
269242
break;
270-
case TIPC_CMD_ENABLE_BEARER:
271-
rep_tlv_buf = cfg_enable_bearer(net);
272-
break;
273-
case TIPC_CMD_DISABLE_BEARER:
274-
rep_tlv_buf = cfg_disable_bearer(net);
275-
break;
276243
case TIPC_CMD_SET_NODE_ADDR:
277244
rep_tlv_buf = cfg_set_own_addr(net);
278245
break;

‎net/tipc/netlink_compat.c‎

Lines changed: 144 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@
4949
struct tipc_nl_compat_msg {
5050
u16 cmd;
5151
int rep_size;
52+
int req_type;
5253
struct sk_buff *rep;
5354
struct tlv_desc *req;
5455
struct sock *dst_sk;
@@ -59,6 +60,11 @@ struct tipc_nl_compat_cmd_dump {
5960
int (*format)(struct tipc_nl_compat_msg *msg, struct nlattr **attrs);
6061
};
6162

63+
struct tipc_nl_compat_cmd_doit {
64+
int (*doit)(struct sk_buff *skb, struct genl_info *info);
65+
int (*transcode)(struct sk_buff *skb, struct tipc_nl_compat_msg *msg);
66+
};
67+
6268
static int tipc_skb_tailroom(struct sk_buff *skb)
6369
{
6470
int tailroom;
@@ -213,6 +219,78 @@ static int tipc_nl_compat_dumpit(struct tipc_nl_compat_cmd_dump *cmd,
213219
return err;
214220
}
215221

222+
static int __tipc_nl_compat_doit(struct tipc_nl_compat_cmd_doit *cmd,
223+
struct tipc_nl_compat_msg *msg)
224+
{
225+
int err;
226+
struct sk_buff *doit_buf;
227+
struct sk_buff *trans_buf;
228+
struct nlattr **attrbuf;
229+
struct genl_info info;
230+
231+
trans_buf = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
232+
if (!trans_buf)
233+
return -ENOMEM;
234+
235+
err = (*cmd->transcode)(trans_buf, msg);
236+
if (err)
237+
goto trans_out;
238+
239+
attrbuf = kmalloc((tipc_genl_family.maxattr + 1) *
240+
sizeof(struct nlattr *), GFP_KERNEL);
241+
if (!attrbuf) {
242+
err = -ENOMEM;
243+
goto trans_out;
244+
}
245+
246+
err = nla_parse(attrbuf, tipc_genl_family.maxattr,
247+
(const struct nlattr *)trans_buf->data,
248+
trans_buf->len, NULL);
249+
if (err)
250+
goto parse_out;
251+
252+
doit_buf = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
253+
if (!doit_buf) {
254+
err = -ENOMEM;
255+
goto parse_out;
256+
}
257+
258+
doit_buf->sk = msg->dst_sk;
259+
260+
memset(&info, 0, sizeof(info));
261+
info.attrs = attrbuf;
262+
263+
err = (*cmd->doit)(doit_buf, &info);
264+
265+
kfree_skb(doit_buf);
266+
parse_out:
267+
kfree(attrbuf);
268+
trans_out:
269+
kfree_skb(trans_buf);
270+
271+
return err;
272+
}
273+
274+
static int tipc_nl_compat_doit(struct tipc_nl_compat_cmd_doit *cmd,
275+
struct tipc_nl_compat_msg *msg)
276+
{
277+
int err;
278+
279+
if (msg->req_type && !TLV_CHECK_TYPE(msg->req, msg->req_type))
280+
return -EINVAL;
281+
282+
err = __tipc_nl_compat_doit(cmd, msg);
283+
if (err)
284+
return err;
285+
286+
/* The legacy API considered an empty message a success message */
287+
msg->rep = tipc_tlv_alloc(0);
288+
if (!msg->rep)
289+
return -ENOMEM;
290+
291+
return 0;
292+
}
293+
216294
static int tipc_nl_compat_bearer_dump(struct tipc_nl_compat_msg *msg,
217295
struct nlattr **attrs)
218296
{
@@ -226,18 +304,82 @@ static int tipc_nl_compat_bearer_dump(struct tipc_nl_compat_msg *msg,
226304
nla_len(bearer[TIPC_NLA_BEARER_NAME]));
227305
}
228306

307+
static int tipc_nl_compat_bearer_enable(struct sk_buff *skb,
308+
struct tipc_nl_compat_msg *msg)
309+
{
310+
struct nlattr *prop;
311+
struct nlattr *bearer;
312+
struct tipc_bearer_config *b;
313+
314+
b = (struct tipc_bearer_config *)TLV_DATA(msg->req);
315+
316+
bearer = nla_nest_start(skb, TIPC_NLA_BEARER);
317+
if (!bearer)
318+
return -EMSGSIZE;
319+
320+
if (nla_put_string(skb, TIPC_NLA_BEARER_NAME, b->name))
321+
return -EMSGSIZE;
322+
323+
if (nla_put_u32(skb, TIPC_NLA_BEARER_DOMAIN, ntohl(b->disc_domain)))
324+
return -EMSGSIZE;
325+
326+
if (ntohl(b->priority) <= TIPC_MAX_LINK_PRI) {
327+
prop = nla_nest_start(skb, TIPC_NLA_BEARER_PROP);
328+
if (!prop)
329+
return -EMSGSIZE;
330+
if (nla_put_u32(skb, TIPC_NLA_PROP_PRIO, ntohl(b->priority)))
331+
return -EMSGSIZE;
332+
nla_nest_end(skb, prop);
333+
}
334+
nla_nest_end(skb, bearer);
335+
336+
return 0;
337+
}
338+
339+
static int tipc_nl_compat_bearer_disable(struct sk_buff *skb,
340+
struct tipc_nl_compat_msg *msg)
341+
{
342+
char *name;
343+
struct nlattr *bearer;
344+
345+
name = (char *)TLV_DATA(msg->req);
346+
347+
bearer = nla_nest_start(skb, TIPC_NLA_BEARER);
348+
if (!bearer)
349+
return -EMSGSIZE;
350+
351+
if (nla_put_string(skb, TIPC_NLA_BEARER_NAME, name))
352+
return -EMSGSIZE;
353+
354+
nla_nest_end(skb, bearer);
355+
356+
return 0;
357+
}
358+
229359
static int tipc_nl_compat_handle(struct tipc_nl_compat_msg *msg)
230360
{
231361
struct tipc_nl_compat_cmd_dump dump;
362+
struct tipc_nl_compat_cmd_doit doit;
232363

233364
memset(&dump, 0, sizeof(dump));
365+
memset(&doit, 0, sizeof(doit));
234366

235367
switch (msg->cmd) {
236368
case TIPC_CMD_GET_BEARER_NAMES:
237369
msg->rep_size = MAX_BEARERS * TLV_SPACE(TIPC_MAX_BEARER_NAME);
238370
dump.dumpit = tipc_nl_bearer_dump;
239371
dump.format = tipc_nl_compat_bearer_dump;
240372
return tipc_nl_compat_dumpit(&dump, msg);
373+
case TIPC_CMD_ENABLE_BEARER:
374+
msg->req_type = TIPC_TLV_BEARER_CONFIG;
375+
doit.doit = tipc_nl_bearer_enable;
376+
doit.transcode = tipc_nl_compat_bearer_enable;
377+
return tipc_nl_compat_doit(&doit, msg);
378+
case TIPC_CMD_DISABLE_BEARER:
379+
msg->req_type = TIPC_TLV_BEARER_NAME;
380+
doit.doit = tipc_nl_bearer_disable;
381+
doit.transcode = tipc_nl_compat_bearer_disable;
382+
return tipc_nl_compat_doit(&doit, msg);
241383
}
242384

243385
return -EOPNOTSUPP;
@@ -335,6 +477,8 @@ static int tipc_nl_compat_tmp_wrap(struct sk_buff *skb, struct genl_info *info)
335477

336478
switch (req->cmd) {
337479
case TIPC_CMD_GET_BEARER_NAMES:
480+
case TIPC_CMD_ENABLE_BEARER:
481+
case TIPC_CMD_DISABLE_BEARER:
338482
return tipc_nl_compat_recv(skb, info);
339483
}
340484

0 commit comments

Comments
 (0)