1*2874c5fdSThomas 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 { 1111da177e4SLinus Torvalds if (!timer_pending(timer)) 1121da177e4SLinus Torvalds return 0; 1131da177e4SLinus Torvalds 1141da177e4SLinus Torvalds return timer->expires - jiffies; 1151da177e4SLinus Torvalds } 1161da177e4SLinus Torvalds 11770868eacSRalf Baechle EXPORT_SYMBOL(ax25_display_timer); 11870868eacSRalf Baechle 1198dbd05ffSKees Cook static void ax25_heartbeat_expiry(struct timer_list *t) 1201da177e4SLinus Torvalds { 1211da177e4SLinus Torvalds int proto = AX25_PROTO_STD_SIMPLEX; 1228dbd05ffSKees Cook ax25_cb *ax25 = from_timer(ax25, t, timer); 1231da177e4SLinus Torvalds 1241da177e4SLinus Torvalds if (ax25->ax25_dev) 1251da177e4SLinus Torvalds proto = ax25->ax25_dev->values[AX25_VALUES_PROTOCOL]; 1261da177e4SLinus Torvalds 1271da177e4SLinus Torvalds switch (proto) { 1281da177e4SLinus Torvalds case AX25_PROTO_STD_SIMPLEX: 1291da177e4SLinus Torvalds case AX25_PROTO_STD_DUPLEX: 1301da177e4SLinus Torvalds ax25_std_heartbeat_expiry(ax25); 1311da177e4SLinus Torvalds break; 1321da177e4SLinus Torvalds 1331da177e4SLinus Torvalds #ifdef CONFIG_AX25_DAMA_SLAVE 1341da177e4SLinus Torvalds case AX25_PROTO_DAMA_SLAVE: 1351da177e4SLinus Torvalds if (ax25->ax25_dev->dama.slave) 1361da177e4SLinus Torvalds ax25_ds_heartbeat_expiry(ax25); 1371da177e4SLinus Torvalds else 1381da177e4SLinus Torvalds ax25_std_heartbeat_expiry(ax25); 1391da177e4SLinus Torvalds break; 1401da177e4SLinus Torvalds #endif 1411da177e4SLinus Torvalds } 1421da177e4SLinus Torvalds } 1431da177e4SLinus Torvalds 1448dbd05ffSKees Cook static void ax25_t1timer_expiry(struct timer_list *t) 1451da177e4SLinus Torvalds { 1468dbd05ffSKees Cook ax25_cb *ax25 = from_timer(ax25, t, t1timer); 1471da177e4SLinus Torvalds 1481da177e4SLinus Torvalds switch (ax25->ax25_dev->values[AX25_VALUES_PROTOCOL]) { 1491da177e4SLinus Torvalds case AX25_PROTO_STD_SIMPLEX: 1501da177e4SLinus Torvalds case AX25_PROTO_STD_DUPLEX: 1511da177e4SLinus Torvalds ax25_std_t1timer_expiry(ax25); 1521da177e4SLinus Torvalds break; 1531da177e4SLinus Torvalds 1541da177e4SLinus Torvalds #ifdef CONFIG_AX25_DAMA_SLAVE 1551da177e4SLinus Torvalds case AX25_PROTO_DAMA_SLAVE: 1561da177e4SLinus Torvalds if (!ax25->ax25_dev->dama.slave) 1571da177e4SLinus Torvalds ax25_std_t1timer_expiry(ax25); 1581da177e4SLinus Torvalds break; 1591da177e4SLinus Torvalds #endif 1601da177e4SLinus Torvalds } 1611da177e4SLinus Torvalds } 1621da177e4SLinus Torvalds 1638dbd05ffSKees Cook static void ax25_t2timer_expiry(struct timer_list *t) 1641da177e4SLinus Torvalds { 1658dbd05ffSKees Cook ax25_cb *ax25 = from_timer(ax25, t, t2timer); 1661da177e4SLinus Torvalds 1671da177e4SLinus Torvalds switch (ax25->ax25_dev->values[AX25_VALUES_PROTOCOL]) { 1681da177e4SLinus Torvalds case AX25_PROTO_STD_SIMPLEX: 1691da177e4SLinus Torvalds case AX25_PROTO_STD_DUPLEX: 1701da177e4SLinus Torvalds ax25_std_t2timer_expiry(ax25); 1711da177e4SLinus Torvalds break; 1721da177e4SLinus Torvalds 1731da177e4SLinus Torvalds #ifdef CONFIG_AX25_DAMA_SLAVE 1741da177e4SLinus Torvalds case AX25_PROTO_DAMA_SLAVE: 1751da177e4SLinus Torvalds if (!ax25->ax25_dev->dama.slave) 1761da177e4SLinus Torvalds ax25_std_t2timer_expiry(ax25); 1771da177e4SLinus Torvalds break; 1781da177e4SLinus Torvalds #endif 1791da177e4SLinus Torvalds } 1801da177e4SLinus Torvalds } 1811da177e4SLinus Torvalds 1828dbd05ffSKees Cook static void ax25_t3timer_expiry(struct timer_list *t) 1831da177e4SLinus Torvalds { 1848dbd05ffSKees Cook ax25_cb *ax25 = from_timer(ax25, t, t3timer); 1851da177e4SLinus Torvalds 1861da177e4SLinus Torvalds switch (ax25->ax25_dev->values[AX25_VALUES_PROTOCOL]) { 1871da177e4SLinus Torvalds case AX25_PROTO_STD_SIMPLEX: 1881da177e4SLinus Torvalds case AX25_PROTO_STD_DUPLEX: 1891da177e4SLinus Torvalds ax25_std_t3timer_expiry(ax25); 1901da177e4SLinus Torvalds break; 1911da177e4SLinus Torvalds 1921da177e4SLinus Torvalds #ifdef CONFIG_AX25_DAMA_SLAVE 1931da177e4SLinus Torvalds case AX25_PROTO_DAMA_SLAVE: 1941da177e4SLinus Torvalds if (ax25->ax25_dev->dama.slave) 1951da177e4SLinus Torvalds ax25_ds_t3timer_expiry(ax25); 1961da177e4SLinus Torvalds else 1971da177e4SLinus Torvalds ax25_std_t3timer_expiry(ax25); 1981da177e4SLinus Torvalds break; 1991da177e4SLinus Torvalds #endif 2001da177e4SLinus Torvalds } 2011da177e4SLinus Torvalds } 2021da177e4SLinus Torvalds 2038dbd05ffSKees Cook static void ax25_idletimer_expiry(struct timer_list *t) 2041da177e4SLinus Torvalds { 2058dbd05ffSKees Cook ax25_cb *ax25 = from_timer(ax25, t, idletimer); 2061da177e4SLinus Torvalds 2071da177e4SLinus Torvalds switch (ax25->ax25_dev->values[AX25_VALUES_PROTOCOL]) { 2081da177e4SLinus Torvalds case AX25_PROTO_STD_SIMPLEX: 2091da177e4SLinus Torvalds case AX25_PROTO_STD_DUPLEX: 2101da177e4SLinus Torvalds ax25_std_idletimer_expiry(ax25); 2111da177e4SLinus Torvalds break; 2121da177e4SLinus Torvalds 2131da177e4SLinus Torvalds #ifdef CONFIG_AX25_DAMA_SLAVE 2141da177e4SLinus Torvalds case AX25_PROTO_DAMA_SLAVE: 2151da177e4SLinus Torvalds if (ax25->ax25_dev->dama.slave) 2161da177e4SLinus Torvalds ax25_ds_idletimer_expiry(ax25); 2171da177e4SLinus Torvalds else 2181da177e4SLinus Torvalds ax25_std_idletimer_expiry(ax25); 2191da177e4SLinus Torvalds break; 2201da177e4SLinus Torvalds #endif 2211da177e4SLinus Torvalds } 2221da177e4SLinus Torvalds } 223