This source file includes following definitions.
- batadv_algo_init
- batadv_algo_get
- batadv_algo_register
- batadv_algo_select
- batadv_algo_seq_print_text
- batadv_param_set_ra
- batadv_algo_dump_entry
- batadv_algo_dump
1
2
3
4
5
6
7 #include "main.h"
8
9 #include <linux/errno.h>
10 #include <linux/list.h>
11 #include <linux/moduleparam.h>
12 #include <linux/netlink.h>
13 #include <linux/printk.h>
14 #include <linux/seq_file.h>
15 #include <linux/skbuff.h>
16 #include <linux/stddef.h>
17 #include <linux/string.h>
18 #include <net/genetlink.h>
19 #include <net/netlink.h>
20 #include <uapi/linux/batman_adv.h>
21
22 #include "bat_algo.h"
23 #include "netlink.h"
24
25 char batadv_routing_algo[20] = "BATMAN_IV";
26 static struct hlist_head batadv_algo_list;
27
28
29
30
31
32 void batadv_algo_init(void)
33 {
34 INIT_HLIST_HEAD(&batadv_algo_list);
35 }
36
37 static struct batadv_algo_ops *batadv_algo_get(char *name)
38 {
39 struct batadv_algo_ops *bat_algo_ops = NULL, *bat_algo_ops_tmp;
40
41 hlist_for_each_entry(bat_algo_ops_tmp, &batadv_algo_list, list) {
42 if (strcmp(bat_algo_ops_tmp->name, name) != 0)
43 continue;
44
45 bat_algo_ops = bat_algo_ops_tmp;
46 break;
47 }
48
49 return bat_algo_ops;
50 }
51
52
53
54
55
56
57
58 int batadv_algo_register(struct batadv_algo_ops *bat_algo_ops)
59 {
60 struct batadv_algo_ops *bat_algo_ops_tmp;
61
62 bat_algo_ops_tmp = batadv_algo_get(bat_algo_ops->name);
63 if (bat_algo_ops_tmp) {
64 pr_info("Trying to register already registered routing algorithm: %s\n",
65 bat_algo_ops->name);
66 return -EEXIST;
67 }
68
69
70 if (!bat_algo_ops->iface.enable ||
71 !bat_algo_ops->iface.disable ||
72 !bat_algo_ops->iface.update_mac ||
73 !bat_algo_ops->iface.primary_set ||
74 !bat_algo_ops->neigh.cmp ||
75 !bat_algo_ops->neigh.is_similar_or_better) {
76 pr_info("Routing algo '%s' does not implement required ops\n",
77 bat_algo_ops->name);
78 return -EINVAL;
79 }
80
81 INIT_HLIST_NODE(&bat_algo_ops->list);
82 hlist_add_head(&bat_algo_ops->list, &batadv_algo_list);
83
84 return 0;
85 }
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100 int batadv_algo_select(struct batadv_priv *bat_priv, char *name)
101 {
102 struct batadv_algo_ops *bat_algo_ops;
103
104 bat_algo_ops = batadv_algo_get(name);
105 if (!bat_algo_ops)
106 return -EINVAL;
107
108 bat_priv->algo_ops = bat_algo_ops;
109
110 return 0;
111 }
112
113 #ifdef CONFIG_BATMAN_ADV_DEBUGFS
114
115
116
117
118
119
120
121
122 int batadv_algo_seq_print_text(struct seq_file *seq, void *offset)
123 {
124 struct batadv_algo_ops *bat_algo_ops;
125
126 seq_puts(seq, "Available routing algorithms:\n");
127
128 hlist_for_each_entry(bat_algo_ops, &batadv_algo_list, list) {
129 seq_printf(seq, " * %s\n", bat_algo_ops->name);
130 }
131
132 return 0;
133 }
134 #endif
135
136 static int batadv_param_set_ra(const char *val, const struct kernel_param *kp)
137 {
138 struct batadv_algo_ops *bat_algo_ops;
139 char *algo_name = (char *)val;
140 size_t name_len = strlen(algo_name);
141
142 if (name_len > 0 && algo_name[name_len - 1] == '\n')
143 algo_name[name_len - 1] = '\0';
144
145 bat_algo_ops = batadv_algo_get(algo_name);
146 if (!bat_algo_ops) {
147 pr_err("Routing algorithm '%s' is not supported\n", algo_name);
148 return -EINVAL;
149 }
150
151 return param_set_copystring(algo_name, kp);
152 }
153
154 static const struct kernel_param_ops batadv_param_ops_ra = {
155 .set = batadv_param_set_ra,
156 .get = param_get_string,
157 };
158
159 static struct kparam_string batadv_param_string_ra = {
160 .maxlen = sizeof(batadv_routing_algo),
161 .string = batadv_routing_algo,
162 };
163
164 module_param_cb(routing_algo, &batadv_param_ops_ra, &batadv_param_string_ra,
165 0644);
166
167
168
169
170
171
172
173
174
175
176
177 static int batadv_algo_dump_entry(struct sk_buff *msg, u32 portid, u32 seq,
178 struct batadv_algo_ops *bat_algo_ops)
179 {
180 void *hdr;
181
182 hdr = genlmsg_put(msg, portid, seq, &batadv_netlink_family,
183 NLM_F_MULTI, BATADV_CMD_GET_ROUTING_ALGOS);
184 if (!hdr)
185 return -EMSGSIZE;
186
187 if (nla_put_string(msg, BATADV_ATTR_ALGO_NAME, bat_algo_ops->name))
188 goto nla_put_failure;
189
190 genlmsg_end(msg, hdr);
191 return 0;
192
193 nla_put_failure:
194 genlmsg_cancel(msg, hdr);
195 return -EMSGSIZE;
196 }
197
198
199
200
201
202
203
204
205
206 int batadv_algo_dump(struct sk_buff *msg, struct netlink_callback *cb)
207 {
208 int portid = NETLINK_CB(cb->skb).portid;
209 struct batadv_algo_ops *bat_algo_ops;
210 int skip = cb->args[0];
211 int i = 0;
212
213 hlist_for_each_entry(bat_algo_ops, &batadv_algo_list, list) {
214 if (i++ < skip)
215 continue;
216
217 if (batadv_algo_dump_entry(msg, portid, cb->nlh->nlmsg_seq,
218 bat_algo_ops)) {
219 i--;
220 break;
221 }
222 }
223
224 cb->args[0] = i;
225
226 return msg->len;
227 }