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