cn_test.c 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * cn_test.c
  4. *
  5. * 2004+ Copyright (c) Evgeniy Polyakov <zbr@ioremap.net>
  6. * All rights reserved.
  7. */
  8. #define pr_fmt(fmt) "cn_test: " fmt
  9. #include <linux/kernel.h>
  10. #include <linux/module.h>
  11. #include <linux/moduleparam.h>
  12. #include <linux/skbuff.h>
  13. #include <linux/slab.h>
  14. #include <linux/timer.h>
  15. #include <linux/connector.h>
  16. static struct cb_id cn_test_id = { CN_NETLINK_USERS + 3, 0x456 };
  17. static char cn_test_name[] = "cn_test";
  18. static struct sock *nls;
  19. static struct timer_list cn_test_timer;
  20. static void cn_test_callback(struct cn_msg *msg, struct netlink_skb_parms *nsp)
  21. {
  22. pr_info("%s: %lu: idx=%x, val=%x, seq=%u, ack=%u, len=%d: %s.\n",
  23. __func__, jiffies, msg->id.idx, msg->id.val,
  24. msg->seq, msg->ack, msg->len,
  25. msg->len ? (char *)msg->data : "");
  26. }
  27. /*
  28. * Do not remove this function even if no one is using it as
  29. * this is an example of how to get notifications about new
  30. * connector user registration
  31. */
  32. #if 0
  33. static int cn_test_want_notify(void)
  34. {
  35. struct cn_ctl_msg *ctl;
  36. struct cn_notify_req *req;
  37. struct cn_msg *msg = NULL;
  38. int size, size0;
  39. struct sk_buff *skb;
  40. struct nlmsghdr *nlh;
  41. u32 group = 1;
  42. size0 = sizeof(*msg) + sizeof(*ctl) + 3 * sizeof(*req);
  43. size = NLMSG_SPACE(size0);
  44. skb = alloc_skb(size, GFP_ATOMIC);
  45. if (!skb) {
  46. pr_err("failed to allocate new skb with size=%u\n", size);
  47. return -ENOMEM;
  48. }
  49. nlh = nlmsg_put(skb, 0, 0x123, NLMSG_DONE, size - sizeof(*nlh), 0);
  50. if (!nlh) {
  51. kfree_skb(skb);
  52. return -EMSGSIZE;
  53. }
  54. msg = nlmsg_data(nlh);
  55. memset(msg, 0, size0);
  56. msg->id.idx = -1;
  57. msg->id.val = -1;
  58. msg->seq = 0x123;
  59. msg->ack = 0x345;
  60. msg->len = size0 - sizeof(*msg);
  61. ctl = (struct cn_ctl_msg *)(msg + 1);
  62. ctl->idx_notify_num = 1;
  63. ctl->val_notify_num = 2;
  64. ctl->group = group;
  65. ctl->len = msg->len - sizeof(*ctl);
  66. req = (struct cn_notify_req *)(ctl + 1);
  67. /*
  68. * Idx.
  69. */
  70. req->first = cn_test_id.idx;
  71. req->range = 10;
  72. /*
  73. * Val 0.
  74. */
  75. req++;
  76. req->first = cn_test_id.val;
  77. req->range = 10;
  78. /*
  79. * Val 1.
  80. */
  81. req++;
  82. req->first = cn_test_id.val + 20;
  83. req->range = 10;
  84. NETLINK_CB(skb).dst_group = ctl->group;
  85. //netlink_broadcast(nls, skb, 0, ctl->group, GFP_ATOMIC);
  86. netlink_unicast(nls, skb, 0, 0);
  87. pr_info("request was sent: group=0x%x\n", ctl->group);
  88. return 0;
  89. }
  90. #endif
  91. static u32 cn_test_timer_counter;
  92. static void cn_test_timer_func(struct timer_list *unused)
  93. {
  94. struct cn_msg *m;
  95. char data[32];
  96. pr_debug("%s: timer fired\n", __func__);
  97. m = kzalloc(sizeof(*m) + sizeof(data), GFP_ATOMIC);
  98. if (m) {
  99. memcpy(&m->id, &cn_test_id, sizeof(m->id));
  100. m->seq = cn_test_timer_counter;
  101. m->len = sizeof(data);
  102. m->len =
  103. scnprintf(data, sizeof(data), "counter = %u",
  104. cn_test_timer_counter) + 1;
  105. memcpy(m + 1, data, m->len);
  106. cn_netlink_send(m, 0, 0, GFP_ATOMIC);
  107. kfree(m);
  108. }
  109. cn_test_timer_counter++;
  110. mod_timer(&cn_test_timer, jiffies + msecs_to_jiffies(1000));
  111. }
  112. static int cn_test_init(void)
  113. {
  114. int err;
  115. err = cn_add_callback(&cn_test_id, cn_test_name, cn_test_callback);
  116. if (err)
  117. goto err_out;
  118. cn_test_id.val++;
  119. err = cn_add_callback(&cn_test_id, cn_test_name, cn_test_callback);
  120. if (err) {
  121. cn_del_callback(&cn_test_id);
  122. goto err_out;
  123. }
  124. timer_setup(&cn_test_timer, cn_test_timer_func, 0);
  125. mod_timer(&cn_test_timer, jiffies + msecs_to_jiffies(1000));
  126. pr_info("initialized with id={%u.%u}\n",
  127. cn_test_id.idx, cn_test_id.val);
  128. return 0;
  129. err_out:
  130. if (nls && nls->sk_socket)
  131. sock_release(nls->sk_socket);
  132. return err;
  133. }
  134. static void cn_test_fini(void)
  135. {
  136. del_timer_sync(&cn_test_timer);
  137. cn_del_callback(&cn_test_id);
  138. cn_test_id.val--;
  139. cn_del_callback(&cn_test_id);
  140. if (nls && nls->sk_socket)
  141. sock_release(nls->sk_socket);
  142. }
  143. module_init(cn_test_init);
  144. module_exit(cn_test_fini);
  145. MODULE_LICENSE("GPL");
  146. MODULE_AUTHOR("Evgeniy Polyakov <zbr@ioremap.net>");
  147. MODULE_DESCRIPTION("Connector's test module");