blob: a8dde9b010da7e44cb407d5954f7678ac2d585dd [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * llc_conn.c - Driver routines for connection 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 <linux/init.h>
16#include <net/llc_sap.h>
17#include <net/llc_conn.h>
18#include <net/sock.h>
Arnaldo Carvalho de Meloc752f072005-08-09 20:08:28 -070019#include <net/tcp_states.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070020#include <net/llc_c_ev.h>
21#include <net/llc_c_ac.h>
22#include <net/llc_c_st.h>
23#include <net/llc_pdu.h>
24
25#if 0
26#define dprintk(args...) printk(KERN_DEBUG args)
27#else
28#define dprintk(args...)
29#endif
30
31static int llc_find_offset(int state, int ev_type);
32static void llc_conn_send_pdus(struct sock *sk);
33static int llc_conn_service(struct sock *sk, struct sk_buff *skb);
34static int llc_exec_conn_trans_actions(struct sock *sk,
35 struct llc_conn_state_trans *trans,
36 struct sk_buff *ev);
37static struct llc_conn_state_trans *llc_qualify_conn_ev(struct sock *sk,
38 struct sk_buff *skb);
39
40/* Offset table on connection states transition diagram */
41static int llc_offset_table[NBR_CONN_STATES][NBR_CONN_EV];
42
Arnaldo Carvalho de Melo590232a2005-09-22 04:30:44 -030043int sysctl_llc2_ack_timeout = LLC2_ACK_TIME * HZ;
44int sysctl_llc2_p_timeout = LLC2_P_TIME * HZ;
45int sysctl_llc2_rej_timeout = LLC2_REJ_TIME * HZ;
46int sysctl_llc2_busy_timeout = LLC2_BUSY_TIME * HZ;
47
Linus Torvalds1da177e2005-04-16 15:20:36 -070048/**
49 * llc_conn_state_process - sends event to connection state machine
50 * @sk: connection
51 * @skb: occurred event
52 *
53 * Sends an event to connection state machine. After processing event
54 * (executing it's actions and changing state), upper layer will be
55 * indicated or confirmed, if needed. Returns 0 for success, 1 for
56 * failure. The socket lock has to be held before calling this function.
57 */
58int llc_conn_state_process(struct sock *sk, struct sk_buff *skb)
59{
60 int rc;
Arnaldo Carvalho de Melod3894242005-09-22 07:57:21 -030061 struct llc_sock *llc = llc_sk(skb->sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -070062 struct llc_conn_state_ev *ev = llc_conn_ev(skb);
63
64 /*
65 * We have to hold the skb, because llc_conn_service will kfree it in
66 * the sending path and we need to look at the skb->cb, where we encode
67 * llc_conn_state_ev.
68 */
69 skb_get(skb);
70 ev->ind_prim = ev->cfm_prim = 0;
Arnaldo Carvalho de Melod3894242005-09-22 07:57:21 -030071 /*
72 * Send event to state machine
73 */
74 rc = llc_conn_service(skb->sk, skb);
Arnaldo Carvalho de Meloaf426d32005-09-22 03:59:22 -030075 if (unlikely(rc != 0)) {
Harvey Harrison0dc47872008-03-05 20:47:47 -080076 printk(KERN_ERR "%s: llc_conn_service failed\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -070077 goto out_kfree_skb;
78 }
79
Arnaldo Carvalho de Meloaf426d32005-09-22 03:59:22 -030080 if (unlikely(!ev->ind_prim && !ev->cfm_prim)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070081 /* indicate or confirm not required */
David S. Miller8728b832005-08-09 19:25:21 -070082 if (!skb->next)
Linus Torvalds1da177e2005-04-16 15:20:36 -070083 goto out_kfree_skb;
84 goto out_skb_put;
85 }
86
Arnaldo Carvalho de Meloaf426d32005-09-22 03:59:22 -030087 if (unlikely(ev->ind_prim && ev->cfm_prim)) /* Paranoia */
Linus Torvalds1da177e2005-04-16 15:20:36 -070088 skb_get(skb);
89
90 switch (ev->ind_prim) {
91 case LLC_DATA_PRIM:
Arnaldo Carvalho de Melo04e42232005-09-22 04:40:59 -030092 llc_save_primitive(sk, skb, LLC_DATA_PRIM);
93 if (unlikely(sock_queue_rcv_skb(sk, skb))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070094 /*
95 * shouldn't happen
96 */
97 printk(KERN_ERR "%s: sock_queue_rcv_skb failed!\n",
Harvey Harrison0dc47872008-03-05 20:47:47 -080098 __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -070099 kfree_skb(skb);
100 }
101 break;
Arnaldo Carvalho de Melod3894242005-09-22 07:57:21 -0300102 case LLC_CONN_PRIM:
Arnaldo Carvalho de Melo04e42232005-09-22 04:40:59 -0300103 /*
Arnaldo Carvalho de Melod3894242005-09-22 07:57:21 -0300104 * Can't be sock_queue_rcv_skb, because we have to leave the
105 * skb->sk pointing to the newly created struct sock in
106 * llc_conn_handler. -acme
Arnaldo Carvalho de Melo04e42232005-09-22 04:40:59 -0300107 */
Arnaldo Carvalho de Melod3894242005-09-22 07:57:21 -0300108 skb_queue_tail(&sk->sk_receive_queue, skb);
109 sk->sk_state_change(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110 break;
111 case LLC_DISC_PRIM:
112 sock_hold(sk);
113 if (sk->sk_type == SOCK_STREAM &&
114 sk->sk_state == TCP_ESTABLISHED) {
115 sk->sk_shutdown = SHUTDOWN_MASK;
116 sk->sk_socket->state = SS_UNCONNECTED;
117 sk->sk_state = TCP_CLOSE;
118 if (!sock_flag(sk, SOCK_DEAD)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119 sock_set_flag(sk, SOCK_DEAD);
Arnaldo Carvalho de Melo8420e1b2005-09-22 08:29:08 -0300120 sk->sk_state_change(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121 }
122 }
123 kfree_skb(skb);
124 sock_put(sk);
125 break;
126 case LLC_RESET_PRIM:
127 /*
128 * FIXME:
129 * RESET is not being notified to upper layers for now
130 */
Harvey Harrison0dc47872008-03-05 20:47:47 -0800131 printk(KERN_INFO "%s: received a reset ind!\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132 kfree_skb(skb);
133 break;
134 default:
135 if (ev->ind_prim) {
136 printk(KERN_INFO "%s: received unknown %d prim!\n",
Harvey Harrison0dc47872008-03-05 20:47:47 -0800137 __func__, ev->ind_prim);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138 kfree_skb(skb);
139 }
140 /* No indication */
141 break;
142 }
143
144 switch (ev->cfm_prim) {
145 case LLC_DATA_PRIM:
146 if (!llc_data_accept_state(llc->state))
147 sk->sk_write_space(sk);
148 else
149 rc = llc->failed_data_req = 1;
150 break;
151 case LLC_CONN_PRIM:
152 if (sk->sk_type == SOCK_STREAM &&
153 sk->sk_state == TCP_SYN_SENT) {
154 if (ev->status) {
155 sk->sk_socket->state = SS_UNCONNECTED;
156 sk->sk_state = TCP_CLOSE;
157 } else {
158 sk->sk_socket->state = SS_CONNECTED;
159 sk->sk_state = TCP_ESTABLISHED;
160 }
161 sk->sk_state_change(sk);
162 }
163 break;
164 case LLC_DISC_PRIM:
165 sock_hold(sk);
166 if (sk->sk_type == SOCK_STREAM && sk->sk_state == TCP_CLOSING) {
167 sk->sk_socket->state = SS_UNCONNECTED;
168 sk->sk_state = TCP_CLOSE;
169 sk->sk_state_change(sk);
170 }
171 sock_put(sk);
172 break;
173 case LLC_RESET_PRIM:
174 /*
175 * FIXME:
176 * RESET is not being notified to upper layers for now
177 */
Harvey Harrison0dc47872008-03-05 20:47:47 -0800178 printk(KERN_INFO "%s: received a reset conf!\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179 break;
180 default:
181 if (ev->cfm_prim) {
182 printk(KERN_INFO "%s: received unknown %d prim!\n",
Harvey Harrison0dc47872008-03-05 20:47:47 -0800183 __func__, ev->cfm_prim);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184 break;
185 }
186 goto out_skb_put; /* No confirmation */
187 }
188out_kfree_skb:
189 kfree_skb(skb);
190out_skb_put:
191 kfree_skb(skb);
192 return rc;
193}
194
195void llc_conn_send_pdu(struct sock *sk, struct sk_buff *skb)
196{
197 /* queue PDU to send to MAC layer */
198 skb_queue_tail(&sk->sk_write_queue, skb);
199 llc_conn_send_pdus(sk);
200}
201
202/**
203 * llc_conn_rtn_pdu - sends received data pdu to upper layer
204 * @sk: Active connection
205 * @skb: Received data frame
206 *
207 * Sends received data pdu to upper layer (by using indicate function).
208 * Prepares service parameters (prim and prim_data). calling indication
209 * function will be done in llc_conn_state_process.
210 */
211void llc_conn_rtn_pdu(struct sock *sk, struct sk_buff *skb)
212{
213 struct llc_conn_state_ev *ev = llc_conn_ev(skb);
214
215 ev->ind_prim = LLC_DATA_PRIM;
216}
217
218/**
219 * llc_conn_resend_i_pdu_as_cmd - resend all all unacknowledged I PDUs
220 * @sk: active connection
221 * @nr: NR
222 * @first_p_bit: p_bit value of first pdu
223 *
224 * Resend all unacknowledged I PDUs, starting with the NR; send first as
225 * command PDU with P bit equal first_p_bit; if more than one send
226 * subsequent as command PDUs with P bit equal zero (0).
227 */
228void llc_conn_resend_i_pdu_as_cmd(struct sock *sk, u8 nr, u8 first_p_bit)
229{
230 struct sk_buff *skb;
231 struct llc_pdu_sn *pdu;
232 u16 nbr_unack_pdus;
233 struct llc_sock *llc;
234 u8 howmany_resend = 0;
235
236 llc_conn_remove_acked_pdus(sk, nr, &nbr_unack_pdus);
237 if (!nbr_unack_pdus)
238 goto out;
239 /*
240 * Process unack PDUs only if unack queue is not empty; remove
241 * appropriate PDUs, fix them up, and put them on mac_pdu_q.
242 */
243 llc = llc_sk(sk);
244
245 while ((skb = skb_dequeue(&llc->pdu_unack_q)) != NULL) {
246 pdu = llc_pdu_sn_hdr(skb);
247 llc_pdu_set_cmd_rsp(skb, LLC_PDU_CMD);
248 llc_pdu_set_pf_bit(skb, first_p_bit);
249 skb_queue_tail(&sk->sk_write_queue, skb);
250 first_p_bit = 0;
251 llc->vS = LLC_I_GET_NS(pdu);
252 howmany_resend++;
253 }
254 if (howmany_resend > 0)
255 llc->vS = (llc->vS + 1) % LLC_2_SEQ_NBR_MODULO;
256 /* any PDUs to re-send are queued up; start sending to MAC */
257 llc_conn_send_pdus(sk);
258out:;
259}
260
261/**
262 * llc_conn_resend_i_pdu_as_rsp - Resend all unacknowledged I PDUs
263 * @sk: active connection.
264 * @nr: NR
265 * @first_f_bit: f_bit value of first pdu.
266 *
267 * Resend all unacknowledged I PDUs, starting with the NR; send first as
268 * response PDU with F bit equal first_f_bit; if more than one send
269 * subsequent as response PDUs with F bit equal zero (0).
270 */
271void llc_conn_resend_i_pdu_as_rsp(struct sock *sk, u8 nr, u8 first_f_bit)
272{
273 struct sk_buff *skb;
274 u16 nbr_unack_pdus;
275 struct llc_sock *llc = llc_sk(sk);
276 u8 howmany_resend = 0;
277
278 llc_conn_remove_acked_pdus(sk, nr, &nbr_unack_pdus);
279 if (!nbr_unack_pdus)
280 goto out;
281 /*
282 * Process unack PDUs only if unack queue is not empty; remove
283 * appropriate PDUs, fix them up, and put them on mac_pdu_q
284 */
285 while ((skb = skb_dequeue(&llc->pdu_unack_q)) != NULL) {
286 struct llc_pdu_sn *pdu = llc_pdu_sn_hdr(skb);
287
288 llc_pdu_set_cmd_rsp(skb, LLC_PDU_RSP);
289 llc_pdu_set_pf_bit(skb, first_f_bit);
290 skb_queue_tail(&sk->sk_write_queue, skb);
291 first_f_bit = 0;
292 llc->vS = LLC_I_GET_NS(pdu);
293 howmany_resend++;
294 }
295 if (howmany_resend > 0)
296 llc->vS = (llc->vS + 1) % LLC_2_SEQ_NBR_MODULO;
297 /* any PDUs to re-send are queued up; start sending to MAC */
298 llc_conn_send_pdus(sk);
299out:;
300}
301
302/**
303 * llc_conn_remove_acked_pdus - Removes acknowledged pdus from tx queue
304 * @sk: active connection
305 * nr: NR
306 * how_many_unacked: size of pdu_unack_q after removing acked pdus
307 *
308 * Removes acknowledged pdus from transmit queue (pdu_unack_q). Returns
309 * the number of pdus that removed from queue.
310 */
311int llc_conn_remove_acked_pdus(struct sock *sk, u8 nr, u16 *how_many_unacked)
312{
313 int pdu_pos, i;
314 struct sk_buff *skb;
315 struct llc_pdu_sn *pdu;
316 int nbr_acked = 0;
317 struct llc_sock *llc = llc_sk(sk);
318 int q_len = skb_queue_len(&llc->pdu_unack_q);
319
320 if (!q_len)
321 goto out;
322 skb = skb_peek(&llc->pdu_unack_q);
323 pdu = llc_pdu_sn_hdr(skb);
324
325 /* finding position of last acked pdu in queue */
326 pdu_pos = ((int)LLC_2_SEQ_NBR_MODULO + (int)nr -
327 (int)LLC_I_GET_NS(pdu)) % LLC_2_SEQ_NBR_MODULO;
328
329 for (i = 0; i < pdu_pos && i < q_len; i++) {
330 skb = skb_dequeue(&llc->pdu_unack_q);
Wei Yongjunc3431ea2009-02-25 00:42:22 +0000331 kfree_skb(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700332 nbr_acked++;
333 }
334out:
335 *how_many_unacked = skb_queue_len(&llc->pdu_unack_q);
336 return nbr_acked;
337}
338
339/**
340 * llc_conn_send_pdus - Sends queued PDUs
341 * @sk: active connection
342 *
343 * Sends queued pdus to MAC layer for transmission.
344 */
345static void llc_conn_send_pdus(struct sock *sk)
346{
347 struct sk_buff *skb;
348
349 while ((skb = skb_dequeue(&sk->sk_write_queue)) != NULL) {
350 struct llc_pdu_sn *pdu = llc_pdu_sn_hdr(skb);
351
352 if (LLC_PDU_TYPE_IS_I(pdu) &&
353 !(skb->dev->flags & IFF_LOOPBACK)) {
354 struct sk_buff *skb2 = skb_clone(skb, GFP_ATOMIC);
355
356 skb_queue_tail(&llc_sk(sk)->pdu_unack_q, skb);
357 if (!skb2)
358 break;
359 skb = skb2;
360 }
361 dev_queue_xmit(skb);
362 }
363}
364
365/**
366 * llc_conn_service - finds transition and changes state of connection
367 * @sk: connection
368 * @skb: happened event
369 *
370 * This function finds transition that matches with happened event, then
371 * executes related actions and finally changes state of connection.
372 * Returns 0 for success, 1 for failure.
373 */
374static int llc_conn_service(struct sock *sk, struct sk_buff *skb)
375{
376 int rc = 1;
377 struct llc_sock *llc = llc_sk(sk);
378 struct llc_conn_state_trans *trans;
379
380 if (llc->state > NBR_CONN_STATES)
381 goto out;
382 rc = 0;
383 trans = llc_qualify_conn_ev(sk, skb);
384 if (trans) {
385 rc = llc_exec_conn_trans_actions(sk, trans, skb);
386 if (!rc && trans->next_state != NO_STATE_CHANGE) {
387 llc->state = trans->next_state;
388 if (!llc_data_accept_state(llc->state))
389 sk->sk_state_change(sk);
390 }
391 }
392out:
393 return rc;
394}
395
396/**
397 * llc_qualify_conn_ev - finds transition for event
398 * @sk: connection
399 * @skb: happened event
400 *
401 * This function finds transition that matches with happened event.
402 * Returns pointer to found transition on success, %NULL otherwise.
403 */
404static struct llc_conn_state_trans *llc_qualify_conn_ev(struct sock *sk,
405 struct sk_buff *skb)
406{
407 struct llc_conn_state_trans **next_trans;
408 llc_conn_ev_qfyr_t *next_qualifier;
409 struct llc_conn_state_ev *ev = llc_conn_ev(skb);
410 struct llc_sock *llc = llc_sk(sk);
411 struct llc_conn_state *curr_state =
412 &llc_conn_state_table[llc->state - 1];
413
414 /* search thru events for this state until
415 * list exhausted or until no more
416 */
417 for (next_trans = curr_state->transitions +
418 llc_find_offset(llc->state - 1, ev->type);
419 (*next_trans)->ev; next_trans++) {
420 if (!((*next_trans)->ev)(sk, skb)) {
421 /* got POSSIBLE event match; the event may require
422 * qualification based on the values of a number of
423 * state flags; if all qualifications are met (i.e.,
424 * if all qualifying functions return success, or 0,
425 * then this is THE event we're looking for
426 */
427 for (next_qualifier = (*next_trans)->ev_qualifiers;
428 next_qualifier && *next_qualifier &&
429 !(*next_qualifier)(sk, skb); next_qualifier++)
430 /* nothing */;
431 if (!next_qualifier || !*next_qualifier)
432 /* all qualifiers executed successfully; this is
433 * our transition; return it so we can perform
434 * the associated actions & change the state
435 */
436 return *next_trans;
437 }
438 }
439 return NULL;
440}
441
442/**
443 * llc_exec_conn_trans_actions - executes related actions
444 * @sk: connection
445 * @trans: transition that it's actions must be performed
446 * @skb: event
447 *
448 * Executes actions that is related to happened event. Returns 0 for
449 * success, 1 to indicate failure of at least one action.
450 */
451static int llc_exec_conn_trans_actions(struct sock *sk,
452 struct llc_conn_state_trans *trans,
453 struct sk_buff *skb)
454{
455 int rc = 0;
456 llc_conn_action_t *next_action;
457
458 for (next_action = trans->ev_actions;
459 next_action && *next_action; next_action++) {
460 int rc2 = (*next_action)(sk, skb);
461
462 if (rc2 == 2) {
463 rc = rc2;
464 break;
465 } else if (rc2)
466 rc = 1;
467 }
468 return rc;
469}
470
Octavian Purdilab76f5a82009-12-26 11:51:02 +0000471static inline bool llc_estab_match(const struct llc_sap *sap,
472 const struct llc_addr *daddr,
473 const struct llc_addr *laddr,
474 const struct sock *sk)
475{
476 struct llc_sock *llc = llc_sk(sk);
477
478 return llc->laddr.lsap == laddr->lsap &&
479 llc->daddr.lsap == daddr->lsap &&
480 llc_mac_match(llc->laddr.mac, laddr->mac) &&
481 llc_mac_match(llc->daddr.mac, daddr->mac);
482}
483
Linus Torvalds1da177e2005-04-16 15:20:36 -0700484/**
Arnaldo Carvalho de Melod3894242005-09-22 07:57:21 -0300485 * __llc_lookup_established - Finds connection for the remote/local sap/mac
Linus Torvalds1da177e2005-04-16 15:20:36 -0700486 * @sap: SAP
487 * @daddr: address of remote LLC (MAC + SAP)
488 * @laddr: address of local LLC (MAC + SAP)
489 *
490 * Search connection list of the SAP and finds connection using the remote
491 * mac, remote sap, local mac, and local sap. Returns pointer for
492 * connection found, %NULL otherwise.
Arnaldo Carvalho de Melod3894242005-09-22 07:57:21 -0300493 * Caller has to make sure local_bh is disabled.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700494 */
Arnaldo Carvalho de Melod3894242005-09-22 07:57:21 -0300495static struct sock *__llc_lookup_established(struct llc_sap *sap,
496 struct llc_addr *daddr,
497 struct llc_addr *laddr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700498{
499 struct sock *rc;
Octavian Purdilab76f5a82009-12-26 11:51:02 +0000500 struct hlist_nulls_node *node;
Octavian Purdila52d58ae2009-12-26 11:51:05 +0000501 int slot = llc_sk_laddr_hashfn(sap, laddr);
502 struct hlist_nulls_head *laddr_hb = &sap->sk_laddr_hash[slot];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700503
Octavian Purdilab76f5a82009-12-26 11:51:02 +0000504 rcu_read_lock();
505again:
Octavian Purdila52d58ae2009-12-26 11:51:05 +0000506 sk_nulls_for_each_rcu(rc, node, laddr_hb) {
Octavian Purdilab76f5a82009-12-26 11:51:02 +0000507 if (llc_estab_match(sap, daddr, laddr, rc)) {
508 /* Extra checks required by SLAB_DESTROY_BY_RCU */
509 if (unlikely(!atomic_inc_not_zero(&rc->sk_refcnt)))
510 goto again;
511 if (unlikely(llc_sk(rc)->sap != sap ||
512 !llc_estab_match(sap, daddr, laddr, rc))) {
513 sock_put(rc);
514 continue;
515 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700516 goto found;
517 }
518 }
519 rc = NULL;
Octavian Purdila52d58ae2009-12-26 11:51:05 +0000520 /*
521 * if the nulls value we got at the end of this lookup is
522 * not the expected one, we must restart lookup.
523 * We probably met an item that was moved to another chain.
524 */
525 if (unlikely(get_nulls_value(node) != slot))
526 goto again;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700527found:
Octavian Purdilab76f5a82009-12-26 11:51:02 +0000528 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700529 return rc;
530}
531
Arnaldo Carvalho de Melod3894242005-09-22 07:57:21 -0300532struct sock *llc_lookup_established(struct llc_sap *sap,
533 struct llc_addr *daddr,
534 struct llc_addr *laddr)
535{
536 struct sock *sk;
537
538 local_bh_disable();
539 sk = __llc_lookup_established(sap, daddr, laddr);
540 local_bh_enable();
541 return sk;
542}
543
Octavian Purdilab76f5a82009-12-26 11:51:02 +0000544static inline bool llc_listener_match(const struct llc_sap *sap,
545 const struct llc_addr *laddr,
546 const struct sock *sk)
547{
548 struct llc_sock *llc = llc_sk(sk);
549
550 return sk->sk_type == SOCK_STREAM && sk->sk_state == TCP_LISTEN &&
551 llc->laddr.lsap == laddr->lsap &&
Octavian Purdila52d58ae2009-12-26 11:51:05 +0000552 llc_mac_match(llc->laddr.mac, laddr->mac);
553}
554
555static struct sock *__llc_lookup_listener(struct llc_sap *sap,
556 struct llc_addr *laddr)
557{
558 struct sock *rc;
559 struct hlist_nulls_node *node;
560 int slot = llc_sk_laddr_hashfn(sap, laddr);
561 struct hlist_nulls_head *laddr_hb = &sap->sk_laddr_hash[slot];
562
563 rcu_read_lock();
564again:
565 sk_nulls_for_each_rcu(rc, node, laddr_hb) {
566 if (llc_listener_match(sap, laddr, rc)) {
567 /* Extra checks required by SLAB_DESTROY_BY_RCU */
568 if (unlikely(!atomic_inc_not_zero(&rc->sk_refcnt)))
569 goto again;
570 if (unlikely(llc_sk(rc)->sap != sap ||
571 !llc_listener_match(sap, laddr, rc))) {
572 sock_put(rc);
573 continue;
574 }
575 goto found;
576 }
577 }
578 rc = NULL;
579 /*
580 * if the nulls value we got at the end of this lookup is
581 * not the expected one, we must restart lookup.
582 * We probably met an item that was moved to another chain.
583 */
584 if (unlikely(get_nulls_value(node) != slot))
585 goto again;
586found:
587 rcu_read_unlock();
588 return rc;
Octavian Purdilab76f5a82009-12-26 11:51:02 +0000589}
590
Linus Torvalds1da177e2005-04-16 15:20:36 -0700591/**
592 * llc_lookup_listener - Finds listener for local MAC + SAP
593 * @sap: SAP
594 * @laddr: address of local LLC (MAC + SAP)
595 *
596 * Search connection list of the SAP and finds connection listening on
597 * local mac, and local sap. Returns pointer for parent socket found,
598 * %NULL otherwise.
Arnaldo Carvalho de Melod3894242005-09-22 07:57:21 -0300599 * Caller has to make sure local_bh is disabled.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700600 */
601static struct sock *llc_lookup_listener(struct llc_sap *sap,
602 struct llc_addr *laddr)
603{
Octavian Purdila52d58ae2009-12-26 11:51:05 +0000604 static struct llc_addr null_addr;
605 struct sock *rc = __llc_lookup_listener(sap, laddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700606
Octavian Purdila52d58ae2009-12-26 11:51:05 +0000607 if (!rc)
608 rc = __llc_lookup_listener(sap, &null_addr);
609
Linus Torvalds1da177e2005-04-16 15:20:36 -0700610 return rc;
611}
612
Arnaldo Carvalho de Melod3894242005-09-22 07:57:21 -0300613static struct sock *__llc_lookup(struct llc_sap *sap,
614 struct llc_addr *daddr,
615 struct llc_addr *laddr)
616{
617 struct sock *sk = __llc_lookup_established(sap, daddr, laddr);
618
619 return sk ? : llc_lookup_listener(sap, laddr);
620}
621
Linus Torvalds1da177e2005-04-16 15:20:36 -0700622/**
623 * llc_data_accept_state - designates if in this state data can be sent.
624 * @state: state of connection.
625 *
626 * Returns 0 if data can be sent, 1 otherwise.
627 */
628u8 llc_data_accept_state(u8 state)
629{
630 return state != LLC_CONN_STATE_NORMAL && state != LLC_CONN_STATE_BUSY &&
631 state != LLC_CONN_STATE_REJ;
632}
633
634/**
Arnaldo Carvalho de Melo0eb80172005-09-22 03:57:55 -0300635 * llc_find_next_offset - finds offset for next category of transitions
Linus Torvalds1da177e2005-04-16 15:20:36 -0700636 * @state: state table.
637 * @offset: start offset.
638 *
639 * Finds offset of next category of transitions in transition table.
640 * Returns the start index of next category.
641 */
Arnaldo Carvalho de Melo0eb80172005-09-22 03:57:55 -0300642static u16 __init llc_find_next_offset(struct llc_conn_state *state, u16 offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700643{
644 u16 cnt = 0;
645 struct llc_conn_state_trans **next_trans;
646
647 for (next_trans = state->transitions + offset;
648 (*next_trans)->ev; next_trans++)
649 ++cnt;
650 return cnt;
651}
652
653/**
654 * llc_build_offset_table - builds offset table of connection
655 *
656 * Fills offset table of connection state transition table
657 * (llc_offset_table).
658 */
659void __init llc_build_offset_table(void)
660{
661 struct llc_conn_state *curr_state;
662 int state, ev_type, next_offset;
663
664 for (state = 0; state < NBR_CONN_STATES; state++) {
665 curr_state = &llc_conn_state_table[state];
666 next_offset = 0;
667 for (ev_type = 0; ev_type < NBR_CONN_EV; ev_type++) {
668 llc_offset_table[state][ev_type] = next_offset;
Arnaldo Carvalho de Melo0eb80172005-09-22 03:57:55 -0300669 next_offset += llc_find_next_offset(curr_state,
670 next_offset) + 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700671 }
672 }
673}
674
675/**
676 * llc_find_offset - finds start offset of category of transitions
677 * @state: state of connection
678 * @ev_type: type of happened event
679 *
680 * Finds start offset of desired category of transitions. Returns the
681 * desired start offset.
682 */
683static int llc_find_offset(int state, int ev_type)
684{
685 int rc = 0;
686 /* at this stage, llc_offset_table[..][2] is not important. it is for
687 * init_pf_cycle and I don't know what is it.
688 */
689 switch (ev_type) {
690 case LLC_CONN_EV_TYPE_PRIM:
691 rc = llc_offset_table[state][0]; break;
692 case LLC_CONN_EV_TYPE_PDU:
693 rc = llc_offset_table[state][4]; break;
694 case LLC_CONN_EV_TYPE_SIMPLE:
695 rc = llc_offset_table[state][1]; break;
696 case LLC_CONN_EV_TYPE_P_TMR:
697 case LLC_CONN_EV_TYPE_ACK_TMR:
698 case LLC_CONN_EV_TYPE_REJ_TMR:
699 case LLC_CONN_EV_TYPE_BUSY_TMR:
700 rc = llc_offset_table[state][3]; break;
701 }
702 return rc;
703}
704
705/**
706 * llc_sap_add_socket - adds a socket to a SAP
707 * @sap: SAP
708 * @sk: socket
709 *
Octavian Purdila52d58ae2009-12-26 11:51:05 +0000710 * This function adds a socket to the hash tables of a SAP.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700711 */
712void llc_sap_add_socket(struct llc_sap *sap, struct sock *sk)
713{
Octavian Purdila6d2e3ea2009-12-26 11:51:04 +0000714 struct llc_sock *llc = llc_sk(sk);
715 struct hlist_head *dev_hb = llc_sk_dev_hash(sap, llc->dev->ifindex);
Octavian Purdila52d58ae2009-12-26 11:51:05 +0000716 struct hlist_nulls_head *laddr_hb = llc_sk_laddr_hash(sap, &llc->laddr);
Octavian Purdila6d2e3ea2009-12-26 11:51:04 +0000717
Arnaldo Carvalho de Melo6e2144b2005-09-22 04:43:05 -0300718 llc_sap_hold(sap);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700719 llc_sk(sk)->sap = sap;
Octavian Purdila6d2e3ea2009-12-26 11:51:04 +0000720
721 spin_lock_bh(&sap->sk_lock);
Octavian Purdila52d58ae2009-12-26 11:51:05 +0000722 sap->sk_count++;
723 sk_nulls_add_node_rcu(sk, laddr_hb);
Octavian Purdila6d2e3ea2009-12-26 11:51:04 +0000724 hlist_add_head(&llc->dev_hash_node, dev_hb);
Octavian Purdilab76f5a82009-12-26 11:51:02 +0000725 spin_unlock_bh(&sap->sk_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700726}
727
728/**
729 * llc_sap_remove_socket - removes a socket from SAP
730 * @sap: SAP
731 * @sk: socket
732 *
Octavian Purdila52d58ae2009-12-26 11:51:05 +0000733 * This function removes a connection from the hash tables of a SAP if
Linus Torvalds1da177e2005-04-16 15:20:36 -0700734 * the connection was in this list.
735 */
736void llc_sap_remove_socket(struct llc_sap *sap, struct sock *sk)
737{
Octavian Purdila6d2e3ea2009-12-26 11:51:04 +0000738 struct llc_sock *llc = llc_sk(sk);
739
Octavian Purdilab76f5a82009-12-26 11:51:02 +0000740 spin_lock_bh(&sap->sk_lock);
741 sk_nulls_del_node_init_rcu(sk);
Octavian Purdila6d2e3ea2009-12-26 11:51:04 +0000742 hlist_del(&llc->dev_hash_node);
Octavian Purdila52d58ae2009-12-26 11:51:05 +0000743 sap->sk_count--;
Octavian Purdilab76f5a82009-12-26 11:51:02 +0000744 spin_unlock_bh(&sap->sk_lock);
Arnaldo Carvalho de Melo6e2144b2005-09-22 04:43:05 -0300745 llc_sap_put(sap);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700746}
747
748/**
749 * llc_conn_rcv - sends received pdus to the connection state machine
750 * @sk: current connection structure.
751 * @skb: received frame.
752 *
753 * Sends received pdus to the connection state machine.
754 */
755static int llc_conn_rcv(struct sock* sk, struct sk_buff *skb)
756{
757 struct llc_conn_state_ev *ev = llc_conn_ev(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700758
Linus Torvalds1da177e2005-04-16 15:20:36 -0700759 ev->type = LLC_CONN_EV_TYPE_PDU;
760 ev->reason = 0;
761 return llc_conn_state_process(sk, skb);
762}
763
Arnaldo Carvalho de Melod3894242005-09-22 07:57:21 -0300764static struct sock *llc_create_incoming_sock(struct sock *sk,
765 struct net_device *dev,
766 struct llc_addr *saddr,
767 struct llc_addr *daddr)
768{
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +0900769 struct sock *newsk = llc_sk_alloc(sock_net(sk), sk->sk_family, GFP_ATOMIC,
Arnaldo Carvalho de Melod3894242005-09-22 07:57:21 -0300770 sk->sk_prot);
771 struct llc_sock *newllc, *llc = llc_sk(sk);
772
773 if (!newsk)
774 goto out;
775 newllc = llc_sk(newsk);
776 memcpy(&newllc->laddr, daddr, sizeof(newllc->laddr));
777 memcpy(&newllc->daddr, saddr, sizeof(newllc->daddr));
778 newllc->dev = dev;
779 dev_hold(dev);
780 llc_sap_add_socket(llc->sap, newsk);
781 llc_sap_hold(llc->sap);
782out:
783 return newsk;
784}
785
Linus Torvalds1da177e2005-04-16 15:20:36 -0700786void llc_conn_handler(struct llc_sap *sap, struct sk_buff *skb)
787{
788 struct llc_addr saddr, daddr;
789 struct sock *sk;
790
791 llc_pdu_decode_sa(skb, saddr.mac);
792 llc_pdu_decode_ssap(skb, &saddr.lsap);
793 llc_pdu_decode_da(skb, daddr.mac);
794 llc_pdu_decode_dsap(skb, &daddr.lsap);
795
Arnaldo Carvalho de Melod3894242005-09-22 07:57:21 -0300796 sk = __llc_lookup(sap, &saddr, &daddr);
797 if (!sk)
798 goto drop;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700799
Linus Torvalds1da177e2005-04-16 15:20:36 -0700800 bh_lock_sock(sk);
Arnaldo Carvalho de Melod3894242005-09-22 07:57:21 -0300801 /*
802 * This has to be done here and not at the upper layer ->accept
803 * method because of the way the PROCOM state machine works:
804 * it needs to set several state variables (see, for instance,
805 * llc_adm_actions_2 in net/llc/llc_c_st.c) and send a packet to
806 * the originator of the new connection, and this state has to be
807 * in the newly created struct sock private area. -acme
808 */
809 if (unlikely(sk->sk_state == TCP_LISTEN)) {
810 struct sock *newsk = llc_create_incoming_sock(sk, skb->dev,
811 &saddr, &daddr);
812 if (!newsk)
813 goto drop_unlock;
814 skb_set_owner_r(skb, newsk);
815 } else {
816 /*
817 * Can't be skb_set_owner_r, this will be done at the
818 * llc_conn_state_process function, later on, when we will use
819 * skb_queue_rcv_skb to send it to upper layers, this is
820 * another trick required to cope with how the PROCOM state
821 * machine works. -acme
822 */
823 skb->sk = sk;
824 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700825 if (!sock_owned_by_user(sk))
826 llc_conn_rcv(sk, skb);
827 else {
Harvey Harrison0dc47872008-03-05 20:47:47 -0800828 dprintk("%s: adding to backlog...\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700829 llc_set_backlog_type(skb, LLC_PACKET);
830 sk_add_backlog(sk, skb);
831 }
Arnaldo Carvalho de Melod3894242005-09-22 07:57:21 -0300832out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700833 bh_unlock_sock(sk);
834 sock_put(sk);
835 return;
836drop:
837 kfree_skb(skb);
Arnaldo Carvalho de Melod3894242005-09-22 07:57:21 -0300838 return;
839drop_unlock:
840 kfree_skb(skb);
841 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700842}
843
844#undef LLC_REFCNT_DEBUG
845#ifdef LLC_REFCNT_DEBUG
846static atomic_t llc_sock_nr;
847#endif
848
849/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700850 * llc_backlog_rcv - Processes rx frames and expired timers.
851 * @sk: LLC sock (p8022 connection)
852 * @skb: queued rx frame or event
853 *
854 * This function processes frames that has received and timers that has
855 * expired during sending an I pdu (refer to data_req_handler). frames
856 * queue by llc_rcv function (llc_mac.c) and timers queue by timer
857 * callback functions(llc_c_ac.c).
858 */
859static int llc_backlog_rcv(struct sock *sk, struct sk_buff *skb)
860{
861 int rc = 0;
862 struct llc_sock *llc = llc_sk(sk);
863
Arnaldo Carvalho de Meloaf426d32005-09-22 03:59:22 -0300864 if (likely(llc_backlog_type(skb) == LLC_PACKET)) {
865 if (likely(llc->state > 1)) /* not closed */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700866 rc = llc_conn_rcv(sk, skb);
867 else
868 goto out_kfree_skb;
869 } else if (llc_backlog_type(skb) == LLC_EVENT) {
870 /* timer expiration event */
Arnaldo Carvalho de Meloaf426d32005-09-22 03:59:22 -0300871 if (likely(llc->state > 1)) /* not closed */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700872 rc = llc_conn_state_process(sk, skb);
873 else
874 goto out_kfree_skb;
875 } else {
Harvey Harrison0dc47872008-03-05 20:47:47 -0800876 printk(KERN_ERR "%s: invalid skb in backlog\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700877 goto out_kfree_skb;
878 }
879out:
880 return rc;
881out_kfree_skb:
882 kfree_skb(skb);
883 goto out;
884}
885
886/**
887 * llc_sk_init - Initializes a socket with default llc values.
888 * @sk: socket to initialize.
889 *
890 * Initializes a socket with default llc values.
891 */
892static void llc_sk_init(struct sock* sk)
893{
894 struct llc_sock *llc = llc_sk(sk);
895
896 llc->state = LLC_CONN_STATE_ADM;
897 llc->inc_cntr = llc->dec_cntr = 2;
898 llc->dec_step = llc->connect_step = 1;
899
Pavel Emelyanovb24b8a22008-01-23 21:20:07 -0800900 setup_timer(&llc->ack_timer.timer, llc_conn_ack_tmr_cb,
901 (unsigned long)sk);
Arnaldo Carvalho de Melo590232a2005-09-22 04:30:44 -0300902 llc->ack_timer.expire = sysctl_llc2_ack_timeout;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700903
Pavel Emelyanovb24b8a22008-01-23 21:20:07 -0800904 setup_timer(&llc->pf_cycle_timer.timer, llc_conn_pf_cycle_tmr_cb,
905 (unsigned long)sk);
Arnaldo Carvalho de Melo590232a2005-09-22 04:30:44 -0300906 llc->pf_cycle_timer.expire = sysctl_llc2_p_timeout;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700907
Pavel Emelyanovb24b8a22008-01-23 21:20:07 -0800908 setup_timer(&llc->rej_sent_timer.timer, llc_conn_rej_tmr_cb,
909 (unsigned long)sk);
Arnaldo Carvalho de Melo590232a2005-09-22 04:30:44 -0300910 llc->rej_sent_timer.expire = sysctl_llc2_rej_timeout;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700911
Pavel Emelyanovb24b8a22008-01-23 21:20:07 -0800912 setup_timer(&llc->busy_state_timer.timer, llc_conn_busy_tmr_cb,
913 (unsigned long)sk);
Arnaldo Carvalho de Melo590232a2005-09-22 04:30:44 -0300914 llc->busy_state_timer.expire = sysctl_llc2_busy_timeout;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700915
916 llc->n2 = 2; /* max retransmit */
917 llc->k = 2; /* tx win size, will adjust dynam */
918 llc->rw = 128; /* rx win size (opt and equal to
YOSHIFUJI Hideakid57b1862007-02-09 23:25:01 +0900919 * tx_win of remote LLC) */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700920 skb_queue_head_init(&llc->pdu_unack_q);
921 sk->sk_backlog_rcv = llc_backlog_rcv;
922}
923
924/**
925 * llc_sk_alloc - Allocates LLC sock
926 * @family: upper layer protocol family
927 * @priority: for allocation (%GFP_KERNEL, %GFP_ATOMIC, etc)
928 *
929 * Allocates a LLC sock and initializes it. Returns the new LLC sock
930 * or %NULL if there's no memory available for one
931 */
Eric W. Biederman1b8d7ae2007-10-08 23:24:22 -0700932struct sock *llc_sk_alloc(struct net *net, int family, gfp_t priority, struct proto *prot)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700933{
Pavel Emelyanov6257ff22007-11-01 00:39:31 -0700934 struct sock *sk = sk_alloc(net, family, priority, prot);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700935
936 if (!sk)
937 goto out;
938 llc_sk_init(sk);
939 sock_init_data(NULL, sk);
940#ifdef LLC_REFCNT_DEBUG
941 atomic_inc(&llc_sock_nr);
942 printk(KERN_DEBUG "LLC socket %p created in %s, now we have %d alive\n", sk,
Harvey Harrison0dc47872008-03-05 20:47:47 -0800943 __func__, atomic_read(&llc_sock_nr));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700944#endif
945out:
946 return sk;
947}
948
949/**
950 * llc_sk_free - Frees a LLC socket
951 * @sk - socket to free
952 *
953 * Frees a LLC socket
954 */
955void llc_sk_free(struct sock *sk)
956{
957 struct llc_sock *llc = llc_sk(sk);
958
959 llc->state = LLC_CONN_OUT_OF_SVC;
960 /* Stop all (possibly) running timers */
961 llc_conn_ac_stop_all_timers(sk, NULL);
962#ifdef DEBUG_LLC_CONN_ALLOC
Harvey Harrison0dc47872008-03-05 20:47:47 -0800963 printk(KERN_INFO "%s: unackq=%d, txq=%d\n", __func__,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700964 skb_queue_len(&llc->pdu_unack_q),
965 skb_queue_len(&sk->sk_write_queue));
966#endif
967 skb_queue_purge(&sk->sk_receive_queue);
968 skb_queue_purge(&sk->sk_write_queue);
969 skb_queue_purge(&llc->pdu_unack_q);
970#ifdef LLC_REFCNT_DEBUG
971 if (atomic_read(&sk->sk_refcnt) != 1) {
972 printk(KERN_DEBUG "Destruction of LLC sock %p delayed in %s, cnt=%d\n",
Harvey Harrison0dc47872008-03-05 20:47:47 -0800973 sk, __func__, atomic_read(&sk->sk_refcnt));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700974 printk(KERN_DEBUG "%d LLC sockets are still alive\n",
975 atomic_read(&llc_sock_nr));
976 } else {
977 atomic_dec(&llc_sock_nr);
978 printk(KERN_DEBUG "LLC socket %p released in %s, %d are still alive\n", sk,
Harvey Harrison0dc47872008-03-05 20:47:47 -0800979 __func__, atomic_read(&llc_sock_nr));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700980 }
981#endif
982 sock_put(sk);
983}
984
985/**
986 * llc_sk_reset - resets a connection
987 * @sk: LLC socket to reset
988 *
989 * Resets a connection to the out of service state. Stops its timers
990 * and frees any frames in the queues of the connection.
991 */
992void llc_sk_reset(struct sock *sk)
993{
994 struct llc_sock *llc = llc_sk(sk);
995
996 llc_conn_ac_stop_all_timers(sk, NULL);
997 skb_queue_purge(&sk->sk_write_queue);
998 skb_queue_purge(&llc->pdu_unack_q);
999 llc->remote_busy_flag = 0;
1000 llc->cause_flag = 0;
1001 llc->retry_count = 0;
1002 llc_conn_set_p_flag(sk, 0);
1003 llc->f_flag = 0;
1004 llc->s_flag = 0;
1005 llc->ack_pf = 0;
1006 llc->first_pdu_Ns = 0;
1007 llc->ack_must_be_send = 0;
1008 llc->dec_step = 1;
1009 llc->inc_cntr = 2;
1010 llc->dec_cntr = 2;
1011 llc->X = 0;
1012 llc->failed_data_req = 0 ;
1013 llc->last_nr = 0;
1014}