12 static LIST_HEAD(interfaces
);
21 static const union config_param_info iface_attr_info
[IFACE_ATTR_MAX
] = {
22 [IFACE_ATTR_IFNAME
].type
= BLOBMSG_TYPE_STRING
,
25 static const struct blobmsg_policy iface_attrs
[IFACE_ATTR_MAX
] = {
26 [IFACE_ATTR_PROTO
] = { .name
= "proto", .type
= BLOBMSG_TYPE_STRING
},
27 [IFACE_ATTR_IFNAME
] = { .name
= "ifname", .type
= BLOBMSG_TYPE_STRING
},
28 [IFACE_ATTR_AUTO
] = { .name
= "auto", .type
= BLOBMSG_TYPE_BOOL
},
31 const struct config_param_list interface_attr_list
= {
32 .n_params
= IFACE_ATTR_MAX
,
33 .params
= iface_attrs
,
34 .info
= iface_attr_info
,
38 clear_interface_errors(struct interface
*iface
)
40 struct interface_error
*error
, *tmp
;
42 list_for_each_entry_safe(error
, tmp
, &iface
->errors
, list
) {
43 list_del(&error
->list
);
48 void interface_add_error(struct interface
*iface
, const char *subsystem
,
49 const char *code
, const char **data
, int n_data
)
51 struct interface_error
*error
;
57 len
= n_data
* sizeof(char *);
58 datalen
= alloca(len
);
59 for (i
= 0; i
< n_data
; i
++) {
60 datalen
[i
] = strlen(data
[i
]) + 1;
65 error
= calloc(1, sizeof(*error
) + sizeof(char *) + len
);
69 list_add_tail(&error
->list
, &iface
->errors
);
70 error
->subsystem
= subsystem
;
73 dest
= (char *) &error
->data
[n_data
+ 1];
74 for (i
= 0; i
< n_data
; i
++) {
75 error
->data
[i
] = dest
;
76 memcpy(dest
, data
[i
], datalen
[i
]);
79 error
->data
[n_data
] = NULL
;
83 interface_event(struct interface
*iface
, enum interface_event ev
)
89 mark_interface_down(struct interface
*iface
)
91 interface_del_ctx_addr(iface
, NULL
);
92 device_release(iface
->main_dev
.dev
);
93 iface
->state
= IFS_DOWN
;
97 __interface_set_up(struct interface
*iface
)
101 if (iface
->state
!= IFS_DOWN
)
104 ret
= device_claim(iface
->main_dev
.dev
);
108 iface
->state
= IFS_SETUP
;
109 ret
= interface_proto_event(iface
->proto
, PROTO_CMD_SETUP
, false);
111 mark_interface_down(iface
);
120 __interface_set_down(struct interface
*iface
, bool force
)
122 clear_interface_errors(iface
);
124 if (iface
->state
== IFS_DOWN
||
125 iface
->state
== IFS_TEARDOWN
)
128 iface
->state
= IFS_TEARDOWN
;
129 interface_event(iface
, IFEV_DOWN
);
131 interface_del_all_routes(iface
);
132 interface_proto_event(iface
->proto
, PROTO_CMD_TEARDOWN
, force
);
136 interface_cb(struct device_user
*dep
, enum device_event ev
)
138 struct interface
*iface
;
141 iface
= container_of(dep
, struct interface
, main_dev
);
146 case DEV_EVENT_REMOVE
:
153 if (iface
->active
== new_state
)
156 iface
->active
= new_state
;
159 if (iface
->autostart
&& !config_init
)
160 interface_set_up(iface
);
162 __interface_set_down(iface
, true);
166 interface_proto_cb(struct interface_proto_state
*state
, enum interface_proto_event ev
)
168 struct interface
*iface
= state
->iface
;
172 if (iface
->state
!= IFS_SETUP
)
175 iface
->state
= IFS_UP
;
176 interface_event(iface
, IFEV_UP
);
179 if (iface
->state
== IFS_DOWN
)
182 mark_interface_down(iface
);
187 void interface_set_proto_state(struct interface
*iface
, struct interface_proto_state
*state
)
190 interface_proto_event(iface
->proto
, PROTO_CMD_TEARDOWN
, true);
191 iface
->proto
->free(iface
->proto
);
194 iface
->state
= IFS_DOWN
;
195 iface
->proto
= state
;
199 state
->proto_event
= interface_proto_cb
;
200 state
->iface
= iface
;
204 interface_alloc(const char *name
, struct uci_section
*s
, struct blob_attr
*attr
)
206 struct interface
*iface
;
207 struct blob_attr
*tb
[IFACE_ATTR_MAX
];
208 struct blob_attr
*cur
;
211 iface
= interface_get(name
);
215 iface
= calloc(1, sizeof(*iface
));
216 iface
->main_dev
.cb
= interface_cb
;
217 iface
->l3_iface
= &iface
->main_dev
;
218 strncpy(iface
->name
, name
, sizeof(iface
->name
) - 1);
219 list_add_tail(&iface
->list
, &interfaces
);
220 INIT_LIST_HEAD(&iface
->errors
);
221 INIT_LIST_HEAD(&iface
->address
);
222 INIT_LIST_HEAD(&iface
->routes
);
224 proto_attach_interface(iface
, s
);
226 netifd_ubus_add_interface(iface
);
228 blobmsg_parse(iface_attrs
, IFACE_ATTR_MAX
, tb
,
229 blob_data(attr
), blob_len(attr
));
231 if ((cur
= tb
[IFACE_ATTR_IFNAME
])) {
232 dev
= device_get(blobmsg_data(cur
), true);
234 device_add_user(&iface
->main_dev
, dev
);
241 interface_free(struct interface
*iface
)
243 netifd_ubus_remove_interface(iface
);
244 list_del(&iface
->list
);
245 if (iface
->proto
->free
)
246 iface
->proto
->free(iface
->proto
);
251 interface_get(const char *name
)
253 struct interface
*iface
;
255 list_for_each_entry(iface
, &interfaces
, list
) {
256 if (!strcmp(iface
->name
, name
))
263 interface_remove_link(struct interface
*iface
, struct device
*dev
)
265 struct device
*mdev
= iface
->main_dev
.dev
;
267 if (mdev
&& mdev
->hotplug_ops
) {
268 mdev
->hotplug_ops
->del(mdev
, dev
);
272 device_remove_user(&iface
->main_dev
);
276 interface_add_link(struct interface
*iface
, struct device
*dev
)
278 struct device
*mdev
= iface
->main_dev
.dev
;
280 if (mdev
&& mdev
->hotplug_ops
)
281 return mdev
->hotplug_ops
->add(mdev
, dev
);
283 if (iface
->main_dev
.dev
)
284 interface_remove_link(iface
, NULL
);
286 device_add_user(&iface
->main_dev
, dev
);
292 interface_set_up(struct interface
*iface
)
294 iface
->autostart
= true;
296 if (!iface
->active
) {
297 interface_add_error(iface
, "interface", "NO_DEVICE", NULL
, 0);
301 if (iface
->state
!= IFS_DOWN
)
304 return __interface_set_up(iface
);
308 interface_set_down(struct interface
*iface
)
310 iface
->autostart
= false;
311 __interface_set_down(iface
, false);
317 interface_start_pending(void)
319 struct interface
*iface
;
321 list_for_each_entry(iface
, &interfaces
, list
) {
322 if (iface
->active
&& iface
->autostart
)
323 interface_set_up(iface
);