Newer
Older
struct e802_hdr *et802 = (struct e802_hdr *)et;
* Got a 802.2 packet. Check the other protocol field.
* XXX VLAN over 802.2+SNAP not implemented!
eth_proto = ntohs(et802->et_prot);
ip = (struct ip_udp_hdr *)(in_packet + E802_HDR_SIZE);
} else if (eth_proto != PROT_VLAN) { /* normal packet */
ip = (struct ip_udp_hdr *)(in_packet + ETHER_HDR_SIZE);
} else { /* VLAN packet */
struct vlan_ethernet_hdr *vet =
(struct vlan_ethernet_hdr *)et;
debug_cond(DEBUG_NET_PKT, "VLAN packet received\n");
/* too small packet? */
if (len < VLAN_ETHER_HDR_SIZE)
return;
/* if no VLAN active */
if ((ntohs(NetOurVLAN) & VLAN_IDMASK) == VLAN_NONE
#if defined(CONFIG_CMD_CDP)
&& iscdp == 0
#endif
)
return;
cti = ntohs(vet->vet_tag);
vlanid = cti & VLAN_IDMASK;
eth_proto = ntohs(vet->vet_type);
ip = (struct ip_udp_hdr *)(in_packet + VLAN_ETHER_HDR_SIZE);
debug_cond(DEBUG_NET_PKT, "Receive from protocol 0x%x\n", eth_proto);
#if defined(CONFIG_CMD_CDP)
cdp_receive((uchar *)ip, len);
return;
}
#endif
if ((myvlanid & VLAN_IDMASK) != VLAN_NONE) {
if (vlanid == VLAN_NONE)
vlanid = (mynvlanid & VLAN_IDMASK);
/* not matched? */
if (vlanid != (myvlanid & VLAN_IDMASK))
return;
}
arp_receive(et, ip, len);
debug_cond(DEBUG_NET_PKT, "Got IP\n");
/* Before we start poking the header, make sure it is there */
if (len < IP_UDP_HDR_SIZE) {
debug("len bad %d < %lu\n", len,
(ulong)IP_UDP_HDR_SIZE);
debug("len bad %d < %d\n", len, ntohs(ip->ip_len));
debug_cond(DEBUG_NET_PKT, "len=%d, v=%02x\n",
len, ip->ip_hl_v & 0xff);
/* Can't deal with anything except IPv4 */
if ((ip->ip_hl_v & 0xf0) != 0x40)
/* Can't deal with IP options (headers != 20 bytes) */
if (!ip_checksum_ok((uchar *)ip, IP_HDR_SIZE)) {
debug("checksum bad\n");
dst_ip = net_read_ip(&ip->ip_dst);
if (net_ip.s_addr && dst_ip.s_addr != net_ip.s_addr &&
dst_ip.s_addr != 0xFFFFFFFF) {
if (net_mcast_addr != dst_ip)
/* Read source IP address for later use */
src_ip = net_read_ip(&ip->ip_src);
/*
* The function returns the unchanged packet if it's not
* a fragment, and either the complete packet or NULL if
* it is a fragment (if !CONFIG_IP_DEFRAG, it returns NULL)
*/
ip = NetDefragment(ip, &len);
if (!ip)
* There is no real handler code (yet). We just watch
* for ICMP host redirect messages. In case anybody
* sees these messages: please contact me
* (wd@denx.de), or - even better - send me the
* necessary fixes :-)
* Note: in all cases where I have seen this so far
* it was a problem with the router configuration,
* for instance when a router was configured in the
* BOOTP reply, but the TFTP server was on the same
* subnet. So this is probably a warning that your
* configuration might be wrong. But I'm not really
* sure if there aren't any other situations.
*
* Simon Glass <sjg@chromium.org>: We get an ICMP when
* we send a tftp packet to a dead connection, or when
* there is no server at the other end.
receive_icmp(ip, len, src_ip, et);
return;
} else if (ip->ip_p != IPPROTO_UDP) { /* Only UDP packets */
return;
}
debug_cond(DEBUG_DEV_PKT,
"received UDP (to=%pI4, from=%pI4, len=%d)\n",
&dst_ip, &src_ip, len);
#ifdef CONFIG_UDP_CHECKSUM
if (ip->udp_xsum != 0) {
ushort *sumptr;
ushort sumlen;
xsum = ip->ip_p;
xsum += (ntohs(ip->udp_len));
xsum += (ntohl(ip->ip_src.s_addr) >> 16) & 0x0000ffff;
xsum += (ntohl(ip->ip_src.s_addr) >> 0) & 0x0000ffff;
xsum += (ntohl(ip->ip_dst.s_addr) >> 16) & 0x0000ffff;
xsum += (ntohl(ip->ip_dst.s_addr) >> 0) & 0x0000ffff;
sumlen = ntohs(ip->udp_len);
sumptr = (ushort *) &(ip->udp_src);
while (sumlen > 1) {
sumdata = *sumptr++;
xsum += ntohs(sumdata);
sumlen -= 2;
}
if (sumlen > 0) {
sumdata = *(unsigned char *) sumptr;
xsum += sumdata;
}
while ((xsum >> 16) != 0) {
xsum = (xsum & 0x0000ffff) +
((xsum >> 16) & 0x0000ffff);
}
if ((xsum != 0x00000000) && (xsum != 0x0000ffff)) {
printf(" UDP wrong checksum %08lx %08x\n",
xsum, ntohs(ip->udp_xsum));
return;
}
}
#endif
#if defined (CONFIG_NETCONSOLE) && !(CONFIG_SPL_BUILD)
nc_input_packet((uchar *)ip + IP_UDP_HDR_SIZE,
ntohs(ip->udp_dst),
ntohs(ip->udp_src),
ntohs(ip->udp_len) - UDP_HDR_SIZE);
/*
* IP header OK. Pass the packet to the current handler.
*/
(*udp_packet_handler)((uchar *)ip + IP_UDP_HDR_SIZE,
ntohs(ip->udp_dst),
src_ip,
ntohs(ip->udp_src),
ntohs(ip->udp_len) - UDP_HDR_SIZE);
break;
}
}
/**********************************************************************/
static int net_check_prereq(enum proto_t protocol)
#if defined(CONFIG_CMD_PING)
if (net_ping_ip.s_addr == 0) {
puts("*** ERROR: ping address not given\n");
#if defined(CONFIG_CMD_SNTP)
if (net_ntp_server.s_addr == 0) {
puts("*** ERROR: NTP server address not given\n");
if (net_dns_server.s_addr == 0) {
puts("*** ERROR: DNS server address not given\n");
return 1;
}
goto common;
#endif
#if defined(CONFIG_CMD_NFS)
if (net_server_ip.s_addr == 0) {
puts("*** ERROR: `serverip' not set\n");
#if defined(CONFIG_CMD_PING) || defined(CONFIG_CMD_SNTP) || \
defined(CONFIG_CMD_DNS)
common:
if (net_ip.s_addr == 0) {
puts("*** ERROR: `ipaddr' not set\n");
if (memcmp(net_ethaddr, "\0\0\0\0\0\0", 6) == 0) {
int num = eth_get_dev_index();
puts("*** ERROR: No ethernet found.\n");
puts("*** ERROR: `ethaddr' not set\n");
printf("*** ERROR: `eth%daddr' not set\n",
return 0; /* OK */
}
/**********************************************************************/
net_eth_hdr_size(void)
myvlanid = ntohs(NetOurVLAN);
if (myvlanid == (ushort)-1)
myvlanid = VLAN_NONE;
return ((myvlanid & VLAN_IDMASK) == VLAN_NONE) ? ETHER_HDR_SIZE :
VLAN_ETHER_HDR_SIZE;
int net_set_ether(uchar *xet, const uchar *dest_ethaddr, uint prot)
struct ethernet_hdr *et = (struct ethernet_hdr *)xet;
ushort myvlanid;
myvlanid = ntohs(NetOurVLAN);
if (myvlanid == (ushort)-1)
myvlanid = VLAN_NONE;
memcpy(et->et_dest, dest_ethaddr, 6);
memcpy(et->et_src, net_ethaddr, 6);
if ((myvlanid & VLAN_IDMASK) == VLAN_NONE) {
return ETHER_HDR_SIZE;
} else {
struct vlan_ethernet_hdr *vet =
(struct vlan_ethernet_hdr *)xet;
vet->vet_vlan_type = htons(PROT_VLAN);
vet->vet_tag = htons((0 << 5) | (myvlanid & VLAN_IDMASK));
vet->vet_type = htons(prot);
return VLAN_ETHER_HDR_SIZE;
}
}
int net_update_ether(struct ethernet_hdr *et, uchar *addr, uint prot)
{
ushort protlen;
memcpy(et->et_dest, addr, 6);
memcpy(et->et_src, net_ethaddr, 6);
protlen = ntohs(et->et_protlen);
if (protlen == PROT_VLAN) {
struct vlan_ethernet_hdr *vet =
(struct vlan_ethernet_hdr *)et;
vet->vet_type = htons(prot);
return VLAN_ETHER_HDR_SIZE;
} else if (protlen > 1514) {
et->et_protlen = htons(prot);
return ETHER_HDR_SIZE;
} else {
/* 802.2 + SNAP */
struct e802_hdr *et802 = (struct e802_hdr *)et;
et802->et_prot = htons(prot);
return E802_HDR_SIZE;
}
}
void net_set_ip_header(uchar *pkt, struct in_addr dest, struct in_addr source)
struct ip_udp_hdr *ip = (struct ip_udp_hdr *)pkt;
* Construct an IP header.
/* IP_HDR_SIZE / 4 (not including UDP) */
ip->ip_hl_v = 0x45;
ip->ip_len = htons(IP_HDR_SIZE);
ip->ip_off = htons(IP_FLAGS_DFRAG); /* Don't fragment */
/* already in network byte order */
net_copy_ip((void *)&ip->ip_src, &source);
/* already in network byte order */
net_copy_ip((void *)&ip->ip_dst, &dest);
void net_set_udp_header(uchar *pkt, struct in_addr dest, int dport, int sport,
int len)
{
struct ip_udp_hdr *ip = (struct ip_udp_hdr *)pkt;
/*
* If the data is an odd number of bytes, zero the
* byte after the last byte so that the checksum
* will work.
*/
if (len & 1)
pkt[IP_UDP_HDR_SIZE + len] = 0;
net_set_ip_header(pkt, dest, net_ip);
ip->ip_len = htons(IP_UDP_HDR_SIZE + len);
ip->ip_p = IPPROTO_UDP;
ip->ip_sum = compute_ip_checksum(ip, IP_HDR_SIZE);
ip->udp_src = htons(sport);
ip->udp_dst = htons(dport);
ip->udp_len = htons(UDP_HDR_SIZE + len);
void copy_filename(char *dst, const char *src, int size)
{
if (*src && (*src == '"')) {
++src;
--size;
}
while ((--size > 0) && *src && (*src != '"'))
#if defined(CONFIG_CMD_NFS) || \
defined(CONFIG_CMD_SNTP) || \
defined(CONFIG_CMD_DNS)
* make port a little random (1024-17407)
* This keeps the math somewhat trivial to compute, and seems to work with
* all supported protocols/clients/servers
return 1024 + (get_timer(0) % 0x4000);
void ip_to_string(struct in_addr x, char *s)
x.s_addr = ntohl(x.s_addr);
(int) ((x.s_addr >> 24) & 0xff),
(int) ((x.s_addr >> 16) & 0xff),
(int) ((x.s_addr >> 8) & 0xff),
(int) ((x.s_addr >> 0) & 0xff)
void VLAN_to_string(ushort x, char *s)
{
x = ntohs(x);
if (x == (ushort)-1)
x = VLAN_NONE;
if (x == VLAN_NONE)
strcpy(s, "none");
else
sprintf(s, "%d", x & VLAN_IDMASK);
}
ushort string_to_VLAN(const char *s)
{
ushort id;
if (s == NULL)
if (*s < '0' || *s > '9')
id = VLAN_NONE;
else
id = (ushort)simple_strtoul(s, NULL, 10);
}
ushort getenv_VLAN(char *var)
{
return string_to_VLAN(getenv(var));