net_watch_interfaces.c 14 KB

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