[En-Nut-Discussion] TCP/IP stack doubts
amit khandelwal
khandelwal.amit at gmail.com
Mon Feb 28 21:56:31 CET 2005
Hello,
I have a need to modify the application data at IP layer. I am adding
4 bytes of data to application buffer at the IP layer and then sending
it. I have written a linux kernel module which hooks at IP_PRE_ROUTING
and strips the 4 byte of data. I see that 2 packets are sent when I
add the application data at the IP layer.
1. TCP checksum uses the application data and size. I am not doing
anything for the tcp checksum. I don't think I need to do anything coz
I am just modifying the application data buffer and size at the IP
layer. Please see the code below
ipout.c <before the comment Set Remaining IP header items>
/**************************************************************************/
if (nb->nb_ap.sz) {
size = 0;
size += nb->nb_ap.sz + 4;
if (NutNetBufAllocData(&nbd, size)) {
NutNetBufFree(nb);
return 0;
}
memset(nbd.vp, 0, size);
memcpy(nbd.vp, nb->nb_ap.vp, size-4);
*(char *)(nbd.vp+(size-4)) = 67;
*(char *)(nbd.vp+(size-3)) = 67;
*(char *)(nbd.vp+(size-2)) = 67;
*(char *)(nbd.vp+(size-1)) = 67;
nb = NutNetBufAlloc(nb, NBAF_APPLICATION, size);
memcpy(nb->nb_ap.vp, nbd.vp, size);
printf("Updating the data %u\n", nb->nb_ap.sz);
printf("Updating the data %u\n", size);
nb->nb_ap.sz = size;
NutNetBufFreeData(&nbd);
}
ip->ip_len = htons(nb->nb_nw.sz + nb->nb_tp.sz + nb->nb_ap.sz);
/***********************************************************************/
In tcpout file there is a code for -
<snip>
/*
* IP output might fail because of routing, ARP or network device
* problems or because the system ran out of memory.
*/
if (NutIpOutput(IPPROTO_TCP, sock->so_remote_addr, nb))
return -1;
/*
* Append the segment to our transmission queue.
*/
if (size || ((th->th_flags & (TH_FIN | TH_SYN)))) {
NETBUF *nbp;
nb->nb_next = 0;
if ((nbp = sock->so_tx_nbq) == 0)
sock->so_tx_nbq = nb;
else {
while (nbp->nb_next)
nbp = nbp->nb_next;
nbp->nb_next = nb;
}
} else
NutNetBufFree(nb);
</snip>
Even if NutIpOutput is successful we will still add the nb to the
socket transmission queue. Why do we need this behavior? Is it for
handling retransmission? I see that the NutTcpSend is used to send the
packet from the socket stream (sostream.c). Now when I comment the
code to add the packet to the socket stream I get one packet as
expected.
1. Do I need to modify anything from the tcp-header?
2. I am not able to understand why this is happening?
I am able to recieve the correct data in my linux kernel module.
Can somebody give me some pointers??
Thanks!
Amit Khandelwal
PS.
Output from the linux module after commenting the code for adding nb
to the socket stream in the tcpout.c file.
------------------------------------------------------
Socket Buffer Length 49
Total length in IP Header 49
Length 9
Data is HelloCCCC
############################################
Socket Buffer Length 45
Total length in IP Header 45
Length 5
Data is Hello
------------------------------------------------------
Output from the linux module after commenting the code for adding nb
to the socket stream in the tcpout.c file.
------------------------------------------------------
Socket Buffer Length 49
Total length in IP Header 49
Length 9
Data is HelloCCCC
############################################
Socket Buffer Length 45
Total length in IP Header 45
Length 5
Data is Hello
------------------------------------------------------
------------------------------------------------------
Socket Buffer Length 53
Total length in IP Header 53
Length 13
Data is HelloCCCCCCCC
############################################
Socket Buffer Length 49
Total length in IP Header 49
Length 9
Data is HelloCCCC
------------------------------------------------------
More information about the En-Nut-Discussion
mailing list