vlandev.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442
  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. case DEV_EVENT_TOPO_CHANGE:
  117. /* Propagate topo changes */
  118. device_broadcast_event(&mvdev->dev, DEV_EVENT_TOPO_CHANGE);
  119. break;
  120. default:
  121. return;
  122. }
  123. }
  124. static int
  125. vlandev_set_down(struct vlandev_device *mvdev)
  126. {
  127. mvdev->set_state(&mvdev->dev, false);
  128. system_vlandev_del(&mvdev->dev);
  129. device_release(&mvdev->parent);
  130. return 0;
  131. }
  132. static int
  133. vlandev_set_up(struct vlandev_device *mvdev)
  134. {
  135. int ret;
  136. ret = device_claim(&mvdev->parent);
  137. if (ret < 0)
  138. return ret;
  139. ret = system_vlandev_add(&mvdev->dev, mvdev->parent.dev, &mvdev->config);
  140. if (ret < 0)
  141. goto release;
  142. ret = mvdev->set_state(&mvdev->dev, true);
  143. if (ret)
  144. goto delete;
  145. return 0;
  146. delete:
  147. system_vlandev_del(&mvdev->dev);
  148. release:
  149. device_release(&mvdev->parent);
  150. return ret;
  151. }
  152. static int
  153. vlandev_set_state(struct device *dev, bool up)
  154. {
  155. struct vlandev_device *mvdev;
  156. D(SYSTEM, "vlandev_set_state(%s, %u)\n", dev->ifname, up);
  157. mvdev = container_of(dev, struct vlandev_device, dev);
  158. if (up)
  159. return vlandev_set_up(mvdev);
  160. else
  161. return vlandev_set_down(mvdev);
  162. }
  163. static void
  164. vlandev_free(struct device *dev)
  165. {
  166. struct vlandev_device *mvdev;
  167. mvdev = container_of(dev, struct vlandev_device, dev);
  168. device_remove_user(&mvdev->parent);
  169. free(mvdev->config_data);
  170. vlist_simple_flush_all(&mvdev->config.ingress_qos_mapping_list);
  171. vlist_simple_flush_all(&mvdev->config.egress_qos_mapping_list);
  172. free(mvdev);
  173. }
  174. static void vlandev_qos_mapping_dump(struct blob_buf *b, const char *name, const struct vlist_simple_tree *qos_mapping_li)
  175. {
  176. const struct vlan_qos_mapping *elem;
  177. void *a, *t;
  178. a = blobmsg_open_array(b, name);
  179. vlist_simple_for_each_element(qos_mapping_li, elem, node) {
  180. t = blobmsg_open_table(b, NULL);
  181. blobmsg_add_u32(b, "from", elem->from);
  182. blobmsg_add_u32(b, "to", elem->to);
  183. blobmsg_close_table(b, t);
  184. }
  185. blobmsg_close_array(b, a);
  186. }
  187. static void
  188. vlandev_dump_info(struct device *dev, struct blob_buf *b)
  189. {
  190. struct vlandev_device *mvdev;
  191. mvdev = container_of(dev, struct vlandev_device, dev);
  192. blobmsg_add_string(b, "parent", mvdev->parent.dev->ifname);
  193. system_if_dump_info(dev, b);
  194. blobmsg_add_u32(b, "vid", mvdev->config.vid);
  195. vlandev_qos_mapping_dump(b, "ingress_qos_mapping", &mvdev->config.ingress_qos_mapping_list);
  196. vlandev_qos_mapping_dump(b, "egress_qos_mapping", &mvdev->config.egress_qos_mapping_list);
  197. }
  198. static uint16_t
  199. vlandev_get_vid(struct device *dev, const char *id_str)
  200. {
  201. unsigned long id;
  202. uint16_t *alias_id;
  203. char *err;
  204. id = strtoul(id_str, &err, 10);
  205. if (err && *err) {
  206. if (!dev)
  207. return 1;
  208. alias_id = kvlist_get(&dev->vlan_aliases, id_str);
  209. if (!alias_id)
  210. return 1;
  211. id = *alias_id;
  212. }
  213. return (uint16_t)id;
  214. }
  215. static void
  216. vlandev_config_init(struct device *dev)
  217. {
  218. struct vlandev_device *mvdev;
  219. struct device *basedev = NULL;
  220. mvdev = container_of(dev, struct vlandev_device, dev);
  221. if (mvdev->ifname)
  222. basedev = device_get(blobmsg_data(mvdev->ifname), true);
  223. if (mvdev->vid)
  224. mvdev->config.vid = vlandev_get_vid(basedev, blobmsg_get_string(mvdev->vid));
  225. else
  226. mvdev->config.vid = 1;
  227. device_add_user(&mvdev->parent, basedev);
  228. vlandev_hotplug_check(mvdev);
  229. }
  230. static void vlandev_qos_mapping_list_apply(struct vlist_simple_tree *qos_mapping_li, struct blob_attr *list)
  231. {
  232. struct blob_attr *cur;
  233. struct vlan_qos_mapping *qos_mapping;
  234. size_t rem;
  235. int rc;
  236. blobmsg_for_each_attr(cur, list, rem) {
  237. if (blobmsg_type(cur) != BLOBMSG_TYPE_STRING)
  238. continue;
  239. if (!blobmsg_check_attr(cur, false))
  240. continue;
  241. qos_mapping = calloc(1, sizeof(*qos_mapping));
  242. if (!qos_mapping)
  243. continue;
  244. rc = sscanf(blobmsg_data(cur), "%" PRIu32 ":%" PRIu32, &qos_mapping->from, &qos_mapping->to);
  245. if (rc != 2) {
  246. free(qos_mapping);
  247. continue;
  248. }
  249. vlist_simple_add(qos_mapping_li, &qos_mapping->node);
  250. }
  251. }
  252. static void
  253. vlandev_apply_settings(struct vlandev_device *mvdev, struct blob_attr **tb)
  254. {
  255. struct vlandev_config *cfg = &mvdev->config;
  256. struct blob_attr *cur;
  257. cfg->proto = (mvdev->dev.type == &vlan8021q_device_type) ?
  258. VLAN_PROTO_8021Q : VLAN_PROTO_8021AD;
  259. vlist_simple_update(&cfg->ingress_qos_mapping_list);
  260. vlist_simple_update(&cfg->egress_qos_mapping_list);
  261. if ((cur = tb[VLANDEV_ATTR_INGRESS_QOS_MAPPING]))
  262. vlandev_qos_mapping_list_apply(&cfg->ingress_qos_mapping_list, cur);
  263. if ((cur = tb[VLANDEV_ATTR_EGRESS_QOS_MAPPING]))
  264. vlandev_qos_mapping_list_apply(&cfg->egress_qos_mapping_list, cur);
  265. vlist_simple_flush(&cfg->ingress_qos_mapping_list);
  266. vlist_simple_flush(&cfg->egress_qos_mapping_list);
  267. }
  268. static enum dev_change_type
  269. vlandev_reload(struct device *dev, struct blob_attr *attr)
  270. {
  271. struct blob_attr *tb_dev[__DEV_ATTR_MAX];
  272. struct blob_attr *tb_mv[__VLANDEV_ATTR_MAX];
  273. enum dev_change_type ret = DEV_CONFIG_APPLIED;
  274. struct vlandev_device *mvdev;
  275. mvdev = container_of(dev, struct vlandev_device, dev);
  276. attr = blob_memdup(attr);
  277. blobmsg_parse(device_attr_list.params, __DEV_ATTR_MAX, tb_dev,
  278. blob_data(attr), blob_len(attr));
  279. blobmsg_parse(vlandev_attrs, __VLANDEV_ATTR_MAX, tb_mv,
  280. blob_data(attr), blob_len(attr));
  281. device_init_settings(dev, tb_dev);
  282. vlandev_apply_settings(mvdev, tb_mv);
  283. mvdev->ifname = tb_mv[VLANDEV_ATTR_IFNAME];
  284. mvdev->vid = tb_mv[VLANDEV_ATTR_VID];
  285. if (mvdev->config_data) {
  286. struct blob_attr *otb_dev[__DEV_ATTR_MAX];
  287. struct blob_attr *otb_mv[__VLANDEV_ATTR_MAX];
  288. blobmsg_parse(device_attr_list.params, __DEV_ATTR_MAX, otb_dev,
  289. blob_data(mvdev->config_data), blob_len(mvdev->config_data));
  290. if (uci_blob_diff(tb_dev, otb_dev, &device_attr_list, NULL))
  291. ret = DEV_CONFIG_RESTART;
  292. blobmsg_parse(vlandev_attrs, __VLANDEV_ATTR_MAX, otb_mv,
  293. blob_data(mvdev->config_data), blob_len(mvdev->config_data));
  294. if (uci_blob_diff(tb_mv, otb_mv, &vlandev_attr_list, NULL))
  295. ret = DEV_CONFIG_RESTART;
  296. vlandev_config_init(dev);
  297. }
  298. free(mvdev->config_data);
  299. mvdev->config_data = attr;
  300. return ret;
  301. }
  302. static struct device *
  303. vlandev_create(const char *name, struct device_type *devtype,
  304. struct blob_attr *attr)
  305. {
  306. struct vlandev_device *mvdev;
  307. struct device *dev = NULL;
  308. mvdev = calloc(1, sizeof(*mvdev));
  309. if (!mvdev)
  310. return NULL;
  311. vlist_simple_init(&mvdev->config.ingress_qos_mapping_list,
  312. struct vlan_qos_mapping, node);
  313. vlist_simple_init(&mvdev->config.egress_qos_mapping_list,
  314. struct vlan_qos_mapping, node);
  315. dev = &mvdev->dev;
  316. if (device_init(dev, devtype, name) < 0) {
  317. device_cleanup(dev);
  318. free(mvdev);
  319. return NULL;
  320. }
  321. dev->config_pending = true;
  322. mvdev->set_state = dev->set_state;
  323. dev->set_state = vlandev_set_state;
  324. dev->hotplug_ops = NULL;
  325. mvdev->parent.cb = vlandev_base_cb;
  326. vlandev_reload(dev, attr);
  327. return dev;
  328. }
  329. static struct device_type vlan8021ad_device_type = {
  330. .name = "8021ad",
  331. .config_params = &vlandev_attr_list,
  332. .create = vlandev_create,
  333. .config_init = vlandev_config_init,
  334. .reload = vlandev_reload,
  335. .free = vlandev_free,
  336. .dump_info = vlandev_dump_info,
  337. };
  338. static struct device_type vlan8021q_device_type = {
  339. .name = "8021q",
  340. .config_params = &vlandev_attr_list,
  341. .create = vlandev_create,
  342. .config_init = vlandev_config_init,
  343. .reload = vlandev_reload,
  344. .free = vlandev_free,
  345. .dump_info = vlandev_dump_info,
  346. };
  347. static void __init vlandev_device_type_init(void)
  348. {
  349. device_type_add(&vlan8021ad_device_type);
  350. device_type_add(&vlan8021q_device_type);
  351. }