Commit 70e23864 authored by Wesley W. Terpstra's avatar Wesley W. Terpstra

Fix a bug found by "Bartosz Bielawski" where 'ip set' doesn't update Etherbone IP.

As part of the fix, make the IP address private to the ipv4.c
parent 4300100f
......@@ -32,10 +32,12 @@ void arp_init(const char* if_name) {
static int process_arp(uint8_t* buf, int len) {
uint8_t hisMAC[6];
uint8_t hisIP[4];
uint8_t myIP[4];
if (len < ARP_END) return 0;
/* Is it ARP request targetting our IP? */
getIP(myIP);
if (buf[ARP_OPER+0] != 0 ||
buf[ARP_OPER+1] != 1 ||
memcmp(buf+ARP_TPA, myIP, 4))
......
#include <string.h>
#include "ipv4.h"
#include "hw/memlayout.h"
#include "hw/etherbone-config.h"
#define IP_VERSION 0
#define IP_TOS (IP_VERSION+1)
......@@ -126,9 +124,6 @@ int send_bootp(uint8_t* buf, int retry) {
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];
get_mac_addr(mac);
......@@ -144,14 +139,7 @@ int process_bootp(uint8_t* buf, int len)
if (memcmp(buf+BOOTP_CHADDR, mac, 6)) return 0;
mprintf("Discovered IP address!\n");
memcpy(myIP, buf+BOOTP_YIADDR, 4);
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;
setIP(buf+BOOTP_YIADDR);
return 1;
}
......@@ -23,9 +23,11 @@
int process_icmp(uint8_t* buf, int len) {
int iplen, hisBodyLen;
uint8_t hisIP[4];
uint8_t myIP[4];
uint16_t sum;
/* Is it IP targetting us? */
getIP(myIP);
if (buf[IP_VERSION] != 0x45 ||
memcmp(buf+IP_DEST, myIP, 4))
return 0;
......
......@@ -3,9 +3,11 @@
#include "endpoint.h"
#include "ipv4.h"
#include "ptpd_netif.h"
#include "hw/memlayout.h"
#include "hw/etherbone-config.h"
uint8_t myIP[4];
int needIP = 1;
static uint8_t myIP[4];
static wr_socket_t* ipv4_socket;
unsigned int ipv4_checksum(unsigned short* buf, int shorts) {
......@@ -35,32 +37,52 @@ void ipv4_init(const char* if_name) {
ipv4_socket = ptpd_netif_create_socket(PTPD_SOCK_RAW_ETHERNET, 0, &saddr);
}
void ipv4_poll(void) {
static int retry = 0;
static int timer = 0;
static int bootp_retry = 0;
static int bootp_timer = 0;
void ipv4_poll(void) {
uint8_t buf[400];
wr_sockaddr_t addr;
int len;
if ((len = ptpd_netif_recvfrom(ipv4_socket, &addr, buf, sizeof(buf), 0)) > 0) {
if (needIP && process_bootp(buf, len-14)) {
retry = 0;
needIP = 0;
timer = 0;
}
if (needIP)
process_bootp(buf, len-14);
if (!needIP && (len = process_icmp(buf, len-14)) > 0)
ptpd_netif_sendto(ipv4_socket, &addr, buf, len, 0);
}
if (needIP && timer == 0) {
len = send_bootp(buf, ++retry);
if (needIP && bootp_timer == 0) {
len = send_bootp(buf, ++bootp_retry);
memset(addr.mac, 0xFF, 6);
addr.ethertype = htons(0x0800); /* IPv4 */
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);
void arp_init(const char* if_name);
void arp_poll(void);
extern uint8_t myMAC[6];
extern uint8_t myIP[4];
extern int needIP;
void setIP(unsigned char* IP);
void getIP(unsigned char* IP);
int process_icmp(uint8_t* buf, int len);
int process_bootp(uint8_t* buf, int len); /* non-zero if IP was set */
......
......@@ -22,12 +22,10 @@ int cmd_ip(const char *args[])
unsigned char ip[4];
if (!args[0] || !strcasecmp(args[0], "get")) {
/* get current IP */
memcpy(ip, myIP, 4);
getIP(ip);
} else if (!strcasecmp(args[0], "set") && args[1]) {
decode_ip(args[1], ip);
memcpy(myIP, ip, 4);
needIP = !ip[0] && !ip[1] && !ip[2] && !ip[3];
setIP(ip);
} else {
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