diff --git a/4_Network/Makefile b/4_Network/Makefile new file mode 100644 index 0000000..c6595e3 --- /dev/null +++ b/4_Network/Makefile @@ -0,0 +1,8 @@ +obj-m += net_monitor.o + +all: + make -C /lib/modules/$(shell uname -r)/build M=$(PWD) modules + +clean: + make -C /lib/modules/$(shell uname -r)/build M=$(PWD) clean + diff --git a/4_Network/net_monitor.c b/4_Network/net_monitor.c new file mode 100644 index 0000000..b079577 --- /dev/null +++ b/4_Network/net_monitor.c @@ -0,0 +1,42 @@ +#include +#include +#include +#include + +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); + +