ftp://ftp.kernel.org/pub/linux/kernel/v2.6/linux-2.6.6.tar.bz2
[linux-2.6.git] / net / llc / llc_pdu.c
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
15 #include <linux/netdevice.h>
16 #include <net/llc_pdu.h>
17
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);
20
21 void llc_pdu_set_cmd_rsp(struct sk_buff *skb, u8 pdu_type)
22 {
23         llc_pdu_un_hdr(skb)->ssap |= pdu_type;
24 }
25
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).
30  *
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.
34  */
35 void llc_pdu_set_pf_bit(struct sk_buff *skb, u8 bit_value)
36 {
37         u8 pdu_type;
38         struct llc_pdu_sn *pdu;
39
40         llc_pdu_decode_pdu_type(skb, &pdu_type);
41         pdu = llc_pdu_sn_hdr(skb);
42         
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;
51         }
52 }
53
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)
58  *
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.
62  */
63 void llc_pdu_decode_pf_bit(struct sk_buff *skb, u8 *pf_bit)
64 {
65         u8 pdu_type;
66         struct llc_pdu_sn *pdu;
67
68         llc_pdu_decode_pdu_type(skb, &pdu_type);
69         pdu = llc_pdu_sn_hdr(skb);
70
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;
79         }
80 }
81
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).
86  *
87  *      This function extracts command/response bit from LLC header. this bit
88  *      is right bit of source SAP.
89  */
90 void llc_pdu_decode_cr_bit(struct sk_buff *skb, u8 *cr_bit)
91 {
92         *cr_bit = llc_pdu_un_hdr(skb)->ssap & LLC_PDU_CMD_RSP_MASK;
93 }
94
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
99  *
100  *      Builds a pdu frame as a DISC command.
101  */
102 void llc_pdu_init_as_disc_cmd(struct sk_buff *skb, u8 p_bit)
103 {
104         struct llc_pdu_un *pdu = llc_pdu_un_hdr(skb);
105
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;
109 }
110
111 /**
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
117  *
118  *      Builds a pdu frame as an I command.
119  */
120 void llc_pdu_init_as_i_cmd(struct sk_buff *skb, u8 p_bit, u8 ns, u8 nr)
121 {
122         struct llc_pdu_sn *pdu = llc_pdu_sn_hdr(skb);
123
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 */
129 }
130
131 /**
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
136  *
137  *      Builds a pdu frame as a REJ command.
138  */
139 void llc_pdu_init_as_rej_cmd(struct sk_buff *skb, u8 p_bit, u8 nr)
140 {
141         struct llc_pdu_sn *pdu = llc_pdu_sn_hdr(skb);
142
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 */
149 }
150
151 /**
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
156  *
157  *      Builds a pdu frame as an RNR command.
158  */
159 void llc_pdu_init_as_rnr_cmd(struct sk_buff *skb, u8 p_bit, u8 nr)
160 {
161         struct llc_pdu_sn *pdu = llc_pdu_sn_hdr(skb);
162
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 */
169 }
170
171 /**
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
176  *
177  *      Builds a pdu frame as an RR command.
178  */
179 void llc_pdu_init_as_rr_cmd(struct sk_buff *skb, u8 p_bit, u8 nr)
180 {
181         struct llc_pdu_sn *pdu = llc_pdu_sn_hdr(skb);
182
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 */
188 }
189
190 /**
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
194  *
195  *      Builds a pdu frame as an SABME command.
196  */
197 void llc_pdu_init_as_sabme_cmd(struct sk_buff *skb, u8 p_bit)
198 {
199         struct llc_pdu_un *pdu = llc_pdu_un_hdr(skb);
200
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;
204 }
205
206 /**
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
210  *
211  *      Builds a pdu frame as a DM response.
212  */
213 void llc_pdu_init_as_dm_rsp(struct sk_buff *skb, u8 f_bit)
214 {
215         struct llc_pdu_un *pdu = llc_pdu_un_hdr(skb);
216
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;
220 }
221
222 /**
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)
230  *
231  *      Builds a pdu frame as a FRMR response.
232  */
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)
235 {
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);
240
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;
244
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);
258 }
259
260 /**
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
265  *
266  *      Builds a pdu frame as an RR response.
267  */
268 void llc_pdu_init_as_rr_rsp(struct sk_buff *skb, u8 f_bit, u8 nr)
269 {
270         struct llc_pdu_sn *pdu = llc_pdu_sn_hdr(skb);
271
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 */
278 }
279
280 /**
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
285  *
286  *      Builds a pdu frame as a REJ response.
287  */
288 void llc_pdu_init_as_rej_rsp(struct sk_buff *skb, u8 f_bit, u8 nr)
289 {
290         struct llc_pdu_sn *pdu = llc_pdu_sn_hdr(skb);
291
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 */
298 }
299
300 /**
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
305  *
306  *      Builds a pdu frame as an RNR response.
307  */
308 void llc_pdu_init_as_rnr_rsp(struct sk_buff *skb, u8 f_bit, u8 nr)
309 {
310         struct llc_pdu_sn *pdu = llc_pdu_sn_hdr(skb);
311
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 */
318 }
319
320 /**
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
324  *
325  *      Builds a pdu frame as a UA response.
326  */
327 void llc_pdu_init_as_ua_rsp(struct sk_buff *skb, u8 f_bit)
328 {
329         struct llc_pdu_un *pdu = llc_pdu_un_hdr(skb);
330
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;
334 }
335
336 /**
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).
340  *
341  *      This function designates type of PDU (I, S or U).
342  */
343 static void llc_pdu_decode_pdu_type(struct sk_buff *skb, u8 *type)
344 {
345         struct llc_pdu_un *pdu = llc_pdu_un_hdr(skb);
346
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;
354 }
355
356 /**
357  *      llc_pdu_get_pf_bit - extracts p/f bit of input PDU
358  *      @pdu: pointer to LLC header.
359  *
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.
362  */
363 static u8 llc_pdu_get_pf_bit(struct llc_pdu_sn *pdu)
364 {
365         u8 pdu_type;
366         u8 pf_bit = 0;
367
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;
383         }
384         return pf_bit;
385 }