/*1* llc_pdu.c - access to PDU internals2*3* Copyright (c) 1997 by Procom Technology, Inc.4* 2001-2003 by Arnaldo Carvalho de Melo <[email protected]>5*6* This program can be redistributed or modified under the terms of the7* GNU General Public License as published by the Free Software Foundation.8* This program is distributed without any warranty or implied warranty9* of merchantability or fitness for a particular purpose.10*11* See the GNU General Public License for more details.12*/1314#include <linux/netdevice.h>15#include <net/llc_pdu.h>1617static void llc_pdu_decode_pdu_type(struct sk_buff *skb, u8 *type);18static u8 llc_pdu_get_pf_bit(struct llc_pdu_sn *pdu);1920void llc_pdu_set_cmd_rsp(struct sk_buff *skb, u8 pdu_type)21{22llc_pdu_un_hdr(skb)->ssap |= pdu_type;23}2425/**26* llc_pdu_set_pf_bit - sets poll/final bit in LLC header27* @skb: Frame to set bit in28* @bit_value: poll/final bit (0 or 1).29*30* This function sets poll/final bit in LLC header (based on type of PDU).31* in I or S pdus, p/f bit is right bit of fourth byte in header. in U32* pdus p/f bit is fifth bit of third byte.33*/34void llc_pdu_set_pf_bit(struct sk_buff *skb, u8 bit_value)35{36u8 pdu_type;37struct llc_pdu_sn *pdu;3839llc_pdu_decode_pdu_type(skb, &pdu_type);40pdu = llc_pdu_sn_hdr(skb);4142switch (pdu_type) {43case LLC_PDU_TYPE_I:44case LLC_PDU_TYPE_S:45pdu->ctrl_2 = (pdu->ctrl_2 & 0xFE) | bit_value;46break;47case LLC_PDU_TYPE_U:48pdu->ctrl_1 |= (pdu->ctrl_1 & 0xEF) | (bit_value << 4);49break;50}51}5253/**54* llc_pdu_decode_pf_bit - extracs poll/final bit from LLC header55* @skb: input skb that p/f bit must be extracted from it56* @pf_bit: poll/final bit (0 or 1)57*58* This function extracts poll/final bit from LLC header (based on type of59* PDU). In I or S pdus, p/f bit is right bit of fourth byte in header. In60* U pdus p/f bit is fifth bit of third byte.61*/62void llc_pdu_decode_pf_bit(struct sk_buff *skb, u8 *pf_bit)63{64u8 pdu_type;65struct llc_pdu_sn *pdu;6667llc_pdu_decode_pdu_type(skb, &pdu_type);68pdu = llc_pdu_sn_hdr(skb);6970switch (pdu_type) {71case LLC_PDU_TYPE_I:72case LLC_PDU_TYPE_S:73*pf_bit = pdu->ctrl_2 & LLC_S_PF_BIT_MASK;74break;75case LLC_PDU_TYPE_U:76*pf_bit = (pdu->ctrl_1 & LLC_U_PF_BIT_MASK) >> 4;77break;78}79}8081/**82* llc_pdu_init_as_disc_cmd - Builds DISC PDU83* @skb: Address of the skb to build84* @p_bit: The P bit to set in the PDU85*86* Builds a pdu frame as a DISC command.87*/88void llc_pdu_init_as_disc_cmd(struct sk_buff *skb, u8 p_bit)89{90struct llc_pdu_un *pdu = llc_pdu_un_hdr(skb);9192pdu->ctrl_1 = LLC_PDU_TYPE_U;93pdu->ctrl_1 |= LLC_2_PDU_CMD_DISC;94pdu->ctrl_1 |= ((p_bit & 1) << 4) & LLC_U_PF_BIT_MASK;95}9697/**98* llc_pdu_init_as_i_cmd - builds I pdu99* @skb: Address of the skb to build100* @p_bit: The P bit to set in the PDU101* @ns: The sequence number of the data PDU102* @nr: The seq. number of the expected I PDU from the remote103*104* Builds a pdu frame as an I command.105*/106void llc_pdu_init_as_i_cmd(struct sk_buff *skb, u8 p_bit, u8 ns, u8 nr)107{108struct llc_pdu_sn *pdu = llc_pdu_sn_hdr(skb);109110pdu->ctrl_1 = LLC_PDU_TYPE_I;111pdu->ctrl_2 = 0;112pdu->ctrl_2 |= (p_bit & LLC_I_PF_BIT_MASK); /* p/f bit */113pdu->ctrl_1 |= (ns << 1) & 0xFE; /* set N(S) in bits 2..8 */114pdu->ctrl_2 |= (nr << 1) & 0xFE; /* set N(R) in bits 10..16 */115}116117/**118* llc_pdu_init_as_rej_cmd - builds REJ PDU119* @skb: Address of the skb to build120* @p_bit: The P bit to set in the PDU121* @nr: The seq. number of the expected I PDU from the remote122*123* Builds a pdu frame as a REJ command.124*/125void llc_pdu_init_as_rej_cmd(struct sk_buff *skb, u8 p_bit, u8 nr)126{127struct llc_pdu_sn *pdu = llc_pdu_sn_hdr(skb);128129pdu->ctrl_1 = LLC_PDU_TYPE_S;130pdu->ctrl_1 |= LLC_2_PDU_CMD_REJ;131pdu->ctrl_2 = 0;132pdu->ctrl_2 |= p_bit & LLC_S_PF_BIT_MASK;133pdu->ctrl_1 &= 0x0F; /* setting bits 5..8 to zero(reserved) */134pdu->ctrl_2 |= (nr << 1) & 0xFE; /* set N(R) in bits 10..16 */135}136137/**138* llc_pdu_init_as_rnr_cmd - builds RNR pdu139* @skb: Address of the skb to build140* @p_bit: The P bit to set in the PDU141* @nr: The seq. number of the expected I PDU from the remote142*143* Builds a pdu frame as an RNR command.144*/145void llc_pdu_init_as_rnr_cmd(struct sk_buff *skb, u8 p_bit, u8 nr)146{147struct llc_pdu_sn *pdu = llc_pdu_sn_hdr(skb);148149pdu->ctrl_1 = LLC_PDU_TYPE_S;150pdu->ctrl_1 |= LLC_2_PDU_CMD_RNR;151pdu->ctrl_2 = 0;152pdu->ctrl_2 |= p_bit & LLC_S_PF_BIT_MASK;153pdu->ctrl_1 &= 0x0F; /* setting bits 5..8 to zero(reserved) */154pdu->ctrl_2 |= (nr << 1) & 0xFE; /* set N(R) in bits 10..16 */155}156157/**158* llc_pdu_init_as_rr_cmd - Builds RR pdu159* @skb: Address of the skb to build160* @p_bit: The P bit to set in the PDU161* @nr: The seq. number of the expected I PDU from the remote162*163* Builds a pdu frame as an RR command.164*/165void llc_pdu_init_as_rr_cmd(struct sk_buff *skb, u8 p_bit, u8 nr)166{167struct llc_pdu_sn *pdu = llc_pdu_sn_hdr(skb);168169pdu->ctrl_1 = LLC_PDU_TYPE_S;170pdu->ctrl_1 |= LLC_2_PDU_CMD_RR;171pdu->ctrl_2 = p_bit & LLC_S_PF_BIT_MASK;172pdu->ctrl_1 &= 0x0F; /* setting bits 5..8 to zero(reserved) */173pdu->ctrl_2 |= (nr << 1) & 0xFE; /* set N(R) in bits 10..16 */174}175176/**177* llc_pdu_init_as_sabme_cmd - builds SABME pdu178* @skb: Address of the skb to build179* @p_bit: The P bit to set in the PDU180*181* Builds a pdu frame as an SABME command.182*/183void llc_pdu_init_as_sabme_cmd(struct sk_buff *skb, u8 p_bit)184{185struct llc_pdu_un *pdu = llc_pdu_un_hdr(skb);186187pdu->ctrl_1 = LLC_PDU_TYPE_U;188pdu->ctrl_1 |= LLC_2_PDU_CMD_SABME;189pdu->ctrl_1 |= ((p_bit & 1) << 4) & LLC_U_PF_BIT_MASK;190}191192/**193* llc_pdu_init_as_dm_rsp - builds DM response pdu194* @skb: Address of the skb to build195* @f_bit: The F bit to set in the PDU196*197* Builds a pdu frame as a DM response.198*/199void llc_pdu_init_as_dm_rsp(struct sk_buff *skb, u8 f_bit)200{201struct llc_pdu_un *pdu = llc_pdu_un_hdr(skb);202203pdu->ctrl_1 = LLC_PDU_TYPE_U;204pdu->ctrl_1 |= LLC_2_PDU_RSP_DM;205pdu->ctrl_1 |= ((f_bit & 1) << 4) & LLC_U_PF_BIT_MASK;206}207208/**209* llc_pdu_init_as_frmr_rsp - builds FRMR response PDU210* @skb: Address of the frame to build211* @prev_pdu: The rejected PDU frame212* @f_bit: The F bit to set in the PDU213* @vs: tx state vari value for the data link conn at the rejecting LLC214* @vr: rx state var value for the data link conn at the rejecting LLC215* @vzyxw: completely described in the IEEE Std 802.2 document (Pg 55)216*217* Builds a pdu frame as a FRMR response.218*/219void llc_pdu_init_as_frmr_rsp(struct sk_buff *skb, struct llc_pdu_sn *prev_pdu,220u8 f_bit, u8 vs, u8 vr, u8 vzyxw)221{222struct llc_frmr_info *frmr_info;223u8 prev_pf = 0;224u8 *ctrl;225struct llc_pdu_sn *pdu = llc_pdu_sn_hdr(skb);226227pdu->ctrl_1 = LLC_PDU_TYPE_U;228pdu->ctrl_1 |= LLC_2_PDU_RSP_FRMR;229pdu->ctrl_1 |= ((f_bit & 1) << 4) & LLC_U_PF_BIT_MASK;230231frmr_info = (struct llc_frmr_info *)&pdu->ctrl_2;232ctrl = (u8 *)&prev_pdu->ctrl_1;233FRMR_INFO_SET_REJ_CNTRL(frmr_info,ctrl);234FRMR_INFO_SET_Vs(frmr_info, vs);235FRMR_INFO_SET_Vr(frmr_info, vr);236prev_pf = llc_pdu_get_pf_bit(prev_pdu);237FRMR_INFO_SET_C_R_BIT(frmr_info, prev_pf);238FRMR_INFO_SET_INVALID_PDU_CTRL_IND(frmr_info, vzyxw);239FRMR_INFO_SET_INVALID_PDU_INFO_IND(frmr_info, vzyxw);240FRMR_INFO_SET_PDU_INFO_2LONG_IND(frmr_info, vzyxw);241FRMR_INFO_SET_PDU_INVALID_Nr_IND(frmr_info, vzyxw);242FRMR_INFO_SET_PDU_INVALID_Ns_IND(frmr_info, vzyxw);243skb_put(skb, sizeof(struct llc_frmr_info));244}245246/**247* llc_pdu_init_as_rr_rsp - builds RR response pdu248* @skb: Address of the skb to build249* @f_bit: The F bit to set in the PDU250* @nr: The seq. number of the expected data PDU from the remote251*252* Builds a pdu frame as an RR response.253*/254void llc_pdu_init_as_rr_rsp(struct sk_buff *skb, u8 f_bit, u8 nr)255{256struct llc_pdu_sn *pdu = llc_pdu_sn_hdr(skb);257258pdu->ctrl_1 = LLC_PDU_TYPE_S;259pdu->ctrl_1 |= LLC_2_PDU_RSP_RR;260pdu->ctrl_2 = 0;261pdu->ctrl_2 |= f_bit & LLC_S_PF_BIT_MASK;262pdu->ctrl_1 &= 0x0F; /* setting bits 5..8 to zero(reserved) */263pdu->ctrl_2 |= (nr << 1) & 0xFE; /* set N(R) in bits 10..16 */264}265266/**267* llc_pdu_init_as_rej_rsp - builds REJ response pdu268* @skb: Address of the skb to build269* @f_bit: The F bit to set in the PDU270* @nr: The seq. number of the expected data PDU from the remote271*272* Builds a pdu frame as a REJ response.273*/274void llc_pdu_init_as_rej_rsp(struct sk_buff *skb, u8 f_bit, u8 nr)275{276struct llc_pdu_sn *pdu = llc_pdu_sn_hdr(skb);277278pdu->ctrl_1 = LLC_PDU_TYPE_S;279pdu->ctrl_1 |= LLC_2_PDU_RSP_REJ;280pdu->ctrl_2 = 0;281pdu->ctrl_2 |= f_bit & LLC_S_PF_BIT_MASK;282pdu->ctrl_1 &= 0x0F; /* setting bits 5..8 to zero(reserved) */283pdu->ctrl_2 |= (nr << 1) & 0xFE; /* set N(R) in bits 10..16 */284}285286/**287* llc_pdu_init_as_rnr_rsp - builds RNR response pdu288* @skb: Address of the frame to build289* @f_bit: The F bit to set in the PDU290* @nr: The seq. number of the expected data PDU from the remote291*292* Builds a pdu frame as an RNR response.293*/294void llc_pdu_init_as_rnr_rsp(struct sk_buff *skb, u8 f_bit, u8 nr)295{296struct llc_pdu_sn *pdu = llc_pdu_sn_hdr(skb);297298pdu->ctrl_1 = LLC_PDU_TYPE_S;299pdu->ctrl_1 |= LLC_2_PDU_RSP_RNR;300pdu->ctrl_2 = 0;301pdu->ctrl_2 |= f_bit & LLC_S_PF_BIT_MASK;302pdu->ctrl_1 &= 0x0F; /* setting bits 5..8 to zero(reserved) */303pdu->ctrl_2 |= (nr << 1) & 0xFE; /* set N(R) in bits 10..16 */304}305306/**307* llc_pdu_init_as_ua_rsp - builds UA response pdu308* @skb: Address of the frame to build309* @f_bit: The F bit to set in the PDU310*311* Builds a pdu frame as a UA response.312*/313void llc_pdu_init_as_ua_rsp(struct sk_buff *skb, u8 f_bit)314{315struct llc_pdu_un *pdu = llc_pdu_un_hdr(skb);316317pdu->ctrl_1 = LLC_PDU_TYPE_U;318pdu->ctrl_1 |= LLC_2_PDU_RSP_UA;319pdu->ctrl_1 |= ((f_bit & 1) << 4) & LLC_U_PF_BIT_MASK;320}321322/**323* llc_pdu_decode_pdu_type - designates PDU type324* @skb: input skb that type of it must be designated.325* @type: type of PDU (output argument).326*327* This function designates type of PDU (I, S or U).328*/329static void llc_pdu_decode_pdu_type(struct sk_buff *skb, u8 *type)330{331struct llc_pdu_un *pdu = llc_pdu_un_hdr(skb);332333if (pdu->ctrl_1 & 1) {334if ((pdu->ctrl_1 & LLC_PDU_TYPE_U) == LLC_PDU_TYPE_U)335*type = LLC_PDU_TYPE_U;336else337*type = LLC_PDU_TYPE_S;338} else339*type = LLC_PDU_TYPE_I;340}341342/**343* llc_pdu_get_pf_bit - extracts p/f bit of input PDU344* @pdu: pointer to LLC header.345*346* This function extracts p/f bit of input PDU. at first examines type of347* PDU and then extracts p/f bit. Returns the p/f bit.348*/349static u8 llc_pdu_get_pf_bit(struct llc_pdu_sn *pdu)350{351u8 pdu_type;352u8 pf_bit = 0;353354if (pdu->ctrl_1 & 1) {355if ((pdu->ctrl_1 & LLC_PDU_TYPE_U) == LLC_PDU_TYPE_U)356pdu_type = LLC_PDU_TYPE_U;357else358pdu_type = LLC_PDU_TYPE_S;359} else360pdu_type = LLC_PDU_TYPE_I;361switch (pdu_type) {362case LLC_PDU_TYPE_I:363case LLC_PDU_TYPE_S:364pf_bit = pdu->ctrl_2 & LLC_S_PF_BIT_MASK;365break;366case LLC_PDU_TYPE_U:367pf_bit = (pdu->ctrl_1 & LLC_U_PF_BIT_MASK) >> 4;368break;369}370return pf_bit;371}372373374