net_watch_interfaces.c 14 KB

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