2 * netifd - network interface daemon
3 * Copyright (C) 2012 Felix Fietkau <nbd@openwrt.org>
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
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.
18 #include <libubox/uloop.h>
21 #include "interface.h"
24 char *hotplug_cmd_path = DEFAULT_HOTPLUG_PATH;
25 static struct interface *current;
26 static enum interface_event current_ev;
27 static struct list_head pending = LIST_HEAD_INIT(pending);
29 static void task_complete(struct uloop_process *proc, int ret);
30 static struct uloop_process task = {
33 static const char * const eventnames[] = {"ifdown", "ifup", "ifupdate"};
36 run_cmd(const char *ifname, const char *device, enum interface_event event,
37 enum interface_update_flags updated)
44 return task_complete(NULL, -1);
48 uloop_process_add(&task);
52 setenv("ACTION", eventnames[event], 1);
53 setenv("INTERFACE", ifname, 1);
55 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);
68 argv[0] = hotplug_cmd_path;
71 execvp(argv[0], argv);
78 const char *device = NULL;
79 if (list_empty(&pending))
82 current = list_first_entry(&pending, struct interface, hotplug_list);
83 current_ev = current->hotplug_ev;
84 list_del_init(¤t->hotplug_list);
86 if ((current_ev == IFEV_UP || current_ev == IFEV_UPDATE) && current->l3_dev.dev)
87 device = current->l3_dev.dev->ifname;
89 D(SYSTEM, "Call hotplug handler for interface '%s', event '%s' (%s)\n",
90 current->name, eventnames[current_ev], device ? device : "none");
91 run_cmd(current->name, device, current_ev, current->updated);
95 task_complete(struct uloop_process *proc, int ret)
98 D(SYSTEM, "Complete hotplug handler for interface '%s'\n", current->name);
104 * Queue an interface for an up/down event.
105 * An interface can only have one event in the queue and one
106 * event waiting for completion.
107 * When queueing an event that is the same as the one waiting for
108 * completion, remove the interface from the queue
111 interface_queue_event(struct interface *iface, enum interface_event ev)
113 D(SYSTEM, "Queue hotplug handler for interface '%s', event '%s'\n",
114 iface->name, eventnames[ev]);
115 if (ev == IFEV_UP || ev == IFEV_DOWN)
116 netifd_ubus_interface_event(iface, ev == IFEV_UP);
118 netifd_ubus_interface_notify(iface, ev != IFEV_DOWN);
120 if (current == iface) {
121 /* an event for iface is being processed */
122 if (!list_empty(&iface->hotplug_list)) {
123 /* an additional event for iface is pending */
124 /* overwrite pending event if it differs from */
126 if (ev != IFEV_UPDATE)
127 iface->hotplug_ev = ev;
130 /* no additional event for iface is pending */
131 if (ev != current_ev || ev == IFEV_UPDATE) {
132 /* only add the interface to the pending list if
133 * the event is different from the one being
134 * handled or if it is an update */
135 iface->hotplug_ev = ev;
136 /* Handle hotplug calls FIFO */
137 list_add_tail(&iface->hotplug_list, &pending);
142 /* currently not handling an event or handling an event
143 * for another interface */
144 if (!list_empty(&iface->hotplug_list)) {
145 /* an event for iface is pending */
146 if (!(iface->hotplug_ev == IFEV_UP &&
147 ev == IFEV_UPDATE)) {
148 /* overwrite pending event, unless the incoming
149 * event is an ifupdate while the pending one
151 iface->hotplug_ev = ev;
155 /* an event for the interface is not yet pending,
157 iface->hotplug_ev = ev;
158 /* Handle hotplug calls FIFO */
159 list_add_tail(&iface->hotplug_list, &pending);
163 if (!task.pending && !current)
168 interface_dequeue_event(struct interface *iface)
170 if (iface == current)
173 if (!list_empty(&iface->hotplug_list))
174 list_del_init(&iface->hotplug_list);
177 static void interface_event_cb(struct interface_user *dep, struct interface *iface,
178 enum interface_event ev)
184 interface_queue_event(iface, ev);
188 interface_dequeue_event(iface);
193 static struct interface_user event_user = {
194 .cb = interface_event_cb
197 static void __init interface_event_init(void)
199 interface_add_user(&event_user, NULL);