#include <linux/module.h>
#include <linux/init.h>
#include <asm/atomic.h>
#include <linux/kthread.h>
#include <linux/delay.h>
#include <linux/netfilter.h>
#include <linux/skbuff.h>
#include <linux/netdevice.h>
#include <net/net_namespace.h>
#include <linux/list.h>
#include <linux/string.h>
#include <linux/inetdevice.h>
#include <net/route.h>
#include <net/inet_sock.h>
#include <net/protocol.h>
#include <net/sock.h>
#include <linux/if_ether.h>
#include <linux/etherdevice.h>
#define err(msg) printk(KERN_ALERT "%s\n", msg)
#define ETH_P_ME 0x0801
extern struct net init_net;
static int me_rcv(struct sk_buff *skb, struct net_device *dev, struct packet_type *pt, struct net_device *orig_dev);
static struct packet_type me_packet_type = {
.type = cpu_to_be16(ETH_P_ME),
.func = me_rcv,
};
static int me_rcv(struct sk_buff *skb, struct net_device *dev, struct packet_type *pt, struct net_device *orig_dev)
{
printk(KERN_INFO "%s\n", skb_pull(skb, 0));
kfree_skb(skb);
return NET_RX_SUCCESS;
}
static int maininit(void)
{
struct sk_buff *skb;
struct ethhdr *ethhdr;
struct net_device *dev;
char *data;
int ret;
dev_add_pack(&me_packet_type);
dev = dev_get_by_name(&init_net, "eth0");
if (!dev) {
err("dev_get_by_name");
goto err;
}
skb = alloc_skb(200, GFP_KERNEL);
if (!skb) {
err("alloc_skb");
goto err;
}
skb_reserve(skb, 2);
ethhdr = (struct ethhdr *)skb_put(skb, sizeof(struct ethhdr));
ethhdr->h_proto = htons(ETH_P_ME); /* note network byteorder */
skb->mac_header = (unsigned char *)ethhdr;
skb->pkt_type = PACKET_HOST;
skb->dev = dev;
skb->protocol = eth_type_trans(skb, dev);
data = skb_put(skb, 12);
memcpy(data, "hello world", 12);
ret = netif_receive_skb(skb);
if (ret != 0) {
err("netif_receive_skb");
goto err1;
}
dev_put(dev);
return 0;
err1:
kfree_skb(skb);
err:
dev_remove_pack(&me_packet_type);
return -1;
}
static void mainexit(void)
{
dev_remove_pack(&me_packet_type);
}
module_init(maininit);
module_exit(mainexit);
MODULE_LICENSE("GPL");
|