NCDInterfaceMonitor.c 5.2 KB

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