summaryrefslogtreecommitdiff
path: root/net/net.c
diff options
context:
space:
mode:
authorJoe Hershberger <joe.hershberger@ni.com>2012-05-23 07:59:07 +0000
committerJoe Hershberger <joe.hershberger@ni.com>2012-05-23 17:46:18 -0500
commit4b11c9166b86ccc5f8f02fda01ded0f9e9760df0 (patch)
treecdf2b9de655df32a3937ea6a22395d67b21c469a /net/net.c
parent674bb249825aa9b0bddab046d23d43c33bb75f78 (diff)
downloadu-boot-imx-4b11c9166b86ccc5f8f02fda01ded0f9e9760df0.zip
u-boot-imx-4b11c9166b86ccc5f8f02fda01ded0f9e9760df0.tar.gz
u-boot-imx-4b11c9166b86ccc5f8f02fda01ded0f9e9760df0.tar.bz2
net: Refactor IP, UPD, and ICMP header writing functions
ICMP (ping) was reimplementing IP header code... it now shares code. Signed-off-by: Joe Hershberger <joe.hershberger@ni.com> Acked-by: Simon Glass <sjg@chromium.org> Acked-by: Mike Frysinger <vapier@gentoo.org>
Diffstat (limited to 'net/net.c')
-rw-r--r--net/net.c47
1 files changed, 28 insertions, 19 deletions
diff --git a/net/net.c b/net/net.c
index de5352c..1c7bf60 100644
--- a/net/net.c
+++ b/net/net.c
@@ -618,7 +618,7 @@ int NetSendUDPPacket(uchar *ether, IPaddr_t dest, int dport, int sport,
pkt = NetArpWaitTxPacket;
pkt += NetSetEther(pkt, NetArpWaitPacketMAC, PROT_IP);
- NetSetIP(pkt, dest, dport, sport, payload_len);
+ net_set_udp_header(pkt, dest, dport, sport, payload_len);
memcpy(pkt + IP_UDP_HDR_SIZE, (uchar *)NetTxPacket +
(pkt - (uchar *)NetArpWaitTxPacket) +
IP_UDP_HDR_SIZE, payload_len);
@@ -638,7 +638,7 @@ int NetSendUDPPacket(uchar *ether, IPaddr_t dest, int dport, int sport,
pkt = (uchar *)NetTxPacket;
pkt += NetSetEther(pkt, ether, PROT_IP);
- NetSetIP(pkt, dest, dport, sport, payload_len);
+ net_set_udp_header(pkt, dest, dport, sport, payload_len);
eth_send(NetTxPacket, (pkt - NetTxPacket) + IP_UDP_HDR_SIZE +
payload_len);
@@ -1245,40 +1245,49 @@ NetSetEther(uchar *xet, uchar * addr, uint prot)
}
}
-void NetSetIP(uchar *xip, IPaddr_t dest, int dport, int sport, int len)
+void net_set_ip_header(uchar *pkt, IPaddr_t dest, IPaddr_t source)
{
- struct ip_udp_hdr *ip = (struct ip_udp_hdr *)xip;
+ 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)
- xip[IP_UDP_HDR_SIZE + len] = 0;
-
- /*
- * Construct an IP and UDP header.
- * (need to set no fragment bit - XXX)
+ * Construct an IP header.
*/
/* IP_HDR_SIZE / 4 (not including UDP) */
ip->ip_hl_v = 0x45;
ip->ip_tos = 0;
- ip->ip_len = htons(IP_UDP_HDR_SIZE + len);
+ ip->ip_len = htons(IP_HDR_SIZE);
ip->ip_id = htons(NetIPID++);
ip->ip_off = htons(IP_FLAGS_DFRAG); /* Don't fragment */
ip->ip_ttl = 255;
- ip->ip_p = 17; /* UDP */
ip->ip_sum = 0;
/* already in network byte order */
- NetCopyIP((void *)&ip->ip_src, &NetOurIP);
- /* - "" - */
+ NetCopyIP((void *)&ip->ip_src, &source);
+ /* already in network byte order */
NetCopyIP((void *)&ip->ip_dst, &dest);
+}
+
+void net_set_udp_header(uchar *pkt, IPaddr_t 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, NetOurIP);
+ ip->ip_len = htons(IP_UDP_HDR_SIZE + len);
+ ip->ip_p = IPPROTO_UDP;
+ ip->ip_sum = ~NetCksum((uchar *)ip, IP_HDR_SIZE >> 1);
+
ip->udp_src = htons(sport);
ip->udp_dst = htons(dport);
ip->udp_len = htons(UDP_HDR_SIZE + len);
ip->udp_xsum = 0;
- ip->ip_sum = ~NetCksum((uchar *)ip, IP_HDR_SIZE / 2);
}
void copy_filename(char *dst, const char *src, int size)