NCDInterfaceMonitor.c 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  1. /**
  2. * @file NCDInterfaceMonitor.c
  3. * @author Ambroz Bizjak <ambrop7@gmail.com>
  4. *
  5. * @section LICENSE
  6. *
  7. * This file is part of BadVPN.
  8. *
  9. * BadVPN is free software: you can redistribute it and/or modify
  10. * it under the terms of the GNU General Public License version 2
  11. * as published by the Free Software Foundation.
  12. *
  13. * BadVPN is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License along
  19. * with this program; if not, write to the Free Software Foundation, Inc.,
  20. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  21. */
  22. #include <string.h>
  23. #include <stdio.h>
  24. #include <unistd.h>
  25. #include <stdint.h>
  26. #include <sys/socket.h>
  27. #include <net/if.h>
  28. #include <linux/netlink.h>
  29. #include <linux/rtnetlink.h>
  30. #include <asm/types.h>
  31. #include <asm/types.h>
  32. #include <misc/debug.h>
  33. #include <misc/nonblocking.h>
  34. #include <base/BLog.h>
  35. #include <ncd/NCDInterfaceMonitor.h>
  36. #include <generated/blog_channel_NCDInterfaceMonitor.h>
  37. static void netlink_fd_handler (NCDInterfaceMonitor *o, int events);
  38. static void process_buffer (NCDInterfaceMonitor *o);
  39. static void more_job_handler (NCDInterfaceMonitor *o);
  40. void netlink_fd_handler (NCDInterfaceMonitor *o, int events)
  41. {
  42. DebugObject_Access(&o->d_obj);
  43. ASSERT(o->buf_left == -1)
  44. // read from netlink fd
  45. int len = read(o->netlink_fd, o->buf, sizeof(o->buf));
  46. if (len < 0) {
  47. BLog(BLOG_ERROR, "read failed");
  48. return;
  49. }
  50. // stop receiving fd events
  51. BReactor_SetFileDescriptorEvents(o->reactor, &o->bfd, 0);
  52. // set buffer
  53. o->buf_nh = (struct nlmsghdr *)o->buf;
  54. o->buf_left = len;
  55. // process buffer
  56. process_buffer(o);
  57. return;
  58. }
  59. void process_buffer (NCDInterfaceMonitor *o)
  60. {
  61. ASSERT(o->buf_left >= 0)
  62. for (; NLMSG_OK(o->buf_nh, o->buf_left); o->buf_nh = NLMSG_NEXT(o->buf_nh, o->buf_left)) {
  63. if (o->buf_nh->nlmsg_type == NLMSG_DONE) {
  64. break;
  65. }
  66. if (o->buf_nh->nlmsg_type != RTM_NEWLINK && o->buf_nh->nlmsg_type != RTM_DELLINK) {
  67. continue;
  68. }
  69. void *pl = NLMSG_DATA(o->buf_nh);
  70. int pl_len = NLMSG_PAYLOAD(o->buf_nh, 0);
  71. if (pl_len < sizeof(struct ifinfomsg)) {
  72. BLog(BLOG_ERROR, "missing infomsg");
  73. continue;
  74. }
  75. struct ifinfomsg *im = (void *)pl;
  76. // parse attributes to get interface name
  77. char *ifname = NULL;
  78. int rta_len = pl_len - sizeof(struct ifinfomsg);
  79. for (struct rtattr *rta = (void *)(im + 1); RTA_OK(rta, rta_len); rta = RTA_NEXT(rta, rta_len)) {
  80. uint8_t *attr = RTA_DATA(rta);
  81. int attr_len = RTA_PAYLOAD(rta);
  82. if (rta->rta_type == IFLA_IFNAME && attr_len > 0 && attr[attr_len - 1] == '\0') {
  83. ifname = (char *)attr;
  84. }
  85. }
  86. if (!ifname) {
  87. continue;
  88. }
  89. // finish this message
  90. o->buf_nh = NLMSG_NEXT(o->buf_nh, o->buf_left);
  91. // schedule more job
  92. BPending_Set(&o->more_job);
  93. // dispatch event
  94. o->handler(o->user, ifname, NCDIfConfig_query(ifname));
  95. return;
  96. }
  97. // set no buffer
  98. o->buf_left = -1;
  99. // continue receiving fd events
  100. BReactor_SetFileDescriptorEvents(o->reactor, &o->bfd, BREACTOR_READ);
  101. }
  102. void more_job_handler (NCDInterfaceMonitor *o)
  103. {
  104. DebugObject_Access(&o->d_obj);
  105. ASSERT(o->buf_left >= 0)
  106. // process buffer
  107. process_buffer(o);
  108. return;
  109. }
  110. int NCDInterfaceMonitor_Init (NCDInterfaceMonitor *o, BReactor *reactor, NCDInterfaceMonitor_handler handler, void *user)
  111. {
  112. // init arguments
  113. o->reactor = reactor;
  114. o->handler = handler;
  115. o->user = user;
  116. // init netlink fd
  117. if ((o->netlink_fd = socket(AF_NETLINK, SOCK_DGRAM, NETLINK_ROUTE)) < 0) {
  118. BLog(BLOG_ERROR, "socket failed");
  119. goto fail0;
  120. }
  121. // set fd non-blocking
  122. if (!badvpn_set_nonblocking(o->netlink_fd)) {
  123. BLog(BLOG_ERROR, "badvpn_set_nonblocking failed");
  124. goto fail1;
  125. }
  126. // bind netlink fd
  127. struct sockaddr_nl sa;
  128. memset(&sa, 0, sizeof(sa));
  129. sa.nl_family = AF_NETLINK;
  130. sa.nl_groups = RTMGRP_LINK;
  131. if (bind(o->netlink_fd, (void *)&sa, sizeof(sa)) < 0) {
  132. BLog(BLOG_ERROR, "bind failed");
  133. goto fail1;
  134. }
  135. // init BFileDescriptor
  136. BFileDescriptor_Init(&o->bfd, o->netlink_fd, (BFileDescriptor_handler)netlink_fd_handler, o);
  137. if (!BReactor_AddFileDescriptor(o->reactor, &o->bfd)) {
  138. BLog(BLOG_ERROR, "BReactor_AddFileDescriptor failed");
  139. goto fail1;
  140. }
  141. BReactor_SetFileDescriptorEvents(o->reactor, &o->bfd, BREACTOR_READ);
  142. // set nothing in buffer
  143. o->buf_left = -1;
  144. // init more job
  145. BPending_Init(&o->more_job, BReactor_PendingGroup(o->reactor), (BPending_handler)more_job_handler, o);
  146. DebugObject_Init(&o->d_obj);
  147. return 1;
  148. fail1:
  149. close(o->netlink_fd);
  150. fail0:
  151. return 0;
  152. }
  153. void NCDInterfaceMonitor_Free (NCDInterfaceMonitor *o)
  154. {
  155. DebugObject_Free(&o->d_obj);
  156. // free more job
  157. BPending_Free(&o->more_job);
  158. // free BFileDescriptor
  159. BReactor_RemoveFileDescriptor(o->reactor, &o->bfd);
  160. // close netlink fd
  161. close(o->netlink_fd);
  162. }
  163. void NCDInterfaceMonitor_Pause (NCDInterfaceMonitor *o)
  164. {
  165. DebugObject_Access(&o->d_obj);
  166. if (o->buf_left >= 0) {
  167. BPending_Unset(&o->more_job);
  168. } else {
  169. BReactor_SetFileDescriptorEvents(o->reactor, &o->bfd, 0);
  170. }
  171. }
  172. void NCDInterfaceMonitor_Continue (NCDInterfaceMonitor *o)
  173. {
  174. DebugObject_Access(&o->d_obj);
  175. if (o->buf_left >= 0) {
  176. BPending_Set(&o->more_job);
  177. } else {
  178. BReactor_SetFileDescriptorEvents(o->reactor, &o->bfd, BREACTOR_READ);
  179. }
  180. }