Linux Audio

Check our new training course

Loading...
v3.1
  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/spinlock.h>
 14#include <linux/in.h>
 15#include <linux/kernel.h>
 16#include <linux/jiffies.h>
 17#include <linux/timer.h>
 18#include <linux/string.h>
 19#include <linux/sockios.h>
 20#include <linux/net.h>
 21#include <net/tcp_states.h>
 22#include <net/ax25.h>
 23#include <linux/inet.h>
 24#include <linux/netdevice.h>
 25#include <linux/skbuff.h>
 26#include <net/sock.h>
 27#include <asm/uaccess.h>
 28#include <asm/system.h>
 29#include <linux/fcntl.h>
 30#include <linux/mm.h>
 31#include <linux/interrupt.h>
 32
 33static void ax25_ds_timeout(unsigned long);
 34
 35/*
 36 *	Add DAMA slave timeout timer to timer list.
 37 *	Unlike the connection based timers the timeout function gets
 38 *	triggered every second. Please note that NET_AX25_DAMA_SLAVE_TIMEOUT
 39 *	(aka /proc/sys/net/ax25/{dev}/dama_slave_timeout) is still in
 40 *	1/10th of a second.
 41 */
 42
 43void ax25_ds_setup_timer(ax25_dev *ax25_dev)
 44{
 45	setup_timer(&ax25_dev->dama.slave_timer, ax25_ds_timeout,
 46		    (unsigned long)ax25_dev);
 47}
 48
 49void ax25_ds_del_timer(ax25_dev *ax25_dev)
 50{
 51	if (ax25_dev)
 52		del_timer(&ax25_dev->dama.slave_timer);
 53}
 54
 55void ax25_ds_set_timer(ax25_dev *ax25_dev)
 56{
 57	if (ax25_dev == NULL)		/* paranoia */
 58		return;
 59
 60	ax25_dev->dama.slave_timeout =
 61		msecs_to_jiffies(ax25_dev->values[AX25_VALUES_DS_TIMEOUT]) / 10;
 62	mod_timer(&ax25_dev->dama.slave_timer, jiffies + HZ);
 63}
 64
 65/*
 66 *	DAMA Slave Timeout
 67 *	Silently discard all (slave) connections in case our master forgot us...
 68 */
 69
 70static void ax25_ds_timeout(unsigned long arg)
 71{
 72	ax25_dev *ax25_dev = (struct ax25_dev *) arg;
 73	ax25_cb *ax25;
 74	struct hlist_node *node;
 75
 76	if (ax25_dev == NULL || !ax25_dev->dama.slave)
 77		return;			/* Yikes! */
 78
 79	if (!ax25_dev->dama.slave_timeout || --ax25_dev->dama.slave_timeout) {
 80		ax25_ds_set_timer(ax25_dev);
 81		return;
 82	}
 83
 84	spin_lock(&ax25_list_lock);
 85	ax25_for_each(ax25, node, &ax25_list) {
 86		if (ax25->ax25_dev != ax25_dev || !(ax25->condition & AX25_COND_DAMA_MODE))
 87			continue;
 88
 89		ax25_send_control(ax25, AX25_DISC, AX25_POLLON, AX25_COMMAND);
 90		ax25_disconnect(ax25, ETIMEDOUT);
 91	}
 92	spin_unlock(&ax25_list_lock);
 93
 94	ax25_dev_dama_off(ax25_dev);
 95}
 96
 97void ax25_ds_heartbeat_expiry(ax25_cb *ax25)
 98{
 99	struct sock *sk=ax25->sk;
100
101	if (sk)
102		bh_lock_sock(sk);
103
104	switch (ax25->state) {
105
106	case AX25_STATE_0:
107		/* Magic here: If we listen() and a new link dies before it
108		   is accepted() it isn't 'dead' so doesn't get removed. */
109		if (!sk || sock_flag(sk, SOCK_DESTROY) ||
110		    (sk->sk_state == TCP_LISTEN &&
111		     sock_flag(sk, SOCK_DEAD))) {
112			if (sk) {
113				sock_hold(sk);
114				ax25_destroy_socket(ax25);
115				bh_unlock_sock(sk);
116				sock_put(sk);
117			} else
118				ax25_destroy_socket(ax25);
119			return;
120		}
121		break;
122
123	case AX25_STATE_3:
124		/*
125		 * Check the state of the receive buffer.
126		 */
127		if (sk != NULL) {
128			if (atomic_read(&sk->sk_rmem_alloc) <
129			    (sk->sk_rcvbuf >> 1) &&
130			    (ax25->condition & AX25_COND_OWN_RX_BUSY)) {
131				ax25->condition &= ~AX25_COND_OWN_RX_BUSY;
132				ax25->condition &= ~AX25_COND_ACK_PENDING;
133				break;
134			}
135		}
136		break;
137	}
138
139	if (sk)
140		bh_unlock_sock(sk);
141
142	ax25_start_heartbeat(ax25);
143}
144
145/* dl1bke 960114: T3 works much like the IDLE timeout, but
146 *                gets reloaded with every frame for this
147 *		  connection.
148 */
149void ax25_ds_t3timer_expiry(ax25_cb *ax25)
150{
151	ax25_send_control(ax25, AX25_DISC, AX25_POLLON, AX25_COMMAND);
152	ax25_dama_off(ax25);
153	ax25_disconnect(ax25, ETIMEDOUT);
154}
155
156/* dl1bke 960228: close the connection when IDLE expires.
157 *		  unlike T3 this timer gets reloaded only on
158 *		  I frames.
159 */
160void ax25_ds_idletimer_expiry(ax25_cb *ax25)
161{
162	ax25_clear_queues(ax25);
163
164	ax25->n2count = 0;
165	ax25->state = AX25_STATE_2;
166
167	ax25_calculate_t1(ax25);
168	ax25_start_t1timer(ax25);
169	ax25_stop_t3timer(ax25);
170
171	if (ax25->sk != NULL) {
172		bh_lock_sock(ax25->sk);
173		ax25->sk->sk_state     = TCP_CLOSE;
174		ax25->sk->sk_err       = 0;
175		ax25->sk->sk_shutdown |= SEND_SHUTDOWN;
176		if (!sock_flag(ax25->sk, SOCK_DEAD)) {
177			ax25->sk->sk_state_change(ax25->sk);
178			sock_set_flag(ax25->sk, SOCK_DEAD);
179		}
180		bh_unlock_sock(ax25->sk);
181	}
182}
183
184/* dl1bke 960114: The DAMA protocol requires to send data and SABM/DISC
185 *                within the poll of any connected channel. Remember
186 *                that we are not allowed to send anything unless we
187 *                get polled by the Master.
188 *
189 *                Thus we'll have to do parts of our T1 handling in
190 *                ax25_enquiry_response().
191 */
192void ax25_ds_t1_timeout(ax25_cb *ax25)
193{
194	switch (ax25->state) {
195	case AX25_STATE_1:
196		if (ax25->n2count == ax25->n2) {
197			if (ax25->modulus == AX25_MODULUS) {
198				ax25_disconnect(ax25, ETIMEDOUT);
199				return;
200			} else {
201				ax25->modulus = AX25_MODULUS;
202				ax25->window  = ax25->ax25_dev->values[AX25_VALUES_WINDOW];
203				ax25->n2count = 0;
204				ax25_send_control(ax25, AX25_SABM, AX25_POLLOFF, AX25_COMMAND);
205			}
206		} else {
207			ax25->n2count++;
208			if (ax25->modulus == AX25_MODULUS)
209				ax25_send_control(ax25, AX25_SABM, AX25_POLLOFF, AX25_COMMAND);
210			else
211				ax25_send_control(ax25, AX25_SABME, AX25_POLLOFF, AX25_COMMAND);
212		}
213		break;
214
215	case AX25_STATE_2:
216		if (ax25->n2count == ax25->n2) {
217			ax25_send_control(ax25, AX25_DISC, AX25_POLLON, AX25_COMMAND);
218			ax25_disconnect(ax25, ETIMEDOUT);
219			return;
220		} else {
221			ax25->n2count++;
222		}
223		break;
224
225	case AX25_STATE_3:
226		if (ax25->n2count == ax25->n2) {
227			ax25_send_control(ax25, AX25_DM, AX25_POLLON, AX25_RESPONSE);
228			ax25_disconnect(ax25, ETIMEDOUT);
229			return;
230		} else {
231			ax25->n2count++;
232		}
233		break;
234	}
235
236	ax25_calculate_t1(ax25);
237	ax25_start_t1timer(ax25);
238}
v4.6
  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/spinlock.h>
 14#include <linux/in.h>
 15#include <linux/kernel.h>
 16#include <linux/jiffies.h>
 17#include <linux/timer.h>
 18#include <linux/string.h>
 19#include <linux/sockios.h>
 20#include <linux/net.h>
 21#include <net/tcp_states.h>
 22#include <net/ax25.h>
 23#include <linux/inet.h>
 24#include <linux/netdevice.h>
 25#include <linux/skbuff.h>
 26#include <net/sock.h>
 27#include <asm/uaccess.h>
 
 28#include <linux/fcntl.h>
 29#include <linux/mm.h>
 30#include <linux/interrupt.h>
 31
 32static void ax25_ds_timeout(unsigned long);
 33
 34/*
 35 *	Add DAMA slave timeout timer to timer list.
 36 *	Unlike the connection based timers the timeout function gets
 37 *	triggered every second. Please note that NET_AX25_DAMA_SLAVE_TIMEOUT
 38 *	(aka /proc/sys/net/ax25/{dev}/dama_slave_timeout) is still in
 39 *	1/10th of a second.
 40 */
 41
 42void ax25_ds_setup_timer(ax25_dev *ax25_dev)
 43{
 44	setup_timer(&ax25_dev->dama.slave_timer, ax25_ds_timeout,
 45		    (unsigned long)ax25_dev);
 46}
 47
 48void ax25_ds_del_timer(ax25_dev *ax25_dev)
 49{
 50	if (ax25_dev)
 51		del_timer(&ax25_dev->dama.slave_timer);
 52}
 53
 54void ax25_ds_set_timer(ax25_dev *ax25_dev)
 55{
 56	if (ax25_dev == NULL)		/* paranoia */
 57		return;
 58
 59	ax25_dev->dama.slave_timeout =
 60		msecs_to_jiffies(ax25_dev->values[AX25_VALUES_DS_TIMEOUT]) / 10;
 61	mod_timer(&ax25_dev->dama.slave_timer, jiffies + HZ);
 62}
 63
 64/*
 65 *	DAMA Slave Timeout
 66 *	Silently discard all (slave) connections in case our master forgot us...
 67 */
 68
 69static void ax25_ds_timeout(unsigned long arg)
 70{
 71	ax25_dev *ax25_dev = (struct ax25_dev *) arg;
 72	ax25_cb *ax25;
 
 73
 74	if (ax25_dev == NULL || !ax25_dev->dama.slave)
 75		return;			/* Yikes! */
 76
 77	if (!ax25_dev->dama.slave_timeout || --ax25_dev->dama.slave_timeout) {
 78		ax25_ds_set_timer(ax25_dev);
 79		return;
 80	}
 81
 82	spin_lock(&ax25_list_lock);
 83	ax25_for_each(ax25, &ax25_list) {
 84		if (ax25->ax25_dev != ax25_dev || !(ax25->condition & AX25_COND_DAMA_MODE))
 85			continue;
 86
 87		ax25_send_control(ax25, AX25_DISC, AX25_POLLON, AX25_COMMAND);
 88		ax25_disconnect(ax25, ETIMEDOUT);
 89	}
 90	spin_unlock(&ax25_list_lock);
 91
 92	ax25_dev_dama_off(ax25_dev);
 93}
 94
 95void ax25_ds_heartbeat_expiry(ax25_cb *ax25)
 96{
 97	struct sock *sk=ax25->sk;
 98
 99	if (sk)
100		bh_lock_sock(sk);
101
102	switch (ax25->state) {
103
104	case AX25_STATE_0:
105		/* Magic here: If we listen() and a new link dies before it
106		   is accepted() it isn't 'dead' so doesn't get removed. */
107		if (!sk || sock_flag(sk, SOCK_DESTROY) ||
108		    (sk->sk_state == TCP_LISTEN &&
109		     sock_flag(sk, SOCK_DEAD))) {
110			if (sk) {
111				sock_hold(sk);
112				ax25_destroy_socket(ax25);
113				bh_unlock_sock(sk);
114				sock_put(sk);
115			} else
116				ax25_destroy_socket(ax25);
117			return;
118		}
119		break;
120
121	case AX25_STATE_3:
122		/*
123		 * Check the state of the receive buffer.
124		 */
125		if (sk != NULL) {
126			if (atomic_read(&sk->sk_rmem_alloc) <
127			    (sk->sk_rcvbuf >> 1) &&
128			    (ax25->condition & AX25_COND_OWN_RX_BUSY)) {
129				ax25->condition &= ~AX25_COND_OWN_RX_BUSY;
130				ax25->condition &= ~AX25_COND_ACK_PENDING;
131				break;
132			}
133		}
134		break;
135	}
136
137	if (sk)
138		bh_unlock_sock(sk);
139
140	ax25_start_heartbeat(ax25);
141}
142
143/* dl1bke 960114: T3 works much like the IDLE timeout, but
144 *                gets reloaded with every frame for this
145 *		  connection.
146 */
147void ax25_ds_t3timer_expiry(ax25_cb *ax25)
148{
149	ax25_send_control(ax25, AX25_DISC, AX25_POLLON, AX25_COMMAND);
150	ax25_dama_off(ax25);
151	ax25_disconnect(ax25, ETIMEDOUT);
152}
153
154/* dl1bke 960228: close the connection when IDLE expires.
155 *		  unlike T3 this timer gets reloaded only on
156 *		  I frames.
157 */
158void ax25_ds_idletimer_expiry(ax25_cb *ax25)
159{
160	ax25_clear_queues(ax25);
161
162	ax25->n2count = 0;
163	ax25->state = AX25_STATE_2;
164
165	ax25_calculate_t1(ax25);
166	ax25_start_t1timer(ax25);
167	ax25_stop_t3timer(ax25);
168
169	if (ax25->sk != NULL) {
170		bh_lock_sock(ax25->sk);
171		ax25->sk->sk_state     = TCP_CLOSE;
172		ax25->sk->sk_err       = 0;
173		ax25->sk->sk_shutdown |= SEND_SHUTDOWN;
174		if (!sock_flag(ax25->sk, SOCK_DEAD)) {
175			ax25->sk->sk_state_change(ax25->sk);
176			sock_set_flag(ax25->sk, SOCK_DEAD);
177		}
178		bh_unlock_sock(ax25->sk);
179	}
180}
181
182/* dl1bke 960114: The DAMA protocol requires to send data and SABM/DISC
183 *                within the poll of any connected channel. Remember
184 *                that we are not allowed to send anything unless we
185 *                get polled by the Master.
186 *
187 *                Thus we'll have to do parts of our T1 handling in
188 *                ax25_enquiry_response().
189 */
190void ax25_ds_t1_timeout(ax25_cb *ax25)
191{
192	switch (ax25->state) {
193	case AX25_STATE_1:
194		if (ax25->n2count == ax25->n2) {
195			if (ax25->modulus == AX25_MODULUS) {
196				ax25_disconnect(ax25, ETIMEDOUT);
197				return;
198			} else {
199				ax25->modulus = AX25_MODULUS;
200				ax25->window  = ax25->ax25_dev->values[AX25_VALUES_WINDOW];
201				ax25->n2count = 0;
202				ax25_send_control(ax25, AX25_SABM, AX25_POLLOFF, AX25_COMMAND);
203			}
204		} else {
205			ax25->n2count++;
206			if (ax25->modulus == AX25_MODULUS)
207				ax25_send_control(ax25, AX25_SABM, AX25_POLLOFF, AX25_COMMAND);
208			else
209				ax25_send_control(ax25, AX25_SABME, AX25_POLLOFF, AX25_COMMAND);
210		}
211		break;
212
213	case AX25_STATE_2:
214		if (ax25->n2count == ax25->n2) {
215			ax25_send_control(ax25, AX25_DISC, AX25_POLLON, AX25_COMMAND);
216			ax25_disconnect(ax25, ETIMEDOUT);
217			return;
218		} else {
219			ax25->n2count++;
220		}
221		break;
222
223	case AX25_STATE_3:
224		if (ax25->n2count == ax25->n2) {
225			ax25_send_control(ax25, AX25_DM, AX25_POLLON, AX25_RESPONSE);
226			ax25_disconnect(ax25, ETIMEDOUT);
227			return;
228		} else {
229			ax25->n2count++;
230		}
231		break;
232	}
233
234	ax25_calculate_t1(ax25);
235	ax25_start_t1timer(ax25);
236}