llc_pdu.h 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444
  1. #ifndef LLC_PDU_H
  2. #define LLC_PDU_H
  3. /*
  4. * Copyright (c) 1997 by Procom Technology,Inc.
  5. * 2001-2003 by Arnaldo Carvalho de Melo <[email protected]>
  6. *
  7. * This program can be redistributed or modified under the terms of the
  8. * GNU General Public License as published by the Free Software Foundation.
  9. * This program is distributed without any warranty or implied warranty
  10. * of merchantability or fitness for a particular purpose.
  11. *
  12. * See the GNU General Public License for more details.
  13. */
  14. #include <linux/if_ether.h>
  15. /* Lengths of frame formats */
  16. #define LLC_PDU_LEN_I 4 /* header and 2 control bytes */
  17. #define LLC_PDU_LEN_S 4
  18. #define LLC_PDU_LEN_U 3 /* header and 1 control byte */
  19. /* header and 1 control byte and XID info */
  20. #define LLC_PDU_LEN_U_XID (LLC_PDU_LEN_U + sizeof(struct llc_xid_info))
  21. /* Known SAP addresses */
  22. #define LLC_GLOBAL_SAP 0xFF
  23. #define LLC_NULL_SAP 0x00 /* not network-layer visible */
  24. #define LLC_MGMT_INDIV 0x02 /* station LLC mgmt indiv addr */
  25. #define LLC_MGMT_GRP 0x03 /* station LLC mgmt group addr */
  26. #define LLC_RDE_SAP 0xA6 /* route ... */
  27. /* SAP field bit masks */
  28. #define LLC_ISO_RESERVED_SAP 0x02
  29. #define LLC_SAP_GROUP_DSAP 0x01
  30. #define LLC_SAP_RESP_SSAP 0x01
  31. /* Group/individual DSAP indicator is DSAP field */
  32. #define LLC_PDU_GROUP_DSAP_MASK 0x01
  33. #define LLC_PDU_IS_GROUP_DSAP(pdu) \
  34. ((pdu->dsap & LLC_PDU_GROUP_DSAP_MASK) ? 0 : 1)
  35. #define LLC_PDU_IS_INDIV_DSAP(pdu) \
  36. (!(pdu->dsap & LLC_PDU_GROUP_DSAP_MASK) ? 0 : 1)
  37. /* Command/response PDU indicator in SSAP field */
  38. #define LLC_PDU_CMD_RSP_MASK 0x01
  39. #define LLC_PDU_CMD 0
  40. #define LLC_PDU_RSP 1
  41. #define LLC_PDU_IS_CMD(pdu) ((pdu->ssap & LLC_PDU_RSP) ? 0 : 1)
  42. #define LLC_PDU_IS_RSP(pdu) ((pdu->ssap & LLC_PDU_RSP) ? 1 : 0)
  43. /* Get PDU type from 2 lowest-order bits of control field first byte */
  44. #define LLC_PDU_TYPE_I_MASK 0x01 /* 16-bit control field */
  45. #define LLC_PDU_TYPE_S_MASK 0x03
  46. #define LLC_PDU_TYPE_U_MASK 0x03 /* 8-bit control field */
  47. #define LLC_PDU_TYPE_MASK 0x03
  48. #define LLC_PDU_TYPE_I 0 /* first bit */
  49. #define LLC_PDU_TYPE_S 1 /* first two bits */
  50. #define LLC_PDU_TYPE_U 3 /* first two bits */
  51. #define LLC_PDU_TYPE_U_XID 4 /* private type for detecting XID commands */
  52. #define LLC_PDU_TYPE_IS_I(pdu) \
  53. ((!(pdu->ctrl_1 & LLC_PDU_TYPE_I_MASK)) ? 1 : 0)
  54. #define LLC_PDU_TYPE_IS_U(pdu) \
  55. (((pdu->ctrl_1 & LLC_PDU_TYPE_U_MASK) == LLC_PDU_TYPE_U) ? 1 : 0)
  56. #define LLC_PDU_TYPE_IS_S(pdu) \
  57. (((pdu->ctrl_1 & LLC_PDU_TYPE_S_MASK) == LLC_PDU_TYPE_S) ? 1 : 0)
  58. /* U-format PDU control field masks */
  59. #define LLC_U_PF_BIT_MASK 0x10 /* P/F bit mask */
  60. #define LLC_U_PF_IS_1(pdu) ((pdu->ctrl_1 & LLC_U_PF_BIT_MASK) ? 1 : 0)
  61. #define LLC_U_PF_IS_0(pdu) ((!(pdu->ctrl_1 & LLC_U_PF_BIT_MASK)) ? 1 : 0)
  62. #define LLC_U_PDU_CMD_MASK 0xEC /* cmd/rsp mask */
  63. #define LLC_U_PDU_CMD(pdu) (pdu->ctrl_1 & LLC_U_PDU_CMD_MASK)
  64. #define LLC_U_PDU_RSP(pdu) (pdu->ctrl_1 & LLC_U_PDU_CMD_MASK)
  65. #define LLC_1_PDU_CMD_UI 0x00 /* Type 1 cmds/rsps */
  66. #define LLC_1_PDU_CMD_XID 0xAC
  67. #define LLC_1_PDU_CMD_TEST 0xE0
  68. #define LLC_2_PDU_CMD_SABME 0x6C /* Type 2 cmds/rsps */
  69. #define LLC_2_PDU_CMD_DISC 0x40
  70. #define LLC_2_PDU_RSP_UA 0x60
  71. #define LLC_2_PDU_RSP_DM 0x0C
  72. #define LLC_2_PDU_RSP_FRMR 0x84
  73. /* Type 1 operations */
  74. /* XID information field bit masks */
  75. /* LLC format identifier (byte 1) */
  76. #define LLC_XID_FMT_ID 0x81 /* first byte must be this */
  77. /* LLC types/classes identifier (byte 2) */
  78. #define LLC_XID_CLASS_ZEROS_MASK 0xE0 /* these must be zeros */
  79. #define LLC_XID_CLASS_MASK 0x1F /* AND with byte to get below */
  80. #define LLC_XID_NULL_CLASS_1 0x01 /* if NULL LSAP...use these */
  81. #define LLC_XID_NULL_CLASS_2 0x03
  82. #define LLC_XID_NULL_CLASS_3 0x05
  83. #define LLC_XID_NULL_CLASS_4 0x07
  84. #define LLC_XID_NNULL_TYPE_1 0x01 /* if non-NULL LSAP...use these */
  85. #define LLC_XID_NNULL_TYPE_2 0x02
  86. #define LLC_XID_NNULL_TYPE_3 0x04
  87. #define LLC_XID_NNULL_TYPE_1_2 0x03
  88. #define LLC_XID_NNULL_TYPE_1_3 0x05
  89. #define LLC_XID_NNULL_TYPE_2_3 0x06
  90. #define LLC_XID_NNULL_ALL 0x07
  91. /* Sender Receive Window (byte 3) */
  92. #define LLC_XID_RW_MASK 0xFE /* AND with value to get below */
  93. #define LLC_XID_MIN_RW 0x02 /* lowest-order bit always zero */
  94. /* Type 2 operations */
  95. #define LLC_2_SEQ_NBR_MODULO ((u8) 128)
  96. /* I-PDU masks ('ctrl' is I-PDU control word) */
  97. #define LLC_I_GET_NS(pdu) (u8)((pdu->ctrl_1 & 0xFE) >> 1)
  98. #define LLC_I_GET_NR(pdu) (u8)((pdu->ctrl_2 & 0xFE) >> 1)
  99. #define LLC_I_PF_BIT_MASK 0x01
  100. #define LLC_I_PF_IS_0(pdu) ((!(pdu->ctrl_2 & LLC_I_PF_BIT_MASK)) ? 1 : 0)
  101. #define LLC_I_PF_IS_1(pdu) ((pdu->ctrl_2 & LLC_I_PF_BIT_MASK) ? 1 : 0)
  102. /* S-PDU supervisory commands and responses */
  103. #define LLC_S_PDU_CMD_MASK 0x0C
  104. #define LLC_S_PDU_CMD(pdu) (pdu->ctrl_1 & LLC_S_PDU_CMD_MASK)
  105. #define LLC_S_PDU_RSP(pdu) (pdu->ctrl_1 & LLC_S_PDU_CMD_MASK)
  106. #define LLC_2_PDU_CMD_RR 0x00 /* rx ready cmd */
  107. #define LLC_2_PDU_RSP_RR 0x00 /* rx ready rsp */
  108. #define LLC_2_PDU_CMD_REJ 0x08 /* reject PDU cmd */
  109. #define LLC_2_PDU_RSP_REJ 0x08 /* reject PDU rsp */
  110. #define LLC_2_PDU_CMD_RNR 0x04 /* rx not ready cmd */
  111. #define LLC_2_PDU_RSP_RNR 0x04 /* rx not ready rsp */
  112. #define LLC_S_PF_BIT_MASK 0x01
  113. #define LLC_S_PF_IS_0(pdu) ((!(pdu->ctrl_2 & LLC_S_PF_BIT_MASK)) ? 1 : 0)
  114. #define LLC_S_PF_IS_1(pdu) ((pdu->ctrl_2 & LLC_S_PF_BIT_MASK) ? 1 : 0)
  115. #define PDU_SUPV_GET_Nr(pdu) ((pdu->ctrl_2 & 0xFE) >> 1)
  116. #define PDU_GET_NEXT_Vr(sn) (((sn) + 1) & ~LLC_2_SEQ_NBR_MODULO)
  117. /* FRMR information field macros */
  118. #define FRMR_INFO_LENGTH 5 /* 5 bytes of information */
  119. /*
  120. * info is pointer to FRMR info field structure; 'rej_ctrl' is byte pointer
  121. * (if U-PDU) or word pointer to rejected PDU control field
  122. */
  123. #define FRMR_INFO_SET_REJ_CNTRL(info,rej_ctrl) \
  124. info->rej_pdu_ctrl = ((*((u8 *) rej_ctrl) & \
  125. LLC_PDU_TYPE_U) != LLC_PDU_TYPE_U ? \
  126. (u16)*((u16 *) rej_ctrl) : \
  127. (((u16) *((u8 *) rej_ctrl)) & 0x00FF))
  128. /*
  129. * Info is pointer to FRMR info field structure; 'vs' is a byte containing
  130. * send state variable value in low-order 7 bits (insure the lowest-order
  131. * bit remains zero (0))
  132. */
  133. #define FRMR_INFO_SET_Vs(info,vs) (info->curr_ssv = (((u8) vs) << 1))
  134. #define FRMR_INFO_SET_Vr(info,vr) (info->curr_rsv = (((u8) vr) << 1))
  135. /*
  136. * Info is pointer to FRMR info field structure; 'cr' is a byte containing
  137. * the C/R bit value in the low-order bit
  138. */
  139. #define FRMR_INFO_SET_C_R_BIT(info, cr) (info->curr_rsv |= (((u8) cr) & 0x01))
  140. /*
  141. * In the remaining five macros, 'info' is pointer to FRMR info field
  142. * structure; 'ind' is a byte containing the bit value to set in the
  143. * lowest-order bit)
  144. */
  145. #define FRMR_INFO_SET_INVALID_PDU_CTRL_IND(info, ind) \
  146. (info->ind_bits = ((info->ind_bits & 0xFE) | (((u8) ind) & 0x01)))
  147. #define FRMR_INFO_SET_INVALID_PDU_INFO_IND(info, ind) \
  148. (info->ind_bits = ( (info->ind_bits & 0xFD) | (((u8) ind) & 0x02)))
  149. #define FRMR_INFO_SET_PDU_INFO_2LONG_IND(info, ind) \
  150. (info->ind_bits = ( (info->ind_bits & 0xFB) | (((u8) ind) & 0x04)))
  151. #define FRMR_INFO_SET_PDU_INVALID_Nr_IND(info, ind) \
  152. (info->ind_bits = ( (info->ind_bits & 0xF7) | (((u8) ind) & 0x08)))
  153. #define FRMR_INFO_SET_PDU_INVALID_Ns_IND(info, ind) \
  154. (info->ind_bits = ( (info->ind_bits & 0xEF) | (((u8) ind) & 0x10)))
  155. /* Sequence-numbered PDU format (4 bytes in length) */
  156. struct llc_pdu_sn {
  157. u8 dsap;
  158. u8 ssap;
  159. u8 ctrl_1;
  160. u8 ctrl_2;
  161. } __packed;
  162. static inline struct llc_pdu_sn *llc_pdu_sn_hdr(struct sk_buff *skb)
  163. {
  164. return (struct llc_pdu_sn *)skb_network_header(skb);
  165. }
  166. /* Un-numbered PDU format (3 bytes in length) */
  167. struct llc_pdu_un {
  168. u8 dsap;
  169. u8 ssap;
  170. u8 ctrl_1;
  171. } __packed;
  172. static inline struct llc_pdu_un *llc_pdu_un_hdr(struct sk_buff *skb)
  173. {
  174. return (struct llc_pdu_un *)skb_network_header(skb);
  175. }
  176. /**
  177. * llc_pdu_header_init - initializes pdu header
  178. * @skb: input skb that header must be set into it.
  179. * @type: type of PDU (U, I or S).
  180. * @ssap: source sap.
  181. * @dsap: destination sap.
  182. * @cr: command/response bit (0 or 1).
  183. *
  184. * This function sets DSAP, SSAP and command/Response bit in LLC header.
  185. */
  186. static inline void llc_pdu_header_init(struct sk_buff *skb, u8 type,
  187. u8 ssap, u8 dsap, u8 cr)
  188. {
  189. int hlen = 4; /* default value for I and S types */
  190. struct llc_pdu_un *pdu;
  191. switch (type) {
  192. case LLC_PDU_TYPE_U:
  193. hlen = 3;
  194. break;
  195. case LLC_PDU_TYPE_U_XID:
  196. hlen = 6;
  197. break;
  198. }
  199. skb_push(skb, hlen);
  200. skb_reset_network_header(skb);
  201. pdu = llc_pdu_un_hdr(skb);
  202. pdu->dsap = dsap;
  203. pdu->ssap = ssap;
  204. pdu->ssap |= cr;
  205. }
  206. /**
  207. * llc_pdu_decode_sa - extracs source address (MAC) of input frame
  208. * @skb: input skb that source address must be extracted from it.
  209. * @sa: pointer to source address (6 byte array).
  210. *
  211. * This function extracts source address(MAC) of input frame.
  212. */
  213. static inline void llc_pdu_decode_sa(struct sk_buff *skb, u8 *sa)
  214. {
  215. if (skb->protocol == htons(ETH_P_802_2))
  216. memcpy(sa, eth_hdr(skb)->h_source, ETH_ALEN);
  217. }
  218. /**
  219. * llc_pdu_decode_da - extracts dest address of input frame
  220. * @skb: input skb that destination address must be extracted from it
  221. * @sa: pointer to destination address (6 byte array).
  222. *
  223. * This function extracts destination address(MAC) of input frame.
  224. */
  225. static inline void llc_pdu_decode_da(struct sk_buff *skb, u8 *da)
  226. {
  227. if (skb->protocol == htons(ETH_P_802_2))
  228. memcpy(da, eth_hdr(skb)->h_dest, ETH_ALEN);
  229. }
  230. /**
  231. * llc_pdu_decode_ssap - extracts source SAP of input frame
  232. * @skb: input skb that source SAP must be extracted from it.
  233. * @ssap: source SAP (output argument).
  234. *
  235. * This function extracts source SAP of input frame. Right bit of SSAP is
  236. * command/response bit.
  237. */
  238. static inline void llc_pdu_decode_ssap(struct sk_buff *skb, u8 *ssap)
  239. {
  240. *ssap = llc_pdu_un_hdr(skb)->ssap & 0xFE;
  241. }
  242. /**
  243. * llc_pdu_decode_dsap - extracts dest SAP of input frame
  244. * @skb: input skb that destination SAP must be extracted from it.
  245. * @dsap: destination SAP (output argument).
  246. *
  247. * This function extracts destination SAP of input frame. right bit of
  248. * DSAP designates individual/group SAP.
  249. */
  250. static inline void llc_pdu_decode_dsap(struct sk_buff *skb, u8 *dsap)
  251. {
  252. *dsap = llc_pdu_un_hdr(skb)->dsap & 0xFE;
  253. }
  254. /**
  255. * llc_pdu_init_as_ui_cmd - sets LLC header as UI PDU
  256. * @skb: input skb that header must be set into it.
  257. *
  258. * This function sets third byte of LLC header as a UI PDU.
  259. */
  260. static inline void llc_pdu_init_as_ui_cmd(struct sk_buff *skb)
  261. {
  262. struct llc_pdu_un *pdu = llc_pdu_un_hdr(skb);
  263. pdu->ctrl_1 = LLC_PDU_TYPE_U;
  264. pdu->ctrl_1 |= LLC_1_PDU_CMD_UI;
  265. }
  266. /**
  267. * llc_pdu_init_as_test_cmd - sets PDU as TEST
  268. * @skb - Address of the skb to build
  269. *
  270. * Sets a PDU as TEST
  271. */
  272. static inline void llc_pdu_init_as_test_cmd(struct sk_buff *skb)
  273. {
  274. struct llc_pdu_un *pdu = llc_pdu_un_hdr(skb);
  275. pdu->ctrl_1 = LLC_PDU_TYPE_U;
  276. pdu->ctrl_1 |= LLC_1_PDU_CMD_TEST;
  277. pdu->ctrl_1 |= LLC_U_PF_BIT_MASK;
  278. }
  279. /**
  280. * llc_pdu_init_as_test_rsp - build TEST response PDU
  281. * @skb: Address of the skb to build
  282. * @ev_skb: The received TEST command PDU frame
  283. *
  284. * Builds a pdu frame as a TEST response.
  285. */
  286. static inline void llc_pdu_init_as_test_rsp(struct sk_buff *skb,
  287. struct sk_buff *ev_skb)
  288. {
  289. struct llc_pdu_un *pdu = llc_pdu_un_hdr(skb);
  290. pdu->ctrl_1 = LLC_PDU_TYPE_U;
  291. pdu->ctrl_1 |= LLC_1_PDU_CMD_TEST;
  292. pdu->ctrl_1 |= LLC_U_PF_BIT_MASK;
  293. if (ev_skb->protocol == htons(ETH_P_802_2)) {
  294. struct llc_pdu_un *ev_pdu = llc_pdu_un_hdr(ev_skb);
  295. int dsize;
  296. dsize = ntohs(eth_hdr(ev_skb)->h_proto) - 3;
  297. memcpy(((u8 *)pdu) + 3, ((u8 *)ev_pdu) + 3, dsize);
  298. skb_put(skb, dsize);
  299. }
  300. }
  301. /* LLC Type 1 XID command/response information fields format */
  302. struct llc_xid_info {
  303. u8 fmt_id; /* always 0x81 for LLC */
  304. u8 type; /* different if NULL/non-NULL LSAP */
  305. u8 rw; /* sender receive window */
  306. } __packed;
  307. /**
  308. * llc_pdu_init_as_xid_cmd - sets bytes 3, 4 & 5 of LLC header as XID
  309. * @skb: input skb that header must be set into it.
  310. *
  311. * This function sets third,fourth,fifth and sixth bytes of LLC header as
  312. * a XID PDU.
  313. */
  314. static inline void llc_pdu_init_as_xid_cmd(struct sk_buff *skb,
  315. u8 svcs_supported, u8 rx_window)
  316. {
  317. struct llc_xid_info *xid_info;
  318. struct llc_pdu_un *pdu = llc_pdu_un_hdr(skb);
  319. pdu->ctrl_1 = LLC_PDU_TYPE_U;
  320. pdu->ctrl_1 |= LLC_1_PDU_CMD_XID;
  321. pdu->ctrl_1 |= LLC_U_PF_BIT_MASK;
  322. xid_info = (struct llc_xid_info *)(((u8 *)&pdu->ctrl_1) + 1);
  323. xid_info->fmt_id = LLC_XID_FMT_ID; /* 0x81 */
  324. xid_info->type = svcs_supported;
  325. xid_info->rw = rx_window << 1; /* size of receive window */
  326. /* no need to push/put since llc_pdu_header_init() has already
  327. * pushed 3 + 3 bytes
  328. */
  329. }
  330. /**
  331. * llc_pdu_init_as_xid_rsp - builds XID response PDU
  332. * @skb: Address of the skb to build
  333. * @svcs_supported: The class of the LLC (I or II)
  334. * @rx_window: The size of the receive window of the LLC
  335. *
  336. * Builds a pdu frame as an XID response.
  337. */
  338. static inline void llc_pdu_init_as_xid_rsp(struct sk_buff *skb,
  339. u8 svcs_supported, u8 rx_window)
  340. {
  341. struct llc_xid_info *xid_info;
  342. struct llc_pdu_un *pdu = llc_pdu_un_hdr(skb);
  343. pdu->ctrl_1 = LLC_PDU_TYPE_U;
  344. pdu->ctrl_1 |= LLC_1_PDU_CMD_XID;
  345. pdu->ctrl_1 |= LLC_U_PF_BIT_MASK;
  346. xid_info = (struct llc_xid_info *)(((u8 *)&pdu->ctrl_1) + 1);
  347. xid_info->fmt_id = LLC_XID_FMT_ID;
  348. xid_info->type = svcs_supported;
  349. xid_info->rw = rx_window << 1;
  350. skb_put(skb, sizeof(struct llc_xid_info));
  351. }
  352. /* LLC Type 2 FRMR response information field format */
  353. struct llc_frmr_info {
  354. u16 rej_pdu_ctrl; /* bits 1-8 if U-PDU */
  355. u8 curr_ssv; /* current send state variable val */
  356. u8 curr_rsv; /* current receive state variable */
  357. u8 ind_bits; /* indicator bits set with macro */
  358. } __packed;
  359. void llc_pdu_set_cmd_rsp(struct sk_buff *skb, u8 type);
  360. void llc_pdu_set_pf_bit(struct sk_buff *skb, u8 bit_value);
  361. void llc_pdu_decode_pf_bit(struct sk_buff *skb, u8 *pf_bit);
  362. void llc_pdu_init_as_disc_cmd(struct sk_buff *skb, u8 p_bit);
  363. void llc_pdu_init_as_i_cmd(struct sk_buff *skb, u8 p_bit, u8 ns, u8 nr);
  364. void llc_pdu_init_as_rej_cmd(struct sk_buff *skb, u8 p_bit, u8 nr);
  365. void llc_pdu_init_as_rnr_cmd(struct sk_buff *skb, u8 p_bit, u8 nr);
  366. void llc_pdu_init_as_rr_cmd(struct sk_buff *skb, u8 p_bit, u8 nr);
  367. void llc_pdu_init_as_sabme_cmd(struct sk_buff *skb, u8 p_bit);
  368. void llc_pdu_init_as_dm_rsp(struct sk_buff *skb, u8 f_bit);
  369. void llc_pdu_init_as_frmr_rsp(struct sk_buff *skb, struct llc_pdu_sn *prev_pdu,
  370. u8 f_bit, u8 vs, u8 vr, u8 vzyxw);
  371. void llc_pdu_init_as_rr_rsp(struct sk_buff *skb, u8 f_bit, u8 nr);
  372. void llc_pdu_init_as_rej_rsp(struct sk_buff *skb, u8 f_bit, u8 nr);
  373. void llc_pdu_init_as_rnr_rsp(struct sk_buff *skb, u8 f_bit, u8 nr);
  374. void llc_pdu_init_as_ua_rsp(struct sk_buff *skb, u8 f_bit);
  375. #endif /* LLC_PDU_H */