interface-event.c 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  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. task_complete(NULL, -1);
  48. return;
  49. }
  50. if (pid > 0) {
  51. task.pid = pid;
  52. uloop_process_add(&task);
  53. return;
  54. }
  55. setenv("ACTION", eventnames[event], 1);
  56. setenv("INTERFACE", ifname, 1);
  57. if (device)
  58. setenv("DEVICE", device, 1);
  59. if (event == IFEV_UPDATE) {
  60. if (updated & IUF_ADDRESS)
  61. setenv("IFUPDATE_ADDRESSES", "1", 1);
  62. if (updated & IUF_ROUTE)
  63. setenv("IFUPDATE_ROUTES", "1", 1);
  64. if (updated & IUF_PREFIX)
  65. setenv("IFUPDATE_PREFIXES", "1", 1);
  66. if (updated & IUF_DATA)
  67. setenv("IFUPDATE_DATA", "1", 1);
  68. }
  69. argv[0] = hotplug_cmd_path;
  70. argv[1] = "iface";
  71. argv[2] = NULL;
  72. execvp(argv[0], argv);
  73. exit(127);
  74. }
  75. static void
  76. call_hotplug(void)
  77. {
  78. const char *device = NULL;
  79. if (list_empty(&pending))
  80. return;
  81. current = list_first_entry(&pending, struct interface, hotplug_list);
  82. current_ev = current->hotplug_ev;
  83. list_del_init(&current->hotplug_list);
  84. if ((current_ev == IFEV_UP || current_ev == IFEV_UPDATE) && current->l3_dev.dev)
  85. device = current->l3_dev.dev->ifname;
  86. D(SYSTEM, "Call hotplug handler for interface '%s', event '%s' (%s)",
  87. current->name, eventnames[current_ev], device ? device : "none");
  88. run_cmd(current->name, device, current_ev, current->updated);
  89. }
  90. static void
  91. task_complete(struct uloop_process *proc, int ret)
  92. {
  93. if (current)
  94. D(SYSTEM, "Complete hotplug handler for interface '%s'", current->name);
  95. current = NULL;
  96. call_hotplug();
  97. }
  98. /*
  99. * Queue an interface for an up/down event.
  100. * An interface can only have one event in the queue and one
  101. * event waiting for completion.
  102. * When queueing an event that is the same as the one waiting for
  103. * completion, remove the interface from the queue
  104. */
  105. static void
  106. interface_queue_event(struct interface *iface, enum interface_event ev)
  107. {
  108. D(SYSTEM, "Queue hotplug handler for interface '%s', event '%s'",
  109. iface->name, eventnames[ev]);
  110. if (ev == IFEV_UP || ev == IFEV_DOWN)
  111. netifd_ubus_interface_event(iface, ev == IFEV_UP);
  112. netifd_ubus_interface_notify(iface, ev != IFEV_DOWN);
  113. /* no hotplug.d calls for link up */
  114. if (ev == IFEV_LINK_UP)
  115. return;
  116. if (current == iface) {
  117. /* an event for iface is being processed */
  118. if (!list_empty(&iface->hotplug_list)) {
  119. /* an additional event for iface is pending */
  120. /* overwrite pending event if it differs from */
  121. /* an update */
  122. if (ev != IFEV_UPDATE)
  123. iface->hotplug_ev = ev;
  124. }
  125. else {
  126. /* no additional event for iface is pending */
  127. if (ev != current_ev || ev == IFEV_UPDATE) {
  128. /* only add the interface to the pending list if
  129. * the event is different from the one being
  130. * handled or if it is an update */
  131. iface->hotplug_ev = ev;
  132. /* Handle hotplug calls FIFO */
  133. list_add_tail(&iface->hotplug_list, &pending);
  134. }
  135. }
  136. }
  137. else {
  138. /* currently not handling an event or handling an event
  139. * for another interface */
  140. if (!list_empty(&iface->hotplug_list)) {
  141. /* an event for iface is pending */
  142. if (!(iface->hotplug_ev == IFEV_UP &&
  143. ev == IFEV_UPDATE)) {
  144. /* overwrite pending event, unless the incoming
  145. * event is an ifupdate while the pending one
  146. * is an ifup */
  147. iface->hotplug_ev = ev;
  148. }
  149. }
  150. else {
  151. /* an event for the interface is not yet pending,
  152. * queue it */
  153. iface->hotplug_ev = ev;
  154. /* Handle hotplug calls FIFO */
  155. list_add_tail(&iface->hotplug_list, &pending);
  156. }
  157. }
  158. if (!task.pending && !current)
  159. call_hotplug();
  160. }
  161. static void
  162. interface_dequeue_event(struct interface *iface)
  163. {
  164. if (iface == current)
  165. current = NULL;
  166. if (!list_empty(&iface->hotplug_list))
  167. list_del_init(&iface->hotplug_list);
  168. }
  169. static void interface_event_cb(struct interface_user *dep, struct interface *iface,
  170. enum interface_event ev)
  171. {
  172. switch (ev) {
  173. case IFEV_LINK_UP:
  174. case IFEV_UP:
  175. case IFEV_UP_FAILED:
  176. case IFEV_UPDATE:
  177. case IFEV_DOWN:
  178. interface_queue_event(iface, ev);
  179. break;
  180. case IFEV_FREE:
  181. interface_dequeue_event(iface);
  182. break;
  183. default:
  184. break;
  185. }
  186. }
  187. static struct interface_user event_user = {
  188. .cb = interface_event_cb
  189. };
  190. static void __init interface_event_init(void)
  191. {
  192. interface_add_user(&event_user, NULL);
  193. }