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