2
0

vlandev.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437
  1. /*
  2. * netifd - network interface daemon
  3. * Copyright (C) 2014 Gioacchino Mazzurco <gio@eigenlab.org>
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License version 2
  7. * as published by the Free Software Foundation
  8. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. */
  14. #include <string.h>
  15. #include <inttypes.h>
  16. #include "netifd.h"
  17. #include "device.h"
  18. #include "interface.h"
  19. #include "system.h"
  20. #include "utils.h"
  21. enum {
  22. VLANDEV_ATTR_IFNAME,
  23. VLANDEV_ATTR_VID,
  24. VLANDEV_ATTR_INGRESS_QOS_MAPPING,
  25. VLANDEV_ATTR_EGRESS_QOS_MAPPING,
  26. __VLANDEV_ATTR_MAX
  27. };
  28. static const struct blobmsg_policy vlandev_attrs[__VLANDEV_ATTR_MAX] = {
  29. [VLANDEV_ATTR_IFNAME] = { "ifname", BLOBMSG_TYPE_STRING },
  30. [VLANDEV_ATTR_VID] = { "vid", BLOBMSG_TYPE_STRING },
  31. [VLANDEV_ATTR_INGRESS_QOS_MAPPING] = { "ingress_qos_mapping", BLOBMSG_TYPE_ARRAY },
  32. [VLANDEV_ATTR_EGRESS_QOS_MAPPING] = { "egress_qos_mapping", BLOBMSG_TYPE_ARRAY },
  33. };
  34. static const struct uci_blob_param_list vlandev_attr_list = {
  35. .n_params = __VLANDEV_ATTR_MAX,
  36. .params = vlandev_attrs,
  37. .n_next = 1,
  38. .next = { &device_attr_list },
  39. };
  40. static struct device_type vlan8021q_device_type;
  41. static struct blob_buf b;
  42. struct vlandev_device {
  43. struct device dev;
  44. struct device_user parent;
  45. device_state_cb set_state;
  46. struct blob_attr *config_data;
  47. struct blob_attr *ifname;
  48. struct blob_attr *vid;
  49. struct vlandev_config config;
  50. };
  51. static int
  52. __vlandev_hotplug_op(struct device *dev, struct device *member, struct blob_attr *vlan, bool add)
  53. {
  54. struct vlandev_device *mvdev = container_of(dev, struct vlandev_device, dev);
  55. void *a;
  56. dev = mvdev->parent.dev;
  57. if (!dev || !dev->hotplug_ops)
  58. return UBUS_STATUS_NOT_SUPPORTED;
  59. blob_buf_init(&b, 0);
  60. a = blobmsg_open_array(&b, "vlans");
  61. blobmsg_printf(&b, NULL, "%d", mvdev->config.vid);
  62. blobmsg_close_array(&b, a);
  63. if (add)
  64. return dev->hotplug_ops->add(dev, member, blobmsg_data(b.head));
  65. else
  66. return dev->hotplug_ops->del(dev, member, blobmsg_data(b.head));
  67. }
  68. static int
  69. vlandev_hotplug_add(struct device *dev, struct device *member, struct blob_attr *vlan)
  70. {
  71. return __vlandev_hotplug_op(dev, member, vlan, true);
  72. }
  73. static int
  74. vlandev_hotplug_del(struct device *dev, struct device *member, struct blob_attr *vlan)
  75. {
  76. return __vlandev_hotplug_op(dev, member, vlan, false);
  77. }
  78. static int
  79. vlandev_hotplug_prepare(struct device *dev, struct device **bridge_dev)
  80. {
  81. struct vlandev_device *mvdev = container_of(dev, struct vlandev_device, dev);
  82. dev = mvdev->parent.dev;
  83. if (!dev || !dev->hotplug_ops)
  84. return UBUS_STATUS_NOT_SUPPORTED;
  85. return dev->hotplug_ops->prepare(dev, bridge_dev);
  86. }
  87. static void vlandev_hotplug_check(struct vlandev_device *mvdev)
  88. {
  89. static const struct device_hotplug_ops hotplug_ops = {
  90. .prepare = vlandev_hotplug_prepare,
  91. .add = vlandev_hotplug_add,
  92. .del = vlandev_hotplug_del
  93. };
  94. struct device *dev = mvdev->parent.dev;
  95. if (!dev || !dev->hotplug_ops || avl_is_empty(&dev->vlans.avl) ||
  96. mvdev->dev.type != &vlan8021q_device_type) {
  97. mvdev->dev.hotplug_ops = NULL;
  98. return;
  99. }
  100. mvdev->dev.hotplug_ops = &hotplug_ops;
  101. }
  102. static void
  103. vlandev_base_cb(struct device_user *dev, enum device_event ev)
  104. {
  105. struct vlandev_device *mvdev = container_of(dev, struct vlandev_device, parent);
  106. switch (ev) {
  107. case DEV_EVENT_ADD:
  108. device_set_present(&mvdev->dev, true);
  109. break;
  110. case DEV_EVENT_REMOVE:
  111. device_set_present(&mvdev->dev, false);
  112. break;
  113. case DEV_EVENT_UPDATE_IFNAME:
  114. vlandev_hotplug_check(mvdev);
  115. break;
  116. default:
  117. return;
  118. }
  119. }
  120. static int
  121. vlandev_set_down(struct vlandev_device *mvdev)
  122. {
  123. mvdev->set_state(&mvdev->dev, false);
  124. system_vlandev_del(&mvdev->dev);
  125. device_release(&mvdev->parent);
  126. return 0;
  127. }
  128. static int
  129. vlandev_set_up(struct vlandev_device *mvdev)
  130. {
  131. int ret;
  132. ret = device_claim(&mvdev->parent);
  133. if (ret < 0)
  134. return ret;
  135. ret = system_vlandev_add(&mvdev->dev, mvdev->parent.dev, &mvdev->config);
  136. if (ret < 0)
  137. goto release;
  138. ret = mvdev->set_state(&mvdev->dev, true);
  139. if (ret)
  140. goto delete;
  141. return 0;
  142. delete:
  143. system_vlandev_del(&mvdev->dev);
  144. release:
  145. device_release(&mvdev->parent);
  146. return ret;
  147. }
  148. static int
  149. vlandev_set_state(struct device *dev, bool up)
  150. {
  151. struct vlandev_device *mvdev;
  152. D(SYSTEM, "vlandev_set_state(%s, %u)\n", dev->ifname, up);
  153. mvdev = container_of(dev, struct vlandev_device, dev);
  154. if (up)
  155. return vlandev_set_up(mvdev);
  156. else
  157. return vlandev_set_down(mvdev);
  158. }
  159. static void
  160. vlandev_free(struct device *dev)
  161. {
  162. struct vlandev_device *mvdev;
  163. mvdev = container_of(dev, struct vlandev_device, dev);
  164. device_remove_user(&mvdev->parent);
  165. free(mvdev->config_data);
  166. vlist_simple_flush_all(&mvdev->config.ingress_qos_mapping_list);
  167. vlist_simple_flush_all(&mvdev->config.egress_qos_mapping_list);
  168. free(mvdev);
  169. }
  170. static void vlandev_qos_mapping_dump(struct blob_buf *b, const char *name, const struct vlist_simple_tree *qos_mapping_li)
  171. {
  172. const struct vlan_qos_mapping *elem;
  173. void *a, *t;
  174. a = blobmsg_open_array(b, name);
  175. vlist_simple_for_each_element(qos_mapping_li, elem, node) {
  176. t = blobmsg_open_table(b, NULL);
  177. blobmsg_add_u32(b, "from", elem->from);
  178. blobmsg_add_u32(b, "to", elem->to);
  179. blobmsg_close_table(b, t);
  180. }
  181. blobmsg_close_array(b, a);
  182. }
  183. static void
  184. vlandev_dump_info(struct device *dev, struct blob_buf *b)
  185. {
  186. struct vlandev_device *mvdev;
  187. mvdev = container_of(dev, struct vlandev_device, dev);
  188. blobmsg_add_string(b, "parent", mvdev->parent.dev->ifname);
  189. system_if_dump_info(dev, b);
  190. blobmsg_add_u32(b, "vid", mvdev->config.vid);
  191. vlandev_qos_mapping_dump(b, "ingress_qos_mapping", &mvdev->config.ingress_qos_mapping_list);
  192. vlandev_qos_mapping_dump(b, "egress_qos_mapping", &mvdev->config.egress_qos_mapping_list);
  193. }
  194. static uint16_t
  195. vlandev_get_vid(struct device *dev, const char *id_str)
  196. {
  197. unsigned long id;
  198. uint16_t *alias_id;
  199. char *err;
  200. id = strtoul(id_str, &err, 10);
  201. if (err && *err) {
  202. if (!dev)
  203. return 1;
  204. alias_id = kvlist_get(&dev->vlan_aliases, id_str);
  205. if (!alias_id)
  206. return 1;
  207. id = *alias_id;
  208. }
  209. return (uint16_t)id;
  210. }
  211. static void
  212. vlandev_config_init(struct device *dev)
  213. {
  214. struct vlandev_device *mvdev;
  215. struct device *basedev = NULL;
  216. mvdev = container_of(dev, struct vlandev_device, dev);
  217. if (mvdev->ifname)
  218. basedev = device_get(blobmsg_data(mvdev->ifname), true);
  219. if (mvdev->vid)
  220. mvdev->config.vid = vlandev_get_vid(basedev, blobmsg_get_string(mvdev->vid));
  221. else
  222. mvdev->config.vid = 1;
  223. device_add_user(&mvdev->parent, basedev);
  224. vlandev_hotplug_check(mvdev);
  225. }
  226. static void vlandev_qos_mapping_list_apply(struct vlist_simple_tree *qos_mapping_li, struct blob_attr *list)
  227. {
  228. struct blob_attr *cur;
  229. struct vlan_qos_mapping *qos_mapping;
  230. int rem, rc;
  231. blobmsg_for_each_attr(cur, list, rem) {
  232. if (blobmsg_type(cur) != BLOBMSG_TYPE_STRING)
  233. continue;
  234. if (!blobmsg_check_attr(cur, false))
  235. continue;
  236. qos_mapping = calloc(1, sizeof(*qos_mapping));
  237. if (!qos_mapping)
  238. continue;
  239. rc = sscanf(blobmsg_data(cur), "%" PRIu32 ":%" PRIu32, &qos_mapping->from, &qos_mapping->to);
  240. if (rc != 2) {
  241. free(qos_mapping);
  242. continue;
  243. }
  244. vlist_simple_add(qos_mapping_li, &qos_mapping->node);
  245. }
  246. }
  247. static void
  248. vlandev_apply_settings(struct vlandev_device *mvdev, struct blob_attr **tb)
  249. {
  250. struct vlandev_config *cfg = &mvdev->config;
  251. struct blob_attr *cur;
  252. cfg->proto = (mvdev->dev.type == &vlan8021q_device_type) ?
  253. VLAN_PROTO_8021Q : VLAN_PROTO_8021AD;
  254. vlist_simple_update(&cfg->ingress_qos_mapping_list);
  255. vlist_simple_update(&cfg->egress_qos_mapping_list);
  256. if ((cur = tb[VLANDEV_ATTR_INGRESS_QOS_MAPPING]))
  257. vlandev_qos_mapping_list_apply(&cfg->ingress_qos_mapping_list, cur);
  258. if ((cur = tb[VLANDEV_ATTR_EGRESS_QOS_MAPPING]))
  259. vlandev_qos_mapping_list_apply(&cfg->egress_qos_mapping_list, cur);
  260. vlist_simple_flush(&cfg->ingress_qos_mapping_list);
  261. vlist_simple_flush(&cfg->egress_qos_mapping_list);
  262. }
  263. static enum dev_change_type
  264. vlandev_reload(struct device *dev, struct blob_attr *attr)
  265. {
  266. struct blob_attr *tb_dev[__DEV_ATTR_MAX];
  267. struct blob_attr *tb_mv[__VLANDEV_ATTR_MAX];
  268. enum dev_change_type ret = DEV_CONFIG_APPLIED;
  269. struct vlandev_device *mvdev;
  270. mvdev = container_of(dev, struct vlandev_device, dev);
  271. attr = blob_memdup(attr);
  272. blobmsg_parse(device_attr_list.params, __DEV_ATTR_MAX, tb_dev,
  273. blob_data(attr), blob_len(attr));
  274. blobmsg_parse(vlandev_attrs, __VLANDEV_ATTR_MAX, tb_mv,
  275. blob_data(attr), blob_len(attr));
  276. device_init_settings(dev, tb_dev);
  277. vlandev_apply_settings(mvdev, tb_mv);
  278. mvdev->ifname = tb_mv[VLANDEV_ATTR_IFNAME];
  279. mvdev->vid = tb_mv[VLANDEV_ATTR_VID];
  280. if (mvdev->config_data) {
  281. struct blob_attr *otb_dev[__DEV_ATTR_MAX];
  282. struct blob_attr *otb_mv[__VLANDEV_ATTR_MAX];
  283. blobmsg_parse(device_attr_list.params, __DEV_ATTR_MAX, otb_dev,
  284. blob_data(mvdev->config_data), blob_len(mvdev->config_data));
  285. if (uci_blob_diff(tb_dev, otb_dev, &device_attr_list, NULL))
  286. ret = DEV_CONFIG_RESTART;
  287. blobmsg_parse(vlandev_attrs, __VLANDEV_ATTR_MAX, otb_mv,
  288. blob_data(mvdev->config_data), blob_len(mvdev->config_data));
  289. if (uci_blob_diff(tb_mv, otb_mv, &vlandev_attr_list, NULL))
  290. ret = DEV_CONFIG_RESTART;
  291. vlandev_config_init(dev);
  292. }
  293. free(mvdev->config_data);
  294. mvdev->config_data = attr;
  295. return ret;
  296. }
  297. static struct device *
  298. vlandev_create(const char *name, struct device_type *devtype,
  299. struct blob_attr *attr)
  300. {
  301. struct vlandev_device *mvdev;
  302. struct device *dev = NULL;
  303. mvdev = calloc(1, sizeof(*mvdev));
  304. if (!mvdev)
  305. return NULL;
  306. vlist_simple_init(&mvdev->config.ingress_qos_mapping_list,
  307. struct vlan_qos_mapping, node);
  308. vlist_simple_init(&mvdev->config.egress_qos_mapping_list,
  309. struct vlan_qos_mapping, node);
  310. dev = &mvdev->dev;
  311. if (device_init(dev, devtype, name) < 0) {
  312. device_cleanup(dev);
  313. free(mvdev);
  314. return NULL;
  315. }
  316. dev->config_pending = true;
  317. mvdev->set_state = dev->set_state;
  318. dev->set_state = vlandev_set_state;
  319. dev->hotplug_ops = NULL;
  320. mvdev->parent.cb = vlandev_base_cb;
  321. vlandev_reload(dev, attr);
  322. return dev;
  323. }
  324. static struct device_type vlan8021ad_device_type = {
  325. .name = "8021ad",
  326. .config_params = &vlandev_attr_list,
  327. .create = vlandev_create,
  328. .config_init = vlandev_config_init,
  329. .reload = vlandev_reload,
  330. .free = vlandev_free,
  331. .dump_info = vlandev_dump_info,
  332. };
  333. static struct device_type vlan8021q_device_type = {
  334. .name = "8021q",
  335. .config_params = &vlandev_attr_list,
  336. .create = vlandev_create,
  337. .config_init = vlandev_config_init,
  338. .reload = vlandev_reload,
  339. .free = vlandev_free,
  340. .dump_info = vlandev_dump_info,
  341. };
  342. static void __init vlandev_device_type_init(void)
  343. {
  344. device_type_add(&vlan8021ad_device_type);
  345. device_type_add(&vlan8021q_device_type);
  346. }