1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206
| #include <linux/init.h>
#include <linux/module.h>
#include <linux/skbuff.h>
#include <linux/netdevice.h>
#include <linux/ip.h>
#include <net/ip.h>
#include <Create6.h>
void compute_udp_checksum(struct iphdr *pIph, unsigned short *ipPayload) {
register unsigned long sum = 0;
struct udphdr *udphdrp = (struct udphdr*)(ipPayload);
unsigned short udpLen = htons(udphdrp->len);
sum += (pIph->saddr>>16)&0xFFFF;
sum += (pIph->saddr)&0xFFFF;
sum += (pIph->daddr>>16)&0xFFFF;
sum += (pIph->daddr)&0xFFFF;
sum += htons(IPPROTO_UDP);
sum += udphdrp->len;
udphdrp->check = 0;
while (udpLen > 1) {
sum += * ipPayload++;
udpLen -= 2;
}
if(udpLen > 0) {
sum += ((*ipPayload)&htons(0xFF00));
}
while (sum>>16) {
sum = (sum & 0xffff) + (sum >> 16);
}
sum = ~sum;
udphdrp->check = ((unsigned short)sum == 0x0000)?0xFFFF:(unsigned short)sum;
}
void compute_ip_checksum(struct iphdr* iphdrp){
iphdrp->check = 0;
iphdrp->check = compute_checksum((unsigned short*)iphdrp, iphdrp->ihl<<2);
}
static unsigned short compute_checksum(unsigned short *addr, unsigned int count) {
register unsigned long sum = 0;
while (count > 1) {
sum += * addr++;
count -= 2;
}
if(count > 0) {
sum += ((*addr)&htons(0xFF00));
}
while (sum>>16) {
sum = (sum & 0xffff) + (sum >> 16);
}
sum = ~sum;
return ((unsigned short)sum);
}
void aff_skb_info(struct sk_buff *skb){
printk(KERN_INFO "Skb : %x", (unsigned int) skb);
printk(KERN_INFO "Head : %x", (unsigned int) skb->head);
printk(KERN_INFO "Data : %x", (unsigned int) skb->data);
printk(KERN_INFO "Tail : %x", (unsigned int) skb->tail);
printk(KERN_INFO "End : %x", (unsigned int) skb->end);
printk(KERN_INFO "Mac header : %x", (unsigned int) skb->mac_header);
printk(KERN_INFO "Transport header : %x", (unsigned int) skb->transport_header);
printk(KERN_INFO "Network header : %x", (unsigned int) skb->network_header);
}
static int temp_init(void)
{
struct sk_buff *skb = NULL;
struct iphdr *newip = NULL;
struct udphdr *newudp = NULL;
struct ethhdr *neweth = NULL;
u_char *data_for = NULL;
int i=0;
printk(KERN_INFO "Create6 : Registering LKM...\n");
/************************************ ALLOCATION *****************************************/
printk(KERN_INFO " -------------------------------------- ALLOCATION -------------------------------------- \n");
skb = alloc_skb(320,GFP_KERNEL);
aff_skb_info(skb);
/************************************ RESERVE ESPACE HEADER ****************************************/
printk(KERN_INFO " -------------------------------------- RESERVE ESPACE HEADER -------------------------------------- \n");
skb_reserve(skb, 70);
aff_skb_info(skb);
/************************************ DATA ****************************************/
printk(KERN_INFO " -------------------------------------- DATA -------------------------------------- \n");
u_char payload[] = "AAAAAAAAAA";
u32 payload_len = 10;
skb_push(skb, payload_len);
memset(skb->data, 0, payload_len);
memcpy(skb->data, payload, payload_len);
aff_skb_info(skb);
i=1;
for(data_for = skb->data; data_for<skb->tail; data_for++ ){
printk(KERN_INFO "\tPayload %d\t: %.2x à l'adresse : %x", i, *data_for, (unsigned int) data_for);
i++;
}
/************************************ UDP *****************************************/
printk(KERN_INFO " -------------------------------------- UDP -------------------------------------- \n");
skb->transport_header = (void *)skb_push(skb, sizeof(struct udphdr));
memset(skb->data,0,sizeof(struct udphdr));
newudp = skb->transport_header
newudp->source = htons(59046);
newudp->dest = htons(5001);
newudp->len = htons(sizeof(struct udphdr));
newudp->check = 0;
newudp->check = csum_tcpudp_magic(in_aton("169.254.121.80"),in_aton("169.254.121.80"),sizeof(struct udphdr), IPPROTO_UDP,csum_partial(newudp, sizeof(struct udphdr), 0));
aff_skb_info(skb);
printk(KERN_INFO "Source Port : %x à l'adresse %x", newudp->source,&(newudp->source));
printk(KERN_INFO "Dest Port : %x à l'adresse %x", newudp->dest,&(newudp->dest));
printk(KERN_INFO "Len : %x à l'adresse %x", newudp->len,&(newudp->len));
printk(KERN_INFO "Check : %x à l'adresse %x", newudp->check,(&newudp->check));
i=1;
for(data_for = skb->data; data_for<skb->tail; data_for++ ){
printk(KERN_INFO "\tPayload %d\t: %.2x à l'adresse : %x", i, *data_for, (unsigned int) data_for);
i++;
}
/************************************ IP *****************************************/
printk(KERN_INFO " -------------------------------------- IP -------------------------------------- \n");
skb->network_header = (void *)skb_push(skb, sizeof(struct iphdr));
memset(skb->data,0,sizeof(struct iphdr));
newip = skb->network_header
newip->version = 4;
newip->ihl = 4;
newip->tos = 0;
newip->id = 0;
newip->frag_off = 0;
newip->ttl = 64;
newip->protocol = IPPROTO_UDP;
newip->saddr = in_aton("169.254.121.80");
newip->daddr = in_aton("169.254.121.80");
newip->check = 0;
compute_ip_checksum(newip);
aff_skb_info(skb);
printk(KERN_INFO "\n");
printk(KERN_INFO "Version : %x", newip->version);
printk(KERN_INFO "Ihl : %x", newip->ihl);
printk(KERN_INFO "TOS : %x à l'adresse %x", newip->tos,&(newip->tos));
printk(KERN_INFO "ID : %x à l'adresse %x", newip->id,(&newip->id));
printk(KERN_INFO "Frag Off : %x à l'adresse %x", newip->frag_off,&(newip->frag_off));
printk(KERN_INFO "TTL : %x à l'adresse %x", newip->ttl,&(newip->ttl));
printk(KERN_INFO "Protocol : %x à l'adresse %x", newip->protocol,&(newip->protocol));
printk(KERN_INFO "Source @ : %x à l'adresse %x", newip->saddr,&(newip->saddr));
printk(KERN_INFO "Destination @ : %x à l'adresse %x", newip->daddr,&(newip->daddr));
printk(KERN_INFO "Check IP : %x à l'adresse %x", newip->check,&(newip->check));
i=1;
for(data_for = skb->data; data_for<skb->tail; data_for++ ){
printk(KERN_INFO "\tPayload %d\t: %.2x à l'adresse : %x", i, *data_for, (unsigned int) data_for);
i++;
}
/************************************ ETHERNET *****************************************/
printk(KERN_INFO " -------------------------------------- ETHERNET -------------------------------------- \n");
skb->mac_header = (void *)skb_push(skb, sizeof(struct ethhdr));
memset(skb->data,0,sizeof(struct ethhdr));
neweth = skb->mac_header;
neweth->h_dest[0] = 0x04;neweth->h_dest[1] = 0x7d;neweth->h_dest[2] = 0x7b;neweth->h_dest[3] = 0x4d;neweth->h_dest[4] = 0xa8;neweth->h_dest[5] = 0x75;
neweth->h_source[0] = 0x04;neweth->h_source[1] = 0x7d;neweth->h_source[2] = 0x7b;neweth->h_source[3] = 0x4d;neweth->h_source[4] = 0xa8;neweth->h_source[5] = 0x75;
neweth->h_proto = ETH_P_IP;
aff_skb_info(skb);
printk(KERN_INFO "\n");
printk(KERN_INFO "MAC Destination : %x", neweth->h_dest);
printk(KERN_INFO "MAC Source : %x", neweth->h_source);
printk(KERN_INFO "Protocole : %x à l'adresse %x", neweth->h_proto);
i=1;
for(data_for = skb->data; data_for<skb->tail; data_for++ ){
printk(KERN_INFO "\tPayload %d\t: %.2x à l'adresse : %x", i, *data_for, (unsigned int) data_for);
i++;
}
dev_queue_xmit(skb);
kfree_skb(skb);
printk(KERN_INFO "\nCreate7 : LKM loaded.\n");
return 0;
}
static void temp_exit(void)
{
printk(KERN_INFO "Create7 : Unregistering LKM...\n");
printk(KERN_INFO "Create7: Unloaded LKM\n");
}
module_init(temp_init);
module_exit(temp_exit); |
Partager