e12d3569dfe0b4ada12ec8cb4139d0a50eab8841
9 static LIST_HEAD(interfaces
);
12 clear_interface_errors(struct interface
*iface
)
14 struct interface_error
*error
, *tmp
;
16 list_for_each_entry_safe(error
, tmp
, &iface
->errors
, list
) {
17 list_del(&error
->list
);
22 void interface_add_error(struct interface
*iface
, const char *subsystem
,
23 const char *code
, const char **data
, int n_data
)
25 struct interface_error
*error
;
31 len
= n_data
* sizeof(char *);
32 datalen
= alloca(len
);
33 for (i
= 0; i
< n_data
; i
++) {
34 datalen
[i
] = strlen(data
[i
]) + 1;
39 error
= calloc(1, sizeof(*error
) + sizeof(char *) + len
);
43 list_add_tail(&error
->list
, &iface
->errors
);
44 error
->subsystem
= subsystem
;
47 dest
= (char *) &error
->data
[n_data
+ 1];
48 for (i
= 0; i
< n_data
; i
++) {
49 error
->data
[i
] = dest
;
50 memcpy(dest
, data
[i
], datalen
[i
]);
53 error
->data
[n_data
] = NULL
;
57 interface_event(struct interface
*iface
, enum interface_event ev
)
63 mark_interface_down(struct interface
*iface
)
65 release_device(iface
->main_dev
.dev
);
66 iface
->state
= IFS_DOWN
;
70 __set_interface_up(struct interface
*iface
)
74 if (iface
->state
!= IFS_DOWN
)
77 ret
= claim_device(iface
->main_dev
.dev
);
81 iface
->state
= IFS_SETUP
;
82 ret
= iface
->proto
->handler(iface
->proto
, PROTO_CMD_SETUP
, false);
84 mark_interface_down(iface
);
93 __set_interface_down(struct interface
*iface
, bool force
)
95 clear_interface_errors(iface
);
97 if (iface
->state
== IFS_DOWN
||
98 iface
->state
== IFS_TEARDOWN
)
101 iface
->state
= IFS_TEARDOWN
;
102 interface_event(iface
, IFEV_DOWN
);
104 iface
->proto
->handler(iface
->proto
, PROTO_CMD_TEARDOWN
, force
);
105 release_device(iface
->main_dev
.dev
);
109 interface_cb(struct device_user
*dep
, enum device_event ev
)
111 struct interface
*iface
;
114 iface
= container_of(dep
, struct interface
, main_dev
);
119 case DEV_EVENT_REMOVE
:
126 if (iface
->active
== new_state
)
129 iface
->active
= new_state
;
132 if (iface
->autostart
)
133 set_interface_up(iface
);
135 __set_interface_down(iface
, true);
139 interface_proto_cb(struct interface_proto_state
*state
, enum interface_proto_event ev
)
141 struct interface
*iface
= state
->iface
;
145 if (iface
->state
!= IFS_SETUP
)
148 iface
->state
= IFS_UP
;
149 interface_event(iface
, IFEV_UP
);
152 if (iface
->state
== IFS_DOWN
)
155 mark_interface_down(iface
);
160 void interface_set_proto_state(struct interface
*iface
, struct interface_proto_state
*state
)
163 iface
->proto
->handler(iface
->proto
, PROTO_CMD_TEARDOWN
, true);
164 iface
->proto
->free(iface
->proto
);
167 iface
->state
= IFS_DOWN
;
168 iface
->proto
= state
;
172 state
->proto_event
= interface_proto_cb
;
173 state
->iface
= iface
;
177 alloc_interface(const char *name
)
179 struct interface
*iface
;
181 iface
= get_interface(name
);
185 iface
= calloc(1, sizeof(*iface
));
187 interface_set_proto_state(iface
, get_default_proto());
193 iface
->main_dev
.cb
= interface_cb
;
194 iface
->l3_iface
= &iface
->main_dev
;
195 strncpy(iface
->name
, name
, sizeof(iface
->name
) - 1);
196 list_add(&iface
->list
, &interfaces
);
197 INIT_LIST_HEAD(&iface
->errors
);
199 netifd_ubus_add_interface(iface
);
205 free_interface(struct interface
*iface
)
207 netifd_ubus_remove_interface(iface
);
208 list_del(&iface
->list
);
209 if (iface
->proto
->free
)
210 iface
->proto
->free(iface
->proto
);
215 get_interface(const char *name
)
217 struct interface
*iface
;
219 list_for_each_entry(iface
, &interfaces
, list
) {
220 if (!strcmp(iface
->name
, name
))
227 interface_remove_link(struct interface
*iface
, struct device
*llif
)
229 struct device
*dev
= iface
->main_dev
.dev
;
231 if (dev
&& dev
->hotplug_ops
) {
232 dev
->hotplug_ops
->del(dev
, llif
);
236 remove_device_user(&iface
->main_dev
);
240 interface_add_link(struct interface
*iface
, struct device
*llif
)
242 struct device
*dev
= iface
->main_dev
.dev
;
244 if (dev
&& dev
->hotplug_ops
)
245 return dev
->hotplug_ops
->add(dev
, llif
);
247 if (iface
->main_dev
.dev
)
248 interface_remove_link(iface
, NULL
);
250 add_device_user(&iface
->main_dev
, llif
);
256 set_interface_up(struct interface
*iface
)
258 iface
->autostart
= true;
260 if (!iface
->active
) {
261 interface_add_error(iface
, "interface", "NO_DEVICE", NULL
, 0);
265 if (iface
->state
!= IFS_DOWN
)
268 return __set_interface_up(iface
);
272 set_interface_down(struct interface
*iface
)
274 iface
->autostart
= false;
275 __set_interface_down(iface
, false);