1 // SPDX-License-Identifier: GPL-2.0-or-later
4 * Copyright (C) Jonathan Naylor G4KLX (g4klx@g4klx.demon.co.uk)
6 #include <linux/errno.h>
7 #include <linux/types.h>
8 #include <linux/socket.h>
9 #include <linux/slab.h>
11 #include <linux/kernel.h>
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>
18 #include <linux/inet.h>
19 #include <linux/netdevice.h>
20 #include <linux/if_arp.h>
21 #include <linux/skbuff.h>
23 #include <linux/uaccess.h>
24 #include <linux/fcntl.h>
26 #include <linux/interrupt.h>
27 #include <linux/init.h>
29 ax25_dev *ax25_dev_list;
30 DEFINE_SPINLOCK(ax25_dev_lock);
32 ax25_dev *ax25_addr_ax25dev(ax25_address *addr)
34 ax25_dev *ax25_dev, *res = NULL;
36 spin_lock_bh(&ax25_dev_lock);
37 for (ax25_dev = ax25_dev_list; ax25_dev != NULL; ax25_dev = ax25_dev->next)
38 if (ax25cmp(addr, (const ax25_address *)ax25_dev->dev->dev_addr) == 0) {
40 ax25_dev_hold(ax25_dev);
42 spin_unlock_bh(&ax25_dev_lock);
48 * This is called when an interface is brought up. These are
49 * reasonable defaults.
51 void ax25_dev_device_up(struct net_device *dev)
55 ax25_dev = kzalloc(sizeof(*ax25_dev), GFP_KERNEL);
57 printk(KERN_ERR "AX.25: ax25_dev_device_up - out of memory\n");
61 refcount_set(&ax25_dev->refcount, 1);
62 dev->ax25_ptr = ax25_dev;
64 netdev_hold(dev, &ax25_dev->dev_tracker, GFP_KERNEL);
65 ax25_dev->forward = NULL;
66 ax25_dev->device_up = true;
68 ax25_dev->values[AX25_VALUES_IPDEFMODE] = AX25_DEF_IPDEFMODE;
69 ax25_dev->values[AX25_VALUES_AXDEFMODE] = AX25_DEF_AXDEFMODE;
70 ax25_dev->values[AX25_VALUES_BACKOFF] = AX25_DEF_BACKOFF;
71 ax25_dev->values[AX25_VALUES_CONMODE] = AX25_DEF_CONMODE;
72 ax25_dev->values[AX25_VALUES_WINDOW] = AX25_DEF_WINDOW;
73 ax25_dev->values[AX25_VALUES_EWINDOW] = AX25_DEF_EWINDOW;
74 ax25_dev->values[AX25_VALUES_T1] = AX25_DEF_T1;
75 ax25_dev->values[AX25_VALUES_T2] = AX25_DEF_T2;
76 ax25_dev->values[AX25_VALUES_T3] = AX25_DEF_T3;
77 ax25_dev->values[AX25_VALUES_IDLE] = AX25_DEF_IDLE;
78 ax25_dev->values[AX25_VALUES_N2] = AX25_DEF_N2;
79 ax25_dev->values[AX25_VALUES_PACLEN] = AX25_DEF_PACLEN;
80 ax25_dev->values[AX25_VALUES_PROTOCOL] = AX25_DEF_PROTOCOL;
81 ax25_dev->values[AX25_VALUES_DS_TIMEOUT]= AX25_DEF_DS_TIMEOUT;
83 #if defined(CONFIG_AX25_DAMA_SLAVE) || defined(CONFIG_AX25_DAMA_MASTER)
84 ax25_ds_setup_timer(ax25_dev);
87 spin_lock_bh(&ax25_dev_lock);
88 ax25_dev->next = ax25_dev_list;
89 ax25_dev_list = ax25_dev;
90 spin_unlock_bh(&ax25_dev_lock);
91 ax25_dev_hold(ax25_dev);
93 ax25_register_dev_sysctl(ax25_dev);
96 void ax25_dev_device_down(struct net_device *dev)
98 ax25_dev *s, *ax25_dev;
100 if ((ax25_dev = ax25_dev_ax25dev(dev)) == NULL)
103 ax25_unregister_dev_sysctl(ax25_dev);
105 spin_lock_bh(&ax25_dev_lock);
107 #ifdef CONFIG_AX25_DAMA_SLAVE
108 ax25_ds_del_timer(ax25_dev);
112 * Remove any packet forwarding that points to this device.
114 for (s = ax25_dev_list; s != NULL; s = s->next)
115 if (s->forward == dev)
118 if ((s = ax25_dev_list) == ax25_dev) {
119 ax25_dev_list = s->next;
123 while (s != NULL && s->next != NULL) {
124 if (s->next == ax25_dev) {
125 s->next = ax25_dev->next;
131 spin_unlock_bh(&ax25_dev_lock);
132 dev->ax25_ptr = NULL;
133 ax25_dev_put(ax25_dev);
137 spin_unlock_bh(&ax25_dev_lock);
138 ax25_dev_put(ax25_dev);
139 dev->ax25_ptr = NULL;
140 netdev_put(dev, &ax25_dev->dev_tracker);
141 ax25_dev_put(ax25_dev);
144 int ax25_fwd_ioctl(unsigned int cmd, struct ax25_fwd_struct *fwd)
146 ax25_dev *ax25_dev, *fwd_dev;
148 if ((ax25_dev = ax25_addr_ax25dev(&fwd->port_from)) == NULL)
153 fwd_dev = ax25_addr_ax25dev(&fwd->port_to);
155 ax25_dev_put(ax25_dev);
158 if (ax25_dev->forward) {
159 ax25_dev_put(fwd_dev);
160 ax25_dev_put(ax25_dev);
163 ax25_dev->forward = fwd_dev->dev;
164 ax25_dev_put(fwd_dev);
165 ax25_dev_put(ax25_dev);
169 if (!ax25_dev->forward) {
170 ax25_dev_put(ax25_dev);
173 ax25_dev->forward = NULL;
174 ax25_dev_put(ax25_dev);
178 ax25_dev_put(ax25_dev);
185 struct net_device *ax25_fwd_dev(struct net_device *dev)
189 if ((ax25_dev = ax25_dev_ax25dev(dev)) == NULL)
192 if (ax25_dev->forward == NULL)
195 return ax25_dev->forward;
199 * Free all memory associated with device structures.
201 void __exit ax25_dev_free(void)
203 ax25_dev *s, *ax25_dev;
205 spin_lock_bh(&ax25_dev_lock);
206 ax25_dev = ax25_dev_list;
207 while (ax25_dev != NULL) {
209 netdev_put(ax25_dev->dev, &ax25_dev->dev_tracker);
210 ax25_dev = ax25_dev->next;
213 ax25_dev_list = NULL;
214 spin_unlock_bh(&ax25_dev_lock);