[U-Boot] [PATCH 00/28] Add link-local addressing support

Link-local support allows the bootloader to get a usable network address regardless of network environment. Given that bootloaders may not always have access to a DHCP server, this functionality makes accessing it in those environments easier. It is also referred to as zero-configuration IP.
The following script allows the network to be configured for DHCP, link-local, and static IP addresses configurably by the environment using "dhcpenabled" and "linklocalenabled".
#define CONFIG_EXTRA_ENV_SETTINGS \ "ipconfigcmd=if test \"$dhcpenabled\" -ne 0;" \ "then " \ "dhcpfail=0;dhcp || dhcpfail=1;" \ "else " \ "dhcpfail=-1;" \ "fi;" \ "if test \"$linklocalenabled\" -ne 0 -a " \ "\"$dhcpfail\" -ne 0;" \ "then " \ "linklocal;" \ "llfail=0;" \ "else " \ "llfail=-1;" \ "fi;" \ "if test \"$llfail\" -ne 0 -a " \ "\"$dhcpfail\" -ne 0; " \ "then " \ "setenv ipaddr $sipaddr; " \ "setenv netmask $snetmask; " \ "setenv gatewayip $sgatewayip; " \ "fi;\0" \
Some refactoring of net/ was done to make adding link-local support cleaner as well as generally improving the readability and organization of the code.
Tested on da850_am18xxevm Tested on MPC8313ERDB_33
check-patch No warnings or errors (depends on ignoring CONSIDER_KSTRTO)
MAKEALL -a powerpc No additional warnings or errors beyond master
MAKEALL -a arm No additional warnings or errors beyond master
git-test-sequence Cleanly bisectable Code-size affects on MPC8313ERDB_33 shown below
cat sizeofMCP8313
make BUILD_DIR=/tmp/u-boot-build/ ARCH=powerpc CROSS_COMPILE=powerpc-linux-gnu- USE_PRIVATE_LIBGG=yes distclean > /dev/null && \ make BUILD_DIR=/tmp/u-boot-build/ ARCH=powerpc CROSS_COMPILE=powerpc-linux-gnu- MPC8313ERDB_33_config > /dev/null && \ make BUILD_DIR=/tmp/u-boot-build/ ARCH=powerpc CROSS_COMPILE=powerpc-linux-gnu- USE_PRIVATE_LIBGG=yes > /dev/null && \ powerpc-linux-gnu-size /tmp/u-boot-build/u-boot
git-test-sequence origin/master.. './sizeofMCP8313'
Testing ./sizeofMCP8313 HEAD is now at 4dbe8d8 net: Remove volatile from all of net except the eth driver interface text data bss dec hex filename 264917 24880 42104 331901 5107d /tmp/u-boot-build/ppc/u-boot Passed: 90ea672853753b5a4244db60d095e5f99cdcfe7d. Testing ./sizeofMCP8313 HEAD is now at 92893c4 net: cosmetic: checkpatch compliance text data bss dec hex filename 264917 24880 42104 331901 5107d /tmp/u-boot-build/ppc/u-boot Passed: 12b0b6e953406a1ed98d1c4dd9701078d1dfe00d. Testing ./sizeofMCP8313 HEAD is now at d6c19a3 net: Move mv64x6x_eth_initialize() to board_eth_init() text data bss dec hex filename 264917 24880 42104 331901 5107d /tmp/u-boot-build/ppc/u-boot Passed: cf9f493ab5d08a282ec9905797fa58d82b15714d. Testing ./sizeofMCP8313 HEAD is now at 52403e8 net: Make the MAC-seeded random number generator available to /net text data bss dec hex filename 264917 24880 42104 331901 5107d /tmp/u-boot-build/ppc/u-boot Passed: a92c9c9e8f68337a78ee2d7c6775299d7c057b52. Testing ./sizeofMCP8313 HEAD is now at ff86c81 net: Move CDP out of net.c text data bss dec hex filename 264917 24880 42104 331901 5107d /tmp/u-boot-build/ppc/u-boot Passed: 9bf01065cb04c34e43cc5d56162aa2dac9d61f3e. Testing ./sizeofMCP8313 HEAD is now at da1998d net: Move ARP out of net.c text data bss dec hex filename 265045 24928 42104 332077 5112d /tmp/u-boot-build/ppc/u-boot Passed: 621d67b8c6126ef6510d0a2b2c06ce410cd60327. Testing ./sizeofMCP8313 HEAD is now at 408b8ab net: Move PING out of net.c text data bss dec hex filename 265237 24976 42096 332309 51215 /tmp/u-boot-build/ppc/u-boot Passed: e8aaa7382de39137c91809ba2e121f87338627d5. Testing ./sizeofMCP8313 HEAD is now at 1b3ed14 net: Move RARP receive logic out of net.c text data bss dec hex filename 265237 24976 42096 332309 51215 /tmp/u-boot-build/ppc/u-boot Passed: 7a8216874def295337178da67078758cbf909681. Testing ./sizeofMCP8313 HEAD is now at 50eb857 net: cosmetic: Un-typedef variables in net text data bss dec hex filename 265237 24976 42096 332309 51215 /tmp/u-boot-build/ppc/u-boot Passed: b679739af8ffc6f9a31264c175077f4ac96c236d. Testing ./sizeofMCP8313 HEAD is now at 22259f9 net: cosmetic: Improve variable names and code readability text data bss dec hex filename 265237 24976 42096 332309 51215 /tmp/u-boot-build/ppc/u-boot Passed: 834a569897337636fe861797625fbdbbe1373f75. Testing ./sizeofMCP8313 HEAD is now at 5340a18 net: Refactor IP, UPD, and ICMP header writing functions text data bss dec hex filename 265237 24972 42096 332305 51211 /tmp/u-boot-build/ppc/u-boot Passed: 20b84dcfa7d355010cacc3493e1e86521b00879f. Testing ./sizeofMCP8313 HEAD is now at 275e230 net: Refactor packet length computations text data bss dec hex filename 265221 24972 42096 332289 51201 /tmp/u-boot-build/ppc/u-boot Passed: d078d84d959dbb40a67baa16e8b0d210e6472c79. Testing ./sizeofMCP8313 HEAD is now at c1214f2 net: Refactor bootp packet length computations text data bss dec hex filename 265221 24972 42096 332289 51201 /tmp/u-boot-build/ppc/u-boot Passed: 3ff5dfd46c8f70ec2304a150a606871b7504cf42. Testing ./sizeofMCP8313 HEAD is now at 9cff376 net: Refactor ping recieve handler text data bss dec hex filename 265093 24968 42096 332157 5117d /tmp/u-boot-build/ppc/u-boot Passed: e531ec8d3986f45d30862eacce73b0db55e2fb13. Testing ./sizeofMCP8313 HEAD is now at e47e5ac net: Refactor to call NetSendPacket() instead of calling eth_send() text data bss dec hex filename 265093 24968 42096 332157 5117d /tmp/u-boot-build/ppc/u-boot Passed: 4a78d3ab2514c8d00bc18978f7a489a800a3a77b. Testing ./sizeofMCP8313 HEAD is now at 9bcb672 net: Refactor to protect access to the NetState variable text data bss dec hex filename 265109 24956 42096 332161 51181 /tmp/u-boot-build/ppc/u-boot Passed: 8ecfa86d86fe2624cb509c41fbaec8dcf13a9945. Testing ./sizeofMCP8313 HEAD is now at a2c79e3 net: Refactor to separate the UDP handler from the ARP handler text data bss dec hex filename 265317 24956 42112 332385 51261 /tmp/u-boot-build/ppc/u-boot Passed: f66f88edda9ec16b3de20a10faa1c6bb35f316c2. Testing ./sizeofMCP8313 HEAD is now at 502deaa net: Add NetUpdateEther() to handle ARP or Ping replies (VLAN or SNAP) text data bss dec hex filename 265429 24952 42112 332493 512cd /tmp/u-boot-build/ppc/u-boot Passed: 3daff36dc0b5b65b8df7a3a7011f1e5f89eb5a9d. Testing ./sizeofMCP8313 HEAD is now at 315bd4d net: Don't write the "serverip" env var if told not to in the config text data bss dec hex filename 265429 24952 42112 332493 512cd /tmp/u-boot-build/ppc/u-boot Passed: b0e7a73485c771eed710d6b9afd1e80e0ccc2cfa. Testing ./sizeofMCP8313 HEAD is now at 82b1968 net: Fix compile warning if CONFIG_BOOTP_SERVERIP is not defined text data bss dec hex filename 265429 24952 42112 332493 512cd /tmp/u-boot-build/ppc/u-boot Passed: 121eb763232d5cf6a4e66f6c100713537f6036cf. Testing ./sizeofMCP8313 HEAD is now at cebdefe net: Remove unneeded static allocation for MAC address in PingSend() text data bss dec hex filename 265397 24944 42104 332445 5129d /tmp/u-boot-build/ppc/u-boot Passed: 1366a9b1529e9752dd4a47d7c82545b13e11b461. Testing ./sizeofMCP8313 HEAD is now at 8f856ad net: Fix net buffer initialization text data bss dec hex filename 265605 24956 42104 332665 51379 /tmp/u-boot-build/ppc/u-boot Passed: 16cbef4189d5be4837c3de9dc2ac0bcf8d89fa75. Testing ./sizeofMCP8313 HEAD is now at 002991b net: Refactor NetSendUDPPacket to share more code text data bss dec hex filename 265557 24956 42104 332617 51349 /tmp/u-boot-build/ppc/u-boot Passed: 7f001d4025dc564fe2a96a1e32214e194b891bd1. Testing ./sizeofMCP8313 HEAD is now at 314902e net: Don't copy every packet that waits for an ARP text data bss dec hex filename 265573 24956 42104 332633 51359 /tmp/u-boot-build/ppc/u-boot Passed: 3be3cdd2224c88c88442e48c0f4f836d7be1d8ae. Testing ./sizeofMCP8313 HEAD is now at 2a43805 net: Add option CONFIG_BOOTP_CAN_FAIL text data bss dec hex filename 265573 24956 42104 332633 51359 /tmp/u-boot-build/ppc/u-boot Passed: 4d578ba84228e51a56249035207085f0845d3be8. Testing ./sizeofMCP8313 HEAD is now at defb33c net: Add link-local addressing support text data bss dec hex filename 265577 24956 42104 332637 5135d /tmp/u-boot-build/ppc/u-boot Passed: cb6b29e82f292b5b27aec6f51f2f9d50e1f8b0f8. Testing ./sizeofMCP8313 HEAD is now at 615c707 net: Work-around for brain-damaged Cisco routers with arp-proxy text data bss dec hex filename 265657 24956 42104 332717 513ad /tmp/u-boot-build/ppc/u-boot Passed: 63a3ddaf7e029bb7ec71b1edb42473ae6a208dec. Testing ./sizeofMCP8313 HEAD is now at 7d5229d net: Allow filtering on debug traces in the net subsystem text data bss dec hex filename 265594 24948 42104 332646 51366 /tmp/u-boot-build/ppc/u-boot Passed: 1cf14c686c5b60bfa48d8bfd6eb40e20ac0c378b. All's well.
Joe Hershberger (28): net: Remove volatile from all of net except the eth driver interface net: cosmetic: checkpatch compliance net: Move mv64x6x_eth_initialize() to board_eth_init() net: Make the MAC-seeded random number generator available to /net net: Move CDP out of net.c net: Move ARP out of net.c net: Move PING out of net.c net: Move RARP receive logic out of net.c net: cosmetic: Un-typedef variables in net net: cosmetic: Improve variable names and code readability net: Refactor IP, UPD, and ICMP header writing functions net: Refactor packet length computations net: Refactor bootp packet length computations net: Refactor ping recieve handler net: Refactor to call NetSendPacket() instead of calling eth_send() net: Refactor to protect access to the NetState variable net: Refactor to separate the UDP handler from the ARP handler net: Add NetUpdateEther() to handle ARP or Ping replies (VLAN or SNAP) net: Don't write the "serverip" env var if told not to in the config net: Fix compile warning if CONFIG_BOOTP_SERVERIP is not defined net: Remove unneeded static allocation for MAC address in PingSend() net: Fix net buffer initialization net: Refactor NetSendUDPPacket to share more code net: Don't copy every packet that waits for an ARP net: Add option CONFIG_BOOTP_CAN_FAIL net: Add link-local addressing support net: Work-around for brain-damaged Cisco routers with arp-proxy net: Allow filtering on debug traces in the net subsystem
README | 22 +- arch/powerpc/cpu/mpc8260/ether_fcc.c | 10 +- arch/powerpc/cpu/mpc8xx/fec.c | 2 +- board/Marvell/db64360/db64360.c | 12 +- board/Marvell/db64360/mv_eth.c | 78 ++- board/Marvell/db64360/mv_eth.h | 63 +-- board/Marvell/db64460/db64460.c | 12 +- board/Marvell/db64460/mv_eth.c | 78 ++- board/Marvell/db64460/mv_eth.h | 63 +-- board/esd/cpci750/cpci750.c | 6 + board/esd/cpci750/mv_eth.c | 78 ++- board/esd/cpci750/mv_eth.h | 63 +-- board/prodrive/p3mx/mv_eth.c | 78 ++- board/prodrive/p3mx/mv_eth.h | 63 +-- board/prodrive/p3mx/p3mx.c | 6 + common/cmd_net.c | 34 +- common/main.c | 1 - drivers/net/netconsole.c | 130 ++-- include/common.h | 4 + include/configs/am3517_crane.h | 2 +- include/configs/am3517_evm.h | 2 +- include/configs/origen.h | 2 +- include/net.h | 206 ++++--- net/Makefile | 8 +- net/arp.c | 242 +++++++ net/arp.h | 30 + net/bootp.c | 435 ++++++------- net/bootp.h | 60 +- net/cdp.c | 372 +++++++++++ net/cdp.h | 18 + net/dns.c | 12 +- net/eth.c | 75 +-- net/link_local.c | 340 ++++++++++ net/link_local.h | 21 + net/net.c | 1152 +++++++++------------------------- net/net_rand.c | 68 ++ net/net_rand.h | 18 + net/nfs.c | 306 +++++----- net/nfs.h | 2 +- net/ping.c | 119 ++++ net/ping.h | 20 + net/rarp.c | 77 ++- net/rarp.h | 5 +- net/sntp.c | 37 +- net/sntp.h | 2 +- net/tftp.c | 50 +- net/tftp.h | 2 +- 47 files changed, 2625 insertions(+), 1861 deletions(-) create mode 100644 net/arp.c create mode 100644 net/arp.h create mode 100644 net/cdp.c create mode 100644 net/cdp.h create mode 100644 net/link_local.c create mode 100644 net/link_local.h create mode 100644 net/net_rand.c create mode 100644 net/net_rand.h create mode 100644 net/ping.c create mode 100644 net/ping.h

The mv_eth driver should not redefine the net function definition
Signed-off-by: Joe Hershberger joe.hershberger@ni.com Cc: Joe Hershberger joe.hershberger@gmail.com Cc: Wolfgang Denk wd@denx.de --- board/Marvell/db64360/mv_eth.c | 2 -- board/Marvell/db64460/mv_eth.c | 2 -- board/esd/cpci750/mv_eth.c | 2 -- board/prodrive/p3mx/mv_eth.c | 2 -- include/net.h | 16 ++++++++-------- net/bootp.c | 4 ++-- net/net.c | 40 ++++++++++++++++++++-------------------- net/rarp.c | 2 +- net/tftp.c | 6 +++--- 9 files changed, 34 insertions(+), 42 deletions(-)
diff --git a/board/Marvell/db64360/mv_eth.c b/board/Marvell/db64360/mv_eth.c index 6340585..550610e 100644 --- a/board/Marvell/db64360/mv_eth.c +++ b/board/Marvell/db64360/mv_eth.c @@ -95,8 +95,6 @@ int mv64360_eth_xmit (struct eth_device *, volatile void *packet, int length); #ifndef UPDATE_STATS_BY_SOFTWARE static void mv64360_eth_print_stat (struct eth_device *dev); #endif -/* Processes a received packet */ -extern void NetReceive (volatile uchar *, int);
extern unsigned int INTERNAL_REG_BASE_ADDR;
diff --git a/board/Marvell/db64460/mv_eth.c b/board/Marvell/db64460/mv_eth.c index 4aefbaf..c447229 100644 --- a/board/Marvell/db64460/mv_eth.c +++ b/board/Marvell/db64460/mv_eth.c @@ -95,8 +95,6 @@ int mv64460_eth_xmit (struct eth_device *, volatile void *packet, int length); #ifndef UPDATE_STATS_BY_SOFTWARE static void mv64460_eth_print_stat (struct eth_device *dev); #endif -/* Processes a received packet */ -extern void NetReceive (volatile uchar *, int);
extern unsigned int INTERNAL_REG_BASE_ADDR;
diff --git a/board/esd/cpci750/mv_eth.c b/board/esd/cpci750/mv_eth.c index 001c1ad..db2324b 100644 --- a/board/esd/cpci750/mv_eth.c +++ b/board/esd/cpci750/mv_eth.c @@ -95,8 +95,6 @@ int mv64360_eth_xmit (struct eth_device *, volatile void *packet, int length); #ifndef UPDATE_STATS_BY_SOFTWARE static void mv64360_eth_print_stat (struct eth_device *dev); #endif -/* Processes a received packet */ -extern void NetReceive (volatile uchar *, int);
extern unsigned int INTERNAL_REG_BASE_ADDR;
diff --git a/board/prodrive/p3mx/mv_eth.c b/board/prodrive/p3mx/mv_eth.c index 15b3bfc..f26ded8 100644 --- a/board/prodrive/p3mx/mv_eth.c +++ b/board/prodrive/p3mx/mv_eth.c @@ -109,8 +109,6 @@ int phy_setup_aneg (char *devname, unsigned char addr); #ifndef UPDATE_STATS_BY_SOFTWARE static void mv64460_eth_print_stat (struct eth_device *dev); #endif -/* Processes a received packet */ -extern void NetReceive (volatile uchar *, int);
extern unsigned int INTERNAL_REG_BASE_ADDR;
diff --git a/include/net.h b/include/net.h index e4d42c2..ccdb01d 100644 --- a/include/net.h +++ b/include/net.h @@ -342,9 +342,9 @@ extern uchar NetOurEther[6]; /* Our ethernet address */ extern uchar NetServerEther[6]; /* Boot server enet address */ extern IPaddr_t NetOurIP; /* Our IP addr (0 = unknown) */ extern IPaddr_t NetServerIP; /* Server IP addr (0 = unknown) */ -extern volatile uchar * NetTxPacket; /* THE transmit packet */ -extern volatile uchar * NetRxPackets[PKTBUFSRX];/* Receive packets */ -extern volatile uchar * NetRxPacket; /* Current receive packet */ +extern uchar *NetTxPacket; /* THE transmit packet */ +extern uchar *NetRxPackets[PKTBUFSRX];/* Receive packets */ +extern uchar *NetRxPacket; /* Current receive packet */ extern int NetRxPacketLen; /* Current rx packet length */ extern unsigned NetIPID; /* IP ID (counting) */ extern uchar NetBcastAddr[6]; /* Ethernet boardcast address */ @@ -408,10 +408,10 @@ extern void NetStartAgain(void); extern int NetEthHdrSize(void);
/* Set ethernet header; returns the size of the header */ -extern int NetSetEther(volatile uchar *, uchar *, uint); +extern int NetSetEther(uchar *, uchar *, uint);
/* Set IP header */ -extern void NetSetIP(volatile uchar *, IPaddr_t, int, int, int); +extern void NetSetIP(uchar *, IPaddr_t, int, int, int);
/* Checksum */ extern int NetCksumOk(uchar *, int); /* Return true if cksum OK */ @@ -423,7 +423,7 @@ extern void net_set_icmp_handler(rxhand_icmp_f *f); /* Set ICMP RX handler */ extern void NetSetTimeout(ulong, thand_f *);/* Set timeout handler */
/* Transmit "NetTxPacket" */ -extern void NetSendPacket(volatile uchar *, int); +extern void NetSendPacket(uchar *, int);
/* Transmit UDP packet, performing ARP request if needed */ extern int NetSendUDPPacket(uchar *ether, IPaddr_t dest, int dport, int sport, int len); @@ -445,7 +445,7 @@ void net_auto_load(void); * footprint in our tests. */ /* return IP *in network byteorder* */ -static inline IPaddr_t NetReadIP(volatile void *from) +static inline IPaddr_t NetReadIP(void *from) { IPaddr_t ip; memcpy((void*)&ip, (void*)from, sizeof(ip)); @@ -467,7 +467,7 @@ static inline void NetWriteIP(void *to, IPaddr_t ip) }
/* copy IP */ -static inline void NetCopyIP(volatile void *to, void *from) +static inline void NetCopyIP(void *to, void *from) { memcpy((void*)to, from, sizeof(IPaddr_t)); } diff --git a/net/bootp.c b/net/bootp.c index 34124b8..6d40608 100644 --- a/net/bootp.c +++ b/net/bootp.c @@ -585,7 +585,7 @@ static int BootpExtended (u8 * e) void BootpRequest (void) { - volatile uchar *pkt, *iphdr; + uchar *pkt, *iphdr; Bootp_t *bp; int ext_len, pktlen, iplen;
@@ -837,7 +837,7 @@ static int DhcpMessageType(unsigned char *popt)
static void DhcpSendRequestPkt(Bootp_t *bp_offer) { - volatile uchar *pkt, *iphdr; + uchar *pkt, *iphdr; Bootp_t *bp; int pktlen, iplen, extlen; IPaddr_t OfferedIP; diff --git a/net/net.c b/net/net.c index 045405b..29e018c 100644 --- a/net/net.c +++ b/net/net.c @@ -152,7 +152,7 @@ IPaddr_t NetOurIP; /* Server IP addr (0 = unknown) */ IPaddr_t NetServerIP; /* Current receive packet */ -volatile uchar *NetRxPacket; +uchar *NetRxPacket; /* Current rx packet length */ int NetRxPacketLen; /* IP packet ID */ @@ -208,10 +208,10 @@ void NcStart(void); int nc_input_packet(uchar *pkt, unsigned dest, unsigned src, unsigned len); #endif
-volatile uchar PktBuf[(PKTBUFSRX+1) * PKTSIZE_ALIGN + PKTALIGN]; +uchar PktBuf[(PKTBUFSRX+1) * PKTSIZE_ALIGN + PKTALIGN];
/* Receive packet */ -volatile uchar *NetRxPackets[PKTBUFSRX]; +uchar *NetRxPackets[PKTBUFSRX];
/* Current RX packet handler */ static rxhand_f *packetHandler; @@ -225,7 +225,7 @@ static ulong timeStart; /* Current timeout value */ static ulong timeDelta; /* THE transmit packet */ -volatile uchar *NetTxPacket; +uchar *NetTxPacket;
static int net_check_prereq(enum proto_t protocol);
@@ -246,7 +246,7 @@ int NetArpWaitTry;
void ArpRequest(void) { - volatile uchar *pkt; + uchar *pkt; ARP_t *arp;
debug("ARP broadcast %d\n", NetArpWaitTry); @@ -704,7 +704,7 @@ NetSetTimeout(ulong iv, thand_f *f)
void -NetSendPacket(volatile uchar *pkt, int len) +NetSendPacket(uchar *pkt, int len) { (void) eth_send(pkt, len); } @@ -767,8 +767,8 @@ static ushort PingSeqNo; int PingSend(void) { static uchar mac[6]; - volatile IP_t *ip; - volatile ushort *s; + IP_t *ip; + ushort *s; uchar *pkt;
/* XXX always send arp request */ @@ -783,7 +783,7 @@ int PingSend(void) pkt = NetArpWaitTxPacket; pkt += NetSetEther(pkt, mac, PROT_IP);
- ip = (volatile IP_t *)pkt; + ip = (IP_t *)pkt;
/* * Construct an IP and ICMP header. @@ -935,9 +935,9 @@ static ushort CDP_compute_csum(const uchar *buff, ushort len)
int CDPSendTrigger(void) { - volatile uchar *pkt; - volatile ushort *s; - volatile ushort *cp; + uchar *pkt; + ushort *s; + ushort *cp; Ethernet_t *et; int len; ushort chksum; @@ -964,7 +964,7 @@ int CDPSendTrigger(void) /* CDP header */ *pkt++ = 0x02; /* CDP version 2 */ *pkt++ = 180; /* TTL */ - s = (volatile ushort *)pkt; + s = (ushort *)pkt; cp = s; /* checksum (0 for later calculation) */ *s++ = htons(0); @@ -1454,9 +1454,9 @@ NetReceive(volatile uchar *inpkt, int len)
debug("packet received\n");
- NetRxPacket = inpkt; + NetRxPacket = (uchar *)inpkt; NetRxPacketLen = len; - et = (Ethernet_t *)inpkt; + et = (Ethernet_t *)NetRxPacket;
/* too small packet? */ if (len < ETHER_HDR_SIZE) @@ -1491,11 +1491,11 @@ NetReceive(volatile uchar *inpkt, int len) */ x = ntohs(et->et_prot);
- ip = (IP_t *)(inpkt + E802_HDR_SIZE); + ip = (IP_t *)(NetRxPacket + E802_HDR_SIZE); len -= E802_HDR_SIZE;
} else if (x != PROT_VLAN) { /* normal packet */ - ip = (IP_t *)(inpkt + ETHER_HDR_SIZE); + ip = (IP_t *)(NetRxPacket + ETHER_HDR_SIZE); len -= ETHER_HDR_SIZE;
} else { /* VLAN packet */ @@ -1519,7 +1519,7 @@ NetReceive(volatile uchar *inpkt, int len) vlanid = cti & VLAN_IDMASK; x = ntohs(vet->vet_type);
- ip = (IP_t *)(inpkt + VLAN_ETHER_HDR_SIZE); + ip = (IP_t *)(NetRxPacket + VLAN_ETHER_HDR_SIZE); len -= VLAN_ETHER_HDR_SIZE; }
@@ -1917,7 +1917,7 @@ NetEthHdrSize(void) }
int -NetSetEther(volatile uchar *xet, uchar * addr, uint prot) +NetSetEther(uchar *xet, uchar * addr, uint prot) { Ethernet_t *et = (Ethernet_t *)xet; ushort myvlanid; @@ -1942,7 +1942,7 @@ NetSetEther(volatile uchar *xet, uchar * addr, uint prot) }
void -NetSetIP(volatile uchar *xip, IPaddr_t dest, int dport, int sport, int len) +NetSetIP(uchar *xip, IPaddr_t dest, int dport, int sport, int len) { IP_t *ip = (IP_t *)xip;
diff --git a/net/rarp.c b/net/rarp.c index 097f970..77d63e8 100644 --- a/net/rarp.c +++ b/net/rarp.c @@ -71,7 +71,7 @@ void RarpRequest (void) { int i; - volatile uchar *pkt; + uchar *pkt; ARP_t * rarp;
printf("RARP broadcast %d\n", ++RarpTry); diff --git a/net/tftp.c b/net/tftp.c index 7aa3e23..e62f229 100644 --- a/net/tftp.c +++ b/net/tftp.c @@ -310,9 +310,9 @@ static void TftpSend(void) { uchar *pkt; - volatile uchar *xp; - int len = 0; - volatile ushort *s; + uchar *xp; + int len = 0; + ushort *s;
#ifdef CONFIG_MCAST_TFTP /* Multicast TFTP.. non-MasterClients do not ACK data. */

Hi Joe,
On Thu, Jan 19, 2012 at 4:53 PM, Joe Hershberger joe.hershberger@ni.com wrote:
The mv_eth driver should not redefine the net function definition
Signed-off-by: Joe Hershberger joe.hershberger@ni.com Cc: Joe Hershberger joe.hershberger@gmail.com Cc: Wolfgang Denk wd@denx.de
board/Marvell/db64360/mv_eth.c | 2 -- board/Marvell/db64460/mv_eth.c | 2 -- board/esd/cpci750/mv_eth.c | 2 -- board/prodrive/p3mx/mv_eth.c | 2 -- include/net.h | 16 ++++++++-------- net/bootp.c | 4 ++-- net/net.c | 40 ++++++++++++++++++++-------------------- net/rarp.c | 2 +- net/tftp.c | 6 +++--- 9 files changed, 34 insertions(+), 42 deletions(-)
diff --git a/board/Marvell/db64360/mv_eth.c b/board/Marvell/db64360/mv_eth.c index 6340585..550610e 100644 --- a/board/Marvell/db64360/mv_eth.c +++ b/board/Marvell/db64360/mv_eth.c @@ -95,8 +95,6 @@ int mv64360_eth_xmit (struct eth_device *, volatile void *packet, int length); #ifndef UPDATE_STATS_BY_SOFTWARE static void mv64360_eth_print_stat (struct eth_device *dev); #endif -/* Processes a received packet */ -extern void NetReceive (volatile uchar *, int);
extern unsigned int INTERNAL_REG_BASE_ADDR;
diff --git a/board/Marvell/db64460/mv_eth.c b/board/Marvell/db64460/mv_eth.c index 4aefbaf..c447229 100644 --- a/board/Marvell/db64460/mv_eth.c +++ b/board/Marvell/db64460/mv_eth.c @@ -95,8 +95,6 @@ int mv64460_eth_xmit (struct eth_device *, volatile void *packet, int length); #ifndef UPDATE_STATS_BY_SOFTWARE static void mv64460_eth_print_stat (struct eth_device *dev); #endif -/* Processes a received packet */ -extern void NetReceive (volatile uchar *, int);
extern unsigned int INTERNAL_REG_BASE_ADDR;
diff --git a/board/esd/cpci750/mv_eth.c b/board/esd/cpci750/mv_eth.c index 001c1ad..db2324b 100644 --- a/board/esd/cpci750/mv_eth.c +++ b/board/esd/cpci750/mv_eth.c @@ -95,8 +95,6 @@ int mv64360_eth_xmit (struct eth_device *, volatile void *packet, int length); #ifndef UPDATE_STATS_BY_SOFTWARE static void mv64360_eth_print_stat (struct eth_device *dev); #endif -/* Processes a received packet */ -extern void NetReceive (volatile uchar *, int);
extern unsigned int INTERNAL_REG_BASE_ADDR;
diff --git a/board/prodrive/p3mx/mv_eth.c b/board/prodrive/p3mx/mv_eth.c index 15b3bfc..f26ded8 100644 --- a/board/prodrive/p3mx/mv_eth.c +++ b/board/prodrive/p3mx/mv_eth.c @@ -109,8 +109,6 @@ int phy_setup_aneg (char *devname, unsigned char addr); #ifndef UPDATE_STATS_BY_SOFTWARE static void mv64460_eth_print_stat (struct eth_device *dev); #endif -/* Processes a received packet */ -extern void NetReceive (volatile uchar *, int);
extern unsigned int INTERNAL_REG_BASE_ADDR;
diff --git a/include/net.h b/include/net.h index e4d42c2..ccdb01d 100644 --- a/include/net.h +++ b/include/net.h @@ -342,9 +342,9 @@ extern uchar NetOurEther[6]; /* Our ethernet address */ extern uchar NetServerEther[6]; /* Boot server enet address */ extern IPaddr_t NetOurIP; /* Our IP addr (0 = unknown) */ extern IPaddr_t NetServerIP; /* Server IP addr (0 = unknown) */ -extern volatile uchar * NetTxPacket; /* THE transmit packet */ -extern volatile uchar * NetRxPackets[PKTBUFSRX];/* Receive packets */ -extern volatile uchar * NetRxPacket; /* Current receive packet */ +extern uchar *NetTxPacket; /* THE transmit packet */ +extern uchar *NetRxPackets[PKTBUFSRX];/* Receive packets */ +extern uchar *NetRxPacket; /* Current receive packet */ extern int NetRxPacketLen; /* Current rx packet length */ extern unsigned NetIPID; /* IP ID (counting) */ extern uchar NetBcastAddr[6]; /* Ethernet boardcast address */ @@ -408,10 +408,10 @@ extern void NetStartAgain(void); extern int NetEthHdrSize(void);
/* Set ethernet header; returns the size of the header */ -extern int NetSetEther(volatile uchar *, uchar *, uint); +extern int NetSetEther(uchar *, uchar *, uint);
/* Set IP header */ -extern void NetSetIP(volatile uchar *, IPaddr_t, int, int, int); +extern void NetSetIP(uchar *, IPaddr_t, int, int, int);
/* Checksum */ extern int NetCksumOk(uchar *, int); /* Return true if cksum OK */ @@ -423,7 +423,7 @@ extern void net_set_icmp_handler(rxhand_icmp_f *f); /* Set ICMP RX handler */ extern void NetSetTimeout(ulong, thand_f *);/* Set timeout handler */
/* Transmit "NetTxPacket" */ -extern void NetSendPacket(volatile uchar *, int); +extern void NetSendPacket(uchar *, int);
/* Transmit UDP packet, performing ARP request if needed */ extern int NetSendUDPPacket(uchar *ether, IPaddr_t dest, int dport, int sport, int len); @@ -445,7 +445,7 @@ void net_auto_load(void); * footprint in our tests. */ /* return IP *in network byteorder* */ -static inline IPaddr_t NetReadIP(volatile void *from) +static inline IPaddr_t NetReadIP(void *from) { IPaddr_t ip; memcpy((void*)&ip, (void*)from, sizeof(ip)); @@ -467,7 +467,7 @@ static inline void NetWriteIP(void *to, IPaddr_t ip) }
/* copy IP */ -static inline void NetCopyIP(volatile void *to, void *from) +static inline void NetCopyIP(void *to, void *from) { memcpy((void*)to, from, sizeof(IPaddr_t)); } diff --git a/net/bootp.c b/net/bootp.c index 34124b8..6d40608 100644 --- a/net/bootp.c +++ b/net/bootp.c @@ -585,7 +585,7 @@ static int BootpExtended (u8 * e) void BootpRequest (void) {
- volatile uchar *pkt, *iphdr;
- uchar *pkt, *iphdr;
Bootp_t *bp; int ext_len, pktlen, iplen;
@@ -837,7 +837,7 @@ static int DhcpMessageType(unsigned char *popt)
static void DhcpSendRequestPkt(Bootp_t *bp_offer) {
- volatile uchar *pkt, *iphdr;
- uchar *pkt, *iphdr;
Bootp_t *bp; int pktlen, iplen, extlen; IPaddr_t OfferedIP; diff --git a/net/net.c b/net/net.c index 045405b..29e018c 100644 --- a/net/net.c +++ b/net/net.c @@ -152,7 +152,7 @@ IPaddr_t NetOurIP; /* Server IP addr (0 = unknown) */ IPaddr_t NetServerIP; /* Current receive packet */ -volatile uchar *NetRxPacket; +uchar *NetRxPacket; /* Current rx packet length */ int NetRxPacketLen; /* IP packet ID */ @@ -208,10 +208,10 @@ void NcStart(void); int nc_input_packet(uchar *pkt, unsigned dest, unsigned src, unsigned len); #endif
-volatile uchar PktBuf[(PKTBUFSRX+1) * PKTSIZE_ALIGN + PKTALIGN]; +uchar PktBuf[(PKTBUFSRX+1) * PKTSIZE_ALIGN + PKTALIGN];
/* Receive packet */ -volatile uchar *NetRxPackets[PKTBUFSRX]; +uchar *NetRxPackets[PKTBUFSRX];
/* Current RX packet handler */ static rxhand_f *packetHandler; @@ -225,7 +225,7 @@ static ulong timeStart; /* Current timeout value */ static ulong timeDelta; /* THE transmit packet */ -volatile uchar *NetTxPacket; +uchar *NetTxPacket;
static int net_check_prereq(enum proto_t protocol);
@@ -246,7 +246,7 @@ int NetArpWaitTry;
void ArpRequest(void) {
- volatile uchar *pkt;
- uchar *pkt;
ARP_t *arp;
debug("ARP broadcast %d\n", NetArpWaitTry); @@ -704,7 +704,7 @@ NetSetTimeout(ulong iv, thand_f *f)
void -NetSendPacket(volatile uchar *pkt, int len) +NetSendPacket(uchar *pkt, int len) { (void) eth_send(pkt, len); } @@ -767,8 +767,8 @@ static ushort PingSeqNo; int PingSend(void) { static uchar mac[6];
- volatile IP_t *ip;
- volatile ushort *s;
- IP_t *ip;
- ushort *s;
uchar *pkt;
/* XXX always send arp request */ @@ -783,7 +783,7 @@ int PingSend(void) pkt = NetArpWaitTxPacket; pkt += NetSetEther(pkt, mac, PROT_IP);
- ip = (volatile IP_t *)pkt;
- ip = (IP_t *)pkt;
/* * Construct an IP and ICMP header. @@ -935,9 +935,9 @@ static ushort CDP_compute_csum(const uchar *buff, ushort len)
int CDPSendTrigger(void) {
- volatile uchar *pkt;
- volatile ushort *s;
- volatile ushort *cp;
- uchar *pkt;
- ushort *s;
- ushort *cp;
Ethernet_t *et; int len; ushort chksum; @@ -964,7 +964,7 @@ int CDPSendTrigger(void) /* CDP header */ *pkt++ = 0x02; /* CDP version 2 */ *pkt++ = 180; /* TTL */
- s = (volatile ushort *)pkt;
- s = (ushort *)pkt;
cp = s; /* checksum (0 for later calculation) */ *s++ = htons(0); @@ -1454,9 +1454,9 @@ NetReceive(volatile uchar *inpkt, int len)
debug("packet received\n");
- NetRxPacket = inpkt;
- NetRxPacket = (uchar *)inpkt;
NetRxPacketLen = len;
- et = (Ethernet_t *)inpkt;
- et = (Ethernet_t *)NetRxPacket;
Why change this?
/* too small packet? */ if (len < ETHER_HDR_SIZE) @@ -1491,11 +1491,11 @@ NetReceive(volatile uchar *inpkt, int len) */ x = ntohs(et->et_prot);
- ip = (IP_t *)(inpkt + E802_HDR_SIZE);
- ip = (IP_t *)(NetRxPacket + E802_HDR_SIZE);
and these? You are using a global instead of the passed-in local.
len -= E802_HDR_SIZE;
} else if (x != PROT_VLAN) { /* normal packet */
- ip = (IP_t *)(inpkt + ETHER_HDR_SIZE);
- ip = (IP_t *)(NetRxPacket + ETHER_HDR_SIZE);
len -= ETHER_HDR_SIZE;
} else { /* VLAN packet */ @@ -1519,7 +1519,7 @@ NetReceive(volatile uchar *inpkt, int len) vlanid = cti & VLAN_IDMASK; x = ntohs(vet->vet_type);
- ip = (IP_t *)(inpkt + VLAN_ETHER_HDR_SIZE);
- ip = (IP_t *)(NetRxPacket + VLAN_ETHER_HDR_SIZE);
len -= VLAN_ETHER_HDR_SIZE; }
@@ -1917,7 +1917,7 @@ NetEthHdrSize(void) }
int -NetSetEther(volatile uchar *xet, uchar * addr, uint prot) +NetSetEther(uchar *xet, uchar * addr, uint prot) { Ethernet_t *et = (Ethernet_t *)xet; ushort myvlanid; @@ -1942,7 +1942,7 @@ NetSetEther(volatile uchar *xet, uchar * addr, uint prot) }
void -NetSetIP(volatile uchar *xip, IPaddr_t dest, int dport, int sport, int len) +NetSetIP(uchar *xip, IPaddr_t dest, int dport, int sport, int len) { IP_t *ip = (IP_t *)xip;
diff --git a/net/rarp.c b/net/rarp.c index 097f970..77d63e8 100644 --- a/net/rarp.c +++ b/net/rarp.c @@ -71,7 +71,7 @@ void RarpRequest (void) { int i;
- volatile uchar *pkt;
- uchar *pkt;
ARP_t * rarp;
printf("RARP broadcast %d\n", ++RarpTry); diff --git a/net/tftp.c b/net/tftp.c index 7aa3e23..e62f229 100644 --- a/net/tftp.c +++ b/net/tftp.c @@ -310,9 +310,9 @@ static void TftpSend(void) { uchar *pkt;
- volatile uchar *xp;
- int len = 0;
- volatile ushort *s;
- uchar *xp;
- int len = 0;
- ushort *s;
#ifdef CONFIG_MCAST_TFTP /* Multicast TFTP.. non-MasterClients do not ACK data. */ -- 1.6.0.2
U-Boot mailing list U-Boot@lists.denx.de http://lists.denx.de/mailman/listinfo/u-boot
Regards, Simon

Hi Simon,
On Fri, Jan 20, 2012 at 10:22 AM, Simon Glass sjg@chromium.org wrote:
Hi Joe,
On Thu, Jan 19, 2012 at 4:53 PM, Joe Hershberger joe.hershberger@ni.com wrote:
The mv_eth driver should not redefine the net function definition
Signed-off-by: Joe Hershberger joe.hershberger@ni.com Cc: Joe Hershberger joe.hershberger@gmail.com Cc: Wolfgang Denk wd@denx.de
<snip> @@ -1454,9 +1454,9 @@ NetReceive(volatile uchar *inpkt, int len)
debug("packet received\n");
- NetRxPacket = inpkt;
- NetRxPacket = (uchar *)inpkt;
NetRxPacketLen = len;
- et = (Ethernet_t *)inpkt;
- et = (Ethernet_t *)NetRxPacket;
Why change this?
/* too small packet? */ if (len < ETHER_HDR_SIZE) @@ -1491,11 +1491,11 @@ NetReceive(volatile uchar *inpkt, int len) */ x = ntohs(et->et_prot);
- ip = (IP_t *)(inpkt + E802_HDR_SIZE);
- ip = (IP_t *)(NetRxPacket + E802_HDR_SIZE);
and these? You are using a global instead of the passed-in local.
len -= E802_HDR_SIZE;
} else if (x != PROT_VLAN) { /* normal packet */
- ip = (IP_t *)(inpkt + ETHER_HDR_SIZE);
- ip = (IP_t *)(NetRxPacket + ETHER_HDR_SIZE);
len -= ETHER_HDR_SIZE;
} else { /* VLAN packet */ @@ -1519,7 +1519,7 @@ NetReceive(volatile uchar *inpkt, int len) vlanid = cti & VLAN_IDMASK; x = ntohs(vet->vet_type);
- ip = (IP_t *)(inpkt + VLAN_ETHER_HDR_SIZE);
- ip = (IP_t *)(NetRxPacket + VLAN_ETHER_HDR_SIZE);
len -= VLAN_ETHER_HDR_SIZE; }
This patch removes volatile from the NetRxPacket and all but 1 of the other places that inpkt was assigned. You will notice that the first assignment of inpkt to NetRxPacket casts away the volatile:
@@ -1454,9 +1454,9 @@ NetReceive(volatile uchar *inpkt, int len)
<snip> - NetRxPacket = inpkt; + NetRxPacket = (uchar *)inpkt;
All the assignments that need a non-volatile pointer now use NetRxPacket instead of inpkt, since it is already assigned and and the same type minus volatile.
Best regards, -Joe

Hi Joe,
On Fri, Jan 20, 2012 at 12:15 PM, Joe Hershberger joe.hershberger@gmail.com wrote:
Hi Simon,
On Fri, Jan 20, 2012 at 10:22 AM, Simon Glass sjg@chromium.org wrote:
Hi Joe,
On Thu, Jan 19, 2012 at 4:53 PM, Joe Hershberger joe.hershberger@ni.com wrote:
The mv_eth driver should not redefine the net function definition
Signed-off-by: Joe Hershberger joe.hershberger@ni.com Cc: Joe Hershberger joe.hershberger@gmail.com Cc: Wolfgang Denk wd@denx.de
<snip> @@ -1454,9 +1454,9 @@ NetReceive(volatile uchar *inpkt, int len)
debug("packet received\n");
- NetRxPacket = inpkt;
- NetRxPacket = (uchar *)inpkt;
NetRxPacketLen = len;
- et = (Ethernet_t *)inpkt;
- et = (Ethernet_t *)NetRxPacket;
Why change this?
/* too small packet? */ if (len < ETHER_HDR_SIZE) @@ -1491,11 +1491,11 @@ NetReceive(volatile uchar *inpkt, int len) */ x = ntohs(et->et_prot);
- ip = (IP_t *)(inpkt + E802_HDR_SIZE);
- ip = (IP_t *)(NetRxPacket + E802_HDR_SIZE);
and these? You are using a global instead of the passed-in local.
len -= E802_HDR_SIZE;
} else if (x != PROT_VLAN) { /* normal packet */
- ip = (IP_t *)(inpkt + ETHER_HDR_SIZE);
- ip = (IP_t *)(NetRxPacket + ETHER_HDR_SIZE);
len -= ETHER_HDR_SIZE;
} else { /* VLAN packet */ @@ -1519,7 +1519,7 @@ NetReceive(volatile uchar *inpkt, int len) vlanid = cti & VLAN_IDMASK; x = ntohs(vet->vet_type);
- ip = (IP_t *)(inpkt + VLAN_ETHER_HDR_SIZE);
- ip = (IP_t *)(NetRxPacket + VLAN_ETHER_HDR_SIZE);
len -= VLAN_ETHER_HDR_SIZE; }
This patch removes volatile from the NetRxPacket and all but 1 of the other places that inpkt was assigned. You will notice that the first assignment of inpkt to NetRxPacket casts away the volatile:
Yes - I wonder why NetReceive needs to remain volatile?
@@ -1454,9 +1454,9 @@ NetReceive(volatile uchar *inpkt, int len)
<snip> - NetRxPacket = inpkt; + NetRxPacket = (uchar *)inpkt;
All the assignments that need a non-volatile pointer now use NetRxPacket instead of inpkt, since it is already assigned and and the same type minus volatile.
Yes, I am only sensitive to this because it is a global and there is enough use of globals in the net code already.
Best regards, -Joe
Regards, Simon

Hi Simon,
On Tue, Jan 24, 2012 at 12:09 AM, Simon Glass sjg@chromium.org wrote:
Hi Joe,
On Fri, Jan 20, 2012 at 12:15 PM, Joe Hershberger joe.hershberger@gmail.com wrote:
Hi Simon,
On Fri, Jan 20, 2012 at 10:22 AM, Simon Glass sjg@chromium.org wrote:
Hi Joe,
On Thu, Jan 19, 2012 at 4:53 PM, Joe Hershberger joe.hershberger@ni.com wrote:
The mv_eth driver should not redefine the net function definition
Signed-off-by: Joe Hershberger joe.hershberger@ni.com Cc: Joe Hershberger joe.hershberger@gmail.com Cc: Wolfgang Denk wd@denx.de
<snip> @@ -1454,9 +1454,9 @@ NetReceive(volatile uchar *inpkt, int len)
debug("packet received\n");
- NetRxPacket = inpkt;
- NetRxPacket = (uchar *)inpkt;
NetRxPacketLen = len;
- et = (Ethernet_t *)inpkt;
- et = (Ethernet_t *)NetRxPacket;
Why change this?
/* too small packet? */ if (len < ETHER_HDR_SIZE) @@ -1491,11 +1491,11 @@ NetReceive(volatile uchar *inpkt, int len) */ x = ntohs(et->et_prot);
- ip = (IP_t *)(inpkt + E802_HDR_SIZE);
- ip = (IP_t *)(NetRxPacket + E802_HDR_SIZE);
and these? You are using a global instead of the passed-in local.
len -= E802_HDR_SIZE;
} else if (x != PROT_VLAN) { /* normal packet */
- ip = (IP_t *)(inpkt + ETHER_HDR_SIZE);
- ip = (IP_t *)(NetRxPacket + ETHER_HDR_SIZE);
len -= ETHER_HDR_SIZE;
} else { /* VLAN packet */ @@ -1519,7 +1519,7 @@ NetReceive(volatile uchar *inpkt, int len) vlanid = cti & VLAN_IDMASK; x = ntohs(vet->vet_type);
- ip = (IP_t *)(inpkt + VLAN_ETHER_HDR_SIZE);
- ip = (IP_t *)(NetRxPacket + VLAN_ETHER_HDR_SIZE);
len -= VLAN_ETHER_HDR_SIZE; }
This patch removes volatile from the NetRxPacket and all but 1 of the other places that inpkt was assigned. You will notice that the first assignment of inpkt to NetRxPacket casts away the volatile:
Yes - I wonder why NetReceive needs to remain volatile?
It is called by all the Ethernet drivers with volatile buffer pointers. At this point, I decided to limit the changes to not include every Ethernet driver (have to split it somewhere).
@@ -1454,9 +1454,9 @@ NetReceive(volatile uchar *inpkt, int len)
<snip> - NetRxPacket = inpkt; + NetRxPacket = (uchar *)inpkt;
All the assignments that need a non-volatile pointer now use NetRxPacket instead of inpkt, since it is already assigned and and the same type minus volatile.
Yes, I am only sensitive to this because it is a global and there is enough use of globals in the net code already.
I chose to not add a local variable to hold the non-volatile pointer since there was already a global. Since u-boot is single threaded, this should not be a problem and could be easily changed later when the Ethernet driver interface is cleaned up.
Best regards, -Joe

On Tuesday 24 January 2012 01:27:53 Joe Hershberger wrote:
On Tue, Jan 24, 2012 at 12:09 AM, Simon Glass wrote:
On Fri, Jan 20, 2012 at 12:15 PM, Joe Hershberger wrote:
On Fri, Jan 20, 2012 at 10:22 AM, Simon Glass wrote:
On Thu, Jan 19, 2012 at 4:53 PM, Joe Hershberger wrote:
@@ -1454,9 +1454,9 @@ NetReceive(volatile uchar *inpkt, int len)
<snip> - NetRxPacket = inpkt; + NetRxPacket = (uchar *)inpkt;
All the assignments that need a non-volatile pointer now use NetRxPacket instead of inpkt, since it is already assigned and and the same type minus volatile.
Yes, I am only sensitive to this because it is a global and there is enough use of globals in the net code already.
I chose to not add a local variable to hold the non-volatile pointer since there was already a global. Since u-boot is single threaded, this should not be a problem and could be easily changed later when the Ethernet driver interface is cleaned up.
funcs given a ptr should operate on that ptr. relying on global variables is a step backwards imo. -mike

in general, i like this. my only concern would be the drivers that might break due to incorrect cache management (which the volatile markers happen to work around for them).
having the API accept a volatile but then casting it away puts us in a worse place i think. on one hand, our API is saying "we treat it as volatile" when we really don't, so any drivers that call it with a volatile don't get build warnings. i'd just drop it from all of the net API so that the drivers which do call with a volatile pointer get a build warning -- now the driver maintainer knows they have to at least look at something. -mike

Hi Mike,
On Fri, Feb 3, 2012 at 5:44 AM, Mike Frysinger vapier@gentoo.org wrote:
in general, i like this. my only concern would be the drivers that might break due to incorrect cache management (which the volatile markers happen to work around for them).
having the API accept a volatile but then casting it away puts us in a worse place i think. on one hand, our API is saying "we treat it as volatile" when we really don't, so any drivers that call it with a volatile don't get build warnings. i'd just drop it from all of the net API so that the drivers which do call with a volatile pointer get a build warning -- now the driver maintainer knows they have to at least look at something. -mike
I'm fine with that... it was my first approach, in fact. However, I was concerned that if I submitted a patch that added warnings to every Ethernet-enabled board, that would be frowned upon. I think it is the correct thing to do, but don't have a good feeling for how open people are to changing an interface like that. I also did not want to attempt to revise every net driver in this series.
Wolfgang,
Would you approve of changing the net driver API to not use volatile buffer pointers and have the driver file warn until the driver maintainer addresses the warnings?
Thanks, -Joe

Signed-off-by: Joe Hershberger joe.hershberger@ni.com Cc: Joe Hershberger joe.hershberger@gmail.com Cc: Wolfgang Denk wd@denx.de --- common/main.c | 1 - drivers/net/netconsole.c | 122 +++++++++-------- include/common.h | 4 + include/net.h | 123 +++++++++-------- net/bootp.c | 338 +++++++++++++++++++++++----------------------- net/bootp.h | 56 ++++---- net/eth.c | 66 +++++----- net/net.c | 19 +-- net/nfs.c | 300 ++++++++++++++++++++--------------------- net/nfs.h | 2 +- net/rarp.c | 28 ++-- net/rarp.h | 4 +- net/sntp.c | 31 +++-- net/sntp.h | 2 +- net/tftp.c | 30 ++--- net/tftp.h | 2 +- 16 files changed, 572 insertions(+), 556 deletions(-)
diff --git a/common/main.c b/common/main.c index e96c95a..c514345 100644 --- a/common/main.c +++ b/common/main.c @@ -979,7 +979,6 @@ int readline_into_buffer (const char *const prompt, char * buffer)
#ifdef CONFIG_SHOW_ACTIVITY while (!tstc()) { - extern void show_activity(int arg); show_activity(0); WATCHDOG_RESET(); } diff --git a/drivers/net/netconsole.c b/drivers/net/netconsole.c index 56ba64f..7acd5b0 100644 --- a/drivers/net/netconsole.c +++ b/drivers/net/netconsole.c @@ -29,81 +29,82 @@ DECLARE_GLOBAL_DATA_PTR;
static char input_buffer[512]; -static int input_size = 0; /* char count in input buffer */ -static int input_offset = 0; /* offset to valid chars in input buffer */ -static int input_recursion = 0; -static int output_recursion = 0; +static int input_size; /* char count in input buffer */ +static int input_offset; /* offset to valid chars in input buffer */ +static int input_recursion; +static int output_recursion; static int net_timeout; -static uchar nc_ether[6]; /* server enet address */ -static IPaddr_t nc_ip; /* server ip */ -static short nc_port; /* source/target port */ -static const char *output_packet; /* used by first send udp */ -static int output_packet_len = 0; +static uchar nc_ether[6]; /* server enet address */ +static IPaddr_t nc_ip; /* server ip */ +static short nc_port; /* source/target port */ +static const char *output_packet; /* used by first send udp */ +static int output_packet_len;
static void nc_wait_arp_handler(uchar *pkt, unsigned dest, IPaddr_t sip, unsigned src, unsigned len) { - NetState = NETLOOP_SUCCESS; /* got arp reply - quit net loop */ + NetState = NETLOOP_SUCCESS; /* got arp reply - quit net loop */ }
static void nc_handler(uchar *pkt, unsigned dest, IPaddr_t sip, unsigned src, unsigned len) { if (input_size) - NetState = NETLOOP_SUCCESS; /* got input - quit net loop */ + NetState = NETLOOP_SUCCESS; /* got input - quit net loop */ }
-static void nc_timeout (void) +static void nc_timeout(void) { NetState = NETLOOP_SUCCESS; }
-void NcStart (void) +void NcStart(void) { - if (!output_packet_len || memcmp (nc_ether, NetEtherNullAddr, 6)) { + if (!output_packet_len || memcmp(nc_ether, NetEtherNullAddr, 6)) { /* going to check for input packet */ - NetSetHandler (nc_handler); - NetSetTimeout (net_timeout, nc_timeout); + NetSetHandler(nc_handler); + NetSetTimeout(net_timeout, nc_timeout); } else { /* send arp request */ uchar *pkt; - NetSetHandler (nc_wait_arp_handler); - pkt = (uchar *) NetTxPacket + NetEthHdrSize () + IP_HDR_SIZE; - memcpy (pkt, output_packet, output_packet_len); - NetSendUDPPacket (nc_ether, nc_ip, nc_port, nc_port, output_packet_len); + NetSetHandler(nc_wait_arp_handler); + pkt = (uchar *)NetTxPacket + NetEthHdrSize() + IP_HDR_SIZE; + memcpy(pkt, output_packet, output_packet_len); + NetSendUDPPacket(nc_ether, nc_ip, nc_port, nc_port, + output_packet_len); } }
-int nc_input_packet (uchar * pkt, unsigned dest, unsigned src, unsigned len) +int nc_input_packet(uchar *pkt, unsigned dest, unsigned src, unsigned len) { int end, chunk;
if (dest != nc_port || !len) - return 0; /* not for us */ + return 0; /* not for us */
- if (input_size == sizeof input_buffer) - return 1; /* no space */ - if (len > sizeof input_buffer - input_size) - len = sizeof input_buffer - input_size; + if (input_size == sizeof(input_buffer)) + return 1; /* no space */ + if (len > sizeof(input_buffer) - input_size) + len = sizeof(input_buffer) - input_size;
end = input_offset + input_size; - if (end > sizeof input_buffer) - end -= sizeof input_buffer; + if (end > sizeof(input_buffer)) + end -= sizeof(input_buffer);
chunk = len; - if (end + len > sizeof input_buffer) { - chunk = sizeof input_buffer - end; + if (end + len > sizeof(input_buffer)) { + chunk = sizeof(input_buffer) - end; memcpy(input_buffer, pkt + chunk, len - chunk); } - memcpy (input_buffer + end, pkt, chunk); + memcpy(input_buffer + end, pkt, chunk);
input_size += len;
return 1; }
-static void nc_send_packet (const char *buf, int len) +static void nc_send_packet(const char *buf, int len) { struct eth_device *eth; int inited = 0; @@ -111,33 +112,33 @@ static void nc_send_packet (const char *buf, int len) uchar *ether; IPaddr_t ip;
- if ((eth = eth_get_dev ()) == NULL) { + eth = eth_get_dev(); + if (eth == NULL) return; - }
- if (!memcmp (nc_ether, NetEtherNullAddr, 6)) { + if (!memcmp(nc_ether, NetEtherNullAddr, 6)) { if (eth->state == ETH_STATE_ACTIVE) return; /* inside net loop */ output_packet = buf; output_packet_len = len; - NetLoop (NETCONS); /* wait for arp reply and send packet */ + NetLoop(NETCONS); /* wait for arp reply and send packet */ output_packet_len = 0; return; }
if (eth->state != ETH_STATE_ACTIVE) { - if (eth_init (gd->bd) < 0) + if (eth_init(gd->bd) < 0) return; inited = 1; } - pkt = (uchar *) NetTxPacket + NetEthHdrSize () + IP_HDR_SIZE; - memcpy (pkt, buf, len); + pkt = (uchar *)NetTxPacket + NetEthHdrSize() + IP_HDR_SIZE; + memcpy(pkt, buf, len); ether = nc_ether; ip = nc_ip; - NetSendUDPPacket (ether, ip, nc_port, nc_port, len); + NetSendUDPPacket(ether, ip, nc_port, nc_port, len);
if (inited) - eth_halt (); + eth_halt(); }
static int nc_start(void) @@ -146,26 +147,27 @@ static int nc_start(void)
nc_port = 6666; /* default port */
- if (getenv ("ncip")) { + if (getenv("ncip")) { char *p;
- nc_ip = getenv_IPaddr ("ncip"); + nc_ip = getenv_IPaddr("ncip"); if (!nc_ip) return -1; /* ncip is 0.0.0.0 */ - if ((p = strchr (getenv ("ncip"), ':')) != NULL) - nc_port = simple_strtoul (p + 1, NULL, 10); + p = strchr(getenv("ncip"), ':'); + if (p != NULL) + nc_port = simple_strtoul(p + 1, NULL, 10); } else nc_ip = ~0; /* ncip is not set */
- our_ip = getenv_IPaddr ("ipaddr"); - netmask = getenv_IPaddr ("netmask"); + our_ip = getenv_IPaddr("ipaddr"); + netmask = getenv_IPaddr("netmask");
if (nc_ip == ~0 || /* 255.255.255.255 */ ((netmask & our_ip) == (netmask & nc_ip) && /* on the same net */ - (netmask | nc_ip) == ~0)) /* broadcast to our net */ - memset (nc_ether, 0xff, sizeof nc_ether); + (netmask | nc_ip) == ~0)) /* broadcast to our net */ + memset(nc_ether, 0xff, sizeof(nc_ether)); else - memset (nc_ether, 0, sizeof nc_ether); /* force arp request */ + memset(nc_ether, 0, sizeof(nc_ether)); /* force arp request */
return 0; } @@ -176,7 +178,7 @@ static void nc_putc(char c) return; output_recursion = 1;
- nc_send_packet (&c, 1); + nc_send_packet(&c, 1);
output_recursion = 0; } @@ -208,14 +210,14 @@ static int nc_getc(void)
net_timeout = 0; /* no timeout */ while (!input_size) - NetLoop (NETCONS); + NetLoop(NETCONS);
input_recursion = 0;
c = input_buffer[input_offset++];
- if (input_offset >= sizeof input_buffer) - input_offset -= sizeof input_buffer; + if (input_offset >= sizeof(input_buffer)) + input_offset -= sizeof(input_buffer); input_size--;
return c; @@ -231,28 +233,28 @@ static int nc_tstc(void) if (input_size) return 1;
- eth = eth_get_dev (); + eth = eth_get_dev(); if (eth && eth->state == ETH_STATE_ACTIVE) return 0; /* inside net loop */
input_recursion = 1;
net_timeout = 1; - NetLoop (NETCONS); /* kind of poll */ + NetLoop(NETCONS); /* kind of poll */
input_recursion = 0;
return input_size != 0; }
-int drv_nc_init (void) +int drv_nc_init(void) { struct stdio_dev dev; int rc;
- memset (&dev, 0, sizeof (dev)); + memset(&dev, 0, sizeof(dev));
- strcpy (dev.name, "nc"); + strcpy(dev.name, "nc"); dev.flags = DEV_FLAGS_OUTPUT | DEV_FLAGS_INPUT | DEV_FLAGS_SYSTEM; dev.start = nc_start; dev.putc = nc_putc; @@ -260,7 +262,7 @@ int drv_nc_init (void) dev.getc = nc_getc; dev.tstc = nc_tstc;
- rc = stdio_register (&dev); + rc = stdio_register(&dev);
return (rc == 0) ? 1 : rc; } diff --git a/include/common.h b/include/common.h index 3df1def..bd557bd 100644 --- a/include/common.h +++ b/include/common.h @@ -804,6 +804,10 @@ int pcmcia_init (void); */ void show_boot_progress(int val);
+#ifdef CONFIG_SHOW_ACTIVITY +void show_activity(int arg); +#endif + /* Multicore arch functions */ #ifdef CONFIG_MP int cpu_status(int nr); diff --git a/include/net.h b/include/net.h index ccdb01d..09d1b51 100644 --- a/include/net.h +++ b/include/net.h @@ -81,27 +81,27 @@ struct eth_device { int iobase; int state;
- int (*init) (struct eth_device*, bd_t*); + int (*init) (struct eth_device *, bd_t *); int (*send) (struct eth_device*, volatile void* packet, int length); - int (*recv) (struct eth_device*); - void (*halt) (struct eth_device*); + int (*recv) (struct eth_device *); + void (*halt) (struct eth_device *); #ifdef CONFIG_MCAST_TFTP - int (*mcast) (struct eth_device*, u32 ip, u8 set); + int (*mcast) (struct eth_device *, u32 ip, u8 set); #endif - int (*write_hwaddr) (struct eth_device*); + int (*write_hwaddr) (struct eth_device *); struct eth_device *next; int index; void *priv; };
extern int eth_initialize(bd_t *bis); /* Initialize network subsystem */ -extern int eth_register(struct eth_device* dev);/* Register network device */ +extern int eth_register(struct eth_device *dev);/* Register network device */ extern void eth_try_another(int first_restart); /* Change the device */ extern void eth_set_current(void); /* set nterface to ethcur var */ extern struct eth_device *eth_get_dev(void); /* get the current device MAC */ extern struct eth_device *eth_get_dev_by_name(const char *devname); extern struct eth_device *eth_get_dev_by_index(int index); /* get dev @ index */ -extern int eth_get_dev_index (void); /* get the device index */ +extern int eth_get_dev_index(void); /* get the device index */ extern void eth_parse_enetaddr(const char *addr, uchar *enetaddr); extern int eth_getenv_enetaddr(char *name, uchar *enetaddr); extern int eth_setenv_enetaddr(char *name, const uchar *enetaddr); @@ -142,8 +142,8 @@ int eth_write_hwaddr(struct eth_device *dev, const char *base_name, int eth_number);
#ifdef CONFIG_MCAST_TFTP -int eth_mcast_join( IPaddr_t mcast_addr, u8 join); -u32 ether_crc (size_t len, unsigned char const *p); +int eth_mcast_join(IPaddr_t mcast_addr, u8 join); +u32 ether_crc(size_t len, unsigned char const *p); #endif
@@ -218,8 +218,8 @@ typedef struct { #define IP_FLAGS_DFRAG 0x4000 /* don't fragments */ #define IP_FLAGS_MFRAG 0x2000 /* more fragments */
-#define IP_HDR_SIZE_NO_UDP (sizeof (IP_t) - 8) -#define IP_HDR_SIZE (sizeof (IP_t)) +#define IP_HDR_SIZE_NO_UDP (sizeof(IP_t) - 8) +#define IP_HDR_SIZE (sizeof(IP_t))
/* @@ -326,46 +326,46 @@ typedef struct icmphdr {
/* net.c */ /** BOOTP EXTENTIONS **/ -extern IPaddr_t NetOurGatewayIP; /* Our gateway IP addresse */ -extern IPaddr_t NetOurSubnetMask; /* Our subnet mask (0 = unknown)*/ -extern IPaddr_t NetOurDNSIP; /* Our Domain Name Server (0 = unknown)*/ +extern IPaddr_t NetOurGatewayIP; /* Our gateway IP address */ +extern IPaddr_t NetOurSubnetMask; /* Our subnet mask (0 = unknown) */ +extern IPaddr_t NetOurDNSIP; /* Our Domain Name Server (0 = unknown) */ #if defined(CONFIG_BOOTP_DNS2) -extern IPaddr_t NetOurDNS2IP; /* Our 2nd Domain Name Server (0 = unknown)*/ +extern IPaddr_t NetOurDNS2IP; /* Our 2nd Domain Name Server (0 = unknown) */ #endif -extern char NetOurNISDomain[32]; /* Our NIS domain */ -extern char NetOurHostName[32]; /* Our hostname */ -extern char NetOurRootPath[64]; /* Our root path */ -extern ushort NetBootFileSize; /* Our boot file size in blocks */ +extern char NetOurNISDomain[32]; /* Our NIS domain */ +extern char NetOurHostName[32]; /* Our hostname */ +extern char NetOurRootPath[64]; /* Our root path */ +extern ushort NetBootFileSize; /* Our boot file size in blocks */ /** END OF BOOTP EXTENTIONS **/ -extern ulong NetBootFileXferSize; /* size of bootfile in bytes */ -extern uchar NetOurEther[6]; /* Our ethernet address */ -extern uchar NetServerEther[6]; /* Boot server enet address */ -extern IPaddr_t NetOurIP; /* Our IP addr (0 = unknown) */ -extern IPaddr_t NetServerIP; /* Server IP addr (0 = unknown) */ +extern ulong NetBootFileXferSize; /* size of bootfile in bytes */ +extern uchar NetOurEther[6]; /* Our ethernet address */ +extern uchar NetServerEther[6]; /* Boot server enet address */ +extern IPaddr_t NetOurIP; /* Our IP addr (0 = unknown) */ +extern IPaddr_t NetServerIP; /* Server IP addr (0 = unknown) */ extern uchar *NetTxPacket; /* THE transmit packet */ -extern uchar *NetRxPackets[PKTBUFSRX];/* Receive packets */ +extern uchar *NetRxPackets[PKTBUFSRX]; /* Receive packets */ extern uchar *NetRxPacket; /* Current receive packet */ -extern int NetRxPacketLen; /* Current rx packet length */ -extern unsigned NetIPID; /* IP ID (counting) */ -extern uchar NetBcastAddr[6]; /* Ethernet boardcast address */ +extern int NetRxPacketLen; /* Current rx packet length */ +extern unsigned NetIPID; /* IP ID (counting) */ +extern uchar NetBcastAddr[6]; /* Ethernet boardcast address */ extern uchar NetEtherNullAddr[6];
-#define VLAN_NONE 4095 /* untagged */ -#define VLAN_IDMASK 0x0fff /* mask of valid vlan id */ -extern ushort NetOurVLAN; /* Our VLAN */ -extern ushort NetOurNativeVLAN; /* Our Native VLAN */ +#define VLAN_NONE 4095 /* untagged */ +#define VLAN_IDMASK 0x0fff /* mask of valid vlan id */ +extern ushort NetOurVLAN; /* Our VLAN */ +extern ushort NetOurNativeVLAN; /* Our Native VLAN */
-extern uchar NetCDPAddr[6]; /* Ethernet CDP address */ -extern ushort CDPNativeVLAN; /* CDP returned native VLAN */ -extern ushort CDPApplianceVLAN; /* CDP returned appliance VLAN */ +extern uchar NetCDPAddr[6]; /* Ethernet CDP address */ +extern ushort CDPNativeVLAN; /* CDP returned native VLAN */ +extern ushort CDPApplianceVLAN; /* CDP returned appliance VLAN */
-extern int NetState; /* Network loop state */ +extern int NetState; /* Network loop state */ #define NETLOOP_CONTINUE 1 #define NETLOOP_RESTART 2 #define NETLOOP_SUCCESS 3 #define NETLOOP_FAIL 4
-extern int NetRestartWrap; /* Tried all network devices */ +extern int NetRestartWrap; /* Tried all network devices */
enum proto_t { BOOTP, RARP, ARP, TFTPGET, DHCP, PING, DNS, NFS, CDP, NETCONS, SNTP, @@ -373,7 +373,7 @@ enum proto_t { };
/* from net/net.c */ -extern char BootFile[128]; /* Boot File name */ +extern char BootFile[128]; /* Boot File name */
#if defined(CONFIG_CMD_DNS) extern char *NetDNSResolve; /* The host to resolve */ @@ -381,7 +381,7 @@ extern char *NetDNSenvvar; /* the env var to put the ip into */ #endif
#if defined(CONFIG_CMD_PING) -extern IPaddr_t NetPingIP; /* the ip address to ping */ +extern IPaddr_t NetPingIP; /* the ip address to ping */ #endif
#if defined(CONFIG_CMD_CDP) @@ -391,8 +391,12 @@ extern ushort CDPApplianceVLAN; #endif
#if defined(CONFIG_CMD_SNTP) -extern IPaddr_t NetNtpServerIP; /* the ip address to NTP */ -extern int NetTimeOffset; /* offset time from UTC */ +extern IPaddr_t NetNtpServerIP; /* the ip address to NTP */ +extern int NetTimeOffset; /* offset time from UTC */ +#endif + +#if defined(CONFIG_MCAST_TFTP) +extern IPaddr_t Mcast_addr; #endif
/* Initialize the network adapter */ @@ -414,23 +418,29 @@ extern int NetSetEther(uchar *, uchar *, uint); extern void NetSetIP(uchar *, IPaddr_t, int, int, int);
/* Checksum */ -extern int NetCksumOk(uchar *, int); /* Return true if cksum OK */ -extern uint NetCksum(uchar *, int); /* Calculate the checksum */ +extern int NetCksumOk(uchar *, int); /* Return true if cksum OK */ +extern uint NetCksum(uchar *, int); /* Calculate the checksum */
/* Set callbacks */ -extern void NetSetHandler(rxhand_f *); /* Set RX packet handler */ +extern void NetSetHandler(rxhand_f *); /* Set RX packet handler */ extern void net_set_icmp_handler(rxhand_icmp_f *f); /* Set ICMP RX handler */ -extern void NetSetTimeout(ulong, thand_f *);/* Set timeout handler */ +extern void NetSetTimeout(ulong, thand_f *);/* Set timeout handler */
/* Transmit "NetTxPacket" */ extern void NetSendPacket(uchar *, int);
/* Transmit UDP packet, performing ARP request if needed */ -extern int NetSendUDPPacket(uchar *ether, IPaddr_t dest, int dport, int sport, int len); +extern int NetSendUDPPacket(uchar *ether, IPaddr_t dest, int dport, + int sport, int len);
/* Processes a received packet */ extern void NetReceive(volatile uchar *, int);
+#ifdef CONFIG_NETCONSOLE +void NcStart(void); +int nc_input_packet(uchar *pkt, unsigned dest, unsigned src, unsigned len); +#endif + /* * Check if autoload is enabled. If so, use either NFS or TFTP to download * the boot file. @@ -448,7 +458,7 @@ void net_auto_load(void); static inline IPaddr_t NetReadIP(void *from) { IPaddr_t ip; - memcpy((void*)&ip, (void*)from, sizeof(ip)); + memcpy((void *)&ip, (void *)from, sizeof(ip)); return ip; }
@@ -456,26 +466,26 @@ static inline IPaddr_t NetReadIP(void *from) static inline ulong NetReadLong(ulong *from) { ulong l; - memcpy((void*)&l, (void*)from, sizeof(l)); + memcpy((void *)&l, (void *)from, sizeof(l)); return l; }
/* write IP *in network byteorder* */ static inline void NetWriteIP(void *to, IPaddr_t ip) { - memcpy(to, (void*)&ip, sizeof(ip)); + memcpy(to, (void *)&ip, sizeof(ip)); }
/* copy IP */ static inline void NetCopyIP(void *to, void *from) { - memcpy((void*)to, from, sizeof(IPaddr_t)); + memcpy((void *)to, from, sizeof(IPaddr_t)); }
/* copy ulong */ static inline void NetCopyLong(ulong *to, ulong *from) { - memcpy((void*)to, (void*)from, sizeof(ulong)); + memcpy((void *)to, (void *)from, sizeof(ulong)); }
/** @@ -498,7 +508,7 @@ static inline int is_zero_ether_addr(const u8 *addr) */ static inline int is_multicast_ether_addr(const u8 *addr) { - return (0x01 & addr[0]); + return 0x01 & addr[0]; }
/* @@ -509,7 +519,8 @@ static inline int is_multicast_ether_addr(const u8 *addr) */ static inline int is_broadcast_ether_addr(const u8 *addr) { - return (addr[0] & addr[1] & addr[2] & addr[3] & addr[4] & addr[5]) == 0xff; + return (addr[0] & addr[1] & addr[2] & addr[3] & addr[4] & addr[5]) == + 0xff; }
/* @@ -529,13 +540,13 @@ static inline int is_valid_ether_addr(const u8 *addr) }
/* Convert an IP address to a string */ -extern void ip_to_string (IPaddr_t x, char *s); +extern void ip_to_string(IPaddr_t x, char *s);
/* Convert a string to ip address */ extern IPaddr_t string_to_ip(const char *s);
/* Convert a VLAN id to a string */ -extern void VLAN_to_string (ushort x, char *s); +extern void VLAN_to_string(ushort x, char *s);
/* Convert a string to a vlan id */ extern ushort string_to_VLAN(const char *s); @@ -544,7 +555,7 @@ extern ushort string_to_VLAN(const char *s); extern ushort getenv_VLAN(char *);
/* copy a filename (allow for "..." notation, limit length) */ -extern void copy_filename (char *dst, const char *src, int size); +extern void copy_filename(char *dst, const char *src, int size);
/* get a random source port */ extern unsigned int random_port(void); diff --git a/net/bootp.c b/net/bootp.c index 6d40608..07870d0 100644 --- a/net/bootp.c +++ b/net/bootp.c @@ -19,19 +19,19 @@ #endif #include <linux/compiler.h>
-#define BOOTP_VENDOR_MAGIC 0x63825363 /* RFC1048 Magic Cookie */ +#define BOOTP_VENDOR_MAGIC 0x63825363 /* RFC1048 Magic Cookie */
#define TIMEOUT 5000UL /* Milliseconds before trying BOOTP again */ #ifndef CONFIG_NET_RETRY_COUNT -# define TIMEOUT_COUNT 5 /* # of timeouts before giving up */ +# define TIMEOUT_COUNT 5 /* # of timeouts before giving up */ #else # define TIMEOUT_COUNT (CONFIG_NET_RETRY_COUNT) #endif
-#define PORT_BOOTPS 67 /* BOOTP server UDP port */ -#define PORT_BOOTPC 68 /* BOOTP client UDP port */ +#define PORT_BOOTPS 67 /* BOOTP server UDP port */ +#define PORT_BOOTPC 68 /* BOOTP client UDP port */
-#ifndef CONFIG_DHCP_MIN_EXT_LEN /* minimal length of extension list */ +#ifndef CONFIG_DHCP_MIN_EXT_LEN /* minimal length of extension list */ #define CONFIG_DHCP_MIN_EXT_LEN 64 #endif
@@ -43,8 +43,8 @@ ulong seed1, seed2;
#if defined(CONFIG_CMD_DHCP) dhcp_state_t dhcp_state = INIT; -unsigned long dhcp_leasetime = 0; -IPaddr_t NetDHCPServerIP = 0; +unsigned long dhcp_leasetime; +IPaddr_t NetDHCPServerIP; static void DhcpHandler(uchar *pkt, unsigned dest, IPaddr_t sip, unsigned src, unsigned len);
@@ -64,37 +64,29 @@ static char *dhcpmsg2str(int type) } } #endif - -#if defined(CONFIG_BOOTP_VENDOREX) -extern u8 *dhcp_vendorex_prep (u8 *e); /*rtn new e after add own opts. */ -extern u8 *dhcp_vendorex_proc (u8 *e); /*rtn next e if mine,else NULL */ -#endif - #endif
static int BootpCheckPkt(uchar *pkt, unsigned dest, unsigned src, unsigned len) { - Bootp_t *bp = (Bootp_t *) pkt; + struct Bootp_t *bp = (struct Bootp_t *) pkt; int retval = 0;
if (dest != PORT_BOOTPC || src != PORT_BOOTPS) retval = -1; - else if (len < sizeof (Bootp_t) - OPT_SIZE) + else if (len < sizeof(struct Bootp_t) - OPT_SIZE) retval = -2; else if (bp->bp_op != OP_BOOTREQUEST && - bp->bp_op != OP_BOOTREPLY && - bp->bp_op != DHCP_OFFER && - bp->bp_op != DHCP_ACK && - bp->bp_op != DHCP_NAK ) { + bp->bp_op != OP_BOOTREPLY && + bp->bp_op != DHCP_OFFER && + bp->bp_op != DHCP_ACK && + bp->bp_op != DHCP_NAK) retval = -3; - } else if (bp->bp_htype != HWT_ETHER) retval = -4; else if (bp->bp_hlen != HWL_ETHER) retval = -5; - else if (NetReadLong((ulong*)&bp->bp_id) != BootpID) { + else if (NetReadLong((ulong *)&bp->bp_id) != BootpID) retval = -6; - }
debug("Filtering pkt = %d\n", retval);
@@ -104,7 +96,7 @@ static int BootpCheckPkt(uchar *pkt, unsigned dest, unsigned src, unsigned len) /* * Copy parameters of interest from BOOTP_REPLY/DHCP_OFFER packet */ -static void BootpCopyNetParams(Bootp_t *bp) +static void BootpCopyNetParams(struct Bootp_t *bp) { __maybe_unused IPaddr_t tmp_ip;
@@ -113,10 +105,10 @@ static void BootpCopyNetParams(Bootp_t *bp) NetCopyIP(&tmp_ip, &bp->bp_siaddr); if (tmp_ip != 0) NetCopyIP(&NetServerIP, &bp->bp_siaddr); - memcpy (NetServerEther, ((Ethernet_t *)NetRxPacket)->et_src, 6); + memcpy(NetServerEther, ((Ethernet_t *)NetRxPacket)->et_src, 6); #endif if (strlen(bp->bp_file) > 0) - copy_filename (BootFile, bp->bp_file, sizeof(BootFile)); + copy_filename(BootFile, bp->bp_file, sizeof(BootFile));
debug("Bootfile: %s\n", BootFile);
@@ -124,97 +116,95 @@ static void BootpCopyNetParams(Bootp_t *bp) * don't delete exising entry when BOOTP / DHCP reply does * not contain a new value */ - if (*BootFile) { - setenv ("bootfile", BootFile); - } + if (*BootFile) + setenv("bootfile", BootFile); }
-static int truncate_sz (const char *name, int maxlen, int curlen) +static int truncate_sz(const char *name, int maxlen, int curlen) { if (curlen >= maxlen) { - printf("*** WARNING: %s is too long (%d - max: %d) - truncated\n", - name, curlen, maxlen); + printf("*** WARNING: %s is too long (%d - max: %d)" + " - truncated\n", name, curlen, maxlen); curlen = maxlen - 1; } - return (curlen); + return curlen; }
#if !defined(CONFIG_CMD_DHCP)
-static void BootpVendorFieldProcess (u8 * ext) +static void BootpVendorFieldProcess(u8 *ext) { int size = *(ext + 1);
debug("[BOOTP] Processing extension %d... (%d bytes)\n", *ext, - *(ext + 1)); + *(ext + 1));
NetBootFileSize = 0;
switch (*ext) { /* Fixed length fields */ - case 1: /* Subnet mask */ + case 1: /* Subnet mask */ if (NetOurSubnetMask == 0) - NetCopyIP (&NetOurSubnetMask, (IPaddr_t *) (ext + 2)); + NetCopyIP(&NetOurSubnetMask, (IPaddr_t *) (ext + 2)); break; - case 2: /* Time offset - Not yet supported */ + case 2: /* Time offset - Not yet supported */ break; /* Variable length fields */ - case 3: /* Gateways list */ - if (NetOurGatewayIP == 0) { - NetCopyIP (&NetOurGatewayIP, (IPaddr_t *) (ext + 2)); - } + case 3: /* Gateways list */ + if (NetOurGatewayIP == 0) + NetCopyIP(&NetOurGatewayIP, (IPaddr_t *) (ext + 2)); break; - case 4: /* Time server - Not yet supported */ + case 4: /* Time server - Not yet supported */ break; - case 5: /* IEN-116 name server - Not yet supported */ + case 5: /* IEN-116 name server - Not yet supported */ break; case 6: - if (NetOurDNSIP == 0) { - NetCopyIP (&NetOurDNSIP, (IPaddr_t *) (ext + 2)); - } + if (NetOurDNSIP == 0) + NetCopyIP(&NetOurDNSIP, (IPaddr_t *) (ext + 2)); #if defined(CONFIG_BOOTP_DNS2) - if ((NetOurDNS2IP == 0) && (size > 4)) { - NetCopyIP (&NetOurDNS2IP, (IPaddr_t *) (ext + 2 + 4)); - } + if ((NetOurDNS2IP == 0) && (size > 4)) + NetCopyIP(&NetOurDNS2IP, (IPaddr_t *) (ext + 2 + 4)); #endif break; - case 7: /* Log server - Not yet supported */ + case 7: /* Log server - Not yet supported */ break; - case 8: /* Cookie/Quote server - Not yet supported */ + case 8: /* Cookie/Quote server - Not yet supported */ break; - case 9: /* LPR server - Not yet supported */ + case 9: /* LPR server - Not yet supported */ break; - case 10: /* Impress server - Not yet supported */ + case 10: /* Impress server - Not yet supported */ break; - case 11: /* RPL server - Not yet supported */ + case 11: /* RPL server - Not yet supported */ break; - case 12: /* Host name */ + case 12: /* Host name */ if (NetOurHostName[0] == 0) { - size = truncate_sz ("Host Name", sizeof (NetOurHostName), size); - memcpy (&NetOurHostName, ext + 2, size); + size = truncate_sz("Host Name", + sizeof(NetOurHostName), size); + memcpy(&NetOurHostName, ext + 2, size); NetOurHostName[size] = 0; } break; - case 13: /* Boot file size */ + case 13: /* Boot file size */ if (size == 2) - NetBootFileSize = ntohs (*(ushort *) (ext + 2)); + NetBootFileSize = ntohs(*(ushort *) (ext + 2)); else if (size == 4) - NetBootFileSize = ntohl (*(ulong *) (ext + 2)); + NetBootFileSize = ntohl(*(ulong *) (ext + 2)); break; - case 14: /* Merit dump file - Not yet supported */ + case 14: /* Merit dump file - Not yet supported */ break; - case 15: /* Domain name - Not yet supported */ + case 15: /* Domain name - Not yet supported */ break; - case 16: /* Swap server - Not yet supported */ + case 16: /* Swap server - Not yet supported */ break; - case 17: /* Root path */ + case 17: /* Root path */ if (NetOurRootPath[0] == 0) { - size = truncate_sz ("Root Path", sizeof (NetOurRootPath), size); - memcpy (&NetOurRootPath, ext + 2, size); + size = truncate_sz("Root Path", + sizeof(NetOurRootPath), size); + memcpy(&NetOurRootPath, ext + 2, size); NetOurRootPath[size] = 0; } break; - case 18: /* Extension path - Not yet supported */ + case 18: /* Extension path - Not yet supported */ /* * This can be used to send the information of the * vendor area in another file that the client can @@ -222,10 +212,11 @@ static void BootpVendorFieldProcess (u8 * ext) */ break; /* IP host layer fields */ - case 40: /* NIS Domain name */ + case 40: /* NIS Domain name */ if (NetOurNISDomain[0] == 0) { - size = truncate_sz ("NIS Domain Name", sizeof (NetOurNISDomain), size); - memcpy (&NetOurNISDomain, ext + 2, size); + size = truncate_sz("NIS Domain Name", + sizeof(NetOurNISDomain), size); + memcpy(&NetOurNISDomain, ext + 2, size); NetOurNISDomain[size] = 0; } break; @@ -235,7 +226,7 @@ static void BootpVendorFieldProcess (u8 * ext) break; #endif /* Application layer fields */ - case 43: /* Vendor specific info - Not yet supported */ + case 43: /* Vendor specific info - Not yet supported */ /* * Binary information to exchange specific * product information. @@ -245,7 +236,7 @@ static void BootpVendorFieldProcess (u8 * ext) } }
-static void BootpVendorProcess (u8 * ext, int size) +static void BootpVendorProcess(u8 *ext, int size) { u8 *end = ext + size;
@@ -259,11 +250,11 @@ static void BootpVendorProcess (u8 * ext, int size)
ext += ext[1] + 2; if (ext <= end) - BootpVendorFieldProcess (opt); + BootpVendorFieldProcess(opt); } }
- debug("[BOOTP] Received fields: \n"); + debug("[BOOTP] Received fields:\n"); if (NetOurSubnetMask) debug("NetOurSubnetMask : %pI4\n", &NetOurSubnetMask);
@@ -298,27 +289,28 @@ static void BootpHandler(uchar *pkt, unsigned dest, IPaddr_t sip, unsigned src, unsigned len) { - Bootp_t *bp; + struct Bootp_t *bp;
debug("got BOOTP packet (src=%d, dst=%d, len=%d want_len=%zu)\n", - src, dest, len, sizeof (Bootp_t)); + src, dest, len, sizeof(struct Bootp_t));
- bp = (Bootp_t *)pkt; + bp = (struct Bootp_t *)pkt;
- if (BootpCheckPkt(pkt, dest, src, len)) /* Filter out pkts we don't want */ + /* Filter out pkts we don't want */ + if (BootpCheckPkt(pkt, dest, src, len)) return;
/* * Got a good BOOTP reply. Copy the data into our variables. */ #ifdef CONFIG_STATUS_LED - status_led_set (STATUS_LED_BOOT, STATUS_LED_OFF); + status_led_set(STATUS_LED_BOOT, STATUS_LED_OFF); #endif
BootpCopyNetParams(bp); /* Store net parameters from reply */
/* Retrieve extended information (we must parse the vendor area) */ - if (NetReadLong((ulong*)&bp->bp_vend[0]) == htonl(BOOTP_VENDOR_MAGIC)) + if (NetReadLong((ulong *)&bp->bp_vend[0]) == htonl(BOOTP_VENDOR_MAGIC)) BootpVendorProcess((uchar *)&bp->bp_vend[4], len);
NetSetTimeout(0, (thand_f *)0); @@ -336,11 +328,11 @@ static void BootpTimeout(void) { if (BootpTry >= TIMEOUT_COUNT) { - puts ("\nRetry count exceeded; starting again\n"); - NetStartAgain (); + puts("\nRetry count exceeded; starting again\n"); + NetStartAgain(); } else { - NetSetTimeout (TIMEOUT, BootpTimeout); - BootpRequest (); + NetSetTimeout(TIMEOUT, BootpTimeout); + BootpRequest(); } }
@@ -348,7 +340,8 @@ BootpTimeout(void) * Initialize BOOTP extension fields in the request. */ #if defined(CONFIG_CMD_DHCP) -static int DhcpExtended (u8 * e, int message_type, IPaddr_t ServerID, IPaddr_t RequestedIP) +static int DhcpExtended(u8 *e, int message_type, IPaddr_t ServerID, + IPaddr_t RequestedIP) { u8 *start = e; u8 *cnt; @@ -380,7 +373,7 @@ static int DhcpExtended (u8 * e, int message_type, IPaddr_t ServerID, IPaddr_t R *e++ = (576 - 312 + OPT_SIZE) & 0xff;
if (ServerID) { - int tmp = ntohl (ServerID); + int tmp = ntohl(ServerID);
*e++ = 54; /* ServerID */ *e++ = 4; @@ -391,7 +384,7 @@ static int DhcpExtended (u8 * e, int message_type, IPaddr_t ServerID, IPaddr_t R }
if (RequestedIP) { - int tmp = ntohl (RequestedIP); + int tmp = ntohl(RequestedIP);
*e++ = 50; /* Requested IP */ *e++ = 4; @@ -401,12 +394,13 @@ static int DhcpExtended (u8 * e, int message_type, IPaddr_t ServerID, IPaddr_t R *e++ = tmp & 0xff; } #if defined(CONFIG_BOOTP_SEND_HOSTNAME) - if ((hostname = getenv ("hostname"))) { - int hostnamelen = strlen (hostname); + hostname = getenv("hostname"); + if (hostname) { + int hostnamelen = strlen(hostname);
*e++ = 12; /* Hostname */ *e++ = hostnamelen; - memcpy (e, hostname, hostnamelen); + memcpy(e, hostname, hostnamelen); e += hostnamelen; } #endif @@ -447,7 +441,8 @@ static int DhcpExtended (u8 * e, int message_type, IPaddr_t ServerID, IPaddr_t R #endif
#if defined(CONFIG_BOOTP_VENDOREX) - if ((x = dhcp_vendorex_prep (e))) + x = dhcp_vendorex_prep(e); + if (x) return x - start; #endif
@@ -507,9 +502,9 @@ static int DhcpExtended (u8 * e, int message_type, IPaddr_t ServerID, IPaddr_t R
#else /* - * Warning: no field size check - change CONFIG_BOOTP_* at your own risk! + * Warning: no field size check - change CONFIG_BOOTP_* at your own risk! */ -static int BootpExtended (u8 * e) +static int BootpExtended(u8 *e) { u8 *start = e;
@@ -583,10 +578,10 @@ static int BootpExtended (u8 * e) #endif
void -BootpRequest (void) +BootpRequest(void) { uchar *pkt, *iphdr; - Bootp_t *bp; + struct Bootp_t *bp; int ext_len, pktlen, iplen;
#if defined(CONFIG_CMD_DHCP) @@ -598,22 +593,22 @@ BootpRequest (void) int reg; ulong tst1, tst2, sum, m_mask, m_value = 0;
- if (BootpTry ==0) { + if (BootpTry == 0) { /* get our mac */ eth_getenv_enetaddr("ethaddr", bi_enetaddr);
debug("BootpRequest => Our Mac: "); - for (reg=0; reg<6; reg++) - debug("%x%c", bi_enetaddr[reg], reg==5 ? '\n' : ':'); + for (reg = 0; reg < 6; reg++) + debug("%x%c", bi_enetaddr[reg], reg == 5 ? '\n' : ':');
/* Mac-Manipulation 2 get seed1 */ - tst1=0; - tst2=0; - for (reg=2; reg<6; reg++) { + tst1 = 0; + tst2 = 0; + for (reg = 2; reg < 6; reg++) { tst1 = tst1 << 8; tst1 = tst1 | bi_enetaddr[reg]; } - for (reg=0; reg<2; reg++) { + for (reg = 0; reg < 2; reg++) { tst2 = tst2 | bi_enetaddr[reg]; tst2 = tst2 << 8; } @@ -621,8 +616,8 @@ BootpRequest (void) seed1 = tst1^tst2;
/* Mirror seed1*/ - m_mask=0x1; - for (reg=1;reg<=32;reg++) { + m_mask = 0x1; + for (reg = 1; reg <= 32; reg++) { m_value |= (m_mask & seed1); seed1 = seed1 >> 1; m_value = m_value << 1; @@ -632,44 +627,45 @@ BootpRequest (void) }
/* Random Number Generator */ - - for (reg=0;reg<=0;reg++) { + for (reg = 0; reg <= 0; reg++) { sum = seed1 + seed2; if (sum < seed1 || sum < seed2) sum++; seed2 = seed1; seed1 = sum;
- if (BootpTry<=2) { /* Start with max 1024 * 1ms */ + if (BootpTry <= 2) { /* Start with max 1024 * 1ms */ sum = sum >> (22-BootpTry); - } else { /*After 3rd BOOTP request max 8192 * 1ms */ + } else { /*After 3rd BOOTP request max 8192 * 1ms */ sum = sum >> 19; } }
- printf ("Random delay: %ld ms...\n", sum); - for (reg=0; reg <sum; reg++) { + printf("Random delay: %ld ms...\n", sum); + for (reg = 0; reg < sum; reg++) udelay(1000); /*Wait 1ms*/ - } + #endif /* CONFIG_BOOTP_RANDOM_DELAY */
printf("BOOTP broadcast %d\n", ++BootpTry); pkt = NetTxPacket; - memset ((void*)pkt, 0, PKTSIZE); + memset((void *)pkt, 0, PKTSIZE);
pkt += NetSetEther(pkt, NetBcastAddr, PROT_IP);
/* - * Next line results in incorrect packet size being transmitted, resulting - * in errors in some DHCP servers, reporting missing bytes. Size must be - * set in packet header after extension length has been determined. + * Next line results in incorrect packet size being transmitted, + * resulting in errors in some DHCP servers, reporting missing bytes. + * Size must be set in packet header after extension length has been + * determined. * C. Hallinan, DS4.COM, Inc. */ - /* NetSetIP(pkt, 0xFFFFFFFFL, PORT_BOOTPS, PORT_BOOTPC, sizeof (Bootp_t)); */ + /* NetSetIP(pkt, 0xFFFFFFFFL, PORT_BOOTPS, PORT_BOOTPC, + sizeof (struct Bootp_t)); */ iphdr = pkt; /* We need this later for NetSetIP() */ pkt += IP_HDR_SIZE;
- bp = (Bootp_t *)pkt; + bp = (struct Bootp_t *)pkt; bp->bp_op = OP_BOOTREQUEST; bp->bp_htype = HWT_ETHER; bp->bp_hlen = HWL_ETHER; @@ -679,8 +675,8 @@ BootpRequest (void) NetWriteIP(&bp->bp_yiaddr, 0); NetWriteIP(&bp->bp_siaddr, 0); NetWriteIP(&bp->bp_giaddr, 0); - memcpy (bp->bp_chaddr, NetOurEther, 6); - copy_filename (bp->bp_file, BootFile, sizeof(bp->bp_file)); + memcpy(bp->bp_chaddr, NetOurEther, 6); + copy_filename(bp->bp_file, BootFile, sizeof(bp->bp_file));
/* Request additional information from the BOOTP/DHCP server */ #if defined(CONFIG_CMD_DHCP) @@ -705,7 +701,8 @@ BootpRequest (void) * Calculate proper packet lengths taking into account the * variable size of the options field */ - pktlen = ((int)(pkt-NetTxPacket)) + BOOTP_HDR_SIZE - sizeof(bp->bp_vend) + ext_len; + pktlen = ((int)(pkt-NetTxPacket)) + BOOTP_HDR_SIZE - + sizeof(bp->bp_vend) + ext_len; iplen = BOOTP_HDR_SIZE - sizeof(bp->bp_vend) + ext_len; NetSetIP(iphdr, 0xFFFFFFFFL, PORT_BOOTPS, PORT_BOOTPC, iplen); NetSetTimeout(SELECT_TIMEOUT, BootpTimeout); @@ -720,7 +717,7 @@ BootpRequest (void) }
#if defined(CONFIG_CMD_DHCP) -static void DhcpOptionsProcess (uchar * popt, Bootp_t *bp) +static void DhcpOptionsProcess(uchar *popt, struct Bootp_t *bp) { uchar *end = popt + BOOTP_HDR_SIZE; int oplen, size; @@ -732,50 +729,51 @@ static void DhcpOptionsProcess (uchar * popt, Bootp_t *bp) oplen = *(popt + 1); switch (*popt) { case 1: - NetCopyIP (&NetOurSubnetMask, (popt + 2)); + NetCopyIP(&NetOurSubnetMask, (popt + 2)); break; #if defined(CONFIG_CMD_SNTP) && defined(CONFIG_BOOTP_TIMEOFFSET) case 2: /* Time offset */ to_ptr = &NetTimeOffset; - NetCopyLong ((ulong *)to_ptr, (ulong *)(popt + 2)); - NetTimeOffset = ntohl (NetTimeOffset); + NetCopyLong((ulong *)to_ptr, (ulong *)(popt + 2)); + NetTimeOffset = ntohl(NetTimeOffset); break; #endif case 3: - NetCopyIP (&NetOurGatewayIP, (popt + 2)); + NetCopyIP(&NetOurGatewayIP, (popt + 2)); break; case 6: - NetCopyIP (&NetOurDNSIP, (popt + 2)); + NetCopyIP(&NetOurDNSIP, (popt + 2)); #if defined(CONFIG_BOOTP_DNS2) - if (*(popt + 1) > 4) { - NetCopyIP (&NetOurDNS2IP, (popt + 2 + 4)); - } + if (*(popt + 1) > 4) + NetCopyIP(&NetOurDNS2IP, (popt + 2 + 4)); #endif break; case 12: - size = truncate_sz ("Host Name", sizeof (NetOurHostName), oplen); - memcpy (&NetOurHostName, popt + 2, size); + size = truncate_sz("Host Name", + sizeof(NetOurHostName), oplen); + memcpy(&NetOurHostName, popt + 2, size); NetOurHostName[size] = 0; break; case 15: /* Ignore Domain Name Option */ break; case 17: - size = truncate_sz ("Root Path", sizeof (NetOurRootPath), oplen); - memcpy (&NetOurRootPath, popt + 2, size); + size = truncate_sz("Root Path", + sizeof(NetOurRootPath), oplen); + memcpy(&NetOurRootPath, popt + 2, size); NetOurRootPath[size] = 0; break; #if defined(CONFIG_CMD_SNTP) && defined(CONFIG_BOOTP_NTPSERVER) case 42: /* NTP server IP */ - NetCopyIP (&NetNtpServerIP, (popt + 2)); + NetCopyIP(&NetNtpServerIP, (popt + 2)); break; #endif case 51: - NetCopyLong (&dhcp_leasetime, (ulong *) (popt + 2)); + NetCopyLong(&dhcp_leasetime, (ulong *) (popt + 2)); break; case 53: /* Ignore Message Type Option */ break; case 54: - NetCopyIP (&NetDHCPServerIP, (popt + 2)); + NetCopyIP(&NetDHCPServerIP, (popt + 2)); break; case 58: /* Ignore Renewal Time Option */ break; @@ -790,7 +788,7 @@ static void DhcpOptionsProcess (uchar * popt, Bootp_t *bp) * pass the bootp packet pointer into here as the * second arg */ - size = truncate_sz ("Opt Boot File", + size = truncate_sz("Opt Boot File", sizeof(bp->bp_file), oplen); if (bp->bp_file[0] == '\0' && size > 0) { @@ -811,10 +809,11 @@ static void DhcpOptionsProcess (uchar * popt, Bootp_t *bp) break; default: #if defined(CONFIG_BOOTP_VENDOREX) - if (dhcp_vendorex_proc (popt)) + if (dhcp_vendorex_proc(popt)) break; #endif - printf ("*** Unhandled DHCP Option in OFFER/ACK: %d\n", *popt); + printf("*** Unhandled DHCP Option in OFFER/ACK:" + " %d\n", *popt); break; } popt += oplen + 2; /* Process next option */ @@ -823,42 +822,42 @@ static void DhcpOptionsProcess (uchar * popt, Bootp_t *bp)
static int DhcpMessageType(unsigned char *popt) { - if (NetReadLong((ulong*)popt) != htonl(BOOTP_VENDOR_MAGIC)) + if (NetReadLong((ulong *)popt) != htonl(BOOTP_VENDOR_MAGIC)) return -1;
popt += 4; - while ( *popt != 0xff ) { - if ( *popt == 53 ) /* DHCP Message Type */ + while (*popt != 0xff) { + if (*popt == 53) /* DHCP Message Type */ return *(popt + 2); popt += *(popt + 1) + 2; /* Scan through all options */ } return -1; }
-static void DhcpSendRequestPkt(Bootp_t *bp_offer) +static void DhcpSendRequestPkt(struct Bootp_t *bp_offer) { uchar *pkt, *iphdr; - Bootp_t *bp; + struct Bootp_t *bp; int pktlen, iplen, extlen; IPaddr_t OfferedIP;
debug("DhcpSendRequestPkt: Sending DHCPREQUEST\n"); pkt = NetTxPacket; - memset ((void*)pkt, 0, PKTSIZE); + memset((void *)pkt, 0, PKTSIZE);
pkt += NetSetEther(pkt, NetBcastAddr, PROT_IP);
- iphdr = pkt; /* We'll need this later to set proper pkt size */ + iphdr = pkt; /* We'll need this later to set proper pkt size */ pkt += IP_HDR_SIZE;
- bp = (Bootp_t *)pkt; + bp = (struct Bootp_t *)pkt; bp->bp_op = OP_BOOTREQUEST; bp->bp_htype = HWT_ETHER; bp->bp_hlen = HWL_ETHER; bp->bp_hops = 0; bp->bp_secs = htons(get_timer(0) / 1000); - /* Do not set the client IP, your IP, or server IP yet, since it hasn't been ACK'ed by - * the server yet */ + /* Do not set the client IP, your IP, or server IP yet, since it + * hasn't been ACK'ed by the server yet */
/* * RFC3046 requires Relay Agents to discard packets with @@ -866,7 +865,7 @@ static void DhcpSendRequestPkt(Bootp_t *bp_offer) */ NetWriteIP(&bp->bp_giaddr, 0);
- memcpy (bp->bp_chaddr, NetOurEther, 6); + memcpy(bp->bp_chaddr, NetOurEther, 6);
/* * ID is the id of the OFFER packet @@ -880,9 +879,11 @@ static void DhcpSendRequestPkt(Bootp_t *bp_offer)
/* Copy offered IP into the parameters request list */ NetCopyIP(&OfferedIP, &bp_offer->bp_yiaddr); - extlen = DhcpExtended((u8 *)bp->bp_vend, DHCP_REQUEST, NetDHCPServerIP, OfferedIP); + extlen = DhcpExtended((u8 *)bp->bp_vend, DHCP_REQUEST, + NetDHCPServerIP, OfferedIP);
- pktlen = ((int)(pkt-NetTxPacket)) + BOOTP_HDR_SIZE - sizeof(bp->bp_vend) + extlen; + pktlen = ((int)(pkt-NetTxPacket)) + BOOTP_HDR_SIZE - + sizeof(bp->bp_vend) + extlen; iplen = BOOTP_HDR_SIZE - sizeof(bp->bp_vend) + extlen; NetSetIP(iphdr, 0xFFFFFFFFL, PORT_BOOTPS, PORT_BOOTPC, iplen);
@@ -900,36 +901,38 @@ static void DhcpHandler(uchar *pkt, unsigned dest, IPaddr_t sip, unsigned src, unsigned len) { - Bootp_t *bp = (Bootp_t *)pkt; + struct Bootp_t *bp = (struct Bootp_t *)pkt;
debug("DHCPHandler: got packet: (src=%d, dst=%d, len=%d) state: %d\n", src, dest, len, dhcp_state);
- if (BootpCheckPkt(pkt, dest, src, len)) /* Filter out pkts we don't want */ + /* Filter out pkts we don't want */ + if (BootpCheckPkt(pkt, dest, src, len)) return;
- debug("DHCPHandler: got DHCP packet: (src=%d, dst=%d, len=%d) state: %d\n", - src, dest, len, dhcp_state); + debug("DHCPHandler: got DHCP packet: (src=%d, dst=%d, len=%d) state:" + " %d\n", src, dest, len, dhcp_state);
switch (dhcp_state) { case SELECTING: /* * Wait an appropriate time for any potential DHCPOFFER packets - * to arrive. Then select one, and generate DHCPREQUEST response. - * If filename is in format we recognize, assume it is a valid - * OFFER from a server we want. + * to arrive. Then select one, and generate DHCPREQUEST + * response. If filename is in format we recognize, assume it + * is a valid OFFER from a server we want. */ debug("DHCP: state=SELECTING bp_file: "%s"\n", bp->bp_file); #ifdef CONFIG_SYS_BOOTFILE_PREFIX if (strncmp(bp->bp_file, CONFIG_SYS_BOOTFILE_PREFIX, - strlen(CONFIG_SYS_BOOTFILE_PREFIX)) == 0 ) { + strlen(CONFIG_SYS_BOOTFILE_PREFIX)) == 0) { #endif /* CONFIG_SYS_BOOTFILE_PREFIX */
debug("TRANSITIONING TO REQUESTING STATE\n"); dhcp_state = REQUESTING;
- if (NetReadLong((ulong*)&bp->bp_vend[0]) == htonl(BOOTP_VENDOR_MAGIC)) + if (NetReadLong((ulong *)&bp->bp_vend[0]) == + htonl(BOOTP_VENDOR_MAGIC)) DhcpOptionsProcess((u8 *)&bp->bp_vend[4], bp);
NetSetTimeout(TIMEOUT, BootpTimeout); @@ -943,12 +946,15 @@ DhcpHandler(uchar *pkt, unsigned dest, IPaddr_t sip, unsigned src, case REQUESTING: debug("DHCP State: REQUESTING\n");
- if ( DhcpMessageType((u8 *)bp->bp_vend) == DHCP_ACK ) { - if (NetReadLong((ulong*)&bp->bp_vend[0]) == htonl(BOOTP_VENDOR_MAGIC)) + if (DhcpMessageType((u8 *)bp->bp_vend) == DHCP_ACK) { + if (NetReadLong((ulong *)&bp->bp_vend[0]) == + htonl(BOOTP_VENDOR_MAGIC)) DhcpOptionsProcess((u8 *)&bp->bp_vend[4], bp); - BootpCopyNetParams(bp); /* Store net params from reply */ + /* Store net params from reply */ + BootpCopyNetParams(bp); dhcp_state = BOUND; - printf ("DHCP client bound to address %pI4\n", &NetOurIP); + printf("DHCP client bound to address %pI4\n", + &NetOurIP);
net_auto_load(); return; @@ -958,7 +964,7 @@ DhcpHandler(uchar *pkt, unsigned dest, IPaddr_t sip, unsigned src, /* DHCP client bound to address */ break; default: - puts ("DHCP: INVALID STATE\n"); + puts("DHCP: INVALID STATE\n"); break; }
diff --git a/net/bootp.h b/net/bootp.h index 50625ab..ce73734 100644 --- a/net/bootp.h +++ b/net/bootp.h @@ -10,7 +10,7 @@ #define __BOOTP_H__
#ifndef __NET_H__ -#include <net.h> +#include <net.h> #endif /* __NET_H__ */
/**********************************************************************/ @@ -19,35 +19,39 @@ * BOOTP header. */ #if defined(CONFIG_CMD_DHCP) -#define OPT_SIZE 312 /* Minimum DHCP Options size per RFC2131 - results in 576 byte pkt */ +/* Minimum DHCP Options size per RFC2131 - results in 576 byte pkt */ +#define OPT_SIZE 312 +#if defined(CONFIG_BOOTP_VENDOREX) +extern u8 *dhcp_vendorex_prep(u8 *e); /*rtn new e after add own opts. */ +extern u8 *dhcp_vendorex_proc(u8 *e); /*rtn next e if mine,else NULL */ +#endif #else #define OPT_SIZE 64 #endif
-typedef struct -{ - uchar bp_op; /* Operation */ +struct Bootp_t { + uchar bp_op; /* Operation */ # define OP_BOOTREQUEST 1 # define OP_BOOTREPLY 2 - uchar bp_htype; /* Hardware type */ + uchar bp_htype; /* Hardware type */ # define HWT_ETHER 1 - uchar bp_hlen; /* Hardware address length */ + uchar bp_hlen; /* Hardware address length */ # define HWL_ETHER 6 - uchar bp_hops; /* Hop count (gateway thing) */ - ulong bp_id; /* Transaction ID */ - ushort bp_secs; /* Seconds since boot */ - ushort bp_spare1; /* Alignment */ - IPaddr_t bp_ciaddr; /* Client IP address */ - IPaddr_t bp_yiaddr; /* Your (client) IP address */ - IPaddr_t bp_siaddr; /* Server IP address */ - IPaddr_t bp_giaddr; /* Gateway IP address */ - uchar bp_chaddr[16]; /* Client hardware address */ - char bp_sname[64]; /* Server host name */ - char bp_file[128]; /* Boot file name */ - char bp_vend[OPT_SIZE]; /* Vendor information */ -} Bootp_t; - -#define BOOTP_HDR_SIZE sizeof (Bootp_t) + uchar bp_hops; /* Hop count (gateway thing) */ + ulong bp_id; /* Transaction ID */ + ushort bp_secs; /* Seconds since boot */ + ushort bp_spare1; /* Alignment */ + IPaddr_t bp_ciaddr; /* Client IP address */ + IPaddr_t bp_yiaddr; /* Your (client) IP address */ + IPaddr_t bp_siaddr; /* Server IP address */ + IPaddr_t bp_giaddr; /* Gateway IP address */ + uchar bp_chaddr[16]; /* Client hardware address */ + char bp_sname[64]; /* Server host name */ + char bp_file[128]; /* Boot file name */ + char bp_vend[OPT_SIZE]; /* Vendor information */ +}; + +#define BOOTP_HDR_SIZE sizeof(struct Bootp_t) #define BOOTP_SIZE (ETHER_HDR_SIZE + IP_HDR_SIZE + BOOTP_HDR_SIZE)
/**********************************************************************/ @@ -56,16 +60,16 @@ typedef struct */
/* bootp.c */ -extern ulong BootpID; /* ID of cur BOOTP request */ -extern char BootFile[128]; /* Boot file name */ +extern ulong BootpID; /* ID of cur BOOTP request */ +extern char BootFile[128]; /* Boot file name */ extern int BootpTry; #ifdef CONFIG_BOOTP_RANDOM_DELAY -extern ulong seed1, seed2; /* seed for random BOOTP delay */ +extern ulong seed1, seed2; /* seed for random BOOTP delay */ #endif
/* Send a BOOTP request */ -extern void BootpRequest (void); +extern void BootpRequest(void);
/****************** DHCP Support *********************/ extern void DhcpRequest(void); diff --git a/net/eth.c b/net/eth.c index b4b9b43..02ccc75 100644 --- a/net/eth.c +++ b/net/eth.c @@ -92,7 +92,7 @@ static struct { int length; } eth_rcv_bufs[PKTBUFSRX];
-static unsigned int eth_rcv_current = 0, eth_rcv_last = 0; +static unsigned int eth_rcv_current, eth_rcv_last; #endif
static struct eth_device *eth_devices, *eth_current; @@ -144,7 +144,7 @@ struct eth_device *eth_get_dev_by_index(int index) return target_dev; }
-int eth_get_dev_index (void) +int eth_get_dev_index(void) { if (!eth_current) { return -1; @@ -180,7 +180,7 @@ int eth_write_hwaddr(struct eth_device *dev, const char *base_name,
if (memcmp(env_enetaddr, "\0\0\0\0\0\0", 6)) { if (memcmp(dev->enetaddr, "\0\0\0\0\0\0", 6) && - memcmp(dev->enetaddr, env_enetaddr, 6)) { + memcmp(dev->enetaddr, env_enetaddr, 6)) { printf("\nWarning: %s MAC addresses don't match:\n", dev->name); printf("Address in SROM is %pM\n", @@ -193,10 +193,9 @@ int eth_write_hwaddr(struct eth_device *dev, const char *base_name, }
if (dev->write_hwaddr && - !eth_mac_skip(eth_number) && - is_valid_ether_addr(dev->enetaddr)) { + !eth_mac_skip(eth_number) && + is_valid_ether_addr(dev->enetaddr)) ret = dev->write_hwaddr(dev); - }
return ret; } @@ -212,7 +211,7 @@ int eth_register(struct eth_device *dev) eth_current = eth_devices = dev; eth_current_changed(); } else { - for (d=eth_devices; d->next!=eth_devices; d=d->next) + for (d = eth_devices; d->next != eth_devices; d = d->next) ; d->next = dev; } @@ -230,7 +229,7 @@ int eth_initialize(bd_t *bis) eth_devices = NULL; eth_current = NULL;
- show_boot_progress (64); + show_boot_progress(64); #if defined(CONFIG_MII) || defined(CONFIG_CMD_MII) miiphy_init(); #endif @@ -259,36 +258,37 @@ int eth_initialize(bd_t *bis) mv6446x_eth_initialize(bis); #endif if (!eth_devices) { - puts ("No ethernet found.\n"); - show_boot_progress (-64); + puts("No ethernet found.\n"); + show_boot_progress(-64); } else { struct eth_device *dev = eth_devices; - char *ethprime = getenv ("ethprime"); + char *ethprime = getenv("ethprime");
- show_boot_progress (65); + show_boot_progress(65); do { if (dev->index) puts (", ");
printf("%s", dev->name);
- if (ethprime && strcmp (dev->name, ethprime) == 0) { + if (ethprime && strcmp(dev->name, ethprime) == 0) { eth_current = dev; - puts (" [PRIME]"); + puts(" [PRIME]"); }
if (strchr(dev->name, ' ')) - puts("\nWarning: eth device name has a space!\n"); + puts("\nWarning: eth device name has a space!" + "\n");
if (eth_write_hwaddr(dev, "eth", dev->index)) puts("\nWarning: failed to set MAC address\n");
dev = dev->next; num_devices++; - } while(dev != eth_devices); + } while (dev != eth_devices);
eth_current_changed(); - putc ('\n'); + putc('\n'); }
return num_devices; @@ -299,9 +299,9 @@ int eth_initialize(bd_t *bis) * mcast_addr: multicast ipaddr from which multicast Mac is made * join: 1=join, 0=leave. */ -int eth_mcast_join( IPaddr_t mcast_ip, u8 join) +int eth_mcast_join(IPaddr_t mcast_ip, u8 join) { - u8 mcast_mac[6]; + u8 mcast_mac[6]; if (!eth_current || !eth_current->mcast) return -1; mcast_mac[5] = htonl(mcast_ip) & 0xff; @@ -318,7 +318,7 @@ int eth_mcast_join( IPaddr_t mcast_ip, u8 join) * some other adapter -- hash tables */ #define CRCPOLY_LE 0xedb88320 -u32 ether_crc (size_t len, unsigned char const *p) +u32 ether_crc(size_t len, unsigned char const *p) { int i; u32 crc; @@ -345,7 +345,7 @@ int eth_init(bd_t *bis) struct eth_device *old_current, *dev;
if (!eth_current) { - puts ("No ethernet found.\n"); + puts("No ethernet found.\n"); return -1; }
@@ -365,7 +365,7 @@ int eth_init(bd_t *bis) do { debug("Trying %s\n", eth_current->name);
- if (eth_current->init(eth_current,bis) >= 0) { + if (eth_current->init(eth_current, bis) >= 0) { eth_current->state = ETH_STATE_ACTIVE;
return 0; @@ -453,38 +453,36 @@ int eth_receive(volatile void *packet, int length)
void eth_try_another(int first_restart) { - static struct eth_device *first_failed = NULL; + static struct eth_device *first_failed; char *ethrotate;
/* * Do not rotate between network interfaces when * 'ethrotate' variable is set to 'no'. */ - if (((ethrotate = getenv ("ethrotate")) != NULL) && - (strcmp(ethrotate, "no") == 0)) + ethrotate = getenv("ethrotate"); + if ((ethrotate != NULL) && (strcmp(ethrotate, "no") == 0)) return;
if (!eth_current) return;
- if (first_restart) { + if (first_restart) first_failed = eth_current; - }
eth_current = eth_current->next;
eth_current_changed();
- if (first_failed == eth_current) { + if (first_failed == eth_current) NetRestartWrap = 1; - } }
void eth_set_current(void) { - static char *act = NULL; - static int env_changed_id = 0; - struct eth_device* old_current; + static char *act; + static int env_changed_id; + struct eth_device *old_current; int env_id;
if (!eth_current) /* XXX no current */ @@ -507,7 +505,7 @@ void eth_set_current(void) eth_current_changed(); }
-char *eth_get_name (void) +char *eth_get_name(void) { - return (eth_current ? eth_current->name : "unknown"); + return eth_current ? eth_current->name : "unknown"; } diff --git a/net/net.c b/net/net.c index 29e018c..1d6252f 100644 --- a/net/net.c +++ b/net/net.c @@ -203,11 +203,6 @@ IPaddr_t NetNtpServerIP; int NetTimeOffset; #endif
-#ifdef CONFIG_NETCONSOLE -void NcStart(void); -int nc_input_packet(uchar *pkt, unsigned dest, unsigned src, unsigned len); -#endif - uchar PktBuf[(PKTBUFSRX+1) * PKTSIZE_ALIGN + PKTALIGN];
/* Receive packet */ @@ -526,10 +521,7 @@ restart: for (;;) { WATCHDOG_RESET(); #ifdef CONFIG_SHOW_ACTIVITY - { - extern void show_activity(int arg); - show_activity(1); - } + show_activity(1); #endif /* * Check the ethernet for a new packet. The ethernet @@ -1102,8 +1094,8 @@ CDPHandler(const uchar *pkt, unsigned len) * output a warning */ if (pkt[0] != 0x02) - printf("** WARNING: CDP packet received with a protocol version %d > 2\n", - pkt[0] & 0xff); + printf("**WARNING: CDP packet received with a protocol version " + "%d > 2\n", pkt[0] & 0xff);
if (CDP_compute_csum(pkt, len) != 0) return; @@ -1610,6 +1602,7 @@ NetReceive(volatile uchar *inpkt, int len) /* matched waiting packet's address */ if (tmp == NetArpWaitReplyIP) { debug("Got it\n"); + /* save address for later use */ memcpy(NetArpWaitPacketMAC, &arp->ar_data[0], 6); @@ -1618,7 +1611,8 @@ NetReceive(volatile uchar *inpkt, int len) (*packetHandler)(0, 0, 0, 0, 0); #endif /* modify header, and transmit it */ - memcpy(((Ethernet_t *)NetArpWaitTxPacket)->et_dest, NetArpWaitPacketMAC, 6); + memcpy(((Ethernet_t *)NetArpWaitTxPacket)-> + et_dest, NetArpWaitPacketMAC, 6); (void) eth_send(NetArpWaitTxPacket, NetArpWaitTxPacketSize);
@@ -1855,7 +1849,6 @@ common: case CDP: case DHCP: if (memcmp(NetOurEther, "\0\0\0\0\0\0", 6) == 0) { - extern int eth_get_dev_index(void); int num = eth_get_dev_index();
switch (num) { diff --git a/net/nfs.c b/net/nfs.c index b5b482c..54f56c4 100644 --- a/net/nfs.c +++ b/net/nfs.c @@ -33,8 +33,8 @@ #define NFS_RETRY_COUNT 30 #define NFS_TIMEOUT 2000UL
-static int fs_mounted = 0; -static unsigned long rpc_id = 0; +static int fs_mounted; +static unsigned long rpc_id; static int nfs_offset = -1; static int nfs_len;
@@ -61,14 +61,14 @@ static char *nfs_filename; static char *nfs_path; static char nfs_path_buff[2048];
-static __inline__ int -store_block (uchar * src, unsigned offset, unsigned len) +static inline int +store_block(uchar *src, unsigned offset, unsigned len) { ulong newsize = offset + len; #ifdef CONFIG_SYS_DIRECT_FLASH_NFS int i, rc = 0;
- for (i=0; i<CONFIG_SYS_MAX_FLASH_BANKS; i++) { + for (i = 0; i < CONFIG_SYS_MAX_FLASH_BANKS; i++) { /* start address in flash? */ if (load_addr + offset >= flash_info[i].start[0]) { rc = 1; @@ -77,15 +77,15 @@ store_block (uchar * src, unsigned offset, unsigned len) }
if (rc) { /* Flash is destination for this packet */ - rc = flash_write ((uchar *)src, (ulong)(load_addr+offset), len); + rc = flash_write((uchar *)src, (ulong)(load_addr+offset), len); if (rc) { - flash_perror (rc); + flash_perror(rc); return -1; } } else #endif /* CONFIG_SYS_DIRECT_FLASH_NFS */ { - (void)memcpy ((void *)(load_addr + offset), src, len); + (void)memcpy((void *)(load_addr + offset), src, len); }
if (NetBootFileXferSize < (offset+len)) @@ -94,7 +94,7 @@ store_block (uchar * src, unsigned offset, unsigned len) }
static char* -basename (char *path) +basename(char *path) { char *fname;
@@ -110,11 +110,11 @@ basename (char *path) }
static char* -dirname (char *path) +dirname(char *path) { char *fname;
- fname = basename (path); + fname = basename(path); --fname; *fname = '\0'; return path; @@ -123,14 +123,14 @@ dirname (char *path) /************************************************************************** RPC_ADD_CREDENTIALS - Add RPC authentication/verifier entries **************************************************************************/ -static long *rpc_add_credentials (long *p) +static long *rpc_add_credentials(long *p) { int hl; int hostnamelen; char hostname[256];
- strcpy (hostname, ""); - hostnamelen=strlen (hostname); + strcpy(hostname, ""); + hostnamelen = strlen(hostname);
/* Here's the executive summary on authentication requirements of the * various NFS server implementations: Linux accepts both AUTH_NONE @@ -148,10 +148,9 @@ static long *rpc_add_credentials (long *p) *p++ = htonl(hl+20); /* auth length */ *p++ = htonl(0); /* stamp */ *p++ = htonl(hostnamelen); /* hostname string */ - if (hostnamelen & 3) { + if (hostnamelen & 3) *(p + hostnamelen / 4) = 0; /* add zero padding */ - } - memcpy (p, hostname, hostnamelen); + memcpy(p, hostname, hostnamelen); p += hl / 4; *p++ = 0; /* uid */ *p++ = 0; /* gid */ @@ -168,7 +167,7 @@ static long *rpc_add_credentials (long *p) RPC_LOOKUP - Lookup RPC Port numbers **************************************************************************/ static void -rpc_req (int rpc_prog, int rpc_proc, uint32_t *data, int datalen) +rpc_req(int rpc_prog, int rpc_proc, uint32_t *data, int datalen) { struct rpc_t pkt; unsigned long id; @@ -186,11 +185,12 @@ rpc_req (int rpc_prog, int rpc_proc, uint32_t *data, int datalen) p = (uint32_t *)&(pkt.u.call.data);
if (datalen) - memcpy ((char *)p, (char *)data, datalen*sizeof(uint32_t)); + memcpy((char *)p, (char *)data, datalen*sizeof(uint32_t));
pktlen = (char *)p + datalen*sizeof(uint32_t) - (char *)&pkt;
- memcpy ((char *)NetTxPacket + NetEthHdrSize() + IP_HDR_SIZE, (char *)&pkt, pktlen); + memcpy((char *)NetTxPacket + NetEthHdrSize() + IP_HDR_SIZE, + (char *)&pkt, pktlen);
if (rpc_prog == PROG_PORTMAP) sport = SUNRPC_PORT; @@ -199,14 +199,15 @@ rpc_req (int rpc_prog, int rpc_proc, uint32_t *data, int datalen) else sport = NfsSrvNfsPort;
- NetSendUDPPacket (NetServerEther, NfsServerIP, sport, NfsOurPort, pktlen); + NetSendUDPPacket(NetServerEther, NfsServerIP, sport, NfsOurPort, + pktlen); }
/************************************************************************** RPC_LOOKUP - Lookup RPC Port numbers **************************************************************************/ static void -rpc_lookup_req (int prog, int ver) +rpc_lookup_req(int prog, int ver) { uint32_t data[16];
@@ -217,56 +218,56 @@ rpc_lookup_req (int prog, int ver) data[6] = htonl(17); /* IP_UDP */ data[7] = 0;
- rpc_req (PROG_PORTMAP, PORTMAP_GETPORT, data, 8); + rpc_req(PROG_PORTMAP, PORTMAP_GETPORT, data, 8); }
/************************************************************************** NFS_MOUNT - Mount an NFS Filesystem **************************************************************************/ static void -nfs_mount_req (char *path) +nfs_mount_req(char *path) { uint32_t data[1024]; uint32_t *p; int len; int pathlen;
- pathlen = strlen (path); + pathlen = strlen(path);
p = &(data[0]); p = (uint32_t *)rpc_add_credentials((long *)p);
*p++ = htonl(pathlen); - if (pathlen & 3) *(p + pathlen / 4) = 0; - memcpy (p, path, pathlen); + if (pathlen & 3) + *(p + pathlen / 4) = 0; + memcpy(p, path, pathlen); p += (pathlen + 3) / 4;
len = (uint32_t *)p - (uint32_t *)&(data[0]);
- rpc_req (PROG_MOUNT, MOUNT_ADDENTRY, data, len); + rpc_req(PROG_MOUNT, MOUNT_ADDENTRY, data, len); }
/************************************************************************** NFS_UMOUNTALL - Unmount all our NFS Filesystems on the Server **************************************************************************/ static void -nfs_umountall_req (void) +nfs_umountall_req(void) { uint32_t data[1024]; uint32_t *p; int len;
- if ((NfsSrvMountPort == -1) || (!fs_mounted)) { + if ((NfsSrvMountPort == -1) || (!fs_mounted)) /* Nothing mounted, nothing to umount */ return; - }
p = &(data[0]); - p = (uint32_t *)rpc_add_credentials ((long *)p); + p = (uint32_t *)rpc_add_credentials((long *)p);
len = (uint32_t *)p - (uint32_t *)&(data[0]);
- rpc_req (PROG_MOUNT, MOUNT_UMOUNTALL, data, len); + rpc_req(PROG_MOUNT, MOUNT_UMOUNTALL, data, len); }
/*************************************************************************** @@ -277,65 +278,66 @@ nfs_umountall_req (void) * so that inside the nfs() function a recursion can be done. **************************************************************************/ static void -nfs_readlink_req (void) +nfs_readlink_req(void) { uint32_t data[1024]; uint32_t *p; int len;
p = &(data[0]); - p = (uint32_t *)rpc_add_credentials ((long *)p); + p = (uint32_t *)rpc_add_credentials((long *)p);
- memcpy (p, filefh, NFS_FHSIZE); + memcpy(p, filefh, NFS_FHSIZE); p += (NFS_FHSIZE / 4);
len = (uint32_t *)p - (uint32_t *)&(data[0]);
- rpc_req (PROG_NFS, NFS_READLINK, data, len); + rpc_req(PROG_NFS, NFS_READLINK, data, len); }
/************************************************************************** NFS_LOOKUP - Lookup Pathname **************************************************************************/ static void -nfs_lookup_req (char *fname) +nfs_lookup_req(char *fname) { uint32_t data[1024]; uint32_t *p; int len; int fnamelen;
- fnamelen = strlen (fname); + fnamelen = strlen(fname);
p = &(data[0]); - p = (uint32_t *)rpc_add_credentials ((long *)p); + p = (uint32_t *)rpc_add_credentials((long *)p);
- memcpy (p, dirfh, NFS_FHSIZE); + memcpy(p, dirfh, NFS_FHSIZE); p += (NFS_FHSIZE / 4); *p++ = htonl(fnamelen); - if (fnamelen & 3) *(p + fnamelen / 4) = 0; - memcpy (p, fname, fnamelen); + if (fnamelen & 3) + *(p + fnamelen / 4) = 0; + memcpy(p, fname, fnamelen); p += (fnamelen + 3) / 4;
len = (uint32_t *)p - (uint32_t *)&(data[0]);
- rpc_req (PROG_NFS, NFS_LOOKUP, data, len); + rpc_req(PROG_NFS, NFS_LOOKUP, data, len); }
/************************************************************************** NFS_READ - Read File on NFS Server **************************************************************************/ static void -nfs_read_req (int offset, int readlen) +nfs_read_req(int offset, int readlen) { uint32_t data[1024]; uint32_t *p; int len;
p = &(data[0]); - p = (uint32_t *)rpc_add_credentials ((long *)p); + p = (uint32_t *)rpc_add_credentials((long *)p);
- memcpy (p, filefh, NFS_FHSIZE); + memcpy(p, filefh, NFS_FHSIZE); p += (NFS_FHSIZE / 4); *p++ = htonl(offset); *p++ = htonl(readlen); @@ -343,7 +345,7 @@ nfs_read_req (int offset, int readlen)
len = (uint32_t *)p - (uint32_t *)&(data[0]);
- rpc_req (PROG_NFS, NFS_READ, data, len); + rpc_req(PROG_NFS, NFS_READ, data, len); }
/************************************************************************** @@ -351,31 +353,31 @@ RPC request dispatcher **************************************************************************/
static void -NfsSend (void) +NfsSend(void) { debug("%s\n", __func__);
switch (NfsState) { case STATE_PRCLOOKUP_PROG_MOUNT_REQ: - rpc_lookup_req (PROG_MOUNT, 1); + rpc_lookup_req(PROG_MOUNT, 1); break; case STATE_PRCLOOKUP_PROG_NFS_REQ: - rpc_lookup_req (PROG_NFS, 2); + rpc_lookup_req(PROG_NFS, 2); break; case STATE_MOUNT_REQ: - nfs_mount_req (nfs_path); + nfs_mount_req(nfs_path); break; case STATE_UMOUNT_REQ: - nfs_umountall_req (); + nfs_umountall_req(); break; case STATE_LOOKUP_REQ: - nfs_lookup_req (nfs_filename); + nfs_lookup_req(nfs_filename); break; case STATE_READ_REQ: - nfs_read_req (nfs_offset, nfs_len); + nfs_read_req(nfs_offset, nfs_len); break; case STATE_READLINK_REQ: - nfs_readlink_req (); + nfs_readlink_req(); break; } } @@ -385,11 +387,11 @@ Handlers for the reply from server **************************************************************************/
static int -rpc_lookup_reply (int prog, uchar *pkt, unsigned len) +rpc_lookup_reply(int prog, uchar *pkt, unsigned len) { struct rpc_t rpc_pkt;
- memcpy ((unsigned char *)&rpc_pkt, pkt, len); + memcpy((unsigned char *)&rpc_pkt, pkt, len);
debug("%s\n", __func__);
@@ -398,9 +400,8 @@ rpc_lookup_reply (int prog, uchar *pkt, unsigned len)
if (rpc_pkt.u.reply.rstatus || rpc_pkt.u.reply.verifier || - rpc_pkt.u.reply.astatus) { + rpc_pkt.u.reply.astatus) return -1; - }
switch (prog) { case PROG_MOUNT: @@ -415,13 +416,13 @@ rpc_lookup_reply (int prog, uchar *pkt, unsigned len) }
static int -nfs_mount_reply (uchar *pkt, unsigned len) +nfs_mount_reply(uchar *pkt, unsigned len) { struct rpc_t rpc_pkt;
debug("%s\n", __func__);
- memcpy ((unsigned char *)&rpc_pkt, pkt, len); + memcpy((unsigned char *)&rpc_pkt, pkt, len);
if (ntohl(rpc_pkt.u.reply.id) != rpc_id) return -1; @@ -429,48 +430,46 @@ nfs_mount_reply (uchar *pkt, unsigned len) if (rpc_pkt.u.reply.rstatus || rpc_pkt.u.reply.verifier || rpc_pkt.u.reply.astatus || - rpc_pkt.u.reply.data[0]) { + rpc_pkt.u.reply.data[0]) return -1; - }
fs_mounted = 1; - memcpy (dirfh, rpc_pkt.u.reply.data + 1, NFS_FHSIZE); + memcpy(dirfh, rpc_pkt.u.reply.data + 1, NFS_FHSIZE);
return 0; }
static int -nfs_umountall_reply (uchar *pkt, unsigned len) +nfs_umountall_reply(uchar *pkt, unsigned len) { struct rpc_t rpc_pkt;
debug("%s\n", __func__);
- memcpy ((unsigned char *)&rpc_pkt, pkt, len); + memcpy((unsigned char *)&rpc_pkt, pkt, len);
if (ntohl(rpc_pkt.u.reply.id) != rpc_id) return -1;
if (rpc_pkt.u.reply.rstatus || rpc_pkt.u.reply.verifier || - rpc_pkt.u.reply.astatus) { + rpc_pkt.u.reply.astatus) return -1; - }
fs_mounted = 0; - memset (dirfh, 0, sizeof(dirfh)); + memset(dirfh, 0, sizeof(dirfh));
return 0; }
static int -nfs_lookup_reply (uchar *pkt, unsigned len) +nfs_lookup_reply(uchar *pkt, unsigned len) { struct rpc_t rpc_pkt;
debug("%s\n", __func__);
- memcpy ((unsigned char *)&rpc_pkt, pkt, len); + memcpy((unsigned char *)&rpc_pkt, pkt, len);
if (ntohl(rpc_pkt.u.reply.id) != rpc_id) return -1; @@ -478,24 +477,23 @@ nfs_lookup_reply (uchar *pkt, unsigned len) if (rpc_pkt.u.reply.rstatus || rpc_pkt.u.reply.verifier || rpc_pkt.u.reply.astatus || - rpc_pkt.u.reply.data[0]) { + rpc_pkt.u.reply.data[0]) return -1; - }
- memcpy (filefh, rpc_pkt.u.reply.data + 1, NFS_FHSIZE); + memcpy(filefh, rpc_pkt.u.reply.data + 1, NFS_FHSIZE);
return 0; }
static int -nfs_readlink_reply (uchar *pkt, unsigned len) +nfs_readlink_reply(uchar *pkt, unsigned len) { struct rpc_t rpc_pkt; int rlen;
debug("%s\n", __func__);
- memcpy ((unsigned char *)&rpc_pkt, pkt, len); + memcpy((unsigned char *)&rpc_pkt, pkt, len);
if (ntohl(rpc_pkt.u.reply.id) != rpc_id) return -1; @@ -503,34 +501,34 @@ nfs_readlink_reply (uchar *pkt, unsigned len) if (rpc_pkt.u.reply.rstatus || rpc_pkt.u.reply.verifier || rpc_pkt.u.reply.astatus || - rpc_pkt.u.reply.data[0]) { + rpc_pkt.u.reply.data[0]) return -1; - }
- rlen = ntohl (rpc_pkt.u.reply.data[1]); /* new path length */ + rlen = ntohl(rpc_pkt.u.reply.data[1]); /* new path length */
if (*((char *)&(rpc_pkt.u.reply.data[2])) != '/') { int pathlen; - strcat (nfs_path, "/"); + strcat(nfs_path, "/"); pathlen = strlen(nfs_path); - memcpy (nfs_path+pathlen, (uchar *)&(rpc_pkt.u.reply.data[2]), rlen); + memcpy(nfs_path + pathlen, (uchar *)&(rpc_pkt.u.reply.data[2]), + rlen); nfs_path[pathlen + rlen] = 0; } else { - memcpy (nfs_path, (uchar *)&(rpc_pkt.u.reply.data[2]), rlen); + memcpy(nfs_path, (uchar *)&(rpc_pkt.u.reply.data[2]), rlen); nfs_path[rlen] = 0; } return 0; }
static int -nfs_read_reply (uchar *pkt, unsigned len) +nfs_read_reply(uchar *pkt, unsigned len) { struct rpc_t rpc_pkt; int rlen;
debug("%s\n", __func__);
- memcpy ((uchar *)&rpc_pkt, pkt, sizeof(rpc_pkt.u.reply)); + memcpy((uchar *)&rpc_pkt, pkt, sizeof(rpc_pkt.u.reply));
if (ntohl(rpc_pkt.u.reply.id) != rpc_id) return -1; @@ -539,24 +537,22 @@ nfs_read_reply (uchar *pkt, unsigned len) rpc_pkt.u.reply.verifier || rpc_pkt.u.reply.astatus || rpc_pkt.u.reply.data[0]) { - if (rpc_pkt.u.reply.rstatus) { + if (rpc_pkt.u.reply.rstatus) return -9999; - } - if (rpc_pkt.u.reply.astatus) { + if (rpc_pkt.u.reply.astatus) return -9999; - } - return -ntohl(rpc_pkt.u.reply.data[0]);; + return -ntohl(rpc_pkt.u.reply.data[0]); }
- if ((nfs_offset!=0) && !((nfs_offset) % (NFS_READ_SIZE/2*10*HASHES_PER_LINE))) { - puts ("\n\t "); - } - if (!(nfs_offset % ((NFS_READ_SIZE/2)*10))) { - putc ('#'); - } + if ((nfs_offset != 0) && !((nfs_offset) % + (NFS_READ_SIZE / 2 * 10 * HASHES_PER_LINE))) + puts("\n\t "); + if (!(nfs_offset % ((NFS_READ_SIZE / 2) * 10))) + putc('#');
rlen = ntohl(rpc_pkt.u.reply.data[18]); - if ( store_block ((uchar *)pkt+sizeof(rpc_pkt.u.reply), nfs_offset, rlen) ) + if (store_block((uchar *)pkt + sizeof(rpc_pkt.u.reply), + nfs_offset, rlen)) return -9999;
return rlen; @@ -567,15 +563,15 @@ Interfaces of U-BOOT **************************************************************************/
static void -NfsTimeout (void) +NfsTimeout(void) { - if ( ++NfsTimeoutCount > NFS_RETRY_COUNT ) { - puts ("\nRetry count exceeded; starting again\n"); - NetStartAgain (); + if (++NfsTimeoutCount > NFS_RETRY_COUNT) { + puts("\nRetry count exceeded; starting again\n"); + NetStartAgain(); } else { puts("T "); - NetSetTimeout (NFS_TIMEOUT, NfsTimeout); - NfsSend (); + NetSetTimeout(NFS_TIMEOUT, NfsTimeout); + NfsSend(); } }
@@ -586,86 +582,87 @@ NfsHandler(uchar *pkt, unsigned dest, IPaddr_t sip, unsigned src, unsigned len)
debug("%s\n", __func__);
- if (dest != NfsOurPort) return; + if (dest != NfsOurPort) + return;
switch (NfsState) { case STATE_PRCLOOKUP_PROG_MOUNT_REQ: - rpc_lookup_reply (PROG_MOUNT, pkt, len); + rpc_lookup_reply(PROG_MOUNT, pkt, len); NfsState = STATE_PRCLOOKUP_PROG_NFS_REQ; - NfsSend (); + NfsSend(); break;
case STATE_PRCLOOKUP_PROG_NFS_REQ: - rpc_lookup_reply (PROG_NFS, pkt, len); + rpc_lookup_reply(PROG_NFS, pkt, len); NfsState = STATE_MOUNT_REQ; - NfsSend (); + NfsSend(); break;
case STATE_MOUNT_REQ: if (nfs_mount_reply(pkt, len)) { - puts ("*** ERROR: Cannot mount\n"); + puts("*** ERROR: Cannot mount\n"); /* just to be sure... */ NfsState = STATE_UMOUNT_REQ; - NfsSend (); + NfsSend(); } else { NfsState = STATE_LOOKUP_REQ; - NfsSend (); + NfsSend(); } break;
case STATE_UMOUNT_REQ: if (nfs_umountall_reply(pkt, len)) { - puts ("*** ERROR: Cannot umount\n"); + puts("*** ERROR: Cannot umount\n"); NetState = NETLOOP_FAIL; } else { - puts ("\ndone\n"); + puts("\ndone\n"); NetState = NfsDownloadState; } break;
case STATE_LOOKUP_REQ: if (nfs_lookup_reply(pkt, len)) { - puts ("*** ERROR: File lookup fail\n"); + puts("*** ERROR: File lookup fail\n"); NfsState = STATE_UMOUNT_REQ; - NfsSend (); + NfsSend(); } else { NfsState = STATE_READ_REQ; nfs_offset = 0; nfs_len = NFS_READ_SIZE; - NfsSend (); + NfsSend(); } break;
case STATE_READLINK_REQ: if (nfs_readlink_reply(pkt, len)) { - puts ("*** ERROR: Symlink fail\n"); + puts("*** ERROR: Symlink fail\n"); NfsState = STATE_UMOUNT_REQ; - NfsSend (); + NfsSend(); } else { debug("Symlink --> %s\n", nfs_path); - nfs_filename = basename (nfs_path); - nfs_path = dirname (nfs_path); + nfs_filename = basename(nfs_path); + nfs_path = dirname(nfs_path);
NfsState = STATE_MOUNT_REQ; - NfsSend (); + NfsSend(); } break;
case STATE_READ_REQ: - rlen = nfs_read_reply (pkt, len); - NetSetTimeout (NFS_TIMEOUT, NfsTimeout); + rlen = nfs_read_reply(pkt, len); + NetSetTimeout(NFS_TIMEOUT, NfsTimeout); if (rlen > 0) { nfs_offset += rlen; - NfsSend (); - } - else if ((rlen == -NFSERR_ISDIR)||(rlen == -NFSERR_INVAL)) { + NfsSend(); + } else if ((rlen == -NFSERR_ISDIR) || (rlen == -NFSERR_INVAL)) { /* symbolic link */ NfsState = STATE_READLINK_REQ; - NfsSend (); + NfsSend(); } else { - if ( ! rlen ) NfsDownloadState = NETLOOP_SUCCESS; + if (!rlen) + NfsDownloadState = NETLOOP_SUCCESS; NfsState = STATE_UMOUNT_REQ; - NfsSend (); + NfsSend(); } break; } @@ -673,7 +670,7 @@ NfsHandler(uchar *pkt, unsigned dest, IPaddr_t sip, unsigned src, unsigned len)
void -NfsStart (void) +NfsStart(void) { debug("%s\n", __func__); NfsDownloadState = NETLOOP_FAIL; @@ -683,7 +680,7 @@ NfsStart (void)
if (nfs_path == NULL) { NetState = NETLOOP_FAIL; - puts ("*** ERROR: Fail allocate memory\n"); + puts("*** ERROR: Fail allocate memory\n"); return; }
@@ -692,29 +689,29 @@ NfsStart (void) NetOurIP & 0xFF, (NetOurIP >> 8) & 0xFF, (NetOurIP >> 16) & 0xFF, - (NetOurIP >> 24) & 0xFF ); - strcpy (nfs_path, default_filename); + (NetOurIP >> 24) & 0xFF); + strcpy(nfs_path, default_filename);
- printf ("*** Warning: no boot file name; using '%s'\n", + printf("*** Warning: no boot file name; using '%s'\n", nfs_path); } else { - char *p=BootFile; + char *p = BootFile;
- p = strchr (p, ':'); + p = strchr(p, ':');
if (p != NULL) { - NfsServerIP = string_to_ip (BootFile); + NfsServerIP = string_to_ip(BootFile); ++p; - strcpy (nfs_path, p); + strcpy(nfs_path, p); } else { - strcpy (nfs_path, BootFile); + strcpy(nfs_path, BootFile); } }
- nfs_filename = basename (nfs_path); - nfs_path = dirname (nfs_path); + nfs_filename = basename(nfs_path); + nfs_path = dirname(nfs_path);
- printf ("Using %s device\n", eth_get_name()); + printf("Using %s device\n", eth_get_name());
printf("File transfer via NFS from server %pI4" "; our IP address is %pI4", &NfsServerIP, &NetOurIP); @@ -725,19 +722,20 @@ NfsStart (void) IPaddr_t ServerNet = NetServerIP & NetOurSubnetMask;
if (OurNet != ServerNet) - printf("; sending through gateway %pI4", &NetOurGatewayIP); + printf("; sending through gateway %pI4", + &NetOurGatewayIP); } - printf ("\nFilename '%s/%s'.", nfs_path, nfs_filename); + printf("\nFilename '%s/%s'.", nfs_path, nfs_filename);
if (NetBootFileSize) { - printf (" Size is 0x%x Bytes = ", NetBootFileSize<<9); - print_size (NetBootFileSize<<9, ""); + printf(" Size is 0x%x Bytes = ", NetBootFileSize<<9); + print_size(NetBootFileSize<<9, ""); } - printf ("\nLoad address: 0x%lx\n" + printf("\nLoad address: 0x%lx\n" "Loading: *\b", load_addr);
- NetSetTimeout (NFS_TIMEOUT, NfsTimeout); - NetSetHandler (NfsHandler); + NetSetTimeout(NFS_TIMEOUT, NfsTimeout); + NetSetHandler(NfsHandler);
NfsTimeoutCount = 0; NfsState = STATE_PRCLOOKUP_PROG_MOUNT_REQ; @@ -747,7 +745,7 @@ NfsStart (void) NfsOurPort = 1000;
/* zero out server ether in case the server ip has changed */ - memset (NetServerEther, 0, 6); + memset(NetServerEther, 0, 6);
- NfsSend (); + NfsSend(); } diff --git a/net/nfs.h b/net/nfs.h index de8a0c6..a5a1b43 100644 --- a/net/nfs.h +++ b/net/nfs.h @@ -72,7 +72,7 @@ struct rpc_t { } reply; } u; }; -extern void NfsStart (void); /* Begin NFS */ +extern void NfsStart(void); /* Begin NFS */
/**********************************************************************/ diff --git a/net/rarp.c b/net/rarp.c index 77d63e8..5a813a2 100644 --- a/net/rarp.c +++ b/net/rarp.c @@ -58,21 +58,21 @@ static void RarpTimeout(void) { if (RarpTry >= TIMEOUT_COUNT) { - puts ("\nRetry count exceeded; starting again\n"); - NetStartAgain (); + puts("\nRetry count exceeded; starting again\n"); + NetStartAgain(); } else { - NetSetTimeout (TIMEOUT, RarpTimeout); - RarpRequest (); + NetSetTimeout(TIMEOUT, RarpTimeout); + RarpRequest(); } }
void -RarpRequest (void) +RarpRequest(void) { int i; uchar *pkt; - ARP_t * rarp; + ARP_t *rarp;
printf("RARP broadcast %d\n", ++RarpTry); pkt = NetTxPacket; @@ -81,18 +81,18 @@ RarpRequest (void)
rarp = (ARP_t *)pkt;
- rarp->ar_hrd = htons (ARP_ETHER); - rarp->ar_pro = htons (PROT_IP); + rarp->ar_hrd = htons(ARP_ETHER); + rarp->ar_pro = htons(PROT_IP); rarp->ar_hln = 6; rarp->ar_pln = 4; - rarp->ar_op = htons (RARPOP_REQUEST); - memcpy (&rarp->ar_data[0], NetOurEther, 6); /* source ET addr */ - memcpy (&rarp->ar_data[6], &NetOurIP, 4); /* source IP addr */ - memcpy (&rarp->ar_data[10], NetOurEther, 6); /* dest ET addr = source ET addr ??*/ + rarp->ar_op = htons(RARPOP_REQUEST); + memcpy(&rarp->ar_data[0], NetOurEther, 6); /* source ET addr */ + memcpy(&rarp->ar_data[6], &NetOurIP, 4); /* source IP addr */ + /* dest ET addr = source ET addr ??*/ + memcpy(&rarp->ar_data[10], NetOurEther, 6); /* dest. IP addr set to broadcast */ - for (i = 0; i <= 3; i++) { + for (i = 0; i <= 3; i++) rarp->ar_data[16 + i] = 0xff; - }
NetSendPacket(NetTxPacket, (pkt - NetTxPacket) + ARP_HDR_SIZE);
diff --git a/net/rarp.h b/net/rarp.h index 0c16d46..4e92d80 100644 --- a/net/rarp.h +++ b/net/rarp.h @@ -35,9 +35,9 @@ * Global functions and variables. */
-extern int RarpTry; +extern int RarpTry;
-extern void RarpRequest (void); /* Send a RARP request */ +extern void RarpRequest(void); /* Send a RARP request */
/**********************************************************************/
diff --git a/net/sntp.c b/net/sntp.c index 82f2fe6..6dd0dbd 100644 --- a/net/sntp.c +++ b/net/sntp.c @@ -17,7 +17,7 @@ static int SntpOurPort;
static void -SntpSend (void) +SntpSend(void) { struct sntp_pkt_t pkt; int pktlen = SNTP_PACKET_LEN; @@ -25,24 +25,26 @@ SntpSend (void)
debug("%s\n", __func__);
- memset (&pkt, 0, sizeof(pkt)); + memset(&pkt, 0, sizeof(pkt));
pkt.li = NTP_LI_NOLEAP; pkt.vn = NTP_VERSION; pkt.mode = NTP_MODE_CLIENT;
- memcpy ((char *)NetTxPacket + NetEthHdrSize() + IP_HDR_SIZE, (char *)&pkt, pktlen); + memcpy((char *)NetTxPacket + NetEthHdrSize() + IP_HDR_SIZE, + (char *)&pkt, pktlen);
SntpOurPort = 10000 + (get_timer(0) % 4096); sport = NTP_SERVICE_PORT;
- NetSendUDPPacket (NetServerEther, NetNtpServerIP, sport, SntpOurPort, pktlen); + NetSendUDPPacket(NetServerEther, NetNtpServerIP, sport, SntpOurPort, + pktlen); }
static void -SntpTimeout (void) +SntpTimeout(void) { - puts ("Timeout\n"); + puts("Timeout\n"); NetState = NETLOOP_FAIL; return; } @@ -57,19 +59,20 @@ SntpHandler(uchar *pkt, unsigned dest, IPaddr_t sip, unsigned src,
debug("%s\n", __func__);
- if (dest != SntpOurPort) return; + if (dest != SntpOurPort) + return;
/* * As the RTC's used in U-Boot sepport second resolution only * we simply ignore the sub-second field. */ - memcpy (&seconds, &rpktp->transmit_timestamp, sizeof(ulong)); + memcpy(&seconds, &rpktp->transmit_timestamp, sizeof(ulong));
to_tm(ntohl(seconds) - 2208988800UL + NetTimeOffset, &tm); #if defined(CONFIG_CMD_DATE) - rtc_set (&tm); + rtc_set(&tm); #endif - printf ("Date: %4d-%02d-%02d Time: %2d:%02d:%02d\n", + printf("Date: %4d-%02d-%02d Time: %2d:%02d:%02d\n", tm.tm_year, tm.tm_mon, tm.tm_mday, tm.tm_hour, tm.tm_min, tm.tm_sec);
@@ -77,13 +80,13 @@ SntpHandler(uchar *pkt, unsigned dest, IPaddr_t sip, unsigned src, }
void -SntpStart (void) +SntpStart(void) { debug("%s\n", __func__);
- NetSetTimeout (SNTP_TIMEOUT, SntpTimeout); + NetSetTimeout(SNTP_TIMEOUT, SntpTimeout); NetSetHandler(SntpHandler); - memset (NetServerEther, 0, 6); + memset(NetServerEther, 0, 6);
- SntpSend (); + SntpSend(); } diff --git a/net/sntp.h b/net/sntp.h index 8a097bf..1d0046e 100644 --- a/net/sntp.h +++ b/net/sntp.h @@ -56,6 +56,6 @@ struct sntp_pkt_t { unsigned long long transmit_timestamp; };
-extern void SntpStart (void); /* Begin SNTP */ +extern void SntpStart(void); /* Begin SNTP */
#endif /* __SNTP_H__ */ diff --git a/net/tftp.c b/net/tftp.c index e62f229..bc7fe05 100644 --- a/net/tftp.c +++ b/net/tftp.c @@ -11,6 +11,9 @@ #include <net.h> #include "tftp.h" #include "bootp.h" +#ifdef CONFIG_SYS_DIRECT_FLASH_TFTP +#include <flash.h> +#endif
/* Well known TFTP port # */ #define WELL_KNOWN_PORT 69 @@ -112,10 +115,6 @@ static char default_filename[DEFAULT_NAME_LEN];
static char tftp_filename[MAX_LEN];
-#ifdef CONFIG_SYS_DIRECT_FLASH_TFTP -extern flash_info_t flash_info[]; -#endif - /* 512 is poor choice for ethernet, MTU is typically 1500. * Minus eth.hdrs thats 1468. Can get 2x better throughput with * almost-MTU block sizes. At least try... fall back to 512 if need be. @@ -137,7 +136,6 @@ static unsigned *Bitmap; static int PrevBitmapHole, Mapsize = MTFTP_BITMAPSIZE; static uchar ProhibitMcast, MasterClient; static uchar Multicast; -extern IPaddr_t Mcast_addr; static int Mcast_port; static ulong TftpEndingBlock; /* can get 'last' block before done..*/
@@ -157,7 +155,7 @@ mcast_cleanup(void)
#endif /* CONFIG_MCAST_TFTP */
-static __inline__ void +static inline void store_block(unsigned block, uchar *src, unsigned len) { ulong offset = block * TftpBlkSize + TftpBlockWrapOffset; @@ -182,8 +180,7 @@ store_block(unsigned block, uchar *src, unsigned len) NetState = NETLOOP_FAIL; return; } - } - else + } else #endif /* CONFIG_SYS_DIRECT_FLASH_TFTP */ { (void)memcpy((void *)(load_addr + offset), src, len); @@ -357,12 +354,14 @@ TftpSend(void) 0, TftpBlkSizeOption, 0); #ifdef CONFIG_MCAST_TFTP /* Check all preconditions before even trying the option */ - if (!ProhibitMcast - && (Bitmap = malloc(Mapsize)) - && eth_get_dev()->mcast) { - free(Bitmap); - Bitmap = NULL; - pkt += sprintf((char *)pkt, "multicast%c%c", 0, 0); + if (!ProhibitMcast) { + Bitmap = malloc(Mapsize); + if (Bitmap && eth_get_dev()->mcast) { + free(Bitmap); + Bitmap = NULL; + pkt += sprintf((char *)pkt, "multicast%c%c", + 0, 0); + } } #endif /* CONFIG_MCAST_TFTP */ len = pkt - xp; @@ -630,8 +629,7 @@ TftpHandler(uchar *pkt, unsigned dest, IPaddr_t sip, unsigned src, mcast_cleanup(); NetState = NETLOOP_SUCCESS; } - } - else + } else #endif if (len < TftpBlkSize) tftp_complete(); diff --git a/net/tftp.h b/net/tftp.h index c51aa25..18e4c9c 100644 --- a/net/tftp.h +++ b/net/tftp.h @@ -19,7 +19,7 @@ void TftpStart(enum proto_t protocol); /* Begin TFTP get/put */
#ifdef CONFIG_CMD_TFTPSRV -extern void TftpStartServer(void); /* Wait for incoming TFTP put */ +extern void TftpStartServer(void); /* Wait for incoming TFTP put */ #endif
/**********************************************************************/

Hi Joe,
On Thu, Jan 19, 2012 at 4:53 PM, Joe Hershberger joe.hershberger@ni.com wrote:
Signed-off-by: Joe Hershberger joe.hershberger@ni.com Cc: Joe Hershberger joe.hershberger@gmail.com Cc: Wolfgang Denk wd@denx.de
common/main.c | 1 - drivers/net/netconsole.c | 122 +++++++++-------- include/common.h | 4 + include/net.h | 123 +++++++++-------- net/bootp.c | 338 +++++++++++++++++++++++----------------------- net/bootp.h | 56 ++++---- net/eth.c | 66 +++++----- net/net.c | 19 +-- net/nfs.c | 300 ++++++++++++++++++++--------------------- net/nfs.h | 2 +- net/rarp.c | 28 ++-- net/rarp.h | 4 +- net/sntp.c | 31 +++-- net/sntp.h | 2 +- net/tftp.c | 30 ++--- net/tftp.h | 2 +- 16 files changed, 572 insertions(+), 556 deletions(-)
Others may fine with it, but it might be worth splitting this very long patch, perhaps split out some of the larger files or the show_activity() change.
Regards, Simon

Hi Simon,
On Fri, Jan 20, 2012 at 10:30 AM, Simon Glass sjg@chromium.org wrote:
Hi Joe,
On Thu, Jan 19, 2012 at 4:53 PM, Joe Hershberger joe.hershberger@ni.com wrote:
Signed-off-by: Joe Hershberger joe.hershberger@ni.com Cc: Joe Hershberger joe.hershberger@gmail.com Cc: Wolfgang Denk wd@denx.de
common/main.c | 1 - drivers/net/netconsole.c | 122 +++++++++-------- include/common.h | 4 + include/net.h | 123 +++++++++-------- net/bootp.c | 338 +++++++++++++++++++++++----------------------- net/bootp.h | 56 ++++---- net/eth.c | 66 +++++----- net/net.c | 19 +-- net/nfs.c | 300 ++++++++++++++++++++--------------------- net/nfs.h | 2 +- net/rarp.c | 28 ++-- net/rarp.h | 4 +- net/sntp.c | 31 +++-- net/sntp.h | 2 +- net/tftp.c | 30 ++--- net/tftp.h | 2 +- 16 files changed, 572 insertions(+), 556 deletions(-)
Others may fine with it, but it might be worth splitting this very long patch, perhaps split out some of the larger files or the show_activity() change.
I had this separated by file, but figured since the series was pretty long, it is all cosmetic (making separate patches not especially helpful), and it was under the 100kB mail limit that it would be better as a single patch. If it will be a barrier to review, I can split it up again.
Best regards, -Joe

Hi Joe,
On Fri, Jan 20, 2012 at 12:21 PM, Joe Hershberger joe.hershberger@gmail.com wrote:
Hi Simon,
On Fri, Jan 20, 2012 at 10:30 AM, Simon Glass sjg@chromium.org wrote:
Hi Joe,
On Thu, Jan 19, 2012 at 4:53 PM, Joe Hershberger joe.hershberger@ni.com wrote:
Signed-off-by: Joe Hershberger joe.hershberger@ni.com Cc: Joe Hershberger joe.hershberger@gmail.com Cc: Wolfgang Denk wd@denx.de
common/main.c | 1 - drivers/net/netconsole.c | 122 +++++++++-------- include/common.h | 4 + include/net.h | 123 +++++++++-------- net/bootp.c | 338 +++++++++++++++++++++++----------------------- net/bootp.h | 56 ++++---- net/eth.c | 66 +++++----- net/net.c | 19 +-- net/nfs.c | 300 ++++++++++++++++++++--------------------- net/nfs.h | 2 +- net/rarp.c | 28 ++-- net/rarp.h | 4 +- net/sntp.c | 31 +++-- net/sntp.h | 2 +- net/tftp.c | 30 ++--- net/tftp.h | 2 +- 16 files changed, 572 insertions(+), 556 deletions(-)
Others may fine with it, but it might be worth splitting this very long patch, perhaps split out some of the larger files or the show_activity() change.
I had this separated by file, but figured since the series was pretty long, it is all cosmetic (making separate patches not especially helpful), and it was under the 100kB mail limit that it would be better as a single patch. If it will be a barrier to review, I can split it up again.
I'm not sure if it is a barrier to review or not, but I did nod off about 15% through :-)
Regards, Simon
Best regards, -Joe

On 20/01/2012 01:53, Joe Hershberger wrote:
Signed-off-by: Joe Hershberger joe.hershberger@ni.com Cc: Joe Hershberger joe.hershberger@gmail.com Cc: Wolfgang Denk wd@denx.de
Hi Joe,
as far as I understand this patch has nothing to do with the main feature (link-local addressing). Maybe is it worth to remove it from this patchset and send it in a separate patch ?
I see the patch address only code styling, but it is quite long for a review. IMHO it is better to have a separate patchset for this.
Best regards, Stefano Babic

i hope you ran a script on this and didn't do it by hand :). can you post the commands that you ran ? -mike

Clears up checkpatch.pl complaint about extern in c file Completes the effort started by Ben Warren in 2008 to generalize eth init
Signed-off-by: Joe Hershberger joe.hershberger@ni.com Cc: Joe Hershberger joe.hershberger@gmail.com Cc: Wolfgang Denk wd@denx.de --- board/Marvell/db64360/db64360.c | 12 ++++++- board/Marvell/db64360/mv_eth.c | 76 +++++++++++++++++++++++++++++++++++---- board/Marvell/db64360/mv_eth.h | 63 +------------------------------- board/Marvell/db64460/db64460.c | 12 ++++++- board/Marvell/db64460/mv_eth.c | 76 +++++++++++++++++++++++++++++++++++---- board/Marvell/db64460/mv_eth.h | 63 +------------------------------- board/esd/cpci750/cpci750.c | 6 +++ board/esd/cpci750/mv_eth.c | 76 +++++++++++++++++++++++++++++++++++---- board/esd/cpci750/mv_eth.h | 63 +------------------------------- board/prodrive/p3mx/mv_eth.c | 76 +++++++++++++++++++++++++++++++++++---- board/prodrive/p3mx/mv_eth.h | 63 +------------------------------- board/prodrive/p3mx/p3mx.c | 6 +++ net/eth.c | 9 ----- 13 files changed, 314 insertions(+), 287 deletions(-)
diff --git a/board/Marvell/db64360/db64360.c b/board/Marvell/db64360/db64360.c index 5183466..c66dc14 100644 --- a/board/Marvell/db64360/db64360.c +++ b/board/Marvell/db64360/db64360.c @@ -40,6 +40,7 @@ #include "mpsc.h" #include "i2c.h" #include "64360.h" +#include "mv_eth.h" #include "mv_regs.h"
#undef DEBUG @@ -934,5 +935,14 @@ void board_prebootm_init ()
int board_eth_init(bd_t *bis) { - return pci_eth_init(bis); + int pci_retval; + int mv_retval; + + pci_retval = pci_eth_init(bis); + mv_retval = mv6436x_eth_initialize(bis); + + if (pci_retval < 0 && mv_retval < 0) + return -1; + else + return 0; } diff --git a/board/Marvell/db64360/mv_eth.c b/board/Marvell/db64360/mv_eth.c index 550610e..c6b618f 100644 --- a/board/Marvell/db64360/mv_eth.c +++ b/board/Marvell/db64360/mv_eth.c @@ -78,6 +78,67 @@ #define MV_SET_REG_BITS(regOffset,bits) ((*((volatile unsigned int*)((INTERNAL_REG_BASE_ADDR) + (regOffset)))) |= ((unsigned int)my_cpu_to_le32(bits))) #define MV_RESET_REG_BITS(regOffset,bits) ((*((volatile unsigned int*)((INTERNAL_REG_BASE_ADDR) + (regOffset)))) &= ~((unsigned int)my_cpu_to_le32(bits)))
+/* ethernet.h API list */ + +/* Port operation control routines */ +static void eth_port_init(ETH_PORT_INFO *p_eth_port_ctrl); +static void eth_port_reset(ETH_PORT eth_port_num); +static bool eth_port_start(ETH_PORT_INFO *p_eth_port_ctrl); + + +/* Port MAC address routines */ +static void eth_port_uc_addr_set(ETH_PORT eth_port_num, + unsigned char *p_addr, + ETH_QUEUE queue); +#if 0 /* FIXME */ +static void eth_port_mc_addr(ETH_PORT eth_port_num, + unsigned char *p_addr, + ETH_QUEUE queue, + int option); +#endif + +/* PHY and MIB routines */ +static bool ethernet_phy_reset(ETH_PORT eth_port_num); + +static bool eth_port_write_smi_reg(ETH_PORT eth_port_num, + unsigned int phy_reg, + unsigned int value); + +static bool eth_port_read_smi_reg(ETH_PORT eth_port_num, + unsigned int phy_reg, + unsigned int *value); + +static void eth_clear_mib_counters(ETH_PORT eth_port_num); + +/* Port data flow control routines */ +static ETH_FUNC_RET_STATUS eth_port_send(ETH_PORT_INFO *p_eth_port_ctrl, + ETH_QUEUE tx_queue, + PKT_INFO *p_pkt_info); +static ETH_FUNC_RET_STATUS eth_tx_return_desc(ETH_PORT_INFO *p_eth_port_ctrl, + ETH_QUEUE tx_queue, + PKT_INFO *p_pkt_info); +static ETH_FUNC_RET_STATUS eth_port_receive(ETH_PORT_INFO *p_eth_port_ctrl, + ETH_QUEUE rx_queue, + PKT_INFO *p_pkt_info); +static ETH_FUNC_RET_STATUS eth_rx_return_buff(ETH_PORT_INFO *p_eth_port_ctrl, + ETH_QUEUE rx_queue, + PKT_INFO *p_pkt_info); + + +static bool ether_init_tx_desc_ring(ETH_PORT_INFO *p_eth_port_ctrl, + ETH_QUEUE tx_queue, + int tx_desc_num, + int tx_buff_size, + unsigned int tx_desc_base_addr, + unsigned int tx_buff_base_addr); + +static bool ether_init_rx_desc_ring(ETH_PORT_INFO *p_eth_port_ctrl, + ETH_QUEUE rx_queue, + int rx_desc_num, + int rx_buff_size, + unsigned int rx_desc_base_addr, + unsigned int rx_buff_base_addr); + /* Static function declarations */ static int mv64360_eth_real_open (struct eth_device *eth); static int mv64360_eth_real_stop (struct eth_device *eth); @@ -203,7 +264,7 @@ void db64360_eth_disable (struct eth_device *dev) }
-void mv6436x_eth_initialize (bd_t * bis) +int mv6436x_eth_initialize(bd_t *bis) { struct eth_device *dev; ETH_PORT_INFO *ethernet_private; @@ -216,7 +277,7 @@ void mv6436x_eth_initialize (bd_t * bis) if (!dev) { printf ("%s: mv_enet%d allocation failure, %s\n", __FUNCTION__, devnum, "eth_device structure"); - return; + return -1; }
/* must be less than NAMESIZE (16) */ @@ -243,7 +304,7 @@ void mv6436x_eth_initialize (bd_t * bis) default: /* this should never happen */ printf ("%s: Invalid device number %d\n", __FUNCTION__, devnum); - return; + return -1; }
temp = getenv_f(s, buf, sizeof (buf)); @@ -273,7 +334,7 @@ void mv6436x_eth_initialize (bd_t * bis) __FUNCTION__, dev->name, "Private Device Structure"); free (dev); - return; + return -1; } /* start with an zeroed ETH_PORT_INFO */ memset (ethernet_private, 0, sizeof (ETH_PORT_INFO)); @@ -289,7 +350,7 @@ void mv6436x_eth_initialize (bd_t * bis)
free (ethernet_private); free (dev); - return; + return -1; }
port_private->stats = @@ -302,7 +363,7 @@ void mv6436x_eth_initialize (bd_t * bis) free (port_private); free (ethernet_private); free (dev); - return; + return -1; } memset (ethernet_private->port_private, 0, sizeof (struct mv64360_eth_priv)); @@ -346,7 +407,7 @@ void mv6436x_eth_initialize (bd_t * bis) default: /* this should never happen */ printf ("%s: Invalid device number %d\n", __FUNCTION__, devnum); - return; + return -1; }
temp = getenv_f(s, buf, sizeof (buf)); @@ -390,6 +451,7 @@ void mv6436x_eth_initialize (bd_t * bis) } DP (printf ("%s: exit\n", __FUNCTION__));
+ return 0; }
/********************************************************************** diff --git a/board/Marvell/db64360/mv_eth.h b/board/Marvell/db64360/mv_eth.h index 142910b..9b2a4da 100644 --- a/board/Marvell/db64360/mv_eth.h +++ b/board/Marvell/db64360/mv_eth.h @@ -165,6 +165,7 @@ struct mv64360_eth_priv { };
+int mv6436x_eth_initialize(bd_t *bis); int mv64360_eth_init (struct eth_device *dev); int mv64360_eth_stop (struct eth_device *dev); int mv64360_eth_start_xmit (struct eth_device*, volatile void* packet, int length); @@ -778,66 +779,4 @@ typedef struct _eth_port_ctrl
} ETH_PORT_INFO;
- -/* ethernet.h API list */ - -/* Port operation control routines */ -static void eth_port_init (ETH_PORT_INFO *p_eth_port_ctrl); -static void eth_port_reset(ETH_PORT eth_port_num); -static bool eth_port_start(ETH_PORT_INFO *p_eth_port_ctrl); - - -/* Port MAC address routines */ -static void eth_port_uc_addr_set (ETH_PORT eth_port_num, - unsigned char *p_addr, - ETH_QUEUE queue); -#if 0 /* FIXME */ -static void eth_port_mc_addr (ETH_PORT eth_port_num, - unsigned char *p_addr, - ETH_QUEUE queue, - int option); -#endif - -/* PHY and MIB routines */ -static bool ethernet_phy_reset(ETH_PORT eth_port_num); - -static bool eth_port_write_smi_reg(ETH_PORT eth_port_num, - unsigned int phy_reg, - unsigned int value); - -static bool eth_port_read_smi_reg(ETH_PORT eth_port_num, - unsigned int phy_reg, - unsigned int* value); - -static void eth_clear_mib_counters(ETH_PORT eth_port_num); - -/* Port data flow control routines */ -static ETH_FUNC_RET_STATUS eth_port_send (ETH_PORT_INFO *p_eth_port_ctrl, - ETH_QUEUE tx_queue, - PKT_INFO *p_pkt_info); -static ETH_FUNC_RET_STATUS eth_tx_return_desc(ETH_PORT_INFO *p_eth_port_ctrl, - ETH_QUEUE tx_queue, - PKT_INFO *p_pkt_info); -static ETH_FUNC_RET_STATUS eth_port_receive (ETH_PORT_INFO *p_eth_port_ctrl, - ETH_QUEUE rx_queue, - PKT_INFO *p_pkt_info); -static ETH_FUNC_RET_STATUS eth_rx_return_buff(ETH_PORT_INFO *p_eth_port_ctrl, - ETH_QUEUE rx_queue, - PKT_INFO *p_pkt_info); - - -static bool ether_init_tx_desc_ring(ETH_PORT_INFO *p_eth_port_ctrl, - ETH_QUEUE tx_queue, - int tx_desc_num, - int tx_buff_size, - unsigned int tx_desc_base_addr, - unsigned int tx_buff_base_addr); - -static bool ether_init_rx_desc_ring(ETH_PORT_INFO *p_eth_port_ctrl, - ETH_QUEUE rx_queue, - int rx_desc_num, - int rx_buff_size, - unsigned int rx_desc_base_addr, - unsigned int rx_buff_base_addr); - #endif /* MV64360_ETH_ */ diff --git a/board/Marvell/db64460/db64460.c b/board/Marvell/db64460/db64460.c index a7836ed..de201b5 100644 --- a/board/Marvell/db64460/db64460.c +++ b/board/Marvell/db64460/db64460.c @@ -40,6 +40,7 @@ #include "mpsc.h" #include "i2c.h" #include "64460.h" +#include "mv_eth.h" #include "mv_regs.h"
#undef DEBUG @@ -934,5 +935,14 @@ void board_prebootm_init ()
int board_eth_init(bd_t *bis) { - return pci_eth_init(bis); + int pci_retval; + int mv_retval; + + pci_retval = pci_eth_init(bis); + mv_retval = mv6446x_eth_initialize(bis); + + if (pci_retval < 0 && mv_retval < 0) + return -1; + else + return 0; } diff --git a/board/Marvell/db64460/mv_eth.c b/board/Marvell/db64460/mv_eth.c index c447229..2630b85 100644 --- a/board/Marvell/db64460/mv_eth.c +++ b/board/Marvell/db64460/mv_eth.c @@ -78,6 +78,67 @@ #define MV_SET_REG_BITS(regOffset,bits) ((*((volatile unsigned int*)((INTERNAL_REG_BASE_ADDR) + (regOffset)))) |= ((unsigned int)my_cpu_to_le32(bits))) #define MV_RESET_REG_BITS(regOffset,bits) ((*((volatile unsigned int*)((INTERNAL_REG_BASE_ADDR) + (regOffset)))) &= ~((unsigned int)my_cpu_to_le32(bits)))
+/* ethernet.h API list */ + +/* Port operation control routines */ +static void eth_port_init(ETH_PORT_INFO *p_eth_port_ctrl); +static void eth_port_reset(ETH_PORT eth_port_num); +static bool eth_port_start(ETH_PORT_INFO *p_eth_port_ctrl); + + +/* Port MAC address routines */ +static void eth_port_uc_addr_set(ETH_PORT eth_port_num, + unsigned char *p_addr, + ETH_QUEUE queue); +#if 0 /* FIXME */ +static void eth_port_mc_addr(ETH_PORT eth_port_num, + unsigned char *p_addr, + ETH_QUEUE queue, + int option); +#endif + +/* PHY and MIB routines */ +static bool ethernet_phy_reset(ETH_PORT eth_port_num); + +static bool eth_port_write_smi_reg(ETH_PORT eth_port_num, + unsigned int phy_reg, + unsigned int value); + +static bool eth_port_read_smi_reg(ETH_PORT eth_port_num, + unsigned int phy_reg, + unsigned int *value); + +static void eth_clear_mib_counters(ETH_PORT eth_port_num); + +/* Port data flow control routines */ +static ETH_FUNC_RET_STATUS eth_port_send(ETH_PORT_INFO *p_eth_port_ctrl, + ETH_QUEUE tx_queue, + PKT_INFO *p_pkt_info); +static ETH_FUNC_RET_STATUS eth_tx_return_desc(ETH_PORT_INFO *p_eth_port_ctrl, + ETH_QUEUE tx_queue, + PKT_INFO *p_pkt_info); +static ETH_FUNC_RET_STATUS eth_port_receive(ETH_PORT_INFO *p_eth_port_ctrl, + ETH_QUEUE rx_queue, + PKT_INFO *p_pkt_info); +static ETH_FUNC_RET_STATUS eth_rx_return_buff(ETH_PORT_INFO *p_eth_port_ctrl, + ETH_QUEUE rx_queue, + PKT_INFO *p_pkt_info); + + +static bool ether_init_tx_desc_ring(ETH_PORT_INFO *p_eth_port_ctrl, + ETH_QUEUE tx_queue, + int tx_desc_num, + int tx_buff_size, + unsigned int tx_desc_base_addr, + unsigned int tx_buff_base_addr); + +static bool ether_init_rx_desc_ring(ETH_PORT_INFO *p_eth_port_ctrl, + ETH_QUEUE rx_queue, + int rx_desc_num, + int rx_buff_size, + unsigned int rx_desc_base_addr, + unsigned int rx_buff_base_addr); + /* Static function declarations */ static int mv64460_eth_real_open (struct eth_device *eth); static int mv64460_eth_real_stop (struct eth_device *eth); @@ -203,7 +264,7 @@ void db64460_eth_disable (struct eth_device *dev) }
-void mv6446x_eth_initialize (bd_t * bis) +int mv6446x_eth_initialize(bd_t *bis) { struct eth_device *dev; ETH_PORT_INFO *ethernet_private; @@ -216,7 +277,7 @@ void mv6446x_eth_initialize (bd_t * bis) if (!dev) { printf ("%s: mv_enet%d allocation failure, %s\n", __FUNCTION__, devnum, "eth_device structure"); - return; + return -1; }
/* must be less than NAMESIZE (16) */ @@ -243,7 +304,7 @@ void mv6446x_eth_initialize (bd_t * bis) default: /* this should never happen */ printf ("%s: Invalid device number %d\n", __FUNCTION__, devnum); - return; + return -1; }
temp = getenv_f(s, buf, sizeof (buf)); @@ -272,7 +333,7 @@ void mv6446x_eth_initialize (bd_t * bis) __FUNCTION__, dev->name, "Private Device Structure"); free (dev); - return; + return -1; } /* start with an zeroed ETH_PORT_INFO */ memset (ethernet_private, 0, sizeof (ETH_PORT_INFO)); @@ -288,7 +349,7 @@ void mv6446x_eth_initialize (bd_t * bis)
free (ethernet_private); free (dev); - return; + return -1; }
port_private->stats = @@ -301,7 +362,7 @@ void mv6446x_eth_initialize (bd_t * bis) free (port_private); free (ethernet_private); free (dev); - return; + return -1; } memset (ethernet_private->port_private, 0, sizeof (struct mv64460_eth_priv)); @@ -345,7 +406,7 @@ void mv6446x_eth_initialize (bd_t * bis) default: /* this should never happen */ printf ("%s: Invalid device number %d\n", __FUNCTION__, devnum); - return; + return -1; }
temp = getenv_f(s, buf, sizeof (buf)); @@ -389,6 +450,7 @@ void mv6446x_eth_initialize (bd_t * bis) } DP (printf ("%s: exit\n", __FUNCTION__));
+ return 0; }
/********************************************************************** diff --git a/board/Marvell/db64460/mv_eth.h b/board/Marvell/db64460/mv_eth.h index cbe751a..61d71a2 100644 --- a/board/Marvell/db64460/mv_eth.h +++ b/board/Marvell/db64460/mv_eth.h @@ -164,6 +164,7 @@ struct mv64460_eth_priv {
};
+int mv6446x_eth_initialize(bd_t *bis); int mv64460_eth_init (struct eth_device *dev); int mv64460_eth_stop (struct eth_device *dev); int mv64460_eth_start_xmit (struct eth_device*, volatile void* packet, int length); @@ -775,66 +776,4 @@ typedef struct _eth_port_ctrl
} ETH_PORT_INFO;
- -/* ethernet.h API list */ - -/* Port operation control routines */ -static void eth_port_init (ETH_PORT_INFO *p_eth_port_ctrl); -static void eth_port_reset(ETH_PORT eth_port_num); -static bool eth_port_start(ETH_PORT_INFO *p_eth_port_ctrl); - - -/* Port MAC address routines */ -static void eth_port_uc_addr_set (ETH_PORT eth_port_num, - unsigned char *p_addr, - ETH_QUEUE queue); -#if 0 /* FIXME */ -static void eth_port_mc_addr (ETH_PORT eth_port_num, - unsigned char *p_addr, - ETH_QUEUE queue, - int option); -#endif - -/* PHY and MIB routines */ -static bool ethernet_phy_reset(ETH_PORT eth_port_num); - -static bool eth_port_write_smi_reg(ETH_PORT eth_port_num, - unsigned int phy_reg, - unsigned int value); - -static bool eth_port_read_smi_reg(ETH_PORT eth_port_num, - unsigned int phy_reg, - unsigned int* value); - -static void eth_clear_mib_counters(ETH_PORT eth_port_num); - -/* Port data flow control routines */ -static ETH_FUNC_RET_STATUS eth_port_send (ETH_PORT_INFO *p_eth_port_ctrl, - ETH_QUEUE tx_queue, - PKT_INFO *p_pkt_info); -static ETH_FUNC_RET_STATUS eth_tx_return_desc(ETH_PORT_INFO *p_eth_port_ctrl, - ETH_QUEUE tx_queue, - PKT_INFO *p_pkt_info); -static ETH_FUNC_RET_STATUS eth_port_receive (ETH_PORT_INFO *p_eth_port_ctrl, - ETH_QUEUE rx_queue, - PKT_INFO *p_pkt_info); -static ETH_FUNC_RET_STATUS eth_rx_return_buff(ETH_PORT_INFO *p_eth_port_ctrl, - ETH_QUEUE rx_queue, - PKT_INFO *p_pkt_info); - - -static bool ether_init_tx_desc_ring(ETH_PORT_INFO *p_eth_port_ctrl, - ETH_QUEUE tx_queue, - int tx_desc_num, - int tx_buff_size, - unsigned int tx_desc_base_addr, - unsigned int tx_buff_base_addr); - -static bool ether_init_rx_desc_ring(ETH_PORT_INFO *p_eth_port_ctrl, - ETH_QUEUE rx_queue, - int rx_desc_num, - int rx_buff_size, - unsigned int rx_desc_base_addr, - unsigned int rx_buff_base_addr); - #endif /* MV64460_ETH_ */ diff --git a/board/esd/cpci750/cpci750.c b/board/esd/cpci750/cpci750.c index 08311c9..904eff7 100644 --- a/board/esd/cpci750/cpci750.c +++ b/board/esd/cpci750/cpci750.c @@ -40,6 +40,7 @@ #include "mpsc.h" #include "i2c.h" #include "64360.h" +#include "mv_eth.h" #include "mv_regs.h"
#undef DEBUG @@ -148,6 +149,11 @@ int board_video_skip(void) return CPCI750_SLAVE_TEST; }
+int board_eth_init(bd_t *bis) +{ + return mv6436x_eth_initialize(bis); +} + /* ------------------------------------------------------------------------- */
/* diff --git a/board/esd/cpci750/mv_eth.c b/board/esd/cpci750/mv_eth.c index db2324b..22bf9b5 100644 --- a/board/esd/cpci750/mv_eth.c +++ b/board/esd/cpci750/mv_eth.c @@ -78,6 +78,67 @@ #define MV_SET_REG_BITS(regOffset,bits) ((*((volatile unsigned int*)((INTERNAL_REG_BASE_ADDR) + (regOffset)))) |= ((unsigned int)my_cpu_to_le32(bits))) #define MV_RESET_REG_BITS(regOffset,bits) ((*((volatile unsigned int*)((INTERNAL_REG_BASE_ADDR) + (regOffset)))) &= ~((unsigned int)my_cpu_to_le32(bits)))
+/* ethernet.h API list */ + +/* Port operation control routines */ +static void eth_port_init(ETH_PORT_INFO *p_eth_port_ctrl); +static void eth_port_reset(ETH_PORT eth_port_num); +static bool eth_port_start(ETH_PORT_INFO *p_eth_port_ctrl); + + +/* Port MAC address routines */ +static void eth_port_uc_addr_set(ETH_PORT eth_port_num, + unsigned char *p_addr, + ETH_QUEUE queue); +#if 0 /* FIXME */ +static void eth_port_mc_addr(ETH_PORT eth_port_num, + unsigned char *p_addr, + ETH_QUEUE queue, + int option); +#endif + +/* PHY and MIB routines */ +static bool ethernet_phy_reset(ETH_PORT eth_port_num); + +static bool eth_port_write_smi_reg(ETH_PORT eth_port_num, + unsigned int phy_reg, + unsigned int value); + +static bool eth_port_read_smi_reg(ETH_PORT eth_port_num, + unsigned int phy_reg, + unsigned int *value); + +static void eth_clear_mib_counters(ETH_PORT eth_port_num); + +/* Port data flow control routines */ +static ETH_FUNC_RET_STATUS eth_port_send(ETH_PORT_INFO *p_eth_port_ctrl, + ETH_QUEUE tx_queue, + PKT_INFO *p_pkt_info); +static ETH_FUNC_RET_STATUS eth_tx_return_desc(ETH_PORT_INFO *p_eth_port_ctrl, + ETH_QUEUE tx_queue, + PKT_INFO *p_pkt_info); +static ETH_FUNC_RET_STATUS eth_port_receive(ETH_PORT_INFO *p_eth_port_ctrl, + ETH_QUEUE rx_queue, + PKT_INFO *p_pkt_info); +static ETH_FUNC_RET_STATUS eth_rx_return_buff(ETH_PORT_INFO *p_eth_port_ctrl, + ETH_QUEUE rx_queue, + PKT_INFO *p_pkt_info); + + +static bool ether_init_tx_desc_ring(ETH_PORT_INFO *p_eth_port_ctrl, + ETH_QUEUE tx_queue, + int tx_desc_num, + int tx_buff_size, + unsigned int tx_desc_base_addr, + unsigned int tx_buff_base_addr); + +static bool ether_init_rx_desc_ring(ETH_PORT_INFO *p_eth_port_ctrl, + ETH_QUEUE rx_queue, + int rx_desc_num, + int rx_buff_size, + unsigned int rx_desc_base_addr, + unsigned int rx_buff_base_addr); + /* Static function declarations */ static int mv64360_eth_real_open (struct eth_device *eth); static int mv64360_eth_real_stop (struct eth_device *eth); @@ -203,7 +264,7 @@ void db64360_eth_disable (struct eth_device *dev) }
-void mv6436x_eth_initialize (bd_t * bis) +int mv6436x_eth_initialize(bd_t *bis) { struct eth_device *dev; ETH_PORT_INFO *ethernet_private; @@ -216,7 +277,7 @@ void mv6436x_eth_initialize (bd_t * bis) if (!dev) { printf ("%s: mv_enet%d allocation failure, %s\n", __FUNCTION__, devnum, "eth_device structure"); - return; + return -1; }
/* must be less than NAMESIZE (16) */ @@ -243,7 +304,7 @@ void mv6436x_eth_initialize (bd_t * bis) default: /* this should never happen */ printf ("%s: Invalid device number %d\n", __FUNCTION__, devnum); - return; + return -1; }
temp = getenv_f(s, buf, sizeof (buf)); @@ -273,7 +334,7 @@ void mv6436x_eth_initialize (bd_t * bis) __FUNCTION__, dev->name, "Private Device Structure"); free (dev); - return; + return -1; } /* start with an zeroed ETH_PORT_INFO */ memset (ethernet_private, 0, sizeof (ETH_PORT_INFO)); @@ -290,7 +351,7 @@ void mv6436x_eth_initialize (bd_t * bis)
free (ethernet_private); free (dev); - return; + return -1; }
port_private->stats = @@ -303,7 +364,7 @@ void mv6436x_eth_initialize (bd_t * bis) free (port_private); free (ethernet_private); free (dev); - return; + return -1; } memset (ethernet_private->port_private, 0, sizeof (struct mv64360_eth_priv)); @@ -347,7 +408,7 @@ void mv6436x_eth_initialize (bd_t * bis) default: /* this should never happen */ printf ("%s: Invalid device number %d\n", __FUNCTION__, devnum); - return; + return -1; }
temp = getenv_f(s, buf, sizeof (buf)); @@ -391,6 +452,7 @@ void mv6436x_eth_initialize (bd_t * bis) } DP (printf ("%s: exit\n", __FUNCTION__));
+ return 0; }
/********************************************************************** diff --git a/board/esd/cpci750/mv_eth.h b/board/esd/cpci750/mv_eth.h index b761135..6dfe807 100644 --- a/board/esd/cpci750/mv_eth.h +++ b/board/esd/cpci750/mv_eth.h @@ -166,6 +166,7 @@ struct mv64360_eth_priv { };
+int mv6436x_eth_initialize(bd_t *bis); int mv64360_eth_init (struct eth_device *dev); int mv64360_eth_stop (struct eth_device *dev); int mv64360_eth_start_xmit (struct eth_device*, volatile void* packet, int length); @@ -779,66 +780,4 @@ typedef struct _eth_port_ctrl
} ETH_PORT_INFO;
- -/* ethernet.h API list */ - -/* Port operation control routines */ -static void eth_port_init (ETH_PORT_INFO *p_eth_port_ctrl); -static void eth_port_reset(ETH_PORT eth_port_num); -static bool eth_port_start(ETH_PORT_INFO *p_eth_port_ctrl); - - -/* Port MAC address routines */ -static void eth_port_uc_addr_set (ETH_PORT eth_port_num, - unsigned char *p_addr, - ETH_QUEUE queue); -#if 0 /* FIXME */ -static void eth_port_mc_addr (ETH_PORT eth_port_num, - unsigned char *p_addr, - ETH_QUEUE queue, - int option); -#endif - -/* PHY and MIB routines */ -static bool ethernet_phy_reset(ETH_PORT eth_port_num); - -static bool eth_port_write_smi_reg(ETH_PORT eth_port_num, - unsigned int phy_reg, - unsigned int value); - -static bool eth_port_read_smi_reg(ETH_PORT eth_port_num, - unsigned int phy_reg, - unsigned int* value); - -static void eth_clear_mib_counters(ETH_PORT eth_port_num); - -/* Port data flow control routines */ -static ETH_FUNC_RET_STATUS eth_port_send (ETH_PORT_INFO *p_eth_port_ctrl, - ETH_QUEUE tx_queue, - PKT_INFO *p_pkt_info); -static ETH_FUNC_RET_STATUS eth_tx_return_desc(ETH_PORT_INFO *p_eth_port_ctrl, - ETH_QUEUE tx_queue, - PKT_INFO *p_pkt_info); -static ETH_FUNC_RET_STATUS eth_port_receive (ETH_PORT_INFO *p_eth_port_ctrl, - ETH_QUEUE rx_queue, - PKT_INFO *p_pkt_info); -static ETH_FUNC_RET_STATUS eth_rx_return_buff(ETH_PORT_INFO *p_eth_port_ctrl, - ETH_QUEUE rx_queue, - PKT_INFO *p_pkt_info); - - -static bool ether_init_tx_desc_ring(ETH_PORT_INFO *p_eth_port_ctrl, - ETH_QUEUE tx_queue, - int tx_desc_num, - int tx_buff_size, - unsigned int tx_desc_base_addr, - unsigned int tx_buff_base_addr); - -static bool ether_init_rx_desc_ring(ETH_PORT_INFO *p_eth_port_ctrl, - ETH_QUEUE rx_queue, - int rx_desc_num, - int rx_buff_size, - unsigned int rx_desc_base_addr, - unsigned int rx_buff_base_addr); - #endif /* MV64360_ETH_ */ diff --git a/board/prodrive/p3mx/mv_eth.c b/board/prodrive/p3mx/mv_eth.c index f26ded8..264dc2b 100644 --- a/board/prodrive/p3mx/mv_eth.c +++ b/board/prodrive/p3mx/mv_eth.c @@ -85,6 +85,67 @@
#define my_cpu_to_le32(x) my_le32_to_cpu((x))
+/* ethernet.h API list */ + +/* Port operation control routines */ +static void eth_port_init(ETH_PORT_INFO *p_eth_port_ctrl); +static void eth_port_reset(ETH_PORT eth_port_num); +static bool eth_port_start(ETH_PORT_INFO *p_eth_port_ctrl); + + +/* Port MAC address routines */ +static void eth_port_uc_addr_set(ETH_PORT eth_port_num, + unsigned char *p_addr, + ETH_QUEUE queue); +#if 0 /* FIXME */ +static void eth_port_mc_addr(ETH_PORT eth_port_num, + unsigned char *p_addr, + ETH_QUEUE queue, + int option); +#endif + +/* PHY and MIB routines */ +static bool ethernet_phy_reset(ETH_PORT eth_port_num); + +static bool eth_port_write_smi_reg(ETH_PORT eth_port_num, + unsigned int phy_reg, + unsigned int value); + +static bool eth_port_read_smi_reg(ETH_PORT eth_port_num, + unsigned int phy_reg, + unsigned int *value); + +static void eth_clear_mib_counters(ETH_PORT eth_port_num); + +/* Port data flow control routines */ +static ETH_FUNC_RET_STATUS eth_port_send(ETH_PORT_INFO *p_eth_port_ctrl, + ETH_QUEUE tx_queue, + PKT_INFO *p_pkt_info); +static ETH_FUNC_RET_STATUS eth_tx_return_desc(ETH_PORT_INFO *p_eth_port_ctrl, + ETH_QUEUE tx_queue, + PKT_INFO *p_pkt_info); +static ETH_FUNC_RET_STATUS eth_port_receive(ETH_PORT_INFO *p_eth_port_ctrl, + ETH_QUEUE rx_queue, + PKT_INFO *p_pkt_info); +static ETH_FUNC_RET_STATUS eth_rx_return_buff(ETH_PORT_INFO *p_eth_port_ctrl, + ETH_QUEUE rx_queue, + PKT_INFO *p_pkt_info); + + +static bool ether_init_tx_desc_ring(ETH_PORT_INFO *p_eth_port_ctrl, + ETH_QUEUE tx_queue, + int tx_desc_num, + int tx_buff_size, + unsigned int tx_desc_base_addr, + unsigned int tx_buff_base_addr); + +static bool ether_init_rx_desc_ring(ETH_PORT_INFO *p_eth_port_ctrl, + ETH_QUEUE rx_queue, + int rx_desc_num, + int rx_buff_size, + unsigned int rx_desc_base_addr, + unsigned int rx_buff_base_addr); + /* Static function declarations */ static int mv64460_eth_real_open (struct eth_device *eth); static int mv64460_eth_real_stop (struct eth_device *eth); @@ -247,7 +308,7 @@ void mv_eth_phy_init (void) MV_REG_WRITE (ETH_PHY_DFCDL_CONFIG0_REG, 0x300000); }
-void mv6446x_eth_initialize (bd_t * bis) +int mv6446x_eth_initialize(bd_t *bis) { struct eth_device *dev; ETH_PORT_INFO *ethernet_private; @@ -269,7 +330,7 @@ void mv6446x_eth_initialize (bd_t * bis) if (!dev) { printf ("%s: mv_enet%d allocation failure, %s\n", __FUNCTION__, devnum, "eth_device structure"); - return; + return -1; }
/* must be less than NAMESIZE (16) */ @@ -293,7 +354,7 @@ void mv6446x_eth_initialize (bd_t * bis) default: /* this should never happen */ printf ("%s: Invalid device number %d\n", __FUNCTION__, devnum); - return; + return -1; }
temp = getenv_f(s, buf, sizeof (buf)); @@ -322,7 +383,7 @@ void mv6446x_eth_initialize (bd_t * bis) __FUNCTION__, dev->name, "Private Device Structure"); free (dev); - return; + return -1; } /* start with an zeroed ETH_PORT_INFO */ memset (ethernet_private, 0, sizeof (ETH_PORT_INFO)); @@ -338,7 +399,7 @@ void mv6446x_eth_initialize (bd_t * bis)
free (ethernet_private); free (dev); - return; + return -1; }
port_private->stats = @@ -351,7 +412,7 @@ void mv6446x_eth_initialize (bd_t * bis) free (port_private); free (ethernet_private); free (dev); - return; + return -1; } memset (ethernet_private->port_private, 0, sizeof (struct mv64460_eth_priv)); @@ -392,7 +453,7 @@ void mv6446x_eth_initialize (bd_t * bis) default: /* this should never happen */ printf ("%s: Invalid device number %d\n", __FUNCTION__, devnum); - return; + return -1; }
temp = getenv_f(s, buf, sizeof (buf)); @@ -437,6 +498,7 @@ void mv6446x_eth_initialize (bd_t * bis) } DP (printf ("%s: exit\n", __FUNCTION__));
+ return 0; }
/********************************************************************** diff --git a/board/prodrive/p3mx/mv_eth.h b/board/prodrive/p3mx/mv_eth.h index af15274..138a3af 100644 --- a/board/prodrive/p3mx/mv_eth.h +++ b/board/prodrive/p3mx/mv_eth.h @@ -164,6 +164,7 @@ struct mv64460_eth_priv { int first_init; };
+int mv6446x_eth_initialize(bd_t *bis); int mv64460_eth_init (struct eth_device *dev); int mv64460_eth_stop (struct eth_device *dev); int mv64460_eth_start_xmit (struct eth_device*, volatile void* packet, int length); @@ -775,66 +776,4 @@ typedef struct _eth_port_ctrl
} ETH_PORT_INFO;
- -/* ethernet.h API list */ - -/* Port operation control routines */ -static void eth_port_init (ETH_PORT_INFO *p_eth_port_ctrl); -static void eth_port_reset(ETH_PORT eth_port_num); -static bool eth_port_start(ETH_PORT_INFO *p_eth_port_ctrl); - - -/* Port MAC address routines */ -static void eth_port_uc_addr_set (ETH_PORT eth_port_num, - unsigned char *p_addr, - ETH_QUEUE queue); -#if 0 /* FIXME */ -static void eth_port_mc_addr (ETH_PORT eth_port_num, - unsigned char *p_addr, - ETH_QUEUE queue, - int option); -#endif - -/* PHY and MIB routines */ -static bool ethernet_phy_reset(ETH_PORT eth_port_num); - -static bool eth_port_write_smi_reg(ETH_PORT eth_port_num, - unsigned int phy_reg, - unsigned int value); - -static bool eth_port_read_smi_reg(ETH_PORT eth_port_num, - unsigned int phy_reg, - unsigned int* value); - -static void eth_clear_mib_counters(ETH_PORT eth_port_num); - -/* Port data flow control routines */ -static ETH_FUNC_RET_STATUS eth_port_send (ETH_PORT_INFO *p_eth_port_ctrl, - ETH_QUEUE tx_queue, - PKT_INFO *p_pkt_info); -static ETH_FUNC_RET_STATUS eth_tx_return_desc(ETH_PORT_INFO *p_eth_port_ctrl, - ETH_QUEUE tx_queue, - PKT_INFO *p_pkt_info); -static ETH_FUNC_RET_STATUS eth_port_receive (ETH_PORT_INFO *p_eth_port_ctrl, - ETH_QUEUE rx_queue, - PKT_INFO *p_pkt_info); -static ETH_FUNC_RET_STATUS eth_rx_return_buff(ETH_PORT_INFO *p_eth_port_ctrl, - ETH_QUEUE rx_queue, - PKT_INFO *p_pkt_info); - - -static bool ether_init_tx_desc_ring(ETH_PORT_INFO *p_eth_port_ctrl, - ETH_QUEUE tx_queue, - int tx_desc_num, - int tx_buff_size, - unsigned int tx_desc_base_addr, - unsigned int tx_buff_base_addr); - -static bool ether_init_rx_desc_ring(ETH_PORT_INFO *p_eth_port_ctrl, - ETH_QUEUE rx_queue, - int rx_desc_num, - int rx_buff_size, - unsigned int rx_desc_base_addr, - unsigned int rx_buff_base_addr); - #endif /* MV64460_ETH_ */ diff --git a/board/prodrive/p3mx/p3mx.c b/board/prodrive/p3mx/p3mx.c index 09e4f82..31c7869 100644 --- a/board/prodrive/p3mx/p3mx.c +++ b/board/prodrive/p3mx/p3mx.c @@ -44,6 +44,7 @@ #include "eth.h" #include "mpsc.h" #include "64460.h" +#include "mv_eth.h" #include "mv_regs.h" #include "p3mx.h"
@@ -82,6 +83,11 @@ void set_led(int);
/* ------------------------------------------------------------------------- */
+int board_eth_init(bd_t *bis) +{ + return mv6446x_eth_initialize(bis); +} + /* * This is a version of the GT register space remapping function that * doesn't touch globals (meaning, it's ok to run from flash.) diff --git a/net/eth.c b/net/eth.c index 02ccc75..fdc9e22 100644 --- a/net/eth.c +++ b/net/eth.c @@ -81,9 +81,6 @@ static int __def_eth_init(bd_t *bis) int cpu_eth_init(bd_t *bis) __attribute__((weak, alias("__def_eth_init"))); int board_eth_init(bd_t *bis) __attribute__((weak, alias("__def_eth_init")));
-extern int mv6436x_eth_initialize(bd_t *); -extern int mv6446x_eth_initialize(bd_t *); - #ifdef CONFIG_API extern void (*push_packet)(volatile void *, int);
@@ -251,12 +248,6 @@ int eth_initialize(bd_t *bis) } else printf("Net Initialization Skipped\n");
-#if defined(CONFIG_DB64360) || defined(CONFIG_CPCI750) - mv6436x_eth_initialize(bis); -#endif -#if defined(CONFIG_DB64460) || defined(CONFIG_P3Mx) - mv6446x_eth_initialize(bis); -#endif if (!eth_devices) { puts("No ethernet found.\n"); show_boot_progress(-64);

On Thursday 19 January 2012 19:53:06 Joe Hershberger wrote:
Clears up checkpatch.pl complaint about extern in c file Completes the effort started by Ben Warren in 2008 to generalize eth init
awesome. these boards have been around "forever", so if Stefan doesn't have any feedback, i'm good to merge it. -mike

On Friday 03 February 2012 12:49:12 Mike Frysinger wrote:
On Thursday 19 January 2012 19:53:06 Joe Hershberger wrote:
Clears up checkpatch.pl complaint about extern in c file Completes the effort started by Ben Warren in 2008 to generalize eth init
awesome. these boards have been around "forever", so if Stefan doesn't have any feedback, i'm good to merge it.
I have no objections. I didn't even remember being maintainer of these PowerPC 750/7448 based boards. It might be possible to drop support for those completely.
But never the less:
Acked-by: Stefan Roese sr@denx.de
Thanks, Stefan

MAC-seeded rand() will be needed by link-local as well, so give it an interface
Signed-off-by: Joe Hershberger joe.hershberger@ni.com Cc: Joe Hershberger joe.hershberger@gmail.com Cc: Wolfgang Denk wd@denx.de --- net/Makefile | 1 + net/bootp.c | 69 +++++++++++-------------------------------------------- net/bootp.h | 3 -- net/net_rand.c | 68 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ net/net_rand.h | 18 ++++++++++++++ 5 files changed, 101 insertions(+), 58 deletions(-) create mode 100644 net/net_rand.c create mode 100644 net/net_rand.h
diff --git a/net/Makefile b/net/Makefile index 0544f6b..5901046 100644 --- a/net/Makefile +++ b/net/Makefile @@ -31,6 +31,7 @@ COBJS-$(CONFIG_CMD_NET) += bootp.o COBJS-$(CONFIG_CMD_DNS) += dns.o COBJS-$(CONFIG_CMD_NET) += eth.o COBJS-$(CONFIG_CMD_NET) += net.o +COBJS-$(CONFIG_BOOTP_RANDOM_DELAY) += net_rand.o COBJS-$(CONFIG_CMD_NFS) += nfs.o COBJS-$(CONFIG_CMD_RARP) += rarp.o COBJS-$(CONFIG_CMD_SNTP) += sntp.o diff --git a/net/bootp.c b/net/bootp.c index 07870d0..9824cd1 100644 --- a/net/bootp.c +++ b/net/bootp.c @@ -12,6 +12,9 @@ #include <command.h> #include <net.h> #include "bootp.h" +#ifdef CONFIG_BOOTP_RANDOM_DELAY +#include "net_rand.h" +#endif #include "tftp.h" #include "nfs.h" #ifdef CONFIG_STATUS_LED @@ -37,9 +40,6 @@
ulong BootpID; int BootpTry; -#ifdef CONFIG_BOOTP_RANDOM_DELAY -ulong seed1, seed2; -#endif
#if defined(CONFIG_CMD_DHCP) dhcp_state_t dhcp_state = INIT; @@ -583,66 +583,25 @@ BootpRequest(void) uchar *pkt, *iphdr; struct Bootp_t *bp; int ext_len, pktlen, iplen; +#ifdef CONFIG_BOOTP_RANDOM_DELAY + ulong i, rand_ms; +#endif
#if defined(CONFIG_CMD_DHCP) dhcp_state = INIT; #endif
#ifdef CONFIG_BOOTP_RANDOM_DELAY /* Random BOOTP delay */ - unsigned char bi_enetaddr[6]; - int reg; - ulong tst1, tst2, sum, m_mask, m_value = 0; - - if (BootpTry == 0) { - /* get our mac */ - eth_getenv_enetaddr("ethaddr", bi_enetaddr); - - debug("BootpRequest => Our Mac: "); - for (reg = 0; reg < 6; reg++) - debug("%x%c", bi_enetaddr[reg], reg == 5 ? '\n' : ':'); - - /* Mac-Manipulation 2 get seed1 */ - tst1 = 0; - tst2 = 0; - for (reg = 2; reg < 6; reg++) { - tst1 = tst1 << 8; - tst1 = tst1 | bi_enetaddr[reg]; - } - for (reg = 0; reg < 2; reg++) { - tst2 = tst2 | bi_enetaddr[reg]; - tst2 = tst2 << 8; - } - - seed1 = tst1^tst2; + if (BootpTry == 0) + srand_mac();
- /* Mirror seed1*/ - m_mask = 0x1; - for (reg = 1; reg <= 32; reg++) { - m_value |= (m_mask & seed1); - seed1 = seed1 >> 1; - m_value = m_value << 1; - } - seed1 = m_value; - seed2 = 0xB78D0945; - } - - /* Random Number Generator */ - for (reg = 0; reg <= 0; reg++) { - sum = seed1 + seed2; - if (sum < seed1 || sum < seed2) - sum++; - seed2 = seed1; - seed1 = sum; - - if (BootpTry <= 2) { /* Start with max 1024 * 1ms */ - sum = sum >> (22-BootpTry); - } else { /*After 3rd BOOTP request max 8192 * 1ms */ - sum = sum >> 19; - } - } + if (BootpTry <= 2) /* Start with max 1024 * 1ms */ + rand_ms = rand() >> (22-BootpTry); + else /* After 3rd BOOTP request max 8192 * 1ms */ + rand_ms = rand() >> 19;
- printf("Random delay: %ld ms...\n", sum); - for (reg = 0; reg < sum; reg++) + printf("Random delay: %ld ms...\n", rand_ms); + for (i = 0; i < rand_ms; i++) udelay(1000); /*Wait 1ms*/
#endif /* CONFIG_BOOTP_RANDOM_DELAY */ diff --git a/net/bootp.h b/net/bootp.h index ce73734..bf4e875 100644 --- a/net/bootp.h +++ b/net/bootp.h @@ -63,9 +63,6 @@ struct Bootp_t { extern ulong BootpID; /* ID of cur BOOTP request */ extern char BootFile[128]; /* Boot file name */ extern int BootpTry; -#ifdef CONFIG_BOOTP_RANDOM_DELAY -extern ulong seed1, seed2; /* seed for random BOOTP delay */ -#endif
/* Send a BOOTP request */ diff --git a/net/net_rand.c b/net/net_rand.c new file mode 100644 index 0000000..744e2f9 --- /dev/null +++ b/net/net_rand.c @@ -0,0 +1,68 @@ +/* + * Based on LiMon - BOOTP. + * + * Copyright 1994, 1995, 2000 Neil Russell. + * (See License) + * Copyright 2000 Roland Borde + * Copyright 2000 Paolo Scaffardi + * Copyright 2000-2004 Wolfgang Denk, wd@denx.de + */ + +#include <common.h> +#include <net.h> +#include "net_rand.h" + +static ulong seed1, seed2; + +void srand_mac(void) +{ + ulong tst1, tst2, m_mask; + ulong m_value = 0; + int reg; + unsigned char bi_enetaddr[6]; + + /* get our mac */ + eth_getenv_enetaddr("ethaddr", bi_enetaddr); + + debug("BootpRequest => Our Mac: "); + for (reg = 0; reg < 6; reg++) + debug("%x%c", bi_enetaddr[reg], reg == 5 ? '\n' : ':'); + + /* Mac-Manipulation 2 get seed1 */ + tst1 = 0; + tst2 = 0; + for (reg = 2; reg < 6; reg++) { + tst1 = tst1 << 8; + tst1 = tst1 | bi_enetaddr[reg]; + } + for (reg = 0; reg < 2; reg++) { + tst2 = tst2 | bi_enetaddr[reg]; + tst2 = tst2 << 8; + } + + seed1 = tst1^tst2; + + /* Mirror seed1*/ + m_mask = 0x1; + for (reg = 1; reg <= 32; reg++) { + m_value |= (m_mask & seed1); + seed1 = seed1 >> 1; + m_value = m_value << 1; + } + seed1 = m_value; + seed2 = 0xB78D0945; +} + +unsigned long rand(void) +{ + ulong sum; + + /* Random Number Generator */ + sum = seed1 + seed2; + if (sum < seed1 || sum < seed2) + sum++; + seed2 = seed1; + seed1 = sum; + + return sum; +} diff --git a/net/net_rand.h b/net/net_rand.h new file mode 100644 index 0000000..2c6a6ac --- /dev/null +++ b/net/net_rand.h @@ -0,0 +1,18 @@ +/* + * Copied from LiMon - BOOTP. + * + * Copyright 1994, 1995, 2000 Neil Russell. + * (See License) + * Copyright 2000 Paolo Scaffardi + */ + +#ifndef __NET_RAND_H__ +#define __NET_RAND_H__ + +#define RAND_MAX 0xFFFFFFFF + +void srand_mac(void); +unsigned long rand(void); + +#endif /* __NET_RAND_H__ */ +

Hi Joe,
On Thu, Jan 19, 2012 at 4:53 PM, Joe Hershberger joe.hershberger@ni.com wrote:
MAC-seeded rand() will be needed by link-local as well, so give it an interface
Signed-off-by: Joe Hershberger joe.hershberger@ni.com
Acked-by: Simon Glass sjg@chromium.org
Cc: Joe Hershberger joe.hershberger@gmail.com Cc: Wolfgang Denk wd@denx.de
net/Makefile | 1 + net/bootp.c | 69 +++++++++++-------------------------------------------- net/bootp.h | 3 -- net/net_rand.c | 68 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ net/net_rand.h | 18 ++++++++++++++ 5 files changed, 101 insertions(+), 58 deletions(-) create mode 100644 net/net_rand.c create mode 100644 net/net_rand.h
diff --git a/net/Makefile b/net/Makefile index 0544f6b..5901046 100644 --- a/net/Makefile +++ b/net/Makefile @@ -31,6 +31,7 @@ COBJS-$(CONFIG_CMD_NET) += bootp.o COBJS-$(CONFIG_CMD_DNS) += dns.o COBJS-$(CONFIG_CMD_NET) += eth.o COBJS-$(CONFIG_CMD_NET) += net.o +COBJS-$(CONFIG_BOOTP_RANDOM_DELAY) += net_rand.o COBJS-$(CONFIG_CMD_NFS) += nfs.o COBJS-$(CONFIG_CMD_RARP) += rarp.o COBJS-$(CONFIG_CMD_SNTP) += sntp.o diff --git a/net/bootp.c b/net/bootp.c index 07870d0..9824cd1 100644 --- a/net/bootp.c +++ b/net/bootp.c @@ -12,6 +12,9 @@ #include <command.h> #include <net.h> #include "bootp.h" +#ifdef CONFIG_BOOTP_RANDOM_DELAY +#include "net_rand.h" +#endif #include "tftp.h" #include "nfs.h" #ifdef CONFIG_STATUS_LED @@ -37,9 +40,6 @@
ulong BootpID; int BootpTry; -#ifdef CONFIG_BOOTP_RANDOM_DELAY -ulong seed1, seed2; -#endif
#if defined(CONFIG_CMD_DHCP) dhcp_state_t dhcp_state = INIT; @@ -583,66 +583,25 @@ BootpRequest(void) uchar *pkt, *iphdr; struct Bootp_t *bp; int ext_len, pktlen, iplen; +#ifdef CONFIG_BOOTP_RANDOM_DELAY
- ulong i, rand_ms;
+#endif
#if defined(CONFIG_CMD_DHCP) dhcp_state = INIT; #endif
#ifdef CONFIG_BOOTP_RANDOM_DELAY /* Random BOOTP delay */
- unsigned char bi_enetaddr[6];
- int reg;
- ulong tst1, tst2, sum, m_mask, m_value = 0;
- if (BootpTry == 0) {
- /* get our mac */
- eth_getenv_enetaddr("ethaddr", bi_enetaddr);
- debug("BootpRequest => Our Mac: ");
- for (reg = 0; reg < 6; reg++)
- debug("%x%c", bi_enetaddr[reg], reg == 5 ? '\n' : ':');
- /* Mac-Manipulation 2 get seed1 */
- tst1 = 0;
- tst2 = 0;
- for (reg = 2; reg < 6; reg++) {
- tst1 = tst1 << 8;
- tst1 = tst1 | bi_enetaddr[reg];
- }
- for (reg = 0; reg < 2; reg++) {
- tst2 = tst2 | bi_enetaddr[reg];
- tst2 = tst2 << 8;
- }
- seed1 = tst1^tst2;
- if (BootpTry == 0)
- srand_mac();
- /* Mirror seed1*/
- m_mask = 0x1;
- for (reg = 1; reg <= 32; reg++) {
- m_value |= (m_mask & seed1);
- seed1 = seed1 >> 1;
- m_value = m_value << 1;
- }
- seed1 = m_value;
- seed2 = 0xB78D0945;
- }
- /* Random Number Generator */
- for (reg = 0; reg <= 0; reg++) {
- sum = seed1 + seed2;
- if (sum < seed1 || sum < seed2)
- sum++;
- seed2 = seed1;
- seed1 = sum;
- if (BootpTry <= 2) { /* Start with max 1024 * 1ms */
- sum = sum >> (22-BootpTry);
- } else { /*After 3rd BOOTP request max 8192 * 1ms */
- sum = sum >> 19;
- }
- }
- if (BootpTry <= 2) /* Start with max 1024 * 1ms */
- rand_ms = rand() >> (22-BootpTry);
- else /* After 3rd BOOTP request max 8192 * 1ms */
- rand_ms = rand() >> 19;
- printf("Random delay: %ld ms...\n", sum);
- for (reg = 0; reg < sum; reg++)
- printf("Random delay: %ld ms...\n", rand_ms);
- for (i = 0; i < rand_ms; i++)
udelay(1000); /*Wait 1ms*/
#endif /* CONFIG_BOOTP_RANDOM_DELAY */ diff --git a/net/bootp.h b/net/bootp.h index ce73734..bf4e875 100644 --- a/net/bootp.h +++ b/net/bootp.h @@ -63,9 +63,6 @@ struct Bootp_t { extern ulong BootpID; /* ID of cur BOOTP request */ extern char BootFile[128]; /* Boot file name */ extern int BootpTry; -#ifdef CONFIG_BOOTP_RANDOM_DELAY -extern ulong seed1, seed2; /* seed for random BOOTP delay */ -#endif
/* Send a BOOTP request */ diff --git a/net/net_rand.c b/net/net_rand.c new file mode 100644 index 0000000..744e2f9 --- /dev/null +++ b/net/net_rand.c @@ -0,0 +1,68 @@ +/*
- Based on LiMon - BOOTP.
- Copyright 1994, 1995, 2000 Neil Russell.
- (See License)
- Copyright 2000 Roland Borde
- Copyright 2000 Paolo Scaffardi
- Copyright 2000-2004 Wolfgang Denk, wd@denx.de
- */
+#include <common.h> +#include <net.h> +#include "net_rand.h"
+static ulong seed1, seed2;
+void srand_mac(void) +{
- ulong tst1, tst2, m_mask;
- ulong m_value = 0;
- int reg;
- unsigned char bi_enetaddr[6];
- /* get our mac */
- eth_getenv_enetaddr("ethaddr", bi_enetaddr);
- debug("BootpRequest => Our Mac: ");
- for (reg = 0; reg < 6; reg++)
- debug("%x%c", bi_enetaddr[reg], reg == 5 ? '\n' : ':');
- /* Mac-Manipulation 2 get seed1 */
- tst1 = 0;
- tst2 = 0;
- for (reg = 2; reg < 6; reg++) {
- tst1 = tst1 << 8;
- tst1 = tst1 | bi_enetaddr[reg];
- }
- for (reg = 0; reg < 2; reg++) {
- tst2 = tst2 | bi_enetaddr[reg];
- tst2 = tst2 << 8;
- }
- seed1 = tst1^tst2;
- /* Mirror seed1*/
- m_mask = 0x1;
- for (reg = 1; reg <= 32; reg++) {
- m_value |= (m_mask & seed1);
- seed1 = seed1 >> 1;
- m_value = m_value << 1;
- }
- seed1 = m_value;
- seed2 = 0xB78D0945;
+}
+unsigned long rand(void) +{
- ulong sum;
- /* Random Number Generator */
- sum = seed1 + seed2;
- if (sum < seed1 || sum < seed2)
- sum++;
- seed2 = seed1;
- seed1 = sum;
- return sum;
+} diff --git a/net/net_rand.h b/net/net_rand.h new file mode 100644 index 0000000..2c6a6ac --- /dev/null +++ b/net/net_rand.h @@ -0,0 +1,18 @@ +/*
- Copied from LiMon - BOOTP.
- Copyright 1994, 1995, 2000 Neil Russell.
- (See License)
- Copyright 2000 Paolo Scaffardi
- */
+#ifndef __NET_RAND_H__ +#define __NET_RAND_H__
+#define RAND_MAX 0xFFFFFFFF
+void srand_mac(void); +unsigned long rand(void);
+#endif /* __NET_RAND_H__ */
-- 1.6.0.2
U-Boot mailing list U-Boot@lists.denx.de http://lists.denx.de/mailman/listinfo/u-boot

On Thursday 19 January 2012 19:53:07 Joe Hershberger wrote:
--- a/net/bootp.c +++ b/net/bootp.c
+#ifdef CONFIG_BOOTP_RANDOM_DELAY +#include "net_rand.h" +#endif
let's push this CONFIG_xxx check into net_rand.h so we don't have to check it every time we include the file -mike

Hi Mike,
On Fri, Feb 3, 2012 at 5:51 AM, Mike Frysinger vapier@gentoo.org wrote:
On Thursday 19 January 2012 19:53:07 Joe Hershberger wrote:
--- a/net/bootp.c +++ b/net/bootp.c
+#ifdef CONFIG_BOOTP_RANDOM_DELAY +#include "net_rand.h" +#endif
let's push this CONFIG_xxx check into net_rand.h so we don't have to check it every time we include the file
In this case, it doesn't make sense. The rand is enabled based on other features that need it... it does not have a config of its own. As a result, putting this in the header would make it worse. This guard is still needed in bootp.c, since rand is only needed when that subfeature is enabled. For link-local, it will always be included.
-Joe

On Friday 10 February 2012 16:51:00 Joe Hershberger wrote:
On Fri, Feb 3, 2012 at 5:51 AM, Mike Frysinger wrote:
On Thursday 19 January 2012 19:53:07 Joe Hershberger wrote:
--- a/net/bootp.c +++ b/net/bootp.c
+#ifdef CONFIG_BOOTP_RANDOM_DELAY +#include "net_rand.h" +#endif
let's push this CONFIG_xxx check into net_rand.h so we don't have to check it every time we include the file
In this case, it doesn't make sense. The rand is enabled based on other features that need it... it does not have a config of its own. As a result, putting this in the header would make it worse. This guard is still needed in bootp.c, since rand is only needed when that subfeature is enabled. For link-local, it will always be included.
i'm not sure guarding the include really gains us anything. i'm inclined to just always include it and be done. -mike

Signed-off-by: Joe Hershberger joe.hershberger@ni.com Cc: Joe Hershberger joe.hershberger@gmail.com Cc: Wolfgang Denk wd@denx.de --- arch/powerpc/cpu/mpc8xx/fec.c | 2 +- include/net.h | 9 +- net/Makefile | 1 + net/cdp.c | 380 +++++++++++++++++++++++++++++++++++++++++ net/cdp.h | 18 ++ net/net.c | 368 +--------------------------------------- 6 files changed, 406 insertions(+), 372 deletions(-) create mode 100644 net/cdp.c create mode 100644 net/cdp.h
diff --git a/arch/powerpc/cpu/mpc8xx/fec.c b/arch/powerpc/cpu/mpc8xx/fec.c index f2a2c3a..0bc1e5d 100644 --- a/arch/powerpc/cpu/mpc8xx/fec.c +++ b/arch/powerpc/cpu/mpc8xx/fec.c @@ -274,7 +274,7 @@ static int fec_recv (struct eth_device *dev) #if defined(CONFIG_CMD_CDP) if ((rx[0] & 1) != 0 && memcmp ((uchar *) rx, NetBcastAddr, 6) != 0 - && memcmp ((uchar *) rx, NetCDPAddr, 6) != 0) + && !is_cdp_packet((uchar *)rx)) rx = NULL; #endif /* diff --git a/include/net.h b/include/net.h index 09d1b51..0396b69 100644 --- a/include/net.h +++ b/include/net.h @@ -355,10 +355,6 @@ extern uchar NetEtherNullAddr[6]; extern ushort NetOurVLAN; /* Our VLAN */ extern ushort NetOurNativeVLAN; /* Our Native VLAN */
-extern uchar NetCDPAddr[6]; /* Ethernet CDP address */ -extern ushort CDPNativeVLAN; /* CDP returned native VLAN */ -extern ushort CDPApplianceVLAN; /* CDP returned appliance VLAN */ - extern int NetState; /* Network loop state */ #define NETLOOP_CONTINUE 1 #define NETLOOP_RESTART 2 @@ -386,8 +382,9 @@ extern IPaddr_t NetPingIP; /* the ip address to ping */
#if defined(CONFIG_CMD_CDP) /* when CDP completes these hold the return values */ -extern ushort CDPNativeVLAN; -extern ushort CDPApplianceVLAN; +extern ushort CDPNativeVLAN; /* CDP returned native VLAN */ +extern ushort CDPApplianceVLAN; /* CDP returned appliance VLAN */ +int is_cdp_packet(const uchar *et_addr); #endif
#if defined(CONFIG_CMD_SNTP) diff --git a/net/Makefile b/net/Makefile index 5901046..b350bfc 100644 --- a/net/Makefile +++ b/net/Makefile @@ -28,6 +28,7 @@ include $(TOPDIR)/config.mk LIB = $(obj)libnet.o
COBJS-$(CONFIG_CMD_NET) += bootp.o +COBJS-$(CONFIG_CMD_CDP) += cdp.o COBJS-$(CONFIG_CMD_DNS) += dns.o COBJS-$(CONFIG_CMD_NET) += eth.o COBJS-$(CONFIG_CMD_NET) += net.o diff --git a/net/cdp.c b/net/cdp.c new file mode 100644 index 0000000..c5e54b2 --- /dev/null +++ b/net/cdp.c @@ -0,0 +1,380 @@ +/* + * Copied from Linux Monitor (LiMon) - Networking. + * + * Copyright 1994 - 2000 Neil Russell. + * (See License) + * Copyright 2000 Roland Borde + * Copyright 2000 Paolo Scaffardi + * Copyright 2000-2002 Wolfgang Denk, wd@denx.de + */ + +#include <common.h> +#include <net.h> +#if defined(CONFIG_CDP_VERSION) +#include <timestamp.h> +#endif + +#include "cdp.h" + +/* Ethernet bcast address */ +static const uchar NetCDPAddr[6] = { 0x01, 0x00, 0x0c, 0xcc, 0xcc, 0xcc }; + +#define CDP_DEVICE_ID_TLV 0x0001 +#define CDP_ADDRESS_TLV 0x0002 +#define CDP_PORT_ID_TLV 0x0003 +#define CDP_CAPABILITIES_TLV 0x0004 +#define CDP_VERSION_TLV 0x0005 +#define CDP_PLATFORM_TLV 0x0006 +#define CDP_NATIVE_VLAN_TLV 0x000a +#define CDP_APPLIANCE_VLAN_TLV 0x000e +#define CDP_TRIGGER_TLV 0x000f +#define CDP_POWER_CONSUMPTION_TLV 0x0010 +#define CDP_SYSNAME_TLV 0x0014 +#define CDP_SYSOBJECT_TLV 0x0015 +#define CDP_MANAGEMENT_ADDRESS_TLV 0x0016 + +#define CDP_TIMEOUT 250UL /* one packet every 250ms */ + +static int CDPSeq; +static int CDPOK; + +ushort CDPNativeVLAN; +ushort CDPApplianceVLAN; + +static const uchar CDP_SNAP_hdr[8] = { 0xAA, 0xAA, 0x03, 0x00, 0x00, 0x0C, 0x20, + 0x00 }; + +static ushort +CDP_compute_csum(const uchar *buff, ushort len) +{ + ushort csum; + int odd; + ulong result = 0; + ushort leftover; + ushort *p; + + if (len > 0) { + odd = 1 & (ulong)buff; + if (odd) { + result = *buff << 8; + len--; + buff++; + } + while (len > 1) { + p = (ushort *)buff; + result += *p++; + buff = (uchar *)p; + if (result & 0x80000000) + result = (result & 0xFFFF) + (result >> 16); + len -= 2; + } + if (len) { + leftover = (signed short)(*(const signed char *)buff); + /* CISCO SUCKS big time! (and blows too): + * CDP uses the IP checksum algorithm with a twist; + * for the last byte it *sign* extends and sums. + */ + result = (result & 0xffff0000) | + ((result + leftover) & 0x0000ffff); + } + while (result >> 16) + result = (result & 0xFFFF) + (result >> 16); + + if (odd) + result = ((result >> 8) & 0xff) | + ((result & 0xff) << 8); + } + + /* add up 16-bit and 17-bit words for 17+c bits */ + result = (result & 0xffff) + (result >> 16); + /* add up 16-bit and 2-bit for 16+c bit */ + result = (result & 0xffff) + (result >> 16); + /* add up carry.. */ + result = (result & 0xffff) + (result >> 16); + + /* negate */ + csum = ~(ushort)result; + + /* run time endian detection */ + if (csum != htons(csum)) /* little endian */ + csum = htons(csum); + + return csum; +} + +static int +CDPSendTrigger(void) +{ + uchar *pkt; + ushort *s; + ushort *cp; + Ethernet_t *et; + int len; + ushort chksum; +#if defined(CONFIG_CDP_DEVICE_ID) || defined(CONFIG_CDP_PORT_ID) || \ + defined(CONFIG_CDP_VERSION) || defined(CONFIG_CDP_PLATFORM) + char buf[32]; +#endif + + pkt = NetTxPacket; + et = (Ethernet_t *)pkt; + + /* NOTE: trigger sent not on any VLAN */ + + /* form ethernet header */ + memcpy(et->et_dest, NetCDPAddr, 6); + memcpy(et->et_src, NetOurEther, 6); + + pkt += ETHER_HDR_SIZE; + + /* SNAP header */ + memcpy((uchar *)pkt, CDP_SNAP_hdr, sizeof(CDP_SNAP_hdr)); + pkt += sizeof(CDP_SNAP_hdr); + + /* CDP header */ + *pkt++ = 0x02; /* CDP version 2 */ + *pkt++ = 180; /* TTL */ + s = (ushort *)pkt; + cp = s; + /* checksum (0 for later calculation) */ + *s++ = htons(0); + + /* CDP fields */ +#ifdef CONFIG_CDP_DEVICE_ID + *s++ = htons(CDP_DEVICE_ID_TLV); + *s++ = htons(CONFIG_CDP_DEVICE_ID); + sprintf(buf, CONFIG_CDP_DEVICE_ID_PREFIX "%pm", NetOurEther); + memcpy((uchar *)s, buf, 16); + s += 16 / 2; +#endif + +#ifdef CONFIG_CDP_PORT_ID + *s++ = htons(CDP_PORT_ID_TLV); + memset(buf, 0, sizeof(buf)); + sprintf(buf, CONFIG_CDP_PORT_ID, eth_get_dev_index()); + len = strlen(buf); + if (len & 1) /* make it even */ + len++; + *s++ = htons(len + 4); + memcpy((uchar *)s, buf, len); + s += len / 2; +#endif + +#ifdef CONFIG_CDP_CAPABILITIES + *s++ = htons(CDP_CAPABILITIES_TLV); + *s++ = htons(8); + *(ulong *)s = htonl(CONFIG_CDP_CAPABILITIES); + s += 2; +#endif + +#ifdef CONFIG_CDP_VERSION + *s++ = htons(CDP_VERSION_TLV); + memset(buf, 0, sizeof(buf)); + strcpy(buf, CONFIG_CDP_VERSION); + len = strlen(buf); + if (len & 1) /* make it even */ + len++; + *s++ = htons(len + 4); + memcpy((uchar *)s, buf, len); + s += len / 2; +#endif + +#ifdef CONFIG_CDP_PLATFORM + *s++ = htons(CDP_PLATFORM_TLV); + memset(buf, 0, sizeof(buf)); + strcpy(buf, CONFIG_CDP_PLATFORM); + len = strlen(buf); + if (len & 1) /* make it even */ + len++; + *s++ = htons(len + 4); + memcpy((uchar *)s, buf, len); + s += len / 2; +#endif + +#ifdef CONFIG_CDP_TRIGGER + *s++ = htons(CDP_TRIGGER_TLV); + *s++ = htons(8); + *(ulong *)s = htonl(CONFIG_CDP_TRIGGER); + s += 2; +#endif + +#ifdef CONFIG_CDP_POWER_CONSUMPTION + *s++ = htons(CDP_POWER_CONSUMPTION_TLV); + *s++ = htons(6); + *s++ = htons(CONFIG_CDP_POWER_CONSUMPTION); +#endif + + /* length of ethernet packet */ + len = (uchar *)s - ((uchar *)NetTxPacket + ETHER_HDR_SIZE); + et->et_protlen = htons(len); + + len = ETHER_HDR_SIZE + sizeof(CDP_SNAP_hdr); + chksum = CDP_compute_csum((uchar *)NetTxPacket + len, + (uchar *)s - (NetTxPacket + len)); + if (chksum == 0) + chksum = 0xFFFF; + *cp = htons(chksum); + + (void) eth_send(NetTxPacket, (uchar *)s - NetTxPacket); + return 0; +} + +static void +CDPTimeout(void) +{ + CDPSeq++; + + if (CDPSeq < 3) { + NetSetTimeout(CDP_TIMEOUT, CDPTimeout); + CDPSendTrigger(); + return; + } + + /* if not OK try again */ + if (!CDPOK) + NetStartAgain(); + else + NetState = NETLOOP_SUCCESS; +} + +static void +CDPDummyHandler(uchar *pkt, unsigned dest, IPaddr_t sip, unsigned src, + unsigned len) +{ + /* nothing */ +} + +void +CDPHandler(const uchar *pkt, unsigned len) +{ + const uchar *t; + const ushort *ss; + ushort type, tlen; + ushort vlan, nvlan; + + /* minimum size? */ + if (len < sizeof(CDP_SNAP_hdr) + 4) + goto pkt_short; + + /* check for valid CDP SNAP header */ + if (memcmp(pkt, CDP_SNAP_hdr, sizeof(CDP_SNAP_hdr)) != 0) + return; + + pkt += sizeof(CDP_SNAP_hdr); + len -= sizeof(CDP_SNAP_hdr); + + /* Version of CDP protocol must be >= 2 and TTL != 0 */ + if (pkt[0] < 0x02 || pkt[1] == 0) + return; + + /* + * if version is greater than 0x02 maybe we'll have a problem; + * output a warning + */ + if (pkt[0] != 0x02) + printf("**WARNING: CDP packet received with a protocol version " + "%d > 2\n", pkt[0] & 0xff); + + if (CDP_compute_csum(pkt, len) != 0) + return; + + pkt += 4; + len -= 4; + + vlan = htons(-1); + nvlan = htons(-1); + while (len > 0) { + if (len < 4) + goto pkt_short; + + ss = (const ushort *)pkt; + type = ntohs(ss[0]); + tlen = ntohs(ss[1]); + if (tlen > len) + goto pkt_short; + + pkt += tlen; + len -= tlen; + + ss += 2; /* point ss to the data of the TLV */ + tlen -= 4; + + switch (type) { + case CDP_DEVICE_ID_TLV: + break; + case CDP_ADDRESS_TLV: + break; + case CDP_PORT_ID_TLV: + break; + case CDP_CAPABILITIES_TLV: + break; + case CDP_VERSION_TLV: + break; + case CDP_PLATFORM_TLV: + break; + case CDP_NATIVE_VLAN_TLV: + nvlan = *ss; + break; + case CDP_APPLIANCE_VLAN_TLV: + t = (const uchar *)ss; + while (tlen > 0) { + if (tlen < 3) + goto pkt_short; + + ss = (const ushort *)(t + 1); + +#ifdef CONFIG_CDP_APPLIANCE_VLAN_TYPE + if (t[0] == CONFIG_CDP_APPLIANCE_VLAN_TYPE) + vlan = *ss; +#else + /* XXX will this work; dunno */ + vlan = ntohs(*ss); +#endif + t += 3; tlen -= 3; + } + break; + case CDP_TRIGGER_TLV: + break; + case CDP_POWER_CONSUMPTION_TLV: + break; + case CDP_SYSNAME_TLV: + break; + case CDP_SYSOBJECT_TLV: + break; + case CDP_MANAGEMENT_ADDRESS_TLV: + break; + } + } + + CDPApplianceVLAN = vlan; + CDPNativeVLAN = nvlan; + + CDPOK = 1; + return; + + pkt_short: + printf("** CDP packet is too short\n"); + return; +} + +void +CDPStart(void) +{ + printf("Using %s device\n", eth_get_name()); + CDPSeq = 0; + CDPOK = 0; + + CDPNativeVLAN = htons(-1); + CDPApplianceVLAN = htons(-1); + + NetSetTimeout(CDP_TIMEOUT, CDPTimeout); + NetSetHandler(CDPDummyHandler); + + CDPSendTrigger(); +} + +int +is_cdp_packet(const uchar *et_addr) +{ + return memcmp(et_addr, NetCDPAddr, 6) == 0; +} diff --git a/net/cdp.h b/net/cdp.h new file mode 100644 index 0000000..fef744e --- /dev/null +++ b/net/cdp.h @@ -0,0 +1,18 @@ +/* + * Copied from Linux Monitor (LiMon) - Networking. + * + * Copyright 1994 - 2000 Neil Russell. + * (See License) + * Copyright 2000 Roland Borde + * Copyright 2000 Paolo Scaffardi + * Copyright 2000-2002 Wolfgang Denk, wd@denx.de + */ + +#ifndef __CDP_H__ +#define __CDP_H__ + +void CDPStart(void); +void CDPHandler(const uchar *pkt, unsigned len); + +#endif /* __CDP_H__ */ + diff --git a/net/net.c b/net/net.c index 1d6252f..48d3ca8 100644 --- a/net/net.c +++ b/net/net.c @@ -91,8 +91,8 @@ #if defined(CONFIG_CMD_SNTP) #include "sntp.h" #endif -#if defined(CONFIG_CDP_VERSION) -#include <timestamp.h> +#if defined(CONFIG_CMD_CDP) +#include "cdp.h" #endif #if defined(CONFIG_CMD_DNS) #include "dns.h" @@ -163,10 +163,6 @@ uchar NetEtherNullAddr[6]; #ifdef CONFIG_API void (*push_packet)(volatile void *, int len) = 0; #endif -#if defined(CONFIG_CMD_CDP) -/* Ethernet bcast address */ -uchar NetCDPAddr[6] = { 0x01, 0x00, 0x0c, 0xcc, 0xcc, 0xcc }; -#endif /* Network loop state */ int NetState; /* Tried all network devices */ @@ -192,10 +188,6 @@ IPaddr_t NetPingIP; static void PingStart(void); #endif
-#if defined(CONFIG_CMD_CDP) -static void CDPStart(void); -#endif - #if defined(CONFIG_CMD_SNTP) /* NTP server IP address */ IPaddr_t NetNtpServerIP; @@ -841,360 +833,6 @@ static void PingStart(void) } #endif
-#if defined(CONFIG_CMD_CDP) - -#define CDP_DEVICE_ID_TLV 0x0001 -#define CDP_ADDRESS_TLV 0x0002 -#define CDP_PORT_ID_TLV 0x0003 -#define CDP_CAPABILITIES_TLV 0x0004 -#define CDP_VERSION_TLV 0x0005 -#define CDP_PLATFORM_TLV 0x0006 -#define CDP_NATIVE_VLAN_TLV 0x000a -#define CDP_APPLIANCE_VLAN_TLV 0x000e -#define CDP_TRIGGER_TLV 0x000f -#define CDP_POWER_CONSUMPTION_TLV 0x0010 -#define CDP_SYSNAME_TLV 0x0014 -#define CDP_SYSOBJECT_TLV 0x0015 -#define CDP_MANAGEMENT_ADDRESS_TLV 0x0016 - -#define CDP_TIMEOUT 250UL /* one packet every 250ms */ - -static int CDPSeq; -static int CDPOK; - -ushort CDPNativeVLAN; -ushort CDPApplianceVLAN; - -static const uchar CDP_SNAP_hdr[8] = { 0xAA, 0xAA, 0x03, 0x00, 0x00, 0x0C, 0x20, - 0x00 }; - -static ushort CDP_compute_csum(const uchar *buff, ushort len) -{ - ushort csum; - int odd; - ulong result = 0; - ushort leftover; - ushort *p; - - if (len > 0) { - odd = 1 & (ulong)buff; - if (odd) { - result = *buff << 8; - len--; - buff++; - } - while (len > 1) { - p = (ushort *)buff; - result += *p++; - buff = (uchar *)p; - if (result & 0x80000000) - result = (result & 0xFFFF) + (result >> 16); - len -= 2; - } - if (len) { - leftover = (signed short)(*(const signed char *)buff); - /* CISCO SUCKS big time! (and blows too): - * CDP uses the IP checksum algorithm with a twist; - * for the last byte it *sign* extends and sums. - */ - result = (result & 0xffff0000) | - ((result + leftover) & 0x0000ffff); - } - while (result >> 16) - result = (result & 0xFFFF) + (result >> 16); - - if (odd) - result = ((result >> 8) & 0xff) | - ((result & 0xff) << 8); - } - - /* add up 16-bit and 17-bit words for 17+c bits */ - result = (result & 0xffff) + (result >> 16); - /* add up 16-bit and 2-bit for 16+c bit */ - result = (result & 0xffff) + (result >> 16); - /* add up carry.. */ - result = (result & 0xffff) + (result >> 16); - - /* negate */ - csum = ~(ushort)result; - - /* run time endian detection */ - if (csum != htons(csum)) /* little endian */ - csum = htons(csum); - - return csum; -} - -int CDPSendTrigger(void) -{ - uchar *pkt; - ushort *s; - ushort *cp; - Ethernet_t *et; - int len; - ushort chksum; -#if defined(CONFIG_CDP_DEVICE_ID) || defined(CONFIG_CDP_PORT_ID) || \ - defined(CONFIG_CDP_VERSION) || defined(CONFIG_CDP_PLATFORM) - char buf[32]; -#endif - - pkt = NetTxPacket; - et = (Ethernet_t *)pkt; - - /* NOTE: trigger sent not on any VLAN */ - - /* form ethernet header */ - memcpy(et->et_dest, NetCDPAddr, 6); - memcpy(et->et_src, NetOurEther, 6); - - pkt += ETHER_HDR_SIZE; - - /* SNAP header */ - memcpy((uchar *)pkt, CDP_SNAP_hdr, sizeof(CDP_SNAP_hdr)); - pkt += sizeof(CDP_SNAP_hdr); - - /* CDP header */ - *pkt++ = 0x02; /* CDP version 2 */ - *pkt++ = 180; /* TTL */ - s = (ushort *)pkt; - cp = s; - /* checksum (0 for later calculation) */ - *s++ = htons(0); - - /* CDP fields */ -#ifdef CONFIG_CDP_DEVICE_ID - *s++ = htons(CDP_DEVICE_ID_TLV); - *s++ = htons(CONFIG_CDP_DEVICE_ID); - sprintf(buf, CONFIG_CDP_DEVICE_ID_PREFIX "%pm", NetOurEther); - memcpy((uchar *)s, buf, 16); - s += 16 / 2; -#endif - -#ifdef CONFIG_CDP_PORT_ID - *s++ = htons(CDP_PORT_ID_TLV); - memset(buf, 0, sizeof(buf)); - sprintf(buf, CONFIG_CDP_PORT_ID, eth_get_dev_index()); - len = strlen(buf); - if (len & 1) /* make it even */ - len++; - *s++ = htons(len + 4); - memcpy((uchar *)s, buf, len); - s += len / 2; -#endif - -#ifdef CONFIG_CDP_CAPABILITIES - *s++ = htons(CDP_CAPABILITIES_TLV); - *s++ = htons(8); - *(ulong *)s = htonl(CONFIG_CDP_CAPABILITIES); - s += 2; -#endif - -#ifdef CONFIG_CDP_VERSION - *s++ = htons(CDP_VERSION_TLV); - memset(buf, 0, sizeof(buf)); - strcpy(buf, CONFIG_CDP_VERSION); - len = strlen(buf); - if (len & 1) /* make it even */ - len++; - *s++ = htons(len + 4); - memcpy((uchar *)s, buf, len); - s += len / 2; -#endif - -#ifdef CONFIG_CDP_PLATFORM - *s++ = htons(CDP_PLATFORM_TLV); - memset(buf, 0, sizeof(buf)); - strcpy(buf, CONFIG_CDP_PLATFORM); - len = strlen(buf); - if (len & 1) /* make it even */ - len++; - *s++ = htons(len + 4); - memcpy((uchar *)s, buf, len); - s += len / 2; -#endif - -#ifdef CONFIG_CDP_TRIGGER - *s++ = htons(CDP_TRIGGER_TLV); - *s++ = htons(8); - *(ulong *)s = htonl(CONFIG_CDP_TRIGGER); - s += 2; -#endif - -#ifdef CONFIG_CDP_POWER_CONSUMPTION - *s++ = htons(CDP_POWER_CONSUMPTION_TLV); - *s++ = htons(6); - *s++ = htons(CONFIG_CDP_POWER_CONSUMPTION); -#endif - - /* length of ethernet packet */ - len = (uchar *)s - ((uchar *)NetTxPacket + ETHER_HDR_SIZE); - et->et_protlen = htons(len); - - len = ETHER_HDR_SIZE + sizeof(CDP_SNAP_hdr); - chksum = CDP_compute_csum((uchar *)NetTxPacket + len, - (uchar *)s - (NetTxPacket + len)); - if (chksum == 0) - chksum = 0xFFFF; - *cp = htons(chksum); - - (void) eth_send(NetTxPacket, (uchar *)s - NetTxPacket); - return 0; -} - -static void -CDPTimeout(void) -{ - CDPSeq++; - - if (CDPSeq < 3) { - NetSetTimeout(CDP_TIMEOUT, CDPTimeout); - CDPSendTrigger(); - return; - } - - /* if not OK try again */ - if (!CDPOK) - NetStartAgain(); - else - NetState = NETLOOP_SUCCESS; -} - -static void -CDPDummyHandler(uchar *pkt, unsigned dest, IPaddr_t sip, unsigned src, - unsigned len) -{ - /* nothing */ -} - -static void -CDPHandler(const uchar *pkt, unsigned len) -{ - const uchar *t; - const ushort *ss; - ushort type, tlen; - ushort vlan, nvlan; - - /* minimum size? */ - if (len < sizeof(CDP_SNAP_hdr) + 4) - goto pkt_short; - - /* check for valid CDP SNAP header */ - if (memcmp(pkt, CDP_SNAP_hdr, sizeof(CDP_SNAP_hdr)) != 0) - return; - - pkt += sizeof(CDP_SNAP_hdr); - len -= sizeof(CDP_SNAP_hdr); - - /* Version of CDP protocol must be >= 2 and TTL != 0 */ - if (pkt[0] < 0x02 || pkt[1] == 0) - return; - - /* - * if version is greater than 0x02 maybe we'll have a problem; - * output a warning - */ - if (pkt[0] != 0x02) - printf("**WARNING: CDP packet received with a protocol version " - "%d > 2\n", pkt[0] & 0xff); - - if (CDP_compute_csum(pkt, len) != 0) - return; - - pkt += 4; - len -= 4; - - vlan = htons(-1); - nvlan = htons(-1); - while (len > 0) { - if (len < 4) - goto pkt_short; - - ss = (const ushort *)pkt; - type = ntohs(ss[0]); - tlen = ntohs(ss[1]); - if (tlen > len) - goto pkt_short; - - pkt += tlen; - len -= tlen; - - ss += 2; /* point ss to the data of the TLV */ - tlen -= 4; - - switch (type) { - case CDP_DEVICE_ID_TLV: - break; - case CDP_ADDRESS_TLV: - break; - case CDP_PORT_ID_TLV: - break; - case CDP_CAPABILITIES_TLV: - break; - case CDP_VERSION_TLV: - break; - case CDP_PLATFORM_TLV: - break; - case CDP_NATIVE_VLAN_TLV: - nvlan = *ss; - break; - case CDP_APPLIANCE_VLAN_TLV: - t = (const uchar *)ss; - while (tlen > 0) { - if (tlen < 3) - goto pkt_short; - - ss = (const ushort *)(t + 1); - -#ifdef CONFIG_CDP_APPLIANCE_VLAN_TYPE - if (t[0] == CONFIG_CDP_APPLIANCE_VLAN_TYPE) - vlan = *ss; -#else - /* XXX will this work; dunno */ - vlan = ntohs(*ss); -#endif - t += 3; tlen -= 3; - } - break; - case CDP_TRIGGER_TLV: - break; - case CDP_POWER_CONSUMPTION_TLV: - break; - case CDP_SYSNAME_TLV: - break; - case CDP_SYSOBJECT_TLV: - break; - case CDP_MANAGEMENT_ADDRESS_TLV: - break; - } - } - - CDPApplianceVLAN = vlan; - CDPNativeVLAN = nvlan; - - CDPOK = 1; - return; - - pkt_short: - printf("** CDP packet is too short\n"); - return; -} - -static void CDPStart(void) -{ - printf("Using %s device\n", eth_get_name()); - CDPSeq = 0; - CDPOK = 0; - - CDPNativeVLAN = htons(-1); - CDPApplianceVLAN = htons(-1); - - NetSetTimeout(CDP_TIMEOUT, CDPTimeout); - NetSetHandler(CDPDummyHandler); - - CDPSendTrigger(); -} -#endif - #ifdef CONFIG_IP_DEFRAG /* * This function collects fragments in a single packet, according @@ -1463,7 +1101,7 @@ NetReceive(volatile uchar *inpkt, int len)
#if defined(CONFIG_CMD_CDP) /* keep track if packet is CDP */ - iscdp = memcmp(et->et_dest, NetCDPAddr, 6) == 0; + iscdp = is_cdp_packet(et->et_dest); #endif
myvlanid = ntohs(NetOurVLAN);

Hi Joe,
On Thu, Jan 19, 2012 at 4:53 PM, Joe Hershberger joe.hershberger@ni.com wrote:
Signed-off-by: Joe Hershberger joe.hershberger@ni.com Cc: Joe Hershberger joe.hershberger@gmail.com Cc: Wolfgang Denk wd@denx.de
arch/powerpc/cpu/mpc8xx/fec.c | 2 +- include/net.h | 9 +- net/Makefile | 1 + net/cdp.c | 380 +++++++++++++++++++++++++++++++++++++++++ net/cdp.h | 18 ++ net/net.c | 368 +--------------------------------------- 6 files changed, 406 insertions(+), 372 deletions(-) create mode 100644 net/cdp.c create mode 100644 net/cdp.h
diff --git a/arch/powerpc/cpu/mpc8xx/fec.c b/arch/powerpc/cpu/mpc8xx/fec.c index f2a2c3a..0bc1e5d 100644 --- a/arch/powerpc/cpu/mpc8xx/fec.c +++ b/arch/powerpc/cpu/mpc8xx/fec.c @@ -274,7 +274,7 @@ static int fec_recv (struct eth_device *dev) #if defined(CONFIG_CMD_CDP) if ((rx[0] & 1) != 0 && memcmp ((uchar *) rx, NetBcastAddr, 6) != 0
- && memcmp ((uchar *) rx, NetCDPAddr, 6) != 0)
- && !is_cdp_packet((uchar *)rx))
Do you think the is_cdp_packet() change should be in a separate commit?
Regards, Simon
rx = NULL; #endif /* diff --git a/include/net.h b/include/net.h index 09d1b51..0396b69 100644 --- a/include/net.h +++ b/include/net.h @@ -355,10 +355,6 @@ extern uchar NetEtherNullAddr[6]; extern ushort NetOurVLAN; /* Our VLAN */ extern ushort NetOurNativeVLAN; /* Our Native VLAN */
-extern uchar NetCDPAddr[6]; /* Ethernet CDP address */ -extern ushort CDPNativeVLAN; /* CDP returned native VLAN */ -extern ushort CDPApplianceVLAN; /* CDP returned appliance VLAN */
extern int NetState; /* Network loop state */ #define NETLOOP_CONTINUE 1 #define NETLOOP_RESTART 2 @@ -386,8 +382,9 @@ extern IPaddr_t NetPingIP; /* the ip address to ping */
#if defined(CONFIG_CMD_CDP) /* when CDP completes these hold the return values */ -extern ushort CDPNativeVLAN; -extern ushort CDPApplianceVLAN; +extern ushort CDPNativeVLAN; /* CDP returned native VLAN */ +extern ushort CDPApplianceVLAN; /* CDP returned appliance VLAN */ +int is_cdp_packet(const uchar *et_addr); #endif
#if defined(CONFIG_CMD_SNTP) diff --git a/net/Makefile b/net/Makefile index 5901046..b350bfc 100644 --- a/net/Makefile +++ b/net/Makefile @@ -28,6 +28,7 @@ include $(TOPDIR)/config.mk LIB = $(obj)libnet.o
COBJS-$(CONFIG_CMD_NET) += bootp.o +COBJS-$(CONFIG_CMD_CDP) += cdp.o COBJS-$(CONFIG_CMD_DNS) += dns.o COBJS-$(CONFIG_CMD_NET) += eth.o COBJS-$(CONFIG_CMD_NET) += net.o diff --git a/net/cdp.c b/net/cdp.c new file mode 100644 index 0000000..c5e54b2 --- /dev/null +++ b/net/cdp.c @@ -0,0 +1,380 @@ +/*
- Copied from Linux Monitor (LiMon) - Networking.
- Copyright 1994 - 2000 Neil Russell.
- (See License)
- Copyright 2000 Roland Borde
- Copyright 2000 Paolo Scaffardi
- Copyright 2000-2002 Wolfgang Denk, wd@denx.de
- */
+#include <common.h> +#include <net.h> +#if defined(CONFIG_CDP_VERSION) +#include <timestamp.h> +#endif
+#include "cdp.h"
+/* Ethernet bcast address */ +static const uchar NetCDPAddr[6] = { 0x01, 0x00, 0x0c, 0xcc, 0xcc, 0xcc };
+#define CDP_DEVICE_ID_TLV 0x0001 +#define CDP_ADDRESS_TLV 0x0002 +#define CDP_PORT_ID_TLV 0x0003 +#define CDP_CAPABILITIES_TLV 0x0004 +#define CDP_VERSION_TLV 0x0005 +#define CDP_PLATFORM_TLV 0x0006 +#define CDP_NATIVE_VLAN_TLV 0x000a +#define CDP_APPLIANCE_VLAN_TLV 0x000e +#define CDP_TRIGGER_TLV 0x000f +#define CDP_POWER_CONSUMPTION_TLV 0x0010 +#define CDP_SYSNAME_TLV 0x0014 +#define CDP_SYSOBJECT_TLV 0x0015 +#define CDP_MANAGEMENT_ADDRESS_TLV 0x0016
+#define CDP_TIMEOUT 250UL /* one packet every 250ms */
+static int CDPSeq; +static int CDPOK;
+ushort CDPNativeVLAN; +ushort CDPApplianceVLAN;
+static const uchar CDP_SNAP_hdr[8] = { 0xAA, 0xAA, 0x03, 0x00, 0x00, 0x0C, 0x20,
- 0x00 };
+static ushort +CDP_compute_csum(const uchar *buff, ushort len) +{
- ushort csum;
- int odd;
- ulong result = 0;
- ushort leftover;
- ushort *p;
- if (len > 0) {
- odd = 1 & (ulong)buff;
- if (odd) {
- result = *buff << 8;
- len--;
- buff++;
- }
- while (len > 1) {
- p = (ushort *)buff;
- result += *p++;
- buff = (uchar *)p;
- if (result & 0x80000000)
- result = (result & 0xFFFF) + (result >> 16);
- len -= 2;
- }
- if (len) {
- leftover = (signed short)(*(const signed char *)buff);
- /* CISCO SUCKS big time! (and blows too):
- * CDP uses the IP checksum algorithm with a twist;
- * for the last byte it *sign* extends and sums.
- */
- result = (result & 0xffff0000) |
- ((result + leftover) & 0x0000ffff);
- }
- while (result >> 16)
- result = (result & 0xFFFF) + (result >> 16);
- if (odd)
- result = ((result >> 8) & 0xff) |
- ((result & 0xff) << 8);
- }
- /* add up 16-bit and 17-bit words for 17+c bits */
- result = (result & 0xffff) + (result >> 16);
- /* add up 16-bit and 2-bit for 16+c bit */
- result = (result & 0xffff) + (result >> 16);
- /* add up carry.. */
- result = (result & 0xffff) + (result >> 16);
- /* negate */
- csum = ~(ushort)result;
- /* run time endian detection */
- if (csum != htons(csum)) /* little endian */
- csum = htons(csum);
- return csum;
+}
+static int +CDPSendTrigger(void) +{
- uchar *pkt;
- ushort *s;
- ushort *cp;
- Ethernet_t *et;
- int len;
- ushort chksum;
+#if defined(CONFIG_CDP_DEVICE_ID) || defined(CONFIG_CDP_PORT_ID) || \
- defined(CONFIG_CDP_VERSION) || defined(CONFIG_CDP_PLATFORM)
- char buf[32];
+#endif
- pkt = NetTxPacket;
- et = (Ethernet_t *)pkt;
- /* NOTE: trigger sent not on any VLAN */
- /* form ethernet header */
- memcpy(et->et_dest, NetCDPAddr, 6);
- memcpy(et->et_src, NetOurEther, 6);
- pkt += ETHER_HDR_SIZE;
- /* SNAP header */
- memcpy((uchar *)pkt, CDP_SNAP_hdr, sizeof(CDP_SNAP_hdr));
- pkt += sizeof(CDP_SNAP_hdr);
- /* CDP header */
- *pkt++ = 0x02; /* CDP version 2 */
- *pkt++ = 180; /* TTL */
- s = (ushort *)pkt;
- cp = s;
- /* checksum (0 for later calculation) */
- *s++ = htons(0);
- /* CDP fields */
+#ifdef CONFIG_CDP_DEVICE_ID
- *s++ = htons(CDP_DEVICE_ID_TLV);
- *s++ = htons(CONFIG_CDP_DEVICE_ID);
- sprintf(buf, CONFIG_CDP_DEVICE_ID_PREFIX "%pm", NetOurEther);
- memcpy((uchar *)s, buf, 16);
- s += 16 / 2;
+#endif
+#ifdef CONFIG_CDP_PORT_ID
- *s++ = htons(CDP_PORT_ID_TLV);
- memset(buf, 0, sizeof(buf));
- sprintf(buf, CONFIG_CDP_PORT_ID, eth_get_dev_index());
- len = strlen(buf);
- if (len & 1) /* make it even */
- len++;
- *s++ = htons(len + 4);
- memcpy((uchar *)s, buf, len);
- s += len / 2;
+#endif
+#ifdef CONFIG_CDP_CAPABILITIES
- *s++ = htons(CDP_CAPABILITIES_TLV);
- *s++ = htons(8);
- *(ulong *)s = htonl(CONFIG_CDP_CAPABILITIES);
- s += 2;
+#endif
+#ifdef CONFIG_CDP_VERSION
- *s++ = htons(CDP_VERSION_TLV);
- memset(buf, 0, sizeof(buf));
- strcpy(buf, CONFIG_CDP_VERSION);
- len = strlen(buf);
- if (len & 1) /* make it even */
- len++;
- *s++ = htons(len + 4);
- memcpy((uchar *)s, buf, len);
- s += len / 2;
+#endif
+#ifdef CONFIG_CDP_PLATFORM
- *s++ = htons(CDP_PLATFORM_TLV);
- memset(buf, 0, sizeof(buf));
- strcpy(buf, CONFIG_CDP_PLATFORM);
- len = strlen(buf);
- if (len & 1) /* make it even */
- len++;
- *s++ = htons(len + 4);
- memcpy((uchar *)s, buf, len);
- s += len / 2;
+#endif
+#ifdef CONFIG_CDP_TRIGGER
- *s++ = htons(CDP_TRIGGER_TLV);
- *s++ = htons(8);
- *(ulong *)s = htonl(CONFIG_CDP_TRIGGER);
- s += 2;
+#endif
+#ifdef CONFIG_CDP_POWER_CONSUMPTION
- *s++ = htons(CDP_POWER_CONSUMPTION_TLV);
- *s++ = htons(6);
- *s++ = htons(CONFIG_CDP_POWER_CONSUMPTION);
+#endif
- /* length of ethernet packet */
- len = (uchar *)s - ((uchar *)NetTxPacket + ETHER_HDR_SIZE);
- et->et_protlen = htons(len);
- len = ETHER_HDR_SIZE + sizeof(CDP_SNAP_hdr);
- chksum = CDP_compute_csum((uchar *)NetTxPacket + len,
- (uchar *)s - (NetTxPacket + len));
- if (chksum == 0)
- chksum = 0xFFFF;
- *cp = htons(chksum);
- (void) eth_send(NetTxPacket, (uchar *)s - NetTxPacket);
- return 0;
+}
+static void +CDPTimeout(void) +{
- CDPSeq++;
- if (CDPSeq < 3) {
- NetSetTimeout(CDP_TIMEOUT, CDPTimeout);
- CDPSendTrigger();
- return;
- }
- /* if not OK try again */
- if (!CDPOK)
- NetStartAgain();
- else
- NetState = NETLOOP_SUCCESS;
+}
+static void +CDPDummyHandler(uchar *pkt, unsigned dest, IPaddr_t sip, unsigned src,
- unsigned len)
+{
- /* nothing */
+}
+void +CDPHandler(const uchar *pkt, unsigned len) +{
- const uchar *t;
- const ushort *ss;
- ushort type, tlen;
- ushort vlan, nvlan;
- /* minimum size? */
- if (len < sizeof(CDP_SNAP_hdr) + 4)
- goto pkt_short;
- /* check for valid CDP SNAP header */
- if (memcmp(pkt, CDP_SNAP_hdr, sizeof(CDP_SNAP_hdr)) != 0)
- return;
- pkt += sizeof(CDP_SNAP_hdr);
- len -= sizeof(CDP_SNAP_hdr);
- /* Version of CDP protocol must be >= 2 and TTL != 0 */
- if (pkt[0] < 0x02 || pkt[1] == 0)
- return;
- /*
- * if version is greater than 0x02 maybe we'll have a problem;
- * output a warning
- */
- if (pkt[0] != 0x02)
- printf("**WARNING: CDP packet received with a protocol version "
- "%d > 2\n", pkt[0] & 0xff);
- if (CDP_compute_csum(pkt, len) != 0)
- return;
- pkt += 4;
- len -= 4;
- vlan = htons(-1);
- nvlan = htons(-1);
- while (len > 0) {
- if (len < 4)
- goto pkt_short;
- ss = (const ushort *)pkt;
- type = ntohs(ss[0]);
- tlen = ntohs(ss[1]);
- if (tlen > len)
- goto pkt_short;
- pkt += tlen;
- len -= tlen;
- ss += 2; /* point ss to the data of the TLV */
- tlen -= 4;
- switch (type) {
- case CDP_DEVICE_ID_TLV:
- break;
- case CDP_ADDRESS_TLV:
- break;
- case CDP_PORT_ID_TLV:
- break;
- case CDP_CAPABILITIES_TLV:
- break;
- case CDP_VERSION_TLV:
- break;
- case CDP_PLATFORM_TLV:
- break;
- case CDP_NATIVE_VLAN_TLV:
- nvlan = *ss;
- break;
- case CDP_APPLIANCE_VLAN_TLV:
- t = (const uchar *)ss;
- while (tlen > 0) {
- if (tlen < 3)
- goto pkt_short;
- ss = (const ushort *)(t + 1);
+#ifdef CONFIG_CDP_APPLIANCE_VLAN_TYPE
- if (t[0] == CONFIG_CDP_APPLIANCE_VLAN_TYPE)
- vlan = *ss;
+#else
- /* XXX will this work; dunno */
- vlan = ntohs(*ss);
+#endif
- t += 3; tlen -= 3;
- }
- break;
- case CDP_TRIGGER_TLV:
- break;
- case CDP_POWER_CONSUMPTION_TLV:
- break;
- case CDP_SYSNAME_TLV:
- break;
- case CDP_SYSOBJECT_TLV:
- break;
- case CDP_MANAGEMENT_ADDRESS_TLV:
- break;
- }
- }
- CDPApplianceVLAN = vlan;
- CDPNativeVLAN = nvlan;
- CDPOK = 1;
- return;
- pkt_short:
- printf("** CDP packet is too short\n");
- return;
+}
+void +CDPStart(void) +{
- printf("Using %s device\n", eth_get_name());
- CDPSeq = 0;
- CDPOK = 0;
- CDPNativeVLAN = htons(-1);
- CDPApplianceVLAN = htons(-1);
- NetSetTimeout(CDP_TIMEOUT, CDPTimeout);
- NetSetHandler(CDPDummyHandler);
- CDPSendTrigger();
+}
+int +is_cdp_packet(const uchar *et_addr) +{
- return memcmp(et_addr, NetCDPAddr, 6) == 0;
+} diff --git a/net/cdp.h b/net/cdp.h new file mode 100644 index 0000000..fef744e --- /dev/null +++ b/net/cdp.h @@ -0,0 +1,18 @@ +/*
- Copied from Linux Monitor (LiMon) - Networking.
- Copyright 1994 - 2000 Neil Russell.
- (See License)
- Copyright 2000 Roland Borde
- Copyright 2000 Paolo Scaffardi
- Copyright 2000-2002 Wolfgang Denk, wd@denx.de
- */
+#ifndef __CDP_H__ +#define __CDP_H__
+void CDPStart(void); +void CDPHandler(const uchar *pkt, unsigned len);
+#endif /* __CDP_H__ */
diff --git a/net/net.c b/net/net.c index 1d6252f..48d3ca8 100644 --- a/net/net.c +++ b/net/net.c @@ -91,8 +91,8 @@ #if defined(CONFIG_CMD_SNTP) #include "sntp.h" #endif -#if defined(CONFIG_CDP_VERSION) -#include <timestamp.h> +#if defined(CONFIG_CMD_CDP) +#include "cdp.h" #endif #if defined(CONFIG_CMD_DNS) #include "dns.h" @@ -163,10 +163,6 @@ uchar NetEtherNullAddr[6]; #ifdef CONFIG_API void (*push_packet)(volatile void *, int len) = 0; #endif -#if defined(CONFIG_CMD_CDP) -/* Ethernet bcast address */ -uchar NetCDPAddr[6] = { 0x01, 0x00, 0x0c, 0xcc, 0xcc, 0xcc }; -#endif /* Network loop state */ int NetState; /* Tried all network devices */ @@ -192,10 +188,6 @@ IPaddr_t NetPingIP; static void PingStart(void); #endif
-#if defined(CONFIG_CMD_CDP) -static void CDPStart(void); -#endif
#if defined(CONFIG_CMD_SNTP) /* NTP server IP address */ IPaddr_t NetNtpServerIP; @@ -841,360 +833,6 @@ static void PingStart(void) } #endif
-#if defined(CONFIG_CMD_CDP)
-#define CDP_DEVICE_ID_TLV 0x0001 -#define CDP_ADDRESS_TLV 0x0002 -#define CDP_PORT_ID_TLV 0x0003 -#define CDP_CAPABILITIES_TLV 0x0004 -#define CDP_VERSION_TLV 0x0005 -#define CDP_PLATFORM_TLV 0x0006 -#define CDP_NATIVE_VLAN_TLV 0x000a -#define CDP_APPLIANCE_VLAN_TLV 0x000e -#define CDP_TRIGGER_TLV 0x000f -#define CDP_POWER_CONSUMPTION_TLV 0x0010 -#define CDP_SYSNAME_TLV 0x0014 -#define CDP_SYSOBJECT_TLV 0x0015 -#define CDP_MANAGEMENT_ADDRESS_TLV 0x0016
-#define CDP_TIMEOUT 250UL /* one packet every 250ms */
-static int CDPSeq; -static int CDPOK;
-ushort CDPNativeVLAN; -ushort CDPApplianceVLAN;
-static const uchar CDP_SNAP_hdr[8] = { 0xAA, 0xAA, 0x03, 0x00, 0x00, 0x0C, 0x20,
- 0x00 };
-static ushort CDP_compute_csum(const uchar *buff, ushort len) -{
- ushort csum;
- int odd;
- ulong result = 0;
- ushort leftover;
- ushort *p;
- if (len > 0) {
- odd = 1 & (ulong)buff;
- if (odd) {
- result = *buff << 8;
- len--;
- buff++;
- }
- while (len > 1) {
- p = (ushort *)buff;
- result += *p++;
- buff = (uchar *)p;
- if (result & 0x80000000)
- result = (result & 0xFFFF) + (result >> 16);
- len -= 2;
- }
- if (len) {
- leftover = (signed short)(*(const signed char *)buff);
- /* CISCO SUCKS big time! (and blows too):
- * CDP uses the IP checksum algorithm with a twist;
- * for the last byte it *sign* extends and sums.
- */
- result = (result & 0xffff0000) |
- ((result + leftover) & 0x0000ffff);
- }
- while (result >> 16)
- result = (result & 0xFFFF) + (result >> 16);
- if (odd)
- result = ((result >> 8) & 0xff) |
- ((result & 0xff) << 8);
- }
- /* add up 16-bit and 17-bit words for 17+c bits */
- result = (result & 0xffff) + (result >> 16);
- /* add up 16-bit and 2-bit for 16+c bit */
- result = (result & 0xffff) + (result >> 16);
- /* add up carry.. */
- result = (result & 0xffff) + (result >> 16);
- /* negate */
- csum = ~(ushort)result;
- /* run time endian detection */
- if (csum != htons(csum)) /* little endian */
- csum = htons(csum);
- return csum;
-}
-int CDPSendTrigger(void) -{
- uchar *pkt;
- ushort *s;
- ushort *cp;
- Ethernet_t *et;
- int len;
- ushort chksum;
-#if defined(CONFIG_CDP_DEVICE_ID) || defined(CONFIG_CDP_PORT_ID) || \
- defined(CONFIG_CDP_VERSION) || defined(CONFIG_CDP_PLATFORM)
- char buf[32];
-#endif
- pkt = NetTxPacket;
- et = (Ethernet_t *)pkt;
- /* NOTE: trigger sent not on any VLAN */
- /* form ethernet header */
- memcpy(et->et_dest, NetCDPAddr, 6);
- memcpy(et->et_src, NetOurEther, 6);
- pkt += ETHER_HDR_SIZE;
- /* SNAP header */
- memcpy((uchar *)pkt, CDP_SNAP_hdr, sizeof(CDP_SNAP_hdr));
- pkt += sizeof(CDP_SNAP_hdr);
- /* CDP header */
- *pkt++ = 0x02; /* CDP version 2 */
- *pkt++ = 180; /* TTL */
- s = (ushort *)pkt;
- cp = s;
- /* checksum (0 for later calculation) */
- *s++ = htons(0);
- /* CDP fields */
-#ifdef CONFIG_CDP_DEVICE_ID
- *s++ = htons(CDP_DEVICE_ID_TLV);
- *s++ = htons(CONFIG_CDP_DEVICE_ID);
- sprintf(buf, CONFIG_CDP_DEVICE_ID_PREFIX "%pm", NetOurEther);
- memcpy((uchar *)s, buf, 16);
- s += 16 / 2;
-#endif
-#ifdef CONFIG_CDP_PORT_ID
- *s++ = htons(CDP_PORT_ID_TLV);
- memset(buf, 0, sizeof(buf));
- sprintf(buf, CONFIG_CDP_PORT_ID, eth_get_dev_index());
- len = strlen(buf);
- if (len & 1) /* make it even */
- len++;
- *s++ = htons(len + 4);
- memcpy((uchar *)s, buf, len);
- s += len / 2;
-#endif
-#ifdef CONFIG_CDP_CAPABILITIES
- *s++ = htons(CDP_CAPABILITIES_TLV);
- *s++ = htons(8);
- *(ulong *)s = htonl(CONFIG_CDP_CAPABILITIES);
- s += 2;
-#endif
-#ifdef CONFIG_CDP_VERSION
- *s++ = htons(CDP_VERSION_TLV);
- memset(buf, 0, sizeof(buf));
- strcpy(buf, CONFIG_CDP_VERSION);
- len = strlen(buf);
- if (len & 1) /* make it even */
- len++;
- *s++ = htons(len + 4);
- memcpy((uchar *)s, buf, len);
- s += len / 2;
-#endif
-#ifdef CONFIG_CDP_PLATFORM
- *s++ = htons(CDP_PLATFORM_TLV);
- memset(buf, 0, sizeof(buf));
- strcpy(buf, CONFIG_CDP_PLATFORM);
- len = strlen(buf);
- if (len & 1) /* make it even */
- len++;
- *s++ = htons(len + 4);
- memcpy((uchar *)s, buf, len);
- s += len / 2;
-#endif
-#ifdef CONFIG_CDP_TRIGGER
- *s++ = htons(CDP_TRIGGER_TLV);
- *s++ = htons(8);
- *(ulong *)s = htonl(CONFIG_CDP_TRIGGER);
- s += 2;
-#endif
-#ifdef CONFIG_CDP_POWER_CONSUMPTION
- *s++ = htons(CDP_POWER_CONSUMPTION_TLV);
- *s++ = htons(6);
- *s++ = htons(CONFIG_CDP_POWER_CONSUMPTION);
-#endif
- /* length of ethernet packet */
- len = (uchar *)s - ((uchar *)NetTxPacket + ETHER_HDR_SIZE);
- et->et_protlen = htons(len);
- len = ETHER_HDR_SIZE + sizeof(CDP_SNAP_hdr);
- chksum = CDP_compute_csum((uchar *)NetTxPacket + len,
- (uchar *)s - (NetTxPacket + len));
- if (chksum == 0)
- chksum = 0xFFFF;
- *cp = htons(chksum);
- (void) eth_send(NetTxPacket, (uchar *)s - NetTxPacket);
- return 0;
-}
-static void -CDPTimeout(void) -{
- CDPSeq++;
- if (CDPSeq < 3) {
- NetSetTimeout(CDP_TIMEOUT, CDPTimeout);
- CDPSendTrigger();
- return;
- }
- /* if not OK try again */
- if (!CDPOK)
- NetStartAgain();
- else
- NetState = NETLOOP_SUCCESS;
-}
-static void -CDPDummyHandler(uchar *pkt, unsigned dest, IPaddr_t sip, unsigned src,
- unsigned len)
-{
- /* nothing */
-}
-static void -CDPHandler(const uchar *pkt, unsigned len) -{
- const uchar *t;
- const ushort *ss;
- ushort type, tlen;
- ushort vlan, nvlan;
- /* minimum size? */
- if (len < sizeof(CDP_SNAP_hdr) + 4)
- goto pkt_short;
- /* check for valid CDP SNAP header */
- if (memcmp(pkt, CDP_SNAP_hdr, sizeof(CDP_SNAP_hdr)) != 0)
- return;
- pkt += sizeof(CDP_SNAP_hdr);
- len -= sizeof(CDP_SNAP_hdr);
- /* Version of CDP protocol must be >= 2 and TTL != 0 */
- if (pkt[0] < 0x02 || pkt[1] == 0)
- return;
- /*
- * if version is greater than 0x02 maybe we'll have a problem;
- * output a warning
- */
- if (pkt[0] != 0x02)
- printf("**WARNING: CDP packet received with a protocol version "
- "%d > 2\n", pkt[0] & 0xff);
- if (CDP_compute_csum(pkt, len) != 0)
- return;
- pkt += 4;
- len -= 4;
- vlan = htons(-1);
- nvlan = htons(-1);
- while (len > 0) {
- if (len < 4)
- goto pkt_short;
- ss = (const ushort *)pkt;
- type = ntohs(ss[0]);
- tlen = ntohs(ss[1]);
- if (tlen > len)
- goto pkt_short;
- pkt += tlen;
- len -= tlen;
- ss += 2; /* point ss to the data of the TLV */
- tlen -= 4;
- switch (type) {
- case CDP_DEVICE_ID_TLV:
- break;
- case CDP_ADDRESS_TLV:
- break;
- case CDP_PORT_ID_TLV:
- break;
- case CDP_CAPABILITIES_TLV:
- break;
- case CDP_VERSION_TLV:
- break;
- case CDP_PLATFORM_TLV:
- break;
- case CDP_NATIVE_VLAN_TLV:
- nvlan = *ss;
- break;
- case CDP_APPLIANCE_VLAN_TLV:
- t = (const uchar *)ss;
- while (tlen > 0) {
- if (tlen < 3)
- goto pkt_short;
- ss = (const ushort *)(t + 1);
-#ifdef CONFIG_CDP_APPLIANCE_VLAN_TYPE
- if (t[0] == CONFIG_CDP_APPLIANCE_VLAN_TYPE)
- vlan = *ss;
-#else
- /* XXX will this work; dunno */
- vlan = ntohs(*ss);
-#endif
- t += 3; tlen -= 3;
- }
- break;
- case CDP_TRIGGER_TLV:
- break;
- case CDP_POWER_CONSUMPTION_TLV:
- break;
- case CDP_SYSNAME_TLV:
- break;
- case CDP_SYSOBJECT_TLV:
- break;
- case CDP_MANAGEMENT_ADDRESS_TLV:
- break;
- }
- }
- CDPApplianceVLAN = vlan;
- CDPNativeVLAN = nvlan;
- CDPOK = 1;
- return;
- pkt_short:
- printf("** CDP packet is too short\n");
- return;
-}
-static void CDPStart(void) -{
- printf("Using %s device\n", eth_get_name());
- CDPSeq = 0;
- CDPOK = 0;
- CDPNativeVLAN = htons(-1);
- CDPApplianceVLAN = htons(-1);
- NetSetTimeout(CDP_TIMEOUT, CDPTimeout);
- NetSetHandler(CDPDummyHandler);
- CDPSendTrigger();
-} -#endif
#ifdef CONFIG_IP_DEFRAG /* * This function collects fragments in a single packet, according @@ -1463,7 +1101,7 @@ NetReceive(volatile uchar *inpkt, int len)
#if defined(CONFIG_CMD_CDP) /* keep track if packet is CDP */
- iscdp = memcmp(et->et_dest, NetCDPAddr, 6) == 0;
- iscdp = is_cdp_packet(et->et_dest);
#endif
myvlanid = ntohs(NetOurVLAN);
1.6.0.2
U-Boot mailing list U-Boot@lists.denx.de http://lists.denx.de/mailman/listinfo/u-boot

On Thursday 19 January 2012 19:53:08 Joe Hershberger wrote:
--- a/include/net.h +++ b/include/net.h
+int is_cdp_packet(const uchar *et_addr);
seems like this would be better as a static inline since it's just a memcmp
--- /dev/null +++ b/net/cdp.h @@ -0,0 +1,18 @@ +/*
- Copied from Linux Monitor (LiMon) - Networking.
- Copyright 1994 - 2000 Neil Russell.
- (See License)
- Copyright 2000 Roland Borde
- Copyright 2000 Paolo Scaffardi
- Copyright 2000-2002 Wolfgang Denk, wd@denx.de
- */
+#ifndef __CDP_H__ +#define __CDP_H__
+void CDPStart(void); +void CDPHandler(const uchar *pkt, unsigned len);
+#endif /* __CDP_H__ */
no blank newlines at end of files
--- a/net/net.c +++ b/net/net.c
+#if defined(CONFIG_CMD_CDP) +#include "cdp.h" #endif
let's push this ifdef down into cdp.h -mike

Signed-off-by: Joe Hershberger joe.hershberger@ni.com Cc: Joe Hershberger joe.hershberger@gmail.com Cc: Wolfgang Denk wd@denx.de --- include/net.h | 3 +- net/Makefile | 1 + net/arp.c | 213 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++ net/arp.h | 30 ++++++++ net/net.c | 209 ++++---------------------------------------------------- 5 files changed, 259 insertions(+), 197 deletions(-) create mode 100644 net/arp.c create mode 100644 net/arp.h
diff --git a/include/net.h b/include/net.h index 0396b69..2d00233 100644 --- a/include/net.h +++ b/include/net.h @@ -418,7 +418,8 @@ extern void NetSetIP(uchar *, IPaddr_t, int, int, int); extern int NetCksumOk(uchar *, int); /* Return true if cksum OK */ extern uint NetCksum(uchar *, int); /* Calculate the checksum */
-/* Set callbacks */ +/* Callbacks */ +extern rxhand_f *NetGetHandler(void); /* Get RX packet handler */ extern void NetSetHandler(rxhand_f *); /* Set RX packet handler */ extern void net_set_icmp_handler(rxhand_icmp_f *f); /* Set ICMP RX handler */ extern void NetSetTimeout(ulong, thand_f *);/* Set timeout handler */ diff --git a/net/Makefile b/net/Makefile index b350bfc..0916a56 100644 --- a/net/Makefile +++ b/net/Makefile @@ -27,6 +27,7 @@ include $(TOPDIR)/config.mk
LIB = $(obj)libnet.o
+COBJS-$(CONFIG_CMD_NET) += arp.o COBJS-$(CONFIG_CMD_NET) += bootp.o COBJS-$(CONFIG_CMD_CDP) += cdp.o COBJS-$(CONFIG_CMD_DNS) += dns.o diff --git a/net/arp.c b/net/arp.c new file mode 100644 index 0000000..f75217c --- /dev/null +++ b/net/arp.c @@ -0,0 +1,213 @@ +/* + * Copied from Linux Monitor (LiMon) - Networking. + * + * Copyright 1994 - 2000 Neil Russell. + * (See License) + * Copyright 2000 Roland Borde + * Copyright 2000 Paolo Scaffardi + * Copyright 2000-2002 Wolfgang Denk, wd@denx.de + */ + +#include <common.h> + +#include "arp.h" + +#ifndef CONFIG_ARP_TIMEOUT +/* Milliseconds before trying ARP again */ +# define ARP_TIMEOUT 5000UL +#else +# define ARP_TIMEOUT CONFIG_ARP_TIMEOUT +#endif + + +#ifndef CONFIG_NET_RETRY_COUNT +# define ARP_TIMEOUT_COUNT 5 /* # of timeouts before giving up */ +#else +# define ARP_TIMEOUT_COUNT CONFIG_NET_RETRY_COUNT +#endif + +IPaddr_t NetArpWaitPacketIP; +IPaddr_t NetArpWaitReplyIP; +/* MAC address of waiting packet's destination */ +uchar *NetArpWaitPacketMAC; +/* THE transmit packet */ +uchar *NetArpWaitTxPacket; +int NetArpWaitTxPacketSize; +uchar NetArpWaitPacketBuf[PKTSIZE_ALIGN + PKTALIGN]; +ulong NetArpWaitTimerStart; +int NetArpWaitTry; + +void ArpInit(void) +{ + /* XXX problem with bss workaround */ + NetArpWaitPacketMAC = NULL; + NetArpWaitPacketIP = 0; + NetArpWaitReplyIP = 0; + NetArpWaitTxPacket = &NetArpWaitPacketBuf[0] + (PKTALIGN - 1); + NetArpWaitTxPacket -= (ulong)NetArpWaitTxPacket % PKTALIGN; + NetArpWaitTxPacketSize = 0; +} + +void ArpRequest(void) +{ + uchar *pkt; + ARP_t *arp; + + debug("ARP broadcast %d\n", NetArpWaitTry); + + pkt = NetTxPacket; + + pkt += NetSetEther(pkt, NetBcastAddr, PROT_ARP); + + arp = (ARP_t *) pkt; + + arp->ar_hrd = htons(ARP_ETHER); + arp->ar_pro = htons(PROT_IP); + arp->ar_hln = 6; + arp->ar_pln = 4; + arp->ar_op = htons(ARPOP_REQUEST); + + /* source ET addr */ + memcpy(&arp->ar_data[0], NetOurEther, 6); + /* source IP addr */ + NetWriteIP((uchar *) &arp->ar_data[6], NetOurIP); + /* dest ET addr = 0 */ + memset(&arp->ar_data[10], '\0', 6); + if ((NetArpWaitPacketIP & NetOurSubnetMask) != + (NetOurIP & NetOurSubnetMask)) { + if (NetOurGatewayIP == 0) { + puts("## Warning: gatewayip needed but not set\n"); + NetArpWaitReplyIP = NetArpWaitPacketIP; + } else { + NetArpWaitReplyIP = NetOurGatewayIP; + } + } else { + NetArpWaitReplyIP = NetArpWaitPacketIP; + } + + NetWriteIP((uchar *) &arp->ar_data[16], NetArpWaitReplyIP); + (void) eth_send(NetTxPacket, (pkt - NetTxPacket) + ARP_HDR_SIZE); +} + +void ArpTimeoutCheck(void) +{ + ulong t; + + if (!NetArpWaitPacketIP) + return; + + t = get_timer(0); + + /* check for arp timeout */ + if ((t - NetArpWaitTimerStart) > ARP_TIMEOUT) { + NetArpWaitTry++; + + if (NetArpWaitTry >= ARP_TIMEOUT_COUNT) { + puts("\nARP Retry count exceeded; starting again\n"); + NetArpWaitTry = 0; + NetStartAgain(); + } else { + NetArpWaitTimerStart = t; + ArpRequest(); + } + } +} + +void ArpReceive(Ethernet_t *et, IP_t *ip, int len) +{ + ARP_t *arp; + IPaddr_t tmp; + uchar *pkt; + + /* + * We have to deal with two types of ARP packets: + * - REQUEST packets will be answered by sending our + * IP address - if we know it. + * - REPLY packates are expected only after we asked + * for the TFTP server's or the gateway's ethernet + * address; so if we receive such a packet, we set + * the server ethernet address + */ + debug("Got ARP\n"); + + arp = (ARP_t *)ip; + if (len < ARP_HDR_SIZE) { + printf("bad length %d < %d\n", len, ARP_HDR_SIZE); + return; + } + if (ntohs(arp->ar_hrd) != ARP_ETHER) + return; + if (ntohs(arp->ar_pro) != PROT_IP) + return; + if (arp->ar_hln != 6) + return; + if (arp->ar_pln != 4) + return; + + if (NetOurIP == 0) + return; + + if (NetReadIP(&arp->ar_data[16]) != NetOurIP) + return; + + switch (ntohs(arp->ar_op)) { + case ARPOP_REQUEST: + /* reply with our IP address */ + debug("Got ARP REQUEST, return our IP\n"); + pkt = (uchar *)et; + pkt += NetSetEther(pkt, et->et_src, PROT_ARP); + arp->ar_op = htons(ARPOP_REPLY); + memcpy(&arp->ar_data[10], &arp->ar_data[0], 6); + NetCopyIP(&arp->ar_data[16], &arp->ar_data[6]); + memcpy(&arp->ar_data[0], NetOurEther, 6); + NetCopyIP(&arp->ar_data[6], &NetOurIP); + (void) eth_send((uchar *)et, + (pkt - (uchar *)et) + ARP_HDR_SIZE); + return; + + case ARPOP_REPLY: /* arp reply */ + /* are we waiting for a reply */ + if (!NetArpWaitPacketIP || !NetArpWaitPacketMAC) + break; + +#ifdef CONFIG_KEEP_SERVERADDR + if (NetServerIP == NetArpWaitPacketIP) { + char buf[20]; + sprintf(buf, "%pM", arp->ar_data); + setenv("serveraddr", buf); + } +#endif + + tmp = NetReadIP(&arp->ar_data[6]); + + /* matched waiting packet's address */ + if (tmp == NetArpWaitReplyIP) { + debug("Got ARP REPLY, set eth addr (%pM)\n", + arp->ar_data); + + /* save address for later use */ + memcpy(NetArpWaitPacketMAC, + &arp->ar_data[0], 6); + +#ifdef CONFIG_NETCONSOLE + NetGetHandler()(0, 0, 0, 0, 0); +#endif + /* modify header, and transmit it */ + memcpy(((Ethernet_t *)NetArpWaitTxPacket)-> + et_dest, NetArpWaitPacketMAC, 6); + (void) eth_send(NetArpWaitTxPacket, + NetArpWaitTxPacketSize); + + /* no arp request pending now */ + NetArpWaitPacketIP = 0; + NetArpWaitTxPacketSize = 0; + NetArpWaitPacketMAC = NULL; + + } + return; + default: + debug("Unexpected ARP opcode 0x%x\n", + ntohs(arp->ar_op)); + return; + } +} diff --git a/net/arp.h b/net/arp.h new file mode 100644 index 0000000..09c763d --- /dev/null +++ b/net/arp.h @@ -0,0 +1,30 @@ +/* + * Copied from Linux Monitor (LiMon) - Networking. + * + * Copyright 1994 - 2000 Neil Russell. + * (See License) + * Copyright 2000 Roland Borde + * Copyright 2000 Paolo Scaffardi + * Copyright 2000-2002 Wolfgang Denk, wd@denx.de + */ + +#ifndef __ARP_H__ +#define __ARP_H__ + +#include <net.h> + +extern IPaddr_t NetArpWaitPacketIP; +/* MAC address of waiting packet's destination */ +extern uchar *NetArpWaitPacketMAC; +/* THE transmit packet */ +extern uchar *NetArpWaitTxPacket; +extern int NetArpWaitTxPacketSize; +extern ulong NetArpWaitTimerStart; +extern int NetArpWaitTry; + +void ArpInit(void); +void ArpRequest(void); +void ArpTimeoutCheck(void); +void ArpReceive(Ethernet_t *et, IP_t *ip, int len); + +#endif /* __ARP_H__ */ diff --git a/net/net.c b/net/net.c index 48d3ca8..2dae5a0 100644 --- a/net/net.c +++ b/net/net.c @@ -78,6 +78,7 @@ #include <watchdog.h> #include <command.h> #include <net.h> +#include "arp.h" #include "bootp.h" #include "tftp.h" #ifdef CONFIG_CMD_RARP @@ -100,20 +101,6 @@
DECLARE_GLOBAL_DATA_PTR;
-#ifndef CONFIG_ARP_TIMEOUT -/* Milliseconds before trying ARP again */ -# define ARP_TIMEOUT 5000UL -#else -# define ARP_TIMEOUT CONFIG_ARP_TIMEOUT -#endif - - -#ifndef CONFIG_NET_RETRY_COUNT -# define ARP_TIMEOUT_COUNT 5 /* # of timeouts before giving up */ -#else -# define ARP_TIMEOUT_COUNT CONFIG_NET_RETRY_COUNT -#endif - /** BOOTP EXTENTIONS **/
/* Our subnet mask (0=unknown) */ @@ -220,82 +207,6 @@ static int NetTryCount;
/**********************************************************************/
-IPaddr_t NetArpWaitPacketIP; -IPaddr_t NetArpWaitReplyIP; -/* MAC address of waiting packet's destination */ -uchar *NetArpWaitPacketMAC; -/* THE transmit packet */ -uchar *NetArpWaitTxPacket; -int NetArpWaitTxPacketSize; -uchar NetArpWaitPacketBuf[PKTSIZE_ALIGN + PKTALIGN]; -ulong NetArpWaitTimerStart; -int NetArpWaitTry; - -void ArpRequest(void) -{ - uchar *pkt; - ARP_t *arp; - - debug("ARP broadcast %d\n", NetArpWaitTry); - - pkt = NetTxPacket; - - pkt += NetSetEther(pkt, NetBcastAddr, PROT_ARP); - - arp = (ARP_t *) pkt; - - arp->ar_hrd = htons(ARP_ETHER); - arp->ar_pro = htons(PROT_IP); - arp->ar_hln = 6; - arp->ar_pln = 4; - arp->ar_op = htons(ARPOP_REQUEST); - - /* source ET addr */ - memcpy(&arp->ar_data[0], NetOurEther, 6); - /* source IP addr */ - NetWriteIP((uchar *) &arp->ar_data[6], NetOurIP); - /* dest ET addr = 0 */ - memset(&arp->ar_data[10], '\0', 6); - if ((NetArpWaitPacketIP & NetOurSubnetMask) != - (NetOurIP & NetOurSubnetMask)) { - if (NetOurGatewayIP == 0) { - puts("## Warning: gatewayip needed but not set\n"); - NetArpWaitReplyIP = NetArpWaitPacketIP; - } else { - NetArpWaitReplyIP = NetOurGatewayIP; - } - } else { - NetArpWaitReplyIP = NetArpWaitPacketIP; - } - - NetWriteIP((uchar *) &arp->ar_data[16], NetArpWaitReplyIP); - (void) eth_send(NetTxPacket, (pkt - NetTxPacket) + ARP_HDR_SIZE); -} - -void ArpTimeoutCheck(void) -{ - ulong t; - - if (!NetArpWaitPacketIP) - return; - - t = get_timer(0); - - /* check for arp timeout */ - if ((t - NetArpWaitTimerStart) > ARP_TIMEOUT) { - NetArpWaitTry++; - - if (NetArpWaitTry >= ARP_TIMEOUT_COUNT) { - puts("\nARP Retry count exceeded; starting again\n"); - NetArpWaitTry = 0; - NetStartAgain(); - } else { - NetArpWaitTimerStart = t; - ArpRequest(); - } - } -} - /* * Check if autoload is enabled. If so, use either NFS or TFTP to download * the boot file. @@ -363,15 +274,11 @@ int NetLoop(enum proto_t protocol) NetRestarted = 0; NetDevExists = 0;
- /* XXX problem with bss workaround */ - NetArpWaitPacketMAC = NULL; - NetArpWaitTxPacket = NULL; - NetArpWaitPacketIP = 0; - NetArpWaitReplyIP = 0; - NetArpWaitTxPacket = NULL; NetTxPacket = NULL; NetTryCount = 1;
+ ArpInit(); + if (!NetTxPacket) { int i; /* @@ -383,12 +290,6 @@ int NetLoop(enum proto_t protocol) NetRxPackets[i] = NetTxPacket + (i+1)*PKTSIZE_ALIGN; }
- if (!NetArpWaitTxPacket) { - NetArpWaitTxPacket = &NetArpWaitPacketBuf[0] + (PKTALIGN - 1); - NetArpWaitTxPacket -= (ulong)NetArpWaitTxPacket % PKTALIGN; - NetArpWaitTxPacketSize = 0; - } - eth_halt(); eth_set_current(); if (eth_init(bd) < 0) { @@ -661,6 +562,13 @@ void NetStartAgain(void) * Miscelaneous bits. */
+rxhand_f * +NetGetHandler(void) +{ + return packetHandler; +} + + void NetSetHandler(rxhand_f *f) { @@ -1072,11 +980,12 @@ NetReceive(volatile uchar *inpkt, int len) { Ethernet_t *et; IP_t *ip; +#ifdef CONFIG_CMD_RARP ARP_t *arp; +#endif IPaddr_t tmp; IPaddr_t src_ip; int x; - uchar *pkt; #if defined(CONFIG_CMD_CDP) int iscdp; #endif @@ -1173,99 +1082,7 @@ NetReceive(volatile uchar *inpkt, int len) switch (x) {
case PROT_ARP: - /* - * We have to deal with two types of ARP packets: - * - REQUEST packets will be answered by sending our - * IP address - if we know it. - * - REPLY packates are expected only after we asked - * for the TFTP server's or the gateway's ethernet - * address; so if we receive such a packet, we set - * the server ethernet address - */ - debug("Got ARP\n"); - - arp = (ARP_t *)ip; - if (len < ARP_HDR_SIZE) { - printf("bad length %d < %d\n", len, ARP_HDR_SIZE); - return; - } - if (ntohs(arp->ar_hrd) != ARP_ETHER) - return; - if (ntohs(arp->ar_pro) != PROT_IP) - return; - if (arp->ar_hln != 6) - return; - if (arp->ar_pln != 4) - return; - - if (NetOurIP == 0) - return; - - if (NetReadIP(&arp->ar_data[16]) != NetOurIP) - return; - - switch (ntohs(arp->ar_op)) { - case ARPOP_REQUEST: - /* reply with our IP address */ - debug("Got ARP REQUEST, return our IP\n"); - pkt = (uchar *)et; - pkt += NetSetEther(pkt, et->et_src, PROT_ARP); - arp->ar_op = htons(ARPOP_REPLY); - memcpy(&arp->ar_data[10], &arp->ar_data[0], 6); - NetCopyIP(&arp->ar_data[16], &arp->ar_data[6]); - memcpy(&arp->ar_data[0], NetOurEther, 6); - NetCopyIP(&arp->ar_data[6], &NetOurIP); - (void) eth_send((uchar *)et, - (pkt - (uchar *)et) + ARP_HDR_SIZE); - return; - - case ARPOP_REPLY: /* arp reply */ - /* are we waiting for a reply */ - if (!NetArpWaitPacketIP || !NetArpWaitPacketMAC) - break; - -#ifdef CONFIG_KEEP_SERVERADDR - if (NetServerIP == NetArpWaitPacketIP) { - char buf[20]; - sprintf(buf, "%pM", arp->ar_data); - setenv("serveraddr", buf); - } -#endif - - debug("Got ARP REPLY, set server/gtwy eth addr (%pM)\n", - arp->ar_data); - - tmp = NetReadIP(&arp->ar_data[6]); - - /* matched waiting packet's address */ - if (tmp == NetArpWaitReplyIP) { - debug("Got it\n"); - - /* save address for later use */ - memcpy(NetArpWaitPacketMAC, - &arp->ar_data[0], 6); - -#ifdef CONFIG_NETCONSOLE - (*packetHandler)(0, 0, 0, 0, 0); -#endif - /* modify header, and transmit it */ - memcpy(((Ethernet_t *)NetArpWaitTxPacket)-> - et_dest, NetArpWaitPacketMAC, 6); - (void) eth_send(NetArpWaitTxPacket, - NetArpWaitTxPacketSize); - - /* no arp request pending now */ - NetArpWaitPacketIP = 0; - NetArpWaitTxPacketSize = 0; - NetArpWaitPacketMAC = NULL; - - } - return; - default: - debug("Unexpected ARP opcode 0x%x\n", - ntohs(arp->ar_op)); - return; - } + ArpReceive(et, ip, len); break;
#ifdef CONFIG_CMD_RARP

On Thu, Jan 19, 2012 at 4:53 PM, Joe Hershberger joe.hershberger@ni.com wrote:
Signed-off-by: Joe Hershberger joe.hershberger@ni.com
Acked-by: Simon Glass sjg@chromium.org
Cc: Joe Hershberger joe.hershberger@gmail.com Cc: Wolfgang Denk wd@denx.de
include/net.h | 3 +- net/Makefile | 1 + net/arp.c | 213 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++ net/arp.h | 30 ++++++++ net/net.c | 209 ++++---------------------------------------------------- 5 files changed, 259 insertions(+), 197 deletions(-) create mode 100644 net/arp.c create mode 100644 net/arp.h
diff --git a/include/net.h b/include/net.h index 0396b69..2d00233 100644 --- a/include/net.h +++ b/include/net.h @@ -418,7 +418,8 @@ extern void NetSetIP(uchar *, IPaddr_t, int, int, int); extern int NetCksumOk(uchar *, int); /* Return true if cksum OK */ extern uint NetCksum(uchar *, int); /* Calculate the checksum */
-/* Set callbacks */ +/* Callbacks */ +extern rxhand_f *NetGetHandler(void); /* Get RX packet handler */ extern void NetSetHandler(rxhand_f *); /* Set RX packet handler */ extern void net_set_icmp_handler(rxhand_icmp_f *f); /* Set ICMP RX handler */ extern void NetSetTimeout(ulong, thand_f *);/* Set timeout handler */ diff --git a/net/Makefile b/net/Makefile index b350bfc..0916a56 100644 --- a/net/Makefile +++ b/net/Makefile @@ -27,6 +27,7 @@ include $(TOPDIR)/config.mk
LIB = $(obj)libnet.o
+COBJS-$(CONFIG_CMD_NET) += arp.o COBJS-$(CONFIG_CMD_NET) += bootp.o COBJS-$(CONFIG_CMD_CDP) += cdp.o COBJS-$(CONFIG_CMD_DNS) += dns.o diff --git a/net/arp.c b/net/arp.c new file mode 100644 index 0000000..f75217c --- /dev/null +++ b/net/arp.c @@ -0,0 +1,213 @@ +/*
- Copied from Linux Monitor (LiMon) - Networking.
- Copyright 1994 - 2000 Neil Russell.
- (See License)
- Copyright 2000 Roland Borde
- Copyright 2000 Paolo Scaffardi
- Copyright 2000-2002 Wolfgang Denk, wd@denx.de
- */
+#include <common.h>
+#include "arp.h"
+#ifndef CONFIG_ARP_TIMEOUT +/* Milliseconds before trying ARP again */ +# define ARP_TIMEOUT 5000UL +#else +# define ARP_TIMEOUT CONFIG_ARP_TIMEOUT +#endif
+#ifndef CONFIG_NET_RETRY_COUNT +# define ARP_TIMEOUT_COUNT 5 /* # of timeouts before giving up */ +#else +# define ARP_TIMEOUT_COUNT CONFIG_NET_RETRY_COUNT +#endif
+IPaddr_t NetArpWaitPacketIP; +IPaddr_t NetArpWaitReplyIP; +/* MAC address of waiting packet's destination */ +uchar *NetArpWaitPacketMAC; +/* THE transmit packet */ +uchar *NetArpWaitTxPacket; +int NetArpWaitTxPacketSize; +uchar NetArpWaitPacketBuf[PKTSIZE_ALIGN + PKTALIGN]; +ulong NetArpWaitTimerStart; +int NetArpWaitTry;
+void ArpInit(void) +{
- /* XXX problem with bss workaround */
- NetArpWaitPacketMAC = NULL;
- NetArpWaitPacketIP = 0;
- NetArpWaitReplyIP = 0;
- NetArpWaitTxPacket = &NetArpWaitPacketBuf[0] + (PKTALIGN - 1);
- NetArpWaitTxPacket -= (ulong)NetArpWaitTxPacket % PKTALIGN;
- NetArpWaitTxPacketSize = 0;
+}
+void ArpRequest(void) +{
- uchar *pkt;
- ARP_t *arp;
- debug("ARP broadcast %d\n", NetArpWaitTry);
- pkt = NetTxPacket;
- pkt += NetSetEther(pkt, NetBcastAddr, PROT_ARP);
- arp = (ARP_t *) pkt;
- arp->ar_hrd = htons(ARP_ETHER);
- arp->ar_pro = htons(PROT_IP);
- arp->ar_hln = 6;
- arp->ar_pln = 4;
- arp->ar_op = htons(ARPOP_REQUEST);
- /* source ET addr */
- memcpy(&arp->ar_data[0], NetOurEther, 6);
- /* source IP addr */
- NetWriteIP((uchar *) &arp->ar_data[6], NetOurIP);
- /* dest ET addr = 0 */
- memset(&arp->ar_data[10], '\0', 6);
- if ((NetArpWaitPacketIP & NetOurSubnetMask) !=
- (NetOurIP & NetOurSubnetMask)) {
- if (NetOurGatewayIP == 0) {
- puts("## Warning: gatewayip needed but not set\n");
- NetArpWaitReplyIP = NetArpWaitPacketIP;
- } else {
- NetArpWaitReplyIP = NetOurGatewayIP;
- }
- } else {
- NetArpWaitReplyIP = NetArpWaitPacketIP;
- }
- NetWriteIP((uchar *) &arp->ar_data[16], NetArpWaitReplyIP);
- (void) eth_send(NetTxPacket, (pkt - NetTxPacket) + ARP_HDR_SIZE);
+}
+void ArpTimeoutCheck(void) +{
- ulong t;
- if (!NetArpWaitPacketIP)
- return;
- t = get_timer(0);
- /* check for arp timeout */
- if ((t - NetArpWaitTimerStart) > ARP_TIMEOUT) {
- NetArpWaitTry++;
- if (NetArpWaitTry >= ARP_TIMEOUT_COUNT) {
- puts("\nARP Retry count exceeded; starting again\n");
- NetArpWaitTry = 0;
- NetStartAgain();
- } else {
- NetArpWaitTimerStart = t;
- ArpRequest();
- }
- }
+}
+void ArpReceive(Ethernet_t *et, IP_t *ip, int len) +{
- ARP_t *arp;
- IPaddr_t tmp;
- uchar *pkt;
- /*
- * We have to deal with two types of ARP packets:
- * - REQUEST packets will be answered by sending our
- * IP address - if we know it.
- * - REPLY packates are expected only after we asked
- * for the TFTP server's or the gateway's ethernet
- * address; so if we receive such a packet, we set
- * the server ethernet address
- */
- debug("Got ARP\n");
- arp = (ARP_t *)ip;
- if (len < ARP_HDR_SIZE) {
- printf("bad length %d < %d\n", len, ARP_HDR_SIZE);
- return;
- }
- if (ntohs(arp->ar_hrd) != ARP_ETHER)
- return;
- if (ntohs(arp->ar_pro) != PROT_IP)
- return;
- if (arp->ar_hln != 6)
- return;
- if (arp->ar_pln != 4)
- return;
- if (NetOurIP == 0)
- return;
- if (NetReadIP(&arp->ar_data[16]) != NetOurIP)
- return;
- switch (ntohs(arp->ar_op)) {
- case ARPOP_REQUEST:
- /* reply with our IP address */
- debug("Got ARP REQUEST, return our IP\n");
- pkt = (uchar *)et;
- pkt += NetSetEther(pkt, et->et_src, PROT_ARP);
- arp->ar_op = htons(ARPOP_REPLY);
- memcpy(&arp->ar_data[10], &arp->ar_data[0], 6);
- NetCopyIP(&arp->ar_data[16], &arp->ar_data[6]);
- memcpy(&arp->ar_data[0], NetOurEther, 6);
- NetCopyIP(&arp->ar_data[6], &NetOurIP);
- (void) eth_send((uchar *)et,
- (pkt - (uchar *)et) + ARP_HDR_SIZE);
- return;
- case ARPOP_REPLY: /* arp reply */
- /* are we waiting for a reply */
- if (!NetArpWaitPacketIP || !NetArpWaitPacketMAC)
- break;
+#ifdef CONFIG_KEEP_SERVERADDR
- if (NetServerIP == NetArpWaitPacketIP) {
- char buf[20];
- sprintf(buf, "%pM", arp->ar_data);
- setenv("serveraddr", buf);
- }
+#endif
- tmp = NetReadIP(&arp->ar_data[6]);
- /* matched waiting packet's address */
- if (tmp == NetArpWaitReplyIP) {
- debug("Got ARP REPLY, set eth addr (%pM)\n",
- arp->ar_data);
- /* save address for later use */
- memcpy(NetArpWaitPacketMAC,
- &arp->ar_data[0], 6);
+#ifdef CONFIG_NETCONSOLE
- NetGetHandler()(0, 0, 0, 0, 0);
+#endif
- /* modify header, and transmit it */
- memcpy(((Ethernet_t *)NetArpWaitTxPacket)->
- et_dest, NetArpWaitPacketMAC, 6);
- (void) eth_send(NetArpWaitTxPacket,
- NetArpWaitTxPacketSize);
- /* no arp request pending now */
- NetArpWaitPacketIP = 0;
- NetArpWaitTxPacketSize = 0;
- NetArpWaitPacketMAC = NULL;
- }
- return;
- default:
- debug("Unexpected ARP opcode 0x%x\n",
- ntohs(arp->ar_op));
- return;
- }
+} diff --git a/net/arp.h b/net/arp.h new file mode 100644 index 0000000..09c763d --- /dev/null +++ b/net/arp.h @@ -0,0 +1,30 @@ +/*
- Copied from Linux Monitor (LiMon) - Networking.
- Copyright 1994 - 2000 Neil Russell.
- (See License)
- Copyright 2000 Roland Borde
- Copyright 2000 Paolo Scaffardi
- Copyright 2000-2002 Wolfgang Denk, wd@denx.de
- */
+#ifndef __ARP_H__ +#define __ARP_H__
+#include <net.h>
+extern IPaddr_t NetArpWaitPacketIP; +/* MAC address of waiting packet's destination */ +extern uchar *NetArpWaitPacketMAC; +/* THE transmit packet */ +extern uchar *NetArpWaitTxPacket; +extern int NetArpWaitTxPacketSize; +extern ulong NetArpWaitTimerStart; +extern int NetArpWaitTry;
+void ArpInit(void); +void ArpRequest(void); +void ArpTimeoutCheck(void); +void ArpReceive(Ethernet_t *et, IP_t *ip, int len);
+#endif /* __ARP_H__ */ diff --git a/net/net.c b/net/net.c index 48d3ca8..2dae5a0 100644 --- a/net/net.c +++ b/net/net.c @@ -78,6 +78,7 @@ #include <watchdog.h> #include <command.h> #include <net.h> +#include "arp.h" #include "bootp.h" #include "tftp.h" #ifdef CONFIG_CMD_RARP @@ -100,20 +101,6 @@
DECLARE_GLOBAL_DATA_PTR;
-#ifndef CONFIG_ARP_TIMEOUT -/* Milliseconds before trying ARP again */ -# define ARP_TIMEOUT 5000UL -#else -# define ARP_TIMEOUT CONFIG_ARP_TIMEOUT -#endif
-#ifndef CONFIG_NET_RETRY_COUNT -# define ARP_TIMEOUT_COUNT 5 /* # of timeouts before giving up */ -#else -# define ARP_TIMEOUT_COUNT CONFIG_NET_RETRY_COUNT -#endif
/** BOOTP EXTENTIONS **/
/* Our subnet mask (0=unknown) */ @@ -220,82 +207,6 @@ static int NetTryCount;
/**********************************************************************/
-IPaddr_t NetArpWaitPacketIP; -IPaddr_t NetArpWaitReplyIP; -/* MAC address of waiting packet's destination */ -uchar *NetArpWaitPacketMAC; -/* THE transmit packet */ -uchar *NetArpWaitTxPacket; -int NetArpWaitTxPacketSize; -uchar NetArpWaitPacketBuf[PKTSIZE_ALIGN + PKTALIGN]; -ulong NetArpWaitTimerStart; -int NetArpWaitTry;
-void ArpRequest(void) -{
- uchar *pkt;
- ARP_t *arp;
- debug("ARP broadcast %d\n", NetArpWaitTry);
- pkt = NetTxPacket;
- pkt += NetSetEther(pkt, NetBcastAddr, PROT_ARP);
- arp = (ARP_t *) pkt;
- arp->ar_hrd = htons(ARP_ETHER);
- arp->ar_pro = htons(PROT_IP);
- arp->ar_hln = 6;
- arp->ar_pln = 4;
- arp->ar_op = htons(ARPOP_REQUEST);
- /* source ET addr */
- memcpy(&arp->ar_data[0], NetOurEther, 6);
- /* source IP addr */
- NetWriteIP((uchar *) &arp->ar_data[6], NetOurIP);
- /* dest ET addr = 0 */
- memset(&arp->ar_data[10], '\0', 6);
- if ((NetArpWaitPacketIP & NetOurSubnetMask) !=
- (NetOurIP & NetOurSubnetMask)) {
- if (NetOurGatewayIP == 0) {
- puts("## Warning: gatewayip needed but not set\n");
- NetArpWaitReplyIP = NetArpWaitPacketIP;
- } else {
- NetArpWaitReplyIP = NetOurGatewayIP;
- }
- } else {
- NetArpWaitReplyIP = NetArpWaitPacketIP;
- }
- NetWriteIP((uchar *) &arp->ar_data[16], NetArpWaitReplyIP);
- (void) eth_send(NetTxPacket, (pkt - NetTxPacket) + ARP_HDR_SIZE);
-}
-void ArpTimeoutCheck(void) -{
- ulong t;
- if (!NetArpWaitPacketIP)
- return;
- t = get_timer(0);
- /* check for arp timeout */
- if ((t - NetArpWaitTimerStart) > ARP_TIMEOUT) {
- NetArpWaitTry++;
- if (NetArpWaitTry >= ARP_TIMEOUT_COUNT) {
- puts("\nARP Retry count exceeded; starting again\n");
- NetArpWaitTry = 0;
- NetStartAgain();
- } else {
- NetArpWaitTimerStart = t;
- ArpRequest();
- }
- }
-}
/* * Check if autoload is enabled. If so, use either NFS or TFTP to download * the boot file. @@ -363,15 +274,11 @@ int NetLoop(enum proto_t protocol) NetRestarted = 0; NetDevExists = 0;
- /* XXX problem with bss workaround */
- NetArpWaitPacketMAC = NULL;
- NetArpWaitTxPacket = NULL;
- NetArpWaitPacketIP = 0;
- NetArpWaitReplyIP = 0;
- NetArpWaitTxPacket = NULL;
NetTxPacket = NULL; NetTryCount = 1;
- ArpInit();
if (!NetTxPacket) { int i; /* @@ -383,12 +290,6 @@ int NetLoop(enum proto_t protocol) NetRxPackets[i] = NetTxPacket + (i+1)*PKTSIZE_ALIGN; }
- if (!NetArpWaitTxPacket) {
- NetArpWaitTxPacket = &NetArpWaitPacketBuf[0] + (PKTALIGN - 1);
- NetArpWaitTxPacket -= (ulong)NetArpWaitTxPacket % PKTALIGN;
- NetArpWaitTxPacketSize = 0;
- }
eth_halt(); eth_set_current(); if (eth_init(bd) < 0) { @@ -661,6 +562,13 @@ void NetStartAgain(void) * Miscelaneous bits. */
+rxhand_f * +NetGetHandler(void) +{
- return packetHandler;
+}
void NetSetHandler(rxhand_f *f) { @@ -1072,11 +980,12 @@ NetReceive(volatile uchar *inpkt, int len) { Ethernet_t *et; IP_t *ip; +#ifdef CONFIG_CMD_RARP ARP_t *arp; +#endif IPaddr_t tmp; IPaddr_t src_ip; int x;
- uchar *pkt;
#if defined(CONFIG_CMD_CDP) int iscdp; #endif @@ -1173,99 +1082,7 @@ NetReceive(volatile uchar *inpkt, int len) switch (x) {
case PROT_ARP:
- /*
- * We have to deal with two types of ARP packets:
- * - REQUEST packets will be answered by sending our
- * IP address - if we know it.
- * - REPLY packates are expected only after we asked
- * for the TFTP server's or the gateway's ethernet
- * address; so if we receive such a packet, we set
- * the server ethernet address
- */
- debug("Got ARP\n");
- arp = (ARP_t *)ip;
- if (len < ARP_HDR_SIZE) {
- printf("bad length %d < %d\n", len, ARP_HDR_SIZE);
- return;
- }
- if (ntohs(arp->ar_hrd) != ARP_ETHER)
- return;
- if (ntohs(arp->ar_pro) != PROT_IP)
- return;
- if (arp->ar_hln != 6)
- return;
- if (arp->ar_pln != 4)
- return;
- if (NetOurIP == 0)
- return;
- if (NetReadIP(&arp->ar_data[16]) != NetOurIP)
- return;
- switch (ntohs(arp->ar_op)) {
- case ARPOP_REQUEST:
- /* reply with our IP address */
- debug("Got ARP REQUEST, return our IP\n");
- pkt = (uchar *)et;
- pkt += NetSetEther(pkt, et->et_src, PROT_ARP);
- arp->ar_op = htons(ARPOP_REPLY);
- memcpy(&arp->ar_data[10], &arp->ar_data[0], 6);
- NetCopyIP(&arp->ar_data[16], &arp->ar_data[6]);
- memcpy(&arp->ar_data[0], NetOurEther, 6);
- NetCopyIP(&arp->ar_data[6], &NetOurIP);
- (void) eth_send((uchar *)et,
- (pkt - (uchar *)et) + ARP_HDR_SIZE);
- return;
- case ARPOP_REPLY: /* arp reply */
- /* are we waiting for a reply */
- if (!NetArpWaitPacketIP || !NetArpWaitPacketMAC)
- break;
-#ifdef CONFIG_KEEP_SERVERADDR
- if (NetServerIP == NetArpWaitPacketIP) {
- char buf[20];
- sprintf(buf, "%pM", arp->ar_data);
- setenv("serveraddr", buf);
- }
-#endif
- debug("Got ARP REPLY, set server/gtwy eth addr (%pM)\n",
- arp->ar_data);
- tmp = NetReadIP(&arp->ar_data[6]);
- /* matched waiting packet's address */
- if (tmp == NetArpWaitReplyIP) {
- debug("Got it\n");
- /* save address for later use */
- memcpy(NetArpWaitPacketMAC,
- &arp->ar_data[0], 6);
-#ifdef CONFIG_NETCONSOLE
- (*packetHandler)(0, 0, 0, 0, 0);
-#endif
- /* modify header, and transmit it */
- memcpy(((Ethernet_t *)NetArpWaitTxPacket)->
- et_dest, NetArpWaitPacketMAC, 6);
- (void) eth_send(NetArpWaitTxPacket,
- NetArpWaitTxPacketSize);
- /* no arp request pending now */
- NetArpWaitPacketIP = 0;
- NetArpWaitTxPacketSize = 0;
- NetArpWaitPacketMAC = NULL;
- }
- return;
- default:
- debug("Unexpected ARP opcode 0x%x\n",
- ntohs(arp->ar_op));
- return;
- }
- ArpReceive(et, ip, len);
break;
#ifdef CONFIG_CMD_RARP
1.6.0.2
U-Boot mailing list U-Boot@lists.denx.de http://lists.denx.de/mailman/listinfo/u-boot

Acked-by: Mike Frysinger vapier@gentoo.org -mike

Signed-off-by: Joe Hershberger joe.hershberger@ni.com Cc: Joe Hershberger joe.hershberger@gmail.com Cc: Wolfgang Denk wd@denx.de --- include/configs/am3517_crane.h | 2 +- include/configs/am3517_evm.h | 2 +- include/configs/origen.h | 2 +- net/Makefile | 1 + net/net.c | 133 ++----------------------------------- net/ping.c | 143 ++++++++++++++++++++++++++++++++++++++++ net/ping.h | 20 ++++++ 7 files changed, 172 insertions(+), 131 deletions(-) create mode 100644 net/ping.c create mode 100644 net/ping.h
diff --git a/include/configs/am3517_crane.h b/include/configs/am3517_crane.h index 0a0c261..acd4e3f 100644 --- a/include/configs/am3517_crane.h +++ b/include/configs/am3517_crane.h @@ -146,7 +146,7 @@ #define CONFIG_CMD_MMC /* MMC support */ #define CONFIG_CMD_NAND /* NAND support */ #define CONFIG_CMD_DHCP -#define CONFIG_CMD_PING +#undef CONFIG_CMD_PING
#undef CONFIG_CMD_FLASH /* flinfo, erase, protect */ #undef CONFIG_CMD_FPGA /* FPGA configuration Support */ diff --git a/include/configs/am3517_evm.h b/include/configs/am3517_evm.h index d44eeec..9775107 100644 --- a/include/configs/am3517_evm.h +++ b/include/configs/am3517_evm.h @@ -145,7 +145,7 @@ #define CONFIG_CMD_MMC /* MMC support */ #define CONFIG_CMD_NAND /* NAND support */ #define CONFIG_CMD_DHCP -#define CONFIG_CMD_PING +#undef CONFIG_CMD_PING
#undef CONFIG_CMD_FLASH /* flinfo, erase, protect */ #undef CONFIG_CMD_FPGA /* FPGA configuration Support */ diff --git a/include/configs/origen.h b/include/configs/origen.h index cd502d1..c9bac30 100644 --- a/include/configs/origen.h +++ b/include/configs/origen.h @@ -84,7 +84,7 @@ /* Command definition*/ #include <config_cmd_default.h>
-#define CONFIG_CMD_PING +#undef CONFIG_CMD_PING #define CONFIG_CMD_ELF #define CONFIG_CMD_DHCP #define CONFIG_CMD_MMC diff --git a/net/Makefile b/net/Makefile index 0916a56..f1c4859 100644 --- a/net/Makefile +++ b/net/Makefile @@ -35,6 +35,7 @@ COBJS-$(CONFIG_CMD_NET) += eth.o COBJS-$(CONFIG_CMD_NET) += net.o COBJS-$(CONFIG_BOOTP_RANDOM_DELAY) += net_rand.o COBJS-$(CONFIG_CMD_NFS) += nfs.o +COBJS-$(CONFIG_CMD_PING) += ping.o COBJS-$(CONFIG_CMD_RARP) += rarp.o COBJS-$(CONFIG_CMD_SNTP) += sntp.o COBJS-$(CONFIG_CMD_NET) += tftp.o diff --git a/net/net.c b/net/net.c index 2dae5a0..c2fbcb5 100644 --- a/net/net.c +++ b/net/net.c @@ -98,6 +98,9 @@ #if defined(CONFIG_CMD_DNS) #include "dns.h" #endif +#if defined(CONFIG_CMD_PING) +#include "ping.h" +#endif
DECLARE_GLOBAL_DATA_PTR;
@@ -168,13 +171,6 @@ ushort NetOurNativeVLAN = 0xFFFF; /* Boot File name */ char BootFile[128];
-#if defined(CONFIG_CMD_PING) -/* the ip address to ping */ -IPaddr_t NetPingIP; - -static void PingStart(void); -#endif - #if defined(CONFIG_CMD_SNTP) /* NTP server IP address */ IPaddr_t NetNtpServerIP; @@ -653,94 +649,6 @@ NetSendUDPPacket(uchar *ether, IPaddr_t dest, int dport, int sport, int len) return 0; /* transmitted */ }
-#if defined(CONFIG_CMD_PING) -static ushort PingSeqNo; - -int PingSend(void) -{ - static uchar mac[6]; - IP_t *ip; - ushort *s; - uchar *pkt; - - /* XXX always send arp request */ - - memcpy(mac, NetEtherNullAddr, 6); - - debug("sending ARP for %08x\n", NetPingIP); - - NetArpWaitPacketIP = NetPingIP; - NetArpWaitPacketMAC = mac; - - pkt = NetArpWaitTxPacket; - pkt += NetSetEther(pkt, mac, PROT_IP); - - ip = (IP_t *)pkt; - - /* - * Construct an IP and ICMP header. - * (need to set no fragment bit - XXX) - */ - /* IP_HDR_SIZE / 4 (not including UDP) */ - ip->ip_hl_v = 0x45; - ip->ip_tos = 0; - ip->ip_len = htons(IP_HDR_SIZE_NO_UDP + 8); - ip->ip_id = htons(NetIPID++); - ip->ip_off = htons(IP_FLAGS_DFRAG); /* Don't fragment */ - ip->ip_ttl = 255; - ip->ip_p = 0x01; /* ICMP */ - ip->ip_sum = 0; - /* already in network byte order */ - NetCopyIP((void *)&ip->ip_src, &NetOurIP); - /* - "" - */ - NetCopyIP((void *)&ip->ip_dst, &NetPingIP); - ip->ip_sum = ~NetCksum((uchar *)ip, IP_HDR_SIZE_NO_UDP / 2); - - s = &ip->udp_src; /* XXX ICMP starts here */ - s[0] = htons(0x0800); /* echo-request, code */ - s[1] = 0; /* checksum */ - s[2] = 0; /* identifier */ - s[3] = htons(PingSeqNo++); /* sequence number */ - s[1] = ~NetCksum((uchar *)s, 8/2); - - /* size of the waiting packet */ - NetArpWaitTxPacketSize = - (pkt - NetArpWaitTxPacket) + IP_HDR_SIZE_NO_UDP + 8; - - /* and do the ARP request */ - NetArpWaitTry = 1; - NetArpWaitTimerStart = get_timer(0); - ArpRequest(); - return 1; /* waiting */ -} - -static void -PingTimeout(void) -{ - eth_halt(); - NetState = NETLOOP_FAIL; /* we did not get the reply */ -} - -static void -PingHandler(uchar *pkt, unsigned dest, IPaddr_t sip, unsigned src, - unsigned len) -{ - if (sip != NetPingIP) - return; - - NetState = NETLOOP_SUCCESS; -} - -static void PingStart(void) -{ - printf("Using %s device\n", eth_get_name()); - NetSetTimeout(10000UL, PingTimeout); - NetSetHandler(PingHandler); - - PingSend(); -} -#endif - #ifdef CONFIG_IP_DEFRAG /* * This function collects fragments in a single packet, according @@ -930,41 +838,10 @@ static void receive_icmp(IP_t *ip, int len, IPaddr_t src_ip, Ethernet_t *et) printf(" ICMP Host Redirect to %pI4 ", &icmph->un.gateway); break; + default: #if defined(CONFIG_CMD_PING) - case ICMP_ECHO_REPLY: - /* - * IP header OK. Pass the packet to the - * current handler. - */ - /* - * XXX point to ip packet - should this use - * packet_icmp_handler? - */ - (*packetHandler)((uchar *)ip, 0, src_ip, 0, 0); - break; - case ICMP_ECHO_REQUEST: - debug("Got ICMP ECHO REQUEST, return %d bytes\n", - ETHER_HDR_SIZE + len); - - memcpy(&et->et_dest[0], &et->et_src[0], 6); - memcpy(&et->et_src[0], NetOurEther, 6); - - ip->ip_sum = 0; - ip->ip_off = 0; - NetCopyIP((void *)&ip->ip_dst, &ip->ip_src); - NetCopyIP((void *)&ip->ip_src, &NetOurIP); - ip->ip_sum = ~NetCksum((uchar *)ip, - IP_HDR_SIZE_NO_UDP >> 1); - - icmph->type = ICMP_ECHO_REPLY; - icmph->checksum = 0; - icmph->checksum = ~NetCksum((uchar *)icmph, - (len - IP_HDR_SIZE_NO_UDP) >> 1); - (void) eth_send((uchar *)et, - ETHER_HDR_SIZE + len); - break; + PingReceive(et, ip, len); #endif - default: #ifdef CONFIG_CMD_TFTPPUT if (packet_icmp_handler) packet_icmp_handler(icmph->type, icmph->code, diff --git a/net/ping.c b/net/ping.c new file mode 100644 index 0000000..4a66b8d --- /dev/null +++ b/net/ping.c @@ -0,0 +1,143 @@ +/* + * Copied from Linux Monitor (LiMon) - Networking. + * + * Copyright 1994 - 2000 Neil Russell. + * (See License) + * Copyright 2000 Roland Borde + * Copyright 2000 Paolo Scaffardi + * Copyright 2000-2002 Wolfgang Denk, wd@denx.de + */ + +#include "ping.h" +#include "arp.h" + +static ushort PingSeqNo; + +/* The ip address to ping */ +IPaddr_t NetPingIP; + +static int PingSend(void) +{ + static uchar mac[6]; + IP_t *ip; + ushort *s; + uchar *pkt; + + /* XXX always send arp request */ + + memcpy(mac, NetEtherNullAddr, 6); + + debug("sending ARP for %pI4\n", &NetPingIP); + + NetArpWaitPacketIP = NetPingIP; + NetArpWaitPacketMAC = mac; + + pkt = NetArpWaitTxPacket; + pkt += NetSetEther(pkt, mac, PROT_IP); + + ip = (IP_t *)pkt; + + /* + * Construct an IP and ICMP header. + * (need to set no fragment bit - XXX) + */ + /* IP_HDR_SIZE / 4 (not including UDP) */ + ip->ip_hl_v = 0x45; + ip->ip_tos = 0; + ip->ip_len = htons(IP_HDR_SIZE_NO_UDP + 8); + ip->ip_id = htons(NetIPID++); + ip->ip_off = htons(IP_FLAGS_DFRAG); /* Don't fragment */ + ip->ip_ttl = 255; + ip->ip_p = 0x01; /* ICMP */ + ip->ip_sum = 0; + /* already in network byte order */ + NetCopyIP((void *)&ip->ip_src, &NetOurIP); + /* - "" - */ + NetCopyIP((void *)&ip->ip_dst, &NetPingIP); + ip->ip_sum = ~NetCksum((uchar *)ip, IP_HDR_SIZE_NO_UDP / 2); + + s = &ip->udp_src; /* XXX ICMP starts here */ + s[0] = htons(0x0800); /* echo-request, code */ + s[1] = 0; /* checksum */ + s[2] = 0; /* identifier */ + s[3] = htons(PingSeqNo++); /* sequence number */ + s[1] = ~NetCksum((uchar *)s, 8/2); + + /* size of the waiting packet */ + NetArpWaitTxPacketSize = + (pkt - NetArpWaitTxPacket) + IP_HDR_SIZE_NO_UDP + 8; + + /* and do the ARP request */ + NetArpWaitTry = 1; + NetArpWaitTimerStart = get_timer(0); + ArpRequest(); + return 1; /* waiting */ +} + +static void +PingTimeout(void) +{ + eth_halt(); + NetState = NETLOOP_FAIL; /* we did not get the reply */ +} + +static void +PingHandler(uchar *pkt, unsigned dest, IPaddr_t sip, unsigned src, + unsigned len) +{ + if (sip != NetPingIP) + return; + + NetState = NETLOOP_SUCCESS; +} + +void +PingStart(void) +{ + printf("Using %s device\n", eth_get_name()); + NetSetTimeout(10000UL, PingTimeout); + NetSetHandler(PingHandler); + + PingSend(); +} + +void PingReceive(Ethernet_t *et, IP_t *ip, int len) +{ + ICMP_t *icmph = (ICMP_t *)&(ip->udp_src); + IPaddr_t src_ip; + + switch (icmph->type) { + case ICMP_ECHO_REPLY: + /* + * IP header OK. Pass the packet to the + * current handler. + */ + /* XXX point to ip packet */ + src_ip = NetReadIP((void *)&ip->ip_src); + NetGetHandler()((uchar *)ip, 0, src_ip, 0, 0); + return; + case ICMP_ECHO_REQUEST: + debug("Got ICMP ECHO REQUEST, return " + "%d bytes\n", ETHER_HDR_SIZE + len); + + memcpy(&et->et_dest[0], &et->et_src[0], 6); + memcpy(&et->et_src[0], NetOurEther, 6); + + ip->ip_sum = 0; + ip->ip_off = 0; + NetCopyIP((void *)&ip->ip_dst, &ip->ip_src); + NetCopyIP((void *)&ip->ip_src, &NetOurIP); + ip->ip_sum = ~NetCksum((uchar *)ip, + IP_HDR_SIZE_NO_UDP >> 1); + + icmph->type = ICMP_ECHO_REPLY; + icmph->checksum = 0; + icmph->checksum = ~NetCksum((uchar *)icmph, + (len - IP_HDR_SIZE_NO_UDP) >> 1); + (void) eth_send((uchar *)et, + ETHER_HDR_SIZE + len); + return; +/* default: + return;*/ + } +} diff --git a/net/ping.h b/net/ping.h new file mode 100644 index 0000000..56aad43 --- /dev/null +++ b/net/ping.h @@ -0,0 +1,20 @@ +/* + * Copied from Linux Monitor (LiMon) - Networking. + * + * Copyright 1994 - 2000 Neil Russell. + * (See License) + * Copyright 2000 Roland Borde + * Copyright 2000 Paolo Scaffardi + * Copyright 2000-2002 Wolfgang Denk, wd@denx.de + */ + +#ifndef __PING_H__ +#define __PING_H__ + +#include <common.h> +#include <net.h> + +void PingStart(void); +void PingReceive(Ethernet_t *et, IP_t *ip, int len); + +#endif /* __PING_H__ */

Hi Joe,
On Thu, Jan 19, 2012 at 4:53 PM, Joe Hershberger joe.hershberger@ni.com wrote:
Signed-off-by: Joe Hershberger joe.hershberger@ni.com Cc: Joe Hershberger joe.hershberger@gmail.com Cc: Wolfgang Denk wd@denx.de
include/configs/am3517_crane.h | 2 +- include/configs/am3517_evm.h | 2 +- include/configs/origen.h | 2 +-
Perhaps move these config changes to another commit?
It would be nice (perhaps later) to remove the camel case.
Regards, Simon
net/Makefile | 1 + net/net.c | 133 ++----------------------------------- net/ping.c | 143 ++++++++++++++++++++++++++++++++++++++++ net/ping.h | 20 ++++++ 7 files changed, 172 insertions(+), 131 deletions(-) create mode 100644 net/ping.c create mode 100644 net/ping.h
diff --git a/include/configs/am3517_crane.h b/include/configs/am3517_crane.h index 0a0c261..acd4e3f 100644 --- a/include/configs/am3517_crane.h +++ b/include/configs/am3517_crane.h @@ -146,7 +146,7 @@ #define CONFIG_CMD_MMC /* MMC support */ #define CONFIG_CMD_NAND /* NAND support */ #define CONFIG_CMD_DHCP -#define CONFIG_CMD_PING +#undef CONFIG_CMD_PING
#undef CONFIG_CMD_FLASH /* flinfo, erase, protect */ #undef CONFIG_CMD_FPGA /* FPGA configuration Support */ diff --git a/include/configs/am3517_evm.h b/include/configs/am3517_evm.h index d44eeec..9775107 100644 --- a/include/configs/am3517_evm.h +++ b/include/configs/am3517_evm.h @@ -145,7 +145,7 @@ #define CONFIG_CMD_MMC /* MMC support */ #define CONFIG_CMD_NAND /* NAND support */ #define CONFIG_CMD_DHCP -#define CONFIG_CMD_PING +#undef CONFIG_CMD_PING
#undef CONFIG_CMD_FLASH /* flinfo, erase, protect */ #undef CONFIG_CMD_FPGA /* FPGA configuration Support */ diff --git a/include/configs/origen.h b/include/configs/origen.h index cd502d1..c9bac30 100644 --- a/include/configs/origen.h +++ b/include/configs/origen.h @@ -84,7 +84,7 @@ /* Command definition*/ #include <config_cmd_default.h>
-#define CONFIG_CMD_PING +#undef CONFIG_CMD_PING #define CONFIG_CMD_ELF #define CONFIG_CMD_DHCP #define CONFIG_CMD_MMC diff --git a/net/Makefile b/net/Makefile index 0916a56..f1c4859 100644 --- a/net/Makefile +++ b/net/Makefile @@ -35,6 +35,7 @@ COBJS-$(CONFIG_CMD_NET) += eth.o COBJS-$(CONFIG_CMD_NET) += net.o COBJS-$(CONFIG_BOOTP_RANDOM_DELAY) += net_rand.o COBJS-$(CONFIG_CMD_NFS) += nfs.o +COBJS-$(CONFIG_CMD_PING) += ping.o COBJS-$(CONFIG_CMD_RARP) += rarp.o COBJS-$(CONFIG_CMD_SNTP) += sntp.o COBJS-$(CONFIG_CMD_NET) += tftp.o diff --git a/net/net.c b/net/net.c index 2dae5a0..c2fbcb5 100644 --- a/net/net.c +++ b/net/net.c @@ -98,6 +98,9 @@ #if defined(CONFIG_CMD_DNS) #include "dns.h" #endif +#if defined(CONFIG_CMD_PING) +#include "ping.h" +#endif
DECLARE_GLOBAL_DATA_PTR;
@@ -168,13 +171,6 @@ ushort NetOurNativeVLAN = 0xFFFF; /* Boot File name */ char BootFile[128];
-#if defined(CONFIG_CMD_PING) -/* the ip address to ping */ -IPaddr_t NetPingIP;
-static void PingStart(void); -#endif
#if defined(CONFIG_CMD_SNTP) /* NTP server IP address */ IPaddr_t NetNtpServerIP; @@ -653,94 +649,6 @@ NetSendUDPPacket(uchar *ether, IPaddr_t dest, int dport, int sport, int len) return 0; /* transmitted */ }
-#if defined(CONFIG_CMD_PING) -static ushort PingSeqNo;
-int PingSend(void) -{
- static uchar mac[6];
- IP_t *ip;
- ushort *s;
- uchar *pkt;
- /* XXX always send arp request */
- memcpy(mac, NetEtherNullAddr, 6);
- debug("sending ARP for %08x\n", NetPingIP);
- NetArpWaitPacketIP = NetPingIP;
- NetArpWaitPacketMAC = mac;
- pkt = NetArpWaitTxPacket;
- pkt += NetSetEther(pkt, mac, PROT_IP);
- ip = (IP_t *)pkt;
- /*
- * Construct an IP and ICMP header.
- * (need to set no fragment bit - XXX)
- */
- /* IP_HDR_SIZE / 4 (not including UDP) */
- ip->ip_hl_v = 0x45;
- ip->ip_tos = 0;
- ip->ip_len = htons(IP_HDR_SIZE_NO_UDP + 8);
- ip->ip_id = htons(NetIPID++);
- ip->ip_off = htons(IP_FLAGS_DFRAG); /* Don't fragment */
- ip->ip_ttl = 255;
- ip->ip_p = 0x01; /* ICMP */
- ip->ip_sum = 0;
- /* already in network byte order */
- NetCopyIP((void *)&ip->ip_src, &NetOurIP);
- /* - "" - */
- NetCopyIP((void *)&ip->ip_dst, &NetPingIP);
- ip->ip_sum = ~NetCksum((uchar *)ip, IP_HDR_SIZE_NO_UDP / 2);
- s = &ip->udp_src; /* XXX ICMP starts here */
- s[0] = htons(0x0800); /* echo-request, code */
- s[1] = 0; /* checksum */
- s[2] = 0; /* identifier */
- s[3] = htons(PingSeqNo++); /* sequence number */
- s[1] = ~NetCksum((uchar *)s, 8/2);
- /* size of the waiting packet */
- NetArpWaitTxPacketSize =
- (pkt - NetArpWaitTxPacket) + IP_HDR_SIZE_NO_UDP + 8;
- /* and do the ARP request */
- NetArpWaitTry = 1;
- NetArpWaitTimerStart = get_timer(0);
- ArpRequest();
- return 1; /* waiting */
-}
-static void -PingTimeout(void) -{
- eth_halt();
- NetState = NETLOOP_FAIL; /* we did not get the reply */
-}
-static void -PingHandler(uchar *pkt, unsigned dest, IPaddr_t sip, unsigned src,
- unsigned len)
-{
- if (sip != NetPingIP)
- return;
- NetState = NETLOOP_SUCCESS;
-}
-static void PingStart(void) -{
- printf("Using %s device\n", eth_get_name());
- NetSetTimeout(10000UL, PingTimeout);
- NetSetHandler(PingHandler);
- PingSend();
-} -#endif
#ifdef CONFIG_IP_DEFRAG /* * This function collects fragments in a single packet, according @@ -930,41 +838,10 @@ static void receive_icmp(IP_t *ip, int len, IPaddr_t src_ip, Ethernet_t *et) printf(" ICMP Host Redirect to %pI4 ", &icmph->un.gateway); break;
- default:
#if defined(CONFIG_CMD_PING)
- case ICMP_ECHO_REPLY:
- /*
- * IP header OK. Pass the packet to the
- * current handler.
- */
- /*
- * XXX point to ip packet - should this use
- * packet_icmp_handler?
- */
- (*packetHandler)((uchar *)ip, 0, src_ip, 0, 0);
- break;
- case ICMP_ECHO_REQUEST:
- debug("Got ICMP ECHO REQUEST, return %d bytes\n",
- ETHER_HDR_SIZE + len);
- memcpy(&et->et_dest[0], &et->et_src[0], 6);
- memcpy(&et->et_src[0], NetOurEther, 6);
- ip->ip_sum = 0;
- ip->ip_off = 0;
- NetCopyIP((void *)&ip->ip_dst, &ip->ip_src);
- NetCopyIP((void *)&ip->ip_src, &NetOurIP);
- ip->ip_sum = ~NetCksum((uchar *)ip,
- IP_HDR_SIZE_NO_UDP >> 1);
- icmph->type = ICMP_ECHO_REPLY;
- icmph->checksum = 0;
- icmph->checksum = ~NetCksum((uchar *)icmph,
- (len - IP_HDR_SIZE_NO_UDP) >> 1);
- (void) eth_send((uchar *)et,
- ETHER_HDR_SIZE + len);
- break;
- PingReceive(et, ip, len);
#endif
- default:
#ifdef CONFIG_CMD_TFTPPUT if (packet_icmp_handler) packet_icmp_handler(icmph->type, icmph->code, diff --git a/net/ping.c b/net/ping.c new file mode 100644 index 0000000..4a66b8d --- /dev/null +++ b/net/ping.c @@ -0,0 +1,143 @@ +/*
- Copied from Linux Monitor (LiMon) - Networking.
- Copyright 1994 - 2000 Neil Russell.
- (See License)
- Copyright 2000 Roland Borde
- Copyright 2000 Paolo Scaffardi
- Copyright 2000-2002 Wolfgang Denk, wd@denx.de
- */
+#include "ping.h" +#include "arp.h"
+static ushort PingSeqNo;
+/* The ip address to ping */ +IPaddr_t NetPingIP;
+static int PingSend(void) +{
- static uchar mac[6];
- IP_t *ip;
- ushort *s;
- uchar *pkt;
- /* XXX always send arp request */
- memcpy(mac, NetEtherNullAddr, 6);
- debug("sending ARP for %pI4\n", &NetPingIP);
- NetArpWaitPacketIP = NetPingIP;
- NetArpWaitPacketMAC = mac;
- pkt = NetArpWaitTxPacket;
- pkt += NetSetEther(pkt, mac, PROT_IP);
- ip = (IP_t *)pkt;
- /*
- * Construct an IP and ICMP header.
- * (need to set no fragment bit - XXX)
- */
- /* IP_HDR_SIZE / 4 (not including UDP) */
- ip->ip_hl_v = 0x45;
- ip->ip_tos = 0;
- ip->ip_len = htons(IP_HDR_SIZE_NO_UDP + 8);
- ip->ip_id = htons(NetIPID++);
- ip->ip_off = htons(IP_FLAGS_DFRAG); /* Don't fragment */
- ip->ip_ttl = 255;
- ip->ip_p = 0x01; /* ICMP */
- ip->ip_sum = 0;
- /* already in network byte order */
- NetCopyIP((void *)&ip->ip_src, &NetOurIP);
- /* - "" - */
- NetCopyIP((void *)&ip->ip_dst, &NetPingIP);
- ip->ip_sum = ~NetCksum((uchar *)ip, IP_HDR_SIZE_NO_UDP / 2);
- s = &ip->udp_src; /* XXX ICMP starts here */
- s[0] = htons(0x0800); /* echo-request, code */
- s[1] = 0; /* checksum */
- s[2] = 0; /* identifier */
- s[3] = htons(PingSeqNo++); /* sequence number */
- s[1] = ~NetCksum((uchar *)s, 8/2);
- /* size of the waiting packet */
- NetArpWaitTxPacketSize =
- (pkt - NetArpWaitTxPacket) + IP_HDR_SIZE_NO_UDP + 8;
- /* and do the ARP request */
- NetArpWaitTry = 1;
- NetArpWaitTimerStart = get_timer(0);
- ArpRequest();
- return 1; /* waiting */
+}
+static void +PingTimeout(void) +{
- eth_halt();
- NetState = NETLOOP_FAIL; /* we did not get the reply */
+}
+static void +PingHandler(uchar *pkt, unsigned dest, IPaddr_t sip, unsigned src,
- unsigned len)
+{
- if (sip != NetPingIP)
- return;
- NetState = NETLOOP_SUCCESS;
+}
+void +PingStart(void) +{
- printf("Using %s device\n", eth_get_name());
- NetSetTimeout(10000UL, PingTimeout);
- NetSetHandler(PingHandler);
- PingSend();
+}
+void PingReceive(Ethernet_t *et, IP_t *ip, int len) +{
- ICMP_t *icmph = (ICMP_t *)&(ip->udp_src);
- IPaddr_t src_ip;
- switch (icmph->type) {
- case ICMP_ECHO_REPLY:
- /*
- * IP header OK. Pass the packet to the
- * current handler.
- */
- /* XXX point to ip packet */
- src_ip = NetReadIP((void *)&ip->ip_src);
- NetGetHandler()((uchar *)ip, 0, src_ip, 0, 0);
- return;
- case ICMP_ECHO_REQUEST:
- debug("Got ICMP ECHO REQUEST, return "
- "%d bytes\n", ETHER_HDR_SIZE + len);
- memcpy(&et->et_dest[0], &et->et_src[0], 6);
- memcpy(&et->et_src[0], NetOurEther, 6);
- ip->ip_sum = 0;
- ip->ip_off = 0;
- NetCopyIP((void *)&ip->ip_dst, &ip->ip_src);
- NetCopyIP((void *)&ip->ip_src, &NetOurIP);
- ip->ip_sum = ~NetCksum((uchar *)ip,
- IP_HDR_SIZE_NO_UDP >> 1);
- icmph->type = ICMP_ECHO_REPLY;
- icmph->checksum = 0;
- icmph->checksum = ~NetCksum((uchar *)icmph,
- (len - IP_HDR_SIZE_NO_UDP) >> 1);
- (void) eth_send((uchar *)et,
- ETHER_HDR_SIZE + len);
- return;
+/* default:
- return;*/
- }
+} diff --git a/net/ping.h b/net/ping.h new file mode 100644 index 0000000..56aad43 --- /dev/null +++ b/net/ping.h @@ -0,0 +1,20 @@ +/*
- Copied from Linux Monitor (LiMon) - Networking.
- Copyright 1994 - 2000 Neil Russell.
- (See License)
- Copyright 2000 Roland Borde
- Copyright 2000 Paolo Scaffardi
- Copyright 2000-2002 Wolfgang Denk, wd@denx.de
- */
+#ifndef __PING_H__ +#define __PING_H__
+#include <common.h> +#include <net.h>
+void PingStart(void); +void PingReceive(Ethernet_t *et, IP_t *ip, int len);
+#endif /* __PING_H__ */
1.6.0.2
U-Boot mailing list U-Boot@lists.denx.de http://lists.denx.de/mailman/listinfo/u-boot

On Tuesday 24 January 2012 00:37:51 Simon Glass wrote:
On Thu, Jan 19, 2012 at 4:53 PM, Joe Hershberger wrote:
include/configs/am3517_crane.h | 2 +- include/configs/am3517_evm.h | 2 +- include/configs/origen.h | 2 +-
Perhaps move these config changes to another commit?
+1 -mike

hmm, thinking a bit more, i wonder if it makes more sense to split all of icmp out of net.c -mike

Hi Mike,
On Fri, Feb 3, 2012 at 6:07 AM, Mike Frysinger vapier@gentoo.org wrote:
hmm, thinking a bit more, i wonder if it makes more sense to split all of icmp out of net.c
This would also be fine. Are you thinking that icmp would include all of icmp or would we still have ping separate? Does this need to be done now? It would be quite a bit of churn to move ping changes to a different file first since the changes after the move would have to be manually recreated.
Thanks, -Joe

On Monday 13 February 2012 18:54:46 Joe Hershberger wrote:
On Fri, Feb 3, 2012 at 6:07 AM, Mike Frysinger wrote:
hmm, thinking a bit more, i wonder if it makes more sense to split all of icmp out of net.c
This would also be fine. Are you thinking that icmp would include all of icmp or would we still have ping separate? Does this need to be done now? It would be quite a bit of churn to move ping changes to a different file first since the changes after the move would have to be manually recreated.
keeping ping.c is fine, but i think we should also have icmp.c broken out of net.c. since we're keeping ping.c, no need to rebase onto a new patch. icmp.c can come after these. -mike

Signed-off-by: Joe Hershberger joe.hershberger@ni.com Cc: Joe Hershberger joe.hershberger@gmail.com Cc: Wolfgang Denk wd@denx.de --- net/net.c | 25 +------------------------ net/rarp.c | 38 +++++++++++++++++++++++++++----------- net/rarp.h | 1 + 3 files changed, 29 insertions(+), 35 deletions(-)
diff --git a/net/net.c b/net/net.c index c2fbcb5..fdc1ebe 100644 --- a/net/net.c +++ b/net/net.c @@ -857,9 +857,6 @@ NetReceive(volatile uchar *inpkt, int len) { Ethernet_t *et; IP_t *ip; -#ifdef CONFIG_CMD_RARP - ARP_t *arp; -#endif IPaddr_t tmp; IPaddr_t src_ip; int x; @@ -964,27 +961,7 @@ NetReceive(volatile uchar *inpkt, int len)
#ifdef CONFIG_CMD_RARP case PROT_RARP: - debug("Got RARP\n"); - arp = (ARP_t *)ip; - if (len < ARP_HDR_SIZE) { - printf("bad length %d < %d\n", len, ARP_HDR_SIZE); - return; - } - - if ((ntohs(arp->ar_op) != RARPOP_REPLY) || - (ntohs(arp->ar_hrd) != ARP_ETHER) || - (ntohs(arp->ar_pro) != PROT_IP) || - (arp->ar_hln != 6) || (arp->ar_pln != 4)) { - - puts("invalid RARP header\n"); - } else { - NetCopyIP(&NetOurIP, &arp->ar_data[16]); - if (NetServerIP == 0) - NetCopyIP(&NetServerIP, &arp->ar_data[6]); - memcpy(NetServerEther, &arp->ar_data[0], 6); - - (*packetHandler)(0, 0, 0, 0, 0); - } + RarpReceive(ip, len); break; #endif case PROT_IP: diff --git a/net/rarp.c b/net/rarp.c index 5a813a2..761560c 100644 --- a/net/rarp.c +++ b/net/rarp.c @@ -36,18 +36,37 @@ # define TIMEOUT_COUNT (CONFIG_NET_RETRY_COUNT) #endif
- int RarpTry;
/* * Handle a RARP received packet. */ -static void -RarpHandler(uchar *dummi0, unsigned dummi1, IPaddr_t sip, unsigned dummi2, - unsigned dummi3) +void +RarpReceive(IP_t *ip, unsigned len) { - debug("Got good RARP\n"); - net_auto_load(); + ARP_t *arp; + + debug("Got RARP\n"); + arp = (ARP_t *)ip; + if (len < ARP_HDR_SIZE) { + printf("bad length %d < %d\n", len, ARP_HDR_SIZE); + return; + } + + if ((ntohs(arp->ar_op) != RARPOP_REPLY) || + (ntohs(arp->ar_hrd) != ARP_ETHER) || + (ntohs(arp->ar_pro) != PROT_IP) || + (arp->ar_hln != 6) || (arp->ar_pln != 4)) { + + puts("invalid RARP header\n"); + } else { + NetCopyIP(&NetOurIP, &arp->ar_data[16]); + if (NetServerIP == 0) + NetCopyIP(&NetServerIP, &arp->ar_data[6]); + memcpy(NetServerEther, &arp->ar_data[0], 6); + debug("Got good RARP\n"); + net_auto_load(); + } }
@@ -70,7 +89,6 @@ RarpTimeout(void) void RarpRequest(void) { - int i; uchar *pkt; ARP_t *rarp;
@@ -90,12 +108,10 @@ RarpRequest(void) memcpy(&rarp->ar_data[6], &NetOurIP, 4); /* source IP addr */ /* dest ET addr = source ET addr ??*/ memcpy(&rarp->ar_data[10], NetOurEther, 6); - /* dest. IP addr set to broadcast */ - for (i = 0; i <= 3; i++) - rarp->ar_data[16 + i] = 0xff; + /* dest IP addr set to broadcast */ + memset(&rarp->ar_data[16], 0xff, 4);
NetSendPacket(NetTxPacket, (pkt - NetTxPacket) + ARP_HDR_SIZE);
NetSetTimeout(TIMEOUT, RarpTimeout); - NetSetHandler(RarpHandler); } diff --git a/net/rarp.h b/net/rarp.h index 4e92d80..0d728dc 100644 --- a/net/rarp.h +++ b/net/rarp.h @@ -37,6 +37,7 @@
extern int RarpTry;
+extern void RarpReceive(IP_t *ip, unsigned len); extern void RarpRequest(void); /* Send a RARP request */
/**********************************************************************/

On Thu, Jan 19, 2012 at 4:53 PM, Joe Hershberger joe.hershberger@ni.com wrote:
Signed-off-by: Joe Hershberger joe.hershberger@ni.com Cc: Joe Hershberger joe.hershberger@gmail.com Cc: Wolfgang Denk wd@denx.de
Acked-by: Simon Glass sjg@chromium.org
net/net.c | 25 +------------------------ net/rarp.c | 38 +++++++++++++++++++++++++++----------- net/rarp.h | 1 + 3 files changed, 29 insertions(+), 35 deletions(-)
diff --git a/net/net.c b/net/net.c index c2fbcb5..fdc1ebe 100644 --- a/net/net.c +++ b/net/net.c @@ -857,9 +857,6 @@ NetReceive(volatile uchar *inpkt, int len) { Ethernet_t *et; IP_t *ip; -#ifdef CONFIG_CMD_RARP
- ARP_t *arp;
-#endif IPaddr_t tmp; IPaddr_t src_ip; int x; @@ -964,27 +961,7 @@ NetReceive(volatile uchar *inpkt, int len)
#ifdef CONFIG_CMD_RARP case PROT_RARP:
- debug("Got RARP\n");
- arp = (ARP_t *)ip;
- if (len < ARP_HDR_SIZE) {
- printf("bad length %d < %d\n", len, ARP_HDR_SIZE);
- return;
- }
- if ((ntohs(arp->ar_op) != RARPOP_REPLY) ||
- (ntohs(arp->ar_hrd) != ARP_ETHER) ||
- (ntohs(arp->ar_pro) != PROT_IP) ||
- (arp->ar_hln != 6) || (arp->ar_pln != 4)) {
- puts("invalid RARP header\n");
- } else {
- NetCopyIP(&NetOurIP, &arp->ar_data[16]);
- if (NetServerIP == 0)
- NetCopyIP(&NetServerIP, &arp->ar_data[6]);
- memcpy(NetServerEther, &arp->ar_data[0], 6);
- (*packetHandler)(0, 0, 0, 0, 0);
- }
- RarpReceive(ip, len);
break; #endif case PROT_IP: diff --git a/net/rarp.c b/net/rarp.c index 5a813a2..761560c 100644 --- a/net/rarp.c +++ b/net/rarp.c @@ -36,18 +36,37 @@ # define TIMEOUT_COUNT (CONFIG_NET_RETRY_COUNT) #endif
int RarpTry;
/* * Handle a RARP received packet. */ -static void -RarpHandler(uchar *dummi0, unsigned dummi1, IPaddr_t sip, unsigned dummi2,
- unsigned dummi3)
+void
void on same line as function?
+RarpReceive(IP_t *ip, unsigned len) {
- debug("Got good RARP\n");
- net_auto_load();
- ARP_t *arp;
- debug("Got RARP\n");
- arp = (ARP_t *)ip;
- if (len < ARP_HDR_SIZE) {
- printf("bad length %d < %d\n", len, ARP_HDR_SIZE);
- return;
- }
- if ((ntohs(arp->ar_op) != RARPOP_REPLY) ||
- (ntohs(arp->ar_hrd) != ARP_ETHER) ||
- (ntohs(arp->ar_pro) != PROT_IP) ||
- (arp->ar_hln != 6) || (arp->ar_pln != 4)) {
- puts("invalid RARP header\n");
- } else {
- NetCopyIP(&NetOurIP, &arp->ar_data[16]);
- if (NetServerIP == 0)
- NetCopyIP(&NetServerIP, &arp->ar_data[6]);
- memcpy(NetServerEther, &arp->ar_data[0], 6);
- debug("Got good RARP\n");
- net_auto_load();
- }
}
@@ -70,7 +89,6 @@ RarpTimeout(void) void RarpRequest(void) {
- int i;
uchar *pkt; ARP_t *rarp;
@@ -90,12 +108,10 @@ RarpRequest(void) memcpy(&rarp->ar_data[6], &NetOurIP, 4); /* source IP addr */ /* dest ET addr = source ET addr ??*/ memcpy(&rarp->ar_data[10], NetOurEther, 6);
- /* dest. IP addr set to broadcast */
- for (i = 0; i <= 3; i++)
- rarp->ar_data[16 + i] = 0xff;
- /* dest IP addr set to broadcast */
- memset(&rarp->ar_data[16], 0xff, 4);
NetSendPacket(NetTxPacket, (pkt - NetTxPacket) + ARP_HDR_SIZE);
NetSetTimeout(TIMEOUT, RarpTimeout);
- NetSetHandler(RarpHandler);
} diff --git a/net/rarp.h b/net/rarp.h index 4e92d80..0d728dc 100644 --- a/net/rarp.h +++ b/net/rarp.h @@ -37,6 +37,7 @@
extern int RarpTry;
+extern void RarpReceive(IP_t *ip, unsigned len); extern void RarpRequest(void); /* Send a RARP request */
/**********************************************************************/
1.6.0.2
U-Boot mailing list U-Boot@lists.denx.de http://lists.denx.de/mailman/listinfo/u-boot

Acked-by: Mike Frysinger vapier@gentoo.org -mike

Un-typedef IP_t, Ethernet_t, VLAN_Ethernet_t, ARP_t, ICMP_t Rename IP header related things to IP_UDP The existing definition of IP_t includes UDP header, so name it to accurately describe the structure
Signed-off-by: Joe Hershberger joe.hershberger@ni.com Cc: Joe Hershberger joe.hershberger@gmail.com Cc: Wolfgang Denk wd@denx.de --- arch/powerpc/cpu/mpc8260/ether_fcc.c | 10 ++-- drivers/net/netconsole.c | 4 +- include/net.h | 30 ++++++----- net/arp.c | 12 ++-- net/arp.h | 2 +- net/bootp.c | 8 ++-- net/bootp.h | 2 +- net/cdp.c | 4 +- net/dns.c | 2 +- net/net.c | 87 +++++++++++++++++---------------- net/nfs.c | 2 +- net/ping.c | 18 ++++---- net/ping.h | 2 +- net/rarp.c | 10 ++-- net/rarp.h | 2 +- net/sntp.c | 2 +- net/tftp.c | 2 +- 17 files changed, 102 insertions(+), 97 deletions(-)
diff --git a/arch/powerpc/cpu/mpc8260/ether_fcc.c b/arch/powerpc/cpu/mpc8260/ether_fcc.c index 879ec0e..f59d9e1 100644 --- a/arch/powerpc/cpu/mpc8260/ether_fcc.c +++ b/arch/powerpc/cpu/mpc8260/ether_fcc.c @@ -1049,11 +1049,11 @@ eth_loopback_test (void) } else { ushort datlen = bdp->cbd_datlen; - Ethernet_t *ehp; + struct Ethernet_t *ehp; ushort prot; int ours, tb, n, nbytes;
- ehp = (Ethernet_t *) \ + ehp = (struct Ethernet_t *) \ &ecp->rxbufs[i][0];
ours = memcmp (ehp->et_src, \ @@ -1064,9 +1064,9 @@ eth_loopback_test (void) n = prot & 0x7fff;
nbytes = ELBT_BUFSZ - \ - offsetof (Ethernet_t, \ - et_dsap) - \ - ELBT_CRCSZ; + offsetof( + struct Ethernet_t, \ + et_dsap) - ELBT_CRCSZ;
/* check the frame is correct */ if (datlen != ELBT_BUFSZ) diff --git a/drivers/net/netconsole.c b/drivers/net/netconsole.c index 7acd5b0..ba02fd7 100644 --- a/drivers/net/netconsole.c +++ b/drivers/net/netconsole.c @@ -69,7 +69,7 @@ void NcStart(void) /* send arp request */ uchar *pkt; NetSetHandler(nc_wait_arp_handler); - pkt = (uchar *)NetTxPacket + NetEthHdrSize() + IP_HDR_SIZE; + pkt = (uchar *)NetTxPacket + NetEthHdrSize() + IP_UDP_HDR_SIZE; memcpy(pkt, output_packet, output_packet_len); NetSendUDPPacket(nc_ether, nc_ip, nc_port, nc_port, output_packet_len); @@ -131,7 +131,7 @@ static void nc_send_packet(const char *buf, int len) return; inited = 1; } - pkt = (uchar *)NetTxPacket + NetEthHdrSize() + IP_HDR_SIZE; + pkt = (uchar *)NetTxPacket + NetEthHdrSize() + IP_UDP_HDR_SIZE; memcpy(pkt, buf, len); ether = nc_ether; ip = nc_ip; diff --git a/include/net.h b/include/net.h index 2d00233..9de1181 100644 --- a/include/net.h +++ b/include/net.h @@ -155,7 +155,7 @@ u32 ether_crc(size_t len, unsigned char const *p); /* * Ethernet header */ -typedef struct { +struct Ethernet_t { uchar et_dest[6]; /* Destination node */ uchar et_src[6]; /* Source node */ ushort et_protlen; /* Protocol or length */ @@ -166,7 +166,7 @@ typedef struct { uchar et_snap2; uchar et_snap3; ushort et_prot; /* 802 protocol */ -} Ethernet_t; +};
#define ETHER_HDR_SIZE 14 /* Ethernet header size */ #define E802_HDR_SIZE 22 /* 802 ethernet header size */ @@ -174,13 +174,13 @@ typedef struct { /* * Ethernet header */ -typedef struct { +struct VLAN_Ethernet_t { uchar vet_dest[6]; /* Destination node */ uchar vet_src[6]; /* Source node */ ushort vet_vlan_type; /* PROT_VLAN */ ushort vet_tag; /* TAG of VLAN */ ushort vet_type; /* protocol type */ -} VLAN_Ethernet_t; +};
#define VLAN_ETHER_HDR_SIZE 18 /* VLAN Ethernet header size */
@@ -193,9 +193,9 @@ typedef struct { #define IPPROTO_UDP 17 /* User Datagram Protocol */
/* - * Internet Protocol (IP) header. + * Internet Protocol (IP) + UDP header. */ -typedef struct { +struct IP_UDP_t { uchar ip_hl_v; /* header length and version */ uchar ip_tos; /* type of service */ ushort ip_len; /* total length */ @@ -210,7 +210,7 @@ typedef struct { ushort udp_dst; /* UDP destination port */ ushort udp_len; /* Length of UDP packet */ ushort udp_xsum; /* Checksum */ -} IP_t; +};
#define IP_OFFS 0x1fff /* ip offset *= 8 */ #define IP_FLAGS 0xe000 /* first 3 bits */ @@ -218,15 +218,15 @@ typedef struct { #define IP_FLAGS_DFRAG 0x4000 /* don't fragments */ #define IP_FLAGS_MFRAG 0x2000 /* more fragments */
-#define IP_HDR_SIZE_NO_UDP (sizeof(IP_t) - 8) -#define IP_HDR_SIZE (sizeof(IP_t)) +#define UDP_HDR_SIZE (sizeof(ushort) * 4) +#define IP_HDR_SIZE (sizeof(struct IP_UDP_t) - UDP_HDR_SIZE) +#define IP_UDP_HDR_SIZE (sizeof(struct IP_UDP_t))
/* * Address Resolution Protocol (ARP) header. */ -typedef struct -{ +struct ARP_t { ushort ar_hrd; /* Format of hardware address */ # define ARP_ETHER 1 /* Ethernet hardware address */ ushort ar_pro; /* Format of protocol address */ @@ -251,7 +251,7 @@ typedef struct uchar ar_tha[]; /* Target hardware address */ uchar ar_tpa[]; /* Target protocol address */ #endif /* 0 */ -} ARP_t; +};
#define ARP_HDR_SIZE (8+20) /* Size assuming ethernet */
@@ -270,7 +270,7 @@ typedef struct /* Codes for NOT_REACH */ #define ICMP_NOT_REACH_PORT 3 /* Port unreachable */
-typedef struct icmphdr { +struct ICMP_t { uchar type; uchar code; ushort checksum; @@ -286,8 +286,10 @@ typedef struct icmphdr { } frag; uchar data[0]; } un; -} ICMP_t; +};
+#define ICMP_HDR_SIZE (sizeof(struct ICMP_t)) +#define IP_ICMP_HDR_SIZE (IP_HDR_SIZE + ICMP_HDR_SIZE)
/* * Maximum packet size; used to allocate packet storage. diff --git a/net/arp.c b/net/arp.c index f75217c..96ffb85 100644 --- a/net/arp.c +++ b/net/arp.c @@ -51,7 +51,7 @@ void ArpInit(void) void ArpRequest(void) { uchar *pkt; - ARP_t *arp; + struct ARP_t *arp;
debug("ARP broadcast %d\n", NetArpWaitTry);
@@ -59,7 +59,7 @@ void ArpRequest(void)
pkt += NetSetEther(pkt, NetBcastAddr, PROT_ARP);
- arp = (ARP_t *) pkt; + arp = (struct ARP_t *) pkt;
arp->ar_hrd = htons(ARP_ETHER); arp->ar_pro = htons(PROT_IP); @@ -113,9 +113,9 @@ void ArpTimeoutCheck(void) } }
-void ArpReceive(Ethernet_t *et, IP_t *ip, int len) +void ArpReceive(struct Ethernet_t *et, struct IP_UDP_t *ip, int len) { - ARP_t *arp; + struct ARP_t *arp; IPaddr_t tmp; uchar *pkt;
@@ -130,7 +130,7 @@ void ArpReceive(Ethernet_t *et, IP_t *ip, int len) */ debug("Got ARP\n");
- arp = (ARP_t *)ip; + arp = (struct ARP_t *)ip; if (len < ARP_HDR_SIZE) { printf("bad length %d < %d\n", len, ARP_HDR_SIZE); return; @@ -193,7 +193,7 @@ void ArpReceive(Ethernet_t *et, IP_t *ip, int len) NetGetHandler()(0, 0, 0, 0, 0); #endif /* modify header, and transmit it */ - memcpy(((Ethernet_t *)NetArpWaitTxPacket)-> + memcpy(((struct Ethernet_t *)NetArpWaitTxPacket)-> et_dest, NetArpWaitPacketMAC, 6); (void) eth_send(NetArpWaitTxPacket, NetArpWaitTxPacketSize); diff --git a/net/arp.h b/net/arp.h index 09c763d..d9ce03d 100644 --- a/net/arp.h +++ b/net/arp.h @@ -25,6 +25,6 @@ extern int NetArpWaitTry; void ArpInit(void); void ArpRequest(void); void ArpTimeoutCheck(void); -void ArpReceive(Ethernet_t *et, IP_t *ip, int len); +void ArpReceive(struct Ethernet_t *et, struct IP_UDP_t *ip, int len);
#endif /* __ARP_H__ */ diff --git a/net/bootp.c b/net/bootp.c index 9824cd1..e95419f 100644 --- a/net/bootp.c +++ b/net/bootp.c @@ -105,7 +105,7 @@ static void BootpCopyNetParams(struct Bootp_t *bp) NetCopyIP(&tmp_ip, &bp->bp_siaddr); if (tmp_ip != 0) NetCopyIP(&NetServerIP, &bp->bp_siaddr); - memcpy(NetServerEther, ((Ethernet_t *)NetRxPacket)->et_src, 6); + memcpy(NetServerEther, ((struct Ethernet_t *)NetRxPacket)->et_src, 6); #endif if (strlen(bp->bp_file) > 0) copy_filename(BootFile, bp->bp_file, sizeof(BootFile)); @@ -621,8 +621,8 @@ BootpRequest(void) */ /* NetSetIP(pkt, 0xFFFFFFFFL, PORT_BOOTPS, PORT_BOOTPC, sizeof (struct Bootp_t)); */ - iphdr = pkt; /* We need this later for NetSetIP() */ - pkt += IP_HDR_SIZE; + iphdr = pkt; /* We need this later for NetSetUDPHeader() */ + pkt += IP_UDP_HDR_SIZE;
bp = (struct Bootp_t *)pkt; bp->bp_op = OP_BOOTREQUEST; @@ -807,7 +807,7 @@ static void DhcpSendRequestPkt(struct Bootp_t *bp_offer) pkt += NetSetEther(pkt, NetBcastAddr, PROT_IP);
iphdr = pkt; /* We'll need this later to set proper pkt size */ - pkt += IP_HDR_SIZE; + pkt += IP_UDP_HDR_SIZE;
bp = (struct Bootp_t *)pkt; bp->bp_op = OP_BOOTREQUEST; diff --git a/net/bootp.h b/net/bootp.h index bf4e875..1cf9a02 100644 --- a/net/bootp.h +++ b/net/bootp.h @@ -52,7 +52,7 @@ struct Bootp_t { };
#define BOOTP_HDR_SIZE sizeof(struct Bootp_t) -#define BOOTP_SIZE (ETHER_HDR_SIZE + IP_HDR_SIZE + BOOTP_HDR_SIZE) +#define BOOTP_SIZE (ETHER_HDR_SIZE + IP_UDP_HDR_SIZE + BOOTP_HDR_SIZE)
/**********************************************************************/ /* diff --git a/net/cdp.c b/net/cdp.c index c5e54b2..d617f18 100644 --- a/net/cdp.c +++ b/net/cdp.c @@ -108,7 +108,7 @@ CDPSendTrigger(void) uchar *pkt; ushort *s; ushort *cp; - Ethernet_t *et; + struct Ethernet_t *et; int len; ushort chksum; #if defined(CONFIG_CDP_DEVICE_ID) || defined(CONFIG_CDP_PORT_ID) || \ @@ -117,7 +117,7 @@ CDPSendTrigger(void) #endif
pkt = NetTxPacket; - et = (Ethernet_t *)pkt; + et = (struct Ethernet_t *)pkt;
/* NOTE: trigger sent not on any VLAN */
diff --git a/net/dns.c b/net/dns.c index 7a3f1f9..cc7ed51 100644 --- a/net/dns.c +++ b/net/dns.c @@ -45,7 +45,7 @@ DnsSend(void) enum dns_query_type qtype = DNS_A_RECORD;
name = NetDNSResolve; - pkt = p = (uchar *)(NetTxPacket + NetEthHdrSize() + IP_HDR_SIZE); + pkt = p = (uchar *)(NetTxPacket + NetEthHdrSize() + IP_UDP_HDR_SIZE);
/* Prepare DNS packet header */ header = (struct header *) pkt; diff --git a/net/net.c b/net/net.c index fdc1ebe..023802d 100644 --- a/net/net.c +++ b/net/net.c @@ -625,12 +625,13 @@ NetSendUDPPacket(uchar *ether, IPaddr_t dest, int dport, int sport, int len) pkt += NetSetEther(pkt, NetArpWaitPacketMAC, PROT_IP);
NetSetIP(pkt, dest, dport, sport, len); - memcpy(pkt + IP_HDR_SIZE, (uchar *)NetTxPacket + - (pkt - (uchar *)NetArpWaitTxPacket) + IP_HDR_SIZE, len); + memcpy(pkt + IP_UDP_HDR_SIZE, (uchar *)NetTxPacket + + (pkt - (uchar *)NetArpWaitTxPacket) + + IP_UDP_HDR_SIZE, len);
/* size of the waiting packet */ NetArpWaitTxPacketSize = (pkt - NetArpWaitTxPacket) + - IP_HDR_SIZE + len; + IP_UDP_HDR_SIZE + len;
/* and do the ARP request */ NetArpWaitTry = 1; @@ -644,7 +645,7 @@ NetSendUDPPacket(uchar *ether, IPaddr_t dest, int dport, int sport, int len) pkt = (uchar *)NetTxPacket; pkt += NetSetEther(pkt, ether, PROT_IP); NetSetIP(pkt, dest, dport, sport, len); - (void) eth_send(NetTxPacket, (pkt - NetTxPacket) + IP_HDR_SIZE + len); + eth_send(NetTxPacket, (pkt - NetTxPacket) + IP_UDP_HDR_SIZE + len);
return 0; /* transmitted */ } @@ -668,7 +669,7 @@ NetSendUDPPacket(uchar *ether, IPaddr_t dest, int dport, int sport, int len) static struct rpc_t rpc_specimen; #define IP_PKTSIZE (CONFIG_NET_MAXDEFRAG + sizeof(rpc_specimen.u.reply))
-#define IP_MAXUDP (IP_PKTSIZE - IP_HDR_SIZE_NO_UDP) +#define IP_MAXUDP (IP_PKTSIZE - IP_HDR_SIZE)
/* * this is the packet being assembled, either data or frag control. @@ -682,22 +683,22 @@ struct hole { u16 unused; };
-static IP_t *__NetDefragment(IP_t *ip, int *lenp) +static struct IP_UDP_t *__NetDefragment(struct IP_UDP_t *ip, int *lenp) { static uchar pkt_buff[IP_PKTSIZE] __attribute__((aligned(PKTALIGN))); static u16 first_hole, total_len; struct hole *payload, *thisfrag, *h, *newh; - IP_t *localip = (IP_t *)pkt_buff; + struct IP_UDP_t *localip = (struct IP_UDP_t *)pkt_buff; uchar *indata = (uchar *)ip; int offset8, start, len, done = 0; u16 ip_off = ntohs(ip->ip_off);
/* payload starts after IP header, this fragment is in there */ - payload = (struct hole *)(pkt_buff + IP_HDR_SIZE_NO_UDP); + payload = (struct hole *)(pkt_buff + IP_HDR_SIZE); offset8 = (ip_off & IP_OFFS); thisfrag = payload + offset8; start = offset8 * 8; - len = ntohs(ip->ip_len) - IP_HDR_SIZE_NO_UDP; + len = ntohs(ip->ip_len) - IP_HDR_SIZE;
if (start + len > IP_MAXUDP) /* fragment extends too far */ return NULL; @@ -710,7 +711,7 @@ static IP_t *__NetDefragment(IP_t *ip, int *lenp) payload[0].prev_hole = 0; first_hole = 0; /* any IP header will work, copy the first we received */ - memcpy(localip, ip, IP_HDR_SIZE_NO_UDP); + memcpy(localip, ip, IP_HDR_SIZE); }
/* @@ -793,16 +794,16 @@ static IP_t *__NetDefragment(IP_t *ip, int *lenp) }
/* finally copy this fragment and possibly return whole packet */ - memcpy((uchar *)thisfrag, indata + IP_HDR_SIZE_NO_UDP, len); + memcpy((uchar *)thisfrag, indata + IP_HDR_SIZE, len); if (!done) return NULL;
localip->ip_len = htons(total_len); - *lenp = total_len + IP_HDR_SIZE_NO_UDP; + *lenp = total_len + IP_HDR_SIZE; return localip; }
-static inline IP_t *NetDefragment(IP_t *ip, int *lenp) +static inline struct IP_UDP_t *NetDefragment(struct IP_UDP_t *ip, int *lenp) { u16 ip_off = ntohs(ip->ip_off); if (!(ip_off & (IP_OFFS | IP_FLAGS_MFRAG))) @@ -812,7 +813,7 @@ static inline IP_t *NetDefragment(IP_t *ip, int *lenp)
#else /* !CONFIG_IP_DEFRAG */
-static inline IP_t *NetDefragment(IP_t *ip, int *lenp) +static inline struct IP_UDP_t *NetDefragment(struct IP_UDP_t *ip, int *lenp) { u16 ip_off = ntohs(ip->ip_off); if (!(ip_off & (IP_OFFS | IP_FLAGS_MFRAG))) @@ -827,9 +828,10 @@ static inline IP_t *NetDefragment(IP_t *ip, int *lenp) * * @parma ip IP packet containing the ICMP */ -static void receive_icmp(IP_t *ip, int len, IPaddr_t src_ip, Ethernet_t *et) +static void receive_icmp(struct IP_UDP_t *ip, int len, + IPaddr_t src_ip, struct Ethernet_t *et) { - ICMP_t *icmph = (ICMP_t *)&ip->udp_src; + struct ICMP_t *icmph = (struct ICMP_t *)&(ip->udp_src);
switch (icmph->type) { case ICMP_REDIRECT: @@ -855,8 +857,8 @@ static void receive_icmp(IP_t *ip, int len, IPaddr_t src_ip, Ethernet_t *et) void NetReceive(volatile uchar *inpkt, int len) { - Ethernet_t *et; - IP_t *ip; + struct Ethernet_t *et; + struct IP_UDP_t *ip; IPaddr_t tmp; IPaddr_t src_ip; int x; @@ -869,7 +871,7 @@ NetReceive(volatile uchar *inpkt, int len)
NetRxPacket = (uchar *)inpkt; NetRxPacketLen = len; - et = (Ethernet_t *)NetRxPacket; + et = (struct Ethernet_t *)NetRxPacket;
/* too small packet? */ if (len < ETHER_HDR_SIZE) @@ -904,15 +906,15 @@ NetReceive(volatile uchar *inpkt, int len) */ x = ntohs(et->et_prot);
- ip = (IP_t *)(NetRxPacket + E802_HDR_SIZE); + ip = (struct IP_UDP_t *)(NetRxPacket + E802_HDR_SIZE); len -= E802_HDR_SIZE;
} else if (x != PROT_VLAN) { /* normal packet */ - ip = (IP_t *)(NetRxPacket + ETHER_HDR_SIZE); + ip = (struct IP_UDP_t *)(NetRxPacket + ETHER_HDR_SIZE); len -= ETHER_HDR_SIZE;
} else { /* VLAN packet */ - VLAN_Ethernet_t *vet = (VLAN_Ethernet_t *)et; + struct VLAN_Ethernet_t *vet = (struct VLAN_Ethernet_t *)et;
debug("VLAN packet received\n");
@@ -932,7 +934,7 @@ NetReceive(volatile uchar *inpkt, int len) vlanid = cti & VLAN_IDMASK; x = ntohs(vet->vet_type);
- ip = (IP_t *)(NetRxPacket + VLAN_ETHER_HDR_SIZE); + ip = (struct IP_UDP_t *)(NetRxPacket + VLAN_ETHER_HDR_SIZE); len -= VLAN_ETHER_HDR_SIZE; }
@@ -967,8 +969,9 @@ NetReceive(volatile uchar *inpkt, int len) case PROT_IP: debug("Got IP\n"); /* Before we start poking the header, make sure it is there */ - if (len < IP_HDR_SIZE) { - debug("len bad %d < %lu\n", len, (ulong)IP_HDR_SIZE); + if (len < IP_UDP_HDR_SIZE) { + debug("len bad %d < %lu\n", len, + (ulong)IP_UDP_HDR_SIZE); return; } /* Check the packet length */ @@ -986,7 +989,7 @@ NetReceive(volatile uchar *inpkt, int len) if ((ip->ip_hl_v & 0x0f) > 0x05) return; /* Check the Checksum of the header */ - if (!NetCksumOk((uchar *)ip, IP_HDR_SIZE_NO_UDP / 2)) { + if (!NetCksumOk((uchar *)ip, IP_HDR_SIZE / 2)) { puts("checksum bad\n"); return; } @@ -1080,19 +1083,19 @@ NetReceive(volatile uchar *inpkt, int len)
#ifdef CONFIG_NETCONSOLE - nc_input_packet((uchar *)ip + IP_HDR_SIZE, - ntohs(ip->udp_dst), - ntohs(ip->udp_src), - ntohs(ip->udp_len) - 8); + nc_input_packet((uchar *)ip + IP_UDP_HDR_SIZE, + ntohs(ip->udp_dst), + ntohs(ip->udp_src), + ntohs(ip->udp_len) - UDP_HDR_SIZE); #endif /* * IP header OK. Pass the packet to the current handler. */ - (*packetHandler)((uchar *)ip + IP_HDR_SIZE, - ntohs(ip->udp_dst), - src_ip, - ntohs(ip->udp_src), - ntohs(ip->udp_len) - 8); + (*packetHandler)((uchar *)ip + IP_UDP_HDR_SIZE, + ntohs(ip->udp_dst), + src_ip, + ntohs(ip->udp_src), + ntohs(ip->udp_len) - UDP_HDR_SIZE); break; } } @@ -1221,7 +1224,7 @@ NetEthHdrSize(void) int NetSetEther(uchar *xet, uchar * addr, uint prot) { - Ethernet_t *et = (Ethernet_t *)xet; + struct Ethernet_t *et = (struct Ethernet_t *)xet; ushort myvlanid;
myvlanid = ntohs(NetOurVLAN); @@ -1234,7 +1237,7 @@ NetSetEther(uchar *xet, uchar * addr, uint prot) et->et_protlen = htons(prot); return ETHER_HDR_SIZE; } else { - VLAN_Ethernet_t *vet = (VLAN_Ethernet_t *)xet; + struct VLAN_Ethernet_t *vet = (struct VLAN_Ethernet_t *)xet;
vet->vet_vlan_type = htons(PROT_VLAN); vet->vet_tag = htons((0 << 5) | (myvlanid & VLAN_IDMASK)); @@ -1246,7 +1249,7 @@ NetSetEther(uchar *xet, uchar * addr, uint prot) void NetSetIP(uchar *xip, IPaddr_t dest, int dport, int sport, int len) { - IP_t *ip = (IP_t *)xip; + struct IP_UDP_t *ip = (struct IP_UDP_t *)xip;
/* * If the data is an odd number of bytes, zero the @@ -1254,7 +1257,7 @@ NetSetIP(uchar *xip, IPaddr_t dest, int dport, int sport, int len) * will work. */ if (len & 1) - xip[IP_HDR_SIZE + len] = 0; + xip[IP_UDP_HDR_SIZE + len] = 0;
/* * Construct an IP and UDP header. @@ -1263,7 +1266,7 @@ NetSetIP(uchar *xip, IPaddr_t dest, int dport, int sport, int len) /* IP_HDR_SIZE / 4 (not including UDP) */ ip->ip_hl_v = 0x45; ip->ip_tos = 0; - ip->ip_len = htons(IP_HDR_SIZE + len); + ip->ip_len = htons(IP_UDP_HDR_SIZE + len); ip->ip_id = htons(NetIPID++); ip->ip_off = htons(IP_FLAGS_DFRAG); /* Don't fragment */ ip->ip_ttl = 255; @@ -1275,9 +1278,9 @@ NetSetIP(uchar *xip, IPaddr_t dest, int dport, int sport, int len) NetCopyIP((void *)&ip->ip_dst, &dest); ip->udp_src = htons(sport); ip->udp_dst = htons(dport); - ip->udp_len = htons(8 + len); + ip->udp_len = htons(UDP_HDR_SIZE + len); ip->udp_xsum = 0; - ip->ip_sum = ~NetCksum((uchar *)ip, IP_HDR_SIZE_NO_UDP / 2); + ip->ip_sum = ~NetCksum((uchar *)ip, IP_HDR_SIZE / 2); }
void copy_filename(char *dst, const char *src, int size) diff --git a/net/nfs.c b/net/nfs.c index 54f56c4..b6188fe 100644 --- a/net/nfs.c +++ b/net/nfs.c @@ -189,7 +189,7 @@ rpc_req(int rpc_prog, int rpc_proc, uint32_t *data, int datalen)
pktlen = (char *)p + datalen*sizeof(uint32_t) - (char *)&pkt;
- memcpy((char *)NetTxPacket + NetEthHdrSize() + IP_HDR_SIZE, + memcpy((char *)NetTxPacket + NetEthHdrSize() + IP_UDP_HDR_SIZE, (char *)&pkt, pktlen);
if (rpc_prog == PROG_PORTMAP) diff --git a/net/ping.c b/net/ping.c index 4a66b8d..34e287c 100644 --- a/net/ping.c +++ b/net/ping.c @@ -19,7 +19,7 @@ IPaddr_t NetPingIP; static int PingSend(void) { static uchar mac[6]; - IP_t *ip; + struct IP_UDP_t *ip; ushort *s; uchar *pkt;
@@ -35,7 +35,7 @@ static int PingSend(void) pkt = NetArpWaitTxPacket; pkt += NetSetEther(pkt, mac, PROT_IP);
- ip = (IP_t *)pkt; + ip = (struct IP_UDP_t *)pkt;
/* * Construct an IP and ICMP header. @@ -44,7 +44,7 @@ static int PingSend(void) /* IP_HDR_SIZE / 4 (not including UDP) */ ip->ip_hl_v = 0x45; ip->ip_tos = 0; - ip->ip_len = htons(IP_HDR_SIZE_NO_UDP + 8); + ip->ip_len = htons(IP_HDR_SIZE + 8); ip->ip_id = htons(NetIPID++); ip->ip_off = htons(IP_FLAGS_DFRAG); /* Don't fragment */ ip->ip_ttl = 255; @@ -54,7 +54,7 @@ static int PingSend(void) NetCopyIP((void *)&ip->ip_src, &NetOurIP); /* - "" - */ NetCopyIP((void *)&ip->ip_dst, &NetPingIP); - ip->ip_sum = ~NetCksum((uchar *)ip, IP_HDR_SIZE_NO_UDP / 2); + ip->ip_sum = ~NetCksum((uchar *)ip, IP_HDR_SIZE / 2);
s = &ip->udp_src; /* XXX ICMP starts here */ s[0] = htons(0x0800); /* echo-request, code */ @@ -65,7 +65,7 @@ static int PingSend(void)
/* size of the waiting packet */ NetArpWaitTxPacketSize = - (pkt - NetArpWaitTxPacket) + IP_HDR_SIZE_NO_UDP + 8; + (pkt - NetArpWaitTxPacket) + IP_HDR_SIZE + 8;
/* and do the ARP request */ NetArpWaitTry = 1; @@ -101,9 +101,9 @@ PingStart(void) PingSend(); }
-void PingReceive(Ethernet_t *et, IP_t *ip, int len) +void PingReceive(struct Ethernet_t *et, struct IP_UDP_t *ip, int len) { - ICMP_t *icmph = (ICMP_t *)&(ip->udp_src); + struct ICMP_t *icmph = (struct ICMP_t *)&(ip->udp_src); IPaddr_t src_ip;
switch (icmph->type) { @@ -128,12 +128,12 @@ void PingReceive(Ethernet_t *et, IP_t *ip, int len) NetCopyIP((void *)&ip->ip_dst, &ip->ip_src); NetCopyIP((void *)&ip->ip_src, &NetOurIP); ip->ip_sum = ~NetCksum((uchar *)ip, - IP_HDR_SIZE_NO_UDP >> 1); + IP_HDR_SIZE >> 1);
icmph->type = ICMP_ECHO_REPLY; icmph->checksum = 0; icmph->checksum = ~NetCksum((uchar *)icmph, - (len - IP_HDR_SIZE_NO_UDP) >> 1); + (len - IP_HDR_SIZE) >> 1); (void) eth_send((uchar *)et, ETHER_HDR_SIZE + len); return; diff --git a/net/ping.h b/net/ping.h index 56aad43..4671909 100644 --- a/net/ping.h +++ b/net/ping.h @@ -15,6 +15,6 @@ #include <net.h>
void PingStart(void); -void PingReceive(Ethernet_t *et, IP_t *ip, int len); +void PingReceive(struct Ethernet_t *et, struct IP_UDP_t *ip, int len);
#endif /* __PING_H__ */ diff --git a/net/rarp.c b/net/rarp.c index 761560c..305b0a4 100644 --- a/net/rarp.c +++ b/net/rarp.c @@ -42,12 +42,12 @@ int RarpTry; * Handle a RARP received packet. */ void -RarpReceive(IP_t *ip, unsigned len) +RarpReceive(struct IP_UDP_t *ip, unsigned len) { - ARP_t *arp; + struct ARP_t *arp;
debug("Got RARP\n"); - arp = (ARP_t *)ip; + arp = (struct ARP_t *)ip; if (len < ARP_HDR_SIZE) { printf("bad length %d < %d\n", len, ARP_HDR_SIZE); return; @@ -90,14 +90,14 @@ void RarpRequest(void) { uchar *pkt; - ARP_t *rarp; + struct ARP_t *rarp;
printf("RARP broadcast %d\n", ++RarpTry); pkt = NetTxPacket;
pkt += NetSetEther(pkt, NetBcastAddr, PROT_RARP);
- rarp = (ARP_t *)pkt; + rarp = (struct ARP_t *)pkt;
rarp->ar_hrd = htons(ARP_ETHER); rarp->ar_pro = htons(PROT_IP); diff --git a/net/rarp.h b/net/rarp.h index 0d728dc..8723dd3 100644 --- a/net/rarp.h +++ b/net/rarp.h @@ -37,7 +37,7 @@
extern int RarpTry;
-extern void RarpReceive(IP_t *ip, unsigned len); +extern void RarpReceive(struct IP_UDP_t *ip, unsigned len); extern void RarpRequest(void); /* Send a RARP request */
/**********************************************************************/ diff --git a/net/sntp.c b/net/sntp.c index 6dd0dbd..7997f98 100644 --- a/net/sntp.c +++ b/net/sntp.c @@ -31,7 +31,7 @@ SntpSend(void) pkt.vn = NTP_VERSION; pkt.mode = NTP_MODE_CLIENT;
- memcpy((char *)NetTxPacket + NetEthHdrSize() + IP_HDR_SIZE, + memcpy((char *)NetTxPacket + NetEthHdrSize() + IP_UDP_HDR_SIZE, (char *)&pkt, pktlen);
SntpOurPort = 10000 + (get_timer(0) % 4096); diff --git a/net/tftp.c b/net/tftp.c index bc7fe05..a04a832 100644 --- a/net/tftp.c +++ b/net/tftp.c @@ -322,7 +322,7 @@ TftpSend(void) * We will always be sending some sort of packet, so * cobble together the packet headers now. */ - pkt = (uchar *)(NetTxPacket + NetEthHdrSize() + IP_HDR_SIZE); + pkt = NetTxPacket + NetEthHdrSize() + IP_UDP_HDR_SIZE;
switch (TftpState) { case STATE_SEND_RRQ:

On Thursday 19 January 2012 19:53:12 Joe Hershberger wrote:
Un-typedef IP_t, Ethernet_t, VLAN_Ethernet_t, ARP_t, ICMP_t
i'm not sure about this. i don't mind the typedefs myself and they have the advantage of already being done ... let's see what Wolfgang prefers.
The existing definition of IP_t includes UDP header, so name it to accurately describe the structure
i feel like we should split it out. drop the UDP parts from IP_t and add a new IP_UDP_t. much of the code operates on UDP packages, but not all (such as the main NetReceive func). -mike

Hi Mike,
On Fri, Feb 3, 2012 at 6:05 AM, Mike Frysinger vapier@gentoo.org wrote:
On Thursday 19 January 2012 19:53:12 Joe Hershberger wrote:
Un-typedef IP_t, Ethernet_t, VLAN_Ethernet_t, ARP_t, ICMP_t
i'm not sure about this. i don't mind the typedefs myself and they have the advantage of already being done ... let's see what Wolfgang prefers.
Wolfgang was clearly against typedef'ed structs when I asked him in October of last year.
On Thu, Oct 13, 2011 at 3:36 PM, Wolfgang Denk wd@denx.de wrote:
In message CANr=Z=bmm64Qxv+Zpc=GShY7c2KapAgh9xm-cq79TM1oQ76V5A@mail.gmail.com you wrote:
WARNING:NEW_TYPEDEFS: do not add new typedefs This seems rather limiting... I'm not sure why even Linux would want this, at least when it applies to typedefs of structs. It makes sense if it's a new typedef for int or something.
See the CodingStyle, start reading at "It's a _mistake_ to use typedef for structures and pointers."
That's why I didn't leave them to be complained about by checkpatch.
-Joe

Rename parameter len to payload_len in NetSendUDPPacket: this name more explicitly claims that it does not include the header size Rename CDPHandler to CDPReceive: this is not called as a handler, so don't name it that way Rename OPT_SIZE to OPT_FIELD_SIZE: clearer constant name and also remove related BOOTP_SIZE which was unused and doesn't take into account VLAN packets Rename tmp to reply_ip_addr in arp.c Alphabetize includes in net.c Replace magic numbers in arp.c with constants Add a more explicit comment about 802.2
Signed-off-by: Joe Hershberger joe.hershberger@ni.com Cc: Joe Hershberger joe.hershberger@gmail.com Cc: Wolfgang Denk wd@denx.de --- include/net.h | 11 +++++++- net/arp.c | 38 ++++++++++++++++---------------- net/bootp.c | 10 ++++---- net/bootp.h | 7 ++--- net/cdp.c | 2 +- net/cdp.h | 2 +- net/net.c | 67 +++++++++++++++++++++++++++++--------------------------- 7 files changed, 73 insertions(+), 64 deletions(-)
diff --git a/include/net.h b/include/net.h index 9de1181..add2080 100644 --- a/include/net.h +++ b/include/net.h @@ -169,7 +169,8 @@ struct Ethernet_t { };
#define ETHER_HDR_SIZE 14 /* Ethernet header size */ -#define E802_HDR_SIZE 22 /* 802 ethernet header size */ + /* 802.2 + SNAP + ethernet header size */ +#define E802_HDR_SIZE (ETHER_HDR_SIZE + 8)
/* * Ethernet header @@ -231,7 +232,9 @@ struct ARP_t { # define ARP_ETHER 1 /* Ethernet hardware address */ ushort ar_pro; /* Format of protocol address */ uchar ar_hln; /* Length of hardware address */ +# define ARP_HLEN 6 uchar ar_pln; /* Length of protocol address */ +# define ARP_PLEN 4 ushort ar_op; /* Operation */ # define ARPOP_REQUEST 1 /* Request to resolve address */ # define ARPOP_REPLY 2 /* Response to previous request */ @@ -245,6 +248,10 @@ struct ARP_t { * specific hardware/protocol combinations. */ uchar ar_data[0]; +#define ar_sha ar_data[0] +#define ar_spa ar_data[ARP_HLEN] +#define ar_tha ar_data[ARP_HLEN + ARP_PLEN] +#define ar_tpa ar_data[ARP_HLEN + ARP_PLEN + ARP_HLEN] #if 0 uchar ar_sha[]; /* Sender hardware address */ uchar ar_spa[]; /* Sender protocol address */ @@ -431,7 +438,7 @@ extern void NetSendPacket(uchar *, int);
/* Transmit UDP packet, performing ARP request if needed */ extern int NetSendUDPPacket(uchar *ether, IPaddr_t dest, int dport, - int sport, int len); + int sport, int payload_len);
/* Processes a received packet */ extern void NetReceive(volatile uchar *, int); diff --git a/net/arp.c b/net/arp.c index 96ffb85..456decd 100644 --- a/net/arp.c +++ b/net/arp.c @@ -63,16 +63,16 @@ void ArpRequest(void)
arp->ar_hrd = htons(ARP_ETHER); arp->ar_pro = htons(PROT_IP); - arp->ar_hln = 6; - arp->ar_pln = 4; + arp->ar_hln = ARP_HLEN; + arp->ar_pln = ARP_PLEN; arp->ar_op = htons(ARPOP_REQUEST);
/* source ET addr */ - memcpy(&arp->ar_data[0], NetOurEther, 6); + memcpy(&arp->ar_sha, NetOurEther, ARP_HLEN); /* source IP addr */ - NetWriteIP((uchar *) &arp->ar_data[6], NetOurIP); + NetWriteIP(&arp->ar_spa, NetOurIP); /* dest ET addr = 0 */ - memset(&arp->ar_data[10], '\0', 6); + memset(&arp->ar_tha, 0, ARP_HLEN); if ((NetArpWaitPacketIP & NetOurSubnetMask) != (NetOurIP & NetOurSubnetMask)) { if (NetOurGatewayIP == 0) { @@ -85,7 +85,7 @@ void ArpRequest(void) NetArpWaitReplyIP = NetArpWaitPacketIP; }
- NetWriteIP((uchar *) &arp->ar_data[16], NetArpWaitReplyIP); + NetWriteIP(&arp->ar_tpa, NetArpWaitReplyIP); (void) eth_send(NetTxPacket, (pkt - NetTxPacket) + ARP_HDR_SIZE); }
@@ -116,7 +116,7 @@ void ArpTimeoutCheck(void) void ArpReceive(struct Ethernet_t *et, struct IP_UDP_t *ip, int len) { struct ARP_t *arp; - IPaddr_t tmp; + IPaddr_t reply_ip_addr; uchar *pkt;
/* @@ -139,15 +139,15 @@ void ArpReceive(struct Ethernet_t *et, struct IP_UDP_t *ip, int len) return; if (ntohs(arp->ar_pro) != PROT_IP) return; - if (arp->ar_hln != 6) + if (arp->ar_hln != ARP_HLEN) return; - if (arp->ar_pln != 4) + if (arp->ar_pln != ARP_PLEN) return;
if (NetOurIP == 0) return;
- if (NetReadIP(&arp->ar_data[16]) != NetOurIP) + if (NetReadIP(&arp->ar_tpa) != NetOurIP) return;
switch (ntohs(arp->ar_op)) { @@ -157,10 +157,10 @@ void ArpReceive(struct Ethernet_t *et, struct IP_UDP_t *ip, int len) pkt = (uchar *)et; pkt += NetSetEther(pkt, et->et_src, PROT_ARP); arp->ar_op = htons(ARPOP_REPLY); - memcpy(&arp->ar_data[10], &arp->ar_data[0], 6); - NetCopyIP(&arp->ar_data[16], &arp->ar_data[6]); - memcpy(&arp->ar_data[0], NetOurEther, 6); - NetCopyIP(&arp->ar_data[6], &NetOurIP); + memcpy(&arp->ar_tha, &arp->ar_sha, ARP_HLEN); + NetCopyIP(&arp->ar_tpa, &arp->ar_spa); + memcpy(&arp->ar_sha, NetOurEther, ARP_HLEN); + NetCopyIP(&arp->ar_spa, &NetOurIP); (void) eth_send((uchar *)et, (pkt - (uchar *)et) + ARP_HDR_SIZE); return; @@ -173,28 +173,28 @@ void ArpReceive(struct Ethernet_t *et, struct IP_UDP_t *ip, int len) #ifdef CONFIG_KEEP_SERVERADDR if (NetServerIP == NetArpWaitPacketIP) { char buf[20]; - sprintf(buf, "%pM", arp->ar_data); + sprintf(buf, "%pM", arp->ar_sha); setenv("serveraddr", buf); } #endif
- tmp = NetReadIP(&arp->ar_data[6]); + reply_ip_addr = NetReadIP(&arp->ar_spa);
/* matched waiting packet's address */ - if (tmp == NetArpWaitReplyIP) { + if (reply_ip_addr == NetArpWaitReplyIP) { debug("Got ARP REPLY, set eth addr (%pM)\n", arp->ar_data);
/* save address for later use */ memcpy(NetArpWaitPacketMAC, - &arp->ar_data[0], 6); + &arp->ar_sha, ARP_HLEN);
#ifdef CONFIG_NETCONSOLE NetGetHandler()(0, 0, 0, 0, 0); #endif /* modify header, and transmit it */ memcpy(((struct Ethernet_t *)NetArpWaitTxPacket)-> - et_dest, NetArpWaitPacketMAC, 6); + et_dest, NetArpWaitPacketMAC, ARP_HLEN); (void) eth_send(NetArpWaitTxPacket, NetArpWaitTxPacketSize);
diff --git a/net/bootp.c b/net/bootp.c index e95419f..2be8083 100644 --- a/net/bootp.c +++ b/net/bootp.c @@ -73,7 +73,7 @@ static int BootpCheckPkt(uchar *pkt, unsigned dest, unsigned src, unsigned len)
if (dest != PORT_BOOTPC || src != PORT_BOOTPS) retval = -1; - else if (len < sizeof(struct Bootp_t) - OPT_SIZE) + else if (len < sizeof(struct Bootp_t) - OPT_FIELD_SIZE) retval = -2; else if (bp->bp_op != OP_BOOTREQUEST && bp->bp_op != OP_BOOTREPLY && @@ -369,8 +369,8 @@ static int DhcpExtended(u8 *e, int message_type, IPaddr_t ServerID,
*e++ = 57; /* Maximum DHCP Message Size */ *e++ = 2; - *e++ = (576 - 312 + OPT_SIZE) >> 8; - *e++ = (576 - 312 + OPT_SIZE) & 0xff; + *e++ = (576 - 312 + OPT_FIELD_SIZE) >> 8; + *e++ = (576 - 312 + OPT_FIELD_SIZE) & 0xff;
if (ServerID) { int tmp = ntohl(ServerID); @@ -520,8 +520,8 @@ static int BootpExtended(u8 *e)
*e++ = 57; /* Maximum DHCP Message Size */ *e++ = 2; - *e++ = (576 - 312 + OPT_SIZE) >> 16; - *e++ = (576 - 312 + OPT_SIZE) & 0xff; + *e++ = (576 - 312 + OPT_FIELD_SIZE) >> 16; + *e++ = (576 - 312 + OPT_FIELD_SIZE) & 0xff; #endif
#if defined(CONFIG_BOOTP_SUBNETMASK) diff --git a/net/bootp.h b/net/bootp.h index 1cf9a02..ecbcc4d 100644 --- a/net/bootp.h +++ b/net/bootp.h @@ -20,13 +20,13 @@ */ #if defined(CONFIG_CMD_DHCP) /* Minimum DHCP Options size per RFC2131 - results in 576 byte pkt */ -#define OPT_SIZE 312 +#define OPT_FIELD_SIZE 312 #if defined(CONFIG_BOOTP_VENDOREX) extern u8 *dhcp_vendorex_prep(u8 *e); /*rtn new e after add own opts. */ extern u8 *dhcp_vendorex_proc(u8 *e); /*rtn next e if mine,else NULL */ #endif #else -#define OPT_SIZE 64 +#define OPT_FIELD_SIZE 64 #endif
struct Bootp_t { @@ -48,11 +48,10 @@ struct Bootp_t { uchar bp_chaddr[16]; /* Client hardware address */ char bp_sname[64]; /* Server host name */ char bp_file[128]; /* Boot file name */ - char bp_vend[OPT_SIZE]; /* Vendor information */ + char bp_vend[OPT_FIELD_SIZE]; /* Vendor information */ };
#define BOOTP_HDR_SIZE sizeof(struct Bootp_t) -#define BOOTP_SIZE (ETHER_HDR_SIZE + IP_UDP_HDR_SIZE + BOOTP_HDR_SIZE)
/**********************************************************************/ /* diff --git a/net/cdp.c b/net/cdp.c index d617f18..38b79bd 100644 --- a/net/cdp.c +++ b/net/cdp.c @@ -245,7 +245,7 @@ CDPDummyHandler(uchar *pkt, unsigned dest, IPaddr_t sip, unsigned src, }
void -CDPHandler(const uchar *pkt, unsigned len) +CDPReceive(const uchar *pkt, unsigned len) { const uchar *t; const ushort *ss; diff --git a/net/cdp.h b/net/cdp.h index fef744e..88191c4 100644 --- a/net/cdp.h +++ b/net/cdp.h @@ -12,7 +12,7 @@ #define __CDP_H__
void CDPStart(void); -void CDPHandler(const uchar *pkt, unsigned len); +void CDPReceive(const uchar *pkt, unsigned len);
#endif /* __CDP_H__ */
diff --git a/net/net.c b/net/net.c index 023802d..9bf74d5 100644 --- a/net/net.c +++ b/net/net.c @@ -75,32 +75,32 @@
#include <common.h> -#include <watchdog.h> #include <command.h> #include <net.h> -#include "arp.h" -#include "bootp.h" -#include "tftp.h" -#ifdef CONFIG_CMD_RARP -#include "rarp.h" -#endif -#include "nfs.h" -#ifdef CONFIG_STATUS_LED +#if defined(CONFIG_STATUS_LED) #include <status_led.h> #include <miiphy.h> #endif -#if defined(CONFIG_CMD_SNTP) -#include "sntp.h" -#endif +#include <watchdog.h> +#include "arp.h" +#include "bootp.h" #if defined(CONFIG_CMD_CDP) #include "cdp.h" #endif #if defined(CONFIG_CMD_DNS) #include "dns.h" #endif +#include "nfs.h" #if defined(CONFIG_CMD_PING) #include "ping.h" #endif +#if defined(CONFIG_CMD_RARP) +#include "rarp.h" +#endif +#if defined(CONFIG_CMD_SNTP) +#include "sntp.h" +#endif +#include "tftp.h"
DECLARE_GLOBAL_DATA_PTR;
@@ -598,7 +598,8 @@ NetSendPacket(uchar *pkt, int len) }
int -NetSendUDPPacket(uchar *ether, IPaddr_t dest, int dport, int sport, int len) +NetSendUDPPacket(uchar *ether, IPaddr_t dest, int dport, int sport, + int payload_len) { uchar *pkt;
@@ -624,14 +625,14 @@ NetSendUDPPacket(uchar *ether, IPaddr_t dest, int dport, int sport, int len) pkt = NetArpWaitTxPacket; pkt += NetSetEther(pkt, NetArpWaitPacketMAC, PROT_IP);
- NetSetIP(pkt, dest, dport, sport, len); + NetSetIP(pkt, dest, dport, sport, payload_len); memcpy(pkt + IP_UDP_HDR_SIZE, (uchar *)NetTxPacket + (pkt - (uchar *)NetArpWaitTxPacket) + - IP_UDP_HDR_SIZE, len); + IP_UDP_HDR_SIZE, payload_len);
/* size of the waiting packet */ NetArpWaitTxPacketSize = (pkt - NetArpWaitTxPacket) + - IP_UDP_HDR_SIZE + len; + IP_UDP_HDR_SIZE + payload_len;
/* and do the ARP request */ NetArpWaitTry = 1; @@ -644,8 +645,9 @@ NetSendUDPPacket(uchar *ether, IPaddr_t dest, int dport, int sport, int len)
pkt = (uchar *)NetTxPacket; pkt += NetSetEther(pkt, ether, PROT_IP); - NetSetIP(pkt, dest, dport, sport, len); - eth_send(NetTxPacket, (pkt - NetTxPacket) + IP_UDP_HDR_SIZE + len); + NetSetIP(pkt, dest, dport, sport, payload_len); + eth_send(NetTxPacket, (pkt - NetTxPacket) + IP_UDP_HDR_SIZE + + payload_len);
return 0; /* transmitted */ } @@ -859,9 +861,9 @@ NetReceive(volatile uchar *inpkt, int len) { struct Ethernet_t *et; struct IP_UDP_t *ip; - IPaddr_t tmp; + IPaddr_t dst_ip; IPaddr_t src_ip; - int x; + int eth_proto; #if defined(CONFIG_CMD_CDP) int iscdp; #endif @@ -896,20 +898,21 @@ NetReceive(volatile uchar *inpkt, int len) if (mynvlanid == (ushort)-1) mynvlanid = VLAN_NONE;
- x = ntohs(et->et_protlen); + eth_proto = ntohs(et->et_protlen);
debug("packet received\n");
- if (x < 1514) { + if (eth_proto < 1514) { /* - * Got a 802 packet. Check the other protocol field. + * Got a 802.2 packet. Check the other protocol field. + * XXX VLAN over 802.2+SNAP not implemented! */ - x = ntohs(et->et_prot); + eth_proto = ntohs(et->et_prot);
ip = (struct IP_UDP_t *)(NetRxPacket + E802_HDR_SIZE); len -= E802_HDR_SIZE;
- } else if (x != PROT_VLAN) { /* normal packet */ + } else if (eth_proto != PROT_VLAN) { /* normal packet */ ip = (struct IP_UDP_t *)(NetRxPacket + ETHER_HDR_SIZE); len -= ETHER_HDR_SIZE;
@@ -932,17 +935,17 @@ NetReceive(volatile uchar *inpkt, int len)
cti = ntohs(vet->vet_tag); vlanid = cti & VLAN_IDMASK; - x = ntohs(vet->vet_type); + eth_proto = ntohs(vet->vet_type);
ip = (struct IP_UDP_t *)(NetRxPacket + VLAN_ETHER_HDR_SIZE); len -= VLAN_ETHER_HDR_SIZE; }
- debug("Receive from protocol 0x%x\n", x); + debug("Receive from protocol 0x%x\n", eth_proto);
#if defined(CONFIG_CMD_CDP) if (iscdp) { - CDPHandler((uchar *)ip, len); + CDPReceive((uchar *)ip, len); return; } #endif @@ -955,7 +958,7 @@ NetReceive(volatile uchar *inpkt, int len) return; }
- switch (x) { + switch (eth_proto) {
case PROT_ARP: ArpReceive(et, ip, len); @@ -994,10 +997,10 @@ NetReceive(volatile uchar *inpkt, int len) return; } /* If it is not for us, ignore it */ - tmp = NetReadIP(&ip->ip_dst); - if (NetOurIP && tmp != NetOurIP && tmp != 0xFFFFFFFF) { + dst_ip = NetReadIP(&ip->ip_dst); + if (NetOurIP && dst_ip != NetOurIP && dst_ip != 0xFFFFFFFF) { #ifdef CONFIG_MCAST_TFTP - if (Mcast_addr != tmp) + if (Mcast_addr != dst_ip) #endif return; }

Hi Joe,
On Thu, Jan 19, 2012 at 4:53 PM, Joe Hershberger joe.hershberger@ni.com wrote:
Rename parameter len to payload_len in NetSendUDPPacket: this name more explicitly claims that it does not include the header size Rename CDPHandler to CDPReceive: this is not called as a handler, so don't name it that way Rename OPT_SIZE to OPT_FIELD_SIZE: clearer constant name and also remove related BOOTP_SIZE which was unused and doesn't take into account VLAN packets Rename tmp to reply_ip_addr in arp.c Alphabetize includes in net.c Replace magic numbers in arp.c with constants Add a more explicit comment about 802.2
Yes but why lump all of these together? It would benefit from 3-4 separate commits IMO.
Regards, Simon
Signed-off-by: Joe Hershberger joe.hershberger@ni.com Cc: Joe Hershberger joe.hershberger@gmail.com Cc: Wolfgang Denk wd@denx.de
include/net.h | 11 +++++++- net/arp.c | 38 ++++++++++++++++---------------- net/bootp.c | 10 ++++---- net/bootp.h | 7 ++--- net/cdp.c | 2 +- net/cdp.h | 2 +- net/net.c | 67 +++++++++++++++++++++++++++++--------------------------- 7 files changed, 73 insertions(+), 64 deletions(-)
diff --git a/include/net.h b/include/net.h index 9de1181..add2080 100644 --- a/include/net.h +++ b/include/net.h @@ -169,7 +169,8 @@ struct Ethernet_t { };
#define ETHER_HDR_SIZE 14 /* Ethernet header size */ -#define E802_HDR_SIZE 22 /* 802 ethernet header size */
- /* 802.2 + SNAP + ethernet header size */
+#define E802_HDR_SIZE (ETHER_HDR_SIZE + 8)
/* * Ethernet header @@ -231,7 +232,9 @@ struct ARP_t { # define ARP_ETHER 1 /* Ethernet hardware address */ ushort ar_pro; /* Format of protocol address */ uchar ar_hln; /* Length of hardware address */ +# define ARP_HLEN 6 uchar ar_pln; /* Length of protocol address */ +# define ARP_PLEN 4 ushort ar_op; /* Operation */ # define ARPOP_REQUEST 1 /* Request to resolve address */ # define ARPOP_REPLY 2 /* Response to previous request */ @@ -245,6 +248,10 @@ struct ARP_t { * specific hardware/protocol combinations. */ uchar ar_data[0]; +#define ar_sha ar_data[0] +#define ar_spa ar_data[ARP_HLEN] +#define ar_tha ar_data[ARP_HLEN + ARP_PLEN] +#define ar_tpa ar_data[ARP_HLEN + ARP_PLEN + ARP_HLEN] #if 0 uchar ar_sha[]; /* Sender hardware address */ uchar ar_spa[]; /* Sender protocol address */ @@ -431,7 +438,7 @@ extern void NetSendPacket(uchar *, int);
/* Transmit UDP packet, performing ARP request if needed */ extern int NetSendUDPPacket(uchar *ether, IPaddr_t dest, int dport,
- int sport, int len);
- int sport, int payload_len);
/* Processes a received packet */ extern void NetReceive(volatile uchar *, int); diff --git a/net/arp.c b/net/arp.c index 96ffb85..456decd 100644 --- a/net/arp.c +++ b/net/arp.c @@ -63,16 +63,16 @@ void ArpRequest(void)
arp->ar_hrd = htons(ARP_ETHER); arp->ar_pro = htons(PROT_IP);
- arp->ar_hln = 6;
- arp->ar_pln = 4;
- arp->ar_hln = ARP_HLEN;
- arp->ar_pln = ARP_PLEN;
arp->ar_op = htons(ARPOP_REQUEST);
/* source ET addr */
- memcpy(&arp->ar_data[0], NetOurEther, 6);
- memcpy(&arp->ar_sha, NetOurEther, ARP_HLEN);
/* source IP addr */
- NetWriteIP((uchar *) &arp->ar_data[6], NetOurIP);
- NetWriteIP(&arp->ar_spa, NetOurIP);
/* dest ET addr = 0 */
- memset(&arp->ar_data[10], '\0', 6);
- memset(&arp->ar_tha, 0, ARP_HLEN);
if ((NetArpWaitPacketIP & NetOurSubnetMask) != (NetOurIP & NetOurSubnetMask)) { if (NetOurGatewayIP == 0) { @@ -85,7 +85,7 @@ void ArpRequest(void) NetArpWaitReplyIP = NetArpWaitPacketIP; }
- NetWriteIP((uchar *) &arp->ar_data[16], NetArpWaitReplyIP);
- NetWriteIP(&arp->ar_tpa, NetArpWaitReplyIP);
(void) eth_send(NetTxPacket, (pkt - NetTxPacket) + ARP_HDR_SIZE); }
@@ -116,7 +116,7 @@ void ArpTimeoutCheck(void) void ArpReceive(struct Ethernet_t *et, struct IP_UDP_t *ip, int len) { struct ARP_t *arp;
- IPaddr_t tmp;
- IPaddr_t reply_ip_addr;
uchar *pkt;
/* @@ -139,15 +139,15 @@ void ArpReceive(struct Ethernet_t *et, struct IP_UDP_t *ip, int len) return; if (ntohs(arp->ar_pro) != PROT_IP) return;
- if (arp->ar_hln != 6)
- if (arp->ar_hln != ARP_HLEN)
return;
- if (arp->ar_pln != 4)
- if (arp->ar_pln != ARP_PLEN)
return;
if (NetOurIP == 0) return;
- if (NetReadIP(&arp->ar_data[16]) != NetOurIP)
- if (NetReadIP(&arp->ar_tpa) != NetOurIP)
return;
switch (ntohs(arp->ar_op)) { @@ -157,10 +157,10 @@ void ArpReceive(struct Ethernet_t *et, struct IP_UDP_t *ip, int len) pkt = (uchar *)et; pkt += NetSetEther(pkt, et->et_src, PROT_ARP); arp->ar_op = htons(ARPOP_REPLY);
- memcpy(&arp->ar_data[10], &arp->ar_data[0], 6);
- NetCopyIP(&arp->ar_data[16], &arp->ar_data[6]);
- memcpy(&arp->ar_data[0], NetOurEther, 6);
- NetCopyIP(&arp->ar_data[6], &NetOurIP);
- memcpy(&arp->ar_tha, &arp->ar_sha, ARP_HLEN);
- NetCopyIP(&arp->ar_tpa, &arp->ar_spa);
- memcpy(&arp->ar_sha, NetOurEther, ARP_HLEN);
- NetCopyIP(&arp->ar_spa, &NetOurIP);
(void) eth_send((uchar *)et, (pkt - (uchar *)et) + ARP_HDR_SIZE); return; @@ -173,28 +173,28 @@ void ArpReceive(struct Ethernet_t *et, struct IP_UDP_t *ip, int len) #ifdef CONFIG_KEEP_SERVERADDR if (NetServerIP == NetArpWaitPacketIP) { char buf[20];
- sprintf(buf, "%pM", arp->ar_data);
- sprintf(buf, "%pM", arp->ar_sha);
setenv("serveraddr", buf); } #endif
- tmp = NetReadIP(&arp->ar_data[6]);
- reply_ip_addr = NetReadIP(&arp->ar_spa);
/* matched waiting packet's address */
- if (tmp == NetArpWaitReplyIP) {
- if (reply_ip_addr == NetArpWaitReplyIP) {
debug("Got ARP REPLY, set eth addr (%pM)\n", arp->ar_data);
/* save address for later use */ memcpy(NetArpWaitPacketMAC,
- &arp->ar_data[0], 6);
- &arp->ar_sha, ARP_HLEN);
#ifdef CONFIG_NETCONSOLE NetGetHandler()(0, 0, 0, 0, 0); #endif /* modify header, and transmit it */ memcpy(((struct Ethernet_t *)NetArpWaitTxPacket)->
- et_dest, NetArpWaitPacketMAC, 6);
- et_dest, NetArpWaitPacketMAC, ARP_HLEN);
(void) eth_send(NetArpWaitTxPacket, NetArpWaitTxPacketSize);
diff --git a/net/bootp.c b/net/bootp.c index e95419f..2be8083 100644 --- a/net/bootp.c +++ b/net/bootp.c @@ -73,7 +73,7 @@ static int BootpCheckPkt(uchar *pkt, unsigned dest, unsigned src, unsigned len)
if (dest != PORT_BOOTPC || src != PORT_BOOTPS) retval = -1;
- else if (len < sizeof(struct Bootp_t) - OPT_SIZE)
- else if (len < sizeof(struct Bootp_t) - OPT_FIELD_SIZE)
retval = -2; else if (bp->bp_op != OP_BOOTREQUEST && bp->bp_op != OP_BOOTREPLY && @@ -369,8 +369,8 @@ static int DhcpExtended(u8 *e, int message_type, IPaddr_t ServerID,
*e++ = 57; /* Maximum DHCP Message Size */ *e++ = 2;
- *e++ = (576 - 312 + OPT_SIZE) >> 8;
- *e++ = (576 - 312 + OPT_SIZE) & 0xff;
- *e++ = (576 - 312 + OPT_FIELD_SIZE) >> 8;
- *e++ = (576 - 312 + OPT_FIELD_SIZE) & 0xff;
if (ServerID) { int tmp = ntohl(ServerID); @@ -520,8 +520,8 @@ static int BootpExtended(u8 *e)
*e++ = 57; /* Maximum DHCP Message Size */ *e++ = 2;
- *e++ = (576 - 312 + OPT_SIZE) >> 16;
- *e++ = (576 - 312 + OPT_SIZE) & 0xff;
- *e++ = (576 - 312 + OPT_FIELD_SIZE) >> 16;
- *e++ = (576 - 312 + OPT_FIELD_SIZE) & 0xff;
#endif
#if defined(CONFIG_BOOTP_SUBNETMASK) diff --git a/net/bootp.h b/net/bootp.h index 1cf9a02..ecbcc4d 100644 --- a/net/bootp.h +++ b/net/bootp.h @@ -20,13 +20,13 @@ */ #if defined(CONFIG_CMD_DHCP) /* Minimum DHCP Options size per RFC2131 - results in 576 byte pkt */ -#define OPT_SIZE 312 +#define OPT_FIELD_SIZE 312 #if defined(CONFIG_BOOTP_VENDOREX) extern u8 *dhcp_vendorex_prep(u8 *e); /*rtn new e after add own opts. */ extern u8 *dhcp_vendorex_proc(u8 *e); /*rtn next e if mine,else NULL */ #endif #else -#define OPT_SIZE 64 +#define OPT_FIELD_SIZE 64 #endif
struct Bootp_t { @@ -48,11 +48,10 @@ struct Bootp_t { uchar bp_chaddr[16]; /* Client hardware address */ char bp_sname[64]; /* Server host name */ char bp_file[128]; /* Boot file name */
- char bp_vend[OPT_SIZE]; /* Vendor information */
- char bp_vend[OPT_FIELD_SIZE]; /* Vendor information */
};
#define BOOTP_HDR_SIZE sizeof(struct Bootp_t) -#define BOOTP_SIZE (ETHER_HDR_SIZE + IP_UDP_HDR_SIZE + BOOTP_HDR_SIZE)
/**********************************************************************/ /* diff --git a/net/cdp.c b/net/cdp.c index d617f18..38b79bd 100644 --- a/net/cdp.c +++ b/net/cdp.c @@ -245,7 +245,7 @@ CDPDummyHandler(uchar *pkt, unsigned dest, IPaddr_t sip, unsigned src, }
void -CDPHandler(const uchar *pkt, unsigned len) +CDPReceive(const uchar *pkt, unsigned len) { const uchar *t; const ushort *ss; diff --git a/net/cdp.h b/net/cdp.h index fef744e..88191c4 100644 --- a/net/cdp.h +++ b/net/cdp.h @@ -12,7 +12,7 @@ #define __CDP_H__
void CDPStart(void); -void CDPHandler(const uchar *pkt, unsigned len); +void CDPReceive(const uchar *pkt, unsigned len);
#endif /* __CDP_H__ */
diff --git a/net/net.c b/net/net.c index 023802d..9bf74d5 100644 --- a/net/net.c +++ b/net/net.c @@ -75,32 +75,32 @@
#include <common.h> -#include <watchdog.h> #include <command.h> #include <net.h> -#include "arp.h" -#include "bootp.h" -#include "tftp.h" -#ifdef CONFIG_CMD_RARP -#include "rarp.h" -#endif -#include "nfs.h" -#ifdef CONFIG_STATUS_LED +#if defined(CONFIG_STATUS_LED) #include <status_led.h> #include <miiphy.h> #endif -#if defined(CONFIG_CMD_SNTP) -#include "sntp.h" -#endif +#include <watchdog.h> +#include "arp.h" +#include "bootp.h" #if defined(CONFIG_CMD_CDP) #include "cdp.h" #endif #if defined(CONFIG_CMD_DNS) #include "dns.h" #endif +#include "nfs.h" #if defined(CONFIG_CMD_PING) #include "ping.h" #endif +#if defined(CONFIG_CMD_RARP) +#include "rarp.h" +#endif +#if defined(CONFIG_CMD_SNTP) +#include "sntp.h" +#endif +#include "tftp.h"
DECLARE_GLOBAL_DATA_PTR;
@@ -598,7 +598,8 @@ NetSendPacket(uchar *pkt, int len) }
int -NetSendUDPPacket(uchar *ether, IPaddr_t dest, int dport, int sport, int len) +NetSendUDPPacket(uchar *ether, IPaddr_t dest, int dport, int sport,
- int payload_len)
{ uchar *pkt;
@@ -624,14 +625,14 @@ NetSendUDPPacket(uchar *ether, IPaddr_t dest, int dport, int sport, int len) pkt = NetArpWaitTxPacket; pkt += NetSetEther(pkt, NetArpWaitPacketMAC, PROT_IP);
- NetSetIP(pkt, dest, dport, sport, len);
- NetSetIP(pkt, dest, dport, sport, payload_len);
memcpy(pkt + IP_UDP_HDR_SIZE, (uchar *)NetTxPacket + (pkt - (uchar *)NetArpWaitTxPacket) +
- IP_UDP_HDR_SIZE, len);
- IP_UDP_HDR_SIZE, payload_len);
/* size of the waiting packet */ NetArpWaitTxPacketSize = (pkt - NetArpWaitTxPacket) +
- IP_UDP_HDR_SIZE + len;
- IP_UDP_HDR_SIZE + payload_len;
/* and do the ARP request */ NetArpWaitTry = 1; @@ -644,8 +645,9 @@ NetSendUDPPacket(uchar *ether, IPaddr_t dest, int dport, int sport, int len)
pkt = (uchar *)NetTxPacket; pkt += NetSetEther(pkt, ether, PROT_IP);
- NetSetIP(pkt, dest, dport, sport, len);
- eth_send(NetTxPacket, (pkt - NetTxPacket) + IP_UDP_HDR_SIZE + len);
- NetSetIP(pkt, dest, dport, sport, payload_len);
- eth_send(NetTxPacket, (pkt - NetTxPacket) + IP_UDP_HDR_SIZE +
- payload_len);
return 0; /* transmitted */ } @@ -859,9 +861,9 @@ NetReceive(volatile uchar *inpkt, int len) { struct Ethernet_t *et; struct IP_UDP_t *ip;
- IPaddr_t tmp;
- IPaddr_t dst_ip;
IPaddr_t src_ip;
- int x;
- int eth_proto;
#if defined(CONFIG_CMD_CDP) int iscdp; #endif @@ -896,20 +898,21 @@ NetReceive(volatile uchar *inpkt, int len) if (mynvlanid == (ushort)-1) mynvlanid = VLAN_NONE;
- x = ntohs(et->et_protlen);
- eth_proto = ntohs(et->et_protlen);
debug("packet received\n");
- if (x < 1514) {
- if (eth_proto < 1514) {
/*
- * Got a 802 packet. Check the other protocol field.
- * Got a 802.2 packet. Check the other protocol field.
- * XXX VLAN over 802.2+SNAP not implemented!
*/
- x = ntohs(et->et_prot);
- eth_proto = ntohs(et->et_prot);
ip = (struct IP_UDP_t *)(NetRxPacket + E802_HDR_SIZE); len -= E802_HDR_SIZE;
- } else if (x != PROT_VLAN) { /* normal packet */
- } else if (eth_proto != PROT_VLAN) { /* normal packet */
ip = (struct IP_UDP_t *)(NetRxPacket + ETHER_HDR_SIZE); len -= ETHER_HDR_SIZE;
@@ -932,17 +935,17 @@ NetReceive(volatile uchar *inpkt, int len)
cti = ntohs(vet->vet_tag); vlanid = cti & VLAN_IDMASK;
- x = ntohs(vet->vet_type);
- eth_proto = ntohs(vet->vet_type);
ip = (struct IP_UDP_t *)(NetRxPacket + VLAN_ETHER_HDR_SIZE); len -= VLAN_ETHER_HDR_SIZE; }
- debug("Receive from protocol 0x%x\n", x);
- debug("Receive from protocol 0x%x\n", eth_proto);
#if defined(CONFIG_CMD_CDP) if (iscdp) {
- CDPHandler((uchar *)ip, len);
- CDPReceive((uchar *)ip, len);
return; } #endif @@ -955,7 +958,7 @@ NetReceive(volatile uchar *inpkt, int len) return; }
- switch (x) {
- switch (eth_proto) {
case PROT_ARP: ArpReceive(et, ip, len); @@ -994,10 +997,10 @@ NetReceive(volatile uchar *inpkt, int len) return; } /* If it is not for us, ignore it */
- tmp = NetReadIP(&ip->ip_dst);
- if (NetOurIP && tmp != NetOurIP && tmp != 0xFFFFFFFF) {
- dst_ip = NetReadIP(&ip->ip_dst);
- if (NetOurIP && dst_ip != NetOurIP && dst_ip != 0xFFFFFFFF) {
#ifdef CONFIG_MCAST_TFTP
- if (Mcast_addr != tmp)
- if (Mcast_addr != dst_ip)
#endif return; } -- 1.6.0.2
U-Boot mailing list U-Boot@lists.denx.de http://lists.denx.de/mailman/listinfo/u-boot

On Tuesday 24 January 2012 01:19:14 Simon Glass wrote:
On Thu, Jan 19, 2012 at 4:53 PM, Joe Hershberger wrote:
Rename parameter len to payload_len in NetSendUDPPacket: this name more explicitly claims that it does not include the header size Rename CDPHandler to CDPReceive: this is not called as a handler, so don't name it that way Rename OPT_SIZE to OPT_FIELD_SIZE: clearer constant name and also remove related BOOTP_SIZE which was unused and doesn't take into account VLAN packets Rename tmp to reply_ip_addr in arp.c Alphabetize includes in net.c Replace magic numbers in arp.c with constants Add a more explicit comment about 802.2
Yes but why lump all of these together? It would benefit from 3-4 separate commits IMO.
+1 -mike

On Thursday 19 January 2012 19:53:13 Joe Hershberger wrote:
--- a/include/net.h +++ b/include/net.h
-#define E802_HDR_SIZE 22 /* 802 ethernet header size */
/* 802.2 + SNAP + ethernet header size */
+#define E802_HDR_SIZE (ETHER_HDR_SIZE + 8)
comment should be moved all the way to the left (i.e. no whitespace before it) -mike

ICMP (ping) was reimplementing IP header code... it now shares code.
Signed-off-by: Joe Hershberger joe.hershberger@ni.com Cc: Joe Hershberger joe.hershberger@gmail.com Cc: Wolfgang Denk wd@denx.de --- include/net.h | 4 +++- net/bootp.c | 6 +++--- net/net.c | 47 ++++++++++++++++++++++++++++------------------- net/ping.c | 52 +++++++++++++++++++++++----------------------------- 4 files changed, 57 insertions(+), 52 deletions(-)
diff --git a/include/net.h b/include/net.h index add2080..19e9463 100644 --- a/include/net.h +++ b/include/net.h @@ -421,7 +421,9 @@ extern int NetEthHdrSize(void); extern int NetSetEther(uchar *, uchar *, uint);
/* Set IP header */ -extern void NetSetIP(uchar *, IPaddr_t, int, int, int); +extern void NetSetIPHeader(uchar *pkt, IPaddr_t dest, IPaddr_t source); +extern void NetSetUDPHeader(uchar *pkt, IPaddr_t dest, int dport, + int sport, int len);
/* Checksum */ extern int NetCksumOk(uchar *, int); /* Return true if cksum OK */ diff --git a/net/bootp.c b/net/bootp.c index 2be8083..0c2af48 100644 --- a/net/bootp.c +++ b/net/bootp.c @@ -619,7 +619,7 @@ BootpRequest(void) * determined. * C. Hallinan, DS4.COM, Inc. */ - /* NetSetIP(pkt, 0xFFFFFFFFL, PORT_BOOTPS, PORT_BOOTPC, + /* NetSetUDPHeader(pkt, 0xFFFFFFFFL, PORT_BOOTPS, PORT_BOOTPC, sizeof (struct Bootp_t)); */ iphdr = pkt; /* We need this later for NetSetUDPHeader() */ pkt += IP_UDP_HDR_SIZE; @@ -663,7 +663,7 @@ BootpRequest(void) pktlen = ((int)(pkt-NetTxPacket)) + BOOTP_HDR_SIZE - sizeof(bp->bp_vend) + ext_len; iplen = BOOTP_HDR_SIZE - sizeof(bp->bp_vend) + ext_len; - NetSetIP(iphdr, 0xFFFFFFFFL, PORT_BOOTPS, PORT_BOOTPC, iplen); + NetSetUDPHeader(iphdr, 0xFFFFFFFFL, PORT_BOOTPS, PORT_BOOTPC, iplen); NetSetTimeout(SELECT_TIMEOUT, BootpTimeout);
#if defined(CONFIG_CMD_DHCP) @@ -844,7 +844,7 @@ static void DhcpSendRequestPkt(struct Bootp_t *bp_offer) pktlen = ((int)(pkt-NetTxPacket)) + BOOTP_HDR_SIZE - sizeof(bp->bp_vend) + extlen; iplen = BOOTP_HDR_SIZE - sizeof(bp->bp_vend) + extlen; - NetSetIP(iphdr, 0xFFFFFFFFL, PORT_BOOTPS, PORT_BOOTPC, iplen); + NetSetUDPHeader(iphdr, 0xFFFFFFFFL, PORT_BOOTPS, PORT_BOOTPC, iplen);
debug("Transmitting DHCPREQUEST packet: len = %d\n", pktlen); #ifdef CONFIG_BOOTP_DHCP_REQUEST_DELAY diff --git a/net/net.c b/net/net.c index 9bf74d5..a47b215 100644 --- a/net/net.c +++ b/net/net.c @@ -625,7 +625,7 @@ NetSendUDPPacket(uchar *ether, IPaddr_t dest, int dport, int sport, pkt = NetArpWaitTxPacket; pkt += NetSetEther(pkt, NetArpWaitPacketMAC, PROT_IP);
- NetSetIP(pkt, dest, dport, sport, payload_len); + NetSetUDPHeader(pkt, dest, dport, sport, payload_len); memcpy(pkt + IP_UDP_HDR_SIZE, (uchar *)NetTxPacket + (pkt - (uchar *)NetArpWaitTxPacket) + IP_UDP_HDR_SIZE, payload_len); @@ -645,7 +645,7 @@ NetSendUDPPacket(uchar *ether, IPaddr_t dest, int dport, int sport,
pkt = (uchar *)NetTxPacket; pkt += NetSetEther(pkt, ether, PROT_IP); - NetSetIP(pkt, dest, dport, sport, payload_len); + NetSetUDPHeader(pkt, dest, dport, sport, payload_len); eth_send(NetTxPacket, (pkt - NetTxPacket) + IP_UDP_HDR_SIZE + payload_len);
@@ -1250,40 +1250,49 @@ NetSetEther(uchar *xet, uchar * addr, uint prot) }
void -NetSetIP(uchar *xip, IPaddr_t dest, int dport, int sport, int len) +NetSetIPHeader(uchar *pkt, IPaddr_t dest, IPaddr_t source) { - struct IP_UDP_t *ip = (struct IP_UDP_t *)xip; + struct IP_UDP_t *ip = (struct IP_UDP_t *)pkt;
/* - * If the data is an odd number of bytes, zero the - * byte after the last byte so that the checksum - * will work. - */ - if (len & 1) - xip[IP_UDP_HDR_SIZE + len] = 0; - - /* - * Construct an IP and UDP header. - * (need to set no fragment bit - XXX) + * Construct an IP header. */ /* IP_HDR_SIZE / 4 (not including UDP) */ ip->ip_hl_v = 0x45; ip->ip_tos = 0; - ip->ip_len = htons(IP_UDP_HDR_SIZE + len); + ip->ip_len = htons(IP_HDR_SIZE); ip->ip_id = htons(NetIPID++); ip->ip_off = htons(IP_FLAGS_DFRAG); /* Don't fragment */ ip->ip_ttl = 255; - ip->ip_p = 17; /* UDP */ ip->ip_sum = 0; /* already in network byte order */ - NetCopyIP((void *)&ip->ip_src, &NetOurIP); - /* - "" - */ + NetCopyIP((void *)&ip->ip_src, &source); + /* already in network byte order */ NetCopyIP((void *)&ip->ip_dst, &dest); +} + +void +NetSetUDPHeader(uchar *pkt, IPaddr_t dest, int dport, int sport, int len) +{ + struct IP_UDP_t *ip = (struct IP_UDP_t *)pkt; + + /* + * If the data is an odd number of bytes, zero the + * byte after the last byte so that the checksum + * will work. + */ + if (len & 1) + pkt[IP_UDP_HDR_SIZE + len] = 0; + + NetSetIPHeader(pkt, dest, NetOurIP); + ip->ip_len = htons(IP_UDP_HDR_SIZE + len); + ip->ip_p = IPPROTO_UDP; + ip->ip_sum = ~NetCksum((uchar *)ip, IP_HDR_SIZE >> 1); + ip->udp_src = htons(sport); ip->udp_dst = htons(dport); ip->udp_len = htons(UDP_HDR_SIZE + len); ip->udp_xsum = 0; - ip->ip_sum = ~NetCksum((uchar *)ip, IP_HDR_SIZE / 2); }
void copy_filename(char *dst, const char *src, int size) diff --git a/net/ping.c b/net/ping.c index 34e287c..356f107 100644 --- a/net/ping.c +++ b/net/ping.c @@ -16,11 +16,31 @@ static ushort PingSeqNo; /* The ip address to ping */ IPaddr_t NetPingIP;
+static void SetICMPHeader(uchar *pkt, IPaddr_t dest) +{ + /* + * Construct an IP and ICMP header. + */ + struct IP_UDP_t *ip = (struct IP_UDP_t *)pkt; + struct ICMP_t *icmp = (struct ICMP_t *)&ip->udp_src; + + NetSetIPHeader(pkt, dest, NetOurIP); + + ip->ip_len = htons(IP_ICMP_HDR_SIZE); + ip->ip_p = IPPROTO_ICMP; + ip->ip_sum = ~NetCksum((uchar *)ip, IP_HDR_SIZE >> 1); + + icmp->type = ICMP_ECHO_REQUEST; + icmp->code = 0; + icmp->checksum = 0; + icmp->un.echo.id = 0; + icmp->un.echo.sequence = htons(PingSeqNo++); + icmp->checksum = ~NetCksum((uchar *)icmp, ICMP_HDR_SIZE >> 1); +} + static int PingSend(void) { static uchar mac[6]; - struct IP_UDP_t *ip; - ushort *s; uchar *pkt;
/* XXX always send arp request */ @@ -35,33 +55,7 @@ static int PingSend(void) pkt = NetArpWaitTxPacket; pkt += NetSetEther(pkt, mac, PROT_IP);
- ip = (struct IP_UDP_t *)pkt; - - /* - * Construct an IP and ICMP header. - * (need to set no fragment bit - XXX) - */ - /* IP_HDR_SIZE / 4 (not including UDP) */ - ip->ip_hl_v = 0x45; - ip->ip_tos = 0; - ip->ip_len = htons(IP_HDR_SIZE + 8); - ip->ip_id = htons(NetIPID++); - ip->ip_off = htons(IP_FLAGS_DFRAG); /* Don't fragment */ - ip->ip_ttl = 255; - ip->ip_p = 0x01; /* ICMP */ - ip->ip_sum = 0; - /* already in network byte order */ - NetCopyIP((void *)&ip->ip_src, &NetOurIP); - /* - "" - */ - NetCopyIP((void *)&ip->ip_dst, &NetPingIP); - ip->ip_sum = ~NetCksum((uchar *)ip, IP_HDR_SIZE / 2); - - s = &ip->udp_src; /* XXX ICMP starts here */ - s[0] = htons(0x0800); /* echo-request, code */ - s[1] = 0; /* checksum */ - s[2] = 0; /* identifier */ - s[3] = htons(PingSeqNo++); /* sequence number */ - s[1] = ~NetCksum((uchar *)s, 8/2); + SetICMPHeader(pkt, NetPingIP);
/* size of the waiting packet */ NetArpWaitTxPacketSize =

Hi Joe,
On Thu, Jan 19, 2012 at 4:53 PM, Joe Hershberger joe.hershberger@ni.com wrote:
ICMP (ping) was reimplementing IP header code... it now shares code.
Signed-off-by: Joe Hershberger joe.hershberger@ni.com Cc: Joe Hershberger joe.hershberger@gmail.com Cc: Wolfgang Denk wd@denx.de
include/net.h | 4 +++- net/bootp.c | 6 +++--- net/net.c | 47 ++++++++++++++++++++++++++++------------------- net/ping.c | 52 +++++++++++++++++++++++----------------------------- 4 files changed, 57 insertions(+), 52 deletions(-)
To be honest these sorts of cleanup are great, but they are very hard to review unless you know the code well. In particular your commit message makes me imagine that you are cutting out duplicate code, but it is changing a bit also.
diff --git a/include/net.h b/include/net.h index add2080..19e9463 100644 --- a/include/net.h +++ b/include/net.h @@ -421,7 +421,9 @@ extern int NetEthHdrSize(void); extern int NetSetEther(uchar *, uchar *, uint);
/* Set IP header */ -extern void NetSetIP(uchar *, IPaddr_t, int, int, int); +extern void NetSetIPHeader(uchar *pkt, IPaddr_t dest, IPaddr_t source); +extern void NetSetUDPHeader(uchar *pkt, IPaddr_t dest, int dport,
- int sport, int len);
/* Checksum */ extern int NetCksumOk(uchar *, int); /* Return true if cksum OK */ diff --git a/net/bootp.c b/net/bootp.c index 2be8083..0c2af48 100644 --- a/net/bootp.c +++ b/net/bootp.c @@ -619,7 +619,7 @@ BootpRequest(void) * determined. * C. Hallinan, DS4.COM, Inc. */
- /* NetSetIP(pkt, 0xFFFFFFFFL, PORT_BOOTPS, PORT_BOOTPC,
- /* NetSetUDPHeader(pkt, 0xFFFFFFFFL, PORT_BOOTPS, PORT_BOOTPC,
sizeof (struct Bootp_t)); */ iphdr = pkt; /* We need this later for NetSetUDPHeader() */ pkt += IP_UDP_HDR_SIZE; @@ -663,7 +663,7 @@ BootpRequest(void) pktlen = ((int)(pkt-NetTxPacket)) + BOOTP_HDR_SIZE - sizeof(bp->bp_vend) + ext_len; iplen = BOOTP_HDR_SIZE - sizeof(bp->bp_vend) + ext_len;
- NetSetIP(iphdr, 0xFFFFFFFFL, PORT_BOOTPS, PORT_BOOTPC, iplen);
- NetSetUDPHeader(iphdr, 0xFFFFFFFFL, PORT_BOOTPS, PORT_BOOTPC, iplen);
NetSetTimeout(SELECT_TIMEOUT, BootpTimeout);
#if defined(CONFIG_CMD_DHCP) @@ -844,7 +844,7 @@ static void DhcpSendRequestPkt(struct Bootp_t *bp_offer) pktlen = ((int)(pkt-NetTxPacket)) + BOOTP_HDR_SIZE - sizeof(bp->bp_vend) + extlen; iplen = BOOTP_HDR_SIZE - sizeof(bp->bp_vend) + extlen;
- NetSetIP(iphdr, 0xFFFFFFFFL, PORT_BOOTPS, PORT_BOOTPC, iplen);
- NetSetUDPHeader(iphdr, 0xFFFFFFFFL, PORT_BOOTPS, PORT_BOOTPC, iplen);
debug("Transmitting DHCPREQUEST packet: len = %d\n", pktlen); #ifdef CONFIG_BOOTP_DHCP_REQUEST_DELAY diff --git a/net/net.c b/net/net.c index 9bf74d5..a47b215 100644 --- a/net/net.c +++ b/net/net.c @@ -625,7 +625,7 @@ NetSendUDPPacket(uchar *ether, IPaddr_t dest, int dport, int sport, pkt = NetArpWaitTxPacket; pkt += NetSetEther(pkt, NetArpWaitPacketMAC, PROT_IP);
- NetSetIP(pkt, dest, dport, sport, payload_len);
- NetSetUDPHeader(pkt, dest, dport, sport, payload_len);
memcpy(pkt + IP_UDP_HDR_SIZE, (uchar *)NetTxPacket + (pkt - (uchar *)NetArpWaitTxPacket) + IP_UDP_HDR_SIZE, payload_len); @@ -645,7 +645,7 @@ NetSendUDPPacket(uchar *ether, IPaddr_t dest, int dport, int sport,
pkt = (uchar *)NetTxPacket; pkt += NetSetEther(pkt, ether, PROT_IP);
- NetSetIP(pkt, dest, dport, sport, payload_len);
- NetSetUDPHeader(pkt, dest, dport, sport, payload_len);
eth_send(NetTxPacket, (pkt - NetTxPacket) + IP_UDP_HDR_SIZE + payload_len);
@@ -1250,40 +1250,49 @@ NetSetEther(uchar *xet, uchar * addr, uint prot) }
void -NetSetIP(uchar *xip, IPaddr_t dest, int dport, int sport, int len) +NetSetIPHeader(uchar *pkt, IPaddr_t dest, IPaddr_t source) {
- struct IP_UDP_t *ip = (struct IP_UDP_t *)xip;
- struct IP_UDP_t *ip = (struct IP_UDP_t *)pkt;
/*
- * If the data is an odd number of bytes, zero the
- * byte after the last byte so that the checksum
- * will work.
- */
- if (len & 1)
- xip[IP_UDP_HDR_SIZE + len] = 0;
- /*
- * Construct an IP and UDP header.
- * (need to set no fragment bit - XXX)
- * Construct an IP header.
*/ /* IP_HDR_SIZE / 4 (not including UDP) */ ip->ip_hl_v = 0x45; ip->ip_tos = 0;
- ip->ip_len = htons(IP_UDP_HDR_SIZE + len);
- ip->ip_len = htons(IP_HDR_SIZE);
ip->ip_id = htons(NetIPID++); ip->ip_off = htons(IP_FLAGS_DFRAG); /* Don't fragment */ ip->ip_ttl = 255;
- ip->ip_p = 17; /* UDP */
ip->ip_sum = 0; /* already in network byte order */
- NetCopyIP((void *)&ip->ip_src, &NetOurIP);
- /* - "" - */
- NetCopyIP((void *)&ip->ip_src, &source);
- /* already in network byte order */
NetCopyIP((void *)&ip->ip_dst, &dest); +}
+void +NetSetUDPHeader(uchar *pkt, IPaddr_t dest, int dport, int sport, int len) +{
- struct IP_UDP_t *ip = (struct IP_UDP_t *)pkt;
- /*
- * If the data is an odd number of bytes, zero the
- * byte after the last byte so that the checksum
- * will work.
- */
- if (len & 1)
- pkt[IP_UDP_HDR_SIZE + len] = 0;
- NetSetIPHeader(pkt, dest, NetOurIP);
- ip->ip_len = htons(IP_UDP_HDR_SIZE + len);
- ip->ip_p = IPPROTO_UDP;
- ip->ip_sum = ~NetCksum((uchar *)ip, IP_HDR_SIZE >> 1);
ip->udp_src = htons(sport); ip->udp_dst = htons(dport); ip->udp_len = htons(UDP_HDR_SIZE + len); ip->udp_xsum = 0;
- ip->ip_sum = ~NetCksum((uchar *)ip, IP_HDR_SIZE / 2);
}
void copy_filename(char *dst, const char *src, int size) diff --git a/net/ping.c b/net/ping.c index 34e287c..356f107 100644 --- a/net/ping.c +++ b/net/ping.c @@ -16,11 +16,31 @@ static ushort PingSeqNo; /* The ip address to ping */ IPaddr_t NetPingIP;
+static void SetICMPHeader(uchar *pkt, IPaddr_t dest) +{
- /*
- * Construct an IP and ICMP header.
- */
- struct IP_UDP_t *ip = (struct IP_UDP_t *)pkt;
- struct ICMP_t *icmp = (struct ICMP_t *)&ip->udp_src;
- NetSetIPHeader(pkt, dest, NetOurIP);
- ip->ip_len = htons(IP_ICMP_HDR_SIZE);
- ip->ip_p = IPPROTO_ICMP;
- ip->ip_sum = ~NetCksum((uchar *)ip, IP_HDR_SIZE >> 1);
Is >> 1 better than / 2 ?
- icmp->type = ICMP_ECHO_REQUEST;
- icmp->code = 0;
- icmp->checksum = 0;
- icmp->un.echo.id = 0;
- icmp->un.echo.sequence = htons(PingSeqNo++);
- icmp->checksum = ~NetCksum((uchar *)icmp, ICMP_HDR_SIZE >> 1);
+}
static int PingSend(void) { static uchar mac[6];
- struct IP_UDP_t *ip;
- ushort *s;
uchar *pkt;
/* XXX always send arp request */ @@ -35,33 +55,7 @@ static int PingSend(void) pkt = NetArpWaitTxPacket; pkt += NetSetEther(pkt, mac, PROT_IP);
- ip = (struct IP_UDP_t *)pkt;
- /*
- * Construct an IP and ICMP header.
- * (need to set no fragment bit - XXX)
- */
- /* IP_HDR_SIZE / 4 (not including UDP) */
- ip->ip_hl_v = 0x45;
- ip->ip_tos = 0;
- ip->ip_len = htons(IP_HDR_SIZE + 8);
- ip->ip_id = htons(NetIPID++);
- ip->ip_off = htons(IP_FLAGS_DFRAG); /* Don't fragment */
- ip->ip_ttl = 255;
- ip->ip_p = 0x01; /* ICMP */
- ip->ip_sum = 0;
- /* already in network byte order */
- NetCopyIP((void *)&ip->ip_src, &NetOurIP);
- /* - "" - */
- NetCopyIP((void *)&ip->ip_dst, &NetPingIP);
- ip->ip_sum = ~NetCksum((uchar *)ip, IP_HDR_SIZE / 2);
- s = &ip->udp_src; /* XXX ICMP starts here */
- s[0] = htons(0x0800); /* echo-request, code */
- s[1] = 0; /* checksum */
- s[2] = 0; /* identifier */
- s[3] = htons(PingSeqNo++); /* sequence number */
- s[1] = ~NetCksum((uchar *)s, 8/2);
- SetICMPHeader(pkt, NetPingIP);
/* size of the waiting packet */ NetArpWaitTxPacketSize = -- 1.6.0.2
U-Boot mailing list U-Boot@lists.denx.de http://lists.denx.de/mailman/listinfo/u-boot
Regards, Simon

Acked-by: Mike Frysinger vapier@gentoo.org -mike

Signed-off-by: Joe Hershberger joe.hershberger@ni.com Cc: Joe Hershberger joe.hershberger@gmail.com Cc: Wolfgang Denk wd@denx.de --- net/arp.c | 13 ++++++++----- net/ping.c | 7 ++++--- net/rarp.c | 6 ++++-- 3 files changed, 16 insertions(+), 10 deletions(-)
diff --git a/net/arp.c b/net/arp.c index 456decd..60a1ded 100644 --- a/net/arp.c +++ b/net/arp.c @@ -52,12 +52,14 @@ void ArpRequest(void) { uchar *pkt; struct ARP_t *arp; + int eth_hdr_size;
debug("ARP broadcast %d\n", NetArpWaitTry);
pkt = NetTxPacket;
- pkt += NetSetEther(pkt, NetBcastAddr, PROT_ARP); + eth_hdr_size = NetSetEther(pkt, NetBcastAddr, PROT_ARP); + pkt += eth_hdr_size;
arp = (struct ARP_t *) pkt;
@@ -86,7 +88,7 @@ void ArpRequest(void) }
NetWriteIP(&arp->ar_tpa, NetArpWaitReplyIP); - (void) eth_send(NetTxPacket, (pkt - NetTxPacket) + ARP_HDR_SIZE); + (void) eth_send(NetTxPacket, eth_hdr_size + ARP_HDR_SIZE); }
void ArpTimeoutCheck(void) @@ -118,6 +120,7 @@ void ArpReceive(struct Ethernet_t *et, struct IP_UDP_t *ip, int len) struct ARP_t *arp; IPaddr_t reply_ip_addr; uchar *pkt; + int eth_hdr_size;
/* * We have to deal with two types of ARP packets: @@ -155,14 +158,14 @@ void ArpReceive(struct Ethernet_t *et, struct IP_UDP_t *ip, int len) /* reply with our IP address */ debug("Got ARP REQUEST, return our IP\n"); pkt = (uchar *)et; - pkt += NetSetEther(pkt, et->et_src, PROT_ARP); + eth_hdr_size = NetSetEther(pkt, et->et_src, PROT_ARP); + pkt += eth_hdr_size; arp->ar_op = htons(ARPOP_REPLY); memcpy(&arp->ar_tha, &arp->ar_sha, ARP_HLEN); NetCopyIP(&arp->ar_tpa, &arp->ar_spa); memcpy(&arp->ar_sha, NetOurEther, ARP_HLEN); NetCopyIP(&arp->ar_spa, &NetOurIP); - (void) eth_send((uchar *)et, - (pkt - (uchar *)et) + ARP_HDR_SIZE); + (void) eth_send((uchar *)et, eth_hdr_size + ARP_HDR_SIZE); return;
case ARPOP_REPLY: /* arp reply */ diff --git a/net/ping.c b/net/ping.c index 356f107..12f9185 100644 --- a/net/ping.c +++ b/net/ping.c @@ -42,6 +42,7 @@ static int PingSend(void) { static uchar mac[6]; uchar *pkt; + int eth_hdr_size;
/* XXX always send arp request */
@@ -53,13 +54,13 @@ static int PingSend(void) NetArpWaitPacketMAC = mac;
pkt = NetArpWaitTxPacket; - pkt += NetSetEther(pkt, mac, PROT_IP); + eth_hdr_size = NetSetEther(pkt, mac, PROT_IP); + pkt += eth_hdr_size;
SetICMPHeader(pkt, NetPingIP);
/* size of the waiting packet */ - NetArpWaitTxPacketSize = - (pkt - NetArpWaitTxPacket) + IP_HDR_SIZE + 8; + NetArpWaitTxPacketSize = eth_hdr_size + IP_ICMP_HDR_SIZE;
/* and do the ARP request */ NetArpWaitTry = 1; diff --git a/net/rarp.c b/net/rarp.c index 305b0a4..3097341 100644 --- a/net/rarp.c +++ b/net/rarp.c @@ -91,11 +91,13 @@ RarpRequest(void) { uchar *pkt; struct ARP_t *rarp; + int eth_hdr_size;
printf("RARP broadcast %d\n", ++RarpTry); pkt = NetTxPacket;
- pkt += NetSetEther(pkt, NetBcastAddr, PROT_RARP); + eth_hdr_size = NetSetEther(pkt, NetBcastAddr, PROT_RARP); + pkt += eth_hdr_size;
rarp = (struct ARP_t *)pkt;
@@ -111,7 +113,7 @@ RarpRequest(void) /* dest IP addr set to broadcast */ memset(&rarp->ar_data[16], 0xff, 4);
- NetSendPacket(NetTxPacket, (pkt - NetTxPacket) + ARP_HDR_SIZE); + NetSendPacket(NetTxPacket, eth_hdr_size + ARP_HDR_SIZE);
NetSetTimeout(TIMEOUT, RarpTimeout); }

On Thu, Jan 19, 2012 at 4:53 PM, Joe Hershberger joe.hershberger@ni.com wrote:
Signed-off-by: Joe Hershberger joe.hershberger@ni.com Cc: Joe Hershberger joe.hershberger@gmail.com Cc: Wolfgang Denk wd@denx.de
Acked-by: Simon Glass sjg@chromium.org
net/arp.c | 13 ++++++++----- net/ping.c | 7 ++++--- net/rarp.c | 6 ++++-- 3 files changed, 16 insertions(+), 10 deletions(-)
diff --git a/net/arp.c b/net/arp.c index 456decd..60a1ded 100644 --- a/net/arp.c +++ b/net/arp.c @@ -52,12 +52,14 @@ void ArpRequest(void) { uchar *pkt; struct ARP_t *arp;
- int eth_hdr_size;
debug("ARP broadcast %d\n", NetArpWaitTry);
pkt = NetTxPacket;
- pkt += NetSetEther(pkt, NetBcastAddr, PROT_ARP);
- eth_hdr_size = NetSetEther(pkt, NetBcastAddr, PROT_ARP);
- pkt += eth_hdr_size;
arp = (struct ARP_t *) pkt;
@@ -86,7 +88,7 @@ void ArpRequest(void) }
NetWriteIP(&arp->ar_tpa, NetArpWaitReplyIP);
- (void) eth_send(NetTxPacket, (pkt - NetTxPacket) + ARP_HDR_SIZE);
- (void) eth_send(NetTxPacket, eth_hdr_size + ARP_HDR_SIZE);
}
void ArpTimeoutCheck(void) @@ -118,6 +120,7 @@ void ArpReceive(struct Ethernet_t *et, struct IP_UDP_t *ip, int len) struct ARP_t *arp; IPaddr_t reply_ip_addr; uchar *pkt;
- int eth_hdr_size;
/* * We have to deal with two types of ARP packets: @@ -155,14 +158,14 @@ void ArpReceive(struct Ethernet_t *et, struct IP_UDP_t *ip, int len) /* reply with our IP address */ debug("Got ARP REQUEST, return our IP\n"); pkt = (uchar *)et;
- pkt += NetSetEther(pkt, et->et_src, PROT_ARP);
- eth_hdr_size = NetSetEther(pkt, et->et_src, PROT_ARP);
- pkt += eth_hdr_size;
arp->ar_op = htons(ARPOP_REPLY); memcpy(&arp->ar_tha, &arp->ar_sha, ARP_HLEN); NetCopyIP(&arp->ar_tpa, &arp->ar_spa); memcpy(&arp->ar_sha, NetOurEther, ARP_HLEN); NetCopyIP(&arp->ar_spa, &NetOurIP);
- (void) eth_send((uchar *)et,
- (pkt - (uchar *)et) + ARP_HDR_SIZE);
- (void) eth_send((uchar *)et, eth_hdr_size + ARP_HDR_SIZE);
return;
case ARPOP_REPLY: /* arp reply */ diff --git a/net/ping.c b/net/ping.c index 356f107..12f9185 100644 --- a/net/ping.c +++ b/net/ping.c @@ -42,6 +42,7 @@ static int PingSend(void) { static uchar mac[6]; uchar *pkt;
- int eth_hdr_size;
/* XXX always send arp request */
@@ -53,13 +54,13 @@ static int PingSend(void) NetArpWaitPacketMAC = mac;
pkt = NetArpWaitTxPacket;
- pkt += NetSetEther(pkt, mac, PROT_IP);
- eth_hdr_size = NetSetEther(pkt, mac, PROT_IP);
- pkt += eth_hdr_size;
SetICMPHeader(pkt, NetPingIP);
/* size of the waiting packet */
- NetArpWaitTxPacketSize =
- (pkt - NetArpWaitTxPacket) + IP_HDR_SIZE + 8;
- NetArpWaitTxPacketSize = eth_hdr_size + IP_ICMP_HDR_SIZE;
/* and do the ARP request */ NetArpWaitTry = 1; diff --git a/net/rarp.c b/net/rarp.c index 305b0a4..3097341 100644 --- a/net/rarp.c +++ b/net/rarp.c @@ -91,11 +91,13 @@ RarpRequest(void) { uchar *pkt; struct ARP_t *rarp;
- int eth_hdr_size;
printf("RARP broadcast %d\n", ++RarpTry); pkt = NetTxPacket;
- pkt += NetSetEther(pkt, NetBcastAddr, PROT_RARP);
- eth_hdr_size = NetSetEther(pkt, NetBcastAddr, PROT_RARP);
- pkt += eth_hdr_size;
rarp = (struct ARP_t *)pkt;
@@ -111,7 +113,7 @@ RarpRequest(void) /* dest IP addr set to broadcast */ memset(&rarp->ar_data[16], 0xff, 4);
- NetSendPacket(NetTxPacket, (pkt - NetTxPacket) + ARP_HDR_SIZE);
- NetSendPacket(NetTxPacket, eth_hdr_size + ARP_HDR_SIZE);
NetSetTimeout(TIMEOUT, RarpTimeout); } -- 1.6.0.2
U-Boot mailing list U-Boot@lists.denx.de http://lists.denx.de/mailman/listinfo/u-boot

Acked-by: Mike Frysinger vapier@gentoo.org -mike

Signed-off-by: Joe Hershberger joe.hershberger@ni.com Cc: Joe Hershberger joe.hershberger@gmail.com Cc: Wolfgang Denk wd@denx.de --- net/bootp.c | 26 ++++++++++++++------------ 1 files changed, 14 insertions(+), 12 deletions(-)
diff --git a/net/bootp.c b/net/bootp.c index 0c2af48..0d5f4cf 100644 --- a/net/bootp.c +++ b/net/bootp.c @@ -582,7 +582,8 @@ BootpRequest(void) { uchar *pkt, *iphdr; struct Bootp_t *bp; - int ext_len, pktlen, iplen; + int extlen, pktlen, iplen; + int eth_hdr_size; #ifdef CONFIG_BOOTP_RANDOM_DELAY ulong i, rand_ms; #endif @@ -610,7 +611,8 @@ BootpRequest(void) pkt = NetTxPacket; memset((void *)pkt, 0, PKTSIZE);
- pkt += NetSetEther(pkt, NetBcastAddr, PROT_IP); + eth_hdr_size = NetSetEther(pkt, NetBcastAddr, PROT_IP); + pkt += eth_hdr_size;
/* * Next line results in incorrect packet size being transmitted, @@ -639,9 +641,9 @@ BootpRequest(void)
/* Request additional information from the BOOTP/DHCP server */ #if defined(CONFIG_CMD_DHCP) - ext_len = DhcpExtended((u8 *)bp->bp_vend, DHCP_DISCOVER, 0, 0); + extlen = DhcpExtended((u8 *)bp->bp_vend, DHCP_DISCOVER, 0, 0); #else - ext_len = BootpExtended((u8 *)bp->bp_vend); + extlen = BootpExtended((u8 *)bp->bp_vend); #endif
/* @@ -660,9 +662,8 @@ BootpRequest(void) * Calculate proper packet lengths taking into account the * variable size of the options field */ - pktlen = ((int)(pkt-NetTxPacket)) + BOOTP_HDR_SIZE - - sizeof(bp->bp_vend) + ext_len; - iplen = BOOTP_HDR_SIZE - sizeof(bp->bp_vend) + ext_len; + iplen = BOOTP_HDR_SIZE - OPT_FIELD_SIZE + extlen; + pktlen = eth_hdr_size + IP_UDP_HDR_SIZE + iplen; NetSetUDPHeader(iphdr, 0xFFFFFFFFL, PORT_BOOTPS, PORT_BOOTPC, iplen); NetSetTimeout(SELECT_TIMEOUT, BootpTimeout);
@@ -798,13 +799,15 @@ static void DhcpSendRequestPkt(struct Bootp_t *bp_offer) uchar *pkt, *iphdr; struct Bootp_t *bp; int pktlen, iplen, extlen; + int eth_hdr_size; IPaddr_t OfferedIP;
debug("DhcpSendRequestPkt: Sending DHCPREQUEST\n"); pkt = NetTxPacket; memset((void *)pkt, 0, PKTSIZE);
- pkt += NetSetEther(pkt, NetBcastAddr, PROT_IP); + eth_hdr_size = NetSetEther(pkt, NetBcastAddr, PROT_IP); + pkt += eth_hdr_size;
iphdr = pkt; /* We'll need this later to set proper pkt size */ pkt += IP_UDP_HDR_SIZE; @@ -841,15 +844,14 @@ static void DhcpSendRequestPkt(struct Bootp_t *bp_offer) extlen = DhcpExtended((u8 *)bp->bp_vend, DHCP_REQUEST, NetDHCPServerIP, OfferedIP);
- pktlen = ((int)(pkt-NetTxPacket)) + BOOTP_HDR_SIZE - - sizeof(bp->bp_vend) + extlen; - iplen = BOOTP_HDR_SIZE - sizeof(bp->bp_vend) + extlen; + iplen = BOOTP_HDR_SIZE - OPT_FIELD_SIZE + extlen; + pktlen = eth_hdr_size + IP_UDP_HDR_SIZE + iplen; NetSetUDPHeader(iphdr, 0xFFFFFFFFL, PORT_BOOTPS, PORT_BOOTPC, iplen);
- debug("Transmitting DHCPREQUEST packet: len = %d\n", pktlen); #ifdef CONFIG_BOOTP_DHCP_REQUEST_DELAY udelay(CONFIG_BOOTP_DHCP_REQUEST_DELAY); #endif /* CONFIG_BOOTP_DHCP_REQUEST_DELAY */ + debug("Transmitting DHCPREQUEST packet: len = %d\n", pktlen); NetSendPacket(NetTxPacket, pktlen); }

Hi Joe,
On Thu, Jan 19, 2012 at 4:53 PM, Joe Hershberger joe.hershberger@ni.com wrote:
Signed-off-by: Joe Hershberger joe.hershberger@ni.com Cc: Joe Hershberger joe.hershberger@gmail.com Cc: Wolfgang Denk wd@denx.de
net/bootp.c | 26 ++++++++++++++------------ 1 files changed, 14 insertions(+), 12 deletions(-)
Refactor why or to what purpose? Perhaps a bit more detail in the commit message?
diff --git a/net/bootp.c b/net/bootp.c index 0c2af48..0d5f4cf 100644 --- a/net/bootp.c +++ b/net/bootp.c @@ -582,7 +582,8 @@ BootpRequest(void) { uchar *pkt, *iphdr; struct Bootp_t *bp;
- int ext_len, pktlen, iplen;
- int extlen, pktlen, iplen;
Is extlen better than ext_len?
- int eth_hdr_size;
#ifdef CONFIG_BOOTP_RANDOM_DELAY ulong i, rand_ms; #endif @@ -610,7 +611,8 @@ BootpRequest(void) pkt = NetTxPacket; memset((void *)pkt, 0, PKTSIZE);
- pkt += NetSetEther(pkt, NetBcastAddr, PROT_IP);
- eth_hdr_size = NetSetEther(pkt, NetBcastAddr, PROT_IP);
- pkt += eth_hdr_size;
/* * Next line results in incorrect packet size being transmitted, @@ -639,9 +641,9 @@ BootpRequest(void)
/* Request additional information from the BOOTP/DHCP server */ #if defined(CONFIG_CMD_DHCP)
- ext_len = DhcpExtended((u8 *)bp->bp_vend, DHCP_DISCOVER, 0, 0);
- extlen = DhcpExtended((u8 *)bp->bp_vend, DHCP_DISCOVER, 0, 0);
#else
- ext_len = BootpExtended((u8 *)bp->bp_vend);
- extlen = BootpExtended((u8 *)bp->bp_vend);
#endif
/* @@ -660,9 +662,8 @@ BootpRequest(void) * Calculate proper packet lengths taking into account the * variable size of the options field */
- pktlen = ((int)(pkt-NetTxPacket)) + BOOTP_HDR_SIZE -
- sizeof(bp->bp_vend) + ext_len;
- iplen = BOOTP_HDR_SIZE - sizeof(bp->bp_vend) + ext_len;
- iplen = BOOTP_HDR_SIZE - OPT_FIELD_SIZE + extlen;
- pktlen = eth_hdr_size + IP_UDP_HDR_SIZE + iplen;
NetSetUDPHeader(iphdr, 0xFFFFFFFFL, PORT_BOOTPS, PORT_BOOTPC, iplen); NetSetTimeout(SELECT_TIMEOUT, BootpTimeout);
@@ -798,13 +799,15 @@ static void DhcpSendRequestPkt(struct Bootp_t *bp_offer) uchar *pkt, *iphdr; struct Bootp_t *bp; int pktlen, iplen, extlen;
- int eth_hdr_size;
IPaddr_t OfferedIP;
debug("DhcpSendRequestPkt: Sending DHCPREQUEST\n"); pkt = NetTxPacket; memset((void *)pkt, 0, PKTSIZE);
- pkt += NetSetEther(pkt, NetBcastAddr, PROT_IP);
- eth_hdr_size = NetSetEther(pkt, NetBcastAddr, PROT_IP);
- pkt += eth_hdr_size;
iphdr = pkt; /* We'll need this later to set proper pkt size */ pkt += IP_UDP_HDR_SIZE; @@ -841,15 +844,14 @@ static void DhcpSendRequestPkt(struct Bootp_t *bp_offer) extlen = DhcpExtended((u8 *)bp->bp_vend, DHCP_REQUEST, NetDHCPServerIP, OfferedIP);
- pktlen = ((int)(pkt-NetTxPacket)) + BOOTP_HDR_SIZE -
- sizeof(bp->bp_vend) + extlen;
- iplen = BOOTP_HDR_SIZE - sizeof(bp->bp_vend) + extlen;
- iplen = BOOTP_HDR_SIZE - OPT_FIELD_SIZE + extlen;
- pktlen = eth_hdr_size + IP_UDP_HDR_SIZE + iplen;
NetSetUDPHeader(iphdr, 0xFFFFFFFFL, PORT_BOOTPS, PORT_BOOTPC, iplen);
- debug("Transmitting DHCPREQUEST packet: len = %d\n", pktlen);
#ifdef CONFIG_BOOTP_DHCP_REQUEST_DELAY udelay(CONFIG_BOOTP_DHCP_REQUEST_DELAY); #endif /* CONFIG_BOOTP_DHCP_REQUEST_DELAY */
- debug("Transmitting DHCPREQUEST packet: len = %d\n", pktlen);
Did you move this on purpose?
NetSendPacket(NetTxPacket, pktlen); }
-- 1.6.0.2
U-Boot mailing list U-Boot@lists.denx.de http://lists.denx.de/mailman/listinfo/u-boot
Regards, Simon

Hi Simon,
On Tue, Jan 24, 2012 at 1:05 AM, Simon Glass sjg@chromium.org wrote:
Hi Joe,
On Thu, Jan 19, 2012 at 4:53 PM, Joe Hershberger joe.hershberger@ni.com wrote:
Signed-off-by: Joe Hershberger joe.hershberger@ni.com Cc: Joe Hershberger joe.hershberger@gmail.com Cc: Wolfgang Denk wd@denx.de
net/bootp.c | 26 ++++++++++++++------------ 1 files changed, 14 insertions(+), 12 deletions(-)
Refactor why or to what purpose? Perhaps a bit more detail in the commit message?
Just removing the pointer subtraction similar to patch 12.
diff --git a/net/bootp.c b/net/bootp.c index 0c2af48..0d5f4cf 100644 --- a/net/bootp.c +++ b/net/bootp.c @@ -582,7 +582,8 @@ BootpRequest(void) { uchar *pkt, *iphdr; struct Bootp_t *bp;
- int ext_len, pktlen, iplen;
- int extlen, pktlen, iplen;
Is extlen better than ext_len?
Just more consistent with other variables in the same calculations. Not especially important, I guess.
- int eth_hdr_size;
#ifdef CONFIG_BOOTP_RANDOM_DELAY ulong i, rand_ms; #endif @@ -610,7 +611,8 @@ BootpRequest(void) pkt = NetTxPacket; memset((void *)pkt, 0, PKTSIZE);
- pkt += NetSetEther(pkt, NetBcastAddr, PROT_IP);
- eth_hdr_size = NetSetEther(pkt, NetBcastAddr, PROT_IP);
- pkt += eth_hdr_size;
/* * Next line results in incorrect packet size being transmitted, @@ -639,9 +641,9 @@ BootpRequest(void)
/* Request additional information from the BOOTP/DHCP server */ #if defined(CONFIG_CMD_DHCP)
- ext_len = DhcpExtended((u8 *)bp->bp_vend, DHCP_DISCOVER, 0, 0);
- extlen = DhcpExtended((u8 *)bp->bp_vend, DHCP_DISCOVER, 0, 0);
#else
- ext_len = BootpExtended((u8 *)bp->bp_vend);
- extlen = BootpExtended((u8 *)bp->bp_vend);
#endif
/* @@ -660,9 +662,8 @@ BootpRequest(void) * Calculate proper packet lengths taking into account the * variable size of the options field */
- pktlen = ((int)(pkt-NetTxPacket)) + BOOTP_HDR_SIZE -
- sizeof(bp->bp_vend) + ext_len;
- iplen = BOOTP_HDR_SIZE - sizeof(bp->bp_vend) + ext_len;
- iplen = BOOTP_HDR_SIZE - OPT_FIELD_SIZE + extlen;
- pktlen = eth_hdr_size + IP_UDP_HDR_SIZE + iplen;
NetSetUDPHeader(iphdr, 0xFFFFFFFFL, PORT_BOOTPS, PORT_BOOTPC, iplen); NetSetTimeout(SELECT_TIMEOUT, BootpTimeout);
@@ -798,13 +799,15 @@ static void DhcpSendRequestPkt(struct Bootp_t *bp_offer) uchar *pkt, *iphdr; struct Bootp_t *bp; int pktlen, iplen, extlen;
- int eth_hdr_size;
IPaddr_t OfferedIP;
debug("DhcpSendRequestPkt: Sending DHCPREQUEST\n"); pkt = NetTxPacket; memset((void *)pkt, 0, PKTSIZE);
- pkt += NetSetEther(pkt, NetBcastAddr, PROT_IP);
- eth_hdr_size = NetSetEther(pkt, NetBcastAddr, PROT_IP);
- pkt += eth_hdr_size;
iphdr = pkt; /* We'll need this later to set proper pkt size */ pkt += IP_UDP_HDR_SIZE; @@ -841,15 +844,14 @@ static void DhcpSendRequestPkt(struct Bootp_t *bp_offer) extlen = DhcpExtended((u8 *)bp->bp_vend, DHCP_REQUEST, NetDHCPServerIP, OfferedIP);
- pktlen = ((int)(pkt-NetTxPacket)) + BOOTP_HDR_SIZE -
- sizeof(bp->bp_vend) + extlen;
- iplen = BOOTP_HDR_SIZE - sizeof(bp->bp_vend) + extlen;
- iplen = BOOTP_HDR_SIZE - OPT_FIELD_SIZE + extlen;
- pktlen = eth_hdr_size + IP_UDP_HDR_SIZE + iplen;
NetSetUDPHeader(iphdr, 0xFFFFFFFFL, PORT_BOOTPS, PORT_BOOTPC, iplen);
- debug("Transmitting DHCPREQUEST packet: len = %d\n", pktlen);
#ifdef CONFIG_BOOTP_DHCP_REQUEST_DELAY udelay(CONFIG_BOOTP_DHCP_REQUEST_DELAY); #endif /* CONFIG_BOOTP_DHCP_REQUEST_DELAY */
- debug("Transmitting DHCPREQUEST packet: len = %d\n", pktlen);
Did you move this on purpose?
Yes... I figured a transmit debug message should be close to the transmit... not before a delay.
NetSendPacket(NetTxPacket, pktlen); }
Best regards, -Joe

On Tuesday 24 January 2012 02:15:41 Joe Hershberger wrote:
On Tue, Jan 24, 2012 at 1:05 AM, Simon Glass wrote:
On Thu, Jan 19, 2012 at 4:53 PM, Joe Hershberger wrote:
debug("Transmitting DHCPREQUEST packet: len = %d\n", pktlen);
#ifdef CONFIG_BOOTP_DHCP_REQUEST_DELAY udelay(CONFIG_BOOTP_DHCP_REQUEST_DELAY); #endif /* CONFIG_BOOTP_DHCP_REQUEST_DELAY */
debug("Transmitting DHCPREQUEST packet: len = %d\n", pktlen);
Did you move this on purpose?
Yes... I figured a transmit debug message should be close to the transmit... not before a delay.
generally we frown on these sort of side-commits ... -mike

There is no need to call through the handler when we can handle it inline
Signed-off-by: Joe Hershberger joe.hershberger@ni.com Cc: Joe Hershberger joe.hershberger@gmail.com Cc: Wolfgang Denk wd@denx.de --- net/ping.c | 19 ++----------------- 1 files changed, 2 insertions(+), 17 deletions(-)
diff --git a/net/ping.c b/net/ping.c index 12f9185..e4a7a21 100644 --- a/net/ping.c +++ b/net/ping.c @@ -76,22 +76,11 @@ PingTimeout(void) NetState = NETLOOP_FAIL; /* we did not get the reply */ }
-static void -PingHandler(uchar *pkt, unsigned dest, IPaddr_t sip, unsigned src, - unsigned len) -{ - if (sip != NetPingIP) - return; - - NetState = NETLOOP_SUCCESS; -} - void PingStart(void) { printf("Using %s device\n", eth_get_name()); NetSetTimeout(10000UL, PingTimeout); - NetSetHandler(PingHandler);
PingSend(); } @@ -103,13 +92,9 @@ void PingReceive(struct Ethernet_t *et, struct IP_UDP_t *ip, int len)
switch (icmph->type) { case ICMP_ECHO_REPLY: - /* - * IP header OK. Pass the packet to the - * current handler. - */ - /* XXX point to ip packet */ src_ip = NetReadIP((void *)&ip->ip_src); - NetGetHandler()((uchar *)ip, 0, src_ip, 0, 0); + if (src_ip == NetPingIP) + NetState = NETLOOP_SUCCESS; return; case ICMP_ECHO_REQUEST: debug("Got ICMP ECHO REQUEST, return "

Acked-by: Mike Frysinger vapier@gentoo.org -mike

Use this entry-point consistently across the net/ code
Signed-off-by: Joe Hershberger joe.hershberger@ni.com Cc: Joe Hershberger joe.hershberger@gmail.com Cc: Wolfgang Denk wd@denx.de --- net/arp.c | 6 +++--- net/cdp.c | 2 +- net/net.c | 2 +- net/ping.c | 3 +-- 4 files changed, 6 insertions(+), 7 deletions(-)
diff --git a/net/arp.c b/net/arp.c index 60a1ded..d9113dc 100644 --- a/net/arp.c +++ b/net/arp.c @@ -88,7 +88,7 @@ void ArpRequest(void) }
NetWriteIP(&arp->ar_tpa, NetArpWaitReplyIP); - (void) eth_send(NetTxPacket, eth_hdr_size + ARP_HDR_SIZE); + NetSendPacket(NetTxPacket, eth_hdr_size + ARP_HDR_SIZE); }
void ArpTimeoutCheck(void) @@ -165,7 +165,7 @@ void ArpReceive(struct Ethernet_t *et, struct IP_UDP_t *ip, int len) NetCopyIP(&arp->ar_tpa, &arp->ar_spa); memcpy(&arp->ar_sha, NetOurEther, ARP_HLEN); NetCopyIP(&arp->ar_spa, &NetOurIP); - (void) eth_send((uchar *)et, eth_hdr_size + ARP_HDR_SIZE); + NetSendPacket((uchar *)et, eth_hdr_size + ARP_HDR_SIZE); return;
case ARPOP_REPLY: /* arp reply */ @@ -198,7 +198,7 @@ void ArpReceive(struct Ethernet_t *et, struct IP_UDP_t *ip, int len) /* modify header, and transmit it */ memcpy(((struct Ethernet_t *)NetArpWaitTxPacket)-> et_dest, NetArpWaitPacketMAC, ARP_HLEN); - (void) eth_send(NetArpWaitTxPacket, + NetSendPacket(NetArpWaitTxPacket, NetArpWaitTxPacketSize);
/* no arp request pending now */ diff --git a/net/cdp.c b/net/cdp.c index 38b79bd..305ecad 100644 --- a/net/cdp.c +++ b/net/cdp.c @@ -215,7 +215,7 @@ CDPSendTrigger(void) chksum = 0xFFFF; *cp = htons(chksum);
- (void) eth_send(NetTxPacket, (uchar *)s - NetTxPacket); + NetSendPacket(NetTxPacket, (uchar *)s - NetTxPacket); return 0; }
diff --git a/net/net.c b/net/net.c index a47b215..16cbadc 100644 --- a/net/net.c +++ b/net/net.c @@ -646,7 +646,7 @@ NetSendUDPPacket(uchar *ether, IPaddr_t dest, int dport, int sport, pkt = (uchar *)NetTxPacket; pkt += NetSetEther(pkt, ether, PROT_IP); NetSetUDPHeader(pkt, dest, dport, sport, payload_len); - eth_send(NetTxPacket, (pkt - NetTxPacket) + IP_UDP_HDR_SIZE + + NetSendPacket(NetTxPacket, (pkt - NetTxPacket) + IP_UDP_HDR_SIZE + payload_len);
return 0; /* transmitted */ diff --git a/net/ping.c b/net/ping.c index e4a7a21..7d03072 100644 --- a/net/ping.c +++ b/net/ping.c @@ -114,8 +114,7 @@ void PingReceive(struct Ethernet_t *et, struct IP_UDP_t *ip, int len) icmph->checksum = 0; icmph->checksum = ~NetCksum((uchar *)icmph, (len - IP_HDR_SIZE) >> 1); - (void) eth_send((uchar *)et, - ETHER_HDR_SIZE + len); + NetSendPacket((uchar *)et, ETHER_HDR_SIZE + len); return; /* default: return;*/

On Thursday 19 January 2012 19:53:18 Joe Hershberger wrote:
Use this entry-point consistently across the net/ code
i like this, but i'd like it even more if NetSendPacket() was a static inline in the header. all this func does is jump straight to eth_send():
01fa2054 <_NetSendPacket>: 1fa2054: ff e2 f6 fd JUMP.L 0x1fa1c40 <_eth_send>; -mike

Changes to NetState now go through an accessor function called NetSetState()
Signed-off-by: Joe Hershberger joe.hershberger@ni.com Cc: Joe Hershberger joe.hershberger@gmail.com Cc: Wolfgang Denk wd@denx.de --- drivers/net/netconsole.c | 6 +++--- include/net.h | 13 +++++++------ net/cdp.c | 2 +- net/dns.c | 8 ++++---- net/net.c | 21 ++++++++++++++------- net/nfs.c | 6 +++--- net/ping.c | 4 ++-- net/sntp.c | 4 ++-- net/tftp.c | 8 ++++---- 9 files changed, 40 insertions(+), 32 deletions(-)
diff --git a/drivers/net/netconsole.c b/drivers/net/netconsole.c index ba02fd7..89b5956 100644 --- a/drivers/net/netconsole.c +++ b/drivers/net/netconsole.c @@ -44,19 +44,19 @@ static void nc_wait_arp_handler(uchar *pkt, unsigned dest, IPaddr_t sip, unsigned src, unsigned len) { - NetState = NETLOOP_SUCCESS; /* got arp reply - quit net loop */ + NetSetState(NETLOOP_SUCCESS); /* got arp reply - quit net loop */ }
static void nc_handler(uchar *pkt, unsigned dest, IPaddr_t sip, unsigned src, unsigned len) { if (input_size) - NetState = NETLOOP_SUCCESS; /* got input - quit net loop */ + NetSetState(NETLOOP_SUCCESS); /* got input - quit net loop */ }
static void nc_timeout(void) { - NetState = NETLOOP_SUCCESS; + NetSetState(NETLOOP_SUCCESS); }
void NcStart(void) diff --git a/include/net.h b/include/net.h index 19e9463..fe49ff7 100644 --- a/include/net.h +++ b/include/net.h @@ -364,12 +364,6 @@ extern uchar NetEtherNullAddr[6]; extern ushort NetOurVLAN; /* Our VLAN */ extern ushort NetOurNativeVLAN; /* Our Native VLAN */
-extern int NetState; /* Network loop state */ -#define NETLOOP_CONTINUE 1 -#define NETLOOP_RESTART 2 -#define NETLOOP_SUCCESS 3 -#define NETLOOP_FAIL 4 - extern int NetRestartWrap; /* Tried all network devices */
enum proto_t { @@ -435,6 +429,13 @@ extern void NetSetHandler(rxhand_f *); /* Set RX packet handler */ extern void net_set_icmp_handler(rxhand_icmp_f *f); /* Set ICMP RX handler */ extern void NetSetTimeout(ulong, thand_f *);/* Set timeout handler */
+/* Network loop state */ +extern void NetSetState(int state); +#define NETLOOP_CONTINUE 1 +#define NETLOOP_RESTART 2 +#define NETLOOP_SUCCESS 3 +#define NETLOOP_FAIL 4 + /* Transmit "NetTxPacket" */ extern void NetSendPacket(uchar *, int);
diff --git a/net/cdp.c b/net/cdp.c index 305ecad..31f9ce7 100644 --- a/net/cdp.c +++ b/net/cdp.c @@ -234,7 +234,7 @@ CDPTimeout(void) if (!CDPOK) NetStartAgain(); else - NetState = NETLOOP_SUCCESS; + NetSetState(NETLOOP_SUCCESS); }
static void diff --git a/net/dns.c b/net/dns.c index cc7ed51..0b655cc 100644 --- a/net/dns.c +++ b/net/dns.c @@ -98,7 +98,7 @@ static void DnsTimeout(void) { puts("Timeout\n"); - NetState = NETLOOP_FAIL; + NetSetState(NETLOOP_FAIL); }
static void @@ -128,7 +128,7 @@ DnsHandler(uchar *pkt, unsigned dest, IPaddr_t sip, unsigned src, unsigned len) /* Received 0 answers */ if (header->nanswers == 0) { puts("DNS: host not found\n"); - NetState = NETLOOP_SUCCESS; + NetSetState(NETLOOP_SUCCESS); return; }
@@ -141,7 +141,7 @@ DnsHandler(uchar *pkt, unsigned dest, IPaddr_t sip, unsigned src, unsigned len) /* We sent query class 1, query type 1 */ if (&p[5] > e || get_unaligned_be16(p+1) != DNS_A_RECORD) { puts("DNS: response was not an A record\n"); - NetState = NETLOOP_SUCCESS; + NetSetState(NETLOOP_SUCCESS); return; }
@@ -191,7 +191,7 @@ DnsHandler(uchar *pkt, unsigned dest, IPaddr_t sip, unsigned src, unsigned len) puts("server responded with invalid IP number\n"); }
- NetState = NETLOOP_SUCCESS; + NetSetState(NETLOOP_SUCCESS); }
void diff --git a/net/net.c b/net/net.c index 16cbadc..a92df90 100644 --- a/net/net.c +++ b/net/net.c @@ -154,7 +154,7 @@ uchar NetEtherNullAddr[6]; void (*push_packet)(volatile void *, int len) = 0; #endif /* Network loop state */ -int NetState; +static int NetState; /* Tried all network devices */ int NetRestartWrap; /* Network loop restarted */ @@ -217,7 +217,7 @@ void net_auto_load(void) * Just use BOOTP/RARP to configure system; * Do not use TFTP to load the bootfile. */ - NetState = NETLOOP_SUCCESS; + NetSetState(NETLOOP_SUCCESS); return; } #if defined(CONFIG_CMD_NFS) @@ -296,7 +296,7 @@ int NetLoop(enum proto_t protocol) restart: memcpy(NetOurEther, eth_get_dev()->enetaddr, 6);
- NetState = NETLOOP_CONTINUE; + NetSetState(NETLOOP_CONTINUE);
/* * Start the ball rolling with the given start function. From @@ -498,7 +498,7 @@ done: static void startAgainTimeout(void) { - NetState = NETLOOP_RESTART; + NetSetState(NETLOOP_RESTART); }
static void @@ -529,7 +529,7 @@ void NetStartAgain(void)
if ((!retry_forever) && (NetTryCount >= retrycnt)) { eth_halt(); - NetState = NETLOOP_FAIL; + NetSetState(NETLOOP_FAIL); return; }
@@ -546,10 +546,10 @@ void NetStartAgain(void) NetSetTimeout(10000UL, startAgainTimeout); NetSetHandler(startAgainHandler); } else { - NetState = NETLOOP_FAIL; + NetSetState(NETLOOP_FAIL); } } else { - NetState = NETLOOP_RESTART; + NetSetState(NETLOOP_RESTART); } }
@@ -592,6 +592,13 @@ NetSetTimeout(ulong iv, thand_f *f)
void +NetSetState(int state) +{ + NetState = state; +} + + +void NetSendPacket(uchar *pkt, int len) { (void) eth_send(pkt, len); diff --git a/net/nfs.c b/net/nfs.c index b6188fe..e366505 100644 --- a/net/nfs.c +++ b/net/nfs.c @@ -613,10 +613,10 @@ NfsHandler(uchar *pkt, unsigned dest, IPaddr_t sip, unsigned src, unsigned len) case STATE_UMOUNT_REQ: if (nfs_umountall_reply(pkt, len)) { puts("*** ERROR: Cannot umount\n"); - NetState = NETLOOP_FAIL; + NetSetState(NETLOOP_FAIL); } else { puts("\ndone\n"); - NetState = NfsDownloadState; + NetSetState(NfsDownloadState); } break;
@@ -679,7 +679,7 @@ NfsStart(void) nfs_path = (char *)nfs_path_buff;
if (nfs_path == NULL) { - NetState = NETLOOP_FAIL; + NetSetState(NETLOOP_FAIL); puts("*** ERROR: Fail allocate memory\n"); return; } diff --git a/net/ping.c b/net/ping.c index 7d03072..795d5c9 100644 --- a/net/ping.c +++ b/net/ping.c @@ -73,7 +73,7 @@ static void PingTimeout(void) { eth_halt(); - NetState = NETLOOP_FAIL; /* we did not get the reply */ + NetSetState(NETLOOP_FAIL); /* we did not get the reply */ }
void @@ -94,7 +94,7 @@ void PingReceive(struct Ethernet_t *et, struct IP_UDP_t *ip, int len) case ICMP_ECHO_REPLY: src_ip = NetReadIP((void *)&ip->ip_src); if (src_ip == NetPingIP) - NetState = NETLOOP_SUCCESS; + NetSetState(NETLOOP_SUCCESS); return; case ICMP_ECHO_REQUEST: debug("Got ICMP ECHO REQUEST, return " diff --git a/net/sntp.c b/net/sntp.c index 7997f98..f5fa6f7 100644 --- a/net/sntp.c +++ b/net/sntp.c @@ -45,7 +45,7 @@ static void SntpTimeout(void) { puts("Timeout\n"); - NetState = NETLOOP_FAIL; + NetSetState(NETLOOP_FAIL); return; }
@@ -76,7 +76,7 @@ SntpHandler(uchar *pkt, unsigned dest, IPaddr_t sip, unsigned src, tm.tm_year, tm.tm_mon, tm.tm_mday, tm.tm_hour, tm.tm_min, tm.tm_sec);
- NetState = NETLOOP_SUCCESS; + NetSetState(NETLOOP_SUCCESS); }
void diff --git a/net/tftp.c b/net/tftp.c index a04a832..49ddbd7 100644 --- a/net/tftp.c +++ b/net/tftp.c @@ -177,7 +177,7 @@ store_block(unsigned block, uchar *src, unsigned len) rc = flash_write((char *)src, (ulong)(load_addr+offset), len); if (rc) { flash_perror(rc); - NetState = NETLOOP_FAIL; + NetSetState(NETLOOP_FAIL); return; } } else @@ -300,7 +300,7 @@ static void tftp_complete(void) } #endif puts("\ndone\n"); - NetState = NETLOOP_SUCCESS; + NetSetState(NETLOOP_SUCCESS); }
static void @@ -627,7 +627,7 @@ TftpHandler(uchar *pkt, unsigned dest, IPaddr_t sip, unsigned src, if (MasterClient && (TftpBlock >= TftpEndingBlock)) { puts("\nMulticast tftp done\n"); mcast_cleanup(); - NetState = NETLOOP_SUCCESS; + NetSetState(NETLOOP_SUCCESS); } } else #endif @@ -644,7 +644,7 @@ TftpHandler(uchar *pkt, unsigned dest, IPaddr_t sip, unsigned src, case TFTP_ERR_ACCESS_DENIED: puts("Not retrying...\n"); eth_halt(); - NetState = NETLOOP_FAIL; + NetSetState(NETLOOP_FAIL); break; case TFTP_ERR_UNDEFINED: case TFTP_ERR_DISK_FULL:

On Thursday 19 January 2012 19:53:19 Joe Hershberger wrote:
Changes to NetState now go through an accessor function called NetSetState()
i'm not sure i see the value here. all i see is overhead being added. if you really want this to be an improvement, there's two minor tweaks to make.
--- a/include/net.h +++ b/include/net.h
+/* Network loop state */ +extern void NetSetState(int state);
make this into a static inline
+#define NETLOOP_CONTINUE 1 +#define NETLOOP_RESTART 2 +#define NETLOOP_SUCCESS 3 +#define NETLOOP_FAIL 4
turn these into an enum -mike

Only call the handlers if one was registered... don't require protocols to register a handler (eliminating dummies) NetConsole now uses the ARP handler when waiting on arp (instead of needing a #define hack in arp.c) Clear handlers at the end of net loop
Signed-off-by: Joe Hershberger joe.hershberger@ni.com Cc: Joe Hershberger joe.hershberger@gmail.com Cc: Wolfgang Denk wd@denx.de --- drivers/net/netconsole.c | 4 +- include/net.h | 9 ++++-- net/arp.c | 7 ++-- net/bootp.c | 4 +- net/cdp.c | 8 ----- net/dns.c | 2 +- net/net.c | 70 +++++++++++++++++++++++++++++----------------- net/nfs.c | 2 +- net/sntp.c | 2 +- net/tftp.c | 4 +- 10 files changed, 63 insertions(+), 49 deletions(-)
diff --git a/drivers/net/netconsole.c b/drivers/net/netconsole.c index 89b5956..1e31159 100644 --- a/drivers/net/netconsole.c +++ b/drivers/net/netconsole.c @@ -63,12 +63,12 @@ void NcStart(void) { if (!output_packet_len || memcmp(nc_ether, NetEtherNullAddr, 6)) { /* going to check for input packet */ - NetSetHandler(nc_handler); + NetSetUDPHandler(nc_handler); NetSetTimeout(net_timeout, nc_timeout); } else { /* send arp request */ uchar *pkt; - NetSetHandler(nc_wait_arp_handler); + NetSetARPHandler(nc_wait_arp_handler); pkt = (uchar *)NetTxPacket + NetEthHdrSize() + IP_UDP_HDR_SIZE; memcpy(pkt, output_packet, output_packet_len); NetSendUDPPacket(nc_ether, nc_ip, nc_port, nc_port, diff --git a/include/net.h b/include/net.h index fe49ff7..dca8e1c 100644 --- a/include/net.h +++ b/include/net.h @@ -424,8 +424,10 @@ extern int NetCksumOk(uchar *, int); /* Return true if cksum OK */ extern uint NetCksum(uchar *, int); /* Calculate the checksum */
/* Callbacks */ -extern rxhand_f *NetGetHandler(void); /* Get RX packet handler */ -extern void NetSetHandler(rxhand_f *); /* Set RX packet handler */ +extern rxhand_f *NetGetUDPHandler(void); /* Get UDP RX packet handler */ +extern void NetSetUDPHandler(rxhand_f *); /* Set UDP RX packet handler */ +extern rxhand_f *NetGetARPHandler(void); /* Get ARP RX packet handler */ +extern void NetSetARPHandler(rxhand_f *); /* Set ARP RX packet handler */ extern void net_set_icmp_handler(rxhand_icmp_f *f); /* Set ICMP RX handler */ extern void NetSetTimeout(ulong, thand_f *);/* Set timeout handler */
@@ -439,7 +441,8 @@ extern void NetSetState(int state); /* Transmit "NetTxPacket" */ extern void NetSendPacket(uchar *, int);
-/* Transmit UDP packet, performing ARP request if needed */ +/* Transmit UDP packet, performing ARP request if needed + (ether will be populated) */ extern int NetSendUDPPacket(uchar *ether, IPaddr_t dest, int dport, int sport, int payload_len);
diff --git a/net/arp.c b/net/arp.c index d9113dc..5b6c787 100644 --- a/net/arp.c +++ b/net/arp.c @@ -192,9 +192,10 @@ void ArpReceive(struct Ethernet_t *et, struct IP_UDP_t *ip, int len) memcpy(NetArpWaitPacketMAC, &arp->ar_sha, ARP_HLEN);
-#ifdef CONFIG_NETCONSOLE - NetGetHandler()(0, 0, 0, 0, 0); -#endif + if (NetGetARPHandler() != NULL) + NetGetARPHandler()((uchar *)arp, 0, + reply_ip_addr, 0, len); + /* modify header, and transmit it */ memcpy(((struct Ethernet_t *)NetArpWaitTxPacket)-> et_dest, NetArpWaitPacketMAC, ARP_HLEN); diff --git a/net/bootp.c b/net/bootp.c index 0d5f4cf..9d709f1 100644 --- a/net/bootp.c +++ b/net/bootp.c @@ -669,9 +669,9 @@ BootpRequest(void)
#if defined(CONFIG_CMD_DHCP) dhcp_state = SELECTING; - NetSetHandler(DhcpHandler); + NetSetUDPHandler(DhcpHandler); #else - NetSetHandler(BootpHandler); + NetSetUDPHandler(BootpHandler); #endif NetSendPacket(NetTxPacket, pktlen); } diff --git a/net/cdp.c b/net/cdp.c index 31f9ce7..120d410 100644 --- a/net/cdp.c +++ b/net/cdp.c @@ -237,13 +237,6 @@ CDPTimeout(void) NetSetState(NETLOOP_SUCCESS); }
-static void -CDPDummyHandler(uchar *pkt, unsigned dest, IPaddr_t sip, unsigned src, - unsigned len) -{ - /* nothing */ -} - void CDPReceive(const uchar *pkt, unsigned len) { @@ -368,7 +361,6 @@ CDPStart(void) CDPApplianceVLAN = htons(-1);
NetSetTimeout(CDP_TIMEOUT, CDPTimeout); - NetSetHandler(CDPDummyHandler);
CDPSendTrigger(); } diff --git a/net/dns.c b/net/dns.c index 0b655cc..9da3cac 100644 --- a/net/dns.c +++ b/net/dns.c @@ -200,7 +200,7 @@ DnsStart(void) debug("%s\n", __func__);
NetSetTimeout(DNS_TIMEOUT, DnsTimeout); - NetSetHandler(DnsHandler); + NetSetUDPHandler(DnsHandler);
DnsSend(); } diff --git a/net/net.c b/net/net.c index a92df90..2dc49f5 100644 --- a/net/net.c +++ b/net/net.c @@ -183,10 +183,13 @@ uchar PktBuf[(PKTBUFSRX+1) * PKTSIZE_ALIGN + PKTALIGN]; /* Receive packet */ uchar *NetRxPackets[PKTBUFSRX];
-/* Current RX packet handler */ -static rxhand_f *packetHandler; +/* Current UDP RX packet handler */ +static rxhand_f *udpPacketHandler; +/* Current ARP RX packet handler */ +static rxhand_f *arpPacketHandler; #ifdef CONFIG_CMD_TFTPPUT -static rxhand_icmp_f *packet_icmp_handler; /* Current ICMP rx handler */ +/* Current ICMP rx handler */ +static rxhand_icmp_f *packet_icmp_handler; #endif /* Current timeout handler */ static thand_f *timeHandler; @@ -257,6 +260,15 @@ static void NetInitLoop(enum proto_t protocol) return; }
+static void +NetCleanupLoop(void) +{ + /* Clear the handlers */ + NetSetUDPHandler(NULL); + NetSetARPHandler(NULL); + NetSetTimeout(0, NULL); +} + /**********************************************************************/ /* * Main network processing loop. @@ -264,6 +276,7 @@ static void NetInitLoop(enum proto_t protocol)
int NetLoop(enum proto_t protocol) { + int i; bd_t *bd = gd->bd; int ret = -1;
@@ -275,16 +288,13 @@ int NetLoop(enum proto_t protocol)
ArpInit();
- if (!NetTxPacket) { - int i; - /* - * Setup packet buffers, aligned correctly. - */ - NetTxPacket = &PktBuf[0] + (PKTALIGN - 1); - NetTxPacket -= (ulong)NetTxPacket % PKTALIGN; - for (i = 0; i < PKTBUFSRX; i++) - NetRxPackets[i] = NetTxPacket + (i+1)*PKTSIZE_ALIGN; - } + /* + * Setup packet buffers, aligned correctly. + */ + NetTxPacket = &PktBuf[0] + (PKTALIGN - 1); + NetTxPacket -= (ulong)NetTxPacket % PKTALIGN; + for (i = 0; i < PKTBUFSRX; i++) + NetRxPackets[i] = NetTxPacket + (i+1)*PKTSIZE_ALIGN;
eth_halt(); eth_set_current(); @@ -422,6 +432,7 @@ restart: * Abort if ctrl-c was pressed. */ if (ctrlc()) { + NetCleanupLoop(); eth_halt(); puts("\nAbort\n"); goto done; @@ -464,6 +475,7 @@ restart: goto restart;
case NETLOOP_SUCCESS: + NetCleanupLoop(); if (NetBootFileXferSize > 0) { char buf[20]; printf("Bytes transferred = %ld (%lx hex)\n", @@ -480,6 +492,7 @@ restart: goto done;
case NETLOOP_FAIL: + NetCleanupLoop(); goto done; } } @@ -501,13 +514,6 @@ startAgainTimeout(void) NetSetState(NETLOOP_RESTART); }
-static void -startAgainHandler(uchar *pkt, unsigned dest, IPaddr_t sip, - unsigned src, unsigned len) -{ - /* Totally ignore the packet */ -} - void NetStartAgain(void) { char *nretry; @@ -544,7 +550,7 @@ void NetStartAgain(void) NetRestartWrap = 0; if (NetDevExists) { NetSetTimeout(10000UL, startAgainTimeout); - NetSetHandler(startAgainHandler); + NetSetUDPHandler(NULL); } else { NetSetState(NETLOOP_FAIL); } @@ -559,16 +565,27 @@ void NetStartAgain(void) */
rxhand_f * -NetGetHandler(void) +NetGetUDPHandler(void) +{ + return udpPacketHandler; +} + +void +NetSetUDPHandler(rxhand_f *f) { - return packetHandler; + udpPacketHandler = f; }
+rxhand_f * +NetGetARPHandler(void) +{ + return arpPacketHandler; +}
void -NetSetHandler(rxhand_f *f) +NetSetARPHandler(rxhand_f *f) { - packetHandler = f; + arpPacketHandler = f; }
#ifdef CONFIG_CMD_TFTPPUT @@ -1101,7 +1118,8 @@ NetReceive(volatile uchar *inpkt, int len) /* * IP header OK. Pass the packet to the current handler. */ - (*packetHandler)((uchar *)ip + IP_UDP_HDR_SIZE, + if (udpPacketHandler != NULL) + (*udpPacketHandler)((uchar *)ip + IP_UDP_HDR_SIZE, ntohs(ip->udp_dst), src_ip, ntohs(ip->udp_src), diff --git a/net/nfs.c b/net/nfs.c index e366505..752ab33 100644 --- a/net/nfs.c +++ b/net/nfs.c @@ -735,7 +735,7 @@ NfsStart(void) "Loading: *\b", load_addr);
NetSetTimeout(NFS_TIMEOUT, NfsTimeout); - NetSetHandler(NfsHandler); + NetSetUDPHandler(NfsHandler);
NfsTimeoutCount = 0; NfsState = STATE_PRCLOOKUP_PROG_MOUNT_REQ; diff --git a/net/sntp.c b/net/sntp.c index f5fa6f7..4d902cb 100644 --- a/net/sntp.c +++ b/net/sntp.c @@ -85,7 +85,7 @@ SntpStart(void) debug("%s\n", __func__);
NetSetTimeout(SNTP_TIMEOUT, SntpTimeout); - NetSetHandler(SntpHandler); + NetSetUDPHandler(SntpHandler); memset(NetServerEther, 0, 6);
SntpSend(); diff --git a/net/tftp.c b/net/tftp.c index 49ddbd7..ae8abc7 100644 --- a/net/tftp.c +++ b/net/tftp.c @@ -778,7 +778,7 @@ void TftpStart(enum proto_t protocol) TftpTimeoutCountMax = TftpRRQTimeoutCountMax;
NetSetTimeout(TftpTimeoutMSecs, TftpTimeout); - NetSetHandler(TftpHandler); + NetSetUDPHandler(TftpHandler); #ifdef CONFIG_CMD_TFTPPUT net_set_icmp_handler(icmp_handler); #endif @@ -840,7 +840,7 @@ TftpStartServer(void) #endif
TftpState = STATE_RECV_WRQ; - NetSetHandler(TftpHandler); + NetSetUDPHandler(TftpHandler); } #endif /* CONFIG_CMD_TFTPSRV */

On Thursday 19 January 2012 19:53:20 Joe Hershberger wrote:
Only call the handlers if one was registered... don't require protocols to register a handler (eliminating dummies) NetConsole now uses the ARP handler when waiting on arp (instead of needing a #define hack in arp.c) Clear handlers at the end of net loop
what if the SetHandler funcs took care of setting to a dummy func when it was given NULL ? then calling code need not worry about creating their own dummies, and you don't have to add NULL pointer checking. -mike

Hi Mike,
On Fri, Feb 3, 2012 at 6:21 AM, Mike Frysinger vapier@gentoo.org wrote:
On Thursday 19 January 2012 19:53:20 Joe Hershberger wrote:
Only call the handlers if one was registered... don't require protocols to register a handler (eliminating dummies) NetConsole now uses the ARP handler when waiting on arp (instead of needing a #define hack in arp.c) Clear handlers at the end of net loop
what if the SetHandler funcs took care of setting to a dummy func when it was given NULL ? then calling code need not worry about creating their own dummies, and you don't have to add NULL pointer checking.
The calling code doesn't create dummies any longer. That is the point of the NULL checking. Do you prefer the set method register a dummy instead of a NULL check?
-Joe

On Wednesday 08 February 2012 17:52:54 Joe Hershberger wrote:
On Fri, Feb 3, 2012 at 6:21 AM, Mike Frysinger wrote:
On Thursday 19 January 2012 19:53:20 Joe Hershberger wrote:
Only call the handlers if one was registered... don't require protocols to register a handler (eliminating dummies) NetConsole now uses the ARP handler when waiting on arp (instead of needing a #define hack in arp.c) Clear handlers at the end of net loop
what if the SetHandler funcs took care of setting to a dummy func when it was given NULL ? then calling code need not worry about creating their own dummies, and you don't have to add NULL pointer checking.
The calling code doesn't create dummies any longer. That is the point of the NULL checking. Do you prefer the set method register a dummy instead of a NULL check?
yes, i like that fringe code need not setup dummies anymore themselves. however, i think the core code should take take on setting up dummies so that the rest of the code need not have to do NULL checking everywhere. -mike

NetUpdateEther() will preserve the original Ethernet packet header and simply replace the src and dest MACs and the protocol
Signed-off-by: Joe Hershberger joe.hershberger@ni.com Cc: Joe Hershberger joe.hershberger@gmail.com Cc: Wolfgang Denk wd@denx.de --- include/net.h | 1 + net/arp.c | 2 +- net/net.c | 22 ++++++++++++++++++++++ net/ping.c | 10 +++++----- 4 files changed, 29 insertions(+), 6 deletions(-)
diff --git a/include/net.h b/include/net.h index dca8e1c..af6a803 100644 --- a/include/net.h +++ b/include/net.h @@ -413,6 +413,7 @@ extern int NetEthHdrSize(void);
/* Set ethernet header; returns the size of the header */ extern int NetSetEther(uchar *, uchar *, uint); +extern int NetUpdateEther(struct Ethernet_t *et, uchar *addr, uint prot);
/* Set IP header */ extern void NetSetIPHeader(uchar *pkt, IPaddr_t dest, IPaddr_t source); diff --git a/net/arp.c b/net/arp.c index 5b6c787..8c3557b 100644 --- a/net/arp.c +++ b/net/arp.c @@ -158,7 +158,7 @@ void ArpReceive(struct Ethernet_t *et, struct IP_UDP_t *ip, int len) /* reply with our IP address */ debug("Got ARP REQUEST, return our IP\n"); pkt = (uchar *)et; - eth_hdr_size = NetSetEther(pkt, et->et_src, PROT_ARP); + eth_hdr_size = NetUpdateEther(et, et->et_src, PROT_ARP); pkt += eth_hdr_size; arp->ar_op = htons(ARPOP_REPLY); memcpy(&arp->ar_tha, &arp->ar_sha, ARP_HLEN); diff --git a/net/net.c b/net/net.c index 2dc49f5..1c0c822 100644 --- a/net/net.c +++ b/net/net.c @@ -1274,6 +1274,28 @@ NetSetEther(uchar *xet, uchar * addr, uint prot) } }
+int +NetUpdateEther(struct Ethernet_t *et, uchar *addr, uint prot) +{ + ushort protlen; + + memcpy(et->et_dest, addr, 6); + memcpy(et->et_src, NetOurEther, 6); + protlen = ntohs(et->et_protlen); + if (protlen == PROT_VLAN) { + struct VLAN_Ethernet_t *vet = (struct VLAN_Ethernet_t *)et; + vet->vet_type = htons(prot); + return VLAN_ETHER_HDR_SIZE; + } else if (protlen > 1514) { + et->et_protlen = htons(prot); + return ETHER_HDR_SIZE; + } else { + /* 802.2 + SNAP */ + et->et_prot = htons(prot); + return E802_HDR_SIZE; + } +} + void NetSetIPHeader(uchar *pkt, IPaddr_t dest, IPaddr_t source) { diff --git a/net/ping.c b/net/ping.c index 795d5c9..913506b 100644 --- a/net/ping.c +++ b/net/ping.c @@ -89,6 +89,7 @@ void PingReceive(struct Ethernet_t *et, struct IP_UDP_t *ip, int len) { struct ICMP_t *icmph = (struct ICMP_t *)&(ip->udp_src); IPaddr_t src_ip; + int eth_hdr_size;
switch (icmph->type) { case ICMP_ECHO_REPLY: @@ -97,11 +98,10 @@ void PingReceive(struct Ethernet_t *et, struct IP_UDP_t *ip, int len) NetSetState(NETLOOP_SUCCESS); return; case ICMP_ECHO_REQUEST: - debug("Got ICMP ECHO REQUEST, return " - "%d bytes\n", ETHER_HDR_SIZE + len); + eth_hdr_size = NetUpdateEther(et, et->et_src, PROT_IP);
- memcpy(&et->et_dest[0], &et->et_src[0], 6); - memcpy(&et->et_src[0], NetOurEther, 6); + debug("Got ICMP ECHO REQUEST, return " + "%d bytes\n", eth_hdr_size + len);
ip->ip_sum = 0; ip->ip_off = 0; @@ -114,7 +114,7 @@ void PingReceive(struct Ethernet_t *et, struct IP_UDP_t *ip, int len) icmph->checksum = 0; icmph->checksum = ~NetCksum((uchar *)icmph, (len - IP_HDR_SIZE) >> 1); - NetSendPacket((uchar *)et, ETHER_HDR_SIZE + len); + NetSendPacket((uchar *)et, eth_hdr_size + len); return; /* default: return;*/

On Thursday 19 January 2012 19:53:21 Joe Hershberger wrote:
--- a/net/net.c +++ b/net/net.c
+int +NetUpdateEther(struct Ethernet_t *et, uchar *addr, uint prot) +{
- ushort protlen;
- memcpy(et->et_dest, addr, 6);
- memcpy(et->et_src, NetOurEther, 6);
- protlen = ntohs(et->et_protlen);
- if (protlen == PROT_VLAN) {
struct VLAN_Ethernet_t *vet = (struct VLAN_Ethernet_t *)et;
vet->vet_type = htons(prot);
return VLAN_ETHER_HDR_SIZE;
- } else if (protlen > 1514) {
et->et_protlen = htons(prot);
return ETHER_HDR_SIZE;
- } else {
/* 802.2 + SNAP */
et->et_prot = htons(prot);
return E802_HDR_SIZE;
- }
+}
seems to overlap a bit with NetSetEther. can't you have one use the other ? -mike

Hi Mike,
On Fri, Feb 3, 2012 at 6:25 AM, Mike Frysinger vapier@gentoo.org wrote:
On Thursday 19 January 2012 19:53:21 Joe Hershberger wrote:
--- a/net/net.c +++ b/net/net.c
+int +NetUpdateEther(struct Ethernet_t *et, uchar *addr, uint prot) +{
- ushort protlen;
- memcpy(et->et_dest, addr, 6);
- memcpy(et->et_src, NetOurEther, 6);
- protlen = ntohs(et->et_protlen);
- if (protlen == PROT_VLAN) {
- struct VLAN_Ethernet_t *vet = (struct VLAN_Ethernet_t *)et;
- vet->vet_type = htons(prot);
- return VLAN_ETHER_HDR_SIZE;
- } else if (protlen > 1514) {
- et->et_protlen = htons(prot);
- return ETHER_HDR_SIZE;
- } else {
- /* 802.2 + SNAP */
- et->et_prot = htons(prot);
- return E802_HDR_SIZE;
- }
+}
seems to overlap a bit with NetSetEther. can't you have one use the other ?
I don't see a very clean way ATM. Maybe some future refactoring will make that more obvious.
-Joe

Before this patch, bootp would not overwrite the value, but the value was still clobbered in the env
Signed-off-by: Joe Hershberger joe.hershberger@ni.com Cc: Joe Hershberger joe.hershberger@gmail.com Cc: Wolfgang Denk wd@denx.de --- common/cmd_net.c | 4 ++-- 1 files changed, 2 insertions(+), 2 deletions(-)
diff --git a/common/cmd_net.c b/common/cmd_net.c index 89519fa..49ef680 100644 --- a/common/cmd_net.c +++ b/common/cmd_net.c @@ -148,12 +148,12 @@ static void netboot_update_env (void) ip_to_string (NetOurIP, tmp); setenv ("ipaddr", tmp); } - +#if !defined(CONFIG_BOOTP_SERVERIP) if (NetServerIP) { ip_to_string (NetServerIP, tmp); setenv ("serverip", tmp); } - +#endif if (NetOurDNSIP) { ip_to_string (NetOurDNSIP, tmp); setenv ("dnsip", tmp);

On Thursday 19 January 2012 19:53:22 Joe Hershberger wrote:
--- a/common/cmd_net.c +++ b/common/cmd_net.c
+#if !defined(CONFIG_BOOTP_SERVERIP) if (NetServerIP) { ip_to_string (NetServerIP, tmp); setenv ("serverip", tmp); } +#endif
please add a comment referring people to net/bootp.c:BootpCopyNetParams() -mike

There is no reason to use compiler magic to fix this Actually move the definition inside the #ifdef
Signed-off-by: Joe Hershberger joe.hershberger@ni.com Cc: Joe Hershberger joe.hershberger@gmail.com Cc: Wolfgang Denk wd@denx.de --- net/bootp.c | 7 +++---- 1 files changed, 3 insertions(+), 4 deletions(-)
diff --git a/net/bootp.c b/net/bootp.c index 9d709f1..187b3a8 100644 --- a/net/bootp.c +++ b/net/bootp.c @@ -20,7 +20,6 @@ #ifdef CONFIG_STATUS_LED #include <status_led.h> #endif -#include <linux/compiler.h>
#define BOOTP_VENDOR_MAGIC 0x63825363 /* RFC1048 Magic Cookie */
@@ -98,15 +97,15 @@ static int BootpCheckPkt(uchar *pkt, unsigned dest, unsigned src, unsigned len) */ static void BootpCopyNetParams(struct Bootp_t *bp) { - __maybe_unused IPaddr_t tmp_ip; - - NetCopyIP(&NetOurIP, &bp->bp_yiaddr); #if !defined(CONFIG_BOOTP_SERVERIP) + IPaddr_t tmp_ip; + NetCopyIP(&tmp_ip, &bp->bp_siaddr); if (tmp_ip != 0) NetCopyIP(&NetServerIP, &bp->bp_siaddr); memcpy(NetServerEther, ((struct Ethernet_t *)NetRxPacket)->et_src, 6); #endif + NetCopyIP(&NetOurIP, &bp->bp_yiaddr); if (strlen(bp->bp_file) > 0) copy_filename(BootFile, bp->bp_file, sizeof(BootFile));

Acked-by: Mike Frysinger vapier@gentoo.org -mike

Don't force ARP clients to return the MAC address if they don't care about it (such as ping)
Signed-off-by: Joe Hershberger joe.hershberger@ni.com Cc: Joe Hershberger joe.hershberger@gmail.com Cc: Wolfgang Denk wd@denx.de --- net/arp.c | 9 +++++---- net/ping.c | 10 +++------- 2 files changed, 8 insertions(+), 11 deletions(-)
diff --git a/net/arp.c b/net/arp.c index 8c3557b..b3be0a6 100644 --- a/net/arp.c +++ b/net/arp.c @@ -170,7 +170,7 @@ void ArpReceive(struct Ethernet_t *et, struct IP_UDP_t *ip, int len)
case ARPOP_REPLY: /* arp reply */ /* are we waiting for a reply */ - if (!NetArpWaitPacketIP || !NetArpWaitPacketMAC) + if (!NetArpWaitPacketIP) break;
#ifdef CONFIG_KEEP_SERVERADDR @@ -189,8 +189,9 @@ void ArpReceive(struct Ethernet_t *et, struct IP_UDP_t *ip, int len) arp->ar_data);
/* save address for later use */ - memcpy(NetArpWaitPacketMAC, - &arp->ar_sha, ARP_HLEN); + if (NetArpWaitPacketMAC != NULL) + memcpy(NetArpWaitPacketMAC, + &arp->ar_sha, ARP_HLEN);
if (NetGetARPHandler() != NULL) NetGetARPHandler()((uchar *)arp, 0, @@ -198,7 +199,7 @@ void ArpReceive(struct Ethernet_t *et, struct IP_UDP_t *ip, int len)
/* modify header, and transmit it */ memcpy(((struct Ethernet_t *)NetArpWaitTxPacket)-> - et_dest, NetArpWaitPacketMAC, ARP_HLEN); + et_dest, &arp->ar_sha, ARP_HLEN); NetSendPacket(NetArpWaitTxPacket, NetArpWaitTxPacketSize);
diff --git a/net/ping.c b/net/ping.c index 913506b..687c5aa 100644 --- a/net/ping.c +++ b/net/ping.c @@ -40,22 +40,18 @@ static void SetICMPHeader(uchar *pkt, IPaddr_t dest)
static int PingSend(void) { - static uchar mac[6]; uchar *pkt; int eth_hdr_size;
/* XXX always send arp request */
- memcpy(mac, NetEtherNullAddr, 6); - debug("sending ARP for %pI4\n", &NetPingIP);
NetArpWaitPacketIP = NetPingIP; - NetArpWaitPacketMAC = mac;
- pkt = NetArpWaitTxPacket; - eth_hdr_size = NetSetEther(pkt, mac, PROT_IP); - pkt += eth_hdr_size; + eth_hdr_size = NetSetEther(NetArpWaitTxPacket, NetEtherNullAddr, + PROT_IP); + pkt = NetArpWaitTxPacket + eth_hdr_size;
SetICMPHeader(pkt, NetPingIP);

Acked-by: Mike Frysinger vapier@gentoo.org -mike

A new non-static function NetInit() will initialize buffers and read from the env Only update from the env on each entry to NetLoop() Check when attempting to send a packet that the buffers were initialized
Signed-off-by: Joe Hershberger joe.hershberger@ni.com Cc: Joe Hershberger joe.hershberger@gmail.com Cc: Wolfgang Denk wd@denx.de --- include/net.h | 7 ++++--- net/net.c | 48 +++++++++++++++++++++++++++++++++--------------- 2 files changed, 37 insertions(+), 18 deletions(-)
diff --git a/include/net.h b/include/net.h index af6a803..1861406 100644 --- a/include/net.h +++ b/include/net.h @@ -400,7 +400,8 @@ extern IPaddr_t Mcast_addr; #endif
/* Initialize the network adapter */ -extern int NetLoop(enum proto_t); +extern void NetInit(void); +extern int NetLoop(enum proto_t protocol);
/* Shutdown adapters and cleanup */ extern void NetStop(void); @@ -439,10 +440,10 @@ extern void NetSetState(int state); #define NETLOOP_SUCCESS 3 #define NETLOOP_FAIL 4
-/* Transmit "NetTxPacket" */ +/* Transmit a packet */ extern void NetSendPacket(uchar *, int);
-/* Transmit UDP packet, performing ARP request if needed +/* Transmit "NetTxPacket" as UDP packet, performing ARP request if needed (ether will be populated) */ extern int NetSendUDPPacket(uchar *ether, IPaddr_t dest, int dport, int sport, int payload_len); diff --git a/net/net.c b/net/net.c index 1c0c822..51d899b 100644 --- a/net/net.c +++ b/net/net.c @@ -236,7 +236,7 @@ void net_auto_load(void) TftpStart(TFTPGET); }
-static void NetInitLoop(enum proto_t protocol) +static void NetInitLoop(void) { static int env_changed_id; bd_t *bd = gd->bd; @@ -269,6 +269,30 @@ NetCleanupLoop(void) NetSetTimeout(0, NULL); }
+void +NetInit(void) +{ + static int first_call = 1; + + if (first_call) { + /* + * Setup packet buffers, aligned correctly. + */ + int i; + NetTxPacket = &PktBuf[0] + (PKTALIGN - 1); + NetTxPacket -= (ulong)NetTxPacket % PKTALIGN; + for (i = 0; i < PKTBUFSRX; i++) + NetRxPackets[i] = NetTxPacket + (i+1)*PKTSIZE_ALIGN; + + ArpInit(); + + /* Only need to setup buffer pointers once. */ + first_call = 0; + } + + NetInitLoop(); +} + /**********************************************************************/ /* * Main network processing loop. @@ -276,26 +300,14 @@ NetCleanupLoop(void)
int NetLoop(enum proto_t protocol) { - int i; bd_t *bd = gd->bd; int ret = -1;
NetRestarted = 0; NetDevExists = 0; - - NetTxPacket = NULL; NetTryCount = 1;
- ArpInit(); - - /* - * Setup packet buffers, aligned correctly. - */ - NetTxPacket = &PktBuf[0] + (PKTALIGN - 1); - NetTxPacket -= (ulong)NetTxPacket % PKTALIGN; - for (i = 0; i < PKTBUFSRX; i++) - NetRxPackets[i] = NetTxPacket + (i+1)*PKTSIZE_ALIGN; - + NetInit(); eth_halt(); eth_set_current(); if (eth_init(bd) < 0) { @@ -313,7 +325,7 @@ restart: * here on, this code is a state machine driven by received * packets and timer events. */ - NetInitLoop(protocol); + NetInitLoop();
switch (net_check_prereq(protocol)) { case 1: @@ -627,6 +639,12 @@ NetSendUDPPacket(uchar *ether, IPaddr_t dest, int dport, int sport, { uchar *pkt;
+ /* make sure the NetTxPacket is initialized (NetInit() was called) */ + if (NetTxPacket == NULL) { + puts("*** ERROR: NetTxPacket buffer pointer is NULL!\n"); + return -1; + } + /* convert to new style broadcast */ if (dest == 0) dest = 0xFFFFFFFF;

On Thursday 19 January 2012 19:53:25 Joe Hershberger wrote:
--- a/include/net.h +++ b/include/net.h
-extern int NetLoop(enum proto_t); +extern int NetLoop(enum proto_t protocol);
explicit variable names are unnecessary when the type is self explanatory
--- a/net/net.c +++ b/net/net.c
-static void NetInitLoop(enum proto_t protocol) +static void NetInitLoop(void)
this could prob do with a sep patch ...
+void +NetInit(void) +{
- static int first_call = 1;
- if (first_call) {
/*
* Setup packet buffers, aligned correctly.
*/
int i;
NetTxPacket = &PktBuf[0] + (PKTALIGN - 1);
NetTxPacket -= (ulong)NetTxPacket % PKTALIGN;
for (i = 0; i < PKTBUFSRX; i++)
NetRxPackets[i] = NetTxPacket + (i+1)*PKTSIZE_ALIGN;
ArpInit();
/* Only need to setup buffer pointers once. */
first_call = 0;
- }
it *seems* like it should be fine, but i worry what might be lurking. guess we'll find out :P.
@@ -627,6 +639,12 @@ NetSendUDPPacket
- /* make sure the NetTxPacket is initialized (NetInit() was called) */
- if (NetTxPacket == NULL) {
puts("*** ERROR: NetTxPacket buffer pointer is NULL!\n");
return -1;
- }
why check here only (udp) ? and why check at all ? this should be an assert() in the comment netsendpacket entry point ... -mike

Hi Mike,
On Fri, Feb 3, 2012 at 6:37 AM, Mike Frysinger vapier@gentoo.org wrote:
On Thursday 19 January 2012 19:53:25 Joe Hershberger wrote:
@@ -627,6 +639,12 @@ NetSendUDPPacket
- /* make sure the NetTxPacket is initialized (NetInit() was called) */
- if (NetTxPacket == NULL) {
- puts("*** ERROR: NetTxPacket buffer pointer is NULL!\n");
- return -1;
- }
why check here only (udp) ? and why check at all ? this should be an assert() in the comment netsendpacket entry point ...
This is specifically needed here because the NetSendUDPPacket() function expects to use the NetTxPacket as a buffer and will write header information into it. The NetSendPacket() function takes the packet buffer itself as a parameter, so it isn't expecting anything as a side-band precondition to avoid crashing. I specifically added the check because the failure to initialize NetTxPacket in all code paths caused problems. It is easy to run into this if someone were to add a new thing that can send a packet. I will change it to an assert.
-Joe

Signed-off-by: Joe Hershberger joe.hershberger@ni.com Cc: Joe Hershberger joe.hershberger@gmail.com Cc: Wolfgang Denk wd@denx.de --- net/net.c | 44 +++++++++++++++++++++++++------------------- 1 files changed, 25 insertions(+), 19 deletions(-)
diff --git a/net/net.c b/net/net.c index 51d899b..59ac167 100644 --- a/net/net.c +++ b/net/net.c @@ -638,6 +638,9 @@ NetSendUDPPacket(uchar *ether, IPaddr_t dest, int dport, int sport, int payload_len) { uchar *pkt; + int need_arp = 0; + int eth_hdr_size; + int pkt_hdr_size;
/* make sure the NetTxPacket is initialized (NetInit() was called) */ if (NetTxPacket == NULL) { @@ -658,40 +661,43 @@ NetSendUDPPacket(uchar *ether, IPaddr_t dest, int dport, int sport, * an ARP request */ if (memcmp(ether, NetEtherNullAddr, 6) == 0) { + need_arp = 1; + pkt = NetArpWaitTxPacket; + } else + pkt = (uchar *)NetTxPacket; + + eth_hdr_size = NetSetEther(pkt, ether, PROT_IP); + pkt += eth_hdr_size; + NetSetUDPHeader(pkt, dest, dport, sport, payload_len); + pkt_hdr_size = eth_hdr_size + IP_UDP_HDR_SIZE;
- debug("sending ARP for %08x\n", dest); + if (need_arp) { + debug("sending ARP for %pI4\n", &dest);
+ /* save the ip and eth addr for the packet to send after arp */ NetArpWaitPacketIP = dest; NetArpWaitPacketMAC = ether;
- pkt = NetArpWaitTxPacket; - pkt += NetSetEther(pkt, NetArpWaitPacketMAC, PROT_IP); - - NetSetUDPHeader(pkt, dest, dport, sport, payload_len); + /* + * Copy the packet data from the NetTxPacket into the + * NetArpWaitTxPacket to send after arp + */ memcpy(pkt + IP_UDP_HDR_SIZE, (uchar *)NetTxPacket + - (pkt - (uchar *)NetArpWaitTxPacket) + - IP_UDP_HDR_SIZE, payload_len); + pkt_hdr_size, payload_len);
/* size of the waiting packet */ - NetArpWaitTxPacketSize = (pkt - NetArpWaitTxPacket) + - IP_UDP_HDR_SIZE + payload_len; + NetArpWaitTxPacketSize = pkt_hdr_size + payload_len;
/* and do the ARP request */ NetArpWaitTry = 1; NetArpWaitTimerStart = get_timer(0); ArpRequest(); return 1; /* waiting */ + } else { + debug("sending UDP to %pI4/%pM\n", &dest, ether); + NetSendPacket(NetTxPacket, pkt_hdr_size + payload_len); + return 0; /* transmitted */ } - - debug("sending UDP to %08x/%pM\n", dest, ether); - - pkt = (uchar *)NetTxPacket; - pkt += NetSetEther(pkt, ether, PROT_IP); - NetSetUDPHeader(pkt, dest, dport, sport, payload_len); - NetSendPacket(NetTxPacket, (pkt - NetTxPacket) + IP_UDP_HDR_SIZE + - payload_len); - - return 0; /* transmitted */ }
#ifdef CONFIG_IP_DEFRAG

Use the NetArpTxPacket for the ARP packet, not to hold what used to be in NetTxPacket This saves a copy and makes the code easier to understand Generalize the formation of the ARP packet
Signed-off-by: Joe Hershberger joe.hershberger@ni.com Cc: Joe Hershberger joe.hershberger@gmail.com Cc: Wolfgang Denk wd@denx.de --- net/arp.c | 44 ++++++++++++++++++++++++-------------------- net/arp.h | 4 ++-- net/net.c | 24 ++++++------------------ net/ping.c | 5 ++--- 4 files changed, 34 insertions(+), 43 deletions(-)
diff --git a/net/arp.c b/net/arp.c index b3be0a6..938db59 100644 --- a/net/arp.c +++ b/net/arp.c @@ -30,25 +30,26 @@ IPaddr_t NetArpWaitPacketIP; IPaddr_t NetArpWaitReplyIP; /* MAC address of waiting packet's destination */ uchar *NetArpWaitPacketMAC; -/* THE transmit packet */ -uchar *NetArpWaitTxPacket; int NetArpWaitTxPacketSize; -uchar NetArpWaitPacketBuf[PKTSIZE_ALIGN + PKTALIGN]; ulong NetArpWaitTimerStart; int NetArpWaitTry;
+uchar *NetArpTxPacket; /* THE ARP transmit packet */ +uchar NetArpPacketBuf[PKTSIZE_ALIGN + PKTALIGN]; + void ArpInit(void) { /* XXX problem with bss workaround */ NetArpWaitPacketMAC = NULL; NetArpWaitPacketIP = 0; NetArpWaitReplyIP = 0; - NetArpWaitTxPacket = &NetArpWaitPacketBuf[0] + (PKTALIGN - 1); - NetArpWaitTxPacket -= (ulong)NetArpWaitTxPacket % PKTALIGN; NetArpWaitTxPacketSize = 0; + NetArpTxPacket = &NetArpPacketBuf[0] + (PKTALIGN - 1); + NetArpTxPacket -= (ulong)NetArpTxPacket % PKTALIGN; }
-void ArpRequest(void) +void ArpRawRequest(IPaddr_t sourceIP, const uchar *targetEther, + IPaddr_t targetIP) { uchar *pkt; struct ARP_t *arp; @@ -56,7 +57,7 @@ void ArpRequest(void)
debug("ARP broadcast %d\n", NetArpWaitTry);
- pkt = NetTxPacket; + pkt = NetArpTxPacket;
eth_hdr_size = NetSetEther(pkt, NetBcastAddr, PROT_ARP); pkt += eth_hdr_size; @@ -69,12 +70,16 @@ void ArpRequest(void) arp->ar_pln = ARP_PLEN; arp->ar_op = htons(ARPOP_REQUEST);
- /* source ET addr */ - memcpy(&arp->ar_sha, NetOurEther, ARP_HLEN); - /* source IP addr */ - NetWriteIP(&arp->ar_spa, NetOurIP); - /* dest ET addr = 0 */ - memset(&arp->ar_tha, 0, ARP_HLEN); + memcpy(&arp->ar_sha, NetOurEther, ARP_HLEN); /* source ET addr */ + NetWriteIP(&arp->ar_spa, sourceIP); /* source IP addr */ + memcpy(&arp->ar_tha, targetEther, ARP_HLEN); /* target ET addr */ + NetWriteIP(&arp->ar_tpa, targetIP); /* target IP addr */ + + NetSendPacket(NetArpTxPacket, eth_hdr_size + ARP_HDR_SIZE); +} + +void ArpRequest(void) +{ if ((NetArpWaitPacketIP & NetOurSubnetMask) != (NetOurIP & NetOurSubnetMask)) { if (NetOurGatewayIP == 0) { @@ -87,8 +92,7 @@ void ArpRequest(void) NetArpWaitReplyIP = NetArpWaitPacketIP; }
- NetWriteIP(&arp->ar_tpa, NetArpWaitReplyIP); - NetSendPacket(NetTxPacket, eth_hdr_size + ARP_HDR_SIZE); + ArpRawRequest(NetOurIP, NetEtherNullAddr, NetArpWaitReplyIP); }
void ArpTimeoutCheck(void) @@ -197,11 +201,11 @@ void ArpReceive(struct Ethernet_t *et, struct IP_UDP_t *ip, int len) NetGetARPHandler()((uchar *)arp, 0, reply_ip_addr, 0, len);
- /* modify header, and transmit it */ - memcpy(((struct Ethernet_t *)NetArpWaitTxPacket)-> - et_dest, &arp->ar_sha, ARP_HLEN); - NetSendPacket(NetArpWaitTxPacket, - NetArpWaitTxPacketSize); + /* set the mac address in the waiting packet's header + and transmit it */ + memcpy(((struct Ethernet_t *)NetTxPacket)->et_dest, + &arp->ar_sha, ARP_HLEN); + NetSendPacket(NetTxPacket, NetArpWaitTxPacketSize);
/* no arp request pending now */ NetArpWaitPacketIP = 0; diff --git a/net/arp.h b/net/arp.h index d9ce03d..9657e67 100644 --- a/net/arp.h +++ b/net/arp.h @@ -16,14 +16,14 @@ extern IPaddr_t NetArpWaitPacketIP; /* MAC address of waiting packet's destination */ extern uchar *NetArpWaitPacketMAC; -/* THE transmit packet */ -extern uchar *NetArpWaitTxPacket; extern int NetArpWaitTxPacketSize; extern ulong NetArpWaitTimerStart; extern int NetArpWaitTry;
void ArpInit(void); void ArpRequest(void); +void ArpRawRequest(IPaddr_t sourceIP, const uchar *targetEther, + IPaddr_t targetIP); void ArpTimeoutCheck(void); void ArpReceive(struct Ethernet_t *et, struct IP_UDP_t *ip, int len);
diff --git a/net/net.c b/net/net.c index 59ac167..fe4e528 100644 --- a/net/net.c +++ b/net/net.c @@ -444,6 +444,9 @@ restart: * Abort if ctrl-c was pressed. */ if (ctrlc()) { + /* cancel any ARP that may not have completed */ + NetArpWaitPacketIP = 0; + NetCleanupLoop(); eth_halt(); puts("\nAbort\n"); @@ -638,7 +641,6 @@ NetSendUDPPacket(uchar *ether, IPaddr_t dest, int dport, int sport, int payload_len) { uchar *pkt; - int need_arp = 0; int eth_hdr_size; int pkt_hdr_size;
@@ -656,35 +658,21 @@ NetSendUDPPacket(uchar *ether, IPaddr_t dest, int dport, int sport, if (dest == 0xFFFFFFFF) ether = NetBcastAddr;
- /* - * if MAC address was not discovered yet, save the packet and do - * an ARP request - */ - if (memcmp(ether, NetEtherNullAddr, 6) == 0) { - need_arp = 1; - pkt = NetArpWaitTxPacket; - } else - pkt = (uchar *)NetTxPacket; + pkt = (uchar *)NetTxPacket;
eth_hdr_size = NetSetEther(pkt, ether, PROT_IP); pkt += eth_hdr_size; NetSetUDPHeader(pkt, dest, dport, sport, payload_len); pkt_hdr_size = eth_hdr_size + IP_UDP_HDR_SIZE;
- if (need_arp) { + /* if MAC address was not discovered yet, do an ARP request */ + if (memcmp(ether, NetEtherNullAddr, 6) == 0) { debug("sending ARP for %pI4\n", &dest);
/* save the ip and eth addr for the packet to send after arp */ NetArpWaitPacketIP = dest; NetArpWaitPacketMAC = ether;
- /* - * Copy the packet data from the NetTxPacket into the - * NetArpWaitTxPacket to send after arp - */ - memcpy(pkt + IP_UDP_HDR_SIZE, (uchar *)NetTxPacket + - pkt_hdr_size, payload_len); - /* size of the waiting packet */ NetArpWaitTxPacketSize = pkt_hdr_size + payload_len;
diff --git a/net/ping.c b/net/ping.c index 687c5aa..8332c88 100644 --- a/net/ping.c +++ b/net/ping.c @@ -49,9 +49,8 @@ static int PingSend(void)
NetArpWaitPacketIP = NetPingIP;
- eth_hdr_size = NetSetEther(NetArpWaitTxPacket, NetEtherNullAddr, - PROT_IP); - pkt = NetArpWaitTxPacket + eth_hdr_size; + eth_hdr_size = NetSetEther(NetTxPacket, NetEtherNullAddr, PROT_IP); + pkt = (uchar *)NetTxPacket + eth_hdr_size;
SetICMPHeader(pkt, NetPingIP);

This is useful if you want to look for a DHCP server, but try some other settings if not available
Signed-off-by: Joe Hershberger joe.hershberger@ni.com Cc: Joe Hershberger joe.hershberger@gmail.com Cc: Wolfgang Denk wd@denx.de --- README | 7 +++++++ net/bootp.c | 5 +++++ 2 files changed, 12 insertions(+), 0 deletions(-)
diff --git a/README b/README index 9d713e8..352db7d 100644 --- a/README +++ b/README @@ -1573,10 +1573,17 @@ The following options need to be configured: CONFIG_BOOTP_NTPSERVER CONFIG_BOOTP_TIMEOFFSET CONFIG_BOOTP_VENDOREX + CONFIG_BOOTP_CAN_FAIL
CONFIG_BOOTP_SERVERIP - TFTP server will be the serverip environment variable, not the BOOTP server.
+ CONFIG_BOOTP_CAN_FAIL - If the DHCP server is not found + after the configured retry count, the call will fail + instead of starting over. This can be used to fail over + to Link-local IP address configuration if the DHCP server + is not available. + CONFIG_BOOTP_DNS2 - If a DHCP client requests the DNS serverip from a DHCP server, it is possible that more than one DNS serverip is offered to the client. diff --git a/net/bootp.c b/net/bootp.c index 187b3a8..07f9d0e 100644 --- a/net/bootp.c +++ b/net/bootp.c @@ -327,8 +327,13 @@ static void BootpTimeout(void) { if (BootpTry >= TIMEOUT_COUNT) { +#ifdef CONFIG_BOOTP_CAN_FAIL + puts("\nRetry count exceeded\n"); + NetSetState(NETLOOP_FAIL); +#else puts("\nRetry count exceeded; starting again\n"); NetStartAgain(); +#endif } else { NetSetTimeout(TIMEOUT, BootpTimeout); BootpRequest();

On Thursday 19 January 2012 19:53:28 Joe Hershberger wrote:
--- a/README +++ b/README
CONFIG_BOOTP_CAN_FAIL - If the DHCP server is not found
after the configured retry count, the call will fail
instead of starting over. This can be used to fail over
to Link-local IP address configuration if the DHCP server
is not available.
the pedant in me says this should be CONFIG_BOOTP_MAY_FAIL -mike

Code based on networking/zcip.c in busybox - commit 8531d76a15890c2c535908ce888b2e2aed35b172
Signed-off-by: Joe Hershberger joe.hershberger@ni.com Cc: Joe Hershberger joe.hershberger@gmail.com Cc: Wolfgang Denk wd@denx.de --- README | 15 +++- common/cmd_net.c | 30 +++++ include/net.h | 2 +- net/Makefile | 4 +- net/arp.c | 3 + net/link_local.c | 333 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ net/link_local.h | 21 ++++ net/net.c | 15 +++ 8 files changed, 420 insertions(+), 3 deletions(-) create mode 100644 net/link_local.c create mode 100644 net/link_local.h
diff --git a/README b/README index 352db7d..776614e 100644 --- a/README +++ b/README @@ -782,6 +782,7 @@ The following options need to be configured: CONFIG_CMD_JFFS2 * JFFS2 Support CONFIG_CMD_KGDB * kgdb CONFIG_CMD_LDRINFO ldrinfo (display Blackfin loader) + CONFIG_CMD_LINK_LOCAL * link-local IP address auto-configuration (169.254.*.*) CONFIG_CMD_LOADB loadb CONFIG_CMD_LOADS loads CONFIG_CMD_MD5SUM print md5 message digest @@ -797,7 +798,7 @@ The following options need to be configured: CONFIG_CMD_PCA953X * PCA953x I2C gpio commands CONFIG_CMD_PCA953X_INFO * PCA953x I2C gpio info command CONFIG_CMD_PCI * pciinfo - CONFIG_CMD_PCMCIA * PCMCIA support + CONFIG_CMD_PCMCIA * PCMCIA support CONFIG_CMD_PING * send ICMP ECHO_REQUEST to network host CONFIG_CMD_PORTIO * Port I/O @@ -1614,6 +1615,18 @@ The following options need to be configured: the DHCP timeout and retry process takes a longer than this delay.
+ - Link-local IP address negotiation: + Negotiate with other link-local clients on the local network + for an address that doesn't require explicit configuration. + This is especially useful if a DHCP server cannot be guaranteed + to exist in all environments that the device must operate. + + The "llipaddr" variable is set with the most recently + negotiated address and is preferred in future negotiations. + + The "ipaddr", "netmask", and "gatewayip" variables are set + after successful negotiation to enable network access. + - CDP Options: CONFIG_CDP_DEVICE_ID
diff --git a/common/cmd_net.c b/common/cmd_net.c index 49ef680..ffca8c9 100644 --- a/common/cmd_net.c +++ b/common/cmd_net.c @@ -418,3 +418,33 @@ U_BOOT_CMD( );
#endif /* CONFIG_CMD_DNS */ + +#if defined(CONFIG_CMD_LINK_LOCAL) +int do_link_local(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[]) +{ + char tmp[22]; + + if (NetLoop(LINKLOCAL) < 0) + return 1; + + NetOurGatewayIP = 0; + ip_to_string(NetOurGatewayIP, tmp); + setenv("gatewayip", tmp); + + ip_to_string(NetOurSubnetMask, tmp); + setenv("netmask", tmp); + + ip_to_string(NetOurIP, tmp); + setenv("ipaddr", tmp); + setenv("llipaddr", tmp); /* store this for next time */ + + return 0; +} + +U_BOOT_CMD( + linklocal, 1, 1, do_link_local, + "acquire a network IP address using the link-local protocol", + "" +); + +#endif /* CONFIG_CMD_LINK_LOCAL */ diff --git a/include/net.h b/include/net.h index 1861406..fbb1412 100644 --- a/include/net.h +++ b/include/net.h @@ -368,7 +368,7 @@ extern int NetRestartWrap; /* Tried all network devices */
enum proto_t { BOOTP, RARP, ARP, TFTPGET, DHCP, PING, DNS, NFS, CDP, NETCONS, SNTP, - TFTPSRV, TFTPPUT + TFTPSRV, TFTPPUT, LINKLOCAL };
/* from net/net.c */ diff --git a/net/Makefile b/net/Makefile index f1c4859..5264687 100644 --- a/net/Makefile +++ b/net/Makefile @@ -32,15 +32,17 @@ COBJS-$(CONFIG_CMD_NET) += bootp.o COBJS-$(CONFIG_CMD_CDP) += cdp.o COBJS-$(CONFIG_CMD_DNS) += dns.o COBJS-$(CONFIG_CMD_NET) += eth.o +COBJS-$(CONFIG_CMD_LINK_LOCAL) += link_local.o COBJS-$(CONFIG_CMD_NET) += net.o COBJS-$(CONFIG_BOOTP_RANDOM_DELAY) += net_rand.o +COBJS-$(CONFIG_CMD_LINK_LOCAL) += net_rand.o COBJS-$(CONFIG_CMD_NFS) += nfs.o COBJS-$(CONFIG_CMD_PING) += ping.o COBJS-$(CONFIG_CMD_RARP) += rarp.o COBJS-$(CONFIG_CMD_SNTP) += sntp.o COBJS-$(CONFIG_CMD_NET) += tftp.o
-COBJS := $(COBJS-y) +COBJS := $(sort $(COBJS-y)) SRCS := $(COBJS:.o=.c) OBJS := $(addprefix $(obj),$(COBJS))
diff --git a/net/arp.c b/net/arp.c index 938db59..9eb4ab0 100644 --- a/net/arp.c +++ b/net/arp.c @@ -11,6 +11,9 @@ #include <common.h>
#include "arp.h" +#if defined(CONFIG_CMD_LINK_LOCAL) +#include "link_local.h" +#endif
#ifndef CONFIG_ARP_TIMEOUT /* Milliseconds before trying ARP again */ diff --git a/net/link_local.c b/net/link_local.c new file mode 100644 index 0000000..16ec9a7 --- /dev/null +++ b/net/link_local.c @@ -0,0 +1,333 @@ +/* + * RFC3927 ZeroConf IPv4 Link-Local addressing + * (see http://www.zeroconf.org/) + * + * Copied from BusyBox - networking/zcip.c + * + * Copyright (C) 2003 by Arthur van Hoff (avh@strangeberry.com) + * Copyright (C) 2004 by David Brownell + * Copyright (C) 2010 by Joe Hershberger + * + * Licensed under the GPL v2 or later + */ + +#include <common.h> +#include <net.h> +#include "arp.h" +#include "net_rand.h" + +/* We don't need more than 32 bits of the counter */ +#define MONOTONIC_MS() ((unsigned)get_timer(0) * (1000 / CONFIG_SYS_HZ)) + +enum { +/* 169.254.0.0 */ + LINKLOCAL_ADDR = 0xa9fe0000, + + IN_CLASSB_NET = 0xffff0000, + IN_CLASSB_HOST = 0x0000ffff, + +/* protocol timeout parameters, specified in seconds */ + PROBE_WAIT = 1, + PROBE_MIN = 1, + PROBE_MAX = 2, + PROBE_NUM = 3, + MAX_CONFLICTS = 10, + RATE_LIMIT_INTERVAL = 60, + ANNOUNCE_WAIT = 2, + ANNOUNCE_NUM = 2, + ANNOUNCE_INTERVAL = 2, + DEFEND_INTERVAL = 10 +}; + +/* States during the configuration process. */ +static enum ll_state_t { + PROBE = 0, + RATE_LIMIT_PROBE, + ANNOUNCE, + MONITOR, + DEFEND, + DISABLED +} state = DISABLED; + +static IPaddr_t ip; +static int timeout_ms = -1; +static unsigned deadline_ms; +static unsigned conflicts; +static unsigned nprobes; +static unsigned nclaims; +static int ready; + +static void LinkLocalTimeout(void); + +/** + * Pick a random link local IP address on 169.254/16, except that + * the first and last 256 addresses are reserved. + */ +static IPaddr_t pick(void) +{ + unsigned tmp; + + do { + tmp = rand() & IN_CLASSB_HOST; + } while (tmp > (IN_CLASSB_HOST - 0x0200)); + return (IPaddr_t) htonl((LINKLOCAL_ADDR + 0x0100) + tmp); +} + +/** + * Return milliseconds of random delay, up to "secs" seconds. + */ +static inline unsigned random_delay_ms(unsigned secs) +{ + return rand() % (secs * 1000); +} + +static void configure_wait(void) +{ + if (timeout_ms == -1) + return; + + /* poll, being ready to adjust current timeout */ + if (!timeout_ms) + timeout_ms = random_delay_ms(PROBE_WAIT); + + /* set deadline_ms to the point in time when we timeout */ + deadline_ms = MONOTONIC_MS() + timeout_ms; + + debug("...wait %d %s nprobes=%u, nclaims=%u\n", + timeout_ms, eth_get_name(), nprobes, nclaims); + + NetSetTimeout(timeout_ms, LinkLocalTimeout); +} + +void LinkLocalStart(void) +{ + ip = getenv_IPaddr("llipaddr"); + if (ip != 0 && (ip & IN_CLASSB_NET) != LINKLOCAL_ADDR) { + puts("invalid link address"); + NetSetState(NETLOOP_FAIL); + return; + } + NetOurSubnetMask = IN_CLASSB_NET; + + srand_mac(); + if (ip == 0) + ip = pick(); + + state = PROBE; + timeout_ms = 0; + conflicts = 0; + nprobes = 0; + nclaims = 0; + ready = 0; + + configure_wait(); +} + +static void +LinkLocalTimeout(void) +{ + switch (state) { + case PROBE: + /* timeouts in the PROBE state mean no conflicting ARP packets + have been received, so we can progress through the states */ + if (nprobes < PROBE_NUM) { + nprobes++; + debug("probe/%u %s@%pI4\n", + nprobes, eth_get_name(), &ip); + ArpRawRequest(0, NetEtherNullAddr, ip); + timeout_ms = PROBE_MIN * 1000; + timeout_ms += random_delay_ms(PROBE_MAX - PROBE_MIN); + } else { + /* Switch to announce state */ + state = ANNOUNCE; + nclaims = 0; + debug("announce/%u %s@%pI4\n", + nclaims, eth_get_name(), &ip); + ArpRawRequest(ip, NetOurEther, ip); + timeout_ms = ANNOUNCE_INTERVAL * 1000; + } + break; + case RATE_LIMIT_PROBE: + /* timeouts in the RATE_LIMIT_PROBE state mean no conflicting + ARP packets have been received, so we can move immediately + to the announce state */ + state = ANNOUNCE; + nclaims = 0; + debug("announce/%u %s@%pI4\n", + nclaims, eth_get_name(), &ip); + ArpRawRequest(ip, NetOurEther, ip); + timeout_ms = ANNOUNCE_INTERVAL * 1000; + break; + case ANNOUNCE: + /* timeouts in the ANNOUNCE state mean no conflicting ARP + packets have been received, so we can progress through + the states */ + if (nclaims < ANNOUNCE_NUM) { + nclaims++; + debug("announce/%u %s@%pI4\n", + nclaims, eth_get_name(), &ip); + ArpRawRequest(ip, NetOurEther, ip); + timeout_ms = ANNOUNCE_INTERVAL * 1000; + } else { + /* Switch to monitor state */ + state = MONITOR; + printf("Successfully assigned %pI4\n", &ip); + NetCopyIP(&NetOurIP, &ip); + ready = 1; + conflicts = 0; + timeout_ms = -1; + /* Never timeout in the monitor state */ + NetSetTimeout(0, NULL); + + /* NOTE: all other exit paths should deconfig ... */ + NetSetState(NETLOOP_SUCCESS); + return; + } + break; + case DEFEND: + /* We won! No ARP replies, so just go back to monitor */ + state = MONITOR; + timeout_ms = -1; + conflicts = 0; + break; + default: + /* Invalid, should never happen. Restart the whole protocol */ + state = PROBE; + ip = pick(); + timeout_ms = 0; + nprobes = 0; + nclaims = 0; + break; + } + configure_wait(); +} + +void LinkLocalReceiveArp(struct ARP_t *arp, int len) +{ + int source_ip_conflict; + int target_ip_conflict; + + if (state == DISABLED) + return; + + /* We need to adjust the timeout in case we didn't receive a + conflicting packet. */ + if (timeout_ms > 0) { + unsigned diff = deadline_ms - MONOTONIC_MS(); + if ((int)(diff) < 0) { + /* Current time is greater than the expected timeout + time. This should never happen */ + debug("missed an expected timeout\n"); + timeout_ms = 0; + } else { + debug("adjusting timeout\n"); + timeout_ms = diff | 1; /* never 0 */ + } + } +/* + * XXX Don't bother with ethernet link just yet + if ((fds[0].revents & POLLIN) == 0) { + if (fds[0].revents & POLLERR) { + // FIXME: links routinely go down; + // this shouldn't necessarily exit. + bb_error_msg("iface %s is down", eth_get_name()); + if (ready) { + run(argv, "deconfig", &ip); + } + return EXIT_FAILURE; + } + continue; + } +*/ + + debug("%s recv arp type=%d, op=%d,\n", + eth_get_name(), ntohs(arp->ar_pro), + ntohs(arp->ar_op)); + debug("\tsource=%pM %pI4\n", + &arp->ar_sha, + &arp->ar_spa); + debug("\ttarget=%pM %pI4\n", + &arp->ar_tha, + &arp->ar_tpa); + + if (arp->ar_op != htons(ARPOP_REQUEST) + && arp->ar_op != htons(ARPOP_REPLY) + ) { + configure_wait(); + return; + } + + source_ip_conflict = 0; + target_ip_conflict = 0; + + if (memcmp(&arp->ar_spa, &ip, ARP_PLEN) == 0 + && memcmp(&arp->ar_sha, NetOurEther, ARP_HLEN) != 0 + ) { + source_ip_conflict = 1; + } + if (arp->ar_op == htons(ARPOP_REQUEST) + && memcmp(&arp->ar_tpa, &ip, ARP_PLEN) == 0 + && memcmp(&arp->ar_tha, NetOurEther, ARP_HLEN) != 0 + ) { + target_ip_conflict = 1; + } + + debug("state = %d, source ip conflict = %d, target ip conflict = %d\n", + state, source_ip_conflict, target_ip_conflict); + switch (state) { + case PROBE: + case ANNOUNCE: + /* When probing or announcing, check for source IP conflicts + and other hosts doing ARP probes (target IP conflicts). */ + if (source_ip_conflict || target_ip_conflict) { + conflicts++; + state = PROBE; + if (conflicts >= MAX_CONFLICTS) { + debug("%s ratelimit\n", eth_get_name()); + timeout_ms = RATE_LIMIT_INTERVAL * 1000; + state = RATE_LIMIT_PROBE; + } + + /* restart the whole protocol */ + ip = pick(); + timeout_ms = 0; + nprobes = 0; + nclaims = 0; + } + break; + case MONITOR: + /* If a conflict, we try to defend with a single ARP probe */ + if (source_ip_conflict) { + debug("monitor conflict -- defending\n"); + state = DEFEND; + timeout_ms = DEFEND_INTERVAL * 1000; + ArpRawRequest(ip, NetOurEther, ip); + } + break; + case DEFEND: + /* Well, we tried. Start over (on conflict) */ + if (source_ip_conflict) { + state = PROBE; + debug("defend conflict -- starting over\n"); + ready = 0; + NetOurIP = 0; + + /* restart the whole protocol */ + ip = pick(); + timeout_ms = 0; + nprobes = 0; + nclaims = 0; + } + break; + default: + /* Invalid, should never happen. Restart the whole protocol */ + debug("invalid state -- starting over\n"); + state = PROBE; + ip = pick(); + timeout_ms = 0; + nprobes = 0; + nclaims = 0; + break; + } + configure_wait(); +} diff --git a/net/link_local.h b/net/link_local.h new file mode 100644 index 0000000..0316321 --- /dev/null +++ b/net/link_local.h @@ -0,0 +1,21 @@ +/* + * RFC3927 ZeroConf IPv4 Link-Local addressing + * (see http://www.zeroconf.org/) + * + * Copied from BusyBox - networking/zcip.c + * + * Copyright (C) 2003 by Arthur van Hoff (avh@strangeberry.com) + * Copyright (C) 2004 by David Brownell + * + * Licensed under the GPL v2 or later + */ + +#ifndef __LINK_LOCAL_H__ +#define __LINK_LOCAL_H__ + +#include <common.h> + +void LinkLocalReceiveArp(struct ARP_t *arp, int len); +void LinkLocalStart(void); + +#endif /* __LINK_LOCAL_H__ */ diff --git a/net/net.c b/net/net.c index fe4e528..d541234 100644 --- a/net/net.c +++ b/net/net.c @@ -23,6 +23,12 @@ * - name of bootfile * Next step: ARP * + * LINK_LOCAL: + * + * Prerequisites: - own ethernet address + * We want: - own IP address + * Next step: ARP + * * RARP: * * Prerequisites: - own ethernet address @@ -90,6 +96,9 @@ #if defined(CONFIG_CMD_DNS) #include "dns.h" #endif +#if defined(CONFIG_CMD_LINK_LOCAL) +#include "link_local.h" +#endif #include "nfs.h" #if defined(CONFIG_CMD_PING) #include "ping.h" @@ -404,6 +413,11 @@ restart: DnsStart(); break; #endif +#if defined(CONFIG_CMD_LINK_LOCAL) + case LINKLOCAL: + LinkLocalStart(); + break; +#endif default: break; } @@ -1200,6 +1214,7 @@ common: case BOOTP: case CDP: case DHCP: + case LINKLOCAL: if (memcmp(NetOurEther, "\0\0\0\0\0\0", 6) == 0) { int num = eth_get_dev_index();

On Thursday 19 January 2012 19:53:29 Joe Hershberger wrote:
--- a/README +++ b/README
CONFIG_CMD_PCMCIA * PCMCIA support
CONFIG_CMD_PCMCIA * PCMCIA support
please omit unrelated whitespace changes
--- a/common/cmd_net.c +++ b/common/cmd_net.c
+int do_link_local(cmd_tbl_t *cmdtp, int flag, int argc, char * const
static
- NetOurGatewayIP = 0;
- ip_to_string(NetOurGatewayIP, tmp);
- setenv("gatewayip", tmp);
- ip_to_string(NetOurSubnetMask, tmp);
- setenv("netmask", tmp);
- ip_to_string(NetOurIP, tmp);
- setenv("ipaddr", tmp);
i feel like we should add a helper to do this setenv_ip("ipaddr", NetOurIP);
--- a/net/Makefile +++ b/net/Makefile
-COBJS := $(COBJS-y) +COBJS := $(sort $(COBJS-y))
why ?
--- a/net/arp.c +++ b/net/arp.c
+#if defined(CONFIG_CMD_LINK_LOCAL) +#include "link_local.h" +#endif
please push the ifdef down into the header -mike

Hi Mike,
On Fri, Feb 3, 2012 at 6:42 AM, Mike Frysinger vapier@gentoo.org wrote:
On Thursday 19 January 2012 19:53:29 Joe Hershberger wrote:
- NetOurGatewayIP = 0;
- ip_to_string(NetOurGatewayIP, tmp);
- setenv("gatewayip", tmp);
- ip_to_string(NetOurSubnetMask, tmp);
- setenv("netmask", tmp);
- ip_to_string(NetOurIP, tmp);
- setenv("ipaddr", tmp);
i feel like we should add a helper to do this setenv_ip("ipaddr", NetOurIP);
I think that's a good idea, but I don't think it should be part of this patch series. It would probably be clearer to do that independently later.
--- a/net/Makefile +++ b/net/Makefile
-COBJS := $(COBJS-y) +COBJS := $(sort $(COBJS-y))
why ?
Sort is needed because there are 2 different config options that could add the net_rand source file and sort will eliminate duplicates.
-Joe

On Monday 13 February 2012 15:21:12 Joe Hershberger wrote:
On Fri, Feb 3, 2012 at 6:42 AM, Mike Frysinger wrote:
On Thursday 19 January 2012 19:53:29 Joe Hershberger wrote:
NetOurGatewayIP = 0;
ip_to_string(NetOurGatewayIP, tmp);
setenv("gatewayip", tmp);
ip_to_string(NetOurSubnetMask, tmp);
setenv("netmask", tmp);
ip_to_string(NetOurIP, tmp);
setenv("ipaddr", tmp);
i feel like we should add a helper to do this setenv_ip("ipaddr", NetOurIP);
I think that's a good idea, but I don't think it should be part of this patch series. It would probably be clearer to do that independently later.
that's fine
--- a/net/Makefile +++ b/net/Makefile
-COBJS := $(COBJS-y) +COBJS := $(sort $(COBJS-y))
why ?
Sort is needed because there are 2 different config options that could add the net_rand source file and sort will eliminate duplicates.
i'd say "needs a comment", but it seems we already crossed that threshold in other Makefiles. should probably make this the standard moving forward so people can copy & paste & not ask again. -mike

Cisco's arp-proxy feature fails to ignore the link-local address range This means that a link-local device on a network with this Cisco equipment will reply to ARP requests for our device (in addition to our reply) If we happen to reply first, the requester's ARP table will be populated with our MAC address, and one packet will be sent to us... shortly following this, the requester will get an ARP reply from the Cisco equipment telling the requester to send packets their way instead of to our device from now on This work-around detects this link-local condition and will delay replying to the ARP request for 5ms so that the first packet is sent to the Cisco equipment and all following packets are sent to our device
Signed-off-by: Joe Hershberger joe.hershberger@ni.com Cc: Joe Hershberger joe.hershberger@gmail.com Cc: Wolfgang Denk wd@denx.de --- net/arp.c | 13 +++++++++++++ 1 files changed, 13 insertions(+), 0 deletions(-)
diff --git a/net/arp.c b/net/arp.c index 9eb4ab0..8a7b4a2 100644 --- a/net/arp.c +++ b/net/arp.c @@ -172,6 +172,19 @@ void ArpReceive(struct Ethernet_t *et, struct IP_UDP_t *ip, int len) NetCopyIP(&arp->ar_tpa, &arp->ar_spa); memcpy(&arp->ar_sha, NetOurEther, ARP_HLEN); NetCopyIP(&arp->ar_spa, &NetOurIP); + + /* + * Work-around for brain-damaged Cisco equipment with + * arp-proxy enabled. + * + * If the requesting IP is not on our subnet, wait 5ms to + * reply to ARP request so that our reply will overwrite + * the arp-proxy's instead of the other way around. + */ + if ((NetReadIP(&arp->ar_tpa) & NetOurSubnetMask) != + (NetReadIP(&arp->ar_spa) & NetOurSubnetMask)) + udelay(5000); + NetSendPacket((uchar *)et, eth_hdr_size + ARP_HDR_SIZE); return;

On Thursday 19 January 2012 19:53:30 Joe Hershberger wrote:
Cisco's arp-proxy feature fails to ignore the link-local address range This means that a link-local device on a network with this Cisco equipment will reply to ARP requests for our device (in addition to our reply) ... --- a/net/arp.c +++ b/net/arp.c
/*
* Work-around for brain-damaged Cisco equipment with
* arp-proxy enabled.
*
* If the requesting IP is not on our subnet, wait 5ms to
* reply to ARP request so that our reply will overwrite
* the arp-proxy's instead of the other way around.
*/
if ((NetReadIP(&arp->ar_tpa) & NetOurSubnetMask) !=
(NetReadIP(&arp->ar_spa) & NetOurSubnetMask))
udelay(5000);
shouldn't this sit behind a link local ifdef then ? -mike

Signed-off-by: Joe Hershberger joe.hershberger@ni.com Cc: Joe Hershberger joe.hershberger@gmail.com Cc: Wolfgang Denk wd@denx.de --- drivers/net/netconsole.c | 6 ++++++ net/arp.c | 12 ++++++++---- net/link_local.c | 31 +++++++++++++++++++------------ net/net.c | 45 ++++++++++++++++++++++++++++++++++----------- net/ping.c | 6 ++++-- net/rarp.c | 7 +++++-- 6 files changed, 76 insertions(+), 31 deletions(-)
diff --git a/drivers/net/netconsole.c b/drivers/net/netconsole.c index 1e31159..aee671e 100644 --- a/drivers/net/netconsole.c +++ b/drivers/net/netconsole.c @@ -26,6 +26,8 @@ #include <stdio_dev.h> #include <net.h>
+#define DEBUG_DEV_PKT 0 + DECLARE_GLOBAL_DATA_PTR;
static char input_buffer[512]; @@ -83,6 +85,8 @@ int nc_input_packet(uchar *pkt, unsigned dest, unsigned src, unsigned len) if (dest != nc_port || !len) return 0; /* not for us */
+ debug_cond(DEBUG_DEV_PKT, "input: "%*.*s"\n", len, len, pkt); + if (input_size == sizeof(input_buffer)) return 1; /* no space */ if (len > sizeof(input_buffer) - input_size) @@ -112,6 +116,8 @@ static void nc_send_packet(const char *buf, int len) uchar *ether; IPaddr_t ip;
+ debug_cond(DEBUG_DEV_PKT, "output: "%*.*s"\n", len, len, buf); + eth = eth_get_dev(); if (eth == NULL) return; diff --git a/net/arp.c b/net/arp.c index 8a7b4a2..b8fb21f 100644 --- a/net/arp.c +++ b/net/arp.c @@ -15,6 +15,9 @@ #include "link_local.h" #endif
+#define DEBUG_DEV_PKT 0 +#define DEBUG_NET_PKT 0 + #ifndef CONFIG_ARP_TIMEOUT /* Milliseconds before trying ARP again */ # define ARP_TIMEOUT 5000UL @@ -58,7 +61,7 @@ void ArpRawRequest(IPaddr_t sourceIP, const uchar *targetEther, struct ARP_t *arp; int eth_hdr_size;
- debug("ARP broadcast %d\n", NetArpWaitTry); + debug_cond(DEBUG_DEV_PKT, "ARP broadcast %d\n", NetArpWaitTry);
pkt = NetArpTxPacket;
@@ -138,7 +141,7 @@ void ArpReceive(struct Ethernet_t *et, struct IP_UDP_t *ip, int len) * address; so if we receive such a packet, we set * the server ethernet address */ - debug("Got ARP\n"); + debug_cond(DEBUG_NET_PKT, "Got ARP\n");
arp = (struct ARP_t *)ip; if (len < ARP_HDR_SIZE) { @@ -163,7 +166,7 @@ void ArpReceive(struct Ethernet_t *et, struct IP_UDP_t *ip, int len) switch (ntohs(arp->ar_op)) { case ARPOP_REQUEST: /* reply with our IP address */ - debug("Got ARP REQUEST, return our IP\n"); + debug_cond(DEBUG_DEV_PKT, "Got ARP REQUEST, return our IP\n"); pkt = (uchar *)et; eth_hdr_size = NetUpdateEther(et, et->et_src, PROT_ARP); pkt += eth_hdr_size; @@ -205,7 +208,8 @@ void ArpReceive(struct Ethernet_t *et, struct IP_UDP_t *ip, int len)
/* matched waiting packet's address */ if (reply_ip_addr == NetArpWaitReplyIP) { - debug("Got ARP REPLY, set eth addr (%pM)\n", + debug_cond(DEBUG_DEV_PKT, + "Got ARP REPLY, set eth addr (%pM)\n", arp->ar_data);
/* save address for later use */ diff --git a/net/link_local.c b/net/link_local.c index 16ec9a7..5395bf6 100644 --- a/net/link_local.c +++ b/net/link_local.c @@ -16,6 +16,11 @@ #include "arp.h" #include "net_rand.h"
+#define DEBUG_LL_STATE 0 +#define DEBUG_DEV_PKT 0 +#define DEBUG_NET_PKT 0 +#define DEBUG_INT_STATE 0 + /* We don't need more than 32 bits of the counter */ #define MONOTONIC_MS() ((unsigned)get_timer(0) * (1000 / CONFIG_SYS_HZ))
@@ -93,7 +98,7 @@ static void configure_wait(void) /* set deadline_ms to the point in time when we timeout */ deadline_ms = MONOTONIC_MS() + timeout_ms;
- debug("...wait %d %s nprobes=%u, nclaims=%u\n", + debug_cond(DEBUG_DEV_PKT, "...wait %d %s nprobes=%u, nclaims=%u\n", timeout_ms, eth_get_name(), nprobes, nclaims);
NetSetTimeout(timeout_ms, LinkLocalTimeout); @@ -132,7 +137,7 @@ LinkLocalTimeout(void) have been received, so we can progress through the states */ if (nprobes < PROBE_NUM) { nprobes++; - debug("probe/%u %s@%pI4\n", + debug_cond(DEBUG_LL_STATE, "probe/%u %s@%pI4\n", nprobes, eth_get_name(), &ip); ArpRawRequest(0, NetEtherNullAddr, ip); timeout_ms = PROBE_MIN * 1000; @@ -141,7 +146,7 @@ LinkLocalTimeout(void) /* Switch to announce state */ state = ANNOUNCE; nclaims = 0; - debug("announce/%u %s@%pI4\n", + debug_cond(DEBUG_LL_STATE, "announce/%u %s@%pI4\n", nclaims, eth_get_name(), &ip); ArpRawRequest(ip, NetOurEther, ip); timeout_ms = ANNOUNCE_INTERVAL * 1000; @@ -153,7 +158,7 @@ LinkLocalTimeout(void) to the announce state */ state = ANNOUNCE; nclaims = 0; - debug("announce/%u %s@%pI4\n", + debug_cond(DEBUG_LL_STATE, "announce/%u %s@%pI4\n", nclaims, eth_get_name(), &ip); ArpRawRequest(ip, NetOurEther, ip); timeout_ms = ANNOUNCE_INTERVAL * 1000; @@ -164,7 +169,7 @@ LinkLocalTimeout(void) the states */ if (nclaims < ANNOUNCE_NUM) { nclaims++; - debug("announce/%u %s@%pI4\n", + debug_cond(DEBUG_LL_STATE, "announce/%u %s@%pI4\n", nclaims, eth_get_name(), &ip); ArpRawRequest(ip, NetOurEther, ip); timeout_ms = ANNOUNCE_INTERVAL * 1000; @@ -217,10 +222,11 @@ void LinkLocalReceiveArp(struct ARP_t *arp, int len) if ((int)(diff) < 0) { /* Current time is greater than the expected timeout time. This should never happen */ - debug("missed an expected timeout\n"); + debug_cond(DEBUG_LL_STATE, + "missed an expected timeout\n"); timeout_ms = 0; } else { - debug("adjusting timeout\n"); + debug_cond(DEBUG_INT_STATE, "adjusting timeout\n"); timeout_ms = diff | 1; /* never 0 */ } } @@ -240,13 +246,13 @@ void LinkLocalReceiveArp(struct ARP_t *arp, int len) } */
- debug("%s recv arp type=%d, op=%d,\n", + debug_cond(DEBUG_INT_STATE, "%s recv arp type=%d, op=%d,\n", eth_get_name(), ntohs(arp->ar_pro), ntohs(arp->ar_op)); - debug("\tsource=%pM %pI4\n", + debug_cond(DEBUG_INT_STATE, "\tsource=%pM %pI4\n", &arp->ar_sha, &arp->ar_spa); - debug("\ttarget=%pM %pI4\n", + debug_cond(DEBUG_INT_STATE, "\ttarget=%pM %pI4\n", &arp->ar_tha, &arp->ar_tpa);
@@ -272,8 +278,9 @@ void LinkLocalReceiveArp(struct ARP_t *arp, int len) target_ip_conflict = 1; }
- debug("state = %d, source ip conflict = %d, target ip conflict = %d\n", - state, source_ip_conflict, target_ip_conflict); + debug_cond(DEBUG_NET_PKT, + "state = %d, source ip conflict = %d, target ip conflict = " + "%d\n", state, source_ip_conflict, target_ip_conflict); switch (state) { case PROBE: case ANNOUNCE: diff --git a/net/net.c b/net/net.c index d541234..2c1e60e 100644 --- a/net/net.c +++ b/net/net.c @@ -111,6 +111,10 @@ #endif #include "tftp.h"
+#define DEBUG_DEV_PKT 0 +#define DEBUG_NET_PKT 0 +#define DEBUG_INT_STATE 0 + DECLARE_GLOBAL_DATA_PTR;
/** BOOTP EXTENTIONS **/ @@ -315,6 +319,7 @@ int NetLoop(enum proto_t protocol) NetRestarted = 0; NetDevExists = 0; NetTryCount = 1; + debug_cond(DEBUG_INT_STATE, "--- NetLoop Entry\n");
NetInit(); eth_halt(); @@ -334,6 +339,7 @@ restart: * here on, this code is a state machine driven by received * packets and timer events. */ + debug_cond(DEBUG_INT_STATE, "--- NetLoop Init\n"); NetInitLoop();
switch (net_check_prereq(protocol)) { @@ -464,6 +470,9 @@ restart: NetCleanupLoop(); eth_halt(); puts("\nAbort\n"); + /* include a debug print as well incase the debug + messages are directed to stderr */ + debug_cond(DEBUG_INT_STATE, "--- NetLoop Abort!\n"); goto done; }
@@ -491,6 +500,7 @@ restart: } #endif /* CONFIG_SYS_FAULT_ECHO_LINK_DOWN, ... */ #endif /* CONFIG_MII, ... */ + debug_cond(DEBUG_INT_STATE, "--- NetLoop timeout\n"); x = timeHandler; timeHandler = (thand_f *)0; (*x)(); @@ -518,10 +528,12 @@ restart: } eth_halt(); ret = NetBootFileXferSize; + debug_cond(DEBUG_INT_STATE, "--- NetLoop Success!\n"); goto done;
case NETLOOP_FAIL: NetCleanupLoop(); + debug_cond(DEBUG_INT_STATE, "--- NetLoop Fail!\n"); goto done; } } @@ -602,6 +614,7 @@ NetGetUDPHandler(void) void NetSetUDPHandler(rxhand_f *f) { + debug_cond(DEBUG_INT_STATE, "--- NetLoop UDP handler set (%p)\n", f); udpPacketHandler = f; }
@@ -614,6 +627,7 @@ NetGetARPHandler(void) void NetSetARPHandler(rxhand_f *f) { + debug_cond(DEBUG_INT_STATE, "--- NetLoop ARP handler set (%p)\n", f); arpPacketHandler = f; }
@@ -628,8 +642,12 @@ void NetSetTimeout(ulong iv, thand_f *f) { if (iv == 0) { + debug_cond(DEBUG_INT_STATE, + "--- NetLoop timeout handler cancelled\n"); timeHandler = (thand_f *)0; } else { + debug_cond(DEBUG_INT_STATE, + "--- NetLoop timeout handler set (%p)\n", f); timeHandler = f; timeStart = get_timer(0); timeDelta = iv; @@ -640,6 +658,7 @@ NetSetTimeout(ulong iv, thand_f *f) void NetSetState(int state) { + debug_cond(DEBUG_INT_STATE, "--- NetState set to %d\n", state); NetState = state; }
@@ -681,7 +700,7 @@ NetSendUDPPacket(uchar *ether, IPaddr_t dest, int dport, int sport,
/* if MAC address was not discovered yet, do an ARP request */ if (memcmp(ether, NetEtherNullAddr, 6) == 0) { - debug("sending ARP for %pI4\n", &dest); + debug_cond(DEBUG_DEV_PKT, "sending ARP for %pI4\n", &dest);
/* save the ip and eth addr for the packet to send after arp */ NetArpWaitPacketIP = dest; @@ -696,7 +715,8 @@ NetSendUDPPacket(uchar *ether, IPaddr_t dest, int dport, int sport, ArpRequest(); return 1; /* waiting */ } else { - debug("sending UDP to %pI4/%pM\n", &dest, ether); + debug_cond(DEBUG_DEV_PKT, "sending UDP to %pI4/%pM\n", + &dest, ether); NetSendPacket(NetTxPacket, pkt_hdr_size + payload_len); return 0; /* transmitted */ } @@ -919,7 +939,7 @@ NetReceive(volatile uchar *inpkt, int len) #endif ushort cti = 0, vlanid = VLAN_NONE, myvlanid, mynvlanid;
- debug("packet received\n"); + debug_cond(DEBUG_NET_PKT, "packet received\n");
NetRxPacket = (uchar *)inpkt; NetRxPacketLen = len; @@ -950,8 +970,6 @@ NetReceive(volatile uchar *inpkt, int len)
eth_proto = ntohs(et->et_protlen);
- debug("packet received\n"); - if (eth_proto < 1514) { /* * Got a 802.2 packet. Check the other protocol field. @@ -969,7 +987,7 @@ NetReceive(volatile uchar *inpkt, int len) } else { /* VLAN packet */ struct VLAN_Ethernet_t *vet = (struct VLAN_Ethernet_t *)et;
- debug("VLAN packet received\n"); + debug_cond(DEBUG_NET_PKT, "VLAN packet received\n");
/* too small packet? */ if (len < VLAN_ETHER_HDR_SIZE) @@ -991,7 +1009,7 @@ NetReceive(volatile uchar *inpkt, int len) len -= VLAN_ETHER_HDR_SIZE; }
- debug("Receive from protocol 0x%x\n", eth_proto); + debug_cond(DEBUG_NET_PKT, "Receive from protocol 0x%x\n", eth_proto);
#if defined(CONFIG_CMD_CDP) if (iscdp) { @@ -1020,7 +1038,7 @@ NetReceive(volatile uchar *inpkt, int len) break; #endif case PROT_IP: - debug("Got IP\n"); + debug_cond(DEBUG_NET_PKT, "Got IP\n"); /* Before we start poking the header, make sure it is there */ if (len < IP_UDP_HDR_SIZE) { debug("len bad %d < %lu\n", len, @@ -1029,11 +1047,12 @@ NetReceive(volatile uchar *inpkt, int len) } /* Check the packet length */ if (len < ntohs(ip->ip_len)) { - printf("len bad %d < %d\n", len, ntohs(ip->ip_len)); + debug("len bad %d < %d\n", len, ntohs(ip->ip_len)); return; } len = ntohs(ip->ip_len); - debug("len=%d, v=%02x\n", len, ip->ip_hl_v & 0xff); + debug_cond(DEBUG_NET_PKT, "len=%d, v=%02x\n", + len, ip->ip_hl_v & 0xff);
/* Can't deal with anything except IPv4 */ if ((ip->ip_hl_v & 0xf0) != 0x40) @@ -1043,7 +1062,7 @@ NetReceive(volatile uchar *inpkt, int len) return; /* Check the Checksum of the header */ if (!NetCksumOk((uchar *)ip, IP_HDR_SIZE / 2)) { - puts("checksum bad\n"); + debug("checksum bad\n"); return; } /* If it is not for us, ignore it */ @@ -1092,6 +1111,10 @@ NetReceive(volatile uchar *inpkt, int len) return; }
+ debug_cond(DEBUG_DEV_PKT, + "received UDP (to=%pI4, from=%pI4, len=%d)\n", + &dst_ip, &src_ip, len); + #ifdef CONFIG_UDP_CHECKSUM if (ip->udp_xsum != 0) { ulong xsum; diff --git a/net/ping.c b/net/ping.c index 8332c88..a4fb7b7 100644 --- a/net/ping.c +++ b/net/ping.c @@ -11,6 +11,8 @@ #include "ping.h" #include "arp.h"
+#define DEBUG_DEV_PKT 0 + static ushort PingSeqNo;
/* The ip address to ping */ @@ -45,7 +47,7 @@ static int PingSend(void)
/* XXX always send arp request */
- debug("sending ARP for %pI4\n", &NetPingIP); + debug_cond(DEBUG_DEV_PKT, "sending ARP for %pI4\n", &NetPingIP);
NetArpWaitPacketIP = NetPingIP;
@@ -95,7 +97,7 @@ void PingReceive(struct Ethernet_t *et, struct IP_UDP_t *ip, int len) case ICMP_ECHO_REQUEST: eth_hdr_size = NetUpdateEther(et, et->et_src, PROT_IP);
- debug("Got ICMP ECHO REQUEST, return " + debug_cond(DEBUG_DEV_PKT, "Got ICMP ECHO REQUEST, return " "%d bytes\n", eth_hdr_size + len);
ip->ip_sum = 0; diff --git a/net/rarp.c b/net/rarp.c index 3097341..edc33ca 100644 --- a/net/rarp.c +++ b/net/rarp.c @@ -29,6 +29,9 @@ #include "rarp.h" #include "tftp.h"
+#define DEBUG_DEV_PKT 0 +#define DEBUG_NET_PKT 0 + #define TIMEOUT 5000UL /* Milliseconds before trying BOOTP again */ #ifndef CONFIG_NET_RETRY_COUNT # define TIMEOUT_COUNT 5 /* # of timeouts before giving up */ @@ -46,7 +49,7 @@ RarpReceive(struct IP_UDP_t *ip, unsigned len) { struct ARP_t *arp;
- debug("Got RARP\n"); + debug_cond(DEBUG_NET_PKT, "Got RARP\n"); arp = (struct ARP_t *)ip; if (len < ARP_HDR_SIZE) { printf("bad length %d < %d\n", len, ARP_HDR_SIZE); @@ -64,7 +67,7 @@ RarpReceive(struct IP_UDP_t *ip, unsigned len) if (NetServerIP == 0) NetCopyIP(&NetServerIP, &arp->ar_data[6]); memcpy(NetServerEther, &arp->ar_data[0], 6); - debug("Got good RARP\n"); + debug_cond(DEBUG_DEV_PKT, "Got good RARP\n"); net_auto_load(); } }

On Thursday 19 January 2012 19:53:31 Joe Hershberger wrote:
--- a/drivers/net/netconsole.c +++ b/drivers/net/netconsole.c
+#define DEBUG_DEV_PKT 0
wouldn't be better to move these to net.h by default rather than copying & pasting to all the source files ? -mike

On 20/01/2012 01:53, Joe Hershberger wrote:
Link-local support allows the bootloader to get a usable network address regardless of network environment. Given that bootloaders may not always have access to a DHCP server, this functionality makes accessing it in those environments easier. It is also referred to as zero-configuration IP.
Hi Joe,
I have a basic question about your patchset. RFC3927 requires that addresses are continuosly checked to avoid conflicts - this is a strict requirement.
However, it is clear we cannot comply with this requirement. The protocol runs once and there is not a background process to check for conflicts.
I can imagine we can get conflicts, when several targets are running at the same time in u_boot and they are attached to a network where local-link addressing is active. Then we can have two or more system using the same ip address, making the network unreliable.
Which are the limitations of this implementation ? They should also described in your patchset, maybe with a README file. Is is thought to connect the target directly to a PC ?
Best regards, Stefano Babic

Hi Stefano,
On Wed, Jan 25, 2012 at 4:27 AM, Stefano Babic sbabic@denx.de wrote:
On 20/01/2012 01:53, Joe Hershberger wrote:
Link-local support allows the bootloader to get a usable network address regardless of network environment. Given that bootloaders may not always have access to a DHCP server, this functionality makes accessing it in those environments easier. It is also referred to as zero-configuration IP.
Hi Joe,
I have a basic question about your patchset. RFC3927 requires that addresses are continuosly checked to avoid conflicts - this is a strict requirement.
True. It cannot be 100% compliant in every use-case.
However, it is clear we cannot comply with this requirement. The protocol runs once and there is not a background process to check for conflicts.
Any time we are processing NetLoop, we will handle conflict correctly. In the case of actually doing something on the network, (generally happens at least some of the time the device is on the network) conflicts are monitored for. In the use case for the devices I use this on, I also run NetConsole, which means the NetLoop is always being run. In that case, it is compliant with the RFC.
I can imagine we can get conflicts, when several targets are running at the same time in u_boot and they are attached to a network where local-link addressing is active. Then we can have two or more system using the same ip address, making the network unreliable.
That is true. I believe there are some cases you could contrive that could allow a device to pick the same address as another device, but it is quite unlikely. Initial address selections are based on MAC-seeded random number, making it hard to get a conflicting address in general. It would have to be a pretty congested 169.254.0.0 network. In addition, any time both devices run NetLoop at the same time as each other, the conflict will be detected and handled.
Which are the limitations of this implementation ? They should also described in your patchset, maybe with a README file. Is is thought to connect the target directly to a PC ?
The most likely use-case is connecting directly to a PC, but it also works on any generic network. I added documentation to the README. I can add a note about the potential for non-compliance if NetLoop is not run.
Best regards, -Joe

On 26/01/2012 00:40, Joe Hershberger wrote:
I have a basic question about your patchset. RFC3927 requires that addresses are continuosly checked to avoid conflicts - this is a strict requirement.
True. It cannot be 100% compliant in every use-case.
It is important that these limitations are well documented
Any time we are processing NetLoop, we will handle conflict correctly.
Yes, but NetLoop is running only when there some activity on the network.
In the case of actually doing something on the network, (generally happens at least some of the time the device is on the network) conflicts are monitored for. In the use case for the devices I use this on, I also run NetConsole, which means the NetLoop is always being run. In that case, it is compliant with the RFC.
Right - I think this is the best case.
I can imagine we can get conflicts, when several targets are running at the same time in u_boot and they are attached to a network where local-link addressing is active. Then we can have two or more system using the same ip address, making the network unreliable.
That is true. I believe there are some cases you could contrive that could allow a device to pick the same address as another device, but it is quite unlikely. Initial address selections are based on MAC-seeded random number, making it hard to get a conflicting address in general.
It depends on the number of devices attached to the network - in a small environment, it is not probable. Anyway, if we document well how this feature should be used and which are the limitations, anybody is warned about its usage and about the best conditions to run the feature.
Which are the limitations of this implementation ? They should also described in your patchset, maybe with a README file. Is is thought to connect the target directly to a PC ?
The most likely use-case is connecting directly to a PC,
I have supposed also this is the best use-case.
but it also works on any generic network. I added documentation to the README.
Ok - please add a separate file doc/README.link-local (or whatever filename you want)
Best regards, Stefano Babic

On Thursday 26 January 2012 04:21:21 Stefano Babic wrote:
On 26/01/2012 00:40, Joe Hershberger wrote:
I have a basic question about your patchset. RFC3927 requires that addresses are continuosly checked to avoid conflicts - this is a strict requirement.
True. It cannot be 100% compliant in every use-case.
It is important that these limitations are well documented
Any time we are processing NetLoop, we will handle conflict correctly.
Yes, but NetLoop is running only when there some activity on the network.
isn't that the only time that matters ? if we're sitting at the command prompt, then u-boot has the network pretty much shut off (doesn't respond to ARP let alone anything higher). as soon as we try to do network communication, we are compliant again. -mike

On 03/02/2012 12:34, Mike Frysinger wrote:
On Thursday 26 January 2012 04:21:21 Stefano Babic wrote:
On 26/01/2012 00:40, Joe Hershberger wrote:
I have a basic question about your patchset. RFC3927 requires that addresses are continuosly checked to avoid conflicts - this is a strict requirement.
True. It cannot be 100% compliant in every use-case.
It is important that these limitations are well documented
Any time we are processing NetLoop, we will handle conflict correctly.
Yes, but NetLoop is running only when there some activity on the network.
isn't that the only time that matters ? if we're sitting at the command prompt, then u-boot has the network pretty much shut off (doesn't respond to ARP let alone anything higher).
This is true, but what does happen with a second / multiple boards on the network ? Some of them can steal the same ip address because our board does not answer and conflicts are not solved. At least, I am expecting that some ip address suddenly changes and that can be some inconsistencies in ARP tables.
Stefano

On Friday 03 February 2012 11:19:22 Stefano Babic wrote:
On 03/02/2012 12:34, Mike Frysinger wrote:
On Thursday 26 January 2012 04:21:21 Stefano Babic wrote:
On 26/01/2012 00:40, Joe Hershberger wrote:
I have a basic question about your patchset. RFC3927 requires that addresses are continuosly checked to avoid conflicts - this is a strict requirement.
True. It cannot be 100% compliant in every use-case.
It is important that these limitations are well documented
Any time we are processing NetLoop, we will handle conflict correctly.
Yes, but NetLoop is running only when there some activity on the network.
isn't that the only time that matters ? if we're sitting at the command prompt, then u-boot has the network pretty much shut off (doesn't respond to ARP let alone anything higher).
This is true, but what does happen with a second / multiple boards on the network ? Some of them can steal the same ip address because our board does not answer and conflicts are not solved. At least, I am expecting that some ip address suddenly changes and that can be some inconsistencies in ARP tables.
as soon as the conflict occurs, wouldn't that get detected then ? u-boot not actively responding to a few requests isn't really all that different from the packets getting dropped by say a busy hub. -mike

This patch-series addresses checkpatch compliance issues throughout the networking stack.
One particularly intrusive change is the removal of "volatile" from the Ethernet driver API. All network drivers need to be updated to not use volatile pointers when calling into the network stack.
This is a portion of the changes split from the series: "Add link-local addressing support"
Joe Hershberger (10): net: Remove volatile from net API net: cosmetic: net.h checkpatch compliance net: cosmetic: net.c checkpatch compliance net: cosmetic: eth.c checkpatch compliance net: cosmetic: net.c checkpatch compliance net: cosmetic: nfs.* checkpatch compliance net: cosmetic: rarp.* checkpatch compliance net: cosmetic: sntp.* checkpatch compliance net: cosmetic: tftp.* checkpatch compliance net: cosmetic: netconsole.c checkpatch compliance
common/main.c | 1 - drivers/net/netconsole.c | 122 +++++++++-------- include/common.h | 4 + include/net.h | 144 +++++++++++--------- net/bootp.c | 344 +++++++++++++++++++++++----------------------- net/bootp.h | 56 ++++---- net/eth.c | 79 +++++------ net/net.c | 56 ++++---- net/nfs.c | 300 ++++++++++++++++++++-------------------- net/nfs.h | 2 +- net/rarp.c | 30 ++-- net/rarp.h | 4 +- net/sntp.c | 31 +++-- net/sntp.h | 2 +- net/tftp.c | 36 +++--- net/tftp.h | 2 +- 16 files changed, 614 insertions(+), 599 deletions(-)

This patch-series addresses checkpatch compliance issues throughout the networking stack.
One particularly intrusive change is the removal of "volatile" from the Ethernet driver API. All network drivers need to be updated to not use volatile pointers when calling into the network stack.
This is a portion of the changes split from the series: "Add link-local addressing support"
Joe Hershberger (10): net: Remove volatile from net API net: cosmetic: net.h checkpatch compliance net: cosmetic: bootp.* checkpatch compliance net: cosmetic: eth.c checkpatch compliance net: cosmetic: net.c checkpatch compliance net: cosmetic: nfs.* checkpatch compliance net: cosmetic: rarp.* checkpatch compliance net: cosmetic: sntp.* checkpatch compliance net: cosmetic: tftp.* checkpatch compliance net: cosmetic: netconsole.c checkpatch compliance
common/main.c | 1 - drivers/net/netconsole.c | 122 +++++++++-------- include/common.h | 4 + include/net.h | 146 +++++++++++--------- net/bootp.c | 344 +++++++++++++++++++++++----------------------- net/bootp.h | 56 ++++---- net/eth.c | 79 +++++------ net/net.c | 56 ++++---- net/nfs.c | 300 ++++++++++++++++++++-------------------- net/nfs.h | 2 +- net/rarp.c | 30 ++-- net/rarp.h | 4 +- net/sntp.c | 31 +++-- net/sntp.h | 2 +- net/tftp.c | 36 +++--- net/tftp.h | 2 +- 16 files changed, 616 insertions(+), 599 deletions(-)

Signed-off-by: Joe Hershberger joe.hershberger@ni.com Cc: Joe Hershberger joe.hershberger@gmail.com --- Changes for v2: - Remove volatile from eth driver API - Not using global NetRxPacket (since not casting away volatile) Changes for v3:
include/net.h | 25 +++++++++++++------------ net/bootp.c | 4 ++-- net/eth.c | 12 +++++------- net/net.c | 34 +++++++++++++++++----------------- net/rarp.c | 2 +- net/tftp.c | 6 +++--- 6 files changed, 41 insertions(+), 42 deletions(-)
diff --git a/include/net.h b/include/net.h index ee11f82..7440182 100644 --- a/include/net.h +++ b/include/net.h @@ -81,7 +81,7 @@ struct eth_device { int state;
int (*init) (struct eth_device*, bd_t*); - int (*send) (struct eth_device*, volatile void* packet, int length); + int (*send) (struct eth_device*, void *packet, int length); int (*recv) (struct eth_device*); void (*halt) (struct eth_device*); #ifdef CONFIG_MCAST_TFTP @@ -120,10 +120,11 @@ extern int eth_getenv_enetaddr_by_index(const char *base_name, int index,
extern int usb_eth_initialize(bd_t *bi); extern int eth_init(bd_t *bis); /* Initialize the device */ -extern int eth_send(volatile void *packet, int length); /* Send a packet */ +extern int eth_send(void *packet, int length); /* Send a packet */
#ifdef CONFIG_API -extern int eth_receive(volatile void *packet, int length); /* Receive a packet*/ +extern int eth_receive(void *packet, int length); /* Receive a packet*/ +extern void (*push_packet)(void *packet, int length); #endif extern int eth_rx(void); /* Check for received packets */ extern void eth_halt(void); /* stop SCC */ @@ -342,9 +343,9 @@ extern uchar NetOurEther[6]; /* Our ethernet address */ extern uchar NetServerEther[6]; /* Boot server enet address */ extern IPaddr_t NetOurIP; /* Our IP addr (0 = unknown) */ extern IPaddr_t NetServerIP; /* Server IP addr (0 = unknown) */ -extern volatile uchar * NetTxPacket; /* THE transmit packet */ -extern volatile uchar * NetRxPackets[PKTBUFSRX];/* Receive packets */ -extern volatile uchar * NetRxPacket; /* Current receive packet */ +extern uchar *NetTxPacket; /* THE transmit packet */ +extern uchar *NetRxPackets[PKTBUFSRX];/* Receive packets */ +extern uchar *NetRxPacket; /* Current receive packet */ extern int NetRxPacketLen; /* Current rx packet length */ extern unsigned NetIPID; /* IP ID (counting) */ extern uchar NetBcastAddr[6]; /* Ethernet boardcast address */ @@ -408,10 +409,10 @@ extern void NetStartAgain(void); extern int NetEthHdrSize(void);
/* Set ethernet header; returns the size of the header */ -extern int NetSetEther(volatile uchar *, uchar *, uint); +extern int NetSetEther(uchar *, uchar *, uint);
/* Set IP header */ -extern void NetSetIP(volatile uchar *, IPaddr_t, int, int, int); +extern void NetSetIP(uchar *, IPaddr_t, int, int, int);
/* Checksum */ extern int NetCksumOk(uchar *, int); /* Return true if cksum OK */ @@ -423,13 +424,13 @@ extern void net_set_icmp_handler(rxhand_icmp_f *f); /* Set ICMP RX handler */ extern void NetSetTimeout(ulong, thand_f *);/* Set timeout handler */
/* Transmit "NetTxPacket" */ -extern void NetSendPacket(volatile uchar *, int); +extern void NetSendPacket(uchar *, int);
/* Transmit UDP packet, performing ARP request if needed */ extern int NetSendUDPPacket(uchar *ether, IPaddr_t dest, int dport, int sport, int len);
/* Processes a received packet */ -extern void NetReceive(volatile uchar *, int); +extern void NetReceive(uchar *, int);
/* * Check if autoload is enabled. If so, use either NFS or TFTP to download @@ -445,7 +446,7 @@ void net_auto_load(void); * footprint in our tests. */ /* return IP *in network byteorder* */ -static inline IPaddr_t NetReadIP(volatile void *from) +static inline IPaddr_t NetReadIP(void *from) { IPaddr_t ip; memcpy((void*)&ip, (void*)from, sizeof(ip)); @@ -467,7 +468,7 @@ static inline void NetWriteIP(void *to, IPaddr_t ip) }
/* copy IP */ -static inline void NetCopyIP(volatile void *to, void *from) +static inline void NetCopyIP(void *to, void *from) { memcpy((void*)to, from, sizeof(IPaddr_t)); } diff --git a/net/bootp.c b/net/bootp.c index 9e32476..35654b4 100644 --- a/net/bootp.c +++ b/net/bootp.c @@ -586,7 +586,7 @@ static int BootpExtended (u8 * e) void BootpRequest (void) { - volatile uchar *pkt, *iphdr; + uchar *pkt, *iphdr; Bootp_t *bp; int ext_len, pktlen, iplen;
@@ -839,7 +839,7 @@ static int DhcpMessageType(unsigned char *popt)
static void DhcpSendRequestPkt(Bootp_t *bp_offer) { - volatile uchar *pkt, *iphdr; + uchar *pkt, *iphdr; Bootp_t *bp; int pktlen, iplen, extlen; IPaddr_t OfferedIP; diff --git a/net/eth.c b/net/eth.c index 3eeb908..66a84d4 100644 --- a/net/eth.c +++ b/net/eth.c @@ -82,8 +82,6 @@ int cpu_eth_init(bd_t *bis) __attribute__((weak, alias("__def_eth_init"))); int board_eth_init(bd_t *bis) __attribute__((weak, alias("__def_eth_init")));
#ifdef CONFIG_API -extern void (*push_packet)(volatile void *, int); - static struct { uchar data[PKTSIZE]; int length; @@ -407,7 +405,7 @@ void eth_halt(void) eth_current->state = ETH_STATE_PASSIVE; }
-int eth_send(volatile void *packet, int length) +int eth_send(void *packet, int length) { if (!eth_current) return -1; @@ -424,9 +422,9 @@ int eth_rx(void) }
#ifdef CONFIG_API -static void eth_save_packet(volatile void *packet, int length) +static void eth_save_packet(void *packet, int length) { - volatile char *p = packet; + char *p = packet; int i;
if ((eth_rcv_last+1) % PKTBUFSRX == eth_rcv_current) @@ -442,9 +440,9 @@ static void eth_save_packet(volatile void *packet, int length) eth_rcv_last = (eth_rcv_last + 1) % PKTBUFSRX; }
-int eth_receive(volatile void *packet, int length) +int eth_receive(void *packet, int length) { - volatile char *p = packet; + char *p = packet; void *pp = push_packet; int i;
diff --git a/net/net.c b/net/net.c index c5acf8f..9a0417d 100644 --- a/net/net.c +++ b/net/net.c @@ -152,7 +152,7 @@ IPaddr_t NetOurIP; /* Server IP addr (0 = unknown) */ IPaddr_t NetServerIP; /* Current receive packet */ -volatile uchar *NetRxPacket; +uchar *NetRxPacket; /* Current rx packet length */ int NetRxPacketLen; /* IP packet ID */ @@ -161,7 +161,7 @@ unsigned NetIPID; uchar NetBcastAddr[6] = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff }; uchar NetEtherNullAddr[6]; #ifdef CONFIG_API -void (*push_packet)(volatile void *, int len) = 0; +void (*push_packet)(void *, int len) = 0; #endif #if defined(CONFIG_CMD_CDP) /* Ethernet bcast address */ @@ -208,10 +208,10 @@ void NcStart(void); int nc_input_packet(uchar *pkt, unsigned dest, unsigned src, unsigned len); #endif
-volatile uchar PktBuf[(PKTBUFSRX+1) * PKTSIZE_ALIGN + PKTALIGN]; +uchar PktBuf[(PKTBUFSRX+1) * PKTSIZE_ALIGN + PKTALIGN];
/* Receive packet */ -volatile uchar *NetRxPackets[PKTBUFSRX]; +uchar *NetRxPackets[PKTBUFSRX];
/* Current RX packet handler */ static rxhand_f *packetHandler; @@ -225,7 +225,7 @@ static ulong timeStart; /* Current timeout value */ static ulong timeDelta; /* THE transmit packet */ -volatile uchar *NetTxPacket; +uchar *NetTxPacket;
static int net_check_prereq(enum proto_t protocol);
@@ -246,7 +246,7 @@ int NetArpWaitTry;
void ArpRequest(void) { - volatile uchar *pkt; + uchar *pkt; ARP_t *arp;
debug("ARP broadcast %d\n", NetArpWaitTry); @@ -705,7 +705,7 @@ NetSetTimeout(ulong iv, thand_f *f)
void -NetSendPacket(volatile uchar *pkt, int len) +NetSendPacket(uchar *pkt, int len) { (void) eth_send(pkt, len); } @@ -768,8 +768,8 @@ static ushort PingSeqNo; int PingSend(void) { static uchar mac[6]; - volatile IP_t *ip; - volatile ushort *s; + IP_t *ip; + ushort *s; uchar *pkt;
/* XXX always send arp request */ @@ -784,7 +784,7 @@ int PingSend(void) pkt = NetArpWaitTxPacket; pkt += NetSetEther(pkt, mac, PROT_IP);
- ip = (volatile IP_t *)pkt; + ip = (IP_t *)pkt;
/* * Construct an IP and ICMP header. @@ -936,9 +936,9 @@ static ushort CDP_compute_csum(const uchar *buff, ushort len)
int CDPSendTrigger(void) { - volatile uchar *pkt; - volatile ushort *s; - volatile ushort *cp; + uchar *pkt; + ushort *s; + ushort *cp; Ethernet_t *et; int len; ushort chksum; @@ -965,7 +965,7 @@ int CDPSendTrigger(void) /* CDP header */ *pkt++ = 0x02; /* CDP version 2 */ *pkt++ = 180; /* TTL */ - s = (volatile ushort *)pkt; + s = (ushort *)pkt; cp = s; /* checksum (0 for later calculation) */ *s++ = htons(0); @@ -1439,7 +1439,7 @@ static void receive_icmp(IP_t *ip, int len, IPaddr_t src_ip, Ethernet_t *et) }
void -NetReceive(volatile uchar *inpkt, int len) +NetReceive(uchar *inpkt, int len) { Ethernet_t *et; IP_t *ip; @@ -1918,7 +1918,7 @@ NetEthHdrSize(void) }
int -NetSetEther(volatile uchar *xet, uchar * addr, uint prot) +NetSetEther(uchar *xet, uchar * addr, uint prot) { Ethernet_t *et = (Ethernet_t *)xet; ushort myvlanid; @@ -1943,7 +1943,7 @@ NetSetEther(volatile uchar *xet, uchar * addr, uint prot) }
void -NetSetIP(volatile uchar *xip, IPaddr_t dest, int dport, int sport, int len) +NetSetIP(uchar *xip, IPaddr_t dest, int dport, int sport, int len) { IP_t *ip = (IP_t *)xip;
diff --git a/net/rarp.c b/net/rarp.c index 097f970..77d63e8 100644 --- a/net/rarp.c +++ b/net/rarp.c @@ -71,7 +71,7 @@ void RarpRequest (void) { int i; - volatile uchar *pkt; + uchar *pkt; ARP_t * rarp;
printf("RARP broadcast %d\n", ++RarpTry); diff --git a/net/tftp.c b/net/tftp.c index 7aa3e23..e62f229 100644 --- a/net/tftp.c +++ b/net/tftp.c @@ -310,9 +310,9 @@ static void TftpSend(void) { uchar *pkt; - volatile uchar *xp; - int len = 0; - volatile ushort *s; + uchar *xp; + int len = 0; + ushort *s;
#ifdef CONFIG_MCAST_TFTP /* Multicast TFTP.. non-MasterClients do not ACK data. */

Dear Joe Hershberger,
In message 1337108353-28086-2-git-send-email-joe.hershberger@ni.com you wrote:
Signed-off-by: Joe Hershberger joe.hershberger@ni.com Cc: Joe Hershberger joe.hershberger@gmail.com
Changes for v2:
- Remove volatile from eth driver API
- Not using global NetRxPacket (since not casting away volatile)
Changes for v3:
include/net.h | 25 +++++++++++++------------ net/bootp.c | 4 ++-- net/eth.c | 12 +++++------- net/net.c | 34 +++++++++++++++++----------------- net/rarp.c | 2 +- net/tftp.c | 6 +++--- 6 files changed, 41 insertions(+), 42 deletions(-)
While getting rid of volatile is a good thing in general, I am worried about the test scope of this patch.
This commit is causing tons of compiler warnings because the respective changes have only be done in the header file and the net/ code, but they are missing in _all_ network drivers. I wonder which compile and run time testing has been done with this? I mean, you decided to pull this into mainline - has it been tested at all ?!?
When are you planning to provide fixes for all these warnings? As is, the whole build is spoiled. Or should we revert these patches?
Best regards,
Wolfgang Denk

Hi Wolfgang,
On Mon, May 21, 2012 at 2:05 AM, Wolfgang Denk wd@denx.de wrote:
Dear Joe Hershberger,
In message 1337108353-28086-2-git-send-email-joe.hershberger@ni.com you wrote:
Signed-off-by: Joe Hershberger joe.hershberger@ni.com Cc: Joe Hershberger joe.hershberger@gmail.com
Changes for v2: - Remove volatile from eth driver API - Not using global NetRxPacket (since not casting away volatile) Changes for v3:
include/net.h | 25 +++++++++++++------------ net/bootp.c | 4 ++-- net/eth.c | 12 +++++------- net/net.c | 34 +++++++++++++++++----------------- net/rarp.c | 2 +- net/tftp.c | 6 +++--- 6 files changed, 41 insertions(+), 42 deletions(-)
While getting rid of volatile is a good thing in general, I am worried about the test scope of this patch.
I was too. That's why I asked for guidance:
On Sun, Feb 26, 2012 at 7:51 AM, Wolfgang Denk wd@denx.de wrote:
Dear Joe,
In message CANr=Z=bA9gPv=OALJ2OR_drNM7-KuZn9H4EL7nEsdSMtMyn1Kw@mail.gmail.com you wrote:
On Fri, Feb 3, 2012 at 5:44 AM, Mike Frysinger vapier@gentoo.org wrote:
in general, i like this. my only concern would be the drivers that might break due to incorrect cache management (which the volatile markers happen to work around for them).
having the API accept a volatile but then casting it away puts us in a worse place i think. on one hand, our API is saying "we treat it as volatile" when we really don't, so any drivers that call it with a volatile don't get build warnings. i'd just drop it from all of the net API so that the drivers which do call with a volatile pointer get a build warning -- now the driver maintainer knows they have to at least look at something.
I'm fine with that... it was my first approach, in fact. However, I was concerned that if I submitted a patch that added warnings to every Ethernet-enabled board, that would be frowned upon. I think it is the correct thing to do, but don't have a good feeling for how open people are to changing an interface like that. I also did not want to attempt to revise every net driver in this series.
Wolfgang,
Would you approve of changing the net driver API to not use volatile buffer pointers and have the driver file warn until the driver maintainer addresses the warnings?
I also think this is the right thing to do, and such cleanups sometimes have to spitout warnings for the not-yet-fixed boards. That's unavoidable. Go for it...
Can I get guidance on this from you? I'm about done preparing the changes to the link-local patch-set and would like to know how you prefer that I proceed.
ACK for above.
This is why I went forward with warnings in the mainline for this interface change.
This commit is causing tons of compiler warnings because the respective changes have only be done in the header file and the net/ code, but they are missing in _all_ network drivers. I wonder which compile and run time testing has been done with this? I mean, you decided to pull this into mainline - has it been tested at all ?!?
Yes... I tested this on MPC8313 (powerpc) and AM1808 (arm).
When are you planning to provide fixes for all these warnings? As is, the whole build is spoiled. Or should we revert these patches?
I plan to submit patches for the boards I am able to test, but for reason that Mike suggested, I'm not sure it is wise to fix the warning unless it can be tested on hardware.
Apologies for not making this clear, -Joe

Dear Joe Hershberger,
In message CANr=Z=ZiMqvn2vxp0_kMA76O091TJFu4ck6wVf7re3hEFMOk2g@mail.gmail.com you wrote:
While getting rid of volatile is a good thing in general, I am worried about the test scope of this patch.
I was too. That's why I asked for guidance:
Sorry, this was a misunderstanding then.
My expectation was that you wuld continue this patch series and clean up the drivers, even though you cannot actually test these.
When are you planning to provide fixes for all these warnings? As is, the whole build is spoiled. Or should we revert these patches?
I plan to submit patches for the boards I am able to test, but for reason that Mike suggested, I'm not sure it is wise to fix the warning unless it can be tested on hardware.
The rule is that he who breaks the code is also responsible to fix it. Please make the code at least compile-clean again. Board maintainers will have to help with actual testing.
Thanks.
Best regards,
Wolfgang Denk

Hi Wolfgang,
On Mon, May 21, 2012 at 2:05 PM, Wolfgang Denk wd@denx.de wrote:
Dear Joe Hershberger,
In message CANr=Z=ZiMqvn2vxp0_kMA76O091TJFu4ck6wVf7re3hEFMOk2g@mail.gmail.com you wrote:
While getting rid of volatile is a good thing in general, I am worried about the test scope of this patch.
I was too. That's why I asked for guidance:
Sorry, this was a misunderstanding then.
My expectation was that you wuld continue this patch series and clean up the drivers, even though you cannot actually test these.
When are you planning to provide fixes for all these warnings? As is, the whole build is spoiled. Or should we revert these patches?
I plan to submit patches for the boards I am able to test, but for reason that Mike suggested, I'm not sure it is wise to fix the warning unless it can be tested on hardware.
The rule is that he who breaks the code is also responsible to fix it. Please make the code at least compile-clean again. Board maintainers will have to help with actual testing.
Will do.
-Joe

Dear Joe Hershberger,
In message CANr=Z=bmuwyjiDmXj1qjJ5hsKCfm15gbK2GbZNp8X2E=1KMp6A@mail.gmail.com you wrote:
The rule is that he who breaks the code is also responsible to fix it. Please make the code at least compile-clean again. Board maintainers will have to help with actual testing.
Will do.
Thanks a lot. Sorry for the miscommunication.
Best regards,
Wolfgang Denk

Commit db288a9 "net: Remove volatile from net API" caused a number of build warnings: fec.c: In function 'fec_initialize': fec.c:183:13: warning: assignment from incompatible pointer type [enabled by default] fec.c: In function 'fec_recv': fec.c:284:5: warning: passing argument 1 of 'NetReceive' discards 'volatile' qualifier from pointer target type [enabled by default] /home/wd/git/u-boot/work/include/net.h:433:13: note: expected 'uchar *' but argument is of type 'volatile uchar *' scc.c: In function 'scc_initialize': scc.c:85:14: warning: assignment from incompatible pointer type [enabled by default]
Fix these.
Signed-off-by: Wolfgang Denk wd@denx.de Cc: Joe Hershberger joe.hershberger@ni.com ---
Joe, I started fixing these issues when I realized that this is not just a few files, but actually _ALL_ network drivers that you forgot to adapt. I wonder how much testing was done before you decided to pull this into mainline? It appears not much compile testing... what about run-time testing?
Sorry, but I don't have time to clean up all of this. Are you going to do that?
-wd
arch/powerpc/cpu/mpc8xx/fec.c | 6 +++--- arch/powerpc/cpu/mpc8xx/scc.c | 4 ++-- 2 files changed, 5 insertions(+), 5 deletions(-)
diff --git a/arch/powerpc/cpu/mpc8xx/fec.c b/arch/powerpc/cpu/mpc8xx/fec.c index f2a2c3a..64e2ef7 100644 --- a/arch/powerpc/cpu/mpc8xx/fec.c +++ b/arch/powerpc/cpu/mpc8xx/fec.c @@ -139,7 +139,7 @@ typedef volatile struct CommonBufferDescriptor {
static RTXBD *rtx = NULL;
-static int fec_send(struct eth_device* dev, volatile void *packet, int length); +static int fec_send(struct eth_device* dev, void *packet, int length); static int fec_recv(struct eth_device* dev); static int fec_init(struct eth_device* dev, bd_t * bd); static void fec_halt(struct eth_device* dev); @@ -193,7 +193,7 @@ int fec_initialize(bd_t *bis) return 1; }
-static int fec_send(struct eth_device* dev, volatile void *packet, int length) +static int fec_send(struct eth_device* dev, void *packet, int length) { int j, rc; struct ether_fcc_info_s *efis = dev->priv; @@ -267,7 +267,7 @@ static int fec_recv (struct eth_device *dev) rtx->rxbd[rxIdx].cbd_sc); #endif } else { - volatile uchar *rx = NetRxPackets[rxIdx]; + uchar *rx = NetRxPackets[rxIdx];
length -= 4;
diff --git a/arch/powerpc/cpu/mpc8xx/scc.c b/arch/powerpc/cpu/mpc8xx/scc.c index e376c64..b172121 100644 --- a/arch/powerpc/cpu/mpc8xx/scc.c +++ b/arch/powerpc/cpu/mpc8xx/scc.c @@ -65,7 +65,7 @@ typedef volatile struct CommonBufferDescriptor {
static RTXBD *rtx;
-static int scc_send(struct eth_device* dev, volatile void *packet, int length); +static int scc_send(struct eth_device* dev, void *packet, int length); static int scc_recv(struct eth_device* dev); static int scc_init (struct eth_device* dev, bd_t * bd); static void scc_halt(struct eth_device* dev); @@ -90,7 +90,7 @@ int scc_initialize(bd_t *bis) return 1; }
-static int scc_send(struct eth_device* dev, volatile void *packet, int length) +static int scc_send(struct eth_device* dev, void *packet, int length) { int i, j=0; #if 0

Hi Wolfgang,
On Mon, May 21, 2012 at 2:14 AM, Wolfgang Denk wd@denx.de wrote:
Commit db288a9 "net: Remove volatile from net API" caused a number of build warnings: fec.c: In function 'fec_initialize': fec.c:183:13: warning: assignment from incompatible pointer type [enabled by default] fec.c: In function 'fec_recv': fec.c:284:5: warning: passing argument 1 of 'NetReceive' discards 'volatile' qualifier from pointer target type [enabled by default] /home/wd/git/u-boot/work/include/net.h:433:13: note: expected 'uchar *' but argument is of type 'volatile uchar *' scc.c: In function 'scc_initialize': scc.c:85:14: warning: assignment from incompatible pointer type [enabled by default]
Fix these.
Signed-off-by: Wolfgang Denk wd@denx.de Cc: Joe Hershberger joe.hershberger@ni.com
Joe, I started fixing these issues when I realized that this is not just a few files, but actually _ALL_ network drivers that you forgot to adapt. I wonder how much testing was done before you decided to pull this into mainline? It appears not much compile testing... what about run-time testing?
Sorry, but I don't have time to clean up all of this. Are you going to do that?
Applied this for a start.
Thanks, -Joe

Signed-off-by: Joe Hershberger joe.hershberger@ni.com Cc: Joe Hershberger joe.hershberger@gmail.com --- Changes for v2: - Split into smaller patches Changes for v3: - Added blank lines for Simon Glass
include/net.h | 116 +++++++++++++++++++++++++++++--------------------------- 1 files changed, 60 insertions(+), 56 deletions(-)
diff --git a/include/net.h b/include/net.h index 7440182..2b6e59a 100644 --- a/include/net.h +++ b/include/net.h @@ -80,14 +80,14 @@ struct eth_device { int iobase; int state;
- int (*init) (struct eth_device*, bd_t*); - int (*send) (struct eth_device*, void *packet, int length); - int (*recv) (struct eth_device*); - void (*halt) (struct eth_device*); + int (*init) (struct eth_device *, bd_t *); + int (*send) (struct eth_device *, void *packet, int length); + int (*recv) (struct eth_device *); + void (*halt) (struct eth_device *); #ifdef CONFIG_MCAST_TFTP - int (*mcast) (struct eth_device*, u32 ip, u8 set); + int (*mcast) (struct eth_device *, u32 ip, u8 set); #endif - int (*write_hwaddr) (struct eth_device*); + int (*write_hwaddr) (struct eth_device *); struct eth_device *next; int index; void *priv; @@ -101,7 +101,7 @@ extern void eth_set_current(void); /* set nterface to ethcur var */ extern struct eth_device *eth_get_dev(void); /* get the current device MAC */ extern struct eth_device *eth_get_dev_by_name(const char *devname); extern struct eth_device *eth_get_dev_by_index(int index); /* get dev @ index */ -extern int eth_get_dev_index (void); /* get the device index */ +extern int eth_get_dev_index(void); /* get the device index */ extern void eth_parse_enetaddr(const char *addr, uchar *enetaddr); extern int eth_getenv_enetaddr(char *name, uchar *enetaddr); extern int eth_setenv_enetaddr(char *name, const uchar *enetaddr); @@ -143,8 +143,8 @@ int eth_write_hwaddr(struct eth_device *dev, const char *base_name, int eth_number);
#ifdef CONFIG_MCAST_TFTP -int eth_mcast_join( IPaddr_t mcast_addr, u8 join); -u32 ether_crc (size_t len, unsigned char const *p); +int eth_mcast_join(IPaddr_t mcast_addr, u8 join); +u32 ether_crc(size_t len, unsigned char const *p); #endif
@@ -219,8 +219,8 @@ typedef struct { #define IP_FLAGS_DFRAG 0x4000 /* don't fragments */ #define IP_FLAGS_MFRAG 0x2000 /* more fragments */
-#define IP_HDR_SIZE_NO_UDP (sizeof (IP_t) - 8) -#define IP_HDR_SIZE (sizeof (IP_t)) +#define IP_HDR_SIZE_NO_UDP (sizeof(IP_t) - 8) +#define IP_HDR_SIZE (sizeof(IP_t))
/* @@ -327,46 +327,46 @@ typedef struct icmphdr {
/* net.c */ /** BOOTP EXTENTIONS **/ -extern IPaddr_t NetOurGatewayIP; /* Our gateway IP addresse */ -extern IPaddr_t NetOurSubnetMask; /* Our subnet mask (0 = unknown)*/ -extern IPaddr_t NetOurDNSIP; /* Our Domain Name Server (0 = unknown)*/ +extern IPaddr_t NetOurGatewayIP; /* Our gateway IP address */ +extern IPaddr_t NetOurSubnetMask; /* Our subnet mask (0 = unknown) */ +extern IPaddr_t NetOurDNSIP; /* Our Domain Name Server (0 = unknown) */ #if defined(CONFIG_BOOTP_DNS2) -extern IPaddr_t NetOurDNS2IP; /* Our 2nd Domain Name Server (0 = unknown)*/ +extern IPaddr_t NetOurDNS2IP; /* Our 2nd Domain Name Server (0 = unknown) */ #endif -extern char NetOurNISDomain[32]; /* Our NIS domain */ -extern char NetOurHostName[32]; /* Our hostname */ -extern char NetOurRootPath[64]; /* Our root path */ -extern ushort NetBootFileSize; /* Our boot file size in blocks */ +extern char NetOurNISDomain[32]; /* Our NIS domain */ +extern char NetOurHostName[32]; /* Our hostname */ +extern char NetOurRootPath[64]; /* Our root path */ +extern ushort NetBootFileSize; /* Our boot file size in blocks */ /** END OF BOOTP EXTENTIONS **/ -extern ulong NetBootFileXferSize; /* size of bootfile in bytes */ -extern uchar NetOurEther[6]; /* Our ethernet address */ -extern uchar NetServerEther[6]; /* Boot server enet address */ -extern IPaddr_t NetOurIP; /* Our IP addr (0 = unknown) */ -extern IPaddr_t NetServerIP; /* Server IP addr (0 = unknown) */ +extern ulong NetBootFileXferSize; /* size of bootfile in bytes */ +extern uchar NetOurEther[6]; /* Our ethernet address */ +extern uchar NetServerEther[6]; /* Boot server enet address */ +extern IPaddr_t NetOurIP; /* Our IP addr (0 = unknown) */ +extern IPaddr_t NetServerIP; /* Server IP addr (0 = unknown) */ extern uchar *NetTxPacket; /* THE transmit packet */ -extern uchar *NetRxPackets[PKTBUFSRX];/* Receive packets */ +extern uchar *NetRxPackets[PKTBUFSRX]; /* Receive packets */ extern uchar *NetRxPacket; /* Current receive packet */ -extern int NetRxPacketLen; /* Current rx packet length */ -extern unsigned NetIPID; /* IP ID (counting) */ -extern uchar NetBcastAddr[6]; /* Ethernet boardcast address */ +extern int NetRxPacketLen; /* Current rx packet length */ +extern unsigned NetIPID; /* IP ID (counting) */ +extern uchar NetBcastAddr[6]; /* Ethernet boardcast address */ extern uchar NetEtherNullAddr[6];
-#define VLAN_NONE 4095 /* untagged */ -#define VLAN_IDMASK 0x0fff /* mask of valid vlan id */ -extern ushort NetOurVLAN; /* Our VLAN */ -extern ushort NetOurNativeVLAN; /* Our Native VLAN */ +#define VLAN_NONE 4095 /* untagged */ +#define VLAN_IDMASK 0x0fff /* mask of valid vlan id */ +extern ushort NetOurVLAN; /* Our VLAN */ +extern ushort NetOurNativeVLAN; /* Our Native VLAN */
-extern uchar NetCDPAddr[6]; /* Ethernet CDP address */ -extern ushort CDPNativeVLAN; /* CDP returned native VLAN */ -extern ushort CDPApplianceVLAN; /* CDP returned appliance VLAN */ +extern uchar NetCDPAddr[6]; /* Ethernet CDP address */ +extern ushort CDPNativeVLAN; /* CDP returned native VLAN */ +extern ushort CDPApplianceVLAN; /* CDP returned appliance VLAN */
-extern int NetState; /* Network loop state */ +extern int NetState; /* Network loop state */ #define NETLOOP_CONTINUE 1 #define NETLOOP_RESTART 2 #define NETLOOP_SUCCESS 3 #define NETLOOP_FAIL 4
-extern int NetRestartWrap; /* Tried all network devices */ +extern int NetRestartWrap; /* Tried all network devices */
enum proto_t { BOOTP, RARP, ARP, TFTPGET, DHCP, PING, DNS, NFS, CDP, NETCONS, SNTP, @@ -374,7 +374,7 @@ enum proto_t { };
/* from net/net.c */ -extern char BootFile[128]; /* Boot File name */ +extern char BootFile[128]; /* Boot File name */
#if defined(CONFIG_CMD_DNS) extern char *NetDNSResolve; /* The host to resolve */ @@ -382,7 +382,7 @@ extern char *NetDNSenvvar; /* the env var to put the ip into */ #endif
#if defined(CONFIG_CMD_PING) -extern IPaddr_t NetPingIP; /* the ip address to ping */ +extern IPaddr_t NetPingIP; /* the ip address to ping */ #endif
#if defined(CONFIG_CMD_CDP) @@ -392,8 +392,8 @@ extern ushort CDPApplianceVLAN; #endif
#if defined(CONFIG_CMD_SNTP) -extern IPaddr_t NetNtpServerIP; /* the ip address to NTP */ -extern int NetTimeOffset; /* offset time from UTC */ +extern IPaddr_t NetNtpServerIP; /* the ip address to NTP */ +extern int NetTimeOffset; /* offset time from UTC */ #endif
/* Initialize the network adapter */ @@ -415,19 +415,20 @@ extern int NetSetEther(uchar *, uchar *, uint); extern void NetSetIP(uchar *, IPaddr_t, int, int, int);
/* Checksum */ -extern int NetCksumOk(uchar *, int); /* Return true if cksum OK */ -extern uint NetCksum(uchar *, int); /* Calculate the checksum */ +extern int NetCksumOk(uchar *, int); /* Return true if cksum OK */ +extern uint NetCksum(uchar *, int); /* Calculate the checksum */
/* Set callbacks */ -extern void NetSetHandler(rxhand_f *); /* Set RX packet handler */ +extern void NetSetHandler(rxhand_f *); /* Set RX packet handler */ extern void net_set_icmp_handler(rxhand_icmp_f *f); /* Set ICMP RX handler */ -extern void NetSetTimeout(ulong, thand_f *);/* Set timeout handler */ +extern void NetSetTimeout(ulong, thand_f *);/* Set timeout handler */
/* Transmit "NetTxPacket" */ extern void NetSendPacket(uchar *, int);
/* Transmit UDP packet, performing ARP request if needed */ -extern int NetSendUDPPacket(uchar *ether, IPaddr_t dest, int dport, int sport, int len); +extern int NetSendUDPPacket(uchar *ether, IPaddr_t dest, int dport, + int sport, int len);
/* Processes a received packet */ extern void NetReceive(uchar *, int); @@ -449,7 +450,8 @@ void net_auto_load(void); static inline IPaddr_t NetReadIP(void *from) { IPaddr_t ip; - memcpy((void*)&ip, (void*)from, sizeof(ip)); + + memcpy((void *)&ip, (void *)from, sizeof(ip)); return ip; }
@@ -457,26 +459,27 @@ static inline IPaddr_t NetReadIP(void *from) static inline ulong NetReadLong(ulong *from) { ulong l; - memcpy((void*)&l, (void*)from, sizeof(l)); + + memcpy((void *)&l, (void *)from, sizeof(l)); return l; }
/* write IP *in network byteorder* */ static inline void NetWriteIP(void *to, IPaddr_t ip) { - memcpy(to, (void*)&ip, sizeof(ip)); + memcpy(to, (void *)&ip, sizeof(ip)); }
/* copy IP */ static inline void NetCopyIP(void *to, void *from) { - memcpy((void*)to, from, sizeof(IPaddr_t)); + memcpy((void *)to, from, sizeof(IPaddr_t)); }
/* copy ulong */ static inline void NetCopyLong(ulong *to, ulong *from) { - memcpy((void*)to, (void*)from, sizeof(ulong)); + memcpy((void *)to, (void *)from, sizeof(ulong)); }
/** @@ -499,7 +502,7 @@ static inline int is_zero_ether_addr(const u8 *addr) */ static inline int is_multicast_ether_addr(const u8 *addr) { - return (0x01 & addr[0]); + return 0x01 & addr[0]; }
/* @@ -510,7 +513,8 @@ static inline int is_multicast_ether_addr(const u8 *addr) */ static inline int is_broadcast_ether_addr(const u8 *addr) { - return (addr[0] & addr[1] & addr[2] & addr[3] & addr[4] & addr[5]) == 0xff; + return (addr[0] & addr[1] & addr[2] & addr[3] & addr[4] & addr[5]) == + 0xff; }
/* @@ -530,13 +534,13 @@ static inline int is_valid_ether_addr(const u8 *addr) }
/* Convert an IP address to a string */ -extern void ip_to_string (IPaddr_t x, char *s); +extern void ip_to_string(IPaddr_t x, char *s);
/* Convert a string to ip address */ extern IPaddr_t string_to_ip(const char *s);
/* Convert a VLAN id to a string */ -extern void VLAN_to_string (ushort x, char *s); +extern void VLAN_to_string(ushort x, char *s);
/* Convert a string to a vlan id */ extern ushort string_to_VLAN(const char *s); @@ -545,7 +549,7 @@ extern ushort string_to_VLAN(const char *s); extern ushort getenv_VLAN(char *);
/* copy a filename (allow for "..." notation, limit length) */ -extern void copy_filename (char *dst, const char *src, int size); +extern void copy_filename(char *dst, const char *src, int size);
/* get a random source port */ extern unsigned int random_port(void);

Signed-off-by: Joe Hershberger joe.hershberger@ni.com Cc: Joe Hershberger joe.hershberger@gmail.com --- Changes for v2: - Split into smaller patches Changes for v3: - Fixed commit summary net.c -> bootp.*
net/bootp.c | 340 ++++++++++++++++++++++++++++++----------------------------- net/bootp.h | 56 +++++----- 2 files changed, 203 insertions(+), 193 deletions(-)
diff --git a/net/bootp.c b/net/bootp.c index 35654b4..d0a7da2 100644 --- a/net/bootp.c +++ b/net/bootp.c @@ -19,19 +19,19 @@ #endif #include <linux/compiler.h>
-#define BOOTP_VENDOR_MAGIC 0x63825363 /* RFC1048 Magic Cookie */ +#define BOOTP_VENDOR_MAGIC 0x63825363 /* RFC1048 Magic Cookie */
#define TIMEOUT 5000UL /* Milliseconds before trying BOOTP again */ #ifndef CONFIG_NET_RETRY_COUNT -# define TIMEOUT_COUNT 5 /* # of timeouts before giving up */ +# define TIMEOUT_COUNT 5 /* # of timeouts before giving up */ #else # define TIMEOUT_COUNT (CONFIG_NET_RETRY_COUNT) #endif
-#define PORT_BOOTPS 67 /* BOOTP server UDP port */ -#define PORT_BOOTPC 68 /* BOOTP client UDP port */ +#define PORT_BOOTPS 67 /* BOOTP server UDP port */ +#define PORT_BOOTPC 68 /* BOOTP client UDP port */
-#ifndef CONFIG_DHCP_MIN_EXT_LEN /* minimal length of extension list */ +#ifndef CONFIG_DHCP_MIN_EXT_LEN /* minimal length of extension list */ #define CONFIG_DHCP_MIN_EXT_LEN 64 #endif
@@ -43,8 +43,8 @@ ulong seed1, seed2;
#if defined(CONFIG_CMD_DHCP) dhcp_state_t dhcp_state = INIT; -unsigned long dhcp_leasetime = 0; -IPaddr_t NetDHCPServerIP = 0; +unsigned long dhcp_leasetime; +IPaddr_t NetDHCPServerIP; static void DhcpHandler(uchar *pkt, unsigned dest, IPaddr_t sip, unsigned src, unsigned len);
@@ -64,37 +64,29 @@ static char *dhcpmsg2str(int type) } } #endif - -#if defined(CONFIG_BOOTP_VENDOREX) -extern u8 *dhcp_vendorex_prep (u8 *e); /*rtn new e after add own opts. */ -extern u8 *dhcp_vendorex_proc (u8 *e); /*rtn next e if mine,else NULL */ -#endif - #endif
static int BootpCheckPkt(uchar *pkt, unsigned dest, unsigned src, unsigned len) { - Bootp_t *bp = (Bootp_t *) pkt; + struct Bootp_t *bp = (struct Bootp_t *) pkt; int retval = 0;
if (dest != PORT_BOOTPC || src != PORT_BOOTPS) retval = -1; - else if (len < sizeof (Bootp_t) - OPT_SIZE) + else if (len < sizeof(struct Bootp_t) - OPT_SIZE) retval = -2; else if (bp->bp_op != OP_BOOTREQUEST && - bp->bp_op != OP_BOOTREPLY && - bp->bp_op != DHCP_OFFER && - bp->bp_op != DHCP_ACK && - bp->bp_op != DHCP_NAK ) { + bp->bp_op != OP_BOOTREPLY && + bp->bp_op != DHCP_OFFER && + bp->bp_op != DHCP_ACK && + bp->bp_op != DHCP_NAK) retval = -3; - } else if (bp->bp_htype != HWT_ETHER) retval = -4; else if (bp->bp_hlen != HWL_ETHER) retval = -5; - else if (NetReadLong((ulong*)&bp->bp_id) != BootpID) { + else if (NetReadLong((ulong *)&bp->bp_id) != BootpID) retval = -6; - }
debug("Filtering pkt = %d\n", retval);
@@ -104,7 +96,7 @@ static int BootpCheckPkt(uchar *pkt, unsigned dest, unsigned src, unsigned len) /* * Copy parameters of interest from BOOTP_REPLY/DHCP_OFFER packet */ -static void BootpCopyNetParams(Bootp_t *bp) +static void BootpCopyNetParams(struct Bootp_t *bp) { __maybe_unused IPaddr_t tmp_ip;
@@ -113,10 +105,10 @@ static void BootpCopyNetParams(Bootp_t *bp) NetCopyIP(&tmp_ip, &bp->bp_siaddr); if (tmp_ip != 0) NetCopyIP(&NetServerIP, &bp->bp_siaddr); - memcpy (NetServerEther, ((Ethernet_t *)NetRxPacket)->et_src, 6); + memcpy(NetServerEther, ((Ethernet_t *)NetRxPacket)->et_src, 6); #endif if (strlen(bp->bp_file) > 0) - copy_filename (BootFile, bp->bp_file, sizeof(BootFile)); + copy_filename(BootFile, bp->bp_file, sizeof(BootFile));
debug("Bootfile: %s\n", BootFile);
@@ -124,97 +116,95 @@ static void BootpCopyNetParams(Bootp_t *bp) * don't delete exising entry when BOOTP / DHCP reply does * not contain a new value */ - if (*BootFile) { - setenv ("bootfile", BootFile); - } + if (*BootFile) + setenv("bootfile", BootFile); }
-static int truncate_sz (const char *name, int maxlen, int curlen) +static int truncate_sz(const char *name, int maxlen, int curlen) { if (curlen >= maxlen) { - printf("*** WARNING: %s is too long (%d - max: %d) - truncated\n", - name, curlen, maxlen); + printf("*** WARNING: %s is too long (%d - max: %d)" + " - truncated\n", name, curlen, maxlen); curlen = maxlen - 1; } - return (curlen); + return curlen; }
#if !defined(CONFIG_CMD_DHCP)
-static void BootpVendorFieldProcess (u8 * ext) +static void BootpVendorFieldProcess(u8 *ext) { int size = *(ext + 1);
debug("[BOOTP] Processing extension %d... (%d bytes)\n", *ext, - *(ext + 1)); + *(ext + 1));
NetBootFileSize = 0;
switch (*ext) { /* Fixed length fields */ - case 1: /* Subnet mask */ + case 1: /* Subnet mask */ if (NetOurSubnetMask == 0) - NetCopyIP (&NetOurSubnetMask, (IPaddr_t *) (ext + 2)); + NetCopyIP(&NetOurSubnetMask, (IPaddr_t *) (ext + 2)); break; - case 2: /* Time offset - Not yet supported */ + case 2: /* Time offset - Not yet supported */ break; /* Variable length fields */ - case 3: /* Gateways list */ - if (NetOurGatewayIP == 0) { - NetCopyIP (&NetOurGatewayIP, (IPaddr_t *) (ext + 2)); - } + case 3: /* Gateways list */ + if (NetOurGatewayIP == 0) + NetCopyIP(&NetOurGatewayIP, (IPaddr_t *) (ext + 2)); break; - case 4: /* Time server - Not yet supported */ + case 4: /* Time server - Not yet supported */ break; - case 5: /* IEN-116 name server - Not yet supported */ + case 5: /* IEN-116 name server - Not yet supported */ break; case 6: - if (NetOurDNSIP == 0) { - NetCopyIP (&NetOurDNSIP, (IPaddr_t *) (ext + 2)); - } + if (NetOurDNSIP == 0) + NetCopyIP(&NetOurDNSIP, (IPaddr_t *) (ext + 2)); #if defined(CONFIG_BOOTP_DNS2) - if ((NetOurDNS2IP == 0) && (size > 4)) { - NetCopyIP (&NetOurDNS2IP, (IPaddr_t *) (ext + 2 + 4)); - } + if ((NetOurDNS2IP == 0) && (size > 4)) + NetCopyIP(&NetOurDNS2IP, (IPaddr_t *) (ext + 2 + 4)); #endif break; - case 7: /* Log server - Not yet supported */ + case 7: /* Log server - Not yet supported */ break; - case 8: /* Cookie/Quote server - Not yet supported */ + case 8: /* Cookie/Quote server - Not yet supported */ break; - case 9: /* LPR server - Not yet supported */ + case 9: /* LPR server - Not yet supported */ break; - case 10: /* Impress server - Not yet supported */ + case 10: /* Impress server - Not yet supported */ break; - case 11: /* RPL server - Not yet supported */ + case 11: /* RPL server - Not yet supported */ break; - case 12: /* Host name */ + case 12: /* Host name */ if (NetOurHostName[0] == 0) { - size = truncate_sz ("Host Name", sizeof (NetOurHostName), size); - memcpy (&NetOurHostName, ext + 2, size); + size = truncate_sz("Host Name", + sizeof(NetOurHostName), size); + memcpy(&NetOurHostName, ext + 2, size); NetOurHostName[size] = 0; } break; - case 13: /* Boot file size */ + case 13: /* Boot file size */ if (size == 2) - NetBootFileSize = ntohs (*(ushort *) (ext + 2)); + NetBootFileSize = ntohs(*(ushort *) (ext + 2)); else if (size == 4) - NetBootFileSize = ntohl (*(ulong *) (ext + 2)); + NetBootFileSize = ntohl(*(ulong *) (ext + 2)); break; - case 14: /* Merit dump file - Not yet supported */ + case 14: /* Merit dump file - Not yet supported */ break; - case 15: /* Domain name - Not yet supported */ + case 15: /* Domain name - Not yet supported */ break; - case 16: /* Swap server - Not yet supported */ + case 16: /* Swap server - Not yet supported */ break; - case 17: /* Root path */ + case 17: /* Root path */ if (NetOurRootPath[0] == 0) { - size = truncate_sz ("Root Path", sizeof (NetOurRootPath), size); - memcpy (&NetOurRootPath, ext + 2, size); + size = truncate_sz("Root Path", + sizeof(NetOurRootPath), size); + memcpy(&NetOurRootPath, ext + 2, size); NetOurRootPath[size] = 0; } break; - case 18: /* Extension path - Not yet supported */ + case 18: /* Extension path - Not yet supported */ /* * This can be used to send the information of the * vendor area in another file that the client can @@ -222,10 +212,11 @@ static void BootpVendorFieldProcess (u8 * ext) */ break; /* IP host layer fields */ - case 40: /* NIS Domain name */ + case 40: /* NIS Domain name */ if (NetOurNISDomain[0] == 0) { - size = truncate_sz ("NIS Domain Name", sizeof (NetOurNISDomain), size); - memcpy (&NetOurNISDomain, ext + 2, size); + size = truncate_sz("NIS Domain Name", + sizeof(NetOurNISDomain), size); + memcpy(&NetOurNISDomain, ext + 2, size); NetOurNISDomain[size] = 0; } break; @@ -235,7 +226,7 @@ static void BootpVendorFieldProcess (u8 * ext) break; #endif /* Application layer fields */ - case 43: /* Vendor specific info - Not yet supported */ + case 43: /* Vendor specific info - Not yet supported */ /* * Binary information to exchange specific * product information. @@ -245,7 +236,7 @@ static void BootpVendorFieldProcess (u8 * ext) } }
-static void BootpVendorProcess (u8 * ext, int size) +static void BootpVendorProcess(u8 *ext, int size) { u8 *end = ext + size;
@@ -259,11 +250,11 @@ static void BootpVendorProcess (u8 * ext, int size)
ext += ext[1] + 2; if (ext <= end) - BootpVendorFieldProcess (opt); + BootpVendorFieldProcess(opt); } }
- debug("[BOOTP] Received fields: \n"); + debug("[BOOTP] Received fields:\n"); if (NetOurSubnetMask) debug("NetOurSubnetMask : %pI4\n", &NetOurSubnetMask);
@@ -298,27 +289,28 @@ static void BootpHandler(uchar *pkt, unsigned dest, IPaddr_t sip, unsigned src, unsigned len) { - Bootp_t *bp; + struct Bootp_t *bp;
debug("got BOOTP packet (src=%d, dst=%d, len=%d want_len=%zu)\n", - src, dest, len, sizeof (Bootp_t)); + src, dest, len, sizeof(struct Bootp_t));
- bp = (Bootp_t *)pkt; + bp = (struct Bootp_t *)pkt;
- if (BootpCheckPkt(pkt, dest, src, len)) /* Filter out pkts we don't want */ + /* Filter out pkts we don't want */ + if (BootpCheckPkt(pkt, dest, src, len)) return;
/* * Got a good BOOTP reply. Copy the data into our variables. */ #ifdef CONFIG_STATUS_LED - status_led_set (STATUS_LED_BOOT, STATUS_LED_OFF); + status_led_set(STATUS_LED_BOOT, STATUS_LED_OFF); #endif
BootpCopyNetParams(bp); /* Store net parameters from reply */
/* Retrieve extended information (we must parse the vendor area) */ - if (NetReadLong((ulong*)&bp->bp_vend[0]) == htonl(BOOTP_VENDOR_MAGIC)) + if (NetReadLong((ulong *)&bp->bp_vend[0]) == htonl(BOOTP_VENDOR_MAGIC)) BootpVendorProcess((uchar *)&bp->bp_vend[4], len);
NetSetTimeout(0, (thand_f *)0); @@ -337,11 +329,11 @@ static void BootpTimeout(void) { if (BootpTry >= TIMEOUT_COUNT) { - puts ("\nRetry count exceeded; starting again\n"); - NetStartAgain (); + puts("\nRetry count exceeded; starting again\n"); + NetStartAgain(); } else { - NetSetTimeout (TIMEOUT, BootpTimeout); - BootpRequest (); + NetSetTimeout(TIMEOUT, BootpTimeout); + BootpRequest(); } }
@@ -349,7 +341,8 @@ BootpTimeout(void) * Initialize BOOTP extension fields in the request. */ #if defined(CONFIG_CMD_DHCP) -static int DhcpExtended (u8 * e, int message_type, IPaddr_t ServerID, IPaddr_t RequestedIP) +static int DhcpExtended(u8 *e, int message_type, IPaddr_t ServerID, + IPaddr_t RequestedIP) { u8 *start = e; u8 *cnt; @@ -381,7 +374,7 @@ static int DhcpExtended (u8 * e, int message_type, IPaddr_t ServerID, IPaddr_t R *e++ = (576 - 312 + OPT_SIZE) & 0xff;
if (ServerID) { - int tmp = ntohl (ServerID); + int tmp = ntohl(ServerID);
*e++ = 54; /* ServerID */ *e++ = 4; @@ -392,7 +385,7 @@ static int DhcpExtended (u8 * e, int message_type, IPaddr_t ServerID, IPaddr_t R }
if (RequestedIP) { - int tmp = ntohl (RequestedIP); + int tmp = ntohl(RequestedIP);
*e++ = 50; /* Requested IP */ *e++ = 4; @@ -402,12 +395,13 @@ static int DhcpExtended (u8 * e, int message_type, IPaddr_t ServerID, IPaddr_t R *e++ = tmp & 0xff; } #if defined(CONFIG_BOOTP_SEND_HOSTNAME) - if ((hostname = getenv ("hostname"))) { - int hostnamelen = strlen (hostname); + hostname = getenv("hostname"); + if (hostname) { + int hostnamelen = strlen(hostname);
*e++ = 12; /* Hostname */ *e++ = hostnamelen; - memcpy (e, hostname, hostnamelen); + memcpy(e, hostname, hostnamelen); e += hostnamelen; } #endif @@ -448,7 +442,8 @@ static int DhcpExtended (u8 * e, int message_type, IPaddr_t ServerID, IPaddr_t R #endif
#if defined(CONFIG_BOOTP_VENDOREX) - if ((x = dhcp_vendorex_prep (e))) + x = dhcp_vendorex_prep(e); + if (x) return x - start; #endif
@@ -508,9 +503,9 @@ static int DhcpExtended (u8 * e, int message_type, IPaddr_t ServerID, IPaddr_t R
#else /* - * Warning: no field size check - change CONFIG_BOOTP_* at your own risk! + * Warning: no field size check - change CONFIG_BOOTP_* at your own risk! */ -static int BootpExtended (u8 * e) +static int BootpExtended(u8 *e) { u8 *start = e;
@@ -584,10 +579,10 @@ static int BootpExtended (u8 * e) #endif
void -BootpRequest (void) +BootpRequest(void) { uchar *pkt, *iphdr; - Bootp_t *bp; + struct Bootp_t *bp; int ext_len, pktlen, iplen;
bootstage_mark_name(BOOTSTAGE_ID_BOOTP_START, "bootp_start"); @@ -600,22 +595,22 @@ BootpRequest (void) int reg; ulong tst1, tst2, sum, m_mask, m_value = 0;
- if (BootpTry ==0) { + if (BootpTry == 0) { /* get our mac */ eth_getenv_enetaddr("ethaddr", bi_enetaddr);
debug("BootpRequest => Our Mac: "); - for (reg=0; reg<6; reg++) - debug("%x%c", bi_enetaddr[reg], reg==5 ? '\n' : ':'); + for (reg = 0; reg < 6; reg++) + debug("%x%c", bi_enetaddr[reg], reg == 5 ? '\n' : ':');
/* Mac-Manipulation 2 get seed1 */ - tst1=0; - tst2=0; - for (reg=2; reg<6; reg++) { + tst1 = 0; + tst2 = 0; + for (reg = 2; reg < 6; reg++) { tst1 = tst1 << 8; tst1 = tst1 | bi_enetaddr[reg]; } - for (reg=0; reg<2; reg++) { + for (reg = 0; reg < 2; reg++) { tst2 = tst2 | bi_enetaddr[reg]; tst2 = tst2 << 8; } @@ -623,8 +618,8 @@ BootpRequest (void) seed1 = tst1^tst2;
/* Mirror seed1*/ - m_mask=0x1; - for (reg=1;reg<=32;reg++) { + m_mask = 0x1; + for (reg = 1; reg <= 32; reg++) { m_value |= (m_mask & seed1); seed1 = seed1 >> 1; m_value = m_value << 1; @@ -634,44 +629,45 @@ BootpRequest (void) }
/* Random Number Generator */ - - for (reg=0;reg<=0;reg++) { + for (reg = 0; reg <= 0; reg++) { sum = seed1 + seed2; if (sum < seed1 || sum < seed2) sum++; seed2 = seed1; seed1 = sum;
- if (BootpTry<=2) { /* Start with max 1024 * 1ms */ + if (BootpTry <= 2) { /* Start with max 1024 * 1ms */ sum = sum >> (22-BootpTry); - } else { /*After 3rd BOOTP request max 8192 * 1ms */ + } else { /*After 3rd BOOTP request max 8192 * 1ms */ sum = sum >> 19; } }
- printf ("Random delay: %ld ms...\n", sum); - for (reg=0; reg <sum; reg++) { + printf("Random delay: %ld ms...\n", sum); + for (reg = 0; reg < sum; reg++) udelay(1000); /*Wait 1ms*/ - } + #endif /* CONFIG_BOOTP_RANDOM_DELAY */
printf("BOOTP broadcast %d\n", ++BootpTry); pkt = NetTxPacket; - memset ((void*)pkt, 0, PKTSIZE); + memset((void *)pkt, 0, PKTSIZE);
pkt += NetSetEther(pkt, NetBcastAddr, PROT_IP);
/* - * Next line results in incorrect packet size being transmitted, resulting - * in errors in some DHCP servers, reporting missing bytes. Size must be - * set in packet header after extension length has been determined. + * Next line results in incorrect packet size being transmitted, + * resulting in errors in some DHCP servers, reporting missing bytes. + * Size must be set in packet header after extension length has been + * determined. * C. Hallinan, DS4.COM, Inc. */ - /* NetSetIP(pkt, 0xFFFFFFFFL, PORT_BOOTPS, PORT_BOOTPC, sizeof (Bootp_t)); */ + /* NetSetIP(pkt, 0xFFFFFFFFL, PORT_BOOTPS, PORT_BOOTPC, + sizeof (struct Bootp_t)); */ iphdr = pkt; /* We need this later for NetSetIP() */ pkt += IP_HDR_SIZE;
- bp = (Bootp_t *)pkt; + bp = (struct Bootp_t *)pkt; bp->bp_op = OP_BOOTREQUEST; bp->bp_htype = HWT_ETHER; bp->bp_hlen = HWL_ETHER; @@ -681,8 +677,8 @@ BootpRequest (void) NetWriteIP(&bp->bp_yiaddr, 0); NetWriteIP(&bp->bp_siaddr, 0); NetWriteIP(&bp->bp_giaddr, 0); - memcpy (bp->bp_chaddr, NetOurEther, 6); - copy_filename (bp->bp_file, BootFile, sizeof(bp->bp_file)); + memcpy(bp->bp_chaddr, NetOurEther, 6); + copy_filename(bp->bp_file, BootFile, sizeof(bp->bp_file));
/* Request additional information from the BOOTP/DHCP server */ #if defined(CONFIG_CMD_DHCP) @@ -707,7 +703,8 @@ BootpRequest (void) * Calculate proper packet lengths taking into account the * variable size of the options field */ - pktlen = ((int)(pkt-NetTxPacket)) + BOOTP_HDR_SIZE - sizeof(bp->bp_vend) + ext_len; + pktlen = ((int)(pkt-NetTxPacket)) + BOOTP_HDR_SIZE - + sizeof(bp->bp_vend) + ext_len; iplen = BOOTP_HDR_SIZE - sizeof(bp->bp_vend) + ext_len; NetSetIP(iphdr, 0xFFFFFFFFL, PORT_BOOTPS, PORT_BOOTPC, iplen); NetSetTimeout(SELECT_TIMEOUT, BootpTimeout); @@ -722,7 +719,7 @@ BootpRequest (void) }
#if defined(CONFIG_CMD_DHCP) -static void DhcpOptionsProcess (uchar * popt, Bootp_t *bp) +static void DhcpOptionsProcess(uchar *popt, struct Bootp_t *bp) { uchar *end = popt + BOOTP_HDR_SIZE; int oplen, size; @@ -734,50 +731,51 @@ static void DhcpOptionsProcess (uchar * popt, Bootp_t *bp) oplen = *(popt + 1); switch (*popt) { case 1: - NetCopyIP (&NetOurSubnetMask, (popt + 2)); + NetCopyIP(&NetOurSubnetMask, (popt + 2)); break; #if defined(CONFIG_CMD_SNTP) && defined(CONFIG_BOOTP_TIMEOFFSET) case 2: /* Time offset */ to_ptr = &NetTimeOffset; - NetCopyLong ((ulong *)to_ptr, (ulong *)(popt + 2)); - NetTimeOffset = ntohl (NetTimeOffset); + NetCopyLong((ulong *)to_ptr, (ulong *)(popt + 2)); + NetTimeOffset = ntohl(NetTimeOffset); break; #endif case 3: - NetCopyIP (&NetOurGatewayIP, (popt + 2)); + NetCopyIP(&NetOurGatewayIP, (popt + 2)); break; case 6: - NetCopyIP (&NetOurDNSIP, (popt + 2)); + NetCopyIP(&NetOurDNSIP, (popt + 2)); #if defined(CONFIG_BOOTP_DNS2) - if (*(popt + 1) > 4) { - NetCopyIP (&NetOurDNS2IP, (popt + 2 + 4)); - } + if (*(popt + 1) > 4) + NetCopyIP(&NetOurDNS2IP, (popt + 2 + 4)); #endif break; case 12: - size = truncate_sz ("Host Name", sizeof (NetOurHostName), oplen); - memcpy (&NetOurHostName, popt + 2, size); + size = truncate_sz("Host Name", + sizeof(NetOurHostName), oplen); + memcpy(&NetOurHostName, popt + 2, size); NetOurHostName[size] = 0; break; case 15: /* Ignore Domain Name Option */ break; case 17: - size = truncate_sz ("Root Path", sizeof (NetOurRootPath), oplen); - memcpy (&NetOurRootPath, popt + 2, size); + size = truncate_sz("Root Path", + sizeof(NetOurRootPath), oplen); + memcpy(&NetOurRootPath, popt + 2, size); NetOurRootPath[size] = 0; break; #if defined(CONFIG_CMD_SNTP) && defined(CONFIG_BOOTP_NTPSERVER) case 42: /* NTP server IP */ - NetCopyIP (&NetNtpServerIP, (popt + 2)); + NetCopyIP(&NetNtpServerIP, (popt + 2)); break; #endif case 51: - NetCopyLong (&dhcp_leasetime, (ulong *) (popt + 2)); + NetCopyLong(&dhcp_leasetime, (ulong *) (popt + 2)); break; case 53: /* Ignore Message Type Option */ break; case 54: - NetCopyIP (&NetDHCPServerIP, (popt + 2)); + NetCopyIP(&NetDHCPServerIP, (popt + 2)); break; case 58: /* Ignore Renewal Time Option */ break; @@ -792,7 +790,7 @@ static void DhcpOptionsProcess (uchar * popt, Bootp_t *bp) * pass the bootp packet pointer into here as the * second arg */ - size = truncate_sz ("Opt Boot File", + size = truncate_sz("Opt Boot File", sizeof(bp->bp_file), oplen); if (bp->bp_file[0] == '\0' && size > 0) { @@ -813,10 +811,11 @@ static void DhcpOptionsProcess (uchar * popt, Bootp_t *bp) break; default: #if defined(CONFIG_BOOTP_VENDOREX) - if (dhcp_vendorex_proc (popt)) + if (dhcp_vendorex_proc(popt)) break; #endif - printf ("*** Unhandled DHCP Option in OFFER/ACK: %d\n", *popt); + printf("*** Unhandled DHCP Option in OFFER/ACK:" + " %d\n", *popt); break; } popt += oplen + 2; /* Process next option */ @@ -825,42 +824,42 @@ static void DhcpOptionsProcess (uchar * popt, Bootp_t *bp)
static int DhcpMessageType(unsigned char *popt) { - if (NetReadLong((ulong*)popt) != htonl(BOOTP_VENDOR_MAGIC)) + if (NetReadLong((ulong *)popt) != htonl(BOOTP_VENDOR_MAGIC)) return -1;
popt += 4; - while ( *popt != 0xff ) { - if ( *popt == 53 ) /* DHCP Message Type */ + while (*popt != 0xff) { + if (*popt == 53) /* DHCP Message Type */ return *(popt + 2); popt += *(popt + 1) + 2; /* Scan through all options */ } return -1; }
-static void DhcpSendRequestPkt(Bootp_t *bp_offer) +static void DhcpSendRequestPkt(struct Bootp_t *bp_offer) { uchar *pkt, *iphdr; - Bootp_t *bp; + struct Bootp_t *bp; int pktlen, iplen, extlen; IPaddr_t OfferedIP;
debug("DhcpSendRequestPkt: Sending DHCPREQUEST\n"); pkt = NetTxPacket; - memset ((void*)pkt, 0, PKTSIZE); + memset((void *)pkt, 0, PKTSIZE);
pkt += NetSetEther(pkt, NetBcastAddr, PROT_IP);
- iphdr = pkt; /* We'll need this later to set proper pkt size */ + iphdr = pkt; /* We'll need this later to set proper pkt size */ pkt += IP_HDR_SIZE;
- bp = (Bootp_t *)pkt; + bp = (struct Bootp_t *)pkt; bp->bp_op = OP_BOOTREQUEST; bp->bp_htype = HWT_ETHER; bp->bp_hlen = HWL_ETHER; bp->bp_hops = 0; bp->bp_secs = htons(get_timer(0) / 1000); - /* Do not set the client IP, your IP, or server IP yet, since it hasn't been ACK'ed by - * the server yet */ + /* Do not set the client IP, your IP, or server IP yet, since it + * hasn't been ACK'ed by the server yet */
/* * RFC3046 requires Relay Agents to discard packets with @@ -868,7 +867,7 @@ static void DhcpSendRequestPkt(Bootp_t *bp_offer) */ NetWriteIP(&bp->bp_giaddr, 0);
- memcpy (bp->bp_chaddr, NetOurEther, 6); + memcpy(bp->bp_chaddr, NetOurEther, 6);
/* * ID is the id of the OFFER packet @@ -882,9 +881,11 @@ static void DhcpSendRequestPkt(Bootp_t *bp_offer)
/* Copy offered IP into the parameters request list */ NetCopyIP(&OfferedIP, &bp_offer->bp_yiaddr); - extlen = DhcpExtended((u8 *)bp->bp_vend, DHCP_REQUEST, NetDHCPServerIP, OfferedIP); + extlen = DhcpExtended((u8 *)bp->bp_vend, DHCP_REQUEST, + NetDHCPServerIP, OfferedIP);
- pktlen = ((int)(pkt-NetTxPacket)) + BOOTP_HDR_SIZE - sizeof(bp->bp_vend) + extlen; + pktlen = ((int)(pkt-NetTxPacket)) + BOOTP_HDR_SIZE - + sizeof(bp->bp_vend) + extlen; iplen = BOOTP_HDR_SIZE - sizeof(bp->bp_vend) + extlen; NetSetIP(iphdr, 0xFFFFFFFFL, PORT_BOOTPS, PORT_BOOTPC, iplen);
@@ -902,36 +903,38 @@ static void DhcpHandler(uchar *pkt, unsigned dest, IPaddr_t sip, unsigned src, unsigned len) { - Bootp_t *bp = (Bootp_t *)pkt; + struct Bootp_t *bp = (struct Bootp_t *)pkt;
debug("DHCPHandler: got packet: (src=%d, dst=%d, len=%d) state: %d\n", src, dest, len, dhcp_state);
- if (BootpCheckPkt(pkt, dest, src, len)) /* Filter out pkts we don't want */ + /* Filter out pkts we don't want */ + if (BootpCheckPkt(pkt, dest, src, len)) return;
- debug("DHCPHandler: got DHCP packet: (src=%d, dst=%d, len=%d) state: %d\n", - src, dest, len, dhcp_state); + debug("DHCPHandler: got DHCP packet: (src=%d, dst=%d, len=%d) state:" + " %d\n", src, dest, len, dhcp_state);
switch (dhcp_state) { case SELECTING: /* * Wait an appropriate time for any potential DHCPOFFER packets - * to arrive. Then select one, and generate DHCPREQUEST response. - * If filename is in format we recognize, assume it is a valid - * OFFER from a server we want. + * to arrive. Then select one, and generate DHCPREQUEST + * response. If filename is in format we recognize, assume it + * is a valid OFFER from a server we want. */ debug("DHCP: state=SELECTING bp_file: "%s"\n", bp->bp_file); #ifdef CONFIG_SYS_BOOTFILE_PREFIX if (strncmp(bp->bp_file, CONFIG_SYS_BOOTFILE_PREFIX, - strlen(CONFIG_SYS_BOOTFILE_PREFIX)) == 0 ) { + strlen(CONFIG_SYS_BOOTFILE_PREFIX)) == 0) { #endif /* CONFIG_SYS_BOOTFILE_PREFIX */
debug("TRANSITIONING TO REQUESTING STATE\n"); dhcp_state = REQUESTING;
- if (NetReadLong((ulong*)&bp->bp_vend[0]) == htonl(BOOTP_VENDOR_MAGIC)) + if (NetReadLong((ulong *)&bp->bp_vend[0]) == + htonl(BOOTP_VENDOR_MAGIC)) DhcpOptionsProcess((u8 *)&bp->bp_vend[4], bp);
NetSetTimeout(TIMEOUT, BootpTimeout); @@ -945,14 +948,17 @@ DhcpHandler(uchar *pkt, unsigned dest, IPaddr_t sip, unsigned src, case REQUESTING: debug("DHCP State: REQUESTING\n");
- if ( DhcpMessageType((u8 *)bp->bp_vend) == DHCP_ACK ) { - if (NetReadLong((ulong*)&bp->bp_vend[0]) == htonl(BOOTP_VENDOR_MAGIC)) + if (DhcpMessageType((u8 *)bp->bp_vend) == DHCP_ACK) { + if (NetReadLong((ulong *)&bp->bp_vend[0]) == + htonl(BOOTP_VENDOR_MAGIC)) DhcpOptionsProcess((u8 *)&bp->bp_vend[4], bp); - BootpCopyNetParams(bp); /* Store net params from reply */ + /* Store net params from reply */ + BootpCopyNetParams(bp); dhcp_state = BOUND; - printf ("DHCP client bound to address %pI4\n", &NetOurIP); + printf("DHCP client bound to address %pI4\n", + &NetOurIP); bootstage_mark_name(BOOTSTAGE_ID_BOOTP_STOP, - "bootp_stop"); + "bootp_stop");
net_auto_load(); return; @@ -962,7 +968,7 @@ DhcpHandler(uchar *pkt, unsigned dest, IPaddr_t sip, unsigned src, /* DHCP client bound to address */ break; default: - puts ("DHCP: INVALID STATE\n"); + puts("DHCP: INVALID STATE\n"); break; }
diff --git a/net/bootp.h b/net/bootp.h index 50625ab..ce73734 100644 --- a/net/bootp.h +++ b/net/bootp.h @@ -10,7 +10,7 @@ #define __BOOTP_H__
#ifndef __NET_H__ -#include <net.h> +#include <net.h> #endif /* __NET_H__ */
/**********************************************************************/ @@ -19,35 +19,39 @@ * BOOTP header. */ #if defined(CONFIG_CMD_DHCP) -#define OPT_SIZE 312 /* Minimum DHCP Options size per RFC2131 - results in 576 byte pkt */ +/* Minimum DHCP Options size per RFC2131 - results in 576 byte pkt */ +#define OPT_SIZE 312 +#if defined(CONFIG_BOOTP_VENDOREX) +extern u8 *dhcp_vendorex_prep(u8 *e); /*rtn new e after add own opts. */ +extern u8 *dhcp_vendorex_proc(u8 *e); /*rtn next e if mine,else NULL */ +#endif #else #define OPT_SIZE 64 #endif
-typedef struct -{ - uchar bp_op; /* Operation */ +struct Bootp_t { + uchar bp_op; /* Operation */ # define OP_BOOTREQUEST 1 # define OP_BOOTREPLY 2 - uchar bp_htype; /* Hardware type */ + uchar bp_htype; /* Hardware type */ # define HWT_ETHER 1 - uchar bp_hlen; /* Hardware address length */ + uchar bp_hlen; /* Hardware address length */ # define HWL_ETHER 6 - uchar bp_hops; /* Hop count (gateway thing) */ - ulong bp_id; /* Transaction ID */ - ushort bp_secs; /* Seconds since boot */ - ushort bp_spare1; /* Alignment */ - IPaddr_t bp_ciaddr; /* Client IP address */ - IPaddr_t bp_yiaddr; /* Your (client) IP address */ - IPaddr_t bp_siaddr; /* Server IP address */ - IPaddr_t bp_giaddr; /* Gateway IP address */ - uchar bp_chaddr[16]; /* Client hardware address */ - char bp_sname[64]; /* Server host name */ - char bp_file[128]; /* Boot file name */ - char bp_vend[OPT_SIZE]; /* Vendor information */ -} Bootp_t; - -#define BOOTP_HDR_SIZE sizeof (Bootp_t) + uchar bp_hops; /* Hop count (gateway thing) */ + ulong bp_id; /* Transaction ID */ + ushort bp_secs; /* Seconds since boot */ + ushort bp_spare1; /* Alignment */ + IPaddr_t bp_ciaddr; /* Client IP address */ + IPaddr_t bp_yiaddr; /* Your (client) IP address */ + IPaddr_t bp_siaddr; /* Server IP address */ + IPaddr_t bp_giaddr; /* Gateway IP address */ + uchar bp_chaddr[16]; /* Client hardware address */ + char bp_sname[64]; /* Server host name */ + char bp_file[128]; /* Boot file name */ + char bp_vend[OPT_SIZE]; /* Vendor information */ +}; + +#define BOOTP_HDR_SIZE sizeof(struct Bootp_t) #define BOOTP_SIZE (ETHER_HDR_SIZE + IP_HDR_SIZE + BOOTP_HDR_SIZE)
/**********************************************************************/ @@ -56,16 +60,16 @@ typedef struct */
/* bootp.c */ -extern ulong BootpID; /* ID of cur BOOTP request */ -extern char BootFile[128]; /* Boot file name */ +extern ulong BootpID; /* ID of cur BOOTP request */ +extern char BootFile[128]; /* Boot file name */ extern int BootpTry; #ifdef CONFIG_BOOTP_RANDOM_DELAY -extern ulong seed1, seed2; /* seed for random BOOTP delay */ +extern ulong seed1, seed2; /* seed for random BOOTP delay */ #endif
/* Send a BOOTP request */ -extern void BootpRequest (void); +extern void BootpRequest(void);
/****************** DHCP Support *********************/ extern void DhcpRequest(void);

Signed-off-by: Joe Hershberger joe.hershberger@ni.com Cc: Joe Hershberger joe.hershberger@gmail.com --- Changes for v2: - Split into smaller patches Changes for v3:
net/eth.c | 67 +++++++++++++++++++++++++++++------------------------------- 1 files changed, 32 insertions(+), 35 deletions(-)
diff --git a/net/eth.c b/net/eth.c index 66a84d4..35e0de6 100644 --- a/net/eth.c +++ b/net/eth.c @@ -87,7 +87,7 @@ static struct { int length; } eth_rcv_bufs[PKTBUFSRX];
-static unsigned int eth_rcv_current = 0, eth_rcv_last = 0; +static unsigned int eth_rcv_current, eth_rcv_last; #endif
static struct eth_device *eth_devices, *eth_current; @@ -139,11 +139,10 @@ struct eth_device *eth_get_dev_by_index(int index) return target_dev; }
-int eth_get_dev_index (void) +int eth_get_dev_index(void) { - if (!eth_current) { + if (!eth_current) return -1; - }
return eth_current->index; } @@ -174,7 +173,7 @@ int eth_write_hwaddr(struct eth_device *dev, const char *base_name,
if (memcmp(env_enetaddr, "\0\0\0\0\0\0", 6)) { if (memcmp(dev->enetaddr, "\0\0\0\0\0\0", 6) && - memcmp(dev->enetaddr, env_enetaddr, 6)) { + memcmp(dev->enetaddr, env_enetaddr, 6)) { printf("\nWarning: %s MAC addresses don't match:\n", dev->name); printf("Address in SROM is %pM\n", @@ -187,10 +186,9 @@ int eth_write_hwaddr(struct eth_device *dev, const char *base_name, }
if (dev->write_hwaddr && - !eth_mac_skip(eth_number) && - is_valid_ether_addr(dev->enetaddr)) { + !eth_mac_skip(eth_number) && + is_valid_ether_addr(dev->enetaddr)) ret = dev->write_hwaddr(dev); - }
return ret; } @@ -198,7 +196,7 @@ int eth_write_hwaddr(struct eth_device *dev, const char *base_name, int eth_register(struct eth_device *dev) { struct eth_device *d; - static int index = 0; + static int index;
assert(strlen(dev->name) < sizeof(dev->name));
@@ -206,7 +204,7 @@ int eth_register(struct eth_device *dev) eth_current = eth_devices = dev; eth_current_changed(); } else { - for (d=eth_devices; d->next!=eth_devices; d=d->next) + for (d = eth_devices; d->next != eth_devices; d = d->next) ; d->next = dev; } @@ -276,36 +274,37 @@ int eth_initialize(bd_t *bis) printf("Net Initialization Skipped\n");
if (!eth_devices) { - puts ("No ethernet found.\n"); + puts("No ethernet found.\n"); bootstage_error(BOOTSTAGE_ID_NET_ETH_START); } else { struct eth_device *dev = eth_devices; - char *ethprime = getenv ("ethprime"); + char *ethprime = getenv("ethprime");
bootstage_mark(BOOTSTAGE_ID_NET_ETH_INIT); do { if (dev->index) - puts (", "); + puts(", ");
printf("%s", dev->name);
- if (ethprime && strcmp (dev->name, ethprime) == 0) { + if (ethprime && strcmp(dev->name, ethprime) == 0) { eth_current = dev; - puts (" [PRIME]"); + puts(" [PRIME]"); }
if (strchr(dev->name, ' ')) - puts("\nWarning: eth device name has a space!\n"); + puts("\nWarning: eth device name has a space!" + "\n");
if (eth_write_hwaddr(dev, "eth", dev->index)) puts("\nWarning: failed to set MAC address\n");
dev = dev->next; num_devices++; - } while(dev != eth_devices); + } while (dev != eth_devices);
eth_current_changed(); - putc ('\n'); + putc('\n'); }
return num_devices; @@ -316,9 +315,9 @@ int eth_initialize(bd_t *bis) * mcast_addr: multicast ipaddr from which multicast Mac is made * join: 1=join, 0=leave. */ -int eth_mcast_join( IPaddr_t mcast_ip, u8 join) +int eth_mcast_join(IPaddr_t mcast_ip, u8 join) { - u8 mcast_mac[6]; + u8 mcast_mac[6]; if (!eth_current || !eth_current->mcast) return -1; mcast_mac[5] = htonl(mcast_ip) & 0xff; @@ -335,7 +334,7 @@ int eth_mcast_join( IPaddr_t mcast_ip, u8 join) * some other adapter -- hash tables */ #define CRCPOLY_LE 0xedb88320 -u32 ether_crc (size_t len, unsigned char const *p) +u32 ether_crc(size_t len, unsigned char const *p) { int i; u32 crc; @@ -362,7 +361,7 @@ int eth_init(bd_t *bis) struct eth_device *old_current, *dev;
if (!eth_current) { - puts ("No ethernet found.\n"); + puts("No ethernet found.\n"); return -1; }
@@ -382,7 +381,7 @@ int eth_init(bd_t *bis) do { debug("Trying %s\n", eth_current->name);
- if (eth_current->init(eth_current,bis) >= 0) { + if (eth_current->init(eth_current, bis) >= 0) { eth_current->state = ETH_STATE_ACTIVE;
return 0; @@ -470,38 +469,36 @@ int eth_receive(void *packet, int length)
void eth_try_another(int first_restart) { - static struct eth_device *first_failed = NULL; + static struct eth_device *first_failed; char *ethrotate;
/* * Do not rotate between network interfaces when * 'ethrotate' variable is set to 'no'. */ - if (((ethrotate = getenv ("ethrotate")) != NULL) && - (strcmp(ethrotate, "no") == 0)) + ethrotate = getenv("ethrotate"); + if ((ethrotate != NULL) && (strcmp(ethrotate, "no") == 0)) return;
if (!eth_current) return;
- if (first_restart) { + if (first_restart) first_failed = eth_current; - }
eth_current = eth_current->next;
eth_current_changed();
- if (first_failed == eth_current) { + if (first_failed == eth_current) NetRestartWrap = 1; - } }
void eth_set_current(void) { - static char *act = NULL; - static int env_changed_id = 0; - struct eth_device* old_current; + static char *act; + static int env_changed_id; + struct eth_device *old_current; int env_id;
if (!eth_current) /* XXX no current */ @@ -524,7 +521,7 @@ void eth_set_current(void) eth_current_changed(); }
-char *eth_get_name (void) +char *eth_get_name(void) { - return (eth_current ? eth_current->name : "unknown"); + return eth_current ? eth_current->name : "unknown"; }

Signed-off-by: Joe Hershberger joe.hershberger@ni.com Cc: Joe Hershberger joe.hershberger@gmail.com --- Changes for v2: - Split into smaller patches Changes for v3:
common/main.c | 1 - include/common.h | 4 ++++ include/net.h | 5 +++++ net/net.c | 22 ++++++++-------------- 4 files changed, 17 insertions(+), 15 deletions(-)
diff --git a/common/main.c b/common/main.c index 3b9e39a..a933357 100644 --- a/common/main.c +++ b/common/main.c @@ -973,7 +973,6 @@ int readline_into_buffer(const char *const prompt, char *buffer, int timeout)
#ifdef CONFIG_SHOW_ACTIVITY while (!tstc()) { - extern void show_activity(int arg); show_activity(0); WATCHDOG_RESET(); } diff --git a/include/common.h b/include/common.h index 4b5841e..7338e39 100644 --- a/include/common.h +++ b/include/common.h @@ -794,6 +794,10 @@ int pcmcia_init (void);
#include <bootstage.h>
+#ifdef CONFIG_SHOW_ACTIVITY +void show_activity(int arg); +#endif + /* Multicore arch functions */ #ifdef CONFIG_MP int cpu_status(int nr); diff --git a/include/net.h b/include/net.h index 2b6e59a..3dbe154 100644 --- a/include/net.h +++ b/include/net.h @@ -433,6 +433,11 @@ extern int NetSendUDPPacket(uchar *ether, IPaddr_t dest, int dport, /* Processes a received packet */ extern void NetReceive(uchar *, int);
+#ifdef CONFIG_NETCONSOLE +void NcStart(void); +int nc_input_packet(uchar *pkt, unsigned dest, unsigned src, unsigned len); +#endif + /* * Check if autoload is enabled. If so, use either NFS or TFTP to download * the boot file. diff --git a/net/net.c b/net/net.c index 9a0417d..1435245 100644 --- a/net/net.c +++ b/net/net.c @@ -77,6 +77,7 @@ #include <common.h> #include <watchdog.h> #include <command.h> +#include <linux/compiler.h> #include <net.h> #include "bootp.h" #include "tftp.h" @@ -203,11 +204,6 @@ IPaddr_t NetNtpServerIP; int NetTimeOffset; #endif
-#ifdef CONFIG_NETCONSOLE -void NcStart(void); -int nc_input_packet(uchar *pkt, unsigned dest, unsigned src, unsigned len); -#endif - uchar PktBuf[(PKTBUFSRX+1) * PKTSIZE_ALIGN + PKTALIGN];
/* Receive packet */ @@ -527,10 +523,7 @@ restart: for (;;) { WATCHDOG_RESET(); #ifdef CONFIG_SHOW_ACTIVITY - { - extern void show_activity(int arg); - show_activity(1); - } + show_activity(1); #endif /* * Check the ethernet for a new packet. The ethernet @@ -1103,8 +1096,8 @@ CDPHandler(const uchar *pkt, unsigned len) * output a warning */ if (pkt[0] != 0x02) - printf("** WARNING: CDP packet received with a protocol version %d > 2\n", - pkt[0] & 0xff); + printf("**WARNING: CDP packet received with a protocol version " + "%d > 2\n", pkt[0] & 0xff);
if (CDP_compute_csum(pkt, len) != 0) return; @@ -1239,7 +1232,7 @@ struct hole {
static IP_t *__NetDefragment(IP_t *ip, int *lenp) { - static uchar pkt_buff[IP_PKTSIZE] __attribute__((aligned(PKTALIGN))); + static uchar pkt_buff[IP_PKTSIZE] __aligned(PKTALIGN); static u16 first_hole, total_len; struct hole *payload, *thisfrag, *h, *newh; IP_t *localip = (IP_t *)pkt_buff; @@ -1611,6 +1604,7 @@ NetReceive(uchar *inpkt, int len) /* matched waiting packet's address */ if (tmp == NetArpWaitReplyIP) { debug("Got it\n"); + /* save address for later use */ memcpy(NetArpWaitPacketMAC, &arp->ar_data[0], 6); @@ -1619,7 +1613,8 @@ NetReceive(uchar *inpkt, int len) (*packetHandler)(0, 0, 0, 0, 0); #endif /* modify header, and transmit it */ - memcpy(((Ethernet_t *)NetArpWaitTxPacket)->et_dest, NetArpWaitPacketMAC, 6); + memcpy(((Ethernet_t *)NetArpWaitTxPacket)-> + et_dest, NetArpWaitPacketMAC, 6); (void) eth_send(NetArpWaitTxPacket, NetArpWaitTxPacketSize);
@@ -1856,7 +1851,6 @@ common: case CDP: case DHCP: if (memcmp(NetOurEther, "\0\0\0\0\0\0", 6) == 0) { - extern int eth_get_dev_index(void); int num = eth_get_dev_index();
switch (num) {

Signed-off-by: Joe Hershberger joe.hershberger@ni.com Cc: Joe Hershberger joe.hershberger@gmail.com --- Changes for v2: - Split into smaller patches Changes for v3:
net/nfs.c | 300 ++++++++++++++++++++++++++++++------------------------------- net/nfs.h | 2 +- 2 files changed, 150 insertions(+), 152 deletions(-)
diff --git a/net/nfs.c b/net/nfs.c index b5b482c..54f56c4 100644 --- a/net/nfs.c +++ b/net/nfs.c @@ -33,8 +33,8 @@ #define NFS_RETRY_COUNT 30 #define NFS_TIMEOUT 2000UL
-static int fs_mounted = 0; -static unsigned long rpc_id = 0; +static int fs_mounted; +static unsigned long rpc_id; static int nfs_offset = -1; static int nfs_len;
@@ -61,14 +61,14 @@ static char *nfs_filename; static char *nfs_path; static char nfs_path_buff[2048];
-static __inline__ int -store_block (uchar * src, unsigned offset, unsigned len) +static inline int +store_block(uchar *src, unsigned offset, unsigned len) { ulong newsize = offset + len; #ifdef CONFIG_SYS_DIRECT_FLASH_NFS int i, rc = 0;
- for (i=0; i<CONFIG_SYS_MAX_FLASH_BANKS; i++) { + for (i = 0; i < CONFIG_SYS_MAX_FLASH_BANKS; i++) { /* start address in flash? */ if (load_addr + offset >= flash_info[i].start[0]) { rc = 1; @@ -77,15 +77,15 @@ store_block (uchar * src, unsigned offset, unsigned len) }
if (rc) { /* Flash is destination for this packet */ - rc = flash_write ((uchar *)src, (ulong)(load_addr+offset), len); + rc = flash_write((uchar *)src, (ulong)(load_addr+offset), len); if (rc) { - flash_perror (rc); + flash_perror(rc); return -1; } } else #endif /* CONFIG_SYS_DIRECT_FLASH_NFS */ { - (void)memcpy ((void *)(load_addr + offset), src, len); + (void)memcpy((void *)(load_addr + offset), src, len); }
if (NetBootFileXferSize < (offset+len)) @@ -94,7 +94,7 @@ store_block (uchar * src, unsigned offset, unsigned len) }
static char* -basename (char *path) +basename(char *path) { char *fname;
@@ -110,11 +110,11 @@ basename (char *path) }
static char* -dirname (char *path) +dirname(char *path) { char *fname;
- fname = basename (path); + fname = basename(path); --fname; *fname = '\0'; return path; @@ -123,14 +123,14 @@ dirname (char *path) /************************************************************************** RPC_ADD_CREDENTIALS - Add RPC authentication/verifier entries **************************************************************************/ -static long *rpc_add_credentials (long *p) +static long *rpc_add_credentials(long *p) { int hl; int hostnamelen; char hostname[256];
- strcpy (hostname, ""); - hostnamelen=strlen (hostname); + strcpy(hostname, ""); + hostnamelen = strlen(hostname);
/* Here's the executive summary on authentication requirements of the * various NFS server implementations: Linux accepts both AUTH_NONE @@ -148,10 +148,9 @@ static long *rpc_add_credentials (long *p) *p++ = htonl(hl+20); /* auth length */ *p++ = htonl(0); /* stamp */ *p++ = htonl(hostnamelen); /* hostname string */ - if (hostnamelen & 3) { + if (hostnamelen & 3) *(p + hostnamelen / 4) = 0; /* add zero padding */ - } - memcpy (p, hostname, hostnamelen); + memcpy(p, hostname, hostnamelen); p += hl / 4; *p++ = 0; /* uid */ *p++ = 0; /* gid */ @@ -168,7 +167,7 @@ static long *rpc_add_credentials (long *p) RPC_LOOKUP - Lookup RPC Port numbers **************************************************************************/ static void -rpc_req (int rpc_prog, int rpc_proc, uint32_t *data, int datalen) +rpc_req(int rpc_prog, int rpc_proc, uint32_t *data, int datalen) { struct rpc_t pkt; unsigned long id; @@ -186,11 +185,12 @@ rpc_req (int rpc_prog, int rpc_proc, uint32_t *data, int datalen) p = (uint32_t *)&(pkt.u.call.data);
if (datalen) - memcpy ((char *)p, (char *)data, datalen*sizeof(uint32_t)); + memcpy((char *)p, (char *)data, datalen*sizeof(uint32_t));
pktlen = (char *)p + datalen*sizeof(uint32_t) - (char *)&pkt;
- memcpy ((char *)NetTxPacket + NetEthHdrSize() + IP_HDR_SIZE, (char *)&pkt, pktlen); + memcpy((char *)NetTxPacket + NetEthHdrSize() + IP_HDR_SIZE, + (char *)&pkt, pktlen);
if (rpc_prog == PROG_PORTMAP) sport = SUNRPC_PORT; @@ -199,14 +199,15 @@ rpc_req (int rpc_prog, int rpc_proc, uint32_t *data, int datalen) else sport = NfsSrvNfsPort;
- NetSendUDPPacket (NetServerEther, NfsServerIP, sport, NfsOurPort, pktlen); + NetSendUDPPacket(NetServerEther, NfsServerIP, sport, NfsOurPort, + pktlen); }
/************************************************************************** RPC_LOOKUP - Lookup RPC Port numbers **************************************************************************/ static void -rpc_lookup_req (int prog, int ver) +rpc_lookup_req(int prog, int ver) { uint32_t data[16];
@@ -217,56 +218,56 @@ rpc_lookup_req (int prog, int ver) data[6] = htonl(17); /* IP_UDP */ data[7] = 0;
- rpc_req (PROG_PORTMAP, PORTMAP_GETPORT, data, 8); + rpc_req(PROG_PORTMAP, PORTMAP_GETPORT, data, 8); }
/************************************************************************** NFS_MOUNT - Mount an NFS Filesystem **************************************************************************/ static void -nfs_mount_req (char *path) +nfs_mount_req(char *path) { uint32_t data[1024]; uint32_t *p; int len; int pathlen;
- pathlen = strlen (path); + pathlen = strlen(path);
p = &(data[0]); p = (uint32_t *)rpc_add_credentials((long *)p);
*p++ = htonl(pathlen); - if (pathlen & 3) *(p + pathlen / 4) = 0; - memcpy (p, path, pathlen); + if (pathlen & 3) + *(p + pathlen / 4) = 0; + memcpy(p, path, pathlen); p += (pathlen + 3) / 4;
len = (uint32_t *)p - (uint32_t *)&(data[0]);
- rpc_req (PROG_MOUNT, MOUNT_ADDENTRY, data, len); + rpc_req(PROG_MOUNT, MOUNT_ADDENTRY, data, len); }
/************************************************************************** NFS_UMOUNTALL - Unmount all our NFS Filesystems on the Server **************************************************************************/ static void -nfs_umountall_req (void) +nfs_umountall_req(void) { uint32_t data[1024]; uint32_t *p; int len;
- if ((NfsSrvMountPort == -1) || (!fs_mounted)) { + if ((NfsSrvMountPort == -1) || (!fs_mounted)) /* Nothing mounted, nothing to umount */ return; - }
p = &(data[0]); - p = (uint32_t *)rpc_add_credentials ((long *)p); + p = (uint32_t *)rpc_add_credentials((long *)p);
len = (uint32_t *)p - (uint32_t *)&(data[0]);
- rpc_req (PROG_MOUNT, MOUNT_UMOUNTALL, data, len); + rpc_req(PROG_MOUNT, MOUNT_UMOUNTALL, data, len); }
/*************************************************************************** @@ -277,65 +278,66 @@ nfs_umountall_req (void) * so that inside the nfs() function a recursion can be done. **************************************************************************/ static void -nfs_readlink_req (void) +nfs_readlink_req(void) { uint32_t data[1024]; uint32_t *p; int len;
p = &(data[0]); - p = (uint32_t *)rpc_add_credentials ((long *)p); + p = (uint32_t *)rpc_add_credentials((long *)p);
- memcpy (p, filefh, NFS_FHSIZE); + memcpy(p, filefh, NFS_FHSIZE); p += (NFS_FHSIZE / 4);
len = (uint32_t *)p - (uint32_t *)&(data[0]);
- rpc_req (PROG_NFS, NFS_READLINK, data, len); + rpc_req(PROG_NFS, NFS_READLINK, data, len); }
/************************************************************************** NFS_LOOKUP - Lookup Pathname **************************************************************************/ static void -nfs_lookup_req (char *fname) +nfs_lookup_req(char *fname) { uint32_t data[1024]; uint32_t *p; int len; int fnamelen;
- fnamelen = strlen (fname); + fnamelen = strlen(fname);
p = &(data[0]); - p = (uint32_t *)rpc_add_credentials ((long *)p); + p = (uint32_t *)rpc_add_credentials((long *)p);
- memcpy (p, dirfh, NFS_FHSIZE); + memcpy(p, dirfh, NFS_FHSIZE); p += (NFS_FHSIZE / 4); *p++ = htonl(fnamelen); - if (fnamelen & 3) *(p + fnamelen / 4) = 0; - memcpy (p, fname, fnamelen); + if (fnamelen & 3) + *(p + fnamelen / 4) = 0; + memcpy(p, fname, fnamelen); p += (fnamelen + 3) / 4;
len = (uint32_t *)p - (uint32_t *)&(data[0]);
- rpc_req (PROG_NFS, NFS_LOOKUP, data, len); + rpc_req(PROG_NFS, NFS_LOOKUP, data, len); }
/************************************************************************** NFS_READ - Read File on NFS Server **************************************************************************/ static void -nfs_read_req (int offset, int readlen) +nfs_read_req(int offset, int readlen) { uint32_t data[1024]; uint32_t *p; int len;
p = &(data[0]); - p = (uint32_t *)rpc_add_credentials ((long *)p); + p = (uint32_t *)rpc_add_credentials((long *)p);
- memcpy (p, filefh, NFS_FHSIZE); + memcpy(p, filefh, NFS_FHSIZE); p += (NFS_FHSIZE / 4); *p++ = htonl(offset); *p++ = htonl(readlen); @@ -343,7 +345,7 @@ nfs_read_req (int offset, int readlen)
len = (uint32_t *)p - (uint32_t *)&(data[0]);
- rpc_req (PROG_NFS, NFS_READ, data, len); + rpc_req(PROG_NFS, NFS_READ, data, len); }
/************************************************************************** @@ -351,31 +353,31 @@ RPC request dispatcher **************************************************************************/
static void -NfsSend (void) +NfsSend(void) { debug("%s\n", __func__);
switch (NfsState) { case STATE_PRCLOOKUP_PROG_MOUNT_REQ: - rpc_lookup_req (PROG_MOUNT, 1); + rpc_lookup_req(PROG_MOUNT, 1); break; case STATE_PRCLOOKUP_PROG_NFS_REQ: - rpc_lookup_req (PROG_NFS, 2); + rpc_lookup_req(PROG_NFS, 2); break; case STATE_MOUNT_REQ: - nfs_mount_req (nfs_path); + nfs_mount_req(nfs_path); break; case STATE_UMOUNT_REQ: - nfs_umountall_req (); + nfs_umountall_req(); break; case STATE_LOOKUP_REQ: - nfs_lookup_req (nfs_filename); + nfs_lookup_req(nfs_filename); break; case STATE_READ_REQ: - nfs_read_req (nfs_offset, nfs_len); + nfs_read_req(nfs_offset, nfs_len); break; case STATE_READLINK_REQ: - nfs_readlink_req (); + nfs_readlink_req(); break; } } @@ -385,11 +387,11 @@ Handlers for the reply from server **************************************************************************/
static int -rpc_lookup_reply (int prog, uchar *pkt, unsigned len) +rpc_lookup_reply(int prog, uchar *pkt, unsigned len) { struct rpc_t rpc_pkt;
- memcpy ((unsigned char *)&rpc_pkt, pkt, len); + memcpy((unsigned char *)&rpc_pkt, pkt, len);
debug("%s\n", __func__);
@@ -398,9 +400,8 @@ rpc_lookup_reply (int prog, uchar *pkt, unsigned len)
if (rpc_pkt.u.reply.rstatus || rpc_pkt.u.reply.verifier || - rpc_pkt.u.reply.astatus) { + rpc_pkt.u.reply.astatus) return -1; - }
switch (prog) { case PROG_MOUNT: @@ -415,13 +416,13 @@ rpc_lookup_reply (int prog, uchar *pkt, unsigned len) }
static int -nfs_mount_reply (uchar *pkt, unsigned len) +nfs_mount_reply(uchar *pkt, unsigned len) { struct rpc_t rpc_pkt;
debug("%s\n", __func__);
- memcpy ((unsigned char *)&rpc_pkt, pkt, len); + memcpy((unsigned char *)&rpc_pkt, pkt, len);
if (ntohl(rpc_pkt.u.reply.id) != rpc_id) return -1; @@ -429,48 +430,46 @@ nfs_mount_reply (uchar *pkt, unsigned len) if (rpc_pkt.u.reply.rstatus || rpc_pkt.u.reply.verifier || rpc_pkt.u.reply.astatus || - rpc_pkt.u.reply.data[0]) { + rpc_pkt.u.reply.data[0]) return -1; - }
fs_mounted = 1; - memcpy (dirfh, rpc_pkt.u.reply.data + 1, NFS_FHSIZE); + memcpy(dirfh, rpc_pkt.u.reply.data + 1, NFS_FHSIZE);
return 0; }
static int -nfs_umountall_reply (uchar *pkt, unsigned len) +nfs_umountall_reply(uchar *pkt, unsigned len) { struct rpc_t rpc_pkt;
debug("%s\n", __func__);
- memcpy ((unsigned char *)&rpc_pkt, pkt, len); + memcpy((unsigned char *)&rpc_pkt, pkt, len);
if (ntohl(rpc_pkt.u.reply.id) != rpc_id) return -1;
if (rpc_pkt.u.reply.rstatus || rpc_pkt.u.reply.verifier || - rpc_pkt.u.reply.astatus) { + rpc_pkt.u.reply.astatus) return -1; - }
fs_mounted = 0; - memset (dirfh, 0, sizeof(dirfh)); + memset(dirfh, 0, sizeof(dirfh));
return 0; }
static int -nfs_lookup_reply (uchar *pkt, unsigned len) +nfs_lookup_reply(uchar *pkt, unsigned len) { struct rpc_t rpc_pkt;
debug("%s\n", __func__);
- memcpy ((unsigned char *)&rpc_pkt, pkt, len); + memcpy((unsigned char *)&rpc_pkt, pkt, len);
if (ntohl(rpc_pkt.u.reply.id) != rpc_id) return -1; @@ -478,24 +477,23 @@ nfs_lookup_reply (uchar *pkt, unsigned len) if (rpc_pkt.u.reply.rstatus || rpc_pkt.u.reply.verifier || rpc_pkt.u.reply.astatus || - rpc_pkt.u.reply.data[0]) { + rpc_pkt.u.reply.data[0]) return -1; - }
- memcpy (filefh, rpc_pkt.u.reply.data + 1, NFS_FHSIZE); + memcpy(filefh, rpc_pkt.u.reply.data + 1, NFS_FHSIZE);
return 0; }
static int -nfs_readlink_reply (uchar *pkt, unsigned len) +nfs_readlink_reply(uchar *pkt, unsigned len) { struct rpc_t rpc_pkt; int rlen;
debug("%s\n", __func__);
- memcpy ((unsigned char *)&rpc_pkt, pkt, len); + memcpy((unsigned char *)&rpc_pkt, pkt, len);
if (ntohl(rpc_pkt.u.reply.id) != rpc_id) return -1; @@ -503,34 +501,34 @@ nfs_readlink_reply (uchar *pkt, unsigned len) if (rpc_pkt.u.reply.rstatus || rpc_pkt.u.reply.verifier || rpc_pkt.u.reply.astatus || - rpc_pkt.u.reply.data[0]) { + rpc_pkt.u.reply.data[0]) return -1; - }
- rlen = ntohl (rpc_pkt.u.reply.data[1]); /* new path length */ + rlen = ntohl(rpc_pkt.u.reply.data[1]); /* new path length */
if (*((char *)&(rpc_pkt.u.reply.data[2])) != '/') { int pathlen; - strcat (nfs_path, "/"); + strcat(nfs_path, "/"); pathlen = strlen(nfs_path); - memcpy (nfs_path+pathlen, (uchar *)&(rpc_pkt.u.reply.data[2]), rlen); + memcpy(nfs_path + pathlen, (uchar *)&(rpc_pkt.u.reply.data[2]), + rlen); nfs_path[pathlen + rlen] = 0; } else { - memcpy (nfs_path, (uchar *)&(rpc_pkt.u.reply.data[2]), rlen); + memcpy(nfs_path, (uchar *)&(rpc_pkt.u.reply.data[2]), rlen); nfs_path[rlen] = 0; } return 0; }
static int -nfs_read_reply (uchar *pkt, unsigned len) +nfs_read_reply(uchar *pkt, unsigned len) { struct rpc_t rpc_pkt; int rlen;
debug("%s\n", __func__);
- memcpy ((uchar *)&rpc_pkt, pkt, sizeof(rpc_pkt.u.reply)); + memcpy((uchar *)&rpc_pkt, pkt, sizeof(rpc_pkt.u.reply));
if (ntohl(rpc_pkt.u.reply.id) != rpc_id) return -1; @@ -539,24 +537,22 @@ nfs_read_reply (uchar *pkt, unsigned len) rpc_pkt.u.reply.verifier || rpc_pkt.u.reply.astatus || rpc_pkt.u.reply.data[0]) { - if (rpc_pkt.u.reply.rstatus) { + if (rpc_pkt.u.reply.rstatus) return -9999; - } - if (rpc_pkt.u.reply.astatus) { + if (rpc_pkt.u.reply.astatus) return -9999; - } - return -ntohl(rpc_pkt.u.reply.data[0]);; + return -ntohl(rpc_pkt.u.reply.data[0]); }
- if ((nfs_offset!=0) && !((nfs_offset) % (NFS_READ_SIZE/2*10*HASHES_PER_LINE))) { - puts ("\n\t "); - } - if (!(nfs_offset % ((NFS_READ_SIZE/2)*10))) { - putc ('#'); - } + if ((nfs_offset != 0) && !((nfs_offset) % + (NFS_READ_SIZE / 2 * 10 * HASHES_PER_LINE))) + puts("\n\t "); + if (!(nfs_offset % ((NFS_READ_SIZE / 2) * 10))) + putc('#');
rlen = ntohl(rpc_pkt.u.reply.data[18]); - if ( store_block ((uchar *)pkt+sizeof(rpc_pkt.u.reply), nfs_offset, rlen) ) + if (store_block((uchar *)pkt + sizeof(rpc_pkt.u.reply), + nfs_offset, rlen)) return -9999;
return rlen; @@ -567,15 +563,15 @@ Interfaces of U-BOOT **************************************************************************/
static void -NfsTimeout (void) +NfsTimeout(void) { - if ( ++NfsTimeoutCount > NFS_RETRY_COUNT ) { - puts ("\nRetry count exceeded; starting again\n"); - NetStartAgain (); + if (++NfsTimeoutCount > NFS_RETRY_COUNT) { + puts("\nRetry count exceeded; starting again\n"); + NetStartAgain(); } else { puts("T "); - NetSetTimeout (NFS_TIMEOUT, NfsTimeout); - NfsSend (); + NetSetTimeout(NFS_TIMEOUT, NfsTimeout); + NfsSend(); } }
@@ -586,86 +582,87 @@ NfsHandler(uchar *pkt, unsigned dest, IPaddr_t sip, unsigned src, unsigned len)
debug("%s\n", __func__);
- if (dest != NfsOurPort) return; + if (dest != NfsOurPort) + return;
switch (NfsState) { case STATE_PRCLOOKUP_PROG_MOUNT_REQ: - rpc_lookup_reply (PROG_MOUNT, pkt, len); + rpc_lookup_reply(PROG_MOUNT, pkt, len); NfsState = STATE_PRCLOOKUP_PROG_NFS_REQ; - NfsSend (); + NfsSend(); break;
case STATE_PRCLOOKUP_PROG_NFS_REQ: - rpc_lookup_reply (PROG_NFS, pkt, len); + rpc_lookup_reply(PROG_NFS, pkt, len); NfsState = STATE_MOUNT_REQ; - NfsSend (); + NfsSend(); break;
case STATE_MOUNT_REQ: if (nfs_mount_reply(pkt, len)) { - puts ("*** ERROR: Cannot mount\n"); + puts("*** ERROR: Cannot mount\n"); /* just to be sure... */ NfsState = STATE_UMOUNT_REQ; - NfsSend (); + NfsSend(); } else { NfsState = STATE_LOOKUP_REQ; - NfsSend (); + NfsSend(); } break;
case STATE_UMOUNT_REQ: if (nfs_umountall_reply(pkt, len)) { - puts ("*** ERROR: Cannot umount\n"); + puts("*** ERROR: Cannot umount\n"); NetState = NETLOOP_FAIL; } else { - puts ("\ndone\n"); + puts("\ndone\n"); NetState = NfsDownloadState; } break;
case STATE_LOOKUP_REQ: if (nfs_lookup_reply(pkt, len)) { - puts ("*** ERROR: File lookup fail\n"); + puts("*** ERROR: File lookup fail\n"); NfsState = STATE_UMOUNT_REQ; - NfsSend (); + NfsSend(); } else { NfsState = STATE_READ_REQ; nfs_offset = 0; nfs_len = NFS_READ_SIZE; - NfsSend (); + NfsSend(); } break;
case STATE_READLINK_REQ: if (nfs_readlink_reply(pkt, len)) { - puts ("*** ERROR: Symlink fail\n"); + puts("*** ERROR: Symlink fail\n"); NfsState = STATE_UMOUNT_REQ; - NfsSend (); + NfsSend(); } else { debug("Symlink --> %s\n", nfs_path); - nfs_filename = basename (nfs_path); - nfs_path = dirname (nfs_path); + nfs_filename = basename(nfs_path); + nfs_path = dirname(nfs_path);
NfsState = STATE_MOUNT_REQ; - NfsSend (); + NfsSend(); } break;
case STATE_READ_REQ: - rlen = nfs_read_reply (pkt, len); - NetSetTimeout (NFS_TIMEOUT, NfsTimeout); + rlen = nfs_read_reply(pkt, len); + NetSetTimeout(NFS_TIMEOUT, NfsTimeout); if (rlen > 0) { nfs_offset += rlen; - NfsSend (); - } - else if ((rlen == -NFSERR_ISDIR)||(rlen == -NFSERR_INVAL)) { + NfsSend(); + } else if ((rlen == -NFSERR_ISDIR) || (rlen == -NFSERR_INVAL)) { /* symbolic link */ NfsState = STATE_READLINK_REQ; - NfsSend (); + NfsSend(); } else { - if ( ! rlen ) NfsDownloadState = NETLOOP_SUCCESS; + if (!rlen) + NfsDownloadState = NETLOOP_SUCCESS; NfsState = STATE_UMOUNT_REQ; - NfsSend (); + NfsSend(); } break; } @@ -673,7 +670,7 @@ NfsHandler(uchar *pkt, unsigned dest, IPaddr_t sip, unsigned src, unsigned len)
void -NfsStart (void) +NfsStart(void) { debug("%s\n", __func__); NfsDownloadState = NETLOOP_FAIL; @@ -683,7 +680,7 @@ NfsStart (void)
if (nfs_path == NULL) { NetState = NETLOOP_FAIL; - puts ("*** ERROR: Fail allocate memory\n"); + puts("*** ERROR: Fail allocate memory\n"); return; }
@@ -692,29 +689,29 @@ NfsStart (void) NetOurIP & 0xFF, (NetOurIP >> 8) & 0xFF, (NetOurIP >> 16) & 0xFF, - (NetOurIP >> 24) & 0xFF ); - strcpy (nfs_path, default_filename); + (NetOurIP >> 24) & 0xFF); + strcpy(nfs_path, default_filename);
- printf ("*** Warning: no boot file name; using '%s'\n", + printf("*** Warning: no boot file name; using '%s'\n", nfs_path); } else { - char *p=BootFile; + char *p = BootFile;
- p = strchr (p, ':'); + p = strchr(p, ':');
if (p != NULL) { - NfsServerIP = string_to_ip (BootFile); + NfsServerIP = string_to_ip(BootFile); ++p; - strcpy (nfs_path, p); + strcpy(nfs_path, p); } else { - strcpy (nfs_path, BootFile); + strcpy(nfs_path, BootFile); } }
- nfs_filename = basename (nfs_path); - nfs_path = dirname (nfs_path); + nfs_filename = basename(nfs_path); + nfs_path = dirname(nfs_path);
- printf ("Using %s device\n", eth_get_name()); + printf("Using %s device\n", eth_get_name());
printf("File transfer via NFS from server %pI4" "; our IP address is %pI4", &NfsServerIP, &NetOurIP); @@ -725,19 +722,20 @@ NfsStart (void) IPaddr_t ServerNet = NetServerIP & NetOurSubnetMask;
if (OurNet != ServerNet) - printf("; sending through gateway %pI4", &NetOurGatewayIP); + printf("; sending through gateway %pI4", + &NetOurGatewayIP); } - printf ("\nFilename '%s/%s'.", nfs_path, nfs_filename); + printf("\nFilename '%s/%s'.", nfs_path, nfs_filename);
if (NetBootFileSize) { - printf (" Size is 0x%x Bytes = ", NetBootFileSize<<9); - print_size (NetBootFileSize<<9, ""); + printf(" Size is 0x%x Bytes = ", NetBootFileSize<<9); + print_size(NetBootFileSize<<9, ""); } - printf ("\nLoad address: 0x%lx\n" + printf("\nLoad address: 0x%lx\n" "Loading: *\b", load_addr);
- NetSetTimeout (NFS_TIMEOUT, NfsTimeout); - NetSetHandler (NfsHandler); + NetSetTimeout(NFS_TIMEOUT, NfsTimeout); + NetSetHandler(NfsHandler);
NfsTimeoutCount = 0; NfsState = STATE_PRCLOOKUP_PROG_MOUNT_REQ; @@ -747,7 +745,7 @@ NfsStart (void) NfsOurPort = 1000;
/* zero out server ether in case the server ip has changed */ - memset (NetServerEther, 0, 6); + memset(NetServerEther, 0, 6);
- NfsSend (); + NfsSend(); } diff --git a/net/nfs.h b/net/nfs.h index de8a0c6..a5a1b43 100644 --- a/net/nfs.h +++ b/net/nfs.h @@ -72,7 +72,7 @@ struct rpc_t { } reply; } u; }; -extern void NfsStart (void); /* Begin NFS */ +extern void NfsStart(void); /* Begin NFS */
/**********************************************************************/

Signed-off-by: Joe Hershberger joe.hershberger@ni.com Cc: Joe Hershberger joe.hershberger@gmail.com --- Changes for v2: - Split into smaller patches Changes for v3:
net/rarp.c | 28 ++++++++++++++-------------- net/rarp.h | 4 ++-- 2 files changed, 16 insertions(+), 16 deletions(-)
diff --git a/net/rarp.c b/net/rarp.c index 77d63e8..5a813a2 100644 --- a/net/rarp.c +++ b/net/rarp.c @@ -58,21 +58,21 @@ static void RarpTimeout(void) { if (RarpTry >= TIMEOUT_COUNT) { - puts ("\nRetry count exceeded; starting again\n"); - NetStartAgain (); + puts("\nRetry count exceeded; starting again\n"); + NetStartAgain(); } else { - NetSetTimeout (TIMEOUT, RarpTimeout); - RarpRequest (); + NetSetTimeout(TIMEOUT, RarpTimeout); + RarpRequest(); } }
void -RarpRequest (void) +RarpRequest(void) { int i; uchar *pkt; - ARP_t * rarp; + ARP_t *rarp;
printf("RARP broadcast %d\n", ++RarpTry); pkt = NetTxPacket; @@ -81,18 +81,18 @@ RarpRequest (void)
rarp = (ARP_t *)pkt;
- rarp->ar_hrd = htons (ARP_ETHER); - rarp->ar_pro = htons (PROT_IP); + rarp->ar_hrd = htons(ARP_ETHER); + rarp->ar_pro = htons(PROT_IP); rarp->ar_hln = 6; rarp->ar_pln = 4; - rarp->ar_op = htons (RARPOP_REQUEST); - memcpy (&rarp->ar_data[0], NetOurEther, 6); /* source ET addr */ - memcpy (&rarp->ar_data[6], &NetOurIP, 4); /* source IP addr */ - memcpy (&rarp->ar_data[10], NetOurEther, 6); /* dest ET addr = source ET addr ??*/ + rarp->ar_op = htons(RARPOP_REQUEST); + memcpy(&rarp->ar_data[0], NetOurEther, 6); /* source ET addr */ + memcpy(&rarp->ar_data[6], &NetOurIP, 4); /* source IP addr */ + /* dest ET addr = source ET addr ??*/ + memcpy(&rarp->ar_data[10], NetOurEther, 6); /* dest. IP addr set to broadcast */ - for (i = 0; i <= 3; i++) { + for (i = 0; i <= 3; i++) rarp->ar_data[16 + i] = 0xff; - }
NetSendPacket(NetTxPacket, (pkt - NetTxPacket) + ARP_HDR_SIZE);
diff --git a/net/rarp.h b/net/rarp.h index 0c16d46..4e92d80 100644 --- a/net/rarp.h +++ b/net/rarp.h @@ -35,9 +35,9 @@ * Global functions and variables. */
-extern int RarpTry; +extern int RarpTry;
-extern void RarpRequest (void); /* Send a RARP request */ +extern void RarpRequest(void); /* Send a RARP request */
/**********************************************************************/

Signed-off-by: Joe Hershberger joe.hershberger@ni.com Cc: Joe Hershberger joe.hershberger@gmail.com --- Changes for v2: - Split into smaller patches Changes for v3: - Changed literal to sizeof() for Simon Glass
net/sntp.c | 31 +++++++++++++++++-------------- net/sntp.h | 2 +- 2 files changed, 18 insertions(+), 15 deletions(-)
diff --git a/net/sntp.c b/net/sntp.c index 82f2fe6..69cddb1 100644 --- a/net/sntp.c +++ b/net/sntp.c @@ -17,7 +17,7 @@ static int SntpOurPort;
static void -SntpSend (void) +SntpSend(void) { struct sntp_pkt_t pkt; int pktlen = SNTP_PACKET_LEN; @@ -25,24 +25,26 @@ SntpSend (void)
debug("%s\n", __func__);
- memset (&pkt, 0, sizeof(pkt)); + memset(&pkt, 0, sizeof(pkt));
pkt.li = NTP_LI_NOLEAP; pkt.vn = NTP_VERSION; pkt.mode = NTP_MODE_CLIENT;
- memcpy ((char *)NetTxPacket + NetEthHdrSize() + IP_HDR_SIZE, (char *)&pkt, pktlen); + memcpy((char *)NetTxPacket + NetEthHdrSize() + IP_HDR_SIZE, + (char *)&pkt, pktlen);
SntpOurPort = 10000 + (get_timer(0) % 4096); sport = NTP_SERVICE_PORT;
- NetSendUDPPacket (NetServerEther, NetNtpServerIP, sport, SntpOurPort, pktlen); + NetSendUDPPacket(NetServerEther, NetNtpServerIP, sport, SntpOurPort, + pktlen); }
static void -SntpTimeout (void) +SntpTimeout(void) { - puts ("Timeout\n"); + puts("Timeout\n"); NetState = NETLOOP_FAIL; return; } @@ -57,19 +59,20 @@ SntpHandler(uchar *pkt, unsigned dest, IPaddr_t sip, unsigned src,
debug("%s\n", __func__);
- if (dest != SntpOurPort) return; + if (dest != SntpOurPort) + return;
/* * As the RTC's used in U-Boot sepport second resolution only * we simply ignore the sub-second field. */ - memcpy (&seconds, &rpktp->transmit_timestamp, sizeof(ulong)); + memcpy(&seconds, &rpktp->transmit_timestamp, sizeof(ulong));
to_tm(ntohl(seconds) - 2208988800UL + NetTimeOffset, &tm); #if defined(CONFIG_CMD_DATE) - rtc_set (&tm); + rtc_set(&tm); #endif - printf ("Date: %4d-%02d-%02d Time: %2d:%02d:%02d\n", + printf("Date: %4d-%02d-%02d Time: %2d:%02d:%02d\n", tm.tm_year, tm.tm_mon, tm.tm_mday, tm.tm_hour, tm.tm_min, tm.tm_sec);
@@ -77,13 +80,13 @@ SntpHandler(uchar *pkt, unsigned dest, IPaddr_t sip, unsigned src, }
void -SntpStart (void) +SntpStart(void) { debug("%s\n", __func__);
- NetSetTimeout (SNTP_TIMEOUT, SntpTimeout); + NetSetTimeout(SNTP_TIMEOUT, SntpTimeout); NetSetHandler(SntpHandler); - memset (NetServerEther, 0, 6); + memset(NetServerEther, 0, sizeof(NetServerEther));
- SntpSend (); + SntpSend(); } diff --git a/net/sntp.h b/net/sntp.h index 8a097bf..1d0046e 100644 --- a/net/sntp.h +++ b/net/sntp.h @@ -56,6 +56,6 @@ struct sntp_pkt_t { unsigned long long transmit_timestamp; };
-extern void SntpStart (void); /* Begin SNTP */ +extern void SntpStart(void); /* Begin SNTP */
#endif /* __SNTP_H__ */

Signed-off-by: Joe Hershberger joe.hershberger@ni.com Cc: Joe Hershberger joe.hershberger@gmail.com --- Changes for v2: - Split into smaller patches Changes for v3:
include/net.h | 4 ++++ net/tftp.c | 30 ++++++++++++++---------------- net/tftp.h | 2 +- 3 files changed, 19 insertions(+), 17 deletions(-)
diff --git a/include/net.h b/include/net.h index 3dbe154..92afc19 100644 --- a/include/net.h +++ b/include/net.h @@ -396,6 +396,10 @@ extern IPaddr_t NetNtpServerIP; /* the ip address to NTP */ extern int NetTimeOffset; /* offset time from UTC */ #endif
+#if defined(CONFIG_MCAST_TFTP) +extern IPaddr_t Mcast_addr; +#endif + /* Initialize the network adapter */ extern int NetLoop(enum proto_t);
diff --git a/net/tftp.c b/net/tftp.c index e62f229..bc7fe05 100644 --- a/net/tftp.c +++ b/net/tftp.c @@ -11,6 +11,9 @@ #include <net.h> #include "tftp.h" #include "bootp.h" +#ifdef CONFIG_SYS_DIRECT_FLASH_TFTP +#include <flash.h> +#endif
/* Well known TFTP port # */ #define WELL_KNOWN_PORT 69 @@ -112,10 +115,6 @@ static char default_filename[DEFAULT_NAME_LEN];
static char tftp_filename[MAX_LEN];
-#ifdef CONFIG_SYS_DIRECT_FLASH_TFTP -extern flash_info_t flash_info[]; -#endif - /* 512 is poor choice for ethernet, MTU is typically 1500. * Minus eth.hdrs thats 1468. Can get 2x better throughput with * almost-MTU block sizes. At least try... fall back to 512 if need be. @@ -137,7 +136,6 @@ static unsigned *Bitmap; static int PrevBitmapHole, Mapsize = MTFTP_BITMAPSIZE; static uchar ProhibitMcast, MasterClient; static uchar Multicast; -extern IPaddr_t Mcast_addr; static int Mcast_port; static ulong TftpEndingBlock; /* can get 'last' block before done..*/
@@ -157,7 +155,7 @@ mcast_cleanup(void)
#endif /* CONFIG_MCAST_TFTP */
-static __inline__ void +static inline void store_block(unsigned block, uchar *src, unsigned len) { ulong offset = block * TftpBlkSize + TftpBlockWrapOffset; @@ -182,8 +180,7 @@ store_block(unsigned block, uchar *src, unsigned len) NetState = NETLOOP_FAIL; return; } - } - else + } else #endif /* CONFIG_SYS_DIRECT_FLASH_TFTP */ { (void)memcpy((void *)(load_addr + offset), src, len); @@ -357,12 +354,14 @@ TftpSend(void) 0, TftpBlkSizeOption, 0); #ifdef CONFIG_MCAST_TFTP /* Check all preconditions before even trying the option */ - if (!ProhibitMcast - && (Bitmap = malloc(Mapsize)) - && eth_get_dev()->mcast) { - free(Bitmap); - Bitmap = NULL; - pkt += sprintf((char *)pkt, "multicast%c%c", 0, 0); + if (!ProhibitMcast) { + Bitmap = malloc(Mapsize); + if (Bitmap && eth_get_dev()->mcast) { + free(Bitmap); + Bitmap = NULL; + pkt += sprintf((char *)pkt, "multicast%c%c", + 0, 0); + } } #endif /* CONFIG_MCAST_TFTP */ len = pkt - xp; @@ -630,8 +629,7 @@ TftpHandler(uchar *pkt, unsigned dest, IPaddr_t sip, unsigned src, mcast_cleanup(); NetState = NETLOOP_SUCCESS; } - } - else + } else #endif if (len < TftpBlkSize) tftp_complete(); diff --git a/net/tftp.h b/net/tftp.h index c51aa25..18e4c9c 100644 --- a/net/tftp.h +++ b/net/tftp.h @@ -19,7 +19,7 @@ void TftpStart(enum proto_t protocol); /* Begin TFTP get/put */
#ifdef CONFIG_CMD_TFTPSRV -extern void TftpStartServer(void); /* Wait for incoming TFTP put */ +extern void TftpStartServer(void); /* Wait for incoming TFTP put */ #endif
/**********************************************************************/

Requires: --ignore CONSIDER_KSTRTO
Signed-off-by: Joe Hershberger joe.hershberger@ni.com Cc: Joe Hershberger joe.hershberger@gmail.com --- Changes for v2: - Split into smaller patches Changes for v3:
drivers/net/netconsole.c | 122 +++++++++++++++++++++++---------------------- 1 files changed, 62 insertions(+), 60 deletions(-)
diff --git a/drivers/net/netconsole.c b/drivers/net/netconsole.c index 56ba64f..7acd5b0 100644 --- a/drivers/net/netconsole.c +++ b/drivers/net/netconsole.c @@ -29,81 +29,82 @@ DECLARE_GLOBAL_DATA_PTR;
static char input_buffer[512]; -static int input_size = 0; /* char count in input buffer */ -static int input_offset = 0; /* offset to valid chars in input buffer */ -static int input_recursion = 0; -static int output_recursion = 0; +static int input_size; /* char count in input buffer */ +static int input_offset; /* offset to valid chars in input buffer */ +static int input_recursion; +static int output_recursion; static int net_timeout; -static uchar nc_ether[6]; /* server enet address */ -static IPaddr_t nc_ip; /* server ip */ -static short nc_port; /* source/target port */ -static const char *output_packet; /* used by first send udp */ -static int output_packet_len = 0; +static uchar nc_ether[6]; /* server enet address */ +static IPaddr_t nc_ip; /* server ip */ +static short nc_port; /* source/target port */ +static const char *output_packet; /* used by first send udp */ +static int output_packet_len;
static void nc_wait_arp_handler(uchar *pkt, unsigned dest, IPaddr_t sip, unsigned src, unsigned len) { - NetState = NETLOOP_SUCCESS; /* got arp reply - quit net loop */ + NetState = NETLOOP_SUCCESS; /* got arp reply - quit net loop */ }
static void nc_handler(uchar *pkt, unsigned dest, IPaddr_t sip, unsigned src, unsigned len) { if (input_size) - NetState = NETLOOP_SUCCESS; /* got input - quit net loop */ + NetState = NETLOOP_SUCCESS; /* got input - quit net loop */ }
-static void nc_timeout (void) +static void nc_timeout(void) { NetState = NETLOOP_SUCCESS; }
-void NcStart (void) +void NcStart(void) { - if (!output_packet_len || memcmp (nc_ether, NetEtherNullAddr, 6)) { + if (!output_packet_len || memcmp(nc_ether, NetEtherNullAddr, 6)) { /* going to check for input packet */ - NetSetHandler (nc_handler); - NetSetTimeout (net_timeout, nc_timeout); + NetSetHandler(nc_handler); + NetSetTimeout(net_timeout, nc_timeout); } else { /* send arp request */ uchar *pkt; - NetSetHandler (nc_wait_arp_handler); - pkt = (uchar *) NetTxPacket + NetEthHdrSize () + IP_HDR_SIZE; - memcpy (pkt, output_packet, output_packet_len); - NetSendUDPPacket (nc_ether, nc_ip, nc_port, nc_port, output_packet_len); + NetSetHandler(nc_wait_arp_handler); + pkt = (uchar *)NetTxPacket + NetEthHdrSize() + IP_HDR_SIZE; + memcpy(pkt, output_packet, output_packet_len); + NetSendUDPPacket(nc_ether, nc_ip, nc_port, nc_port, + output_packet_len); } }
-int nc_input_packet (uchar * pkt, unsigned dest, unsigned src, unsigned len) +int nc_input_packet(uchar *pkt, unsigned dest, unsigned src, unsigned len) { int end, chunk;
if (dest != nc_port || !len) - return 0; /* not for us */ + return 0; /* not for us */
- if (input_size == sizeof input_buffer) - return 1; /* no space */ - if (len > sizeof input_buffer - input_size) - len = sizeof input_buffer - input_size; + if (input_size == sizeof(input_buffer)) + return 1; /* no space */ + if (len > sizeof(input_buffer) - input_size) + len = sizeof(input_buffer) - input_size;
end = input_offset + input_size; - if (end > sizeof input_buffer) - end -= sizeof input_buffer; + if (end > sizeof(input_buffer)) + end -= sizeof(input_buffer);
chunk = len; - if (end + len > sizeof input_buffer) { - chunk = sizeof input_buffer - end; + if (end + len > sizeof(input_buffer)) { + chunk = sizeof(input_buffer) - end; memcpy(input_buffer, pkt + chunk, len - chunk); } - memcpy (input_buffer + end, pkt, chunk); + memcpy(input_buffer + end, pkt, chunk);
input_size += len;
return 1; }
-static void nc_send_packet (const char *buf, int len) +static void nc_send_packet(const char *buf, int len) { struct eth_device *eth; int inited = 0; @@ -111,33 +112,33 @@ static void nc_send_packet (const char *buf, int len) uchar *ether; IPaddr_t ip;
- if ((eth = eth_get_dev ()) == NULL) { + eth = eth_get_dev(); + if (eth == NULL) return; - }
- if (!memcmp (nc_ether, NetEtherNullAddr, 6)) { + if (!memcmp(nc_ether, NetEtherNullAddr, 6)) { if (eth->state == ETH_STATE_ACTIVE) return; /* inside net loop */ output_packet = buf; output_packet_len = len; - NetLoop (NETCONS); /* wait for arp reply and send packet */ + NetLoop(NETCONS); /* wait for arp reply and send packet */ output_packet_len = 0; return; }
if (eth->state != ETH_STATE_ACTIVE) { - if (eth_init (gd->bd) < 0) + if (eth_init(gd->bd) < 0) return; inited = 1; } - pkt = (uchar *) NetTxPacket + NetEthHdrSize () + IP_HDR_SIZE; - memcpy (pkt, buf, len); + pkt = (uchar *)NetTxPacket + NetEthHdrSize() + IP_HDR_SIZE; + memcpy(pkt, buf, len); ether = nc_ether; ip = nc_ip; - NetSendUDPPacket (ether, ip, nc_port, nc_port, len); + NetSendUDPPacket(ether, ip, nc_port, nc_port, len);
if (inited) - eth_halt (); + eth_halt(); }
static int nc_start(void) @@ -146,26 +147,27 @@ static int nc_start(void)
nc_port = 6666; /* default port */
- if (getenv ("ncip")) { + if (getenv("ncip")) { char *p;
- nc_ip = getenv_IPaddr ("ncip"); + nc_ip = getenv_IPaddr("ncip"); if (!nc_ip) return -1; /* ncip is 0.0.0.0 */ - if ((p = strchr (getenv ("ncip"), ':')) != NULL) - nc_port = simple_strtoul (p + 1, NULL, 10); + p = strchr(getenv("ncip"), ':'); + if (p != NULL) + nc_port = simple_strtoul(p + 1, NULL, 10); } else nc_ip = ~0; /* ncip is not set */
- our_ip = getenv_IPaddr ("ipaddr"); - netmask = getenv_IPaddr ("netmask"); + our_ip = getenv_IPaddr("ipaddr"); + netmask = getenv_IPaddr("netmask");
if (nc_ip == ~0 || /* 255.255.255.255 */ ((netmask & our_ip) == (netmask & nc_ip) && /* on the same net */ - (netmask | nc_ip) == ~0)) /* broadcast to our net */ - memset (nc_ether, 0xff, sizeof nc_ether); + (netmask | nc_ip) == ~0)) /* broadcast to our net */ + memset(nc_ether, 0xff, sizeof(nc_ether)); else - memset (nc_ether, 0, sizeof nc_ether); /* force arp request */ + memset(nc_ether, 0, sizeof(nc_ether)); /* force arp request */
return 0; } @@ -176,7 +178,7 @@ static void nc_putc(char c) return; output_recursion = 1;
- nc_send_packet (&c, 1); + nc_send_packet(&c, 1);
output_recursion = 0; } @@ -208,14 +210,14 @@ static int nc_getc(void)
net_timeout = 0; /* no timeout */ while (!input_size) - NetLoop (NETCONS); + NetLoop(NETCONS);
input_recursion = 0;
c = input_buffer[input_offset++];
- if (input_offset >= sizeof input_buffer) - input_offset -= sizeof input_buffer; + if (input_offset >= sizeof(input_buffer)) + input_offset -= sizeof(input_buffer); input_size--;
return c; @@ -231,28 +233,28 @@ static int nc_tstc(void) if (input_size) return 1;
- eth = eth_get_dev (); + eth = eth_get_dev(); if (eth && eth->state == ETH_STATE_ACTIVE) return 0; /* inside net loop */
input_recursion = 1;
net_timeout = 1; - NetLoop (NETCONS); /* kind of poll */ + NetLoop(NETCONS); /* kind of poll */
input_recursion = 0;
return input_size != 0; }
-int drv_nc_init (void) +int drv_nc_init(void) { struct stdio_dev dev; int rc;
- memset (&dev, 0, sizeof (dev)); + memset(&dev, 0, sizeof(dev));
- strcpy (dev.name, "nc"); + strcpy(dev.name, "nc"); dev.flags = DEV_FLAGS_OUTPUT | DEV_FLAGS_INPUT | DEV_FLAGS_SYSTEM; dev.start = nc_start; dev.putc = nc_putc; @@ -260,7 +262,7 @@ int drv_nc_init (void) dev.getc = nc_getc; dev.tstc = nc_tstc;
- rc = stdio_register (&dev); + rc = stdio_register(&dev);
return (rc == 0) ? 1 : rc; }

Signed-off-by: Joe Hershberger joe.hershberger@ni.com Cc: Joe Hershberger joe.hershberger@gmail.com Cc: Simon Glass sjg@chromium.org Cc: Mike Frysinger vapier@gentoo.org --- Changes for v2: - Remove volatile from eth driver API - Not using global NetRxPacket (since not casting away volatile)
include/net.h | 25 +++++++++++++------------ net/bootp.c | 4 ++-- net/eth.c | 12 +++++------- net/net.c | 34 +++++++++++++++++----------------- net/rarp.c | 2 +- net/tftp.c | 6 +++--- 6 files changed, 41 insertions(+), 42 deletions(-)
diff --git a/include/net.h b/include/net.h index 5560785..883fd1b 100644 --- a/include/net.h +++ b/include/net.h @@ -80,7 +80,7 @@ struct eth_device { int state;
int (*init) (struct eth_device*, bd_t*); - int (*send) (struct eth_device*, volatile void* packet, int length); + int (*send) (struct eth_device*, void *packet, int length); int (*recv) (struct eth_device*); void (*halt) (struct eth_device*); #ifdef CONFIG_MCAST_TFTP @@ -119,10 +119,11 @@ extern int eth_getenv_enetaddr_by_index(const char *base_name, int index,
extern int usb_eth_initialize(bd_t *bi); extern int eth_init(bd_t *bis); /* Initialize the device */ -extern int eth_send(volatile void *packet, int length); /* Send a packet */ +extern int eth_send(void *packet, int length); /* Send a packet */
#ifdef CONFIG_API -extern int eth_receive(volatile void *packet, int length); /* Receive a packet*/ +extern int eth_receive(void *packet, int length); /* Receive a packet*/ +extern void (*push_packet)(void *packet, int length); #endif extern int eth_rx(void); /* Check for received packets */ extern void eth_halt(void); /* stop SCC */ @@ -341,9 +342,9 @@ extern uchar NetOurEther[6]; /* Our ethernet address */ extern uchar NetServerEther[6]; /* Boot server enet address */ extern IPaddr_t NetOurIP; /* Our IP addr (0 = unknown) */ extern IPaddr_t NetServerIP; /* Server IP addr (0 = unknown) */ -extern volatile uchar * NetTxPacket; /* THE transmit packet */ -extern volatile uchar * NetRxPackets[PKTBUFSRX];/* Receive packets */ -extern volatile uchar * NetRxPacket; /* Current receive packet */ +extern uchar *NetTxPacket; /* THE transmit packet */ +extern uchar *NetRxPackets[PKTBUFSRX];/* Receive packets */ +extern uchar *NetRxPacket; /* Current receive packet */ extern int NetRxPacketLen; /* Current rx packet length */ extern unsigned NetIPID; /* IP ID (counting) */ extern uchar NetBcastAddr[6]; /* Ethernet boardcast address */ @@ -407,10 +408,10 @@ extern void NetStartAgain(void); extern int NetEthHdrSize(void);
/* Set ethernet header; returns the size of the header */ -extern int NetSetEther(volatile uchar *, uchar *, uint); +extern int NetSetEther(uchar *, uchar *, uint);
/* Set IP header */ -extern void NetSetIP(volatile uchar *, IPaddr_t, int, int, int); +extern void NetSetIP(uchar *, IPaddr_t, int, int, int);
/* Checksum */ extern int NetCksumOk(uchar *, int); /* Return true if cksum OK */ @@ -422,13 +423,13 @@ extern void net_set_icmp_handler(rxhand_icmp_f *f); /* Set ICMP RX handler */ extern void NetSetTimeout(ulong, thand_f *);/* Set timeout handler */
/* Transmit "NetTxPacket" */ -extern void NetSendPacket(volatile uchar *, int); +extern void NetSendPacket(uchar *, int);
/* Transmit UDP packet, performing ARP request if needed */ extern int NetSendUDPPacket(uchar *ether, IPaddr_t dest, int dport, int sport, int len);
/* Processes a received packet */ -extern void NetReceive(volatile uchar *, int); +extern void NetReceive(uchar *, int);
/* * Check if autoload is enabled. If so, use either NFS or TFTP to download @@ -444,7 +445,7 @@ void net_auto_load(void); * footprint in our tests. */ /* return IP *in network byteorder* */ -static inline IPaddr_t NetReadIP(volatile void *from) +static inline IPaddr_t NetReadIP(void *from) { IPaddr_t ip; memcpy((void*)&ip, (void*)from, sizeof(ip)); @@ -466,7 +467,7 @@ static inline void NetWriteIP(void *to, IPaddr_t ip) }
/* copy IP */ -static inline void NetCopyIP(volatile void *to, void *from) +static inline void NetCopyIP(void *to, void *from) { memcpy((void*)to, from, sizeof(IPaddr_t)); } diff --git a/net/bootp.c b/net/bootp.c index 9e32476..35654b4 100644 --- a/net/bootp.c +++ b/net/bootp.c @@ -586,7 +586,7 @@ static int BootpExtended (u8 * e) void BootpRequest (void) { - volatile uchar *pkt, *iphdr; + uchar *pkt, *iphdr; Bootp_t *bp; int ext_len, pktlen, iplen;
@@ -839,7 +839,7 @@ static int DhcpMessageType(unsigned char *popt)
static void DhcpSendRequestPkt(Bootp_t *bp_offer) { - volatile uchar *pkt, *iphdr; + uchar *pkt, *iphdr; Bootp_t *bp; int pktlen, iplen, extlen; IPaddr_t OfferedIP; diff --git a/net/eth.c b/net/eth.c index aabb343..1aba089 100644 --- a/net/eth.c +++ b/net/eth.c @@ -82,8 +82,6 @@ int cpu_eth_init(bd_t *bis) __attribute__((weak, alias("__def_eth_init"))); int board_eth_init(bd_t *bis) __attribute__((weak, alias("__def_eth_init")));
#ifdef CONFIG_API -extern void (*push_packet)(volatile void *, int); - static struct { uchar data[PKTSIZE]; int length; @@ -408,7 +406,7 @@ void eth_halt(void) eth_current->state = ETH_STATE_PASSIVE; }
-int eth_send(volatile void *packet, int length) +int eth_send(void *packet, int length) { if (!eth_current) return -1; @@ -425,9 +423,9 @@ int eth_rx(void) }
#ifdef CONFIG_API -static void eth_save_packet(volatile void *packet, int length) +static void eth_save_packet(void *packet, int length) { - volatile char *p = packet; + char *p = packet; int i;
if ((eth_rcv_last+1) % PKTBUFSRX == eth_rcv_current) @@ -443,9 +441,9 @@ static void eth_save_packet(volatile void *packet, int length) eth_rcv_last = (eth_rcv_last + 1) % PKTBUFSRX; }
-int eth_receive(volatile void *packet, int length) +int eth_receive(void *packet, int length) { - volatile char *p = packet; + char *p = packet; void *pp = push_packet; int i;
diff --git a/net/net.c b/net/net.c index c5acf8f..9a0417d 100644 --- a/net/net.c +++ b/net/net.c @@ -152,7 +152,7 @@ IPaddr_t NetOurIP; /* Server IP addr (0 = unknown) */ IPaddr_t NetServerIP; /* Current receive packet */ -volatile uchar *NetRxPacket; +uchar *NetRxPacket; /* Current rx packet length */ int NetRxPacketLen; /* IP packet ID */ @@ -161,7 +161,7 @@ unsigned NetIPID; uchar NetBcastAddr[6] = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff }; uchar NetEtherNullAddr[6]; #ifdef CONFIG_API -void (*push_packet)(volatile void *, int len) = 0; +void (*push_packet)(void *, int len) = 0; #endif #if defined(CONFIG_CMD_CDP) /* Ethernet bcast address */ @@ -208,10 +208,10 @@ void NcStart(void); int nc_input_packet(uchar *pkt, unsigned dest, unsigned src, unsigned len); #endif
-volatile uchar PktBuf[(PKTBUFSRX+1) * PKTSIZE_ALIGN + PKTALIGN]; +uchar PktBuf[(PKTBUFSRX+1) * PKTSIZE_ALIGN + PKTALIGN];
/* Receive packet */ -volatile uchar *NetRxPackets[PKTBUFSRX]; +uchar *NetRxPackets[PKTBUFSRX];
/* Current RX packet handler */ static rxhand_f *packetHandler; @@ -225,7 +225,7 @@ static ulong timeStart; /* Current timeout value */ static ulong timeDelta; /* THE transmit packet */ -volatile uchar *NetTxPacket; +uchar *NetTxPacket;
static int net_check_prereq(enum proto_t protocol);
@@ -246,7 +246,7 @@ int NetArpWaitTry;
void ArpRequest(void) { - volatile uchar *pkt; + uchar *pkt; ARP_t *arp;
debug("ARP broadcast %d\n", NetArpWaitTry); @@ -705,7 +705,7 @@ NetSetTimeout(ulong iv, thand_f *f)
void -NetSendPacket(volatile uchar *pkt, int len) +NetSendPacket(uchar *pkt, int len) { (void) eth_send(pkt, len); } @@ -768,8 +768,8 @@ static ushort PingSeqNo; int PingSend(void) { static uchar mac[6]; - volatile IP_t *ip; - volatile ushort *s; + IP_t *ip; + ushort *s; uchar *pkt;
/* XXX always send arp request */ @@ -784,7 +784,7 @@ int PingSend(void) pkt = NetArpWaitTxPacket; pkt += NetSetEther(pkt, mac, PROT_IP);
- ip = (volatile IP_t *)pkt; + ip = (IP_t *)pkt;
/* * Construct an IP and ICMP header. @@ -936,9 +936,9 @@ static ushort CDP_compute_csum(const uchar *buff, ushort len)
int CDPSendTrigger(void) { - volatile uchar *pkt; - volatile ushort *s; - volatile ushort *cp; + uchar *pkt; + ushort *s; + ushort *cp; Ethernet_t *et; int len; ushort chksum; @@ -965,7 +965,7 @@ int CDPSendTrigger(void) /* CDP header */ *pkt++ = 0x02; /* CDP version 2 */ *pkt++ = 180; /* TTL */ - s = (volatile ushort *)pkt; + s = (ushort *)pkt; cp = s; /* checksum (0 for later calculation) */ *s++ = htons(0); @@ -1439,7 +1439,7 @@ static void receive_icmp(IP_t *ip, int len, IPaddr_t src_ip, Ethernet_t *et) }
void -NetReceive(volatile uchar *inpkt, int len) +NetReceive(uchar *inpkt, int len) { Ethernet_t *et; IP_t *ip; @@ -1918,7 +1918,7 @@ NetEthHdrSize(void) }
int -NetSetEther(volatile uchar *xet, uchar * addr, uint prot) +NetSetEther(uchar *xet, uchar * addr, uint prot) { Ethernet_t *et = (Ethernet_t *)xet; ushort myvlanid; @@ -1943,7 +1943,7 @@ NetSetEther(volatile uchar *xet, uchar * addr, uint prot) }
void -NetSetIP(volatile uchar *xip, IPaddr_t dest, int dport, int sport, int len) +NetSetIP(uchar *xip, IPaddr_t dest, int dport, int sport, int len) { IP_t *ip = (IP_t *)xip;
diff --git a/net/rarp.c b/net/rarp.c index 097f970..77d63e8 100644 --- a/net/rarp.c +++ b/net/rarp.c @@ -71,7 +71,7 @@ void RarpRequest (void) { int i; - volatile uchar *pkt; + uchar *pkt; ARP_t * rarp;
printf("RARP broadcast %d\n", ++RarpTry); diff --git a/net/tftp.c b/net/tftp.c index 7aa3e23..e62f229 100644 --- a/net/tftp.c +++ b/net/tftp.c @@ -310,9 +310,9 @@ static void TftpSend(void) { uchar *pkt; - volatile uchar *xp; - int len = 0; - volatile ushort *s; + uchar *xp; + int len = 0; + ushort *s;
#ifdef CONFIG_MCAST_TFTP /* Multicast TFTP.. non-MasterClients do not ACK data. */

Hi Joe,
On Tue, Mar 27, 2012 at 4:42 PM, Joe Hershberger joe.hershberger@ni.com wrote:
Signed-off-by: Joe Hershberger joe.hershberger@ni.com Cc: Joe Hershberger joe.hershberger@gmail.com Cc: Simon Glass sjg@chromium.org Cc: Mike Frysinger vapier@gentoo.org
I have a suggestion below that you might want to take up, but otherwise:
Acked-by: Simon Glass sjg@chromium.org
Changes for v2: - Remove volatile from eth driver API - Not using global NetRxPacket (since not casting away volatile)
include/net.h | 25 +++++++++++++------------ net/bootp.c | 4 ++-- net/eth.c | 12 +++++------- net/net.c | 34 +++++++++++++++++----------------- net/rarp.c | 2 +- net/tftp.c | 6 +++--- 6 files changed, 41 insertions(+), 42 deletions(-)
diff --git a/include/net.h b/include/net.h index 5560785..883fd1b 100644 --- a/include/net.h +++ b/include/net.h
@@ -407,10 +408,10 @@ extern void NetStartAgain(void); extern int NetEthHdrSize(void);
/* Set ethernet header; returns the size of the header */ -extern int NetSetEther(volatile uchar *, uchar *, uint); +extern int NetSetEther(uchar *, uchar *, uint);
I wonder if you can add parameters names into this one and others below as well while you are here. It at least gives some clue as to the parameters even if we don't document them yet.
You may intend to do this in later patches, since this is only for checkpatch.
Regards, Simon

Signed-off-by: Joe Hershberger joe.hershberger@ni.com Cc: Joe Hershberger joe.hershberger@gmail.com Cc: Simon Glass sjg@chromium.org Cc: Mike Frysinger vapier@gentoo.org --- Changes for v2: - Split into smaller patches
include/net.h | 114 +++++++++++++++++++++++++++++---------------------------- 1 files changed, 58 insertions(+), 56 deletions(-)
diff --git a/include/net.h b/include/net.h index 883fd1b..cb3e36d 100644 --- a/include/net.h +++ b/include/net.h @@ -79,14 +79,14 @@ struct eth_device { int iobase; int state;
- int (*init) (struct eth_device*, bd_t*); - int (*send) (struct eth_device*, void *packet, int length); - int (*recv) (struct eth_device*); - void (*halt) (struct eth_device*); + int (*init) (struct eth_device *, bd_t *); + int (*send) (struct eth_device *, void *packet, int length); + int (*recv) (struct eth_device *); + void (*halt) (struct eth_device *); #ifdef CONFIG_MCAST_TFTP - int (*mcast) (struct eth_device*, u32 ip, u8 set); + int (*mcast) (struct eth_device *, u32 ip, u8 set); #endif - int (*write_hwaddr) (struct eth_device*); + int (*write_hwaddr) (struct eth_device *); struct eth_device *next; int index; void *priv; @@ -100,7 +100,7 @@ extern void eth_set_current(void); /* set nterface to ethcur var */ extern struct eth_device *eth_get_dev(void); /* get the current device MAC */ extern struct eth_device *eth_get_dev_by_name(const char *devname); extern struct eth_device *eth_get_dev_by_index(int index); /* get dev @ index */ -extern int eth_get_dev_index (void); /* get the device index */ +extern int eth_get_dev_index(void); /* get the device index */ extern void eth_parse_enetaddr(const char *addr, uchar *enetaddr); extern int eth_getenv_enetaddr(char *name, uchar *enetaddr); extern int eth_setenv_enetaddr(char *name, const uchar *enetaddr); @@ -142,8 +142,8 @@ int eth_write_hwaddr(struct eth_device *dev, const char *base_name, int eth_number);
#ifdef CONFIG_MCAST_TFTP -int eth_mcast_join( IPaddr_t mcast_addr, u8 join); -u32 ether_crc (size_t len, unsigned char const *p); +int eth_mcast_join(IPaddr_t mcast_addr, u8 join); +u32 ether_crc(size_t len, unsigned char const *p); #endif
@@ -218,8 +218,8 @@ typedef struct { #define IP_FLAGS_DFRAG 0x4000 /* don't fragments */ #define IP_FLAGS_MFRAG 0x2000 /* more fragments */
-#define IP_HDR_SIZE_NO_UDP (sizeof (IP_t) - 8) -#define IP_HDR_SIZE (sizeof (IP_t)) +#define IP_HDR_SIZE_NO_UDP (sizeof(IP_t) - 8) +#define IP_HDR_SIZE (sizeof(IP_t))
/* @@ -326,46 +326,46 @@ typedef struct icmphdr {
/* net.c */ /** BOOTP EXTENTIONS **/ -extern IPaddr_t NetOurGatewayIP; /* Our gateway IP addresse */ -extern IPaddr_t NetOurSubnetMask; /* Our subnet mask (0 = unknown)*/ -extern IPaddr_t NetOurDNSIP; /* Our Domain Name Server (0 = unknown)*/ +extern IPaddr_t NetOurGatewayIP; /* Our gateway IP address */ +extern IPaddr_t NetOurSubnetMask; /* Our subnet mask (0 = unknown) */ +extern IPaddr_t NetOurDNSIP; /* Our Domain Name Server (0 = unknown) */ #if defined(CONFIG_BOOTP_DNS2) -extern IPaddr_t NetOurDNS2IP; /* Our 2nd Domain Name Server (0 = unknown)*/ +extern IPaddr_t NetOurDNS2IP; /* Our 2nd Domain Name Server (0 = unknown) */ #endif -extern char NetOurNISDomain[32]; /* Our NIS domain */ -extern char NetOurHostName[32]; /* Our hostname */ -extern char NetOurRootPath[64]; /* Our root path */ -extern ushort NetBootFileSize; /* Our boot file size in blocks */ +extern char NetOurNISDomain[32]; /* Our NIS domain */ +extern char NetOurHostName[32]; /* Our hostname */ +extern char NetOurRootPath[64]; /* Our root path */ +extern ushort NetBootFileSize; /* Our boot file size in blocks */ /** END OF BOOTP EXTENTIONS **/ -extern ulong NetBootFileXferSize; /* size of bootfile in bytes */ -extern uchar NetOurEther[6]; /* Our ethernet address */ -extern uchar NetServerEther[6]; /* Boot server enet address */ -extern IPaddr_t NetOurIP; /* Our IP addr (0 = unknown) */ -extern IPaddr_t NetServerIP; /* Server IP addr (0 = unknown) */ +extern ulong NetBootFileXferSize; /* size of bootfile in bytes */ +extern uchar NetOurEther[6]; /* Our ethernet address */ +extern uchar NetServerEther[6]; /* Boot server enet address */ +extern IPaddr_t NetOurIP; /* Our IP addr (0 = unknown) */ +extern IPaddr_t NetServerIP; /* Server IP addr (0 = unknown) */ extern uchar *NetTxPacket; /* THE transmit packet */ -extern uchar *NetRxPackets[PKTBUFSRX];/* Receive packets */ +extern uchar *NetRxPackets[PKTBUFSRX]; /* Receive packets */ extern uchar *NetRxPacket; /* Current receive packet */ -extern int NetRxPacketLen; /* Current rx packet length */ -extern unsigned NetIPID; /* IP ID (counting) */ -extern uchar NetBcastAddr[6]; /* Ethernet boardcast address */ +extern int NetRxPacketLen; /* Current rx packet length */ +extern unsigned NetIPID; /* IP ID (counting) */ +extern uchar NetBcastAddr[6]; /* Ethernet boardcast address */ extern uchar NetEtherNullAddr[6];
-#define VLAN_NONE 4095 /* untagged */ -#define VLAN_IDMASK 0x0fff /* mask of valid vlan id */ -extern ushort NetOurVLAN; /* Our VLAN */ -extern ushort NetOurNativeVLAN; /* Our Native VLAN */ +#define VLAN_NONE 4095 /* untagged */ +#define VLAN_IDMASK 0x0fff /* mask of valid vlan id */ +extern ushort NetOurVLAN; /* Our VLAN */ +extern ushort NetOurNativeVLAN; /* Our Native VLAN */
-extern uchar NetCDPAddr[6]; /* Ethernet CDP address */ -extern ushort CDPNativeVLAN; /* CDP returned native VLAN */ -extern ushort CDPApplianceVLAN; /* CDP returned appliance VLAN */ +extern uchar NetCDPAddr[6]; /* Ethernet CDP address */ +extern ushort CDPNativeVLAN; /* CDP returned native VLAN */ +extern ushort CDPApplianceVLAN; /* CDP returned appliance VLAN */
-extern int NetState; /* Network loop state */ +extern int NetState; /* Network loop state */ #define NETLOOP_CONTINUE 1 #define NETLOOP_RESTART 2 #define NETLOOP_SUCCESS 3 #define NETLOOP_FAIL 4
-extern int NetRestartWrap; /* Tried all network devices */ +extern int NetRestartWrap; /* Tried all network devices */
enum proto_t { BOOTP, RARP, ARP, TFTPGET, DHCP, PING, DNS, NFS, CDP, NETCONS, SNTP, @@ -373,7 +373,7 @@ enum proto_t { };
/* from net/net.c */ -extern char BootFile[128]; /* Boot File name */ +extern char BootFile[128]; /* Boot File name */
#if defined(CONFIG_CMD_DNS) extern char *NetDNSResolve; /* The host to resolve */ @@ -381,7 +381,7 @@ extern char *NetDNSenvvar; /* the env var to put the ip into */ #endif
#if defined(CONFIG_CMD_PING) -extern IPaddr_t NetPingIP; /* the ip address to ping */ +extern IPaddr_t NetPingIP; /* the ip address to ping */ #endif
#if defined(CONFIG_CMD_CDP) @@ -391,8 +391,8 @@ extern ushort CDPApplianceVLAN; #endif
#if defined(CONFIG_CMD_SNTP) -extern IPaddr_t NetNtpServerIP; /* the ip address to NTP */ -extern int NetTimeOffset; /* offset time from UTC */ +extern IPaddr_t NetNtpServerIP; /* the ip address to NTP */ +extern int NetTimeOffset; /* offset time from UTC */ #endif
/* Initialize the network adapter */ @@ -414,19 +414,20 @@ extern int NetSetEther(uchar *, uchar *, uint); extern void NetSetIP(uchar *, IPaddr_t, int, int, int);
/* Checksum */ -extern int NetCksumOk(uchar *, int); /* Return true if cksum OK */ -extern uint NetCksum(uchar *, int); /* Calculate the checksum */ +extern int NetCksumOk(uchar *, int); /* Return true if cksum OK */ +extern uint NetCksum(uchar *, int); /* Calculate the checksum */
/* Set callbacks */ -extern void NetSetHandler(rxhand_f *); /* Set RX packet handler */ +extern void NetSetHandler(rxhand_f *); /* Set RX packet handler */ extern void net_set_icmp_handler(rxhand_icmp_f *f); /* Set ICMP RX handler */ -extern void NetSetTimeout(ulong, thand_f *);/* Set timeout handler */ +extern void NetSetTimeout(ulong, thand_f *);/* Set timeout handler */
/* Transmit "NetTxPacket" */ extern void NetSendPacket(uchar *, int);
/* Transmit UDP packet, performing ARP request if needed */ -extern int NetSendUDPPacket(uchar *ether, IPaddr_t dest, int dport, int sport, int len); +extern int NetSendUDPPacket(uchar *ether, IPaddr_t dest, int dport, + int sport, int len);
/* Processes a received packet */ extern void NetReceive(uchar *, int); @@ -448,7 +449,7 @@ void net_auto_load(void); static inline IPaddr_t NetReadIP(void *from) { IPaddr_t ip; - memcpy((void*)&ip, (void*)from, sizeof(ip)); + memcpy((void *)&ip, (void *)from, sizeof(ip)); return ip; }
@@ -456,26 +457,26 @@ static inline IPaddr_t NetReadIP(void *from) static inline ulong NetReadLong(ulong *from) { ulong l; - memcpy((void*)&l, (void*)from, sizeof(l)); + memcpy((void *)&l, (void *)from, sizeof(l)); return l; }
/* write IP *in network byteorder* */ static inline void NetWriteIP(void *to, IPaddr_t ip) { - memcpy(to, (void*)&ip, sizeof(ip)); + memcpy(to, (void *)&ip, sizeof(ip)); }
/* copy IP */ static inline void NetCopyIP(void *to, void *from) { - memcpy((void*)to, from, sizeof(IPaddr_t)); + memcpy((void *)to, from, sizeof(IPaddr_t)); }
/* copy ulong */ static inline void NetCopyLong(ulong *to, ulong *from) { - memcpy((void*)to, (void*)from, sizeof(ulong)); + memcpy((void *)to, (void *)from, sizeof(ulong)); }
/** @@ -498,7 +499,7 @@ static inline int is_zero_ether_addr(const u8 *addr) */ static inline int is_multicast_ether_addr(const u8 *addr) { - return (0x01 & addr[0]); + return 0x01 & addr[0]; }
/* @@ -509,7 +510,8 @@ static inline int is_multicast_ether_addr(const u8 *addr) */ static inline int is_broadcast_ether_addr(const u8 *addr) { - return (addr[0] & addr[1] & addr[2] & addr[3] & addr[4] & addr[5]) == 0xff; + return (addr[0] & addr[1] & addr[2] & addr[3] & addr[4] & addr[5]) == + 0xff; }
/* @@ -529,13 +531,13 @@ static inline int is_valid_ether_addr(const u8 *addr) }
/* Convert an IP address to a string */ -extern void ip_to_string (IPaddr_t x, char *s); +extern void ip_to_string(IPaddr_t x, char *s);
/* Convert a string to ip address */ extern IPaddr_t string_to_ip(const char *s);
/* Convert a VLAN id to a string */ -extern void VLAN_to_string (ushort x, char *s); +extern void VLAN_to_string(ushort x, char *s);
/* Convert a string to a vlan id */ extern ushort string_to_VLAN(const char *s); @@ -544,7 +546,7 @@ extern ushort string_to_VLAN(const char *s); extern ushort getenv_VLAN(char *);
/* copy a filename (allow for "..." notation, limit length) */ -extern void copy_filename (char *dst, const char *src, int size); +extern void copy_filename(char *dst, const char *src, int size);
/* get a random source port */ extern unsigned int random_port(void);

Hi Joe,
On Tue, Mar 27, 2012 at 4:42 PM, Joe Hershberger joe.hershberger@ni.com wrote:
Signed-off-by: Joe Hershberger joe.hershberger@ni.com Cc: Joe Hershberger joe.hershberger@gmail.com Cc: Simon Glass sjg@chromium.org Cc: Mike Frysinger vapier@gentoo.org
Changes for v2: - Split into smaller patches
Same suggestion about naming parameters...
include/net.h | 114 +++++++++++++++++++++++++++++---------------------------- 1 files changed, 58 insertions(+), 56 deletions(-)
diff --git a/include/net.h b/include/net.h index 883fd1b..cb3e36d 100644 --- a/include/net.h +++ b/include/net.h @@ -79,14 +79,14 @@ struct eth_device { int iobase; int state;
- int (*init) (struct eth_device*, bd_t*);
- int (*send) (struct eth_device*, void *packet, int length);
- int (*recv) (struct eth_device*);
- void (*halt) (struct eth_device*);
- int (*init) (struct eth_device *, bd_t *);
- int (*send) (struct eth_device *, void *packet, int length);
- int (*recv) (struct eth_device *);
- void (*halt) (struct eth_device *);
#ifdef CONFIG_MCAST_TFTP
- int (*mcast) (struct eth_device*, u32 ip, u8 set);
- int (*mcast) (struct eth_device *, u32 ip, u8 set);
#endif
- int (*write_hwaddr) (struct eth_device*);
- int (*write_hwaddr) (struct eth_device *);
struct eth_device *next; int index; void *priv; @@ -100,7 +100,7 @@ extern void eth_set_current(void); /* set nterface to ethcur var */ extern struct eth_device *eth_get_dev(void); /* get the current device MAC */ extern struct eth_device *eth_get_dev_by_name(const char *devname); extern struct eth_device *eth_get_dev_by_index(int index); /* get dev @ index */ -extern int eth_get_dev_index (void); /* get the device index */ +extern int eth_get_dev_index(void); /* get the device index */ extern void eth_parse_enetaddr(const char *addr, uchar *enetaddr); extern int eth_getenv_enetaddr(char *name, uchar *enetaddr); extern int eth_setenv_enetaddr(char *name, const uchar *enetaddr); @@ -142,8 +142,8 @@ int eth_write_hwaddr(struct eth_device *dev, const char *base_name, int eth_number);
#ifdef CONFIG_MCAST_TFTP -int eth_mcast_join( IPaddr_t mcast_addr, u8 join); -u32 ether_crc (size_t len, unsigned char const *p); +int eth_mcast_join(IPaddr_t mcast_addr, u8 join); +u32 ether_crc(size_t len, unsigned char const *p); #endif
@@ -218,8 +218,8 @@ typedef struct { #define IP_FLAGS_DFRAG 0x4000 /* don't fragments */ #define IP_FLAGS_MFRAG 0x2000 /* more fragments */
-#define IP_HDR_SIZE_NO_UDP (sizeof (IP_t) - 8) -#define IP_HDR_SIZE (sizeof (IP_t)) +#define IP_HDR_SIZE_NO_UDP (sizeof(IP_t) - 8) +#define IP_HDR_SIZE (sizeof(IP_t))
/* @@ -326,46 +326,46 @@ typedef struct icmphdr {
/* net.c */ /** BOOTP EXTENTIONS **/ -extern IPaddr_t NetOurGatewayIP; /* Our gateway IP addresse */ -extern IPaddr_t NetOurSubnetMask; /* Our subnet mask (0 = unknown)*/ -extern IPaddr_t NetOurDNSIP; /* Our Domain Name Server (0 = unknown)*/ +extern IPaddr_t NetOurGatewayIP; /* Our gateway IP address */ +extern IPaddr_t NetOurSubnetMask; /* Our subnet mask (0 = unknown) */ +extern IPaddr_t NetOurDNSIP; /* Our Domain Name Server (0 = unknown) */ #if defined(CONFIG_BOOTP_DNS2) -extern IPaddr_t NetOurDNS2IP; /* Our 2nd Domain Name Server (0 = unknown)*/ +extern IPaddr_t NetOurDNS2IP; /* Our 2nd Domain Name Server (0 = unknown) */ #endif -extern char NetOurNISDomain[32]; /* Our NIS domain */ -extern char NetOurHostName[32]; /* Our hostname */ -extern char NetOurRootPath[64]; /* Our root path */ -extern ushort NetBootFileSize; /* Our boot file size in blocks */ +extern char NetOurNISDomain[32]; /* Our NIS domain */ +extern char NetOurHostName[32]; /* Our hostname */ +extern char NetOurRootPath[64]; /* Our root path */ +extern ushort NetBootFileSize; /* Our boot file size in blocks */ /** END OF BOOTP EXTENTIONS **/ -extern ulong NetBootFileXferSize; /* size of bootfile in bytes */ -extern uchar NetOurEther[6]; /* Our ethernet address */ -extern uchar NetServerEther[6]; /* Boot server enet address */ -extern IPaddr_t NetOurIP; /* Our IP addr (0 = unknown) */ -extern IPaddr_t NetServerIP; /* Server IP addr (0 = unknown) */ +extern ulong NetBootFileXferSize; /* size of bootfile in bytes */ +extern uchar NetOurEther[6]; /* Our ethernet address */ +extern uchar NetServerEther[6]; /* Boot server enet address */ +extern IPaddr_t NetOurIP; /* Our IP addr (0 = unknown) */ +extern IPaddr_t NetServerIP; /* Server IP addr (0 = unknown) */ extern uchar *NetTxPacket; /* THE transmit packet */ -extern uchar *NetRxPackets[PKTBUFSRX];/* Receive packets */ +extern uchar *NetRxPackets[PKTBUFSRX]; /* Receive packets */ extern uchar *NetRxPacket; /* Current receive packet */ -extern int NetRxPacketLen; /* Current rx packet length */ -extern unsigned NetIPID; /* IP ID (counting) */ -extern uchar NetBcastAddr[6]; /* Ethernet boardcast address */ +extern int NetRxPacketLen; /* Current rx packet length */ +extern unsigned NetIPID; /* IP ID (counting) */ +extern uchar NetBcastAddr[6]; /* Ethernet boardcast address */ extern uchar NetEtherNullAddr[6];
-#define VLAN_NONE 4095 /* untagged */ -#define VLAN_IDMASK 0x0fff /* mask of valid vlan id */ -extern ushort NetOurVLAN; /* Our VLAN */ -extern ushort NetOurNativeVLAN; /* Our Native VLAN */ +#define VLAN_NONE 4095 /* untagged */ +#define VLAN_IDMASK 0x0fff /* mask of valid vlan id */ +extern ushort NetOurVLAN; /* Our VLAN */ +extern ushort NetOurNativeVLAN; /* Our Native VLAN */
-extern uchar NetCDPAddr[6]; /* Ethernet CDP address */ -extern ushort CDPNativeVLAN; /* CDP returned native VLAN */ -extern ushort CDPApplianceVLAN; /* CDP returned appliance VLAN */ +extern uchar NetCDPAddr[6]; /* Ethernet CDP address */ +extern ushort CDPNativeVLAN; /* CDP returned native VLAN */ +extern ushort CDPApplianceVLAN; /* CDP returned appliance VLAN */
-extern int NetState; /* Network loop state */ +extern int NetState; /* Network loop state */ #define NETLOOP_CONTINUE 1 #define NETLOOP_RESTART 2 #define NETLOOP_SUCCESS 3 #define NETLOOP_FAIL 4
-extern int NetRestartWrap; /* Tried all network devices */ +extern int NetRestartWrap; /* Tried all network devices */
enum proto_t { BOOTP, RARP, ARP, TFTPGET, DHCP, PING, DNS, NFS, CDP, NETCONS, SNTP, @@ -373,7 +373,7 @@ enum proto_t { };
/* from net/net.c */ -extern char BootFile[128]; /* Boot File name */ +extern char BootFile[128]; /* Boot File name */
#if defined(CONFIG_CMD_DNS) extern char *NetDNSResolve; /* The host to resolve */ @@ -381,7 +381,7 @@ extern char *NetDNSenvvar; /* the env var to put the ip into */ #endif
#if defined(CONFIG_CMD_PING) -extern IPaddr_t NetPingIP; /* the ip address to ping */ +extern IPaddr_t NetPingIP; /* the ip address to ping */ #endif
#if defined(CONFIG_CMD_CDP) @@ -391,8 +391,8 @@ extern ushort CDPApplianceVLAN; #endif
#if defined(CONFIG_CMD_SNTP) -extern IPaddr_t NetNtpServerIP; /* the ip address to NTP */ -extern int NetTimeOffset; /* offset time from UTC */ +extern IPaddr_t NetNtpServerIP; /* the ip address to NTP */ +extern int NetTimeOffset; /* offset time from UTC */ #endif
/* Initialize the network adapter */ @@ -414,19 +414,20 @@ extern int NetSetEther(uchar *, uchar *, uint); extern void NetSetIP(uchar *, IPaddr_t, int, int, int);
/* Checksum */ -extern int NetCksumOk(uchar *, int); /* Return true if cksum OK */ -extern uint NetCksum(uchar *, int); /* Calculate the checksum */ +extern int NetCksumOk(uchar *, int); /* Return true if cksum OK */ +extern uint NetCksum(uchar *, int); /* Calculate the checksum */
/* Set callbacks */ -extern void NetSetHandler(rxhand_f *); /* Set RX packet handler */ +extern void NetSetHandler(rxhand_f *); /* Set RX packet handler */ extern void net_set_icmp_handler(rxhand_icmp_f *f); /* Set ICMP RX handler */ -extern void NetSetTimeout(ulong, thand_f *);/* Set timeout handler */ +extern void NetSetTimeout(ulong, thand_f *);/* Set timeout handler */
/* Transmit "NetTxPacket" */ extern void NetSendPacket(uchar *, int);
/* Transmit UDP packet, performing ARP request if needed */ -extern int NetSendUDPPacket(uchar *ether, IPaddr_t dest, int dport, int sport, int len); +extern int NetSendUDPPacket(uchar *ether, IPaddr_t dest, int dport,
- int sport, int len);
/* Processes a received packet */ extern void NetReceive(uchar *, int); @@ -448,7 +449,7 @@ void net_auto_load(void); static inline IPaddr_t NetReadIP(void *from) { IPaddr_t ip;
blank line here?
- memcpy((void*)&ip, (void*)from, sizeof(ip));
- memcpy((void *)&ip, (void *)from, sizeof(ip));
return ip; }
@@ -456,26 +457,26 @@ static inline IPaddr_t NetReadIP(void *from) static inline ulong NetReadLong(ulong *from) { ulong l;
blank line here?
- memcpy((void*)&l, (void*)from, sizeof(l));
- memcpy((void *)&l, (void *)from, sizeof(l));
return l; }
/* write IP *in network byteorder* */ static inline void NetWriteIP(void *to, IPaddr_t ip) {
- memcpy(to, (void*)&ip, sizeof(ip));
- memcpy(to, (void *)&ip, sizeof(ip));
}
/* copy IP */ static inline void NetCopyIP(void *to, void *from) {
- memcpy((void*)to, from, sizeof(IPaddr_t));
- memcpy((void *)to, from, sizeof(IPaddr_t));
}
/* copy ulong */ static inline void NetCopyLong(ulong *to, ulong *from) {
- memcpy((void*)to, (void*)from, sizeof(ulong));
- memcpy((void *)to, (void *)from, sizeof(ulong));
}
/** @@ -498,7 +499,7 @@ static inline int is_zero_ether_addr(const u8 *addr) */ static inline int is_multicast_ether_addr(const u8 *addr) {
- return (0x01 & addr[0]);
- return 0x01 & addr[0];
}
/* @@ -509,7 +510,8 @@ static inline int is_multicast_ether_addr(const u8 *addr) */ static inline int is_broadcast_ether_addr(const u8 *addr) {
- return (addr[0] & addr[1] & addr[2] & addr[3] & addr[4] & addr[5]) == 0xff;
- return (addr[0] & addr[1] & addr[2] & addr[3] & addr[4] & addr[5]) ==
- 0xff;
}
/* @@ -529,13 +531,13 @@ static inline int is_valid_ether_addr(const u8 *addr) }
/* Convert an IP address to a string */ -extern void ip_to_string (IPaddr_t x, char *s); +extern void ip_to_string(IPaddr_t x, char *s);
/* Convert a string to ip address */ extern IPaddr_t string_to_ip(const char *s);
/* Convert a VLAN id to a string */ -extern void VLAN_to_string (ushort x, char *s); +extern void VLAN_to_string(ushort x, char *s);
/* Convert a string to a vlan id */ extern ushort string_to_VLAN(const char *s); @@ -544,7 +546,7 @@ extern ushort string_to_VLAN(const char *s); extern ushort getenv_VLAN(char *);
/* copy a filename (allow for "..." notation, limit length) */ -extern void copy_filename (char *dst, const char *src, int size); +extern void copy_filename(char *dst, const char *src, int size);
/* get a random source port */ extern unsigned int random_port(void); -- 1.6.0.2
Regards, Simon

Signed-off-by: Joe Hershberger joe.hershberger@ni.com Cc: Joe Hershberger joe.hershberger@gmail.com Cc: Simon Glass sjg@chromium.org Cc: Mike Frysinger vapier@gentoo.org --- Changes for v2: - Split into smaller patches
net/bootp.c | 340 ++++++++++++++++++++++++++++++----------------------------- net/bootp.h | 56 +++++----- 2 files changed, 203 insertions(+), 193 deletions(-)
diff --git a/net/bootp.c b/net/bootp.c index 35654b4..d0a7da2 100644 --- a/net/bootp.c +++ b/net/bootp.c @@ -19,19 +19,19 @@ #endif #include <linux/compiler.h>
-#define BOOTP_VENDOR_MAGIC 0x63825363 /* RFC1048 Magic Cookie */ +#define BOOTP_VENDOR_MAGIC 0x63825363 /* RFC1048 Magic Cookie */
#define TIMEOUT 5000UL /* Milliseconds before trying BOOTP again */ #ifndef CONFIG_NET_RETRY_COUNT -# define TIMEOUT_COUNT 5 /* # of timeouts before giving up */ +# define TIMEOUT_COUNT 5 /* # of timeouts before giving up */ #else # define TIMEOUT_COUNT (CONFIG_NET_RETRY_COUNT) #endif
-#define PORT_BOOTPS 67 /* BOOTP server UDP port */ -#define PORT_BOOTPC 68 /* BOOTP client UDP port */ +#define PORT_BOOTPS 67 /* BOOTP server UDP port */ +#define PORT_BOOTPC 68 /* BOOTP client UDP port */
-#ifndef CONFIG_DHCP_MIN_EXT_LEN /* minimal length of extension list */ +#ifndef CONFIG_DHCP_MIN_EXT_LEN /* minimal length of extension list */ #define CONFIG_DHCP_MIN_EXT_LEN 64 #endif
@@ -43,8 +43,8 @@ ulong seed1, seed2;
#if defined(CONFIG_CMD_DHCP) dhcp_state_t dhcp_state = INIT; -unsigned long dhcp_leasetime = 0; -IPaddr_t NetDHCPServerIP = 0; +unsigned long dhcp_leasetime; +IPaddr_t NetDHCPServerIP; static void DhcpHandler(uchar *pkt, unsigned dest, IPaddr_t sip, unsigned src, unsigned len);
@@ -64,37 +64,29 @@ static char *dhcpmsg2str(int type) } } #endif - -#if defined(CONFIG_BOOTP_VENDOREX) -extern u8 *dhcp_vendorex_prep (u8 *e); /*rtn new e after add own opts. */ -extern u8 *dhcp_vendorex_proc (u8 *e); /*rtn next e if mine,else NULL */ -#endif - #endif
static int BootpCheckPkt(uchar *pkt, unsigned dest, unsigned src, unsigned len) { - Bootp_t *bp = (Bootp_t *) pkt; + struct Bootp_t *bp = (struct Bootp_t *) pkt; int retval = 0;
if (dest != PORT_BOOTPC || src != PORT_BOOTPS) retval = -1; - else if (len < sizeof (Bootp_t) - OPT_SIZE) + else if (len < sizeof(struct Bootp_t) - OPT_SIZE) retval = -2; else if (bp->bp_op != OP_BOOTREQUEST && - bp->bp_op != OP_BOOTREPLY && - bp->bp_op != DHCP_OFFER && - bp->bp_op != DHCP_ACK && - bp->bp_op != DHCP_NAK ) { + bp->bp_op != OP_BOOTREPLY && + bp->bp_op != DHCP_OFFER && + bp->bp_op != DHCP_ACK && + bp->bp_op != DHCP_NAK) retval = -3; - } else if (bp->bp_htype != HWT_ETHER) retval = -4; else if (bp->bp_hlen != HWL_ETHER) retval = -5; - else if (NetReadLong((ulong*)&bp->bp_id) != BootpID) { + else if (NetReadLong((ulong *)&bp->bp_id) != BootpID) retval = -6; - }
debug("Filtering pkt = %d\n", retval);
@@ -104,7 +96,7 @@ static int BootpCheckPkt(uchar *pkt, unsigned dest, unsigned src, unsigned len) /* * Copy parameters of interest from BOOTP_REPLY/DHCP_OFFER packet */ -static void BootpCopyNetParams(Bootp_t *bp) +static void BootpCopyNetParams(struct Bootp_t *bp) { __maybe_unused IPaddr_t tmp_ip;
@@ -113,10 +105,10 @@ static void BootpCopyNetParams(Bootp_t *bp) NetCopyIP(&tmp_ip, &bp->bp_siaddr); if (tmp_ip != 0) NetCopyIP(&NetServerIP, &bp->bp_siaddr); - memcpy (NetServerEther, ((Ethernet_t *)NetRxPacket)->et_src, 6); + memcpy(NetServerEther, ((Ethernet_t *)NetRxPacket)->et_src, 6); #endif if (strlen(bp->bp_file) > 0) - copy_filename (BootFile, bp->bp_file, sizeof(BootFile)); + copy_filename(BootFile, bp->bp_file, sizeof(BootFile));
debug("Bootfile: %s\n", BootFile);
@@ -124,97 +116,95 @@ static void BootpCopyNetParams(Bootp_t *bp) * don't delete exising entry when BOOTP / DHCP reply does * not contain a new value */ - if (*BootFile) { - setenv ("bootfile", BootFile); - } + if (*BootFile) + setenv("bootfile", BootFile); }
-static int truncate_sz (const char *name, int maxlen, int curlen) +static int truncate_sz(const char *name, int maxlen, int curlen) { if (curlen >= maxlen) { - printf("*** WARNING: %s is too long (%d - max: %d) - truncated\n", - name, curlen, maxlen); + printf("*** WARNING: %s is too long (%d - max: %d)" + " - truncated\n", name, curlen, maxlen); curlen = maxlen - 1; } - return (curlen); + return curlen; }
#if !defined(CONFIG_CMD_DHCP)
-static void BootpVendorFieldProcess (u8 * ext) +static void BootpVendorFieldProcess(u8 *ext) { int size = *(ext + 1);
debug("[BOOTP] Processing extension %d... (%d bytes)\n", *ext, - *(ext + 1)); + *(ext + 1));
NetBootFileSize = 0;
switch (*ext) { /* Fixed length fields */ - case 1: /* Subnet mask */ + case 1: /* Subnet mask */ if (NetOurSubnetMask == 0) - NetCopyIP (&NetOurSubnetMask, (IPaddr_t *) (ext + 2)); + NetCopyIP(&NetOurSubnetMask, (IPaddr_t *) (ext + 2)); break; - case 2: /* Time offset - Not yet supported */ + case 2: /* Time offset - Not yet supported */ break; /* Variable length fields */ - case 3: /* Gateways list */ - if (NetOurGatewayIP == 0) { - NetCopyIP (&NetOurGatewayIP, (IPaddr_t *) (ext + 2)); - } + case 3: /* Gateways list */ + if (NetOurGatewayIP == 0) + NetCopyIP(&NetOurGatewayIP, (IPaddr_t *) (ext + 2)); break; - case 4: /* Time server - Not yet supported */ + case 4: /* Time server - Not yet supported */ break; - case 5: /* IEN-116 name server - Not yet supported */ + case 5: /* IEN-116 name server - Not yet supported */ break; case 6: - if (NetOurDNSIP == 0) { - NetCopyIP (&NetOurDNSIP, (IPaddr_t *) (ext + 2)); - } + if (NetOurDNSIP == 0) + NetCopyIP(&NetOurDNSIP, (IPaddr_t *) (ext + 2)); #if defined(CONFIG_BOOTP_DNS2) - if ((NetOurDNS2IP == 0) && (size > 4)) { - NetCopyIP (&NetOurDNS2IP, (IPaddr_t *) (ext + 2 + 4)); - } + if ((NetOurDNS2IP == 0) && (size > 4)) + NetCopyIP(&NetOurDNS2IP, (IPaddr_t *) (ext + 2 + 4)); #endif break; - case 7: /* Log server - Not yet supported */ + case 7: /* Log server - Not yet supported */ break; - case 8: /* Cookie/Quote server - Not yet supported */ + case 8: /* Cookie/Quote server - Not yet supported */ break; - case 9: /* LPR server - Not yet supported */ + case 9: /* LPR server - Not yet supported */ break; - case 10: /* Impress server - Not yet supported */ + case 10: /* Impress server - Not yet supported */ break; - case 11: /* RPL server - Not yet supported */ + case 11: /* RPL server - Not yet supported */ break; - case 12: /* Host name */ + case 12: /* Host name */ if (NetOurHostName[0] == 0) { - size = truncate_sz ("Host Name", sizeof (NetOurHostName), size); - memcpy (&NetOurHostName, ext + 2, size); + size = truncate_sz("Host Name", + sizeof(NetOurHostName), size); + memcpy(&NetOurHostName, ext + 2, size); NetOurHostName[size] = 0; } break; - case 13: /* Boot file size */ + case 13: /* Boot file size */ if (size == 2) - NetBootFileSize = ntohs (*(ushort *) (ext + 2)); + NetBootFileSize = ntohs(*(ushort *) (ext + 2)); else if (size == 4) - NetBootFileSize = ntohl (*(ulong *) (ext + 2)); + NetBootFileSize = ntohl(*(ulong *) (ext + 2)); break; - case 14: /* Merit dump file - Not yet supported */ + case 14: /* Merit dump file - Not yet supported */ break; - case 15: /* Domain name - Not yet supported */ + case 15: /* Domain name - Not yet supported */ break; - case 16: /* Swap server - Not yet supported */ + case 16: /* Swap server - Not yet supported */ break; - case 17: /* Root path */ + case 17: /* Root path */ if (NetOurRootPath[0] == 0) { - size = truncate_sz ("Root Path", sizeof (NetOurRootPath), size); - memcpy (&NetOurRootPath, ext + 2, size); + size = truncate_sz("Root Path", + sizeof(NetOurRootPath), size); + memcpy(&NetOurRootPath, ext + 2, size); NetOurRootPath[size] = 0; } break; - case 18: /* Extension path - Not yet supported */ + case 18: /* Extension path - Not yet supported */ /* * This can be used to send the information of the * vendor area in another file that the client can @@ -222,10 +212,11 @@ static void BootpVendorFieldProcess (u8 * ext) */ break; /* IP host layer fields */ - case 40: /* NIS Domain name */ + case 40: /* NIS Domain name */ if (NetOurNISDomain[0] == 0) { - size = truncate_sz ("NIS Domain Name", sizeof (NetOurNISDomain), size); - memcpy (&NetOurNISDomain, ext + 2, size); + size = truncate_sz("NIS Domain Name", + sizeof(NetOurNISDomain), size); + memcpy(&NetOurNISDomain, ext + 2, size); NetOurNISDomain[size] = 0; } break; @@ -235,7 +226,7 @@ static void BootpVendorFieldProcess (u8 * ext) break; #endif /* Application layer fields */ - case 43: /* Vendor specific info - Not yet supported */ + case 43: /* Vendor specific info - Not yet supported */ /* * Binary information to exchange specific * product information. @@ -245,7 +236,7 @@ static void BootpVendorFieldProcess (u8 * ext) } }
-static void BootpVendorProcess (u8 * ext, int size) +static void BootpVendorProcess(u8 *ext, int size) { u8 *end = ext + size;
@@ -259,11 +250,11 @@ static void BootpVendorProcess (u8 * ext, int size)
ext += ext[1] + 2; if (ext <= end) - BootpVendorFieldProcess (opt); + BootpVendorFieldProcess(opt); } }
- debug("[BOOTP] Received fields: \n"); + debug("[BOOTP] Received fields:\n"); if (NetOurSubnetMask) debug("NetOurSubnetMask : %pI4\n", &NetOurSubnetMask);
@@ -298,27 +289,28 @@ static void BootpHandler(uchar *pkt, unsigned dest, IPaddr_t sip, unsigned src, unsigned len) { - Bootp_t *bp; + struct Bootp_t *bp;
debug("got BOOTP packet (src=%d, dst=%d, len=%d want_len=%zu)\n", - src, dest, len, sizeof (Bootp_t)); + src, dest, len, sizeof(struct Bootp_t));
- bp = (Bootp_t *)pkt; + bp = (struct Bootp_t *)pkt;
- if (BootpCheckPkt(pkt, dest, src, len)) /* Filter out pkts we don't want */ + /* Filter out pkts we don't want */ + if (BootpCheckPkt(pkt, dest, src, len)) return;
/* * Got a good BOOTP reply. Copy the data into our variables. */ #ifdef CONFIG_STATUS_LED - status_led_set (STATUS_LED_BOOT, STATUS_LED_OFF); + status_led_set(STATUS_LED_BOOT, STATUS_LED_OFF); #endif
BootpCopyNetParams(bp); /* Store net parameters from reply */
/* Retrieve extended information (we must parse the vendor area) */ - if (NetReadLong((ulong*)&bp->bp_vend[0]) == htonl(BOOTP_VENDOR_MAGIC)) + if (NetReadLong((ulong *)&bp->bp_vend[0]) == htonl(BOOTP_VENDOR_MAGIC)) BootpVendorProcess((uchar *)&bp->bp_vend[4], len);
NetSetTimeout(0, (thand_f *)0); @@ -337,11 +329,11 @@ static void BootpTimeout(void) { if (BootpTry >= TIMEOUT_COUNT) { - puts ("\nRetry count exceeded; starting again\n"); - NetStartAgain (); + puts("\nRetry count exceeded; starting again\n"); + NetStartAgain(); } else { - NetSetTimeout (TIMEOUT, BootpTimeout); - BootpRequest (); + NetSetTimeout(TIMEOUT, BootpTimeout); + BootpRequest(); } }
@@ -349,7 +341,8 @@ BootpTimeout(void) * Initialize BOOTP extension fields in the request. */ #if defined(CONFIG_CMD_DHCP) -static int DhcpExtended (u8 * e, int message_type, IPaddr_t ServerID, IPaddr_t RequestedIP) +static int DhcpExtended(u8 *e, int message_type, IPaddr_t ServerID, + IPaddr_t RequestedIP) { u8 *start = e; u8 *cnt; @@ -381,7 +374,7 @@ static int DhcpExtended (u8 * e, int message_type, IPaddr_t ServerID, IPaddr_t R *e++ = (576 - 312 + OPT_SIZE) & 0xff;
if (ServerID) { - int tmp = ntohl (ServerID); + int tmp = ntohl(ServerID);
*e++ = 54; /* ServerID */ *e++ = 4; @@ -392,7 +385,7 @@ static int DhcpExtended (u8 * e, int message_type, IPaddr_t ServerID, IPaddr_t R }
if (RequestedIP) { - int tmp = ntohl (RequestedIP); + int tmp = ntohl(RequestedIP);
*e++ = 50; /* Requested IP */ *e++ = 4; @@ -402,12 +395,13 @@ static int DhcpExtended (u8 * e, int message_type, IPaddr_t ServerID, IPaddr_t R *e++ = tmp & 0xff; } #if defined(CONFIG_BOOTP_SEND_HOSTNAME) - if ((hostname = getenv ("hostname"))) { - int hostnamelen = strlen (hostname); + hostname = getenv("hostname"); + if (hostname) { + int hostnamelen = strlen(hostname);
*e++ = 12; /* Hostname */ *e++ = hostnamelen; - memcpy (e, hostname, hostnamelen); + memcpy(e, hostname, hostnamelen); e += hostnamelen; } #endif @@ -448,7 +442,8 @@ static int DhcpExtended (u8 * e, int message_type, IPaddr_t ServerID, IPaddr_t R #endif
#if defined(CONFIG_BOOTP_VENDOREX) - if ((x = dhcp_vendorex_prep (e))) + x = dhcp_vendorex_prep(e); + if (x) return x - start; #endif
@@ -508,9 +503,9 @@ static int DhcpExtended (u8 * e, int message_type, IPaddr_t ServerID, IPaddr_t R
#else /* - * Warning: no field size check - change CONFIG_BOOTP_* at your own risk! + * Warning: no field size check - change CONFIG_BOOTP_* at your own risk! */ -static int BootpExtended (u8 * e) +static int BootpExtended(u8 *e) { u8 *start = e;
@@ -584,10 +579,10 @@ static int BootpExtended (u8 * e) #endif
void -BootpRequest (void) +BootpRequest(void) { uchar *pkt, *iphdr; - Bootp_t *bp; + struct Bootp_t *bp; int ext_len, pktlen, iplen;
bootstage_mark_name(BOOTSTAGE_ID_BOOTP_START, "bootp_start"); @@ -600,22 +595,22 @@ BootpRequest (void) int reg; ulong tst1, tst2, sum, m_mask, m_value = 0;
- if (BootpTry ==0) { + if (BootpTry == 0) { /* get our mac */ eth_getenv_enetaddr("ethaddr", bi_enetaddr);
debug("BootpRequest => Our Mac: "); - for (reg=0; reg<6; reg++) - debug("%x%c", bi_enetaddr[reg], reg==5 ? '\n' : ':'); + for (reg = 0; reg < 6; reg++) + debug("%x%c", bi_enetaddr[reg], reg == 5 ? '\n' : ':');
/* Mac-Manipulation 2 get seed1 */ - tst1=0; - tst2=0; - for (reg=2; reg<6; reg++) { + tst1 = 0; + tst2 = 0; + for (reg = 2; reg < 6; reg++) { tst1 = tst1 << 8; tst1 = tst1 | bi_enetaddr[reg]; } - for (reg=0; reg<2; reg++) { + for (reg = 0; reg < 2; reg++) { tst2 = tst2 | bi_enetaddr[reg]; tst2 = tst2 << 8; } @@ -623,8 +618,8 @@ BootpRequest (void) seed1 = tst1^tst2;
/* Mirror seed1*/ - m_mask=0x1; - for (reg=1;reg<=32;reg++) { + m_mask = 0x1; + for (reg = 1; reg <= 32; reg++) { m_value |= (m_mask & seed1); seed1 = seed1 >> 1; m_value = m_value << 1; @@ -634,44 +629,45 @@ BootpRequest (void) }
/* Random Number Generator */ - - for (reg=0;reg<=0;reg++) { + for (reg = 0; reg <= 0; reg++) { sum = seed1 + seed2; if (sum < seed1 || sum < seed2) sum++; seed2 = seed1; seed1 = sum;
- if (BootpTry<=2) { /* Start with max 1024 * 1ms */ + if (BootpTry <= 2) { /* Start with max 1024 * 1ms */ sum = sum >> (22-BootpTry); - } else { /*After 3rd BOOTP request max 8192 * 1ms */ + } else { /*After 3rd BOOTP request max 8192 * 1ms */ sum = sum >> 19; } }
- printf ("Random delay: %ld ms...\n", sum); - for (reg=0; reg <sum; reg++) { + printf("Random delay: %ld ms...\n", sum); + for (reg = 0; reg < sum; reg++) udelay(1000); /*Wait 1ms*/ - } + #endif /* CONFIG_BOOTP_RANDOM_DELAY */
printf("BOOTP broadcast %d\n", ++BootpTry); pkt = NetTxPacket; - memset ((void*)pkt, 0, PKTSIZE); + memset((void *)pkt, 0, PKTSIZE);
pkt += NetSetEther(pkt, NetBcastAddr, PROT_IP);
/* - * Next line results in incorrect packet size being transmitted, resulting - * in errors in some DHCP servers, reporting missing bytes. Size must be - * set in packet header after extension length has been determined. + * Next line results in incorrect packet size being transmitted, + * resulting in errors in some DHCP servers, reporting missing bytes. + * Size must be set in packet header after extension length has been + * determined. * C. Hallinan, DS4.COM, Inc. */ - /* NetSetIP(pkt, 0xFFFFFFFFL, PORT_BOOTPS, PORT_BOOTPC, sizeof (Bootp_t)); */ + /* NetSetIP(pkt, 0xFFFFFFFFL, PORT_BOOTPS, PORT_BOOTPC, + sizeof (struct Bootp_t)); */ iphdr = pkt; /* We need this later for NetSetIP() */ pkt += IP_HDR_SIZE;
- bp = (Bootp_t *)pkt; + bp = (struct Bootp_t *)pkt; bp->bp_op = OP_BOOTREQUEST; bp->bp_htype = HWT_ETHER; bp->bp_hlen = HWL_ETHER; @@ -681,8 +677,8 @@ BootpRequest (void) NetWriteIP(&bp->bp_yiaddr, 0); NetWriteIP(&bp->bp_siaddr, 0); NetWriteIP(&bp->bp_giaddr, 0); - memcpy (bp->bp_chaddr, NetOurEther, 6); - copy_filename (bp->bp_file, BootFile, sizeof(bp->bp_file)); + memcpy(bp->bp_chaddr, NetOurEther, 6); + copy_filename(bp->bp_file, BootFile, sizeof(bp->bp_file));
/* Request additional information from the BOOTP/DHCP server */ #if defined(CONFIG_CMD_DHCP) @@ -707,7 +703,8 @@ BootpRequest (void) * Calculate proper packet lengths taking into account the * variable size of the options field */ - pktlen = ((int)(pkt-NetTxPacket)) + BOOTP_HDR_SIZE - sizeof(bp->bp_vend) + ext_len; + pktlen = ((int)(pkt-NetTxPacket)) + BOOTP_HDR_SIZE - + sizeof(bp->bp_vend) + ext_len; iplen = BOOTP_HDR_SIZE - sizeof(bp->bp_vend) + ext_len; NetSetIP(iphdr, 0xFFFFFFFFL, PORT_BOOTPS, PORT_BOOTPC, iplen); NetSetTimeout(SELECT_TIMEOUT, BootpTimeout); @@ -722,7 +719,7 @@ BootpRequest (void) }
#if defined(CONFIG_CMD_DHCP) -static void DhcpOptionsProcess (uchar * popt, Bootp_t *bp) +static void DhcpOptionsProcess(uchar *popt, struct Bootp_t *bp) { uchar *end = popt + BOOTP_HDR_SIZE; int oplen, size; @@ -734,50 +731,51 @@ static void DhcpOptionsProcess (uchar * popt, Bootp_t *bp) oplen = *(popt + 1); switch (*popt) { case 1: - NetCopyIP (&NetOurSubnetMask, (popt + 2)); + NetCopyIP(&NetOurSubnetMask, (popt + 2)); break; #if defined(CONFIG_CMD_SNTP) && defined(CONFIG_BOOTP_TIMEOFFSET) case 2: /* Time offset */ to_ptr = &NetTimeOffset; - NetCopyLong ((ulong *)to_ptr, (ulong *)(popt + 2)); - NetTimeOffset = ntohl (NetTimeOffset); + NetCopyLong((ulong *)to_ptr, (ulong *)(popt + 2)); + NetTimeOffset = ntohl(NetTimeOffset); break; #endif case 3: - NetCopyIP (&NetOurGatewayIP, (popt + 2)); + NetCopyIP(&NetOurGatewayIP, (popt + 2)); break; case 6: - NetCopyIP (&NetOurDNSIP, (popt + 2)); + NetCopyIP(&NetOurDNSIP, (popt + 2)); #if defined(CONFIG_BOOTP_DNS2) - if (*(popt + 1) > 4) { - NetCopyIP (&NetOurDNS2IP, (popt + 2 + 4)); - } + if (*(popt + 1) > 4) + NetCopyIP(&NetOurDNS2IP, (popt + 2 + 4)); #endif break; case 12: - size = truncate_sz ("Host Name", sizeof (NetOurHostName), oplen); - memcpy (&NetOurHostName, popt + 2, size); + size = truncate_sz("Host Name", + sizeof(NetOurHostName), oplen); + memcpy(&NetOurHostName, popt + 2, size); NetOurHostName[size] = 0; break; case 15: /* Ignore Domain Name Option */ break; case 17: - size = truncate_sz ("Root Path", sizeof (NetOurRootPath), oplen); - memcpy (&NetOurRootPath, popt + 2, size); + size = truncate_sz("Root Path", + sizeof(NetOurRootPath), oplen); + memcpy(&NetOurRootPath, popt + 2, size); NetOurRootPath[size] = 0; break; #if defined(CONFIG_CMD_SNTP) && defined(CONFIG_BOOTP_NTPSERVER) case 42: /* NTP server IP */ - NetCopyIP (&NetNtpServerIP, (popt + 2)); + NetCopyIP(&NetNtpServerIP, (popt + 2)); break; #endif case 51: - NetCopyLong (&dhcp_leasetime, (ulong *) (popt + 2)); + NetCopyLong(&dhcp_leasetime, (ulong *) (popt + 2)); break; case 53: /* Ignore Message Type Option */ break; case 54: - NetCopyIP (&NetDHCPServerIP, (popt + 2)); + NetCopyIP(&NetDHCPServerIP, (popt + 2)); break; case 58: /* Ignore Renewal Time Option */ break; @@ -792,7 +790,7 @@ static void DhcpOptionsProcess (uchar * popt, Bootp_t *bp) * pass the bootp packet pointer into here as the * second arg */ - size = truncate_sz ("Opt Boot File", + size = truncate_sz("Opt Boot File", sizeof(bp->bp_file), oplen); if (bp->bp_file[0] == '\0' && size > 0) { @@ -813,10 +811,11 @@ static void DhcpOptionsProcess (uchar * popt, Bootp_t *bp) break; default: #if defined(CONFIG_BOOTP_VENDOREX) - if (dhcp_vendorex_proc (popt)) + if (dhcp_vendorex_proc(popt)) break; #endif - printf ("*** Unhandled DHCP Option in OFFER/ACK: %d\n", *popt); + printf("*** Unhandled DHCP Option in OFFER/ACK:" + " %d\n", *popt); break; } popt += oplen + 2; /* Process next option */ @@ -825,42 +824,42 @@ static void DhcpOptionsProcess (uchar * popt, Bootp_t *bp)
static int DhcpMessageType(unsigned char *popt) { - if (NetReadLong((ulong*)popt) != htonl(BOOTP_VENDOR_MAGIC)) + if (NetReadLong((ulong *)popt) != htonl(BOOTP_VENDOR_MAGIC)) return -1;
popt += 4; - while ( *popt != 0xff ) { - if ( *popt == 53 ) /* DHCP Message Type */ + while (*popt != 0xff) { + if (*popt == 53) /* DHCP Message Type */ return *(popt + 2); popt += *(popt + 1) + 2; /* Scan through all options */ } return -1; }
-static void DhcpSendRequestPkt(Bootp_t *bp_offer) +static void DhcpSendRequestPkt(struct Bootp_t *bp_offer) { uchar *pkt, *iphdr; - Bootp_t *bp; + struct Bootp_t *bp; int pktlen, iplen, extlen; IPaddr_t OfferedIP;
debug("DhcpSendRequestPkt: Sending DHCPREQUEST\n"); pkt = NetTxPacket; - memset ((void*)pkt, 0, PKTSIZE); + memset((void *)pkt, 0, PKTSIZE);
pkt += NetSetEther(pkt, NetBcastAddr, PROT_IP);
- iphdr = pkt; /* We'll need this later to set proper pkt size */ + iphdr = pkt; /* We'll need this later to set proper pkt size */ pkt += IP_HDR_SIZE;
- bp = (Bootp_t *)pkt; + bp = (struct Bootp_t *)pkt; bp->bp_op = OP_BOOTREQUEST; bp->bp_htype = HWT_ETHER; bp->bp_hlen = HWL_ETHER; bp->bp_hops = 0; bp->bp_secs = htons(get_timer(0) / 1000); - /* Do not set the client IP, your IP, or server IP yet, since it hasn't been ACK'ed by - * the server yet */ + /* Do not set the client IP, your IP, or server IP yet, since it + * hasn't been ACK'ed by the server yet */
/* * RFC3046 requires Relay Agents to discard packets with @@ -868,7 +867,7 @@ static void DhcpSendRequestPkt(Bootp_t *bp_offer) */ NetWriteIP(&bp->bp_giaddr, 0);
- memcpy (bp->bp_chaddr, NetOurEther, 6); + memcpy(bp->bp_chaddr, NetOurEther, 6);
/* * ID is the id of the OFFER packet @@ -882,9 +881,11 @@ static void DhcpSendRequestPkt(Bootp_t *bp_offer)
/* Copy offered IP into the parameters request list */ NetCopyIP(&OfferedIP, &bp_offer->bp_yiaddr); - extlen = DhcpExtended((u8 *)bp->bp_vend, DHCP_REQUEST, NetDHCPServerIP, OfferedIP); + extlen = DhcpExtended((u8 *)bp->bp_vend, DHCP_REQUEST, + NetDHCPServerIP, OfferedIP);
- pktlen = ((int)(pkt-NetTxPacket)) + BOOTP_HDR_SIZE - sizeof(bp->bp_vend) + extlen; + pktlen = ((int)(pkt-NetTxPacket)) + BOOTP_HDR_SIZE - + sizeof(bp->bp_vend) + extlen; iplen = BOOTP_HDR_SIZE - sizeof(bp->bp_vend) + extlen; NetSetIP(iphdr, 0xFFFFFFFFL, PORT_BOOTPS, PORT_BOOTPC, iplen);
@@ -902,36 +903,38 @@ static void DhcpHandler(uchar *pkt, unsigned dest, IPaddr_t sip, unsigned src, unsigned len) { - Bootp_t *bp = (Bootp_t *)pkt; + struct Bootp_t *bp = (struct Bootp_t *)pkt;
debug("DHCPHandler: got packet: (src=%d, dst=%d, len=%d) state: %d\n", src, dest, len, dhcp_state);
- if (BootpCheckPkt(pkt, dest, src, len)) /* Filter out pkts we don't want */ + /* Filter out pkts we don't want */ + if (BootpCheckPkt(pkt, dest, src, len)) return;
- debug("DHCPHandler: got DHCP packet: (src=%d, dst=%d, len=%d) state: %d\n", - src, dest, len, dhcp_state); + debug("DHCPHandler: got DHCP packet: (src=%d, dst=%d, len=%d) state:" + " %d\n", src, dest, len, dhcp_state);
switch (dhcp_state) { case SELECTING: /* * Wait an appropriate time for any potential DHCPOFFER packets - * to arrive. Then select one, and generate DHCPREQUEST response. - * If filename is in format we recognize, assume it is a valid - * OFFER from a server we want. + * to arrive. Then select one, and generate DHCPREQUEST + * response. If filename is in format we recognize, assume it + * is a valid OFFER from a server we want. */ debug("DHCP: state=SELECTING bp_file: "%s"\n", bp->bp_file); #ifdef CONFIG_SYS_BOOTFILE_PREFIX if (strncmp(bp->bp_file, CONFIG_SYS_BOOTFILE_PREFIX, - strlen(CONFIG_SYS_BOOTFILE_PREFIX)) == 0 ) { + strlen(CONFIG_SYS_BOOTFILE_PREFIX)) == 0) { #endif /* CONFIG_SYS_BOOTFILE_PREFIX */
debug("TRANSITIONING TO REQUESTING STATE\n"); dhcp_state = REQUESTING;
- if (NetReadLong((ulong*)&bp->bp_vend[0]) == htonl(BOOTP_VENDOR_MAGIC)) + if (NetReadLong((ulong *)&bp->bp_vend[0]) == + htonl(BOOTP_VENDOR_MAGIC)) DhcpOptionsProcess((u8 *)&bp->bp_vend[4], bp);
NetSetTimeout(TIMEOUT, BootpTimeout); @@ -945,14 +948,17 @@ DhcpHandler(uchar *pkt, unsigned dest, IPaddr_t sip, unsigned src, case REQUESTING: debug("DHCP State: REQUESTING\n");
- if ( DhcpMessageType((u8 *)bp->bp_vend) == DHCP_ACK ) { - if (NetReadLong((ulong*)&bp->bp_vend[0]) == htonl(BOOTP_VENDOR_MAGIC)) + if (DhcpMessageType((u8 *)bp->bp_vend) == DHCP_ACK) { + if (NetReadLong((ulong *)&bp->bp_vend[0]) == + htonl(BOOTP_VENDOR_MAGIC)) DhcpOptionsProcess((u8 *)&bp->bp_vend[4], bp); - BootpCopyNetParams(bp); /* Store net params from reply */ + /* Store net params from reply */ + BootpCopyNetParams(bp); dhcp_state = BOUND; - printf ("DHCP client bound to address %pI4\n", &NetOurIP); + printf("DHCP client bound to address %pI4\n", + &NetOurIP); bootstage_mark_name(BOOTSTAGE_ID_BOOTP_STOP, - "bootp_stop"); + "bootp_stop");
net_auto_load(); return; @@ -962,7 +968,7 @@ DhcpHandler(uchar *pkt, unsigned dest, IPaddr_t sip, unsigned src, /* DHCP client bound to address */ break; default: - puts ("DHCP: INVALID STATE\n"); + puts("DHCP: INVALID STATE\n"); break; }
diff --git a/net/bootp.h b/net/bootp.h index 50625ab..ce73734 100644 --- a/net/bootp.h +++ b/net/bootp.h @@ -10,7 +10,7 @@ #define __BOOTP_H__
#ifndef __NET_H__ -#include <net.h> +#include <net.h> #endif /* __NET_H__ */
/**********************************************************************/ @@ -19,35 +19,39 @@ * BOOTP header. */ #if defined(CONFIG_CMD_DHCP) -#define OPT_SIZE 312 /* Minimum DHCP Options size per RFC2131 - results in 576 byte pkt */ +/* Minimum DHCP Options size per RFC2131 - results in 576 byte pkt */ +#define OPT_SIZE 312 +#if defined(CONFIG_BOOTP_VENDOREX) +extern u8 *dhcp_vendorex_prep(u8 *e); /*rtn new e after add own opts. */ +extern u8 *dhcp_vendorex_proc(u8 *e); /*rtn next e if mine,else NULL */ +#endif #else #define OPT_SIZE 64 #endif
-typedef struct -{ - uchar bp_op; /* Operation */ +struct Bootp_t { + uchar bp_op; /* Operation */ # define OP_BOOTREQUEST 1 # define OP_BOOTREPLY 2 - uchar bp_htype; /* Hardware type */ + uchar bp_htype; /* Hardware type */ # define HWT_ETHER 1 - uchar bp_hlen; /* Hardware address length */ + uchar bp_hlen; /* Hardware address length */ # define HWL_ETHER 6 - uchar bp_hops; /* Hop count (gateway thing) */ - ulong bp_id; /* Transaction ID */ - ushort bp_secs; /* Seconds since boot */ - ushort bp_spare1; /* Alignment */ - IPaddr_t bp_ciaddr; /* Client IP address */ - IPaddr_t bp_yiaddr; /* Your (client) IP address */ - IPaddr_t bp_siaddr; /* Server IP address */ - IPaddr_t bp_giaddr; /* Gateway IP address */ - uchar bp_chaddr[16]; /* Client hardware address */ - char bp_sname[64]; /* Server host name */ - char bp_file[128]; /* Boot file name */ - char bp_vend[OPT_SIZE]; /* Vendor information */ -} Bootp_t; - -#define BOOTP_HDR_SIZE sizeof (Bootp_t) + uchar bp_hops; /* Hop count (gateway thing) */ + ulong bp_id; /* Transaction ID */ + ushort bp_secs; /* Seconds since boot */ + ushort bp_spare1; /* Alignment */ + IPaddr_t bp_ciaddr; /* Client IP address */ + IPaddr_t bp_yiaddr; /* Your (client) IP address */ + IPaddr_t bp_siaddr; /* Server IP address */ + IPaddr_t bp_giaddr; /* Gateway IP address */ + uchar bp_chaddr[16]; /* Client hardware address */ + char bp_sname[64]; /* Server host name */ + char bp_file[128]; /* Boot file name */ + char bp_vend[OPT_SIZE]; /* Vendor information */ +}; + +#define BOOTP_HDR_SIZE sizeof(struct Bootp_t) #define BOOTP_SIZE (ETHER_HDR_SIZE + IP_HDR_SIZE + BOOTP_HDR_SIZE)
/**********************************************************************/ @@ -56,16 +60,16 @@ typedef struct */
/* bootp.c */ -extern ulong BootpID; /* ID of cur BOOTP request */ -extern char BootFile[128]; /* Boot file name */ +extern ulong BootpID; /* ID of cur BOOTP request */ +extern char BootFile[128]; /* Boot file name */ extern int BootpTry; #ifdef CONFIG_BOOTP_RANDOM_DELAY -extern ulong seed1, seed2; /* seed for random BOOTP delay */ +extern ulong seed1, seed2; /* seed for random BOOTP delay */ #endif
/* Send a BOOTP request */ -extern void BootpRequest (void); +extern void BootpRequest(void);
/****************** DHCP Support *********************/ extern void DhcpRequest(void);

Hi Joe,
On Tue, Mar 27, 2012 at 4:42 PM, Joe Hershberger joe.hershberger@ni.com wrote:
Signed-off-by: Joe Hershberger joe.hershberger@ni.com Cc: Joe Hershberger joe.hershberger@gmail.com Cc: Simon Glass sjg@chromium.org Cc: Mike Frysinger vapier@gentoo.org
Changes for v2: - Split into smaller patches
net/bootp.c | 340 ++++++++++++++++++++++++++++++----------------------------- net/bootp.h | 56 +++++----- 2 files changed, 203 insertions(+), 193 deletions(-)
diff --git a/net/bootp.c b/net/bootp.c index 35654b4..d0a7da2 100644 --- a/net/bootp.c +++ b/net/bootp.c
@@ -64,37 +64,29 @@ static char *dhcpmsg2str(int type) } } #endif
-#if defined(CONFIG_BOOTP_VENDOREX) -extern u8 *dhcp_vendorex_prep (u8 *e); /*rtn new e after add own opts. */ -extern u8 *dhcp_vendorex_proc (u8 *e); /*rtn next e if mine,else NULL */ -#endif
#endif
static int BootpCheckPkt(uchar *pkt, unsigned dest, unsigned src, unsigned len) {
- Bootp_t *bp = (Bootp_t *) pkt;
- struct Bootp_t *bp = (struct Bootp_t *) pkt;
I'm not sure of the ordering of the serieses, but if this one comes first, then struct Bootp_t doesn't exist yet - it is only a typedef.
Regards, Simon

Signed-off-by: Joe Hershberger joe.hershberger@ni.com Cc: Joe Hershberger joe.hershberger@gmail.com Cc: Simon Glass sjg@chromium.org Cc: Mike Frysinger vapier@gentoo.org --- Changes for v2: - Split into smaller patches
net/eth.c | 67 +++++++++++++++++++++++++++++------------------------------- 1 files changed, 32 insertions(+), 35 deletions(-)
diff --git a/net/eth.c b/net/eth.c index 1aba089..05afbd3 100644 --- a/net/eth.c +++ b/net/eth.c @@ -87,7 +87,7 @@ static struct { int length; } eth_rcv_bufs[PKTBUFSRX];
-static unsigned int eth_rcv_current = 0, eth_rcv_last = 0; +static unsigned int eth_rcv_current, eth_rcv_last; #endif
static struct eth_device *eth_devices, *eth_current; @@ -139,11 +139,10 @@ struct eth_device *eth_get_dev_by_index(int index) return target_dev; }
-int eth_get_dev_index (void) +int eth_get_dev_index(void) { - if (!eth_current) { + if (!eth_current) return -1; - }
return eth_current->index; } @@ -175,7 +174,7 @@ int eth_write_hwaddr(struct eth_device *dev, const char *base_name,
if (memcmp(env_enetaddr, "\0\0\0\0\0\0", 6)) { if (memcmp(dev->enetaddr, "\0\0\0\0\0\0", 6) && - memcmp(dev->enetaddr, env_enetaddr, 6)) { + memcmp(dev->enetaddr, env_enetaddr, 6)) { printf("\nWarning: %s MAC addresses don't match:\n", dev->name); printf("Address in SROM is %pM\n", @@ -188,10 +187,9 @@ int eth_write_hwaddr(struct eth_device *dev, const char *base_name, }
if (dev->write_hwaddr && - !eth_mac_skip(eth_number) && - is_valid_ether_addr(dev->enetaddr)) { + !eth_mac_skip(eth_number) && + is_valid_ether_addr(dev->enetaddr)) ret = dev->write_hwaddr(dev); - }
return ret; } @@ -199,7 +197,7 @@ int eth_write_hwaddr(struct eth_device *dev, const char *base_name, int eth_register(struct eth_device *dev) { struct eth_device *d; - static int index = 0; + static int index;
assert(strlen(dev->name) < sizeof(dev->name));
@@ -207,7 +205,7 @@ int eth_register(struct eth_device *dev) eth_current = eth_devices = dev; eth_current_changed(); } else { - for (d=eth_devices; d->next!=eth_devices; d=d->next) + for (d = eth_devices; d->next != eth_devices; d = d->next) ; d->next = dev; } @@ -277,36 +275,37 @@ int eth_initialize(bd_t *bis) printf("Net Initialization Skipped\n");
if (!eth_devices) { - puts ("No ethernet found.\n"); + puts("No ethernet found.\n"); bootstage_error(BOOTSTAGE_ID_NET_ETH_START); } else { struct eth_device *dev = eth_devices; - char *ethprime = getenv ("ethprime"); + char *ethprime = getenv("ethprime");
bootstage_mark(BOOTSTAGE_ID_NET_ETH_INIT); do { if (dev->index) - puts (", "); + puts(", ");
printf("%s", dev->name);
- if (ethprime && strcmp (dev->name, ethprime) == 0) { + if (ethprime && strcmp(dev->name, ethprime) == 0) { eth_current = dev; - puts (" [PRIME]"); + puts(" [PRIME]"); }
if (strchr(dev->name, ' ')) - puts("\nWarning: eth device name has a space!\n"); + puts("\nWarning: eth device name has a space!" + "\n");
if (eth_write_hwaddr(dev, "eth", dev->index)) puts("\nWarning: failed to set MAC address\n");
dev = dev->next; num_devices++; - } while(dev != eth_devices); + } while (dev != eth_devices);
eth_current_changed(); - putc ('\n'); + putc('\n'); }
return num_devices; @@ -317,9 +316,9 @@ int eth_initialize(bd_t *bis) * mcast_addr: multicast ipaddr from which multicast Mac is made * join: 1=join, 0=leave. */ -int eth_mcast_join( IPaddr_t mcast_ip, u8 join) +int eth_mcast_join(IPaddr_t mcast_ip, u8 join) { - u8 mcast_mac[6]; + u8 mcast_mac[6]; if (!eth_current || !eth_current->mcast) return -1; mcast_mac[5] = htonl(mcast_ip) & 0xff; @@ -336,7 +335,7 @@ int eth_mcast_join( IPaddr_t mcast_ip, u8 join) * some other adapter -- hash tables */ #define CRCPOLY_LE 0xedb88320 -u32 ether_crc (size_t len, unsigned char const *p) +u32 ether_crc(size_t len, unsigned char const *p) { int i; u32 crc; @@ -363,7 +362,7 @@ int eth_init(bd_t *bis) struct eth_device *old_current, *dev;
if (!eth_current) { - puts ("No ethernet found.\n"); + puts("No ethernet found.\n"); return -1; }
@@ -383,7 +382,7 @@ int eth_init(bd_t *bis) do { debug("Trying %s\n", eth_current->name);
- if (eth_current->init(eth_current,bis) >= 0) { + if (eth_current->init(eth_current, bis) >= 0) { eth_current->state = ETH_STATE_ACTIVE;
return 0; @@ -471,38 +470,36 @@ int eth_receive(void *packet, int length)
void eth_try_another(int first_restart) { - static struct eth_device *first_failed = NULL; + static struct eth_device *first_failed; char *ethrotate;
/* * Do not rotate between network interfaces when * 'ethrotate' variable is set to 'no'. */ - if (((ethrotate = getenv ("ethrotate")) != NULL) && - (strcmp(ethrotate, "no") == 0)) + ethrotate = getenv("ethrotate"); + if ((ethrotate != NULL) && (strcmp(ethrotate, "no") == 0)) return;
if (!eth_current) return;
- if (first_restart) { + if (first_restart) first_failed = eth_current; - }
eth_current = eth_current->next;
eth_current_changed();
- if (first_failed == eth_current) { + if (first_failed == eth_current) NetRestartWrap = 1; - } }
void eth_set_current(void) { - static char *act = NULL; - static int env_changed_id = 0; - struct eth_device* old_current; + static char *act; + static int env_changed_id; + struct eth_device *old_current; int env_id;
if (!eth_current) /* XXX no current */ @@ -525,7 +522,7 @@ void eth_set_current(void) eth_current_changed(); }
-char *eth_get_name (void) +char *eth_get_name(void) { - return (eth_current ? eth_current->name : "unknown"); + return eth_current ? eth_current->name : "unknown"; }

Signed-off-by: Joe Hershberger joe.hershberger@ni.com Cc: Joe Hershberger joe.hershberger@gmail.com Cc: Simon Glass sjg@chromium.org Cc: Mike Frysinger vapier@gentoo.org --- Changes for v2: - Split into smaller patches
common/main.c | 1 - include/common.h | 4 ++++ include/net.h | 5 +++++ net/net.c | 22 ++++++++-------------- 4 files changed, 17 insertions(+), 15 deletions(-)
diff --git a/common/main.c b/common/main.c index db181d3..e4cf3b6 100644 --- a/common/main.c +++ b/common/main.c @@ -973,7 +973,6 @@ int readline_into_buffer(const char *const prompt, char *buffer, int timeout)
#ifdef CONFIG_SHOW_ACTIVITY while (!tstc()) { - extern void show_activity(int arg); show_activity(0); WATCHDOG_RESET(); } diff --git a/include/common.h b/include/common.h index 74d9704..b627ff4 100644 --- a/include/common.h +++ b/include/common.h @@ -801,6 +801,10 @@ int pcmcia_init (void);
#include <bootstage.h>
+#ifdef CONFIG_SHOW_ACTIVITY +void show_activity(int arg); +#endif + /* Multicore arch functions */ #ifdef CONFIG_MP int cpu_status(int nr); diff --git a/include/net.h b/include/net.h index cb3e36d..f87ef8c 100644 --- a/include/net.h +++ b/include/net.h @@ -432,6 +432,11 @@ extern int NetSendUDPPacket(uchar *ether, IPaddr_t dest, int dport, /* Processes a received packet */ extern void NetReceive(uchar *, int);
+#ifdef CONFIG_NETCONSOLE +void NcStart(void); +int nc_input_packet(uchar *pkt, unsigned dest, unsigned src, unsigned len); +#endif + /* * Check if autoload is enabled. If so, use either NFS or TFTP to download * the boot file. diff --git a/net/net.c b/net/net.c index 9a0417d..1435245 100644 --- a/net/net.c +++ b/net/net.c @@ -77,6 +77,7 @@ #include <common.h> #include <watchdog.h> #include <command.h> +#include <linux/compiler.h> #include <net.h> #include "bootp.h" #include "tftp.h" @@ -203,11 +204,6 @@ IPaddr_t NetNtpServerIP; int NetTimeOffset; #endif
-#ifdef CONFIG_NETCONSOLE -void NcStart(void); -int nc_input_packet(uchar *pkt, unsigned dest, unsigned src, unsigned len); -#endif - uchar PktBuf[(PKTBUFSRX+1) * PKTSIZE_ALIGN + PKTALIGN];
/* Receive packet */ @@ -527,10 +523,7 @@ restart: for (;;) { WATCHDOG_RESET(); #ifdef CONFIG_SHOW_ACTIVITY - { - extern void show_activity(int arg); - show_activity(1); - } + show_activity(1); #endif /* * Check the ethernet for a new packet. The ethernet @@ -1103,8 +1096,8 @@ CDPHandler(const uchar *pkt, unsigned len) * output a warning */ if (pkt[0] != 0x02) - printf("** WARNING: CDP packet received with a protocol version %d > 2\n", - pkt[0] & 0xff); + printf("**WARNING: CDP packet received with a protocol version " + "%d > 2\n", pkt[0] & 0xff);
if (CDP_compute_csum(pkt, len) != 0) return; @@ -1239,7 +1232,7 @@ struct hole {
static IP_t *__NetDefragment(IP_t *ip, int *lenp) { - static uchar pkt_buff[IP_PKTSIZE] __attribute__((aligned(PKTALIGN))); + static uchar pkt_buff[IP_PKTSIZE] __aligned(PKTALIGN); static u16 first_hole, total_len; struct hole *payload, *thisfrag, *h, *newh; IP_t *localip = (IP_t *)pkt_buff; @@ -1611,6 +1604,7 @@ NetReceive(uchar *inpkt, int len) /* matched waiting packet's address */ if (tmp == NetArpWaitReplyIP) { debug("Got it\n"); + /* save address for later use */ memcpy(NetArpWaitPacketMAC, &arp->ar_data[0], 6); @@ -1619,7 +1613,8 @@ NetReceive(uchar *inpkt, int len) (*packetHandler)(0, 0, 0, 0, 0); #endif /* modify header, and transmit it */ - memcpy(((Ethernet_t *)NetArpWaitTxPacket)->et_dest, NetArpWaitPacketMAC, 6); + memcpy(((Ethernet_t *)NetArpWaitTxPacket)-> + et_dest, NetArpWaitPacketMAC, 6); (void) eth_send(NetArpWaitTxPacket, NetArpWaitTxPacketSize);
@@ -1856,7 +1851,6 @@ common: case CDP: case DHCP: if (memcmp(NetOurEther, "\0\0\0\0\0\0", 6) == 0) { - extern int eth_get_dev_index(void); int num = eth_get_dev_index();
switch (num) {

Signed-off-by: Joe Hershberger joe.hershberger@ni.com Cc: Joe Hershberger joe.hershberger@gmail.com Cc: Simon Glass sjg@chromium.org Cc: Mike Frysinger vapier@gentoo.org --- Changes for v2: - Split into smaller patches
net/nfs.c | 300 ++++++++++++++++++++++++++++++------------------------------- net/nfs.h | 2 +- 2 files changed, 150 insertions(+), 152 deletions(-)
diff --git a/net/nfs.c b/net/nfs.c index b5b482c..54f56c4 100644 --- a/net/nfs.c +++ b/net/nfs.c @@ -33,8 +33,8 @@ #define NFS_RETRY_COUNT 30 #define NFS_TIMEOUT 2000UL
-static int fs_mounted = 0; -static unsigned long rpc_id = 0; +static int fs_mounted; +static unsigned long rpc_id; static int nfs_offset = -1; static int nfs_len;
@@ -61,14 +61,14 @@ static char *nfs_filename; static char *nfs_path; static char nfs_path_buff[2048];
-static __inline__ int -store_block (uchar * src, unsigned offset, unsigned len) +static inline int +store_block(uchar *src, unsigned offset, unsigned len) { ulong newsize = offset + len; #ifdef CONFIG_SYS_DIRECT_FLASH_NFS int i, rc = 0;
- for (i=0; i<CONFIG_SYS_MAX_FLASH_BANKS; i++) { + for (i = 0; i < CONFIG_SYS_MAX_FLASH_BANKS; i++) { /* start address in flash? */ if (load_addr + offset >= flash_info[i].start[0]) { rc = 1; @@ -77,15 +77,15 @@ store_block (uchar * src, unsigned offset, unsigned len) }
if (rc) { /* Flash is destination for this packet */ - rc = flash_write ((uchar *)src, (ulong)(load_addr+offset), len); + rc = flash_write((uchar *)src, (ulong)(load_addr+offset), len); if (rc) { - flash_perror (rc); + flash_perror(rc); return -1; } } else #endif /* CONFIG_SYS_DIRECT_FLASH_NFS */ { - (void)memcpy ((void *)(load_addr + offset), src, len); + (void)memcpy((void *)(load_addr + offset), src, len); }
if (NetBootFileXferSize < (offset+len)) @@ -94,7 +94,7 @@ store_block (uchar * src, unsigned offset, unsigned len) }
static char* -basename (char *path) +basename(char *path) { char *fname;
@@ -110,11 +110,11 @@ basename (char *path) }
static char* -dirname (char *path) +dirname(char *path) { char *fname;
- fname = basename (path); + fname = basename(path); --fname; *fname = '\0'; return path; @@ -123,14 +123,14 @@ dirname (char *path) /************************************************************************** RPC_ADD_CREDENTIALS - Add RPC authentication/verifier entries **************************************************************************/ -static long *rpc_add_credentials (long *p) +static long *rpc_add_credentials(long *p) { int hl; int hostnamelen; char hostname[256];
- strcpy (hostname, ""); - hostnamelen=strlen (hostname); + strcpy(hostname, ""); + hostnamelen = strlen(hostname);
/* Here's the executive summary on authentication requirements of the * various NFS server implementations: Linux accepts both AUTH_NONE @@ -148,10 +148,9 @@ static long *rpc_add_credentials (long *p) *p++ = htonl(hl+20); /* auth length */ *p++ = htonl(0); /* stamp */ *p++ = htonl(hostnamelen); /* hostname string */ - if (hostnamelen & 3) { + if (hostnamelen & 3) *(p + hostnamelen / 4) = 0; /* add zero padding */ - } - memcpy (p, hostname, hostnamelen); + memcpy(p, hostname, hostnamelen); p += hl / 4; *p++ = 0; /* uid */ *p++ = 0; /* gid */ @@ -168,7 +167,7 @@ static long *rpc_add_credentials (long *p) RPC_LOOKUP - Lookup RPC Port numbers **************************************************************************/ static void -rpc_req (int rpc_prog, int rpc_proc, uint32_t *data, int datalen) +rpc_req(int rpc_prog, int rpc_proc, uint32_t *data, int datalen) { struct rpc_t pkt; unsigned long id; @@ -186,11 +185,12 @@ rpc_req (int rpc_prog, int rpc_proc, uint32_t *data, int datalen) p = (uint32_t *)&(pkt.u.call.data);
if (datalen) - memcpy ((char *)p, (char *)data, datalen*sizeof(uint32_t)); + memcpy((char *)p, (char *)data, datalen*sizeof(uint32_t));
pktlen = (char *)p + datalen*sizeof(uint32_t) - (char *)&pkt;
- memcpy ((char *)NetTxPacket + NetEthHdrSize() + IP_HDR_SIZE, (char *)&pkt, pktlen); + memcpy((char *)NetTxPacket + NetEthHdrSize() + IP_HDR_SIZE, + (char *)&pkt, pktlen);
if (rpc_prog == PROG_PORTMAP) sport = SUNRPC_PORT; @@ -199,14 +199,15 @@ rpc_req (int rpc_prog, int rpc_proc, uint32_t *data, int datalen) else sport = NfsSrvNfsPort;
- NetSendUDPPacket (NetServerEther, NfsServerIP, sport, NfsOurPort, pktlen); + NetSendUDPPacket(NetServerEther, NfsServerIP, sport, NfsOurPort, + pktlen); }
/************************************************************************** RPC_LOOKUP - Lookup RPC Port numbers **************************************************************************/ static void -rpc_lookup_req (int prog, int ver) +rpc_lookup_req(int prog, int ver) { uint32_t data[16];
@@ -217,56 +218,56 @@ rpc_lookup_req (int prog, int ver) data[6] = htonl(17); /* IP_UDP */ data[7] = 0;
- rpc_req (PROG_PORTMAP, PORTMAP_GETPORT, data, 8); + rpc_req(PROG_PORTMAP, PORTMAP_GETPORT, data, 8); }
/************************************************************************** NFS_MOUNT - Mount an NFS Filesystem **************************************************************************/ static void -nfs_mount_req (char *path) +nfs_mount_req(char *path) { uint32_t data[1024]; uint32_t *p; int len; int pathlen;
- pathlen = strlen (path); + pathlen = strlen(path);
p = &(data[0]); p = (uint32_t *)rpc_add_credentials((long *)p);
*p++ = htonl(pathlen); - if (pathlen & 3) *(p + pathlen / 4) = 0; - memcpy (p, path, pathlen); + if (pathlen & 3) + *(p + pathlen / 4) = 0; + memcpy(p, path, pathlen); p += (pathlen + 3) / 4;
len = (uint32_t *)p - (uint32_t *)&(data[0]);
- rpc_req (PROG_MOUNT, MOUNT_ADDENTRY, data, len); + rpc_req(PROG_MOUNT, MOUNT_ADDENTRY, data, len); }
/************************************************************************** NFS_UMOUNTALL - Unmount all our NFS Filesystems on the Server **************************************************************************/ static void -nfs_umountall_req (void) +nfs_umountall_req(void) { uint32_t data[1024]; uint32_t *p; int len;
- if ((NfsSrvMountPort == -1) || (!fs_mounted)) { + if ((NfsSrvMountPort == -1) || (!fs_mounted)) /* Nothing mounted, nothing to umount */ return; - }
p = &(data[0]); - p = (uint32_t *)rpc_add_credentials ((long *)p); + p = (uint32_t *)rpc_add_credentials((long *)p);
len = (uint32_t *)p - (uint32_t *)&(data[0]);
- rpc_req (PROG_MOUNT, MOUNT_UMOUNTALL, data, len); + rpc_req(PROG_MOUNT, MOUNT_UMOUNTALL, data, len); }
/*************************************************************************** @@ -277,65 +278,66 @@ nfs_umountall_req (void) * so that inside the nfs() function a recursion can be done. **************************************************************************/ static void -nfs_readlink_req (void) +nfs_readlink_req(void) { uint32_t data[1024]; uint32_t *p; int len;
p = &(data[0]); - p = (uint32_t *)rpc_add_credentials ((long *)p); + p = (uint32_t *)rpc_add_credentials((long *)p);
- memcpy (p, filefh, NFS_FHSIZE); + memcpy(p, filefh, NFS_FHSIZE); p += (NFS_FHSIZE / 4);
len = (uint32_t *)p - (uint32_t *)&(data[0]);
- rpc_req (PROG_NFS, NFS_READLINK, data, len); + rpc_req(PROG_NFS, NFS_READLINK, data, len); }
/************************************************************************** NFS_LOOKUP - Lookup Pathname **************************************************************************/ static void -nfs_lookup_req (char *fname) +nfs_lookup_req(char *fname) { uint32_t data[1024]; uint32_t *p; int len; int fnamelen;
- fnamelen = strlen (fname); + fnamelen = strlen(fname);
p = &(data[0]); - p = (uint32_t *)rpc_add_credentials ((long *)p); + p = (uint32_t *)rpc_add_credentials((long *)p);
- memcpy (p, dirfh, NFS_FHSIZE); + memcpy(p, dirfh, NFS_FHSIZE); p += (NFS_FHSIZE / 4); *p++ = htonl(fnamelen); - if (fnamelen & 3) *(p + fnamelen / 4) = 0; - memcpy (p, fname, fnamelen); + if (fnamelen & 3) + *(p + fnamelen / 4) = 0; + memcpy(p, fname, fnamelen); p += (fnamelen + 3) / 4;
len = (uint32_t *)p - (uint32_t *)&(data[0]);
- rpc_req (PROG_NFS, NFS_LOOKUP, data, len); + rpc_req(PROG_NFS, NFS_LOOKUP, data, len); }
/************************************************************************** NFS_READ - Read File on NFS Server **************************************************************************/ static void -nfs_read_req (int offset, int readlen) +nfs_read_req(int offset, int readlen) { uint32_t data[1024]; uint32_t *p; int len;
p = &(data[0]); - p = (uint32_t *)rpc_add_credentials ((long *)p); + p = (uint32_t *)rpc_add_credentials((long *)p);
- memcpy (p, filefh, NFS_FHSIZE); + memcpy(p, filefh, NFS_FHSIZE); p += (NFS_FHSIZE / 4); *p++ = htonl(offset); *p++ = htonl(readlen); @@ -343,7 +345,7 @@ nfs_read_req (int offset, int readlen)
len = (uint32_t *)p - (uint32_t *)&(data[0]);
- rpc_req (PROG_NFS, NFS_READ, data, len); + rpc_req(PROG_NFS, NFS_READ, data, len); }
/************************************************************************** @@ -351,31 +353,31 @@ RPC request dispatcher **************************************************************************/
static void -NfsSend (void) +NfsSend(void) { debug("%s\n", __func__);
switch (NfsState) { case STATE_PRCLOOKUP_PROG_MOUNT_REQ: - rpc_lookup_req (PROG_MOUNT, 1); + rpc_lookup_req(PROG_MOUNT, 1); break; case STATE_PRCLOOKUP_PROG_NFS_REQ: - rpc_lookup_req (PROG_NFS, 2); + rpc_lookup_req(PROG_NFS, 2); break; case STATE_MOUNT_REQ: - nfs_mount_req (nfs_path); + nfs_mount_req(nfs_path); break; case STATE_UMOUNT_REQ: - nfs_umountall_req (); + nfs_umountall_req(); break; case STATE_LOOKUP_REQ: - nfs_lookup_req (nfs_filename); + nfs_lookup_req(nfs_filename); break; case STATE_READ_REQ: - nfs_read_req (nfs_offset, nfs_len); + nfs_read_req(nfs_offset, nfs_len); break; case STATE_READLINK_REQ: - nfs_readlink_req (); + nfs_readlink_req(); break; } } @@ -385,11 +387,11 @@ Handlers for the reply from server **************************************************************************/
static int -rpc_lookup_reply (int prog, uchar *pkt, unsigned len) +rpc_lookup_reply(int prog, uchar *pkt, unsigned len) { struct rpc_t rpc_pkt;
- memcpy ((unsigned char *)&rpc_pkt, pkt, len); + memcpy((unsigned char *)&rpc_pkt, pkt, len);
debug("%s\n", __func__);
@@ -398,9 +400,8 @@ rpc_lookup_reply (int prog, uchar *pkt, unsigned len)
if (rpc_pkt.u.reply.rstatus || rpc_pkt.u.reply.verifier || - rpc_pkt.u.reply.astatus) { + rpc_pkt.u.reply.astatus) return -1; - }
switch (prog) { case PROG_MOUNT: @@ -415,13 +416,13 @@ rpc_lookup_reply (int prog, uchar *pkt, unsigned len) }
static int -nfs_mount_reply (uchar *pkt, unsigned len) +nfs_mount_reply(uchar *pkt, unsigned len) { struct rpc_t rpc_pkt;
debug("%s\n", __func__);
- memcpy ((unsigned char *)&rpc_pkt, pkt, len); + memcpy((unsigned char *)&rpc_pkt, pkt, len);
if (ntohl(rpc_pkt.u.reply.id) != rpc_id) return -1; @@ -429,48 +430,46 @@ nfs_mount_reply (uchar *pkt, unsigned len) if (rpc_pkt.u.reply.rstatus || rpc_pkt.u.reply.verifier || rpc_pkt.u.reply.astatus || - rpc_pkt.u.reply.data[0]) { + rpc_pkt.u.reply.data[0]) return -1; - }
fs_mounted = 1; - memcpy (dirfh, rpc_pkt.u.reply.data + 1, NFS_FHSIZE); + memcpy(dirfh, rpc_pkt.u.reply.data + 1, NFS_FHSIZE);
return 0; }
static int -nfs_umountall_reply (uchar *pkt, unsigned len) +nfs_umountall_reply(uchar *pkt, unsigned len) { struct rpc_t rpc_pkt;
debug("%s\n", __func__);
- memcpy ((unsigned char *)&rpc_pkt, pkt, len); + memcpy((unsigned char *)&rpc_pkt, pkt, len);
if (ntohl(rpc_pkt.u.reply.id) != rpc_id) return -1;
if (rpc_pkt.u.reply.rstatus || rpc_pkt.u.reply.verifier || - rpc_pkt.u.reply.astatus) { + rpc_pkt.u.reply.astatus) return -1; - }
fs_mounted = 0; - memset (dirfh, 0, sizeof(dirfh)); + memset(dirfh, 0, sizeof(dirfh));
return 0; }
static int -nfs_lookup_reply (uchar *pkt, unsigned len) +nfs_lookup_reply(uchar *pkt, unsigned len) { struct rpc_t rpc_pkt;
debug("%s\n", __func__);
- memcpy ((unsigned char *)&rpc_pkt, pkt, len); + memcpy((unsigned char *)&rpc_pkt, pkt, len);
if (ntohl(rpc_pkt.u.reply.id) != rpc_id) return -1; @@ -478,24 +477,23 @@ nfs_lookup_reply (uchar *pkt, unsigned len) if (rpc_pkt.u.reply.rstatus || rpc_pkt.u.reply.verifier || rpc_pkt.u.reply.astatus || - rpc_pkt.u.reply.data[0]) { + rpc_pkt.u.reply.data[0]) return -1; - }
- memcpy (filefh, rpc_pkt.u.reply.data + 1, NFS_FHSIZE); + memcpy(filefh, rpc_pkt.u.reply.data + 1, NFS_FHSIZE);
return 0; }
static int -nfs_readlink_reply (uchar *pkt, unsigned len) +nfs_readlink_reply(uchar *pkt, unsigned len) { struct rpc_t rpc_pkt; int rlen;
debug("%s\n", __func__);
- memcpy ((unsigned char *)&rpc_pkt, pkt, len); + memcpy((unsigned char *)&rpc_pkt, pkt, len);
if (ntohl(rpc_pkt.u.reply.id) != rpc_id) return -1; @@ -503,34 +501,34 @@ nfs_readlink_reply (uchar *pkt, unsigned len) if (rpc_pkt.u.reply.rstatus || rpc_pkt.u.reply.verifier || rpc_pkt.u.reply.astatus || - rpc_pkt.u.reply.data[0]) { + rpc_pkt.u.reply.data[0]) return -1; - }
- rlen = ntohl (rpc_pkt.u.reply.data[1]); /* new path length */ + rlen = ntohl(rpc_pkt.u.reply.data[1]); /* new path length */
if (*((char *)&(rpc_pkt.u.reply.data[2])) != '/') { int pathlen; - strcat (nfs_path, "/"); + strcat(nfs_path, "/"); pathlen = strlen(nfs_path); - memcpy (nfs_path+pathlen, (uchar *)&(rpc_pkt.u.reply.data[2]), rlen); + memcpy(nfs_path + pathlen, (uchar *)&(rpc_pkt.u.reply.data[2]), + rlen); nfs_path[pathlen + rlen] = 0; } else { - memcpy (nfs_path, (uchar *)&(rpc_pkt.u.reply.data[2]), rlen); + memcpy(nfs_path, (uchar *)&(rpc_pkt.u.reply.data[2]), rlen); nfs_path[rlen] = 0; } return 0; }
static int -nfs_read_reply (uchar *pkt, unsigned len) +nfs_read_reply(uchar *pkt, unsigned len) { struct rpc_t rpc_pkt; int rlen;
debug("%s\n", __func__);
- memcpy ((uchar *)&rpc_pkt, pkt, sizeof(rpc_pkt.u.reply)); + memcpy((uchar *)&rpc_pkt, pkt, sizeof(rpc_pkt.u.reply));
if (ntohl(rpc_pkt.u.reply.id) != rpc_id) return -1; @@ -539,24 +537,22 @@ nfs_read_reply (uchar *pkt, unsigned len) rpc_pkt.u.reply.verifier || rpc_pkt.u.reply.astatus || rpc_pkt.u.reply.data[0]) { - if (rpc_pkt.u.reply.rstatus) { + if (rpc_pkt.u.reply.rstatus) return -9999; - } - if (rpc_pkt.u.reply.astatus) { + if (rpc_pkt.u.reply.astatus) return -9999; - } - return -ntohl(rpc_pkt.u.reply.data[0]);; + return -ntohl(rpc_pkt.u.reply.data[0]); }
- if ((nfs_offset!=0) && !((nfs_offset) % (NFS_READ_SIZE/2*10*HASHES_PER_LINE))) { - puts ("\n\t "); - } - if (!(nfs_offset % ((NFS_READ_SIZE/2)*10))) { - putc ('#'); - } + if ((nfs_offset != 0) && !((nfs_offset) % + (NFS_READ_SIZE / 2 * 10 * HASHES_PER_LINE))) + puts("\n\t "); + if (!(nfs_offset % ((NFS_READ_SIZE / 2) * 10))) + putc('#');
rlen = ntohl(rpc_pkt.u.reply.data[18]); - if ( store_block ((uchar *)pkt+sizeof(rpc_pkt.u.reply), nfs_offset, rlen) ) + if (store_block((uchar *)pkt + sizeof(rpc_pkt.u.reply), + nfs_offset, rlen)) return -9999;
return rlen; @@ -567,15 +563,15 @@ Interfaces of U-BOOT **************************************************************************/
static void -NfsTimeout (void) +NfsTimeout(void) { - if ( ++NfsTimeoutCount > NFS_RETRY_COUNT ) { - puts ("\nRetry count exceeded; starting again\n"); - NetStartAgain (); + if (++NfsTimeoutCount > NFS_RETRY_COUNT) { + puts("\nRetry count exceeded; starting again\n"); + NetStartAgain(); } else { puts("T "); - NetSetTimeout (NFS_TIMEOUT, NfsTimeout); - NfsSend (); + NetSetTimeout(NFS_TIMEOUT, NfsTimeout); + NfsSend(); } }
@@ -586,86 +582,87 @@ NfsHandler(uchar *pkt, unsigned dest, IPaddr_t sip, unsigned src, unsigned len)
debug("%s\n", __func__);
- if (dest != NfsOurPort) return; + if (dest != NfsOurPort) + return;
switch (NfsState) { case STATE_PRCLOOKUP_PROG_MOUNT_REQ: - rpc_lookup_reply (PROG_MOUNT, pkt, len); + rpc_lookup_reply(PROG_MOUNT, pkt, len); NfsState = STATE_PRCLOOKUP_PROG_NFS_REQ; - NfsSend (); + NfsSend(); break;
case STATE_PRCLOOKUP_PROG_NFS_REQ: - rpc_lookup_reply (PROG_NFS, pkt, len); + rpc_lookup_reply(PROG_NFS, pkt, len); NfsState = STATE_MOUNT_REQ; - NfsSend (); + NfsSend(); break;
case STATE_MOUNT_REQ: if (nfs_mount_reply(pkt, len)) { - puts ("*** ERROR: Cannot mount\n"); + puts("*** ERROR: Cannot mount\n"); /* just to be sure... */ NfsState = STATE_UMOUNT_REQ; - NfsSend (); + NfsSend(); } else { NfsState = STATE_LOOKUP_REQ; - NfsSend (); + NfsSend(); } break;
case STATE_UMOUNT_REQ: if (nfs_umountall_reply(pkt, len)) { - puts ("*** ERROR: Cannot umount\n"); + puts("*** ERROR: Cannot umount\n"); NetState = NETLOOP_FAIL; } else { - puts ("\ndone\n"); + puts("\ndone\n"); NetState = NfsDownloadState; } break;
case STATE_LOOKUP_REQ: if (nfs_lookup_reply(pkt, len)) { - puts ("*** ERROR: File lookup fail\n"); + puts("*** ERROR: File lookup fail\n"); NfsState = STATE_UMOUNT_REQ; - NfsSend (); + NfsSend(); } else { NfsState = STATE_READ_REQ; nfs_offset = 0; nfs_len = NFS_READ_SIZE; - NfsSend (); + NfsSend(); } break;
case STATE_READLINK_REQ: if (nfs_readlink_reply(pkt, len)) { - puts ("*** ERROR: Symlink fail\n"); + puts("*** ERROR: Symlink fail\n"); NfsState = STATE_UMOUNT_REQ; - NfsSend (); + NfsSend(); } else { debug("Symlink --> %s\n", nfs_path); - nfs_filename = basename (nfs_path); - nfs_path = dirname (nfs_path); + nfs_filename = basename(nfs_path); + nfs_path = dirname(nfs_path);
NfsState = STATE_MOUNT_REQ; - NfsSend (); + NfsSend(); } break;
case STATE_READ_REQ: - rlen = nfs_read_reply (pkt, len); - NetSetTimeout (NFS_TIMEOUT, NfsTimeout); + rlen = nfs_read_reply(pkt, len); + NetSetTimeout(NFS_TIMEOUT, NfsTimeout); if (rlen > 0) { nfs_offset += rlen; - NfsSend (); - } - else if ((rlen == -NFSERR_ISDIR)||(rlen == -NFSERR_INVAL)) { + NfsSend(); + } else if ((rlen == -NFSERR_ISDIR) || (rlen == -NFSERR_INVAL)) { /* symbolic link */ NfsState = STATE_READLINK_REQ; - NfsSend (); + NfsSend(); } else { - if ( ! rlen ) NfsDownloadState = NETLOOP_SUCCESS; + if (!rlen) + NfsDownloadState = NETLOOP_SUCCESS; NfsState = STATE_UMOUNT_REQ; - NfsSend (); + NfsSend(); } break; } @@ -673,7 +670,7 @@ NfsHandler(uchar *pkt, unsigned dest, IPaddr_t sip, unsigned src, unsigned len)
void -NfsStart (void) +NfsStart(void) { debug("%s\n", __func__); NfsDownloadState = NETLOOP_FAIL; @@ -683,7 +680,7 @@ NfsStart (void)
if (nfs_path == NULL) { NetState = NETLOOP_FAIL; - puts ("*** ERROR: Fail allocate memory\n"); + puts("*** ERROR: Fail allocate memory\n"); return; }
@@ -692,29 +689,29 @@ NfsStart (void) NetOurIP & 0xFF, (NetOurIP >> 8) & 0xFF, (NetOurIP >> 16) & 0xFF, - (NetOurIP >> 24) & 0xFF ); - strcpy (nfs_path, default_filename); + (NetOurIP >> 24) & 0xFF); + strcpy(nfs_path, default_filename);
- printf ("*** Warning: no boot file name; using '%s'\n", + printf("*** Warning: no boot file name; using '%s'\n", nfs_path); } else { - char *p=BootFile; + char *p = BootFile;
- p = strchr (p, ':'); + p = strchr(p, ':');
if (p != NULL) { - NfsServerIP = string_to_ip (BootFile); + NfsServerIP = string_to_ip(BootFile); ++p; - strcpy (nfs_path, p); + strcpy(nfs_path, p); } else { - strcpy (nfs_path, BootFile); + strcpy(nfs_path, BootFile); } }
- nfs_filename = basename (nfs_path); - nfs_path = dirname (nfs_path); + nfs_filename = basename(nfs_path); + nfs_path = dirname(nfs_path);
- printf ("Using %s device\n", eth_get_name()); + printf("Using %s device\n", eth_get_name());
printf("File transfer via NFS from server %pI4" "; our IP address is %pI4", &NfsServerIP, &NetOurIP); @@ -725,19 +722,20 @@ NfsStart (void) IPaddr_t ServerNet = NetServerIP & NetOurSubnetMask;
if (OurNet != ServerNet) - printf("; sending through gateway %pI4", &NetOurGatewayIP); + printf("; sending through gateway %pI4", + &NetOurGatewayIP); } - printf ("\nFilename '%s/%s'.", nfs_path, nfs_filename); + printf("\nFilename '%s/%s'.", nfs_path, nfs_filename);
if (NetBootFileSize) { - printf (" Size is 0x%x Bytes = ", NetBootFileSize<<9); - print_size (NetBootFileSize<<9, ""); + printf(" Size is 0x%x Bytes = ", NetBootFileSize<<9); + print_size(NetBootFileSize<<9, ""); } - printf ("\nLoad address: 0x%lx\n" + printf("\nLoad address: 0x%lx\n" "Loading: *\b", load_addr);
- NetSetTimeout (NFS_TIMEOUT, NfsTimeout); - NetSetHandler (NfsHandler); + NetSetTimeout(NFS_TIMEOUT, NfsTimeout); + NetSetHandler(NfsHandler);
NfsTimeoutCount = 0; NfsState = STATE_PRCLOOKUP_PROG_MOUNT_REQ; @@ -747,7 +745,7 @@ NfsStart (void) NfsOurPort = 1000;
/* zero out server ether in case the server ip has changed */ - memset (NetServerEther, 0, 6); + memset(NetServerEther, 0, 6);
- NfsSend (); + NfsSend(); } diff --git a/net/nfs.h b/net/nfs.h index de8a0c6..a5a1b43 100644 --- a/net/nfs.h +++ b/net/nfs.h @@ -72,7 +72,7 @@ struct rpc_t { } reply; } u; }; -extern void NfsStart (void); /* Begin NFS */ +extern void NfsStart(void); /* Begin NFS */
/**********************************************************************/

Hi Joe,
On Tue, Mar 27, 2012 at 4:42 PM, Joe Hershberger joe.hershberger@ni.com wrote:
Signed-off-by: Joe Hershberger joe.hershberger@ni.com Cc: Joe Hershberger joe.hershberger@gmail.com Cc: Simon Glass sjg@chromium.org Cc: Mike Frysinger vapier@gentoo.org
Looks good.
Acked-by: Simon Glass sjg@chromium.org

Signed-off-by: Joe Hershberger joe.hershberger@ni.com Cc: Joe Hershberger joe.hershberger@gmail.com Cc: Simon Glass sjg@chromium.org Cc: Mike Frysinger vapier@gentoo.org --- Changes for v2: - Split into smaller patches
net/rarp.c | 28 ++++++++++++++-------------- net/rarp.h | 4 ++-- 2 files changed, 16 insertions(+), 16 deletions(-)
diff --git a/net/rarp.c b/net/rarp.c index 77d63e8..5a813a2 100644 --- a/net/rarp.c +++ b/net/rarp.c @@ -58,21 +58,21 @@ static void RarpTimeout(void) { if (RarpTry >= TIMEOUT_COUNT) { - puts ("\nRetry count exceeded; starting again\n"); - NetStartAgain (); + puts("\nRetry count exceeded; starting again\n"); + NetStartAgain(); } else { - NetSetTimeout (TIMEOUT, RarpTimeout); - RarpRequest (); + NetSetTimeout(TIMEOUT, RarpTimeout); + RarpRequest(); } }
void -RarpRequest (void) +RarpRequest(void) { int i; uchar *pkt; - ARP_t * rarp; + ARP_t *rarp;
printf("RARP broadcast %d\n", ++RarpTry); pkt = NetTxPacket; @@ -81,18 +81,18 @@ RarpRequest (void)
rarp = (ARP_t *)pkt;
- rarp->ar_hrd = htons (ARP_ETHER); - rarp->ar_pro = htons (PROT_IP); + rarp->ar_hrd = htons(ARP_ETHER); + rarp->ar_pro = htons(PROT_IP); rarp->ar_hln = 6; rarp->ar_pln = 4; - rarp->ar_op = htons (RARPOP_REQUEST); - memcpy (&rarp->ar_data[0], NetOurEther, 6); /* source ET addr */ - memcpy (&rarp->ar_data[6], &NetOurIP, 4); /* source IP addr */ - memcpy (&rarp->ar_data[10], NetOurEther, 6); /* dest ET addr = source ET addr ??*/ + rarp->ar_op = htons(RARPOP_REQUEST); + memcpy(&rarp->ar_data[0], NetOurEther, 6); /* source ET addr */ + memcpy(&rarp->ar_data[6], &NetOurIP, 4); /* source IP addr */ + /* dest ET addr = source ET addr ??*/ + memcpy(&rarp->ar_data[10], NetOurEther, 6); /* dest. IP addr set to broadcast */ - for (i = 0; i <= 3; i++) { + for (i = 0; i <= 3; i++) rarp->ar_data[16 + i] = 0xff; - }
NetSendPacket(NetTxPacket, (pkt - NetTxPacket) + ARP_HDR_SIZE);
diff --git a/net/rarp.h b/net/rarp.h index 0c16d46..4e92d80 100644 --- a/net/rarp.h +++ b/net/rarp.h @@ -35,9 +35,9 @@ * Global functions and variables. */
-extern int RarpTry; +extern int RarpTry;
-extern void RarpRequest (void); /* Send a RARP request */ +extern void RarpRequest(void); /* Send a RARP request */
/**********************************************************************/

On Tue, Mar 27, 2012 at 4:42 PM, Joe Hershberger joe.hershberger@ni.com wrote:
Signed-off-by: Joe Hershberger joe.hershberger@ni.com Cc: Joe Hershberger joe.hershberger@gmail.com Cc: Simon Glass sjg@chromium.org Cc: Mike Frysinger vapier@gentoo.org
Acked-by: Simon Glass sjg@chromium.org
Changes for v2: - Split into smaller patches
net/rarp.c | 28 ++++++++++++++-------------- net/rarp.h | 4 ++-- 2 files changed, 16 insertions(+), 16 deletions(-)
diff --git a/net/rarp.c b/net/rarp.c index 77d63e8..5a813a2 100644 --- a/net/rarp.c +++ b/net/rarp.c @@ -58,21 +58,21 @@ static void RarpTimeout(void) { if (RarpTry >= TIMEOUT_COUNT) {
- puts ("\nRetry count exceeded; starting again\n");
- NetStartAgain ();
- puts("\nRetry count exceeded; starting again\n");
- NetStartAgain();
} else {
- NetSetTimeout (TIMEOUT, RarpTimeout);
- RarpRequest ();
- NetSetTimeout(TIMEOUT, RarpTimeout);
- RarpRequest();
} }
void -RarpRequest (void) +RarpRequest(void) { int i; uchar *pkt;
- ARP_t * rarp;
- ARP_t *rarp;
printf("RARP broadcast %d\n", ++RarpTry); pkt = NetTxPacket; @@ -81,18 +81,18 @@ RarpRequest (void)
rarp = (ARP_t *)pkt;
- rarp->ar_hrd = htons (ARP_ETHER);
- rarp->ar_pro = htons (PROT_IP);
- rarp->ar_hrd = htons(ARP_ETHER);
- rarp->ar_pro = htons(PROT_IP);
rarp->ar_hln = 6; rarp->ar_pln = 4;
- rarp->ar_op = htons (RARPOP_REQUEST);
- memcpy (&rarp->ar_data[0], NetOurEther, 6); /* source ET addr */
- memcpy (&rarp->ar_data[6], &NetOurIP, 4); /* source IP addr */
- memcpy (&rarp->ar_data[10], NetOurEther, 6); /* dest ET addr = source ET addr ??*/
- rarp->ar_op = htons(RARPOP_REQUEST);
- memcpy(&rarp->ar_data[0], NetOurEther, 6); /* source ET addr */
- memcpy(&rarp->ar_data[6], &NetOurIP, 4); /* source IP addr */
- /* dest ET addr = source ET addr ??*/
- memcpy(&rarp->ar_data[10], NetOurEther, 6);
/* dest. IP addr set to broadcast */
- for (i = 0; i <= 3; i++) {
- for (i = 0; i <= 3; i++)
rarp->ar_data[16 + i] = 0xff;
- }
NetSendPacket(NetTxPacket, (pkt - NetTxPacket) + ARP_HDR_SIZE);
diff --git a/net/rarp.h b/net/rarp.h index 0c16d46..4e92d80 100644 --- a/net/rarp.h +++ b/net/rarp.h @@ -35,9 +35,9 @@ * Global functions and variables. */
-extern int RarpTry; +extern int RarpTry;
-extern void RarpRequest (void); /* Send a RARP request */ +extern void RarpRequest(void); /* Send a RARP request */
/**********************************************************************/
-- 1.6.0.2

Signed-off-by: Joe Hershberger joe.hershberger@ni.com Cc: Joe Hershberger joe.hershberger@gmail.com Cc: Simon Glass sjg@chromium.org Cc: Mike Frysinger vapier@gentoo.org --- Changes for v2: - Split into smaller patches
net/sntp.c | 31 +++++++++++++++++-------------- net/sntp.h | 2 +- 2 files changed, 18 insertions(+), 15 deletions(-)
diff --git a/net/sntp.c b/net/sntp.c index 82f2fe6..6dd0dbd 100644 --- a/net/sntp.c +++ b/net/sntp.c @@ -17,7 +17,7 @@ static int SntpOurPort;
static void -SntpSend (void) +SntpSend(void) { struct sntp_pkt_t pkt; int pktlen = SNTP_PACKET_LEN; @@ -25,24 +25,26 @@ SntpSend (void)
debug("%s\n", __func__);
- memset (&pkt, 0, sizeof(pkt)); + memset(&pkt, 0, sizeof(pkt));
pkt.li = NTP_LI_NOLEAP; pkt.vn = NTP_VERSION; pkt.mode = NTP_MODE_CLIENT;
- memcpy ((char *)NetTxPacket + NetEthHdrSize() + IP_HDR_SIZE, (char *)&pkt, pktlen); + memcpy((char *)NetTxPacket + NetEthHdrSize() + IP_HDR_SIZE, + (char *)&pkt, pktlen);
SntpOurPort = 10000 + (get_timer(0) % 4096); sport = NTP_SERVICE_PORT;
- NetSendUDPPacket (NetServerEther, NetNtpServerIP, sport, SntpOurPort, pktlen); + NetSendUDPPacket(NetServerEther, NetNtpServerIP, sport, SntpOurPort, + pktlen); }
static void -SntpTimeout (void) +SntpTimeout(void) { - puts ("Timeout\n"); + puts("Timeout\n"); NetState = NETLOOP_FAIL; return; } @@ -57,19 +59,20 @@ SntpHandler(uchar *pkt, unsigned dest, IPaddr_t sip, unsigned src,
debug("%s\n", __func__);
- if (dest != SntpOurPort) return; + if (dest != SntpOurPort) + return;
/* * As the RTC's used in U-Boot sepport second resolution only * we simply ignore the sub-second field. */ - memcpy (&seconds, &rpktp->transmit_timestamp, sizeof(ulong)); + memcpy(&seconds, &rpktp->transmit_timestamp, sizeof(ulong));
to_tm(ntohl(seconds) - 2208988800UL + NetTimeOffset, &tm); #if defined(CONFIG_CMD_DATE) - rtc_set (&tm); + rtc_set(&tm); #endif - printf ("Date: %4d-%02d-%02d Time: %2d:%02d:%02d\n", + printf("Date: %4d-%02d-%02d Time: %2d:%02d:%02d\n", tm.tm_year, tm.tm_mon, tm.tm_mday, tm.tm_hour, tm.tm_min, tm.tm_sec);
@@ -77,13 +80,13 @@ SntpHandler(uchar *pkt, unsigned dest, IPaddr_t sip, unsigned src, }
void -SntpStart (void) +SntpStart(void) { debug("%s\n", __func__);
- NetSetTimeout (SNTP_TIMEOUT, SntpTimeout); + NetSetTimeout(SNTP_TIMEOUT, SntpTimeout); NetSetHandler(SntpHandler); - memset (NetServerEther, 0, 6); + memset(NetServerEther, 0, 6);
- SntpSend (); + SntpSend(); } diff --git a/net/sntp.h b/net/sntp.h index 8a097bf..1d0046e 100644 --- a/net/sntp.h +++ b/net/sntp.h @@ -56,6 +56,6 @@ struct sntp_pkt_t { unsigned long long transmit_timestamp; };
-extern void SntpStart (void); /* Begin SNTP */ +extern void SntpStart(void); /* Begin SNTP */
#endif /* __SNTP_H__ */

On Tue, Mar 27, 2012 at 4:42 PM, Joe Hershberger joe.hershberger@ni.com wrote:
Signed-off-by: Joe Hershberger joe.hershberger@ni.com Cc: Joe Hershberger joe.hershberger@gmail.com Cc: Simon Glass sjg@chromium.org Cc: Mike Frysinger vapier@gentoo.org
Acked-by: Simon Glass sjg@chromium.org
but see suggestion below (not checkpatch issue though)
Changes for v2: - Split into smaller patches
net/sntp.c | 31 +++++++++++++++++-------------- net/sntp.h | 2 +- 2 files changed, 18 insertions(+), 15 deletions(-)
diff --git a/net/sntp.c b/net/sntp.c index 82f2fe6..6dd0dbd 100644 --- a/net/sntp.c +++ b/net/sntp.c
@@ -77,13 +80,13 @@ SntpHandler(uchar *pkt, unsigned dest, IPaddr_t sip, unsigned src, }
void -SntpStart (void) +SntpStart(void) { debug("%s\n", __func__);
- NetSetTimeout (SNTP_TIMEOUT, SntpTimeout);
- NetSetTimeout(SNTP_TIMEOUT, SntpTimeout);
NetSetHandler(SntpHandler);
- memset (NetServerEther, 0, 6);
- memset(NetServerEther, 0, 6);
sizeof(NetServerEther) might be better.
- SntpSend ();
- SntpSend();
} diff --git a/net/sntp.h b/net/sntp.h index 8a097bf..1d0046e 100644 --- a/net/sntp.h +++ b/net/sntp.h @@ -56,6 +56,6 @@ struct sntp_pkt_t { unsigned long long transmit_timestamp; };
-extern void SntpStart (void); /* Begin SNTP */ +extern void SntpStart(void); /* Begin SNTP */
#endif /* __SNTP_H__ */
1.6.0.2

Signed-off-by: Joe Hershberger joe.hershberger@ni.com Cc: Joe Hershberger joe.hershberger@gmail.com Cc: Simon Glass sjg@chromium.org Cc: Mike Frysinger vapier@gentoo.org --- Changes for v2: - Split into smaller patches
include/net.h | 4 ++++ net/tftp.c | 30 ++++++++++++++---------------- net/tftp.h | 2 +- 3 files changed, 19 insertions(+), 17 deletions(-)
diff --git a/include/net.h b/include/net.h index f87ef8c..e39d88b 100644 --- a/include/net.h +++ b/include/net.h @@ -395,6 +395,10 @@ extern IPaddr_t NetNtpServerIP; /* the ip address to NTP */ extern int NetTimeOffset; /* offset time from UTC */ #endif
+#if defined(CONFIG_MCAST_TFTP) +extern IPaddr_t Mcast_addr; +#endif + /* Initialize the network adapter */ extern int NetLoop(enum proto_t);
diff --git a/net/tftp.c b/net/tftp.c index e62f229..bc7fe05 100644 --- a/net/tftp.c +++ b/net/tftp.c @@ -11,6 +11,9 @@ #include <net.h> #include "tftp.h" #include "bootp.h" +#ifdef CONFIG_SYS_DIRECT_FLASH_TFTP +#include <flash.h> +#endif
/* Well known TFTP port # */ #define WELL_KNOWN_PORT 69 @@ -112,10 +115,6 @@ static char default_filename[DEFAULT_NAME_LEN];
static char tftp_filename[MAX_LEN];
-#ifdef CONFIG_SYS_DIRECT_FLASH_TFTP -extern flash_info_t flash_info[]; -#endif - /* 512 is poor choice for ethernet, MTU is typically 1500. * Minus eth.hdrs thats 1468. Can get 2x better throughput with * almost-MTU block sizes. At least try... fall back to 512 if need be. @@ -137,7 +136,6 @@ static unsigned *Bitmap; static int PrevBitmapHole, Mapsize = MTFTP_BITMAPSIZE; static uchar ProhibitMcast, MasterClient; static uchar Multicast; -extern IPaddr_t Mcast_addr; static int Mcast_port; static ulong TftpEndingBlock; /* can get 'last' block before done..*/
@@ -157,7 +155,7 @@ mcast_cleanup(void)
#endif /* CONFIG_MCAST_TFTP */
-static __inline__ void +static inline void store_block(unsigned block, uchar *src, unsigned len) { ulong offset = block * TftpBlkSize + TftpBlockWrapOffset; @@ -182,8 +180,7 @@ store_block(unsigned block, uchar *src, unsigned len) NetState = NETLOOP_FAIL; return; } - } - else + } else #endif /* CONFIG_SYS_DIRECT_FLASH_TFTP */ { (void)memcpy((void *)(load_addr + offset), src, len); @@ -357,12 +354,14 @@ TftpSend(void) 0, TftpBlkSizeOption, 0); #ifdef CONFIG_MCAST_TFTP /* Check all preconditions before even trying the option */ - if (!ProhibitMcast - && (Bitmap = malloc(Mapsize)) - && eth_get_dev()->mcast) { - free(Bitmap); - Bitmap = NULL; - pkt += sprintf((char *)pkt, "multicast%c%c", 0, 0); + if (!ProhibitMcast) { + Bitmap = malloc(Mapsize); + if (Bitmap && eth_get_dev()->mcast) { + free(Bitmap); + Bitmap = NULL; + pkt += sprintf((char *)pkt, "multicast%c%c", + 0, 0); + } } #endif /* CONFIG_MCAST_TFTP */ len = pkt - xp; @@ -630,8 +629,7 @@ TftpHandler(uchar *pkt, unsigned dest, IPaddr_t sip, unsigned src, mcast_cleanup(); NetState = NETLOOP_SUCCESS; } - } - else + } else #endif if (len < TftpBlkSize) tftp_complete(); diff --git a/net/tftp.h b/net/tftp.h index c51aa25..18e4c9c 100644 --- a/net/tftp.h +++ b/net/tftp.h @@ -19,7 +19,7 @@ void TftpStart(enum proto_t protocol); /* Begin TFTP get/put */
#ifdef CONFIG_CMD_TFTPSRV -extern void TftpStartServer(void); /* Wait for incoming TFTP put */ +extern void TftpStartServer(void); /* Wait for incoming TFTP put */ #endif
/**********************************************************************/

Hi Joe,
On Tue, Mar 27, 2012 at 4:42 PM, Joe Hershberger joe.hershberger@ni.com wrote:
Signed-off-by: Joe Hershberger joe.hershberger@ni.com Cc: Joe Hershberger joe.hershberger@gmail.com Cc: Simon Glass sjg@chromium.org Cc: Mike Frysinger vapier@gentoo.org
Acked-by: Simon Glass sjg@chromium.org
Changes for v2: - Split into smaller patches
include/net.h | 4 ++++ net/tftp.c | 30 ++++++++++++++---------------- net/tftp.h | 2 +- 3 files changed, 19 insertions(+), 17 deletions(-)
@@ -157,7 +155,7 @@ mcast_cleanup(void)
#endif /* CONFIG_MCAST_TFTP */
-static __inline__ void +static inline void store_block(unsigned block, uchar *src, unsigned len)
Q: Do we really need this inline?
Regards, Simon

Requires: --ignore CONSIDER_KSTRTO
Signed-off-by: Joe Hershberger joe.hershberger@ni.com Cc: Joe Hershberger joe.hershberger@gmail.com Cc: Simon Glass sjg@chromium.org Cc: Mike Frysinger vapier@gentoo.org --- Changes for v2: - Split into smaller patches
drivers/net/netconsole.c | 122 +++++++++++++++++++++++---------------------- 1 files changed, 62 insertions(+), 60 deletions(-)
diff --git a/drivers/net/netconsole.c b/drivers/net/netconsole.c index 56ba64f..7acd5b0 100644 --- a/drivers/net/netconsole.c +++ b/drivers/net/netconsole.c @@ -29,81 +29,82 @@ DECLARE_GLOBAL_DATA_PTR;
static char input_buffer[512]; -static int input_size = 0; /* char count in input buffer */ -static int input_offset = 0; /* offset to valid chars in input buffer */ -static int input_recursion = 0; -static int output_recursion = 0; +static int input_size; /* char count in input buffer */ +static int input_offset; /* offset to valid chars in input buffer */ +static int input_recursion; +static int output_recursion; static int net_timeout; -static uchar nc_ether[6]; /* server enet address */ -static IPaddr_t nc_ip; /* server ip */ -static short nc_port; /* source/target port */ -static const char *output_packet; /* used by first send udp */ -static int output_packet_len = 0; +static uchar nc_ether[6]; /* server enet address */ +static IPaddr_t nc_ip; /* server ip */ +static short nc_port; /* source/target port */ +static const char *output_packet; /* used by first send udp */ +static int output_packet_len;
static void nc_wait_arp_handler(uchar *pkt, unsigned dest, IPaddr_t sip, unsigned src, unsigned len) { - NetState = NETLOOP_SUCCESS; /* got arp reply - quit net loop */ + NetState = NETLOOP_SUCCESS; /* got arp reply - quit net loop */ }
static void nc_handler(uchar *pkt, unsigned dest, IPaddr_t sip, unsigned src, unsigned len) { if (input_size) - NetState = NETLOOP_SUCCESS; /* got input - quit net loop */ + NetState = NETLOOP_SUCCESS; /* got input - quit net loop */ }
-static void nc_timeout (void) +static void nc_timeout(void) { NetState = NETLOOP_SUCCESS; }
-void NcStart (void) +void NcStart(void) { - if (!output_packet_len || memcmp (nc_ether, NetEtherNullAddr, 6)) { + if (!output_packet_len || memcmp(nc_ether, NetEtherNullAddr, 6)) { /* going to check for input packet */ - NetSetHandler (nc_handler); - NetSetTimeout (net_timeout, nc_timeout); + NetSetHandler(nc_handler); + NetSetTimeout(net_timeout, nc_timeout); } else { /* send arp request */ uchar *pkt; - NetSetHandler (nc_wait_arp_handler); - pkt = (uchar *) NetTxPacket + NetEthHdrSize () + IP_HDR_SIZE; - memcpy (pkt, output_packet, output_packet_len); - NetSendUDPPacket (nc_ether, nc_ip, nc_port, nc_port, output_packet_len); + NetSetHandler(nc_wait_arp_handler); + pkt = (uchar *)NetTxPacket + NetEthHdrSize() + IP_HDR_SIZE; + memcpy(pkt, output_packet, output_packet_len); + NetSendUDPPacket(nc_ether, nc_ip, nc_port, nc_port, + output_packet_len); } }
-int nc_input_packet (uchar * pkt, unsigned dest, unsigned src, unsigned len) +int nc_input_packet(uchar *pkt, unsigned dest, unsigned src, unsigned len) { int end, chunk;
if (dest != nc_port || !len) - return 0; /* not for us */ + return 0; /* not for us */
- if (input_size == sizeof input_buffer) - return 1; /* no space */ - if (len > sizeof input_buffer - input_size) - len = sizeof input_buffer - input_size; + if (input_size == sizeof(input_buffer)) + return 1; /* no space */ + if (len > sizeof(input_buffer) - input_size) + len = sizeof(input_buffer) - input_size;
end = input_offset + input_size; - if (end > sizeof input_buffer) - end -= sizeof input_buffer; + if (end > sizeof(input_buffer)) + end -= sizeof(input_buffer);
chunk = len; - if (end + len > sizeof input_buffer) { - chunk = sizeof input_buffer - end; + if (end + len > sizeof(input_buffer)) { + chunk = sizeof(input_buffer) - end; memcpy(input_buffer, pkt + chunk, len - chunk); } - memcpy (input_buffer + end, pkt, chunk); + memcpy(input_buffer + end, pkt, chunk);
input_size += len;
return 1; }
-static void nc_send_packet (const char *buf, int len) +static void nc_send_packet(const char *buf, int len) { struct eth_device *eth; int inited = 0; @@ -111,33 +112,33 @@ static void nc_send_packet (const char *buf, int len) uchar *ether; IPaddr_t ip;
- if ((eth = eth_get_dev ()) == NULL) { + eth = eth_get_dev(); + if (eth == NULL) return; - }
- if (!memcmp (nc_ether, NetEtherNullAddr, 6)) { + if (!memcmp(nc_ether, NetEtherNullAddr, 6)) { if (eth->state == ETH_STATE_ACTIVE) return; /* inside net loop */ output_packet = buf; output_packet_len = len; - NetLoop (NETCONS); /* wait for arp reply and send packet */ + NetLoop(NETCONS); /* wait for arp reply and send packet */ output_packet_len = 0; return; }
if (eth->state != ETH_STATE_ACTIVE) { - if (eth_init (gd->bd) < 0) + if (eth_init(gd->bd) < 0) return; inited = 1; } - pkt = (uchar *) NetTxPacket + NetEthHdrSize () + IP_HDR_SIZE; - memcpy (pkt, buf, len); + pkt = (uchar *)NetTxPacket + NetEthHdrSize() + IP_HDR_SIZE; + memcpy(pkt, buf, len); ether = nc_ether; ip = nc_ip; - NetSendUDPPacket (ether, ip, nc_port, nc_port, len); + NetSendUDPPacket(ether, ip, nc_port, nc_port, len);
if (inited) - eth_halt (); + eth_halt(); }
static int nc_start(void) @@ -146,26 +147,27 @@ static int nc_start(void)
nc_port = 6666; /* default port */
- if (getenv ("ncip")) { + if (getenv("ncip")) { char *p;
- nc_ip = getenv_IPaddr ("ncip"); + nc_ip = getenv_IPaddr("ncip"); if (!nc_ip) return -1; /* ncip is 0.0.0.0 */ - if ((p = strchr (getenv ("ncip"), ':')) != NULL) - nc_port = simple_strtoul (p + 1, NULL, 10); + p = strchr(getenv("ncip"), ':'); + if (p != NULL) + nc_port = simple_strtoul(p + 1, NULL, 10); } else nc_ip = ~0; /* ncip is not set */
- our_ip = getenv_IPaddr ("ipaddr"); - netmask = getenv_IPaddr ("netmask"); + our_ip = getenv_IPaddr("ipaddr"); + netmask = getenv_IPaddr("netmask");
if (nc_ip == ~0 || /* 255.255.255.255 */ ((netmask & our_ip) == (netmask & nc_ip) && /* on the same net */ - (netmask | nc_ip) == ~0)) /* broadcast to our net */ - memset (nc_ether, 0xff, sizeof nc_ether); + (netmask | nc_ip) == ~0)) /* broadcast to our net */ + memset(nc_ether, 0xff, sizeof(nc_ether)); else - memset (nc_ether, 0, sizeof nc_ether); /* force arp request */ + memset(nc_ether, 0, sizeof(nc_ether)); /* force arp request */
return 0; } @@ -176,7 +178,7 @@ static void nc_putc(char c) return; output_recursion = 1;
- nc_send_packet (&c, 1); + nc_send_packet(&c, 1);
output_recursion = 0; } @@ -208,14 +210,14 @@ static int nc_getc(void)
net_timeout = 0; /* no timeout */ while (!input_size) - NetLoop (NETCONS); + NetLoop(NETCONS);
input_recursion = 0;
c = input_buffer[input_offset++];
- if (input_offset >= sizeof input_buffer) - input_offset -= sizeof input_buffer; + if (input_offset >= sizeof(input_buffer)) + input_offset -= sizeof(input_buffer); input_size--;
return c; @@ -231,28 +233,28 @@ static int nc_tstc(void) if (input_size) return 1;
- eth = eth_get_dev (); + eth = eth_get_dev(); if (eth && eth->state == ETH_STATE_ACTIVE) return 0; /* inside net loop */
input_recursion = 1;
net_timeout = 1; - NetLoop (NETCONS); /* kind of poll */ + NetLoop(NETCONS); /* kind of poll */
input_recursion = 0;
return input_size != 0; }
-int drv_nc_init (void) +int drv_nc_init(void) { struct stdio_dev dev; int rc;
- memset (&dev, 0, sizeof (dev)); + memset(&dev, 0, sizeof(dev));
- strcpy (dev.name, "nc"); + strcpy(dev.name, "nc"); dev.flags = DEV_FLAGS_OUTPUT | DEV_FLAGS_INPUT | DEV_FLAGS_SYSTEM; dev.start = nc_start; dev.putc = nc_putc; @@ -260,7 +262,7 @@ int drv_nc_init (void) dev.getc = nc_getc; dev.tstc = nc_tstc;
- rc = stdio_register (&dev); + rc = stdio_register(&dev);
return (rc == 0) ? 1 : rc; }

On Tue, Mar 27, 2012 at 4:42 PM, Joe Hershberger joe.hershberger@ni.com wrote:
Requires: --ignore CONSIDER_KSTRTO
Signed-off-by: Joe Hershberger joe.hershberger@ni.com Cc: Joe Hershberger joe.hershberger@gmail.com Cc: Simon Glass sjg@chromium.org Cc: Mike Frysinger vapier@gentoo.org
Acked-by: Simon Glass sjg@chromium.org

This patch-series reorganizes some of the net code to make it easier to navigate and easier to leverage common functionality while encapsulating private implementation details. It also makes simple readability clean-ups.
This is dependant on the "Network stack checkpatch.pl compliance" patch-series.
This is a portion of the changes split from the series: "Add link-local addressing support"
Joe Hershberger (21): net: Remove CMD_PING where there's no CMD_NET net: Remove redefinitions of net.h functions net: Move MAC-seeded rand out of bootp.c net: Move CDP out of net.c net: Encapsulate CDP packet identification net: Move ARP out of net.c net: Move PING out of net.c net: Move RARP receive logic out of net.c net: cosmetic: Un-typedef IP_t net: cosmetic: Un-typedef Ethernet_t net: cosmetic: Un-typedef VLAN_Ethernet_t net: cosmetic: Un-typedef ARP_t net: cosmetic: Un-typedef ICMP_t net: cosmetic: Rename parameter len to payload_len net: cosmetic: Add a more explicit comment about 802.2 net: cosmetic: Rename "x" to "eth_proto" net: cosmetic: Rename CDPHandler to CDPReceive net: cosmetic: Rename OPT_SIZE to OPT_FIELD_SIZE net: cosmetic: Alphabetize includes in net.c net: cosmetic: Rename tmp to reply_ip_addr in arp.c net: cosmetic: Replace magic numbers in arp.c with constants
arch/powerpc/cpu/mpc8260/ether_fcc.c | 17 +- arch/powerpc/cpu/mpc8xx/fec.c | 2 +- board/Marvell/db64360/mv_eth.c | 2 - board/Marvell/db64460/mv_eth.c | 2 - board/esd/cpci750/mv_eth.c | 2 - board/prodrive/p3mx/mv_eth.c | 2 - drivers/net/netconsole.c | 4 +- include/configs/am3517_crane.h | 2 +- include/configs/am3517_evm.h | 2 +- include/configs/origen.h | 2 +- include/net.h | 93 +++-- net/Makefile | 4 + net/arp.c | 213 ++++++++ net/arp.h | 30 ++ net/bootp.c | 87 +--- net/bootp.h | 10 +- net/cdp.c | 374 ++++++++++++++ net/cdp.h | 20 + net/dns.c | 2 +- net/net.c | 881 ++++------------------------------ net/net_rand.c | 68 +++ net/net_rand.h | 17 + net/nfs.c | 2 +- net/ping.c | 140 ++++++ net/ping.h | 23 + net/rarp.c | 49 ++- net/rarp.h | 6 +- net/sntp.c | 2 +- net/tftp.c | 2 +- 29 files changed, 1128 insertions(+), 932 deletions(-) create mode 100644 net/arp.c create mode 100644 net/arp.h create mode 100644 net/cdp.c create mode 100644 net/cdp.h create mode 100644 net/net_rand.c create mode 100644 net/net_rand.h create mode 100644 net/ping.c create mode 100644 net/ping.h

This patch-series reorganizes some of the net code to make it easier to navigate and easier to leverage common functionality while encapsulating private implementation details. It also makes simple readability clean-ups.
This is dependant on the "Network stack checkpatch.pl compliance" patch-series.
This is a portion of the changes split from the series: "Add link-local addressing support"
Joe Hershberger (21): net: Remove CMD_PING where there's no CMD_NET net: Move MAC-seeded rand out of bootp.c net: Move CDP out of net.c net: Encapsulate CDP packet identification net: Move ARP out of net.c net: Move PING out of net.c net: Move RARP receive logic out of net.c net: cosmetic: Un-typedef IP_t net: cosmetic: Split struct ip_udp_hdr into ip_hdr net: cosmetic: Un-typedef Ethernet_t net: cosmetic: Un-typedef VLAN_Ethernet_t net: cosmetic: Un-typedef ARP_t net: cosmetic: Un-typedef ICMP_t net: cosmetic: Rename parameter len to payload_len net: cosmetic: Add a more explicit comment about 802.2 net: cosmetic: Rename "x" to "eth_proto" net: cosmetic: Rename CDPHandler to cdp_receive net: cosmetic: Rename OPT_SIZE to OPT_FIELD_SIZE net: cosmetic: Alphabetize includes in net.c net: cosmetic: Rename tmp to reply_ip_addr in arp.c net: cosmetic: Replace magic numbers in arp.c with constants
arch/powerpc/cpu/mpc8260/ether_fcc.c | 17 +- arch/powerpc/cpu/mpc8xx/fec.c | 2 +- drivers/net/netconsole.c | 4 +- include/configs/am3517_crane.h | 2 +- include/configs/am3517_evm.h | 2 +- include/configs/origen.h | 2 +- include/net.h | 110 +++-- net/Makefile | 4 + net/arp.c | 213 ++++++++ net/arp.h | 30 ++ net/bootp.c | 83 +--- net/bootp.h | 10 +- net/cdp.c | 374 ++++++++++++++ net/cdp.h | 21 + net/dns.c | 2 +- net/net.c | 884 ++++------------------------------ net/net_rand.c | 68 +++ net/net_rand.h | 26 + net/nfs.c | 2 +- net/ping.c | 140 ++++++ net/ping.h | 34 ++ net/rarp.c | 55 ++- net/rarp.h | 7 +- net/sntp.c | 2 +- net/tftp.c | 2 +- 25 files changed, 1166 insertions(+), 930 deletions(-) create mode 100644 net/arp.c create mode 100644 net/arp.h create mode 100644 net/cdp.c create mode 100644 net/cdp.h create mode 100644 net/net_rand.c create mode 100644 net/net_rand.h create mode 100644 net/ping.c create mode 100644 net/ping.h

Some boards claim to need ping, but don't support net. That makes no sense, and causes problems now that ping is in a separate file.
Signed-off-by: Joe Hershberger joe.hershberger@ni.com Cc: Joe Hershberger joe.hershberger@gmail.com --- Changes for v2: - Separated from "Move PING out of net.c"
include/configs/am3517_crane.h | 2 +- include/configs/am3517_evm.h | 2 +- include/configs/origen.h | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-)
diff --git a/include/configs/am3517_crane.h b/include/configs/am3517_crane.h index 1233985..827a615 100644 --- a/include/configs/am3517_crane.h +++ b/include/configs/am3517_crane.h @@ -146,7 +146,7 @@ #define CONFIG_CMD_MMC /* MMC support */ #define CONFIG_CMD_NAND /* NAND support */ #define CONFIG_CMD_DHCP -#define CONFIG_CMD_PING +#undef CONFIG_CMD_PING
#undef CONFIG_CMD_FLASH /* flinfo, erase, protect */ #undef CONFIG_CMD_FPGA /* FPGA configuration Support */ diff --git a/include/configs/am3517_evm.h b/include/configs/am3517_evm.h index ff8d1b0..6766ee2 100644 --- a/include/configs/am3517_evm.h +++ b/include/configs/am3517_evm.h @@ -145,7 +145,7 @@ #define CONFIG_CMD_MMC /* MMC support */ #define CONFIG_CMD_NAND /* NAND support */ #define CONFIG_CMD_DHCP -#define CONFIG_CMD_PING +#undef CONFIG_CMD_PING
#undef CONFIG_CMD_FLASH /* flinfo, erase, protect */ #undef CONFIG_CMD_FPGA /* FPGA configuration Support */ diff --git a/include/configs/origen.h b/include/configs/origen.h index 367f991..d4d0f84 100644 --- a/include/configs/origen.h +++ b/include/configs/origen.h @@ -83,7 +83,7 @@ /* Command definition*/ #include <config_cmd_default.h>
-#define CONFIG_CMD_PING +#undef CONFIG_CMD_PING #define CONFIG_CMD_ELF #define CONFIG_CMD_DHCP #define CONFIG_CMD_MMC

Make the MAC-seeded random number generator available to /net in general. MAC-seeded rand will be needed by link-local as well, so give it an interface.
Signed-off-by: Joe Hershberger joe.hershberger@ni.com Cc: Joe Hershberger joe.hershberger@gmail.com --- Changes for v3: - Lowercase hex - Add blank line - Always include header, even when it isn't needed - Add function comments
net/Makefile | 1 + net/bootp.c | 67 ++++++++++--------------------------------------------- net/bootp.h | 3 -- net/net_rand.c | 68 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ net/net_rand.h | 26 +++++++++++++++++++++ 5 files changed, 107 insertions(+), 58 deletions(-) create mode 100644 net/net_rand.c create mode 100644 net/net_rand.h
diff --git a/net/Makefile b/net/Makefile index 0544f6b..5901046 100644 --- a/net/Makefile +++ b/net/Makefile @@ -31,6 +31,7 @@ COBJS-$(CONFIG_CMD_NET) += bootp.o COBJS-$(CONFIG_CMD_DNS) += dns.o COBJS-$(CONFIG_CMD_NET) += eth.o COBJS-$(CONFIG_CMD_NET) += net.o +COBJS-$(CONFIG_BOOTP_RANDOM_DELAY) += net_rand.o COBJS-$(CONFIG_CMD_NFS) += nfs.o COBJS-$(CONFIG_CMD_RARP) += rarp.o COBJS-$(CONFIG_CMD_SNTP) += sntp.o diff --git a/net/bootp.c b/net/bootp.c index d0a7da2..0185e56 100644 --- a/net/bootp.c +++ b/net/bootp.c @@ -12,6 +12,7 @@ #include <command.h> #include <net.h> #include "bootp.h" +#include "net_rand.h" #include "tftp.h" #include "nfs.h" #ifdef CONFIG_STATUS_LED @@ -37,9 +38,6 @@
ulong BootpID; int BootpTry; -#ifdef CONFIG_BOOTP_RANDOM_DELAY -ulong seed1, seed2; -#endif
#if defined(CONFIG_CMD_DHCP) dhcp_state_t dhcp_state = INIT; @@ -584,6 +582,9 @@ BootpRequest(void) uchar *pkt, *iphdr; struct Bootp_t *bp; int ext_len, pktlen, iplen; +#ifdef CONFIG_BOOTP_RANDOM_DELAY + ulong i, rand_ms; +#endif
bootstage_mark_name(BOOTSTAGE_ID_BOOTP_START, "bootp_start"); #if defined(CONFIG_CMD_DHCP) @@ -591,60 +592,16 @@ BootpRequest(void) #endif
#ifdef CONFIG_BOOTP_RANDOM_DELAY /* Random BOOTP delay */ - unsigned char bi_enetaddr[6]; - int reg; - ulong tst1, tst2, sum, m_mask, m_value = 0; - - if (BootpTry == 0) { - /* get our mac */ - eth_getenv_enetaddr("ethaddr", bi_enetaddr); - - debug("BootpRequest => Our Mac: "); - for (reg = 0; reg < 6; reg++) - debug("%x%c", bi_enetaddr[reg], reg == 5 ? '\n' : ':'); - - /* Mac-Manipulation 2 get seed1 */ - tst1 = 0; - tst2 = 0; - for (reg = 2; reg < 6; reg++) { - tst1 = tst1 << 8; - tst1 = tst1 | bi_enetaddr[reg]; - } - for (reg = 0; reg < 2; reg++) { - tst2 = tst2 | bi_enetaddr[reg]; - tst2 = tst2 << 8; - } + if (BootpTry == 0) + srand_mac();
- seed1 = tst1^tst2; - - /* Mirror seed1*/ - m_mask = 0x1; - for (reg = 1; reg <= 32; reg++) { - m_value |= (m_mask & seed1); - seed1 = seed1 >> 1; - m_value = m_value << 1; - } - seed1 = m_value; - seed2 = 0xB78D0945; - } - - /* Random Number Generator */ - for (reg = 0; reg <= 0; reg++) { - sum = seed1 + seed2; - if (sum < seed1 || sum < seed2) - sum++; - seed2 = seed1; - seed1 = sum; - - if (BootpTry <= 2) { /* Start with max 1024 * 1ms */ - sum = sum >> (22-BootpTry); - } else { /*After 3rd BOOTP request max 8192 * 1ms */ - sum = sum >> 19; - } - } + if (BootpTry <= 2) /* Start with max 1024 * 1ms */ + rand_ms = rand() >> (22 - BootpTry); + else /* After 3rd BOOTP request max 8192 * 1ms */ + rand_ms = rand() >> 19;
- printf("Random delay: %ld ms...\n", sum); - for (reg = 0; reg < sum; reg++) + printf("Random delay: %ld ms...\n", rand_ms); + for (i = 0; i < rand_ms; i++) udelay(1000); /*Wait 1ms*/
#endif /* CONFIG_BOOTP_RANDOM_DELAY */ diff --git a/net/bootp.h b/net/bootp.h index ce73734..bf4e875 100644 --- a/net/bootp.h +++ b/net/bootp.h @@ -63,9 +63,6 @@ struct Bootp_t { extern ulong BootpID; /* ID of cur BOOTP request */ extern char BootFile[128]; /* Boot file name */ extern int BootpTry; -#ifdef CONFIG_BOOTP_RANDOM_DELAY -extern ulong seed1, seed2; /* seed for random BOOTP delay */ -#endif
/* Send a BOOTP request */ diff --git a/net/net_rand.c b/net/net_rand.c new file mode 100644 index 0000000..5387aba --- /dev/null +++ b/net/net_rand.c @@ -0,0 +1,68 @@ +/* + * Based on LiMon - BOOTP. + * + * Copyright 1994, 1995, 2000 Neil Russell. + * (See License) + * Copyright 2000 Roland Borde + * Copyright 2000 Paolo Scaffardi + * Copyright 2000-2004 Wolfgang Denk, wd@denx.de + */ + +#include <common.h> +#include <net.h> +#include "net_rand.h" + +static ulong seed1, seed2; + +void srand_mac(void) +{ + ulong tst1, tst2, m_mask; + ulong m_value = 0; + int reg; + unsigned char bi_enetaddr[6]; + + /* get our mac */ + eth_getenv_enetaddr("ethaddr", bi_enetaddr); + + debug("BootpRequest => Our Mac: "); + for (reg = 0; reg < 6; reg++) + debug("%x%c", bi_enetaddr[reg], reg == 5 ? '\n' : ':'); + + /* Mac-Manipulation 2 get seed1 */ + tst1 = 0; + tst2 = 0; + for (reg = 2; reg < 6; reg++) { + tst1 = tst1 << 8; + tst1 = tst1 | bi_enetaddr[reg]; + } + for (reg = 0; reg < 2; reg++) { + tst2 = tst2 | bi_enetaddr[reg]; + tst2 = tst2 << 8; + } + + seed1 = tst1^tst2; + + /* Mirror seed1*/ + m_mask = 0x1; + for (reg = 1; reg <= 32; reg++) { + m_value |= (m_mask & seed1); + seed1 = seed1 >> 1; + m_value = m_value << 1; + } + seed1 = m_value; + seed2 = 0xb78d0945; +} + +unsigned long rand(void) +{ + ulong sum; + + /* Random Number Generator */ + sum = seed1 + seed2; + if (sum < seed1 || sum < seed2) + sum++; + seed2 = seed1; + seed1 = sum; + + return sum; +} diff --git a/net/net_rand.h b/net/net_rand.h new file mode 100644 index 0000000..c98db64 --- /dev/null +++ b/net/net_rand.h @@ -0,0 +1,26 @@ +/* + * Copied from LiMon - BOOTP. + * + * Copyright 1994, 1995, 2000 Neil Russell. + * (See License) + * Copyright 2000 Paolo Scaffardi + */ + +#ifndef __NET_RAND_H__ +#define __NET_RAND_H__ + +#define RAND_MAX 0xffffffff + +/* + * Seed the random number generator using the eth0 MAC address + */ +void srand_mac(void); + +/* + * Get a random number (after seeding with MAC address) + * + * @return random number + */ +unsigned long rand(void); + +#endif /* __NET_RAND_H__ */

Am Mittwoch 23 Mai 2012, 19:57:58 schrieb Joe Hershberger:
t_ran

[sorry for my first mail.. wasn't intented to be send]
Sorry for being too late on this.
Am Mittwoch 23 Mai 2012, 19:57:58 schrieb Joe Hershberger:
Make the MAC-seeded random number generator available to /net in general. MAC-seeded rand will be needed by link-local as well, so give it an interface.
Signed-off-by: Joe Hershberger joe.hershberger@ni.com Cc: Joe Hershberger joe.hershberger@gmail.com
Changes for v3:
- Lowercase hex
- Add blank line
- Always include header, even when it isn't needed
- Add function comments
net/Makefile | 1 + net/bootp.c | 67 ++++++++++--------------------------------------------- net/bootp.h | 3 -- net/net_rand.c | 68 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ net/net_rand.h | 26 +++++++++++++++++++++ 5 files changed, 107 insertions(+), 58 deletions(-) create mode 100644 net/net_rand.c create mode 100644 net/net_rand.h
[..]
diff --git a/net/bootp.h b/net/bootp.h index ce73734..bf4e875 100644 --- a/net/bootp.h +++ b/net/bootp.h @@ -63,9 +63,6 @@ struct Bootp_t { extern ulong BootpID; /* ID of cur BOOTP request */ extern char BootFile[128]; /* Boot file name */ extern int BootpTry; -#ifdef CONFIG_BOOTP_RANDOM_DELAY -extern ulong seed1, seed2; /* seed for random BOOTP delay */ -#endif
/* Send a BOOTP request */ diff --git a/net/net_rand.c b/net/net_rand.c new file mode 100644 index 0000000..5387aba --- /dev/null +++ b/net/net_rand.c @@ -0,0 +1,68 @@ +/*
- Based on LiMon - BOOTP.
- Copyright 1994, 1995, 2000 Neil Russell.
- (See License)
- Copyright 2000 Roland Borde
- Copyright 2000 Paolo Scaffardi
- Copyright 2000-2004 Wolfgang Denk, wd@denx.de
- */
+#include <common.h> +#include <net.h> +#include "net_rand.h"
+static ulong seed1, seed2;
+void srand_mac(void) +{
- ulong tst1, tst2, m_mask;
- ulong m_value = 0;
- int reg;
- unsigned char bi_enetaddr[6];
- /* get our mac */
- eth_getenv_enetaddr("ethaddr", bi_enetaddr);
- debug("BootpRequest => Our Mac: ");
mh? this shouldn't be here anymore should it?
- for (reg = 0; reg < 6; reg++)
debug("%x%c", bi_enetaddr[reg], reg == 5 ? '\n' : ':');
- /* Mac-Manipulation 2 get seed1 */
- tst1 = 0;
- tst2 = 0;
- for (reg = 2; reg < 6; reg++) {
tst1 = tst1 << 8;
tst1 = tst1 | bi_enetaddr[reg];
- }
- for (reg = 0; reg < 2; reg++) {
tst2 = tst2 | bi_enetaddr[reg];
tst2 = tst2 << 8;
- }
- seed1 = tst1^tst2;
- /* Mirror seed1*/
- m_mask = 0x1;
- for (reg = 1; reg <= 32; reg++) {
m_value |= (m_mask & seed1);
seed1 = seed1 >> 1;
m_value = m_value << 1;
- }
- seed1 = m_value;
- seed2 = 0xb78d0945;
+}
+unsigned long rand(void) +{
- ulong sum;
- /* Random Number Generator */
- sum = seed1 + seed2;
- if (sum < seed1 || sum < seed2)
sum++;
- seed2 = seed1;
- seed1 = sum;
- return sum;
+}
is this really random? this conflicts with my patch (lib/rand.c) i'm trying to put into upstream.
diff --git a/net/net_rand.h b/net/net_rand.h new file mode 100644 index 0000000..c98db64 --- /dev/null +++ b/net/net_rand.h @@ -0,0 +1,26 @@ +/*
- Copied from LiMon - BOOTP.
- Copyright 1994, 1995, 2000 Neil Russell.
- (See License)
- Copyright 2000 Paolo Scaffardi
- */
+#ifndef __NET_RAND_H__ +#define __NET_RAND_H__
+#define RAND_MAX 0xffffffff
+/*
- Seed the random number generator using the eth0 MAC address
- */
+void srand_mac(void);
+/*
- Get a random number (after seeding with MAC address)
- @return random number
- */
+unsigned long rand(void);
+#endif /* __NET_RAND_H__ */
if this is unconditionally included, my patch fails because there are multiple declarations of rand().
Can we unify this with my patch, which is based on a paper about PRNGs? Eg. use the rand()/srand() from my patch and create an srand_mac() inline function which uses srand() from lib/rand.c ?
Joe, if that is ok, i'll put that into the next version of my "lsxl: add support for lschlv2 and lsxhl" patch series.

Hi Michael,
On Mon, May 28, 2012 at 5:03 PM, Michael Walle michael@walle.cc wrote:
[sorry for my first mail.. wasn't intented to be send]
Sorry for being too late on this.
Am Mittwoch 23 Mai 2012, 19:57:58 schrieb Joe Hershberger:
Make the MAC-seeded random number generator available to /net in general. MAC-seeded rand will be needed by link-local as well, so give it an interface.
Signed-off-by: Joe Hershberger joe.hershberger@ni.com Cc: Joe Hershberger joe.hershberger@gmail.com
Changes for v3: - Lowercase hex - Add blank line - Always include header, even when it isn't needed - Add function comments
net/Makefile | 1 + net/bootp.c | 67 ++++++++++--------------------------------------------- net/bootp.h | 3 -- net/net_rand.c | 68 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ net/net_rand.h | 26 +++++++++++++++++++++ 5 files changed, 107 insertions(+), 58 deletions(-) create mode 100644 net/net_rand.c create mode 100644 net/net_rand.h
[..]
diff --git a/net/net_rand.c b/net/net_rand.c new file mode 100644 index 0000000..5387aba --- /dev/null +++ b/net/net_rand.c @@ -0,0 +1,68 @@ +/*
- Based on LiMon - BOOTP.
- Copyright 1994, 1995, 2000 Neil Russell.
- (See License)
- Copyright 2000 Roland Borde
- Copyright 2000 Paolo Scaffardi
- Copyright 2000-2004 Wolfgang Denk, wd@denx.de
- */
+#include <common.h> +#include <net.h> +#include "net_rand.h"
+static ulong seed1, seed2;
+void srand_mac(void) +{
- ulong tst1, tst2, m_mask;
- ulong m_value = 0;
- int reg;
- unsigned char bi_enetaddr[6];
- /* get our mac */
- eth_getenv_enetaddr("ethaddr", bi_enetaddr);
- debug("BootpRequest => Our Mac: ");
mh? this shouldn't be here anymore should it?
Yes... It's not very appropriate any longer.
- for (reg = 0; reg < 6; reg++)
- debug("%x%c", bi_enetaddr[reg], reg == 5 ? '\n' : ':');
- /* Mac-Manipulation 2 get seed1 */
- tst1 = 0;
- tst2 = 0;
- for (reg = 2; reg < 6; reg++) {
- tst1 = tst1 << 8;
- tst1 = tst1 | bi_enetaddr[reg];
- }
- for (reg = 0; reg < 2; reg++) {
- tst2 = tst2 | bi_enetaddr[reg];
- tst2 = tst2 << 8;
- }
- seed1 = tst1^tst2;
- /* Mirror seed1*/
- m_mask = 0x1;
- for (reg = 1; reg <= 32; reg++) {
- m_value |= (m_mask & seed1);
- seed1 = seed1 >> 1;
- m_value = m_value << 1;
- }
- seed1 = m_value;
- seed2 = 0xb78d0945;
+}
+unsigned long rand(void) +{
- ulong sum;
- /* Random Number Generator */
- sum = seed1 + seed2;
- if (sum < seed1 || sum < seed2)
- sum++;
- seed2 = seed1;
- seed1 = sum;
- return sum;
+}
is this really random?
It was the implementation used for bootp delays. It is some form of PRNG... just not the same as what you've implemented.
this conflicts with my patch (lib/rand.c) i'm trying to put into upstream.
True.
diff --git a/net/net_rand.h b/net/net_rand.h new file mode 100644 index 0000000..c98db64 --- /dev/null +++ b/net/net_rand.h @@ -0,0 +1,26 @@ +/*
- Copied from LiMon - BOOTP.
- Copyright 1994, 1995, 2000 Neil Russell.
- (See License)
- Copyright 2000 Paolo Scaffardi
- */
+#ifndef __NET_RAND_H__ +#define __NET_RAND_H__
+#define RAND_MAX 0xffffffff
+/*
- Seed the random number generator using the eth0 MAC address
- */
+void srand_mac(void);
+/*
- Get a random number (after seeding with MAC address)
- @return random number
- */
+unsigned long rand(void);
+#endif /* __NET_RAND_H__ */
if this is unconditionally included, my patch fails because there are multiple declarations of rand().
Can we unify this with my patch, which is based on a paper about PRNGs? Eg. use the rand()/srand() from my patch and create an srand_mac() inline function which uses srand() from lib/rand.c ?
If you can verify that the functionality of the CONFIG_BOOTP_RANDOM_DELAY and CONFIG_CMD_LINK_LOCAL are uneffected, then I'm OK with it.
One thing to note is that the link-local implementation needs to use a MAC seeded random number. That means we can't have other things coming in and seeding it with a time. It is nice that it is separate for that reason. Can you solve that and integrate it with your PRNG?
Joe, if that is ok, i'll put that into the next version of my "lsxl: add support for lschlv2 and lsxhl" patch series.
I'm certainly willing to look at what you come up with.
Thanks, -Joe

Hi Joe,
Am Dienstag 29 Mai 2012, 20:08:26 schrieb Joe Hershberger:
Hi Michael,
On Mon, May 28, 2012 at 5:03 PM, Michael Walle michael@walle.cc wrote:
[sorry for my first mail.. wasn't intented to be send]
Sorry for being too late on this.
Am Mittwoch 23 Mai 2012, 19:57:58 schrieb Joe Hershberger:
Make the MAC-seeded random number generator available to /net in general. MAC-seeded rand will be needed by link-local as well, so give it an interface.
Signed-off-by: Joe Hershberger joe.hershberger@ni.com Cc: Joe Hershberger joe.hershberger@gmail.com
Changes for v3:
- Lowercase hex
- Add blank line
- Always include header, even when it isn't needed
- Add function comments
net/Makefile | 1 + net/bootp.c | 67 ++++++++++--------------------------------------------- net/bootp.h | 3 -- net/net_rand.c | 68 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ net/net_rand.h | 26 +++++++++++++++++++++ 5 files changed, 107 insertions(+), 58 deletions(-) create mode 100644 net/net_rand.c create mode 100644 net/net_rand.h
[..]
diff --git a/net/net_rand.c b/net/net_rand.c new file mode 100644 index 0000000..5387aba --- /dev/null +++ b/net/net_rand.c @@ -0,0 +1,68 @@ +/*
- Based on LiMon - BOOTP.
- Copyright 1994, 1995, 2000 Neil Russell.
- (See License)
- Copyright 2000 Roland Borde
- Copyright 2000 Paolo Scaffardi
- Copyright 2000-2004 Wolfgang Denk, wd@denx.de
- */
+#include <common.h> +#include <net.h> +#include "net_rand.h"
+static ulong seed1, seed2;
+void srand_mac(void) +{
ulong tst1, tst2, m_mask;
ulong m_value = 0;
int reg;
unsigned char bi_enetaddr[6];
/* get our mac */
eth_getenv_enetaddr("ethaddr", bi_enetaddr);
debug("BootpRequest => Our Mac: ");
mh? this shouldn't be here anymore should it?
Yes... It's not very appropriate any longer.
for (reg = 0; reg < 6; reg++)
debug("%x%c", bi_enetaddr[reg], reg == 5 ? '\n' : ':');
/* Mac-Manipulation 2 get seed1 */
tst1 = 0;
tst2 = 0;
for (reg = 2; reg < 6; reg++) {
tst1 = tst1 << 8;
tst1 = tst1 | bi_enetaddr[reg];
}
for (reg = 0; reg < 2; reg++) {
tst2 = tst2 | bi_enetaddr[reg];
tst2 = tst2 << 8;
}
seed1 = tst1^tst2;
/* Mirror seed1*/
m_mask = 0x1;
for (reg = 1; reg <= 32; reg++) {
m_value |= (m_mask & seed1);
seed1 = seed1 >> 1;
m_value = m_value << 1;
}
seed1 = m_value;
seed2 = 0xb78d0945;
+}
+unsigned long rand(void) +{
ulong sum;
/* Random Number Generator */
sum = seed1 + seed2;
if (sum < seed1 || sum < seed2)
sum++;
seed2 = seed1;
seed1 = sum;
return sum;
+}
is this really random?
It was the implementation used for bootp delays. It is some form of PRNG... just not the same as what you've implemented.
this conflicts with my patch (lib/rand.c) i'm trying to put into upstream.
True.
diff --git a/net/net_rand.h b/net/net_rand.h new file mode 100644 index 0000000..c98db64 --- /dev/null +++ b/net/net_rand.h @@ -0,0 +1,26 @@ +/*
- Copied from LiMon - BOOTP.
- Copyright 1994, 1995, 2000 Neil Russell.
- (See License)
- Copyright 2000 Paolo Scaffardi
- */
+#ifndef __NET_RAND_H__ +#define __NET_RAND_H__
+#define RAND_MAX 0xffffffff
+/*
- Seed the random number generator using the eth0 MAC address
- */
+void srand_mac(void);
+/*
- Get a random number (after seeding with MAC address)
- @return random number
- */
+unsigned long rand(void);
+#endif /* __NET_RAND_H__ */
if this is unconditionally included, my patch fails because there are multiple declarations of rand().
Can we unify this with my patch, which is based on a paper about PRNGs? Eg. use the rand()/srand() from my patch and create an srand_mac() inline function which uses srand() from lib/rand.c ?
If you can verify that the functionality of the CONFIG_BOOTP_RANDOM_DELAY and CONFIG_CMD_LINK_LOCAL are uneffected, then I'm OK with it.
One thing to note is that the link-local implementation needs to use a MAC seeded random number. That means we can't have other things coming in and seeding it with a time. It is nice that it is separate for that reason. Can you solve that and integrate it with your PRNG?
I'm in a hurry, short answer for now: I thought of sth like this:
static inline void srand_mac(void) { unsigned char enetaddr[6]; unsigned int seed;
/* get our mac */ eth_getenv_enetaddr("ethaddr", enetaddr);
seed = enetaddr[5]; seed |= enetaddr[4] << 8; seed |= enetaddr[3] << 16; seed |= enetaddr[2] << 24;
srand(seed); }

Hi Michael,
On Tue, May 29, 2012 at 1:23 PM, Michael Walle michael@walle.cc wrote:
Hi Joe,
Am Dienstag 29 Mai 2012, 20:08:26 schrieb Joe Hershberger:
If you can verify that the functionality of the CONFIG_BOOTP_RANDOM_DELAY and CONFIG_CMD_LINK_LOCAL are uneffected, then I'm OK with it.
One thing to note is that the link-local implementation needs to use a MAC seeded random number. That means we can't have other things coming in and seeding it with a time. It is nice that it is separate for that reason. Can you solve that and integrate it with your PRNG?
I'm in a hurry, short answer for now: I thought of sth like this:
static inline void srand_mac(void) { unsigned char enetaddr[6]; unsigned int seed;
/* get our mac */ eth_getenv_enetaddr("ethaddr", enetaddr);
seed = enetaddr[5]; seed |= enetaddr[4] << 8; seed |= enetaddr[3] << 16; seed |= enetaddr[2] << 24;
srand(seed); }
I'm not sure why you are only seeding with the last 4 bytes of the MAC. The original algorithm used all 6 (in its way). You also haven't addressed the issue of isolating the link-local algorithm from other non-MAC-seeded random numbers. The most naive way around it would be to rename the rand() in net to rand_mac() or something like that and not attempt to combine them.
-Joe

Hi Joe,
Am Dienstag 29 Mai 2012, 23:08:23 schrieb Joe Hershberger:
Hi Michael,
On Tue, May 29, 2012 at 1:23 PM, Michael Walle michael@walle.cc wrote:
Hi Joe,
Am Dienstag 29 Mai 2012, 20:08:26 schrieb Joe Hershberger:
If you can verify that the functionality of the CONFIG_BOOTP_RANDOM_DELAY and CONFIG_CMD_LINK_LOCAL are uneffected, then I'm OK with it.
One thing to note is that the link-local implementation needs to use a MAC seeded random number. That means we can't have other things coming in and seeding it with a time. It is nice that it is separate for that reason. Can you solve that and integrate it with your PRNG?
I'm in a hurry, short answer for now: I thought of sth like this:
static inline void srand_mac(void) { unsigned char enetaddr[6]; unsigned int seed;
/* get our mac */ eth_getenv_enetaddr("ethaddr", enetaddr); seed = enetaddr[5]; seed |= enetaddr[4] << 8; seed |= enetaddr[3] << 16; seed |= enetaddr[2] << 24; srand(seed);
}
I'm not sure why you are only seeding with the last 4 bytes of the MAC. The original algorithm used all 6 (in its way).
yeah i'll fix that.
You also haven't addressed the issue of isolating the link-local algorithm from other non-MAC-seeded random numbers.
Ah now i get it. But which other code may run and seed the NG with the timer only, while we are in the link local netloop? Shouldn't it be safe to first call srand(mac+timer) and then rand() multiple times? No other code can be run unless NetLoop(LINKLOCAL) returns, right?
The most naive way around it would be to rename the rand() in net to rand_mac() or something like that and not attempt to combine them.
i think we should focus on combining not reinventing the wheel multiple times.

Hi Michael,
On Tue, May 29, 2012 at 5:06 PM, Michael Walle michael@walle.cc wrote:
Hi Joe,
Am Dienstag 29 Mai 2012, 23:08:23 schrieb Joe Hershberger:
Hi Michael,
On Tue, May 29, 2012 at 1:23 PM, Michael Walle michael@walle.cc wrote:
Hi Joe,
Am Dienstag 29 Mai 2012, 20:08:26 schrieb Joe Hershberger:
If you can verify that the functionality of the CONFIG_BOOTP_RANDOM_DELAY and CONFIG_CMD_LINK_LOCAL are uneffected, then I'm OK with it.
One thing to note is that the link-local implementation needs to use a MAC seeded random number. That means we can't have other things coming in and seeding it with a time. It is nice that it is separate for that reason. Can you solve that and integrate it with your PRNG?
I'm in a hurry, short answer for now: I thought of sth like this:
static inline void srand_mac(void) { unsigned char enetaddr[6]; unsigned int seed;
/* get our mac */ eth_getenv_enetaddr("ethaddr", enetaddr);
seed = enetaddr[5]; seed |= enetaddr[4] << 8; seed |= enetaddr[3] << 16; seed |= enetaddr[2] << 24;
srand(seed); }
I'm not sure why you are only seeding with the last 4 bytes of the MAC. The original algorithm used all 6 (in its way).
yeah i'll fix that.
You also haven't addressed the issue of isolating the link-local algorithm from other non-MAC-seeded random numbers.
Ah now i get it. But which other code may run and seed the NG with the timer only, while we are in the link local netloop? Shouldn't it be safe to first call srand(mac+timer) and then rand() multiple times? No other code can be run unless NetLoop(LINKLOCAL) returns, right?
It needs to run multiple times, yes. But it will also process any packets that are received when executing any other net command later. The state machine will keep running. There can obviously be other commands that run in between.
Perhaps we can separate the state for the PRNG between the 2 cases but share the code.
The most naive way around it would be to rename the rand() in net to rand_mac() or something like that and not attempt to combine them.
i think we should focus on combining not reinventing the wheel multiple times.
Agreed, but we have to solve the problem above.
Thanks, -Joe

Hi Joe,
Am Mittwoch 30 Mai 2012, 00:40:18 schrieb Joe Hershberger:
Ah now i get it. But which other code may run and seed the NG with the timer only, while we are in the link local netloop? Shouldn't it be safe to first call srand(mac+timer) and then rand() multiple times? No other code can be run unless NetLoop(LINKLOCAL) returns, right?
It needs to run multiple times, yes. But it will also process any packets that are received when executing any other net command later. The state machine will keep running. There can obviously be other commands that run in between.
i guess i have to study the netloop a bit more ;)
Perhaps we can separate the state for the PRNG between the 2 cases but share the code.
yeah i was thinking of rand_r().
The most naive way around it would be to rename the rand() in net to rand_mac() or something like that and not attempt to combine them.
i think we should focus on combining not reinventing the wheel multiple times.
Agreed, but we have to solve the problem above.
fully argreeing ;)

Separate this functionality out of the net.c behemoth
Signed-off-by: Joe Hershberger joe.hershberger@ni.com Cc: Joe Hershberger joe.hershberger@gmail.com --- Changes for v2: - Moved is_cdp_packet to a separate patch - Fixed blank newline at the end of cdp.h - Pushed #ifdef CONFIG_CMD_CDP into header Changes for v3: - Fix comment style - Clean up format of array initializer - Added a short commit message
include/net.h | 8 +- net/Makefile | 1 + net/cdp.c | 375 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++ net/cdp.h | 20 +++ net/net.c | 366 +------------------------------------------------------- 5 files changed, 400 insertions(+), 370 deletions(-) create mode 100644 net/cdp.c create mode 100644 net/cdp.h
diff --git a/include/net.h b/include/net.h index 92afc19..8a5b09f 100644 --- a/include/net.h +++ b/include/net.h @@ -356,9 +356,7 @@ extern uchar NetEtherNullAddr[6]; extern ushort NetOurVLAN; /* Our VLAN */ extern ushort NetOurNativeVLAN; /* Our Native VLAN */
-extern uchar NetCDPAddr[6]; /* Ethernet CDP address */ -extern ushort CDPNativeVLAN; /* CDP returned native VLAN */ -extern ushort CDPApplianceVLAN; /* CDP returned appliance VLAN */ +extern const uchar NetCDPAddr[6]; /* Ethernet CDP address */
extern int NetState; /* Network loop state */ #define NETLOOP_CONTINUE 1 @@ -387,8 +385,8 @@ extern IPaddr_t NetPingIP; /* the ip address to ping */
#if defined(CONFIG_CMD_CDP) /* when CDP completes these hold the return values */ -extern ushort CDPNativeVLAN; -extern ushort CDPApplianceVLAN; +extern ushort CDPNativeVLAN; /* CDP returned native VLAN */ +extern ushort CDPApplianceVLAN; /* CDP returned appliance VLAN */ #endif
#if defined(CONFIG_CMD_SNTP) diff --git a/net/Makefile b/net/Makefile index 5901046..b350bfc 100644 --- a/net/Makefile +++ b/net/Makefile @@ -28,6 +28,7 @@ include $(TOPDIR)/config.mk LIB = $(obj)libnet.o
COBJS-$(CONFIG_CMD_NET) += bootp.o +COBJS-$(CONFIG_CMD_CDP) += cdp.o COBJS-$(CONFIG_CMD_DNS) += dns.o COBJS-$(CONFIG_CMD_NET) += eth.o COBJS-$(CONFIG_CMD_NET) += net.o diff --git a/net/cdp.c b/net/cdp.c new file mode 100644 index 0000000..004aae2 --- /dev/null +++ b/net/cdp.c @@ -0,0 +1,375 @@ +/* + * Copied from Linux Monitor (LiMon) - Networking. + * + * Copyright 1994 - 2000 Neil Russell. + * (See License) + * Copyright 2000 Roland Borde + * Copyright 2000 Paolo Scaffardi + * Copyright 2000-2002 Wolfgang Denk, wd@denx.de + */ + +#include <common.h> +#include <net.h> +#if defined(CONFIG_CDP_VERSION) +#include <timestamp.h> +#endif + +#include "cdp.h" + +/* Ethernet bcast address */ +const uchar NetCDPAddr[6] = { 0x01, 0x00, 0x0c, 0xcc, 0xcc, 0xcc }; + +#define CDP_DEVICE_ID_TLV 0x0001 +#define CDP_ADDRESS_TLV 0x0002 +#define CDP_PORT_ID_TLV 0x0003 +#define CDP_CAPABILITIES_TLV 0x0004 +#define CDP_VERSION_TLV 0x0005 +#define CDP_PLATFORM_TLV 0x0006 +#define CDP_NATIVE_VLAN_TLV 0x000a +#define CDP_APPLIANCE_VLAN_TLV 0x000e +#define CDP_TRIGGER_TLV 0x000f +#define CDP_POWER_CONSUMPTION_TLV 0x0010 +#define CDP_SYSNAME_TLV 0x0014 +#define CDP_SYSOBJECT_TLV 0x0015 +#define CDP_MANAGEMENT_ADDRESS_TLV 0x0016 + +#define CDP_TIMEOUT 250UL /* one packet every 250ms */ + +static int CDPSeq; +static int CDPOK; + +ushort CDPNativeVLAN; +ushort CDPApplianceVLAN; + +static const uchar CDP_SNAP_hdr[8] = { + 0xAA, 0xAA, 0x03, 0x00, 0x00, 0x0C, 0x20, 0x00 }; + +static ushort +CDP_compute_csum(const uchar *buff, ushort len) +{ + ushort csum; + int odd; + ulong result = 0; + ushort leftover; + ushort *p; + + if (len > 0) { + odd = 1 & (ulong)buff; + if (odd) { + result = *buff << 8; + len--; + buff++; + } + while (len > 1) { + p = (ushort *)buff; + result += *p++; + buff = (uchar *)p; + if (result & 0x80000000) + result = (result & 0xFFFF) + (result >> 16); + len -= 2; + } + if (len) { + leftover = (signed short)(*(const signed char *)buff); + /* + * CISCO SUCKS big time! (and blows too): + * CDP uses the IP checksum algorithm with a twist; + * for the last byte it *sign* extends and sums. + */ + result = (result & 0xffff0000) | + ((result + leftover) & 0x0000ffff); + } + while (result >> 16) + result = (result & 0xFFFF) + (result >> 16); + + if (odd) + result = ((result >> 8) & 0xff) | + ((result & 0xff) << 8); + } + + /* add up 16-bit and 17-bit words for 17+c bits */ + result = (result & 0xffff) + (result >> 16); + /* add up 16-bit and 2-bit for 16+c bit */ + result = (result & 0xffff) + (result >> 16); + /* add up carry.. */ + result = (result & 0xffff) + (result >> 16); + + /* negate */ + csum = ~(ushort)result; + + /* run time endian detection */ + if (csum != htons(csum)) /* little endian */ + csum = htons(csum); + + return csum; +} + +static int +CDPSendTrigger(void) +{ + uchar *pkt; + ushort *s; + ushort *cp; + Ethernet_t *et; + int len; + ushort chksum; +#if defined(CONFIG_CDP_DEVICE_ID) || defined(CONFIG_CDP_PORT_ID) || \ + defined(CONFIG_CDP_VERSION) || defined(CONFIG_CDP_PLATFORM) + char buf[32]; +#endif + + pkt = NetTxPacket; + et = (Ethernet_t *)pkt; + + /* NOTE: trigger sent not on any VLAN */ + + /* form ethernet header */ + memcpy(et->et_dest, NetCDPAddr, 6); + memcpy(et->et_src, NetOurEther, 6); + + pkt += ETHER_HDR_SIZE; + + /* SNAP header */ + memcpy((uchar *)pkt, CDP_SNAP_hdr, sizeof(CDP_SNAP_hdr)); + pkt += sizeof(CDP_SNAP_hdr); + + /* CDP header */ + *pkt++ = 0x02; /* CDP version 2 */ + *pkt++ = 180; /* TTL */ + s = (ushort *)pkt; + cp = s; + /* checksum (0 for later calculation) */ + *s++ = htons(0); + + /* CDP fields */ +#ifdef CONFIG_CDP_DEVICE_ID + *s++ = htons(CDP_DEVICE_ID_TLV); + *s++ = htons(CONFIG_CDP_DEVICE_ID); + sprintf(buf, CONFIG_CDP_DEVICE_ID_PREFIX "%pm", NetOurEther); + memcpy((uchar *)s, buf, 16); + s += 16 / 2; +#endif + +#ifdef CONFIG_CDP_PORT_ID + *s++ = htons(CDP_PORT_ID_TLV); + memset(buf, 0, sizeof(buf)); + sprintf(buf, CONFIG_CDP_PORT_ID, eth_get_dev_index()); + len = strlen(buf); + if (len & 1) /* make it even */ + len++; + *s++ = htons(len + 4); + memcpy((uchar *)s, buf, len); + s += len / 2; +#endif + +#ifdef CONFIG_CDP_CAPABILITIES + *s++ = htons(CDP_CAPABILITIES_TLV); + *s++ = htons(8); + *(ulong *)s = htonl(CONFIG_CDP_CAPABILITIES); + s += 2; +#endif + +#ifdef CONFIG_CDP_VERSION + *s++ = htons(CDP_VERSION_TLV); + memset(buf, 0, sizeof(buf)); + strcpy(buf, CONFIG_CDP_VERSION); + len = strlen(buf); + if (len & 1) /* make it even */ + len++; + *s++ = htons(len + 4); + memcpy((uchar *)s, buf, len); + s += len / 2; +#endif + +#ifdef CONFIG_CDP_PLATFORM + *s++ = htons(CDP_PLATFORM_TLV); + memset(buf, 0, sizeof(buf)); + strcpy(buf, CONFIG_CDP_PLATFORM); + len = strlen(buf); + if (len & 1) /* make it even */ + len++; + *s++ = htons(len + 4); + memcpy((uchar *)s, buf, len); + s += len / 2; +#endif + +#ifdef CONFIG_CDP_TRIGGER + *s++ = htons(CDP_TRIGGER_TLV); + *s++ = htons(8); + *(ulong *)s = htonl(CONFIG_CDP_TRIGGER); + s += 2; +#endif + +#ifdef CONFIG_CDP_POWER_CONSUMPTION + *s++ = htons(CDP_POWER_CONSUMPTION_TLV); + *s++ = htons(6); + *s++ = htons(CONFIG_CDP_POWER_CONSUMPTION); +#endif + + /* length of ethernet packet */ + len = (uchar *)s - ((uchar *)NetTxPacket + ETHER_HDR_SIZE); + et->et_protlen = htons(len); + + len = ETHER_HDR_SIZE + sizeof(CDP_SNAP_hdr); + chksum = CDP_compute_csum((uchar *)NetTxPacket + len, + (uchar *)s - (NetTxPacket + len)); + if (chksum == 0) + chksum = 0xFFFF; + *cp = htons(chksum); + + (void) eth_send(NetTxPacket, (uchar *)s - NetTxPacket); + return 0; +} + +static void +CDPTimeout(void) +{ + CDPSeq++; + + if (CDPSeq < 3) { + NetSetTimeout(CDP_TIMEOUT, CDPTimeout); + CDPSendTrigger(); + return; + } + + /* if not OK try again */ + if (!CDPOK) + NetStartAgain(); + else + NetState = NETLOOP_SUCCESS; +} + +static void +CDPDummyHandler(uchar *pkt, unsigned dest, IPaddr_t sip, unsigned src, + unsigned len) +{ + /* nothing */ +} + +void +CDPHandler(const uchar *pkt, unsigned len) +{ + const uchar *t; + const ushort *ss; + ushort type, tlen; + ushort vlan, nvlan; + + /* minimum size? */ + if (len < sizeof(CDP_SNAP_hdr) + 4) + goto pkt_short; + + /* check for valid CDP SNAP header */ + if (memcmp(pkt, CDP_SNAP_hdr, sizeof(CDP_SNAP_hdr)) != 0) + return; + + pkt += sizeof(CDP_SNAP_hdr); + len -= sizeof(CDP_SNAP_hdr); + + /* Version of CDP protocol must be >= 2 and TTL != 0 */ + if (pkt[0] < 0x02 || pkt[1] == 0) + return; + + /* + * if version is greater than 0x02 maybe we'll have a problem; + * output a warning + */ + if (pkt[0] != 0x02) + printf("**WARNING: CDP packet received with a protocol version " + "%d > 2\n", pkt[0] & 0xff); + + if (CDP_compute_csum(pkt, len) != 0) + return; + + pkt += 4; + len -= 4; + + vlan = htons(-1); + nvlan = htons(-1); + while (len > 0) { + if (len < 4) + goto pkt_short; + + ss = (const ushort *)pkt; + type = ntohs(ss[0]); + tlen = ntohs(ss[1]); + if (tlen > len) + goto pkt_short; + + pkt += tlen; + len -= tlen; + + ss += 2; /* point ss to the data of the TLV */ + tlen -= 4; + + switch (type) { + case CDP_DEVICE_ID_TLV: + break; + case CDP_ADDRESS_TLV: + break; + case CDP_PORT_ID_TLV: + break; + case CDP_CAPABILITIES_TLV: + break; + case CDP_VERSION_TLV: + break; + case CDP_PLATFORM_TLV: + break; + case CDP_NATIVE_VLAN_TLV: + nvlan = *ss; + break; + case CDP_APPLIANCE_VLAN_TLV: + t = (const uchar *)ss; + while (tlen > 0) { + if (tlen < 3) + goto pkt_short; + + ss = (const ushort *)(t + 1); + +#ifdef CONFIG_CDP_APPLIANCE_VLAN_TYPE + if (t[0] == CONFIG_CDP_APPLIANCE_VLAN_TYPE) + vlan = *ss; +#else + /* XXX will this work; dunno */ + vlan = ntohs(*ss); +#endif + t += 3; tlen -= 3; + } + break; + case CDP_TRIGGER_TLV: + break; + case CDP_POWER_CONSUMPTION_TLV: + break; + case CDP_SYSNAME_TLV: + break; + case CDP_SYSOBJECT_TLV: + break; + case CDP_MANAGEMENT_ADDRESS_TLV: + break; + } + } + + CDPApplianceVLAN = vlan; + CDPNativeVLAN = nvlan; + + CDPOK = 1; + return; + + pkt_short: + printf("** CDP packet is too short\n"); + return; +} + +void +CDPStart(void) +{ + printf("Using %s device\n", eth_get_name()); + CDPSeq = 0; + CDPOK = 0; + + CDPNativeVLAN = htons(-1); + CDPApplianceVLAN = htons(-1); + + NetSetTimeout(CDP_TIMEOUT, CDPTimeout); + NetSetHandler(CDPDummyHandler); + + CDPSendTrigger(); +} diff --git a/net/cdp.h b/net/cdp.h new file mode 100644 index 0000000..93bc461 --- /dev/null +++ b/net/cdp.h @@ -0,0 +1,20 @@ +/* + * Copied from Linux Monitor (LiMon) - Networking. + * + * Copyright 1994 - 2000 Neil Russell. + * (See License) + * Copyright 2000 Roland Borde + * Copyright 2000 Paolo Scaffardi + * Copyright 2000-2002 Wolfgang Denk, wd@denx.de + */ + +#if defined(CONFIG_CMD_CDP) + +#ifndef __CDP_H__ +#define __CDP_H__ + +void CDPStart(void); +void CDPHandler(const uchar *pkt, unsigned len); + +#endif /* __CDP_H__ */ +#endif diff --git a/net/net.c b/net/net.c index e9536b3..4cd5a0a 100644 --- a/net/net.c +++ b/net/net.c @@ -92,9 +92,7 @@ #if defined(CONFIG_CMD_SNTP) #include "sntp.h" #endif -#if defined(CONFIG_CDP_VERSION) -#include <timestamp.h> -#endif +#include "cdp.h" #if defined(CONFIG_CMD_DNS) #include "dns.h" #endif @@ -164,10 +162,6 @@ uchar NetEtherNullAddr[6]; #ifdef CONFIG_API void (*push_packet)(void *, int len) = 0; #endif -#if defined(CONFIG_CMD_CDP) -/* Ethernet bcast address */ -uchar NetCDPAddr[6] = { 0x01, 0x00, 0x0c, 0xcc, 0xcc, 0xcc }; -#endif /* Network loop state */ int NetState; /* Tried all network devices */ @@ -193,10 +187,6 @@ IPaddr_t NetPingIP; static void PingStart(void); #endif
-#if defined(CONFIG_CMD_CDP) -static void CDPStart(void); -#endif - #if defined(CONFIG_CMD_SNTP) /* NTP server IP address */ IPaddr_t NetNtpServerIP; @@ -841,360 +831,6 @@ static void PingStart(void) } #endif
-#if defined(CONFIG_CMD_CDP) - -#define CDP_DEVICE_ID_TLV 0x0001 -#define CDP_ADDRESS_TLV 0x0002 -#define CDP_PORT_ID_TLV 0x0003 -#define CDP_CAPABILITIES_TLV 0x0004 -#define CDP_VERSION_TLV 0x0005 -#define CDP_PLATFORM_TLV 0x0006 -#define CDP_NATIVE_VLAN_TLV 0x000a -#define CDP_APPLIANCE_VLAN_TLV 0x000e -#define CDP_TRIGGER_TLV 0x000f -#define CDP_POWER_CONSUMPTION_TLV 0x0010 -#define CDP_SYSNAME_TLV 0x0014 -#define CDP_SYSOBJECT_TLV 0x0015 -#define CDP_MANAGEMENT_ADDRESS_TLV 0x0016 - -#define CDP_TIMEOUT 250UL /* one packet every 250ms */ - -static int CDPSeq; -static int CDPOK; - -ushort CDPNativeVLAN; -ushort CDPApplianceVLAN; - -static const uchar CDP_SNAP_hdr[8] = { 0xAA, 0xAA, 0x03, 0x00, 0x00, 0x0C, 0x20, - 0x00 }; - -static ushort CDP_compute_csum(const uchar *buff, ushort len) -{ - ushort csum; - int odd; - ulong result = 0; - ushort leftover; - ushort *p; - - if (len > 0) { - odd = 1 & (ulong)buff; - if (odd) { - result = *buff << 8; - len--; - buff++; - } - while (len > 1) { - p = (ushort *)buff; - result += *p++; - buff = (uchar *)p; - if (result & 0x80000000) - result = (result & 0xFFFF) + (result >> 16); - len -= 2; - } - if (len) { - leftover = (signed short)(*(const signed char *)buff); - /* CISCO SUCKS big time! (and blows too): - * CDP uses the IP checksum algorithm with a twist; - * for the last byte it *sign* extends and sums. - */ - result = (result & 0xffff0000) | - ((result + leftover) & 0x0000ffff); - } - while (result >> 16) - result = (result & 0xFFFF) + (result >> 16); - - if (odd) - result = ((result >> 8) & 0xff) | - ((result & 0xff) << 8); - } - - /* add up 16-bit and 17-bit words for 17+c bits */ - result = (result & 0xffff) + (result >> 16); - /* add up 16-bit and 2-bit for 16+c bit */ - result = (result & 0xffff) + (result >> 16); - /* add up carry.. */ - result = (result & 0xffff) + (result >> 16); - - /* negate */ - csum = ~(ushort)result; - - /* run time endian detection */ - if (csum != htons(csum)) /* little endian */ - csum = htons(csum); - - return csum; -} - -int CDPSendTrigger(void) -{ - uchar *pkt; - ushort *s; - ushort *cp; - Ethernet_t *et; - int len; - ushort chksum; -#if defined(CONFIG_CDP_DEVICE_ID) || defined(CONFIG_CDP_PORT_ID) || \ - defined(CONFIG_CDP_VERSION) || defined(CONFIG_CDP_PLATFORM) - char buf[32]; -#endif - - pkt = NetTxPacket; - et = (Ethernet_t *)pkt; - - /* NOTE: trigger sent not on any VLAN */ - - /* form ethernet header */ - memcpy(et->et_dest, NetCDPAddr, 6); - memcpy(et->et_src, NetOurEther, 6); - - pkt += ETHER_HDR_SIZE; - - /* SNAP header */ - memcpy((uchar *)pkt, CDP_SNAP_hdr, sizeof(CDP_SNAP_hdr)); - pkt += sizeof(CDP_SNAP_hdr); - - /* CDP header */ - *pkt++ = 0x02; /* CDP version 2 */ - *pkt++ = 180; /* TTL */ - s = (ushort *)pkt; - cp = s; - /* checksum (0 for later calculation) */ - *s++ = htons(0); - - /* CDP fields */ -#ifdef CONFIG_CDP_DEVICE_ID - *s++ = htons(CDP_DEVICE_ID_TLV); - *s++ = htons(CONFIG_CDP_DEVICE_ID); - sprintf(buf, CONFIG_CDP_DEVICE_ID_PREFIX "%pm", NetOurEther); - memcpy((uchar *)s, buf, 16); - s += 16 / 2; -#endif - -#ifdef CONFIG_CDP_PORT_ID - *s++ = htons(CDP_PORT_ID_TLV); - memset(buf, 0, sizeof(buf)); - sprintf(buf, CONFIG_CDP_PORT_ID, eth_get_dev_index()); - len = strlen(buf); - if (len & 1) /* make it even */ - len++; - *s++ = htons(len + 4); - memcpy((uchar *)s, buf, len); - s += len / 2; -#endif - -#ifdef CONFIG_CDP_CAPABILITIES - *s++ = htons(CDP_CAPABILITIES_TLV); - *s++ = htons(8); - *(ulong *)s = htonl(CONFIG_CDP_CAPABILITIES); - s += 2; -#endif - -#ifdef CONFIG_CDP_VERSION - *s++ = htons(CDP_VERSION_TLV); - memset(buf, 0, sizeof(buf)); - strcpy(buf, CONFIG_CDP_VERSION); - len = strlen(buf); - if (len & 1) /* make it even */ - len++; - *s++ = htons(len + 4); - memcpy((uchar *)s, buf, len); - s += len / 2; -#endif - -#ifdef CONFIG_CDP_PLATFORM - *s++ = htons(CDP_PLATFORM_TLV); - memset(buf, 0, sizeof(buf)); - strcpy(buf, CONFIG_CDP_PLATFORM); - len = strlen(buf); - if (len & 1) /* make it even */ - len++; - *s++ = htons(len + 4); - memcpy((uchar *)s, buf, len); - s += len / 2; -#endif - -#ifdef CONFIG_CDP_TRIGGER - *s++ = htons(CDP_TRIGGER_TLV); - *s++ = htons(8); - *(ulong *)s = htonl(CONFIG_CDP_TRIGGER); - s += 2; -#endif - -#ifdef CONFIG_CDP_POWER_CONSUMPTION - *s++ = htons(CDP_POWER_CONSUMPTION_TLV); - *s++ = htons(6); - *s++ = htons(CONFIG_CDP_POWER_CONSUMPTION); -#endif - - /* length of ethernet packet */ - len = (uchar *)s - ((uchar *)NetTxPacket + ETHER_HDR_SIZE); - et->et_protlen = htons(len); - - len = ETHER_HDR_SIZE + sizeof(CDP_SNAP_hdr); - chksum = CDP_compute_csum((uchar *)NetTxPacket + len, - (uchar *)s - (NetTxPacket + len)); - if (chksum == 0) - chksum = 0xFFFF; - *cp = htons(chksum); - - (void) eth_send(NetTxPacket, (uchar *)s - NetTxPacket); - return 0; -} - -static void -CDPTimeout(void) -{ - CDPSeq++; - - if (CDPSeq < 3) { - NetSetTimeout(CDP_TIMEOUT, CDPTimeout); - CDPSendTrigger(); - return; - } - - /* if not OK try again */ - if (!CDPOK) - NetStartAgain(); - else - NetState = NETLOOP_SUCCESS; -} - -static void -CDPDummyHandler(uchar *pkt, unsigned dest, IPaddr_t sip, unsigned src, - unsigned len) -{ - /* nothing */ -} - -static void -CDPHandler(const uchar *pkt, unsigned len) -{ - const uchar *t; - const ushort *ss; - ushort type, tlen; - ushort vlan, nvlan; - - /* minimum size? */ - if (len < sizeof(CDP_SNAP_hdr) + 4) - goto pkt_short; - - /* check for valid CDP SNAP header */ - if (memcmp(pkt, CDP_SNAP_hdr, sizeof(CDP_SNAP_hdr)) != 0) - return; - - pkt += sizeof(CDP_SNAP_hdr); - len -= sizeof(CDP_SNAP_hdr); - - /* Version of CDP protocol must be >= 2 and TTL != 0 */ - if (pkt[0] < 0x02 || pkt[1] == 0) - return; - - /* - * if version is greater than 0x02 maybe we'll have a problem; - * output a warning - */ - if (pkt[0] != 0x02) - printf("**WARNING: CDP packet received with a protocol version " - "%d > 2\n", pkt[0] & 0xff); - - if (CDP_compute_csum(pkt, len) != 0) - return; - - pkt += 4; - len -= 4; - - vlan = htons(-1); - nvlan = htons(-1); - while (len > 0) { - if (len < 4) - goto pkt_short; - - ss = (const ushort *)pkt; - type = ntohs(ss[0]); - tlen = ntohs(ss[1]); - if (tlen > len) - goto pkt_short; - - pkt += tlen; - len -= tlen; - - ss += 2; /* point ss to the data of the TLV */ - tlen -= 4; - - switch (type) { - case CDP_DEVICE_ID_TLV: - break; - case CDP_ADDRESS_TLV: - break; - case CDP_PORT_ID_TLV: - break; - case CDP_CAPABILITIES_TLV: - break; - case CDP_VERSION_TLV: - break; - case CDP_PLATFORM_TLV: - break; - case CDP_NATIVE_VLAN_TLV: - nvlan = *ss; - break; - case CDP_APPLIANCE_VLAN_TLV: - t = (const uchar *)ss; - while (tlen > 0) { - if (tlen < 3) - goto pkt_short; - - ss = (const ushort *)(t + 1); - -#ifdef CONFIG_CDP_APPLIANCE_VLAN_TYPE - if (t[0] == CONFIG_CDP_APPLIANCE_VLAN_TYPE) - vlan = *ss; -#else - /* XXX will this work; dunno */ - vlan = ntohs(*ss); -#endif - t += 3; tlen -= 3; - } - break; - case CDP_TRIGGER_TLV: - break; - case CDP_POWER_CONSUMPTION_TLV: - break; - case CDP_SYSNAME_TLV: - break; - case CDP_SYSOBJECT_TLV: - break; - case CDP_MANAGEMENT_ADDRESS_TLV: - break; - } - } - - CDPApplianceVLAN = vlan; - CDPNativeVLAN = nvlan; - - CDPOK = 1; - return; - - pkt_short: - printf("** CDP packet is too short\n"); - return; -} - -static void CDPStart(void) -{ - printf("Using %s device\n", eth_get_name()); - CDPSeq = 0; - CDPOK = 0; - - CDPNativeVLAN = htons(-1); - CDPApplianceVLAN = htons(-1); - - NetSetTimeout(CDP_TIMEOUT, CDPTimeout); - NetSetHandler(CDPDummyHandler); - - CDPSendTrigger(); -} -#endif - #ifdef CONFIG_IP_DEFRAG /* * This function collects fragments in a single packet, according

Checking for CDP packets should be encapsulated, not copied code.
Signed-off-by: Joe Hershberger joe.hershberger@ni.com Cc: Joe Hershberger joe.hershberger@gmail.com --- Changes for v2: - Split from patch "Move CDP out of net.c" - Changed to static inline Changes for v3: - Added a function comment - Added a blank line
arch/powerpc/cpu/mpc8xx/fec.c | 2 +- include/net.h | 12 ++++++++++-- net/net.c | 2 +- 3 files changed, 12 insertions(+), 4 deletions(-)
diff --git a/arch/powerpc/cpu/mpc8xx/fec.c b/arch/powerpc/cpu/mpc8xx/fec.c index f2a2c3a..0bc1e5d 100644 --- a/arch/powerpc/cpu/mpc8xx/fec.c +++ b/arch/powerpc/cpu/mpc8xx/fec.c @@ -274,7 +274,7 @@ static int fec_recv (struct eth_device *dev) #if defined(CONFIG_CMD_CDP) if ((rx[0] & 1) != 0 && memcmp ((uchar *) rx, NetBcastAddr, 6) != 0 - && memcmp ((uchar *) rx, NetCDPAddr, 6) != 0) + && !is_cdp_packet((uchar *)rx)) rx = NULL; #endif /* diff --git a/include/net.h b/include/net.h index 8a5b09f..453231b 100644 --- a/include/net.h +++ b/include/net.h @@ -356,8 +356,6 @@ extern uchar NetEtherNullAddr[6]; extern ushort NetOurVLAN; /* Our VLAN */ extern ushort NetOurNativeVLAN; /* Our Native VLAN */
-extern const uchar NetCDPAddr[6]; /* Ethernet CDP address */ - extern int NetState; /* Network loop state */ #define NETLOOP_CONTINUE 1 #define NETLOOP_RESTART 2 @@ -387,6 +385,16 @@ extern IPaddr_t NetPingIP; /* the ip address to ping */ /* when CDP completes these hold the return values */ extern ushort CDPNativeVLAN; /* CDP returned native VLAN */ extern ushort CDPApplianceVLAN; /* CDP returned appliance VLAN */ + +/* + * Check for a CDP packet by examining the received MAC address field + */ +static inline int is_cdp_packet(const uchar *et_addr) +{ + extern const uchar NetCDPAddr[6]; + + return memcmp(et_addr, NetCDPAddr, 6) == 0; +} #endif
#if defined(CONFIG_CMD_SNTP) diff --git a/net/net.c b/net/net.c index 4cd5a0a..8624db0 100644 --- a/net/net.c +++ b/net/net.c @@ -1099,7 +1099,7 @@ NetReceive(uchar *inpkt, int len)
#if defined(CONFIG_CMD_CDP) /* keep track if packet is CDP */ - iscdp = memcmp(et->et_dest, NetCDPAddr, 6) == 0; + iscdp = is_cdp_packet(et->et_dest); #endif
myvlanid = ntohs(NetOurVLAN);

Separate this functionality out of the net.c behemoth
Signed-off-by: Joe Hershberger joe.hershberger@ni.com Cc: Joe Hershberger joe.hershberger@gmail.com --- Changes for v2: Changes for v3: - Added a short commit message
include/net.h | 3 +- net/Makefile | 1 + net/arp.c | 213 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++ net/arp.h | 30 ++++++++ net/net.c | 209 ++++---------------------------------------------------- 5 files changed, 259 insertions(+), 197 deletions(-) create mode 100644 net/arp.c create mode 100644 net/arp.h
diff --git a/include/net.h b/include/net.h index 453231b..64700d9 100644 --- a/include/net.h +++ b/include/net.h @@ -428,7 +428,8 @@ extern void NetSetIP(uchar *, IPaddr_t, int, int, int); extern int NetCksumOk(uchar *, int); /* Return true if cksum OK */ extern uint NetCksum(uchar *, int); /* Calculate the checksum */
-/* Set callbacks */ +/* Callbacks */ +extern rxhand_f *NetGetHandler(void); /* Get RX packet handler */ extern void NetSetHandler(rxhand_f *); /* Set RX packet handler */ extern void net_set_icmp_handler(rxhand_icmp_f *f); /* Set ICMP RX handler */ extern void NetSetTimeout(ulong, thand_f *);/* Set timeout handler */ diff --git a/net/Makefile b/net/Makefile index b350bfc..0916a56 100644 --- a/net/Makefile +++ b/net/Makefile @@ -27,6 +27,7 @@ include $(TOPDIR)/config.mk
LIB = $(obj)libnet.o
+COBJS-$(CONFIG_CMD_NET) += arp.o COBJS-$(CONFIG_CMD_NET) += bootp.o COBJS-$(CONFIG_CMD_CDP) += cdp.o COBJS-$(CONFIG_CMD_DNS) += dns.o diff --git a/net/arp.c b/net/arp.c new file mode 100644 index 0000000..f75217c --- /dev/null +++ b/net/arp.c @@ -0,0 +1,213 @@ +/* + * Copied from Linux Monitor (LiMon) - Networking. + * + * Copyright 1994 - 2000 Neil Russell. + * (See License) + * Copyright 2000 Roland Borde + * Copyright 2000 Paolo Scaffardi + * Copyright 2000-2002 Wolfgang Denk, wd@denx.de + */ + +#include <common.h> + +#include "arp.h" + +#ifndef CONFIG_ARP_TIMEOUT +/* Milliseconds before trying ARP again */ +# define ARP_TIMEOUT 5000UL +#else +# define ARP_TIMEOUT CONFIG_ARP_TIMEOUT +#endif + + +#ifndef CONFIG_NET_RETRY_COUNT +# define ARP_TIMEOUT_COUNT 5 /* # of timeouts before giving up */ +#else +# define ARP_TIMEOUT_COUNT CONFIG_NET_RETRY_COUNT +#endif + +IPaddr_t NetArpWaitPacketIP; +IPaddr_t NetArpWaitReplyIP; +/* MAC address of waiting packet's destination */ +uchar *NetArpWaitPacketMAC; +/* THE transmit packet */ +uchar *NetArpWaitTxPacket; +int NetArpWaitTxPacketSize; +uchar NetArpWaitPacketBuf[PKTSIZE_ALIGN + PKTALIGN]; +ulong NetArpWaitTimerStart; +int NetArpWaitTry; + +void ArpInit(void) +{ + /* XXX problem with bss workaround */ + NetArpWaitPacketMAC = NULL; + NetArpWaitPacketIP = 0; + NetArpWaitReplyIP = 0; + NetArpWaitTxPacket = &NetArpWaitPacketBuf[0] + (PKTALIGN - 1); + NetArpWaitTxPacket -= (ulong)NetArpWaitTxPacket % PKTALIGN; + NetArpWaitTxPacketSize = 0; +} + +void ArpRequest(void) +{ + uchar *pkt; + ARP_t *arp; + + debug("ARP broadcast %d\n", NetArpWaitTry); + + pkt = NetTxPacket; + + pkt += NetSetEther(pkt, NetBcastAddr, PROT_ARP); + + arp = (ARP_t *) pkt; + + arp->ar_hrd = htons(ARP_ETHER); + arp->ar_pro = htons(PROT_IP); + arp->ar_hln = 6; + arp->ar_pln = 4; + arp->ar_op = htons(ARPOP_REQUEST); + + /* source ET addr */ + memcpy(&arp->ar_data[0], NetOurEther, 6); + /* source IP addr */ + NetWriteIP((uchar *) &arp->ar_data[6], NetOurIP); + /* dest ET addr = 0 */ + memset(&arp->ar_data[10], '\0', 6); + if ((NetArpWaitPacketIP & NetOurSubnetMask) != + (NetOurIP & NetOurSubnetMask)) { + if (NetOurGatewayIP == 0) { + puts("## Warning: gatewayip needed but not set\n"); + NetArpWaitReplyIP = NetArpWaitPacketIP; + } else { + NetArpWaitReplyIP = NetOurGatewayIP; + } + } else { + NetArpWaitReplyIP = NetArpWaitPacketIP; + } + + NetWriteIP((uchar *) &arp->ar_data[16], NetArpWaitReplyIP); + (void) eth_send(NetTxPacket, (pkt - NetTxPacket) + ARP_HDR_SIZE); +} + +void ArpTimeoutCheck(void) +{ + ulong t; + + if (!NetArpWaitPacketIP) + return; + + t = get_timer(0); + + /* check for arp timeout */ + if ((t - NetArpWaitTimerStart) > ARP_TIMEOUT) { + NetArpWaitTry++; + + if (NetArpWaitTry >= ARP_TIMEOUT_COUNT) { + puts("\nARP Retry count exceeded; starting again\n"); + NetArpWaitTry = 0; + NetStartAgain(); + } else { + NetArpWaitTimerStart = t; + ArpRequest(); + } + } +} + +void ArpReceive(Ethernet_t *et, IP_t *ip, int len) +{ + ARP_t *arp; + IPaddr_t tmp; + uchar *pkt; + + /* + * We have to deal with two types of ARP packets: + * - REQUEST packets will be answered by sending our + * IP address - if we know it. + * - REPLY packates are expected only after we asked + * for the TFTP server's or the gateway's ethernet + * address; so if we receive such a packet, we set + * the server ethernet address + */ + debug("Got ARP\n"); + + arp = (ARP_t *)ip; + if (len < ARP_HDR_SIZE) { + printf("bad length %d < %d\n", len, ARP_HDR_SIZE); + return; + } + if (ntohs(arp->ar_hrd) != ARP_ETHER) + return; + if (ntohs(arp->ar_pro) != PROT_IP) + return; + if (arp->ar_hln != 6) + return; + if (arp->ar_pln != 4) + return; + + if (NetOurIP == 0) + return; + + if (NetReadIP(&arp->ar_data[16]) != NetOurIP) + return; + + switch (ntohs(arp->ar_op)) { + case ARPOP_REQUEST: + /* reply with our IP address */ + debug("Got ARP REQUEST, return our IP\n"); + pkt = (uchar *)et; + pkt += NetSetEther(pkt, et->et_src, PROT_ARP); + arp->ar_op = htons(ARPOP_REPLY); + memcpy(&arp->ar_data[10], &arp->ar_data[0], 6); + NetCopyIP(&arp->ar_data[16], &arp->ar_data[6]); + memcpy(&arp->ar_data[0], NetOurEther, 6); + NetCopyIP(&arp->ar_data[6], &NetOurIP); + (void) eth_send((uchar *)et, + (pkt - (uchar *)et) + ARP_HDR_SIZE); + return; + + case ARPOP_REPLY: /* arp reply */ + /* are we waiting for a reply */ + if (!NetArpWaitPacketIP || !NetArpWaitPacketMAC) + break; + +#ifdef CONFIG_KEEP_SERVERADDR + if (NetServerIP == NetArpWaitPacketIP) { + char buf[20]; + sprintf(buf, "%pM", arp->ar_data); + setenv("serveraddr", buf); + } +#endif + + tmp = NetReadIP(&arp->ar_data[6]); + + /* matched waiting packet's address */ + if (tmp == NetArpWaitReplyIP) { + debug("Got ARP REPLY, set eth addr (%pM)\n", + arp->ar_data); + + /* save address for later use */ + memcpy(NetArpWaitPacketMAC, + &arp->ar_data[0], 6); + +#ifdef CONFIG_NETCONSOLE + NetGetHandler()(0, 0, 0, 0, 0); +#endif + /* modify header, and transmit it */ + memcpy(((Ethernet_t *)NetArpWaitTxPacket)-> + et_dest, NetArpWaitPacketMAC, 6); + (void) eth_send(NetArpWaitTxPacket, + NetArpWaitTxPacketSize); + + /* no arp request pending now */ + NetArpWaitPacketIP = 0; + NetArpWaitTxPacketSize = 0; + NetArpWaitPacketMAC = NULL; + + } + return; + default: + debug("Unexpected ARP opcode 0x%x\n", + ntohs(arp->ar_op)); + return; + } +} diff --git a/net/arp.h b/net/arp.h new file mode 100644 index 0000000..4016a90 --- /dev/null +++ b/net/arp.h @@ -0,0 +1,30 @@ +/* + * Copied from Linux Monitor (LiMon) - Networking. + * + * Copyright 1994 - 2000 Neil Russell. + * (See License) + * Copyright 2000 Roland Borde + * Copyright 2000 Paolo Scaffardi + * Copyright 2000-2002 Wolfgang Denk, wd@denx.de + */ + +#ifndef __ARP_H__ +#define __ARP_H__ + +#include <common.h> + +extern IPaddr_t NetArpWaitPacketIP; +/* MAC address of waiting packet's destination */ +extern uchar *NetArpWaitPacketMAC; +/* THE transmit packet */ +extern uchar *NetArpWaitTxPacket; +extern int NetArpWaitTxPacketSize; +extern ulong NetArpWaitTimerStart; +extern int NetArpWaitTry; + +void ArpInit(void); +void ArpRequest(void); +void ArpTimeoutCheck(void); +void ArpReceive(Ethernet_t *et, IP_t *ip, int len); + +#endif /* __ARP_H__ */ diff --git a/net/net.c b/net/net.c index 8624db0..ae72746 100644 --- a/net/net.c +++ b/net/net.c @@ -79,6 +79,7 @@ #include <command.h> #include <linux/compiler.h> #include <net.h> +#include "arp.h" #include "bootp.h" #include "tftp.h" #ifdef CONFIG_CMD_RARP @@ -99,20 +100,6 @@
DECLARE_GLOBAL_DATA_PTR;
-#ifndef CONFIG_ARP_TIMEOUT -/* Milliseconds before trying ARP again */ -# define ARP_TIMEOUT 5000UL -#else -# define ARP_TIMEOUT CONFIG_ARP_TIMEOUT -#endif - - -#ifndef CONFIG_NET_RETRY_COUNT -# define ARP_TIMEOUT_COUNT 5 /* # of timeouts before giving up */ -#else -# define ARP_TIMEOUT_COUNT CONFIG_NET_RETRY_COUNT -#endif - /** BOOTP EXTENTIONS **/
/* Our subnet mask (0=unknown) */ @@ -219,82 +206,6 @@ static int NetTryCount;
/**********************************************************************/
-IPaddr_t NetArpWaitPacketIP; -IPaddr_t NetArpWaitReplyIP; -/* MAC address of waiting packet's destination */ -uchar *NetArpWaitPacketMAC; -/* THE transmit packet */ -uchar *NetArpWaitTxPacket; -int NetArpWaitTxPacketSize; -uchar NetArpWaitPacketBuf[PKTSIZE_ALIGN + PKTALIGN]; -ulong NetArpWaitTimerStart; -int NetArpWaitTry; - -void ArpRequest(void) -{ - uchar *pkt; - ARP_t *arp; - - debug("ARP broadcast %d\n", NetArpWaitTry); - - pkt = NetTxPacket; - - pkt += NetSetEther(pkt, NetBcastAddr, PROT_ARP); - - arp = (ARP_t *) pkt; - - arp->ar_hrd = htons(ARP_ETHER); - arp->ar_pro = htons(PROT_IP); - arp->ar_hln = 6; - arp->ar_pln = 4; - arp->ar_op = htons(ARPOP_REQUEST); - - /* source ET addr */ - memcpy(&arp->ar_data[0], NetOurEther, 6); - /* source IP addr */ - NetWriteIP((uchar *) &arp->ar_data[6], NetOurIP); - /* dest ET addr = 0 */ - memset(&arp->ar_data[10], '\0', 6); - if ((NetArpWaitPacketIP & NetOurSubnetMask) != - (NetOurIP & NetOurSubnetMask)) { - if (NetOurGatewayIP == 0) { - puts("## Warning: gatewayip needed but not set\n"); - NetArpWaitReplyIP = NetArpWaitPacketIP; - } else { - NetArpWaitReplyIP = NetOurGatewayIP; - } - } else { - NetArpWaitReplyIP = NetArpWaitPacketIP; - } - - NetWriteIP((uchar *) &arp->ar_data[16], NetArpWaitReplyIP); - (void) eth_send(NetTxPacket, (pkt - NetTxPacket) + ARP_HDR_SIZE); -} - -void ArpTimeoutCheck(void) -{ - ulong t; - - if (!NetArpWaitPacketIP) - return; - - t = get_timer(0); - - /* check for arp timeout */ - if ((t - NetArpWaitTimerStart) > ARP_TIMEOUT) { - NetArpWaitTry++; - - if (NetArpWaitTry >= ARP_TIMEOUT_COUNT) { - puts("\nARP Retry count exceeded; starting again\n"); - NetArpWaitTry = 0; - NetStartAgain(); - } else { - NetArpWaitTimerStart = t; - ArpRequest(); - } - } -} - /* * Check if autoload is enabled. If so, use either NFS or TFTP to download * the boot file. @@ -360,15 +271,11 @@ int NetLoop(enum proto_t protocol) NetRestarted = 0; NetDevExists = 0;
- /* XXX problem with bss workaround */ - NetArpWaitPacketMAC = NULL; - NetArpWaitTxPacket = NULL; - NetArpWaitPacketIP = 0; - NetArpWaitReplyIP = 0; - NetArpWaitTxPacket = NULL; NetTxPacket = NULL; NetTryCount = 1;
+ ArpInit(); + if (!NetTxPacket) { int i; /* @@ -380,12 +287,6 @@ int NetLoop(enum proto_t protocol) NetRxPackets[i] = NetTxPacket + (i+1)*PKTSIZE_ALIGN; }
- if (!NetArpWaitTxPacket) { - NetArpWaitTxPacket = &NetArpWaitPacketBuf[0] + (PKTALIGN - 1); - NetArpWaitTxPacket -= (ulong)NetArpWaitTxPacket % PKTALIGN; - NetArpWaitTxPacketSize = 0; - } - bootstage_mark_name(BOOTSTAGE_ID_ETH_START, "eth_start"); eth_halt(); eth_set_current(); @@ -659,6 +560,13 @@ void NetStartAgain(void) * Miscelaneous bits. */
+rxhand_f * +NetGetHandler(void) +{ + return packetHandler; +} + + void NetSetHandler(rxhand_f *f) { @@ -1070,11 +978,12 @@ NetReceive(uchar *inpkt, int len) { Ethernet_t *et; IP_t *ip; +#ifdef CONFIG_CMD_RARP ARP_t *arp; +#endif IPaddr_t tmp; IPaddr_t src_ip; int x; - uchar *pkt; #if defined(CONFIG_CMD_CDP) int iscdp; #endif @@ -1171,99 +1080,7 @@ NetReceive(uchar *inpkt, int len) switch (x) {
case PROT_ARP: - /* - * We have to deal with two types of ARP packets: - * - REQUEST packets will be answered by sending our - * IP address - if we know it. - * - REPLY packates are expected only after we asked - * for the TFTP server's or the gateway's ethernet - * address; so if we receive such a packet, we set - * the server ethernet address - */ - debug("Got ARP\n"); - - arp = (ARP_t *)ip; - if (len < ARP_HDR_SIZE) { - printf("bad length %d < %d\n", len, ARP_HDR_SIZE); - return; - } - if (ntohs(arp->ar_hrd) != ARP_ETHER) - return; - if (ntohs(arp->ar_pro) != PROT_IP) - return; - if (arp->ar_hln != 6) - return; - if (arp->ar_pln != 4) - return; - - if (NetOurIP == 0) - return; - - if (NetReadIP(&arp->ar_data[16]) != NetOurIP) - return; - - switch (ntohs(arp->ar_op)) { - case ARPOP_REQUEST: - /* reply with our IP address */ - debug("Got ARP REQUEST, return our IP\n"); - pkt = (uchar *)et; - pkt += NetSetEther(pkt, et->et_src, PROT_ARP); - arp->ar_op = htons(ARPOP_REPLY); - memcpy(&arp->ar_data[10], &arp->ar_data[0], 6); - NetCopyIP(&arp->ar_data[16], &arp->ar_data[6]); - memcpy(&arp->ar_data[0], NetOurEther, 6); - NetCopyIP(&arp->ar_data[6], &NetOurIP); - (void) eth_send((uchar *)et, - (pkt - (uchar *)et) + ARP_HDR_SIZE); - return; - - case ARPOP_REPLY: /* arp reply */ - /* are we waiting for a reply */ - if (!NetArpWaitPacketIP || !NetArpWaitPacketMAC) - break; - -#ifdef CONFIG_KEEP_SERVERADDR - if (NetServerIP == NetArpWaitPacketIP) { - char buf[20]; - sprintf(buf, "%pM", arp->ar_data); - setenv("serveraddr", buf); - } -#endif - - debug("Got ARP REPLY, set server/gtwy eth addr (%pM)\n", - arp->ar_data); - - tmp = NetReadIP(&arp->ar_data[6]); - - /* matched waiting packet's address */ - if (tmp == NetArpWaitReplyIP) { - debug("Got it\n"); - - /* save address for later use */ - memcpy(NetArpWaitPacketMAC, - &arp->ar_data[0], 6); - -#ifdef CONFIG_NETCONSOLE - (*packetHandler)(0, 0, 0, 0, 0); -#endif - /* modify header, and transmit it */ - memcpy(((Ethernet_t *)NetArpWaitTxPacket)-> - et_dest, NetArpWaitPacketMAC, 6); - (void) eth_send(NetArpWaitTxPacket, - NetArpWaitTxPacketSize); - - /* no arp request pending now */ - NetArpWaitPacketIP = 0; - NetArpWaitTxPacketSize = 0; - NetArpWaitPacketMAC = NULL; - - } - return; - default: - debug("Unexpected ARP opcode 0x%x\n", - ntohs(arp->ar_op)); - return; - } + ArpReceive(et, ip, len); break;
#ifdef CONFIG_CMD_RARP

Separate this functionality out of the net.c behemoth
Signed-off-by: Joe Hershberger joe.hershberger@ni.com Cc: Joe Hershberger joe.hershberger@gmail.com --- Changes for v2: - Moved config fixes to a separate patch - Pushed #ifdef CONFIG_CMD_PING into header - Eliminated CamelCase Changes for v3: - Added function comments - Added a short commit message
net/Makefile | 1 + net/net.c | 133 ++----------------------------------------------------- net/ping.c | 140 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ net/ping.h | 34 ++++++++++++++ 4 files changed, 179 insertions(+), 129 deletions(-) create mode 100644 net/ping.c create mode 100644 net/ping.h
diff --git a/net/Makefile b/net/Makefile index 0916a56..f1c4859 100644 --- a/net/Makefile +++ b/net/Makefile @@ -35,6 +35,7 @@ COBJS-$(CONFIG_CMD_NET) += eth.o COBJS-$(CONFIG_CMD_NET) += net.o COBJS-$(CONFIG_BOOTP_RANDOM_DELAY) += net_rand.o COBJS-$(CONFIG_CMD_NFS) += nfs.o +COBJS-$(CONFIG_CMD_PING) += ping.o COBJS-$(CONFIG_CMD_RARP) += rarp.o COBJS-$(CONFIG_CMD_SNTP) += sntp.o COBJS-$(CONFIG_CMD_NET) += tftp.o diff --git a/net/net.c b/net/net.c index ae72746..b488490 100644 --- a/net/net.c +++ b/net/net.c @@ -97,6 +97,7 @@ #if defined(CONFIG_CMD_DNS) #include "dns.h" #endif +#include "ping.h"
DECLARE_GLOBAL_DATA_PTR;
@@ -167,13 +168,6 @@ ushort NetOurNativeVLAN = 0xFFFF; /* Boot File name */ char BootFile[128];
-#if defined(CONFIG_CMD_PING) -/* the ip address to ping */ -IPaddr_t NetPingIP; - -static void PingStart(void); -#endif - #if defined(CONFIG_CMD_SNTP) /* NTP server IP address */ IPaddr_t NetNtpServerIP; @@ -356,7 +350,7 @@ restart: #endif #if defined(CONFIG_CMD_PING) case PING: - PingStart(); + ping_start(); break; #endif #if defined(CONFIG_CMD_NFS) @@ -651,94 +645,6 @@ NetSendUDPPacket(uchar *ether, IPaddr_t dest, int dport, int sport, int len) return 0; /* transmitted */ }
-#if defined(CONFIG_CMD_PING) -static ushort PingSeqNo; - -int PingSend(void) -{ - static uchar mac[6]; - IP_t *ip; - ushort *s; - uchar *pkt; - - /* XXX always send arp request */ - - memcpy(mac, NetEtherNullAddr, 6); - - debug("sending ARP for %08x\n", NetPingIP); - - NetArpWaitPacketIP = NetPingIP; - NetArpWaitPacketMAC = mac; - - pkt = NetArpWaitTxPacket; - pkt += NetSetEther(pkt, mac, PROT_IP); - - ip = (IP_t *)pkt; - - /* - * Construct an IP and ICMP header. - * (need to set no fragment bit - XXX) - */ - /* IP_HDR_SIZE / 4 (not including UDP) */ - ip->ip_hl_v = 0x45; - ip->ip_tos = 0; - ip->ip_len = htons(IP_HDR_SIZE_NO_UDP + 8); - ip->ip_id = htons(NetIPID++); - ip->ip_off = htons(IP_FLAGS_DFRAG); /* Don't fragment */ - ip->ip_ttl = 255; - ip->ip_p = 0x01; /* ICMP */ - ip->ip_sum = 0; - /* already in network byte order */ - NetCopyIP((void *)&ip->ip_src, &NetOurIP); - /* - "" - */ - NetCopyIP((void *)&ip->ip_dst, &NetPingIP); - ip->ip_sum = ~NetCksum((uchar *)ip, IP_HDR_SIZE_NO_UDP / 2); - - s = &ip->udp_src; /* XXX ICMP starts here */ - s[0] = htons(0x0800); /* echo-request, code */ - s[1] = 0; /* checksum */ - s[2] = 0; /* identifier */ - s[3] = htons(PingSeqNo++); /* sequence number */ - s[1] = ~NetCksum((uchar *)s, 8/2); - - /* size of the waiting packet */ - NetArpWaitTxPacketSize = - (pkt - NetArpWaitTxPacket) + IP_HDR_SIZE_NO_UDP + 8; - - /* and do the ARP request */ - NetArpWaitTry = 1; - NetArpWaitTimerStart = get_timer(0); - ArpRequest(); - return 1; /* waiting */ -} - -static void -PingTimeout(void) -{ - eth_halt(); - NetState = NETLOOP_FAIL; /* we did not get the reply */ -} - -static void -PingHandler(uchar *pkt, unsigned dest, IPaddr_t sip, unsigned src, - unsigned len) -{ - if (sip != NetPingIP) - return; - - NetState = NETLOOP_SUCCESS; -} - -static void PingStart(void) -{ - printf("Using %s device\n", eth_get_name()); - NetSetTimeout(10000UL, PingTimeout); - NetSetHandler(PingHandler); - - PingSend(); -} -#endif - #ifdef CONFIG_IP_DEFRAG /* * This function collects fragments in a single packet, according @@ -928,41 +834,10 @@ static void receive_icmp(IP_t *ip, int len, IPaddr_t src_ip, Ethernet_t *et) printf(" ICMP Host Redirect to %pI4 ", &icmph->un.gateway); break; + default: #if defined(CONFIG_CMD_PING) - case ICMP_ECHO_REPLY: - /* - * IP header OK. Pass the packet to the - * current handler. - */ - /* - * XXX point to ip packet - should this use - * packet_icmp_handler? - */ - (*packetHandler)((uchar *)ip, 0, src_ip, 0, 0); - break; - case ICMP_ECHO_REQUEST: - debug("Got ICMP ECHO REQUEST, return %d bytes\n", - ETHER_HDR_SIZE + len); - - memcpy(&et->et_dest[0], &et->et_src[0], 6); - memcpy(&et->et_src[0], NetOurEther, 6); - - ip->ip_sum = 0; - ip->ip_off = 0; - NetCopyIP((void *)&ip->ip_dst, &ip->ip_src); - NetCopyIP((void *)&ip->ip_src, &NetOurIP); - ip->ip_sum = ~NetCksum((uchar *)ip, - IP_HDR_SIZE_NO_UDP >> 1); - - icmph->type = ICMP_ECHO_REPLY; - icmph->checksum = 0; - icmph->checksum = ~NetCksum((uchar *)icmph, - (len - IP_HDR_SIZE_NO_UDP) >> 1); - (void) eth_send((uchar *)et, - ETHER_HDR_SIZE + len); - break; + ping_receive(et, ip, len); #endif - default: #ifdef CONFIG_CMD_TFTPPUT if (packet_icmp_handler) packet_icmp_handler(icmph->type, icmph->code, diff --git a/net/ping.c b/net/ping.c new file mode 100644 index 0000000..0e5b1da --- /dev/null +++ b/net/ping.c @@ -0,0 +1,140 @@ +/* + * Copied from Linux Monitor (LiMon) - Networking. + * + * Copyright 1994 - 2000 Neil Russell. + * (See License) + * Copyright 2000 Roland Borde + * Copyright 2000 Paolo Scaffardi + * Copyright 2000-2002 Wolfgang Denk, wd@denx.de + */ + +#include "ping.h" +#include "arp.h" + +static ushort PingSeqNo; + +/* The ip address to ping */ +IPaddr_t NetPingIP; + +static int ping_send(void) +{ + static uchar mac[6]; + IP_t *ip; + ushort *s; + uchar *pkt; + + /* XXX always send arp request */ + + memcpy(mac, NetEtherNullAddr, 6); + + debug("sending ARP for %pI4\n", &NetPingIP); + + NetArpWaitPacketIP = NetPingIP; + NetArpWaitPacketMAC = mac; + + pkt = NetArpWaitTxPacket; + pkt += NetSetEther(pkt, mac, PROT_IP); + + ip = (IP_t *)pkt; + + /* + * Construct an IP and ICMP header. + * (need to set no fragment bit - XXX) + */ + /* IP_HDR_SIZE / 4 (not including UDP) */ + ip->ip_hl_v = 0x45; + ip->ip_tos = 0; + ip->ip_len = htons(IP_HDR_SIZE_NO_UDP + 8); + ip->ip_id = htons(NetIPID++); + ip->ip_off = htons(IP_FLAGS_DFRAG); /* Don't fragment */ + ip->ip_ttl = 255; + ip->ip_p = 0x01; /* ICMP */ + ip->ip_sum = 0; + /* already in network byte order */ + NetCopyIP((void *)&ip->ip_src, &NetOurIP); + /* - "" - */ + NetCopyIP((void *)&ip->ip_dst, &NetPingIP); + ip->ip_sum = ~NetCksum((uchar *)ip, IP_HDR_SIZE_NO_UDP / 2); + + s = &ip->udp_src; /* XXX ICMP starts here */ + s[0] = htons(0x0800); /* echo-request, code */ + s[1] = 0; /* checksum */ + s[2] = 0; /* identifier */ + s[3] = htons(PingSeqNo++); /* sequence number */ + s[1] = ~NetCksum((uchar *)s, 8/2); + + /* size of the waiting packet */ + NetArpWaitTxPacketSize = + (pkt - NetArpWaitTxPacket) + IP_HDR_SIZE_NO_UDP + 8; + + /* and do the ARP request */ + NetArpWaitTry = 1; + NetArpWaitTimerStart = get_timer(0); + ArpRequest(); + return 1; /* waiting */ +} + +static void ping_timeout(void) +{ + eth_halt(); + NetState = NETLOOP_FAIL; /* we did not get the reply */ +} + +static void ping_handler(uchar *pkt, unsigned dest, IPaddr_t sip, + unsigned src, unsigned len) +{ + if (sip != NetPingIP) + return; + + NetState = NETLOOP_SUCCESS; +} + +void ping_start(void) +{ + printf("Using %s device\n", eth_get_name()); + NetSetTimeout(10000UL, ping_timeout); + NetSetHandler(ping_handler); + + ping_send(); +} + +void ping_receive(Ethernet_t *et, IP_t *ip, int len) +{ + ICMP_t *icmph = (ICMP_t *)&(ip->udp_src); + IPaddr_t src_ip; + + switch (icmph->type) { + case ICMP_ECHO_REPLY: + /* + * IP header OK. Pass the packet to the + * current handler. + */ + /* XXX point to ip packet */ + src_ip = NetReadIP((void *)&ip->ip_src); + NetGetHandler()((uchar *)ip, 0, src_ip, 0, 0); + return; + case ICMP_ECHO_REQUEST: + debug("Got ICMP ECHO REQUEST, return " + "%d bytes\n", ETHER_HDR_SIZE + len); + + memcpy(&et->et_dest[0], &et->et_src[0], 6); + memcpy(&et->et_src[0], NetOurEther, 6); + + ip->ip_sum = 0; + ip->ip_off = 0; + NetCopyIP((void *)&ip->ip_dst, &ip->ip_src); + NetCopyIP((void *)&ip->ip_src, &NetOurIP); + ip->ip_sum = ~NetCksum((uchar *)ip, + IP_HDR_SIZE_NO_UDP >> 1); + + icmph->type = ICMP_ECHO_REPLY; + icmph->checksum = 0; + icmph->checksum = ~NetCksum((uchar *)icmph, + (len - IP_HDR_SIZE_NO_UDP) >> 1); + (void) eth_send((uchar *)et, + ETHER_HDR_SIZE + len); + return; +/* default: + return;*/ + } +} diff --git a/net/ping.h b/net/ping.h new file mode 100644 index 0000000..246d3aa --- /dev/null +++ b/net/ping.h @@ -0,0 +1,34 @@ +/* + * Copied from Linux Monitor (LiMon) - Networking. + * + * Copyright 1994 - 2000 Neil Russell. + * (See License) + * Copyright 2000 Roland Borde + * Copyright 2000 Paolo Scaffardi + * Copyright 2000-2002 Wolfgang Denk, wd@denx.de + */ + +#if defined(CONFIG_CMD_PING) + +#ifndef __PING_H__ +#define __PING_H__ + +#include <common.h> +#include <net.h> + +/* + * Initialize ping (beginning of netloop) + */ +void ping_start(void); + +/* + * Deal with the receipt of a ping packet + * + * @param et Ethernet header in packet + * @param ip IP header in the same packet + * @param len Packet length + */ +void ping_receive(Ethernet_t *et, IP_t *ip, int len); + +#endif /* __PING_H__ */ +#endif

Separate this functionality out of the net.c behemoth
Signed-off-by: Joe Hershberger joe.hershberger@ni.com Cc: Joe Hershberger joe.hershberger@gmail.com --- Changes for v2: - Moved void to the same line as function - Eliminate CamelCase in new function name Changes for v3: - Removed extra spaces - Added a function comment - Added a short commit message
net/net.c | 27 +-------------------------- net/rarp.c | 51 ++++++++++++++++++++++++++++++++------------------- net/rarp.h | 7 ++++--- 3 files changed, 37 insertions(+), 48 deletions(-)
diff --git a/net/net.c b/net/net.c index b488490..ed86d01 100644 --- a/net/net.c +++ b/net/net.c @@ -82,9 +82,7 @@ #include "arp.h" #include "bootp.h" #include "tftp.h" -#ifdef CONFIG_CMD_RARP #include "rarp.h" -#endif #include "nfs.h" #ifdef CONFIG_STATUS_LED #include <status_led.h> @@ -853,9 +851,6 @@ NetReceive(uchar *inpkt, int len) { Ethernet_t *et; IP_t *ip; -#ifdef CONFIG_CMD_RARP - ARP_t *arp; -#endif IPaddr_t tmp; IPaddr_t src_ip; int x; @@ -960,27 +955,7 @@ NetReceive(uchar *inpkt, int len)
#ifdef CONFIG_CMD_RARP case PROT_RARP: - debug("Got RARP\n"); - arp = (ARP_t *)ip; - if (len < ARP_HDR_SIZE) { - printf("bad length %d < %d\n", len, ARP_HDR_SIZE); - return; - } - - if ((ntohs(arp->ar_op) != RARPOP_REPLY) || - (ntohs(arp->ar_hrd) != ARP_ETHER) || - (ntohs(arp->ar_pro) != PROT_IP) || - (arp->ar_hln != 6) || (arp->ar_pln != 4)) { - - puts("invalid RARP header\n"); - } else { - NetCopyIP(&NetOurIP, &arp->ar_data[16]); - if (NetServerIP == 0) - NetCopyIP(&NetServerIP, &arp->ar_data[6]); - memcpy(NetServerEther, &arp->ar_data[0], 6); - - (*packetHandler)(0, 0, 0, 0, 0); - } + rarp_receive(ip, len); break; #endif case PROT_IP: diff --git a/net/rarp.c b/net/rarp.c index 5a813a2..660a02c 100644 --- a/net/rarp.c +++ b/net/rarp.c @@ -29,33 +29,50 @@ #include "rarp.h" #include "tftp.h"
-#define TIMEOUT 5000UL /* Milliseconds before trying BOOTP again */ +#define TIMEOUT 5000UL /* Milliseconds before trying BOOTP again */ #ifndef CONFIG_NET_RETRY_COUNT -# define TIMEOUT_COUNT 5 /* # of timeouts before giving up */ +#define TIMEOUT_COUNT 5 /* # of timeouts before giving up */ #else -# define TIMEOUT_COUNT (CONFIG_NET_RETRY_COUNT) +#define TIMEOUT_COUNT (CONFIG_NET_RETRY_COUNT) #endif
- -int RarpTry; +int RarpTry;
/* * Handle a RARP received packet. */ -static void -RarpHandler(uchar *dummi0, unsigned dummi1, IPaddr_t sip, unsigned dummi2, - unsigned dummi3) +void rarp_receive(IP_t *ip, unsigned len) { - debug("Got good RARP\n"); - net_auto_load(); + ARP_t *arp; + + debug("Got RARP\n"); + arp = (ARP_t *)ip; + if (len < ARP_HDR_SIZE) { + printf("bad length %d < %d\n", len, ARP_HDR_SIZE); + return; + } + + if ((ntohs(arp->ar_op) != RARPOP_REPLY) || + (ntohs(arp->ar_hrd) != ARP_ETHER) || + (ntohs(arp->ar_pro) != PROT_IP) || + (arp->ar_hln != 6) || (arp->ar_pln != 4)) { + + puts("invalid RARP header\n"); + } else { + NetCopyIP(&NetOurIP, &arp->ar_data[16]); + if (NetServerIP == 0) + NetCopyIP(&NetServerIP, &arp->ar_data[6]); + memcpy(NetServerEther, &arp->ar_data[0], 6); + debug("Got good RARP\n"); + net_auto_load(); + } }
/* * Timeout on BOOTP request. */ -static void -RarpTimeout(void) +static void RarpTimeout(void) { if (RarpTry >= TIMEOUT_COUNT) { puts("\nRetry count exceeded; starting again\n"); @@ -67,10 +84,8 @@ RarpTimeout(void) }
-void -RarpRequest(void) +void RarpRequest(void) { - int i; uchar *pkt; ARP_t *rarp;
@@ -90,12 +105,10 @@ RarpRequest(void) memcpy(&rarp->ar_data[6], &NetOurIP, 4); /* source IP addr */ /* dest ET addr = source ET addr ??*/ memcpy(&rarp->ar_data[10], NetOurEther, 6); - /* dest. IP addr set to broadcast */ - for (i = 0; i <= 3; i++) - rarp->ar_data[16 + i] = 0xff; + /* dest IP addr set to broadcast */ + memset(&rarp->ar_data[16], 0xff, 4);
NetSendPacket(NetTxPacket, (pkt - NetTxPacket) + ARP_HDR_SIZE);
NetSetTimeout(TIMEOUT, RarpTimeout); - NetSetHandler(RarpHandler); } diff --git a/net/rarp.h b/net/rarp.h index 4e92d80..fc5b363 100644 --- a/net/rarp.h +++ b/net/rarp.h @@ -21,14 +21,12 @@ * MA 02111-1307 USA */
+#if defined(CONFIG_CMD_RARP)
#ifndef __RARP_H__ #define __RARP_H__
-#ifndef __NET_H__ #include <net.h> -#endif /* __NET_H__ */ -
/**********************************************************************/ /* @@ -37,8 +35,11 @@
extern int RarpTry;
+/* Process the receipt of a RARP packet */ +extern void rarp_receive(IP_t *ip, unsigned len); extern void RarpRequest(void); /* Send a RARP request */
/**********************************************************************/
#endif /* __RARP_H__ */ +#endif

Rename IP header related things to IP_UDP. The existing definition of IP_t includes UDP header, so name it to accurately describe the structure.
Signed-off-by: Joe Hershberger joe.hershberger@ni.com Cc: Joe Hershberger joe.hershberger@gmail.com --- Changes for v2: - Split apart from "Un-typedef variables in net" - Created a new IP_UDP_hdr struct and removed UDP members from IP_hdr - Renamed structs from *_t to *_hdr since they are nolonger types Changes for v3: - Change IP_UDP_hdr to ip_udp_hdr - Split out the new ip_hdr to the next patch
drivers/net/netconsole.c | 4 +- include/net.h | 11 ++++--- net/arp.c | 2 +- net/arp.h | 2 +- net/bootp.c | 4 +- net/bootp.h | 2 +- net/dns.c | 2 +- net/net.c | 62 +++++++++++++++++++++++---------------------- net/nfs.c | 2 +- net/ping.c | 6 ++-- net/ping.h | 2 +- net/rarp.c | 2 +- net/rarp.h | 2 +- net/sntp.c | 2 +- net/tftp.c | 2 +- 15 files changed, 55 insertions(+), 52 deletions(-)
diff --git a/drivers/net/netconsole.c b/drivers/net/netconsole.c index 7acd5b0..ba02fd7 100644 --- a/drivers/net/netconsole.c +++ b/drivers/net/netconsole.c @@ -69,7 +69,7 @@ void NcStart(void) /* send arp request */ uchar *pkt; NetSetHandler(nc_wait_arp_handler); - pkt = (uchar *)NetTxPacket + NetEthHdrSize() + IP_HDR_SIZE; + pkt = (uchar *)NetTxPacket + NetEthHdrSize() + IP_UDP_HDR_SIZE; memcpy(pkt, output_packet, output_packet_len); NetSendUDPPacket(nc_ether, nc_ip, nc_port, nc_port, output_packet_len); @@ -131,7 +131,7 @@ static void nc_send_packet(const char *buf, int len) return; inited = 1; } - pkt = (uchar *)NetTxPacket + NetEthHdrSize() + IP_HDR_SIZE; + pkt = (uchar *)NetTxPacket + NetEthHdrSize() + IP_UDP_HDR_SIZE; memcpy(pkt, buf, len); ether = nc_ether; ip = nc_ip; diff --git a/include/net.h b/include/net.h index 64700d9..3e4f304 100644 --- a/include/net.h +++ b/include/net.h @@ -194,9 +194,9 @@ typedef struct { #define IPPROTO_UDP 17 /* User Datagram Protocol */
/* - * Internet Protocol (IP) header. + * Internet Protocol (IP) + UDP header. */ -typedef struct { +struct ip_udp_hdr { uchar ip_hl_v; /* header length and version */ uchar ip_tos; /* type of service */ ushort ip_len; /* total length */ @@ -211,7 +211,7 @@ typedef struct { ushort udp_dst; /* UDP destination port */ ushort udp_len; /* Length of UDP packet */ ushort udp_xsum; /* Checksum */ -} IP_t; +};
#define IP_OFFS 0x1fff /* ip offset *= 8 */ #define IP_FLAGS 0xe000 /* first 3 bits */ @@ -219,9 +219,10 @@ typedef struct { #define IP_FLAGS_DFRAG 0x4000 /* don't fragments */ #define IP_FLAGS_MFRAG 0x2000 /* more fragments */
-#define IP_HDR_SIZE_NO_UDP (sizeof(IP_t) - 8) -#define IP_HDR_SIZE (sizeof(IP_t)) +#define IP_HDR_SIZE_NO_UDP (sizeof(struct ip_udp_hdr) - 8)
+#define IP_UDP_HDR_SIZE (sizeof(struct ip_udp_hdr)) +#define UDP_HDR_SIZE (IP_UDP_HDR_SIZE - IP_HDR_SIZE_NO_UDP)
/* * Address Resolution Protocol (ARP) header. diff --git a/net/arp.c b/net/arp.c index f75217c..03272ad 100644 --- a/net/arp.c +++ b/net/arp.c @@ -113,7 +113,7 @@ void ArpTimeoutCheck(void) } }
-void ArpReceive(Ethernet_t *et, IP_t *ip, int len) +void ArpReceive(Ethernet_t *et, struct ip_udp_hdr *ip, int len) { ARP_t *arp; IPaddr_t tmp; diff --git a/net/arp.h b/net/arp.h index 4016a90..b59fbdd 100644 --- a/net/arp.h +++ b/net/arp.h @@ -25,6 +25,6 @@ extern int NetArpWaitTry; void ArpInit(void); void ArpRequest(void); void ArpTimeoutCheck(void); -void ArpReceive(Ethernet_t *et, IP_t *ip, int len); +void ArpReceive(Ethernet_t *et, struct ip_udp_hdr *ip, int len);
#endif /* __ARP_H__ */ diff --git a/net/bootp.c b/net/bootp.c index 0185e56..b8d2760 100644 --- a/net/bootp.c +++ b/net/bootp.c @@ -622,7 +622,7 @@ BootpRequest(void) /* NetSetIP(pkt, 0xFFFFFFFFL, PORT_BOOTPS, PORT_BOOTPC, sizeof (struct Bootp_t)); */ iphdr = pkt; /* We need this later for NetSetIP() */ - pkt += IP_HDR_SIZE; + pkt += IP_UDP_HDR_SIZE;
bp = (struct Bootp_t *)pkt; bp->bp_op = OP_BOOTREQUEST; @@ -807,7 +807,7 @@ static void DhcpSendRequestPkt(struct Bootp_t *bp_offer) pkt += NetSetEther(pkt, NetBcastAddr, PROT_IP);
iphdr = pkt; /* We'll need this later to set proper pkt size */ - pkt += IP_HDR_SIZE; + pkt += IP_UDP_HDR_SIZE;
bp = (struct Bootp_t *)pkt; bp->bp_op = OP_BOOTREQUEST; diff --git a/net/bootp.h b/net/bootp.h index bf4e875..1cf9a02 100644 --- a/net/bootp.h +++ b/net/bootp.h @@ -52,7 +52,7 @@ struct Bootp_t { };
#define BOOTP_HDR_SIZE sizeof(struct Bootp_t) -#define BOOTP_SIZE (ETHER_HDR_SIZE + IP_HDR_SIZE + BOOTP_HDR_SIZE) +#define BOOTP_SIZE (ETHER_HDR_SIZE + IP_UDP_HDR_SIZE + BOOTP_HDR_SIZE)
/**********************************************************************/ /* diff --git a/net/dns.c b/net/dns.c index 7a3f1f9..cc7ed51 100644 --- a/net/dns.c +++ b/net/dns.c @@ -45,7 +45,7 @@ DnsSend(void) enum dns_query_type qtype = DNS_A_RECORD;
name = NetDNSResolve; - pkt = p = (uchar *)(NetTxPacket + NetEthHdrSize() + IP_HDR_SIZE); + pkt = p = (uchar *)(NetTxPacket + NetEthHdrSize() + IP_UDP_HDR_SIZE);
/* Prepare DNS packet header */ header = (struct header *) pkt; diff --git a/net/net.c b/net/net.c index ed86d01..f0e8a67 100644 --- a/net/net.c +++ b/net/net.c @@ -619,12 +619,13 @@ NetSendUDPPacket(uchar *ether, IPaddr_t dest, int dport, int sport, int len) pkt += NetSetEther(pkt, NetArpWaitPacketMAC, PROT_IP);
NetSetIP(pkt, dest, dport, sport, len); - memcpy(pkt + IP_HDR_SIZE, (uchar *)NetTxPacket + - (pkt - (uchar *)NetArpWaitTxPacket) + IP_HDR_SIZE, len); + memcpy(pkt + IP_UDP_HDR_SIZE, (uchar *)NetTxPacket + + (pkt - (uchar *)NetArpWaitTxPacket) + + IP_UDP_HDR_SIZE, len);
/* size of the waiting packet */ NetArpWaitTxPacketSize = (pkt - NetArpWaitTxPacket) + - IP_HDR_SIZE + len; + IP_UDP_HDR_SIZE + len;
/* and do the ARP request */ NetArpWaitTry = 1; @@ -638,7 +639,7 @@ NetSendUDPPacket(uchar *ether, IPaddr_t dest, int dport, int sport, int len) pkt = (uchar *)NetTxPacket; pkt += NetSetEther(pkt, ether, PROT_IP); NetSetIP(pkt, dest, dport, sport, len); - (void) eth_send(NetTxPacket, (pkt - NetTxPacket) + IP_HDR_SIZE + len); + eth_send(NetTxPacket, (pkt - NetTxPacket) + IP_UDP_HDR_SIZE + len);
return 0; /* transmitted */ } @@ -676,12 +677,12 @@ struct hole { u16 unused; };
-static IP_t *__NetDefragment(IP_t *ip, int *lenp) +static struct ip_udp_hdr *__NetDefragment(struct ip_udp_hdr *ip, int *lenp) { static uchar pkt_buff[IP_PKTSIZE] __aligned(PKTALIGN); static u16 first_hole, total_len; struct hole *payload, *thisfrag, *h, *newh; - IP_t *localip = (IP_t *)pkt_buff; + struct ip_udp_hdr *localip = (struct ip_udp_hdr *)pkt_buff; uchar *indata = (uchar *)ip; int offset8, start, len, done = 0; u16 ip_off = ntohs(ip->ip_off); @@ -796,7 +797,7 @@ static IP_t *__NetDefragment(IP_t *ip, int *lenp) return localip; }
-static inline IP_t *NetDefragment(IP_t *ip, int *lenp) +static inline struct ip_udp_hdr *NetDefragment(struct ip_udp_hdr *ip, int *lenp) { u16 ip_off = ntohs(ip->ip_off); if (!(ip_off & (IP_OFFS | IP_FLAGS_MFRAG))) @@ -806,7 +807,7 @@ static inline IP_t *NetDefragment(IP_t *ip, int *lenp)
#else /* !CONFIG_IP_DEFRAG */
-static inline IP_t *NetDefragment(IP_t *ip, int *lenp) +static inline struct ip_udp_hdr *NetDefragment(struct ip_udp_hdr *ip, int *lenp) { u16 ip_off = ntohs(ip->ip_off); if (!(ip_off & (IP_OFFS | IP_FLAGS_MFRAG))) @@ -821,7 +822,8 @@ static inline IP_t *NetDefragment(IP_t *ip, int *lenp) * * @parma ip IP packet containing the ICMP */ -static void receive_icmp(IP_t *ip, int len, IPaddr_t src_ip, Ethernet_t *et) +static void receive_icmp(struct ip_udp_hdr *ip, int len, + IPaddr_t src_ip, Ethernet_t *et) { ICMP_t *icmph = (ICMP_t *)&ip->udp_src;
@@ -850,7 +852,7 @@ void NetReceive(uchar *inpkt, int len) { Ethernet_t *et; - IP_t *ip; + struct ip_udp_hdr *ip; IPaddr_t tmp; IPaddr_t src_ip; int x; @@ -898,11 +900,11 @@ NetReceive(uchar *inpkt, int len) */ x = ntohs(et->et_prot);
- ip = (IP_t *)(inpkt + E802_HDR_SIZE); + ip = (struct ip_udp_hdr *)(inpkt + E802_HDR_SIZE); len -= E802_HDR_SIZE;
} else if (x != PROT_VLAN) { /* normal packet */ - ip = (IP_t *)(inpkt + ETHER_HDR_SIZE); + ip = (struct ip_udp_hdr *)(inpkt + ETHER_HDR_SIZE); len -= ETHER_HDR_SIZE;
} else { /* VLAN packet */ @@ -926,7 +928,7 @@ NetReceive(uchar *inpkt, int len) vlanid = cti & VLAN_IDMASK; x = ntohs(vet->vet_type);
- ip = (IP_t *)(inpkt + VLAN_ETHER_HDR_SIZE); + ip = (struct ip_udp_hdr *)(inpkt + VLAN_ETHER_HDR_SIZE); len -= VLAN_ETHER_HDR_SIZE; }
@@ -961,8 +963,9 @@ NetReceive(uchar *inpkt, int len) case PROT_IP: debug("Got IP\n"); /* Before we start poking the header, make sure it is there */ - if (len < IP_HDR_SIZE) { - debug("len bad %d < %lu\n", len, (ulong)IP_HDR_SIZE); + if (len < IP_UDP_HDR_SIZE) { + debug("len bad %d < %lu\n", len, + (ulong)IP_UDP_HDR_SIZE); return; } /* Check the packet length */ @@ -1074,19 +1077,19 @@ NetReceive(uchar *inpkt, int len)
#ifdef CONFIG_NETCONSOLE - nc_input_packet((uchar *)ip + IP_HDR_SIZE, - ntohs(ip->udp_dst), - ntohs(ip->udp_src), - ntohs(ip->udp_len) - 8); + nc_input_packet((uchar *)ip + IP_UDP_HDR_SIZE, + ntohs(ip->udp_dst), + ntohs(ip->udp_src), + ntohs(ip->udp_len) - UDP_HDR_SIZE); #endif /* * IP header OK. Pass the packet to the current handler. */ - (*packetHandler)((uchar *)ip + IP_HDR_SIZE, - ntohs(ip->udp_dst), - src_ip, - ntohs(ip->udp_src), - ntohs(ip->udp_len) - 8); + (*packetHandler)((uchar *)ip + IP_UDP_HDR_SIZE, + ntohs(ip->udp_dst), + src_ip, + ntohs(ip->udp_src), + ntohs(ip->udp_len) - UDP_HDR_SIZE); break; } } @@ -1237,10 +1240,9 @@ NetSetEther(uchar *xet, uchar * addr, uint prot) } }
-void -NetSetIP(uchar *xip, IPaddr_t dest, int dport, int sport, int len) +void NetSetIP(uchar *xip, IPaddr_t dest, int dport, int sport, int len) { - IP_t *ip = (IP_t *)xip; + struct ip_udp_hdr *ip = (struct ip_udp_hdr *)xip;
/* * If the data is an odd number of bytes, zero the @@ -1248,7 +1250,7 @@ NetSetIP(uchar *xip, IPaddr_t dest, int dport, int sport, int len) * will work. */ if (len & 1) - xip[IP_HDR_SIZE + len] = 0; + xip[IP_UDP_HDR_SIZE + len] = 0;
/* * Construct an IP and UDP header. @@ -1257,7 +1259,7 @@ NetSetIP(uchar *xip, IPaddr_t dest, int dport, int sport, int len) /* IP_HDR_SIZE / 4 (not including UDP) */ ip->ip_hl_v = 0x45; ip->ip_tos = 0; - ip->ip_len = htons(IP_HDR_SIZE + len); + ip->ip_len = htons(IP_UDP_HDR_SIZE + len); ip->ip_id = htons(NetIPID++); ip->ip_off = htons(IP_FLAGS_DFRAG); /* Don't fragment */ ip->ip_ttl = 255; @@ -1269,7 +1271,7 @@ NetSetIP(uchar *xip, IPaddr_t dest, int dport, int sport, int len) NetCopyIP((void *)&ip->ip_dst, &dest); ip->udp_src = htons(sport); ip->udp_dst = htons(dport); - ip->udp_len = htons(8 + len); + ip->udp_len = htons(UDP_HDR_SIZE + len); ip->udp_xsum = 0; ip->ip_sum = ~NetCksum((uchar *)ip, IP_HDR_SIZE_NO_UDP / 2); } diff --git a/net/nfs.c b/net/nfs.c index 54f56c4..b6188fe 100644 --- a/net/nfs.c +++ b/net/nfs.c @@ -189,7 +189,7 @@ rpc_req(int rpc_prog, int rpc_proc, uint32_t *data, int datalen)
pktlen = (char *)p + datalen*sizeof(uint32_t) - (char *)&pkt;
- memcpy((char *)NetTxPacket + NetEthHdrSize() + IP_HDR_SIZE, + memcpy((char *)NetTxPacket + NetEthHdrSize() + IP_UDP_HDR_SIZE, (char *)&pkt, pktlen);
if (rpc_prog == PROG_PORTMAP) diff --git a/net/ping.c b/net/ping.c index 0e5b1da..6a2e85d 100644 --- a/net/ping.c +++ b/net/ping.c @@ -19,7 +19,7 @@ IPaddr_t NetPingIP; static int ping_send(void) { static uchar mac[6]; - IP_t *ip; + struct ip_udp_hdr *ip; ushort *s; uchar *pkt;
@@ -35,7 +35,7 @@ static int ping_send(void) pkt = NetArpWaitTxPacket; pkt += NetSetEther(pkt, mac, PROT_IP);
- ip = (IP_t *)pkt; + ip = (struct ip_udp_hdr *)pkt;
/* * Construct an IP and ICMP header. @@ -98,7 +98,7 @@ void ping_start(void) ping_send(); }
-void ping_receive(Ethernet_t *et, IP_t *ip, int len) +void ping_receive(Ethernet_t *et, struct ip_udp_hdr *ip, int len) { ICMP_t *icmph = (ICMP_t *)&(ip->udp_src); IPaddr_t src_ip; diff --git a/net/ping.h b/net/ping.h index 246d3aa..0a2d7d1 100644 --- a/net/ping.h +++ b/net/ping.h @@ -28,7 +28,7 @@ void ping_start(void); * @param ip IP header in the same packet * @param len Packet length */ -void ping_receive(Ethernet_t *et, IP_t *ip, int len); +void ping_receive(Ethernet_t *et, struct ip_udp_hdr *ip, int len);
#endif /* __PING_H__ */ #endif diff --git a/net/rarp.c b/net/rarp.c index 660a02c..1b1f502 100644 --- a/net/rarp.c +++ b/net/rarp.c @@ -41,7 +41,7 @@ int RarpTry; /* * Handle a RARP received packet. */ -void rarp_receive(IP_t *ip, unsigned len) +void rarp_receive(struct ip_upd_hdr *ip, unsigned len) { ARP_t *arp;
diff --git a/net/rarp.h b/net/rarp.h index fc5b363..2561289 100644 --- a/net/rarp.h +++ b/net/rarp.h @@ -36,7 +36,7 @@ extern int RarpTry;
/* Process the receipt of a RARP packet */ -extern void rarp_receive(IP_t *ip, unsigned len); +extern void rarp_receive(struct ip_upd_hdr *ip, unsigned len); extern void RarpRequest(void); /* Send a RARP request */
/**********************************************************************/ diff --git a/net/sntp.c b/net/sntp.c index 69cddb1..07d036d 100644 --- a/net/sntp.c +++ b/net/sntp.c @@ -31,7 +31,7 @@ SntpSend(void) pkt.vn = NTP_VERSION; pkt.mode = NTP_MODE_CLIENT;
- memcpy((char *)NetTxPacket + NetEthHdrSize() + IP_HDR_SIZE, + memcpy((char *)NetTxPacket + NetEthHdrSize() + IP_UDP_HDR_SIZE, (char *)&pkt, pktlen);
SntpOurPort = 10000 + (get_timer(0) % 4096); diff --git a/net/tftp.c b/net/tftp.c index bc7fe05..a04a832 100644 --- a/net/tftp.c +++ b/net/tftp.c @@ -322,7 +322,7 @@ TftpSend(void) * We will always be sending some sort of packet, so * cobble together the packet headers now. */ - pkt = (uchar *)(NetTxPacket + NetEthHdrSize() + IP_HDR_SIZE); + pkt = NetTxPacket + NetEthHdrSize() + IP_UDP_HDR_SIZE;
switch (TftpState) { case STATE_SEND_RRQ:

Hi Joe,
On Wed, 23 May 2012 12:58:04 -0500 Joe Hershberger joe.hershberger@ni.com wrote: ...
--- a/net/rarp.c +++ b/net/rarp.c @@ -41,7 +41,7 @@ int RarpTry; /*
- Handle a RARP received packet.
*/ -void rarp_receive(IP_t *ip, unsigned len) +void rarp_receive(struct ip_upd_hdr *ip, unsigned len)
Typo, s/ip_upd_hdr/ip_udp_hdr/
...
--- a/net/rarp.h +++ b/net/rarp.h @@ -36,7 +36,7 @@ extern int RarpTry;
/* Process the receipt of a RARP packet */ -extern void rarp_receive(IP_t *ip, unsigned len); +extern void rarp_receive(struct ip_upd_hdr *ip, unsigned len);
Typo, s/ip_upd_hdr/ip_udp_hdr/
It breaks compiling with CONFIG_CMD_RARP defined.
Thanks, Anatolij

Hi Anatolij,
On Wed, May 23, 2012 at 5:36 PM, Anatolij Gustschin agust@denx.de wrote:
Hi Joe,
On Wed, 23 May 2012 12:58:04 -0500 Joe Hershberger joe.hershberger@ni.com wrote: ...
--- a/net/rarp.c +++ b/net/rarp.c @@ -41,7 +41,7 @@ int RarpTry; /* * Handle a RARP received packet. */ -void rarp_receive(IP_t *ip, unsigned len) +void rarp_receive(struct ip_upd_hdr *ip, unsigned len)
Typo, s/ip_upd_hdr/ip_udp_hdr/
...
--- a/net/rarp.h +++ b/net/rarp.h @@ -36,7 +36,7 @@ extern int RarpTry;
/* Process the receipt of a RARP packet */ -extern void rarp_receive(IP_t *ip, unsigned len); +extern void rarp_receive(struct ip_upd_hdr *ip, unsigned len);
Typo, s/ip_upd_hdr/ip_udp_hdr/
It breaks compiling with CONFIG_CMD_RARP defined.
Apparently I didn't build test the ethernut5 target since that last round of changes.
I think I'll just fix it inline instead of spamming the mailing list with the whole series again.
Thanks, -Joe

Add a structure that only contains IP header fields to be used by functions that don't need UDP Rename IP_HDR_SIZE_NO_UDP to IP_HDR_SIZE
Signed-off-by: Joe Hershberger joe.hershberger@ni.com Cc: Joe Hershberger joe.hershberger@gmail.com --- Changes for v2: - Split apart from "Un-typedef variables in net" - Created a new IP_UDP_hdr struct and removed UDP members from IP_hdr - Renamed structs from *_t to *_hdr since they are nolonger types Changes for v3: - Change IP_hdr to ip_hdr - Split this out from the previous patch
include/net.h | 32 ++++++++++++++++++++++++-------- net/net.c | 16 ++++++++-------- net/ping.c | 10 +++++----- 3 files changed, 37 insertions(+), 21 deletions(-)
diff --git a/include/net.h b/include/net.h index 3e4f304..7692153 100644 --- a/include/net.h +++ b/include/net.h @@ -194,9 +194,9 @@ typedef struct { #define IPPROTO_UDP 17 /* User Datagram Protocol */
/* - * Internet Protocol (IP) + UDP header. + * Internet Protocol (IP) header. */ -struct ip_udp_hdr { +struct ip_hdr { uchar ip_hl_v; /* header length and version */ uchar ip_tos; /* type of service */ ushort ip_len; /* total length */ @@ -207,10 +207,6 @@ struct ip_udp_hdr { ushort ip_sum; /* checksum */ IPaddr_t ip_src; /* Source IP address */ IPaddr_t ip_dst; /* Destination IP address */ - ushort udp_src; /* UDP source port */ - ushort udp_dst; /* UDP destination port */ - ushort udp_len; /* Length of UDP packet */ - ushort udp_xsum; /* Checksum */ };
#define IP_OFFS 0x1fff /* ip offset *= 8 */ @@ -219,10 +215,30 @@ struct ip_udp_hdr { #define IP_FLAGS_DFRAG 0x4000 /* don't fragments */ #define IP_FLAGS_MFRAG 0x2000 /* more fragments */
-#define IP_HDR_SIZE_NO_UDP (sizeof(struct ip_udp_hdr) - 8) +#define IP_HDR_SIZE (sizeof(struct ip_hdr)) + +/* + * Internet Protocol (IP) + UDP header. + */ +struct ip_udp_hdr { + uchar ip_hl_v; /* header length and version */ + uchar ip_tos; /* type of service */ + ushort ip_len; /* total length */ + ushort ip_id; /* identification */ + ushort ip_off; /* fragment offset field */ + uchar ip_ttl; /* time to live */ + uchar ip_p; /* protocol */ + ushort ip_sum; /* checksum */ + IPaddr_t ip_src; /* Source IP address */ + IPaddr_t ip_dst; /* Destination IP address */ + ushort udp_src; /* UDP source port */ + ushort udp_dst; /* UDP destination port */ + ushort udp_len; /* Length of UDP packet */ + ushort udp_xsum; /* Checksum */ +};
#define IP_UDP_HDR_SIZE (sizeof(struct ip_udp_hdr)) -#define UDP_HDR_SIZE (IP_UDP_HDR_SIZE - IP_HDR_SIZE_NO_UDP) +#define UDP_HDR_SIZE (IP_UDP_HDR_SIZE - IP_HDR_SIZE)
/* * Address Resolution Protocol (ARP) header. diff --git a/net/net.c b/net/net.c index f0e8a67..2bf5631 100644 --- a/net/net.c +++ b/net/net.c @@ -663,7 +663,7 @@ NetSendUDPPacket(uchar *ether, IPaddr_t dest, int dport, int sport, int len) static struct rpc_t rpc_specimen; #define IP_PKTSIZE (CONFIG_NET_MAXDEFRAG + sizeof(rpc_specimen.u.reply))
-#define IP_MAXUDP (IP_PKTSIZE - IP_HDR_SIZE_NO_UDP) +#define IP_MAXUDP (IP_PKTSIZE - IP_HDR_SIZE)
/* * this is the packet being assembled, either data or frag control. @@ -688,11 +688,11 @@ static struct ip_udp_hdr *__NetDefragment(struct ip_udp_hdr *ip, int *lenp) u16 ip_off = ntohs(ip->ip_off);
/* payload starts after IP header, this fragment is in there */ - payload = (struct hole *)(pkt_buff + IP_HDR_SIZE_NO_UDP); + payload = (struct hole *)(pkt_buff + IP_HDR_SIZE); offset8 = (ip_off & IP_OFFS); thisfrag = payload + offset8; start = offset8 * 8; - len = ntohs(ip->ip_len) - IP_HDR_SIZE_NO_UDP; + len = ntohs(ip->ip_len) - IP_HDR_SIZE;
if (start + len > IP_MAXUDP) /* fragment extends too far */ return NULL; @@ -705,7 +705,7 @@ static struct ip_udp_hdr *__NetDefragment(struct ip_udp_hdr *ip, int *lenp) payload[0].prev_hole = 0; first_hole = 0; /* any IP header will work, copy the first we received */ - memcpy(localip, ip, IP_HDR_SIZE_NO_UDP); + memcpy(localip, ip, IP_HDR_SIZE); }
/* @@ -788,12 +788,12 @@ static struct ip_udp_hdr *__NetDefragment(struct ip_udp_hdr *ip, int *lenp) }
/* finally copy this fragment and possibly return whole packet */ - memcpy((uchar *)thisfrag, indata + IP_HDR_SIZE_NO_UDP, len); + memcpy((uchar *)thisfrag, indata + IP_HDR_SIZE, len); if (!done) return NULL;
localip->ip_len = htons(total_len); - *lenp = total_len + IP_HDR_SIZE_NO_UDP; + *lenp = total_len + IP_HDR_SIZE; return localip; }
@@ -983,7 +983,7 @@ NetReceive(uchar *inpkt, int len) if ((ip->ip_hl_v & 0x0f) > 0x05) return; /* Check the Checksum of the header */ - if (!NetCksumOk((uchar *)ip, IP_HDR_SIZE_NO_UDP / 2)) { + if (!NetCksumOk((uchar *)ip, IP_HDR_SIZE / 2)) { puts("checksum bad\n"); return; } @@ -1273,7 +1273,7 @@ void NetSetIP(uchar *xip, IPaddr_t dest, int dport, int sport, int len) ip->udp_dst = htons(dport); ip->udp_len = htons(UDP_HDR_SIZE + len); ip->udp_xsum = 0; - ip->ip_sum = ~NetCksum((uchar *)ip, IP_HDR_SIZE_NO_UDP / 2); + ip->ip_sum = ~NetCksum((uchar *)ip, IP_HDR_SIZE / 2); }
void copy_filename(char *dst, const char *src, int size) diff --git a/net/ping.c b/net/ping.c index 6a2e85d..8542e22 100644 --- a/net/ping.c +++ b/net/ping.c @@ -44,7 +44,7 @@ static int ping_send(void) /* IP_HDR_SIZE / 4 (not including UDP) */ ip->ip_hl_v = 0x45; ip->ip_tos = 0; - ip->ip_len = htons(IP_HDR_SIZE_NO_UDP + 8); + ip->ip_len = htons(IP_HDR_SIZE + 8); ip->ip_id = htons(NetIPID++); ip->ip_off = htons(IP_FLAGS_DFRAG); /* Don't fragment */ ip->ip_ttl = 255; @@ -54,7 +54,7 @@ static int ping_send(void) NetCopyIP((void *)&ip->ip_src, &NetOurIP); /* - "" - */ NetCopyIP((void *)&ip->ip_dst, &NetPingIP); - ip->ip_sum = ~NetCksum((uchar *)ip, IP_HDR_SIZE_NO_UDP / 2); + ip->ip_sum = ~NetCksum((uchar *)ip, IP_HDR_SIZE / 2);
s = &ip->udp_src; /* XXX ICMP starts here */ s[0] = htons(0x0800); /* echo-request, code */ @@ -65,7 +65,7 @@ static int ping_send(void)
/* size of the waiting packet */ NetArpWaitTxPacketSize = - (pkt - NetArpWaitTxPacket) + IP_HDR_SIZE_NO_UDP + 8; + (pkt - NetArpWaitTxPacket) + IP_HDR_SIZE + 8;
/* and do the ARP request */ NetArpWaitTry = 1; @@ -125,12 +125,12 @@ void ping_receive(Ethernet_t *et, struct ip_udp_hdr *ip, int len) NetCopyIP((void *)&ip->ip_dst, &ip->ip_src); NetCopyIP((void *)&ip->ip_src, &NetOurIP); ip->ip_sum = ~NetCksum((uchar *)ip, - IP_HDR_SIZE_NO_UDP >> 1); + IP_HDR_SIZE >> 1);
icmph->type = ICMP_ECHO_REPLY; icmph->checksum = 0; icmph->checksum = ~NetCksum((uchar *)icmph, - (len - IP_HDR_SIZE_NO_UDP) >> 1); + (len - IP_HDR_SIZE) >> 1); (void) eth_send((uchar *)et, ETHER_HDR_SIZE + len); return;

Separate the Ethernet header from the 802 header. Base the size constants on the structs.
Signed-off-by: Joe Hershberger joe.hershberger@ni.com Cc: Joe Hershberger joe.hershberger@gmail.com --- Changes for v2: - Split apart from "Un-typedef variables in net" - Created a new E802_hdr struct and removed 802 members from Ethernet_hdr - Renamed structs from *_t to *_hdr since they are nolonger types - Replaced offsetof(Ethernet_t, et_dsap) with ETHER_HDR_SIZE in arch/powerpc/cpu/mpc8260/ether_fcc.c Changes for v3: - Renamed Ethernet_hdr to ethernet_hdr - Renamed E802_hdr to e802_hdr
arch/powerpc/cpu/mpc8260/ether_fcc.c | 17 ++++++++--------- include/net.h | 18 ++++++++++++++---- net/arp.c | 4 ++-- net/arp.h | 2 +- net/bootp.c | 2 +- net/cdp.c | 4 ++-- net/net.c | 11 ++++++----- net/ping.c | 2 +- net/ping.h | 2 +- 9 files changed, 36 insertions(+), 26 deletions(-)
diff --git a/arch/powerpc/cpu/mpc8260/ether_fcc.c b/arch/powerpc/cpu/mpc8260/ether_fcc.c index 879ec0e..0eb74d4 100644 --- a/arch/powerpc/cpu/mpc8260/ether_fcc.c +++ b/arch/powerpc/cpu/mpc8260/ether_fcc.c @@ -1049,11 +1049,11 @@ eth_loopback_test (void) } else { ushort datlen = bdp->cbd_datlen; - Ethernet_t *ehp; + struct ethernet_hdr *ehp; ushort prot; int ours, tb, n, nbytes;
- ehp = (Ethernet_t *) \ + ehp = (struct ethernet_hdr *) \ &ecp->rxbufs[i][0];
ours = memcmp (ehp->et_src, \ @@ -1063,9 +1063,8 @@ eth_loopback_test (void) tb = prot & 0x8000; n = prot & 0x7fff;
- nbytes = ELBT_BUFSZ - \ - offsetof (Ethernet_t, \ - et_dsap) - \ + nbytes = ELBT_BUFSZ - + ETHER_HDR_SIZE - ELBT_CRCSZ;
/* check the frame is correct */ @@ -1080,10 +1079,10 @@ eth_loopback_test (void) patwords[n]; uint nbb;
- nbb = badbits ( \ - &ehp->et_dsap, \ - nbytes, \ - patword); + nbb = badbits( + ((uchar *)&ehp) + + ETHER_HDR_SIZE, + nbytes, patword);
ecp->rxeacc.badbit += \ nbb; diff --git a/include/net.h b/include/net.h index 7692153..2551516 100644 --- a/include/net.h +++ b/include/net.h @@ -156,7 +156,17 @@ u32 ether_crc(size_t len, unsigned char const *p); /* * Ethernet header */ -typedef struct { + +struct ethernet_hdr { + uchar et_dest[6]; /* Destination node */ + uchar et_src[6]; /* Source node */ + ushort et_protlen; /* Protocol or length */ +}; + +/* Ethernet header size */ +#define ETHER_HDR_SIZE (sizeof(struct ethernet_hdr)) + +struct e802_hdr { uchar et_dest[6]; /* Destination node */ uchar et_src[6]; /* Source node */ ushort et_protlen; /* Protocol or length */ @@ -167,10 +177,10 @@ typedef struct { uchar et_snap2; uchar et_snap3; ushort et_prot; /* 802 protocol */ -} Ethernet_t; +};
-#define ETHER_HDR_SIZE 14 /* Ethernet header size */ -#define E802_HDR_SIZE 22 /* 802 ethernet header size */ +/* 802 ethernet header size */ +#define E802_HDR_SIZE (sizeof(struct e802_hdr))
/* * Ethernet header diff --git a/net/arp.c b/net/arp.c index 03272ad..c3def63 100644 --- a/net/arp.c +++ b/net/arp.c @@ -113,7 +113,7 @@ void ArpTimeoutCheck(void) } }
-void ArpReceive(Ethernet_t *et, struct ip_udp_hdr *ip, int len) +void ArpReceive(struct ethernet_hdr *et, struct ip_udp_hdr *ip, int len) { ARP_t *arp; IPaddr_t tmp; @@ -193,7 +193,7 @@ void ArpReceive(Ethernet_t *et, struct ip_udp_hdr *ip, int len) NetGetHandler()(0, 0, 0, 0, 0); #endif /* modify header, and transmit it */ - memcpy(((Ethernet_t *)NetArpWaitTxPacket)-> + memcpy(((struct ethernet_hdr *)NetArpWaitTxPacket)-> et_dest, NetArpWaitPacketMAC, 6); (void) eth_send(NetArpWaitTxPacket, NetArpWaitTxPacketSize); diff --git a/net/arp.h b/net/arp.h index b59fbdd..b7a1ac9 100644 --- a/net/arp.h +++ b/net/arp.h @@ -25,6 +25,6 @@ extern int NetArpWaitTry; void ArpInit(void); void ArpRequest(void); void ArpTimeoutCheck(void); -void ArpReceive(Ethernet_t *et, struct ip_udp_hdr *ip, int len); +void ArpReceive(struct ethernet_hdr *et, struct ip_udp_hdr *ip, int len);
#endif /* __ARP_H__ */ diff --git a/net/bootp.c b/net/bootp.c index b8d2760..3285250 100644 --- a/net/bootp.c +++ b/net/bootp.c @@ -103,7 +103,7 @@ static void BootpCopyNetParams(struct Bootp_t *bp) NetCopyIP(&tmp_ip, &bp->bp_siaddr); if (tmp_ip != 0) NetCopyIP(&NetServerIP, &bp->bp_siaddr); - memcpy(NetServerEther, ((Ethernet_t *)NetRxPacket)->et_src, 6); + memcpy(NetServerEther, ((struct ethernet_hdr *)NetRxPacket)->et_src, 6); #endif if (strlen(bp->bp_file) > 0) copy_filename(BootFile, bp->bp_file, sizeof(BootFile)); diff --git a/net/cdp.c b/net/cdp.c index 004aae2..63be570 100644 --- a/net/cdp.c +++ b/net/cdp.c @@ -109,7 +109,7 @@ CDPSendTrigger(void) uchar *pkt; ushort *s; ushort *cp; - Ethernet_t *et; + struct ethernet_hdr *et; int len; ushort chksum; #if defined(CONFIG_CDP_DEVICE_ID) || defined(CONFIG_CDP_PORT_ID) || \ @@ -118,7 +118,7 @@ CDPSendTrigger(void) #endif
pkt = NetTxPacket; - et = (Ethernet_t *)pkt; + et = (struct ethernet_hdr *)pkt;
/* NOTE: trigger sent not on any VLAN */
diff --git a/net/net.c b/net/net.c index 2bf5631..33de399 100644 --- a/net/net.c +++ b/net/net.c @@ -823,7 +823,7 @@ static inline struct ip_udp_hdr *NetDefragment(struct ip_udp_hdr *ip, int *lenp) * @parma ip IP packet containing the ICMP */ static void receive_icmp(struct ip_udp_hdr *ip, int len, - IPaddr_t src_ip, Ethernet_t *et) + IPaddr_t src_ip, struct ethernet_hdr *et) { ICMP_t *icmph = (ICMP_t *)&ip->udp_src;
@@ -851,7 +851,7 @@ static void receive_icmp(struct ip_udp_hdr *ip, int len, void NetReceive(uchar *inpkt, int len) { - Ethernet_t *et; + struct ethernet_hdr *et; struct ip_udp_hdr *ip; IPaddr_t tmp; IPaddr_t src_ip; @@ -865,7 +865,7 @@ NetReceive(uchar *inpkt, int len)
NetRxPacket = inpkt; NetRxPacketLen = len; - et = (Ethernet_t *)inpkt; + et = (struct ethernet_hdr *)inpkt;
/* too small packet? */ if (len < ETHER_HDR_SIZE) @@ -895,10 +895,11 @@ NetReceive(uchar *inpkt, int len) debug("packet received\n");
if (x < 1514) { + struct e802_hdr *et802 = (struct e802_hdr *)et; /* * Got a 802 packet. Check the other protocol field. */ - x = ntohs(et->et_prot); + x = ntohs(et802->et_prot);
ip = (struct ip_udp_hdr *)(inpkt + E802_HDR_SIZE); len -= E802_HDR_SIZE; @@ -1218,7 +1219,7 @@ NetEthHdrSize(void) int NetSetEther(uchar *xet, uchar * addr, uint prot) { - Ethernet_t *et = (Ethernet_t *)xet; + struct ethernet_hdr *et = (struct ethernet_hdr *)xet; ushort myvlanid;
myvlanid = ntohs(NetOurVLAN); diff --git a/net/ping.c b/net/ping.c index 8542e22..04a594c 100644 --- a/net/ping.c +++ b/net/ping.c @@ -98,7 +98,7 @@ void ping_start(void) ping_send(); }
-void ping_receive(Ethernet_t *et, struct ip_udp_hdr *ip, int len) +void ping_receive(struct ethernet_hdr *et, struct ip_udp_hdr *ip, int len) { ICMP_t *icmph = (ICMP_t *)&(ip->udp_src); IPaddr_t src_ip; diff --git a/net/ping.h b/net/ping.h index 0a2d7d1..fd8d8d9 100644 --- a/net/ping.h +++ b/net/ping.h @@ -28,7 +28,7 @@ void ping_start(void); * @param ip IP header in the same packet * @param len Packet length */ -void ping_receive(Ethernet_t *et, struct ip_udp_hdr *ip, int len); +void ping_receive(struct ethernet_hdr *et, struct ip_udp_hdr *ip, int len);
#endif /* __PING_H__ */ #endif

Eliminate the typedef and remove capital letters
Signed-off-by: Joe Hershberger joe.hershberger@ni.com Cc: Joe Hershberger joe.hershberger@gmail.com --- Changes for v2: - Split apart from "Un-typedef variables in net" - Renamed structs from *_t to *_hdr since they are nolonger types Changes for v3: - Renamed VLAN_Ethernet_hdr to vlan_ethernet_hdr
include/net.h | 7 ++++--- net/net.c | 6 ++++-- 2 files changed, 8 insertions(+), 5 deletions(-)
diff --git a/include/net.h b/include/net.h index 2551516..69dceb1 100644 --- a/include/net.h +++ b/include/net.h @@ -185,15 +185,16 @@ struct e802_hdr { /* * Ethernet header */ -typedef struct { +struct vlan_ethernet_hdr { uchar vet_dest[6]; /* Destination node */ uchar vet_src[6]; /* Source node */ ushort vet_vlan_type; /* PROT_VLAN */ ushort vet_tag; /* TAG of VLAN */ ushort vet_type; /* protocol type */ -} VLAN_Ethernet_t; +};
-#define VLAN_ETHER_HDR_SIZE 18 /* VLAN Ethernet header size */ +/* VLAN Ethernet header size */ +#define VLAN_ETHER_HDR_SIZE (sizeof(struct vlan_ethernet_hdr))
#define PROT_IP 0x0800 /* IP protocol */ #define PROT_ARP 0x0806 /* IP ARP protocol */ diff --git a/net/net.c b/net/net.c index 33de399..6d34254 100644 --- a/net/net.c +++ b/net/net.c @@ -909,7 +909,8 @@ NetReceive(uchar *inpkt, int len) len -= ETHER_HDR_SIZE;
} else { /* VLAN packet */ - VLAN_Ethernet_t *vet = (VLAN_Ethernet_t *)et; + struct vlan_ethernet_hdr *vet = + (struct vlan_ethernet_hdr *)et;
debug("VLAN packet received\n");
@@ -1232,7 +1233,8 @@ NetSetEther(uchar *xet, uchar * addr, uint prot) et->et_protlen = htons(prot); return ETHER_HDR_SIZE; } else { - VLAN_Ethernet_t *vet = (VLAN_Ethernet_t *)xet; + struct vlan_ethernet_hdr *vet = + (struct vlan_ethernet_hdr *)xet;
vet->vet_vlan_type = htons(PROT_VLAN); vet->vet_tag = htons((0 << 5) | (myvlanid & VLAN_IDMASK));

Remove typedef and lower-case letters
Signed-off-by: Joe Hershberger joe.hershberger@ni.com Cc: Joe Hershberger joe.hershberger@gmail.com --- Changes for v2: - Split apart from "Un-typedef variables in net" - Renamed structs from *_t to *_hdr since they are nolonger types Changes for v3: - Renamed ARP_hdr to arp_hdr
include/net.h | 5 ++--- net/arp.c | 8 ++++---- net/rarp.c | 8 ++++---- 3 files changed, 10 insertions(+), 11 deletions(-)
diff --git a/include/net.h b/include/net.h index 69dceb1..eeea466 100644 --- a/include/net.h +++ b/include/net.h @@ -254,8 +254,7 @@ struct ip_udp_hdr { /* * Address Resolution Protocol (ARP) header. */ -typedef struct -{ +struct arp_hdr { ushort ar_hrd; /* Format of hardware address */ # define ARP_ETHER 1 /* Ethernet hardware address */ ushort ar_pro; /* Format of protocol address */ @@ -280,7 +279,7 @@ typedef struct uchar ar_tha[]; /* Target hardware address */ uchar ar_tpa[]; /* Target protocol address */ #endif /* 0 */ -} ARP_t; +};
#define ARP_HDR_SIZE (8+20) /* Size assuming ethernet */
diff --git a/net/arp.c b/net/arp.c index c3def63..5b16de7 100644 --- a/net/arp.c +++ b/net/arp.c @@ -51,7 +51,7 @@ void ArpInit(void) void ArpRequest(void) { uchar *pkt; - ARP_t *arp; + struct arp_hdr *arp;
debug("ARP broadcast %d\n", NetArpWaitTry);
@@ -59,7 +59,7 @@ void ArpRequest(void)
pkt += NetSetEther(pkt, NetBcastAddr, PROT_ARP);
- arp = (ARP_t *) pkt; + arp = (struct arp_hdr *) pkt;
arp->ar_hrd = htons(ARP_ETHER); arp->ar_pro = htons(PROT_IP); @@ -115,7 +115,7 @@ void ArpTimeoutCheck(void)
void ArpReceive(struct ethernet_hdr *et, struct ip_udp_hdr *ip, int len) { - ARP_t *arp; + struct arp_hdr *arp; IPaddr_t tmp; uchar *pkt;
@@ -130,7 +130,7 @@ void ArpReceive(struct ethernet_hdr *et, struct ip_udp_hdr *ip, int len) */ debug("Got ARP\n");
- arp = (ARP_t *)ip; + arp = (struct arp_hdr *)ip; if (len < ARP_HDR_SIZE) { printf("bad length %d < %d\n", len, ARP_HDR_SIZE); return; diff --git a/net/rarp.c b/net/rarp.c index 1b1f502..8d7582e 100644 --- a/net/rarp.c +++ b/net/rarp.c @@ -43,10 +43,10 @@ int RarpTry; */ void rarp_receive(struct ip_upd_hdr *ip, unsigned len) { - ARP_t *arp; + struct arp_hdr *arp;
debug("Got RARP\n"); - arp = (ARP_t *)ip; + arp = (struct arp_hdr *)ip; if (len < ARP_HDR_SIZE) { printf("bad length %d < %d\n", len, ARP_HDR_SIZE); return; @@ -87,14 +87,14 @@ static void RarpTimeout(void) void RarpRequest(void) { uchar *pkt; - ARP_t *rarp; + struct arp_hdr *rarp;
printf("RARP broadcast %d\n", ++RarpTry); pkt = NetTxPacket;
pkt += NetSetEther(pkt, NetBcastAddr, PROT_RARP);
- rarp = (ARP_t *)pkt; + rarp = (struct arp_hdr *)pkt;
rarp->ar_hrd = htons(ARP_ETHER); rarp->ar_pro = htons(PROT_IP);

Remove typedef and lower-case name
Signed-off-by: Joe Hershberger joe.hershberger@ni.com Cc: Joe Hershberger joe.hershberger@gmail.com --- Changes for v2: - Split apart from "Un-typedef variables in net" - Renamed structs from *_t to *_hdr since they are nolonger types Changes for v3: - Renamed ICMP_hdr to icmp_hdr - Removed extra brackets
include/net.h | 6 ++++-- net/net.c | 2 +- net/ping.c | 2 +- 3 files changed, 6 insertions(+), 4 deletions(-)
diff --git a/include/net.h b/include/net.h index eeea466..fd19c42 100644 --- a/include/net.h +++ b/include/net.h @@ -298,7 +298,7 @@ struct arp_hdr { /* Codes for NOT_REACH */ #define ICMP_NOT_REACH_PORT 3 /* Port unreachable */
-typedef struct icmphdr { +struct icmp_hdr { uchar type; uchar code; ushort checksum; @@ -314,8 +314,10 @@ typedef struct icmphdr { } frag; uchar data[0]; } un; -} ICMP_t; +};
+#define ICMP_HDR_SIZE (sizeof(struct icmp_hdr)) +#define IP_ICMP_HDR_SIZE (IP_HDR_SIZE + ICMP_HDR_SIZE)
/* * Maximum packet size; used to allocate packet storage. diff --git a/net/net.c b/net/net.c index 6d34254..4cdd9cd 100644 --- a/net/net.c +++ b/net/net.c @@ -825,7 +825,7 @@ static inline struct ip_udp_hdr *NetDefragment(struct ip_udp_hdr *ip, int *lenp) static void receive_icmp(struct ip_udp_hdr *ip, int len, IPaddr_t src_ip, struct ethernet_hdr *et) { - ICMP_t *icmph = (ICMP_t *)&ip->udp_src; + struct icmp_hdr *icmph = (struct icmp_hdr *)&ip->udp_src;
switch (icmph->type) { case ICMP_REDIRECT: diff --git a/net/ping.c b/net/ping.c index 04a594c..3102521 100644 --- a/net/ping.c +++ b/net/ping.c @@ -100,7 +100,7 @@ void ping_start(void)
void ping_receive(struct ethernet_hdr *et, struct ip_udp_hdr *ip, int len) { - ICMP_t *icmph = (ICMP_t *)&(ip->udp_src); + struct icmp_hdr *icmph = (struct icmp_hdr *)&ip->udp_src; IPaddr_t src_ip;
switch (icmph->type) {

This name more explicitly claims that it does not include the header size
Signed-off-by: Joe Hershberger joe.hershberger@ni.com Cc: Joe Hershberger joe.hershberger@gmail.com --- Changes for v2: - Split from "Improve variable names and code readability" Changes for v3: - Documented arguments to NetSendUDPPacket()
include/net.h | 12 ++++++++++-- net/net.c | 15 ++++++++------- 2 files changed, 18 insertions(+), 9 deletions(-)
diff --git a/include/net.h b/include/net.h index fd19c42..ff66073 100644 --- a/include/net.h +++ b/include/net.h @@ -466,9 +466,17 @@ extern void NetSetTimeout(ulong, thand_f *);/* Set timeout handler */ /* Transmit "NetTxPacket" */ extern void NetSendPacket(uchar *, int);
-/* Transmit UDP packet, performing ARP request if needed */ +/* + * Transmit UDP packet, performing ARP request if needed + * + * @param ether Raw packet buffer + * @param dest IP address to send the datagram to + * @param dport Destination UDP port + * @param sport Source UDP port + * @param payload_len Length of data after the UDP header + */ extern int NetSendUDPPacket(uchar *ether, IPaddr_t dest, int dport, - int sport, int len); + int sport, int payload_len);
/* Processes a received packet */ extern void NetReceive(uchar *, int); diff --git a/net/net.c b/net/net.c index 4cdd9cd..b8a267c 100644 --- a/net/net.c +++ b/net/net.c @@ -591,8 +591,8 @@ NetSendPacket(uchar *pkt, int len) (void) eth_send(pkt, len); }
-int -NetSendUDPPacket(uchar *ether, IPaddr_t dest, int dport, int sport, int len) +int NetSendUDPPacket(uchar *ether, IPaddr_t dest, int dport, int sport, + int payload_len) { uchar *pkt;
@@ -618,14 +618,14 @@ NetSendUDPPacket(uchar *ether, IPaddr_t dest, int dport, int sport, int len) pkt = NetArpWaitTxPacket; pkt += NetSetEther(pkt, NetArpWaitPacketMAC, PROT_IP);
- NetSetIP(pkt, dest, dport, sport, len); + NetSetIP(pkt, dest, dport, sport, payload_len); memcpy(pkt + IP_UDP_HDR_SIZE, (uchar *)NetTxPacket + (pkt - (uchar *)NetArpWaitTxPacket) + - IP_UDP_HDR_SIZE, len); + IP_UDP_HDR_SIZE, payload_len);
/* size of the waiting packet */ NetArpWaitTxPacketSize = (pkt - NetArpWaitTxPacket) + - IP_UDP_HDR_SIZE + len; + IP_UDP_HDR_SIZE + payload_len;
/* and do the ARP request */ NetArpWaitTry = 1; @@ -638,8 +638,9 @@ NetSendUDPPacket(uchar *ether, IPaddr_t dest, int dport, int sport, int len)
pkt = (uchar *)NetTxPacket; pkt += NetSetEther(pkt, ether, PROT_IP); - NetSetIP(pkt, dest, dport, sport, len); - eth_send(NetTxPacket, (pkt - NetTxPacket) + IP_UDP_HDR_SIZE + len); + NetSetIP(pkt, dest, dport, sport, payload_len); + eth_send(NetTxPacket, (pkt - NetTxPacket) + IP_UDP_HDR_SIZE + + payload_len);
return 0; /* transmitted */ }

Make the comment more accurate about the header including SNAP
Signed-off-by: Joe Hershberger joe.hershberger@ni.com Cc: Joe Hershberger joe.hershberger@gmail.com --- Changes for v2: - Split from "Improve variable names and code readability" - Fixed comment indention Changes for v3: - Added a short commit message
include/net.h | 4 ++-- net/net.c | 3 ++- 2 files changed, 4 insertions(+), 3 deletions(-)
diff --git a/include/net.h b/include/net.h index ff66073..730af4f 100644 --- a/include/net.h +++ b/include/net.h @@ -179,11 +179,11 @@ struct e802_hdr { ushort et_prot; /* 802 protocol */ };
-/* 802 ethernet header size */ +/* 802 + SNAP + ethernet header size */ #define E802_HDR_SIZE (sizeof(struct e802_hdr))
/* - * Ethernet header + * Virtual LAN Ethernet header */ struct vlan_ethernet_hdr { uchar vet_dest[6]; /* Destination node */ diff --git a/net/net.c b/net/net.c index b8a267c..1293688 100644 --- a/net/net.c +++ b/net/net.c @@ -898,7 +898,8 @@ NetReceive(uchar *inpkt, int len) if (x < 1514) { struct e802_hdr *et802 = (struct e802_hdr *)et; /* - * Got a 802 packet. Check the other protocol field. + * Got a 802.2 packet. Check the other protocol field. + * XXX VLAN over 802.2+SNAP not implemented! */ x = ntohs(et802->et_prot);

x is a poor variable name
Signed-off-by: Joe Hershberger joe.hershberger@ni.com Cc: Joe Hershberger joe.hershberger@gmail.com --- Changes for v2: - Split from "Improve variable names and code readability" Changes for v3:
net/net.c | 24 ++++++++++++------------ 1 files changed, 12 insertions(+), 12 deletions(-)
diff --git a/net/net.c b/net/net.c index 1293688..67921c3 100644 --- a/net/net.c +++ b/net/net.c @@ -854,9 +854,9 @@ NetReceive(uchar *inpkt, int len) { struct ethernet_hdr *et; struct ip_udp_hdr *ip; - IPaddr_t tmp; + IPaddr_t dst_ip; IPaddr_t src_ip; - int x; + int eth_proto; #if defined(CONFIG_CMD_CDP) int iscdp; #endif @@ -891,22 +891,22 @@ NetReceive(uchar *inpkt, int len) if (mynvlanid == (ushort)-1) mynvlanid = VLAN_NONE;
- x = ntohs(et->et_protlen); + eth_proto = ntohs(et->et_protlen);
debug("packet received\n");
- if (x < 1514) { + if (eth_proto < 1514) { struct e802_hdr *et802 = (struct e802_hdr *)et; /* * Got a 802.2 packet. Check the other protocol field. * XXX VLAN over 802.2+SNAP not implemented! */ - x = ntohs(et802->et_prot); + eth_proto = ntohs(et802->et_prot);
ip = (struct ip_udp_hdr *)(inpkt + E802_HDR_SIZE); len -= E802_HDR_SIZE;
- } else if (x != PROT_VLAN) { /* normal packet */ + } else if (eth_proto != PROT_VLAN) { /* normal packet */ ip = (struct ip_udp_hdr *)(inpkt + ETHER_HDR_SIZE); len -= ETHER_HDR_SIZE;
@@ -930,13 +930,13 @@ NetReceive(uchar *inpkt, int len)
cti = ntohs(vet->vet_tag); vlanid = cti & VLAN_IDMASK; - x = ntohs(vet->vet_type); + eth_proto = ntohs(vet->vet_type);
ip = (struct ip_udp_hdr *)(inpkt + VLAN_ETHER_HDR_SIZE); len -= VLAN_ETHER_HDR_SIZE; }
- debug("Receive from protocol 0x%x\n", x); + debug("Receive from protocol 0x%x\n", eth_proto);
#if defined(CONFIG_CMD_CDP) if (iscdp) { @@ -953,7 +953,7 @@ NetReceive(uchar *inpkt, int len) return; }
- switch (x) { + switch (eth_proto) {
case PROT_ARP: ArpReceive(et, ip, len); @@ -992,10 +992,10 @@ NetReceive(uchar *inpkt, int len) return; } /* If it is not for us, ignore it */ - tmp = NetReadIP(&ip->ip_dst); - if (NetOurIP && tmp != NetOurIP && tmp != 0xFFFFFFFF) { + dst_ip = NetReadIP(&ip->ip_dst); + if (NetOurIP && dst_ip != NetOurIP && dst_ip != 0xFFFFFFFF) { #ifdef CONFIG_MCAST_TFTP - if (Mcast_addr != tmp) + if (Mcast_addr != dst_ip) #endif return; }

This is not called as a handler, so don't name it that way
Signed-off-by: Joe Hershberger joe.hershberger@ni.com Cc: Joe Hershberger joe.hershberger@gmail.com --- Changes for v2: - Split from "Improve variable names and code readability" Changes for v3: - Added a function comment - Renamed CDPReceive to cdp_receive
net/cdp.c | 3 +-- net/cdp.h | 3 ++- net/net.c | 2 +- 3 files changed, 4 insertions(+), 4 deletions(-)
diff --git a/net/cdp.c b/net/cdp.c index 63be570..9bec441 100644 --- a/net/cdp.c +++ b/net/cdp.c @@ -245,8 +245,7 @@ CDPDummyHandler(uchar *pkt, unsigned dest, IPaddr_t sip, unsigned src, /* nothing */ }
-void -CDPHandler(const uchar *pkt, unsigned len) +void cdp_receive(const uchar *pkt, unsigned len) { const uchar *t; const ushort *ss; diff --git a/net/cdp.h b/net/cdp.h index 93bc461..ec7315a 100644 --- a/net/cdp.h +++ b/net/cdp.h @@ -14,7 +14,8 @@ #define __CDP_H__
void CDPStart(void); -void CDPHandler(const uchar *pkt, unsigned len); +/* Process a received CDP packet */ +void cdp_receive(const uchar *pkt, unsigned len);
#endif /* __CDP_H__ */ #endif diff --git a/net/net.c b/net/net.c index 67921c3..f6d294e 100644 --- a/net/net.c +++ b/net/net.c @@ -940,7 +940,7 @@ NetReceive(uchar *inpkt, int len)
#if defined(CONFIG_CMD_CDP) if (iscdp) { - CDPHandler((uchar *)ip, len); + cdp_receive((uchar *)ip, len); return; } #endif

Clearer constant name. Also remove related BOOTP_SIZE which was unused and doesn't take into account VLAN packets.
Signed-off-by: Joe Hershberger joe.hershberger@ni.com Cc: Joe Hershberger joe.hershberger@gmail.com --- Changes for v2: - Split from "Improve variable names and code readability" Changes for v3:
net/bootp.c | 10 +++++----- net/bootp.h | 7 +++---- 2 files changed, 8 insertions(+), 9 deletions(-)
diff --git a/net/bootp.c b/net/bootp.c index 3285250..009369b 100644 --- a/net/bootp.c +++ b/net/bootp.c @@ -71,7 +71,7 @@ static int BootpCheckPkt(uchar *pkt, unsigned dest, unsigned src, unsigned len)
if (dest != PORT_BOOTPC || src != PORT_BOOTPS) retval = -1; - else if (len < sizeof(struct Bootp_t) - OPT_SIZE) + else if (len < sizeof(struct Bootp_t) - OPT_FIELD_SIZE) retval = -2; else if (bp->bp_op != OP_BOOTREQUEST && bp->bp_op != OP_BOOTREPLY && @@ -368,8 +368,8 @@ static int DhcpExtended(u8 *e, int message_type, IPaddr_t ServerID,
*e++ = 57; /* Maximum DHCP Message Size */ *e++ = 2; - *e++ = (576 - 312 + OPT_SIZE) >> 8; - *e++ = (576 - 312 + OPT_SIZE) & 0xff; + *e++ = (576 - 312 + OPT_FIELD_SIZE) >> 8; + *e++ = (576 - 312 + OPT_FIELD_SIZE) & 0xff;
if (ServerID) { int tmp = ntohl(ServerID); @@ -519,8 +519,8 @@ static int BootpExtended(u8 *e)
*e++ = 57; /* Maximum DHCP Message Size */ *e++ = 2; - *e++ = (576 - 312 + OPT_SIZE) >> 16; - *e++ = (576 - 312 + OPT_SIZE) & 0xff; + *e++ = (576 - 312 + OPT_FIELD_SIZE) >> 16; + *e++ = (576 - 312 + OPT_FIELD_SIZE) & 0xff; #endif
#if defined(CONFIG_BOOTP_SUBNETMASK) diff --git a/net/bootp.h b/net/bootp.h index 1cf9a02..ecbcc4d 100644 --- a/net/bootp.h +++ b/net/bootp.h @@ -20,13 +20,13 @@ */ #if defined(CONFIG_CMD_DHCP) /* Minimum DHCP Options size per RFC2131 - results in 576 byte pkt */ -#define OPT_SIZE 312 +#define OPT_FIELD_SIZE 312 #if defined(CONFIG_BOOTP_VENDOREX) extern u8 *dhcp_vendorex_prep(u8 *e); /*rtn new e after add own opts. */ extern u8 *dhcp_vendorex_proc(u8 *e); /*rtn next e if mine,else NULL */ #endif #else -#define OPT_SIZE 64 +#define OPT_FIELD_SIZE 64 #endif
struct Bootp_t { @@ -48,11 +48,10 @@ struct Bootp_t { uchar bp_chaddr[16]; /* Client hardware address */ char bp_sname[64]; /* Server host name */ char bp_file[128]; /* Boot file name */ - char bp_vend[OPT_SIZE]; /* Vendor information */ + char bp_vend[OPT_FIELD_SIZE]; /* Vendor information */ };
#define BOOTP_HDR_SIZE sizeof(struct Bootp_t) -#define BOOTP_SIZE (ETHER_HDR_SIZE + IP_UDP_HDR_SIZE + BOOTP_HDR_SIZE)
/**********************************************************************/ /*

Easier to find when alphabetized
Signed-off-by: Joe Hershberger joe.hershberger@ni.com Cc: Joe Hershberger joe.hershberger@gmail.com --- Changes for v2: - Split from "Improve variable names and code readability" Changes for v3: - Slight adjustment to order as recommended by Simon
net/net.c | 24 ++++++++++++------------ 1 files changed, 12 insertions(+), 12 deletions(-)
diff --git a/net/net.c b/net/net.c index f6d294e..de5352c 100644 --- a/net/net.c +++ b/net/net.c @@ -75,27 +75,27 @@
#include <common.h> -#include <watchdog.h> #include <command.h> -#include <linux/compiler.h> #include <net.h> -#include "arp.h" -#include "bootp.h" -#include "tftp.h" -#include "rarp.h" -#include "nfs.h" -#ifdef CONFIG_STATUS_LED -#include <status_led.h> +#if defined(CONFIG_STATUS_LED) #include <miiphy.h> +#include <status_led.h> #endif -#if defined(CONFIG_CMD_SNTP) -#include "sntp.h" -#endif +#include <watchdog.h> +#include <linux/compiler.h> +#include "arp.h" +#include "bootp.h" #include "cdp.h" #if defined(CONFIG_CMD_DNS) #include "dns.h" #endif +#include "nfs.h" #include "ping.h" +#include "rarp.h" +#if defined(CONFIG_CMD_SNTP) +#include "sntp.h" +#endif +#include "tftp.h"
DECLARE_GLOBAL_DATA_PTR;

Renamed for clarity
Signed-off-by: Joe Hershberger joe.hershberger@ni.com Cc: Joe Hershberger joe.hershberger@gmail.com --- Changes for v2: - Split from "Improve variable names and code readability" Changes for v3:
net/arp.c | 6 +++--- 1 files changed, 3 insertions(+), 3 deletions(-)
diff --git a/net/arp.c b/net/arp.c index 5b16de7..d36a642 100644 --- a/net/arp.c +++ b/net/arp.c @@ -116,7 +116,7 @@ void ArpTimeoutCheck(void) void ArpReceive(struct ethernet_hdr *et, struct ip_udp_hdr *ip, int len) { struct arp_hdr *arp; - IPaddr_t tmp; + IPaddr_t reply_ip_addr; uchar *pkt;
/* @@ -178,10 +178,10 @@ void ArpReceive(struct ethernet_hdr *et, struct ip_udp_hdr *ip, int len) } #endif
- tmp = NetReadIP(&arp->ar_data[6]); + reply_ip_addr = NetReadIP(&arp->ar_data[6]);
/* matched waiting packet's address */ - if (tmp == NetArpWaitReplyIP) { + if (reply_ip_addr == NetArpWaitReplyIP) { debug("Got ARP REPLY, set eth addr (%pM)\n", arp->ar_data);

Use field names and sizes when accessing ARP packets
Signed-off-by: Joe Hershberger joe.hershberger@ni.com Cc: Joe Hershberger joe.hershberger@gmail.com --- Changes for v2: - Split from "Improve variable names and code readability" Changes for v3:
include/net.h | 6 ++++++ net/arp.c | 34 +++++++++++++++++----------------- 2 files changed, 23 insertions(+), 17 deletions(-)
diff --git a/include/net.h b/include/net.h index 730af4f..3bf0806 100644 --- a/include/net.h +++ b/include/net.h @@ -259,7 +259,9 @@ struct arp_hdr { # define ARP_ETHER 1 /* Ethernet hardware address */ ushort ar_pro; /* Format of protocol address */ uchar ar_hln; /* Length of hardware address */ +# define ARP_HLEN 6 uchar ar_pln; /* Length of protocol address */ +# define ARP_PLEN 4 ushort ar_op; /* Operation */ # define ARPOP_REQUEST 1 /* Request to resolve address */ # define ARPOP_REPLY 2 /* Response to previous request */ @@ -273,6 +275,10 @@ struct arp_hdr { * specific hardware/protocol combinations. */ uchar ar_data[0]; +#define ar_sha ar_data[0] +#define ar_spa ar_data[ARP_HLEN] +#define ar_tha ar_data[ARP_HLEN + ARP_PLEN] +#define ar_tpa ar_data[ARP_HLEN + ARP_PLEN + ARP_HLEN] #if 0 uchar ar_sha[]; /* Sender hardware address */ uchar ar_spa[]; /* Sender protocol address */ diff --git a/net/arp.c b/net/arp.c index d36a642..5ed875c 100644 --- a/net/arp.c +++ b/net/arp.c @@ -63,16 +63,16 @@ void ArpRequest(void)
arp->ar_hrd = htons(ARP_ETHER); arp->ar_pro = htons(PROT_IP); - arp->ar_hln = 6; - arp->ar_pln = 4; + arp->ar_hln = ARP_HLEN; + arp->ar_pln = ARP_PLEN; arp->ar_op = htons(ARPOP_REQUEST);
/* source ET addr */ - memcpy(&arp->ar_data[0], NetOurEther, 6); + memcpy(&arp->ar_sha, NetOurEther, ARP_HLEN); /* source IP addr */ - NetWriteIP((uchar *) &arp->ar_data[6], NetOurIP); + NetWriteIP(&arp->ar_spa, NetOurIP); /* dest ET addr = 0 */ - memset(&arp->ar_data[10], '\0', 6); + memset(&arp->ar_tha, 0, ARP_HLEN); if ((NetArpWaitPacketIP & NetOurSubnetMask) != (NetOurIP & NetOurSubnetMask)) { if (NetOurGatewayIP == 0) { @@ -85,7 +85,7 @@ void ArpRequest(void) NetArpWaitReplyIP = NetArpWaitPacketIP; }
- NetWriteIP((uchar *) &arp->ar_data[16], NetArpWaitReplyIP); + NetWriteIP(&arp->ar_tpa, NetArpWaitReplyIP); (void) eth_send(NetTxPacket, (pkt - NetTxPacket) + ARP_HDR_SIZE); }
@@ -139,15 +139,15 @@ void ArpReceive(struct ethernet_hdr *et, struct ip_udp_hdr *ip, int len) return; if (ntohs(arp->ar_pro) != PROT_IP) return; - if (arp->ar_hln != 6) + if (arp->ar_hln != ARP_HLEN) return; - if (arp->ar_pln != 4) + if (arp->ar_pln != ARP_PLEN) return;
if (NetOurIP == 0) return;
- if (NetReadIP(&arp->ar_data[16]) != NetOurIP) + if (NetReadIP(&arp->ar_tpa) != NetOurIP) return;
switch (ntohs(arp->ar_op)) { @@ -157,10 +157,10 @@ void ArpReceive(struct ethernet_hdr *et, struct ip_udp_hdr *ip, int len) pkt = (uchar *)et; pkt += NetSetEther(pkt, et->et_src, PROT_ARP); arp->ar_op = htons(ARPOP_REPLY); - memcpy(&arp->ar_data[10], &arp->ar_data[0], 6); - NetCopyIP(&arp->ar_data[16], &arp->ar_data[6]); - memcpy(&arp->ar_data[0], NetOurEther, 6); - NetCopyIP(&arp->ar_data[6], &NetOurIP); + memcpy(&arp->ar_tha, &arp->ar_sha, ARP_HLEN); + NetCopyIP(&arp->ar_tpa, &arp->ar_spa); + memcpy(&arp->ar_sha, NetOurEther, ARP_HLEN); + NetCopyIP(&arp->ar_spa, &NetOurIP); (void) eth_send((uchar *)et, (pkt - (uchar *)et) + ARP_HDR_SIZE); return; @@ -173,12 +173,12 @@ void ArpReceive(struct ethernet_hdr *et, struct ip_udp_hdr *ip, int len) #ifdef CONFIG_KEEP_SERVERADDR if (NetServerIP == NetArpWaitPacketIP) { char buf[20]; - sprintf(buf, "%pM", arp->ar_data); + sprintf(buf, "%pM", arp->ar_sha); setenv("serveraddr", buf); } #endif
- reply_ip_addr = NetReadIP(&arp->ar_data[6]); + reply_ip_addr = NetReadIP(&arp->ar_spa);
/* matched waiting packet's address */ if (reply_ip_addr == NetArpWaitReplyIP) { @@ -187,14 +187,14 @@ void ArpReceive(struct ethernet_hdr *et, struct ip_udp_hdr *ip, int len)
/* save address for later use */ memcpy(NetArpWaitPacketMAC, - &arp->ar_data[0], 6); + &arp->ar_sha, ARP_HLEN);
#ifdef CONFIG_NETCONSOLE NetGetHandler()(0, 0, 0, 0, 0); #endif /* modify header, and transmit it */ memcpy(((struct ethernet_hdr *)NetArpWaitTxPacket)-> - et_dest, NetArpWaitPacketMAC, 6); + et_dest, NetArpWaitPacketMAC, ARP_HLEN); (void) eth_send(NetArpWaitTxPacket, NetArpWaitTxPacketSize);

Some boards claim to need ping, but don't support net. That makes no sense, and causes problems now that ping is in a separate file.
Signed-off-by: Joe Hershberger joe.hershberger@ni.com Cc: Joe Hershberger joe.hershberger@gmail.com Cc: Simon Glass sjg@chromium.org Cc: Mike Frysinger vapier@gentoo.org --- Changes for v2: - Separated from "Move PING out of net.c"
include/configs/am3517_crane.h | 2 +- include/configs/am3517_evm.h | 2 +- include/configs/origen.h | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-)
diff --git a/include/configs/am3517_crane.h b/include/configs/am3517_crane.h index b0dd2f0..848e4ae 100644 --- a/include/configs/am3517_crane.h +++ b/include/configs/am3517_crane.h @@ -146,7 +146,7 @@ #define CONFIG_CMD_MMC /* MMC support */ #define CONFIG_CMD_NAND /* NAND support */ #define CONFIG_CMD_DHCP -#define CONFIG_CMD_PING +#undef CONFIG_CMD_PING
#undef CONFIG_CMD_FLASH /* flinfo, erase, protect */ #undef CONFIG_CMD_FPGA /* FPGA configuration Support */ diff --git a/include/configs/am3517_evm.h b/include/configs/am3517_evm.h index b5f75d1..3b4cb07 100644 --- a/include/configs/am3517_evm.h +++ b/include/configs/am3517_evm.h @@ -145,7 +145,7 @@ #define CONFIG_CMD_MMC /* MMC support */ #define CONFIG_CMD_NAND /* NAND support */ #define CONFIG_CMD_DHCP -#define CONFIG_CMD_PING +#undef CONFIG_CMD_PING
#undef CONFIG_CMD_FLASH /* flinfo, erase, protect */ #undef CONFIG_CMD_FPGA /* FPGA configuration Support */ diff --git a/include/configs/origen.h b/include/configs/origen.h index 8ede825..985b16d 100644 --- a/include/configs/origen.h +++ b/include/configs/origen.h @@ -82,7 +82,7 @@ /* Command definition*/ #include <config_cmd_default.h>
-#define CONFIG_CMD_PING +#undef CONFIG_CMD_PING #define CONFIG_CMD_ELF #define CONFIG_CMD_DHCP #define CONFIG_CMD_MMC

Hi Joe,
On Tue, Mar 27, 2012 at 4:42 PM, Joe Hershberger joe.hershberger@ni.com wrote:
Some boards claim to need ping, but don't support net. That makes no sense, and causes problems now that ping is in a separate file.
Signed-off-by: Joe Hershberger joe.hershberger@ni.com Cc: Joe Hershberger joe.hershberger@gmail.com Cc: Simon Glass sjg@chromium.org Cc: Mike Frysinger vapier@gentoo.org
Acked-by: Simon Glass sjg@chromium.org
See question below too.
Changes for v2: - Separated from "Move PING out of net.c"
include/configs/am3517_crane.h | 2 +- include/configs/am3517_evm.h | 2 +- include/configs/origen.h | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-)
diff --git a/include/configs/am3517_crane.h b/include/configs/am3517_crane.h index b0dd2f0..848e4ae 100644 --- a/include/configs/am3517_crane.h +++ b/include/configs/am3517_crane.h @@ -146,7 +146,7 @@ #define CONFIG_CMD_MMC /* MMC support */ #define CONFIG_CMD_NAND /* NAND support */ #define CONFIG_CMD_DHCP
Should you remove the DHCP also?
-#define CONFIG_CMD_PING +#undef CONFIG_CMD_PING
#undef CONFIG_CMD_FLASH /* flinfo, erase, protect */ #undef CONFIG_CMD_FPGA /* FPGA configuration Support */ diff --git a/include/configs/am3517_evm.h b/include/configs/am3517_evm.h index b5f75d1..3b4cb07 100644 --- a/include/configs/am3517_evm.h +++ b/include/configs/am3517_evm.h @@ -145,7 +145,7 @@ #define CONFIG_CMD_MMC /* MMC support */ #define CONFIG_CMD_NAND /* NAND support */ #define CONFIG_CMD_DHCP -#define CONFIG_CMD_PING +#undef CONFIG_CMD_PING
#undef CONFIG_CMD_FLASH /* flinfo, erase, protect */ #undef CONFIG_CMD_FPGA /* FPGA configuration Support */ diff --git a/include/configs/origen.h b/include/configs/origen.h index 8ede825..985b16d 100644 --- a/include/configs/origen.h +++ b/include/configs/origen.h @@ -82,7 +82,7 @@ /* Command definition*/ #include <config_cmd_default.h>
-#define CONFIG_CMD_PING +#undef CONFIG_CMD_PING #define CONFIG_CMD_ELF #define CONFIG_CMD_DHCP #define CONFIG_CMD_MMC -- 1.6.0.2
Regards, Simon

Hi Simon,
On Fri, Apr 20, 2012 at 9:47 PM, Simon Glass sjg@chromium.org wrote:
Hi Joe,
On Tue, Mar 27, 2012 at 4:42 PM, Joe Hershberger joe.hershberger@ni.com wrote:
diff --git a/include/configs/am3517_crane.h b/include/configs/am3517_crane.h index b0dd2f0..848e4ae 100644 --- a/include/configs/am3517_crane.h +++ b/include/configs/am3517_crane.h @@ -146,7 +146,7 @@ #define CONFIG_CMD_MMC /* MMC support */ #define CONFIG_CMD_NAND /* NAND support */ #define CONFIG_CMD_DHCP
Should you remove the DHCP also?
I was interested in changing only what actually caused a build failure in a board I have no way to test, though it would be safe to change as well.
-Joe

Some copy/pasted drivers redefine functions defined in include/net.h. Remove these so that the definitions can be changed without error.
Signed-off-by: Joe Hershberger joe.hershberger@ni.com Cc: Joe Hershberger joe.hershberger@gmail.com Cc: Simon Glass sjg@chromium.org Cc: Mike Frysinger vapier@gentoo.org --- Changes for v2: - Split from "Remove volatile from all of net except the eth driver interface"
board/Marvell/db64360/mv_eth.c | 2 -- board/Marvell/db64460/mv_eth.c | 2 -- board/esd/cpci750/mv_eth.c | 2 -- board/prodrive/p3mx/mv_eth.c | 2 -- 4 files changed, 0 insertions(+), 8 deletions(-)
diff --git a/board/Marvell/db64360/mv_eth.c b/board/Marvell/db64360/mv_eth.c index e89ea43..1841355 100644 --- a/board/Marvell/db64360/mv_eth.c +++ b/board/Marvell/db64360/mv_eth.c @@ -95,8 +95,6 @@ int mv64360_eth_xmit (struct eth_device *, volatile void *packet, int length); #ifndef UPDATE_STATS_BY_SOFTWARE static void mv64360_eth_print_stat (struct eth_device *dev); #endif -/* Processes a received packet */ -extern void NetReceive (volatile uchar *, int);
extern unsigned int INTERNAL_REG_BASE_ADDR;
diff --git a/board/Marvell/db64460/mv_eth.c b/board/Marvell/db64460/mv_eth.c index a763d79..da48fa8 100644 --- a/board/Marvell/db64460/mv_eth.c +++ b/board/Marvell/db64460/mv_eth.c @@ -95,8 +95,6 @@ int mv64460_eth_xmit (struct eth_device *, volatile void *packet, int length); #ifndef UPDATE_STATS_BY_SOFTWARE static void mv64460_eth_print_stat (struct eth_device *dev); #endif -/* Processes a received packet */ -extern void NetReceive (volatile uchar *, int);
extern unsigned int INTERNAL_REG_BASE_ADDR;
diff --git a/board/esd/cpci750/mv_eth.c b/board/esd/cpci750/mv_eth.c index a3bde81..9cccbdb 100644 --- a/board/esd/cpci750/mv_eth.c +++ b/board/esd/cpci750/mv_eth.c @@ -95,8 +95,6 @@ int mv64360_eth_xmit (struct eth_device *, volatile void *packet, int length); #ifndef UPDATE_STATS_BY_SOFTWARE static void mv64360_eth_print_stat (struct eth_device *dev); #endif -/* Processes a received packet */ -extern void NetReceive (volatile uchar *, int);
extern unsigned int INTERNAL_REG_BASE_ADDR;
diff --git a/board/prodrive/p3mx/mv_eth.c b/board/prodrive/p3mx/mv_eth.c index 9e3213b..976cb0b 100644 --- a/board/prodrive/p3mx/mv_eth.c +++ b/board/prodrive/p3mx/mv_eth.c @@ -109,8 +109,6 @@ int phy_setup_aneg (char *devname, unsigned char addr); #ifndef UPDATE_STATS_BY_SOFTWARE static void mv64460_eth_print_stat (struct eth_device *dev); #endif -/* Processes a received packet */ -extern void NetReceive (volatile uchar *, int);
extern unsigned int INTERNAL_REG_BASE_ADDR;

On Tue, Mar 27, 2012 at 4:42 PM, Joe Hershberger joe.hershberger@ni.com wrote:
Some copy/pasted drivers redefine functions defined in include/net.h. Remove these so that the definitions can be changed without error.
Signed-off-by: Joe Hershberger joe.hershberger@ni.com Cc: Joe Hershberger joe.hershberger@gmail.com Cc: Simon Glass sjg@chromium.org Cc: Mike Frysinger vapier@gentoo.org
Acked-by: Simon Glass sjg@chromium.org
(I assume MAKEALL throws no errors due to these)
Changes for v2: - Split from "Remove volatile from all of net except the eth driver interface"
board/Marvell/db64360/mv_eth.c | 2 -- board/Marvell/db64460/mv_eth.c | 2 -- board/esd/cpci750/mv_eth.c | 2 -- board/prodrive/p3mx/mv_eth.c | 2 -- 4 files changed, 0 insertions(+), 8 deletions(-)
diff --git a/board/Marvell/db64360/mv_eth.c b/board/Marvell/db64360/mv_eth.c index e89ea43..1841355 100644 --- a/board/Marvell/db64360/mv_eth.c +++ b/board/Marvell/db64360/mv_eth.c @@ -95,8 +95,6 @@ int mv64360_eth_xmit (struct eth_device *, volatile void *packet, int length); #ifndef UPDATE_STATS_BY_SOFTWARE static void mv64360_eth_print_stat (struct eth_device *dev); #endif -/* Processes a received packet */ -extern void NetReceive (volatile uchar *, int);
extern unsigned int INTERNAL_REG_BASE_ADDR;
diff --git a/board/Marvell/db64460/mv_eth.c b/board/Marvell/db64460/mv_eth.c index a763d79..da48fa8 100644 --- a/board/Marvell/db64460/mv_eth.c +++ b/board/Marvell/db64460/mv_eth.c @@ -95,8 +95,6 @@ int mv64460_eth_xmit (struct eth_device *, volatile void *packet, int length); #ifndef UPDATE_STATS_BY_SOFTWARE static void mv64460_eth_print_stat (struct eth_device *dev); #endif -/* Processes a received packet */ -extern void NetReceive (volatile uchar *, int);
extern unsigned int INTERNAL_REG_BASE_ADDR;
diff --git a/board/esd/cpci750/mv_eth.c b/board/esd/cpci750/mv_eth.c index a3bde81..9cccbdb 100644 --- a/board/esd/cpci750/mv_eth.c +++ b/board/esd/cpci750/mv_eth.c @@ -95,8 +95,6 @@ int mv64360_eth_xmit (struct eth_device *, volatile void *packet, int length); #ifndef UPDATE_STATS_BY_SOFTWARE static void mv64360_eth_print_stat (struct eth_device *dev); #endif -/* Processes a received packet */ -extern void NetReceive (volatile uchar *, int);
extern unsigned int INTERNAL_REG_BASE_ADDR;
diff --git a/board/prodrive/p3mx/mv_eth.c b/board/prodrive/p3mx/mv_eth.c index 9e3213b..976cb0b 100644 --- a/board/prodrive/p3mx/mv_eth.c +++ b/board/prodrive/p3mx/mv_eth.c @@ -109,8 +109,6 @@ int phy_setup_aneg (char *devname, unsigned char addr); #ifndef UPDATE_STATS_BY_SOFTWARE static void mv64460_eth_print_stat (struct eth_device *dev); #endif -/* Processes a received packet */ -extern void NetReceive (volatile uchar *, int);
extern unsigned int INTERNAL_REG_BASE_ADDR;
-- 1.6.0.2

Make the MAC-seeded random number generator available to /net in general. MAC-seeded rand will be needed by link-local as well, so give it an interface.
Signed-off-by: Joe Hershberger joe.hershberger@ni.com Cc: Joe Hershberger joe.hershberger@gmail.com Cc: Simon Glass sjg@chromium.org Cc: Mike Frysinger vapier@gentoo.org --- net/Makefile | 1 + net/bootp.c | 69 +++++++++++-------------------------------------------- net/bootp.h | 3 -- net/net_rand.c | 68 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ net/net_rand.h | 17 +++++++++++++ 5 files changed, 100 insertions(+), 58 deletions(-) create mode 100644 net/net_rand.c create mode 100644 net/net_rand.h
diff --git a/net/Makefile b/net/Makefile index 0544f6b..5901046 100644 --- a/net/Makefile +++ b/net/Makefile @@ -31,6 +31,7 @@ COBJS-$(CONFIG_CMD_NET) += bootp.o COBJS-$(CONFIG_CMD_DNS) += dns.o COBJS-$(CONFIG_CMD_NET) += eth.o COBJS-$(CONFIG_CMD_NET) += net.o +COBJS-$(CONFIG_BOOTP_RANDOM_DELAY) += net_rand.o COBJS-$(CONFIG_CMD_NFS) += nfs.o COBJS-$(CONFIG_CMD_RARP) += rarp.o COBJS-$(CONFIG_CMD_SNTP) += sntp.o diff --git a/net/bootp.c b/net/bootp.c index d0a7da2..5949e77 100644 --- a/net/bootp.c +++ b/net/bootp.c @@ -12,6 +12,9 @@ #include <command.h> #include <net.h> #include "bootp.h" +#ifdef CONFIG_BOOTP_RANDOM_DELAY +#include "net_rand.h" +#endif #include "tftp.h" #include "nfs.h" #ifdef CONFIG_STATUS_LED @@ -37,9 +40,6 @@
ulong BootpID; int BootpTry; -#ifdef CONFIG_BOOTP_RANDOM_DELAY -ulong seed1, seed2; -#endif
#if defined(CONFIG_CMD_DHCP) dhcp_state_t dhcp_state = INIT; @@ -584,6 +584,9 @@ BootpRequest(void) uchar *pkt, *iphdr; struct Bootp_t *bp; int ext_len, pktlen, iplen; +#ifdef CONFIG_BOOTP_RANDOM_DELAY + ulong i, rand_ms; +#endif
bootstage_mark_name(BOOTSTAGE_ID_BOOTP_START, "bootp_start"); #if defined(CONFIG_CMD_DHCP) @@ -591,60 +594,16 @@ BootpRequest(void) #endif
#ifdef CONFIG_BOOTP_RANDOM_DELAY /* Random BOOTP delay */ - unsigned char bi_enetaddr[6]; - int reg; - ulong tst1, tst2, sum, m_mask, m_value = 0; - - if (BootpTry == 0) { - /* get our mac */ - eth_getenv_enetaddr("ethaddr", bi_enetaddr); - - debug("BootpRequest => Our Mac: "); - for (reg = 0; reg < 6; reg++) - debug("%x%c", bi_enetaddr[reg], reg == 5 ? '\n' : ':'); - - /* Mac-Manipulation 2 get seed1 */ - tst1 = 0; - tst2 = 0; - for (reg = 2; reg < 6; reg++) { - tst1 = tst1 << 8; - tst1 = tst1 | bi_enetaddr[reg]; - } - for (reg = 0; reg < 2; reg++) { - tst2 = tst2 | bi_enetaddr[reg]; - tst2 = tst2 << 8; - } - - seed1 = tst1^tst2; + if (BootpTry == 0) + srand_mac();
- /* Mirror seed1*/ - m_mask = 0x1; - for (reg = 1; reg <= 32; reg++) { - m_value |= (m_mask & seed1); - seed1 = seed1 >> 1; - m_value = m_value << 1; - } - seed1 = m_value; - seed2 = 0xB78D0945; - } - - /* Random Number Generator */ - for (reg = 0; reg <= 0; reg++) { - sum = seed1 + seed2; - if (sum < seed1 || sum < seed2) - sum++; - seed2 = seed1; - seed1 = sum; - - if (BootpTry <= 2) { /* Start with max 1024 * 1ms */ - sum = sum >> (22-BootpTry); - } else { /*After 3rd BOOTP request max 8192 * 1ms */ - sum = sum >> 19; - } - } + if (BootpTry <= 2) /* Start with max 1024 * 1ms */ + rand_ms = rand() >> (22-BootpTry); + else /* After 3rd BOOTP request max 8192 * 1ms */ + rand_ms = rand() >> 19;
- printf("Random delay: %ld ms...\n", sum); - for (reg = 0; reg < sum; reg++) + printf("Random delay: %ld ms...\n", rand_ms); + for (i = 0; i < rand_ms; i++) udelay(1000); /*Wait 1ms*/
#endif /* CONFIG_BOOTP_RANDOM_DELAY */ diff --git a/net/bootp.h b/net/bootp.h index ce73734..bf4e875 100644 --- a/net/bootp.h +++ b/net/bootp.h @@ -63,9 +63,6 @@ struct Bootp_t { extern ulong BootpID; /* ID of cur BOOTP request */ extern char BootFile[128]; /* Boot file name */ extern int BootpTry; -#ifdef CONFIG_BOOTP_RANDOM_DELAY -extern ulong seed1, seed2; /* seed for random BOOTP delay */ -#endif
/* Send a BOOTP request */ diff --git a/net/net_rand.c b/net/net_rand.c new file mode 100644 index 0000000..744e2f9 --- /dev/null +++ b/net/net_rand.c @@ -0,0 +1,68 @@ +/* + * Based on LiMon - BOOTP. + * + * Copyright 1994, 1995, 2000 Neil Russell. + * (See License) + * Copyright 2000 Roland Borde + * Copyright 2000 Paolo Scaffardi + * Copyright 2000-2004 Wolfgang Denk, wd@denx.de + */ + +#include <common.h> +#include <net.h> +#include "net_rand.h" + +static ulong seed1, seed2; + +void srand_mac(void) +{ + ulong tst1, tst2, m_mask; + ulong m_value = 0; + int reg; + unsigned char bi_enetaddr[6]; + + /* get our mac */ + eth_getenv_enetaddr("ethaddr", bi_enetaddr); + + debug("BootpRequest => Our Mac: "); + for (reg = 0; reg < 6; reg++) + debug("%x%c", bi_enetaddr[reg], reg == 5 ? '\n' : ':'); + + /* Mac-Manipulation 2 get seed1 */ + tst1 = 0; + tst2 = 0; + for (reg = 2; reg < 6; reg++) { + tst1 = tst1 << 8; + tst1 = tst1 | bi_enetaddr[reg]; + } + for (reg = 0; reg < 2; reg++) { + tst2 = tst2 | bi_enetaddr[reg]; + tst2 = tst2 << 8; + } + + seed1 = tst1^tst2; + + /* Mirror seed1*/ + m_mask = 0x1; + for (reg = 1; reg <= 32; reg++) { + m_value |= (m_mask & seed1); + seed1 = seed1 >> 1; + m_value = m_value << 1; + } + seed1 = m_value; + seed2 = 0xB78D0945; +} + +unsigned long rand(void) +{ + ulong sum; + + /* Random Number Generator */ + sum = seed1 + seed2; + if (sum < seed1 || sum < seed2) + sum++; + seed2 = seed1; + seed1 = sum; + + return sum; +} diff --git a/net/net_rand.h b/net/net_rand.h new file mode 100644 index 0000000..8569f93 --- /dev/null +++ b/net/net_rand.h @@ -0,0 +1,17 @@ +/* + * Copied from LiMon - BOOTP. + * + * Copyright 1994, 1995, 2000 Neil Russell. + * (See License) + * Copyright 2000 Paolo Scaffardi + */ + +#ifndef __NET_RAND_H__ +#define __NET_RAND_H__ + +#define RAND_MAX 0xFFFFFFFF + +void srand_mac(void); +unsigned long rand(void); + +#endif /* __NET_RAND_H__ */

Hi Joe,
On Tue, Mar 27, 2012 at 4:42 PM, Joe Hershberger joe.hershberger@ni.com wrote:
Make the MAC-seeded random number generator available to /net in general. MAC-seeded rand will be needed by link-local as well, so give it an interface.
Signed-off-by: Joe Hershberger joe.hershberger@ni.com Cc: Joe Hershberger joe.hershberger@gmail.com Cc: Simon Glass sjg@chromium.org Cc: Mike Frysinger vapier@gentoo.org
net/Makefile | 1 + net/bootp.c | 69 +++++++++++-------------------------------------------- net/bootp.h | 3 -- net/net_rand.c | 68 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ net/net_rand.h | 17 +++++++++++++ 5 files changed, 100 insertions(+), 58 deletions(-) create mode 100644 net/net_rand.c create mode 100644 net/net_rand.h
diff --git a/net/Makefile b/net/Makefile index 0544f6b..5901046 100644 --- a/net/Makefile +++ b/net/Makefile @@ -31,6 +31,7 @@ COBJS-$(CONFIG_CMD_NET) += bootp.o COBJS-$(CONFIG_CMD_DNS) += dns.o COBJS-$(CONFIG_CMD_NET) += eth.o COBJS-$(CONFIG_CMD_NET) += net.o +COBJS-$(CONFIG_BOOTP_RANDOM_DELAY) += net_rand.o COBJS-$(CONFIG_CMD_NFS) += nfs.o COBJS-$(CONFIG_CMD_RARP) += rarp.o COBJS-$(CONFIG_CMD_SNTP) += sntp.o diff --git a/net/bootp.c b/net/bootp.c index d0a7da2..5949e77 100644 --- a/net/bootp.c +++ b/net/bootp.c @@ -12,6 +12,9 @@ #include <command.h> #include <net.h> #include "bootp.h" +#ifdef CONFIG_BOOTP_RANDOM_DELAY +#include "net_rand.h" +#endif
How about just always including it?
#include "tftp.h" #include "nfs.h" #ifdef CONFIG_STATUS_LED @@ -37,9 +40,6 @@
ulong BootpID; int BootpTry; -#ifdef CONFIG_BOOTP_RANDOM_DELAY -ulong seed1, seed2; -#endif
#if defined(CONFIG_CMD_DHCP) dhcp_state_t dhcp_state = INIT; @@ -584,6 +584,9 @@ BootpRequest(void) uchar *pkt, *iphdr; struct Bootp_t *bp; int ext_len, pktlen, iplen; +#ifdef CONFIG_BOOTP_RANDOM_DELAY
- ulong i, rand_ms;
+#endif
bootstage_mark_name(BOOTSTAGE_ID_BOOTP_START, "bootp_start"); #if defined(CONFIG_CMD_DHCP) @@ -591,60 +594,16 @@ BootpRequest(void) #endif
#ifdef CONFIG_BOOTP_RANDOM_DELAY /* Random BOOTP delay */
- unsigned char bi_enetaddr[6];
- int reg;
- ulong tst1, tst2, sum, m_mask, m_value = 0;
- if (BootpTry == 0) {
- /* get our mac */
- eth_getenv_enetaddr("ethaddr", bi_enetaddr);
- debug("BootpRequest => Our Mac: ");
- for (reg = 0; reg < 6; reg++)
- debug("%x%c", bi_enetaddr[reg], reg == 5 ? '\n' : ':');
- /* Mac-Manipulation 2 get seed1 */
- tst1 = 0;
- tst2 = 0;
- for (reg = 2; reg < 6; reg++) {
- tst1 = tst1 << 8;
- tst1 = tst1 | bi_enetaddr[reg];
- }
- for (reg = 0; reg < 2; reg++) {
- tst2 = tst2 | bi_enetaddr[reg];
- tst2 = tst2 << 8;
- }
- seed1 = tst1^tst2;
- if (BootpTry == 0)
- srand_mac();
- /* Mirror seed1*/
- m_mask = 0x1;
- for (reg = 1; reg <= 32; reg++) {
- m_value |= (m_mask & seed1);
- seed1 = seed1 >> 1;
- m_value = m_value << 1;
- }
- seed1 = m_value;
- seed2 = 0xB78D0945;
- }
- /* Random Number Generator */
- for (reg = 0; reg <= 0; reg++) {
- sum = seed1 + seed2;
- if (sum < seed1 || sum < seed2)
- sum++;
- seed2 = seed1;
- seed1 = sum;
- if (BootpTry <= 2) { /* Start with max 1024 * 1ms */
- sum = sum >> (22-BootpTry);
- } else { /*After 3rd BOOTP request max 8192 * 1ms */
- sum = sum >> 19;
- }
- }
- if (BootpTry <= 2) /* Start with max 1024 * 1ms */
- rand_ms = rand() >> (22-BootpTry);
Spaces around operators
- else /* After 3rd BOOTP request max 8192 * 1ms */
- rand_ms = rand() >> 19;
- printf("Random delay: %ld ms...\n", sum);
- for (reg = 0; reg < sum; reg++)
- printf("Random delay: %ld ms...\n", rand_ms);
- for (i = 0; i < rand_ms; i++)
udelay(1000); /*Wait 1ms*/
#endif /* CONFIG_BOOTP_RANDOM_DELAY */ diff --git a/net/bootp.h b/net/bootp.h index ce73734..bf4e875 100644 --- a/net/bootp.h +++ b/net/bootp.h @@ -63,9 +63,6 @@ struct Bootp_t { extern ulong BootpID; /* ID of cur BOOTP request */ extern char BootFile[128]; /* Boot file name */ extern int BootpTry; -#ifdef CONFIG_BOOTP_RANDOM_DELAY -extern ulong seed1, seed2; /* seed for random BOOTP delay */ -#endif
/* Send a BOOTP request */ diff --git a/net/net_rand.c b/net/net_rand.c new file mode 100644 index 0000000..744e2f9 --- /dev/null +++ b/net/net_rand.c @@ -0,0 +1,68 @@ +/*
- Based on LiMon - BOOTP.
- Copyright 1994, 1995, 2000 Neil Russell.
- (See License)
- Copyright 2000 Roland Borde
- Copyright 2000 Paolo Scaffardi
- Copyright 2000-2004 Wolfgang Denk, wd@denx.de
- */
+#include <common.h> +#include <net.h> +#include "net_rand.h"
+static ulong seed1, seed2;
+void srand_mac(void)
Perhaps pass in the place to put the random mac, rather than using a global?
+{
- ulong tst1, tst2, m_mask;
- ulong m_value = 0;
- int reg;
- unsigned char bi_enetaddr[6];
- /* get our mac */
- eth_getenv_enetaddr("ethaddr", bi_enetaddr);
- debug("BootpRequest => Our Mac: ");
- for (reg = 0; reg < 6; reg++)
- debug("%x%c", bi_enetaddr[reg], reg == 5 ? '\n' : ':');
- /* Mac-Manipulation 2 get seed1 */
- tst1 = 0;
- tst2 = 0;
- for (reg = 2; reg < 6; reg++) {
- tst1 = tst1 << 8;
- tst1 = tst1 | bi_enetaddr[reg];
- }
- for (reg = 0; reg < 2; reg++) {
- tst2 = tst2 | bi_enetaddr[reg];
- tst2 = tst2 << 8;
- }
- seed1 = tst1^tst2;
- /* Mirror seed1*/
- m_mask = 0x1;
- for (reg = 1; reg <= 32; reg++) {
- m_value |= (m_mask & seed1);
- seed1 = seed1 >> 1;
- m_value = m_value << 1;
- }
- seed1 = m_value;
- seed2 = 0xB78D0945;
+}
+unsigned long rand(void) +{
- ulong sum;
- /* Random Number Generator */
- sum = seed1 + seed2;
- if (sum < seed1 || sum < seed2)
- sum++;
- seed2 = seed1;
- seed1 = sum;
- return sum;
+} diff --git a/net/net_rand.h b/net/net_rand.h new file mode 100644 index 0000000..8569f93 --- /dev/null +++ b/net/net_rand.h @@ -0,0 +1,17 @@ +/*
- Copied from LiMon - BOOTP.
- Copyright 1994, 1995, 2000 Neil Russell.
- (See License)
- Copyright 2000 Paolo Scaffardi
- */
+#ifndef __NET_RAND_H__ +#define __NET_RAND_H__
+#define RAND_MAX 0xFFFFFFFF
Do we want lower case hex? Not sure.
+void srand_mac(void); +unsigned long rand(void);
Function comments are nice :-)
+#endif /* __NET_RAND_H__ */
1.6.0.2
Regards, Simon

Hi Simon,
On Fri, Apr 20, 2012 at 9:54 PM, Simon Glass sjg@chromium.org wrote:
Hi Joe,
On Tue, Mar 27, 2012 at 4:42 PM, Joe Hershberger joe.hershberger@ni.com wrote:
diff --git a/net/bootp.c b/net/bootp.c index d0a7da2..5949e77 100644 --- a/net/bootp.c +++ b/net/bootp.c @@ -12,6 +12,9 @@ #include <command.h> #include <net.h> #include "bootp.h" +#ifdef CONFIG_BOOTP_RANDOM_DELAY +#include "net_rand.h" +#endif
How about just always including it?
OK.
#include "tftp.h" #include "nfs.h" #ifdef CONFIG_STATUS_LED diff --git a/net/net_rand.c b/net/net_rand.c new file mode 100644 index 0000000..744e2f9 --- /dev/null +++ b/net/net_rand.c @@ -0,0 +1,68 @@ +/*
- Based on LiMon - BOOTP.
- Copyright 1994, 1995, 2000 Neil Russell.
- (See License)
- Copyright 2000 Roland Borde
- Copyright 2000 Paolo Scaffardi
- Copyright 2000-2004 Wolfgang Denk, wd@denx.de
- */
+#include <common.h> +#include <net.h> +#include "net_rand.h"
+static ulong seed1, seed2;
+void srand_mac(void)
Perhaps pass in the place to put the random mac, rather than using a global?
In this case it is using the env to read the mac. It is not creating a random mac. The point is to create a random number based on the MAC so that the numbers vary from device to device in the absence of other entropy. In the link-local case it also should lead to the same device choosing the same address each time by default and most likely different than other devices.
In the future I could make it take an index for which MAC address to seed with, but for now, using the first is best suited to its application.
+{
- ulong tst1, tst2, m_mask;
- ulong m_value = 0;
- int reg;
- unsigned char bi_enetaddr[6];
- /* get our mac */
- eth_getenv_enetaddr("ethaddr", bi_enetaddr);
diff --git a/net/net_rand.h b/net/net_rand.h new file mode 100644 index 0000000..8569f93 --- /dev/null +++ b/net/net_rand.h @@ -0,0 +1,17 @@ +/*
- Copied from LiMon - BOOTP.
- Copyright 1994, 1995, 2000 Neil Russell.
- (See License)
- Copyright 2000 Paolo Scaffardi
- */
+#ifndef __NET_RAND_H__ +#define __NET_RAND_H__
+#define RAND_MAX 0xFFFFFFFF
Do we want lower case hex? Not sure.
OK
+void srand_mac(void); +unsigned long rand(void);
Function comments are nice :-)
OK
+#endif /* __NET_RAND_H__ */
Thanks, -Joe

Signed-off-by: Joe Hershberger joe.hershberger@ni.com Cc: Joe Hershberger joe.hershberger@gmail.com Cc: Simon Glass sjg@chromium.org Cc: Mike Frysinger vapier@gentoo.org --- Changes for v2: - Moved is_cdp_packet to a separate patch - Fixed blank newline at the end of cdp.h - Pushed #ifdef CONFIG_CMD_CDP into header
include/net.h | 8 +- net/Makefile | 1 + net/cdp.c | 374 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++ net/cdp.h | 20 +++ net/net.c | 366 +------------------------------------------------------- 5 files changed, 399 insertions(+), 370 deletions(-) create mode 100644 net/cdp.c create mode 100644 net/cdp.h
diff --git a/include/net.h b/include/net.h index e39d88b..54f05bb 100644 --- a/include/net.h +++ b/include/net.h @@ -355,9 +355,7 @@ extern uchar NetEtherNullAddr[6]; extern ushort NetOurVLAN; /* Our VLAN */ extern ushort NetOurNativeVLAN; /* Our Native VLAN */
-extern uchar NetCDPAddr[6]; /* Ethernet CDP address */ -extern ushort CDPNativeVLAN; /* CDP returned native VLAN */ -extern ushort CDPApplianceVLAN; /* CDP returned appliance VLAN */ +extern const uchar NetCDPAddr[6]; /* Ethernet CDP address */
extern int NetState; /* Network loop state */ #define NETLOOP_CONTINUE 1 @@ -386,8 +384,8 @@ extern IPaddr_t NetPingIP; /* the ip address to ping */
#if defined(CONFIG_CMD_CDP) /* when CDP completes these hold the return values */ -extern ushort CDPNativeVLAN; -extern ushort CDPApplianceVLAN; +extern ushort CDPNativeVLAN; /* CDP returned native VLAN */ +extern ushort CDPApplianceVLAN; /* CDP returned appliance VLAN */ #endif
#if defined(CONFIG_CMD_SNTP) diff --git a/net/Makefile b/net/Makefile index 5901046..b350bfc 100644 --- a/net/Makefile +++ b/net/Makefile @@ -28,6 +28,7 @@ include $(TOPDIR)/config.mk LIB = $(obj)libnet.o
COBJS-$(CONFIG_CMD_NET) += bootp.o +COBJS-$(CONFIG_CMD_CDP) += cdp.o COBJS-$(CONFIG_CMD_DNS) += dns.o COBJS-$(CONFIG_CMD_NET) += eth.o COBJS-$(CONFIG_CMD_NET) += net.o diff --git a/net/cdp.c b/net/cdp.c new file mode 100644 index 0000000..028be82 --- /dev/null +++ b/net/cdp.c @@ -0,0 +1,374 @@ +/* + * Copied from Linux Monitor (LiMon) - Networking. + * + * Copyright 1994 - 2000 Neil Russell. + * (See License) + * Copyright 2000 Roland Borde + * Copyright 2000 Paolo Scaffardi + * Copyright 2000-2002 Wolfgang Denk, wd@denx.de + */ + +#include <common.h> +#include <net.h> +#if defined(CONFIG_CDP_VERSION) +#include <timestamp.h> +#endif + +#include "cdp.h" + +/* Ethernet bcast address */ +const uchar NetCDPAddr[6] = { 0x01, 0x00, 0x0c, 0xcc, 0xcc, 0xcc }; + +#define CDP_DEVICE_ID_TLV 0x0001 +#define CDP_ADDRESS_TLV 0x0002 +#define CDP_PORT_ID_TLV 0x0003 +#define CDP_CAPABILITIES_TLV 0x0004 +#define CDP_VERSION_TLV 0x0005 +#define CDP_PLATFORM_TLV 0x0006 +#define CDP_NATIVE_VLAN_TLV 0x000a +#define CDP_APPLIANCE_VLAN_TLV 0x000e +#define CDP_TRIGGER_TLV 0x000f +#define CDP_POWER_CONSUMPTION_TLV 0x0010 +#define CDP_SYSNAME_TLV 0x0014 +#define CDP_SYSOBJECT_TLV 0x0015 +#define CDP_MANAGEMENT_ADDRESS_TLV 0x0016 + +#define CDP_TIMEOUT 250UL /* one packet every 250ms */ + +static int CDPSeq; +static int CDPOK; + +ushort CDPNativeVLAN; +ushort CDPApplianceVLAN; + +static const uchar CDP_SNAP_hdr[8] = { 0xAA, 0xAA, 0x03, 0x00, 0x00, 0x0C, 0x20, + 0x00 }; + +static ushort +CDP_compute_csum(const uchar *buff, ushort len) +{ + ushort csum; + int odd; + ulong result = 0; + ushort leftover; + ushort *p; + + if (len > 0) { + odd = 1 & (ulong)buff; + if (odd) { + result = *buff << 8; + len--; + buff++; + } + while (len > 1) { + p = (ushort *)buff; + result += *p++; + buff = (uchar *)p; + if (result & 0x80000000) + result = (result & 0xFFFF) + (result >> 16); + len -= 2; + } + if (len) { + leftover = (signed short)(*(const signed char *)buff); + /* CISCO SUCKS big time! (and blows too): + * CDP uses the IP checksum algorithm with a twist; + * for the last byte it *sign* extends and sums. + */ + result = (result & 0xffff0000) | + ((result + leftover) & 0x0000ffff); + } + while (result >> 16) + result = (result & 0xFFFF) + (result >> 16); + + if (odd) + result = ((result >> 8) & 0xff) | + ((result & 0xff) << 8); + } + + /* add up 16-bit and 17-bit words for 17+c bits */ + result = (result & 0xffff) + (result >> 16); + /* add up 16-bit and 2-bit for 16+c bit */ + result = (result & 0xffff) + (result >> 16); + /* add up carry.. */ + result = (result & 0xffff) + (result >> 16); + + /* negate */ + csum = ~(ushort)result; + + /* run time endian detection */ + if (csum != htons(csum)) /* little endian */ + csum = htons(csum); + + return csum; +} + +static int +CDPSendTrigger(void) +{ + uchar *pkt; + ushort *s; + ushort *cp; + Ethernet_t *et; + int len; + ushort chksum; +#if defined(CONFIG_CDP_DEVICE_ID) || defined(CONFIG_CDP_PORT_ID) || \ + defined(CONFIG_CDP_VERSION) || defined(CONFIG_CDP_PLATFORM) + char buf[32]; +#endif + + pkt = NetTxPacket; + et = (Ethernet_t *)pkt; + + /* NOTE: trigger sent not on any VLAN */ + + /* form ethernet header */ + memcpy(et->et_dest, NetCDPAddr, 6); + memcpy(et->et_src, NetOurEther, 6); + + pkt += ETHER_HDR_SIZE; + + /* SNAP header */ + memcpy((uchar *)pkt, CDP_SNAP_hdr, sizeof(CDP_SNAP_hdr)); + pkt += sizeof(CDP_SNAP_hdr); + + /* CDP header */ + *pkt++ = 0x02; /* CDP version 2 */ + *pkt++ = 180; /* TTL */ + s = (ushort *)pkt; + cp = s; + /* checksum (0 for later calculation) */ + *s++ = htons(0); + + /* CDP fields */ +#ifdef CONFIG_CDP_DEVICE_ID + *s++ = htons(CDP_DEVICE_ID_TLV); + *s++ = htons(CONFIG_CDP_DEVICE_ID); + sprintf(buf, CONFIG_CDP_DEVICE_ID_PREFIX "%pm", NetOurEther); + memcpy((uchar *)s, buf, 16); + s += 16 / 2; +#endif + +#ifdef CONFIG_CDP_PORT_ID + *s++ = htons(CDP_PORT_ID_TLV); + memset(buf, 0, sizeof(buf)); + sprintf(buf, CONFIG_CDP_PORT_ID, eth_get_dev_index()); + len = strlen(buf); + if (len & 1) /* make it even */ + len++; + *s++ = htons(len + 4); + memcpy((uchar *)s, buf, len); + s += len / 2; +#endif + +#ifdef CONFIG_CDP_CAPABILITIES + *s++ = htons(CDP_CAPABILITIES_TLV); + *s++ = htons(8); + *(ulong *)s = htonl(CONFIG_CDP_CAPABILITIES); + s += 2; +#endif + +#ifdef CONFIG_CDP_VERSION + *s++ = htons(CDP_VERSION_TLV); + memset(buf, 0, sizeof(buf)); + strcpy(buf, CONFIG_CDP_VERSION); + len = strlen(buf); + if (len & 1) /* make it even */ + len++; + *s++ = htons(len + 4); + memcpy((uchar *)s, buf, len); + s += len / 2; +#endif + +#ifdef CONFIG_CDP_PLATFORM + *s++ = htons(CDP_PLATFORM_TLV); + memset(buf, 0, sizeof(buf)); + strcpy(buf, CONFIG_CDP_PLATFORM); + len = strlen(buf); + if (len & 1) /* make it even */ + len++; + *s++ = htons(len + 4); + memcpy((uchar *)s, buf, len); + s += len / 2; +#endif + +#ifdef CONFIG_CDP_TRIGGER + *s++ = htons(CDP_TRIGGER_TLV); + *s++ = htons(8); + *(ulong *)s = htonl(CONFIG_CDP_TRIGGER); + s += 2; +#endif + +#ifdef CONFIG_CDP_POWER_CONSUMPTION + *s++ = htons(CDP_POWER_CONSUMPTION_TLV); + *s++ = htons(6); + *s++ = htons(CONFIG_CDP_POWER_CONSUMPTION); +#endif + + /* length of ethernet packet */ + len = (uchar *)s - ((uchar *)NetTxPacket + ETHER_HDR_SIZE); + et->et_protlen = htons(len); + + len = ETHER_HDR_SIZE + sizeof(CDP_SNAP_hdr); + chksum = CDP_compute_csum((uchar *)NetTxPacket + len, + (uchar *)s - (NetTxPacket + len)); + if (chksum == 0) + chksum = 0xFFFF; + *cp = htons(chksum); + + (void) eth_send(NetTxPacket, (uchar *)s - NetTxPacket); + return 0; +} + +static void +CDPTimeout(void) +{ + CDPSeq++; + + if (CDPSeq < 3) { + NetSetTimeout(CDP_TIMEOUT, CDPTimeout); + CDPSendTrigger(); + return; + } + + /* if not OK try again */ + if (!CDPOK) + NetStartAgain(); + else + NetState = NETLOOP_SUCCESS; +} + +static void +CDPDummyHandler(uchar *pkt, unsigned dest, IPaddr_t sip, unsigned src, + unsigned len) +{ + /* nothing */ +} + +void +CDPHandler(const uchar *pkt, unsigned len) +{ + const uchar *t; + const ushort *ss; + ushort type, tlen; + ushort vlan, nvlan; + + /* minimum size? */ + if (len < sizeof(CDP_SNAP_hdr) + 4) + goto pkt_short; + + /* check for valid CDP SNAP header */ + if (memcmp(pkt, CDP_SNAP_hdr, sizeof(CDP_SNAP_hdr)) != 0) + return; + + pkt += sizeof(CDP_SNAP_hdr); + len -= sizeof(CDP_SNAP_hdr); + + /* Version of CDP protocol must be >= 2 and TTL != 0 */ + if (pkt[0] < 0x02 || pkt[1] == 0) + return; + + /* + * if version is greater than 0x02 maybe we'll have a problem; + * output a warning + */ + if (pkt[0] != 0x02) + printf("**WARNING: CDP packet received with a protocol version " + "%d > 2\n", pkt[0] & 0xff); + + if (CDP_compute_csum(pkt, len) != 0) + return; + + pkt += 4; + len -= 4; + + vlan = htons(-1); + nvlan = htons(-1); + while (len > 0) { + if (len < 4) + goto pkt_short; + + ss = (const ushort *)pkt; + type = ntohs(ss[0]); + tlen = ntohs(ss[1]); + if (tlen > len) + goto pkt_short; + + pkt += tlen; + len -= tlen; + + ss += 2; /* point ss to the data of the TLV */ + tlen -= 4; + + switch (type) { + case CDP_DEVICE_ID_TLV: + break; + case CDP_ADDRESS_TLV: + break; + case CDP_PORT_ID_TLV: + break; + case CDP_CAPABILITIES_TLV: + break; + case CDP_VERSION_TLV: + break; + case CDP_PLATFORM_TLV: + break; + case CDP_NATIVE_VLAN_TLV: + nvlan = *ss; + break; + case CDP_APPLIANCE_VLAN_TLV: + t = (const uchar *)ss; + while (tlen > 0) { + if (tlen < 3) + goto pkt_short; + + ss = (const ushort *)(t + 1); + +#ifdef CONFIG_CDP_APPLIANCE_VLAN_TYPE + if (t[0] == CONFIG_CDP_APPLIANCE_VLAN_TYPE) + vlan = *ss; +#else + /* XXX will this work; dunno */ + vlan = ntohs(*ss); +#endif + t += 3; tlen -= 3; + } + break; + case CDP_TRIGGER_TLV: + break; + case CDP_POWER_CONSUMPTION_TLV: + break; + case CDP_SYSNAME_TLV: + break; + case CDP_SYSOBJECT_TLV: + break; + case CDP_MANAGEMENT_ADDRESS_TLV: + break; + } + } + + CDPApplianceVLAN = vlan; + CDPNativeVLAN = nvlan; + + CDPOK = 1; + return; + + pkt_short: + printf("** CDP packet is too short\n"); + return; +} + +void +CDPStart(void) +{ + printf("Using %s device\n", eth_get_name()); + CDPSeq = 0; + CDPOK = 0; + + CDPNativeVLAN = htons(-1); + CDPApplianceVLAN = htons(-1); + + NetSetTimeout(CDP_TIMEOUT, CDPTimeout); + NetSetHandler(CDPDummyHandler); + + CDPSendTrigger(); +} diff --git a/net/cdp.h b/net/cdp.h new file mode 100644 index 0000000..93bc461 --- /dev/null +++ b/net/cdp.h @@ -0,0 +1,20 @@ +/* + * Copied from Linux Monitor (LiMon) - Networking. + * + * Copyright 1994 - 2000 Neil Russell. + * (See License) + * Copyright 2000 Roland Borde + * Copyright 2000 Paolo Scaffardi + * Copyright 2000-2002 Wolfgang Denk, wd@denx.de + */ + +#if defined(CONFIG_CMD_CDP) + +#ifndef __CDP_H__ +#define __CDP_H__ + +void CDPStart(void); +void CDPHandler(const uchar *pkt, unsigned len); + +#endif /* __CDP_H__ */ +#endif diff --git a/net/net.c b/net/net.c index 1435245..b5b072a 100644 --- a/net/net.c +++ b/net/net.c @@ -92,9 +92,7 @@ #if defined(CONFIG_CMD_SNTP) #include "sntp.h" #endif -#if defined(CONFIG_CDP_VERSION) -#include <timestamp.h> -#endif +#include "cdp.h" #if defined(CONFIG_CMD_DNS) #include "dns.h" #endif @@ -164,10 +162,6 @@ uchar NetEtherNullAddr[6]; #ifdef CONFIG_API void (*push_packet)(void *, int len) = 0; #endif -#if defined(CONFIG_CMD_CDP) -/* Ethernet bcast address */ -uchar NetCDPAddr[6] = { 0x01, 0x00, 0x0c, 0xcc, 0xcc, 0xcc }; -#endif /* Network loop state */ int NetState; /* Tried all network devices */ @@ -193,10 +187,6 @@ IPaddr_t NetPingIP; static void PingStart(void); #endif
-#if defined(CONFIG_CMD_CDP) -static void CDPStart(void); -#endif - #if defined(CONFIG_CMD_SNTP) /* NTP server IP address */ IPaddr_t NetNtpServerIP; @@ -843,360 +833,6 @@ static void PingStart(void) } #endif
-#if defined(CONFIG_CMD_CDP) - -#define CDP_DEVICE_ID_TLV 0x0001 -#define CDP_ADDRESS_TLV 0x0002 -#define CDP_PORT_ID_TLV 0x0003 -#define CDP_CAPABILITIES_TLV 0x0004 -#define CDP_VERSION_TLV 0x0005 -#define CDP_PLATFORM_TLV 0x0006 -#define CDP_NATIVE_VLAN_TLV 0x000a -#define CDP_APPLIANCE_VLAN_TLV 0x000e -#define CDP_TRIGGER_TLV 0x000f -#define CDP_POWER_CONSUMPTION_TLV 0x0010 -#define CDP_SYSNAME_TLV 0x0014 -#define CDP_SYSOBJECT_TLV 0x0015 -#define CDP_MANAGEMENT_ADDRESS_TLV 0x0016 - -#define CDP_TIMEOUT 250UL /* one packet every 250ms */ - -static int CDPSeq; -static int CDPOK; - -ushort CDPNativeVLAN; -ushort CDPApplianceVLAN; - -static const uchar CDP_SNAP_hdr[8] = { 0xAA, 0xAA, 0x03, 0x00, 0x00, 0x0C, 0x20, - 0x00 }; - -static ushort CDP_compute_csum(const uchar *buff, ushort len) -{ - ushort csum; - int odd; - ulong result = 0; - ushort leftover; - ushort *p; - - if (len > 0) { - odd = 1 & (ulong)buff; - if (odd) { - result = *buff << 8; - len--; - buff++; - } - while (len > 1) { - p = (ushort *)buff; - result += *p++; - buff = (uchar *)p; - if (result & 0x80000000) - result = (result & 0xFFFF) + (result >> 16); - len -= 2; - } - if (len) { - leftover = (signed short)(*(const signed char *)buff); - /* CISCO SUCKS big time! (and blows too): - * CDP uses the IP checksum algorithm with a twist; - * for the last byte it *sign* extends and sums. - */ - result = (result & 0xffff0000) | - ((result + leftover) & 0x0000ffff); - } - while (result >> 16) - result = (result & 0xFFFF) + (result >> 16); - - if (odd) - result = ((result >> 8) & 0xff) | - ((result & 0xff) << 8); - } - - /* add up 16-bit and 17-bit words for 17+c bits */ - result = (result & 0xffff) + (result >> 16); - /* add up 16-bit and 2-bit for 16+c bit */ - result = (result & 0xffff) + (result >> 16); - /* add up carry.. */ - result = (result & 0xffff) + (result >> 16); - - /* negate */ - csum = ~(ushort)result; - - /* run time endian detection */ - if (csum != htons(csum)) /* little endian */ - csum = htons(csum); - - return csum; -} - -int CDPSendTrigger(void) -{ - uchar *pkt; - ushort *s; - ushort *cp; - Ethernet_t *et; - int len; - ushort chksum; -#if defined(CONFIG_CDP_DEVICE_ID) || defined(CONFIG_CDP_PORT_ID) || \ - defined(CONFIG_CDP_VERSION) || defined(CONFIG_CDP_PLATFORM) - char buf[32]; -#endif - - pkt = NetTxPacket; - et = (Ethernet_t *)pkt; - - /* NOTE: trigger sent not on any VLAN */ - - /* form ethernet header */ - memcpy(et->et_dest, NetCDPAddr, 6); - memcpy(et->et_src, NetOurEther, 6); - - pkt += ETHER_HDR_SIZE; - - /* SNAP header */ - memcpy((uchar *)pkt, CDP_SNAP_hdr, sizeof(CDP_SNAP_hdr)); - pkt += sizeof(CDP_SNAP_hdr); - - /* CDP header */ - *pkt++ = 0x02; /* CDP version 2 */ - *pkt++ = 180; /* TTL */ - s = (ushort *)pkt; - cp = s; - /* checksum (0 for later calculation) */ - *s++ = htons(0); - - /* CDP fields */ -#ifdef CONFIG_CDP_DEVICE_ID - *s++ = htons(CDP_DEVICE_ID_TLV); - *s++ = htons(CONFIG_CDP_DEVICE_ID); - sprintf(buf, CONFIG_CDP_DEVICE_ID_PREFIX "%pm", NetOurEther); - memcpy((uchar *)s, buf, 16); - s += 16 / 2; -#endif - -#ifdef CONFIG_CDP_PORT_ID - *s++ = htons(CDP_PORT_ID_TLV); - memset(buf, 0, sizeof(buf)); - sprintf(buf, CONFIG_CDP_PORT_ID, eth_get_dev_index()); - len = strlen(buf); - if (len & 1) /* make it even */ - len++; - *s++ = htons(len + 4); - memcpy((uchar *)s, buf, len); - s += len / 2; -#endif - -#ifdef CONFIG_CDP_CAPABILITIES - *s++ = htons(CDP_CAPABILITIES_TLV); - *s++ = htons(8); - *(ulong *)s = htonl(CONFIG_CDP_CAPABILITIES); - s += 2; -#endif - -#ifdef CONFIG_CDP_VERSION - *s++ = htons(CDP_VERSION_TLV); - memset(buf, 0, sizeof(buf)); - strcpy(buf, CONFIG_CDP_VERSION); - len = strlen(buf); - if (len & 1) /* make it even */ - len++; - *s++ = htons(len + 4); - memcpy((uchar *)s, buf, len); - s += len / 2; -#endif - -#ifdef CONFIG_CDP_PLATFORM - *s++ = htons(CDP_PLATFORM_TLV); - memset(buf, 0, sizeof(buf)); - strcpy(buf, CONFIG_CDP_PLATFORM); - len = strlen(buf); - if (len & 1) /* make it even */ - len++; - *s++ = htons(len + 4); - memcpy((uchar *)s, buf, len); - s += len / 2; -#endif - -#ifdef CONFIG_CDP_TRIGGER - *s++ = htons(CDP_TRIGGER_TLV); - *s++ = htons(8); - *(ulong *)s = htonl(CONFIG_CDP_TRIGGER); - s += 2; -#endif - -#ifdef CONFIG_CDP_POWER_CONSUMPTION - *s++ = htons(CDP_POWER_CONSUMPTION_TLV); - *s++ = htons(6); - *s++ = htons(CONFIG_CDP_POWER_CONSUMPTION); -#endif - - /* length of ethernet packet */ - len = (uchar *)s - ((uchar *)NetTxPacket + ETHER_HDR_SIZE); - et->et_protlen = htons(len); - - len = ETHER_HDR_SIZE + sizeof(CDP_SNAP_hdr); - chksum = CDP_compute_csum((uchar *)NetTxPacket + len, - (uchar *)s - (NetTxPacket + len)); - if (chksum == 0) - chksum = 0xFFFF; - *cp = htons(chksum); - - (void) eth_send(NetTxPacket, (uchar *)s - NetTxPacket); - return 0; -} - -static void -CDPTimeout(void) -{ - CDPSeq++; - - if (CDPSeq < 3) { - NetSetTimeout(CDP_TIMEOUT, CDPTimeout); - CDPSendTrigger(); - return; - } - - /* if not OK try again */ - if (!CDPOK) - NetStartAgain(); - else - NetState = NETLOOP_SUCCESS; -} - -static void -CDPDummyHandler(uchar *pkt, unsigned dest, IPaddr_t sip, unsigned src, - unsigned len) -{ - /* nothing */ -} - -static void -CDPHandler(const uchar *pkt, unsigned len) -{ - const uchar *t; - const ushort *ss; - ushort type, tlen; - ushort vlan, nvlan; - - /* minimum size? */ - if (len < sizeof(CDP_SNAP_hdr) + 4) - goto pkt_short; - - /* check for valid CDP SNAP header */ - if (memcmp(pkt, CDP_SNAP_hdr, sizeof(CDP_SNAP_hdr)) != 0) - return; - - pkt += sizeof(CDP_SNAP_hdr); - len -= sizeof(CDP_SNAP_hdr); - - /* Version of CDP protocol must be >= 2 and TTL != 0 */ - if (pkt[0] < 0x02 || pkt[1] == 0) - return; - - /* - * if version is greater than 0x02 maybe we'll have a problem; - * output a warning - */ - if (pkt[0] != 0x02) - printf("**WARNING: CDP packet received with a protocol version " - "%d > 2\n", pkt[0] & 0xff); - - if (CDP_compute_csum(pkt, len) != 0) - return; - - pkt += 4; - len -= 4; - - vlan = htons(-1); - nvlan = htons(-1); - while (len > 0) { - if (len < 4) - goto pkt_short; - - ss = (const ushort *)pkt; - type = ntohs(ss[0]); - tlen = ntohs(ss[1]); - if (tlen > len) - goto pkt_short; - - pkt += tlen; - len -= tlen; - - ss += 2; /* point ss to the data of the TLV */ - tlen -= 4; - - switch (type) { - case CDP_DEVICE_ID_TLV: - break; - case CDP_ADDRESS_TLV: - break; - case CDP_PORT_ID_TLV: - break; - case CDP_CAPABILITIES_TLV: - break; - case CDP_VERSION_TLV: - break; - case CDP_PLATFORM_TLV: - break; - case CDP_NATIVE_VLAN_TLV: - nvlan = *ss; - break; - case CDP_APPLIANCE_VLAN_TLV: - t = (const uchar *)ss; - while (tlen > 0) { - if (tlen < 3) - goto pkt_short; - - ss = (const ushort *)(t + 1); - -#ifdef CONFIG_CDP_APPLIANCE_VLAN_TYPE - if (t[0] == CONFIG_CDP_APPLIANCE_VLAN_TYPE) - vlan = *ss; -#else - /* XXX will this work; dunno */ - vlan = ntohs(*ss); -#endif - t += 3; tlen -= 3; - } - break; - case CDP_TRIGGER_TLV: - break; - case CDP_POWER_CONSUMPTION_TLV: - break; - case CDP_SYSNAME_TLV: - break; - case CDP_SYSOBJECT_TLV: - break; - case CDP_MANAGEMENT_ADDRESS_TLV: - break; - } - } - - CDPApplianceVLAN = vlan; - CDPNativeVLAN = nvlan; - - CDPOK = 1; - return; - - pkt_short: - printf("** CDP packet is too short\n"); - return; -} - -static void CDPStart(void) -{ - printf("Using %s device\n", eth_get_name()); - CDPSeq = 0; - CDPOK = 0; - - CDPNativeVLAN = htons(-1); - CDPApplianceVLAN = htons(-1); - - NetSetTimeout(CDP_TIMEOUT, CDPTimeout); - NetSetHandler(CDPDummyHandler); - - CDPSendTrigger(); -} -#endif - #ifdef CONFIG_IP_DEFRAG /* * This function collects fragments in a single packet, according

Hi Joe,
On Tue, Mar 27, 2012 at 4:42 PM, Joe Hershberger joe.hershberger@ni.com wrote:
This needs a commit message.
Signed-off-by: Joe Hershberger joe.hershberger@ni.com Cc: Joe Hershberger joe.hershberger@gmail.com Cc: Simon Glass sjg@chromium.org Cc: Mike Frysinger vapier@gentoo.org
Changes for v2: - Moved is_cdp_packet to a separate patch - Fixed blank newline at the end of cdp.h - Pushed #ifdef CONFIG_CMD_CDP into header
include/net.h | 8 +- net/Makefile | 1 + net/cdp.c | 374 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++ net/cdp.h | 20 +++ net/net.c | 366 +------------------------------------------------------- 5 files changed, 399 insertions(+), 370 deletions(-) create mode 100644 net/cdp.c create mode 100644 net/cdp.h
diff --git a/include/net.h b/include/net.h index e39d88b..54f05bb 100644 --- a/include/net.h +++ b/include/net.h @@ -355,9 +355,7 @@ extern uchar NetEtherNullAddr[6]; extern ushort NetOurVLAN; /* Our VLAN */ extern ushort NetOurNativeVLAN; /* Our Native VLAN */
-extern uchar NetCDPAddr[6]; /* Ethernet CDP address */ -extern ushort CDPNativeVLAN; /* CDP returned native VLAN */ -extern ushort CDPApplianceVLAN; /* CDP returned appliance VLAN */ +extern const uchar NetCDPAddr[6]; /* Ethernet CDP address */
extern int NetState; /* Network loop state */ #define NETLOOP_CONTINUE 1 @@ -386,8 +384,8 @@ extern IPaddr_t NetPingIP; /* the ip address to ping */
#if defined(CONFIG_CMD_CDP) /* when CDP completes these hold the return values */ -extern ushort CDPNativeVLAN; -extern ushort CDPApplianceVLAN; +extern ushort CDPNativeVLAN; /* CDP returned native VLAN */ +extern ushort CDPApplianceVLAN; /* CDP returned appliance VLAN */ #endif
#if defined(CONFIG_CMD_SNTP) diff --git a/net/Makefile b/net/Makefile index 5901046..b350bfc 100644 --- a/net/Makefile +++ b/net/Makefile @@ -28,6 +28,7 @@ include $(TOPDIR)/config.mk LIB = $(obj)libnet.o
COBJS-$(CONFIG_CMD_NET) += bootp.o +COBJS-$(CONFIG_CMD_CDP) += cdp.o COBJS-$(CONFIG_CMD_DNS) += dns.o COBJS-$(CONFIG_CMD_NET) += eth.o COBJS-$(CONFIG_CMD_NET) += net.o diff --git a/net/cdp.c b/net/cdp.c new file mode 100644 index 0000000..028be82 --- /dev/null +++ b/net/cdp.c @@ -0,0 +1,374 @@ +/*
- Copied from Linux Monitor (LiMon) - Networking.
- Copyright 1994 - 2000 Neil Russell.
- (See License)
- Copyright 2000 Roland Borde
- Copyright 2000 Paolo Scaffardi
- Copyright 2000-2002 Wolfgang Denk, wd@denx.de
- */
+#include <common.h> +#include <net.h> +#if defined(CONFIG_CDP_VERSION) +#include <timestamp.h> +#endif
+#include "cdp.h"
+/* Ethernet bcast address */ +const uchar NetCDPAddr[6] = { 0x01, 0x00, 0x0c, 0xcc, 0xcc, 0xcc };
+#define CDP_DEVICE_ID_TLV 0x0001 +#define CDP_ADDRESS_TLV 0x0002 +#define CDP_PORT_ID_TLV 0x0003 +#define CDP_CAPABILITIES_TLV 0x0004 +#define CDP_VERSION_TLV 0x0005 +#define CDP_PLATFORM_TLV 0x0006 +#define CDP_NATIVE_VLAN_TLV 0x000a +#define CDP_APPLIANCE_VLAN_TLV 0x000e +#define CDP_TRIGGER_TLV 0x000f +#define CDP_POWER_CONSUMPTION_TLV 0x0010 +#define CDP_SYSNAME_TLV 0x0014 +#define CDP_SYSOBJECT_TLV 0x0015 +#define CDP_MANAGEMENT_ADDRESS_TLV 0x0016
+#define CDP_TIMEOUT 250UL /* one packet every 250ms */
+static int CDPSeq; +static int CDPOK;
+ushort CDPNativeVLAN; +ushort CDPApplianceVLAN;
+static const uchar CDP_SNAP_hdr[8] = { 0xAA, 0xAA, 0x03, 0x00, 0x00, 0x0C, 0x20,
- 0x00 };
Can we put the values on the next line?
+static ushort +CDP_compute_csum(const uchar *buff, ushort len)
Would be nice to add a function comment if you know what it does, and returns
+{
- ushort csum;
- int odd;
- ulong result = 0;
- ushort leftover;
- ushort *p;
- if (len > 0) {
- odd = 1 & (ulong)buff;
- if (odd) {
- result = *buff << 8;
- len--;
- buff++;
- }
- while (len > 1) {
- p = (ushort *)buff;
- result += *p++;
- buff = (uchar *)p;
- if (result & 0x80000000)
- result = (result & 0xFFFF) + (result >> 16);
- len -= 2;
- }
- if (len) {
- leftover = (signed short)(*(const signed char *)buff);
- /* CISCO SUCKS big time! (and blows too):
Comment style. But it is existing code, so perhaps you can leave it along? Poor Cisco.
- * CDP uses the IP checksum algorithm with a twist;
- * for the last byte it *sign* extends and sums.
- */
- result = (result & 0xffff0000) |
- ((result + leftover) & 0x0000ffff);
- }
- while (result >> 16)
- result = (result & 0xFFFF) + (result >> 16);
- if (odd)
- result = ((result >> 8) & 0xff) |
- ((result & 0xff) << 8);
- }
Regards, Simon

Checking for CDP packets should be encapsulated, not copied code.
Signed-off-by: Joe Hershberger joe.hershberger@ni.com Cc: Joe Hershberger joe.hershberger@gmail.com Cc: Simon Glass sjg@chromium.org Cc: Mike Frysinger vapier@gentoo.org --- Changes for v2: - Split from patch "Move CDP out of net.c" - Changed to static inline
arch/powerpc/cpu/mpc8xx/fec.c | 2 +- include/net.h | 7 +++++-- net/net.c | 2 +- 3 files changed, 7 insertions(+), 4 deletions(-)
diff --git a/arch/powerpc/cpu/mpc8xx/fec.c b/arch/powerpc/cpu/mpc8xx/fec.c index f2a2c3a..0bc1e5d 100644 --- a/arch/powerpc/cpu/mpc8xx/fec.c +++ b/arch/powerpc/cpu/mpc8xx/fec.c @@ -274,7 +274,7 @@ static int fec_recv (struct eth_device *dev) #if defined(CONFIG_CMD_CDP) if ((rx[0] & 1) != 0 && memcmp ((uchar *) rx, NetBcastAddr, 6) != 0 - && memcmp ((uchar *) rx, NetCDPAddr, 6) != 0) + && !is_cdp_packet((uchar *)rx)) rx = NULL; #endif /* diff --git a/include/net.h b/include/net.h index 54f05bb..7bd8043 100644 --- a/include/net.h +++ b/include/net.h @@ -355,8 +355,6 @@ extern uchar NetEtherNullAddr[6]; extern ushort NetOurVLAN; /* Our VLAN */ extern ushort NetOurNativeVLAN; /* Our Native VLAN */
-extern const uchar NetCDPAddr[6]; /* Ethernet CDP address */ - extern int NetState; /* Network loop state */ #define NETLOOP_CONTINUE 1 #define NETLOOP_RESTART 2 @@ -386,6 +384,11 @@ extern IPaddr_t NetPingIP; /* the ip address to ping */ /* when CDP completes these hold the return values */ extern ushort CDPNativeVLAN; /* CDP returned native VLAN */ extern ushort CDPApplianceVLAN; /* CDP returned appliance VLAN */ +static inline int is_cdp_packet(const uchar *et_addr) +{ + extern const uchar NetCDPAddr[6]; + return memcmp(et_addr, NetCDPAddr, 6) == 0; +} #endif
#if defined(CONFIG_CMD_SNTP) diff --git a/net/net.c b/net/net.c index b5b072a..7fdd71f 100644 --- a/net/net.c +++ b/net/net.c @@ -1101,7 +1101,7 @@ NetReceive(uchar *inpkt, int len)
#if defined(CONFIG_CMD_CDP) /* keep track if packet is CDP */ - iscdp = memcmp(et->et_dest, NetCDPAddr, 6) == 0; + iscdp = is_cdp_packet(et->et_dest); #endif
myvlanid = ntohs(NetOurVLAN);

Hi Joe,
On Tue, Mar 27, 2012 at 4:42 PM, Joe Hershberger joe.hershberger@ni.com wrote:
Checking for CDP packets should be encapsulated, not copied code.
Signed-off-by: Joe Hershberger joe.hershberger@ni.com Cc: Joe Hershberger joe.hershberger@gmail.com Cc: Simon Glass sjg@chromium.org Cc: Mike Frysinger vapier@gentoo.org
Changes for v2: - Split from patch "Move CDP out of net.c" - Changed to static inline
arch/powerpc/cpu/mpc8xx/fec.c | 2 +- include/net.h | 7 +++++-- net/net.c | 2 +- 3 files changed, 7 insertions(+), 4 deletions(-)
diff --git a/arch/powerpc/cpu/mpc8xx/fec.c b/arch/powerpc/cpu/mpc8xx/fec.c index f2a2c3a..0bc1e5d 100644 --- a/arch/powerpc/cpu/mpc8xx/fec.c +++ b/arch/powerpc/cpu/mpc8xx/fec.c @@ -274,7 +274,7 @@ static int fec_recv (struct eth_device *dev) #if defined(CONFIG_CMD_CDP) if ((rx[0] & 1) != 0 && memcmp ((uchar *) rx, NetBcastAddr, 6) != 0
- && memcmp ((uchar *) rx, NetCDPAddr, 6) != 0)
- && !is_cdp_packet((uchar *)rx))
rx = NULL; #endif /* diff --git a/include/net.h b/include/net.h index 54f05bb..7bd8043 100644 --- a/include/net.h +++ b/include/net.h @@ -355,8 +355,6 @@ extern uchar NetEtherNullAddr[6]; extern ushort NetOurVLAN; /* Our VLAN */ extern ushort NetOurNativeVLAN; /* Our Native VLAN */
-extern const uchar NetCDPAddr[6]; /* Ethernet CDP address */
extern int NetState; /* Network loop state */ #define NETLOOP_CONTINUE 1 #define NETLOOP_RESTART 2 @@ -386,6 +384,11 @@ extern IPaddr_t NetPingIP; /* the ip address to ping */ /* when CDP completes these hold the return values */ extern ushort CDPNativeVLAN; /* CDP returned native VLAN */ extern ushort CDPApplianceVLAN; /* CDP returned appliance VLAN */
blank line, function comment
+static inline int is_cdp_packet(const uchar *et_addr) +{
- extern const uchar NetCDPAddr[6];
blank line?
- return memcmp(et_addr, NetCDPAddr, 6) == 0;
+} #endif
#if defined(CONFIG_CMD_SNTP) diff --git a/net/net.c b/net/net.c index b5b072a..7fdd71f 100644 --- a/net/net.c +++ b/net/net.c @@ -1101,7 +1101,7 @@ NetReceive(uchar *inpkt, int len)
#if defined(CONFIG_CMD_CDP) /* keep track if packet is CDP */
- iscdp = memcmp(et->et_dest, NetCDPAddr, 6) == 0;
- iscdp = is_cdp_packet(et->et_dest);
#endif
myvlanid = ntohs(NetOurVLAN);
1.6.0.2
Regards, Simon

Signed-off-by: Joe Hershberger joe.hershberger@ni.com Cc: Joe Hershberger joe.hershberger@gmail.com Cc: Simon Glass sjg@chromium.org Cc: Mike Frysinger vapier@gentoo.org --- include/net.h | 3 +- net/Makefile | 1 + net/arp.c | 213 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++ net/arp.h | 30 ++++++++ net/net.c | 209 ++++---------------------------------------------------- 5 files changed, 259 insertions(+), 197 deletions(-) create mode 100644 net/arp.c create mode 100644 net/arp.h
diff --git a/include/net.h b/include/net.h index 7bd8043..7fc0b60 100644 --- a/include/net.h +++ b/include/net.h @@ -422,7 +422,8 @@ extern void NetSetIP(uchar *, IPaddr_t, int, int, int); extern int NetCksumOk(uchar *, int); /* Return true if cksum OK */ extern uint NetCksum(uchar *, int); /* Calculate the checksum */
-/* Set callbacks */ +/* Callbacks */ +extern rxhand_f *NetGetHandler(void); /* Get RX packet handler */ extern void NetSetHandler(rxhand_f *); /* Set RX packet handler */ extern void net_set_icmp_handler(rxhand_icmp_f *f); /* Set ICMP RX handler */ extern void NetSetTimeout(ulong, thand_f *);/* Set timeout handler */ diff --git a/net/Makefile b/net/Makefile index b350bfc..0916a56 100644 --- a/net/Makefile +++ b/net/Makefile @@ -27,6 +27,7 @@ include $(TOPDIR)/config.mk
LIB = $(obj)libnet.o
+COBJS-$(CONFIG_CMD_NET) += arp.o COBJS-$(CONFIG_CMD_NET) += bootp.o COBJS-$(CONFIG_CMD_CDP) += cdp.o COBJS-$(CONFIG_CMD_DNS) += dns.o diff --git a/net/arp.c b/net/arp.c new file mode 100644 index 0000000..f75217c --- /dev/null +++ b/net/arp.c @@ -0,0 +1,213 @@ +/* + * Copied from Linux Monitor (LiMon) - Networking. + * + * Copyright 1994 - 2000 Neil Russell. + * (See License) + * Copyright 2000 Roland Borde + * Copyright 2000 Paolo Scaffardi + * Copyright 2000-2002 Wolfgang Denk, wd@denx.de + */ + +#include <common.h> + +#include "arp.h" + +#ifndef CONFIG_ARP_TIMEOUT +/* Milliseconds before trying ARP again */ +# define ARP_TIMEOUT 5000UL +#else +# define ARP_TIMEOUT CONFIG_ARP_TIMEOUT +#endif + + +#ifndef CONFIG_NET_RETRY_COUNT +# define ARP_TIMEOUT_COUNT 5 /* # of timeouts before giving up */ +#else +# define ARP_TIMEOUT_COUNT CONFIG_NET_RETRY_COUNT +#endif + +IPaddr_t NetArpWaitPacketIP; +IPaddr_t NetArpWaitReplyIP; +/* MAC address of waiting packet's destination */ +uchar *NetArpWaitPacketMAC; +/* THE transmit packet */ +uchar *NetArpWaitTxPacket; +int NetArpWaitTxPacketSize; +uchar NetArpWaitPacketBuf[PKTSIZE_ALIGN + PKTALIGN]; +ulong NetArpWaitTimerStart; +int NetArpWaitTry; + +void ArpInit(void) +{ + /* XXX problem with bss workaround */ + NetArpWaitPacketMAC = NULL; + NetArpWaitPacketIP = 0; + NetArpWaitReplyIP = 0; + NetArpWaitTxPacket = &NetArpWaitPacketBuf[0] + (PKTALIGN - 1); + NetArpWaitTxPacket -= (ulong)NetArpWaitTxPacket % PKTALIGN; + NetArpWaitTxPacketSize = 0; +} + +void ArpRequest(void) +{ + uchar *pkt; + ARP_t *arp; + + debug("ARP broadcast %d\n", NetArpWaitTry); + + pkt = NetTxPacket; + + pkt += NetSetEther(pkt, NetBcastAddr, PROT_ARP); + + arp = (ARP_t *) pkt; + + arp->ar_hrd = htons(ARP_ETHER); + arp->ar_pro = htons(PROT_IP); + arp->ar_hln = 6; + arp->ar_pln = 4; + arp->ar_op = htons(ARPOP_REQUEST); + + /* source ET addr */ + memcpy(&arp->ar_data[0], NetOurEther, 6); + /* source IP addr */ + NetWriteIP((uchar *) &arp->ar_data[6], NetOurIP); + /* dest ET addr = 0 */ + memset(&arp->ar_data[10], '\0', 6); + if ((NetArpWaitPacketIP & NetOurSubnetMask) != + (NetOurIP & NetOurSubnetMask)) { + if (NetOurGatewayIP == 0) { + puts("## Warning: gatewayip needed but not set\n"); + NetArpWaitReplyIP = NetArpWaitPacketIP; + } else { + NetArpWaitReplyIP = NetOurGatewayIP; + } + } else { + NetArpWaitReplyIP = NetArpWaitPacketIP; + } + + NetWriteIP((uchar *) &arp->ar_data[16], NetArpWaitReplyIP); + (void) eth_send(NetTxPacket, (pkt - NetTxPacket) + ARP_HDR_SIZE); +} + +void ArpTimeoutCheck(void) +{ + ulong t; + + if (!NetArpWaitPacketIP) + return; + + t = get_timer(0); + + /* check for arp timeout */ + if ((t - NetArpWaitTimerStart) > ARP_TIMEOUT) { + NetArpWaitTry++; + + if (NetArpWaitTry >= ARP_TIMEOUT_COUNT) { + puts("\nARP Retry count exceeded; starting again\n"); + NetArpWaitTry = 0; + NetStartAgain(); + } else { + NetArpWaitTimerStart = t; + ArpRequest(); + } + } +} + +void ArpReceive(Ethernet_t *et, IP_t *ip, int len) +{ + ARP_t *arp; + IPaddr_t tmp; + uchar *pkt; + + /* + * We have to deal with two types of ARP packets: + * - REQUEST packets will be answered by sending our + * IP address - if we know it. + * - REPLY packates are expected only after we asked + * for the TFTP server's or the gateway's ethernet + * address; so if we receive such a packet, we set + * the server ethernet address + */ + debug("Got ARP\n"); + + arp = (ARP_t *)ip; + if (len < ARP_HDR_SIZE) { + printf("bad length %d < %d\n", len, ARP_HDR_SIZE); + return; + } + if (ntohs(arp->ar_hrd) != ARP_ETHER) + return; + if (ntohs(arp->ar_pro) != PROT_IP) + return; + if (arp->ar_hln != 6) + return; + if (arp->ar_pln != 4) + return; + + if (NetOurIP == 0) + return; + + if (NetReadIP(&arp->ar_data[16]) != NetOurIP) + return; + + switch (ntohs(arp->ar_op)) { + case ARPOP_REQUEST: + /* reply with our IP address */ + debug("Got ARP REQUEST, return our IP\n"); + pkt = (uchar *)et; + pkt += NetSetEther(pkt, et->et_src, PROT_ARP); + arp->ar_op = htons(ARPOP_REPLY); + memcpy(&arp->ar_data[10], &arp->ar_data[0], 6); + NetCopyIP(&arp->ar_data[16], &arp->ar_data[6]); + memcpy(&arp->ar_data[0], NetOurEther, 6); + NetCopyIP(&arp->ar_data[6], &NetOurIP); + (void) eth_send((uchar *)et, + (pkt - (uchar *)et) + ARP_HDR_SIZE); + return; + + case ARPOP_REPLY: /* arp reply */ + /* are we waiting for a reply */ + if (!NetArpWaitPacketIP || !NetArpWaitPacketMAC) + break; + +#ifdef CONFIG_KEEP_SERVERADDR + if (NetServerIP == NetArpWaitPacketIP) { + char buf[20]; + sprintf(buf, "%pM", arp->ar_data); + setenv("serveraddr", buf); + } +#endif + + tmp = NetReadIP(&arp->ar_data[6]); + + /* matched waiting packet's address */ + if (tmp == NetArpWaitReplyIP) { + debug("Got ARP REPLY, set eth addr (%pM)\n", + arp->ar_data); + + /* save address for later use */ + memcpy(NetArpWaitPacketMAC, + &arp->ar_data[0], 6); + +#ifdef CONFIG_NETCONSOLE + NetGetHandler()(0, 0, 0, 0, 0); +#endif + /* modify header, and transmit it */ + memcpy(((Ethernet_t *)NetArpWaitTxPacket)-> + et_dest, NetArpWaitPacketMAC, 6); + (void) eth_send(NetArpWaitTxPacket, + NetArpWaitTxPacketSize); + + /* no arp request pending now */ + NetArpWaitPacketIP = 0; + NetArpWaitTxPacketSize = 0; + NetArpWaitPacketMAC = NULL; + + } + return; + default: + debug("Unexpected ARP opcode 0x%x\n", + ntohs(arp->ar_op)); + return; + } +} diff --git a/net/arp.h b/net/arp.h new file mode 100644 index 0000000..4016a90 --- /dev/null +++ b/net/arp.h @@ -0,0 +1,30 @@ +/* + * Copied from Linux Monitor (LiMon) - Networking. + * + * Copyright 1994 - 2000 Neil Russell. + * (See License) + * Copyright 2000 Roland Borde + * Copyright 2000 Paolo Scaffardi + * Copyright 2000-2002 Wolfgang Denk, wd@denx.de + */ + +#ifndef __ARP_H__ +#define __ARP_H__ + +#include <common.h> + +extern IPaddr_t NetArpWaitPacketIP; +/* MAC address of waiting packet's destination */ +extern uchar *NetArpWaitPacketMAC; +/* THE transmit packet */ +extern uchar *NetArpWaitTxPacket; +extern int NetArpWaitTxPacketSize; +extern ulong NetArpWaitTimerStart; +extern int NetArpWaitTry; + +void ArpInit(void); +void ArpRequest(void); +void ArpTimeoutCheck(void); +void ArpReceive(Ethernet_t *et, IP_t *ip, int len); + +#endif /* __ARP_H__ */ diff --git a/net/net.c b/net/net.c index 7fdd71f..f941f91 100644 --- a/net/net.c +++ b/net/net.c @@ -79,6 +79,7 @@ #include <command.h> #include <linux/compiler.h> #include <net.h> +#include "arp.h" #include "bootp.h" #include "tftp.h" #ifdef CONFIG_CMD_RARP @@ -99,20 +100,6 @@
DECLARE_GLOBAL_DATA_PTR;
-#ifndef CONFIG_ARP_TIMEOUT -/* Milliseconds before trying ARP again */ -# define ARP_TIMEOUT 5000UL -#else -# define ARP_TIMEOUT CONFIG_ARP_TIMEOUT -#endif - - -#ifndef CONFIG_NET_RETRY_COUNT -# define ARP_TIMEOUT_COUNT 5 /* # of timeouts before giving up */ -#else -# define ARP_TIMEOUT_COUNT CONFIG_NET_RETRY_COUNT -#endif - /** BOOTP EXTENTIONS **/
/* Our subnet mask (0=unknown) */ @@ -219,82 +206,6 @@ static int NetTryCount;
/**********************************************************************/
-IPaddr_t NetArpWaitPacketIP; -IPaddr_t NetArpWaitReplyIP; -/* MAC address of waiting packet's destination */ -uchar *NetArpWaitPacketMAC; -/* THE transmit packet */ -uchar *NetArpWaitTxPacket; -int NetArpWaitTxPacketSize; -uchar NetArpWaitPacketBuf[PKTSIZE_ALIGN + PKTALIGN]; -ulong NetArpWaitTimerStart; -int NetArpWaitTry; - -void ArpRequest(void) -{ - uchar *pkt; - ARP_t *arp; - - debug("ARP broadcast %d\n", NetArpWaitTry); - - pkt = NetTxPacket; - - pkt += NetSetEther(pkt, NetBcastAddr, PROT_ARP); - - arp = (ARP_t *) pkt; - - arp->ar_hrd = htons(ARP_ETHER); - arp->ar_pro = htons(PROT_IP); - arp->ar_hln = 6; - arp->ar_pln = 4; - arp->ar_op = htons(ARPOP_REQUEST); - - /* source ET addr */ - memcpy(&arp->ar_data[0], NetOurEther, 6); - /* source IP addr */ - NetWriteIP((uchar *) &arp->ar_data[6], NetOurIP); - /* dest ET addr = 0 */ - memset(&arp->ar_data[10], '\0', 6); - if ((NetArpWaitPacketIP & NetOurSubnetMask) != - (NetOurIP & NetOurSubnetMask)) { - if (NetOurGatewayIP == 0) { - puts("## Warning: gatewayip needed but not set\n"); - NetArpWaitReplyIP = NetArpWaitPacketIP; - } else { - NetArpWaitReplyIP = NetOurGatewayIP; - } - } else { - NetArpWaitReplyIP = NetArpWaitPacketIP; - } - - NetWriteIP((uchar *) &arp->ar_data[16], NetArpWaitReplyIP); - (void) eth_send(NetTxPacket, (pkt - NetTxPacket) + ARP_HDR_SIZE); -} - -void ArpTimeoutCheck(void) -{ - ulong t; - - if (!NetArpWaitPacketIP) - return; - - t = get_timer(0); - - /* check for arp timeout */ - if ((t - NetArpWaitTimerStart) > ARP_TIMEOUT) { - NetArpWaitTry++; - - if (NetArpWaitTry >= ARP_TIMEOUT_COUNT) { - puts("\nARP Retry count exceeded; starting again\n"); - NetArpWaitTry = 0; - NetStartAgain(); - } else { - NetArpWaitTimerStart = t; - ArpRequest(); - } - } -} - /* * Check if autoload is enabled. If so, use either NFS or TFTP to download * the boot file. @@ -362,15 +273,11 @@ int NetLoop(enum proto_t protocol) NetRestarted = 0; NetDevExists = 0;
- /* XXX problem with bss workaround */ - NetArpWaitPacketMAC = NULL; - NetArpWaitTxPacket = NULL; - NetArpWaitPacketIP = 0; - NetArpWaitReplyIP = 0; - NetArpWaitTxPacket = NULL; NetTxPacket = NULL; NetTryCount = 1;
+ ArpInit(); + if (!NetTxPacket) { int i; /* @@ -382,12 +289,6 @@ int NetLoop(enum proto_t protocol) NetRxPackets[i] = NetTxPacket + (i+1)*PKTSIZE_ALIGN; }
- if (!NetArpWaitTxPacket) { - NetArpWaitTxPacket = &NetArpWaitPacketBuf[0] + (PKTALIGN - 1); - NetArpWaitTxPacket -= (ulong)NetArpWaitTxPacket % PKTALIGN; - NetArpWaitTxPacketSize = 0; - } - bootstage_mark_name(BOOTSTAGE_ID_ETH_START, "eth_start"); eth_halt(); eth_set_current(); @@ -661,6 +562,13 @@ void NetStartAgain(void) * Miscelaneous bits. */
+rxhand_f * +NetGetHandler(void) +{ + return packetHandler; +} + + void NetSetHandler(rxhand_f *f) { @@ -1072,11 +980,12 @@ NetReceive(uchar *inpkt, int len) { Ethernet_t *et; IP_t *ip; +#ifdef CONFIG_CMD_RARP ARP_t *arp; +#endif IPaddr_t tmp; IPaddr_t src_ip; int x; - uchar *pkt; #if defined(CONFIG_CMD_CDP) int iscdp; #endif @@ -1173,99 +1082,7 @@ NetReceive(uchar *inpkt, int len) switch (x) {
case PROT_ARP: - /* - * We have to deal with two types of ARP packets: - * - REQUEST packets will be answered by sending our - * IP address - if we know it. - * - REPLY packates are expected only after we asked - * for the TFTP server's or the gateway's ethernet - * address; so if we receive such a packet, we set - * the server ethernet address - */ - debug("Got ARP\n"); - - arp = (ARP_t *)ip; - if (len < ARP_HDR_SIZE) { - printf("bad length %d < %d\n", len, ARP_HDR_SIZE); - return; - } - if (ntohs(arp->ar_hrd) != ARP_ETHER) - return; - if (ntohs(arp->ar_pro) != PROT_IP) - return; - if (arp->ar_hln != 6) - return; - if (arp->ar_pln != 4) - return; - - if (NetOurIP == 0) - return; - - if (NetReadIP(&arp->ar_data[16]) != NetOurIP) - return; - - switch (ntohs(arp->ar_op)) { - case ARPOP_REQUEST: - /* reply with our IP address */ - debug("Got ARP REQUEST, return our IP\n"); - pkt = (uchar *)et; - pkt += NetSetEther(pkt, et->et_src, PROT_ARP); - arp->ar_op = htons(ARPOP_REPLY); - memcpy(&arp->ar_data[10], &arp->ar_data[0], 6); - NetCopyIP(&arp->ar_data[16], &arp->ar_data[6]); - memcpy(&arp->ar_data[0], NetOurEther, 6); - NetCopyIP(&arp->ar_data[6], &NetOurIP); - (void) eth_send((uchar *)et, - (pkt - (uchar *)et) + ARP_HDR_SIZE); - return; - - case ARPOP_REPLY: /* arp reply */ - /* are we waiting for a reply */ - if (!NetArpWaitPacketIP || !NetArpWaitPacketMAC) - break; - -#ifdef CONFIG_KEEP_SERVERADDR - if (NetServerIP == NetArpWaitPacketIP) { - char buf[20]; - sprintf(buf, "%pM", arp->ar_data); - setenv("serveraddr", buf); - } -#endif - - debug("Got ARP REPLY, set server/gtwy eth addr (%pM)\n", - arp->ar_data); - - tmp = NetReadIP(&arp->ar_data[6]); - - /* matched waiting packet's address */ - if (tmp == NetArpWaitReplyIP) { - debug("Got it\n"); - - /* save address for later use */ - memcpy(NetArpWaitPacketMAC, - &arp->ar_data[0], 6); - -#ifdef CONFIG_NETCONSOLE - (*packetHandler)(0, 0, 0, 0, 0); -#endif - /* modify header, and transmit it */ - memcpy(((Ethernet_t *)NetArpWaitTxPacket)-> - et_dest, NetArpWaitPacketMAC, 6); - (void) eth_send(NetArpWaitTxPacket, - NetArpWaitTxPacketSize); - - /* no arp request pending now */ - NetArpWaitPacketIP = 0; - NetArpWaitTxPacketSize = 0; - NetArpWaitPacketMAC = NULL; - - } - return; - default: - debug("Unexpected ARP opcode 0x%x\n", - ntohs(arp->ar_op)); - return; - } + ArpReceive(et, ip, len); break;
#ifdef CONFIG_CMD_RARP

Hi Joe,
On Tue, Mar 27, 2012 at 4:42 PM, Joe Hershberger joe.hershberger@ni.com wrote:
Signed-off-by: Joe Hershberger joe.hershberger@ni.com Cc: Joe Hershberger joe.hershberger@gmail.com Cc: Simon Glass sjg@chromium.org Cc: Mike Frysinger vapier@gentoo.org
No changes?
Acked-by: Simon Glass sjg@chromium.org
include/net.h | 3 +- net/Makefile | 1 + net/arp.c | 213 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++ net/arp.h | 30 ++++++++ net/net.c | 209 ++++---------------------------------------------------- 5 files changed, 259 insertions(+), 197 deletions(-) create mode 100644 net/arp.c create mode 100644 net/arp.h
diff --git a/include/net.h b/include/net.h index 7bd8043..7fc0b60 100644 --- a/include/net.h +++ b/include/net.h @@ -422,7 +422,8 @@ extern void NetSetIP(uchar *, IPaddr_t, int, int, int); extern int NetCksumOk(uchar *, int); /* Return true if cksum OK */ extern uint NetCksum(uchar *, int); /* Calculate the checksum */
-/* Set callbacks */ +/* Callbacks */ +extern rxhand_f *NetGetHandler(void); /* Get RX packet handler */ extern void NetSetHandler(rxhand_f *); /* Set RX packet handler */ extern void net_set_icmp_handler(rxhand_icmp_f *f); /* Set ICMP RX handler */ extern void NetSetTimeout(ulong, thand_f *);/* Set timeout handler */ diff --git a/net/Makefile b/net/Makefile index b350bfc..0916a56 100644 --- a/net/Makefile +++ b/net/Makefile @@ -27,6 +27,7 @@ include $(TOPDIR)/config.mk
LIB = $(obj)libnet.o
+COBJS-$(CONFIG_CMD_NET) += arp.o COBJS-$(CONFIG_CMD_NET) += bootp.o COBJS-$(CONFIG_CMD_CDP) += cdp.o COBJS-$(CONFIG_CMD_DNS) += dns.o diff --git a/net/arp.c b/net/arp.c new file mode 100644 index 0000000..f75217c --- /dev/null +++ b/net/arp.c @@ -0,0 +1,213 @@ +/*
- Copied from Linux Monitor (LiMon) - Networking.
- Copyright 1994 - 2000 Neil Russell.
- (See License)
- Copyright 2000 Roland Borde
- Copyright 2000 Paolo Scaffardi
- Copyright 2000-2002 Wolfgang Denk, wd@denx.de
- */
+#include <common.h>
+#include "arp.h"
+#ifndef CONFIG_ARP_TIMEOUT +/* Milliseconds before trying ARP again */ +# define ARP_TIMEOUT 5000UL +#else +# define ARP_TIMEOUT CONFIG_ARP_TIMEOUT +#endif
+#ifndef CONFIG_NET_RETRY_COUNT +# define ARP_TIMEOUT_COUNT 5 /* # of timeouts before giving up */ +#else +# define ARP_TIMEOUT_COUNT CONFIG_NET_RETRY_COUNT +#endif
+IPaddr_t NetArpWaitPacketIP; +IPaddr_t NetArpWaitReplyIP; +/* MAC address of waiting packet's destination */ +uchar *NetArpWaitPacketMAC; +/* THE transmit packet */ +uchar *NetArpWaitTxPacket; +int NetArpWaitTxPacketSize; +uchar NetArpWaitPacketBuf[PKTSIZE_ALIGN + PKTALIGN]; +ulong NetArpWaitTimerStart; +int NetArpWaitTry;
+void ArpInit(void) +{
- /* XXX problem with bss workaround */
I wonder what this is?
- NetArpWaitPacketMAC = NULL;
- NetArpWaitPacketIP = 0;
- NetArpWaitReplyIP = 0;
- NetArpWaitTxPacket = &NetArpWaitPacketBuf[0] + (PKTALIGN - 1);
- NetArpWaitTxPacket -= (ulong)NetArpWaitTxPacket % PKTALIGN;
- NetArpWaitTxPacketSize = 0;
+}
+void ArpRequest(void) +{
- uchar *pkt;
- ARP_t *arp;
- debug("ARP broadcast %d\n", NetArpWaitTry);
- pkt = NetTxPacket;
- pkt += NetSetEther(pkt, NetBcastAddr, PROT_ARP);
- arp = (ARP_t *) pkt;
- arp->ar_hrd = htons(ARP_ETHER);
- arp->ar_pro = htons(PROT_IP);
- arp->ar_hln = 6;
- arp->ar_pln = 4;
- arp->ar_op = htons(ARPOP_REQUEST);
- /* source ET addr */
- memcpy(&arp->ar_data[0], NetOurEther, 6);
- /* source IP addr */
- NetWriteIP((uchar *) &arp->ar_data[6], NetOurIP);
- /* dest ET addr = 0 */
- memset(&arp->ar_data[10], '\0', 6);
- if ((NetArpWaitPacketIP & NetOurSubnetMask) !=
- (NetOurIP & NetOurSubnetMask)) {
- if (NetOurGatewayIP == 0) {
- puts("## Warning: gatewayip needed but not set\n");
- NetArpWaitReplyIP = NetArpWaitPacketIP;
- } else {
- NetArpWaitReplyIP = NetOurGatewayIP;
- }
- } else {
- NetArpWaitReplyIP = NetArpWaitPacketIP;
- }
- NetWriteIP((uchar *) &arp->ar_data[16], NetArpWaitReplyIP);
- (void) eth_send(NetTxPacket, (pkt - NetTxPacket) + ARP_HDR_SIZE);
+}
+void ArpTimeoutCheck(void) +{
- ulong t;
- if (!NetArpWaitPacketIP)
- return;
- t = get_timer(0);
- /* check for arp timeout */
- if ((t - NetArpWaitTimerStart) > ARP_TIMEOUT) {
- NetArpWaitTry++;
- if (NetArpWaitTry >= ARP_TIMEOUT_COUNT) {
- puts("\nARP Retry count exceeded; starting again\n");
- NetArpWaitTry = 0;
- NetStartAgain();
- } else {
- NetArpWaitTimerStart = t;
- ArpRequest();
- }
- }
+}
+void ArpReceive(Ethernet_t *et, IP_t *ip, int len) +{
- ARP_t *arp;
- IPaddr_t tmp;
- uchar *pkt;
- /*
- * We have to deal with two types of ARP packets:
- * - REQUEST packets will be answered by sending our
- * IP address - if we know it.
- * - REPLY packates are expected only after we asked
- * for the TFTP server's or the gateway's ethernet
- * address; so if we receive such a packet, we set
- * the server ethernet address
- */
- debug("Got ARP\n");
- arp = (ARP_t *)ip;
- if (len < ARP_HDR_SIZE) {
- printf("bad length %d < %d\n", len, ARP_HDR_SIZE);
- return;
- }
- if (ntohs(arp->ar_hrd) != ARP_ETHER)
- return;
- if (ntohs(arp->ar_pro) != PROT_IP)
- return;
- if (arp->ar_hln != 6)
- return;
- if (arp->ar_pln != 4)
- return;
- if (NetOurIP == 0)
- return;
- if (NetReadIP(&arp->ar_data[16]) != NetOurIP)
- return;
- switch (ntohs(arp->ar_op)) {
- case ARPOP_REQUEST:
- /* reply with our IP address */
- debug("Got ARP REQUEST, return our IP\n");
- pkt = (uchar *)et;
- pkt += NetSetEther(pkt, et->et_src, PROT_ARP);
- arp->ar_op = htons(ARPOP_REPLY);
- memcpy(&arp->ar_data[10], &arp->ar_data[0], 6);
- NetCopyIP(&arp->ar_data[16], &arp->ar_data[6]);
- memcpy(&arp->ar_data[0], NetOurEther, 6);
- NetCopyIP(&arp->ar_data[6], &NetOurIP);
- (void) eth_send((uchar *)et,
- (pkt - (uchar *)et) + ARP_HDR_SIZE);
- return;
- case ARPOP_REPLY: /* arp reply */
- /* are we waiting for a reply */
- if (!NetArpWaitPacketIP || !NetArpWaitPacketMAC)
- break;
+#ifdef CONFIG_KEEP_SERVERADDR
- if (NetServerIP == NetArpWaitPacketIP) {
- char buf[20];
- sprintf(buf, "%pM", arp->ar_data);
- setenv("serveraddr", buf);
- }
+#endif
- tmp = NetReadIP(&arp->ar_data[6]);
- /* matched waiting packet's address */
- if (tmp == NetArpWaitReplyIP) {
- debug("Got ARP REPLY, set eth addr (%pM)\n",
- arp->ar_data);
- /* save address for later use */
- memcpy(NetArpWaitPacketMAC,
- &arp->ar_data[0], 6);
+#ifdef CONFIG_NETCONSOLE
- NetGetHandler()(0, 0, 0, 0, 0);
+#endif
- /* modify header, and transmit it */
- memcpy(((Ethernet_t *)NetArpWaitTxPacket)->
- et_dest, NetArpWaitPacketMAC, 6);
- (void) eth_send(NetArpWaitTxPacket,
- NetArpWaitTxPacketSize);
- /* no arp request pending now */
- NetArpWaitPacketIP = 0;
- NetArpWaitTxPacketSize = 0;
- NetArpWaitPacketMAC = NULL;
- }
- return;
- default:
- debug("Unexpected ARP opcode 0x%x\n",
- ntohs(arp->ar_op));
- return;
- }
+} diff --git a/net/arp.h b/net/arp.h new file mode 100644 index 0000000..4016a90 --- /dev/null +++ b/net/arp.h @@ -0,0 +1,30 @@ +/*
- Copied from Linux Monitor (LiMon) - Networking.
- Copyright 1994 - 2000 Neil Russell.
- (See License)
- Copyright 2000 Roland Borde
- Copyright 2000 Paolo Scaffardi
- Copyright 2000-2002 Wolfgang Denk, wd@denx.de
- */
+#ifndef __ARP_H__ +#define __ARP_H__
+#include <common.h>
+extern IPaddr_t NetArpWaitPacketIP; +/* MAC address of waiting packet's destination */ +extern uchar *NetArpWaitPacketMAC; +/* THE transmit packet */ +extern uchar *NetArpWaitTxPacket; +extern int NetArpWaitTxPacketSize; +extern ulong NetArpWaitTimerStart; +extern int NetArpWaitTry;
+void ArpInit(void); +void ArpRequest(void); +void ArpTimeoutCheck(void); +void ArpReceive(Ethernet_t *et, IP_t *ip, int len);
+#endif /* __ARP_H__ */ diff --git a/net/net.c b/net/net.c index 7fdd71f..f941f91 100644 --- a/net/net.c +++ b/net/net.c @@ -79,6 +79,7 @@ #include <command.h> #include <linux/compiler.h> #include <net.h> +#include "arp.h" #include "bootp.h" #include "tftp.h" #ifdef CONFIG_CMD_RARP @@ -99,20 +100,6 @@
DECLARE_GLOBAL_DATA_PTR;
-#ifndef CONFIG_ARP_TIMEOUT -/* Milliseconds before trying ARP again */ -# define ARP_TIMEOUT 5000UL -#else -# define ARP_TIMEOUT CONFIG_ARP_TIMEOUT -#endif
-#ifndef CONFIG_NET_RETRY_COUNT -# define ARP_TIMEOUT_COUNT 5 /* # of timeouts before giving up */ -#else -# define ARP_TIMEOUT_COUNT CONFIG_NET_RETRY_COUNT -#endif
/** BOOTP EXTENTIONS **/
/* Our subnet mask (0=unknown) */ @@ -219,82 +206,6 @@ static int NetTryCount;
/**********************************************************************/
-IPaddr_t NetArpWaitPacketIP; -IPaddr_t NetArpWaitReplyIP; -/* MAC address of waiting packet's destination */ -uchar *NetArpWaitPacketMAC; -/* THE transmit packet */ -uchar *NetArpWaitTxPacket; -int NetArpWaitTxPacketSize; -uchar NetArpWaitPacketBuf[PKTSIZE_ALIGN + PKTALIGN]; -ulong NetArpWaitTimerStart; -int NetArpWaitTry;
-void ArpRequest(void) -{
- uchar *pkt;
- ARP_t *arp;
- debug("ARP broadcast %d\n", NetArpWaitTry);
- pkt = NetTxPacket;
- pkt += NetSetEther(pkt, NetBcastAddr, PROT_ARP);
- arp = (ARP_t *) pkt;
- arp->ar_hrd = htons(ARP_ETHER);
- arp->ar_pro = htons(PROT_IP);
- arp->ar_hln = 6;
- arp->ar_pln = 4;
- arp->ar_op = htons(ARPOP_REQUEST);
- /* source ET addr */
- memcpy(&arp->ar_data[0], NetOurEther, 6);
- /* source IP addr */
- NetWriteIP((uchar *) &arp->ar_data[6], NetOurIP);
- /* dest ET addr = 0 */
- memset(&arp->ar_data[10], '\0', 6);
- if ((NetArpWaitPacketIP & NetOurSubnetMask) !=
- (NetOurIP & NetOurSubnetMask)) {
- if (NetOurGatewayIP == 0) {
- puts("## Warning: gatewayip needed but not set\n");
- NetArpWaitReplyIP = NetArpWaitPacketIP;
- } else {
- NetArpWaitReplyIP = NetOurGatewayIP;
- }
- } else {
- NetArpWaitReplyIP = NetArpWaitPacketIP;
- }
- NetWriteIP((uchar *) &arp->ar_data[16], NetArpWaitReplyIP);
- (void) eth_send(NetTxPacket, (pkt - NetTxPacket) + ARP_HDR_SIZE);
-}
-void ArpTimeoutCheck(void) -{
- ulong t;
- if (!NetArpWaitPacketIP)
- return;
- t = get_timer(0);
- /* check for arp timeout */
- if ((t - NetArpWaitTimerStart) > ARP_TIMEOUT) {
- NetArpWaitTry++;
- if (NetArpWaitTry >= ARP_TIMEOUT_COUNT) {
- puts("\nARP Retry count exceeded; starting again\n");
- NetArpWaitTry = 0;
- NetStartAgain();
- } else {
- NetArpWaitTimerStart = t;
- ArpRequest();
- }
- }
-}
/* * Check if autoload is enabled. If so, use either NFS or TFTP to download * the boot file. @@ -362,15 +273,11 @@ int NetLoop(enum proto_t protocol) NetRestarted = 0; NetDevExists = 0;
- /* XXX problem with bss workaround */
- NetArpWaitPacketMAC = NULL;
- NetArpWaitTxPacket = NULL;
- NetArpWaitPacketIP = 0;
- NetArpWaitReplyIP = 0;
- NetArpWaitTxPacket = NULL;
NetTxPacket = NULL; NetTryCount = 1;
- ArpInit();
if (!NetTxPacket) { int i; /* @@ -382,12 +289,6 @@ int NetLoop(enum proto_t protocol) NetRxPackets[i] = NetTxPacket + (i+1)*PKTSIZE_ALIGN; }
- if (!NetArpWaitTxPacket) {
- NetArpWaitTxPacket = &NetArpWaitPacketBuf[0] + (PKTALIGN - 1);
- NetArpWaitTxPacket -= (ulong)NetArpWaitTxPacket % PKTALIGN;
- NetArpWaitTxPacketSize = 0;
- }
bootstage_mark_name(BOOTSTAGE_ID_ETH_START, "eth_start"); eth_halt(); eth_set_current(); @@ -661,6 +562,13 @@ void NetStartAgain(void) * Miscelaneous bits. */
+rxhand_f * +NetGetHandler(void) +{
- return packetHandler;
+}
void NetSetHandler(rxhand_f *f) { @@ -1072,11 +980,12 @@ NetReceive(uchar *inpkt, int len) { Ethernet_t *et; IP_t *ip; +#ifdef CONFIG_CMD_RARP ARP_t *arp; +#endif IPaddr_t tmp; IPaddr_t src_ip; int x;
- uchar *pkt;
#if defined(CONFIG_CMD_CDP) int iscdp; #endif @@ -1173,99 +1082,7 @@ NetReceive(uchar *inpkt, int len) switch (x) {
case PROT_ARP:
- /*
- * We have to deal with two types of ARP packets:
- * - REQUEST packets will be answered by sending our
- * IP address - if we know it.
- * - REPLY packates are expected only after we asked
- * for the TFTP server's or the gateway's ethernet
- * address; so if we receive such a packet, we set
- * the server ethernet address
- */
- debug("Got ARP\n");
- arp = (ARP_t *)ip;
- if (len < ARP_HDR_SIZE) {
- printf("bad length %d < %d\n", len, ARP_HDR_SIZE);
- return;
- }
- if (ntohs(arp->ar_hrd) != ARP_ETHER)
- return;
- if (ntohs(arp->ar_pro) != PROT_IP)
- return;
- if (arp->ar_hln != 6)
- return;
- if (arp->ar_pln != 4)
- return;
- if (NetOurIP == 0)
- return;
- if (NetReadIP(&arp->ar_data[16]) != NetOurIP)
- return;
- switch (ntohs(arp->ar_op)) {
- case ARPOP_REQUEST:
- /* reply with our IP address */
- debug("Got ARP REQUEST, return our IP\n");
- pkt = (uchar *)et;
- pkt += NetSetEther(pkt, et->et_src, PROT_ARP);
- arp->ar_op = htons(ARPOP_REPLY);
- memcpy(&arp->ar_data[10], &arp->ar_data[0], 6);
- NetCopyIP(&arp->ar_data[16], &arp->ar_data[6]);
- memcpy(&arp->ar_data[0], NetOurEther, 6);
- NetCopyIP(&arp->ar_data[6], &NetOurIP);
- (void) eth_send((uchar *)et,
- (pkt - (uchar *)et) + ARP_HDR_SIZE);
- return;
- case ARPOP_REPLY: /* arp reply */
- /* are we waiting for a reply */
- if (!NetArpWaitPacketIP || !NetArpWaitPacketMAC)
- break;
-#ifdef CONFIG_KEEP_SERVERADDR
- if (NetServerIP == NetArpWaitPacketIP) {
- char buf[20];
- sprintf(buf, "%pM", arp->ar_data);
- setenv("serveraddr", buf);
- }
-#endif
- debug("Got ARP REPLY, set server/gtwy eth addr (%pM)\n",
- arp->ar_data);
- tmp = NetReadIP(&arp->ar_data[6]);
- /* matched waiting packet's address */
- if (tmp == NetArpWaitReplyIP) {
- debug("Got it\n");
- /* save address for later use */
- memcpy(NetArpWaitPacketMAC,
- &arp->ar_data[0], 6);
-#ifdef CONFIG_NETCONSOLE
- (*packetHandler)(0, 0, 0, 0, 0);
-#endif
- /* modify header, and transmit it */
- memcpy(((Ethernet_t *)NetArpWaitTxPacket)->
- et_dest, NetArpWaitPacketMAC, 6);
- (void) eth_send(NetArpWaitTxPacket,
- NetArpWaitTxPacketSize);
- /* no arp request pending now */
- NetArpWaitPacketIP = 0;
- NetArpWaitTxPacketSize = 0;
- NetArpWaitPacketMAC = NULL;
- }
- return;
- default:
- debug("Unexpected ARP opcode 0x%x\n",
- ntohs(arp->ar_op));
- return;
- }
- ArpReceive(et, ip, len);
break;
#ifdef CONFIG_CMD_RARP
1.6.0.2
Regards, Simon

Signed-off-by: Joe Hershberger joe.hershberger@ni.com Cc: Joe Hershberger joe.hershberger@gmail.com Cc: Simon Glass sjg@chromium.org Cc: Mike Frysinger vapier@gentoo.org --- Changes for v2: - Moved config fixes to a separate patch - Pushed #ifdef CONFIG_CMD_PING into header - Eliminated CamelCase
net/Makefile | 1 + net/net.c | 133 ++----------------------------------------------------- net/ping.c | 140 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ net/ping.h | 23 ++++++++++ 4 files changed, 168 insertions(+), 129 deletions(-) create mode 100644 net/ping.c create mode 100644 net/ping.h
diff --git a/net/Makefile b/net/Makefile index 0916a56..f1c4859 100644 --- a/net/Makefile +++ b/net/Makefile @@ -35,6 +35,7 @@ COBJS-$(CONFIG_CMD_NET) += eth.o COBJS-$(CONFIG_CMD_NET) += net.o COBJS-$(CONFIG_BOOTP_RANDOM_DELAY) += net_rand.o COBJS-$(CONFIG_CMD_NFS) += nfs.o +COBJS-$(CONFIG_CMD_PING) += ping.o COBJS-$(CONFIG_CMD_RARP) += rarp.o COBJS-$(CONFIG_CMD_SNTP) += sntp.o COBJS-$(CONFIG_CMD_NET) += tftp.o diff --git a/net/net.c b/net/net.c index f941f91..ef084ba 100644 --- a/net/net.c +++ b/net/net.c @@ -97,6 +97,7 @@ #if defined(CONFIG_CMD_DNS) #include "dns.h" #endif +#include "ping.h"
DECLARE_GLOBAL_DATA_PTR;
@@ -167,13 +168,6 @@ ushort NetOurNativeVLAN = 0xFFFF; /* Boot File name */ char BootFile[128];
-#if defined(CONFIG_CMD_PING) -/* the ip address to ping */ -IPaddr_t NetPingIP; - -static void PingStart(void); -#endif - #if defined(CONFIG_CMD_SNTP) /* NTP server IP address */ IPaddr_t NetNtpServerIP; @@ -358,7 +352,7 @@ restart: #endif #if defined(CONFIG_CMD_PING) case PING: - PingStart(); + ping_start(); break; #endif #if defined(CONFIG_CMD_NFS) @@ -653,94 +647,6 @@ NetSendUDPPacket(uchar *ether, IPaddr_t dest, int dport, int sport, int len) return 0; /* transmitted */ }
-#if defined(CONFIG_CMD_PING) -static ushort PingSeqNo; - -int PingSend(void) -{ - static uchar mac[6]; - IP_t *ip; - ushort *s; - uchar *pkt; - - /* XXX always send arp request */ - - memcpy(mac, NetEtherNullAddr, 6); - - debug("sending ARP for %08x\n", NetPingIP); - - NetArpWaitPacketIP = NetPingIP; - NetArpWaitPacketMAC = mac; - - pkt = NetArpWaitTxPacket; - pkt += NetSetEther(pkt, mac, PROT_IP); - - ip = (IP_t *)pkt; - - /* - * Construct an IP and ICMP header. - * (need to set no fragment bit - XXX) - */ - /* IP_HDR_SIZE / 4 (not including UDP) */ - ip->ip_hl_v = 0x45; - ip->ip_tos = 0; - ip->ip_len = htons(IP_HDR_SIZE_NO_UDP + 8); - ip->ip_id = htons(NetIPID++); - ip->ip_off = htons(IP_FLAGS_DFRAG); /* Don't fragment */ - ip->ip_ttl = 255; - ip->ip_p = 0x01; /* ICMP */ - ip->ip_sum = 0; - /* already in network byte order */ - NetCopyIP((void *)&ip->ip_src, &NetOurIP); - /* - "" - */ - NetCopyIP((void *)&ip->ip_dst, &NetPingIP); - ip->ip_sum = ~NetCksum((uchar *)ip, IP_HDR_SIZE_NO_UDP / 2); - - s = &ip->udp_src; /* XXX ICMP starts here */ - s[0] = htons(0x0800); /* echo-request, code */ - s[1] = 0; /* checksum */ - s[2] = 0; /* identifier */ - s[3] = htons(PingSeqNo++); /* sequence number */ - s[1] = ~NetCksum((uchar *)s, 8/2); - - /* size of the waiting packet */ - NetArpWaitTxPacketSize = - (pkt - NetArpWaitTxPacket) + IP_HDR_SIZE_NO_UDP + 8; - - /* and do the ARP request */ - NetArpWaitTry = 1; - NetArpWaitTimerStart = get_timer(0); - ArpRequest(); - return 1; /* waiting */ -} - -static void -PingTimeout(void) -{ - eth_halt(); - NetState = NETLOOP_FAIL; /* we did not get the reply */ -} - -static void -PingHandler(uchar *pkt, unsigned dest, IPaddr_t sip, unsigned src, - unsigned len) -{ - if (sip != NetPingIP) - return; - - NetState = NETLOOP_SUCCESS; -} - -static void PingStart(void) -{ - printf("Using %s device\n", eth_get_name()); - NetSetTimeout(10000UL, PingTimeout); - NetSetHandler(PingHandler); - - PingSend(); -} -#endif - #ifdef CONFIG_IP_DEFRAG /* * This function collects fragments in a single packet, according @@ -930,41 +836,10 @@ static void receive_icmp(IP_t *ip, int len, IPaddr_t src_ip, Ethernet_t *et) printf(" ICMP Host Redirect to %pI4 ", &icmph->un.gateway); break; + default: #if defined(CONFIG_CMD_PING) - case ICMP_ECHO_REPLY: - /* - * IP header OK. Pass the packet to the - * current handler. - */ - /* - * XXX point to ip packet - should this use - * packet_icmp_handler? - */ - (*packetHandler)((uchar *)ip, 0, src_ip, 0, 0); - break; - case ICMP_ECHO_REQUEST: - debug("Got ICMP ECHO REQUEST, return %d bytes\n", - ETHER_HDR_SIZE + len); - - memcpy(&et->et_dest[0], &et->et_src[0], 6); - memcpy(&et->et_src[0], NetOurEther, 6); - - ip->ip_sum = 0; - ip->ip_off = 0; - NetCopyIP((void *)&ip->ip_dst, &ip->ip_src); - NetCopyIP((void *)&ip->ip_src, &NetOurIP); - ip->ip_sum = ~NetCksum((uchar *)ip, - IP_HDR_SIZE_NO_UDP >> 1); - - icmph->type = ICMP_ECHO_REPLY; - icmph->checksum = 0; - icmph->checksum = ~NetCksum((uchar *)icmph, - (len - IP_HDR_SIZE_NO_UDP) >> 1); - (void) eth_send((uchar *)et, - ETHER_HDR_SIZE + len); - break; + ping_receive(et, ip, len); #endif - default: #ifdef CONFIG_CMD_TFTPPUT if (packet_icmp_handler) packet_icmp_handler(icmph->type, icmph->code, diff --git a/net/ping.c b/net/ping.c new file mode 100644 index 0000000..0e5b1da --- /dev/null +++ b/net/ping.c @@ -0,0 +1,140 @@ +/* + * Copied from Linux Monitor (LiMon) - Networking. + * + * Copyright 1994 - 2000 Neil Russell. + * (See License) + * Copyright 2000 Roland Borde + * Copyright 2000 Paolo Scaffardi + * Copyright 2000-2002 Wolfgang Denk, wd@denx.de + */ + +#include "ping.h" +#include "arp.h" + +static ushort PingSeqNo; + +/* The ip address to ping */ +IPaddr_t NetPingIP; + +static int ping_send(void) +{ + static uchar mac[6]; + IP_t *ip; + ushort *s; + uchar *pkt; + + /* XXX always send arp request */ + + memcpy(mac, NetEtherNullAddr, 6); + + debug("sending ARP for %pI4\n", &NetPingIP); + + NetArpWaitPacketIP = NetPingIP; + NetArpWaitPacketMAC = mac; + + pkt = NetArpWaitTxPacket; + pkt += NetSetEther(pkt, mac, PROT_IP); + + ip = (IP_t *)pkt; + + /* + * Construct an IP and ICMP header. + * (need to set no fragment bit - XXX) + */ + /* IP_HDR_SIZE / 4 (not including UDP) */ + ip->ip_hl_v = 0x45; + ip->ip_tos = 0; + ip->ip_len = htons(IP_HDR_SIZE_NO_UDP + 8); + ip->ip_id = htons(NetIPID++); + ip->ip_off = htons(IP_FLAGS_DFRAG); /* Don't fragment */ + ip->ip_ttl = 255; + ip->ip_p = 0x01; /* ICMP */ + ip->ip_sum = 0; + /* already in network byte order */ + NetCopyIP((void *)&ip->ip_src, &NetOurIP); + /* - "" - */ + NetCopyIP((void *)&ip->ip_dst, &NetPingIP); + ip->ip_sum = ~NetCksum((uchar *)ip, IP_HDR_SIZE_NO_UDP / 2); + + s = &ip->udp_src; /* XXX ICMP starts here */ + s[0] = htons(0x0800); /* echo-request, code */ + s[1] = 0; /* checksum */ + s[2] = 0; /* identifier */ + s[3] = htons(PingSeqNo++); /* sequence number */ + s[1] = ~NetCksum((uchar *)s, 8/2); + + /* size of the waiting packet */ + NetArpWaitTxPacketSize = + (pkt - NetArpWaitTxPacket) + IP_HDR_SIZE_NO_UDP + 8; + + /* and do the ARP request */ + NetArpWaitTry = 1; + NetArpWaitTimerStart = get_timer(0); + ArpRequest(); + return 1; /* waiting */ +} + +static void ping_timeout(void) +{ + eth_halt(); + NetState = NETLOOP_FAIL; /* we did not get the reply */ +} + +static void ping_handler(uchar *pkt, unsigned dest, IPaddr_t sip, + unsigned src, unsigned len) +{ + if (sip != NetPingIP) + return; + + NetState = NETLOOP_SUCCESS; +} + +void ping_start(void) +{ + printf("Using %s device\n", eth_get_name()); + NetSetTimeout(10000UL, ping_timeout); + NetSetHandler(ping_handler); + + ping_send(); +} + +void ping_receive(Ethernet_t *et, IP_t *ip, int len) +{ + ICMP_t *icmph = (ICMP_t *)&(ip->udp_src); + IPaddr_t src_ip; + + switch (icmph->type) { + case ICMP_ECHO_REPLY: + /* + * IP header OK. Pass the packet to the + * current handler. + */ + /* XXX point to ip packet */ + src_ip = NetReadIP((void *)&ip->ip_src); + NetGetHandler()((uchar *)ip, 0, src_ip, 0, 0); + return; + case ICMP_ECHO_REQUEST: + debug("Got ICMP ECHO REQUEST, return " + "%d bytes\n", ETHER_HDR_SIZE + len); + + memcpy(&et->et_dest[0], &et->et_src[0], 6); + memcpy(&et->et_src[0], NetOurEther, 6); + + ip->ip_sum = 0; + ip->ip_off = 0; + NetCopyIP((void *)&ip->ip_dst, &ip->ip_src); + NetCopyIP((void *)&ip->ip_src, &NetOurIP); + ip->ip_sum = ~NetCksum((uchar *)ip, + IP_HDR_SIZE_NO_UDP >> 1); + + icmph->type = ICMP_ECHO_REPLY; + icmph->checksum = 0; + icmph->checksum = ~NetCksum((uchar *)icmph, + (len - IP_HDR_SIZE_NO_UDP) >> 1); + (void) eth_send((uchar *)et, + ETHER_HDR_SIZE + len); + return; +/* default: + return;*/ + } +} diff --git a/net/ping.h b/net/ping.h new file mode 100644 index 0000000..94d434e --- /dev/null +++ b/net/ping.h @@ -0,0 +1,23 @@ +/* + * Copied from Linux Monitor (LiMon) - Networking. + * + * Copyright 1994 - 2000 Neil Russell. + * (See License) + * Copyright 2000 Roland Borde + * Copyright 2000 Paolo Scaffardi + * Copyright 2000-2002 Wolfgang Denk, wd@denx.de + */ + +#if defined(CONFIG_CMD_PING) + +#ifndef __PING_H__ +#define __PING_H__ + +#include <common.h> +#include <net.h> + +void ping_start(void); +void ping_receive(Ethernet_t *et, IP_t *ip, int len); + +#endif /* __PING_H__ */ +#endif

Hi Joe,
On Tue, Mar 27, 2012 at 4:42 PM, Joe Hershberger joe.hershberger@ni.com wrote:
Signed-off-by: Joe Hershberger joe.hershberger@ni.com Cc: Joe Hershberger joe.hershberger@gmail.com Cc: Simon Glass sjg@chromium.org Cc: Mike Frysinger vapier@gentoo.org
Acked-by: Simon Glass sjg@chromium.org
Changes for v2: - Moved config fixes to a separate patch - Pushed #ifdef CONFIG_CMD_PING into header - Eliminated CamelCase
net/Makefile | 1 + net/net.c | 133 ++----------------------------------------------------- net/ping.c | 140 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ net/ping.h | 23 ++++++++++ 4 files changed, 168 insertions(+), 129 deletions(-) create mode 100644 net/ping.c create mode 100644 net/ping.h
diff --git a/net/Makefile b/net/Makefile index 0916a56..f1c4859 100644 --- a/net/Makefile +++ b/net/Makefile @@ -35,6 +35,7 @@ COBJS-$(CONFIG_CMD_NET) += eth.o COBJS-$(CONFIG_CMD_NET) += net.o COBJS-$(CONFIG_BOOTP_RANDOM_DELAY) += net_rand.o COBJS-$(CONFIG_CMD_NFS) += nfs.o +COBJS-$(CONFIG_CMD_PING) += ping.o COBJS-$(CONFIG_CMD_RARP) += rarp.o COBJS-$(CONFIG_CMD_SNTP) += sntp.o COBJS-$(CONFIG_CMD_NET) += tftp.o diff --git a/net/net.c b/net/net.c index f941f91..ef084ba 100644 --- a/net/net.c +++ b/net/net.c @@ -97,6 +97,7 @@ #if defined(CONFIG_CMD_DNS) #include "dns.h" #endif +#include "ping.h"
DECLARE_GLOBAL_DATA_PTR;
@@ -167,13 +168,6 @@ ushort NetOurNativeVLAN = 0xFFFF; /* Boot File name */ char BootFile[128];
-#if defined(CONFIG_CMD_PING) -/* the ip address to ping */ -IPaddr_t NetPingIP;
-static void PingStart(void); -#endif
#if defined(CONFIG_CMD_SNTP) /* NTP server IP address */ IPaddr_t NetNtpServerIP; @@ -358,7 +352,7 @@ restart: #endif #if defined(CONFIG_CMD_PING) case PING:
- PingStart();
- ping_start();
break; #endif #if defined(CONFIG_CMD_NFS) @@ -653,94 +647,6 @@ NetSendUDPPacket(uchar *ether, IPaddr_t dest, int dport, int sport, int len) return 0; /* transmitted */ }
-#if defined(CONFIG_CMD_PING) -static ushort PingSeqNo;
-int PingSend(void) -{
- static uchar mac[6];
- IP_t *ip;
- ushort *s;
- uchar *pkt;
- /* XXX always send arp request */
- memcpy(mac, NetEtherNullAddr, 6);
- debug("sending ARP for %08x\n", NetPingIP);
- NetArpWaitPacketIP = NetPingIP;
- NetArpWaitPacketMAC = mac;
- pkt = NetArpWaitTxPacket;
- pkt += NetSetEther(pkt, mac, PROT_IP);
- ip = (IP_t *)pkt;
- /*
- * Construct an IP and ICMP header.
- * (need to set no fragment bit - XXX)
- */
- /* IP_HDR_SIZE / 4 (not including UDP) */
- ip->ip_hl_v = 0x45;
- ip->ip_tos = 0;
- ip->ip_len = htons(IP_HDR_SIZE_NO_UDP + 8);
- ip->ip_id = htons(NetIPID++);
- ip->ip_off = htons(IP_FLAGS_DFRAG); /* Don't fragment */
- ip->ip_ttl = 255;
- ip->ip_p = 0x01; /* ICMP */
- ip->ip_sum = 0;
- /* already in network byte order */
- NetCopyIP((void *)&ip->ip_src, &NetOurIP);
- /* - "" - */
- NetCopyIP((void *)&ip->ip_dst, &NetPingIP);
- ip->ip_sum = ~NetCksum((uchar *)ip, IP_HDR_SIZE_NO_UDP / 2);
- s = &ip->udp_src; /* XXX ICMP starts here */
- s[0] = htons(0x0800); /* echo-request, code */
- s[1] = 0; /* checksum */
- s[2] = 0; /* identifier */
- s[3] = htons(PingSeqNo++); /* sequence number */
- s[1] = ~NetCksum((uchar *)s, 8/2);
- /* size of the waiting packet */
- NetArpWaitTxPacketSize =
- (pkt - NetArpWaitTxPacket) + IP_HDR_SIZE_NO_UDP + 8;
- /* and do the ARP request */
- NetArpWaitTry = 1;
- NetArpWaitTimerStart = get_timer(0);
- ArpRequest();
- return 1; /* waiting */
-}
-static void -PingTimeout(void) -{
- eth_halt();
- NetState = NETLOOP_FAIL; /* we did not get the reply */
-}
-static void -PingHandler(uchar *pkt, unsigned dest, IPaddr_t sip, unsigned src,
- unsigned len)
-{
- if (sip != NetPingIP)
- return;
- NetState = NETLOOP_SUCCESS;
-}
-static void PingStart(void) -{
- printf("Using %s device\n", eth_get_name());
- NetSetTimeout(10000UL, PingTimeout);
- NetSetHandler(PingHandler);
- PingSend();
-} -#endif
#ifdef CONFIG_IP_DEFRAG /* * This function collects fragments in a single packet, according @@ -930,41 +836,10 @@ static void receive_icmp(IP_t *ip, int len, IPaddr_t src_ip, Ethernet_t *et) printf(" ICMP Host Redirect to %pI4 ", &icmph->un.gateway); break;
- default:
#if defined(CONFIG_CMD_PING)
- case ICMP_ECHO_REPLY:
- /*
- * IP header OK. Pass the packet to the
- * current handler.
- */
- /*
- * XXX point to ip packet - should this use
- * packet_icmp_handler?
- */
- (*packetHandler)((uchar *)ip, 0, src_ip, 0, 0);
- break;
- case ICMP_ECHO_REQUEST:
- debug("Got ICMP ECHO REQUEST, return %d bytes\n",
- ETHER_HDR_SIZE + len);
- memcpy(&et->et_dest[0], &et->et_src[0], 6);
- memcpy(&et->et_src[0], NetOurEther, 6);
- ip->ip_sum = 0;
- ip->ip_off = 0;
- NetCopyIP((void *)&ip->ip_dst, &ip->ip_src);
- NetCopyIP((void *)&ip->ip_src, &NetOurIP);
- ip->ip_sum = ~NetCksum((uchar *)ip,
- IP_HDR_SIZE_NO_UDP >> 1);
- icmph->type = ICMP_ECHO_REPLY;
- icmph->checksum = 0;
- icmph->checksum = ~NetCksum((uchar *)icmph,
- (len - IP_HDR_SIZE_NO_UDP) >> 1);
- (void) eth_send((uchar *)et,
- ETHER_HDR_SIZE + len);
- break;
- ping_receive(et, ip, len);
#endif
- default:
#ifdef CONFIG_CMD_TFTPPUT if (packet_icmp_handler) packet_icmp_handler(icmph->type, icmph->code, diff --git a/net/ping.c b/net/ping.c new file mode 100644 index 0000000..0e5b1da --- /dev/null +++ b/net/ping.c @@ -0,0 +1,140 @@ +/*
- Copied from Linux Monitor (LiMon) - Networking.
- Copyright 1994 - 2000 Neil Russell.
- (See License)
- Copyright 2000 Roland Borde
- Copyright 2000 Paolo Scaffardi
- Copyright 2000-2002 Wolfgang Denk, wd@denx.de
- */
+#include "ping.h" +#include "arp.h"
+static ushort PingSeqNo;
+/* The ip address to ping */ +IPaddr_t NetPingIP;
+static int ping_send(void) +{
- static uchar mac[6];
- IP_t *ip;
- ushort *s;
- uchar *pkt;
- /* XXX always send arp request */
- memcpy(mac, NetEtherNullAddr, 6);
- debug("sending ARP for %pI4\n", &NetPingIP);
- NetArpWaitPacketIP = NetPingIP;
- NetArpWaitPacketMAC = mac;
- pkt = NetArpWaitTxPacket;
- pkt += NetSetEther(pkt, mac, PROT_IP);
- ip = (IP_t *)pkt;
- /*
- * Construct an IP and ICMP header.
- * (need to set no fragment bit - XXX)
- */
- /* IP_HDR_SIZE / 4 (not including UDP) */
- ip->ip_hl_v = 0x45;
- ip->ip_tos = 0;
- ip->ip_len = htons(IP_HDR_SIZE_NO_UDP + 8);
- ip->ip_id = htons(NetIPID++);
- ip->ip_off = htons(IP_FLAGS_DFRAG); /* Don't fragment */
- ip->ip_ttl = 255;
- ip->ip_p = 0x01; /* ICMP */
- ip->ip_sum = 0;
- /* already in network byte order */
- NetCopyIP((void *)&ip->ip_src, &NetOurIP);
- /* - "" - */
- NetCopyIP((void *)&ip->ip_dst, &NetPingIP);
- ip->ip_sum = ~NetCksum((uchar *)ip, IP_HDR_SIZE_NO_UDP / 2);
- s = &ip->udp_src; /* XXX ICMP starts here */
- s[0] = htons(0x0800); /* echo-request, code */
- s[1] = 0; /* checksum */
- s[2] = 0; /* identifier */
- s[3] = htons(PingSeqNo++); /* sequence number */
- s[1] = ~NetCksum((uchar *)s, 8/2);
- /* size of the waiting packet */
- NetArpWaitTxPacketSize =
- (pkt - NetArpWaitTxPacket) + IP_HDR_SIZE_NO_UDP + 8;
- /* and do the ARP request */
- NetArpWaitTry = 1;
- NetArpWaitTimerStart = get_timer(0);
- ArpRequest();
- return 1; /* waiting */
+}
+static void ping_timeout(void) +{
- eth_halt();
- NetState = NETLOOP_FAIL; /* we did not get the reply */
+}
+static void ping_handler(uchar *pkt, unsigned dest, IPaddr_t sip,
- unsigned src, unsigned len)
+{
- if (sip != NetPingIP)
- return;
- NetState = NETLOOP_SUCCESS;
+}
+void ping_start(void) +{
- printf("Using %s device\n", eth_get_name());
- NetSetTimeout(10000UL, ping_timeout);
- NetSetHandler(ping_handler);
- ping_send();
+}
+void ping_receive(Ethernet_t *et, IP_t *ip, int len) +{
- ICMP_t *icmph = (ICMP_t *)&(ip->udp_src);
- IPaddr_t src_ip;
- switch (icmph->type) {
- case ICMP_ECHO_REPLY:
- /*
- * IP header OK. Pass the packet to the
- * current handler.
- */
- /* XXX point to ip packet */
- src_ip = NetReadIP((void *)&ip->ip_src);
- NetGetHandler()((uchar *)ip, 0, src_ip, 0, 0);
- return;
- case ICMP_ECHO_REQUEST:
- debug("Got ICMP ECHO REQUEST, return "
- "%d bytes\n", ETHER_HDR_SIZE + len);
- memcpy(&et->et_dest[0], &et->et_src[0], 6);
- memcpy(&et->et_src[0], NetOurEther, 6);
- ip->ip_sum = 0;
- ip->ip_off = 0;
- NetCopyIP((void *)&ip->ip_dst, &ip->ip_src);
- NetCopyIP((void *)&ip->ip_src, &NetOurIP);
- ip->ip_sum = ~NetCksum((uchar *)ip,
- IP_HDR_SIZE_NO_UDP >> 1);
- icmph->type = ICMP_ECHO_REPLY;
- icmph->checksum = 0;
- icmph->checksum = ~NetCksum((uchar *)icmph,
- (len - IP_HDR_SIZE_NO_UDP) >> 1);
- (void) eth_send((uchar *)et,
- ETHER_HDR_SIZE + len);
- return;
+/* default:
- return;*/
- }
+} diff --git a/net/ping.h b/net/ping.h new file mode 100644 index 0000000..94d434e --- /dev/null +++ b/net/ping.h @@ -0,0 +1,23 @@ +/*
- Copied from Linux Monitor (LiMon) - Networking.
- Copyright 1994 - 2000 Neil Russell.
- (See License)
- Copyright 2000 Roland Borde
- Copyright 2000 Paolo Scaffardi
- Copyright 2000-2002 Wolfgang Denk, wd@denx.de
- */
+#if defined(CONFIG_CMD_PING)
+#ifndef __PING_H__ +#define __PING_H__
+#include <common.h> +#include <net.h>
+void ping_start(void); +void ping_receive(Ethernet_t *et, IP_t *ip, int len);
Function comments would be nice
+#endif /* __PING_H__ */
+#endif
1.6.0.2
Regards, Simon

Signed-off-by: Joe Hershberger joe.hershberger@ni.com Cc: Joe Hershberger joe.hershberger@gmail.com Cc: Simon Glass sjg@chromium.org Cc: Mike Frysinger vapier@gentoo.org --- Changes for v2: - Moved void to the same line as function - Eliminate CamelCase in new function name
net/net.c | 27 +-------------------------- net/rarp.c | 45 +++++++++++++++++++++++++++++---------------- net/rarp.h | 6 +++--- 3 files changed, 33 insertions(+), 45 deletions(-)
diff --git a/net/net.c b/net/net.c index ef084ba..a812298 100644 --- a/net/net.c +++ b/net/net.c @@ -82,9 +82,7 @@ #include "arp.h" #include "bootp.h" #include "tftp.h" -#ifdef CONFIG_CMD_RARP #include "rarp.h" -#endif #include "nfs.h" #ifdef CONFIG_STATUS_LED #include <status_led.h> @@ -855,9 +853,6 @@ NetReceive(uchar *inpkt, int len) { Ethernet_t *et; IP_t *ip; -#ifdef CONFIG_CMD_RARP - ARP_t *arp; -#endif IPaddr_t tmp; IPaddr_t src_ip; int x; @@ -962,27 +957,7 @@ NetReceive(uchar *inpkt, int len)
#ifdef CONFIG_CMD_RARP case PROT_RARP: - debug("Got RARP\n"); - arp = (ARP_t *)ip; - if (len < ARP_HDR_SIZE) { - printf("bad length %d < %d\n", len, ARP_HDR_SIZE); - return; - } - - if ((ntohs(arp->ar_op) != RARPOP_REPLY) || - (ntohs(arp->ar_hrd) != ARP_ETHER) || - (ntohs(arp->ar_pro) != PROT_IP) || - (arp->ar_hln != 6) || (arp->ar_pln != 4)) { - - puts("invalid RARP header\n"); - } else { - NetCopyIP(&NetOurIP, &arp->ar_data[16]); - if (NetServerIP == 0) - NetCopyIP(&NetServerIP, &arp->ar_data[6]); - memcpy(NetServerEther, &arp->ar_data[0], 6); - - (*packetHandler)(0, 0, 0, 0, 0); - } + rarp_receive(ip, len); break; #endif case PROT_IP: diff --git a/net/rarp.c b/net/rarp.c index 5a813a2..9864468 100644 --- a/net/rarp.c +++ b/net/rarp.c @@ -36,26 +36,43 @@ # define TIMEOUT_COUNT (CONFIG_NET_RETRY_COUNT) #endif
- -int RarpTry; +int RarpTry;
/* * Handle a RARP received packet. */ -static void -RarpHandler(uchar *dummi0, unsigned dummi1, IPaddr_t sip, unsigned dummi2, - unsigned dummi3) +void rarp_receive(IP_t *ip, unsigned len) { - debug("Got good RARP\n"); - net_auto_load(); + ARP_t *arp; + + debug("Got RARP\n"); + arp = (ARP_t *)ip; + if (len < ARP_HDR_SIZE) { + printf("bad length %d < %d\n", len, ARP_HDR_SIZE); + return; + } + + if ((ntohs(arp->ar_op) != RARPOP_REPLY) || + (ntohs(arp->ar_hrd) != ARP_ETHER) || + (ntohs(arp->ar_pro) != PROT_IP) || + (arp->ar_hln != 6) || (arp->ar_pln != 4)) { + + puts("invalid RARP header\n"); + } else { + NetCopyIP(&NetOurIP, &arp->ar_data[16]); + if (NetServerIP == 0) + NetCopyIP(&NetServerIP, &arp->ar_data[6]); + memcpy(NetServerEther, &arp->ar_data[0], 6); + debug("Got good RARP\n"); + net_auto_load(); + } }
/* * Timeout on BOOTP request. */ -static void -RarpTimeout(void) +static void RarpTimeout(void) { if (RarpTry >= TIMEOUT_COUNT) { puts("\nRetry count exceeded; starting again\n"); @@ -67,10 +84,8 @@ RarpTimeout(void) }
-void -RarpRequest(void) +void RarpRequest(void) { - int i; uchar *pkt; ARP_t *rarp;
@@ -90,12 +105,10 @@ RarpRequest(void) memcpy(&rarp->ar_data[6], &NetOurIP, 4); /* source IP addr */ /* dest ET addr = source ET addr ??*/ memcpy(&rarp->ar_data[10], NetOurEther, 6); - /* dest. IP addr set to broadcast */ - for (i = 0; i <= 3; i++) - rarp->ar_data[16 + i] = 0xff; + /* dest IP addr set to broadcast */ + memset(&rarp->ar_data[16], 0xff, 4);
NetSendPacket(NetTxPacket, (pkt - NetTxPacket) + ARP_HDR_SIZE);
NetSetTimeout(TIMEOUT, RarpTimeout); - NetSetHandler(RarpHandler); } diff --git a/net/rarp.h b/net/rarp.h index 4e92d80..e5a6c39 100644 --- a/net/rarp.h +++ b/net/rarp.h @@ -21,14 +21,12 @@ * MA 02111-1307 USA */
+#if defined(CONFIG_CMD_RARP)
#ifndef __RARP_H__ #define __RARP_H__
-#ifndef __NET_H__ #include <net.h> -#endif /* __NET_H__ */ -
/**********************************************************************/ /* @@ -37,8 +35,10 @@
extern int RarpTry;
+extern void rarp_receive(IP_t *ip, unsigned len); extern void RarpRequest(void); /* Send a RARP request */
/**********************************************************************/
#endif /* __RARP_H__ */ +#endif

Hi Joe,
On Wed, Mar 28, 2012 at 12:42 PM, Joe Hershberger joe.hershberger@ni.comwrote:
Signed-off-by: Joe Hershberger joe.hershberger@ni.com Cc: Joe Hershberger joe.hershberger@gmail.com Cc: Simon Glass sjg@chromium.org Cc: Mike Frysinger vapier@gentoo.org
Just a few nits, but otherwise:
Acked-by: Simon Glass sjg@chromium.org
Changes for v2:
- Moved void to the same line as function
- Eliminate CamelCase in new function name
net/net.c | 27 +-------------------------- net/rarp.c | 45 +++++++++++++++++++++++++++++---------------- net/rarp.h | 6 +++--- 3 files changed, 33 insertions(+), 45 deletions(-)
diff --git a/net/net.c b/net/net.c index ef084ba..a812298 100644 --- a/net/net.c +++ b/net/net.c @@ -82,9 +82,7 @@ #include "arp.h" #include "bootp.h" #include "tftp.h" -#ifdef CONFIG_CMD_RARP #include "rarp.h" -#endif #include "nfs.h" #ifdef CONFIG_STATUS_LED #include <status_led.h> @@ -855,9 +853,6 @@ NetReceive(uchar *inpkt, int len) { Ethernet_t *et; IP_t *ip; -#ifdef CONFIG_CMD_RARP
ARP_t *arp;
-#endif IPaddr_t tmp; IPaddr_t src_ip; int x; @@ -962,27 +957,7 @@ NetReceive(uchar *inpkt, int len)
#ifdef CONFIG_CMD_RARP case PROT_RARP:
debug("Got RARP\n");
arp = (ARP_t *)ip;
if (len < ARP_HDR_SIZE) {
printf("bad length %d < %d\n", len, ARP_HDR_SIZE);
return;
}
if ((ntohs(arp->ar_op) != RARPOP_REPLY) ||
(ntohs(arp->ar_hrd) != ARP_ETHER) ||
(ntohs(arp->ar_pro) != PROT_IP) ||
(arp->ar_hln != 6) || (arp->ar_pln != 4)) {
puts("invalid RARP header\n");
} else {
NetCopyIP(&NetOurIP, &arp->ar_data[16]);
if (NetServerIP == 0)
NetCopyIP(&NetServerIP, &arp->ar_data[6]);
memcpy(NetServerEther, &arp->ar_data[0], 6);
(*packetHandler)(0, 0, 0, 0, 0);
}
rarp_receive(ip, len); break;
#endif case PROT_IP: diff --git a/net/rarp.c b/net/rarp.c index 5a813a2..9864468 100644 --- a/net/rarp.c +++ b/net/rarp.c @@ -36,26 +36,43 @@ # define TIMEOUT_COUNT (CONFIG_NET_RETRY_COUNT) #endif
-int RarpTry; +int RarpTry;
/*
Handle a RARP received packet.
*/ -static void -RarpHandler(uchar *dummi0, unsigned dummi1, IPaddr_t sip, unsigned dummi2,
unsigned dummi3)
+void rarp_receive(IP_t *ip, unsigned len) {
debug("Got good RARP\n");
net_auto_load();
ARP_t *arp;
debug("Got RARP\n");
arp = (ARP_t *)ip;
if (len < ARP_HDR_SIZE) {
printf("bad length %d < %d\n", len, ARP_HDR_SIZE);
return;
}
if ((ntohs(arp->ar_op) != RARPOP_REPLY) ||
(ntohs(arp->ar_hrd) != ARP_ETHER) ||
(ntohs(arp->ar_pro) != PROT_IP) ||
(arp->ar_hln != 6) || (arp->ar_pln != 4)) {
puts("invalid RARP header\n");
} else {
NetCopyIP(&NetOurIP, &arp->ar_data[16]);
if (NetServerIP == 0)
NetCopyIP(&NetServerIP, &arp->ar_data[6]);
memcpy(NetServerEther, &arp->ar_data[0], 6);
debug("Got good RARP\n");
net_auto_load();
}
}
/*
Timeout on BOOTP request.
*/ -static void -RarpTimeout(void) +static void RarpTimeout(void) { if (RarpTry >= TIMEOUT_COUNT) { puts("\nRetry count exceeded; starting again\n"); @@ -67,10 +84,8 @@ RarpTimeout(void) }
-void -RarpRequest(void) +void RarpRequest(void) {
int i; uchar *pkt; ARP_t *rarp;
@@ -90,12 +105,10 @@ RarpRequest(void) memcpy(&rarp->ar_data[6], &NetOurIP, 4); /* source IP addr */ /* dest ET addr = source ET addr ??*/ memcpy(&rarp->ar_data[10], NetOurEther, 6);
/* dest. IP addr set to broadcast */
for (i = 0; i <= 3; i++)
rarp->ar_data[16 + i] = 0xff;
/* dest IP addr set to broadcast */
memset(&rarp->ar_data[16], 0xff, 4);
Remove extra spaces perhaps
NetSendPacket(NetTxPacket, (pkt - NetTxPacket) + ARP_HDR_SIZE); NetSetTimeout(TIMEOUT, RarpTimeout);
NetSetHandler(RarpHandler);
} diff --git a/net/rarp.h b/net/rarp.h index 4e92d80..e5a6c39 100644 --- a/net/rarp.h +++ b/net/rarp.h @@ -21,14 +21,12 @@
- MA 02111-1307 USA
*/
+#if defined(CONFIG_CMD_RARP)
#ifndef __RARP_H__ #define __RARP_H__
-#ifndef __NET_H__ #include <net.h> -#endif /* __NET_H__ */
/**********************************************************************/ /* @@ -37,8 +35,10 @@
extern int RarpTry;
+extern void rarp_receive(IP_t *ip, unsigned len);
How about a comment on this?
extern void RarpRequest(void); /* Send a RARP request */
/**********************************************************************/
#endif /* __RARP_H__ */
+#endif
1.6.0.2

Rename IP header related things to IP_UDP. The existing definition of IP_t includes UDP header, so name it to accurately describe the structure. Also add a structure that only contains IP header fields to be used by functions that don't need UDP.
Signed-off-by: Joe Hershberger joe.hershberger@ni.com Cc: Joe Hershberger joe.hershberger@gmail.com Cc: Simon Glass sjg@chromium.org Cc: Mike Frysinger vapier@gentoo.org --- Changes for v2: - Split apart from "Un-typedef variables in net" - Created a new IP_UDP_hdr struct and removed UDP members from IP_hdr - Renamed structs from *_t to *_hdr since they are nolonger types
drivers/net/netconsole.c | 4 +- include/net.h | 33 +++++++++++++++----- net/arp.c | 2 +- net/arp.h | 2 +- net/bootp.c | 6 ++-- net/bootp.h | 2 +- net/dns.c | 2 +- net/net.c | 78 +++++++++++++++++++++++---------------------- net/nfs.c | 2 +- net/ping.c | 16 +++++----- net/ping.h | 2 +- net/rarp.c | 2 +- net/rarp.h | 2 +- net/sntp.c | 2 +- net/tftp.c | 2 +- 15 files changed, 88 insertions(+), 69 deletions(-)
diff --git a/drivers/net/netconsole.c b/drivers/net/netconsole.c index 7acd5b0..ba02fd7 100644 --- a/drivers/net/netconsole.c +++ b/drivers/net/netconsole.c @@ -69,7 +69,7 @@ void NcStart(void) /* send arp request */ uchar *pkt; NetSetHandler(nc_wait_arp_handler); - pkt = (uchar *)NetTxPacket + NetEthHdrSize() + IP_HDR_SIZE; + pkt = (uchar *)NetTxPacket + NetEthHdrSize() + IP_UDP_HDR_SIZE; memcpy(pkt, output_packet, output_packet_len); NetSendUDPPacket(nc_ether, nc_ip, nc_port, nc_port, output_packet_len); @@ -131,7 +131,7 @@ static void nc_send_packet(const char *buf, int len) return; inited = 1; } - pkt = (uchar *)NetTxPacket + NetEthHdrSize() + IP_HDR_SIZE; + pkt = (uchar *)NetTxPacket + NetEthHdrSize() + IP_UDP_HDR_SIZE; memcpy(pkt, buf, len); ether = nc_ether; ip = nc_ip; diff --git a/include/net.h b/include/net.h index 7fc0b60..e9b398a 100644 --- a/include/net.h +++ b/include/net.h @@ -195,7 +195,7 @@ typedef struct { /* * Internet Protocol (IP) header. */ -typedef struct { +struct IP_hdr { uchar ip_hl_v; /* header length and version */ uchar ip_tos; /* type of service */ ushort ip_len; /* total length */ @@ -206,11 +206,7 @@ typedef struct { ushort ip_sum; /* checksum */ IPaddr_t ip_src; /* Source IP address */ IPaddr_t ip_dst; /* Destination IP address */ - ushort udp_src; /* UDP source port */ - ushort udp_dst; /* UDP destination port */ - ushort udp_len; /* Length of UDP packet */ - ushort udp_xsum; /* Checksum */ -} IP_t; +};
#define IP_OFFS 0x1fff /* ip offset *= 8 */ #define IP_FLAGS 0xe000 /* first 3 bits */ @@ -218,9 +214,30 @@ typedef struct { #define IP_FLAGS_DFRAG 0x4000 /* don't fragments */ #define IP_FLAGS_MFRAG 0x2000 /* more fragments */
-#define IP_HDR_SIZE_NO_UDP (sizeof(IP_t) - 8) -#define IP_HDR_SIZE (sizeof(IP_t)) +#define IP_HDR_SIZE (sizeof(struct IP_hdr)) + +/* + * Internet Protocol (IP) + UDP header. + */ +struct IP_UDP_hdr { + uchar ip_hl_v; /* header length and version */ + uchar ip_tos; /* type of service */ + ushort ip_len; /* total length */ + ushort ip_id; /* identification */ + ushort ip_off; /* fragment offset field */ + uchar ip_ttl; /* time to live */ + uchar ip_p; /* protocol */ + ushort ip_sum; /* checksum */ + IPaddr_t ip_src; /* Source IP address */ + IPaddr_t ip_dst; /* Destination IP address */ + ushort udp_src; /* UDP source port */ + ushort udp_dst; /* UDP destination port */ + ushort udp_len; /* Length of UDP packet */ + ushort udp_xsum; /* Checksum */ +};
+#define IP_UDP_HDR_SIZE (sizeof(struct IP_UDP_hdr)) +#define UDP_HDR_SIZE (IP_UDP_HDR_SIZE - IP_HDR_SIZE)
/* * Address Resolution Protocol (ARP) header. diff --git a/net/arp.c b/net/arp.c index f75217c..d918810 100644 --- a/net/arp.c +++ b/net/arp.c @@ -113,7 +113,7 @@ void ArpTimeoutCheck(void) } }
-void ArpReceive(Ethernet_t *et, IP_t *ip, int len) +void ArpReceive(Ethernet_t *et, struct IP_UDP_hdr *ip, int len) { ARP_t *arp; IPaddr_t tmp; diff --git a/net/arp.h b/net/arp.h index 4016a90..3c27348 100644 --- a/net/arp.h +++ b/net/arp.h @@ -25,6 +25,6 @@ extern int NetArpWaitTry; void ArpInit(void); void ArpRequest(void); void ArpTimeoutCheck(void); -void ArpReceive(Ethernet_t *et, IP_t *ip, int len); +void ArpReceive(Ethernet_t *et, struct IP_UDP_hdr *ip, int len);
#endif /* __ARP_H__ */ diff --git a/net/bootp.c b/net/bootp.c index 5949e77..f5f9119 100644 --- a/net/bootp.c +++ b/net/bootp.c @@ -623,8 +623,8 @@ BootpRequest(void) */ /* NetSetIP(pkt, 0xFFFFFFFFL, PORT_BOOTPS, PORT_BOOTPC, sizeof (struct Bootp_t)); */ - iphdr = pkt; /* We need this later for NetSetIP() */ - pkt += IP_HDR_SIZE; + iphdr = pkt; /* We need this later for NetSetUDPHeader() */ + pkt += IP_UDP_HDR_SIZE;
bp = (struct Bootp_t *)pkt; bp->bp_op = OP_BOOTREQUEST; @@ -809,7 +809,7 @@ static void DhcpSendRequestPkt(struct Bootp_t *bp_offer) pkt += NetSetEther(pkt, NetBcastAddr, PROT_IP);
iphdr = pkt; /* We'll need this later to set proper pkt size */ - pkt += IP_HDR_SIZE; + pkt += IP_UDP_HDR_SIZE;
bp = (struct Bootp_t *)pkt; bp->bp_op = OP_BOOTREQUEST; diff --git a/net/bootp.h b/net/bootp.h index bf4e875..1cf9a02 100644 --- a/net/bootp.h +++ b/net/bootp.h @@ -52,7 +52,7 @@ struct Bootp_t { };
#define BOOTP_HDR_SIZE sizeof(struct Bootp_t) -#define BOOTP_SIZE (ETHER_HDR_SIZE + IP_HDR_SIZE + BOOTP_HDR_SIZE) +#define BOOTP_SIZE (ETHER_HDR_SIZE + IP_UDP_HDR_SIZE + BOOTP_HDR_SIZE)
/**********************************************************************/ /* diff --git a/net/dns.c b/net/dns.c index 7a3f1f9..cc7ed51 100644 --- a/net/dns.c +++ b/net/dns.c @@ -45,7 +45,7 @@ DnsSend(void) enum dns_query_type qtype = DNS_A_RECORD;
name = NetDNSResolve; - pkt = p = (uchar *)(NetTxPacket + NetEthHdrSize() + IP_HDR_SIZE); + pkt = p = (uchar *)(NetTxPacket + NetEthHdrSize() + IP_UDP_HDR_SIZE);
/* Prepare DNS packet header */ header = (struct header *) pkt; diff --git a/net/net.c b/net/net.c index a812298..35ead0e 100644 --- a/net/net.c +++ b/net/net.c @@ -621,12 +621,13 @@ NetSendUDPPacket(uchar *ether, IPaddr_t dest, int dport, int sport, int len) pkt += NetSetEther(pkt, NetArpWaitPacketMAC, PROT_IP);
NetSetIP(pkt, dest, dport, sport, len); - memcpy(pkt + IP_HDR_SIZE, (uchar *)NetTxPacket + - (pkt - (uchar *)NetArpWaitTxPacket) + IP_HDR_SIZE, len); + memcpy(pkt + IP_UDP_HDR_SIZE, (uchar *)NetTxPacket + + (pkt - (uchar *)NetArpWaitTxPacket) + + IP_UDP_HDR_SIZE, len);
/* size of the waiting packet */ NetArpWaitTxPacketSize = (pkt - NetArpWaitTxPacket) + - IP_HDR_SIZE + len; + IP_UDP_HDR_SIZE + len;
/* and do the ARP request */ NetArpWaitTry = 1; @@ -640,7 +641,7 @@ NetSendUDPPacket(uchar *ether, IPaddr_t dest, int dport, int sport, int len) pkt = (uchar *)NetTxPacket; pkt += NetSetEther(pkt, ether, PROT_IP); NetSetIP(pkt, dest, dport, sport, len); - (void) eth_send(NetTxPacket, (pkt - NetTxPacket) + IP_HDR_SIZE + len); + eth_send(NetTxPacket, (pkt - NetTxPacket) + IP_UDP_HDR_SIZE + len);
return 0; /* transmitted */ } @@ -664,7 +665,7 @@ NetSendUDPPacket(uchar *ether, IPaddr_t dest, int dport, int sport, int len) static struct rpc_t rpc_specimen; #define IP_PKTSIZE (CONFIG_NET_MAXDEFRAG + sizeof(rpc_specimen.u.reply))
-#define IP_MAXUDP (IP_PKTSIZE - IP_HDR_SIZE_NO_UDP) +#define IP_MAXUDP (IP_PKTSIZE - IP_HDR_SIZE)
/* * this is the packet being assembled, either data or frag control. @@ -678,22 +679,22 @@ struct hole { u16 unused; };
-static IP_t *__NetDefragment(IP_t *ip, int *lenp) +static struct IP_UDP_hdr *__NetDefragment(struct IP_UDP_hdr *ip, int *lenp) { static uchar pkt_buff[IP_PKTSIZE] __aligned(PKTALIGN); static u16 first_hole, total_len; struct hole *payload, *thisfrag, *h, *newh; - IP_t *localip = (IP_t *)pkt_buff; + struct IP_UDP_hdr *localip = (struct IP_UDP_hdr *)pkt_buff; uchar *indata = (uchar *)ip; int offset8, start, len, done = 0; u16 ip_off = ntohs(ip->ip_off);
/* payload starts after IP header, this fragment is in there */ - payload = (struct hole *)(pkt_buff + IP_HDR_SIZE_NO_UDP); + payload = (struct hole *)(pkt_buff + IP_HDR_SIZE); offset8 = (ip_off & IP_OFFS); thisfrag = payload + offset8; start = offset8 * 8; - len = ntohs(ip->ip_len) - IP_HDR_SIZE_NO_UDP; + len = ntohs(ip->ip_len) - IP_HDR_SIZE;
if (start + len > IP_MAXUDP) /* fragment extends too far */ return NULL; @@ -706,7 +707,7 @@ static IP_t *__NetDefragment(IP_t *ip, int *lenp) payload[0].prev_hole = 0; first_hole = 0; /* any IP header will work, copy the first we received */ - memcpy(localip, ip, IP_HDR_SIZE_NO_UDP); + memcpy(localip, ip, IP_HDR_SIZE); }
/* @@ -789,16 +790,16 @@ static IP_t *__NetDefragment(IP_t *ip, int *lenp) }
/* finally copy this fragment and possibly return whole packet */ - memcpy((uchar *)thisfrag, indata + IP_HDR_SIZE_NO_UDP, len); + memcpy((uchar *)thisfrag, indata + IP_HDR_SIZE, len); if (!done) return NULL;
localip->ip_len = htons(total_len); - *lenp = total_len + IP_HDR_SIZE_NO_UDP; + *lenp = total_len + IP_HDR_SIZE; return localip; }
-static inline IP_t *NetDefragment(IP_t *ip, int *lenp) +static inline struct IP_UDP_hdr *NetDefragment(struct IP_UDP_hdr *ip, int *lenp) { u16 ip_off = ntohs(ip->ip_off); if (!(ip_off & (IP_OFFS | IP_FLAGS_MFRAG))) @@ -808,7 +809,7 @@ static inline IP_t *NetDefragment(IP_t *ip, int *lenp)
#else /* !CONFIG_IP_DEFRAG */
-static inline IP_t *NetDefragment(IP_t *ip, int *lenp) +static inline struct IP_UDP_hdr *NetDefragment(struct IP_UDP_hdr *ip, int *lenp) { u16 ip_off = ntohs(ip->ip_off); if (!(ip_off & (IP_OFFS | IP_FLAGS_MFRAG))) @@ -823,7 +824,8 @@ static inline IP_t *NetDefragment(IP_t *ip, int *lenp) * * @parma ip IP packet containing the ICMP */ -static void receive_icmp(IP_t *ip, int len, IPaddr_t src_ip, Ethernet_t *et) +static void receive_icmp(struct IP_UDP_hdr *ip, int len, + IPaddr_t src_ip, Ethernet_t *et) { ICMP_t *icmph = (ICMP_t *)&ip->udp_src;
@@ -852,7 +854,7 @@ void NetReceive(uchar *inpkt, int len) { Ethernet_t *et; - IP_t *ip; + struct IP_UDP_hdr *ip; IPaddr_t tmp; IPaddr_t src_ip; int x; @@ -900,11 +902,11 @@ NetReceive(uchar *inpkt, int len) */ x = ntohs(et->et_prot);
- ip = (IP_t *)(inpkt + E802_HDR_SIZE); + ip = (struct IP_UDP_hdr *)(inpkt + E802_HDR_SIZE); len -= E802_HDR_SIZE;
} else if (x != PROT_VLAN) { /* normal packet */ - ip = (IP_t *)(inpkt + ETHER_HDR_SIZE); + ip = (struct IP_UDP_hdr *)(inpkt + ETHER_HDR_SIZE); len -= ETHER_HDR_SIZE;
} else { /* VLAN packet */ @@ -928,7 +930,7 @@ NetReceive(uchar *inpkt, int len) vlanid = cti & VLAN_IDMASK; x = ntohs(vet->vet_type);
- ip = (IP_t *)(inpkt + VLAN_ETHER_HDR_SIZE); + ip = (struct IP_UDP_hdr *)(inpkt + VLAN_ETHER_HDR_SIZE); len -= VLAN_ETHER_HDR_SIZE; }
@@ -963,8 +965,9 @@ NetReceive(uchar *inpkt, int len) case PROT_IP: debug("Got IP\n"); /* Before we start poking the header, make sure it is there */ - if (len < IP_HDR_SIZE) { - debug("len bad %d < %lu\n", len, (ulong)IP_HDR_SIZE); + if (len < IP_UDP_HDR_SIZE) { + debug("len bad %d < %lu\n", len, + (ulong)IP_UDP_HDR_SIZE); return; } /* Check the packet length */ @@ -982,7 +985,7 @@ NetReceive(uchar *inpkt, int len) if ((ip->ip_hl_v & 0x0f) > 0x05) return; /* Check the Checksum of the header */ - if (!NetCksumOk((uchar *)ip, IP_HDR_SIZE_NO_UDP / 2)) { + if (!NetCksumOk((uchar *)ip, IP_HDR_SIZE / 2)) { puts("checksum bad\n"); return; } @@ -1076,19 +1079,19 @@ NetReceive(uchar *inpkt, int len)
#ifdef CONFIG_NETCONSOLE - nc_input_packet((uchar *)ip + IP_HDR_SIZE, - ntohs(ip->udp_dst), - ntohs(ip->udp_src), - ntohs(ip->udp_len) - 8); + nc_input_packet((uchar *)ip + IP_UDP_HDR_SIZE, + ntohs(ip->udp_dst), + ntohs(ip->udp_src), + ntohs(ip->udp_len) - UDP_HDR_SIZE); #endif /* * IP header OK. Pass the packet to the current handler. */ - (*packetHandler)((uchar *)ip + IP_HDR_SIZE, - ntohs(ip->udp_dst), - src_ip, - ntohs(ip->udp_src), - ntohs(ip->udp_len) - 8); + (*packetHandler)((uchar *)ip + IP_UDP_HDR_SIZE, + ntohs(ip->udp_dst), + src_ip, + ntohs(ip->udp_src), + ntohs(ip->udp_len) - UDP_HDR_SIZE); break; } } @@ -1239,10 +1242,9 @@ NetSetEther(uchar *xet, uchar * addr, uint prot) } }
-void -NetSetIP(uchar *xip, IPaddr_t dest, int dport, int sport, int len) +void NetSetIP(uchar *xip, IPaddr_t dest, int dport, int sport, int len) { - IP_t *ip = (IP_t *)xip; + struct IP_UDP_hdr *ip = (struct IP_UDP_hdr *)xip;
/* * If the data is an odd number of bytes, zero the @@ -1250,7 +1252,7 @@ NetSetIP(uchar *xip, IPaddr_t dest, int dport, int sport, int len) * will work. */ if (len & 1) - xip[IP_HDR_SIZE + len] = 0; + xip[IP_UDP_HDR_SIZE + len] = 0;
/* * Construct an IP and UDP header. @@ -1259,7 +1261,7 @@ NetSetIP(uchar *xip, IPaddr_t dest, int dport, int sport, int len) /* IP_HDR_SIZE / 4 (not including UDP) */ ip->ip_hl_v = 0x45; ip->ip_tos = 0; - ip->ip_len = htons(IP_HDR_SIZE + len); + ip->ip_len = htons(IP_UDP_HDR_SIZE + len); ip->ip_id = htons(NetIPID++); ip->ip_off = htons(IP_FLAGS_DFRAG); /* Don't fragment */ ip->ip_ttl = 255; @@ -1271,9 +1273,9 @@ NetSetIP(uchar *xip, IPaddr_t dest, int dport, int sport, int len) NetCopyIP((void *)&ip->ip_dst, &dest); ip->udp_src = htons(sport); ip->udp_dst = htons(dport); - ip->udp_len = htons(8 + len); + ip->udp_len = htons(UDP_HDR_SIZE + len); ip->udp_xsum = 0; - ip->ip_sum = ~NetCksum((uchar *)ip, IP_HDR_SIZE_NO_UDP / 2); + ip->ip_sum = ~NetCksum((uchar *)ip, IP_HDR_SIZE / 2); }
void copy_filename(char *dst, const char *src, int size) diff --git a/net/nfs.c b/net/nfs.c index 54f56c4..b6188fe 100644 --- a/net/nfs.c +++ b/net/nfs.c @@ -189,7 +189,7 @@ rpc_req(int rpc_prog, int rpc_proc, uint32_t *data, int datalen)
pktlen = (char *)p + datalen*sizeof(uint32_t) - (char *)&pkt;
- memcpy((char *)NetTxPacket + NetEthHdrSize() + IP_HDR_SIZE, + memcpy((char *)NetTxPacket + NetEthHdrSize() + IP_UDP_HDR_SIZE, (char *)&pkt, pktlen);
if (rpc_prog == PROG_PORTMAP) diff --git a/net/ping.c b/net/ping.c index 0e5b1da..fe4cead 100644 --- a/net/ping.c +++ b/net/ping.c @@ -19,7 +19,7 @@ IPaddr_t NetPingIP; static int ping_send(void) { static uchar mac[6]; - IP_t *ip; + struct IP_UDP_hdr *ip; ushort *s; uchar *pkt;
@@ -35,7 +35,7 @@ static int ping_send(void) pkt = NetArpWaitTxPacket; pkt += NetSetEther(pkt, mac, PROT_IP);
- ip = (IP_t *)pkt; + ip = (struct IP_UDP_hdr *)pkt;
/* * Construct an IP and ICMP header. @@ -44,7 +44,7 @@ static int ping_send(void) /* IP_HDR_SIZE / 4 (not including UDP) */ ip->ip_hl_v = 0x45; ip->ip_tos = 0; - ip->ip_len = htons(IP_HDR_SIZE_NO_UDP + 8); + ip->ip_len = htons(IP_HDR_SIZE + 8); ip->ip_id = htons(NetIPID++); ip->ip_off = htons(IP_FLAGS_DFRAG); /* Don't fragment */ ip->ip_ttl = 255; @@ -54,7 +54,7 @@ static int ping_send(void) NetCopyIP((void *)&ip->ip_src, &NetOurIP); /* - "" - */ NetCopyIP((void *)&ip->ip_dst, &NetPingIP); - ip->ip_sum = ~NetCksum((uchar *)ip, IP_HDR_SIZE_NO_UDP / 2); + ip->ip_sum = ~NetCksum((uchar *)ip, IP_HDR_SIZE / 2);
s = &ip->udp_src; /* XXX ICMP starts here */ s[0] = htons(0x0800); /* echo-request, code */ @@ -65,7 +65,7 @@ static int ping_send(void)
/* size of the waiting packet */ NetArpWaitTxPacketSize = - (pkt - NetArpWaitTxPacket) + IP_HDR_SIZE_NO_UDP + 8; + (pkt - NetArpWaitTxPacket) + IP_HDR_SIZE + 8;
/* and do the ARP request */ NetArpWaitTry = 1; @@ -98,7 +98,7 @@ void ping_start(void) ping_send(); }
-void ping_receive(Ethernet_t *et, IP_t *ip, int len) +void ping_receive(Ethernet_t *et, struct IP_UDP_hdr *ip, int len) { ICMP_t *icmph = (ICMP_t *)&(ip->udp_src); IPaddr_t src_ip; @@ -125,12 +125,12 @@ void ping_receive(Ethernet_t *et, IP_t *ip, int len) NetCopyIP((void *)&ip->ip_dst, &ip->ip_src); NetCopyIP((void *)&ip->ip_src, &NetOurIP); ip->ip_sum = ~NetCksum((uchar *)ip, - IP_HDR_SIZE_NO_UDP >> 1); + IP_HDR_SIZE >> 1);
icmph->type = ICMP_ECHO_REPLY; icmph->checksum = 0; icmph->checksum = ~NetCksum((uchar *)icmph, - (len - IP_HDR_SIZE_NO_UDP) >> 1); + (len - IP_HDR_SIZE) >> 1); (void) eth_send((uchar *)et, ETHER_HDR_SIZE + len); return; diff --git a/net/ping.h b/net/ping.h index 94d434e..dd97b58 100644 --- a/net/ping.h +++ b/net/ping.h @@ -17,7 +17,7 @@ #include <net.h>
void ping_start(void); -void ping_receive(Ethernet_t *et, IP_t *ip, int len); +void ping_receive(Ethernet_t *et, struct IP_UDP_hdr *ip, int len);
#endif /* __PING_H__ */ #endif diff --git a/net/rarp.c b/net/rarp.c index 9864468..b30c812 100644 --- a/net/rarp.c +++ b/net/rarp.c @@ -41,7 +41,7 @@ int RarpTry; /* * Handle a RARP received packet. */ -void rarp_receive(IP_t *ip, unsigned len) +void rarp_receive(struct IP_UDP_hdr *ip, unsigned len) { ARP_t *arp;
diff --git a/net/rarp.h b/net/rarp.h index e5a6c39..ac58881 100644 --- a/net/rarp.h +++ b/net/rarp.h @@ -35,7 +35,7 @@
extern int RarpTry;
-extern void rarp_receive(IP_t *ip, unsigned len); +extern void rarp_receive(struct IP_UDP_hdr *ip, unsigned len); extern void RarpRequest(void); /* Send a RARP request */
/**********************************************************************/ diff --git a/net/sntp.c b/net/sntp.c index 6dd0dbd..7997f98 100644 --- a/net/sntp.c +++ b/net/sntp.c @@ -31,7 +31,7 @@ SntpSend(void) pkt.vn = NTP_VERSION; pkt.mode = NTP_MODE_CLIENT;
- memcpy((char *)NetTxPacket + NetEthHdrSize() + IP_HDR_SIZE, + memcpy((char *)NetTxPacket + NetEthHdrSize() + IP_UDP_HDR_SIZE, (char *)&pkt, pktlen);
SntpOurPort = 10000 + (get_timer(0) % 4096); diff --git a/net/tftp.c b/net/tftp.c index bc7fe05..a04a832 100644 --- a/net/tftp.c +++ b/net/tftp.c @@ -322,7 +322,7 @@ TftpSend(void) * We will always be sending some sort of packet, so * cobble together the packet headers now. */ - pkt = (uchar *)(NetTxPacket + NetEthHdrSize() + IP_HDR_SIZE); + pkt = NetTxPacket + NetEthHdrSize() + IP_UDP_HDR_SIZE;
switch (TftpState) { case STATE_SEND_RRQ:

Hi Joe,
On Wed, Mar 28, 2012 at 12:42 PM, Joe Hershberger joe.hershberger@ni.comwrote:
Rename IP header related things to IP_UDP. The existing definition of IP_t includes UDP header, so name it to accurately describe the structure. Also add a structure that only contains IP header fields to be used by functions that don't need UDP.
Signed-off-by: Joe Hershberger joe.hershberger@ni.com Cc: Joe Hershberger joe.hershberger@gmail.com Cc: Simon Glass sjg@chromium.org Cc: Mike Frysinger vapier@gentoo.org
I feel that these two changes should be separated.
Also, shouldn't we get rid of the capital letters in the structure names at this point? Or are you planning on doing that later?
Regards, Simon

Separate the Ethernet header from the 802 header. Base the size constants on the structs.
Signed-off-by: Joe Hershberger joe.hershberger@ni.com Cc: Joe Hershberger joe.hershberger@gmail.com Cc: Simon Glass sjg@chromium.org Cc: Mike Frysinger vapier@gentoo.org --- Changes for v2: - Split apart from "Un-typedef variables in net" - Created a new E802_hdr struct and removed 802 members from Ethernet_hdr - Renamed structs from *_t to *_hdr since they are nolonger types - Replaced offsetof(Ethernet_t, et_dsap) with ETHER_HDR_SIZE in arch/powerpc/cpu/mpc8260/ether_fcc.c
arch/powerpc/cpu/mpc8260/ether_fcc.c | 17 ++++++++--------- include/net.h | 18 ++++++++++++++---- net/arp.c | 4 ++-- net/arp.h | 2 +- net/bootp.c | 2 +- net/cdp.c | 4 ++-- net/net.c | 11 ++++++----- net/ping.c | 2 +- net/ping.h | 2 +- 9 files changed, 36 insertions(+), 26 deletions(-)
diff --git a/arch/powerpc/cpu/mpc8260/ether_fcc.c b/arch/powerpc/cpu/mpc8260/ether_fcc.c index 879ec0e..22e0730 100644 --- a/arch/powerpc/cpu/mpc8260/ether_fcc.c +++ b/arch/powerpc/cpu/mpc8260/ether_fcc.c @@ -1049,11 +1049,11 @@ eth_loopback_test (void) } else { ushort datlen = bdp->cbd_datlen; - Ethernet_t *ehp; + struct Ethernet_hdr *ehp; ushort prot; int ours, tb, n, nbytes;
- ehp = (Ethernet_t *) \ + ehp = (struct Ethernet_hdr *) \ &ecp->rxbufs[i][0];
ours = memcmp (ehp->et_src, \ @@ -1063,9 +1063,8 @@ eth_loopback_test (void) tb = prot & 0x8000; n = prot & 0x7fff;
- nbytes = ELBT_BUFSZ - \ - offsetof (Ethernet_t, \ - et_dsap) - \ + nbytes = ELBT_BUFSZ - + ETHER_HDR_SIZE - ELBT_CRCSZ;
/* check the frame is correct */ @@ -1080,10 +1079,10 @@ eth_loopback_test (void) patwords[n]; uint nbb;
- nbb = badbits ( \ - &ehp->et_dsap, \ - nbytes, \ - patword); + nbb = badbits( + ((uchar *)&ehp) + + ETHER_HDR_SIZE, + nbytes, patword);
ecp->rxeacc.badbit += \ nbb; diff --git a/include/net.h b/include/net.h index e9b398a..328dc2c 100644 --- a/include/net.h +++ b/include/net.h @@ -155,7 +155,17 @@ u32 ether_crc(size_t len, unsigned char const *p); /* * Ethernet header */ -typedef struct { + +struct Ethernet_hdr { + uchar et_dest[6]; /* Destination node */ + uchar et_src[6]; /* Source node */ + ushort et_protlen; /* Protocol or length */ +}; + +/* Ethernet header size */ +#define ETHER_HDR_SIZE (sizeof(struct Ethernet_hdr)) + +struct E802_hdr { uchar et_dest[6]; /* Destination node */ uchar et_src[6]; /* Source node */ ushort et_protlen; /* Protocol or length */ @@ -166,10 +176,10 @@ typedef struct { uchar et_snap2; uchar et_snap3; ushort et_prot; /* 802 protocol */ -} Ethernet_t; +};
-#define ETHER_HDR_SIZE 14 /* Ethernet header size */ -#define E802_HDR_SIZE 22 /* 802 ethernet header size */ +/* 802 ethernet header size */ +#define E802_HDR_SIZE (sizeof(struct E802_hdr))
/* * Ethernet header diff --git a/net/arp.c b/net/arp.c index d918810..8b30a73 100644 --- a/net/arp.c +++ b/net/arp.c @@ -113,7 +113,7 @@ void ArpTimeoutCheck(void) } }
-void ArpReceive(Ethernet_t *et, struct IP_UDP_hdr *ip, int len) +void ArpReceive(struct Ethernet_hdr *et, struct IP_UDP_hdr *ip, int len) { ARP_t *arp; IPaddr_t tmp; @@ -193,7 +193,7 @@ void ArpReceive(Ethernet_t *et, struct IP_UDP_hdr *ip, int len) NetGetHandler()(0, 0, 0, 0, 0); #endif /* modify header, and transmit it */ - memcpy(((Ethernet_t *)NetArpWaitTxPacket)-> + memcpy(((struct Ethernet_hdr *)NetArpWaitTxPacket)-> et_dest, NetArpWaitPacketMAC, 6); (void) eth_send(NetArpWaitTxPacket, NetArpWaitTxPacketSize); diff --git a/net/arp.h b/net/arp.h index 3c27348..61a7a21 100644 --- a/net/arp.h +++ b/net/arp.h @@ -25,6 +25,6 @@ extern int NetArpWaitTry; void ArpInit(void); void ArpRequest(void); void ArpTimeoutCheck(void); -void ArpReceive(Ethernet_t *et, struct IP_UDP_hdr *ip, int len); +void ArpReceive(struct Ethernet_hdr *et, struct IP_UDP_hdr *ip, int len);
#endif /* __ARP_H__ */ diff --git a/net/bootp.c b/net/bootp.c index f5f9119..44c0b5c 100644 --- a/net/bootp.c +++ b/net/bootp.c @@ -105,7 +105,7 @@ static void BootpCopyNetParams(struct Bootp_t *bp) NetCopyIP(&tmp_ip, &bp->bp_siaddr); if (tmp_ip != 0) NetCopyIP(&NetServerIP, &bp->bp_siaddr); - memcpy(NetServerEther, ((Ethernet_t *)NetRxPacket)->et_src, 6); + memcpy(NetServerEther, ((struct Ethernet_hdr *)NetRxPacket)->et_src, 6); #endif if (strlen(bp->bp_file) > 0) copy_filename(BootFile, bp->bp_file, sizeof(BootFile)); diff --git a/net/cdp.c b/net/cdp.c index 028be82..6be6665 100644 --- a/net/cdp.c +++ b/net/cdp.c @@ -108,7 +108,7 @@ CDPSendTrigger(void) uchar *pkt; ushort *s; ushort *cp; - Ethernet_t *et; + struct Ethernet_hdr *et; int len; ushort chksum; #if defined(CONFIG_CDP_DEVICE_ID) || defined(CONFIG_CDP_PORT_ID) || \ @@ -117,7 +117,7 @@ CDPSendTrigger(void) #endif
pkt = NetTxPacket; - et = (Ethernet_t *)pkt; + et = (struct Ethernet_hdr *)pkt;
/* NOTE: trigger sent not on any VLAN */
diff --git a/net/net.c b/net/net.c index 35ead0e..9c386ae 100644 --- a/net/net.c +++ b/net/net.c @@ -825,7 +825,7 @@ static inline struct IP_UDP_hdr *NetDefragment(struct IP_UDP_hdr *ip, int *lenp) * @parma ip IP packet containing the ICMP */ static void receive_icmp(struct IP_UDP_hdr *ip, int len, - IPaddr_t src_ip, Ethernet_t *et) + IPaddr_t src_ip, struct Ethernet_hdr *et) { ICMP_t *icmph = (ICMP_t *)&ip->udp_src;
@@ -853,7 +853,7 @@ static void receive_icmp(struct IP_UDP_hdr *ip, int len, void NetReceive(uchar *inpkt, int len) { - Ethernet_t *et; + struct Ethernet_hdr *et; struct IP_UDP_hdr *ip; IPaddr_t tmp; IPaddr_t src_ip; @@ -867,7 +867,7 @@ NetReceive(uchar *inpkt, int len)
NetRxPacket = inpkt; NetRxPacketLen = len; - et = (Ethernet_t *)inpkt; + et = (struct Ethernet_hdr *)inpkt;
/* too small packet? */ if (len < ETHER_HDR_SIZE) @@ -897,10 +897,11 @@ NetReceive(uchar *inpkt, int len) debug("packet received\n");
if (x < 1514) { + struct E802_hdr *et802 = (struct E802_hdr *)et; /* * Got a 802 packet. Check the other protocol field. */ - x = ntohs(et->et_prot); + x = ntohs(et802->et_prot);
ip = (struct IP_UDP_hdr *)(inpkt + E802_HDR_SIZE); len -= E802_HDR_SIZE; @@ -1220,7 +1221,7 @@ NetEthHdrSize(void) int NetSetEther(uchar *xet, uchar * addr, uint prot) { - Ethernet_t *et = (Ethernet_t *)xet; + struct Ethernet_hdr *et = (struct Ethernet_hdr *)xet; ushort myvlanid;
myvlanid = ntohs(NetOurVLAN); diff --git a/net/ping.c b/net/ping.c index fe4cead..e7fa64e 100644 --- a/net/ping.c +++ b/net/ping.c @@ -98,7 +98,7 @@ void ping_start(void) ping_send(); }
-void ping_receive(Ethernet_t *et, struct IP_UDP_hdr *ip, int len) +void ping_receive(struct Ethernet_hdr *et, struct IP_UDP_hdr *ip, int len) { ICMP_t *icmph = (ICMP_t *)&(ip->udp_src); IPaddr_t src_ip; diff --git a/net/ping.h b/net/ping.h index dd97b58..2560f66 100644 --- a/net/ping.h +++ b/net/ping.h @@ -17,7 +17,7 @@ #include <net.h>
void ping_start(void); -void ping_receive(Ethernet_t *et, struct IP_UDP_hdr *ip, int len); +void ping_receive(struct Ethernet_hdr *et, struct IP_UDP_hdr *ip, int len);
#endif /* __PING_H__ */ #endif

Hi Joe,
On Wed, Mar 28, 2012 at 12:42 PM, Joe Hershberger joe.hershberger@ni.comwrote:
Separate the Ethernet header from the 802 header. Base the size constants on the structs.
Signed-off-by: Joe Hershberger joe.hershberger@ni.com Cc: Joe Hershberger joe.hershberger@gmail.com Cc: Simon Glass sjg@chromium.org Cc: Mike Frysinger vapier@gentoo.org
How about struct ethernet_hdr? Otherwise it looks good.
Just an aside - I assume that the sizeof() does not word align the result on any architectures? In other words, does sizeof(struct Ethernet_hdr) definitely give you 14 instead of 16 in all cases? I am sure this might be right, or it would have broken things horribly.
Regards, Simon
Changes for v2:
- Split apart from "Un-typedef variables in net"
- Created a new E802_hdr struct and removed 802 members from Ethernet_hdr
- Renamed structs from *_t to *_hdr since they are nolonger types
- Replaced offsetof(Ethernet_t, et_dsap) with ETHER_HDR_SIZE in
arch/powerpc/cpu/mpc8260/ether_fcc.c

Signed-off-by: Joe Hershberger joe.hershberger@ni.com Cc: Joe Hershberger joe.hershberger@gmail.com Cc: Simon Glass sjg@chromium.org Cc: Mike Frysinger vapier@gentoo.org --- Changes for v2: - Split apart from "Un-typedef variables in net" - Renamed structs from *_t to *_hdr since they are nolonger types
include/net.h | 7 ++++--- net/net.c | 6 ++++-- 2 files changed, 8 insertions(+), 5 deletions(-)
diff --git a/include/net.h b/include/net.h index 328dc2c..0ddb2b4 100644 --- a/include/net.h +++ b/include/net.h @@ -184,15 +184,16 @@ struct E802_hdr { /* * Ethernet header */ -typedef struct { +struct VLAN_Ethernet_hdr { uchar vet_dest[6]; /* Destination node */ uchar vet_src[6]; /* Source node */ ushort vet_vlan_type; /* PROT_VLAN */ ushort vet_tag; /* TAG of VLAN */ ushort vet_type; /* protocol type */ -} VLAN_Ethernet_t; +};
-#define VLAN_ETHER_HDR_SIZE 18 /* VLAN Ethernet header size */ +/* VLAN Ethernet header size */ +#define VLAN_ETHER_HDR_SIZE (sizeof(struct VLAN_Ethernet_hdr))
#define PROT_IP 0x0800 /* IP protocol */ #define PROT_ARP 0x0806 /* IP ARP protocol */ diff --git a/net/net.c b/net/net.c index 9c386ae..76df737 100644 --- a/net/net.c +++ b/net/net.c @@ -911,7 +911,8 @@ NetReceive(uchar *inpkt, int len) len -= ETHER_HDR_SIZE;
} else { /* VLAN packet */ - VLAN_Ethernet_t *vet = (VLAN_Ethernet_t *)et; + struct VLAN_Ethernet_hdr *vet = + (struct VLAN_Ethernet_hdr *)et;
debug("VLAN packet received\n");
@@ -1234,7 +1235,8 @@ NetSetEther(uchar *xet, uchar * addr, uint prot) et->et_protlen = htons(prot); return ETHER_HDR_SIZE; } else { - VLAN_Ethernet_t *vet = (VLAN_Ethernet_t *)xet; + struct VLAN_Ethernet_hdr *vet = + (struct VLAN_Ethernet_hdr *)xet;
vet->vet_vlan_type = htons(PROT_VLAN); vet->vet_tag = htons((0 << 5) | (myvlanid & VLAN_IDMASK));

Hi Joe,
On Wed, Mar 28, 2012 at 12:42 PM, Joe Hershberger joe.hershberger@ni.comwrote:
Signed-off-by: Joe Hershberger joe.hershberger@ni.com Cc: Joe Hershberger joe.hershberger@gmail.com Cc: Simon Glass sjg@chromium.org Cc: Mike Frysinger vapier@gentoo.org
Changes for v2:
- Split apart from "Un-typedef variables in net"
- Renamed structs from *_t to *_hdr since they are nolonger types
Same comment about lower case in structure name. What is the plan here?
Regards, Simon
include/net.h | 7 ++++--- net/net.c | 6 ++++-- 2 files changed, 8 insertions(+), 5 deletions(-)
diff --git a/include/net.h b/include/net.h index 328dc2c..0ddb2b4 100644 --- a/include/net.h +++ b/include/net.h @@ -184,15 +184,16 @@ struct E802_hdr { /*
Ethernet header
*/ -typedef struct { +struct VLAN_Ethernet_hdr { uchar vet_dest[6]; /* Destination node */ uchar vet_src[6]; /* Source node */ ushort vet_vlan_type; /* PROT_VLAN */ ushort vet_tag; /* TAG of VLAN */ ushort vet_type; /* protocol type */ -} VLAN_Ethernet_t; +};
-#define VLAN_ETHER_HDR_SIZE 18 /* VLAN Ethernet header size */ +/* VLAN Ethernet header size */ +#define VLAN_ETHER_HDR_SIZE (sizeof(struct VLAN_Ethernet_hdr))
#define PROT_IP 0x0800 /* IP protocol */ #define PROT_ARP 0x0806 /* IP ARP protocol */ diff --git a/net/net.c b/net/net.c index 9c386ae..76df737 100644 --- a/net/net.c +++ b/net/net.c @@ -911,7 +911,8 @@ NetReceive(uchar *inpkt, int len) len -= ETHER_HDR_SIZE;
} else { /* VLAN packet */
VLAN_Ethernet_t *vet = (VLAN_Ethernet_t *)et;
struct VLAN_Ethernet_hdr *vet =
(struct VLAN_Ethernet_hdr *)et; debug("VLAN packet received\n");
@@ -1234,7 +1235,8 @@ NetSetEther(uchar *xet, uchar * addr, uint prot) et->et_protlen = htons(prot); return ETHER_HDR_SIZE; } else {
VLAN_Ethernet_t *vet = (VLAN_Ethernet_t *)xet;
struct VLAN_Ethernet_hdr *vet =
(struct VLAN_Ethernet_hdr *)xet; vet->vet_vlan_type = htons(PROT_VLAN); vet->vet_tag = htons((0 << 5) | (myvlanid & VLAN_IDMASK));
-- 1.6.0.2

Signed-off-by: Joe Hershberger joe.hershberger@ni.com Cc: Joe Hershberger joe.hershberger@gmail.com Cc: Simon Glass sjg@chromium.org Cc: Mike Frysinger vapier@gentoo.org --- Changes for v2: - Split apart from "Un-typedef variables in net" - Renamed structs from *_t to *_hdr since they are nolonger types
include/net.h | 5 ++--- net/arp.c | 8 ++++---- net/rarp.c | 8 ++++---- 3 files changed, 10 insertions(+), 11 deletions(-)
diff --git a/include/net.h b/include/net.h index 0ddb2b4..937a99e 100644 --- a/include/net.h +++ b/include/net.h @@ -253,8 +253,7 @@ struct IP_UDP_hdr { /* * Address Resolution Protocol (ARP) header. */ -typedef struct -{ +struct ARP_hdr { ushort ar_hrd; /* Format of hardware address */ # define ARP_ETHER 1 /* Ethernet hardware address */ ushort ar_pro; /* Format of protocol address */ @@ -279,7 +278,7 @@ typedef struct uchar ar_tha[]; /* Target hardware address */ uchar ar_tpa[]; /* Target protocol address */ #endif /* 0 */ -} ARP_t; +};
#define ARP_HDR_SIZE (8+20) /* Size assuming ethernet */
diff --git a/net/arp.c b/net/arp.c index 8b30a73..3d5dc83 100644 --- a/net/arp.c +++ b/net/arp.c @@ -51,7 +51,7 @@ void ArpInit(void) void ArpRequest(void) { uchar *pkt; - ARP_t *arp; + struct ARP_hdr *arp;
debug("ARP broadcast %d\n", NetArpWaitTry);
@@ -59,7 +59,7 @@ void ArpRequest(void)
pkt += NetSetEther(pkt, NetBcastAddr, PROT_ARP);
- arp = (ARP_t *) pkt; + arp = (struct ARP_hdr *) pkt;
arp->ar_hrd = htons(ARP_ETHER); arp->ar_pro = htons(PROT_IP); @@ -115,7 +115,7 @@ void ArpTimeoutCheck(void)
void ArpReceive(struct Ethernet_hdr *et, struct IP_UDP_hdr *ip, int len) { - ARP_t *arp; + struct ARP_hdr *arp; IPaddr_t tmp; uchar *pkt;
@@ -130,7 +130,7 @@ void ArpReceive(struct Ethernet_hdr *et, struct IP_UDP_hdr *ip, int len) */ debug("Got ARP\n");
- arp = (ARP_t *)ip; + arp = (struct ARP_hdr *)ip; if (len < ARP_HDR_SIZE) { printf("bad length %d < %d\n", len, ARP_HDR_SIZE); return; diff --git a/net/rarp.c b/net/rarp.c index b30c812..a77f077 100644 --- a/net/rarp.c +++ b/net/rarp.c @@ -43,10 +43,10 @@ int RarpTry; */ void rarp_receive(struct IP_UDP_hdr *ip, unsigned len) { - ARP_t *arp; + struct ARP_hdr *arp;
debug("Got RARP\n"); - arp = (ARP_t *)ip; + arp = (struct ARP_hdr *)ip; if (len < ARP_HDR_SIZE) { printf("bad length %d < %d\n", len, ARP_HDR_SIZE); return; @@ -87,14 +87,14 @@ static void RarpTimeout(void) void RarpRequest(void) { uchar *pkt; - ARP_t *rarp; + struct ARP_hdr *rarp;
printf("RARP broadcast %d\n", ++RarpTry); pkt = NetTxPacket;
pkt += NetSetEther(pkt, NetBcastAddr, PROT_RARP);
- rarp = (ARP_t *)pkt; + rarp = (struct ARP_hdr *)pkt;
rarp->ar_hrd = htons(ARP_ETHER); rarp->ar_pro = htons(PROT_IP);

Hi Joe,
On Wed, Mar 28, 2012 at 12:42 PM, Joe Hershberger joe.hershberger@ni.comwrote:
Signed-off-by: Joe Hershberger joe.hershberger@ni.com Cc: Joe Hershberger joe.hershberger@gmail.com Cc: Simon Glass sjg@chromium.org Cc: Mike Frysinger vapier@gentoo.org
Changes for v2:
- Split apart from "Un-typedef variables in net"
- Renamed structs from *_t to *_hdr since they are nolonger types
struct arp_hdr?
Regards, Simon
include/net.h | 5 ++--- net/arp.c | 8 ++++---- net/rarp.c | 8 ++++---- 3 files changed, 10 insertions(+), 11 deletions(-)
diff --git a/include/net.h b/include/net.h index 0ddb2b4..937a99e 100644 --- a/include/net.h +++ b/include/net.h @@ -253,8 +253,7 @@ struct IP_UDP_hdr { /*
Address Resolution Protocol (ARP) header.
*/ -typedef struct -{ +struct ARP_hdr { ushort ar_hrd; /* Format of hardware address */ # define ARP_ETHER 1 /* Ethernet hardware address */ ushort ar_pro; /* Format of protocol address */ @@ -279,7 +278,7 @@ typedef struct uchar ar_tha[]; /* Target hardware address */ uchar ar_tpa[]; /* Target protocol address */ #endif /* 0 */ -} ARP_t; +};
#define ARP_HDR_SIZE (8+20) /* Size assuming ethernet */
diff --git a/net/arp.c b/net/arp.c index 8b30a73..3d5dc83 100644 --- a/net/arp.c +++ b/net/arp.c @@ -51,7 +51,7 @@ void ArpInit(void) void ArpRequest(void) { uchar *pkt;
ARP_t *arp;
struct ARP_hdr *arp; debug("ARP broadcast %d\n", NetArpWaitTry);
@@ -59,7 +59,7 @@ void ArpRequest(void)
pkt += NetSetEther(pkt, NetBcastAddr, PROT_ARP);
arp = (ARP_t *) pkt;
arp = (struct ARP_hdr *) pkt; arp->ar_hrd = htons(ARP_ETHER); arp->ar_pro = htons(PROT_IP);
@@ -115,7 +115,7 @@ void ArpTimeoutCheck(void)
void ArpReceive(struct Ethernet_hdr *et, struct IP_UDP_hdr *ip, int len) {
ARP_t *arp;
struct ARP_hdr *arp; IPaddr_t tmp; uchar *pkt;
@@ -130,7 +130,7 @@ void ArpReceive(struct Ethernet_hdr *et, struct IP_UDP_hdr *ip, int len) */ debug("Got ARP\n");
arp = (ARP_t *)ip;
arp = (struct ARP_hdr *)ip; if (len < ARP_HDR_SIZE) { printf("bad length %d < %d\n", len, ARP_HDR_SIZE); return;
diff --git a/net/rarp.c b/net/rarp.c index b30c812..a77f077 100644 --- a/net/rarp.c +++ b/net/rarp.c @@ -43,10 +43,10 @@ int RarpTry; */ void rarp_receive(struct IP_UDP_hdr *ip, unsigned len) {
ARP_t *arp;
struct ARP_hdr *arp; debug("Got RARP\n");
arp = (ARP_t *)ip;
arp = (struct ARP_hdr *)ip; if (len < ARP_HDR_SIZE) { printf("bad length %d < %d\n", len, ARP_HDR_SIZE); return;
@@ -87,14 +87,14 @@ static void RarpTimeout(void) void RarpRequest(void) { uchar *pkt;
ARP_t *rarp;
struct ARP_hdr *rarp; printf("RARP broadcast %d\n", ++RarpTry); pkt = NetTxPacket; pkt += NetSetEther(pkt, NetBcastAddr, PROT_RARP);
rarp = (ARP_t *)pkt;
rarp = (struct ARP_hdr *)pkt; rarp->ar_hrd = htons(ARP_ETHER); rarp->ar_pro = htons(PROT_IP);
-- 1.6.0.2

Signed-off-by: Joe Hershberger joe.hershberger@ni.com Cc: Joe Hershberger joe.hershberger@gmail.com Cc: Simon Glass sjg@chromium.org Cc: Mike Frysinger vapier@gentoo.org --- Changes for v2: - Split apart from "Un-typedef variables in net" - Renamed structs from *_t to *_hdr since they are nolonger types
include/net.h | 6 ++++-- net/net.c | 2 +- net/ping.c | 2 +- 3 files changed, 6 insertions(+), 4 deletions(-)
diff --git a/include/net.h b/include/net.h index 937a99e..538eb86 100644 --- a/include/net.h +++ b/include/net.h @@ -297,7 +297,7 @@ struct ARP_hdr { /* Codes for NOT_REACH */ #define ICMP_NOT_REACH_PORT 3 /* Port unreachable */
-typedef struct icmphdr { +struct ICMP_hdr { uchar type; uchar code; ushort checksum; @@ -313,8 +313,10 @@ typedef struct icmphdr { } frag; uchar data[0]; } un; -} ICMP_t; +};
+#define ICMP_HDR_SIZE (sizeof(struct ICMP_hdr)) +#define IP_ICMP_HDR_SIZE (IP_HDR_SIZE + ICMP_HDR_SIZE)
/* * Maximum packet size; used to allocate packet storage. diff --git a/net/net.c b/net/net.c index 76df737..f8ec623 100644 --- a/net/net.c +++ b/net/net.c @@ -827,7 +827,7 @@ static inline struct IP_UDP_hdr *NetDefragment(struct IP_UDP_hdr *ip, int *lenp) static void receive_icmp(struct IP_UDP_hdr *ip, int len, IPaddr_t src_ip, struct Ethernet_hdr *et) { - ICMP_t *icmph = (ICMP_t *)&ip->udp_src; + struct ICMP_hdr *icmph = (struct ICMP_hdr *)&(ip->udp_src);
switch (icmph->type) { case ICMP_REDIRECT: diff --git a/net/ping.c b/net/ping.c index e7fa64e..27fca6a 100644 --- a/net/ping.c +++ b/net/ping.c @@ -100,7 +100,7 @@ void ping_start(void)
void ping_receive(struct Ethernet_hdr *et, struct IP_UDP_hdr *ip, int len) { - ICMP_t *icmph = (ICMP_t *)&(ip->udp_src); + struct ICMP_hdr *icmph = (struct ICMP_hdr *)&(ip->udp_src); IPaddr_t src_ip;
switch (icmph->type) {

Hi Joe,
On Wed, Mar 28, 2012 at 12:42 PM, Joe Hershberger joe.hershberger@ni.comwrote:
Signed-off-by: Joe Hershberger joe.hershberger@ni.com Cc: Joe Hershberger joe.hershberger@gmail.com Cc: Simon Glass sjg@chromium.org Cc: Mike Frysinger vapier@gentoo.org
Changes for v2:
- Split apart from "Un-typedef variables in net"
- Renamed structs from *_t to *_hdr since they are nolonger types
include/net.h | 6 ++++-- net/net.c | 2 +- net/ping.c | 2 +- 3 files changed, 6 insertions(+), 4 deletions(-)
diff --git a/include/net.h b/include/net.h index 937a99e..538eb86 100644 --- a/include/net.h +++ b/include/net.h @@ -297,7 +297,7 @@ struct ARP_hdr { /* Codes for NOT_REACH */ #define ICMP_NOT_REACH_PORT 3 /* Port unreachable */
-typedef struct icmphdr { +struct ICMP_hdr {
struct icmp_hdr might be better?
uchar type;
uchar code; ushort checksum;
@@ -313,8 +313,10 @@ typedef struct icmphdr { } frag; uchar data[0]; } un; -} ICMP_t; +};
+#define ICMP_HDR_SIZE (sizeof(struct ICMP_hdr)) +#define IP_ICMP_HDR_SIZE (IP_HDR_SIZE + ICMP_HDR_SIZE)
/*
- Maximum packet size; used to allocate packet storage.
diff --git a/net/net.c b/net/net.c index 76df737..f8ec623 100644 --- a/net/net.c +++ b/net/net.c @@ -827,7 +827,7 @@ static inline struct IP_UDP_hdr *NetDefragment(struct IP_UDP_hdr *ip, int *lenp) static void receive_icmp(struct IP_UDP_hdr *ip, int len, IPaddr_t src_ip, struct Ethernet_hdr *et) {
ICMP_t *icmph = (ICMP_t *)&ip->udp_src;
struct ICMP_hdr *icmph = (struct ICMP_hdr *)&(ip->udp_src);
I don't think you need to add the second lot of brackets.
switch (icmph->type) { case ICMP_REDIRECT:
diff --git a/net/ping.c b/net/ping.c index e7fa64e..27fca6a 100644 --- a/net/ping.c +++ b/net/ping.c @@ -100,7 +100,7 @@ void ping_start(void)
void ping_receive(struct Ethernet_hdr *et, struct IP_UDP_hdr *ip, int len) {
ICMP_t *icmph = (ICMP_t *)&(ip->udp_src);
struct ICMP_hdr *icmph = (struct ICMP_hdr *)&(ip->udp_src);
Same here.
IPaddr_t src_ip; switch (icmph->type) {
-- 1.6.0.2
Regards, Simon

This name more explicitly claims that it does not include the header size
Signed-off-by: Joe Hershberger joe.hershberger@ni.com Cc: Joe Hershberger joe.hershberger@gmail.com Cc: Simon Glass sjg@chromium.org Cc: Mike Frysinger vapier@gentoo.org --- Changes for v2: - Split from "Improve variable names and code readability"
include/net.h | 2 +- net/net.c | 14 ++++++++------ 2 files changed, 9 insertions(+), 7 deletions(-)
diff --git a/include/net.h b/include/net.h index 538eb86..64ecb38 100644 --- a/include/net.h +++ b/include/net.h @@ -462,7 +462,7 @@ extern void NetSendPacket(uchar *, int);
/* Transmit UDP packet, performing ARP request if needed */ extern int NetSendUDPPacket(uchar *ether, IPaddr_t dest, int dport, - int sport, int len); + int sport, int payload_len);
/* Processes a received packet */ extern void NetReceive(uchar *, int); diff --git a/net/net.c b/net/net.c index f8ec623..19d0de6 100644 --- a/net/net.c +++ b/net/net.c @@ -594,7 +594,8 @@ NetSendPacket(uchar *pkt, int len) }
int -NetSendUDPPacket(uchar *ether, IPaddr_t dest, int dport, int sport, int len) +NetSendUDPPacket(uchar *ether, IPaddr_t dest, int dport, int sport, + int payload_len) { uchar *pkt;
@@ -620,14 +621,14 @@ NetSendUDPPacket(uchar *ether, IPaddr_t dest, int dport, int sport, int len) pkt = NetArpWaitTxPacket; pkt += NetSetEther(pkt, NetArpWaitPacketMAC, PROT_IP);
- NetSetIP(pkt, dest, dport, sport, len); + NetSetIP(pkt, dest, dport, sport, payload_len); memcpy(pkt + IP_UDP_HDR_SIZE, (uchar *)NetTxPacket + (pkt - (uchar *)NetArpWaitTxPacket) + - IP_UDP_HDR_SIZE, len); + IP_UDP_HDR_SIZE, payload_len);
/* size of the waiting packet */ NetArpWaitTxPacketSize = (pkt - NetArpWaitTxPacket) + - IP_UDP_HDR_SIZE + len; + IP_UDP_HDR_SIZE + payload_len;
/* and do the ARP request */ NetArpWaitTry = 1; @@ -640,8 +641,9 @@ NetSendUDPPacket(uchar *ether, IPaddr_t dest, int dport, int sport, int len)
pkt = (uchar *)NetTxPacket; pkt += NetSetEther(pkt, ether, PROT_IP); - NetSetIP(pkt, dest, dport, sport, len); - eth_send(NetTxPacket, (pkt - NetTxPacket) + IP_UDP_HDR_SIZE + len); + NetSetIP(pkt, dest, dport, sport, payload_len); + eth_send(NetTxPacket, (pkt - NetTxPacket) + IP_UDP_HDR_SIZE + + payload_len);
return 0; /* transmitted */ }

Hi Joe,
On Wed, Mar 28, 2012 at 12:42 PM, Joe Hershberger joe.hershberger@ni.comwrote:
This name more explicitly claims that it does not include the header size
Signed-off-by: Joe Hershberger joe.hershberger@ni.com Cc: Joe Hershberger joe.hershberger@gmail.com Cc: Simon Glass sjg@chromium.org Cc: Mike Frysinger vapier@gentoo.org
Acked-by: Simon Glass sjg@chromium.org
Changes for v2:
- Split from "Improve variable names and code readability"
include/net.h | 2 +- net/net.c | 14 ++++++++------ 2 files changed, 9 insertions(+), 7 deletions(-)
diff --git a/include/net.h b/include/net.h index 538eb86..64ecb38 100644 --- a/include/net.h +++ b/include/net.h @@ -462,7 +462,7 @@ extern void NetSendPacket(uchar *, int);
/* Transmit UDP packet, performing ARP request if needed */ extern int NetSendUDPPacket(uchar *ether, IPaddr_t dest, int dport,
int sport, int len);
int sport, int payload_len);
Would be nice to document the arguments here
/* Processes a received packet */ extern void NetReceive(uchar *, int); diff --git a/net/net.c b/net/net.c index f8ec623..19d0de6 100644 --- a/net/net.c +++ b/net/net.c @@ -594,7 +594,8 @@ NetSendPacket(uchar *pkt, int len) }
int -NetSendUDPPacket(uchar *ether, IPaddr_t dest, int dport, int sport, int len) +NetSendUDPPacket(uchar *ether, IPaddr_t dest, int dport, int sport,
int payload_len)
{ uchar *pkt;
@@ -620,14 +621,14 @@ NetSendUDPPacket(uchar *ether, IPaddr_t dest, int dport, int sport, int len) pkt = NetArpWaitTxPacket; pkt += NetSetEther(pkt, NetArpWaitPacketMAC, PROT_IP);
NetSetIP(pkt, dest, dport, sport, len);
NetSetIP(pkt, dest, dport, sport, payload_len); memcpy(pkt + IP_UDP_HDR_SIZE, (uchar *)NetTxPacket + (pkt - (uchar *)NetArpWaitTxPacket) +
IP_UDP_HDR_SIZE, len);
IP_UDP_HDR_SIZE, payload_len); /* size of the waiting packet */ NetArpWaitTxPacketSize = (pkt - NetArpWaitTxPacket) +
IP_UDP_HDR_SIZE + len;
IP_UDP_HDR_SIZE + payload_len; /* and do the ARP request */ NetArpWaitTry = 1;
@@ -640,8 +641,9 @@ NetSendUDPPacket(uchar *ether, IPaddr_t dest, int dport, int sport, int len)
pkt = (uchar *)NetTxPacket; pkt += NetSetEther(pkt, ether, PROT_IP);
NetSetIP(pkt, dest, dport, sport, len);
eth_send(NetTxPacket, (pkt - NetTxPacket) + IP_UDP_HDR_SIZE + len);
NetSetIP(pkt, dest, dport, sport, payload_len);
eth_send(NetTxPacket, (pkt - NetTxPacket) + IP_UDP_HDR_SIZE +
payload_len); return 0; /* transmitted */
}
1.6.0.2
Regards,
Simon

Signed-off-by: Joe Hershberger joe.hershberger@ni.com Cc: Joe Hershberger joe.hershberger@gmail.com Cc: Simon Glass sjg@chromium.org Cc: Mike Frysinger vapier@gentoo.org --- Changes for v2: - Split from "Improve variable names and code readability" - Fixed comment indention
include/net.h | 2 +- net/net.c | 3 ++- 2 files changed, 3 insertions(+), 2 deletions(-)
diff --git a/include/net.h b/include/net.h index 64ecb38..3314b4e 100644 --- a/include/net.h +++ b/include/net.h @@ -178,7 +178,7 @@ struct E802_hdr { ushort et_prot; /* 802 protocol */ };
-/* 802 ethernet header size */ +/* 802 + SNAP + ethernet header size */ #define E802_HDR_SIZE (sizeof(struct E802_hdr))
/* diff --git a/net/net.c b/net/net.c index 19d0de6..cac540a 100644 --- a/net/net.c +++ b/net/net.c @@ -901,7 +901,8 @@ NetReceive(uchar *inpkt, int len) if (x < 1514) { struct E802_hdr *et802 = (struct E802_hdr *)et; /* - * Got a 802 packet. Check the other protocol field. + * Got a 802.2 packet. Check the other protocol field. + * XXX VLAN over 802.2+SNAP not implemented! */ x = ntohs(et802->et_prot);

Hi Joe,
On Wed, Mar 28, 2012 at 12:42 PM, Joe Hershberger joe.hershberger@ni.comwrote:
Signed-off-by: Joe Hershberger joe.hershberger@ni.com Cc: Joe Hershberger joe.hershberger@gmail.com Cc: Simon Glass sjg@chromium.org Cc: Mike Frysinger vapier@gentoo.org
Acked-by: Simon Glass sjg@chromium.org
A general comment - it would be good to have a short commit message on all patches.
Regards, Simon
Changes for v2:
- Split from "Improve variable names and code readability"
- Fixed comment indention
include/net.h | 2 +- net/net.c | 3 ++- 2 files changed, 3 insertions(+), 2 deletions(-)
diff --git a/include/net.h b/include/net.h index 64ecb38..3314b4e 100644 --- a/include/net.h +++ b/include/net.h @@ -178,7 +178,7 @@ struct E802_hdr { ushort et_prot; /* 802 protocol */ };
-/* 802 ethernet header size */ +/* 802 + SNAP + ethernet header size */ #define E802_HDR_SIZE (sizeof(struct E802_hdr))
/* diff --git a/net/net.c b/net/net.c index 19d0de6..cac540a 100644 --- a/net/net.c +++ b/net/net.c @@ -901,7 +901,8 @@ NetReceive(uchar *inpkt, int len) if (x < 1514) { struct E802_hdr *et802 = (struct E802_hdr *)et; /*
* Got a 802 packet. Check the other protocol field.
* Got a 802.2 packet. Check the other protocol
field.
* XXX VLAN over 802.2+SNAP not implemented! */ x = ntohs(et802->et_prot);
-- 1.6.0.2

x is a poor variable name
Signed-off-by: Joe Hershberger joe.hershberger@ni.com Cc: Joe Hershberger joe.hershberger@gmail.com Cc: Simon Glass sjg@chromium.org Cc: Mike Frysinger vapier@gentoo.org --- Changes for v2: - Split from "Improve variable names and code readability"
net/net.c | 24 ++++++++++++------------ 1 files changed, 12 insertions(+), 12 deletions(-)
diff --git a/net/net.c b/net/net.c index cac540a..aa1ae05 100644 --- a/net/net.c +++ b/net/net.c @@ -857,9 +857,9 @@ NetReceive(uchar *inpkt, int len) { struct Ethernet_hdr *et; struct IP_UDP_hdr *ip; - IPaddr_t tmp; + IPaddr_t dst_ip; IPaddr_t src_ip; - int x; + int eth_proto; #if defined(CONFIG_CMD_CDP) int iscdp; #endif @@ -894,22 +894,22 @@ NetReceive(uchar *inpkt, int len) if (mynvlanid == (ushort)-1) mynvlanid = VLAN_NONE;
- x = ntohs(et->et_protlen); + eth_proto = ntohs(et->et_protlen);
debug("packet received\n");
- if (x < 1514) { + if (eth_proto < 1514) { struct E802_hdr *et802 = (struct E802_hdr *)et; /* * Got a 802.2 packet. Check the other protocol field. * XXX VLAN over 802.2+SNAP not implemented! */ - x = ntohs(et802->et_prot); + eth_proto = ntohs(et802->et_prot);
ip = (struct IP_UDP_hdr *)(inpkt + E802_HDR_SIZE); len -= E802_HDR_SIZE;
- } else if (x != PROT_VLAN) { /* normal packet */ + } else if (eth_proto != PROT_VLAN) { /* normal packet */ ip = (struct IP_UDP_hdr *)(inpkt + ETHER_HDR_SIZE); len -= ETHER_HDR_SIZE;
@@ -933,13 +933,13 @@ NetReceive(uchar *inpkt, int len)
cti = ntohs(vet->vet_tag); vlanid = cti & VLAN_IDMASK; - x = ntohs(vet->vet_type); + eth_proto = ntohs(vet->vet_type);
ip = (struct IP_UDP_hdr *)(inpkt + VLAN_ETHER_HDR_SIZE); len -= VLAN_ETHER_HDR_SIZE; }
- debug("Receive from protocol 0x%x\n", x); + debug("Receive from protocol 0x%x\n", eth_proto);
#if defined(CONFIG_CMD_CDP) if (iscdp) { @@ -956,7 +956,7 @@ NetReceive(uchar *inpkt, int len) return; }
- switch (x) { + switch (eth_proto) {
case PROT_ARP: ArpReceive(et, ip, len); @@ -995,10 +995,10 @@ NetReceive(uchar *inpkt, int len) return; } /* If it is not for us, ignore it */ - tmp = NetReadIP(&ip->ip_dst); - if (NetOurIP && tmp != NetOurIP && tmp != 0xFFFFFFFF) { + dst_ip = NetReadIP(&ip->ip_dst); + if (NetOurIP && dst_ip != NetOurIP && dst_ip != 0xFFFFFFFF) { #ifdef CONFIG_MCAST_TFTP - if (Mcast_addr != tmp) + if (Mcast_addr != dst_ip) #endif return; }

On Wed, Mar 28, 2012 at 12:42 PM, Joe Hershberger joe.hershberger@ni.comwrote:
x is a poor variable name
Signed-off-by: Joe Hershberger joe.hershberger@ni.com Cc: Joe Hershberger joe.hershberger@gmail.com Cc: Simon Glass sjg@chromium.org Cc: Mike Frysinger vapier@gentoo.org
Acked-by: Simon Glass sjg@chromium.org
Changes for v2:
- Split from "Improve variable names and code readability"
net/net.c | 24 ++++++++++++------------ 1 files changed, 12 insertions(+), 12 deletions(-)
diff --git a/net/net.c b/net/net.c index cac540a..aa1ae05 100644 --- a/net/net.c +++ b/net/net.c @@ -857,9 +857,9 @@ NetReceive(uchar *inpkt, int len) { struct Ethernet_hdr *et; struct IP_UDP_hdr *ip;
IPaddr_t tmp;
IPaddr_t dst_ip; IPaddr_t src_ip;
int x;
int eth_proto;
#if defined(CONFIG_CMD_CDP) int iscdp; #endif @@ -894,22 +894,22 @@ NetReceive(uchar *inpkt, int len) if (mynvlanid == (ushort)-1) mynvlanid = VLAN_NONE;
x = ntohs(et->et_protlen);
eth_proto = ntohs(et->et_protlen); debug("packet received\n");
if (x < 1514) {
if (eth_proto < 1514) { struct E802_hdr *et802 = (struct E802_hdr *)et; /* * Got a 802.2 packet. Check the other protocol field. * XXX VLAN over 802.2+SNAP not implemented! */
x = ntohs(et802->et_prot);
eth_proto = ntohs(et802->et_prot); ip = (struct IP_UDP_hdr *)(inpkt + E802_HDR_SIZE); len -= E802_HDR_SIZE;
} else if (x != PROT_VLAN) { /* normal packet */
} else if (eth_proto != PROT_VLAN) { /* normal packet */ ip = (struct IP_UDP_hdr *)(inpkt + ETHER_HDR_SIZE); len -= ETHER_HDR_SIZE;
@@ -933,13 +933,13 @@ NetReceive(uchar *inpkt, int len)
cti = ntohs(vet->vet_tag); vlanid = cti & VLAN_IDMASK;
x = ntohs(vet->vet_type);
eth_proto = ntohs(vet->vet_type); ip = (struct IP_UDP_hdr *)(inpkt + VLAN_ETHER_HDR_SIZE); len -= VLAN_ETHER_HDR_SIZE; }
debug("Receive from protocol 0x%x\n", x);
debug("Receive from protocol 0x%x\n", eth_proto);
#if defined(CONFIG_CMD_CDP) if (iscdp) { @@ -956,7 +956,7 @@ NetReceive(uchar *inpkt, int len) return; }
switch (x) {
switch (eth_proto) { case PROT_ARP: ArpReceive(et, ip, len);
@@ -995,10 +995,10 @@ NetReceive(uchar *inpkt, int len) return; } /* If it is not for us, ignore it */
tmp = NetReadIP(&ip->ip_dst);
if (NetOurIP && tmp != NetOurIP && tmp != 0xFFFFFFFF) {
dst_ip = NetReadIP(&ip->ip_dst);
if (NetOurIP && dst_ip != NetOurIP && dst_ip !=
0xFFFFFFFF) { #ifdef CONFIG_MCAST_TFTP
if (Mcast_addr != tmp)
if (Mcast_addr != dst_ip)
#endif return; } -- 1.6.0.2

This is not called as a handler, so don't name it that way
Signed-off-by: Joe Hershberger joe.hershberger@ni.com Cc: Joe Hershberger joe.hershberger@gmail.com Cc: Simon Glass sjg@chromium.org Cc: Mike Frysinger vapier@gentoo.org --- Changes for v2: - Split from "Improve variable names and code readability"
net/cdp.c | 2 +- net/cdp.h | 2 +- net/net.c | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-)
diff --git a/net/cdp.c b/net/cdp.c index 6be6665..5c4b3e8 100644 --- a/net/cdp.c +++ b/net/cdp.c @@ -245,7 +245,7 @@ CDPDummyHandler(uchar *pkt, unsigned dest, IPaddr_t sip, unsigned src, }
void -CDPHandler(const uchar *pkt, unsigned len) +CDPReceive(const uchar *pkt, unsigned len) { const uchar *t; const ushort *ss; diff --git a/net/cdp.h b/net/cdp.h index 93bc461..7ec5c70 100644 --- a/net/cdp.h +++ b/net/cdp.h @@ -14,7 +14,7 @@ #define __CDP_H__
void CDPStart(void); -void CDPHandler(const uchar *pkt, unsigned len); +void CDPReceive(const uchar *pkt, unsigned len);
#endif /* __CDP_H__ */ #endif diff --git a/net/net.c b/net/net.c index aa1ae05..4736ba1 100644 --- a/net/net.c +++ b/net/net.c @@ -943,7 +943,7 @@ NetReceive(uchar *inpkt, int len)
#if defined(CONFIG_CMD_CDP) if (iscdp) { - CDPHandler((uchar *)ip, len); + CDPReceive((uchar *)ip, len); return; } #endif

Hi Joe,
On Wed, Mar 28, 2012 at 12:42 PM, Joe Hershberger joe.hershberger@ni.comwrote:
This is not called as a handler, so don't name it that way
Signed-off-by: Joe Hershberger joe.hershberger@ni.com Cc: Joe Hershberger joe.hershberger@gmail.com Cc: Simon Glass sjg@chromium.org Cc: Mike Frysinger vapier@gentoo.org
Changes for v2:
- Split from "Improve variable names and code readability"
net/cdp.c | 2 +- net/cdp.h | 2 +- net/net.c | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-)
diff --git a/net/cdp.c b/net/cdp.c index 6be6665..5c4b3e8 100644 --- a/net/cdp.c +++ b/net/cdp.c @@ -245,7 +245,7 @@ CDPDummyHandler(uchar *pkt, unsigned dest, IPaddr_t sip, unsigned src, }
void -CDPHandler(const uchar *pkt, unsigned len) +CDPReceive(const uchar *pkt, unsigned len)
cdp_receive?
{ const uchar *t; const ushort *ss; diff --git a/net/cdp.h b/net/cdp.h index 93bc461..7ec5c70 100644 --- a/net/cdp.h +++ b/net/cdp.h @@ -14,7 +14,7 @@ #define __CDP_H__
void CDPStart(void); -void CDPHandler(const uchar *pkt, unsigned len); +void CDPReceive(const uchar *pkt, unsigned len);
How about a comment on the function?
#endif /* __CDP_H__ */ #endif diff --git a/net/net.c b/net/net.c index aa1ae05..4736ba1 100644 --- a/net/net.c +++ b/net/net.c @@ -943,7 +943,7 @@ NetReceive(uchar *inpkt, int len)
#if defined(CONFIG_CMD_CDP) if (iscdp) {
CDPHandler((uchar *)ip, len);
CDPReceive((uchar *)ip, len); return; }
#endif
1.6.0.2
Regards,
Simon

Clearer constant name. Also remove related BOOTP_SIZE which was unused and doesn't take into account VLAN packets.
Signed-off-by: Joe Hershberger joe.hershberger@ni.com Cc: Joe Hershberger joe.hershberger@gmail.com Cc: Simon Glass sjg@chromium.org Cc: Mike Frysinger vapier@gentoo.org --- Changes for v2: - Split from "Improve variable names and code readability"
net/bootp.c | 10 +++++----- net/bootp.h | 7 +++---- 2 files changed, 8 insertions(+), 9 deletions(-)
diff --git a/net/bootp.c b/net/bootp.c index 44c0b5c..62d3c71 100644 --- a/net/bootp.c +++ b/net/bootp.c @@ -73,7 +73,7 @@ static int BootpCheckPkt(uchar *pkt, unsigned dest, unsigned src, unsigned len)
if (dest != PORT_BOOTPC || src != PORT_BOOTPS) retval = -1; - else if (len < sizeof(struct Bootp_t) - OPT_SIZE) + else if (len < sizeof(struct Bootp_t) - OPT_FIELD_SIZE) retval = -2; else if (bp->bp_op != OP_BOOTREQUEST && bp->bp_op != OP_BOOTREPLY && @@ -370,8 +370,8 @@ static int DhcpExtended(u8 *e, int message_type, IPaddr_t ServerID,
*e++ = 57; /* Maximum DHCP Message Size */ *e++ = 2; - *e++ = (576 - 312 + OPT_SIZE) >> 8; - *e++ = (576 - 312 + OPT_SIZE) & 0xff; + *e++ = (576 - 312 + OPT_FIELD_SIZE) >> 8; + *e++ = (576 - 312 + OPT_FIELD_SIZE) & 0xff;
if (ServerID) { int tmp = ntohl(ServerID); @@ -521,8 +521,8 @@ static int BootpExtended(u8 *e)
*e++ = 57; /* Maximum DHCP Message Size */ *e++ = 2; - *e++ = (576 - 312 + OPT_SIZE) >> 16; - *e++ = (576 - 312 + OPT_SIZE) & 0xff; + *e++ = (576 - 312 + OPT_FIELD_SIZE) >> 16; + *e++ = (576 - 312 + OPT_FIELD_SIZE) & 0xff; #endif
#if defined(CONFIG_BOOTP_SUBNETMASK) diff --git a/net/bootp.h b/net/bootp.h index 1cf9a02..ecbcc4d 100644 --- a/net/bootp.h +++ b/net/bootp.h @@ -20,13 +20,13 @@ */ #if defined(CONFIG_CMD_DHCP) /* Minimum DHCP Options size per RFC2131 - results in 576 byte pkt */ -#define OPT_SIZE 312 +#define OPT_FIELD_SIZE 312 #if defined(CONFIG_BOOTP_VENDOREX) extern u8 *dhcp_vendorex_prep(u8 *e); /*rtn new e after add own opts. */ extern u8 *dhcp_vendorex_proc(u8 *e); /*rtn next e if mine,else NULL */ #endif #else -#define OPT_SIZE 64 +#define OPT_FIELD_SIZE 64 #endif
struct Bootp_t { @@ -48,11 +48,10 @@ struct Bootp_t { uchar bp_chaddr[16]; /* Client hardware address */ char bp_sname[64]; /* Server host name */ char bp_file[128]; /* Boot file name */ - char bp_vend[OPT_SIZE]; /* Vendor information */ + char bp_vend[OPT_FIELD_SIZE]; /* Vendor information */ };
#define BOOTP_HDR_SIZE sizeof(struct Bootp_t) -#define BOOTP_SIZE (ETHER_HDR_SIZE + IP_UDP_HDR_SIZE + BOOTP_HDR_SIZE)
/**********************************************************************/ /*

Hi Joe,
On Wed, Mar 28, 2012 at 12:42 PM, Joe Hershberger joe.hershberger@ni.comwrote:
Clearer constant name. Also remove related BOOTP_SIZE which was unused and doesn't take into account VLAN packets.
Signed-off-by: Joe Hershberger joe.hershberger@ni.com Cc: Joe Hershberger joe.hershberger@gmail.com Cc: Simon Glass sjg@chromium.org Cc: Mike Frysinger vapier@gentoo.org
Changes for v2:
- Split from "Improve variable names and code readability"
net/bootp.c | 10 +++++----- net/bootp.h | 7 +++---- 2 files changed, 8 insertions(+), 9 deletions(-)
diff --git a/net/bootp.c b/net/bootp.c index 44c0b5c..62d3c71 100644 --- a/net/bootp.c +++ b/net/bootp.c @@ -73,7 +73,7 @@ static int BootpCheckPkt(uchar *pkt, unsigned dest, unsigned src, unsigned len)
if (dest != PORT_BOOTPC || src != PORT_BOOTPS) retval = -1;
else if (len < sizeof(struct Bootp_t) - OPT_SIZE)
else if (len < sizeof(struct Bootp_t) - OPT_FIELD_SIZE) retval = -2; else if (bp->bp_op != OP_BOOTREQUEST && bp->bp_op != OP_BOOTREPLY &&
@@ -370,8 +370,8 @@ static int DhcpExtended(u8 *e, int message_type, IPaddr_t ServerID,
*e++ = 57; /* Maximum DHCP Message Size */ *e++ = 2;
*e++ = (576 - 312 + OPT_SIZE) >> 8;
*e++ = (576 - 312 + OPT_SIZE) & 0xff;
*e++ = (576 - 312 + OPT_FIELD_SIZE) >> 8;
*e++ = (576 - 312 + OPT_FIELD_SIZE) & 0xff;
Better to use a temporary variable here, and below?
if (ServerID) { int tmp = ntohl(ServerID);
@@ -521,8 +521,8 @@ static int BootpExtended(u8 *e)
*e++ = 57; /* Maximum DHCP Message Size */ *e++ = 2;
*e++ = (576 - 312 + OPT_SIZE) >> 16;
*e++ = (576 - 312 + OPT_SIZE) & 0xff;
*e++ = (576 - 312 + OPT_FIELD_SIZE) >> 16;
*e++ = (576 - 312 + OPT_FIELD_SIZE) & 0xff;
#endif
#if defined(CONFIG_BOOTP_SUBNETMASK) diff --git a/net/bootp.h b/net/bootp.h index 1cf9a02..ecbcc4d 100644 --- a/net/bootp.h +++ b/net/bootp.h @@ -20,13 +20,13 @@ */ #if defined(CONFIG_CMD_DHCP) /* Minimum DHCP Options size per RFC2131 - results in 576 byte pkt */ -#define OPT_SIZE 312 +#define OPT_FIELD_SIZE 312 #if defined(CONFIG_BOOTP_VENDOREX) extern u8 *dhcp_vendorex_prep(u8 *e); /*rtn new e after add own opts. */ extern u8 *dhcp_vendorex_proc(u8 *e); /*rtn next e if mine,else NULL */ #endif #else -#define OPT_SIZE 64 +#define OPT_FIELD_SIZE 64 #endif
struct Bootp_t { @@ -48,11 +48,10 @@ struct Bootp_t { uchar bp_chaddr[16]; /* Client hardware address */ char bp_sname[64]; /* Server host name */ char bp_file[128]; /* Boot file name */
char bp_vend[OPT_SIZE]; /* Vendor information */
char bp_vend[OPT_FIELD_SIZE]; /* Vendor information */
};
#define BOOTP_HDR_SIZE sizeof(struct Bootp_t) -#define BOOTP_SIZE (ETHER_HDR_SIZE + IP_UDP_HDR_SIZE + BOOTP_HDR_SIZE)
/**********************************************************************/ /* -- 1.6.0.2
Regards,
Simon

Signed-off-by: Joe Hershberger joe.hershberger@ni.com Cc: Joe Hershberger joe.hershberger@gmail.com Cc: Simon Glass sjg@chromium.org Cc: Mike Frysinger vapier@gentoo.org --- Changes for v2: - Split from "Improve variable names and code readability"
net/net.c | 20 ++++++++++---------- 1 files changed, 10 insertions(+), 10 deletions(-)
diff --git a/net/net.c b/net/net.c index 4736ba1..0371e2e 100644 --- a/net/net.c +++ b/net/net.c @@ -75,27 +75,27 @@
#include <common.h> -#include <watchdog.h> #include <command.h> #include <linux/compiler.h> #include <net.h> -#include "arp.h" -#include "bootp.h" -#include "tftp.h" -#include "rarp.h" -#include "nfs.h" -#ifdef CONFIG_STATUS_LED +#if defined(CONFIG_STATUS_LED) #include <status_led.h> #include <miiphy.h> #endif -#if defined(CONFIG_CMD_SNTP) -#include "sntp.h" -#endif +#include <watchdog.h> +#include "arp.h" +#include "bootp.h" #include "cdp.h" #if defined(CONFIG_CMD_DNS) #include "dns.h" #endif +#include "nfs.h" #include "ping.h" +#include "rarp.h" +#if defined(CONFIG_CMD_SNTP) +#include "sntp.h" +#endif +#include "tftp.h"
DECLARE_GLOBAL_DATA_PTR;

Hi Joe,
On Wed, Mar 28, 2012 at 12:42 PM, Joe Hershberger joe.hershberger@ni.comwrote:
Signed-off-by: Joe Hershberger joe.hershberger@ni.com Cc: Joe Hershberger joe.hershberger@gmail.com Cc: Simon Glass sjg@chromium.org Cc: Mike Frysinger vapier@gentoo.org
Changes for v2:
- Split from "Improve variable names and code readability"
net/net.c | 20 ++++++++++---------- 1 files changed, 10 insertions(+), 10 deletions(-)
diff --git a/net/net.c b/net/net.c index 4736ba1..0371e2e 100644 --- a/net/net.c +++ b/net/net.c @@ -75,27 +75,27 @@
#include <common.h> -#include <watchdog.h> #include <command.h> #include <linux/compiler.h>
put this just above "arp.h" I think
#include <net.h> -#include "arp.h" -#include "bootp.h" -#include "tftp.h" -#include "rarp.h" -#include "nfs.h" -#ifdef CONFIG_STATUS_LED +#if defined(CONFIG_STATUS_LED) #include <status_led.h> #include <miiphy.h>
Swap those?
#endif -#if defined(CONFIG_CMD_SNTP) -#include "sntp.h" -#endif +#include <watchdog.h> +#include "arp.h" +#include "bootp.h" #include "cdp.h" #if defined(CONFIG_CMD_DNS) #include "dns.h" #endif +#include "nfs.h" #include "ping.h" +#include "rarp.h" +#if defined(CONFIG_CMD_SNTP) +#include "sntp.h" +#endif +#include "tftp.h"
DECLARE_GLOBAL_DATA_PTR;
-- 1.6.0.2
Regards, Simon

This patch-series fixes a number of bugs in the network stack as well as improve the readability and maintainability of the network stack by eliminating duplicated code. These are more substantial changes than those in the "Network stack cosmetic improvements" series.
This is dependant on the "Network stack cosmetic improvements" patch-series.
This is a portion of the changes split from the series: "Add link-local addressing support"
Joe Hershberger (18): net: Refactor IP, UPD, and ICMP header writing functions net: Refactor NetSendUDPPacket to share more code net: Refactor packet length computations net: Refactor bootp packet length computations net: Move debug trace to point of action net: Refactor ping receive handler net: Refactor to use NetSendPacket instead of eth_send directly net: Refactor to protect access to the NetState variable net: Refactor to separate the UDP handler from the ARP handler net: Add NetUpdateEther() to handle ARP and Ping replies net: Don't write the "serverip" env var if configured not to net: Fix unused variable compile warning net: Add option CONFIG_BOOTP_MAY_FAIL net: Remove static allocation for MAC address in PingSend() net: Remove unused parameter from NetInitLoop() net: Fix net buffer initialization net: Make sure NetLoop is initialized when using NetConsole net: Don't copy every packet that waits for an ARP
README | 7 ++ common/cmd_net.c | 6 +- drivers/net/netconsole.c | 14 ++- include/net.h | 38 +++++-- net/arp.c | 48 +++++---- net/arp.h | 2 - net/bootp.c | 50 +++++---- net/cdp.c | 12 +-- net/dns.c | 10 +- net/net.c | 263 +++++++++++++++++++++++++++++----------------- net/nfs.c | 10 +- net/ping.c | 95 ++++++----------- net/rarp.c | 6 +- net/sntp.c | 6 +- net/tftp.c | 12 +- 15 files changed, 325 insertions(+), 254 deletions(-)

This patch-series fixes a number of bugs in the network stack as well as improve the readability and maintainability of the network stack by eliminating duplicated code. These are more substantial changes than those in the "Network stack cosmetic improvements" series.
This is dependant on the "Network stack cosmetic improvements" patch-series.
This is a portion of the changes split from the series: "Add link-local addressing support"
Joe Hershberger (18): net: Refactor IP, UPD, and ICMP header writing functions net: Refactor NetSendUDPPacket to share more code net: Refactor packet length computations net: Refactor bootp packet length computations net: Move debug trace to point of action net: Refactor ping receive handler net: Refactor to use NetSendPacket instead of eth_send directly net: Refactor to protect access to the NetState variable net: Refactor to separate the UDP handler from the ARP handler net: Add net_update_ether() to handle ARP and Ping replies net: Don't write the "serverip" env var if configured not to net: Fix unused variable compile warning net: Add option CONFIG_BOOTP_MAY_FAIL net: Remove static allocation for MAC address in PingSend() net: Remove unused parameter from NetInitLoop() net: Fix net buffer initialization net: Make sure NetLoop is initialized when using NetConsole net: Don't copy every packet that waits for an ARP
README | 7 ++ common/cmd_net.c | 8 +- drivers/net/netconsole.c | 16 ++- include/net.h | 44 ++++++--- net/arp.c | 48 +++++---- net/arp.h | 2 - net/bootp.c | 50 +++++---- net/cdp.c | 12 +-- net/dns.c | 10 +- net/net.c | 264 +++++++++++++++++++++++++++++----------------- net/nfs.c | 14 ++-- net/ping.c | 95 ++++++----------- net/rarp.c | 6 +- net/sntp.c | 6 +- net/tftp.c | 12 +- 15 files changed, 337 insertions(+), 257 deletions(-)

ICMP (ping) was reimplementing IP header code... it now shares code.
Signed-off-by: Joe Hershberger joe.hershberger@ni.com Cc: Joe Hershberger joe.hershberger@gmail.com --- Changes for v2: - Eliminate CamelCase in new functions Changes for v3: - Rebased onto other changed patches
include/net.h | 4 +++- net/bootp.c | 8 ++++---- net/net.c | 47 ++++++++++++++++++++++++++++------------------- net/ping.c | 52 +++++++++++++++++++++++----------------------------- 4 files changed, 58 insertions(+), 53 deletions(-)
diff --git a/include/net.h b/include/net.h index 3bf0806..128c5ea 100644 --- a/include/net.h +++ b/include/net.h @@ -457,7 +457,9 @@ extern int NetEthHdrSize(void); extern int NetSetEther(uchar *, uchar *, uint);
/* Set IP header */ -extern void NetSetIP(uchar *, IPaddr_t, int, int, int); +extern void net_set_ip_header(uchar *pkt, IPaddr_t dest, IPaddr_t source); +extern void net_set_udp_header(uchar *pkt, IPaddr_t dest, int dport, + int sport, int len);
/* Checksum */ extern int NetCksumOk(uchar *, int); /* Return true if cksum OK */ diff --git a/net/bootp.c b/net/bootp.c index 009369b..7346993 100644 --- a/net/bootp.c +++ b/net/bootp.c @@ -619,9 +619,9 @@ BootpRequest(void) * determined. * C. Hallinan, DS4.COM, Inc. */ - /* NetSetIP(pkt, 0xFFFFFFFFL, PORT_BOOTPS, PORT_BOOTPC, + /* net_set_udp_header(pkt, 0xFFFFFFFFL, PORT_BOOTPS, PORT_BOOTPC, sizeof (struct Bootp_t)); */ - iphdr = pkt; /* We need this later for NetSetIP() */ + iphdr = pkt; /* We need this later for net_set_udp_header() */ pkt += IP_UDP_HDR_SIZE;
bp = (struct Bootp_t *)pkt; @@ -663,7 +663,7 @@ BootpRequest(void) pktlen = ((int)(pkt-NetTxPacket)) + BOOTP_HDR_SIZE - sizeof(bp->bp_vend) + ext_len; iplen = BOOTP_HDR_SIZE - sizeof(bp->bp_vend) + ext_len; - NetSetIP(iphdr, 0xFFFFFFFFL, PORT_BOOTPS, PORT_BOOTPC, iplen); + net_set_udp_header(iphdr, 0xFFFFFFFFL, PORT_BOOTPS, PORT_BOOTPC, iplen); NetSetTimeout(SELECT_TIMEOUT, BootpTimeout);
#if defined(CONFIG_CMD_DHCP) @@ -844,7 +844,7 @@ static void DhcpSendRequestPkt(struct Bootp_t *bp_offer) pktlen = ((int)(pkt-NetTxPacket)) + BOOTP_HDR_SIZE - sizeof(bp->bp_vend) + extlen; iplen = BOOTP_HDR_SIZE - sizeof(bp->bp_vend) + extlen; - NetSetIP(iphdr, 0xFFFFFFFFL, PORT_BOOTPS, PORT_BOOTPC, iplen); + net_set_udp_header(iphdr, 0xFFFFFFFFL, PORT_BOOTPS, PORT_BOOTPC, iplen);
debug("Transmitting DHCPREQUEST packet: len = %d\n", pktlen); #ifdef CONFIG_BOOTP_DHCP_REQUEST_DELAY diff --git a/net/net.c b/net/net.c index de5352c..1c7bf60 100644 --- a/net/net.c +++ b/net/net.c @@ -618,7 +618,7 @@ int NetSendUDPPacket(uchar *ether, IPaddr_t dest, int dport, int sport, pkt = NetArpWaitTxPacket; pkt += NetSetEther(pkt, NetArpWaitPacketMAC, PROT_IP);
- NetSetIP(pkt, dest, dport, sport, payload_len); + net_set_udp_header(pkt, dest, dport, sport, payload_len); memcpy(pkt + IP_UDP_HDR_SIZE, (uchar *)NetTxPacket + (pkt - (uchar *)NetArpWaitTxPacket) + IP_UDP_HDR_SIZE, payload_len); @@ -638,7 +638,7 @@ int NetSendUDPPacket(uchar *ether, IPaddr_t dest, int dport, int sport,
pkt = (uchar *)NetTxPacket; pkt += NetSetEther(pkt, ether, PROT_IP); - NetSetIP(pkt, dest, dport, sport, payload_len); + net_set_udp_header(pkt, dest, dport, sport, payload_len); eth_send(NetTxPacket, (pkt - NetTxPacket) + IP_UDP_HDR_SIZE + payload_len);
@@ -1245,40 +1245,49 @@ NetSetEther(uchar *xet, uchar * addr, uint prot) } }
-void NetSetIP(uchar *xip, IPaddr_t dest, int dport, int sport, int len) +void net_set_ip_header(uchar *pkt, IPaddr_t dest, IPaddr_t source) { - struct ip_udp_hdr *ip = (struct ip_udp_hdr *)xip; + struct ip_udp_hdr *ip = (struct ip_udp_hdr *)pkt;
/* - * If the data is an odd number of bytes, zero the - * byte after the last byte so that the checksum - * will work. - */ - if (len & 1) - xip[IP_UDP_HDR_SIZE + len] = 0; - - /* - * Construct an IP and UDP header. - * (need to set no fragment bit - XXX) + * Construct an IP header. */ /* IP_HDR_SIZE / 4 (not including UDP) */ ip->ip_hl_v = 0x45; ip->ip_tos = 0; - ip->ip_len = htons(IP_UDP_HDR_SIZE + len); + ip->ip_len = htons(IP_HDR_SIZE); ip->ip_id = htons(NetIPID++); ip->ip_off = htons(IP_FLAGS_DFRAG); /* Don't fragment */ ip->ip_ttl = 255; - ip->ip_p = 17; /* UDP */ ip->ip_sum = 0; /* already in network byte order */ - NetCopyIP((void *)&ip->ip_src, &NetOurIP); - /* - "" - */ + NetCopyIP((void *)&ip->ip_src, &source); + /* already in network byte order */ NetCopyIP((void *)&ip->ip_dst, &dest); +} + +void net_set_udp_header(uchar *pkt, IPaddr_t dest, int dport, int sport, + int len) +{ + struct ip_udp_hdr *ip = (struct ip_udp_hdr *)pkt; + + /* + * If the data is an odd number of bytes, zero the + * byte after the last byte so that the checksum + * will work. + */ + if (len & 1) + pkt[IP_UDP_HDR_SIZE + len] = 0; + + net_set_ip_header(pkt, dest, NetOurIP); + ip->ip_len = htons(IP_UDP_HDR_SIZE + len); + ip->ip_p = IPPROTO_UDP; + ip->ip_sum = ~NetCksum((uchar *)ip, IP_HDR_SIZE >> 1); + ip->udp_src = htons(sport); ip->udp_dst = htons(dport); ip->udp_len = htons(UDP_HDR_SIZE + len); ip->udp_xsum = 0; - ip->ip_sum = ~NetCksum((uchar *)ip, IP_HDR_SIZE / 2); }
void copy_filename(char *dst, const char *src, int size) diff --git a/net/ping.c b/net/ping.c index 3102521..c525635 100644 --- a/net/ping.c +++ b/net/ping.c @@ -16,11 +16,31 @@ static ushort PingSeqNo; /* The ip address to ping */ IPaddr_t NetPingIP;
+static void set_icmp_header(uchar *pkt, IPaddr_t dest) +{ + /* + * Construct an IP and ICMP header. + */ + struct ip_hdr *ip = (struct ip_hdr *)pkt; + struct icmp_hdr *icmp = (struct icmp_hdr *)(pkt + IP_HDR_SIZE); + + net_set_ip_header(pkt, dest, NetOurIP); + + ip->ip_len = htons(IP_ICMP_HDR_SIZE); + ip->ip_p = IPPROTO_ICMP; + ip->ip_sum = ~NetCksum((uchar *)ip, IP_HDR_SIZE >> 1); + + icmp->type = ICMP_ECHO_REQUEST; + icmp->code = 0; + icmp->checksum = 0; + icmp->un.echo.id = 0; + icmp->un.echo.sequence = htons(PingSeqNo++); + icmp->checksum = ~NetCksum((uchar *)icmp, ICMP_HDR_SIZE >> 1); +} + static int ping_send(void) { static uchar mac[6]; - struct ip_udp_hdr *ip; - ushort *s; uchar *pkt;
/* XXX always send arp request */ @@ -35,33 +55,7 @@ static int ping_send(void) pkt = NetArpWaitTxPacket; pkt += NetSetEther(pkt, mac, PROT_IP);
- ip = (struct ip_udp_hdr *)pkt; - - /* - * Construct an IP and ICMP header. - * (need to set no fragment bit - XXX) - */ - /* IP_HDR_SIZE / 4 (not including UDP) */ - ip->ip_hl_v = 0x45; - ip->ip_tos = 0; - ip->ip_len = htons(IP_HDR_SIZE + 8); - ip->ip_id = htons(NetIPID++); - ip->ip_off = htons(IP_FLAGS_DFRAG); /* Don't fragment */ - ip->ip_ttl = 255; - ip->ip_p = 0x01; /* ICMP */ - ip->ip_sum = 0; - /* already in network byte order */ - NetCopyIP((void *)&ip->ip_src, &NetOurIP); - /* - "" - */ - NetCopyIP((void *)&ip->ip_dst, &NetPingIP); - ip->ip_sum = ~NetCksum((uchar *)ip, IP_HDR_SIZE / 2); - - s = &ip->udp_src; /* XXX ICMP starts here */ - s[0] = htons(0x0800); /* echo-request, code */ - s[1] = 0; /* checksum */ - s[2] = 0; /* identifier */ - s[3] = htons(PingSeqNo++); /* sequence number */ - s[1] = ~NetCksum((uchar *)s, 8/2); + set_icmp_header(pkt, NetPingIP);
/* size of the waiting packet */ NetArpWaitTxPacketSize =

Share more of the code that is common between ARP vs not.
Signed-off-by: Joe Hershberger joe.hershberger@ni.com Cc: Joe Hershberger joe.hershberger@gmail.com --- Changes for v2: Changes for v3: - Rebased onto other changed patches
net/net.c | 44 +++++++++++++++++++++++++------------------- 1 files changed, 25 insertions(+), 19 deletions(-)
diff --git a/net/net.c b/net/net.c index 1c7bf60..b29d372 100644 --- a/net/net.c +++ b/net/net.c @@ -595,6 +595,9 @@ int NetSendUDPPacket(uchar *ether, IPaddr_t dest, int dport, int sport, int payload_len) { uchar *pkt; + int need_arp = 0; + int eth_hdr_size; + int pkt_hdr_size;
/* convert to new style broadcast */ if (dest == 0) @@ -609,40 +612,43 @@ int NetSendUDPPacket(uchar *ether, IPaddr_t dest, int dport, int sport, * an ARP request */ if (memcmp(ether, NetEtherNullAddr, 6) == 0) { + need_arp = 1; + pkt = NetArpWaitTxPacket; + } else + pkt = (uchar *)NetTxPacket; + + eth_hdr_size = NetSetEther(pkt, ether, PROT_IP); + pkt += eth_hdr_size; + net_set_udp_header(pkt, dest, dport, sport, payload_len); + pkt_hdr_size = eth_hdr_size + IP_UDP_HDR_SIZE;
- debug("sending ARP for %08x\n", dest); + if (need_arp) { + debug("sending ARP for %pI4\n", &dest);
+ /* save the ip and eth addr for the packet to send after arp */ NetArpWaitPacketIP = dest; NetArpWaitPacketMAC = ether;
- pkt = NetArpWaitTxPacket; - pkt += NetSetEther(pkt, NetArpWaitPacketMAC, PROT_IP); - - net_set_udp_header(pkt, dest, dport, sport, payload_len); + /* + * Copy the packet data from the NetTxPacket into the + * NetArpWaitTxPacket to send after arp + */ memcpy(pkt + IP_UDP_HDR_SIZE, (uchar *)NetTxPacket + - (pkt - (uchar *)NetArpWaitTxPacket) + - IP_UDP_HDR_SIZE, payload_len); + pkt_hdr_size, payload_len);
/* size of the waiting packet */ - NetArpWaitTxPacketSize = (pkt - NetArpWaitTxPacket) + - IP_UDP_HDR_SIZE + payload_len; + NetArpWaitTxPacketSize = pkt_hdr_size + payload_len;
/* and do the ARP request */ NetArpWaitTry = 1; NetArpWaitTimerStart = get_timer(0); ArpRequest(); return 1; /* waiting */ + } else { + debug("sending UDP to %pI4/%pM\n", &dest, ether); + eth_send(NetTxPacket, pkt_hdr_size + payload_len); + return 0; /* transmitted */ } - - debug("sending UDP to %08x/%pM\n", dest, ether); - - pkt = (uchar *)NetTxPacket; - pkt += NetSetEther(pkt, ether, PROT_IP); - net_set_udp_header(pkt, dest, dport, sport, payload_len); - eth_send(NetTxPacket, (pkt - NetTxPacket) + IP_UDP_HDR_SIZE + - payload_len); - - return 0; /* transmitted */ }
#ifdef CONFIG_IP_DEFRAG

Save the length when it is computed instead of forgetting it and subtracting pointers to figure it out again.
Signed-off-by: Joe Hershberger joe.hershberger@ni.com Cc: Joe Hershberger joe.hershberger@gmail.com --- Changes for v2: Changes for v3: - Rebased onto other changed patches
net/arp.c | 13 ++++++++----- net/ping.c | 7 ++++--- net/rarp.c | 6 ++++-- 3 files changed, 16 insertions(+), 10 deletions(-)
diff --git a/net/arp.c b/net/arp.c index 5ed875c..e58a074 100644 --- a/net/arp.c +++ b/net/arp.c @@ -52,12 +52,14 @@ void ArpRequest(void) { uchar *pkt; struct arp_hdr *arp; + int eth_hdr_size;
debug("ARP broadcast %d\n", NetArpWaitTry);
pkt = NetTxPacket;
- pkt += NetSetEther(pkt, NetBcastAddr, PROT_ARP); + eth_hdr_size = NetSetEther(pkt, NetBcastAddr, PROT_ARP); + pkt += eth_hdr_size;
arp = (struct arp_hdr *) pkt;
@@ -86,7 +88,7 @@ void ArpRequest(void) }
NetWriteIP(&arp->ar_tpa, NetArpWaitReplyIP); - (void) eth_send(NetTxPacket, (pkt - NetTxPacket) + ARP_HDR_SIZE); + (void) eth_send(NetTxPacket, eth_hdr_size + ARP_HDR_SIZE); }
void ArpTimeoutCheck(void) @@ -118,6 +120,7 @@ void ArpReceive(struct ethernet_hdr *et, struct ip_udp_hdr *ip, int len) struct arp_hdr *arp; IPaddr_t reply_ip_addr; uchar *pkt; + int eth_hdr_size;
/* * We have to deal with two types of ARP packets: @@ -155,14 +158,14 @@ void ArpReceive(struct ethernet_hdr *et, struct ip_udp_hdr *ip, int len) /* reply with our IP address */ debug("Got ARP REQUEST, return our IP\n"); pkt = (uchar *)et; - pkt += NetSetEther(pkt, et->et_src, PROT_ARP); + eth_hdr_size = NetSetEther(pkt, et->et_src, PROT_ARP); + pkt += eth_hdr_size; arp->ar_op = htons(ARPOP_REPLY); memcpy(&arp->ar_tha, &arp->ar_sha, ARP_HLEN); NetCopyIP(&arp->ar_tpa, &arp->ar_spa); memcpy(&arp->ar_sha, NetOurEther, ARP_HLEN); NetCopyIP(&arp->ar_spa, &NetOurIP); - (void) eth_send((uchar *)et, - (pkt - (uchar *)et) + ARP_HDR_SIZE); + (void) eth_send((uchar *)et, eth_hdr_size + ARP_HDR_SIZE); return;
case ARPOP_REPLY: /* arp reply */ diff --git a/net/ping.c b/net/ping.c index c525635..939f8ff 100644 --- a/net/ping.c +++ b/net/ping.c @@ -42,6 +42,7 @@ static int ping_send(void) { static uchar mac[6]; uchar *pkt; + int eth_hdr_size;
/* XXX always send arp request */
@@ -53,13 +54,13 @@ static int ping_send(void) NetArpWaitPacketMAC = mac;
pkt = NetArpWaitTxPacket; - pkt += NetSetEther(pkt, mac, PROT_IP); + eth_hdr_size = NetSetEther(pkt, mac, PROT_IP); + pkt += eth_hdr_size;
set_icmp_header(pkt, NetPingIP);
/* size of the waiting packet */ - NetArpWaitTxPacketSize = - (pkt - NetArpWaitTxPacket) + IP_HDR_SIZE + 8; + NetArpWaitTxPacketSize = eth_hdr_size + IP_ICMP_HDR_SIZE;
/* and do the ARP request */ NetArpWaitTry = 1; diff --git a/net/rarp.c b/net/rarp.c index 8d7582e..0243177 100644 --- a/net/rarp.c +++ b/net/rarp.c @@ -88,11 +88,13 @@ void RarpRequest(void) { uchar *pkt; struct arp_hdr *rarp; + int eth_hdr_size;
printf("RARP broadcast %d\n", ++RarpTry); pkt = NetTxPacket;
- pkt += NetSetEther(pkt, NetBcastAddr, PROT_RARP); + eth_hdr_size = NetSetEther(pkt, NetBcastAddr, PROT_RARP); + pkt += eth_hdr_size;
rarp = (struct arp_hdr *)pkt;
@@ -108,7 +110,7 @@ void RarpRequest(void) /* dest IP addr set to broadcast */ memset(&rarp->ar_data[16], 0xff, 4);
- NetSendPacket(NetTxPacket, (pkt - NetTxPacket) + ARP_HDR_SIZE); + NetSendPacket(NetTxPacket, eth_hdr_size + ARP_HDR_SIZE);
NetSetTimeout(TIMEOUT, RarpTimeout); }

Eliminate pointer subtraction that recovers values computed earlier
Signed-off-by: Joe Hershberger joe.hershberger@ni.com Cc: Joe Hershberger joe.hershberger@gmail.com --- Changes for v2: - Split debug message move to a separate commit Changes for v3: - Rebased onto other changed patches
net/bootp.c | 24 +++++++++++++----------- 1 files changed, 13 insertions(+), 11 deletions(-)
diff --git a/net/bootp.c b/net/bootp.c index 7346993..84ac8a0 100644 --- a/net/bootp.c +++ b/net/bootp.c @@ -581,7 +581,8 @@ BootpRequest(void) { uchar *pkt, *iphdr; struct Bootp_t *bp; - int ext_len, pktlen, iplen; + int extlen, pktlen, iplen; + int eth_hdr_size; #ifdef CONFIG_BOOTP_RANDOM_DELAY ulong i, rand_ms; #endif @@ -610,7 +611,8 @@ BootpRequest(void) pkt = NetTxPacket; memset((void *)pkt, 0, PKTSIZE);
- pkt += NetSetEther(pkt, NetBcastAddr, PROT_IP); + eth_hdr_size = NetSetEther(pkt, NetBcastAddr, PROT_IP); + pkt += eth_hdr_size;
/* * Next line results in incorrect packet size being transmitted, @@ -639,9 +641,9 @@ BootpRequest(void)
/* Request additional information from the BOOTP/DHCP server */ #if defined(CONFIG_CMD_DHCP) - ext_len = DhcpExtended((u8 *)bp->bp_vend, DHCP_DISCOVER, 0, 0); + extlen = DhcpExtended((u8 *)bp->bp_vend, DHCP_DISCOVER, 0, 0); #else - ext_len = BootpExtended((u8 *)bp->bp_vend); + extlen = BootpExtended((u8 *)bp->bp_vend); #endif
/* @@ -660,9 +662,8 @@ BootpRequest(void) * Calculate proper packet lengths taking into account the * variable size of the options field */ - pktlen = ((int)(pkt-NetTxPacket)) + BOOTP_HDR_SIZE - - sizeof(bp->bp_vend) + ext_len; - iplen = BOOTP_HDR_SIZE - sizeof(bp->bp_vend) + ext_len; + iplen = BOOTP_HDR_SIZE - OPT_FIELD_SIZE + extlen; + pktlen = eth_hdr_size + IP_UDP_HDR_SIZE + iplen; net_set_udp_header(iphdr, 0xFFFFFFFFL, PORT_BOOTPS, PORT_BOOTPC, iplen); NetSetTimeout(SELECT_TIMEOUT, BootpTimeout);
@@ -798,13 +799,15 @@ static void DhcpSendRequestPkt(struct Bootp_t *bp_offer) uchar *pkt, *iphdr; struct Bootp_t *bp; int pktlen, iplen, extlen; + int eth_hdr_size; IPaddr_t OfferedIP;
debug("DhcpSendRequestPkt: Sending DHCPREQUEST\n"); pkt = NetTxPacket; memset((void *)pkt, 0, PKTSIZE);
- pkt += NetSetEther(pkt, NetBcastAddr, PROT_IP); + eth_hdr_size = NetSetEther(pkt, NetBcastAddr, PROT_IP); + pkt += eth_hdr_size;
iphdr = pkt; /* We'll need this later to set proper pkt size */ pkt += IP_UDP_HDR_SIZE; @@ -841,9 +844,8 @@ static void DhcpSendRequestPkt(struct Bootp_t *bp_offer) extlen = DhcpExtended((u8 *)bp->bp_vend, DHCP_REQUEST, NetDHCPServerIP, OfferedIP);
- pktlen = ((int)(pkt-NetTxPacket)) + BOOTP_HDR_SIZE - - sizeof(bp->bp_vend) + extlen; - iplen = BOOTP_HDR_SIZE - sizeof(bp->bp_vend) + extlen; + iplen = BOOTP_HDR_SIZE - OPT_FIELD_SIZE + extlen; + pktlen = eth_hdr_size + IP_UDP_HDR_SIZE + iplen; net_set_udp_header(iphdr, 0xFFFFFFFFL, PORT_BOOTPS, PORT_BOOTPC, iplen);
debug("Transmitting DHCPREQUEST packet: len = %d\n", pktlen);

Signed-off-by: Joe Hershberger joe.hershberger@ni.com Cc: Joe Hershberger joe.hershberger@gmail.com --- Changes for v2: - Split from "Refactor bootp packet length computations" Changes for v3: - Rebased onto other changed patches
net/bootp.c | 2 +- 1 files changed, 1 insertions(+), 1 deletions(-)
diff --git a/net/bootp.c b/net/bootp.c index 84ac8a0..b472051 100644 --- a/net/bootp.c +++ b/net/bootp.c @@ -848,10 +848,10 @@ static void DhcpSendRequestPkt(struct Bootp_t *bp_offer) pktlen = eth_hdr_size + IP_UDP_HDR_SIZE + iplen; net_set_udp_header(iphdr, 0xFFFFFFFFL, PORT_BOOTPS, PORT_BOOTPC, iplen);
- debug("Transmitting DHCPREQUEST packet: len = %d\n", pktlen); #ifdef CONFIG_BOOTP_DHCP_REQUEST_DELAY udelay(CONFIG_BOOTP_DHCP_REQUEST_DELAY); #endif /* CONFIG_BOOTP_DHCP_REQUEST_DELAY */ + debug("Transmitting DHCPREQUEST packet: len = %d\n", pktlen); NetSendPacket(NetTxPacket, pktlen); }

There is no need to call through the handler... inline it
Signed-off-by: Joe Hershberger joe.hershberger@ni.com Cc: Joe Hershberger joe.hershberger@gmail.com --- Changes for v2: Changes for v3: - Rebased onto other changed patches
net/ping.c | 18 ++---------------- 1 files changed, 2 insertions(+), 16 deletions(-)
diff --git a/net/ping.c b/net/ping.c index 939f8ff..0f4d990 100644 --- a/net/ping.c +++ b/net/ping.c @@ -75,20 +75,10 @@ static void ping_timeout(void) NetState = NETLOOP_FAIL; /* we did not get the reply */ }
-static void ping_handler(uchar *pkt, unsigned dest, IPaddr_t sip, - unsigned src, unsigned len) -{ - if (sip != NetPingIP) - return; - - NetState = NETLOOP_SUCCESS; -} - void ping_start(void) { printf("Using %s device\n", eth_get_name()); NetSetTimeout(10000UL, ping_timeout); - NetSetHandler(ping_handler);
ping_send(); } @@ -100,13 +90,9 @@ void ping_receive(struct ethernet_hdr *et, struct ip_udp_hdr *ip, int len)
switch (icmph->type) { case ICMP_ECHO_REPLY: - /* - * IP header OK. Pass the packet to the - * current handler. - */ - /* XXX point to ip packet */ src_ip = NetReadIP((void *)&ip->ip_src); - NetGetHandler()((uchar *)ip, 0, src_ip, 0, 0); + if (src_ip == NetPingIP) + NetState = NETLOOP_SUCCESS; return; case ICMP_ECHO_REQUEST: debug("Got ICMP ECHO REQUEST, return "

Use this entry-point consistently across the net/ code Use a static inline function to preserve code size
Signed-off-by: Joe Hershberger joe.hershberger@ni.com Cc: Joe Hershberger joe.hershberger@gmail.com --- Changes for v2: - Change NetSendPacket into a static inline Changes for v3: - Rebased onto other changed patches
include/net.h | 5 ++++- net/arp.c | 6 +++--- net/cdp.c | 2 +- net/net.c | 9 +-------- net/ping.c | 3 +-- 5 files changed, 10 insertions(+), 15 deletions(-)
diff --git a/include/net.h b/include/net.h index 128c5ea..3214830 100644 --- a/include/net.h +++ b/include/net.h @@ -472,7 +472,10 @@ extern void net_set_icmp_handler(rxhand_icmp_f *f); /* Set ICMP RX handler */ extern void NetSetTimeout(ulong, thand_f *);/* Set timeout handler */
/* Transmit "NetTxPacket" */ -extern void NetSendPacket(uchar *, int); +static inline void NetSendPacket(uchar *pkt, int len) +{ + (void) eth_send(pkt, len); +}
/* * Transmit UDP packet, performing ARP request if needed diff --git a/net/arp.c b/net/arp.c index e58a074..997c2ab 100644 --- a/net/arp.c +++ b/net/arp.c @@ -88,7 +88,7 @@ void ArpRequest(void) }
NetWriteIP(&arp->ar_tpa, NetArpWaitReplyIP); - (void) eth_send(NetTxPacket, eth_hdr_size + ARP_HDR_SIZE); + NetSendPacket(NetTxPacket, eth_hdr_size + ARP_HDR_SIZE); }
void ArpTimeoutCheck(void) @@ -165,7 +165,7 @@ void ArpReceive(struct ethernet_hdr *et, struct ip_udp_hdr *ip, int len) NetCopyIP(&arp->ar_tpa, &arp->ar_spa); memcpy(&arp->ar_sha, NetOurEther, ARP_HLEN); NetCopyIP(&arp->ar_spa, &NetOurIP); - (void) eth_send((uchar *)et, eth_hdr_size + ARP_HDR_SIZE); + NetSendPacket((uchar *)et, eth_hdr_size + ARP_HDR_SIZE); return;
case ARPOP_REPLY: /* arp reply */ @@ -198,7 +198,7 @@ void ArpReceive(struct ethernet_hdr *et, struct ip_udp_hdr *ip, int len) /* modify header, and transmit it */ memcpy(((struct ethernet_hdr *)NetArpWaitTxPacket)-> et_dest, NetArpWaitPacketMAC, ARP_HLEN); - (void) eth_send(NetArpWaitTxPacket, + NetSendPacket(NetArpWaitTxPacket, NetArpWaitTxPacketSize);
/* no arp request pending now */ diff --git a/net/cdp.c b/net/cdp.c index 9bec441..3dab6a1 100644 --- a/net/cdp.c +++ b/net/cdp.c @@ -216,7 +216,7 @@ CDPSendTrigger(void) chksum = 0xFFFF; *cp = htons(chksum);
- (void) eth_send(NetTxPacket, (uchar *)s - NetTxPacket); + NetSendPacket(NetTxPacket, (uchar *)s - NetTxPacket); return 0; }
diff --git a/net/net.c b/net/net.c index b29d372..e5fbda3 100644 --- a/net/net.c +++ b/net/net.c @@ -584,13 +584,6 @@ NetSetTimeout(ulong iv, thand_f *f) } }
- -void -NetSendPacket(uchar *pkt, int len) -{ - (void) eth_send(pkt, len); -} - int NetSendUDPPacket(uchar *ether, IPaddr_t dest, int dport, int sport, int payload_len) { @@ -646,7 +639,7 @@ int NetSendUDPPacket(uchar *ether, IPaddr_t dest, int dport, int sport, return 1; /* waiting */ } else { debug("sending UDP to %pI4/%pM\n", &dest, ether); - eth_send(NetTxPacket, pkt_hdr_size + payload_len); + NetSendPacket(NetTxPacket, pkt_hdr_size + payload_len); return 0; /* transmitted */ } } diff --git a/net/ping.c b/net/ping.c index 0f4d990..f0026cc 100644 --- a/net/ping.c +++ b/net/ping.c @@ -112,8 +112,7 @@ void ping_receive(struct ethernet_hdr *et, struct ip_udp_hdr *ip, int len) icmph->checksum = 0; icmph->checksum = ~NetCksum((uchar *)icmph, (len - IP_HDR_SIZE) >> 1); - (void) eth_send((uchar *)et, - ETHER_HDR_SIZE + len); + NetSendPacket((uchar *)et, ETHER_HDR_SIZE + len); return; /* default: return;*/

Changes to NetState now go through an accessor function called net_set_state()
Signed-off-by: Joe Hershberger joe.hershberger@ni.com Cc: Joe Hershberger joe.hershberger@gmail.com --- Changes for v2: - net_set_state changed to static inline - States changed to an enum - Eliminate CamelCase in new functions. Changes for v3: - Rebased onto other changed patches - Added a blank line - Renamed NfsDownloadState to nfs_download_state - Put net_loop_state values on separate lines
drivers/net/netconsole.c | 6 +++--- include/net.h | 20 ++++++++++++++------ net/cdp.c | 2 +- net/dns.c | 8 ++++---- net/net.c | 21 ++++++++++++--------- net/nfs.c | 12 ++++++------ net/ping.c | 4 ++-- net/sntp.c | 4 ++-- net/tftp.c | 8 ++++---- 9 files changed, 48 insertions(+), 37 deletions(-)
diff --git a/drivers/net/netconsole.c b/drivers/net/netconsole.c index ba02fd7..28bb955 100644 --- a/drivers/net/netconsole.c +++ b/drivers/net/netconsole.c @@ -44,19 +44,19 @@ static void nc_wait_arp_handler(uchar *pkt, unsigned dest, IPaddr_t sip, unsigned src, unsigned len) { - NetState = NETLOOP_SUCCESS; /* got arp reply - quit net loop */ + net_set_state(NETLOOP_SUCCESS); /* got arp reply - quit net loop */ }
static void nc_handler(uchar *pkt, unsigned dest, IPaddr_t sip, unsigned src, unsigned len) { if (input_size) - NetState = NETLOOP_SUCCESS; /* got input - quit net loop */ + net_set_state(NETLOOP_SUCCESS); /* got input - quit net loop */ }
static void nc_timeout(void) { - NetState = NETLOOP_SUCCESS; + net_set_state(NETLOOP_SUCCESS); }
void NcStart(void) diff --git a/include/net.h b/include/net.h index 3214830..8dd407e 100644 --- a/include/net.h +++ b/include/net.h @@ -391,12 +391,6 @@ extern uchar NetEtherNullAddr[6]; extern ushort NetOurVLAN; /* Our VLAN */ extern ushort NetOurNativeVLAN; /* Our Native VLAN */
-extern int NetState; /* Network loop state */ -#define NETLOOP_CONTINUE 1 -#define NETLOOP_RESTART 2 -#define NETLOOP_SUCCESS 3 -#define NETLOOP_FAIL 4 - extern int NetRestartWrap; /* Tried all network devices */
enum proto_t { @@ -471,6 +465,20 @@ extern void NetSetHandler(rxhand_f *); /* Set RX packet handler */ extern void net_set_icmp_handler(rxhand_icmp_f *f); /* Set ICMP RX handler */ extern void NetSetTimeout(ulong, thand_f *);/* Set timeout handler */
+/* Network loop state */ +enum net_loop_state { + NETLOOP_CONTINUE, + NETLOOP_RESTART, + NETLOOP_SUCCESS, + NETLOOP_FAIL +}; +static inline void net_set_state(enum net_loop_state state) +{ + extern enum net_loop_state net_state; + + net_state = state; +} + /* Transmit "NetTxPacket" */ static inline void NetSendPacket(uchar *pkt, int len) { diff --git a/net/cdp.c b/net/cdp.c index 3dab6a1..fa92409 100644 --- a/net/cdp.c +++ b/net/cdp.c @@ -235,7 +235,7 @@ CDPTimeout(void) if (!CDPOK) NetStartAgain(); else - NetState = NETLOOP_SUCCESS; + net_set_state(NETLOOP_SUCCESS); }
static void diff --git a/net/dns.c b/net/dns.c index cc7ed51..cc0aa0a 100644 --- a/net/dns.c +++ b/net/dns.c @@ -98,7 +98,7 @@ static void DnsTimeout(void) { puts("Timeout\n"); - NetState = NETLOOP_FAIL; + net_set_state(NETLOOP_FAIL); }
static void @@ -128,7 +128,7 @@ DnsHandler(uchar *pkt, unsigned dest, IPaddr_t sip, unsigned src, unsigned len) /* Received 0 answers */ if (header->nanswers == 0) { puts("DNS: host not found\n"); - NetState = NETLOOP_SUCCESS; + net_set_state(NETLOOP_SUCCESS); return; }
@@ -141,7 +141,7 @@ DnsHandler(uchar *pkt, unsigned dest, IPaddr_t sip, unsigned src, unsigned len) /* We sent query class 1, query type 1 */ if (&p[5] > e || get_unaligned_be16(p+1) != DNS_A_RECORD) { puts("DNS: response was not an A record\n"); - NetState = NETLOOP_SUCCESS; + net_set_state(NETLOOP_SUCCESS); return; }
@@ -191,7 +191,7 @@ DnsHandler(uchar *pkt, unsigned dest, IPaddr_t sip, unsigned src, unsigned len) puts("server responded with invalid IP number\n"); }
- NetState = NETLOOP_SUCCESS; + net_set_state(NETLOOP_SUCCESS); }
void diff --git a/net/net.c b/net/net.c index e5fbda3..60be978 100644 --- a/net/net.c +++ b/net/net.c @@ -149,7 +149,7 @@ uchar NetEtherNullAddr[6]; void (*push_packet)(void *, int len) = 0; #endif /* Network loop state */ -int NetState; +enum net_loop_state net_state; /* Tried all network devices */ int NetRestartWrap; /* Network loop restarted */ @@ -212,7 +212,7 @@ void net_auto_load(void) * Just use BOOTP/RARP to configure system; * Do not use TFTP to load the bootfile. */ - NetState = NETLOOP_SUCCESS; + net_set_state(NETLOOP_SUCCESS); return; } #if defined(CONFIG_CMD_NFS) @@ -290,7 +290,7 @@ int NetLoop(enum proto_t protocol) restart: memcpy(NetOurEther, eth_get_dev()->enetaddr, 6);
- NetState = NETLOOP_CONTINUE; + net_set_state(NETLOOP_CONTINUE);
/* * Start the ball rolling with the given start function. From @@ -399,7 +399,7 @@ restart:
/* * Main packet reception loop. Loop receiving packets until - * someone sets `NetState' to a state that terminates. + * someone sets `net_state' to a state that terminates. */ for (;;) { WATCHDOG_RESET(); @@ -451,7 +451,7 @@ restart: }
- switch (NetState) { + switch (net_state) {
case NETLOOP_RESTART: NetRestarted = 1; @@ -475,6 +475,9 @@ restart:
case NETLOOP_FAIL: goto done; + + case NETLOOP_CONTINUE: + continue; } }
@@ -492,7 +495,7 @@ done: static void startAgainTimeout(void) { - NetState = NETLOOP_RESTART; + net_set_state(NETLOOP_RESTART); }
static void @@ -523,7 +526,7 @@ void NetStartAgain(void)
if ((!retry_forever) && (NetTryCount >= retrycnt)) { eth_halt(); - NetState = NETLOOP_FAIL; + net_set_state(NETLOOP_FAIL); return; }
@@ -540,10 +543,10 @@ void NetStartAgain(void) NetSetTimeout(10000UL, startAgainTimeout); NetSetHandler(startAgainHandler); } else { - NetState = NETLOOP_FAIL; + net_set_state(NETLOOP_FAIL); } } else { - NetState = NETLOOP_RESTART; + net_set_state(NETLOOP_RESTART); } }
diff --git a/net/nfs.c b/net/nfs.c index b6188fe..db73e93 100644 --- a/net/nfs.c +++ b/net/nfs.c @@ -41,7 +41,7 @@ static int nfs_len; static char dirfh[NFS_FHSIZE]; /* file handle of directory */ static char filefh[NFS_FHSIZE]; /* file handle of kernel image */
-static int NfsDownloadState; +static enum net_loop_state nfs_download_state; static IPaddr_t NfsServerIP; static int NfsSrvMountPort; static int NfsSrvNfsPort; @@ -613,10 +613,10 @@ NfsHandler(uchar *pkt, unsigned dest, IPaddr_t sip, unsigned src, unsigned len) case STATE_UMOUNT_REQ: if (nfs_umountall_reply(pkt, len)) { puts("*** ERROR: Cannot umount\n"); - NetState = NETLOOP_FAIL; + net_set_state(NETLOOP_FAIL); } else { puts("\ndone\n"); - NetState = NfsDownloadState; + net_set_state(nfs_download_state); } break;
@@ -660,7 +660,7 @@ NfsHandler(uchar *pkt, unsigned dest, IPaddr_t sip, unsigned src, unsigned len) NfsSend(); } else { if (!rlen) - NfsDownloadState = NETLOOP_SUCCESS; + nfs_download_state = NETLOOP_SUCCESS; NfsState = STATE_UMOUNT_REQ; NfsSend(); } @@ -673,13 +673,13 @@ void NfsStart(void) { debug("%s\n", __func__); - NfsDownloadState = NETLOOP_FAIL; + nfs_download_state = NETLOOP_FAIL;
NfsServerIP = NetServerIP; nfs_path = (char *)nfs_path_buff;
if (nfs_path == NULL) { - NetState = NETLOOP_FAIL; + net_set_state(NETLOOP_FAIL); puts("*** ERROR: Fail allocate memory\n"); return; } diff --git a/net/ping.c b/net/ping.c index f0026cc..9c690d8 100644 --- a/net/ping.c +++ b/net/ping.c @@ -72,7 +72,7 @@ static int ping_send(void) static void ping_timeout(void) { eth_halt(); - NetState = NETLOOP_FAIL; /* we did not get the reply */ + net_set_state(NETLOOP_FAIL); /* we did not get the reply */ }
void ping_start(void) @@ -92,7 +92,7 @@ void ping_receive(struct ethernet_hdr *et, struct ip_udp_hdr *ip, int len) case ICMP_ECHO_REPLY: src_ip = NetReadIP((void *)&ip->ip_src); if (src_ip == NetPingIP) - NetState = NETLOOP_SUCCESS; + net_set_state(NETLOOP_SUCCESS); return; case ICMP_ECHO_REQUEST: debug("Got ICMP ECHO REQUEST, return " diff --git a/net/sntp.c b/net/sntp.c index 07d036d..48ce07f 100644 --- a/net/sntp.c +++ b/net/sntp.c @@ -45,7 +45,7 @@ static void SntpTimeout(void) { puts("Timeout\n"); - NetState = NETLOOP_FAIL; + net_set_state(NETLOOP_FAIL); return; }
@@ -76,7 +76,7 @@ SntpHandler(uchar *pkt, unsigned dest, IPaddr_t sip, unsigned src, tm.tm_year, tm.tm_mon, tm.tm_mday, tm.tm_hour, tm.tm_min, tm.tm_sec);
- NetState = NETLOOP_SUCCESS; + net_set_state(NETLOOP_SUCCESS); }
void diff --git a/net/tftp.c b/net/tftp.c index a04a832..bf32eab 100644 --- a/net/tftp.c +++ b/net/tftp.c @@ -177,7 +177,7 @@ store_block(unsigned block, uchar *src, unsigned len) rc = flash_write((char *)src, (ulong)(load_addr+offset), len); if (rc) { flash_perror(rc); - NetState = NETLOOP_FAIL; + net_set_state(NETLOOP_FAIL); return; } } else @@ -300,7 +300,7 @@ static void tftp_complete(void) } #endif puts("\ndone\n"); - NetState = NETLOOP_SUCCESS; + net_set_state(NETLOOP_SUCCESS); }
static void @@ -627,7 +627,7 @@ TftpHandler(uchar *pkt, unsigned dest, IPaddr_t sip, unsigned src, if (MasterClient && (TftpBlock >= TftpEndingBlock)) { puts("\nMulticast tftp done\n"); mcast_cleanup(); - NetState = NETLOOP_SUCCESS; + net_set_state(NETLOOP_SUCCESS); } } else #endif @@ -644,7 +644,7 @@ TftpHandler(uchar *pkt, unsigned dest, IPaddr_t sip, unsigned src, case TFTP_ERR_ACCESS_DENIED: puts("Not retrying...\n"); eth_halt(); - NetState = NETLOOP_FAIL; + net_set_state(NETLOOP_FAIL); break; case TFTP_ERR_UNDEFINED: case TFTP_ERR_DISK_FULL:

Call a built-in dummy if none is registered... don't require protocols to register a handler (eliminating dummies) NetConsole now uses the ARP handler when waiting on arp (instead of needing a #define hack in arp.c) Clear handlers at the end of net loop
Signed-off-by: Joe Hershberger joe.hershberger@ni.com Cc: Joe Hershberger joe.hershberger@gmail.com --- Changes for v2: - Change NULL checks into dummy functions - Must now call net_set_*_handler(NULL); in init to set dummies - Eliminate CamelCase for new functions Changes for v3: - Rebased onto other changed patches - Fixed comment style - Cleaned up parameters on line wrap
drivers/net/netconsole.c | 4 +- include/net.h | 7 ++- net/arp.c | 6 +- net/bootp.c | 4 +- net/cdp.c | 8 ---- net/dns.c | 2 +- net/net.c | 95 ++++++++++++++++++++++++++++++---------------- net/nfs.c | 2 +- net/sntp.c | 2 +- net/tftp.c | 4 +- 10 files changed, 79 insertions(+), 55 deletions(-)
diff --git a/drivers/net/netconsole.c b/drivers/net/netconsole.c index 28bb955..744f4d1 100644 --- a/drivers/net/netconsole.c +++ b/drivers/net/netconsole.c @@ -63,12 +63,12 @@ void NcStart(void) { if (!output_packet_len || memcmp(nc_ether, NetEtherNullAddr, 6)) { /* going to check for input packet */ - NetSetHandler(nc_handler); + net_set_udp_handler(nc_handler); NetSetTimeout(net_timeout, nc_timeout); } else { /* send arp request */ uchar *pkt; - NetSetHandler(nc_wait_arp_handler); + net_set_arp_handler(nc_wait_arp_handler); pkt = (uchar *)NetTxPacket + NetEthHdrSize() + IP_UDP_HDR_SIZE; memcpy(pkt, output_packet, output_packet_len); NetSendUDPPacket(nc_ether, nc_ip, nc_port, nc_port, diff --git a/include/net.h b/include/net.h index 8dd407e..09f040f 100644 --- a/include/net.h +++ b/include/net.h @@ -460,8 +460,10 @@ extern int NetCksumOk(uchar *, int); /* Return true if cksum OK */ extern uint NetCksum(uchar *, int); /* Calculate the checksum */
/* Callbacks */ -extern rxhand_f *NetGetHandler(void); /* Get RX packet handler */ -extern void NetSetHandler(rxhand_f *); /* Set RX packet handler */ +extern rxhand_f *net_get_udp_handler(void); /* Get UDP RX packet handler */ +extern void net_set_udp_handler(rxhand_f *); /* Set UDP RX packet handler */ +extern rxhand_f *net_get_arp_handler(void); /* Get ARP RX packet handler */ +extern void net_set_arp_handler(rxhand_f *); /* Set ARP RX packet handler */ extern void net_set_icmp_handler(rxhand_icmp_f *f); /* Set ICMP RX handler */ extern void NetSetTimeout(ulong, thand_f *);/* Set timeout handler */
@@ -487,6 +489,7 @@ static inline void NetSendPacket(uchar *pkt, int len)
/* * Transmit UDP packet, performing ARP request if needed + * (ether will be populated) * * @param ether Raw packet buffer * @param dest IP address to send the datagram to diff --git a/net/arp.c b/net/arp.c index 997c2ab..fcff536 100644 --- a/net/arp.c +++ b/net/arp.c @@ -192,9 +192,9 @@ void ArpReceive(struct ethernet_hdr *et, struct ip_udp_hdr *ip, int len) memcpy(NetArpWaitPacketMAC, &arp->ar_sha, ARP_HLEN);
-#ifdef CONFIG_NETCONSOLE - NetGetHandler()(0, 0, 0, 0, 0); -#endif + net_get_arp_handler()((uchar *)arp, 0, reply_ip_addr, + 0, len); + /* modify header, and transmit it */ memcpy(((struct ethernet_hdr *)NetArpWaitTxPacket)-> et_dest, NetArpWaitPacketMAC, ARP_HLEN); diff --git a/net/bootp.c b/net/bootp.c index b472051..b36abac 100644 --- a/net/bootp.c +++ b/net/bootp.c @@ -669,9 +669,9 @@ BootpRequest(void)
#if defined(CONFIG_CMD_DHCP) dhcp_state = SELECTING; - NetSetHandler(DhcpHandler); + net_set_udp_handler(DhcpHandler); #else - NetSetHandler(BootpHandler); + net_set_udp_handler(BootpHandler); #endif NetSendPacket(NetTxPacket, pktlen); } diff --git a/net/cdp.c b/net/cdp.c index fa92409..3d9559e 100644 --- a/net/cdp.c +++ b/net/cdp.c @@ -238,13 +238,6 @@ CDPTimeout(void) net_set_state(NETLOOP_SUCCESS); }
-static void -CDPDummyHandler(uchar *pkt, unsigned dest, IPaddr_t sip, unsigned src, - unsigned len) -{ - /* nothing */ -} - void cdp_receive(const uchar *pkt, unsigned len) { const uchar *t; @@ -368,7 +361,6 @@ CDPStart(void) CDPApplianceVLAN = htons(-1);
NetSetTimeout(CDP_TIMEOUT, CDPTimeout); - NetSetHandler(CDPDummyHandler);
CDPSendTrigger(); } diff --git a/net/dns.c b/net/dns.c index cc0aa0a..ff9ddff 100644 --- a/net/dns.c +++ b/net/dns.c @@ -200,7 +200,7 @@ DnsStart(void) debug("%s\n", __func__);
NetSetTimeout(DNS_TIMEOUT, DnsTimeout); - NetSetHandler(DnsHandler); + net_set_udp_handler(DnsHandler);
DnsSend(); } diff --git a/net/net.c b/net/net.c index 60be978..ccac70f 100644 --- a/net/net.c +++ b/net/net.c @@ -178,10 +178,13 @@ uchar PktBuf[(PKTBUFSRX+1) * PKTSIZE_ALIGN + PKTALIGN]; /* Receive packet */ uchar *NetRxPackets[PKTBUFSRX];
-/* Current RX packet handler */ -static rxhand_f *packetHandler; +/* Current UDP RX packet handler */ +static rxhand_f *udp_packet_handler; +/* Current ARP RX packet handler */ +static rxhand_f *arp_packet_handler; #ifdef CONFIG_CMD_TFTPPUT -static rxhand_icmp_f *packet_icmp_handler; /* Current ICMP rx handler */ +/* Current ICMP rx handler */ +static rxhand_icmp_f *packet_icmp_handler; #endif /* Current timeout handler */ static thand_f *timeHandler; @@ -250,6 +253,18 @@ static void NetInitLoop(enum proto_t protocol) return; }
+static void net_clear_handlers(void) +{ + net_set_udp_handler(NULL); + net_set_arp_handler(NULL); + NetSetTimeout(0, NULL); +} + +static void net_cleanup_loop(void) +{ + net_clear_handlers(); +} + /**********************************************************************/ /* * Main network processing loop. @@ -257,6 +272,7 @@ static void NetInitLoop(enum proto_t protocol)
int NetLoop(enum proto_t protocol) { + int i; bd_t *bd = gd->bd; int ret = -1;
@@ -267,17 +283,15 @@ int NetLoop(enum proto_t protocol) NetTryCount = 1;
ArpInit(); + net_clear_handlers();
- if (!NetTxPacket) { - int i; - /* - * Setup packet buffers, aligned correctly. - */ - NetTxPacket = &PktBuf[0] + (PKTALIGN - 1); - NetTxPacket -= (ulong)NetTxPacket % PKTALIGN; - for (i = 0; i < PKTBUFSRX; i++) - NetRxPackets[i] = NetTxPacket + (i+1)*PKTSIZE_ALIGN; - } + /* + * Setup packet buffers, aligned correctly. + */ + NetTxPacket = &PktBuf[0] + (PKTALIGN - 1); + NetTxPacket -= (ulong)NetTxPacket % PKTALIGN; + for (i = 0; i < PKTBUFSRX; i++) + NetRxPackets[i] = NetTxPacket + (i+1)*PKTSIZE_ALIGN;
bootstage_mark_name(BOOTSTAGE_ID_ETH_START, "eth_start"); eth_halt(); @@ -416,6 +430,7 @@ restart: * Abort if ctrl-c was pressed. */ if (ctrlc()) { + net_cleanup_loop(); eth_halt(); puts("\nAbort\n"); goto done; @@ -458,6 +473,7 @@ restart: goto restart;
case NETLOOP_SUCCESS: + net_cleanup_loop(); if (NetBootFileXferSize > 0) { char buf[20]; printf("Bytes transferred = %ld (%lx hex)\n", @@ -474,6 +490,7 @@ restart: goto done;
case NETLOOP_FAIL: + net_cleanup_loop(); goto done;
case NETLOOP_CONTINUE: @@ -484,7 +501,7 @@ restart: done: #ifdef CONFIG_CMD_TFTPPUT /* Clear out the handlers */ - NetSetHandler(NULL); + net_set_upd_handler(NULL); net_set_icmp_handler(NULL); #endif return ret; @@ -498,13 +515,6 @@ startAgainTimeout(void) net_set_state(NETLOOP_RESTART); }
-static void -startAgainHandler(uchar *pkt, unsigned dest, IPaddr_t sip, - unsigned src, unsigned len) -{ - /* Totally ignore the packet */ -} - void NetStartAgain(void) { char *nretry; @@ -541,7 +551,7 @@ void NetStartAgain(void) NetRestartWrap = 0; if (NetDevExists) { NetSetTimeout(10000UL, startAgainTimeout); - NetSetHandler(startAgainHandler); + net_set_udp_handler(NULL); } else { net_set_state(NETLOOP_FAIL); } @@ -555,17 +565,36 @@ void NetStartAgain(void) * Miscelaneous bits. */
-rxhand_f * -NetGetHandler(void) +static void dummy_handler(uchar *pkt, unsigned dport, + IPaddr_t sip, unsigned sport, + unsigned len) { - return packetHandler; }
+rxhand_f *net_get_udp_handler(void) +{ + return udp_packet_handler; +}
-void -NetSetHandler(rxhand_f *f) +void net_set_udp_handler(rxhand_f *f) +{ + if (f == NULL) + udp_packet_handler = dummy_handler; + else + udp_packet_handler = f; +} + +rxhand_f *net_get_arp_handler(void) { - packetHandler = f; + return arp_packet_handler; +} + +void net_set_arp_handler(rxhand_f *f) +{ + if (f == NULL) + arp_packet_handler = dummy_handler; + else + arp_packet_handler = f; }
#ifdef CONFIG_CMD_TFTPPUT @@ -1091,11 +1120,11 @@ NetReceive(uchar *inpkt, int len) /* * IP header OK. Pass the packet to the current handler. */ - (*packetHandler)((uchar *)ip + IP_UDP_HDR_SIZE, - ntohs(ip->udp_dst), - src_ip, - ntohs(ip->udp_src), - ntohs(ip->udp_len) - UDP_HDR_SIZE); + (*udp_packet_handler)((uchar *)ip + IP_UDP_HDR_SIZE, + ntohs(ip->udp_dst), + src_ip, + ntohs(ip->udp_src), + ntohs(ip->udp_len) - UDP_HDR_SIZE); break; } } diff --git a/net/nfs.c b/net/nfs.c index db73e93..5b99763 100644 --- a/net/nfs.c +++ b/net/nfs.c @@ -735,7 +735,7 @@ NfsStart(void) "Loading: *\b", load_addr);
NetSetTimeout(NFS_TIMEOUT, NfsTimeout); - NetSetHandler(NfsHandler); + net_set_udp_handler(NfsHandler);
NfsTimeoutCount = 0; NfsState = STATE_PRCLOOKUP_PROG_MOUNT_REQ; diff --git a/net/sntp.c b/net/sntp.c index 48ce07f..5de1952 100644 --- a/net/sntp.c +++ b/net/sntp.c @@ -85,7 +85,7 @@ SntpStart(void) debug("%s\n", __func__);
NetSetTimeout(SNTP_TIMEOUT, SntpTimeout); - NetSetHandler(SntpHandler); + net_set_udp_handler(SntpHandler); memset(NetServerEther, 0, sizeof(NetServerEther));
SntpSend(); diff --git a/net/tftp.c b/net/tftp.c index bf32eab..b2e08b4 100644 --- a/net/tftp.c +++ b/net/tftp.c @@ -778,7 +778,7 @@ void TftpStart(enum proto_t protocol) TftpTimeoutCountMax = TftpRRQTimeoutCountMax;
NetSetTimeout(TftpTimeoutMSecs, TftpTimeout); - NetSetHandler(TftpHandler); + net_set_udp_handler(TftpHandler); #ifdef CONFIG_CMD_TFTPPUT net_set_icmp_handler(icmp_handler); #endif @@ -840,7 +840,7 @@ TftpStartServer(void) #endif
TftpState = STATE_RECV_WRQ; - NetSetHandler(TftpHandler); + net_set_udp_handler(TftpHandler); } #endif /* CONFIG_CMD_TFTPSRV */

When the network is VLAN or SNAP, net_update_ether() will preserve the original Ethernet packet header and simply replace the src and dest MACs and the protocol
Signed-off-by: Joe Hershberger joe.hershberger@ni.com Cc: Joe Hershberger joe.hershberger@gmail.com --- Changes for v2: Changes for v3: - Rebased onto other changed patches
include/net.h | 1 + net/arp.c | 2 +- net/net.c | 23 +++++++++++++++++++++++ net/ping.c | 10 +++++----- 4 files changed, 30 insertions(+), 6 deletions(-)
diff --git a/include/net.h b/include/net.h index 09f040f..9564051 100644 --- a/include/net.h +++ b/include/net.h @@ -449,6 +449,7 @@ extern int NetEthHdrSize(void);
/* Set ethernet header; returns the size of the header */ extern int NetSetEther(uchar *, uchar *, uint); +extern int net_update_ether(struct ethernet_hdr *et, uchar *addr, uint prot);
/* Set IP header */ extern void net_set_ip_header(uchar *pkt, IPaddr_t dest, IPaddr_t source); diff --git a/net/arp.c b/net/arp.c index fcff536..8cc175a 100644 --- a/net/arp.c +++ b/net/arp.c @@ -158,7 +158,7 @@ void ArpReceive(struct ethernet_hdr *et, struct ip_udp_hdr *ip, int len) /* reply with our IP address */ debug("Got ARP REQUEST, return our IP\n"); pkt = (uchar *)et; - eth_hdr_size = NetSetEther(pkt, et->et_src, PROT_ARP); + eth_hdr_size = net_update_ether(et, et->et_src, PROT_ARP); pkt += eth_hdr_size; arp->ar_op = htons(ARPOP_REPLY); memcpy(&arp->ar_tha, &arp->ar_sha, ARP_HLEN); diff --git a/net/net.c b/net/net.c index ccac70f..a916a59 100644 --- a/net/net.c +++ b/net/net.c @@ -1276,6 +1276,29 @@ NetSetEther(uchar *xet, uchar * addr, uint prot) } }
+int net_update_ether(struct ethernet_hdr *et, uchar *addr, uint prot) +{ + ushort protlen; + + memcpy(et->et_dest, addr, 6); + memcpy(et->et_src, NetOurEther, 6); + protlen = ntohs(et->et_protlen); + if (protlen == PROT_VLAN) { + struct vlan_ethernet_hdr *vet = + (struct vlan_ethernet_hdr *)et; + vet->vet_type = htons(prot); + return VLAN_ETHER_HDR_SIZE; + } else if (protlen > 1514) { + et->et_protlen = htons(prot); + return ETHER_HDR_SIZE; + } else { + /* 802.2 + SNAP */ + struct e802_hdr *et802 = (struct e802_hdr *)et; + et802->et_prot = htons(prot); + return E802_HDR_SIZE; + } +} + void net_set_ip_header(uchar *pkt, IPaddr_t dest, IPaddr_t source) { struct ip_udp_hdr *ip = (struct ip_udp_hdr *)pkt; diff --git a/net/ping.c b/net/ping.c index 9c690d8..71246de 100644 --- a/net/ping.c +++ b/net/ping.c @@ -87,6 +87,7 @@ void ping_receive(struct ethernet_hdr *et, struct ip_udp_hdr *ip, int len) { struct icmp_hdr *icmph = (struct icmp_hdr *)&ip->udp_src; IPaddr_t src_ip; + int eth_hdr_size;
switch (icmph->type) { case ICMP_ECHO_REPLY: @@ -95,11 +96,10 @@ void ping_receive(struct ethernet_hdr *et, struct ip_udp_hdr *ip, int len) net_set_state(NETLOOP_SUCCESS); return; case ICMP_ECHO_REQUEST: - debug("Got ICMP ECHO REQUEST, return " - "%d bytes\n", ETHER_HDR_SIZE + len); + eth_hdr_size = net_update_ether(et, et->et_src, PROT_IP);
- memcpy(&et->et_dest[0], &et->et_src[0], 6); - memcpy(&et->et_src[0], NetOurEther, 6); + debug("Got ICMP ECHO REQUEST, return " + "%d bytes\n", eth_hdr_size + len);
ip->ip_sum = 0; ip->ip_off = 0; @@ -112,7 +112,7 @@ void ping_receive(struct ethernet_hdr *et, struct ip_udp_hdr *ip, int len) icmph->checksum = 0; icmph->checksum = ~NetCksum((uchar *)icmph, (len - IP_HDR_SIZE) >> 1); - NetSendPacket((uchar *)et, ETHER_HDR_SIZE + len); + NetSendPacket((uchar *)et, eth_hdr_size + len); return; /* default: return;*/

Before this patch, bootp would not overwrite the value, but the value was still clobbered in the env
Signed-off-by: Joe Hershberger joe.hershberger@ni.com Cc: Joe Hershberger joe.hershberger@gmail.com --- Changes for v2: - Added a comment Changes for v3: - Rebased onto other changed patches - Fixed comment style
common/cmd_net.c | 8 ++++++-- 1 files changed, 6 insertions(+), 2 deletions(-)
diff --git a/common/cmd_net.c b/common/cmd_net.c index 65f32bc..2f66056 100644 --- a/common/cmd_net.c +++ b/common/cmd_net.c @@ -153,12 +153,16 @@ static void netboot_update_env (void) ip_to_string (NetOurIP, tmp); setenv ("ipaddr", tmp); } - +#if !defined(CONFIG_BOOTP_SERVERIP) + /* + * Only attempt to change serverip if net/bootp.c:BootpCopyNetParams() + * could have set it + */ if (NetServerIP) { ip_to_string (NetServerIP, tmp); setenv ("serverip", tmp); } - +#endif if (NetOurDNSIP) { ip_to_string (NetOurDNSIP, tmp); setenv ("dnsip", tmp);

If CONFIG_BOOTP_SERVERIP is not defined, unused variable warning is reported. This was fixed upstream using a compiler feature instead of a simple reorder of the statements.
Signed-off-by: Joe Hershberger joe.hershberger@ni.com Cc: Joe Hershberger joe.hershberger@gmail.com --- Changes for v2: Changes for v3: - Rebased onto other changed patches - Fixed commit message
net/bootp.c | 7 +++---- 1 files changed, 3 insertions(+), 4 deletions(-)
diff --git a/net/bootp.c b/net/bootp.c index b36abac..7e24b66 100644 --- a/net/bootp.c +++ b/net/bootp.c @@ -18,7 +18,6 @@ #ifdef CONFIG_STATUS_LED #include <status_led.h> #endif -#include <linux/compiler.h>
#define BOOTP_VENDOR_MAGIC 0x63825363 /* RFC1048 Magic Cookie */
@@ -96,15 +95,15 @@ static int BootpCheckPkt(uchar *pkt, unsigned dest, unsigned src, unsigned len) */ static void BootpCopyNetParams(struct Bootp_t *bp) { - __maybe_unused IPaddr_t tmp_ip; - - NetCopyIP(&NetOurIP, &bp->bp_yiaddr); #if !defined(CONFIG_BOOTP_SERVERIP) + IPaddr_t tmp_ip; + NetCopyIP(&tmp_ip, &bp->bp_siaddr); if (tmp_ip != 0) NetCopyIP(&NetServerIP, &bp->bp_siaddr); memcpy(NetServerEther, ((struct ethernet_hdr *)NetRxPacket)->et_src, 6); #endif + NetCopyIP(&NetOurIP, &bp->bp_yiaddr); if (strlen(bp->bp_file) > 0) copy_filename(BootFile, bp->bp_file, sizeof(BootFile));

This is useful if you want to look for a DHCP server, but try some other settings if not available.
Signed-off-by: Joe Hershberger joe.hershberger@ni.com Cc: Joe Hershberger joe.hershberger@gmail.com --- Changes for v2: - Renamed to CONFIG_BOOTP_MAY_FAIL Changes for v3: - Rebased onto other changed patches
README | 7 +++++++ net/bootp.c | 5 +++++ 2 files changed, 12 insertions(+), 0 deletions(-)
diff --git a/README b/README index 6919392..6e313ad 100644 --- a/README +++ b/README @@ -1592,10 +1592,17 @@ The following options need to be configured: CONFIG_BOOTP_NTPSERVER CONFIG_BOOTP_TIMEOFFSET CONFIG_BOOTP_VENDOREX + CONFIG_BOOTP_MAY_FAIL
CONFIG_BOOTP_SERVERIP - TFTP server will be the serverip environment variable, not the BOOTP server.
+ CONFIG_BOOTP_MAY_FAIL - If the DHCP server is not found + after the configured retry count, the call will fail + instead of starting over. This can be used to fail over + to Link-local IP address configuration if the DHCP server + is not available. + CONFIG_BOOTP_DNS2 - If a DHCP client requests the DNS serverip from a DHCP server, it is possible that more than one DNS serverip is offered to the client. diff --git a/net/bootp.c b/net/bootp.c index 7e24b66..0f0867b 100644 --- a/net/bootp.c +++ b/net/bootp.c @@ -326,8 +326,13 @@ static void BootpTimeout(void) { if (BootpTry >= TIMEOUT_COUNT) { +#ifdef CONFIG_BOOTP_MAY_FAIL + puts("\nRetry count exceeded\n"); + NetSetState(NETLOOP_FAIL); +#else puts("\nRetry count exceeded; starting again\n"); NetStartAgain(); +#endif } else { NetSetTimeout(TIMEOUT, BootpTimeout); BootpRequest();

Don't force ARP clients to return the MAC address if they don't care (such as ping)
Signed-off-by: Joe Hershberger joe.hershberger@ni.com Cc: Joe Hershberger joe.hershberger@gmail.com --- Changes for v2: Changes for v3: - Rebased onto other changed patches
net/arp.c | 9 +++++---- net/ping.c | 10 +++------- 2 files changed, 8 insertions(+), 11 deletions(-)
diff --git a/net/arp.c b/net/arp.c index 8cc175a..b2993ec 100644 --- a/net/arp.c +++ b/net/arp.c @@ -170,7 +170,7 @@ void ArpReceive(struct ethernet_hdr *et, struct ip_udp_hdr *ip, int len)
case ARPOP_REPLY: /* arp reply */ /* are we waiting for a reply */ - if (!NetArpWaitPacketIP || !NetArpWaitPacketMAC) + if (!NetArpWaitPacketIP) break;
#ifdef CONFIG_KEEP_SERVERADDR @@ -189,15 +189,16 @@ void ArpReceive(struct ethernet_hdr *et, struct ip_udp_hdr *ip, int len) arp->ar_data);
/* save address for later use */ - memcpy(NetArpWaitPacketMAC, - &arp->ar_sha, ARP_HLEN); + if (NetArpWaitPacketMAC != NULL) + memcpy(NetArpWaitPacketMAC, + &arp->ar_sha, ARP_HLEN);
net_get_arp_handler()((uchar *)arp, 0, reply_ip_addr, 0, len);
/* modify header, and transmit it */ memcpy(((struct ethernet_hdr *)NetArpWaitTxPacket)-> - et_dest, NetArpWaitPacketMAC, ARP_HLEN); + et_dest, &arp->ar_sha, ARP_HLEN); NetSendPacket(NetArpWaitTxPacket, NetArpWaitTxPacketSize);
diff --git a/net/ping.c b/net/ping.c index 71246de..068aa96 100644 --- a/net/ping.c +++ b/net/ping.c @@ -40,22 +40,18 @@ static void set_icmp_header(uchar *pkt, IPaddr_t dest)
static int ping_send(void) { - static uchar mac[6]; uchar *pkt; int eth_hdr_size;
/* XXX always send arp request */
- memcpy(mac, NetEtherNullAddr, 6); - debug("sending ARP for %pI4\n", &NetPingIP);
NetArpWaitPacketIP = NetPingIP; - NetArpWaitPacketMAC = mac;
- pkt = NetArpWaitTxPacket; - eth_hdr_size = NetSetEther(pkt, mac, PROT_IP); - pkt += eth_hdr_size; + eth_hdr_size = NetSetEther(NetArpWaitTxPacket, NetEtherNullAddr, + PROT_IP); + pkt = NetArpWaitTxPacket + eth_hdr_size;
set_icmp_header(pkt, NetPingIP);

Noone uses it.
Signed-off-by: Joe Hershberger joe.hershberger@ni.com Cc: Joe Hershberger joe.hershberger@gmail.com --- Changes for v2: - Split from "Fix net buffer initialization" Changes for v3: - Rebased onto other changed patches - Added a short commit message
net/net.c | 4 ++-- 1 files changed, 2 insertions(+), 2 deletions(-)
diff --git a/net/net.c b/net/net.c index a916a59..530b172 100644 --- a/net/net.c +++ b/net/net.c @@ -231,7 +231,7 @@ void net_auto_load(void) TftpStart(TFTPGET); }
-static void NetInitLoop(enum proto_t protocol) +static void NetInitLoop(void) { static int env_changed_id; int env_id = get_env_id(); @@ -311,7 +311,7 @@ restart: * here on, this code is a state machine driven by received * packets and timer events. */ - NetInitLoop(protocol); + NetInitLoop();
switch (net_check_prereq(protocol)) { case 1:

A new non-static function net_init() will initialize buffers and read from the environment. Only update from the env on each entry to NetLoop().
Signed-off-by: Joe Hershberger joe.hershberger@ni.com Cc: Joe Hershberger joe.hershberger@gmail.com --- Changes for v2: - Unadded explicit parameter name in net.h for NetLoop - Eliminate CamelCase from new functions - Changed null pointer print to an assert Changes for v3: - Rebased onto other changed patches - Added a blank line - Added space around operators
include/net.h | 1 + net/net.c | 45 +++++++++++++++++++++++++++++++-------------- 2 files changed, 32 insertions(+), 14 deletions(-)
diff --git a/include/net.h b/include/net.h index 9564051..ec41366 100644 --- a/include/net.h +++ b/include/net.h @@ -436,6 +436,7 @@ extern IPaddr_t Mcast_addr; #endif
/* Initialize the network adapter */ +extern void net_init(void); extern int NetLoop(enum proto_t);
/* Shutdown adapters and cleanup */ diff --git a/net/net.c b/net/net.c index 530b172..c071986 100644 --- a/net/net.c +++ b/net/net.c @@ -265,6 +265,31 @@ static void net_cleanup_loop(void) net_clear_handlers(); }
+void net_init(void) +{ + static int first_call = 1; + + if (first_call) { + /* + * Setup packet buffers, aligned correctly. + */ + int i; + + NetTxPacket = &PktBuf[0] + (PKTALIGN - 1); + NetTxPacket -= (ulong)NetTxPacket % PKTALIGN; + for (i = 0; i < PKTBUFSRX; i++) + NetRxPackets[i] = NetTxPacket + (i + 1) * PKTSIZE_ALIGN; + + ArpInit(); + net_clear_handlers(); + + /* Only need to setup buffer pointers once. */ + first_call = 0; + } + + NetInitLoop(); +} + /**********************************************************************/ /* * Main network processing loop. @@ -272,28 +297,15 @@ static void net_cleanup_loop(void)
int NetLoop(enum proto_t protocol) { - int i; bd_t *bd = gd->bd; int ret = -1;
NetRestarted = 0; NetDevExists = 0; - - NetTxPacket = NULL; NetTryCount = 1;
- ArpInit(); - net_clear_handlers(); - - /* - * Setup packet buffers, aligned correctly. - */ - NetTxPacket = &PktBuf[0] + (PKTALIGN - 1); - NetTxPacket -= (ulong)NetTxPacket % PKTALIGN; - for (i = 0; i < PKTBUFSRX; i++) - NetRxPackets[i] = NetTxPacket + (i+1)*PKTSIZE_ALIGN; - bootstage_mark_name(BOOTSTAGE_ID_ETH_START, "eth_start"); + net_init(); eth_halt(); eth_set_current(); if (eth_init(bd) < 0) { @@ -624,6 +636,11 @@ int NetSendUDPPacket(uchar *ether, IPaddr_t dest, int dport, int sport, int eth_hdr_size; int pkt_hdr_size;
+ /* make sure the NetTxPacket is initialized (NetInit() was called) */ + assert(NetTxPacket != NULL); + if (NetTxPacket == NULL) + return -1; + /* convert to new style broadcast */ if (dest == 0) dest = 0xFFFFFFFF;

Fix NetConsole bug that causes first packet header to claim a source IP address of 0.0.0.0
Signed-off-by: Joe Hershberger joe.hershberger@ni.com Cc: Joe Hershberger joe.hershberger@gmail.com --- Changes for v2: Changes for v3: - Rebased onto other changed patches - Fix comment style
drivers/net/netconsole.c | 6 ++++++ 1 files changed, 6 insertions(+), 0 deletions(-)
diff --git a/drivers/net/netconsole.c b/drivers/net/netconsole.c index 744f4d1..19e563e 100644 --- a/drivers/net/netconsole.c +++ b/drivers/net/netconsole.c @@ -169,6 +169,12 @@ static int nc_start(void) else memset(nc_ether, 0, sizeof(nc_ether)); /* force arp request */
+ /* + * Initialize the static IP settings and buffer pointers + * incase we call NetSendUDPPacket before NetLoop + */ + net_init(); + return 0; }

Use the NetArpTxPacket for the ARP packet, not to hold what used to be in NetTxPacket. This saves a copy and makes the code easier to understand.
Signed-off-by: Joe Hershberger joe.hershberger@ni.com Cc: Joe Hershberger joe.hershberger@gmail.com --- Changes for v2: Changes for v3: - Rebased onto other changed patches - Fixed comment style
include/net.h | 6 +++--- net/arp.c | 24 ++++++++++++------------ net/arp.h | 2 -- net/net.c | 24 ++++++------------------ net/ping.c | 5 ++--- 5 files changed, 23 insertions(+), 38 deletions(-)
diff --git a/include/net.h b/include/net.h index ec41366..52b9a29 100644 --- a/include/net.h +++ b/include/net.h @@ -483,14 +483,14 @@ static inline void net_set_state(enum net_loop_state state) net_state = state; }
-/* Transmit "NetTxPacket" */ +/* Transmit a packet */ static inline void NetSendPacket(uchar *pkt, int len) { (void) eth_send(pkt, len); }
/* - * Transmit UDP packet, performing ARP request if needed + * Transmit "NetTxPacket" as UDP packet, performing ARP request if needed * (ether will be populated) * * @param ether Raw packet buffer @@ -499,7 +499,7 @@ static inline void NetSendPacket(uchar *pkt, int len) * @param sport Source UDP port * @param payload_len Length of data after the UDP header */ -extern int NetSendUDPPacket(uchar *ether, IPaddr_t dest, int dport, +extern int NetSendUDPPacket(uchar *ether, IPaddr_t dest, int dport, int sport, int payload_len);
/* Processes a received packet */ diff --git a/net/arp.c b/net/arp.c index b2993ec..4a73a0f 100644 --- a/net/arp.c +++ b/net/arp.c @@ -30,22 +30,22 @@ IPaddr_t NetArpWaitPacketIP; IPaddr_t NetArpWaitReplyIP; /* MAC address of waiting packet's destination */ uchar *NetArpWaitPacketMAC; -/* THE transmit packet */ -uchar *NetArpWaitTxPacket; int NetArpWaitTxPacketSize; -uchar NetArpWaitPacketBuf[PKTSIZE_ALIGN + PKTALIGN]; ulong NetArpWaitTimerStart; int NetArpWaitTry;
+uchar *NetArpTxPacket; /* THE ARP transmit packet */ +uchar NetArpPacketBuf[PKTSIZE_ALIGN + PKTALIGN]; + void ArpInit(void) { /* XXX problem with bss workaround */ NetArpWaitPacketMAC = NULL; NetArpWaitPacketIP = 0; NetArpWaitReplyIP = 0; - NetArpWaitTxPacket = &NetArpWaitPacketBuf[0] + (PKTALIGN - 1); - NetArpWaitTxPacket -= (ulong)NetArpWaitTxPacket % PKTALIGN; NetArpWaitTxPacketSize = 0; + NetArpTxPacket = &NetArpPacketBuf[0] + (PKTALIGN - 1); + NetArpTxPacket -= (ulong)NetArpTxPacket % PKTALIGN; }
void ArpRequest(void) @@ -56,7 +56,7 @@ void ArpRequest(void)
debug("ARP broadcast %d\n", NetArpWaitTry);
- pkt = NetTxPacket; + pkt = NetArpTxPacket;
eth_hdr_size = NetSetEther(pkt, NetBcastAddr, PROT_ARP); pkt += eth_hdr_size; @@ -88,7 +88,7 @@ void ArpRequest(void) }
NetWriteIP(&arp->ar_tpa, NetArpWaitReplyIP); - NetSendPacket(NetTxPacket, eth_hdr_size + ARP_HDR_SIZE); + NetSendPacket(NetArpTxPacket, eth_hdr_size + ARP_HDR_SIZE); }
void ArpTimeoutCheck(void) @@ -196,11 +196,11 @@ void ArpReceive(struct ethernet_hdr *et, struct ip_udp_hdr *ip, int len) net_get_arp_handler()((uchar *)arp, 0, reply_ip_addr, 0, len);
- /* modify header, and transmit it */ - memcpy(((struct ethernet_hdr *)NetArpWaitTxPacket)-> - et_dest, &arp->ar_sha, ARP_HLEN); - NetSendPacket(NetArpWaitTxPacket, - NetArpWaitTxPacketSize); + /* set the mac address in the waiting packet's header + and transmit it */ + memcpy(((struct ethernet_hdr *)NetTxPacket)->et_dest, + &arp->ar_sha, ARP_HLEN); + NetSendPacket(NetTxPacket, NetArpWaitTxPacketSize);
/* no arp request pending now */ NetArpWaitPacketIP = 0; diff --git a/net/arp.h b/net/arp.h index b7a1ac9..956fc5e 100644 --- a/net/arp.h +++ b/net/arp.h @@ -16,8 +16,6 @@ extern IPaddr_t NetArpWaitPacketIP; /* MAC address of waiting packet's destination */ extern uchar *NetArpWaitPacketMAC; -/* THE transmit packet */ -extern uchar *NetArpWaitTxPacket; extern int NetArpWaitTxPacketSize; extern ulong NetArpWaitTimerStart; extern int NetArpWaitTry; diff --git a/net/net.c b/net/net.c index c071986..b381974 100644 --- a/net/net.c +++ b/net/net.c @@ -442,6 +442,9 @@ restart: * Abort if ctrl-c was pressed. */ if (ctrlc()) { + /* cancel any ARP that may not have completed */ + NetArpWaitPacketIP = 0; + net_cleanup_loop(); eth_halt(); puts("\nAbort\n"); @@ -632,7 +635,6 @@ int NetSendUDPPacket(uchar *ether, IPaddr_t dest, int dport, int sport, int payload_len) { uchar *pkt; - int need_arp = 0; int eth_hdr_size; int pkt_hdr_size;
@@ -649,35 +651,21 @@ int NetSendUDPPacket(uchar *ether, IPaddr_t dest, int dport, int sport, if (dest == 0xFFFFFFFF) ether = NetBcastAddr;
- /* - * if MAC address was not discovered yet, save the packet and do - * an ARP request - */ - if (memcmp(ether, NetEtherNullAddr, 6) == 0) { - need_arp = 1; - pkt = NetArpWaitTxPacket; - } else - pkt = (uchar *)NetTxPacket; + pkt = (uchar *)NetTxPacket;
eth_hdr_size = NetSetEther(pkt, ether, PROT_IP); pkt += eth_hdr_size; net_set_udp_header(pkt, dest, dport, sport, payload_len); pkt_hdr_size = eth_hdr_size + IP_UDP_HDR_SIZE;
- if (need_arp) { + /* if MAC address was not discovered yet, do an ARP request */ + if (memcmp(ether, NetEtherNullAddr, 6) == 0) { debug("sending ARP for %pI4\n", &dest);
/* save the ip and eth addr for the packet to send after arp */ NetArpWaitPacketIP = dest; NetArpWaitPacketMAC = ether;
- /* - * Copy the packet data from the NetTxPacket into the - * NetArpWaitTxPacket to send after arp - */ - memcpy(pkt + IP_UDP_HDR_SIZE, (uchar *)NetTxPacket + - pkt_hdr_size, payload_len); - /* size of the waiting packet */ NetArpWaitTxPacketSize = pkt_hdr_size + payload_len;
diff --git a/net/ping.c b/net/ping.c index 068aa96..2ba9f76 100644 --- a/net/ping.c +++ b/net/ping.c @@ -49,9 +49,8 @@ static int ping_send(void)
NetArpWaitPacketIP = NetPingIP;
- eth_hdr_size = NetSetEther(NetArpWaitTxPacket, NetEtherNullAddr, - PROT_IP); - pkt = NetArpWaitTxPacket + eth_hdr_size; + eth_hdr_size = NetSetEther(NetTxPacket, NetEtherNullAddr, PROT_IP); + pkt = (uchar *)NetTxPacket + eth_hdr_size;
set_icmp_header(pkt, NetPingIP);

Signed-off-by: Joe Hershberger joe.hershberger@ni.com Cc: Joe Hershberger joe.hershberger@gmail.com Cc: Simon Glass sjg@chromium.org Cc: Mike Frysinger vapier@gentoo.org --- Changes for v2: - Split from "Improve variable names and code readability"
net/arp.c | 6 +++--- 1 files changed, 3 insertions(+), 3 deletions(-)
diff --git a/net/arp.c b/net/arp.c index 3d5dc83..f994e7d 100644 --- a/net/arp.c +++ b/net/arp.c @@ -116,7 +116,7 @@ void ArpTimeoutCheck(void) void ArpReceive(struct Ethernet_hdr *et, struct IP_UDP_hdr *ip, int len) { struct ARP_hdr *arp; - IPaddr_t tmp; + IPaddr_t reply_ip_addr; uchar *pkt;
/* @@ -178,10 +178,10 @@ void ArpReceive(struct Ethernet_hdr *et, struct IP_UDP_hdr *ip, int len) } #endif
- tmp = NetReadIP(&arp->ar_data[6]); + reply_ip_addr = NetReadIP(&arp->ar_data[6]);
/* matched waiting packet's address */ - if (tmp == NetArpWaitReplyIP) { + if (reply_ip_addr == NetArpWaitReplyIP) { debug("Got ARP REPLY, set eth addr (%pM)\n", arp->ar_data);

On Wed, Mar 28, 2012 at 12:42 PM, Joe Hershberger joe.hershberger@ni.comwrote:
Signed-off-by: Joe Hershberger joe.hershberger@ni.com Cc: Joe Hershberger joe.hershberger@gmail.com Cc: Simon Glass sjg@chromium.org Cc: Mike Frysinger vapier@gentoo.org
Acked-by: Simon Glass sjg@chromium.org
Changes for v2:
- Split from "Improve variable names and code readability"
net/arp.c | 6 +++--- 1 files changed, 3 insertions(+), 3 deletions(-)
diff --git a/net/arp.c b/net/arp.c index 3d5dc83..f994e7d 100644 --- a/net/arp.c +++ b/net/arp.c @@ -116,7 +116,7 @@ void ArpTimeoutCheck(void) void ArpReceive(struct Ethernet_hdr *et, struct IP_UDP_hdr *ip, int len) { struct ARP_hdr *arp;
IPaddr_t tmp;
IPaddr_t reply_ip_addr; uchar *pkt; /*
@@ -178,10 +178,10 @@ void ArpReceive(struct Ethernet_hdr *et, struct IP_UDP_hdr *ip, int len) } #endif
tmp = NetReadIP(&arp->ar_data[6]);
reply_ip_addr = NetReadIP(&arp->ar_data[6]); /* matched waiting packet's address */
if (tmp == NetArpWaitReplyIP) {
if (reply_ip_addr == NetArpWaitReplyIP) { debug("Got ARP REPLY, set eth addr (%pM)\n", arp->ar_data);
-- 1.6.0.2

ICMP (ping) was reimplementing IP header code... it now shares code.
Signed-off-by: Joe Hershberger joe.hershberger@ni.com Cc: Joe Hershberger joe.hershberger@gmail.com Cc: Simon Glass sjg@chromium.org Cc: Mike Frysinger vapier@gentoo.org --- Changes for v2: - Eliminate CamelCase in new functions
include/net.h | 4 +++- net/bootp.c | 8 ++++---- net/net.c | 47 ++++++++++++++++++++++++++++------------------- net/ping.c | 52 +++++++++++++++++++++++----------------------------- 4 files changed, 58 insertions(+), 53 deletions(-)
diff --git a/include/net.h b/include/net.h index bb41f5e..6eb25ee 100644 --- a/include/net.h +++ b/include/net.h @@ -451,7 +451,9 @@ extern int NetEthHdrSize(void); extern int NetSetEther(uchar *, uchar *, uint);
/* Set IP header */ -extern void NetSetIP(uchar *, IPaddr_t, int, int, int); +extern void net_set_ip_header(uchar *pkt, IPaddr_t dest, IPaddr_t source); +extern void net_set_udp_header(uchar *pkt, IPaddr_t dest, int dport, + int sport, int len);
/* Checksum */ extern int NetCksumOk(uchar *, int); /* Return true if cksum OK */ diff --git a/net/bootp.c b/net/bootp.c index 62d3c71..8914ada 100644 --- a/net/bootp.c +++ b/net/bootp.c @@ -621,9 +621,9 @@ BootpRequest(void) * determined. * C. Hallinan, DS4.COM, Inc. */ - /* NetSetIP(pkt, 0xFFFFFFFFL, PORT_BOOTPS, PORT_BOOTPC, + /* net_set_udp_header(pkt, 0xFFFFFFFFL, PORT_BOOTPS, PORT_BOOTPC, sizeof (struct Bootp_t)); */ - iphdr = pkt; /* We need this later for NetSetUDPHeader() */ + iphdr = pkt; /* We need this later for net_set_udp_header() */ pkt += IP_UDP_HDR_SIZE;
bp = (struct Bootp_t *)pkt; @@ -665,7 +665,7 @@ BootpRequest(void) pktlen = ((int)(pkt-NetTxPacket)) + BOOTP_HDR_SIZE - sizeof(bp->bp_vend) + ext_len; iplen = BOOTP_HDR_SIZE - sizeof(bp->bp_vend) + ext_len; - NetSetIP(iphdr, 0xFFFFFFFFL, PORT_BOOTPS, PORT_BOOTPC, iplen); + net_set_udp_header(iphdr, 0xFFFFFFFFL, PORT_BOOTPS, PORT_BOOTPC, iplen); NetSetTimeout(SELECT_TIMEOUT, BootpTimeout);
#if defined(CONFIG_CMD_DHCP) @@ -846,7 +846,7 @@ static void DhcpSendRequestPkt(struct Bootp_t *bp_offer) pktlen = ((int)(pkt-NetTxPacket)) + BOOTP_HDR_SIZE - sizeof(bp->bp_vend) + extlen; iplen = BOOTP_HDR_SIZE - sizeof(bp->bp_vend) + extlen; - NetSetIP(iphdr, 0xFFFFFFFFL, PORT_BOOTPS, PORT_BOOTPC, iplen); + net_set_udp_header(iphdr, 0xFFFFFFFFL, PORT_BOOTPS, PORT_BOOTPC, iplen);
debug("Transmitting DHCPREQUEST packet: len = %d\n", pktlen); #ifdef CONFIG_BOOTP_DHCP_REQUEST_DELAY diff --git a/net/net.c b/net/net.c index 0371e2e..a36f5e7 100644 --- a/net/net.c +++ b/net/net.c @@ -621,7 +621,7 @@ NetSendUDPPacket(uchar *ether, IPaddr_t dest, int dport, int sport, pkt = NetArpWaitTxPacket; pkt += NetSetEther(pkt, NetArpWaitPacketMAC, PROT_IP);
- NetSetIP(pkt, dest, dport, sport, payload_len); + net_set_udp_header(pkt, dest, dport, sport, payload_len); memcpy(pkt + IP_UDP_HDR_SIZE, (uchar *)NetTxPacket + (pkt - (uchar *)NetArpWaitTxPacket) + IP_UDP_HDR_SIZE, payload_len); @@ -641,7 +641,7 @@ NetSendUDPPacket(uchar *ether, IPaddr_t dest, int dport, int sport,
pkt = (uchar *)NetTxPacket; pkt += NetSetEther(pkt, ether, PROT_IP); - NetSetIP(pkt, dest, dport, sport, payload_len); + net_set_udp_header(pkt, dest, dport, sport, payload_len); eth_send(NetTxPacket, (pkt - NetTxPacket) + IP_UDP_HDR_SIZE + payload_len);
@@ -1248,40 +1248,49 @@ NetSetEther(uchar *xet, uchar * addr, uint prot) } }
-void NetSetIP(uchar *xip, IPaddr_t dest, int dport, int sport, int len) +void net_set_ip_header(uchar *pkt, IPaddr_t dest, IPaddr_t source) { - struct IP_UDP_hdr *ip = (struct IP_UDP_hdr *)xip; + struct IP_UDP_hdr *ip = (struct IP_UDP_hdr *)pkt;
/* - * If the data is an odd number of bytes, zero the - * byte after the last byte so that the checksum - * will work. - */ - if (len & 1) - xip[IP_UDP_HDR_SIZE + len] = 0; - - /* - * Construct an IP and UDP header. - * (need to set no fragment bit - XXX) + * Construct an IP header. */ /* IP_HDR_SIZE / 4 (not including UDP) */ ip->ip_hl_v = 0x45; ip->ip_tos = 0; - ip->ip_len = htons(IP_UDP_HDR_SIZE + len); + ip->ip_len = htons(IP_HDR_SIZE); ip->ip_id = htons(NetIPID++); ip->ip_off = htons(IP_FLAGS_DFRAG); /* Don't fragment */ ip->ip_ttl = 255; - ip->ip_p = 17; /* UDP */ ip->ip_sum = 0; /* already in network byte order */ - NetCopyIP((void *)&ip->ip_src, &NetOurIP); - /* - "" - */ + NetCopyIP((void *)&ip->ip_src, &source); + /* already in network byte order */ NetCopyIP((void *)&ip->ip_dst, &dest); +} + +void net_set_udp_header(uchar *pkt, IPaddr_t dest, int dport, int sport, + int len) +{ + struct IP_UDP_hdr *ip = (struct IP_UDP_hdr *)pkt; + + /* + * If the data is an odd number of bytes, zero the + * byte after the last byte so that the checksum + * will work. + */ + if (len & 1) + pkt[IP_UDP_HDR_SIZE + len] = 0; + + net_set_ip_header(pkt, dest, NetOurIP); + ip->ip_len = htons(IP_UDP_HDR_SIZE + len); + ip->ip_p = IPPROTO_UDP; + ip->ip_sum = ~NetCksum((uchar *)ip, IP_HDR_SIZE >> 1); + ip->udp_src = htons(sport); ip->udp_dst = htons(dport); ip->udp_len = htons(UDP_HDR_SIZE + len); ip->udp_xsum = 0; - ip->ip_sum = ~NetCksum((uchar *)ip, IP_HDR_SIZE / 2); }
void copy_filename(char *dst, const char *src, int size) diff --git a/net/ping.c b/net/ping.c index 27fca6a..0ef42a7 100644 --- a/net/ping.c +++ b/net/ping.c @@ -16,11 +16,31 @@ static ushort PingSeqNo; /* The ip address to ping */ IPaddr_t NetPingIP;
+static void set_icmp_header(uchar *pkt, IPaddr_t dest) +{ + /* + * Construct an IP and ICMP header. + */ + struct IP_hdr *ip = (struct IP_hdr *)pkt; + struct ICMP_hdr *icmp = (struct ICMP_hdr *)(pkt + IP_HDR_SIZE); + + net_set_ip_header(pkt, dest, NetOurIP); + + ip->ip_len = htons(IP_ICMP_HDR_SIZE); + ip->ip_p = IPPROTO_ICMP; + ip->ip_sum = ~NetCksum((uchar *)ip, IP_HDR_SIZE >> 1); + + icmp->type = ICMP_ECHO_REQUEST; + icmp->code = 0; + icmp->checksum = 0; + icmp->un.echo.id = 0; + icmp->un.echo.sequence = htons(PingSeqNo++); + icmp->checksum = ~NetCksum((uchar *)icmp, ICMP_HDR_SIZE >> 1); +} + static int ping_send(void) { static uchar mac[6]; - struct IP_UDP_hdr *ip; - ushort *s; uchar *pkt;
/* XXX always send arp request */ @@ -35,33 +55,7 @@ static int ping_send(void) pkt = NetArpWaitTxPacket; pkt += NetSetEther(pkt, mac, PROT_IP);
- ip = (struct IP_UDP_hdr *)pkt; - - /* - * Construct an IP and ICMP header. - * (need to set no fragment bit - XXX) - */ - /* IP_HDR_SIZE / 4 (not including UDP) */ - ip->ip_hl_v = 0x45; - ip->ip_tos = 0; - ip->ip_len = htons(IP_HDR_SIZE + 8); - ip->ip_id = htons(NetIPID++); - ip->ip_off = htons(IP_FLAGS_DFRAG); /* Don't fragment */ - ip->ip_ttl = 255; - ip->ip_p = 0x01; /* ICMP */ - ip->ip_sum = 0; - /* already in network byte order */ - NetCopyIP((void *)&ip->ip_src, &NetOurIP); - /* - "" - */ - NetCopyIP((void *)&ip->ip_dst, &NetPingIP); - ip->ip_sum = ~NetCksum((uchar *)ip, IP_HDR_SIZE / 2); - - s = &ip->udp_src; /* XXX ICMP starts here */ - s[0] = htons(0x0800); /* echo-request, code */ - s[1] = 0; /* checksum */ - s[2] = 0; /* identifier */ - s[3] = htons(PingSeqNo++); /* sequence number */ - s[1] = ~NetCksum((uchar *)s, 8/2); + set_icmp_header(pkt, NetPingIP);
/* size of the waiting packet */ NetArpWaitTxPacketSize =

On Tue, Mar 27, 2012 at 4:42 PM, Joe Hershberger joe.hershberger@ni.com wrote:
ICMP (ping) was reimplementing IP header code... it now shares code.
Signed-off-by: Joe Hershberger joe.hershberger@ni.com Cc: Joe Hershberger joe.hershberger@gmail.com Cc: Simon Glass sjg@chromium.org Cc: Mike Frysinger vapier@gentoo.org
Acked-by: Simon Glass sjg@chromium.org
Seems like the call to NetCksum() could be tidied up later.

Signed-off-by: Joe Hershberger joe.hershberger@ni.com Cc: Joe Hershberger joe.hershberger@gmail.com Cc: Simon Glass sjg@chromium.org Cc: Mike Frysinger vapier@gentoo.org --- Changes for v2: - Split from "Improve variable names and code readability"
include/net.h | 6 ++++++ net/arp.c | 34 +++++++++++++++++----------------- 2 files changed, 23 insertions(+), 17 deletions(-)
diff --git a/include/net.h b/include/net.h index 3314b4e..bb41f5e 100644 --- a/include/net.h +++ b/include/net.h @@ -258,7 +258,9 @@ struct ARP_hdr { # define ARP_ETHER 1 /* Ethernet hardware address */ ushort ar_pro; /* Format of protocol address */ uchar ar_hln; /* Length of hardware address */ +# define ARP_HLEN 6 uchar ar_pln; /* Length of protocol address */ +# define ARP_PLEN 4 ushort ar_op; /* Operation */ # define ARPOP_REQUEST 1 /* Request to resolve address */ # define ARPOP_REPLY 2 /* Response to previous request */ @@ -272,6 +274,10 @@ struct ARP_hdr { * specific hardware/protocol combinations. */ uchar ar_data[0]; +#define ar_sha ar_data[0] +#define ar_spa ar_data[ARP_HLEN] +#define ar_tha ar_data[ARP_HLEN + ARP_PLEN] +#define ar_tpa ar_data[ARP_HLEN + ARP_PLEN + ARP_HLEN] #if 0 uchar ar_sha[]; /* Sender hardware address */ uchar ar_spa[]; /* Sender protocol address */ diff --git a/net/arp.c b/net/arp.c index f994e7d..6e3d7ab 100644 --- a/net/arp.c +++ b/net/arp.c @@ -63,16 +63,16 @@ void ArpRequest(void)
arp->ar_hrd = htons(ARP_ETHER); arp->ar_pro = htons(PROT_IP); - arp->ar_hln = 6; - arp->ar_pln = 4; + arp->ar_hln = ARP_HLEN; + arp->ar_pln = ARP_PLEN; arp->ar_op = htons(ARPOP_REQUEST);
/* source ET addr */ - memcpy(&arp->ar_data[0], NetOurEther, 6); + memcpy(&arp->ar_sha, NetOurEther, ARP_HLEN); /* source IP addr */ - NetWriteIP((uchar *) &arp->ar_data[6], NetOurIP); + NetWriteIP(&arp->ar_spa, NetOurIP); /* dest ET addr = 0 */ - memset(&arp->ar_data[10], '\0', 6); + memset(&arp->ar_tha, 0, ARP_HLEN); if ((NetArpWaitPacketIP & NetOurSubnetMask) != (NetOurIP & NetOurSubnetMask)) { if (NetOurGatewayIP == 0) { @@ -85,7 +85,7 @@ void ArpRequest(void) NetArpWaitReplyIP = NetArpWaitPacketIP; }
- NetWriteIP((uchar *) &arp->ar_data[16], NetArpWaitReplyIP); + NetWriteIP(&arp->ar_tpa, NetArpWaitReplyIP); (void) eth_send(NetTxPacket, (pkt - NetTxPacket) + ARP_HDR_SIZE); }
@@ -139,15 +139,15 @@ void ArpReceive(struct Ethernet_hdr *et, struct IP_UDP_hdr *ip, int len) return; if (ntohs(arp->ar_pro) != PROT_IP) return; - if (arp->ar_hln != 6) + if (arp->ar_hln != ARP_HLEN) return; - if (arp->ar_pln != 4) + if (arp->ar_pln != ARP_PLEN) return;
if (NetOurIP == 0) return;
- if (NetReadIP(&arp->ar_data[16]) != NetOurIP) + if (NetReadIP(&arp->ar_tpa) != NetOurIP) return;
switch (ntohs(arp->ar_op)) { @@ -157,10 +157,10 @@ void ArpReceive(struct Ethernet_hdr *et, struct IP_UDP_hdr *ip, int len) pkt = (uchar *)et; pkt += NetSetEther(pkt, et->et_src, PROT_ARP); arp->ar_op = htons(ARPOP_REPLY); - memcpy(&arp->ar_data[10], &arp->ar_data[0], 6); - NetCopyIP(&arp->ar_data[16], &arp->ar_data[6]); - memcpy(&arp->ar_data[0], NetOurEther, 6); - NetCopyIP(&arp->ar_data[6], &NetOurIP); + memcpy(&arp->ar_tha, &arp->ar_sha, ARP_HLEN); + NetCopyIP(&arp->ar_tpa, &arp->ar_spa); + memcpy(&arp->ar_sha, NetOurEther, ARP_HLEN); + NetCopyIP(&arp->ar_spa, &NetOurIP); (void) eth_send((uchar *)et, (pkt - (uchar *)et) + ARP_HDR_SIZE); return; @@ -173,12 +173,12 @@ void ArpReceive(struct Ethernet_hdr *et, struct IP_UDP_hdr *ip, int len) #ifdef CONFIG_KEEP_SERVERADDR if (NetServerIP == NetArpWaitPacketIP) { char buf[20]; - sprintf(buf, "%pM", arp->ar_data); + sprintf(buf, "%pM", arp->ar_sha); setenv("serveraddr", buf); } #endif
- reply_ip_addr = NetReadIP(&arp->ar_data[6]); + reply_ip_addr = NetReadIP(&arp->ar_spa);
/* matched waiting packet's address */ if (reply_ip_addr == NetArpWaitReplyIP) { @@ -187,14 +187,14 @@ void ArpReceive(struct Ethernet_hdr *et, struct IP_UDP_hdr *ip, int len)
/* save address for later use */ memcpy(NetArpWaitPacketMAC, - &arp->ar_data[0], 6); + &arp->ar_sha, ARP_HLEN);
#ifdef CONFIG_NETCONSOLE NetGetHandler()(0, 0, 0, 0, 0); #endif /* modify header, and transmit it */ memcpy(((struct Ethernet_hdr *)NetArpWaitTxPacket)-> - et_dest, NetArpWaitPacketMAC, 6); + et_dest, NetArpWaitPacketMAC, ARP_HLEN); (void) eth_send(NetArpWaitTxPacket, NetArpWaitTxPacketSize);

Hi Joe,
On Wed, Mar 28, 2012 at 12:42 PM, Joe Hershberger joe.hershberger@ni.comwrote:
Signed-off-by: Joe Hershberger joe.hershberger@ni.com Cc: Joe Hershberger joe.hershberger@gmail.com Cc: Simon Glass sjg@chromium.org Cc: Mike Frysinger vapier@gentoo.org
Changes for v2:
- Split from "Improve variable names and code readability"
include/net.h | 6 ++++++ net/arp.c | 34 +++++++++++++++++----------------- 2 files changed, 23 insertions(+), 17 deletions(-)
diff --git a/include/net.h b/include/net.h index 3314b4e..bb41f5e 100644 --- a/include/net.h +++ b/include/net.h @@ -258,7 +258,9 @@ struct ARP_hdr { # define ARP_ETHER 1 /* Ethernet hardware address */ ushort ar_pro; /* Format of protocol address */ uchar ar_hln; /* Length of hardware address */ +# define ARP_HLEN 6 uchar ar_pln; /* Length of protocol address */ +# define ARP_PLEN 4 ushort ar_op; /* Operation */ # define ARPOP_REQUEST 1 /* Request to resolve address */ # define ARPOP_REPLY 2 /* Response to previous request */ @@ -272,6 +274,10 @@ struct ARP_hdr { * specific hardware/protocol combinations. */ uchar ar_data[0]; +#define ar_sha ar_data[0] +#define ar_spa ar_data[ARP_HLEN] +#define ar_tha ar_data[ARP_HLEN + ARP_PLEN] +#define ar_tpa ar_data[ARP_HLEN + ARP_PLEN + ARP_HLEN] #if 0 uchar ar_sha[]; /* Sender hardware address */ uchar ar_spa[]; /* Sender protocol address */ diff --git a/net/arp.c b/net/arp.c index f994e7d..6e3d7ab 100644 --- a/net/arp.c +++ b/net/arp.c @@ -63,16 +63,16 @@ void ArpRequest(void)
arp->ar_hrd = htons(ARP_ETHER); arp->ar_pro = htons(PROT_IP);
arp->ar_hln = 6;
arp->ar_pln = 4;
arp->ar_hln = ARP_HLEN;
arp->ar_pln = ARP_PLEN; arp->ar_op = htons(ARPOP_REQUEST); /* source ET addr */
memcpy(&arp->ar_data[0], NetOurEther, 6);
memcpy(&arp->ar_sha, NetOurEther, ARP_HLEN); /* source IP addr */
NetWriteIP((uchar *) &arp->ar_data[6], NetOurIP);
NetWriteIP(&arp->ar_spa, NetOurIP); /* dest ET addr = 0 */
memset(&arp->ar_data[10], '\0', 6);
memset(&arp->ar_tha, 0, ARP_HLEN);
Did you mean to change this to 0? I prefer '\0\ but I might be the only one.
if ((NetArpWaitPacketIP & NetOurSubnetMask) != (NetOurIP & NetOurSubnetMask)) { if (NetOurGatewayIP == 0) {
@@ -85,7 +85,7 @@ void ArpRequest(void) NetArpWaitReplyIP = NetArpWaitPacketIP; }
NetWriteIP((uchar *) &arp->ar_data[16], NetArpWaitReplyIP);
NetWriteIP(&arp->ar_tpa, NetArpWaitReplyIP); (void) eth_send(NetTxPacket, (pkt - NetTxPacket) + ARP_HDR_SIZE);
}
@@ -139,15 +139,15 @@ void ArpReceive(struct Ethernet_hdr *et, struct IP_UDP_hdr *ip, int len) return; if (ntohs(arp->ar_pro) != PROT_IP) return;
if (arp->ar_hln != 6)
if (arp->ar_hln != ARP_HLEN) return;
if (arp->ar_pln != 4)
if (arp->ar_pln != ARP_PLEN) return; if (NetOurIP == 0) return;
if (NetReadIP(&arp->ar_data[16]) != NetOurIP)
if (NetReadIP(&arp->ar_tpa) != NetOurIP) return; switch (ntohs(arp->ar_op)) {
@@ -157,10 +157,10 @@ void ArpReceive(struct Ethernet_hdr *et, struct IP_UDP_hdr *ip, int len) pkt = (uchar *)et; pkt += NetSetEther(pkt, et->et_src, PROT_ARP); arp->ar_op = htons(ARPOP_REPLY);
memcpy(&arp->ar_data[10], &arp->ar_data[0], 6);
NetCopyIP(&arp->ar_data[16], &arp->ar_data[6]);
memcpy(&arp->ar_data[0], NetOurEther, 6);
NetCopyIP(&arp->ar_data[6], &NetOurIP);
memcpy(&arp->ar_tha, &arp->ar_sha, ARP_HLEN);
NetCopyIP(&arp->ar_tpa, &arp->ar_spa);
memcpy(&arp->ar_sha, NetOurEther, ARP_HLEN);
NetCopyIP(&arp->ar_spa, &NetOurIP); (void) eth_send((uchar *)et, (pkt - (uchar *)et) + ARP_HDR_SIZE); return;
@@ -173,12 +173,12 @@ void ArpReceive(struct Ethernet_hdr *et, struct IP_UDP_hdr *ip, int len) #ifdef CONFIG_KEEP_SERVERADDR if (NetServerIP == NetArpWaitPacketIP) { char buf[20];
sprintf(buf, "%pM", arp->ar_data);
sprintf(buf, "%pM", arp->ar_sha); setenv("serveraddr", buf); }
#endif
reply_ip_addr = NetReadIP(&arp->ar_data[6]);
reply_ip_addr = NetReadIP(&arp->ar_spa); /* matched waiting packet's address */ if (reply_ip_addr == NetArpWaitReplyIP) {
@@ -187,14 +187,14 @@ void ArpReceive(struct Ethernet_hdr *et, struct IP_UDP_hdr *ip, int len)
/* save address for later use */ memcpy(NetArpWaitPacketMAC,
&arp->ar_data[0], 6);
&arp->ar_sha, ARP_HLEN);
#ifdef CONFIG_NETCONSOLE NetGetHandler()(0, 0, 0, 0, 0); #endif /* modify header, and transmit it */ memcpy(((struct Ethernet_hdr *)NetArpWaitTxPacket)->
et_dest, NetArpWaitPacketMAC, 6);
et_dest, NetArpWaitPacketMAC, ARP_HLEN); (void) eth_send(NetArpWaitTxPacket, NetArpWaitTxPacketSize);
-- 1.6.0.2
Regards,
Simon

Share more of the code that is common between ARP vs not.
Signed-off-by: Joe Hershberger joe.hershberger@ni.com Cc: Joe Hershberger joe.hershberger@gmail.com Cc: Simon Glass sjg@chromium.org Cc: Mike Frysinger vapier@gentoo.org --- net/net.c | 44 +++++++++++++++++++++++++------------------- 1 files changed, 25 insertions(+), 19 deletions(-)
diff --git a/net/net.c b/net/net.c index a36f5e7..d5e6e54 100644 --- a/net/net.c +++ b/net/net.c @@ -598,6 +598,9 @@ NetSendUDPPacket(uchar *ether, IPaddr_t dest, int dport, int sport, int payload_len) { uchar *pkt; + int need_arp = 0; + int eth_hdr_size; + int pkt_hdr_size;
/* convert to new style broadcast */ if (dest == 0) @@ -612,40 +615,43 @@ NetSendUDPPacket(uchar *ether, IPaddr_t dest, int dport, int sport, * an ARP request */ if (memcmp(ether, NetEtherNullAddr, 6) == 0) { + need_arp = 1; + pkt = NetArpWaitTxPacket; + } else + pkt = (uchar *)NetTxPacket; + + eth_hdr_size = NetSetEther(pkt, ether, PROT_IP); + pkt += eth_hdr_size; + net_set_udp_header(pkt, dest, dport, sport, payload_len); + pkt_hdr_size = eth_hdr_size + IP_UDP_HDR_SIZE;
- debug("sending ARP for %08x\n", dest); + if (need_arp) { + debug("sending ARP for %pI4\n", &dest);
+ /* save the ip and eth addr for the packet to send after arp */ NetArpWaitPacketIP = dest; NetArpWaitPacketMAC = ether;
- pkt = NetArpWaitTxPacket; - pkt += NetSetEther(pkt, NetArpWaitPacketMAC, PROT_IP); - - net_set_udp_header(pkt, dest, dport, sport, payload_len); + /* + * Copy the packet data from the NetTxPacket into the + * NetArpWaitTxPacket to send after arp + */ memcpy(pkt + IP_UDP_HDR_SIZE, (uchar *)NetTxPacket + - (pkt - (uchar *)NetArpWaitTxPacket) + - IP_UDP_HDR_SIZE, payload_len); + pkt_hdr_size, payload_len);
/* size of the waiting packet */ - NetArpWaitTxPacketSize = (pkt - NetArpWaitTxPacket) + - IP_UDP_HDR_SIZE + payload_len; + NetArpWaitTxPacketSize = pkt_hdr_size + payload_len;
/* and do the ARP request */ NetArpWaitTry = 1; NetArpWaitTimerStart = get_timer(0); ArpRequest(); return 1; /* waiting */ + } else { + debug("sending UDP to %pI4/%pM\n", &dest, ether); + eth_send(NetTxPacket, pkt_hdr_size + payload_len); + return 0; /* transmitted */ } - - debug("sending UDP to %08x/%pM\n", dest, ether); - - pkt = (uchar *)NetTxPacket; - pkt += NetSetEther(pkt, ether, PROT_IP); - net_set_udp_header(pkt, dest, dport, sport, payload_len); - eth_send(NetTxPacket, (pkt - NetTxPacket) + IP_UDP_HDR_SIZE + - payload_len); - - return 0; /* transmitted */ }
#ifdef CONFIG_IP_DEFRAG

Hi Joe,
On Tue, Mar 27, 2012 at 4:42 PM, Joe Hershberger joe.hershberger@ni.com wrote:
Share more of the code that is common between ARP vs not.
Signed-off-by: Joe Hershberger joe.hershberger@ni.com Cc: Joe Hershberger joe.hershberger@gmail.com Cc: Simon Glass sjg@chromium.org Cc: Mike Frysinger vapier@gentoo.org
It seems ok to my eye.
Acked-by: Simon Glass sjg@chromium.org
Regards, Simon

Signed-off-by: Joe Hershberger joe.hershberger@ni.com Cc: Joe Hershberger joe.hershberger@gmail.com Cc: Simon Glass sjg@chromium.org Cc: Mike Frysinger vapier@gentoo.org --- net/arp.c | 13 ++++++++----- net/ping.c | 7 ++++--- net/rarp.c | 6 ++++-- 3 files changed, 16 insertions(+), 10 deletions(-)
diff --git a/net/arp.c b/net/arp.c index 6e3d7ab..6cb3c10 100644 --- a/net/arp.c +++ b/net/arp.c @@ -52,12 +52,14 @@ void ArpRequest(void) { uchar *pkt; struct ARP_hdr *arp; + int eth_hdr_size;
debug("ARP broadcast %d\n", NetArpWaitTry);
pkt = NetTxPacket;
- pkt += NetSetEther(pkt, NetBcastAddr, PROT_ARP); + eth_hdr_size = NetSetEther(pkt, NetBcastAddr, PROT_ARP); + pkt += eth_hdr_size;
arp = (struct ARP_hdr *) pkt;
@@ -86,7 +88,7 @@ void ArpRequest(void) }
NetWriteIP(&arp->ar_tpa, NetArpWaitReplyIP); - (void) eth_send(NetTxPacket, (pkt - NetTxPacket) + ARP_HDR_SIZE); + (void) eth_send(NetTxPacket, eth_hdr_size + ARP_HDR_SIZE); }
void ArpTimeoutCheck(void) @@ -118,6 +120,7 @@ void ArpReceive(struct Ethernet_hdr *et, struct IP_UDP_hdr *ip, int len) struct ARP_hdr *arp; IPaddr_t reply_ip_addr; uchar *pkt; + int eth_hdr_size;
/* * We have to deal with two types of ARP packets: @@ -155,14 +158,14 @@ void ArpReceive(struct Ethernet_hdr *et, struct IP_UDP_hdr *ip, int len) /* reply with our IP address */ debug("Got ARP REQUEST, return our IP\n"); pkt = (uchar *)et; - pkt += NetSetEther(pkt, et->et_src, PROT_ARP); + eth_hdr_size = NetSetEther(pkt, et->et_src, PROT_ARP); + pkt += eth_hdr_size; arp->ar_op = htons(ARPOP_REPLY); memcpy(&arp->ar_tha, &arp->ar_sha, ARP_HLEN); NetCopyIP(&arp->ar_tpa, &arp->ar_spa); memcpy(&arp->ar_sha, NetOurEther, ARP_HLEN); NetCopyIP(&arp->ar_spa, &NetOurIP); - (void) eth_send((uchar *)et, - (pkt - (uchar *)et) + ARP_HDR_SIZE); + (void) eth_send((uchar *)et, eth_hdr_size + ARP_HDR_SIZE); return;
case ARPOP_REPLY: /* arp reply */ diff --git a/net/ping.c b/net/ping.c index 0ef42a7..93942bc 100644 --- a/net/ping.c +++ b/net/ping.c @@ -42,6 +42,7 @@ static int ping_send(void) { static uchar mac[6]; uchar *pkt; + int eth_hdr_size;
/* XXX always send arp request */
@@ -53,13 +54,13 @@ static int ping_send(void) NetArpWaitPacketMAC = mac;
pkt = NetArpWaitTxPacket; - pkt += NetSetEther(pkt, mac, PROT_IP); + eth_hdr_size = NetSetEther(pkt, mac, PROT_IP); + pkt += eth_hdr_size;
set_icmp_header(pkt, NetPingIP);
/* size of the waiting packet */ - NetArpWaitTxPacketSize = - (pkt - NetArpWaitTxPacket) + IP_HDR_SIZE + 8; + NetArpWaitTxPacketSize = eth_hdr_size + IP_ICMP_HDR_SIZE;
/* and do the ARP request */ NetArpWaitTry = 1; diff --git a/net/rarp.c b/net/rarp.c index a77f077..d9dcd17 100644 --- a/net/rarp.c +++ b/net/rarp.c @@ -88,11 +88,13 @@ void RarpRequest(void) { uchar *pkt; struct ARP_hdr *rarp; + int eth_hdr_size;
printf("RARP broadcast %d\n", ++RarpTry); pkt = NetTxPacket;
- pkt += NetSetEther(pkt, NetBcastAddr, PROT_RARP); + eth_hdr_size = NetSetEther(pkt, NetBcastAddr, PROT_RARP); + pkt += eth_hdr_size;
rarp = (struct ARP_hdr *)pkt;
@@ -108,7 +110,7 @@ void RarpRequest(void) /* dest IP addr set to broadcast */ memset(&rarp->ar_data[16], 0xff, 4);
- NetSendPacket(NetTxPacket, (pkt - NetTxPacket) + ARP_HDR_SIZE); + NetSendPacket(NetTxPacket, eth_hdr_size + ARP_HDR_SIZE);
NetSetTimeout(TIMEOUT, RarpTimeout); }

On Tue, Mar 27, 2012 at 4:42 PM, Joe Hershberger joe.hershberger@ni.com wrote:
Signed-off-by: Joe Hershberger joe.hershberger@ni.com Cc: Joe Hershberger joe.hershberger@gmail.com Cc: Simon Glass sjg@chromium.org Cc: Mike Frysinger vapier@gentoo.org
Acked-by: Simon Glass sjg@chromium.org

Eliminate pointer subtraction that recovers values computed earlier
Signed-off-by: Joe Hershberger joe.hershberger@ni.com Cc: Joe Hershberger joe.hershberger@gmail.com Cc: Simon Glass sjg@chromium.org Cc: Mike Frysinger vapier@gentoo.org --- Changes for v2: - Split debug message move to a separate commit
net/bootp.c | 24 +++++++++++++----------- 1 files changed, 13 insertions(+), 11 deletions(-)
diff --git a/net/bootp.c b/net/bootp.c index 8914ada..caa6d03 100644 --- a/net/bootp.c +++ b/net/bootp.c @@ -583,7 +583,8 @@ BootpRequest(void) { uchar *pkt, *iphdr; struct Bootp_t *bp; - int ext_len, pktlen, iplen; + int extlen, pktlen, iplen; + int eth_hdr_size; #ifdef CONFIG_BOOTP_RANDOM_DELAY ulong i, rand_ms; #endif @@ -612,7 +613,8 @@ BootpRequest(void) pkt = NetTxPacket; memset((void *)pkt, 0, PKTSIZE);
- pkt += NetSetEther(pkt, NetBcastAddr, PROT_IP); + eth_hdr_size = NetSetEther(pkt, NetBcastAddr, PROT_IP); + pkt += eth_hdr_size;
/* * Next line results in incorrect packet size being transmitted, @@ -641,9 +643,9 @@ BootpRequest(void)
/* Request additional information from the BOOTP/DHCP server */ #if defined(CONFIG_CMD_DHCP) - ext_len = DhcpExtended((u8 *)bp->bp_vend, DHCP_DISCOVER, 0, 0); + extlen = DhcpExtended((u8 *)bp->bp_vend, DHCP_DISCOVER, 0, 0); #else - ext_len = BootpExtended((u8 *)bp->bp_vend); + extlen = BootpExtended((u8 *)bp->bp_vend); #endif
/* @@ -662,9 +664,8 @@ BootpRequest(void) * Calculate proper packet lengths taking into account the * variable size of the options field */ - pktlen = ((int)(pkt-NetTxPacket)) + BOOTP_HDR_SIZE - - sizeof(bp->bp_vend) + ext_len; - iplen = BOOTP_HDR_SIZE - sizeof(bp->bp_vend) + ext_len; + iplen = BOOTP_HDR_SIZE - OPT_FIELD_SIZE + extlen; + pktlen = eth_hdr_size + IP_UDP_HDR_SIZE + iplen; net_set_udp_header(iphdr, 0xFFFFFFFFL, PORT_BOOTPS, PORT_BOOTPC, iplen); NetSetTimeout(SELECT_TIMEOUT, BootpTimeout);
@@ -800,13 +801,15 @@ static void DhcpSendRequestPkt(struct Bootp_t *bp_offer) uchar *pkt, *iphdr; struct Bootp_t *bp; int pktlen, iplen, extlen; + int eth_hdr_size; IPaddr_t OfferedIP;
debug("DhcpSendRequestPkt: Sending DHCPREQUEST\n"); pkt = NetTxPacket; memset((void *)pkt, 0, PKTSIZE);
- pkt += NetSetEther(pkt, NetBcastAddr, PROT_IP); + eth_hdr_size = NetSetEther(pkt, NetBcastAddr, PROT_IP); + pkt += eth_hdr_size;
iphdr = pkt; /* We'll need this later to set proper pkt size */ pkt += IP_UDP_HDR_SIZE; @@ -843,9 +846,8 @@ static void DhcpSendRequestPkt(struct Bootp_t *bp_offer) extlen = DhcpExtended((u8 *)bp->bp_vend, DHCP_REQUEST, NetDHCPServerIP, OfferedIP);
- pktlen = ((int)(pkt-NetTxPacket)) + BOOTP_HDR_SIZE - - sizeof(bp->bp_vend) + extlen; - iplen = BOOTP_HDR_SIZE - sizeof(bp->bp_vend) + extlen; + iplen = BOOTP_HDR_SIZE - OPT_FIELD_SIZE + extlen; + pktlen = eth_hdr_size + IP_UDP_HDR_SIZE + iplen; net_set_udp_header(iphdr, 0xFFFFFFFFL, PORT_BOOTPS, PORT_BOOTPC, iplen);
debug("Transmitting DHCPREQUEST packet: len = %d\n", pktlen);

On Tue, Mar 27, 2012 at 4:42 PM, Joe Hershberger joe.hershberger@ni.com wrote:
Eliminate pointer subtraction that recovers values computed earlier
Signed-off-by: Joe Hershberger joe.hershberger@ni.com Cc: Joe Hershberger joe.hershberger@gmail.com Cc: Simon Glass sjg@chromium.org Cc: Mike Frysinger vapier@gentoo.org
Acked-by: Simon Glass sjg@chromium.org

Signed-off-by: Joe Hershberger joe.hershberger@ni.com Cc: Joe Hershberger joe.hershberger@gmail.com Cc: Simon Glass sjg@chromium.org Cc: Mike Frysinger vapier@gentoo.org --- Changes for v2: - Split from "Refactor bootp packet length computations"
net/bootp.c | 2 +- 1 files changed, 1 insertions(+), 1 deletions(-)
diff --git a/net/bootp.c b/net/bootp.c index caa6d03..2eeb14e 100644 --- a/net/bootp.c +++ b/net/bootp.c @@ -850,10 +850,10 @@ static void DhcpSendRequestPkt(struct Bootp_t *bp_offer) pktlen = eth_hdr_size + IP_UDP_HDR_SIZE + iplen; net_set_udp_header(iphdr, 0xFFFFFFFFL, PORT_BOOTPS, PORT_BOOTPC, iplen);
- debug("Transmitting DHCPREQUEST packet: len = %d\n", pktlen); #ifdef CONFIG_BOOTP_DHCP_REQUEST_DELAY udelay(CONFIG_BOOTP_DHCP_REQUEST_DELAY); #endif /* CONFIG_BOOTP_DHCP_REQUEST_DELAY */ + debug("Transmitting DHCPREQUEST packet: len = %d\n", pktlen); NetSendPacket(NetTxPacket, pktlen); }

On Tue, Mar 27, 2012 at 4:42 PM, Joe Hershberger joe.hershberger@ni.com wrote:
Signed-off-by: Joe Hershberger joe.hershberger@ni.com Cc: Joe Hershberger joe.hershberger@gmail.com Cc: Simon Glass sjg@chromium.org Cc: Mike Frysinger vapier@gentoo.org
Acked-by: Simon Glass sjg@chromium.org
Changes for v2: - Split from "Refactor bootp packet length computations"
net/bootp.c | 2 +- 1 files changed, 1 insertions(+), 1 deletions(-)
diff --git a/net/bootp.c b/net/bootp.c index caa6d03..2eeb14e 100644 --- a/net/bootp.c +++ b/net/bootp.c @@ -850,10 +850,10 @@ static void DhcpSendRequestPkt(struct Bootp_t *bp_offer) pktlen = eth_hdr_size + IP_UDP_HDR_SIZE + iplen; net_set_udp_header(iphdr, 0xFFFFFFFFL, PORT_BOOTPS, PORT_BOOTPC, iplen);
- debug("Transmitting DHCPREQUEST packet: len = %d\n", pktlen);
#ifdef CONFIG_BOOTP_DHCP_REQUEST_DELAY udelay(CONFIG_BOOTP_DHCP_REQUEST_DELAY); #endif /* CONFIG_BOOTP_DHCP_REQUEST_DELAY */
- debug("Transmitting DHCPREQUEST packet: len = %d\n", pktlen);
NetSendPacket(NetTxPacket, pktlen); }
-- 1.6.0.2

There is no need to call through the handler... inline it
Signed-off-by: Joe Hershberger joe.hershberger@ni.com Cc: Joe Hershberger joe.hershberger@gmail.com Cc: Simon Glass sjg@chromium.org Cc: Mike Frysinger vapier@gentoo.org --- net/ping.c | 18 ++---------------- 1 files changed, 2 insertions(+), 16 deletions(-)
diff --git a/net/ping.c b/net/ping.c index 93942bc..eb0f0cc 100644 --- a/net/ping.c +++ b/net/ping.c @@ -75,20 +75,10 @@ static void ping_timeout(void) NetState = NETLOOP_FAIL; /* we did not get the reply */ }
-static void ping_handler(uchar *pkt, unsigned dest, IPaddr_t sip, - unsigned src, unsigned len) -{ - if (sip != NetPingIP) - return; - - NetState = NETLOOP_SUCCESS; -} - void ping_start(void) { printf("Using %s device\n", eth_get_name()); NetSetTimeout(10000UL, ping_timeout); - NetSetHandler(ping_handler);
ping_send(); } @@ -100,13 +90,9 @@ void ping_receive(struct Ethernet_hdr *et, struct IP_UDP_hdr *ip, int len)
switch (icmph->type) { case ICMP_ECHO_REPLY: - /* - * IP header OK. Pass the packet to the - * current handler. - */ - /* XXX point to ip packet */ src_ip = NetReadIP((void *)&ip->ip_src); - NetGetHandler()((uchar *)ip, 0, src_ip, 0, 0); + if (src_ip == NetPingIP) + NetState = NETLOOP_SUCCESS; return; case ICMP_ECHO_REQUEST: debug("Got ICMP ECHO REQUEST, return "

On Tue, Mar 27, 2012 at 4:42 PM, Joe Hershberger joe.hershberger@ni.com wrote:
There is no need to call through the handler... inline it
Signed-off-by: Joe Hershberger joe.hershberger@ni.com Cc: Joe Hershberger joe.hershberger@gmail.com Cc: Simon Glass sjg@chromium.org Cc: Mike Frysinger vapier@gentoo.org
Acked-by: Simon Glass sjg@chromium.org
net/ping.c | 18 ++---------------- 1 files changed, 2 insertions(+), 16 deletions(-)
diff --git a/net/ping.c b/net/ping.c index 93942bc..eb0f0cc 100644 --- a/net/ping.c +++ b/net/ping.c @@ -75,20 +75,10 @@ static void ping_timeout(void) NetState = NETLOOP_FAIL; /* we did not get the reply */ }
-static void ping_handler(uchar *pkt, unsigned dest, IPaddr_t sip,
- unsigned src, unsigned len)
-{
- if (sip != NetPingIP)
- return;
- NetState = NETLOOP_SUCCESS;
-}
void ping_start(void) { printf("Using %s device\n", eth_get_name()); NetSetTimeout(10000UL, ping_timeout);
- NetSetHandler(ping_handler);
ping_send(); } @@ -100,13 +90,9 @@ void ping_receive(struct Ethernet_hdr *et, struct IP_UDP_hdr *ip, int len)
switch (icmph->type) { case ICMP_ECHO_REPLY:
- /*
- * IP header OK. Pass the packet to the
- * current handler.
- */
- /* XXX point to ip packet */
src_ip = NetReadIP((void *)&ip->ip_src);
- NetGetHandler()((uchar *)ip, 0, src_ip, 0, 0);
- if (src_ip == NetPingIP)
- NetState = NETLOOP_SUCCESS;
return; case ICMP_ECHO_REQUEST: debug("Got ICMP ECHO REQUEST, return " -- 1.6.0.2

Use this entry-point consistently across the net/ code Use a static inline function to preserve code size
Signed-off-by: Joe Hershberger joe.hershberger@ni.com Cc: Joe Hershberger joe.hershberger@gmail.com Cc: Simon Glass sjg@chromium.org Cc: Mike Frysinger vapier@gentoo.org --- Changes for v2: - Change NetSendPacket into a static inline
include/net.h | 5 ++++- net/arp.c | 6 +++--- net/cdp.c | 2 +- net/net.c | 9 +-------- net/ping.c | 3 +-- 5 files changed, 10 insertions(+), 15 deletions(-)
diff --git a/include/net.h b/include/net.h index 6eb25ee..f84511c 100644 --- a/include/net.h +++ b/include/net.h @@ -466,7 +466,10 @@ extern void net_set_icmp_handler(rxhand_icmp_f *f); /* Set ICMP RX handler */ extern void NetSetTimeout(ulong, thand_f *);/* Set timeout handler */
/* Transmit "NetTxPacket" */ -extern void NetSendPacket(uchar *, int); +static inline void NetSendPacket(uchar *pkt, int len) +{ + (void) eth_send(pkt, len); +}
/* Transmit UDP packet, performing ARP request if needed */ extern int NetSendUDPPacket(uchar *ether, IPaddr_t dest, int dport, diff --git a/net/arp.c b/net/arp.c index 6cb3c10..90a6b01 100644 --- a/net/arp.c +++ b/net/arp.c @@ -88,7 +88,7 @@ void ArpRequest(void) }
NetWriteIP(&arp->ar_tpa, NetArpWaitReplyIP); - (void) eth_send(NetTxPacket, eth_hdr_size + ARP_HDR_SIZE); + NetSendPacket(NetTxPacket, eth_hdr_size + ARP_HDR_SIZE); }
void ArpTimeoutCheck(void) @@ -165,7 +165,7 @@ void ArpReceive(struct Ethernet_hdr *et, struct IP_UDP_hdr *ip, int len) NetCopyIP(&arp->ar_tpa, &arp->ar_spa); memcpy(&arp->ar_sha, NetOurEther, ARP_HLEN); NetCopyIP(&arp->ar_spa, &NetOurIP); - (void) eth_send((uchar *)et, eth_hdr_size + ARP_HDR_SIZE); + NetSendPacket((uchar *)et, eth_hdr_size + ARP_HDR_SIZE); return;
case ARPOP_REPLY: /* arp reply */ @@ -198,7 +198,7 @@ void ArpReceive(struct Ethernet_hdr *et, struct IP_UDP_hdr *ip, int len) /* modify header, and transmit it */ memcpy(((struct Ethernet_hdr *)NetArpWaitTxPacket)-> et_dest, NetArpWaitPacketMAC, ARP_HLEN); - (void) eth_send(NetArpWaitTxPacket, + NetSendPacket(NetArpWaitTxPacket, NetArpWaitTxPacketSize);
/* no arp request pending now */ diff --git a/net/cdp.c b/net/cdp.c index 5c4b3e8..34c5401 100644 --- a/net/cdp.c +++ b/net/cdp.c @@ -215,7 +215,7 @@ CDPSendTrigger(void) chksum = 0xFFFF; *cp = htons(chksum);
- (void) eth_send(NetTxPacket, (uchar *)s - NetTxPacket); + NetSendPacket(NetTxPacket, (uchar *)s - NetTxPacket); return 0; }
diff --git a/net/net.c b/net/net.c index d5e6e54..0d14e2c 100644 --- a/net/net.c +++ b/net/net.c @@ -586,13 +586,6 @@ NetSetTimeout(ulong iv, thand_f *f) } }
- -void -NetSendPacket(uchar *pkt, int len) -{ - (void) eth_send(pkt, len); -} - int NetSendUDPPacket(uchar *ether, IPaddr_t dest, int dport, int sport, int payload_len) @@ -649,7 +642,7 @@ NetSendUDPPacket(uchar *ether, IPaddr_t dest, int dport, int sport, return 1; /* waiting */ } else { debug("sending UDP to %pI4/%pM\n", &dest, ether); - eth_send(NetTxPacket, pkt_hdr_size + payload_len); + NetSendPacket(NetTxPacket, pkt_hdr_size + payload_len); return 0; /* transmitted */ } } diff --git a/net/ping.c b/net/ping.c index eb0f0cc..4896bce 100644 --- a/net/ping.c +++ b/net/ping.c @@ -112,8 +112,7 @@ void ping_receive(struct Ethernet_hdr *et, struct IP_UDP_hdr *ip, int len) icmph->checksum = 0; icmph->checksum = ~NetCksum((uchar *)icmph, (len - IP_HDR_SIZE) >> 1); - (void) eth_send((uchar *)et, - ETHER_HDR_SIZE + len); + NetSendPacket((uchar *)et, ETHER_HDR_SIZE + len); return; /* default: return;*/

On Tue, Mar 27, 2012 at 4:42 PM, Joe Hershberger joe.hershberger@ni.com wrote:
Use this entry-point consistently across the net/ code Use a static inline function to preserve code size
Signed-off-by: Joe Hershberger joe.hershberger@ni.com Cc: Joe Hershberger joe.hershberger@gmail.com Cc: Simon Glass sjg@chromium.org Cc: Mike Frysinger vapier@gentoo.org
Acked-by: Simon Glass sjg@chromium.org
Changes for v2: - Change NetSendPacket into a static inline
include/net.h | 5 ++++- net/arp.c | 6 +++--- net/cdp.c | 2 +- net/net.c | 9 +-------- net/ping.c | 3 +-- 5 files changed, 10 insertions(+), 15 deletions(-)
diff --git a/include/net.h b/include/net.h index 6eb25ee..f84511c 100644 --- a/include/net.h +++ b/include/net.h @@ -466,7 +466,10 @@ extern void net_set_icmp_handler(rxhand_icmp_f *f); /* Set ICMP RX handler */ extern void NetSetTimeout(ulong, thand_f *);/* Set timeout handler */
/* Transmit "NetTxPacket" */ -extern void NetSendPacket(uchar *, int); +static inline void NetSendPacket(uchar *pkt, int len) +{
- (void) eth_send(pkt, len);
+}
/* Transmit UDP packet, performing ARP request if needed */ extern int NetSendUDPPacket(uchar *ether, IPaddr_t dest, int dport, diff --git a/net/arp.c b/net/arp.c index 6cb3c10..90a6b01 100644 --- a/net/arp.c +++ b/net/arp.c @@ -88,7 +88,7 @@ void ArpRequest(void) }
NetWriteIP(&arp->ar_tpa, NetArpWaitReplyIP);
- (void) eth_send(NetTxPacket, eth_hdr_size + ARP_HDR_SIZE);
- NetSendPacket(NetTxPacket, eth_hdr_size + ARP_HDR_SIZE);
}
void ArpTimeoutCheck(void) @@ -165,7 +165,7 @@ void ArpReceive(struct Ethernet_hdr *et, struct IP_UDP_hdr *ip, int len) NetCopyIP(&arp->ar_tpa, &arp->ar_spa); memcpy(&arp->ar_sha, NetOurEther, ARP_HLEN); NetCopyIP(&arp->ar_spa, &NetOurIP);
- (void) eth_send((uchar *)et, eth_hdr_size + ARP_HDR_SIZE);
- NetSendPacket((uchar *)et, eth_hdr_size + ARP_HDR_SIZE);
return;
case ARPOP_REPLY: /* arp reply */ @@ -198,7 +198,7 @@ void ArpReceive(struct Ethernet_hdr *et, struct IP_UDP_hdr *ip, int len) /* modify header, and transmit it */ memcpy(((struct Ethernet_hdr *)NetArpWaitTxPacket)-> et_dest, NetArpWaitPacketMAC, ARP_HLEN);
- (void) eth_send(NetArpWaitTxPacket,
- NetSendPacket(NetArpWaitTxPacket,
NetArpWaitTxPacketSize);
/* no arp request pending now */ diff --git a/net/cdp.c b/net/cdp.c index 5c4b3e8..34c5401 100644 --- a/net/cdp.c +++ b/net/cdp.c @@ -215,7 +215,7 @@ CDPSendTrigger(void) chksum = 0xFFFF; *cp = htons(chksum);
- (void) eth_send(NetTxPacket, (uchar *)s - NetTxPacket);
- NetSendPacket(NetTxPacket, (uchar *)s - NetTxPacket);
return 0; }
diff --git a/net/net.c b/net/net.c index d5e6e54..0d14e2c 100644 --- a/net/net.c +++ b/net/net.c @@ -586,13 +586,6 @@ NetSetTimeout(ulong iv, thand_f *f) } }
-void -NetSendPacket(uchar *pkt, int len) -{
- (void) eth_send(pkt, len);
-}
int NetSendUDPPacket(uchar *ether, IPaddr_t dest, int dport, int sport, int payload_len) @@ -649,7 +642,7 @@ NetSendUDPPacket(uchar *ether, IPaddr_t dest, int dport, int sport, return 1; /* waiting */ } else { debug("sending UDP to %pI4/%pM\n", &dest, ether);
- eth_send(NetTxPacket, pkt_hdr_size + payload_len);
- NetSendPacket(NetTxPacket, pkt_hdr_size + payload_len);
return 0; /* transmitted */ } } diff --git a/net/ping.c b/net/ping.c index eb0f0cc..4896bce 100644 --- a/net/ping.c +++ b/net/ping.c @@ -112,8 +112,7 @@ void ping_receive(struct Ethernet_hdr *et, struct IP_UDP_hdr *ip, int len) icmph->checksum = 0; icmph->checksum = ~NetCksum((uchar *)icmph, (len - IP_HDR_SIZE) >> 1);
- (void) eth_send((uchar *)et,
- ETHER_HDR_SIZE + len);
- NetSendPacket((uchar *)et, ETHER_HDR_SIZE + len);
return; /* default: return;*/ -- 1.6.0.2

Changes to NetState now go through an accessor function called net_set_state()
Signed-off-by: Joe Hershberger joe.hershberger@ni.com Cc: Joe Hershberger joe.hershberger@gmail.com Cc: Simon Glass sjg@chromium.org Cc: Mike Frysinger vapier@gentoo.org --- Changes for v2: - net_set_state changed to static inline - States changed to an enum - Eliminate CamelCase in new functions.
drivers/net/netconsole.c | 6 +++--- include/net.h | 16 ++++++++++------ net/cdp.c | 2 +- net/dns.c | 8 ++++---- net/net.c | 21 ++++++++++++--------- net/nfs.c | 8 ++++---- net/ping.c | 4 ++-- net/sntp.c | 4 ++-- net/tftp.c | 8 ++++---- 9 files changed, 42 insertions(+), 35 deletions(-)
diff --git a/drivers/net/netconsole.c b/drivers/net/netconsole.c index ba02fd7..28bb955 100644 --- a/drivers/net/netconsole.c +++ b/drivers/net/netconsole.c @@ -44,19 +44,19 @@ static void nc_wait_arp_handler(uchar *pkt, unsigned dest, IPaddr_t sip, unsigned src, unsigned len) { - NetState = NETLOOP_SUCCESS; /* got arp reply - quit net loop */ + net_set_state(NETLOOP_SUCCESS); /* got arp reply - quit net loop */ }
static void nc_handler(uchar *pkt, unsigned dest, IPaddr_t sip, unsigned src, unsigned len) { if (input_size) - NetState = NETLOOP_SUCCESS; /* got input - quit net loop */ + net_set_state(NETLOOP_SUCCESS); /* got input - quit net loop */ }
static void nc_timeout(void) { - NetState = NETLOOP_SUCCESS; + net_set_state(NETLOOP_SUCCESS); }
void NcStart(void) diff --git a/include/net.h b/include/net.h index f84511c..c33cd28 100644 --- a/include/net.h +++ b/include/net.h @@ -390,12 +390,6 @@ extern uchar NetEtherNullAddr[6]; extern ushort NetOurVLAN; /* Our VLAN */ extern ushort NetOurNativeVLAN; /* Our Native VLAN */
-extern int NetState; /* Network loop state */ -#define NETLOOP_CONTINUE 1 -#define NETLOOP_RESTART 2 -#define NETLOOP_SUCCESS 3 -#define NETLOOP_FAIL 4 - extern int NetRestartWrap; /* Tried all network devices */
enum proto_t { @@ -465,6 +459,16 @@ extern void NetSetHandler(rxhand_f *); /* Set RX packet handler */ extern void net_set_icmp_handler(rxhand_icmp_f *f); /* Set ICMP RX handler */ extern void NetSetTimeout(ulong, thand_f *);/* Set timeout handler */
+/* Network loop state */ +enum net_loop_state { + NETLOOP_CONTINUE, NETLOOP_RESTART, NETLOOP_SUCCESS, NETLOOP_FAIL +}; +static inline void net_set_state(enum net_loop_state state) +{ + extern enum net_loop_state net_state; + net_state = state; +} + /* Transmit "NetTxPacket" */ static inline void NetSendPacket(uchar *pkt, int len) { diff --git a/net/cdp.c b/net/cdp.c index 34c5401..f1c1c14 100644 --- a/net/cdp.c +++ b/net/cdp.c @@ -234,7 +234,7 @@ CDPTimeout(void) if (!CDPOK) NetStartAgain(); else - NetState = NETLOOP_SUCCESS; + net_set_state(NETLOOP_SUCCESS); }
static void diff --git a/net/dns.c b/net/dns.c index cc7ed51..cc0aa0a 100644 --- a/net/dns.c +++ b/net/dns.c @@ -98,7 +98,7 @@ static void DnsTimeout(void) { puts("Timeout\n"); - NetState = NETLOOP_FAIL; + net_set_state(NETLOOP_FAIL); }
static void @@ -128,7 +128,7 @@ DnsHandler(uchar *pkt, unsigned dest, IPaddr_t sip, unsigned src, unsigned len) /* Received 0 answers */ if (header->nanswers == 0) { puts("DNS: host not found\n"); - NetState = NETLOOP_SUCCESS; + net_set_state(NETLOOP_SUCCESS); return; }
@@ -141,7 +141,7 @@ DnsHandler(uchar *pkt, unsigned dest, IPaddr_t sip, unsigned src, unsigned len) /* We sent query class 1, query type 1 */ if (&p[5] > e || get_unaligned_be16(p+1) != DNS_A_RECORD) { puts("DNS: response was not an A record\n"); - NetState = NETLOOP_SUCCESS; + net_set_state(NETLOOP_SUCCESS); return; }
@@ -191,7 +191,7 @@ DnsHandler(uchar *pkt, unsigned dest, IPaddr_t sip, unsigned src, unsigned len) puts("server responded with invalid IP number\n"); }
- NetState = NETLOOP_SUCCESS; + net_set_state(NETLOOP_SUCCESS); }
void diff --git a/net/net.c b/net/net.c index 0d14e2c..8076e5f 100644 --- a/net/net.c +++ b/net/net.c @@ -149,7 +149,7 @@ uchar NetEtherNullAddr[6]; void (*push_packet)(void *, int len) = 0; #endif /* Network loop state */ -int NetState; +enum net_loop_state net_state; /* Tried all network devices */ int NetRestartWrap; /* Network loop restarted */ @@ -212,7 +212,7 @@ void net_auto_load(void) * Just use BOOTP/RARP to configure system; * Do not use TFTP to load the bootfile. */ - NetState = NETLOOP_SUCCESS; + net_set_state(NETLOOP_SUCCESS); return; } #if defined(CONFIG_CMD_NFS) @@ -292,7 +292,7 @@ int NetLoop(enum proto_t protocol) restart: memcpy(NetOurEther, eth_get_dev()->enetaddr, 6);
- NetState = NETLOOP_CONTINUE; + net_set_state(NETLOOP_CONTINUE);
/* * Start the ball rolling with the given start function. From @@ -401,7 +401,7 @@ restart:
/* * Main packet reception loop. Loop receiving packets until - * someone sets `NetState' to a state that terminates. + * someone sets `net_state' to a state that terminates. */ for (;;) { WATCHDOG_RESET(); @@ -453,7 +453,7 @@ restart: }
- switch (NetState) { + switch (net_state) {
case NETLOOP_RESTART: NetRestarted = 1; @@ -477,6 +477,9 @@ restart:
case NETLOOP_FAIL: goto done; + + case NETLOOP_CONTINUE: + continue; } }
@@ -494,7 +497,7 @@ done: static void startAgainTimeout(void) { - NetState = NETLOOP_RESTART; + net_set_state(NETLOOP_RESTART); }
static void @@ -525,7 +528,7 @@ void NetStartAgain(void)
if ((!retry_forever) && (NetTryCount >= retrycnt)) { eth_halt(); - NetState = NETLOOP_FAIL; + net_set_state(NETLOOP_FAIL); return; }
@@ -542,10 +545,10 @@ void NetStartAgain(void) NetSetTimeout(10000UL, startAgainTimeout); NetSetHandler(startAgainHandler); } else { - NetState = NETLOOP_FAIL; + net_set_state(NETLOOP_FAIL); } } else { - NetState = NETLOOP_RESTART; + net_set_state(NETLOOP_RESTART); } }
diff --git a/net/nfs.c b/net/nfs.c index b6188fe..4f2041c 100644 --- a/net/nfs.c +++ b/net/nfs.c @@ -41,7 +41,7 @@ static int nfs_len; static char dirfh[NFS_FHSIZE]; /* file handle of directory */ static char filefh[NFS_FHSIZE]; /* file handle of kernel image */
-static int NfsDownloadState; +static enum net_loop_state NfsDownloadState; static IPaddr_t NfsServerIP; static int NfsSrvMountPort; static int NfsSrvNfsPort; @@ -613,10 +613,10 @@ NfsHandler(uchar *pkt, unsigned dest, IPaddr_t sip, unsigned src, unsigned len) case STATE_UMOUNT_REQ: if (nfs_umountall_reply(pkt, len)) { puts("*** ERROR: Cannot umount\n"); - NetState = NETLOOP_FAIL; + net_set_state(NETLOOP_FAIL); } else { puts("\ndone\n"); - NetState = NfsDownloadState; + net_set_state(NfsDownloadState); } break;
@@ -679,7 +679,7 @@ NfsStart(void) nfs_path = (char *)nfs_path_buff;
if (nfs_path == NULL) { - NetState = NETLOOP_FAIL; + net_set_state(NETLOOP_FAIL); puts("*** ERROR: Fail allocate memory\n"); return; } diff --git a/net/ping.c b/net/ping.c index 4896bce..6cdcdf0 100644 --- a/net/ping.c +++ b/net/ping.c @@ -72,7 +72,7 @@ static int ping_send(void) static void ping_timeout(void) { eth_halt(); - NetState = NETLOOP_FAIL; /* we did not get the reply */ + net_set_state(NETLOOP_FAIL); /* we did not get the reply */ }
void ping_start(void) @@ -92,7 +92,7 @@ void ping_receive(struct Ethernet_hdr *et, struct IP_UDP_hdr *ip, int len) case ICMP_ECHO_REPLY: src_ip = NetReadIP((void *)&ip->ip_src); if (src_ip == NetPingIP) - NetState = NETLOOP_SUCCESS; + net_set_state(NETLOOP_SUCCESS); return; case ICMP_ECHO_REQUEST: debug("Got ICMP ECHO REQUEST, return " diff --git a/net/sntp.c b/net/sntp.c index 7997f98..2adcc6f 100644 --- a/net/sntp.c +++ b/net/sntp.c @@ -45,7 +45,7 @@ static void SntpTimeout(void) { puts("Timeout\n"); - NetState = NETLOOP_FAIL; + net_set_state(NETLOOP_FAIL); return; }
@@ -76,7 +76,7 @@ SntpHandler(uchar *pkt, unsigned dest, IPaddr_t sip, unsigned src, tm.tm_year, tm.tm_mon, tm.tm_mday, tm.tm_hour, tm.tm_min, tm.tm_sec);
- NetState = NETLOOP_SUCCESS; + net_set_state(NETLOOP_SUCCESS); }
void diff --git a/net/tftp.c b/net/tftp.c index a04a832..bf32eab 100644 --- a/net/tftp.c +++ b/net/tftp.c @@ -177,7 +177,7 @@ store_block(unsigned block, uchar *src, unsigned len) rc = flash_write((char *)src, (ulong)(load_addr+offset), len); if (rc) { flash_perror(rc); - NetState = NETLOOP_FAIL; + net_set_state(NETLOOP_FAIL); return; } } else @@ -300,7 +300,7 @@ static void tftp_complete(void) } #endif puts("\ndone\n"); - NetState = NETLOOP_SUCCESS; + net_set_state(NETLOOP_SUCCESS); }
static void @@ -627,7 +627,7 @@ TftpHandler(uchar *pkt, unsigned dest, IPaddr_t sip, unsigned src, if (MasterClient && (TftpBlock >= TftpEndingBlock)) { puts("\nMulticast tftp done\n"); mcast_cleanup(); - NetState = NETLOOP_SUCCESS; + net_set_state(NETLOOP_SUCCESS); } } else #endif @@ -644,7 +644,7 @@ TftpHandler(uchar *pkt, unsigned dest, IPaddr_t sip, unsigned src, case TFTP_ERR_ACCESS_DENIED: puts("Not retrying...\n"); eth_halt(); - NetState = NETLOOP_FAIL; + net_set_state(NETLOOP_FAIL); break; case TFTP_ERR_UNDEFINED: case TFTP_ERR_DISK_FULL:

Hi Joe,
On Tue, Mar 27, 2012 at 4:42 PM, Joe Hershberger joe.hershberger@ni.com wrote:
Changes to NetState now go through an accessor function called net_set_state()
Signed-off-by: Joe Hershberger joe.hershberger@ni.com Cc: Joe Hershberger joe.hershberger@gmail.com Cc: Simon Glass sjg@chromium.org Cc: Mike Frysinger vapier@gentoo.org
Changes for v2: - net_set_state changed to static inline - States changed to an enum - Eliminate CamelCase in new functions.
drivers/net/netconsole.c | 6 +++--- include/net.h | 16 ++++++++++------ net/cdp.c | 2 +- net/dns.c | 8 ++++---- net/net.c | 21 ++++++++++++--------- net/nfs.c | 8 ++++---- net/ping.c | 4 ++-- net/sntp.c | 4 ++-- net/tftp.c | 8 ++++---- 9 files changed, 42 insertions(+), 35 deletions(-)
diff --git a/drivers/net/netconsole.c b/drivers/net/netconsole.c index ba02fd7..28bb955 100644 --- a/drivers/net/netconsole.c +++ b/drivers/net/netconsole.c @@ -44,19 +44,19 @@ static void nc_wait_arp_handler(uchar *pkt, unsigned dest, IPaddr_t sip, unsigned src, unsigned len) {
- NetState = NETLOOP_SUCCESS; /* got arp reply - quit net loop */
- net_set_state(NETLOOP_SUCCESS); /* got arp reply - quit net loop */
}
static void nc_handler(uchar *pkt, unsigned dest, IPaddr_t sip, unsigned src, unsigned len) { if (input_size)
- NetState = NETLOOP_SUCCESS; /* got input - quit net loop */
- net_set_state(NETLOOP_SUCCESS); /* got input - quit net loop */
}
static void nc_timeout(void) {
- NetState = NETLOOP_SUCCESS;
- net_set_state(NETLOOP_SUCCESS);
}
void NcStart(void) diff --git a/include/net.h b/include/net.h index f84511c..c33cd28 100644 --- a/include/net.h +++ b/include/net.h @@ -390,12 +390,6 @@ extern uchar NetEtherNullAddr[6]; extern ushort NetOurVLAN; /* Our VLAN */ extern ushort NetOurNativeVLAN; /* Our Native VLAN */
-extern int NetState; /* Network loop state */ -#define NETLOOP_CONTINUE 1 -#define NETLOOP_RESTART 2 -#define NETLOOP_SUCCESS 3 -#define NETLOOP_FAIL 4
extern int NetRestartWrap; /* Tried all network devices */
enum proto_t { @@ -465,6 +459,16 @@ extern void NetSetHandler(rxhand_f *); /* Set RX packet handler */ extern void net_set_icmp_handler(rxhand_icmp_f *f); /* Set ICMP RX handler */ extern void NetSetTimeout(ulong, thand_f *);/* Set timeout handler */
+/* Network loop state */ +enum net_loop_state {
- NETLOOP_CONTINUE, NETLOOP_RESTART, NETLOOP_SUCCESS, NETLOOP_FAIL
Do we need these all on one line?
+}; +static inline void net_set_state(enum net_loop_state state) +{
- extern enum net_loop_state net_state;
blank line?
- net_state = state;
+}
/* Transmit "NetTxPacket" */ static inline void NetSendPacket(uchar *pkt, int len) { diff --git a/net/cdp.c b/net/cdp.c index 34c5401..f1c1c14 100644 --- a/net/cdp.c +++ b/net/cdp.c @@ -234,7 +234,7 @@ CDPTimeout(void) if (!CDPOK) NetStartAgain(); else
- NetState = NETLOOP_SUCCESS;
- net_set_state(NETLOOP_SUCCESS);
}
static void diff --git a/net/dns.c b/net/dns.c index cc7ed51..cc0aa0a 100644 --- a/net/dns.c +++ b/net/dns.c @@ -98,7 +98,7 @@ static void DnsTimeout(void) { puts("Timeout\n");
- NetState = NETLOOP_FAIL;
- net_set_state(NETLOOP_FAIL);
}
static void @@ -128,7 +128,7 @@ DnsHandler(uchar *pkt, unsigned dest, IPaddr_t sip, unsigned src, unsigned len) /* Received 0 answers */ if (header->nanswers == 0) { puts("DNS: host not found\n");
- NetState = NETLOOP_SUCCESS;
- net_set_state(NETLOOP_SUCCESS);
return; }
@@ -141,7 +141,7 @@ DnsHandler(uchar *pkt, unsigned dest, IPaddr_t sip, unsigned src, unsigned len) /* We sent query class 1, query type 1 */ if (&p[5] > e || get_unaligned_be16(p+1) != DNS_A_RECORD) { puts("DNS: response was not an A record\n");
- NetState = NETLOOP_SUCCESS;
- net_set_state(NETLOOP_SUCCESS);
return; }
@@ -191,7 +191,7 @@ DnsHandler(uchar *pkt, unsigned dest, IPaddr_t sip, unsigned src, unsigned len) puts("server responded with invalid IP number\n"); }
- NetState = NETLOOP_SUCCESS;
- net_set_state(NETLOOP_SUCCESS);
}
void diff --git a/net/net.c b/net/net.c index 0d14e2c..8076e5f 100644 --- a/net/net.c +++ b/net/net.c @@ -149,7 +149,7 @@ uchar NetEtherNullAddr[6]; void (*push_packet)(void *, int len) = 0; #endif /* Network loop state */ -int NetState; +enum net_loop_state net_state; /* Tried all network devices */ int NetRestartWrap; /* Network loop restarted */ @@ -212,7 +212,7 @@ void net_auto_load(void) * Just use BOOTP/RARP to configure system; * Do not use TFTP to load the bootfile. */
- NetState = NETLOOP_SUCCESS;
- net_set_state(NETLOOP_SUCCESS);
return; } #if defined(CONFIG_CMD_NFS) @@ -292,7 +292,7 @@ int NetLoop(enum proto_t protocol) restart: memcpy(NetOurEther, eth_get_dev()->enetaddr, 6);
- NetState = NETLOOP_CONTINUE;
- net_set_state(NETLOOP_CONTINUE);
/* * Start the ball rolling with the given start function. From @@ -401,7 +401,7 @@ restart:
/* * Main packet reception loop. Loop receiving packets until
- * someone sets `NetState' to a state that terminates.
- * someone sets `net_state' to a state that terminates.
*/ for (;;) { WATCHDOG_RESET(); @@ -453,7 +453,7 @@ restart: }
- switch (NetState) {
- switch (net_state) {
case NETLOOP_RESTART: NetRestarted = 1; @@ -477,6 +477,9 @@ restart:
case NETLOOP_FAIL: goto done;
- case NETLOOP_CONTINUE:
- continue;
} }
@@ -494,7 +497,7 @@ done: static void startAgainTimeout(void) {
- NetState = NETLOOP_RESTART;
- net_set_state(NETLOOP_RESTART);
}
static void @@ -525,7 +528,7 @@ void NetStartAgain(void)
if ((!retry_forever) && (NetTryCount >= retrycnt)) { eth_halt();
- NetState = NETLOOP_FAIL;
- net_set_state(NETLOOP_FAIL);
return; }
@@ -542,10 +545,10 @@ void NetStartAgain(void) NetSetTimeout(10000UL, startAgainTimeout); NetSetHandler(startAgainHandler); } else {
- NetState = NETLOOP_FAIL;
- net_set_state(NETLOOP_FAIL);
} } else {
- NetState = NETLOOP_RESTART;
- net_set_state(NETLOOP_RESTART);
} }
diff --git a/net/nfs.c b/net/nfs.c index b6188fe..4f2041c 100644 --- a/net/nfs.c +++ b/net/nfs.c @@ -41,7 +41,7 @@ static int nfs_len; static char dirfh[NFS_FHSIZE]; /* file handle of directory */ static char filefh[NFS_FHSIZE]; /* file handle of kernel image */
-static int NfsDownloadState; +static enum net_loop_state NfsDownloadState;
Can you use nfs_download_state instead?
static IPaddr_t NfsServerIP; static int NfsSrvMountPort; static int NfsSrvNfsPort; @@ -613,10 +613,10 @@ NfsHandler(uchar *pkt, unsigned dest, IPaddr_t sip, unsigned src, unsigned len) case STATE_UMOUNT_REQ: if (nfs_umountall_reply(pkt, len)) { puts("*** ERROR: Cannot umount\n");
- NetState = NETLOOP_FAIL;
- net_set_state(NETLOOP_FAIL);
} else { puts("\ndone\n");
- NetState = NfsDownloadState;
- net_set_state(NfsDownloadState);
} break;
@@ -679,7 +679,7 @@ NfsStart(void) nfs_path = (char *)nfs_path_buff;
if (nfs_path == NULL) {
- NetState = NETLOOP_FAIL;
- net_set_state(NETLOOP_FAIL);
puts("*** ERROR: Fail allocate memory\n"); return; } diff --git a/net/ping.c b/net/ping.c index 4896bce..6cdcdf0 100644 --- a/net/ping.c +++ b/net/ping.c @@ -72,7 +72,7 @@ static int ping_send(void) static void ping_timeout(void) { eth_halt();
- NetState = NETLOOP_FAIL; /* we did not get the reply */
- net_set_state(NETLOOP_FAIL); /* we did not get the reply */
}
void ping_start(void) @@ -92,7 +92,7 @@ void ping_receive(struct Ethernet_hdr *et, struct IP_UDP_hdr *ip, int len) case ICMP_ECHO_REPLY: src_ip = NetReadIP((void *)&ip->ip_src); if (src_ip == NetPingIP)
- NetState = NETLOOP_SUCCESS;
- net_set_state(NETLOOP_SUCCESS);
return; case ICMP_ECHO_REQUEST: debug("Got ICMP ECHO REQUEST, return " diff --git a/net/sntp.c b/net/sntp.c index 7997f98..2adcc6f 100644 --- a/net/sntp.c +++ b/net/sntp.c @@ -45,7 +45,7 @@ static void SntpTimeout(void) { puts("Timeout\n");
- NetState = NETLOOP_FAIL;
- net_set_state(NETLOOP_FAIL);
return; }
@@ -76,7 +76,7 @@ SntpHandler(uchar *pkt, unsigned dest, IPaddr_t sip, unsigned src, tm.tm_year, tm.tm_mon, tm.tm_mday, tm.tm_hour, tm.tm_min, tm.tm_sec);
- NetState = NETLOOP_SUCCESS;
- net_set_state(NETLOOP_SUCCESS);
}
void diff --git a/net/tftp.c b/net/tftp.c index a04a832..bf32eab 100644 --- a/net/tftp.c +++ b/net/tftp.c @@ -177,7 +177,7 @@ store_block(unsigned block, uchar *src, unsigned len) rc = flash_write((char *)src, (ulong)(load_addr+offset), len); if (rc) { flash_perror(rc);
- NetState = NETLOOP_FAIL;
- net_set_state(NETLOOP_FAIL);
return; } } else @@ -300,7 +300,7 @@ static void tftp_complete(void) } #endif puts("\ndone\n");
- NetState = NETLOOP_SUCCESS;
- net_set_state(NETLOOP_SUCCESS);
}
static void @@ -627,7 +627,7 @@ TftpHandler(uchar *pkt, unsigned dest, IPaddr_t sip, unsigned src, if (MasterClient && (TftpBlock >= TftpEndingBlock)) { puts("\nMulticast tftp done\n"); mcast_cleanup();
- NetState = NETLOOP_SUCCESS;
- net_set_state(NETLOOP_SUCCESS);
} } else #endif @@ -644,7 +644,7 @@ TftpHandler(uchar *pkt, unsigned dest, IPaddr_t sip, unsigned src, case TFTP_ERR_ACCESS_DENIED: puts("Not retrying...\n"); eth_halt();
- NetState = NETLOOP_FAIL;
- net_set_state(NETLOOP_FAIL);
break; case TFTP_ERR_UNDEFINED: case TFTP_ERR_DISK_FULL: -- 1.6.0.2
Regards, Simon

Call a built-in dummy if none is registered... don't require protocols to register a handler (eliminating dummies) NetConsole now uses the ARP handler when waiting on arp (instead of needing a #define hack in arp.c) Clear handlers at the end of net loop
Signed-off-by: Joe Hershberger joe.hershberger@ni.com Cc: Joe Hershberger joe.hershberger@gmail.com Cc: Simon Glass sjg@chromium.org Cc: Mike Frysinger vapier@gentoo.org --- Changes for v2: - Change NULL checks into dummy functions - Must now call net_set_*_handler(NULL); in init to set dummies - Eliminate CamelCase for new functions
drivers/net/netconsole.c | 4 +- include/net.h | 9 +++- net/arp.c | 6 +- net/bootp.c | 4 +- net/cdp.c | 8 ---- net/dns.c | 2 +- net/net.c | 95 ++++++++++++++++++++++++++++++---------------- net/nfs.c | 2 +- net/sntp.c | 2 +- net/tftp.c | 4 +- 10 files changed, 80 insertions(+), 56 deletions(-)
diff --git a/drivers/net/netconsole.c b/drivers/net/netconsole.c index 28bb955..744f4d1 100644 --- a/drivers/net/netconsole.c +++ b/drivers/net/netconsole.c @@ -63,12 +63,12 @@ void NcStart(void) { if (!output_packet_len || memcmp(nc_ether, NetEtherNullAddr, 6)) { /* going to check for input packet */ - NetSetHandler(nc_handler); + net_set_udp_handler(nc_handler); NetSetTimeout(net_timeout, nc_timeout); } else { /* send arp request */ uchar *pkt; - NetSetHandler(nc_wait_arp_handler); + net_set_arp_handler(nc_wait_arp_handler); pkt = (uchar *)NetTxPacket + NetEthHdrSize() + IP_UDP_HDR_SIZE; memcpy(pkt, output_packet, output_packet_len); NetSendUDPPacket(nc_ether, nc_ip, nc_port, nc_port, diff --git a/include/net.h b/include/net.h index c33cd28..796d59f 100644 --- a/include/net.h +++ b/include/net.h @@ -454,8 +454,10 @@ extern int NetCksumOk(uchar *, int); /* Return true if cksum OK */ extern uint NetCksum(uchar *, int); /* Calculate the checksum */
/* Callbacks */ -extern rxhand_f *NetGetHandler(void); /* Get RX packet handler */ -extern void NetSetHandler(rxhand_f *); /* Set RX packet handler */ +extern rxhand_f *net_get_udp_handler(void); /* Get UDP RX packet handler */ +extern void net_set_udp_handler(rxhand_f *); /* Set UDP RX packet handler */ +extern rxhand_f *net_get_arp_handler(void); /* Get ARP RX packet handler */ +extern void net_set_arp_handler(rxhand_f *); /* Set ARP RX packet handler */ extern void net_set_icmp_handler(rxhand_icmp_f *f); /* Set ICMP RX handler */ extern void NetSetTimeout(ulong, thand_f *);/* Set timeout handler */
@@ -475,7 +477,8 @@ static inline void NetSendPacket(uchar *pkt, int len) (void) eth_send(pkt, len); }
-/* Transmit UDP packet, performing ARP request if needed */ +/* Transmit UDP packet, performing ARP request if needed + (ether will be populated) */ extern int NetSendUDPPacket(uchar *ether, IPaddr_t dest, int dport, int sport, int payload_len);
diff --git a/net/arp.c b/net/arp.c index 90a6b01..90bcc09 100644 --- a/net/arp.c +++ b/net/arp.c @@ -192,9 +192,9 @@ void ArpReceive(struct Ethernet_hdr *et, struct IP_UDP_hdr *ip, int len) memcpy(NetArpWaitPacketMAC, &arp->ar_sha, ARP_HLEN);
-#ifdef CONFIG_NETCONSOLE - NetGetHandler()(0, 0, 0, 0, 0); -#endif + net_get_arp_handler()((uchar *)arp, 0, + reply_ip_addr, 0, len); + /* modify header, and transmit it */ memcpy(((struct Ethernet_hdr *)NetArpWaitTxPacket)-> et_dest, NetArpWaitPacketMAC, ARP_HLEN); diff --git a/net/bootp.c b/net/bootp.c index 2eeb14e..0cf8beb 100644 --- a/net/bootp.c +++ b/net/bootp.c @@ -671,9 +671,9 @@ BootpRequest(void)
#if defined(CONFIG_CMD_DHCP) dhcp_state = SELECTING; - NetSetHandler(DhcpHandler); + net_set_udp_handler(DhcpHandler); #else - NetSetHandler(BootpHandler); + net_set_udp_handler(BootpHandler); #endif NetSendPacket(NetTxPacket, pktlen); } diff --git a/net/cdp.c b/net/cdp.c index f1c1c14..3b8a9a1 100644 --- a/net/cdp.c +++ b/net/cdp.c @@ -237,13 +237,6 @@ CDPTimeout(void) net_set_state(NETLOOP_SUCCESS); }
-static void -CDPDummyHandler(uchar *pkt, unsigned dest, IPaddr_t sip, unsigned src, - unsigned len) -{ - /* nothing */ -} - void CDPReceive(const uchar *pkt, unsigned len) { @@ -368,7 +361,6 @@ CDPStart(void) CDPApplianceVLAN = htons(-1);
NetSetTimeout(CDP_TIMEOUT, CDPTimeout); - NetSetHandler(CDPDummyHandler);
CDPSendTrigger(); } diff --git a/net/dns.c b/net/dns.c index cc0aa0a..ff9ddff 100644 --- a/net/dns.c +++ b/net/dns.c @@ -200,7 +200,7 @@ DnsStart(void) debug("%s\n", __func__);
NetSetTimeout(DNS_TIMEOUT, DnsTimeout); - NetSetHandler(DnsHandler); + net_set_udp_handler(DnsHandler);
DnsSend(); } diff --git a/net/net.c b/net/net.c index 8076e5f..cac9406 100644 --- a/net/net.c +++ b/net/net.c @@ -178,10 +178,13 @@ uchar PktBuf[(PKTBUFSRX+1) * PKTSIZE_ALIGN + PKTALIGN]; /* Receive packet */ uchar *NetRxPackets[PKTBUFSRX];
-/* Current RX packet handler */ -static rxhand_f *packetHandler; +/* Current UDP RX packet handler */ +static rxhand_f *udp_packet_handler; +/* Current ARP RX packet handler */ +static rxhand_f *arp_packet_handler; #ifdef CONFIG_CMD_TFTPPUT -static rxhand_icmp_f *packet_icmp_handler; /* Current ICMP rx handler */ +/* Current ICMP rx handler */ +static rxhand_icmp_f *packet_icmp_handler; #endif /* Current timeout handler */ static thand_f *timeHandler; @@ -252,6 +255,18 @@ static void NetInitLoop(enum proto_t protocol) return; }
+static void net_clear_handlers(void) +{ + net_set_udp_handler(NULL); + net_set_arp_handler(NULL); + NetSetTimeout(0, NULL); +} + +static void net_cleanup_loop(void) +{ + net_clear_handlers(); +} + /**********************************************************************/ /* * Main network processing loop. @@ -259,6 +274,7 @@ static void NetInitLoop(enum proto_t protocol)
int NetLoop(enum proto_t protocol) { + int i; bd_t *bd = gd->bd; int ret = -1;
@@ -269,17 +285,15 @@ int NetLoop(enum proto_t protocol) NetTryCount = 1;
ArpInit(); + net_clear_handlers();
- if (!NetTxPacket) { - int i; - /* - * Setup packet buffers, aligned correctly. - */ - NetTxPacket = &PktBuf[0] + (PKTALIGN - 1); - NetTxPacket -= (ulong)NetTxPacket % PKTALIGN; - for (i = 0; i < PKTBUFSRX; i++) - NetRxPackets[i] = NetTxPacket + (i+1)*PKTSIZE_ALIGN; - } + /* + * Setup packet buffers, aligned correctly. + */ + NetTxPacket = &PktBuf[0] + (PKTALIGN - 1); + NetTxPacket -= (ulong)NetTxPacket % PKTALIGN; + for (i = 0; i < PKTBUFSRX; i++) + NetRxPackets[i] = NetTxPacket + (i+1)*PKTSIZE_ALIGN;
bootstage_mark_name(BOOTSTAGE_ID_ETH_START, "eth_start"); eth_halt(); @@ -418,6 +432,7 @@ restart: * Abort if ctrl-c was pressed. */ if (ctrlc()) { + net_cleanup_loop(); eth_halt(); puts("\nAbort\n"); goto done; @@ -460,6 +475,7 @@ restart: goto restart;
case NETLOOP_SUCCESS: + net_cleanup_loop(); if (NetBootFileXferSize > 0) { char buf[20]; printf("Bytes transferred = %ld (%lx hex)\n", @@ -476,6 +492,7 @@ restart: goto done;
case NETLOOP_FAIL: + net_cleanup_loop(); goto done;
case NETLOOP_CONTINUE: @@ -486,7 +503,7 @@ restart: done: #ifdef CONFIG_CMD_TFTPPUT /* Clear out the handlers */ - NetSetHandler(NULL); + net_set_upd_handler(NULL); net_set_icmp_handler(NULL); #endif return ret; @@ -500,13 +517,6 @@ startAgainTimeout(void) net_set_state(NETLOOP_RESTART); }
-static void -startAgainHandler(uchar *pkt, unsigned dest, IPaddr_t sip, - unsigned src, unsigned len) -{ - /* Totally ignore the packet */ -} - void NetStartAgain(void) { char *nretry; @@ -543,7 +553,7 @@ void NetStartAgain(void) NetRestartWrap = 0; if (NetDevExists) { NetSetTimeout(10000UL, startAgainTimeout); - NetSetHandler(startAgainHandler); + net_set_udp_handler(NULL); } else { net_set_state(NETLOOP_FAIL); } @@ -557,17 +567,36 @@ void NetStartAgain(void) * Miscelaneous bits. */
-rxhand_f * -NetGetHandler(void) +static void dummy_handler(uchar *pkt, unsigned dport, + IPaddr_t sip, unsigned sport, + unsigned len) { - return packetHandler; }
+rxhand_f *net_get_udp_handler(void) +{ + return udp_packet_handler; +}
-void -NetSetHandler(rxhand_f *f) +void net_set_udp_handler(rxhand_f *f) +{ + if (f == NULL) + udp_packet_handler = dummy_handler; + else + udp_packet_handler = f; +} + +rxhand_f *net_get_arp_handler(void) { - packetHandler = f; + return arp_packet_handler; +} + +void net_set_arp_handler(rxhand_f *f) +{ + if (f == NULL) + arp_packet_handler = dummy_handler; + else + arp_packet_handler = f; }
#ifdef CONFIG_CMD_TFTPPUT @@ -1094,11 +1123,11 @@ NetReceive(uchar *inpkt, int len) /* * IP header OK. Pass the packet to the current handler. */ - (*packetHandler)((uchar *)ip + IP_UDP_HDR_SIZE, - ntohs(ip->udp_dst), - src_ip, - ntohs(ip->udp_src), - ntohs(ip->udp_len) - UDP_HDR_SIZE); + (*udp_packet_handler)((uchar *)ip + IP_UDP_HDR_SIZE, + ntohs(ip->udp_dst), + src_ip, + ntohs(ip->udp_src), + ntohs(ip->udp_len) - UDP_HDR_SIZE); break; } } diff --git a/net/nfs.c b/net/nfs.c index 4f2041c..acae4de 100644 --- a/net/nfs.c +++ b/net/nfs.c @@ -735,7 +735,7 @@ NfsStart(void) "Loading: *\b", load_addr);
NetSetTimeout(NFS_TIMEOUT, NfsTimeout); - NetSetHandler(NfsHandler); + net_set_udp_handler(NfsHandler);
NfsTimeoutCount = 0; NfsState = STATE_PRCLOOKUP_PROG_MOUNT_REQ; diff --git a/net/sntp.c b/net/sntp.c index 2adcc6f..73ade8b 100644 --- a/net/sntp.c +++ b/net/sntp.c @@ -85,7 +85,7 @@ SntpStart(void) debug("%s\n", __func__);
NetSetTimeout(SNTP_TIMEOUT, SntpTimeout); - NetSetHandler(SntpHandler); + net_set_udp_handler(SntpHandler); memset(NetServerEther, 0, 6);
SntpSend(); diff --git a/net/tftp.c b/net/tftp.c index bf32eab..b2e08b4 100644 --- a/net/tftp.c +++ b/net/tftp.c @@ -778,7 +778,7 @@ void TftpStart(enum proto_t protocol) TftpTimeoutCountMax = TftpRRQTimeoutCountMax;
NetSetTimeout(TftpTimeoutMSecs, TftpTimeout); - NetSetHandler(TftpHandler); + net_set_udp_handler(TftpHandler); #ifdef CONFIG_CMD_TFTPPUT net_set_icmp_handler(icmp_handler); #endif @@ -840,7 +840,7 @@ TftpStartServer(void) #endif
TftpState = STATE_RECV_WRQ; - NetSetHandler(TftpHandler); + net_set_udp_handler(TftpHandler); } #endif /* CONFIG_CMD_TFTPSRV */

Hi Joe,
On Tue, Mar 27, 2012 at 4:42 PM, Joe Hershberger joe.hershberger@ni.com wrote:
Call a built-in dummy if none is registered... don't require protocols to register a handler (eliminating dummies) NetConsole now uses the ARP handler when waiting on arp (instead of needing a #define hack in arp.c) Clear handlers at the end of net loop
Signed-off-by: Joe Hershberger joe.hershberger@ni.com Cc: Joe Hershberger joe.hershberger@gmail.com Cc: Simon Glass sjg@chromium.org Cc: Mike Frysinger vapier@gentoo.org
Couple of questions, but not important.
Acked-by:: Simon Glass sjg@chromium.org
Changes for v2: - Change NULL checks into dummy functions - Must now call net_set_*_handler(NULL); in init to set dummies - Eliminate CamelCase for new functions
drivers/net/netconsole.c | 4 +- include/net.h | 9 +++- net/arp.c | 6 +- net/bootp.c | 4 +- net/cdp.c | 8 ---- net/dns.c | 2 +- net/net.c | 95 ++++++++++++++++++++++++++++++---------------- net/nfs.c | 2 +- net/sntp.c | 2 +- net/tftp.c | 4 +- 10 files changed, 80 insertions(+), 56 deletions(-)
diff --git a/drivers/net/netconsole.c b/drivers/net/netconsole.c index 28bb955..744f4d1 100644 --- a/drivers/net/netconsole.c +++ b/drivers/net/netconsole.c @@ -63,12 +63,12 @@ void NcStart(void) { if (!output_packet_len || memcmp(nc_ether, NetEtherNullAddr, 6)) { /* going to check for input packet */
- NetSetHandler(nc_handler);
- net_set_udp_handler(nc_handler);
NetSetTimeout(net_timeout, nc_timeout); } else { /* send arp request */ uchar *pkt;
- NetSetHandler(nc_wait_arp_handler);
- net_set_arp_handler(nc_wait_arp_handler);
pkt = (uchar *)NetTxPacket + NetEthHdrSize() + IP_UDP_HDR_SIZE; memcpy(pkt, output_packet, output_packet_len); NetSendUDPPacket(nc_ether, nc_ip, nc_port, nc_port, diff --git a/include/net.h b/include/net.h index c33cd28..796d59f 100644 --- a/include/net.h +++ b/include/net.h @@ -454,8 +454,10 @@ extern int NetCksumOk(uchar *, int); /* Return true if cksum OK */ extern uint NetCksum(uchar *, int); /* Calculate the checksum */
/* Callbacks */ -extern rxhand_f *NetGetHandler(void); /* Get RX packet handler */ -extern void NetSetHandler(rxhand_f *); /* Set RX packet handler */ +extern rxhand_f *net_get_udp_handler(void); /* Get UDP RX packet handler */ +extern void net_set_udp_handler(rxhand_f *); /* Set UDP RX packet handler */ +extern rxhand_f *net_get_arp_handler(void); /* Get ARP RX packet handler */ +extern void net_set_arp_handler(rxhand_f *); /* Set ARP RX packet handler */ extern void net_set_icmp_handler(rxhand_icmp_f *f); /* Set ICMP RX handler */ extern void NetSetTimeout(ulong, thand_f *);/* Set timeout handler */
@@ -475,7 +477,8 @@ static inline void NetSendPacket(uchar *pkt, int len) (void) eth_send(pkt, len); }
-/* Transmit UDP packet, performing ARP request if needed */ +/* Transmit UDP packet, performing ARP request if needed
- (ether will be populated) */
comment style
extern int NetSendUDPPacket(uchar *ether, IPaddr_t dest, int dport, int sport, int payload_len);
diff --git a/net/arp.c b/net/arp.c index 90a6b01..90bcc09 100644 --- a/net/arp.c +++ b/net/arp.c @@ -192,9 +192,9 @@ void ArpReceive(struct Ethernet_hdr *et, struct IP_UDP_hdr *ip, int len) memcpy(NetArpWaitPacketMAC, &arp->ar_sha, ARP_HLEN);
-#ifdef CONFIG_NETCONSOLE
- NetGetHandler()(0, 0, 0, 0, 0);
-#endif
- net_get_arp_handler()((uchar *)arp, 0,
- reply_ip_addr, 0, len);
I presume this is too long for one line?
/* modify header, and transmit it */ memcpy(((struct Ethernet_hdr *)NetArpWaitTxPacket)-> et_dest, NetArpWaitPacketMAC, ARP_HLEN); diff --git a/net/bootp.c b/net/bootp.c index 2eeb14e..0cf8beb 100644 --- a/net/bootp.c +++ b/net/bootp.c @@ -671,9 +671,9 @@ BootpRequest(void)
#if defined(CONFIG_CMD_DHCP) dhcp_state = SELECTING;
- NetSetHandler(DhcpHandler);
- net_set_udp_handler(DhcpHandler);
#else
- NetSetHandler(BootpHandler);
- net_set_udp_handler(BootpHandler);
#endif NetSendPacket(NetTxPacket, pktlen); } diff --git a/net/cdp.c b/net/cdp.c index f1c1c14..3b8a9a1 100644 --- a/net/cdp.c +++ b/net/cdp.c @@ -237,13 +237,6 @@ CDPTimeout(void) net_set_state(NETLOOP_SUCCESS); }
-static void -CDPDummyHandler(uchar *pkt, unsigned dest, IPaddr_t sip, unsigned src,
- unsigned len)
-{
- /* nothing */
-}
void CDPReceive(const uchar *pkt, unsigned len) { @@ -368,7 +361,6 @@ CDPStart(void) CDPApplianceVLAN = htons(-1);
NetSetTimeout(CDP_TIMEOUT, CDPTimeout);
- NetSetHandler(CDPDummyHandler);
CDPSendTrigger(); } diff --git a/net/dns.c b/net/dns.c index cc0aa0a..ff9ddff 100644 --- a/net/dns.c +++ b/net/dns.c @@ -200,7 +200,7 @@ DnsStart(void) debug("%s\n", __func__);
NetSetTimeout(DNS_TIMEOUT, DnsTimeout);
- NetSetHandler(DnsHandler);
- net_set_udp_handler(DnsHandler);
DnsSend(); } diff --git a/net/net.c b/net/net.c index 8076e5f..cac9406 100644 --- a/net/net.c +++ b/net/net.c @@ -178,10 +178,13 @@ uchar PktBuf[(PKTBUFSRX+1) * PKTSIZE_ALIGN + PKTALIGN]; /* Receive packet */ uchar *NetRxPackets[PKTBUFSRX];
-/* Current RX packet handler */ -static rxhand_f *packetHandler; +/* Current UDP RX packet handler */ +static rxhand_f *udp_packet_handler; +/* Current ARP RX packet handler */ +static rxhand_f *arp_packet_handler; #ifdef CONFIG_CMD_TFTPPUT -static rxhand_icmp_f *packet_icmp_handler; /* Current ICMP rx handler */ +/* Current ICMP rx handler */ +static rxhand_icmp_f *packet_icmp_handler; #endif /* Current timeout handler */ static thand_f *timeHandler; @@ -252,6 +255,18 @@ static void NetInitLoop(enum proto_t protocol) return; }
+static void net_clear_handlers(void) +{
- net_set_udp_handler(NULL);
- net_set_arp_handler(NULL);
- NetSetTimeout(0, NULL);
+}
+static void net_cleanup_loop(void) +{
- net_clear_handlers();
If this is static anyway, and net_clear_handlers() also clear the timeout, why not just put the three lines of code in here instead of a separate function?
+}
/**********************************************************************/ /* * Main network processing loop. @@ -259,6 +274,7 @@ static void NetInitLoop(enum proto_t protocol)
int NetLoop(enum proto_t protocol) {
- int i;
bd_t *bd = gd->bd; int ret = -1;
@@ -269,17 +285,15 @@ int NetLoop(enum proto_t protocol) NetTryCount = 1;
ArpInit();
- net_clear_handlers();
- if (!NetTxPacket) {
- int i;
- /*
- * Setup packet buffers, aligned correctly.
- */
- NetTxPacket = &PktBuf[0] + (PKTALIGN - 1);
- NetTxPacket -= (ulong)NetTxPacket % PKTALIGN;
- for (i = 0; i < PKTBUFSRX; i++)
- NetRxPackets[i] = NetTxPacket + (i+1)*PKTSIZE_ALIGN;
- }
- /*
- * Setup packet buffers, aligned correctly.
- */
- NetTxPacket = &PktBuf[0] + (PKTALIGN - 1);
- NetTxPacket -= (ulong)NetTxPacket % PKTALIGN;
- for (i = 0; i < PKTBUFSRX; i++)
- NetRxPackets[i] = NetTxPacket + (i+1)*PKTSIZE_ALIGN;
bootstage_mark_name(BOOTSTAGE_ID_ETH_START, "eth_start"); eth_halt(); @@ -418,6 +432,7 @@ restart: * Abort if ctrl-c was pressed. */ if (ctrlc()) {
- net_cleanup_loop();
eth_halt(); puts("\nAbort\n"); goto done; @@ -460,6 +475,7 @@ restart: goto restart;
case NETLOOP_SUCCESS:
- net_cleanup_loop();
if (NetBootFileXferSize > 0) { char buf[20]; printf("Bytes transferred = %ld (%lx hex)\n", @@ -476,6 +492,7 @@ restart: goto done;
case NETLOOP_FAIL:
- net_cleanup_loop();
goto done;
case NETLOOP_CONTINUE: @@ -486,7 +503,7 @@ restart: done: #ifdef CONFIG_CMD_TFTPPUT /* Clear out the handlers */
- NetSetHandler(NULL);
- net_set_upd_handler(NULL);
net_set_icmp_handler(NULL); #endif return ret; @@ -500,13 +517,6 @@ startAgainTimeout(void) net_set_state(NETLOOP_RESTART); }
-static void -startAgainHandler(uchar *pkt, unsigned dest, IPaddr_t sip,
- unsigned src, unsigned len)
-{
- /* Totally ignore the packet */
-}
void NetStartAgain(void) { char *nretry; @@ -543,7 +553,7 @@ void NetStartAgain(void) NetRestartWrap = 0; if (NetDevExists) { NetSetTimeout(10000UL, startAgainTimeout);
- NetSetHandler(startAgainHandler);
- net_set_udp_handler(NULL);
} else { net_set_state(NETLOOP_FAIL); } @@ -557,17 +567,36 @@ void NetStartAgain(void) * Miscelaneous bits. */
-rxhand_f * -NetGetHandler(void) +static void dummy_handler(uchar *pkt, unsigned dport,
- IPaddr_t sip, unsigned sport,
- unsigned len)
{
- return packetHandler;
}
+rxhand_f *net_get_udp_handler(void) +{
- return udp_packet_handler;
+}
-void -NetSetHandler(rxhand_f *f) +void net_set_udp_handler(rxhand_f *f) +{
- if (f == NULL)
- udp_packet_handler = dummy_handler;
- else
- udp_packet_handler = f;
+}
+rxhand_f *net_get_arp_handler(void) {
- packetHandler = f;
- return arp_packet_handler;
+}
+void net_set_arp_handler(rxhand_f *f) +{
- if (f == NULL)
- arp_packet_handler = dummy_handler;
- else
- arp_packet_handler = f;
}
#ifdef CONFIG_CMD_TFTPPUT @@ -1094,11 +1123,11 @@ NetReceive(uchar *inpkt, int len) /* * IP header OK. Pass the packet to the current handler. */
- (*packetHandler)((uchar *)ip + IP_UDP_HDR_SIZE,
- ntohs(ip->udp_dst),
- src_ip,
- ntohs(ip->udp_src),
- ntohs(ip->udp_len) - UDP_HDR_SIZE);
- (*udp_packet_handler)((uchar *)ip + IP_UDP_HDR_SIZE,
- ntohs(ip->udp_dst),
- src_ip,
- ntohs(ip->udp_src),
- ntohs(ip->udp_len) - UDP_HDR_SIZE);
break; } } diff --git a/net/nfs.c b/net/nfs.c index 4f2041c..acae4de 100644 --- a/net/nfs.c +++ b/net/nfs.c @@ -735,7 +735,7 @@ NfsStart(void) "Loading: *\b", load_addr);
NetSetTimeout(NFS_TIMEOUT, NfsTimeout);
- NetSetHandler(NfsHandler);
- net_set_udp_handler(NfsHandler);
NfsTimeoutCount = 0; NfsState = STATE_PRCLOOKUP_PROG_MOUNT_REQ; diff --git a/net/sntp.c b/net/sntp.c index 2adcc6f..73ade8b 100644 --- a/net/sntp.c +++ b/net/sntp.c @@ -85,7 +85,7 @@ SntpStart(void) debug("%s\n", __func__);
NetSetTimeout(SNTP_TIMEOUT, SntpTimeout);
- NetSetHandler(SntpHandler);
- net_set_udp_handler(SntpHandler);
memset(NetServerEther, 0, 6);
SntpSend(); diff --git a/net/tftp.c b/net/tftp.c index bf32eab..b2e08b4 100644 --- a/net/tftp.c +++ b/net/tftp.c @@ -778,7 +778,7 @@ void TftpStart(enum proto_t protocol) TftpTimeoutCountMax = TftpRRQTimeoutCountMax;
NetSetTimeout(TftpTimeoutMSecs, TftpTimeout);
- NetSetHandler(TftpHandler);
- net_set_udp_handler(TftpHandler);
#ifdef CONFIG_CMD_TFTPPUT net_set_icmp_handler(icmp_handler); #endif @@ -840,7 +840,7 @@ TftpStartServer(void) #endif
TftpState = STATE_RECV_WRQ;
- NetSetHandler(TftpHandler);
- net_set_udp_handler(TftpHandler);
} #endif /* CONFIG_CMD_TFTPSRV */
-- 1.6.0.2
Regards, Simon

When the network is VLAN or SNAP, NetUpdateEther() will preserve the original Ethernet packet header and simply replace the src and dest MACs and the protocol
Signed-off-by: Joe Hershberger joe.hershberger@ni.com Cc: Joe Hershberger joe.hershberger@gmail.com Cc: Simon Glass sjg@chromium.org Cc: Mike Frysinger vapier@gentoo.org --- include/net.h | 1 + net/arp.c | 2 +- net/net.c | 23 +++++++++++++++++++++++ net/ping.c | 10 +++++----- 4 files changed, 30 insertions(+), 6 deletions(-)
diff --git a/include/net.h b/include/net.h index 796d59f..33570c7 100644 --- a/include/net.h +++ b/include/net.h @@ -443,6 +443,7 @@ extern int NetEthHdrSize(void);
/* Set ethernet header; returns the size of the header */ extern int NetSetEther(uchar *, uchar *, uint); +extern int net_update_ether(struct Ethernet_hdr *et, uchar *addr, uint prot);
/* Set IP header */ extern void net_set_ip_header(uchar *pkt, IPaddr_t dest, IPaddr_t source); diff --git a/net/arp.c b/net/arp.c index 90bcc09..8d5ac20 100644 --- a/net/arp.c +++ b/net/arp.c @@ -158,7 +158,7 @@ void ArpReceive(struct Ethernet_hdr *et, struct IP_UDP_hdr *ip, int len) /* reply with our IP address */ debug("Got ARP REQUEST, return our IP\n"); pkt = (uchar *)et; - eth_hdr_size = NetSetEther(pkt, et->et_src, PROT_ARP); + eth_hdr_size = net_update_ether(et, et->et_src, PROT_ARP); pkt += eth_hdr_size; arp->ar_op = htons(ARPOP_REPLY); memcpy(&arp->ar_tha, &arp->ar_sha, ARP_HLEN); diff --git a/net/net.c b/net/net.c index cac9406..0a35858 100644 --- a/net/net.c +++ b/net/net.c @@ -1279,6 +1279,29 @@ NetSetEther(uchar *xet, uchar * addr, uint prot) } }
+int net_update_ether(struct Ethernet_hdr *et, uchar *addr, uint prot) +{ + ushort protlen; + + memcpy(et->et_dest, addr, 6); + memcpy(et->et_src, NetOurEther, 6); + protlen = ntohs(et->et_protlen); + if (protlen == PROT_VLAN) { + struct VLAN_Ethernet_hdr *vet = + (struct VLAN_Ethernet_hdr *)et; + vet->vet_type = htons(prot); + return VLAN_ETHER_HDR_SIZE; + } else if (protlen > 1514) { + et->et_protlen = htons(prot); + return ETHER_HDR_SIZE; + } else { + /* 802.2 + SNAP */ + struct E802_hdr *et802 = (struct E802_hdr *)et; + et802->et_prot = htons(prot); + return E802_HDR_SIZE; + } +} + void net_set_ip_header(uchar *pkt, IPaddr_t dest, IPaddr_t source) { struct IP_UDP_hdr *ip = (struct IP_UDP_hdr *)pkt; diff --git a/net/ping.c b/net/ping.c index 6cdcdf0..09935b3 100644 --- a/net/ping.c +++ b/net/ping.c @@ -87,6 +87,7 @@ void ping_receive(struct Ethernet_hdr *et, struct IP_UDP_hdr *ip, int len) { struct ICMP_hdr *icmph = (struct ICMP_hdr *)&(ip->udp_src); IPaddr_t src_ip; + int eth_hdr_size;
switch (icmph->type) { case ICMP_ECHO_REPLY: @@ -95,11 +96,10 @@ void ping_receive(struct Ethernet_hdr *et, struct IP_UDP_hdr *ip, int len) net_set_state(NETLOOP_SUCCESS); return; case ICMP_ECHO_REQUEST: - debug("Got ICMP ECHO REQUEST, return " - "%d bytes\n", ETHER_HDR_SIZE + len); + eth_hdr_size = net_update_ether(et, et->et_src, PROT_IP);
- memcpy(&et->et_dest[0], &et->et_src[0], 6); - memcpy(&et->et_src[0], NetOurEther, 6); + debug("Got ICMP ECHO REQUEST, return " + "%d bytes\n", eth_hdr_size + len);
ip->ip_sum = 0; ip->ip_off = 0; @@ -112,7 +112,7 @@ void ping_receive(struct Ethernet_hdr *et, struct IP_UDP_hdr *ip, int len) icmph->checksum = 0; icmph->checksum = ~NetCksum((uchar *)icmph, (len - IP_HDR_SIZE) >> 1); - NetSendPacket((uchar *)et, ETHER_HDR_SIZE + len); + NetSendPacket((uchar *)et, eth_hdr_size + len); return; /* default: return;*/

Hi Joe,
On Tue, Mar 27, 2012 at 4:43 PM, Joe Hershberger joe.hershberger@ni.com wrote:
When the network is VLAN or SNAP, NetUpdateEther() will preserve the original Ethernet packet header and simply replace the src and dest MACs and the protocol
Signed-off-by: Joe Hershberger joe.hershberger@ni.com Cc: Joe Hershberger joe.hershberger@gmail.com Cc: Simon Glass sjg@chromium.org Cc: Mike Frysinger vapier@gentoo.org
Acked-by: Simon Glass sjg@chromium.org
Q below...
include/net.h | 1 + net/arp.c | 2 +- net/net.c | 23 +++++++++++++++++++++++ net/ping.c | 10 +++++----- 4 files changed, 30 insertions(+), 6 deletions(-)
diff --git a/include/net.h b/include/net.h index 796d59f..33570c7 100644 --- a/include/net.h +++ b/include/net.h @@ -443,6 +443,7 @@ extern int NetEthHdrSize(void);
/* Set ethernet header; returns the size of the header */ extern int NetSetEther(uchar *, uchar *, uint); +extern int net_update_ether(struct Ethernet_hdr *et, uchar *addr, uint prot);
/* Set IP header */ extern void net_set_ip_header(uchar *pkt, IPaddr_t dest, IPaddr_t source); diff --git a/net/arp.c b/net/arp.c index 90bcc09..8d5ac20 100644 --- a/net/arp.c +++ b/net/arp.c @@ -158,7 +158,7 @@ void ArpReceive(struct Ethernet_hdr *et, struct IP_UDP_hdr *ip, int len) /* reply with our IP address */ debug("Got ARP REQUEST, return our IP\n"); pkt = (uchar *)et;
- eth_hdr_size = NetSetEther(pkt, et->et_src, PROT_ARP);
- eth_hdr_size = net_update_ether(et, et->et_src, PROT_ARP);
pkt += eth_hdr_size; arp->ar_op = htons(ARPOP_REPLY); memcpy(&arp->ar_tha, &arp->ar_sha, ARP_HLEN); diff --git a/net/net.c b/net/net.c index cac9406..0a35858 100644 --- a/net/net.c +++ b/net/net.c @@ -1279,6 +1279,29 @@ NetSetEther(uchar *xet, uchar * addr, uint prot) } }
+int net_update_ether(struct Ethernet_hdr *et, uchar *addr, uint prot) +{
- ushort protlen;
- memcpy(et->et_dest, addr, 6);
- memcpy(et->et_src, NetOurEther, 6);
- protlen = ntohs(et->et_protlen);
- if (protlen == PROT_VLAN) {
- struct VLAN_Ethernet_hdr *vet =
- (struct VLAN_Ethernet_hdr *)et;
- vet->vet_type = htons(prot);
- return VLAN_ETHER_HDR_SIZE;
- } else if (protlen > 1514) {
- et->et_protlen = htons(prot);
- return ETHER_HDR_SIZE;
- } else {
- /* 802.2 + SNAP */
- struct E802_hdr *et802 = (struct E802_hdr *)et;
- et802->et_prot = htons(prot);
- return E802_HDR_SIZE;
- }
+}
void net_set_ip_header(uchar *pkt, IPaddr_t dest, IPaddr_t source) { struct IP_UDP_hdr *ip = (struct IP_UDP_hdr *)pkt; diff --git a/net/ping.c b/net/ping.c index 6cdcdf0..09935b3 100644 --- a/net/ping.c +++ b/net/ping.c @@ -87,6 +87,7 @@ void ping_receive(struct Ethernet_hdr *et, struct IP_UDP_hdr *ip, int len) { struct ICMP_hdr *icmph = (struct ICMP_hdr *)&(ip->udp_src); IPaddr_t src_ip;
- int eth_hdr_size;
switch (icmph->type) { case ICMP_ECHO_REPLY: @@ -95,11 +96,10 @@ void ping_receive(struct Ethernet_hdr *et, struct IP_UDP_hdr *ip, int len) net_set_state(NETLOOP_SUCCESS); return; case ICMP_ECHO_REQUEST:
- debug("Got ICMP ECHO REQUEST, return "
- "%d bytes\n", ETHER_HDR_SIZE + len);
- eth_hdr_size = net_update_ether(et, et->et_src, PROT_IP);
- memcpy(&et->et_dest[0], &et->et_src[0], 6);
- memcpy(&et->et_src[0], NetOurEther, 6);
- debug("Got ICMP ECHO REQUEST, return "
- "%d bytes\n", eth_hdr_size + len);
Did you mean to move this?
ip->ip_sum = 0; ip->ip_off = 0; @@ -112,7 +112,7 @@ void ping_receive(struct Ethernet_hdr *et, struct IP_UDP_hdr *ip, int len) icmph->checksum = 0; icmph->checksum = ~NetCksum((uchar *)icmph, (len - IP_HDR_SIZE) >> 1);
- NetSendPacket((uchar *)et, ETHER_HDR_SIZE + len);
- NetSendPacket((uchar *)et, eth_hdr_size + len);
return; /* default: return;*/ -- 1.6.0.2
Regards, Simon

Before this patch, bootp would not overwrite the value, but the value was still clobbered in the env
Signed-off-by: Joe Hershberger joe.hershberger@ni.com Cc: Joe Hershberger joe.hershberger@gmail.com Cc: Simon Glass sjg@chromium.org Cc: Mike Frysinger vapier@gentoo.org --- Changes for v2: - Added a comment
common/cmd_net.c | 6 ++++-- 1 files changed, 4 insertions(+), 2 deletions(-)
diff --git a/common/cmd_net.c b/common/cmd_net.c index 65f32bc..97befd9 100644 --- a/common/cmd_net.c +++ b/common/cmd_net.c @@ -153,12 +153,14 @@ static void netboot_update_env (void) ip_to_string (NetOurIP, tmp); setenv ("ipaddr", tmp); } - +#if !defined(CONFIG_BOOTP_SERVERIP) + /* Only attempt to change serverip if net/bootp.c:BootpCopyNetParams() + could have set it */ if (NetServerIP) { ip_to_string (NetServerIP, tmp); setenv ("serverip", tmp); } - +#endif if (NetOurDNSIP) { ip_to_string (NetOurDNSIP, tmp); setenv ("dnsip", tmp);

On Tue, Mar 27, 2012 at 4:43 PM, Joe Hershberger joe.hershberger@ni.com wrote:
Before this patch, bootp would not overwrite the value, but the value was still clobbered in the env
Signed-off-by: Joe Hershberger joe.hershberger@ni.com Cc: Joe Hershberger joe.hershberger@gmail.com Cc: Simon Glass sjg@chromium.org Cc: Mike Frysinger vapier@gentoo.org
Changes for v2: - Added a comment
common/cmd_net.c | 6 ++++-- 1 files changed, 4 insertions(+), 2 deletions(-)
diff --git a/common/cmd_net.c b/common/cmd_net.c index 65f32bc..97befd9 100644 --- a/common/cmd_net.c +++ b/common/cmd_net.c @@ -153,12 +153,14 @@ static void netboot_update_env (void) ip_to_string (NetOurIP, tmp); setenv ("ipaddr", tmp); }
+#if !defined(CONFIG_BOOTP_SERVERIP)
- /* Only attempt to change serverip if net/bootp.c:BootpCopyNetParams()
- could have set it */
Comment style
if (NetServerIP) { ip_to_string (NetServerIP, tmp); setenv ("serverip", tmp); }
+#endif if (NetOurDNSIP) { ip_to_string (NetOurDNSIP, tmp); setenv ("dnsip", tmp); -- 1.6.0.2

If CONFIG_BOOTP_SERVERIP is not defined, unused variable warning is reported. This was fixed in origin using a compiler feature instead of a simple reorder of the statements.
Signed-off-by: Joe Hershberger joe.hershberger@ni.com Cc: Joe Hershberger joe.hershberger@gmail.com Cc: Simon Glass sjg@chromium.org Cc: Mike Frysinger vapier@gentoo.org --- net/bootp.c | 7 +++---- 1 files changed, 3 insertions(+), 4 deletions(-)
diff --git a/net/bootp.c b/net/bootp.c index 0cf8beb..650ae4d 100644 --- a/net/bootp.c +++ b/net/bootp.c @@ -20,7 +20,6 @@ #ifdef CONFIG_STATUS_LED #include <status_led.h> #endif -#include <linux/compiler.h>
#define BOOTP_VENDOR_MAGIC 0x63825363 /* RFC1048 Magic Cookie */
@@ -98,15 +97,15 @@ static int BootpCheckPkt(uchar *pkt, unsigned dest, unsigned src, unsigned len) */ static void BootpCopyNetParams(struct Bootp_t *bp) { - __maybe_unused IPaddr_t tmp_ip; - - NetCopyIP(&NetOurIP, &bp->bp_yiaddr); #if !defined(CONFIG_BOOTP_SERVERIP) + IPaddr_t tmp_ip; + NetCopyIP(&tmp_ip, &bp->bp_siaddr); if (tmp_ip != 0) NetCopyIP(&NetServerIP, &bp->bp_siaddr); memcpy(NetServerEther, ((struct Ethernet_hdr *)NetRxPacket)->et_src, 6); #endif + NetCopyIP(&NetOurIP, &bp->bp_yiaddr); if (strlen(bp->bp_file) > 0) copy_filename(BootFile, bp->bp_file, sizeof(BootFile));

Hi Joe,
On Tue, Mar 27, 2012 at 4:43 PM, Joe Hershberger joe.hershberger@ni.com wrote:
If CONFIG_BOOTP_SERVERIP is not defined, unused variable warning is reported. This was fixed in origin using a compiler feature instead of a simple reorder of the statements.
What do you mean by 'origin'? You can use more columns in your commit messages.
Signed-off-by: Joe Hershberger joe.hershberger@ni.com Cc: Joe Hershberger joe.hershberger@gmail.com Cc: Simon Glass sjg@chromium.org Cc: Mike Frysinger vapier@gentoo.org
With commit message fixed:
Acked-by: Simon Glass sjg@chromium.org
net/bootp.c | 7 +++---- 1 files changed, 3 insertions(+), 4 deletions(-)
diff --git a/net/bootp.c b/net/bootp.c index 0cf8beb..650ae4d 100644 --- a/net/bootp.c +++ b/net/bootp.c @@ -20,7 +20,6 @@ #ifdef CONFIG_STATUS_LED #include <status_led.h> #endif -#include <linux/compiler.h>
#define BOOTP_VENDOR_MAGIC 0x63825363 /* RFC1048 Magic Cookie */
@@ -98,15 +97,15 @@ static int BootpCheckPkt(uchar *pkt, unsigned dest, unsigned src, unsigned len) */ static void BootpCopyNetParams(struct Bootp_t *bp) {
- __maybe_unused IPaddr_t tmp_ip;
- NetCopyIP(&NetOurIP, &bp->bp_yiaddr);
#if !defined(CONFIG_BOOTP_SERVERIP)
- IPaddr_t tmp_ip;
NetCopyIP(&tmp_ip, &bp->bp_siaddr); if (tmp_ip != 0) NetCopyIP(&NetServerIP, &bp->bp_siaddr); memcpy(NetServerEther, ((struct Ethernet_hdr *)NetRxPacket)->et_src, 6); #endif
- NetCopyIP(&NetOurIP, &bp->bp_yiaddr);
if (strlen(bp->bp_file) > 0) copy_filename(BootFile, bp->bp_file, sizeof(BootFile));
-- 1.6.0.2
Regards, Simon

This is useful if you want to look for a DHCP server, but try some other settings if not available.
Signed-off-by: Joe Hershberger joe.hershberger@ni.com Cc: Joe Hershberger joe.hershberger@gmail.com Cc: Simon Glass sjg@chromium.org Cc: Mike Frysinger vapier@gentoo.org --- Changes for v2: - Renamed to CONFIG_BOOTP_MAY_FAIL
README | 7 +++++++ net/bootp.c | 5 +++++ 2 files changed, 12 insertions(+), 0 deletions(-)
diff --git a/README b/README index b69a3b6..ed8c79f 100644 --- a/README +++ b/README @@ -1563,10 +1563,17 @@ The following options need to be configured: CONFIG_BOOTP_NTPSERVER CONFIG_BOOTP_TIMEOFFSET CONFIG_BOOTP_VENDOREX + CONFIG_BOOTP_MAY_FAIL
CONFIG_BOOTP_SERVERIP - TFTP server will be the serverip environment variable, not the BOOTP server.
+ CONFIG_BOOTP_MAY_FAIL - If the DHCP server is not found + after the configured retry count, the call will fail + instead of starting over. This can be used to fail over + to Link-local IP address configuration if the DHCP server + is not available. + CONFIG_BOOTP_DNS2 - If a DHCP client requests the DNS serverip from a DHCP server, it is possible that more than one DNS serverip is offered to the client. diff --git a/net/bootp.c b/net/bootp.c index 650ae4d..d945bdd 100644 --- a/net/bootp.c +++ b/net/bootp.c @@ -328,8 +328,13 @@ static void BootpTimeout(void) { if (BootpTry >= TIMEOUT_COUNT) { +#ifdef CONFIG_BOOTP_MAY_FAIL + puts("\nRetry count exceeded\n"); + NetSetState(NETLOOP_FAIL); +#else puts("\nRetry count exceeded; starting again\n"); NetStartAgain(); +#endif } else { NetSetTimeout(TIMEOUT, BootpTimeout); BootpRequest();

On Tue, Mar 27, 2012 at 4:43 PM, Joe Hershberger joe.hershberger@ni.com wrote:
This is useful if you want to look for a DHCP server, but try some other settings if not available.
Signed-off-by: Joe Hershberger joe.hershberger@ni.com Cc: Joe Hershberger joe.hershberger@gmail.com Cc: Simon Glass sjg@chromium.org Cc: Mike Frysinger vapier@gentoo.org
Looks ok to me
Acked-by: Simon Glass sjg@chromium.org
Changes for v2: - Renamed to CONFIG_BOOTP_MAY_FAIL
README | 7 +++++++ net/bootp.c | 5 +++++ 2 files changed, 12 insertions(+), 0 deletions(-)
diff --git a/README b/README index b69a3b6..ed8c79f 100644 --- a/README +++ b/README @@ -1563,10 +1563,17 @@ The following options need to be configured: CONFIG_BOOTP_NTPSERVER CONFIG_BOOTP_TIMEOFFSET CONFIG_BOOTP_VENDOREX
- CONFIG_BOOTP_MAY_FAIL
CONFIG_BOOTP_SERVERIP - TFTP server will be the serverip environment variable, not the BOOTP server.
- CONFIG_BOOTP_MAY_FAIL - If the DHCP server is not found
- after the configured retry count, the call will fail
- instead of starting over. This can be used to fail over
- to Link-local IP address configuration if the DHCP server
- is not available.
CONFIG_BOOTP_DNS2 - If a DHCP client requests the DNS serverip from a DHCP server, it is possible that more than one DNS serverip is offered to the client. diff --git a/net/bootp.c b/net/bootp.c index 650ae4d..d945bdd 100644 --- a/net/bootp.c +++ b/net/bootp.c @@ -328,8 +328,13 @@ static void BootpTimeout(void) { if (BootpTry >= TIMEOUT_COUNT) { +#ifdef CONFIG_BOOTP_MAY_FAIL
- puts("\nRetry count exceeded\n");
- NetSetState(NETLOOP_FAIL);
+#else puts("\nRetry count exceeded; starting again\n"); NetStartAgain(); +#endif } else { NetSetTimeout(TIMEOUT, BootpTimeout); BootpRequest(); -- 1.6.0.2

Don't force ARP clients to return the MAC address if they don't care (such as ping)
Signed-off-by: Joe Hershberger joe.hershberger@ni.com Cc: Joe Hershberger joe.hershberger@gmail.com Cc: Simon Glass sjg@chromium.org Cc: Mike Frysinger vapier@gentoo.org --- net/arp.c | 9 +++++---- net/ping.c | 10 +++------- 2 files changed, 8 insertions(+), 11 deletions(-)
diff --git a/net/arp.c b/net/arp.c index 8d5ac20..7d0297d 100644 --- a/net/arp.c +++ b/net/arp.c @@ -170,7 +170,7 @@ void ArpReceive(struct Ethernet_hdr *et, struct IP_UDP_hdr *ip, int len)
case ARPOP_REPLY: /* arp reply */ /* are we waiting for a reply */ - if (!NetArpWaitPacketIP || !NetArpWaitPacketMAC) + if (!NetArpWaitPacketIP) break;
#ifdef CONFIG_KEEP_SERVERADDR @@ -189,15 +189,16 @@ void ArpReceive(struct Ethernet_hdr *et, struct IP_UDP_hdr *ip, int len) arp->ar_data);
/* save address for later use */ - memcpy(NetArpWaitPacketMAC, - &arp->ar_sha, ARP_HLEN); + if (NetArpWaitPacketMAC != NULL) + memcpy(NetArpWaitPacketMAC, + &arp->ar_sha, ARP_HLEN);
net_get_arp_handler()((uchar *)arp, 0, reply_ip_addr, 0, len);
/* modify header, and transmit it */ memcpy(((struct Ethernet_hdr *)NetArpWaitTxPacket)-> - et_dest, NetArpWaitPacketMAC, ARP_HLEN); + et_dest, &arp->ar_sha, ARP_HLEN); NetSendPacket(NetArpWaitTxPacket, NetArpWaitTxPacketSize);
diff --git a/net/ping.c b/net/ping.c index 09935b3..50a1047 100644 --- a/net/ping.c +++ b/net/ping.c @@ -40,22 +40,18 @@ static void set_icmp_header(uchar *pkt, IPaddr_t dest)
static int ping_send(void) { - static uchar mac[6]; uchar *pkt; int eth_hdr_size;
/* XXX always send arp request */
- memcpy(mac, NetEtherNullAddr, 6); - debug("sending ARP for %pI4\n", &NetPingIP);
NetArpWaitPacketIP = NetPingIP; - NetArpWaitPacketMAC = mac;
- pkt = NetArpWaitTxPacket; - eth_hdr_size = NetSetEther(pkt, mac, PROT_IP); - pkt += eth_hdr_size; + eth_hdr_size = NetSetEther(NetArpWaitTxPacket, NetEtherNullAddr, + PROT_IP); + pkt = NetArpWaitTxPacket + eth_hdr_size;
set_icmp_header(pkt, NetPingIP);

Signed-off-by: Joe Hershberger joe.hershberger@ni.com Cc: Joe Hershberger joe.hershberger@gmail.com Cc: Simon Glass sjg@chromium.org Cc: Mike Frysinger vapier@gentoo.org --- Changes for v2: - Split from "Fix net buffer initialization"
net/net.c | 4 ++-- 1 files changed, 2 insertions(+), 2 deletions(-)
diff --git a/net/net.c b/net/net.c index 0a35858..2cfb115 100644 --- a/net/net.c +++ b/net/net.c @@ -231,7 +231,7 @@ void net_auto_load(void) TftpStart(TFTPGET); }
-static void NetInitLoop(enum proto_t protocol) +static void NetInitLoop(void) { static int env_changed_id; bd_t *bd = gd->bd; @@ -313,7 +313,7 @@ restart: * here on, this code is a state machine driven by received * packets and timer events. */ - NetInitLoop(protocol); + NetInitLoop();
switch (net_check_prereq(protocol)) { case 1:

On Tue, Mar 27, 2012 at 4:43 PM, Joe Hershberger joe.hershberger@ni.com wrote:
Signed-off-by: Joe Hershberger joe.hershberger@ni.com Cc: Joe Hershberger joe.hershberger@gmail.com Cc: Simon Glass sjg@chromium.org Cc: Mike Frysinger vapier@gentoo.org
Acked-by: Simon Glass sjg@chromium.org
But it is still nice to have a commit message, even if brief.
Changes for v2: - Split from "Fix net buffer initialization"
net/net.c | 4 ++-- 1 files changed, 2 insertions(+), 2 deletions(-)
diff --git a/net/net.c b/net/net.c index 0a35858..2cfb115 100644 --- a/net/net.c +++ b/net/net.c @@ -231,7 +231,7 @@ void net_auto_load(void) TftpStart(TFTPGET); }
-static void NetInitLoop(enum proto_t protocol) +static void NetInitLoop(void) { static int env_changed_id; bd_t *bd = gd->bd; @@ -313,7 +313,7 @@ restart: * here on, this code is a state machine driven by received * packets and timer events. */
- NetInitLoop(protocol);
- NetInitLoop();
switch (net_check_prereq(protocol)) { case 1: -- 1.6.0.2

A new non-static function net_init() will initialize buffers and read from the environment. Only update from the env on each entry to NetLoop().
Signed-off-by: Joe Hershberger joe.hershberger@ni.com Cc: Joe Hershberger joe.hershberger@gmail.com Cc: Simon Glass sjg@chromium.org Cc: Mike Frysinger vapier@gentoo.org --- Changes for v2: - Unadded explicit parameter name in net.h for NetLoop - Eliminate CamelCase from new functions - Changed null pointer print to an assert
include/net.h | 1 + net/net.c | 44 ++++++++++++++++++++++++++++++-------------- 2 files changed, 31 insertions(+), 14 deletions(-)
diff --git a/include/net.h b/include/net.h index 33570c7..2c47604 100644 --- a/include/net.h +++ b/include/net.h @@ -430,6 +430,7 @@ extern IPaddr_t Mcast_addr; #endif
/* Initialize the network adapter */ +extern void net_init(void); extern int NetLoop(enum proto_t);
/* Shutdown adapters and cleanup */ diff --git a/net/net.c b/net/net.c index 2cfb115..01e7b29 100644 --- a/net/net.c +++ b/net/net.c @@ -267,6 +267,30 @@ static void net_cleanup_loop(void) net_clear_handlers(); }
+void net_init(void) +{ + static int first_call = 1; + + if (first_call) { + /* + * Setup packet buffers, aligned correctly. + */ + int i; + NetTxPacket = &PktBuf[0] + (PKTALIGN - 1); + NetTxPacket -= (ulong)NetTxPacket % PKTALIGN; + for (i = 0; i < PKTBUFSRX; i++) + NetRxPackets[i] = NetTxPacket + (i+1)*PKTSIZE_ALIGN; + + ArpInit(); + net_clear_handlers(); + + /* Only need to setup buffer pointers once. */ + first_call = 0; + } + + NetInitLoop(); +} + /**********************************************************************/ /* * Main network processing loop. @@ -274,28 +298,15 @@ static void net_cleanup_loop(void)
int NetLoop(enum proto_t protocol) { - int i; bd_t *bd = gd->bd; int ret = -1;
NetRestarted = 0; NetDevExists = 0; - - NetTxPacket = NULL; NetTryCount = 1;
- ArpInit(); - net_clear_handlers(); - - /* - * Setup packet buffers, aligned correctly. - */ - NetTxPacket = &PktBuf[0] + (PKTALIGN - 1); - NetTxPacket -= (ulong)NetTxPacket % PKTALIGN; - for (i = 0; i < PKTBUFSRX; i++) - NetRxPackets[i] = NetTxPacket + (i+1)*PKTSIZE_ALIGN; - bootstage_mark_name(BOOTSTAGE_ID_ETH_START, "eth_start"); + net_init(); eth_halt(); eth_set_current(); if (eth_init(bd) < 0) { @@ -627,6 +638,11 @@ NetSendUDPPacket(uchar *ether, IPaddr_t dest, int dport, int sport, int eth_hdr_size; int pkt_hdr_size;
+ /* make sure the NetTxPacket is initialized (NetInit() was called) */ + assert(NetTxPacket != NULL); + if (NetTxPacket == NULL) + return -1; + /* convert to new style broadcast */ if (dest == 0) dest = 0xFFFFFFFF;

Hi Joe,
On Tue, Mar 27, 2012 at 4:43 PM, Joe Hershberger joe.hershberger@ni.com wrote:
A new non-static function net_init() will initialize buffers and read from the environment. Only update from the env on each entry to NetLoop().
Signed-off-by: Joe Hershberger joe.hershberger@ni.com Cc: Joe Hershberger joe.hershberger@gmail.com Cc: Simon Glass sjg@chromium.org Cc: Mike Frysinger vapier@gentoo.org
Some nits, but:
Acked-by: Simon Glass sjg@chromium.org
Changes for v2: - Unadded explicit parameter name in net.h for NetLoop - Eliminate CamelCase from new functions - Changed null pointer print to an assert
include/net.h | 1 + net/net.c | 44 ++++++++++++++++++++++++++++++-------------- 2 files changed, 31 insertions(+), 14 deletions(-)
diff --git a/include/net.h b/include/net.h index 33570c7..2c47604 100644 --- a/include/net.h +++ b/include/net.h @@ -430,6 +430,7 @@ extern IPaddr_t Mcast_addr; #endif
/* Initialize the network adapter */ +extern void net_init(void); extern int NetLoop(enum proto_t);
/* Shutdown adapters and cleanup */ diff --git a/net/net.c b/net/net.c index 2cfb115..01e7b29 100644 --- a/net/net.c +++ b/net/net.c @@ -267,6 +267,30 @@ static void net_cleanup_loop(void) net_clear_handlers(); }
+void net_init(void) +{
- static int first_call = 1;
- if (first_call) {
- /*
- * Setup packet buffers, aligned correctly.
- */
- int i;
blank line after
- NetTxPacket = &PktBuf[0] + (PKTALIGN - 1);
- NetTxPacket -= (ulong)NetTxPacket % PKTALIGN;
- for (i = 0; i < PKTBUFSRX; i++)
- NetRxPackets[i] = NetTxPacket + (i+1)*PKTSIZE_ALIGN;
Spaces around operators
- ArpInit();
- net_clear_handlers();
- /* Only need to setup buffer pointers once. */
- first_call = 0;
- }
- NetInitLoop();
+}
/**********************************************************************/ /* * Main network processing loop. @@ -274,28 +298,15 @@ static void net_cleanup_loop(void)
int NetLoop(enum proto_t protocol) {
- int i;
bd_t *bd = gd->bd; int ret = -1;
NetRestarted = 0; NetDevExists = 0;
- NetTxPacket = NULL;
NetTryCount = 1;
- ArpInit();
- net_clear_handlers();
- /*
- * Setup packet buffers, aligned correctly.
- */
- NetTxPacket = &PktBuf[0] + (PKTALIGN - 1);
- NetTxPacket -= (ulong)NetTxPacket % PKTALIGN;
- for (i = 0; i < PKTBUFSRX; i++)
- NetRxPackets[i] = NetTxPacket + (i+1)*PKTSIZE_ALIGN;
bootstage_mark_name(BOOTSTAGE_ID_ETH_START, "eth_start");
- net_init();
eth_halt(); eth_set_current(); if (eth_init(bd) < 0) { @@ -627,6 +638,11 @@ NetSendUDPPacket(uchar *ether, IPaddr_t dest, int dport, int sport, int eth_hdr_size; int pkt_hdr_size;
- /* make sure the NetTxPacket is initialized (NetInit() was called) */
- assert(NetTxPacket != NULL);
- if (NetTxPacket == NULL)
- return -1;
/* convert to new style broadcast */ if (dest == 0) dest = 0xFFFFFFFF; -- 1.6.0.2

Fix NetConsole bug that causes first packet header to claim a source IP address of 0.0.0.0
Signed-off-by: Joe Hershberger joe.hershberger@ni.com Cc: Joe Hershberger joe.hershberger@gmail.com Cc: Simon Glass sjg@chromium.org Cc: Mike Frysinger vapier@gentoo.org --- drivers/net/netconsole.c | 4 ++++ 1 files changed, 4 insertions(+), 0 deletions(-)
diff --git a/drivers/net/netconsole.c b/drivers/net/netconsole.c index 744f4d1..6db8a26 100644 --- a/drivers/net/netconsole.c +++ b/drivers/net/netconsole.c @@ -169,6 +169,10 @@ static int nc_start(void) else memset(nc_ether, 0, sizeof(nc_ether)); /* force arp request */
+ /* Initialize the static IP settings and buffer pointers + incase we call NetSendUDPPacket before NetLoop */ + net_init(); + return 0; }

Hi Joe,
On Tue, Mar 27, 2012 at 4:43 PM, Joe Hershberger joe.hershberger@ni.com wrote:
Fix NetConsole bug that causes first packet header to claim a source IP address of 0.0.0.0
Signed-off-by: Joe Hershberger joe.hershberger@ni.com Cc: Joe Hershberger joe.hershberger@gmail.com Cc: Simon Glass sjg@chromium.org Cc: Mike Frysinger vapier@gentoo.org
drivers/net/netconsole.c | 4 ++++ 1 files changed, 4 insertions(+), 0 deletions(-)
diff --git a/drivers/net/netconsole.c b/drivers/net/netconsole.c index 744f4d1..6db8a26 100644 --- a/drivers/net/netconsole.c +++ b/drivers/net/netconsole.c @@ -169,6 +169,10 @@ static int nc_start(void) else memset(nc_ether, 0, sizeof(nc_ether)); /* force arp request */
- /* Initialize the static IP settings and buffer pointers
- incase we call NetSendUDPPacket before NetLoop */
Comment style
/* * Initialize... */
- net_init();
return 0; }
-- 1.6.0.2
Regards, Simon

Use the NetArpTxPacket for the ARP packet, not to hold what used to be in NetTxPacket. This saves a copy and makes the code easier to understand.
Signed-off-by: Joe Hershberger joe.hershberger@ni.com Cc: Joe Hershberger joe.hershberger@gmail.com Cc: Simon Glass sjg@chromium.org Cc: Mike Frysinger vapier@gentoo.org --- include/net.h | 4 ++-- net/arp.c | 24 ++++++++++++------------ net/arp.h | 2 -- net/net.c | 24 ++++++------------------ net/ping.c | 5 ++--- 5 files changed, 22 insertions(+), 37 deletions(-)
diff --git a/include/net.h b/include/net.h index 2c47604..b116d36 100644 --- a/include/net.h +++ b/include/net.h @@ -473,13 +473,13 @@ static inline void net_set_state(enum net_loop_state state) net_state = state; }
-/* Transmit "NetTxPacket" */ +/* Transmit a packet */ static inline void NetSendPacket(uchar *pkt, int len) { (void) eth_send(pkt, len); }
-/* Transmit UDP packet, performing ARP request if needed +/* Transmit "NetTxPacket" as UDP packet, performing ARP request if needed (ether will be populated) */ extern int NetSendUDPPacket(uchar *ether, IPaddr_t dest, int dport, int sport, int payload_len); diff --git a/net/arp.c b/net/arp.c index 7d0297d..e6c929b 100644 --- a/net/arp.c +++ b/net/arp.c @@ -30,22 +30,22 @@ IPaddr_t NetArpWaitPacketIP; IPaddr_t NetArpWaitReplyIP; /* MAC address of waiting packet's destination */ uchar *NetArpWaitPacketMAC; -/* THE transmit packet */ -uchar *NetArpWaitTxPacket; int NetArpWaitTxPacketSize; -uchar NetArpWaitPacketBuf[PKTSIZE_ALIGN + PKTALIGN]; ulong NetArpWaitTimerStart; int NetArpWaitTry;
+uchar *NetArpTxPacket; /* THE ARP transmit packet */ +uchar NetArpPacketBuf[PKTSIZE_ALIGN + PKTALIGN]; + void ArpInit(void) { /* XXX problem with bss workaround */ NetArpWaitPacketMAC = NULL; NetArpWaitPacketIP = 0; NetArpWaitReplyIP = 0; - NetArpWaitTxPacket = &NetArpWaitPacketBuf[0] + (PKTALIGN - 1); - NetArpWaitTxPacket -= (ulong)NetArpWaitTxPacket % PKTALIGN; NetArpWaitTxPacketSize = 0; + NetArpTxPacket = &NetArpPacketBuf[0] + (PKTALIGN - 1); + NetArpTxPacket -= (ulong)NetArpTxPacket % PKTALIGN; }
void ArpRequest(void) @@ -56,7 +56,7 @@ void ArpRequest(void)
debug("ARP broadcast %d\n", NetArpWaitTry);
- pkt = NetTxPacket; + pkt = NetArpTxPacket;
eth_hdr_size = NetSetEther(pkt, NetBcastAddr, PROT_ARP); pkt += eth_hdr_size; @@ -88,7 +88,7 @@ void ArpRequest(void) }
NetWriteIP(&arp->ar_tpa, NetArpWaitReplyIP); - NetSendPacket(NetTxPacket, eth_hdr_size + ARP_HDR_SIZE); + NetSendPacket(NetArpTxPacket, eth_hdr_size + ARP_HDR_SIZE); }
void ArpTimeoutCheck(void) @@ -196,11 +196,11 @@ void ArpReceive(struct Ethernet_hdr *et, struct IP_UDP_hdr *ip, int len) net_get_arp_handler()((uchar *)arp, 0, reply_ip_addr, 0, len);
- /* modify header, and transmit it */ - memcpy(((struct Ethernet_hdr *)NetArpWaitTxPacket)-> - et_dest, &arp->ar_sha, ARP_HLEN); - NetSendPacket(NetArpWaitTxPacket, - NetArpWaitTxPacketSize); + /* set the mac address in the waiting packet's header + and transmit it */ + memcpy(((struct Ethernet_hdr *)NetTxPacket)->et_dest, + &arp->ar_sha, ARP_HLEN); + NetSendPacket(NetTxPacket, NetArpWaitTxPacketSize);
/* no arp request pending now */ NetArpWaitPacketIP = 0; diff --git a/net/arp.h b/net/arp.h index 61a7a21..aba0c0b 100644 --- a/net/arp.h +++ b/net/arp.h @@ -16,8 +16,6 @@ extern IPaddr_t NetArpWaitPacketIP; /* MAC address of waiting packet's destination */ extern uchar *NetArpWaitPacketMAC; -/* THE transmit packet */ -extern uchar *NetArpWaitTxPacket; extern int NetArpWaitTxPacketSize; extern ulong NetArpWaitTimerStart; extern int NetArpWaitTry; diff --git a/net/net.c b/net/net.c index 01e7b29..c41cfb2 100644 --- a/net/net.c +++ b/net/net.c @@ -443,6 +443,9 @@ restart: * Abort if ctrl-c was pressed. */ if (ctrlc()) { + /* cancel any ARP that may not have completed */ + NetArpWaitPacketIP = 0; + net_cleanup_loop(); eth_halt(); puts("\nAbort\n"); @@ -634,7 +637,6 @@ NetSendUDPPacket(uchar *ether, IPaddr_t dest, int dport, int sport, int payload_len) { uchar *pkt; - int need_arp = 0; int eth_hdr_size; int pkt_hdr_size;
@@ -651,35 +653,21 @@ NetSendUDPPacket(uchar *ether, IPaddr_t dest, int dport, int sport, if (dest == 0xFFFFFFFF) ether = NetBcastAddr;
- /* - * if MAC address was not discovered yet, save the packet and do - * an ARP request - */ - if (memcmp(ether, NetEtherNullAddr, 6) == 0) { - need_arp = 1; - pkt = NetArpWaitTxPacket; - } else - pkt = (uchar *)NetTxPacket; + pkt = (uchar *)NetTxPacket;
eth_hdr_size = NetSetEther(pkt, ether, PROT_IP); pkt += eth_hdr_size; net_set_udp_header(pkt, dest, dport, sport, payload_len); pkt_hdr_size = eth_hdr_size + IP_UDP_HDR_SIZE;
- if (need_arp) { + /* if MAC address was not discovered yet, do an ARP request */ + if (memcmp(ether, NetEtherNullAddr, 6) == 0) { debug("sending ARP for %pI4\n", &dest);
/* save the ip and eth addr for the packet to send after arp */ NetArpWaitPacketIP = dest; NetArpWaitPacketMAC = ether;
- /* - * Copy the packet data from the NetTxPacket into the - * NetArpWaitTxPacket to send after arp - */ - memcpy(pkt + IP_UDP_HDR_SIZE, (uchar *)NetTxPacket + - pkt_hdr_size, payload_len); - /* size of the waiting packet */ NetArpWaitTxPacketSize = pkt_hdr_size + payload_len;
diff --git a/net/ping.c b/net/ping.c index 50a1047..e72f7a6 100644 --- a/net/ping.c +++ b/net/ping.c @@ -49,9 +49,8 @@ static int ping_send(void)
NetArpWaitPacketIP = NetPingIP;
- eth_hdr_size = NetSetEther(NetArpWaitTxPacket, NetEtherNullAddr, - PROT_IP); - pkt = NetArpWaitTxPacket + eth_hdr_size; + eth_hdr_size = NetSetEther(NetTxPacket, NetEtherNullAddr, PROT_IP); + pkt = (uchar *)NetTxPacket + eth_hdr_size;
set_icmp_header(pkt, NetPingIP);

Hi Joe,
On Tue, Mar 27, 2012 at 4:43 PM, Joe Hershberger joe.hershberger@ni.com wrote:
Use the NetArpTxPacket for the ARP packet, not to hold what used to be in NetTxPacket. This saves a copy and makes the code easier to understand.
Signed-off-by: Joe Hershberger joe.hershberger@ni.com Cc: Joe Hershberger joe.hershberger@gmail.com Cc: Simon Glass sjg@chromium.org Cc: Mike Frysinger vapier@gentoo.org
Acked-by: Simon Glass sjg@chromium.org
include/net.h | 4 ++-- net/arp.c | 24 ++++++++++++------------ net/arp.h | 2 -- net/net.c | 24 ++++++------------------ net/ping.c | 5 ++--- 5 files changed, 22 insertions(+), 37 deletions(-)
diff --git a/include/net.h b/include/net.h index 2c47604..b116d36 100644 --- a/include/net.h +++ b/include/net.h @@ -473,13 +473,13 @@ static inline void net_set_state(enum net_loop_state state) net_state = state; }
-/* Transmit "NetTxPacket" */ +/* Transmit a packet */ static inline void NetSendPacket(uchar *pkt, int len) { (void) eth_send(pkt, len); }
-/* Transmit UDP packet, performing ARP request if needed +/* Transmit "NetTxPacket" as UDP packet, performing ARP request if needed (ether will be populated) */
Should fix up comment style.
extern int NetSendUDPPacket(uchar *ether, IPaddr_t dest, int dport, int sport, int payload_len); diff --git a/net/arp.c b/net/arp.c index 7d0297d..e6c929b 100644 --- a/net/arp.c +++ b/net/arp.c @@ -30,22 +30,22 @@ IPaddr_t NetArpWaitPacketIP; IPaddr_t NetArpWaitReplyIP; /* MAC address of waiting packet's destination */ uchar *NetArpWaitPacketMAC; -/* THE transmit packet */ -uchar *NetArpWaitTxPacket; int NetArpWaitTxPacketSize; -uchar NetArpWaitPacketBuf[PKTSIZE_ALIGN + PKTALIGN]; ulong NetArpWaitTimerStart; int NetArpWaitTry;
+uchar *NetArpTxPacket; /* THE ARP transmit packet */ +uchar NetArpPacketBuf[PKTSIZE_ALIGN + PKTALIGN];
void ArpInit(void) { /* XXX problem with bss workaround */ NetArpWaitPacketMAC = NULL; NetArpWaitPacketIP = 0; NetArpWaitReplyIP = 0;
- NetArpWaitTxPacket = &NetArpWaitPacketBuf[0] + (PKTALIGN - 1);
- NetArpWaitTxPacket -= (ulong)NetArpWaitTxPacket % PKTALIGN;
NetArpWaitTxPacketSize = 0;
- NetArpTxPacket = &NetArpPacketBuf[0] + (PKTALIGN - 1);
- NetArpTxPacket -= (ulong)NetArpTxPacket % PKTALIGN;
I don't understand what this is doing. Perhaps that is just as well.
}
void ArpRequest(void) @@ -56,7 +56,7 @@ void ArpRequest(void)
debug("ARP broadcast %d\n", NetArpWaitTry);
- pkt = NetTxPacket;
- pkt = NetArpTxPacket;
eth_hdr_size = NetSetEther(pkt, NetBcastAddr, PROT_ARP); pkt += eth_hdr_size; @@ -88,7 +88,7 @@ void ArpRequest(void) }
NetWriteIP(&arp->ar_tpa, NetArpWaitReplyIP);
- NetSendPacket(NetTxPacket, eth_hdr_size + ARP_HDR_SIZE);
- NetSendPacket(NetArpTxPacket, eth_hdr_size + ARP_HDR_SIZE);
}
void ArpTimeoutCheck(void) @@ -196,11 +196,11 @@ void ArpReceive(struct Ethernet_hdr *et, struct IP_UDP_hdr *ip, int len) net_get_arp_handler()((uchar *)arp, 0, reply_ip_addr, 0, len);
- /* modify header, and transmit it */
- memcpy(((struct Ethernet_hdr *)NetArpWaitTxPacket)->
- et_dest, &arp->ar_sha, ARP_HLEN);
- NetSendPacket(NetArpWaitTxPacket,
- NetArpWaitTxPacketSize);
- /* set the mac address in the waiting packet's header
- and transmit it */
- memcpy(((struct Ethernet_hdr *)NetTxPacket)->et_dest,
- &arp->ar_sha, ARP_HLEN);
- NetSendPacket(NetTxPacket, NetArpWaitTxPacketSize);
/* no arp request pending now */ NetArpWaitPacketIP = 0; diff --git a/net/arp.h b/net/arp.h index 61a7a21..aba0c0b 100644 --- a/net/arp.h +++ b/net/arp.h @@ -16,8 +16,6 @@ extern IPaddr_t NetArpWaitPacketIP; /* MAC address of waiting packet's destination */ extern uchar *NetArpWaitPacketMAC; -/* THE transmit packet */ -extern uchar *NetArpWaitTxPacket; extern int NetArpWaitTxPacketSize; extern ulong NetArpWaitTimerStart; extern int NetArpWaitTry; diff --git a/net/net.c b/net/net.c index 01e7b29..c41cfb2 100644 --- a/net/net.c +++ b/net/net.c @@ -443,6 +443,9 @@ restart: * Abort if ctrl-c was pressed. */ if (ctrlc()) {
- /* cancel any ARP that may not have completed */
- NetArpWaitPacketIP = 0;
net_cleanup_loop(); eth_halt(); puts("\nAbort\n"); @@ -634,7 +637,6 @@ NetSendUDPPacket(uchar *ether, IPaddr_t dest, int dport, int sport, int payload_len) { uchar *pkt;
- int need_arp = 0;
int eth_hdr_size; int pkt_hdr_size;
@@ -651,35 +653,21 @@ NetSendUDPPacket(uchar *ether, IPaddr_t dest, int dport, int sport, if (dest == 0xFFFFFFFF) ether = NetBcastAddr;
- /*
- * if MAC address was not discovered yet, save the packet and do
- * an ARP request
- */
- if (memcmp(ether, NetEtherNullAddr, 6) == 0) {
- need_arp = 1;
- pkt = NetArpWaitTxPacket;
- } else
- pkt = (uchar *)NetTxPacket;
- pkt = (uchar *)NetTxPacket;
eth_hdr_size = NetSetEther(pkt, ether, PROT_IP); pkt += eth_hdr_size; net_set_udp_header(pkt, dest, dport, sport, payload_len); pkt_hdr_size = eth_hdr_size + IP_UDP_HDR_SIZE;
- if (need_arp) {
- /* if MAC address was not discovered yet, do an ARP request */
- if (memcmp(ether, NetEtherNullAddr, 6) == 0) {
debug("sending ARP for %pI4\n", &dest);
/* save the ip and eth addr for the packet to send after arp */ NetArpWaitPacketIP = dest; NetArpWaitPacketMAC = ether;
- /*
- * Copy the packet data from the NetTxPacket into the
- * NetArpWaitTxPacket to send after arp
- */
- memcpy(pkt + IP_UDP_HDR_SIZE, (uchar *)NetTxPacket +
- pkt_hdr_size, payload_len);
/* size of the waiting packet */ NetArpWaitTxPacketSize = pkt_hdr_size + payload_len;
diff --git a/net/ping.c b/net/ping.c index 50a1047..e72f7a6 100644 --- a/net/ping.c +++ b/net/ping.c @@ -49,9 +49,8 @@ static int ping_send(void)
NetArpWaitPacketIP = NetPingIP;
- eth_hdr_size = NetSetEther(NetArpWaitTxPacket, NetEtherNullAddr,
- PROT_IP);
- pkt = NetArpWaitTxPacket + eth_hdr_size;
- eth_hdr_size = NetSetEther(NetTxPacket, NetEtherNullAddr, PROT_IP);
- pkt = (uchar *)NetTxPacket + eth_hdr_size;
set_icmp_header(pkt, NetPingIP);
-- 1.6.0.2
Regards, Simon

Link-local support allows the bootloader to get a usable network address regardless of network environment. Given that bootloaders may not always have access to a DHCP server, this functionality makes accessing it in those environments easier. It is also referred to as zero-configuration IP.
Tested on da850_am18xxevm Tested on MPC8313ERDB_33
This is dependant on the "Network stack refactoring" patch-series.
This is a portion of the changes split from the series: "Add link-local addressing support"
Joe Hershberger (3): net: Separate ArpRequest() into lower-level func net: Add link-local addressing support net: Work-around for brain-damaged Cisco equipment with arp-proxy
README | 10 ++ common/cmd_net.c | 31 +++++ doc/README.link-local | 76 +++++++++++ include/net.h | 2 +- net/Makefile | 4 +- net/arp.c | 36 ++++-- net/arp.h | 2 + net/link_local.c | 332 +++++++++++++++++++++++++++++++++++++++++++++++++ net/link_local.h | 24 ++++ net/net.c | 13 ++ 10 files changed, 519 insertions(+), 11 deletions(-) create mode 100644 doc/README.link-local create mode 100644 net/link_local.c create mode 100644 net/link_local.h

Link-local support allows the bootloader to get a usable network address regardless of network environment. Given that bootloaders may not always have access to a DHCP server, this functionality makes accessing it in those environments easier. It is also referred to as zero-configuration IP.
Tested on da850_am18xxevm Tested on MPC8313ERDB_33
This is dependant on the "Network stack refactoring" patch-series.
This is a portion of the changes split from the series: "Add link-local addressing support"
Joe Hershberger (3): net: Separate ArpRequest() into lower-level func net: Add link-local addressing support net: Work-around for brain-damaged Cisco equipment with arp-proxy
README | 10 ++ common/cmd_net.c | 31 +++++ doc/README.link-local | 76 +++++++++++ include/net.h | 2 +- net/Makefile | 4 +- net/arp.c | 36 ++++-- net/arp.h | 2 + net/link_local.c | 332 +++++++++++++++++++++++++++++++++++++++++++++++++ net/link_local.h | 24 ++++ net/net.c | 13 ++ 10 files changed, 519 insertions(+), 11 deletions(-) create mode 100644 doc/README.link-local create mode 100644 net/link_local.c create mode 100644 net/link_local.h

Link-local support will need to send ARP packets, but needs more fine-grained control over the contents. Split the implementation into 2 parts so link-local can share the code.
Signed-off-by: Joe Hershberger joe.hershberger@ni.com Cc: Joe Hershberger joe.hershberger@gmail.com --- Changes for v2: - Split from "Add link-local addressing support" - Eliminate CamelCase from new functions Changes for v3: - Rebased onto other changed patches
net/arp.c | 22 +++++++++++++--------- net/arp.h | 2 ++ 2 files changed, 15 insertions(+), 9 deletions(-)
diff --git a/net/arp.c b/net/arp.c index 4a73a0f..908ebf5 100644 --- a/net/arp.c +++ b/net/arp.c @@ -48,7 +48,8 @@ void ArpInit(void) NetArpTxPacket -= (ulong)NetArpTxPacket % PKTALIGN; }
-void ArpRequest(void) +void arp_raw_request(IPaddr_t sourceIP, const uchar *targetEther, + IPaddr_t targetIP) { uchar *pkt; struct arp_hdr *arp; @@ -69,12 +70,16 @@ void ArpRequest(void) arp->ar_pln = ARP_PLEN; arp->ar_op = htons(ARPOP_REQUEST);
- /* source ET addr */ - memcpy(&arp->ar_sha, NetOurEther, ARP_HLEN); - /* source IP addr */ - NetWriteIP(&arp->ar_spa, NetOurIP); - /* dest ET addr = 0 */ - memset(&arp->ar_tha, 0, ARP_HLEN); + memcpy(&arp->ar_sha, NetOurEther, ARP_HLEN); /* source ET addr */ + NetWriteIP(&arp->ar_spa, sourceIP); /* source IP addr */ + memcpy(&arp->ar_tha, targetEther, ARP_HLEN); /* target ET addr */ + NetWriteIP(&arp->ar_tpa, targetIP); /* target IP addr */ + + NetSendPacket(NetArpTxPacket, eth_hdr_size + ARP_HDR_SIZE); +} + +void ArpRequest(void) +{ if ((NetArpWaitPacketIP & NetOurSubnetMask) != (NetOurIP & NetOurSubnetMask)) { if (NetOurGatewayIP == 0) { @@ -87,8 +92,7 @@ void ArpRequest(void) NetArpWaitReplyIP = NetArpWaitPacketIP; }
- NetWriteIP(&arp->ar_tpa, NetArpWaitReplyIP); - NetSendPacket(NetArpTxPacket, eth_hdr_size + ARP_HDR_SIZE); + arp_raw_request(NetOurIP, NetEtherNullAddr, NetArpWaitReplyIP); }
void ArpTimeoutCheck(void) diff --git a/net/arp.h b/net/arp.h index 956fc5e..bfd57e0 100644 --- a/net/arp.h +++ b/net/arp.h @@ -22,6 +22,8 @@ extern int NetArpWaitTry;
void ArpInit(void); void ArpRequest(void); +void arp_raw_request(IPaddr_t sourceIP, const uchar *targetEther, + IPaddr_t targetIP); void ArpTimeoutCheck(void); void ArpReceive(struct ethernet_hdr *et, struct ip_udp_hdr *ip, int len);

Code based on networking/zcip.c in busybox commit 8531d76a15890c2c535908ce888b2e2aed35b172
Signed-off-by: Joe Hershberger joe.hershberger@ni.com Cc: Joe Hershberger joe.hershberger@gmail.com --- Changes for v2: - Omitted unrelated whitespace changes - Made do_link_local() static - Pushed #ifdef CONFIG_CMD_LINK_LOCAL into header Changes for v3: - Rebased onto other changed patches
README | 10 ++ common/cmd_net.c | 31 +++++ doc/README.link-local | 76 +++++++++++ include/net.h | 2 +- net/Makefile | 4 +- net/link_local.c | 332 +++++++++++++++++++++++++++++++++++++++++++++++++ net/link_local.h | 24 ++++ net/net.c | 13 ++ 8 files changed, 490 insertions(+), 2 deletions(-) create mode 100644 doc/README.link-local create mode 100644 net/link_local.c create mode 100644 net/link_local.h
diff --git a/README b/README index 6e313ad..67dc444 100644 --- a/README +++ b/README @@ -785,6 +785,8 @@ The following options need to be configured: CONFIG_CMD_JFFS2 * JFFS2 Support CONFIG_CMD_KGDB * kgdb CONFIG_CMD_LDRINFO ldrinfo (display Blackfin loader) + CONFIG_CMD_LINK_LOCAL * link-local IP address auto-configuration + (169.254.*.*) CONFIG_CMD_LOADB loadb CONFIG_CMD_LOADS loads CONFIG_CMD_MD5SUM print md5 message digest @@ -1633,6 +1635,14 @@ The following options need to be configured: the DHCP timeout and retry process takes a longer than this delay.
+ - Link-local IP address negotiation: + Negotiate with other link-local clients on the local network + for an address that doesn't require explicit configuration. + This is especially useful if a DHCP server cannot be guaranteed + to exist in all environments that the device must operate. + + See doc/README.link-local for more information. + - CDP Options: CONFIG_CDP_DEVICE_ID
diff --git a/common/cmd_net.c b/common/cmd_net.c index 2f66056..a9ade8b 100644 --- a/common/cmd_net.c +++ b/common/cmd_net.c @@ -428,3 +428,34 @@ U_BOOT_CMD( );
#endif /* CONFIG_CMD_DNS */ + +#if defined(CONFIG_CMD_LINK_LOCAL) +static int do_link_local(cmd_tbl_t *cmdtp, int flag, int argc, + char * const argv[]) +{ + char tmp[22]; + + if (NetLoop(LINKLOCAL) < 0) + return 1; + + NetOurGatewayIP = 0; + ip_to_string(NetOurGatewayIP, tmp); + setenv("gatewayip", tmp); + + ip_to_string(NetOurSubnetMask, tmp); + setenv("netmask", tmp); + + ip_to_string(NetOurIP, tmp); + setenv("ipaddr", tmp); + setenv("llipaddr", tmp); /* store this for next time */ + + return 0; +} + +U_BOOT_CMD( + linklocal, 1, 1, do_link_local, + "acquire a network IP address using the link-local protocol", + "" +); + +#endif /* CONFIG_CMD_LINK_LOCAL */ diff --git a/doc/README.link-local b/doc/README.link-local new file mode 100644 index 0000000..e623e56 --- /dev/null +++ b/doc/README.link-local @@ -0,0 +1,76 @@ +------------------------------------------ + Link-local IP address auto-configuration +------------------------------------------ + +Negotiate with other link-local clients on the local network +for an address that doesn't require explicit configuration. +This is especially useful if a DHCP server cannot be guaranteed +to exist in all environments that the device must operate. + +This is an implementation of RFC3927. + +---------- + Commands +---------- + +When CONFIG_CMD_LINK_LOCAL is defined in the board config file, +the "linklocal" command is available. This running this will +take approximately 5 seconds while the address is negotiated. + +------------------------ + Environment interation +------------------------ + +The "llipaddr" variable is set with the most recently +negotiated address and is preferred in future negotiations. + +The "ipaddr", "netmask", and "gatewayip" variables are set +after successful negotiation to enable network access. + +------------- + Limitations +------------- + +RFC3927 requires that addresses are continuously checked to +avoid conflicts, however this can only happen when the NetLoop +is getting called. It is possible for a conflict to go undetected +until a command that accesses the network is executed. + +Using NetConsole is one way to ensure that NetLoop is always +processing packets and monitoring for conflicts. + +This is also not a concern if the feature is use to connect +directly to another machine that may not be running a DHCP server. + +---------------- + Example script +---------------- + +This script allows use of DHCP and/or Link-local controlled +by env variables. It depends on CONFIG_CMD_LINK_LOCAL, CONFIG_CMD_DHCP, +and CONFIG_BOOTP_MAY_FAIL. +If both fail or are disabled, static settings are used. + +#define CONFIG_EXTRA_ENV_SETTINGS \ + "ipconfigcmd=if test \"$dhcpenabled\" -ne 0;" \ + "then " \ + "dhcpfail=0;dhcp || dhcpfail=1;" \ + "else " \ + "dhcpfail=-1;" \ + "fi;" \ + "if test \"$linklocalenabled\" -ne 0 -a " \ + "\"$dhcpfail\" -ne 0;" \ + "then " \ + "linklocal;" \ + "llfail=0;" \ + "else " \ + "llfail=-1;" \ + "fi;" \ + "if test \"$llfail\" -ne 0 -a " \ + "\"$dhcpfail\" -ne 0; " \ + "then " \ + "setenv ipaddr $sipaddr; " \ + "setenv netmask $snetmask; " \ + "setenv gatewayip $sgatewayip; " \ + "fi;\0" \ + diff --git a/include/net.h b/include/net.h index 52b9a29..a902a64 100644 --- a/include/net.h +++ b/include/net.h @@ -395,7 +395,7 @@ extern int NetRestartWrap; /* Tried all network devices */
enum proto_t { BOOTP, RARP, ARP, TFTPGET, DHCP, PING, DNS, NFS, CDP, NETCONS, SNTP, - TFTPSRV, TFTPPUT + TFTPSRV, TFTPPUT, LINKLOCAL };
/* from net/net.c */ diff --git a/net/Makefile b/net/Makefile index f1c4859..5264687 100644 --- a/net/Makefile +++ b/net/Makefile @@ -32,15 +32,17 @@ COBJS-$(CONFIG_CMD_NET) += bootp.o COBJS-$(CONFIG_CMD_CDP) += cdp.o COBJS-$(CONFIG_CMD_DNS) += dns.o COBJS-$(CONFIG_CMD_NET) += eth.o +COBJS-$(CONFIG_CMD_LINK_LOCAL) += link_local.o COBJS-$(CONFIG_CMD_NET) += net.o COBJS-$(CONFIG_BOOTP_RANDOM_DELAY) += net_rand.o +COBJS-$(CONFIG_CMD_LINK_LOCAL) += net_rand.o COBJS-$(CONFIG_CMD_NFS) += nfs.o COBJS-$(CONFIG_CMD_PING) += ping.o COBJS-$(CONFIG_CMD_RARP) += rarp.o COBJS-$(CONFIG_CMD_SNTP) += sntp.o COBJS-$(CONFIG_CMD_NET) += tftp.o
-COBJS := $(COBJS-y) +COBJS := $(sort $(COBJS-y)) SRCS := $(COBJS:.o=.c) OBJS := $(addprefix $(obj),$(COBJS))
diff --git a/net/link_local.c b/net/link_local.c new file mode 100644 index 0000000..dde96ae --- /dev/null +++ b/net/link_local.c @@ -0,0 +1,332 @@ +/* + * RFC3927 ZeroConf IPv4 Link-Local addressing + * (see http://www.zeroconf.org/) + * + * Copied from BusyBox - networking/zcip.c + * + * Copyright (C) 2003 by Arthur van Hoff (avh@strangeberry.com) + * Copyright (C) 2004 by David Brownell + * Copyright (C) 2010 by Joe Hershberger + * + * Licensed under the GPL v2 or later + */ + +#include <common.h> +#include <net.h> +#include "arp.h" +#include "net_rand.h" + +/* We don't need more than 32 bits of the counter */ +#define MONOTONIC_MS() ((unsigned)get_timer(0) * (1000 / CONFIG_SYS_HZ)) + +enum { +/* 169.254.0.0 */ + LINKLOCAL_ADDR = 0xa9fe0000, + + IN_CLASSB_NET = 0xffff0000, + IN_CLASSB_HOST = 0x0000ffff, + +/* protocol timeout parameters, specified in seconds */ + PROBE_WAIT = 1, + PROBE_MIN = 1, + PROBE_MAX = 2, + PROBE_NUM = 3, + MAX_CONFLICTS = 10, + RATE_LIMIT_INTERVAL = 60, + ANNOUNCE_WAIT = 2, + ANNOUNCE_NUM = 2, + ANNOUNCE_INTERVAL = 2, + DEFEND_INTERVAL = 10 +}; + +/* States during the configuration process. */ +static enum ll_state_t { + PROBE = 0, + RATE_LIMIT_PROBE, + ANNOUNCE, + MONITOR, + DEFEND, + DISABLED +} state = DISABLED; + +static IPaddr_t ip; +static int timeout_ms = -1; +static unsigned deadline_ms; +static unsigned conflicts; +static unsigned nprobes; +static unsigned nclaims; +static int ready; + +static void link_local_timeout(void); + +/** + * Pick a random link local IP address on 169.254/16, except that + * the first and last 256 addresses are reserved. + */ +static IPaddr_t pick(void) +{ + unsigned tmp; + + do { + tmp = rand() & IN_CLASSB_HOST; + } while (tmp > (IN_CLASSB_HOST - 0x0200)); + return (IPaddr_t) htonl((LINKLOCAL_ADDR + 0x0100) + tmp); +} + +/** + * Return milliseconds of random delay, up to "secs" seconds. + */ +static inline unsigned random_delay_ms(unsigned secs) +{ + return rand() % (secs * 1000); +} + +static void configure_wait(void) +{ + if (timeout_ms == -1) + return; + + /* poll, being ready to adjust current timeout */ + if (!timeout_ms) + timeout_ms = random_delay_ms(PROBE_WAIT); + + /* set deadline_ms to the point in time when we timeout */ + deadline_ms = MONOTONIC_MS() + timeout_ms; + + debug("...wait %d %s nprobes=%u, nclaims=%u\n", + timeout_ms, eth_get_name(), nprobes, nclaims); + + NetSetTimeout(timeout_ms, link_local_timeout); +} + +void link_local_start(void) +{ + ip = getenv_IPaddr("llipaddr"); + if (ip != 0 && (ip & IN_CLASSB_NET) != LINKLOCAL_ADDR) { + puts("invalid link address"); + net_set_state(NETLOOP_FAIL); + return; + } + NetOurSubnetMask = IN_CLASSB_NET; + + srand_mac(); + if (ip == 0) + ip = pick(); + + state = PROBE; + timeout_ms = 0; + conflicts = 0; + nprobes = 0; + nclaims = 0; + ready = 0; + + configure_wait(); +} + +static void link_local_timeout(void) +{ + switch (state) { + case PROBE: + /* timeouts in the PROBE state mean no conflicting ARP packets + have been received, so we can progress through the states */ + if (nprobes < PROBE_NUM) { + nprobes++; + debug("probe/%u %s@%pI4\n", + nprobes, eth_get_name(), &ip); + arp_raw_request(0, NetEtherNullAddr, ip); + timeout_ms = PROBE_MIN * 1000; + timeout_ms += random_delay_ms(PROBE_MAX - PROBE_MIN); + } else { + /* Switch to announce state */ + state = ANNOUNCE; + nclaims = 0; + debug("announce/%u %s@%pI4\n", + nclaims, eth_get_name(), &ip); + arp_raw_request(ip, NetOurEther, ip); + timeout_ms = ANNOUNCE_INTERVAL * 1000; + } + break; + case RATE_LIMIT_PROBE: + /* timeouts in the RATE_LIMIT_PROBE state mean no conflicting + ARP packets have been received, so we can move immediately + to the announce state */ + state = ANNOUNCE; + nclaims = 0; + debug("announce/%u %s@%pI4\n", + nclaims, eth_get_name(), &ip); + arp_raw_request(ip, NetOurEther, ip); + timeout_ms = ANNOUNCE_INTERVAL * 1000; + break; + case ANNOUNCE: + /* timeouts in the ANNOUNCE state mean no conflicting ARP + packets have been received, so we can progress through + the states */ + if (nclaims < ANNOUNCE_NUM) { + nclaims++; + debug("announce/%u %s@%pI4\n", + nclaims, eth_get_name(), &ip); + arp_raw_request(ip, NetOurEther, ip); + timeout_ms = ANNOUNCE_INTERVAL * 1000; + } else { + /* Switch to monitor state */ + state = MONITOR; + printf("Successfully assigned %pI4\n", &ip); + NetCopyIP(&NetOurIP, &ip); + ready = 1; + conflicts = 0; + timeout_ms = -1; + /* Never timeout in the monitor state */ + NetSetTimeout(0, NULL); + + /* NOTE: all other exit paths should deconfig ... */ + net_set_state(NETLOOP_SUCCESS); + return; + } + break; + case DEFEND: + /* We won! No ARP replies, so just go back to monitor */ + state = MONITOR; + timeout_ms = -1; + conflicts = 0; + break; + default: + /* Invalid, should never happen. Restart the whole protocol */ + state = PROBE; + ip = pick(); + timeout_ms = 0; + nprobes = 0; + nclaims = 0; + break; + } + configure_wait(); +} + +void link_local_receive_arp(struct arp_hdr *arp, int len) +{ + int source_ip_conflict; + int target_ip_conflict; + + if (state == DISABLED) + return; + + /* We need to adjust the timeout in case we didn't receive a + conflicting packet. */ + if (timeout_ms > 0) { + unsigned diff = deadline_ms - MONOTONIC_MS(); + if ((int)(diff) < 0) { + /* Current time is greater than the expected timeout + time. This should never happen */ + debug("missed an expected timeout\n"); + timeout_ms = 0; + } else { + debug("adjusting timeout\n"); + timeout_ms = diff | 1; /* never 0 */ + } + } +/* + * XXX Don't bother with ethernet link just yet + if ((fds[0].revents & POLLIN) == 0) { + if (fds[0].revents & POLLERR) { + // FIXME: links routinely go down; + // this shouldn't necessarily exit. + bb_error_msg("iface %s is down", eth_get_name()); + if (ready) { + run(argv, "deconfig", &ip); + } + return EXIT_FAILURE; + } + continue; + } +*/ + + debug("%s recv arp type=%d, op=%d,\n", + eth_get_name(), ntohs(arp->ar_pro), + ntohs(arp->ar_op)); + debug("\tsource=%pM %pI4\n", + &arp->ar_sha, + &arp->ar_spa); + debug("\ttarget=%pM %pI4\n", + &arp->ar_tha, + &arp->ar_tpa); + + if (arp->ar_op != htons(ARPOP_REQUEST) + && arp->ar_op != htons(ARPOP_REPLY) + ) { + configure_wait(); + return; + } + + source_ip_conflict = 0; + target_ip_conflict = 0; + + if (memcmp(&arp->ar_spa, &ip, ARP_PLEN) == 0 + && memcmp(&arp->ar_sha, NetOurEther, ARP_HLEN) != 0 + ) { + source_ip_conflict = 1; + } + if (arp->ar_op == htons(ARPOP_REQUEST) + && memcmp(&arp->ar_tpa, &ip, ARP_PLEN) == 0 + && memcmp(&arp->ar_tha, NetOurEther, ARP_HLEN) != 0 + ) { + target_ip_conflict = 1; + } + + debug("state = %d, source ip conflict = %d, target ip conflict = %d\n", + state, source_ip_conflict, target_ip_conflict); + switch (state) { + case PROBE: + case ANNOUNCE: + /* When probing or announcing, check for source IP conflicts + and other hosts doing ARP probes (target IP conflicts). */ + if (source_ip_conflict || target_ip_conflict) { + conflicts++; + state = PROBE; + if (conflicts >= MAX_CONFLICTS) { + debug("%s ratelimit\n", eth_get_name()); + timeout_ms = RATE_LIMIT_INTERVAL * 1000; + state = RATE_LIMIT_PROBE; + } + + /* restart the whole protocol */ + ip = pick(); + timeout_ms = 0; + nprobes = 0; + nclaims = 0; + } + break; + case MONITOR: + /* If a conflict, we try to defend with a single ARP probe */ + if (source_ip_conflict) { + debug("monitor conflict -- defending\n"); + state = DEFEND; + timeout_ms = DEFEND_INTERVAL * 1000; + arp_raw_request(ip, NetOurEther, ip); + } + break; + case DEFEND: + /* Well, we tried. Start over (on conflict) */ + if (source_ip_conflict) { + state = PROBE; + debug("defend conflict -- starting over\n"); + ready = 0; + NetOurIP = 0; + + /* restart the whole protocol */ + ip = pick(); + timeout_ms = 0; + nprobes = 0; + nclaims = 0; + } + break; + default: + /* Invalid, should never happen. Restart the whole protocol */ + debug("invalid state -- starting over\n"); + state = PROBE; + ip = pick(); + timeout_ms = 0; + nprobes = 0; + nclaims = 0; + break; + } + configure_wait(); +} diff --git a/net/link_local.h b/net/link_local.h new file mode 100644 index 0000000..bb99816 --- /dev/null +++ b/net/link_local.h @@ -0,0 +1,24 @@ +/* + * RFC3927 ZeroConf IPv4 Link-Local addressing + * (see http://www.zeroconf.org/) + * + * Copied from BusyBox - networking/zcip.c + * + * Copyright (C) 2003 by Arthur van Hoff (avh@strangeberry.com) + * Copyright (C) 2004 by David Brownell + * + * Licensed under the GPL v2 or later + */ + +#if defined(CONFIG_CMD_LINK_LOCAL) + +#ifndef __LINK_LOCAL_H__ +#define __LINK_LOCAL_H__ + +#include <common.h> + +void link_local_receive_arp(struct arp_hdr *arp, int len); +void link_local_start(void); + +#endif /* __LINK_LOCAL_H__ */ +#endif diff --git a/net/net.c b/net/net.c index b381974..c540c43 100644 --- a/net/net.c +++ b/net/net.c @@ -23,6 +23,12 @@ * - name of bootfile * Next step: ARP * + * LINK_LOCAL: + * + * Prerequisites: - own ethernet address + * We want: - own IP address + * Next step: ARP + * * RARP: * * Prerequisites: - own ethernet address @@ -89,6 +95,7 @@ #if defined(CONFIG_CMD_DNS) #include "dns.h" #endif +#include "link_local.h" #include "nfs.h" #include "ping.h" #include "rarp.h" @@ -402,6 +409,11 @@ restart: DnsStart(); break; #endif +#if defined(CONFIG_CMD_LINK_LOCAL) + case LINKLOCAL: + link_local_start(); + break; +#endif default: break; } @@ -1194,6 +1206,7 @@ common: case BOOTP: case CDP: case DHCP: + case LINKLOCAL: if (memcmp(NetOurEther, "\0\0\0\0\0\0", 6) == 0) { int num = eth_get_dev_index();

Cisco's arp-proxy feature fails to ignore the link-local address range This means that a link-local device on a network with this Cisco equipment will reply to ARP requests for our device (in addition to our reply). If we happen to reply first, the requester's ARP table will be populated with our MAC address, and one packet will be sent to us... shortly following this, the requester will get an ARP reply from the Cisco equipment telling the requester to send packets their way instead of to our device from now on. This work-around detects this link-local condition and will delay replying to the ARP request for 5ms so that the first packet is sent to the Cisco equipment and all following packets are sent to our device.
Signed-off-by: Joe Hershberger joe.hershberger@ni.com Cc: Joe Hershberger joe.hershberger@gmail.com --- Changes for v2: - Guard with #ifdef CONFIG_CMD_LINK_LOCAL Changes for v3: - Rebased onto other changed patches
net/arp.c | 14 ++++++++++++++ 1 files changed, 14 insertions(+), 0 deletions(-)
diff --git a/net/arp.c b/net/arp.c index 908ebf5..0b0ccbb 100644 --- a/net/arp.c +++ b/net/arp.c @@ -169,6 +169,20 @@ void ArpReceive(struct ethernet_hdr *et, struct ip_udp_hdr *ip, int len) NetCopyIP(&arp->ar_tpa, &arp->ar_spa); memcpy(&arp->ar_sha, NetOurEther, ARP_HLEN); NetCopyIP(&arp->ar_spa, &NetOurIP); + +#ifdef CONFIG_CMD_LINK_LOCAL + /* + * Work-around for brain-damaged Cisco equipment with + * arp-proxy enabled. + * + * If the requesting IP is not on our subnet, wait 5ms to + * reply to ARP request so that our reply will overwrite + * the arp-proxy's instead of the other way around. + */ + if ((NetReadIP(&arp->ar_tpa) & NetOurSubnetMask) != + (NetReadIP(&arp->ar_spa) & NetOurSubnetMask)) + udelay(5000); +#endif NetSendPacket((uchar *)et, eth_hdr_size + ARP_HDR_SIZE); return;

Link-local support will need to send ARP packets, but needs more fine-grained control over the contents. Split the implementation into 2 parts so link-local can share the code.
Signed-off-by: Joe Hershberger joe.hershberger@ni.com Cc: Joe Hershberger joe.hershberger@gmail.com Cc: Simon Glass sjg@chromium.org Cc: Mike Frysinger vapier@gentoo.org --- Changes for v2: - Split from "Add link-local addressing support" - Eliminate CamelCase from new functions
net/arp.c | 22 +++++++++++++--------- net/arp.h | 2 ++ 2 files changed, 15 insertions(+), 9 deletions(-)
diff --git a/net/arp.c b/net/arp.c index e6c929b..7599d30 100644 --- a/net/arp.c +++ b/net/arp.c @@ -48,7 +48,8 @@ void ArpInit(void) NetArpTxPacket -= (ulong)NetArpTxPacket % PKTALIGN; }
-void ArpRequest(void) +void arp_raw_request(IPaddr_t sourceIP, const uchar *targetEther, + IPaddr_t targetIP) { uchar *pkt; struct ARP_hdr *arp; @@ -69,12 +70,16 @@ void ArpRequest(void) arp->ar_pln = ARP_PLEN; arp->ar_op = htons(ARPOP_REQUEST);
- /* source ET addr */ - memcpy(&arp->ar_sha, NetOurEther, ARP_HLEN); - /* source IP addr */ - NetWriteIP(&arp->ar_spa, NetOurIP); - /* dest ET addr = 0 */ - memset(&arp->ar_tha, 0, ARP_HLEN); + memcpy(&arp->ar_sha, NetOurEther, ARP_HLEN); /* source ET addr */ + NetWriteIP(&arp->ar_spa, sourceIP); /* source IP addr */ + memcpy(&arp->ar_tha, targetEther, ARP_HLEN); /* target ET addr */ + NetWriteIP(&arp->ar_tpa, targetIP); /* target IP addr */ + + NetSendPacket(NetArpTxPacket, eth_hdr_size + ARP_HDR_SIZE); +} + +void ArpRequest(void) +{ if ((NetArpWaitPacketIP & NetOurSubnetMask) != (NetOurIP & NetOurSubnetMask)) { if (NetOurGatewayIP == 0) { @@ -87,8 +92,7 @@ void ArpRequest(void) NetArpWaitReplyIP = NetArpWaitPacketIP; }
- NetWriteIP(&arp->ar_tpa, NetArpWaitReplyIP); - NetSendPacket(NetArpTxPacket, eth_hdr_size + ARP_HDR_SIZE); + arp_raw_request(NetOurIP, NetEtherNullAddr, NetArpWaitReplyIP); }
void ArpTimeoutCheck(void) diff --git a/net/arp.h b/net/arp.h index aba0c0b..3998411 100644 --- a/net/arp.h +++ b/net/arp.h @@ -22,6 +22,8 @@ extern int NetArpWaitTry;
void ArpInit(void); void ArpRequest(void); +void arp_raw_request(IPaddr_t sourceIP, const uchar *targetEther, + IPaddr_t targetIP); void ArpTimeoutCheck(void); void ArpReceive(struct Ethernet_hdr *et, struct IP_UDP_hdr *ip, int len);

Code based on networking/zcip.c in busybox commit 8531d76a15890c2c535908ce888b2e2aed35b172
Signed-off-by: Joe Hershberger joe.hershberger@ni.com Cc: Joe Hershberger joe.hershberger@gmail.com Cc: Simon Glass sjg@chromium.org Cc: Mike Frysinger vapier@gentoo.org --- Changes for v2: - Omitted unrelated whitespace changes - Made do_link_local() static - Pushed #ifdef CONFIG_CMD_LINK_LOCAL into header
README | 10 ++ common/cmd_net.c | 31 +++++ doc/README.link-local | 76 +++++++++++ include/net.h | 2 +- net/Makefile | 4 +- net/link_local.c | 332 +++++++++++++++++++++++++++++++++++++++++++++++++ net/link_local.h | 24 ++++ net/net.c | 13 ++ 8 files changed, 490 insertions(+), 2 deletions(-) create mode 100644 doc/README.link-local create mode 100644 net/link_local.c create mode 100644 net/link_local.h
diff --git a/README b/README index ed8c79f..f804991 100644 --- a/README +++ b/README @@ -771,6 +771,8 @@ The following options need to be configured: CONFIG_CMD_JFFS2 * JFFS2 Support CONFIG_CMD_KGDB * kgdb CONFIG_CMD_LDRINFO ldrinfo (display Blackfin loader) + CONFIG_CMD_LINK_LOCAL * link-local IP address auto-configuration + (169.254.*.*) CONFIG_CMD_LOADB loadb CONFIG_CMD_LOADS loads CONFIG_CMD_MD5SUM print md5 message digest @@ -1604,6 +1606,14 @@ The following options need to be configured: the DHCP timeout and retry process takes a longer than this delay.
+ - Link-local IP address negotiation: + Negotiate with other link-local clients on the local network + for an address that doesn't require explicit configuration. + This is especially useful if a DHCP server cannot be guaranteed + to exist in all environments that the device must operate. + + See doc/README.link-local for more information. + - CDP Options: CONFIG_CDP_DEVICE_ID
diff --git a/common/cmd_net.c b/common/cmd_net.c index 97befd9..2e9ffea 100644 --- a/common/cmd_net.c +++ b/common/cmd_net.c @@ -426,3 +426,34 @@ U_BOOT_CMD( );
#endif /* CONFIG_CMD_DNS */ + +#if defined(CONFIG_CMD_LINK_LOCAL) +static int do_link_local(cmd_tbl_t *cmdtp, int flag, int argc, + char * const argv[]) +{ + char tmp[22]; + + if (NetLoop(LINKLOCAL) < 0) + return 1; + + NetOurGatewayIP = 0; + ip_to_string(NetOurGatewayIP, tmp); + setenv("gatewayip", tmp); + + ip_to_string(NetOurSubnetMask, tmp); + setenv("netmask", tmp); + + ip_to_string(NetOurIP, tmp); + setenv("ipaddr", tmp); + setenv("llipaddr", tmp); /* store this for next time */ + + return 0; +} + +U_BOOT_CMD( + linklocal, 1, 1, do_link_local, + "acquire a network IP address using the link-local protocol", + "" +); + +#endif /* CONFIG_CMD_LINK_LOCAL */ diff --git a/doc/README.link-local b/doc/README.link-local new file mode 100644 index 0000000..e623e56 --- /dev/null +++ b/doc/README.link-local @@ -0,0 +1,76 @@ +------------------------------------------ + Link-local IP address auto-configuration +------------------------------------------ + +Negotiate with other link-local clients on the local network +for an address that doesn't require explicit configuration. +This is especially useful if a DHCP server cannot be guaranteed +to exist in all environments that the device must operate. + +This is an implementation of RFC3927. + +---------- + Commands +---------- + +When CONFIG_CMD_LINK_LOCAL is defined in the board config file, +the "linklocal" command is available. This running this will +take approximately 5 seconds while the address is negotiated. + +------------------------ + Environment interation +------------------------ + +The "llipaddr" variable is set with the most recently +negotiated address and is preferred in future negotiations. + +The "ipaddr", "netmask", and "gatewayip" variables are set +after successful negotiation to enable network access. + +------------- + Limitations +------------- + +RFC3927 requires that addresses are continuously checked to +avoid conflicts, however this can only happen when the NetLoop +is getting called. It is possible for a conflict to go undetected +until a command that accesses the network is executed. + +Using NetConsole is one way to ensure that NetLoop is always +processing packets and monitoring for conflicts. + +This is also not a concern if the feature is use to connect +directly to another machine that may not be running a DHCP server. + +---------------- + Example script +---------------- + +This script allows use of DHCP and/or Link-local controlled +by env variables. It depends on CONFIG_CMD_LINK_LOCAL, CONFIG_CMD_DHCP, +and CONFIG_BOOTP_MAY_FAIL. +If both fail or are disabled, static settings are used. + +#define CONFIG_EXTRA_ENV_SETTINGS \ + "ipconfigcmd=if test \"$dhcpenabled\" -ne 0;" \ + "then " \ + "dhcpfail=0;dhcp || dhcpfail=1;" \ + "else " \ + "dhcpfail=-1;" \ + "fi;" \ + "if test \"$linklocalenabled\" -ne 0 -a " \ + "\"$dhcpfail\" -ne 0;" \ + "then " \ + "linklocal;" \ + "llfail=0;" \ + "else " \ + "llfail=-1;" \ + "fi;" \ + "if test \"$llfail\" -ne 0 -a " \ + "\"$dhcpfail\" -ne 0; " \ + "then " \ + "setenv ipaddr $sipaddr; " \ + "setenv netmask $snetmask; " \ + "setenv gatewayip $sgatewayip; " \ + "fi;\0" \ + diff --git a/include/net.h b/include/net.h index b116d36..ed8dfdc 100644 --- a/include/net.h +++ b/include/net.h @@ -394,7 +394,7 @@ extern int NetRestartWrap; /* Tried all network devices */
enum proto_t { BOOTP, RARP, ARP, TFTPGET, DHCP, PING, DNS, NFS, CDP, NETCONS, SNTP, - TFTPSRV, TFTPPUT + TFTPSRV, TFTPPUT, LINKLOCAL };
/* from net/net.c */ diff --git a/net/Makefile b/net/Makefile index f1c4859..5264687 100644 --- a/net/Makefile +++ b/net/Makefile @@ -32,15 +32,17 @@ COBJS-$(CONFIG_CMD_NET) += bootp.o COBJS-$(CONFIG_CMD_CDP) += cdp.o COBJS-$(CONFIG_CMD_DNS) += dns.o COBJS-$(CONFIG_CMD_NET) += eth.o +COBJS-$(CONFIG_CMD_LINK_LOCAL) += link_local.o COBJS-$(CONFIG_CMD_NET) += net.o COBJS-$(CONFIG_BOOTP_RANDOM_DELAY) += net_rand.o +COBJS-$(CONFIG_CMD_LINK_LOCAL) += net_rand.o COBJS-$(CONFIG_CMD_NFS) += nfs.o COBJS-$(CONFIG_CMD_PING) += ping.o COBJS-$(CONFIG_CMD_RARP) += rarp.o COBJS-$(CONFIG_CMD_SNTP) += sntp.o COBJS-$(CONFIG_CMD_NET) += tftp.o
-COBJS := $(COBJS-y) +COBJS := $(sort $(COBJS-y)) SRCS := $(COBJS:.o=.c) OBJS := $(addprefix $(obj),$(COBJS))
diff --git a/net/link_local.c b/net/link_local.c new file mode 100644 index 0000000..d47882e --- /dev/null +++ b/net/link_local.c @@ -0,0 +1,332 @@ +/* + * RFC3927 ZeroConf IPv4 Link-Local addressing + * (see http://www.zeroconf.org/) + * + * Copied from BusyBox - networking/zcip.c + * + * Copyright (C) 2003 by Arthur van Hoff (avh@strangeberry.com) + * Copyright (C) 2004 by David Brownell + * Copyright (C) 2010 by Joe Hershberger + * + * Licensed under the GPL v2 or later + */ + +#include <common.h> +#include <net.h> +#include "arp.h" +#include "net_rand.h" + +/* We don't need more than 32 bits of the counter */ +#define MONOTONIC_MS() ((unsigned)get_timer(0) * (1000 / CONFIG_SYS_HZ)) + +enum { +/* 169.254.0.0 */ + LINKLOCAL_ADDR = 0xa9fe0000, + + IN_CLASSB_NET = 0xffff0000, + IN_CLASSB_HOST = 0x0000ffff, + +/* protocol timeout parameters, specified in seconds */ + PROBE_WAIT = 1, + PROBE_MIN = 1, + PROBE_MAX = 2, + PROBE_NUM = 3, + MAX_CONFLICTS = 10, + RATE_LIMIT_INTERVAL = 60, + ANNOUNCE_WAIT = 2, + ANNOUNCE_NUM = 2, + ANNOUNCE_INTERVAL = 2, + DEFEND_INTERVAL = 10 +}; + +/* States during the configuration process. */ +static enum ll_state_t { + PROBE = 0, + RATE_LIMIT_PROBE, + ANNOUNCE, + MONITOR, + DEFEND, + DISABLED +} state = DISABLED; + +static IPaddr_t ip; +static int timeout_ms = -1; +static unsigned deadline_ms; +static unsigned conflicts; +static unsigned nprobes; +static unsigned nclaims; +static int ready; + +static void link_local_timeout(void); + +/** + * Pick a random link local IP address on 169.254/16, except that + * the first and last 256 addresses are reserved. + */ +static IPaddr_t pick(void) +{ + unsigned tmp; + + do { + tmp = rand() & IN_CLASSB_HOST; + } while (tmp > (IN_CLASSB_HOST - 0x0200)); + return (IPaddr_t) htonl((LINKLOCAL_ADDR + 0x0100) + tmp); +} + +/** + * Return milliseconds of random delay, up to "secs" seconds. + */ +static inline unsigned random_delay_ms(unsigned secs) +{ + return rand() % (secs * 1000); +} + +static void configure_wait(void) +{ + if (timeout_ms == -1) + return; + + /* poll, being ready to adjust current timeout */ + if (!timeout_ms) + timeout_ms = random_delay_ms(PROBE_WAIT); + + /* set deadline_ms to the point in time when we timeout */ + deadline_ms = MONOTONIC_MS() + timeout_ms; + + debug("...wait %d %s nprobes=%u, nclaims=%u\n", + timeout_ms, eth_get_name(), nprobes, nclaims); + + NetSetTimeout(timeout_ms, link_local_timeout); +} + +void link_local_start(void) +{ + ip = getenv_IPaddr("llipaddr"); + if (ip != 0 && (ip & IN_CLASSB_NET) != LINKLOCAL_ADDR) { + puts("invalid link address"); + net_set_state(NETLOOP_FAIL); + return; + } + NetOurSubnetMask = IN_CLASSB_NET; + + srand_mac(); + if (ip == 0) + ip = pick(); + + state = PROBE; + timeout_ms = 0; + conflicts = 0; + nprobes = 0; + nclaims = 0; + ready = 0; + + configure_wait(); +} + +static void link_local_timeout(void) +{ + switch (state) { + case PROBE: + /* timeouts in the PROBE state mean no conflicting ARP packets + have been received, so we can progress through the states */ + if (nprobes < PROBE_NUM) { + nprobes++; + debug("probe/%u %s@%pI4\n", + nprobes, eth_get_name(), &ip); + arp_raw_request(0, NetEtherNullAddr, ip); + timeout_ms = PROBE_MIN * 1000; + timeout_ms += random_delay_ms(PROBE_MAX - PROBE_MIN); + } else { + /* Switch to announce state */ + state = ANNOUNCE; + nclaims = 0; + debug("announce/%u %s@%pI4\n", + nclaims, eth_get_name(), &ip); + arp_raw_request(ip, NetOurEther, ip); + timeout_ms = ANNOUNCE_INTERVAL * 1000; + } + break; + case RATE_LIMIT_PROBE: + /* timeouts in the RATE_LIMIT_PROBE state mean no conflicting + ARP packets have been received, so we can move immediately + to the announce state */ + state = ANNOUNCE; + nclaims = 0; + debug("announce/%u %s@%pI4\n", + nclaims, eth_get_name(), &ip); + arp_raw_request(ip, NetOurEther, ip); + timeout_ms = ANNOUNCE_INTERVAL * 1000; + break; + case ANNOUNCE: + /* timeouts in the ANNOUNCE state mean no conflicting ARP + packets have been received, so we can progress through + the states */ + if (nclaims < ANNOUNCE_NUM) { + nclaims++; + debug("announce/%u %s@%pI4\n", + nclaims, eth_get_name(), &ip); + arp_raw_request(ip, NetOurEther, ip); + timeout_ms = ANNOUNCE_INTERVAL * 1000; + } else { + /* Switch to monitor state */ + state = MONITOR; + printf("Successfully assigned %pI4\n", &ip); + NetCopyIP(&NetOurIP, &ip); + ready = 1; + conflicts = 0; + timeout_ms = -1; + /* Never timeout in the monitor state */ + NetSetTimeout(0, NULL); + + /* NOTE: all other exit paths should deconfig ... */ + net_set_state(NETLOOP_SUCCESS); + return; + } + break; + case DEFEND: + /* We won! No ARP replies, so just go back to monitor */ + state = MONITOR; + timeout_ms = -1; + conflicts = 0; + break; + default: + /* Invalid, should never happen. Restart the whole protocol */ + state = PROBE; + ip = pick(); + timeout_ms = 0; + nprobes = 0; + nclaims = 0; + break; + } + configure_wait(); +} + +void link_local_receive_arp(struct ARP_hdr *arp, int len) +{ + int source_ip_conflict; + int target_ip_conflict; + + if (state == DISABLED) + return; + + /* We need to adjust the timeout in case we didn't receive a + conflicting packet. */ + if (timeout_ms > 0) { + unsigned diff = deadline_ms - MONOTONIC_MS(); + if ((int)(diff) < 0) { + /* Current time is greater than the expected timeout + time. This should never happen */ + debug("missed an expected timeout\n"); + timeout_ms = 0; + } else { + debug("adjusting timeout\n"); + timeout_ms = diff | 1; /* never 0 */ + } + } +/* + * XXX Don't bother with ethernet link just yet + if ((fds[0].revents & POLLIN) == 0) { + if (fds[0].revents & POLLERR) { + // FIXME: links routinely go down; + // this shouldn't necessarily exit. + bb_error_msg("iface %s is down", eth_get_name()); + if (ready) { + run(argv, "deconfig", &ip); + } + return EXIT_FAILURE; + } + continue; + } +*/ + + debug("%s recv arp type=%d, op=%d,\n", + eth_get_name(), ntohs(arp->ar_pro), + ntohs(arp->ar_op)); + debug("\tsource=%pM %pI4\n", + &arp->ar_sha, + &arp->ar_spa); + debug("\ttarget=%pM %pI4\n", + &arp->ar_tha, + &arp->ar_tpa); + + if (arp->ar_op != htons(ARPOP_REQUEST) + && arp->ar_op != htons(ARPOP_REPLY) + ) { + configure_wait(); + return; + } + + source_ip_conflict = 0; + target_ip_conflict = 0; + + if (memcmp(&arp->ar_spa, &ip, ARP_PLEN) == 0 + && memcmp(&arp->ar_sha, NetOurEther, ARP_HLEN) != 0 + ) { + source_ip_conflict = 1; + } + if (arp->ar_op == htons(ARPOP_REQUEST) + && memcmp(&arp->ar_tpa, &ip, ARP_PLEN) == 0 + && memcmp(&arp->ar_tha, NetOurEther, ARP_HLEN) != 0 + ) { + target_ip_conflict = 1; + } + + debug("state = %d, source ip conflict = %d, target ip conflict = %d\n", + state, source_ip_conflict, target_ip_conflict); + switch (state) { + case PROBE: + case ANNOUNCE: + /* When probing or announcing, check for source IP conflicts + and other hosts doing ARP probes (target IP conflicts). */ + if (source_ip_conflict || target_ip_conflict) { + conflicts++; + state = PROBE; + if (conflicts >= MAX_CONFLICTS) { + debug("%s ratelimit\n", eth_get_name()); + timeout_ms = RATE_LIMIT_INTERVAL * 1000; + state = RATE_LIMIT_PROBE; + } + + /* restart the whole protocol */ + ip = pick(); + timeout_ms = 0; + nprobes = 0; + nclaims = 0; + } + break; + case MONITOR: + /* If a conflict, we try to defend with a single ARP probe */ + if (source_ip_conflict) { + debug("monitor conflict -- defending\n"); + state = DEFEND; + timeout_ms = DEFEND_INTERVAL * 1000; + arp_raw_request(ip, NetOurEther, ip); + } + break; + case DEFEND: + /* Well, we tried. Start over (on conflict) */ + if (source_ip_conflict) { + state = PROBE; + debug("defend conflict -- starting over\n"); + ready = 0; + NetOurIP = 0; + + /* restart the whole protocol */ + ip = pick(); + timeout_ms = 0; + nprobes = 0; + nclaims = 0; + } + break; + default: + /* Invalid, should never happen. Restart the whole protocol */ + debug("invalid state -- starting over\n"); + state = PROBE; + ip = pick(); + timeout_ms = 0; + nprobes = 0; + nclaims = 0; + break; + } + configure_wait(); +} diff --git a/net/link_local.h b/net/link_local.h new file mode 100644 index 0000000..a558f96 --- /dev/null +++ b/net/link_local.h @@ -0,0 +1,24 @@ +/* + * RFC3927 ZeroConf IPv4 Link-Local addressing + * (see http://www.zeroconf.org/) + * + * Copied from BusyBox - networking/zcip.c + * + * Copyright (C) 2003 by Arthur van Hoff (avh@strangeberry.com) + * Copyright (C) 2004 by David Brownell + * + * Licensed under the GPL v2 or later + */ + +#if defined(CONFIG_CMD_LINK_LOCAL) + +#ifndef __LINK_LOCAL_H__ +#define __LINK_LOCAL_H__ + +#include <common.h> + +void link_local_receive_arp(struct ARP_hdr *arp, int len); +void link_local_start(void); + +#endif /* __LINK_LOCAL_H__ */ +#endif diff --git a/net/net.c b/net/net.c index c41cfb2..8658444 100644 --- a/net/net.c +++ b/net/net.c @@ -23,6 +23,12 @@ * - name of bootfile * Next step: ARP * + * LINK_LOCAL: + * + * Prerequisites: - own ethernet address + * We want: - own IP address + * Next step: ARP + * * RARP: * * Prerequisites: - own ethernet address @@ -89,6 +95,7 @@ #if defined(CONFIG_CMD_DNS) #include "dns.h" #endif +#include "link_local.h" #include "nfs.h" #include "ping.h" #include "rarp.h" @@ -403,6 +410,11 @@ restart: DnsStart(); break; #endif +#if defined(CONFIG_CMD_LINK_LOCAL) + case LINKLOCAL: + link_local_start(); + break; +#endif default: break; } @@ -1196,6 +1208,7 @@ common: case BOOTP: case CDP: case DHCP: + case LINKLOCAL: if (memcmp(NetOurEther, "\0\0\0\0\0\0", 6) == 0) { int num = eth_get_dev_index();

Cisco's arp-proxy feature fails to ignore the link-local address range This means that a link-local device on a network with this Cisco equipment will reply to ARP requests for our device (in addition to our reply). If we happen to reply first, the requester's ARP table will be populated with our MAC address, and one packet will be sent to us... shortly following this, the requester will get an ARP reply from the Cisco equipment telling the requester to send packets their way instead of to our device from now on. This work-around detects this link-local condition and will delay replying to the ARP request for 5ms so that the first packet is sent to the Cisco equipment and all following packets are sent to our device.
Signed-off-by: Joe Hershberger joe.hershberger@ni.com Cc: Joe Hershberger joe.hershberger@gmail.com Cc: Simon Glass sjg@chromium.org Cc: Mike Frysinger vapier@gentoo.org --- Changes for v2: - Guard with #ifdef CONFIG_CMD_LINK_LOCAL
net/arp.c | 14 ++++++++++++++ 1 files changed, 14 insertions(+), 0 deletions(-)
diff --git a/net/arp.c b/net/arp.c index 7599d30..56c5da8 100644 --- a/net/arp.c +++ b/net/arp.c @@ -169,6 +169,20 @@ void ArpReceive(struct Ethernet_hdr *et, struct IP_UDP_hdr *ip, int len) NetCopyIP(&arp->ar_tpa, &arp->ar_spa); memcpy(&arp->ar_sha, NetOurEther, ARP_HLEN); NetCopyIP(&arp->ar_spa, &NetOurIP); + +#ifdef CONFIG_CMD_LINK_LOCAL + /* + * Work-around for brain-damaged Cisco equipment with + * arp-proxy enabled. + * + * If the requesting IP is not on our subnet, wait 5ms to + * reply to ARP request so that our reply will overwrite + * the arp-proxy's instead of the other way around. + */ + if ((NetReadIP(&arp->ar_tpa) & NetOurSubnetMask) != + (NetReadIP(&arp->ar_spa) & NetOurSubnetMask)) + udelay(5000); +#endif NetSendPacket((uchar *)et, eth_hdr_size + ARP_HDR_SIZE); return;

Hi Joe,
On Tue, Mar 27, 2012 at 4:43 PM, Joe Hershberger joe.hershberger@ni.com wrote:
Cisco's arp-proxy feature fails to ignore the link-local address range This means that a link-local device on a network with this Cisco equipment will reply to ARP requests for our device (in addition to our reply). If we happen to reply first, the requester's ARP table will be populated with our MAC address, and one packet will be sent to us... shortly following this, the requester will get an ARP reply from the Cisco equipment telling the requester to send packets their way instead of to our device from now on. This work-around detects this link-local condition and will delay replying to the ARP request for 5ms so that the first packet is sent to the Cisco equipment and all following packets are sent to our device.
Signed-off-by: Joe Hershberger joe.hershberger@ni.com Cc: Joe Hershberger joe.hershberger@gmail.com Cc: Simon Glass sjg@chromium.org Cc: Mike Frysinger vapier@gentoo.org
Changes for v2: - Guard with #ifdef CONFIG_CMD_LINK_LOCAL
net/arp.c | 14 ++++++++++++++ 1 files changed, 14 insertions(+), 0 deletions(-)
diff --git a/net/arp.c b/net/arp.c index 7599d30..56c5da8 100644 --- a/net/arp.c +++ b/net/arp.c @@ -169,6 +169,20 @@ void ArpReceive(struct Ethernet_hdr *et, struct IP_UDP_hdr *ip, int len) NetCopyIP(&arp->ar_tpa, &arp->ar_spa); memcpy(&arp->ar_sha, NetOurEther, ARP_HLEN); NetCopyIP(&arp->ar_spa, &NetOurIP);
+#ifdef CONFIG_CMD_LINK_LOCAL
- /*
- * Work-around for brain-damaged Cisco equipment with
- * arp-proxy enabled.
- *
- * If the requesting IP is not on our subnet, wait 5ms to
- * reply to ARP request so that our reply will overwrite
- * the arp-proxy's instead of the other way around.
- */
- if ((NetReadIP(&arp->ar_tpa) & NetOurSubnetMask) !=
- (NetReadIP(&arp->ar_spa) & NetOurSubnetMask))
- udelay(5000);
+#endif
I'm sure this solves the problem, but is 5ms enough, and should we make this a CONFIG option so it can be turned off if needed?
NetSendPacket((uchar *)et, eth_hdr_size + ARP_HDR_SIZE); return;
-- 1.6.0.2
Regards, Simon

Hi Simon,
On Fri, Apr 20, 2012 at 9:44 PM, Simon Glass sjg@chromium.org wrote:
+#ifdef CONFIG_CMD_LINK_LOCAL
- /*
- * Work-around for brain-damaged Cisco equipment with
- * arp-proxy enabled.
- *
- * If the requesting IP is not on our subnet, wait 5ms to
- * reply to ARP request so that our reply will overwrite
- * the arp-proxy's instead of the other way around.
- */
- if ((NetReadIP(&arp->ar_tpa) & NetOurSubnetMask) !=
- (NetReadIP(&arp->ar_spa) & NetOurSubnetMask))
- udelay(5000);
+#endif
I'm sure this solves the problem, but is 5ms enough, and should we make this a CONFIG option so it can be turned off if needed?
In my experience it is enough. I think I'd rather keep it simple until someone sees an environment where it needs configurability... in which case it should probably be an env variable.
-Joe

Signed-off-by: Joe Hershberger joe.hershberger@ni.com Cc: Joe Hershberger joe.hershberger@gmail.com Cc: Simon Glass sjg@chromium.org Cc: Mike Frysinger vapier@gentoo.org --- Changes for v2: - Split from "Add link-local addressing support" patch-series - This patch is dependant on "Add link-local addressing support" patch-series - Knobs for enabling filters moved into net.h
drivers/net/netconsole.c | 4 ++++ include/common.h | 14 +++++++------- include/net.h | 5 +++++ net/arp.c | 9 +++++---- net/link_local.c | 26 ++++++++++++++------------ net/net.c | 40 +++++++++++++++++++++++++++++----------- net/ping.c | 4 ++-- net/rarp.c | 4 ++-- 8 files changed, 68 insertions(+), 38 deletions(-)
diff --git a/drivers/net/netconsole.c b/drivers/net/netconsole.c index 6db8a26..9f7f97c 100644 --- a/drivers/net/netconsole.c +++ b/drivers/net/netconsole.c @@ -83,6 +83,8 @@ int nc_input_packet(uchar *pkt, unsigned dest, unsigned src, unsigned len) if (dest != nc_port || !len) return 0; /* not for us */
+ debug_cond(DEBUG_DEV_PKT, "input: "%*.*s"\n", len, len, pkt); + if (input_size == sizeof(input_buffer)) return 1; /* no space */ if (len > sizeof(input_buffer) - input_size) @@ -112,6 +114,8 @@ static void nc_send_packet(const char *buf, int len) uchar *ether; IPaddr_t ip;
+ debug_cond(DEBUG_DEV_PKT, "output: "%*.*s"\n", len, len, buf); + eth = eth_get_dev(); if (eth == NULL) return; diff --git a/include/common.h b/include/common.h index b627ff4..2c1d6c1 100644 --- a/include/common.h +++ b/include/common.h @@ -711,13 +711,6 @@ int gunzip(void *, int, unsigned char *, unsigned long *); int zunzip(void *dst, int dstlen, unsigned char *src, unsigned long *lenp, int stoponerr, int offset);
-/* lib/net_utils.c */ -#include <net.h> -static inline IPaddr_t getenv_IPaddr (char *var) -{ - return (string_to_ip(getenv(var))); -} - /* lib/qsort.c */ void qsort(void *base, size_t nmemb, size_t size, int(*compar)(const void *, const void *)); @@ -786,6 +779,13 @@ void fputc(int file, const char c); int ftstc(int file); int fgetc(int file);
+/* lib/net_utils.c */ +#include <net.h> +static inline IPaddr_t getenv_IPaddr(char *var) +{ + return string_to_ip(getenv(var)); +} + /* * CONSOLE multiplexing. */ diff --git a/include/net.h b/include/net.h index ed8dfdc..b4f9b33 100644 --- a/include/net.h +++ b/include/net.h @@ -18,6 +18,10 @@
#include <asm/byteorder.h> /* for nton* / ntoh* stuff */
+#define DEBUG_LL_STATE 0 +#define DEBUG_DEV_PKT 0 +#define DEBUG_NET_PKT 0 +#define DEBUG_INT_STATE 0
/* * The number of receive packet buffers, and the required packet buffer @@ -470,6 +474,7 @@ enum net_loop_state { static inline void net_set_state(enum net_loop_state state) { extern enum net_loop_state net_state; + debug_cond(DEBUG_INT_STATE, "--- NetState set to %d\n", state); net_state = state; }
diff --git a/net/arp.c b/net/arp.c index 56c5da8..ec65e7c 100644 --- a/net/arp.c +++ b/net/arp.c @@ -55,7 +55,7 @@ void arp_raw_request(IPaddr_t sourceIP, const uchar *targetEther, struct ARP_hdr *arp; int eth_hdr_size;
- debug("ARP broadcast %d\n", NetArpWaitTry); + debug_cond(DEBUG_DEV_PKT, "ARP broadcast %d\n", NetArpWaitTry);
pkt = NetArpTxPacket;
@@ -135,7 +135,7 @@ void ArpReceive(struct Ethernet_hdr *et, struct IP_UDP_hdr *ip, int len) * address; so if we receive such a packet, we set * the server ethernet address */ - debug("Got ARP\n"); + debug_cond(DEBUG_NET_PKT, "Got ARP\n");
arp = (struct ARP_hdr *)ip; if (len < ARP_HDR_SIZE) { @@ -160,7 +160,7 @@ void ArpReceive(struct Ethernet_hdr *et, struct IP_UDP_hdr *ip, int len) switch (ntohs(arp->ar_op)) { case ARPOP_REQUEST: /* reply with our IP address */ - debug("Got ARP REQUEST, return our IP\n"); + debug_cond(DEBUG_DEV_PKT, "Got ARP REQUEST, return our IP\n"); pkt = (uchar *)et; eth_hdr_size = net_update_ether(et, et->et_src, PROT_ARP); pkt += eth_hdr_size; @@ -203,7 +203,8 @@ void ArpReceive(struct Ethernet_hdr *et, struct IP_UDP_hdr *ip, int len)
/* matched waiting packet's address */ if (reply_ip_addr == NetArpWaitReplyIP) { - debug("Got ARP REPLY, set eth addr (%pM)\n", + debug_cond(DEBUG_DEV_PKT, + "Got ARP REPLY, set eth addr (%pM)\n", arp->ar_data);
/* save address for later use */ diff --git a/net/link_local.c b/net/link_local.c index d47882e..f1cc382 100644 --- a/net/link_local.c +++ b/net/link_local.c @@ -93,7 +93,7 @@ static void configure_wait(void) /* set deadline_ms to the point in time when we timeout */ deadline_ms = MONOTONIC_MS() + timeout_ms;
- debug("...wait %d %s nprobes=%u, nclaims=%u\n", + debug_cond(DEBUG_DEV_PKT, "...wait %d %s nprobes=%u, nclaims=%u\n", timeout_ms, eth_get_name(), nprobes, nclaims);
NetSetTimeout(timeout_ms, link_local_timeout); @@ -131,7 +131,7 @@ static void link_local_timeout(void) have been received, so we can progress through the states */ if (nprobes < PROBE_NUM) { nprobes++; - debug("probe/%u %s@%pI4\n", + debug_cond(DEBUG_LL_STATE, "probe/%u %s@%pI4\n", nprobes, eth_get_name(), &ip); arp_raw_request(0, NetEtherNullAddr, ip); timeout_ms = PROBE_MIN * 1000; @@ -140,7 +140,7 @@ static void link_local_timeout(void) /* Switch to announce state */ state = ANNOUNCE; nclaims = 0; - debug("announce/%u %s@%pI4\n", + debug_cond(DEBUG_LL_STATE, "announce/%u %s@%pI4\n", nclaims, eth_get_name(), &ip); arp_raw_request(ip, NetOurEther, ip); timeout_ms = ANNOUNCE_INTERVAL * 1000; @@ -152,7 +152,7 @@ static void link_local_timeout(void) to the announce state */ state = ANNOUNCE; nclaims = 0; - debug("announce/%u %s@%pI4\n", + debug_cond(DEBUG_LL_STATE, "announce/%u %s@%pI4\n", nclaims, eth_get_name(), &ip); arp_raw_request(ip, NetOurEther, ip); timeout_ms = ANNOUNCE_INTERVAL * 1000; @@ -163,7 +163,7 @@ static void link_local_timeout(void) the states */ if (nclaims < ANNOUNCE_NUM) { nclaims++; - debug("announce/%u %s@%pI4\n", + debug_cond(DEBUG_LL_STATE, "announce/%u %s@%pI4\n", nclaims, eth_get_name(), &ip); arp_raw_request(ip, NetOurEther, ip); timeout_ms = ANNOUNCE_INTERVAL * 1000; @@ -216,10 +216,11 @@ void link_local_receive_arp(struct ARP_hdr *arp, int len) if ((int)(diff) < 0) { /* Current time is greater than the expected timeout time. This should never happen */ - debug("missed an expected timeout\n"); + debug_cond(DEBUG_LL_STATE, + "missed an expected timeout\n"); timeout_ms = 0; } else { - debug("adjusting timeout\n"); + debug_cond(DEBUG_INT_STATE, "adjusting timeout\n"); timeout_ms = diff | 1; /* never 0 */ } } @@ -239,13 +240,13 @@ void link_local_receive_arp(struct ARP_hdr *arp, int len) } */
- debug("%s recv arp type=%d, op=%d,\n", + debug_cond(DEBUG_INT_STATE, "%s recv arp type=%d, op=%d,\n", eth_get_name(), ntohs(arp->ar_pro), ntohs(arp->ar_op)); - debug("\tsource=%pM %pI4\n", + debug_cond(DEBUG_INT_STATE, "\tsource=%pM %pI4\n", &arp->ar_sha, &arp->ar_spa); - debug("\ttarget=%pM %pI4\n", + debug_cond(DEBUG_INT_STATE, "\ttarget=%pM %pI4\n", &arp->ar_tha, &arp->ar_tpa);
@@ -271,8 +272,9 @@ void link_local_receive_arp(struct ARP_hdr *arp, int len) target_ip_conflict = 1; }
- debug("state = %d, source ip conflict = %d, target ip conflict = %d\n", - state, source_ip_conflict, target_ip_conflict); + debug_cond(DEBUG_NET_PKT, + "state = %d, source ip conflict = %d, target ip conflict = " + "%d\n", state, source_ip_conflict, target_ip_conflict); switch (state) { case PROBE: case ANNOUNCE: diff --git a/net/net.c b/net/net.c index 8658444..de2d593 100644 --- a/net/net.c +++ b/net/net.c @@ -311,6 +311,7 @@ int NetLoop(enum proto_t protocol) NetRestarted = 0; NetDevExists = 0; NetTryCount = 1; + debug_cond(DEBUG_INT_STATE, "--- NetLoop Entry\n");
bootstage_mark_name(BOOTSTAGE_ID_ETH_START, "eth_start"); net_init(); @@ -331,6 +332,7 @@ restart: * here on, this code is a state machine driven by received * packets and timer events. */ + debug_cond(DEBUG_INT_STATE, "--- NetLoop Init\n"); NetInitLoop();
switch (net_check_prereq(protocol)) { @@ -461,6 +463,9 @@ restart: net_cleanup_loop(); eth_halt(); puts("\nAbort\n"); + /* include a debug print as well incase the debug + messages are directed to stderr */ + debug_cond(DEBUG_INT_STATE, "--- NetLoop Abort!\n"); goto done; }
@@ -488,6 +493,7 @@ restart: } #endif /* CONFIG_SYS_FAULT_ECHO_LINK_DOWN, ... */ #endif /* CONFIG_MII, ... */ + debug_cond(DEBUG_INT_STATE, "--- NetLoop timeout\n"); x = timeHandler; timeHandler = (thand_f *)0; (*x)(); @@ -515,10 +521,12 @@ restart: } eth_halt(); ret = NetBootFileXferSize; + debug_cond(DEBUG_INT_STATE, "--- NetLoop Success!\n"); goto done;
case NETLOOP_FAIL: net_cleanup_loop(); + debug_cond(DEBUG_INT_STATE, "--- NetLoop Fail!\n"); goto done;
case NETLOOP_CONTINUE: @@ -606,6 +614,7 @@ rxhand_f *net_get_udp_handler(void)
void net_set_udp_handler(rxhand_f *f) { + debug_cond(DEBUG_INT_STATE, "--- NetLoop UDP handler set (%p)\n", f); if (f == NULL) udp_packet_handler = dummy_handler; else @@ -619,6 +628,7 @@ rxhand_f *net_get_arp_handler(void)
void net_set_arp_handler(rxhand_f *f) { + debug_cond(DEBUG_INT_STATE, "--- NetLoop ARP handler set (%p)\n", f); if (f == NULL) arp_packet_handler = dummy_handler; else @@ -636,8 +646,12 @@ void NetSetTimeout(ulong iv, thand_f *f) { if (iv == 0) { + debug_cond(DEBUG_INT_STATE, + "--- NetLoop timeout handler cancelled\n"); timeHandler = (thand_f *)0; } else { + debug_cond(DEBUG_INT_STATE, + "--- NetLoop timeout handler set (%p)\n", f); timeHandler = f; timeStart = get_timer(0); timeDelta = iv; @@ -674,7 +688,7 @@ NetSendUDPPacket(uchar *ether, IPaddr_t dest, int dport, int sport,
/* if MAC address was not discovered yet, do an ARP request */ if (memcmp(ether, NetEtherNullAddr, 6) == 0) { - debug("sending ARP for %pI4\n", &dest); + debug_cond(DEBUG_DEV_PKT, "sending ARP for %pI4\n", &dest);
/* save the ip and eth addr for the packet to send after arp */ NetArpWaitPacketIP = dest; @@ -689,7 +703,8 @@ NetSendUDPPacket(uchar *ether, IPaddr_t dest, int dport, int sport, ArpRequest(); return 1; /* waiting */ } else { - debug("sending UDP to %pI4/%pM\n", &dest, ether); + debug_cond(DEBUG_DEV_PKT, "sending UDP to %pI4/%pM\n", + &dest, ether); NetSendPacket(NetTxPacket, pkt_hdr_size + payload_len); return 0; /* transmitted */ } @@ -912,7 +927,7 @@ NetReceive(uchar *inpkt, int len) #endif ushort cti = 0, vlanid = VLAN_NONE, myvlanid, mynvlanid;
- debug("packet received\n"); + debug_cond(DEBUG_NET_PKT, "packet received\n");
NetRxPacket = inpkt; NetRxPacketLen = len; @@ -943,8 +958,6 @@ NetReceive(uchar *inpkt, int len)
eth_proto = ntohs(et->et_protlen);
- debug("packet received\n"); - if (eth_proto < 1514) { struct E802_hdr *et802 = (struct E802_hdr *)et; /* @@ -964,7 +977,7 @@ NetReceive(uchar *inpkt, int len) struct VLAN_Ethernet_hdr *vet = (struct VLAN_Ethernet_hdr *)et;
- debug("VLAN packet received\n"); + debug_cond(DEBUG_NET_PKT, "VLAN packet received\n");
/* too small packet? */ if (len < VLAN_ETHER_HDR_SIZE) @@ -986,7 +999,7 @@ NetReceive(uchar *inpkt, int len) len -= VLAN_ETHER_HDR_SIZE; }
- debug("Receive from protocol 0x%x\n", eth_proto); + debug_cond(DEBUG_NET_PKT, "Receive from protocol 0x%x\n", eth_proto);
#if defined(CONFIG_CMD_CDP) if (iscdp) { @@ -1015,7 +1028,7 @@ NetReceive(uchar *inpkt, int len) break; #endif case PROT_IP: - debug("Got IP\n"); + debug_cond(DEBUG_NET_PKT, "Got IP\n"); /* Before we start poking the header, make sure it is there */ if (len < IP_UDP_HDR_SIZE) { debug("len bad %d < %lu\n", len, @@ -1024,11 +1037,12 @@ NetReceive(uchar *inpkt, int len) } /* Check the packet length */ if (len < ntohs(ip->ip_len)) { - printf("len bad %d < %d\n", len, ntohs(ip->ip_len)); + debug("len bad %d < %d\n", len, ntohs(ip->ip_len)); return; } len = ntohs(ip->ip_len); - debug("len=%d, v=%02x\n", len, ip->ip_hl_v & 0xff); + debug_cond(DEBUG_NET_PKT, "len=%d, v=%02x\n", + len, ip->ip_hl_v & 0xff);
/* Can't deal with anything except IPv4 */ if ((ip->ip_hl_v & 0xf0) != 0x40) @@ -1038,7 +1052,7 @@ NetReceive(uchar *inpkt, int len) return; /* Check the Checksum of the header */ if (!NetCksumOk((uchar *)ip, IP_HDR_SIZE / 2)) { - puts("checksum bad\n"); + debug("checksum bad\n"); return; } /* If it is not for us, ignore it */ @@ -1087,6 +1101,10 @@ NetReceive(uchar *inpkt, int len) return; }
+ debug_cond(DEBUG_DEV_PKT, + "received UDP (to=%pI4, from=%pI4, len=%d)\n", + &dst_ip, &src_ip, len); + #ifdef CONFIG_UDP_CHECKSUM if (ip->udp_xsum != 0) { ulong xsum; diff --git a/net/ping.c b/net/ping.c index e72f7a6..5d94f7f 100644 --- a/net/ping.c +++ b/net/ping.c @@ -45,7 +45,7 @@ static int ping_send(void)
/* XXX always send arp request */
- debug("sending ARP for %pI4\n", &NetPingIP); + debug_cond(DEBUG_DEV_PKT, "sending ARP for %pI4\n", &NetPingIP);
NetArpWaitPacketIP = NetPingIP;
@@ -93,7 +93,7 @@ void ping_receive(struct Ethernet_hdr *et, struct IP_UDP_hdr *ip, int len) case ICMP_ECHO_REQUEST: eth_hdr_size = net_update_ether(et, et->et_src, PROT_IP);
- debug("Got ICMP ECHO REQUEST, return " + debug_cond(DEBUG_DEV_PKT, "Got ICMP ECHO REQUEST, return " "%d bytes\n", eth_hdr_size + len);
ip->ip_sum = 0; diff --git a/net/rarp.c b/net/rarp.c index d9dcd17..0383c04 100644 --- a/net/rarp.c +++ b/net/rarp.c @@ -45,7 +45,7 @@ void rarp_receive(struct IP_UDP_hdr *ip, unsigned len) { struct ARP_hdr *arp;
- debug("Got RARP\n"); + debug_cond(DEBUG_NET_PKT, "Got RARP\n"); arp = (struct ARP_hdr *)ip; if (len < ARP_HDR_SIZE) { printf("bad length %d < %d\n", len, ARP_HDR_SIZE); @@ -63,7 +63,7 @@ void rarp_receive(struct IP_UDP_hdr *ip, unsigned len) if (NetServerIP == 0) NetCopyIP(&NetServerIP, &arp->ar_data[6]); memcpy(NetServerEther, &arp->ar_data[0], 6); - debug("Got good RARP\n"); + debug_cond(DEBUG_DEV_PKT, "Got good RARP\n"); net_auto_load(); } }

Add several levels of DEBUG prints so that you can limit the noise to the severety of your problem.
DEBUG_LL_STATE = Link local state machine changes DEBUG_DEV_PKT = Packets or info directed to the device DEBUG_NET_PKT = Packets on info on the network at large DEBUG_INT_STATE = Internal network state changes
Signed-off-by: Joe Hershberger joe.hershberger@ni.com Cc: Joe Hershberger joe.hershberger@gmail.com --- Changes for v2: - Split from "Add link-local addressing support" patch-series - This patch is dependant on "Add link-local addressing support" patch-series - Knobs for enabling filters moved into net.h Changes for v3: - Rebased onto other changed patches
drivers/net/netconsole.c | 4 ++++ include/common.h | 14 +++++++------- include/net.h | 5 +++++ net/arp.c | 9 +++++---- net/link_local.c | 26 ++++++++++++++------------ net/net.c | 40 +++++++++++++++++++++++++++++----------- net/ping.c | 4 ++-- net/rarp.c | 4 ++-- 8 files changed, 68 insertions(+), 38 deletions(-)
diff --git a/drivers/net/netconsole.c b/drivers/net/netconsole.c index 19e563e..14243b8 100644 --- a/drivers/net/netconsole.c +++ b/drivers/net/netconsole.c @@ -83,6 +83,8 @@ int nc_input_packet(uchar *pkt, unsigned dest, unsigned src, unsigned len) if (dest != nc_port || !len) return 0; /* not for us */
+ debug_cond(DEBUG_DEV_PKT, "input: "%*.*s"\n", len, len, pkt); + if (input_size == sizeof(input_buffer)) return 1; /* no space */ if (len > sizeof(input_buffer) - input_size) @@ -112,6 +114,8 @@ static void nc_send_packet(const char *buf, int len) uchar *ether; IPaddr_t ip;
+ debug_cond(DEBUG_DEV_PKT, "output: "%*.*s"\n", len, len, buf); + eth = eth_get_dev(); if (eth == NULL) return; diff --git a/include/common.h b/include/common.h index eb9de18..8564a65 100644 --- a/include/common.h +++ b/include/common.h @@ -729,13 +729,6 @@ int gunzip(void *, int, unsigned char *, unsigned long *); int zunzip(void *dst, int dstlen, unsigned char *src, unsigned long *lenp, int stoponerr, int offset);
-/* lib/net_utils.c */ -#include <net.h> -static inline IPaddr_t getenv_IPaddr (char *var) -{ - return (string_to_ip(getenv(var))); -} - /* lib/qsort.c */ void qsort(void *base, size_t nmemb, size_t size, int(*compar)(const void *, const void *)); @@ -804,6 +797,13 @@ void fputc(int file, const char c); int ftstc(int file); int fgetc(int file);
+/* lib/net_utils.c */ +#include <net.h> +static inline IPaddr_t getenv_IPaddr(char *var) +{ + return string_to_ip(getenv(var)); +} + /* * CONSOLE multiplexing. */ diff --git a/include/net.h b/include/net.h index a902a64..a092f29 100644 --- a/include/net.h +++ b/include/net.h @@ -19,6 +19,10 @@ #include <asm/cache.h> #include <asm/byteorder.h> /* for nton* / ntoh* stuff */
+#define DEBUG_LL_STATE 0 /* Link local state machine changes */ +#define DEBUG_DEV_PKT 0 /* Packets or info directed to the device */ +#define DEBUG_NET_PKT 0 /* Packets on info on the network at large */ +#define DEBUG_INT_STATE 0 /* Internal network state changes */
/* * The number of receive packet buffers, and the required packet buffer @@ -480,6 +484,7 @@ static inline void net_set_state(enum net_loop_state state) { extern enum net_loop_state net_state;
+ debug_cond(DEBUG_INT_STATE, "--- NetState set to %d\n", state); net_state = state; }
diff --git a/net/arp.c b/net/arp.c index 0b0ccbb..bac4cab 100644 --- a/net/arp.c +++ b/net/arp.c @@ -55,7 +55,7 @@ void arp_raw_request(IPaddr_t sourceIP, const uchar *targetEther, struct arp_hdr *arp; int eth_hdr_size;
- debug("ARP broadcast %d\n", NetArpWaitTry); + debug_cond(DEBUG_DEV_PKT, "ARP broadcast %d\n", NetArpWaitTry);
pkt = NetArpTxPacket;
@@ -135,7 +135,7 @@ void ArpReceive(struct ethernet_hdr *et, struct ip_udp_hdr *ip, int len) * address; so if we receive such a packet, we set * the server ethernet address */ - debug("Got ARP\n"); + debug_cond(DEBUG_NET_PKT, "Got ARP\n");
arp = (struct arp_hdr *)ip; if (len < ARP_HDR_SIZE) { @@ -160,7 +160,7 @@ void ArpReceive(struct ethernet_hdr *et, struct ip_udp_hdr *ip, int len) switch (ntohs(arp->ar_op)) { case ARPOP_REQUEST: /* reply with our IP address */ - debug("Got ARP REQUEST, return our IP\n"); + debug_cond(DEBUG_DEV_PKT, "Got ARP REQUEST, return our IP\n"); pkt = (uchar *)et; eth_hdr_size = net_update_ether(et, et->et_src, PROT_ARP); pkt += eth_hdr_size; @@ -203,7 +203,8 @@ void ArpReceive(struct ethernet_hdr *et, struct ip_udp_hdr *ip, int len)
/* matched waiting packet's address */ if (reply_ip_addr == NetArpWaitReplyIP) { - debug("Got ARP REPLY, set eth addr (%pM)\n", + debug_cond(DEBUG_DEV_PKT, + "Got ARP REPLY, set eth addr (%pM)\n", arp->ar_data);
/* save address for later use */ diff --git a/net/link_local.c b/net/link_local.c index dde96ae..3362863 100644 --- a/net/link_local.c +++ b/net/link_local.c @@ -93,7 +93,7 @@ static void configure_wait(void) /* set deadline_ms to the point in time when we timeout */ deadline_ms = MONOTONIC_MS() + timeout_ms;
- debug("...wait %d %s nprobes=%u, nclaims=%u\n", + debug_cond(DEBUG_DEV_PKT, "...wait %d %s nprobes=%u, nclaims=%u\n", timeout_ms, eth_get_name(), nprobes, nclaims);
NetSetTimeout(timeout_ms, link_local_timeout); @@ -131,7 +131,7 @@ static void link_local_timeout(void) have been received, so we can progress through the states */ if (nprobes < PROBE_NUM) { nprobes++; - debug("probe/%u %s@%pI4\n", + debug_cond(DEBUG_LL_STATE, "probe/%u %s@%pI4\n", nprobes, eth_get_name(), &ip); arp_raw_request(0, NetEtherNullAddr, ip); timeout_ms = PROBE_MIN * 1000; @@ -140,7 +140,7 @@ static void link_local_timeout(void) /* Switch to announce state */ state = ANNOUNCE; nclaims = 0; - debug("announce/%u %s@%pI4\n", + debug_cond(DEBUG_LL_STATE, "announce/%u %s@%pI4\n", nclaims, eth_get_name(), &ip); arp_raw_request(ip, NetOurEther, ip); timeout_ms = ANNOUNCE_INTERVAL * 1000; @@ -152,7 +152,7 @@ static void link_local_timeout(void) to the announce state */ state = ANNOUNCE; nclaims = 0; - debug("announce/%u %s@%pI4\n", + debug_cond(DEBUG_LL_STATE, "announce/%u %s@%pI4\n", nclaims, eth_get_name(), &ip); arp_raw_request(ip, NetOurEther, ip); timeout_ms = ANNOUNCE_INTERVAL * 1000; @@ -163,7 +163,7 @@ static void link_local_timeout(void) the states */ if (nclaims < ANNOUNCE_NUM) { nclaims++; - debug("announce/%u %s@%pI4\n", + debug_cond(DEBUG_LL_STATE, "announce/%u %s@%pI4\n", nclaims, eth_get_name(), &ip); arp_raw_request(ip, NetOurEther, ip); timeout_ms = ANNOUNCE_INTERVAL * 1000; @@ -216,10 +216,11 @@ void link_local_receive_arp(struct arp_hdr *arp, int len) if ((int)(diff) < 0) { /* Current time is greater than the expected timeout time. This should never happen */ - debug("missed an expected timeout\n"); + debug_cond(DEBUG_LL_STATE, + "missed an expected timeout\n"); timeout_ms = 0; } else { - debug("adjusting timeout\n"); + debug_cond(DEBUG_INT_STATE, "adjusting timeout\n"); timeout_ms = diff | 1; /* never 0 */ } } @@ -239,13 +240,13 @@ void link_local_receive_arp(struct arp_hdr *arp, int len) } */
- debug("%s recv arp type=%d, op=%d,\n", + debug_cond(DEBUG_INT_STATE, "%s recv arp type=%d, op=%d,\n", eth_get_name(), ntohs(arp->ar_pro), ntohs(arp->ar_op)); - debug("\tsource=%pM %pI4\n", + debug_cond(DEBUG_INT_STATE, "\tsource=%pM %pI4\n", &arp->ar_sha, &arp->ar_spa); - debug("\ttarget=%pM %pI4\n", + debug_cond(DEBUG_INT_STATE, "\ttarget=%pM %pI4\n", &arp->ar_tha, &arp->ar_tpa);
@@ -271,8 +272,9 @@ void link_local_receive_arp(struct arp_hdr *arp, int len) target_ip_conflict = 1; }
- debug("state = %d, source ip conflict = %d, target ip conflict = %d\n", - state, source_ip_conflict, target_ip_conflict); + debug_cond(DEBUG_NET_PKT, + "state = %d, source ip conflict = %d, target ip conflict = " + "%d\n", state, source_ip_conflict, target_ip_conflict); switch (state) { case PROBE: case ANNOUNCE: diff --git a/net/net.c b/net/net.c index c540c43..159b91a 100644 --- a/net/net.c +++ b/net/net.c @@ -310,6 +310,7 @@ int NetLoop(enum proto_t protocol) NetRestarted = 0; NetDevExists = 0; NetTryCount = 1; + debug_cond(DEBUG_INT_STATE, "--- NetLoop Entry\n");
bootstage_mark_name(BOOTSTAGE_ID_ETH_START, "eth_start"); net_init(); @@ -330,6 +331,7 @@ restart: * here on, this code is a state machine driven by received * packets and timer events. */ + debug_cond(DEBUG_INT_STATE, "--- NetLoop Init\n"); NetInitLoop();
switch (net_check_prereq(protocol)) { @@ -460,6 +462,9 @@ restart: net_cleanup_loop(); eth_halt(); puts("\nAbort\n"); + /* include a debug print as well incase the debug + messages are directed to stderr */ + debug_cond(DEBUG_INT_STATE, "--- NetLoop Abort!\n"); goto done; }
@@ -487,6 +492,7 @@ restart: } #endif /* CONFIG_SYS_FAULT_ECHO_LINK_DOWN, ... */ #endif /* CONFIG_MII, ... */ + debug_cond(DEBUG_INT_STATE, "--- NetLoop timeout\n"); x = timeHandler; timeHandler = (thand_f *)0; (*x)(); @@ -514,10 +520,12 @@ restart: } eth_halt(); ret = NetBootFileXferSize; + debug_cond(DEBUG_INT_STATE, "--- NetLoop Success!\n"); goto done;
case NETLOOP_FAIL: net_cleanup_loop(); + debug_cond(DEBUG_INT_STATE, "--- NetLoop Fail!\n"); goto done;
case NETLOOP_CONTINUE: @@ -605,6 +613,7 @@ rxhand_f *net_get_udp_handler(void)
void net_set_udp_handler(rxhand_f *f) { + debug_cond(DEBUG_INT_STATE, "--- NetLoop UDP handler set (%p)\n", f); if (f == NULL) udp_packet_handler = dummy_handler; else @@ -618,6 +627,7 @@ rxhand_f *net_get_arp_handler(void)
void net_set_arp_handler(rxhand_f *f) { + debug_cond(DEBUG_INT_STATE, "--- NetLoop ARP handler set (%p)\n", f); if (f == NULL) arp_packet_handler = dummy_handler; else @@ -635,8 +645,12 @@ void NetSetTimeout(ulong iv, thand_f *f) { if (iv == 0) { + debug_cond(DEBUG_INT_STATE, + "--- NetLoop timeout handler cancelled\n"); timeHandler = (thand_f *)0; } else { + debug_cond(DEBUG_INT_STATE, + "--- NetLoop timeout handler set (%p)\n", f); timeHandler = f; timeStart = get_timer(0); timeDelta = iv; @@ -672,7 +686,7 @@ int NetSendUDPPacket(uchar *ether, IPaddr_t dest, int dport, int sport,
/* if MAC address was not discovered yet, do an ARP request */ if (memcmp(ether, NetEtherNullAddr, 6) == 0) { - debug("sending ARP for %pI4\n", &dest); + debug_cond(DEBUG_DEV_PKT, "sending ARP for %pI4\n", &dest);
/* save the ip and eth addr for the packet to send after arp */ NetArpWaitPacketIP = dest; @@ -687,7 +701,8 @@ int NetSendUDPPacket(uchar *ether, IPaddr_t dest, int dport, int sport, ArpRequest(); return 1; /* waiting */ } else { - debug("sending UDP to %pI4/%pM\n", &dest, ether); + debug_cond(DEBUG_DEV_PKT, "sending UDP to %pI4/%pM\n", + &dest, ether); NetSendPacket(NetTxPacket, pkt_hdr_size + payload_len); return 0; /* transmitted */ } @@ -910,7 +925,7 @@ NetReceive(uchar *inpkt, int len) #endif ushort cti = 0, vlanid = VLAN_NONE, myvlanid, mynvlanid;
- debug("packet received\n"); + debug_cond(DEBUG_NET_PKT, "packet received\n");
NetRxPacket = inpkt; NetRxPacketLen = len; @@ -941,8 +956,6 @@ NetReceive(uchar *inpkt, int len)
eth_proto = ntohs(et->et_protlen);
- debug("packet received\n"); - if (eth_proto < 1514) { struct e802_hdr *et802 = (struct e802_hdr *)et; /* @@ -962,7 +975,7 @@ NetReceive(uchar *inpkt, int len) struct vlan_ethernet_hdr *vet = (struct vlan_ethernet_hdr *)et;
- debug("VLAN packet received\n"); + debug_cond(DEBUG_NET_PKT, "VLAN packet received\n");
/* too small packet? */ if (len < VLAN_ETHER_HDR_SIZE) @@ -984,7 +997,7 @@ NetReceive(uchar *inpkt, int len) len -= VLAN_ETHER_HDR_SIZE; }
- debug("Receive from protocol 0x%x\n", eth_proto); + debug_cond(DEBUG_NET_PKT, "Receive from protocol 0x%x\n", eth_proto);
#if defined(CONFIG_CMD_CDP) if (iscdp) { @@ -1013,7 +1026,7 @@ NetReceive(uchar *inpkt, int len) break; #endif case PROT_IP: - debug("Got IP\n"); + debug_cond(DEBUG_NET_PKT, "Got IP\n"); /* Before we start poking the header, make sure it is there */ if (len < IP_UDP_HDR_SIZE) { debug("len bad %d < %lu\n", len, @@ -1022,11 +1035,12 @@ NetReceive(uchar *inpkt, int len) } /* Check the packet length */ if (len < ntohs(ip->ip_len)) { - printf("len bad %d < %d\n", len, ntohs(ip->ip_len)); + debug("len bad %d < %d\n", len, ntohs(ip->ip_len)); return; } len = ntohs(ip->ip_len); - debug("len=%d, v=%02x\n", len, ip->ip_hl_v & 0xff); + debug_cond(DEBUG_NET_PKT, "len=%d, v=%02x\n", + len, ip->ip_hl_v & 0xff);
/* Can't deal with anything except IPv4 */ if ((ip->ip_hl_v & 0xf0) != 0x40) @@ -1036,7 +1050,7 @@ NetReceive(uchar *inpkt, int len) return; /* Check the Checksum of the header */ if (!NetCksumOk((uchar *)ip, IP_HDR_SIZE / 2)) { - puts("checksum bad\n"); + debug("checksum bad\n"); return; } /* If it is not for us, ignore it */ @@ -1085,6 +1099,10 @@ NetReceive(uchar *inpkt, int len) return; }
+ debug_cond(DEBUG_DEV_PKT, + "received UDP (to=%pI4, from=%pI4, len=%d)\n", + &dst_ip, &src_ip, len); + #ifdef CONFIG_UDP_CHECKSUM if (ip->udp_xsum != 0) { ulong xsum; diff --git a/net/ping.c b/net/ping.c index 2ba9f76..0710b92 100644 --- a/net/ping.c +++ b/net/ping.c @@ -45,7 +45,7 @@ static int ping_send(void)
/* XXX always send arp request */
- debug("sending ARP for %pI4\n", &NetPingIP); + debug_cond(DEBUG_DEV_PKT, "sending ARP for %pI4\n", &NetPingIP);
NetArpWaitPacketIP = NetPingIP;
@@ -93,7 +93,7 @@ void ping_receive(struct ethernet_hdr *et, struct ip_udp_hdr *ip, int len) case ICMP_ECHO_REQUEST: eth_hdr_size = net_update_ether(et, et->et_src, PROT_IP);
- debug("Got ICMP ECHO REQUEST, return " + debug_cond(DEBUG_DEV_PKT, "Got ICMP ECHO REQUEST, return " "%d bytes\n", eth_hdr_size + len);
ip->ip_sum = 0; diff --git a/net/rarp.c b/net/rarp.c index 0243177..1df384d 100644 --- a/net/rarp.c +++ b/net/rarp.c @@ -45,7 +45,7 @@ void rarp_receive(struct ip_upd_hdr *ip, unsigned len) { struct arp_hdr *arp;
- debug("Got RARP\n"); + debug_cond(DEBUG_NET_PKT, "Got RARP\n"); arp = (struct arp_hdr *)ip; if (len < ARP_HDR_SIZE) { printf("bad length %d < %d\n", len, ARP_HDR_SIZE); @@ -63,7 +63,7 @@ void rarp_receive(struct ip_upd_hdr *ip, unsigned len) if (NetServerIP == 0) NetCopyIP(&NetServerIP, &arp->ar_data[6]); memcpy(NetServerEther, &arp->ar_data[0], 6); - debug("Got good RARP\n"); + debug_cond(DEBUG_DEV_PKT, "Got good RARP\n"); net_auto_load(); } }

Dear Joe Hershberger,
Link-local support allows the bootloader to get a usable network address regardless of network environment. Given that bootloaders may not always have access to a DHCP server, this functionality makes accessing it in those environments easier. It is also referred to as zero-configuration IP.
The following script allows the network to be configured for DHCP, link-local, and static IP addresses configurably by the environment using "dhcpenabled" and "linklocalenabled".
#define CONFIG_EXTRA_ENV_SETTINGS \ "ipconfigcmd=if test \"$dhcpenabled\" -ne 0;" \ "then " \ "dhcpfail=0;dhcp || dhcpfail=1;" \ "else " \ "dhcpfail=-1;" \ "fi;" \ "if test \"$linklocalenabled\" -ne 0 -a " \ "\"$dhcpfail\" -ne 0;" \ "then " \ "linklocal;" \ "llfail=0;" \ "else " \ "llfail=-1;" \ "fi;" \ "if test \"$llfail\" -ne 0 -a " \ "\"$dhcpfail\" -ne 0; " \ "then " \ "setenv ipaddr $sipaddr; " \ "setenv netmask $snetmask; " \ "setenv gatewayip $sgatewayip; " \ "fi;\0" \
Some refactoring of net/ was done to make adding link-local support cleaner as well as generally improving the readability and organization of the code.
Tested on da850_am18xxevm Tested on MPC8313ERDB_33
check-patch No warnings or errors (depends on ignoring CONSIDER_KSTRTO)
MAKEALL -a powerpc No additional warnings or errors beyond master
MAKEALL -a arm No additional warnings or errors beyond master
git-test-sequence Cleanly bisectable Code-size affects on MPC8313ERDB_33 shown below
cat sizeofMCP8313
make BUILD_DIR=/tmp/u-boot-build/ ARCH=powerpc CROSS_COMPILE=powerpc-linux-gnu- USE_PRIVATE_LIBGG=yes distclean > /dev/null && \ make BUILD_DIR=/tmp/u-boot-build/ ARCH=powerpc CROSS_COMPILE=powerpc-linux-gnu- MPC8313ERDB_33_config > /dev/null && \ make BUILD_DIR=/tmp/u-boot-build/ ARCH=powerpc CROSS_COMPILE=powerpc-linux-gnu- USE_PRIVATE_LIBGG=yes > /dev/null && \ powerpc-linux-gnu-size /tmp/u-boot-build/u-boot
git-test-sequence origin/master.. './sizeofMCP8313'
Testing ./sizeofMCP8313 HEAD is now at 4dbe8d8 net: Remove volatile from all of net except the eth driver interface text data bss dec hex filename 264917 24880 42104 331901 5107d /tmp/u-boot-build/ppc/u-boot Passed: 90ea672853753b5a4244db60d095e5f99cdcfe7d. Testing ./sizeofMCP8313 HEAD is now at 92893c4 net: cosmetic: checkpatch compliance text data bss dec hex filename 264917 24880 42104 331901 5107d /tmp/u-boot-build/ppc/u-boot Passed: 12b0b6e953406a1ed98d1c4dd9701078d1dfe00d. Testing ./sizeofMCP8313 HEAD is now at d6c19a3 net: Move mv64x6x_eth_initialize() to board_eth_init() text data bss dec hex filename 264917 24880 42104 331901 5107d /tmp/u-boot-build/ppc/u-boot Passed: cf9f493ab5d08a282ec9905797fa58d82b15714d. Testing ./sizeofMCP8313 HEAD is now at 52403e8 net: Make the MAC-seeded random number generator available to /net text data bss dec hex filename 264917 24880 42104 331901 5107d /tmp/u-boot-build/ppc/u-boot Passed: a92c9c9e8f68337a78ee2d7c6775299d7c057b52. Testing ./sizeofMCP8313 HEAD is now at ff86c81 net: Move CDP out of net.c text data bss dec hex filename 264917 24880 42104 331901 5107d /tmp/u-boot-build/ppc/u-boot Passed: 9bf01065cb04c34e43cc5d56162aa2dac9d61f3e. Testing ./sizeofMCP8313 HEAD is now at da1998d net: Move ARP out of net.c text data bss dec hex filename 265045 24928 42104 332077 5112d /tmp/u-boot-build/ppc/u-boot Passed: 621d67b8c6126ef6510d0a2b2c06ce410cd60327. Testing ./sizeofMCP8313 HEAD is now at 408b8ab net: Move PING out of net.c text data bss dec hex filename 265237 24976 42096 332309 51215 /tmp/u-boot-build/ppc/u-boot Passed: e8aaa7382de39137c91809ba2e121f87338627d5. Testing ./sizeofMCP8313 HEAD is now at 1b3ed14 net: Move RARP receive logic out of net.c text data bss dec hex filename 265237 24976 42096 332309 51215 /tmp/u-boot-build/ppc/u-boot Passed: 7a8216874def295337178da67078758cbf909681. Testing ./sizeofMCP8313 HEAD is now at 50eb857 net: cosmetic: Un-typedef variables in net text data bss dec hex filename 265237 24976 42096 332309 51215 /tmp/u-boot-build/ppc/u-boot Passed: b679739af8ffc6f9a31264c175077f4ac96c236d. Testing ./sizeofMCP8313 HEAD is now at 22259f9 net: cosmetic: Improve variable names and code readability text data bss dec hex filename 265237 24976 42096 332309 51215 /tmp/u-boot-build/ppc/u-boot Passed: 834a569897337636fe861797625fbdbbe1373f75. Testing ./sizeofMCP8313 HEAD is now at 5340a18 net: Refactor IP, UPD, and ICMP header writing functions text data bss dec hex filename 265237 24972 42096 332305 51211 /tmp/u-boot-build/ppc/u-boot Passed: 20b84dcfa7d355010cacc3493e1e86521b00879f. Testing ./sizeofMCP8313 HEAD is now at 275e230 net: Refactor packet length computations text data bss dec hex filename 265221 24972 42096 332289 51201 /tmp/u-boot-build/ppc/u-boot Passed: d078d84d959dbb40a67baa16e8b0d210e6472c79. Testing ./sizeofMCP8313 HEAD is now at c1214f2 net: Refactor bootp packet length computations text data bss dec hex filename 265221 24972 42096 332289 51201 /tmp/u-boot-build/ppc/u-boot Passed: 3ff5dfd46c8f70ec2304a150a606871b7504cf42. Testing ./sizeofMCP8313 HEAD is now at 9cff376 net: Refactor ping recieve handler text data bss dec hex filename 265093 24968 42096 332157 5117d /tmp/u-boot-build/ppc/u-boot Passed: e531ec8d3986f45d30862eacce73b0db55e2fb13. Testing ./sizeofMCP8313 HEAD is now at e47e5ac net: Refactor to call NetSendPacket() instead of calling eth_send() text data bss dec hex filename 265093 24968 42096 332157 5117d /tmp/u-boot-build/ppc/u-boot Passed: 4a78d3ab2514c8d00bc18978f7a489a800a3a77b. Testing ./sizeofMCP8313 HEAD is now at 9bcb672 net: Refactor to protect access to the NetState variable text data bss dec hex filename 265109 24956 42096 332161 51181 /tmp/u-boot-build/ppc/u-boot Passed: 8ecfa86d86fe2624cb509c41fbaec8dcf13a9945. Testing ./sizeofMCP8313 HEAD is now at a2c79e3 net: Refactor to separate the UDP handler from the ARP handler text data bss dec hex filename 265317 24956 42112 332385 51261 /tmp/u-boot-build/ppc/u-boot Passed: f66f88edda9ec16b3de20a10faa1c6bb35f316c2. Testing ./sizeofMCP8313 HEAD is now at 502deaa net: Add NetUpdateEther() to handle ARP or Ping replies (VLAN or SNAP) text data bss dec hex filename 265429 24952 42112 332493 512cd /tmp/u-boot-build/ppc/u-boot Passed: 3daff36dc0b5b65b8df7a3a7011f1e5f89eb5a9d. Testing ./sizeofMCP8313 HEAD is now at 315bd4d net: Don't write the "serverip" env var if told not to in the config text data bss dec hex filename 265429 24952 42112 332493 512cd /tmp/u-boot-build/ppc/u-boot Passed: b0e7a73485c771eed710d6b9afd1e80e0ccc2cfa. Testing ./sizeofMCP8313 HEAD is now at 82b1968 net: Fix compile warning if CONFIG_BOOTP_SERVERIP is not defined text data bss dec hex filename 265429 24952 42112 332493 512cd /tmp/u-boot-build/ppc/u-boot Passed: 121eb763232d5cf6a4e66f6c100713537f6036cf. Testing ./sizeofMCP8313 HEAD is now at cebdefe net: Remove unneeded static allocation for MAC address in PingSend() text data bss dec hex filename 265397 24944 42104 332445 5129d /tmp/u-boot-build/ppc/u-boot Passed: 1366a9b1529e9752dd4a47d7c82545b13e11b461. Testing ./sizeofMCP8313 HEAD is now at 8f856ad net: Fix net buffer initialization text data bss dec hex filename 265605 24956 42104 332665 51379 /tmp/u-boot-build/ppc/u-boot Passed: 16cbef4189d5be4837c3de9dc2ac0bcf8d89fa75. Testing ./sizeofMCP8313 HEAD is now at 002991b net: Refactor NetSendUDPPacket to share more code text data bss dec hex filename 265557 24956 42104 332617 51349 /tmp/u-boot-build/ppc/u-boot Passed: 7f001d4025dc564fe2a96a1e32214e194b891bd1. Testing ./sizeofMCP8313 HEAD is now at 314902e net: Don't copy every packet that waits for an ARP text data bss dec hex filename 265573 24956 42104 332633 51359 /tmp/u-boot-build/ppc/u-boot Passed: 3be3cdd2224c88c88442e48c0f4f836d7be1d8ae. Testing ./sizeofMCP8313 HEAD is now at 2a43805 net: Add option CONFIG_BOOTP_CAN_FAIL text data bss dec hex filename 265573 24956 42104 332633 51359 /tmp/u-boot-build/ppc/u-boot Passed: 4d578ba84228e51a56249035207085f0845d3be8. Testing ./sizeofMCP8313 HEAD is now at defb33c net: Add link-local addressing support text data bss dec hex filename 265577 24956 42104 332637 5135d /tmp/u-boot-build/ppc/u-boot Passed: cb6b29e82f292b5b27aec6f51f2f9d50e1f8b0f8. Testing ./sizeofMCP8313 HEAD is now at 615c707 net: Work-around for brain-damaged Cisco routers with arp-proxy text data bss dec hex filename 265657 24956 42104 332717 513ad /tmp/u-boot-build/ppc/u-boot Passed: 63a3ddaf7e029bb7ec71b1edb42473ae6a208dec. Testing ./sizeofMCP8313 HEAD is now at 7d5229d net: Allow filtering on debug traces in the net subsystem text data bss dec hex filename 265594 24948 42104 332646 51366 /tmp/u-boot-build/ppc/u-boot Passed: 1cf14c686c5b60bfa48d8bfd6eb40e20ac0c378b. All's well.
Joe Hershberger (28): net: Remove volatile from all of net except the eth driver interface net: cosmetic: checkpatch compliance net: Move mv64x6x_eth_initialize() to board_eth_init() net: Make the MAC-seeded random number generator available to /net net: Move CDP out of net.c net: Move ARP out of net.c net: Move PING out of net.c net: Move RARP receive logic out of net.c net: cosmetic: Un-typedef variables in net net: cosmetic: Improve variable names and code readability net: Refactor IP, UPD, and ICMP header writing functions net: Refactor packet length computations net: Refactor bootp packet length computations net: Refactor ping recieve handler net: Refactor to call NetSendPacket() instead of calling eth_send() net: Refactor to protect access to the NetState variable net: Refactor to separate the UDP handler from the ARP handler net: Add NetUpdateEther() to handle ARP or Ping replies (VLAN or SNAP) net: Don't write the "serverip" env var if told not to in the config net: Fix compile warning if CONFIG_BOOTP_SERVERIP is not defined net: Remove unneeded static allocation for MAC address in PingSend() net: Fix net buffer initialization net: Refactor NetSendUDPPacket to share more code net: Don't copy every packet that waits for an ARP net: Add option CONFIG_BOOTP_CAN_FAIL net: Add link-local addressing support net: Work-around for brain-damaged Cisco routers with arp-proxy net: Allow filtering on debug traces in the net subsystem
README | 22 +- arch/powerpc/cpu/mpc8260/ether_fcc.c | 10 +- arch/powerpc/cpu/mpc8xx/fec.c | 2 +- board/Marvell/db64360/db64360.c | 12 +- board/Marvell/db64360/mv_eth.c | 78 ++- board/Marvell/db64360/mv_eth.h | 63 +-- board/Marvell/db64460/db64460.c | 12 +- board/Marvell/db64460/mv_eth.c | 78 ++- board/Marvell/db64460/mv_eth.h | 63 +-- board/esd/cpci750/cpci750.c | 6 + board/esd/cpci750/mv_eth.c | 78 ++- board/esd/cpci750/mv_eth.h | 63 +-- board/prodrive/p3mx/mv_eth.c | 78 ++- board/prodrive/p3mx/mv_eth.h | 63 +-- board/prodrive/p3mx/p3mx.c | 6 + common/cmd_net.c | 34 +- common/main.c | 1 - drivers/net/netconsole.c | 130 ++-- include/common.h | 4 + include/configs/am3517_crane.h | 2 +- include/configs/am3517_evm.h | 2 +- include/configs/origen.h | 2 +- include/net.h | 206 ++++--- net/Makefile | 8 +- net/arp.c | 242 +++++++ net/arp.h | 30 + net/bootp.c | 435 ++++++------- net/bootp.h | 60 +- net/cdp.c | 372 +++++++++++ net/cdp.h | 18 + net/dns.c | 12 +- net/eth.c | 75 +-- net/link_local.c | 340 ++++++++++ net/link_local.h | 21 + net/net.c | 1152 +++++++++------------------------- net/net_rand.c | 68 ++ net/net_rand.h | 18 + net/nfs.c | 306 +++++----- net/nfs.h | 2 +- net/ping.c | 119 ++++ net/ping.h | 20 + net/rarp.c | 77 ++- net/rarp.h | 5 +- net/sntp.c | 37 +- net/sntp.h | 2 +- net/tftp.c | 50 +- net/tftp.h | 2 +- 47 files changed, 2625 insertions(+), 1861 deletions(-) create mode 100644 net/arp.c create mode 100644 net/arp.h create mode 100644 net/cdp.c create mode 100644 net/cdp.h create mode 100644 net/link_local.c create mode 100644 net/link_local.h create mode 100644 net/net_rand.c create mode 100644 net/net_rand.h create mode 100644 net/ping.c create mode 100644 net/ping.h
is it correct you're the new network custodian in uboot?

On Mar 31, 2012, at 4:11 PM, Marek Vasut marek.vasut@gmail.com wrote:
is it correct you're the new network custodian in uboot?
Yes, that is correct.
Joe

Dear Joe Hershberger,
On Mar 31, 2012, at 4:11 PM, Marek Vasut marek.vasut@gmail.com wrote:
is it correct you're the new network custodian in uboot?
Yes, that is correct.
Hey, welcome abroad! :-)
btw. I reassigned most of the net patches in patchwork to you, hope you don't mind.
Joe
Best regards, Marek Vasut

On Mar 31, 2012, at 5:40 PM, Marek Vasut marex@denx.de wrote:
Dear Joe Hershberger,
On Mar 31, 2012, at 4:11 PM, Marek Vasut marek.vasut@gmail.com wrote:
is it correct you're the new network custodian in uboot?
Yes, that is correct.
Hey, welcome abroad! :-)
btw. I reassigned most of the net patches in patchwork to you, hope you don't mind.
Don't mind at all. Quite the opposite actually. Thank you. Easier to find the patches to potentially merge.
Joe
participants (11)
-
Anatolij Gustschin
-
Joe Hershberger
-
Joe Hershberger
-
Marek Vasut
-
Marek Vasut
-
Michael Walle
-
Mike Frysinger
-
Simon Glass
-
Stefan Roese
-
Stefano Babic
-
Wolfgang Denk