configs: am65x_evm_a53: Disable PSCI_RESET
[oweals/u-boot.git] / net / net.c
index 31cf306ae71e11cf0f5ad65e992a1380d84faeba..ded86e7456715ebfc39ce1f52cdd5fd954870963 100644 (file)
--- a/net/net.c
+++ b/net/net.c
 #include <common.h>
 #include <command.h>
 #include <console.h>
-#include <environment.h>
+#include <env.h>
+#include <env_internal.h>
 #include <errno.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>
@@ -131,10 +135,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 */
@@ -657,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:
@@ -674,6 +675,11 @@ done:
        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;
 }
 
@@ -799,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;
@@ -822,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) {
@@ -1063,6 +1092,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;
@@ -1192,9 +1224,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 */
@@ -1235,6 +1264,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);
@@ -1455,7 +1487,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;
 
@@ -1465,7 +1498,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;
@@ -1474,6 +1508,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,
@@ -1489,10 +1525,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);
@@ -1594,3 +1628,15 @@ ushort env_get_vlan(char *var)
 {
        return string_to_vlan(env_get(var));
 }
+
+void eth_parse_enetaddr(const char *addr, uint8_t *enetaddr)
+{
+       char *end;
+       int i;
+
+       for (i = 0; i < 6; ++i) {
+               enetaddr[i] = addr ? simple_strtoul(addr, &end, 16) : 0;
+               if (addr)
+                       addr = (*end) ? end + 1 : end;
+       }
+}