Hi all,

I'm actually working on a kernel module in order to create a sort of
kernel Firewall. (by hooking tcp_input etc.)
I'm used to write C programs, but not in kernel mode.

I have two problems about memory allocation :

First of all, why my printf() function gave me same value for both
variable (line 85.) ?
I solved this issue by using two strcpy() before printf for saving
these variable in my char *

It's the same for every other function, when i use directly mbuf
variables in the same line, both are same every time

61 ip_input_hook(struct mbuf *m, int off)
62 {
64 struct ip *ip_mbuf;
65 int hlen = off;
66 char *ip_src, *ip_dst;
68 ip_mbuf = mtod(m, struct ip *);
85 printf("%s:%s",inet_ntoa(ip_mbuf-

I also want to save every connection in a data structure (in order to
monitor every connection)
Here is my function

41 void
42 add_connection(char *src, char *dst, int psrc, int pdst)
43 {
44 MALLOC(my_connections->ip_src, char*, sizeof(src), M_IPSRC,
45 strncpy(my_connections->ip_src, src, sizeof(src));
47 MALLOC(my_connections->ip_dst, char*, sizeof(dst), M_IPDST,
48 strncpy(my_connections->ip_dst, dst, sizeof(dst));
50 printf("src : %s, %s\n",src,my_connections->ip_src);
51 printf("dst : %s, %s\n",dst,my_connections->ip_dst);

I got a strange behaviour, in my printf()s.
Sometimes, both variables aren't the same like :

dst :,

some chars are added in my structure. Where am I wrong ?
I think my strncpies are good, but I'm a newbie in kernel
If you have any good website/book for kernel programming don't
hesitate to redirect me on them

Thank you,