interface-event.c 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  1. /*
  2. * netifd - network interface daemon
  3. * Copyright (C) 2012 Felix Fietkau <nbd@openwrt.org>
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License version 2
  7. * as published by the Free Software Foundation
  8. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. */
  14. #include <stdio.h>
  15. #include <stdlib.h>
  16. #include <string.h>
  17. #include <libubox/uloop.h>
  18. #include "netifd.h"
  19. #include "interface.h"
  20. #include "ubus.h"
  21. char *hotplug_cmd_path = DEFAULT_HOTPLUG_PATH;
  22. static struct interface *current;
  23. static enum interface_event current_ev;
  24. static struct list_head pending = LIST_HEAD_INIT(pending);
  25. static void task_complete(struct uloop_process *proc, int ret);
  26. static struct uloop_process task = {
  27. .cb = task_complete,
  28. };
  29. static const char * const eventnames[] = {
  30. [IFEV_DOWN] = "ifdown",
  31. [IFEV_UP] = "ifup",
  32. [IFEV_UP_FAILED] = "ifup-failed",
  33. [IFEV_UPDATE] = "ifupdate",
  34. [IFEV_FREE] = "free",
  35. [IFEV_RELOAD] = "reload",
  36. [IFEV_LINK_UP] = "iflink",
  37. [IFEV_CREATE] = "create",
  38. };
  39. static void
  40. run_cmd(const char *ifname, const char *device, enum interface_event event,
  41. enum interface_update_flags updated)
  42. {
  43. char *argv[3];
  44. int pid;
  45. pid = fork();
  46. if (pid < 0)
  47. return task_complete(NULL, -1);
  48. if (pid > 0) {
  49. task.pid = pid;
  50. uloop_process_add(&task);
  51. return;
  52. }
  53. setenv("ACTION", eventnames[event], 1);
  54. setenv("INTERFACE", ifname, 1);
  55. if (device)
  56. setenv("DEVICE", device, 1);
  57. if (event == IFEV_UPDATE) {
  58. if (updated & IUF_ADDRESS)
  59. setenv("IFUPDATE_ADDRESSES", "1", 1);
  60. if (updated & IUF_ROUTE)
  61. setenv("IFUPDATE_ROUTES", "1", 1);
  62. if (updated & IUF_PREFIX)
  63. setenv("IFUPDATE_PREFIXES", "1", 1);
  64. if (updated & IUF_DATA)
  65. setenv("IFUPDATE_DATA", "1", 1);
  66. }
  67. argv[0] = hotplug_cmd_path;
  68. argv[1] = "iface";
  69. argv[2] = NULL;
  70. execvp(argv[0], argv);
  71. exit(127);
  72. }
  73. static void
  74. call_hotplug(void)
  75. {
  76. const char *device = NULL;
  77. if (list_empty(&pending))
  78. return;
  79. current = list_first_entry(&pending, struct interface, hotplug_list);
  80. current_ev = current->hotplug_ev;
  81. list_del_init(&current->hotplug_list);
  82. if ((current_ev == IFEV_UP || current_ev == IFEV_UPDATE) && current->l3_dev.dev)
  83. device = current->l3_dev.dev->ifname;
  84. D(SYSTEM, "Call hotplug handler for interface '%s', event '%s' (%s)\n",
  85. current->name, eventnames[current_ev], device ? device : "none");
  86. run_cmd(current->name, device, current_ev, current->updated);
  87. }
  88. static void
  89. task_complete(struct uloop_process *proc, int ret)
  90. {
  91. if (current)
  92. D(SYSTEM, "Complete hotplug handler for interface '%s'\n", current->name);
  93. current = NULL;
  94. call_hotplug();
  95. }
  96. /*
  97. * Queue an interface for an up/down event.
  98. * An interface can only have one event in the queue and one
  99. * event waiting for completion.
  100. * When queueing an event that is the same as the one waiting for
  101. * completion, remove the interface from the queue
  102. */
  103. static void
  104. interface_queue_event(struct interface *iface, enum interface_event ev)
  105. {
  106. D(SYSTEM, "Queue hotplug handler for interface '%s', event '%s'\n",
  107. iface->name, eventnames[ev]);
  108. if (ev == IFEV_UP || ev == IFEV_DOWN)
  109. netifd_ubus_interface_event(iface, ev == IFEV_UP);
  110. netifd_ubus_interface_notify(iface, ev != IFEV_DOWN);
  111. /* no hotplug.d calls for link up */
  112. if (ev == IFEV_LINK_UP)
  113. return;
  114. if (current == iface) {
  115. /* an event for iface is being processed */
  116. if (!list_empty(&iface->hotplug_list)) {
  117. /* an additional event for iface is pending */
  118. /* overwrite pending event if it differs from */
  119. /* an update */
  120. if (ev != IFEV_UPDATE)
  121. iface->hotplug_ev = ev;
  122. }
  123. else {
  124. /* no additional event for iface is pending */
  125. if (ev != current_ev || ev == IFEV_UPDATE) {
  126. /* only add the interface to the pending list if
  127. * the event is different from the one being
  128. * handled or if it is an update */
  129. iface->hotplug_ev = ev;
  130. /* Handle hotplug calls FIFO */
  131. list_add_tail(&iface->hotplug_list, &pending);
  132. }
  133. }
  134. }
  135. else {
  136. /* currently not handling an event or handling an event
  137. * for another interface */
  138. if (!list_empty(&iface->hotplug_list)) {
  139. /* an event for iface is pending */
  140. if (!(iface->hotplug_ev == IFEV_UP &&
  141. ev == IFEV_UPDATE)) {
  142. /* overwrite pending event, unless the incoming
  143. * event is an ifupdate while the pending one
  144. * is an ifup */
  145. iface->hotplug_ev = ev;
  146. }
  147. }
  148. else {
  149. /* an event for the interface is not yet pending,
  150. * queue it */
  151. iface->hotplug_ev = ev;
  152. /* Handle hotplug calls FIFO */
  153. list_add_tail(&iface->hotplug_list, &pending);
  154. }
  155. }
  156. if (!task.pending && !current)
  157. call_hotplug();
  158. }
  159. static void
  160. interface_dequeue_event(struct interface *iface)
  161. {
  162. if (iface == current)
  163. current = NULL;
  164. if (!list_empty(&iface->hotplug_list))
  165. list_del_init(&iface->hotplug_list);
  166. }
  167. static void interface_event_cb(struct interface_user *dep, struct interface *iface,
  168. enum interface_event ev)
  169. {
  170. switch (ev) {
  171. case IFEV_LINK_UP:
  172. case IFEV_UP:
  173. case IFEV_UP_FAILED:
  174. case IFEV_UPDATE:
  175. case IFEV_DOWN:
  176. interface_queue_event(iface, ev);
  177. break;
  178. case IFEV_FREE:
  179. interface_dequeue_event(iface);
  180. break;
  181. default:
  182. break;
  183. }
  184. }
  185. static struct interface_user event_user = {
  186. .cb = interface_event_cb
  187. };
  188. static void __init interface_event_init(void)
  189. {
  190. interface_add_user(&event_user, NULL);
  191. }