1 // SPDX-License-Identifier: GPL-2.0 2 /* Copyright (C) 2007-2020 B.A.T.M.A.N. contributors: 3 * 4 * Marek Lindner, Simon Wunderlich 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 * batadv_algo_init() - Initialize batman-adv algorithm management data 30 * structures 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 * batadv_algo_register() - Register callbacks for a mesh algorithm 54 * @bat_algo_ops: mesh algorithm callbacks to add 55 * 56 * Return: 0 on success or negative error number in case of failure 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 /* all algorithms must implement all ops (for now) */ 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 * batadv_algo_select() - Select algorithm of soft interface 89 * @bat_priv: the bat priv with all the soft interface information 90 * @name: name of the algorithm to select 91 * 92 * The algorithm callbacks for the soft interface will be set when the algorithm 93 * with the correct name was found. Any previous selected algorithm will not be 94 * deinitialized and the new selected algorithm will also not be initialized. 95 * It is therefore not allowed to call batadv_algo_select outside the creation 96 * function of the soft interface. 97 * 98 * Return: 0 on success or negative error number in case of failure 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 * batadv_algo_seq_print_text() - Print the supported algorithms in a seq file 117 * @seq: seq file to print on 118 * @offset: not used 119 * 120 * Return: always 0 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 * batadv_algo_dump_entry() - fill in information about one supported routing 169 * algorithm 170 * @msg: netlink message to be sent back 171 * @portid: Port to reply to 172 * @seq: Sequence number of message 173 * @bat_algo_ops: Algorithm to be dumped 174 * 175 * Return: Error number, or 0 on success 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 * batadv_algo_dump() - fill in information about supported routing 200 * algorithms 201 * @msg: netlink message to be sent back 202 * @cb: Parameters to the netlink request 203 * 204 * Return: Length of reply message. 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 } 228