NCDInterfaceMonitor.c 5.1 KB

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