1 /** @file lldp_port.h
2  *
3  * OpenLLDP Port Header
4  *
5  * See LICENSE file for more info.
6  *
7  *
8  * Authors: Terry Simons (terry.simons@gmail.com)
9  *
10  *******************************************************************/
11 
12 #ifndef LLDP_PORT_H
13 #define LLDP_PORT_H
14 
15 #ifdef WIN32
16 #include "stdintwin.h"
17 #include <BaseTsd.h>
18 #define ssize_t SSIZE_T
19 #else
20 #include <stdint.h>
21 #include <unistd.h>
22 #endif // WIN32
23 
24 #include <time.h>
25 
26 /**
27   Maxiumum ethernet interfaces.
28 
29   Used to iterate through the interface list.
30   */
31 #define MIN_INTERFACES   1
32 #define MAX_INTERFACES 254
33 
34 /**
35   LLDP interface transmission statistics.
36 
37   Part of lldp_tx_port.  Tied to the tx state machine.
38   */
39 struct lldp_tx_port_statistics {
40     uint64_t statsFramesOutTotal; /**< Defined by IEEE 802.1AB Secion 10.5.2.1 */
41 };
42 
43 
44 /**
45   LLDP interface transmission timers.
46 
47   Part of lldp_tx_port.
48 
49   These timers are per-interface, and have a resolution of:
50   0 < n < 6553 as per the IEEE 802.1AB specification.
51   */
52 struct lldp_tx_port_timers {
53     uint16_t reinitDelay;   /**< IEEE 802.1AB 10.5.3 */
54     uint16_t msgTxHold;     /**< IEEE 802.1AB 10.5.3 */
55     uint16_t msgTxInterval; /**< IEEE 802.1AB 10.5.3 */
56     uint16_t txDelay;       /**< IEEE 802.1AB 10.5.3 */
57 
58     uint16_t txTTR;         /**< IEEE 802.1AB 10.5.3 - transmit on expire. */
59 
60     /* Not sure what this is for */
61     uint16_t txShutdownWhile;
62     uint16_t txDelayWhile;
63 };
64 
65 /**
66   The LLDP transmit state machine guts.
67 
68   This is a per-interface structure.  Part of lldp_port.
69   */
70 struct lldp_tx_port {
71     uint8_t *frame;    /**< The tx frame buffer */
72     uint64_t sendsize; /**< The size of our tx frame */
73     uint8_t state;     /**< The tx state for this interface */
74     uint8_t somethingChangedLocal; /**< IEEE 802.1AB var (from where?) */
75     uint16_t txTTL;/**< IEEE 802.1AB var (from where?) */
76     struct lldp_tx_port_timers timers; /**< The lldp tx state machine timers for this interface */
77     struct lldp_tx_port_statistics statistics; /**< The lldp tx statistics for this interface */
78 };
79 
80 
81 
82 struct lldp_rx_port_statistics {
83     uint64_t statsAgeoutsTotal;
84     uint64_t statsFramesDiscardedTotal;
85     uint64_t statsFramesInErrorsTotal;
86     uint64_t statsFramesInTotal;
87     uint64_t statsTLVsDiscardedTotal;
88     uint64_t statsTLVsUnrecognizedTotal;
89 };
90 
91 struct lldp_rx_port_timers {
92     uint16_t tooManyNeighborsTimer;
93     uint16_t rxTTL;
94 };
95 
96 struct lldp_rx_port {
97     uint8_t *frame;
98     ssize_t recvsize;
99     uint8_t state;
100     uint8_t badFrame;
101     uint8_t rcvFrame;
102     //uint8_t rxChanges; /* This belongs in the MSAP cache */
103     uint8_t rxInfoAge;
104     uint8_t somethingChangedRemote;
105     uint8_t tooManyNeighbors;
106     struct lldp_rx_port_timers timers;
107     struct lldp_rx_port_statistics statistics;
108   //    struct lldp_msap_cache *msap;
109 };
110 
111 struct eth_hdr {
112     char dst[6];
113     char src[6];
114     uint16_t ethertype;
115 };
116 
117 enum portAdminStatus {
118     disabled,
119     enabledTxOnly,
120     enabledRxOnly,
121     enabledRxTx,
122 };
123 
124 struct lldp_port {
125   struct lldp_port *next;
126   int socket;        // The socket descriptor for this interface.
127   char *if_name;     // The interface name.
128   uint32_t if_index; // The interface index.
129   uint32_t mtu;      // The interface MTU.
130   uint8_t source_mac[6];
131   uint8_t source_ipaddr[4];
132   struct lldp_rx_port rx;
133   struct lldp_tx_port tx;
134   uint8_t portEnabled;
135   uint8_t adminStatus;
136 
137   /* I'm not sure where this goes... the state machine indicates it's per-port */
138   uint8_t rxChanges;
139 
140   // I'm really unsure about the best way to handle this...
141   uint8_t tick;
142   time_t last_tick;
143 
144   struct lldp_msap *msap_cache;
145 
146 
147   // 802.1AB Appendix G flag variables.
148   uint8_t  auto_neg_status;
149   uint16_t auto_neg_advertized_capabilities;
150   uint16_t operational_mau_type;
151 };
152 
153 struct lldp_msap {
154   struct lldp_msap *next;
155   uint8_t *id;
156   uint8_t length;
157   struct lldp_tlv_list *tlv_list;
158 
159   // XXX Revisit this
160   // A pointer to the TTL TLV
161   // This is a hack to decrement
162   // the timer properly for
163   // lldpneighbors output
164   struct lldp_tlv *ttl_tlv;
165 
166   /* IEEE 802.1AB MSAP-specific counters */
167   uint16_t rxInfoTTL;
168 };
169 
170 #endif /* LLDP_PORT_H */
171