18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0-only
28c2ecf20Sopenharmony_ci#include <linux/kernel.h>
38c2ecf20Sopenharmony_ci#include <linux/module.h>
48c2ecf20Sopenharmony_ci#include <linux/netdevice.h>
58c2ecf20Sopenharmony_ci
68c2ecf20Sopenharmony_ci#include "notifier-error-inject.h"
78c2ecf20Sopenharmony_ci
88c2ecf20Sopenharmony_cistatic int priority;
98c2ecf20Sopenharmony_cimodule_param(priority, int, 0);
108c2ecf20Sopenharmony_ciMODULE_PARM_DESC(priority, "specify netdevice notifier priority");
118c2ecf20Sopenharmony_ci
128c2ecf20Sopenharmony_cistatic struct notifier_err_inject netdev_notifier_err_inject = {
138c2ecf20Sopenharmony_ci	.actions = {
148c2ecf20Sopenharmony_ci		{ NOTIFIER_ERR_INJECT_ACTION(NETDEV_REGISTER) },
158c2ecf20Sopenharmony_ci		{ NOTIFIER_ERR_INJECT_ACTION(NETDEV_CHANGEMTU) },
168c2ecf20Sopenharmony_ci		{ NOTIFIER_ERR_INJECT_ACTION(NETDEV_CHANGENAME) },
178c2ecf20Sopenharmony_ci		{ NOTIFIER_ERR_INJECT_ACTION(NETDEV_PRE_UP) },
188c2ecf20Sopenharmony_ci		{ NOTIFIER_ERR_INJECT_ACTION(NETDEV_PRE_TYPE_CHANGE) },
198c2ecf20Sopenharmony_ci		{ NOTIFIER_ERR_INJECT_ACTION(NETDEV_POST_INIT) },
208c2ecf20Sopenharmony_ci		{ NOTIFIER_ERR_INJECT_ACTION(NETDEV_PRECHANGEMTU) },
218c2ecf20Sopenharmony_ci		{ NOTIFIER_ERR_INJECT_ACTION(NETDEV_PRECHANGEUPPER) },
228c2ecf20Sopenharmony_ci		{ NOTIFIER_ERR_INJECT_ACTION(NETDEV_CHANGEUPPER) },
238c2ecf20Sopenharmony_ci		{}
248c2ecf20Sopenharmony_ci	}
258c2ecf20Sopenharmony_ci};
268c2ecf20Sopenharmony_ci
278c2ecf20Sopenharmony_cistatic struct dentry *dir;
288c2ecf20Sopenharmony_ci
298c2ecf20Sopenharmony_cistatic int netdev_err_inject_init(void)
308c2ecf20Sopenharmony_ci{
318c2ecf20Sopenharmony_ci	int err;
328c2ecf20Sopenharmony_ci
338c2ecf20Sopenharmony_ci	dir = notifier_err_inject_init("netdev", notifier_err_inject_dir,
348c2ecf20Sopenharmony_ci				       &netdev_notifier_err_inject, priority);
358c2ecf20Sopenharmony_ci	if (IS_ERR(dir))
368c2ecf20Sopenharmony_ci		return PTR_ERR(dir);
378c2ecf20Sopenharmony_ci
388c2ecf20Sopenharmony_ci	err = register_netdevice_notifier(&netdev_notifier_err_inject.nb);
398c2ecf20Sopenharmony_ci	if (err)
408c2ecf20Sopenharmony_ci		debugfs_remove_recursive(dir);
418c2ecf20Sopenharmony_ci
428c2ecf20Sopenharmony_ci	return err;
438c2ecf20Sopenharmony_ci}
448c2ecf20Sopenharmony_ci
458c2ecf20Sopenharmony_cistatic void netdev_err_inject_exit(void)
468c2ecf20Sopenharmony_ci{
478c2ecf20Sopenharmony_ci	unregister_netdevice_notifier(&netdev_notifier_err_inject.nb);
488c2ecf20Sopenharmony_ci	debugfs_remove_recursive(dir);
498c2ecf20Sopenharmony_ci}
508c2ecf20Sopenharmony_ci
518c2ecf20Sopenharmony_cimodule_init(netdev_err_inject_init);
528c2ecf20Sopenharmony_cimodule_exit(netdev_err_inject_exit);
538c2ecf20Sopenharmony_ci
548c2ecf20Sopenharmony_ciMODULE_DESCRIPTION("Netdevice notifier error injection module");
558c2ecf20Sopenharmony_ciMODULE_LICENSE("GPL");
568c2ecf20Sopenharmony_ciMODULE_AUTHOR("Nikolay Aleksandrov <razor@blackwall.org>");
57