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 */
43 ushort CDPApplianceVLAN
;
45 static const uchar CDP_SNAP_hdr
[8] = {
46 0xAA, 0xAA, 0x03, 0x00, 0x00, 0x0C, 0x20, 0x00 };
49 CDP_compute_csum(const uchar
*buff
, ushort len
)
58 odd
= 1 & (ulong
)buff
;
68 if (result
& 0x80000000)
69 result
= (result
& 0xFFFF) + (result
>> 16);
73 leftover
= (signed short)(*(const signed char *)buff
);
75 * CISCO SUCKS big time! (and blows too):
76 * CDP uses the IP checksum algorithm with a twist;
77 * for the last byte it *sign* extends and sums.
79 result
= (result
& 0xffff0000) |
80 ((result
+ leftover
) & 0x0000ffff);
83 result
= (result
& 0xFFFF) + (result
>> 16);
86 result
= ((result
>> 8) & 0xff) |
87 ((result
& 0xff) << 8);
90 /* add up 16-bit and 17-bit words for 17+c bits */
91 result
= (result
& 0xffff) + (result
>> 16);
92 /* add up 16-bit and 2-bit for 16+c bit */
93 result
= (result
& 0xffff) + (result
>> 16);
95 result
= (result
& 0xffff) + (result
>> 16);
98 csum
= ~(ushort
)result
;
100 /* run time endian detection */
101 if (csum
!= htons(csum
)) /* little endian */
113 struct ethernet_hdr
*et
;
116 #if defined(CONFIG_CDP_DEVICE_ID) || defined(CONFIG_CDP_PORT_ID) || \
117 defined(CONFIG_CDP_VERSION) || defined(CONFIG_CDP_PLATFORM)
122 et
= (struct ethernet_hdr
*)pkt
;
124 /* NOTE: trigger sent not on any VLAN */
126 /* form ethernet header */
127 memcpy(et
->et_dest
, net_cdp_ethaddr
, 6);
128 memcpy(et
->et_src
, net_ethaddr
, 6);
130 pkt
+= ETHER_HDR_SIZE
;
133 memcpy((uchar
*)pkt
, CDP_SNAP_hdr
, sizeof(CDP_SNAP_hdr
));
134 pkt
+= sizeof(CDP_SNAP_hdr
);
137 *pkt
++ = 0x02; /* CDP version 2 */
138 *pkt
++ = 180; /* TTL */
141 /* checksum (0 for later calculation) */
145 #ifdef CONFIG_CDP_DEVICE_ID
146 *s
++ = htons(CDP_DEVICE_ID_TLV
);
147 *s
++ = htons(CONFIG_CDP_DEVICE_ID
);
148 sprintf(buf
, CONFIG_CDP_DEVICE_ID_PREFIX
"%pm", net_ethaddr
);
149 memcpy((uchar
*)s
, buf
, 16);
153 #ifdef CONFIG_CDP_PORT_ID
154 *s
++ = htons(CDP_PORT_ID_TLV
);
155 memset(buf
, 0, sizeof(buf
));
156 sprintf(buf
, CONFIG_CDP_PORT_ID
, eth_get_dev_index());
158 if (len
& 1) /* make it even */
160 *s
++ = htons(len
+ 4);
161 memcpy((uchar
*)s
, buf
, len
);
165 #ifdef CONFIG_CDP_CAPABILITIES
166 *s
++ = htons(CDP_CAPABILITIES_TLV
);
168 *(ulong
*)s
= htonl(CONFIG_CDP_CAPABILITIES
);
172 #ifdef CONFIG_CDP_VERSION
173 *s
++ = htons(CDP_VERSION_TLV
);
174 memset(buf
, 0, sizeof(buf
));
175 strcpy(buf
, CONFIG_CDP_VERSION
);
177 if (len
& 1) /* make it even */
179 *s
++ = htons(len
+ 4);
180 memcpy((uchar
*)s
, buf
, len
);
184 #ifdef CONFIG_CDP_PLATFORM
185 *s
++ = htons(CDP_PLATFORM_TLV
);
186 memset(buf
, 0, sizeof(buf
));
187 strcpy(buf
, CONFIG_CDP_PLATFORM
);
189 if (len
& 1) /* make it even */
191 *s
++ = htons(len
+ 4);
192 memcpy((uchar
*)s
, buf
, len
);
196 #ifdef CONFIG_CDP_TRIGGER
197 *s
++ = htons(CDP_TRIGGER_TLV
);
199 *(ulong
*)s
= htonl(CONFIG_CDP_TRIGGER
);
203 #ifdef CONFIG_CDP_POWER_CONSUMPTION
204 *s
++ = htons(CDP_POWER_CONSUMPTION_TLV
);
206 *s
++ = htons(CONFIG_CDP_POWER_CONSUMPTION
);
209 /* length of ethernet packet */
210 len
= (uchar
*)s
- ((uchar
*)NetTxPacket
+ ETHER_HDR_SIZE
);
211 et
->et_protlen
= htons(len
);
213 len
= ETHER_HDR_SIZE
+ sizeof(CDP_SNAP_hdr
);
214 chksum
= CDP_compute_csum((uchar
*)NetTxPacket
+ len
,
215 (uchar
*)s
- (NetTxPacket
+ len
));
220 NetSendPacket(NetTxPacket
, (uchar
*)s
- NetTxPacket
);
230 NetSetTimeout(CDP_TIMEOUT
, CDPTimeout
);
235 /* if not OK try again */
239 net_set_state(NETLOOP_SUCCESS
);
242 void cdp_receive(const uchar
*pkt
, unsigned len
)
250 if (len
< sizeof(CDP_SNAP_hdr
) + 4)
253 /* check for valid CDP SNAP header */
254 if (memcmp(pkt
, CDP_SNAP_hdr
, sizeof(CDP_SNAP_hdr
)) != 0)
257 pkt
+= sizeof(CDP_SNAP_hdr
);
258 len
-= sizeof(CDP_SNAP_hdr
);
260 /* Version of CDP protocol must be >= 2 and TTL != 0 */
261 if (pkt
[0] < 0x02 || pkt
[1] == 0)
265 * if version is greater than 0x02 maybe we'll have a problem;
269 printf("**WARNING: CDP packet received with a protocol version "
270 "%d > 2\n", pkt
[0] & 0xff);
272 if (CDP_compute_csum(pkt
, len
) != 0)
284 ss
= (const ushort
*)pkt
;
293 ss
+= 2; /* point ss to the data of the TLV */
297 case CDP_DEVICE_ID_TLV
:
299 case CDP_ADDRESS_TLV
:
301 case CDP_PORT_ID_TLV
:
303 case CDP_CAPABILITIES_TLV
:
305 case CDP_VERSION_TLV
:
307 case CDP_PLATFORM_TLV
:
309 case CDP_NATIVE_VLAN_TLV
:
312 case CDP_APPLIANCE_VLAN_TLV
:
313 t
= (const uchar
*)ss
;
318 ss
= (const ushort
*)(t
+ 1);
320 #ifdef CONFIG_CDP_APPLIANCE_VLAN_TYPE
321 if (t
[0] == CONFIG_CDP_APPLIANCE_VLAN_TYPE
)
324 /* XXX will this work; dunno */
330 case CDP_TRIGGER_TLV
:
332 case CDP_POWER_CONSUMPTION_TLV
:
334 case CDP_SYSNAME_TLV
:
336 case CDP_SYSOBJECT_TLV
:
338 case CDP_MANAGEMENT_ADDRESS_TLV
:
343 CDPApplianceVLAN
= vlan
;
344 CDPNativeVLAN
= nvlan
;
350 printf("** CDP packet is too short\n");
357 printf("Using %s device\n", eth_get_name());
361 CDPNativeVLAN
= htons(-1);
362 CDPApplianceVLAN
= htons(-1);
364 NetSetTimeout(CDP_TIMEOUT
, CDPTimeout
);