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