Merge tag 'efi-2020-07-rc6' of https://gitlab.denx.de/u-boot/custodians/u-boot-efi
[oweals/u-boot.git] / net / net.c
index a4932f46d905f51d0466983b2032a35b134e4e73..37932919d049a7a834bb999e83f593f9b5d47a29 100644 (file)
--- a/net/net.c
+++ b/net/net.c
  *                     - own IP address
  *     We want:        - network time
  *     Next step:      none
+ *
+ * WOL:
+ *
+ *     Prerequisites:  - own ethernet address
+ *     We want:        - magic packet or timeout
+ *     Next step:      none
  */
 
 
 #include <common.h>
+#include <bootstage.h>
 #include <command.h>
 #include <console.h>
-#include <environment.h>
+#include <env.h>
+#include <env_internal.h>
 #include <errno.h>
+#include <image.h>
+#include <log.h>
 #include <net.h>
 #include <net/fastboot.h>
 #include <net/tftp.h>
+#if defined(CONFIG_CMD_PCAP)
+#include <net/pcap.h>
+#endif
 #if defined(CONFIG_LED_STATUS)
 #include <miiphy.h>
 #include <status_led.h>
 #if defined(CONFIG_CMD_SNTP)
 #include "sntp.h"
 #endif
+#if defined(CONFIG_CMD_WOL)
+#include "wol.h"
+#endif
 
 /** BOOTP EXTENTIONS **/
 
@@ -122,10 +138,6 @@ struct in_addr net_dns_server;
 struct in_addr net_dns_server2;
 #endif
 
-#ifdef CONFIG_MCAST_TFTP       /* Multicast TFTP */
-struct in_addr net_mcast_addr;
-#endif
-
 /** END OF BOOTP EXTENTIONS **/
 
 /* Our ethernet address */
@@ -165,6 +177,8 @@ ushort              net_native_vlan = 0xFFFF;
 
 /* Boot File name */
 char net_boot_file_name[1024];
+/* Indicates whether the file name was specified on the command line */
+bool net_boot_file_name_explicit;
 /* The actual transferred size of the bootfile (in bytes) */
 u32 net_boot_file_size;
 /* Boot file size in blocks as reported by the DHCP server */
@@ -205,26 +219,6 @@ int __maybe_unused net_busy_flag;
 
 /**********************************************************************/
 
-static int on_bootfile(const char *name, const char *value, enum env_op op,
-       int flags)
-{
-       if (flags & H_PROGRAMMATIC)
-               return 0;
-
-       switch (op) {
-       case env_op_create:
-       case env_op_overwrite:
-               copy_filename(net_boot_file_name, value,
-                             sizeof(net_boot_file_name));
-               break;
-       default:
-               break;
-       }
-
-       return 0;
-}
-U_BOOT_ENV_CALLBACK(bootfile, on_bootfile);
-
 static int on_ipaddr(const char *name, const char *value, enum env_op op,
        int flags)
 {
@@ -317,10 +311,20 @@ U_BOOT_ENV_CALLBACK(dnsip, on_dnsip);
  */
 void net_auto_load(void)
 {
-#if defined(CONFIG_CMD_NFS)
+#if defined(CONFIG_CMD_NFS) && !defined(CONFIG_SPL_BUILD)
        const char *s = env_get("autoload");
 
        if (s != NULL && strcmp(s, "NFS") == 0) {
+               if (net_check_prereq(NFS)) {
+/* We aren't expecting to get a serverip, so just accept the assigned IP */
+#ifdef CONFIG_BOOTP_SERVERIP
+                       net_set_state(NETLOOP_SUCCESS);
+#else
+                       printf("Cannot autoload with NFS\n");
+                       net_set_state(NETLOOP_FAIL);
+#endif
+                       return;
+               }
                /*
                 * Use NFS to load the bootfile.
                 */
@@ -336,6 +340,16 @@ void net_auto_load(void)
                net_set_state(NETLOOP_SUCCESS);
                return;
        }
+       if (net_check_prereq(TFTPGET)) {
+/* We aren't expecting to get a serverip, so just accept the assigned IP */
+#ifdef CONFIG_BOOTP_SERVERIP
+               net_set_state(NETLOOP_SUCCESS);
+#else
+               printf("Cannot autoload with TFTPGET\n");
+               net_set_state(NETLOOP_FAIL);
+#endif
+               return;
+       }
        tftp_start(TFTPGET);
 }
 
@@ -393,6 +407,7 @@ void net_init(void)
 int net_loop(enum proto_t protocol)
 {
        int ret = -EINVAL;
+       enum net_loop_state prev_net_state = net_state;
 
        net_restarted = 0;
        net_dev_exists = 0;
@@ -430,6 +445,7 @@ restart:
        case 1:
                /* network not configured */
                eth_halt();
+               net_set_state(prev_net_state);
                return -ENODEV;
 
        case 2:
@@ -483,7 +499,7 @@ restart:
                        ping_start();
                        break;
 #endif
-#if defined(CONFIG_CMD_NFS)
+#if defined(CONFIG_CMD_NFS) && !defined(CONFIG_SPL_BUILD)
                case NFS:
                        nfs_start();
                        break;
@@ -512,6 +528,11 @@ restart:
                case LINKLOCAL:
                        link_local_start();
                        break;
+#endif
+#if defined(CONFIG_CMD_WOL)
+               case WOL:
+                       wol_start();
+                       break;
 #endif
                default:
                        break;
@@ -543,9 +564,6 @@ restart:
         */
        for (;;) {
                WATCHDOG_RESET();
-#ifdef CONFIG_SHOW_ACTIVITY
-               show_activity(1);
-#endif
                if (arp_timeout_check() > 0)
                        time_start = get_timer(0);
 
@@ -621,7 +639,7 @@ restart:
                                printf("Bytes transferred = %d (%x hex)\n",
                                       net_boot_file_size, net_boot_file_size);
                                env_set_hex("filesize", net_boot_file_size);
-                               env_set_hex("fileaddr", load_addr);
+                               env_set_hex("fileaddr", image_load_addr);
                        }
                        if (protocol != NETCONS)
                                eth_halt();
@@ -639,6 +657,7 @@ restart:
                        /* Invalidate the last protocol */
                        eth_set_last_protocol(BOOTP);
                        debug_cond(DEBUG_INT_STATE, "--- net_loop Fail!\n");
+                       ret = -ENONET;
                        goto done;
 
                case NETLOOP_CONTINUE:
@@ -654,6 +673,12 @@ done:
        /* Clear out the handlers */
        net_set_udp_handler(NULL);
        net_set_icmp_handler(NULL);
+#endif
+       net_set_state(prev_net_state);
+
+#if defined(CONFIG_CMD_PCAP)
+       if (pcap_active())
+               pcap_print_status();
 #endif
        return ret;
 }
@@ -780,8 +805,24 @@ void net_set_timeout_handler(ulong iv, thand_f *f)
        }
 }
 
+uchar *net_get_async_tx_pkt_buf(void)
+{
+       if (arp_is_waiting())
+               return arp_tx_packet; /* If we are waiting, we already sent */
+       else
+               return net_tx_packet;
+}
+
 int net_send_udp_packet(uchar *ether, struct in_addr dest, int dport, int sport,
                int payload_len)
+{
+       return net_send_ip_packet(ether, dest, dport, sport, payload_len,
+                                 IPPROTO_UDP, 0, 0, 0);
+}
+
+int net_send_ip_packet(uchar *ether, struct in_addr dest, int dport, int sport,
+                      int payload_len, int proto, u8 action, u32 tcp_seq_num,
+                      u32 tcp_ack_num)
 {
        uchar *pkt;
        int eth_hdr_size;
@@ -803,9 +844,16 @@ int net_send_udp_packet(uchar *ether, struct in_addr dest, int dport, int sport,
        pkt = (uchar *)net_tx_packet;
 
        eth_hdr_size = net_set_ether(pkt, ether, PROT_IP);
-       pkt += eth_hdr_size;
-       net_set_udp_header(pkt, dest, dport, sport, payload_len);
-       pkt_hdr_size = eth_hdr_size + IP_UDP_HDR_SIZE;
+
+       switch (proto) {
+       case IPPROTO_UDP:
+               net_set_udp_header(pkt + eth_hdr_size, dest, dport, sport,
+                                  payload_len);
+               pkt_hdr_size = eth_hdr_size + IP_UDP_HDR_SIZE;
+               break;
+       default:
+               return -EINVAL;
+       }
 
        /* if MAC address was not discovered yet, do an ARP request */
        if (memcmp(ether, net_null_ethaddr, 6) == 0) {
@@ -837,9 +885,6 @@ int net_send_udp_packet(uchar *ether, struct in_addr dest, int dport, int sport,
  * to the algorithm in RFC815. It returns NULL or the pointer to
  * a complete packet, in static storage
  */
-#ifndef CONFIG_NET_MAXDEFRAG
-#define CONFIG_NET_MAXDEFRAG 16384
-#endif
 #define IP_PKTSIZE (CONFIG_NET_MAXDEFRAG)
 
 #define IP_MAXUDP (IP_PKTSIZE - IP_HDR_SIZE)
@@ -1044,6 +1089,9 @@ void net_process_received_packet(uchar *in_packet, int len)
 
        debug_cond(DEBUG_NET_PKT, "packet received\n");
 
+#if defined(CONFIG_CMD_PCAP)
+       pcap_post(in_packet, len, false);
+#endif
        net_rx_packet = in_packet;
        net_rx_packet_len = len;
        et = (struct ethernet_hdr *)in_packet;
@@ -1173,9 +1221,6 @@ void net_process_received_packet(uchar *in_packet, int len)
                dst_ip = net_read_ip(&ip->ip_dst);
                if (net_ip.s_addr && dst_ip.s_addr != net_ip.s_addr &&
                    dst_ip.s_addr != 0xFFFFFFFF) {
-#ifdef CONFIG_MCAST_TFTP
-                       if (net_mcast_addr != dst_ip)
-#endif
                                return;
                }
                /* Read source IP address for later use */
@@ -1216,6 +1261,9 @@ void net_process_received_packet(uchar *in_packet, int len)
                        return;
                }
 
+               if (ntohs(ip->udp_len) < UDP_HDR_SIZE || ntohs(ip->udp_len) > ntohs(ip->ip_len))
+                       return;
+
                debug_cond(DEBUG_DEV_PKT,
                           "received UDP (to=%pI4, from=%pI4, len=%d)\n",
                           &dst_ip, &src_ip, len);
@@ -1223,7 +1271,7 @@ void net_process_received_packet(uchar *in_packet, int len)
 #ifdef CONFIG_UDP_CHECKSUM
                if (ip->udp_xsum != 0) {
                        ulong   xsum;
-                       ushort *sumptr;
+                       u8 *sumptr;
                        ushort  sumlen;
 
                        xsum  = ip->ip_p;
@@ -1234,22 +1282,16 @@ void net_process_received_packet(uchar *in_packet, int len)
                        xsum += (ntohl(ip->ip_dst.s_addr) >>  0) & 0x0000ffff;
 
                        sumlen = ntohs(ip->udp_len);
-                       sumptr = (ushort *)&(ip->udp_src);
+                       sumptr = (u8 *)&ip->udp_src;
 
                        while (sumlen > 1) {
-                               ushort sumdata;
-
-                               sumdata = *sumptr++;
-                               xsum += ntohs(sumdata);
+                               /* inlined ntohs() to avoid alignment errors */
+                               xsum += (sumptr[0] << 8) + sumptr[1];
+                               sumptr += 2;
                                sumlen -= 2;
                        }
-                       if (sumlen > 0) {
-                               ushort sumdata;
-
-                               sumdata = *(unsigned char *)sumptr;
-                               sumdata = (sumdata << 8) & 0xff00;
-                               xsum += sumdata;
-                       }
+                       if (sumlen > 0)
+                               xsum += (sumptr[0] << 8) + sumptr[0];
                        while ((xsum >> 16) != 0) {
                                xsum = (xsum & 0x0000ffff) +
                                       ((xsum >> 16) & 0x0000ffff);
@@ -1278,6 +1320,11 @@ void net_process_received_packet(uchar *in_packet, int len)
                                      ntohs(ip->udp_src),
                                      ntohs(ip->udp_len) - UDP_HDR_SIZE);
                break;
+#ifdef CONFIG_CMD_WOL
+       case PROT_WOL:
+               wol_receive(ip, len);
+               break;
+#endif
        }
 }
 
@@ -1317,7 +1364,7 @@ static int net_check_prereq(enum proto_t protocol)
                /* Fall through */
        case TFTPGET:
        case TFTPPUT:
-               if (net_server_ip.s_addr == 0) {
+               if (net_server_ip.s_addr == 0 && !is_serverip_in_cmd()) {
                        puts("*** ERROR: `serverip' not set\n");
                        return 1;
                }
@@ -1431,7 +1478,8 @@ int net_update_ether(struct ethernet_hdr *et, uchar *addr, uint prot)
        }
 }
 
-void net_set_ip_header(uchar *pkt, struct in_addr dest, struct in_addr source)
+void net_set_ip_header(uchar *pkt, struct in_addr dest, struct in_addr source,
+                      u16 pkt_len, u8 proto)
 {
        struct ip_udp_hdr *ip = (struct ip_udp_hdr *)pkt;
 
@@ -1441,7 +1489,8 @@ void net_set_ip_header(uchar *pkt, struct in_addr dest, struct in_addr source)
        /* IP_HDR_SIZE / 4 (not including UDP) */
        ip->ip_hl_v  = 0x45;
        ip->ip_tos   = 0;
-       ip->ip_len   = htons(IP_HDR_SIZE);
+       ip->ip_len   = htons(pkt_len);
+       ip->ip_p     = proto;
        ip->ip_id    = htons(net_ip_id++);
        ip->ip_off   = htons(IP_FLAGS_DFRAG);   /* Don't fragment */
        ip->ip_ttl   = 255;
@@ -1450,6 +1499,8 @@ void net_set_ip_header(uchar *pkt, struct in_addr dest, struct in_addr source)
        net_copy_ip((void *)&ip->ip_src, &source);
        /* already in network byte order */
        net_copy_ip((void *)&ip->ip_dst, &dest);
+
+       ip->ip_sum   = compute_ip_checksum(ip, IP_HDR_SIZE);
 }
 
 void net_set_udp_header(uchar *pkt, struct in_addr dest, int dport, int sport,
@@ -1465,10 +1516,8 @@ void net_set_udp_header(uchar *pkt, struct in_addr dest, int dport, int sport,
        if (len & 1)
                pkt[IP_UDP_HDR_SIZE + len] = 0;
 
-       net_set_ip_header(pkt, dest, net_ip);
-       ip->ip_len   = htons(IP_UDP_HDR_SIZE + len);
-       ip->ip_p     = IPPROTO_UDP;
-       ip->ip_sum   = compute_ip_checksum(ip, IP_HDR_SIZE);
+       net_set_ip_header(pkt, dest, net_ip, IP_UDP_HDR_SIZE + len,
+                         IPPROTO_UDP);
 
        ip->udp_src  = htons(sport);
        ip->udp_dst  = htons(dport);
@@ -1478,16 +1527,41 @@ void net_set_udp_header(uchar *pkt, struct in_addr dest, int dport, int sport,
 
 void copy_filename(char *dst, const char *src, int size)
 {
-       if (*src && (*src == '"')) {
+       if (src && *src && (*src == '"')) {
                ++src;
                --size;
        }
 
-       while ((--size > 0) && *src && (*src != '"'))
+       while ((--size > 0) && src && *src && (*src != '"'))
                *dst++ = *src++;
        *dst = '\0';
 }
 
+int is_serverip_in_cmd(void)
+{
+       return !!strchr(net_boot_file_name, ':');
+}
+
+int net_parse_bootfile(struct in_addr *ipaddr, char *filename, int max_len)
+{
+       char *colon;
+
+       if (net_boot_file_name[0] == '\0')
+               return 0;
+
+       colon = strchr(net_boot_file_name, ':');
+       if (colon) {
+               if (ipaddr)
+                       *ipaddr = string_to_ip(net_boot_file_name);
+               strncpy(filename, colon + 1, max_len);
+       } else {
+               strncpy(filename, net_boot_file_name, max_len);
+       }
+       filename[max_len - 1] = '\0';
+
+       return 1;
+}
+
 #if    defined(CONFIG_CMD_NFS)         || \
        defined(CONFIG_CMD_SNTP)        || \
        defined(CONFIG_CMD_DNS)