1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
|
/*
* Copyright (C) 2023 Michael Brown <mbrown@fensystems.co.uk>.
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License as
* published by the Free Software Foundation; either version 2 of the
* License, or any later version.
*
* This program is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
* 02110-1301, USA.
*
* You can also choose to distribute this program under the terms of
* the Unmodified Binary Distribution Licence (as given in the file
* COPYING.UBDL), provided that you have satisfied its requirements.
*/
FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL );
/** @file
*
* Link Layer Discovery Protocol
*
*/
#include <stdlib.h>
#include <errno.h>
#include <byteswap.h>
#include <ipxe/iobuf.h>
#include <ipxe/netdevice.h>
#include <ipxe/if_ether.h>
#include <ipxe/settings.h>
#include <ipxe/lldp.h>
/** An LLDP settings block */
struct lldp_settings {
/** Reference counter */
struct refcnt refcnt;
/** Settings interface */
struct settings settings;
/** List of LLDP settings blocks */
struct list_head list;
/** Name */
const char *name;
/** LLDP data */
void *data;
/** Length of LLDP data */
size_t len;
};
/** LLDP settings scope */
static const struct settings_scope lldp_settings_scope;
/** List of LLDP settings blocks */
static LIST_HEAD ( lldp_settings );
/**
* Free LLDP settings block
*
* @v refcnt Reference counter
*/
static void lldp_free ( struct refcnt *refcnt ) {
struct lldp_settings *lldpset =
container_of ( refcnt, struct lldp_settings, refcnt );
DBGC ( lldpset, "LLDP %s freed\n", lldpset->name );
list_del ( &lldpset->list );
free ( lldpset->data );
free ( lldpset );
}
/**
* Find LLDP settings block
*
* @v netdev Network device
* @ret lldpset LLDP settings block
*/
static struct lldp_settings * lldp_find ( struct net_device *netdev ) {
struct lldp_settings *lldpset;
/* Find matching LLDP settings block */
list_for_each_entry ( lldpset, &lldp_settings, list ) {
if ( netdev_settings ( netdev ) == lldpset->settings.parent )
return lldpset;
}
return NULL;
}
/**
* Check applicability of LLDP setting
*
* @v settings Settings block
* @v setting Setting to fetch
* @ret applies Setting applies within this settings block
*/
static int lldp_applies ( struct settings *settings __unused,
const struct setting *setting ) {
return ( setting->scope == &lldp_settings_scope );
}
/**
* Fetch value of LLDP setting
*
* @v settings Settings block
* @v setting Setting to fetch
* @v buf Buffer to fill with setting data
* @v len Length of buffer
* @ret len Length of setting data, or negative error
*/
static int lldp_fetch ( struct settings *settings,
struct setting *setting,
void *buf, size_t len ) {
struct lldp_settings *lldpset =
container_of ( settings, struct lldp_settings, settings );
union {
uint32_t high;
uint8_t raw[4];
} tag_prefix;
uint32_t tag_low;
uint8_t tag_type;
uint8_t tag_index;
uint8_t tag_offset;
uint8_t tag_length;
const void *match;
const void *data;
size_t match_len;
size_t remaining;
const struct lldp_tlv *tlv;
unsigned int tlv_type_len;
unsigned int tlv_type;
unsigned int tlv_len;
/* Parse setting tag */
tag_prefix.high = htonl ( setting->tag >> 32 );
tag_low = setting->tag;
tag_type = ( tag_low >> 24 );
tag_index = ( tag_low >> 16 );
tag_offset = ( tag_low >> 8 );
tag_length = ( tag_low >> 0 );
/* Identify match prefix */
match_len = tag_offset;
if ( match_len > sizeof ( tag_prefix ) )
match_len = sizeof ( tag_prefix );
if ( ! tag_prefix.high )
match_len = 0;
match = &tag_prefix.raw[ sizeof ( tag_prefix ) - match_len ];
/* Locate matching TLV */
for ( data = lldpset->data, remaining = lldpset->len ; remaining ;
data += tlv_len, remaining -= tlv_len ) {
/* Parse TLV header */
if ( remaining < sizeof ( *tlv ) ) {
DBGC ( lldpset, "LLDP %s underlength TLV header\n",
lldpset->name );
DBGC_HDA ( lldpset, 0, data, remaining );
break;
}
tlv = data;
data += sizeof ( *tlv );
remaining -= sizeof ( *tlv );
tlv_type_len = ntohs ( tlv->type_len );
tlv_type = LLDP_TLV_TYPE ( tlv_type_len );
if ( tlv_type == LLDP_TYPE_END )
break;
tlv_len = LLDP_TLV_LEN ( tlv_type_len );
if ( remaining < tlv_len ) {
DBGC ( lldpset, "LLDP %s underlength TLV value\n",
lldpset->name );
DBGC_HDA ( lldpset, 0, data, remaining );
break;
}
DBGC2 ( lldpset, "LLDP %s found type %d:\n",
lldpset->name, tlv_type );
DBGC2_HDA ( lldpset, 0, data, tlv_len );
/* Check for matching tag type */
if ( tlv_type != tag_type )
continue;
/* Check for matching prefix */
if ( tlv_len < match_len )
continue;
if ( memcmp ( data, match, match_len ) != 0 )
continue;
/* Check for matching index */
if ( tag_index-- )
continue;
/* Skip offset */
if ( tlv_len < tag_offset )
return 0;
data += tag_offset;
tlv_len -= tag_offset;
/* Set type if not already specified */
if ( ! setting->type ) {
setting->type = ( tag_length ? &setting_type_hex :
&setting_type_string );
}
/* Extract value */
if ( tag_length && ( tlv_len > tag_length ) )
tlv_len = tag_length;
if ( len > tlv_len )
len = tlv_len;
memcpy ( buf, data, len );
return tlv_len;
}
return -ENOENT;
}
/** LLDP settings operations */
static struct settings_operations lldp_settings_operations = {
.applies = lldp_applies,
.fetch = lldp_fetch,
};
/**
* Process LLDP packet
*
* @v iobuf I/O buffer
* @v netdev Network device
* @v ll_dest Link-layer destination address
* @v ll_source Link-layer source address
* @v flags Packet flags
* @ret rc Return status code
*/
static int lldp_rx ( struct io_buffer *iobuf, struct net_device *netdev,
const void *ll_dest, const void *ll_source,
unsigned int flags __unused ) {
struct lldp_settings *lldpset;
size_t len;
void *data;
int rc;
/* Find matching LLDP settings block */
lldpset = lldp_find ( netdev );
if ( ! lldpset ) {
DBGC ( netdev, "LLDP %s has no \"%s\" settings block\n",
netdev->name, LLDP_SETTINGS_NAME );
rc = -ENOENT;
goto err_find;
}
/* Create trimmed copy of received LLDP data */
len = iob_len ( iobuf );
data = malloc ( len );
if ( ! data ) {
rc = -ENOMEM;
goto err_alloc;
}
memcpy ( data, iobuf->data, len );
/* Free any existing LLDP data */
free ( lldpset->data );
/* Transfer data to LLDP settings block */
lldpset->data = data;
lldpset->len = len;
data = NULL;
DBGC2 ( lldpset, "LLDP %s src %s ",
lldpset->name, netdev->ll_protocol->ntoa ( ll_source ) );
DBGC2 ( lldpset, "dst %s\n", netdev->ll_protocol->ntoa ( ll_dest ) );
DBGC2_HDA ( lldpset, 0, lldpset->data, lldpset->len );
/* Success */
rc = 0;
free ( data );
err_alloc:
err_find:
free_iob ( iobuf );
return rc;
}
/** LLDP protocol */
struct net_protocol lldp_protocol __net_protocol = {
.name = "LLDP",
.net_proto = htons ( ETH_P_LLDP ),
.rx = lldp_rx,
};
/**
* Create LLDP settings block
*
* @v netdev Network device
* @ret rc Return status code
*/
static int lldp_probe ( struct net_device *netdev ) {
struct lldp_settings *lldpset;
int rc;
/* Allocate LLDP settings block */
lldpset = zalloc ( sizeof ( *lldpset ) );
if ( ! lldpset ) {
rc = -ENOMEM;
goto err_alloc;
}
ref_init ( &lldpset->refcnt, lldp_free );
settings_init ( &lldpset->settings, &lldp_settings_operations,
&lldpset->refcnt, &lldp_settings_scope );
list_add_tail ( &lldpset->list, &lldp_settings );
lldpset->name = netdev->name;
/* Register settings */
if ( ( rc = register_settings ( &lldpset->settings, netdev_settings ( netdev ),
LLDP_SETTINGS_NAME ) ) != 0 ) {
DBGC ( lldpset, "LLDP %s could not register settings: %s\n",
lldpset->name, strerror ( rc ) );
goto err_register;
}
DBGC ( lldpset, "LLDP %s registered\n", lldpset->name );
ref_put ( &lldpset->refcnt );
return 0;
unregister_settings ( &lldpset->settings );
err_register:
ref_put ( &lldpset->refcnt );
err_alloc:
return rc;
}
/** LLDP driver */
struct net_driver lldp_driver __net_driver = {
.name = "LLDP",
.probe = lldp_probe,
};
|