1 /* $Id: ethernet.h,v 1.6 2007/02/15 01:27:13 fredette Exp $ */
2 
3 /* tme/generic/ethernet.h - header file for generic ethernet support: */
4 
5 /*
6  * Copyright (c) 2003 Matt Fredette
7  * All rights reserved.
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions
11  * are met:
12  * 1. Redistributions of source code must retain the above copyright
13  *    notice, this list of conditions and the following disclaimer.
14  * 2. Redistributions in binary form must reproduce the above copyright
15  *    notice, this list of conditions and the following disclaimer in the
16  *    documentation and/or other materials provided with the distribution.
17  * 3. All advertising materials mentioning features or use of this software
18  *    must display the following acknowledgement:
19  *      This product includes software developed by Matt Fredette.
20  * 4. The name of the author may not be used to endorse or promote products
21  *    derived from this software without specific prior written permission.
22  *
23  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
24  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
25  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
26  * DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
27  * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
28  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
29  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
31  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
32  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
33  * POSSIBILITY OF SUCH DAMAGE.
34  */
35 
36 #ifndef _TME_GENERIC_ETHERNET_H
37 #define _TME_GENERIC_ETHERNET_H
38 
39 #include <tme/common.h>
40 _TME_RCSID("$Id: ethernet.h,v 1.6 2007/02/15 01:27:13 fredette Exp $");
41 
42 /* includes: */
43 #include <tme/element.h>
44 
45 /* macros: */
46 
47 /* the size of an address: */
48 #define TME_ETHERNET_ADDR_SIZE		(6)
49 
50 /* the size of a length field: */
51 #define TME_ETHERNET_LENGTH_SIZE	(2)
52 
53 /* the size of a CRC: */
54 #define TME_ETHERNET_CRC_SIZE		(4)
55 
56 /* the size of an Ethernet header: */
57 #define TME_ETHERNET_HEADER_SIZE	(TME_ETHERNET_ADDR_SIZE + TME_ETHERNET_ADDR_SIZE + TME_ETHERNET_LENGTH_SIZE)
58 
59 /* the minimum and maximum sizes of an Ethernet frame, including the
60    complete header and CRC: */
61 #define TME_ETHERNET_FRAME_MIN		(64)
62 #define TME_ETHERNET_FRAME_MAX		(1518)
63 
64 /* Ethernet types: */
65 #define TME_ETHERNET_TYPE_IPV4		(0x0800)
66 #define TME_ETHERNET_TYPE_ARP		(0x0806)
67 #define TME_ETHERNET_TYPE_RARP		(0x8035)
68 
69 /* Ethernet config flags: */
70 #define TME_ETHERNET_CONFIG_NORMAL	(0)
71 #define TME_ETHERNET_CONFIG_PROMISC	TME_BIT(0)
72 
73 /* Ethernet control flags: */
74 #define TME_ETHERNET_CTRL_COLL_INPUT	TME_BIT(0)
75 #define TME_ETHERNET_CTRL_COLL_OUTPUT	TME_BIT(1)
76 #define TME_ETHERNET_CTRL_OK_READ	TME_BIT(2)
77 
78 /* Ethernet read flags: */
79 #define TME_ETHERNET_READ_SPECIFIC	(0)
80 #define TME_ETHERNET_READ_NEXT		TME_BIT(0)
81 #define TME_ETHERNET_READ_PEEK		TME_BIT(1)
82 
83 /* types: */
84 
85 /* an ethernet frame identifier: */
86 typedef tme_uint32_t tme_ethernet_fid_t;
87 
88 /* an ethernet header: */
89 struct tme_ethernet_header {
90 
91   /* the destination address: */
92   tme_uint8_t tme_ethernet_header_dst[TME_ETHERNET_ADDR_SIZE];
93 
94   /* the source address: */
95   tme_uint8_t tme_ethernet_header_src[TME_ETHERNET_ADDR_SIZE];
96 
97   /* the type: */
98   tme_uint8_t tme_ethernet_header_type[2];
99 };
100 
101 /* a ethernet configuration: */
102 struct tme_ethernet_config {
103 
104   /* the config flags: */
105   tme_uint32_t tme_ethernet_config_flags;
106 
107   /* the set of specific Ethernet addresses to receive packets for: */
108   unsigned int tme_ethernet_config_addr_count;
109   _tme_const tme_uint8_t **tme_ethernet_config_addrs;
110 };
111 
112 /* an ethernet frame chunk: */
113 struct tme_ethernet_frame_chunk {
114 
115   /* the next chunk in the frame: */
116   struct tme_ethernet_frame_chunk *tme_ethernet_frame_chunk_next;
117 
118   /* a pointer to the bytes in this chunk: */
119   tme_uint8_t *tme_ethernet_frame_chunk_bytes;
120 
121   /* the number of bytes in this chunk: */
122   unsigned int tme_ethernet_frame_chunk_bytes_count;
123 };
124 
125 /* a ethernet connection: */
126 struct tme_ethernet_connection {
127 
128   /* the generic connection side: */
129   struct tme_connection tme_ethernet_connection;
130 
131   /* this is called when the ethernet configuration changes: */
132   int (*tme_ethernet_connection_config) _TME_P((struct tme_ethernet_connection *,
133 						struct tme_ethernet_config *));
134 
135   /* this is called when control flags change: */
136   int (*tme_ethernet_connection_ctrl) _TME_P((struct tme_ethernet_connection *,
137 					      unsigned int));
138 
139   /* this is called to read data: */
140   int (*tme_ethernet_connection_read) _TME_P((struct tme_ethernet_connection *,
141 					      tme_ethernet_fid_t *,
142 					      struct tme_ethernet_frame_chunk *,
143 					      unsigned int));
144 };
145 
146 /* globals: */
147 extern _tme_const tme_uint8_t tme_ethernet_addr_broadcast[TME_ETHERNET_ADDR_SIZE];
148 
149 /* prototypes: */
150 int tme_ethernet_addr_parse _TME_P((_tme_const char *, tme_uint8_t *));
151 int tme_ethernet_connection_score _TME_P((struct tme_connection *, unsigned int *));
152 unsigned int tme_ethernet_chunks_copy _TME_P((_tme_const struct tme_ethernet_frame_chunk *, _tme_const struct tme_ethernet_frame_chunk *));
153 tme_uint32_t tme_ethernet_crc32_el _TME_P((_tme_const tme_uint8_t *, unsigned int));
154 
155 #endif /* !_TME_GENERIC_ETHERNET_H */
156