add config.c, neigh_ann_rcv.c, neigh_ann_snd.c
[cor.git] / net / cor / config.c
blobef9699d40034c47fc407f7ccf08f99492441e773
1 /**
2 * Connection oriented routing
3 * Copyright (C) 2007-2021 Michael Blizek
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License
7 * as published by the Free Software Foundation; either version 2
8 * of the License, or (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
18 * 02110-1301, USA.
21 #include "cor.h"
23 static DEFINE_MUTEX(cor_config_lock);
25 DEFINE_SPINLOCK(cor_local_addr_lock);
26 char *cor_local_addr;
27 __u32 cor_local_addrlen;
28 __be32 cor_local_addr_sessionid;
31 static DEFINE_SPINLOCK(cor_interface_config_lock);
32 static struct cor_interface_config *cor_interface_config = 0;
33 static __u32 cor_num_interfaces = 0;
34 static int cor_all_interfaces = 0;
37 int cor_is_device_configurated(struct net_device *dev)
39 int ret = 0;
41 unsigned long iflags;
43 __u32 i;
44 spin_lock_irqsave(&cor_interface_config_lock, iflags);
46 if (cor_all_interfaces != 0) {
47 ret = 1;
48 goto out;
51 BUG_ON(cor_num_interfaces > 65536);
52 for (i=0;i<cor_num_interfaces;i++) {
53 struct cor_interface_config *curr = &(cor_interface_config[i]);
54 __u32 j;
56 BUG_ON(curr->name == 0);
58 for (j=0;;j++) {
59 if (j >= sizeof(dev->name))
60 break;
62 if (dev->name[j] == 0 && j == curr->name_len) {
63 ret = 1;
64 goto out;
67 if (dev->name[j] == 0 || j >= curr->name_len)
68 break;
70 if (dev->name[j] != curr->name[j])
71 break;
75 out:
76 spin_unlock_irqrestore(&cor_interface_config_lock, iflags);
78 return ret;
81 void cor_set_interface_config(struct cor_interface_config *new_config,
82 __u32 new_num_interfaces, int new_all_interfaces)
84 unsigned long iflags;
85 __u32 i;
87 spin_lock_irqsave(&cor_interface_config_lock, iflags);
89 BUG_ON(cor_num_interfaces > 65536);
90 for (i=0;i<cor_num_interfaces;i++) {
91 struct cor_interface_config *curr = &(cor_interface_config[i]);
93 BUG_ON(curr->name == 0);
94 kfree(curr->name);
95 curr->name = 0;
98 kfree(cor_interface_config);
99 cor_interface_config = 0;
100 cor_num_interfaces = 0;
101 cor_all_interfaces = 0;
104 cor_interface_config = new_config;
105 cor_num_interfaces = new_num_interfaces;
106 cor_all_interfaces = new_all_interfaces;
108 spin_unlock_irqrestore(&cor_interface_config_lock, iflags);
112 void _cor_config_down(void)
114 cor_dev_down();
116 spin_lock_bh(&cor_local_addr_lock);
117 if (cor_local_addr != 0) {
118 kfree(cor_local_addr);
119 cor_local_addr = 0;
121 cor_local_addrlen = 0;
122 spin_unlock_bh(&cor_local_addr_lock);
124 cor_reset_neighbors(0);
125 cor_reset_neighbors(0);
126 cor_destroy_queue(0);
128 cor_announce_send_stop(0, 0, ANNOUNCE_TYPE_BROADCAST);
131 void cor_config_down(void)
133 mutex_lock(&(cor_config_lock));
134 _cor_config_down();
135 mutex_unlock(&(cor_config_lock));
138 int cor_config_up(char *addr2, __u32 addrlen2)
140 int rc = 0;
142 char *addr2_copy = kmalloc(addrlen2, GFP_KERNEL);
143 if (unlikely(addr2_copy == 0))
144 return 1;
146 memcpy(addr2_copy, addr2, addrlen2);
148 mutex_lock(&(cor_config_lock));
150 _cor_config_down();
152 spin_lock_bh(&cor_local_addr_lock);
154 BUG_ON(cor_local_addr != 0);
155 BUG_ON(cor_local_addrlen != 0);
157 cor_local_addr = addr2_copy;
158 addr2_copy = 0;
159 cor_local_addrlen = addrlen2;
160 get_random_bytes((char *) &cor_local_addr_sessionid,
161 sizeof(cor_local_addr_sessionid));
163 spin_unlock_bh(&cor_local_addr_lock);
165 if (cor_dev_up() != 0) {
166 spin_lock_bh(&cor_local_addr_lock);
167 kfree(cor_local_addr);
168 cor_local_addr = 0;
169 cor_local_addrlen = 0;
170 spin_unlock_bh(&cor_local_addr_lock);
171 rc = 1;
174 mutex_unlock(&(cor_config_lock));
176 return rc;
179 int cor_is_clientmode(void)
181 int rc;
182 spin_lock_bh(&cor_local_addr_lock);
183 rc = (cor_local_addrlen == 0 ? 1 : 0);
184 spin_unlock_bh(&cor_local_addr_lock);
185 return rc;