xref: /openbmc/linux/net/ax25/ax25_dev.c (revision 93696d8f)
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3  *
4  * Copyright (C) Jonathan Naylor G4KLX (g4klx@g4klx.demon.co.uk)
5  */
6 #include <linux/errno.h>
7 #include <linux/types.h>
8 #include <linux/socket.h>
9 #include <linux/slab.h>
10 #include <linux/in.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>
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>
23 #include <linux/uaccess.h>
24 #include <linux/fcntl.h>
25 #include <linux/list.h>
26 #include <linux/mm.h>
27 #include <linux/interrupt.h>
28 #include <linux/init.h>
29 
30 static LIST_HEAD(ax25_dev_list);
31 DEFINE_SPINLOCK(ax25_dev_lock);
32 
33 ax25_dev *ax25_addr_ax25dev(ax25_address *addr)
34 {
35 	ax25_dev *ax25_dev, *res = NULL;
36 
37 	spin_lock_bh(&ax25_dev_lock);
38 	list_for_each_entry(ax25_dev, &ax25_dev_list, list)
39 		if (ax25cmp(addr, (const ax25_address *)ax25_dev->dev->dev_addr) == 0) {
40 			res = ax25_dev;
41 			ax25_dev_hold(ax25_dev);
42 			break;
43 		}
44 	spin_unlock_bh(&ax25_dev_lock);
45 
46 	return res;
47 }
48 
49 /*
50  *	This is called when an interface is brought up. These are
51  *	reasonable defaults.
52  */
53 void ax25_dev_device_up(struct net_device *dev)
54 {
55 	ax25_dev *ax25_dev;
56 
57 	ax25_dev = kzalloc(sizeof(*ax25_dev), GFP_KERNEL);
58 	if (!ax25_dev) {
59 		printk(KERN_ERR "AX.25: ax25_dev_device_up - out of memory\n");
60 		return;
61 	}
62 
63 	refcount_set(&ax25_dev->refcount, 1);
64 	ax25_dev->dev     = dev;
65 	netdev_hold(dev, &ax25_dev->dev_tracker, GFP_KERNEL);
66 	ax25_dev->forward = NULL;
67 	ax25_dev->device_up = true;
68 
69 	ax25_dev->values[AX25_VALUES_IPDEFMODE] = AX25_DEF_IPDEFMODE;
70 	ax25_dev->values[AX25_VALUES_AXDEFMODE] = AX25_DEF_AXDEFMODE;
71 	ax25_dev->values[AX25_VALUES_BACKOFF]   = AX25_DEF_BACKOFF;
72 	ax25_dev->values[AX25_VALUES_CONMODE]   = AX25_DEF_CONMODE;
73 	ax25_dev->values[AX25_VALUES_WINDOW]    = AX25_DEF_WINDOW;
74 	ax25_dev->values[AX25_VALUES_EWINDOW]   = AX25_DEF_EWINDOW;
75 	ax25_dev->values[AX25_VALUES_T1]        = AX25_DEF_T1;
76 	ax25_dev->values[AX25_VALUES_T2]        = AX25_DEF_T2;
77 	ax25_dev->values[AX25_VALUES_T3]        = AX25_DEF_T3;
78 	ax25_dev->values[AX25_VALUES_IDLE]	= AX25_DEF_IDLE;
79 	ax25_dev->values[AX25_VALUES_N2]        = AX25_DEF_N2;
80 	ax25_dev->values[AX25_VALUES_PACLEN]	= AX25_DEF_PACLEN;
81 	ax25_dev->values[AX25_VALUES_PROTOCOL]  = AX25_DEF_PROTOCOL;
82 	ax25_dev->values[AX25_VALUES_DS_TIMEOUT]= AX25_DEF_DS_TIMEOUT;
83 
84 #if defined(CONFIG_AX25_DAMA_SLAVE) || defined(CONFIG_AX25_DAMA_MASTER)
85 	ax25_ds_setup_timer(ax25_dev);
86 #endif
87 
88 	spin_lock_bh(&ax25_dev_lock);
89 	list_add(&ax25_dev->list, &ax25_dev_list);
90 	dev->ax25_ptr     = ax25_dev;
91 	spin_unlock_bh(&ax25_dev_lock);
92 
93 	ax25_register_dev_sysctl(ax25_dev);
94 }
95 
96 void ax25_dev_device_down(struct net_device *dev)
97 {
98 	ax25_dev *s, *ax25_dev;
99 
100 	if ((ax25_dev = ax25_dev_ax25dev(dev)) == NULL)
101 		return;
102 
103 	ax25_unregister_dev_sysctl(ax25_dev);
104 
105 	spin_lock_bh(&ax25_dev_lock);
106 
107 #ifdef CONFIG_AX25_DAMA_SLAVE
108 	timer_shutdown_sync(&ax25_dev->dama.slave_timer);
109 #endif
110 
111 	/*
112 	 *	Remove any packet forwarding that points to this device.
113 	 */
114 	list_for_each_entry(s, &ax25_dev_list, list)
115 		if (s->forward == dev)
116 			s->forward = NULL;
117 
118 	list_for_each_entry(s, &ax25_dev_list, list) {
119 		if (s == ax25_dev) {
120 			list_del(&s->list);
121 			break;
122 		}
123 	}
124 
125 	dev->ax25_ptr = NULL;
126 	spin_unlock_bh(&ax25_dev_lock);
127 	netdev_put(dev, &ax25_dev->dev_tracker);
128 	ax25_dev_put(ax25_dev);
129 }
130 
131 int ax25_fwd_ioctl(unsigned int cmd, struct ax25_fwd_struct *fwd)
132 {
133 	ax25_dev *ax25_dev, *fwd_dev;
134 
135 	if ((ax25_dev = ax25_addr_ax25dev(&fwd->port_from)) == NULL)
136 		return -EINVAL;
137 
138 	switch (cmd) {
139 	case SIOCAX25ADDFWD:
140 		fwd_dev = ax25_addr_ax25dev(&fwd->port_to);
141 		if (!fwd_dev) {
142 			ax25_dev_put(ax25_dev);
143 			return -EINVAL;
144 		}
145 		if (ax25_dev->forward) {
146 			ax25_dev_put(fwd_dev);
147 			ax25_dev_put(ax25_dev);
148 			return -EINVAL;
149 		}
150 		ax25_dev->forward = fwd_dev->dev;
151 		ax25_dev_put(fwd_dev);
152 		ax25_dev_put(ax25_dev);
153 		break;
154 
155 	case SIOCAX25DELFWD:
156 		if (!ax25_dev->forward) {
157 			ax25_dev_put(ax25_dev);
158 			return -EINVAL;
159 		}
160 		ax25_dev->forward = NULL;
161 		ax25_dev_put(ax25_dev);
162 		break;
163 
164 	default:
165 		ax25_dev_put(ax25_dev);
166 		return -EINVAL;
167 	}
168 
169 	return 0;
170 }
171 
172 struct net_device *ax25_fwd_dev(struct net_device *dev)
173 {
174 	ax25_dev *ax25_dev;
175 
176 	if ((ax25_dev = ax25_dev_ax25dev(dev)) == NULL)
177 		return dev;
178 
179 	if (ax25_dev->forward == NULL)
180 		return dev;
181 
182 	return ax25_dev->forward;
183 }
184 
185 /*
186  *	Free all memory associated with device structures.
187  */
188 void __exit ax25_dev_free(void)
189 {
190 	ax25_dev *s, *n;
191 
192 	spin_lock_bh(&ax25_dev_lock);
193 	list_for_each_entry_safe(s, n, &ax25_dev_list, list) {
194 		netdev_put(s->dev, &s->dev_tracker);
195 		list_del(&s->list);
196 		ax25_dev_put(s);
197 	}
198 	spin_unlock_bh(&ax25_dev_lock);
199 }
200