ext4: Fix potential memory leak in ext4_fill_super
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / net / ax25 / ax25_dev.c
blobc1cb982f6e86954018c7311d17016cb2189f9bf1
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.
7 * Copyright (C) Jonathan Naylor G4KLX (g4klx@g4klx.demon.co.uk)
8 */
9 #include <linux/errno.h>
10 #include <linux/types.h>
11 #include <linux/socket.h>
12 #include <linux/slab.h>
13 #include <linux/in.h>
14 #include <linux/kernel.h>
15 #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>
27 #include <asm/system.h>
28 #include <linux/fcntl.h>
29 #include <linux/mm.h>
30 #include <linux/interrupt.h>
31 #include <linux/init.h>
33 ax25_dev *ax25_dev_list;
34 DEFINE_SPINLOCK(ax25_dev_lock);
36 ax25_dev *ax25_addr_ax25dev(ax25_address *addr)
38 ax25_dev *ax25_dev, *res = NULL;
40 spin_lock_bh(&ax25_dev_lock);
41 for (ax25_dev = ax25_dev_list; ax25_dev != NULL; ax25_dev = ax25_dev->next)
42 if (ax25cmp(addr, (ax25_address *)ax25_dev->dev->dev_addr) == 0) {
43 res = ax25_dev;
45 spin_unlock_bh(&ax25_dev_lock);
47 return res;
51 * This is called when an interface is brought up. These are
52 * reasonable defaults.
54 void ax25_dev_device_up(struct net_device *dev)
56 ax25_dev *ax25_dev;
58 if ((ax25_dev = kzalloc(sizeof(*ax25_dev), GFP_ATOMIC)) == NULL) {
59 printk(KERN_ERR "AX.25: ax25_dev_device_up - out of memory\n");
60 return;
63 ax25_unregister_sysctl();
65 dev->ax25_ptr = ax25_dev;
66 ax25_dev->dev = dev;
67 dev_hold(dev);
68 ax25_dev->forward = NULL;
70 ax25_dev->values[AX25_VALUES_IPDEFMODE] = AX25_DEF_IPDEFMODE;
71 ax25_dev->values[AX25_VALUES_AXDEFMODE] = AX25_DEF_AXDEFMODE;
72 ax25_dev->values[AX25_VALUES_BACKOFF] = AX25_DEF_BACKOFF;
73 ax25_dev->values[AX25_VALUES_CONMODE] = AX25_DEF_CONMODE;
74 ax25_dev->values[AX25_VALUES_WINDOW] = AX25_DEF_WINDOW;
75 ax25_dev->values[AX25_VALUES_EWINDOW] = AX25_DEF_EWINDOW;
76 ax25_dev->values[AX25_VALUES_T1] = AX25_DEF_T1;
77 ax25_dev->values[AX25_VALUES_T2] = AX25_DEF_T2;
78 ax25_dev->values[AX25_VALUES_T3] = AX25_DEF_T3;
79 ax25_dev->values[AX25_VALUES_IDLE] = AX25_DEF_IDLE;
80 ax25_dev->values[AX25_VALUES_N2] = AX25_DEF_N2;
81 ax25_dev->values[AX25_VALUES_PACLEN] = AX25_DEF_PACLEN;
82 ax25_dev->values[AX25_VALUES_PROTOCOL] = AX25_DEF_PROTOCOL;
83 ax25_dev->values[AX25_VALUES_DS_TIMEOUT]= AX25_DEF_DS_TIMEOUT;
85 #if defined(CONFIG_AX25_DAMA_SLAVE) || defined(CONFIG_AX25_DAMA_MASTER)
86 ax25_ds_setup_timer(ax25_dev);
87 #endif
89 spin_lock_bh(&ax25_dev_lock);
90 ax25_dev->next = ax25_dev_list;
91 ax25_dev_list = ax25_dev;
92 spin_unlock_bh(&ax25_dev_lock);
94 ax25_register_sysctl();
97 void ax25_dev_device_down(struct net_device *dev)
99 ax25_dev *s, *ax25_dev;
101 if ((ax25_dev = ax25_dev_ax25dev(dev)) == NULL)
102 return;
104 ax25_unregister_sysctl();
106 spin_lock_bh(&ax25_dev_lock);
108 #ifdef CONFIG_AX25_DAMA_SLAVE
109 ax25_ds_del_timer(ax25_dev);
110 #endif
113 * Remove any packet forwarding that points to this device.
115 for (s = ax25_dev_list; s != NULL; s = s->next)
116 if (s->forward == dev)
117 s->forward = NULL;
119 if ((s = ax25_dev_list) == ax25_dev) {
120 ax25_dev_list = s->next;
121 spin_unlock_bh(&ax25_dev_lock);
122 dev_put(dev);
123 kfree(ax25_dev);
124 ax25_register_sysctl();
125 return;
128 while (s != NULL && s->next != NULL) {
129 if (s->next == ax25_dev) {
130 s->next = ax25_dev->next;
131 spin_unlock_bh(&ax25_dev_lock);
132 dev_put(dev);
133 kfree(ax25_dev);
134 ax25_register_sysctl();
135 return;
138 s = s->next;
140 spin_unlock_bh(&ax25_dev_lock);
141 dev->ax25_ptr = NULL;
143 ax25_register_sysctl();
146 int ax25_fwd_ioctl(unsigned int cmd, struct ax25_fwd_struct *fwd)
148 ax25_dev *ax25_dev, *fwd_dev;
150 if ((ax25_dev = ax25_addr_ax25dev(&fwd->port_from)) == NULL)
151 return -EINVAL;
153 switch (cmd) {
154 case SIOCAX25ADDFWD:
155 if ((fwd_dev = ax25_addr_ax25dev(&fwd->port_to)) == NULL)
156 return -EINVAL;
157 if (ax25_dev->forward != NULL)
158 return -EINVAL;
159 ax25_dev->forward = fwd_dev->dev;
160 break;
162 case SIOCAX25DELFWD:
163 if (ax25_dev->forward == NULL)
164 return -EINVAL;
165 ax25_dev->forward = NULL;
166 break;
168 default:
169 return -EINVAL;
172 return 0;
175 struct net_device *ax25_fwd_dev(struct net_device *dev)
177 ax25_dev *ax25_dev;
179 if ((ax25_dev = ax25_dev_ax25dev(dev)) == NULL)
180 return dev;
182 if (ax25_dev->forward == NULL)
183 return dev;
185 return ax25_dev->forward;
189 * Free all memory associated with device structures.
191 void __exit ax25_dev_free(void)
193 ax25_dev *s, *ax25_dev;
195 spin_lock_bh(&ax25_dev_lock);
196 ax25_dev = ax25_dev_list;
197 while (ax25_dev != NULL) {
198 s = ax25_dev;
199 dev_put(ax25_dev->dev);
200 ax25_dev = ax25_dev->next;
201 kfree(s);
203 ax25_dev_list = NULL;
204 spin_unlock_bh(&ax25_dev_lock);