blob: b9290a183a2fd8017b286bdf575916f293733fee [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>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090016#include <linux/slab.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070017#include <net/llc_sap.h>
18#include <net/llc_conn.h>
19#include <net/sock.h>
Arnaldo Carvalho de Meloc752f072005-08-09 20:08:28 -070020#include <net/tcp_states.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070021#include <net/llc_c_ev.h>
22#include <net/llc_c_ac.h>
23#include <net/llc_c_st.h>
24#include <net/llc_pdu.h>
25
26#if 0
27#define dprintk(args...) printk(KERN_DEBUG args)
28#else
29#define dprintk(args...)
30#endif
31
32static int llc_find_offset(int state, int ev_type);
Cong Wang4703f3f2018-03-26 15:08:33 -070033static int llc_conn_send_pdus(struct sock *sk, struct sk_buff *skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -070034static int llc_conn_service(struct sock *sk, struct sk_buff *skb);
35static int llc_exec_conn_trans_actions(struct sock *sk,
36 struct llc_conn_state_trans *trans,
37 struct sk_buff *ev);
38static struct llc_conn_state_trans *llc_qualify_conn_ev(struct sock *sk,
39 struct sk_buff *skb);
40
41/* Offset table on connection states transition diagram */
42static int llc_offset_table[NBR_CONN_STATES][NBR_CONN_EV];
43
Arnaldo Carvalho de Melo590232a2005-09-22 04:30:44 -030044int sysctl_llc2_ack_timeout = LLC2_ACK_TIME * HZ;
45int sysctl_llc2_p_timeout = LLC2_P_TIME * HZ;
46int sysctl_llc2_rej_timeout = LLC2_REJ_TIME * HZ;
47int sysctl_llc2_busy_timeout = LLC2_BUSY_TIME * HZ;
48
Linus Torvalds1da177e2005-04-16 15:20:36 -070049/**
50 * llc_conn_state_process - sends event to connection state machine
51 * @sk: connection
52 * @skb: occurred event
53 *
54 * Sends an event to connection state machine. After processing event
55 * (executing it's actions and changing state), upper layer will be
56 * indicated or confirmed, if needed. Returns 0 for success, 1 for
57 * failure. The socket lock has to be held before calling this function.
58 */
59int llc_conn_state_process(struct sock *sk, struct sk_buff *skb)
60{
61 int rc;
Arnaldo Carvalho de Melod3894242005-09-22 07:57:21 -030062 struct llc_sock *llc = llc_sk(skb->sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -070063 struct llc_conn_state_ev *ev = llc_conn_ev(skb);
64
65 /*
66 * We have to hold the skb, because llc_conn_service will kfree it in
67 * the sending path and we need to look at the skb->cb, where we encode
68 * llc_conn_state_ev.
69 */
70 skb_get(skb);
71 ev->ind_prim = ev->cfm_prim = 0;
Arnaldo Carvalho de Melod3894242005-09-22 07:57:21 -030072 /*
73 * Send event to state machine
74 */
75 rc = llc_conn_service(skb->sk, skb);
Arnaldo Carvalho de Meloaf426d32005-09-22 03:59:22 -030076 if (unlikely(rc != 0)) {
Harvey Harrison0dc47872008-03-05 20:47:47 -080077 printk(KERN_ERR "%s: llc_conn_service failed\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -070078 goto out_kfree_skb;
79 }
80
Arnaldo Carvalho de Meloaf426d32005-09-22 03:59:22 -030081 if (unlikely(!ev->ind_prim && !ev->cfm_prim)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070082 /* indicate or confirm not required */
David S. Miller8728b832005-08-09 19:25:21 -070083 if (!skb->next)
Linus Torvalds1da177e2005-04-16 15:20:36 -070084 goto out_kfree_skb;
85 goto out_skb_put;
86 }
87
Arnaldo Carvalho de Meloaf426d32005-09-22 03:59:22 -030088 if (unlikely(ev->ind_prim && ev->cfm_prim)) /* Paranoia */
Linus Torvalds1da177e2005-04-16 15:20:36 -070089 skb_get(skb);
90
91 switch (ev->ind_prim) {
92 case LLC_DATA_PRIM:
Arnaldo Carvalho de Melo04e42232005-09-22 04:40:59 -030093 llc_save_primitive(sk, skb, LLC_DATA_PRIM);
94 if (unlikely(sock_queue_rcv_skb(sk, skb))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070095 /*
96 * shouldn't happen
97 */
98 printk(KERN_ERR "%s: sock_queue_rcv_skb failed!\n",
Harvey Harrison0dc47872008-03-05 20:47:47 -080099 __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100 kfree_skb(skb);
101 }
102 break;
Arnaldo Carvalho de Melod3894242005-09-22 07:57:21 -0300103 case LLC_CONN_PRIM:
Arnaldo Carvalho de Melo04e42232005-09-22 04:40:59 -0300104 /*
Arnaldo Carvalho de Melod3894242005-09-22 07:57:21 -0300105 * Can't be sock_queue_rcv_skb, because we have to leave the
106 * skb->sk pointing to the newly created struct sock in
107 * llc_conn_handler. -acme
Arnaldo Carvalho de Melo04e42232005-09-22 04:40:59 -0300108 */
Arnaldo Carvalho de Melod3894242005-09-22 07:57:21 -0300109 skb_queue_tail(&sk->sk_receive_queue, skb);
110 sk->sk_state_change(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111 break;
112 case LLC_DISC_PRIM:
113 sock_hold(sk);
114 if (sk->sk_type == SOCK_STREAM &&
115 sk->sk_state == TCP_ESTABLISHED) {
116 sk->sk_shutdown = SHUTDOWN_MASK;
117 sk->sk_socket->state = SS_UNCONNECTED;
118 sk->sk_state = TCP_CLOSE;
119 if (!sock_flag(sk, SOCK_DEAD)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120 sock_set_flag(sk, SOCK_DEAD);
Arnaldo Carvalho de Melo8420e1b2005-09-22 08:29:08 -0300121 sk->sk_state_change(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122 }
123 }
124 kfree_skb(skb);
125 sock_put(sk);
126 break;
127 case LLC_RESET_PRIM:
128 /*
129 * FIXME:
130 * RESET is not being notified to upper layers for now
131 */
Harvey Harrison0dc47872008-03-05 20:47:47 -0800132 printk(KERN_INFO "%s: received a reset ind!\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133 kfree_skb(skb);
134 break;
135 default:
136 if (ev->ind_prim) {
137 printk(KERN_INFO "%s: received unknown %d prim!\n",
Harvey Harrison0dc47872008-03-05 20:47:47 -0800138 __func__, ev->ind_prim);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139 kfree_skb(skb);
140 }
141 /* No indication */
142 break;
143 }
144
145 switch (ev->cfm_prim) {
146 case LLC_DATA_PRIM:
147 if (!llc_data_accept_state(llc->state))
148 sk->sk_write_space(sk);
149 else
150 rc = llc->failed_data_req = 1;
151 break;
152 case LLC_CONN_PRIM:
153 if (sk->sk_type == SOCK_STREAM &&
154 sk->sk_state == TCP_SYN_SENT) {
155 if (ev->status) {
156 sk->sk_socket->state = SS_UNCONNECTED;
157 sk->sk_state = TCP_CLOSE;
158 } else {
159 sk->sk_socket->state = SS_CONNECTED;
160 sk->sk_state = TCP_ESTABLISHED;
161 }
162 sk->sk_state_change(sk);
163 }
164 break;
165 case LLC_DISC_PRIM:
166 sock_hold(sk);
167 if (sk->sk_type == SOCK_STREAM && sk->sk_state == TCP_CLOSING) {
168 sk->sk_socket->state = SS_UNCONNECTED;
169 sk->sk_state = TCP_CLOSE;
170 sk->sk_state_change(sk);
171 }
172 sock_put(sk);
173 break;
174 case LLC_RESET_PRIM:
175 /*
176 * FIXME:
177 * RESET is not being notified to upper layers for now
178 */
Harvey Harrison0dc47872008-03-05 20:47:47 -0800179 printk(KERN_INFO "%s: received a reset conf!\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180 break;
181 default:
182 if (ev->cfm_prim) {
183 printk(KERN_INFO "%s: received unknown %d prim!\n",
Harvey Harrison0dc47872008-03-05 20:47:47 -0800184 __func__, ev->cfm_prim);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185 break;
186 }
187 goto out_skb_put; /* No confirmation */
188 }
189out_kfree_skb:
190 kfree_skb(skb);
191out_skb_put:
192 kfree_skb(skb);
193 return rc;
194}
195
Cong Wang4703f3f2018-03-26 15:08:33 -0700196int llc_conn_send_pdu(struct sock *sk, struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700197{
198 /* queue PDU to send to MAC layer */
199 skb_queue_tail(&sk->sk_write_queue, skb);
Cong Wang4703f3f2018-03-26 15:08:33 -0700200 return llc_conn_send_pdus(sk, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201}
202
203/**
204 * llc_conn_rtn_pdu - sends received data pdu to upper layer
205 * @sk: Active connection
206 * @skb: Received data frame
207 *
208 * Sends received data pdu to upper layer (by using indicate function).
209 * Prepares service parameters (prim and prim_data). calling indication
210 * function will be done in llc_conn_state_process.
211 */
212void llc_conn_rtn_pdu(struct sock *sk, struct sk_buff *skb)
213{
214 struct llc_conn_state_ev *ev = llc_conn_ev(skb);
215
216 ev->ind_prim = LLC_DATA_PRIM;
217}
218
219/**
220 * llc_conn_resend_i_pdu_as_cmd - resend all all unacknowledged I PDUs
221 * @sk: active connection
222 * @nr: NR
223 * @first_p_bit: p_bit value of first pdu
224 *
225 * Resend all unacknowledged I PDUs, starting with the NR; send first as
226 * command PDU with P bit equal first_p_bit; if more than one send
227 * subsequent as command PDUs with P bit equal zero (0).
228 */
229void llc_conn_resend_i_pdu_as_cmd(struct sock *sk, u8 nr, u8 first_p_bit)
230{
231 struct sk_buff *skb;
232 struct llc_pdu_sn *pdu;
233 u16 nbr_unack_pdus;
234 struct llc_sock *llc;
235 u8 howmany_resend = 0;
236
237 llc_conn_remove_acked_pdus(sk, nr, &nbr_unack_pdus);
238 if (!nbr_unack_pdus)
239 goto out;
240 /*
241 * Process unack PDUs only if unack queue is not empty; remove
242 * appropriate PDUs, fix them up, and put them on mac_pdu_q.
243 */
244 llc = llc_sk(sk);
245
246 while ((skb = skb_dequeue(&llc->pdu_unack_q)) != NULL) {
247 pdu = llc_pdu_sn_hdr(skb);
248 llc_pdu_set_cmd_rsp(skb, LLC_PDU_CMD);
249 llc_pdu_set_pf_bit(skb, first_p_bit);
250 skb_queue_tail(&sk->sk_write_queue, skb);
251 first_p_bit = 0;
252 llc->vS = LLC_I_GET_NS(pdu);
253 howmany_resend++;
254 }
255 if (howmany_resend > 0)
256 llc->vS = (llc->vS + 1) % LLC_2_SEQ_NBR_MODULO;
257 /* any PDUs to re-send are queued up; start sending to MAC */
Cong Wang4703f3f2018-03-26 15:08:33 -0700258 llc_conn_send_pdus(sk, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700259out:;
260}
261
262/**
263 * llc_conn_resend_i_pdu_as_rsp - Resend all unacknowledged I PDUs
264 * @sk: active connection.
265 * @nr: NR
266 * @first_f_bit: f_bit value of first pdu.
267 *
268 * Resend all unacknowledged I PDUs, starting with the NR; send first as
269 * response PDU with F bit equal first_f_bit; if more than one send
270 * subsequent as response PDUs with F bit equal zero (0).
271 */
272void llc_conn_resend_i_pdu_as_rsp(struct sock *sk, u8 nr, u8 first_f_bit)
273{
274 struct sk_buff *skb;
275 u16 nbr_unack_pdus;
276 struct llc_sock *llc = llc_sk(sk);
277 u8 howmany_resend = 0;
278
279 llc_conn_remove_acked_pdus(sk, nr, &nbr_unack_pdus);
280 if (!nbr_unack_pdus)
281 goto out;
282 /*
283 * Process unack PDUs only if unack queue is not empty; remove
284 * appropriate PDUs, fix them up, and put them on mac_pdu_q
285 */
286 while ((skb = skb_dequeue(&llc->pdu_unack_q)) != NULL) {
287 struct llc_pdu_sn *pdu = llc_pdu_sn_hdr(skb);
288
289 llc_pdu_set_cmd_rsp(skb, LLC_PDU_RSP);
290 llc_pdu_set_pf_bit(skb, first_f_bit);
291 skb_queue_tail(&sk->sk_write_queue, skb);
292 first_f_bit = 0;
293 llc->vS = LLC_I_GET_NS(pdu);
294 howmany_resend++;
295 }
296 if (howmany_resend > 0)
297 llc->vS = (llc->vS + 1) % LLC_2_SEQ_NBR_MODULO;
298 /* any PDUs to re-send are queued up; start sending to MAC */
Cong Wang4703f3f2018-03-26 15:08:33 -0700299 llc_conn_send_pdus(sk, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700300out:;
301}
302
303/**
304 * llc_conn_remove_acked_pdus - Removes acknowledged pdus from tx queue
305 * @sk: active connection
306 * nr: NR
307 * how_many_unacked: size of pdu_unack_q after removing acked pdus
308 *
309 * Removes acknowledged pdus from transmit queue (pdu_unack_q). Returns
310 * the number of pdus that removed from queue.
311 */
312int llc_conn_remove_acked_pdus(struct sock *sk, u8 nr, u16 *how_many_unacked)
313{
314 int pdu_pos, i;
315 struct sk_buff *skb;
316 struct llc_pdu_sn *pdu;
317 int nbr_acked = 0;
318 struct llc_sock *llc = llc_sk(sk);
319 int q_len = skb_queue_len(&llc->pdu_unack_q);
320
321 if (!q_len)
322 goto out;
323 skb = skb_peek(&llc->pdu_unack_q);
324 pdu = llc_pdu_sn_hdr(skb);
325
326 /* finding position of last acked pdu in queue */
327 pdu_pos = ((int)LLC_2_SEQ_NBR_MODULO + (int)nr -
328 (int)LLC_I_GET_NS(pdu)) % LLC_2_SEQ_NBR_MODULO;
329
330 for (i = 0; i < pdu_pos && i < q_len; i++) {
331 skb = skb_dequeue(&llc->pdu_unack_q);
Wei Yongjunc3431ea2009-02-25 00:42:22 +0000332 kfree_skb(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700333 nbr_acked++;
334 }
335out:
336 *how_many_unacked = skb_queue_len(&llc->pdu_unack_q);
337 return nbr_acked;
338}
339
340/**
341 * llc_conn_send_pdus - Sends queued PDUs
342 * @sk: active connection
Cong Wang4703f3f2018-03-26 15:08:33 -0700343 * @hold_skb: the skb held by caller, or NULL if does not care
Linus Torvalds1da177e2005-04-16 15:20:36 -0700344 *
Cong Wang4703f3f2018-03-26 15:08:33 -0700345 * Sends queued pdus to MAC layer for transmission. When @hold_skb is
346 * NULL, always return 0. Otherwise, return 0 if @hold_skb is sent
347 * successfully, or 1 for failure.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700348 */
Cong Wang4703f3f2018-03-26 15:08:33 -0700349static int llc_conn_send_pdus(struct sock *sk, struct sk_buff *hold_skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700350{
351 struct sk_buff *skb;
Cong Wang4703f3f2018-03-26 15:08:33 -0700352 int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353
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;
Cong Wang4703f3f2018-03-26 15:08:33 -0700364 dev_queue_xmit(skb2);
365 } else {
366 bool is_target = skb == hold_skb;
367 int rc;
368
369 if (is_target)
370 skb_get(skb);
371 rc = dev_queue_xmit(skb);
372 if (is_target)
373 ret = rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700374 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700375 }
Cong Wang4703f3f2018-03-26 15:08:33 -0700376
377 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700378}
379
380/**
381 * llc_conn_service - finds transition and changes state of connection
382 * @sk: connection
383 * @skb: happened event
384 *
385 * This function finds transition that matches with happened event, then
386 * executes related actions and finally changes state of connection.
387 * Returns 0 for success, 1 for failure.
388 */
389static int llc_conn_service(struct sock *sk, struct sk_buff *skb)
390{
391 int rc = 1;
392 struct llc_sock *llc = llc_sk(sk);
393 struct llc_conn_state_trans *trans;
394
395 if (llc->state > NBR_CONN_STATES)
396 goto out;
397 rc = 0;
398 trans = llc_qualify_conn_ev(sk, skb);
399 if (trans) {
400 rc = llc_exec_conn_trans_actions(sk, trans, skb);
401 if (!rc && trans->next_state != NO_STATE_CHANGE) {
402 llc->state = trans->next_state;
403 if (!llc_data_accept_state(llc->state))
404 sk->sk_state_change(sk);
405 }
406 }
407out:
408 return rc;
409}
410
411/**
412 * llc_qualify_conn_ev - finds transition for event
413 * @sk: connection
414 * @skb: happened event
415 *
416 * This function finds transition that matches with happened event.
417 * Returns pointer to found transition on success, %NULL otherwise.
418 */
419static struct llc_conn_state_trans *llc_qualify_conn_ev(struct sock *sk,
420 struct sk_buff *skb)
421{
422 struct llc_conn_state_trans **next_trans;
Joe Perches9b373062014-12-10 09:43:57 -0800423 const llc_conn_ev_qfyr_t *next_qualifier;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700424 struct llc_conn_state_ev *ev = llc_conn_ev(skb);
425 struct llc_sock *llc = llc_sk(sk);
426 struct llc_conn_state *curr_state =
427 &llc_conn_state_table[llc->state - 1];
428
429 /* search thru events for this state until
430 * list exhausted or until no more
431 */
432 for (next_trans = curr_state->transitions +
433 llc_find_offset(llc->state - 1, ev->type);
434 (*next_trans)->ev; next_trans++) {
435 if (!((*next_trans)->ev)(sk, skb)) {
436 /* got POSSIBLE event match; the event may require
437 * qualification based on the values of a number of
438 * state flags; if all qualifications are met (i.e.,
439 * if all qualifying functions return success, or 0,
440 * then this is THE event we're looking for
441 */
442 for (next_qualifier = (*next_trans)->ev_qualifiers;
443 next_qualifier && *next_qualifier &&
444 !(*next_qualifier)(sk, skb); next_qualifier++)
445 /* nothing */;
446 if (!next_qualifier || !*next_qualifier)
447 /* all qualifiers executed successfully; this is
448 * our transition; return it so we can perform
449 * the associated actions & change the state
450 */
451 return *next_trans;
452 }
453 }
454 return NULL;
455}
456
457/**
458 * llc_exec_conn_trans_actions - executes related actions
459 * @sk: connection
460 * @trans: transition that it's actions must be performed
461 * @skb: event
462 *
463 * Executes actions that is related to happened event. Returns 0 for
464 * success, 1 to indicate failure of at least one action.
465 */
466static int llc_exec_conn_trans_actions(struct sock *sk,
467 struct llc_conn_state_trans *trans,
468 struct sk_buff *skb)
469{
470 int rc = 0;
Joe Perches14b7d952014-12-10 08:18:50 -0800471 const llc_conn_action_t *next_action;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700472
473 for (next_action = trans->ev_actions;
474 next_action && *next_action; next_action++) {
475 int rc2 = (*next_action)(sk, skb);
476
477 if (rc2 == 2) {
478 rc = rc2;
479 break;
480 } else if (rc2)
481 rc = 1;
482 }
483 return rc;
484}
485
Octavian Purdilab76f5a82009-12-26 11:51:02 +0000486static inline bool llc_estab_match(const struct llc_sap *sap,
487 const struct llc_addr *daddr,
488 const struct llc_addr *laddr,
489 const struct sock *sk)
490{
491 struct llc_sock *llc = llc_sk(sk);
492
493 return llc->laddr.lsap == laddr->lsap &&
494 llc->daddr.lsap == daddr->lsap &&
Joe Perches951fd872013-09-01 13:11:55 -0700495 ether_addr_equal(llc->laddr.mac, laddr->mac) &&
496 ether_addr_equal(llc->daddr.mac, daddr->mac);
Octavian Purdilab76f5a82009-12-26 11:51:02 +0000497}
498
Linus Torvalds1da177e2005-04-16 15:20:36 -0700499/**
Arnaldo Carvalho de Melod3894242005-09-22 07:57:21 -0300500 * __llc_lookup_established - Finds connection for the remote/local sap/mac
Linus Torvalds1da177e2005-04-16 15:20:36 -0700501 * @sap: SAP
502 * @daddr: address of remote LLC (MAC + SAP)
503 * @laddr: address of local LLC (MAC + SAP)
504 *
505 * Search connection list of the SAP and finds connection using the remote
506 * mac, remote sap, local mac, and local sap. Returns pointer for
507 * connection found, %NULL otherwise.
Arnaldo Carvalho de Melod3894242005-09-22 07:57:21 -0300508 * Caller has to make sure local_bh is disabled.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700509 */
Arnaldo Carvalho de Melod3894242005-09-22 07:57:21 -0300510static struct sock *__llc_lookup_established(struct llc_sap *sap,
511 struct llc_addr *daddr,
512 struct llc_addr *laddr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700513{
514 struct sock *rc;
Octavian Purdilab76f5a82009-12-26 11:51:02 +0000515 struct hlist_nulls_node *node;
Octavian Purdila52d58ae2009-12-26 11:51:05 +0000516 int slot = llc_sk_laddr_hashfn(sap, laddr);
517 struct hlist_nulls_head *laddr_hb = &sap->sk_laddr_hash[slot];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700518
Octavian Purdilab76f5a82009-12-26 11:51:02 +0000519 rcu_read_lock();
520again:
Octavian Purdila52d58ae2009-12-26 11:51:05 +0000521 sk_nulls_for_each_rcu(rc, node, laddr_hb) {
Octavian Purdilab76f5a82009-12-26 11:51:02 +0000522 if (llc_estab_match(sap, daddr, laddr, rc)) {
523 /* Extra checks required by SLAB_DESTROY_BY_RCU */
524 if (unlikely(!atomic_inc_not_zero(&rc->sk_refcnt)))
525 goto again;
526 if (unlikely(llc_sk(rc)->sap != sap ||
527 !llc_estab_match(sap, daddr, laddr, rc))) {
528 sock_put(rc);
529 continue;
530 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700531 goto found;
532 }
533 }
534 rc = NULL;
Octavian Purdila52d58ae2009-12-26 11:51:05 +0000535 /*
536 * if the nulls value we got at the end of this lookup is
537 * not the expected one, we must restart lookup.
538 * We probably met an item that was moved to another chain.
539 */
540 if (unlikely(get_nulls_value(node) != slot))
541 goto again;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700542found:
Octavian Purdilab76f5a82009-12-26 11:51:02 +0000543 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700544 return rc;
545}
546
Arnaldo Carvalho de Melod3894242005-09-22 07:57:21 -0300547struct sock *llc_lookup_established(struct llc_sap *sap,
548 struct llc_addr *daddr,
549 struct llc_addr *laddr)
550{
551 struct sock *sk;
552
553 local_bh_disable();
554 sk = __llc_lookup_established(sap, daddr, laddr);
555 local_bh_enable();
556 return sk;
557}
558
Octavian Purdilab76f5a82009-12-26 11:51:02 +0000559static inline bool llc_listener_match(const struct llc_sap *sap,
560 const struct llc_addr *laddr,
561 const struct sock *sk)
562{
563 struct llc_sock *llc = llc_sk(sk);
564
565 return sk->sk_type == SOCK_STREAM && sk->sk_state == TCP_LISTEN &&
566 llc->laddr.lsap == laddr->lsap &&
Joe Perches951fd872013-09-01 13:11:55 -0700567 ether_addr_equal(llc->laddr.mac, laddr->mac);
Octavian Purdila52d58ae2009-12-26 11:51:05 +0000568}
569
570static struct sock *__llc_lookup_listener(struct llc_sap *sap,
571 struct llc_addr *laddr)
572{
573 struct sock *rc;
574 struct hlist_nulls_node *node;
575 int slot = llc_sk_laddr_hashfn(sap, laddr);
576 struct hlist_nulls_head *laddr_hb = &sap->sk_laddr_hash[slot];
577
578 rcu_read_lock();
579again:
580 sk_nulls_for_each_rcu(rc, node, laddr_hb) {
581 if (llc_listener_match(sap, laddr, rc)) {
582 /* Extra checks required by SLAB_DESTROY_BY_RCU */
583 if (unlikely(!atomic_inc_not_zero(&rc->sk_refcnt)))
584 goto again;
585 if (unlikely(llc_sk(rc)->sap != sap ||
586 !llc_listener_match(sap, laddr, rc))) {
587 sock_put(rc);
588 continue;
589 }
590 goto found;
591 }
592 }
593 rc = NULL;
594 /*
595 * if the nulls value we got at the end of this lookup is
596 * not the expected one, we must restart lookup.
597 * We probably met an item that was moved to another chain.
598 */
599 if (unlikely(get_nulls_value(node) != slot))
600 goto again;
601found:
602 rcu_read_unlock();
603 return rc;
Octavian Purdilab76f5a82009-12-26 11:51:02 +0000604}
605
Linus Torvalds1da177e2005-04-16 15:20:36 -0700606/**
607 * llc_lookup_listener - Finds listener for local MAC + SAP
608 * @sap: SAP
609 * @laddr: address of local LLC (MAC + SAP)
610 *
611 * Search connection list of the SAP and finds connection listening on
612 * local mac, and local sap. Returns pointer for parent socket found,
613 * %NULL otherwise.
Arnaldo Carvalho de Melod3894242005-09-22 07:57:21 -0300614 * Caller has to make sure local_bh is disabled.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700615 */
616static struct sock *llc_lookup_listener(struct llc_sap *sap,
617 struct llc_addr *laddr)
618{
Octavian Purdila52d58ae2009-12-26 11:51:05 +0000619 static struct llc_addr null_addr;
620 struct sock *rc = __llc_lookup_listener(sap, laddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700621
Octavian Purdila52d58ae2009-12-26 11:51:05 +0000622 if (!rc)
623 rc = __llc_lookup_listener(sap, &null_addr);
624
Linus Torvalds1da177e2005-04-16 15:20:36 -0700625 return rc;
626}
627
Arnaldo Carvalho de Melod3894242005-09-22 07:57:21 -0300628static struct sock *__llc_lookup(struct llc_sap *sap,
629 struct llc_addr *daddr,
630 struct llc_addr *laddr)
631{
632 struct sock *sk = __llc_lookup_established(sap, daddr, laddr);
633
634 return sk ? : llc_lookup_listener(sap, laddr);
635}
636
Linus Torvalds1da177e2005-04-16 15:20:36 -0700637/**
638 * llc_data_accept_state - designates if in this state data can be sent.
639 * @state: state of connection.
640 *
641 * Returns 0 if data can be sent, 1 otherwise.
642 */
643u8 llc_data_accept_state(u8 state)
644{
645 return state != LLC_CONN_STATE_NORMAL && state != LLC_CONN_STATE_BUSY &&
646 state != LLC_CONN_STATE_REJ;
647}
648
649/**
Arnaldo Carvalho de Melo0eb80172005-09-22 03:57:55 -0300650 * llc_find_next_offset - finds offset for next category of transitions
Linus Torvalds1da177e2005-04-16 15:20:36 -0700651 * @state: state table.
652 * @offset: start offset.
653 *
654 * Finds offset of next category of transitions in transition table.
655 * Returns the start index of next category.
656 */
Arnaldo Carvalho de Melo0eb80172005-09-22 03:57:55 -0300657static u16 __init llc_find_next_offset(struct llc_conn_state *state, u16 offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700658{
659 u16 cnt = 0;
660 struct llc_conn_state_trans **next_trans;
661
662 for (next_trans = state->transitions + offset;
663 (*next_trans)->ev; next_trans++)
664 ++cnt;
665 return cnt;
666}
667
668/**
669 * llc_build_offset_table - builds offset table of connection
670 *
671 * Fills offset table of connection state transition table
672 * (llc_offset_table).
673 */
674void __init llc_build_offset_table(void)
675{
676 struct llc_conn_state *curr_state;
677 int state, ev_type, next_offset;
678
679 for (state = 0; state < NBR_CONN_STATES; state++) {
680 curr_state = &llc_conn_state_table[state];
681 next_offset = 0;
682 for (ev_type = 0; ev_type < NBR_CONN_EV; ev_type++) {
683 llc_offset_table[state][ev_type] = next_offset;
Arnaldo Carvalho de Melo0eb80172005-09-22 03:57:55 -0300684 next_offset += llc_find_next_offset(curr_state,
685 next_offset) + 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700686 }
687 }
688}
689
690/**
691 * llc_find_offset - finds start offset of category of transitions
692 * @state: state of connection
693 * @ev_type: type of happened event
694 *
695 * Finds start offset of desired category of transitions. Returns the
696 * desired start offset.
697 */
698static int llc_find_offset(int state, int ev_type)
699{
700 int rc = 0;
701 /* at this stage, llc_offset_table[..][2] is not important. it is for
702 * init_pf_cycle and I don't know what is it.
703 */
704 switch (ev_type) {
705 case LLC_CONN_EV_TYPE_PRIM:
706 rc = llc_offset_table[state][0]; break;
707 case LLC_CONN_EV_TYPE_PDU:
708 rc = llc_offset_table[state][4]; break;
709 case LLC_CONN_EV_TYPE_SIMPLE:
710 rc = llc_offset_table[state][1]; break;
711 case LLC_CONN_EV_TYPE_P_TMR:
712 case LLC_CONN_EV_TYPE_ACK_TMR:
713 case LLC_CONN_EV_TYPE_REJ_TMR:
714 case LLC_CONN_EV_TYPE_BUSY_TMR:
715 rc = llc_offset_table[state][3]; break;
716 }
717 return rc;
718}
719
720/**
721 * llc_sap_add_socket - adds a socket to a SAP
722 * @sap: SAP
723 * @sk: socket
724 *
Octavian Purdila52d58ae2009-12-26 11:51:05 +0000725 * This function adds a socket to the hash tables of a SAP.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700726 */
727void llc_sap_add_socket(struct llc_sap *sap, struct sock *sk)
728{
Octavian Purdila6d2e3ea2009-12-26 11:51:04 +0000729 struct llc_sock *llc = llc_sk(sk);
730 struct hlist_head *dev_hb = llc_sk_dev_hash(sap, llc->dev->ifindex);
Octavian Purdila52d58ae2009-12-26 11:51:05 +0000731 struct hlist_nulls_head *laddr_hb = llc_sk_laddr_hash(sap, &llc->laddr);
Octavian Purdila6d2e3ea2009-12-26 11:51:04 +0000732
Arnaldo Carvalho de Melo6e2144b2005-09-22 04:43:05 -0300733 llc_sap_hold(sap);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700734 llc_sk(sk)->sap = sap;
Octavian Purdila6d2e3ea2009-12-26 11:51:04 +0000735
736 spin_lock_bh(&sap->sk_lock);
Cong Wangaa23c222018-10-11 11:15:13 -0700737 sock_set_flag(sk, SOCK_RCU_FREE);
Octavian Purdila52d58ae2009-12-26 11:51:05 +0000738 sap->sk_count++;
739 sk_nulls_add_node_rcu(sk, laddr_hb);
Octavian Purdila6d2e3ea2009-12-26 11:51:04 +0000740 hlist_add_head(&llc->dev_hash_node, dev_hb);
Octavian Purdilab76f5a82009-12-26 11:51:02 +0000741 spin_unlock_bh(&sap->sk_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700742}
743
744/**
745 * llc_sap_remove_socket - removes a socket from SAP
746 * @sap: SAP
747 * @sk: socket
748 *
Octavian Purdila52d58ae2009-12-26 11:51:05 +0000749 * This function removes a connection from the hash tables of a SAP if
Linus Torvalds1da177e2005-04-16 15:20:36 -0700750 * the connection was in this list.
751 */
752void llc_sap_remove_socket(struct llc_sap *sap, struct sock *sk)
753{
Octavian Purdila6d2e3ea2009-12-26 11:51:04 +0000754 struct llc_sock *llc = llc_sk(sk);
755
Octavian Purdilab76f5a82009-12-26 11:51:02 +0000756 spin_lock_bh(&sap->sk_lock);
757 sk_nulls_del_node_init_rcu(sk);
Octavian Purdila6d2e3ea2009-12-26 11:51:04 +0000758 hlist_del(&llc->dev_hash_node);
Octavian Purdila52d58ae2009-12-26 11:51:05 +0000759 sap->sk_count--;
Octavian Purdilab76f5a82009-12-26 11:51:02 +0000760 spin_unlock_bh(&sap->sk_lock);
Arnaldo Carvalho de Melo6e2144b2005-09-22 04:43:05 -0300761 llc_sap_put(sap);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700762}
763
764/**
765 * llc_conn_rcv - sends received pdus to the connection state machine
766 * @sk: current connection structure.
767 * @skb: received frame.
768 *
769 * Sends received pdus to the connection state machine.
770 */
Weilong Chen3cdba602013-12-20 11:14:59 +0800771static int llc_conn_rcv(struct sock *sk, struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700772{
773 struct llc_conn_state_ev *ev = llc_conn_ev(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700774
Linus Torvalds1da177e2005-04-16 15:20:36 -0700775 ev->type = LLC_CONN_EV_TYPE_PDU;
776 ev->reason = 0;
777 return llc_conn_state_process(sk, skb);
778}
779
Arnaldo Carvalho de Melod3894242005-09-22 07:57:21 -0300780static struct sock *llc_create_incoming_sock(struct sock *sk,
781 struct net_device *dev,
782 struct llc_addr *saddr,
783 struct llc_addr *daddr)
784{
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +0900785 struct sock *newsk = llc_sk_alloc(sock_net(sk), sk->sk_family, GFP_ATOMIC,
Eric W. Biederman11aa9c22015-05-08 21:09:13 -0500786 sk->sk_prot, 0);
Arnaldo Carvalho de Melod3894242005-09-22 07:57:21 -0300787 struct llc_sock *newllc, *llc = llc_sk(sk);
788
789 if (!newsk)
790 goto out;
791 newllc = llc_sk(newsk);
792 memcpy(&newllc->laddr, daddr, sizeof(newllc->laddr));
793 memcpy(&newllc->daddr, saddr, sizeof(newllc->daddr));
794 newllc->dev = dev;
795 dev_hold(dev);
796 llc_sap_add_socket(llc->sap, newsk);
797 llc_sap_hold(llc->sap);
798out:
799 return newsk;
800}
801
Linus Torvalds1da177e2005-04-16 15:20:36 -0700802void llc_conn_handler(struct llc_sap *sap, struct sk_buff *skb)
803{
804 struct llc_addr saddr, daddr;
805 struct sock *sk;
806
807 llc_pdu_decode_sa(skb, saddr.mac);
808 llc_pdu_decode_ssap(skb, &saddr.lsap);
809 llc_pdu_decode_da(skb, daddr.mac);
810 llc_pdu_decode_dsap(skb, &daddr.lsap);
811
Arnaldo Carvalho de Melod3894242005-09-22 07:57:21 -0300812 sk = __llc_lookup(sap, &saddr, &daddr);
813 if (!sk)
814 goto drop;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700815
Linus Torvalds1da177e2005-04-16 15:20:36 -0700816 bh_lock_sock(sk);
Arnaldo Carvalho de Melod3894242005-09-22 07:57:21 -0300817 /*
818 * This has to be done here and not at the upper layer ->accept
819 * method because of the way the PROCOM state machine works:
820 * it needs to set several state variables (see, for instance,
821 * llc_adm_actions_2 in net/llc/llc_c_st.c) and send a packet to
822 * the originator of the new connection, and this state has to be
823 * in the newly created struct sock private area. -acme
824 */
825 if (unlikely(sk->sk_state == TCP_LISTEN)) {
826 struct sock *newsk = llc_create_incoming_sock(sk, skb->dev,
827 &saddr, &daddr);
828 if (!newsk)
829 goto drop_unlock;
830 skb_set_owner_r(skb, newsk);
831 } else {
832 /*
833 * Can't be skb_set_owner_r, this will be done at the
834 * llc_conn_state_process function, later on, when we will use
835 * skb_queue_rcv_skb to send it to upper layers, this is
836 * another trick required to cope with how the PROCOM state
837 * machine works. -acme
838 */
Eric Dumazet42b52782017-02-12 14:03:52 -0800839 skb_orphan(skb);
840 sock_hold(sk);
Arnaldo Carvalho de Melod3894242005-09-22 07:57:21 -0300841 skb->sk = sk;
Eric Dumazet42b52782017-02-12 14:03:52 -0800842 skb->destructor = sock_efree;
Arnaldo Carvalho de Melod3894242005-09-22 07:57:21 -0300843 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700844 if (!sock_owned_by_user(sk))
845 llc_conn_rcv(sk, skb);
846 else {
Harvey Harrison0dc47872008-03-05 20:47:47 -0800847 dprintk("%s: adding to backlog...\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700848 llc_set_backlog_type(skb, LLC_PACKET);
Eric Dumazetf545a382012-04-22 23:34:26 +0000849 if (sk_add_backlog(sk, skb, sk->sk_rcvbuf))
Zhu Yi79545b62010-03-04 18:01:43 +0000850 goto drop_unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700851 }
Arnaldo Carvalho de Melod3894242005-09-22 07:57:21 -0300852out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700853 bh_unlock_sock(sk);
854 sock_put(sk);
855 return;
856drop:
857 kfree_skb(skb);
Arnaldo Carvalho de Melod3894242005-09-22 07:57:21 -0300858 return;
859drop_unlock:
860 kfree_skb(skb);
861 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700862}
863
864#undef LLC_REFCNT_DEBUG
865#ifdef LLC_REFCNT_DEBUG
866static atomic_t llc_sock_nr;
867#endif
868
869/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700870 * llc_backlog_rcv - Processes rx frames and expired timers.
871 * @sk: LLC sock (p8022 connection)
872 * @skb: queued rx frame or event
873 *
874 * This function processes frames that has received and timers that has
875 * expired during sending an I pdu (refer to data_req_handler). frames
876 * queue by llc_rcv function (llc_mac.c) and timers queue by timer
877 * callback functions(llc_c_ac.c).
878 */
879static int llc_backlog_rcv(struct sock *sk, struct sk_buff *skb)
880{
881 int rc = 0;
882 struct llc_sock *llc = llc_sk(sk);
883
Arnaldo Carvalho de Meloaf426d32005-09-22 03:59:22 -0300884 if (likely(llc_backlog_type(skb) == LLC_PACKET)) {
885 if (likely(llc->state > 1)) /* not closed */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700886 rc = llc_conn_rcv(sk, skb);
887 else
888 goto out_kfree_skb;
889 } else if (llc_backlog_type(skb) == LLC_EVENT) {
890 /* timer expiration event */
Arnaldo Carvalho de Meloaf426d32005-09-22 03:59:22 -0300891 if (likely(llc->state > 1)) /* not closed */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700892 rc = llc_conn_state_process(sk, skb);
893 else
894 goto out_kfree_skb;
895 } else {
Harvey Harrison0dc47872008-03-05 20:47:47 -0800896 printk(KERN_ERR "%s: invalid skb in backlog\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700897 goto out_kfree_skb;
898 }
899out:
900 return rc;
901out_kfree_skb:
902 kfree_skb(skb);
903 goto out;
904}
905
906/**
907 * llc_sk_init - Initializes a socket with default llc values.
908 * @sk: socket to initialize.
909 *
910 * Initializes a socket with default llc values.
911 */
Weilong Chen3cdba602013-12-20 11:14:59 +0800912static void llc_sk_init(struct sock *sk)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700913{
914 struct llc_sock *llc = llc_sk(sk);
915
916 llc->state = LLC_CONN_STATE_ADM;
917 llc->inc_cntr = llc->dec_cntr = 2;
918 llc->dec_step = llc->connect_step = 1;
919
Pavel Emelyanovb24b8a22008-01-23 21:20:07 -0800920 setup_timer(&llc->ack_timer.timer, llc_conn_ack_tmr_cb,
921 (unsigned long)sk);
Arnaldo Carvalho de Melo590232a2005-09-22 04:30:44 -0300922 llc->ack_timer.expire = sysctl_llc2_ack_timeout;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700923
Pavel Emelyanovb24b8a22008-01-23 21:20:07 -0800924 setup_timer(&llc->pf_cycle_timer.timer, llc_conn_pf_cycle_tmr_cb,
925 (unsigned long)sk);
Arnaldo Carvalho de Melo590232a2005-09-22 04:30:44 -0300926 llc->pf_cycle_timer.expire = sysctl_llc2_p_timeout;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700927
Pavel Emelyanovb24b8a22008-01-23 21:20:07 -0800928 setup_timer(&llc->rej_sent_timer.timer, llc_conn_rej_tmr_cb,
929 (unsigned long)sk);
Arnaldo Carvalho de Melo590232a2005-09-22 04:30:44 -0300930 llc->rej_sent_timer.expire = sysctl_llc2_rej_timeout;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700931
Pavel Emelyanovb24b8a22008-01-23 21:20:07 -0800932 setup_timer(&llc->busy_state_timer.timer, llc_conn_busy_tmr_cb,
933 (unsigned long)sk);
Arnaldo Carvalho de Melo590232a2005-09-22 04:30:44 -0300934 llc->busy_state_timer.expire = sysctl_llc2_busy_timeout;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700935
936 llc->n2 = 2; /* max retransmit */
937 llc->k = 2; /* tx win size, will adjust dynam */
938 llc->rw = 128; /* rx win size (opt and equal to
YOSHIFUJI Hideakid57b1862007-02-09 23:25:01 +0900939 * tx_win of remote LLC) */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700940 skb_queue_head_init(&llc->pdu_unack_q);
941 sk->sk_backlog_rcv = llc_backlog_rcv;
942}
943
944/**
945 * llc_sk_alloc - Allocates LLC sock
946 * @family: upper layer protocol family
947 * @priority: for allocation (%GFP_KERNEL, %GFP_ATOMIC, etc)
948 *
949 * Allocates a LLC sock and initializes it. Returns the new LLC sock
950 * or %NULL if there's no memory available for one
951 */
Eric W. Biederman11aa9c22015-05-08 21:09:13 -0500952struct sock *llc_sk_alloc(struct net *net, int family, gfp_t priority, struct proto *prot, int kern)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700953{
Eric W. Biederman11aa9c22015-05-08 21:09:13 -0500954 struct sock *sk = sk_alloc(net, family, priority, prot, kern);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700955
956 if (!sk)
957 goto out;
958 llc_sk_init(sk);
959 sock_init_data(NULL, sk);
960#ifdef LLC_REFCNT_DEBUG
961 atomic_inc(&llc_sock_nr);
962 printk(KERN_DEBUG "LLC socket %p created in %s, now we have %d alive\n", sk,
Harvey Harrison0dc47872008-03-05 20:47:47 -0800963 __func__, atomic_read(&llc_sock_nr));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700964#endif
965out:
966 return sk;
967}
968
Cong Wange202fa92018-04-19 12:25:38 -0700969void llc_sk_stop_all_timers(struct sock *sk, bool sync)
970{
971 struct llc_sock *llc = llc_sk(sk);
972
973 if (sync) {
974 del_timer_sync(&llc->pf_cycle_timer.timer);
975 del_timer_sync(&llc->ack_timer.timer);
976 del_timer_sync(&llc->rej_sent_timer.timer);
977 del_timer_sync(&llc->busy_state_timer.timer);
978 } else {
979 del_timer(&llc->pf_cycle_timer.timer);
980 del_timer(&llc->ack_timer.timer);
981 del_timer(&llc->rej_sent_timer.timer);
982 del_timer(&llc->busy_state_timer.timer);
983 }
984
985 llc->ack_must_be_send = 0;
986 llc->ack_pf = 0;
987}
988
Linus Torvalds1da177e2005-04-16 15:20:36 -0700989/**
990 * llc_sk_free - Frees a LLC socket
991 * @sk - socket to free
992 *
993 * Frees a LLC socket
994 */
995void llc_sk_free(struct sock *sk)
996{
997 struct llc_sock *llc = llc_sk(sk);
998
999 llc->state = LLC_CONN_OUT_OF_SVC;
1000 /* Stop all (possibly) running timers */
Cong Wange202fa92018-04-19 12:25:38 -07001001 llc_sk_stop_all_timers(sk, true);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001002#ifdef DEBUG_LLC_CONN_ALLOC
Harvey Harrison0dc47872008-03-05 20:47:47 -08001003 printk(KERN_INFO "%s: unackq=%d, txq=%d\n", __func__,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001004 skb_queue_len(&llc->pdu_unack_q),
1005 skb_queue_len(&sk->sk_write_queue));
1006#endif
1007 skb_queue_purge(&sk->sk_receive_queue);
1008 skb_queue_purge(&sk->sk_write_queue);
1009 skb_queue_purge(&llc->pdu_unack_q);
1010#ifdef LLC_REFCNT_DEBUG
1011 if (atomic_read(&sk->sk_refcnt) != 1) {
1012 printk(KERN_DEBUG "Destruction of LLC sock %p delayed in %s, cnt=%d\n",
Harvey Harrison0dc47872008-03-05 20:47:47 -08001013 sk, __func__, atomic_read(&sk->sk_refcnt));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001014 printk(KERN_DEBUG "%d LLC sockets are still alive\n",
1015 atomic_read(&llc_sock_nr));
1016 } else {
1017 atomic_dec(&llc_sock_nr);
1018 printk(KERN_DEBUG "LLC socket %p released in %s, %d are still alive\n", sk,
Harvey Harrison0dc47872008-03-05 20:47:47 -08001019 __func__, atomic_read(&llc_sock_nr));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001020 }
1021#endif
1022 sock_put(sk);
1023}
1024
1025/**
1026 * llc_sk_reset - resets a connection
1027 * @sk: LLC socket to reset
1028 *
1029 * Resets a connection to the out of service state. Stops its timers
1030 * and frees any frames in the queues of the connection.
1031 */
1032void llc_sk_reset(struct sock *sk)
1033{
1034 struct llc_sock *llc = llc_sk(sk);
1035
1036 llc_conn_ac_stop_all_timers(sk, NULL);
1037 skb_queue_purge(&sk->sk_write_queue);
1038 skb_queue_purge(&llc->pdu_unack_q);
1039 llc->remote_busy_flag = 0;
1040 llc->cause_flag = 0;
1041 llc->retry_count = 0;
1042 llc_conn_set_p_flag(sk, 0);
1043 llc->f_flag = 0;
1044 llc->s_flag = 0;
1045 llc->ack_pf = 0;
1046 llc->first_pdu_Ns = 0;
1047 llc->ack_must_be_send = 0;
1048 llc->dec_step = 1;
1049 llc->inc_cntr = 2;
1050 llc->dec_cntr = 2;
1051 llc->X = 0;
1052 llc->failed_data_req = 0 ;
1053 llc->last_nr = 0;
1054}