initial commit with v2.6.9
[linux-2.6.9-moxart.git] / net / llc / llc_pdu.c
blob66344fabd84def7e36682d6ef776906cdf488e86
1 /*
2 * llc_pdu.c - access to PDU internals
4 * Copyright (c) 1997 by Procom Technology, Inc.
5 * 2001-2003 by Arnaldo Carvalho de Melo <acme@conectiva.com.br>
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.
12 * See the GNU General Public License for more details.
15 #include <linux/netdevice.h>
16 #include <net/llc_pdu.h>
18 static void llc_pdu_decode_pdu_type(struct sk_buff *skb, u8 *type);
19 static u8 llc_pdu_get_pf_bit(struct llc_pdu_sn *pdu);
21 void llc_pdu_set_cmd_rsp(struct sk_buff *skb, u8 pdu_type)
23 llc_pdu_un_hdr(skb)->ssap |= pdu_type;
26 /**
27 * pdu_set_pf_bit - sets poll/final bit in LLC header
28 * @pdu_frame: input frame that p/f bit must be set into it.
29 * @bit_value: poll/final bit (0 or 1).
31 * This function sets poll/final bit in LLC header (based on type of PDU).
32 * in I or S pdus, p/f bit is right bit of fourth byte in header. in U
33 * pdus p/f bit is fifth bit of third byte.
35 void llc_pdu_set_pf_bit(struct sk_buff *skb, u8 bit_value)
37 u8 pdu_type;
38 struct llc_pdu_sn *pdu;
40 llc_pdu_decode_pdu_type(skb, &pdu_type);
41 pdu = llc_pdu_sn_hdr(skb);
43 switch (pdu_type) {
44 case LLC_PDU_TYPE_I:
45 case LLC_PDU_TYPE_S:
46 pdu->ctrl_2 = (pdu->ctrl_2 & 0xFE) | bit_value;
47 break;
48 case LLC_PDU_TYPE_U:
49 pdu->ctrl_1 |= (pdu->ctrl_1 & 0xEF) | (bit_value << 4);
50 break;
54 /**
55 * llc_pdu_decode_pf_bit - extracs poll/final bit from LLC header
56 * @skb: input skb that p/f bit must be extracted from it
57 * @pf_bit: poll/final bit (0 or 1)
59 * This function extracts poll/final bit from LLC header (based on type of
60 * PDU). In I or S pdus, p/f bit is right bit of fourth byte in header. In
61 * U pdus p/f bit is fifth bit of third byte.
63 void llc_pdu_decode_pf_bit(struct sk_buff *skb, u8 *pf_bit)
65 u8 pdu_type;
66 struct llc_pdu_sn *pdu;
68 llc_pdu_decode_pdu_type(skb, &pdu_type);
69 pdu = llc_pdu_sn_hdr(skb);
71 switch (pdu_type) {
72 case LLC_PDU_TYPE_I:
73 case LLC_PDU_TYPE_S:
74 *pf_bit = pdu->ctrl_2 & LLC_S_PF_BIT_MASK;
75 break;
76 case LLC_PDU_TYPE_U:
77 *pf_bit = (pdu->ctrl_1 & LLC_U_PF_BIT_MASK) >> 4;
78 break;
82 /**
83 * llc_pdu_decode_cr_bit - extracts command response bit from LLC header
84 * @skb: input skb that c/r bit must be extracted from it.
85 * @cr_bit: command/response bit (0 or 1).
87 * This function extracts command/response bit from LLC header. this bit
88 * is right bit of source SAP.
90 void llc_pdu_decode_cr_bit(struct sk_buff *skb, u8 *cr_bit)
92 *cr_bit = llc_pdu_un_hdr(skb)->ssap & LLC_PDU_CMD_RSP_MASK;
95 /**
96 * llc_pdu_init_as_disc_cmd - Builds DISC PDU
97 * @skb: Address of the skb to build
98 * @p_bit: The P bit to set in the PDU
100 * Builds a pdu frame as a DISC command.
102 void llc_pdu_init_as_disc_cmd(struct sk_buff *skb, u8 p_bit)
104 struct llc_pdu_un *pdu = llc_pdu_un_hdr(skb);
106 pdu->ctrl_1 = LLC_PDU_TYPE_U;
107 pdu->ctrl_1 |= LLC_2_PDU_CMD_DISC;
108 pdu->ctrl_1 |= ((p_bit & 1) << 4) & LLC_U_PF_BIT_MASK;
112 * llc_pdu_init_as_i_cmd - builds I pdu
113 * @skb: Address of the skb to build
114 * @p_bit: The P bit to set in the PDU
115 * @ns: The sequence number of the data PDU
116 * @nr: The seq. number of the expected I PDU from the remote
118 * Builds a pdu frame as an I command.
120 void llc_pdu_init_as_i_cmd(struct sk_buff *skb, u8 p_bit, u8 ns, u8 nr)
122 struct llc_pdu_sn *pdu = llc_pdu_sn_hdr(skb);
124 pdu->ctrl_1 = LLC_PDU_TYPE_I;
125 pdu->ctrl_2 = 0;
126 pdu->ctrl_2 |= (p_bit & LLC_I_PF_BIT_MASK); /* p/f bit */
127 pdu->ctrl_1 |= (ns << 1) & 0xFE; /* set N(S) in bits 2..8 */
128 pdu->ctrl_2 |= (nr << 1) & 0xFE; /* set N(R) in bits 10..16 */
132 * llc_pdu_init_as_rej_cmd - builds REJ PDU
133 * @skb: Address of the skb to build
134 * @p_bit: The P bit to set in the PDU
135 * @nr: The seq. number of the expected I PDU from the remote
137 * Builds a pdu frame as a REJ command.
139 void llc_pdu_init_as_rej_cmd(struct sk_buff *skb, u8 p_bit, u8 nr)
141 struct llc_pdu_sn *pdu = llc_pdu_sn_hdr(skb);
143 pdu->ctrl_1 = LLC_PDU_TYPE_S;
144 pdu->ctrl_1 |= LLC_2_PDU_CMD_REJ;
145 pdu->ctrl_2 = 0;
146 pdu->ctrl_2 |= p_bit & LLC_S_PF_BIT_MASK;
147 pdu->ctrl_1 &= 0x0F; /* setting bits 5..8 to zero(reserved) */
148 pdu->ctrl_2 |= (nr << 1) & 0xFE; /* set N(R) in bits 10..16 */
152 * llc_pdu_init_as_rnr_cmd - builds RNR pdu
153 * @skb: Address of the skb to build
154 * @p_bit: The P bit to set in the PDU
155 * @nr: The seq. number of the expected I PDU from the remote
157 * Builds a pdu frame as an RNR command.
159 void llc_pdu_init_as_rnr_cmd(struct sk_buff *skb, u8 p_bit, u8 nr)
161 struct llc_pdu_sn *pdu = llc_pdu_sn_hdr(skb);
163 pdu->ctrl_1 = LLC_PDU_TYPE_S;
164 pdu->ctrl_1 |= LLC_2_PDU_CMD_RNR;
165 pdu->ctrl_2 = 0;
166 pdu->ctrl_2 |= p_bit & LLC_S_PF_BIT_MASK;
167 pdu->ctrl_1 &= 0x0F; /* setting bits 5..8 to zero(reserved) */
168 pdu->ctrl_2 |= (nr << 1) & 0xFE; /* set N(R) in bits 10..16 */
172 * llc_pdu_init_as_rr_cmd - Builds RR pdu
173 * @skb: Address of the skb to build
174 * @p_bit: The P bit to set in the PDU
175 * @nr: The seq. number of the expected I PDU from the remote
177 * Builds a pdu frame as an RR command.
179 void llc_pdu_init_as_rr_cmd(struct sk_buff *skb, u8 p_bit, u8 nr)
181 struct llc_pdu_sn *pdu = llc_pdu_sn_hdr(skb);
183 pdu->ctrl_1 = LLC_PDU_TYPE_S;
184 pdu->ctrl_1 |= LLC_2_PDU_CMD_RR;
185 pdu->ctrl_2 = p_bit & LLC_S_PF_BIT_MASK;
186 pdu->ctrl_1 &= 0x0F; /* setting bits 5..8 to zero(reserved) */
187 pdu->ctrl_2 |= (nr << 1) & 0xFE; /* set N(R) in bits 10..16 */
191 * llc_pdu_init_as_sabme_cmd - builds SABME pdu
192 * @skb: Address of the skb to build
193 * @p_bit: The P bit to set in the PDU
195 * Builds a pdu frame as an SABME command.
197 void llc_pdu_init_as_sabme_cmd(struct sk_buff *skb, u8 p_bit)
199 struct llc_pdu_un *pdu = llc_pdu_un_hdr(skb);
201 pdu->ctrl_1 = LLC_PDU_TYPE_U;
202 pdu->ctrl_1 |= LLC_2_PDU_CMD_SABME;
203 pdu->ctrl_1 |= ((p_bit & 1) << 4) & LLC_U_PF_BIT_MASK;
207 * llc_pdu_init_as_dm_rsp - builds DM response pdu
208 * @skb: Address of the skb to build
209 * @f_bit: The F bit to set in the PDU
211 * Builds a pdu frame as a DM response.
213 void llc_pdu_init_as_dm_rsp(struct sk_buff *skb, u8 f_bit)
215 struct llc_pdu_un *pdu = llc_pdu_un_hdr(skb);
217 pdu->ctrl_1 = LLC_PDU_TYPE_U;
218 pdu->ctrl_1 |= LLC_2_PDU_RSP_DM;
219 pdu->ctrl_1 |= ((f_bit & 1) << 4) & LLC_U_PF_BIT_MASK;
223 * llc_pdu_init_as_frmr_rsp - builds FRMR response PDU
224 * @skb: Address of the frame to build
225 * @prev_pdu: The rejected PDU frame
226 * @f_bit: The F bit to set in the PDU
227 * @vs: tx state vari value for the data link conn at the rejecting LLC
228 * @vr: rx state var value for the data link conn at the rejecting LLC
229 * @vzyxw: completely described in the IEEE Std 802.2 document (Pg 55)
231 * Builds a pdu frame as a FRMR response.
233 void llc_pdu_init_as_frmr_rsp(struct sk_buff *skb, struct llc_pdu_sn *prev_pdu,
234 u8 f_bit, u8 vs, u8 vr, u8 vzyxw)
236 struct llc_frmr_info *frmr_info;
237 u8 prev_pf = 0;
238 u8 *ctrl;
239 struct llc_pdu_sn *pdu = llc_pdu_sn_hdr(skb);
241 pdu->ctrl_1 = LLC_PDU_TYPE_U;
242 pdu->ctrl_1 |= LLC_2_PDU_RSP_FRMR;
243 pdu->ctrl_1 |= ((f_bit & 1) << 4) & LLC_U_PF_BIT_MASK;
245 frmr_info = (struct llc_frmr_info *)&pdu->ctrl_2;
246 ctrl = (u8 *)&prev_pdu->ctrl_1;
247 FRMR_INFO_SET_REJ_CNTRL(frmr_info,ctrl);
248 FRMR_INFO_SET_Vs(frmr_info, vs);
249 FRMR_INFO_SET_Vr(frmr_info, vr);
250 prev_pf = llc_pdu_get_pf_bit(prev_pdu);
251 FRMR_INFO_SET_C_R_BIT(frmr_info, prev_pf);
252 FRMR_INFO_SET_INVALID_PDU_CTRL_IND(frmr_info, vzyxw);
253 FRMR_INFO_SET_INVALID_PDU_INFO_IND(frmr_info, vzyxw);
254 FRMR_INFO_SET_PDU_INFO_2LONG_IND(frmr_info, vzyxw);
255 FRMR_INFO_SET_PDU_INVALID_Nr_IND(frmr_info, vzyxw);
256 FRMR_INFO_SET_PDU_INVALID_Ns_IND(frmr_info, vzyxw);
257 skb_put(skb, 5);
261 * llc_pdu_init_as_rr_rsp - builds RR response pdu
262 * @skb: Address of the skb to build
263 * @f_bit: The F bit to set in the PDU
264 * @nr: The seq. number of the expected data PDU from the remote
266 * Builds a pdu frame as an RR response.
268 void llc_pdu_init_as_rr_rsp(struct sk_buff *skb, u8 f_bit, u8 nr)
270 struct llc_pdu_sn *pdu = llc_pdu_sn_hdr(skb);
272 pdu->ctrl_1 = LLC_PDU_TYPE_S;
273 pdu->ctrl_1 |= LLC_2_PDU_RSP_RR;
274 pdu->ctrl_2 = 0;
275 pdu->ctrl_2 |= f_bit & LLC_S_PF_BIT_MASK;
276 pdu->ctrl_1 &= 0x0F; /* setting bits 5..8 to zero(reserved) */
277 pdu->ctrl_2 |= (nr << 1) & 0xFE; /* set N(R) in bits 10..16 */
281 * llc_pdu_init_as_rej_rsp - builds REJ response pdu
282 * @skb: Address of the skb to build
283 * @f_bit: The F bit to set in the PDU
284 * @nr: The seq. number of the expected data PDU from the remote
286 * Builds a pdu frame as a REJ response.
288 void llc_pdu_init_as_rej_rsp(struct sk_buff *skb, u8 f_bit, u8 nr)
290 struct llc_pdu_sn *pdu = llc_pdu_sn_hdr(skb);
292 pdu->ctrl_1 = LLC_PDU_TYPE_S;
293 pdu->ctrl_1 |= LLC_2_PDU_RSP_REJ;
294 pdu->ctrl_2 = 0;
295 pdu->ctrl_2 |= f_bit & LLC_S_PF_BIT_MASK;
296 pdu->ctrl_1 &= 0x0F; /* setting bits 5..8 to zero(reserved) */
297 pdu->ctrl_2 |= (nr << 1) & 0xFE; /* set N(R) in bits 10..16 */
301 * llc_pdu_init_as_rnr_rsp - builds RNR response pdu
302 * @skb: Address of the frame to build
303 * @f_bit: The F bit to set in the PDU
304 * @nr: The seq. number of the expected data PDU from the remote
306 * Builds a pdu frame as an RNR response.
308 void llc_pdu_init_as_rnr_rsp(struct sk_buff *skb, u8 f_bit, u8 nr)
310 struct llc_pdu_sn *pdu = llc_pdu_sn_hdr(skb);
312 pdu->ctrl_1 = LLC_PDU_TYPE_S;
313 pdu->ctrl_1 |= LLC_2_PDU_RSP_RNR;
314 pdu->ctrl_2 = 0;
315 pdu->ctrl_2 |= f_bit & LLC_S_PF_BIT_MASK;
316 pdu->ctrl_1 &= 0x0F; /* setting bits 5..8 to zero(reserved) */
317 pdu->ctrl_2 |= (nr << 1) & 0xFE; /* set N(R) in bits 10..16 */
321 * llc_pdu_init_as_ua_rsp - builds UA response pdu
322 * @skb: Address of the frame to build
323 * @f_bit: The F bit to set in the PDU
325 * Builds a pdu frame as a UA response.
327 void llc_pdu_init_as_ua_rsp(struct sk_buff *skb, u8 f_bit)
329 struct llc_pdu_un *pdu = llc_pdu_un_hdr(skb);
331 pdu->ctrl_1 = LLC_PDU_TYPE_U;
332 pdu->ctrl_1 |= LLC_2_PDU_RSP_UA;
333 pdu->ctrl_1 |= ((f_bit & 1) << 4) & LLC_U_PF_BIT_MASK;
337 * llc_pdu_decode_pdu_type - designates PDU type
338 * @skb: input skb that type of it must be designated.
339 * @type: type of PDU (output argument).
341 * This function designates type of PDU (I, S or U).
343 static void llc_pdu_decode_pdu_type(struct sk_buff *skb, u8 *type)
345 struct llc_pdu_un *pdu = llc_pdu_un_hdr(skb);
347 if (pdu->ctrl_1 & 1) {
348 if ((pdu->ctrl_1 & LLC_PDU_TYPE_U) == LLC_PDU_TYPE_U)
349 *type = LLC_PDU_TYPE_U;
350 else
351 *type = LLC_PDU_TYPE_S;
352 } else
353 *type = LLC_PDU_TYPE_I;
357 * llc_pdu_get_pf_bit - extracts p/f bit of input PDU
358 * @pdu: pointer to LLC header.
360 * This function extracts p/f bit of input PDU. at first examines type of
361 * PDU and then extracts p/f bit. Returns the p/f bit.
363 static u8 llc_pdu_get_pf_bit(struct llc_pdu_sn *pdu)
365 u8 pdu_type;
366 u8 pf_bit = 0;
368 if (pdu->ctrl_1 & 1) {
369 if ((pdu->ctrl_1 & LLC_PDU_TYPE_U) == LLC_PDU_TYPE_U)
370 pdu_type = LLC_PDU_TYPE_U;
371 else
372 pdu_type = LLC_PDU_TYPE_S;
373 } else
374 pdu_type = LLC_PDU_TYPE_I;
375 switch (pdu_type) {
376 case LLC_PDU_TYPE_I:
377 case LLC_PDU_TYPE_S:
378 pf_bit = pdu->ctrl_2 & LLC_S_PF_BIT_MASK;
379 break;
380 case LLC_PDU_TYPE_U:
381 pf_bit = (pdu->ctrl_1 & LLC_U_PF_BIT_MASK) >> 4;
382 break;
384 return pf_bit;