pf_lldp.c 57 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037
  1. /*********************************************************************
  2. * _ _ _
  3. * _ __ | |_ _ | | __ _ | |__ ___
  4. * | '__|| __|(_)| | / _` || '_ \ / __|
  5. * | | | |_ _ | || (_| || |_) |\__ \
  6. * |_| \__|(_)|_| \__,_||_.__/ |___/
  7. *
  8. * www.rt-labs.com
  9. * Copyright 2018 rt-labs AB, Sweden.
  10. *
  11. * This software is dual-licensed under GPLv3 and a commercial
  12. * license. See the file LICENSE.md distributed with this software for
  13. * full license information.
  14. ********************************************************************/
  15. #ifdef UNIT_TEST
  16. #define pnal_get_system_uptime_10ms mock_pnal_get_system_uptime_10ms
  17. #define pnal_get_interface_index mock_pnal_get_interface_index
  18. #endif
  19. #define STRINGIFY(s) STRINGIFIED (s)
  20. #define STRINGIFIED(s) #s
  21. /**
  22. * @file
  23. * @brief Implements Link Layer Discovery Protocol (LLDP), for neighbourhood
  24. * detection.
  25. *
  26. * Builds and sends LLDP frames.
  27. * Receives and parses LLDP frames.
  28. *
  29. * This file should not have any knowledge of Profinet slots, subslots etc.
  30. *
  31. * ToDo: Differentiate between device and port MAC addresses.
  32. */
  33. #include "pf_includes.h"
  34. #include "pf_block_writer.h"
  35. #include "pf_block_reader.h"
  36. #include "pf_cmrpc.h"
  37. #include <string.h>
  38. #include <ctype.h>
  39. #if PNET_MAX_PORT_DESCRIPTION_SIZE > 256
  40. #error "Port description can't be larger than 255 bytes plus termination"
  41. #endif
  42. #if PNET_MAX_PORT_DESCRIPTION_SIZE < PNET_INTERFACE_NAME_MAX_SIZE
  43. #error "Port description should be at least as large as interface name"
  44. #endif
  45. #define LLDP_TYPE_END 0
  46. #define LLDP_TYPE_CHASSIS_ID 1
  47. #define LLDP_TYPE_PORT_ID 2
  48. #define LLDP_TYPE_TTL 3
  49. #define LLDP_TYPE_PORT_DESCRIPTION 4
  50. #define LLDP_TYPE_MANAGEMENT_ADDRESS 8
  51. #define LLDP_TYPE_ORG_SPEC 127
  52. #define LLDP_IEEE_SUBTYPE_MAC_PHY 1
  53. #define LLDP_TYPE_MASK 0xFE00
  54. #define LLDP_TYPE_SHIFT 0x9
  55. #define LLDP_LENGTH_MASK 0x1FF
  56. #define LLDP_MAX_TLV 512
  57. #define LLDP_PEER_MAX_TTL_IN_SECS \
  58. 60 /* Profinet peers should have TTL = 20 seconds. Use some margin. */
  59. #define LLDP_PNIO_SUBTYPE_MEAS_DELAY_VALUES_TLV_LEN 24
  60. #define LLDP_PNIO_SUBTYPE_PORT_STATUS_TLV_LEN 8
  61. #define LLDP_PNIO_SUBTYPE_CHASSIS_MAC_TLV_LEN 10
  62. #define LLDP_IEEE_SUBTYPE_MAC_PHY_TLV_LEN 9
  63. /* Autonegotiation status */
  64. #define LLDP_AUTONEG_SUPPORTED BIT (0)
  65. #define LLDP_AUTONEG_ENABLED BIT (1)
  66. typedef struct lldp_tlv
  67. {
  68. uint8_t type;
  69. uint16_t len;
  70. const uint8_t * p_data;
  71. } lldp_tlv_t;
  72. /** Header for Organizationally Specific TLV */
  73. typedef struct pf_lldp_org_header
  74. {
  75. /** Organizationally unique identifier */
  76. uint8_t id[3];
  77. /** Organizationally defined subtype */
  78. uint8_t subtype;
  79. } pf_lldp_org_header_t;
  80. static const char org_id_pnio[] = {0x00, 0x0e, 0xcf};
  81. static const char org_id_ieee_8023[] = {0x00, 0x12, 0x0f};
  82. typedef enum lldp_pnio_subtype_values
  83. {
  84. LLDP_PNIO_SUBTYPE_RESERVED = 0,
  85. LLDP_PNIO_SUBTYPE_MEAS_DELAY_VALUES = 1,
  86. LLDP_PNIO_SUBTYPE_PORT_STATUS = 2,
  87. LLDP_PNIO_SUBTYPE_PORT_ALIAS = 3,
  88. LLDP_PNIO_SUBTYPE_MRP_RING_PORT_STATUS = 4,
  89. LLDP_PNIO_SUBTYPE_CHASSIS_MAC = 5,
  90. LLDP_PNIO_SUBTYPE_PTCP_STATUS = 6,
  91. LLDP_PNIO_SUBTYPE_MAU_TYPE_EXTENSION = 7,
  92. LLDP_PNIO_SUBTYPE_MRP_INTERCONNECTION_PORT_STATUS = 8,
  93. /* 0x09..0xff reserved */
  94. } lldp_pnio_subtype_values_t;
  95. static const pnet_ethaddr_t lldp_dst_addr = {
  96. {0x01, 0x80, 0xc2, 0x00, 0x00, 0x0e} /* LLDP Multicast */
  97. };
  98. /******************* Insert data into buffer ********************************/
  99. /**
  100. * @internal
  101. * Insert header of a TLV field into a buffer.
  102. * Each TLV is structured as follows:
  103. * - Type = 7 bits
  104. * - Length = 9 bits
  105. * - data = 0-511 bytes
  106. *
  107. * This is for the type and the payload length.
  108. *
  109. * @param p_buf InOut: The buffer.
  110. * @param p_pos InOut: The buffer position.
  111. * @param typ In: The TLV header type.
  112. * @param len In: The TLV payload length.
  113. */
  114. static inline void pf_lldp_add_tlv_header (
  115. uint8_t * p_buf,
  116. uint16_t * p_pos,
  117. uint8_t typ,
  118. uint8_t len)
  119. {
  120. uint16_t tlv_header = ((typ) << LLDP_TYPE_SHIFT) + ((len)&LLDP_LENGTH_MASK);
  121. pf_put_uint16 (true, tlv_header, PF_FRAME_BUFFER_SIZE, p_buf, p_pos);
  122. }
  123. /**
  124. * @internal
  125. * Parse LLDP TLV header.
  126. * Each TLV is structured as follows:
  127. * - Type = 7 bits
  128. * - Length = 9 bits
  129. * - data = 0-511 bytes
  130. *
  131. * @param p_info InOut: Parser state
  132. * @param p_pos InOut: Offset in parsed buffer
  133. * @return parsed frame. If parsing fails, frame type is set to LLDP_TYPE_END
  134. * and len field set to 0.
  135. */
  136. lldp_tlv_t pf_lldp_get_tlv_from_packet (pf_get_info_t * p_info, uint16_t * p_pos)
  137. {
  138. lldp_tlv_t tlv;
  139. uint16_t tlv_head = pf_get_uint16 (p_info, p_pos);
  140. if (p_info->result == PF_PARSE_OK)
  141. {
  142. tlv.type = (tlv_head & LLDP_TYPE_MASK) >> LLDP_TYPE_SHIFT;
  143. tlv.len = (tlv_head & LLDP_LENGTH_MASK);
  144. tlv.p_data = &p_info->p_buf[*p_pos];
  145. }
  146. else
  147. {
  148. tlv.type = LLDP_TYPE_END;
  149. tlv.len = 0;
  150. tlv.p_data = NULL;
  151. }
  152. return tlv;
  153. }
  154. /**
  155. * Insert Ethernet header into a buffer.
  156. *
  157. * @param p_buf InOut: The buffer.
  158. * @param p_pos InOut: The buffer position.
  159. * @param dst_addr In: Destination MAC address.
  160. * @param src_addr In: Source MAC address.
  161. */
  162. static void pf_lldp_add_ethernet_header (
  163. uint8_t * p_buf,
  164. uint16_t * p_pos,
  165. pnet_ethaddr_t dst_addr,
  166. pnet_ethaddr_t src_addr)
  167. {
  168. pf_put_mem (
  169. dst_addr.addr,
  170. sizeof (dst_addr.addr),
  171. PF_FRAME_BUFFER_SIZE,
  172. p_buf,
  173. p_pos);
  174. pf_put_mem (
  175. src_addr.addr,
  176. sizeof (src_addr.addr),
  177. PF_FRAME_BUFFER_SIZE,
  178. p_buf,
  179. p_pos);
  180. pf_put_uint16 (true, PNAL_ETHTYPE_LLDP, PF_FRAME_BUFFER_SIZE, p_buf, p_pos);
  181. }
  182. /**
  183. * @internal
  184. * Insert a Profinet-specific header for a TLV field into a buffer.
  185. *
  186. * This inserts a TLV header with type="organisation-specific", and
  187. * the Profinet organisation identifier as the first part of the TLV payload.
  188. *
  189. * @param p_buf InOut: The buffer.
  190. * @param p_pos InOut: The buffer position.
  191. * @param len In: The TLV payload length (for the part after the
  192. * organisation identifier)
  193. */
  194. static inline void pf_lldp_add_pnio_header (
  195. uint8_t * p_buf,
  196. uint16_t * p_pos,
  197. uint8_t len)
  198. {
  199. pf_lldp_add_tlv_header (p_buf, p_pos, LLDP_TYPE_ORG_SPEC, (len) + 3);
  200. pf_put_byte (0x00, PF_FRAME_BUFFER_SIZE, p_buf, p_pos);
  201. pf_put_byte (0x0e, PF_FRAME_BUFFER_SIZE, p_buf, p_pos);
  202. pf_put_byte (0xcf, PF_FRAME_BUFFER_SIZE, p_buf, p_pos);
  203. }
  204. /**
  205. * @internal
  206. * Insert a IEEE 802.3-specific header for a TLV field into a buffer.
  207. *
  208. * This inserts a TLV header with type="organisation-specific", and
  209. * the IEEE 802.3 organisation identifier as the first part of the TLV payload.
  210. *
  211. * @param p_buf InOut: The buffer.
  212. * @param p_pos InOut: The buffer position.
  213. * @param len In: The TLV payload length (for the part after the
  214. * organisation identifier)
  215. */
  216. static inline void pf_lldp_add_ieee_header (
  217. uint8_t * p_buf,
  218. uint16_t * p_pos,
  219. uint8_t len)
  220. {
  221. pf_lldp_add_tlv_header (p_buf, p_pos, LLDP_TYPE_ORG_SPEC, (len) + 3);
  222. pf_put_byte (0x00, PF_FRAME_BUFFER_SIZE, p_buf, p_pos);
  223. pf_put_byte (0x12, PF_FRAME_BUFFER_SIZE, p_buf, p_pos);
  224. pf_put_byte (0x0f, PF_FRAME_BUFFER_SIZE, p_buf, p_pos);
  225. }
  226. /**
  227. * @internal
  228. * Insert the mandatory chassis_id TLV into a buffer.
  229. *
  230. * @param p_chassis_id In: The Chassis ID.
  231. * @param p_buf InOut: The buffer.
  232. * @param p_pos InOut: The position in the buffer.
  233. */
  234. static void pf_lldp_add_chassis_id_tlv (
  235. const pf_lldp_chassis_id_t * p_chassis_id,
  236. uint8_t * p_buf,
  237. uint16_t * p_pos)
  238. {
  239. pf_lldp_add_tlv_header (
  240. p_buf,
  241. p_pos,
  242. LLDP_TYPE_CHASSIS_ID,
  243. 1 + p_chassis_id->len);
  244. pf_put_byte (p_chassis_id->subtype, PF_FRAME_BUFFER_SIZE, p_buf, p_pos);
  245. pf_put_mem (
  246. p_chassis_id->string,
  247. p_chassis_id->len,
  248. PF_FRAME_BUFFER_SIZE,
  249. p_buf,
  250. p_pos);
  251. }
  252. /**
  253. * @internal
  254. * Insert the mandatory port_id TLV into a buffer.
  255. * @param p_port_id In: Port Id
  256. * @param p_buf InOut: The buffer.
  257. * @param p_pos InOut: The position in the buffer.
  258. */
  259. static void pf_lldp_add_port_id_tlv (
  260. const pf_lldp_port_id_t * p_port_id,
  261. uint8_t * p_buf,
  262. uint16_t * p_pos)
  263. {
  264. pf_lldp_add_tlv_header (p_buf, p_pos, LLDP_TYPE_PORT_ID, 1 + p_port_id->len);
  265. pf_put_byte (p_port_id->subtype, PF_FRAME_BUFFER_SIZE, p_buf, p_pos);
  266. pf_put_mem (
  267. p_port_id->string,
  268. p_port_id->len,
  269. PF_FRAME_BUFFER_SIZE,
  270. p_buf,
  271. p_pos);
  272. }
  273. /**
  274. * @internal
  275. * Insert the mandatory time-to-live (TTL) TLV into a buffer.
  276. * @param p_buf InOut: The buffer.
  277. * @param p_pos InOut: The position in the buffer.
  278. */
  279. static void pf_lldp_add_ttl_tlv (uint8_t * p_buf, uint16_t * p_pos)
  280. {
  281. pf_lldp_add_tlv_header (p_buf, p_pos, LLDP_TYPE_TTL, 2);
  282. pf_put_uint16 (true, PF_LLDP_TTL, PF_FRAME_BUFFER_SIZE, p_buf, p_pos);
  283. }
  284. /**
  285. * @internal
  286. * Insert the optional Profinet port status TLV into a buffer.
  287. *
  288. * The port status TLV is mandatory for ProfiNet.
  289. * @param rtclass_2_status In: Use 0. Not supported.
  290. * @param rtclass_3_status In: Use 0. Not supported.
  291. * @param p_buf InOut: The buffer.
  292. * @param p_pos InOut: The position in the buffer.
  293. */
  294. static void pf_lldp_add_port_status (
  295. uint16_t rtclass_2_status,
  296. uint16_t rtclass_3_status,
  297. uint8_t * p_buf,
  298. uint16_t * p_pos)
  299. {
  300. pf_lldp_add_pnio_header (p_buf, p_pos, 5);
  301. pf_put_byte (
  302. LLDP_PNIO_SUBTYPE_PORT_STATUS,
  303. PF_FRAME_BUFFER_SIZE,
  304. p_buf,
  305. p_pos);
  306. pf_put_uint16 (true, rtclass_2_status, PF_FRAME_BUFFER_SIZE, p_buf, p_pos);
  307. pf_put_uint16 (true, rtclass_3_status, PF_FRAME_BUFFER_SIZE, p_buf, p_pos);
  308. }
  309. /**
  310. * @internal
  311. * Insert the optional Profinet chassis MAC TLV into a buffer.
  312. *
  313. * The chassis MAC TLV is mandatory for ProfiNet.
  314. * @param mac_address In: Device MAC address.
  315. * @param p_buf InOut: The buffer.
  316. * @param p_pos InOut: The position in the buffer.
  317. */
  318. static void pf_lldp_add_chassis_mac (
  319. const pnet_ethaddr_t * mac_address,
  320. uint8_t * p_buf,
  321. uint16_t * p_pos)
  322. {
  323. pf_lldp_add_pnio_header (p_buf, p_pos, 1 + sizeof (pnet_ethaddr_t));
  324. pf_put_byte (
  325. LLDP_PNIO_SUBTYPE_CHASSIS_MAC,
  326. PF_FRAME_BUFFER_SIZE,
  327. p_buf,
  328. p_pos);
  329. memcpy (&p_buf[*p_pos], mac_address->addr, sizeof (pnet_ethaddr_t));
  330. (*p_pos) += sizeof (pnet_ethaddr_t);
  331. }
  332. /**
  333. * @internal
  334. * Insert the optional IEEE 802.3 MAC TLV into a buffer.
  335. *
  336. * This is the autonegotiation capabilities and available speeds, and cable MAU
  337. * type.
  338. *
  339. * The IEEE 802.3 MAC TLV is mandatory for ProfiNet on 803.2 interfaces.
  340. * @param p_status In: Link status for this port.
  341. * @param p_buf InOut: The buffer.
  342. * @param p_pos InOut: The position in the buffer.
  343. */
  344. static void pf_lldp_add_ieee_mac_phy (
  345. const pf_lldp_link_status_t * p_status,
  346. uint8_t * p_buf,
  347. uint16_t * p_pos)
  348. {
  349. uint8_t autoneg_status;
  350. autoneg_status = 0x00;
  351. if (p_status->is_autonegotiation_supported)
  352. {
  353. autoneg_status |= LLDP_AUTONEG_SUPPORTED;
  354. }
  355. if (p_status->is_autonegotiation_enabled)
  356. {
  357. autoneg_status |= LLDP_AUTONEG_ENABLED;
  358. }
  359. pf_lldp_add_ieee_header (p_buf, p_pos, 6);
  360. pf_put_byte (LLDP_IEEE_SUBTYPE_MAC_PHY, PF_FRAME_BUFFER_SIZE, p_buf, p_pos);
  361. pf_put_byte (autoneg_status, PF_FRAME_BUFFER_SIZE, p_buf, p_pos);
  362. pf_put_uint16 (
  363. true,
  364. p_status->autonegotiation_advertised_capabilities,
  365. PF_FRAME_BUFFER_SIZE,
  366. p_buf,
  367. p_pos);
  368. pf_put_uint16 (
  369. true,
  370. p_status->operational_mau_type,
  371. PF_FRAME_BUFFER_SIZE,
  372. p_buf,
  373. p_pos);
  374. }
  375. /**
  376. * Insert the optional management data TLV into a buffer.
  377. * It is mandatory for ProfiNet.
  378. *
  379. * Contains the IP address as well as information about the
  380. * management interface.
  381. *
  382. * @param p_man_address In: Management address
  383. * @param p_buf InOut: The buffer.
  384. * @param p_pos InOut: The position in the buffer.
  385. */
  386. static void pf_lldp_add_management (
  387. const pf_lldp_management_address_t * p_man_address,
  388. uint8_t * p_buf,
  389. uint16_t * p_pos)
  390. {
  391. pf_lldp_add_tlv_header (p_buf, p_pos, LLDP_TYPE_MANAGEMENT_ADDRESS, 12);
  392. /* Management address length, type and value */
  393. pf_put_byte (1 + p_man_address->len, PF_FRAME_BUFFER_SIZE, p_buf, p_pos);
  394. pf_put_byte (p_man_address->subtype, PF_FRAME_BUFFER_SIZE, p_buf, p_pos);
  395. pf_put_mem (
  396. p_man_address->value,
  397. p_man_address->len,
  398. PF_FRAME_BUFFER_SIZE,
  399. p_buf,
  400. p_pos);
  401. /* Interface number for management interface */
  402. pf_put_byte (
  403. p_man_address->interface_number.subtype,
  404. PF_FRAME_BUFFER_SIZE,
  405. p_buf,
  406. p_pos);
  407. pf_put_uint32 (
  408. true,
  409. p_man_address->interface_number.value,
  410. PF_FRAME_BUFFER_SIZE,
  411. p_buf,
  412. p_pos);
  413. /* Management OID. Not supported */
  414. pf_put_byte (0, PF_FRAME_BUFFER_SIZE, p_buf, p_pos);
  415. }
  416. void pf_lldp_invalidate_peer_info (pnet_t * net, int loc_port_num)
  417. {
  418. pf_port_t * p_port_data = pf_port_get_state (net, loc_port_num);
  419. os_mutex_lock (net->lldp_mutex);
  420. p_port_data->lldp.is_peer_info_received = false;
  421. p_port_data->lldp.peer_info.chassis_id.is_valid = false;
  422. p_port_data->lldp.peer_info.port_id.is_valid = false;
  423. p_port_data->lldp.peer_info.port_description.is_valid = false;
  424. p_port_data->lldp.peer_info.management_address.is_valid = false;
  425. p_port_data->lldp.peer_info.port_delay.is_valid = false;
  426. p_port_data->lldp.peer_info.phy_config.is_valid = false;
  427. p_port_data->lldp.peer_info.ttl = 0;
  428. os_mutex_unlock (net->lldp_mutex);
  429. }
  430. /********************* Initialize and send **********************************/
  431. /**
  432. * @internal
  433. * Handle LLDP peer timeout. Timeout occurs if no peer LLDP data is received
  434. * on port within the LLPD TTL timeout interval.
  435. *
  436. * This is a callback for the scheduler. Arguments should fulfill
  437. * pf_scheduler_timeout_ftn_t
  438. *
  439. * @param net InOut: The p-net stack instance
  440. * @param arg In: Port instance, p_port_data
  441. * @param current_time In: Not used
  442. */
  443. static void pf_lldp_receive_timeout (
  444. pnet_t * net,
  445. void * arg,
  446. uint32_t current_time)
  447. {
  448. pf_port_t * p_port_data = (pf_port_t *)arg;
  449. pf_scheduler_reset_handle (&p_port_data->lldp.rx_timeout);
  450. LOG_INFO (
  451. PF_LLDP_LOG,
  452. "LLDP(%d): Port %d, receive timeout expired\n",
  453. __LINE__,
  454. p_port_data->port_num);
  455. pf_lldp_invalidate_peer_info (net, p_port_data->port_num);
  456. pf_pdport_peer_indication (net, p_port_data->port_num);
  457. }
  458. void pf_lldp_restart_peer_timeout (
  459. pnet_t * net,
  460. int loc_port_num,
  461. uint16_t timeout_in_secs)
  462. {
  463. pf_port_t * p_port_data = pf_port_get_state (net, loc_port_num);
  464. pf_scheduler_remove_if_running (net, &p_port_data->lldp.rx_timeout);
  465. /*
  466. * Profinet states that the time to live shall be 20 seconds,
  467. * See IEC61158-6-10 6.3.13.2.2
  468. *
  469. * To handle situations where ttl is longer than 20s and timeout gets
  470. * longer than scheduler can handle, only start receive timeout if
  471. * ttl is within configured max ttl.
  472. * Timeout will always be started if a profinet enabled switch
  473. * with 20s LLDP peer ttl is used.
  474. */
  475. if (timeout_in_secs > LLDP_PEER_MAX_TTL_IN_SECS)
  476. {
  477. return;
  478. }
  479. if (
  480. pf_scheduler_add (
  481. net,
  482. timeout_in_secs * 1000000,
  483. pf_lldp_receive_timeout,
  484. p_port_data,
  485. &p_port_data->lldp.rx_timeout) != 0)
  486. {
  487. LOG_ERROR (
  488. PF_LLDP_LOG,
  489. "LLDP(%d): Failed to add LLDP timeout\n",
  490. __LINE__);
  491. }
  492. }
  493. void pf_lldp_stop_peer_timeout (pnet_t * net, int loc_port_num)
  494. {
  495. pf_port_t * p_port_data = pf_port_get_state (net, loc_port_num);
  496. pf_scheduler_remove_if_running (net, &p_port_data->lldp.rx_timeout);
  497. }
  498. /**
  499. * @internal
  500. * Check if management address is of type IPv4
  501. *
  502. * @param address In: Management address.
  503. * @return true if address is a valid IPv4 address,
  504. * false if not.
  505. */
  506. static bool pf_lldp_management_address_is_ipv4 (
  507. const pf_lldp_management_address_t * address)
  508. {
  509. return address->is_valid && address->subtype == 1 && address->len == 4;
  510. }
  511. int pf_lldp_get_peer_timestamp (
  512. pnet_t * net,
  513. int loc_port_num,
  514. uint32_t * timestamp_10ms)
  515. {
  516. bool is_received;
  517. const pf_port_t * p_port_data = pf_port_get_state (net, loc_port_num);
  518. os_mutex_lock (net->lldp_mutex);
  519. *timestamp_10ms = p_port_data->lldp.timestamp_for_last_peer_change;
  520. is_received = p_port_data->lldp.is_peer_info_received;
  521. os_mutex_unlock (net->lldp_mutex);
  522. return is_received ? 0 : -1;
  523. }
  524. void pf_lldp_get_chassis_id (pnet_t * net, pf_lldp_chassis_id_t * p_chassis_id)
  525. {
  526. const pnet_ethaddr_t * device_mac_address =
  527. pf_cmina_get_device_macaddr (net);
  528. char station_name[PNET_STATION_NAME_MAX_SIZE]; /** Terminated */
  529. if (
  530. net->pf_interface.name_of_device_mode.mode ==
  531. PF_LLDP_NAME_OF_DEVICE_MODE_LEGACY)
  532. {
  533. /*
  534. * FIXME: Use of pf_cmina_get_station_name() is not thread-safe.
  535. * Fix this, e.g. using a mutex.
  536. */
  537. pf_cmina_get_station_name (net, station_name);
  538. p_chassis_id->len = strlen (station_name);
  539. if (
  540. p_chassis_id->len == 0 ||
  541. p_chassis_id->len >= PNET_LLDP_CHASSIS_ID_MAX_SIZE)
  542. {
  543. /* Use the device MAC address */
  544. p_chassis_id->subtype = PF_LLDP_SUBTYPE_MAC;
  545. p_chassis_id->len = sizeof (pnet_ethaddr_t);
  546. memcpy (
  547. p_chassis_id->string,
  548. device_mac_address->addr,
  549. sizeof (pnet_ethaddr_t));
  550. }
  551. else
  552. {
  553. /* Use the station name (NameOfStation/NameOfInterface) */
  554. p_chassis_id->subtype = PF_LLDP_SUBTYPE_LOCALLY_ASSIGNED;
  555. memcpy (p_chassis_id->string, station_name, p_chassis_id->len);
  556. }
  557. }
  558. else /* PF_LLDP_NAME_OF_DEVICE_MODE_STANDARD */
  559. {
  560. p_chassis_id->subtype = PF_LLDP_SUBTYPE_LOCALLY_ASSIGNED;
  561. p_chassis_id->len = pf_lldp_get_system_description (
  562. net,
  563. p_chassis_id->string,
  564. sizeof (p_chassis_id->string));
  565. }
  566. p_chassis_id->string[p_chassis_id->len] = '\0';
  567. p_chassis_id->is_valid = true;
  568. }
  569. int pf_lldp_get_peer_chassis_id (
  570. pnet_t * net,
  571. int loc_port_num,
  572. pf_lldp_chassis_id_t * p_chassis_id)
  573. {
  574. const pf_port_t * p_port_data = pf_port_get_state (net, loc_port_num);
  575. os_mutex_lock (net->lldp_mutex);
  576. *p_chassis_id = p_port_data->lldp.peer_info.chassis_id;
  577. os_mutex_unlock (net->lldp_mutex);
  578. return p_chassis_id->is_valid ? 0 : -1;
  579. }
  580. void pf_lldp_get_port_id (
  581. pnet_t * net,
  582. int loc_port_num,
  583. pf_lldp_port_id_t * p_port_id)
  584. {
  585. const pf_port_t * p_port_data = pf_port_get_state (net, loc_port_num);
  586. char station_name[PNET_STATION_NAME_MAX_SIZE]; /** Terminated */
  587. const pnet_ethaddr_t * device_mac_address =
  588. pf_cmina_get_device_macaddr (net);
  589. if (
  590. net->pf_interface.name_of_device_mode.mode ==
  591. PF_LLDP_NAME_OF_DEVICE_MODE_LEGACY)
  592. {
  593. snprintf (
  594. p_port_id->string,
  595. sizeof (p_port_id->string),
  596. "%s",
  597. p_port_data->port_name);
  598. }
  599. else /* PF_LLDP_NAME_OF_DEVICE_MODE_STANDARD */
  600. {
  601. pf_cmina_get_station_name (net, station_name);
  602. if (strlen (station_name) == 0)
  603. {
  604. /* Profinet 2.4 Protocol, table 335 "LLDP substitutions":
  605. Use chassis MAC address if no NameOfStation is assigned */
  606. pf_lldp_mac_address_to_string (
  607. device_mac_address->addr,
  608. station_name,
  609. sizeof (station_name));
  610. }
  611. snprintf (
  612. p_port_id->string,
  613. sizeof (p_port_id->string),
  614. "%s.%s",
  615. p_port_data->port_name,
  616. station_name);
  617. }
  618. p_port_id->subtype = PF_LLDP_SUBTYPE_LOCALLY_ASSIGNED;
  619. p_port_id->len = strlen (p_port_id->string);
  620. p_port_id->is_valid = true;
  621. }
  622. int pf_lldp_get_peer_port_id (
  623. pnet_t * net,
  624. int loc_port_num,
  625. pf_lldp_port_id_t * p_port_id)
  626. {
  627. const pf_port_t * p_port_data = pf_port_get_state (net, loc_port_num);
  628. os_mutex_lock (net->lldp_mutex);
  629. *p_port_id = p_port_data->lldp.peer_info.port_id;
  630. os_mutex_unlock (net->lldp_mutex);
  631. return p_port_id->is_valid ? 0 : -1;
  632. }
  633. void pf_lldp_get_port_description (
  634. pnet_t * net,
  635. int loc_port_num,
  636. pf_lldp_port_description_t * p_port_descr)
  637. {
  638. const pf_port_t * p_port_data = pf_port_get_state (net, loc_port_num);
  639. snprintf (
  640. p_port_descr->string,
  641. sizeof (p_port_descr->string),
  642. "%s",
  643. p_port_data->netif.name);
  644. p_port_descr->len = strlen (p_port_descr->string);
  645. p_port_descr->is_valid = true;
  646. }
  647. int pf_lldp_get_peer_port_description (
  648. pnet_t * net,
  649. int loc_port_num,
  650. pf_lldp_port_description_t * p_port_desc)
  651. {
  652. const pf_port_t * p_port_data = pf_port_get_state (net, loc_port_num);
  653. os_mutex_lock (net->lldp_mutex);
  654. *p_port_desc = p_port_data->lldp.peer_info.port_description;
  655. os_mutex_unlock (net->lldp_mutex);
  656. return p_port_desc->is_valid ? 0 : -1;
  657. }
  658. void pf_lldp_get_management_address (
  659. pnet_t * net,
  660. pf_lldp_management_address_t * p_man_address)
  661. {
  662. pnal_ipaddr_t ipaddr = pf_cmina_get_ipaddr (net);
  663. p_man_address->subtype = 1; /* IPv4 */
  664. ipaddr = CC_TO_BE32 (ipaddr);
  665. memcpy (p_man_address->value, &ipaddr, sizeof (ipaddr));
  666. p_man_address->len = sizeof (ipaddr);
  667. p_man_address->interface_number.subtype = 2; /* ifIndex */
  668. p_man_address->interface_number.value =
  669. pnal_get_interface_index (net->pf_interface.main_port.name);
  670. p_man_address->is_valid = true;
  671. }
  672. int pf_lldp_get_peer_management_address (
  673. pnet_t * net,
  674. int loc_port_num,
  675. pf_lldp_management_address_t * p_man_address)
  676. {
  677. const pf_port_t * p_port_data = pf_port_get_state (net, loc_port_num);
  678. os_mutex_lock (net->lldp_mutex);
  679. *p_man_address = p_port_data->lldp.peer_info.management_address;
  680. os_mutex_unlock (net->lldp_mutex);
  681. return p_man_address->is_valid ? 0 : -1;
  682. }
  683. int pf_lldp_get_peer_station_name (
  684. pnet_t * net,
  685. int loc_port_num,
  686. pf_lldp_station_name_t * p_station_name)
  687. {
  688. pf_lldp_chassis_id_t chassis_id;
  689. pf_lldp_port_id_t port_id;
  690. char * p;
  691. bool found = false;
  692. const pf_port_t * p_port_data = pf_port_get_state (net, loc_port_num);
  693. memset (p_station_name, 0, sizeof (*p_station_name));
  694. os_mutex_lock (net->lldp_mutex);
  695. port_id = p_port_data->lldp.peer_info.port_id;
  696. chassis_id = p_port_data->lldp.peer_info.chassis_id;
  697. os_mutex_unlock (net->lldp_mutex);
  698. if (port_id.is_valid)
  699. {
  700. p = strchr (port_id.string, '.');
  701. if (p != NULL)
  702. {
  703. /* PF_LLDP_NAME_OF_DEVICE_MODE_STANDARD
  704. * Example: port-001.dut
  705. */
  706. snprintf (
  707. p_station_name->string,
  708. sizeof (p_station_name->string),
  709. "%s",
  710. p + 1);
  711. found = true;
  712. }
  713. }
  714. if (!found && chassis_id.is_valid)
  715. {
  716. /* PF_LLDP_NAME_OF_DEVICE_MODE_LEGACY */
  717. if (chassis_id.subtype != PF_LLDP_SUBTYPE_MAC)
  718. {
  719. snprintf (
  720. p_station_name->string,
  721. sizeof (p_station_name->string),
  722. "%s",
  723. chassis_id.string);
  724. }
  725. else
  726. {
  727. pf_lldp_mac_address_to_string (
  728. (uint8_t *)chassis_id.string,
  729. p_station_name->string,
  730. sizeof (p_station_name->string));
  731. }
  732. }
  733. /* Do not use peer mac address here since its status is unknown. */
  734. p_station_name->len = strlen (p_station_name->string);
  735. return p_station_name->len ? 0 : -1;
  736. }
  737. int pf_lldp_get_peer_port_name (
  738. pnet_t * net,
  739. int loc_port_num,
  740. pf_lldp_port_name_t * p_port_name)
  741. {
  742. pf_lldp_port_id_t port_id;
  743. size_t i;
  744. const pf_port_t * p_port_data = pf_port_get_state (net, loc_port_num);
  745. memset (p_port_name, 0, sizeof (*p_port_name));
  746. os_mutex_lock (net->lldp_mutex);
  747. port_id = p_port_data->lldp.peer_info.port_id;
  748. os_mutex_unlock (net->lldp_mutex);
  749. if (!port_id.is_valid)
  750. {
  751. return -1;
  752. }
  753. for (i = 0; (i < port_id.len) && (port_id.string[i] != '\0') &&
  754. (i < (sizeof (p_port_name->string) - 1));
  755. i++)
  756. {
  757. if (port_id.string[i] == '.')
  758. {
  759. /* PF_LLDP_NAME_OF_DEVICE_MODE_STANDARD */
  760. break;
  761. }
  762. p_port_name->string[i] = port_id.string[i];
  763. }
  764. p_port_name->len = strlen (p_port_name->string);
  765. return p_port_name->len ? 0 : -1;
  766. }
  767. void pf_lldp_get_signal_delays (
  768. pnet_t * net,
  769. int loc_port_num,
  770. pf_lldp_signal_delay_t * p_delays)
  771. {
  772. /* Line delay measurement is not supported */
  773. p_delays->cable_delay_local = 0; /* Not measured */
  774. p_delays->rx_delay_local = 0; /* Not measured */
  775. p_delays->tx_delay_local = 0; /* Not measured */
  776. p_delays->rx_delay_remote = 0; /* Not measured */
  777. p_delays->tx_delay_remote = 0; /* Not measured */
  778. p_delays->is_valid = true;
  779. }
  780. int pf_lldp_get_peer_signal_delays (
  781. pnet_t * net,
  782. int loc_port_num,
  783. pf_lldp_signal_delay_t * p_delays)
  784. {
  785. const pf_port_t * p_port_data = pf_port_get_state (net, loc_port_num);
  786. os_mutex_lock (net->lldp_mutex);
  787. *p_delays = p_port_data->lldp.peer_info.port_delay;
  788. os_mutex_unlock (net->lldp_mutex);
  789. return p_delays->is_valid ? 0 : -1;
  790. }
  791. void pf_lldp_get_link_status (
  792. pnet_t * net,
  793. int loc_port_num,
  794. pf_lldp_link_status_t * p_link_status)
  795. {
  796. pnal_eth_status_t status =
  797. pf_pdport_get_eth_status_filtered_mau (net, loc_port_num);
  798. p_link_status->is_autonegotiation_supported =
  799. status.is_autonegotiation_supported;
  800. p_link_status->is_autonegotiation_enabled =
  801. status.is_autonegotiation_enabled;
  802. p_link_status->autonegotiation_advertised_capabilities =
  803. status.autonegotiation_advertised_capabilities;
  804. p_link_status->operational_mau_type = status.operational_mau_type;
  805. p_link_status->is_valid = true;
  806. }
  807. int pf_lldp_get_peer_link_status (
  808. pnet_t * net,
  809. int loc_port_num,
  810. pf_lldp_link_status_t * p_link_status)
  811. {
  812. const pf_port_t * p_port_data = pf_port_get_state (net, loc_port_num);
  813. os_mutex_lock (net->lldp_mutex);
  814. *p_link_status = p_port_data->lldp.peer_info.phy_config;
  815. os_mutex_unlock (net->lldp_mutex);
  816. return p_link_status->is_valid ? 0 : -1;
  817. }
  818. size_t pf_lldp_get_system_description (
  819. const pnet_t * net,
  820. char * system_description,
  821. size_t system_description_max_len)
  822. {
  823. return snprintf (
  824. system_description,
  825. system_description_max_len,
  826. /* clang-format off */
  827. "%-" STRINGIFY (PNET_PRODUCT_NAME_MAX_LEN) "s "
  828. "%-" STRINGIFY (PNET_ORDER_ID_MAX_LEN) "s "
  829. "%-" STRINGIFY (PNET_SERIAL_NUMBER_MAX_LEN) "s "
  830. "%5u "
  831. "%c%3u%3u%3u",
  832. /* clang-format on */
  833. net->fspm_cfg.product_name,
  834. net->fspm_cfg.im_0_data.im_order_id,
  835. net->fspm_cfg.im_0_data.im_serial_number,
  836. net->fspm_cfg.im_0_data.im_hardware_revision,
  837. net->fspm_cfg.im_0_data.im_sw_revision_prefix,
  838. net->fspm_cfg.im_0_data.im_sw_revision_functional_enhancement,
  839. net->fspm_cfg.im_0_data.im_sw_revision_bug_fix,
  840. net->fspm_cfg.im_0_data.im_sw_revision_internal_change);
  841. }
  842. void pf_lldp_mac_address_to_string (
  843. const uint8_t * mac,
  844. char * mac_str,
  845. size_t mac_str_max_len)
  846. {
  847. snprintf (
  848. mac_str,
  849. mac_str_max_len,
  850. "%02X-%02X-%02X-%02X-%02X-%02X",
  851. mac[0],
  852. mac[1],
  853. mac[2],
  854. mac[3],
  855. mac[4],
  856. mac[5]);
  857. }
  858. /**
  859. * Construct Ethernet frame containing LLDP PDU as payload
  860. *
  861. * @param net In: The p-net stack instance.
  862. * @param loc_port_num In: Local port number.
  863. * Valid range: 1 .. num_physical_ports
  864. * @param buf Out: Ethernet frame buffer of size
  865. * PF_FRAME_BUFFER_SIZE bytes.
  866. *
  867. * @return Size of constructed frame, in bytes.
  868. */
  869. size_t pf_lldp_construct_frame (pnet_t * net, int loc_port_num, uint8_t buf[])
  870. {
  871. uint16_t pos;
  872. const pnet_ethaddr_t * device_mac_address =
  873. pf_cmina_get_device_macaddr (net);
  874. pf_lldp_link_status_t link_status;
  875. pf_lldp_chassis_id_t chassis_id;
  876. pf_lldp_port_id_t port_id;
  877. pf_lldp_management_address_t man_address;
  878. const pf_port_t * p_port_data = pf_port_get_state (net, loc_port_num);
  879. #if LOG_DEBUG_ENABLED(PF_LLDP_LOG)
  880. pnal_ipaddr_t ipaddr = pf_cmina_get_ipaddr (net);
  881. char ip_string[PNAL_INET_ADDRSTR_SIZE] = {0}; /** Terminated string */
  882. const char * chassis_id_description = "<MAC address>";
  883. #endif
  884. pf_lldp_get_chassis_id (net, &chassis_id);
  885. pf_lldp_get_port_id (net, loc_port_num, &port_id);
  886. pf_lldp_get_link_status (net, loc_port_num, &link_status);
  887. pf_lldp_get_management_address (net, &man_address);
  888. #if LOG_DEBUG_ENABLED(PF_LLDP_LOG)
  889. pf_cmina_ip_to_string (ipaddr, ip_string);
  890. if (chassis_id.subtype == PF_LLDP_SUBTYPE_LOCALLY_ASSIGNED)
  891. {
  892. chassis_id_description = chassis_id.string;
  893. }
  894. LOG_DEBUG (
  895. PF_LLDP_LOG,
  896. "LLDP(%d): Send port %u MAC: "
  897. "%02X:%02X:%02X:%02X:%02X:%02X "
  898. "IP: %s Chassis ID: \"%s\" Port ID: \"%s\"\n",
  899. __LINE__,
  900. loc_port_num,
  901. p_port_data->netif.mac_address.addr[0],
  902. p_port_data->netif.mac_address.addr[1],
  903. p_port_data->netif.mac_address.addr[2],
  904. p_port_data->netif.mac_address.addr[3],
  905. p_port_data->netif.mac_address.addr[4],
  906. p_port_data->netif.mac_address.addr[5],
  907. ip_string,
  908. chassis_id_description,
  909. port_id.string);
  910. #endif
  911. pos = 0;
  912. pf_lldp_add_ethernet_header (
  913. buf,
  914. &pos,
  915. lldp_dst_addr,
  916. p_port_data->netif.mac_address);
  917. /* Add mandatory parts */
  918. pf_lldp_add_chassis_id_tlv (&chassis_id, buf, &pos);
  919. pf_lldp_add_port_id_tlv (&port_id, buf, &pos);
  920. pf_lldp_add_ttl_tlv (buf, &pos);
  921. /* Add optional parts */
  922. pf_lldp_add_port_status (0, 0, buf, &pos);
  923. pf_lldp_add_chassis_mac (device_mac_address, buf, &pos);
  924. pf_lldp_add_ieee_mac_phy (&link_status, buf, &pos);
  925. pf_lldp_add_management (&man_address, buf, &pos);
  926. /* Add end of LLDP-PDU marker */
  927. pf_lldp_add_tlv_header (buf, &pos, LLDP_TYPE_END, 0);
  928. return pos;
  929. }
  930. /**
  931. * Build and send a LLDP message on a specific port.
  932. *
  933. * @param net InOut: The p-net stack instance
  934. * @param loc_port_num In: Local port number.
  935. * Valid range: 1 .. num_physical_ports
  936. */
  937. static void pf_lldp_send (pnet_t * net, int loc_port_num)
  938. {
  939. /* FIXME: Buffer size should include Ethernet header (14 bytes) */
  940. pnal_buf_t * p_buffer = pnal_buf_alloc (PF_FRAME_BUFFER_SIZE);
  941. if (p_buffer == NULL)
  942. {
  943. return;
  944. }
  945. if (p_buffer->payload != NULL)
  946. {
  947. p_buffer->len =
  948. pf_lldp_construct_frame (net, loc_port_num, p_buffer->payload);
  949. (void)pf_eth_send_on_physical_port (net, loc_port_num, p_buffer);
  950. }
  951. pnal_buf_free (p_buffer);
  952. }
  953. /**
  954. * @internal
  955. * Trigger sending a LLDP frame.
  956. *
  957. * This is a callback for the scheduler. Arguments should fulfill
  958. * pf_scheduler_timeout_ftn_t
  959. *
  960. * Re-schedules itself after 5 s.
  961. *
  962. * @param net InOut: The p-net stack instance
  963. * @param arg In: Reference to port_data
  964. * @param current_time In: Not used.
  965. */
  966. static void pf_lldp_trigger_sending (
  967. pnet_t * net,
  968. void * arg,
  969. uint32_t current_time)
  970. {
  971. pf_port_t * p_port_data = (pf_port_t *)arg;
  972. pf_lldp_send (net, p_port_data->port_num);
  973. if (
  974. pf_scheduler_add (
  975. net,
  976. PF_LLDP_SEND_INTERVAL * 1000,
  977. pf_lldp_trigger_sending,
  978. p_port_data,
  979. &p_port_data->lldp.tx_timeout) != 0)
  980. {
  981. LOG_ERROR (
  982. PF_LLDP_LOG,
  983. "LLDP(%d): Failed to reschedule LLDP sending\n",
  984. __LINE__);
  985. }
  986. }
  987. /**
  988. * @internal
  989. * Restart LLDP transmission timer and optionally send LLDP frame.
  990. *
  991. * @param net InOut: The p-net stack instance
  992. * @param loc_port_num In: Local port number.
  993. * Valid range: 1 .. num_physical_ports
  994. * @param send In: Send LLDP message
  995. */
  996. static void pf_lldp_tx_restart (pnet_t * net, int loc_port_num, bool send)
  997. {
  998. pf_port_t * p_port_data = pf_port_get_state (net, loc_port_num);
  999. if (
  1000. pf_scheduler_restart (
  1001. net,
  1002. PF_LLDP_SEND_INTERVAL * 1000,
  1003. pf_lldp_trigger_sending,
  1004. p_port_data,
  1005. &p_port_data->lldp.tx_timeout) != 0)
  1006. {
  1007. LOG_ERROR (
  1008. PF_ETH_LOG,
  1009. "LLDP(%d): Failed to schedule LLDP sending for port %d\n",
  1010. __LINE__,
  1011. loc_port_num);
  1012. }
  1013. if (send)
  1014. {
  1015. pf_lldp_send (net, loc_port_num);
  1016. }
  1017. }
  1018. void pf_lldp_init (pnet_t * net)
  1019. {
  1020. int port;
  1021. pf_port_iterator_t port_iterator;
  1022. pf_port_t * p_port_data = NULL;
  1023. pf_port_init_iterator_over_ports (net, &port_iterator);
  1024. port = pf_port_get_next (&port_iterator);
  1025. while (port != 0)
  1026. {
  1027. p_port_data = pf_port_get_state (net, port);
  1028. memset (&p_port_data->lldp, 0, sizeof (p_port_data->lldp));
  1029. pf_scheduler_init_handle (&p_port_data->lldp.rx_timeout, "lldp_rx");
  1030. pf_scheduler_init_handle (&p_port_data->lldp.tx_timeout, "lldp_tx");
  1031. port = pf_port_get_next (&port_iterator);
  1032. }
  1033. net->lldp_mutex = os_mutex_create();
  1034. CC_ASSERT (net->lldp_mutex != NULL);
  1035. }
  1036. void pf_lldp_send_enable (pnet_t * net, int loc_port_num)
  1037. {
  1038. LOG_DEBUG (
  1039. PF_LLDP_LOG,
  1040. "LLDP(%d): Enabling LLDP transmission for port %d\n",
  1041. __LINE__,
  1042. loc_port_num);
  1043. pf_lldp_tx_restart (net, loc_port_num, true);
  1044. }
  1045. void pf_lldp_send_disable (pnet_t * net, int loc_port_num)
  1046. {
  1047. pf_port_t * p_port_data = pf_port_get_state (net, loc_port_num);
  1048. LOG_DEBUG (
  1049. PF_LLDP_LOG,
  1050. "LLDP(%d): Disabling LLDP transmission for port %d\n",
  1051. __LINE__,
  1052. loc_port_num);
  1053. pf_scheduler_remove_if_running (net, &p_port_data->lldp.tx_timeout);
  1054. }
  1055. /**
  1056. * Get organizationally specific header from received packet
  1057. *
  1058. * Layout of organizationally specific header:
  1059. * 3 Organizationally unique identifier
  1060. * 1 Organizationally defined subtype
  1061. *
  1062. * @param parse_info InOut: Parser state.
  1063. * @param offset InOut: Offset in parsed buffer.
  1064. * @param header Out: Parsed organizationally specific header.
  1065. */
  1066. void pf_lldp_get_org_header_from_packet (
  1067. pf_get_info_t * parse_info,
  1068. uint16_t * offset,
  1069. pf_lldp_org_header_t * header)
  1070. {
  1071. pf_get_mem (parse_info, offset, sizeof (header->id), header->id);
  1072. header->subtype = pf_get_byte (parse_info, offset);
  1073. }
  1074. /**
  1075. * Get Chassis ID from received packet
  1076. *
  1077. * Layout of Chassis ID TLV:
  1078. * 2 TLV header (already processed)
  1079. * 1 Chassis ID subtype
  1080. * 1-255 Chassis ID
  1081. *
  1082. * @param parse_info InOut: Parsing information.
  1083. * @param offset InOut: Byte offset in buffer.
  1084. * @param tlv_len In: Length of TLV in bytes.
  1085. * @param chassis_id Out: Parsed Chassis ID.
  1086. */
  1087. static void pf_lldp_get_chassis_id_from_packet (
  1088. pf_get_info_t * parse_info,
  1089. uint16_t * const offset,
  1090. const size_t tlv_len,
  1091. pf_lldp_chassis_id_t * chassis_id)
  1092. {
  1093. size_t max_len = sizeof (chassis_id->string) - 1;
  1094. if (tlv_len == 0)
  1095. {
  1096. LOG_WARNING (
  1097. PF_LLDP_LOG,
  1098. "LLDP(%d): Ignoring Chassis Id, tlv len=%d\n",
  1099. __LINE__,
  1100. (int)tlv_len);
  1101. return;
  1102. }
  1103. chassis_id->len = tlv_len - 1;
  1104. if (chassis_id->len > max_len)
  1105. {
  1106. *offset += tlv_len;
  1107. LOG_ERROR (
  1108. PF_LLDP_LOG,
  1109. "LLDP(%d): Chassis Id too long (%d)\n",
  1110. __LINE__,
  1111. (int)chassis_id->len);
  1112. /* Should not happen. Reconfigure PNET_LLDP_CHASSIS_ID_MAX_LEN
  1113. * if it does*/
  1114. return;
  1115. }
  1116. chassis_id->subtype = pf_get_byte (parse_info, offset);
  1117. pf_get_mem (parse_info, offset, chassis_id->len, chassis_id->string);
  1118. chassis_id->string[chassis_id->len] = '\0';
  1119. chassis_id->is_valid = true;
  1120. }
  1121. /**
  1122. * Get Port ID from received packet
  1123. *
  1124. * Layout of Port ID TLV:
  1125. * 2 TLV header (already processed)
  1126. * 1 Port ID subtype
  1127. * 1-255 Port ID
  1128. *
  1129. * @param parse_info InOut: Parsing information.
  1130. * @param offset InOut: Byte offset in buffer.
  1131. * @param tlv_len In: Length of TLV in bytes.
  1132. * @param port_id Out: Parsed Port ID.
  1133. */
  1134. static void pf_lldp_get_port_id_from_packet (
  1135. pf_get_info_t * parse_info,
  1136. uint16_t * const offset,
  1137. const size_t tlv_len,
  1138. pf_lldp_port_id_t * port_id)
  1139. {
  1140. size_t max_len = sizeof (port_id->string) - 1;
  1141. if (tlv_len == 0)
  1142. {
  1143. LOG_WARNING (
  1144. PF_LLDP_LOG,
  1145. "LLDP(%d): Ignoring Port Id, tlv len=%d\n",
  1146. __LINE__,
  1147. (int)tlv_len);
  1148. return;
  1149. }
  1150. port_id->len = tlv_len - 1;
  1151. if (port_id->len > max_len)
  1152. {
  1153. *offset += tlv_len;
  1154. LOG_ERROR (
  1155. PF_LLDP_LOG,
  1156. "LLDP(%d): Port Id too long (%d)\n",
  1157. __LINE__,
  1158. (int)port_id->len);
  1159. /* Should not happen. Reconfigure PNET_LLDP_PORT_ID_MAX_LEN
  1160. * if it does*/
  1161. return;
  1162. }
  1163. port_id->subtype = pf_get_byte (parse_info, offset);
  1164. pf_get_mem (parse_info, offset, port_id->len, port_id->string);
  1165. port_id->string[port_id->len] = '\0';
  1166. port_id->is_valid = true;
  1167. }
  1168. /**
  1169. * Get TTL from received packet
  1170. *
  1171. * Layout of Time To Live TLV:
  1172. * 2 TLV header (already processed)
  1173. * 2 Time to live (TTL)
  1174. *
  1175. * @param parse_info InOut: Parsing information.
  1176. * @param offset InOut: Byte offset in buffer.
  1177. * @param tlv_len In: Length of TLV in bytes.
  1178. * @param ttl Out: Parsed TTL.
  1179. */
  1180. static void pf_lldp_get_ttl_from_packet (
  1181. pf_get_info_t * parse_info,
  1182. uint16_t * const offset,
  1183. const size_t tlv_len,
  1184. uint16_t * ttl)
  1185. {
  1186. if (tlv_len != 2)
  1187. {
  1188. parse_info->result = PF_PARSE_ERROR;
  1189. LOG_WARNING (
  1190. PF_LLDP_LOG,
  1191. "LLDP(%d): Ignoring TTL, unexpected tlv len %d(2)\n",
  1192. __LINE__,
  1193. (int)tlv_len);
  1194. return;
  1195. }
  1196. *ttl = pf_get_uint16 (parse_info, offset);
  1197. }
  1198. /**
  1199. * Get port description from received packet
  1200. *
  1201. * Layout of Port description TLV:
  1202. * 2 TLV header (already processed)
  1203. * 0-255 Port description
  1204. *
  1205. * @param parse_info InOut: Parsing information.
  1206. * @param offset InOut: Byte offset in buffer.
  1207. * @param tlv_len In: Length of TLV in bytes.
  1208. * @param port_description Out: Parsed port description.
  1209. */
  1210. static void pf_lldp_get_port_description_from_packet (
  1211. pf_get_info_t * parse_info,
  1212. uint16_t * const offset,
  1213. const size_t tlv_len,
  1214. pf_lldp_port_description_t * port_description)
  1215. {
  1216. size_t max_len = sizeof (port_description->string) - 1;
  1217. if (tlv_len > max_len)
  1218. {
  1219. *offset += tlv_len;
  1220. LOG_ERROR (
  1221. PF_LLDP_LOG,
  1222. "LLDP(%d): Port Description too long (%d)\n",
  1223. __LINE__,
  1224. (int)tlv_len);
  1225. return;
  1226. }
  1227. port_description->len = tlv_len;
  1228. pf_get_mem (parse_info, offset, tlv_len, port_description->string);
  1229. port_description->string[tlv_len] = '\0';
  1230. port_description->is_valid = true;
  1231. }
  1232. /**
  1233. * Get management address from received packet
  1234. *
  1235. * Layout of Management address TLV:
  1236. * 2 TLV header (already processed)
  1237. * 1 Management address string length
  1238. * 1 Management address subtype
  1239. * 1-31 Management address
  1240. * 1 Interface numbering subtype
  1241. * 4 Interface number
  1242. * 1 OID string length
  1243. * 0-128 Object identifier
  1244. *
  1245. * @param parse_info InOut: Parsing information.
  1246. * @param offset InOut: Byte offset in buffer.
  1247. * @param tlv_len In: Length of TLV in bytes.
  1248. * @param management_address Out: Parsed management address.
  1249. */
  1250. static void pf_lldp_get_management_address_from_packet (
  1251. pf_get_info_t * parse_info,
  1252. uint16_t * const offset,
  1253. const size_t tlv_len,
  1254. pf_lldp_management_address_t * management_address)
  1255. {
  1256. size_t oid_len;
  1257. memset (management_address, 0, sizeof (*management_address));
  1258. management_address->len = pf_get_byte (parse_info, offset) - 1;
  1259. if (management_address->len < 1 || management_address->len > 31)
  1260. {
  1261. *offset += tlv_len - 1;
  1262. LOG_ERROR (
  1263. PF_LLDP_LOG,
  1264. "LLDP(%d): Invalid management address length (%d)\n",
  1265. __LINE__,
  1266. (int)management_address->len);
  1267. return;
  1268. }
  1269. management_address->subtype = pf_get_byte (parse_info, offset);
  1270. pf_get_mem (
  1271. parse_info,
  1272. offset,
  1273. management_address->len,
  1274. management_address->value);
  1275. management_address->interface_number.subtype =
  1276. pf_get_byte (parse_info, offset);
  1277. management_address->interface_number.value =
  1278. pf_get_uint32 (parse_info, offset);
  1279. /* We don't save the Object ID */
  1280. oid_len = pf_get_byte (parse_info, offset);
  1281. *offset += oid_len;
  1282. management_address->is_valid = true;
  1283. }
  1284. /**
  1285. * Get signal delay values from received packet
  1286. *
  1287. * Layout of LLDP_PNIO_DELAY TLV:
  1288. * 2 TLV header (already processed)
  1289. * 3 LLDP_PNIO_Header (already processed)
  1290. * 1 LLDP_PNIO_SubType (already processed)
  1291. * 4 PTCP_PortRxDelayLocal
  1292. * 4 PTCP_PortRxDelayRemote
  1293. * 4 PTCP_PortTxDelayLocal
  1294. * 4 PTCP_PortTxDelayRemote
  1295. * 4 CableDelayLocal
  1296. *
  1297. * @param parse_info InOut: Parsing information.
  1298. * @param offset InOut: Byte offset in buffer.
  1299. * @param tlv_len In: Length of TLV in bytes.
  1300. * @param delay Out: Parsed delay values.
  1301. */
  1302. static void pf_lldp_get_signal_delay_from_packet (
  1303. pf_get_info_t * parse_info,
  1304. uint16_t * const offset,
  1305. const size_t tlv_len,
  1306. pf_lldp_signal_delay_t * delay)
  1307. {
  1308. if (tlv_len != LLDP_PNIO_SUBTYPE_MEAS_DELAY_VALUES_TLV_LEN)
  1309. {
  1310. parse_info->result = PF_PARSE_ERROR;
  1311. return;
  1312. }
  1313. delay->rx_delay_local = pf_get_uint32 (parse_info, offset);
  1314. delay->rx_delay_remote = pf_get_uint32 (parse_info, offset);
  1315. delay->tx_delay_local = pf_get_uint32 (parse_info, offset);
  1316. delay->tx_delay_remote = pf_get_uint32 (parse_info, offset);
  1317. delay->cable_delay_local = pf_get_uint32 (parse_info, offset);
  1318. delay->is_valid = true;
  1319. }
  1320. /**
  1321. * Get port status from received packet
  1322. *
  1323. * Layout of LLDP_PNIO_PORTSTATUS TLV:
  1324. * 2 TLV header (already processed)
  1325. * 3 LLDP_PNIO_Header (already processed)
  1326. * 1 LLDP_PNIO_SubType (already processed)
  1327. * 2 RTClass2_PortStatus
  1328. * 2 RTClass3_PortStatus
  1329. *
  1330. * @param parse_info InOut: Parsing information.
  1331. * @param offset InOut: Byte offset in buffer.
  1332. * @param tlv_len In: Length of TLV in bytes.
  1333. * @param status Out: Parsed port status.
  1334. */
  1335. static void pf_lldp_get_port_status_from_packet (
  1336. pf_get_info_t * parse_info,
  1337. uint16_t * const offset,
  1338. const size_t tlv_len,
  1339. uint8_t status[4])
  1340. {
  1341. if (tlv_len != LLDP_PNIO_SUBTYPE_PORT_STATUS_TLV_LEN)
  1342. {
  1343. parse_info->result = PF_PARSE_ERROR;
  1344. return;
  1345. }
  1346. /* FIXME: Should interpret as two 16-bit integers */
  1347. pf_get_mem (parse_info, offset, 4, status);
  1348. }
  1349. /**
  1350. * Get Chassis MAC address from received packet
  1351. *
  1352. * Layout of LLDP_PNIO_CHASSIS_MAC TLV:
  1353. * 2 TLV header (already processed)
  1354. * 3 LLDP_PNIO_Header (already processed)
  1355. * 1 LLDP_PNIO_SubType (already processed)
  1356. * 6 Chassis MAC address
  1357. *
  1358. * @param parse_info InOut: Parsing information.
  1359. * @param offset InOut: Byte offset in buffer.
  1360. * @param tlv_len In: Length of TLV in bytes.
  1361. * @param chassis_mac Out: Parsed Chassis MAC address.
  1362. */
  1363. static void pf_lldp_get_chassis_mac_from_packet (
  1364. pf_get_info_t * parse_info,
  1365. uint16_t * const offset,
  1366. const size_t tlv_len,
  1367. pnet_ethaddr_t * chassis_mac)
  1368. {
  1369. if (tlv_len != LLDP_PNIO_SUBTYPE_CHASSIS_MAC_TLV_LEN)
  1370. {
  1371. parse_info->result = PF_PARSE_ERROR;
  1372. return;
  1373. }
  1374. pf_get_mem (
  1375. parse_info,
  1376. offset,
  1377. sizeof (chassis_mac->addr),
  1378. chassis_mac->addr);
  1379. }
  1380. /**
  1381. * Get link status from received packet
  1382. *
  1383. * Layout of MAC/PHY Configuration/Status TLV:
  1384. * 2 TLV header (already processed)
  1385. * 4 802.3 header (already processed)
  1386. * 1 Auto-negotiation support/status:
  1387. * Bit 0: Auto-negotiation support
  1388. * Bit 1: Auto-negotiation status
  1389. * 2 PMD auto-negotiation advertised capability
  1390. * 2 Operational MAU typ
  1391. *
  1392. * @param parse_info InOut: Parsing information.
  1393. * @param offset InOut: Byte offset in buffer.
  1394. * @param tlv_len In: Length of TLV in bytes.
  1395. * @param link_status Out: Parsed link status.
  1396. */
  1397. static void pf_lldp_get_link_status_from_packet (
  1398. pf_get_info_t * parse_info,
  1399. uint16_t * const offset,
  1400. const size_t tlv_len,
  1401. pf_lldp_link_status_t * link_status)
  1402. {
  1403. uint8_t aneg_support_status;
  1404. memset (link_status, 0, sizeof (*link_status));
  1405. if (tlv_len != LLDP_IEEE_SUBTYPE_MAC_PHY_TLV_LEN)
  1406. {
  1407. parse_info->result = PF_PARSE_ERROR;
  1408. return;
  1409. }
  1410. aneg_support_status = pf_get_byte (parse_info, offset);
  1411. link_status->is_autonegotiation_supported = aneg_support_status &
  1412. LLDP_AUTONEG_SUPPORTED;
  1413. link_status->is_autonegotiation_enabled = aneg_support_status &
  1414. LLDP_AUTONEG_ENABLED;
  1415. link_status->autonegotiation_advertised_capabilities =
  1416. pf_get_uint16 (parse_info, offset);
  1417. link_status->operational_mau_type = pf_get_uint16 (parse_info, offset);
  1418. link_status->is_valid = true;
  1419. }
  1420. /**
  1421. * @internal
  1422. * Parse LLDP frame buffer
  1423. *
  1424. * @param buf In: LLDP data.
  1425. * @param len In: Length of LLDP data.
  1426. * @param lldp_peer_info Out: Record holding parsed LLDP information.
  1427. * Nulled initially (also on failure).
  1428. * @return 0 if parsing is successful, -1 on error.
  1429. */
  1430. int pf_lldp_parse_packet (
  1431. const uint8_t buf[],
  1432. uint16_t len,
  1433. pf_lldp_peer_info_t * lldp_peer_info)
  1434. {
  1435. lldp_tlv_t tlv;
  1436. pf_lldp_org_header_t org;
  1437. pf_get_info_t parse_info;
  1438. pf_lldp_management_address_t management_address;
  1439. uint16_t offset = 0;
  1440. memset (lldp_peer_info, 0, sizeof (*lldp_peer_info));
  1441. parse_info.result = PF_PARSE_OK;
  1442. parse_info.is_big_endian = true;
  1443. parse_info.len = len;
  1444. parse_info.p_buf = buf;
  1445. tlv = pf_lldp_get_tlv_from_packet (&parse_info, &offset);
  1446. while (tlv.type != LLDP_TYPE_END)
  1447. {
  1448. switch (tlv.type)
  1449. {
  1450. case LLDP_TYPE_CHASSIS_ID:
  1451. pf_lldp_get_chassis_id_from_packet (
  1452. &parse_info,
  1453. &offset,
  1454. tlv.len,
  1455. &lldp_peer_info->chassis_id);
  1456. break;
  1457. case LLDP_TYPE_PORT_ID:
  1458. pf_lldp_get_port_id_from_packet (
  1459. &parse_info,
  1460. &offset,
  1461. tlv.len,
  1462. &lldp_peer_info->port_id);
  1463. break;
  1464. case LLDP_TYPE_TTL:
  1465. pf_lldp_get_ttl_from_packet (
  1466. &parse_info,
  1467. &offset,
  1468. tlv.len,
  1469. &lldp_peer_info->ttl);
  1470. break;
  1471. case LLDP_TYPE_PORT_DESCRIPTION:
  1472. pf_lldp_get_port_description_from_packet (
  1473. &parse_info,
  1474. &offset,
  1475. tlv.len,
  1476. &lldp_peer_info->port_description);
  1477. break;
  1478. case LLDP_TYPE_MANAGEMENT_ADDRESS:
  1479. pf_lldp_get_management_address_from_packet (
  1480. &parse_info,
  1481. &offset,
  1482. tlv.len,
  1483. &management_address);
  1484. if (pf_lldp_management_address_is_ipv4 (&management_address))
  1485. {
  1486. lldp_peer_info->management_address = management_address;
  1487. }
  1488. break;
  1489. case LLDP_TYPE_ORG_SPEC:
  1490. {
  1491. pf_lldp_get_org_header_from_packet (&parse_info, &offset, &org);
  1492. if (memcmp (org_id_pnio, org.id, sizeof (org.id)) == 0)
  1493. {
  1494. switch (org.subtype)
  1495. {
  1496. case LLDP_PNIO_SUBTYPE_MEAS_DELAY_VALUES:
  1497. pf_lldp_get_signal_delay_from_packet (
  1498. &parse_info,
  1499. &offset,
  1500. tlv.len,
  1501. &lldp_peer_info->port_delay);
  1502. break;
  1503. case LLDP_PNIO_SUBTYPE_PORT_STATUS:
  1504. pf_lldp_get_port_status_from_packet (
  1505. &parse_info,
  1506. &offset,
  1507. tlv.len,
  1508. lldp_peer_info->port_status);
  1509. break;
  1510. case LLDP_PNIO_SUBTYPE_CHASSIS_MAC:
  1511. pf_lldp_get_chassis_mac_from_packet (
  1512. &parse_info,
  1513. &offset,
  1514. tlv.len,
  1515. &lldp_peer_info->mac_address);
  1516. break;
  1517. default:
  1518. offset += (tlv.len - 4);
  1519. break;
  1520. }
  1521. }
  1522. else if (memcmp (org_id_ieee_8023, org.id, sizeof (org.id)) == 0)
  1523. {
  1524. switch (org.subtype)
  1525. {
  1526. case LLDP_IEEE_SUBTYPE_MAC_PHY:
  1527. pf_lldp_get_link_status_from_packet (
  1528. &parse_info,
  1529. &offset,
  1530. tlv.len,
  1531. &lldp_peer_info->phy_config);
  1532. break;
  1533. default:
  1534. offset += (tlv.len - 4);
  1535. break;
  1536. }
  1537. }
  1538. else
  1539. {
  1540. offset += (tlv.len - 4);
  1541. }
  1542. }
  1543. break;
  1544. default:
  1545. offset += tlv.len;
  1546. break;
  1547. }
  1548. tlv = pf_lldp_get_tlv_from_packet (&parse_info, &offset);
  1549. }
  1550. if (parse_info.result != PF_PARSE_OK)
  1551. {
  1552. LOG_ERROR (
  1553. PF_LLDP_LOG,
  1554. "LLDP(%d): Received LLDP frame, but failed to parse it.\n",
  1555. __LINE__);
  1556. return -1;
  1557. }
  1558. return 0;
  1559. }
  1560. /**
  1561. * @internal
  1562. * Generate an alias name (for me) from the peer port_id and peer chassis_id
  1563. *
  1564. * See PN-Topology 6.4.2 and PN-Protocol 4.3.1.4.18
  1565. *
  1566. * @param port_id In: Peer port ID. Terminated string.
  1567. * @param chassis_id In: Peer chassis ID. Terminated string.
  1568. * @param alias Out: Alias name. Terminated string.
  1569. * @param len In: Size of alias name output buffer
  1570. * @return 0 if the operation succeeded.
  1571. * -1 if an error occurred.
  1572. */
  1573. int pf_lldp_generate_alias_name (
  1574. const char * port_id,
  1575. const char * chassis_id,
  1576. char * alias,
  1577. uint16_t len)
  1578. {
  1579. int res = 0;
  1580. if (port_id == NULL || chassis_id == NULL || alias == NULL)
  1581. {
  1582. return -1;
  1583. }
  1584. if (strchr (port_id, '.') != NULL)
  1585. {
  1586. /* Assume PN v2.3+ device. PortId include NameOfStation.
  1587. * and the PortId can can used as alias.
  1588. */
  1589. if (strlen (port_id) >= len)
  1590. {
  1591. return -1;
  1592. }
  1593. strncpy (alias, port_id, len);
  1594. }
  1595. else
  1596. {
  1597. /* Assume PN v2.2 device. ChassisId is same as NameOfStation
  1598. * Alias includes PortId and ChassisId
  1599. */
  1600. if ((strlen (port_id) + strlen (chassis_id) + 1) >= len)
  1601. {
  1602. return -1;
  1603. }
  1604. res = snprintf (alias, len, "%s.%s", port_id, chassis_id);
  1605. if ((res < 0) || (res >= (int)len))
  1606. {
  1607. return -1;
  1608. }
  1609. }
  1610. return 0;
  1611. }
  1612. bool pf_lldp_is_alias_matching (pnet_t * net, const char * alias)
  1613. {
  1614. char port_alias[PF_ALIAS_NAME_MAX_SIZE]; /** Terminated */
  1615. int port;
  1616. pf_port_iterator_t port_iterator;
  1617. pf_port_t * p_port_data = NULL;
  1618. pf_port_init_iterator_over_ports (net, &port_iterator);
  1619. port = pf_port_get_next (&port_iterator);
  1620. while (port != 0)
  1621. {
  1622. p_port_data = pf_port_get_state (net, port);
  1623. if (
  1624. p_port_data->lldp.is_peer_info_received &&
  1625. (pf_lldp_generate_alias_name (
  1626. p_port_data->lldp.peer_info.port_id.string,
  1627. p_port_data->lldp.peer_info.chassis_id.string,
  1628. port_alias,
  1629. sizeof (port_alias)) == 0))
  1630. {
  1631. if (strcmp (alias, port_alias) == 0)
  1632. {
  1633. return true;
  1634. }
  1635. }
  1636. port = pf_port_get_next (&port_iterator);
  1637. }
  1638. return false;
  1639. }
  1640. /**
  1641. * @internal
  1642. * Store peer information
  1643. *
  1644. * Information is stored atomically (by means of the LLDP mutex).
  1645. * This ensures that other threads (e.g. SNMP) will read consistent data.
  1646. *
  1647. * A timestamp is also generated and stored.
  1648. *
  1649. * @param net InOut: p-net stack instance
  1650. * @param loc_port_num In: Local port number.
  1651. * Valid range: 1 .. num_physical_ports
  1652. * @param new_info In: Peer data to be stored
  1653. */
  1654. void pf_lldp_store_peer_info (
  1655. pnet_t * net,
  1656. int loc_port_num,
  1657. const pf_lldp_peer_info_t * new_info)
  1658. {
  1659. pf_port_t * p_port_data = pf_port_get_state (net, loc_port_num);
  1660. uint32_t timestamp = pnal_get_system_uptime_10ms();
  1661. os_mutex_lock (net->lldp_mutex);
  1662. p_port_data->lldp.is_peer_info_received = true;
  1663. p_port_data->lldp.timestamp_for_last_peer_change = timestamp;
  1664. p_port_data->lldp.peer_info = *new_info,
  1665. os_mutex_unlock (net->lldp_mutex);
  1666. }
  1667. /**
  1668. * Apply updated peer information
  1669. *
  1670. * Trigger a diagnosis if the peer information not is as expected.
  1671. * Resets the peer watchdog timer.
  1672. *
  1673. * @param net InOut: p-net stack instance
  1674. * @param loc_port_num In: Local port number.
  1675. * Valid range: 1 .. num_physical_ports
  1676. * @param lldp_peer_info In: Peer data to be applied
  1677. */
  1678. void pf_lldp_update_peer (
  1679. pnet_t * net,
  1680. int loc_port_num,
  1681. const pf_lldp_peer_info_t * lldp_peer_info)
  1682. {
  1683. pf_port_t * p_port_data = pf_port_get_state (net, loc_port_num);
  1684. pf_lldp_restart_peer_timeout (net, loc_port_num, lldp_peer_info->ttl);
  1685. if (
  1686. memcmp (
  1687. lldp_peer_info,
  1688. &p_port_data->lldp.peer_info,
  1689. sizeof (pf_lldp_peer_info_t)) == 0)
  1690. {
  1691. /* No changes */
  1692. return;
  1693. }
  1694. if (p_port_data->lldp.is_peer_info_received)
  1695. {
  1696. if (
  1697. p_port_data->lldp.peer_info.chassis_id.subtype ==
  1698. PF_LLDP_SUBTYPE_LOCALLY_ASSIGNED &&
  1699. p_port_data->lldp.peer_info.port_id.subtype ==
  1700. PF_LLDP_SUBTYPE_LOCALLY_ASSIGNED)
  1701. {
  1702. LOG_INFO (
  1703. PF_LLDP_LOG,
  1704. "LLDP(%d): Peer info changed on port %d. Old peer info - MAC: "
  1705. "%02X:%02X:%02X:%02X:%02X:%02X "
  1706. "Chassis ID: %s Port ID: %s\n",
  1707. __LINE__,
  1708. loc_port_num,
  1709. p_port_data->lldp.peer_info.mac_address.addr[0],
  1710. p_port_data->lldp.peer_info.mac_address.addr[1],
  1711. p_port_data->lldp.peer_info.mac_address.addr[2],
  1712. p_port_data->lldp.peer_info.mac_address.addr[3],
  1713. p_port_data->lldp.peer_info.mac_address.addr[4],
  1714. p_port_data->lldp.peer_info.mac_address.addr[5],
  1715. p_port_data->lldp.peer_info.chassis_id.string,
  1716. p_port_data->lldp.peer_info.port_id.string);
  1717. }
  1718. else
  1719. {
  1720. LOG_INFO (
  1721. PF_LLDP_LOG,
  1722. "LLDP(%d): Peer info changed on port %d.\n",
  1723. __LINE__,
  1724. loc_port_num);
  1725. }
  1726. }
  1727. if (
  1728. lldp_peer_info->chassis_id.subtype == PF_LLDP_SUBTYPE_LOCALLY_ASSIGNED &&
  1729. lldp_peer_info->port_id.subtype == PF_LLDP_SUBTYPE_LOCALLY_ASSIGNED)
  1730. {
  1731. LOG_INFO (
  1732. PF_LLDP_LOG,
  1733. "LLDP(%d): New peer info on port %d - MAC: "
  1734. "%02X:%02X:%02X:%02X:%02X:%02X "
  1735. "Chassis ID: %s Port ID: %s\n",
  1736. __LINE__,
  1737. loc_port_num,
  1738. lldp_peer_info->mac_address.addr[0],
  1739. lldp_peer_info->mac_address.addr[1],
  1740. lldp_peer_info->mac_address.addr[2],
  1741. lldp_peer_info->mac_address.addr[3],
  1742. lldp_peer_info->mac_address.addr[4],
  1743. lldp_peer_info->mac_address.addr[5],
  1744. lldp_peer_info->chassis_id.string,
  1745. lldp_peer_info->port_id.string);
  1746. }
  1747. pf_lldp_store_peer_info (net, loc_port_num, lldp_peer_info);
  1748. pf_pdport_peer_indication (net, loc_port_num);
  1749. }
  1750. int pf_lldp_recv (
  1751. pnet_t * net,
  1752. int loc_port_num,
  1753. pnal_buf_t * p_frame_buf,
  1754. uint16_t offset)
  1755. {
  1756. // uint8_t * buf = p_frame_buf->payload + offset;
  1757. uint8_t * buf = (uint8_t *)(p_frame_buf->payload) + offset;
  1758. uint16_t buf_len = p_frame_buf->len - offset;
  1759. pf_lldp_peer_info_t peer_data;
  1760. int err = 0;
  1761. err = pf_lldp_parse_packet (buf, buf_len, &peer_data);
  1762. if (!err)
  1763. {
  1764. if (
  1765. peer_data.chassis_id.subtype == PF_LLDP_SUBTYPE_LOCALLY_ASSIGNED &&
  1766. peer_data.port_id.subtype == PF_LLDP_SUBTYPE_LOCALLY_ASSIGNED)
  1767. {
  1768. LOG_DEBUG (
  1769. PF_LLDP_LOG,
  1770. "LLDP(%d): Receive port %u MAC: "
  1771. "%02X:%02X:%02X:%02X:%02X:%02X "
  1772. "Len: %d Chassis ID: %s Port ID: %s\n",
  1773. __LINE__,
  1774. loc_port_num,
  1775. peer_data.mac_address.addr[0],
  1776. peer_data.mac_address.addr[1],
  1777. peer_data.mac_address.addr[2],
  1778. peer_data.mac_address.addr[3],
  1779. peer_data.mac_address.addr[4],
  1780. peer_data.mac_address.addr[5],
  1781. p_frame_buf->len,
  1782. peer_data.chassis_id.string,
  1783. peer_data.port_id.string);
  1784. }
  1785. else if (peer_data.port_id.subtype == PF_LLDP_SUBTYPE_LOCALLY_ASSIGNED)
  1786. {
  1787. LOG_DEBUG (
  1788. PF_LLDP_LOG,
  1789. "LLDP(%d): LLDP frame received on port %u Len: %d Port "
  1790. "ID: %s\n",
  1791. __LINE__,
  1792. loc_port_num,
  1793. p_frame_buf->len,
  1794. peer_data.port_id.string);
  1795. }
  1796. else
  1797. {
  1798. LOG_DEBUG (
  1799. PF_LLDP_LOG,
  1800. "LLDP(%d): Non-profinet LLDP frame received on port %u "
  1801. "Len: "
  1802. "%d\n",
  1803. __LINE__,
  1804. loc_port_num,
  1805. p_frame_buf->len);
  1806. }
  1807. if (pf_port_is_valid (net, loc_port_num))
  1808. {
  1809. pf_lldp_update_peer (net, loc_port_num, &peer_data);
  1810. }
  1811. else
  1812. {
  1813. LOG_ERROR (
  1814. PF_LLDP_LOG,
  1815. "LLDP(%d): Unexpected local port %u, frame discarded.\n",
  1816. __LINE__,
  1817. loc_port_num);
  1818. }
  1819. }
  1820. pnal_buf_free (p_frame_buf);
  1821. return 1; /* Means: handled */
  1822. }