net_watch_interfaces.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482
  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. * string bus - bus location, for example "pci:0000:06:00.0", "usb:2-1.3:1.0", or "unknown"
  35. *
  36. * Synopsis: net.watch_interfaces::nextevent()
  37. * Description: makes the watch_interfaces module transition down in order to report the next event.
  38. */
  39. #include <stdlib.h>
  40. #include <string.h>
  41. #include <regex.h>
  42. #include <misc/debug.h>
  43. #include <misc/offset.h>
  44. #include <misc/parse_number.h>
  45. #include <misc/bsize.h>
  46. #include <structure/LinkedList1.h>
  47. #include <udevmonitor/NCDUdevManager.h>
  48. #include <ncd/NCDModule.h>
  49. #include <ncd/modules/event_template.h>
  50. #include <generated/blog_channel_ncd_net_watch_interfaces.h>
  51. #define ModuleLog(i, ...) NCDModuleInst_Backend_Log((i), BLOG_CURRENT_CHANNEL, __VA_ARGS__)
  52. struct device {
  53. char *ifname;
  54. char *devpath;
  55. uintmax_t ifindex;
  56. BStringMap removed_map;
  57. LinkedList1Node devices_list_node;
  58. };
  59. struct instance {
  60. NCDModuleInst *i;
  61. NCDUdevClient client;
  62. LinkedList1 devices_list;
  63. regex_t preg;
  64. event_template templ;
  65. };
  66. struct nextevent_instance {
  67. NCDModuleInst *i;
  68. };
  69. static void templ_func_free (struct instance *o);
  70. static struct device * find_device_by_ifname (struct instance *o, const char *ifname)
  71. {
  72. LinkedList1Node *list_node = LinkedList1_GetFirst(&o->devices_list);
  73. while (list_node) {
  74. struct device *device = UPPER_OBJECT(list_node, struct device, devices_list_node);
  75. if (!strcmp(device->ifname, ifname)) {
  76. return device;
  77. }
  78. list_node = LinkedList1Node_Next(list_node);
  79. }
  80. return NULL;
  81. }
  82. static struct device * find_device_by_devpath (struct instance *o, const char *devpath)
  83. {
  84. LinkedList1Node *list_node = LinkedList1_GetFirst(&o->devices_list);
  85. while (list_node) {
  86. struct device *device = UPPER_OBJECT(list_node, struct device, devices_list_node);
  87. if (!strcmp(device->devpath, devpath)) {
  88. return device;
  89. }
  90. list_node = LinkedList1Node_Next(list_node);
  91. }
  92. return NULL;
  93. }
  94. static void free_device (struct instance *o, struct device *device, int have_removed_map)
  95. {
  96. // remove from devices list
  97. LinkedList1_Remove(&o->devices_list, &device->devices_list_node);
  98. // free removed map
  99. if (have_removed_map) {
  100. BStringMap_Free(&device->removed_map);
  101. }
  102. // free devpath
  103. free(device->devpath);
  104. // free ifname
  105. free(device->ifname);
  106. // free structure
  107. free(device);
  108. }
  109. static int make_event_map (struct instance *o, int added, const char *ifname, const char *bus, BStringMap *out_map)
  110. {
  111. // init map
  112. BStringMap map;
  113. BStringMap_Init(&map);
  114. // set type
  115. if (!BStringMap_Set(&map, "event_type", (added ? "added" : "removed"))) {
  116. ModuleLog(o->i, BLOG_ERROR, "BStringMap_Set failed");
  117. goto fail1;
  118. }
  119. // set ifname
  120. if (!BStringMap_Set(&map, "devname", ifname)) {
  121. ModuleLog(o->i, BLOG_ERROR, "BStringMap_Set failed");
  122. goto fail1;
  123. }
  124. // set bus
  125. if (!BStringMap_Set(&map, "bus", bus)) {
  126. ModuleLog(o->i, BLOG_ERROR, "BStringMap_Set failed");
  127. goto fail1;
  128. }
  129. *out_map = map;
  130. return 1;
  131. fail1:
  132. BStringMap_Free(&map);
  133. return 0;
  134. }
  135. static void queue_event (struct instance *o, BStringMap map)
  136. {
  137. // pass event to template
  138. int was_empty;
  139. event_template_queue(&o->templ, map, &was_empty);
  140. // if event queue was empty, stop receiving udev events
  141. if (was_empty) {
  142. NCDUdevClient_Pause(&o->client);
  143. }
  144. }
  145. static void add_device (struct instance *o, const char *ifname, const char *devpath, uintmax_t ifindex, const char *bus)
  146. {
  147. ASSERT(!find_device_by_ifname(o, ifname))
  148. ASSERT(!find_device_by_devpath(o, devpath))
  149. // allocate structure
  150. struct device *device = malloc(sizeof(*device));
  151. if (!device) {
  152. ModuleLog(o->i, BLOG_ERROR, "malloc failed");
  153. goto fail0;
  154. }
  155. // init ifname
  156. if (!(device->ifname = strdup(ifname))) {
  157. ModuleLog(o->i, BLOG_ERROR, "strdup failed");
  158. goto fail1;
  159. }
  160. // init devpath
  161. if (!(device->devpath = strdup(devpath))) {
  162. ModuleLog(o->i, BLOG_ERROR, "strdup failed");
  163. goto fail2;
  164. }
  165. // set ifindex
  166. device->ifindex = ifindex;
  167. // init removed map
  168. if (!make_event_map(o, 0, ifname, bus, &device->removed_map)) {
  169. ModuleLog(o->i, BLOG_ERROR, "make_event_map failed");
  170. goto fail3;
  171. }
  172. // init added map
  173. BStringMap added_map;
  174. if (!make_event_map(o, 1, ifname, bus, &added_map)) {
  175. ModuleLog(o->i, BLOG_ERROR, "make_event_map failed");
  176. goto fail4;
  177. }
  178. // insert to devices list
  179. LinkedList1_Append(&o->devices_list, &device->devices_list_node);
  180. // queue event
  181. queue_event(o, added_map);
  182. return;
  183. fail4:
  184. BStringMap_Free(&device->removed_map);
  185. fail3:
  186. free(device->devpath);
  187. fail2:
  188. free(device->ifname);
  189. fail1:
  190. free(device);
  191. fail0:
  192. ModuleLog(o->i, BLOG_ERROR, "failed to add device %s", ifname);
  193. }
  194. static void remove_device (struct instance *o, struct device *device)
  195. {
  196. queue_event(o, device->removed_map);
  197. free_device(o, device, 0);
  198. }
  199. static void next_event (struct instance *o)
  200. {
  201. event_template_assert_enabled(&o->templ);
  202. // order template to finish the current event
  203. int is_empty;
  204. event_template_dequeue(&o->templ, &is_empty);
  205. // if template has no events, continue udev events
  206. if (is_empty) {
  207. NCDUdevClient_Continue(&o->client);
  208. }
  209. }
  210. static void make_bus (struct instance *o, const char *devpath, const BStringMap *map, char *out_bus, size_t bus_avail)
  211. {
  212. const char *type = BStringMap_Get(map, "ID_BUS");
  213. if (!type) {
  214. goto fail;
  215. }
  216. size_t type_len = strlen(type);
  217. if (strcmp(type, "pci") && strcmp(type, "usb")) {
  218. goto fail;
  219. }
  220. regmatch_t pmatch[2];
  221. if (regexec(&o->preg, devpath, 2, pmatch, 0)) {
  222. goto fail;
  223. }
  224. const char *id = devpath + pmatch[1].rm_so;
  225. size_t id_len = pmatch[1].rm_eo - pmatch[1].rm_so;
  226. bsize_t bus_len = bsize_add(bsize_fromsize(type_len), bsize_add(bsize_fromint(1), bsize_add(bsize_fromsize(id_len), bsize_fromint(1))));
  227. if (bus_len.is_overflow || bus_len.value > bus_avail) {
  228. goto fail;
  229. }
  230. memcpy(out_bus, type, type_len);
  231. out_bus[type_len] = ':';
  232. memcpy(out_bus + type_len + 1, id, id_len);
  233. out_bus[type_len + 1 + id_len] = '\0';
  234. return;
  235. fail:
  236. snprintf(out_bus, bus_avail, "%s", "unknown");
  237. }
  238. static void client_handler (struct instance *o, char *devpath, int have_map, BStringMap map)
  239. {
  240. // lookup existing device with this devpath
  241. struct device *ex_device = find_device_by_devpath(o, devpath);
  242. // lookup cache entry
  243. const BStringMap *cache_map = NCDUdevManager_Query(o->i->umanager, devpath);
  244. if (!cache_map) {
  245. if (ex_device) {
  246. remove_device(o, ex_device);
  247. }
  248. goto out;
  249. }
  250. const char *subsystem = BStringMap_Get(cache_map, "SUBSYSTEM");
  251. const char *interface = BStringMap_Get(cache_map, "INTERFACE");
  252. const char *ifindex_str = BStringMap_Get(cache_map, "IFINDEX");
  253. uintmax_t ifindex;
  254. if (!(subsystem && !strcmp(subsystem, "net") && interface && ifindex_str && parse_unsigned_integer(ifindex_str, &ifindex))) {
  255. if (ex_device) {
  256. remove_device(o, ex_device);
  257. }
  258. goto out;
  259. }
  260. if (ex_device && (strcmp(ex_device->ifname, interface) || ex_device->ifindex != ifindex)) {
  261. remove_device(o, ex_device);
  262. ex_device = NULL;
  263. }
  264. if (!ex_device) {
  265. struct device *ex_ifname_device = find_device_by_ifname(o, interface);
  266. if (ex_ifname_device) {
  267. remove_device(o, ex_ifname_device);
  268. }
  269. char bus[128];
  270. make_bus(o, devpath, cache_map, bus, sizeof(bus));
  271. add_device(o, interface, devpath, ifindex, bus);
  272. }
  273. out:
  274. free(devpath);
  275. if (have_map) {
  276. BStringMap_Free(&map);
  277. }
  278. }
  279. static void func_new (NCDModuleInst *i)
  280. {
  281. // allocate instance
  282. struct instance *o = malloc(sizeof(*o));
  283. if (!o) {
  284. ModuleLog(i, BLOG_ERROR, "failed to allocate instance");
  285. goto fail0;
  286. }
  287. NCDModuleInst_Backend_SetUser(i, o);
  288. // init arguments
  289. o->i = i;
  290. // check arguments
  291. if (!NCDValue_ListRead(o->i->args, 0)) {
  292. ModuleLog(o->i, BLOG_ERROR, "wrong arity");
  293. goto fail1;
  294. }
  295. // init client
  296. NCDUdevClient_Init(&o->client, o->i->umanager, o, (NCDUdevClient_handler)client_handler);
  297. // init devices list
  298. LinkedList1_Init(&o->devices_list);
  299. // compile regex
  300. if (regcomp(&o->preg, "/([^/]+)/net/", REG_EXTENDED)) {
  301. ModuleLog(o->i, BLOG_ERROR, "regcomp failed");
  302. goto fail2;
  303. }
  304. event_template_new(&o->templ, o->i, BLOG_CURRENT_CHANNEL, 3, o, (event_template_func_free)templ_func_free);
  305. return;
  306. fail2:
  307. NCDUdevClient_Free(&o->client);
  308. fail1:
  309. free(o);
  310. fail0:
  311. NCDModuleInst_Backend_SetError(i);
  312. NCDModuleInst_Backend_Dead(i);
  313. }
  314. static void templ_func_free (struct instance *o)
  315. {
  316. NCDModuleInst *i = o->i;
  317. // free devices
  318. LinkedList1Node *list_node;
  319. while (list_node = LinkedList1_GetFirst(&o->devices_list)) {
  320. struct device *device = UPPER_OBJECT(list_node, struct device, devices_list_node);
  321. free_device(o, device, 1);
  322. }
  323. // free regex
  324. regfree(&o->preg);
  325. // free client
  326. NCDUdevClient_Free(&o->client);
  327. // free instance
  328. free(o);
  329. NCDModuleInst_Backend_Dead(i);
  330. }
  331. static void func_die (void *vo)
  332. {
  333. struct instance *o = vo;
  334. event_template_die(&o->templ);
  335. }
  336. static int func_getvar (void *vo, const char *name, NCDValue *out)
  337. {
  338. struct instance *o = vo;
  339. return event_template_getvar(&o->templ, name, out);
  340. }
  341. static void nextevent_func_new (NCDModuleInst *i)
  342. {
  343. // allocate instance
  344. struct nextevent_instance *o = malloc(sizeof(*o));
  345. if (!o) {
  346. ModuleLog(i, BLOG_ERROR, "failed to allocate instance");
  347. goto fail0;
  348. }
  349. NCDModuleInst_Backend_SetUser(i, o);
  350. // init arguments
  351. o->i = i;
  352. // check arguments
  353. if (!NCDValue_ListRead(o->i->args, 0)) {
  354. ModuleLog(o->i, BLOG_ERROR, "wrong arity");
  355. goto fail1;
  356. }
  357. // get method object
  358. struct instance *mo = i->method_object->inst_user;
  359. event_template_assert_enabled(&mo->templ);
  360. // signal up.
  361. // Do it before finishing the event so our process does not advance any further if
  362. // we would be killed the event provider going down.
  363. NCDModuleInst_Backend_Up(o->i);
  364. // wait for next event
  365. next_event(mo);
  366. return;
  367. fail1:
  368. free(o);
  369. fail0:
  370. NCDModuleInst_Backend_SetError(i);
  371. NCDModuleInst_Backend_Dead(i);
  372. }
  373. static void nextevent_func_die (void *vo)
  374. {
  375. struct nextevent_instance *o = vo;
  376. NCDModuleInst *i = o->i;
  377. // free instance
  378. free(o);
  379. NCDModuleInst_Backend_Dead(i);
  380. }
  381. static const struct NCDModule modules[] = {
  382. {
  383. .type = "net.watch_interfaces",
  384. .func_new = func_new,
  385. .func_die = func_die,
  386. .func_getvar = func_getvar
  387. }, {
  388. .type = "net.watch_interfaces::nextevent",
  389. .func_new = nextevent_func_new,
  390. .func_die = nextevent_func_die
  391. }, {
  392. .type = NULL
  393. }
  394. };
  395. const struct NCDModuleGroup ncdmodule_net_watch_interfaces = {
  396. .modules = modules
  397. };