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 | */ |
| 9 | #include <linux/config.h> |
| 10 | #include <linux/errno.h> |
| 11 | #include <linux/types.h> |
| 12 | #include <linux/socket.h> |
| 13 | #include <linux/in.h> |
| 14 | #include <linux/kernel.h> |
| 15 | #include <linux/sched.h> |
| 16 | #include <linux/timer.h> |
| 17 | #include <linux/string.h> |
| 18 | #include <linux/sockios.h> |
| 19 | #include <linux/net.h> |
| 20 | #include <linux/spinlock.h> |
| 21 | #include <net/ax25.h> |
| 22 | #include <linux/inet.h> |
| 23 | #include <linux/netdevice.h> |
| 24 | #include <linux/if_arp.h> |
| 25 | #include <linux/skbuff.h> |
| 26 | #include <net/sock.h> |
| 27 | #include <asm/uaccess.h> |
| 28 | #include <asm/system.h> |
| 29 | #include <linux/fcntl.h> |
| 30 | #include <linux/mm.h> |
| 31 | #include <linux/interrupt.h> |
| 32 | #include <linux/init.h> |
| 33 | |
| 34 | ax25_dev *ax25_dev_list; |
| 35 | DEFINE_SPINLOCK(ax25_dev_lock); |
| 36 | |
| 37 | ax25_dev *ax25_addr_ax25dev(ax25_address *addr) |
| 38 | { |
| 39 | ax25_dev *ax25_dev, *res = NULL; |
| 40 | |
| 41 | spin_lock_bh(&ax25_dev_lock); |
| 42 | for (ax25_dev = ax25_dev_list; ax25_dev != NULL; ax25_dev = ax25_dev->next) |
| 43 | if (ax25cmp(addr, (ax25_address *)ax25_dev->dev->dev_addr) == 0) { |
| 44 | res = ax25_dev; |
| 45 | } |
| 46 | spin_unlock_bh(&ax25_dev_lock); |
| 47 | |
| 48 | return res; |
| 49 | } |
| 50 | |
| 51 | /* |
| 52 | * This is called when an interface is brought up. These are |
| 53 | * reasonable defaults. |
| 54 | */ |
| 55 | void ax25_dev_device_up(struct net_device *dev) |
| 56 | { |
| 57 | ax25_dev *ax25_dev; |
| 58 | |
| 59 | if ((ax25_dev = kmalloc(sizeof(*ax25_dev), GFP_ATOMIC)) == NULL) { |
| 60 | printk(KERN_ERR "AX.25: ax25_dev_device_up - out of memory\n"); |
| 61 | return; |
| 62 | } |
| 63 | |
| 64 | ax25_unregister_sysctl(); |
| 65 | |
| 66 | memset(ax25_dev, 0x00, sizeof(*ax25_dev)); |
| 67 | |
| 68 | dev->ax25_ptr = ax25_dev; |
| 69 | ax25_dev->dev = dev; |
| 70 | dev_hold(dev); |
| 71 | ax25_dev->forward = NULL; |
| 72 | |
| 73 | ax25_dev->values[AX25_VALUES_IPDEFMODE] = AX25_DEF_IPDEFMODE; |
| 74 | ax25_dev->values[AX25_VALUES_AXDEFMODE] = AX25_DEF_AXDEFMODE; |
| 75 | ax25_dev->values[AX25_VALUES_BACKOFF] = AX25_DEF_BACKOFF; |
| 76 | ax25_dev->values[AX25_VALUES_CONMODE] = AX25_DEF_CONMODE; |
| 77 | ax25_dev->values[AX25_VALUES_WINDOW] = AX25_DEF_WINDOW; |
| 78 | ax25_dev->values[AX25_VALUES_EWINDOW] = AX25_DEF_EWINDOW; |
| 79 | ax25_dev->values[AX25_VALUES_T1] = AX25_DEF_T1; |
| 80 | ax25_dev->values[AX25_VALUES_T2] = AX25_DEF_T2; |
| 81 | ax25_dev->values[AX25_VALUES_T3] = AX25_DEF_T3; |
| 82 | ax25_dev->values[AX25_VALUES_IDLE] = AX25_DEF_IDLE; |
| 83 | ax25_dev->values[AX25_VALUES_N2] = AX25_DEF_N2; |
| 84 | ax25_dev->values[AX25_VALUES_PACLEN] = AX25_DEF_PACLEN; |
| 85 | ax25_dev->values[AX25_VALUES_PROTOCOL] = AX25_DEF_PROTOCOL; |
| 86 | ax25_dev->values[AX25_VALUES_DS_TIMEOUT]= AX25_DEF_DS_TIMEOUT; |
| 87 | |
| 88 | #if defined(CONFIG_AX25_DAMA_SLAVE) || defined(CONFIG_AX25_DAMA_MASTER) |
| 89 | init_timer(&ax25_dev->dama.slave_timer); |
| 90 | #endif |
| 91 | |
| 92 | spin_lock_bh(&ax25_dev_lock); |
| 93 | ax25_dev->next = ax25_dev_list; |
| 94 | ax25_dev_list = ax25_dev; |
| 95 | spin_unlock_bh(&ax25_dev_lock); |
| 96 | |
| 97 | ax25_register_sysctl(); |
| 98 | } |
| 99 | |
| 100 | void ax25_dev_device_down(struct net_device *dev) |
| 101 | { |
| 102 | ax25_dev *s, *ax25_dev; |
| 103 | |
| 104 | if ((ax25_dev = ax25_dev_ax25dev(dev)) == NULL) |
| 105 | return; |
| 106 | |
| 107 | ax25_unregister_sysctl(); |
| 108 | |
| 109 | spin_lock_bh(&ax25_dev_lock); |
| 110 | |
| 111 | #ifdef CONFIG_AX25_DAMA_SLAVE |
| 112 | ax25_ds_del_timer(ax25_dev); |
| 113 | #endif |
| 114 | |
| 115 | /* |
| 116 | * Remove any packet forwarding that points to this device. |
| 117 | */ |
| 118 | for (s = ax25_dev_list; s != NULL; s = s->next) |
| 119 | if (s->forward == dev) |
| 120 | s->forward = NULL; |
| 121 | |
| 122 | if ((s = ax25_dev_list) == ax25_dev) { |
| 123 | ax25_dev_list = s->next; |
| 124 | spin_unlock_bh(&ax25_dev_lock); |
| 125 | dev_put(dev); |
| 126 | kfree(ax25_dev); |
| 127 | ax25_register_sysctl(); |
| 128 | return; |
| 129 | } |
| 130 | |
| 131 | while (s != NULL && s->next != NULL) { |
| 132 | if (s->next == ax25_dev) { |
| 133 | s->next = ax25_dev->next; |
| 134 | spin_unlock_bh(&ax25_dev_lock); |
| 135 | dev_put(dev); |
| 136 | kfree(ax25_dev); |
| 137 | ax25_register_sysctl(); |
| 138 | return; |
| 139 | } |
| 140 | |
| 141 | s = s->next; |
| 142 | } |
| 143 | spin_unlock_bh(&ax25_dev_lock); |
| 144 | dev->ax25_ptr = NULL; |
| 145 | |
| 146 | ax25_register_sysctl(); |
| 147 | } |
| 148 | |
| 149 | int ax25_fwd_ioctl(unsigned int cmd, struct ax25_fwd_struct *fwd) |
| 150 | { |
| 151 | ax25_dev *ax25_dev, *fwd_dev; |
| 152 | |
| 153 | if ((ax25_dev = ax25_addr_ax25dev(&fwd->port_from)) == NULL) |
| 154 | return -EINVAL; |
| 155 | |
| 156 | switch (cmd) { |
| 157 | case SIOCAX25ADDFWD: |
| 158 | if ((fwd_dev = ax25_addr_ax25dev(&fwd->port_to)) == NULL) |
| 159 | return -EINVAL; |
| 160 | if (ax25_dev->forward != NULL) |
| 161 | return -EINVAL; |
| 162 | ax25_dev->forward = fwd_dev->dev; |
| 163 | break; |
| 164 | |
| 165 | case SIOCAX25DELFWD: |
| 166 | if (ax25_dev->forward == NULL) |
| 167 | return -EINVAL; |
| 168 | ax25_dev->forward = NULL; |
| 169 | break; |
| 170 | |
| 171 | default: |
| 172 | return -EINVAL; |
| 173 | } |
| 174 | |
| 175 | return 0; |
| 176 | } |
| 177 | |
| 178 | struct net_device *ax25_fwd_dev(struct net_device *dev) |
| 179 | { |
| 180 | ax25_dev *ax25_dev; |
| 181 | |
| 182 | if ((ax25_dev = ax25_dev_ax25dev(dev)) == NULL) |
| 183 | return dev; |
| 184 | |
| 185 | if (ax25_dev->forward == NULL) |
| 186 | return dev; |
| 187 | |
| 188 | return ax25_dev->forward; |
| 189 | } |
| 190 | |
| 191 | /* |
| 192 | * Free all memory associated with device structures. |
| 193 | */ |
| 194 | void __exit ax25_dev_free(void) |
| 195 | { |
| 196 | ax25_dev *s, *ax25_dev; |
| 197 | |
| 198 | spin_lock_bh(&ax25_dev_lock); |
| 199 | ax25_dev = ax25_dev_list; |
| 200 | while (ax25_dev != NULL) { |
| 201 | s = ax25_dev; |
| 202 | dev_put(ax25_dev->dev); |
| 203 | ax25_dev = ax25_dev->next; |
| 204 | kfree(s); |
| 205 | } |
| 206 | ax25_dev_list = NULL; |
| 207 | spin_unlock_bh(&ax25_dev_lock); |
| 208 | } |