uIP (FreeTCP/IP) Broadcast Support broken

sjackerman wrote on Thursday, March 06, 2014:

Trying to use uIP for the Art-Net protocol. In development I found that the UDP support for receiving broadcast datagrams is broken - someone else has found the same uIP issue:

https://list.sics.se/sympa/arc/uip-users/2009-09/msg00022.html

Here’s a fix:

/---------------------------------------------------------------------------/
// see: https://list.sics.se/sympa/arc/uip-users/2009-09/msg00022.html
static bool uip_ipaddr_test_addr(uip_ipaddr_t *addr)
{
//addr1 is the external address, addr2 is the address to match
uip_ipaddr_t masked, masked1;

//are the addresses equal?
if (uip_ipaddr_cmp(addr, &uip_hostaddr)) return true;

//broadcast to 255.255.255.255 ?
if (uip_ipaddr_cmp(addr, &uip_broadcast_addr)) return true;

//broadcast to 0.0.0.0 ?
if (uip_ipaddr_cmp(addr, &uip_all_zeroes_addr)) return true;

//Test if the address is a broadcast to a subnet
masked.u16[0]=addr->u16[0] & uip_netmask.u16[0];
masked.u16[1]=addr->u16[1] & uip_netmask.u16[1];

masked1.u16[0]=uip_hostaddr.u16[0] & uip_netmask.u16[0];
masked1.u16[1]=uip_hostaddr.u16[1] & uip_netmask.u16[1];

//Check wether the masked IP addresses are correct (same subnet)
if (!uip_ipaddr_cmp(&masked, &masked1)) return false;      

//same subnet, now test whether the remaining bits are all set to 1
masked.u16[0]=addr->u16[0] | uip_netmask.u16[0];
masked.u16[1]=addr->u16[1] | uip_netmask.u16[1];

//If masked is all_ones, then a message was sent to subnet.allones 
//like 192.168.1.255
if (uip_ipaddr_cmp(&masked, &uip_broadcast_addr)) return true;

return false;

}

and this new function must be called from two places in uip.c - when checking for a broadcast address match:

	/* If IP broadcast support is configured, we check for a broadcast
	UDP packet, which may be destined to us. */
	#if UIP_BROADCAST
		//DEBUG_PRINTF( "UDP IP checksum 0x%04x\n", uip_ipchksum() );
		if( 	BUF->proto == UIP_PROTO_UDP 
			// &&	uip_ipaddr_cmp(&BUF->destipaddr, &uip_broadcast_addr) /*&& uip_ipchksum() == 0xffff*/ )
			&&	uip_ipaddr_test_addr(&BUF->destipaddr) /*&& uip_ipchksum() == 0xffff*/ )
		{
			goto udp_input;
		}
	#endif /* UIP_BROADCAST */

and also when demultiplexing the datagram to the connections:

	/* Demultiplex this UDP packet between the UDP "connections". */
	for( uip_udp_conn = &uip_udp_conns[ 0 ]; uip_udp_conn < &uip_udp_conns[UIP_UDP_CONNS]; ++uip_udp_conn )
	{
		/* If the local UDP port is non-zero, the connection is considered
	   to be used. If so, the local port number is checked against the
	   destination port number in the received packet. If the two port
	   numbers match, the remote port number is checked if the
	   connection is bound to a remote port. Finally, if the
	   connection is bound to a remote IP address, the source IP
	   address of the packet is checked. */
		if
		(
			uip_udp_conn->lport != 0 &&
			UDPBUF->destport == uip_udp_conn->lport &&
			(uip_udp_conn->rport == 0 || UDPBUF->srcport == uip_udp_conn->rport) &&
			(
				uip_ipaddr_cmp(&uip_udp_conn->ripaddr, &uip_all_zeroes_addr) ||
				uip_ipaddr_cmp(&uip_udp_conn->ripaddr, &uip_broadcast_addr) ||
				// uip_ipaddr_cmp(&BUF->srcipaddr, &uip_udp_conn->ripaddr)
				uip_ipaddr_test_addr(&BUF->destipaddr)
			)
		)
		{
			goto udp_found;
		}
	}

The RX62N Ethernet driver sends each packet twice to circumvent the TCP/IP delayed ACK problem. This results in the UDP datagrams being sent twice as well.

I need to fix the Ethernet driver to only send the packet once, and then send TCP packets twice and UDP packets once.

Steven J. Ackerman
Consultant
ACS, Sarasota, FL
http://www.acscontrol.com

rtel wrote on Thursday, March 06, 2014:

Thanks for taking the time to point this out. We will look into it and update as appropriate.

Regards.