Commit 13801408 authored by hongming's avatar hongming

Add new shell command tcpip.

        tcpip tcp x , set the tcp server listen port
        tcpip udp txip x.x.x.x , set the udp tx destination ip addr.
        tcpip udp txport x , set the udp tx destination port, default
2000
        tcpip udp rxport x , set the udp rx destiniation port. default
(>256).
        tcpip udp txgw x.x.x.x , set the udp gateway, default x.x.x.1
        tcpip udp txsn x.x.x.x , set the subnet, default 255.255.255.0
parent 5db20d6c
#include <wrc.h>
#include <wrpc.h>
#include <string.h>
#include "endpoint.h"
#include "arp.h"
#include "hw/tcpip-config.h"
#include "tcpip_config.h"
enum tcpip_status tcpip_status;
uint8_t arp_count=0;
void tcpip_init(uint8_t mac_addr[])
{
volatile unsigned int *tcpip_tmp;
uint32_t tmp=0;
tcpip_tmp = (unsigned int *)(BASE_TCPIP_CFG + TCPIP_MAC_HIGH16);
tmp = ((uint32_t) mac_addr[0] << 8)
| ((uint32_t) mac_addr[1]);
......@@ -19,27 +26,93 @@ void tcpip_init(uint8_t mac_addr[])
| ((uint32_t) mac_addr[4] << 8)
| ((uint32_t) mac_addr[5]);
*tcpip_tmp = tmp;
// default udp tx dst/src port
tcpip_tx_dst_port(2000);
tcpip_tx_src_port(2000);
}
void tcpip_config(unsigned char *IP)
void tcpip_ip_addr(uint8_t *ip)
{
unsigned int *tcpip_tmp;
uint32_t tmp;
memcpy((uint8_t *)(BASE_TCPIP_CFG + TCPIP_IP_ADDR), ip, 4);
}
void tcpip_gateway_addr(uint8_t *gw)
{
memcpy((uint8_t *)(BASE_TCPIP_CFG + TCPIP_GATEWAY), gw, 4);
}
tcpip_tmp = (unsigned int *)(BASE_TCPIP_CFG + TCPIP_IP_ADDR);
tmp = (*IP << 24) | (*(IP+1) << 16) | (*(IP+2) << 8) | (*(IP+3));
*tcpip_tmp = tmp;
void tcpip_subnet_addr(uint8_t *sn)
{
memcpy((uint8_t *)(BASE_TCPIP_CFG + TCPIP_SUBNET_MASK), sn, 4);
}
tcpip_tmp = (unsigned int *)(BASE_TCPIP_CFG + TCPIP_GATEWAY);
tmp = (tmp & 0xFFFFFF00) + 1;
*tcpip_tmp = tmp;
void tcpip_rx_dst_port(uint16_t port)
{
volatile unsigned int *rdp =
(unsigned int *)(BASE_TCPIP_CFG + TCPIP_UDP_RX_PORT);
*rdp = 0x10000 + (uint32_t)port; // 0x10000 is used to enable setting rx dst port
}
void tcpip_tx_src_port(uint16_t port)
{
volatile unsigned int *tsp =
(unsigned int *)(BASE_TCPIP_CFG + TCPIP_UDP_TX_SRC_PORT);
*tsp = (uint32_t)port;
}
void tcpip_tx_dst_port(uint16_t port)
{
volatile unsigned int *tdp =
(unsigned int *)(BASE_TCPIP_CFG + TCPIP_UDP_TX_DST_PORT);
*tdp = (uint32_t)port;
}
void tcpip_set_hisIP(uint8_t *ip)
{
memcpy((uint8_t *)(BASE_TCPIP_CFG + TCPIP_UDP_TX_DST_IP), ip, 4);
tcpip_status = TCPIP_ARP;
arp_count = 0;
}
tcpip_tmp = (unsigned int *)(BASE_TCPIP_CFG + TCPIP_SUBNET_MASK);
tmp = 0xFFFFFF00;
void tcpip_get_hisIP(uint8_t *ip)
{
memcpy(ip, (uint8_t *)(BASE_TCPIP_CFG + TCPIP_UDP_TX_DST_IP), 4);
}
void tcpip_set_hisMAC(uint8_t mac_addr[])
{
volatile unsigned int *tcpip_tmp;
uint32_t tmp=0;
tcpip_tmp = (unsigned int *)(BASE_TCPIP_CFG + TCPIP_MAC_HIGH16);
tmp = ((uint32_t) mac_addr[0] << 8)
| ((uint32_t) mac_addr[1]);
*tcpip_tmp = tmp;
tcpip_tmp = (unsigned int *)(BASE_TCPIP_CFG + TCPIP_TCP_LOCAL_PORT);
tmp = 8000;
tcpip_tmp = (unsigned int *)(BASE_TCPIP_CFG + TCPIP_MAC_LOW32);
tmp = ((uint32_t) mac_addr[2] << 24)
| ((uint32_t) mac_addr[3] << 16)
| ((uint32_t) mac_addr[4] << 8)
| ((uint32_t) mac_addr[5]);
*tcpip_tmp = tmp;
}
void tcpip_get_hisMAC(uint8_t mac_addr[])
{
memcpy(mac_addr, (uint8_t *)(BASE_TCPIP_CFG + TCPIP_MAC_HIGH16+2), 2);
memcpy(mac_addr+2, (uint8_t *)(BASE_TCPIP_CFG + TCPIP_MAC_LOW32), 4);
}
void tcpip_rx_tcp_port(uint16_t port)
{
volatile unsigned int *rtp =
(unsigned int *)(BASE_TCPIP_CFG + TCPIP_TCP_LOCAL_PORT);
*rtp = (uint32_t)port;
}
void tcpip_arp()
{
uint8_t * ip;
tcpip_get_hisIP(ip);
send_arp(ip);
}
\ No newline at end of file
/*
* This work is part of the White Rabbit project
*
* Released according to the GNU GPL, version 2 or any later version.
*/
#ifndef ARP_H
#define ARP_H
#include <inttypes.h>
#include "ptpd_netif.h" /* for sockaddr in prototype */
#define ARP_HTYPE 0
#define ARP_PTYPE (ARP_HTYPE+2)
#define ARP_HLEN (ARP_PTYPE+2)
#define ARP_PLEN (ARP_HLEN+1)
#define ARP_OPER (ARP_PLEN+1)
#define ARP_SHA (ARP_OPER+2)
#define ARP_SPA (ARP_SHA+6)
#define ARP_THA (ARP_SPA+4)
#define ARP_TPA (ARP_THA+6)
#define ARP_END (ARP_TPA+4)
void send_arp(uint8_t * hisIP);
#endif
......@@ -4,6 +4,23 @@
#include <stdint.h>
void tcpip_init(uint8_t mac_addr[]);
void tcpip_config(unsigned char *IP);
void tcpip_ip_addr(uint8_t *ip);
void tcpip_gateway_addr(uint8_t *gw);
void tcpip_subnet_addr(uint8_t *sn);
void tcpip_rx_dst_port(uint16_t port);
void tcpip_tx_src_port(uint16_t port);
void tcpip_tx_dst_port(uint16_t port);
void tcpip_set_hisIP(uint8_t *ip);
void tcpip_get_hisIP(uint8_t *ip);
void tcpip_set_hisMAC(uint8_t mac_addr[]);
void tcpip_rx_tcp_port(uint16_t port);
void tcpip_arp();
enum tcpip_status {
TCPIP_ARP,
TCPIP_OK,
};
extern enum tcpip_status tcpip_status;
extern uint8_t arp_count;
#endif
......@@ -13,6 +13,8 @@
#include "endpoint.h"
#include "ipv4.h"
#include "ptpd_netif.h"
#include "arp.h"
#include "tcpip_config.h"
static uint8_t __arp_queue[128];
static struct wrpc_socket __static_arp_socket = {
......@@ -21,28 +23,17 @@ static struct wrpc_socket __static_arp_socket = {
};
static struct wrpc_socket *arp_socket;
#define ARP_HTYPE 0
#define ARP_PTYPE (ARP_HTYPE+2)
#define ARP_HLEN (ARP_PTYPE+2)
#define ARP_PLEN (ARP_HLEN+1)
#define ARP_OPER (ARP_PLEN+1)
#define ARP_SHA (ARP_OPER+2)
#define ARP_SPA (ARP_SHA+6)
#define ARP_THA (ARP_SPA+4)
#define ARP_TPA (ARP_THA+6)
#define ARP_END (ARP_TPA+4)
static void arp_init(void)
{
struct wr_sockaddr saddr;
/* Configure socket filter */
memset(&saddr, 0, sizeof(saddr));
memset(&saddr.mac, 0xFF, 6); /* Broadcast */
// memset(&saddr.mac, 0xFF, 6); /* Broadcast */
saddr.ethertype = htons(0x0806); /* ARP */
arp_socket = ptpd_netif_create_socket(&__static_arp_socket, &saddr,
PTPD_SOCK_RAW_ETHERNET, 0);
PTPD_SOCK_RAW_ETHERNET, 0);
}
static int process_arp(uint8_t * buf, int len)
......@@ -55,35 +46,48 @@ static int process_arp(uint8_t * buf, int len)
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))
if (buf[ARP_OPER + 0] != 0)
return 0;
memcpy(hisMAC, buf + ARP_SHA, 6);
memcpy(hisIP, buf + ARP_SPA, 4);
// ------------- ARP ------------
// HW ethernet
buf[ARP_HTYPE + 0] = 0;
buf[ARP_HTYPE + 1] = 1;
// proto IP
buf[ARP_PTYPE + 0] = 8;
buf[ARP_PTYPE + 1] = 0;
// lengths
buf[ARP_HLEN] = 6;
buf[ARP_PLEN] = 4;
// Response
buf[ARP_OPER + 0] = 0;
buf[ARP_OPER + 1] = 2;
// my MAC+IP
get_mac_addr(buf + ARP_SHA);
memcpy(buf + ARP_SPA, myIP, 4);
// his MAC+IP
memcpy(buf + ARP_THA, hisMAC, 6);
memcpy(buf + ARP_TPA, hisIP, 4);
getIP(myIP);
if ( ((buf[ARP_OPER + 1] != 1)||memcmp(buf + ARP_TPA, myIP, 4)) == 0 )
{
memcpy(hisMAC, buf + ARP_SHA, 6);
memcpy(hisIP, buf + ARP_SPA, 4);
// ------------- ARP ------------
// HW ethernet
buf[ARP_HTYPE + 0] = 0;
buf[ARP_HTYPE + 1] = 1;
// proto IP
buf[ARP_PTYPE + 0] = 8;
buf[ARP_PTYPE + 1] = 0;
// lengths
buf[ARP_HLEN] = 6;
buf[ARP_PLEN] = 4;
// Response
buf[ARP_OPER + 0] = 0;
buf[ARP_OPER + 1] = 2;
// my MAC+IP
get_mac_addr(buf + ARP_SHA);
memcpy(buf + ARP_SPA, myIP, 4);
// his MAC+IP
memcpy(buf + ARP_THA, hisMAC, 6);
memcpy(buf + ARP_TPA, hisIP, 4);
return ARP_END;
}
tcpip_get_hisIP(hisIP);
if ( ((buf[ARP_OPER + 1] != 2)||memcmp(buf + ARP_SPA, hisIP, 4)) == 0 )
{
memcpy(hisMAC, buf + ARP_SHA, 6);
tcpip_set_hisMAC(hisMAC);
tcpip_get_hisMAC(hisMAC);
tcpip_status = TCPIP_OK;
}
return ARP_END;
return 0;
}
static int arp_poll(void)
......@@ -94,9 +98,14 @@ static int arp_poll(void)
if (ip_status == IP_TRAINING)
return 0; /* can't do ARP w/o an address... */
if ((tcpip_status == TCPIP_ARP) && arp_count < 200)
{
tcpip_arp();
arp_count++;
}
if ((len = ptpd_netif_recvfrom(arp_socket,
&addr, buf, sizeof(buf), 0)) > 0) {
&addr, buf, sizeof(buf), 0)) > 0) {
if ((len = process_arp(buf, len)) > 0)
ptpd_netif_sendto(arp_socket, &addr, buf, len, 0);
return 1;
......@@ -104,6 +113,33 @@ static int arp_poll(void)
return 0;
}
void send_arp(uint8_t * hisIP)
{
uint8_t buf[ARP_END + 100];
struct wr_sockaddr addr;
/* Configure socket filter */
memset(&addr.mac, 0xFF, 6); /* Broadcast */
// ------------- ARP ------------
// HW ethernet
buf[ARP_HTYPE + 0] = 0;
buf[ARP_HTYPE + 1] = 1;
// proto IP
buf[ARP_PTYPE + 0] = 8;
buf[ARP_PTYPE + 1] = 0;
// lengths
buf[ARP_HLEN] = 6;
buf[ARP_PLEN] = 4;
// request
buf[ARP_OPER + 0] = 0;
buf[ARP_OPER + 1] = 1;
get_mac_addr(buf + ARP_SHA);
getIP(buf + ARP_SPA);
memset(buf + ARP_THA, 0x00, 6); /* Broadcast */
memcpy(buf + ARP_TPA, hisIP, 4);
return (ptpd_netif_sendto(arp_socket, &addr, buf, ARP_END+10, 0));
}
DEFINE_WRC_TASK(arp) = {
.name = "arp",
.enable = &link_status,
......
......@@ -88,17 +88,17 @@ static void ipv4_init(void)
/* time (rdate): UDP */
rdate_socket = ptpd_netif_create_socket(&__static_rdate_socket, NULL,
PTPD_SOCK_UDP, 37 /* time */);
PTPD_SOCK_UDP, 37 /* time */);
/* remote update (rmupdate): UDP */
rmupdate_socket = ptpd_netif_create_socket(&__static_rmupdate_socket, NULL,
PTPD_SOCK_UDP, 71 /* remote update */);
PTPD_SOCK_UDP, 71 /* remote update */);
/* ICMP: specify raw (not UDP), with IPV4 ethtype */
memset(&saddr, 0, sizeof(saddr));
saddr.ethertype = htons(0x0800);
icmp_socket = ptpd_netif_create_socket(&__static_icmp_socket, &saddr,
PTPD_SOCK_RAW_ETHERNET, 0);
PTPD_SOCK_RAW_ETHERNET, 0);
syslog_init();
}
......@@ -228,9 +228,9 @@ static int rmupdate_poll(void)
case FLASH_WRITE:
data_addr = (buf[UDP_END+8]<<24)+(buf[UDP_END+9]<<16)+(buf[UDP_END+10]<<8)+buf[UDP_END+11];
data_size = (buf[UDP_END+12]<<24)+(buf[UDP_END+13]<<16)+(buf[UDP_END+14]<<8)+buf[UDP_END+15];
flash_write(data_addr,buf+UDP_END+16,data_size);
memset(buf+UDP_END+12, 0x00000000, 4);
len = UDP_END + 16 + 64 ;
flash_write(data_addr,buf+UDP_END+16,data_size);
memset(buf+UDP_END+12, 0x00000000, 4);
len = UDP_END + 16 + 64 ;
break;
case FLASH_READ:
data_addr = (buf[UDP_END+8]<<24)+(buf[UDP_END+9]<<16)+(buf[UDP_END+10]<<8)+buf[UDP_END+11];
......@@ -252,7 +252,7 @@ static int rmupdate_poll(void)
len = UDP_END + 16 + data_size + 64;
break;
default:
// type error
// type error
memset(buf+UDP_END, 0x00000002, 4);
len = UDP_END + 4 + 64;
}
......@@ -274,7 +274,7 @@ static int ipv4_poll(void)
ret += icmp_poll();
ret += rdate_poll();
ret += rmupdate_poll();
ret += syslog_poll();
......@@ -296,17 +296,26 @@ DEFINE_WRC_TASK(ipv4) = {
void setIP(unsigned char *IP)
{
uint8_t tmp[4];
// volatile unsigned int *eb_ip =
// (unsigned int *)(BASE_ETHERBONE_CFG + EB_IPV4);
unsigned int ip;
// (unsigned int *)(BASE_ETHERBONE_CFG + EB_IPV4);
// unsigned int ip;
// while (*eb_ip != ip)
// *eb_ip = ip;
memcpy(myIP, IP, 4);
ip = (myIP[0] << 24) | (myIP[1] << 16) | (myIP[2] << 8) | (myIP[3]);
// while (*eb_ip != ip)
// *eb_ip = ip;
// tcpip module, default IP
tcpip_ip_addr(IP);
// tcpip module, default gateway
memcpy(tmp, IP, 3);
tmp[3]=0x01;
tcpip_gateway_addr(tmp);
tcpip_set_hisIP(tmp);
tcpip_config(IP);
// tcpip module, default subnet mask
tmp[0]=0xff;tmp[1]=0xff;tmp[2]=0xff;tmp[3]=0x00;
tcpip_subnet_addr(tmp);
bootp_retry = 0;
}
......@@ -337,7 +346,7 @@ int check_magic_udp(unsigned char *buf)
sum += ntohs(buf[IP_PROTOCOL]);
packet_size = (ntohs(buf[UDP_LENGTH])<<8) + ntohs(buf[UDP_LENGTH+1]);
sum += packet_size;
// udp header
// udp header
for (i=IP_END; (i <= UDP_END+packet_size-8); i=i+2)
{
sum += (ntohs(buf[i])<<8);
......
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <wrc.h>
#include <lib/ipv4.h>
#include "tcpip_config.h"
#include "shell.h"
void tcpip_help()
{
pp_printf("help\n");
}
static int cmd_tcpip(const char *args[])
{
unsigned char ip[4];
uint16_t port;
if (!args[0])
tcpip_help();
else if (!strcasecmp(args[0], "tcp") && args[1]) {
port = (uint16_t)args[1];
tcpip_rx_tcp_port(port);
} else if (!strcasecmp(args[0], "udp") && args[1] && args[2]) {
if (!strcasecmp(args[1],"rxport"))
{
port = (uint16_t)args[2];
tcpip_rx_dst_port(port);
} else if (!strcasecmp(args[1], "txport")) {
port = (uint16_t)args[2];
tcpip_tx_dst_port(port);
} else if (!strcasecmp(args[1], "txip")) {
decode_ip(args[2], ip);
tcpip_set_hisIP(ip);
} else if (!strcasecmp(args[1], "txgw")) {
decode_ip(args[2], ip);
tcpip_gateway_addr(ip);
} else if (!strcasecmp(args[1], "txsn")) {
decode_ip(args[2], ip);
tcpip_subnet_addr(ip);
}
} else {
return -EINVAL;
}
return 0;
}
DEFINE_WRC_COMMAND(tcpip) = {
.name = "tcpip",
.exec = cmd_tcpip,
};
......@@ -19,7 +19,7 @@ obj-$(CONFIG_EMBEDDED_NODE) += \
shell/cmd_ptrack.o \
obj-$(CONFIG_IP) += shell/cmd_ip.o
obj-$(CONFIG_IP) += shell/cmd_ip.o shell/cmd_tcpip.o
obj-$(CONFIG_PPSI) += shell/cmd_verbose.o
obj-$(CONFIG_CMD_CONFIG) += shell/cmd_config.o
obj-$(CONFIG_CMD_SLEEP) += shell/cmd_sleep.o
......
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