[U-Boot] [RFC PATCH v2 8/8] net: Add actual networking support to sandbox's driver
Joe Hershberger
joe.hershberger at ni.com
Tue Feb 3 01:38:45 CET 2015
This doesn't build right now
Signed-off-by: Joe Hershberger <joe.hershberger at ni.com>
---
Changes in v2:
-Added the raw packet proof-of-concept patch.
drivers/net/sandbox-raw.c | 60 +++++++++++++++++++++++++++++++++++++++++++++++
drivers/net/sandbox.c | 23 ++++++++++++++++++
2 files changed, 83 insertions(+)
create mode 100644 drivers/net/sandbox-raw.c
diff --git a/drivers/net/sandbox-raw.c b/drivers/net/sandbox-raw.c
new file mode 100644
index 0000000..735783c
--- /dev/null
+++ b/drivers/net/sandbox-raw.c
@@ -0,0 +1,60 @@
+/*
+ * Copyright (c) 2015 National Instruments
+ *
+ * (C) Copyright 2015
+ * Joe Hershberger <joe.hershberger at ni.com>
+ *
+ * SPDX-License-Identifier: GPL-2.0+
+ */
+
+#include <linux/if.h>
+#include <linux/if_ether.h>
+#include <linux/if_packet.h>
+#include <sys/socket.h>
+#include <netinet/in.h>
+
+int sandbox_raw_init(int *sd, void **devp)
+{
+ uint8_t eth0mac[6];
+ int tempsd = 0;
+ struct ifreq ifr;
+ const char *ifname = "eth0";
+
+ strcpy(ifr.ifr_name, ifname);
+ ifr.ifr_addr.sa_family = AF_INET;
+ memset(eth0mac, 0, 6);
+ tempsd = socket(AF_INET, SOCK_RAW, IPPROTO_RAW);
+ if (tempsd < 0)
+ return 1;
+ if (ioctl(tempsd, SIOCGIFHWADDR, &ifr) < 0) {
+ close(tempsd);
+ return 1;
+ }
+ memcpy(eth0mac, ifr.ifr_hwaddr.sa_data, 6 * sizeof(uint8_t));
+ close(tempsd);
+
+ *devp = malloc(sizeof(struct sockaddr_ll));
+ struct sockaddr_ll *device = *devp;
+ memset(device, 0, sizeof(struct sockaddr_ll));
+ device->sll_ifindex = if_nametoindex(ifname);
+ device->sll_family = AF_PACKET;
+ memcpy(device->sll_addr, eth0mac, 6);
+ device->sll_halen = htons(6);
+
+ *sd = socket(PF_PACKET, SOCK_RAW, htons(ETH_P_ALL));
+
+ return 0;
+}
+
+int sandbox_raw_send(void *packet, int length, int sd, void *device)
+{
+ if (sendto(sd, packet, length, 0,
+ (struct sockaddr *)device, sizeof(struct sockaddr)) <= 0)
+ return 1;
+ return 0;
+}
+
+int sandbox_raw_halt(int sd)
+{
+ close(sd);
+}
diff --git a/drivers/net/sandbox.c b/drivers/net/sandbox.c
index 79a2fd5..c5113b4 100644
--- a/drivers/net/sandbox.c
+++ b/drivers/net/sandbox.c
@@ -20,8 +20,18 @@ struct eth_sandbox_priv {
IPaddr_t fake_host_ipaddr;
uchar recv_packet_buffer[PKTSIZE];
int recv_packet_length;
+#ifdef CONFIG_ETH_SANDBOX_RAW
+ void *device;
+ int sd;
+#endif
};
+#ifdef CONFIG_ETH_SANDBOX_RAW
+int sandbox_raw_init(int *sd, void **devp);
+int sandbox_raw_send(void *packet, int length, int sd, void *device);
+int sandbox_raw_halt(int sd);
+#endif
+
int sb_eth_init(struct udevice *dev, bd_t *bis)
{
debug("eth_sandbox: Init\n");
@@ -35,6 +45,11 @@ int sb_eth_init(struct udevice *dev, bd_t *bis)
for (i = 0; i < ARP_HLEN; i++)
priv->fake_host_hwaddr[i] = (uchar)int_array[i];
+#ifdef CONFIG_ETH_SANDBOX_RAW
+ int ret = sandbox_raw_init(&(priv->sd), &(priv->device));
+ if (ret)
+ return ret;
+#endif
return 0;
}
@@ -43,6 +58,9 @@ int sb_eth_send(struct udevice *dev, void *packet, int length)
debug("eth_sandbox: Send packet %d\n", length);
struct eth_sandbox_priv *priv = dev->priv;
+#ifdef CONFIG_ETH_SANDBOX_RAW
+ return sandbox_raw_send(packet, length, priv->sd, priv->device);
+#else
struct ethernet_hdr *eth = packet;
if (ntohs(eth->et_protlen) == PROT_ARP) {
struct arp_hdr *arp = packet + ETHER_HDR_SIZE;
@@ -133,6 +151,11 @@ int sb_eth_recv(struct udevice *dev)
void sb_eth_halt(struct udevice *dev)
{
debug("eth_sandbox: Halt\n");
+
+#ifdef CONFIG_ETH_SANDBOX_RAW
+ struct eth_sandbox_priv *priv = dev->priv;
+ sandbox_raw_halt(priv->sd);
+#endif
}
int sb_eth_write_hwaddr(struct udevice *dev)
--
1.7.11.5
More information about the U-Boot
mailing list