Blame view

net/batman-adv/bat_algo.c 5.57 KB
7db7d9f36   Sven Eckelmann   batman-adv: Add S...
1
  // SPDX-License-Identifier: GPL-2.0
7a79d717e   Sven Eckelmann   batman-adv: Updat...
2
  /* Copyright (C) 2007-2019  B.A.T.M.A.N. contributors:
01d350d14   Sven Eckelmann   batman-adv: move ...
3
4
   *
   * Marek Lindner, Simon Wunderlich
01d350d14   Sven Eckelmann   batman-adv: move ...
5
6
7
8
9
10
11
   */
  
  #include "main.h"
  
  #include <linux/errno.h>
  #include <linux/list.h>
  #include <linux/moduleparam.h>
07a3061e0   Matthias Schiffer   batman-adv: netli...
12
  #include <linux/netlink.h>
01d350d14   Sven Eckelmann   batman-adv: move ...
13
14
  #include <linux/printk.h>
  #include <linux/seq_file.h>
07a3061e0   Matthias Schiffer   batman-adv: netli...
15
  #include <linux/skbuff.h>
01d350d14   Sven Eckelmann   batman-adv: move ...
16
17
  #include <linux/stddef.h>
  #include <linux/string.h>
07a3061e0   Matthias Schiffer   batman-adv: netli...
18
19
20
  #include <net/genetlink.h>
  #include <net/netlink.h>
  #include <uapi/linux/batman_adv.h>
01d350d14   Sven Eckelmann   batman-adv: move ...
21
22
  
  #include "bat_algo.h"
07a3061e0   Matthias Schiffer   batman-adv: netli...
23
  #include "netlink.h"
01d350d14   Sven Eckelmann   batman-adv: move ...
24
25
26
27
28
  
  char batadv_routing_algo[20] = "BATMAN_IV";
  static struct hlist_head batadv_algo_list;
  
  /**
7e9a8c2ce   Sven Eckelmann   batman-adv: Use p...
29
30
   * batadv_algo_init() - Initialize batman-adv algorithm management data
   *  structures
01d350d14   Sven Eckelmann   batman-adv: move ...
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
   */
  void batadv_algo_init(void)
  {
  	INIT_HLIST_HEAD(&batadv_algo_list);
  }
  
  static struct batadv_algo_ops *batadv_algo_get(char *name)
  {
  	struct batadv_algo_ops *bat_algo_ops = NULL, *bat_algo_ops_tmp;
  
  	hlist_for_each_entry(bat_algo_ops_tmp, &batadv_algo_list, list) {
  		if (strcmp(bat_algo_ops_tmp->name, name) != 0)
  			continue;
  
  		bat_algo_ops = bat_algo_ops_tmp;
  		break;
  	}
  
  	return bat_algo_ops;
  }
ff15c27c9   Sven Eckelmann   batman-adv: Add k...
51
52
53
54
55
56
  /**
   * batadv_algo_register() - Register callbacks for a mesh algorithm
   * @bat_algo_ops: mesh algorithm callbacks to add
   *
   * Return: 0 on success or negative error number in case of failure
   */
01d350d14   Sven Eckelmann   batman-adv: move ...
57
58
59
60
61
62
63
64
65
66
67
68
69
  int batadv_algo_register(struct batadv_algo_ops *bat_algo_ops)
  {
  	struct batadv_algo_ops *bat_algo_ops_tmp;
  
  	bat_algo_ops_tmp = batadv_algo_get(bat_algo_ops->name);
  	if (bat_algo_ops_tmp) {
  		pr_info("Trying to register already registered routing algorithm: %s
  ",
  			bat_algo_ops->name);
  		return -EEXIST;
  	}
  
  	/* all algorithms must implement all ops (for now) */
29824a55c   Antonio Quartulli   batman-adv: split...
70
71
72
73
74
75
  	if (!bat_algo_ops->iface.enable ||
  	    !bat_algo_ops->iface.disable ||
  	    !bat_algo_ops->iface.update_mac ||
  	    !bat_algo_ops->iface.primary_set ||
  	    !bat_algo_ops->neigh.cmp ||
  	    !bat_algo_ops->neigh.is_similar_or_better) {
01d350d14   Sven Eckelmann   batman-adv: move ...
76
77
78
79
80
81
82
83
84
85
86
  		pr_info("Routing algo '%s' does not implement required ops
  ",
  			bat_algo_ops->name);
  		return -EINVAL;
  	}
  
  	INIT_HLIST_NODE(&bat_algo_ops->list);
  	hlist_add_head(&bat_algo_ops->list, &batadv_algo_list);
  
  	return 0;
  }
ff15c27c9   Sven Eckelmann   batman-adv: Add k...
87
88
89
90
91
92
93
94
95
96
97
98
99
  /**
   * batadv_algo_select() - Select algorithm of soft interface
   * @bat_priv: the bat priv with all the soft interface information
   * @name: name of the algorithm to select
   *
   * The algorithm callbacks for the soft interface will be set when the algorithm
   * with the correct name was found. Any previous selected algorithm will not be
   * deinitialized and the new selected algorithm will also not be initialized.
   * It is therefore not allowed to call batadv_algo_select outside the creation
   * function of the soft interface.
   *
   * Return: 0 on success or negative error number in case of failure
   */
01d350d14   Sven Eckelmann   batman-adv: move ...
100
101
102
103
104
105
106
  int batadv_algo_select(struct batadv_priv *bat_priv, char *name)
  {
  	struct batadv_algo_ops *bat_algo_ops;
  
  	bat_algo_ops = batadv_algo_get(name);
  	if (!bat_algo_ops)
  		return -EINVAL;
29824a55c   Antonio Quartulli   batman-adv: split...
107
  	bat_priv->algo_ops = bat_algo_ops;
01d350d14   Sven Eckelmann   batman-adv: move ...
108
109
110
  
  	return 0;
  }
dc1cbd145   Sven Eckelmann   batman-adv: Allow...
111
  #ifdef CONFIG_BATMAN_ADV_DEBUGFS
ff15c27c9   Sven Eckelmann   batman-adv: Add k...
112
113
114
115
116
117
118
119
  
  /**
   * batadv_algo_seq_print_text() - Print the supported algorithms in a seq file
   * @seq: seq file to print on
   * @offset: not used
   *
   * Return: always 0
   */
01d350d14   Sven Eckelmann   batman-adv: move ...
120
121
122
123
124
125
126
127
128
129
130
131
132
133
  int batadv_algo_seq_print_text(struct seq_file *seq, void *offset)
  {
  	struct batadv_algo_ops *bat_algo_ops;
  
  	seq_puts(seq, "Available routing algorithms:
  ");
  
  	hlist_for_each_entry(bat_algo_ops, &batadv_algo_list, list) {
  		seq_printf(seq, " * %s
  ", bat_algo_ops->name);
  	}
  
  	return 0;
  }
dc1cbd145   Sven Eckelmann   batman-adv: Allow...
134
  #endif
01d350d14   Sven Eckelmann   batman-adv: move ...
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
  
  static int batadv_param_set_ra(const char *val, const struct kernel_param *kp)
  {
  	struct batadv_algo_ops *bat_algo_ops;
  	char *algo_name = (char *)val;
  	size_t name_len = strlen(algo_name);
  
  	if (name_len > 0 && algo_name[name_len - 1] == '
  ')
  		algo_name[name_len - 1] = '\0';
  
  	bat_algo_ops = batadv_algo_get(algo_name);
  	if (!bat_algo_ops) {
  		pr_err("Routing algorithm '%s' is not supported
  ", algo_name);
  		return -EINVAL;
  	}
  
  	return param_set_copystring(algo_name, kp);
  }
  
  static const struct kernel_param_ops batadv_param_ops_ra = {
  	.set = batadv_param_set_ra,
  	.get = param_get_string,
  };
  
  static struct kparam_string batadv_param_string_ra = {
  	.maxlen = sizeof(batadv_routing_algo),
  	.string = batadv_routing_algo,
  };
  
  module_param_cb(routing_algo, &batadv_param_ops_ra, &batadv_param_string_ra,
  		0644);
07a3061e0   Matthias Schiffer   batman-adv: netli...
168
169
  
  /**
7e9a8c2ce   Sven Eckelmann   batman-adv: Use p...
170
   * batadv_algo_dump_entry() - fill in information about one supported routing
07a3061e0   Matthias Schiffer   batman-adv: netli...
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
   *  algorithm
   * @msg: netlink message to be sent back
   * @portid: Port to reply to
   * @seq: Sequence number of message
   * @bat_algo_ops: Algorithm to be dumped
   *
   * Return: Error number, or 0 on success
   */
  static int batadv_algo_dump_entry(struct sk_buff *msg, u32 portid, u32 seq,
  				  struct batadv_algo_ops *bat_algo_ops)
  {
  	void *hdr;
  
  	hdr = genlmsg_put(msg, portid, seq, &batadv_netlink_family,
  			  NLM_F_MULTI, BATADV_CMD_GET_ROUTING_ALGOS);
  	if (!hdr)
  		return -EMSGSIZE;
  
  	if (nla_put_string(msg, BATADV_ATTR_ALGO_NAME, bat_algo_ops->name))
  		goto nla_put_failure;
  
  	genlmsg_end(msg, hdr);
  	return 0;
  
   nla_put_failure:
  	genlmsg_cancel(msg, hdr);
  	return -EMSGSIZE;
  }
  
  /**
7e9a8c2ce   Sven Eckelmann   batman-adv: Use p...
201
   * batadv_algo_dump() - fill in information about supported routing
07a3061e0   Matthias Schiffer   batman-adv: netli...
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
   *  algorithms
   * @msg: netlink message to be sent back
   * @cb: Parameters to the netlink request
   *
   * Return: Length of reply message.
   */
  int batadv_algo_dump(struct sk_buff *msg, struct netlink_callback *cb)
  {
  	int portid = NETLINK_CB(cb->skb).portid;
  	struct batadv_algo_ops *bat_algo_ops;
  	int skip = cb->args[0];
  	int i = 0;
  
  	hlist_for_each_entry(bat_algo_ops, &batadv_algo_list, list) {
  		if (i++ < skip)
  			continue;
  
  		if (batadv_algo_dump_entry(msg, portid, cb->nlh->nlmsg_seq,
  					   bat_algo_ops)) {
  			i--;
  			break;
  		}
  	}
  
  	cb->args[0] = i;
  
  	return msg->len;
  }