blob: 5faca5db638568d2cd34adc12cc690f349adca65 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
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 Torvalds1da177e2005-04-16 15:20:36 -07009#include <linux/errno.h>
10#include <linux/types.h>
11#include <linux/socket.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090012#include <linux/slab.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070013#include <linux/in.h>
14#include <linux/kernel.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070015#include <linux/timer.h>
16#include <linux/string.h>
17#include <linux/sockios.h>
18#include <linux/net.h>
19#include <linux/spinlock.h>
20#include <net/ax25.h>
21#include <linux/inet.h>
22#include <linux/netdevice.h>
23#include <linux/if_arp.h>
24#include <linux/skbuff.h>
25#include <net/sock.h>
26#include <asm/uaccess.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070027#include <linux/fcntl.h>
28#include <linux/mm.h>
29#include <linux/interrupt.h>
30#include <linux/init.h>
31
32ax25_dev *ax25_dev_list;
33DEFINE_SPINLOCK(ax25_dev_lock);
34
35ax25_dev *ax25_addr_ax25dev(ax25_address *addr)
36{
37 ax25_dev *ax25_dev, *res = NULL;
38
39 spin_lock_bh(&ax25_dev_lock);
40 for (ax25_dev = ax25_dev_list; ax25_dev != NULL; ax25_dev = ax25_dev->next)
41 if (ax25cmp(addr, (ax25_address *)ax25_dev->dev->dev_addr) == 0) {
42 res = ax25_dev;
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 */
53void ax25_dev_device_up(struct net_device *dev)
54{
55 ax25_dev *ax25_dev;
56
Ralf Baechle1b30dd32006-07-09 12:14:22 -070057 if ((ax25_dev = kzalloc(sizeof(*ax25_dev), GFP_ATOMIC)) == NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070058 printk(KERN_ERR "AX.25: ax25_dev_device_up - out of memory\n");
59 return;
60 }
61
Linus Torvalds1da177e2005-04-16 15:20:36 -070062 dev->ax25_ptr = ax25_dev;
63 ax25_dev->dev = dev;
64 dev_hold(dev);
65 ax25_dev->forward = NULL;
66
67 ax25_dev->values[AX25_VALUES_IPDEFMODE] = AX25_DEF_IPDEFMODE;
68 ax25_dev->values[AX25_VALUES_AXDEFMODE] = AX25_DEF_AXDEFMODE;
69 ax25_dev->values[AX25_VALUES_BACKOFF] = AX25_DEF_BACKOFF;
70 ax25_dev->values[AX25_VALUES_CONMODE] = AX25_DEF_CONMODE;
71 ax25_dev->values[AX25_VALUES_WINDOW] = AX25_DEF_WINDOW;
72 ax25_dev->values[AX25_VALUES_EWINDOW] = AX25_DEF_EWINDOW;
73 ax25_dev->values[AX25_VALUES_T1] = AX25_DEF_T1;
74 ax25_dev->values[AX25_VALUES_T2] = AX25_DEF_T2;
75 ax25_dev->values[AX25_VALUES_T3] = AX25_DEF_T3;
76 ax25_dev->values[AX25_VALUES_IDLE] = AX25_DEF_IDLE;
77 ax25_dev->values[AX25_VALUES_N2] = AX25_DEF_N2;
78 ax25_dev->values[AX25_VALUES_PACLEN] = AX25_DEF_PACLEN;
79 ax25_dev->values[AX25_VALUES_PROTOCOL] = AX25_DEF_PROTOCOL;
80 ax25_dev->values[AX25_VALUES_DS_TIMEOUT]= AX25_DEF_DS_TIMEOUT;
81
82#if defined(CONFIG_AX25_DAMA_SLAVE) || defined(CONFIG_AX25_DAMA_MASTER)
Jarek Poplawskie848b582008-02-11 21:38:32 -080083 ax25_ds_setup_timer(ax25_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -070084#endif
85
86 spin_lock_bh(&ax25_dev_lock);
87 ax25_dev->next = ax25_dev_list;
88 ax25_dev_list = ax25_dev;
89 spin_unlock_bh(&ax25_dev_lock);
90
Eric W. Biederman0ca7a4c2012-04-19 13:34:18 +000091 ax25_register_dev_sysctl(ax25_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -070092}
93
94void ax25_dev_device_down(struct net_device *dev)
95{
96 ax25_dev *s, *ax25_dev;
97
98 if ((ax25_dev = ax25_dev_ax25dev(dev)) == NULL)
99 return;
100
Eric W. Biederman0ca7a4c2012-04-19 13:34:18 +0000101 ax25_unregister_dev_sysctl(ax25_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102
103 spin_lock_bh(&ax25_dev_lock);
104
105#ifdef CONFIG_AX25_DAMA_SLAVE
106 ax25_ds_del_timer(ax25_dev);
107#endif
108
109 /*
110 * Remove any packet forwarding that points to this device.
111 */
112 for (s = ax25_dev_list; s != NULL; s = s->next)
113 if (s->forward == dev)
114 s->forward = NULL;
115
116 if ((s = ax25_dev_list) == ax25_dev) {
117 ax25_dev_list = s->next;
118 spin_unlock_bh(&ax25_dev_lock);
Cong Wangc39b8fd2018-12-29 13:56:36 -0800119 dev->ax25_ptr = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120 dev_put(dev);
121 kfree(ax25_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122 return;
123 }
124
125 while (s != NULL && s->next != NULL) {
126 if (s->next == ax25_dev) {
127 s->next = ax25_dev->next;
128 spin_unlock_bh(&ax25_dev_lock);
Cong Wangc39b8fd2018-12-29 13:56:36 -0800129 dev->ax25_ptr = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130 dev_put(dev);
131 kfree(ax25_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132 return;
133 }
134
135 s = s->next;
136 }
137 spin_unlock_bh(&ax25_dev_lock);
138 dev->ax25_ptr = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139}
140
141int ax25_fwd_ioctl(unsigned int cmd, struct ax25_fwd_struct *fwd)
142{
143 ax25_dev *ax25_dev, *fwd_dev;
144
145 if ((ax25_dev = ax25_addr_ax25dev(&fwd->port_from)) == NULL)
146 return -EINVAL;
147
148 switch (cmd) {
149 case SIOCAX25ADDFWD:
150 if ((fwd_dev = ax25_addr_ax25dev(&fwd->port_to)) == NULL)
151 return -EINVAL;
152 if (ax25_dev->forward != NULL)
153 return -EINVAL;
154 ax25_dev->forward = fwd_dev->dev;
155 break;
156
157 case SIOCAX25DELFWD:
158 if (ax25_dev->forward == NULL)
159 return -EINVAL;
160 ax25_dev->forward = NULL;
161 break;
162
163 default:
164 return -EINVAL;
165 }
166
167 return 0;
168}
169
170struct net_device *ax25_fwd_dev(struct net_device *dev)
171{
172 ax25_dev *ax25_dev;
173
174 if ((ax25_dev = ax25_dev_ax25dev(dev)) == NULL)
175 return dev;
176
177 if (ax25_dev->forward == NULL)
178 return dev;
179
180 return ax25_dev->forward;
181}
182
183/*
184 * Free all memory associated with device structures.
185 */
186void __exit ax25_dev_free(void)
187{
188 ax25_dev *s, *ax25_dev;
189
190 spin_lock_bh(&ax25_dev_lock);
191 ax25_dev = ax25_dev_list;
192 while (ax25_dev != NULL) {
193 s = ax25_dev;
194 dev_put(ax25_dev->dev);
195 ax25_dev = ax25_dev->next;
196 kfree(s);
197 }
198 ax25_dev_list = NULL;
199 spin_unlock_bh(&ax25_dev_lock);
200}