llc_sap.c revision 1d67e6501b8dba54ef8dcabebe2ad049b8ad0d67
1/* 2 * llc_sap.c - driver routines for SAP component. 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 <net/llc.h> 16#include <net/llc_if.h> 17#include <net/llc_conn.h> 18#include <net/llc_pdu.h> 19#include <net/llc_sap.h> 20#include <net/llc_s_ac.h> 21#include <net/llc_s_ev.h> 22#include <net/llc_s_st.h> 23#include <net/sock.h> 24#include <net/tcp_states.h> 25#include <linux/llc.h> 26 27/** 28 * llc_alloc_frame - allocates sk_buff for frame 29 * @dev: network device this skb will be sent over 30 * 31 * Allocates an sk_buff for frame and initializes sk_buff fields. 32 * Returns allocated skb or %NULL when out of memory. 33 */ 34struct sk_buff *llc_alloc_frame(struct net_device *dev) 35{ 36 struct sk_buff *skb = alloc_skb(128, GFP_ATOMIC); 37 38 if (skb) { 39 skb_reserve(skb, 50); 40 skb->nh.raw = skb->h.raw = skb->data; 41 skb->protocol = htons(ETH_P_802_2); 42 skb->dev = dev; 43 skb->mac.raw = skb->head; 44 } 45 return skb; 46} 47 48void llc_save_primitive(struct sk_buff* skb, u8 prim) 49{ 50 struct sockaddr_llc *addr = llc_ui_skb_cb(skb); 51 52 /* save primitive for use by the user. */ 53 addr->sllc_family = skb->sk->sk_family; 54 addr->sllc_arphrd = skb->dev->type; 55 addr->sllc_test = prim == LLC_TEST_PRIM; 56 addr->sllc_xid = prim == LLC_XID_PRIM; 57 addr->sllc_ua = prim == LLC_DATAUNIT_PRIM; 58 llc_pdu_decode_sa(skb, addr->sllc_mac); 59 llc_pdu_decode_ssap(skb, &addr->sllc_sap); 60} 61 62/** 63 * llc_sap_rtn_pdu - Informs upper layer on rx of an UI, XID or TEST pdu. 64 * @sap: pointer to SAP 65 * @skb: received pdu 66 */ 67void llc_sap_rtn_pdu(struct llc_sap *sap, struct sk_buff *skb) 68{ 69 struct llc_sap_state_ev *ev = llc_sap_ev(skb); 70 struct llc_pdu_un *pdu = llc_pdu_un_hdr(skb); 71 72 switch (LLC_U_PDU_RSP(pdu)) { 73 case LLC_1_PDU_CMD_TEST: 74 ev->prim = LLC_TEST_PRIM; break; 75 case LLC_1_PDU_CMD_XID: 76 ev->prim = LLC_XID_PRIM; break; 77 case LLC_1_PDU_CMD_UI: 78 ev->prim = LLC_DATAUNIT_PRIM; break; 79 } 80 ev->ind_cfm_flag = LLC_IND; 81} 82 83/** 84 * llc_find_sap_trans - finds transition for event 85 * @sap: pointer to SAP 86 * @skb: happened event 87 * 88 * This function finds transition that matches with happened event. 89 * Returns the pointer to found transition on success or %NULL for 90 * failure. 91 */ 92static struct llc_sap_state_trans *llc_find_sap_trans(struct llc_sap *sap, 93 struct sk_buff* skb) 94{ 95 int i = 0; 96 struct llc_sap_state_trans *rc = NULL; 97 struct llc_sap_state_trans **next_trans; 98 struct llc_sap_state *curr_state = &llc_sap_state_table[sap->state - 1]; 99 /* 100 * Search thru events for this state until list exhausted or until 101 * its obvious the event is not valid for the current state 102 */ 103 for (next_trans = curr_state->transitions; next_trans[i]->ev; i++) 104 if (!next_trans[i]->ev(sap, skb)) { 105 rc = next_trans[i]; /* got event match; return it */ 106 break; 107 } 108 return rc; 109} 110 111/** 112 * llc_exec_sap_trans_actions - execute actions related to event 113 * @sap: pointer to SAP 114 * @trans: pointer to transition that it's actions must be performed 115 * @skb: happened event. 116 * 117 * This function executes actions that is related to happened event. 118 * Returns 0 for success and 1 for failure of at least one action. 119 */ 120static int llc_exec_sap_trans_actions(struct llc_sap *sap, 121 struct llc_sap_state_trans *trans, 122 struct sk_buff *skb) 123{ 124 int rc = 0; 125 llc_sap_action_t *next_action = trans->ev_actions; 126 127 for (; next_action && *next_action; next_action++) 128 if ((*next_action)(sap, skb)) 129 rc = 1; 130 return rc; 131} 132 133/** 134 * llc_sap_next_state - finds transition, execs actions & change SAP state 135 * @sap: pointer to SAP 136 * @skb: happened event 137 * 138 * This function finds transition that matches with happened event, then 139 * executes related actions and finally changes state of SAP. It returns 140 * 0 on success and 1 for failure. 141 */ 142static int llc_sap_next_state(struct llc_sap *sap, struct sk_buff *skb) 143{ 144 int rc = 1; 145 struct llc_sap_state_trans *trans; 146 147 if (sap->state > LLC_NR_SAP_STATES) 148 goto out; 149 trans = llc_find_sap_trans(sap, skb); 150 if (!trans) 151 goto out; 152 /* 153 * Got the state to which we next transition; perform the actions 154 * associated with this transition before actually transitioning to the 155 * next state 156 */ 157 rc = llc_exec_sap_trans_actions(sap, trans, skb); 158 if (rc) 159 goto out; 160 /* 161 * Transition SAP to next state if all actions execute successfully 162 */ 163 sap->state = trans->next_state; 164out: 165 return rc; 166} 167 168/** 169 * llc_sap_state_process - sends event to SAP state machine 170 * @sap: sap to use 171 * @skb: pointer to occurred event 172 * 173 * After executing actions of the event, upper layer will be indicated 174 * if needed(on receiving an UI frame). sk can be null for the 175 * datalink_proto case. 176 */ 177static void llc_sap_state_process(struct llc_sap *sap, struct sk_buff *skb) 178{ 179 struct llc_sap_state_ev *ev = llc_sap_ev(skb); 180 181 /* 182 * We have to hold the skb, because llc_sap_next_state 183 * will kfree it in the sending path and we need to 184 * look at the skb->cb, where we encode llc_sap_state_ev. 185 */ 186 skb_get(skb); 187 ev->ind_cfm_flag = 0; 188 llc_sap_next_state(sap, skb); 189 if (ev->ind_cfm_flag == LLC_IND) { 190 if (skb->sk->sk_state == TCP_LISTEN) 191 kfree_skb(skb); 192 else { 193 llc_save_primitive(skb, ev->prim); 194 195 /* queue skb to the user. */ 196 if (sock_queue_rcv_skb(skb->sk, skb)) 197 kfree_skb(skb); 198 } 199 } 200 kfree_skb(skb); 201} 202 203/** 204 * llc_build_and_send_test_pkt - TEST interface for upper layers. 205 * @sap: sap to use 206 * @skb: packet to send 207 * @dmac: destination mac address 208 * @dsap: destination sap 209 * 210 * This function is called when upper layer wants to send a TEST pdu. 211 * Returns 0 for success, 1 otherwise. 212 */ 213void llc_build_and_send_test_pkt(struct llc_sap *sap, 214 struct sk_buff *skb, u8 *dmac, u8 dsap) 215{ 216 struct llc_sap_state_ev *ev = llc_sap_ev(skb); 217 218 ev->saddr.lsap = sap->laddr.lsap; 219 ev->daddr.lsap = dsap; 220 memcpy(ev->saddr.mac, skb->dev->dev_addr, IFHWADDRLEN); 221 memcpy(ev->daddr.mac, dmac, IFHWADDRLEN); 222 223 ev->type = LLC_SAP_EV_TYPE_PRIM; 224 ev->prim = LLC_TEST_PRIM; 225 ev->prim_type = LLC_PRIM_TYPE_REQ; 226 llc_sap_state_process(sap, skb); 227} 228 229/** 230 * llc_build_and_send_xid_pkt - XID interface for upper layers 231 * @sap: sap to use 232 * @skb: packet to send 233 * @dmac: destination mac address 234 * @dsap: destination sap 235 * 236 * This function is called when upper layer wants to send a XID pdu. 237 * Returns 0 for success, 1 otherwise. 238 */ 239void llc_build_and_send_xid_pkt(struct llc_sap *sap, struct sk_buff *skb, 240 u8 *dmac, u8 dsap) 241{ 242 struct llc_sap_state_ev *ev = llc_sap_ev(skb); 243 244 ev->saddr.lsap = sap->laddr.lsap; 245 ev->daddr.lsap = dsap; 246 memcpy(ev->saddr.mac, skb->dev->dev_addr, IFHWADDRLEN); 247 memcpy(ev->daddr.mac, dmac, IFHWADDRLEN); 248 249 ev->type = LLC_SAP_EV_TYPE_PRIM; 250 ev->prim = LLC_XID_PRIM; 251 ev->prim_type = LLC_PRIM_TYPE_REQ; 252 llc_sap_state_process(sap, skb); 253} 254 255/** 256 * llc_sap_rcv - sends received pdus to the sap state machine 257 * @sap: current sap component structure. 258 * @skb: received frame. 259 * 260 * Sends received pdus to the sap state machine. 261 */ 262static void llc_sap_rcv(struct llc_sap *sap, struct sk_buff *skb) 263{ 264 struct llc_sap_state_ev *ev = llc_sap_ev(skb); 265 266 ev->type = LLC_SAP_EV_TYPE_PDU; 267 ev->reason = 0; 268 llc_sap_state_process(sap, skb); 269} 270 271/** 272 * llc_lookup_dgram - Finds dgram socket for the local sap/mac 273 * @sap: SAP 274 * @laddr: address of local LLC (MAC + SAP) 275 * 276 * Search socket list of the SAP and finds connection using the local 277 * mac, and local sap. Returns pointer for socket found, %NULL otherwise. 278 */ 279static struct sock *llc_lookup_dgram(struct llc_sap *sap, 280 struct llc_addr *laddr) 281{ 282 struct sock *rc; 283 struct hlist_node *node; 284 285 read_lock_bh(&sap->sk_list.lock); 286 sk_for_each(rc, node, &sap->sk_list.list) { 287 struct llc_sock *llc = llc_sk(rc); 288 289 if (rc->sk_type == SOCK_DGRAM && 290 llc->laddr.lsap == laddr->lsap && 291 llc_mac_match(llc->laddr.mac, laddr->mac)) { 292 sock_hold(rc); 293 goto found; 294 } 295 } 296 rc = NULL; 297found: 298 read_unlock_bh(&sap->sk_list.lock); 299 return rc; 300} 301 302void llc_sap_handler(struct llc_sap *sap, struct sk_buff *skb) 303{ 304 struct llc_addr laddr; 305 struct sock *sk; 306 307 llc_pdu_decode_da(skb, laddr.mac); 308 llc_pdu_decode_dsap(skb, &laddr.lsap); 309 310 sk = llc_lookup_dgram(sap, &laddr); 311 if (sk) { 312 skb->sk = sk; 313 llc_sap_rcv(sap, skb); 314 sock_put(sk); 315 } else 316 kfree_skb(skb); 317} 318