Line data Source code
1 : /* SPDX-License-Identifier: LGPL-2.1+ */
2 : #ifndef foosdlldphfoo
3 : #define foosdlldphfoo
4 :
5 : /***
6 : systemd is free software; you can redistribute it and/or modify it
7 : under the terms of the GNU Lesser General Public License as published by
8 : the Free Software Foundation; either version 2.1 of the License, or
9 : (at your option) any later version.
10 :
11 : systemd is distributed in the hope that it will be useful, but
12 : WITHOUT ANY WARRANTY; without even the implied warranty of
13 : MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 : Lesser General Public License for more details.
15 :
16 : You should have received a copy of the GNU Lesser General Public License
17 : along with systemd; If not, see <http://www.gnu.org/licenses/>.
18 : ***/
19 :
20 : #include <inttypes.h>
21 : #include <net/ethernet.h>
22 : #include <sys/types.h>
23 :
24 : #include "sd-event.h"
25 :
26 : #include "_sd-common.h"
27 :
28 : _SD_BEGIN_DECLARATIONS;
29 :
30 : /* IEEE 802.1AB-2009 Clause 8: TLV Types */
31 : enum {
32 : SD_LLDP_TYPE_END = 0,
33 : SD_LLDP_TYPE_CHASSIS_ID = 1,
34 : SD_LLDP_TYPE_PORT_ID = 2,
35 : SD_LLDP_TYPE_TTL = 3,
36 : SD_LLDP_TYPE_PORT_DESCRIPTION = 4,
37 : SD_LLDP_TYPE_SYSTEM_NAME = 5,
38 : SD_LLDP_TYPE_SYSTEM_DESCRIPTION = 6,
39 : SD_LLDP_TYPE_SYSTEM_CAPABILITIES = 7,
40 : SD_LLDP_TYPE_MGMT_ADDRESS = 8,
41 : SD_LLDP_TYPE_PRIVATE = 127,
42 : };
43 :
44 : /* IEEE 802.1AB-2009 Clause 8.5.2: Chassis subtypes */
45 : enum {
46 : SD_LLDP_CHASSIS_SUBTYPE_RESERVED = 0,
47 : SD_LLDP_CHASSIS_SUBTYPE_CHASSIS_COMPONENT = 1,
48 : SD_LLDP_CHASSIS_SUBTYPE_INTERFACE_ALIAS = 2,
49 : SD_LLDP_CHASSIS_SUBTYPE_PORT_COMPONENT = 3,
50 : SD_LLDP_CHASSIS_SUBTYPE_MAC_ADDRESS = 4,
51 : SD_LLDP_CHASSIS_SUBTYPE_NETWORK_ADDRESS = 5,
52 : SD_LLDP_CHASSIS_SUBTYPE_INTERFACE_NAME = 6,
53 : SD_LLDP_CHASSIS_SUBTYPE_LOCALLY_ASSIGNED = 7,
54 : };
55 :
56 : /* IEEE 802.1AB-2009 Clause 8.5.3: Port subtype */
57 : enum {
58 : SD_LLDP_PORT_SUBTYPE_RESERVED = 0,
59 : SD_LLDP_PORT_SUBTYPE_INTERFACE_ALIAS = 1,
60 : SD_LLDP_PORT_SUBTYPE_PORT_COMPONENT = 2,
61 : SD_LLDP_PORT_SUBTYPE_MAC_ADDRESS = 3,
62 : SD_LLDP_PORT_SUBTYPE_NETWORK_ADDRESS = 4,
63 : SD_LLDP_PORT_SUBTYPE_INTERFACE_NAME = 5,
64 : SD_LLDP_PORT_SUBTYPE_AGENT_CIRCUIT_ID = 6,
65 : SD_LLDP_PORT_SUBTYPE_LOCALLY_ASSIGNED = 7,
66 : };
67 :
68 : /* IEEE 802.1AB-2009 Clause 8.5.8: System capabilities */
69 : enum {
70 : SD_LLDP_SYSTEM_CAPABILITIES_OTHER = 1 << 0,
71 : SD_LLDP_SYSTEM_CAPABILITIES_REPEATER = 1 << 1,
72 : SD_LLDP_SYSTEM_CAPABILITIES_BRIDGE = 1 << 2,
73 : SD_LLDP_SYSTEM_CAPABILITIES_WLAN_AP = 1 << 3,
74 : SD_LLDP_SYSTEM_CAPABILITIES_ROUTER = 1 << 4,
75 : SD_LLDP_SYSTEM_CAPABILITIES_PHONE = 1 << 5,
76 : SD_LLDP_SYSTEM_CAPABILITIES_DOCSIS = 1 << 6,
77 : SD_LLDP_SYSTEM_CAPABILITIES_STATION = 1 << 7,
78 : SD_LLDP_SYSTEM_CAPABILITIES_CVLAN = 1 << 8,
79 : SD_LLDP_SYSTEM_CAPABILITIES_SVLAN = 1 << 9,
80 : SD_LLDP_SYSTEM_CAPABILITIES_TPMR = 1 << 10,
81 : };
82 :
83 : #define SD_LLDP_SYSTEM_CAPABILITIES_ALL ((uint16_t) -1)
84 :
85 : #define SD_LLDP_SYSTEM_CAPABILITIES_ALL_ROUTERS \
86 : ((uint16_t) \
87 : (SD_LLDP_SYSTEM_CAPABILITIES_REPEATER| \
88 : SD_LLDP_SYSTEM_CAPABILITIES_BRIDGE| \
89 : SD_LLDP_SYSTEM_CAPABILITIES_WLAN_AP| \
90 : SD_LLDP_SYSTEM_CAPABILITIES_ROUTER| \
91 : SD_LLDP_SYSTEM_CAPABILITIES_DOCSIS| \
92 : SD_LLDP_SYSTEM_CAPABILITIES_CVLAN| \
93 : SD_LLDP_SYSTEM_CAPABILITIES_SVLAN| \
94 : SD_LLDP_SYSTEM_CAPABILITIES_TPMR))
95 :
96 : #define SD_LLDP_OUI_802_1 (uint8_t[]) { 0x00, 0x80, 0xc2 }
97 : #define SD_LLDP_OUI_802_3 (uint8_t[]) { 0x00, 0x12, 0x0f }
98 :
99 : /* IEEE 802.1AB-2009 Annex E */
100 : enum {
101 : SD_LLDP_OUI_802_1_SUBTYPE_PORT_VLAN_ID = 1,
102 : SD_LLDP_OUI_802_1_SUBTYPE_PORT_PROTOCOL_VLAN_ID = 2,
103 : SD_LLDP_OUI_802_1_SUBTYPE_VLAN_NAME = 3,
104 : SD_LLDP_OUI_802_1_SUBTYPE_PROTOCOL_IDENTITY = 4,
105 : SD_LLDP_OUI_802_1_SUBTYPE_VID_USAGE_DIGEST = 5,
106 : SD_LLDP_OUI_802_1_SUBTYPE_MANAGEMENT_VID = 6,
107 : SD_LLDP_OUI_802_1_SUBTYPE_LINK_AGGREGATION = 7,
108 : };
109 :
110 : /* IEEE 802.1AB-2009 Annex F */
111 : enum {
112 : SD_LLDP_OUI_802_3_SUBTYPE_MAC_PHY_CONFIG_STATUS = 1,
113 : SD_LLDP_OUI_802_3_SUBTYPE_POWER_VIA_MDI = 2,
114 : SD_LLDP_OUI_802_3_SUBTYPE_LINK_AGGREGATION = 3,
115 : SD_LLDP_OUI_802_3_SUBTYPE_MAXIMUM_FRAME_SIZE = 4,
116 : };
117 :
118 : typedef struct sd_lldp sd_lldp;
119 : typedef struct sd_lldp_neighbor sd_lldp_neighbor;
120 :
121 : typedef enum sd_lldp_event {
122 : SD_LLDP_EVENT_ADDED,
123 : SD_LLDP_EVENT_REMOVED,
124 : SD_LLDP_EVENT_UPDATED,
125 : SD_LLDP_EVENT_REFRESHED,
126 : _SD_LLDP_EVENT_MAX,
127 : _SD_LLDP_EVENT_INVALID = -1,
128 : } sd_lldp_event;
129 :
130 : typedef void (*sd_lldp_callback_t)(sd_lldp *lldp, sd_lldp_event event, sd_lldp_neighbor *n, void *userdata);
131 :
132 : int sd_lldp_new(sd_lldp **ret);
133 : sd_lldp* sd_lldp_ref(sd_lldp *lldp);
134 : sd_lldp* sd_lldp_unref(sd_lldp *lldp);
135 :
136 : int sd_lldp_start(sd_lldp *lldp);
137 : int sd_lldp_stop(sd_lldp *lldp);
138 :
139 : int sd_lldp_attach_event(sd_lldp *lldp, sd_event *event, int64_t priority);
140 : int sd_lldp_detach_event(sd_lldp *lldp);
141 : sd_event *sd_lldp_get_event(sd_lldp *lldp);
142 :
143 : int sd_lldp_set_callback(sd_lldp *lldp, sd_lldp_callback_t cb, void *userdata);
144 : int sd_lldp_set_ifindex(sd_lldp *lldp, int ifindex);
145 :
146 : /* Controls how much and what to store in the neighbors database */
147 : int sd_lldp_set_neighbors_max(sd_lldp *lldp, uint64_t n);
148 : int sd_lldp_match_capabilities(sd_lldp *lldp, uint16_t mask);
149 : int sd_lldp_set_filter_address(sd_lldp *lldp, const struct ether_addr *address);
150 :
151 : int sd_lldp_get_neighbors(sd_lldp *lldp, sd_lldp_neighbor ***neighbors);
152 :
153 : int sd_lldp_neighbor_from_raw(sd_lldp_neighbor **ret, const void *raw, size_t raw_size);
154 : sd_lldp_neighbor *sd_lldp_neighbor_ref(sd_lldp_neighbor *n);
155 : sd_lldp_neighbor *sd_lldp_neighbor_unref(sd_lldp_neighbor *n);
156 :
157 : /* Access to LLDP frame metadata */
158 : int sd_lldp_neighbor_get_source_address(sd_lldp_neighbor *n, struct ether_addr* address);
159 : int sd_lldp_neighbor_get_destination_address(sd_lldp_neighbor *n, struct ether_addr* address);
160 : int sd_lldp_neighbor_get_timestamp(sd_lldp_neighbor *n, clockid_t clock, uint64_t *ret);
161 : int sd_lldp_neighbor_get_raw(sd_lldp_neighbor *n, const void **ret, size_t *size);
162 :
163 : /* High-level, direct, parsed out field access. These fields exist at most once, hence may be queried directly. */
164 : int sd_lldp_neighbor_get_chassis_id(sd_lldp_neighbor *n, uint8_t *type, const void **ret, size_t *size);
165 : int sd_lldp_neighbor_get_chassis_id_as_string(sd_lldp_neighbor *n, const char **ret);
166 : int sd_lldp_neighbor_get_port_id(sd_lldp_neighbor *n, uint8_t *type, const void **ret, size_t *size);
167 : int sd_lldp_neighbor_get_port_id_as_string(sd_lldp_neighbor *n, const char **ret);
168 : int sd_lldp_neighbor_get_ttl(sd_lldp_neighbor *n, uint16_t *ret_sec);
169 : int sd_lldp_neighbor_get_system_name(sd_lldp_neighbor *n, const char **ret);
170 : int sd_lldp_neighbor_get_system_description(sd_lldp_neighbor *n, const char **ret);
171 : int sd_lldp_neighbor_get_port_description(sd_lldp_neighbor *n, const char **ret);
172 : int sd_lldp_neighbor_get_system_capabilities(sd_lldp_neighbor *n, uint16_t *ret);
173 : int sd_lldp_neighbor_get_enabled_capabilities(sd_lldp_neighbor *n, uint16_t *ret);
174 :
175 : /* Low-level, iterative TLV access. This is for evertyhing else, it iteratively goes through all available TLVs
176 : * (including the ones covered with the calls above), and allows multiple TLVs for the same fields. */
177 : int sd_lldp_neighbor_tlv_rewind(sd_lldp_neighbor *n);
178 : int sd_lldp_neighbor_tlv_next(sd_lldp_neighbor *n);
179 : int sd_lldp_neighbor_tlv_get_type(sd_lldp_neighbor *n, uint8_t *type);
180 : int sd_lldp_neighbor_tlv_is_type(sd_lldp_neighbor *n, uint8_t type);
181 : int sd_lldp_neighbor_tlv_get_oui(sd_lldp_neighbor *n, uint8_t oui[_SD_ARRAY_STATIC 3], uint8_t *subtype);
182 : int sd_lldp_neighbor_tlv_is_oui(sd_lldp_neighbor *n, const uint8_t oui[_SD_ARRAY_STATIC 3], uint8_t subtype);
183 : int sd_lldp_neighbor_tlv_get_raw(sd_lldp_neighbor *n, const void **ret, size_t *size);
184 :
185 4 : _SD_DEFINE_POINTER_CLEANUP_FUNC(sd_lldp, sd_lldp_unref);
186 25 : _SD_DEFINE_POINTER_CLEANUP_FUNC(sd_lldp_neighbor, sd_lldp_neighbor_unref);
187 :
188 : _SD_END_DECLARATIONS;
189 :
190 : #endif
|