Linux Audio

Check our new training course

Yocto distribution development and maintenance

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