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