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