NCDInterfaceMonitor.c 5.4 KB

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