net_watch_interfaces.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473
  1. /**
  2. * @file net_watch_interfaces.c
  3. * @author Ambroz Bizjak <ambrop7@gmail.com>
  4. *
  5. * @section LICENSE
  6. *
  7. * Redistribution and use in source and binary forms, with or without
  8. * modification, are permitted provided that the following conditions are met:
  9. * 1. Redistributions of source code must retain the above copyright
  10. * notice, this list of conditions and the following disclaimer.
  11. * 2. Redistributions in binary form must reproduce the above copyright
  12. * notice, this list of conditions and the following disclaimer in the
  13. * documentation and/or other materials provided with the distribution.
  14. * 3. Neither the name of the author nor the
  15. * names of its contributors may be used to endorse or promote products
  16. * derived from this software without specific prior written permission.
  17. *
  18. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
  19. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  20. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  21. * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
  22. * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  23. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  24. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  25. * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  26. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  27. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  28. *
  29. * @section DESCRIPTION
  30. *
  31. * Network interface watcher.
  32. *
  33. * Synopsis: net.watch_interfaces()
  34. * Description: reports network interface events. Transitions up when an event is detected, and
  35. * goes down waiting for the next event when net.watch_interfaces::nextevent() is called.
  36. * On startup, "added" events are reported for existing interfaces.
  37. * Variables:
  38. * string event_type - what happened with the interface: "added" or "removed". This may not be
  39. * consistent across events.
  40. * string devname - interface name
  41. * string bus - bus location, for example "pci:0000:06:00.0", "usb:2-1.3:1.0", or "unknown"
  42. *
  43. * Synopsis: net.watch_interfaces::nextevent()
  44. * Description: makes the watch_interfaces module transition down in order to report the next event.
  45. */
  46. #include <stdlib.h>
  47. #include <string.h>
  48. #include <regex.h>
  49. #include <misc/debug.h>
  50. #include <misc/offset.h>
  51. #include <misc/parse_number.h>
  52. #include <misc/bsize.h>
  53. #include <structure/LinkedList1.h>
  54. #include <udevmonitor/NCDUdevManager.h>
  55. #include <ncd/modules/event_template.h>
  56. #include <ncd/module_common.h>
  57. #include <generated/blog_channel_ncd_net_watch_interfaces.h>
  58. #define DEVPATH_REGEX "/net/[^/]+$"
  59. #define DEVPATH_USB_REGEX "/usb[^/]*(/[^/]+)+/([^/]+)/net/[^/]+$"
  60. #define DEVPATH_PCI_REGEX "/pci[^/]*/[^/]+/([^/]+)/net/[^/]+$"
  61. struct device {
  62. char *ifname;
  63. char *devpath;
  64. uintmax_t ifindex;
  65. BStringMap removed_map;
  66. LinkedList1Node devices_list_node;
  67. };
  68. struct instance {
  69. NCDModuleInst *i;
  70. NCDUdevClient client;
  71. LinkedList1 devices_list;
  72. regex_t reg;
  73. regex_t usb_reg;
  74. regex_t pci_reg;
  75. event_template templ;
  76. };
  77. static void templ_func_free (struct instance *o, int is_error);
  78. static struct device * find_device_by_ifname (struct instance *o, const char *ifname)
  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->ifname, ifname)) {
  84. return device;
  85. }
  86. list_node = LinkedList1Node_Next(list_node);
  87. }
  88. return NULL;
  89. }
  90. static struct device * find_device_by_devpath (struct instance *o, const char *devpath)
  91. {
  92. LinkedList1Node *list_node = LinkedList1_GetFirst(&o->devices_list);
  93. while (list_node) {
  94. struct device *device = UPPER_OBJECT(list_node, struct device, devices_list_node);
  95. if (!strcmp(device->devpath, devpath)) {
  96. return device;
  97. }
  98. list_node = LinkedList1Node_Next(list_node);
  99. }
  100. return NULL;
  101. }
  102. static void free_device (struct instance *o, struct device *device, int have_removed_map)
  103. {
  104. // remove from devices list
  105. LinkedList1_Remove(&o->devices_list, &device->devices_list_node);
  106. // free removed map
  107. if (have_removed_map) {
  108. BStringMap_Free(&device->removed_map);
  109. }
  110. // free devpath
  111. free(device->devpath);
  112. // free ifname
  113. free(device->ifname);
  114. // free structure
  115. free(device);
  116. }
  117. static int make_event_map (struct instance *o, int added, const char *ifname, const char *bus, BStringMap *out_map)
  118. {
  119. // init map
  120. BStringMap map;
  121. BStringMap_Init(&map);
  122. // set type
  123. if (!BStringMap_Set(&map, "event_type", (added ? "added" : "removed"))) {
  124. ModuleLog(o->i, BLOG_ERROR, "BStringMap_Set failed");
  125. goto fail1;
  126. }
  127. // set ifname
  128. if (!BStringMap_Set(&map, "devname", ifname)) {
  129. ModuleLog(o->i, BLOG_ERROR, "BStringMap_Set failed");
  130. goto fail1;
  131. }
  132. // set bus
  133. if (!BStringMap_Set(&map, "bus", bus)) {
  134. ModuleLog(o->i, BLOG_ERROR, "BStringMap_Set failed");
  135. goto fail1;
  136. }
  137. *out_map = map;
  138. return 1;
  139. fail1:
  140. BStringMap_Free(&map);
  141. return 0;
  142. }
  143. static void queue_event (struct instance *o, BStringMap map)
  144. {
  145. // pass event to template
  146. int was_empty;
  147. event_template_queue(&o->templ, map, &was_empty);
  148. // if event queue was empty, stop receiving udev events
  149. if (was_empty) {
  150. NCDUdevClient_Pause(&o->client);
  151. }
  152. }
  153. static void add_device (struct instance *o, const char *ifname, const char *devpath, uintmax_t ifindex, const char *bus)
  154. {
  155. ASSERT(!find_device_by_ifname(o, ifname))
  156. ASSERT(!find_device_by_devpath(o, devpath))
  157. // allocate structure
  158. struct device *device = malloc(sizeof(*device));
  159. if (!device) {
  160. ModuleLog(o->i, BLOG_ERROR, "malloc failed");
  161. goto fail0;
  162. }
  163. // init ifname
  164. if (!(device->ifname = strdup(ifname))) {
  165. ModuleLog(o->i, BLOG_ERROR, "strdup failed");
  166. goto fail1;
  167. }
  168. // init devpath
  169. if (!(device->devpath = strdup(devpath))) {
  170. ModuleLog(o->i, BLOG_ERROR, "strdup failed");
  171. goto fail2;
  172. }
  173. // set ifindex
  174. device->ifindex = ifindex;
  175. // init removed map
  176. if (!make_event_map(o, 0, ifname, bus, &device->removed_map)) {
  177. ModuleLog(o->i, BLOG_ERROR, "make_event_map failed");
  178. goto fail3;
  179. }
  180. // init added map
  181. BStringMap added_map;
  182. if (!make_event_map(o, 1, ifname, bus, &added_map)) {
  183. ModuleLog(o->i, BLOG_ERROR, "make_event_map failed");
  184. goto fail4;
  185. }
  186. // insert to devices list
  187. LinkedList1_Append(&o->devices_list, &device->devices_list_node);
  188. // queue event
  189. queue_event(o, added_map);
  190. return;
  191. fail4:
  192. BStringMap_Free(&device->removed_map);
  193. fail3:
  194. free(device->devpath);
  195. fail2:
  196. free(device->ifname);
  197. fail1:
  198. free(device);
  199. fail0:
  200. ModuleLog(o->i, BLOG_ERROR, "failed to add device %s", ifname);
  201. }
  202. static void remove_device (struct instance *o, struct device *device)
  203. {
  204. queue_event(o, device->removed_map);
  205. free_device(o, device, 0);
  206. }
  207. static void next_event (struct instance *o)
  208. {
  209. ASSERT(event_template_is_enabled(&o->templ))
  210. // order template to finish the current event
  211. int is_empty;
  212. event_template_dequeue(&o->templ, &is_empty);
  213. // if template has no events, continue udev events
  214. if (is_empty) {
  215. NCDUdevClient_Continue(&o->client);
  216. }
  217. }
  218. static void make_bus (struct instance *o, const char *devpath, const BStringMap *map, char *out_bus, size_t bus_avail)
  219. {
  220. regmatch_t pmatch[3];
  221. const char *type;
  222. const char *id;
  223. size_t id_len;
  224. if (!regexec(&o->usb_reg, devpath, 3, pmatch, 0)) {
  225. type = "usb";
  226. id = devpath + pmatch[2].rm_so;
  227. id_len = pmatch[2].rm_eo - pmatch[2].rm_so;
  228. }
  229. else if (!regexec(&o->pci_reg, devpath, 3, pmatch, 0)) {
  230. type = "pci";
  231. id = devpath + pmatch[1].rm_so;
  232. id_len = pmatch[1].rm_eo - pmatch[1].rm_so;
  233. } else {
  234. goto fail;
  235. }
  236. size_t type_len = strlen(type);
  237. bsize_t bus_len = bsize_add(bsize_fromsize(type_len), bsize_add(bsize_fromint(1), bsize_add(bsize_fromsize(id_len), bsize_fromint(1))));
  238. if (bus_len.is_overflow || bus_len.value > bus_avail) {
  239. goto fail;
  240. }
  241. memcpy(out_bus, type, type_len);
  242. out_bus[type_len] = ':';
  243. memcpy(out_bus + type_len + 1, id, id_len);
  244. out_bus[type_len + 1 + id_len] = '\0';
  245. return;
  246. fail:
  247. snprintf(out_bus, bus_avail, "%s", "unknown");
  248. }
  249. static void client_handler (struct instance *o, char *devpath, int have_map, BStringMap map)
  250. {
  251. // lookup existing device with this devpath
  252. struct device *ex_device = find_device_by_devpath(o, devpath);
  253. // lookup cache entry
  254. const BStringMap *cache_map = NCDUdevManager_Query(o->i->params->iparams->umanager, devpath);
  255. if (!cache_map) {
  256. if (ex_device) {
  257. remove_device(o, ex_device);
  258. }
  259. goto out;
  260. }
  261. int match_res = regexec(&o->reg, devpath, 0, NULL, 0);
  262. const char *interface = BStringMap_Get(cache_map, "INTERFACE");
  263. const char *ifindex_str = BStringMap_Get(cache_map, "IFINDEX");
  264. uintmax_t ifindex;
  265. if (!(!match_res && interface && ifindex_str && parse_unsigned_integer(ifindex_str, &ifindex))) {
  266. if (ex_device) {
  267. remove_device(o, ex_device);
  268. }
  269. goto out;
  270. }
  271. if (ex_device && (strcmp(ex_device->ifname, interface) || ex_device->ifindex != ifindex)) {
  272. remove_device(o, ex_device);
  273. ex_device = NULL;
  274. }
  275. if (!ex_device) {
  276. struct device *ex_ifname_device = find_device_by_ifname(o, interface);
  277. if (ex_ifname_device) {
  278. remove_device(o, ex_ifname_device);
  279. }
  280. char bus[128];
  281. make_bus(o, devpath, cache_map, bus, sizeof(bus));
  282. add_device(o, interface, devpath, ifindex, bus);
  283. }
  284. out:
  285. free(devpath);
  286. if (have_map) {
  287. BStringMap_Free(&map);
  288. }
  289. }
  290. static void func_new (void *vo, NCDModuleInst *i, const struct NCDModuleInst_new_params *params)
  291. {
  292. struct instance *o = vo;
  293. o->i = i;
  294. // check arguments
  295. if (!NCDVal_ListRead(params->args, 0)) {
  296. ModuleLog(o->i, BLOG_ERROR, "wrong arity");
  297. goto fail0;
  298. }
  299. // init client
  300. NCDUdevClient_Init(&o->client, o->i->params->iparams->umanager, o, (NCDUdevClient_handler)client_handler);
  301. // init devices list
  302. LinkedList1_Init(&o->devices_list);
  303. // compile regex's
  304. if (regcomp(&o->reg, DEVPATH_REGEX, REG_EXTENDED)) {
  305. ModuleLog(o->i, BLOG_ERROR, "regcomp failed");
  306. goto fail1;
  307. }
  308. if (regcomp(&o->usb_reg, DEVPATH_USB_REGEX, REG_EXTENDED)) {
  309. ModuleLog(o->i, BLOG_ERROR, "regcomp failed");
  310. goto fail2;
  311. }
  312. if (regcomp(&o->pci_reg, DEVPATH_PCI_REGEX, REG_EXTENDED)) {
  313. ModuleLog(o->i, BLOG_ERROR, "regcomp failed");
  314. goto fail3;
  315. }
  316. event_template_new(&o->templ, o->i, BLOG_CURRENT_CHANNEL, 3, o, (event_template_func_free)templ_func_free);
  317. return;
  318. fail3:
  319. regfree(&o->usb_reg);
  320. fail2:
  321. regfree(&o->reg);
  322. fail1:
  323. NCDUdevClient_Free(&o->client);
  324. fail0:
  325. NCDModuleInst_Backend_DeadError(i);
  326. }
  327. static void templ_func_free (struct instance *o, int is_error)
  328. {
  329. // free devices
  330. LinkedList1Node *list_node;
  331. while (list_node = LinkedList1_GetFirst(&o->devices_list)) {
  332. struct device *device = UPPER_OBJECT(list_node, struct device, devices_list_node);
  333. free_device(o, device, 1);
  334. }
  335. // free regex's
  336. regfree(&o->pci_reg);
  337. regfree(&o->usb_reg);
  338. regfree(&o->reg);
  339. // free client
  340. NCDUdevClient_Free(&o->client);
  341. if (is_error) {
  342. NCDModuleInst_Backend_DeadError(o->i);
  343. } else {
  344. NCDModuleInst_Backend_Dead(o->i);
  345. }
  346. }
  347. static void func_die (void *vo)
  348. {
  349. struct instance *o = vo;
  350. event_template_die(&o->templ);
  351. }
  352. static int func_getvar (void *vo, const char *name, NCDValMem *mem, NCDValRef *out)
  353. {
  354. struct instance *o = vo;
  355. return event_template_getvar(&o->templ, name, mem, out);
  356. }
  357. static void nextevent_func_new (void *unused, NCDModuleInst *i, const struct NCDModuleInst_new_params *params)
  358. {
  359. // check arguments
  360. if (!NCDVal_ListRead(params->args, 0)) {
  361. ModuleLog(i, BLOG_ERROR, "wrong arity");
  362. goto fail0;
  363. }
  364. // get method object
  365. struct instance *mo = NCDModuleInst_Backend_GetUser((NCDModuleInst *)params->method_user);
  366. // make sure we are currently reporting an event
  367. if (!event_template_is_enabled(&mo->templ)) {
  368. ModuleLog(i, BLOG_ERROR, "not reporting an event");
  369. goto fail0;
  370. }
  371. // signal up.
  372. // Do it before finishing the event so our process does not advance any further if
  373. // we would be killed the event provider going down.
  374. NCDModuleInst_Backend_Up(i);
  375. // wait for next event
  376. next_event(mo);
  377. return;
  378. fail0:
  379. NCDModuleInst_Backend_DeadError(i);
  380. }
  381. static struct NCDModule modules[] = {
  382. {
  383. .type = "net.watch_interfaces",
  384. .func_new2 = func_new,
  385. .func_die = func_die,
  386. .func_getvar = func_getvar,
  387. .alloc_size = sizeof(struct instance)
  388. }, {
  389. .type = "net.watch_interfaces::nextevent",
  390. .func_new2 = nextevent_func_new
  391. }, {
  392. .type = NULL
  393. }
  394. };
  395. const struct NCDModuleGroup ncdmodule_net_watch_interfaces = {
  396. .modules = modules
  397. };