add a function for adding a virtual device (not tracked in the avl tree)
[project/netifd.git] / interface.c
1 #include <string.h>
2 #include <stdlib.h>
3 #include <stdio.h>
4
5 #include "netifd.h"
6 #include "ubus.h"
7
8 LIST_HEAD(interfaces);
9
10 static int interface_event(struct interface *iface, enum interface_event ev)
11 {
12         if (!iface->state || !iface->state->event)
13                 return 0;
14
15         return iface->state->event(iface, iface->state, ev);
16 }
17
18 static void
19 __set_interface_up(struct interface *iface)
20 {
21         if (iface->up)
22                 return;
23
24         if (claim_device(iface->main_dev.dev) < 0)
25                 return;
26
27         if (interface_event(iface, IFEV_UP) < 0) {
28                 release_device(iface->main_dev.dev);
29                 return;
30         }
31
32         iface->up = true;
33 }
34
35 static void
36 __set_interface_down(struct interface *iface)
37 {
38         if (!iface->up)
39                 return;
40
41         iface->up = false;
42         interface_event(iface, IFEV_DOWN);
43         release_device(iface->main_dev.dev);
44 }
45
46 static void
47 interface_cb(struct device_user *dep, enum device_event ev)
48 {
49         struct interface *iface;
50         bool new_state;
51
52         iface = container_of(dep, struct interface, main_dev);
53         switch (ev) {
54         case DEV_EVENT_ADD:
55                 new_state = true;
56                 break;
57         case DEV_EVENT_REMOVE:
58                 new_state = false;
59                 break;
60         default:
61                 return;
62         }
63
64         if (iface->active == new_state)
65                 return;
66
67         iface->active = new_state;
68
69         if (new_state) {
70                 if (iface->autostart)
71                         __set_interface_up(iface);
72         } else
73                 __set_interface_down(iface);
74 }
75
76 struct interface *
77 alloc_interface(const char *name)
78 {
79         struct interface *iface;
80
81         iface = get_interface(name);
82         if (iface)
83                 return iface;
84
85         iface = calloc(1, sizeof(*iface));
86         iface->main_dev.cb = interface_cb;
87         iface->l3_iface = &iface->main_dev;
88         strncpy(iface->name, name, sizeof(iface->name) - 1);
89         list_add(&iface->list, &interfaces);
90         netifd_ubus_add_interface(iface);
91
92         return iface;
93 }
94
95 void
96 free_interface(struct interface *iface)
97 {
98         netifd_ubus_remove_interface(iface);
99         list_del(&iface->list);
100         if (iface->state && iface->state->free)
101                 iface->state->free(iface, iface->state);
102         free(iface);
103 }
104
105 struct interface *
106 get_interface(const char *name)
107 {
108         struct interface *iface;
109
110         list_for_each_entry(iface, &interfaces, list) {
111                 if (!strcmp(iface->name, name))
112                         return iface;
113         }
114         return NULL;
115 }
116
117 void
118 interface_remove_link(struct interface *iface, struct device *llif)
119 {
120         struct device *dev = iface->main_dev.dev;
121
122         if (dev && dev->hotplug_ops) {
123                 dev->hotplug_ops->del(dev, llif);
124                 return;
125         }
126
127         remove_device_user(&iface->main_dev);
128 }
129
130 int
131 interface_add_link(struct interface *iface, struct device *llif)
132 {
133         struct device *dev = iface->main_dev.dev;
134
135         if (dev && dev->hotplug_ops)
136                 return dev->hotplug_ops->add(dev, llif);
137
138         if (iface->main_dev.dev)
139                 interface_remove_link(iface, NULL);
140
141         add_device_user(&iface->main_dev, llif);
142
143         return 0;
144 }
145
146 int
147 set_interface_up(struct interface *iface)
148 {
149         iface->autostart = true;
150
151         if (iface->up || !iface->active)
152                 return -1;
153
154         __set_interface_up(iface);
155         return 0;
156 }
157
158 int
159 set_interface_down(struct interface *iface)
160 {
161         iface->autostart = false;
162
163         if (!iface->up)
164                 return -1;
165
166         __set_interface_down(iface);
167
168         return 0;
169 }