Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0
0002 /* Copyright (C) B.A.T.M.A.N. contributors:
0003  *
0004  * Marek Lindner, Simon Wunderlich
0005  */
0006 
0007 #include "main.h"
0008 
0009 #include <linux/errno.h>
0010 #include <linux/list.h>
0011 #include <linux/moduleparam.h>
0012 #include <linux/netlink.h>
0013 #include <linux/printk.h>
0014 #include <linux/skbuff.h>
0015 #include <linux/stddef.h>
0016 #include <linux/string.h>
0017 #include <net/genetlink.h>
0018 #include <net/netlink.h>
0019 #include <uapi/linux/batman_adv.h>
0020 
0021 #include "bat_algo.h"
0022 #include "netlink.h"
0023 
0024 char batadv_routing_algo[20] = "BATMAN_IV";
0025 static struct hlist_head batadv_algo_list;
0026 
0027 /**
0028  * batadv_algo_init() - Initialize batman-adv algorithm management data
0029  *  structures
0030  */
0031 void batadv_algo_init(void)
0032 {
0033     INIT_HLIST_HEAD(&batadv_algo_list);
0034 }
0035 
0036 /**
0037  * batadv_algo_get() - Search for algorithm with specific name
0038  * @name: algorithm name to find
0039  *
0040  * Return: Pointer to batadv_algo_ops on success, NULL otherwise
0041  */
0042 struct batadv_algo_ops *batadv_algo_get(const char *name)
0043 {
0044     struct batadv_algo_ops *bat_algo_ops = NULL, *bat_algo_ops_tmp;
0045 
0046     hlist_for_each_entry(bat_algo_ops_tmp, &batadv_algo_list, list) {
0047         if (strcmp(bat_algo_ops_tmp->name, name) != 0)
0048             continue;
0049 
0050         bat_algo_ops = bat_algo_ops_tmp;
0051         break;
0052     }
0053 
0054     return bat_algo_ops;
0055 }
0056 
0057 /**
0058  * batadv_algo_register() - Register callbacks for a mesh algorithm
0059  * @bat_algo_ops: mesh algorithm callbacks to add
0060  *
0061  * Return: 0 on success or negative error number in case of failure
0062  */
0063 int batadv_algo_register(struct batadv_algo_ops *bat_algo_ops)
0064 {
0065     struct batadv_algo_ops *bat_algo_ops_tmp;
0066 
0067     bat_algo_ops_tmp = batadv_algo_get(bat_algo_ops->name);
0068     if (bat_algo_ops_tmp) {
0069         pr_info("Trying to register already registered routing algorithm: %s\n",
0070             bat_algo_ops->name);
0071         return -EEXIST;
0072     }
0073 
0074     /* all algorithms must implement all ops (for now) */
0075     if (!bat_algo_ops->iface.enable ||
0076         !bat_algo_ops->iface.disable ||
0077         !bat_algo_ops->iface.update_mac ||
0078         !bat_algo_ops->iface.primary_set ||
0079         !bat_algo_ops->neigh.cmp ||
0080         !bat_algo_ops->neigh.is_similar_or_better) {
0081         pr_info("Routing algo '%s' does not implement required ops\n",
0082             bat_algo_ops->name);
0083         return -EINVAL;
0084     }
0085 
0086     INIT_HLIST_NODE(&bat_algo_ops->list);
0087     hlist_add_head(&bat_algo_ops->list, &batadv_algo_list);
0088 
0089     return 0;
0090 }
0091 
0092 /**
0093  * batadv_algo_select() - Select algorithm of soft interface
0094  * @bat_priv: the bat priv with all the soft interface information
0095  * @name: name of the algorithm to select
0096  *
0097  * The algorithm callbacks for the soft interface will be set when the algorithm
0098  * with the correct name was found. Any previous selected algorithm will not be
0099  * deinitialized and the new selected algorithm will also not be initialized.
0100  * It is therefore not allowed to call batadv_algo_select outside the creation
0101  * function of the soft interface.
0102  *
0103  * Return: 0 on success or negative error number in case of failure
0104  */
0105 int batadv_algo_select(struct batadv_priv *bat_priv, const char *name)
0106 {
0107     struct batadv_algo_ops *bat_algo_ops;
0108 
0109     bat_algo_ops = batadv_algo_get(name);
0110     if (!bat_algo_ops)
0111         return -EINVAL;
0112 
0113     bat_priv->algo_ops = bat_algo_ops;
0114 
0115     return 0;
0116 }
0117 
0118 static int batadv_param_set_ra(const char *val, const struct kernel_param *kp)
0119 {
0120     struct batadv_algo_ops *bat_algo_ops;
0121     char *algo_name = (char *)val;
0122     size_t name_len = strlen(algo_name);
0123 
0124     if (name_len > 0 && algo_name[name_len - 1] == '\n')
0125         algo_name[name_len - 1] = '\0';
0126 
0127     bat_algo_ops = batadv_algo_get(algo_name);
0128     if (!bat_algo_ops) {
0129         pr_err("Routing algorithm '%s' is not supported\n", algo_name);
0130         return -EINVAL;
0131     }
0132 
0133     return param_set_copystring(algo_name, kp);
0134 }
0135 
0136 static const struct kernel_param_ops batadv_param_ops_ra = {
0137     .set = batadv_param_set_ra,
0138     .get = param_get_string,
0139 };
0140 
0141 static struct kparam_string batadv_param_string_ra = {
0142     .maxlen = sizeof(batadv_routing_algo),
0143     .string = batadv_routing_algo,
0144 };
0145 
0146 module_param_cb(routing_algo, &batadv_param_ops_ra, &batadv_param_string_ra,
0147         0644);
0148 
0149 /**
0150  * batadv_algo_dump_entry() - fill in information about one supported routing
0151  *  algorithm
0152  * @msg: netlink message to be sent back
0153  * @portid: Port to reply to
0154  * @seq: Sequence number of message
0155  * @bat_algo_ops: Algorithm to be dumped
0156  *
0157  * Return: Error number, or 0 on success
0158  */
0159 static int batadv_algo_dump_entry(struct sk_buff *msg, u32 portid, u32 seq,
0160                   struct batadv_algo_ops *bat_algo_ops)
0161 {
0162     void *hdr;
0163 
0164     hdr = genlmsg_put(msg, portid, seq, &batadv_netlink_family,
0165               NLM_F_MULTI, BATADV_CMD_GET_ROUTING_ALGOS);
0166     if (!hdr)
0167         return -EMSGSIZE;
0168 
0169     if (nla_put_string(msg, BATADV_ATTR_ALGO_NAME, bat_algo_ops->name))
0170         goto nla_put_failure;
0171 
0172     genlmsg_end(msg, hdr);
0173     return 0;
0174 
0175  nla_put_failure:
0176     genlmsg_cancel(msg, hdr);
0177     return -EMSGSIZE;
0178 }
0179 
0180 /**
0181  * batadv_algo_dump() - fill in information about supported routing
0182  *  algorithms
0183  * @msg: netlink message to be sent back
0184  * @cb: Parameters to the netlink request
0185  *
0186  * Return: Length of reply message.
0187  */
0188 int batadv_algo_dump(struct sk_buff *msg, struct netlink_callback *cb)
0189 {
0190     int portid = NETLINK_CB(cb->skb).portid;
0191     struct batadv_algo_ops *bat_algo_ops;
0192     int skip = cb->args[0];
0193     int i = 0;
0194 
0195     hlist_for_each_entry(bat_algo_ops, &batadv_algo_list, list) {
0196         if (i++ < skip)
0197             continue;
0198 
0199         if (batadv_algo_dump_entry(msg, portid, cb->nlh->nlmsg_seq,
0200                        bat_algo_ops)) {
0201             i--;
0202             break;
0203         }
0204     }
0205 
0206     cb->args[0] = i;
0207 
0208     return msg->len;
0209 }