| 1 | /*
|
|---|
| 2 | * Copyright (c) 2009 Lukas Mejdrech
|
|---|
| 3 | * Copyright (c) 2011 Radim Vansa
|
|---|
| 4 | * All rights reserved.
|
|---|
| 5 | *
|
|---|
| 6 | * Redistribution and use in source and binary forms, with or without
|
|---|
| 7 | * modification, are permitted provided that the following conditions
|
|---|
| 8 | * are met:
|
|---|
| 9 | *
|
|---|
| 10 | * - Redistributions of source code must retain the above copyright
|
|---|
| 11 | * notice, this list of conditions and the following disclaimer.
|
|---|
| 12 | * - Redistributions in binary form must reproduce the above copyright
|
|---|
| 13 | * notice, this list of conditions and the following disclaimer in the
|
|---|
| 14 | * documentation and/or other materials provided with the distribution.
|
|---|
| 15 | * - The name of the author may not be used to endorse or promote products
|
|---|
| 16 | * derived from this software without specific prior written permission.
|
|---|
| 17 | *
|
|---|
| 18 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
|---|
| 19 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
|---|
| 20 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
|---|
| 21 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
|---|
| 22 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
|---|
| 23 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
|---|
| 24 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
|---|
| 25 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
|---|
| 26 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
|---|
| 27 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|---|
| 28 | */
|
|---|
| 29 |
|
|---|
| 30 | /** @addtogroup eth
|
|---|
| 31 | * @{
|
|---|
| 32 | */
|
|---|
| 33 |
|
|---|
| 34 | /** @file
|
|---|
| 35 | * Ethernet module.
|
|---|
| 36 | */
|
|---|
| 37 |
|
|---|
| 38 | #ifndef NET_ETH_H_
|
|---|
| 39 | #define NET_ETH_H_
|
|---|
| 40 |
|
|---|
| 41 | #include <async.h>
|
|---|
| 42 | #include <fibril_synch.h>
|
|---|
| 43 | #include <ipc/services.h>
|
|---|
| 44 | #include <net/device.h>
|
|---|
| 45 | #include <adt/measured_strings.h>
|
|---|
| 46 | #include <devman.h>
|
|---|
| 47 |
|
|---|
| 48 | /** Ethernet address length. */
|
|---|
| 49 | #define ETH_ADDR 6
|
|---|
| 50 |
|
|---|
| 51 | /** Ethernet header preamble value. */
|
|---|
| 52 | #define ETH_PREAMBLE 0x55
|
|---|
| 53 |
|
|---|
| 54 | /** Ethernet header start of frame value. */
|
|---|
| 55 | #define ETH_SFD 0xD5
|
|---|
| 56 |
|
|---|
| 57 | /** IEEE 802.2 unordered information control field. */
|
|---|
| 58 | #define IEEE_8023_2_UI 0x03
|
|---|
| 59 |
|
|---|
| 60 | /** Type definition of the Ethernet header IEEE 802.3 + 802.2 + SNAP extensions.
|
|---|
| 61 | * @see eth_header_snap
|
|---|
| 62 | */
|
|---|
| 63 | typedef struct eth_header_snap eth_header_snap_t;
|
|---|
| 64 |
|
|---|
| 65 | /** Type definition of the Ethernet header IEEE 802.3 + 802.2 + SNAP extensions.
|
|---|
| 66 | * @see eth_header_lsap
|
|---|
| 67 | */
|
|---|
| 68 | typedef struct eth_header_lsap eth_header_lsap_t;
|
|---|
| 69 |
|
|---|
| 70 | /** Type definition of the Ethernet header LSAP extension.
|
|---|
| 71 | * @see eth_ieee_lsap
|
|---|
| 72 | */
|
|---|
| 73 | typedef struct eth_ieee_lsap eth_ieee_lsap_t;
|
|---|
| 74 |
|
|---|
| 75 | /** Type definition of the Ethernet header SNAP extension.
|
|---|
| 76 | * @see eth_snap
|
|---|
| 77 | */
|
|---|
| 78 | typedef struct eth_snap eth_snap_t;
|
|---|
| 79 |
|
|---|
| 80 | /** Type definition of the Ethernet header preamble.
|
|---|
| 81 | * @see preamble
|
|---|
| 82 | */
|
|---|
| 83 | typedef struct eth_preamble eth_preamble_t;
|
|---|
| 84 |
|
|---|
| 85 | /** Type definition of the Ethernet header.
|
|---|
| 86 | * @see eth_header
|
|---|
| 87 | */
|
|---|
| 88 | typedef struct eth_header eth_header_t;
|
|---|
| 89 |
|
|---|
| 90 | /** Ethernet header Link Service Access Point extension. */
|
|---|
| 91 | struct eth_ieee_lsap {
|
|---|
| 92 | /**
|
|---|
| 93 | * Destination Service Access Point identifier.
|
|---|
| 94 | * The possible values are assigned by an IEEE committee.
|
|---|
| 95 | */
|
|---|
| 96 | uint8_t dsap;
|
|---|
| 97 |
|
|---|
| 98 | /**
|
|---|
| 99 | * Source Service Access Point identifier.
|
|---|
| 100 | * The possible values are assigned by an IEEE committee.
|
|---|
| 101 | */
|
|---|
| 102 | uint8_t ssap;
|
|---|
| 103 |
|
|---|
| 104 | /**
|
|---|
| 105 | * Control parameter.
|
|---|
| 106 | * The possible values are assigned by an IEEE committee.
|
|---|
| 107 | */
|
|---|
| 108 | uint8_t ctrl;
|
|---|
| 109 | } __attribute__ ((packed));
|
|---|
| 110 |
|
|---|
| 111 | /** Ethernet header SNAP extension. */
|
|---|
| 112 | struct eth_snap {
|
|---|
| 113 | /** Protocol identifier or organization code. */
|
|---|
| 114 | uint8_t protocol[3];
|
|---|
| 115 |
|
|---|
| 116 | /**
|
|---|
| 117 | * Ethernet protocol identifier in the network byte order (big endian).
|
|---|
| 118 | * @see ethernet_protocols.h
|
|---|
| 119 | */
|
|---|
| 120 | uint16_t ethertype;
|
|---|
| 121 | } __attribute__ ((packed));
|
|---|
| 122 |
|
|---|
| 123 | /** Ethernet header preamble.
|
|---|
| 124 | *
|
|---|
| 125 | * Used for dummy devices.
|
|---|
| 126 | */
|
|---|
| 127 | struct eth_preamble {
|
|---|
| 128 | /**
|
|---|
| 129 | * Controlling preamble used for the frame transmission synchronization.
|
|---|
| 130 | * All should be set to ETH_PREAMBLE.
|
|---|
| 131 | */
|
|---|
| 132 | uint8_t preamble[7];
|
|---|
| 133 |
|
|---|
| 134 | /**
|
|---|
| 135 | * Start of Frame Delimiter used for the frame transmission
|
|---|
| 136 | * synchronization.
|
|---|
| 137 | * Should be set to ETH_SFD.
|
|---|
| 138 | */
|
|---|
| 139 | uint8_t sfd;
|
|---|
| 140 | } __attribute__ ((packed));
|
|---|
| 141 |
|
|---|
| 142 | /** Ethernet header. */
|
|---|
| 143 | struct eth_header {
|
|---|
| 144 | /** Destination host Ethernet address (MAC address). */
|
|---|
| 145 | uint8_t destination_address[ETH_ADDR];
|
|---|
| 146 | /** Source host Ethernet address (MAC address). */
|
|---|
| 147 | uint8_t source_address[ETH_ADDR];
|
|---|
| 148 |
|
|---|
| 149 | /**
|
|---|
| 150 | * Ethernet protocol identifier in the network byte order (big endian).
|
|---|
| 151 | * @see ethernet_protocols.h
|
|---|
| 152 | */
|
|---|
| 153 | uint16_t ethertype;
|
|---|
| 154 | } __attribute__ ((packed));
|
|---|
| 155 |
|
|---|
| 156 | /** Ethernet header IEEE 802.3 + 802.2 extension. */
|
|---|
| 157 | struct eth_header_lsap {
|
|---|
| 158 | /** Ethernet header. */
|
|---|
| 159 | eth_header_t header;
|
|---|
| 160 |
|
|---|
| 161 | /**
|
|---|
| 162 | * LSAP extension.
|
|---|
| 163 | * If DSAP and SSAP are set to ETH_LSAP_SNAP the SNAP extension is being
|
|---|
| 164 | * used.
|
|---|
| 165 | * If DSAP and SSAP fields are equal to ETH_RAW the raw Ethernet packet
|
|---|
| 166 | * without any extensions is being used and the frame content starts
|
|---|
| 167 | * rigth after the two fields.
|
|---|
| 168 | */
|
|---|
| 169 | eth_ieee_lsap_t lsap;
|
|---|
| 170 | } __attribute__ ((packed));
|
|---|
| 171 |
|
|---|
| 172 | /** Ethernet header IEEE 802.3 + 802.2 + SNAP extensions. */
|
|---|
| 173 | struct eth_header_snap {
|
|---|
| 174 | /** Ethernet header. */
|
|---|
| 175 | eth_header_t header;
|
|---|
| 176 |
|
|---|
| 177 | /**
|
|---|
| 178 | * LSAP extension.
|
|---|
| 179 | * If DSAP and SSAP are set to ETH_LSAP_SNAP the SNAP extension is being
|
|---|
| 180 | * used.
|
|---|
| 181 | * If DSAP and SSAP fields are equal to ETH_RAW the raw Ethernet packet
|
|---|
| 182 | * without any extensions is being used and the frame content starts
|
|---|
| 183 | * rigth after the two fields.
|
|---|
| 184 | */
|
|---|
| 185 | eth_ieee_lsap_t lsap;
|
|---|
| 186 |
|
|---|
| 187 | /** SNAP extension. */
|
|---|
| 188 | eth_snap_t snap;
|
|---|
| 189 | } __attribute__ ((packed));
|
|---|
| 190 |
|
|---|
| 191 | /** Ethernet Frame Check Sequence. */
|
|---|
| 192 | typedef uint32_t eth_fcs_t;
|
|---|
| 193 |
|
|---|
| 194 | /** Type definition of the Ethernet global data.
|
|---|
| 195 | * @see eth_globals
|
|---|
| 196 | */
|
|---|
| 197 | typedef struct eth_globals eth_globals_t;
|
|---|
| 198 |
|
|---|
| 199 | /** Type definition of the Ethernet device specific data.
|
|---|
| 200 | * @see eth_device
|
|---|
| 201 | */
|
|---|
| 202 | typedef struct eth_device eth_device_t;
|
|---|
| 203 |
|
|---|
| 204 | /** Type definition of the Ethernet protocol specific data.
|
|---|
| 205 | * @see eth_proto
|
|---|
| 206 | */
|
|---|
| 207 | typedef struct eth_proto eth_proto_t;
|
|---|
| 208 |
|
|---|
| 209 | /** Ethernet device map.
|
|---|
| 210 | * Maps devices to the Ethernet device specific data.
|
|---|
| 211 | * @see device.h
|
|---|
| 212 | */
|
|---|
| 213 | DEVICE_MAP_DECLARE(eth_devices, eth_device_t);
|
|---|
| 214 |
|
|---|
| 215 | /** Ethernet protocol map.
|
|---|
| 216 | * Maps protocol identifiers to the Ethernet protocol specific data.
|
|---|
| 217 | * @see int_map.h
|
|---|
| 218 | */
|
|---|
| 219 | INT_MAP_DECLARE(eth_protos, eth_proto_t);
|
|---|
| 220 |
|
|---|
| 221 | /** Ethernet device specific data. */
|
|---|
| 222 | struct eth_device {
|
|---|
| 223 | /** Device identifier. */
|
|---|
| 224 | nic_device_id_t device_id;
|
|---|
| 225 | /** Device handle */
|
|---|
| 226 | devman_handle_t handle;
|
|---|
| 227 | /** Driver session. */
|
|---|
| 228 | async_sess_t *sess;
|
|---|
| 229 | /** Maximal transmission unit. */
|
|---|
| 230 | size_t mtu;
|
|---|
| 231 |
|
|---|
| 232 | /**
|
|---|
| 233 | * Various device flags.
|
|---|
| 234 | * @see ETH_DUMMY
|
|---|
| 235 | * @see ETH_MODE_MASK
|
|---|
| 236 | */
|
|---|
| 237 | int flags;
|
|---|
| 238 |
|
|---|
| 239 | /** Actual device hardware address. */
|
|---|
| 240 | nic_address_t addr;
|
|---|
| 241 | };
|
|---|
| 242 |
|
|---|
| 243 | /** Ethernet protocol specific data. */
|
|---|
| 244 | struct eth_proto {
|
|---|
| 245 | /** Protocol service. */
|
|---|
| 246 | services_t service;
|
|---|
| 247 | /** Protocol identifier. */
|
|---|
| 248 | int protocol;
|
|---|
| 249 | /** Protocol module session. */
|
|---|
| 250 | async_sess_t *sess;
|
|---|
| 251 | };
|
|---|
| 252 |
|
|---|
| 253 | /** Ethernet global data. */
|
|---|
| 254 | struct eth_globals {
|
|---|
| 255 | /** Networking module session. */
|
|---|
| 256 | async_sess_t *net_sess;
|
|---|
| 257 | /** Safety lock for devices. */
|
|---|
| 258 | fibril_rwlock_t devices_lock;
|
|---|
| 259 | /** All known Ethernet devices. */
|
|---|
| 260 | eth_devices_t devices;
|
|---|
| 261 | /** Safety lock for protocols. */
|
|---|
| 262 | fibril_rwlock_t protos_lock;
|
|---|
| 263 |
|
|---|
| 264 | /**
|
|---|
| 265 | * Protocol map.
|
|---|
| 266 | * Service map for each protocol.
|
|---|
| 267 | */
|
|---|
| 268 | eth_protos_t protos;
|
|---|
| 269 |
|
|---|
| 270 | /** Broadcast device hardware address. */
|
|---|
| 271 | uint8_t broadcast_addr[ETH_ADDR];
|
|---|
| 272 | };
|
|---|
| 273 |
|
|---|
| 274 | #endif
|
|---|
| 275 |
|
|---|
| 276 | /** @}
|
|---|
| 277 | */
|
|---|