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