8 #include "interface-ip.h"
13 static LIST_HEAD(interfaces
);
22 static const union config_param_info iface_attr_info
[IFACE_ATTR_MAX
] = {
23 [IFACE_ATTR_IFNAME
].type
= BLOBMSG_TYPE_STRING
,
26 static const struct blobmsg_policy iface_attrs
[IFACE_ATTR_MAX
] = {
27 [IFACE_ATTR_PROTO
] = { .name
= "proto", .type
= BLOBMSG_TYPE_STRING
},
28 [IFACE_ATTR_IFNAME
] = { .name
= "ifname", .type
= BLOBMSG_TYPE_STRING
},
29 [IFACE_ATTR_AUTO
] = { .name
= "auto", .type
= BLOBMSG_TYPE_BOOL
},
32 const struct config_param_list interface_attr_list
= {
33 .n_params
= IFACE_ATTR_MAX
,
34 .params
= iface_attrs
,
35 .info
= iface_attr_info
,
39 clear_interface_errors(struct interface
*iface
)
41 struct interface_error
*error
, *tmp
;
43 list_for_each_entry_safe(error
, tmp
, &iface
->errors
, list
) {
44 list_del(&error
->list
);
49 void interface_add_error(struct interface
*iface
, const char *subsystem
,
50 const char *code
, const char **data
, int n_data
)
52 struct interface_error
*error
;
58 len
= n_data
* sizeof(char *);
59 datalen
= alloca(len
);
60 for (i
= 0; i
< n_data
; i
++) {
61 datalen
[i
] = strlen(data
[i
]) + 1;
66 error
= calloc(1, sizeof(*error
) + sizeof(char *) + len
);
70 list_add_tail(&error
->list
, &iface
->errors
);
71 error
->subsystem
= subsystem
;
74 dest
= (char *) &error
->data
[n_data
+ 1];
75 for (i
= 0; i
< n_data
; i
++) {
76 error
->data
[i
] = dest
;
77 memcpy(dest
, data
[i
], datalen
[i
]);
80 error
->data
[n_data
] = NULL
;
84 interface_event(struct interface
*iface
, enum interface_event ev
)
90 mark_interface_down(struct interface
*iface
)
92 vlist_flush_all(&iface
->proto_addr
);
93 vlist_flush_all(&iface
->proto_route
);
94 device_release(&iface
->main_dev
);
95 iface
->state
= IFS_DOWN
;
99 __interface_set_up(struct interface
*iface
)
103 if (iface
->state
!= IFS_DOWN
)
106 ret
= device_claim(&iface
->main_dev
);
110 iface
->state
= IFS_SETUP
;
111 ret
= interface_proto_event(iface
->proto
, PROTO_CMD_SETUP
, false);
113 mark_interface_down(iface
);
122 __interface_set_down(struct interface
*iface
, bool force
)
124 clear_interface_errors(iface
);
126 if (iface
->state
== IFS_DOWN
||
127 iface
->state
== IFS_TEARDOWN
)
130 iface
->state
= IFS_TEARDOWN
;
131 interface_event(iface
, IFEV_DOWN
);
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 blob_attr
*attr
)
206 struct interface
*iface
;
207 struct blob_attr
*tb
[IFACE_ATTR_MAX
];
208 struct blob_attr
*cur
;
210 const char *proto_name
= NULL
;
212 iface
= interface_get(name
);
216 iface
= calloc(1, sizeof(*iface
));
217 iface
->main_dev
.cb
= interface_cb
;
218 iface
->l3_iface
= &iface
->main_dev
;
219 strncpy(iface
->name
, name
, sizeof(iface
->name
) - 1);
220 list_add_tail(&iface
->list
, &interfaces
);
221 INIT_LIST_HEAD(&iface
->errors
);
223 interface_ip_init(iface
);
225 blobmsg_parse(iface_attrs
, IFACE_ATTR_MAX
, tb
,
226 blob_data(attr
), blob_len(attr
));
228 if ((cur
= tb
[IFACE_ATTR_PROTO
]))
229 proto_name
= blobmsg_data(cur
);
231 proto_attach_interface(iface
, proto_name
);
233 if ((cur
= tb
[IFACE_ATTR_IFNAME
])) {
234 dev
= device_get(blobmsg_data(cur
), true);
236 device_add_user(&iface
->main_dev
, dev
);
239 if ((cur
= tb
[IFACE_ATTR_AUTO
]))
240 iface
->autostart
= blobmsg_get_bool(cur
);
242 iface
->autostart
= true;
244 netifd_ubus_add_interface(iface
);
245 config_set_state(&iface
->config
, attr
);
251 interface_free(struct interface
*iface
)
253 netifd_ubus_remove_interface(iface
);
254 list_del(&iface
->list
);
255 if (iface
->proto
->free
)
256 iface
->proto
->free(iface
->proto
);
261 interface_get(const char *name
)
263 struct interface
*iface
;
265 list_for_each_entry(iface
, &interfaces
, list
) {
266 if (!strcmp(iface
->name
, name
))
273 interface_remove_link(struct interface
*iface
, struct device
*dev
)
275 struct device
*mdev
= iface
->main_dev
.dev
;
277 if (mdev
&& mdev
->hotplug_ops
) {
278 mdev
->hotplug_ops
->del(mdev
, dev
);
282 device_remove_user(&iface
->main_dev
);
286 interface_add_link(struct interface
*iface
, struct device
*dev
)
288 struct device
*mdev
= iface
->main_dev
.dev
;
290 if (mdev
&& mdev
->hotplug_ops
)
291 return mdev
->hotplug_ops
->add(mdev
, dev
);
293 if (iface
->main_dev
.dev
)
294 interface_remove_link(iface
, NULL
);
296 device_add_user(&iface
->main_dev
, dev
);
302 interface_set_up(struct interface
*iface
)
304 iface
->autostart
= true;
306 if (!iface
->active
) {
307 interface_add_error(iface
, "interface", "NO_DEVICE", NULL
, 0);
311 if (iface
->state
!= IFS_DOWN
)
314 return __interface_set_up(iface
);
318 interface_set_down(struct interface
*iface
)
320 iface
->autostart
= false;
321 __interface_set_down(iface
, false);
327 interface_start_pending(void)
329 struct interface
*iface
;
331 list_for_each_entry(iface
, &interfaces
, list
) {
332 if (iface
->active
&& iface
->autostart
)
333 interface_set_up(iface
);