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
[] = {
34 [IFEV_DOWN
] = "ifdown",
36 [IFEV_UP_FAILED
] = "ifup-failed",
37 [IFEV_UPDATE
] = "ifupdate",
39 [IFEV_RELOAD
] = "reload",
40 [IFEV_LINK_UP
] = "iflink",
41 [IFEV_CREATE
] = "create",
45 run_cmd(const char *ifname
, const char *device
, enum interface_event event
,
46 enum interface_update_flags updated
)
53 return task_complete(NULL
, -1);
57 uloop_process_add(&task
);
61 setenv("ACTION", eventnames
[event
], 1);
62 setenv("INTERFACE", ifname
, 1);
64 setenv("DEVICE", device
, 1);
66 if (event
== IFEV_UPDATE
) {
67 if (updated
& IUF_ADDRESS
)
68 setenv("IFUPDATE_ADDRESSES", "1", 1);
69 if (updated
& IUF_ROUTE
)
70 setenv("IFUPDATE_ROUTES", "1", 1);
71 if (updated
& IUF_PREFIX
)
72 setenv("IFUPDATE_PREFIXES", "1", 1);
73 if (updated
& IUF_DATA
)
74 setenv("IFUPDATE_DATA", "1", 1);
77 argv
[0] = hotplug_cmd_path
;
80 execvp(argv
[0], argv
);
87 const char *device
= NULL
;
88 if (list_empty(&pending
))
91 current
= list_first_entry(&pending
, struct interface
, hotplug_list
);
92 current_ev
= current
->hotplug_ev
;
93 list_del_init(¤t
->hotplug_list
);
95 if ((current_ev
== IFEV_UP
|| current_ev
== IFEV_UPDATE
) && current
->l3_dev
.dev
)
96 device
= current
->l3_dev
.dev
->ifname
;
98 D(SYSTEM
, "Call hotplug handler for interface '%s', event '%s' (%s)\n",
99 current
->name
, eventnames
[current_ev
], device
? device
: "none");
100 run_cmd(current
->name
, device
, current_ev
, current
->updated
);
104 task_complete(struct uloop_process
*proc
, int ret
)
107 D(SYSTEM
, "Complete hotplug handler for interface '%s'\n", current
->name
);
113 * Queue an interface for an up/down event.
114 * An interface can only have one event in the queue and one
115 * event waiting for completion.
116 * When queueing an event that is the same as the one waiting for
117 * completion, remove the interface from the queue
120 interface_queue_event(struct interface
*iface
, enum interface_event ev
)
122 D(SYSTEM
, "Queue hotplug handler for interface '%s', event '%s'\n",
123 iface
->name
, eventnames
[ev
]);
124 if (ev
== IFEV_UP
|| ev
== IFEV_DOWN
)
125 netifd_ubus_interface_event(iface
, ev
== IFEV_UP
);
127 netifd_ubus_interface_notify(iface
, ev
!= IFEV_DOWN
);
129 /* no hotplug.d calls for link up */
130 if (ev
== IFEV_LINK_UP
)
133 if (current
== iface
) {
134 /* an event for iface is being processed */
135 if (!list_empty(&iface
->hotplug_list
)) {
136 /* an additional event for iface is pending */
137 /* overwrite pending event if it differs from */
139 if (ev
!= IFEV_UPDATE
)
140 iface
->hotplug_ev
= ev
;
143 /* no additional event for iface is pending */
144 if (ev
!= current_ev
|| ev
== IFEV_UPDATE
) {
145 /* only add the interface to the pending list if
146 * the event is different from the one being
147 * handled or if it is an update */
148 iface
->hotplug_ev
= ev
;
149 /* Handle hotplug calls FIFO */
150 list_add_tail(&iface
->hotplug_list
, &pending
);
155 /* currently not handling an event or handling an event
156 * for another interface */
157 if (!list_empty(&iface
->hotplug_list
)) {
158 /* an event for iface is pending */
159 if (!(iface
->hotplug_ev
== IFEV_UP
&&
160 ev
== IFEV_UPDATE
)) {
161 /* overwrite pending event, unless the incoming
162 * event is an ifupdate while the pending one
164 iface
->hotplug_ev
= ev
;
168 /* an event for the interface is not yet pending,
170 iface
->hotplug_ev
= ev
;
171 /* Handle hotplug calls FIFO */
172 list_add_tail(&iface
->hotplug_list
, &pending
);
176 if (!task
.pending
&& !current
)
181 interface_dequeue_event(struct interface
*iface
)
183 if (iface
== current
)
186 if (!list_empty(&iface
->hotplug_list
))
187 list_del_init(&iface
->hotplug_list
);
190 static void interface_event_cb(struct interface_user
*dep
, struct interface
*iface
,
191 enum interface_event ev
)
199 interface_queue_event(iface
, ev
);
202 interface_dequeue_event(iface
);
209 static struct interface_user event_user
= {
210 .cb
= interface_event_cb
213 static void __init
interface_event_init(void)
215 interface_add_user(&event_user
, NULL
);