Thomas Gleixner | 2874c5f | 2019-05-27 08:55:01 +0200 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0-or-later |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3 | * |
| 4 | * Copyright (C) Alan Cox GW4PTS (alan@lxorguk.ukuu.org.uk) |
| 5 | * Copyright (C) Jonathan Naylor G4KLX (g4klx@g4klx.demon.co.uk) |
| 6 | * Copyright (C) Tomi Manninen OH2BNS (oh2bns@sral.fi) |
| 7 | * Copyright (C) Darryl Miles G7LED (dlm@g7led.demon.co.uk) |
| 8 | * Copyright (C) Joerg Reuter DL1BKE (jreuter@yaina.de) |
| 9 | * Copyright (C) Frederic Rible F1OAT (frible@teaser.fr) |
| 10 | * Copyright (C) 2002 Ralf Baechle DO1GRB (ralf@gnu.org) |
| 11 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 12 | #include <linux/errno.h> |
| 13 | #include <linux/types.h> |
| 14 | #include <linux/socket.h> |
| 15 | #include <linux/in.h> |
| 16 | #include <linux/kernel.h> |
Ralf Baechle | 70868ea | 2006-05-03 23:25:17 -0700 | [diff] [blame] | 17 | #include <linux/module.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 18 | #include <linux/jiffies.h> |
| 19 | #include <linux/timer.h> |
| 20 | #include <linux/string.h> |
| 21 | #include <linux/sockios.h> |
| 22 | #include <linux/net.h> |
| 23 | #include <net/ax25.h> |
| 24 | #include <linux/inet.h> |
| 25 | #include <linux/netdevice.h> |
| 26 | #include <linux/skbuff.h> |
| 27 | #include <net/sock.h> |
Linus Torvalds | 7c0f6ba | 2016-12-24 11:46:01 -0800 | [diff] [blame] | 28 | #include <linux/uaccess.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 29 | #include <linux/fcntl.h> |
| 30 | #include <linux/mm.h> |
| 31 | #include <linux/interrupt.h> |
| 32 | |
Kees Cook | 8dbd05f | 2017-10-24 01:45:39 -0700 | [diff] [blame] | 33 | static void ax25_heartbeat_expiry(struct timer_list *); |
| 34 | static void ax25_t1timer_expiry(struct timer_list *); |
| 35 | static void ax25_t2timer_expiry(struct timer_list *); |
| 36 | static void ax25_t3timer_expiry(struct timer_list *); |
| 37 | static void ax25_idletimer_expiry(struct timer_list *); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 38 | |
Jarek Poplawski | 21fab4a | 2008-02-11 21:36:39 -0800 | [diff] [blame] | 39 | void ax25_setup_timers(ax25_cb *ax25) |
| 40 | { |
Kees Cook | 8dbd05f | 2017-10-24 01:45:39 -0700 | [diff] [blame] | 41 | timer_setup(&ax25->timer, ax25_heartbeat_expiry, 0); |
| 42 | timer_setup(&ax25->t1timer, ax25_t1timer_expiry, 0); |
| 43 | timer_setup(&ax25->t2timer, ax25_t2timer_expiry, 0); |
| 44 | timer_setup(&ax25->t3timer, ax25_t3timer_expiry, 0); |
| 45 | timer_setup(&ax25->idletimer, ax25_idletimer_expiry, 0); |
Jarek Poplawski | 21fab4a | 2008-02-11 21:36:39 -0800 | [diff] [blame] | 46 | } |
| 47 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 48 | void ax25_start_heartbeat(ax25_cb *ax25) |
| 49 | { |
Jarek Poplawski | 21fab4a | 2008-02-11 21:36:39 -0800 | [diff] [blame] | 50 | mod_timer(&ax25->timer, jiffies + 5 * HZ); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 51 | } |
| 52 | |
| 53 | void ax25_start_t1timer(ax25_cb *ax25) |
| 54 | { |
Jarek Poplawski | 21fab4a | 2008-02-11 21:36:39 -0800 | [diff] [blame] | 55 | mod_timer(&ax25->t1timer, jiffies + ax25->t1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 56 | } |
| 57 | |
| 58 | void ax25_start_t2timer(ax25_cb *ax25) |
| 59 | { |
Jarek Poplawski | 21fab4a | 2008-02-11 21:36:39 -0800 | [diff] [blame] | 60 | mod_timer(&ax25->t2timer, jiffies + ax25->t2); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 61 | } |
| 62 | |
| 63 | void ax25_start_t3timer(ax25_cb *ax25) |
| 64 | { |
Jarek Poplawski | 21fab4a | 2008-02-11 21:36:39 -0800 | [diff] [blame] | 65 | if (ax25->t3 > 0) |
| 66 | mod_timer(&ax25->t3timer, jiffies + ax25->t3); |
| 67 | else |
| 68 | del_timer(&ax25->t3timer); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 69 | } |
| 70 | |
| 71 | void ax25_start_idletimer(ax25_cb *ax25) |
| 72 | { |
Jarek Poplawski | 21fab4a | 2008-02-11 21:36:39 -0800 | [diff] [blame] | 73 | if (ax25->idle > 0) |
| 74 | mod_timer(&ax25->idletimer, jiffies + ax25->idle); |
| 75 | else |
| 76 | del_timer(&ax25->idletimer); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 77 | } |
| 78 | |
| 79 | void ax25_stop_heartbeat(ax25_cb *ax25) |
| 80 | { |
| 81 | del_timer(&ax25->timer); |
| 82 | } |
| 83 | |
| 84 | void ax25_stop_t1timer(ax25_cb *ax25) |
| 85 | { |
| 86 | del_timer(&ax25->t1timer); |
| 87 | } |
| 88 | |
| 89 | void ax25_stop_t2timer(ax25_cb *ax25) |
| 90 | { |
| 91 | del_timer(&ax25->t2timer); |
| 92 | } |
| 93 | |
| 94 | void ax25_stop_t3timer(ax25_cb *ax25) |
| 95 | { |
| 96 | del_timer(&ax25->t3timer); |
| 97 | } |
| 98 | |
| 99 | void ax25_stop_idletimer(ax25_cb *ax25) |
| 100 | { |
| 101 | del_timer(&ax25->idletimer); |
| 102 | } |
| 103 | |
| 104 | int ax25_t1timer_running(ax25_cb *ax25) |
| 105 | { |
| 106 | return timer_pending(&ax25->t1timer); |
| 107 | } |
| 108 | |
| 109 | unsigned long ax25_display_timer(struct timer_list *timer) |
| 110 | { |
| 111 | if (!timer_pending(timer)) |
| 112 | return 0; |
| 113 | |
| 114 | return timer->expires - jiffies; |
| 115 | } |
| 116 | |
Ralf Baechle | 70868ea | 2006-05-03 23:25:17 -0700 | [diff] [blame] | 117 | EXPORT_SYMBOL(ax25_display_timer); |
| 118 | |
Kees Cook | 8dbd05f | 2017-10-24 01:45:39 -0700 | [diff] [blame] | 119 | static void ax25_heartbeat_expiry(struct timer_list *t) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 120 | { |
| 121 | int proto = AX25_PROTO_STD_SIMPLEX; |
Kees Cook | 8dbd05f | 2017-10-24 01:45:39 -0700 | [diff] [blame] | 122 | ax25_cb *ax25 = from_timer(ax25, t, timer); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 123 | |
| 124 | if (ax25->ax25_dev) |
| 125 | proto = ax25->ax25_dev->values[AX25_VALUES_PROTOCOL]; |
| 126 | |
| 127 | switch (proto) { |
| 128 | case AX25_PROTO_STD_SIMPLEX: |
| 129 | case AX25_PROTO_STD_DUPLEX: |
| 130 | ax25_std_heartbeat_expiry(ax25); |
| 131 | break; |
| 132 | |
| 133 | #ifdef CONFIG_AX25_DAMA_SLAVE |
| 134 | case AX25_PROTO_DAMA_SLAVE: |
| 135 | if (ax25->ax25_dev->dama.slave) |
| 136 | ax25_ds_heartbeat_expiry(ax25); |
| 137 | else |
| 138 | ax25_std_heartbeat_expiry(ax25); |
| 139 | break; |
| 140 | #endif |
| 141 | } |
| 142 | } |
| 143 | |
Kees Cook | 8dbd05f | 2017-10-24 01:45:39 -0700 | [diff] [blame] | 144 | static void ax25_t1timer_expiry(struct timer_list *t) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 145 | { |
Kees Cook | 8dbd05f | 2017-10-24 01:45:39 -0700 | [diff] [blame] | 146 | ax25_cb *ax25 = from_timer(ax25, t, t1timer); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 147 | |
| 148 | switch (ax25->ax25_dev->values[AX25_VALUES_PROTOCOL]) { |
| 149 | case AX25_PROTO_STD_SIMPLEX: |
| 150 | case AX25_PROTO_STD_DUPLEX: |
| 151 | ax25_std_t1timer_expiry(ax25); |
| 152 | break; |
| 153 | |
| 154 | #ifdef CONFIG_AX25_DAMA_SLAVE |
| 155 | case AX25_PROTO_DAMA_SLAVE: |
| 156 | if (!ax25->ax25_dev->dama.slave) |
| 157 | ax25_std_t1timer_expiry(ax25); |
| 158 | break; |
| 159 | #endif |
| 160 | } |
| 161 | } |
| 162 | |
Kees Cook | 8dbd05f | 2017-10-24 01:45:39 -0700 | [diff] [blame] | 163 | static void ax25_t2timer_expiry(struct timer_list *t) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 164 | { |
Kees Cook | 8dbd05f | 2017-10-24 01:45:39 -0700 | [diff] [blame] | 165 | ax25_cb *ax25 = from_timer(ax25, t, t2timer); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 166 | |
| 167 | switch (ax25->ax25_dev->values[AX25_VALUES_PROTOCOL]) { |
| 168 | case AX25_PROTO_STD_SIMPLEX: |
| 169 | case AX25_PROTO_STD_DUPLEX: |
| 170 | ax25_std_t2timer_expiry(ax25); |
| 171 | break; |
| 172 | |
| 173 | #ifdef CONFIG_AX25_DAMA_SLAVE |
| 174 | case AX25_PROTO_DAMA_SLAVE: |
| 175 | if (!ax25->ax25_dev->dama.slave) |
| 176 | ax25_std_t2timer_expiry(ax25); |
| 177 | break; |
| 178 | #endif |
| 179 | } |
| 180 | } |
| 181 | |
Kees Cook | 8dbd05f | 2017-10-24 01:45:39 -0700 | [diff] [blame] | 182 | static void ax25_t3timer_expiry(struct timer_list *t) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 183 | { |
Kees Cook | 8dbd05f | 2017-10-24 01:45:39 -0700 | [diff] [blame] | 184 | ax25_cb *ax25 = from_timer(ax25, t, t3timer); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 185 | |
| 186 | switch (ax25->ax25_dev->values[AX25_VALUES_PROTOCOL]) { |
| 187 | case AX25_PROTO_STD_SIMPLEX: |
| 188 | case AX25_PROTO_STD_DUPLEX: |
| 189 | ax25_std_t3timer_expiry(ax25); |
| 190 | break; |
| 191 | |
| 192 | #ifdef CONFIG_AX25_DAMA_SLAVE |
| 193 | case AX25_PROTO_DAMA_SLAVE: |
| 194 | if (ax25->ax25_dev->dama.slave) |
| 195 | ax25_ds_t3timer_expiry(ax25); |
| 196 | else |
| 197 | ax25_std_t3timer_expiry(ax25); |
| 198 | break; |
| 199 | #endif |
| 200 | } |
| 201 | } |
| 202 | |
Kees Cook | 8dbd05f | 2017-10-24 01:45:39 -0700 | [diff] [blame] | 203 | static void ax25_idletimer_expiry(struct timer_list *t) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 204 | { |
Kees Cook | 8dbd05f | 2017-10-24 01:45:39 -0700 | [diff] [blame] | 205 | ax25_cb *ax25 = from_timer(ax25, t, idletimer); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 206 | |
| 207 | switch (ax25->ax25_dev->values[AX25_VALUES_PROTOCOL]) { |
| 208 | case AX25_PROTO_STD_SIMPLEX: |
| 209 | case AX25_PROTO_STD_DUPLEX: |
| 210 | ax25_std_idletimer_expiry(ax25); |
| 211 | break; |
| 212 | |
| 213 | #ifdef CONFIG_AX25_DAMA_SLAVE |
| 214 | case AX25_PROTO_DAMA_SLAVE: |
| 215 | if (ax25->ax25_dev->dama.slave) |
| 216 | ax25_ds_idletimer_expiry(ax25); |
| 217 | else |
| 218 | ax25_std_idletimer_expiry(ax25); |
| 219 | break; |
| 220 | #endif |
| 221 | } |
| 222 | } |