irnetlink.c 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. /*
  2. * IrDA netlink layer, for stack configuration.
  3. *
  4. * Copyright (c) 2007 Samuel Ortiz <samuel@sortiz.org>
  5. *
  6. * Partly based on the 802.11 nelink implementation
  7. * (see net/wireless/nl80211.c) which is:
  8. * Copyright 2006 Johannes Berg <johannes@sipsolutions.net>
  9. *
  10. * This program is free software; you can redistribute it and/or modify
  11. * it under the terms of the GNU General Public License version 2 as
  12. * published by the Free Software Foundation.
  13. *
  14. */
  15. #include <linux/socket.h>
  16. #include <linux/irda.h>
  17. #include <linux/gfp.h>
  18. #include <net/net_namespace.h>
  19. #include <net/sock.h>
  20. #include <net/irda/irda.h>
  21. #include <net/irda/irlap.h>
  22. #include <net/genetlink.h>
  23. static struct genl_family irda_nl_family = {
  24. .id = GENL_ID_GENERATE,
  25. .name = IRDA_NL_NAME,
  26. .hdrsize = 0,
  27. .version = IRDA_NL_VERSION,
  28. .maxattr = IRDA_NL_CMD_MAX,
  29. };
  30. static struct net_device * ifname_to_netdev(struct net *net, struct genl_info *info)
  31. {
  32. char * ifname;
  33. if (!info->attrs[IRDA_NL_ATTR_IFNAME])
  34. return NULL;
  35. ifname = nla_data(info->attrs[IRDA_NL_ATTR_IFNAME]);
  36. pr_debug("%s(): Looking for %s\n", __func__, ifname);
  37. return dev_get_by_name(net, ifname);
  38. }
  39. static int irda_nl_set_mode(struct sk_buff *skb, struct genl_info *info)
  40. {
  41. struct net_device * dev;
  42. struct irlap_cb * irlap;
  43. u32 mode;
  44. if (!info->attrs[IRDA_NL_ATTR_MODE])
  45. return -EINVAL;
  46. mode = nla_get_u32(info->attrs[IRDA_NL_ATTR_MODE]);
  47. pr_debug("%s(): Switching to mode: %d\n", __func__, mode);
  48. dev = ifname_to_netdev(&init_net, info);
  49. if (!dev)
  50. return -ENODEV;
  51. irlap = (struct irlap_cb *)dev->atalk_ptr;
  52. if (!irlap) {
  53. dev_put(dev);
  54. return -ENODEV;
  55. }
  56. irlap->mode = mode;
  57. dev_put(dev);
  58. return 0;
  59. }
  60. static int irda_nl_get_mode(struct sk_buff *skb, struct genl_info *info)
  61. {
  62. struct net_device * dev;
  63. struct irlap_cb * irlap;
  64. struct sk_buff *msg;
  65. void *hdr;
  66. int ret = -ENOBUFS;
  67. dev = ifname_to_netdev(&init_net, info);
  68. if (!dev)
  69. return -ENODEV;
  70. msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
  71. if (!msg) {
  72. dev_put(dev);
  73. return -ENOMEM;
  74. }
  75. irlap = (struct irlap_cb *)dev->atalk_ptr;
  76. if (!irlap) {
  77. ret = -ENODEV;
  78. goto err_out;
  79. }
  80. hdr = genlmsg_put(msg, info->snd_portid, info->snd_seq,
  81. &irda_nl_family, 0, IRDA_NL_CMD_GET_MODE);
  82. if (hdr == NULL) {
  83. ret = -EMSGSIZE;
  84. goto err_out;
  85. }
  86. if(nla_put_string(msg, IRDA_NL_ATTR_IFNAME,
  87. dev->name))
  88. goto err_out;
  89. if(nla_put_u32(msg, IRDA_NL_ATTR_MODE, irlap->mode))
  90. goto err_out;
  91. genlmsg_end(msg, hdr);
  92. return genlmsg_reply(msg, info);
  93. err_out:
  94. nlmsg_free(msg);
  95. dev_put(dev);
  96. return ret;
  97. }
  98. static const struct nla_policy irda_nl_policy[IRDA_NL_ATTR_MAX + 1] = {
  99. [IRDA_NL_ATTR_IFNAME] = { .type = NLA_NUL_STRING,
  100. .len = IFNAMSIZ-1 },
  101. [IRDA_NL_ATTR_MODE] = { .type = NLA_U32 },
  102. };
  103. static const struct genl_ops irda_nl_ops[] = {
  104. {
  105. .cmd = IRDA_NL_CMD_SET_MODE,
  106. .doit = irda_nl_set_mode,
  107. .policy = irda_nl_policy,
  108. .flags = GENL_ADMIN_PERM,
  109. },
  110. {
  111. .cmd = IRDA_NL_CMD_GET_MODE,
  112. .doit = irda_nl_get_mode,
  113. .policy = irda_nl_policy,
  114. /* can be retrieved by unprivileged users */
  115. },
  116. };
  117. int irda_nl_register(void)
  118. {
  119. return genl_register_family_with_ops(&irda_nl_family, irda_nl_ops);
  120. }
  121. void irda_nl_unregister(void)
  122. {
  123. genl_unregister_family(&irda_nl_family);
  124. }