blob: 441bc18f996d51d47d482a1a3c97ab8e8f2439b7 [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)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070076 printk(KERN_ERR "%s: llc_conn_service failed\n", __FUNCTION__);
77 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 /* XXX this is not very pretty, perhaps we should store
83 * XXX indicate/confirm-needed state in the llc_conn_state_ev
84 * XXX control block of the SKB instead? -DaveM
85 */
86 if (!skb->next)
Linus Torvalds1da177e2005-04-16 15:20:36 -070087 goto out_kfree_skb;
88 goto out_skb_put;
89 }
90
Arnaldo Carvalho de Meloaf426d32005-09-22 03:59:22 -030091 if (unlikely(ev->ind_prim && ev->cfm_prim)) /* Paranoia */
Linus Torvalds1da177e2005-04-16 15:20:36 -070092 skb_get(skb);
93
94 switch (ev->ind_prim) {
95 case LLC_DATA_PRIM:
Arnaldo Carvalho de Melo04e42232005-09-22 04:40:59 -030096 llc_save_primitive(sk, skb, LLC_DATA_PRIM);
97 if (unlikely(sock_queue_rcv_skb(sk, skb))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070098 /*
99 * shouldn't happen
100 */
101 printk(KERN_ERR "%s: sock_queue_rcv_skb failed!\n",
102 __FUNCTION__);
103 kfree_skb(skb);
104 }
105 break;
Arnaldo Carvalho de Melod3894242005-09-22 07:57:21 -0300106 case LLC_CONN_PRIM:
Arnaldo Carvalho de Melo04e42232005-09-22 04:40:59 -0300107 /*
Arnaldo Carvalho de Melod3894242005-09-22 07:57:21 -0300108 * Can't be sock_queue_rcv_skb, because we have to leave the
109 * skb->sk pointing to the newly created struct sock in
110 * llc_conn_handler. -acme
Arnaldo Carvalho de Melo04e42232005-09-22 04:40:59 -0300111 */
Arnaldo Carvalho de Melod3894242005-09-22 07:57:21 -0300112 skb_queue_tail(&sk->sk_receive_queue, skb);
113 sk->sk_state_change(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114 break;
115 case LLC_DISC_PRIM:
116 sock_hold(sk);
117 if (sk->sk_type == SOCK_STREAM &&
118 sk->sk_state == TCP_ESTABLISHED) {
119 sk->sk_shutdown = SHUTDOWN_MASK;
120 sk->sk_socket->state = SS_UNCONNECTED;
121 sk->sk_state = TCP_CLOSE;
122 if (!sock_flag(sk, SOCK_DEAD)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123 sock_set_flag(sk, SOCK_DEAD);
Arnaldo Carvalho de Melo8420e1b2005-09-22 08:29:08 -0300124 sk->sk_state_change(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125 }
126 }
127 kfree_skb(skb);
128 sock_put(sk);
129 break;
130 case LLC_RESET_PRIM:
131 /*
132 * FIXME:
133 * RESET is not being notified to upper layers for now
134 */
135 printk(KERN_INFO "%s: received a reset ind!\n", __FUNCTION__);
136 kfree_skb(skb);
137 break;
138 default:
139 if (ev->ind_prim) {
140 printk(KERN_INFO "%s: received unknown %d prim!\n",
141 __FUNCTION__, ev->ind_prim);
142 kfree_skb(skb);
143 }
144 /* No indication */
145 break;
146 }
147
148 switch (ev->cfm_prim) {
149 case LLC_DATA_PRIM:
150 if (!llc_data_accept_state(llc->state))
151 sk->sk_write_space(sk);
152 else
153 rc = llc->failed_data_req = 1;
154 break;
155 case LLC_CONN_PRIM:
156 if (sk->sk_type == SOCK_STREAM &&
157 sk->sk_state == TCP_SYN_SENT) {
158 if (ev->status) {
159 sk->sk_socket->state = SS_UNCONNECTED;
160 sk->sk_state = TCP_CLOSE;
161 } else {
162 sk->sk_socket->state = SS_CONNECTED;
163 sk->sk_state = TCP_ESTABLISHED;
164 }
165 sk->sk_state_change(sk);
166 }
167 break;
168 case LLC_DISC_PRIM:
169 sock_hold(sk);
170 if (sk->sk_type == SOCK_STREAM && sk->sk_state == TCP_CLOSING) {
171 sk->sk_socket->state = SS_UNCONNECTED;
172 sk->sk_state = TCP_CLOSE;
173 sk->sk_state_change(sk);
174 }
175 sock_put(sk);
176 break;
177 case LLC_RESET_PRIM:
178 /*
179 * FIXME:
180 * RESET is not being notified to upper layers for now
181 */
182 printk(KERN_INFO "%s: received a reset conf!\n", __FUNCTION__);
183 break;
184 default:
185 if (ev->cfm_prim) {
186 printk(KERN_INFO "%s: received unknown %d prim!\n",
187 __FUNCTION__, ev->cfm_prim);
188 break;
189 }
190 goto out_skb_put; /* No confirmation */
191 }
192out_kfree_skb:
193 kfree_skb(skb);
194out_skb_put:
195 kfree_skb(skb);
196 return rc;
197}
198
199void llc_conn_send_pdu(struct sock *sk, struct sk_buff *skb)
200{
201 /* queue PDU to send to MAC layer */
202 skb_queue_tail(&sk->sk_write_queue, skb);
203 llc_conn_send_pdus(sk);
204}
205
206/**
207 * llc_conn_rtn_pdu - sends received data pdu to upper layer
208 * @sk: Active connection
209 * @skb: Received data frame
210 *
211 * Sends received data pdu to upper layer (by using indicate function).
212 * Prepares service parameters (prim and prim_data). calling indication
213 * function will be done in llc_conn_state_process.
214 */
215void llc_conn_rtn_pdu(struct sock *sk, struct sk_buff *skb)
216{
217 struct llc_conn_state_ev *ev = llc_conn_ev(skb);
218
219 ev->ind_prim = LLC_DATA_PRIM;
220}
221
222/**
223 * llc_conn_resend_i_pdu_as_cmd - resend all all unacknowledged I PDUs
224 * @sk: active connection
225 * @nr: NR
226 * @first_p_bit: p_bit value of first pdu
227 *
228 * Resend all unacknowledged I PDUs, starting with the NR; send first as
229 * command PDU with P bit equal first_p_bit; if more than one send
230 * subsequent as command PDUs with P bit equal zero (0).
231 */
232void llc_conn_resend_i_pdu_as_cmd(struct sock *sk, u8 nr, u8 first_p_bit)
233{
234 struct sk_buff *skb;
235 struct llc_pdu_sn *pdu;
236 u16 nbr_unack_pdus;
237 struct llc_sock *llc;
238 u8 howmany_resend = 0;
239
240 llc_conn_remove_acked_pdus(sk, nr, &nbr_unack_pdus);
241 if (!nbr_unack_pdus)
242 goto out;
243 /*
244 * Process unack PDUs only if unack queue is not empty; remove
245 * appropriate PDUs, fix them up, and put them on mac_pdu_q.
246 */
247 llc = llc_sk(sk);
248
249 while ((skb = skb_dequeue(&llc->pdu_unack_q)) != NULL) {
250 pdu = llc_pdu_sn_hdr(skb);
251 llc_pdu_set_cmd_rsp(skb, LLC_PDU_CMD);
252 llc_pdu_set_pf_bit(skb, first_p_bit);
253 skb_queue_tail(&sk->sk_write_queue, skb);
254 first_p_bit = 0;
255 llc->vS = LLC_I_GET_NS(pdu);
256 howmany_resend++;
257 }
258 if (howmany_resend > 0)
259 llc->vS = (llc->vS + 1) % LLC_2_SEQ_NBR_MODULO;
260 /* any PDUs to re-send are queued up; start sending to MAC */
261 llc_conn_send_pdus(sk);
262out:;
263}
264
265/**
266 * llc_conn_resend_i_pdu_as_rsp - Resend all unacknowledged I PDUs
267 * @sk: active connection.
268 * @nr: NR
269 * @first_f_bit: f_bit value of first pdu.
270 *
271 * Resend all unacknowledged I PDUs, starting with the NR; send first as
272 * response PDU with F bit equal first_f_bit; if more than one send
273 * subsequent as response PDUs with F bit equal zero (0).
274 */
275void llc_conn_resend_i_pdu_as_rsp(struct sock *sk, u8 nr, u8 first_f_bit)
276{
277 struct sk_buff *skb;
278 u16 nbr_unack_pdus;
279 struct llc_sock *llc = llc_sk(sk);
280 u8 howmany_resend = 0;
281
282 llc_conn_remove_acked_pdus(sk, nr, &nbr_unack_pdus);
283 if (!nbr_unack_pdus)
284 goto out;
285 /*
286 * Process unack PDUs only if unack queue is not empty; remove
287 * appropriate PDUs, fix them up, and put them on mac_pdu_q
288 */
289 while ((skb = skb_dequeue(&llc->pdu_unack_q)) != NULL) {
290 struct llc_pdu_sn *pdu = llc_pdu_sn_hdr(skb);
291
292 llc_pdu_set_cmd_rsp(skb, LLC_PDU_RSP);
293 llc_pdu_set_pf_bit(skb, first_f_bit);
294 skb_queue_tail(&sk->sk_write_queue, skb);
295 first_f_bit = 0;
296 llc->vS = LLC_I_GET_NS(pdu);
297 howmany_resend++;
298 }
299 if (howmany_resend > 0)
300 llc->vS = (llc->vS + 1) % LLC_2_SEQ_NBR_MODULO;
301 /* any PDUs to re-send are queued up; start sending to MAC */
302 llc_conn_send_pdus(sk);
303out:;
304}
305
306/**
307 * llc_conn_remove_acked_pdus - Removes acknowledged pdus from tx queue
308 * @sk: active connection
309 * nr: NR
310 * how_many_unacked: size of pdu_unack_q after removing acked pdus
311 *
312 * Removes acknowledged pdus from transmit queue (pdu_unack_q). Returns
313 * the number of pdus that removed from queue.
314 */
315int llc_conn_remove_acked_pdus(struct sock *sk, u8 nr, u16 *how_many_unacked)
316{
317 int pdu_pos, i;
318 struct sk_buff *skb;
319 struct llc_pdu_sn *pdu;
320 int nbr_acked = 0;
321 struct llc_sock *llc = llc_sk(sk);
322 int q_len = skb_queue_len(&llc->pdu_unack_q);
323
324 if (!q_len)
325 goto out;
326 skb = skb_peek(&llc->pdu_unack_q);
327 pdu = llc_pdu_sn_hdr(skb);
328
329 /* finding position of last acked pdu in queue */
330 pdu_pos = ((int)LLC_2_SEQ_NBR_MODULO + (int)nr -
331 (int)LLC_I_GET_NS(pdu)) % LLC_2_SEQ_NBR_MODULO;
332
333 for (i = 0; i < pdu_pos && i < q_len; i++) {
334 skb = skb_dequeue(&llc->pdu_unack_q);
335 if (skb)
336 kfree_skb(skb);
337 nbr_acked++;
338 }
339out:
340 *how_many_unacked = skb_queue_len(&llc->pdu_unack_q);
341 return nbr_acked;
342}
343
344/**
345 * llc_conn_send_pdus - Sends queued PDUs
346 * @sk: active connection
347 *
348 * Sends queued pdus to MAC layer for transmission.
349 */
350static void llc_conn_send_pdus(struct sock *sk)
351{
352 struct sk_buff *skb;
353
354 while ((skb = skb_dequeue(&sk->sk_write_queue)) != NULL) {
355 struct llc_pdu_sn *pdu = llc_pdu_sn_hdr(skb);
356
357 if (LLC_PDU_TYPE_IS_I(pdu) &&
358 !(skb->dev->flags & IFF_LOOPBACK)) {
359 struct sk_buff *skb2 = skb_clone(skb, GFP_ATOMIC);
360
361 skb_queue_tail(&llc_sk(sk)->pdu_unack_q, skb);
362 if (!skb2)
363 break;
364 skb = skb2;
365 }
366 dev_queue_xmit(skb);
367 }
368}
369
370/**
371 * llc_conn_service - finds transition and changes state of connection
372 * @sk: connection
373 * @skb: happened event
374 *
375 * This function finds transition that matches with happened event, then
376 * executes related actions and finally changes state of connection.
377 * Returns 0 for success, 1 for failure.
378 */
379static int llc_conn_service(struct sock *sk, struct sk_buff *skb)
380{
381 int rc = 1;
382 struct llc_sock *llc = llc_sk(sk);
383 struct llc_conn_state_trans *trans;
384
385 if (llc->state > NBR_CONN_STATES)
386 goto out;
387 rc = 0;
388 trans = llc_qualify_conn_ev(sk, skb);
389 if (trans) {
390 rc = llc_exec_conn_trans_actions(sk, trans, skb);
391 if (!rc && trans->next_state != NO_STATE_CHANGE) {
392 llc->state = trans->next_state;
393 if (!llc_data_accept_state(llc->state))
394 sk->sk_state_change(sk);
395 }
396 }
397out:
398 return rc;
399}
400
401/**
402 * llc_qualify_conn_ev - finds transition for event
403 * @sk: connection
404 * @skb: happened event
405 *
406 * This function finds transition that matches with happened event.
407 * Returns pointer to found transition on success, %NULL otherwise.
408 */
409static struct llc_conn_state_trans *llc_qualify_conn_ev(struct sock *sk,
410 struct sk_buff *skb)
411{
412 struct llc_conn_state_trans **next_trans;
413 llc_conn_ev_qfyr_t *next_qualifier;
414 struct llc_conn_state_ev *ev = llc_conn_ev(skb);
415 struct llc_sock *llc = llc_sk(sk);
416 struct llc_conn_state *curr_state =
417 &llc_conn_state_table[llc->state - 1];
418
419 /* search thru events for this state until
420 * list exhausted or until no more
421 */
422 for (next_trans = curr_state->transitions +
423 llc_find_offset(llc->state - 1, ev->type);
424 (*next_trans)->ev; next_trans++) {
425 if (!((*next_trans)->ev)(sk, skb)) {
426 /* got POSSIBLE event match; the event may require
427 * qualification based on the values of a number of
428 * state flags; if all qualifications are met (i.e.,
429 * if all qualifying functions return success, or 0,
430 * then this is THE event we're looking for
431 */
432 for (next_qualifier = (*next_trans)->ev_qualifiers;
433 next_qualifier && *next_qualifier &&
434 !(*next_qualifier)(sk, skb); next_qualifier++)
435 /* nothing */;
436 if (!next_qualifier || !*next_qualifier)
437 /* all qualifiers executed successfully; this is
438 * our transition; return it so we can perform
439 * the associated actions & change the state
440 */
441 return *next_trans;
442 }
443 }
444 return NULL;
445}
446
447/**
448 * llc_exec_conn_trans_actions - executes related actions
449 * @sk: connection
450 * @trans: transition that it's actions must be performed
451 * @skb: event
452 *
453 * Executes actions that is related to happened event. Returns 0 for
454 * success, 1 to indicate failure of at least one action.
455 */
456static int llc_exec_conn_trans_actions(struct sock *sk,
457 struct llc_conn_state_trans *trans,
458 struct sk_buff *skb)
459{
460 int rc = 0;
461 llc_conn_action_t *next_action;
462
463 for (next_action = trans->ev_actions;
464 next_action && *next_action; next_action++) {
465 int rc2 = (*next_action)(sk, skb);
466
467 if (rc2 == 2) {
468 rc = rc2;
469 break;
470 } else if (rc2)
471 rc = 1;
472 }
473 return rc;
474}
475
476/**
Arnaldo Carvalho de Melod3894242005-09-22 07:57:21 -0300477 * __llc_lookup_established - Finds connection for the remote/local sap/mac
Linus Torvalds1da177e2005-04-16 15:20:36 -0700478 * @sap: SAP
479 * @daddr: address of remote LLC (MAC + SAP)
480 * @laddr: address of local LLC (MAC + SAP)
481 *
482 * Search connection list of the SAP and finds connection using the remote
483 * mac, remote sap, local mac, and local sap. Returns pointer for
484 * connection found, %NULL otherwise.
Arnaldo Carvalho de Melod3894242005-09-22 07:57:21 -0300485 * Caller has to make sure local_bh is disabled.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700486 */
Arnaldo Carvalho de Melod3894242005-09-22 07:57:21 -0300487static struct sock *__llc_lookup_established(struct llc_sap *sap,
488 struct llc_addr *daddr,
489 struct llc_addr *laddr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700490{
491 struct sock *rc;
492 struct hlist_node *node;
493
Arnaldo Carvalho de Melod3894242005-09-22 07:57:21 -0300494 read_lock(&sap->sk_list.lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700495 sk_for_each(rc, node, &sap->sk_list.list) {
496 struct llc_sock *llc = llc_sk(rc);
497
498 if (llc->laddr.lsap == laddr->lsap &&
499 llc->daddr.lsap == daddr->lsap &&
500 llc_mac_match(llc->laddr.mac, laddr->mac) &&
501 llc_mac_match(llc->daddr.mac, daddr->mac)) {
502 sock_hold(rc);
503 goto found;
504 }
505 }
506 rc = NULL;
507found:
Arnaldo Carvalho de Melod3894242005-09-22 07:57:21 -0300508 read_unlock(&sap->sk_list.lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700509 return rc;
510}
511
Arnaldo Carvalho de Melod3894242005-09-22 07:57:21 -0300512struct sock *llc_lookup_established(struct llc_sap *sap,
513 struct llc_addr *daddr,
514 struct llc_addr *laddr)
515{
516 struct sock *sk;
517
518 local_bh_disable();
519 sk = __llc_lookup_established(sap, daddr, laddr);
520 local_bh_enable();
521 return sk;
522}
523
Linus Torvalds1da177e2005-04-16 15:20:36 -0700524/**
525 * llc_lookup_listener - Finds listener for local MAC + SAP
526 * @sap: SAP
527 * @laddr: address of local LLC (MAC + SAP)
528 *
529 * Search connection list of the SAP and finds connection listening on
530 * local mac, and local sap. Returns pointer for parent socket found,
531 * %NULL otherwise.
Arnaldo Carvalho de Melod3894242005-09-22 07:57:21 -0300532 * Caller has to make sure local_bh is disabled.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700533 */
534static struct sock *llc_lookup_listener(struct llc_sap *sap,
535 struct llc_addr *laddr)
536{
537 struct sock *rc;
538 struct hlist_node *node;
539
Arnaldo Carvalho de Melod3894242005-09-22 07:57:21 -0300540 read_lock(&sap->sk_list.lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700541 sk_for_each(rc, node, &sap->sk_list.list) {
542 struct llc_sock *llc = llc_sk(rc);
543
544 if (rc->sk_type == SOCK_STREAM && rc->sk_state == TCP_LISTEN &&
545 llc->laddr.lsap == laddr->lsap &&
546 (llc_mac_match(llc->laddr.mac, laddr->mac) ||
547 llc_mac_null(llc->laddr.mac))) {
548 sock_hold(rc);
549 goto found;
550 }
551 }
552 rc = NULL;
553found:
Arnaldo Carvalho de Melod3894242005-09-22 07:57:21 -0300554 read_unlock(&sap->sk_list.lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700555 return rc;
556}
557
Arnaldo Carvalho de Melod3894242005-09-22 07:57:21 -0300558static struct sock *__llc_lookup(struct llc_sap *sap,
559 struct llc_addr *daddr,
560 struct llc_addr *laddr)
561{
562 struct sock *sk = __llc_lookup_established(sap, daddr, laddr);
563
564 return sk ? : llc_lookup_listener(sap, laddr);
565}
566
Linus Torvalds1da177e2005-04-16 15:20:36 -0700567/**
568 * llc_data_accept_state - designates if in this state data can be sent.
569 * @state: state of connection.
570 *
571 * Returns 0 if data can be sent, 1 otherwise.
572 */
573u8 llc_data_accept_state(u8 state)
574{
575 return state != LLC_CONN_STATE_NORMAL && state != LLC_CONN_STATE_BUSY &&
576 state != LLC_CONN_STATE_REJ;
577}
578
579/**
Arnaldo Carvalho de Melo0eb80172005-09-22 03:57:55 -0300580 * llc_find_next_offset - finds offset for next category of transitions
Linus Torvalds1da177e2005-04-16 15:20:36 -0700581 * @state: state table.
582 * @offset: start offset.
583 *
584 * Finds offset of next category of transitions in transition table.
585 * Returns the start index of next category.
586 */
Arnaldo Carvalho de Melo0eb80172005-09-22 03:57:55 -0300587static u16 __init llc_find_next_offset(struct llc_conn_state *state, u16 offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700588{
589 u16 cnt = 0;
590 struct llc_conn_state_trans **next_trans;
591
592 for (next_trans = state->transitions + offset;
593 (*next_trans)->ev; next_trans++)
594 ++cnt;
595 return cnt;
596}
597
598/**
599 * llc_build_offset_table - builds offset table of connection
600 *
601 * Fills offset table of connection state transition table
602 * (llc_offset_table).
603 */
604void __init llc_build_offset_table(void)
605{
606 struct llc_conn_state *curr_state;
607 int state, ev_type, next_offset;
608
609 for (state = 0; state < NBR_CONN_STATES; state++) {
610 curr_state = &llc_conn_state_table[state];
611 next_offset = 0;
612 for (ev_type = 0; ev_type < NBR_CONN_EV; ev_type++) {
613 llc_offset_table[state][ev_type] = next_offset;
Arnaldo Carvalho de Melo0eb80172005-09-22 03:57:55 -0300614 next_offset += llc_find_next_offset(curr_state,
615 next_offset) + 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700616 }
617 }
618}
619
620/**
621 * llc_find_offset - finds start offset of category of transitions
622 * @state: state of connection
623 * @ev_type: type of happened event
624 *
625 * Finds start offset of desired category of transitions. Returns the
626 * desired start offset.
627 */
628static int llc_find_offset(int state, int ev_type)
629{
630 int rc = 0;
631 /* at this stage, llc_offset_table[..][2] is not important. it is for
632 * init_pf_cycle and I don't know what is it.
633 */
634 switch (ev_type) {
635 case LLC_CONN_EV_TYPE_PRIM:
636 rc = llc_offset_table[state][0]; break;
637 case LLC_CONN_EV_TYPE_PDU:
638 rc = llc_offset_table[state][4]; break;
639 case LLC_CONN_EV_TYPE_SIMPLE:
640 rc = llc_offset_table[state][1]; break;
641 case LLC_CONN_EV_TYPE_P_TMR:
642 case LLC_CONN_EV_TYPE_ACK_TMR:
643 case LLC_CONN_EV_TYPE_REJ_TMR:
644 case LLC_CONN_EV_TYPE_BUSY_TMR:
645 rc = llc_offset_table[state][3]; break;
646 }
647 return rc;
648}
649
650/**
651 * llc_sap_add_socket - adds a socket to a SAP
652 * @sap: SAP
653 * @sk: socket
654 *
655 * This function adds a socket to sk_list of a SAP.
656 */
657void llc_sap_add_socket(struct llc_sap *sap, struct sock *sk)
658{
Arnaldo Carvalho de Melo6e2144b2005-09-22 04:43:05 -0300659 llc_sap_hold(sap);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700660 write_lock_bh(&sap->sk_list.lock);
661 llc_sk(sk)->sap = sap;
662 sk_add_node(sk, &sap->sk_list.list);
663 write_unlock_bh(&sap->sk_list.lock);
664}
665
666/**
667 * llc_sap_remove_socket - removes a socket from SAP
668 * @sap: SAP
669 * @sk: socket
670 *
671 * This function removes a connection from sk_list.list of a SAP if
672 * the connection was in this list.
673 */
674void llc_sap_remove_socket(struct llc_sap *sap, struct sock *sk)
675{
676 write_lock_bh(&sap->sk_list.lock);
677 sk_del_node_init(sk);
678 write_unlock_bh(&sap->sk_list.lock);
Arnaldo Carvalho de Melo6e2144b2005-09-22 04:43:05 -0300679 llc_sap_put(sap);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700680}
681
682/**
683 * llc_conn_rcv - sends received pdus to the connection state machine
684 * @sk: current connection structure.
685 * @skb: received frame.
686 *
687 * Sends received pdus to the connection state machine.
688 */
689static int llc_conn_rcv(struct sock* sk, struct sk_buff *skb)
690{
691 struct llc_conn_state_ev *ev = llc_conn_ev(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700692
Linus Torvalds1da177e2005-04-16 15:20:36 -0700693 ev->type = LLC_CONN_EV_TYPE_PDU;
694 ev->reason = 0;
695 return llc_conn_state_process(sk, skb);
696}
697
Arnaldo Carvalho de Melod3894242005-09-22 07:57:21 -0300698static struct sock *llc_create_incoming_sock(struct sock *sk,
699 struct net_device *dev,
700 struct llc_addr *saddr,
701 struct llc_addr *daddr)
702{
Eric W. Biederman1b8d7ae2007-10-08 23:24:22 -0700703 struct sock *newsk = llc_sk_alloc(sk->sk_net, sk->sk_family, GFP_ATOMIC,
Arnaldo Carvalho de Melod3894242005-09-22 07:57:21 -0300704 sk->sk_prot);
705 struct llc_sock *newllc, *llc = llc_sk(sk);
706
707 if (!newsk)
708 goto out;
709 newllc = llc_sk(newsk);
710 memcpy(&newllc->laddr, daddr, sizeof(newllc->laddr));
711 memcpy(&newllc->daddr, saddr, sizeof(newllc->daddr));
712 newllc->dev = dev;
713 dev_hold(dev);
714 llc_sap_add_socket(llc->sap, newsk);
715 llc_sap_hold(llc->sap);
716out:
717 return newsk;
718}
719
Linus Torvalds1da177e2005-04-16 15:20:36 -0700720void llc_conn_handler(struct llc_sap *sap, struct sk_buff *skb)
721{
722 struct llc_addr saddr, daddr;
723 struct sock *sk;
724
725 llc_pdu_decode_sa(skb, saddr.mac);
726 llc_pdu_decode_ssap(skb, &saddr.lsap);
727 llc_pdu_decode_da(skb, daddr.mac);
728 llc_pdu_decode_dsap(skb, &daddr.lsap);
729
Arnaldo Carvalho de Melod3894242005-09-22 07:57:21 -0300730 sk = __llc_lookup(sap, &saddr, &daddr);
731 if (!sk)
732 goto drop;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700733
Linus Torvalds1da177e2005-04-16 15:20:36 -0700734 bh_lock_sock(sk);
Arnaldo Carvalho de Melod3894242005-09-22 07:57:21 -0300735 /*
736 * This has to be done here and not at the upper layer ->accept
737 * method because of the way the PROCOM state machine works:
738 * it needs to set several state variables (see, for instance,
739 * llc_adm_actions_2 in net/llc/llc_c_st.c) and send a packet to
740 * the originator of the new connection, and this state has to be
741 * in the newly created struct sock private area. -acme
742 */
743 if (unlikely(sk->sk_state == TCP_LISTEN)) {
744 struct sock *newsk = llc_create_incoming_sock(sk, skb->dev,
745 &saddr, &daddr);
746 if (!newsk)
747 goto drop_unlock;
748 skb_set_owner_r(skb, newsk);
749 } else {
750 /*
751 * Can't be skb_set_owner_r, this will be done at the
752 * llc_conn_state_process function, later on, when we will use
753 * skb_queue_rcv_skb to send it to upper layers, this is
754 * another trick required to cope with how the PROCOM state
755 * machine works. -acme
756 */
757 skb->sk = sk;
758 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700759 if (!sock_owned_by_user(sk))
760 llc_conn_rcv(sk, skb);
761 else {
762 dprintk("%s: adding to backlog...\n", __FUNCTION__);
763 llc_set_backlog_type(skb, LLC_PACKET);
764 sk_add_backlog(sk, skb);
765 }
Arnaldo Carvalho de Melod3894242005-09-22 07:57:21 -0300766out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700767 bh_unlock_sock(sk);
768 sock_put(sk);
769 return;
770drop:
771 kfree_skb(skb);
Arnaldo Carvalho de Melod3894242005-09-22 07:57:21 -0300772 return;
773drop_unlock:
774 kfree_skb(skb);
775 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700776}
777
778#undef LLC_REFCNT_DEBUG
779#ifdef LLC_REFCNT_DEBUG
780static atomic_t llc_sock_nr;
781#endif
782
783/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700784 * llc_backlog_rcv - Processes rx frames and expired timers.
785 * @sk: LLC sock (p8022 connection)
786 * @skb: queued rx frame or event
787 *
788 * This function processes frames that has received and timers that has
789 * expired during sending an I pdu (refer to data_req_handler). frames
790 * queue by llc_rcv function (llc_mac.c) and timers queue by timer
791 * callback functions(llc_c_ac.c).
792 */
793static int llc_backlog_rcv(struct sock *sk, struct sk_buff *skb)
794{
795 int rc = 0;
796 struct llc_sock *llc = llc_sk(sk);
797
Arnaldo Carvalho de Meloaf426d32005-09-22 03:59:22 -0300798 if (likely(llc_backlog_type(skb) == LLC_PACKET)) {
799 if (likely(llc->state > 1)) /* not closed */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700800 rc = llc_conn_rcv(sk, skb);
801 else
802 goto out_kfree_skb;
803 } else if (llc_backlog_type(skb) == LLC_EVENT) {
804 /* timer expiration event */
Arnaldo Carvalho de Meloaf426d32005-09-22 03:59:22 -0300805 if (likely(llc->state > 1)) /* not closed */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700806 rc = llc_conn_state_process(sk, skb);
807 else
808 goto out_kfree_skb;
809 } else {
810 printk(KERN_ERR "%s: invalid skb in backlog\n", __FUNCTION__);
811 goto out_kfree_skb;
812 }
813out:
814 return rc;
815out_kfree_skb:
816 kfree_skb(skb);
817 goto out;
818}
819
820/**
821 * llc_sk_init - Initializes a socket with default llc values.
822 * @sk: socket to initialize.
823 *
824 * Initializes a socket with default llc values.
825 */
826static void llc_sk_init(struct sock* sk)
827{
828 struct llc_sock *llc = llc_sk(sk);
829
830 llc->state = LLC_CONN_STATE_ADM;
831 llc->inc_cntr = llc->dec_cntr = 2;
832 llc->dec_step = llc->connect_step = 1;
833
Pavel Emelyanovb24b8a22008-01-23 21:20:07 -0800834 setup_timer(&llc->ack_timer.timer, llc_conn_ack_tmr_cb,
835 (unsigned long)sk);
Arnaldo Carvalho de Melo590232a2005-09-22 04:30:44 -0300836 llc->ack_timer.expire = sysctl_llc2_ack_timeout;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700837
Pavel Emelyanovb24b8a22008-01-23 21:20:07 -0800838 setup_timer(&llc->pf_cycle_timer.timer, llc_conn_pf_cycle_tmr_cb,
839 (unsigned long)sk);
Arnaldo Carvalho de Melo590232a2005-09-22 04:30:44 -0300840 llc->pf_cycle_timer.expire = sysctl_llc2_p_timeout;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700841
Pavel Emelyanovb24b8a22008-01-23 21:20:07 -0800842 setup_timer(&llc->rej_sent_timer.timer, llc_conn_rej_tmr_cb,
843 (unsigned long)sk);
Arnaldo Carvalho de Melo590232a2005-09-22 04:30:44 -0300844 llc->rej_sent_timer.expire = sysctl_llc2_rej_timeout;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700845
Pavel Emelyanovb24b8a22008-01-23 21:20:07 -0800846 setup_timer(&llc->busy_state_timer.timer, llc_conn_busy_tmr_cb,
847 (unsigned long)sk);
Arnaldo Carvalho de Melo590232a2005-09-22 04:30:44 -0300848 llc->busy_state_timer.expire = sysctl_llc2_busy_timeout;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700849
850 llc->n2 = 2; /* max retransmit */
851 llc->k = 2; /* tx win size, will adjust dynam */
852 llc->rw = 128; /* rx win size (opt and equal to
YOSHIFUJI Hideakid57b1862007-02-09 23:25:01 +0900853 * tx_win of remote LLC) */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700854 skb_queue_head_init(&llc->pdu_unack_q);
855 sk->sk_backlog_rcv = llc_backlog_rcv;
856}
857
858/**
859 * llc_sk_alloc - Allocates LLC sock
860 * @family: upper layer protocol family
861 * @priority: for allocation (%GFP_KERNEL, %GFP_ATOMIC, etc)
862 *
863 * Allocates a LLC sock and initializes it. Returns the new LLC sock
864 * or %NULL if there's no memory available for one
865 */
Eric W. Biederman1b8d7ae2007-10-08 23:24:22 -0700866struct sock *llc_sk_alloc(struct net *net, int family, gfp_t priority, struct proto *prot)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700867{
Pavel Emelyanov6257ff22007-11-01 00:39:31 -0700868 struct sock *sk = sk_alloc(net, family, priority, prot);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700869
870 if (!sk)
871 goto out;
872 llc_sk_init(sk);
873 sock_init_data(NULL, sk);
874#ifdef LLC_REFCNT_DEBUG
875 atomic_inc(&llc_sock_nr);
876 printk(KERN_DEBUG "LLC socket %p created in %s, now we have %d alive\n", sk,
877 __FUNCTION__, atomic_read(&llc_sock_nr));
878#endif
879out:
880 return sk;
881}
882
883/**
884 * llc_sk_free - Frees a LLC socket
885 * @sk - socket to free
886 *
887 * Frees a LLC socket
888 */
889void llc_sk_free(struct sock *sk)
890{
891 struct llc_sock *llc = llc_sk(sk);
892
893 llc->state = LLC_CONN_OUT_OF_SVC;
894 /* Stop all (possibly) running timers */
895 llc_conn_ac_stop_all_timers(sk, NULL);
896#ifdef DEBUG_LLC_CONN_ALLOC
897 printk(KERN_INFO "%s: unackq=%d, txq=%d\n", __FUNCTION__,
898 skb_queue_len(&llc->pdu_unack_q),
899 skb_queue_len(&sk->sk_write_queue));
900#endif
901 skb_queue_purge(&sk->sk_receive_queue);
902 skb_queue_purge(&sk->sk_write_queue);
903 skb_queue_purge(&llc->pdu_unack_q);
904#ifdef LLC_REFCNT_DEBUG
905 if (atomic_read(&sk->sk_refcnt) != 1) {
906 printk(KERN_DEBUG "Destruction of LLC sock %p delayed in %s, cnt=%d\n",
907 sk, __FUNCTION__, atomic_read(&sk->sk_refcnt));
908 printk(KERN_DEBUG "%d LLC sockets are still alive\n",
909 atomic_read(&llc_sock_nr));
910 } else {
911 atomic_dec(&llc_sock_nr);
912 printk(KERN_DEBUG "LLC socket %p released in %s, %d are still alive\n", sk,
913 __FUNCTION__, atomic_read(&llc_sock_nr));
914 }
915#endif
916 sock_put(sk);
917}
918
919/**
920 * llc_sk_reset - resets a connection
921 * @sk: LLC socket to reset
922 *
923 * Resets a connection to the out of service state. Stops its timers
924 * and frees any frames in the queues of the connection.
925 */
926void llc_sk_reset(struct sock *sk)
927{
928 struct llc_sock *llc = llc_sk(sk);
929
930 llc_conn_ac_stop_all_timers(sk, NULL);
931 skb_queue_purge(&sk->sk_write_queue);
932 skb_queue_purge(&llc->pdu_unack_q);
933 llc->remote_busy_flag = 0;
934 llc->cause_flag = 0;
935 llc->retry_count = 0;
936 llc_conn_set_p_flag(sk, 0);
937 llc->f_flag = 0;
938 llc->s_flag = 0;
939 llc->ack_pf = 0;
940 llc->first_pdu_Ns = 0;
941 llc->ack_must_be_send = 0;
942 llc->dec_step = 1;
943 llc->inc_cntr = 2;
944 llc->dec_cntr = 2;
945 llc->X = 0;
946 llc->failed_data_req = 0 ;
947 llc->last_nr = 0;
948}