Skip to content
This repository was archived by the owner on Nov 8, 2023. It is now read-only.

Commit 35b72be

Browse files
q2vengregkh
authored andcommitted
rtnetlink: Add bulk registration helpers for rtnetlink message handlers.
[ Upstream commit 07cc7b0 ] Before commit addf9b9 ("net: rtnetlink: use rcu to free rtnl message handlers"), once rtnl_msg_handlers[protocol] was allocated, the following rtnl_register_module() for the same protocol never failed. However, after the commit, rtnl_msg_handler[protocol][msgtype] needs to be allocated in each rtnl_register_module(), so each call could fail. Many callers of rtnl_register_module() do not handle the returned error, and we need to add many error handlings. To handle that easily, let's add wrapper functions for bulk registration of rtnetlink message handlers. Signed-off-by: Kuniyuki Iwashima <[email protected]> Signed-off-by: Paolo Abeni <[email protected]> Stable-dep-of: 78b7b99 ("vxlan: Handle error of rtnl_register_module().") Signed-off-by: Sasha Levin <[email protected]>
1 parent eae7435 commit 35b72be

File tree

2 files changed

+46
-0
lines changed

2 files changed

+46
-0
lines changed

include/net/rtnetlink.h

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,13 +27,30 @@ static inline enum rtnl_kinds rtnl_msgtype_kind(int msgtype)
2727
return msgtype & RTNL_KIND_MASK;
2828
}
2929

30+
struct rtnl_msg_handler {
31+
struct module *owner;
32+
int protocol;
33+
int msgtype;
34+
rtnl_doit_func doit;
35+
rtnl_dumpit_func dumpit;
36+
int flags;
37+
};
38+
3039
void rtnl_register(int protocol, int msgtype,
3140
rtnl_doit_func, rtnl_dumpit_func, unsigned int flags);
3241
int rtnl_register_module(struct module *owner, int protocol, int msgtype,
3342
rtnl_doit_func, rtnl_dumpit_func, unsigned int flags);
3443
int rtnl_unregister(int protocol, int msgtype);
3544
void rtnl_unregister_all(int protocol);
3645

46+
int __rtnl_register_many(const struct rtnl_msg_handler *handlers, int n);
47+
void __rtnl_unregister_many(const struct rtnl_msg_handler *handlers, int n);
48+
49+
#define rtnl_register_many(handlers) \
50+
__rtnl_register_many(handlers, ARRAY_SIZE(handlers))
51+
#define rtnl_unregister_many(handlers) \
52+
__rtnl_unregister_many(handlers, ARRAY_SIZE(handlers))
53+
3754
static inline int rtnl_msg_family(const struct nlmsghdr *nlh)
3855
{
3956
if (nlmsg_len(nlh) >= sizeof(struct rtgenmsg))

net/core/rtnetlink.c

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -389,6 +389,35 @@ void rtnl_unregister_all(int protocol)
389389
}
390390
EXPORT_SYMBOL_GPL(rtnl_unregister_all);
391391

392+
int __rtnl_register_many(const struct rtnl_msg_handler *handlers, int n)
393+
{
394+
const struct rtnl_msg_handler *handler;
395+
int i, err;
396+
397+
for (i = 0, handler = handlers; i < n; i++, handler++) {
398+
err = rtnl_register_internal(handler->owner, handler->protocol,
399+
handler->msgtype, handler->doit,
400+
handler->dumpit, handler->flags);
401+
if (err) {
402+
__rtnl_unregister_many(handlers, i);
403+
break;
404+
}
405+
}
406+
407+
return err;
408+
}
409+
EXPORT_SYMBOL_GPL(__rtnl_register_many);
410+
411+
void __rtnl_unregister_many(const struct rtnl_msg_handler *handlers, int n)
412+
{
413+
const struct rtnl_msg_handler *handler;
414+
int i;
415+
416+
for (i = n - 1, handler = handlers + n - 1; i >= 0; i--, handler--)
417+
rtnl_unregister(handler->protocol, handler->msgtype);
418+
}
419+
EXPORT_SYMBOL_GPL(__rtnl_unregister_many);
420+
392421
static LIST_HEAD(link_ops);
393422

394423
static const struct rtnl_link_ops *rtnl_link_ops_get(const char *kind)

0 commit comments

Comments
 (0)