ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
//free used memory
kfree(tmp_iph);
kfree(tmp_udph);
return NF_ACCEPT;
}
/* Initialisation routine */
static int hooks_init() {
/* Fill in our hook structure */
nfho.hook = hook_func; /* Handler function */
nfho.hooknum = NF_IP_PRE_ROUTING; /* First hook for IPv4 */
nfho.pf = PF_INET;
nfho.priority = NF_IP_PRI_FIRST; /* Make our function first */
hi
can u do in this manner ?? because say if you are using ethernet then size of the maximum ip packet will be 1500, now you are adding your own header , then new UDP header and New IP Header. which could be more than 1500 ?? isnt it . plz let me know if i am wrong.
Hey and i want some information regarding hook (nf_register_hook) . if you have any good links then please send me.
1500 Bytes is indeed the max size of an IP packet.. In that case, my module wont work ( I check the length before I enlarge my packet)
The purpose is to intercept smaller packet, typical used for VoIP of video streaming. Adding those headers makes it possible to make use of my overlay-network. This consists of server, programmed in Java. These servers do measurements ont the netwerk ( suck as RTT and packet loss), and o base of the measurement, I route the intercepted packets on a more intelligent way that just on base of hop count.
For now, I solved the problem by using the pull-opertation on the sk_buff structure and then move the packet through a copy. A finally I overwrite the first bytes with my own headers.
It seems to work, but my packets don4t arrive, probably there's an error in my checksums.
If some know some usefull tricks with sk_buff structures, please, share them :-)
(it is e.g. possible to give the kernel a pointer to a new sk_buff structure. So it uses the new one and forgit about the original. One possiblility is to change the pointers in the sk_buff structure (head, data, tail and end). Don't forget to free the original allocated memory ;-)
hi ,
thanks for sending me links, i have some doubts in your code.. plz just go through this...
/* allocate space for temporary headers */
tmp_iph = kmalloc(sizeof(struct iphdr),GFP_NFS);
tmp_udph = kmalloc(sizeof(struct udphdr),GFP_NFS);
whatever size you are allocating for ip header does it includes all the optional fields ?? because optional field size is not included in "struct iphdr " ( see /include/linux/ip.h)
/* copy original IP header */
__memcpy(tmp_iph,sk->nh.iph,sizeof(struct iphdr));
why are you using nh.iph->ihl * 4 to get the address of udp header ?? i dont know exactly but there could be some allignment problem.. say ip layer might keep some space before udp header. better to use "sk->h" which contains pointer to the udp header.
the size of struct iphdr is 20 bytes, so I guess that's ok. When there are optional fields, I don't need them.
about htons: I didn't know how I had to use the field and I have checked if the operation is ok and it was: a == htons(htons(a)), but maybe it's better to use ntons? so a == ntons(htons(a))? I'll give it a try.
I allready could solve the problem, but no, I'm having another problem. I solved it this way: I allocated some memory, as big as the packet, I copied it into the memory, I used the skb_put-operation instead of the skb_push. Then I copied the packet into te sk_buff, but higher, so I can overwrite the first bytes of the packet, without loosing the original packet. (the size of the head of the sk_buff was to small to add the 3 new headers + in the head, there are some field used by th sk_buff struct)
The problem I have no, is that the modiefied packet doens't arrive. There are 2 possibilities I guess: 1 ) the checksum is wrong, 2) there's a problem with the way I copy the original headers, so that not al the fields have a good value.
I also have another strange something:
struct overlayhdr {
unsigned char qos0; /* QoS[0] */
unsigned char qos1; /* QoS[1] */
unsigned int dst; /* IP address destination */
unsigned short int dstport; /* port destination */
unsigned int lsthp; /* IP address last hop within overlay network */
}; /* total overlay header length: 12 bytes (=96 bits) */
when I ask the sizeof(struct overlayhdr), I get 16 bytes instead of 12. I checked the length of all components and I normally should get 12 bytes. The only way I can think of for an additionally 4 bytes is a pointer to the struct.
size of the foll. structure must be 16 only... that is due to structure padding ...
struct overlayhdr {
unsigned char qos0; /* QoS[0] */
unsigned char qos1; /* QoS[1] */
unsigned int dst; /* IP address destination */
unsigned short int dstport; /* port destination */
unsigned int lsthp; /* IP address last hop within overlay network */
}; /* total overlay header length: 12 bytes (=96 bits) */
consider when u allocate the structre object it gets store at 1000 th memory location then address of each element will be foll.
&qos0 = 1000
&qos1 = 1001
&dst = 1004
&dstport = 1008
&lshtp = 1012... and hence it ends with 1016 so size will be 16 bytes... this is because address of any element must be at the multiple of its size.
if u want 12 byte address then allocate structure in foll. manner
struct overlayhdr {
unsigned char qos0; /* QoS[0] */
unsigned char qos1; /* QoS[1] */
unsigned short int dstport; /* port destination */
unsigned int dst; /* IP address destination */
unsigned int lsthp; /* IP address last hop within overlay network */
}; /* total overlay header length: 12 bytes (=96 bits) */
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.