11da177e4SLinus Torvalds /* 21da177e4SLinus Torvalds * X.25 Packet Layer release 002 31da177e4SLinus Torvalds * 41da177e4SLinus Torvalds * This is ALPHA test software. This code may break your machine, 51da177e4SLinus Torvalds * randomly fail to work with new releases, misbehave and/or generally 61da177e4SLinus Torvalds * screw up. It might even work. 71da177e4SLinus Torvalds * 81da177e4SLinus Torvalds * This code REQUIRES 2.1.15 or higher 91da177e4SLinus Torvalds * 101da177e4SLinus Torvalds * This module: 111da177e4SLinus Torvalds * This module is free software; you can redistribute it and/or 121da177e4SLinus Torvalds * modify it under the terms of the GNU General Public License 131da177e4SLinus Torvalds * as published by the Free Software Foundation; either version 141da177e4SLinus Torvalds * 2 of the License, or (at your option) any later version. 151da177e4SLinus Torvalds * 161da177e4SLinus Torvalds * History 171da177e4SLinus Torvalds * X.25 001 Jonathan Naylor Started coding. 181da177e4SLinus Torvalds * X.25 002 Jonathan Naylor Centralised disconnection code. 191da177e4SLinus Torvalds * New timer architecture. 201da177e4SLinus Torvalds * 2000-03-20 Daniela Squassoni Disabling/enabling of facilities 211da177e4SLinus Torvalds * negotiation. 221da177e4SLinus Torvalds * 2000-11-10 Henner Eisen Check and reset for out-of-sequence 231da177e4SLinus Torvalds * i-frames. 241da177e4SLinus Torvalds */ 251da177e4SLinus Torvalds 261da177e4SLinus Torvalds #include <linux/errno.h> 271da177e4SLinus Torvalds #include <linux/kernel.h> 281da177e4SLinus Torvalds #include <linux/string.h> 291da177e4SLinus Torvalds #include <linux/skbuff.h> 301da177e4SLinus Torvalds #include <net/sock.h> 31c752f073SArnaldo Carvalho de Melo #include <net/tcp_states.h> 321da177e4SLinus Torvalds #include <net/x25.h> 331da177e4SLinus Torvalds 341da177e4SLinus Torvalds static int x25_queue_rx_frame(struct sock *sk, struct sk_buff *skb, int more) 351da177e4SLinus Torvalds { 361da177e4SLinus Torvalds struct sk_buff *skbo, *skbn = skb; 371da177e4SLinus Torvalds struct x25_sock *x25 = x25_sk(sk); 381da177e4SLinus Torvalds 391da177e4SLinus Torvalds if (more) { 401da177e4SLinus Torvalds x25->fraglen += skb->len; 411da177e4SLinus Torvalds skb_queue_tail(&x25->fragment_queue, skb); 421da177e4SLinus Torvalds skb_set_owner_r(skb, sk); 431da177e4SLinus Torvalds return 0; 441da177e4SLinus Torvalds } 451da177e4SLinus Torvalds 461da177e4SLinus Torvalds if (!more && x25->fraglen > 0) { /* End of fragment */ 471da177e4SLinus Torvalds int len = x25->fraglen + skb->len; 481da177e4SLinus Torvalds 491da177e4SLinus Torvalds if ((skbn = alloc_skb(len, GFP_ATOMIC)) == NULL){ 501da177e4SLinus Torvalds kfree_skb(skb); 511da177e4SLinus Torvalds return 1; 521da177e4SLinus Torvalds } 531da177e4SLinus Torvalds 541da177e4SLinus Torvalds skb_queue_tail(&x25->fragment_queue, skb); 551da177e4SLinus Torvalds 56badff6d0SArnaldo Carvalho de Melo skb_reset_transport_header(skbn); 571da177e4SLinus Torvalds 581da177e4SLinus Torvalds skbo = skb_dequeue(&x25->fragment_queue); 591a4e2d09SArnaldo Carvalho de Melo skb_copy_from_linear_data(skbo, skb_put(skbn, skbo->len), 601a4e2d09SArnaldo Carvalho de Melo skbo->len); 611da177e4SLinus Torvalds kfree_skb(skbo); 621da177e4SLinus Torvalds 631da177e4SLinus Torvalds while ((skbo = 641da177e4SLinus Torvalds skb_dequeue(&x25->fragment_queue)) != NULL) { 651da177e4SLinus Torvalds skb_pull(skbo, (x25->neighbour->extended) ? 661da177e4SLinus Torvalds X25_EXT_MIN_LEN : X25_STD_MIN_LEN); 671a4e2d09SArnaldo Carvalho de Melo skb_copy_from_linear_data(skbo, 681a4e2d09SArnaldo Carvalho de Melo skb_put(skbn, skbo->len), 691a4e2d09SArnaldo Carvalho de Melo skbo->len); 701da177e4SLinus Torvalds kfree_skb(skbo); 711da177e4SLinus Torvalds } 721da177e4SLinus Torvalds 731da177e4SLinus Torvalds x25->fraglen = 0; 741da177e4SLinus Torvalds } 751da177e4SLinus Torvalds 761da177e4SLinus Torvalds skb_set_owner_r(skbn, sk); 771da177e4SLinus Torvalds skb_queue_tail(&sk->sk_receive_queue, skbn); 781da177e4SLinus Torvalds if (!sock_flag(sk, SOCK_DEAD)) 791da177e4SLinus Torvalds sk->sk_data_ready(sk, skbn->len); 801da177e4SLinus Torvalds 811da177e4SLinus Torvalds return 0; 821da177e4SLinus Torvalds } 831da177e4SLinus Torvalds 841da177e4SLinus Torvalds /* 851da177e4SLinus Torvalds * State machine for state 1, Awaiting Call Accepted State. 861da177e4SLinus Torvalds * The handling of the timer(s) is in file x25_timer.c. 871da177e4SLinus Torvalds * Handling of state 0 and connection release is in af_x25.c. 881da177e4SLinus Torvalds */ 891da177e4SLinus Torvalds static int x25_state1_machine(struct sock *sk, struct sk_buff *skb, int frametype) 901da177e4SLinus Torvalds { 911da177e4SLinus Torvalds struct x25_address source_addr, dest_addr; 92*f5eb917bSJohn Hughes int len; 931da177e4SLinus Torvalds 941da177e4SLinus Torvalds switch (frametype) { 951da177e4SLinus Torvalds case X25_CALL_ACCEPTED: { 961da177e4SLinus Torvalds struct x25_sock *x25 = x25_sk(sk); 971da177e4SLinus Torvalds 981da177e4SLinus Torvalds x25_stop_timer(sk); 991da177e4SLinus Torvalds x25->condition = 0x00; 1001da177e4SLinus Torvalds x25->vs = 0; 1011da177e4SLinus Torvalds x25->va = 0; 1021da177e4SLinus Torvalds x25->vr = 0; 1031da177e4SLinus Torvalds x25->vl = 0; 1041da177e4SLinus Torvalds x25->state = X25_STATE_3; 1051da177e4SLinus Torvalds sk->sk_state = TCP_ESTABLISHED; 1061da177e4SLinus Torvalds /* 1071da177e4SLinus Torvalds * Parse the data in the frame. 1081da177e4SLinus Torvalds */ 1091da177e4SLinus Torvalds skb_pull(skb, X25_STD_MIN_LEN); 110*f5eb917bSJohn Hughes 111*f5eb917bSJohn Hughes len = x25_parse_address_block(skb, &source_addr, 112*f5eb917bSJohn Hughes &dest_addr); 113*f5eb917bSJohn Hughes if (len > 0) 114*f5eb917bSJohn Hughes skb_pull(skb, len); 115*f5eb917bSJohn Hughes 116*f5eb917bSJohn Hughes len = x25_parse_facilities(skb, &x25->facilities, 117a64b7b93SShaun Pereira &x25->dte_facilities, 118*f5eb917bSJohn Hughes &x25->vc_facil_mask); 119*f5eb917bSJohn Hughes if (len > 0) 120*f5eb917bSJohn Hughes skb_pull(skb, len); 1211da177e4SLinus Torvalds /* 1221da177e4SLinus Torvalds * Copy any Call User Data. 1231da177e4SLinus Torvalds */ 124091bb8abSroel kluin if (skb->len > 0) { 125d626f62bSArnaldo Carvalho de Melo skb_copy_from_linear_data(skb, 126d626f62bSArnaldo Carvalho de Melo x25->calluserdata.cuddata, 1271da177e4SLinus Torvalds skb->len); 1281da177e4SLinus Torvalds x25->calluserdata.cudlength = skb->len; 1291da177e4SLinus Torvalds } 1301da177e4SLinus Torvalds if (!sock_flag(sk, SOCK_DEAD)) 1311da177e4SLinus Torvalds sk->sk_state_change(sk); 1321da177e4SLinus Torvalds break; 1331da177e4SLinus Torvalds } 1341da177e4SLinus Torvalds case X25_CLEAR_REQUEST: 1351da177e4SLinus Torvalds x25_write_internal(sk, X25_CLEAR_CONFIRMATION); 1361da177e4SLinus Torvalds x25_disconnect(sk, ECONNREFUSED, skb->data[3], skb->data[4]); 1371da177e4SLinus Torvalds break; 1381da177e4SLinus Torvalds 1391da177e4SLinus Torvalds default: 1401da177e4SLinus Torvalds break; 1411da177e4SLinus Torvalds } 1421da177e4SLinus Torvalds 1431da177e4SLinus Torvalds return 0; 1441da177e4SLinus Torvalds } 1451da177e4SLinus Torvalds 1461da177e4SLinus Torvalds /* 1471da177e4SLinus Torvalds * State machine for state 2, Awaiting Clear Confirmation State. 1481da177e4SLinus Torvalds * The handling of the timer(s) is in file x25_timer.c 1491da177e4SLinus Torvalds * Handling of state 0 and connection release is in af_x25.c. 1501da177e4SLinus Torvalds */ 1511da177e4SLinus Torvalds static int x25_state2_machine(struct sock *sk, struct sk_buff *skb, int frametype) 1521da177e4SLinus Torvalds { 1531da177e4SLinus Torvalds switch (frametype) { 1541da177e4SLinus Torvalds 1551da177e4SLinus Torvalds case X25_CLEAR_REQUEST: 1561da177e4SLinus Torvalds x25_write_internal(sk, X25_CLEAR_CONFIRMATION); 1571da177e4SLinus Torvalds x25_disconnect(sk, 0, skb->data[3], skb->data[4]); 1581da177e4SLinus Torvalds break; 1591da177e4SLinus Torvalds 1601da177e4SLinus Torvalds case X25_CLEAR_CONFIRMATION: 1611da177e4SLinus Torvalds x25_disconnect(sk, 0, 0, 0); 1621da177e4SLinus Torvalds break; 1631da177e4SLinus Torvalds 1641da177e4SLinus Torvalds default: 1651da177e4SLinus Torvalds break; 1661da177e4SLinus Torvalds } 1671da177e4SLinus Torvalds 1681da177e4SLinus Torvalds return 0; 1691da177e4SLinus Torvalds } 1701da177e4SLinus Torvalds 1711da177e4SLinus Torvalds /* 1721da177e4SLinus Torvalds * State machine for state 3, Connected State. 1731da177e4SLinus Torvalds * The handling of the timer(s) is in file x25_timer.c 1741da177e4SLinus Torvalds * Handling of state 0 and connection release is in af_x25.c. 1751da177e4SLinus Torvalds */ 1761da177e4SLinus Torvalds static int x25_state3_machine(struct sock *sk, struct sk_buff *skb, int frametype, int ns, int nr, int q, int d, int m) 1771da177e4SLinus Torvalds { 1781da177e4SLinus Torvalds int queued = 0; 1791da177e4SLinus Torvalds int modulus; 1801da177e4SLinus Torvalds struct x25_sock *x25 = x25_sk(sk); 1811da177e4SLinus Torvalds 1821da177e4SLinus Torvalds modulus = (x25->neighbour->extended) ? X25_EMODULUS : X25_SMODULUS; 1831da177e4SLinus Torvalds 1841da177e4SLinus Torvalds switch (frametype) { 1851da177e4SLinus Torvalds 1861da177e4SLinus Torvalds case X25_RESET_REQUEST: 1871da177e4SLinus Torvalds x25_write_internal(sk, X25_RESET_CONFIRMATION); 1881da177e4SLinus Torvalds x25_stop_timer(sk); 1891da177e4SLinus Torvalds x25->condition = 0x00; 1901da177e4SLinus Torvalds x25->vs = 0; 1911da177e4SLinus Torvalds x25->vr = 0; 1921da177e4SLinus Torvalds x25->va = 0; 1931da177e4SLinus Torvalds x25->vl = 0; 1941da177e4SLinus Torvalds x25_requeue_frames(sk); 1951da177e4SLinus Torvalds break; 1961da177e4SLinus Torvalds 1971da177e4SLinus Torvalds case X25_CLEAR_REQUEST: 1981da177e4SLinus Torvalds x25_write_internal(sk, X25_CLEAR_CONFIRMATION); 1991da177e4SLinus Torvalds x25_disconnect(sk, 0, skb->data[3], skb->data[4]); 2001da177e4SLinus Torvalds break; 2011da177e4SLinus Torvalds 2021da177e4SLinus Torvalds case X25_RR: 2031da177e4SLinus Torvalds case X25_RNR: 2041da177e4SLinus Torvalds if (!x25_validate_nr(sk, nr)) { 2051da177e4SLinus Torvalds x25_clear_queues(sk); 2061da177e4SLinus Torvalds x25_write_internal(sk, X25_RESET_REQUEST); 2071da177e4SLinus Torvalds x25_start_t22timer(sk); 2081da177e4SLinus Torvalds x25->condition = 0x00; 2091da177e4SLinus Torvalds x25->vs = 0; 2101da177e4SLinus Torvalds x25->vr = 0; 2111da177e4SLinus Torvalds x25->va = 0; 2121da177e4SLinus Torvalds x25->vl = 0; 2131da177e4SLinus Torvalds x25->state = X25_STATE_4; 2141da177e4SLinus Torvalds } else { 2151da177e4SLinus Torvalds x25_frames_acked(sk, nr); 2161da177e4SLinus Torvalds if (frametype == X25_RNR) { 2171da177e4SLinus Torvalds x25->condition |= X25_COND_PEER_RX_BUSY; 2181da177e4SLinus Torvalds } else { 2191da177e4SLinus Torvalds x25->condition &= ~X25_COND_PEER_RX_BUSY; 2201da177e4SLinus Torvalds } 2211da177e4SLinus Torvalds } 2221da177e4SLinus Torvalds break; 2231da177e4SLinus Torvalds 2241da177e4SLinus Torvalds case X25_DATA: /* XXX */ 2251da177e4SLinus Torvalds x25->condition &= ~X25_COND_PEER_RX_BUSY; 2261da177e4SLinus Torvalds if ((ns != x25->vr) || !x25_validate_nr(sk, nr)) { 2271da177e4SLinus Torvalds x25_clear_queues(sk); 2281da177e4SLinus Torvalds x25_write_internal(sk, X25_RESET_REQUEST); 2291da177e4SLinus Torvalds x25_start_t22timer(sk); 2301da177e4SLinus Torvalds x25->condition = 0x00; 2311da177e4SLinus Torvalds x25->vs = 0; 2321da177e4SLinus Torvalds x25->vr = 0; 2331da177e4SLinus Torvalds x25->va = 0; 2341da177e4SLinus Torvalds x25->vl = 0; 2351da177e4SLinus Torvalds x25->state = X25_STATE_4; 2361da177e4SLinus Torvalds break; 2371da177e4SLinus Torvalds } 2381da177e4SLinus Torvalds x25_frames_acked(sk, nr); 2391da177e4SLinus Torvalds if (ns == x25->vr) { 2401da177e4SLinus Torvalds if (x25_queue_rx_frame(sk, skb, m) == 0) { 2411da177e4SLinus Torvalds x25->vr = (x25->vr + 1) % modulus; 2421da177e4SLinus Torvalds queued = 1; 2431da177e4SLinus Torvalds } else { 2441da177e4SLinus Torvalds /* Should never happen */ 2451da177e4SLinus Torvalds x25_clear_queues(sk); 2461da177e4SLinus Torvalds x25_write_internal(sk, X25_RESET_REQUEST); 2471da177e4SLinus Torvalds x25_start_t22timer(sk); 2481da177e4SLinus Torvalds x25->condition = 0x00; 2491da177e4SLinus Torvalds x25->vs = 0; 2501da177e4SLinus Torvalds x25->vr = 0; 2511da177e4SLinus Torvalds x25->va = 0; 2521da177e4SLinus Torvalds x25->vl = 0; 2531da177e4SLinus Torvalds x25->state = X25_STATE_4; 2541da177e4SLinus Torvalds break; 2551da177e4SLinus Torvalds } 2561da177e4SLinus Torvalds if (atomic_read(&sk->sk_rmem_alloc) > 2576bf1574eSEric Dumazet (sk->sk_rcvbuf >> 1)) 2581da177e4SLinus Torvalds x25->condition |= X25_COND_OWN_RX_BUSY; 2591da177e4SLinus Torvalds } 2601da177e4SLinus Torvalds /* 2611da177e4SLinus Torvalds * If the window is full Ack it immediately, else 2621da177e4SLinus Torvalds * start the holdback timer. 2631da177e4SLinus Torvalds */ 2641da177e4SLinus Torvalds if (((x25->vl + x25->facilities.winsize_in) % modulus) == x25->vr) { 2651da177e4SLinus Torvalds x25->condition &= ~X25_COND_ACK_PENDING; 2661da177e4SLinus Torvalds x25_stop_timer(sk); 2671da177e4SLinus Torvalds x25_enquiry_response(sk); 2681da177e4SLinus Torvalds } else { 2691da177e4SLinus Torvalds x25->condition |= X25_COND_ACK_PENDING; 2701da177e4SLinus Torvalds x25_start_t2timer(sk); 2711da177e4SLinus Torvalds } 2721da177e4SLinus Torvalds break; 2731da177e4SLinus Torvalds 2741da177e4SLinus Torvalds case X25_INTERRUPT_CONFIRMATION: 2751da177e4SLinus Torvalds x25->intflag = 0; 2761da177e4SLinus Torvalds break; 2771da177e4SLinus Torvalds 2781da177e4SLinus Torvalds case X25_INTERRUPT: 2791da177e4SLinus Torvalds if (sock_flag(sk, SOCK_URGINLINE)) 2801da177e4SLinus Torvalds queued = !sock_queue_rcv_skb(sk, skb); 2811da177e4SLinus Torvalds else { 2821da177e4SLinus Torvalds skb_set_owner_r(skb, sk); 2831da177e4SLinus Torvalds skb_queue_tail(&x25->interrupt_in_queue, skb); 2841da177e4SLinus Torvalds queued = 1; 2851da177e4SLinus Torvalds } 2861da177e4SLinus Torvalds sk_send_sigurg(sk); 2871da177e4SLinus Torvalds x25_write_internal(sk, X25_INTERRUPT_CONFIRMATION); 2881da177e4SLinus Torvalds break; 2891da177e4SLinus Torvalds 2901da177e4SLinus Torvalds default: 2911da177e4SLinus Torvalds printk(KERN_WARNING "x25: unknown %02X in state 3\n", frametype); 2921da177e4SLinus Torvalds break; 2931da177e4SLinus Torvalds } 2941da177e4SLinus Torvalds 2951da177e4SLinus Torvalds return queued; 2961da177e4SLinus Torvalds } 2971da177e4SLinus Torvalds 2981da177e4SLinus Torvalds /* 2991da177e4SLinus Torvalds * State machine for state 4, Awaiting Reset Confirmation State. 3001da177e4SLinus Torvalds * The handling of the timer(s) is in file x25_timer.c 3011da177e4SLinus Torvalds * Handling of state 0 and connection release is in af_x25.c. 3021da177e4SLinus Torvalds */ 3031da177e4SLinus Torvalds static int x25_state4_machine(struct sock *sk, struct sk_buff *skb, int frametype) 3041da177e4SLinus Torvalds { 3051da177e4SLinus Torvalds switch (frametype) { 3061da177e4SLinus Torvalds 3071da177e4SLinus Torvalds case X25_RESET_REQUEST: 3081da177e4SLinus Torvalds x25_write_internal(sk, X25_RESET_CONFIRMATION); 3091da177e4SLinus Torvalds case X25_RESET_CONFIRMATION: { 3101da177e4SLinus Torvalds struct x25_sock *x25 = x25_sk(sk); 3111da177e4SLinus Torvalds 3121da177e4SLinus Torvalds x25_stop_timer(sk); 3131da177e4SLinus Torvalds x25->condition = 0x00; 3141da177e4SLinus Torvalds x25->va = 0; 3151da177e4SLinus Torvalds x25->vr = 0; 3161da177e4SLinus Torvalds x25->vs = 0; 3171da177e4SLinus Torvalds x25->vl = 0; 3181da177e4SLinus Torvalds x25->state = X25_STATE_3; 3191da177e4SLinus Torvalds x25_requeue_frames(sk); 3201da177e4SLinus Torvalds break; 3211da177e4SLinus Torvalds } 3221da177e4SLinus Torvalds case X25_CLEAR_REQUEST: 3231da177e4SLinus Torvalds x25_write_internal(sk, X25_CLEAR_CONFIRMATION); 3241da177e4SLinus Torvalds x25_disconnect(sk, 0, skb->data[3], skb->data[4]); 3251da177e4SLinus Torvalds break; 3261da177e4SLinus Torvalds 3271da177e4SLinus Torvalds default: 3281da177e4SLinus Torvalds break; 3291da177e4SLinus Torvalds } 3301da177e4SLinus Torvalds 3311da177e4SLinus Torvalds return 0; 3321da177e4SLinus Torvalds } 3331da177e4SLinus Torvalds 3341da177e4SLinus Torvalds /* Higher level upcall for a LAPB frame */ 3351da177e4SLinus Torvalds int x25_process_rx_frame(struct sock *sk, struct sk_buff *skb) 3361da177e4SLinus Torvalds { 3371da177e4SLinus Torvalds struct x25_sock *x25 = x25_sk(sk); 3381da177e4SLinus Torvalds int queued = 0, frametype, ns, nr, q, d, m; 3391da177e4SLinus Torvalds 3401da177e4SLinus Torvalds if (x25->state == X25_STATE_0) 3411da177e4SLinus Torvalds return 0; 3421da177e4SLinus Torvalds 3431da177e4SLinus Torvalds frametype = x25_decode(sk, skb, &ns, &nr, &q, &d, &m); 3441da177e4SLinus Torvalds 3451da177e4SLinus Torvalds switch (x25->state) { 3461da177e4SLinus Torvalds case X25_STATE_1: 3471da177e4SLinus Torvalds queued = x25_state1_machine(sk, skb, frametype); 3481da177e4SLinus Torvalds break; 3491da177e4SLinus Torvalds case X25_STATE_2: 3501da177e4SLinus Torvalds queued = x25_state2_machine(sk, skb, frametype); 3511da177e4SLinus Torvalds break; 3521da177e4SLinus Torvalds case X25_STATE_3: 3531da177e4SLinus Torvalds queued = x25_state3_machine(sk, skb, frametype, ns, nr, q, d, m); 3541da177e4SLinus Torvalds break; 3551da177e4SLinus Torvalds case X25_STATE_4: 3561da177e4SLinus Torvalds queued = x25_state4_machine(sk, skb, frametype); 3571da177e4SLinus Torvalds break; 3581da177e4SLinus Torvalds } 3591da177e4SLinus Torvalds 3601da177e4SLinus Torvalds x25_kick(sk); 3611da177e4SLinus Torvalds 3621da177e4SLinus Torvalds return queued; 3631da177e4SLinus Torvalds } 3641da177e4SLinus Torvalds 3651da177e4SLinus Torvalds int x25_backlog_rcv(struct sock *sk, struct sk_buff *skb) 3661da177e4SLinus Torvalds { 3671da177e4SLinus Torvalds int queued = x25_process_rx_frame(sk, skb); 3681da177e4SLinus Torvalds 3691da177e4SLinus Torvalds if (!queued) 3701da177e4SLinus Torvalds kfree_skb(skb); 3711da177e4SLinus Torvalds 3721da177e4SLinus Torvalds return 0; 3731da177e4SLinus Torvalds } 374