ctrl.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547
  1. /*
  2. * lib/genl/ctrl.c Generic Netlink Controller
  3. *
  4. * This library is free software; you can redistribute it and/or
  5. * modify it under the terms of the GNU Lesser General Public
  6. * License as published by the Free Software Foundation version 2.1
  7. * of the License.
  8. *
  9. * Copyright (c) 2003-2012 Thomas Graf <tgraf@suug.ch>
  10. */
  11. /**
  12. * @ingroup genl
  13. * @defgroup genl_ctrl Controller (Resolver)
  14. *
  15. * Resolves Generic Netlink family names to numeric identifiers.
  16. *
  17. * The controller is a component in the kernel that resolves Generic Netlink
  18. * family names to their numeric identifiers. This module provides functions
  19. * to query the controller to access the resolving functionality.
  20. * @{
  21. */
  22. #include <netlink-private/genl.h>
  23. #include <netlink/netlink.h>
  24. #include <netlink/genl/genl.h>
  25. #include <netlink/genl/family.h>
  26. #include <netlink/genl/mngt.h>
  27. #include <netlink/genl/ctrl.h>
  28. #include <netlink/utils.h>
  29. /** @cond SKIP */
  30. #define CTRL_VERSION 0x0001
  31. static struct nl_cache_ops genl_ctrl_ops;
  32. static int ctrl_request_update(struct nl_cache *c, struct nl_sock *h)
  33. {
  34. return genl_send_simple(h, GENL_ID_CTRL, CTRL_CMD_GETFAMILY,
  35. CTRL_VERSION, NLM_F_DUMP);
  36. }
  37. static struct nla_policy ctrl_policy[CTRL_ATTR_MAX+1] = {
  38. [CTRL_ATTR_FAMILY_ID] = { .type = NLA_U16 },
  39. [CTRL_ATTR_FAMILY_NAME] = { .type = NLA_STRING,
  40. .maxlen = GENL_NAMSIZ },
  41. [CTRL_ATTR_VERSION] = { .type = NLA_U32 },
  42. [CTRL_ATTR_HDRSIZE] = { .type = NLA_U32 },
  43. [CTRL_ATTR_MAXATTR] = { .type = NLA_U32 },
  44. [CTRL_ATTR_OPS] = { .type = NLA_NESTED },
  45. [CTRL_ATTR_MCAST_GROUPS] = { .type = NLA_NESTED },
  46. };
  47. static struct nla_policy family_op_policy[CTRL_ATTR_OP_MAX+1] = {
  48. [CTRL_ATTR_OP_ID] = { .type = NLA_U32 },
  49. [CTRL_ATTR_OP_FLAGS] = { .type = NLA_U32 },
  50. };
  51. static struct nla_policy family_grp_policy[CTRL_ATTR_MCAST_GRP_MAX+1] = {
  52. [CTRL_ATTR_MCAST_GRP_NAME] = { .type = NLA_STRING },
  53. [CTRL_ATTR_MCAST_GRP_ID] = { .type = NLA_U32 },
  54. };
  55. static int parse_mcast_grps(struct genl_family *family, struct nlattr *grp_attr)
  56. {
  57. struct nlattr *nla;
  58. int remaining, err;
  59. if (!grp_attr)
  60. BUG();
  61. nla_for_each_nested(nla, grp_attr, remaining) {
  62. struct nlattr *tb[CTRL_ATTR_MCAST_GRP_MAX+1];
  63. int id;
  64. const char * name;
  65. err = nla_parse_nested(tb, CTRL_ATTR_MCAST_GRP_MAX, nla,
  66. family_grp_policy);
  67. if (err < 0)
  68. goto errout;
  69. if (tb[CTRL_ATTR_MCAST_GRP_ID] == NULL) {
  70. err = -NLE_MISSING_ATTR;
  71. goto errout;
  72. }
  73. id = nla_get_u32(tb[CTRL_ATTR_MCAST_GRP_ID]);
  74. if (tb[CTRL_ATTR_MCAST_GRP_NAME] == NULL) {
  75. err = -NLE_MISSING_ATTR;
  76. goto errout;
  77. }
  78. name = nla_get_string(tb[CTRL_ATTR_MCAST_GRP_NAME]);
  79. err = genl_family_add_grp(family, id, name);
  80. if (err < 0)
  81. goto errout;
  82. }
  83. err = 0;
  84. errout:
  85. return err;
  86. }
  87. static int ctrl_msg_parser(struct nl_cache_ops *ops, struct genl_cmd *cmd,
  88. struct genl_info *info, void *arg)
  89. {
  90. struct genl_family *family;
  91. struct nl_parser_param *pp = arg;
  92. int err;
  93. family = genl_family_alloc();
  94. if (family == NULL) {
  95. err = -NLE_NOMEM;
  96. goto errout;
  97. }
  98. if (info->attrs[CTRL_ATTR_FAMILY_NAME] == NULL) {
  99. err = -NLE_MISSING_ATTR;
  100. goto errout;
  101. }
  102. if (info->attrs[CTRL_ATTR_FAMILY_ID] == NULL) {
  103. err = -NLE_MISSING_ATTR;
  104. goto errout;
  105. }
  106. family->ce_msgtype = info->nlh->nlmsg_type;
  107. genl_family_set_id(family,
  108. nla_get_u16(info->attrs[CTRL_ATTR_FAMILY_ID]));
  109. genl_family_set_name(family,
  110. nla_get_string(info->attrs[CTRL_ATTR_FAMILY_NAME]));
  111. if (info->attrs[CTRL_ATTR_VERSION]) {
  112. uint32_t version = nla_get_u32(info->attrs[CTRL_ATTR_VERSION]);
  113. genl_family_set_version(family, version);
  114. }
  115. if (info->attrs[CTRL_ATTR_HDRSIZE]) {
  116. uint32_t hdrsize = nla_get_u32(info->attrs[CTRL_ATTR_HDRSIZE]);
  117. genl_family_set_hdrsize(family, hdrsize);
  118. }
  119. if (info->attrs[CTRL_ATTR_MAXATTR]) {
  120. uint32_t maxattr = nla_get_u32(info->attrs[CTRL_ATTR_MAXATTR]);
  121. genl_family_set_maxattr(family, maxattr);
  122. }
  123. if (info->attrs[CTRL_ATTR_OPS]) {
  124. struct nlattr *nla, *nla_ops;
  125. int remaining;
  126. nla_ops = info->attrs[CTRL_ATTR_OPS];
  127. nla_for_each_nested(nla, nla_ops, remaining) {
  128. struct nlattr *tb[CTRL_ATTR_OP_MAX+1];
  129. int flags = 0, id;
  130. err = nla_parse_nested(tb, CTRL_ATTR_OP_MAX, nla,
  131. family_op_policy);
  132. if (err < 0)
  133. goto errout;
  134. if (tb[CTRL_ATTR_OP_ID] == NULL) {
  135. err = -NLE_MISSING_ATTR;
  136. goto errout;
  137. }
  138. id = nla_get_u32(tb[CTRL_ATTR_OP_ID]);
  139. if (tb[CTRL_ATTR_OP_FLAGS])
  140. flags = nla_get_u32(tb[CTRL_ATTR_OP_FLAGS]);
  141. err = genl_family_add_op(family, id, flags);
  142. if (err < 0)
  143. goto errout;
  144. }
  145. }
  146. if (info->attrs[CTRL_ATTR_MCAST_GROUPS]) {
  147. err = parse_mcast_grps(family, info->attrs[CTRL_ATTR_MCAST_GROUPS]);
  148. if (err < 0)
  149. goto errout;
  150. }
  151. err = pp->pp_cb((struct nl_object *) family, pp);
  152. errout:
  153. genl_family_put(family);
  154. return err;
  155. }
  156. /**
  157. * process responses from from the query sent by genl_ctrl_probe_by_name
  158. * @arg nl_msg Returned message.
  159. * @arg name genl_family structure to fill out.
  160. *
  161. * Process returned messages, filling out the missing informatino in the
  162. * genl_family structure
  163. *
  164. * @return Indicator to keep processing frames or not
  165. *
  166. */
  167. static int probe_response(struct nl_msg *msg, void *arg)
  168. {
  169. struct nlattr *tb[CTRL_ATTR_MAX+1];
  170. struct nlmsghdr *nlh = nlmsg_hdr(msg);
  171. struct genl_family *ret = (struct genl_family *)arg;
  172. if (genlmsg_parse(nlh, 0, tb, CTRL_ATTR_MAX, ctrl_policy))
  173. return NL_SKIP;
  174. if (tb[CTRL_ATTR_FAMILY_ID])
  175. genl_family_set_id(ret, nla_get_u16(tb[CTRL_ATTR_FAMILY_ID]));
  176. if (tb[CTRL_ATTR_MCAST_GROUPS])
  177. if (parse_mcast_grps(ret, tb[CTRL_ATTR_MCAST_GROUPS]) < 0)
  178. return NL_SKIP;
  179. return NL_STOP;
  180. }
  181. /**
  182. * Look up generic netlink family by family name querying the kernel directly
  183. * @arg sk Socket.
  184. * @arg name Family name.
  185. *
  186. * Directly query's the kernel for a given family name. The caller will own a
  187. * reference on the returned object which needsd to be given back after usage
  188. * using genl_family_put.
  189. *
  190. * Note: This API call differs from genl_ctrl_search_by_name in that it querys
  191. * the kernel directly, alowing for module autoload to take place to resolve the
  192. * family request. Using an nl_cache prevents that operation
  193. *
  194. * @return Generic netlink family object or NULL if no match was found.
  195. */
  196. static struct genl_family *genl_ctrl_probe_by_name(struct nl_sock *sk,
  197. const char *name)
  198. {
  199. struct nl_msg *msg;
  200. struct genl_family *ret;
  201. struct nl_cb *cb, *orig;
  202. int rc;
  203. ret = genl_family_alloc();
  204. if (!ret)
  205. goto out;
  206. genl_family_set_name(ret, name);
  207. msg = nlmsg_alloc();
  208. if (!msg)
  209. goto out_fam_free;
  210. if (!(orig = nl_socket_get_cb(sk)))
  211. goto out_msg_free;
  212. cb = nl_cb_clone(orig);
  213. nl_cb_put(orig);
  214. if (!cb)
  215. goto out_msg_free;
  216. if (!genlmsg_put(msg, NL_AUTO_PORT, NL_AUTO_SEQ, GENL_ID_CTRL,
  217. 0, 0, CTRL_CMD_GETFAMILY, 1)) {
  218. BUG();
  219. goto out_cb_free;
  220. }
  221. if (nla_put_string(msg, CTRL_ATTR_FAMILY_NAME, name) < 0)
  222. goto out_cb_free;
  223. rc = nl_cb_set(cb, NL_CB_VALID, NL_CB_CUSTOM, probe_response,
  224. (void *) ret);
  225. if (rc < 0)
  226. goto out_cb_free;
  227. rc = nl_send_auto_complete(sk, msg);
  228. if (rc < 0)
  229. goto out_cb_free;
  230. rc = nl_recvmsgs(sk, cb);
  231. if (rc < 0)
  232. goto out_cb_free;
  233. /* If search was successful, request may be ACKed after data */
  234. rc = wait_for_ack(sk);
  235. if (rc < 0)
  236. goto out_cb_free;
  237. if (genl_family_get_id(ret) != 0) {
  238. nlmsg_free(msg);
  239. nl_cb_put(cb);
  240. return ret;
  241. }
  242. out_cb_free:
  243. nl_cb_put(cb);
  244. out_msg_free:
  245. nlmsg_free(msg);
  246. out_fam_free:
  247. genl_family_put(ret);
  248. ret = NULL;
  249. out:
  250. return ret;
  251. }
  252. /** @endcond */
  253. /**
  254. * @name Controller Cache
  255. *
  256. * The controller cache allows to keep a local copy of the list of all
  257. * kernel side registered Generic Netlink families to quickly resolve
  258. * multiple Generic Netlink family names without requiring to communicate
  259. * with the kernel for each resolving iteration.
  260. *
  261. * @{
  262. */
  263. /**
  264. * Allocate a new controller cache
  265. * @arg sk Generic Netlink socket
  266. * @arg result Pointer to store resulting cache
  267. *
  268. * Allocates a new cache mirroring the state of the controller and stores it
  269. * in \c *result. The allocated cache will contain a list of all currently
  270. * registered kernel side Generic Netlink families. The cache is meant to be
  271. * used to resolve family names locally.
  272. *
  273. * @return 0 on success or a negative error code.
  274. */
  275. int genl_ctrl_alloc_cache(struct nl_sock *sk, struct nl_cache **result)
  276. {
  277. return nl_cache_alloc_and_fill(&genl_ctrl_ops, sk, result);
  278. }
  279. /**
  280. * Search controller cache for a numeric address match
  281. * @arg cache Controller cache
  282. * @arg id Numeric family identifier.
  283. *
  284. * Searches a previously allocated controller cache and looks for an entry
  285. * that matches the specified numeric family identifier \c id. If a match
  286. * is found successfully, the reference count of the matching object is
  287. * increased by one before the objet is returned.
  288. *
  289. * @see genl_ctrl_alloc_cache()
  290. * @see genl_ctrl_search_by_name()
  291. * @see genl_family_put()
  292. *
  293. * @return Generic Netlink family object or NULL if no match was found.
  294. */
  295. struct genl_family *genl_ctrl_search(struct nl_cache *cache, int id)
  296. {
  297. struct genl_family *fam;
  298. if (cache->c_ops != &genl_ctrl_ops)
  299. BUG();
  300. nl_list_for_each_entry(fam, &cache->c_items, ce_list) {
  301. if (fam->gf_id == id) {
  302. nl_object_get((struct nl_object *) fam);
  303. return fam;
  304. }
  305. }
  306. return NULL;
  307. }
  308. /**
  309. * Search controller cache for a family name match
  310. * @arg cache Controller cache
  311. * @arg name Name of Generic Netlink family
  312. *
  313. * Searches a previously allocated controller cache and looks for an entry
  314. * that matches the specified family \c name. If a match is found successfully,
  315. * the reference count of the matching object is increased by one before the
  316. * objet is returned.
  317. *
  318. * @see genl_ctrl_alloc_cache()
  319. * @see genl_ctrl_search()
  320. * @see genl_family_put()
  321. *
  322. * @return Generic Netlink family object or NULL if no match was found.
  323. */
  324. struct genl_family *genl_ctrl_search_by_name(struct nl_cache *cache,
  325. const char *name)
  326. {
  327. struct genl_family *fam;
  328. if (cache->c_ops != &genl_ctrl_ops)
  329. BUG();
  330. nl_list_for_each_entry(fam, &cache->c_items, ce_list) {
  331. if (!strcmp(name, fam->gf_name)) {
  332. nl_object_get((struct nl_object *) fam);
  333. return fam;
  334. }
  335. }
  336. return NULL;
  337. }
  338. /** @} */
  339. /**
  340. * @name Direct Resolvers
  341. *
  342. * These functions communicate directly with the kernel and do not require
  343. * a cache to be kept up to date.
  344. *
  345. * @{
  346. */
  347. /**
  348. * Resolve Generic Netlink family name to numeric identifier
  349. * @arg sk Generic Netlink socket.
  350. * @arg name Name of Generic Netlink family
  351. *
  352. * Resolves the Generic Netlink family name to the corresponding numeric
  353. * family identifier. This function queries the kernel directly, use
  354. * genl_ctrl_search_by_name() if you need to resolve multiple names.
  355. *
  356. * @see genl_ctrl_search_by_name()
  357. *
  358. * @return The numeric family identifier or a negative error code.
  359. */
  360. int genl_ctrl_resolve(struct nl_sock *sk, const char *name)
  361. {
  362. struct genl_family *family;
  363. int err;
  364. family = genl_ctrl_probe_by_name(sk, name);
  365. if (family == NULL) {
  366. err = -NLE_OBJ_NOTFOUND;
  367. goto errout;
  368. }
  369. err = genl_family_get_id(family);
  370. genl_family_put(family);
  371. errout:
  372. return err;
  373. }
  374. static int genl_ctrl_grp_by_name(const struct genl_family *family,
  375. const char *grp_name)
  376. {
  377. struct genl_family_grp *grp;
  378. nl_list_for_each_entry(grp, &family->gf_mc_grps, list) {
  379. if (!strcmp(grp->name, grp_name)) {
  380. return grp->id;
  381. }
  382. }
  383. return -NLE_OBJ_NOTFOUND;
  384. }
  385. /**
  386. * Resolve Generic Netlink family group name
  387. * @arg sk Generic Netlink socket
  388. * @arg family_name Name of Generic Netlink family
  389. * @arg grp_name Name of group to resolve
  390. *
  391. * Looks up the family object and resolves the group name to the numeric
  392. * group identifier.
  393. *
  394. * @return Numeric group identifier or a negative error code.
  395. */
  396. int genl_ctrl_resolve_grp(struct nl_sock *sk, const char *family_name,
  397. const char *grp_name)
  398. {
  399. struct genl_family *family;
  400. int err;
  401. family = genl_ctrl_probe_by_name(sk, family_name);
  402. if (family == NULL) {
  403. err = -NLE_OBJ_NOTFOUND;
  404. goto errout;
  405. }
  406. err = genl_ctrl_grp_by_name(family, grp_name);
  407. genl_family_put(family);
  408. errout:
  409. return err;
  410. }
  411. /** @} */
  412. /** @cond SKIP */
  413. static struct genl_cmd genl_cmds[] = {
  414. {
  415. .c_id = CTRL_CMD_NEWFAMILY,
  416. .c_name = "NEWFAMILY" ,
  417. .c_maxattr = CTRL_ATTR_MAX,
  418. .c_attr_policy = ctrl_policy,
  419. .c_msg_parser = ctrl_msg_parser,
  420. },
  421. {
  422. .c_id = CTRL_CMD_DELFAMILY,
  423. .c_name = "DELFAMILY" ,
  424. },
  425. {
  426. .c_id = CTRL_CMD_GETFAMILY,
  427. .c_name = "GETFAMILY" ,
  428. },
  429. {
  430. .c_id = CTRL_CMD_NEWOPS,
  431. .c_name = "NEWOPS" ,
  432. },
  433. {
  434. .c_id = CTRL_CMD_DELOPS,
  435. .c_name = "DELOPS" ,
  436. },
  437. };
  438. static struct genl_ops genl_ops = {
  439. .o_cmds = genl_cmds,
  440. .o_ncmds = ARRAY_SIZE(genl_cmds),
  441. };
  442. extern struct nl_object_ops genl_family_ops;
  443. static struct nl_cache_ops genl_ctrl_ops = {
  444. .co_name = "genl/family",
  445. .co_hdrsize = GENL_HDRSIZE(0),
  446. .co_msgtypes = GENL_FAMILY(GENL_ID_CTRL, "nlctrl"),
  447. .co_genl = &genl_ops,
  448. .co_protocol = NETLINK_GENERIC,
  449. .co_request_update = ctrl_request_update,
  450. .co_obj_ops = &genl_family_ops,
  451. };
  452. static void __init ctrl_init(void)
  453. {
  454. genl_register(&genl_ctrl_ops);
  455. }
  456. static void __exit ctrl_exit(void)
  457. {
  458. genl_unregister(&genl_ctrl_ops);
  459. }
  460. /** @endcond */
  461. /** @} */