xref: /linux/net/ax25/ax25_timer.c (revision df1c941468fca014ad092f76672966bb412c2848)
12874c5fdSThomas Gleixner // SPDX-License-Identifier: GPL-2.0-or-later
21da177e4SLinus Torvalds /*
31da177e4SLinus Torvalds  *
41da177e4SLinus Torvalds  * Copyright (C) Alan Cox GW4PTS (alan@lxorguk.ukuu.org.uk)
51da177e4SLinus Torvalds  * Copyright (C) Jonathan Naylor G4KLX (g4klx@g4klx.demon.co.uk)
61da177e4SLinus Torvalds  * Copyright (C) Tomi Manninen OH2BNS (oh2bns@sral.fi)
71da177e4SLinus Torvalds  * Copyright (C) Darryl Miles G7LED (dlm@g7led.demon.co.uk)
81da177e4SLinus Torvalds  * Copyright (C) Joerg Reuter DL1BKE (jreuter@yaina.de)
91da177e4SLinus Torvalds  * Copyright (C) Frederic Rible F1OAT (frible@teaser.fr)
101da177e4SLinus Torvalds  * Copyright (C) 2002 Ralf Baechle DO1GRB (ralf@gnu.org)
111da177e4SLinus Torvalds  */
121da177e4SLinus Torvalds #include <linux/errno.h>
131da177e4SLinus Torvalds #include <linux/types.h>
141da177e4SLinus Torvalds #include <linux/socket.h>
151da177e4SLinus Torvalds #include <linux/in.h>
161da177e4SLinus Torvalds #include <linux/kernel.h>
1770868eacSRalf Baechle #include <linux/module.h>
181da177e4SLinus Torvalds #include <linux/jiffies.h>
191da177e4SLinus Torvalds #include <linux/timer.h>
201da177e4SLinus Torvalds #include <linux/string.h>
211da177e4SLinus Torvalds #include <linux/sockios.h>
221da177e4SLinus Torvalds #include <linux/net.h>
231da177e4SLinus Torvalds #include <net/ax25.h>
241da177e4SLinus Torvalds #include <linux/inet.h>
251da177e4SLinus Torvalds #include <linux/netdevice.h>
261da177e4SLinus Torvalds #include <linux/skbuff.h>
271da177e4SLinus Torvalds #include <net/sock.h>
287c0f6ba6SLinus Torvalds #include <linux/uaccess.h>
291da177e4SLinus Torvalds #include <linux/fcntl.h>
301da177e4SLinus Torvalds #include <linux/mm.h>
311da177e4SLinus Torvalds #include <linux/interrupt.h>
321da177e4SLinus Torvalds 
338dbd05ffSKees Cook static void ax25_heartbeat_expiry(struct timer_list *);
348dbd05ffSKees Cook static void ax25_t1timer_expiry(struct timer_list *);
358dbd05ffSKees Cook static void ax25_t2timer_expiry(struct timer_list *);
368dbd05ffSKees Cook static void ax25_t3timer_expiry(struct timer_list *);
378dbd05ffSKees Cook static void ax25_idletimer_expiry(struct timer_list *);
381da177e4SLinus Torvalds 
3921fab4a8SJarek Poplawski void ax25_setup_timers(ax25_cb *ax25)
4021fab4a8SJarek Poplawski {
418dbd05ffSKees Cook 	timer_setup(&ax25->timer, ax25_heartbeat_expiry, 0);
428dbd05ffSKees Cook 	timer_setup(&ax25->t1timer, ax25_t1timer_expiry, 0);
438dbd05ffSKees Cook 	timer_setup(&ax25->t2timer, ax25_t2timer_expiry, 0);
448dbd05ffSKees Cook 	timer_setup(&ax25->t3timer, ax25_t3timer_expiry, 0);
458dbd05ffSKees Cook 	timer_setup(&ax25->idletimer, ax25_idletimer_expiry, 0);
4621fab4a8SJarek Poplawski }
4721fab4a8SJarek Poplawski 
481da177e4SLinus Torvalds void ax25_start_heartbeat(ax25_cb *ax25)
491da177e4SLinus Torvalds {
5021fab4a8SJarek Poplawski 	mod_timer(&ax25->timer, jiffies + 5 * HZ);
511da177e4SLinus Torvalds }
521da177e4SLinus Torvalds 
531da177e4SLinus Torvalds void ax25_start_t1timer(ax25_cb *ax25)
541da177e4SLinus Torvalds {
5521fab4a8SJarek Poplawski 	mod_timer(&ax25->t1timer, jiffies + ax25->t1);
561da177e4SLinus Torvalds }
571da177e4SLinus Torvalds 
581da177e4SLinus Torvalds void ax25_start_t2timer(ax25_cb *ax25)
591da177e4SLinus Torvalds {
6021fab4a8SJarek Poplawski 	mod_timer(&ax25->t2timer, jiffies + ax25->t2);
611da177e4SLinus Torvalds }
621da177e4SLinus Torvalds 
631da177e4SLinus Torvalds void ax25_start_t3timer(ax25_cb *ax25)
641da177e4SLinus Torvalds {
6521fab4a8SJarek Poplawski 	if (ax25->t3 > 0)
6621fab4a8SJarek Poplawski 		mod_timer(&ax25->t3timer, jiffies + ax25->t3);
6721fab4a8SJarek Poplawski 	else
681da177e4SLinus Torvalds 		del_timer(&ax25->t3timer);
691da177e4SLinus Torvalds }
701da177e4SLinus Torvalds 
711da177e4SLinus Torvalds void ax25_start_idletimer(ax25_cb *ax25)
721da177e4SLinus Torvalds {
7321fab4a8SJarek Poplawski 	if (ax25->idle > 0)
7421fab4a8SJarek Poplawski 		mod_timer(&ax25->idletimer, jiffies + ax25->idle);
7521fab4a8SJarek Poplawski 	else
761da177e4SLinus Torvalds 		del_timer(&ax25->idletimer);
771da177e4SLinus Torvalds }
781da177e4SLinus Torvalds 
791da177e4SLinus Torvalds void ax25_stop_heartbeat(ax25_cb *ax25)
801da177e4SLinus Torvalds {
811da177e4SLinus Torvalds 	del_timer(&ax25->timer);
821da177e4SLinus Torvalds }
831da177e4SLinus Torvalds 
841da177e4SLinus Torvalds void ax25_stop_t1timer(ax25_cb *ax25)
851da177e4SLinus Torvalds {
861da177e4SLinus Torvalds 	del_timer(&ax25->t1timer);
871da177e4SLinus Torvalds }
881da177e4SLinus Torvalds 
891da177e4SLinus Torvalds void ax25_stop_t2timer(ax25_cb *ax25)
901da177e4SLinus Torvalds {
911da177e4SLinus Torvalds 	del_timer(&ax25->t2timer);
921da177e4SLinus Torvalds }
931da177e4SLinus Torvalds 
941da177e4SLinus Torvalds void ax25_stop_t3timer(ax25_cb *ax25)
951da177e4SLinus Torvalds {
961da177e4SLinus Torvalds 	del_timer(&ax25->t3timer);
971da177e4SLinus Torvalds }
981da177e4SLinus Torvalds 
991da177e4SLinus Torvalds void ax25_stop_idletimer(ax25_cb *ax25)
1001da177e4SLinus Torvalds {
1011da177e4SLinus Torvalds 	del_timer(&ax25->idletimer);
1021da177e4SLinus Torvalds }
1031da177e4SLinus Torvalds 
1041da177e4SLinus Torvalds int ax25_t1timer_running(ax25_cb *ax25)
1051da177e4SLinus Torvalds {
1061da177e4SLinus Torvalds 	return timer_pending(&ax25->t1timer);
1071da177e4SLinus Torvalds }
1081da177e4SLinus Torvalds 
1091da177e4SLinus Torvalds unsigned long ax25_display_timer(struct timer_list *timer)
1101da177e4SLinus Torvalds {
111*df1c9414SFrancois Romieu 	long delta = timer->expires - jiffies;
112*df1c9414SFrancois Romieu 
1131da177e4SLinus Torvalds 	if (!timer_pending(timer))
1141da177e4SLinus Torvalds 		return 0;
1151da177e4SLinus Torvalds 
116*df1c9414SFrancois Romieu 	return max(0L, delta);
1171da177e4SLinus Torvalds }
1181da177e4SLinus Torvalds 
11970868eacSRalf Baechle EXPORT_SYMBOL(ax25_display_timer);
12070868eacSRalf Baechle 
1218dbd05ffSKees Cook static void ax25_heartbeat_expiry(struct timer_list *t)
1221da177e4SLinus Torvalds {
1231da177e4SLinus Torvalds 	int proto = AX25_PROTO_STD_SIMPLEX;
1248dbd05ffSKees Cook 	ax25_cb *ax25 = from_timer(ax25, t, timer);
1251da177e4SLinus Torvalds 
1261da177e4SLinus Torvalds 	if (ax25->ax25_dev)
1271da177e4SLinus Torvalds 		proto = ax25->ax25_dev->values[AX25_VALUES_PROTOCOL];
1281da177e4SLinus Torvalds 
1291da177e4SLinus Torvalds 	switch (proto) {
1301da177e4SLinus Torvalds 	case AX25_PROTO_STD_SIMPLEX:
1311da177e4SLinus Torvalds 	case AX25_PROTO_STD_DUPLEX:
1321da177e4SLinus Torvalds 		ax25_std_heartbeat_expiry(ax25);
1331da177e4SLinus Torvalds 		break;
1341da177e4SLinus Torvalds 
1351da177e4SLinus Torvalds #ifdef CONFIG_AX25_DAMA_SLAVE
1361da177e4SLinus Torvalds 	case AX25_PROTO_DAMA_SLAVE:
1371da177e4SLinus Torvalds 		if (ax25->ax25_dev->dama.slave)
1381da177e4SLinus Torvalds 			ax25_ds_heartbeat_expiry(ax25);
1391da177e4SLinus Torvalds 		else
1401da177e4SLinus Torvalds 			ax25_std_heartbeat_expiry(ax25);
1411da177e4SLinus Torvalds 		break;
1421da177e4SLinus Torvalds #endif
1431da177e4SLinus Torvalds 	}
1441da177e4SLinus Torvalds }
1451da177e4SLinus Torvalds 
1468dbd05ffSKees Cook static void ax25_t1timer_expiry(struct timer_list *t)
1471da177e4SLinus Torvalds {
1488dbd05ffSKees Cook 	ax25_cb *ax25 = from_timer(ax25, t, t1timer);
1491da177e4SLinus Torvalds 
1501da177e4SLinus Torvalds 	switch (ax25->ax25_dev->values[AX25_VALUES_PROTOCOL]) {
1511da177e4SLinus Torvalds 	case AX25_PROTO_STD_SIMPLEX:
1521da177e4SLinus Torvalds 	case AX25_PROTO_STD_DUPLEX:
1531da177e4SLinus Torvalds 		ax25_std_t1timer_expiry(ax25);
1541da177e4SLinus Torvalds 		break;
1551da177e4SLinus Torvalds 
1561da177e4SLinus Torvalds #ifdef CONFIG_AX25_DAMA_SLAVE
1571da177e4SLinus Torvalds 	case AX25_PROTO_DAMA_SLAVE:
1581da177e4SLinus Torvalds 		if (!ax25->ax25_dev->dama.slave)
1591da177e4SLinus Torvalds 			ax25_std_t1timer_expiry(ax25);
1601da177e4SLinus Torvalds 		break;
1611da177e4SLinus Torvalds #endif
1621da177e4SLinus Torvalds 	}
1631da177e4SLinus Torvalds }
1641da177e4SLinus Torvalds 
1658dbd05ffSKees Cook static void ax25_t2timer_expiry(struct timer_list *t)
1661da177e4SLinus Torvalds {
1678dbd05ffSKees Cook 	ax25_cb *ax25 = from_timer(ax25, t, t2timer);
1681da177e4SLinus Torvalds 
1691da177e4SLinus Torvalds 	switch (ax25->ax25_dev->values[AX25_VALUES_PROTOCOL]) {
1701da177e4SLinus Torvalds 	case AX25_PROTO_STD_SIMPLEX:
1711da177e4SLinus Torvalds 	case AX25_PROTO_STD_DUPLEX:
1721da177e4SLinus Torvalds 		ax25_std_t2timer_expiry(ax25);
1731da177e4SLinus Torvalds 		break;
1741da177e4SLinus Torvalds 
1751da177e4SLinus Torvalds #ifdef CONFIG_AX25_DAMA_SLAVE
1761da177e4SLinus Torvalds 	case AX25_PROTO_DAMA_SLAVE:
1771da177e4SLinus Torvalds 		if (!ax25->ax25_dev->dama.slave)
1781da177e4SLinus Torvalds 			ax25_std_t2timer_expiry(ax25);
1791da177e4SLinus Torvalds 		break;
1801da177e4SLinus Torvalds #endif
1811da177e4SLinus Torvalds 	}
1821da177e4SLinus Torvalds }
1831da177e4SLinus Torvalds 
1848dbd05ffSKees Cook static void ax25_t3timer_expiry(struct timer_list *t)
1851da177e4SLinus Torvalds {
1868dbd05ffSKees Cook 	ax25_cb *ax25 = from_timer(ax25, t, t3timer);
1871da177e4SLinus Torvalds 
1881da177e4SLinus Torvalds 	switch (ax25->ax25_dev->values[AX25_VALUES_PROTOCOL]) {
1891da177e4SLinus Torvalds 	case AX25_PROTO_STD_SIMPLEX:
1901da177e4SLinus Torvalds 	case AX25_PROTO_STD_DUPLEX:
1911da177e4SLinus Torvalds 		ax25_std_t3timer_expiry(ax25);
1921da177e4SLinus Torvalds 		break;
1931da177e4SLinus Torvalds 
1941da177e4SLinus Torvalds #ifdef CONFIG_AX25_DAMA_SLAVE
1951da177e4SLinus Torvalds 	case AX25_PROTO_DAMA_SLAVE:
1961da177e4SLinus Torvalds 		if (ax25->ax25_dev->dama.slave)
1971da177e4SLinus Torvalds 			ax25_ds_t3timer_expiry(ax25);
1981da177e4SLinus Torvalds 		else
1991da177e4SLinus Torvalds 			ax25_std_t3timer_expiry(ax25);
2001da177e4SLinus Torvalds 		break;
2011da177e4SLinus Torvalds #endif
2021da177e4SLinus Torvalds 	}
2031da177e4SLinus Torvalds }
2041da177e4SLinus Torvalds 
2058dbd05ffSKees Cook static void ax25_idletimer_expiry(struct timer_list *t)
2061da177e4SLinus Torvalds {
2078dbd05ffSKees Cook 	ax25_cb *ax25 = from_timer(ax25, t, idletimer);
2081da177e4SLinus Torvalds 
2091da177e4SLinus Torvalds 	switch (ax25->ax25_dev->values[AX25_VALUES_PROTOCOL]) {
2101da177e4SLinus Torvalds 	case AX25_PROTO_STD_SIMPLEX:
2111da177e4SLinus Torvalds 	case AX25_PROTO_STD_DUPLEX:
2121da177e4SLinus Torvalds 		ax25_std_idletimer_expiry(ax25);
2131da177e4SLinus Torvalds 		break;
2141da177e4SLinus Torvalds 
2151da177e4SLinus Torvalds #ifdef CONFIG_AX25_DAMA_SLAVE
2161da177e4SLinus Torvalds 	case AX25_PROTO_DAMA_SLAVE:
2171da177e4SLinus Torvalds 		if (ax25->ax25_dev->dama.slave)
2181da177e4SLinus Torvalds 			ax25_ds_idletimer_expiry(ax25);
2191da177e4SLinus Torvalds 		else
2201da177e4SLinus Torvalds 			ax25_std_idletimer_expiry(ax25);
2211da177e4SLinus Torvalds 		break;
2221da177e4SLinus Torvalds #endif
2231da177e4SLinus Torvalds 	}
2241da177e4SLinus Torvalds }
225