diff options
Diffstat (limited to 'net')
-rw-r--r-- | net/Kconfig | 19 | ||||
-rw-r--r-- | net/Makefile | 3 | ||||
-rw-r--r-- | net/dhcp.c | 491 | ||||
-rw-r--r-- | net/dns.c | 264 | ||||
-rw-r--r-- | net/eth.c | 98 | ||||
-rw-r--r-- | net/net.c | 1244 | ||||
-rw-r--r-- | net/netconsole.c | 228 | ||||
-rw-r--r-- | net/nfs.c | 726 | ||||
-rw-r--r-- | net/ping.c | 152 | ||||
-rw-r--r-- | net/rarp.c | 101 | ||||
-rw-r--r-- | net/sntp.h | 61 | ||||
-rw-r--r-- | net/tftp.c | 447 |
12 files changed, 1903 insertions, 1931 deletions
diff --git a/net/Kconfig b/net/Kconfig index cca2b00387..3169d20832 100644 --- a/net/Kconfig +++ b/net/Kconfig @@ -7,10 +7,6 @@ config NET_DHCP bool prompt "dhcp support" -config NET_RARP - bool - prompt "rarp protocol support" - config NET_NFS bool prompt "nfs support" @@ -22,4 +18,19 @@ config NET_PING config NET_TFTP bool prompt "tftp support" + +config NET_TFTP_PUSH + bool + prompt "tftp push support" + +config NET_NETCONSOLE + bool + prompt "network console support" + help + This option adds support for a simple udp based network console. + +config NET_RESOLV + bool + prompt "dns support" + endif diff --git a/net/Makefile b/net/Makefile index 0ffc8959c1..66dc564dac 100644 --- a/net/Makefile +++ b/net/Makefile @@ -2,6 +2,7 @@ obj-$(CONFIG_NET_DHCP) += dhcp.o obj-$(CONFIG_NET) += eth.o obj-$(CONFIG_NET) += net.o obj-$(CONFIG_NET_NFS) += nfs.o -obj-$(CONFIG_NET_RARP) += rarp.o obj-$(CONFIG_NET_TFTP) += tftp.o obj-$(CONFIG_NET_PING) += ping.o +obj-$(CONFIG_NET_RESOLV)+= dns.o +obj-$(CONFIG_NET_NETCONSOLE) += netconsole.o diff --git a/net/dhcp.c b/net/dhcp.c index f27a6966d7..0771964b91 100644 --- a/net/dhcp.c +++ b/net/dhcp.c @@ -14,64 +14,44 @@ #include <clock.h> #include <net.h> #include <libbb.h> -#include "tftp.h" -#include "nfs.h" +#include <errno.h> +#include <linux/err.h> #define OPT_SIZE 312 /* Minimum DHCP Options size per RFC2131 - results in 576 byte pkt */ -typedef struct -{ - uchar bp_op; /* Operation */ -# define OP_BOOTREQUEST 1 -# define OP_BOOTREPLY 2 - uchar bp_htype; /* Hardware type */ -# define HWT_ETHER 1 - 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 */ +struct bootp { + uint8_t bp_op; /* Operation */ +#define OP_BOOTREQUEST 1 +#define OP_BOOTREPLY 2 + uint8_t bp_htype; /* Hardware type */ +#define HWT_ETHER 1 + uint8_t bp_hlen; /* Hardware address length */ +#define HWL_ETHER 6 + uint8_t bp_hops; /* Hop count (gateway thing) */ + uint32_t bp_id; /* Transaction ID */ + uint16_t bp_secs; /* Seconds since boot */ + uint16_t 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 */ + uint8_t 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) -#define BOOTP_SIZE (ETHER_HDR_SIZE + IP_HDR_SIZE + BOOTP_HDR_SIZE) - -/**********************************************************************/ -/* - * Global functions and variables. - */ - -/* bootp.c */ -extern ulong BootpID; /* ID of cur BOOTP request */ -extern char BootFile[128]; /* Boot file name */ -#ifdef CONFIG_BOOTP_RANDOM_DELAY -ulong seed1, seed2; /* seed for random BOOTP delay */ -#endif - - -/* Send a BOOTP request */ -extern void BootpRequest (void); - -/****************** DHCP Support *********************/ + char bp_vend[0]; /* Vendor information */ +}; /* DHCP States */ -typedef enum { INIT, - INIT_REBOOT, - REBOOTING, - SELECTING, - REQUESTING, - REBINDING, - BOUND, - RENEWING } dhcp_state_t; +typedef enum { + INIT, + INIT_REBOOT, + REBOOTING, + SELECTING, + REQUESTING, + REBINDING, + BOUND, + RENEWING, +} dhcp_state_t; #define DHCP_DISCOVER 1 #define DHCP_OFFER 2 @@ -81,44 +61,27 @@ typedef enum { INIT, #define DHCP_NAK 6 #define DHCP_RELEASE 7 -#define SELECT_TIMEOUT 3 /* Seconds to wait for offers */ - -/**********************************************************************/ - #define BOOTP_VENDOR_MAGIC 0x63825363 /* RFC1048 Magic Cookie */ -#define TIMEOUT 5 /* Seconds before trying BOOTP again */ -#ifndef CONFIG_NET_RETRY_COUNT -# 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 */ -#ifndef CONFIG_DHCP_MIN_EXT_LEN /* minimal length of extension list */ -#define CONFIG_DHCP_MIN_EXT_LEN 64 -#endif - -ulong BootpID; -#ifdef CONFIG_BOOTP_RANDOM_DELAY -ulong seed1, seed2; -#endif +#define DHCP_MIN_EXT_LEN 64 /* minimal length of extension list */ -dhcp_state_t dhcp_state = INIT; -unsigned long dhcp_leasetime = 0; -IPaddr_t NetDHCPServerIP = 0; -static void DhcpHandler(uchar * pkt, unsigned dest, unsigned src, unsigned len); +static uint32_t Bootp_id; +static dhcp_state_t dhcp_state; +static uint32_t dhcp_leasetime; +static IPaddr_t net_dhcp_server_ip; +static uint64_t dhcp_start; -static int BootpCheckPkt(uchar *pkt, unsigned dest, unsigned src, unsigned len) +static int bootp_check_packet(unsigned char *pkt, unsigned src, unsigned len) { - Bootp_t *bp = (Bootp_t *) pkt; + struct bootp *bp = (struct bootp *) pkt; int retval = 0; - if (dest != PORT_BOOTPC || src != PORT_BOOTPS) + if (src != PORT_BOOTPS) retval = -1; - else if (len < sizeof (Bootp_t) - OPT_SIZE) + else if (len < sizeof(struct bootp)) retval = -2; else if (bp->bp_op != OP_BOOTREQUEST && bp->bp_op != OP_BOOTREPLY && @@ -131,11 +94,11 @@ static int BootpCheckPkt(uchar *pkt, unsigned dest, unsigned src, unsigned len) retval = -4; else if (bp->bp_hlen != HWL_ETHER) retval = -5; - else if (NetReadLong((ulong*)&bp->bp_id) != BootpID) { + else if (net_read_uint32(&bp->bp_id) != Bootp_id) { retval = -6; } - debug ("Filtering pkt = %d\n", retval); + debug("Filtering pkt = %d\n", retval); return retval; } @@ -143,64 +106,31 @@ 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 bootp_copy_net_params(struct bootp *bp) { IPaddr_t tmp_ip; - NetCopyIP(&NetOurIP, &bp->bp_yiaddr); - NetCopyIP(&tmp_ip, &bp->bp_siaddr); - if (tmp_ip != 0) - NetCopyIP(&NetServerIP, &bp->bp_siaddr); - memcpy (NetServerEther, ((Ethernet_t *)NetRxPkt)->et_src, 6); - if (strlen(bp->bp_file) > 0) - safe_strncpy (BootFile, bp->bp_file, sizeof(BootFile)); + tmp_ip = net_read_ip(&bp->bp_yiaddr); + net_set_ip(tmp_ip); - debug ("Bootfile: %s\n", BootFile); - - /* Propagate to environment: - * don't delete exising entry when BOOTP / DHCP reply does - * not contain a new value - */ - if (*BootFile) { - setenv ("bootfile", BootFile); - } -} + tmp_ip = net_read_ip(&bp->bp_siaddr); + if (tmp_ip != 0) + net_set_serverip(tmp_ip); -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); - curlen = maxlen - 1; - } - return (curlen); -} + if (strlen(bp->bp_file) > 0) + setenv("bootfile", bp->bp_file); -/* - * Timeout on BOOTP/DHCP request. - */ -static void -BootpTimeout(void) -{ - NetSetTimeout (TIMEOUT * SECOND, BootpTimeout); - BootpRequest (); + debug("bootfile: %s\n", bp->bp_file); } /* - * Initialize BOOTP extension fields in the request. + * Initialize BOOTP extension fields in the request. */ -static int DhcpExtended (u8 * e, int message_type, IPaddr_t ServerID, IPaddr_t RequestedIP) +static int dhcp_extended (u8 *e, int message_type, IPaddr_t ServerID, IPaddr_t RequestedIP) { u8 *start = e; u8 *cnt; -#ifdef CONFIG_BOOTP_VENDOREX - u8 *x; -#endif -#ifdef CONFIG_BOOTP_SEND_HOSTNAME - char *hostname; -#endif - *e++ = 99; /* RFC1048 Magic Cookie */ *e++ = 130; *e++ = 83; @@ -236,175 +166,118 @@ static int DhcpExtended (u8 * e, int message_type, IPaddr_t ServerID, IPaddr_t R *e++ = tmp >> 8; *e++ = tmp & 0xff; } -#ifdef CONFIG_BOOTP_SEND_HOSTNAME - if ((hostname = getenv ("hostname"))) { - int hostnamelen = strlen (hostname); - - *e++ = 12; /* Hostname */ - *e++ = hostnamelen; - memcpy (e, hostname, hostnamelen); - e += hostnamelen; - } -#endif *e++ = 55; /* Parameter Request List */ cnt = e++; /* Pointer to count of requested items */ *cnt = 0; -#ifdef CONFIG_BOOTP_SUBNETMASK *e++ = 1; /* Subnet Mask */ *cnt += 1; -#endif -#ifdef CONFIG_BOOTP_TIMEOFFSET - *e++ = 2; - *cnt += 1; -#endif -#ifdef CONFIG_BOOTP_GATEWAY *e++ = 3; /* Router Option */ *cnt += 1; -#endif -#ifdef CONFIG_BOOTP_DNS *e++ = 6; /* DNS Server(s) */ *cnt += 1; -#endif -#ifdef CONFIG_BOOTP_HOSTNAME *e++ = 12; /* Hostname */ *cnt += 1; -#endif -#ifdef CONFIG_BOOTP_BOOTFILESIZE - *e++ = 13; /* Boot File Size */ + *e++ = 15; /* domain name */ *cnt += 1; -#endif -#ifdef CONFIG_BOOTP_BOOTPATH *e++ = 17; /* Boot path */ *cnt += 1; -#endif -#ifdef CONFIG_BOOTP_NISDOMAIN - *e++ = 40; /* NIS Domain name request */ - *cnt += 1; -#endif -#ifdef CONFIG_BOOTP_NTPSERVER - *e++ = 42; - *cnt += 1; -#endif *e++ = 255; /* End of the list */ /* Pad to minimal length */ -#ifdef CONFIG_DHCP_MIN_EXT_LEN - while ((e - start) <= CONFIG_DHCP_MIN_EXT_LEN) + while ((e - start) <= DHCP_MIN_EXT_LEN) *e++ = 0; -#endif return e - start; } -void -BootpRequest (void) +static struct net_connection *dhcp_con; + +static int bootp_request(void) { - uchar *pkt, *iphdr; - Bootp_t *bp; - int ext_len, pktlen, iplen; + struct bootp *bp; + int ext_len; + int ret; + unsigned char *payload = net_udp_get_payload(dhcp_con); + const char *bfile; dhcp_state = INIT; - printf("BOOTP broadcast\n"); - pkt = NetTxPacket; - memset ((void*)pkt, 0, PKTSIZE); + debug("BOOTP broadcast\n"); - 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. - * C. Hallinan, DS4.COM, Inc. - */ - /* NetSetIP(pkt, 0xFFFFFFFFL, PORT_BOOTPS, PORT_BOOTPC, sizeof (Bootp_t)); */ - iphdr = pkt; /* We need this later for NetSetIP() */ - pkt += IP_HDR_SIZE; - - bp = (Bootp_t *)pkt; + bp = (struct bootp *)payload; bp->bp_op = OP_BOOTREQUEST; bp->bp_htype = HWT_ETHER; bp->bp_hlen = HWL_ETHER; bp->bp_hops = 0; bp->bp_secs = htons(get_time_ns() >> 30); - NetWriteIP(&bp->bp_ciaddr, 0); - NetWriteIP(&bp->bp_yiaddr, 0); - NetWriteIP(&bp->bp_siaddr, 0); - NetWriteIP(&bp->bp_giaddr, 0); - memcpy (bp->bp_chaddr, NetOurEther, 6); - safe_strncpy (bp->bp_file, BootFile, sizeof(bp->bp_file)); + net_write_ip(&bp->bp_ciaddr, 0); + net_write_ip(&bp->bp_yiaddr, 0); + net_write_ip(&bp->bp_siaddr, 0); + net_write_ip(&bp->bp_giaddr, 0); + memcpy(bp->bp_chaddr, dhcp_con->et->et_src, 6); - /* Request additional information from the BOOTP/DHCP server */ - ext_len = DhcpExtended((u8 *)bp->bp_vend, DHCP_DISCOVER, 0, 0); + bfile = getenv("bootfile"); + if (bfile) + safe_strncpy (bp->bp_file, bfile, sizeof(bp->bp_file)); - /* - * Bootp ID is the lower 4 bytes of our ethernet address - * plus the current time in HZ. - */ - BootpID = ((ulong)NetOurEther[2] << 24) - | ((ulong)NetOurEther[3] << 16) - | ((ulong)NetOurEther[4] << 8) - | (ulong)NetOurEther[5]; - BootpID += (uint32_t)get_time_ns(); - BootpID = htonl(BootpID); - NetCopyLong(&bp->bp_id, &BootpID); + /* Request additional information from the BOOTP/DHCP server */ + ext_len = dhcp_extended((u8 *)bp->bp_vend, DHCP_DISCOVER, 0, 0); - /* - * Calculate proper packet lengths taking into account the - * variable size of the options field - */ - pktlen = BOOTP_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 * SECOND, BootpTimeout); + Bootp_id = (uint32_t)get_time_ns(); + net_copy_uint32(&bp->bp_id, &Bootp_id); dhcp_state = SELECTING; - NetSetHandler(DhcpHandler); - NetSendPacket(NetTxPacket, pktlen); + + ret = net_udp_send(dhcp_con, sizeof(*bp) + ext_len); + + return ret; } -static void DhcpOptionsProcess (uchar * popt, Bootp_t *bp) +static void dhcp_options_process(unsigned char *popt, struct bootp *bp) { - uchar *end = popt + BOOTP_HDR_SIZE; - int oplen, size; + unsigned char *end = popt + sizeof(*bp) + OPT_SIZE; + int oplen; + IPaddr_t ip; + char str[256]; while (popt < end && *popt != 0xff) { oplen = *(popt + 1); switch (*popt) { case 1: - NetCopyIP (&NetOurSubnetMask, (popt + 2)); + ip = net_read_ip(popt + 2); + net_set_netmask(ip); break; case 3: - NetCopyIP (&NetOurGatewayIP, (popt + 2)); + ip = net_read_ip(popt + 2); + net_set_gateway(ip); break; case 6: - NetCopyIP (&NetOurDNSIP, (popt + 2)); -#ifdef CONFIG_BOOTP_DNS2 - if (*(popt + 1) > 4) { - NetCopyIP (&NetOurDNS2IP, (popt + 2 + 4)); - } -#endif + ip = net_read_ip(popt + 2); + setenv_ip("nameserver", ip); break; case 12: - size = truncate_sz ("Host Name", sizeof (NetOurHostName), oplen); - memcpy (&NetOurHostName, popt + 2, size); - NetOurHostName[size] = 0; + memcpy(str, popt + 2, oplen); + str[oplen] = 0; + setenv("hostname", str); break; - case 15: /* Ignore Domain Name Option */ + case 15: + memcpy(str, popt + 2, oplen); + str[oplen] = 0; + setenv("domainname", str); break; case 17: - size = truncate_sz ("Root Path", sizeof (NetOurRootPath), oplen); - memcpy (&NetOurRootPath, popt + 2, size); - NetOurRootPath[size] = 0; + memcpy(str, popt + 2, oplen); + str[oplen] = 0; + setenv("rootpath", str); break; case 51: - NetCopyLong (&dhcp_leasetime, (ulong *) (popt + 2)); + net_copy_uint32 (&dhcp_leasetime, (uint32_t *)(popt + 2)); break; case 53: /* Ignore Message Type Option */ break; case 54: - NetCopyIP (&NetDHCPServerIP, (popt + 2)); + net_copy_ip(&net_dhcp_server_ip, (popt + 2)); break; case 58: /* Ignore Renewal Time Option */ break; @@ -419,10 +292,9 @@ 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", - sizeof(bp->bp_file), - oplen); - if (bp->bp_file[0] == '\0' && size > 0) { + memcpy(str, popt + 2, oplen); + str[oplen] = 0; + if (bp->bp_file[0] == '\0') { /* * only use vendor boot file if we didn't * receive a boot file in the main non-vendor @@ -434,8 +306,7 @@ static void DhcpOptionsProcess (uchar * popt, Bootp_t *bp) */ printf("*** WARNING: using vendor " "optional boot file\n"); - memcpy(bp->bp_file, popt + 2, size); - bp->bp_file[size] = '\0'; + setenv("bootfile", str); } break; default: @@ -443,16 +314,16 @@ static void DhcpOptionsProcess (uchar * popt, Bootp_t *bp) if (dhcp_vendorex_proc (popt)) break; #endif - printf ("*** Unhandled DHCP Option in OFFER/ACK: %d\n", *popt); + debug("*** Unhandled DHCP Option in OFFER/ACK: %d\n", *popt); break; } popt += oplen + 2; /* Process next option */ } } -static int DhcpMessageType(unsigned char *popt) +static int dhcp_message_type(unsigned char *popt) { - if (NetReadLong((ulong*)popt) != htonl(BOOTP_VENDOR_MAGIC)) + if (net_read_uint32((uint32_t *)popt) != htonl(BOOTP_VENDOR_MAGIC)) return -1; popt += 4; @@ -464,76 +335,65 @@ static int DhcpMessageType(unsigned char *popt) return -1; } -static void DhcpSendRequestPkt(Bootp_t *bp_offer) +static void dhcp_send_request_packet(struct bootp *bp_offer) { - uchar *pkt, *iphdr; - Bootp_t *bp; - int pktlen, iplen, extlen; + struct bootp *bp; + int extlen; IPaddr_t OfferedIP; + unsigned char *payload = net_udp_get_payload(dhcp_con); - debug ("DhcpSendRequestPkt: Sending DHCPREQUEST\n"); - pkt = NetTxPacket; - memset ((void*)pkt, 0, PKTSIZE); - - pkt += NetSetEther(pkt, NetBcastAddr, PROT_IP); + debug("%s: Sending DHCPREQUEST\n", __func__); - iphdr = pkt; /* We'll need this later to set proper pkt size */ - pkt += IP_HDR_SIZE; - - bp = (Bootp_t *)pkt; + bp = (struct bootp *)payload; bp->bp_op = OP_BOOTREQUEST; bp->bp_htype = HWT_ETHER; bp->bp_hlen = HWL_ETHER; bp->bp_hops = 0; /* FIXME what is this? */ // bp->bp_secs = htons(get_timer(0) / CFG_HZ); - NetCopyIP(&bp->bp_ciaddr, &bp_offer->bp_ciaddr); /* both in network byte order */ - NetCopyIP(&bp->bp_yiaddr, &bp_offer->bp_yiaddr); - NetCopyIP(&bp->bp_siaddr, &bp_offer->bp_siaddr); + net_copy_ip(&bp->bp_ciaddr, &bp_offer->bp_ciaddr); /* both in network byte order */ + net_copy_ip(&bp->bp_yiaddr, &bp_offer->bp_yiaddr); + net_copy_ip(&bp->bp_siaddr, &bp_offer->bp_siaddr); /* * RFC3046 requires Relay Agents to discard packets with * nonzero and offered giaddr */ - NetWriteIP(&bp->bp_giaddr, 0); + net_write_ip(&bp->bp_giaddr, 0); - memcpy (bp->bp_chaddr, NetOurEther, 6); + memcpy(bp->bp_chaddr, dhcp_con->et->et_src, 6); /* * ID is the id of the OFFER packet */ - NetCopyLong(&bp->bp_id, &bp_offer->bp_id); + net_copy_uint32(&bp->bp_id, &bp_offer->bp_id); /* * Copy options from OFFER packet if present */ - NetCopyIP(&OfferedIP, &bp->bp_yiaddr); - extlen = DhcpExtended((u8 *)bp->bp_vend, DHCP_REQUEST, NetDHCPServerIP, OfferedIP); - - pktlen = BOOTP_SIZE - sizeof(bp->bp_vend) + extlen; - iplen = BOOTP_HDR_SIZE - sizeof(bp->bp_vend) + extlen; - NetSetIP(iphdr, 0xFFFFFFFFL, PORT_BOOTPS, PORT_BOOTPC, iplen); + net_copy_ip(&OfferedIP, &bp->bp_yiaddr); + extlen = dhcp_extended((u8 *)bp->bp_vend, DHCP_REQUEST, net_dhcp_server_ip, OfferedIP); - debug ("Transmitting DHCPREQUEST packet: len = %d\n", pktlen); - NetSendPacket(NetTxPacket, pktlen); + debug("Transmitting DHCPREQUEST packet\n"); + net_udp_send(dhcp_con, sizeof(*bp) + extlen); } /* * Handle DHCP received packets. */ -static void -DhcpHandler(uchar * pkt, unsigned dest, unsigned src, unsigned len) +static void dhcp_handler(char *packet, unsigned int len) { - Bootp_t *bp = (Bootp_t *)pkt; + char *pkt = net_eth_to_udp_payload(packet); + struct udphdr *udp = net_eth_to_udphdr(packet); + struct bootp *bp = (struct bootp *)pkt; - debug ("DHCPHandler: got packet: (src=%d, dst=%d, len=%d) state: %d\n", - src, dest, len, dhcp_state); + len = net_eth_to_udplen(packet); - if (BootpCheckPkt(pkt, dest, src, len)) /* Filter out pkts we don't want */ - return; + debug("DHCPHandler: got packet: (len=%d) state: %d\n", + len, dhcp_state); - debug ("DHCPHandler: got DHCP packet: (src=%d, dst=%d, len=%d) state: %d\n", - src, dest, len, dhcp_state); + if (bootp_check_packet(pkt, ntohs(udp->uh_sport), len)) /* Filter out pkts we don't want */ + return; switch (dhcp_state) { case SELECTING: @@ -543,69 +403,78 @@ DhcpHandler(uchar * pkt, unsigned dest, unsigned src, unsigned len) * 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 CFG_BOOTFILE_PREFIX - if (strncmp(bp->bp_file, - CFG_BOOTFILE_PREFIX, - strlen(CFG_BOOTFILE_PREFIX)) == 0 ) { -#endif /* CFG_BOOTFILE_PREFIX */ - - debug ("TRANSITIONING TO REQUESTING STATE\n"); - dhcp_state = REQUESTING; + debug ("%s: state SELECTING, bp_file: \"%s\"\n", __func__, bp->bp_file); + dhcp_state = REQUESTING; - if (NetReadLong((ulong*)&bp->bp_vend[0]) == htonl(BOOTP_VENDOR_MAGIC)) - DhcpOptionsProcess((u8 *)&bp->bp_vend[4], bp); + if (net_read_uint32((uint32_t *)&bp->bp_vend[0]) == htonl(BOOTP_VENDOR_MAGIC)) + dhcp_options_process((u8 *)&bp->bp_vend[4], bp); - BootpCopyNetParams(bp); /* Store net params from reply */ + bootp_copy_net_params(bp); /* Store net params from reply */ - NetSetTimeout(TIMEOUT * SECOND, BootpTimeout); - DhcpSendRequestPkt(bp); -#ifdef CFG_BOOTFILE_PREFIX - } -#endif /* CFG_BOOTFILE_PREFIX */ + dhcp_start = get_time_ns(); + dhcp_send_request_packet(bp); - return; break; case REQUESTING: - debug ("DHCP State: REQUESTING\n"); + debug ("%s: State REQUESTING\n", __func__); - 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 */ + if (dhcp_message_type((u8 *)bp->bp_vend) == DHCP_ACK ) { + if (net_read_uint32((uint32_t *)&bp->bp_vend[0]) == htonl(BOOTP_VENDOR_MAGIC)) + dhcp_options_process((u8 *)&bp->bp_vend[4], bp); + bootp_copy_net_params(bp); /* Store net params from reply */ dhcp_state = BOUND; puts ("DHCP client bound to address "); - print_IPaddr(NetOurIP); + print_IPaddr(net_get_ip()); putchar('\n'); - - NetState = NETLOOP_SUCCESS; return; } break; default: - puts ("DHCP: INVALID STATE\n"); + debug("%s: INVALID STATE\n", __func__); break; } - } static int do_dhcp(struct command *cmdtp, int argc, char *argv[]) { - int size; + int ret; + + dhcp_con = net_udp_new(0xffffffff, PORT_BOOTPS, dhcp_handler); + if (IS_ERR(dhcp_con)) { + ret = PTR_ERR(dhcp_con); + goto out; + } + + ret = net_udp_bind(dhcp_con, PORT_BOOTPC); + if (ret) + goto out1; - if (NetLoopInit(DHCP) < 0) - return 1; + net_set_ip(0); - NetOurIP = 0; - BootpRequest(); /* Basically same as BOOTP */ + ret = bootp_request(); /* Basically same as BOOTP */ + if (ret) + goto out1; - if ((size = NetLoop()) < 0) - return 1; + while (dhcp_state != BOUND) { + if (ctrlc()) + break; + net_poll(); + if (is_timeout(dhcp_start, 3 * SECOND)) { + dhcp_start = get_time_ns(); + printf("T "); + ret = bootp_request(); + if (ret) + goto out1; + } + } - /* NetLoop ok, update environment */ - netboot_update_env(); +out1: + net_unregister(dhcp_con); +out: + if (ret) + printf("dhcp failed: %s\n", strerror(-ret)); - return 0; + return ret ? 1 : 0; } BAREBOX_CMD_START(dhcp) diff --git a/net/dns.c b/net/dns.c new file mode 100644 index 0000000000..1ee270b678 --- /dev/null +++ b/net/dns.c @@ -0,0 +1,264 @@ +/* + * DNS support driver + * + * Copyright (c) 2008 Pieter Voorthuijsen <pieter.voorthuijsen@prodrive.nl> + * Copyright (c) 2009 Robin Getz <rgetz@blackfin.uclinux.org> + * + * This is a simple DNS implementation for U-Boot. It will use the first IP + * in the DNS response as NetServerIP. This can then be used for any other + * network related activities. + * + * The packet handling is partly based on TADNS, original copyrights + * follow below. + * + */ + +/* + * Copyright (c) 2004-2005 Sergey Lyubka <valenok@gmail.com> + * + * "THE BEER-WARE LICENSE" (Revision 42): + * Sergey Lyubka wrote this file. As long as you retain this notice you + * can do whatever you want with this stuff. If we meet some day, and you think + * this stuff is worth it, you can buy me a beer in return. + */ +//#define DEBUG +#include <common.h> +#include <command.h> +#include <net.h> +#include <clock.h> +#include <environment.h> +#include <linux/err.h> + +#define DNS_PORT 53 + +/* http://en.wikipedia.org/wiki/List_of_DNS_record_types */ +enum dns_query_type { + DNS_A_RECORD = 0x01, + DNS_CNAME_RECORD = 0x05, + DNS_MX_RECORD = 0x0f, +}; + +/* + * DNS network packet + */ +struct header { + uint16_t tid; /* Transaction ID */ + uint16_t flags; /* Flags */ + uint16_t nqueries; /* Questions */ + uint16_t nanswers; /* Answers */ + uint16_t nauth; /* Authority PRs */ + uint16_t nother; /* Other PRs */ + unsigned char data[1]; /* Data, variable length */ +}; + +#define STATE_INIT 0 +#define STATE_DONE 1 + +static struct net_connection *dns_con; +static uint64_t dns_timer_start; +static int dns_state; +static IPaddr_t dns_ip; + +static int dns_send(char *name) +{ + int ret; + struct header *header; + enum dns_query_type qtype = DNS_A_RECORD; + unsigned char *packet = net_udp_get_payload(dns_con); + unsigned char *p, *s, *fullname, *dotptr; + const unsigned char *domain; + + /* Prepare DNS packet header */ + header = (struct header *)packet; + header->tid = 1; + header->flags = htons(0x100); /* standard query */ + header->nqueries = htons(1); /* Just one query */ + header->nanswers = 0; + header->nauth = 0; + header->nother = 0; + + domain = getenv("domainname"); + + if (!strchr(name, '.') && domain && *domain) + fullname = asprintf(".%s.%s.", name, domain); + else + fullname = asprintf(".%s.", name); + + /* replace dots in fullname with chunk len */ + dotptr = fullname; + do { + int len; + + s = strchr(dotptr + 1, '.'); + + len = s - dotptr - 1; + + *dotptr = len; + dotptr = s; + } while (*(dotptr + 1)); + *dotptr = 0; +//memory_display(fullname, 0, strlen(fullname), 1); + strcpy(header->data, fullname); + + p = header->data + strlen(fullname); + + *p++ = 0; /* Mark end of host name */ + *p++ = 0; /* Some servers require double null */ + *p++ = (unsigned char)qtype; /* Query Type */ + + *p++ = 0; + *p++ = 1; /* Class: inet, 0x0001 */ + + ret = net_udp_send(dns_con, p - packet); + + free(fullname); + + return ret; +} + +static void dns_handler(char *packet, unsigned len) +{ + struct header *header; + unsigned char *p, *e, *s; + u16 type; + int found, stop, dlen; + short tmp; + + debug("%s\n", __func__); + + /* We sent 1 query. We want to see more that 1 answer. */ + header = (struct header *)net_eth_to_udp_payload(packet);; + if (ntohs(header->nqueries) != 1) + return; + + /* Received 0 answers */ + if (header->nanswers == 0) { + dns_state = STATE_DONE; + debug("DNS server returned no answers\n"); + return; + } + + /* Skip host name */ + s = &header->data[0]; + e = packet + len; + for (p = s; p < e && *p != '\0'; p++) + continue; + + /* We sent query class 1, query type 1 */ + tmp = p[1] | (p[2] << 8); + if (&p[5] > e || ntohs(tmp) != DNS_A_RECORD) { + debug("DNS response was not A record\n"); + return; + } + + /* Go to the first answer section */ + p += 5; + + /* Loop through the answers, we want A type answer */ + for (found = stop = 0; !stop && &p[12] < e; ) { + + /* Skip possible name in CNAME answer */ + if (*p != 0xc0) { + while (*p && &p[12] < e) + p++; + p--; + } + debug("Name (Offset in header): %d\n", p[1]); + + tmp = p[2] | (p[3] << 8); + type = ntohs(tmp); + debug("type = %d\n", type); + if (type == DNS_CNAME_RECORD) { + /* CNAME answer. shift to the next section */ + debug("Found canonical name\n"); + tmp = p[10] | (p[11] << 8); + dlen = ntohs(tmp); + debug("dlen = %d\n", dlen); + p += 12 + dlen; + } else if (type == DNS_A_RECORD) { + debug("Found A-record\n"); + found = stop = 1; + } else { + debug("Unknown type\n"); + stop = 1; + } + } + + if (found && &p[12] < e) { + + tmp = p[10] | (p[11] << 8); + dlen = ntohs(tmp); + p += 12; + dns_ip = net_read_ip(p); + dns_state = STATE_DONE; + } +} + +IPaddr_t resolv(char *host) +{ + IPaddr_t ip; + + if (!string_to_ip(host, &ip)) + return ip; + + dns_ip = 0; + + dns_state = STATE_INIT; + + ip = getenv_ip("nameserver"); + if (!ip) + return 0; + + debug("resolving host %s via nameserver %s\n", host, getenv("nameserver")); + + dns_con = net_udp_new(ip, DNS_PORT, dns_handler); + if (IS_ERR(dns_con)) + return PTR_ERR(dns_con); + dns_timer_start = get_time_ns(); + dns_send(host); + + while (dns_state != STATE_DONE) { + if (ctrlc()) { + break; + } + net_poll(); + if (is_timeout(dns_timer_start, SECOND)) { + dns_timer_start = get_time_ns(); + printf("T "); + dns_send(host); + } + } + + net_unregister(dns_con); + + return dns_ip; +} + +static int do_host(struct command *cmdtp, int argc, char *argv[]) +{ + IPaddr_t ip; + + if (argc != 2) + return COMMAND_ERROR_USAGE; + + ip = resolv(argv[1]); + if (!ip) + printf("unknown host %s\n", argv[1]); + else { + printf("%s is at ", argv[1]); + print_IPaddr(ip); + printf("\n"); + } + + return 0; +} + +static const __maybe_unused char cmd_host_help[] = +"Usage: host <hostname>\n"; + +BAREBOX_CMD_START(host) + .cmd = do_host, + .usage = "resolve a hostname", + BAREBOX_CMD_HELP(cmd_host_help) +BAREBOX_CMD_END + @@ -36,7 +36,13 @@ static LIST_HEAD(netdev_list); void eth_set_current(struct eth_device *eth) { + if (eth_current && eth_current->active) { + eth_current->halt(eth_current); + eth_current->active = 0; + } + eth_current = eth; + net_update_env(); } struct eth_device * eth_get_current(void) @@ -57,37 +63,37 @@ struct eth_device *eth_get_byname(char *ethname) return NULL; } -int eth_open(void) -{ - if (!eth_current) - return -ENODEV; - - return eth_current->open(eth_current); -} - -void eth_halt(void) -{ - if (!eth_current) - return; - - eth_current->halt(eth_current); - - eth_current->state = ETH_STATE_PASSIVE; -} - int eth_send(void *packet, int length) { + int ret; + if (!eth_current) return -ENODEV; + if (!eth_current->active) { + ret = eth_current->open(eth_current); + if (ret) + return ret; + eth_current->active = 1; + } + return eth_current->send(eth_current, packet, length); } int eth_rx(void) { + int ret; + if (!eth_current) return -ENODEV; + if (!eth_current->active) { + ret = eth_current->open(eth_current); + if (ret) + return ret; + eth_current->active = 1; + } + return eth_current->recv(eth_current); } @@ -96,26 +102,37 @@ static int eth_set_ethaddr(struct device_d *dev, struct param_d *param, const ch struct eth_device *edev = dev->type_data; char ethaddr[sizeof("xx:xx:xx:xx:xx:xx")]; + if (!val) + return dev_param_set_generic(dev, param, NULL); + if (string_to_ethaddr(val, ethaddr) < 0) return -EINVAL; - free(param->value); - param->value = strdup(val); + dev_param_set_generic(dev, param, val); edev->set_ethaddr(edev, ethaddr); + if (edev == eth_current) + net_update_env(); + return 0; } static int eth_set_ipaddr(struct device_d *dev, struct param_d *param, const char *val) { + struct eth_device *edev = dev->type_data; IPaddr_t ip; + if (!val) + return dev_param_set_generic(dev, param, NULL); + if (string_to_ip(val, &ip)) return -EINVAL; - free(param->value); - param->value = strdup(val); + dev_param_set_generic(dev, param, val); + + if (edev == eth_current) + net_update_env(); return 0; } @@ -135,21 +152,11 @@ int eth_register(struct eth_device *edev) register_device(&edev->dev); dev->type_data = edev; - edev->param_ip.name = "ipaddr"; - edev->param_ip.set = ð_set_ipaddr; - edev->param_ethaddr.name = "ethaddr"; - edev->param_ethaddr.set = ð_set_ethaddr; - edev->param_gateway.name = "gateway"; - edev->param_gateway.set = ð_set_ipaddr; - edev->param_netmask.name = "netmask"; - edev->param_netmask.set = ð_set_ipaddr; - edev->param_serverip.name = "serverip"; - edev->param_serverip.set = ð_set_ipaddr; - dev_add_param(dev, &edev->param_ip); - dev_add_param(dev, &edev->param_ethaddr); - dev_add_param(dev, &edev->param_gateway); - dev_add_param(dev, &edev->param_netmask); - dev_add_param(dev, &edev->param_serverip); + dev_add_param(dev, "ipaddr", eth_set_ipaddr, NULL, 0); + dev_add_param(dev, "ethaddr", eth_set_ethaddr, NULL, 0); + dev_add_param(dev, "gateway", eth_set_ipaddr, NULL, 0); + dev_add_param(dev, "netmask", eth_set_ipaddr, NULL, 0); + dev_add_param(dev, "serverip", eth_set_ipaddr, NULL, 0); edev->init(edev); @@ -157,7 +164,7 @@ int eth_register(struct eth_device *edev) if (edev->get_ethaddr(edev, ethaddr) == 0) { ethaddr_to_string(ethaddr, ethaddr_str); - printf("got MAC address from EEPROM: %s\n",ethaddr_str); + printf("got MAC address from EEPROM: %s\n",ðaddr_str); dev_set_param(dev, "ethaddr", ethaddr_str); } @@ -169,20 +176,9 @@ int eth_register(struct eth_device *edev) void eth_unregister(struct eth_device *edev) { - if (edev->param_ip.value) - free(edev->param_ip.value); - if (edev->param_ethaddr.value) - free(edev->param_ethaddr.value); - if (edev->param_gateway.value) - free(edev->param_gateway.value); - if (edev->param_netmask.value) - free(edev->param_netmask.value); - if (edev->param_serverip.value) - free(edev->param_serverip.value); - - if (eth_current == edev) - eth_current = NULL; + dev_remove_parameters(&edev->dev); list_del(&edev->list); } + @@ -1,73 +1,31 @@ /* - * Copied from Linux Monitor (LiMon) - Networking. + * net.c - barebox networking support * - * Copyright 1994 - 2000 Neil Russell. - * (See License) - * Copyright 2000 Roland Borde - * Copyright 2000 Paolo Scaffardi - * Copyright 2000-2002 Wolfgang Denk, wd@denx.de - */ - -/* - * General Desription: - * - * The user interface supports commands for BOOTP, RARP, and TFTP. - * Also, we support ARP internally. Depending on available data, - * these interact as follows: - * - * BOOTP: - * - * Prerequisites: - own ethernet address - * We want: - own IP address - * - TFTP server IP address - * - name of bootfile - * Next step: ARP - * - * RARP: - * - * Prerequisites: - own ethernet address - * We want: - own IP address - * - TFTP server IP address - * Next step: ARP - * - * ARP: - * - * Prerequisites: - own ethernet address - * - own IP address - * - TFTP server IP address - * We want: - TFTP server ethernet address - * Next step: TFTP + * Copyright (c) 2010 Sascha Hauer <s.hauer@pengutronix.de>, Pengutronix * - * DHCP: + * based on U-Boot (LiMon) code * - * Prerequisites: - own ethernet address - * We want: - IP, Netmask, ServerIP, Gateway IP - * - bootfilename, lease time - * Next step: - TFTP + * Copyright 1994 - 2000 Neil Russell. + * Copyright 2000 Roland Borde + * Copyright 2000 Paolo Scaffardi + * Copyright 2000-2002 Wolfgang Denk, wd@denx.de * - * TFTP: + * See file CREDITS for list of people who contributed to this + * project. * - * Prerequisites: - own ethernet address - * - own IP address - * - TFTP server IP address - * - TFTP server ethernet address - * - name of bootfile (if unknown, we use a default name - * derived from our own IP address) - * We want: - load the boot file - * Next step: none + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License version 2 + * as published by the Free Software Foundation. * - * NFS: - * - * Prerequisites: - own ethernet address - * - own IP address - * - name of bootfile (if unknown, we use a default name - * derived from our own IP address) - * We want: - load the boot file - * Next step: none + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */ - - #include <common.h> #include <clock.h> #include <watchdog.h> @@ -77,885 +35,623 @@ #include <net.h> #include <driver.h> #include <errno.h> +#include <malloc.h> +#include <init.h> #include <linux/ctype.h> -#include "tftp.h" -#include "rarp.h" -#include "nfs.h" - -#define ARP_TIMEOUT (5 * SECOND) /* Seconds before trying ARP again */ -#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 +#include <linux/err.h> +static IPaddr_t net_netmask; /* Our subnet mask (0=unknown) */ +static IPaddr_t net_gateway; /* Our gateways IP address */ -/** BOOTP EXTENTIONS **/ +static unsigned char net_ether[6]; /* Our ethernet address */ +static IPaddr_t net_ip; /* Our IP addr (0 = unknown) */ +static IPaddr_t net_serverip; /* Our IP addr (0 = unknown) */ -IPaddr_t NetOurSubnetMask=0; /* Our subnet mask (0=unknown) */ -IPaddr_t NetOurGatewayIP=0; /* Our gateways IP address */ -IPaddr_t NetOurDNSIP=0; /* Our DNS IP address */ -#ifdef CONFIG_BOOTP_DNS2 -IPaddr_t NetOurDNS2IP=0; /* Our 2nd DNS IP address */ -#endif -char NetOurNISDomain[32]={0,}; /* Our NIS domain */ -char NetOurHostName[32]={0,}; /* Our hostname */ -char NetOurRootPath[64]={0,}; /* Our bootpath */ +unsigned char *NetRxPackets[PKTBUFSRX]; /* Receive packets */ +static unsigned int net_ip_id; -/** END OF BOOTP EXTENTIONS **/ +void net_update_env(void) +{ + struct eth_device *edev = eth_get_current(); -ulong NetBootFileXferSize; /* The actual transferred size of the bootfile (in bytes) */ -uchar NetOurEther[6]; /* Our ethernet address */ -uchar NetServerEther[6] = /* Boot server enet address */ - { 0, 0, 0, 0, 0, 0 }; -IPaddr_t NetOurIP; /* Our IP addr (0 = unknown) */ -IPaddr_t NetServerIP; /* Our IP addr (0 = unknown) */ -uchar *NetRxPkt; /* Current receive packet */ -int NetRxPktLen; /* Current rx packet length */ -unsigned NetIPID; /* IP packet ID */ -uchar NetBcastAddr[6] = /* Ethernet bcast address */ - { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff }; -uchar NetEtherNullAddr[6] = - { 0, 0, 0, 0, 0, 0 }; -int NetState; /* Network loop state */ + net_ip = dev_get_param_ip(&edev->dev, "ipaddr"); + net_serverip = dev_get_param_ip(&edev->dev, "serverip"); + net_gateway = dev_get_param_ip(&edev->dev, "gateway"); + net_netmask = dev_get_param_ip(&edev->dev, "netmask"); -/* XXX in both little & big endian machines 0xFFFF == ntohs(-1) */ -ushort NetOurVLAN = 0xFFFF; /* default is without VLAN */ -ushort NetOurNativeVLAN = 0xFFFF; /* ditto */ + string_to_ethaddr(dev_get_param(&edev->dev, "ethaddr"), + net_ether); +} -char BootFile[128]; /* Boot File name */ +int net_checksum_ok(unsigned char *ptr, int len) +{ + return net_checksum(ptr, len) + 1; +} -uchar PktBuf[(PKTBUFSRX+1) * PKTSIZE_ALIGN + PKTALIGN]; +uint16_t net_checksum(unsigned char *ptr, int len) +{ + uint32_t xsum = 0; + uint16_t *p = (uint16_t *)ptr; -uchar *NetRxPackets[PKTBUFSRX]; /* Receive packets */ + if (len & 1) + ptr[len] = 0; -static rxhand_f *packetHandler; /* Current RX packet handler */ -static thand_f *timeHandler; /* Current timeout handler */ -static uint64_t timeStart; /* Time base value */ -static uint64_t timeDelta; /* Current timeout value */ -uchar *NetTxPacket = 0; /* THE transmit packet */ + len = (len + 1) >> 1; -static int net_check_prereq (proto_t protocol); + while (len-- > 0) + xsum += *p++; -/**********************************************************************/ + xsum = (xsum & 0xffff) + (xsum >> 16); + xsum = (xsum & 0xffff) + (xsum >> 16); + return xsum & 0xffff; +} -IPaddr_t NetArpWaitPacketIP; -IPaddr_t NetArpWaitReplyIP; -uchar *NetArpWaitPacketMAC; /* MAC address of waiting packet's destination */ -uchar *NetArpWaitTxPacket; /* THE transmit packet */ -int NetArpWaitTxPacketSize; -uchar NetArpWaitPacketBuf[PKTSIZE_ALIGN + PKTALIGN]; -uint64_t NetArpWaitTimerStart; +char *ip_to_string (IPaddr_t x, char *s) +{ + x = ntohl (x); + sprintf (s, "%d.%d.%d.%d", + (int) ((x >> 24) & 0xff), + (int) ((x >> 16) & 0xff), + (int) ((x >> 8) & 0xff), (int) ((x >> 0) & 0xff) + ); + return s; +} -void ArpRequest (void) +int string_to_ip(const char *s, IPaddr_t *ip) { + IPaddr_t addr = 0; + char *e; int i; - uchar *pkt; - ARP_t *arp; - - pr_debug("ARP broadcast\n"); - pkt = NetTxPacket; + if (!s) + return -EINVAL; - pkt += NetSetEther (pkt, NetBcastAddr, PROT_ARP); + for (i = 0; i < 4; i++) { + unsigned long val; - arp = (ARP_t *) pkt; + if (!isdigit(*s)) + return -EINVAL; - 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); + val = simple_strtoul(s, &e, 10); + addr <<= 8; + addr |= (val & 0xFF); - memcpy (&arp->ar_data[0], NetOurEther, 6); /* source ET addr */ - NetWriteIP ((uchar *) & arp->ar_data[6], NetOurIP); /* source IP addr */ - for (i = 10; i < 16; ++i) { - arp->ar_data[i] = 0; /* dest ET addr = 0 */ - } + if (*e != '.' && i != 3) + return -EINVAL; - if ((NetArpWaitPacketIP & NetOurSubnetMask) != - (NetOurIP & NetOurSubnetMask)) { - if (NetOurGatewayIP == 0) { - puts ("## Warning: gatewayip needed but not set\n"); - NetArpWaitReplyIP = NetArpWaitPacketIP; - } else { - NetArpWaitReplyIP = NetOurGatewayIP; - } - } else { - NetArpWaitReplyIP = NetArpWaitPacketIP; + s = e + 1; } - NetWriteIP ((uchar *) & arp->ar_data[16], NetArpWaitReplyIP); - (void) eth_send (NetTxPacket, (pkt - NetTxPacket) + ARP_HDR_SIZE); + *ip = htonl(addr); + return 0; } -/**********************************************************************/ -/* - * Main network processing loop. - */ - -int NetLoopInit(proto_t protocol) +IPaddr_t getenv_ip(const char *name) { - struct eth_device *eth_current = eth_get_current(); IPaddr_t ip; - int ret; - int i; - - if (!eth_current) { - printf("Current ethernet device not set!\n"); - return -1; - } + const char *var = getenv(name); - ip = dev_get_param_ip(ð_current->dev, "ipaddr"); - NetCopyIP(&NetOurIP, &ip); - - /* XXX problem with bss workaround */ - NetArpWaitPacketMAC = NULL; - NetArpWaitTxPacket = NULL; - NetArpWaitPacketIP = 0; - NetArpWaitReplyIP = 0; - - /* - * 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; - } - - NetArpWaitTxPacket = &NetArpWaitPacketBuf[0] + (PKTALIGN - 1); - NetArpWaitTxPacket -= (ulong)NetArpWaitTxPacket % PKTALIGN; - NetArpWaitTxPacketSize = 0; + if (!var) + return 0; - if (eth_open() < 0) - return -1; + string_to_ip(var, &ip); - string_to_ethaddr(dev_get_param(ð_get_current()->dev, "ethaddr"), - NetOurEther); + return ip; +} - NetState = NETLOOP_CONTINUE; +int setenv_ip(const char *name, IPaddr_t ip) +{ + char str[sizeof("xxx.xxx.xxx.xxx")]; - NetOurGatewayIP = dev_get_param_ip(ð_current->dev, "gateway"); - NetOurSubnetMask = dev_get_param_ip(ð_current->dev, "netmask"); - NetOurVLAN = getenv_VLAN("vlan"); - NetOurNativeVLAN = getenv_VLAN("nvlan"); - NetServerIP = dev_get_param_ip(ð_current->dev, "serverip"); + ip_to_string(ip, str); - ret = net_check_prereq(protocol); - if (ret) - eth_halt(); + setenv(name, str); - return ret; + return 0; } -int NetLoop(void) +void print_IPaddr (IPaddr_t x) { - /* - * Start the ball rolling with the given start function. From - * here on, this code is a state machine driven by received - * packets and timer events. - */ + char tmp[16]; - NetBootFileXferSize = 0; + ip_to_string (x, tmp); - /* - * Main packet reception loop. Loop receiving packets until - * someone sets `NetState' to a state that terminates. - */ - for (;;) { - WATCHDOG_RESET(); -#ifdef CONFIG_SHOW_ACTIVITY - { - extern void show_activity(int arg); - show_activity(1); - } -#endif - /* - * Check the ethernet for a new packet. The ethernet - * receive routine will process it. - */ - eth_rx(); - - /* - * Abort if ctrl-c was pressed. - */ - if (ctrlc()) { - eth_halt(); - puts ("\nAbort\n"); - return -1; - } + puts (tmp); +} - /* check for arp timeout */ - if (NetArpWaitPacketIP && - is_timeout(NetArpWaitTimerStart, ARP_TIMEOUT)) { - NetArpWaitTimerStart = get_time_ns(); - ArpRequest(); - } +int string_to_ethaddr(const char *str, char *enetaddr) +{ + int reg; + char *e; - /* - * Check for a timeout, and run the timeout handler - * if we have one. - */ - if (timeHandler && is_timeout(timeStart, timeDelta)) { - thand_f *x; - x = timeHandler; - timeHandler = (thand_f *)0; - (*x)(); - } + if (!str || strlen(str) != 17) + return -1; + if (str[2] != ':' || str[5] != ':' || str[8] != ':' || + str[11] != ':' || str[14] != ':') + return -1; - switch (NetState) { - case NETLOOP_SUCCESS: - if (NetBootFileXferSize > 0) { - char buf[10]; - printf("Bytes transferred = %ld (%lx hex)\n", - NetBootFileXferSize, - NetBootFileXferSize); - sprintf(buf, "0x%lx", NetBootFileXferSize); - setenv("filesize", buf); - } - eth_halt(); - return NetBootFileXferSize; - - case NETLOOP_FAIL: - eth_halt(); - return -1; - } + for (reg = 0; reg < 6; ++reg) { + enetaddr[reg] = simple_strtoul (str, &e, 16); + str = e + 1; } -} -/**********************************************************************/ -/* - * Miscelaneous bits. - */ + return 0; +} -void -NetSetHandler(rxhand_f * f) +void ethaddr_to_string(const unsigned char *enetaddr, char *str) { - packetHandler = f; + sprintf(str, "%02X:%02X:%02X:%02X:%02X:%02X", + enetaddr[0], enetaddr[1], enetaddr[2], enetaddr[3], + enetaddr[4], enetaddr[5]); } +static unsigned char *arp_ether; +static IPaddr_t arp_wait_ip; -void -NetSetTimeout(uint64_t iv, thand_f * f) +static void arp_handler(struct arprequest *arp) { - if (iv == 0) { - timeHandler = (thand_f *)0; - } else { - timeHandler = f; - timeStart = get_time_ns(); - timeDelta = iv; - } -} + IPaddr_t tmp; + /* are we waiting for a reply */ + if (!arp_wait_ip) + return; -void -NetSendPacket(uchar * pkt, int len) -{ - (void) eth_send(pkt, len); + tmp = net_read_ip(&arp->ar_data[6]); + + /* matched waiting packet's address */ + if (tmp == arp_wait_ip) { + /* save address for later use */ + memcpy(arp_ether, &arp->ar_data[0], 6); + + /* no arp request pending now */ + arp_wait_ip = 0; + } } -int -NetSendUDPPacket(uchar *ether, IPaddr_t dest, int dport, int sport, int len) +int arp_request(IPaddr_t dest, unsigned char *ether) { - uchar *pkt; + char *pkt; + struct arprequest *arp; + uint64_t arp_start; + static char *arp_packet; + struct ethernet *et; + + if (!arp_packet) { + arp_packet = net_alloc_packet(); + if (!arp_packet) + return -ENOMEM; + } - /* convert to new style broadcast */ - if (dest == 0) - dest = 0xFFFFFFFF; + pkt = arp_packet; + et = (struct ethernet *)arp_packet; - /* if broadcast, make the ether address a broadcast and don't do ARP */ - if (dest == 0xFFFFFFFF) - ether = NetBcastAddr; + arp_wait_ip = dest; - /* if MAC address was not discovered yet, save the packet and do an ARP request */ - if (memcmp(ether, NetEtherNullAddr, 6) == 0) { - pr_debug("sending ARP for %08lx\n", dest); + pr_debug("ARP broadcast\n"); - NetArpWaitPacketIP = dest; - NetArpWaitPacketMAC = ether; + memset(et->et_dest, 0xff, 6); + memcpy(et->et_src, net_ether, 6); + et->et_protlen = htons(PROT_ARP); - pkt = NetArpWaitTxPacket; - pkt += NetSetEther (pkt, NetArpWaitPacketMAC, PROT_IP); + arp = (struct arprequest *)(pkt + ETHER_HDR_SIZE); - NetSetIP (pkt, dest, dport, sport, len); - memcpy(pkt + IP_HDR_SIZE, (uchar *)NetTxPacket + (pkt - (uchar *)NetArpWaitTxPacket) + IP_HDR_SIZE, len); + 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); - /* size of the waiting packet */ - NetArpWaitTxPacketSize = (pkt - NetArpWaitTxPacket) + IP_HDR_SIZE + len; + memcpy(arp->ar_data, net_ether, 6); /* source ET addr */ + net_write_ip(arp->ar_data + 6, net_ip); /* source IP addr */ + memset(arp->ar_data + 10, 0, 6); /* dest ET addr = 0 */ - /* and do the ARP request */ - NetArpWaitTimerStart = get_time_ns(); - ArpRequest(); - return 1; /* waiting */ + if ((dest & net_netmask) != (net_ip & net_netmask)) { + if (!net_gateway) + arp_wait_ip = dest; + else + arp_wait_ip = net_gateway; + } else { + arp_wait_ip = dest; } - pr_debug("sending UDP to %08lx/%02x:%02x:%02x:%02x:%02x:%02x\n", - dest, ether[0], ether[1], ether[2], ether[3], ether[4], ether[5]); + net_write_ip(arp->ar_data + 16, arp_wait_ip); - 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); + arp_ether = ether; - return 0; /* transmitted */ -} + eth_send(arp_packet, ETHER_HDR_SIZE + ARP_HDR_SIZE); + arp_start = get_time_ns(); -void -NetReceive(uchar * inpkt, int len) -{ - Ethernet_t *et; - IP_t *ip; - ARP_t *arp; - IPaddr_t tmp; - int x; - uchar *pkt; - ushort cti = 0, vlanid = VLAN_NONE, myvlanid, mynvlanid; + while (arp_wait_ip) { + if (ctrlc()) + return -EINTR; - pr_debug("packet received\n"); + if (is_timeout(arp_start, 3 * SECOND)) { + printf("T "); + arp_start = get_time_ns(); + eth_send(arp_packet, ETHER_HDR_SIZE + ARP_HDR_SIZE); + } - NetRxPkt = inpkt; - NetRxPktLen = len; - et = (Ethernet_t *)inpkt; + net_poll(); + } - /* too small packet? */ - if (len < ETHER_HDR_SIZE) - return; + pr_debug("Got ARP REPLY, set server/gtwy eth addr (%02x:%02x:%02x:%02x:%02x:%02x)\n", + ether[0], ether[1], + ether[2], ether[3], + ether[4], ether[5]); + return 0; +} - myvlanid = ntohs(NetOurVLAN); - if (myvlanid == (ushort)-1) - myvlanid = VLAN_NONE; - mynvlanid = ntohs(NetOurNativeVLAN); - if (mynvlanid == (ushort)-1) - mynvlanid = VLAN_NONE; +void net_poll(void) +{ + eth_rx(); +} - x = ntohs(et->et_protlen); +static uint16_t net_udp_new_localport(void) +{ + static uint16_t localport; - pr_debug("packet received\n"); + localport++; - if (x < 1514) { - /* - * Got a 802 packet. Check the other protocol field. - */ - x = ntohs(et->et_prot); + if (localport < 1024) + localport = 1024; - ip = (IP_t *)(inpkt + E802_HDR_SIZE); - len -= E802_HDR_SIZE; + return localport; +} - } else if (x != PROT_VLAN) { /* normal packet */ - ip = (IP_t *)(inpkt + ETHER_HDR_SIZE); - len -= ETHER_HDR_SIZE; +IPaddr_t net_get_serverip(void) +{ + return net_serverip; +} - } else { /* VLAN packet */ - VLAN_Ethernet_t *vet = (VLAN_Ethernet_t *)et; +void net_set_serverip(IPaddr_t ip) +{ + struct eth_device *edev = eth_get_current(); - pr_debug("VLAN packet received\n"); + net_serverip = ip; + dev_set_param_ip(&edev->dev, "serverip", net_serverip); +} - /* too small packet? */ - if (len < VLAN_ETHER_HDR_SIZE) - return; +void net_set_ip(IPaddr_t ip) +{ + struct eth_device *edev = eth_get_current(); - /* if no VLAN active */ - if ((ntohs(NetOurVLAN) & VLAN_IDMASK) == VLAN_NONE - ) - return; + net_ip = ip; + dev_set_param_ip(&edev->dev, "ipaddr", net_ip); +} - cti = ntohs(vet->vet_tag); - vlanid = cti & VLAN_IDMASK; - x = ntohs(vet->vet_type); +IPaddr_t net_get_ip(void) +{ + return net_ip; +} - ip = (IP_t *)(inpkt + VLAN_ETHER_HDR_SIZE); - len -= VLAN_ETHER_HDR_SIZE; - } +void net_set_netmask(IPaddr_t nm) +{ + struct eth_device *edev = eth_get_current(); - pr_debug("Receive from protocol 0x%x\n", x); + net_netmask = nm; + dev_set_param_ip(&edev->dev, "netmask", net_netmask); +} - if ((myvlanid & VLAN_IDMASK) != VLAN_NONE) { - if (vlanid == VLAN_NONE) - vlanid = (mynvlanid & VLAN_IDMASK); - /* not matched? */ - if (vlanid != (myvlanid & VLAN_IDMASK)) - return; - } +void net_set_gateway(IPaddr_t gw) +{ + struct eth_device *edev = eth_get_current(); - switch (x) { + net_gateway = gw; + dev_set_param_ip(&edev->dev, "gateway", net_gateway); +} - 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 packets 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 - */ - pr_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; - } +static LIST_HEAD(connection_list); - if (NetOurIP == 0) { - return; - } +static struct net_connection *net_new(IPaddr_t dest, rx_handler_f *handler) +{ + struct eth_device *edev = eth_get_current(); + struct net_connection *con; + int ret; - if (NetReadIP(&arp->ar_data[16]) != NetOurIP) { - return; - } + if (!edev) + return ERR_PTR(-ENETDOWN); - switch (ntohs(arp->ar_op)) { - case ARPOP_REQUEST: /* reply with our IP address */ - pr_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); - memcpy(NetTxPacket, et, (pkt - (uchar *)et) + ARP_HDR_SIZE); - eth_send((uchar *)NetTxPacket, (pkt - (uchar *)et) + ARP_HDR_SIZE); - return; - - case ARPOP_REPLY: /* arp reply */ - /* are we waiting for a reply */ - if (!NetArpWaitPacketIP || !NetArpWaitPacketMAC) - break; - pr_debug("Got ARP REPLY, set server/gtwy eth addr (%02x:%02x:%02x:%02x:%02x:%02x)\n", - arp->ar_data[0], arp->ar_data[1], - arp->ar_data[2], arp->ar_data[3], - arp->ar_data[4], arp->ar_data[5]); - - tmp = NetReadIP(&arp->ar_data[6]); - - /* matched waiting packet's address */ - if (tmp == NetArpWaitReplyIP) { - pr_debug("Got it\n"); - - /* save address for later use */ - memcpy(NetArpWaitPacketMAC, &arp->ar_data[0], 6); - - /* 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: - pr_debug("Unexpected ARP opcode 0x%x\n", ntohs(arp->ar_op)); - return; - } - break; + if (!is_valid_ether_addr(net_ether)) { + char str[sizeof("xx:xx:xx:xx:xx:xx")]; + random_ether_addr(net_ether); + ethaddr_to_string(net_ether, str); + printf("warning: No MAC address set. Using random address %s\n", str); + dev_set_param(&edev->dev, "ethaddr", str); + } - case PROT_RARP: - pr_debug("Got RARP\n"); + /* If we don't have an ip only broadcast is allowed */ + if (!net_ip && dest != 0xffffffff) + return ERR_PTR(-ENETDOWN); - arp = (ARP_t *)ip; - if (len < ARP_HDR_SIZE) { - printf("bad length %d < %d\n", len, ARP_HDR_SIZE); - return; - } + con = xzalloc(sizeof(*con)); + con->packet = memalign(32, PKTSIZE); + memset(con->packet, 0, PKTSIZE); - 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)) { + con->et = (struct ethernet *)con->packet; + con->ip = (struct iphdr *)(con->packet + ETHER_HDR_SIZE); + con->udp = (struct udphdr *)(con->packet + ETHER_HDR_SIZE + sizeof(struct iphdr)); + con->icmp = (struct icmphdr *)(con->packet + ETHER_HDR_SIZE + sizeof(struct iphdr)); + con->handler = handler; - 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); + if (dest == 0xffffffff) { + memset(con->et->et_dest, 0xff, 6); + } else { + ret = arp_request(dest, con->et->et_dest); + if (ret) + goto out; + } - (*packetHandler)(0,0,0,0); - } - break; + con->et->et_protlen = htons(PROT_IP); + memcpy(con->et->et_src, net_ether, 6); - case PROT_IP: - pr_debug("Got IP\n"); + con->ip->hl_v = 0x45; + con->ip->tos = 0; + con->ip->frag_off = htons(0x4000); /* No fragmentation */; + con->ip->ttl = 255; + net_copy_ip(&con->ip->daddr, &dest); + net_copy_ip(&con->ip->saddr, &net_ip); - if (len < IP_HDR_SIZE) { - debug ("len bad %d < %d\n", len, IP_HDR_SIZE); - return; - } - if (len < ntohs(ip->ip_len)) { - printf("len bad %d < %d\n", len, ntohs(ip->ip_len)); - return; - } - len = ntohs(ip->ip_len); + list_add_tail(&con->list, &connection_list); - pr_debug("len=%d, v=%02x\n", len, ip->ip_hl_v & 0xff); + return con; +out: + free(con->packet); + free(con); + return ERR_PTR(ret); +} - if ((ip->ip_hl_v & 0xf0) != 0x40) { - return; - } - if (ip->ip_off & htons(0x1fff)) { /* Can't deal w/ fragments */ - return; - } - if (!NetCksumOk((uchar *)ip, IP_HDR_SIZE_NO_UDP / 2)) { - puts ("checksum bad\n"); - return; - } - tmp = NetReadIP(&ip->ip_dst); - if (NetOurIP && tmp != NetOurIP && tmp != 0xFFFFFFFF) { - return; - } - /* - * watch for ICMP host redirects - * - * There is no real handler code (yet). We just watch - * for ICMP host redirect messages. In case anybody - * sees these messages: please contact me - * (wd@denx.de), or - even better - send me the - * necessary fixes :-) - * - * Note: in all cases where I have seen this so far - * it was a problem with the router configuration, - * for instance when a router was configured in the - * BOOTP reply, but the TFTP server was on the same - * subnet. So this is probably a warning that your - * configuration might be wrong. But I'm not really - * sure if there aren't any other situations. - */ - if (ip->ip_p == IPPROTO_ICMP) { - ICMP_t *icmph = (ICMP_t *)&(ip->udp_src); - - switch (icmph->type) { - case ICMP_REDIRECT: - if (icmph->code != ICMP_REDIR_HOST) - return; - puts (" ICMP Host Redirect to "); - print_IPaddr(icmph->un.gateway); - putchar(' '); - return; -#ifdef CONFIG_NET_PING - case ICMP_ECHO_REPLY: - /* - * IP header OK. Pass the packet to the current handler. - */ - /* XXX point to ip packet */ - (*packetHandler)((uchar *)ip, 0, 0, 0); - return; -#endif - default: - return; - } - } else if (ip->ip_p != IPPROTO_UDP) { /* Only UDP packets */ - return; - } +struct net_connection *net_udp_new(IPaddr_t dest, uint16_t dport, + rx_handler_f *handler) +{ + struct net_connection *con = net_new(dest, handler); -#ifdef CONFIG_UDP_CHECKSUM - if (ip->udp_xsum != 0) { - ulong xsum; - ushort *sumptr; - ushort sumlen; - - xsum = ip->ip_p; - xsum += (ntohs(ip->udp_len)); - xsum += (ntohl(ip->ip_src) >> 16) & 0x0000ffff; - xsum += (ntohl(ip->ip_src) >> 0) & 0x0000ffff; - xsum += (ntohl(ip->ip_dst) >> 16) & 0x0000ffff; - xsum += (ntohl(ip->ip_dst) >> 0) & 0x0000ffff; - - sumlen = ntohs(ip->udp_len); - sumptr = (ushort *) &(ip->udp_src); - - while (sumlen > 1) { - ushort sumdata; - - sumdata = *sumptr++; - xsum += ntohs(sumdata); - sumlen -= 2; - } - if (sumlen > 0) { - ushort sumdata; - - sumdata = *(unsigned char *) sumptr; - sumdata = (sumdata << 8) & 0xff00; - xsum += sumdata; - } - while ((xsum >> 16) != 0) { - xsum = (xsum & 0x0000ffff) + ((xsum >> 16) & 0x0000ffff); - } - if ((xsum != 0x00000000) && (xsum != 0x0000ffff)) { - printf(" UDP wrong checksum %08x %08x\n", xsum, ntohs(ip->udp_xsum)); - return; - } - } -#endif - /* - * IP header OK. Pass the packet to the current handler. - */ - (*packetHandler)((uchar *)ip +IP_HDR_SIZE, - ntohs(ip->udp_dst), - ntohs(ip->udp_src), - ntohs(ip->udp_len) - 8); - break; - } -} + if (IS_ERR(con)) + return con; + con->proto = IPPROTO_UDP; + con->udp->uh_dport = htons(dport); + con->udp->uh_sport = htons(net_udp_new_localport()); + con->ip->protocol = IPPROTO_UDP; -/**********************************************************************/ + return con; +} -static int net_check_prereq (proto_t protocol) +struct net_connection *net_icmp_new(IPaddr_t dest, rx_handler_f *handler) { - struct eth_device *edev = eth_get_current(); + struct net_connection *con = net_new(dest, handler); - switch (protocol) { - /* Fall through */ -#ifdef CONFIG_NET_NFS - case NFS: -#endif - case NETCONS: - case TFTP: - if (NetServerIP == 0) { - printf("*** ERROR: `%s.serverip' not set\n", dev_id(&edev->dev)); - return -1; - } + if (IS_ERR(con)) + return con; - if (NetOurIP == 0) { - printf("*** ERROR: `%s.ipaddr' not set\n", dev_id(&edev->dev)); - return -1; - } - /* Fall through */ - - case DHCP: - case RARP: - case BOOTP: - if (memcmp (NetOurEther, "\0\0\0\0\0\0", 6) == 0) { - printf("*** ERROR: `%s.ethaddr' not set\n", dev_id(&edev->dev)); - return -1; - } - /* Fall through */ - default: - return 0; - } + con->proto = IPPROTO_ICMP; + con->ip->protocol = IPPROTO_ICMP; - return -1; /* not reached */ + return con; } -/**********************************************************************/ -int -NetCksumOk(uchar * ptr, int len) +void net_unregister(struct net_connection *con) { - return !((NetCksum(ptr, len) + 1) & 0xfffe); + list_del(&con->list); + free(con->packet); + free(con); } - -unsigned -NetCksum(uchar * ptr, int len) +int net_ip_send(struct net_connection *con, int len) { - ulong xsum; - ushort *p = (ushort *)ptr; + con->ip->tot_len = htons(sizeof(struct iphdr) + len); + con->ip->id = htons(net_ip_id++);; + con->ip->check = 0; + con->ip->check = ~net_checksum((unsigned char *)con->ip, sizeof(struct iphdr)); - xsum = 0; - while (len-- > 0) - xsum += *p++; - xsum = (xsum & 0xffff) + (xsum >> 16); - xsum = (xsum & 0xffff) + (xsum >> 16); - return xsum & 0xffff; + eth_send(con->packet, ETHER_HDR_SIZE + sizeof(struct iphdr) + len); + + return 0; } -int -NetEthHdrSize(void) +int net_udp_send(struct net_connection *con, int len) { - ushort myvlanid; + con->udp->uh_ulen = htons(len + 8); + con->udp->uh_sum = 0; - myvlanid = ntohs(NetOurVLAN); - if (myvlanid == (ushort)-1) - myvlanid = VLAN_NONE; - - return ((myvlanid & VLAN_IDMASK) == VLAN_NONE) ? ETHER_HDR_SIZE : VLAN_ETHER_HDR_SIZE; + return net_ip_send(con, sizeof(struct udphdr) + len); } -int -NetSetEther(uchar * xet, uchar * addr, uint prot) +int net_icmp_send(struct net_connection *con, int len) { - Ethernet_t *et = (Ethernet_t *)xet; - ushort myvlanid; - - myvlanid = ntohs(NetOurVLAN); - if (myvlanid == (ushort)-1) - myvlanid = VLAN_NONE; - - memcpy (et->et_dest, addr, 6); - memcpy (et->et_src, NetOurEther, 6); - if ((myvlanid & VLAN_IDMASK) == VLAN_NONE) { - et->et_protlen = htons(prot); - return ETHER_HDR_SIZE; - } else { - VLAN_Ethernet_t *vet = (VLAN_Ethernet_t *)xet; + con->icmp->checksum = ~net_checksum((unsigned char *)con->icmp, + sizeof(struct icmphdr) + len); - vet->vet_vlan_type = htons(PROT_VLAN); - vet->vet_tag = htons((0 << 5) | (myvlanid & VLAN_IDMASK)); - vet->vet_type = htons(prot); - return VLAN_ETHER_HDR_SIZE; - } + return net_ip_send(con, sizeof(struct icmphdr) + len); } -void -NetSetIP(uchar * xip, IPaddr_t dest, int dport, int sport, int len) +static int net_answer_arp(unsigned char *pkt, int len) { - IP_t *ip = (IP_t *)xip; + struct arprequest *arp = (struct arprequest *)(pkt + ETHER_HDR_SIZE); + struct ethernet *et = (struct ethernet *)pkt; + unsigned char *packet; - /* - * 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_HDR_SIZE + len] = 0; + debug("%s\n", __func__); - /* - * Construct an IP and UDP header. - * (need to set no fragment bit - XXX) - */ - ip->ip_hl_v = 0x45; /* IP_HDR_SIZE / 4 (not including UDP) */ - ip->ip_tos = 0; - ip->ip_len = htons(IP_HDR_SIZE + len); - ip->ip_id = htons(NetIPID++); - ip->ip_off = htons(0x4000); /* No fragmentation */ - ip->ip_ttl = 255; - ip->ip_p = 17; /* UDP */ - ip->ip_sum = 0; - NetCopyIP((void*)&ip->ip_src, &NetOurIP); /* already in network byte order */ - NetCopyIP((void*)&ip->ip_dst, &dest); /* - "" - */ - ip->udp_src = htons(sport); - ip->udp_dst = htons(dport); - ip->udp_len = htons(8 + len); - ip->udp_xsum = 0; - ip->ip_sum = ~NetCksum((uchar *)ip, IP_HDR_SIZE_NO_UDP / 2); + memcpy (et->et_dest, et->et_src, 6); + memcpy (et->et_src, net_ether, 6); + + et->et_protlen = htons(PROT_ARP); + arp->ar_op = htons(ARPOP_REPLY); + memcpy(&arp->ar_data[10], &arp->ar_data[0], 6); + net_copy_ip(&arp->ar_data[16], &arp->ar_data[6]); + memcpy(&arp->ar_data[0], net_ether, 6); + net_copy_ip(&arp->ar_data[6], &net_ip); + + packet = net_alloc_packet(); + if (!packet) + return 0; + memcpy(packet, pkt, ETHER_HDR_SIZE + ARP_HDR_SIZE); + eth_send(packet, ETHER_HDR_SIZE + ARP_HDR_SIZE); + free(packet); + + return 0; } -char *ip_to_string (IPaddr_t x, char *s) +static void net_bad_packet(unsigned char *pkt, int len) { - x = ntohl (x); - sprintf (s, "%d.%d.%d.%d", - (int) ((x >> 24) & 0xff), - (int) ((x >> 16) & 0xff), - (int) ((x >> 8) & 0xff), (int) ((x >> 0) & 0xff) - ); - return s; +#ifdef DEBUG + /* + * We received a bad packet. for now just dump it. + * We could add more sophisticated debugging here + */ + memory_display(pkt, 0, len, 1); +#endif } -int string_to_ip(const char *s, IPaddr_t *ip) +static int net_handle_arp(unsigned char *pkt, int len) { - IPaddr_t addr = 0; - char *e; - int i; + struct arprequest *arp; - if (!s) - return -EINVAL; + debug("%s: got arp\n", __func__); - for (i = 0; i < 4; i++) { - ulong val; + /* + * 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 packets 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 + */ + arp = (struct arprequest *)(pkt + ETHER_HDR_SIZE); + if (len < ARP_HDR_SIZE) + goto bad; + if (ntohs(arp->ar_hrd) != ARP_ETHER) + goto bad; + if (ntohs(arp->ar_pro) != PROT_IP) + goto bad; + if (arp->ar_hln != 6) + goto bad; + if (arp->ar_pln != 4) + goto bad; + if (net_ip == 0) + return 0; + if (net_read_ip(&arp->ar_data[16]) != net_ip) + return 0; - if (!isdigit(*s)) - return -EINVAL; + switch (ntohs(arp->ar_op)) { + case ARPOP_REQUEST: + return net_answer_arp(pkt, len); + case ARPOP_REPLY: + arp_handler(arp); + return 1; + default: + pr_debug("Unexpected ARP opcode 0x%x\n", ntohs(arp->ar_op)); + return -EINVAL; + } - val = simple_strtoul(s, &e, 10); - addr <<= 8; - addr |= (val & 0xFF); + return 0; - if (*e != '.' && i != 3) - return -EINVAL; +bad: + net_bad_packet(pkt, len); + return -EINVAL; +} - s = e + 1; +static int net_handle_udp(unsigned char *pkt, int len) +{ + struct iphdr *ip = (struct iphdr *)(pkt + ETHER_HDR_SIZE); + struct net_connection *con; + struct udphdr *udp; + int port; + + udp = (struct udphdr *)(ip + 1); + port = ntohs(udp->uh_dport); + list_for_each_entry(con, &connection_list, list) { + if (con->proto == IPPROTO_UDP && port == ntohs(con->udp->uh_sport)) { + con->handler(pkt, len); + return 0; + } } - - *ip = htonl(addr); - return 0; + return -EINVAL; } -void VLAN_to_string(ushort x, char *s) +static int net_handle_icmp(unsigned char *pkt, int len) { - x = ntohs(x); + struct net_connection *con; - if (x == (ushort)-1) - x = VLAN_NONE; + debug("%s\n", __func__); - if (x == VLAN_NONE) - strcpy(s, "none"); - else - sprintf(s, "%d", x & VLAN_IDMASK); + list_for_each_entry(con, &connection_list, list) { + if (con->proto == IPPROTO_ICMP) { + con->handler(pkt, len); + return 0; + } + } + return 0; } -ushort string_to_VLAN(const char *s) +static int net_handle_ip(unsigned char *pkt, int len) { - ushort id; + struct iphdr *ip = (struct iphdr *)(pkt + ETHER_HDR_SIZE); + IPaddr_t tmp; - if (s == NULL) - return htons(VLAN_NONE); + debug("%s\n", __func__); - if (*s < '0' || *s > '9') - id = VLAN_NONE; - else - id = (ushort)simple_strtoul(s, NULL, 10); + if (len < sizeof(struct ethernet) + sizeof(struct iphdr) || + len < ETHER_HDR_SIZE + ntohs(ip->tot_len)) { + debug("%s: bad len\n", __func__); + goto bad; + } - return htons(id); -} + if ((ip->hl_v & 0xf0) != 0x40) + goto bad; -void print_IPaddr (IPaddr_t x) -{ - char tmp[16]; + if (ip->frag_off & htons(0x1fff)) /* Can't deal w/ fragments */ + goto bad; + if (!net_checksum_ok((unsigned char *)ip, sizeof(struct iphdr))) + goto bad; - ip_to_string (x, tmp); + tmp = net_read_ip(&ip->daddr); + if (net_ip && tmp != net_ip && tmp != 0xffffffff) + return 0; - puts (tmp); -} + switch (ip->protocol) { + case IPPROTO_ICMP: + return net_handle_icmp(pkt, len); + case IPPROTO_UDP: + return net_handle_udp(pkt, len); + } -ushort getenv_VLAN(char *var) -{ - return string_to_VLAN(getenv(var)); + return 0; +bad: + net_bad_packet(pkt, len); + return 0; } -int string_to_ethaddr(const char *str, char *enetaddr) +int net_receive(unsigned char *pkt, int len) { - ulong reg; - char *e; + struct ethernet *et = (struct ethernet *)pkt; + int et_protlen = ntohs(et->et_protlen); - if (!str || strlen(str) != 17) - return -1; - - if (str[2] != ':' || str[5] != ':' || str[8] != ':' || - str[11] != ':' || str[14] != ':') - return -1; + if (len < ETHER_HDR_SIZE) + return 0; - for (reg = 0; reg < 6; ++reg) { - enetaddr[reg] = simple_strtoul (str, &e, 16); - str = e + 1; + switch (et_protlen) { + case PROT_ARP: + return net_handle_arp(pkt, len); + case PROT_IP: + return net_handle_ip(pkt, len); + default: + debug("%s: got unknown protocol type: %d\n", __func__, et_protlen); + return 1; } - - return 0; } -void ethaddr_to_string(const unsigned char *enetaddr, char *str) +static int net_init(void) { - sprintf (str, "%02X:%02X:%02X:%02X:%02X:%02X", - enetaddr[0], enetaddr[1], enetaddr[2], enetaddr[3], - enetaddr[4], enetaddr[5]); + int i; + + for (i = 0; i < PKTBUFSRX; i++) + NetRxPackets[i] = memalign(32, PKTSIZE); + + return 0; } +postcore_initcall(net_init); + diff --git a/net/netconsole.c b/net/netconsole.c new file mode 100644 index 0000000000..07e6a6cf27 --- /dev/null +++ b/net/netconsole.c @@ -0,0 +1,228 @@ +/* + * netconsole.c - network console support + * + * Copyright (c) 2010 Sascha Hauer <s.hauer@pengutronix.de>, Pengutronix + * + * See file CREDITS for list of people who contributed to this + * project. + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License version 2 + * as published by the Free Software Foundation. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + */ + +#include <common.h> +#include <command.h> +#include <fs.h> +#include <linux/stat.h> +#include <errno.h> +#include <malloc.h> +#include <getopt.h> +#include <stringlist.h> +#include <net.h> +#include <kfifo.h> +#include <init.h> +#include <linux/err.h> + +/** + * @file + * @brief Network console support + */ + +struct nc_priv { + struct console_device cdev; + struct kfifo *fifo; + int busy; + struct net_connection *con; + + uint16_t port; + IPaddr_t ip; +}; + +static struct nc_priv *g_priv; + +static void nc_handler(char *pkt, unsigned len) +{ + struct nc_priv *priv = g_priv; + unsigned char *packet = net_eth_to_udp_payload(pkt); + + kfifo_put(priv->fifo, packet, net_eth_to_udplen(pkt)); +} + +static int nc_init(void) +{ + struct nc_priv *priv = g_priv; + + if (priv->con) + net_unregister(priv->con); + + priv->con = net_udp_new(priv->ip, priv->port, nc_handler); + if (IS_ERR(priv->con)) { + int ret = PTR_ERR(priv->con); + priv->con = NULL; + return ret; + } + + net_udp_bind(priv->con, priv->port); + priv->cdev.f_caps = CONSOLE_STDIN | CONSOLE_STDOUT | CONSOLE_STDERR; + return 0; +} + +static int nc_getc(struct console_device *cdev) +{ + struct nc_priv *priv = container_of(cdev, + struct nc_priv, cdev); + unsigned char c; + + while (!kfifo_len(priv->fifo)) + net_poll(); + + kfifo_getc(priv->fifo, &c); + + return c; +} + +static int nc_tstc(struct console_device *cdev) +{ + struct nc_priv *priv = container_of(cdev, + struct nc_priv, cdev); + + if (priv->busy) + return kfifo_len(priv->fifo) ? 1 : 0; + + net_poll(); + + return kfifo_len(priv->fifo) ? 1 : 0; +} + +static void nc_putc(struct console_device *cdev, char c) +{ + struct nc_priv *priv = container_of(cdev, + struct nc_priv, cdev); + unsigned char *packet; + + if (!priv->con) + return; + + if (priv->busy) + return; + + packet = net_udp_get_payload(priv->con); + *packet = c; + + priv->busy = 1; + net_udp_send(priv->con, 1); + priv->busy = 0; +} + +static int nc_setbaudrate(struct console_device *cdev, int baudrate) +{ + return 0; +} + +static int nc_port_set(struct device_d *dev, struct param_d *param, + const char *val) +{ + struct nc_priv *priv = g_priv; + char portstr[16]; + int port; + + if (!val) + dev_param_set_generic(dev, param, NULL); + + port = simple_strtoul(val, NULL, 10); + if (port > 65535) + return -EINVAL; + + priv->port = port; + nc_init(); + + sprintf(portstr, "%d", port); + dev_param_set_generic(dev, param, portstr); + + return 0; +} + +static int nc_remoteip_set(struct device_d *dev, struct param_d *param, + const char *val) +{ + struct nc_priv *priv = g_priv; + IPaddr_t ip; + int ret; + + if (!val) + dev_param_set_generic(dev, param, NULL); + + if (string_to_ip(val, &ip)) + return -EINVAL; + + priv->ip = ip; + ret = nc_init(); + if (ret) + return ret; + + dev_param_set_generic(dev, param, val); + + return 0; +} + +static int netconsole_init(void) +{ + struct nc_priv *priv; + struct console_device *cdev; + + priv = xzalloc(sizeof(*priv)); + cdev = &priv->cdev; + cdev->tstc = nc_tstc; + cdev->putc = nc_putc; + cdev->getc = nc_getc; + cdev->setbrg = nc_setbaudrate; + + g_priv = priv; + + priv->fifo = kfifo_alloc(1024); + + console_register(cdev); + + dev_add_param(&cdev->class_dev, "ip", nc_remoteip_set, NULL, 0); + dev_add_param(&cdev->class_dev, "port", nc_port_set, NULL, 0); + dev_set_param(&cdev->class_dev, "port", "6666"); + + printf("registered netconsole as %s%d\n", cdev->class_dev.name, cdev->class_dev.id); + + return 0; +} + +device_initcall(netconsole_init); + +/** @page net_netconsole Network console + +@section net_netconsole Using an UDP based network console + +If enabled barebox supports a console via udp networking. There is only +one network console supported registered during init time. It is deactivated +by default because it opens great security holes, so use with care. + +To use the network console you have to configure the remote ip and the local +and remote ports. Assuming the network console is registered as cs1, it can be +configured with: + +@code +cs1.ip=<remotehost> +cs1.port=<port> +cs1.active=ioe +@endcode + +On the remote host call scripts/netconsole with bareboxes ip and port as +parameters. port is initialized to 6666 by default. + +*/ @@ -32,28 +32,111 @@ #include <libgen.h> #include <fcntl.h> #include <errno.h> -#include "nfs.h" +#include <progress.h> +#include <linux/err.h> + +#define SUNRPC_PORT 111 + +#define PROG_PORTMAP 100000 +#define PROG_NFS 100003 +#define PROG_MOUNT 100005 + +#define MSG_CALL 0 +#define MSG_REPLY 1 + +#define PORTMAP_GETPORT 3 + +#define MOUNT_ADDENTRY 1 +#define MOUNT_UMOUNTALL 4 + +#define NFS_LOOKUP 4 +#define NFS_READLINK 5 +#define NFS_READ 6 + +#define NFS_FHSIZE 32 + +enum nfs_stat { + NFS_OK = 0, + NFSERR_PERM = 1, + NFSERR_NOENT = 2, + NFSERR_IO = 5, + NFSERR_NXIO = 6, + NFSERR_ACCES = 13, + NFSERR_EXIST = 17, + NFSERR_NODEV = 19, + NFSERR_NOTDIR = 20, + NFSERR_ISDIR = 21, + NFSERR_FBIG = 27, + NFSERR_NOSPC = 28, + NFSERR_ROFS = 30, + NFSERR_NAMETOOLONG=63, + NFSERR_NOTEMPTY = 66, + NFSERR_DQUOT = 69, + NFSERR_STALE = 70, + NFSERR_WFLUSH = 99, +}; + +/* Block size used for NFS read accesses. A RPC reply packet (including all + * headers) must fit within a single Ethernet frame to avoid fragmentation. + * Chosen to be a power of two, as most NFS servers are optimized for this. */ +#define NFS_READ_SIZE 1024 + +struct rpc_call { + uint32_t id; + uint32_t type; + uint32_t rpcvers; + uint32_t prog; + uint32_t vers; + uint32_t proc; + uint32_t data[0]; +}; + +struct rpc_reply { + uint32_t id; + uint32_t type; + uint32_t rstatus; + uint32_t verifier; + uint32_t v2; + uint32_t astatus; + uint32_t data[0]; +}; + +struct rpc_t { + union { + struct { + uint32_t id; + uint32_t type; + uint32_t rpcvers; + uint32_t prog; + uint32_t vers; + uint32_t proc; + uint32_t data[1]; + } call; + struct { + uint32_t id; + uint32_t type; + uint32_t rstatus; + uint32_t verifier; + uint32_t v2; + uint32_t astatus; + uint32_t data[19]; + } reply; + } u; +}; + +#define NFS_TIMEOUT 1 -/*#define NFS_DEBUG*/ - -#define HASHES_PER_LINE 65 /* Number of "loading" hashes per line */ -#define NFS_TIMEOUT 60 - -static int fs_mounted = 0; static unsigned long rpc_id = 0; static int nfs_offset = -1; -static int nfs_len; +static uint64_t nfs_timer_start; +static int nfs_err; static char dirfh[NFS_FHSIZE]; /* file handle of directory */ static char filefh[NFS_FHSIZE]; /* file handle of kernel image */ -static int NfsDownloadState; -static IPaddr_t NfsServerIP; -static int NfsSrvMountPort; -static int NfsSrvNfsPort; -static int NfsOurPort; -static int NfsTimeoutCount; -static int NfsState; +static int nfs_server_mount_port; +static int nfs_server_nfs_port; +static int nfs_state; #define STATE_PRCLOOKUP_PROG_MOUNT_REQ 1 #define STATE_PRCLOOKUP_PROG_NFS_REQ 2 #define STATE_MOUNT_REQ 3 @@ -61,40 +144,22 @@ static int NfsState; #define STATE_LOOKUP_REQ 5 #define STATE_READ_REQ 6 #define STATE_READLINK_REQ 7 +#define STATE_DONE 8 static char *nfs_filename; static char *nfs_path; static char nfs_path_buff[2048]; static int net_store_fd; - -static __inline__ int -store_block (uchar * src, unsigned offset, unsigned len) -{ - ulong newsize = offset + len; - int ret; - - ret = write(net_store_fd, src, len); - if (ret < 0) - return ret; - - if (NetBootFileXferSize < (offset+len)) - NetBootFileXferSize = newsize; - - return 0; -} +static struct net_connection *nfs_con; /************************************************************************** RPC_ADD_CREDENTIALS - Add RPC authentication/verifier entries **************************************************************************/ -static long *rpc_add_credentials (long *p) +static uint32_t *rpc_add_credentials(uint32_t *p) { int hl; - int hostnamelen; - char hostname[256]; - - strcpy (hostname, ""); - hostnamelen=strlen (hostname); + int hostnamelen = 0; /* Here's the executive summary on authentication requirements of the * various NFS server implementations: Linux accepts both AUTH_NONE @@ -112,10 +177,12 @@ 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); */ /* empty hostname */ + p += hl / 4; *p++ = 0; /* uid */ *p++ = 0; /* gid */ @@ -131,46 +198,42 @@ 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) +static int rpc_req(int rpc_prog, int rpc_proc, uint32_t *data, int datalen) { - struct rpc_t pkt; + struct rpc_call pkt; unsigned long id; - uint32_t *p; - int pktlen; int sport; + int ret; + unsigned char *payload = net_udp_get_payload(nfs_con); id = ++rpc_id; - pkt.u.call.id = htonl(id); - pkt.u.call.type = htonl(MSG_CALL); - pkt.u.call.rpcvers = htonl(2); /* use RPC version 2 */ - pkt.u.call.prog = htonl(rpc_prog); - pkt.u.call.vers = htonl(2); /* portmapper is version 2 */ - pkt.u.call.proc = htonl(rpc_proc); - p = (uint32_t *)&(pkt.u.call.data); - - if (datalen) - memcpy ((char *)p, (char *)data, datalen*sizeof(uint32_t)); + pkt.id = htonl(id); + pkt.type = htonl(MSG_CALL); + pkt.rpcvers = htonl(2); /* use RPC version 2 */ + pkt.prog = htonl(rpc_prog); + pkt.vers = htonl(2); /* portmapper is version 2 */ + pkt.proc = htonl(rpc_proc); - pktlen = (char *)p + datalen*sizeof(uint32_t) - (char *)&pkt; - - memcpy ((char *)NetTxPacket + NetEthHdrSize() + IP_HDR_SIZE, (char *)&pkt, pktlen); + memcpy(payload, &pkt, sizeof(pkt)); + memcpy(payload + sizeof(pkt), data, datalen * sizeof(uint32_t)); if (rpc_prog == PROG_PORTMAP) sport = SUNRPC_PORT; else if (rpc_prog == PROG_MOUNT) - sport = NfsSrvMountPort; + sport = nfs_server_mount_port; else - sport = NfsSrvNfsPort; + sport = nfs_server_nfs_port; + + nfs_con->udp->uh_dport = htons(sport); + ret = net_udp_send(nfs_con, sizeof(pkt) + datalen * sizeof(uint32_t)); - NetSendUDPPacket (NetServerEther, NfsServerIP, sport, NfsOurPort, pktlen); + return ret; } /************************************************************************** RPC_LOOKUP - Lookup RPC Port numbers **************************************************************************/ -static void -rpc_lookup_req (int prog, int ver) +static void rpc_lookup_req(int prog, int ver) { uint32_t data[16]; @@ -181,14 +244,13 @@ 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) +static void nfs_mount_req(char *path) { uint32_t data[1024]; uint32_t *p; @@ -198,39 +260,38 @@ nfs_mount_req (char *path) pathlen = strlen (path); p = &(data[0]); - p = (uint32_t *)rpc_add_credentials((long *)p); + p = rpc_add_credentials(p); *p++ = htonl(pathlen); - if (pathlen & 3) *(p + pathlen / 4) = 0; + if (pathlen & 3) + *(p + pathlen / 4) = 0; memcpy (p, path, pathlen); p += (pathlen + 3) / 4; - len = (uint32_t *)p - (uint32_t *)&(data[0]); + len = p - &(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) +static void nfs_umountall_req(void) { uint32_t data[1024]; uint32_t *p; int len; - if ((NfsSrvMountPort == -1) || (!fs_mounted)) { + if (nfs_server_mount_port < 0) /* Nothing mounted, nothing to umount */ return; - } p = &(data[0]); - p = (uint32_t *)rpc_add_credentials ((long *)p); + p = rpc_add_credentials(p); - len = (uint32_t *)p - (uint32_t *)&(data[0]); + len = p - &(data[0]); - rpc_req (PROG_MOUNT, MOUNT_UMOUNTALL, data, len); + rpc_req(PROG_MOUNT, MOUNT_UMOUNTALL, data, len); } /*************************************************************************** @@ -240,29 +301,27 @@ nfs_umountall_req (void) * In case of successful readlink(), the dirname is manipulated, * so that inside the nfs() function a recursion can be done. **************************************************************************/ -static void -nfs_readlink_req (void) +static 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 = rpc_add_credentials(p); memcpy (p, filefh, NFS_FHSIZE); p += (NFS_FHSIZE / 4); - len = (uint32_t *)p - (uint32_t *)&(data[0]); + len = p - &(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) +static void nfs_lookup_req(char *fname) { uint32_t data[1024]; uint32_t *p; @@ -272,32 +331,32 @@ nfs_lookup_req (char *fname) fnamelen = strlen (fname); p = &(data[0]); - p = (uint32_t *)rpc_add_credentials ((long *)p); + p = rpc_add_credentials(p); memcpy (p, dirfh, NFS_FHSIZE); p += (NFS_FHSIZE / 4); *p++ = htonl(fnamelen); - if (fnamelen & 3) *(p + fnamelen / 4) = 0; + if (fnamelen & 3) + *(p + fnamelen / 4) = 0; memcpy (p, fname, fnamelen); p += (fnamelen + 3) / 4; - len = (uint32_t *)p - (uint32_t *)&(data[0]); + len = p - &(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) +static void 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 = rpc_add_credentials(p); memcpy (p, filefh, NFS_FHSIZE); p += (NFS_FHSIZE / 4); @@ -305,238 +364,196 @@ nfs_read_req (int offset, int readlen) *p++ = htonl(readlen); *p++ = 0; - len = (uint32_t *)p - (uint32_t *)&(data[0]); + len = p - &(data[0]); - rpc_req (PROG_NFS, NFS_READ, data, len); + rpc_req(PROG_NFS, NFS_READ, data, len); } /************************************************************************** RPC request dispatcher **************************************************************************/ - -static void -NfsSend (void) +static void nfs_send(void) { -#ifdef NFS_DEBUG - printf ("%s\n", __FUNCTION__); -#endif + debug("%s\n", __func__); - switch (NfsState) { + switch (nfs_state) { 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_READ_SIZE); break; case STATE_READLINK_REQ: - nfs_readlink_req (); + nfs_readlink_req(); break; } -} -/************************************************************************** -Handlers for the reply from server -**************************************************************************/ + nfs_timer_start = get_time_ns(); +} -static int -rpc_lookup_reply (int prog, uchar *pkt, unsigned len) +static int rpc_check_reply(unsigned char *pkt, int isnfs) { - struct rpc_t rpc_pkt; - - memcpy ((unsigned char *)&rpc_pkt, pkt, len); + uint32_t *data; + int nfserr; + struct rpc_reply rpc; -#ifdef NFS_DEBUG - printf ("%s\n", __FUNCTION__); -#endif + memcpy(&rpc, pkt, sizeof(rpc)); - if (ntohl(rpc_pkt.u.reply.id) != rpc_id) - return -1; + if (ntohl(rpc.id) != rpc_id) + return -EINVAL; - if (rpc_pkt.u.reply.rstatus || - rpc_pkt.u.reply.verifier || - rpc_pkt.u.reply.astatus || - rpc_pkt.u.reply.astatus) { - return -1; + if (rpc.rstatus || + rpc.verifier || + rpc.astatus ) { + return -EINVAL; } + if (!isnfs) + return 0; + + data = (uint32_t *)(pkt + sizeof(struct rpc_reply)); + nfserr = ntohl(net_read_uint32(data)); + + debug("%s: state: %d, err %d\n", __func__, nfs_state, -nfserr); + + if (nfserr <= 30) + /* These nfs codes correspond with those in errno.h */ + return -nfserr; + if (nfserr == NFSERR_STALE) + return -ESTALE; + + return -EINVAL; +} + +static int rpc_lookup_reply(int prog, unsigned char *pkt, unsigned len) +{ + uint32_t port; + int ret; + + ret = rpc_check_reply(pkt, 0); + if (ret) + return ret; + + port = net_read_uint32((uint32_t *)(pkt + sizeof(struct rpc_reply))); switch (prog) { case PROG_MOUNT: - NfsSrvMountPort = ntohl(rpc_pkt.u.reply.data[0]); + nfs_server_mount_port = ntohl(port); break; case PROG_NFS: - NfsSrvNfsPort = ntohl(rpc_pkt.u.reply.data[0]); + nfs_server_nfs_port = ntohl(port); break; } return 0; } -static int -nfs_mount_reply (uchar *pkt, unsigned len) +static int nfs_mount_reply(unsigned char *pkt, unsigned len) { - struct rpc_t rpc_pkt; - -#ifdef NFS_DEBUG - printf ("%s\n", __FUNCTION__); -#endif - - memcpy ((unsigned char *)&rpc_pkt, pkt, len); - - if (ntohl(rpc_pkt.u.reply.id) != rpc_id) - return -1; + int ret; - if (rpc_pkt.u.reply.rstatus || - rpc_pkt.u.reply.verifier || - rpc_pkt.u.reply.astatus || - rpc_pkt.u.reply.data[0]) { - return -1; - } + ret = rpc_check_reply(pkt, 1); + if (ret) + return ret; - fs_mounted = 1; - memcpy (dirfh, rpc_pkt.u.reply.data + 1, NFS_FHSIZE); + memcpy(dirfh, pkt + sizeof(struct rpc_reply) + 4, NFS_FHSIZE); return 0; } -static int -nfs_umountall_reply (uchar *pkt, unsigned len) +static int nfs_umountall_reply(unsigned char *pkt, unsigned len) { - struct rpc_t rpc_pkt; - -#ifdef NFS_DEBUG - printf ("%s\n", __FUNCTION__); -#endif - - memcpy ((unsigned char *)&rpc_pkt, pkt, len); - - if (ntohl(rpc_pkt.u.reply.id) != rpc_id) - return -1; + int ret; - if (rpc_pkt.u.reply.rstatus || - rpc_pkt.u.reply.verifier || - rpc_pkt.u.reply.astatus) { - return -1; - } + ret = rpc_check_reply(pkt, 0); + if (ret) + return ret; - fs_mounted = 0; - memset (dirfh, 0, sizeof(dirfh)); + memset(dirfh, 0, sizeof(dirfh)); return 0; } -static int -nfs_lookup_reply (uchar *pkt, unsigned len) +static int nfs_lookup_reply(unsigned char *pkt, unsigned len) { - struct rpc_t rpc_pkt; - -#ifdef NFS_DEBUG - printf ("%s\n", __FUNCTION__); -#endif - - memcpy ((unsigned char *)&rpc_pkt, pkt, len); - - if (ntohl(rpc_pkt.u.reply.id) != rpc_id) - return -1; + int ret; - if (rpc_pkt.u.reply.rstatus || - rpc_pkt.u.reply.verifier || - rpc_pkt.u.reply.astatus || - rpc_pkt.u.reply.data[0]) { - return -1; - } + ret = rpc_check_reply(pkt, 1); + if (ret) + return ret; - memcpy (filefh, rpc_pkt.u.reply.data + 1, NFS_FHSIZE); + memcpy(filefh, pkt + sizeof(struct rpc_reply) + 4, NFS_FHSIZE); return 0; } -static int -nfs_readlink_reply (uchar *pkt, unsigned len) +static int nfs_readlink_reply(unsigned char *pkt, unsigned len) { - struct rpc_t rpc_pkt; + uint32_t *data; + char *path; int rlen; + int ret; -#ifdef NFS_DEBUG - printf ("%s\n", __FUNCTION__); -#endif + ret = rpc_check_reply(pkt, 1); + if (ret) + return ret; - memcpy ((unsigned char *)&rpc_pkt, pkt, len); + data = (uint32_t *)(pkt + sizeof(struct rpc_reply)); - if (ntohl(rpc_pkt.u.reply.id) != rpc_id) - return -1; + data++; - if (rpc_pkt.u.reply.rstatus || - rpc_pkt.u.reply.verifier || - rpc_pkt.u.reply.astatus || - rpc_pkt.u.reply.data[0]) { - return -1; - } + rlen = ntohl(net_read_uint32(data)); /* new path length */ - rlen = ntohl (rpc_pkt.u.reply.data[1]); /* new path length */ + data++; + path = (char *)data; - if (*((char *)&(rpc_pkt.u.reply.data[2])) != '/') { - int pathlen; - strcat (nfs_path, "/"); - pathlen = strlen(nfs_path); - memcpy (nfs_path+pathlen, (uchar *)&(rpc_pkt.u.reply.data[2]), rlen); - nfs_path[pathlen+rlen+1] = 0; + if (*path != '/') { + strcat(nfs_path, "/"); + strncat(nfs_path, path, rlen); } else { - memcpy (nfs_path, (uchar *)&(rpc_pkt.u.reply.data[2]), rlen); + memcpy(nfs_path, path, rlen); nfs_path[rlen] = 0; } return 0; } -static int -nfs_read_reply (uchar *pkt, unsigned len) +static int nfs_read_reply(unsigned char *pkt, unsigned len) { - struct rpc_t rpc_pkt; int rlen; + uint32_t *data; + int ret; -#ifdef NFS_DEBUG_nop - printf ("%s\n", __FUNCTION__); -#endif + debug("%s\n", __func__); - memcpy ((uchar *)&rpc_pkt, pkt, sizeof(rpc_pkt.u.reply)); + ret = rpc_check_reply(pkt, 1); + if (ret) + return ret; - if (ntohl(rpc_pkt.u.reply.id) != rpc_id) - return -1; + data = (uint32_t *)(pkt + sizeof(struct rpc_reply)); - if (rpc_pkt.u.reply.rstatus || - rpc_pkt.u.reply.verifier || - rpc_pkt.u.reply.astatus || - rpc_pkt.u.reply.data[0]) { - if (rpc_pkt.u.reply.rstatus) { - return -9999; - } - if (rpc_pkt.u.reply.astatus) { - return -9999; - } - return -ntohl(rpc_pkt.u.reply.data[0]);; + if (!nfs_offset) { + uint32_t filesize = ntohl(net_read_uint32(data + 6)); + init_progression_bar(filesize); } - 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))) { - putchar ('#'); - } + rlen = ntohl(net_read_uint32(data + 18)); - rlen = ntohl(rpc_pkt.u.reply.data[18]); - if ( store_block ((uchar *)pkt+sizeof(rpc_pkt.u.reply), nfs_offset, rlen) ) - return -9999; + ret = write(net_store_fd, (char *)(data + 19), rlen); + if (ret < 0) { + perror("write"); + return ret; + } return rlen; } @@ -545,170 +562,114 @@ nfs_read_reply (uchar *pkt, unsigned len) Interfaces of barebox **************************************************************************/ -static void -NfsTimeout (void) -{ - puts ("Timeout\n"); - NetState = NETLOOP_FAIL; - return; -} - -static void -NfsHandler (uchar *pkt, unsigned dest, unsigned src, unsigned len) +static void nfs_handler(char *packet, unsigned len) { - int rlen; - -#ifdef NFS_DEBUG - printf ("%s\n", __FUNCTION__); -#endif + char *pkt = net_eth_to_udp_payload(packet); + int ret; - if (dest != NfsOurPort) return; + debug("%s\n", __func__); - switch (NfsState) { + switch (nfs_state) { case STATE_PRCLOOKUP_PROG_MOUNT_REQ: - rpc_lookup_reply (PROG_MOUNT, pkt, len); - NfsState = STATE_PRCLOOKUP_PROG_NFS_REQ; - NfsSend (); + ret = rpc_lookup_reply(PROG_MOUNT, pkt, len); + if (ret) + goto err_out; + nfs_state = STATE_PRCLOOKUP_PROG_NFS_REQ; break; case STATE_PRCLOOKUP_PROG_NFS_REQ: - rpc_lookup_reply (PROG_NFS, pkt, len); - NfsState = STATE_MOUNT_REQ; - NfsSend (); + ret = rpc_lookup_reply(PROG_NFS, pkt, len); + if (ret) + goto err_out; + nfs_state = STATE_MOUNT_REQ; break; case STATE_MOUNT_REQ: - if (nfs_mount_reply(pkt, len)) { - puts ("*** ERROR: Cannot mount\n"); - /* just to be sure... */ - NfsState = STATE_UMOUNT_REQ; - NfsSend (); - } else { - NfsState = STATE_LOOKUP_REQ; - NfsSend (); - } + ret = nfs_mount_reply(pkt, len); + if (ret) + goto err_out; + + nfs_state = STATE_LOOKUP_REQ; break; case STATE_UMOUNT_REQ: - if (nfs_umountall_reply(pkt, len)) { - puts ("*** ERROR: Cannot umount\n"); - NetState = NETLOOP_FAIL; - } else { - puts ("\ndone\n"); - NetState = NfsDownloadState; - } - break; + ret = nfs_umountall_reply(pkt, len); + if (ret) + nfs_err = ret; + nfs_state = STATE_DONE; + return; case STATE_LOOKUP_REQ: - if (nfs_lookup_reply(pkt, len)) { - puts ("*** ERROR: File lookup fail\n"); - NfsState = STATE_UMOUNT_REQ; - NfsSend (); - } else { - NfsState = STATE_READ_REQ; - nfs_offset = 0; - nfs_len = NFS_READ_SIZE; - NfsSend (); - } + ret = nfs_lookup_reply(pkt, len); + if (ret) + goto err_umount; + + nfs_state = STATE_READ_REQ; + nfs_offset = 0; break; case STATE_READLINK_REQ: - if (nfs_readlink_reply(pkt, len)) { - puts ("*** ERROR: Symlink fail\n"); - NfsState = STATE_UMOUNT_REQ; - NfsSend (); - } else { -#ifdef NFS_DEBUG - printf ("Symlink --> %s\n", nfs_path); -#endif - nfs_filename = basename (nfs_path); - nfs_path = dirname (nfs_path); - - NfsState = STATE_MOUNT_REQ; - NfsSend (); - } + ret = nfs_readlink_reply(pkt, len); + if (ret) + goto err_umount; + + debug("Symlink --> %s\n", nfs_path); + + nfs_filename = basename(nfs_path); + nfs_path = dirname(nfs_path); + + nfs_state = STATE_MOUNT_REQ; break; case STATE_READ_REQ: - rlen = nfs_read_reply (pkt, len); - NetSetTimeout (NFS_TIMEOUT * SECOND, NfsTimeout); - if (rlen > 0) { - nfs_offset += rlen; - NfsSend (); - } - else if ((rlen == -NFSERR_ISDIR)||(rlen == -NFSERR_INVAL)) { + ret = nfs_read_reply(pkt, len); + nfs_timer_start = get_time_ns(); + if (ret > 0) + nfs_offset += ret; + else if (ret == -EISDIR || ret == -EINVAL) /* symbolic link */ - NfsState = STATE_READLINK_REQ; - NfsSend (); - } else { - if ( ! rlen ) NfsDownloadState = NETLOOP_SUCCESS; - NfsState = STATE_UMOUNT_REQ; - NfsSend (); - } + nfs_state = STATE_READLINK_REQ; + else + goto err_umount; + show_progress(nfs_offset); break; } -} + nfs_send(); + + return; + +err_umount: + nfs_state = STATE_UMOUNT_REQ; + nfs_err = ret; + nfs_send(); + return; + +err_out: + nfs_state = STATE_DONE; + nfs_err = ret; +} -void -NfsStart (char *p) +static void nfs_start(char *p) { -#ifdef NFS_DEBUG - printf ("%s\n", __FUNCTION__); -#endif - NfsDownloadState = NETLOOP_FAIL; + debug("%s\n", __func__); - NfsServerIP = NetServerIP; nfs_path = (char *)nfs_path_buff; - if (nfs_path == NULL) { - NetState = NETLOOP_FAIL; - puts ("*** ERROR: Fail allocate memory\n"); - return; - } - - strcpy (nfs_path, p); + strcpy(nfs_path, p); nfs_filename = basename (nfs_path); nfs_path = dirname (nfs_path); -#if defined(CONFIG_NET_MULTI) - printf ("Using %s device\n", eth_get_name()); -#endif - - puts ("File transfer via NFS from server "); print_IPaddr (NfsServerIP); - puts ("; our IP address is "); print_IPaddr (NetOurIP); - - /* Check if we need to send across this subnet */ - if (NetOurGatewayIP && NetOurSubnetMask) { - IPaddr_t OurNet = NetOurIP & NetOurSubnetMask; - IPaddr_t ServerNet = NetServerIP & NetOurSubnetMask; + printf("\nFilename '%s/%s'.\n", nfs_path, nfs_filename); - if (OurNet != ServerNet) { - puts ("; sending through gateway "); - print_IPaddr (NetOurGatewayIP) ; - } - } - printf ("\nFilename '%s/%s'.", nfs_path, nfs_filename); - - NetSetTimeout (NFS_TIMEOUT * SECOND, NfsTimeout); - NetSetHandler (NfsHandler); - - NfsTimeoutCount = 0; - NfsState = STATE_PRCLOOKUP_PROG_MOUNT_REQ; + nfs_state = STATE_PRCLOOKUP_PROG_MOUNT_REQ; - /*FIX ME !!!*/ - NfsOurPort = 1000; - - /* zero out server ether in case the server ip has changed */ - memset (NetServerEther, 0, 6); - - NfsSend (); + nfs_send(); } static int do_nfs(struct command *cmdtp, int argc, char *argv[]) { - int rcode = 0; char *localfile; char *remotefile; @@ -728,23 +689,40 @@ static int do_nfs(struct command *cmdtp, int argc, char *argv[]) return 1; } - if (NetLoopInit(NFS) < 0) - goto out; + nfs_con = net_udp_new(net_get_serverip(), 0, nfs_handler); + if (IS_ERR(nfs_con)) { + nfs_err = PTR_ERR(nfs_con); + goto err_udp; + } + net_udp_bind(nfs_con, 1000); + + nfs_err = 0; - NfsStart(remotefile); + nfs_start(remotefile); - rcode = NetLoop(); - if (rcode < 0) { - rcode = 1; - goto out; + while (nfs_state != STATE_DONE) { + if (ctrlc()) { + nfs_err = -EINTR; + break; + } + net_poll(); + if (is_timeout(nfs_timer_start, NFS_TIMEOUT * SECOND)) { + show_progress(-1); + nfs_send(); + } } - /* NetLoop ok, update environment */ - netboot_update_env(); - -out: + net_unregister(nfs_con); +err_udp: close(net_store_fd); - return rcode; + if (nfs_err) { + printf("NFS failed: %s\n", strerror(-nfs_err)); + unlink(localfile); + } + + printf("\n"); + + return nfs_err == 0 ? 0 : 1; } static const __maybe_unused char cmd_nfs_help[] = diff --git a/net/ping.c b/net/ping.c index 1bc481af44..440e229ba0 100644 --- a/net/ping.c +++ b/net/ping.c @@ -2,103 +2,109 @@ #include <command.h> #include <clock.h> #include <net.h> +#include <errno.h> +#include <linux/err.h> -static ushort PingSeqNo; +static uint16_t ping_sequence_number; -static IPaddr_t NetPingIP; /* the ip address to ping */ +static IPaddr_t net_ping_ip; /* the ip address to ping */ -static int PingSend(void) -{ - static uchar mac[6]; - IP_t *ip; - ushort *s; - uchar *pkt; - - /* XXX always send arp request */ - - memcpy(mac, NetEtherNullAddr, 6); - - pr_debug("sending ARP for %08lx\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->ip_hl_v = 0x45; /* IP_HDR_SIZE / 4 (not including UDP) */ - ip->ip_tos = 0; - ip->ip_len = htons(IP_HDR_SIZE_NO_UDP + 8); - ip->ip_id = htons(NetIPID++); - ip->ip_off = htons(0x4000); /* No fragmentation */ - ip->ip_ttl = 255; - ip->ip_p = 0x01; /* ICMP */ - ip->ip_sum = 0; - NetCopyIP((void*)&ip->ip_src, &NetOurIP); /* already in network byte order */ - 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 */ - NetArpWaitTimerStart = get_time_ns(); - ArpRequest(); - return 1; /* waiting */ -} +#define PING_STATE_INIT 0 +#define PING_STATE_SUCCESS 1 + +static int ping_state; -static void -PingTimeout (void) +static struct net_connection *ping_con; + +static int ping_send(void) { - eth_halt(); - NetState = NETLOOP_FAIL; /* we did not get the reply */ + unsigned char *payload; + struct icmphdr *icmp; + uint64_t ts; + + icmp = ping_con->icmp; + + icmp->type = ICMP_ECHO_REQUEST; + icmp->code = 0; + icmp->checksum = 0; + icmp->un.echo.id = 0; + icmp->un.echo.sequence = htons(ping_sequence_number); + + ping_sequence_number++; + + payload = (char *)(icmp + 1); + ts = get_time_ns(); + memcpy(payload, &ts, sizeof(ts)); + payload[8] = 0xab; + payload[9] = 0xcd; + return net_icmp_send(ping_con, 9); } -static void -PingHandler (uchar * pkt, unsigned dest, unsigned src, unsigned len) +void ping_handler(char *pkt, unsigned len) { IPaddr_t tmp; - IP_t *ip = (IP_t *)pkt; + struct iphdr *ip = net_eth_to_iphdr(pkt); - tmp = NetReadIP((void *)&ip->ip_src); - if (tmp != NetPingIP) + tmp = net_read_ip((void *)&ip->saddr); + if (tmp != net_ping_ip) return; - NetState = NETLOOP_SUCCESS; + ping_state = PING_STATE_SUCCESS; } int do_ping(struct command *cmdtp, int argc, char *argv[]) { - if (argc < 2 || string_to_ip(argv[1], &NetPingIP)) + int ret; + uint64_t ping_start = 0; + + if (argc < 2) return COMMAND_ERROR_USAGE; - if (NetLoopInit(PING) < 0) + net_ping_ip = resolv(argv[1]); + if (!net_ping_ip) { + printf("unknown host %s\n", argv[1]); return 1; + } - NetSetTimeout (10 * SECOND, PingTimeout); - NetSetHandler (PingHandler); - PingSend(); + ping_con = net_icmp_new(net_ping_ip, ping_handler); + if (IS_ERR(ping_con)) { + ret = PTR_ERR(ping_con); + goto out; + } - if (NetLoop() < 0) { - printf("ping failed; host %s is not alive\n", argv[1]); - return 1; + ping_start = get_time_ns(); + ret = ping_send(); + if (ret) + goto out_unreg; + + ping_state = PING_STATE_INIT; + ping_sequence_number = 0; + + while (ping_state == PING_STATE_INIT) { + if (ctrlc()) { + ret = -EINTR; + break; + } + + net_poll(); + + if (is_timeout(ping_start, 10 * SECOND)) { + ping_start = get_time_ns(); + ret = ping_send(); + if (ret) + goto out_unreg; + } } - printf("host %s is alive\n", argv[1]); + if (!ret) + printf("host %s is alive\n", argv[1]); - return 0; +out_unreg: + net_unregister(ping_con); +out: + if (ret) + printf("ping failed: %s\n", strerror(-ret)); + return ping_state == PING_STATE_SUCCESS ? 0 : 1; } BAREBOX_CMD_START(ping) diff --git a/net/rarp.c b/net/rarp.c deleted file mode 100644 index 24818f8dba..0000000000 --- a/net/rarp.c +++ /dev/null @@ -1,101 +0,0 @@ -/* - * (C) Copyright 2000-2002 - * Wolfgang Denk, DENX Software Engineering, wd@denx.de. - * - * See file CREDITS for list of people who contributed to this - * project. - * - * This program is free software; you can redistribute it and/or - * modify it under the terms of the GNU General Public License as - * published by the Free Software Foundation; either version 2 of - * the License, or (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program; if not, write to the Free Software - * Foundation, Inc., 59 Temple Place, Suite 330, Boston, - * MA 02111-1307 USA - */ - -#include <common.h> -#include <command.h> -#include <clock.h> -#include <net.h> -#include "nfs.h" -#include "rarp.h" -#include "tftp.h" - -#define TIMEOUT 5 /* Seconds before trying BOOTP again */ -#ifndef CONFIG_NET_RETRY_COUNT -# define TIMEOUT_COUNT 5 /* # of timeouts before giving up */ -#else -# define TIMEOUT_COUNT (CONFIG_NET_RETRY_COUNT) -#endif - - -int RarpTry; - -/* - * Handle a RARP received packet. - */ -static void -RarpHandler(uchar * dummi0, unsigned dummi1, unsigned dummi2, unsigned dummi3) -{ -#ifdef DEBUG - puts ("Got good RARP\n"); -#endif - NetState = NETLOOP_SUCCESS; -} - - -/* - * Timeout on BOOTP request. - */ -static void -RarpTimeout(void) -{ - NetSetTimeout (TIMEOUT * SECOND, RarpTimeout); - RarpRequest (); -} - - -void -RarpRequest (void) -{ - int i; - uchar *pkt; - ARP_t * rarp; - - NetOurIP = 0; - RarpTry = 0; - - printf("RARP broadcast %d\n", ++RarpTry); - pkt = NetTxPacket; - - pkt += NetSetEther(pkt, NetBcastAddr, PROT_RARP); - - rarp = (ARP_t *)pkt; - - 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 ??*/ - /* dest. IP addr set to broadcast */ - for (i = 0; i <= 3; i++) { - rarp->ar_data[16 + i] = 0xff; - } - - NetSendPacket(NetTxPacket, (pkt - NetTxPacket) + ARP_HDR_SIZE); - - NetSetTimeout(TIMEOUT * SECOND, RarpTimeout); - NetSetHandler(RarpHandler); -} - diff --git a/net/sntp.h b/net/sntp.h deleted file mode 100644 index 8a097bfa38..0000000000 --- a/net/sntp.h +++ /dev/null @@ -1,61 +0,0 @@ -/* - * (C) Masami Komiya <mkomiya@sonare.it> 2005 - * - * This program is free software; you can redistribute it and/or - * modify it under the terms of the GNU General Public License as - * published by the Free Software Foundation; either version 2, or (at - * your option) any later version. - */ - -#ifndef __SNTP_H__ -#define __SNTP_H__ - -#define NTP_SERVICE_PORT 123 -#define SNTP_PACKET_LEN 48 - - -/* Leap Indicator */ -#define NTP_LI_NOLEAP 0x0 -#define NTP_LI_61SECS 0x1 -#define NTP_LI_59SECS 0x2 -#define NTP_LI_ALARM 0x3 - -/* Version */ - -#define NTP_VERSION 4 - -/* Mode */ -#define NTP_MODE_RESERVED 0 -#define NTP_MODE_SYMACTIVE 1 /* Symmetric Active */ -#define NTP_MODE_SYMPASSIVE 2 /* Symmetric Passive */ -#define NTP_MODE_CLIENT 3 -#define NTP_MODE_SERVER 4 -#define NTP_MODE_BROADCAST 5 -#define NTP_MODE_NTPCTRL 6 /* Reserved for NTP control message */ -#define NTP_MODE_PRIVATE 7 /* Reserved for private use */ - -struct sntp_pkt_t { -#if __LITTLE_ENDIAN - uchar mode:3; - uchar vn:3; - uchar li:2; -#else - uchar li:2; - uchar vn:3; - uchar mode:3; -#endif - uchar stratum; - uchar poll; - uchar precision; - uint root_delay; - uint root_dispersion; - uint reference_id; - unsigned long long reference_timestamp; - unsigned long long originate_timestamp; - unsigned long long receive_timestamp; - unsigned long long transmit_timestamp; -}; - -extern void SntpStart (void); /* Begin SNTP */ - -#endif /* __SNTP_H__ */ diff --git a/net/tftp.c b/net/tftp.c index e8a8a3a899..6345a7220d 100644 --- a/net/tftp.c +++ b/net/tftp.c @@ -13,13 +13,14 @@ #include <errno.h> #include <libgen.h> #include <fcntl.h> -#include "tftp.h" +#include <progress.h> +#include <getopt.h> +#include <fs.h> +#include <linux/stat.h> +#include <linux/err.h> -#define WELL_KNOWN_PORT 69 /* Well known TFTP port # */ +#define TFTP_PORT 69 /* Well known TFTP port # */ #define TIMEOUT 5 /* Seconds to timeout for a lost pkt */ -# define TIMEOUT_COUNT 10 /* # of timeouts before giving up */ - /* (for checking the image size) */ -#define HASHES_PER_LINE 65 /* Number of "loading" hashes per line */ /* * TFTP operations. @@ -32,174 +33,213 @@ #define TFTP_OACK 6 -static int TftpServerPort; /* The UDP port at their end */ -static int TftpOurPort; /* The UDP port at our end */ -static ulong TftpBlock; /* packet sequence number */ -static ulong TftpLastBlock; /* last packet sequence number received */ -static ulong TftpBlockWrap; /* count of sequence number wraparounds */ -static ulong TftpBlockWrapOffset; /* memory offset due to wrapping */ -static int TftpState; +static int tftp_server_port; /* The UDP port at their end */ +static unsigned int tftp_block; /* packet sequence number */ +static unsigned int tftp_last_block; /* last packet sequence number received */ +static int tftp_state; +static uint64_t tftp_timer_start; +static int tftp_err; #define STATE_RRQ 1 -#define STATE_DATA 2 -#define STATE_OACK 3 +#define STATE_WRQ 2 +#define STATE_RDATA 3 +#define STATE_WDATA 4 +#define STATE_OACK 5 +#define STATE_LAST 6 +#define STATE_DONE 7 #define TFTP_BLOCK_SIZE 512 /* default TFTP block size */ -#define TFTP_SEQUENCE_SIZE ((ulong)(1<<16)) /* sequence number is 16 bit */ static char *tftp_filename; +static struct net_connection *tftp_con; +static int tftp_fd; +static int tftp_size; -static int net_store_fd; +#ifdef CONFIG_NET_TFTP_PUSH +static int tftp_push; -static int store_block(unsigned block, uchar * src, unsigned len) +static inline void do_tftp_push(int push) { - ulong offset = block * TFTP_BLOCK_SIZE + TftpBlockWrapOffset; - ulong newsize = offset + len; - int ret; + tftp_push = push; +} - ret = write(net_store_fd, src, len); - if (ret < 0) - return ret; +#else - if (NetBootFileXferSize < newsize) - NetBootFileXferSize = newsize; - return 0; +#define tftp_push 0 + +static inline void do_tftp_push(int push) +{ } +#endif -static void TftpSend(void) +static int tftp_send(void) { - uchar *pkt; - uchar *xp; + unsigned char *xp; int len = 0; - ushort *s; - - /* - * We will always be sending some sort of packet, so - * cobble together the packet headers now. - */ - pkt = NetTxPacket + NetEthHdrSize() + IP_HDR_SIZE; + uint16_t *s; + unsigned char *pkt = net_udp_get_payload(tftp_con); + int ret; + static int last_len; - switch (TftpState) { + switch (tftp_state) { case STATE_RRQ: + case STATE_WRQ: xp = pkt; - s = (ushort *)pkt; - *s++ = htons(TFTP_RRQ); - pkt = (uchar *)s; - pkt += sprintf((uchar *)pkt, "%s%coctet%ctimeout%c%d", + s = (uint16_t *)pkt; + if (tftp_state == STATE_RRQ) + *s++ = htons(TFTP_RRQ); + else + *s++ = htons(TFTP_WRQ); + pkt = (unsigned char *)s; + pkt += sprintf((unsigned char *)pkt, "%s%coctet%ctimeout%c%d", tftp_filename, 0, 0, 0, TIMEOUT) + 1; len = pkt - xp; break; - case STATE_DATA: + case STATE_WDATA: + if (!tftp_push) + break; + + if (tftp_last_block == tftp_block) { + len = last_len; + break; + } + + tftp_last_block = tftp_block; + s = (uint16_t *)pkt; + *s++ = htons(TFTP_DATA); + *s++ = htons(tftp_block); + len = read(tftp_fd, s, 512); + if (len < 0) { + perror("read"); + tftp_err = -errno; + tftp_state = STATE_DONE; + return tftp_err; + } + tftp_size += len; + if (len < 512) + tftp_state = STATE_LAST; + len += 4; + last_len = len; + break; + + case STATE_RDATA: case STATE_OACK: xp = pkt; - s = (ushort *)pkt; + s = (uint16_t *)pkt; *s++ = htons(TFTP_ACK); - *s++ = htons(TftpBlock); - pkt = (uchar *)s; + *s++ = htons(tftp_block); + pkt = (unsigned char *)s; len = pkt - xp; break; } - NetSendUDPPacket(NetServerEther, NetServerIP, TftpServerPort, - TftpOurPort, len); -} + tftp_timer_start = get_time_ns(); + show_progress(tftp_size); + ret = net_udp_send(tftp_con, len); -static void TftpTimeout(void) -{ - puts("T "); - NetSetTimeout(TIMEOUT * SECOND, TftpTimeout); - TftpSend(); + return ret; } -static void TftpHandler(uchar * pkt, unsigned dest, unsigned src, unsigned len) +static void tftp_handler(char *packet, unsigned len) { - ushort proto; - ushort *s; - - if (dest != TftpOurPort) - return; - - if (TftpState != STATE_RRQ && src != TftpServerPort) - return; + uint16_t proto; + uint16_t *s; + char *pkt = net_eth_to_udp_payload(packet); + struct udphdr *udp = net_eth_to_udphdr(packet); + int ret; + len = net_eth_to_udplen(packet); if (len < 2) return; len -= 2; - /* warning: don't use increment (++) in ntohs() macros!! */ - s = (ushort *)pkt; + + s = (uint16_t *)pkt; proto = *s++; - pkt = (uchar *)s; + pkt = (unsigned char *)s; + switch (ntohs(proto)) { case TFTP_RRQ: case TFTP_WRQ: - case TFTP_ACK: - break; default: break; + case TFTP_ACK: + if (!tftp_push) + break; + + tftp_block = ntohs(*(uint16_t *)pkt); + if (tftp_block != tftp_last_block) { + debug("ack %d != %d\n", tftp_block, tftp_last_block); + break; + } + tftp_block++; + if (tftp_state == STATE_LAST) { + tftp_state = STATE_DONE; + break; + } + tftp_con->udp->uh_dport = udp->uh_sport; + tftp_state = STATE_WDATA; + tftp_send(); + break; case TFTP_OACK: - debug("Got OACK: %s %s\n", pkt, pkt+strlen(pkt)+1); - TftpState = STATE_OACK; - TftpServerPort = src; - TftpSend(); /* Send ACK */ + debug("Got OACK: %s %s\n", pkt, pkt + strlen(pkt) + 1); + tftp_server_port = ntohs(udp->uh_sport); + tftp_con->udp->uh_dport = udp->uh_sport; + + if (tftp_push) { + /* send first block */ + tftp_state = STATE_WDATA; + tftp_block = 1; + } else { + /* send ACK */ + tftp_state = STATE_OACK; + tftp_block = 0; + } + + tftp_send(); + break; case TFTP_DATA: if (len < 2) return; len -= 2; - TftpBlock = ntohs(*(ushort *)pkt); + tftp_block = ntohs(*(uint16_t *)pkt); - /* - * RFC1350 specifies that the first data packet will - * have sequence number 1. If we receive a sequence - * number of 0 this means that there was a wrap - * around of the (16 bit) counter. - */ - if (TftpBlock == 0) { - TftpBlockWrap++; - TftpBlockWrapOffset += TFTP_BLOCK_SIZE * TFTP_SEQUENCE_SIZE; - printf ("\n\t %lu MB received\n\t ", TftpBlockWrapOffset>>20); - } else { - if (((TftpBlock - 1) % 10) == 0) { - putchar('#'); - } else if ((TftpBlock % (10 * HASHES_PER_LINE)) == 0) { - puts("\n\t "); - } - } - - if (TftpState == STATE_RRQ) + if (tftp_state == STATE_RRQ) debug("Server did not acknowledge timeout option!\n"); - if (TftpState == STATE_RRQ || TftpState == STATE_OACK) { + if (tftp_state == STATE_RRQ || tftp_state == STATE_OACK) { /* first block received */ - TftpState = STATE_DATA; - TftpServerPort = src; - TftpLastBlock = 0; - TftpBlockWrap = 0; - TftpBlockWrapOffset = 0; - - if (TftpBlock != 1) { /* Assertion */ - printf("\nTFTP error: " - "First block is not block 1 (%ld)\n" - "Starting again\n\n", - TftpBlock); - NetState = NETLOOP_FAIL; + tftp_state = STATE_RDATA; + tftp_con->udp->uh_dport = udp->uh_sport; + tftp_server_port = ntohs(udp->uh_sport); + tftp_last_block = 0; + + if (tftp_block != 1) { /* Assertion */ + printf("error: First block is not block 1 (%ld)\n", + tftp_block); + tftp_err = -EINVAL; + tftp_state = STATE_DONE; break; } } - if (TftpBlock == TftpLastBlock) + if (tftp_block == tftp_last_block) /* Same block again; ignore it. */ break; - TftpLastBlock = TftpBlock; - NetSetTimeout(TIMEOUT * SECOND, TftpTimeout); + tftp_last_block = tftp_block; - if (store_block(TftpBlock - 1, pkt + 2, len) < 0) { + if (!(tftp_block % 10)) + tftp_size++; + + ret = write(tftp_fd, pkt + 2, len); + if (ret < 0) { perror("write"); - NetState = NETLOOP_FAIL; + tftp_err = -errno; + tftp_state = STATE_DONE; return; } @@ -207,116 +247,161 @@ static void TftpHandler(uchar * pkt, unsigned dest, unsigned src, unsigned len) * Acknowledge the block just received, which will prompt * the server for the next one. */ - TftpSend(); - - if (len < TFTP_BLOCK_SIZE) { - /* - * We received the whole thing. Try to - * run it. - */ - puts("\ndone\n"); - NetState = NETLOOP_SUCCESS; - } + tftp_send(); + + if (len < TFTP_BLOCK_SIZE) + tftp_state = STATE_DONE; + break; case TFTP_ERROR: - printf("\nTFTP error: '%s' (%d)\n", - pkt + 2, ntohs(*(ushort *)pkt)); - NetState = NETLOOP_FAIL; + debug("\nTFTP error: '%s' (%d)\n", + pkt + 2, ntohs(*(uint16_t *)pkt)); + switch (ntohs(*(uint16_t *)pkt)) { + case 1: tftp_err = -ENOENT; break; + case 2: tftp_err = -EACCES; break; + default: tftp_err = -EINVAL; break; + } + tftp_state = STATE_DONE; break; } } -void TftpStart(char *filename) -{ - char ip1[16], ip2[16]; - - tftp_filename = filename; - - printf("TFTP from server %s; our IP address is %s\n" - "\nFilename '%s'.\nLoading: *\b", - ip_to_string(NetServerIP, ip1), - ip_to_string(NetOurIP, ip2), - tftp_filename); - - NetSetTimeout(TIMEOUT * SECOND, TftpTimeout); - NetSetHandler(TftpHandler); - - TftpServerPort = WELL_KNOWN_PORT; - TftpState = STATE_RRQ; - /* Use a pseudo-random port */ - TftpOurPort = 1024 + ((unsigned int)get_time_ns() % 3072); - TftpBlock = 0; - - /* zero out server ether in case the server ip has changed */ - memset(NetServerEther, 0, 6); - - TftpSend(); -} - static int do_tftpb(struct command *cmdtp, int argc, char *argv[]) { - int rcode = 0; - char *localfile; - char *remotefile; + char *localfile, *remotefile, *file1, *file2; + char ip1[16]; + int opt; + struct stat s; + unsigned long flags; + + do_tftp_push(0); + tftp_last_block = 0; + tftp_size = 0; + + while((opt = getopt(argc, argv, "p")) > 0) { + switch(opt) { + case 'p': + do_tftp_push(1); + break; + } + } - if (argc < 2) + if (argc <= optind) return COMMAND_ERROR_USAGE; - remotefile = argv[1]; + file1 = argv[optind++]; - if (argc == 2) - localfile = basename(remotefile); + if (argc == optind) + file2 = basename(file1); else - localfile = argv[2]; + file2 = argv[optind]; + + if (tftp_push) { + localfile = file1; + remotefile = file2; + stat(localfile, &s); + flags = O_RDONLY; + } else { + localfile = file2; + remotefile = file1; + flags = O_WRONLY | O_CREAT; + } - net_store_fd = open(localfile, O_WRONLY | O_CREAT); - if (net_store_fd < 0) { + tftp_fd = open(localfile, flags); + if (tftp_fd < 0) { perror("open"); return 1; } - if (NetLoopInit(TFTP) < 0) - goto out; + tftp_con = net_udp_new(net_get_serverip(), TFTP_PORT, tftp_handler); + if (IS_ERR(tftp_con)) { + tftp_err = PTR_ERR(tftp_con); + goto out_close; + } + + tftp_filename = remotefile; - TftpStart(remotefile); + printf("TFTP %s server %s ('%s' -> '%s')\n", + tftp_push ? "to" : "from", + ip_to_string(net_get_serverip(), ip1), + file1, file2); - if (NetLoop() < 0) { - rcode = 1; - goto out; + init_progression_bar(tftp_push ? s.st_size : 0); + + tftp_timer_start = get_time_ns(); + tftp_state = tftp_push ? STATE_WRQ : STATE_RRQ; + tftp_block = 1; + + tftp_err = tftp_send(); + if (tftp_err) + goto out_unreg; + + while (tftp_state != STATE_DONE) { + if (ctrlc()) { + tftp_err = -EINTR; + break; + } + net_poll(); + if (is_timeout(tftp_timer_start, SECOND)) { + show_progress(-1); + tftp_send(); + } + } +out_unreg: + net_unregister(tftp_con); +out_close: + close(tftp_fd); + + if (tftp_err) { + printf("\ntftp failed: %s\n", strerror(-tftp_err)); + if (!tftp_push) + unlink(localfile); } - /* NetLoop ok, update environment */ - netboot_update_env(); + printf("\n"); -out: - close(net_store_fd); - return rcode; + return tftp_err == 0 ? 0 : 1; } static const __maybe_unused char cmd_tftp_help[] = -"Usage: tftp <file> [localfile]\n" -"Load a file via network using BootP/TFTP protocol.\n"; +"Usage: tftp <remotefile> [localfile]\n" +"Load a file from a TFTP server.\n" +#ifdef CONFIG_NET_TFTP_PUSH +"or\n" +" tftp -p <localfile> [remotefile]\n" +"Upload a file to a TFTP server\n" +#endif +; BAREBOX_CMD_START(tftp) .cmd = do_tftpb, - .usage = "Load file using tftp protocol", + .usage = +#ifdef CONFIG_NET_TFTP_PUSH + "(up-)" +#endif + "Load file using tftp protocol", BAREBOX_CMD_HELP(cmd_tftp_help) BAREBOX_CMD_END /** * @page tftp_command tftp * - * Usage is: tftp \<filename\> [\<localfilename\>] + * Usage: + * tftp \<remotefilename\> [\<localfilename\>] + * + * or + * + * tftp -p \<localfilename\> [\<remotefilename\>] * - * Load a file via network using BootP/TFTP protocol. The loaded file you - * can find after download in you current ramdisk. Refer \b ls command. + * Load a file from a tftp server or upload a file to a tftp server if + * the -p option is given. The second file argument can be skipped in + * which case the first filename is used (without the directory part). * - * \<localfile> can be the local filename only, or also a device name. In the - * case of a device name, the will gets stored there. This works also for - * partitions of flash memory. Refer \b erase, \b unprotect for flash - * preparation. + * \<localfile> can be the local filename or a device file under /dev. + * This also works for flash memory. Refer to \b erase, \b unprotect for + * flash preparation. * - * Note: This command is available only, if enabled in the menuconfig. + * Note: This command is available only if enabled in menuconfig. */ |