2 * Copied from Linux Monitor (LiMon) - Networking.
4 * Copyright 1994 - 2000 Neil Russell.
6 * Copyright 2000 Roland Borde
7 * Copyright 2000 Paolo Scaffardi
8 * Copyright 2000-2002 Wolfgang Denk, wd@denx.de
9 * SPDX-License-Identifier: GPL-2.0
14 #if defined(CONFIG_CDP_VERSION)
15 #include <timestamp.h>
20 /* Ethernet bcast address */
21 const u8 net_cdp_ethaddr
[6] = { 0x01, 0x00, 0x0c, 0xcc, 0xcc, 0xcc };
23 #define CDP_DEVICE_ID_TLV 0x0001
24 #define CDP_ADDRESS_TLV 0x0002
25 #define CDP_PORT_ID_TLV 0x0003
26 #define CDP_CAPABILITIES_TLV 0x0004
27 #define CDP_VERSION_TLV 0x0005
28 #define CDP_PLATFORM_TLV 0x0006
29 #define CDP_NATIVE_VLAN_TLV 0x000a
30 #define CDP_APPLIANCE_VLAN_TLV 0x000e
31 #define CDP_TRIGGER_TLV 0x000f
32 #define CDP_POWER_CONSUMPTION_TLV 0x0010
33 #define CDP_SYSNAME_TLV 0x0014
34 #define CDP_SYSOBJECT_TLV 0x0015
35 #define CDP_MANAGEMENT_ADDRESS_TLV 0x0016
37 #define CDP_TIMEOUT 250UL /* one packet every 250ms */
42 ushort cdp_native_vlan
;
43 ushort cdp_appliance_vlan
;
45 static const uchar cdp_snap_hdr
[8] = {
46 0xAA, 0xAA, 0x03, 0x00, 0x00, 0x0C, 0x20, 0x00 };
48 static ushort
cdp_compute_csum(const uchar
*buff
, ushort len
)
57 odd
= 1 & (ulong
)buff
;
67 if (result
& 0x80000000)
68 result
= (result
& 0xFFFF) + (result
>> 16);
72 leftover
= (signed short)(*(const signed char *)buff
);
74 * CISCO SUCKS big time! (and blows too):
75 * CDP uses the IP checksum algorithm with a twist;
76 * for the last byte it *sign* extends and sums.
78 result
= (result
& 0xffff0000) |
79 ((result
+ leftover
) & 0x0000ffff);
82 result
= (result
& 0xFFFF) + (result
>> 16);
85 result
= ((result
>> 8) & 0xff) |
86 ((result
& 0xff) << 8);
89 /* add up 16-bit and 17-bit words for 17+c bits */
90 result
= (result
& 0xffff) + (result
>> 16);
91 /* add up 16-bit and 2-bit for 16+c bit */
92 result
= (result
& 0xffff) + (result
>> 16);
94 result
= (result
& 0xffff) + (result
>> 16);
97 csum
= ~(ushort
)result
;
99 /* run time endian detection */
100 if (csum
!= htons(csum
)) /* little endian */
106 static int cdp_send_trigger(void)
111 struct ethernet_hdr
*et
;
114 #if defined(CONFIG_CDP_DEVICE_ID) || defined(CONFIG_CDP_PORT_ID) || \
115 defined(CONFIG_CDP_VERSION) || defined(CONFIG_CDP_PLATFORM)
120 et
= (struct ethernet_hdr
*)pkt
;
122 /* NOTE: trigger sent not on any VLAN */
124 /* form ethernet header */
125 memcpy(et
->et_dest
, net_cdp_ethaddr
, 6);
126 memcpy(et
->et_src
, net_ethaddr
, 6);
128 pkt
+= ETHER_HDR_SIZE
;
131 memcpy((uchar
*)pkt
, cdp_snap_hdr
, sizeof(cdp_snap_hdr
));
132 pkt
+= sizeof(cdp_snap_hdr
);
135 *pkt
++ = 0x02; /* CDP version 2 */
136 *pkt
++ = 180; /* TTL */
139 /* checksum (0 for later calculation) */
143 #ifdef CONFIG_CDP_DEVICE_ID
144 *s
++ = htons(CDP_DEVICE_ID_TLV
);
145 *s
++ = htons(CONFIG_CDP_DEVICE_ID
);
146 sprintf(buf
, CONFIG_CDP_DEVICE_ID_PREFIX
"%pm", net_ethaddr
);
147 memcpy((uchar
*)s
, buf
, 16);
151 #ifdef CONFIG_CDP_PORT_ID
152 *s
++ = htons(CDP_PORT_ID_TLV
);
153 memset(buf
, 0, sizeof(buf
));
154 sprintf(buf
, CONFIG_CDP_PORT_ID
, eth_get_dev_index());
156 if (len
& 1) /* make it even */
158 *s
++ = htons(len
+ 4);
159 memcpy((uchar
*)s
, buf
, len
);
163 #ifdef CONFIG_CDP_CAPABILITIES
164 *s
++ = htons(CDP_CAPABILITIES_TLV
);
166 *(ulong
*)s
= htonl(CONFIG_CDP_CAPABILITIES
);
170 #ifdef CONFIG_CDP_VERSION
171 *s
++ = htons(CDP_VERSION_TLV
);
172 memset(buf
, 0, sizeof(buf
));
173 strcpy(buf
, CONFIG_CDP_VERSION
);
175 if (len
& 1) /* make it even */
177 *s
++ = htons(len
+ 4);
178 memcpy((uchar
*)s
, buf
, len
);
182 #ifdef CONFIG_CDP_PLATFORM
183 *s
++ = htons(CDP_PLATFORM_TLV
);
184 memset(buf
, 0, sizeof(buf
));
185 strcpy(buf
, CONFIG_CDP_PLATFORM
);
187 if (len
& 1) /* make it even */
189 *s
++ = htons(len
+ 4);
190 memcpy((uchar
*)s
, buf
, len
);
194 #ifdef CONFIG_CDP_TRIGGER
195 *s
++ = htons(CDP_TRIGGER_TLV
);
197 *(ulong
*)s
= htonl(CONFIG_CDP_TRIGGER
);
201 #ifdef CONFIG_CDP_POWER_CONSUMPTION
202 *s
++ = htons(CDP_POWER_CONSUMPTION_TLV
);
204 *s
++ = htons(CONFIG_CDP_POWER_CONSUMPTION
);
207 /* length of ethernet packet */
208 len
= (uchar
*)s
- ((uchar
*)net_tx_packet
+ ETHER_HDR_SIZE
);
209 et
->et_protlen
= htons(len
);
211 len
= ETHER_HDR_SIZE
+ sizeof(cdp_snap_hdr
);
212 chksum
= cdp_compute_csum((uchar
*)net_tx_packet
+ len
,
213 (uchar
*)s
- (net_tx_packet
+ len
));
218 net_send_packet(net_tx_packet
, (uchar
*)s
- net_tx_packet
);
222 static void cdp_timeout_handler(void)
227 net_set_timeout_handler(CDP_TIMEOUT
, cdp_timeout_handler
);
232 /* if not OK try again */
236 net_set_state(NETLOOP_SUCCESS
);
239 void cdp_receive(const uchar
*pkt
, unsigned len
)
247 if (len
< sizeof(cdp_snap_hdr
) + 4)
250 /* check for valid CDP SNAP header */
251 if (memcmp(pkt
, cdp_snap_hdr
, sizeof(cdp_snap_hdr
)) != 0)
254 pkt
+= sizeof(cdp_snap_hdr
);
255 len
-= sizeof(cdp_snap_hdr
);
257 /* Version of CDP protocol must be >= 2 and TTL != 0 */
258 if (pkt
[0] < 0x02 || pkt
[1] == 0)
262 * if version is greater than 0x02 maybe we'll have a problem;
266 printf("**WARNING: CDP packet received with a protocol version "
267 "%d > 2\n", pkt
[0] & 0xff);
269 if (cdp_compute_csum(pkt
, len
) != 0)
281 ss
= (const ushort
*)pkt
;
290 ss
+= 2; /* point ss to the data of the TLV */
294 case CDP_DEVICE_ID_TLV
:
296 case CDP_ADDRESS_TLV
:
298 case CDP_PORT_ID_TLV
:
300 case CDP_CAPABILITIES_TLV
:
302 case CDP_VERSION_TLV
:
304 case CDP_PLATFORM_TLV
:
306 case CDP_NATIVE_VLAN_TLV
:
309 case CDP_APPLIANCE_VLAN_TLV
:
310 t
= (const uchar
*)ss
;
315 ss
= (const ushort
*)(t
+ 1);
317 #ifdef CONFIG_CDP_APPLIANCE_VLAN_TYPE
318 if (t
[0] == CONFIG_CDP_APPLIANCE_VLAN_TYPE
)
321 /* XXX will this work; dunno */
327 case CDP_TRIGGER_TLV
:
329 case CDP_POWER_CONSUMPTION_TLV
:
331 case CDP_SYSNAME_TLV
:
333 case CDP_SYSOBJECT_TLV
:
335 case CDP_MANAGEMENT_ADDRESS_TLV
:
340 cdp_appliance_vlan
= vlan
;
341 cdp_native_vlan
= nvlan
;
347 printf("** CDP packet is too short\n");
353 printf("Using %s device\n", eth_get_name());
357 cdp_native_vlan
= htons(-1);
358 cdp_appliance_vlan
= htons(-1);
360 net_set_timeout_handler(CDP_TIMEOUT
, cdp_timeout_handler
);