1 /* 2 * Copyright 2005, Ingo Weinhold <bonefish@cs.tu-berlin.de>. 3 * All rights reserved. Distributed under the terms of the MIT License. 4 */ 5 6 #ifndef _BOOT_NET_DEFS_H 7 #define _BOOT_NET_DEFS_H 8 9 #include <string.h> 10 11 #include <ByteOrder.h> 12 #include <SupportDefs.h> 13 14 #include <util/kernel_cpp.h> 15 16 // Ethernet 17 18 #define ETH_ALEN 6 19 #define ETHERTYPE_IP 0x0800 // IP 20 #define ETHERTYPE_ARP 0x0806 // Address resolution 21 22 #define ETHER_MIN_TRANSFER_UNIT 46 23 #define ETHER_MAX_TRANSFER_UNIT 1500 24 25 struct mac_addr_t { 26 mac_addr_t() {} 27 28 mac_addr_t(uint8 *address) 29 { 30 memcpy(this->address, address, ETH_ALEN); 31 } 32 33 mac_addr_t(const mac_addr_t& other) 34 { 35 memcpy(address, other.address, sizeof(address)); 36 } 37 38 uint64 ToUInt64() const 39 { 40 return ((uint64)address[0] << 40) 41 | ((uint64)address[1] << 32) 42 | ((uint64)address[2] << 24) 43 | ((uint64)address[3] << 16) 44 | ((uint64)address[4] << 8) 45 | (uint64)address[5]; 46 } 47 48 mac_addr_t& operator=(const mac_addr_t& other) 49 { 50 memcpy(address, other.address, sizeof(address)); 51 return *this; 52 } 53 54 bool operator==(const mac_addr_t& other) const 55 { 56 return memcmp(address, other.address, sizeof(address)) == 0; 57 } 58 59 bool operator!=(const mac_addr_t& other) const 60 { 61 return !(*this == other); 62 } 63 64 uint8 address[ETH_ALEN]; 65 } __attribute__ ((__packed__)); 66 67 extern const mac_addr_t kBroadcastMACAddress; 68 extern const mac_addr_t kNoMACAddress; 69 70 // 10/100 Mb/s ethernet header 71 struct ether_header { 72 mac_addr_t destination; /* destination eth addr */ 73 mac_addr_t source; /* source ether addr */ 74 uint16 type; /* packet type ID field */ 75 } __attribute__ ((__packed__)); 76 77 78 // #pragma mark - 79 80 // Address Resolution Protocol (ARP) 81 82 typedef uint32 ip_addr_t; 83 84 // ARP protocol opcodes 85 #define ARPOP_REQUEST 1 /* ARP request. */ 86 #define ARPOP_REPLY 2 /* ARP reply. */ 87 #define ARPOP_RREQUEST 3 /* RARP request. */ 88 #define ARPOP_RREPLY 4 /* RARP reply. */ 89 #define ARPOP_InREQUEST 8 /* InARP request. */ 90 #define ARPOP_InREPLY 9 /* InARP reply. */ 91 #define ARPOP_NAK 10 /* (ATM)ARP NAK. */ 92 93 // ARP header for IP over ethernet (RFC 826) 94 struct arp_header { 95 uint16 hardware_format; /* Format of hardware address. */ 96 uint16 protocol_format; /* Format of protocol address. */ 97 uint8 hardware_length; /* Length of hardware address. */ 98 uint8 protocol_length; /* Length of protocol address. */ 99 uint16 opcode; /* ARP opcode (command). */ 100 101 // IP over ethernet 102 mac_addr_t sender_mac; /* Sender hardware address. */ 103 ip_addr_t sender_ip; /* Sender IP address. */ 104 mac_addr_t target_mac; /* Target hardware address. */ 105 ip_addr_t target_ip; /* Target IP address. */ 106 } __attribute__ ((__packed__)); 107 108 // ARP protocol HARDWARE identifiers. 109 #define ARPHRD_ETHER 1 /* Ethernet 10/100Mbps. */ 110 111 112 // #pragma mark - 113 114 // Internet Protocol (IP) 115 116 #define INADDR_ANY ((ip_addr_t) 0x00000000) 117 /* Address to send to all hosts. */ 118 #define INADDR_BROADCAST ((ip_addr_t) 0xffffffff) 119 /* Address indicating an error return. */ 120 #define INADDR_NONE ((ip_addr_t) 0xffffffff) 121 122 // IP packet header (no options 123 struct ip_header { 124 #if __BYTE_ORDER == __LITTLE_ENDIAN 125 uint8 header_length:4; // header length 126 uint8 version:4; // IP protocol version 127 #endif 128 #if __BYTE_ORDER == __BIG_ENDIAN 129 uint8 version:4; // IP protocol version 130 uint8 header_length:4; // header length 131 #endif 132 uint8 type_of_service; // type of service 133 uint16 total_length; // total IP packet length 134 uint16 identifier; // fragment identification 135 uint16 fragment_offset; // fragment offset and flags (0xe000) 136 uint8 time_to_live; // time to live 137 uint8 protocol; // protocol 138 uint16 checksum; // checksum (header) 139 ip_addr_t source; // source IP address 140 ip_addr_t destination; // destination IP address 141 } __attribute__ ((__packed__)); 142 143 // IP protocol version 4 144 #define IP_PROTOCOL_VERSION_4 4 145 146 // fragment flags/offset mask 147 #define IP_DONT_FRAGMENT 0x4000 /* dont fragment flag */ 148 #define IP_FRAGMENT_OFFSET_MASK 0x1fff /* mask for fragment offset */ 149 150 // Internet implementation parameters. 151 #define IP_MAX_TIME_TO_LIVE 255 /* maximum time to live */ 152 #define IP_DEFAULT_TIME_TO_LIVE 64 /* default ttl, from RFC 1340 */ 153 154 // IP protocols 155 #define IPPROTO_UDP 17 156 157 158 // #pragma mark - 159 160 // User Datagram Protocol (UDP) 161 162 // UDP header (RFC 768) 163 struct udp_header 164 { 165 uint16 source; // source port 166 uint16 destination; // destination port 167 uint16 length; // length of UDP packet (header + data) 168 uint16 checksum; // checksum 169 } __attribute__ ((__packed__)); 170 171 172 // #pragma mark - 173 174 // NetService 175 176 // net service names 177 extern const char *const kEthernetServiceName; 178 extern const char *const kARPServiceName; 179 extern const char *const kIPServiceName; 180 extern const char *const kUDPServiceName; 181 182 class NetService { 183 public: 184 NetService(const char *name); 185 virtual ~NetService(); 186 187 const char *NetServiceName(); 188 189 virtual int CountSubNetServices() const; 190 virtual NetService *SubNetServiceAt(int index) const; 191 virtual NetService *FindSubNetService(const char *name) const; 192 193 template<typename ServiceType> 194 ServiceType *FindSubNetService(const char *name) const 195 { 196 // We should actually use dynamic_cast<>(), but we better spare us the 197 // RTTI stuff. 198 if (NetService *service = FindSubNetService(name)) 199 return static_cast<ServiceType*>(service); 200 return NULL; 201 } 202 203 private: 204 const char *fName; 205 }; 206 207 #endif // _BOOT_NET_DEFS_H 208