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