net_watch_interfaces.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423
  1. /**
  2. * @file net_watch_interfaces.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. * @section DESCRIPTION
  23. *
  24. * Network interface watcher.
  25. *
  26. * Synopsis: net.watch_interfaces()
  27. * Description: reports network interface events. Transitions up when an event is detected, and
  28. * goes down waiting for the next event when net.watch_interfaces::nextevent() is called.
  29. * On startup, "added" events are reported for existing interfaces.
  30. * Variables:
  31. * string event_type - what happened with the interface: "added" or "removed". This may not be
  32. * consistent across events.
  33. * string devname - interface name
  34. *
  35. * Synopsis: net.watch_interfaces::nextevent()
  36. * Description: makes the watch_interfaces module transition down in order to report the next event.
  37. */
  38. #include <stdlib.h>
  39. #include <string.h>
  40. #include <misc/debug.h>
  41. #include <misc/offset.h>
  42. #include <misc/parse_number.h>
  43. #include <structure/LinkedList1.h>
  44. #include <udevmonitor/NCDUdevManager.h>
  45. #include <ncd/NCDModule.h>
  46. #include <ncd/modules/event_template.h>
  47. #include <generated/blog_channel_ncd_net_watch_interfaces.h>
  48. #define ModuleLog(i, ...) NCDModuleInst_Backend_Log((i), BLOG_CURRENT_CHANNEL, __VA_ARGS__)
  49. struct device {
  50. char *ifname;
  51. char *devpath;
  52. uintmax_t ifindex;
  53. BStringMap removed_map;
  54. LinkedList1Node devices_list_node;
  55. };
  56. struct instance {
  57. NCDModuleInst *i;
  58. NCDUdevClient client;
  59. LinkedList1 devices_list;
  60. event_template templ;
  61. };
  62. struct nextevent_instance {
  63. NCDModuleInst *i;
  64. };
  65. static void templ_func_free (struct instance *o);
  66. static struct device * find_device_by_ifname (struct instance *o, const char *ifname)
  67. {
  68. LinkedList1Node *list_node = LinkedList1_GetFirst(&o->devices_list);
  69. while (list_node) {
  70. struct device *device = UPPER_OBJECT(list_node, struct device, devices_list_node);
  71. if (!strcmp(device->ifname, ifname)) {
  72. return device;
  73. }
  74. list_node = LinkedList1Node_Next(list_node);
  75. }
  76. return NULL;
  77. }
  78. static struct device * find_device_by_devpath (struct instance *o, const char *devpath)
  79. {
  80. LinkedList1Node *list_node = LinkedList1_GetFirst(&o->devices_list);
  81. while (list_node) {
  82. struct device *device = UPPER_OBJECT(list_node, struct device, devices_list_node);
  83. if (!strcmp(device->devpath, devpath)) {
  84. return device;
  85. }
  86. list_node = LinkedList1Node_Next(list_node);
  87. }
  88. return NULL;
  89. }
  90. static void free_device (struct instance *o, struct device *device, int have_removed_map)
  91. {
  92. // remove from devices list
  93. LinkedList1_Remove(&o->devices_list, &device->devices_list_node);
  94. // free removed map
  95. if (have_removed_map) {
  96. BStringMap_Free(&device->removed_map);
  97. }
  98. // free devpath
  99. free(device->devpath);
  100. // free ifname
  101. free(device->ifname);
  102. // free structure
  103. free(device);
  104. }
  105. static int make_event_map (struct instance *o, int added, const char *ifname, BStringMap *out_map)
  106. {
  107. // init map
  108. BStringMap map;
  109. BStringMap_Init(&map);
  110. // set type
  111. if (!BStringMap_Set(&map, "event_type", (added ? "added" : "removed"))) {
  112. ModuleLog(o->i, BLOG_ERROR, "BStringMap_Set failed");
  113. goto fail1;
  114. }
  115. // set ifname
  116. if (!BStringMap_Set(&map, "devname", ifname)) {
  117. ModuleLog(o->i, BLOG_ERROR, "BStringMap_Set failed");
  118. goto fail1;
  119. }
  120. *out_map = map;
  121. return 1;
  122. fail1:
  123. BStringMap_Free(&map);
  124. return 0;
  125. }
  126. static void queue_event (struct instance *o, BStringMap map)
  127. {
  128. // pass event to template
  129. int was_empty;
  130. event_template_queue(&o->templ, map, &was_empty);
  131. // if event queue was empty, stop receiving udev events
  132. if (was_empty) {
  133. NCDUdevClient_Pause(&o->client);
  134. }
  135. }
  136. static void add_device (struct instance *o, const char *ifname, const char *devpath, uintmax_t ifindex)
  137. {
  138. ASSERT(!find_device_by_ifname(o, ifname))
  139. ASSERT(!find_device_by_devpath(o, devpath))
  140. // allocate structure
  141. struct device *device = malloc(sizeof(*device));
  142. if (!device) {
  143. ModuleLog(o->i, BLOG_ERROR, "malloc failed");
  144. goto fail0;
  145. }
  146. // init ifname
  147. if (!(device->ifname = strdup(ifname))) {
  148. ModuleLog(o->i, BLOG_ERROR, "strdup failed");
  149. goto fail1;
  150. }
  151. // init devpath
  152. if (!(device->devpath = strdup(devpath))) {
  153. ModuleLog(o->i, BLOG_ERROR, "strdup failed");
  154. goto fail2;
  155. }
  156. // set ifindex
  157. device->ifindex = ifindex;
  158. // init removed map
  159. if (!make_event_map(o, 0, ifname, &device->removed_map)) {
  160. ModuleLog(o->i, BLOG_ERROR, "make_event_map failed");
  161. goto fail3;
  162. }
  163. // init added map
  164. BStringMap added_map;
  165. if (!make_event_map(o, 1, ifname, &added_map)) {
  166. ModuleLog(o->i, BLOG_ERROR, "make_event_map failed");
  167. goto fail4;
  168. }
  169. // insert to devices list
  170. LinkedList1_Append(&o->devices_list, &device->devices_list_node);
  171. // queue event
  172. queue_event(o, added_map);
  173. return;
  174. fail4:
  175. BStringMap_Free(&device->removed_map);
  176. fail3:
  177. free(device->devpath);
  178. fail2:
  179. free(device->ifname);
  180. fail1:
  181. free(device);
  182. fail0:
  183. ModuleLog(o->i, BLOG_ERROR, "failed to add device %s", ifname);
  184. }
  185. static void remove_device (struct instance *o, struct device *device)
  186. {
  187. queue_event(o, device->removed_map);
  188. free_device(o, device, 0);
  189. }
  190. static void next_event (struct instance *o)
  191. {
  192. event_template_assert_enabled(&o->templ);
  193. // order template to finish the current event
  194. int is_empty;
  195. event_template_dequeue(&o->templ, &is_empty);
  196. // if template has no events, continue udev events
  197. if (is_empty) {
  198. NCDUdevClient_Continue(&o->client);
  199. }
  200. }
  201. static void client_handler (struct instance *o, char *devpath, int have_map, BStringMap map)
  202. {
  203. // lookup existing device with this devpath
  204. struct device *ex_device = find_device_by_devpath(o, devpath);
  205. // lookup cache entry
  206. const BStringMap *cache_map = NCDUdevManager_Query(o->i->umanager, devpath);
  207. if (!cache_map) {
  208. if (ex_device) {
  209. remove_device(o, ex_device);
  210. }
  211. goto out;
  212. }
  213. const char *subsystem = BStringMap_Get(cache_map, "SUBSYSTEM");
  214. const char *interface = BStringMap_Get(cache_map, "INTERFACE");
  215. const char *ifindex_str = BStringMap_Get(cache_map, "IFINDEX");
  216. uintmax_t ifindex;
  217. if (!(subsystem && !strcmp(subsystem, "net") && interface && ifindex_str && parse_unsigned_integer(ifindex_str, &ifindex))) {
  218. if (ex_device) {
  219. remove_device(o, ex_device);
  220. }
  221. goto out;
  222. }
  223. if (ex_device && (strcmp(ex_device->ifname, interface) || ex_device->ifindex != ifindex)) {
  224. remove_device(o, ex_device);
  225. ex_device = NULL;
  226. }
  227. if (!ex_device) {
  228. struct device *ex_ifname_device = find_device_by_ifname(o, interface);
  229. if (ex_ifname_device) {
  230. remove_device(o, ex_ifname_device);
  231. }
  232. add_device(o, interface, devpath, ifindex);
  233. }
  234. out:
  235. free(devpath);
  236. if (have_map) {
  237. BStringMap_Free(&map);
  238. }
  239. }
  240. static void func_new (NCDModuleInst *i)
  241. {
  242. // allocate instance
  243. struct instance *o = malloc(sizeof(*o));
  244. if (!o) {
  245. ModuleLog(i, BLOG_ERROR, "failed to allocate instance");
  246. goto fail0;
  247. }
  248. NCDModuleInst_Backend_SetUser(i, o);
  249. // init arguments
  250. o->i = i;
  251. // check arguments
  252. if (!NCDValue_ListRead(o->i->args, 0)) {
  253. ModuleLog(o->i, BLOG_ERROR, "wrong arity");
  254. goto fail1;
  255. }
  256. // init client
  257. NCDUdevClient_Init(&o->client, o->i->umanager, o, (NCDUdevClient_handler)client_handler);
  258. // init devices list
  259. LinkedList1_Init(&o->devices_list);
  260. event_template_new(&o->templ, o->i, BLOG_CURRENT_CHANNEL, 3, o, (event_template_func_free)templ_func_free);
  261. return;
  262. fail1:
  263. free(o);
  264. fail0:
  265. NCDModuleInst_Backend_SetError(i);
  266. NCDModuleInst_Backend_Event(i, NCDMODULE_EVENT_DEAD);
  267. }
  268. static void templ_func_free (struct instance *o)
  269. {
  270. NCDModuleInst *i = o->i;
  271. // free devices
  272. LinkedList1Node *list_node;
  273. while (list_node = LinkedList1_GetFirst(&o->devices_list)) {
  274. struct device *device = UPPER_OBJECT(list_node, struct device, devices_list_node);
  275. free_device(o, device, 1);
  276. }
  277. // free client
  278. NCDUdevClient_Free(&o->client);
  279. // free instance
  280. free(o);
  281. NCDModuleInst_Backend_Event(i, NCDMODULE_EVENT_DEAD);
  282. }
  283. static void func_die (void *vo)
  284. {
  285. struct instance *o = vo;
  286. event_template_die(&o->templ);
  287. }
  288. static int func_getvar (void *vo, const char *name, NCDValue *out)
  289. {
  290. struct instance *o = vo;
  291. return event_template_getvar(&o->templ, name, out);
  292. }
  293. static void nextevent_func_new (NCDModuleInst *i)
  294. {
  295. // allocate instance
  296. struct nextevent_instance *o = malloc(sizeof(*o));
  297. if (!o) {
  298. ModuleLog(i, BLOG_ERROR, "failed to allocate instance");
  299. goto fail0;
  300. }
  301. NCDModuleInst_Backend_SetUser(i, o);
  302. // init arguments
  303. o->i = i;
  304. // check arguments
  305. if (!NCDValue_ListRead(o->i->args, 0)) {
  306. ModuleLog(o->i, BLOG_ERROR, "wrong arity");
  307. goto fail1;
  308. }
  309. // get method object
  310. struct instance *mo = i->method_object->inst_user;
  311. event_template_assert_enabled(&mo->templ);
  312. // signal up.
  313. // Do it before finishing the event so our process does not advance any further if
  314. // we would be killed the event provider going down.
  315. NCDModuleInst_Backend_Event(o->i, NCDMODULE_EVENT_UP);
  316. // wait for next event
  317. next_event(mo);
  318. return;
  319. fail1:
  320. free(o);
  321. fail0:
  322. NCDModuleInst_Backend_SetError(i);
  323. NCDModuleInst_Backend_Event(i, NCDMODULE_EVENT_DEAD);
  324. }
  325. static void nextevent_func_die (void *vo)
  326. {
  327. struct nextevent_instance *o = vo;
  328. NCDModuleInst *i = o->i;
  329. // free instance
  330. free(o);
  331. NCDModuleInst_Backend_Event(i, NCDMODULE_EVENT_DEAD);
  332. }
  333. static const struct NCDModule modules[] = {
  334. {
  335. .type = "net.watch_interfaces",
  336. .func_new = func_new,
  337. .func_die = func_die,
  338. .func_getvar = func_getvar
  339. }, {
  340. .type = "net.watch_interfaces::nextevent",
  341. .func_new = nextevent_func_new,
  342. .func_die = nextevent_func_die
  343. }, {
  344. .type = NULL
  345. }
  346. };
  347. const struct NCDModuleGroup ncdmodule_net_watch_interfaces = {
  348. .modules = modules
  349. };