writing divert sockets

yavuz sakncli at gmail.com
Mon Jan 25 12:45:41 UTC 2010

Hi all,

I have a problem while writing divert sockets. I found a simple application
and modified it to compile in freebsd.
Simple divert socket application only prints incomming packet and reinject
packet to IP stack.
My simple application and test codes are available at (

I think I have a problem in reinject part, i.e writing packet back to ip

I have a server application listening on port 2000 and a client sends packet
to port 2000. And I have a ipfw rule as:

> ipfw add 1000 divert 2000 tcp from any to any 2000

In divert socket application a divert socket is created and bind to port
2000.  Socket receives packet with no problem:

> n=recvfrom( fd, packet, BUFSIZE, 0, (struct sockaddr_in *) sin, &sinlen);

After receiving packet what I want to do is simply reinjecting packet.

>n=sendto(fd, packet, n ,0, (struct sockaddr *) &sin, sinlen);

Reinjecting packet is not working in my case. I miss some point:) please
help me.

In addition to the problem, I couldn't understand a point in man page of

"The port part of the socket address passed to the sendto(2) contains a tag
that should be meaningful to the diversion module.  In the case of
     ipfw(8) the tag is interpreted as the rule number after which rule
processing should restart."

If I change the port value of sending address to ipfw rule number, how can
server receive the packet? (Assume the rule number is 1000 and server
listens port 2000. )

Thanks in advance

More information about the freebsd-questions mailing list