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) Jonathan Naylor G4KLX (g4klx@g4klx.demon.co.uk) |
| 5 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6 | #include <linux/errno.h> |
| 7 | #include <linux/types.h> |
| 8 | #include <linux/socket.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 9 | #include <linux/slab.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 10 | #include <linux/in.h> |
| 11 | #include <linux/kernel.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 12 | #include <linux/timer.h> |
| 13 | #include <linux/string.h> |
| 14 | #include <linux/sockios.h> |
| 15 | #include <linux/net.h> |
| 16 | #include <linux/spinlock.h> |
| 17 | #include <net/ax25.h> |
| 18 | #include <linux/inet.h> |
| 19 | #include <linux/netdevice.h> |
| 20 | #include <linux/if_arp.h> |
| 21 | #include <linux/skbuff.h> |
| 22 | #include <net/sock.h> |
Linus Torvalds | 7c0f6ba | 2016-12-24 11:46:01 -0800 | [diff] [blame] | 23 | #include <linux/uaccess.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 24 | #include <linux/fcntl.h> |
| 25 | #include <linux/mm.h> |
| 26 | #include <linux/interrupt.h> |
| 27 | #include <linux/init.h> |
| 28 | |
| 29 | ax25_dev *ax25_dev_list; |
| 30 | DEFINE_SPINLOCK(ax25_dev_lock); |
| 31 | |
| 32 | ax25_dev *ax25_addr_ax25dev(ax25_address *addr) |
| 33 | { |
| 34 | ax25_dev *ax25_dev, *res = NULL; |
| 35 | |
| 36 | spin_lock_bh(&ax25_dev_lock); |
| 37 | for (ax25_dev = ax25_dev_list; ax25_dev != NULL; ax25_dev = ax25_dev->next) |
Jakub Kicinski | c045ad2 | 2021-10-12 08:58:35 -0700 | [diff] [blame] | 38 | if (ax25cmp(addr, (const ax25_address *)ax25_dev->dev->dev_addr) == 0) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 39 | res = ax25_dev; |
Duoming Zhou | d01ffb9 | 2022-01-28 12:47:16 +0800 | [diff] [blame] | 40 | ax25_dev_hold(ax25_dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 41 | } |
| 42 | spin_unlock_bh(&ax25_dev_lock); |
| 43 | |
| 44 | return res; |
| 45 | } |
| 46 | |
| 47 | /* |
| 48 | * This is called when an interface is brought up. These are |
| 49 | * reasonable defaults. |
| 50 | */ |
| 51 | void ax25_dev_device_up(struct net_device *dev) |
| 52 | { |
| 53 | ax25_dev *ax25_dev; |
| 54 | |
Ralf Baechle | 1b30dd3 | 2006-07-09 12:14:22 -0700 | [diff] [blame] | 55 | if ((ax25_dev = kzalloc(sizeof(*ax25_dev), GFP_ATOMIC)) == NULL) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 56 | printk(KERN_ERR "AX.25: ax25_dev_device_up - out of memory\n"); |
| 57 | return; |
| 58 | } |
| 59 | |
Duoming Zhou | d01ffb9 | 2022-01-28 12:47:16 +0800 | [diff] [blame] | 60 | refcount_set(&ax25_dev->refcount, 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 61 | dev->ax25_ptr = ax25_dev; |
| 62 | ax25_dev->dev = dev; |
Eric Dumazet | 66ce07f | 2021-12-06 17:30:33 -0800 | [diff] [blame] | 63 | dev_hold_track(dev, &ax25_dev->dev_tracker, GFP_ATOMIC); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 64 | ax25_dev->forward = NULL; |
| 65 | |
| 66 | ax25_dev->values[AX25_VALUES_IPDEFMODE] = AX25_DEF_IPDEFMODE; |
| 67 | ax25_dev->values[AX25_VALUES_AXDEFMODE] = AX25_DEF_AXDEFMODE; |
| 68 | ax25_dev->values[AX25_VALUES_BACKOFF] = AX25_DEF_BACKOFF; |
| 69 | ax25_dev->values[AX25_VALUES_CONMODE] = AX25_DEF_CONMODE; |
| 70 | ax25_dev->values[AX25_VALUES_WINDOW] = AX25_DEF_WINDOW; |
| 71 | ax25_dev->values[AX25_VALUES_EWINDOW] = AX25_DEF_EWINDOW; |
| 72 | ax25_dev->values[AX25_VALUES_T1] = AX25_DEF_T1; |
| 73 | ax25_dev->values[AX25_VALUES_T2] = AX25_DEF_T2; |
| 74 | ax25_dev->values[AX25_VALUES_T3] = AX25_DEF_T3; |
| 75 | ax25_dev->values[AX25_VALUES_IDLE] = AX25_DEF_IDLE; |
| 76 | ax25_dev->values[AX25_VALUES_N2] = AX25_DEF_N2; |
| 77 | ax25_dev->values[AX25_VALUES_PACLEN] = AX25_DEF_PACLEN; |
| 78 | ax25_dev->values[AX25_VALUES_PROTOCOL] = AX25_DEF_PROTOCOL; |
| 79 | ax25_dev->values[AX25_VALUES_DS_TIMEOUT]= AX25_DEF_DS_TIMEOUT; |
| 80 | |
| 81 | #if defined(CONFIG_AX25_DAMA_SLAVE) || defined(CONFIG_AX25_DAMA_MASTER) |
Jarek Poplawski | e848b58 | 2008-02-11 21:38:32 -0800 | [diff] [blame] | 82 | ax25_ds_setup_timer(ax25_dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 83 | #endif |
| 84 | |
| 85 | spin_lock_bh(&ax25_dev_lock); |
| 86 | ax25_dev->next = ax25_dev_list; |
| 87 | ax25_dev_list = ax25_dev; |
| 88 | spin_unlock_bh(&ax25_dev_lock); |
Duoming Zhou | 87563a0 | 2022-02-03 23:08:11 +0800 | [diff] [blame] | 89 | ax25_dev_hold(ax25_dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 90 | |
Eric W. Biederman | 0ca7a4c | 2012-04-19 13:34:18 +0000 | [diff] [blame] | 91 | ax25_register_dev_sysctl(ax25_dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 92 | } |
| 93 | |
| 94 | void ax25_dev_device_down(struct net_device *dev) |
| 95 | { |
| 96 | ax25_dev *s, *ax25_dev; |
| 97 | |
| 98 | if ((ax25_dev = ax25_dev_ax25dev(dev)) == NULL) |
| 99 | return; |
| 100 | |
Eric W. Biederman | 0ca7a4c | 2012-04-19 13:34:18 +0000 | [diff] [blame] | 101 | ax25_unregister_dev_sysctl(ax25_dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 102 | |
| 103 | spin_lock_bh(&ax25_dev_lock); |
| 104 | |
| 105 | #ifdef CONFIG_AX25_DAMA_SLAVE |
| 106 | ax25_ds_del_timer(ax25_dev); |
| 107 | #endif |
| 108 | |
| 109 | /* |
| 110 | * Remove any packet forwarding that points to this device. |
| 111 | */ |
| 112 | for (s = ax25_dev_list; s != NULL; s = s->next) |
| 113 | if (s->forward == dev) |
| 114 | s->forward = NULL; |
| 115 | |
| 116 | if ((s = ax25_dev_list) == ax25_dev) { |
| 117 | ax25_dev_list = s->next; |
| 118 | spin_unlock_bh(&ax25_dev_lock); |
Duoming Zhou | 87563a0 | 2022-02-03 23:08:11 +0800 | [diff] [blame] | 119 | ax25_dev_put(ax25_dev); |
Cong Wang | c433570 | 2018-12-29 13:56:36 -0800 | [diff] [blame] | 120 | dev->ax25_ptr = NULL; |
Eric Dumazet | 66ce07f | 2021-12-06 17:30:33 -0800 | [diff] [blame] | 121 | dev_put_track(dev, &ax25_dev->dev_tracker); |
Duoming Zhou | d01ffb9 | 2022-01-28 12:47:16 +0800 | [diff] [blame] | 122 | ax25_dev_put(ax25_dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 123 | return; |
| 124 | } |
| 125 | |
| 126 | while (s != NULL && s->next != NULL) { |
| 127 | if (s->next == ax25_dev) { |
| 128 | s->next = ax25_dev->next; |
| 129 | spin_unlock_bh(&ax25_dev_lock); |
Duoming Zhou | 87563a0 | 2022-02-03 23:08:11 +0800 | [diff] [blame] | 130 | ax25_dev_put(ax25_dev); |
Cong Wang | c433570 | 2018-12-29 13:56:36 -0800 | [diff] [blame] | 131 | dev->ax25_ptr = NULL; |
Eric Dumazet | 66ce07f | 2021-12-06 17:30:33 -0800 | [diff] [blame] | 132 | dev_put_track(dev, &ax25_dev->dev_tracker); |
Duoming Zhou | d01ffb9 | 2022-01-28 12:47:16 +0800 | [diff] [blame] | 133 | ax25_dev_put(ax25_dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 134 | return; |
| 135 | } |
| 136 | |
| 137 | s = s->next; |
| 138 | } |
| 139 | spin_unlock_bh(&ax25_dev_lock); |
| 140 | dev->ax25_ptr = NULL; |
Duoming Zhou | d01ffb9 | 2022-01-28 12:47:16 +0800 | [diff] [blame] | 141 | ax25_dev_put(ax25_dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 142 | } |
| 143 | |
| 144 | int ax25_fwd_ioctl(unsigned int cmd, struct ax25_fwd_struct *fwd) |
| 145 | { |
| 146 | ax25_dev *ax25_dev, *fwd_dev; |
| 147 | |
| 148 | if ((ax25_dev = ax25_addr_ax25dev(&fwd->port_from)) == NULL) |
| 149 | return -EINVAL; |
| 150 | |
| 151 | switch (cmd) { |
| 152 | case SIOCAX25ADDFWD: |
Duoming Zhou | 87563a0 | 2022-02-03 23:08:11 +0800 | [diff] [blame] | 153 | fwd_dev = ax25_addr_ax25dev(&fwd->port_to); |
| 154 | if (!fwd_dev) { |
| 155 | ax25_dev_put(ax25_dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 156 | return -EINVAL; |
Duoming Zhou | 87563a0 | 2022-02-03 23:08:11 +0800 | [diff] [blame] | 157 | } |
| 158 | if (ax25_dev->forward) { |
| 159 | ax25_dev_put(fwd_dev); |
| 160 | ax25_dev_put(ax25_dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 161 | return -EINVAL; |
Duoming Zhou | 87563a0 | 2022-02-03 23:08:11 +0800 | [diff] [blame] | 162 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 163 | ax25_dev->forward = fwd_dev->dev; |
Duoming Zhou | d01ffb9 | 2022-01-28 12:47:16 +0800 | [diff] [blame] | 164 | ax25_dev_put(fwd_dev); |
Duoming Zhou | 87563a0 | 2022-02-03 23:08:11 +0800 | [diff] [blame] | 165 | ax25_dev_put(ax25_dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 166 | break; |
| 167 | |
| 168 | case SIOCAX25DELFWD: |
Duoming Zhou | 87563a0 | 2022-02-03 23:08:11 +0800 | [diff] [blame] | 169 | if (!ax25_dev->forward) { |
| 170 | ax25_dev_put(ax25_dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 171 | return -EINVAL; |
Duoming Zhou | 87563a0 | 2022-02-03 23:08:11 +0800 | [diff] [blame] | 172 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 173 | ax25_dev->forward = NULL; |
Duoming Zhou | 87563a0 | 2022-02-03 23:08:11 +0800 | [diff] [blame] | 174 | ax25_dev_put(ax25_dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 175 | break; |
| 176 | |
| 177 | default: |
Duoming Zhou | 87563a0 | 2022-02-03 23:08:11 +0800 | [diff] [blame] | 178 | ax25_dev_put(ax25_dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 179 | return -EINVAL; |
| 180 | } |
| 181 | |
| 182 | return 0; |
| 183 | } |
| 184 | |
| 185 | struct net_device *ax25_fwd_dev(struct net_device *dev) |
| 186 | { |
| 187 | ax25_dev *ax25_dev; |
| 188 | |
| 189 | if ((ax25_dev = ax25_dev_ax25dev(dev)) == NULL) |
| 190 | return dev; |
| 191 | |
| 192 | if (ax25_dev->forward == NULL) |
| 193 | return dev; |
| 194 | |
| 195 | return ax25_dev->forward; |
| 196 | } |
| 197 | |
| 198 | /* |
| 199 | * Free all memory associated with device structures. |
| 200 | */ |
| 201 | void __exit ax25_dev_free(void) |
| 202 | { |
| 203 | ax25_dev *s, *ax25_dev; |
| 204 | |
| 205 | spin_lock_bh(&ax25_dev_lock); |
| 206 | ax25_dev = ax25_dev_list; |
| 207 | while (ax25_dev != NULL) { |
| 208 | s = ax25_dev; |
Eric Dumazet | 66ce07f | 2021-12-06 17:30:33 -0800 | [diff] [blame] | 209 | dev_put_track(ax25_dev->dev, &ax25_dev->dev_tracker); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 210 | ax25_dev = ax25_dev->next; |
| 211 | kfree(s); |
| 212 | } |
| 213 | ax25_dev_list = NULL; |
| 214 | spin_unlock_bh(&ax25_dev_lock); |
| 215 | } |