Leaked source code of windows server 2003
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

704 lines
20 KiB

  1. /*++
  2. Copyright (c) 1991 Microsoft Corporation
  3. Module Name:
  4. llcaddr.c
  5. Abstract:
  6. This module provides a network independent interface to build
  7. lan headers for the sent and received frames.
  8. This is a re-entarnt library module having no access to the data
  9. structures of data link module.
  10. Contents:
  11. LlcBuildAddress
  12. LlcBuildAddressFromLanHeader
  13. LlcGetMaxInfoField
  14. LlcCopyReceivedLanHeader
  15. CopyLanHeader
  16. Author:
  17. Antti Saarenheimo (o-anttis) 30-MAY-1991
  18. Revision History:
  19. --*/
  20. #include <llc.h>
  21. #define FDDI_FC 0x57 // Frame Control: async, 48-bit address, LLC, pri 7
  22. static USHORT ausLongestFrameTranslation[8] = {
  23. 516,
  24. 1470,
  25. 2052,
  26. 4472,
  27. 8144,
  28. 11407,
  29. 17800,
  30. 17800
  31. };
  32. UINT
  33. LlcBuildAddress(
  34. IN NDIS_MEDIUM NdisMedium,
  35. IN PUCHAR DestinationAddress,
  36. IN PVOID pSrcRouting,
  37. IN OUT PUCHAR pLanHeader
  38. )
  39. /*++
  40. Routine Description:
  41. The primitive implements a network independent handling of
  42. the address information in the LAN header. It builds LAN a
  43. 802.3, DIX or 802.5 header from an address and an optional source
  44. routing info. The source SAP number and the current node address are
  45. provided by the link driver.
  46. Arguments:
  47. NdisMedium - medium that MAC talks over
  48. DestinationAddress - LAN destination address (or a broadcast/multicast/
  49. functional address).
  50. pSrcRouting - optional source routing information. Must be set
  51. NULL when not used.
  52. pLanHeader - buffer provided by the upper protocol for the
  53. address information of the frame header.
  54. Return Value:
  55. length of the LAN header
  56. --*/
  57. {
  58. UCHAR SourceRoutingIndicator = 0x01;
  59. UINT minimumLanHeader = 14;
  60. if (NdisMedium == NdisMediumFddi) {
  61. pLanHeader[0] = FDDI_FC;
  62. ++pLanHeader;
  63. minimumLanHeader = 13;
  64. } else if (NdisMedium == NdisMedium802_5) {
  65. pLanHeader[0] = 0; // DEFAULT_TR_ACCESS;
  66. pLanHeader[1] = 0x40; // NON_MAC_FRAME;
  67. pLanHeader += 2;
  68. SourceRoutingIndicator = 0x80;
  69. } else {
  70. //
  71. // We build always the DIX ethernet header, even if it is not used
  72. // when the link works in 802.3. IN the automatic mode
  73. // we need just to copy the lan header length from all received U-
  74. // command frames and we are using the same mode as the other side.
  75. // However, there is a problem: the remote node may response
  76. // the SAMBE request with Rxx or even with I-frame, but we
  77. // don't want to put any extra checking on our main code path
  78. // (U- command frames would definitely be outside of it).
  79. // The other side will send RR- if it cannot get any acknowledge
  80. // from us, and after a while it closes the link.
  81. //-----
  82. // We considered the using of connect timeout to change the
  83. // ethernet mode, but that might have procedured too long waithing
  84. // times (something 1s and 30s)
  85. //-----
  86. // Usually the client tries to connect to the host and the client
  87. // sends first the data. We may also assume, that IBM host machines
  88. // use the standard LLC implementations (SABME is always acknowledged
  89. // by UA)
  90. //
  91. pLanHeader[12] = 0x80;
  92. pLanHeader[13] = 0xD5;
  93. }
  94. LlcMemCpy(pLanHeader, DestinationAddress, 6);
  95. //
  96. // We must support the source routing information also for
  97. // ethernet, because the underlaying network could be token-ring.
  98. // The source routing bit must be a local variable, because it
  99. // is different in token-ring and ethernet
  100. //
  101. if (!pSrcRouting) {
  102. pLanHeader[6] &= ~SourceRoutingIndicator;
  103. return minimumLanHeader;
  104. } else {
  105. pLanHeader[6] |= SourceRoutingIndicator;
  106. pLanHeader[0] &= ~SourceRoutingIndicator;
  107. //
  108. // 5 Lowest bits are the source routing information length.
  109. //
  110. if ( (*((PUCHAR)pSrcRouting) & 0x1f) > MAX_TR_SRC_ROUTING_INFO) {
  111. return DLC_STATUS_INVALID_ROUTING_INFO;
  112. }
  113. LlcMemCpy(&pLanHeader[12],
  114. pSrcRouting,
  115. *((PUCHAR)pSrcRouting) & 0x1f // Length
  116. );
  117. return minimumLanHeader + *((PUCHAR)pSrcRouting) & 0x1f;
  118. }
  119. }
  120. UINT
  121. LlcBuildAddressFromLanHeader(
  122. IN NDIS_MEDIUM NdisMedium,
  123. IN PUCHAR pRcvLanHeader,
  124. IN OUT PUCHAR pLanHeader
  125. )
  126. /*++
  127. Routine Description:
  128. The routine builds a transmit lan header form the received frame and
  129. returns the frames header length, nul if invalid address
  130. Arguments:
  131. NdisMedium - Ndis menium of the address information
  132. pRcvFrameHeader - pointer to any frame received from the network.
  133. The buffer must include the whole LAN header of the frame
  134. including the LLC header.
  135. pLanHeader - buffer to receive the information (minimum size is 32 bytes)
  136. Return Value:
  137. None
  138. --*/
  139. {
  140. UINT LlcOffset;
  141. if (NdisMedium == NdisMedium802_3) {
  142. #ifndef SUPPORT_ETHERNET_CLIENT
  143. LlcOffset = LlcBuildAddress(NdisMedium,
  144. &pRcvLanHeader[6],
  145. NULL,
  146. pLanHeader
  147. );
  148. #else
  149. LlcOffset = LlcBuildAddress(NdisMedium,
  150. &pRcvLanHeader[6],
  151. (pRcvLanHeader[6] & 0x01)
  152. ? &pRcvLanHeader[12]
  153. : NULL,
  154. pLanHeader
  155. );
  156. //
  157. // We must swap the direction bit and reset the possible
  158. // broadcast type in the source routing header.
  159. //
  160. if (pLanHeader[6] & 0x01) {
  161. pLanHeader[12] &= 0x1f;
  162. pLanHeader[13] ^= 0x80;
  163. }
  164. #endif
  165. } else if (NdisMedium == NdisMediumFddi) {
  166. LlcOffset = LlcBuildAddress(NdisMedium,
  167. &pRcvLanHeader[7],
  168. NULL, // no source routing in FDDI
  169. pLanHeader
  170. );
  171. } else {
  172. LlcOffset = LlcBuildAddress(NdisMedium,
  173. &pRcvLanHeader[8],
  174. (pRcvLanHeader[8] & 0x80)
  175. ? &pRcvLanHeader[14]
  176. : NULL,
  177. pLanHeader
  178. );
  179. //
  180. // We must swap the direction bit and reset the possible broadcast type
  181. //
  182. if (pLanHeader[8] & 0x80) {
  183. pLanHeader[14] &= 0x1f;
  184. pLanHeader[15] ^= 0x80;
  185. }
  186. }
  187. return LlcOffset;
  188. }
  189. USHORT
  190. LlcGetMaxInfoField(
  191. IN NDIS_MEDIUM NdisMedium,
  192. IN PVOID hBinding,
  193. IN PUCHAR pLanHeader
  194. )
  195. /*++
  196. Routine Description:
  197. Procedure returns the maximum informatiuon field for
  198. the given LAN header. It checks both the source routing
  199. information and the maximum packet length defined for
  200. the adapter and decrements the LAN and LLC headers
  201. from the length
  202. Arguments:
  203. NdisMedium - Ndis medium of the address information
  204. pBinding - current binding context on data link driver
  205. pLanHeader - any lan header
  206. Return Value:
  207. Maximum information field length
  208. --*/
  209. {
  210. PUCHAR pSourceRouting;
  211. UCHAR LanHeaderLength = 14;
  212. USHORT MaxFrameSize;
  213. USHORT usMaxBridgeSize;
  214. MaxFrameSize = (USHORT)((PBINDING_CONTEXT)hBinding)->pAdapterContext->MaxFrameSize;
  215. //
  216. // We may run DLC within the DIX in ethernet network, so add 3 bytes for
  217. // SNA DIX LAN header and padding
  218. //
  219. if (((PBINDING_CONTEXT)hBinding)->pAdapterContext->NdisMedium == NdisMedium802_3) {
  220. LanHeaderLength += 3;
  221. }
  222. pSourceRouting = NULL;
  223. if (NdisMedium == NdisMedium802_5) {
  224. if (pLanHeader[8] & 0x80) {
  225. pSourceRouting = pLanHeader + 14;
  226. }
  227. } else if (NdisMedium == NdisMediumFddi) {
  228. if (pLanHeader[6] & 0x80) {
  229. pSourceRouting = pLanHeader + 13;
  230. }
  231. } else {
  232. if (pLanHeader[6] & 0x01) {
  233. pSourceRouting = pLanHeader + 12;
  234. }
  235. }
  236. if (pSourceRouting != NULL) {
  237. //
  238. // Add the source routing info length to the LAN header length
  239. //
  240. LanHeaderLength += (UCHAR)(*pSourceRouting & (UCHAR)0x1f);
  241. usMaxBridgeSize = ausLongestFrameTranslation[(pSourceRouting[1] & SRC_ROUTING_LF_MASK) >> 4];
  242. //
  243. // RLF 10/01/92. Ignore the 'bridge size'. This is misleading. For
  244. // instance, the IBM mainframe is currently sending RI of 02 80 in
  245. // all frames, indicating that the max I-frame it can accept/transmit
  246. // is 516 bytes (x000xxxx in byte #2). It later sends us a 712 byte
  247. // frame, 683 bytes of which are info field. We reject it (FRMR)
  248. //
  249. //
  250. // must work this out properly
  251. //
  252. //if (MaxFrameSize > usMaxBridgeSize) {
  253. // MaxFrameSize = usMaxBridgeSize;
  254. //}
  255. }
  256. return (USHORT)(MaxFrameSize - LanHeaderLength - sizeof(LLC_HEADER));
  257. }
  258. UINT
  259. LlcCopyReceivedLanHeader(
  260. IN PBINDING_CONTEXT pBinding,
  261. IN PUCHAR DestinationAddress,
  262. IN PUCHAR SourceAddress
  263. )
  264. /*++
  265. Routine Description:
  266. Function copies and translates the received lan header to
  267. the address format used by the client. By default the
  268. source is the current received frame.
  269. Arguments:
  270. pBinding - pointer to Binding Context
  271. DestinationAddress - pointer to output destination net address
  272. SourceAddress - pointer to input source net address
  273. Return Value:
  274. Length of copied LAN header
  275. --*/
  276. {
  277. if (SourceAddress == NULL) {
  278. SourceAddress = pBinding->pAdapterContext->pHeadBuf;
  279. }
  280. if (pBinding->pAdapterContext->FrameType == LLC_DIRECT_ETHERNET_TYPE) {
  281. //
  282. // when receiving a DIX frame, the LAN header is always the 12 bytes
  283. // before the ethernet type field
  284. //
  285. LlcMemCpy(DestinationAddress, SourceAddress, 12);
  286. return 12;
  287. } else {
  288. UCHAR LanHeaderLength = 14;
  289. switch (pBinding->AddressTranslation) {
  290. case LLC_SEND_802_5_TO_802_5:
  291. //
  292. // Copy also the source routing info, if it has been defined
  293. //
  294. LlcMemCpy(DestinationAddress,
  295. SourceAddress,
  296. (SourceAddress[8] & 0x80)
  297. ? LanHeaderLength + (SourceAddress[14] & 0x1f)
  298. : LanHeaderLength
  299. );
  300. break;
  301. case LLC_SEND_802_5_TO_802_3:
  302. case LLC_SEND_802_5_TO_DIX:
  303. DestinationAddress[0] = 0; // default AC
  304. DestinationAddress[1] = 0x40; // default frame type: non-MAC
  305. //
  306. // RLF 03/31/93
  307. //
  308. // we are receiving an Ethernet frame. We always reverse the bits
  309. // in the destination address (ie our node address). We reverse the
  310. // source (sender's node address) based on the SwapAddressBits flag.
  311. // If this is TRUE (default) then we also swap the source address
  312. // bits so that ethernet addresses are presented to the app in
  313. // standard non-canonical format. If SwapAddressBits is FALSE then
  314. // we leave them in canonical format (a real ethernet address) or
  315. // presumably the address is a Token Ring address from the other
  316. // side of an ethernet-token ring bridge
  317. //
  318. SwappingMemCpy(&DestinationAddress[2],
  319. SourceAddress,
  320. 6
  321. );
  322. SwapMemCpy(pBinding->pAdapterContext->ConfigInfo.SwapAddressBits,
  323. &DestinationAddress[8],
  324. SourceAddress+6,
  325. 6
  326. );
  327. break;
  328. case LLC_SEND_802_5_TO_FDDI:
  329. DestinationAddress[0] = 0;
  330. DestinationAddress[1] = 0x40;
  331. SwappingMemCpy(&DestinationAddress[2],
  332. SourceAddress+1,
  333. 6
  334. );
  335. SwapMemCpy(pBinding->pAdapterContext->ConfigInfo.SwapAddressBits,
  336. &DestinationAddress[8],
  337. SourceAddress+7,
  338. 6
  339. );
  340. break;
  341. #ifdef SUPPORT_ETHERNET_CLIENT
  342. case LLC_SEND_802_3_TO_802_3:
  343. case LLC_SEND_802_3_TO_DIX:
  344. LlcMemCpy(DestinationAddress, SourceAddress, LanHeaderLength);
  345. break;
  346. case LLC_SEND_802_3_TO_802_5:
  347. //
  348. // check the source routing bit
  349. //
  350. SwappingMemCpy(DestinationAddress, &SourceAddress[2], 12);
  351. if (SourceAddress[8] & 0x80) {
  352. LanHeaderLength += SourceAddress[14] & 0x1f;
  353. LlcMemCpy(&DestinationAddress[12],
  354. &SourceAddress[14],
  355. SourceAddress[14] & 0x1f
  356. );
  357. }
  358. break;
  359. #endif
  360. case LLC_SEND_FDDI_TO_FDDI:
  361. DestinationAddress[0] = FDDI_FC;
  362. LlcMemCpy(DestinationAddress+1, SourceAddress, 12);
  363. return 13;
  364. #if LLC_DBG
  365. default:
  366. LlcInvalidObjectType();
  367. break;
  368. #endif
  369. }
  370. return LanHeaderLength;
  371. }
  372. }
  373. UCHAR
  374. CopyLanHeader(
  375. IN UINT AddressTranslationMode,
  376. IN PUCHAR pSrcLanHeader,
  377. IN PUCHAR pNodeAddress,
  378. OUT PUCHAR pDestLanHeader,
  379. IN BOOLEAN SwapAddressBits
  380. )
  381. /*++
  382. Routine Description:
  383. The primitive translates the given LAN header and its type to
  384. a real network header and patches the local node address to
  385. the source address field. It also returns the length
  386. of the LAN header (== offset of LLC header).
  387. Arguments:
  388. AddressTranslationMode - the network format mapping case
  389. pSrcLanHeader - the initial LAN header
  390. pNodeAddress - the current node address
  391. pDestLanHeader - storage for the new LAN header
  392. SwapAddressBits - TRUE if we will swap address bits for Ethernet/FDDI
  393. Return Value:
  394. Length of the built network header
  395. --*/
  396. {
  397. UCHAR LlcOffset = 14;
  398. UCHAR NodeAddressOffset = 6;
  399. UCHAR SourceRoutingFlag = 0;
  400. //
  401. // LLC driver API supports both 802.3 (ethernet) and 802.5 (token-ring)
  402. // address presentation formats. The 802.3 header may include the source
  403. // routing information, when it is used on token-ring.
  404. //
  405. // Internally LLC supports 802.3, DIX and 802.5 networks.
  406. // The transport level driver needs to support only one format. It is
  407. // translated to the actual network header by LLC.
  408. // Thus we have these six address mappings.
  409. //
  410. switch (AddressTranslationMode) {
  411. case LLC_SEND_802_5_TO_802_5:
  412. //
  413. // TOKEN-RING 802.5 -> TOKEN-RING 802.5
  414. //
  415. NodeAddressOffset = 8;
  416. LlcMemCpy(pDestLanHeader, pSrcLanHeader, 8);
  417. //
  418. // set the AC & FC bytes: FC = 0x40 == LLC-level frame
  419. //
  420. // THIS MAY NOT BE THE CORRECT PLACE TO DO THIS, UNLESS MAC
  421. // LEVEL FRAMES CHANGE FC BACK TO 0x00 AFTER THIS ROUTINE AND
  422. // BEFORE THE FRAME IS PUT ON THE WIRE
  423. //
  424. pDestLanHeader[0] = 0; // AC = no pritority
  425. pDestLanHeader[1] = 0x40; // FS = Non-MAC
  426. SourceRoutingFlag = pSrcLanHeader[8] & (UCHAR)0x80;
  427. if (SourceRoutingFlag) {
  428. //
  429. // Copy the source routing info
  430. //
  431. LlcOffset += pSrcLanHeader[14] & 0x1f;
  432. LlcMemCpy(&pDestLanHeader[14],
  433. &pSrcLanHeader[14],
  434. pSrcLanHeader[14] & 0x1f
  435. );
  436. }
  437. break;
  438. case LLC_SEND_802_5_TO_DIX:
  439. //
  440. // TOKEN-RING -> DIX-ETHERNET
  441. //
  442. // The ethernet type is a small endiand!!
  443. //
  444. pDestLanHeader[12] = 0x80;
  445. pDestLanHeader[13] = 0xD5;
  446. LlcOffset = 17;
  447. //
  448. // **** FALL THROUGH ****
  449. //
  450. case LLC_SEND_802_5_TO_802_3:
  451. //
  452. // TOKEN-RING 802.5 -> ETHERNET 802.3
  453. //
  454. //
  455. // RLF 03/31/93
  456. //
  457. // Once again, we swap the bits in the destination address ONLY if the
  458. // SwapAddressBits flag is TRUE.
  459. // This *could* be a frame intended to go to an ethernet-token ring
  460. // bridge. The bridge may or may not swap the destination address bits
  461. // depending on whether there's a 'y' in the month
  462. //
  463. SwapMemCpy(SwapAddressBits,
  464. pDestLanHeader,
  465. &pSrcLanHeader[2],
  466. 6
  467. );
  468. break;
  469. case LLC_SEND_802_5_TO_FDDI:
  470. pDestLanHeader[0] = FDDI_FC;
  471. SwapMemCpy(SwapAddressBits, pDestLanHeader+1, &pSrcLanHeader[2], 6);
  472. NodeAddressOffset = 7;
  473. LlcOffset = 13;
  474. break;
  475. case LLC_SEND_DIX_TO_DIX:
  476. LlcOffset = 12;
  477. case LLC_SEND_802_3_TO_802_3:
  478. //
  479. // ETHERNET 802.3 -> ETHERNET 802.3
  480. //
  481. LlcMemCpy(pDestLanHeader, pSrcLanHeader, 6);
  482. break;
  483. case LLC_SEND_802_3_TO_DIX:
  484. //
  485. // The ethernet type is a small endiand!!
  486. //
  487. pDestLanHeader[12] = 0x80;
  488. pDestLanHeader[13] = 0xD5;
  489. LlcOffset = 17;
  490. //
  491. // ETHERNET 802.3 -> DIX-ETHERNET
  492. //
  493. LlcMemCpy(pDestLanHeader, pSrcLanHeader, 6);
  494. break;
  495. #ifdef SUPPORT_ETHERNET_CLIENT
  496. case LLC_SEND_802_3_TO_802_5:
  497. //
  498. // ETHERNET 802.3 -> TOKEN-RING 802.5
  499. //
  500. NodeAddressOffset = 8;
  501. pDestLanHeader[0] = 0; // AC = no pritority
  502. pDestLanHeader[1] = 0x40; // FS = Non-MAC
  503. SwappingMemCpy(pDestLanHeader + 2, pSrcLanHeader, 6);
  504. //
  505. // Note: Ethernet source routing info indication flag is swapped!
  506. //
  507. if (pSrcLanHeader[6] & 0x01) {
  508. SourceRoutingFlag = 0x80;
  509. //
  510. // Copy the source routing info, the source routing info
  511. // must always be in token-ring bit order (reverse)
  512. //
  513. pDestLanHeader[8] |= 0x80;
  514. LlcOffset += pSrcLanHeader[12] & 0x1f;
  515. LlcMemCpy(&pDestLanHeader[14],
  516. &pSrcLanHeader[12],
  517. pSrcLanHeader[12] & 0x1f
  518. );
  519. }
  520. break;
  521. #endif
  522. case LLC_SEND_UNMODIFIED:
  523. return 0;
  524. break;
  525. case LLC_SEND_FDDI_TO_FDDI:
  526. LlcMemCpy(pDestLanHeader, pSrcLanHeader, 7);
  527. NodeAddressOffset = 7;
  528. LlcOffset = 13;
  529. break;
  530. case LLC_SEND_FDDI_TO_802_5:
  531. break;
  532. case LLC_SEND_FDDI_TO_802_3:
  533. break;
  534. #if LLC_DBG
  535. default:
  536. LlcInvalidObjectType();
  537. break;
  538. #endif
  539. }
  540. //
  541. // copy the source address from the node address (ie. our adapter's address)
  542. // in the correct format for the medium (canonical or non-canonical address
  543. // format)
  544. //
  545. LlcMemCpy(&pDestLanHeader[NodeAddressOffset], pNodeAddress, 6);
  546. pDestLanHeader[NodeAddressOffset] |= SourceRoutingFlag;
  547. return LlcOffset;
  548. }