NCDInterfaceMonitor.c 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  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 <ncd/NCDInterfaceMonitor.h>
  35. static void netlink_fd_handler (NCDInterfaceMonitor *o, int events);
  36. static void process_buffer (NCDInterfaceMonitor *o);
  37. static void more_job_handler (NCDInterfaceMonitor *o);
  38. void netlink_fd_handler (NCDInterfaceMonitor *o, int events)
  39. {
  40. DebugObject_Access(&o->d_obj);
  41. ASSERT(o->buf_left == -1)
  42. // read from netlink fd
  43. int len = read(o->netlink_fd, o->buf, sizeof(o->buf));
  44. if (len < 0) {
  45. DEBUG("read failed");
  46. return;
  47. }
  48. // stop receiving fd events
  49. BReactor_SetFileDescriptorEvents(o->reactor, &o->bfd, 0);
  50. // set buffer
  51. o->buf_nh = (struct nlmsghdr *)o->buf;
  52. o->buf_left = len;
  53. // process buffer
  54. process_buffer(o);
  55. return;
  56. }
  57. void process_buffer (NCDInterfaceMonitor *o)
  58. {
  59. ASSERT(o->buf_left >= 0)
  60. for (; NLMSG_OK(o->buf_nh, o->buf_left); o->buf_nh = NLMSG_NEXT(o->buf_nh, o->buf_left)) {
  61. if (o->buf_nh->nlmsg_type == NLMSG_DONE) {
  62. break;
  63. }
  64. if (o->buf_nh->nlmsg_type != RTM_NEWLINK && o->buf_nh->nlmsg_type != RTM_DELLINK) {
  65. continue;
  66. }
  67. void *pl = NLMSG_DATA(o->buf_nh);
  68. int pl_len = NLMSG_PAYLOAD(o->buf_nh, 0);
  69. if (pl_len < sizeof(struct ifinfomsg)) {
  70. DEBUG("missing infomsg");
  71. continue;
  72. }
  73. struct ifinfomsg *im = (void *)pl;
  74. // parse attributes to get interface name
  75. char *ifname = NULL;
  76. int rta_len = pl_len - sizeof(struct ifinfomsg);
  77. for (struct rtattr *rta = (void *)(im + 1); RTA_OK(rta, rta_len); rta = RTA_NEXT(rta, rta_len)) {
  78. uint8_t *attr = RTA_DATA(rta);
  79. int attr_len = RTA_PAYLOAD(rta);
  80. if (rta->rta_type == IFLA_IFNAME && attr_len > 0 && attr[attr_len - 1] == '\0') {
  81. ifname = (char *)attr;
  82. }
  83. }
  84. if (!ifname) {
  85. continue;
  86. }
  87. // finish this message
  88. o->buf_nh = NLMSG_NEXT(o->buf_nh, o->buf_left);
  89. // schedule more job
  90. BPending_Set(&o->more_job);
  91. // dispatch event
  92. o->handler(o->user, ifname, NCDIfConfig_query(ifname));
  93. return;
  94. }
  95. // set no buffer
  96. o->buf_left = -1;
  97. // continue receiving fd events
  98. BReactor_SetFileDescriptorEvents(o->reactor, &o->bfd, BREACTOR_READ);
  99. }
  100. void more_job_handler (NCDInterfaceMonitor *o)
  101. {
  102. DebugObject_Access(&o->d_obj);
  103. ASSERT(o->buf_left >= 0)
  104. // process buffer
  105. process_buffer(o);
  106. return;
  107. }
  108. int NCDInterfaceMonitor_Init (NCDInterfaceMonitor *o, BReactor *reactor, NCDInterfaceMonitor_handler handler, void *user)
  109. {
  110. // init arguments
  111. o->reactor = reactor;
  112. o->handler = handler;
  113. o->user = user;
  114. // init netlink fd
  115. if ((o->netlink_fd = socket(AF_NETLINK, SOCK_DGRAM, NETLINK_ROUTE)) < 0) {
  116. DEBUG("socket failed");
  117. goto fail0;
  118. }
  119. // set fd non-blocking
  120. if (!badvpn_set_nonblocking(o->netlink_fd)) {
  121. DEBUG("badvpn_set_nonblocking failed");
  122. goto fail1;
  123. }
  124. // bind netlink fd
  125. struct sockaddr_nl sa;
  126. memset(&sa, 0, sizeof(sa));
  127. sa.nl_family = AF_NETLINK;
  128. sa.nl_groups = RTMGRP_LINK;
  129. if (bind(o->netlink_fd, (void *)&sa, sizeof(sa)) < 0) {
  130. DEBUG("bind failed");
  131. goto fail1;
  132. }
  133. // init BFileDescriptor
  134. BFileDescriptor_Init(&o->bfd, o->netlink_fd, (BFileDescriptor_handler)netlink_fd_handler, o);
  135. if (!BReactor_AddFileDescriptor(o->reactor, &o->bfd)) {
  136. DEBUG("BReactor_AddFileDescriptor failed");
  137. goto fail1;
  138. }
  139. BReactor_SetFileDescriptorEvents(o->reactor, &o->bfd, BREACTOR_READ);
  140. // set nothing in buffer
  141. o->buf_left = -1;
  142. // init more job
  143. BPending_Init(&o->more_job, BReactor_PendingGroup(o->reactor), (BPending_handler)more_job_handler, o);
  144. DebugObject_Init(&o->d_obj);
  145. return 1;
  146. fail1:
  147. close(o->netlink_fd);
  148. fail0:
  149. return 0;
  150. }
  151. void NCDInterfaceMonitor_Free (NCDInterfaceMonitor *o)
  152. {
  153. DebugObject_Free(&o->d_obj);
  154. // free more job
  155. BPending_Free(&o->more_job);
  156. // free BFileDescriptor
  157. BReactor_RemoveFileDescriptor(o->reactor, &o->bfd);
  158. // close netlink fd
  159. close(o->netlink_fd);
  160. }
  161. void NCDInterfaceMonitor_Pause (NCDInterfaceMonitor *o)
  162. {
  163. DebugObject_Access(&o->d_obj);
  164. if (o->buf_left >= 0) {
  165. BPending_Unset(&o->more_job);
  166. } else {
  167. BReactor_SetFileDescriptorEvents(o->reactor, &o->bfd, 0);
  168. }
  169. }
  170. void NCDInterfaceMonitor_Continue (NCDInterfaceMonitor *o)
  171. {
  172. DebugObject_Access(&o->d_obj);
  173. if (o->buf_left >= 0) {
  174. BPending_Set(&o->more_job);
  175. } else {
  176. BReactor_SetFileDescriptorEvents(o->reactor, &o->bfd, BREACTOR_READ);
  177. }
  178. }