Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
| 2 | * This program is free software; you can redistribute it and/or modify |
| 3 | * it under the terms of the GNU General Public License as published by |
| 4 | * the Free Software Foundation; either version 2 of the License, or |
| 5 | * (at your option) any later version. |
| 6 | * |
| 7 | * Copyright (C) Jonathan Naylor G4KLX (g4klx@g4klx.demon.co.uk) |
| 8 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 9 | #include <linux/errno.h> |
| 10 | #include <linux/types.h> |
| 11 | #include <linux/socket.h> |
| 12 | #include <linux/in.h> |
| 13 | #include <linux/kernel.h> |
Ralf Baechle | 70868ea | 2006-05-03 23:25:17 -0700 | [diff] [blame] | 14 | #include <linux/module.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 15 | #include <linux/sched.h> |
| 16 | #include <linux/spinlock.h> |
| 17 | #include <linux/timer.h> |
| 18 | #include <linux/string.h> |
| 19 | #include <linux/sockios.h> |
| 20 | #include <linux/net.h> |
| 21 | #include <net/ax25.h> |
| 22 | #include <linux/inet.h> |
| 23 | #include <linux/netdevice.h> |
| 24 | #include <linux/skbuff.h> |
| 25 | #include <net/sock.h> |
| 26 | #include <asm/uaccess.h> |
| 27 | #include <asm/system.h> |
| 28 | #include <linux/fcntl.h> |
| 29 | #include <linux/mm.h> |
| 30 | #include <linux/interrupt.h> |
| 31 | |
Ralf Baechle | 8d5cf59 | 2006-12-14 15:50:01 -0800 | [diff] [blame] | 32 | static struct ax25_protocol *protocol_list; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 33 | static DEFINE_RWLOCK(protocol_list_lock); |
| 34 | |
Ralf Baechle | a428271 | 2006-12-14 15:51:23 -0800 | [diff] [blame^] | 35 | static HLIST_HEAD(ax25_linkfail_list); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 36 | static DEFINE_SPINLOCK(linkfail_lock); |
| 37 | |
| 38 | static struct listen_struct { |
| 39 | struct listen_struct *next; |
| 40 | ax25_address callsign; |
| 41 | struct net_device *dev; |
| 42 | } *listen_list = NULL; |
| 43 | static DEFINE_SPINLOCK(listen_lock); |
| 44 | |
Ralf Baechle | 8d5cf59 | 2006-12-14 15:50:01 -0800 | [diff] [blame] | 45 | /* |
| 46 | * Do not register the internal protocols AX25_P_TEXT, AX25_P_SEGMENT, |
| 47 | * AX25_P_IP or AX25_P_ARP ... |
| 48 | */ |
| 49 | void ax25_register_pid(struct ax25_protocol *ap) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 50 | { |
Ralf Baechle | 95ff9f4 | 2006-07-10 16:21:29 -0700 | [diff] [blame] | 51 | write_lock_bh(&protocol_list_lock); |
Ralf Baechle | 8d5cf59 | 2006-12-14 15:50:01 -0800 | [diff] [blame] | 52 | ap->next = protocol_list; |
| 53 | protocol_list = ap; |
Ralf Baechle | 95ff9f4 | 2006-07-10 16:21:29 -0700 | [diff] [blame] | 54 | write_unlock_bh(&protocol_list_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 55 | } |
| 56 | |
Ralf Baechle | 8d5cf59 | 2006-12-14 15:50:01 -0800 | [diff] [blame] | 57 | EXPORT_SYMBOL_GPL(ax25_register_pid); |
Ralf Baechle | 70868ea | 2006-05-03 23:25:17 -0700 | [diff] [blame] | 58 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 59 | void ax25_protocol_release(unsigned int pid) |
| 60 | { |
Ralf Baechle | 8d5cf59 | 2006-12-14 15:50:01 -0800 | [diff] [blame] | 61 | struct ax25_protocol *s, *protocol; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 62 | |
Ralf Baechle | 95ff9f4 | 2006-07-10 16:21:29 -0700 | [diff] [blame] | 63 | write_lock_bh(&protocol_list_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 64 | protocol = protocol_list; |
| 65 | if (protocol == NULL) { |
Ralf Baechle | 95ff9f4 | 2006-07-10 16:21:29 -0700 | [diff] [blame] | 66 | write_unlock_bh(&protocol_list_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 67 | return; |
| 68 | } |
| 69 | |
| 70 | if (protocol->pid == pid) { |
| 71 | protocol_list = protocol->next; |
Ralf Baechle | 95ff9f4 | 2006-07-10 16:21:29 -0700 | [diff] [blame] | 72 | write_unlock_bh(&protocol_list_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 73 | kfree(protocol); |
| 74 | return; |
| 75 | } |
| 76 | |
| 77 | while (protocol != NULL && protocol->next != NULL) { |
| 78 | if (protocol->next->pid == pid) { |
| 79 | s = protocol->next; |
| 80 | protocol->next = protocol->next->next; |
Ralf Baechle | 95ff9f4 | 2006-07-10 16:21:29 -0700 | [diff] [blame] | 81 | write_unlock_bh(&protocol_list_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 82 | kfree(s); |
| 83 | return; |
| 84 | } |
| 85 | |
| 86 | protocol = protocol->next; |
| 87 | } |
Ralf Baechle | 95ff9f4 | 2006-07-10 16:21:29 -0700 | [diff] [blame] | 88 | write_unlock_bh(&protocol_list_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 89 | } |
| 90 | |
Ralf Baechle | 70868ea | 2006-05-03 23:25:17 -0700 | [diff] [blame] | 91 | EXPORT_SYMBOL(ax25_protocol_release); |
| 92 | |
Ralf Baechle | a428271 | 2006-12-14 15:51:23 -0800 | [diff] [blame^] | 93 | void ax25_linkfail_register(struct ax25_linkfail *lf) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 94 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 95 | spin_lock_bh(&linkfail_lock); |
Ralf Baechle | a428271 | 2006-12-14 15:51:23 -0800 | [diff] [blame^] | 96 | hlist_add_head(&lf->lf_node, &ax25_linkfail_list); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 97 | spin_unlock_bh(&linkfail_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 98 | } |
| 99 | |
Ralf Baechle | 70868ea | 2006-05-03 23:25:17 -0700 | [diff] [blame] | 100 | EXPORT_SYMBOL(ax25_linkfail_register); |
| 101 | |
Ralf Baechle | a428271 | 2006-12-14 15:51:23 -0800 | [diff] [blame^] | 102 | void ax25_linkfail_release(struct ax25_linkfail *lf) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 103 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 104 | spin_lock_bh(&linkfail_lock); |
Ralf Baechle | a428271 | 2006-12-14 15:51:23 -0800 | [diff] [blame^] | 105 | hlist_del_init(&lf->lf_node); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 106 | spin_unlock_bh(&linkfail_lock); |
| 107 | } |
| 108 | |
Ralf Baechle | 70868ea | 2006-05-03 23:25:17 -0700 | [diff] [blame] | 109 | EXPORT_SYMBOL(ax25_linkfail_release); |
| 110 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 111 | int ax25_listen_register(ax25_address *callsign, struct net_device *dev) |
| 112 | { |
| 113 | struct listen_struct *listen; |
| 114 | |
| 115 | if (ax25_listen_mine(callsign, dev)) |
| 116 | return 0; |
| 117 | |
| 118 | if ((listen = kmalloc(sizeof(*listen), GFP_ATOMIC)) == NULL) |
Ralf Baechle | 81dcd16 | 2006-12-14 15:50:34 -0800 | [diff] [blame] | 119 | return -ENOMEM; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 120 | |
| 121 | listen->callsign = *callsign; |
| 122 | listen->dev = dev; |
| 123 | |
| 124 | spin_lock_bh(&listen_lock); |
| 125 | listen->next = listen_list; |
| 126 | listen_list = listen; |
| 127 | spin_unlock_bh(&listen_lock); |
| 128 | |
Ralf Baechle | 81dcd16 | 2006-12-14 15:50:34 -0800 | [diff] [blame] | 129 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 130 | } |
| 131 | |
Ralf Baechle | 70868ea | 2006-05-03 23:25:17 -0700 | [diff] [blame] | 132 | EXPORT_SYMBOL(ax25_listen_register); |
| 133 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 134 | void ax25_listen_release(ax25_address *callsign, struct net_device *dev) |
| 135 | { |
| 136 | struct listen_struct *s, *listen; |
| 137 | |
| 138 | spin_lock_bh(&listen_lock); |
| 139 | listen = listen_list; |
| 140 | if (listen == NULL) { |
| 141 | spin_unlock_bh(&listen_lock); |
| 142 | return; |
| 143 | } |
| 144 | |
| 145 | if (ax25cmp(&listen->callsign, callsign) == 0 && listen->dev == dev) { |
| 146 | listen_list = listen->next; |
| 147 | spin_unlock_bh(&listen_lock); |
| 148 | kfree(listen); |
| 149 | return; |
| 150 | } |
| 151 | |
| 152 | while (listen != NULL && listen->next != NULL) { |
| 153 | if (ax25cmp(&listen->next->callsign, callsign) == 0 && listen->next->dev == dev) { |
| 154 | s = listen->next; |
| 155 | listen->next = listen->next->next; |
| 156 | spin_unlock_bh(&listen_lock); |
| 157 | kfree(s); |
| 158 | return; |
| 159 | } |
| 160 | |
| 161 | listen = listen->next; |
| 162 | } |
| 163 | spin_unlock_bh(&listen_lock); |
| 164 | } |
| 165 | |
Ralf Baechle | 70868ea | 2006-05-03 23:25:17 -0700 | [diff] [blame] | 166 | EXPORT_SYMBOL(ax25_listen_release); |
| 167 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 168 | int (*ax25_protocol_function(unsigned int pid))(struct sk_buff *, ax25_cb *) |
| 169 | { |
| 170 | int (*res)(struct sk_buff *, ax25_cb *) = NULL; |
Ralf Baechle | 8d5cf59 | 2006-12-14 15:50:01 -0800 | [diff] [blame] | 171 | struct ax25_protocol *protocol; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 172 | |
| 173 | read_lock(&protocol_list_lock); |
| 174 | for (protocol = protocol_list; protocol != NULL; protocol = protocol->next) |
| 175 | if (protocol->pid == pid) { |
| 176 | res = protocol->func; |
| 177 | break; |
| 178 | } |
| 179 | read_unlock(&protocol_list_lock); |
| 180 | |
| 181 | return res; |
| 182 | } |
| 183 | |
| 184 | int ax25_listen_mine(ax25_address *callsign, struct net_device *dev) |
| 185 | { |
| 186 | struct listen_struct *listen; |
| 187 | |
| 188 | spin_lock_bh(&listen_lock); |
| 189 | for (listen = listen_list; listen != NULL; listen = listen->next) |
Ralf Baechle | 81dcd16 | 2006-12-14 15:50:34 -0800 | [diff] [blame] | 190 | if (ax25cmp(&listen->callsign, callsign) == 0 && |
| 191 | (listen->dev == dev || listen->dev == NULL)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 192 | spin_unlock_bh(&listen_lock); |
| 193 | return 1; |
| 194 | } |
| 195 | spin_unlock_bh(&listen_lock); |
| 196 | |
| 197 | return 0; |
| 198 | } |
| 199 | |
| 200 | void ax25_link_failed(ax25_cb *ax25, int reason) |
| 201 | { |
Ralf Baechle | a428271 | 2006-12-14 15:51:23 -0800 | [diff] [blame^] | 202 | struct ax25_linkfail *lf; |
| 203 | struct hlist_node *node; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 204 | |
| 205 | spin_lock_bh(&linkfail_lock); |
Ralf Baechle | a428271 | 2006-12-14 15:51:23 -0800 | [diff] [blame^] | 206 | hlist_for_each_entry(lf, node, &ax25_linkfail_list, lf_node) |
| 207 | lf->func(ax25, reason); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 208 | spin_unlock_bh(&linkfail_lock); |
| 209 | } |
| 210 | |
| 211 | int ax25_protocol_is_registered(unsigned int pid) |
| 212 | { |
Ralf Baechle | 8d5cf59 | 2006-12-14 15:50:01 -0800 | [diff] [blame] | 213 | struct ax25_protocol *protocol; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 214 | int res = 0; |
| 215 | |
Ralf Baechle | 95ff9f4 | 2006-07-10 16:21:29 -0700 | [diff] [blame] | 216 | read_lock_bh(&protocol_list_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 217 | for (protocol = protocol_list; protocol != NULL; protocol = protocol->next) |
| 218 | if (protocol->pid == pid) { |
| 219 | res = 1; |
| 220 | break; |
| 221 | } |
Ralf Baechle | 95ff9f4 | 2006-07-10 16:21:29 -0700 | [diff] [blame] | 222 | read_unlock_bh(&protocol_list_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 223 | |
| 224 | return res; |
| 225 | } |