2 * Copyright (c) 2011-2016 Cisco and/or its affiliates.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at:
7 * http://www.apache.org/licenses/LICENSE-2.0
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
17 * @brief LLDP packet parsing implementation
19 #include <vnet/lldp/lldp_node.h>
20 #include <vnet/lldp/lldp_protocol.h>
23 lldp_tlv_get_code (const lldp_tlv_t * tlv)
25 return tlv->head.byte1 >> 1;
29 lldp_tlv_set_code (lldp_tlv_t * tlv, lldp_tlv_code_t code)
31 tlv->head.byte1 = (tlv->head.byte1 & 1) + (code << 1);
35 lldp_tlv_get_length (const lldp_tlv_t * tlv)
37 return (((u16) (tlv->head.byte1 & 1)) << 8) + tlv->head.byte2;
41 lldp_tlv_set_length (lldp_tlv_t * tlv, u16 length)
43 tlv->head.byte2 = length & ((1 << 8) - 1);
44 if (length > (1 << 8) - 1)
50 tlv->head.byte1 &= (1 << 8) - 2;
54 lldp_main_t lldp_main;
57 lldp_packet_scan (lldp_main_t * lm, lldp_intf_t * n, const lldp_tlv_t * pkt)
59 lldp_error_t e = LLDP_ERROR_NONE;
60 const lldp_tlv_t *tlv = pkt;
62 /* first check if the header fits in before extracting data from it */
63 #define TLV_VIOLATES_PKT_BOUNDARY(pkt, tlv) \
64 (((((u8 *)tlv) + sizeof(lldp_tlv_t)) > ((u8 *)pkt + vec_len(pkt))) || \
65 ((((u8 *)tlv) + lldp_tlv_get_length(tlv)) > ((u8 *)pkt + vec_len(pkt))))
67 /* first tlv is always chassis id, followed by port id and ttl tlvs */
68 if (TLV_VIOLATES_PKT_BOUNDARY (pkt, tlv) ||
69 LLDP_TLV_NAME (chassis_id) != lldp_tlv_get_code (tlv))
71 return LLDP_ERROR_BAD_TLV;
74 u16 l = lldp_tlv_get_length (tlv);
75 if (l < STRUCT_SIZE_OF (lldp_chassis_id_tlv_t, subtype) +
76 LLDP_MIN_CHASS_ID_LEN ||
77 l > STRUCT_SIZE_OF (lldp_chassis_id_tlv_t, subtype) +
78 LLDP_MAX_CHASS_ID_LEN)
80 return LLDP_ERROR_BAD_TLV;
83 u8 chid_subtype = ((lldp_chassis_id_tlv_t *) tlv)->subtype;
84 u8 *chid = ((lldp_chassis_id_tlv_t *) tlv)->id;
85 u8 chid_len = l - STRUCT_SIZE_OF (lldp_chassis_id_tlv_t, subtype);
87 tlv = (lldp_tlv_t *) ((u8 *) tlv + STRUCT_SIZE_OF (lldp_tlv_t, head) + l);
89 if (TLV_VIOLATES_PKT_BOUNDARY (pkt, tlv) ||
90 LLDP_TLV_NAME (port_id) != lldp_tlv_get_code (tlv))
92 return LLDP_ERROR_BAD_TLV;
94 l = lldp_tlv_get_length (tlv);
95 if (l < STRUCT_SIZE_OF (lldp_port_id_tlv_t, subtype) +
96 LLDP_MIN_PORT_ID_LEN ||
97 l > STRUCT_SIZE_OF (lldp_chassis_id_tlv_t, subtype) +
100 return LLDP_ERROR_BAD_TLV;
103 u8 portid_subtype = ((lldp_port_id_tlv_t *) tlv)->subtype;
104 u8 *portid = ((lldp_port_id_tlv_t *) tlv)->id;
105 u8 portid_len = l - STRUCT_SIZE_OF (lldp_port_id_tlv_t, subtype);
107 tlv = (lldp_tlv_t *) ((u8 *) tlv + STRUCT_SIZE_OF (lldp_tlv_t, head) + l);
109 if (TLV_VIOLATES_PKT_BOUNDARY (pkt, tlv) ||
110 LLDP_TLV_NAME (ttl) != lldp_tlv_get_code (tlv))
112 return LLDP_ERROR_BAD_TLV;
114 l = lldp_tlv_get_length (tlv);
115 if (l != STRUCT_SIZE_OF (lldp_ttl_tlv_t, ttl))
117 return LLDP_ERROR_BAD_TLV;
119 u16 ttl = ntohs (((lldp_ttl_tlv_t *) tlv)->ttl);
120 tlv = (lldp_tlv_t *) ((u8 *) tlv + STRUCT_SIZE_OF (lldp_tlv_t, head) + l);
121 while (!TLV_VIOLATES_PKT_BOUNDARY (pkt, tlv) &&
122 LLDP_TLV_NAME (pdu_end) != lldp_tlv_get_code (tlv))
124 switch (lldp_tlv_get_code (tlv))
126 #define F(num, type, str) \
127 case LLDP_TLV_NAME(type): \
128 /* ignore optional TLV */ \
130 foreach_lldp_optional_tlv_type (F);
133 return LLDP_ERROR_BAD_TLV;
139 tlv = (lldp_tlv_t *) ((u8 *) tlv + STRUCT_SIZE_OF (lldp_tlv_t, head) +
140 lldp_tlv_get_length (tlv));
142 /* last tlv is pdu_end */
143 if (TLV_VIOLATES_PKT_BOUNDARY (pkt, tlv) ||
144 LLDP_TLV_NAME (pdu_end) != lldp_tlv_get_code (tlv) ||
145 0 != lldp_tlv_get_length (tlv))
147 return LLDP_ERROR_BAD_TLV;
149 /* LLDP PDU validated, now store data */
152 _vec_len (n->chassis_id) = 0;
154 vec_add (n->chassis_id, chid, chid_len);
155 n->chassis_id_subtype = chid_subtype;
158 _vec_len (n->port_id) = 0;
160 vec_add (n->port_id, portid, portid_len);
161 n->port_id_subtype = portid_subtype;
163 return LLDP_ERROR_NONE;
167 lldp_get_intf (lldp_main_t * lm, u32 hw_if_index)
169 uword *p = hash_get (lm->intf_by_hw_if_index, hw_if_index);
173 return pool_elt_at_index (lm->intfs, p[0]);
179 lldp_create_intf (lldp_main_t * lm, u32 hw_if_index)
184 p = hash_get (lm->intf_by_hw_if_index, hw_if_index);
188 pool_get (lm->intfs, n);
189 memset (n, 0, sizeof (*n));
190 n->hw_if_index = hw_if_index;
191 hash_set (lm->intf_by_hw_if_index, n->hw_if_index, n - lm->intfs);
195 n = pool_elt_at_index (lm->intfs, p[0]);
204 lldp_input (vlib_main_t * vm, vlib_buffer_t * b0, u32 bi0)
206 lldp_main_t *lm = &lldp_main;
209 /* find our interface */
210 lldp_intf_t *n = lldp_get_intf (lm, vnet_buffer (b0)->sw_if_index[VLIB_RX]);
214 /* lldp disabled on this interface, we're done */
215 return LLDP_ERROR_DISABLED;
218 /* Actually scan the packet */
219 e = lldp_packet_scan (lm, n, vlib_buffer_get_current (b0));
221 if (LLDP_ERROR_NONE == e)
223 n->last_heard = vlib_time_now (vm);
232 static clib_error_t *
233 lldp_init (vlib_main_t * vm)
236 lldp_main_t *lm = &lldp_main;
238 if ((error = vlib_call_init_function (vm, lldp_template_init)))
242 lm->vnet_main = vnet_get_main ();
243 lm->msg_tx_hold = 4; /* default value per IEEE 802.1AB-2009 */
244 lm->msg_tx_interval = 30; /* default value per IEEE 802.1AB-2009 */
249 VLIB_INIT_FUNCTION (lldp_init);
252 * fd.io coding-style-patch-verification: ON
255 * eval: (c-set-style "gnu")