checkpatch fixes
[cor.git] / net / cor / config.c
blobaeb598bd3d76920bf88b0860a21414e9149364b3
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.
16 #include "cor.h"
18 static DEFINE_MUTEX(cor_config_lock);
20 DEFINE_SPINLOCK(cor_local_addr_lock);
21 __u8 cor_local_has_addr = 0;
22 __be64 cor_local_addr;
23 __be32 cor_local_addr_sessionid;
26 static DEFINE_SPINLOCK(cor_interface_config_lock);
27 static struct cor_interface_config *cor_interface_config = 0;
28 static __u32 cor_num_interfaces = 0;
29 static int cor_all_interfaces = 0;
32 int cor_is_device_configurated(struct net_device *dev)
34 int ret = 0;
36 unsigned long iflags;
38 __u32 i;
40 spin_lock_irqsave(&cor_interface_config_lock, iflags);
42 if (cor_all_interfaces != 0) {
43 ret = 1;
44 goto out;
47 BUG_ON(cor_num_interfaces > 65536);
48 for (i = 0; i < cor_num_interfaces; i++) {
49 struct cor_interface_config *curr = &(cor_interface_config[i]);
50 __u32 j;
52 BUG_ON(curr->name == 0);
54 for (j = 0;; j++) {
55 if (j >= sizeof(dev->name))
56 break;
58 if (dev->name[j] == 0 && j == curr->name_len) {
59 ret = 1;
60 goto out;
63 if (dev->name[j] == 0 || j >= curr->name_len)
64 break;
66 if (dev->name[j] != curr->name[j])
67 break;
71 out:
72 spin_unlock_irqrestore(&cor_interface_config_lock, iflags);
74 return ret;
77 void cor_set_interface_config(struct cor_interface_config *new_config,
78 __u32 new_num_interfaces, int new_all_interfaces)
80 unsigned long iflags;
81 __u32 i;
83 spin_lock_irqsave(&cor_interface_config_lock, iflags);
85 BUG_ON(cor_num_interfaces > 65536);
86 for (i = 0; i < cor_num_interfaces; i++) {
87 struct cor_interface_config *curr = &(cor_interface_config[i]);
89 BUG_ON(curr->name == 0);
90 kfree(curr->name);
91 curr->name = 0;
94 kfree(cor_interface_config);
95 cor_interface_config = 0;
96 cor_num_interfaces = 0;
97 cor_all_interfaces = 0;
100 cor_interface_config = new_config;
101 cor_num_interfaces = new_num_interfaces;
102 cor_all_interfaces = new_all_interfaces;
104 spin_unlock_irqrestore(&cor_interface_config_lock, iflags);
108 void _cor_config_down(void)
110 cor_dev_down();
112 spin_lock_bh(&cor_local_addr_lock);
113 cor_local_has_addr = 0;
114 cor_local_addr = 0;
115 spin_unlock_bh(&cor_local_addr_lock);
117 cor_reset_neighbors(0);
118 cor_reset_neighbors(0);
119 cor_destroy_queue(0);
121 cor_announce_send_stop(0, 0, ANNOUNCE_TYPE_BROADCAST);
124 void cor_config_down(void)
126 mutex_lock(&cor_config_lock);
127 _cor_config_down();
128 mutex_unlock(&cor_config_lock);
131 int cor_config_up(__u8 has_addr, __be64 addr)
133 int rc = 0;
135 if (has_addr != 0 && be64_to_cpu(addr) == 0)
136 return 1;
138 mutex_lock(&cor_config_lock);
140 _cor_config_down();
142 spin_lock_bh(&cor_local_addr_lock);
144 BUG_ON(cor_local_has_addr != 0);
145 BUG_ON(cor_local_addr != 0);
147 cor_local_has_addr = has_addr;
148 cor_local_addr = addr;
149 get_random_bytes((char *) &cor_local_addr_sessionid,
150 sizeof(cor_local_addr_sessionid));
152 spin_unlock_bh(&cor_local_addr_lock);
154 if (cor_dev_up() != 0) {
155 spin_lock_bh(&cor_local_addr_lock);
156 cor_local_has_addr = 0;
157 cor_local_addr = 0;
158 spin_unlock_bh(&cor_local_addr_lock);
159 rc = 1;
162 mutex_unlock(&(cor_config_lock));
164 return rc;
167 int cor_is_clientmode(void)
169 int rc;
171 spin_lock_bh(&cor_local_addr_lock);
172 rc = (cor_local_has_addr == 0);
173 spin_unlock_bh(&cor_local_addr_lock);
174 return rc;
177 MODULE_LICENSE("GPL");