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 <string.h>
34 #include <errno.h>
35 #include <byteswap.h>
36 #include <ipxe/iobuf.h>
37 #include <ipxe/netdevice.h>
38 #include <ipxe/if_ether.h>
39 #include <ipxe/settings.h>
40 #include <ipxe/lldp.h>
41 
42 /** An LLDP settings block */
43 struct lldp_settings {
44  /** Settings interface */
46  /** Name */
47  const char *name;
48  /** LLDP data */
49  void *data;
50  /** Length of LLDP data */
51  size_t len;
52 };
53 
54 /* Forward declaration */
55 struct net_driver lldp_driver __net_driver;
56 
57 /** LLDP settings scope */
58 static const struct settings_scope lldp_settings_scope;
59 
60 /**
61  * Check applicability of LLDP setting
62  *
63  * @v settings Settings block
64  * @v setting Setting to fetch
65  * @ret applies Setting applies within this settings block
66  */
67 static int lldp_applies ( struct settings *settings __unused,
68  const struct setting *setting ) {
69 
70  return ( setting->scope == &lldp_settings_scope );
71 }
72 
73 /**
74  * Fetch value of LLDP setting
75  *
76  * @v settings Settings block
77  * @v setting Setting to fetch
78  * @v buf Buffer to fill with setting data
79  * @v len Length of buffer
80  * @ret len Length of setting data, or negative error
81  */
82 static int lldp_fetch ( struct settings *settings,
83  struct setting *setting,
84  void *buf, size_t len ) {
85  struct lldp_settings *lldpset =
87  union {
88  uint32_t high;
89  uint8_t raw[4];
90  } tag_prefix;
91  uint32_t tag_low;
92  uint8_t tag_type;
93  uint8_t tag_index;
94  uint8_t tag_offset;
95  uint8_t tag_length;
96  const void *match;
97  const void *data;
98  size_t match_len;
99  size_t remaining;
100  const struct lldp_tlv *tlv;
101  unsigned int tlv_type_len;
102  unsigned int tlv_type;
103  unsigned int tlv_len;
104 
105  /* Parse setting tag */
106  tag_prefix.high = htonl ( setting->tag >> 32 );
107  tag_low = setting->tag;
108  tag_type = ( tag_low >> 24 );
109  tag_index = ( tag_low >> 16 );
110  tag_offset = ( tag_low >> 8 );
111  tag_length = ( tag_low >> 0 );
112 
113  /* Identify match prefix */
114  match_len = tag_offset;
115  if ( match_len > sizeof ( tag_prefix ) )
116  match_len = sizeof ( tag_prefix );
117  if ( ! tag_prefix.high )
118  match_len = 0;
119  match = &tag_prefix.raw[ sizeof ( tag_prefix ) - match_len ];
120 
121  /* Locate matching TLV */
122  for ( data = lldpset->data, remaining = lldpset->len ; remaining ;
123  data += tlv_len, remaining -= tlv_len ) {
124 
125  /* Parse TLV header */
126  if ( remaining < sizeof ( *tlv ) ) {
127  DBGC ( lldpset, "LLDP %s underlength TLV header\n",
128  lldpset->name );
129  DBGC_HDA ( lldpset, 0, data, remaining );
130  break;
131  }
132  tlv = data;
133  data += sizeof ( *tlv );
134  remaining -= sizeof ( *tlv );
135  tlv_type_len = ntohs ( tlv->type_len );
136  tlv_type = LLDP_TLV_TYPE ( tlv_type_len );
137  if ( tlv_type == LLDP_TYPE_END )
138  break;
139  tlv_len = LLDP_TLV_LEN ( tlv_type_len );
140  if ( remaining < tlv_len ) {
141  DBGC ( lldpset, "LLDP %s underlength TLV value\n",
142  lldpset->name );
143  DBGC_HDA ( lldpset, 0, data, remaining );
144  break;
145  }
146  DBGC2 ( lldpset, "LLDP %s found type %d:\n",
147  lldpset->name, tlv_type );
148  DBGC2_HDA ( lldpset, 0, data, tlv_len );
149 
150  /* Check for matching tag type */
151  if ( tlv_type != tag_type )
152  continue;
153 
154  /* Check for matching prefix */
155  if ( tlv_len < match_len )
156  continue;
157  if ( memcmp ( data, match, match_len ) != 0 )
158  continue;
159 
160  /* Check for matching index */
161  if ( tag_index-- )
162  continue;
163 
164  /* Skip offset */
165  if ( tlv_len < tag_offset )
166  return 0;
167  data += tag_offset;
168  tlv_len -= tag_offset;
169 
170  /* Set type if not already specified */
171  if ( ! setting->type ) {
172  setting->type = ( tag_length ? &setting_type_hex :
173  &setting_type_string );
174  }
175 
176  /* Extract value */
177  if ( tag_length && ( tlv_len > tag_length ) )
178  tlv_len = tag_length;
179  if ( len > tlv_len )
180  len = tlv_len;
181  memcpy ( buf, data, len );
182  return tlv_len;
183  }
184 
185  return -ENOENT;
186 }
187 
188 /** LLDP settings operations */
191  .fetch = lldp_fetch,
192 };
193 
194 /**
195  * Process LLDP packet
196  *
197  * @v iobuf I/O buffer
198  * @v netdev Network device
199  * @v ll_dest Link-layer destination address
200  * @v ll_source Link-layer source address
201  * @v flags Packet flags
202  * @ret rc Return status code
203  */
204 static int lldp_rx ( struct io_buffer *iobuf, struct net_device *netdev,
205  const void *ll_dest, const void *ll_source,
206  unsigned int flags __unused ) {
207  struct lldp_settings *lldpset;
208  size_t len;
209  void *data;
210  int rc;
211 
212  /* Find matching LLDP settings block */
213  lldpset = netdev_priv ( netdev, &lldp_driver );
214 
215  /* Create trimmed copy of received LLDP data */
216  len = iob_len ( iobuf );
217  data = malloc ( len );
218  if ( ! data ) {
219  rc = -ENOMEM;
220  goto err_alloc;
221  }
222  memcpy ( data, iobuf->data, len );
223 
224  /* Free any existing LLDP data */
225  free ( lldpset->data );
226 
227  /* Transfer data to LLDP settings block */
228  lldpset->data = data;
229  lldpset->len = len;
230  data = NULL;
231  DBGC2 ( lldpset, "LLDP %s src %s ",
232  lldpset->name, netdev->ll_protocol->ntoa ( ll_source ) );
233  DBGC2 ( lldpset, "dst %s\n", netdev->ll_protocol->ntoa ( ll_dest ) );
234  DBGC2_HDA ( lldpset, 0, lldpset->data, lldpset->len );
235 
236  /* Success */
237  rc = 0;
238 
239  free ( data );
240  err_alloc:
241  free_iob ( iobuf );
242  return rc;
243 }
244 
245 /** LLDP protocol */
246 struct net_protocol lldp_protocol __net_protocol = {
247  .name = "LLDP",
248  .net_proto = htons ( ETH_P_LLDP ),
249  .rx = lldp_rx,
250 };
251 
252 /**
253  * Create LLDP settings block
254  *
255  * @v netdev Network device
256  * @v priv Private data
257  * @ret rc Return status code
258  */
259 static int lldp_probe ( struct net_device *netdev, void *priv ) {
260  struct lldp_settings *lldpset = priv;
261  int rc;
262 
263  /* Initialise LLDP settings block */
266  lldpset->name = netdev->name;
267 
268  /* Register settings */
269  if ( ( rc = register_settings ( &lldpset->settings,
271  LLDP_SETTINGS_NAME ) ) != 0 ) {
272  DBGC ( lldpset, "LLDP %s could not register settings: %s\n",
273  lldpset->name, strerror ( rc ) );
274  goto err_register;
275  }
276  DBGC ( lldpset, "LLDP %s registered\n", lldpset->name );
277 
278  return 0;
279 
280  unregister_settings ( &lldpset->settings );
281  err_register:
282  assert ( lldpset->data == NULL );
283  return rc;
284 }
285 
286 /**
287  * Remove LLDP settings block
288  *
289  * @v netdev Network device
290  * @v priv Private data
291  */
292 static void lldp_remove ( struct net_device *netdev __unused, void *priv ) {
293  struct lldp_settings *lldpset = priv;
294 
295  /* Unregister settings */
296  unregister_settings ( &lldpset->settings );
297  DBGC ( lldpset, "LLDP %s unregistered\n", lldpset->name );
298 
299  /* Free any LLDP data */
300  free ( lldpset->data );
301  lldpset->data = NULL;
302 }
303 
304 /** LLDP driver */
305 struct net_driver lldp_driver __net_driver = {
306  .name = "LLDP",
307  .priv_len = sizeof ( struct lldp_settings ),
308  .probe = lldp_probe,
309  .remove = lldp_remove,
310 };
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:51
void * data
LLDP data.
Definition: lldp.c:49
void unregister_settings(struct settings *settings)
Unregister settings block.
Definition: settings.c:514
Error codes.
I/O buffers.
void free_iob(struct io_buffer *iobuf)
Free I/O buffer.
Definition: iobuf.c:152
static int lldp_probe(struct net_device *netdev, void *priv)
Create LLDP settings block.
Definition: lldp.c:259
#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:476
static struct settings * netdev_settings(struct net_device *netdev)
Get per-netdevice configuration settings block.
Definition: netdevice.h:586
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:502
FILE_LICENCE(GPL2_OR_LATER_OR_UBDL)
uint64_t tag
Setting tag, if applicable.
Definition: settings.h:43
#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:478
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:204
static void lldp_remove(struct net_device *netdev __unused, void *priv)
Remove LLDP settings block.
Definition: lldp.c:292
assert((readw(&hdr->flags) &(GTF_reading|GTF_writing))==0)
#define container_of(ptr, type, field)
Get containing structure.
Definition: stddef.h:35
void * netdev_priv(struct net_device *netdev, struct net_driver *driver)
Get network device driver private data.
Definition: netdevice.c:152
#define DBGC_HDA(...)
Definition: compiler.h:506
#define __unused
Declare a variable or data structure as unused.
Definition: compiler.h:573
#define LLDP_TYPE_END
End of LLDP data unit.
Definition: lldp.h:39
ring len
Length.
Definition: dwmac.h:231
static struct net_device * netdev
Definition: gdbudp.c:52
const char * name
Name.
Definition: lldp.c:47
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:82
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:45
uint8_t flags
Flags.
Definition: ena.h:18
char * strerror(int errno)
Retrieve string representation of error number.
Definition: strerror.c:78
struct refcnt refcnt
Reference counter.
Definition: netdevice.h:354
static void(* free)(struct refcnt *refcnt))
Definition: refcnt.h:54
static size_t iob_len(struct io_buffer *iobuf)
Calculate length of data in an I/O buffer.
Definition: iobuf.h:159
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:620
Definition: bnxt_hsi.h:52
An LLDP settings block.
Definition: lldp.c:43
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:67
A network-layer protocol.
Definition: netdevice.h:64
Network device management.
char name[NETDEV_NAME_LEN]
Name of this network device.
Definition: netdevice.h:362
#define DBGC2(...)
Definition: compiler.h:522
static struct tlan_private * priv
Definition: tlan.c:225
void * data
Start of data.
Definition: iobuf.h:52
#define LLDP_TLV_TYPE(type_len)
Extract LLDP TLV type.
Definition: lldp.h:28
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
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
String functions.
#define htons(value)
Definition: byteswap.h:135
struct net_protocol lldp_protocol __net_protocol
LLDP protocol.
Definition: lldp.c:246
struct ll_protocol * ll_protocol
Link-layer protocol.
Definition: netdevice.h:372
static struct settings_operations lldp_settings_operations
LLDP settings operations.
Definition: lldp.c:189
struct net_driver lldp_driver __net_driver
LLDP driver.
Definition: lldp.c:55
A persistent I/O buffer.
Definition: iobuf.h:37