xref: /OK3568_Linux_fs/kernel/net/llc/llc_if.c (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1*4882a593Smuzhiyun /*
2*4882a593Smuzhiyun  * llc_if.c - Defines LLC interface to upper layer
3*4882a593Smuzhiyun  *
4*4882a593Smuzhiyun  * Copyright (c) 1997 by Procom Technology, Inc.
5*4882a593Smuzhiyun  * 		 2001-2003 by Arnaldo Carvalho de Melo <acme@conectiva.com.br>
6*4882a593Smuzhiyun  *
7*4882a593Smuzhiyun  * This program can be redistributed or modified under the terms of the
8*4882a593Smuzhiyun  * GNU General Public License as published by the Free Software Foundation.
9*4882a593Smuzhiyun  * This program is distributed without any warranty or implied warranty
10*4882a593Smuzhiyun  * of merchantability or fitness for a particular purpose.
11*4882a593Smuzhiyun  *
12*4882a593Smuzhiyun  * See the GNU General Public License for more details.
13*4882a593Smuzhiyun  */
14*4882a593Smuzhiyun #include <linux/gfp.h>
15*4882a593Smuzhiyun #include <linux/module.h>
16*4882a593Smuzhiyun #include <linux/kernel.h>
17*4882a593Smuzhiyun #include <linux/netdevice.h>
18*4882a593Smuzhiyun #include <linux/errno.h>
19*4882a593Smuzhiyun #include <net/llc_if.h>
20*4882a593Smuzhiyun #include <net/llc_sap.h>
21*4882a593Smuzhiyun #include <net/llc_s_ev.h>
22*4882a593Smuzhiyun #include <net/llc_conn.h>
23*4882a593Smuzhiyun #include <net/sock.h>
24*4882a593Smuzhiyun #include <net/llc_c_ev.h>
25*4882a593Smuzhiyun #include <net/llc_c_ac.h>
26*4882a593Smuzhiyun #include <net/llc_c_st.h>
27*4882a593Smuzhiyun #include <net/tcp_states.h>
28*4882a593Smuzhiyun 
29*4882a593Smuzhiyun /**
30*4882a593Smuzhiyun  *	llc_build_and_send_pkt - Connection data sending for upper layers.
31*4882a593Smuzhiyun  *	@sk: connection
32*4882a593Smuzhiyun  *	@skb: packet to send
33*4882a593Smuzhiyun  *
34*4882a593Smuzhiyun  *	This function is called when upper layer wants to send data using
35*4882a593Smuzhiyun  *	connection oriented communication mode. During sending data, connection
36*4882a593Smuzhiyun  *	will be locked and received frames and expired timers will be queued.
37*4882a593Smuzhiyun  *	Returns 0 for success, -ECONNABORTED when the connection already
38*4882a593Smuzhiyun  *	closed and -EBUSY when sending data is not permitted in this state or
39*4882a593Smuzhiyun  *	LLC has send an I pdu with p bit set to 1 and is waiting for it's
40*4882a593Smuzhiyun  *	response.
41*4882a593Smuzhiyun  *
42*4882a593Smuzhiyun  *	This function always consumes a reference to the skb.
43*4882a593Smuzhiyun  */
llc_build_and_send_pkt(struct sock * sk,struct sk_buff * skb)44*4882a593Smuzhiyun int llc_build_and_send_pkt(struct sock *sk, struct sk_buff *skb)
45*4882a593Smuzhiyun {
46*4882a593Smuzhiyun 	struct llc_conn_state_ev *ev;
47*4882a593Smuzhiyun 	int rc = -ECONNABORTED;
48*4882a593Smuzhiyun 	struct llc_sock *llc = llc_sk(sk);
49*4882a593Smuzhiyun 
50*4882a593Smuzhiyun 	if (unlikely(llc->state == LLC_CONN_STATE_ADM))
51*4882a593Smuzhiyun 		goto out_free;
52*4882a593Smuzhiyun 	rc = -EBUSY;
53*4882a593Smuzhiyun 	if (unlikely(llc_data_accept_state(llc->state) || /* data_conn_refuse */
54*4882a593Smuzhiyun 		     llc->p_flag)) {
55*4882a593Smuzhiyun 		llc->failed_data_req = 1;
56*4882a593Smuzhiyun 		goto out_free;
57*4882a593Smuzhiyun 	}
58*4882a593Smuzhiyun 	ev = llc_conn_ev(skb);
59*4882a593Smuzhiyun 	ev->type      = LLC_CONN_EV_TYPE_PRIM;
60*4882a593Smuzhiyun 	ev->prim      = LLC_DATA_PRIM;
61*4882a593Smuzhiyun 	ev->prim_type = LLC_PRIM_TYPE_REQ;
62*4882a593Smuzhiyun 	skb->dev      = llc->dev;
63*4882a593Smuzhiyun 	return llc_conn_state_process(sk, skb);
64*4882a593Smuzhiyun 
65*4882a593Smuzhiyun out_free:
66*4882a593Smuzhiyun 	kfree_skb(skb);
67*4882a593Smuzhiyun 	return rc;
68*4882a593Smuzhiyun }
69*4882a593Smuzhiyun 
70*4882a593Smuzhiyun /**
71*4882a593Smuzhiyun  *	llc_establish_connection - Called by upper layer to establish a conn
72*4882a593Smuzhiyun  *	@sk: connection
73*4882a593Smuzhiyun  *	@lmac: local mac address
74*4882a593Smuzhiyun  *	@dmac: destination mac address
75*4882a593Smuzhiyun  *	@dsap: destination sap
76*4882a593Smuzhiyun  *
77*4882a593Smuzhiyun  *	Upper layer calls this to establish an LLC connection with a remote
78*4882a593Smuzhiyun  *	machine. This function packages a proper event and sends it connection
79*4882a593Smuzhiyun  *	component state machine. Success or failure of connection
80*4882a593Smuzhiyun  *	establishment will inform to upper layer via calling it's confirm
81*4882a593Smuzhiyun  *	function and passing proper information.
82*4882a593Smuzhiyun  */
llc_establish_connection(struct sock * sk,u8 * lmac,u8 * dmac,u8 dsap)83*4882a593Smuzhiyun int llc_establish_connection(struct sock *sk, u8 *lmac, u8 *dmac, u8 dsap)
84*4882a593Smuzhiyun {
85*4882a593Smuzhiyun 	int rc = -EISCONN;
86*4882a593Smuzhiyun 	struct llc_addr laddr, daddr;
87*4882a593Smuzhiyun 	struct sk_buff *skb;
88*4882a593Smuzhiyun 	struct llc_sock *llc = llc_sk(sk);
89*4882a593Smuzhiyun 	struct sock *existing;
90*4882a593Smuzhiyun 
91*4882a593Smuzhiyun 	laddr.lsap = llc->sap->laddr.lsap;
92*4882a593Smuzhiyun 	daddr.lsap = dsap;
93*4882a593Smuzhiyun 	memcpy(daddr.mac, dmac, sizeof(daddr.mac));
94*4882a593Smuzhiyun 	memcpy(laddr.mac, lmac, sizeof(laddr.mac));
95*4882a593Smuzhiyun 	existing = llc_lookup_established(llc->sap, &daddr, &laddr);
96*4882a593Smuzhiyun 	if (existing) {
97*4882a593Smuzhiyun 		if (existing->sk_state == TCP_ESTABLISHED) {
98*4882a593Smuzhiyun 			sk = existing;
99*4882a593Smuzhiyun 			goto out_put;
100*4882a593Smuzhiyun 		} else
101*4882a593Smuzhiyun 			sock_put(existing);
102*4882a593Smuzhiyun 	}
103*4882a593Smuzhiyun 	sock_hold(sk);
104*4882a593Smuzhiyun 	rc = -ENOMEM;
105*4882a593Smuzhiyun 	skb = alloc_skb(0, GFP_ATOMIC);
106*4882a593Smuzhiyun 	if (skb) {
107*4882a593Smuzhiyun 		struct llc_conn_state_ev *ev = llc_conn_ev(skb);
108*4882a593Smuzhiyun 
109*4882a593Smuzhiyun 		ev->type      = LLC_CONN_EV_TYPE_PRIM;
110*4882a593Smuzhiyun 		ev->prim      = LLC_CONN_PRIM;
111*4882a593Smuzhiyun 		ev->prim_type = LLC_PRIM_TYPE_REQ;
112*4882a593Smuzhiyun 		skb_set_owner_w(skb, sk);
113*4882a593Smuzhiyun 		rc = llc_conn_state_process(sk, skb);
114*4882a593Smuzhiyun 	}
115*4882a593Smuzhiyun out_put:
116*4882a593Smuzhiyun 	sock_put(sk);
117*4882a593Smuzhiyun 	return rc;
118*4882a593Smuzhiyun }
119*4882a593Smuzhiyun 
120*4882a593Smuzhiyun /**
121*4882a593Smuzhiyun  *	llc_send_disc - Called by upper layer to close a connection
122*4882a593Smuzhiyun  *	@sk: connection to be closed
123*4882a593Smuzhiyun  *
124*4882a593Smuzhiyun  *	Upper layer calls this when it wants to close an established LLC
125*4882a593Smuzhiyun  *	connection with a remote machine. This function packages a proper event
126*4882a593Smuzhiyun  *	and sends it to connection component state machine. Returns 0 for
127*4882a593Smuzhiyun  *	success, 1 otherwise.
128*4882a593Smuzhiyun  */
llc_send_disc(struct sock * sk)129*4882a593Smuzhiyun int llc_send_disc(struct sock *sk)
130*4882a593Smuzhiyun {
131*4882a593Smuzhiyun 	u16 rc = 1;
132*4882a593Smuzhiyun 	struct llc_conn_state_ev *ev;
133*4882a593Smuzhiyun 	struct sk_buff *skb;
134*4882a593Smuzhiyun 
135*4882a593Smuzhiyun 	sock_hold(sk);
136*4882a593Smuzhiyun 	if (sk->sk_type != SOCK_STREAM || sk->sk_state != TCP_ESTABLISHED ||
137*4882a593Smuzhiyun 	    llc_sk(sk)->state == LLC_CONN_STATE_ADM ||
138*4882a593Smuzhiyun 	    llc_sk(sk)->state == LLC_CONN_OUT_OF_SVC)
139*4882a593Smuzhiyun 		goto out;
140*4882a593Smuzhiyun 	/*
141*4882a593Smuzhiyun 	 * Postpone unassigning the connection from its SAP and returning the
142*4882a593Smuzhiyun 	 * connection until all ACTIONs have been completely executed
143*4882a593Smuzhiyun 	 */
144*4882a593Smuzhiyun 	skb = alloc_skb(0, GFP_ATOMIC);
145*4882a593Smuzhiyun 	if (!skb)
146*4882a593Smuzhiyun 		goto out;
147*4882a593Smuzhiyun 	skb_set_owner_w(skb, sk);
148*4882a593Smuzhiyun 	sk->sk_state  = TCP_CLOSING;
149*4882a593Smuzhiyun 	ev	      = llc_conn_ev(skb);
150*4882a593Smuzhiyun 	ev->type      = LLC_CONN_EV_TYPE_PRIM;
151*4882a593Smuzhiyun 	ev->prim      = LLC_DISC_PRIM;
152*4882a593Smuzhiyun 	ev->prim_type = LLC_PRIM_TYPE_REQ;
153*4882a593Smuzhiyun 	rc = llc_conn_state_process(sk, skb);
154*4882a593Smuzhiyun out:
155*4882a593Smuzhiyun 	sock_put(sk);
156*4882a593Smuzhiyun 	return rc;
157*4882a593Smuzhiyun }
158