postage/postage.c

98 lines
2 KiB
C
Raw Normal View History

2024-07-14 17:09:46 +02:00
#include <linux/kernel.h>
#include <linux/module.h>
#include <linux/stddef.h>
#include <linux/netdevice.h>
MODULE_LICENSE("GPL");
struct net_device *dev;
static int my_eth_init(struct net_device *dev)
{
// Initialize device
netif_start_queue(dev);
2024-07-14 22:17:29 +02:00
printk(KERN_INFO "Postens initades\n");
2024-07-14 17:09:46 +02:00
return 0;
}
static void my_eth_exit(struct net_device *dev)
{
// Cleanup device
2024-07-14 22:17:29 +02:00
pr_info(KERN_INFO "Posten slutade\n");
2024-07-14 17:09:46 +02:00
netif_stop_queue(dev);
}
static netdev_tx_t my_eth_start_xmit(struct sk_buff *skb,
struct net_device *dev)
{
// Start xmit
2024-07-14 22:17:29 +02:00
printk(KERN_INFO "Posten ska skicka nåt\n");
2024-07-14 17:09:46 +02:00
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
};
2024-07-14 22:17:29 +02:00
void init_poster(struct net_device *dev)
2024-07-14 17:09:46 +02:00
{
dev->netdev_ops = &my_netdev_ops;
2024-07-14 22:17:29 +02:00
printk(KERN_INFO "Postens allokerades\n");
2024-07-14 17:09:46 +02:00
}
struct net_device testcard;
int register_device(void) {
2024-07-14 22:17:29 +02:00
printk(KERN_INFO "Posten skapas\n");
dev = alloc_netdev(0, "post%d", NET_NAME_UNKNOWN, init_poster);
2024-07-14 17:09:46 +02:00
if (!dev)
return -ENOMEM;
if (register_netdev(dev)) {
2024-07-14 22:17:29 +02:00
printk(KERN_ERR "Men vafan Linux vill inte registrera postjävel :(\n");
2024-07-14 17:09:46 +02:00
free_netdev(dev);
return -ENODEV;
}
return 0;
}
int __init custom_init(void)
{
int err;
if ((err = register_device())) {
2024-07-14 22:17:29 +02:00
printk(KERN_ERR "Postjävel funkar inte\n");
2024-07-14 17:09:46 +02:00
return err;
}
return 0;
}
void __exit custom_exit(void)
{
if (!dev) {
2024-07-14 22:17:29 +02:00
printk(KERN_ERR "Postjävel finns inte :/\n");
2024-07-14 17:09:46 +02:00
return;
}
unregister_netdev(dev);
free_netdev(dev);
2024-07-14 22:17:29 +02:00
printk(KERN_INFO "Posten försvann :,)\n");
2024-07-14 17:09:46 +02:00
}
module_init(custom_init);
module_exit(custom_exit);