1*7db7d9f3SSven Eckelmann // SPDX-License-Identifier: GPL-2.0 2ac79cbb9SSven Eckelmann /* Copyright (C) 2007-2017 B.A.T.M.A.N. contributors: 301d350d1SSven Eckelmann * 401d350d1SSven Eckelmann * Marek Lindner, Simon Wunderlich 501d350d1SSven Eckelmann * 601d350d1SSven Eckelmann * This program is free software; you can redistribute it and/or 701d350d1SSven Eckelmann * modify it under the terms of version 2 of the GNU General Public 801d350d1SSven Eckelmann * License as published by the Free Software Foundation. 901d350d1SSven Eckelmann * 1001d350d1SSven Eckelmann * This program is distributed in the hope that it will be useful, but 1101d350d1SSven Eckelmann * WITHOUT ANY WARRANTY; without even the implied warranty of 1201d350d1SSven Eckelmann * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 1301d350d1SSven Eckelmann * General Public License for more details. 1401d350d1SSven Eckelmann * 1501d350d1SSven Eckelmann * You should have received a copy of the GNU General Public License 1601d350d1SSven Eckelmann * along with this program; if not, see <http://www.gnu.org/licenses/>. 1701d350d1SSven Eckelmann */ 1801d350d1SSven Eckelmann 1901d350d1SSven Eckelmann #include "main.h" 2001d350d1SSven Eckelmann 2101d350d1SSven Eckelmann #include <linux/errno.h> 2201d350d1SSven Eckelmann #include <linux/list.h> 2301d350d1SSven Eckelmann #include <linux/moduleparam.h> 2407a3061eSMatthias Schiffer #include <linux/netlink.h> 2501d350d1SSven Eckelmann #include <linux/printk.h> 2601d350d1SSven Eckelmann #include <linux/seq_file.h> 2707a3061eSMatthias Schiffer #include <linux/skbuff.h> 2801d350d1SSven Eckelmann #include <linux/stddef.h> 2901d350d1SSven Eckelmann #include <linux/string.h> 3007a3061eSMatthias Schiffer #include <net/genetlink.h> 3107a3061eSMatthias Schiffer #include <net/netlink.h> 3207a3061eSMatthias Schiffer #include <uapi/linux/batman_adv.h> 3301d350d1SSven Eckelmann 3401d350d1SSven Eckelmann #include "bat_algo.h" 3507a3061eSMatthias Schiffer #include "netlink.h" 3601d350d1SSven Eckelmann 3701d350d1SSven Eckelmann char batadv_routing_algo[20] = "BATMAN_IV"; 3801d350d1SSven Eckelmann static struct hlist_head batadv_algo_list; 3901d350d1SSven Eckelmann 4001d350d1SSven Eckelmann /** 4101d350d1SSven Eckelmann * batadv_algo_init - Initialize batman-adv algorithm management data structures 4201d350d1SSven Eckelmann */ 4301d350d1SSven Eckelmann void batadv_algo_init(void) 4401d350d1SSven Eckelmann { 4501d350d1SSven Eckelmann INIT_HLIST_HEAD(&batadv_algo_list); 4601d350d1SSven Eckelmann } 4701d350d1SSven Eckelmann 4801d350d1SSven Eckelmann static struct batadv_algo_ops *batadv_algo_get(char *name) 4901d350d1SSven Eckelmann { 5001d350d1SSven Eckelmann struct batadv_algo_ops *bat_algo_ops = NULL, *bat_algo_ops_tmp; 5101d350d1SSven Eckelmann 5201d350d1SSven Eckelmann hlist_for_each_entry(bat_algo_ops_tmp, &batadv_algo_list, list) { 5301d350d1SSven Eckelmann if (strcmp(bat_algo_ops_tmp->name, name) != 0) 5401d350d1SSven Eckelmann continue; 5501d350d1SSven Eckelmann 5601d350d1SSven Eckelmann bat_algo_ops = bat_algo_ops_tmp; 5701d350d1SSven Eckelmann break; 5801d350d1SSven Eckelmann } 5901d350d1SSven Eckelmann 6001d350d1SSven Eckelmann return bat_algo_ops; 6101d350d1SSven Eckelmann } 6201d350d1SSven 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 9201d350d1SSven Eckelmann int batadv_algo_select(struct batadv_priv *bat_priv, char *name) 9301d350d1SSven Eckelmann { 9401d350d1SSven Eckelmann struct batadv_algo_ops *bat_algo_ops; 9501d350d1SSven Eckelmann 9601d350d1SSven Eckelmann bat_algo_ops = batadv_algo_get(name); 9701d350d1SSven Eckelmann if (!bat_algo_ops) 9801d350d1SSven Eckelmann return -EINVAL; 9901d350d1SSven Eckelmann 10029824a55SAntonio Quartulli bat_priv->algo_ops = bat_algo_ops; 10101d350d1SSven Eckelmann 10201d350d1SSven Eckelmann return 0; 10301d350d1SSven Eckelmann } 10401d350d1SSven Eckelmann 105dc1cbd14SSven Eckelmann #ifdef CONFIG_BATMAN_ADV_DEBUGFS 10601d350d1SSven Eckelmann int batadv_algo_seq_print_text(struct seq_file *seq, void *offset) 10701d350d1SSven Eckelmann { 10801d350d1SSven Eckelmann struct batadv_algo_ops *bat_algo_ops; 10901d350d1SSven Eckelmann 11001d350d1SSven Eckelmann seq_puts(seq, "Available routing algorithms:\n"); 11101d350d1SSven Eckelmann 11201d350d1SSven Eckelmann hlist_for_each_entry(bat_algo_ops, &batadv_algo_list, list) { 11301d350d1SSven Eckelmann seq_printf(seq, " * %s\n", bat_algo_ops->name); 11401d350d1SSven Eckelmann } 11501d350d1SSven Eckelmann 11601d350d1SSven Eckelmann return 0; 11701d350d1SSven Eckelmann } 118dc1cbd14SSven Eckelmann #endif 11901d350d1SSven Eckelmann 12001d350d1SSven Eckelmann static int batadv_param_set_ra(const char *val, const struct kernel_param *kp) 12101d350d1SSven Eckelmann { 12201d350d1SSven Eckelmann struct batadv_algo_ops *bat_algo_ops; 12301d350d1SSven Eckelmann char *algo_name = (char *)val; 12401d350d1SSven Eckelmann size_t name_len = strlen(algo_name); 12501d350d1SSven Eckelmann 12601d350d1SSven Eckelmann if (name_len > 0 && algo_name[name_len - 1] == '\n') 12701d350d1SSven Eckelmann algo_name[name_len - 1] = '\0'; 12801d350d1SSven Eckelmann 12901d350d1SSven Eckelmann bat_algo_ops = batadv_algo_get(algo_name); 13001d350d1SSven Eckelmann if (!bat_algo_ops) { 13101d350d1SSven Eckelmann pr_err("Routing algorithm '%s' is not supported\n", algo_name); 13201d350d1SSven Eckelmann return -EINVAL; 13301d350d1SSven Eckelmann } 13401d350d1SSven Eckelmann 13501d350d1SSven Eckelmann return param_set_copystring(algo_name, kp); 13601d350d1SSven Eckelmann } 13701d350d1SSven Eckelmann 13801d350d1SSven Eckelmann static const struct kernel_param_ops batadv_param_ops_ra = { 13901d350d1SSven Eckelmann .set = batadv_param_set_ra, 14001d350d1SSven Eckelmann .get = param_get_string, 14101d350d1SSven Eckelmann }; 14201d350d1SSven Eckelmann 14301d350d1SSven Eckelmann static struct kparam_string batadv_param_string_ra = { 14401d350d1SSven Eckelmann .maxlen = sizeof(batadv_routing_algo), 14501d350d1SSven Eckelmann .string = batadv_routing_algo, 14601d350d1SSven Eckelmann }; 14701d350d1SSven Eckelmann 14801d350d1SSven Eckelmann module_param_cb(routing_algo, &batadv_param_ops_ra, &batadv_param_string_ra, 14901d350d1SSven Eckelmann 0644); 15007a3061eSMatthias Schiffer 15107a3061eSMatthias Schiffer /** 15207a3061eSMatthias Schiffer * batadv_algo_dump_entry - fill in information about one supported routing 15307a3061eSMatthias Schiffer * algorithm 15407a3061eSMatthias Schiffer * @msg: netlink message to be sent back 15507a3061eSMatthias Schiffer * @portid: Port to reply to 15607a3061eSMatthias Schiffer * @seq: Sequence number of message 15707a3061eSMatthias Schiffer * @bat_algo_ops: Algorithm to be dumped 15807a3061eSMatthias Schiffer * 15907a3061eSMatthias Schiffer * Return: Error number, or 0 on success 16007a3061eSMatthias Schiffer */ 16107a3061eSMatthias Schiffer static int batadv_algo_dump_entry(struct sk_buff *msg, u32 portid, u32 seq, 16207a3061eSMatthias Schiffer struct batadv_algo_ops *bat_algo_ops) 16307a3061eSMatthias Schiffer { 16407a3061eSMatthias Schiffer void *hdr; 16507a3061eSMatthias Schiffer 16607a3061eSMatthias Schiffer hdr = genlmsg_put(msg, portid, seq, &batadv_netlink_family, 16707a3061eSMatthias Schiffer NLM_F_MULTI, BATADV_CMD_GET_ROUTING_ALGOS); 16807a3061eSMatthias Schiffer if (!hdr) 16907a3061eSMatthias Schiffer return -EMSGSIZE; 17007a3061eSMatthias Schiffer 17107a3061eSMatthias Schiffer if (nla_put_string(msg, BATADV_ATTR_ALGO_NAME, bat_algo_ops->name)) 17207a3061eSMatthias Schiffer goto nla_put_failure; 17307a3061eSMatthias Schiffer 17407a3061eSMatthias Schiffer genlmsg_end(msg, hdr); 17507a3061eSMatthias Schiffer return 0; 17607a3061eSMatthias Schiffer 17707a3061eSMatthias Schiffer nla_put_failure: 17807a3061eSMatthias Schiffer genlmsg_cancel(msg, hdr); 17907a3061eSMatthias Schiffer return -EMSGSIZE; 18007a3061eSMatthias Schiffer } 18107a3061eSMatthias Schiffer 18207a3061eSMatthias Schiffer /** 18307a3061eSMatthias Schiffer * batadv_algo_dump - fill in information about supported routing 18407a3061eSMatthias Schiffer * algorithms 18507a3061eSMatthias Schiffer * @msg: netlink message to be sent back 18607a3061eSMatthias Schiffer * @cb: Parameters to the netlink request 18707a3061eSMatthias Schiffer * 18807a3061eSMatthias Schiffer * Return: Length of reply message. 18907a3061eSMatthias Schiffer */ 19007a3061eSMatthias Schiffer int batadv_algo_dump(struct sk_buff *msg, struct netlink_callback *cb) 19107a3061eSMatthias Schiffer { 19207a3061eSMatthias Schiffer int portid = NETLINK_CB(cb->skb).portid; 19307a3061eSMatthias Schiffer struct batadv_algo_ops *bat_algo_ops; 19407a3061eSMatthias Schiffer int skip = cb->args[0]; 19507a3061eSMatthias Schiffer int i = 0; 19607a3061eSMatthias Schiffer 19707a3061eSMatthias Schiffer hlist_for_each_entry(bat_algo_ops, &batadv_algo_list, list) { 19807a3061eSMatthias Schiffer if (i++ < skip) 19907a3061eSMatthias Schiffer continue; 20007a3061eSMatthias Schiffer 20107a3061eSMatthias Schiffer if (batadv_algo_dump_entry(msg, portid, cb->nlh->nlmsg_seq, 20207a3061eSMatthias Schiffer bat_algo_ops)) { 20307a3061eSMatthias Schiffer i--; 20407a3061eSMatthias Schiffer break; 20507a3061eSMatthias Schiffer } 20607a3061eSMatthias Schiffer } 20707a3061eSMatthias Schiffer 20807a3061eSMatthias Schiffer cb->args[0] = i; 20907a3061eSMatthias Schiffer 21007a3061eSMatthias Schiffer return msg->len; 21107a3061eSMatthias Schiffer } 212