Skip to content
Snippets Groups Projects
net.c 33.6 KiB
Newer Older
  • Learn to ignore specific revisions
  • 	if (eth_proto < 1514) {
    
    		struct e802_hdr *et802 = (struct e802_hdr *)et;
    
    Wolfgang Denk's avatar
    Wolfgang Denk committed
    		/*
    
    		 *	Got a 802.2 packet.  Check the other protocol field.
    		 *	XXX VLAN over 802.2+SNAP not implemented!
    
    Wolfgang Denk's avatar
    Wolfgang Denk committed
    		 */
    
    		eth_proto = ntohs(et802->et_prot);
    
    		ip = (struct ip_udp_hdr *)(in_packet + E802_HDR_SIZE);
    
    Wolfgang Denk's avatar
    Wolfgang Denk committed
    		len -= E802_HDR_SIZE;
    
    	} else if (eth_proto != PROT_VLAN) {	/* normal packet */
    
    		ip = (struct ip_udp_hdr *)(in_packet + ETHER_HDR_SIZE);
    
    Wolfgang Denk's avatar
    Wolfgang Denk committed
    		len -= 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);
    
    		len -= VLAN_ETHER_HDR_SIZE;
    
    	debug_cond(DEBUG_NET_PKT, "Receive from protocol 0x%x\n", eth_proto);
    
    Wolfgang Denk's avatar
    Wolfgang Denk committed
    
    
    #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;
    	}
    
    
    	switch (eth_proto) {
    
    Wolfgang Denk's avatar
    Wolfgang Denk committed
    
    	case PROT_ARP:
    
    		arp_receive(et, ip, len);
    
    Wolfgang Denk's avatar
    Wolfgang Denk committed
    		break;
    
    Wolfgang Denk's avatar
    Wolfgang Denk committed
    
    
    #ifdef CONFIG_CMD_RARP
    
    Wolfgang Denk's avatar
    Wolfgang Denk committed
    	case PROT_RARP:
    
    		rarp_receive(ip, len);
    
    Wolfgang Denk's avatar
    Wolfgang Denk committed
    		break;
    
    Wolfgang Denk's avatar
    Wolfgang Denk committed
    	case PROT_IP:
    
    		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);
    
    Wolfgang Denk's avatar
    Wolfgang Denk committed
    			return;
    		}
    
    		/* Check the packet length */
    
    Wolfgang Denk's avatar
    Wolfgang Denk committed
    		if (len < ntohs(ip->ip_len)) {
    
    			debug("len bad %d < %d\n", len, ntohs(ip->ip_len));
    
    Wolfgang Denk's avatar
    Wolfgang Denk committed
    			return;
    		}
    		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)
    
    Wolfgang Denk's avatar
    Wolfgang Denk committed
    			return;
    
    		/* Can't deal with IP options (headers != 20 bytes) */
    
    		if ((ip->ip_hl_v & 0x0f) > 0x05)
    
    		/* Check the Checksum of the header */
    
    		if (!ip_checksum_ok((uchar *)ip, IP_HDR_SIZE)) {
    
    Wolfgang Denk's avatar
    Wolfgang Denk committed
    			return;
    		}
    
    		/* If it is not for us, ignore it */
    
    		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) {
    
    David Updegraff's avatar
    David Updegraff committed
    #ifdef CONFIG_MCAST_TFTP
    
    			if (net_mcast_addr != dst_ip)
    
    David Updegraff's avatar
    David Updegraff committed
    #endif
    
    Wolfgang Denk's avatar
    Wolfgang Denk committed
    		}
    
    		/* 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)
    
    			return;
    
    Wolfgang Denk's avatar
    Wolfgang Denk committed
    		/*
    		 * watch for ICMP host redirects
    		 *
    
    Wolfgang Denk's avatar
    Wolfgang Denk committed
    		 * 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 :-)
    
    Wolfgang Denk's avatar
    Wolfgang Denk committed
    		 *
    
    Wolfgang Denk's avatar
    Wolfgang Denk committed
    		 * 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.
    
    Wolfgang Denk's avatar
    Wolfgang Denk committed
    		 */
    		if (ip->ip_p == IPPROTO_ICMP) {
    
    			receive_icmp(ip, len, src_ip, et);
    			return;
    
    Wolfgang Denk's avatar
    Wolfgang Denk committed
    		} 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) {
    
    Wolfgang Denk's avatar
    Wolfgang Denk committed
    			ulong   xsum;
    
    			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) {
    
    Wolfgang Denk's avatar
    Wolfgang Denk committed
    				ushort sumdata;
    
    
    				sumdata = *sumptr++;
    				xsum += ntohs(sumdata);
    				sumlen -= 2;
    			}
    			if (sumlen > 0) {
    
    Wolfgang Denk's avatar
    Wolfgang Denk committed
    				ushort sumdata;
    
    Wolfgang Denk's avatar
    Wolfgang Denk committed
    				sumdata = (sumdata << 8) & 0xff00;
    
    				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));
    
    #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);
    
    Wolfgang Denk's avatar
    Wolfgang Denk committed
    		/*
    		 *	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);
    
    Wolfgang Denk's avatar
    Wolfgang Denk committed
    		break;
    	}
    }
    
    
    /**********************************************************************/
    
    
    static int net_check_prereq(enum proto_t protocol)
    
    Wolfgang Denk's avatar
    Wolfgang Denk committed
    {
    	switch (protocol) {
    
    Wolfgang Denk's avatar
    Wolfgang Denk committed
    		/* Fall through */
    
    #if defined(CONFIG_CMD_PING)
    
    	case PING:
    
    		if (net_ping_ip.s_addr == 0) {
    
    			puts("*** ERROR: ping address not given\n");
    
    Wolfgang Denk's avatar
    Wolfgang Denk committed
    		}
    		goto common;
    
    #if defined(CONFIG_CMD_SNTP)
    
    		if (net_ntp_server.s_addr == 0) {
    
    			puts("*** ERROR: NTP server address not given\n");
    
    Robin Getz's avatar
    Robin Getz committed
    #if defined(CONFIG_CMD_DNS)
    	case DNS:
    
    		if (net_dns_server.s_addr == 0) {
    
    Robin Getz's avatar
    Robin Getz committed
    			puts("*** ERROR: DNS server address not given\n");
    			return 1;
    		}
    		goto common;
    #endif
    
    #if defined(CONFIG_CMD_NFS)
    
    #endif
    
    	case TFTPGET:
    
    	case TFTPPUT:
    
    		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:
    
    #endif
    
    		/* Fall through */
    
    	case NETCONS:
    
    Luca Ceresoli's avatar
    Luca Ceresoli committed
    	case TFTPSRV:
    
    		if (net_ip.s_addr == 0) {
    
    			puts("*** ERROR: `ipaddr' not set\n");
    
    Wolfgang Denk's avatar
    Wolfgang Denk committed
    		}
    		/* Fall through */
    
    Wolfgang Denk's avatar
    Wolfgang Denk committed
    
    
    #ifdef CONFIG_CMD_RARP
    
    Wolfgang Denk's avatar
    Wolfgang Denk committed
    	case RARP:
    
    Wolfgang Denk's avatar
    Wolfgang Denk committed
    	case BOOTP:
    
    	case LINKLOCAL:
    
    		if (memcmp(net_ethaddr, "\0\0\0\0\0\0", 6) == 0) {
    
    			int num = eth_get_dev_index();
    
    Wolfgang Denk's avatar
    Wolfgang Denk committed
    
    
    Wolfgang Denk's avatar
    Wolfgang Denk committed
    			switch (num) {
    			case -1:
    
    				puts("*** ERROR: No ethernet found.\n");
    
    Wolfgang Denk's avatar
    Wolfgang Denk committed
    			case 0:
    
    				puts("*** ERROR: `ethaddr' not set\n");
    
    Wolfgang Denk's avatar
    Wolfgang Denk committed
    				break;
    
    Wolfgang Denk's avatar
    Wolfgang Denk committed
    			default:
    
    				printf("*** ERROR: `eth%daddr' not set\n",
    
    Wolfgang Denk's avatar
    Wolfgang Denk committed
    					num);
    				break;
    
    Wolfgang Denk's avatar
    Wolfgang Denk committed
    
    
    			NetStartAgain();
    
    Wolfgang Denk's avatar
    Wolfgang Denk committed
    		}
    		/* Fall through */
    	default:
    
    Wolfgang Denk's avatar
    Wolfgang Denk committed
    	}
    
    Wolfgang Denk's avatar
    Wolfgang Denk committed
    }
    /**********************************************************************/
    
    
    Wolfgang Denk's avatar
    Wolfgang Denk committed
    
    
    	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)
    
    Wolfgang Denk's avatar
    Wolfgang Denk committed
    {
    
    	struct ethernet_hdr *et = (struct ethernet_hdr *)xet;
    
    	ushort myvlanid;
    
    	myvlanid = ntohs(NetOurVLAN);
    	if (myvlanid == (ushort)-1)
    		myvlanid = VLAN_NONE;
    
    Wolfgang Denk's avatar
    Wolfgang Denk committed
    
    
    	memcpy(et->et_dest, dest_ethaddr, 6);
    	memcpy(et->et_src, net_ethaddr, 6);
    
    	if ((myvlanid & VLAN_IDMASK) == VLAN_NONE) {
    
    		et->et_protlen = htons(prot);
    
    		return ETHER_HDR_SIZE;
    	} else {
    
    		struct vlan_ethernet_hdr *vet =
    			(struct vlan_ethernet_hdr *)xet;
    
    Wolfgang Denk's avatar
    Wolfgang Denk committed
    
    
    		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;
    	}
    }
    
    Wolfgang Denk's avatar
    Wolfgang Denk committed
    
    
    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)
    
    Wolfgang Denk's avatar
    Wolfgang Denk committed
    {
    
    	struct ip_udp_hdr *ip = (struct ip_udp_hdr *)pkt;
    
    Wolfgang Denk's avatar
    Wolfgang Denk committed
    	 */
    
    	/* IP_HDR_SIZE / 4 (not including UDP) */
    	ip->ip_hl_v  = 0x45;
    
    Wolfgang Denk's avatar
    Wolfgang Denk committed
    	ip->ip_tos   = 0;
    
    	ip->ip_len   = htons(IP_HDR_SIZE);
    
    Wolfgang Denk's avatar
    Wolfgang Denk committed
    	ip->ip_id    = htons(NetIPID++);
    
    	ip->ip_off   = htons(IP_FLAGS_DFRAG);	/* Don't fragment */
    
    Wolfgang Denk's avatar
    Wolfgang Denk committed
    	ip->ip_ttl   = 255;
    	ip->ip_sum   = 0;
    
    	/* 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);
    
    Wolfgang Denk's avatar
    Wolfgang Denk committed
    	ip->udp_src  = htons(sport);
    	ip->udp_dst  = htons(dport);
    
    	ip->udp_len  = htons(UDP_HDR_SIZE + len);
    
    Wolfgang Denk's avatar
    Wolfgang Denk committed
    	ip->udp_xsum = 0;
    }
    
    
    void copy_filename(char *dst, const char *src, int size)
    
    Wolfgang Denk's avatar
    Wolfgang Denk committed
    {
    	if (*src && (*src == '"')) {
    		++src;
    		--size;
    	}
    
    
    	while ((--size > 0) && *src && (*src != '"'))
    
    Wolfgang Denk's avatar
    Wolfgang Denk committed
    		*dst++ = *src++;
    	*dst = '\0';
    }
    
    
    #if	defined(CONFIG_CMD_NFS)		|| \
    	defined(CONFIG_CMD_SNTP)	|| \
    	defined(CONFIG_CMD_DNS)
    
    Robin Getz's avatar
    Robin Getz committed
    /*
    
     * 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
    
    Robin Getz's avatar
    Robin Getz committed
     */
    unsigned int random_port(void)
    {
    
    	return 1024 + (get_timer(0) % 0x4000);
    
    Robin Getz's avatar
    Robin Getz committed
    }
    #endif
    
    
    void ip_to_string(struct in_addr x, char *s)
    
    Wolfgang Denk's avatar
    Wolfgang Denk committed
    {
    
    	x.s_addr = ntohl(x.s_addr);
    
    	sprintf(s, "%d.%d.%d.%d",
    
    		(int) ((x.s_addr >> 24) & 0xff),
    		(int) ((x.s_addr >> 16) & 0xff),
    		(int) ((x.s_addr >> 8) & 0xff),
    		(int) ((x.s_addr >> 0) & 0xff)
    
    Wolfgang Denk's avatar
    Wolfgang Denk committed
    	);
    
    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)
    
    		return htons(VLAN_NONE);
    
    
    	if (*s < '0' || *s > '9')
    		id = VLAN_NONE;
    	else
    		id = (ushort)simple_strtoul(s, NULL, 10);
    
    
    	return htons(id);
    
    }
    
    ushort getenv_VLAN(char *var)
    {
    
    	return string_to_VLAN(getenv(var));