0001
0002
0003
0004
0005
0006
0007
0008
0009 #include <linux/errno.h>
0010 #include <linux/types.h>
0011 #include <linux/socket.h>
0012 #include <linux/in.h>
0013 #include <linux/kernel.h>
0014 #include <linux/timer.h>
0015 #include <linux/string.h>
0016 #include <linux/sockios.h>
0017 #include <linux/net.h>
0018 #include <linux/slab.h>
0019 #include <net/ax25.h>
0020 #include <linux/inet.h>
0021 #include <linux/netdevice.h>
0022 #include <linux/skbuff.h>
0023 #include <net/sock.h>
0024 #include <net/tcp_states.h>
0025 #include <linux/uaccess.h>
0026 #include <linux/fcntl.h>
0027 #include <linux/mm.h>
0028 #include <linux/interrupt.h>
0029
0030
0031
0032
0033 void ax25_clear_queues(ax25_cb *ax25)
0034 {
0035 skb_queue_purge(&ax25->write_queue);
0036 skb_queue_purge(&ax25->ack_queue);
0037 skb_queue_purge(&ax25->reseq_queue);
0038 skb_queue_purge(&ax25->frag_queue);
0039 }
0040
0041
0042
0043
0044
0045
0046 void ax25_frames_acked(ax25_cb *ax25, unsigned short nr)
0047 {
0048 struct sk_buff *skb;
0049
0050
0051
0052
0053 if (ax25->va != nr) {
0054 while (skb_peek(&ax25->ack_queue) != NULL && ax25->va != nr) {
0055 skb = skb_dequeue(&ax25->ack_queue);
0056 kfree_skb(skb);
0057 ax25->va = (ax25->va + 1) % ax25->modulus;
0058 }
0059 }
0060 }
0061
0062 void ax25_requeue_frames(ax25_cb *ax25)
0063 {
0064 struct sk_buff *skb;
0065
0066
0067
0068
0069
0070
0071 while ((skb = skb_dequeue_tail(&ax25->ack_queue)) != NULL)
0072 skb_queue_head(&ax25->write_queue, skb);
0073 }
0074
0075
0076
0077
0078
0079 int ax25_validate_nr(ax25_cb *ax25, unsigned short nr)
0080 {
0081 unsigned short vc = ax25->va;
0082
0083 while (vc != ax25->vs) {
0084 if (nr == vc) return 1;
0085 vc = (vc + 1) % ax25->modulus;
0086 }
0087
0088 if (nr == ax25->vs) return 1;
0089
0090 return 0;
0091 }
0092
0093
0094
0095
0096
0097 int ax25_decode(ax25_cb *ax25, struct sk_buff *skb, int *ns, int *nr, int *pf)
0098 {
0099 unsigned char *frame;
0100 int frametype = AX25_ILLEGAL;
0101
0102 frame = skb->data;
0103 *ns = *nr = *pf = 0;
0104
0105 if (ax25->modulus == AX25_MODULUS) {
0106 if ((frame[0] & AX25_S) == 0) {
0107 frametype = AX25_I;
0108 *ns = (frame[0] >> 1) & 0x07;
0109 *nr = (frame[0] >> 5) & 0x07;
0110 *pf = frame[0] & AX25_PF;
0111 } else if ((frame[0] & AX25_U) == 1) {
0112 frametype = frame[0] & 0x0F;
0113 *nr = (frame[0] >> 5) & 0x07;
0114 *pf = frame[0] & AX25_PF;
0115 } else if ((frame[0] & AX25_U) == 3) {
0116 frametype = frame[0] & ~AX25_PF;
0117 *pf = frame[0] & AX25_PF;
0118 }
0119 skb_pull(skb, 1);
0120 } else {
0121 if ((frame[0] & AX25_S) == 0) {
0122 frametype = AX25_I;
0123 *ns = (frame[0] >> 1) & 0x7F;
0124 *nr = (frame[1] >> 1) & 0x7F;
0125 *pf = frame[1] & AX25_EPF;
0126 skb_pull(skb, 2);
0127 } else if ((frame[0] & AX25_U) == 1) {
0128 frametype = frame[0] & 0x0F;
0129 *nr = (frame[1] >> 1) & 0x7F;
0130 *pf = frame[1] & AX25_EPF;
0131 skb_pull(skb, 2);
0132 } else if ((frame[0] & AX25_U) == 3) {
0133 frametype = frame[0] & ~AX25_PF;
0134 *pf = frame[0] & AX25_PF;
0135 skb_pull(skb, 1);
0136 }
0137 }
0138
0139 return frametype;
0140 }
0141
0142
0143
0144
0145
0146
0147 void ax25_send_control(ax25_cb *ax25, int frametype, int poll_bit, int type)
0148 {
0149 struct sk_buff *skb;
0150 unsigned char *dptr;
0151
0152 if ((skb = alloc_skb(ax25->ax25_dev->dev->hard_header_len + 2, GFP_ATOMIC)) == NULL)
0153 return;
0154
0155 skb_reserve(skb, ax25->ax25_dev->dev->hard_header_len);
0156
0157 skb_reset_network_header(skb);
0158
0159
0160 if (ax25->modulus == AX25_MODULUS) {
0161 dptr = skb_put(skb, 1);
0162 *dptr = frametype;
0163 *dptr |= (poll_bit) ? AX25_PF : 0;
0164 if ((frametype & AX25_U) == AX25_S)
0165 *dptr |= (ax25->vr << 5);
0166 } else {
0167 if ((frametype & AX25_U) == AX25_U) {
0168 dptr = skb_put(skb, 1);
0169 *dptr = frametype;
0170 *dptr |= (poll_bit) ? AX25_PF : 0;
0171 } else {
0172 dptr = skb_put(skb, 2);
0173 dptr[0] = frametype;
0174 dptr[1] = (ax25->vr << 1);
0175 dptr[1] |= (poll_bit) ? AX25_EPF : 0;
0176 }
0177 }
0178
0179 ax25_transmit_buffer(ax25, skb, type);
0180 }
0181
0182
0183
0184
0185
0186
0187 void ax25_return_dm(struct net_device *dev, ax25_address *src, ax25_address *dest, ax25_digi *digi)
0188 {
0189 struct sk_buff *skb;
0190 char *dptr;
0191 ax25_digi retdigi;
0192
0193 if (dev == NULL)
0194 return;
0195
0196 if ((skb = alloc_skb(dev->hard_header_len + 1, GFP_ATOMIC)) == NULL)
0197 return;
0198
0199 skb_reserve(skb, dev->hard_header_len);
0200 skb_reset_network_header(skb);
0201
0202 ax25_digi_invert(digi, &retdigi);
0203
0204 dptr = skb_put(skb, 1);
0205
0206 *dptr = AX25_DM | AX25_PF;
0207
0208
0209
0210
0211 dptr = skb_push(skb, ax25_addr_size(digi));
0212 dptr += ax25_addr_build(dptr, dest, src, &retdigi, AX25_RESPONSE, AX25_MODULUS);
0213
0214 ax25_queue_xmit(skb, dev);
0215 }
0216
0217
0218
0219
0220 void ax25_calculate_t1(ax25_cb *ax25)
0221 {
0222 int n, t = 2;
0223
0224 switch (ax25->backoff) {
0225 case 0:
0226 break;
0227
0228 case 1:
0229 t += 2 * ax25->n2count;
0230 break;
0231
0232 case 2:
0233 for (n = 0; n < ax25->n2count; n++)
0234 t *= 2;
0235 if (t > 8) t = 8;
0236 break;
0237 }
0238
0239 ax25->t1 = t * ax25->rtt;
0240 }
0241
0242
0243
0244
0245 void ax25_calculate_rtt(ax25_cb *ax25)
0246 {
0247 if (ax25->backoff == 0)
0248 return;
0249
0250 if (ax25_t1timer_running(ax25) && ax25->n2count == 0)
0251 ax25->rtt = (9 * ax25->rtt + ax25->t1 - ax25_display_timer(&ax25->t1timer)) / 10;
0252
0253 if (ax25->rtt < AX25_T1CLAMPLO)
0254 ax25->rtt = AX25_T1CLAMPLO;
0255
0256 if (ax25->rtt > AX25_T1CLAMPHI)
0257 ax25->rtt = AX25_T1CLAMPHI;
0258 }
0259
0260 void ax25_disconnect(ax25_cb *ax25, int reason)
0261 {
0262 ax25_clear_queues(ax25);
0263
0264 if (reason == ENETUNREACH) {
0265 del_timer_sync(&ax25->timer);
0266 del_timer_sync(&ax25->t1timer);
0267 del_timer_sync(&ax25->t2timer);
0268 del_timer_sync(&ax25->t3timer);
0269 del_timer_sync(&ax25->idletimer);
0270 } else {
0271 if (ax25->sk && !sock_flag(ax25->sk, SOCK_DESTROY))
0272 ax25_stop_heartbeat(ax25);
0273 ax25_stop_t1timer(ax25);
0274 ax25_stop_t2timer(ax25);
0275 ax25_stop_t3timer(ax25);
0276 ax25_stop_idletimer(ax25);
0277 }
0278
0279 ax25->state = AX25_STATE_0;
0280
0281 ax25_link_failed(ax25, reason);
0282
0283 if (ax25->sk != NULL) {
0284 local_bh_disable();
0285 bh_lock_sock(ax25->sk);
0286 ax25->sk->sk_state = TCP_CLOSE;
0287 ax25->sk->sk_err = reason;
0288 ax25->sk->sk_shutdown |= SEND_SHUTDOWN;
0289 if (!sock_flag(ax25->sk, SOCK_DEAD)) {
0290 ax25->sk->sk_state_change(ax25->sk);
0291 sock_set_flag(ax25->sk, SOCK_DEAD);
0292 }
0293 bh_unlock_sock(ax25->sk);
0294 local_bh_enable();
0295 }
0296 }