8 #include "interface-ip.h"
13 static LIST_HEAD(interfaces
);
22 static const struct blobmsg_policy iface_attrs
[IFACE_ATTR_MAX
] = {
23 [IFACE_ATTR_PROTO
] = { .name
= "proto", .type
= BLOBMSG_TYPE_STRING
},
24 [IFACE_ATTR_IFNAME
] = { .name
= "ifname", .type
= BLOBMSG_TYPE_STRING
},
25 [IFACE_ATTR_AUTO
] = { .name
= "auto", .type
= BLOBMSG_TYPE_BOOL
},
28 const struct config_param_list interface_attr_list
= {
29 .n_params
= IFACE_ATTR_MAX
,
30 .params
= iface_attrs
,
34 clear_interface_errors(struct interface
*iface
)
36 struct interface_error
*error
, *tmp
;
38 list_for_each_entry_safe(error
, tmp
, &iface
->errors
, list
) {
39 list_del(&error
->list
);
44 void interface_add_error(struct interface
*iface
, const char *subsystem
,
45 const char *code
, const char **data
, int n_data
)
47 struct interface_error
*error
;
53 len
= n_data
* sizeof(char *);
54 datalen
= alloca(len
);
55 for (i
= 0; i
< n_data
; i
++) {
56 datalen
[i
] = strlen(data
[i
]) + 1;
61 error
= calloc(1, sizeof(*error
) + sizeof(char *) + len
);
65 list_add_tail(&error
->list
, &iface
->errors
);
66 error
->subsystem
= subsystem
;
69 dest
= (char *) &error
->data
[n_data
+ 1];
70 for (i
= 0; i
< n_data
; i
++) {
71 error
->data
[i
] = dest
;
72 memcpy(dest
, data
[i
], datalen
[i
]);
75 error
->data
[n_data
] = NULL
;
79 interface_event(struct interface
*iface
, enum interface_event ev
)
85 mark_interface_down(struct interface
*iface
)
87 vlist_flush_all(&iface
->proto_addr
);
88 vlist_flush_all(&iface
->proto_route
);
89 device_release(&iface
->main_dev
);
90 iface
->state
= IFS_DOWN
;
94 __interface_set_up(struct interface
*iface
)
98 if (iface
->state
!= IFS_DOWN
)
101 ret
= device_claim(&iface
->main_dev
);
105 iface
->state
= IFS_SETUP
;
106 ret
= interface_proto_event(iface
->proto
, PROTO_CMD_SETUP
, false);
108 mark_interface_down(iface
);
117 __interface_set_down(struct interface
*iface
, bool force
)
119 clear_interface_errors(iface
);
121 if (iface
->state
== IFS_DOWN
||
122 iface
->state
== IFS_TEARDOWN
)
125 iface
->state
= IFS_TEARDOWN
;
126 interface_event(iface
, IFEV_DOWN
);
127 interface_proto_event(iface
->proto
, PROTO_CMD_TEARDOWN
, force
);
131 interface_cb(struct device_user
*dep
, enum device_event ev
)
133 struct interface
*iface
;
136 iface
= container_of(dep
, struct interface
, main_dev
);
141 case DEV_EVENT_REMOVE
:
148 if (iface
->available
== new_state
)
151 iface
->available
= new_state
;
154 if (iface
->autostart
&& !config_init
)
155 interface_set_up(iface
);
157 __interface_set_down(iface
, true);
161 interface_proto_cb(struct interface_proto_state
*state
, enum interface_proto_event ev
)
163 struct interface
*iface
= state
->iface
;
167 if (iface
->state
!= IFS_SETUP
)
170 iface
->state
= IFS_UP
;
171 interface_event(iface
, IFEV_UP
);
174 if (iface
->state
== IFS_DOWN
)
177 mark_interface_down(iface
);
182 void interface_set_proto_state(struct interface
*iface
, struct interface_proto_state
*state
)
185 interface_proto_event(iface
->proto
, PROTO_CMD_TEARDOWN
, true);
186 iface
->proto
->free(iface
->proto
);
189 iface
->state
= IFS_DOWN
;
190 iface
->proto
= state
;
194 state
->proto_event
= interface_proto_cb
;
195 state
->iface
= iface
;
199 interface_alloc(const char *name
, struct blob_attr
*attr
)
201 struct interface
*iface
;
202 struct blob_attr
*tb
[IFACE_ATTR_MAX
];
203 struct blob_attr
*cur
;
205 const char *proto_name
= NULL
;
207 iface
= interface_get(name
);
211 iface
= calloc(1, sizeof(*iface
));
212 iface
->main_dev
.cb
= interface_cb
;
213 iface
->l3_iface
= &iface
->main_dev
;
214 strncpy(iface
->name
, name
, sizeof(iface
->name
) - 1);
215 list_add_tail(&iface
->list
, &interfaces
);
216 INIT_LIST_HEAD(&iface
->errors
);
218 interface_ip_init(iface
);
220 blobmsg_parse(iface_attrs
, IFACE_ATTR_MAX
, tb
,
221 blob_data(attr
), blob_len(attr
));
223 if ((cur
= tb
[IFACE_ATTR_PROTO
]))
224 proto_name
= blobmsg_data(cur
);
226 proto_attach_interface(iface
, proto_name
);
228 if ((cur
= tb
[IFACE_ATTR_IFNAME
])) {
229 dev
= device_get(blobmsg_data(cur
), true);
231 device_add_user(&iface
->main_dev
, dev
);
234 if ((cur
= tb
[IFACE_ATTR_AUTO
]))
235 iface
->autostart
= blobmsg_get_bool(cur
);
237 iface
->autostart
= true;
239 netifd_ubus_add_interface(iface
);
240 config_set_state(&iface
->config
, attr
);
246 interface_free(struct interface
*iface
)
248 netifd_ubus_remove_interface(iface
);
249 list_del(&iface
->list
);
250 if (iface
->proto
->free
)
251 iface
->proto
->free(iface
->proto
);
256 interface_get(const char *name
)
258 struct interface
*iface
;
260 list_for_each_entry(iface
, &interfaces
, list
) {
261 if (!strcmp(iface
->name
, name
))
268 interface_remove_link(struct interface
*iface
, struct device
*dev
)
270 struct device
*mdev
= iface
->main_dev
.dev
;
272 if (mdev
&& mdev
->hotplug_ops
) {
273 mdev
->hotplug_ops
->del(mdev
, dev
);
277 device_remove_user(&iface
->main_dev
);
281 interface_add_link(struct interface
*iface
, struct device
*dev
)
283 struct device
*mdev
= iface
->main_dev
.dev
;
285 if (mdev
&& mdev
->hotplug_ops
)
286 return mdev
->hotplug_ops
->add(mdev
, dev
);
288 if (iface
->main_dev
.dev
)
289 interface_remove_link(iface
, NULL
);
291 device_add_user(&iface
->main_dev
, dev
);
297 interface_set_up(struct interface
*iface
)
299 iface
->autostart
= true;
301 if (!iface
->available
) {
302 interface_add_error(iface
, "interface", "NO_DEVICE", NULL
, 0);
306 if (iface
->state
!= IFS_DOWN
)
309 return __interface_set_up(iface
);
313 interface_set_down(struct interface
*iface
)
315 iface
->autostart
= false;
316 __interface_set_down(iface
, false);
322 interface_start_pending(void)
324 struct interface
*iface
;
326 list_for_each_entry(iface
, &interfaces
, list
) {
327 if (iface
->available
&& iface
->autostart
)
328 interface_set_up(iface
);