00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043
00044
00045
00046
00047
00048
00049
00050
00051
00052
00053
00054
00055
00056
00057
00058
00059
00060
00061
00062
00063
00064
00065
00066
00067
00068
00069
00070
00071
00072
00073
00074
00075
00076
00077
00078
00079
00080
00081
00082
00083 #include <netlink-generic.h>
00084 #include <netlink/netlink.h>
00085 #include <netlink/genl/genl.h>
00086 #include <netlink/genl/mngt.h>
00087 #include <netlink/genl/family.h>
00088 #include <netlink/genl/ctrl.h>
00089 #include <netlink/utils.h>
00090
00091 static NL_LIST_HEAD(genl_ops_list);
00092
00093 static int genl_msg_parser(struct nl_cache_ops *ops, struct sockaddr_nl *who,
00094 struct nlmsghdr *nlh, struct nl_parser_param *pp)
00095 {
00096 int i, err;
00097 struct genlmsghdr *ghdr;
00098 struct genl_cmd *cmd;
00099
00100 ghdr = nlmsg_data(nlh);
00101
00102 if (ops->co_genl == NULL)
00103 BUG();
00104
00105 for (i = 0; i < ops->co_genl->o_ncmds; i++) {
00106 cmd = &ops->co_genl->o_cmds[i];
00107 if (cmd->c_id == ghdr->cmd)
00108 goto found;
00109 }
00110
00111 err = -NLE_MSGTYPE_NOSUPPORT;
00112 goto errout;
00113
00114 found:
00115 if (cmd->c_msg_parser == NULL)
00116 err = -NLE_OPNOTSUPP;
00117 else {
00118 struct nlattr *tb[cmd->c_maxattr + 1];
00119 struct genl_info info = {
00120 .who = who,
00121 .nlh = nlh,
00122 .genlhdr = ghdr,
00123 .userhdr = genlmsg_data(ghdr),
00124 .attrs = tb,
00125 };
00126
00127 err = nlmsg_parse(nlh, ops->co_hdrsize, tb, cmd->c_maxattr,
00128 cmd->c_attr_policy);
00129 if (err < 0)
00130 goto errout;
00131
00132 err = cmd->c_msg_parser(ops, cmd, &info, pp);
00133 }
00134 errout:
00135 return err;
00136
00137 }
00138
00139 char *genl_op2name(int family, int op, char *buf, size_t len)
00140 {
00141 struct genl_ops *ops;
00142 int i;
00143
00144 nl_list_for_each_entry(ops, &genl_ops_list, o_list) {
00145 if (ops->o_family == family) {
00146 for (i = 0; i < ops->o_ncmds; i++) {
00147 struct genl_cmd *cmd;
00148 cmd = &ops->o_cmds[i];
00149
00150 if (cmd->c_id == op) {
00151 strncpy(buf, cmd->c_name, len - 1);
00152 return buf;
00153 }
00154 }
00155 }
00156 }
00157
00158 strncpy(buf, "unknown", len - 1);
00159 return NULL;
00160 }
00161
00162
00163
00164
00165
00166
00167
00168
00169
00170
00171
00172 int genl_register(struct nl_cache_ops *ops)
00173 {
00174 int err;
00175
00176 if (ops->co_protocol != NETLINK_GENERIC) {
00177 err = -NLE_PROTO_MISMATCH;
00178 goto errout;
00179 }
00180
00181 if (ops->co_hdrsize < GENL_HDRSIZE(0)) {
00182 err = -NLE_INVAL;
00183 goto errout;
00184 }
00185
00186 if (ops->co_genl == NULL) {
00187 err = -NLE_INVAL;
00188 goto errout;
00189 }
00190
00191 ops->co_genl->o_cache_ops = ops;
00192 ops->co_genl->o_name = ops->co_msgtypes[0].mt_name;
00193 ops->co_genl->o_family = ops->co_msgtypes[0].mt_id;
00194 ops->co_msg_parser = genl_msg_parser;
00195
00196
00197
00198 nl_list_add_tail(&ops->co_genl->o_list, &genl_ops_list);
00199
00200 err = nl_cache_mngt_register(ops);
00201 errout:
00202 return err;
00203 }
00204
00205
00206
00207
00208
00209 void genl_unregister(struct nl_cache_ops *ops)
00210 {
00211 nl_cache_mngt_unregister(ops);
00212 nl_list_del(&ops->co_genl->o_list);
00213 }
00214
00215
00216
00217
00218
00219
00220
00221
00222 static int __genl_ops_resolve(struct nl_cache *ctrl, struct genl_ops *ops)
00223 {
00224 struct genl_family *family;
00225
00226 family = genl_ctrl_search_by_name(ctrl, ops->o_name);
00227 if (family != NULL) {
00228 ops->o_id = genl_family_get_id(family);
00229 genl_family_put(family);
00230
00231 return 0;
00232 }
00233
00234 return -NLE_OBJ_NOTFOUND;
00235 }
00236
00237 int genl_ops_resolve(struct nl_sock *sk, struct genl_ops *ops)
00238 {
00239 struct nl_cache *ctrl;
00240 int err;
00241
00242 if ((err = genl_ctrl_alloc_cache(sk, &ctrl)) < 0)
00243 goto errout;
00244
00245 err = __genl_ops_resolve(ctrl, ops);
00246
00247 nl_cache_free(ctrl);
00248 errout:
00249 return err;
00250 }
00251
00252 int genl_mngt_resolve(struct nl_sock *sk)
00253 {
00254 struct nl_cache *ctrl;
00255 struct genl_ops *ops;
00256 int err = 0;
00257
00258 if ((err = genl_ctrl_alloc_cache(sk, &ctrl)) < 0)
00259 goto errout;
00260
00261 nl_list_for_each_entry(ops, &genl_ops_list, o_list) {
00262 err = __genl_ops_resolve(ctrl, ops);
00263 }
00264
00265 nl_cache_free(ctrl);
00266 errout:
00267 return err;
00268 }
00269
00270
00271
00272
00273