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