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> |
| 9 | #include <linux/in.h> |
| 10 | #include <linux/kernel.h> |
Ralf Baechle | 70868ea | 2006-05-03 23:25:17 -0700 | [diff] [blame] | 11 | #include <linux/module.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 12 | #include <linux/spinlock.h> |
| 13 | #include <linux/timer.h> |
| 14 | #include <linux/string.h> |
| 15 | #include <linux/sockios.h> |
| 16 | #include <linux/net.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 17 | #include <linux/slab.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 18 | #include <net/ax25.h> |
| 19 | #include <linux/inet.h> |
| 20 | #include <linux/netdevice.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 | |
Ralf Baechle | 8d5cf59 | 2006-12-14 15:50:01 -0800 | [diff] [blame] | 28 | static struct ax25_protocol *protocol_list; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 29 | static DEFINE_RWLOCK(protocol_list_lock); |
| 30 | |
Ralf Baechle | a428271 | 2006-12-14 15:51:23 -0800 | [diff] [blame] | 31 | static HLIST_HEAD(ax25_linkfail_list); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 32 | static DEFINE_SPINLOCK(linkfail_lock); |
| 33 | |
| 34 | static struct listen_struct { |
| 35 | struct listen_struct *next; |
| 36 | ax25_address callsign; |
| 37 | struct net_device *dev; |
| 38 | } *listen_list = NULL; |
| 39 | static DEFINE_SPINLOCK(listen_lock); |
| 40 | |
Ralf Baechle | 8d5cf59 | 2006-12-14 15:50:01 -0800 | [diff] [blame] | 41 | /* |
| 42 | * Do not register the internal protocols AX25_P_TEXT, AX25_P_SEGMENT, |
| 43 | * AX25_P_IP or AX25_P_ARP ... |
| 44 | */ |
| 45 | void ax25_register_pid(struct ax25_protocol *ap) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 46 | { |
Ralf Baechle | 95ff9f4 | 2006-07-10 16:21:29 -0700 | [diff] [blame] | 47 | write_lock_bh(&protocol_list_lock); |
Ralf Baechle | 8d5cf59 | 2006-12-14 15:50:01 -0800 | [diff] [blame] | 48 | ap->next = protocol_list; |
| 49 | protocol_list = ap; |
Ralf Baechle | 95ff9f4 | 2006-07-10 16:21:29 -0700 | [diff] [blame] | 50 | write_unlock_bh(&protocol_list_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 51 | } |
| 52 | |
Ralf Baechle | 8d5cf59 | 2006-12-14 15:50:01 -0800 | [diff] [blame] | 53 | EXPORT_SYMBOL_GPL(ax25_register_pid); |
Ralf Baechle | 70868ea | 2006-05-03 23:25:17 -0700 | [diff] [blame] | 54 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 55 | void ax25_protocol_release(unsigned int pid) |
| 56 | { |
David S. Miller | d8f969e | 2011-04-17 00:46:45 -0700 | [diff] [blame] | 57 | struct ax25_protocol *protocol; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 58 | |
Ralf Baechle | 95ff9f4 | 2006-07-10 16:21:29 -0700 | [diff] [blame] | 59 | write_lock_bh(&protocol_list_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 60 | protocol = protocol_list; |
Ilpo Järvinen | 910d30b | 2009-02-06 23:47:14 -0800 | [diff] [blame] | 61 | if (protocol == NULL) |
| 62 | goto out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 63 | |
| 64 | if (protocol->pid == pid) { |
| 65 | protocol_list = protocol->next; |
Ilpo Järvinen | 910d30b | 2009-02-06 23:47:14 -0800 | [diff] [blame] | 66 | goto out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 67 | } |
| 68 | |
| 69 | while (protocol != NULL && protocol->next != NULL) { |
| 70 | if (protocol->next->pid == pid) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 71 | protocol->next = protocol->next->next; |
Ilpo Järvinen | 910d30b | 2009-02-06 23:47:14 -0800 | [diff] [blame] | 72 | goto out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 73 | } |
| 74 | |
| 75 | protocol = protocol->next; |
| 76 | } |
Ilpo Järvinen | 910d30b | 2009-02-06 23:47:14 -0800 | [diff] [blame] | 77 | out: |
Ralf Baechle | 95ff9f4 | 2006-07-10 16:21:29 -0700 | [diff] [blame] | 78 | write_unlock_bh(&protocol_list_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 79 | } |
| 80 | |
Ralf Baechle | 70868ea | 2006-05-03 23:25:17 -0700 | [diff] [blame] | 81 | EXPORT_SYMBOL(ax25_protocol_release); |
| 82 | |
Ralf Baechle | a428271 | 2006-12-14 15:51:23 -0800 | [diff] [blame] | 83 | void ax25_linkfail_register(struct ax25_linkfail *lf) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 84 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 85 | spin_lock_bh(&linkfail_lock); |
Ralf Baechle | a428271 | 2006-12-14 15:51:23 -0800 | [diff] [blame] | 86 | hlist_add_head(&lf->lf_node, &ax25_linkfail_list); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 87 | spin_unlock_bh(&linkfail_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 88 | } |
| 89 | |
Ralf Baechle | 70868ea | 2006-05-03 23:25:17 -0700 | [diff] [blame] | 90 | EXPORT_SYMBOL(ax25_linkfail_register); |
| 91 | |
Ralf Baechle | a428271 | 2006-12-14 15:51:23 -0800 | [diff] [blame] | 92 | void ax25_linkfail_release(struct ax25_linkfail *lf) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 93 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 94 | spin_lock_bh(&linkfail_lock); |
Ralf Baechle | a428271 | 2006-12-14 15:51:23 -0800 | [diff] [blame] | 95 | hlist_del_init(&lf->lf_node); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 96 | spin_unlock_bh(&linkfail_lock); |
| 97 | } |
| 98 | |
Ralf Baechle | 70868ea | 2006-05-03 23:25:17 -0700 | [diff] [blame] | 99 | EXPORT_SYMBOL(ax25_linkfail_release); |
| 100 | |
Jakub Kicinski | c045ad2 | 2021-10-12 08:58:35 -0700 | [diff] [blame] | 101 | int ax25_listen_register(const ax25_address *callsign, struct net_device *dev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 102 | { |
| 103 | struct listen_struct *listen; |
| 104 | |
| 105 | if (ax25_listen_mine(callsign, dev)) |
| 106 | return 0; |
| 107 | |
| 108 | if ((listen = kmalloc(sizeof(*listen), GFP_ATOMIC)) == NULL) |
Ralf Baechle | 81dcd16 | 2006-12-14 15:50:34 -0800 | [diff] [blame] | 109 | return -ENOMEM; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 110 | |
| 111 | listen->callsign = *callsign; |
| 112 | listen->dev = dev; |
| 113 | |
| 114 | spin_lock_bh(&listen_lock); |
| 115 | listen->next = listen_list; |
| 116 | listen_list = listen; |
| 117 | spin_unlock_bh(&listen_lock); |
| 118 | |
Ralf Baechle | 81dcd16 | 2006-12-14 15:50:34 -0800 | [diff] [blame] | 119 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 120 | } |
| 121 | |
Ralf Baechle | 70868ea | 2006-05-03 23:25:17 -0700 | [diff] [blame] | 122 | EXPORT_SYMBOL(ax25_listen_register); |
| 123 | |
Jakub Kicinski | c045ad2 | 2021-10-12 08:58:35 -0700 | [diff] [blame] | 124 | void ax25_listen_release(const ax25_address *callsign, struct net_device *dev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 125 | { |
| 126 | struct listen_struct *s, *listen; |
| 127 | |
| 128 | spin_lock_bh(&listen_lock); |
| 129 | listen = listen_list; |
| 130 | if (listen == NULL) { |
| 131 | spin_unlock_bh(&listen_lock); |
| 132 | return; |
| 133 | } |
| 134 | |
| 135 | if (ax25cmp(&listen->callsign, callsign) == 0 && listen->dev == dev) { |
| 136 | listen_list = listen->next; |
| 137 | spin_unlock_bh(&listen_lock); |
| 138 | kfree(listen); |
| 139 | return; |
| 140 | } |
| 141 | |
| 142 | while (listen != NULL && listen->next != NULL) { |
| 143 | if (ax25cmp(&listen->next->callsign, callsign) == 0 && listen->next->dev == dev) { |
| 144 | s = listen->next; |
| 145 | listen->next = listen->next->next; |
| 146 | spin_unlock_bh(&listen_lock); |
| 147 | kfree(s); |
| 148 | return; |
| 149 | } |
| 150 | |
| 151 | listen = listen->next; |
| 152 | } |
| 153 | spin_unlock_bh(&listen_lock); |
| 154 | } |
| 155 | |
Ralf Baechle | 70868ea | 2006-05-03 23:25:17 -0700 | [diff] [blame] | 156 | EXPORT_SYMBOL(ax25_listen_release); |
| 157 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 158 | int (*ax25_protocol_function(unsigned int pid))(struct sk_buff *, ax25_cb *) |
| 159 | { |
| 160 | int (*res)(struct sk_buff *, ax25_cb *) = NULL; |
Ralf Baechle | 8d5cf59 | 2006-12-14 15:50:01 -0800 | [diff] [blame] | 161 | struct ax25_protocol *protocol; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 162 | |
| 163 | read_lock(&protocol_list_lock); |
| 164 | for (protocol = protocol_list; protocol != NULL; protocol = protocol->next) |
| 165 | if (protocol->pid == pid) { |
| 166 | res = protocol->func; |
| 167 | break; |
| 168 | } |
| 169 | read_unlock(&protocol_list_lock); |
| 170 | |
| 171 | return res; |
| 172 | } |
| 173 | |
Jakub Kicinski | c045ad2 | 2021-10-12 08:58:35 -0700 | [diff] [blame] | 174 | int ax25_listen_mine(const ax25_address *callsign, struct net_device *dev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 175 | { |
| 176 | struct listen_struct *listen; |
| 177 | |
| 178 | spin_lock_bh(&listen_lock); |
| 179 | for (listen = listen_list; listen != NULL; listen = listen->next) |
Ralf Baechle | 81dcd16 | 2006-12-14 15:50:34 -0800 | [diff] [blame] | 180 | if (ax25cmp(&listen->callsign, callsign) == 0 && |
| 181 | (listen->dev == dev || listen->dev == NULL)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 182 | spin_unlock_bh(&listen_lock); |
| 183 | return 1; |
| 184 | } |
| 185 | spin_unlock_bh(&listen_lock); |
| 186 | |
| 187 | return 0; |
| 188 | } |
| 189 | |
| 190 | void ax25_link_failed(ax25_cb *ax25, int reason) |
| 191 | { |
Ralf Baechle | a428271 | 2006-12-14 15:51:23 -0800 | [diff] [blame] | 192 | struct ax25_linkfail *lf; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 193 | |
| 194 | spin_lock_bh(&linkfail_lock); |
Sasha Levin | b67bfe0 | 2013-02-27 17:06:00 -0800 | [diff] [blame] | 195 | hlist_for_each_entry(lf, &ax25_linkfail_list, lf_node) |
Ralf Baechle | a428271 | 2006-12-14 15:51:23 -0800 | [diff] [blame] | 196 | lf->func(ax25, reason); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 197 | spin_unlock_bh(&linkfail_lock); |
| 198 | } |
| 199 | |
| 200 | int ax25_protocol_is_registered(unsigned int pid) |
| 201 | { |
Ralf Baechle | 8d5cf59 | 2006-12-14 15:50:01 -0800 | [diff] [blame] | 202 | struct ax25_protocol *protocol; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 203 | int res = 0; |
| 204 | |
Ralf Baechle | 95ff9f4 | 2006-07-10 16:21:29 -0700 | [diff] [blame] | 205 | read_lock_bh(&protocol_list_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 206 | for (protocol = protocol_list; protocol != NULL; protocol = protocol->next) |
| 207 | if (protocol->pid == pid) { |
| 208 | res = 1; |
| 209 | break; |
| 210 | } |
Ralf Baechle | 95ff9f4 | 2006-07-10 16:21:29 -0700 | [diff] [blame] | 211 | read_unlock_bh(&protocol_list_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 212 | |
| 213 | return res; |
| 214 | } |