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