llc_pdu.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372
  1. /*
  2. * llc_pdu.c - access to PDU internals
  3. *
  4. * Copyright (c) 1997 by Procom Technology, Inc.
  5. * 2001-2003 by Arnaldo Carvalho de Melo <acme@conectiva.com.br>
  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/netdevice.h>
  15. #include <net/llc_pdu.h>
  16. static void llc_pdu_decode_pdu_type(struct sk_buff *skb, u8 *type);
  17. static u8 llc_pdu_get_pf_bit(struct llc_pdu_sn *pdu);
  18. void llc_pdu_set_cmd_rsp(struct sk_buff *skb, u8 pdu_type)
  19. {
  20. llc_pdu_un_hdr(skb)->ssap |= pdu_type;
  21. }
  22. /**
  23. * pdu_set_pf_bit - sets poll/final bit in LLC header
  24. * @pdu_frame: input frame that p/f bit must be set into it.
  25. * @bit_value: poll/final bit (0 or 1).
  26. *
  27. * This function sets poll/final bit in LLC header (based on type of PDU).
  28. * in I or S pdus, p/f bit is right bit of fourth byte in header. in U
  29. * pdus p/f bit is fifth bit of third byte.
  30. */
  31. void llc_pdu_set_pf_bit(struct sk_buff *skb, u8 bit_value)
  32. {
  33. u8 pdu_type;
  34. struct llc_pdu_sn *pdu;
  35. llc_pdu_decode_pdu_type(skb, &pdu_type);
  36. pdu = llc_pdu_sn_hdr(skb);
  37. switch (pdu_type) {
  38. case LLC_PDU_TYPE_I:
  39. case LLC_PDU_TYPE_S:
  40. pdu->ctrl_2 = (pdu->ctrl_2 & 0xFE) | bit_value;
  41. break;
  42. case LLC_PDU_TYPE_U:
  43. pdu->ctrl_1 |= (pdu->ctrl_1 & 0xEF) | (bit_value << 4);
  44. break;
  45. }
  46. }
  47. /**
  48. * llc_pdu_decode_pf_bit - extracs poll/final bit from LLC header
  49. * @skb: input skb that p/f bit must be extracted from it
  50. * @pf_bit: poll/final bit (0 or 1)
  51. *
  52. * This function extracts poll/final bit from LLC header (based on type of
  53. * PDU). In I or S pdus, p/f bit is right bit of fourth byte in header. In
  54. * U pdus p/f bit is fifth bit of third byte.
  55. */
  56. void llc_pdu_decode_pf_bit(struct sk_buff *skb, u8 *pf_bit)
  57. {
  58. u8 pdu_type;
  59. struct llc_pdu_sn *pdu;
  60. llc_pdu_decode_pdu_type(skb, &pdu_type);
  61. pdu = llc_pdu_sn_hdr(skb);
  62. switch (pdu_type) {
  63. case LLC_PDU_TYPE_I:
  64. case LLC_PDU_TYPE_S:
  65. *pf_bit = pdu->ctrl_2 & LLC_S_PF_BIT_MASK;
  66. break;
  67. case LLC_PDU_TYPE_U:
  68. *pf_bit = (pdu->ctrl_1 & LLC_U_PF_BIT_MASK) >> 4;
  69. break;
  70. }
  71. }
  72. /**
  73. * llc_pdu_init_as_disc_cmd - Builds DISC PDU
  74. * @skb: Address of the skb to build
  75. * @p_bit: The P bit to set in the PDU
  76. *
  77. * Builds a pdu frame as a DISC command.
  78. */
  79. void llc_pdu_init_as_disc_cmd(struct sk_buff *skb, u8 p_bit)
  80. {
  81. struct llc_pdu_un *pdu = llc_pdu_un_hdr(skb);
  82. pdu->ctrl_1 = LLC_PDU_TYPE_U;
  83. pdu->ctrl_1 |= LLC_2_PDU_CMD_DISC;
  84. pdu->ctrl_1 |= ((p_bit & 1) << 4) & LLC_U_PF_BIT_MASK;
  85. }
  86. /**
  87. * llc_pdu_init_as_i_cmd - builds I pdu
  88. * @skb: Address of the skb to build
  89. * @p_bit: The P bit to set in the PDU
  90. * @ns: The sequence number of the data PDU
  91. * @nr: The seq. number of the expected I PDU from the remote
  92. *
  93. * Builds a pdu frame as an I command.
  94. */
  95. void llc_pdu_init_as_i_cmd(struct sk_buff *skb, u8 p_bit, u8 ns, u8 nr)
  96. {
  97. struct llc_pdu_sn *pdu = llc_pdu_sn_hdr(skb);
  98. pdu->ctrl_1 = LLC_PDU_TYPE_I;
  99. pdu->ctrl_2 = 0;
  100. pdu->ctrl_2 |= (p_bit & LLC_I_PF_BIT_MASK); /* p/f bit */
  101. pdu->ctrl_1 |= (ns << 1) & 0xFE; /* set N(S) in bits 2..8 */
  102. pdu->ctrl_2 |= (nr << 1) & 0xFE; /* set N(R) in bits 10..16 */
  103. }
  104. /**
  105. * llc_pdu_init_as_rej_cmd - builds REJ PDU
  106. * @skb: Address of the skb to build
  107. * @p_bit: The P bit to set in the PDU
  108. * @nr: The seq. number of the expected I PDU from the remote
  109. *
  110. * Builds a pdu frame as a REJ command.
  111. */
  112. void llc_pdu_init_as_rej_cmd(struct sk_buff *skb, u8 p_bit, u8 nr)
  113. {
  114. struct llc_pdu_sn *pdu = llc_pdu_sn_hdr(skb);
  115. pdu->ctrl_1 = LLC_PDU_TYPE_S;
  116. pdu->ctrl_1 |= LLC_2_PDU_CMD_REJ;
  117. pdu->ctrl_2 = 0;
  118. pdu->ctrl_2 |= p_bit & LLC_S_PF_BIT_MASK;
  119. pdu->ctrl_1 &= 0x0F; /* setting bits 5..8 to zero(reserved) */
  120. pdu->ctrl_2 |= (nr << 1) & 0xFE; /* set N(R) in bits 10..16 */
  121. }
  122. /**
  123. * llc_pdu_init_as_rnr_cmd - builds RNR pdu
  124. * @skb: Address of the skb to build
  125. * @p_bit: The P bit to set in the PDU
  126. * @nr: The seq. number of the expected I PDU from the remote
  127. *
  128. * Builds a pdu frame as an RNR command.
  129. */
  130. void llc_pdu_init_as_rnr_cmd(struct sk_buff *skb, u8 p_bit, u8 nr)
  131. {
  132. struct llc_pdu_sn *pdu = llc_pdu_sn_hdr(skb);
  133. pdu->ctrl_1 = LLC_PDU_TYPE_S;
  134. pdu->ctrl_1 |= LLC_2_PDU_CMD_RNR;
  135. pdu->ctrl_2 = 0;
  136. pdu->ctrl_2 |= p_bit & LLC_S_PF_BIT_MASK;
  137. pdu->ctrl_1 &= 0x0F; /* setting bits 5..8 to zero(reserved) */
  138. pdu->ctrl_2 |= (nr << 1) & 0xFE; /* set N(R) in bits 10..16 */
  139. }
  140. /**
  141. * llc_pdu_init_as_rr_cmd - Builds RR pdu
  142. * @skb: Address of the skb to build
  143. * @p_bit: The P bit to set in the PDU
  144. * @nr: The seq. number of the expected I PDU from the remote
  145. *
  146. * Builds a pdu frame as an RR command.
  147. */
  148. void llc_pdu_init_as_rr_cmd(struct sk_buff *skb, u8 p_bit, u8 nr)
  149. {
  150. struct llc_pdu_sn *pdu = llc_pdu_sn_hdr(skb);
  151. pdu->ctrl_1 = LLC_PDU_TYPE_S;
  152. pdu->ctrl_1 |= LLC_2_PDU_CMD_RR;
  153. pdu->ctrl_2 = p_bit & LLC_S_PF_BIT_MASK;
  154. pdu->ctrl_1 &= 0x0F; /* setting bits 5..8 to zero(reserved) */
  155. pdu->ctrl_2 |= (nr << 1) & 0xFE; /* set N(R) in bits 10..16 */
  156. }
  157. /**
  158. * llc_pdu_init_as_sabme_cmd - builds SABME pdu
  159. * @skb: Address of the skb to build
  160. * @p_bit: The P bit to set in the PDU
  161. *
  162. * Builds a pdu frame as an SABME command.
  163. */
  164. void llc_pdu_init_as_sabme_cmd(struct sk_buff *skb, u8 p_bit)
  165. {
  166. struct llc_pdu_un *pdu = llc_pdu_un_hdr(skb);
  167. pdu->ctrl_1 = LLC_PDU_TYPE_U;
  168. pdu->ctrl_1 |= LLC_2_PDU_CMD_SABME;
  169. pdu->ctrl_1 |= ((p_bit & 1) << 4) & LLC_U_PF_BIT_MASK;
  170. }
  171. /**
  172. * llc_pdu_init_as_dm_rsp - builds DM response pdu
  173. * @skb: Address of the skb to build
  174. * @f_bit: The F bit to set in the PDU
  175. *
  176. * Builds a pdu frame as a DM response.
  177. */
  178. void llc_pdu_init_as_dm_rsp(struct sk_buff *skb, u8 f_bit)
  179. {
  180. struct llc_pdu_un *pdu = llc_pdu_un_hdr(skb);
  181. pdu->ctrl_1 = LLC_PDU_TYPE_U;
  182. pdu->ctrl_1 |= LLC_2_PDU_RSP_DM;
  183. pdu->ctrl_1 |= ((f_bit & 1) << 4) & LLC_U_PF_BIT_MASK;
  184. }
  185. /**
  186. * llc_pdu_init_as_frmr_rsp - builds FRMR response PDU
  187. * @skb: Address of the frame to build
  188. * @prev_pdu: The rejected PDU frame
  189. * @f_bit: The F bit to set in the PDU
  190. * @vs: tx state vari value for the data link conn at the rejecting LLC
  191. * @vr: rx state var value for the data link conn at the rejecting LLC
  192. * @vzyxw: completely described in the IEEE Std 802.2 document (Pg 55)
  193. *
  194. * Builds a pdu frame as a FRMR response.
  195. */
  196. void llc_pdu_init_as_frmr_rsp(struct sk_buff *skb, struct llc_pdu_sn *prev_pdu,
  197. u8 f_bit, u8 vs, u8 vr, u8 vzyxw)
  198. {
  199. struct llc_frmr_info *frmr_info;
  200. u8 prev_pf = 0;
  201. u8 *ctrl;
  202. struct llc_pdu_sn *pdu = llc_pdu_sn_hdr(skb);
  203. pdu->ctrl_1 = LLC_PDU_TYPE_U;
  204. pdu->ctrl_1 |= LLC_2_PDU_RSP_FRMR;
  205. pdu->ctrl_1 |= ((f_bit & 1) << 4) & LLC_U_PF_BIT_MASK;
  206. frmr_info = (struct llc_frmr_info *)&pdu->ctrl_2;
  207. ctrl = (u8 *)&prev_pdu->ctrl_1;
  208. FRMR_INFO_SET_REJ_CNTRL(frmr_info,ctrl);
  209. FRMR_INFO_SET_Vs(frmr_info, vs);
  210. FRMR_INFO_SET_Vr(frmr_info, vr);
  211. prev_pf = llc_pdu_get_pf_bit(prev_pdu);
  212. FRMR_INFO_SET_C_R_BIT(frmr_info, prev_pf);
  213. FRMR_INFO_SET_INVALID_PDU_CTRL_IND(frmr_info, vzyxw);
  214. FRMR_INFO_SET_INVALID_PDU_INFO_IND(frmr_info, vzyxw);
  215. FRMR_INFO_SET_PDU_INFO_2LONG_IND(frmr_info, vzyxw);
  216. FRMR_INFO_SET_PDU_INVALID_Nr_IND(frmr_info, vzyxw);
  217. FRMR_INFO_SET_PDU_INVALID_Ns_IND(frmr_info, vzyxw);
  218. skb_put(skb, 5);
  219. }
  220. /**
  221. * llc_pdu_init_as_rr_rsp - builds RR response pdu
  222. * @skb: Address of the skb to build
  223. * @f_bit: The F bit to set in the PDU
  224. * @nr: The seq. number of the expected data PDU from the remote
  225. *
  226. * Builds a pdu frame as an RR response.
  227. */
  228. void llc_pdu_init_as_rr_rsp(struct sk_buff *skb, u8 f_bit, u8 nr)
  229. {
  230. struct llc_pdu_sn *pdu = llc_pdu_sn_hdr(skb);
  231. pdu->ctrl_1 = LLC_PDU_TYPE_S;
  232. pdu->ctrl_1 |= LLC_2_PDU_RSP_RR;
  233. pdu->ctrl_2 = 0;
  234. pdu->ctrl_2 |= f_bit & LLC_S_PF_BIT_MASK;
  235. pdu->ctrl_1 &= 0x0F; /* setting bits 5..8 to zero(reserved) */
  236. pdu->ctrl_2 |= (nr << 1) & 0xFE; /* set N(R) in bits 10..16 */
  237. }
  238. /**
  239. * llc_pdu_init_as_rej_rsp - builds REJ response pdu
  240. * @skb: Address of the skb to build
  241. * @f_bit: The F bit to set in the PDU
  242. * @nr: The seq. number of the expected data PDU from the remote
  243. *
  244. * Builds a pdu frame as a REJ response.
  245. */
  246. void llc_pdu_init_as_rej_rsp(struct sk_buff *skb, u8 f_bit, u8 nr)
  247. {
  248. struct llc_pdu_sn *pdu = llc_pdu_sn_hdr(skb);
  249. pdu->ctrl_1 = LLC_PDU_TYPE_S;
  250. pdu->ctrl_1 |= LLC_2_PDU_RSP_REJ;
  251. pdu->ctrl_2 = 0;
  252. pdu->ctrl_2 |= f_bit & LLC_S_PF_BIT_MASK;
  253. pdu->ctrl_1 &= 0x0F; /* setting bits 5..8 to zero(reserved) */
  254. pdu->ctrl_2 |= (nr << 1) & 0xFE; /* set N(R) in bits 10..16 */
  255. }
  256. /**
  257. * llc_pdu_init_as_rnr_rsp - builds RNR response pdu
  258. * @skb: Address of the frame to build
  259. * @f_bit: The F bit to set in the PDU
  260. * @nr: The seq. number of the expected data PDU from the remote
  261. *
  262. * Builds a pdu frame as an RNR response.
  263. */
  264. void llc_pdu_init_as_rnr_rsp(struct sk_buff *skb, u8 f_bit, u8 nr)
  265. {
  266. struct llc_pdu_sn *pdu = llc_pdu_sn_hdr(skb);
  267. pdu->ctrl_1 = LLC_PDU_TYPE_S;
  268. pdu->ctrl_1 |= LLC_2_PDU_RSP_RNR;
  269. pdu->ctrl_2 = 0;
  270. pdu->ctrl_2 |= f_bit & LLC_S_PF_BIT_MASK;
  271. pdu->ctrl_1 &= 0x0F; /* setting bits 5..8 to zero(reserved) */
  272. pdu->ctrl_2 |= (nr << 1) & 0xFE; /* set N(R) in bits 10..16 */
  273. }
  274. /**
  275. * llc_pdu_init_as_ua_rsp - builds UA response pdu
  276. * @skb: Address of the frame to build
  277. * @f_bit: The F bit to set in the PDU
  278. *
  279. * Builds a pdu frame as a UA response.
  280. */
  281. void llc_pdu_init_as_ua_rsp(struct sk_buff *skb, u8 f_bit)
  282. {
  283. struct llc_pdu_un *pdu = llc_pdu_un_hdr(skb);
  284. pdu->ctrl_1 = LLC_PDU_TYPE_U;
  285. pdu->ctrl_1 |= LLC_2_PDU_RSP_UA;
  286. pdu->ctrl_1 |= ((f_bit & 1) << 4) & LLC_U_PF_BIT_MASK;
  287. }
  288. /**
  289. * llc_pdu_decode_pdu_type - designates PDU type
  290. * @skb: input skb that type of it must be designated.
  291. * @type: type of PDU (output argument).
  292. *
  293. * This function designates type of PDU (I, S or U).
  294. */
  295. static void llc_pdu_decode_pdu_type(struct sk_buff *skb, u8 *type)
  296. {
  297. struct llc_pdu_un *pdu = llc_pdu_un_hdr(skb);
  298. if (pdu->ctrl_1 & 1) {
  299. if ((pdu->ctrl_1 & LLC_PDU_TYPE_U) == LLC_PDU_TYPE_U)
  300. *type = LLC_PDU_TYPE_U;
  301. else
  302. *type = LLC_PDU_TYPE_S;
  303. } else
  304. *type = LLC_PDU_TYPE_I;
  305. }
  306. /**
  307. * llc_pdu_get_pf_bit - extracts p/f bit of input PDU
  308. * @pdu: pointer to LLC header.
  309. *
  310. * This function extracts p/f bit of input PDU. at first examines type of
  311. * PDU and then extracts p/f bit. Returns the p/f bit.
  312. */
  313. static u8 llc_pdu_get_pf_bit(struct llc_pdu_sn *pdu)
  314. {
  315. u8 pdu_type;
  316. u8 pf_bit = 0;
  317. if (pdu->ctrl_1 & 1) {
  318. if ((pdu->ctrl_1 & LLC_PDU_TYPE_U) == LLC_PDU_TYPE_U)
  319. pdu_type = LLC_PDU_TYPE_U;
  320. else
  321. pdu_type = LLC_PDU_TYPE_S;
  322. } else
  323. pdu_type = LLC_PDU_TYPE_I;
  324. switch (pdu_type) {
  325. case LLC_PDU_TYPE_I:
  326. case LLC_PDU_TYPE_S:
  327. pf_bit = pdu->ctrl_2 & LLC_S_PF_BIT_MASK;
  328. break;
  329. case LLC_PDU_TYPE_U:
  330. pf_bit = (pdu->ctrl_1 & LLC_U_PF_BIT_MASK) >> 4;
  331. break;
  332. }
  333. return pf_bit;
  334. }