17db7d9f3SSven Eckelmann // SPDX-License-Identifier: GPL-2.0 2*cfa55c6dSSven Eckelmann /* Copyright (C) B.A.T.M.A.N. contributors: 301d350d1SSven Eckelmann * 401d350d1SSven Eckelmann * Marek Lindner, Simon Wunderlich 501d350d1SSven Eckelmann */ 601d350d1SSven Eckelmann 701d350d1SSven Eckelmann #include "main.h" 801d350d1SSven Eckelmann 901d350d1SSven Eckelmann #include <linux/errno.h> 1001d350d1SSven Eckelmann #include <linux/list.h> 1101d350d1SSven Eckelmann #include <linux/moduleparam.h> 1207a3061eSMatthias Schiffer #include <linux/netlink.h> 1301d350d1SSven Eckelmann #include <linux/printk.h> 1407a3061eSMatthias Schiffer #include <linux/skbuff.h> 1501d350d1SSven Eckelmann #include <linux/stddef.h> 1601d350d1SSven Eckelmann #include <linux/string.h> 1707a3061eSMatthias Schiffer #include <net/genetlink.h> 1807a3061eSMatthias Schiffer #include <net/netlink.h> 1907a3061eSMatthias Schiffer #include <uapi/linux/batman_adv.h> 2001d350d1SSven Eckelmann 2101d350d1SSven Eckelmann #include "bat_algo.h" 2207a3061eSMatthias Schiffer #include "netlink.h" 2301d350d1SSven Eckelmann 2401d350d1SSven Eckelmann char batadv_routing_algo[20] = "BATMAN_IV"; 2501d350d1SSven Eckelmann static struct hlist_head batadv_algo_list; 2601d350d1SSven Eckelmann 2701d350d1SSven Eckelmann /** 287e9a8c2cSSven Eckelmann * batadv_algo_init() - Initialize batman-adv algorithm management data 297e9a8c2cSSven Eckelmann * structures 3001d350d1SSven Eckelmann */ 3101d350d1SSven Eckelmann void batadv_algo_init(void) 3201d350d1SSven Eckelmann { 3301d350d1SSven Eckelmann INIT_HLIST_HEAD(&batadv_algo_list); 3401d350d1SSven Eckelmann } 3501d350d1SSven Eckelmann 36a5ad457eSSven Eckelmann /** 37a5ad457eSSven Eckelmann * batadv_algo_get() - Search for algorithm with specific name 38a5ad457eSSven Eckelmann * @name: algorithm name to find 39a5ad457eSSven Eckelmann * 40a5ad457eSSven Eckelmann * Return: Pointer to batadv_algo_ops on success, NULL otherwise 41a5ad457eSSven Eckelmann */ 42a5ad457eSSven Eckelmann struct batadv_algo_ops *batadv_algo_get(const char *name) 4301d350d1SSven Eckelmann { 4401d350d1SSven Eckelmann struct batadv_algo_ops *bat_algo_ops = NULL, *bat_algo_ops_tmp; 4501d350d1SSven Eckelmann 4601d350d1SSven Eckelmann hlist_for_each_entry(bat_algo_ops_tmp, &batadv_algo_list, list) { 4701d350d1SSven Eckelmann if (strcmp(bat_algo_ops_tmp->name, name) != 0) 4801d350d1SSven Eckelmann continue; 4901d350d1SSven Eckelmann 5001d350d1SSven Eckelmann bat_algo_ops = bat_algo_ops_tmp; 5101d350d1SSven Eckelmann break; 5201d350d1SSven Eckelmann } 5301d350d1SSven Eckelmann 5401d350d1SSven Eckelmann return bat_algo_ops; 5501d350d1SSven Eckelmann } 5601d350d1SSven Eckelmann 57ff15c27cSSven Eckelmann /** 58ff15c27cSSven Eckelmann * batadv_algo_register() - Register callbacks for a mesh algorithm 59ff15c27cSSven Eckelmann * @bat_algo_ops: mesh algorithm callbacks to add 60ff15c27cSSven Eckelmann * 61ff15c27cSSven Eckelmann * Return: 0 on success or negative error number in case of failure 62ff15c27cSSven Eckelmann */ 6301d350d1SSven Eckelmann int batadv_algo_register(struct batadv_algo_ops *bat_algo_ops) 6401d350d1SSven Eckelmann { 6501d350d1SSven Eckelmann struct batadv_algo_ops *bat_algo_ops_tmp; 6601d350d1SSven Eckelmann 6701d350d1SSven Eckelmann bat_algo_ops_tmp = batadv_algo_get(bat_algo_ops->name); 6801d350d1SSven Eckelmann if (bat_algo_ops_tmp) { 6901d350d1SSven Eckelmann pr_info("Trying to register already registered routing algorithm: %s\n", 7001d350d1SSven Eckelmann bat_algo_ops->name); 7101d350d1SSven Eckelmann return -EEXIST; 7201d350d1SSven Eckelmann } 7301d350d1SSven Eckelmann 7401d350d1SSven Eckelmann /* all algorithms must implement all ops (for now) */ 7529824a55SAntonio Quartulli if (!bat_algo_ops->iface.enable || 7629824a55SAntonio Quartulli !bat_algo_ops->iface.disable || 7729824a55SAntonio Quartulli !bat_algo_ops->iface.update_mac || 7829824a55SAntonio Quartulli !bat_algo_ops->iface.primary_set || 7929824a55SAntonio Quartulli !bat_algo_ops->neigh.cmp || 8029824a55SAntonio Quartulli !bat_algo_ops->neigh.is_similar_or_better) { 8101d350d1SSven Eckelmann pr_info("Routing algo '%s' does not implement required ops\n", 8201d350d1SSven Eckelmann bat_algo_ops->name); 8301d350d1SSven Eckelmann return -EINVAL; 8401d350d1SSven Eckelmann } 8501d350d1SSven Eckelmann 8601d350d1SSven Eckelmann INIT_HLIST_NODE(&bat_algo_ops->list); 8701d350d1SSven Eckelmann hlist_add_head(&bat_algo_ops->list, &batadv_algo_list); 8801d350d1SSven Eckelmann 8901d350d1SSven Eckelmann return 0; 9001d350d1SSven Eckelmann } 9101d350d1SSven Eckelmann 92ff15c27cSSven Eckelmann /** 93ff15c27cSSven Eckelmann * batadv_algo_select() - Select algorithm of soft interface 94ff15c27cSSven Eckelmann * @bat_priv: the bat priv with all the soft interface information 95ff15c27cSSven Eckelmann * @name: name of the algorithm to select 96ff15c27cSSven Eckelmann * 97ff15c27cSSven Eckelmann * The algorithm callbacks for the soft interface will be set when the algorithm 98ff15c27cSSven Eckelmann * with the correct name was found. Any previous selected algorithm will not be 99ff15c27cSSven Eckelmann * deinitialized and the new selected algorithm will also not be initialized. 100ff15c27cSSven Eckelmann * It is therefore not allowed to call batadv_algo_select outside the creation 101ff15c27cSSven Eckelmann * function of the soft interface. 102ff15c27cSSven Eckelmann * 103ff15c27cSSven Eckelmann * Return: 0 on success or negative error number in case of failure 104ff15c27cSSven Eckelmann */ 105a5ad457eSSven Eckelmann int batadv_algo_select(struct batadv_priv *bat_priv, const char *name) 10601d350d1SSven Eckelmann { 10701d350d1SSven Eckelmann struct batadv_algo_ops *bat_algo_ops; 10801d350d1SSven Eckelmann 10901d350d1SSven Eckelmann bat_algo_ops = batadv_algo_get(name); 11001d350d1SSven Eckelmann if (!bat_algo_ops) 11101d350d1SSven Eckelmann return -EINVAL; 11201d350d1SSven Eckelmann 11329824a55SAntonio Quartulli bat_priv->algo_ops = bat_algo_ops; 11401d350d1SSven Eckelmann 11501d350d1SSven Eckelmann return 0; 11601d350d1SSven Eckelmann } 11701d350d1SSven Eckelmann 11801d350d1SSven Eckelmann static int batadv_param_set_ra(const char *val, const struct kernel_param *kp) 11901d350d1SSven Eckelmann { 12001d350d1SSven Eckelmann struct batadv_algo_ops *bat_algo_ops; 12101d350d1SSven Eckelmann char *algo_name = (char *)val; 12201d350d1SSven Eckelmann size_t name_len = strlen(algo_name); 12301d350d1SSven Eckelmann 12401d350d1SSven Eckelmann if (name_len > 0 && algo_name[name_len - 1] == '\n') 12501d350d1SSven Eckelmann algo_name[name_len - 1] = '\0'; 12601d350d1SSven Eckelmann 12701d350d1SSven Eckelmann bat_algo_ops = batadv_algo_get(algo_name); 12801d350d1SSven Eckelmann if (!bat_algo_ops) { 12901d350d1SSven Eckelmann pr_err("Routing algorithm '%s' is not supported\n", algo_name); 13001d350d1SSven Eckelmann return -EINVAL; 13101d350d1SSven Eckelmann } 13201d350d1SSven Eckelmann 13301d350d1SSven Eckelmann return param_set_copystring(algo_name, kp); 13401d350d1SSven Eckelmann } 13501d350d1SSven Eckelmann 13601d350d1SSven Eckelmann static const struct kernel_param_ops batadv_param_ops_ra = { 13701d350d1SSven Eckelmann .set = batadv_param_set_ra, 13801d350d1SSven Eckelmann .get = param_get_string, 13901d350d1SSven Eckelmann }; 14001d350d1SSven Eckelmann 14101d350d1SSven Eckelmann static struct kparam_string batadv_param_string_ra = { 14201d350d1SSven Eckelmann .maxlen = sizeof(batadv_routing_algo), 14301d350d1SSven Eckelmann .string = batadv_routing_algo, 14401d350d1SSven Eckelmann }; 14501d350d1SSven Eckelmann 14601d350d1SSven Eckelmann module_param_cb(routing_algo, &batadv_param_ops_ra, &batadv_param_string_ra, 14701d350d1SSven Eckelmann 0644); 14807a3061eSMatthias Schiffer 14907a3061eSMatthias Schiffer /** 1507e9a8c2cSSven Eckelmann * batadv_algo_dump_entry() - fill in information about one supported routing 15107a3061eSMatthias Schiffer * algorithm 15207a3061eSMatthias Schiffer * @msg: netlink message to be sent back 15307a3061eSMatthias Schiffer * @portid: Port to reply to 15407a3061eSMatthias Schiffer * @seq: Sequence number of message 15507a3061eSMatthias Schiffer * @bat_algo_ops: Algorithm to be dumped 15607a3061eSMatthias Schiffer * 15707a3061eSMatthias Schiffer * Return: Error number, or 0 on success 15807a3061eSMatthias Schiffer */ 15907a3061eSMatthias Schiffer static int batadv_algo_dump_entry(struct sk_buff *msg, u32 portid, u32 seq, 16007a3061eSMatthias Schiffer struct batadv_algo_ops *bat_algo_ops) 16107a3061eSMatthias Schiffer { 16207a3061eSMatthias Schiffer void *hdr; 16307a3061eSMatthias Schiffer 16407a3061eSMatthias Schiffer hdr = genlmsg_put(msg, portid, seq, &batadv_netlink_family, 16507a3061eSMatthias Schiffer NLM_F_MULTI, BATADV_CMD_GET_ROUTING_ALGOS); 16607a3061eSMatthias Schiffer if (!hdr) 16707a3061eSMatthias Schiffer return -EMSGSIZE; 16807a3061eSMatthias Schiffer 16907a3061eSMatthias Schiffer if (nla_put_string(msg, BATADV_ATTR_ALGO_NAME, bat_algo_ops->name)) 17007a3061eSMatthias Schiffer goto nla_put_failure; 17107a3061eSMatthias Schiffer 17207a3061eSMatthias Schiffer genlmsg_end(msg, hdr); 17307a3061eSMatthias Schiffer return 0; 17407a3061eSMatthias Schiffer 17507a3061eSMatthias Schiffer nla_put_failure: 17607a3061eSMatthias Schiffer genlmsg_cancel(msg, hdr); 17707a3061eSMatthias Schiffer return -EMSGSIZE; 17807a3061eSMatthias Schiffer } 17907a3061eSMatthias Schiffer 18007a3061eSMatthias Schiffer /** 1817e9a8c2cSSven Eckelmann * batadv_algo_dump() - fill in information about supported routing 18207a3061eSMatthias Schiffer * algorithms 18307a3061eSMatthias Schiffer * @msg: netlink message to be sent back 18407a3061eSMatthias Schiffer * @cb: Parameters to the netlink request 18507a3061eSMatthias Schiffer * 18607a3061eSMatthias Schiffer * Return: Length of reply message. 18707a3061eSMatthias Schiffer */ 18807a3061eSMatthias Schiffer int batadv_algo_dump(struct sk_buff *msg, struct netlink_callback *cb) 18907a3061eSMatthias Schiffer { 19007a3061eSMatthias Schiffer int portid = NETLINK_CB(cb->skb).portid; 19107a3061eSMatthias Schiffer struct batadv_algo_ops *bat_algo_ops; 19207a3061eSMatthias Schiffer int skip = cb->args[0]; 19307a3061eSMatthias Schiffer int i = 0; 19407a3061eSMatthias Schiffer 19507a3061eSMatthias Schiffer hlist_for_each_entry(bat_algo_ops, &batadv_algo_list, list) { 19607a3061eSMatthias Schiffer if (i++ < skip) 19707a3061eSMatthias Schiffer continue; 19807a3061eSMatthias Schiffer 19907a3061eSMatthias Schiffer if (batadv_algo_dump_entry(msg, portid, cb->nlh->nlmsg_seq, 20007a3061eSMatthias Schiffer bat_algo_ops)) { 20107a3061eSMatthias Schiffer i--; 20207a3061eSMatthias Schiffer break; 20307a3061eSMatthias Schiffer } 20407a3061eSMatthias Schiffer } 20507a3061eSMatthias Schiffer 20607a3061eSMatthias Schiffer cb->args[0] = i; 20707a3061eSMatthias Schiffer 20807a3061eSMatthias Schiffer return msg->len; 20907a3061eSMatthias Schiffer } 210