^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1) // SPDX-License-Identifier: GPL-2.0-or-later
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3) * X.25 Packet Layer release 002
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5) * This is ALPHA test software. This code may break your machine,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6) * randomly fail to work with new releases, misbehave and/or generally
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7) * screw up. It might even work.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9) * This code REQUIRES 2.1.15 or higher
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11) * History
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12) * X.25 001 Jonathan Naylor Started coding.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13) * X.25 002 Jonathan Naylor New timer architecture.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14) * 2000-09-04 Henner Eisen Prevented x25_output() skb leakage.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15) * 2000-10-27 Henner Eisen MSG_DONTWAIT for fragment allocation.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16) * 2000-11-10 Henner Eisen x25_send_iframe(): re-queued frames
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17) * needed cleaned seq-number fields.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20) #include <linux/slab.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21) #include <linux/socket.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22) #include <linux/kernel.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23) #include <linux/string.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24) #include <linux/skbuff.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25) #include <net/sock.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26) #include <net/x25.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28) static int x25_pacsize_to_bytes(unsigned int pacsize)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30) int bytes = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32) if (!pacsize)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33) return 128;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35) while (pacsize-- > 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36) bytes *= 2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38) return bytes;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42) * This is where all X.25 information frames pass.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44) * Returns the amount of user data bytes sent on success
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45) * or a negative error code on failure.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47) int x25_output(struct sock *sk, struct sk_buff *skb)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49) struct sk_buff *skbn;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50) unsigned char header[X25_EXT_MIN_LEN];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51) int err, frontlen, len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52) int sent=0, noblock = X25_SKB_CB(skb)->flags & MSG_DONTWAIT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53) struct x25_sock *x25 = x25_sk(sk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54) int header_len = x25->neighbour->extended ? X25_EXT_MIN_LEN :
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55) X25_STD_MIN_LEN;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56) int max_len = x25_pacsize_to_bytes(x25->facilities.pacsize_out);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58) if (skb->len - header_len > max_len) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59) /* Save a copy of the Header */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60) skb_copy_from_linear_data(skb, header, header_len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61) skb_pull(skb, header_len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63) frontlen = skb_headroom(skb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 64)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 65) while (skb->len > 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 66) release_sock(sk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 67) skbn = sock_alloc_send_skb(sk, frontlen + max_len,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 68) noblock, &err);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 69) lock_sock(sk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 70) if (!skbn) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 71) if (err == -EWOULDBLOCK && noblock){
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 72) kfree_skb(skb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 73) return sent;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 74) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 75) SOCK_DEBUG(sk, "x25_output: fragment alloc"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 76) " failed, err=%d, %d bytes "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 77) "sent\n", err, sent);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 78) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 79) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 80)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 81) skb_reserve(skbn, frontlen);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 82)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 83) len = max_len > skb->len ? skb->len : max_len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 84)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 85) /* Copy the user data */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 86) skb_copy_from_linear_data(skb, skb_put(skbn, len), len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 87) skb_pull(skb, len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 88)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 89) /* Duplicate the Header */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 90) skb_push(skbn, header_len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 91) skb_copy_to_linear_data(skbn, header, header_len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 92)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 93) if (skb->len > 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 94) if (x25->neighbour->extended)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 95) skbn->data[3] |= X25_EXT_M_BIT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 96) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 97) skbn->data[2] |= X25_STD_M_BIT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 98) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 99)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) skb_queue_tail(&sk->sk_write_queue, skbn);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) sent += len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) kfree_skb(skb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) skb_queue_tail(&sk->sk_write_queue, skb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) sent = skb->len - header_len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) return sent;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) * This procedure is passed a buffer descriptor for an iframe. It builds
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) * the rest of the control part of the frame and then writes it out.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) static void x25_send_iframe(struct sock *sk, struct sk_buff *skb)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) struct x25_sock *x25 = x25_sk(sk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) if (!skb)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) if (x25->neighbour->extended) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) skb->data[2] = (x25->vs << 1) & 0xFE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) skb->data[3] &= X25_EXT_M_BIT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) skb->data[3] |= (x25->vr << 1) & 0xFE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) skb->data[2] &= X25_STD_M_BIT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) skb->data[2] |= (x25->vs << 1) & 0x0E;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) skb->data[2] |= (x25->vr << 5) & 0xE0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) x25_transmit_link(skb, x25->neighbour);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) void x25_kick(struct sock *sk)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) struct sk_buff *skb, *skbn;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) unsigned short start, end;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) int modulus;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) struct x25_sock *x25 = x25_sk(sk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) if (x25->state != X25_STATE_3)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) * Transmit interrupt data.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) if (skb_peek(&x25->interrupt_out_queue) != NULL &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) !test_and_set_bit(X25_INTERRUPT_FLAG, &x25->flags)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) skb = skb_dequeue(&x25->interrupt_out_queue);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) x25_transmit_link(skb, x25->neighbour);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) if (x25->condition & X25_COND_PEER_RX_BUSY)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) if (!skb_peek(&sk->sk_write_queue))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) modulus = x25->neighbour->extended ? X25_EMODULUS : X25_SMODULUS;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) start = skb_peek(&x25->ack_queue) ? x25->vs : x25->va;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) end = (x25->va + x25->facilities.winsize_out) % modulus;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) if (start == end)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) x25->vs = start;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) * Transmit data until either we're out of data to send or
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) * the window is full.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177) skb = skb_dequeue(&sk->sk_write_queue);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179) do {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180) if ((skbn = skb_clone(skb, GFP_ATOMIC)) == NULL) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181) skb_queue_head(&sk->sk_write_queue, skb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185) skb_set_owner_w(skbn, sk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188) * Transmit the frame copy.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190) x25_send_iframe(sk, skbn);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192) x25->vs = (x25->vs + 1) % modulus;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195) * Requeue the original data frame.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197) skb_queue_tail(&x25->ack_queue, skb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199) } while (x25->vs != end &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200) (skb = skb_dequeue(&sk->sk_write_queue)) != NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202) x25->vl = x25->vr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 203) x25->condition &= ~X25_COND_ACK_PENDING;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 204)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 205) x25_stop_timer(sk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 206) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 207)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 208) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 209) * The following routines are taken from page 170 of the 7th ARRL Computer
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 210) * Networking Conference paper, as is the whole state machine.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 211) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 212)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 213) void x25_enquiry_response(struct sock *sk)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 214) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 215) struct x25_sock *x25 = x25_sk(sk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 216)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 217) if (x25->condition & X25_COND_OWN_RX_BUSY)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 218) x25_write_internal(sk, X25_RNR);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 219) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 220) x25_write_internal(sk, X25_RR);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 221)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 222) x25->vl = x25->vr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 223) x25->condition &= ~X25_COND_ACK_PENDING;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 224)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 225) x25_stop_timer(sk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 226) }