--- /dev/null
+/* Copyright © 2005-2014 Rich Felker, et al.
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining
+ * a copy of this software and associated documentation files (the
+ * "Software"), to deal in the Software without restriction, including
+ * without limitation the rights to use, copy, modify, merge, publish,
+ * distribute, sublicense, and/or sell copies of the Software, and to
+ * permit persons to whom the Software is furnished to do so, subject to
+ * the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be
+ * included in all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+ * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+ * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
+ * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
+ * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
+ * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
+ * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
+ */
+
+#ifndef __INET_H
+#define __INET_H
+
+#include <stdint.h>
+
+static __inline uint16_t __bswap_16(uint16_t __x)
+{
+ return (__x<<8) | (__x>>8);
+}
+
+static __inline uint32_t __bswap_32(uint32_t __x)
+{
+ return (__x>>24) | ((__x>>8)&0xff00) | ((__x<<8)&0xff0000) | (__x<<24);
+}
+
+static __inline uint64_t __bswap_64(uint64_t __x)
+{
+ return (__bswap_32(__x)+(0ULL<<32)) | __bswap_32(__x>>32);
+}
+
+#define bswap_16(x) __bswap_16(x)
+#define bswap_32(x) __bswap_32(x)
+#define bswap_64(x) __bswap_64(x)
+
+uint16_t htons(uint16_t n);
+uint32_t htonl(uint32_t n);
+uint16_t ntohs(uint16_t n);
+uint32_t ntohl(uint32_t n);
+
+#endif /* __INET_H */
#ifdef CSR_ETHMAC_BASE
#include <stdio.h>
+#include <inet.h>
#include <system.h>
#include <crc.h>
#include <hw/flags.h>
h->destmac[i] = destmac[i];
for(i=0;i<6;i++)
h->srcmac[i] = srcmac[i];
- h->ethertype = ethertype;
+ h->ethertype = htons(ethertype);
}
#define ARP_HWTYPE_ETHERNET 0x0001
txbuffer->raw[txlen+1] = (crc & 0xff00) >> 8;
txbuffer->raw[txlen+2] = (crc & 0xff0000) >> 16;
txbuffer->raw[txlen+3] = (crc & 0xff000000) >> 24;
- txlen += 4;
+ //txlen += 4;
#endif
+ txlen += 4; // FIXME
#ifdef DEBUG_MICROUDP_TX
int j;
const struct arp_frame *rx_arp = &rxbuffer->frame.contents.arp;
struct arp_frame *tx_arp = &txbuffer->frame.contents.arp;
- if(rxlen < ARP_PACKET_LENGTH) return;
- if(rx_arp->hwtype != ARP_HWTYPE_ETHERNET) return;
- if(rx_arp->proto != ARP_PROTO_IP) return;
+ //if(rxlen < ARP_PACKET_LENGTH) return; // FIXME
+ if(ntohs(rx_arp->hwtype) != ARP_HWTYPE_ETHERNET) return;
+ if(ntohs(rx_arp->proto) != ARP_PROTO_IP) return;
if(rx_arp->hwsize != 6) return;
if(rx_arp->protosize != 4) return;
- if(rx_arp->opcode == ARP_OPCODE_REPLY) {
- if(rx_arp->sender_ip == cached_ip) {
+
+ if(ntohs(rx_arp->opcode) == ARP_OPCODE_REPLY) {
+ if(ntohl(rx_arp->sender_ip) == cached_ip) {
int i;
for(i=0;i<6;i++)
cached_mac[i] = rx_arp->sender_mac[i];
}
return;
}
- if(rx_arp->opcode == ARP_OPCODE_REQUEST) {
- if(rx_arp->target_ip == my_ip) {
+ if(ntohs(rx_arp->opcode) == ARP_OPCODE_REQUEST) {
+ if(ntohl(rx_arp->target_ip) == my_ip) {
int i;
fill_eth_header(&txbuffer->frame.eth_header,
my_mac,
ETHERTYPE_ARP);
txlen = ARP_PACKET_LENGTH;
- tx_arp->hwtype = ARP_HWTYPE_ETHERNET;
- tx_arp->proto = ARP_PROTO_IP;
+ tx_arp->hwtype = htons(ARP_HWTYPE_ETHERNET);
+ tx_arp->proto = htons(ARP_PROTO_IP);
tx_arp->hwsize = 6;
tx_arp->protosize = 4;
- tx_arp->opcode = ARP_OPCODE_REPLY;
- tx_arp->sender_ip = my_ip;
+ tx_arp->opcode = htons(ARP_OPCODE_REPLY);
+ tx_arp->sender_ip = htonl(my_ip);
for(i=0;i<6;i++)
tx_arp->sender_mac[i] = my_mac[i];
- tx_arp->target_ip = rx_arp->sender_ip;
+ tx_arp->target_ip = htonl(ntohl(rx_arp->sender_ip));
for(i=0;i<6;i++)
tx_arp->target_mac[i] = rx_arp->sender_mac[i];
send_packet();
int microudp_arp_resolve(unsigned int ip)
{
- struct arp_frame *arp = &txbuffer->frame.contents.arp;
+ struct arp_frame *arp;
int i;
int tries;
int timeout;
my_mac,
ETHERTYPE_ARP);
txlen = ARP_PACKET_LENGTH;
- arp->hwtype = ARP_HWTYPE_ETHERNET;
- arp->proto = ARP_PROTO_IP;
+ arp = &txbuffer->frame.contents.arp;
+ arp->hwtype = htons(ARP_HWTYPE_ETHERNET);
+ arp->proto = htons(ARP_PROTO_IP);
arp->hwsize = 6;
arp->protosize = 4;
- arp->opcode = ARP_OPCODE_REQUEST;
- arp->sender_ip = my_ip;
+ arp->opcode = htons(ARP_OPCODE_REQUEST);
+ arp->sender_ip = htonl(my_ip);
for(i=0;i<6;i++)
arp->sender_mac[i] = my_mac[i];
- arp->target_ip = ip;
+ arp->target_ip = htonl(ip);
for(i=0;i<6;i++)
arp->target_mac[i] = 0;
+
send_packet();
/* Do we get a reply ? */
txbuffer->frame.contents.udp.ip.version = IP_IPV4;
txbuffer->frame.contents.udp.ip.diff_services = 0;
- txbuffer->frame.contents.udp.ip.total_length = length + sizeof(struct udp_frame);
- txbuffer->frame.contents.udp.ip.identification = 0;
- txbuffer->frame.contents.udp.ip.fragment_offset = IP_DONT_FRAGMENT;
+ txbuffer->frame.contents.udp.ip.total_length = htons(length + sizeof(struct udp_frame));
+ txbuffer->frame.contents.udp.ip.identification = htons(0);
+ txbuffer->frame.contents.udp.ip.fragment_offset = htons(IP_DONT_FRAGMENT);
txbuffer->frame.contents.udp.ip.ttl = IP_TTL;
h.proto = txbuffer->frame.contents.udp.ip.proto = IP_PROTO_UDP;
txbuffer->frame.contents.udp.ip.checksum = 0;
- h.src_ip = txbuffer->frame.contents.udp.ip.src_ip = my_ip;
- h.dst_ip = txbuffer->frame.contents.udp.ip.dst_ip = cached_ip;
- txbuffer->frame.contents.udp.ip.checksum = ip_checksum(0, &txbuffer->frame.contents.udp.ip,
- sizeof(struct ip_header), 1);
-
- txbuffer->frame.contents.udp.udp.src_port = src_port;
- txbuffer->frame.contents.udp.udp.dst_port = dst_port;
- h.length = txbuffer->frame.contents.udp.udp.length = length + sizeof(struct udp_header);
+ h.src_ip = txbuffer->frame.contents.udp.ip.src_ip = htonl(my_ip);
+ h.dst_ip = txbuffer->frame.contents.udp.ip.dst_ip = htonl(cached_ip);
+ txbuffer->frame.contents.udp.ip.checksum = htons(ip_checksum(0, &txbuffer->frame.contents.udp.ip,
+ sizeof(struct ip_header), 1));
+
+ txbuffer->frame.contents.udp.udp.src_port = htons(src_port);
+ txbuffer->frame.contents.udp.udp.dst_port = htons(dst_port);
+ h.length = txbuffer->frame.contents.udp.udp.length = htons(length + sizeof(struct udp_header));
txbuffer->frame.contents.udp.udp.checksum = 0;
h.zero = 0;
}
r = ip_checksum(r, &txbuffer->frame.contents.udp.udp,
sizeof(struct udp_header)+length, 1);
- txbuffer->frame.contents.udp.udp.checksum = r;
+ txbuffer->frame.contents.udp.udp.checksum = htons(r);
send_packet();
static void process_ip(void)
{
if(rxlen < (sizeof(struct ethernet_header)+sizeof(struct udp_frame))) return;
+ struct udp_frame *udp_ip = &rxbuffer->frame.contents.udp;
/* We don't verify UDP and IP checksums and rely on the Ethernet checksum solely */
- if(rxbuffer->frame.contents.udp.ip.version != IP_IPV4) return;
+ if(udp_ip->ip.version != IP_IPV4) return;
// check disabled for QEMU compatibility
//if(rxbuffer->frame.contents.udp.ip.diff_services != 0) return;
- if(rxbuffer->frame.contents.udp.ip.total_length < sizeof(struct udp_frame)) return;
+ if(ntohs(udp_ip->ip.total_length) < sizeof(struct udp_frame)) return;
// check disabled for QEMU compatibility
- //if(rxbuffer->frame.contents.udp.ip.fragment_offset != IP_DONT_FRAGMENT) return;
- if(rxbuffer->frame.contents.udp.ip.proto != IP_PROTO_UDP) return;
- if(rxbuffer->frame.contents.udp.ip.dst_ip != my_ip) return;
- if(rxbuffer->frame.contents.udp.udp.length < sizeof(struct udp_header)) return;
+ //if(ntohs(rxbuffer->frame.contents.udp.ip.fragment_offset) != IP_DONT_FRAGMENT) return;
+ if(udp_ip->ip.proto != IP_PROTO_UDP) return;
+ if(ntohl(udp_ip->ip.dst_ip) != my_ip) return;
+ if(ntohs(udp_ip->udp.length) < sizeof(struct udp_header)) return;
if(rx_callback)
- rx_callback(rxbuffer->frame.contents.udp.ip.src_ip, rxbuffer->frame.contents.udp.udp.src_port, rxbuffer->frame.contents.udp.udp.dst_port, rxbuffer->frame.contents.udp.payload, rxbuffer->frame.contents.udp.udp.length-sizeof(struct udp_header));
+ rx_callback(ntohl(udp_ip->ip.src_ip), ntohs(udp_ip->udp.src_port), ntohs(udp_ip->udp.dst_port),
+ udp_ip->payload, ntohs(udp_ip->udp.length)-sizeof(struct udp_header));
}
void microudp_set_callback(udp_callback callback)
rxlen -= 4; /* strip CRC here to be consistent with TX */
#endif
- if(rxbuffer->frame.eth_header.ethertype == ETHERTYPE_ARP) process_arp();
- else if(rxbuffer->frame.eth_header.ethertype == ETHERTYPE_IP) process_ip();
+ if(ntohs(rxbuffer->frame.eth_header.ethertype) == ETHERTYPE_ARP) process_arp();
+ else if(ntohs(rxbuffer->frame.eth_header.ethertype) == ETHERTYPE_IP) process_ip();
}
void microudp_start(const unsigned char *macaddr, unsigned int ip)