can: gw: Fix error path of cgw_module_init
This patch add error path for cgw_module_init to avoid possible crash if
some error occurs.
Fixes: c1aabdf379
("can-gw: add netlink based CAN routing")
Signed-off-by: YueHaibing <yuehaibing@huawei.com>
Acked-by: Oliver Hartkopp <socketcan@hartkopp.net>
Signed-off-by: Marc Kleine-Budde <mkl@pengutronix.de>
This commit is contained in:
parent
fee6a8923a
commit
b7a14297f1
46
net/can/gw.c
46
net/can/gw.c
|
@ -1046,32 +1046,50 @@ static __init int cgw_module_init(void)
|
||||||
pr_info("can: netlink gateway (rev " CAN_GW_VERSION ") max_hops=%d\n",
|
pr_info("can: netlink gateway (rev " CAN_GW_VERSION ") max_hops=%d\n",
|
||||||
max_hops);
|
max_hops);
|
||||||
|
|
||||||
register_pernet_subsys(&cangw_pernet_ops);
|
ret = register_pernet_subsys(&cangw_pernet_ops);
|
||||||
|
if (ret)
|
||||||
|
return ret;
|
||||||
|
|
||||||
|
ret = -ENOMEM;
|
||||||
cgw_cache = kmem_cache_create("can_gw", sizeof(struct cgw_job),
|
cgw_cache = kmem_cache_create("can_gw", sizeof(struct cgw_job),
|
||||||
0, 0, NULL);
|
0, 0, NULL);
|
||||||
|
|
||||||
if (!cgw_cache)
|
if (!cgw_cache)
|
||||||
return -ENOMEM;
|
goto out_cache_create;
|
||||||
|
|
||||||
/* set notifier */
|
/* set notifier */
|
||||||
notifier.notifier_call = cgw_notifier;
|
notifier.notifier_call = cgw_notifier;
|
||||||
register_netdevice_notifier(¬ifier);
|
ret = register_netdevice_notifier(¬ifier);
|
||||||
|
if (ret)
|
||||||
|
goto out_register_notifier;
|
||||||
|
|
||||||
ret = rtnl_register_module(THIS_MODULE, PF_CAN, RTM_GETROUTE,
|
ret = rtnl_register_module(THIS_MODULE, PF_CAN, RTM_GETROUTE,
|
||||||
NULL, cgw_dump_jobs, 0);
|
NULL, cgw_dump_jobs, 0);
|
||||||
if (ret) {
|
if (ret)
|
||||||
unregister_netdevice_notifier(¬ifier);
|
goto out_rtnl_register1;
|
||||||
kmem_cache_destroy(cgw_cache);
|
|
||||||
return -ENOBUFS;
|
|
||||||
}
|
|
||||||
|
|
||||||
/* Only the first call to rtnl_register_module can fail */
|
ret = rtnl_register_module(THIS_MODULE, PF_CAN, RTM_NEWROUTE,
|
||||||
rtnl_register_module(THIS_MODULE, PF_CAN, RTM_NEWROUTE,
|
cgw_create_job, NULL, 0);
|
||||||
cgw_create_job, NULL, 0);
|
if (ret)
|
||||||
rtnl_register_module(THIS_MODULE, PF_CAN, RTM_DELROUTE,
|
goto out_rtnl_register2;
|
||||||
cgw_remove_job, NULL, 0);
|
ret = rtnl_register_module(THIS_MODULE, PF_CAN, RTM_DELROUTE,
|
||||||
|
cgw_remove_job, NULL, 0);
|
||||||
|
if (ret)
|
||||||
|
goto out_rtnl_register3;
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
|
|
||||||
|
out_rtnl_register3:
|
||||||
|
rtnl_unregister(PF_CAN, RTM_NEWROUTE);
|
||||||
|
out_rtnl_register2:
|
||||||
|
rtnl_unregister(PF_CAN, RTM_GETROUTE);
|
||||||
|
out_rtnl_register1:
|
||||||
|
unregister_netdevice_notifier(¬ifier);
|
||||||
|
out_register_notifier:
|
||||||
|
kmem_cache_destroy(cgw_cache);
|
||||||
|
out_cache_create:
|
||||||
|
unregister_pernet_subsys(&cangw_pernet_ops);
|
||||||
|
|
||||||
|
return ret;
|
||||||
}
|
}
|
||||||
|
|
||||||
static __exit void cgw_module_exit(void)
|
static __exit void cgw_module_exit(void)
|
||||||
|
|
Loading…
Reference in New Issue