Commit 50868480 authored by Grzegorz Daniluk's avatar Grzegorz Daniluk

Merge branch 'tom-shell' of ohwr.org:hdl-core-lib/wr-cores/wrpc-sw into tom-shell

parents 8b0d60e5 3576653b
...@@ -32,10 +32,12 @@ void arp_init(const char* if_name) { ...@@ -32,10 +32,12 @@ void arp_init(const char* if_name) {
static int process_arp(uint8_t* buf, int len) { static int process_arp(uint8_t* buf, int len) {
uint8_t hisMAC[6]; uint8_t hisMAC[6];
uint8_t hisIP[4]; uint8_t hisIP[4];
uint8_t myIP[4];
if (len < ARP_END) return 0; if (len < ARP_END) return 0;
/* Is it ARP request targetting our IP? */ /* Is it ARP request targetting our IP? */
getIP(myIP);
if (buf[ARP_OPER+0] != 0 || if (buf[ARP_OPER+0] != 0 ||
buf[ARP_OPER+1] != 1 || buf[ARP_OPER+1] != 1 ||
memcmp(buf+ARP_TPA, myIP, 4)) memcmp(buf+ARP_TPA, myIP, 4))
......
#include <string.h> #include <string.h>
#include "ipv4.h" #include "ipv4.h"
#include "hw/memlayout.h"
#include "hw/etherbone-config.h"
#define IP_VERSION 0 #define IP_VERSION 0
#define IP_TOS (IP_VERSION+1) #define IP_TOS (IP_VERSION+1)
...@@ -126,9 +124,6 @@ int send_bootp(uint8_t* buf, int retry) { ...@@ -126,9 +124,6 @@ int send_bootp(uint8_t* buf, int retry) {
int process_bootp(uint8_t* buf, int len) int process_bootp(uint8_t* buf, int len)
{ {
volatile unsigned int *eb_ip =
(unsigned int*)(BASE_ETHERBONE_CFG + EB_IPV4);
unsigned int ip;
uint8_t mac[6]; uint8_t mac[6];
get_mac_addr(mac); get_mac_addr(mac);
...@@ -144,14 +139,7 @@ int process_bootp(uint8_t* buf, int len) ...@@ -144,14 +139,7 @@ int process_bootp(uint8_t* buf, int len)
if (memcmp(buf+BOOTP_CHADDR, mac, 6)) return 0; if (memcmp(buf+BOOTP_CHADDR, mac, 6)) return 0;
mprintf("Discovered IP address!\n"); mprintf("Discovered IP address!\n");
memcpy(myIP, buf+BOOTP_YIADDR, 4); setIP(buf+BOOTP_YIADDR);
ip =
(buf[BOOTP_YIADDR+0] << 24) |
(buf[BOOTP_YIADDR+1] << 16) |
(buf[BOOTP_YIADDR+2] << 8) |
(buf[BOOTP_YIADDR+3]);
while (*eb_ip != ip) *eb_ip = ip;
return 1; return 1;
} }
...@@ -23,9 +23,11 @@ ...@@ -23,9 +23,11 @@
int process_icmp(uint8_t* buf, int len) { int process_icmp(uint8_t* buf, int len) {
int iplen, hisBodyLen; int iplen, hisBodyLen;
uint8_t hisIP[4]; uint8_t hisIP[4];
uint8_t myIP[4];
uint16_t sum; uint16_t sum;
/* Is it IP targetting us? */ /* Is it IP targetting us? */
getIP(myIP);
if (buf[IP_VERSION] != 0x45 || if (buf[IP_VERSION] != 0x45 ||
memcmp(buf+IP_DEST, myIP, 4)) memcmp(buf+IP_DEST, myIP, 4))
return 0; return 0;
......
...@@ -3,9 +3,11 @@ ...@@ -3,9 +3,11 @@
#include "endpoint.h" #include "endpoint.h"
#include "ipv4.h" #include "ipv4.h"
#include "ptpd_netif.h" #include "ptpd_netif.h"
#include "hw/memlayout.h"
#include "hw/etherbone-config.h"
uint8_t myIP[4];
int needIP = 1; int needIP = 1;
static uint8_t myIP[4];
static wr_socket_t* ipv4_socket; static wr_socket_t* ipv4_socket;
unsigned int ipv4_checksum(unsigned short* buf, int shorts) { unsigned int ipv4_checksum(unsigned short* buf, int shorts) {
...@@ -35,32 +37,52 @@ void ipv4_init(const char* if_name) { ...@@ -35,32 +37,52 @@ void ipv4_init(const char* if_name) {
ipv4_socket = ptpd_netif_create_socket(PTPD_SOCK_RAW_ETHERNET, 0, &saddr); ipv4_socket = ptpd_netif_create_socket(PTPD_SOCK_RAW_ETHERNET, 0, &saddr);
} }
void ipv4_poll(void) { static int bootp_retry = 0;
static int retry = 0; static int bootp_timer = 0;
static int timer = 0;
void ipv4_poll(void) {
uint8_t buf[400]; uint8_t buf[400];
wr_sockaddr_t addr; wr_sockaddr_t addr;
int len; int len;
if ((len = ptpd_netif_recvfrom(ipv4_socket, &addr, buf, sizeof(buf), 0)) > 0) { if ((len = ptpd_netif_recvfrom(ipv4_socket, &addr, buf, sizeof(buf), 0)) > 0) {
if (needIP && process_bootp(buf, len-14)) { if (needIP)
retry = 0; process_bootp(buf, len-14);
needIP = 0;
timer = 0;
}
if (!needIP && (len = process_icmp(buf, len-14)) > 0) if (!needIP && (len = process_icmp(buf, len-14)) > 0)
ptpd_netif_sendto(ipv4_socket, &addr, buf, len, 0); ptpd_netif_sendto(ipv4_socket, &addr, buf, len, 0);
} }
if (needIP && timer == 0) { if (needIP && bootp_timer == 0) {
len = send_bootp(buf, ++retry); len = send_bootp(buf, ++bootp_retry);
memset(addr.mac, 0xFF, 6); memset(addr.mac, 0xFF, 6);
addr.ethertype = htons(0x0800); /* IPv4 */ addr.ethertype = htons(0x0800); /* IPv4 */
ptpd_netif_sendto(ipv4_socket, &addr, buf, len, 0); ptpd_netif_sendto(ipv4_socket, &addr, buf, len, 0);
} }
if (needIP && ++timer == 100000) timer = 0; if (needIP && ++bootp_timer == 100000) bootp_timer = 0;
}
void getIP(unsigned char* IP) {
memcpy(IP, myIP, 4);
}
void setIP(unsigned char* IP) {
volatile unsigned int *eb_ip =
(unsigned int*)(BASE_ETHERBONE_CFG + EB_IPV4);
unsigned int ip;
memcpy(myIP, IP, 4);
ip =
(myIP[0] << 24) | (myIP[1] << 16) |
(myIP[2] << 8) | (myIP[3]);
while (*eb_ip != ip) *eb_ip = ip;
needIP = (ip == 0);
if (!needIP) {
bootp_retry = 0;
bootp_timer = 0;
}
} }
...@@ -10,9 +10,9 @@ unsigned int ipv4_checksum(unsigned short* buf, int shorts); ...@@ -10,9 +10,9 @@ unsigned int ipv4_checksum(unsigned short* buf, int shorts);
void arp_init(const char* if_name); void arp_init(const char* if_name);
void arp_poll(void); void arp_poll(void);
extern uint8_t myMAC[6];
extern uint8_t myIP[4];
extern int needIP; extern int needIP;
void setIP(unsigned char* IP);
void getIP(unsigned char* IP);
int process_icmp(uint8_t* buf, int len); int process_icmp(uint8_t* buf, int len);
int process_bootp(uint8_t* buf, int len); /* non-zero if IP was set */ int process_bootp(uint8_t* buf, int len); /* non-zero if IP was set */
......
ptp-noposix @ 45c029e5
Subproject commit d04a278a9dc441984c8947e11395b7c6e39d8325 Subproject commit 45c029e5fe5e64a75393b174d8e1478001916425
...@@ -22,12 +22,10 @@ int cmd_ip(const char *args[]) ...@@ -22,12 +22,10 @@ int cmd_ip(const char *args[])
unsigned char ip[4]; unsigned char ip[4];
if (!args[0] || !strcasecmp(args[0], "get")) { if (!args[0] || !strcasecmp(args[0], "get")) {
/* get current IP */ getIP(ip);
memcpy(ip, myIP, 4);
} else if (!strcasecmp(args[0], "set") && args[1]) { } else if (!strcasecmp(args[0], "set") && args[1]) {
decode_ip(args[1], ip); decode_ip(args[1], ip);
memcpy(myIP, ip, 4); setIP(ip);
needIP = !ip[0] && !ip[1] && !ip[2] && !ip[3];
} else { } else {
return -EINVAL; return -EINVAL;
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment