NCDInterfaceMonitor.c 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  1. /**
  2. * @file NCDInterfaceMonitor.c
  3. * @author Ambroz Bizjak <ambrop7@gmail.com>
  4. *
  5. * @section LICENSE
  6. *
  7. * Redistribution and use in source and binary forms, with or without
  8. * modification, are permitted provided that the following conditions are met:
  9. * 1. Redistributions of source code must retain the above copyright
  10. * notice, this list of conditions and the following disclaimer.
  11. * 2. Redistributions in binary form must reproduce the above copyright
  12. * notice, this list of conditions and the following disclaimer in the
  13. * documentation and/or other materials provided with the distribution.
  14. * 3. Neither the name of the author nor the
  15. * names of its contributors may be used to endorse or promote products
  16. * derived from this software without specific prior written permission.
  17. *
  18. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
  19. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  20. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  21. * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
  22. * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  23. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  24. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  25. * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  26. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  27. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  28. */
  29. #include <string.h>
  30. #include <stdio.h>
  31. #include <unistd.h>
  32. #include <stdint.h>
  33. #include <sys/socket.h>
  34. #include <net/if.h>
  35. #include <linux/netlink.h>
  36. #include <linux/rtnetlink.h>
  37. #include <asm/types.h>
  38. #include <asm/types.h>
  39. #include <misc/debug.h>
  40. #include <misc/nonblocking.h>
  41. #include <base/BLog.h>
  42. #include <ncd/NCDInterfaceMonitor.h>
  43. #include <generated/blog_channel_NCDInterfaceMonitor.h>
  44. static void netlink_fd_handler (NCDInterfaceMonitor *o, int events);
  45. static void process_buffer (NCDInterfaceMonitor *o);
  46. static void more_job_handler (NCDInterfaceMonitor *o);
  47. void netlink_fd_handler (NCDInterfaceMonitor *o, int events)
  48. {
  49. DebugObject_Access(&o->d_obj);
  50. ASSERT(o->buf_left == -1)
  51. // read from netlink fd
  52. int len = read(o->netlink_fd, o->buf, sizeof(o->buf));
  53. if (len < 0) {
  54. BLog(BLOG_ERROR, "read failed");
  55. return;
  56. }
  57. // stop receiving fd events
  58. BReactor_SetFileDescriptorEvents(o->reactor, &o->bfd, 0);
  59. // set buffer
  60. o->buf_nh = (struct nlmsghdr *)o->buf;
  61. o->buf_left = len;
  62. // process buffer
  63. process_buffer(o);
  64. return;
  65. }
  66. void process_buffer (NCDInterfaceMonitor *o)
  67. {
  68. ASSERT(o->buf_left >= 0)
  69. for (; NLMSG_OK(o->buf_nh, o->buf_left); o->buf_nh = NLMSG_NEXT(o->buf_nh, o->buf_left)) {
  70. if (o->buf_nh->nlmsg_type == NLMSG_DONE) {
  71. break;
  72. }
  73. if (o->buf_nh->nlmsg_type != RTM_NEWLINK && o->buf_nh->nlmsg_type != RTM_DELLINK) {
  74. continue;
  75. }
  76. void *pl = NLMSG_DATA(o->buf_nh);
  77. int pl_len = NLMSG_PAYLOAD(o->buf_nh, 0);
  78. if (pl_len < sizeof(struct ifinfomsg)) {
  79. BLog(BLOG_ERROR, "missing infomsg");
  80. continue;
  81. }
  82. struct ifinfomsg *im = (void *)pl;
  83. // parse attributes to get interface name
  84. char *ifname = NULL;
  85. int rta_len = pl_len - sizeof(struct ifinfomsg);
  86. for (struct rtattr *rta = (void *)(im + 1); RTA_OK(rta, rta_len); rta = RTA_NEXT(rta, rta_len)) {
  87. uint8_t *attr = RTA_DATA(rta);
  88. int attr_len = RTA_PAYLOAD(rta);
  89. if (rta->rta_type == IFLA_IFNAME && attr_len > 0 && attr[attr_len - 1] == '\0') {
  90. ifname = (char *)attr;
  91. }
  92. }
  93. if (!ifname) {
  94. continue;
  95. }
  96. // finish this message
  97. o->buf_nh = NLMSG_NEXT(o->buf_nh, o->buf_left);
  98. // schedule more job
  99. BPending_Set(&o->more_job);
  100. // dispatch event
  101. o->handler(o->user, ifname, NCDIfConfig_query(ifname));
  102. return;
  103. }
  104. // set no buffer
  105. o->buf_left = -1;
  106. // continue receiving fd events
  107. BReactor_SetFileDescriptorEvents(o->reactor, &o->bfd, BREACTOR_READ);
  108. }
  109. void more_job_handler (NCDInterfaceMonitor *o)
  110. {
  111. DebugObject_Access(&o->d_obj);
  112. ASSERT(o->buf_left >= 0)
  113. // process buffer
  114. process_buffer(o);
  115. return;
  116. }
  117. int NCDInterfaceMonitor_Init (NCDInterfaceMonitor *o, BReactor *reactor, NCDInterfaceMonitor_handler handler, void *user)
  118. {
  119. // init arguments
  120. o->reactor = reactor;
  121. o->handler = handler;
  122. o->user = user;
  123. // init netlink fd
  124. if ((o->netlink_fd = socket(AF_NETLINK, SOCK_DGRAM, NETLINK_ROUTE)) < 0) {
  125. BLog(BLOG_ERROR, "socket failed");
  126. goto fail0;
  127. }
  128. // set fd non-blocking
  129. if (!badvpn_set_nonblocking(o->netlink_fd)) {
  130. BLog(BLOG_ERROR, "badvpn_set_nonblocking failed");
  131. goto fail1;
  132. }
  133. // bind netlink fd
  134. struct sockaddr_nl sa;
  135. memset(&sa, 0, sizeof(sa));
  136. sa.nl_family = AF_NETLINK;
  137. sa.nl_groups = RTMGRP_LINK;
  138. if (bind(o->netlink_fd, (void *)&sa, sizeof(sa)) < 0) {
  139. BLog(BLOG_ERROR, "bind failed");
  140. goto fail1;
  141. }
  142. // init BFileDescriptor
  143. BFileDescriptor_Init(&o->bfd, o->netlink_fd, (BFileDescriptor_handler)netlink_fd_handler, o);
  144. if (!BReactor_AddFileDescriptor(o->reactor, &o->bfd)) {
  145. BLog(BLOG_ERROR, "BReactor_AddFileDescriptor failed");
  146. goto fail1;
  147. }
  148. BReactor_SetFileDescriptorEvents(o->reactor, &o->bfd, BREACTOR_READ);
  149. // set nothing in buffer
  150. o->buf_left = -1;
  151. // init more job
  152. BPending_Init(&o->more_job, BReactor_PendingGroup(o->reactor), (BPending_handler)more_job_handler, o);
  153. DebugObject_Init(&o->d_obj);
  154. return 1;
  155. fail1:
  156. close(o->netlink_fd);
  157. fail0:
  158. return 0;
  159. }
  160. void NCDInterfaceMonitor_Free (NCDInterfaceMonitor *o)
  161. {
  162. DebugObject_Free(&o->d_obj);
  163. // free more job
  164. BPending_Free(&o->more_job);
  165. // free BFileDescriptor
  166. BReactor_RemoveFileDescriptor(o->reactor, &o->bfd);
  167. // close netlink fd
  168. close(o->netlink_fd);
  169. }
  170. void NCDInterfaceMonitor_Pause (NCDInterfaceMonitor *o)
  171. {
  172. DebugObject_Access(&o->d_obj);
  173. if (o->buf_left >= 0) {
  174. BPending_Unset(&o->more_job);
  175. } else {
  176. BReactor_SetFileDescriptorEvents(o->reactor, &o->bfd, 0);
  177. }
  178. }
  179. void NCDInterfaceMonitor_Continue (NCDInterfaceMonitor *o)
  180. {
  181. DebugObject_Access(&o->d_obj);
  182. if (o->buf_left >= 0) {
  183. BPending_Set(&o->more_job);
  184. } else {
  185. BReactor_SetFileDescriptorEvents(o->reactor, &o->bfd, BREACTOR_READ);
  186. }
  187. }