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