1ee5d8f4dSThomas Gleixner // SPDX-License-Identifier: GPL-2.0-or-later
21da177e4SLinus Torvalds /*
31da177e4SLinus Torvalds * X.25 Packet Layer release 002
41da177e4SLinus Torvalds *
51da177e4SLinus Torvalds * This is ALPHA test software. This code may break your machine,
61da177e4SLinus Torvalds * randomly fail to work with new releases, misbehave and/or generally
71da177e4SLinus Torvalds * screw up. It might even work.
81da177e4SLinus Torvalds *
91da177e4SLinus Torvalds * This code REQUIRES 2.1.15 or higher
101da177e4SLinus Torvalds *
111da177e4SLinus Torvalds * History
121da177e4SLinus Torvalds * X.25 001 Jonathan Naylor Started coding.
131da177e4SLinus Torvalds * X.25 002 Jonathan Naylor Centralised disconnection code.
141da177e4SLinus Torvalds * New timer architecture.
151da177e4SLinus Torvalds * 2000-03-20 Daniela Squassoni Disabling/enabling of facilities
161da177e4SLinus Torvalds * negotiation.
171da177e4SLinus Torvalds * 2000-11-10 Henner Eisen Check and reset for out-of-sequence
181da177e4SLinus Torvalds * i-frames.
191da177e4SLinus Torvalds */
201da177e4SLinus Torvalds
21b73e9e3cSwangweidong #define pr_fmt(fmt) "X25: " fmt
22b73e9e3cSwangweidong
235a0e3ad6STejun Heo #include <linux/slab.h>
241da177e4SLinus Torvalds #include <linux/errno.h>
251da177e4SLinus Torvalds #include <linux/kernel.h>
261da177e4SLinus Torvalds #include <linux/string.h>
271da177e4SLinus Torvalds #include <linux/skbuff.h>
281da177e4SLinus Torvalds #include <net/sock.h>
29c752f073SArnaldo Carvalho de Melo #include <net/tcp_states.h>
301da177e4SLinus Torvalds #include <net/x25.h>
311da177e4SLinus Torvalds
x25_queue_rx_frame(struct sock * sk,struct sk_buff * skb,int more)321da177e4SLinus Torvalds static int x25_queue_rx_frame(struct sock *sk, struct sk_buff *skb, int more)
331da177e4SLinus Torvalds {
341da177e4SLinus Torvalds struct sk_buff *skbo, *skbn = skb;
351da177e4SLinus Torvalds struct x25_sock *x25 = x25_sk(sk);
361da177e4SLinus Torvalds
371da177e4SLinus Torvalds if (more) {
381da177e4SLinus Torvalds x25->fraglen += skb->len;
391da177e4SLinus Torvalds skb_queue_tail(&x25->fragment_queue, skb);
401da177e4SLinus Torvalds skb_set_owner_r(skb, sk);
411da177e4SLinus Torvalds return 0;
421da177e4SLinus Torvalds }
431da177e4SLinus Torvalds
44*9745177cSJean Sacren if (x25->fraglen > 0) { /* End of fragment */
451da177e4SLinus Torvalds int len = x25->fraglen + skb->len;
461da177e4SLinus Torvalds
471da177e4SLinus Torvalds if ((skbn = alloc_skb(len, GFP_ATOMIC)) == NULL){
481da177e4SLinus Torvalds kfree_skb(skb);
491da177e4SLinus Torvalds return 1;
501da177e4SLinus Torvalds }
511da177e4SLinus Torvalds
521da177e4SLinus Torvalds skb_queue_tail(&x25->fragment_queue, skb);
531da177e4SLinus Torvalds
54badff6d0SArnaldo Carvalho de Melo skb_reset_transport_header(skbn);
551da177e4SLinus Torvalds
561da177e4SLinus Torvalds skbo = skb_dequeue(&x25->fragment_queue);
571a4e2d09SArnaldo Carvalho de Melo skb_copy_from_linear_data(skbo, skb_put(skbn, skbo->len),
581a4e2d09SArnaldo Carvalho de Melo skbo->len);
591da177e4SLinus Torvalds kfree_skb(skbo);
601da177e4SLinus Torvalds
611da177e4SLinus Torvalds while ((skbo =
621da177e4SLinus Torvalds skb_dequeue(&x25->fragment_queue)) != NULL) {
631da177e4SLinus Torvalds skb_pull(skbo, (x25->neighbour->extended) ?
641da177e4SLinus Torvalds X25_EXT_MIN_LEN : X25_STD_MIN_LEN);
651a4e2d09SArnaldo Carvalho de Melo skb_copy_from_linear_data(skbo,
661a4e2d09SArnaldo Carvalho de Melo skb_put(skbn, skbo->len),
671a4e2d09SArnaldo Carvalho de Melo skbo->len);
681da177e4SLinus Torvalds kfree_skb(skbo);
691da177e4SLinus Torvalds }
701da177e4SLinus Torvalds
711da177e4SLinus Torvalds x25->fraglen = 0;
721da177e4SLinus Torvalds }
731da177e4SLinus Torvalds
741da177e4SLinus Torvalds skb_set_owner_r(skbn, sk);
751da177e4SLinus Torvalds skb_queue_tail(&sk->sk_receive_queue, skbn);
761da177e4SLinus Torvalds if (!sock_flag(sk, SOCK_DEAD))
77676d2369SDavid S. Miller sk->sk_data_ready(sk);
781da177e4SLinus Torvalds
791da177e4SLinus Torvalds return 0;
801da177e4SLinus Torvalds }
811da177e4SLinus Torvalds
821da177e4SLinus Torvalds /*
831da177e4SLinus Torvalds * State machine for state 1, Awaiting Call Accepted State.
841da177e4SLinus Torvalds * The handling of the timer(s) is in file x25_timer.c.
851da177e4SLinus Torvalds * Handling of state 0 and connection release is in af_x25.c.
861da177e4SLinus Torvalds */
x25_state1_machine(struct sock * sk,struct sk_buff * skb,int frametype)871da177e4SLinus Torvalds static int x25_state1_machine(struct sock *sk, struct sk_buff *skb, int frametype)
881da177e4SLinus Torvalds {
891da177e4SLinus Torvalds struct x25_address source_addr, dest_addr;
90f5eb917bSJohn Hughes int len;
9195c30430Sandrew hendry struct x25_sock *x25 = x25_sk(sk);
921da177e4SLinus Torvalds
931da177e4SLinus Torvalds switch (frametype) {
941da177e4SLinus Torvalds case X25_CALL_ACCEPTED: {
951da177e4SLinus Torvalds
961da177e4SLinus Torvalds x25_stop_timer(sk);
971da177e4SLinus Torvalds x25->condition = 0x00;
981da177e4SLinus Torvalds x25->vs = 0;
991da177e4SLinus Torvalds x25->va = 0;
1001da177e4SLinus Torvalds x25->vr = 0;
1011da177e4SLinus Torvalds x25->vl = 0;
1021da177e4SLinus Torvalds x25->state = X25_STATE_3;
1031da177e4SLinus Torvalds sk->sk_state = TCP_ESTABLISHED;
1041da177e4SLinus Torvalds /*
1051da177e4SLinus Torvalds * Parse the data in the frame.
1061da177e4SLinus Torvalds */
107cb101ed2SMatthew Daley if (!pskb_may_pull(skb, X25_STD_MIN_LEN))
108cb101ed2SMatthew Daley goto out_clear;
1091da177e4SLinus Torvalds skb_pull(skb, X25_STD_MIN_LEN);
110f5eb917bSJohn Hughes
111f5eb917bSJohn Hughes len = x25_parse_address_block(skb, &source_addr,
112f5eb917bSJohn Hughes &dest_addr);
113f5eb917bSJohn Hughes if (len > 0)
114f5eb917bSJohn Hughes skb_pull(skb, len);
11595c30430Sandrew hendry else if (len < 0)
11695c30430Sandrew hendry goto out_clear;
117f5eb917bSJohn Hughes
118f5eb917bSJohn Hughes len = x25_parse_facilities(skb, &x25->facilities,
119a64b7b93SShaun Pereira &x25->dte_facilities,
120f5eb917bSJohn Hughes &x25->vc_facil_mask);
121f5eb917bSJohn Hughes if (len > 0)
122f5eb917bSJohn Hughes skb_pull(skb, len);
12395c30430Sandrew hendry else if (len < 0)
12495c30430Sandrew hendry goto out_clear;
1251da177e4SLinus Torvalds /*
1261da177e4SLinus Torvalds * Copy any Call User Data.
1271da177e4SLinus Torvalds */
128091bb8abSroel kluin if (skb->len > 0) {
129c7fd0d48SMatthew Daley if (skb->len > X25_MAX_CUD_LEN)
130c7fd0d48SMatthew Daley goto out_clear;
131c7fd0d48SMatthew Daley
132cb101ed2SMatthew Daley skb_copy_bits(skb, 0, x25->calluserdata.cuddata,
1331da177e4SLinus Torvalds skb->len);
1341da177e4SLinus Torvalds x25->calluserdata.cudlength = skb->len;
1351da177e4SLinus Torvalds }
1361da177e4SLinus Torvalds if (!sock_flag(sk, SOCK_DEAD))
1371da177e4SLinus Torvalds sk->sk_state_change(sk);
1381da177e4SLinus Torvalds break;
1391da177e4SLinus Torvalds }
140b020fcf6SMartin Schiller case X25_CALL_REQUEST:
141b020fcf6SMartin Schiller /* call collision */
142b020fcf6SMartin Schiller x25->causediag.cause = 0x01;
143b020fcf6SMartin Schiller x25->causediag.diagnostic = 0x48;
144b020fcf6SMartin Schiller
145b020fcf6SMartin Schiller x25_write_internal(sk, X25_CLEAR_REQUEST);
146b020fcf6SMartin Schiller x25_disconnect(sk, EISCONN, 0x01, 0x48);
147b020fcf6SMartin Schiller break;
148b020fcf6SMartin Schiller
1491da177e4SLinus Torvalds case X25_CLEAR_REQUEST:
150cb101ed2SMatthew Daley if (!pskb_may_pull(skb, X25_STD_MIN_LEN + 2))
151cb101ed2SMatthew Daley goto out_clear;
152cb101ed2SMatthew Daley
1531da177e4SLinus Torvalds x25_write_internal(sk, X25_CLEAR_CONFIRMATION);
1541da177e4SLinus Torvalds x25_disconnect(sk, ECONNREFUSED, skb->data[3], skb->data[4]);
1551da177e4SLinus Torvalds break;
1561da177e4SLinus Torvalds
1571da177e4SLinus Torvalds default:
1581da177e4SLinus Torvalds break;
1591da177e4SLinus Torvalds }
1601da177e4SLinus Torvalds
1611da177e4SLinus Torvalds return 0;
16295c30430Sandrew hendry
16395c30430Sandrew hendry out_clear:
16495c30430Sandrew hendry x25_write_internal(sk, X25_CLEAR_REQUEST);
16595c30430Sandrew hendry x25->state = X25_STATE_2;
16695c30430Sandrew hendry x25_start_t23timer(sk);
16795c30430Sandrew hendry return 0;
1681da177e4SLinus Torvalds }
1691da177e4SLinus Torvalds
1701da177e4SLinus Torvalds /*
1711da177e4SLinus Torvalds * State machine for state 2, Awaiting Clear Confirmation State.
1721da177e4SLinus Torvalds * The handling of the timer(s) is in file x25_timer.c
1731da177e4SLinus Torvalds * Handling of state 0 and connection release is in af_x25.c.
1741da177e4SLinus Torvalds */
x25_state2_machine(struct sock * sk,struct sk_buff * skb,int frametype)1751da177e4SLinus Torvalds static int x25_state2_machine(struct sock *sk, struct sk_buff *skb, int frametype)
1761da177e4SLinus Torvalds {
1771da177e4SLinus Torvalds switch (frametype) {
1781da177e4SLinus Torvalds
1791da177e4SLinus Torvalds case X25_CLEAR_REQUEST:
180cb101ed2SMatthew Daley if (!pskb_may_pull(skb, X25_STD_MIN_LEN + 2))
181cb101ed2SMatthew Daley goto out_clear;
182cb101ed2SMatthew Daley
1831da177e4SLinus Torvalds x25_write_internal(sk, X25_CLEAR_CONFIRMATION);
1841da177e4SLinus Torvalds x25_disconnect(sk, 0, skb->data[3], skb->data[4]);
1851da177e4SLinus Torvalds break;
1861da177e4SLinus Torvalds
1871da177e4SLinus Torvalds case X25_CLEAR_CONFIRMATION:
1881da177e4SLinus Torvalds x25_disconnect(sk, 0, 0, 0);
1891da177e4SLinus Torvalds break;
1901da177e4SLinus Torvalds
1911da177e4SLinus Torvalds default:
1921da177e4SLinus Torvalds break;
1931da177e4SLinus Torvalds }
1941da177e4SLinus Torvalds
1951da177e4SLinus Torvalds return 0;
196cb101ed2SMatthew Daley
197cb101ed2SMatthew Daley out_clear:
198cb101ed2SMatthew Daley x25_write_internal(sk, X25_CLEAR_REQUEST);
199cb101ed2SMatthew Daley x25_start_t23timer(sk);
200cb101ed2SMatthew Daley return 0;
2011da177e4SLinus Torvalds }
2021da177e4SLinus Torvalds
2031da177e4SLinus Torvalds /*
2041da177e4SLinus Torvalds * State machine for state 3, Connected State.
2051da177e4SLinus Torvalds * The handling of the timer(s) is in file x25_timer.c
2061da177e4SLinus Torvalds * Handling of state 0 and connection release is in af_x25.c.
2071da177e4SLinus Torvalds */
x25_state3_machine(struct sock * sk,struct sk_buff * skb,int frametype,int ns,int nr,int q,int d,int m)2081da177e4SLinus 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)
2091da177e4SLinus Torvalds {
2101da177e4SLinus Torvalds int queued = 0;
2111da177e4SLinus Torvalds int modulus;
2121da177e4SLinus Torvalds struct x25_sock *x25 = x25_sk(sk);
2131da177e4SLinus Torvalds
2141da177e4SLinus Torvalds modulus = (x25->neighbour->extended) ? X25_EMODULUS : X25_SMODULUS;
2151da177e4SLinus Torvalds
2161da177e4SLinus Torvalds switch (frametype) {
2171da177e4SLinus Torvalds
2181da177e4SLinus Torvalds case X25_RESET_REQUEST:
2191da177e4SLinus Torvalds x25_write_internal(sk, X25_RESET_CONFIRMATION);
2201da177e4SLinus Torvalds x25_stop_timer(sk);
2211da177e4SLinus Torvalds x25->condition = 0x00;
2221da177e4SLinus Torvalds x25->vs = 0;
2231da177e4SLinus Torvalds x25->vr = 0;
2241da177e4SLinus Torvalds x25->va = 0;
2251da177e4SLinus Torvalds x25->vl = 0;
2261da177e4SLinus Torvalds x25_requeue_frames(sk);
2271da177e4SLinus Torvalds break;
2281da177e4SLinus Torvalds
2291da177e4SLinus Torvalds case X25_CLEAR_REQUEST:
230cb101ed2SMatthew Daley if (!pskb_may_pull(skb, X25_STD_MIN_LEN + 2))
231cb101ed2SMatthew Daley goto out_clear;
232cb101ed2SMatthew Daley
2331da177e4SLinus Torvalds x25_write_internal(sk, X25_CLEAR_CONFIRMATION);
2341da177e4SLinus Torvalds x25_disconnect(sk, 0, skb->data[3], skb->data[4]);
2351da177e4SLinus Torvalds break;
2361da177e4SLinus Torvalds
2371da177e4SLinus Torvalds case X25_RR:
2381da177e4SLinus Torvalds case X25_RNR:
2391da177e4SLinus Torvalds if (!x25_validate_nr(sk, nr)) {
2401da177e4SLinus Torvalds x25_clear_queues(sk);
2411da177e4SLinus Torvalds x25_write_internal(sk, X25_RESET_REQUEST);
2421da177e4SLinus Torvalds x25_start_t22timer(sk);
2431da177e4SLinus Torvalds x25->condition = 0x00;
2441da177e4SLinus Torvalds x25->vs = 0;
2451da177e4SLinus Torvalds x25->vr = 0;
2461da177e4SLinus Torvalds x25->va = 0;
2471da177e4SLinus Torvalds x25->vl = 0;
2481da177e4SLinus Torvalds x25->state = X25_STATE_4;
2491da177e4SLinus Torvalds } else {
2501da177e4SLinus Torvalds x25_frames_acked(sk, nr);
2511da177e4SLinus Torvalds if (frametype == X25_RNR) {
2521da177e4SLinus Torvalds x25->condition |= X25_COND_PEER_RX_BUSY;
2531da177e4SLinus Torvalds } else {
2541da177e4SLinus Torvalds x25->condition &= ~X25_COND_PEER_RX_BUSY;
2551da177e4SLinus Torvalds }
2561da177e4SLinus Torvalds }
2571da177e4SLinus Torvalds break;
2581da177e4SLinus Torvalds
2591da177e4SLinus Torvalds case X25_DATA: /* XXX */
2601da177e4SLinus Torvalds x25->condition &= ~X25_COND_PEER_RX_BUSY;
2611da177e4SLinus Torvalds if ((ns != x25->vr) || !x25_validate_nr(sk, nr)) {
2621da177e4SLinus Torvalds x25_clear_queues(sk);
2631da177e4SLinus Torvalds x25_write_internal(sk, X25_RESET_REQUEST);
2641da177e4SLinus Torvalds x25_start_t22timer(sk);
2651da177e4SLinus Torvalds x25->condition = 0x00;
2661da177e4SLinus Torvalds x25->vs = 0;
2671da177e4SLinus Torvalds x25->vr = 0;
2681da177e4SLinus Torvalds x25->va = 0;
2691da177e4SLinus Torvalds x25->vl = 0;
2701da177e4SLinus Torvalds x25->state = X25_STATE_4;
2711da177e4SLinus Torvalds break;
2721da177e4SLinus Torvalds }
2731da177e4SLinus Torvalds x25_frames_acked(sk, nr);
2741da177e4SLinus Torvalds if (ns == x25->vr) {
2751da177e4SLinus Torvalds if (x25_queue_rx_frame(sk, skb, m) == 0) {
2761da177e4SLinus Torvalds x25->vr = (x25->vr + 1) % modulus;
2771da177e4SLinus Torvalds queued = 1;
2781da177e4SLinus Torvalds } else {
2791da177e4SLinus Torvalds /* Should never happen */
2801da177e4SLinus Torvalds x25_clear_queues(sk);
2811da177e4SLinus Torvalds x25_write_internal(sk, X25_RESET_REQUEST);
2821da177e4SLinus Torvalds x25_start_t22timer(sk);
2831da177e4SLinus Torvalds x25->condition = 0x00;
2841da177e4SLinus Torvalds x25->vs = 0;
2851da177e4SLinus Torvalds x25->vr = 0;
2861da177e4SLinus Torvalds x25->va = 0;
2871da177e4SLinus Torvalds x25->vl = 0;
2881da177e4SLinus Torvalds x25->state = X25_STATE_4;
2891da177e4SLinus Torvalds break;
2901da177e4SLinus Torvalds }
2911da177e4SLinus Torvalds if (atomic_read(&sk->sk_rmem_alloc) >
2926bf1574eSEric Dumazet (sk->sk_rcvbuf >> 1))
2931da177e4SLinus Torvalds x25->condition |= X25_COND_OWN_RX_BUSY;
2941da177e4SLinus Torvalds }
2951da177e4SLinus Torvalds /*
2961da177e4SLinus Torvalds * If the window is full Ack it immediately, else
2971da177e4SLinus Torvalds * start the holdback timer.
2981da177e4SLinus Torvalds */
2991da177e4SLinus Torvalds if (((x25->vl + x25->facilities.winsize_in) % modulus) == x25->vr) {
3001da177e4SLinus Torvalds x25->condition &= ~X25_COND_ACK_PENDING;
3011da177e4SLinus Torvalds x25_stop_timer(sk);
3021da177e4SLinus Torvalds x25_enquiry_response(sk);
3031da177e4SLinus Torvalds } else {
3041da177e4SLinus Torvalds x25->condition |= X25_COND_ACK_PENDING;
3051da177e4SLinus Torvalds x25_start_t2timer(sk);
3061da177e4SLinus Torvalds }
3071da177e4SLinus Torvalds break;
3081da177e4SLinus Torvalds
3091da177e4SLinus Torvalds case X25_INTERRUPT_CONFIRMATION:
310b7792e34Sandrew hendry clear_bit(X25_INTERRUPT_FLAG, &x25->flags);
3111da177e4SLinus Torvalds break;
3121da177e4SLinus Torvalds
3131da177e4SLinus Torvalds case X25_INTERRUPT:
3141da177e4SLinus Torvalds if (sock_flag(sk, SOCK_URGINLINE))
3151da177e4SLinus Torvalds queued = !sock_queue_rcv_skb(sk, skb);
3161da177e4SLinus Torvalds else {
3171da177e4SLinus Torvalds skb_set_owner_r(skb, sk);
3181da177e4SLinus Torvalds skb_queue_tail(&x25->interrupt_in_queue, skb);
3191da177e4SLinus Torvalds queued = 1;
3201da177e4SLinus Torvalds }
3211da177e4SLinus Torvalds sk_send_sigurg(sk);
3221da177e4SLinus Torvalds x25_write_internal(sk, X25_INTERRUPT_CONFIRMATION);
3231da177e4SLinus Torvalds break;
3241da177e4SLinus Torvalds
3251da177e4SLinus Torvalds default:
326b73e9e3cSwangweidong pr_warn("unknown %02X in state 3\n", frametype);
3271da177e4SLinus Torvalds break;
3281da177e4SLinus Torvalds }
3291da177e4SLinus Torvalds
3301da177e4SLinus Torvalds return queued;
331cb101ed2SMatthew Daley
332cb101ed2SMatthew Daley out_clear:
333cb101ed2SMatthew Daley x25_write_internal(sk, X25_CLEAR_REQUEST);
334cb101ed2SMatthew Daley x25->state = X25_STATE_2;
335cb101ed2SMatthew Daley x25_start_t23timer(sk);
336cb101ed2SMatthew Daley return 0;
3371da177e4SLinus Torvalds }
3381da177e4SLinus Torvalds
3391da177e4SLinus Torvalds /*
3401da177e4SLinus Torvalds * State machine for state 4, Awaiting Reset Confirmation State.
3411da177e4SLinus Torvalds * The handling of the timer(s) is in file x25_timer.c
3421da177e4SLinus Torvalds * Handling of state 0 and connection release is in af_x25.c.
3431da177e4SLinus Torvalds */
x25_state4_machine(struct sock * sk,struct sk_buff * skb,int frametype)3441da177e4SLinus Torvalds static int x25_state4_machine(struct sock *sk, struct sk_buff *skb, int frametype)
3451da177e4SLinus Torvalds {
346cb101ed2SMatthew Daley struct x25_sock *x25 = x25_sk(sk);
347cb101ed2SMatthew Daley
3481da177e4SLinus Torvalds switch (frametype) {
3491da177e4SLinus Torvalds
3501da177e4SLinus Torvalds case X25_RESET_REQUEST:
3511da177e4SLinus Torvalds x25_write_internal(sk, X25_RESET_CONFIRMATION);
352df561f66SGustavo A. R. Silva fallthrough;
3531da177e4SLinus Torvalds case X25_RESET_CONFIRMATION: {
3541da177e4SLinus Torvalds x25_stop_timer(sk);
3551da177e4SLinus Torvalds x25->condition = 0x00;
3561da177e4SLinus Torvalds x25->va = 0;
3571da177e4SLinus Torvalds x25->vr = 0;
3581da177e4SLinus Torvalds x25->vs = 0;
3591da177e4SLinus Torvalds x25->vl = 0;
3601da177e4SLinus Torvalds x25->state = X25_STATE_3;
3611da177e4SLinus Torvalds x25_requeue_frames(sk);
3621da177e4SLinus Torvalds break;
3631da177e4SLinus Torvalds }
3641da177e4SLinus Torvalds case X25_CLEAR_REQUEST:
365cb101ed2SMatthew Daley if (!pskb_may_pull(skb, X25_STD_MIN_LEN + 2))
366cb101ed2SMatthew Daley goto out_clear;
367cb101ed2SMatthew Daley
3681da177e4SLinus Torvalds x25_write_internal(sk, X25_CLEAR_CONFIRMATION);
3691da177e4SLinus Torvalds x25_disconnect(sk, 0, skb->data[3], skb->data[4]);
3701da177e4SLinus Torvalds break;
3711da177e4SLinus Torvalds
3721da177e4SLinus Torvalds default:
3731da177e4SLinus Torvalds break;
3741da177e4SLinus Torvalds }
3751da177e4SLinus Torvalds
3761da177e4SLinus Torvalds return 0;
377cb101ed2SMatthew Daley
378cb101ed2SMatthew Daley out_clear:
379cb101ed2SMatthew Daley x25_write_internal(sk, X25_CLEAR_REQUEST);
380cb101ed2SMatthew Daley x25->state = X25_STATE_2;
381cb101ed2SMatthew Daley x25_start_t23timer(sk);
382cb101ed2SMatthew Daley return 0;
3831da177e4SLinus Torvalds }
3841da177e4SLinus Torvalds
385f8fc57e8SMartin Schiller /*
386f8fc57e8SMartin Schiller * State machine for state 5, Call Accepted / Call Connected pending (X25_ACCPT_APPRV_FLAG).
387f8fc57e8SMartin Schiller * The handling of the timer(s) is in file x25_timer.c
388f8fc57e8SMartin Schiller * Handling of state 0 and connection release is in af_x25.c.
389f8fc57e8SMartin Schiller */
x25_state5_machine(struct sock * sk,struct sk_buff * skb,int frametype)390f8fc57e8SMartin Schiller static int x25_state5_machine(struct sock *sk, struct sk_buff *skb, int frametype)
391f8fc57e8SMartin Schiller {
392f8fc57e8SMartin Schiller struct x25_sock *x25 = x25_sk(sk);
393f8fc57e8SMartin Schiller
394f8fc57e8SMartin Schiller switch (frametype) {
395f8fc57e8SMartin Schiller case X25_CLEAR_REQUEST:
396f8fc57e8SMartin Schiller if (!pskb_may_pull(skb, X25_STD_MIN_LEN + 2)) {
397f8fc57e8SMartin Schiller x25_write_internal(sk, X25_CLEAR_REQUEST);
398f8fc57e8SMartin Schiller x25->state = X25_STATE_2;
399f8fc57e8SMartin Schiller x25_start_t23timer(sk);
400f8fc57e8SMartin Schiller return 0;
401f8fc57e8SMartin Schiller }
402f8fc57e8SMartin Schiller
403f8fc57e8SMartin Schiller x25_write_internal(sk, X25_CLEAR_CONFIRMATION);
404f8fc57e8SMartin Schiller x25_disconnect(sk, 0, skb->data[3], skb->data[4]);
405f8fc57e8SMartin Schiller break;
406f8fc57e8SMartin Schiller
407f8fc57e8SMartin Schiller default:
408f8fc57e8SMartin Schiller break;
409f8fc57e8SMartin Schiller }
410f8fc57e8SMartin Schiller
411f8fc57e8SMartin Schiller return 0;
412f8fc57e8SMartin Schiller }
413f8fc57e8SMartin Schiller
4141da177e4SLinus Torvalds /* Higher level upcall for a LAPB frame */
x25_process_rx_frame(struct sock * sk,struct sk_buff * skb)4151da177e4SLinus Torvalds int x25_process_rx_frame(struct sock *sk, struct sk_buff *skb)
4161da177e4SLinus Torvalds {
4171da177e4SLinus Torvalds struct x25_sock *x25 = x25_sk(sk);
4181da177e4SLinus Torvalds int queued = 0, frametype, ns, nr, q, d, m;
4191da177e4SLinus Torvalds
4201da177e4SLinus Torvalds if (x25->state == X25_STATE_0)
4211da177e4SLinus Torvalds return 0;
4221da177e4SLinus Torvalds
4231da177e4SLinus Torvalds frametype = x25_decode(sk, skb, &ns, &nr, &q, &d, &m);
4241da177e4SLinus Torvalds
4251da177e4SLinus Torvalds switch (x25->state) {
4261da177e4SLinus Torvalds case X25_STATE_1:
4271da177e4SLinus Torvalds queued = x25_state1_machine(sk, skb, frametype);
4281da177e4SLinus Torvalds break;
4291da177e4SLinus Torvalds case X25_STATE_2:
4301da177e4SLinus Torvalds queued = x25_state2_machine(sk, skb, frametype);
4311da177e4SLinus Torvalds break;
4321da177e4SLinus Torvalds case X25_STATE_3:
4331da177e4SLinus Torvalds queued = x25_state3_machine(sk, skb, frametype, ns, nr, q, d, m);
4341da177e4SLinus Torvalds break;
4351da177e4SLinus Torvalds case X25_STATE_4:
4361da177e4SLinus Torvalds queued = x25_state4_machine(sk, skb, frametype);
4371da177e4SLinus Torvalds break;
438f8fc57e8SMartin Schiller case X25_STATE_5:
439f8fc57e8SMartin Schiller queued = x25_state5_machine(sk, skb, frametype);
440f8fc57e8SMartin Schiller break;
4411da177e4SLinus Torvalds }
4421da177e4SLinus Torvalds
4431da177e4SLinus Torvalds x25_kick(sk);
4441da177e4SLinus Torvalds
4451da177e4SLinus Torvalds return queued;
4461da177e4SLinus Torvalds }
4471da177e4SLinus Torvalds
x25_backlog_rcv(struct sock * sk,struct sk_buff * skb)4481da177e4SLinus Torvalds int x25_backlog_rcv(struct sock *sk, struct sk_buff *skb)
4491da177e4SLinus Torvalds {
4501da177e4SLinus Torvalds int queued = x25_process_rx_frame(sk, skb);
4511da177e4SLinus Torvalds
4521da177e4SLinus Torvalds if (!queued)
4531da177e4SLinus Torvalds kfree_skb(skb);
4541da177e4SLinus Torvalds
4551da177e4SLinus Torvalds return 0;
4561da177e4SLinus Torvalds }
457