Commit 0378cd6e authored by Alessandro Rubini's avatar Alessandro Rubini

net: don't run bootp if IP address is static

This solves http://www.ohwr.org/issues/1218

We used to have a "needIP" variable, but we need three states:
if the address was set by bootp, we run bootp again when the link goes
up, but if it was a static assignment we do not (and keep the static one).

So I renamend needIP to a 3-valued ip_status. The rename ensured the
compiler found all users.  Also, the messages are now 3-headed, so
the user know whether the address is static or it comes from bootp.
Signed-off-by: Alessandro Rubini's avatarAlessandro Rubini <rubini@gnudd.com>
parent 0c72e452
......@@ -94,7 +94,7 @@ void arp_poll(void)
struct wr_sockaddr addr;
int len;
if (needIP)
if (ip_status == IP_TRAINING)
return; /* can't do ARP w/o an address... */
if ((len = ptpd_netif_recvfrom(arp_socket,
......
......@@ -92,6 +92,7 @@ int process_bootp(uint8_t * buf, int len)
if (memcmp(buf + BOOTP_CHADDR, mac, 6))
return 0;
ip_status = IP_OK_BOOTP;
setIP(buf + BOOTP_YIADDR);
getIP(ip);
......
......@@ -20,7 +20,7 @@
#define htons(x) x
#endif
int needIP = 1;
enum ip_status ip_status = IP_TRAINING;
static uint8_t myIP[4];
/* bootp: bigger buffer, UDP based */
......@@ -105,11 +105,13 @@ static void bootp_poll(void)
len = ptpd_netif_recvfrom(bootp_socket, &addr,
buf, sizeof(buf), NULL);
if (len > 0 && needIP)
process_bootp(buf, len);
if (!needIP)
if (ip_status != IP_TRAINING)
return;
if (len > 0)
process_bootp(buf, len);
if (time_before(timer_get_tics(), bootp_tics))
return;
......@@ -128,7 +130,7 @@ static void icmp_poll(void)
buf, sizeof(buf), NULL);
if (len <= 0)
return;
if (needIP)
if (ip_status == IP_TRAINING)
return;
if ((len = process_icmp(buf, len)) > 0)
......@@ -162,8 +164,8 @@ static void rdate_poll(void)
void ipv4_poll(int l_status)
{
if (l_status == LINK_WENT_UP)
needIP = 1;
if (l_status == LINK_WENT_UP && ip_status == IP_OK_BOOTP)
ip_status = IP_TRAINING;
bootp_poll();
icmp_poll();
......@@ -190,7 +192,7 @@ void setIP(unsigned char *IP)
while (*eb_ip != ip)
*eb_ip = ip;
needIP = (ip == 0);
if (!needIP)
if (ip == 0)
ip_status = IP_TRAINING;
bootp_retry = 0;
}
......@@ -42,7 +42,12 @@ unsigned int ipv4_checksum(unsigned short *buf, int shorts);
void arp_init(void);
void arp_poll(void);
extern int needIP;
enum ip_status {
IP_TRAINING,
IP_OK_BOOTP,
IP_OK_STATIC,
};
extern enum ip_status ip_status;
void setIP(unsigned char *IP);
void getIP(unsigned char *IP);
......
......@@ -66,7 +66,7 @@ void syslog_poll(int l_status)
static uint32_t down_tics;
int len = 0;
if (needIP)
if (ip_status == IP_TRAINING)
return;
if (!syslog_addr.daddr)
return;
......
......@@ -21,6 +21,7 @@
#include "wrc_ptp.h"
#include "hal_exports.h"
#include "lib/ipv4.h"
#include "shell.h"
extern int wrc_man_phase;
......@@ -93,6 +94,7 @@ void wrc_mon_gui(void)
&((struct wr_data *)ppi->ext_data)->servo_state;
int64_t crtt;
int64_t total_asymmetry;
char buf[20];
if (!last_jiffies)
last_jiffies = timer_get_tics() - 1 - wrc_ui_refperiod;
......@@ -157,10 +159,18 @@ void wrc_mon_gui(void)
cprintf(C_WHITE, "\nIPv4: ");
getIP(ip);
if (needIP)
format_ip(buf, ip);
switch (ip_status) {
case IP_TRAINING:
cprintf(C_RED, "BOOTP running");
else
cprintf(C_GREEN, "%d.%d.%d.%d", ip[0], ip[1], ip[2], ip[3]);
break;
case IP_OK_BOOTP:
cprintf(C_GREEN, "%s (from bootp)", buf);
break;
case IP_OK_STATIC:
cprintf(C_GREEN, "%s (static assignment)", buf);
break;
}
}
if (wrc_mon_status() == 0)
......
......@@ -43,16 +43,24 @@ static int cmd_ip(const char *args[])
if (!args[0] || !strcasecmp(args[0], "get")) {
getIP(ip);
} else if (!strcasecmp(args[0], "set") && args[1]) {
ip_status = IP_OK_STATIC;
decode_ip(args[1], ip);
setIP(ip);
} else {
return -EINVAL;
}
if (needIP) {
format_ip(buf, ip);
switch (ip_status) {
case IP_TRAINING:
pp_printf("IP-address: in training\n");
} else {
pp_printf("IP-address: %s\n", format_ip(buf, ip));
break;
case IP_OK_BOOTP:
pp_printf("IP-address: %s (from bootp)\n", buf);
break;
case IP_OK_STATIC:
pp_printf("IP-address: %s (static assignment)\n", buf);
break;
}
return 0;
}
......
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