Newer
Older
linux_kernel_hacking / 4_Network / net_monitor.c
@motoki miura motoki miura on 8 Dec 2023 1 KB sec 4
#include <linux/init.h>
#include <linux/module.h>
#include <linux/netdevice.h>
#include <linux/inetdevice.h>

MODULE_LICENSE("GPL");
MODULE_AUTHOR("Your Name");
MODULE_DESCRIPTION("Network Monitor Module");

static int interface_event_handler(struct notifier_block *nb, unsigned long event, void *ptr);

static struct notifier_block nb = {
    .notifier_call = interface_event_handler,
};

static int __init net_monitor_init(void) {
    register_netdevice_notifier(&nb);
    pr_info("Network Monitor Module initialized\n");
    return 0;
}

static void __exit net_monitor_exit(void) {
    unregister_netdevice_notifier(&nb);
    pr_info("Network Monitor Module exited\n");
}

static int interface_event_handler(struct notifier_block *nb, unsigned long event, void *ptr) {
    struct net_device *dev = netdev_notifier_info_to_dev(ptr);

    if (event == NETDEV_UP) {
        pr_info("Interface %s is UP\n", dev->name);
    } else if (event == NETDEV_DOWN) {
        pr_info("Interface %s is DOWN\n", dev->name);
    }

    return NOTIFY_OK;
}

module_init(net_monitor_init);
module_exit(net_monitor_exit);