commit 8bd315602349bf7d1d47bba0bfc981ecc5cc0a3d Author: vanten-s Date: Sun Jul 14 17:09:46 2024 +0200 IT WORKS YIPPEEEE diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..075c5a3 --- /dev/null +++ b/Makefile @@ -0,0 +1 @@ +obj-m += postage.o diff --git a/build.sh b/build.sh new file mode 100755 index 0000000..389395d --- /dev/null +++ b/build.sh @@ -0,0 +1 @@ +make -C $(nix-build -E '(import {}).linux.dev' --no-out-link)/lib/modules/*/build M=$(pwd) modules diff --git a/clean.sh b/clean.sh new file mode 100755 index 0000000..5d3ce62 --- /dev/null +++ b/clean.sh @@ -0,0 +1 @@ +make -C $(nix-build -E '(import {}).linux.dev' --no-out-link)/lib/modules/*/build M=$(pwd) clean diff --git a/postage.c b/postage.c new file mode 100644 index 0000000..19091d6 --- /dev/null +++ b/postage.c @@ -0,0 +1,101 @@ +#include +#include +#include +#include + +MODULE_LICENSE("GPL"); + +struct net_device *dev; + +static int my_eth_init(struct net_device *dev) +{ + // Initialize device + printk(KERN_INFO "My Ethernet device initialized\n"); + netif_start_queue(dev); + return 0; + +} + +static void my_eth_exit(struct net_device *dev) +{ + // Cleanup device + pr_info(KERN_INFO "My Ethernet device exited\n"); + netif_stop_queue(dev); +} + +static netdev_tx_t my_eth_start_xmit(struct sk_buff *skb, + struct net_device *dev) +{ + // Start xmit + printk(KERN_INFO "My Ethernet device start xmit\n"); + printk("len: %d\n", skb->len); + for (int i = 0; i < skb->len; i++) { + printk("data: %c\n", skb->data[i]); + } + printk("protocol: %d\n", skb->protocol); + + netif_receive_skb(skb); + + return NETDEV_TX_OK; +} + +static struct net_device_ops my_netdev_ops = { + .ndo_init = my_eth_init, + .ndo_uninit = my_eth_exit, + .ndo_start_xmit = my_eth_start_xmit +}; + +void init_testcard(struct net_device *dev) +{ + dev->netdev_ops = &my_netdev_ops; + printk("Init testcard\n"); +} + +struct net_device testcard; + +int register_device(void) { + dev = alloc_netdev(0, "myeth%d", NET_NAME_UNKNOWN, init_testcard); + + if (!dev) + return -ENOMEM; + + printk("Registering\n"); + + if (register_netdev(dev)) { + printk("Failed :(\n"); + free_netdev(dev); + return -ENODEV; + } + + printk("Registered\n"); + + return 0; +} + +int __init custom_init(void) +{ + int err; + if ((err = register_device())) { + printk("Error loading postage\n"); + return err; + } + + return 0; +} + +void __exit custom_exit(void) +{ + if (!dev) { + printk(KERN_ERR "Ethernet device not found\n"); + return; + } + + unregister_netdev(dev); + printk(KERN_INFO "Ethernet device unregistered\n"); + + free_netdev(dev); + printk(KERN_INFO "Ethernet device freed\n"); +} + +module_init(custom_init); +module_exit(custom_exit); diff --git a/shell.nix b/shell.nix new file mode 100644 index 0000000..4c4d325 --- /dev/null +++ b/shell.nix @@ -0,0 +1,14 @@ +{ pkgs ? import {} }: + +pkgs.mkShell rec { + buildInputs = with pkgs; [ + linux.dev + gcc + wireshark + gnumake + ]; + shellHook = '' + export PATH="$PATH:$PWD/opt/bin" + export CC="i686-elf-gcc" + ''; +}