/*1* llc_if.c - Defines LLC interface to upper layer2*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*/13#include <linux/gfp.h>14#include <linux/module.h>15#include <linux/kernel.h>16#include <linux/netdevice.h>17#include <linux/errno.h>18#include <net/llc_if.h>19#include <net/llc_sap.h>20#include <net/llc_s_ev.h>21#include <net/llc_conn.h>22#include <net/sock.h>23#include <net/llc_c_ev.h>24#include <net/llc_c_ac.h>25#include <net/llc_c_st.h>26#include <net/tcp_states.h>2728/**29* llc_build_and_send_pkt - Connection data sending for upper layers.30* @sk: connection31* @skb: packet to send32*33* This function is called when upper layer wants to send data using34* connection oriented communication mode. During sending data, connection35* will be locked and received frames and expired timers will be queued.36* Returns 0 for success, -ECONNABORTED when the connection already37* closed and -EBUSY when sending data is not permitted in this state or38* LLC has send an I pdu with p bit set to 1 and is waiting for it's39* response.40*41* This function always consumes a reference to the skb.42*/43int llc_build_and_send_pkt(struct sock *sk, struct sk_buff *skb)44{45struct llc_conn_state_ev *ev;46int rc = -ECONNABORTED;47struct llc_sock *llc = llc_sk(sk);4849if (unlikely(llc->state == LLC_CONN_STATE_ADM))50goto out_free;51rc = -EBUSY;52if (unlikely(llc_data_accept_state(llc->state) || /* data_conn_refuse */53llc->p_flag)) {54llc->failed_data_req = 1;55goto out_free;56}57ev = llc_conn_ev(skb);58ev->type = LLC_CONN_EV_TYPE_PRIM;59ev->prim = LLC_DATA_PRIM;60ev->prim_type = LLC_PRIM_TYPE_REQ;61skb->dev = llc->dev;62return llc_conn_state_process(sk, skb);6364out_free:65kfree_skb(skb);66return rc;67}6869/**70* llc_establish_connection - Called by upper layer to establish a conn71* @sk: connection72* @lmac: local mac address73* @dmac: destination mac address74* @dsap: destination sap75*76* Upper layer calls this to establish an LLC connection with a remote77* machine. This function packages a proper event and sends it connection78* component state machine. Success or failure of connection79* establishment will inform to upper layer via calling it's confirm80* function and passing proper information.81*/82int llc_establish_connection(struct sock *sk, const u8 *lmac, u8 *dmac, u8 dsap)83{84int rc = -EISCONN;85struct llc_addr laddr, daddr;86struct sk_buff *skb;87struct llc_sock *llc = llc_sk(sk);88struct sock *existing;8990laddr.lsap = llc->sap->laddr.lsap;91daddr.lsap = dsap;92memcpy(daddr.mac, dmac, sizeof(daddr.mac));93memcpy(laddr.mac, lmac, sizeof(laddr.mac));94existing = llc_lookup_established(llc->sap, &daddr, &laddr, sock_net(sk));95if (existing) {96if (existing->sk_state == TCP_ESTABLISHED) {97sk = existing;98goto out_put;99} else100sock_put(existing);101}102sock_hold(sk);103rc = -ENOMEM;104skb = alloc_skb(0, GFP_ATOMIC);105if (skb) {106struct llc_conn_state_ev *ev = llc_conn_ev(skb);107108ev->type = LLC_CONN_EV_TYPE_PRIM;109ev->prim = LLC_CONN_PRIM;110ev->prim_type = LLC_PRIM_TYPE_REQ;111skb_set_owner_w(skb, sk);112rc = llc_conn_state_process(sk, skb);113}114out_put:115sock_put(sk);116return rc;117}118119/**120* llc_send_disc - Called by upper layer to close a connection121* @sk: connection to be closed122*123* Upper layer calls this when it wants to close an established LLC124* connection with a remote machine. This function packages a proper event125* and sends it to connection component state machine. Returns 0 for126* success, 1 otherwise.127*/128int llc_send_disc(struct sock *sk)129{130u16 rc = 1;131struct llc_conn_state_ev *ev;132struct sk_buff *skb;133134sock_hold(sk);135if (sk->sk_type != SOCK_STREAM || sk->sk_state != TCP_ESTABLISHED ||136llc_sk(sk)->state == LLC_CONN_STATE_ADM ||137llc_sk(sk)->state == LLC_CONN_OUT_OF_SVC)138goto out;139/*140* Postpone unassigning the connection from its SAP and returning the141* connection until all ACTIONs have been completely executed142*/143skb = alloc_skb(0, GFP_ATOMIC);144if (!skb)145goto out;146skb_set_owner_w(skb, sk);147sk->sk_state = TCP_CLOSING;148ev = llc_conn_ev(skb);149ev->type = LLC_CONN_EV_TYPE_PRIM;150ev->prim = LLC_DISC_PRIM;151ev->prim_type = LLC_PRIM_TYPE_REQ;152rc = llc_conn_state_process(sk, skb);153out:154sock_put(sk);155return rc;156}157158159