vlandev.c 11 KB

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