1/*
2 * This program is free software; you can redistribute it and/or modify
3 * it under the terms of the GNU General Public License as published by
4 * the Free Software Foundation; either version 2 of the License, or
5 * (at your option) any later version.
6 *
7 * Copyright (C) Jonathan Naylor G4KLX (g4klx@g4klx.demon.co.uk)
8 * Copyright (C) Joerg Reuter DL1BKE (jreuter@yaina.de)
9 */
10#include <linux/errno.h>
11#include <linux/types.h>
12#include <linux/socket.h>
13#include <linux/in.h>
14#include <linux/kernel.h>
15#include <linux/timer.h>
16#include <linux/string.h>
17#include <linux/sockios.h>
18#include <linux/spinlock.h>
19#include <linux/net.h>
20#include <net/ax25.h>
21#include <linux/inet.h>
22#include <linux/netdevice.h>
23#include <linux/skbuff.h>
24#include <net/sock.h>
25#include <asm/uaccess.h>
26#include <asm/system.h>
27#include <linux/fcntl.h>
28#include <linux/mm.h>
29#include <linux/interrupt.h>
30
31void ax25_ds_nr_error_recovery(ax25_cb *ax25)
32{
33	ax25_ds_establish_data_link(ax25);
34}
35
36/*
37 *	dl1bke 960114: transmit I frames on DAMA poll
38 */
39void ax25_ds_enquiry_response(ax25_cb *ax25)
40{
41	ax25_cb *ax25o;
42	struct hlist_node *node;
43
44	/* Please note that neither DK4EG�s nor DG2FEF�s
45	 * DAMA spec mention the following behaviour as seen
46	 * with TheFirmware:
47	 *
48	 * 	DB0ACH->DL1BKE <RR C P R0> [DAMA]
49	 *	DL1BKE->DB0ACH <I NR=0 NS=0>
50	 *	DL1BKE-7->DB0PRA-6 DB0ACH <I C S3 R5>
51	 *	DL1BKE->DB0ACH <RR R F R0>
52	 *
53	 * The Flexnet DAMA Master implementation apparently
54	 * insists on the "proper" AX.25 behaviour:
55	 *
56	 * 	DB0ACH->DL1BKE <RR C P R0> [DAMA]
57	 *	DL1BKE->DB0ACH <RR R F R0>
58	 *	DL1BKE->DB0ACH <I NR=0 NS=0>
59	 *	DL1BKE-7->DB0PRA-6 DB0ACH <I C S3 R5>
60	 *
61	 * Flexnet refuses to send us *any* I frame if we send
62	 * a REJ in case AX25_COND_REJECT is set. It is superfluous in
63	 * this mode anyway (a RR or RNR invokes the retransmission).
64	 * Is this a Flexnet bug?
65	 */
66
67	ax25_std_enquiry_response(ax25);
68
69	if (!(ax25->condition & AX25_COND_PEER_RX_BUSY)) {
70		ax25_requeue_frames(ax25);
71		ax25_kick(ax25);
72	}
73
74	if (ax25->state == AX25_STATE_1 || ax25->state == AX25_STATE_2 || skb_peek(&ax25->ack_queue) != NULL)
75		ax25_ds_t1_timeout(ax25);
76	else
77		ax25->n2count = 0;
78
79	ax25_start_t3timer(ax25);
80	ax25_ds_set_timer(ax25->ax25_dev);
81
82	spin_lock(&ax25_list_lock);
83	ax25_for_each(ax25o, node, &ax25_list) {
84		if (ax25o == ax25)
85			continue;
86
87		if (ax25o->ax25_dev != ax25->ax25_dev)
88			continue;
89
90		if (ax25o->state == AX25_STATE_1 || ax25o->state == AX25_STATE_2) {
91			ax25_ds_t1_timeout(ax25o);
92			continue;
93		}
94
95		if (!(ax25o->condition & AX25_COND_PEER_RX_BUSY) && ax25o->state == AX25_STATE_3) {
96			ax25_requeue_frames(ax25o);
97			ax25_kick(ax25o);
98		}
99
100		if (ax25o->state == AX25_STATE_1 || ax25o->state == AX25_STATE_2 || skb_peek(&ax25o->ack_queue) != NULL)
101			ax25_ds_t1_timeout(ax25o);
102
103		/* do not start T3 for listening sockets (tnx DD8NE) */
104
105		if (ax25o->state != AX25_STATE_0)
106			ax25_start_t3timer(ax25o);
107	}
108	spin_unlock(&ax25_list_lock);
109}
110
111void ax25_ds_establish_data_link(ax25_cb *ax25)
112{
113	ax25->condition &= AX25_COND_DAMA_MODE;
114	ax25->n2count    = 0;
115	ax25_calculate_t1(ax25);
116	ax25_start_t1timer(ax25);
117	ax25_stop_t2timer(ax25);
118	ax25_start_t3timer(ax25);
119}
120
121static void ax25_kiss_cmd(ax25_dev *ax25_dev, unsigned char cmd, unsigned char param)
122{
123	struct sk_buff *skb;
124	unsigned char *p;
125
126	if (ax25_dev->dev == NULL)
127		return;
128
129	if ((skb = alloc_skb(2, GFP_ATOMIC)) == NULL)
130		return;
131
132	skb_reset_network_header(skb);
133	p = skb_put(skb, 2);
134
135	*p++ = cmd;
136	*p++ = param;
137
138	skb->protocol = ax25_type_trans(skb, ax25_dev->dev);
139
140	dev_queue_xmit(skb);
141}
142
143/*
144 *	A nasty problem arises if we count the number of DAMA connections
145 *	wrong, especially when connections on the device already existed
146 *	and our network node (or the sysop) decides to turn on DAMA Master
147 *	mode. We thus flag the 'real' slave connections with
148 *	ax25->dama_slave=1 and look on every disconnect if still slave
149 *	connections exist.
150 */
151static int ax25_check_dama_slave(ax25_dev *ax25_dev)
152{
153	ax25_cb *ax25;
154	int res = 0;
155	struct hlist_node *node;
156
157	spin_lock(&ax25_list_lock);
158	ax25_for_each(ax25, node, &ax25_list)
159		if (ax25->ax25_dev == ax25_dev && (ax25->condition & AX25_COND_DAMA_MODE) && ax25->state > AX25_STATE_1) {
160			res = 1;
161			break;
162		}
163	spin_unlock(&ax25_list_lock);
164
165	return res;
166}
167
168static void ax25_dev_dama_on(ax25_dev *ax25_dev)
169{
170	if (ax25_dev == NULL)
171		return;
172
173	if (ax25_dev->dama.slave == 0)
174		ax25_kiss_cmd(ax25_dev, 5, 1);
175
176	ax25_dev->dama.slave = 1;
177	ax25_ds_set_timer(ax25_dev);
178}
179
180void ax25_dev_dama_off(ax25_dev *ax25_dev)
181{
182	if (ax25_dev == NULL)
183		return;
184
185	if (ax25_dev->dama.slave && !ax25_check_dama_slave(ax25_dev)) {
186		ax25_kiss_cmd(ax25_dev, 5, 0);
187		ax25_dev->dama.slave = 0;
188		ax25_ds_del_timer(ax25_dev);
189	}
190}
191
192void ax25_dama_on(ax25_cb *ax25)
193{
194	ax25_dev_dama_on(ax25->ax25_dev);
195	ax25->condition |= AX25_COND_DAMA_MODE;
196}
197
198void ax25_dama_off(ax25_cb *ax25)
199{
200	ax25->condition &= ~AX25_COND_DAMA_MODE;
201	ax25_dev_dama_off(ax25->ax25_dev);
202}
203